WebRTC

Introduction

WebRTC (Web Real-Time Communication) is an open-source technology framework that enables real-time communication of audio, video, and data directly between web browsers and mobile applications, typically without requiring plugins or native software installations.

Key Points

How it Works

  1. Signaling (Finding and Connecting Peers)
    • WebRTC itself doesn't define how devices initially find each other or exchange control messages (signaling). Developers must implement this separately, often using WebSockets or other methods via a central server.
    • This signaling process is used to exchange crucial information:
      • Session control messages: To open or close communication.
      • Session Description Protocol (SDP): An "offer" and "answer" describing what media (audio/video codecs, formats) each peer can send/receive and other connection parameters.
      • Network information (ICE Candidates): Potential network paths (IP addresses, ports) that peers can use to connect directly.
  2. NAT/Firewall Traversal (ICE, STUN, TURN)
    • Most devices are behind home routers (NATs) or firewalls, which hide their private IP addresses. To establish a direct P2P connection, peers need to discover their public IP addresses and figure out how to bypass these network barriers.
    • ICE (Interactive Connectivity Establishment) is the framework used to find the best possible connection path. It uses two main protocols:
      • STUN (Session Traversal Utilities for NAT): A STUN server helps a peer discover its public IP address and port, and the type of NAT it's behind. If the NAT is simple enough, this information allows a direct P2P connection.
      • TURN (Traversal Using Relays around NAT): If a direct connection via STUN fails (due to restrictive firewalls or complex NATs like Symmetric NAT), a TURN server acts as a relay. Media data flows through the TURN server between the peers. This ensures connectivity but adds latency and requires more server resources.
    • Peers gather potential connection addresses (candidates) from STUN/TURN servers and their local network, exchange these candidates via the signaling channel, and then test them to find a working path.
  3. Establishing the Peer Connection (RTCPeerConnection)
    • This is the core JavaScript API in WebRTC. It takes the SDP offers/answers and the ICE candidates gathered during signaling and NAT traversal.
    • RTCPeerConnection handles the negotiation process, establishes the secure P2P connection using the best path found by ICE, manages the media streams, and monitors the connection status.
  4. Accessing Media (getUserMedia)
    • Before starting a call, the application needs to access the user's camera and microphone. The getUserMedia API prompts the user for permission and provides access to audio and video streams (MediaStream objects).
  5. Streaming Media and Data
    • Once the RTCPeerConnection is established:
      • Audio/Video: Media streams obtained via getUserMedia are attached to the RTCPeerConnection. They are transmitted using RTP (Real-time Transport Protocol), typically over UDP for speed. Security is mandatory; media is encrypted using SRTP (Secure RTP), with key exchange handled via DTLS (Datagram Transport Layer Security).
      • Arbitrary Data: The RTCDataChannel API allows sending any kind of data directly between peers (e.g., text chat, file transfers, game data). This uses SCTP (Stream Control Transmission Protocol) tunneled over DTLS.