-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathcodecs.go
More file actions
242 lines (212 loc) · 5.57 KB
/
Copy pathcodecs.go
File metadata and controls
242 lines (212 loc) · 5.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
// Copyright 2024 LiveKit, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package media
import (
"slices"
"strings"
)
type CodecParams []CodecParam
func (arr CodecParams) String() string {
params := make([]string, 0, len(arr))
for _, p := range arr {
params = append(params, p.String())
}
return strings.Join(params, ";")
}
func (arr CodecParams) Get(key string) (string, bool) {
for _, p := range arr {
if p.Key == key {
return p.Val, true
}
}
return "", false
}
func (arr CodecParams) Has(key string) bool {
for _, p := range arr {
if p.Key == key {
return true
}
}
return false
}
func (arr CodecParams) HasValue(key, val string) bool {
for _, p := range arr {
if p.Key == key {
return p.Val == val
}
}
return false
}
func (arr CodecParams) HasParam(p2 CodecParam) bool {
for _, p := range arr {
if p.Key == p2.Key {
return p.Val == p2.Val
}
}
return false
}
type CodecParam struct {
Key string
Val string
}
func (p CodecParam) String() string {
if p.Val == "" {
return p.Key
}
return p.Key + "=" + p.Val
}
type CodecInfo struct {
SDPName string
SampleRate int
RTPClockRate int
RTPDefType byte
RTPIsStatic bool
Priority int // higher is preferable
Disabled bool // codec is disabled in GlobalCodecs by default
Hidden bool // codec should not appear in SDP offer, but can be used in the answer
FileExt string
ReqParams CodecParams // a list of required codec params (fmtp)
}
type Codec interface {
Info() CodecInfo
}
var (
globalSet = NewCodecSet()
codecs []Codec
codecOnRegister []func(c Codec)
)
// GlobalCodecs returns a shared codec set.
func GlobalCodecs() *CodecSet {
return globalSet
}
// NewCodecSet creates an empty codec set. All codecs are disabled, unless enabled explicitly.
func NewCodecSet() *CodecSet {
return &CodecSet{
enabled: make(map[string]bool),
}
}
// CodecSet represents a set of codecs that can be enabled or disabled.
type CodecSet struct {
parent *CodecSet
enabled map[string]bool
}
// NewSet creates a codec set that overlays the current codec set.
// It will inherit all codecs enabled in the parent set.
func (s *CodecSet) NewSet() *CodecSet {
c2 := NewCodecSet()
c2.parent = s
return c2
}
// SetEnabled enables or disables a given codec.
func (s *CodecSet) SetEnabled(name string, enabled bool) {
name = strings.ToLower(name)
s.enabled[name] = enabled
}
// SetEnabledMap is the same as SetEnabled, but accepts a map with multiple codecs.
func (s *CodecSet) SetEnabledMap(codecs map[string]bool) {
for name, enabled := range codecs {
s.SetEnabled(name, enabled)
}
}
// IsEnabledByName checks if a given codec is enabled by its name.
func (s *CodecSet) IsEnabledByName(name string) bool {
if s == nil {
return false
}
name = strings.ToLower(name)
for s := s; s != nil; s = s.parent {
if enabled, ok := s.enabled[name]; ok {
return enabled
}
}
return false
}
// IsEnabled checks if a given codec is enabled.
func (s *CodecSet) IsEnabled(c Codec) bool {
if s == nil || c == nil {
return false
}
return s.IsEnabledByName(c.Info().SDPName)
}
// ListEnabled lists all enabled codecs.
func (s *CodecSet) ListEnabled() []Codec {
if s == nil {
return nil
}
out := make([]Codec, 0, len(codecs))
for _, c := range codecs {
if s.IsEnabled(c) {
out = append(out, c)
}
}
return out
}
// CodecSetEnabled enables or disables a codec in the GlobalCodecs set.
func CodecSetEnabled(name string, enabled bool) {
GlobalCodecs().SetEnabled(name, enabled)
}
// CodecsSetEnabled enables or disables multiple codecs in the GlobalCodecs set.
func CodecsSetEnabled(codecs map[string]bool) {
GlobalCodecs().SetEnabledMap(codecs)
}
// CodecEnabled checks if the codec is enabled in the GlobalCodecs set.
func CodecEnabled(c Codec) bool {
return GlobalCodecs().IsEnabled(c)
}
// CodecEnabledByName checks if the codec name is enabled in the GlobalCodecs set.
func CodecEnabledByName(name string) bool {
return GlobalCodecs().IsEnabledByName(name)
}
func OnRegister(fnc func(c Codec)) {
// Call it on already registered codecs first, so that the import order doesn't matter.
for _, c := range codecs {
fnc(c)
}
// Add the function for codecs that will be registered next.
codecOnRegister = append(codecOnRegister, fnc)
}
// Codecs lists all registered codecs.
func Codecs() []Codec {
return slices.Clone(codecs)
}
// EnabledCodecs lists all codecs enabled in the GlobalCodecs set.
func EnabledCodecs() []Codec {
return GlobalCodecs().ListEnabled()
}
// RegisterCodec registers the codec.
func RegisterCodec(c Codec) {
info := c.Info()
global := GlobalCodecs()
codecs = append(codecs, c)
global.SetEnabled(info.SDPName, !info.Disabled)
for _, fnc := range codecOnRegister {
fnc(c)
}
}
// NewCodec creates a generic codec definition without a specific implementation.
func NewCodec(info CodecInfo) Codec {
if info.SampleRate <= 0 {
panic("invalid sample rate")
}
if info.RTPClockRate == 0 {
info.RTPClockRate = info.SampleRate
}
return &baseCodec{info: info}
}
type baseCodec struct {
info CodecInfo
}
func (c *baseCodec) Info() CodecInfo {
return c.info
}