-
Notifications
You must be signed in to change notification settings - Fork 119
/
transponder.go
318 lines (252 loc) · 7.13 KB
/
transponder.go
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
package mediaserver
import (
"errors"
"math"
"sort"
native "github.com/notedit/media-server-go/wrapper"
)
type BitrateTraversal string
const (
TraversalDefault BitrateTraversal = "default"
TraversalSpatialTemporal BitrateTraversal = "spatial-temporal"
TraversalTemporalSpatial BitrateTraversal = "temporal-spatial"
TraversalZigZagSpatialTemporal BitrateTraversal = "zig-zag-spatial-temporal"
TraversalZigZagTemporalSpatial BitrateTraversal = "zig-zag-temporal-spatial"
)
// Transponder
type Transponder struct {
muted bool
track *IncomingStreamTrack
transponder native.RTPStreamTransponderFacade
encodingId string
spatialLayerId int
temporalLayerId int
maxSpatialLayerId int
maxTemporalLayerId int
onStopListeners []func()
}
func NewTransponder(transponderFacade native.RTPStreamTransponderFacade) *Transponder {
transponder := new(Transponder)
transponder.muted = false
transponder.transponder = transponderFacade
transponder.spatialLayerId = MaxLayerId
transponder.temporalLayerId = MaxLayerId
transponder.maxSpatialLayerId = MaxLayerId
transponder.maxTemporalLayerId = MaxLayerId
transponder.onStopListeners = make([]func(), 0)
return transponder
}
func (t *Transponder) SetIncomingTrack(incomingTrack *IncomingStreamTrack) error {
if t.transponder == nil {
return errors.New("Transponder is already closed")
}
if incomingTrack == nil {
return errors.New("Track can not be nil")
}
if t.track != nil {
t.track.Detached()
}
t.track = incomingTrack
// we need make sure first encoding id not nil
// todo check
// get first encoding
encoding := t.track.GetFirstEncoding()
if encoding == nil {
panic("encoding is nil")
}
t.transponder.SetIncoming(encoding.GetSource(), incomingTrack.receiver)
t.encodingId = encoding.GetID()
t.spatialLayerId = MaxLayerId
t.temporalLayerId = MaxLayerId
t.maxSpatialLayerId = MaxLayerId
t.maxTemporalLayerId = MaxLayerId
t.track.Attached()
return nil
}
func (t *Transponder) GetIncomingTrack() *IncomingStreamTrack {
return t.track
}
// GetAvailableLayers Get available encodings and layers
func (t *Transponder) GetAvailableLayers() *ActiveLayersInfo {
if t.track != nil {
return t.track.GetActiveLayers()
}
return nil
}
func (t *Transponder) IsMuted() bool {
return t.muted
}
func (t *Transponder) Mute(muting bool) {
if t.muted != muting {
t.muted = muting
if t.transponder != nil {
t.transponder.Mute(muting)
}
}
}
func getSpatialLayerId(layer *Layer) int {
if layer.SpatialLayerId != MaxLayerId {
return layer.SpatialLayerId
}
return layer.SimulcastIdx
}
func (t *Transponder) SetTargetBitrate(bitrate uint, traversal BitrateTraversal, strict bool) uint {
if t.track == nil {
return 0
}
var current uint
var encodingId string
var encodingIdMin string
spatialLayerId := MaxLayerId
temporalLayerId := MaxLayerId
min := uint(math.MaxInt32)
spatialLayerIdMin := MaxLayerId
temporalLayerIdMin := MaxLayerId
var orderfunc func(i int, j int) bool
layers := t.track.GetActiveLayers().Layers
if len(layers) == 0 {
t.Mute(false)
return 0
}
switch traversal {
case TraversalSpatialTemporal:
orderfunc = func(i, j int) bool {
a := layers[i]
b := layers[j]
ret1 := getSpatialLayerId(b)*MaxLayerId + b.TemporalLayerId
ret2 := getSpatialLayerId(a)*MaxLayerId + a.TemporalLayerId
if ret1-ret2 < 0 {
return true
}
return false
}
break
case TraversalZigZagSpatialTemporal:
orderfunc = func(i, j int) bool {
a := layers[i]
b := layers[j]
ret1 := (getSpatialLayerId(b)+b.TemporalLayerId+1)*MaxLayerId - b.TemporalLayerId
ret2 := (getSpatialLayerId(a)+a.TemporalLayerId+1)*MaxLayerId - a.TemporalLayerId
if ret1-ret2 < 0 {
return true
}
return false
}
break
case TraversalTemporalSpatial:
orderfunc = func(i, j int) bool {
a := layers[i]
b := layers[j]
ret1 := b.TemporalLayerId*MaxLayerId + getSpatialLayerId(b)
ret2 := a.TemporalLayerId*MaxLayerId + getSpatialLayerId(a)
if ret1-ret2 < 0 {
return true
}
return false
}
break
case TraversalZigZagTemporalSpatial:
orderfunc = func(i, j int) bool {
a := layers[i]
b := layers[j]
ret1 := (getSpatialLayerId(b)+b.TemporalLayerId+1)*MaxLayerId - getSpatialLayerId(b)
ret2 := (getSpatialLayerId(a)+a.TemporalLayerId+1)*MaxLayerId - getSpatialLayerId(a)
if ret1-ret2 < 0 {
return true
}
return false
}
break
default:
break
}
if orderfunc != nil {
sort.SliceStable(layers, orderfunc)
}
for _, layer := range layers {
if layer.Bitrate <= bitrate && layer.Bitrate > current && t.maxSpatialLayerId >= layer.SpatialLayerId && t.maxSpatialLayerId >= layer.TemporalLayerId {
encodingId = layer.EncodingId
spatialLayerId = layer.SpatialLayerId
temporalLayerId = layer.TemporalLayerId
current = layer.Bitrate
break
}
if layer.Bitrate > 0 && layer.Bitrate < min && t.maxSpatialLayerId >= layer.SpatialLayerId {
encodingIdMin = layer.EncodingId
spatialLayerIdMin = layer.SpatialLayerId
temporalLayerIdMin = layer.TemporalLayerId
min = layer.Bitrate
}
}
// Check if we have been able to find a layer that matched the target bitrate
if current <= 0 {
if strict == false {
t.Mute(false)
t.SelectEncoding(encodingIdMin)
t.SelectLayer(spatialLayerIdMin, temporalLayerIdMin)
return min
} else {
t.Mute(true)
return 0
}
}
t.Mute(false)
t.SelectEncoding(encodingId)
t.SelectLayer(spatialLayerId, temporalLayerId)
return current
}
// SelectEncoding by id
func (t *Transponder) SelectEncoding(encodingId string) {
if t.encodingId == encodingId {
return
}
encoding := t.track.GetEncoding(encodingId)
if encoding == nil {
return
}
t.transponder.SetIncoming(encoding.GetSource(), t.track.receiver)
t.encodingId = encodingId
}
// GetSelectedEncoding get selected encoding id
func (t *Transponder) GetSelectedEncoding() string {
return t.encodingId
}
// GetSelectedSpatialLayerId return int
func (t *Transponder) GetSelectedSpatialLayerId() int {
return t.spatialLayerId
}
// GetSelectedTemporalLayerId return int
func (t *Transponder) GetSelectedTemporalLayerId() int {
return t.temporalLayerId
}
// SelectLayer Select SVC temporatl and spatial layers. Only available for VP9 media.
func (t *Transponder) SelectLayer(spatialLayerId, temporalLayerId int) {
spatialLayerId = Min(spatialLayerId, t.maxSpatialLayerId)
temporalLayerId = Min(temporalLayerId, t.maxTemporalLayerId)
if t.spatialLayerId == spatialLayerId && t.temporalLayerId == temporalLayerId {
return
}
t.transponder.SelectLayer(spatialLayerId, temporalLayerId)
t.spatialLayerId = spatialLayerId
t.temporalLayerId = temporalLayerId
}
func (t *Transponder) SetMaximumLayers(maxSpatialLayerId, maxTemporalLayerId int) {
if maxSpatialLayerId < 0 || maxTemporalLayerId < 0 {
return
}
t.maxSpatialLayerId = maxSpatialLayerId
t.maxTemporalLayerId = maxTemporalLayerId
}
// Stop stop this transponder
func (t *Transponder) Stop() {
if t.transponder == nil {
return
}
if t.track != nil {
t.track.Detached()
}
t.transponder.Close()
native.DeleteRTPStreamTransponderFacade(t.transponder)
t.transponder = nil
t.track = nil
}