@@ -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 ) ) (* 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 ) ) (* 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,17 @@ 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+ option (pc )
144+ }
145+
139146 pc .ops = newOperations (pc .updateNegotiationNeededFlagOnEmptyChain , pc .onNegotiationNeeded )
140147
141148 pc .iceConnectionState .Store (ICEConnectionStateNew )
142149 pc .connectionState .Store (PeerConnectionStateNew )
143150
144- i , err := api .interceptorRegistry .Build ("" )
151+ i , err := api .interceptorRegistry .Build ("" , pc . extraInterceptors ... )
145152 if err != nil {
146153 return nil , err
147154 }
@@ -195,6 +202,12 @@ func (api *API) NewPeerConnection(configuration Configuration) (*PeerConnection,
195202 return pc , nil
196203}
197204
205+ func WithInterceptor (i interceptor.Interceptor ) func (* PeerConnection ) {
206+ return func (pc * PeerConnection ) {
207+ pc .extraInterceptors = append (pc .extraInterceptors , i )
208+ }
209+ }
210+
198211// initConfiguration defines validation of the specified Configuration and
199212// its assignment to the internal configuration variable. This function differs
200213// from its SetConfiguration counterpart because most of the checks do not
0 commit comments