# CodecNegotiator

<a id="codecnegotiator"></a>

# CodecNegotiator

```cpp
#include <icy/webrtc/codecnegotiator.h>

class CodecNegotiator
```

[Defined in src/webrtc/include/icy/webrtc/codecnegotiator.h:118](https://github.com/auscaster/icey/blob/main/src/webrtc/include/icy/webrtc/codecnegotiator.h#L118)

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

| Return | Name | Description |
|--------|------|-------------|
| std::optional< [`NegotiatedCodec`](https://auscaster.com/icey/docs/api-reference/icy-wrtc-NegotiatedCodec.md#negotiatedcodec) > | [`negotiateVideo`](#negotiatevideo) `static` | 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 |
| std::optional< [`NegotiatedCodec`](https://auscaster.com/icey/docs/api-reference/icy-wrtc-NegotiatedCodec.md#negotiatedcodec) > | [`negotiateAudio`](#negotiateaudio) `static` | 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 |
| `bool` | [`hasEncoder`](#hasencoder) `static` | Check if FFmpeg has an encoder for the given codec name. Accepts both FFmpeg names ("libx264") and RTP names ("H264"). |
| `std::string` | [`rtpToFfmpeg`](#rtptoffmpeg) `static` | Map an RTP codec name to the preferred FFmpeg encoder name. Returns empty string if no mapping exists. |
| `std::string` | [`ffmpegToRtp`](#ffmpegtortp) `static` | Map an FFmpeg encoder name to the RTP codec name. Returns empty string if no mapping exists. |
| `uint32_t` | [`clockRate`](#clockrate-2) `static` | Get the standard RTP clock rate for a codec. |
| `int` | [`defaultPayloadType`](#defaultpayloadtype) `static` | Get the default RTP payload type for a codec. Returns 0 for dynamic payload types (caller should assign). |
| std::optional< [`CodecSpec`](https://auscaster.com/icey/docs/api-reference/icy-wrtc-CodecSpec.md#codecspec) > | [`specFromRtp`](#specfromrtp) `static` | Return the canonical codec spec for an RTP name, if known. |
| std::optional< [`CodecSpec`](https://auscaster.com/icey/docs/api-reference/icy-wrtc-CodecSpec.md#codecspec) > | [`specFromFfmpeg`](#specfromffmpeg) `static` | Return the canonical codec spec for an FFmpeg encoder name, if known. |
| std::optional< [`CodecSpec`](https://auscaster.com/icey/docs/api-reference/icy-wrtc-CodecSpec.md#codecspec) > | [`specFromVideoCodec`](#specfromvideocodec) `static` | Resolve 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`](https://auscaster.com/icey/docs/api-reference/icy-wrtc-CodecSpec.md#codecspec) > | [`specFromAudioCodec`](#specfromaudiocodec) `static` | Resolve the canonical codec spec from an explicit audio codec config. Prefers the FFmpeg encoder when present, otherwise falls back to RTP name. |
| [`CodecSpec`](https://auscaster.com/icey/docs/api-reference/icy-wrtc-CodecSpec.md#codecspec) | [`requireVideoSpec`](#requirevideospec) `static` | Resolve a strict canonical video codec spec or throw. |
| [`CodecSpec`](https://auscaster.com/icey/docs/api-reference/icy-wrtc-CodecSpec.md#codecspec) | [`requireAudioSpec`](#requireaudiospec) `static` | Resolve a strict canonical audio codec spec or throw. |
| [`av::VideoCodec`](https://auscaster.com/icey/docs/api-reference/icy-av-VideoCodec.md#videocodec) | [`resolveWebRtcVideoCodec`](#resolvewebrtcvideocodec) `static` | Resolve a browser-safe WebRTC video codec config from an explicit codec. |
| [`av::AudioCodec`](https://auscaster.com/icey/docs/api-reference/icy-av-AudioCodec.md#audiocodec) | [`resolveWebRtcAudioCodec`](#resolvewebrtcaudiocodec) `static` | Resolve a browser-safe WebRTC audio codec config from an explicit codec. |
| std::optional< [`CodecSpec`](https://auscaster.com/icey/docs/api-reference/icy-wrtc-CodecSpec.md#codecspec) > | [`detectCodec`](#detectcodec) `static` | Detect the first known codec present in an SDP snippet for the given media type. |
| std::optional< [`CodecSpec`](https://auscaster.com/icey/docs/api-reference/icy-wrtc-CodecSpec.md#codecspec) > | [`detectCodecInMedia`](#detectcodecinmedia) `static` | Detect the first known codec from a structured rtc::Description::Media object for the given media type. |
| `AVCodecID` | [`decoderCodecId`](#decodercodecid) `static` | Return the FFmpeg decoder codec ID for a given codec spec. Returns AV_CODEC_ID_NONE if no mapping is known. |

---

<a id="negotiatevideo"></a>

### negotiateVideo

`static`

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

[Defined in src/webrtc/include/icy/webrtc/codecnegotiator.h:125](https://github.com/auscaster/icey/blob/main/src/webrtc/include/icy/webrtc/codecnegotiator.h#L125)

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

---

<a id="negotiateaudio"></a>

### negotiateAudio

`static`

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

[Defined in src/webrtc/include/icy/webrtc/codecnegotiator.h:131](https://github.com/auscaster/icey/blob/main/src/webrtc/include/icy/webrtc/codecnegotiator.h#L131)

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

---

<a id="hasencoder"></a>

### hasEncoder

`static`

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

[Defined in src/webrtc/include/icy/webrtc/codecnegotiator.h:135](https://github.com/auscaster/icey/blob/main/src/webrtc/include/icy/webrtc/codecnegotiator.h#L135)

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

---

<a id="rtptoffmpeg"></a>

### rtpToFfmpeg

`static`

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

[Defined in src/webrtc/include/icy/webrtc/codecnegotiator.h:139](https://github.com/auscaster/icey/blob/main/src/webrtc/include/icy/webrtc/codecnegotiator.h#L139)

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

---

<a id="ffmpegtortp"></a>

### ffmpegToRtp

`static`

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

[Defined in src/webrtc/include/icy/webrtc/codecnegotiator.h:143](https://github.com/auscaster/icey/blob/main/src/webrtc/include/icy/webrtc/codecnegotiator.h#L143)

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

---

<a id="clockrate-2"></a>

### clockRate

`static`

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

[Defined in src/webrtc/include/icy/webrtc/codecnegotiator.h:146](https://github.com/auscaster/icey/blob/main/src/webrtc/include/icy/webrtc/codecnegotiator.h#L146)

Get the standard RTP clock rate for a codec.

---

<a id="defaultpayloadtype"></a>

### defaultPayloadType

`static`

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

[Defined in src/webrtc/include/icy/webrtc/codecnegotiator.h:150](https://github.com/auscaster/icey/blob/main/src/webrtc/include/icy/webrtc/codecnegotiator.h#L150)

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

---

<a id="specfromrtp"></a>

### specFromRtp

`static`

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

[Defined in src/webrtc/include/icy/webrtc/codecnegotiator.h:154](https://github.com/auscaster/icey/blob/main/src/webrtc/include/icy/webrtc/codecnegotiator.h#L154)

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

---

<a id="specfromffmpeg"></a>

### specFromFfmpeg

`static`

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

[Defined in src/webrtc/include/icy/webrtc/codecnegotiator.h:158](https://github.com/auscaster/icey/blob/main/src/webrtc/include/icy/webrtc/codecnegotiator.h#L158)

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

---

<a id="specfromvideocodec"></a>

### specFromVideoCodec

`static`

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

[Defined in src/webrtc/include/icy/webrtc/codecnegotiator.h:163](https://github.com/auscaster/icey/blob/main/src/webrtc/include/icy/webrtc/codecnegotiator.h#L163)

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

---

<a id="specfromaudiocodec"></a>

### specFromAudioCodec

`static`

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

[Defined in src/webrtc/include/icy/webrtc/codecnegotiator.h:168](https://github.com/auscaster/icey/blob/main/src/webrtc/include/icy/webrtc/codecnegotiator.h#L168)

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

---

<a id="requirevideospec"></a>

### requireVideoSpec

`static`

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

[Defined in src/webrtc/include/icy/webrtc/codecnegotiator.h:171](https://github.com/auscaster/icey/blob/main/src/webrtc/include/icy/webrtc/codecnegotiator.h#L171)

Resolve a strict canonical video codec spec or throw.

---

<a id="requireaudiospec"></a>

### requireAudioSpec

`static`

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

[Defined in src/webrtc/include/icy/webrtc/codecnegotiator.h:174](https://github.com/auscaster/icey/blob/main/src/webrtc/include/icy/webrtc/codecnegotiator.h#L174)

Resolve a strict canonical audio codec spec or throw.

---

<a id="resolvewebrtcvideocodec"></a>

### resolveWebRtcVideoCodec

`static`

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

[Defined in src/webrtc/include/icy/webrtc/codecnegotiator.h:178](https://github.com/auscaster/icey/blob/main/src/webrtc/include/icy/webrtc/codecnegotiator.h#L178)

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

---

<a id="resolvewebrtcaudiocodec"></a>

### resolveWebRtcAudioCodec

`static`

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

[Defined in src/webrtc/include/icy/webrtc/codecnegotiator.h:182](https://github.com/auscaster/icey/blob/main/src/webrtc/include/icy/webrtc/codecnegotiator.h#L182)

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

---

<a id="detectcodec"></a>

### detectCodec

`static`

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

[Defined in src/webrtc/include/icy/webrtc/codecnegotiator.h:187](https://github.com/auscaster/icey/blob/main/src/webrtc/include/icy/webrtc/codecnegotiator.h#L187)

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

---

<a id="detectcodecinmedia"></a>

### detectCodecInMedia

`static`

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

[Defined in src/webrtc/include/icy/webrtc/codecnegotiator.h:192](https://github.com/auscaster/icey/blob/main/src/webrtc/include/icy/webrtc/codecnegotiator.h#L192)

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

---

<a id="decodercodecid"></a>

### decoderCodecId

`static`

```cpp
static AVCodecID decoderCodecId(const CodecSpec & spec)
```

[Defined in src/webrtc/include/icy/webrtc/codecnegotiator.h:197](https://github.com/auscaster/icey/blob/main/src/webrtc/include/icy/webrtc/codecnegotiator.h#L197)

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