Overview
This example receives VP8 video and Opus audio from a browser’s webcam/microphone and saves them as.ivf (video) and .ogg (audio) files. It showcases proper media pipeline configuration, RTP packet handling, and file writing.
Key Features
- Custom MediaEngine configuration for specific codecs
- Interceptor registry for RTCP features (NACK, PLI)
- Automatic codec detection and file format selection
- Graceful connection handling and file closure
- Interval PLI for keyframe generation
How It Works
1
Configure MediaEngine
Create a custom MediaEngine and register only the codecs you need:
2
Setup Interceptors
Configure the RTP/RTCP pipeline with interceptors:
3
Create API and PeerConnection
Build a custom API instance with the configured MediaEngine:
4
Add Transceivers
Explicitly add transceivers to receive audio and video:
5
Create File Writers
Open file writers for audio and video:
6
Handle Incoming Tracks
Process incoming tracks and write to appropriate files:
Complete Source Code
Important Implementation Details
Why Use Interval PLI Interceptor?
Why Use Interval PLI Interceptor?
The interval PLI (Picture Loss Indication) interceptor sends a PLI request every 3 seconds:Benefits:
- Forces generation of video keyframes periodically
- Makes recorded video seekable
- Improves error resilience
- Lower picture quality (keyframes are larger)
- Higher bitrates
In production, forward RTCP packets from viewers to senders instead of using interval PLI.
Custom MediaEngine vs Default
Custom MediaEngine vs Default
This example uses a custom MediaEngine instead of
webrtc.NewPeerConnection():Custom MediaEngine allows:- Registering only specific codecs
- Setting custom payload types
- Fine-grained control over RTP parameters
- You need specific codec support
- Interoperating with systems that require specific payload types
- Building specialized media processing pipelines
File Format Support
File Format Support
The example uses Pion’s media writers:
- IVF Writer: Container for VP8/VP9/AV1 video
- OGG Writer: Container for Opus audio
- Read RTP packets with
track.ReadRTP() - Extract payload data
- Write to your custom format
Interceptor Registry
Interceptor Registry
The InterceptorRegistry provides the RTP/RTCP processing pipeline:Default interceptors include:
- NACK (Negative Acknowledgment) for packet loss recovery
- RTCP report generation
- Statistics collection
Running the Example
1
Start the application
2
Open in browser
Navigate to
http://localhost and select the Save to Disk example3
Grant permissions
Allow the browser to access your webcam and microphone
4
Complete handshake
- Copy the offer from the browser
- Paste it into the terminal
- Copy the answer from terminal
- Paste it back into the browser
5
Record and stop
- Recording starts automatically when connected
- Press Ctrl+C in the browser to stop
- Files
output.oggandoutput.ivfwill be created
Output Files
After recording, you’ll have two files:- output.ogg: Opus audio at 48kHz, 2 channels
- output.ivf: VP8 video with original dimensions and framerate
- VLC Media Player
- FFmpeg:
ffmpeg -i output.ivf -i output.ogg output.mp4 - The play-from-disk example
Related Examples
- Play from Disk - Play back recorded files
- Broadcast - Forward streams to multiple viewers
- RTP Forwarder - Forward RTP to external applications