Skip to content

Commit e8c160b

Browse files
committedOct 23, 2023
Fix GetCodecForPayloadType() with Alexa WebRTC
WebRTC offers from Alexa do not include the rtpmap attribute for payload types 0 and 8, and this makes GetCodecForPayloadType() return a fatal error. The current SDP specification (RFC8866) explicitly tells that rtpmap is not mandatory in case of these static payloads: "consider u-law PCM encoded single-channel audio sampled at 8 kHz. This is completely defined in the RTP audio/video profile as payload type 0, so there is no need for an "a=rtpmap:" attribute" This patch fixes the issue.
1 parent 18a7702 commit e8c160b

File tree

2 files changed

+80
-18
lines changed

2 files changed

+80
-18
lines changed
 

‎util.go

+13-1
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,19 @@ func mergeCodecs(codec Codec, codecs map[uint8]Codec) {
203203
}
204204

205205
func (s *SessionDescription) buildCodecMap() map[uint8]Codec {
206-
codecs := make(map[uint8]Codec)
206+
codecs := map[uint8]Codec{
207+
// static codecs that do not require a rtpmap
208+
0: {
209+
PayloadType: 0,
210+
Name: "PCMU",
211+
ClockRate: 8000,
212+
},
213+
8: {
214+
PayloadType: 8,
215+
Name: "PCMA",
216+
ClockRate: 8000,
217+
},
218+
}
207219

208220
for _, m := range s.MediaDescriptions {
209221
for _, a := range m.Attributes {

‎util_test.go

+67-17
Original file line numberDiff line numberDiff line change
@@ -93,57 +93,107 @@ func TestGetPayloadTypeForVP8(t *testing.T) {
9393

9494
func TestGetCodecForPayloadType(t *testing.T) {
9595
for _, test := range []struct {
96+
name string
97+
SD SessionDescription
9698
PayloadType uint8
9799
Expected Codec
98100
}{
99101
{
100-
PayloadType: 120,
101-
Expected: Codec{
102+
"vp8",
103+
getTestSessionDescription(),
104+
120,
105+
Codec{
102106
PayloadType: 120,
103107
Name: "VP8",
104108
ClockRate: 90000,
105109
Fmtp: "max-fs=12288;max-fr=60",
106110
},
107111
},
108112
{
109-
PayloadType: 121,
110-
Expected: Codec{
113+
"vp9",
114+
getTestSessionDescription(),
115+
121,
116+
Codec{
111117
PayloadType: 121,
112118
Name: "VP9",
113119
ClockRate: 90000,
114120
Fmtp: "max-fs=12288;max-fr=60",
115121
},
116122
},
117123
{
118-
PayloadType: 126,
119-
Expected: Codec{
124+
"h264 126",
125+
getTestSessionDescription(),
126+
126,
127+
Codec{
120128
PayloadType: 126,
121129
Name: "H264",
122130
ClockRate: 90000,
123131
Fmtp: "profile-level-id=42e01f;level-asymmetry-allowed=1;packetization-mode=1",
124132
},
125133
},
126134
{
127-
PayloadType: 97,
128-
Expected: Codec{
135+
"h264 97",
136+
getTestSessionDescription(),
137+
97,
138+
Codec{
129139
PayloadType: 97,
130140
Name: "H264",
131141
ClockRate: 90000,
132142
Fmtp: "profile-level-id=42e01f;level-asymmetry-allowed=1",
133143
RTCPFeedback: []string{"ccm fir", "nack", "nack pli"},
134144
},
135145
},
146+
{
147+
"pcmu without rtpmap",
148+
SessionDescription{
149+
MediaDescriptions: []*MediaDescription{
150+
{
151+
MediaName: MediaName{
152+
Media: "audio",
153+
Protos: []string{"RTP", "AVP"},
154+
Formats: []string{"0", "8"},
155+
},
156+
},
157+
},
158+
},
159+
0,
160+
Codec{
161+
PayloadType: 0,
162+
Name: "PCMU",
163+
ClockRate: 8000,
164+
},
165+
},
166+
{
167+
"pcma without rtpmap",
168+
SessionDescription{
169+
MediaDescriptions: []*MediaDescription{
170+
{
171+
MediaName: MediaName{
172+
Media: "audio",
173+
Protos: []string{"RTP", "AVP"},
174+
Formats: []string{"0", "8"},
175+
},
176+
},
177+
},
178+
},
179+
8,
180+
Codec{
181+
PayloadType: 8,
182+
Name: "PCMA",
183+
ClockRate: 8000,
184+
},
185+
},
136186
} {
137-
sd := getTestSessionDescription()
187+
t.Run(test.name, func(t *testing.T) {
188+
actual, err := test.SD.GetCodecForPayloadType(test.PayloadType)
189+
if got, want := err, error(nil); !errors.Is(got, want) {
190+
t.Fatalf("GetCodecForPayloadType(): err=%v, want=%v", got, want)
191+
}
138192

139-
actual, err := sd.GetCodecForPayloadType(test.PayloadType)
140-
if got, want := err, error(nil); !errors.Is(got, want) {
141-
t.Fatalf("GetCodecForPayloadType(): err=%v, want=%v", got, want)
142-
}
143-
144-
if !reflect.DeepEqual(actual, test.Expected) {
145-
t.Errorf("error:\n\nEXPECTED:\n%v\nACTUAL:\n%v", test.Expected, actual)
146-
}
193+
if !reflect.DeepEqual(actual, test.Expected) {
194+
t.Errorf("error:\n\nEXPECTED:\n%v\nACTUAL:\n%v", test.Expected, actual)
195+
}
196+
})
147197
}
148198
}
149199

0 commit comments

Comments
 (0)
Please sign in to comment.