@@ -91,24 +91,26 @@ type PeerConnection struct {
91
91
log logging.LeveledLogger
92
92
93
93
interceptorRTCPWriter interceptor.RTCPWriter
94
+
95
+ extraInterceptors []interceptor.Interceptor
94
96
}
95
97
96
98
// NewPeerConnection creates a PeerConnection with the default codecs and interceptors.
97
99
//
98
100
// If you wish to customize the set of available codecs and/or the set of active interceptors,
99
101
// create an API with a custom MediaEngine and/or interceptor.Registry,
100
102
// then call [(*API).NewPeerConnection] instead of this function.
101
- func NewPeerConnection (configuration Configuration ) (* PeerConnection , error ) {
103
+ func NewPeerConnection (configuration Configuration , options ... func ( * PeerConnection ) ) (* PeerConnection , error ) {
102
104
api := NewAPI ()
103
- return api .NewPeerConnection (configuration )
105
+ return api .NewPeerConnection (configuration , options ... )
104
106
}
105
107
106
108
// NewPeerConnection creates a new PeerConnection with the provided configuration against the received API object.
107
109
// This method will attach a default set of codecs and interceptors to
108
110
// the resulting PeerConnection. If this behavior is not desired,
109
111
// set the set of codecs and interceptors explicitly by using
110
112
// [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 ) ) (* PeerConnection , error ) {
112
114
// https://w3c.github.io/webrtc-pc/#constructor (Step #2)
113
115
// Some variables defined explicitly despite their implicit zero values to
114
116
// allow better readability to understand what is happening.
@@ -136,12 +138,17 @@ func (api *API) NewPeerConnection(configuration Configuration) (*PeerConnection,
136
138
api : api ,
137
139
log : api .settingEngine .LoggerFactory .NewLogger ("pc" ),
138
140
}
141
+
142
+ for _ , option := range options {
143
+ option (pc )
144
+ }
145
+
139
146
pc .ops = newOperations (pc .updateNegotiationNeededFlagOnEmptyChain , pc .onNegotiationNeeded )
140
147
141
148
pc .iceConnectionState .Store (ICEConnectionStateNew )
142
149
pc .connectionState .Store (PeerConnectionStateNew )
143
150
144
- i , err := api .interceptorRegistry .Build ("" )
151
+ i , err := api .interceptorRegistry .Build ("" , pc . extraInterceptors ... )
145
152
if err != nil {
146
153
return nil , err
147
154
}
@@ -195,6 +202,12 @@ func (api *API) NewPeerConnection(configuration Configuration) (*PeerConnection,
195
202
return pc , nil
196
203
}
197
204
205
+ func WithInterceptor (i interceptor.Interceptor ) func (* PeerConnection ) {
206
+ return func (pc * PeerConnection ) {
207
+ pc .extraInterceptors = append (pc .extraInterceptors , i )
208
+ }
209
+ }
210
+
198
211
// initConfiguration defines validation of the specified Configuration and
199
212
// its assignment to the internal configuration variable. This function differs
200
213
// from its SetConfiguration counterpart because most of the checks do not
0 commit comments