From 067852057ed7d74c881c09c1d98e384db18a01ba Mon Sep 17 00:00:00 2001 From: Andrei Nistor Date: Fri, 1 Dec 2023 22:52:23 +0200 Subject: [PATCH] NewAPI: register default codecs and interceptors --- api.go | 29 ++++++++++++++++++++++------- track_local_static_test.go | 2 +- vnet_test.go | 11 +++++------ 3 files changed, 28 insertions(+), 14 deletions(-) diff --git a/api.go b/api.go index 61e52eee11c..369b1a9a378 100644 --- a/api.go +++ b/api.go @@ -26,15 +26,12 @@ type API struct { // NewAPI Creates a new API object for keeping semi-global settings to WebRTC objects // -// WARNING: No Codecs or Interceptors are enabled by default. Unless configured properly a -// PeerConnection will not be able to transmit media. For an example of how to do that, -// see the body of the webrtc.NewPeerConnection() function. +// It uses the default Codecs and Interceptors unless you customize them +// using WithMediaEngine and WithInterceptorRegistry respectively. func NewAPI(options ...func(*API)) *API { a := &API{ - interceptor: &interceptor.NoOp{}, - settingEngine: &SettingEngine{}, - mediaEngine: &MediaEngine{}, - interceptorRegistry: &interceptor.Registry{}, + interceptor: &interceptor.NoOp{}, + settingEngine: &SettingEngine{}, } for _, o := range options { @@ -45,6 +42,24 @@ func NewAPI(options ...func(*API)) *API { a.settingEngine.LoggerFactory = logging.NewDefaultLoggerFactory() } + logger := a.settingEngine.LoggerFactory.NewLogger("api") + + if a.mediaEngine == nil { + a.mediaEngine = &MediaEngine{} + err := a.mediaEngine.RegisterDefaultCodecs() + if err != nil { + logger.Errorf("Failed to register default codecs %s", err) + } + } + + if a.interceptorRegistry == nil { + a.interceptorRegistry = &interceptor.Registry{} + err := RegisterDefaultInterceptors(a.mediaEngine, a.interceptorRegistry) + if err != nil { + logger.Errorf("Failed to register default interceptors %s", err) + } + } + return a } diff --git a/track_local_static_test.go b/track_local_static_test.go index 0c09eddff2b..423d3942ec2 100644 --- a/track_local_static_test.go +++ b/track_local_static_test.go @@ -33,7 +33,7 @@ func Test_TrackLocalStatic_NoCodecIntersection(t *testing.T) { pc, err := NewPeerConnection(Configuration{}) assert.NoError(t, err) - noCodecPC, err := NewAPI().NewPeerConnection(Configuration{}) + noCodecPC, err := NewAPI(WithMediaEngine(&MediaEngine{})).NewPeerConnection(Configuration{}) assert.NoError(t, err) _, err = pc.AddTrack(track) diff --git a/vnet_test.go b/vnet_test.go index 047ce56ffa2..2ebef42f9e8 100644 --- a/vnet_test.go +++ b/vnet_test.go @@ -7,6 +7,7 @@ package webrtc import ( + "github.com/pion/interceptor" "testing" "time" @@ -52,14 +53,12 @@ func createVNetPair(t *testing.T) (*PeerConnection, *PeerConnection, *vnet.Route // Start the virtual network by calling Start() on the root router assert.NoError(t, wan.Start()) - offerMediaEngine := &MediaEngine{} - assert.NoError(t, offerMediaEngine.RegisterDefaultCodecs()) - offerPeerConnection, err := NewAPI(WithSettingEngine(offerSettingEngine), WithMediaEngine(offerMediaEngine)).NewPeerConnection(Configuration{}) + offerInterceptorRegistry := &interceptor.Registry{} + offerPeerConnection, err := NewAPI(WithSettingEngine(offerSettingEngine), WithInterceptorRegistry(offerInterceptorRegistry)).NewPeerConnection(Configuration{}) assert.NoError(t, err) - answerMediaEngine := &MediaEngine{} - assert.NoError(t, answerMediaEngine.RegisterDefaultCodecs()) - answerPeerConnection, err := NewAPI(WithSettingEngine(answerSettingEngine), WithMediaEngine(answerMediaEngine)).NewPeerConnection(Configuration{}) + answerInterceptorRegistry := &interceptor.Registry{} + answerPeerConnection, err := NewAPI(WithSettingEngine(answerSettingEngine), WithInterceptorRegistry(answerInterceptorRegistry)).NewPeerConnection(Configuration{}) assert.NoError(t, err) return offerPeerConnection, answerPeerConnection, wan