Repository: pion/webrtc (go)

Issue:
Make WithMediaEngine to receive a pointer
<!--
  Come join our community on Slack https://pion.ly/slack for quicker support and more resources

  We also provide a Knowledge Base https://pion.ly/knowledge-base that covers Pion and general WebRTC
  
  We maintain a FAQ of common issues and questions https://pion.ly/knowledge-base/pion-basics/faq/

  Please answer these questions before submitting your issue. Thanks!
-->

## Summary

Now that mediaengine allow to register and get ext map headers, api sould receive a pointer in order to use the ME outside Pion conext.

## Motivation

On pion-sfu the subscribers starts with not default codecs, once a user share it's media we need to register that media codec to not offer other codecs asside the one shared.

## Describe alternatives you've considered

No alternatives atm.



Pull request:
Make WithMediaEngine to receive a pointer
#### Description

These change will allow to use the MediaEngine outside Pion context


#### Reference issue
Fixes #1538 


Patch:
diff --git a/api.go b/api.go
index 069fb936..c6010eb5 100644
--- a/api.go
+++ b/api.go
@@ -39,10 +39,14 @@ func NewAPI(options ...func(*API)) *API {
 }
 
 // WithMediaEngine allows providing a MediaEngine to the API.
-// Settings should not be changed after passing the engine to an API.
-func WithMediaEngine(m MediaEngine) func(a *API) {
+// Settings can be changed after passing the engine to an API.
+func WithMediaEngine(m *MediaEngine) func(a *API) {
 	return func(a *API) {
-		a.mediaEngine = &m
+		if m != nil {
+			a.mediaEngine = m
+		} else {
+			a.mediaEngine = &MediaEngine{}
+		}
 	}
 }
 
diff --git a/examples/ice-tcp/main.go b/examples/ice-tcp/main.go
index 8ce3a31b..e60d0767 100644
--- a/examples/ice-tcp/main.go
+++ b/examples/ice-tcp/main.go
@@ -47,7 +47,7 @@ func doSignaling(w http.ResponseWriter, r *http.Request) {
 		settingEngine.SetICETCPMux(tcpMux)
 
 		api := webrtc.NewAPI(
-			webrtc.WithMediaEngine(m),
+			webrtc.WithMediaEngine(&m),
 			webrtc.WithSettingEngine(settingEngine),
 		)
 		if peerConnection, err = api.NewPeerConnection(webrtc.Configuration{}); err != nil {
diff --git a/examples/reflect/main.go b/examples/reflect/main.go
index f240b9b5..cb1037a7 100644
--- a/examples/reflect/main.go
+++ b/examples/reflect/main.go
@@ -27,7 +27,7 @@ func main() {
 	}
 
 	// Create the API object with the MediaEngine
-	api := webrtc.NewAPI(webrtc.WithMediaEngine(m))
+	api := webrtc.NewAPI(webrtc.WithMediaEngine(&m))
 
 	// Prepare the configuration
 	config := webrtc.Configuration{
diff --git a/examples/rtp-forwarder/main.go b/examples/rtp-forwarder/main.go
index 6dadd9c8..4b0166ce 100644
--- a/examples/rtp-forwarder/main.go
+++ b/examples/rtp-forwarder/main.go
@@ -40,7 +40,7 @@ func main() {
 	}
 
 	// Create the API object with the MediaEngine
-	api := webrtc.NewAPI(webrtc.WithMediaEngine(m))
+	api := webrtc.NewAPI(webrtc.WithMediaEngine(&m))
 
 	// Prepare the configuration
 	config := webrtc.Configuration{
diff --git a/examples/save-to-disk/main.go b/examples/save-to-disk/main.go
index 81992887..bf05bf24 100644
--- a/examples/save-to-disk/main.go
+++ b/examples/save-to-disk/main.go
@@ -55,7 +55,7 @@ func main() {
 	}
 
 	// Create the API object with the MediaEngine
-	api := webrtc.NewAPI(webrtc.WithMediaEngine(m))
+	api := webrtc.NewAPI(webrtc.WithMediaEngine(&m))
 
 	// Prepare the configuration
 	config := webrtc.Configuration{
diff --git a/peerconnection.go b/peerconnection.go
index ef15a11f..1797a2d0 100644
--- a/peerconnection.go
+++ b/peerconnection.go
@@ -85,7 +85,7 @@ func NewPeerConnection(configuration Configuration) (*PeerConnection, error) {
 	if err := m.RegisterDefaultCodecs(); err != nil {
 		return nil, err
 	}
-	api := NewAPI(WithMediaEngine(m))
+	api := NewAPI(WithMediaEngine(&m))
 	return api.NewPeerConnection(configuration)
 }
 


Test patch:
diff --git a/api_test.go b/api_test.go
index af7eaf79..b63756c3 100644
--- a/api_test.go
+++ b/api_test.go
@@ -28,7 +28,7 @@ func TestNewAPI_Options(t *testing.T) {
 
 	api := NewAPI(
 		WithSettingEngine(s),
-		WithMediaEngine(m),
+		WithMediaEngine(&m),
 	)
 
 	if !api.settingEngine.detach.DataChannels {