Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebRTC - change video stream in the middle of communication

My goal is to enable screen sharing in the middle of a video or audio call using webrtc web application .

Well I found that I can use MediaStreamTrack.applyConstraints() to change video property but is it possible to change the video source ? further more how can I add video to an existing audio stream .

I need this to work on chrome only for now .

like image 277
Abdullah Al Noman Avatar asked Mar 16 '17 04:03

Abdullah Al Noman


2 Answers

As of today, I feel a better approach for your case would be to use the RTCRtpSender.replaceTrack method.

Assuming your camera stream is "camStream", you can obtain the required RTCRtpSender object using the following:

var camVideoTrack = camStream.getVideoTracks()[0];
var camAudioTrack = camStream.getAudioTracks()[0];
var videoSender = peerConnection.addTrack(camVideoTrack, camStream);
var audioSender = peerConnection.addTrack(camAudioTrack, camStream);

...

The last two lines add video and audio capability to the connection.

...

Assuming your screen stream is "screenStream", then you can switch from camera to screen share video like this:

var screenVideoTrack = screenStream.getVideoTracks()[0];
videoSender.replaceTrack(screenVideoTrack);

...

No need to replace audio track since we are only interested in changing the visual while keeping the audio input the same.

Using this approach has the benefit of not requiring peer renegotiation to switch the video sources.

Another benefit of this approach is that you don't need to stop your camStream. When you are done sharing your screen, you can switch back the video source using:

videoSender.replaceTrack(camStream.getVideoTracks()[0]);

You can check out the documentation for replaceTrack here

I have a working webrtc meetings solution which supports screen sharing and screen recording using these similar steps. you can check it out here

It works out of the box on firefox, but to make it work on chrome, you need to enable "Experimental Web Platform" flag (go to chrome://flags/ )

like image 111
Anthony Anyanwu Avatar answered Oct 02 '22 23:10

Anthony Anyanwu


localStream.stop();
peerconnection.removeStream(localStream);

I was able to find the solution by following steps

  1. remove the current stream

  2. add new stream

  3. create new offer

Note that removeStream is deprecated and no longer in the spec, and not implemented in all browsers. E.g. this won't work in Firefox. stream.stop() is also deprecated in favor of stream.getTracks().forEach(track => track.stop())

like image 36
Abdullah Al Noman Avatar answered Oct 03 '22 01:10

Abdullah Al Noman