Changing a MediaStream of RTCPeerConnection

前端 未结 2 1670
礼貌的吻别
礼貌的吻别 2020-12-13 10:32

I want to change from a audio/video stream to a \"screensharing\" stream:

peerConnection.removeStream(streamA) // __o_j_sep... in Screenshots below
peerCon         


        
2条回答
  •  别那么骄傲
    2020-12-13 11:07

    I do something similar, on clicking a button I remove the active stream and add the other.

    This is the way I do it and it works for me perfectly,

    _this.rtc.localstream.stop();
    _this.rtc.pc.removeStream(_this.rtc.localstream);
    
    gotStream = function (localstream_aud){
    var constraints_audio={
        audio:true
       }
    
    _this.rtc.localstream_aud = localstream_aud;
    _this.rtc.mediaConstraints= constraints_audio;   
    _this.rtc.createOffer();
    }
    getUserMedia(constraints_audio, gotStream);
    
    gotStream = function (localstream){
    var constraints_screen={
            audio:false,
            video:{
                mandatory:{
                    chromeMediaSource: 'screen'
                }
            }
        }
      _this.rtc.localstream = localstream;
      _this.rtc.mediaConstraints=constraints_video;
    
    
      _this.rtc.createStream();  
      _this.rtc.createOffer();
    }
    getUserMedia(constraints_video, gotStream);
    

    Chrome doesn't allow audio along with the 'screen' so I create a separate stream for it. You will need to do the opposite in order to switch back to your older video stream or actually to any other stream you want.

    Hope this helps

提交回复
热议问题