Skip to content

Latest commit

 

History

History
122 lines (77 loc) · 4.72 KB

WebRTC.md

File metadata and controls

122 lines (77 loc) · 4.72 KB

WebRTC (Web Real-Time Communication) is an open-source technology that enables real-time communication (RTC) directly between web browsers and applications. It allows audio, video, and data sharing without requiring third-party plugins or external software.

WebRTC is designed to support:

  • Real-time video and audio communication (e.g., video calls, live streaming)
  • Peer-to-peer (P2P) data transfer (e.g., file sharing, gaming)
  • Low latency communication (ideal for applications like VoIP, conferencing, and remote control)
  • Secure connections via end-to-end encryption

WebRTC is supported in all major browsers, including Google Chrome, Mozilla Firefox, Safari, and Microsoft Edge, making it widely accessible.

How WebRTC Works

WebRTC enables direct communication between peers by handling three key challenges:

  1. Device Access (Media Capture)
  2. Connection Establishment (Signaling & NAT Traversal)
  3. Data Transmission (Peer-to-Peer Streaming)

Media Capture (GetUserMedia API)

WebRTC provides an API called getUserMedia() that allows web applications to access the user’s microphone, camera, and screen.

Example: Capturing Media in JavaScript

navigator.mediaDevices.getUserMedia({ video: true, audio: true })
  .then(stream => {
    document.getElementById('videoElement').srcObject = stream;
  })
  .catch(error => console.error('Error accessing media devices.', error));

This API ensures that only authorized web applications can access the user's media.

Connection Establishment (Signaling & NAT Traversal)

For two devices to communicate, WebRTC must establish a connection, even if both devices are behind Network Address Translation (NAT) or firewalls. This process involves:

Signaling (Exchange of Connection Information)

WebRTC does not define a built-in signaling mechanism. Instead, applications must use external protocols such as WebSockets, SIP, or Firebase to exchange metadata required to set up a connection.

Signaling involves exchanging three key elements:

  • Session Description Protocol (SDP): Describes media formats and connection preferences.
  • ICE Candidates: Lists available network paths.
  • Transport Protocols: Determines how data should be transmitted.

NAT Traversal Using ICE, STUN, and TURN

Most users are behind a NAT, meaning they don’t have a public IP address. WebRTC uses ICE (Interactive Connectivity Establishment) to find the best path for peer-to-peer communication.

  • STUN (Session Traversal Utilities for NAT): Finds the public IP and port of the device.
  • TURN (Traversal Using Relays around NAT): If direct connection fails, a relay server acts as an intermediary.

Example: Establishing a Peer Connection

const peerConnection = new RTCPeerConnection();
peerConnection.onicecandidate = event => {
  if (event.candidate) {
    sendToSignalingServer(event.candidate);
  }
};

Data Transmission (Peer-to-Peer Streaming)

Once a connection is established, WebRTC enables efficient, low-latency data transmission between peers.

  1. Media Streaming (Audio/Video)

WebRTC allows real-time streaming using the RTCPeerConnection API.

navigator.mediaDevices.getUserMedia({ video: true, audio: true })
  .then(stream => peerConnection.addStream(stream));
  1. Data Channels (Real-Time Messaging & File Transfer)

WebRTC provides a DataChannel API, allowing applications to send non-media data, such as text messages, game state updates, and files.

const dataChannel = peerConnection.createDataChannel('chat');
dataChannel.onmessage = event => console.log('Received message:', event.data);
dataChannel.send('Hello, WebRTC!');

Underlying Wireless Communication Standards in WebRTC

WebRTC relies on a combination of audio, video, and network protocols to ensure efficient, secure, and high-quality real-time communication.

Audio & Video Codecs

WebRTC uses highly optimized codecs to reduce bandwidth usage while maintaining quality. Audio Codecs: Opus (default, optimized for speech), G.711 Video Codecs: VP8, VP9 (Google), H.264 (used in some implementations)

RTP & SRTP (Real-Time Transport Protocol)

WebRTC uses RTP (Real-Time Transport Protocol) for streaming media. SRTP (Secure RTP) ensures encryption and integrity of the transmitted media.

SCTP (Stream Control Transmission Protocol)

Used for WebRTC Data Channels to transmit messages and files. Supports ordered and unordered delivery (ideal for gaming, messaging, etc.).

DTLS (Datagram Transport Layer Security)

Ensures encryption of data exchanged over WebRTC connections. Prevents eavesdropping and man-in-the-middle attacks.

ICE (Interactive Connectivity Establishment)

Helps negotiate the best path for peer-to-peer communication. Works with STUN (to discover public IPs) and TURN (to relay data when necessary).