github.com/pion/webrtc/v3@v3.2.24/api.go (about)

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