Overview
This example acts as a WebRTC-to-RTP bridge, receiving VP8 video and Opus audio from a browser and forwarding them via UDP to local ports where external applications (like FFmpeg, VLC, or GStreamer) can consume them.Key Features
- WebRTC to RTP bridge functionality
- Separate audio and video UDP forwarding
- Payload type normalization for compatibility
- Custom MediaEngine and Interceptor configuration
- Integration with external media processing tools
Architecture
How It Works
1
Configure Media Engine
Register specific codecs for RTP forwarding:
2
Setup UDP Connections
Create UDP connections for audio and video forwarding:
3
Handle Incoming Tracks
Forward RTP packets from WebRTC tracks to UDP:
4
Add Transceivers
Explicitly request audio and video tracks:
Complete Source Code
Important Implementation Details
Payload Type Normalization
Payload Type Normalization
The browser may use different payload types than your external application expects:This example normalizes payload types to:
- 96 for VP8 video
- 111 for Opus audio
Handling Connection Refused
Handling Connection Refused
The example gracefully handles cases where the receiver isn’t ready:Why this matters:
Users typically complete the WebRTC handshake before starting the receiver application. Without this handling, the forwarder would crash.Best practice:
- Start the Pion forwarder
- Complete WebRTC handshake
- Start your receiver application
Direct Packet Reading
Direct Packet Reading
The example uses This reads raw RTP packets as byte arrays, allowing manipulation before forwarding. For forwarding without modification, you could use:
track.Read() instead of track.ReadRTP():Port Selection
Port Selection
The example uses specific ports:
- 4000: Audio (Opus)
- 4002: Video (VP8)
Running with FFmpeg
Here’s how to use this example with FFmpeg to receive and process the streams:1
Create SDP file
Create
rtp-forwarder.sdp with the following content:2
Start the forwarder
3
Complete WebRTC handshake
- Open the example in your browser
- Enable webcam/microphone
- Copy the offer and paste into terminal
- Copy the answer and paste into browser
4
Start FFmpeg receiver
Use Cases with External Tools
FFmpeg Processing
Transcode, record, or stream to RTMP servers
VLC Playback
Watch the stream in VLC Media Player
GStreamer Pipeline
Build custom processing pipelines
Custom Processing
Consume RTP in your own application for:
- AI/ML video analysis
- Computer vision processing
- Custom encoding/decoding
- Quality monitoring
Running the Example
1
Start the application
2
Complete WebRTC handshake
Follow the copy-paste signaling process with your browser
3
Start your RTP receiver
Launch FFmpeg, VLC, or your custom application to receive the RTP streams on ports 4000 (audio) and 4002 (video)
4
Verify streaming
Check that your receiver is processing the audio and video streams correctly
Related Examples
- Save to Disk - Save media directly to files
- Broadcast - Forward to multiple WebRTC viewers
- Play from Disk - Stream pre-recorded content