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
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
Complete handshake
- Copy the offer from the browser
- Paste it into the terminal
- Copy the answer from terminal
- Paste it back into the browser
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