/
streaming_engine_integration.js
604 lines (509 loc) · 19 KB
/
streaming_engine_integration.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
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
/*! @license
* Shaka Player
* Copyright 2016 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
describe('StreamingEngine', () => {
const ContentType = shaka.util.ManifestParserUtils.ContentType;
const Util = shaka.test.Util;
let metadata;
let generators;
/** @type {!shaka.util.EventManager} */
let eventManager;
/** @type {shaka.test.Waiter} */
let waiter;
/** @type {!HTMLVideoElement} */
let video;
/** @type {{start: number, end: number}} */
let segmentAvailability;
/** @type {!shaka.test.FakePresentationTimeline} */
let timeline;
/** @type {!shaka.media.Playhead} */
let playhead;
/** @type {shaka.extern.StreamingConfiguration} */
let config;
/** @type {!shaka.test.FakeNetworkingEngine} */
let netEngine;
/** @type {!shaka.media.MediaSourceEngine} */
let mediaSourceEngine;
/** @type {!shaka.media.StreamingEngine} */
let streamingEngine;
/** @type {shaka.extern.Variant} */
let variant;
/** @type {shaka.extern.Manifest} */
let manifest;
/** @type {!jasmine.Spy} */
let onError;
/** @type {!jasmine.Spy} */
let onEvent;
beforeAll(() => {
video = shaka.test.UiUtils.createVideoElement();
document.body.appendChild(video);
metadata = shaka.test.TestScheme.DATA['sintel'];
generators = {};
});
beforeEach(() => {
config = shaka.util.PlayerConfiguration.createDefault().streaming;
onError = jasmine.createSpy('onError');
onError.and.callFake(fail);
onEvent = jasmine.createSpy('onEvent');
eventManager = new shaka.util.EventManager();
waiter = new shaka.test.Waiter(eventManager);
mediaSourceEngine = new shaka.media.MediaSourceEngine(
video,
new shaka.test.FakeClosedCaptionParser(),
new shaka.test.FakeTextDisplayer());
waiter.setMediaSourceEngine(mediaSourceEngine);
});
afterEach(async () => {
eventManager.release();
await streamingEngine.destroy();
await mediaSourceEngine.destroy();
playhead.release();
});
afterAll(() => {
document.body.removeChild(video);
});
async function setupVod() {
await createVodStreamGenerator(metadata.audio, ContentType.AUDIO);
await createVodStreamGenerator(metadata.video, ContentType.VIDEO);
segmentAvailability = {
start: 0,
end: 60,
};
timeline = shaka.test.StreamingEngineUtil.createFakePresentationTimeline(
segmentAvailability,
/* presentationDuration= */ 60,
/* maxSegmentDuration= */ metadata.video.segmentDuration,
/* isLive= */ false);
setupNetworkingEngine(
/* presentationDuration= */ 60,
{
audio: metadata.audio.segmentDuration,
video: metadata.video.segmentDuration,
});
setupManifest(
/* firstPeriodStartTime= */ 0,
/* secondPeriodStartTime= */ 30,
/* presentationDuration= */ 60);
setupPlayhead();
createStreamingEngine();
}
async function setupLive() {
await createLiveStreamGenerator(
metadata.audio,
ContentType.AUDIO,
/* timeShiftBufferDepth= */ 20);
await createLiveStreamGenerator(
metadata.video,
ContentType.VIDEO,
/* timeShiftBufferDepth= */ 20);
// The generator's AST is set to 295 seconds in the past, so the live-edge
// is at 295 - 10 seconds.
// -10 to account for maxSegmentDuration.
segmentAvailability = {
start: 275 - 10,
end: 295 - 10,
};
timeline = shaka.test.StreamingEngineUtil.createFakePresentationTimeline(
segmentAvailability,
/* presentationDuration= */ Infinity,
/* maxSegmentDuration= */ metadata.video.segmentDuration,
/* isLive= */ true);
setupNetworkingEngine(
/* presentationDuration= */ Infinity,
{
audio: metadata.audio.segmentDuration,
video: metadata.video.segmentDuration,
});
setupManifest(
/* firstPeriodStartTime= */ 0,
/* secondPeriodStartTime= */ 300,
/* presentationDuration= */ Infinity);
setupPlayhead();
createStreamingEngine();
}
function createVodStreamGenerator(metadata, type) {
const generator = new shaka.test.Mp4VodStreamGenerator(
metadata.initSegmentUri,
metadata.mdhdOffset,
metadata.segmentUri,
metadata.tfdtOffset,
metadata.segmentDuration);
generators[type] = generator;
return generator.init();
}
function createLiveStreamGenerator(metadata, type, timeShiftBufferDepth) {
// Set the generator's AST to 295 seconds in the past so the
// StreamingEngine begins streaming close to the end of the first Period.
const now = Date.now() / 1000;
const generator = new shaka.test.Mp4LiveStreamGenerator(
metadata.initSegmentUri,
metadata.mdhdOffset,
metadata.segmentUri,
metadata.tfdtOffset,
metadata.segmentDuration,
/* broadcastStartTime= */ now - 295,
/* availabilityStartTime= */ now - 295,
timeShiftBufferDepth);
generators[type] = generator;
return generator.init();
}
function setupNetworkingEngine(presentationDuration, segmentDurations) {
// Create the fake NetworkingEngine. Note: the StreamingEngine should never
// request a segment that does not exist.
netEngine = shaka.test.StreamingEngineUtil.createFakeNetworkingEngine(
// Init segment generator:
(type, periodNumber) => {
const wallClockTime = Date.now() / 1000;
const segment = generators[type].getInitSegment(wallClockTime);
expect(segment).not.toBeNull();
return segment;
},
// Media segment generator:
(type, periodNumber, position) => {
const wallClockTime = Date.now() / 1000;
const segment = generators[type].getSegment(position, wallClockTime);
return segment;
},
/* delays= */{audio: 0, video: 0, text: 0});
}
function setupPlayhead() {
const onSeek = () => {
streamingEngine.seeked();
};
playhead = new shaka.media.MediaSourcePlayhead(
/** @type {!HTMLVideoElement} */(video),
manifest,
config,
/* startTime= */ null,
onSeek);
}
function setupManifest(
firstPeriodStartTime, secondPeriodStartTime, presentationDuration) {
manifest = shaka.test.StreamingEngineUtil.createManifest(
/** @type {!shaka.media.PresentationTimeline} */(timeline),
[firstPeriodStartTime, secondPeriodStartTime], presentationDuration,
/* segmentDurations= */ {
audio: metadata.audio.segmentDuration,
video: metadata.video.segmentDuration,
},
/* initSegmentRanges= */ {
audio: [0, null],
video: [0, null],
});
variant = manifest.variants[0];
}
function createStreamingEngine() {
const playerInterface = {
modifySegmentRequest: (request, segmentInfo) => {},
getPresentationTime: () => playhead.getTime(),
getBandwidthEstimate: () => 1e6,
mediaSourceEngine: mediaSourceEngine,
netEngine: /** @type {!shaka.net.NetworkingEngine} */(netEngine),
onError: Util.spyFunc(onError),
onEvent: Util.spyFunc(onEvent),
onManifestUpdate: () => {},
onSegmentAppended: () => playhead.notifyOfBufferingChange(),
onInitSegmentAppended: () => {},
};
streamingEngine = new shaka.media.StreamingEngine(
/** @type {shaka.extern.Manifest} */(manifest), playerInterface);
streamingEngine.configure(config);
}
describe('VOD', () => {
beforeEach(async () => {
await setupVod();
});
it('plays', async () => {
// Let's go!
streamingEngine.switchVariant(variant);
await streamingEngine.start();
video.play();
// The overall test timeout is 120 seconds, and the content is 60
// seconds. It should be possible to complete this test in 100 seconds,
// and if not, we want the error thrown to be within the overall test's
// timeout window.
await waiter.timeoutAfter(100).waitForEnd(video);
});
it('plays at high playback rates', async () => {
// Experimentally, we find that playback rates above 2x in this test seem
// to cause decoder failures on Tizen 3. This is out of our control, and
// seems to be a Tizen bug, so this test is skipped on Tizen completely.
if (shaka.util.Platform.isTizen()) {
pending('High playbackRate tests cause decoder errors on Tizen 3.');
}
// Let's go!
streamingEngine.switchVariant(variant);
await streamingEngine.start();
video.play();
// Wait for playback to begin before increasing the playback rate. This
// improves test reliability on slow platforms like Chromecast.
await waiter.timeoutAfter(10).waitForMovement(video);
video.playbackRate = 10;
await waiter.timeoutAfter(30).waitForEnd(video);
});
it('can handle buffered seeks', async () => {
// Let's go!
streamingEngine.switchVariant(variant);
await streamingEngine.start();
video.play();
// After 35 seconds seek back 10 seconds into the first Period.
await waiter.timeoutAfter(80).waitUntilPlayheadReaches(video, 35);
video.currentTime = 25;
await waiter.timeoutAfter(80).waitForEnd(video);
});
it('can handle unbuffered seeks', async () => {
// Let's go!
streamingEngine.switchVariant(variant);
await streamingEngine.start();
video.play();
await waiter.timeoutAfter(60).waitUntilPlayheadReaches(video, 20);
video.currentTime = 40;
await waiter.timeoutAfter(60).waitForEnd(video);
});
});
describe('Live', () => {
/** @type {number} */
let slideSegmentAvailabilityWindow;
beforeEach(async () => {
await setupLive();
slideSegmentAvailabilityWindow = window.setInterval(() => {
segmentAvailability.start++;
segmentAvailability.end++;
}, 1000);
});
afterEach(() => {
window.clearInterval(slideSegmentAvailabilityWindow);
});
it('plays through Period transition', async () => {
// Let's go!
streamingEngine.switchVariant(variant);
await streamingEngine.start();
video.play();
await waiter.timeoutAfter(60).waitUntilPlayheadReaches(video, 305);
const segmentType = shaka.net.NetworkingEngine.RequestType.SEGMENT;
// firstSegmentNumber =
// [(segmentAvailabilityEnd - rebufferingGoal) / segmentDuration] + 1
netEngine.expectRequest('0_video_29', segmentType);
netEngine.expectRequest('0_audio_29', segmentType);
});
it('can handle seeks ahead of availability window', async () => {
// Let's go!
streamingEngine.switchVariant(variant);
await streamingEngine.start();
await waiter.timeoutAfter(5).waitForEvent(video, 'loadeddata');
// Seek outside the availability window right away. The playhead
// should adjust the video's current time.
video.currentTime = segmentAvailability.end + 120;
video.play();
// Wait until the repositioning is complete so we don't
// immediately hit this case.
await shaka.test.Util.delay(/* seconds= */ 1);
await waiter.timeoutAfter(60).waitUntilPlayheadReaches(video, 305);
});
it('can handle seeks behind availability window', async () => {
let seekCount = 0;
eventManager.listen(video, 'seeking', () => {
seekCount++;
});
// Let's go!
streamingEngine.switchVariant(variant);
await streamingEngine.start();
await waiter.timeoutAfter(5).waitForEvent(video, 'loadeddata');
// Seek outside the availability window right away. The playhead
// should adjust the video's current time.
video.currentTime = segmentAvailability.start - 120;
expect(video.currentTime).toBeGreaterThan(0);
video.play();
await waiter.timeoutAfter(60).waitUntilPlayheadReaches(video, 305);
// We are playing close to the beginning of the availability window.
// We should be playing smoothly and not seeking repeatedly as we fall
// outside the window.
//
// Expected seeks:
// 1. seek to live stream start time during startup
// 2. explicit seek in the test to get outside the window
// 3. Playhead seeks to force us back inside the window
// 4. (maybe) seek if there is a gap at the period boundary
// 5. (maybe) seek to flush a pipeline stall
// 6. (maybe) on slower platforms (e.g. GitHub actions)
expect(seekCount).toBeGreaterThan(2);
expect(seekCount).toBeLessThan(7);
});
});
// This tests gaps created by missing segments.
// TODO: Consider also adding tests for missing frames.
describe('gap jumping', () => {
it('jumps small gaps at the beginning', async () => {
await setupGappyContent(/* gapAtStart= */ 1, /* dropSegment= */ false);
// Let's go!
streamingEngine.switchVariant(variant);
await streamingEngine.start();
video.play();
await waiter.timeoutAfter(5).waitUntilPlayheadReaches(video, 0.01);
expect(video.buffered.length).toBeGreaterThan(0);
expect(video.buffered.start(0)).toBeCloseTo(1);
await waiter.timeoutAfter(20).waitUntilPlayheadReaches(video, 5);
});
it('jumps large gaps at the beginning', async () => {
await setupGappyContent(/* gapAtStart= */ 5, /* dropSegment= */ false);
// Let's go!
streamingEngine.switchVariant(variant);
await streamingEngine.start();
video.play();
await waiter.timeoutAfter(5).waitUntilPlayheadReaches(video, 0.01);
expect(video.buffered.length).toBeGreaterThan(0);
expect(video.buffered.start(0)).toBeCloseTo(5);
await waiter.timeoutAfter(20).waitUntilPlayheadReaches(video, 8);
});
it('jumps small gaps in the middle', async () => {
await setupGappyContent(/* gapAtStart= */ 0, /* dropSegment= */ true);
// Let's go!
streamingEngine.switchVariant(variant);
await streamingEngine.start();
await waiter.timeoutAfter(5).waitForEvent(video, 'loadeddata');
video.currentTime = 8;
video.play();
await waiter.timeoutAfter(60).waitUntilPlayheadReaches(video, 23);
// Should be close enough to still have the gap buffered.
expect(video.buffered.length).toBe(2);
});
it('jumps large gaps in the middle', async () => {
await setupGappyContent(/* gapAtStart= */ 0, /* dropSegment= */ true);
// Let's go!
streamingEngine.switchVariant(variant);
await streamingEngine.start();
await waiter.timeoutAfter(5).waitForEvent(video, 'loadeddata');
video.currentTime = 8;
video.play();
await waiter.timeoutAfter(60).waitUntilPlayheadReaches(video, 23);
// Should be close enough to still have the gap buffered.
expect(video.buffered.length).toBe(2);
});
/**
* @param {number} gapAtStart The gap to introduce before start, in seconds.
* @param {boolean} dropSegment Whether to drop a segment in the middle.
* @return {!Promise}
*/
async function setupGappyContent(gapAtStart, dropSegment) {
// This uses "normal" stream generators and networking engine. The only
// difference is the segments are removed from the manifest. The segments
// should not be downloaded.
await createVodStreamGenerator(metadata.audio, ContentType.AUDIO);
await createVodStreamGenerator(metadata.video, ContentType.VIDEO);
segmentAvailability = {
start: 0,
end: 30,
};
timeline =
shaka.test.StreamingEngineUtil.createFakePresentationTimeline(
segmentAvailability,
/* presentationDuration= */ 30,
/* maxSegmentDuration= */ metadata.video.segmentDuration,
/* isLive= */ false);
setupNetworkingEngine(
/* presentationDuration= */ 30,
{
audio: metadata.audio.segmentDuration,
video: metadata.video.segmentDuration,
});
manifest = setupGappyManifest(gapAtStart, dropSegment);
variant = manifest.variants[0];
setupPlayhead();
createStreamingEngine();
}
/**
* TODO: Consolidate with StreamingEngineUtil.createManifest?
* @param {number} gapAtStart
* @param {boolean} dropSegment
* @return {shaka.extern.Manifest}
*/
function setupGappyManifest(gapAtStart, dropSegment) {
/**
* @param {string} type
* @param {shaka.media.InitSegmentReference} initSegmentReference
* @return {!shaka.media.SegmentIndex}
*/
function createIndex(type, initSegmentReference) {
const d = metadata[type].segmentDuration;
const refs = [];
let i = 0;
let time = gapAtStart;
while (time < 30) {
let end = time + d;
// Make segment 0 longer to make the manifest continuous, despite the
// dropped segment.
if (i == 0 && dropSegment) {
end += d;
}
let cur = i;
const getUris = () => {
// The times in the media are based on the URL; so to drop a
// segment, we change the URL.
if (cur >= 1 && dropSegment) {
cur++;
}
return ['0_' + type + '_' + cur];
};
refs.push(new shaka.media.SegmentReference(
/* startTime= */ time,
/* endTime= */ end,
getUris,
/* startByte= */ 0,
/* endByte= */ null,
initSegmentReference,
/* timestampOffset= */ gapAtStart,
/* appendWindowStart= */ 0,
/* appendWindowEnd= */ Infinity));
i++;
time = end;
}
return new shaka.media.SegmentIndex(refs);
}
function createInit(type) {
const getUris = () => {
return ['0_' + type + '_init'];
};
return new shaka.media.InitSegmentReference(getUris, 0, null);
}
const videoInit = createInit('video');
const videoIndex = createIndex('video', videoInit);
const audioInit = createInit('audio');
const audioIndex = createIndex('audio', audioInit);
return {
presentationTimeline: timeline,
offlineSessionIds: [],
minBufferTime: 2,
textStreams: [],
imageStreams: [],
sequenceMode: false,
variants: [{
id: 1,
video: {
id: 2,
createSegmentIndex: () => Promise.resolve(),
segmentIndex: videoIndex,
mimeType: 'video/mp4',
codecs: 'avc1.42c01e',
bandwidth: 5000000,
width: 600,
height: 400,
type: shaka.util.ManifestParserUtils.ContentType.VIDEO,
drmInfos: [],
},
audio: {
id: 3,
createSegmentIndex: () => Promise.resolve(),
segmentIndex: audioIndex,
mimeType: 'audio/mp4',
codecs: 'mp4a.40.2',
bandwidth: 192000,
type: shaka.util.ManifestParserUtils.ContentType.AUDIO,
drmInfos: [],
},
}],
};
}
});
});