forked from shaka-project/shaka-player
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer_integration.js
413 lines (364 loc) · 14.3 KB
/
player_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
/**
* @license
* Copyright 2016 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
describe('Player', function() {
/** @const */
var Util = shaka.test.Util;
/** @const */
var Feature = shakaAssets.Feature;
/** @type {!jasmine.Spy} */
var onErrorSpy;
/** @type {shakaExtern.SupportType} */
var support;
/** @type {!HTMLVideoElement} */
var video;
/** @type {shaka.Player} */
var player;
/** @type {shaka.util.EventManager} */
var eventManager;
var compiledShaka;
beforeAll(function(done) {
video = /** @type {!HTMLVideoElement} */ (document.createElement('video'));
video.width = 600;
video.height = 400;
video.muted = true;
document.body.appendChild(video);
var loaded = new shaka.util.PublicPromise();
if (getClientArg('uncompiled')) {
// For debugging purposes, use the uncompiled library.
compiledShaka = shaka;
loaded.resolve();
} else {
// Load the compiled library as a module.
// All tests in this suite will use the compiled library.
require(['/base/dist/shaka-player.compiled.js'], function(shakaModule) {
compiledShaka = shakaModule;
compiledShaka.net.NetworkingEngine.registerScheme(
'test', shaka.test.TestScheme);
compiledShaka.media.ManifestParser.registerParserByMime(
'application/x-test-manifest',
shaka.test.TestScheme.ManifestParser);
loaded.resolve();
});
}
loaded.then(function() {
return shaka.test.TestScheme.createManifests(compiledShaka, '_compiled');
}).then(function() {
return compiledShaka.Player.probeSupport();
}).then(function(supportResults) {
support = supportResults;
done();
});
});
beforeEach(function() {
player = new compiledShaka.Player(video);
// Grab event manager from the uncompiled library:
eventManager = new shaka.util.EventManager();
onErrorSpy = jasmine.createSpy('onError');
onErrorSpy.and.callFake(function(event) { fail(event.detail); });
eventManager.listen(player, 'error', Util.spyFunc(onErrorSpy));
});
afterEach(function(done) {
Promise.all([
eventManager.destroy(),
player.destroy()
]).then(function() {
// Work-around: allow the Tizen media pipeline to cool down.
// Without this, Tizen's pipeline seems to hang in subsequent tests.
// TODO: file a bug on Tizen
return Util.delay(0.1);
}).catch(fail).then(done);
});
afterAll(function() {
document.body.removeChild(video);
});
describe('getStats', function() {
it('gives stats about current stream', function(done) {
// This is tested more in player_unit.js. This is here to test the public
// API and to check for renaming.
player.load('test:sintel_compiled').then(function() {
video.play();
return waitUntilPlayheadReaches(video, 1, 10);
}).then(function() {
var stats = player.getStats();
var expected = {
width: jasmine.any(Number),
height: jasmine.any(Number),
streamBandwidth: jasmine.any(Number),
decodedFrames: jasmine.any(Number),
droppedFrames: jasmine.any(Number),
estimatedBandwidth: jasmine.any(Number),
loadLatency: jasmine.any(Number),
playTime: jasmine.any(Number),
bufferingTime: jasmine.any(Number),
// We should have loaded the first Period by now, so we should have a
// history.
switchHistory: jasmine.arrayContaining([{
timestamp: jasmine.any(Number),
id: jasmine.any(Number),
type: 'variant',
fromAdaptation: true,
bandwidth: 0
}]),
stateHistory: jasmine.arrayContaining([{
state: 'playing',
timestamp: jasmine.any(Number),
duration: jasmine.any(Number)
}])
};
expect(stats).toEqual(expected);
}).catch(fail).then(done);
});
});
describe('setTextTrackVisibility', function() {
// Using mode='disabled' on TextTrack causes cues to go null, which leads
// to a crash in TextEngine. This validates that we do not trigger this
// behavior when changing visibility of text.
it('does not cause cues to be null', function(done) {
player.load('test:sintel_compiled').then(function() {
video.play();
return waitUntilPlayheadReaches(video, 1, 10);
}).then(function() {
// This TextTrack was created as part of load() when we set up the
// TextDisplayer.
var textTrack = video.textTracks[0];
expect(textTrack).not.toBe(null);
if (textTrack) {
// This should not be null initially.
expect(textTrack.cues).not.toBe(null);
player.setTextTrackVisibility(true);
// This should definitely not be null when visible.
expect(textTrack.cues).not.toBe(null);
player.setTextTrackVisibility(false);
// This should not transition to null when invisible.
expect(textTrack.cues).not.toBe(null);
}
}).catch(fail).then(done);
});
});
describe('plays', function() {
it('with external text tracks', function(done) {
player.load('test:sintel_no_text_compiled').then(function() {
// For some reason, using path-absolute URLs (i.e. without the hostname)
// like this doesn't work on Safari. So manually resolve the URL.
var locationUri = new goog.Uri(location.href);
var partialUri = new goog.Uri('/base/test/test/assets/text-clip.vtt');
var absoluteUri = locationUri.resolve(partialUri);
player.addTextTrack(absoluteUri.toString(), 'en', 'subtitles',
'text/vtt');
video.play();
return Util.delay(5);
}).then(function() {
var textTracks = player.getTextTracks();
expect(textTracks).toBeTruthy();
expect(textTracks.length).toBe(1);
expect(textTracks[0].active).toBe(true);
expect(textTracks[0].language).toEqual('en');
}).catch(fail).then(done);
});
it('while changing languages with short Periods', function(done) {
// See: https://github.com/google/shaka-player/issues/797
player.configure({preferredAudioLanguage: 'en'});
player.load('test:sintel_short_periods_compiled').then(function() {
video.play();
return waitUntilPlayheadReaches(video, 8, 30);
}).then(function() {
// The Period changes at 10 seconds. Assert that we are in the previous
// Period and have buffered into the next one.
expect(video.currentTime).toBeLessThan(9);
// The two periods might not be in a single contiguous buffer, so don't
// check end(0). Gap-jumping will deal with any discontinuities.
var bufferEnd = video.buffered.end(video.buffered.length - 1);
expect(bufferEnd).toBeGreaterThan(11);
// Change to a different language; this should clear the buffers and
// cause a Period transition again.
expect(getActiveLanguage()).toBe('en');
player.selectAudioLanguage('es');
return waitUntilPlayheadReaches(video, 21, 30);
}).then(function() {
// Should have gotten past the next Period transition and still be
// playing the new language.
expect(getActiveLanguage()).toBe('es');
}).catch(fail).then(done);
});
shakaAssets.testAssets.forEach(function(asset) {
if (asset.disabled) return;
var testName =
asset.source + ' / ' + asset.name + ' : ' + asset.manifestUri;
var wit = asset.focus ? fit : external_it;
wit(testName, function(done) {
if (asset.drm.length && !asset.drm.some(
function(keySystem) { return support.drm[keySystem]; })) {
pending('None of the required key systems are supported.');
}
var mimeTypes = [];
if (asset.features.indexOf(Feature.WEBM) >= 0)
mimeTypes.push('video/webm');
if (asset.features.indexOf(Feature.MP4) >= 0)
mimeTypes.push('video/mp4');
if (!mimeTypes.some(
function(type) { return support.media[type]; })) {
pending('None of the required MIME types are supported.');
}
var isLive = asset.features.indexOf(Feature.LIVE) >= 0;
var config = { abr: {}, drm: {}, manifest: { dash: {} } };
config.abr.enabled = false;
config.manifest.dash.clockSyncUri =
'//shaka-player-demo.appspot.com/time.txt';
if (asset.licenseServers)
config.drm.servers = asset.licenseServers;
if (asset.drmCallback)
config.manifest.dash.customScheme = asset.drmCallback;
if (asset.clearKeys)
config.drm.clearKeys = asset.clearKeys;
player.configure(config);
if (asset.licenseRequestHeaders) {
player.getNetworkingEngine().registerRequestFilter(
addLicenseRequestHeaders.bind(null, asset.licenseRequestHeaders));
}
var networkingEngine = player.getNetworkingEngine();
if (asset.requestFilter)
networkingEngine.registerRequestFilter(asset.requestFilter);
if (asset.responseFilter)
networkingEngine.registerResponseFilter(asset.responseFilter);
if (asset.extraConfig)
player.configure(asset.extraConfig);
player.load(asset.manifestUri).then(function() {
expect(player.isLive()).toEqual(isLive);
video.play();
// 30 seconds or video ended, whichever comes first.
return waitForTimeOrEnd(video, 40);
}).then(function() {
if (video.ended) {
expect(video.currentTime).toBeCloseTo(video.duration, 1);
} else {
expect(video.currentTime).toBeGreaterThan(20);
// If it were very close to duration, why !video.ended?
expect(video.currentTime).not.toBeCloseTo(video.duration);
if (!player.isLive()) {
// Seek and play out the end.
video.currentTime = video.duration - 15;
// 30 seconds or video ended, whichever comes first.
return waitForTimeOrEnd(video, 40).then(function() {
expect(video.ended).toBe(true);
expect(video.currentTime).toBeCloseTo(video.duration, 1);
});
}
}
}).catch(fail).then(done);
});
});
/**
* Gets the language of the active Variant.
* @return {string}
*/
function getActiveLanguage() {
var tracks = player.getVariantTracks().filter(function(t) {
return t.active;
});
expect(tracks.length).toBeGreaterThan(0);
return tracks[0].language;
}
});
describe('cancel', function() {
/** @type {!jasmine.Spy} */
var schemeSpy;
beforeAll(function() {
schemeSpy = jasmine.createSpy('reject scheme');
schemeSpy.and.callFake(function() {
// Throw a recoverable error so it will retry.
var error = new shaka.util.Error(
shaka.util.Error.Severity.RECOVERABLE,
shaka.util.Error.Category.NETWORK,
shaka.util.Error.Code.HTTP_ERROR);
return Promise.reject(error);
});
compiledShaka.net.NetworkingEngine.registerScheme('reject',
Util.spyFunc(schemeSpy));
});
afterEach(function() {
schemeSpy.calls.reset();
});
afterAll(function() {
compiledShaka.net.NetworkingEngine.unregisterScheme('reject');
});
function testTemplate(operationFn) {
// No data will be loaded for this test, so it can use a real manifest
// parser safely.
player.load('reject://www.foo.com/bar.mpd').then(fail);
return shaka.test.Util.delay(0.1).then(operationFn).then(function() {
expect(schemeSpy.calls.count()).toBe(1);
});
}
it('unload prevents further manifest load retries', function(done) {
testTemplate(function() { return player.unload(); }).then(done);
});
it('destroy prevents further manifest load retries', function(done) {
testTemplate(function() { return player.destroy(); }).then(done);
});
});
/**
* @param {!HTMLMediaElement} video
* @param {number} playheadTime The time to wait for.
* @param {number} timeout in seconds, after which the Promise fails
* @return {!Promise}
*/
function waitUntilPlayheadReaches(video, playheadTime, timeout) {
var curEventManager = eventManager;
return new Promise(function(resolve, reject) {
curEventManager.listen(video, 'timeupdate', function() {
if (video.currentTime >= playheadTime) {
curEventManager.unlisten(video, 'timeupdate');
resolve();
}
});
Util.delay(timeout).then(function() {
curEventManager.unlisten(video, 'timeupdate');
reject('Timeout waiting for time');
});
});
}
/**
* @param {!EventTarget} target
* @param {number} timeout in seconds, after which the Promise succeeds
* @return {!Promise}
*/
function waitForTimeOrEnd(target, timeout) {
var curEventManager = eventManager;
return new Promise(function(resolve, reject) {
var callback = function() {
curEventManager.unlisten(target, 'ended');
resolve();
};
curEventManager.listen(target, 'ended', callback);
Util.delay(timeout).then(callback);
});
}
/**
* @param {!Object.<string, string>} headers
* @param {shaka.net.NetworkingEngine.RequestType} requestType
* @param {shakaExtern.Request} request
*/
function addLicenseRequestHeaders(headers, requestType, request) {
var RequestType = compiledShaka.net.NetworkingEngine.RequestType;
if (requestType != RequestType.LICENSE) return;
// Add these to the existing headers. Do not clobber them!
// For PlayReady, there will already be headers in the request.
for (var k in headers) {
request.headers[k] = headers[k];
}
}
});