Skip to main content
The rtp-forwarder example demonstrates how to receive media from a web browser via WebRTC and forward it to external applications using raw RTP. This enables integration with media processing tools, streaming servers, or any application that can consume RTP streams.

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

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
These match common RTP configurations. Adjust based on your receiver’s expectations.
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:
  1. Start the Pion forwarder
  2. Complete WebRTC handshake
  3. Start your receiver application
The example uses track.Read() instead of track.ReadRTP():
This reads raw RTP packets as byte arrays, allowing manipulation before forwarding. For forwarding without modification, you could use:
The example uses specific ports:
  • 4000: Audio (Opus)
  • 4002: Video (VP8)
These ports are arbitrary. Configure your receiver to listen on the same ports, or modify the example to use different ports.

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

  1. Open the example in your browser
  2. Enable webcam/microphone
  3. Copy the offer and paste into terminal
  4. 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
For production use, consider implementing proper session management, dynamic port allocation, and SDP negotiation to handle multiple simultaneous connections.