mediasoup

repository·v3·Indexed 27 days ago

https://github.com/versatica/mediasoup

A high-performance, low-level Selective Forwarding Unit (SFU) for real-time media applications. It supports WebRTC and plain RTP, providing server-side implementations for Node.js and Rust, and client-side libraries in TypeScript and C++. Key features include support for Simulcast, SVC, congestion control, and data message exchange via WebRTC DataChannels, SCTP over UDP, or direct termination.

Tokens
16.1K
Snippets
20
Records
125
Agent score
93%

What's inside mediasoup

  1. Overview of mediasoup v3

    v3

    mediasoup v3 is a Selective Forwarding Unit (SFU) designed to handle the media layer of real-time communication applications. It provides a low-level API that is signaling agnostic, meaning it does not mandate a specific signaling protocol.

    Key characteristics include:

    • Server-side: Available as a Node.js module or a Rust crate.
    • Client-side: Provided as tiny TypeScript and C++ libraries.
    • Capabilities: Supports both WebRTC and plain RTP input/output, multi-stream over a single ICE + DTLS transport, and is IPv6 ready.
    • Media Handling: Supports Simulcast, SVC, congestion control, and data message exchange (WebRTC DataChannels, SCTP over UDP, or direct termination).
  2. Understand the mediasoup Rust crate structure

    v3

    The mediasoup Rust implementation is divided into three distinct crates:

    • mediasoup-sys: A low-level crate that wraps the C++ worker into Rust.
    • mediasoup-types: A crate that defines and exposes the core mediasoup Rust types.
    • mediasoup: The high-level crate that consumes mediasoup-sys and mediasoup-types to provide an idiomatic Rust API for users.

    Dependency Rules:

    • If changes are internal to the worker or mediasoup-sys, only mediasoup-sys strictly requires an update.
    • Adding new APIs that the mediasoup crate must understand is considered a breaking change, as it may cause crashes or errors if unexpected data is encountered.
  3. Understand RTCP packet processing in mediasoup

    v3

    As an SFU, mediasoup processes some RTCP packets locally while ignoring others. It does not forward feedback information from a remote RTP receiver to a remote sender. This design prevents a single remote receiver from influencing the transmission rate of a remote sender in a way that would negatively impact other participants in the router.

    Key behaviors include:

    • Sender/Receiver Reports: Used to determine link quality. Information from these reports is available at the JavaScript API level to help determine the quality of each participant.
    • NACK: mediasoup consumes received NACK requests and re-sends the solicited RTP packets. It also generates NACK requests for remote senders.
    • PLI/FIR: mediasoup consumes these to generate PLI requests to the corresponding RTP sender. It also generates them locally (e.g., when a new participant joins) to facilitate fast rendering.
    • REMB: mediasoup consumes REMB messages to perform sender-side bandwidth estimation and generates them locally based on remote bitrate estimation.
  4. Build mediasoup TypeScript code

    v3

    Use the following NPM scripts to compile the TypeScript source code located in node/src into JavaScript in the node/lib directory:

    • npm run typescript:build: Performs a one-time compilation.
    • npm run typescript:watch: Compiles the code and watches for changes in the TypeScript files to trigger automatic recompilation.
    npm run typescript:build
    # or
    npm run typescript:watch
  5. Explore mediasoup features and use cases

    v3

    mediasoup is intended for high-performance media applications where a low-level API is required to avoid constraints or assumptions.

    Common Use Cases

    • Group video chat applications.
    • One-to-many or few-to-many real-time broadcasting.
    • RTP streaming.

    Core Features

    • Low-level API: ECMAScript 6 for Node.js and idiomatic Rust.
    • Transport: ICE / DTLS / RTP / RTCP over both UDP and TCP.
    • Scalability: Support for Simulcast and SVC (Scalable Video Coding).
    • Bandwidth Management: Sender and receiver bandwidth estimation with spatial/temporal layers distribution algorithms.
    • Performance: Media worker threads/subprocesses are implemented in C++ on top of libuv.
  6. Append SHA1 hashes to corpora files using add_sha1.sh

    v3

    To prevent redundant testing caused by duplicate files (files with different names but identical binary content), you can use the add_sha1.sh script. This script appends the SHA1 hashsum to the filenames in the target directory without touching file extensions.

    Usage: Pass the target folder as the first argument.

    Key behaviors:

    • Renaming: It renames files to include their hash (e.g., file -> file-hash).
    • Idempotency: It detects files that are already hashed by looking for the SHA1sum in the filename and marks them as OK.
    • Duplicate Detection (Local): It identifies duplicate files within the same subfolder and labels them as DUPLICATE.
    $ ./add_sha1.sh .
  7. Use corpus files and manage crash reports in the fuzzer

    v3

    Corpus Files

    Use the deps/webrtc-fuzzer-corpora/corpora directory to provide input to the fuzzer.

    Because libFuzzer uses the first command-line directory parameter for both reading and storing newly generated corpus files, it is recommended to pass a dedicated directory like fuzzer/new-corpus as the first argument.

    Crash Reports

    When an issue is detected, the fuzzer generates a crash report file containing the input bytes. To store these reports in a specific directory (e.g., fuzzer/reports/), use the following CLI flag:

    • -artifact_prefix=fuzzer/reports/
  8. Determine link quality using Sender and Receiver Reports

    v3

    mediasoup uses the combination of Sender Reports (SR) and Receiver Reports (RR) to determine the quality of each link.

    • Sender Reports: mediasoup locally generates these for the streams it sends and processes those received from producer endpoints.
    • Receiver Reports: mediasoup locally generates these for the streams it receives and consumes those from every remote RTP receiver.

    Quality information derived from these reports is exposed via the JavaScript API, allowing developers to monitor and react to the quality of each participant in the router.

  9. Build and run mediasoup Docker images

    v3

    Mediamosoup provides several Docker images for different environments:

    • invoke docker: Builds a Linux Ubuntu image with fuzzer-capable clang++ and all dependencies.
    • invoke docker-run: Runs the Ubuntu container with a bash session.
    • invoke docker-alpine: Builds a Linux Alpine image with all dependencies.
    • invoke docker-alpine-run: Runs the Alpine container with an ash session.
    • invoke docker-386: Builds a 386 Linux Debian (32-bit) image.
    • invoke docker-alpine-386: Runs the 386 Linux Debian (32-bit) container.

    Important Note: Before running a container, you must clean previous installations by running invoke clean-all inside the worker directory.

    cd worker
    invoke clean-all
    invoke docker
    invoke docker-run
  10. Build the mediasoup-worker binary

    v3

    To build the mediasoup-worker binary, use the following scripts:

    • npm run worker:build: Builds the binary.
    • npm run worker:prebuild: Creates a prebuilt of the mediasoup-worker binary in the worker/prebuild folder.
    • npm run worker:prebuild-name: Prints the name of the corresponding mediasoup-worker prebuild tar file.
    npm run worker:build