I want to be able to switch the camera in the middle of a call using WebRTC, without having to redo the call.
Suppose we have 2 mediaSources objects using the MediaStreamTrack.getSources method:
{ id: "id_source_1" | "id_source_2", facing: "user" | "environment", kind: "kind_1" | "kind_2", label: "label_1" | "label_2" }
We start the call with "source_1" (referring to the "user"), and we want the user to be able to switch to "source_2" (before the "environment").
In my current code, when the user presses the "camera switch" button, the following is performed: ( callingSession - current WebRTC session)
var mediaParams = { audio: true, video: { deviceId : source_2.id}, options: { muted: true, mirror: true }, elemId: 'localVideo' }; callingSession.getUserMedia(mediaParams, function (error, stream) { if (error) { console.error('error getting user media'); } else { var oldVideoTracks = callingSession.localStream.getVideoTracks(); var newVideoTracks = stream.getVideoTracks(); if (oldVideoTracks.length > 0 && newVideoTracks.length > 0) { callingSession.localStream.removeTrack(oldVideoTracks[0]); callingSession.localStream.addTrack(newVideoTracks[0]); } } });
As you can see, the mediaParams restrictions mediaParams now set to "source_2", we pass this mediaParams with the new restrictions to the getUserMedia method. Then we get the video tracks from both the old and the new stream.
The main problem in this code is that the old stream still exactly matches the new stream, even with the new restrictions passed to the getUserMedia method, so obviously the same video tracks, and of course, nothing happens, and the camera does not switch !!!
Am I doing something wrong in this code? Is there any way to switch the camera without re-arranging the call in WebRTC? what about the experimental applyConstraint() method I can't see it in chrome?
Thanks.
UPDATE My WebRTC application is an ionic application with crosswalk => chrome webview.