-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathhtml5.ts
1276 lines (1165 loc) · 39.6 KB
/
html5.ts
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
import { FakeEventTarget } from '../../event/fake-event-target';
import { FakeEvent } from '../../event/fake-event';
import { EventManager } from '../../event/event-manager';
import { CustomEventType, Html5EventType } from '../../event/event-type';
import MediaSourceProvider from './media-source/media-source-provider';
import VideoTrack from '../../track/video-track';
import AudioTrack from '../../track/audio-track';
import { PKTextTrack, getActiveCues } from '../../track/text-track';
import ImageTrack from '../../track/image-track';
import { createTimedMetadata } from '../../track/timed-metadata';
import * as Utils from '../../utils/util';
import Html5AutoPlayCapability from './capabilities/html5-autoplay';
import Error from '../../error/error';
import getLogger from '../../utils/logger';
import { DroppedFramesWatcher } from '../dropped-frames-watcher';
import { ThumbnailInfo } from '../../thumbnail/thumbnail-info';
import { IMediaSourceAdapter } from '../../types';
import { CapabilityResult, ICapability } from '../../types';
import { PKABRRestrictionObject, PKDrmConfigObject, PKDrmDataObject, PKMediaSourceObject, PKVideoElementStore } from '../../types';
import { IEngine } from '../../types';
import Track from '../../track/track';
const SHORT_BUFFERING_TIMEOUT: number = 200;
/**
* Html5 engine for playback.
* @classdesc
*/
export default class Html5 extends FakeEventTarget implements IEngine {
/**
* The video element.
* @type {HTMLVideoElement}
* @private
*/
private _el!: HTMLVideoElement;
/**
* The event manager of the engine.
* @type {EventManager}
* @private
*/
private _eventManager: EventManager;
/**
* The selected media source adapter of the engine.
* @type {?IMediaSourceAdapter}
* @private
*/
private _mediaSourceAdapter!: IMediaSourceAdapter | null;
/**
* The player config object.
* @type {Object}
* @private
*/
private _config: any;
/**
* Promise to indicate when a media source adapter can be loaded.
* @type {Promise<*>}
* @private
*/
private _canLoadMediaSourceAdapterPromise: Promise<void>;
private _droppedFramesWatcher: DroppedFramesWatcher | undefined;
private _reset: boolean = false;
private _cachedUrls: string[] = [];
/**
* The html5 class logger.
* @type {any}
* @static
* @private
*/
private static _logger: any = getLogger('Html5');
/**
* The html5 capabilities handlers.
* @private
* @static
*/
private static _capabilities: Array<ICapability> = [Html5AutoPlayCapability];
/**
* @type {string} - The engine id.
* @public
* @static
*/
public static id: string = 'html5';
/**
* @type {PKVideoElementStore} - Store object which maps between playerId to its video element.
*/
public static videoElementStore: PKVideoElementStore = {};
/**
* Checks if html5 is supported.
* @returns {boolean} - Whether the html5 is supported.
*/
public static isSupported(): boolean {
try {
const el = Utils.Dom.createElement('video');
el.volume = 0.5;
return !!el.canPlayType;
} catch (e) {
return false;
}
}
/**
* Factory method to create an engine.
* @param {PKMediaSourceObject} source - The selected source object.
* @param {Object} config - The player configuration.
* @param {string} playerId - The player id.
* @returns {IEngine} - New instance of the run time engine.
* @public
* @static
*/
public static createEngine(source: PKMediaSourceObject, config: any, playerId: string): IEngine {
return new this(source, config, playerId);
}
/**
* Checks if the engine can play a given source.
* @param {PKMediaSourceObject} source - The source object to check.
* @param {boolean} preferNative - prefer native flag.
* @param {PKDrmConfigObject} drmConfig - The drm config.
* @returns {boolean} - Whether the engine can play the source.
* @public
* @static
*/
public static canPlaySource(source: PKMediaSourceObject, preferNative: boolean, drmConfig: PKDrmConfigObject): boolean {
return MediaSourceProvider.canPlaySource(source, preferNative, drmConfig);
}
/**
* Runs the html5 capabilities tests.
* @returns {void}
* @public
* @static
*/
public static runCapabilities(): void {
Html5._capabilities.forEach((capability) => capability.runCapability());
}
/**
* Gets the html5 capabilities.
* @return {Promise<Object>} - The html5 capabilities object.
* @public
* @static
*/
public static getCapabilities(): Promise<any> {
const promises: CapabilityResult[] = [];
Html5._capabilities.forEach((capability) => promises.push(capability.getCapability()));
return Promise.all(promises).then((arrayOfResults) => {
const mergedResults: CapabilityResult = {};
arrayOfResults.forEach((res) => Object.assign(mergedResults, res));
return { [Html5.id]: mergedResults };
});
}
/**
* Sets an engine capabilities.
* @param {Object} capabilities - The engine capabilities.
* @returns {void}
* @public
* @static
*/
public static setCapabilities(capabilities: { [name: string]: any }): void {
Html5._capabilities.forEach((capability) => capability.setCapabilities(capabilities));
}
/**
* For browsers which block auto play, use the user gesture to open the video element and enable playing via API.
* @returns {void}
* @param {string} playerId - the id to be set as the key of the video element
* @private
* @public
*/
public static prepareVideoElement(playerId: string): void {
if (!Html5.videoElementStore[playerId]) {
Html5._logger.debug(`Create the video element for playing ${playerId}`);
const videoElement = Utils.Dom.createElement('video');
Html5.videoElementStore[playerId] = videoElement;
}
Html5._logger.debug(`Prepare the video element for playing ${playerId}`);
Html5.videoElementStore[playerId].load();
}
/**
* The player playback rates.
* @type {Array<number>}
*/
public static PLAYBACK_RATES: Array<number> = [0.5, 1, 1.5, 2];
/**
* @constructor
* @param {PKMediaSourceObject} source - The selected source object.
* @param {Object} config - The player configuration.
* @param {string} playerId - The player id.
*/
constructor(source: PKMediaSourceObject, config: any, playerId: string) {
super();
this._eventManager = new EventManager();
this._canLoadMediaSourceAdapterPromise = Promise.resolve();
this._createVideoElement(playerId);
this._init(source, config);
}
/**
* Restores the engine.
* @param {PKMediaSourceObject} source - The selected source object.
* @param {Object} config - The player configuration.
* @returns {void}
*/
public restore(source: PKMediaSourceObject, config: any): void {
this.reset();
this._init(source, config);
}
/**
* Resets the engine.
* @returns {void}
*/
public reset(): void {
if (this._reset) return;
this._reset = true;
this._eventManager.removeAll();
if (this._droppedFramesWatcher) {
this._droppedFramesWatcher.destroy();
this._droppedFramesWatcher = undefined;
}
this._canLoadMediaSourceAdapterPromise = new Promise((resolve, reject) => {
const mediaSourceAdapterDestroyed = this._mediaSourceAdapter ? this._mediaSourceAdapter.destroy() : Promise.resolve();
if (this._el && this._el.src) {
mediaSourceAdapterDestroyed.then(() => {
Utils.Dom.setAttribute(this._el, 'src', '');
Utils.Dom.removeAttribute(this._el, 'src');
resolve();
}, reject);
} else {
mediaSourceAdapterDestroyed.then(resolve, reject);
}
});
this._mediaSourceAdapter = null;
}
/**
* Destroys the engine.
* @public
* @returns {void}
*/
public destroy(): void {
this.detach();
if (this._el) {
this.pause();
Utils.Dom.removeAttribute(this._el, 'src');
Utils.Dom.removeChild(this._el.parentNode, this._el);
}
this._eventManager.destroy();
MediaSourceProvider.destroy();
if (this._droppedFramesWatcher) {
this._droppedFramesWatcher.destroy();
this._droppedFramesWatcher = undefined;
}
if (this._mediaSourceAdapter) {
this._mediaSourceAdapter.setCachedUrls([]);
this._mediaSourceAdapter.destroy();
this._mediaSourceAdapter = null;
}
}
/**
* Get the engine mediaSourceAdapter
* @public
* @returns {IMediaSourceAdapter | null}
*/
public get mediaSourceAdapter(): IMediaSourceAdapter | null {
return this._mediaSourceAdapter;
}
/**
* Get the engine's id
* @public
* @returns {string} the engine's id
*/
public get id(): string {
return Html5.id;
}
/**
* attach media - return the media source to handle the video tag
* @public
* @returns {void}
*/
public attachMediaSource(): void {
if (this._mediaSourceAdapter) {
this._mediaSourceAdapter.attachMediaSource();
}
}
/**
* detach media - will remove the media source from handling the video
* @public
* @returns {void}
*/
public detachMediaSource(): void {
if (this._mediaSourceAdapter) {
this._mediaSourceAdapter.detachMediaSource();
}
}
/**
* Listen to the video element events and triggers them from the engine.
* @public
* @returns {void}
*/
public attach(): void {
Object.keys(Html5EventType).forEach((html5Event) => {
if (![Html5EventType.ERROR, Html5EventType.WAITING].includes(Html5EventType[html5Event])) {
this._eventManager.listen(this._el, Html5EventType[html5Event], () => {
return this.dispatchEvent(new FakeEvent(Html5EventType[html5Event]));
});
}
});
this._eventManager.listen(this._el, Html5EventType.ERROR, () => this._handleVideoError());
this._eventManager.listen(this._el, Html5EventType.WAITING, () => this._handleWaiting());
this._handleMetadataTrackEvents();
this._eventManager.listen(this._el.textTracks, 'addtrack', (event: any) => {
if (PKTextTrack.isNativeTextTrack(event.track)) {
this.dispatchEvent(new FakeEvent(CustomEventType.TEXT_TRACK_ADDED, { track: event.track }));
}
});
const mediaSourceAdapter = this._mediaSourceAdapter;
if (mediaSourceAdapter) {
this._eventManager.listen(mediaSourceAdapter, CustomEventType.VIDEO_TRACK_CHANGED, (event: FakeEvent) => this.dispatchEvent(event));
this._eventManager.listen(mediaSourceAdapter, CustomEventType.AUDIO_TRACK_CHANGED, (event: FakeEvent) => this.dispatchEvent(event));
this._eventManager.listen(mediaSourceAdapter, CustomEventType.TEXT_TRACK_CHANGED, (event: FakeEvent) => this.dispatchEvent(event));
this._eventManager.listen(mediaSourceAdapter, CustomEventType.IMAGE_TRACK_CHANGED, (event: FakeEvent) => this.dispatchEvent(event));
this._eventManager.listen(mediaSourceAdapter, CustomEventType.ABR_MODE_CHANGED, (event: FakeEvent) => this.dispatchEvent(event));
this._eventManager.listen(mediaSourceAdapter, CustomEventType.TEXT_CUE_CHANGED, (event: FakeEvent) => this.dispatchEvent(event));
this._eventManager.listen(mediaSourceAdapter, CustomEventType.TRACKS_CHANGED, (event: FakeEvent) => this.dispatchEvent(event));
this._eventManager.listen(mediaSourceAdapter, CustomEventType.FRAG_LOADED, (event: FakeEvent) => this.dispatchEvent(event));
this._eventManager.listen(mediaSourceAdapter, CustomEventType.DRM_LICENSE_LOADED, (event: FakeEvent) => this.dispatchEvent(event));
this._eventManager.listen(mediaSourceAdapter, CustomEventType.MANIFEST_LOADED, (event: FakeEvent) => this.dispatchEvent(event));
this._eventManager.listen(mediaSourceAdapter, Html5EventType.ERROR, (event: FakeEvent) => this.dispatchEvent(event));
this._eventManager.listen(mediaSourceAdapter, Html5EventType.TIME_UPDATE, (event: FakeEvent) => this.dispatchEvent(event));
this._eventManager.listen(mediaSourceAdapter, Html5EventType.PLAYING, (event: FakeEvent) => this.dispatchEvent(event));
this._eventManager.listen(mediaSourceAdapter, Html5EventType.WAITING, (event: FakeEvent) => this.dispatchEvent(event));
this._eventManager.listen(mediaSourceAdapter, CustomEventType.MEDIA_RECOVERED, (event: FakeEvent) => this.dispatchEvent(event));
this._eventManager.listen(mediaSourceAdapter, CustomEventType.TIMED_METADATA_ADDED, (event: FakeEvent) => this.dispatchEvent(event));
this._eventManager.listen(mediaSourceAdapter, 'hlsFragParsingMetadata', (event: FakeEvent) => this.dispatchEvent(event));
if (this._droppedFramesWatcher) {
this._eventManager.listen(this._droppedFramesWatcher, CustomEventType.FPS_DROP, (event: FakeEvent) => this.dispatchEvent(event));
}
}
}
/**
* Remove the listeners of the video element events.
* @public
* @returns {void}
*/
public detach(): void {
Object.keys(Html5EventType).forEach((html5Event) => {
this._eventManager.unlisten(this._el, Html5EventType[html5Event]);
});
if (this._mediaSourceAdapter) {
this._eventManager.unlisten(this._mediaSourceAdapter, CustomEventType.VIDEO_TRACK_CHANGED);
this._eventManager.unlisten(this._mediaSourceAdapter, CustomEventType.AUDIO_TRACK_CHANGED);
this._eventManager.unlisten(this._mediaSourceAdapter, CustomEventType.TEXT_TRACK_CHANGED);
this._eventManager.unlisten(this._mediaSourceAdapter, CustomEventType.TEXT_CUE_CHANGED);
}
}
/**
* @returns {HTMLVideoElement} - The video element.
* @public
*/
public getVideoElement(): HTMLVideoElement {
return this._el;
}
/**
* Select a new video track.
* @param {VideoTrack} videoTrack - The video track object to set.
* @returns {void}
*/
public selectVideoTrack(videoTrack: VideoTrack): void {
if (this._mediaSourceAdapter) {
this._mediaSourceAdapter.selectVideoTrack(videoTrack);
}
}
/**
* Select a new audio track.
* @param {AudioTrack} audioTrack - The video track object to set.
* @returns {void}
*/
public selectAudioTrack(audioTrack: AudioTrack): void {
if (this._mediaSourceAdapter) {
this._mediaSourceAdapter.selectAudioTrack(audioTrack);
}
}
/**
* Select a new text track.
* @param {PKTextTrack} textTrack - The playkit text track object to set.
* @returns {void}
*/
public selectTextTrack(textTrack: PKTextTrack): void {
this._removeCueChangeListeners();
if (this._mediaSourceAdapter) {
this._mediaSourceAdapter.selectTextTrack(textTrack);
}
this.resetAllCues();
this._addCueChangeListener();
}
/**
* Select a new image track.
* @param {ImageTrack} imageTrack - The image track object to set.
* @returns {void}
*/
public selectImageTrack(imageTrack: ImageTrack): void {
if (this._mediaSourceAdapter) {
this._mediaSourceAdapter.selectImageTrack(imageTrack);
}
}
/**
* Hide the text track
* @function hideTextTrack
* @returns {void}
* @public
*/
public hideTextTrack(): void {
if (this._mediaSourceAdapter) {
this._mediaSourceAdapter.hideTextTrack();
}
this._removeCueChangeListeners();
}
/**
* Enables adaptive bitrate switching according to the media source extension logic.
* @function enableAdaptiveBitrate
* @returns {void}
* @public
*/
public enableAdaptiveBitrate(): void {
if (this._mediaSourceAdapter) {
this._mediaSourceAdapter.enableAdaptiveBitrate();
}
}
/**
* Checking if adaptive bitrate switching is enabled.
* @function isAdaptiveBitrateEnabled
* @returns {boolean} - Whether adaptive bitrate is enabled.
* @public
*/
public isAdaptiveBitrateEnabled(): boolean {
if (this._mediaSourceAdapter) {
return this._mediaSourceAdapter.isAdaptiveBitrateEnabled();
}
return false;
}
/**
* Apply ABR restriction
* @function applyABRRestriction
* @param {PKABRRestrictionObject} restriction - abr restriction config
* @returns {void}
* @public
*/
public applyABRRestriction(restriction: PKABRRestrictionObject): void {
if (this._mediaSourceAdapter) {
return this._mediaSourceAdapter.applyABRRestriction(restriction);
}
}
/**
* Seeking to live edge.
* @function seekToLiveEdge
* @returns {void}
* @public
*/
public seekToLiveEdge(): void {
if (this._mediaSourceAdapter) {
this._mediaSourceAdapter.seekToLiveEdge();
}
}
public isOnLiveEdge(): boolean {
if (this._mediaSourceAdapter) {
return this._mediaSourceAdapter.isOnLiveEdge();
}
return false;
}
/**
* Get the start time of DVR window in live playback in seconds.
* @returns {Number} - start time of DVR window.
* @public
*/
public getStartTimeOfDvrWindow(): number {
return this._mediaSourceAdapter ? this._mediaSourceAdapter.getStartTimeOfDvrWindow() : 0;
}
/**
* Checking if the current playback is live.
* @function isLive
* @returns {boolean} - Whether playback is live.
* @public
*/
public isLive(): boolean {
return this._mediaSourceAdapter ? this._mediaSourceAdapter.isLive() : false;
}
/**
* Start/resume playback.
* @public
* @returns {?Promise<*>} - play promise
*/
public play(): Promise<void> {
const playPromise = this._el.play();
if (playPromise) {
playPromise.catch((err) => this.dispatchEvent(new FakeEvent(CustomEventType.PLAY_FAILED, { error: err })));
}
return playPromise;
}
/**
* Pause playback.
* @public
* @returns {void}
*/
public pause(): void {
return this._el.pause();
}
/**
* Load media.
* @param {number} startTime - Optional time to start the video from.
* @public
* @returns {Promise<Object>} - The loaded data
*/
public load(startTime?: number): Promise<{ tracks: Track[] }> {
this._el.load();
return this._canLoadMediaSourceAdapterPromise
.then(() => {
return this._mediaSourceAdapter ? this._mediaSourceAdapter.load(startTime) : Promise.resolve({} as { tracks: Track[] });
})
.catch((error) => {
this.dispatchEvent(new FakeEvent(Html5EventType.ERROR, error));
return Promise.reject(error);
});
}
/**
* Request the engine to enter picture in picture mode
* @public
* @returns {void}
*/
public enterPictureInPicture(): void {
try {
// Currently it's supported in chrome and in safari. So if we consider checking support before,
// we can use this flag to distinguish between the two. In the future we might need a different method.
// Second condition is because flow does not support this API yet
if (document.pictureInPictureEnabled && typeof this._el.requestPictureInPicture === 'function' && !this._el.disablePictureInPicture) {
this._el.requestPictureInPicture().catch((error) => {
this.dispatchEvent(new FakeEvent(Html5EventType.ERROR, new Error(Error.Severity.RECOVERABLE, Error.Category.PLAYER, Error.Code.ENTER_PICTURE_IN_PICTURE_FAILED, error)));
});
// @ts-expect-error - Property 'webkitSetPresentationMode' does not exist on type 'HTMLVideoElement'
} else if (typeof this._el.webkitSetPresentationMode === 'function') {
// @ts-expect-error - Property 'webkitSetPresentationMode' does not exist on type 'HTMLVideoElement'
this._el.webkitSetPresentationMode('picture-in-picture');
// Safari does not fire this event but Chrome does, normalizing the behaviour
setTimeout(() => this.dispatchEvent(new FakeEvent(Html5EventType.ENTER_PICTURE_IN_PICTURE)), 0);
}
} catch (error) {
this.dispatchEvent(new FakeEvent(Html5EventType.ERROR, new Error(Error.Severity.RECOVERABLE, Error.Category.PLAYER, Error.Code.ENTER_PICTURE_IN_PICTURE_FAILED, error)));
}
}
/**
* Request the engine to exit picture in picture mode
* @public
* @returns {void}
*/
public exitPictureInPicture(): void {
try {
// Currently it's supported in chrome and in safari. So if we consider checking support before,
// we can use this flag to distinguish between the two. In the future we might need a different method.
// Second condition is because flow does not support this API yet
if (document.pictureInPictureEnabled && typeof document.exitPictureInPicture === 'function' && this._el === document.pictureInPictureElement) {
document.exitPictureInPicture().catch((error) => {
this.dispatchEvent(new FakeEvent(Html5EventType.ERROR, new Error(Error.Severity.RECOVERABLE, Error.Category.PLAYER, Error.Code.EXIT_PICTURE_IN_PICTURE_FAILED, error)));
});
} else if (typeof this._el['webkitSetPresentationMode'] === 'function') {
//@ts-expect-error - Property 'webkitSetPresentationMode' does not exist on type 'HTMLVideoElement'.
this._el.webkitSetPresentationMode('inline');
}
} catch (error) {
this.dispatchEvent(new FakeEvent(Html5EventType.ERROR, new Error(Error.Severity.RECOVERABLE, Error.Category.PLAYER, Error.Code.EXIT_PICTURE_IN_PICTURE_FAILED, error)));
}
}
/**
* Check if the engine is in picture in picture mode
* @public
* @return {boolean} if the engine is in picture in picture mode or not
*/
public isPictureInPictureSupported(): boolean {
// due to a bug in shaka pip_webkit which sets pictureInPictureEnabled to true in unsupported devices like iphones we will
// first rely on the response of webkitSupportsPresentationMode (if exists) and only if not on the pictureInPictureEnabled property
//@ts-expect-error - Property 'webkitSupportsPresentationMode' does not exist on type 'HTMLVideoElement'.
if (typeof this._el.webkitSupportsPresentationMode === 'function') {
//@ts-expect-error - Property 'webkitSupportsPresentationMode' does not exist on type 'HTMLVideoElement'.
return this._el.webkitSupportsPresentationMode('picture-in-picture');
} else {
return !!document.pictureInPictureEnabled;
}
}
/**
* Returns in-stream thumbnail for a chosen time.
* @param {number} time - playback time.
* @public
* @return {?ThumbnailInfo} - Thumbnail info
*/
public getThumbnail(time: number): ThumbnailInfo | null {
if (this._mediaSourceAdapter) {
return this._mediaSourceAdapter.getThumbnail(time);
}
return null;
}
/**
* Set a source.
* @param {string} source - Source to set.
* @public
* @returns {void}
*/
public set src(source: string) {
if (this._mediaSourceAdapter) {
this._mediaSourceAdapter.src = source;
}
}
/**
* Get the source url.
* @returns {string} - The source url.
* @public
*/
public get src(): string {
if (this._mediaSourceAdapter) {
return this._mediaSourceAdapter.src;
}
return '';
}
/**
* Get the current time in seconds.
* @returns {Number} - The current playback time.
* @public
*/
public get currentTime(): number {
return this._el ? this._el.currentTime : 0;
}
/**
* Set the current time in seconds.
* @param {Number} to - The number to set in seconds.
* @public
* @returns {void}
*/
public set currentTime(to: number) {
if (this._el) {
this._el.currentTime = to;
}
}
/**
* Get the duration in seconds.
* @returns {Number} - The playback duration.
* @public
*/
public get duration(): number {
return this._el.duration;
}
public get liveDuration(): number {
return this._mediaSourceAdapter ? this._mediaSourceAdapter.liveDuration : -1;
}
/**
* Set playback volume.
* @param {Number} vol - The volume to set.
* @public
* @returns {void}
*/
public set volume(vol: number) {
this._el.volume = vol;
}
/**
* Get playback volume.
* @returns {Number} - The volume value of the video element.
* @public
*/
public get volume(): number {
return this._el.volume;
}
/**
* Get paused state.
* @returns {boolean} - The paused value of the video element.
* @public
*/
public get paused(): boolean {
return this._el.paused;
}
/**
* Get seeking state.
* @returns {boolean} - The seeking value of the video element.
* @public
*/
public get seeking(): boolean {
return this._el.seeking;
}
/**
* Get the first seekable range (part) of the video in seconds.
* @returns {TimeRanges} - First seekable range (part) of the video in seconds.
* @public
*/
public get seekable(): TimeRanges {
return this._el.seekable;
}
/**
* Get the first played range (part) of the video in seconds.
* @returns {TimeRanges} - First played range (part) of the video in seconds.
* @public
*/
public get played(): TimeRanges {
return this._el.played;
}
/**
* Get the first buffered range (part) of the video in seconds.
* @returns {TimeRanges} - First buffered range (part) of the video in seconds.
* @public
*/
public get buffered(): TimeRanges {
return this._el.buffered;
}
/**
* Set player muted state.
* @param {boolean} mute - The new mute value.
* @public
* @returns {void}
*/
public set muted(mute: boolean) {
this._el.muted = mute;
}
/**
* Get player muted state.
* @returns {boolean} - The muted value of the video element.
* @public
*/
public get muted(): boolean {
return this._el.muted;
}
/**
* Get the default mute value.
* @returns {boolean} - The defaultMuted of the video element.
* @public
*/
public get defaultMuted(): boolean {
return this._el.defaultMuted;
}
/**
* Sets an image to be shown while the video is downloading, or until the user hits the play button.
* @param {string} poster - The image url to be shown.
* @returns {void}
* @public
*/
public set poster(poster: string) {
this._el.poster = poster;
}
/**
* Gets an image to be shown while the video is downloading, or until the user hits the play button.
* @returns {poster} - The image url.
* @public
*/
public get poster(): string {
return this._el.poster;
}
/**
* Specifies if and how the author thinks that the video should be loaded when the page loads.
* @param {string} preload - The preload value.
* @public
* @returns {void}
*/
public set preload(preload: 'none' | 'metadata' | 'auto' | '') {
this._el.preload = preload;
}
/**
* Gets the preload value of the video element.
* @returns {string} - The preload value.
* @public
*/
public get preload(): 'none' | 'metadata' | 'auto' | '' {
return this._el.preload;
}
/**
* Set if the video will automatically start playing as soon as it can do so without stopping.
* @param {boolean} autoplay - The autoplay value.
* @public
* @returns {void}
*/
public set autoplay(autoplay: boolean) {
this._el.autoplay = autoplay;
}
/**
* Gets the autoplay value of the video element.
* @returns {boolean} - The autoplay value.
* @public
*/
public get autoplay(): boolean {
return this._el.autoplay;
}
/**
* Set to specifies that the video will start over again, every time it is finished.
* @param {boolean} loop - the loop value.
* @public
* @returns {void}
*/
public set loop(loop: boolean) {
this._el.loop = loop;
}
/**
* Gets the loop value of the video element.
* @returns {boolean} - The loop value.
* @public
*/
public get loop(): boolean {
return this._el.loop;
}
/**
* Set to specifies that video controls should be displayed.
* @param {boolean} controls - the controls value.
* @public
* @returns {void}
*/
public set controls(controls: boolean) {
this._el.controls = controls;
}
/**
* Gets the controls value of the video element.
* @returns {boolean} - The controls value.
* @public
*/
public get controls(): boolean {
return this._el.controls;
}
/**
* Sets the current playback speed of the audio/video.
* @param {Number} playbackRate - The playback speed value.
* @public
* @returns {void}
*/
public set playbackRate(playbackRate: number) {
this._el.playbackRate = playbackRate;
}
/**
* Gets the current playback speed of the audio/video.
* @returns {Number} - The current playback speed value.
* @public
*/
public get playbackRate(): number {
return this._el.playbackRate;
}
/**
* Sets the default playback speed of the audio/video.
* @param {Number} defaultPlaybackRate - The default playback speed value.
* @public
* @returns {void}
*/
public set defaultPlaybackRate(defaultPlaybackRate: number) {
this._el.defaultPlaybackRate = defaultPlaybackRate;
}
/**
* Gets the default playback speed of the audio/video.
* @returns {Number} - The default playback speed value.
* @public
*/
public get defaultPlaybackRate(): number {
return this._el.defaultPlaybackRate;
}
/**
* The ended property returns whether the playback of the audio/video has ended.
* @returns {boolean} - The ended value.
* @public
*/
public get ended(): boolean {
return this._el.ended;
}
/**
* The error property returns a MediaError object.
* @returns {MediaError} - The MediaError object has a code property containing the error state of the audio/video.
* @public
*/
public get error(): MediaError | null {
return this._el.error;
}
/**
* @returns {Number} - The current network state (activity) of the audio/video.
* @public
*/
public get networkState(): number {
return this._el.networkState;
}
/**
* Indicates if the audio/video is ready to play or not.
* @returns {Number} - The current ready state of the audio/video.
* 0 = HAVE_NOTHING - no information whether or not the audio/video is ready.
* 1 = HAVE_METADATA - metadata for the audio/video is ready.
* 2 = HAVE_CURRENT_DATA - data for the current playback position is available, but not enough data to play next frame/millisecond.
* 3 = HAVE_FUTURE_DATA - data for the current and at least the next frame is available.
* 4 = HAVE_ENOUGH_DATA - enough data available to start playing.
*/
public get readyState(): number {
return this._el.readyState;
}
/**
* @returns {Number} - The height of the video player, in pixels.
* @public
*/
public get videoHeight(): number {
return this._el.videoHeight;
}
/**
* @returns {Number} - The width of the video player, in pixels.
* @public
*/
public get videoWidth(): number {
return this._el.videoWidth;
}
/**
* @param {boolean} playsinline - Whether to set on the video tag the playsinline attribute.
*/
public set playsinline(playsinline: boolean) {
if (playsinline) {
this._el.setAttribute('playsinline', '');
} else {
this._el.removeAttribute('playsinline');
}
}
/**
* @returns {boolean} - Whether the video tag has an attribute of playsinline.
*/
public get playsinline(): boolean {
return this._el.getAttribute('playsinline') === '';
}
/**
* Set crossOrigin attribute.
* @param {?string} crossOrigin - 'anonymous' or 'use-credentials'
*/
public set crossOrigin(crossOrigin: string) {
if (typeof crossOrigin === 'string') {
this._el.setAttribute('crossorigin', crossOrigin);
} else {
this._el.removeAttribute('crossorigin');
}