/
media_source_engine_integration.js
391 lines (346 loc) · 15 KB
/
media_source_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
/*! @license
* Shaka Player
* Copyright 2016 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
goog.require('shaka.media.ClosedCaptionParser');
goog.require('shaka.media.MediaSourceEngine');
goog.require('shaka.test.FakeTextDisplayer');
goog.require('shaka.test.TestScheme');
goog.require('shaka.test.UiUtils');
goog.require('shaka.util.ManifestParserUtils');
describe('MediaSourceEngine', () => {
const ContentType = shaka.util.ManifestParserUtils.ContentType;
const presentationDuration = 840;
/** @type {!HTMLVideoElement} */
let video;
/** @type {!MediaSource} */
let mediaSource;
/** @type {!shaka.media.MediaSourceEngine} */
let mediaSourceEngine;
let generators;
let metadata;
// TODO: add text streams to MSE integration tests
/**
* We use a fake text displayer so that we can check if CEA text is being
* passed through the system correctly.
*
* @type {!shaka.test.FakeTextDisplayer}
*/
let textDisplayer;
beforeAll(() => {
video = shaka.test.UiUtils.createVideoElement();
document.body.appendChild(video);
});
beforeEach(async () => {
metadata = shaka.test.TestScheme.DATA['sintel'];
generators = shaka.test.TestScheme.GENERATORS['sintel'];
textDisplayer = new shaka.test.FakeTextDisplayer();
mediaSourceEngine = new shaka.media.MediaSourceEngine(
video,
new shaka.media.ClosedCaptionParser(),
textDisplayer);
mediaSource = /** @type {?} */(mediaSourceEngine)['mediaSource_'];
expect(video.src).toBeTruthy();
await mediaSourceEngine.init(new Map(), false);
});
afterEach(async () => {
await mediaSourceEngine.destroy();
});
afterAll(() => {
document.body.removeChild(video);
});
function appendInit(type) {
const segment = generators[type].getInitSegment(Date.now() / 1000);
return mediaSourceEngine.appendBuffer(
type, segment, null, null, /* hasClosedCaptions= */ false);
}
function append(type, segmentNumber) {
const segment = generators[type]
.getSegment(segmentNumber, Date.now() / 1000);
return mediaSourceEngine.appendBuffer(
type, segment, null, null, /* hasClosedCaptions= */ false);
}
// The start time and end time should be null for init segment with closed
// captions.
function appendInitWithClosedCaptions(type) {
const segment = generators[type].getInitSegment(Date.now() / 1000);
return mediaSourceEngine.appendBuffer(type, segment, /* startTime= */ null,
/* endTime= */ null, /* hasClosedCaptions= */ true);
}
// The start time and end time should be valid for the segments with closed
// captions.
function appendWithClosedCaptions(type, segmentNumber) {
const segment = generators[type]
.getSegment(segmentNumber, Date.now() / 1000);
return mediaSourceEngine.appendBuffer(type, segment, /* startTime= */ 0,
/* endTime= */ 2, /* hasClosedCaptions= */ true);
}
function buffered(type, time) {
return mediaSourceEngine.bufferedAheadOf(type, time);
}
function bufferStart(type) {
return mediaSourceEngine.bufferStart(type);
}
function remove(type, segmentNumber) {
const start = segmentNumber * metadata[type].segmentDuration;
const end = (segmentNumber + 1) * metadata[type].segmentDuration;
return mediaSourceEngine.remove(type, start, end);
}
function getFakeStream(streamMetadata) {
return {
mimeType: streamMetadata.mimeType,
codecs: streamMetadata.codecs,
};
}
it('buffers MP4 video', async () => {
const initObject = new Map();
initObject.set(ContentType.VIDEO, getFakeStream(metadata.video));
await mediaSourceEngine.init(initObject, false);
await mediaSourceEngine.setDuration(presentationDuration);
await appendInit(ContentType.VIDEO);
expect(buffered(ContentType.VIDEO, 0)).toBe(0);
await append(ContentType.VIDEO, 0);
expect(buffered(ContentType.VIDEO, 0)).toBeCloseTo(10);
await append(ContentType.VIDEO, 1);
expect(buffered(ContentType.VIDEO, 0)).toBeCloseTo(20);
await append(ContentType.VIDEO, 2);
expect(buffered(ContentType.VIDEO, 0)).toBeCloseTo(30);
});
it('removes segments', async () => {
const initObject = new Map();
initObject.set(ContentType.VIDEO, getFakeStream(metadata.video));
await mediaSourceEngine.init(initObject, false);
await mediaSourceEngine.setDuration(presentationDuration);
await appendInit(ContentType.VIDEO);
await Promise.all([
append(ContentType.VIDEO, 0),
append(ContentType.VIDEO, 1),
append(ContentType.VIDEO, 2),
]);
expect(buffered(ContentType.VIDEO, 0)).toBeCloseTo(30);
await remove(ContentType.VIDEO, 0);
expect(bufferStart(ContentType.VIDEO)).toBeCloseTo(10);
expect(buffered(ContentType.VIDEO, 10)).toBeCloseTo(20);
await remove(ContentType.VIDEO, 1);
expect(bufferStart(ContentType.VIDEO)).toBe(20);
expect(buffered(ContentType.VIDEO, 20)).toBeCloseTo(10);
await remove(ContentType.VIDEO, 2);
expect(bufferStart(ContentType.VIDEO)).toBe(null);
});
it('extends the duration', async () => {
const initObject = new Map();
initObject.set(ContentType.VIDEO, getFakeStream(metadata.video));
await mediaSourceEngine.init(initObject, false);
await mediaSourceEngine.setDuration(0);
await appendInit(ContentType.VIDEO);
await mediaSourceEngine.setDuration(20);
expect(mediaSource.duration).toBeCloseTo(20);
await append(ContentType.VIDEO, 0);
expect(mediaSource.duration).toBeCloseTo(20);
await mediaSourceEngine.setDuration(35);
expect(mediaSource.duration).toBeCloseTo(35);
await Promise.all([
append(ContentType.VIDEO, 1),
append(ContentType.VIDEO, 2),
append(ContentType.VIDEO, 3),
]);
expect(mediaSource.duration).toBeCloseTo(40);
await mediaSourceEngine.setDuration(60);
expect(mediaSource.duration).toBeCloseTo(60);
});
it('ends the stream, truncating the duration', async () => {
const initObject = new Map();
initObject.set(ContentType.VIDEO, getFakeStream(metadata.video));
await mediaSourceEngine.init(initObject, false);
await mediaSourceEngine.setDuration(presentationDuration);
await appendInit(ContentType.VIDEO);
await append(ContentType.VIDEO, 0);
await append(ContentType.VIDEO, 1);
await append(ContentType.VIDEO, 2);
await mediaSourceEngine.endOfStream();
expect(mediaSource.duration).toBeCloseTo(30);
});
it('does not throw if endOfStrem called more than once', async () => {
const initObject = new Map();
initObject.set(ContentType.VIDEO, getFakeStream(metadata.video));
await mediaSourceEngine.init(initObject, false);
await mediaSourceEngine.setDuration(presentationDuration);
await appendInit(ContentType.VIDEO);
await append(ContentType.VIDEO, 0);
// Call endOfStream twice. There should be no exception.
await mediaSourceEngine.endOfStream();
await mediaSourceEngine.endOfStream();
});
it('queues operations', async () => {
/** @type {!Array.<number>} */
const resolutionOrder = [];
/** @type {!Array.<!Promise>} */
const requests = [];
function checkOrder(p) {
const nextIndex = requests.length;
requests.push(p.then(() => {
resolutionOrder.push(nextIndex);
}));
}
const initObject = new Map();
initObject.set(ContentType.VIDEO, getFakeStream(metadata.video));
await mediaSourceEngine.init(initObject, false);
checkOrder(mediaSourceEngine.setDuration(presentationDuration));
checkOrder(appendInit(ContentType.VIDEO));
checkOrder(append(ContentType.VIDEO, 0));
checkOrder(append(ContentType.VIDEO, 1));
checkOrder(append(ContentType.VIDEO, 2));
checkOrder(mediaSourceEngine.endOfStream());
await Promise.all(requests);
expect(resolutionOrder).toEqual([0, 1, 2, 3, 4, 5]);
});
it('buffers MP4 audio', async () => {
const initObject = new Map();
initObject.set(ContentType.AUDIO, getFakeStream(metadata.audio));
await mediaSourceEngine.init(initObject, false);
await mediaSourceEngine.setDuration(presentationDuration);
// NOTE: For some reason, this appendInit never resolves on my Windows VM.
// The test operates correctly on real hardware.
await appendInit(ContentType.AUDIO);
expect(buffered(ContentType.AUDIO, 0)).toBe(0);
await append(ContentType.AUDIO, 0);
expect(buffered(ContentType.AUDIO, 0)).toBeCloseTo(10, 1);
await append(ContentType.AUDIO, 1);
expect(buffered(ContentType.AUDIO, 0)).toBeCloseTo(20, 1);
await append(ContentType.AUDIO, 2);
expect(buffered(ContentType.AUDIO, 0)).toBeCloseTo(30, 1);
});
it('buffers MP4 video and audio', async () => {
const initObject = new Map();
initObject.set(ContentType.AUDIO, getFakeStream(metadata.audio));
initObject.set(ContentType.VIDEO, getFakeStream(metadata.video));
await mediaSourceEngine.init(initObject, false);
await mediaSourceEngine.setDuration(presentationDuration);
const audioStreaming = async () => {
await appendInit(ContentType.AUDIO);
await append(ContentType.AUDIO, 0);
expect(buffered(ContentType.AUDIO, 0)).toBeCloseTo(10, 1);
await append(ContentType.AUDIO, 1);
expect(buffered(ContentType.AUDIO, 0)).toBeCloseTo(20, 1);
await append(ContentType.AUDIO, 2);
expect(buffered(ContentType.AUDIO, 0)).toBeCloseTo(30, 1);
await append(ContentType.AUDIO, 3);
expect(buffered(ContentType.AUDIO, 0)).toBeCloseTo(40, 1);
await append(ContentType.AUDIO, 4);
expect(buffered(ContentType.AUDIO, 0)).toBeCloseTo(50, 1);
await append(ContentType.AUDIO, 5);
expect(buffered(ContentType.AUDIO, 0)).toBeCloseTo(60, 1);
};
const videoStreaming = async () => {
await appendInit(ContentType.VIDEO);
await append(ContentType.VIDEO, 0);
expect(buffered(ContentType.VIDEO, 0)).toBeCloseTo(10);
await append(ContentType.VIDEO, 1);
expect(buffered(ContentType.VIDEO, 0)).toBeCloseTo(20);
await append(ContentType.VIDEO, 2);
expect(buffered(ContentType.VIDEO, 0)).toBeCloseTo(30);
await append(ContentType.VIDEO, 3);
expect(buffered(ContentType.VIDEO, 0)).toBeCloseTo(40);
await append(ContentType.VIDEO, 4);
expect(buffered(ContentType.VIDEO, 0)).toBeCloseTo(50);
await append(ContentType.VIDEO, 5);
expect(buffered(ContentType.VIDEO, 0)).toBeCloseTo(60);
};
await Promise.all([audioStreaming(), videoStreaming()]);
await mediaSourceEngine.endOfStream();
expect(mediaSource.duration).toBeCloseTo(60, 1);
});
it('trims content at the append window', async () => {
const initObject = new Map();
initObject.set(ContentType.VIDEO, getFakeStream(metadata.video));
await mediaSourceEngine.init(initObject, false);
await mediaSourceEngine.setDuration(presentationDuration);
await appendInit(ContentType.VIDEO);
await mediaSourceEngine.setStreamProperties(ContentType.VIDEO,
/* timestampOffset= */ 0,
/* appendWindowStart= */ 5,
/* appendWindowEnd= */ 18,
/* sequenceMode= */ false);
expect(buffered(ContentType.VIDEO, 0)).toBe(0);
await append(ContentType.VIDEO, 0);
expect(bufferStart(ContentType.VIDEO)).toBeCloseTo(5, 1);
expect(buffered(ContentType.VIDEO, 5)).toBeCloseTo(5, 1);
await append(ContentType.VIDEO, 1);
expect(buffered(ContentType.VIDEO, 5)).toBeCloseTo(13, 1);
});
it('does not initialize timestamp offset in sequence mode', async () => {
const initObject = new Map();
initObject.set(ContentType.VIDEO, getFakeStream(metadata.video));
await mediaSourceEngine.init(initObject, false);
await mediaSourceEngine.setDuration(presentationDuration);
await appendInit(ContentType.VIDEO);
await mediaSourceEngine.setStreamProperties(ContentType.VIDEO,
/* timestampOffset= */ 100,
/* appendWindowStart= */ 5,
/* appendWindowEnd= */ 18,
/* sequenceMode= */ true);
expect(buffered(ContentType.VIDEO, 0)).toBe(0);
await append(ContentType.VIDEO, 0);
expect(bufferStart(ContentType.VIDEO)).toBeCloseTo(5, 1);
expect(buffered(ContentType.VIDEO, 5)).toBeCloseTo(5, 1);
await append(ContentType.VIDEO, 1);
expect(buffered(ContentType.VIDEO, 5)).toBeCloseTo(13, 1);
});
it('does not remove when overlap is outside append window', async () => {
const initObject = new Map();
initObject.set(ContentType.VIDEO, getFakeStream(metadata.video));
await mediaSourceEngine.init(initObject, false);
await mediaSourceEngine.setDuration(presentationDuration);
await appendInit(ContentType.VIDEO);
// Simulate period 1, with 20 seconds of content, no timestamp offset
await mediaSourceEngine.setStreamProperties(ContentType.VIDEO,
/* timestampOffset= */ 0,
/* appendWindowStart= */ 0,
/* appendWindowEnd= */ 20,
/* sequenceMode= */ false);
await append(ContentType.VIDEO, 0);
await append(ContentType.VIDEO, 1);
expect(bufferStart(ContentType.VIDEO)).toBeCloseTo(0, 1);
expect(buffered(ContentType.VIDEO, 0)).toBeCloseTo(20, 1);
// Simulate period 2, with 20 seconds of content offset back by 5 seconds.
// The 5 seconds of overlap should be trimmed off, and we should still
// have a continuous stream with 35 seconds of content.
await mediaSourceEngine.setStreamProperties(ContentType.VIDEO,
/* timestampOffset= */ 15,
/* appendWindowStart= */ 20,
/* appendWindowEnd= */ 35,
/* sequenceMode= */ false);
await append(ContentType.VIDEO, 0);
await append(ContentType.VIDEO, 1);
expect(bufferStart(ContentType.VIDEO)).toBeCloseTo(0, 1);
expect(buffered(ContentType.VIDEO, 0)).toBeCloseTo(35, 1);
});
it('extracts CEA-708 captions from hls', async () => {
// Load TS file with CEA-708 captions.
metadata = shaka.test.TestScheme.DATA['cea-708_ts'];
generators = shaka.test.TestScheme.GENERATORS['cea-708_ts'];
const initObject = new Map();
initObject.set(ContentType.VIDEO, getFakeStream(metadata.video));
initObject.set(ContentType.TEXT, getFakeStream(metadata.text));
// Call with forceTransmuxTS = true, so that it will transmux even on
// platforms with native TS support.
await mediaSourceEngine.init(initObject, /* forceTransmuxTS= */ true);
mediaSourceEngine.setSelectedClosedCaptionId('CC1');
await append(ContentType.VIDEO, 0);
expect(textDisplayer.appendSpy).toHaveBeenCalledTimes(3);
});
it('extracts CEA-708 captions from dash', async () => {
// Load MP4 file with CEA-708 closed captions.
metadata = shaka.test.TestScheme.DATA['cea-708_mp4'];
generators = shaka.test.TestScheme.GENERATORS['cea-708_mp4'];
const initObject = new Map();
initObject.set(ContentType.VIDEO, getFakeStream(metadata.video));
await mediaSourceEngine.init(initObject, /* forceTransmuxTS= */ false);
await mediaSourceEngine.setDuration(presentationDuration);
await appendInitWithClosedCaptions(ContentType.VIDEO);
mediaSourceEngine.setSelectedClosedCaptionId('CC1');
await appendWithClosedCaptions(ContentType.VIDEO, 0);
expect(textDisplayer.appendSpy).toHaveBeenCalled();
});
});