/
streaming.controllers.BufferControllerSpec.js
555 lines (486 loc) · 20 KB
/
streaming.controllers.BufferControllerSpec.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
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
import ObjectUtils from '../../src/streaming/utils/ObjectUtils';
import BufferController from '../../src/streaming/controllers/BufferController';
import EventBus from '../../src/core/EventBus';
import Events from '../../src/core/events/Events';
import InitCache from '../../src/streaming/utils/InitCache';
import Debug from '../../src/core/Debug';
import Settings from '../../src/core/Settings';
import StreamControllerMock from './mocks/StreamControllerMock';
import PlaybackControllerMock from './mocks/PlaybackControllerMock';
import StreamProcessorMock from './mocks/StreamProcessorMock';
import DashMetricsMock from './mocks/DashMetricsMock';
import AdapterMock from './mocks/AdapterMock';
import MediaSourceMock from './mocks/MediaSourceMock';
import MediaPlayerModelMock from './mocks/MediaPlayerModelMock';
import ErrorHandlerMock from './mocks/ErrorHandlerMock';
import MediaControllerMock from './mocks/MediaControllerMock';
import AbrControllerMock from './mocks/AbrControllerMock';
import TextControllerMock from './mocks/TextControllerMock';
const chai = require('chai');
const expect = chai.expect;
const context = {};
const testType = 'video';
const streamInfo = {
id: 'id'
};
const eventBus = EventBus(context).getInstance();
const objectUtils = ObjectUtils(context).getInstance();
const initCache = InitCache(context).getInstance();
describe('BufferController', function () {
// disable log
let settings = Settings(context).getInstance();
const debug = Debug(context).getInstance();
debug.setLogToBrowserConsole(false);
const streamProcessor = new StreamProcessorMock(testType, streamInfo);
const streamControllerMock = new StreamControllerMock();
const adapterMock = new AdapterMock();
const dashMetricsMock = new DashMetricsMock();
const playbackControllerMock = new PlaybackControllerMock();
const mediaPlayerModelMock = new MediaPlayerModelMock();
const errorHandlerMock = new ErrorHandlerMock();
const mediaControllerMock = new MediaControllerMock();
const abrControllerMock = new AbrControllerMock();
const textControllerMock = new TextControllerMock();
let bufferController;
let mediaSourceMock;
const mediaInfo = { codec: 'video/webm; codecs="vp8, vorbis"' };
beforeEach(function () {
global.navigator = {
userAgent: 'node.js'
};
mediaSourceMock = new MediaSourceMock();
bufferController = BufferController(context).create({
dashMetrics: dashMetricsMock,
errHandler: errorHandlerMock,
streamController: streamControllerMock,
mediaController: mediaControllerMock,
adapter: adapterMock,
textController: textControllerMock,
abrController: abrControllerMock,
streamProcessor: streamProcessor,
type: testType,
playbackController: playbackControllerMock,
mediaPlayerModel: mediaPlayerModelMock,
settings: settings
});
});
afterEach(function () {
delete global.navigator;
bufferController.reset();
bufferController = null;
streamProcessor.reset();
});
describe('Method initialize', function () {
it('should initialize the controller', function () {
expect(bufferController.getType()).to.equal(testType);
bufferController.initialize({});
});
});
describe('Method createBuffer/getBuffer', function () {
it('should not create a preBufferSink if mediaInfo is undefined', function () {
expect(bufferController.createBuffer()).to.be.null; // jshint ignore:line
});
it('should create a preBufferSink if controller is initialized without a mediaSource', function () {
bufferController.initialize(null);
bufferController.createBuffer(mediaInfo);
expect(bufferController.getBuffer().discharge).to.be.a('function'); //Is of type PreBufferSink.
});
it('should create a sourceBufferSink and initialize it when given a mediaSource', function () {
bufferController.initialize(mediaSourceMock);
bufferController.createBuffer(mediaInfo);
const sink = bufferController.getBuffer();
expect(sink.getBuffer).to.be.a('function'); //Is of type SourceBufferSink
expect(sink.getBuffer()).to.equal(mediaSourceMock.buffers[0]);
});
});
describe('Method getStreamProcessor', function () {
it('should return configured stream processor', function () {
const configuredSP = bufferController.getStreamProcessor();
expect(objectUtils.areEqual(configuredSP, streamProcessor)).to.be.true; // jshint ignore:line
});
});
describe('Methods get/set Media Source', function () {
it('should update media source', function () {
bufferController.setMediaSource(mediaSourceMock);
expect(bufferController.getMediaSource()).to.equal(mediaSourceMock);
});
});
describe('Method switchInitData', function () {
beforeEach(function () {
bufferController.initialize(mediaSourceMock);
bufferController.createBuffer(mediaInfo);
});
it('should append init data to source buffer if data have been cached', function () {
const chunk = {
bytes: 'initData',
quality: 2,
mediaInfo: {
type: 'video'
},
streamId: 'streamId',
representationId: 'representationId'
};
initCache.save(chunk);
bufferController.switchInitData('streamId', 'representationId');
expect(mediaSourceMock.buffers[0].chunk).to.equal(chunk.bytes);
});
it('should trigger INIT_REQUESTED if no init data is cached', function (done) {
// reset cache
initCache.reset();
const onInitRequest = function () {
eventBus.off(Events.INIT_REQUESTED, onInitRequest);
done();
};
eventBus.on(Events.INIT_REQUESTED, onInitRequest, this);
bufferController.switchInitData('streamId', 'representationId');
});
});
describe('Method reset', function () {
it('should reset buffer controller', function () {
bufferController.initialize(mediaSourceMock);
bufferController.createBuffer(mediaInfo);
const buffer = mediaSourceMock.buffers[0];
expect(buffer).to.exist; // jshint ignore:line
bufferController.reset();
expect(buffer.aborted).to.be.true; // jshint ignore:line
expect(mediaSourceMock.buffers[0]).to.not.exist; // jshint ignore:line
expect(bufferController.getBuffer()).to.not.exist; // jshint ignore:line
});
});
describe('Event INIT_FRAGMENT_LOADED handler', function () {
beforeEach(function () {
bufferController.initialize(mediaSourceMock);
bufferController.createBuffer(mediaInfo);
});
it('should not append data to source buffer if wrong fragment model', function (done) {
const event = {
fragmentModel: 'wrongFragmentModel',
chunk: {
bytes: 'initData',
quality: 2,
mediaInfo: {
type: 'video'
},
streamId: 'streamId',
representationId: 'representationId'
}
};
const onInitDataLoaded = function () {
eventBus.off(Events.INIT_FRAGMENT_LOADED, onInitDataLoaded);
expect(mediaSourceMock.buffers[0].chunk).to.be.null; // jshint ignore:line
done();
};
eventBus.on(Events.INIT_FRAGMENT_LOADED, onInitDataLoaded, this);
expect(mediaSourceMock.buffers[0].chunk).to.be.null; // jshint ignore:line
// send event
eventBus.trigger(Events.INIT_FRAGMENT_LOADED, event);
});
it('should append data to source buffer ', function (done) {
const event = {
fragmentModel: streamProcessor.getFragmentModel(),
chunk: {
bytes: 'initData',
quality: 2,
mediaInfo: {
type: 'video'
},
streamId: 'streamId',
representationId: 'representationId'
}
};
const onInitDataLoaded = function () {
eventBus.off(Events.INIT_FRAGMENT_LOADED, onInitDataLoaded);
expect(mediaSourceMock.buffers[0].chunk).to.equal(event.chunk.bytes);
done();
};
eventBus.on(Events.INIT_FRAGMENT_LOADED, onInitDataLoaded, this);
expect(mediaSourceMock.buffers[0].chunk).to.be.null; // jshint ignore:line
// send event
eventBus.trigger(Events.INIT_FRAGMENT_LOADED, event);
});
it('should save init data into cache', function (done) {
const chunk = {
bytes: 'initData',
quality: 2,
mediaInfo: {
type: 'video'
},
streamId: 'streamId',
representationId: 'representationId'
};
const event = {
fragmentModel: streamProcessor.getFragmentModel(),
chunk: chunk
};
initCache.reset();
let cache = initCache.extract(chunk.streamId, chunk.representationId);
const onInitDataLoaded = function () {
eventBus.off(Events.INIT_FRAGMENT_LOADED, onInitDataLoaded);
// check initCache
cache = initCache.extract(chunk.streamId, chunk.representationId);
expect(cache.bytes).to.equal(chunk.bytes);
done();
};
eventBus.on(Events.INIT_FRAGMENT_LOADED, onInitDataLoaded, this);
expect(cache).to.not.exist; // jshint ignore:line
// send event
eventBus.trigger(Events.INIT_FRAGMENT_LOADED, event);
});
});
describe('Event MEDIA_FRAGMENT_LOADED handler', function () {
beforeEach(function () {
bufferController.initialize(mediaSourceMock);
bufferController.createBuffer(mediaInfo);
});
it('should not append data to source buffer if wrong fragment model', function (done) {
const event = {
fragmentModel: 'wrongFragmentModel',
chunk: {
bytes: 'data',
quality: 2,
mediaInfo: {
type: 'video'
},
streamId: 'streamId',
representationId: 'representationId'
}
};
const onMediaFragmentLoaded = function () {
eventBus.off(Events.MEDIA_FRAGMENT_LOADED, onMediaFragmentLoaded);
expect(mediaSourceMock.buffers[0].chunk).to.be.null; // jshint ignore:line
done();
};
eventBus.on(Events.MEDIA_FRAGMENT_LOADED, onMediaFragmentLoaded, this);
expect(mediaSourceMock.buffers[0].chunk).to.be.null; // jshint ignore:line
// send event
eventBus.trigger(Events.MEDIA_FRAGMENT_LOADED, event);
});
it('should append data to source buffer ', function (done) {
const event = {
fragmentModel: streamProcessor.getFragmentModel(),
chunk: {
bytes: 'data',
quality: 2,
mediaInfo: 'video'
}
};
const onMediaFragmentLoaded = function () {
eventBus.off(Events.MEDIA_FRAGMENT_LOADED, onMediaFragmentLoaded);
expect(mediaSourceMock.buffers[0].chunk).to.equal(event.chunk.bytes);
done();
};
eventBus.on(Events.MEDIA_FRAGMENT_LOADED, onMediaFragmentLoaded, this);
expect(mediaSourceMock.buffers[0].chunk).to.be.null; // jshint ignore:line
// send event
eventBus.trigger(Events.MEDIA_FRAGMENT_LOADED, event);
});
it('should trigger VIDEO_CHUNK_RECEIVED if event is video', function (done) {
const event = {
fragmentModel: streamProcessor.getFragmentModel(),
chunk: {
bytes: 'data',
quality: 2,
mediaInfo: {
type: 'video'
}
}
};
const onVideoChunk = function () {
eventBus.off(Events.VIDEO_CHUNK_RECEIVED, onVideoChunk);
done();
};
eventBus.on(Events.VIDEO_CHUNK_RECEIVED, onVideoChunk, this);
// send event
eventBus.trigger(Events.MEDIA_FRAGMENT_LOADED, event);
});
});
describe('Event MEDIA_FRAGMENT_LOADED handler', function () {
beforeEach(function () {
bufferController.initialize(mediaSourceMock);
bufferController.createBuffer(mediaInfo);
});
it('should not update buffer timestamp offset - wrong stream processor id', function () {
expect(mediaSourceMock.buffers[0].timestampOffset).to.equal(1);
const event = {
newQuality: 2,
mediaType: testType,
streamInfo: {
id: 'wrongid'
}
};
// send event
eventBus.trigger(Events.QUALITY_CHANGE_REQUESTED, event);
expect(mediaSourceMock.buffers[0].timestampOffset).to.equal(1);
});
it('should not update buffer timestamp offset - wrong media type', function () {
expect(mediaSourceMock.buffers[0].timestampOffset).to.equal(1);
const event = {
newQuality: 2,
mediaType: 'wrongMediaType',
streamInfo: {
id: streamProcessor.getStreamInfo().id
}
};
// send event
eventBus.trigger(Events.QUALITY_CHANGE_REQUESTED, event);
expect(mediaSourceMock.buffers[0].timestampOffset).to.equal(1);
});
it('should not update buffer timestamp offset - wrong quality', function () {
expect(mediaSourceMock.buffers[0].timestampOffset).to.equal(1);
const event = {
newQuality: 0,
mediaType: testType,
streamInfo: {
id: streamProcessor.getStreamInfo().id
}
};
// send event
eventBus.trigger(Events.QUALITY_CHANGE_REQUESTED, event);
expect(mediaSourceMock.buffers[0].timestampOffset).to.equal(1);
});
it('should update buffer timestamp offset', function () {
expect(mediaSourceMock.buffers[0].timestampOffset).to.equal(1);
const event = {
newQuality: 2,
mediaType: testType,
streamInfo: {
id: streamProcessor.getStreamInfo().id
}
};
// send event
eventBus.trigger(Events.QUALITY_CHANGE_REQUESTED, event);
expect(mediaSourceMock.buffers[0].timestampOffset).to.equal(2);
});
});
describe('Event PLAYBACK_SEEKING handler', function () {
beforeEach(function () {
bufferController.initialize(mediaSourceMock);
bufferController.createBuffer(mediaInfo);
});
it('should trigger BUFFER_LEVEL_UPDATED event', function (done) {
const buffer = mediaSourceMock.buffers[0];
const onBufferLevelUpdated = function (e) {
eventBus.off(Events.BUFFER_LEVEL_UPDATED, onBufferLevelUpdated, this);
expect(e.bufferLevel).to.equal(buffer.buffered.end(0) - buffer.buffered.start(0));
done();
};
eventBus.on(Events.BUFFER_LEVEL_UPDATED, onBufferLevelUpdated, this);
// send event
eventBus.trigger(Events.PLAYBACK_SEEKING);
});
/*
it('should trigger BUFFER_LEVEL_STATE_CHANGED event', function (done) {
const buffer = mediaSourceMock.buffers[0];
const onBufferStateChanged = function (e) {
eventBus.off(Events.BUFFER_LEVEL_STATE_CHANGED, onBufferStateChanged, this);
expect(e.state).to.equal('bufferLoaded');
done();
};
eventBus.on(Events.BUFFER_LEVEL_STATE_CHANGED, onBufferStateChanged, this);
// send event
eventBus.trigger(Events.PLAYBACK_SEEKING);
});
it('should trigger BUFFER_LOADED event if enough buffer', function (done) {
const buffer = mediaSourceMock.buffers[0];
const onBufferLoaded = function () {
eventBus.off(Events.BUFFER_LOADED, onBufferLoaded, this);
done();
};
eventBus.on(Events.BUFFER_LOADED, onBufferLoaded, this);
// send event
eventBus.trigger(Events.PLAYBACK_SEEKING);
});*/
});
describe('Method getBufferRange', function () {
let buffer;
beforeEach(function () {
let mediaInfo = {
codec: 'video/webm; codecs="vp8, vorbis"'
};
bufferController.initialize(mediaSourceMock);
bufferController.createBuffer(mediaInfo);
const sink = bufferController.getBuffer();
buffer = sink.getBuffer();
expect(mediaSourceMock.buffers).to.have.lengthOf(1);
});
it('should return range of buffered data', function () {
buffer.addRange({
start: 2,
end: 5
});
buffer.addRange({
start: 8,
end: 9
});
buffer.addRange({
start: 9,
end: 11
});
let range = bufferController.getRangeAt(10);
expect(range.start).to.equal(9);
expect(range.end).to.equal(11);
});
it('should return range of buffered data - small discontinuity', function () {
buffer.addRange({
start: 2,
end: 5
});
buffer.addRange({
start: 8,
end: 9
});
buffer.addRange({
start: 9,
end: 10.05
});
buffer.addRange({
start: 10.1,
end: 11
});
let range = bufferController.getRangeAt(10);
expect(range.start).to.equal(9);
expect(range.end).to.equal(11);
});
it('should return null - time not in range', function () {
buffer.addRange({
start: 2,
end: 5
});
buffer.addRange({
start: 8,
end: 9
});
buffer.addRange({
start: 9,
end: 9.5
});
buffer.addRange({
start: 10.5,
end: 11
});
let range = bufferController.getRangeAt(10);
expect(range).to.be.null; // jshint ignore:line
});
it('should return range of buffered data - time not in range (little gap)', function () {
buffer.addRange({
start: 2,
end: 5
});
buffer.addRange({
start: 8,
end: 9
});
buffer.addRange({
start: 9,
end: 9.9
});
buffer.addRange({
start: 10.1,
end: 11
});
let range = bufferController.getRangeAt(10);
expect(range.start).to.equal(10.1);
expect(range.end).to.equal(11);
});
});
});