The Registry type is a collector for interceptor factories that can build a composite interceptor from all registered factories.
Type Definition
type Registry struct {
factories []Factory
}
Internal slice of registered interceptor factories
Methods
Add
func (r *Registry) Add(f Factory)
Adds a new interceptor factory to the registry.
The factory to add to the registry
Build
func (r *Registry) Build(id string) (Interceptor, error)
Constructs a single interceptor from all registered factories. If no factories are registered, returns a NoOp interceptor. If multiple factories are registered, returns a Chain containing all the constructed interceptors.
Unique identifier passed to all factories (typically a PeerConnection ID)
Returns:
Interceptor - Either a NoOp, a single interceptor, or a Chain of interceptors
error - Any error that occurred during construction
Usage Example
import (
"github.com/pion/interceptor"
"github.com/pion/interceptor/pkg/nack"
"github.com/pion/interceptor/pkg/report"
"github.com/pion/interceptor/pkg/stats"
)
// Create a registry
registry := &interceptor.Registry{}
// Add factories
nackGen, _ := nack.NewGeneratorInterceptor()
registry.Add(nackGen)
nackResp, _ := nack.NewResponderInterceptor()
registry.Add(nackResp)
reportRecv, _ := report.NewReceiverInterceptor()
registry.Add(reportRecv)
statsInt, _ := stats.NewInterceptor()
registry.Add(statsInt)
// Build a composite interceptor for a PeerConnection
i, err := registry.Build("peer-connection-1")
if err != nil {
panic(err)
}
defer i.Close()
// Use the interceptor
// ...
Integration with WebRTC
import (
"github.com/pion/interceptor"
"github.com/pion/webrtc/v4"
)
func setupPeerConnection() (*webrtc.PeerConnection, error) {
// Create MediaEngine and register codecs
m := &webrtc.MediaEngine{}
if err := m.RegisterDefaultCodecs(); err != nil {
return nil, err
}
// Create InterceptorRegistry
ir := &interceptor.Registry{}
// Register default interceptors
if err := webrtc.RegisterDefaultInterceptors(m, ir); err != nil {
return nil, err
}
// Add custom interceptors
customFactory, _ := nack.NewGeneratorInterceptor(
nack.GeneratorSize(512),
)
ir.Add(customFactory)
// Create API and PeerConnection
api := webrtc.NewAPI(
webrtc.WithMediaEngine(m),
webrtc.WithInterceptorRegistry(ir),
)
return api.NewPeerConnection(webrtc.Configuration{})
}
Advanced Usage: Conditional Registration
func createRegistry(config Config) *interceptor.Registry {
registry := &interceptor.Registry{}
// Always add stats
statsFactory, _ := stats.NewInterceptor()
registry.Add(statsFactory)
// Conditionally add NACK
if config.EnableNACK {
nackGen, _ := nack.NewGeneratorInterceptor(
nack.GeneratorSize(config.NACKBufferSize),
)
registry.Add(nackGen)
nackResp, _ := nack.NewResponderInterceptor(
nack.ResponderSize(config.NACKBufferSize),
)
registry.Add(nackResp)
}
// Conditionally add congestion control
if config.EnableCC {
ccFactory, _ := cc.NewInterceptor(nil)
registry.Add(ccFactory)
}
return registry
}
Behavior
Empty Registry
If Build() is called on an empty registry:
registry := &interceptor.Registry{}
i, _ := registry.Build("id")
// i will be a &interceptor.NoOp{}
Single Factory
If only one factory is registered:
registry := &interceptor.Registry{}
registry.Add(someFactory)
i, _ := registry.Build("id")
// i will be the single interceptor from someFactory
Multiple Factories
If multiple factories are registered:
registry := &interceptor.Registry{}
registry.Add(factory1)
registry.Add(factory2)
registry.Add(factory3)
i, _ := registry.Build("id")
// i will be a *interceptor.Chain containing all three interceptors
See Also
- Factory - Interface for creating interceptors
- Chain - Combines multiple interceptors
- Interceptor - The core interface
- NoOp - Default empty interceptor
Reference
For more details, see the pkg.go.dev documentation.