Understanding mediasoup
mediasoup is a powerful WebRTC SFU (Selective Forwarding Unit) library. This guide explains how it works in simple terms.
What is an SFU?
An SFU (Selective Forwarding Unit) is a server that receives media streams from participants and forwards them to others. Unlike peer-to-peer, all media flows through the server.
Peer-to-Peer (Mesh) - The Problem
Problem: Each person sends to everyone (N×N connections). With 4 people, that's 6 connections. With 10 people, that's 45 connections!
SFU Architecture - The Solution
Solution: Each person sends once, server forwards to everyone else. Scales much better!
Core Concepts
1. Worker
A Worker is a separate process that handles media. Think of it as a "media engine."
Key points:
- One Worker per CPU core (recommended)
- Workers are isolated - if one crashes, others continue
- Workers handle the heavy lifting of media processing
2. Router
A Router is like a "virtual room" inside a Worker. It routes media between participants.
Key points:
- One Router per conference room (typically)
- Router knows which media goes where
- Participants in the same Router can exchange media
3. Transport
A Transport is the network connection between a client and the server. It carries media data.
Each participant typically has 2 transports:
- Send Transport - for sending their media (camera, mic, screen)
- Receive Transport - for receiving others' media
4. Producer
A Producer sends media to the server. When you share your camera, you create a Producer.
Producer types:
audio- Microphonevideo- Cameravideo(withscreenlabel) - Screen share
5. Consumer
A Consumer receives media from the server. When you see someone's video, that's a Consumer.
Key insight: One Producer can have many Consumers. Bob's camera creates 1 Producer, but Alice, Charlie, and Dave each get their own Consumer for it.
Complete Flow Diagram
Here's how everything connects when Alice joins a room and shares her camera:
RTP Capabilities
Before producing or consuming, clients and server exchange "RTP Capabilities" - a list of supported codecs and features.
Simulcast
Simulcast sends multiple quality layers of the same video. The server can forward different qualities to different receivers.
Summary
| Concept | What it is | Analogy |
|---|---|---|
| Worker | Process handling media | Kitchen in a restaurant |
| Router | Virtual room for participants | A private dining room |
| Transport | Network connection | The waiter serving your table |
| Producer | Sends media to server | Chef preparing your dish |
| Consumer | Receives media from server | You eating the dish |
Next Steps
- QuickRTC Architecture - How QuickRTC simplifies mediasoup
- Getting Started - Build your first app