Goodnight M
I use a webrtc application and I want to switch between the two cameras but when I try to activate, nothing happens and the camera is not displayed
function broadcastNewCameraTracks(stream, type, mirrorMode = true) {
h.setLocalStream(stream, mirrorMode);
let track = type == 'audio' ? stream.getAudioTracks()[0] : s tream.getVideoTracks()[0];
for (let p in pc) {
if (pc.hasOwnProperty(p)) {
let pName = pc[p];
if (typeof pName == 'object' && pName.replaceTrack) {
pName.replaceTrack(track);
}
}
}
}
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: {
// width: { max: 800 },
// height: { max: 720 },
// frameRate: { ideal: 15 },
// bitrate: 500000, // Réduire le débit binaire à 500 Kbps
facingMode: { ideal: 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);
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);
}
});
New contributor
abdoulaye sidibe is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.