Skip to content

Commit 9feebd5

Browse files
committed
Add WithInterceptor to NewPeerConnection
Make it possible to add interceptors at NewPeerConnection time.
1 parent cdacd1c commit 9feebd5

File tree

1 file changed

+21
-4
lines changed

1 file changed

+21
-4
lines changed

Diff for: peerconnection.go

+21-4
Original file line numberDiff line numberDiff line change
@@ -91,24 +91,26 @@ type PeerConnection struct {
9191
log logging.LeveledLogger
9292

9393
interceptorRTCPWriter interceptor.RTCPWriter
94+
95+
extraInterceptors []interceptor.Interceptor
9496
}
9597

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

106108
// NewPeerConnection creates a new PeerConnection with the provided configuration against the received API object.
107109
// This method will attach a default set of codecs and interceptors to
108110
// the resulting PeerConnection. If this behavior is not desired,
109111
// set the set of codecs and interceptors explicitly by using
110112
// [WithMediaEngine] and [WithInterceptorRegistry] when calling [NewAPI].
111-
func (api *API) NewPeerConnection(configuration Configuration) (*PeerConnection, error) {
113+
func (api *API) NewPeerConnection(configuration Configuration, options... func (*PeerConnection) error) (*PeerConnection, error) {
112114
// https://w3c.github.io/webrtc-pc/#constructor (Step #2)
113115
// Some variables defined explicitly despite their implicit zero values to
114116
// allow better readability to understand what is happening.
@@ -136,12 +138,20 @@ func (api *API) NewPeerConnection(configuration Configuration) (*PeerConnection,
136138
api: api,
137139
log: api.settingEngine.LoggerFactory.NewLogger("pc"),
138140
}
141+
142+
for _, option := range options {
143+
err := option(pc)
144+
if err != nil {
145+
return nil, err
146+
}
147+
}
148+
139149
pc.ops = newOperations(pc.updateNegotiationNeededFlagOnEmptyChain, pc.onNegotiationNeeded)
140150

141151
pc.iceConnectionState.Store(ICEConnectionStateNew)
142152
pc.connectionState.Store(PeerConnectionStateNew)
143153

144-
i, err := api.interceptorRegistry.Build("")
154+
i, err := api.interceptorRegistry.Build("", pc.extraInterceptors...)
145155
if err != nil {
146156
return nil, err
147157
}
@@ -195,6 +205,13 @@ func (api *API) NewPeerConnection(configuration Configuration) (*PeerConnection,
195205
return pc, nil
196206
}
197207

208+
func WithInterceptor(i interceptor.Interceptor) func (*PeerConnection) error {
209+
return func(pc *PeerConnection) error {
210+
pc.extraInterceptors = append(pc.extraInterceptors, i)
211+
return nil
212+
}
213+
}
214+
198215
// initConfiguration defines validation of the specified Configuration and
199216
// its assignment to the internal configuration variable. This function differs
200217
// from its SetConfiguration counterpart because most of the checks do not

0 commit comments

Comments
 (0)