Skip to content

Commit 29d6e41

Browse files
itzmanishnils-ohlmeier
authored andcommitted
Add support for multi codec negotiation
1 parent c79463d commit 29d6e41

File tree

2 files changed

+84
-2
lines changed

2 files changed

+84
-2
lines changed

mediaengine.go

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ type mediaEngineHeaderExtension struct {
3333
type MediaEngine struct {
3434
// If we have attempted to negotiate a codec type yet.
3535
negotiatedVideo, negotiatedAudio bool
36+
negotiateMultiCodecs bool
3637

3738
videoCodecs, audioCodecs []RTPCodecParameters
3839
negotiatedVideoCodecs, negotiatedAudioCodecs []RTPCodecParameters
@@ -43,6 +44,22 @@ type MediaEngine struct {
4344
mu sync.RWMutex
4445
}
4546

47+
// SetMultiCodecNegotiation enables or disables the negotiation of multiple codecs.
48+
func (m *MediaEngine) SetMultiCodecNegotiation(negotiateMultiCodecs bool) {
49+
m.mu.Lock()
50+
defer m.mu.Unlock()
51+
52+
m.negotiateMultiCodecs = negotiateMultiCodecs
53+
}
54+
55+
// MultiCodecNegotiation returns the current state of the negotiation of multiple codecs.
56+
func (m *MediaEngine) MultiCodecNegotiation() bool {
57+
m.mu.RLock()
58+
defer m.mu.RUnlock()
59+
60+
return m.negotiateMultiCodecs
61+
}
62+
4663
// RegisterDefaultCodecs registers the default codecs supported by Pion WebRTC.
4764
// RegisterDefaultCodecs is not safe for concurrent use.
4865
func (m *MediaEngine) RegisterDefaultCodecs() error {
@@ -579,9 +596,9 @@ func (m *MediaEngine) updateFromRemoteDescription(desc sdp.SessionDescription) e
579596
}
580597

581598
switch {
582-
case !m.negotiatedAudio && typ == RTPCodecTypeAudio:
599+
case (!m.negotiatedAudio || m.negotiateMultiCodecs) && typ == RTPCodecTypeAudio:
583600
m.negotiatedAudio = true
584-
case !m.negotiatedVideo && typ == RTPCodecTypeVideo:
601+
case (!m.negotiatedVideo || m.negotiateMultiCodecs) && typ == RTPCodecTypeVideo:
585602
m.negotiatedVideo = true
586603
default:
587604
// update header extesions from remote sdp if codec is negotiated, Firefox

mediaengine_test.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -896,3 +896,68 @@ a=rtcp-fb:96 nack
896896
runTest(t, true)
897897
})
898898
}
899+
900+
func TestMultiCodecNegotiation(t *testing.T) {
901+
const offerSdp = `v=0
902+
o=- 781500112831855234 6 IN IP4 127.0.0.1
903+
s=-
904+
t=0 0
905+
a=group:BUNDLE 0 1 2 3
906+
a=extmap-allow-mixed
907+
a=msid-semantic: WMS be0216be-f3d8-40ca-a624-379edf70f1c9
908+
m=application 53555 UDP/DTLS/SCTP webrtc-datachannel
909+
a=mid:0
910+
a=sctp-port:5000
911+
a=max-message-size:262144
912+
m=video 9 UDP/TLS/RTP/SAVPF 98
913+
a=mid:1
914+
a=sendonly
915+
a=msid:be0216be-f3d8-40ca-a624-379edf70f1c9 3d032b3b-ffe5-48ec-b783-21375668d1c3
916+
a=rtcp-mux
917+
a=rtcp-rsize
918+
a=rtpmap:98 VP9/90000
919+
a=rtcp-fb:98 goog-remb
920+
a=rtcp-fb:98 transport-cc
921+
a=rtcp-fb:98 ccm fir
922+
a=rtcp-fb:98 nack
923+
a=rtcp-fb:98 nack pli
924+
a=fmtp:98 profile-id=0
925+
a=rid:q send
926+
a=rid:h send
927+
a=simulcast:send q;h
928+
m=video 9 UDP/TLS/RTP/SAVPF 96
929+
a=mid:2
930+
a=sendonly
931+
a=msid:6ff05509-be96-4ef1-a74f-425e14720983 16d5d7fe-d076-4718-9ca9-ec62b4543727
932+
a=rtcp-mux
933+
a=rtcp-rsize
934+
a=rtpmap:96 VP8/90000
935+
a=rtcp-fb:96 goog-remb
936+
a=rtcp-fb:96 transport-cc
937+
a=rtcp-fb:96 ccm fir
938+
a=rtcp-fb:96 nack
939+
a=rtcp-fb:96 nack pli
940+
a=ssrc:4281768245 cname:JDM9GNMEg+9To6K7
941+
a=ssrc:4281768245 msid:6ff05509-be96-4ef1-a74f-425e14720983 16d5d7fe-d076-4718-9ca9-ec62b4543727
942+
`
943+
mustParse := func(raw string) sdp.SessionDescription {
944+
s := sdp.SessionDescription{}
945+
assert.NoError(t, s.Unmarshal([]byte(raw)))
946+
947+
return s
948+
}
949+
t.Run("Multi codec negotiation disabled", func(t *testing.T) {
950+
mediaEngine := MediaEngine{}
951+
assert.NoError(t, mediaEngine.RegisterDefaultCodecs())
952+
assert.NoError(t, mediaEngine.updateFromRemoteDescription(mustParse(offerSdp)))
953+
assert.Len(t, mediaEngine.negotiatedVideoCodecs, 1)
954+
})
955+
t.Run("Multi codec negotiation enabled", func(t *testing.T) {
956+
mediaEngine := MediaEngine{}
957+
mediaEngine.SetMultiCodecNegotiation(true)
958+
assert.True(t, mediaEngine.MultiCodecNegotiation())
959+
assert.NoError(t, mediaEngine.RegisterDefaultCodecs())
960+
assert.NoError(t, mediaEngine.updateFromRemoteDescription(mustParse(offerSdp)))
961+
assert.Len(t, mediaEngine.negotiatedVideoCodecs, 2)
962+
})
963+
}

0 commit comments

Comments
 (0)