How to Stream Libopus Packets over RTP

This article outlines the standard, RFC-compliant methodology for streaming libopus audio packets over Real-time Transport Protocol (RTP) networks. It details the core specifications defined in RFC 7587, including the mandatory 48 kHz clock rate rule, Session Description Protocol (SDP) parameters, packetization configurations, and best practices for achieving low-latency, high-resilience audio transmission.

The Standard: RFC 7587

The strictly recommended specification for encapsulating Opus audio in RTP is RFC 7587 (“RTP Payload Format for the Opus Speech and Audio Codec”). To ensure interoperability across different VoIP, WebRTC, and broadcast systems, your streaming implementation must strictly adhere to this standard rather than using custom framing.

The RTP Timestamp and Clock Rate Rule

Unlike other codecs where the RTP timestamp clock rate matches the sampling rate of the encoded audio, RFC 7587 defines a strict rule for Opus:

RTP Payload Structure

The RTP payload for an Opus packet is straightforward:

  1. RTP Header: Standard 12-byte header. The Payload Type (PT) must be dynamically allocated (typically in the range of 96–127).
  2. Opus Payload: The libopus compressed audio data is placed directly into the RTP payload field.
  3. TOC (Table of Contents) Byte: The very first byte of the libopus payload is the TOC byte, which describes the configuration (bandwidth, channel count, and frame duration) of the packet. No extra encapsulation headers are allowed between the RTP header and the Opus payload.

Typically, one RTP packet should contain exactly one Opus frame (or one multi-frame Opus packet generated by the encoder) to minimize packetization latency.

SDP Signaling and Media Negotiation

To establish a stream, you must negotiate the connection using SDP. Because Opus uses dynamic payload types, you must define the mapping explicitly.

An RFC 7587 compliant SDP media description looks like this:

m=audio 5004 RTP/AVP 111
a=rtpmap:111 opus/48000/2
a=fmtp:111 minptime=10; useinbandfec=1; stereo=1; maxaveragebitrate=128000

Key SDP Parameter Rules:

Packetization and Latency (ptime)

Opus supports frame sizes of 2.5, 5, 10, 20, 40, and 60 ms.

Congestion Control and DTX

To optimize network utilization, the streaming application should leverage libopus’s native features: