From 0646de8b0a2f2db537f5b8c3fbb941da92ee7788 Mon Sep 17 00:00:00 2001 From: shaunjstokes Date: Mon, 14 Oct 2024 13:18:57 +0200 Subject: [PATCH 1/2] [core/media] allow transcoding between different rates in Opus MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit In vars.xml , when you use codec settings like this: `` The switch_core_media.c line 5520 only takes the OPUS default rate (48k) instead of 16k in the config. Because of this, FS won't be able to choose opus if your client asks for 16k opus. You will see same log symptom as #2226 We've found that the patch #2582 has the side effect of causing calls that fail to negotiate the same codec as Leg A for Leg B to fail with SIP 488 INCOMPATIBLE_DESTINATION. That's not an option for us, we need backwards compatibility with other codecs. Rather than apply this for all codecs it should only apply for Opus. Now calls using Opus and other codecs establish correctly on both legs, and there are no issues with codec negotiation when the codecs on Leg A and Leg B don't match. Tested-by: Jérôme Poulin Co-authored-by: magiclin99 --- src/switch_core_media.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/switch_core_media.c b/src/switch_core_media.c index de5d0eff74c..8b533fa50eb 100644 --- a/src/switch_core_media.c +++ b/src/switch_core_media.c @@ -5517,6 +5517,10 @@ SWITCH_DECLARE(uint8_t) switch_core_media_negotiate_sdp(switch_core_session_t *s uint32_t bit_rate = imp->bits_per_second; uint32_t codec_rate = imp->samples_per_second; + if (!strcasecmp(map->rm_encoding, "opus")) { + codec_rate = imp->actual_samples_per_second; + } + if (imp->codec_type != SWITCH_CODEC_TYPE_AUDIO) { continue; } From cc5cca32df37f903e82eae4ae52d338acbaf9fd0 Mon Sep 17 00:00:00 2001 From: Dat Nguyen Date: Fri, 4 Jul 2025 14:33:57 +0700 Subject: [PATCH 2/2] [core/media] update remote_codec_rate before codec comparison MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When the configured candidates are OPUS 48kHz, PCMA,..., the negotiation unexpectedly matches OPUS 16k with OPUS 48kHz. This occurs because the implementation updates the remote sample rate after the initial media negotiation (cooperation loop), leading to incorrect matching. Before: ``` 2025-07-04 15:56:19.826475 96.27% [DEBUG] sofia.c:7586 Remote SDP: v=0 o=- 3960604579 3960604579 IN IP4 52.76.213.59 s=pjmedia b=AS:50 t=0 0 a=X-nat:0 m=audio 24598 RTP/AVP 96 120 c=IN IP4 52.76.213.59 b=TIAS:32000 a=rtpmap:96 opus/48000/2 a=fmtp:96 useinbandfec=1;maxplaybackrate=16000;sprop-maxcapturerate=16000 a=rtpmap:120 telephone-event/48000 a=fmtp:120 0-16 a=ssrc:184830022 cname:742327632f371b3d a=rtcp:24599 switch_core_media.c:5526 Audio Codec Compare [opus:96:48000:20:0:1]/[opus:116:48000:20:0:1] switch_core_media.c:5569 Audio Codec Compare [opus:116:48000:20:0:1] is saved as a near-match switch_core_media.c:5526 Audio Codec Compare [opus:96:16000:20:0:1]/[PCMU:0:8000:20:64000:1] switch_core_media.c:5526 Audio Codec Compare [opus:96:16000:20:0:1]/[G729:18:8000:20:8000:1] switch_core_media.c:5526 Audio Codec Compare [opus:96:16000:20:0:1]/[GSM:3:8000:20:13200:1] ``` As shown in the log above, the sample rate compared with the first supported codec is 48kHz. It should be 16kHz as configured by `sprop-maxcapturerate`. After this patch, the sample rate is updated to 16kHz before comparison: ``` 2025-07-04 17:24:02.999514 97.50% [DEBUG] sofia.c:7586 Remote SDP: v=0 o=- 3960609842 3960609842 IN IP4 52.76.213.59 s=pjmedia b=AS:50 t=0 0 a=X-nat:0 m=audio 19208 RTP/AVP 96 120 c=IN IP4 52.76.213.59 b=TIAS:32000 a=rtpmap:96 opus/48000/2 a=fmtp:96 useinbandfec=1;maxplaybackrate=16000;sprop-maxcapturerate=16000 a=rtpmap:120 telephone-event/48000 a=fmtp:120 0-16 a=ssrc:17356830 cname:4cb67c6474c259d7 a=rtcp:19209 switch_core_media.c:5529 Audio Codec Compare [opus:96:16000:20:0:1]/[opus:116:48000:20:0:1] switch_core_media.c:5529 Audio Codec Compare [opus:96:16000:20:0:1]/[PCMU:0:8000:20:64000:1] switch_core_media.c:5529 Audio Codec Compare [opus:96:16000:20:0:1]/[G729:18:8000:20:8000:1] switch_core_media.c:5529 Audio Codec Compare [opus:96:16000:20:0:1]/[GSM:3:8000:20:13200:1] ``` Tested-By: Jérôme Poulin --- src/switch_core_media.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/switch_core_media.c b/src/switch_core_media.c index 8b533fa50eb..493f5f7f8f3 100644 --- a/src/switch_core_media.c +++ b/src/switch_core_media.c @@ -5512,6 +5512,9 @@ SWITCH_DECLARE(uint8_t) switch_core_media_negotiate_sdp(switch_core_session_t *s } } + if (fmtp_remote_codec_rate) { + remote_codec_rate = fmtp_remote_codec_rate; + } for (i = 0; i < smh->mparams->num_codecs && i < total_codecs; i++) { const switch_codec_implementation_t *imp = codec_array[i]; uint32_t bit_rate = imp->bits_per_second; @@ -5533,10 +5536,7 @@ SWITCH_DECLARE(uint8_t) switch_core_media_negotiate_sdp(switch_core_session_t *s } else { match = (!strcasecmp(rm_encoding, imp->iananame) && ((map->rm_pt < 96 && imp->ianacode < 96) || (map->rm_pt > 95 && imp->ianacode > 95)) && - (remote_codec_rate == codec_rate || fmtp_remote_codec_rate == imp->actual_samples_per_second)) ? 1 : 0; - if (fmtp_remote_codec_rate) { - remote_codec_rate = fmtp_remote_codec_rate; - } + (remote_codec_rate == codec_rate || remote_codec_rate == imp->actual_samples_per_second)) ? 1 : 0; } if (match && bit_rate && map_bit_rate && map_bit_rate != bit_rate && strcasecmp(map->rm_encoding, "ilbc") &&