Search code examples
javascriptgoogle-chromevideo-streamingwebrtcaudio-streaming

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 .


Solution

  • 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())