api.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. //go:build !js
  2. // +build !js
  3. package webrtc
  4. import (
  5. "github.com/pion/interceptor"
  6. "github.com/pion/logging"
  7. )
  8. // API allows configuration of a PeerConnection
  9. // with APIs that are available in the standard. This
  10. // lets you set custom behavior via the SettingEngine, configure
  11. // codecs via the MediaEngine and define custom media behaviors via
  12. // Interceptors.
  13. type API struct {
  14. settingEngine *SettingEngine
  15. mediaEngine *MediaEngine
  16. interceptorRegistry *interceptor.Registry
  17. interceptor interceptor.Interceptor // Generated per PeerConnection
  18. }
  19. // NewAPI Creates a new API object for keeping semi-global settings to WebRTC objects
  20. func NewAPI(options ...func(*API)) *API {
  21. a := &API{
  22. interceptor: &interceptor.NoOp{},
  23. settingEngine: &SettingEngine{},
  24. mediaEngine: &MediaEngine{},
  25. interceptorRegistry: &interceptor.Registry{},
  26. }
  27. for _, o := range options {
  28. o(a)
  29. }
  30. if a.settingEngine.LoggerFactory == nil {
  31. a.settingEngine.LoggerFactory = logging.NewDefaultLoggerFactory()
  32. }
  33. return a
  34. }
  35. // WithMediaEngine allows providing a MediaEngine to the API.
  36. // Settings can be changed after passing the engine to an API.
  37. // When a PeerConnection is created the MediaEngine is copied
  38. // and no more changes can be made.
  39. func WithMediaEngine(m *MediaEngine) func(a *API) {
  40. return func(a *API) {
  41. a.mediaEngine = m
  42. if a.mediaEngine == nil {
  43. a.mediaEngine = &MediaEngine{}
  44. }
  45. }
  46. }
  47. // WithSettingEngine allows providing a SettingEngine to the API.
  48. // Settings should not be changed after passing the engine to an API.
  49. func WithSettingEngine(s SettingEngine) func(a *API) {
  50. return func(a *API) {
  51. a.settingEngine = &s
  52. }
  53. }
  54. // WithInterceptorRegistry allows providing Interceptors to the API.
  55. // Settings should not be changed after passing the registry to an API.
  56. func WithInterceptorRegistry(ir *interceptor.Registry) func(a *API) {
  57. return func(a *API) {
  58. a.interceptorRegistry = ir
  59. if a.interceptorRegistry == nil {
  60. a.interceptorRegistry = &interceptor.Registry{}
  61. }
  62. }
  63. }