WebRTC pauses and resumes flow

I am trying to use WebRTC to create a web application that should pause / resume the video / audio stream when some events are fired. I tried getTracks()[0].stop() , but I don't know how to resume the stream.

+5
source share
2 answers

getTracks()[0].stop() is constant.

Use getTracks()[0].enabled = false instead. To disable getTracks()[0].enabled = true .

This will replace your video with black and your audio will be quiet.

Try it (use https script for Chrome):

 var pc1 = new RTCPeerConnection(), pc2 = new RTCPeerConnection(); navigator.mediaDevices.getUserMedia({ video: true, audio: true }) .then(stream => pc1.addStream(video1.srcObject = stream)) .catch(log); var mute = () => video1.srcObject.getTracks().forEach(t => t.enabled = !t.enabled); var add = (pc, can) => can && pc.addIceCandidate(can).catch(log); pc1.onicecandidate = e => add(pc2, e.candidate); pc2.onicecandidate = e => add(pc1, e.candidate); pc2.onaddstream = e => video2.srcObject = e.stream; pc1.onnegotiationneeded = e => pc1.createOffer().then(d => pc1.setLocalDescription(d)) .then(() => pc2.setRemoteDescription(pc1.localDescription)) .then(() => pc2.createAnswer()).then(d => pc2.setLocalDescription(d)) .then(() => pc1.setRemoteDescription(pc2.localDescription)) .catch(log); var log = msg => div.innerHTML += "<br>" + msg; 
 <video id="video1" height="120" width="160" autoplay muted></video> <video id="video2" height="120" width="160" autoplay></video><br> <input type="checkbox" onclick="mute()">mute</input><div id="div"></div> <script src="https://webrtc.imtqy.com/adapter/adapter-latest.js"></script> 

PeerConnections basically stop sending packets in this disconnected state, so it is very efficient.

+10
source

You should try using a revision, I believe the difference still exists, as is done in chrome and firefox:

  • In chrome, you simply call addStream or removeStream in a PeerConnection object to add / remove a stream, then create and share sdp .

  • There is no direct removeStream in firefox, you need to use RTCRtpSender and addTrack and removeTrack , you can take a look at this question

+1
source

Source: https://habr.com/ru/post/1244624/


All Articles