Skip to content

Commit

Permalink
NewAPI: register default codecs and interceptors
Browse files Browse the repository at this point in the history
  • Loading branch information
andrein authored and Sean-Der committed Dec 11, 2023
1 parent 2fcd8a1 commit 9726417
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 14 deletions.
29 changes: 22 additions & 7 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
}

Check warning on line 52 in api.go

View check run for this annotation

Codecov / codecov/patch

api.go#L51-L52

Added lines #L51 - L52 were not covered by tests
}

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)
}

Check warning on line 60 in api.go

View check run for this annotation

Codecov / codecov/patch

api.go#L59-L60

Added lines #L59 - L60 were not covered by tests
}

return a
}

Expand Down
2 changes: 1 addition & 1 deletion track_local_static_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
11 changes: 5 additions & 6 deletions vnet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package webrtc

import (
"github.com/pion/interceptor"

Check failure on line 10 in vnet_test.go

View workflow job for this annotation

GitHub Actions / lint / Go

File is not `gci`-ed with --skip-generated -s standard,default (gci)
"testing"
"time"

Check failure on line 12 in vnet_test.go

View workflow job for this annotation

GitHub Actions / lint / Go

File is not `gofumpt`-ed (gofumpt)

Check failure on line 13 in vnet_test.go

View workflow job for this annotation

GitHub Actions / lint / Go

File is not `gci`-ed with --skip-generated -s standard,default (gci)
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 9726417

Please sign in to comment.