Skip to content

Commit 40b3807

Browse files
committed
Remove RTX codec if no primary
While adding transceivers from SetRemoteDescription, the filtered codecs could filter out the primart codec and leave the RTX codec in. Generating an answer with that fails `SetRemoteDescription` on remote peer due to an unrecognisable codec. Fix it by filtering out RTX is primary is not there.
1 parent 8efd17e commit 40b3807

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

peerconnection.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1170,6 +1170,20 @@ func (pc *PeerConnection) SetRemoteDescription(desc SessionDescription) error {
11701170
filteredCodecs = append(filteredCodecs, codec)
11711171
}
11721172
}
1173+
1174+
// remove RTX codecs if there is no corresponding primary codec
1175+
for i := len(filteredCodecs) - 1; i >= 0; i-- {
1176+
c := filteredCodecs[i]
1177+
if !strings.EqualFold(c.MimeType, MimeTypeRTX) {
1178+
continue
1179+
}
1180+
1181+
primaryPayload := findPrimaryPayloadTypeForRTX(c, filteredCodecs)
1182+
if primaryPayload == PayloadType(0) {
1183+
// no primary for RTX, remove the RTX
1184+
filteredCodecs = append(filteredCodecs[:i], filteredCodecs[i+1:]...)
1185+
}
1186+
}
11731187
_ = transceiver.SetCodecPreferences(filteredCodecs)
11741188
}
11751189

rtpcodec.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package webrtc
55

66
import (
77
"fmt"
8+
"strconv"
89
"strings"
910

1011
"github.com/pion/webrtc/v4/internal/fmtp"
@@ -155,6 +156,27 @@ func findRTXPayloadType(needle PayloadType, haystack []RTPCodecParameters) Paylo
155156
return PayloadType(0)
156157
}
157158

159+
// Given RTX CodecParameters find the primary CodecParameters if one exists.
160+
func findPrimaryPayloadTypeForRTX(needle RTPCodecParameters, haystack []RTPCodecParameters) PayloadType {
161+
parts := strings.Split(needle.SDPFmtpLine, "=")
162+
if len(parts) != 2 || parts[0] != "apt" {
163+
return PayloadType(0)
164+
}
165+
166+
primaryPayloadType, err := strconv.Atoi(parts[1])
167+
if err != nil || primaryPayloadType < 0 || primaryPayloadType > 255 {
168+
return PayloadType(0)
169+
}
170+
171+
for _, c := range haystack {
172+
if c.PayloadType == PayloadType(primaryPayloadType) {
173+
return c.PayloadType
174+
}
175+
}
176+
177+
return PayloadType(0)
178+
}
179+
158180
// For now, only FlexFEC is supported.
159181
func findFECPayloadType(haystack []RTPCodecParameters) PayloadType {
160182
for _, c := range haystack {

0 commit comments

Comments
 (0)