Here is a sequence of events I have working today (Feb 2014) in Chrome. This is for a simplified case where peer 1 will stream video to peer 2.
- 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. But mentally, and in your code organization, try to separate this logic out from the rest.)
- On each side, set up message handlers for the important signalling messages. You can set them up and leave them up. There are 3 core messages to handle & send:
- an ice candidate sent from the other side ==> call
addIceCandidate
with it
- an offer message ==>
SetRemoteDescription
with it, then make an answer & send it
- an answer message ===>
SetRemoteDescription
with it
- On each side, create a new peerconnection object and attach event handlers to it for important events: onicecandidate, onremovestream, onaddstream, etc.
- ice candidate ===> send it to other side
- stream added ===> attach it to a video element so you can see it
- 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 (using the
getUserMedia
call)
- Once
getUserMedia
succeeds, we have a stream. Call addStream
on the peer 1's peer connection object.
- Then -- and only then -- peer 1 makes an offer
- Due to the handlers we set up in step 2, peer 2 gets this and sends an answer
- Concurrently with this (and somewhat obscurely), the peer connection object starts producing ice candidates. They get sent back and forth between the two peers and handled (steps 2 & 3 above)
- Streaming starts by itself, opaquely, as a result of 2 conditions:
- offer/answer exchange
- ice candidates received, exchanged, and added
I haven't found a way to add video after step 9. When I want to change something, I go back to step 3.