Skip to content

Commit

Permalink
Add WithInterceptor to NewPeerConnection
Browse files Browse the repository at this point in the history
Make it possible to add interceptors at NewPeerConnection time.
  • Loading branch information
jech committed Jan 13, 2025
1 parent f2191fb commit ab2e05f
Showing 1 changed file with 21 additions and 4 deletions.
25 changes: 21 additions & 4 deletions peerconnection.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,24 +91,26 @@ type PeerConnection struct {
log logging.LeveledLogger

interceptorRTCPWriter interceptor.RTCPWriter

extraInterceptors []interceptor.Interceptor
}

// NewPeerConnection creates a PeerConnection with the default codecs and interceptors.
//
// If you wish to customize the set of available codecs and/or the set of active interceptors,
// create an API with a custom MediaEngine and/or interceptor.Registry,
// then call [(*API).NewPeerConnection] instead of this function.
func NewPeerConnection(configuration Configuration) (*PeerConnection, error) {
func NewPeerConnection(configuration Configuration, options... func (*PeerConnection) error) (*PeerConnection, error) {
api := NewAPI()
return api.NewPeerConnection(configuration)
return api.NewPeerConnection(configuration, options...)
}

// NewPeerConnection creates a new PeerConnection with the provided configuration against the received API object.
// This method will attach a default set of codecs and interceptors to
// the resulting PeerConnection. If this behavior is not desired,
// set the set of codecs and interceptors explicitly by using
// [WithMediaEngine] and [WithInterceptorRegistry] when calling [NewAPI].
func (api *API) NewPeerConnection(configuration Configuration) (*PeerConnection, error) {
func (api *API) NewPeerConnection(configuration Configuration, options... func (*PeerConnection) error) (*PeerConnection, error) {
// https://w3c.github.io/webrtc-pc/#constructor (Step #2)
// Some variables defined explicitly despite their implicit zero values to
// allow better readability to understand what is happening.
Expand Down Expand Up @@ -136,12 +138,20 @@ func (api *API) NewPeerConnection(configuration Configuration) (*PeerConnection,
api: api,
log: api.settingEngine.LoggerFactory.NewLogger("pc"),
}

for _, option := range options {
err := option(pc)
if err != nil {
return nil, err
}
}

pc.ops = newOperations(pc.updateNegotiationNeededFlagOnEmptyChain, pc.onNegotiationNeeded)

pc.iceConnectionState.Store(ICEConnectionStateNew)
pc.connectionState.Store(PeerConnectionStateNew)

i, err := api.interceptorRegistry.Build("")
i, err := api.interceptorRegistry.Build("", pc.extraInterceptors...)

Check failure on line 154 in peerconnection.go

View workflow job for this annotation

GitHub Actions / lint / Go

cannot use ... in call to non-variadic api.interceptorRegistry.Build) (typecheck)

Check failure on line 154 in peerconnection.go

View workflow job for this annotation

GitHub Actions / lint / Go

cannot use ... in call to non-variadic api.interceptorRegistry.Build) (typecheck)

Check failure on line 154 in peerconnection.go

View workflow job for this annotation

GitHub Actions / lint / Go

cannot use ... in call to non-variadic api.interceptorRegistry.Build) (typecheck)

Check failure on line 154 in peerconnection.go

View workflow job for this annotation

GitHub Actions / lint / Go

cannot use ... in call to non-variadic api.interceptorRegistry.Build (typecheck)

Check failure on line 154 in peerconnection.go

View workflow job for this annotation

GitHub Actions / test (1.22) / Go 1.22

cannot use ... in call to non-variadic api.interceptorRegistry.Build

Check failure on line 154 in peerconnection.go

View workflow job for this annotation

GitHub Actions / test (1.22) / Go 1.22

cannot use ... in call to non-variadic api.interceptorRegistry.Build

Check failure on line 154 in peerconnection.go

View workflow job for this annotation

GitHub Actions / test (1.23) / Go 1.23

cannot use ... in call to non-variadic api.interceptorRegistry.Build

Check failure on line 154 in peerconnection.go

View workflow job for this annotation

GitHub Actions / test (1.23) / Go 1.23

cannot use ... in call to non-variadic api.interceptorRegistry.Build
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -195,6 +205,13 @@ func (api *API) NewPeerConnection(configuration Configuration) (*PeerConnection,
return pc, nil
}

func WithInterceptor(i interceptor.Interceptor) func (*PeerConnection) error {
return func(pc *PeerConnection) error {
pc.extraInterceptors = append(pc.extraInterceptors, i)
return nil
}
}

// initConfiguration defines validation of the specified Configuration and
// its assignment to the internal configuration variable. This function differs
// from its SetConfiguration counterpart because most of the checks do not
Expand Down

0 comments on commit ab2e05f

Please sign in to comment.