andersch.dev

<2024-11-20 Wed>
[ web ]

WebRTC (Web Real-Time Communication)

WebRTC (Web Real-Time Communication) is an open-source project that provides web browsers with peer-to-peer data sharing, without the need for intermediary servers. It leverages technologies like STUN and TURN for NAT traversal.

It provides a set of standardized APIs that facilitate real-time communication capabilities, allowing developers to build applications such as video conferencing, file sharing, and live streaming with minimal latency.

Since direct UDP communication is not available in browsers, WebRTC can serve as a close alternative by performing UDP-like communication with WebRTC Data Channels.

Protocols

webrtc-protocols.svg

Steps

  1. Signaling: Finding peers using SDP
  2. Connecting: NAT traversal using ICE with STUN/TURN
  3. Securing: Encryption using DTLS (TLS for UDP) and SRTP (Secure RTP)
  4. Communicating: Media exchange using RTP and DataChannel messages using SCTP

Javascript API

// RTCPeerConnection
//   createOffer()
//   createAnswer()
//   setLocalDescription()
//   setRemoteDescription()
//   addIceCandidate()
//   getStats()

// MediaStream
//   getTracks()
//   getAudioTracks()
//   getVideoTracks()
//   addTrack()
//   removeTrack()

// RTCDataChannel
//   send()
//   close()

Topologies

One-To-One

  • Connect two WebRTC Agents directly

Full Mesh

  • Each user establishes a one-to-one connection with every other user directly

Hybrid Mesh

  • Media is relayed through peers in the network
  • Uses less bandwidth for each peer
  • Increase in latency with every hop in the Hybrid Mesh network.

SFU (Selective Forwarding Unit):

  • Client/server topology instead of P2P.
  • Each WebRTC uploads to the SFU, which then forwards media to each client

MCU (Multi-point Conferencing Unit)

  • Client/server topology, but output stream is composited
  • Outbound media is re-encoded as one feed

Implementations

Resources