-
Notifications
You must be signed in to change notification settings - Fork 224
Fix AMR answer fmtp mismatch for octet-align offers (#747) #768
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
lixuanqun
wants to merge
2
commits into
livekit:main
Choose a base branch
from
lixuanqun:cursor/fix-amr-fmtp-answer-33f3
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.
+463
−1
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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,276 @@ | ||
| package sip | ||
|
|
||
| import ( | ||
| "strings" | ||
|
|
||
| "github.com/pion/sdp/v3" | ||
| ) | ||
|
|
||
| // AMR/AMR-WB SDP fmtp handling for answers (livekit/sip#747). | ||
| // | ||
| // media-sdk registers AMR/AMR-WB as bandwidth-efficient only (octet-align=0), | ||
| // and AnswerMedia emits rtpmap without echoing offer fmtp. Accepting an | ||
| // octet-aligned offer (or other unsupported modes) therefore produces a | ||
| // mismatched answer and garbled audio. RFC 4867 §8.3.1 requires the answerer | ||
| // to echo certain parameters for the accepted payload type (or reject it). | ||
| // | ||
| // Until media-sdk can negotiate octet-aligned RTP we: | ||
| // 1. strip unsupported AMR formats from the offer before answering, and | ||
| // 2. echo the required fmtp attributes for any AMR format we accept. | ||
|
|
||
| var amrAnswerUnsupported = map[string]func(string) bool{ | ||
| // Default is 0 (bandwidth-efficient). octet-align=1 needs a different | ||
| // RTP payload format that media-sdk does not implement yet. | ||
| "octet-align": func(v string) bool { return v == "1" }, | ||
| // CRC-protected frames are not implemented. | ||
| "crc": func(v string) bool { return v == "1" }, | ||
| // Robust sorting is not implemented. | ||
| "robust-sorting": func(v string) bool { return v == "1" }, | ||
| // Interleaving requires a different de-interleave path. | ||
| "interleaving": func(v string) bool { return v != "" && v != "0" }, | ||
| } | ||
|
|
||
| func isAMRName(name string) bool { | ||
| n := strings.ToUpper(name) | ||
| return n == "AMR" || n == "AMR-WB" | ||
| } | ||
|
|
||
| func parseFmtpParams(fmtp string) map[string]string { | ||
| out := make(map[string]string) | ||
| for _, part := range strings.Split(fmtp, ";") { | ||
| part = strings.TrimSpace(part) | ||
| if part == "" { | ||
| continue | ||
| } | ||
| k, v, ok := strings.Cut(part, "=") | ||
| if !ok { | ||
| out[strings.ToLower(strings.TrimSpace(part))] = "" | ||
| continue | ||
| } | ||
| out[strings.ToLower(strings.TrimSpace(k))] = strings.TrimSpace(v) | ||
| } | ||
| return out | ||
| } | ||
|
|
||
| func amrFmtpUnsupported(fmtp string) bool { | ||
| if fmtp == "" { | ||
| // No fmtp → octet-align defaults to 0 (bandwidth-efficient). | ||
| return false | ||
| } | ||
| params := parseFmtpParams(fmtp) | ||
| for key, bad := range amrAnswerUnsupported { | ||
| if v, ok := params[key]; ok && bad(v) { | ||
| return true | ||
| } | ||
| } | ||
| return false | ||
| } | ||
|
|
||
| func amrFmtpForAnswer(offerFmtp string) string { | ||
| if offerFmtp == "" || amrFmtpUnsupported(offerFmtp) { | ||
| return "" | ||
| } | ||
| params := parseFmtpParams(offerFmtp) | ||
| var parts []string | ||
| // RFC 4867 §8.3.1: answerer MUST include these if present in the offer | ||
| // for the accepted payload type (omit defaults). | ||
| for _, key := range []string{ | ||
| "octet-align", | ||
| "mode-change-capability", | ||
| "max-red", | ||
| } { | ||
| v, ok := params[key] | ||
| if !ok { | ||
| continue | ||
| } | ||
| // Skip octet-align=0 (default). | ||
| if key == "octet-align" && (v == "" || v == "0") { | ||
| continue | ||
| } | ||
| parts = append(parts, key+"="+v) | ||
| } | ||
| // mode-set / mode-change-period / mode-change-neighbor may be answered | ||
| // with a subset; echo offer values when present so both sides agree. | ||
| for _, key := range []string{ | ||
| "mode-set", | ||
| "mode-change-period", | ||
| "mode-change-neighbor", | ||
| } { | ||
| if v, ok := params[key]; ok { | ||
| parts = append(parts, key+"="+v) | ||
| } | ||
| } | ||
| return strings.Join(parts, ";") | ||
| } | ||
|
|
||
| func attrPayloadType(value string) string { | ||
| pt, _, ok := strings.Cut(value, " ") | ||
| if !ok { | ||
| return value | ||
| } | ||
| return pt | ||
| } | ||
|
|
||
| func rtpmapCodecName(value string) string { | ||
| _, rest, ok := strings.Cut(value, " ") | ||
| if !ok { | ||
| return "" | ||
| } | ||
| name, _, _ := strings.Cut(rest, "/") | ||
| return name | ||
| } | ||
|
|
||
| // filterAMROfferSDP removes AMR/AMR-WB dynamic formats whose fmtp we cannot | ||
| // answer correctly, returning a rewritten offer suitable for media-sdk Answer. | ||
| // Remaining formats (including bandwidth-efficient AMR) are left intact. | ||
| // On parse/marshal failure it returns the original offerData with an error; | ||
| // callers should treat the error as non-fatal and continue with the original. | ||
| func filterAMROfferSDP(offerData []byte) ([]byte, error) { | ||
| var offer sdp.SessionDescription | ||
| if err := offer.Unmarshal(offerData); err != nil { | ||
| return offerData, err | ||
| } | ||
| changed := filterOfferAMRFormats(&offer) | ||
| if !changed { | ||
| return offerData, nil | ||
| } | ||
| out, err := offer.Marshal() | ||
| if err != nil { | ||
| return offerData, err | ||
| } | ||
| return out, nil | ||
| } | ||
|
|
||
| // filterOfferAMRFormats removes unsupported AMR formats in-place. | ||
| // Returns true if the description was modified. | ||
| func filterOfferAMRFormats(offer *sdp.SessionDescription) bool { | ||
| if offer == nil { | ||
| return false | ||
| } | ||
| changed := false | ||
| for _, md := range offer.MediaDescriptions { | ||
| if md.MediaName.Media != "audio" { | ||
| continue | ||
| } | ||
| dropPT := make(map[string]struct{}) | ||
| rtpmapNameByPT := make(map[string]string) | ||
| fmtpByPT := make(map[string]string) | ||
| for _, a := range md.Attributes { | ||
| switch a.Key { | ||
| case "rtpmap": | ||
| pt := attrPayloadType(a.Value) | ||
| rtpmapNameByPT[pt] = rtpmapCodecName(a.Value) | ||
| case "fmtp": | ||
| pt, rest, ok := strings.Cut(a.Value, " ") | ||
| if ok { | ||
| fmtpByPT[pt] = rest | ||
| } | ||
| } | ||
| } | ||
| for pt, name := range rtpmapNameByPT { | ||
| if !isAMRName(name) { | ||
| continue | ||
| } | ||
| if amrFmtpUnsupported(fmtpByPT[pt]) { | ||
| dropPT[pt] = struct{}{} | ||
| } | ||
| } | ||
| if len(dropPT) == 0 { | ||
| continue | ||
| } | ||
| changed = true | ||
| filtered := make([]string, 0, len(md.MediaName.Formats)) | ||
| for _, f := range md.MediaName.Formats { | ||
| if _, drop := dropPT[f]; !drop { | ||
| filtered = append(filtered, f) | ||
| } | ||
| } | ||
| md.MediaName.Formats = filtered | ||
| attrs := make([]sdp.Attribute, 0, len(md.Attributes)) | ||
| for _, a := range md.Attributes { | ||
| switch a.Key { | ||
| case "rtpmap", "fmtp": | ||
| if _, drop := dropPT[attrPayloadType(a.Value)]; drop { | ||
| continue | ||
| } | ||
| } | ||
| attrs = append(attrs, a) | ||
| } | ||
| md.Attributes = attrs | ||
| } | ||
| return changed | ||
| } | ||
|
|
||
| // appendAMRFmtpToAnswer mutates answer SDP: for each accepted AMR payload type, | ||
| // copy the offer's answerable fmtp onto the answer (RFC 4867 §8.3.1). | ||
| func appendAMRFmtpToAnswer(answer *sdp.SessionDescription, offerData []byte) { | ||
| if answer == nil { | ||
| return | ||
| } | ||
| var offer sdp.SessionDescription | ||
| if err := offer.Unmarshal(offerData); err != nil { | ||
| return | ||
| } | ||
|
|
||
| offerFmtpByPT := map[string]string{} | ||
| for _, md := range offer.MediaDescriptions { | ||
| if md.MediaName.Media != "audio" { | ||
| continue | ||
| } | ||
| rtpNames := map[string]string{} | ||
| for _, a := range md.Attributes { | ||
| if a.Key == "rtpmap" { | ||
| rtpNames[attrPayloadType(a.Value)] = rtpmapCodecName(a.Value) | ||
| } | ||
| } | ||
| for _, a := range md.Attributes { | ||
| if a.Key != "fmtp" { | ||
| continue | ||
| } | ||
| pt, rest, ok := strings.Cut(a.Value, " ") | ||
| if !ok || !isAMRName(rtpNames[pt]) { | ||
| continue | ||
| } | ||
| offerFmtpByPT[pt] = rest | ||
| } | ||
| } | ||
| if len(offerFmtpByPT) == 0 { | ||
| return | ||
| } | ||
|
|
||
| for _, md := range answer.MediaDescriptions { | ||
| if md.MediaName.Media != "audio" { | ||
| continue | ||
| } | ||
| haveFmtp := map[string]struct{}{} | ||
| rtpNames := map[string]string{} | ||
| for _, a := range md.Attributes { | ||
| switch a.Key { | ||
| case "fmtp": | ||
| haveFmtp[attrPayloadType(a.Value)] = struct{}{} | ||
| case "rtpmap": | ||
| rtpNames[attrPayloadType(a.Value)] = rtpmapCodecName(a.Value) | ||
| } | ||
| } | ||
| for pt, name := range rtpNames { | ||
| if !isAMRName(name) { | ||
| continue | ||
| } | ||
| if _, ok := haveFmtp[pt]; ok { | ||
| continue | ||
| } | ||
| offerFmtp, ok := offerFmtpByPT[pt] | ||
| if !ok { | ||
| continue | ||
| } | ||
| echo := amrFmtpForAnswer(offerFmtp) | ||
| if echo == "" { | ||
| continue | ||
| } | ||
| md.Attributes = append(md.Attributes, sdp.Attribute{ | ||
| Key: "fmtp", | ||
| Value: pt + " " + echo, | ||
| }) | ||
| } | ||
| } | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.