rustrtc
A pure Rust implementation of WebRTC.
Features
- PeerConnection: The main entry point for WebRTC connections.
- Data Channels: Support for reliable and unreliable data channels.
- Media Support: RTP/SRTP handling for audio and video.
- ICE/STUN: Interactive Connectivity Establishment and STUN protocol support.
- DTLS: Datagram Transport Layer Security for secure communication.
- SDP: Session Description Protocol parsing and generation.
Performance (vs webrtc-rs & pion)
From Apple M4 machine result
mpi@mpis-MacBook-Air rustrtc % cargo build --release --example benchmark && ./target/release/examples/benchmark
Comparison (Baseline: webrtc)
Metric | webrtc | rustrtc | pion
--------------------------------------------------------------------------------
Duration (s) | 10.02 | 10.02 | 11.02
Setup Latency (ms) | 1.14 | 0.69 | 1.80
Throughput (MB/s) | 135.45 | 213.38 | 177.92
Msg Rate (msg/s) | 138696.71 | 218497.60 | 182190.56
CPU Usage (%) | 820.38 | 829.33 | 596.12
Memory (MB) | 28.00 | 10.00 | 41.00
--------------------------------------------------------------------------------
Key Findings:
- Throughput:
rustrtcis ~57% faster thanwebrtc-rsand ~20% faster thanpion. - Memory:
rustrtcuses ~64% less memory thanwebrtc-rsand ~75% less thanpion. - Setup Latency: Significantly faster connection setup (0.69ms vs 1.14ms/1.80ms).
Usage
Here is a simple example of how to create a PeerConnection and handle an offer:
use ;
async
Configuration
rustrtc allows customizing the WebRTC session via RtcConfiguration:
- ice_servers: Configure STUN/TURN servers.
- ice_transport_policy: Control ICE candidate gathering (e.g.,
All,Relay). - ssrc_start: Set the starting SSRC value for local tracks.
- media_capabilities: Configure supported codecs (payload types, names) and SCTP ports.
use ;
let mut config = default;
// Configure ICE servers
config.ice_servers.push;
// Set ICE transport policy (optional)
config.ice_transport_policy = All;
config.ssrc_start = 10000;
// Customize media capabilities
let mut caps = default;
// ... configure audio/video/application caps ...
config.media_capabilities = Some;
let pc = new;
Examples
You can run the examples provided in the repository.
Echo Server
The echo server example demonstrates how to accept a WebRTC connection, receive data on a data channel, and echo it back. It also supports video playback if an IVF file is provided.
-
Run the server:
-
Open your browser and navigate to
http://127.0.0.1:3000.
DataChannel Chat
A multi-user chat room using WebRTC DataChannels.
-
Run the server:
-
Open your browser and navigate to
http://127.0.0.1:3000. Open multiple tabs to chat between them.
Audio Saver
Records audio from the browser's microphone and saves it to a file (output.ulaw) on the server.
-
Run the server:
-
Open your browser and navigate to
http://127.0.0.1:3000. Click "Start" to begin recording.
RTP Play (FFmpeg)
Streams a video file (examples/static/output.ivf) via RTP to a UDP port, which can be played back using ffplay.
-
Run the server:
-
In a separate terminal, run
ffplay(requires ffmpeg installed):
License
This project is licensed under the MIT License.