Configure RTP Payload Type for Audio in FFmpeg

This article explains how to configure custom payload type identifiers for Real-time Transport Protocol (RTP) audio streams using FFmpeg. You will learn the command-line syntax for setting specific payload types, how to handle static and dynamic identifiers, and how to generate the corresponding Session Description Protocol (SDP) file required for receivers to decode the stream.

Understanding RTP Payload Types

RTP payload types (PT) are 7-bit identifiers in the RTP packet header that designate the format of the transmitted media.

When streaming custom or modern audio codecs, you must explicitly define this identifier so the receiving client knows how to parse the incoming stream.

Setting the Payload Type in FFmpeg

To set a custom payload type in FFmpeg, use the -payload_type private option for the RTP muxer.

Basic Command Syntax

Here is the basic command template for streaming audio with a custom payload type:

ffmpeg -re -i input.wav -c:a libopus -payload_type 101 -f rtp rtp://127.0.0.1:5004

In this example: * -re: Reads the input at the native frame rate (essential for real-time streaming). * -i input.wav: Specifies the input audio file. * -c:a libopus: Encodes the audio using the Opus codec. * -payload_type 101: Forces the RTP packet headers to use payload type identifier 101. * -f rtp: Specifies the output format as RTP. * rtp://127.0.0.1:5004: Defines the destination IP address and port.

Generating the SDP File

For dynamic payload types (like 101 for Opus), the receiver cannot decode the stream based on the RTP packets alone; it requires an SDP file that maps the payload type to the codec parameters.

You can instruct FFmpeg to output this SDP configuration to a file during the stream initialization by adding the -sdp_file flag:

ffmpeg -re -i input.wav -c:a libopus -payload_type 101 -f rtp -sdp_file stream.sdp rtp://127.0.0.1:5004

Example of the Generated SDP

The resulting stream.sdp file will contain lines mapping the payload type to the codec:

v=0
o=- 0 0 IN IP4 127.0.0.1
s=No Name
c=IN IP4 127.0.0.1
t=0 0
m=audio 5004 RTP/AVP 101
a=rtpmap:101 opus/48000/2

The line m=audio 5004 RTP/AVP 101 states that media is expected on port 5004 using payload type 101. The line a=rtpmap:101 opus/48000/2 binds payload type 101 to the Opus codec at a 48 kHz sample rate with 2 channels.

Handling Multiple Streams

If you are broadcasting multiple RTP streams from a single FFmpeg command, you can specify the payload type for individual streams using the stream specifier syntax:

ffmpeg -re -i input.wav -map 0:a -c:a:0 libopus -payload_type:a:0 101 -f rtp rtp://127.0.0.1:5004

Using -payload_type:a:0 101 ensures that the configuration specifically targets the first audio stream in the output specifier.