简体   繁体   中英

WebRTC: How to add stream after offer and answer?

I am working on webRTC video calling. I got datachannel successfully implemented. 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.

pc1.addstream(stream)

Then once again create offer and send it to other Peer.

Remote peer will add stream and send answer SDP.

To stop streams:

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

In my experience, what Konga Raju advised didn't work. 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. 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.)
  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:
    • an ice candidate sent from the other side ==> call addIceCandidate with it
    • an offer message ==> SetRemoteDescription & make an answer & send it
    • an answer message ===> SetRemoteDescription
  3. On each side, create the peerconnection object with the event handlers we care about: onicecandidate, onremovestream, onaddstream, etc.
    • ice candidate pops out of the peerconnection object ===> send it to other side
  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)
  5. Once getUserMedia succeeds, we have a stream. Call addStream on the peer connection object.
  6. Then peer 1 makes an offer
  7. Due to the handlers we set up earlier, peer 2 sends an answer
  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)
  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.

MediaStream should be added to peerconnection first only then exchange of offer, answer ,candidates should be done. If the onAddStream() is called ,that mean you are receiving the remote video.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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