-
Notifications
You must be signed in to change notification settings - Fork 233
fix(sip): answer re-INVITE with the RFC 3264 direction, not the cached SDP #780
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
prasanth-33460
wants to merge
1
commit into
livekit:main
Choose a base branch
from
prasanth-33460:fix/rfc3264-hold-direction
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,143 @@ | ||
| // Copyright 2026 LiveKit, Inc. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
|
|
||
| package sip | ||
|
|
||
| import ( | ||
| "strings" | ||
| "testing" | ||
| ) | ||
|
|
||
| // A carrier's HOLD re-INVITE, as captured on a live trunk (Plivo/genband SBC). | ||
| const holdOffer = "v=0\r\n" + | ||
| "o=genband 1642851412 1894065156 IN IP4 203.0.113.20\r\n" + | ||
| "s=-\r\n" + | ||
| "c=IN IP4 203.0.113.20\r\n" + | ||
| "t=0 0\r\n" + | ||
| "m=audio 29076 RTP/AVP 0 8 101\r\n" + | ||
| "c=IN IP4 203.0.113.20\r\n" + | ||
| "a=rtpmap:0 PCMU/8000\r\n" + | ||
| "a=sendonly\r\n" + | ||
| "a=ptime:20\r\n" | ||
|
|
||
| // Our cached, already-negotiated local SDP — note a=sendrecv. | ||
| const cachedLocal = "v=0\r\n" + | ||
| "o=- 9157931411869268290 9157931411869268294 IN IP4 203.0.113.10\r\n" + | ||
| "s=LiveKit\r\n" + | ||
| "c=IN IP4 203.0.113.10\r\n" + | ||
| "t=0 0\r\n" + | ||
| "m=audio 17830 RTP/AVP 0 101\r\n" + | ||
| "a=rtpmap:0 PCMU/8000\r\n" + | ||
| "a=rtpmap:101 telephone-event/8000\r\n" + | ||
| "a=fmtp:101 0-16\r\n" + | ||
| "a=ptime:20\r\n" + | ||
| "a=sendrecv\r\n" | ||
|
|
||
| // TestHoldOfferIsAnsweredRecvonly is THE regression test. | ||
| // | ||
| // Before this fix, both re-INVITE paths replayed cachedLocal verbatim, so a sendonly (hold) | ||
| // offer was answered a=sendrecv. RFC 3264 §6.1 requires recvonly, and carriers BYE the dialog | ||
| // ~60ms after the invalid answer — which on a bridged/transferred call ends BOTH legs, so a | ||
| // supervisor pressing HOLD hung up the customer. | ||
| func TestHoldOfferIsAnsweredRecvonly(t *testing.T) { | ||
| dir := answerDirectionFor([]byte(holdOffer)) | ||
| if dir != "recvonly" { | ||
| t.Fatalf("a=sendonly offer must be answered recvonly (RFC 3264 §6.1), got %q", dir) | ||
| } | ||
|
|
||
| answer := string(withSDPDirection([]byte(cachedLocal), dir)) | ||
| if !strings.Contains(answer, "a=recvonly") { | ||
| t.Fatalf("answer must carry a=recvonly:\n%s", answer) | ||
| } | ||
| if strings.Contains(answer, "a=sendrecv") { | ||
| t.Fatalf("answer must NOT still say sendrecv — this is the bug that makes carriers BYE:\n%s", answer) | ||
| } | ||
| // Everything else about the negotiated body must survive untouched. | ||
| for _, keep := range []string{ | ||
| "m=audio 17830 RTP/AVP 0 101", | ||
| "a=rtpmap:0 PCMU/8000", | ||
| "a=rtpmap:101 telephone-event/8000", | ||
| "a=fmtp:101 0-16", | ||
| "a=ptime:20", | ||
| "c=IN IP4 203.0.113.10", | ||
| } { | ||
| if !strings.Contains(answer, keep) { | ||
| t.Errorf("rewrite must not disturb %q:\n%s", keep, answer) | ||
| } | ||
| } | ||
| if !strings.Contains(answer, "\r\n") { | ||
| t.Error("CRLF line endings must be preserved (RFC 4566; strict SBCs care)") | ||
| } | ||
| } | ||
|
|
||
| func TestDirectionMapping(t *testing.T) { | ||
| cases := map[string]string{ | ||
| "a=sendonly": "recvonly", | ||
| "a=recvonly": "sendonly", | ||
| "a=inactive": "inactive", | ||
| "a=sendrecv": "", // no rewrite needed | ||
| } | ||
| for offer, want := range cases { | ||
| body := "v=0\r\nm=audio 1 RTP/AVP 0\r\n" + offer + "\r\n" | ||
| if got := answerDirectionFor([]byte(body)); got != want { | ||
| t.Errorf("offer %s -> got %q, want %q", offer, got, want) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // A re-INVITE with NO direction attribute (codec renegotiation, port change, session-timer | ||
| // refresh) must be answered exactly as before — sendrecv is implied by RFC 4566 §6, and this | ||
| // is the overwhelmingly common case. Regressing it would break every non-hold re-INVITE. | ||
| func TestNoDirectionInOfferLeavesSDPUntouched(t *testing.T) { | ||
| offer := "v=0\r\nm=audio 29076 RTP/AVP 0\r\na=rtpmap:0 PCMU/8000\r\n" | ||
| if dir := answerDirectionFor([]byte(offer)); dir != "" { | ||
| t.Fatalf("absent direction must yield no rewrite, got %q", dir) | ||
| } | ||
| if got := string(withSDPDirection([]byte(cachedLocal), "")); got != cachedLocal { | ||
| t.Fatal("empty direction must return the cached SDP byte-identical") | ||
| } | ||
| } | ||
|
|
||
| // Unhold: the carrier re-offers sendrecv, and we must go back to sending audio. | ||
| func TestUnholdRestoresSendrecv(t *testing.T) { | ||
| held := withSDPDirection([]byte(cachedLocal), "recvonly") | ||
| if !strings.Contains(string(held), "a=recvonly") { | ||
| t.Fatal("setup: expected held SDP to be recvonly") | ||
| } | ||
| // carrier unholds -> offer has a=sendrecv -> answerDirectionFor returns "" (no rewrite), | ||
| // so the caller passes the cached (sendrecv) SDP through unchanged. | ||
| if dir := answerDirectionFor([]byte(strings.Replace(holdOffer, "a=sendonly", "a=sendrecv", 1))); dir != "" { | ||
| t.Fatalf("sendrecv offer needs no rewrite, got %q", dir) | ||
| } | ||
| } | ||
|
|
||
| func TestDirectionLineAppendedWhenAbsentFromLocal(t *testing.T) { | ||
| local := "v=0\r\nm=audio 17830 RTP/AVP 0\r\na=rtpmap:0 PCMU/8000\r\n" | ||
| out := string(withSDPDirection([]byte(local), "recvonly")) | ||
| if !strings.Contains(out, "a=recvonly") { | ||
| t.Fatalf("direction must be appended when local has none:\n%s", out) | ||
| } | ||
| if strings.Count(out, "a=recvonly") != 1 { | ||
| t.Fatalf("exactly one direction line expected:\n%s", out) | ||
| } | ||
| } | ||
|
|
||
| func TestDuplicateDirectionLinesCollapseToOne(t *testing.T) { | ||
| local := "v=0\r\na=sendrecv\r\nm=audio 1 RTP/AVP 0\r\na=sendrecv\r\n" | ||
| out := string(withSDPDirection([]byte(local), "recvonly")) | ||
| if strings.Count(out, "a=recvonly") != 1 { | ||
| t.Fatalf("duplicate direction lines must collapse to one:\n%s", out) | ||
| } | ||
| if strings.Contains(out, "a=sendrecv") { | ||
| t.Fatalf("no stale sendrecv may remain:\n%s", out) | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🟡 Hold answer can pick the wrong media direction when a call offer contains more than one media stream
The direction to answer with is chosen by scanning the whole incoming offer and keeping the last direction found (
answerDirectionForatpkg/sip/inbound.go:378-404) instead of the one belonging to the audio stream, so an offer that carries a second stream with a different direction makes the audio reply say the wrong thing.Impact: On calls where the far end includes an extra (e.g. video or image/T.38) stream in its hold or re-negotiation request, audio can be answered as inactive/wrong direction and the caller hears silence or the far end drops the call.
Why last-line-wins parsing breaks with multiple m= sections
answerDirectionForsplits the entire SDP body into lines and records everya=sendonly|recvonly|inactive|sendrecvit sees, letting the last one win (pkg/sip/inbound.go:381-393). SDP direction attributes are scoped: a session-level attribute is the default, and eachm=section may override it. With an offer such as:the function returns
inactive, andwithSDPDirection(pkg/sip/inbound.go:409-442) then rewrites our single audio section toa=inactive, halting media in both directions rather than answeringrecvonly.withSDPDirectionhas the mirror-image limitation: it replaces the first direction line found anywhere (possibly a session-level one) and silently drops all subsequent direction lines, which would corrupt a multi-section local body.A fix is to track the current section while scanning (session-level default, then the first
m=audiosection's own attribute overriding it) and to apply the rewrite only inside that audio section.Prompt for agents
Was this helpful? React with 👍 or 👎 to provide feedback.