Skip to content

Negotiate NACK for Opus audio - #1683

Merged
lukasIO merged 9 commits into
mainfrom
claude/audio-nack-f0c7b8
Nov 19, 2026
Merged

lukasIO merged 9 commits into
mainfrom
claude/audio-nack-f0c7b8

Conversation

@lukasIO

@lukasIO lukasIO commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

Subscribed audio gets no NACK today, so lost packets are not retransmitted. libwebrtc advertises only transport-cc feedback for Opus. In single PC mode the client sends the offer, and the server answer keeps only feedback that both sides offer. The server also takes audio feedback only from the first audio section of the first offer. The server wants NACK for tracks without RED (livekit/livekit#3889), but no audio NACK is negotiated. Rust turns RED off for E2EE (#706), so Rust E2EE users get neither RED nor NACK. This ports livekit/client-sdk-js#2270, which fixes livekit/client-sdk-js#2277.

Change

  • Single PC offers add a=rtcp-fb:<pt> nack for every Opus payload type in every audio section, whatever its direction. This includes the initial offer sent with the join, which is the one that counts. The first audio section can be the client's own sendonly mic, so recvonly sections alone are not enough. This matches the final commit of fix: negotiate Opus NACK for received audio in single peer connection mode client-sdk-js#2270. Dual PC publisher offers do not change.
  • Existing plain NACK is not added twice. The * wildcard counts as existing NACK. nack pli is a different feedback type and does not count as NACK.
  • The munge runs in munge_offer, so the initial offer gets it too. The SDP sent with the join is the SDP set as the local description.
  • Dual PC answers add Opus NACK to each audio section whose offer section, matched by mid, has it. libwebrtc drops it from the answer otherwise. client-sdk-js and client-sdk-swift already do this.
  • If the munged SDP does not parse, the original SDP is used, as for the existing stereo munge.

Verified

  • cargo test -p livekit --lib: 91 passed. The new tests cover these cases:
    • String tests: every Opus payload type in every audio direction gets NACK, with LF and CRLF. Other codecs and video sections do not. Existing, wildcard or tab-separated NACK is not added twice. The answer mirrors the offer by mid.
    • Tests with real libwebrtc peer connections: the initial offer has NACK in all three directions. The stored offer is the one sent with the join, and NACK stays after the answer is applied. Later offers have NACK in single PC mode and none in dual PC mode. If the remote offer has NACK, the dual PC answer has it, and otherwise not.
  • With the munges disabled, 7 of the 8 NACK tests fail. The eighth checks that an answer stays unchanged. With only the answer munge disabled, the dual PC test fails, so libwebrtc does drop NACK from the answer.
  • New e2e test v1_opus_nack_negotiates_initial_offer against a local livekit-server --dev (1.13.10): the initial offer and the server answer both carry Opus NACK. It fails with the munge disabled. audio_test also passes.
  • cargo fmt --all --check passes. cargo clippy -p livekit --no-deps has no new findings in the changed code.

The tests with real peer connections and the e2e test are adapted from #1695.

Not done yet

  • A test under packet loss that compares nack_count in the inbound audio stats. Loss injection on macOS needs root, so it did not run. The JS SDK test under 10% loss went from about 300 lost packets per 30 s to 0.

🤖 Generated with Claude Code

libwebrtc advertises only transport-cc feedback for Opus, so no audio
NACK is negotiated. Without RED (for example with E2EE), lost
subscribed audio packets are never retransmitted.

Single PC offers add `a=rtcp-fb:<opus pt> nack` to recvonly audio
sections, including the initial offer sent with the join. Dual PC
answers mirror Opus NACK from the remote offer by mid. This ports
livekit/client-sdk-js#2270.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@lukasIO
lukasIO requested a review from ladvoc as a code owner November 16, 2026 17:28
@lukasIO lukasIO self-assigned this Nov 16, 2026
@github-actions

github-actions Bot commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

Changeset ✓

This PR includes a changeset covering all affected packages:

Package Bump
livekit patch
livekit-capture patch
livekit-ffi patch

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no bugs or issues to report.

Devin Review

lukasIO and others added 2 commits November 18, 2026 17:40
The server takes audio feedback from the first audio section of the
first offer only. That section can be the client's own sendonly mic, so
single PC offers now add Opus NACK to every audio section, whatever its
direction. This matches the final behavior of
livekit/client-sdk-js#2270.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
lukasIO and others added 6 commits November 19, 2026 14:38
- Add NACK to every Opus payload type, not only the first. Treat
  `a=rtcp-fb:* nack` and whitespace other than one space as existing
  NACK, and keep treating `nack pli` as a different feedback type.
- Drop the extra NACK munge in `create_initial_offer`, since
  `munge_offer` from main already applies it.
- Add tests that run the munges on SDP that libwebrtc writes: the
  initial offer, later offers in both modes, and the dual PC answer.
- Add `v1_opus_nack_negotiates_initial_offer`, an e2e test that checks
  NACK in the initial offer and in the server answer.

The real-SDP tests and the e2e test are adapted from
#1695.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Replace the five loose helpers that passed `&[&str]` slices around with
`SdpSection`, a borrowed view of one SDP section. Its methods give the
mid, the Opus payload types and whether a payload type has plain NACK.

The munge now reads lines with `split_inclusive`, so every line keeps
its own line ending, and it writes into one `String` instead of a
`Vec<String>` that it joins again.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The extra 64 bytes were an unexplained guess. The buffer grows at most
once when NACK lines are added, once per negotiation.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- `munge_opus_nack*` return `None` when there is nothing to add, so the
  SDP is no longer copied and compared just to find no change.
- `munge_offer` keeps the munged string instead of serializing the
  parsed offer again. `parse_munged` replaces `reparse_munged`.
- `SdpSection<'s, 'a>` borrows the line slice and the SDP text with
  separate lifetimes, so the strings it returns outlive the slice.
- Add one-line docs to `is_audio`, `mid` and `opus_pts`.
- Test the NACK line after a last line that has no line ending.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@lukasIO
lukasIO merged commit 840215d into main Nov 19, 2026
36 checks passed
@lukasIO
lukasIO deleted the claude/audio-nack-f0c7b8 branch November 19, 2026 17:46
lukasIO pushed a commit that referenced this pull request Nov 24, 2026
…1735)

<!-- verwalter release -->
Merging this PR releases:

## livekit `0.12.0` → `0.12.1`

### Patch Changes

- Negotiate NACK for Opus. If RED is off, for example with E2EE, lost
audio packets are now retransmitted. -
[#1683](#1683)
([@lukasIO](https://github.com/lukasIO))

Changesets: `opus_nack.md`

## livekit-capture `0.3.0` → `0.3.1`

### Patch Changes

- Negotiate NACK for Opus. If RED is off, for example with E2EE, lost
audio packets are now retransmitted. -
[#1683](#1683)
([@lukasIO](https://github.com/lukasIO))
- Updated dependencies: livekit@0.12.1

Changesets: `opus_nack.md`

## livekit-ffi `0.15.0` → `0.16.0`

### Minor Changes

- Expose `LocalDataTrack`, `RemoteDataTrack` and `DataTrackStream` over
UniFFI in room-apis builds too, as the same objects core-modules
exports. - [#1620](#1620)
([@1egoman](https://github.com/1egoman))

### Patch Changes

- Negotiate NACK for Opus. If RED is off, for example with E2EE, lost
audio packets are now retransmitted. -
[#1683](#1683)
([@lukasIO](https://github.com/lukasIO))
- Updated dependencies: livekit@0.12.1, livekit-capture@0.3.1

Changesets: `data_tracks_shared_uniffi_objects.md`, `opus_nack.md`

Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
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.

Single peer connection: audio never negotiates Opus NACK

2 participants