Overview
This example reads VP8/VP9/AV1 video (.ivf) and Opus audio (.ogg) files from disk and streams them to a connected peer in real-time, maintaining proper pacing to match the original playback speed.The example expects files named
output.ogg (audio) and output.ivf (video) in the working directory. You can generate these files using the save-to-disk example.Key Features
- Supports multiple video codecs (VP8, VP9, AV1)
- Proper frame pacing using time.Ticker
- Handles both audio and video tracks independently
- Demonstrates RTCP packet processing
- Uses context for connection synchronization
How It Works
Complete Source Code
Important Implementation Details
Why use time.Ticker instead of time.Sleep?
Why use time.Ticker instead of time.Sleep?
The example uses
time.Ticker instead of time.Sleep for two critical reasons:- Prevents skew accumulation:
time.Sleepdoesn’t compensate for time spent parsing and processing data - Avoids latency issues: Works around known latency issues with
time.Sleep(see Go issue #44343)
RTCP Packet Reading
RTCP Packet Reading
The example spawns goroutines to continuously read RTCP packets:This is essential because RTCP packets are processed by interceptors before being returned. Features like NACK (Negative Acknowledgment) require this continuous reading.
Codec Detection
Codec Detection
The example automatically detects the video codec from the IVF header:
AV01→ AV1VP90→ VP9VP80→ VP8
Running the Example
Prepare media files
Ensure you have
output.ogg and/or output.ivf in your working directory. You can create these using the save-to-disk example.Complete the WebRTC handshake
- Open the example in your browser (via the examples server)
- Copy the offer from the browser
- Paste it into the terminal
- Copy the answer from the terminal
- Paste it back into the browser
Related Examples
- Save to Disk - Record media to create playable files
- Broadcast - Stream to multiple viewers
- RTP Forwarder - Forward streams to external applications