WebRTC module

CodecNegotiator

Maps RTP codec names to FFmpeg encoders and queries FFmpeg at runtime to determine what codecs are available.

CodecNegotiator

#include <icy/webrtc/codecnegotiator.h>

Maps RTP codec names to FFmpeg encoders and queries FFmpeg at runtime to determine what codecs are available.

This is stateless - all methods are static. Call negotiate*() with a list of RTP codec names offered by the remote peer, and it returns the best match that FFmpeg can encode.

Public Static Methods

ReturnNameDescription
std::optional< NegotiatedCodec >negotiateVideo staticNegotiate the best video codec from a list of offered RTP codec names. Returns the first match that FFmpeg can encode, in preference order: H264 > VP8 > VP9 > AV1 > H265
std::optional< NegotiatedCodec >negotiateAudio staticNegotiate the best audio codec from a list of offered RTP codec names. Returns the first match that FFmpeg can encode, in preference order: opus > PCMU > PCMA
boolhasEncoder staticCheck if FFmpeg has an encoder for the given codec name. Accepts both FFmpeg names ("libx264") and RTP names ("H264").
std::stringrtpToFfmpeg staticMap an RTP codec name to the preferred FFmpeg encoder name. Returns empty string if no mapping exists.
std::stringffmpegToRtp staticMap an FFmpeg encoder name to the RTP codec name. Returns empty string if no mapping exists.
uint32_tclockRate staticGet the standard RTP clock rate for a codec.
intdefaultPayloadType staticGet the default RTP payload type for a codec. Returns 0 for dynamic payload types (caller should assign).
std::optional< CodecSpec >specFromRtp staticReturn the canonical codec spec for an RTP name, if known.
std::optional< CodecSpec >specFromFfmpeg staticReturn the canonical codec spec for an FFmpeg encoder name, if known.
std::optional< CodecSpec >specFromVideoCodec staticResolve the canonical codec spec from an explicit video codec config. Prefers the FFmpeg encoder when present, otherwise falls back to RTP name.
std::optional< CodecSpec >specFromAudioCodec staticResolve the canonical codec spec from an explicit audio codec config. Prefers the FFmpeg encoder when present, otherwise falls back to RTP name.
CodecSpecrequireVideoSpec staticResolve a strict canonical video codec spec or throw.
CodecSpecrequireAudioSpec staticResolve a strict canonical audio codec spec or throw.
av::VideoCodecresolveWebRtcVideoCodec staticResolve a browser-safe WebRTC video codec config from an explicit codec.
av::AudioCodecresolveWebRtcAudioCodec staticResolve a browser-safe WebRTC audio codec config from an explicit codec.
std::optional< CodecSpec >detectCodec staticDetect the first known codec present in an SDP snippet for the given media type.
std::optional< CodecSpec >detectCodecInMedia staticDetect the first known codec from a structured rtc::Description::Media object for the given media type.
AVCodecIDdecoderCodecId staticReturn the FFmpeg decoder codec ID for a given codec spec. Returns AV_CODEC_ID_NONE if no mapping is known.

negotiateVideo

static

static std::optional< NegotiatedCodec > negotiateVideo(const std::vector< std::string > & offeredCodecs)

Negotiate the best video codec from a list of offered RTP codec names. Returns the first match that FFmpeg can encode, in preference order: H264 > VP8 > VP9 > AV1 > H265


negotiateAudio

static

static std::optional< NegotiatedCodec > negotiateAudio(const std::vector< std::string > & offeredCodecs)

Negotiate the best audio codec from a list of offered RTP codec names. Returns the first match that FFmpeg can encode, in preference order: opus > PCMU > PCMA


hasEncoder

static

static bool hasEncoder(const std::string & name)

Check if FFmpeg has an encoder for the given codec name. Accepts both FFmpeg names ("libx264") and RTP names ("H264").


rtpToFfmpeg

static

static std::string rtpToFfmpeg(const std::string & rtpName)

Map an RTP codec name to the preferred FFmpeg encoder name. Returns empty string if no mapping exists.


ffmpegToRtp

static

static std::string ffmpegToRtp(const std::string & ffmpegName)

Map an FFmpeg encoder name to the RTP codec name. Returns empty string if no mapping exists.


clockRate

static

static uint32_t clockRate(const std::string & rtpName)

Get the standard RTP clock rate for a codec.


defaultPayloadType

static

static int defaultPayloadType(const std::string & rtpName)

Get the default RTP payload type for a codec. Returns 0 for dynamic payload types (caller should assign).


specFromRtp

static

static std::optional< CodecSpec > specFromRtp(const std::string & rtpName)

Return the canonical codec spec for an RTP name, if known.


specFromFfmpeg

static

static std::optional< CodecSpec > specFromFfmpeg(const std::string & ffmpegName)

Return the canonical codec spec for an FFmpeg encoder name, if known.


specFromVideoCodec

static

static std::optional< CodecSpec > specFromVideoCodec(const av::VideoCodec & codec)

Resolve the canonical codec spec from an explicit video codec config. Prefers the FFmpeg encoder when present, otherwise falls back to RTP name.


specFromAudioCodec

static

static std::optional< CodecSpec > specFromAudioCodec(const av::AudioCodec & codec)

Resolve the canonical codec spec from an explicit audio codec config. Prefers the FFmpeg encoder when present, otherwise falls back to RTP name.


requireVideoSpec

static

static CodecSpec requireVideoSpec(const av::VideoCodec & codec)

Resolve a strict canonical video codec spec or throw.


requireAudioSpec

static

static CodecSpec requireAudioSpec(const av::AudioCodec & codec)

Resolve a strict canonical audio codec spec or throw.


resolveWebRtcVideoCodec

static

static av::VideoCodec resolveWebRtcVideoCodec(const av::VideoCodec & codec)

Resolve a browser-safe WebRTC video codec config from an explicit codec.


resolveWebRtcAudioCodec

static

static av::AudioCodec resolveWebRtcAudioCodec(const av::AudioCodec & codec)

Resolve a browser-safe WebRTC audio codec config from an explicit codec.


detectCodec

static

static std::optional< CodecSpec > detectCodec(std::string_view sdp, CodecMediaType mediaType)

Detect the first known codec present in an SDP snippet for the given media type.


detectCodecInMedia

static

static std::optional< CodecSpec > detectCodecInMedia(const rtc::Description::Media & media, CodecMediaType mediaType)

Detect the first known codec from a structured rtc::Description::Media object for the given media type.


decoderCodecId

static

static AVCodecID decoderCodecId(const CodecSpec & spec)

Return the FFmpeg decoder codec ID for a given codec spec. Returns AV_CODEC_ID_NONE if no mapping is known.