api.go 2.1 KB

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