Skip to content

[core/media] Opus: multi-rate transcoding negotiation + RFC 7587 RTP timestamp (#2226) - #3064

Open
celliso1 wants to merge 7 commits into
signalwire:masterfrom
celliso1:opus-transcoding-rfc7587
Open

celliso1 wants to merge 7 commits into
signalwire:masterfrom
celliso1:opus-transcoding-rfc7587

Conversation

@celliso1

@celliso1 celliso1 commented Jul 3, 2026 •

Copy link
Copy Markdown
Contributor

Description

Update 2026-11-06. @JROtto5 found in review that commits 1 and 2 reject a lone 16k-constrained Opus offer with 488 when 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 the OPUS@8000h,PCMU prefs @JROtto5 re-tested (34 cases). The earlier version of this description also said master rejects that offer with 488; that was wrong. Master answers it through the near-match path, and the 488 came 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):

  1. Negotiation. An offer of opus/48000/2 constrained 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.

  2. 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/16000 to 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 to AMR-WB/16000 passed 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/2 and 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:

  • the configured implementation at the offered rate (sprop-maxcapturerate), if there is one;
  • otherwise the first Opus implementation in the codec prefs.

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@16000h but no OPUS pick 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 to samples_per_packet and dropped the 48 kHz Opus clock that switch_rtp_new() applies at init. It now mirrors init.
  • switch_core_session_write_frame(): the RTP timestamp was regenerated only when samples_per_packet changed. AMR-WB 16k and Opus 16k both pack 320 samples per 20 ms, so it now also regenerates when samples_per_second (the RTP clock) differs. That covers both directions.

Recording-safe. This reuses the existing timestamp regeneration path and leaves samples_per_packet at 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 set samples = 480 in mod_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 touch mod_opus.c. (record_session recomputes its frame length from the byte count, so it tolerates the samples=480 mislabel; 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

  • Incorrect OPUS negotiation #2226: 16k-constrained Opus plus PCMU selects PCMU. Fixed by commit 5 for default prefs (the reporter's setup) as well as multi-rate prefs; commits 1 and 2 alone fixed it only when OPUS@16000h was configured. Unit test, SIPp, harness.
  • Update remote_codec_rate before codec comparison #2833: fmtp rate applied mid-loop, so the offered rate was compared inconsistently. Commit 2 sets it before the loop. Unit test.
  • Fix bug #2226 #2582: its all-codecs form caused 488 on 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.
  • Lone constrained Opus offer rejected with 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.
  • Prefs with OPUS@16000h but no OPUS pick PCMU for a plain or WebRTC Opus offer plus PCMU (introduced by commits 1 and 2, found in review). Commit 5. Unit test, SIPp.
  • [mod_opus] Fix to allow transcoding between different rates #2832 / Incorrect OPUS negotiation #2226: wrong-clock Opus RTP timestamp on same-rate transcodes. Commit 3. Harness: B-leg ts delta 320 to 960; media test.
  • Opus to AMR-WB on the same-rate path sends AMR-WB at the Opus clock (960 per 20 ms instead of 320; found by the media test). Commit 3, same check. Media test.
  • [mod_opus] Fix to allow transcoding between different rates #2832: samples=480 broke recording, playback and captures. Avoided; mod_opus.c untouched. Harness: record_session WAV 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 rate switch_core_media_negotiate_sdp() selects.

master (12ac48a) commits 1-4 (ee28a63) this PR
failing cases, of 34 10 20 0
Opus offered and configured, PCMU selected 9 10 0
Opus offer rejected though Opus is configured 0 10 0
Opus moved above the configured rate 1 0 0

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:

master (12ac48a) commits 1-4 (ee28a63) this PR
200 with Opus 15 10 18
200 with PCMU 3 3 0
488 0 5 0

The 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.

case master (12ac48a) this PR
default prefs, 16k-constrained Opus 6/6 6/6
16k-only prefs, WebRTC-style Opus 6/6 5/6, one 40 ms timing slip that passed on rerun
default prefs, 24k-constrained Opus 6/6 6/6
default prefs, 16k-constrained Opus plus PCMU negotiates PCMU 6/6
multi-rate prefs, 16k-constrained Opus 6/6 6/6
PCMU plus AMR-WB caller (greedy) bridged to an Opus 16k callee 0/6: Opus leg ts step 320, AMR-WB leg ts step 960 6/6

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:

check stock v1.11.1 this PR
Opus 16k plus PCMU offer, prefs OPUS@16000h,OPUS,AMR-WB,PCMU,PCMA PCMU Opus 16k
AMR-WB 16k to Opus 16k same-rate transcode, B-leg Opus RTP ts delta 320 (bug) 960 (RFC 7587)
PCMU 8k to Opus 16k transcode, ts delta 960 960 (unchanged)
inbound Opus 16k recorded with record_session intact 16 kHz WAV intact 16 kHz WAV, 6.5 s

The core library still builds with -Werror.

Type of Change

  • Bug fix
  • New feature
  • Breaking change
  • Documentation update
  • Code cleanup / refactor

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):

The 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_rtp and switch_rtp_pcap, all passing. I haven't run the full unit suite locally.

  • Added/updated unit tests
  • Tested manually
  • Tested with live SignalWire credentials (if applicable)

Checklist

  • I have read the CONTRIBUTING guidelines
  • My code follows the project's style guidelines
  • I have added tests for my changes (if applicable)
  • I have updated documentation (if applicable)
  • All existing tests pass

@celliso1

celliso1 commented Jul 3, 2026 •

Copy link
Copy Markdown
Contributor Author

@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
correctly would be appreciated.

@ticpu

ticpu commented Jul 17, 2026

Copy link
Copy Markdown

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.

@shaunjstokes

Copy link
Copy Markdown
Contributor

This is now applied on our development box. I'll provide further feedback once I've had a chance to test thoroughly.

@celliso1
celliso1 force-pushed the opus-transcoding-rfc7587 branch 2 times, most recently from ae7c494 to 71fb26e Compare September 14, 2026 15:14
@shaunjstokes

Copy link
Copy Markdown
Contributor

We've been unable to reproduce any issues in our dev environment after deploying this patch.

@celliso1
celliso1 force-pushed the opus-transcoding-rfc7587 branch 2 times, most recently from d0ab944 to cceb572 Compare September 20, 2026 15:16
@celliso1
celliso1 force-pushed the opus-transcoding-rfc7587 branch from cceb572 to e25b812 Compare September 30, 2026 16:48
@celliso1
celliso1 force-pushed the opus-transcoding-rfc7587 branch from 8db21a3 to ee28a63 Compare October 12, 2026 22:46
@celliso1

Copy link
Copy Markdown
Contributor Author

@andywolk @morbit85 is this PR a candidate for approval?

@JROtto5

JROtto5 commented Nov 6, 2026

Copy link
Copy Markdown

I tested this against its base (12ac48a) by sending SDP offers with SIPp to the external profile (Debian bookworm, built from source, mod_sofia and mod_opus), with stock codec prefs and with prefs like yours.

offer base, stock prefs PR, stock prefs base, OPUS@16000h,OPUS@8000h,OPUS,G722,PCMU,PCMA PR, same prefs
opus/48000/2 + maxplaybackrate=16000;sprop-maxcapturerate=16000, alone 200, opus 488 200, opus 200, opus
same + PCMU 200, PCMU 200, PCMU 200, PCMU 200, opus
opus/48000/2, no fmtp 200, opus 200, opus 200, opus 200, opus

With OPUS@16000h in the prefs, this fixes #2226: the PCMU fallback becomes Opus. With the stock prefs (OPUS,G722,PCMU,PCMA,...), though, a 16 kHz-constrained Opus offer on its own now gets a 488 where the base answers Opus. That reproduced on two runs.

I think it comes from setting remote_codec_rate = fmtp_remote_codec_rate before the loop. The only Opus implementation in the stock prefs is the 48 kHz one, so 16000 matches neither its samples_per_second nor its actual_samples_per_second. Before, the rtpmap rate (48000) still matched it. Keeping the rtpmap rate as a fallback, so an exact actual_samples_per_second match wins but the 48 kHz implementation still matches when no 16k one is configured, should keep the stock behaviour. I didn't test the RFC 7587 timestamp part.

@celliso1

celliso1 commented Nov 6, 2026

Copy link
Copy Markdown
Contributor Author

With OPUS@16000h in the prefs, this fixes #2226: the PCMU fallback becomes Opus. With the stock prefs (OPUS,G722,PCMU,PCMA,...), though, a 16 kHz-constrained Opus offer on its own now gets a 488 where the base answers Opus. That reproduced on two runs.

Oops. Looking into this now.

@JROtto5

JROtto5 commented Nov 7, 2026

Copy link
Copy Markdown

Re-tested b0d94b8 with the same offers, against the base (12ac48a), now with three prefs:

offer stock prefs (base → b0d94b8) OPUS@16000h,OPUS@8000h,OPUS,… OPUS@8000h,PCMU
opus/48000/2 + maxplaybackrate=16000;sprop-maxcapturerate=16000, alone 200 opus → 200 opus 200 opus → 200 opus 200 opus → 200 opus
same + PCMU 200 PCMU → 200 opus 200 PCMU → 200 opus 200 PCMU → 200 opus
opus/48000/2, no fmtp 200 opus → 200 opus 200 opus → 200 opus 200 opus → 200 opus

The stock-prefs 488 is gone, and the PCMU fallback now becomes Opus with the stock prefs too, not only with OPUS@16000h configured. As before, I didn't test the RFC 7587 timestamp part.

celliso1 pushed a commit to celliso1/freeswitch that referenced this pull request Nov 7, 2026
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>
@celliso1

celliso1 commented Nov 7, 2026

Copy link
Copy Markdown
Contributor Author

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.

celliso1 pushed a commit to celliso1/freeswitch that referenced this pull request Nov 7, 2026
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>
@celliso1
celliso1 force-pushed the opus-transcoding-rfc7587 branch from 55363a7 to 95a80d7 Compare November 7, 2026 15:45
@JROtto5

JROtto5 commented Nov 8, 2026

Copy link
Copy Markdown

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.

offer stock prefs (base → 95a80d7) OPUS@16000h,OPUS@8000h,OPUS,… OPUS@8000h,PCMU
opus/48000/2 + maxplaybackrate=16000;sprop-maxcapturerate=16000, alone 200 opus → 200 opus 200 opus → 200 opus 200 opus → 200 opus
same + PCMU 200 PCMU → 200 opus 200 PCMU → 200 opus 200 PCMU → 200 opus
opus/48000/2 + maxplaybackrate=8000;sprop-maxcapturerate=8000 + PCMU 200 PCMU → 200 opus 200 PCMU → 200 opus 200 PCMU → 200 opus
opus/48000/2, no fmtp 200 opus → 200 opus 200 opus → 200 opus 200 opus → 200 opus

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 switch_core_media wants mod_loopback. The RFC 7587 timestamp part is still untested on my side.

@celliso1
celliso1 force-pushed the opus-transcoding-rfc7587 branch from 95a80d7 to fbe836d Compare November 9, 2026 18:10
celliso1 pushed a commit to celliso1/freeswitch that referenced this pull request Nov 9, 2026
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>
shaunjstokes and others added 5 commits November 12, 2026 09:53
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>
@celliso1
celliso1 force-pushed the opus-transcoding-rfc7587 branch from fbe836d to e57f81c Compare November 12, 2026 17:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants