how to activate the rear camera

20 views
Skip to first unread message

Abdoulaye sidibe

unread,
Jun 27, 2024, 6:15:02 AMJun 27
to discuss-webrtc
how to activate the rear camera? When I launch the activation, the screen remains frozen
code : 

document.getElementById('camera-rotate').addEventListener('click', async (e) => {
e.preventDefault();

let elem = document.getElementById('camera-rotate');
let usingRearCamera = elem.getAttribute('data-using-rear-camera') === 'true';
let onPath = elem.querySelector('path.on');
let offPath = elem.querySelector('path.off');

// if (myStream) {
// myStream.getTracks().forEach(track => track.stop());
// }

try {

const desiredCamera = usingRearCamera ? "user" : "environment";

if (desiredCamera) {

const constraints = {
video: {
facingMode: { exact: desiredCamera } // Utilisez 'ideal' pour éviter les erreurs si la caméra n'est pas disponible
},
audio: true // Optionnel, si vous avez besoin de l'audio aussi
};

const stream = await navigator.mediaDevices.getUserMedia(constraints);

console.log('------------------ streamng----------------');
console.log(stream);
myStream = stream;

// Diffuse les nouvelles pistes vidéo et audio
broadcastNewCameraTracks(myStream, 'video', true);

// Met à jour l'état de la caméra et l'interface utilisateur
usingRearCamera = !usingRearCamera;
elem.setAttribute('data-using-rear-camera', usingRearCamera);

if (usingRearCamera) {
onPath.classList.remove('on');
onPath.classList.add('off');
offPath.classList.remove('off');
offPath.classList.add('on');
elem.classList.add('camera-off');
elem.setAttribute('title', 'Activer la caméra avant');
} else {

onPath.classList.remove('off');
onPath.classList.add('on');
offPath.classList.remove('on');
offPath.classList.add('off');

elem.setAttribute('title', 'Deactivate Rear Camera');
elem.classList.remove('camera-off');

}

} else {
console.error('No appropriate camera found.');
}
} catch (error) {
console.error('Error accessing camera:', error);
}
});

function broadcastNewCameraTracks(stream, type, mirrorMode = true) {

h.setLocalStream(stream, mirrorMode);

let track = type == 'audio' ? stream.getAudioTracks()[0] : stream.getVideoTracks()[0];

for (let p in pc) {
if (pc.hasOwnProperty(p)) {
let pName = pc[p];
if (typeof pName == 'object' && pName.replaceTrack) {
pName.replaceTrack(track);
}
}
}
}
Reply all
Reply to author
Forward
0 new messages