forked from shaka-project/shaka-player
-
Notifications
You must be signed in to change notification settings - Fork 0
/
media_capabilities_unit.js
276 lines (245 loc) · 10 KB
/
media_capabilities_unit.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
/*! @license
* Shaka Player
* Copyright 2016 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
describe('MediaCapabilities', () => {
const originalCast = window['cast'];
const originalVendor = navigator.vendor;
const originalUserAgent = navigator.userAgent;
const originalRequestMediaKeySystemAccess =
navigator.requestMediaKeySystemAccess;
const originalMediaCapabilities = navigator.mediaCapabilities;
const supportMap = shaka.media.Capabilities.MediaSourceTypeSupportMap;
/** @type {MediaDecodingConfiguration} */
let mockDecodingConfig;
/** @type {!jasmine.Spy} */
let mockCanDisplayType;
beforeAll(() => {
Object.defineProperty(window['navigator'],
'userAgent', {
value: 'unknown', configurable: true,
writable: true,
});
Object.defineProperty(window['navigator'],
'vendor', {
value: 'unknown', configurable: true,
writable: true,
});
Object.defineProperty(window['navigator'],
'requestMediaKeySystemAccess', {
value: 'unknown', configurable: true,
writable: true,
});
Object.defineProperty(window['navigator'],
'mediaCapabilities', {
value: undefined, configurable: true,
writable: true,
});
});
beforeEach(() => {
mockDecodingConfig = {
audio: {
bitrate: 100891,
channels: 2,
contentType: 'audio/mp4; codecs="mp4a.40.2"',
samplerate: 48000,
spatialRendering: false,
},
keySystemConfiguration: {
audio: {robustness: 'SW_SECURE_CRYPTO'},
distinctiveIdentifier: 'optional',
initDataType: 'cenc',
keySystem: 'com.widevine.alpha',
persistentState: 'optional',
sessionTypes: ['temporary'],
video: {robustness: 'SW_SECURE_CRYPTO'},
},
type: 'media-source',
video: {
bitrate: 349265,
contentType: 'video/mp4; codecs="avc1.4D4015"',
framerate: 23.976023976023978,
height: 288,
width: 512,
},
};
shaka.polyfill.MediaCapabilities.memoizedMediaKeySystemAccessRequests_ = {};
mockCanDisplayType = jasmine.createSpy('canDisplayType');
mockCanDisplayType.and.returnValue(false);
});
afterEach(() => {
window['cast'] = originalCast;
});
afterAll(() => {
window['cast'] = originalCast;
Object.defineProperty(window['navigator'],
'userAgent', {value: originalUserAgent});
Object.defineProperty(window['navigator'],
'vendor', {value: originalVendor});
Object.defineProperty(window['navigator'],
'requestMediaKeySystemAccess',
{value: originalRequestMediaKeySystemAccess});
Object.defineProperty(window['navigator'],
'mediaCapabilities', {value: originalMediaCapabilities});
});
describe('install', () => {
it('should define decoding info method', () => {
shaka.polyfill.MediaCapabilities.install();
expect(navigator.mediaCapabilities.decodingInfo).toBeDefined();
});
});
describe('decodingInfo', () => {
it('should check codec support when MediaDecodingConfiguration.type ' +
'is "media-source"', () => {
expect(window['MediaSource']['isTypeSupported']).toBeDefined();
shaka.polyfill.MediaCapabilities.install();
navigator.mediaCapabilities.decodingInfo(mockDecodingConfig);
expect(supportMap.has(mockDecodingConfig.video.contentType)).toBe(true);
expect(supportMap.has(mockDecodingConfig.audio.contentType)).toBe(true);
});
it('should check codec support when MediaDecodingConfiguration.type ' +
'is "file"', () => {
const supportsMediaTypeSpy =
spyOn(shaka['util']['Platform'],
'supportsMediaType').and.returnValue(true);
mockDecodingConfig.type = 'file';
shaka.polyfill.MediaCapabilities.install();
navigator.mediaCapabilities.decodingInfo(mockDecodingConfig);
expect(supportsMediaTypeSpy).toHaveBeenCalledTimes(2);
expect(supportsMediaTypeSpy).toHaveBeenCalledWith(
mockDecodingConfig.video.contentType,
);
expect(supportsMediaTypeSpy).toHaveBeenCalledWith(
mockDecodingConfig.audio.contentType,
);
});
it('should check MediaKeySystem when keySystemConfiguration is present',
async () => {
const mockResult = {mockKeySystemAccess: 'mockKeySystemAccess'};
spyOn(window['MediaSource'], 'isTypeSupported').and.returnValue(true);
const requestKeySystemAccessSpy =
spyOn(window['navigator'],
'requestMediaKeySystemAccess').and.returnValue(mockResult);
shaka.polyfill.MediaCapabilities.install();
const result = await navigator.mediaCapabilities
.decodingInfo(mockDecodingConfig);
expect(requestKeySystemAccessSpy).toHaveBeenCalledWith(
'com.widevine.alpha',
[{
audioCapabilities: [
{
robustness: 'SW_SECURE_CRYPTO',
contentType: 'audio/mp4; codecs="mp4a.40.2"',
},
],
distinctiveIdentifier: 'optional',
initDataTypes: ['cenc'],
persistentState: 'optional',
sessionTypes: ['temporary'],
videoCapabilities: [{
robustness: 'SW_SECURE_CRYPTO',
contentType: 'video/mp4; codecs="avc1.4D4015"',
}],
}],
);
expect(result.keySystemAccess).toEqual(mockResult);
});
it('should read previously requested codec/key system'+
'combinations from cache', async () => {
const mockResult = {mockKeySystemAccess: 'mockKeySystemAccess'};
spyOn(window['MediaSource'], 'isTypeSupported').and.returnValue(true);
const requestKeySystemAccessSpy =
spyOn(window['navigator'],
'requestMediaKeySystemAccess').and.returnValue(mockResult);
shaka.polyfill.MediaCapabilities.install();
await navigator.mediaCapabilities
.decodingInfo(mockDecodingConfig);
await navigator.mediaCapabilities
.decodingInfo(mockDecodingConfig);
expect(requestKeySystemAccessSpy)
.toHaveBeenCalledTimes(1);
});
it('falls back to isTypeSupported() when cast namespace is not available',
async () => {
// Temporarily remove window.cast to trigger error. It's restored
// after every test.
delete window['cast'];
const isChromecastSpy =
spyOn(shaka['util']['Platform'],
'isChromecast').and.returnValue(true);
expect(window['MediaSource']['isTypeSupported']).toBeDefined();
shaka.polyfill.MediaCapabilities.install();
await navigator.mediaCapabilities.decodingInfo(mockDecodingConfig);
expect(mockCanDisplayType).not.toHaveBeenCalled();
// 1 (during install()) + 1 (for video config check).
expect(isChromecastSpy).toHaveBeenCalledTimes(2);
// 1 (fallback in canCastDisplayType()) +
// 1 (mockDecodingConfig.audio).
expect(supportMap.has(mockDecodingConfig.video.contentType))
.toBe(true);
expect(supportMap.has(mockDecodingConfig.audio.contentType))
.toBe(true);
});
it('falls back to isTypeSupported() when canDisplayType() missing',
async () => {
// We only set the cast namespace, but not the canDisplayType() API.
window['cast'] = {};
const isChromecastSpy =
spyOn(shaka['util']['Platform'],
'isChromecast').and.returnValue(true);
expect(window['MediaSource']['isTypeSupported']).toBeDefined();
shaka.polyfill.MediaCapabilities.install();
await navigator.mediaCapabilities.decodingInfo(mockDecodingConfig);
expect(mockCanDisplayType).not.toHaveBeenCalled();
// 1 (during install()) + 1 (for video config check).
expect(isChromecastSpy).toHaveBeenCalledTimes(2);
// 1 (fallback in canCastDisplayType()) +
// 1 (mockDecodingConfig.audio).
expect(supportMap.has(mockDecodingConfig.video.contentType))
.toBe(true);
expect(supportMap.has(mockDecodingConfig.audio.contentType))
.toBe(true);
});
it('should use cast.__platform__.canDisplayType for "supported" field ' +
'when platform is Cast', async () => {
// We're using quotes to access window.cast because the compiler
// knows about lots of Cast-specific APIs we aren't mocking. We
// don't need this mock strictly type-checked.
window['cast'] = {
__platform__: {canDisplayType: mockCanDisplayType},
};
const isChromecastSpy =
spyOn(shaka['util']['Platform'],
'isChromecast').and.returnValue(true);
expect(window['MediaSource']['isTypeSupported']).toBeDefined();
// Tests an HDR stream's extended MIME type is correctly provided.
mockDecodingConfig.video.transferFunction = 'pq';
mockDecodingConfig.video.contentType =
'video/mp4; codecs="hev1.2.4.L153.B0"';
// Round to a whole number since we can't rely on number => string
// conversion precision on all devices.
mockDecodingConfig.video.framerate = 24;
const chromecastType =
'video/mp4; ' +
'codecs="hev1.2.4.L153.B0"; ' +
'width=512; ' +
'height=288; ' +
'framerate=24; ' +
'eotf=smpte2084';
mockCanDisplayType.and.callFake((type) => {
expect(type).toBe(chromecastType);
supportMap.set(type, true);
return true;
});
shaka.polyfill.MediaCapabilities.install();
await navigator.mediaCapabilities.decodingInfo(mockDecodingConfig);
// 1 (during install()) + 1 (for video config check).
expect(isChromecastSpy).toHaveBeenCalledTimes(2);
// 1 (mockDecodingConfig.audio).
expect(supportMap.has(chromecastType)).toBe(true);
// Called once in canCastDisplayType.
expect(mockCanDisplayType).toHaveBeenCalledTimes(1);
});
});
});