WebRTC pause and resume stream

半城伤御伤魂 提交于 2019-12-04 17:58:01

问题


I am trying to use WebRTC to build a web application that needs to pause/resume the video/audio stream when some events trigger. I have tried the getTracks()[0].stop() but I don't know how to resume the stream.


回答1:


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

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

This will replace your video with black, and your audio with silence.

Try it (use https fiddle 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.github.io/adapter/adapter-latest.js"></script>

PeerConnections basically stop sending packets in this muted state, so it is highly efficient.




回答2:


You should try using renegotiation, I believe the difference still exists how it is done in chrome and firefox:

  • In chrome, you just call addStream or removeStream on the PeerConnection object to add/ remove the stream, then create and exchange sdp.

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



来源:https://stackoverflow.com/questions/35857576/webrtc-pause-and-resume-stream

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!