Repository navigation
Conversation
|
@andywolk @morbit85 this consolidates the stalled Opus@16k negotiation PRs (#2582 / #2623 / #2833) and adds the RFC 7587 timestamp fix for #2226, recording-safe (mod_opus.c untouched, avoids the samples=480 approach @ticpu flagged). A per-issue "Failure modes covered" matrix and the validation table are in the description. cc @ticpu (diagnosed the samples=480 recording/playback breakage and tested the negotiation commits), @nltd101, @shaunjstokes, @magiclin99. Authorship is preserved on the cherry-picks; a sanity-check that I represented your fixes |
c7f4f48 to
2c829fd
Compare
|
Our test setup is not available until mid to end of August, I can't try these yet but really want and need to test these as we have some situations that we still degrade to G722 to prevent issues. |
|
This is now applied on our development box. I'll provide further feedback once I've had a chance to test thoroughly. |
ae7c494 to
71fb26e
Compare
|
We've been unable to reproduce any issues in our dev environment after deploying this patch. |
d0ab944 to
cceb572
Compare
cceb572 to
e25b812
Compare
8db21a3 to
ee28a63
Compare
|
I tested this against its base (12ac48a) by sending SDP offers with SIPp to the external profile (Debian bookworm, built from source,
With I think it comes from setting |
Oops. Looking into this now. |
|
Re-tested b0d94b8 with the same offers, against the base (12ac48a), now with three prefs:
The stock-prefs 488 is gone, and the PCMU fallback now becomes Opus with the stock prefs too, not only with |
Add the OPUS@8000h,PCMU prefs from the signalwire#3064 review: 16k-constrained, plain and 8k Opus offers, with and without PCMU. All of them negotiate Opus at 8 kHz, the configured rate; the base answers a lone 16k offer at 16 kHz and falls back to PCMU when PCMU is offered. Each case originates a session, and FreeSWITCH's default limit of 30 sessions per second lets 29 start between its one-second refreshes. The 29 sessions this binary already started fit exactly; with the new cases some rows failed with "Throttle Error". Raise the limit in the test and wait for the refresh that applies it. Signed-off-by: Calvin Ellison <cellison@youmail.com>
|
Thanks for re-testing. For OPUS@8000h,PCMU the answer is Opus at 8 kHz in all three rows: the rework matches the first configured Opus rate and only moves down to the offered rate, never up. On 12ac48a the lone 16k offer got Opus at 16 kHz through the near-match substitution, so that row changes rate, not codec. Your prefs are in the unit test now (55363a7). The RFC 7587 part is covered by our harness and a decode test (now in the description): on 12ac48a an AMR-WB/16000 leg bridged to Opus 16k is garbled both ways, ts step 320 on the Opus side and 960 on the AMR-WB side. On this head both are correct. |
Add the OPUS@8000h,PCMU prefs from the signalwire#3064 review: 16k-constrained, plain and 8k Opus offers, with and without PCMU. All of them negotiate Opus at 8 kHz, the configured rate; the base answers a lone 16k offer at 16 kHz and falls back to PCMU when PCMU is offered. Each case originates a session, and FreeSWITCH's default limit of 30 sessions per second lets 29 start between its one-second refreshes. The 29 sessions this binary already started fit exactly; with the new cases some rows failed with "Throttle Error". Raise the limit in the test and wait for the refresh that applies it. Signed-off-by: Calvin Ellison <cellison@youmail.com>
55363a7 to
95a80d7
Compare
|
Re-ran the same SIPp offers on the rebased head (95a80d7) against its new base (f37a27f), both built from source, with one more offer: Opus capped at 8 kHz plus PCMU.
The three original rows match what b0d94b8 gave, and the 8 kHz-capped offer behaves like the 16 kHz one. I didn't run the new unit tests: my image only builds the modules these SIP tests need, and |
95a80d7 to
fbe836d
Compare
Add the OPUS@8000h,PCMU prefs from the signalwire#3064 review: 16k-constrained, plain and 8k Opus offers, with and without PCMU. All of them negotiate Opus at 8 kHz, the configured rate; the base answers a lone 16k offer at 16 kHz and falls back to PCMU when PCMU is offered. Each case originates a session, and FreeSWITCH's default limit of 30 sessions per second lets 29 start between its one-second refreshes. The 29 sessions this binary already started fit exactly; with the new cases some rows failed with "Throttle Error". Raise the limit in the test and wait for the refresh that applies it. Signed-off-by: Calvin Ellison <cellison@youmail.com>
In vars.xml , when you use codec settings like this: `<X-PRE-PROCESS cmd="set" data="internal_codec_prefs=OPUS@16000h@1c,PCMU"/>` 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 signalwire#2226 We've found that the patch signalwire#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 <jeromepoulin@gmail.com> Co-authored-by: magiclin99 <magiclin99@gmail.com> (cherry picked from commit 0646de8) Signed-off-by: Calvin Ellison <cellison@youmail.com>
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 <jeromepoulin@gmail.com> (cherry picked from commit cc5cca3) Signed-off-by: Calvin Ellison <cellison@youmail.com>
) RFC 7587 requires Opus RTP timestamps to advance at a fixed 48 kHz clock regardless of the payload sample rate. For 16k/8k Opus this was wrong in two coupled spots, so a same-rate transcode (AMR-WB/16000 -> Opus, as offered by some carriers) advanced the Opus timestamp at 16 kHz (320/20ms) instead of 48 kHz (960/20ms), breaking audio on endpoints that honour the RFC 7587 clock. 1. switch_core_media_set_codec(): after negotiation, switch_rtp_change_interval() reset samples_per_interval to read_impl.samples_per_packet and omitted the Opus 48 kHz clock that switch_rtp_new() already applies at init. Derive the interval from samples_per_second for Opus, matching the init path. 2. switch_core_session_write_frame() encode path: it only regenerated the RTP timestamp (instead of passing the source timestamp through) when samples_per_packet changed. A same-rate transcode keeps the same samples_per_packet (AMR-WB 16k and Opus 16k both pack 320 samples/20ms), so the 16 kHz source timestamp was passed onto the 48 kHz Opus stream. Also regenerate when samples_per_second (the RTP clock rate) differs. This reuses FreeSWITCH's existing timestamp-regeneration path rather than a per-packet timestamp rescaler, and leaves samples_per_packet at the true PCM frame count (160/80), so media bugs, recording and playback that read it are unaffected. Validated with a same-rate AMR-WB->Opus transcode: the B-leg Opus RTP timestamp delta goes from 320 to 960, with recording intact. Signed-off-by: Calvin Ellison <cellison@youmail.com>
…ire#2226) Regression test for the recording-safe property of the RFC 7587 timestamp fix. The Opus RTP clock is 48 kHz (samples_per_second == 48000) regardless of the audio rate, but samples_per_packet must stay the TRUE decoded PCM frame count (rate * ptime), because media bugs, record_session and playback size PCM frames from it. Assert the 8k/16k/48k opus implementations register 160/320/960 samples per 20 ms frame (not the 48 kHz-domain value), catching any reintroduction of a mod_opus.c samples=480 hack. Signed-off-by: Calvin Ellison <cellison@youmail.com>
…#2226) The negotiation commits earlier in this branch match an Opus offer only against the implementation at its fmtp rate (sprop-maxcapturerate). When that rate is not configured, no Opus implementation matches at all: - stock prefs (OPUS is 48 kHz only): a 16k- or 8k-constrained Opus offer on its own is rejected with 488; - a 24k- or 12k-constrained offer is rejected under any prefs, since mod_opus registers no 24 or 12 kHz implementation; - prefs with OPUS@16000h but no OPUS: a plain or WebRTC-style Opus offer plus PCMU negotiates PCMU, where the base negotiated Opus. RFC 7587 fixes the Opus rtpmap at opus/48000/2 and any Opus decoder plays any rate, so every configured Opus implementation is a valid answer. Match exactly one per Opus m-line: the configured one at the offered rate, otherwise the first one configured, and do not demote it to a near-match over its rate. When the offer caps Opus below the matched rate, use the loaded implementation at the offered rate if there is one, as the near-match path did before; scrooge keeps the configured implementation. Matching exactly one also avoids a payload map collision: every Opus implementation registers a 48 kHz RTP clock, so several matches for one payload type share a payload map and the last match's rate wins. The rate switch runs after greedy_sort(), which only keeps implementations listed in codec_array. Signed-off-by: Calvin Ellison <cellison@youmail.com>
) Negotiate 28 Opus offers (16k, 8k and 24k constrained, plain, WebRTC-style, with and without PCMU, PCMU first, ptime 40) against stock, multi-rate and 16k-only codec prefs in generous, greedy and scrooge modes, and check the codec and rate that switch_core_media_negotiate_sdp() selects. On the base (12ac48a) 7 cases fail, all Opus losing to PCMU (GH signalwire#2226). Without the previous commit (at ee28a63) 15 fail, 8 of them by rejecting the offer. Signed-off-by: Calvin Ellison <cellison@youmail.com>
Add the OPUS@8000h,PCMU prefs from the signalwire#3064 review: 16k-constrained, plain and 8k Opus offers, with and without PCMU. All of them negotiate Opus at 8 kHz, the configured rate; the base answers a lone 16k offer at 16 kHz and falls back to PCMU when PCMU is offered. Each case originates a session, and FreeSWITCH's default limit of 30 sessions per second lets 29 start between its one-second refreshes. The 29 sessions this binary already started fit exactly; with the new cases some rows failed with "Throttle Error". Raise the limit in the test and wait for the refresh that applies it. Signed-off-by: Calvin Ellison <cellison@youmail.com>
fbe836d to
e57f81c
Compare
Description
Update 2026-11-06. @JROtto5 found in review that commits 1 and 2 reject a lone 16k-constrained Opus offer with
488when only the default 48 kHz Opus is configured, where master answers it with Opus. A wider test matrix found more cases, so commit 5 reworks the Opus match and commit 6 adds a negotiation unit test, which commit 7 extends with theOPUS@8000h,PCMUprefs @JROtto5 re-tested (34 cases). The earlier version of this description also said master rejects that offer with488; that was wrong. Master answers it through the near-match path, and the488came from this PR. It also said the non-Opus path was byte-identical to stock; it behaves the same, but the code is not identical. A media decode test (under Validation) also found the timestamp bug in the Opus to AMR-WB direction, which commit 3 already fixes.Problem
Two defects break Opus below 48 kHz (#2226):
Negotiation. An offer of
opus/48000/2constrained to 16 kHz (maxplaybackrate=16000;sprop-maxcapturerate=16000) is only saved as a near-match against the configured Opus, because the loop compares the constrained rate with Opus's 48 kHz clock. A near-match is used only when nothing else matches, so when the offer also lists PCMU (the usual case with carriers) the call connects on PCMU even though both ends support Opus. The Incorrect OPUS negotiation #2226 reporter saw it start in 1.10.10.RFC 7587 RTP timestamp. RFC 7587 section 4.1 requires the Opus RTP timestamp to advance at 48 kHz whatever the audio rate. On a same-rate transcode (
AMR-WB/16000to Opus at 16 kHz, as some carriers offer) FreeSWITCH advanced it at 16 kHz, 320 per 20 ms instead of 960, which breaks audio on endpoints that follow the RFC. The reverse direction has the same cause: Opus at 16 kHz toAMR-WB/16000passed the Opus timestamp through, so the AMR-WB stream advanced 960 per 20 ms instead of 320. Resampled paths such as PCMU 8k to Opus were already correct.Fix
Negotiation (commits 1, 2 and 5). RFC 7587 fixes the Opus rtpmap at
opus/48000/2and any Opus decoder plays any rate, so any configured Opus implementation is a valid answer to an Opus offer. Each Opus m-line now matches exactly one configured Opus rate:sprop-maxcapturerate), if there is one;That match is never demoted to a near-match over its rate, so Opus no longer loses to PCMU. When the offer caps Opus below the matched rate, the loaded implementation at the offered rate is used if one exists, as the near-match path did before; scrooge keeps the configured implementation. The rate switch runs after
greedy_sort(), which only keeps implementations listed in the codec prefs.Matching only one also avoids a payload map collision: every Opus implementation registers a 48 kHz RTP clock, so several Opus matches for one payload type share a payload map and the last match's rate wins.
Commits 1 and 2 on their own matched only the implementation at the exact offered rate. That rejected the offer whenever that rate wasn't configured (default prefs list only the 48 kHz implementation, and mod_opus has no 12 or 24 kHz one) and made prefs with
OPUS@16000hbut noOPUSpick PCMU for a plain or WebRTC Opus offer. Commit 5 replaces that rule.Behaviour change to note: with default prefs, a 16k-constrained Opus offer plus PCMU now negotiates Opus at 16 kHz where master picks PCMU. That is the #2226 fix for the reporter's setup, but it changes the codec for anyone who relied on the PCMU fallback.
RTP timestamp (commit 3), in two coupled spots:
switch_core_media_set_codec(): after negotiation,switch_rtp_change_interval()reset the RTP interval tosamples_per_packetand dropped the 48 kHz Opus clock thatswitch_rtp_new()applies at init. It now mirrors init.switch_core_session_write_frame(): the RTP timestamp was regenerated only whensamples_per_packetchanged. AMR-WB 16k and Opus 16k both pack 320 samples per 20 ms, so it now also regenerates whensamples_per_second(the RTP clock) differs. That covers both directions.Recording-safe. This reuses the existing timestamp regeneration path and leaves
samples_per_packetat the true PCM frame count (320 at 16 kHz, 160 at 8 kHz per 20 ms), so nothing that reads the per-frame sample count changes. An earlier approach setsamples = 480inmod_opus.c; @ticpu reported on #2832 that it broke playback and captures and caused one-way audio, which is why it was dropped. This PR does not touchmod_opus.c. (record_sessionrecomputes its frame length from the byte count, so it tolerates thesamples=480mislabel; we verified that, but the approach here doesn't rely on it.) Commit 4 asserts that the 8k, 16k and 48k Opus implementations keep 160, 320 and 960 samples per 20 ms.Failure modes covered
OPUS@16000hwas configured. Unit test, SIPp, harness.488on non-Opus legs. The rework only changes how Opus offers match; non-Opus codecs still match on the rtpmap rate. Unit test PCMU-only case, SIPp.488(introduced by commits 1 and 2, found in review): 16k or 8k with only 48 kHz configured, 12k or 24k under any prefs. Commit 5. Unit test, SIPp.OPUS@16000hbut noOPUSpick PCMU for a plain or WebRTC Opus offer plus PCMU (introduced by commits 1 and 2, found in review). Commit 5. Unit test, SIPp.samples=480broke recording, playback and captures. Avoided;mod_opus.cuntouched. Harness:record_sessionWAV intact.Out of scope: G.722's 8 kHz clock with 16 kHz audio is unchanged.
Validation
Negotiation unit test (
tests/unit/switch_core_media.c,test_opus_offer_negotiation). 34 offers (16k, 8k and 24k constrained, plain, WebRTC-style, with and without PCMU, PCMU first, ptime 40) against default (OPUS,PCMU,PCMA), multi-rate (OPUS@16000h,OPUS@8000h,OPUS,PCMU,PCMA), 16k-only (OPUS@16000h,PCMU,PCMA) and 8k-only (OPUS@8000h,PCMU) prefs in generous, greedy and scrooge modes, checking the codec and rateswitch_core_media_negotiate_sdp()selects.Plain reading: master loses Opus to PCMU in 9 cases and moves a lone 16k offer up to 16 kHz when only 8 kHz is configured; commits 1-4 fix four of the PCMU cases but reject Opus outright in 10 others and still pick PCMU in 10; this PR passes all 34.
End to end. SIPp offers (16k alone, 16k plus PCMU, 24k alone, plain alone, plain plus PCMU, WebRTC-style plus PCMU) to three sofia profiles with the prefs above, generous negotiation, 18 calls per build:
200with Opus200with PCMU488The unit test and SIPp agree on every case they share.
Media. The SIPp calls above carry no audio, so each new negotiation path also ran as a real two-way call. Caller and callee each replay a different synthetic sentence as RTP, encoded with libopus, vo-amrwbenc or G.711 outside FreeSWITCH. What FreeSWITCH sends on each leg is decoded the same way (libopus, opencore-amrwb, G.711; not mod_opus), laid out by RTP timestamp as a receiver would play it, and compared with the sentence the other side sent: spectral envelope correlation, coverage and drift. The pass thresholds were set from synthetic chains before any FreeSWITCH result: good codec chains score 0.955 to 0.992, the wrong sentence at most 0.611, and a stream with timestamp step 320 scores 0.574 with 1080 ms drift. Each case ran 3 times, so 6/6 means both directions in every run.
Plain reading: on master the AMR-WB to Opus call is garbled in both directions; with this PR every case carries the right audio both ways, at the right speed and with no packet loss. Single 10 to 40 ms frame slips from the loopback rig's timing show up on both builds at about the same rate. This is loopback with synthetic speech: it shows the right audio arrives intact, not a perceptual quality score.
Transcoding harness. Real AMR-WB media replayed into FreeSWITCH, bridged to an Opus 16k UAS, RTP timestamps measured on the wire:
OPUS@16000h,OPUS,AMR-WB,PCMU,PCMArecord_sessionThe core library still builds with
-Werror.Type of Change
Related Issues
Credit / relationship to prior PRs
This consolidates and supersedes the stalled negotiation PRs, with credit to the original authors (their commits are cherry-picked here, authorship preserved):
488on mismatched non-Opus legs)remote_codec_ratebefore the comparisonsamples=480timestamp approach breaks recording, playback and captures, and tested the negotiation commits488regression in commits 1 and 2 during reviewThe RFC 7587 timestamp fix (commit 3) is the piece none of the above landed: #2832 sidestepped the timestamp by forcing a transcode but left the clock wrong.
Testing
Commits 1 to 3 have been in our production FreeSWITCH fleet since July 2026, carrying PSTN (AMR-WB and PCMU) to Opus 16k VoIP calls. Commit 5 is validated by the unit test, SIPp and the harness above and is not in production yet.
Unit suites run on this branch:
switch_core_media,switch_core_codec,switch_rtpandswitch_rtp_pcap, all passing. I haven't run the full unit suite locally.Checklist