forked from shaka-project/shaka-player
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer_external.js
223 lines (190 loc) · 7.79 KB
/
player_external.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
/** @license
* Copyright 2016 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
describe('Player', () => {
const Util = shaka.test.Util;
const Feature = shakaAssets.Feature;
const waitForMovementOrFailOnTimeout = Util.waitForMovementOrFailOnTimeout;
const waitForEndOrTimeout = Util.waitForEndOrTimeout;
/** @type {!jasmine.Spy} */
let onErrorSpy;
/** @type {shaka.extern.SupportType} */
let support;
/** @type {!HTMLVideoElement} */
let video;
/** @type {shaka.Player} */
let player;
/** @type {!shaka.util.EventManager} */
let eventManager;
let compiledShaka;
beforeAll(async () => {
video = shaka.test.UiUtils.createVideoElement();
document.body.appendChild(video);
compiledShaka = await Util.loadShaka(getClientArg('uncompiled'));
support = await compiledShaka.Player.probeSupport();
});
beforeEach(() => {
player = new compiledShaka.Player(video);
// Grab event manager from the uncompiled library:
eventManager = new shaka.util.EventManager();
onErrorSpy = jasmine.createSpy('onError');
onErrorSpy.and.callFake((event) => fail(event.detail));
eventManager.listen(player, 'error', Util.spyFunc(onErrorSpy));
});
afterEach(async () => {
eventManager.release();
await player.destroy();
});
afterAll(() => {
document.body.removeChild(video);
});
describe('plays', () => {
/** @param {!ShakaDemoAssetInfo} asset */
function createAssetTest(asset) {
if (asset.disabled) {
return;
}
const testName =
asset.source + ' / ' + asset.name + ' : ' + asset.manifestUri;
const wit = asset.focus ? fit : it;
wit(testName, async () => {
const idFor = shakaAssets.identifierForKeySystem;
if (!asset.isClear() &&
!asset.drm.some((keySystem) => support.drm[idFor(keySystem)])) {
pending('None of the required key systems are supported.');
}
if (asset.features) {
const mimeTypes = [];
if (asset.features.includes(Feature.WEBM)) {
mimeTypes.push('video/webm');
}
if (asset.features.includes(Feature.MP4)) {
mimeTypes.push('video/mp4');
}
if (mimeTypes.length &&
!mimeTypes.some((type) => support.media[type])) {
pending('None of the required MIME types are supported.');
}
}
// Make sure we are playing the lowest res available to avoid test flake
// based on network issues. Note that disabling ABR and setting a low
// abr.defaultBandwidthEstimate would not be sufficient, because it
// would only affect the choice of track on the first period. When we
// cross a period boundary, the default bandwidth estimate will no
// longer be in effect, and AbrManager may choose higher res tracks for
// the new period. Using abr.restrictions.maxHeight will let us force
// AbrManager to the lowest resolution, which is its fallback when these
// soft restrictions cannot be met.
player.configure('abr.restrictions.maxHeight', 1);
// Make sure that live streams are synced against a good clock.
player.configure('manifest.dash.clockSyncUri',
'https://shaka-player-demo.appspot.com/time.txt');
// Make sure we don't get stuck on gaps that only appear in some
// browsers (Safari, Firefox).
// TODO(https://github.com/google/shaka-player/issues/1702):
// Is this necessary because of a bug in Shaka Player?
player.configure('streaming.jumpLargeGaps', true);
// Add asset-specific configuration.
player.configure(asset.getConfiguration());
// Configure networking for this asset.
const networkingEngine = player.getNetworkingEngine();
asset.applyFilters(networkingEngine);
await player.load(asset.manifestUri);
if (asset.features) {
const isLive = asset.features.includes(Feature.LIVE);
expect(player.isLive()).toBe(isLive);
}
video.play();
// Wait for the video to start playback. If it takes longer than 20
// seconds, fail the test.
await waitForMovementOrFailOnTimeout(eventManager, video, 20);
// Play for 30 seconds, but stop early if the video ends.
await waitForEndOrTimeout(eventManager, video, 30);
if (video.ended) {
checkEndedTime();
} else {
// Expect that in 30 seconds of playback, we go through at least 20
// seconds of content. This allows for some buffering or network
// flake.
expect(video.currentTime).toBeGreaterThan(20);
// Since video.ended is false, we expect the current time to be before
// the video duration.
expect(video.currentTime).toBeLessThan(video.duration);
if (!player.isLive()) {
// Seek close to the end and play the rest of the content.
video.currentTime = video.duration - 15;
// Wait for the video to start playback again after seeking. If it
// takes longer than 20 seconds, fail the test.
await waitForMovementOrFailOnTimeout(eventManager, video, 20);
// Play for 30 seconds, but stop early if the video ends.
await waitForEndOrTimeout(eventManager, video, 30);
// By now, ended should be true.
expect(video.ended).toBe(true);
checkEndedTime();
}
}
}); // actual test
} // createAssetTest
// The user can run tests on a specific manifest URI that is not in the
// asset list.
const testCustomAsset = getClientArg('testCustomAsset');
if (testCustomAsset) {
// Construct an "asset" structure to reuse the test logic above.
/** @type {Object} */
const licenseServers = getClientArg('testCustomLicenseServer');
const keySystems = Object.keys(licenseServers || {});
const asset = new ShakaDemoAssetInfo(
/* name= */ 'custom',
/* iconUri= */ '',
/* manifestUri= */ testCustomAsset,
/* source= */ shakaAssets.Source.CUSTOM);
if (keySystems.length) {
for (const keySystem of keySystems) {
asset.addKeySystem(/** @type {!shakaAssets.KeySystem} */ (keySystem));
const licenseServer = licenseServers[keySystem];
if (licenseServer) {
asset.addLicenseServer(keySystem, licenseServer);
}
}
}
createAssetTest(asset);
} else {
// No custom assets? Create a test for each asset in the demo asset list.
for (const asset of shakaAssets.testAssets) {
createAssetTest(asset);
}
}
});
/**
* Check the video time for videos that we expect to have ended.
*/
function checkEndedTime() {
if (video.currentTime >= video.duration) {
// On some platforms, currentTime surpasses duration by more than 0.1s.
// For the purposes of this test, this is fine, so don't set any precise
// expectations on currentTime if it's larger.
} else {
// On some platforms, currentTime is less than duration, but it should be
// close.
expect(video.currentTime).toBeCloseTo(
video.duration, /* decimal= */ 1);
}
}
/**
* @param {!Object.<string, string>} headers
* @param {shaka.net.NetworkingEngine.RequestType} requestType
* @param {shaka.extern.Request} request
*/
function addLicenseRequestHeaders(headers, requestType, request) {
const 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 (const k in headers) {
request.headers[k] = headers[k];
}
}
});