Skip to content

Commit 43afb28

Browse files
authored
Ensure the same codec is used if peer reorders the SDP. (#336)
1 parent 1f4dd02 commit 43afb28

File tree

2 files changed

+47
-12
lines changed

2 files changed

+47
-12
lines changed

pkg/media/sdp/offer.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ func NewOffer(publicIp netip.Addr, rtpListenerPort int, encrypted Encryption) (*
252252
}
253253

254254
func (d *Offer) Answer(publicIp netip.Addr, rtpListenerPort int, enc Encryption) (*Answer, *MediaConfig, error) {
255-
audio, err := SelectAudio(d.MediaDesc)
255+
audio, err := SelectAudio(d.MediaDesc, false)
256256
if err != nil {
257257
return nil, nil, err
258258
}
@@ -321,7 +321,7 @@ func (d *Offer) Answer(publicIp netip.Addr, rtpListenerPort int, enc Encryption)
321321
}
322322

323323
func (d *Answer) Apply(offer *Offer, enc Encryption) (*MediaConfig, error) {
324-
audio, err := SelectAudio(d.MediaDesc)
324+
audio, err := SelectAudio(d.MediaDesc, true)
325325
if err != nil {
326326
return nil, err
327327
}
@@ -473,7 +473,7 @@ type AudioConfig struct {
473473
DTMFType byte
474474
}
475475

476-
func SelectAudio(desc MediaDesc) (*AudioConfig, error) {
476+
func SelectAudio(desc MediaDesc, answer bool) (*AudioConfig, error) {
477477
var (
478478
priority int
479479
audioCodec rtp.AudioCodec
@@ -489,6 +489,9 @@ func SelectAudio(desc MediaDesc) (*AudioConfig, error) {
489489
audioCodec = codec
490490
priority = codec.Info().Priority
491491
}
492+
if answer {
493+
break
494+
}
492495
}
493496
if audioCodec == nil {
494497
return nil, ErrNoCommonMedia

pkg/media/sdp/offer_test.go

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -127,11 +127,11 @@ func TestSDPMediaAnswer(t *testing.T) {
127127
name: "default",
128128
offer: sdp.MediaDescription{
129129
MediaName: sdp.MediaName{
130-
Formats: []string{"0", "9", "8", "101"},
130+
Formats: []string{"9", "0", "8", "101"},
131131
},
132132
Attributes: []sdp.Attribute{
133-
{Key: "rtpmap", Value: "0 PCMU/8000"},
134133
{Key: "rtpmap", Value: "9 G722/8000"},
134+
{Key: "rtpmap", Value: "0 PCMU/8000"},
135135
{Key: "rtpmap", Value: "101 telephone-event/8000"},
136136
},
137137
},
@@ -145,11 +145,11 @@ func TestSDPMediaAnswer(t *testing.T) {
145145
name: "lowercase",
146146
offer: sdp.MediaDescription{
147147
MediaName: sdp.MediaName{
148-
Formats: []string{"0", "9", "101"},
148+
Formats: []string{"9", "0", "101"},
149149
},
150150
Attributes: []sdp.Attribute{
151-
{Key: "rtpmap", Value: "0 pcmu/8000"},
152151
{Key: "rtpmap", Value: "9 g722/8000"},
152+
{Key: "rtpmap", Value: "0 pcmu/8000"},
153153
{Key: "rtpmap", Value: "101 telephone-event/8000"},
154154
},
155155
},
@@ -163,11 +163,11 @@ func TestSDPMediaAnswer(t *testing.T) {
163163
name: "no dtmf",
164164
offer: sdp.MediaDescription{
165165
MediaName: sdp.MediaName{
166-
Formats: []string{"0", "9"},
166+
Formats: []string{"9", "0"},
167167
},
168168
Attributes: []sdp.Attribute{
169-
{Key: "rtpmap", Value: "0 PCMU/8000"},
170169
{Key: "rtpmap", Value: "9 G722/8000"},
170+
{Key: "rtpmap", Value: "0 PCMU/8000"},
171171
},
172172
},
173173
exp: &AudioConfig{
@@ -179,11 +179,11 @@ func TestSDPMediaAnswer(t *testing.T) {
179179
name: "custom dtmf",
180180
offer: sdp.MediaDescription{
181181
MediaName: sdp.MediaName{
182-
Formats: []string{"0", "9", "103"},
182+
Formats: []string{"9", "0", "103"},
183183
},
184184
Attributes: []sdp.Attribute{
185-
{Key: "rtpmap", Value: "0 PCMU/8000"},
186185
{Key: "rtpmap", Value: "9 G722/8000"},
186+
{Key: "rtpmap", Value: "0 PCMU/8000"},
187187
{Key: "rtpmap", Value: "103 telephone-event/8000"},
188188
},
189189
},
@@ -270,13 +270,45 @@ func TestSDPMediaAnswer(t *testing.T) {
270270
DTMFType: 101,
271271
},
272272
},
273+
{
274+
name: "changed order",
275+
offer: sdp.MediaDescription{
276+
MediaName: sdp.MediaName{
277+
Formats: []string{"0", "9"},
278+
},
279+
Attributes: []sdp.Attribute{
280+
{Key: "rtpmap", Value: "0 PCMU/8000"},
281+
{Key: "rtpmap", Value: "9 G722/8000"},
282+
},
283+
},
284+
exp: &AudioConfig{
285+
Codec: getCodec(g711.ULawSDPName),
286+
Type: 0,
287+
},
288+
},
289+
{
290+
name: "changed order g711",
291+
offer: sdp.MediaDescription{
292+
MediaName: sdp.MediaName{
293+
Formats: []string{"8", "0"},
294+
},
295+
Attributes: []sdp.Attribute{
296+
{Key: "rtpmap", Value: "8 PCMA/8000"},
297+
{Key: "rtpmap", Value: "0 PCMU/8000"},
298+
},
299+
},
300+
exp: &AudioConfig{
301+
Codec: getCodec(g711.ALawSDPName),
302+
Type: 8,
303+
},
304+
},
273305
}
274306
for _, c := range cases {
275307
c := c
276308
t.Run(c.name, func(t *testing.T) {
277309
m, err := ParseMedia(&c.offer)
278310
require.NoError(t, err)
279-
got, err := SelectAudio(*m)
311+
got, err := SelectAudio(*m, true)
280312
if c.exp == nil {
281313
require.Error(t, err)
282314
return

0 commit comments

Comments
 (0)