@@ -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 ) error ) (* 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 ) error ) (* 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,20 @@ 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
+ err := option (pc )
144
+ if err != nil {
145
+ return nil , err
146
+ }
147
+ }
148
+
139
149
pc .ops = newOperations (pc .updateNegotiationNeededFlagOnEmptyChain , pc .onNegotiationNeeded )
140
150
141
151
pc .iceConnectionState .Store (ICEConnectionStateNew )
142
152
pc .connectionState .Store (PeerConnectionStateNew )
143
153
144
- i , err := api .interceptorRegistry .Build ("" )
154
+ i , err := api .interceptorRegistry .Build ("" , pc . extraInterceptors ... )
145
155
if err != nil {
146
156
return nil , err
147
157
}
@@ -195,6 +205,13 @@ func (api *API) NewPeerConnection(configuration Configuration) (*PeerConnection,
195
205
return pc , nil
196
206
}
197
207
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
+
198
215
// initConfiguration defines validation of the specified Configuration and
199
216
// its assignment to the internal configuration variable. This function differs
200
217
// from its SetConfiguration counterpart because most of the checks do not
0 commit comments