forked from robreys/media-server-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTransponder.js
491 lines (439 loc) · 13.2 KB
/
Transponder.js
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
const EventEmitter = require("events").EventEmitter;
const LayerInfo = require("./LayerInfo");
/**
* Transponder copies data from an incoming track to an outgoing track and allows stream modifications
*/
class Transponder
{
/**
* @ignore
* @hideconstructor
* private constructor
*/
constructor(transponder)
{
//Store native trasnceiver
this.transponder = transponder;
//No track
this.track = null;
this.muted = false;
this.spatialLayerId = LayerInfo.MaxLayerId;
this.temporalLayerId = LayerInfo.MaxLayerId;
this.maxSpatialLayerId = LayerInfo.MaxLayerId;
this.maxTemporalLayerId = LayerInfo.MaxLayerId;
//The listener for attached tracks end event
this.onAttachedTrackStopped = () => {
//If stopped already
if (!this.transponder)
//Do nothing
return;
//Signal dettached
this.track.detached();
//Dettach
this.track = null;
//Stop listening
this.transponder.SetIncoming(null,null);
};
//Create event emitter
this.emitter = new EventEmitter();
}
/**
* Set incoming track
* @param {IncomingStreamTrack} track
*/
setIncomingTrack(track)
{
//If it is the same track
if (this.track == track)
//Do nothing
return;
//Check we are not already closed
if (!this.transponder)
//Error
throw new Error("Transponder is already closed");
//If was previously attached
if (this.track)
{
//Remove stop listener
this.track.off("stopped", this.onAttachedTrackStopped);
//Signal dettached
this.track.detached();
}
//Store new track info
this.track = track;
//If removing track
if (this.track)
{
//Get first encoding
const encoding = this.track.encodings.values().next();
//Start listening to it
this.transponder.SetIncoming(encoding.value.source,track.receiver);
//Store current values
this.encodingId = encoding.first;
this.spatialLayerId = LayerInfo.MaxLayerId;
this.temporalLayerId = LayerInfo.MaxLayerId;
this.maxSpatialLayerId = LayerInfo.MaxLayerId;
this.maxTemporalLayerId = LayerInfo.MaxLayerId;
//Add stop listener
this.track.once("stopped",this.onAttachedTrackStopped);
//Singal track is attached
this.track.attached();
//If it has h264 properties
if (this.track.hasH264ParameterSets && this.track.hasH264ParameterSets())
//Set it
this.transponder.AppendH264ParameterSets(this.track.getH264ParameterSets());
} else {
//Stop listening
this.transponder.SetIncoming(null,null);
}
}
appendH264ParameterSets(sprop)
{
this.transponder.AppendH264ParameterSets(sprop);
}
/**
* Get attached track
* @returns {IncomingStreamTrack} track
*/
getIncomingTrack()
{
return this.track;
}
/**
* Get available encodings and layers
* @returns {Object}
*/
getAvailableLayers()
{
return this.track ? this.track.getActiveLayers() : null;
}
/**
* Check if the track is muted or not
* @returns {boolean} muted
*/
isMuted()
{
return this.muted;
}
/**
* Mute/Unmute track
* This operation will not change the muted state of the stream this track belongs too.
* @param {boolean} muting - if we want to mute or unmute
*/
mute(muting)
{
//If we are different
if (this.muted!==muting)
{
//Store it
this.muted = muting;
//Call native transponder
this.transponder && this.transponder.Mute(muting);
/**
* Transponder muted event
*
* @name muted
* @memberof Transponder
* @kind event
* @argument {Transponder} transponder
*/
this.emitter.emit("muted",this.muted);
}
}
/**
* Select encoding and temporal and spatial layers based on the desired bitrate. This operation will unmute the transponder if it was mutted and it is possible to select an encoding and layer based on the target bitrate and options.
*
* @param {Number} bitrate
* @param {Object} options - Options for configuring algorithm to select best encoding/layers [Optional]
* @param {Object} options.traversal - Traversal algorithm "default", "spatial-temporal", "zig-zag-spatial-temporal", "temporal-spatial", "zig-zag-temporal-spatial" [Default: "default"]
* @param {Object} options.strict - If there is not a layer with a bitrate lower thatn target, stop sending media [Default: false]
* @returns {Number} Current bitrate of the selected encoding and layers, it aslo incudes the selected layer indexes and available layers as properties of the Number object.
*/
setTargetBitrate(target, options)
{
//Check track
if (!this.track)
//Ignore
return;
//For optimum fit
let current = -1;
let encodingId = "";
let spatialLayerId = LayerInfo.MaxLayerId;
let temporalLayerId = LayerInfo.MaxLayerId;
//For minimum fit
let min = Number.MAX_SAFE_INTEGER;
let encodingIdMin = "";
let spatialLayerIdMin = LayerInfo.MaxLayerId;
let temporalLayerIdMin = LayerInfo.MaxLayerId;
let ordering = false;
//Helper for retrieving spatial info
const getSpatialLayerId = function(layer) {
// Either spatialLayerId on SVC stream or simulcastIdx on simulcast stream
return layer.spatialLayerId!=LayerInfo.MaxLayerId ? layer.spatialLayerId : layer.simulcastIdx ;
};
//Depending on the traversal method
switch (options && options.traversal)
{
case "spatial-temporal":
ordering = (a,b) => ((getSpatialLayerId(b)*LayerInfo.MaxLayerId+b.temporalLayerId) - (getSpatialLayerId(a)*LayerInfo.MaxLayerId+a.temporalLayerId));
break;
case "zig-zag-spatial-temporal":
ordering = (a,b) => (((getSpatialLayerId(b)+b.temporalLayerId+1)*LayerInfo.MaxLayerId-b.temporalLayerId) - ((getSpatialLayerId(a)+a.temporalLayerId+1)*LayerInfo.MaxLayerId-a.temporalLayerId));
break;
case "temporal-spatial":
ordering = (a,b) => ((b.temporalLayerId*LayerInfo.MaxLayerId+getSpatialLayerId(b)) - (a.temporalLayerId*LayerInfo.MaxLayerId+getSpatialLayerId(a)));
break;
case "zig-zag-temporal-spatial":
ordering = (a,b) => (((getSpatialLayerId(b)+b.temporalLayerId+1)*LayerInfo.MaxLayerId-getSpatialLayerId(b)) - ((getSpatialLayerId(a)+a.temporalLayerId+1)*LayerInfo.MaxLayerId-getSpatialLayerId(a)));
break;
default:
//Default
}
//Get all active layers
const info = this.track.getActiveLayers();
//Filter layers by max TL and SL
const filtered = info.layers.filter(layer=>this.maxSpatialLayerId>=layer.spatialLayerId && this.maxTemporalLayerId>=layer.temporalLayerId);
//Get layers and filter by max TL & SL
const layers = ordering ? filtered.sort(ordering) : filtered;
//If there are no layers
if (!layers.length)
{
//Important: Do not mute us!
//Not sending anything
return Object.assign(new Number(0),{
layerIndex : -1,
layers : layers
});
}
//selected layer index
let layerMinIndex = 0;
let layerIndex = 0;
//Try to do layer selection instead
for (let layer of layers)
{
//If this layer is better than the one before
if (layer.bitrate<=target && layer.bitrate>current &&
this.maxSpatialLayerId>=layer.spatialLayerId && this.maxTemporalLayerId>=layer.temporalLayerId)
{
//Use it as is
encodingId = layer.encodingId;
spatialLayerId = layer.spatialLayerId;
temporalLayerId = layer.temporalLayerId;
//Update max current bitrate
current = layer.bitrate;
//we don't want to look more
break;
}
//Check if it is the minimum
if (layer.bitrate && layer.bitrate<min &&
this.maxSpatialLayerId>=layer.spatialLayerId && this.maxTemporalLayerId>=layer.temporalLayerId)
{
//Use it as min
layerMinIndex = layerIndex;
encodingIdMin = layer.encodingId;
spatialLayerIdMin = layer.spatialLayerId;
temporalLayerIdMin = layer.temporalLayerId;
//Update min bitrate
min = layer.bitrate;
}
//Next
layerIndex++;
}
//Check if we have been able to find a layer that matched the target bitrate
if (current<=0)
{
//If we can use the minimun
if (!options || !options["strict"])
{
//Unmute (jic)
this.mute(false);
//Select mimimun as no layer is able to match the desired bitrate
this.selectEncoding(encodingIdMin);
//And temporal/spatial layers
this.selectLayer(spatialLayerIdMin,temporalLayerIdMin);
//Return minimun bitrate for selected encoding/layer
return Object.assign(new Number(min),{
layer : layers[layerMinIndex],
layerIndex : layerMinIndex,
encodingId : encodingIdMin,
spatialLayerId : spatialLayerIdMin,
temporalLayerId : temporalLayerIdMin,
layers : layers
});
} else {
//Mute it
this.mute(true);
//Not sending anything
return Object.assign(new Number(0),{
layerIndex : -1,
layers : layers
});
}
}
//Unmute (jic)
this.mute(false);
//Select enccoding
this.selectEncoding(encodingId);
//And temporal/spatial layers
this.selectLayer(spatialLayerId,temporalLayerId);
//Return current bitrate for selected encoding/layer
return Object.assign(new Number(current),{
layer : layers[layerIndex],
layerIndex : layerIndex,
encodingId : encodingId,
spatialLayerId : spatialLayerId,
temporalLayerId : temporalLayerId,
layers : layers
});
}
/*
* Select the simulcast encoding layer
* @param {String} encoding Id - rid value of the simulcast encoding of the track
*/
selectEncoding(encodingId)
{
//If not changed
if (this.encodingId==encodingId)
//Do nothing
return;
//Get encoding
const encoding = this.track.encodings.get(encodingId);
//If not found
if (!encoding)
//Error
throw new Error("Encoding id ["+encodingId+"] not found on transpoder track");
//Start listening to it
this.transponder.SetIncoming(encoding.source,this.track.receiver);
//store encoding
this.encodingId = encodingId;
}
/**
* Return the encoding that is being forwarded
* @returns {String} encodingId
*/
getSelectedtEncoding()
{
// Return the encoding that is being forwarded
return this.encodingId;
}
/**
* Return the spatial layer id that is being forwarded
* @returns {Number} spatial layer id
*/
getSelectedSpatialLayerId()
{
// Return the spatial layer id that is being forwarded
return this.spatialLayerId;
}
/**
* Return the temporal layer id that is being forwarded
* @returns {Number} temporal layer id
*/
getSelectedTemporalLayerId()
{
// Return the temporal layer id that is being forwarded
return this.temporalLayerId;
}
/**
* Select SVC temporatl and spatial layers. Only available for VP9 media.
* @param {Number} spatialLayerId The spatial layer id to send to the outgoing stream
* @param {Number} temporalLayerId The temporaral layer id to send to the outgoing stream
*/
selectLayer(spatialLayerId,temporalLayerId)
{
//Limit with max layers allowed
if (this.maxSpatialLayerId)
spatialLayerId = Math.min(spatialLayerId,this.maxSpatialLayerId);
if (this.maxTemporalLayerId)
temporalLayerId = Math.min(temporalLayerId,this.maxTemporalLayerId);
//Check if not changed
if (this.spatialLayerId===spatialLayerId && this.temporalLayerId===temporalLayerId)
//Nothing
return;
//Call native interface
this.transponder.SelectLayer(spatialLayerId,temporalLayerId);
//Store new values
this.spatialLayerId = spatialLayerId;
this.temporalLayerId = temporalLayerId;
}
/**
* Set maximum statial and temporal layers to be forwrarded. Base layer is always enabled.
* @param {Number} maxSpatialLayerId - Max spatial layer id
* @param {Number} maxTemporalLayerId - Max temporal layer id
*/
setMaximumLayers(maxSpatialLayerId,maxTemporalLayerId)
{
//Check both are higher layers than the base layer
if (maxSpatialLayerId<0 || maxTemporalLayerId<0)
//Error
throw new Error("Maximum layers not allowed, base layer (0,0) must be always enabled");
//Store them
this.maxSpatialLayerId = maxSpatialLayerId;
this.maxTemporalLayerId = maxTemporalLayerId;
}
/**
* Add event listener
* @param {String} event - Event name
* @param {function} listener - Event listener
* @returns {IncomingStreamTrack}
*/
on()
{
//Delegate event listeners to event emitter
this.emitter.on.apply(this.emitter, arguments);
//Return object so it can be chained
return this;
}
/**
* Add event listener once
* @param {String} event - Event name
* @param {function} listener - Event listener
* @returns {IncomingStream}
*/
once()
{
//Delegate event listeners to event emitter
this.emitter.once.apply(this.emitter, arguments);
//Return object so it can be chained
return this;
}
/**
* Remove event listener
* @param {String} event - Event name
* @param {function} listener - Event listener
* @returns {IncomingStreamTrack}
*/
off()
{
//Delegate event listeners to event emitter
this.emitter.removeListener.apply(this.emitter, arguments);
//Return object so it can be chained
return this;
}
/**
* Stop this transponder, will dettach the OutgoingStreamTrack
*/
stop()
{
//Don't call it twice
if (!this.transponder) return;
//Remove incoming track
this.setIncomingTrack(null);
//Stop it
this.transponder.Close();
/**
* Transponder stopped event
*
* @name stopped
* @memberof Transponder
* @kind event
* @argument {Transponder} transponder
*/
this.emitter.emit("stopped",this);
//Remove transport reference, so destructor is called on GC
this.transponder = null;
//Remove track referecne also
this.track = null;
}
};
module.exports = Transponder;