简体   繁体   English

WebRTC:如何在提供和回答后添加流?

[英]WebRTC: How to add stream after offer and answer?

I am working on webRTC video calling. 我正在进行webRTC视频通话。 I got datachannel successfully implemented. 我已成功实施datachannel。 Now I would like to add video stream to the same peer connection. 现在我想将视频流添加到同一个对等连接。

I have read that stream should be added before answer and offer. 我已经读过应该在回答和提供之前添加流。 Is there a way to add stream after answer or offer? 有没有办法在回答或提供后添加流?

In case I have added stream before offer or answer, how could I stop streaming and start it again when needed? 如果我在提供或回答之前添加了流,我怎么能停止流式传输并在需要时再次启动它?

Could there be any issues in maintaining so many streams? 维护这么多流可能有任何问题吗?

To add stream after creating complete signalling, the Peer connection should renegotiate with stream. 要在创建完整信令后添加流,Peer连接应与流重新协商

pc1.addstream(stream)

Then once again create offer and send it to other Peer. 然后再次创建优惠并将其发送给其他同行。

Remote peer will add stream and send answer SDP. 远程对等体将添加流并发送应答SDP。

To stop streams: 要阻止流:

stream.stop();
pc1.removeStream(stream);

In my experience, what Konga Raju advised didn't work. 根据我的经验,Konga Raju建议不起作用。 I couldn't send an "updated offer" and have the video streaming actually happen. 我无法发送“更新的优惠”并且实际上发生了视频流。

I found that this sequence of events works for my case, in which I wish to stream video from peer 1 to peer 2. 我发现这个事件序列适用于我的情况,我希望将视频从对等体1流式传输到对等体2。

  1. set up some way for the peers to exchange messages. 为对等方设置某种方式来交换消息。 (The variance in how people accomplish this is what makes different WebRTC code samples so incommensurable, sadly.) (人们如何实现这一点的差异使得不同的WebRTC代码样本如此不可通约,令人遗憾。)
  2. On each side, set up handlers for the important signalling events. 在每一侧,为重要的信号事件设置处理程序。 (Some folks have reported that you need to create these handlers at special times, but I haven't found that to be the case. ) There are 3 basic events: (有些人报告说你需要在特殊时间创建这些处理程序,但我没有发现这种情况。)有3个基本事件:
    • an ice candidate sent from the other side ==> call addIceCandidate with it 从另一方发送的冰候选人==>用它调用addIceCandidate
    • an offer message ==> SetRemoteDescription & make an answer & send it 要约消息==> SetRemoteDescription并回答并发送
    • an answer message ===> SetRemoteDescription 答案消息===> SetRemoteDescription
  3. On each side, create the peerconnection object with the event handlers we care about: onicecandidate, onremovestream, onaddstream, etc. 在每一侧,使用我们关心的事件处理程序创建peerconnection对象:onicecandidate,onremovestream,onaddstream等。
    • ice candidate pops out of the peerconnection object ===> send it to other side ice候选者弹出peerconnection对象===>将其发送到另一侧
  4. When both peers are present and all the handlers are in place, peer 1 gets a trigger message of some kind to start video capture (the getUserMedia call) 当两个对等体都存在且所有处理程序都到位时,对等体1获得某种触发消息以开始视频捕获( getUserMedia调用)
  5. Once getUserMedia succeeds, we have a stream. 一旦getUserMedia成功,我们就有了一个流。 Call addStream on the peer connection object. 在对等连接对象上调用addStream
  6. Then peer 1 makes an offer 然后同伴1提出要约
  7. Due to the handlers we set up earlier, peer 2 sends an answer 由于我们之前设置的处理程序,peer 2发送了一个答案
  8. Concurrently with this (and rather opaquely), the peer connection object starts producing ice candidates. 与此同时(并且相当不透明地),对等连接对象开始产生冰候选者。 They get sent back and forth between the two peers and handled (steps 2 & 3 above) 他们在两个同伴之间来回传递并处理(上面的步骤2和3)
  9. Streaming starts by itself, opaquely, as a result of 2 conditions: 由于两个条件,流式传输本身是不透明的:
    • offer/answer exchange 提供/回答交换
    • ice candidates received, exchanged, and handled 冰候选人收到,交换和处理

I haven't found a way to add video after step 9. When I want to change something, I go back to step 3. 我还没有找到在步骤9之后添加视频的方法。当我想要改变某些内容时,我会回到第3步。

MediaStream should be added to peerconnection first only then exchange of offer, answer ,candidates should be done. 首先应该将MediaStream添加到peerconnection,然后才能进行提议,答案,候选人的交换。 If the onAddStream() is called ,that mean you are receiving the remote video. 如果调用onAddStream(),则表示您正在接收远程视频。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM