# Explicitly setting a non-default codec when creating producer in libmediasoupclient

**URL:** <https://mediasoup.discourse.group/t/explicitly-setting-a-non-default-codec-when-creating-producer-in-libmediasoupclient/5353>\
**Category:** mediasoup libraries\
**Created:** [July 14, 2023, 11:42am UTC](https://mediasoup.discourse.group/t/explicitly-setting-a-non-default-codec-when-creating-producer-in-libmediasoupclient/5353 "2023-07-14T11:42:27Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![fedulvtubudul](https://yyz2.discourse-cdn.com/free1/user_avatar/mediasoup.discourse.group/fedulvtubudul/32/1128_2.png) [@fedulvtubudul](https://mediasoup.discourse.group/u/fedulvtubudul)\
**Post date:** [July 14, 2023, 11:42am UTC](https://mediasoup.discourse.group/t/explicitly-setting-a-non-default-codec-when-creating-producer-in-libmediasoupclient/5353/1 "2023-07-14T11:42:27Z")

</div>

I can’t create two video producers with different codecs on same SendTransport using up-to-date libmediasoupclient version (from m112 branch).

Our media server declares both h264 and vp8 codecs as supported (both of them are listed in router RTP capabilities). After initialising and loading a mediasoup client device, I can also see both of them in device’s RTP capabilities. We have some custom logic on the client side for selecting producer codec, just like [this document](https://mediasoup.org/documentation/v3/tricks/#using-a-different-video-codec-in-each-producer) suggests.

When creating a producer, we pass desired codec as `codec` parameter to SendTransport. That approach works on JS client as expected. But on the native client (iOS, using the libmediasoupclient) the `codec` parameter doesn’t work. After digging into the implementation, I’ve found that selecting a non-default codec is not assumed by underlying code.

Here is a simplified trace that shows the error (as I see it):

```plaintext
1. Send transport creation
Application level code
Device::CreateSendTransport(...)
SendTransport::SendTransport(...)
ortc::getSendingRtpParameters(...) / getSendingRemoteRtpParameters(...)

2. Producer creation
Application level code
SendTransport::Produce(..., codec, ...)
SendHandler::Send(..., codec)
ortc::reduceCodecs(..., codec);

```

On the first step, inside `getSendingRtpParameters(...)` / `getSendingRemoteRtpParameters(...)` methods we have a `break` in loop over codecs after finding the first codec with given media kind. There is also a comment stating “We assume a single media codec plus an optional RTX codec.”. So after the first step `SendHandler` only has one supported codec per media kind.

On the second step we pass the `codec` parameter from application level down to `reduceCodecs(...)` method, which will search in array always containing only one codec.

If I remove the `break` statement from `getSendingRtpParameters(...)` / `getSendingRemoteRtpParameters(...)` methods, producer codec selection begins to work. So I want to known if it’s safe and what was the original intention to limit this codecs array in SendHandler? Shouldn’t we have all the supported codecs now as there is the `codec` parameter in producer creation method?

---

<div class="post-metadata">

**Author:** ![ibc](https://yyz2.discourse-cdn.com/free1/user_avatar/mediasoup.discourse.group/ibc/32/1540_2.png) [@ibc](https://mediasoup.discourse.group/u/ibc)\
**Post date:** [July 14, 2023, 12:20pm UTC](https://mediasoup.discourse.group/t/explicitly-setting-a-non-default-codec-when-creating-producer-in-libmediasoupclient/5353/2 "2023-07-14T12:20:44Z")

</div>

This looks like a bug in the code. If you feel brave write a PR please. Otherwise fill and issue in GH. In both cases ensure (or highlight) that the code in libmediasoupclient matches (or must match) the corresponding code in mediasoup-client, please.

---

<div class="post-metadata">

**Author:** ![fedulvtubudul](https://yyz2.discourse-cdn.com/free1/user_avatar/mediasoup.discourse.group/fedulvtubudul/32/1128_2.png) [@fedulvtubudul](https://mediasoup.discourse.group/u/fedulvtubudul)\
**Post date:** [August 1, 2023, 1:03am UTC](https://mediasoup.discourse.group/t/explicitly-setting-a-non-default-codec-when-creating-producer-in-libmediasoupclient/5353/3 "2023-08-01T01:03:35Z")

</div>

Sorry for a delay. I’ve posted [an issue](https://github.com/versatica/libmediasoupclient/issues/163) and related [pull request](https://github.com/versatica/libmediasoupclient/pull/164) on github.
