Repository: pion/webrtc
Pull request #2576 (2023-09-12 19:28:00)

Enable Simulcast RTP Headers by default
Relates to #2557

codecov[bot]: ## [Codecov](https://app.codecov.io/gh/pion/webrtc/pull/2576?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pion) Report
Patch has no changes to coverable lines.



:loudspeaker: Thoughts on this report? [Let us know!](https://about.codecov.io/pull-request-comment-report/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pion).


Patch:
diff --git a/examples/simulcast/main.go b/examples/simulcast/main.go
index f2c90f55..5ae22f1c 100644
--- a/examples/simulcast/main.go
+++ b/examples/simulcast/main.go
@@ -14,7 +14,6 @@ import (
 	"os"
 	"time"
 
-	"github.com/pion/interceptor"
 	"github.com/pion/rtcp"
 	"github.com/pion/webrtc/v4"
 	"github.com/pion/webrtc/v4/examples/internal/signal"
@@ -33,34 +32,8 @@ func main() {
 		},
 	}
 
-	// Enable Extension Headers needed for Simulcast
-	m := &webrtc.MediaEngine{}
-	if err := m.RegisterDefaultCodecs(); err != nil {
-		panic(err)
-	}
-	for _, extension := range []string{
-		"urn:ietf:params:rtp-hdrext:sdes:mid",
-		"urn:ietf:params:rtp-hdrext:sdes:rtp-stream-id",
-		"urn:ietf:params:rtp-hdrext:sdes:repaired-rtp-stream-id",
-	} {
-		if err := m.RegisterHeaderExtension(webrtc.RTPHeaderExtensionCapability{URI: extension}, webrtc.RTPCodecTypeVideo); err != nil {
-			panic(err)
-		}
-	}
-
-	// Create a InterceptorRegistry. This is the user configurable RTP/RTCP Pipeline.
-	// This provides NACKs, RTCP Reports and other features. If you use `webrtc.NewPeerConnection`
-	// this is enabled by default. If you are manually managing You MUST create a InterceptorRegistry
-	// for each PeerConnection.
-	i := &interceptor.Registry{}
-
-	// Use the default set of Interceptors
-	if err := webrtc.RegisterDefaultInterceptors(m, i); err != nil {
-		panic(err)
-	}
-
 	// Create a new RTCPeerConnection
-	peerConnection, err := webrtc.NewAPI(webrtc.WithMediaEngine(m), webrtc.WithInterceptorRegistry(i)).NewPeerConnection(config)
+	peerConnection, err := webrtc.NewPeerConnection(config)
 	if err != nil {
 		panic(err)
 	}
diff --git a/interceptor.go b/interceptor.go
index 2dfe5735..fec45ab7 100644
--- a/interceptor.go
+++ b/interceptor.go
@@ -30,6 +30,10 @@ func RegisterDefaultInterceptors(mediaEngine *MediaEngine, interceptorRegistry *
 		return err
 	}
 
+	if err := ConfigureSimulcastExtensionHeaders(mediaEngine); err != nil {
+		return err
+	}
+
 	return ConfigureTWCCSender(mediaEngine, interceptorRegistry)
 }
 
@@ -123,6 +127,19 @@ func ConfigureCongestionControlFeedback(mediaEngine *MediaEngine, interceptorReg
 	return nil
 }
 
+// ConfigureSimulcastExtensionHeaders enables the RTP Extenison Headers needed for Simulcast
+func ConfigureSimulcastExtensionHeaders(mediaEngine *MediaEngine) error {
+	if err := mediaEngine.RegisterHeaderExtension(RTPHeaderExtensionCapability{URI: sdp.SDESMidURI}, RTPCodecTypeVideo); err != nil {
+		return err
+	}
+
+	if err := mediaEngine.RegisterHeaderExtension(RTPHeaderExtensionCapability{URI: sdp.SDESRTPStreamIDURI}, RTPCodecTypeVideo); err != nil {
+		return err
+	}
+
+	return mediaEngine.RegisterHeaderExtension(RTPHeaderExtensionCapability{URI: sdesRepairRTPStreamIDURI}, RTPCodecTypeVideo)
+}
+
 type interceptorToTrackLocalWriter struct{ interceptor atomic.Value } // interceptor.RTPWriter }
 
 func (i *interceptorToTrackLocalWriter) WriteRTP(header *rtp.Header, payload []byte) (int, error) {
diff --git a/peerconnection.go b/peerconnection.go
index 7b5dec14..867dafcb 100644
--- a/peerconnection.go
+++ b/peerconnection.go
@@ -1671,17 +1671,17 @@ func (pc *PeerConnection) undeclaredRTPMediaProcessor() {
 			continue
 		}
 
+		pc.dtlsTransport.storeSimulcastStream(stream)
+
 		if atomic.AddUint64(&simulcastRoutineCount, 1) >= simulcastMaxProbeRoutines {
 			atomic.AddUint64(&simulcastRoutineCount, ^uint64(0))
 			pc.log.Warn(ErrSimulcastProbeOverflow.Error())
-			pc.dtlsTransport.storeSimulcastStream(stream)
 			continue
 		}
 
 		go func(rtpStream io.Reader, ssrc SSRC) {
 			if err := pc.handleIncomingSSRC(rtpStream, ssrc); err != nil {
 				pc.log.Errorf(incomingUnhandledRTPSsrc, ssrc, err)
-				pc.dtlsTransport.storeSimulcastStream(stream)
 			}
 			atomic.AddUint64(&simulcastRoutineCount, ^uint64(0))
 		}(stream, SSRC(ssrc))


Test patch:
diff --git a/mediaengine_test.go b/mediaengine_test.go
index 9ecf690e..00022da1 100644
--- a/mediaengine_test.go
+++ b/mediaengine_test.go
@@ -195,7 +195,7 @@ a=rtpmap:111 opus/48000/2
 
 		m := MediaEngine{}
 		assert.NoError(t, m.RegisterDefaultCodecs())
-		registerSimulcastHeaderExtensions(&m, RTPCodecTypeAudio)
+		assert.NoError(t, m.RegisterHeaderExtension(RTPHeaderExtensionCapability{URI: sdp.SDESMidURI}, RTPCodecTypeAudio))
 		assert.NoError(t, m.updateFromRemoteDescription(mustParse(headerExtensions)))
 
 		assert.False(t, m.negotiatedVideo)
diff --git a/peerconnection_media_test.go b/peerconnection_media_test.go
index 0fc052e8..2571db5f 100644
--- a/peerconnection_media_test.go
+++ b/peerconnection_media_test.go
@@ -36,18 +36,6 @@ var (
 	errNoTransceiverwithMid      = errors.New("no transceiver with mid")
 )
 
-func registerSimulcastHeaderExtensions(m *MediaEngine, codecType RTPCodecType) {
-	for _, extension := range []string{
-		sdp.SDESMidURI,
-		sdp.SDESRTPStreamIDURI,
-		sdesRepairRTPStreamIDURI,
-	} {
-		if err := m.RegisterHeaderExtension(RTPHeaderExtensionCapability{URI: extension}, codecType); err != nil {
-			panic(err)
-		}
-	}
-}
-
 /*
 Integration test for bi-directional peers
 
@@ -1051,10 +1039,8 @@ func TestPeerConnection_Simulcast_Probe(t *testing.T) {
 		unhandledSimulcastError := make(chan struct{})
 
 		m := &MediaEngine{}
-		if err := m.RegisterDefaultCodecs(); err != nil {
-			panic(err)
-		}
-		registerSimulcastHeaderExtensions(m, RTPCodecTypeVideo)
+		assert.NoError(t, m.RegisterDefaultCodecs())
+		assert.NoError(t, ConfigureSimulcastExtensionHeaders(m))
 
 		pcOffer, pcAnswer, err := NewAPI(WithSettingEngine(SettingEngine{
 			LoggerFactory: &undeclaredSsrcLoggerFactory{unhandledSimulcastError},
@@ -1086,7 +1072,6 @@ func TestPeerConnection_Simulcast_Probe(t *testing.T) {
 					filtered += scanner.Text() + "\r\n"
 				}
 			}
-
 			return
 		}))
 
@@ -1228,13 +1213,6 @@ func TestPeerConnection_Simulcast(t *testing.T) {
 	var ridMapLock sync.RWMutex
 	ridMap := map[string]int{}
 
-	// Enable Extension Headers needed for Simulcast
-	m := &MediaEngine{}
-	if err := m.RegisterDefaultCodecs(); err != nil {
-		panic(err)
-	}
-	registerSimulcastHeaderExtensions(m, RTPCodecTypeVideo)
-
 	assertRidCorrect := func(t *testing.T) {
 		ridMapLock.Lock()
 		defer ridMapLock.Unlock()
@@ -1260,7 +1238,7 @@ func TestPeerConnection_Simulcast(t *testing.T) {
 	}
 
 	t.Run("RTP Extension Based", func(t *testing.T) {
-		pcOffer, pcAnswer, err := NewAPI(WithMediaEngine(m)).newPair(Configuration{})
+		pcOffer, pcAnswer, err := newPair()
 		assert.NoError(t, err)
 
 		vp8WriterA, err := NewTrackLocalStaticRTP(RTPCodecCapability{MimeType: MimeTypeVP8}, "video", "pion2", WithRTPStreamID("a"))
@@ -1360,14 +1338,7 @@ func TestPeerConnection_Simulcast_NoDataChannel(t *testing.T) {
 	report := test.CheckRoutines(t)
 	defer report()
 
-	// Enable Extension Headers needed for Simulcast
-	m := &MediaEngine{}
-	if err := m.RegisterDefaultCodecs(); err != nil {
-		panic(err)
-	}
-	registerSimulcastHeaderExtensions(m, RTPCodecTypeVideo)
-
-	pcSender, pcReceiver, err := NewAPI(WithMediaEngine(m)).newPair(Configuration{})
+	pcSender, pcReceiver, err := newPair()
 	assert.NoError(t, err)
 
 	var wg sync.WaitGroup
diff --git a/peerconnection_renegotiation_test.go b/peerconnection_renegotiation_test.go
index 35bd9a5d..e7f245d2 100644
--- a/peerconnection_renegotiation_test.go
+++ b/peerconnection_renegotiation_test.go
@@ -1014,12 +1014,6 @@ func TestPeerConnection_Renegotiation_Simulcast(t *testing.T) {
 	report := test.CheckRoutines(t)
 	defer report()
 
-	m := &MediaEngine{}
-	if err := m.RegisterDefaultCodecs(); err != nil {
-		panic(err)
-	}
-	registerSimulcastHeaderExtensions(m, RTPCodecTypeVideo)
-
 	originalRids := []string{"a", "b", "c"}
 	signalWithRids := func(sessionDescription string, rids []string) string {
 		sessionDescription = strings.SplitAfter(sessionDescription, "a=end-of-candidates\r\n")[0]
@@ -1081,7 +1075,7 @@ func TestPeerConnection_Renegotiation_Simulcast(t *testing.T) {
 
 	t.Run("Disable Transceiver", func(t *testing.T) {
 		trackMap = map[string]*TrackRemote{}
-		pcOffer, pcAnswer, err := NewAPI(WithMediaEngine(m)).newPair(Configuration{})
+		pcOffer, pcAnswer, err := newPair()
 		assert.NoError(t, err)
 
 		vp8Writer, err := NewTrackLocalStaticRTP(RTPCodecCapability{MimeType: MimeTypeVP8}, "video", "pion2")
@@ -1114,7 +1108,7 @@ func TestPeerConnection_Renegotiation_Simulcast(t *testing.T) {
 
 	t.Run("Change RID", func(t *testing.T) {
 		trackMap = map[string]*TrackRemote{}
-		pcOffer, pcAnswer, err := NewAPI(WithMediaEngine(m)).newPair(Configuration{})
+		pcOffer, pcAnswer, err := newPair()
 		assert.NoError(t, err)
 
 		vp8Writer, err := NewTrackLocalStaticRTP(RTPCodecCapability{MimeType: MimeTypeVP8}, "video", "pion2")
diff --git a/rtpreceiver_go_test.go b/rtpreceiver_go_test.go
index 84f1d8fe..7ea51cb7 100644
--- a/rtpreceiver_go_test.go
+++ b/rtpreceiver_go_test.go
@@ -11,6 +11,7 @@ import (
 	"testing"
 	"time"
 
+	"github.com/pion/sdp/v3"
 	"github.com/pion/webrtc/v4/pkg/media"
 	"github.com/stretchr/testify/assert"
 )
@@ -34,9 +35,9 @@ func TestSetRTPParameters(t *testing.T) {
 			},
 		},
 		HeaderExtensions: []RTPHeaderExtensionParameter{
-			{URI: "urn:ietf:params:rtp-hdrext:sdes:mid"},
-			{URI: "urn:ietf:params:rtp-hdrext:sdes:rtp-stream-id"},
-			{URI: "urn:ietf:params:rtp-hdrext:sdes:repaired-rtp-stream-id"},
+			{URI: sdp.SDESMidURI},
+			{URI: sdp.SDESRTPStreamIDURI},
+			{URI: sdesRepairRTPStreamIDURI},
 		},
 	}