forked from shaka-project/shaka-player
-
Notifications
You must be signed in to change notification settings - Fork 0
/
player_unit.js
3683 lines (3210 loc) · 123 KB
/
player_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
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
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*! @license
* Shaka Player
* Copyright 2016 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
goog.require('goog.asserts');
goog.require('shaka.Player');
goog.require('shaka.log');
goog.require('shaka.media.BufferingObserver');
goog.require('shaka.media.ManifestParser');
goog.require('shaka.media.PresentationTimeline');
goog.require('shaka.media.SegmentReference');
goog.require('shaka.media.SegmentIndex');
goog.require('shaka.test.FakeAbrManager');
goog.require('shaka.test.FakeDrmEngine');
goog.require('shaka.test.FakeManifestParser');
goog.require('shaka.test.FakeNetworkingEngine');
goog.require('shaka.test.FakePlayhead');
goog.require('shaka.test.FakeStreamingEngine');
goog.require('shaka.test.FakeTextDisplayer');
goog.require('shaka.test.FakeVideo');
goog.require('shaka.test.ManifestGenerator');
goog.require('shaka.test.Util');
goog.require('shaka.util.ConfigUtils');
goog.require('shaka.util.Error');
goog.require('shaka.util.Iterables');
goog.require('shaka.util.ManifestParserUtils');
goog.requireType('shaka.media.Playhead');
describe('Player', () => {
const ContentType = shaka.util.ManifestParserUtils.ContentType;
const Util = shaka.test.Util;
const originalLogError = shaka.log.error;
const originalLogWarn = shaka.log.warning;
const originalLogAlwaysWarn = shaka.log.alwaysWarn;
const originalIsTypeSupported = window.MediaSource.isTypeSupported;
const originalDecodingInfo = window.shakaMediaCapabilities.decodingInfo;
const fakeManifestUri = 'fake-manifest-uri';
const fakeMimeType = 'application/test';
/** @type {!jasmine.Spy} */
let logErrorSpy;
/** @type {!jasmine.Spy} */
let logWarnSpy;
/** @type {!jasmine.Spy} */
let onError;
/** @type {shaka.extern.Manifest} */
let manifest;
/** @type {!shaka.Player} */
let player;
/** @type {!shaka.test.FakeAbrManager} */
let abrManager;
/** @type {!shaka.test.FakeNetworkingEngine} */
let networkingEngine;
/** @type {!shaka.test.FakeStreamingEngine} */
let streamingEngine;
/** @type {!shaka.test.FakeDrmEngine} */
let drmEngine;
/** @type {!shaka.test.FakePlayhead} */
let playhead;
/** @type {!shaka.test.FakeTextDisplayer} */
let textDisplayer;
let mediaSourceEngine;
/** @type {!shaka.test.FakeVideo} */
let video;
beforeEach(() => {
// By default, errors are a failure.
logErrorSpy = jasmine.createSpy('shaka.log.error');
logErrorSpy.calls.reset();
shaka.log.error = shaka.test.Util.spyFunc(logErrorSpy);
shaka.log.alwaysError = shaka.test.Util.spyFunc(logErrorSpy);
logWarnSpy = jasmine.createSpy('shaka.log.warning');
logErrorSpy.and.callFake(fail);
shaka.log.warning = shaka.test.Util.spyFunc(logWarnSpy);
shaka.log.alwaysWarn = shaka.test.Util.spyFunc(logWarnSpy);
// Since this is not an integration test, we don't want MediaSourceEngine to
// fail assertions based on browser support for types. Pretend that all
// video and audio types are supported.
window.MediaSource.isTypeSupported = (mimeType) => {
const type = mimeType.split('/')[0];
return type == 'video' || type == 'audio';
};
// Since this is not an integration test, we don't want MediaCapabilities to
// fail assertions based on browser support for types. Pretend that all
// video and audio types are supported.
window.shakaMediaCapabilities.decodingInfo = async (config) => {
await Promise.resolve();
const videoType = config['video'] ?
config['video'].contentType.split('/')[0] : null;
const audioType = config['audio'] ?
config['audio'].contentType.split('/')[0] : null;
if (videoType == 'video' || audioType == 'audio') {
return {supported: true};
} else {
return {supported: false};
}
};
// Many tests assume the existence of a manifest, so create a basic one.
// Test suites can override this with more specific manifests.
manifest = shaka.test.ManifestGenerator.generate((manifest) => {
manifest.addVariant(0, (variant) => {
variant.addAudio(1);
variant.addVideo(2);
});
manifest.addVariant(1, (variant) => {
variant.addAudio(3);
variant.addVideo(4);
});
});
shaka.media.ManifestParser.registerParserByMime(
fakeMimeType, () => new shaka.test.FakeManifestParser(manifest));
abrManager = new shaka.test.FakeAbrManager();
textDisplayer = createTextDisplayer();
function dependencyInjector(player) {
// Create a networking engine that always returns an empty buffer.
networkingEngine = new shaka.test.FakeNetworkingEngine();
networkingEngine.setDefaultValue(new ArrayBuffer(0));
drmEngine = new shaka.test.FakeDrmEngine();
playhead = new shaka.test.FakePlayhead();
streamingEngine = new shaka.test.FakeStreamingEngine();
mediaSourceEngine = {
init: jasmine.createSpy('init').and.returnValue(Promise.resolve()),
open: jasmine.createSpy('open').and.returnValue(Promise.resolve()),
destroy:
jasmine.createSpy('destroy').and.returnValue(Promise.resolve()),
setUseEmbeddedText: jasmine.createSpy('setUseEmbeddedText'),
getUseEmbeddedText: jasmine.createSpy('getUseEmbeddedText'),
getTextDisplayer: () => textDisplayer,
ended: jasmine.createSpy('ended').and.returnValue(false),
};
player.createDrmEngine = () => drmEngine;
player.createNetworkingEngine = () => networkingEngine;
player.createPlayhead = () => playhead;
player.createMediaSourceEngine = () => mediaSourceEngine;
player.createStreamingEngine = () => streamingEngine;
}
video = new shaka.test.FakeVideo(20);
player = new shaka.Player(video, dependencyInjector);
player.configure({
// Ensures we don't get a warning about missing preference.
preferredAudioLanguage: 'en',
abrFactory: () => abrManager,
textDisplayFactory: () => textDisplayer,
});
onError = jasmine.createSpy('error event');
onError.and.callFake((event) => {
fail(event.detail);
});
player.addEventListener('error', shaka.test.Util.spyFunc(onError));
});
afterEach(async () => {
try {
await player.destroy();
} finally {
shaka.log.error = originalLogError;
shaka.log.alwaysError = originalLogError;
shaka.log.warning = originalLogWarn;
shaka.log.alwaysWarn = originalLogAlwaysWarn;
window.MediaSource.isTypeSupported = originalIsTypeSupported;
shaka.media.ManifestParser.unregisterParserByMime(fakeMimeType);
window.shakaMediaCapabilities.decodingInfo = originalDecodingInfo;
}
});
describe('destroy', () => {
it('cleans up all dependencies', async () => {
goog.asserts.assert(manifest, 'Manifest should be non-null');
const segmentIndexes = [];
for (const variant of manifest.variants) {
if (variant.audio) {
segmentIndexes.push(variant.audio.segmentIndex);
}
if (variant.video) {
segmentIndexes.push(variant.video.segmentIndex);
}
}
for (const textStream of manifest.textStreams) {
segmentIndexes.push(textStream.segmentIndex);
}
for (const segmentIndex of segmentIndexes) {
spyOn(segmentIndex, 'release');
}
await player.load(fakeManifestUri, 0, fakeMimeType);
await player.destroy();
expect(abrManager.stop).toHaveBeenCalled();
expect(networkingEngine.destroy).toHaveBeenCalled();
expect(drmEngine.destroy).toHaveBeenCalled();
expect(playhead.release).toHaveBeenCalled();
expect(mediaSourceEngine.destroy).toHaveBeenCalled();
expect(streamingEngine.destroy).toHaveBeenCalled();
for (const segmentIndex of segmentIndexes) {
if (segmentIndex) {
expect(segmentIndex.release).toHaveBeenCalled();
}
}
});
it('destroys mediaSourceEngine before drmEngine', async () => {
goog.asserts.assert(manifest, 'Manifest should be non-null');
mediaSourceEngine.destroy.and.callFake(async () => {
expect(drmEngine.destroy).not.toHaveBeenCalled();
await Util.shortDelay();
expect(drmEngine.destroy).not.toHaveBeenCalled();
});
await player.load(fakeManifestUri, 0, fakeMimeType);
await player.destroy();
expect(mediaSourceEngine.destroy).toHaveBeenCalled();
expect(drmEngine.destroy).toHaveBeenCalled();
});
// TODO(vaage): Re-enable once the parser is integrated into the load graph
// better.
xit('destroys parser first when interrupting load', async () => {
const p = shaka.test.Util.shortDelay();
/** @type {!shaka.test.FakeManifestParser} */
const parser = new shaka.test.FakeManifestParser(manifest);
parser.start.and.returnValue(p);
parser.stop.and.callFake(() => {
expect(abrManager.stop).not.toHaveBeenCalled();
expect(networkingEngine.destroy).not.toHaveBeenCalled();
});
shaka.media.ManifestParser.registerParserByMime(
fakeMimeType, () => parser);
const load = player.load(fakeManifestUri, 0, fakeMimeType);
await shaka.test.Util.shortDelay();
await player.destroy();
expect(abrManager.stop).toHaveBeenCalled();
expect(networkingEngine.destroy).toHaveBeenCalled();
expect(parser.stop).toHaveBeenCalled();
await expectAsync(load).toBeRejected();
});
});
describe('load/unload', () => {
/** @type {!jasmine.Spy} */
let checkError;
beforeEach(() => {
goog.asserts.assert(manifest, 'manifest must be non-null');
checkError = jasmine.createSpy('checkError');
checkError.and.callFake((error) => {
expect(error.code).toBe(shaka.util.Error.Code.LOAD_INTERRUPTED);
});
});
describe('streaming event', () => {
/** @type {jasmine.Spy} */
let streamingListener;
beforeEach(() => {
streamingListener = jasmine.createSpy('listener');
player.addEventListener('streaming', Util.spyFunc(streamingListener));
// We must have two different sets of codecs for some of our tests.
manifest = shaka.test.ManifestGenerator.generate((manifest) => {
manifest.addVariant(0, (variant) => {
variant.addAudio(1, (stream) => {
stream.mime('audio/mp4', 'mp4a.40.2');
});
variant.addVideo(2, (stream) => {
stream.mime('video/mp4', 'avc1.4d401f');
});
});
manifest.addVariant(1, (variant) => {
variant.addAudio(3, (stream) => {
stream.mime('audio/webm', 'opus');
});
variant.addVideo(4, (stream) => {
stream.mime('video/webm', 'vp9');
});
});
});
});
async function runTest() {
expect(streamingListener).not.toHaveBeenCalled();
await player.load(fakeManifestUri, 0, fakeMimeType);
expect(streamingListener).toHaveBeenCalled();
}
it('fires after tracks exist', async () => {
streamingListener.and.callFake(() => {
const tracks = player.getVariantTracks();
expect(tracks).toBeDefined();
expect(tracks.length).toBeGreaterThan(0);
});
await runTest();
});
it('fires before any tracks are active', async () => {
streamingListener.and.callFake(() => {
const activeTracks =
player.getVariantTracks().filter((t) => t.active);
expect(activeTracks.length).toBe(0);
});
await runTest();
});
// We used to fire the event /before/ filtering, which meant that for
// multi-codec content, the application might select something which will
// later be removed during filtering.
// https://github.com/google/shaka-player/issues/1119
it('fires after tracks have been filtered', async () => {
streamingListener.and.callFake(() => {
const tracks = player.getVariantTracks();
// Either WebM, or MP4, but not both.
expect(tracks.length).toBe(1);
});
await runTest();
});
});
describe('setTextTrackVisibility', () => {
beforeEach(() => {
manifest = shaka.test.ManifestGenerator.generate((manifest) => {
manifest.addVariant(0, (variant) => {
variant.addAudio(1);
variant.addVideo(2);
});
manifest.addTextStream(3, (stream) => {
stream.bandwidth = 100;
stream.kind = 'caption';
stream.label = 'Spanish';
stream.language = 'es';
});
});
});
it('load text stream if caption is visible', async () => {
await player.setTextTrackVisibility(true);
await player.load(fakeManifestUri, 0, fakeMimeType);
expect(streamingEngine.switchTextStream).toHaveBeenCalled();
expect(shaka.test.Util.invokeSpy(streamingEngine.getCurrentTextStream))
.not.toBe(null);
});
it('does not load text stream if caption is invisible', async () => {
await player.setTextTrackVisibility(false);
await player.load(fakeManifestUri, 0, fakeMimeType);
expect(streamingEngine.switchTextStream).not.toHaveBeenCalled();
expect(shaka.test.Util.invokeSpy(streamingEngine.getCurrentTextStream))
.toBe(null);
});
it('loads text stream if alwaysStreamText is set', async () => {
await player.setTextTrackVisibility(false);
player.configure({streaming: {alwaysStreamText: true}});
await player.load(fakeManifestUri, 0, fakeMimeType);
expect(streamingEngine.switchTextStream).toHaveBeenCalled();
expect(shaka.test.Util.invokeSpy(streamingEngine.getCurrentTextStream))
.not.toBe(null);
streamingEngine.switchTextStream.calls.reset();
await player.setTextTrackVisibility(true);
expect(streamingEngine.switchTextStream).not.toHaveBeenCalled();
expect(streamingEngine.unloadTextStream).not.toHaveBeenCalled();
await player.setTextTrackVisibility(false);
expect(streamingEngine.switchTextStream).not.toHaveBeenCalled();
expect(streamingEngine.unloadTextStream).not.toHaveBeenCalled();
});
});
}); // describe('load/unload')
describe('getConfiguration', () => {
it('returns a copy of the configuration', () => {
const config1 = player.getConfiguration();
config1.streaming.bufferBehind = -99;
const config2 = player.getConfiguration();
expect(config1.streaming.bufferBehind).not.toBe(
config2.streaming.bufferBehind);
});
});
describe('configure', () => {
it('overwrites defaults', () => {
const defaultConfig = player.getConfiguration();
// Make sure the default differs from our test value:
expect(defaultConfig.drm.retryParameters.backoffFactor).not.toBe(5);
expect(defaultConfig.manifest.retryParameters.backoffFactor).not.toBe(5);
player.configure({
drm: {
retryParameters: {backoffFactor: 5},
},
});
const newConfig = player.getConfiguration();
// Make sure we changed the backoff for DRM, but not for manifests:
expect(newConfig.drm.retryParameters.backoffFactor).toBe(5);
expect(newConfig.manifest.retryParameters.backoffFactor).not.toBe(5);
});
it('reverts to defaults when undefined is given', () => {
player.configure({
streaming: {
retryParameters: {backoffFactor: 5},
bufferBehind: 7,
},
});
let newConfig = player.getConfiguration();
expect(newConfig.streaming.retryParameters.backoffFactor).toBe(5);
expect(newConfig.streaming.bufferBehind).toBe(7);
player.configure({
streaming: {
retryParameters: undefined,
},
});
newConfig = player.getConfiguration();
expect(newConfig.streaming.retryParameters.backoffFactor).not.toBe(5);
expect(newConfig.streaming.bufferBehind).toBe(7);
player.configure({streaming: undefined});
newConfig = player.getConfiguration();
expect(newConfig.streaming.bufferBehind).not.toBe(7);
});
it('restricts the types of config values', () => {
logErrorSpy.and.stub();
const defaultConfig = player.getConfiguration();
// Try a bogus bufferBehind (string instead of number)
player.configure({
streaming: {bufferBehind: '77'},
});
let newConfig = player.getConfiguration();
expect(newConfig).toEqual(defaultConfig);
expect(logErrorSpy).toHaveBeenCalledWith(
stringContaining('.streaming.bufferBehind'));
// Try a bogus streaming config (number instead of Object)
logErrorSpy.calls.reset();
player.configure({
streaming: 5,
});
newConfig = player.getConfiguration();
expect(newConfig).toEqual(defaultConfig);
expect(logErrorSpy).toHaveBeenCalledWith(
stringContaining('.streaming'));
});
it('accepts synchronous function values for async function fields', () => {
const defaultConfig = player.getConfiguration();
// Make sure the default is async, or the test is invalid.
const AsyncFunction = (async () => {}).constructor;
expect(defaultConfig.offline.trackSelectionCallback.constructor)
.toBe(AsyncFunction);
// Try a synchronous callback.
player.configure('offline.trackSelectionCallback', () => {});
// If this fails, an error log will trigger test failure.
});
it('expands dictionaries that allow arbitrary keys', () => {
player.configure({
drm: {servers: {'com.widevine.alpha': 'http://foo/widevine'}},
});
let newConfig = player.getConfiguration();
expect(newConfig.drm.servers).toEqual({
'com.widevine.alpha': 'http://foo/widevine',
});
player.configure({
drm: {servers: {'com.microsoft.playready': 'http://foo/playready'}},
});
newConfig = player.getConfiguration();
expect(newConfig.drm.servers).toEqual({
'com.widevine.alpha': 'http://foo/widevine',
'com.microsoft.playready': 'http://foo/playready',
});
});
it('expands dictionaries but still restricts their values', () => {
// Try a bogus server value (number instead of string)
logErrorSpy.and.stub();
player.configure({
drm: {servers: {'com.widevine.alpha': 7}},
});
let newConfig = player.getConfiguration();
expect(newConfig.drm.servers).toEqual({});
expect(logErrorSpy).toHaveBeenCalledWith(
stringContaining('.drm.servers.com.widevine.alpha'));
// Try a valid advanced config.
logErrorSpy.calls.reset();
player.configure({
drm: {advanced: {'ks1': {distinctiveIdentifierRequired: true}}},
});
newConfig = player.getConfiguration();
expect(newConfig.drm.advanced).toEqual({
'ks1': jasmine.objectContaining({distinctiveIdentifierRequired: true}),
});
expect(logErrorSpy).not.toHaveBeenCalled();
const lastGoodConfig = newConfig;
// Try an invalid advanced config key.
player.configure({
drm: {advanced: {'ks1': {bogus: true}}},
});
newConfig = player.getConfiguration();
expect(newConfig).toEqual(lastGoodConfig);
expect(logErrorSpy).toHaveBeenCalledWith(
stringContaining('.drm.advanced.ks1.bogus'));
});
it('removes dictionary entries when undefined is given', () => {
player.configure({
drm: {
servers: {
'com.widevine.alpha': 'http://foo/widevine',
'com.microsoft.playready': 'http://foo/playready',
},
},
});
let newConfig = player.getConfiguration();
expect(newConfig.drm.servers).toEqual({
'com.widevine.alpha': 'http://foo/widevine',
'com.microsoft.playready': 'http://foo/playready',
});
player.configure({
drm: {servers: {'com.widevine.alpha': undefined}},
});
newConfig = player.getConfiguration();
expect(newConfig.drm.servers).toEqual({
'com.microsoft.playready': 'http://foo/playready',
});
player.configure({
drm: {servers: undefined},
});
newConfig = player.getConfiguration();
expect(newConfig.drm.servers).toEqual({});
});
it('checks the number of arguments to functions', () => {
const goodFailureCallback = (error) => {};
const badFailureCallback1 = () => {}; // too few args
const badFailureCallback2 = (x, y) => {}; // too many args
// Takes good callback.
player.configure({
streaming: {failureCallback: goodFailureCallback},
});
let newConfig = player.getConfiguration();
expect(newConfig.streaming.failureCallback).toBe(goodFailureCallback);
expect(logWarnSpy).not.toHaveBeenCalled();
// Warns about bad callback #1, still takes it.
logWarnSpy.calls.reset();
player.configure({
streaming: {failureCallback: badFailureCallback1},
});
newConfig = player.getConfiguration();
expect(newConfig.streaming.failureCallback).toBe(badFailureCallback1);
expect(logWarnSpy).toHaveBeenCalledWith(
stringContaining('.streaming.failureCallback'));
// Warns about bad callback #2, still takes it.
logWarnSpy.calls.reset();
player.configure({
streaming: {failureCallback: badFailureCallback2},
});
newConfig = player.getConfiguration();
expect(newConfig.streaming.failureCallback).toBe(badFailureCallback2);
expect(logWarnSpy).toHaveBeenCalledWith(
stringContaining('.streaming.failureCallback'));
// Resets to default if undefined.
logWarnSpy.calls.reset();
player.configure({
streaming: {failureCallback: undefined},
});
newConfig = player.getConfiguration();
expect(newConfig.streaming.failureCallback).not.toBe(badFailureCallback2);
expect(logWarnSpy).not.toHaveBeenCalled();
});
// Regression test for https://github.com/google/shaka-player/issues/784
it('does not throw when overwriting serverCertificate', () => {
player.configure({
drm: {
advanced: {
'com.widevine.alpha': {
serverCertificate: new Uint8Array(1),
},
},
},
});
player.configure({
drm: {
advanced: {
'com.widevine.alpha': {
serverCertificate: new Uint8Array(2),
},
},
},
});
});
it('checks the type of serverCertificate', () => {
logErrorSpy.and.stub();
player.configure({
drm: {
advanced: {
'com.widevine.alpha': {
serverCertificate: null,
},
},
},
});
expect(logErrorSpy).toHaveBeenCalledWith(
stringContaining('.serverCertificate'));
logErrorSpy.calls.reset();
player.configure({
drm: {
advanced: {
'com.widevine.alpha': {
serverCertificate: 'foobar',
},
},
},
});
expect(logErrorSpy).toHaveBeenCalledWith(
stringContaining('.serverCertificate'));
});
it('does not throw when null appears instead of an object', () => {
logErrorSpy.and.stub();
player.configure({
drm: {advanced: null},
});
expect(logErrorSpy).toHaveBeenCalledWith(
stringContaining('.drm.advanced'));
});
it('configures play and seek range for VOD', async () => {
player.configure({playRangeStart: 5, playRangeEnd: 10});
const timeline = new shaka.media.PresentationTimeline(300, 0);
timeline.setStatic(true);
manifest = shaka.test.ManifestGenerator.generate((manifest) => {
manifest.presentationTimeline = timeline;
manifest.addVariant(0, (variant) => {
variant.addVideo(1);
});
});
goog.asserts.assert(manifest, 'manifest must be non-null');
await player.load(fakeManifestUri, 0, fakeMimeType);
const seekRange = player.seekRange();
expect(seekRange.start).toBe(5);
expect(seekRange.end).toBe(10);
});
it('configures play and seek range after playback starts', async () => {
const timeline = new shaka.media.PresentationTimeline(300, 0);
timeline.setStatic(true);
manifest = shaka.test.ManifestGenerator.generate((manifest) => {
manifest.presentationTimeline = timeline;
manifest.addVariant(0, (variant) => {
variant.addVideo(1);
});
});
goog.asserts.assert(manifest, 'manifest must be non-null');
await player.load(fakeManifestUri, 0, fakeMimeType);
const seekRange = player.seekRange();
expect(seekRange.start).toBe(0);
expect(seekRange.end).toBe(Infinity);
// Change the configuration after the playback starts.
player.configure({playRangeStart: 5, playRangeEnd: 10});
const seekRange2 = player.seekRange();
expect(seekRange2.start).toBe(5);
expect(seekRange2.end).toBe(10);
});
it('does not switch for plain configuration changes', async () => {
await player.load(fakeManifestUri, 0, fakeMimeType);
streamingEngine.switchVariant.calls.reset();
player.configure({abr: {enabled: false}});
player.configure({streaming: {bufferingGoal: 9001}});
// Delay to ensure that the switch would have been called.
await shaka.test.Util.shortDelay();
expect(streamingEngine.switchVariant).not.toHaveBeenCalled();
});
it('accepts parameters in a (fieldName, value) format', () => {
const oldConfig = player.getConfiguration();
const oldDelayLicense = oldConfig.drm.delayLicenseRequestUntilPlayed;
const oldSwitchInterval = oldConfig.abr.switchInterval;
const oldPreferredLang = oldConfig.preferredAudioLanguage;
expect(oldDelayLicense).toBe(false);
expect(oldSwitchInterval).toBe(8);
expect(oldPreferredLang).toBe('en');
player.configure('drm.delayLicenseRequestUntilPlayed', true);
player.configure('abr.switchInterval', 10);
player.configure('preferredAudioLanguage', 'fr');
const newConfig = player.getConfiguration();
const newDelayLicense = newConfig.drm.delayLicenseRequestUntilPlayed;
const newSwitchInterval = newConfig.abr.switchInterval;
const newPreferredLang = newConfig.preferredAudioLanguage;
expect(newDelayLicense).toBe(true);
expect(newSwitchInterval).toBe(10);
expect(newPreferredLang).toBe('fr');
});
it('accepts escaped "." in names', () => {
const convert = (name, value) => {
return shaka.util.ConfigUtils.convertToConfigObject(name, value);
};
expect(convert('foo', 1)).toEqual({foo: 1});
expect(convert('foo.bar', 1)).toEqual({foo: {bar: 1}});
expect(convert('foo..bar', 1)).toEqual({foo: {'': {bar: 1}}});
expect(convert('foo.bar.baz', 1)).toEqual({foo: {bar: {baz: 1}}});
expect(convert('foo.bar\\.baz', 1)).toEqual({foo: {'bar.baz': 1}});
expect(convert('foo.baz.', 1)).toEqual({foo: {baz: {'': 1}}});
expect(convert('foo.baz\\.', 1)).toEqual({foo: {'baz.': 1}});
expect(convert('foo\\.bar', 1)).toEqual({'foo.bar': 1});
expect(convert('.foo', 1)).toEqual({'': {foo: 1}});
expect(convert('\\.foo', 1)).toEqual({'.foo': 1});
});
it('returns whether the config was valid', () => {
logErrorSpy.and.stub();
expect(player.configure({streaming: {bufferBehind: '77'}})).toBe(false);
expect(player.configure({streaming: {bufferBehind: 77}})).toBe(true);
});
it('still sets other fields when there are errors', () => {
logErrorSpy.and.stub();
const changes = {
manifest: {foobar: false},
streaming: {bufferBehind: 77},
};
expect(player.configure(changes)).toBe(false);
const newConfig = player.getConfiguration();
expect(newConfig.streaming.bufferBehind).toBe(77);
});
// https://github.com/google/shaka-player/issues/1524
it('does not pollute other advanced DRM configs', () => {
player.configure('drm.advanced.foo', {});
player.configure('drm.advanced.bar', {});
const fooConfig1 = player.getConfiguration().drm.advanced['foo'];
const barConfig1 = player.getConfiguration().drm.advanced['bar'];
expect(fooConfig1.distinctiveIdentifierRequired).toBe(false);
expect(barConfig1.distinctiveIdentifierRequired).toBe(false);
player.configure('drm.advanced.foo.distinctiveIdentifierRequired', true);
const fooConfig2 = player.getConfiguration().drm.advanced['foo'];
const barConfig2 = player.getConfiguration().drm.advanced['bar'];
expect(fooConfig2.distinctiveIdentifierRequired).toBe(true);
expect(barConfig2.distinctiveIdentifierRequired).toBe(false);
});
it('sets default streaming configuration with low latency mode', () => {
player.configure({
streaming: {
lowLatencyMode: true,
rebufferingGoal: 1,
inaccurateManifestTolerance: 1,
},
});
expect(player.getConfiguration().streaming.rebufferingGoal).toBe(1);
expect(player.getConfiguration().streaming.inaccurateManifestTolerance)
.toBe(1);
// When low latency streaming gets enabled, rebufferingGoal will default
// to 0.01 if unless specified, and inaccurateManifestTolerance will
// default to 0 unless specified.
player.configure('streaming.lowLatencyMode', true);
expect(player.getConfiguration().streaming.rebufferingGoal).toBe(0.01);
expect(player.getConfiguration().streaming.inaccurateManifestTolerance)
.toBe(0);
});
});
describe('resetConfiguration', () => {
it('resets configurations to default', () => {
const default_ = player.getConfiguration().streaming.bufferingGoal;
expect(default_).not.toBe(100);
player.configure('streaming.bufferingGoal', 100);
expect(player.getConfiguration().streaming.bufferingGoal).toBe(100);
player.resetConfiguration();
expect(player.getConfiguration().streaming.bufferingGoal).toBe(default_);
});
it('resets the arbitrary keys', () => {
player.configure('drm.servers.org\\.w3\\.clearKey', 'http://foo.com');
expect(player.getConfiguration().drm.servers).toEqual({
'org.w3.clearKey': 'http://foo.com',
});
player.resetConfiguration();
expect(player.getConfiguration().drm.servers).toEqual({});
});
it('keeps shared configuration the same', () => {
const config = player.getSharedConfiguration();
player.resetConfiguration();
expect(player.getSharedConfiguration()).toBe(config);
});
});
describe('AbrManager', () => {
beforeEach(() => {
goog.asserts.assert(manifest, 'manifest must be non-null');
});
it('sets through load', async () => {
await player.load(fakeManifestUri, 0, fakeMimeType);
expect(abrManager.init).toHaveBeenCalled();
});
it('calls chooseVariant', async () => {
await player.load(fakeManifestUri, 0, fakeMimeType);
expect(abrManager.chooseVariant).toHaveBeenCalled();
});
it('enables automatically', async () => {
await player.load(fakeManifestUri, 0, fakeMimeType);
expect(abrManager.enable).toHaveBeenCalled();
});
it('does not enable if adaptation is disabled', async () => {
player.configure({abr: {enabled: false}});
await player.load(fakeManifestUri, 0, fakeMimeType);
expect(abrManager.enable).not.toHaveBeenCalled();
});
it('enables/disables though configure', async () => {
await player.load(fakeManifestUri, 0, fakeMimeType);
abrManager.enable.calls.reset();
abrManager.disable.calls.reset();
player.configure({abr: {enabled: false}});
expect(abrManager.disable).toHaveBeenCalled();
player.configure({abr: {enabled: true}});
expect(abrManager.enable).toHaveBeenCalled();
});
it('reuses AbrManager instance', async () => {
/** @type {!jasmine.Spy} */
const spy =
jasmine.createSpy('AbrManagerFactory').and.returnValue(abrManager);
player.configure({abrFactory: spy});
await player.load(fakeManifestUri, 0, fakeMimeType);
expect(spy).toHaveBeenCalled();
spy.calls.reset();
await player.load(fakeManifestUri, 0, fakeMimeType);
expect(spy).not.toHaveBeenCalled();
});
it('creates new AbrManager if factory changes', async () => {
/** @type {!jasmine.Spy} */
const spy1 =
jasmine.createSpy('AbrManagerFactory').and.returnValue(abrManager);
/** @type {!jasmine.Spy} */
const spy2 =
jasmine.createSpy('AbrManagerFactory').and.returnValue(abrManager);
player.configure({abrFactory: spy1});
await player.load(fakeManifestUri, 0, fakeMimeType);
expect(spy1).toHaveBeenCalled();
expect(spy2).not.toHaveBeenCalled();
spy1.calls.reset();
player.configure({abrFactory: spy2});
await player.load(fakeManifestUri, 0, fakeMimeType);
expect(spy1).not.toHaveBeenCalled();
expect(spy2).toHaveBeenCalled();
});
});
describe('filterTracks', () => {
it('retains only video+audio variants if they exist', async () => {
manifest = shaka.test.ManifestGenerator.generate((manifest) => {
manifest.addVariant(10, (variant) => {
variant.addAudio(1);
});
manifest.addVariant(11, (variant) => {
variant.addAudio(2);
variant.addVideo(3);
});
manifest.addVariant(12, (variant) => {
variant.addVideo(4);
});
});
const variantTracks = [
jasmine.objectContaining({
id: 11,
active: true,
type: 'variant',
}),
];
await player.load(fakeManifestUri, 0, fakeMimeType);
const actualVariantTracks = player.getVariantTracks();
expect(actualVariantTracks).toEqual(variantTracks);
});
});
describe('tracks', () => {
/** @type {!Array.<shaka.extern.Track>} */
let variantTracks;
/** @type {!Array.<shaka.extern.Track>} */
let textTracks;
/** @type {!Array.<shaka.extern.Track>} */
let imageTracks;
beforeEach(async () => {
// A manifest we can use to test track expectations.
manifest = shaka.test.ManifestGenerator.generate((manifest) => {
manifest.addVariant(100, (variant) => { // main surround, low res
variant.bandwidth = 1300;
variant.language = 'en';
variant.addVideo(1, (stream) => {
stream.originalId = 'video-1kbps';
stream.bandwidth = 1000;
stream.width = 100;
stream.height = 200;
stream.frameRate = 1000000 / 42000;
stream.pixelAspectRatio = '59:54';
stream.roles = ['main'];
});
variant.addAudio(3, (stream) => {
stream.originalId = 'audio-en-6c';
stream.bandwidth = 300;
stream.channelsCount = 6;
stream.audioSamplingRate = 48000;
stream.roles = ['main'];
});
});
manifest.addVariant(101, (variant) => { // main surround, high res
variant.bandwidth = 2300;
variant.language = 'en';