Hi, I’m trying to switch camera during call but I cannot make it work.
on Chrome calling another client with Chrome it works well but not if I try it when in the call there are Chrome and Mobile app.
In my case Chrome send a socket.io event that triggers mobile app and run the switch code for pass from front camera to back one.
I warn you that I’m a little bit noob about WebRTC so it can depends on that.
Here my code:
The pc class
/**
* Starting the call
* @param {Boolean} isCaller
* @param {Object} config - configuration for the call {audio: boolean, video: boolean}
*/
start(isCaller, config) {
this.mediaDevice
.on('stream', stream => {
//addStream and createOffer() to start the call
}
})
.on('switch', streamsObj => {
const {oldStream, newStream} = streamsObj;
console.log('switch', streamsObj);
this.pc.removeStream(oldStream);
this.pc.addStream(newStream);
this.emit('localStream', newStream); //here I only set the stream video to the RTCView component
this.createOffer();
})
.start(config);
return this;
}
The mediaDevice method:
switch() {
if (this.stream) {
this.stream.getTracks().forEach(track => {
track.stop();
});
this.stream.release();
this.numCamera = (this.numCamera + 1) % this.cameras.length;
console.log('next camera', this.cameras[this.numCamera]);
const constraints = {
video: {
deviceId: this.cameras[this.numCamera].deviceId,
height: {min: 360, ideal: 720, max: 1080},
},
audio: true,
};
mediaDevices
.getUserMedia(constraints)
.then(stream => {
// this.stream = stream;
stream.getTracks().forEach(track => {
this.stream.addTrack(track);
});
const oldS = {...this.stream};
this.stream = stream;
this.emit('switch', {oldStream: oldS, newStream: this.stream});
})
.catch(err => console.log(err));
}
return this;
}
When I run this code on the mobile app the cam swap from front to back but on Chrome client the peer video remains black. During last days I tried lot’s of different methods but still remain on the “stop stream - getUserMedia - addStream(new stream) - createOffer” but never worked and sometimes it throws error on Chrome client like " Failed to execute ‘createAnswer’ on ‘RTCPeerConnection’: PeerConnection cannot create an answer in a state other than have-remote-offer or have-local-pranswer. "
I don’t know how to move, even if you say to me that in the mobile app my code works and the problem is on the Chrome side it would be useful !
Thanks <3