WebRTC and you, the bare minimum to get going
RTCPeerConnection
object. It contains the iceServers
array which contains the STUN and TURN servers that the client will use to establish the connection.
RTCPeerConnection
object. It contains the information about the media streams that the client is willing to share and the configuration of the connection. IP addresses, codecs, etc. Everything that you can accept or send. This object is then sent to the other peer.
RTCPeerConnection
object in response to the RTCOffer
object. It contains the information about the media streams that the client is willing to share and the configuration of the connection. IP addresses, codecs, etc. Everything that you can accept or send. This object is then sent to the other peer.
RTCPeerConnection
object to be sent to the other peer. It is also the object that you get from the getUserMedia
function when you specify what you’re sending in the offer.
createOffer()
function. This creates a json that will be sent to all peers that you want to connect to. Also you must make sure to tell the RTCPeerConnection object to use the offer as the local description using setLocalDescription()
function.
There’s no exact way that specifies how to send the offer and get the answer. Some examples make you paste the offer in a CLI, others can use a POST request (easiest way in my opinion), and someone thought using WebSockets is a good idea (if you’re doing trickle ICE then probably but that’s a different rabbithole that I won’t get into here).
Regardless of how you’re getting the answer, you need to set the remote description of the RTCPeerConnection object to the answer using setRemoteDescription()
function. The peer does the same thing, but in reverse. They get the offer set it as the RemoteDescription and create an answer and set it as the LocalDescription.
AAAND Tada, this is the actual bare minimum. If you’re not going over the internet and just testing on localhost, you can now see the video and audio streams of the other peer (assuming you added the streams to the RTCPeerConnection object and have the HTML stuff figured out correctly). As an example, you can see the server example on the aiortc (python’s webrtc) github page which streams a video from disk to the client, using the same principles described here.
track
event. This event is emitted when a new MediaStreamTrack is added to the RTCPeerConnection object. You can then use this event to add the track to the HTML video or audio element by setting the srcObject
property of the element to the MediaStream object that the track is a part of.
createDataChannel()
function. You can then send messages using the send()
function. You can also listen for messages using the message
event. You can also listen for the open
event to know when the datachannel is ready to send messages. You can also send binary data without base64 encoding by using the send()
function with an UInt8Array as the argument.