-
Notifications
You must be signed in to change notification settings - Fork 35
/
player.ts
2932 lines (2730 loc) · 87.6 KB
/
player.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 Env from './utils/env';
import { EventManager } from './event/event-manager';
import PosterManager from './utils/poster-manager';
import { FakeEvent } from './event/fake-event';
import { FakeEventTarget } from './event/fake-event-target';
import { IEngine, IEngineStatic, LoggerLevels } from './types';
import { CustomEventType, EventType, Html5EventType } from './event/event-type';
import * as Utils from './utils/util';
import Locale from './utils/locale';
import getLogger, { getLogLevel, LogLevel, LogLevelType, setLogHandler, setLogLevel } from './utils/logger';
import StateManager from './state/state-manager';
import Track from './track/track';
import VideoTrack from './track/video-track';
import AudioTrack, { audioDescriptionTrackHandler } from './track/audio-track';
import { PKTextTrack } from './track/text-track';
import TextStyle from './track/text-style';
import { processCues } from './track/text-track-display';
import { StateType } from './state/state-type';
import { TrackType, TrackTypes } from './track/track-type';
import { StreamType } from './engines/stream-type';
import { EngineType } from './engines/engine-type';
import { MediaType } from './enums/media-type';
import { AbrMode } from './track/abr-mode-type';
import { CorsType } from './engines/html5/cors-types';
import PlaybackMiddleware from './middleware/playback-middleware';
import { DefaultConfig, DefaultSources } from './player-config.js';
import './assets/style.css';
import PKError from './error/error';
import { EngineProvider } from './engines/engine-provider';
import { ExternalCaptionsHandler } from './track/external-captions-handler';
import { AdBreakType } from './ads/ad-break-type';
import { AdTagType } from './ads/ad-tag-type';
import { ResizeWatcher, getSubtitleStyleSheet, resetSubtitleStyleSheet } from './utils';
import { FullscreenController } from './fullscreen/fullscreen-controller';
import { EngineDecorator, EngineDecoratorType } from './engines/engine-decorator';
import { LabelOptions } from './track/label-options';
import { AutoPlayType } from './enums/auto-play-type';
import ImageTrack from './track/image-track';
import { ThumbnailInfo } from './thumbnail/thumbnail-info';
import { EngineDecoratorManager } from './engines/engine-decorator-manager';
import { filterTracksByRestriction } from './utils/restrictions';
import { ExternalThumbnailsHandler } from './thumbnail/external-thumbnails-handler';
import {
IEngineDecoratorProvider,
PKDimensionsConfig,
PKDrmDataObject,
PKEventTypes,
PKMediaSourceObject,
PKMetadataConfigObject,
PKPlayerDimensions,
PKPlayOptionsObject,
PKSourcesConfigObject,
PKStatsObject,
PKTextTrackDisplaySettingObject
} from './types';
import { ILogger, ILogLevel } from 'js-logger';
import { IEnv } from './types/ua-parser';
/**
* The black cover class name.
* @type {string}
* @const
*/
const BLACK_COVER_CLASS_NAME: string = 'playkit-black-cover';
/**
* The player container class name.
* @type {string}
* @const
*/
const CONTAINER_CLASS_NAME: string = 'playkit-container';
/**
/**
* The player poster class name.
* @type {string}
* @const
*/
const POSTER_CLASS_NAME: string = 'playkit-poster';
/**
* The video filter class name.
* @type {string}
* @const
*/
const VIDEO_FILTER_CLASS_NAME: string = 'playkit-video-filter';
/**
* The engine class name.
* @type {string}
* @const
*/
const ENGINE_CLASS_NAME: string = 'playkit-engine';
/**
* The subtitles class name.
* @type {string}
* @const
*/
const SUBTITLES_CLASS_NAME: string = 'playkit-subtitles';
/**
* The auto string, for captions
* @type {string}
* @const
*/
const AUTO: string = 'auto';
/**
* The off string, for captions
* @type {string}
* @const
*/
const OFF: string = 'off';
/**
* The duration offset, for seeking to duration safety.
* @type {number}
* @const
*/
const DURATION_OFFSET: number = 0.1;
/**
* The threshold in seconds from duration that we still consider it as live edge
* @type {number}
* @const
*/
const LIVE_EDGE_THRESHOLD: number = 1;
/**
* The HTML5 player class.
* @classdesc
*/
export default class Player extends FakeEventTarget {
/**
* The player class logger.
* @type {any}
* @static
* @private
*/
public static _logger: ILogger = getLogger('Player');
/**
* Runs the engines capabilities tests.
* @returns {void}
* @public
* @static
*/
public static runCapabilities(): void {
Player._logger.debug('Running player capabilities');
EngineProvider.getEngines().forEach((Engine) => Engine.runCapabilities());
}
/**
* Gets the engines capabilities.
* @param {?string} engineType - The engine type.
* @return {Promise<Object>} - The engines capabilities object.
* @public
* @static
*/
public static getCapabilities(engineType?: string): Promise<{ [name: string]: any }> {
Player._logger.debug('Get player capabilities', engineType);
const promises: { [name: string]: any }[] = [];
EngineProvider.getEngines().forEach((Engine) => promises.push(Engine.getCapabilities()));
return Promise.all(promises).then((arrayOfResults) => {
const playerCapabilities = {};
arrayOfResults.forEach((res) => Object.assign(playerCapabilities, res));
return engineType ? playerCapabilities[engineType] : playerCapabilities;
});
}
/**
* Sets an engine capabilities.
* @param {string} engineType - The engine type.
* @param {Object} capabilities - The engine capabilities.
* @returns {void}
* @public
* @static
*/
public static setCapabilities(engineType: string, capabilities: { [name: string]: any }): void {
Player._logger.debug('Set player capabilities', engineType, capabilities);
const selectedEngine = EngineProvider.getEngines().find((Engine) => Engine.id === engineType);
if (selectedEngine) {
selectedEngine.setCapabilities(capabilities);
}
}
/**
* The event manager of the player.
* @type {EventManager}
* @private
*/
private _eventManager!: EventManager;
/**
* The poster manager of the player.
* @type {PosterManager}
* @private
*/
private _posterManager!: PosterManager;
/**
* The runtime configuration of the player.
* @type {Object}
* @private
*/
private _config: any;
/**
* The current sources object.
* @type {PKSourcesConfigObject}
* @private
*/
private _sources: PKSourcesConfigObject = {} as PKSourcesConfigObject;
/**
* The playback engine.
* @type {IEngine}
* @private
*/
private _engine!: IEngine;
/**
* The state manager of the player.
* @type {StateManager}
* @private
*/
private _stateManager!: StateManager;
/**
* The tracks of the player.
* @type {Array<Track | TextTrack | AudioTrack | VideoTrack>}
* @private
*/
private _tracks: Array<Track | PKTextTrack | AudioTrack | VideoTrack>;
/**
* The player ready promise
* @type {Promise<*>}
* @private
*/
private _readyPromise?: Promise<void>;
/**
* Whether the play is the first or not
* @type {boolean}
* @private
*/
private _firstPlay!: boolean;
/**
* Whether the playing is the first or not
* @type {boolean}
* @private
*/
private _firstPlaying!: boolean;
/**
* Whether the playback already start
* @type {boolean}
* @private
*/
private _playbackStart!: boolean;
/**
* If quality has changed after playback ended - pend the change
* @type {boolean}
* @private
*/
private _pendingSelectedVideoTrack: VideoTrack | undefined;
/**
* The available playback rates for the player.
* @type {Array<number>}
* @private
*/
private _playbackRates!: Array<number>;
/**
* The player DOM element container.
* @type {HTMLDivElement}
* @private
*/
private _el!: HTMLDivElement;
/**
* The player text DOM element container.
* @type {HTMLDivElement}
* @private
*/
private _textDisplayEl!: HTMLDivElement;
/**
* The player black cover div.
* @type {HTMLDivElement}
* @private
*/
private _blackCoverEl!: HTMLDivElement;
/**
* The player DOM id.
* @type {string}
* @private
*/
private _playerId!: string;
/**
* The player last updated text cues list
* @type {Array<any>}
* @private
*/
private _activeTextCues: VTTCue[] = [];
/**
* The player text disaply settings
* @type {PKTextTrackDisplaySettingObject}
* @private
*/
private _textDisplaySettings: PKTextTrackDisplaySettingObject = {} as PKTextTrackDisplaySettingObject;
/**
* The player text style settings
* @type {TextStyle}
* @private
*/
private _textStyle!: TextStyle;
/**
* The playback middleware of the player.
* @type {PlaybackMiddleware}
* @private
*/
private _playbackMiddleware!: PlaybackMiddleware;
/**
* The environment(os,device,browser) object of the player.
* @type {Object}
* @private
*/
private _env!: IEnv;
/**
* The currently selected engine type
* @type {string}
* @private
*/
private _engineType!: string;
/**
* The currently selected stream type
* @type {string}
* @private
*/
private _streamType!: string;
/**
* The current playback attributes state
* @type {Object}
* @private
*/
private _playbackAttributesState: { [attribute: string]: any } = {
muted: undefined,
volume: undefined,
rate: undefined,
videoTrack: undefined,
audioLanguage: '',
textLanguage: '',
captionsDisplay: undefined
};
/**
* Whether a load media request has sent, the player should wait to media.
* @type {boolean}
* @private
*/
private _loadingMedia!: boolean;
/**
* Whether the player is loading a source.
* @type {boolean}
* @private
*/
private _loading!: boolean;
/**
* Reset indicator state.
* @type {boolean}
* @private
*/
private _reset!: boolean;
/**
* Destroyed indicator state.
* @type {boolean}
* @private
*/
private _destroyed!: boolean;
/**
* Fallback to muted auto play mode indicator.
* @type {boolean}
* @private
*/
private _fallbackToMutedAutoPlay!: boolean;
/**
* holds the external tracks handler controller
* @type {ExternalCaptionsHandler}
* @private
*/
private _externalCaptionsHandler!: ExternalCaptionsHandler;
/**
* holds the external tracks handler controller
* @type {ExternalCaptionsHandler}
* @private
*/
private _externalThumbnailsHandler!: ExternalThumbnailsHandler;
/**
* holds the full screen controller
* @type {FullscreenController}
* @private
*/
private _fullscreenController!: FullscreenController;
/**
* holds the resize observer. Incharge of notifying on resize changes.
* @type {?AdsController}
* @private
*/
private _resizeWatcher!: ResizeWatcher;
/**
* Whether the user interacted with the player
* @type {boolean}
* @private
*/
private _hasUserInteracted: boolean = false;
/**
* Whether the video is seeked to live edge
* @type {boolean}
* @private
*/
private _isOnLiveEdge: boolean = false;
/**
* Whether should load after attach media used
* @type {boolean}
* @private
*/
private _shouldLoadAfterAttach: boolean = false;
/**
* The aspect ratio of the player.
* @type {?string}
* @private
*/
private _aspectRatio!: string | null;
/**
* The engine decorator manager.
* @type {?EngineDecoratorManager}
* @private
*/
private _engineDecoratorManager!: EngineDecoratorManager;
private _cachedUrls: string[] = [];
/**
* @param {Object} config - The configuration for the player instance.
* @constructor
*/
constructor(config: any = {}) {
super();
this._setConfigLogLevel(config);
this._playerId = Utils.Generator.uniqueId(5);
this._prepareVideoElement();
Player.runCapabilities();
this._env = Env;
this._tracks = [];
this._firstPlay = true;
this._loadingMedia = false;
this._loading = false;
this._playbackStart = false;
this._firstPlaying = false;
this._reset = true;
this._destroyed = false;
this._fallbackToMutedAutoPlay = false;
this._config = Player._defaultConfig;
this._sources = Utils.Object.copyDeep(DefaultSources);
this._eventManager = new EventManager();
this._posterManager = new PosterManager();
this._stateManager = new StateManager(this);
this._resizeWatcher = new ResizeWatcher();
this._playbackMiddleware = new PlaybackMiddleware();
this._textStyle = new TextStyle();
this._createReadyPromise();
this._createPlayerContainer();
this._appendDomElements();
this._externalCaptionsHandler = new ExternalCaptionsHandler(this);
this._externalThumbnailsHandler = new ExternalThumbnailsHandler();
this._fullscreenController = new FullscreenController(this);
this.configure(config);
}
// <editor-fold desc="Public API">
// <editor-fold desc="Playback API">
/**
* Configures the player according to a given configuration.
* @param {Object} config - The configuration for the player instance.
* @returns {void}
*/
public configure(config: any = {}): void {
this._setConfigLogLevel(config);
Utils.Object.mergeDeep(this._config, config);
this._applyTextTrackConfig();
this._applyABRRestriction(config);
}
/**
* Configures the player metadata according to a given configuration.
* @param {PKMetadataConfigObject} sourcesMetadata - The sources metadata for the player instance.
* @returns {void}
*/
public setSourcesMetadata(sourcesMetadata: PKMetadataConfigObject): void {
if (this._sources) {
if (!this._sources.metadata) {
this._sources.metadata = {};
}
Utils.Object.mergeDeep(this._sources.metadata, sourcesMetadata);
}
}
/**
* Configures the player according to a given configuration.
* @param {PKSourcesConfigObject} sources - The sources for the player instance.
* @returns {void}
*/
public setSources(sources: PKSourcesConfigObject): void {
if (this._hasSources(sources)) {
this.reset();
Utils.Object.mergeDeep(this._sources, sources);
this._resizeWatcher.init(Utils.Dom.getElementById(this._playerId));
Player._logger.debug('Change source started');
this.dispatchEvent(new FakeEvent(CustomEventType.CHANGE_SOURCE_STARTED));
this._reset = false;
if (this._selectEngineByPriority()) {
this.dispatchEvent(new FakeEvent(CustomEventType.SOURCE_SELECTED, { selectedSource: this._sources[this._streamType] }));
this._attachMedia();
this._handlePlaybackOptions();
this._posterManager.setSrc(this._sources.poster);
this._handleDimensions();
this._handlePreload();
this._handleAutoPlay();
Player._logger.debug('Change source ended');
this.dispatchEvent(new FakeEvent(CustomEventType.CHANGE_SOURCE_ENDED));
} else {
Player._logger.warn('No playable engines was found to play the given sources');
this.dispatchEvent(
new FakeEvent(Html5EventType.ERROR, new PKError(PKError.Severity.CRITICAL, PKError.Category.PLAYER, PKError.Code.NO_ENGINE_FOUND_TO_PLAY_THE_SOURCE, 'No Engine Found To Play The Source'))
);
}
} else {
Utils.Object.mergeDeep(this._sources, sources);
}
}
/**
* The player readiness
* @public
* @returns {Promise<*>} - The ready promise
*/
public ready(): Promise<void> {
return this._readyPromise ? this._readyPromise : Promise.resolve();
}
/**
* Load media
* @public
* @returns {void}
*/
public load(): void {
const loadPlayer = (): void => {
if (this._engine) {
this._load();
} else {
this._eventManager.listenOnce(this, CustomEventType.SOURCE_SELECTED, () => this._load());
}
};
if (!this.src) {
this._playbackMiddleware.load(() => loadPlayer());
} else {
Player._logger.debug('The source has already been loaded. load request ignored');
}
}
/**
* Start/resume playback.
* @param {PKPlayOptionsObject} playOptions - additional options to control the play.
* @param {boolean} playOptions.programmatic - if true, the play call was not initiated by a user gesture and should be handled like auto play.
* @returns {void}
* @public
*/
public play(playOptions?: PKPlayOptionsObject): void {
if (playOptions && playOptions.programmatic) {
this._autoPlay();
return;
}
if (!this._playbackStart) {
this._playbackStart = true;
this.dispatchEvent(new FakeEvent(CustomEventType.PLAYBACK_START));
if (!this.src) {
this._prepareVideoElement();
}
this.load();
}
if (this._engine) {
this._playbackMiddleware.play(() => this._play());
} else if (this._loadingMedia) {
// load media requested but the response is delayed
this._playbackMiddleware.play(() => this._playAfterAsyncMiddleware());
} else {
this.dispatchEvent(new FakeEvent(Html5EventType.ERROR, new PKError(PKError.Severity.CRITICAL, PKError.Category.PLAYER, PKError.Code.NO_SOURCE_PROVIDED, 'No Source Provided')));
}
}
/**
* Pause playback.
* @returns {void}
* @public
*/
public pause(): void {
if (this._engine) {
this._playbackMiddleware.pause(this._pause.bind(this));
}
}
/**
* Gets the view of the player (i.e the dom container object).
* @return {HTMLElement} - The dom container.
* @public
*/
public getView(): HTMLElement {
return this._el;
}
/**
* @returns {HTMLVideoElement} - The video element.
* @public
*/
public getVideoElement(): HTMLVideoElement | undefined {
if (this._engine) {
return this._engine.getVideoElement();
}
}
/**
* Get video height.
* @returns {?number} - The intrinsic height of the video.
* @public
*/
public get videoHeight(): number | null {
if (this._engine) {
return this._engine.videoHeight;
}
return null;
}
/**
* Get video width.
* @returns {?number} - The intrinsic width of the video.
* @public
*/
public get videoWidth(): number | null {
if (this._engine) {
return this._engine.videoWidth;
}
return null;
}
/**
* Resets the necessary components before change media.
* @public
* @param {boolean} isChangeMedia - Whether or not this reset triggered due to change media
* @returns {void}
*/
public reset(isChangeMedia: boolean = false): void {
if (this._reset) return;
this.pause();
//make sure all services are reset before engine and engine attributes are reset
this._externalCaptionsHandler.reset();
this._externalThumbnailsHandler.reset();
this._posterManager.reset();
this._stateManager.reset();
this._sources = Utils.Object.copyDeep(DefaultSources);
this._activeTextCues = [];
this._updateTextDisplay([]);
this._tracks = [];
PKTextTrack.reset();
this._resetStateFlags();
this._engineType = '';
this._streamType = '';
this._pendingSelectedVideoTrack = undefined;
if (this._engine) {
this._engine.reset();
}
this.showBlackCover();
this._reset = true;
this.dispatchEvent(new FakeEvent(CustomEventType.PLAYER_RESET, { isChangeMedia }));
this._eventManager.removeAll();
this._resizeWatcher.init(Utils.Dom.getElementById(this._playerId));
this._createReadyPromise();
this._isOnLiveEdge = false;
this._shouldLoadAfterAttach = false;
}
/**
* Destroys the player.
* @returns {void}
* @public
*/
public destroy(): void {
if (this._destroyed) return;
//make sure all services are destroyed before engine and engine attributes are destroyed
this._externalCaptionsHandler.destroy();
this._posterManager.destroy();
this._stateManager.destroy();
this._fullscreenController.destroy();
this._activeTextCues = [];
this._textDisplaySettings = {} as PKTextTrackDisplaySettingObject;
this._config = {};
this._tracks = [];
this._engineType = '';
this._streamType = '';
this._readyPromise = undefined;
this._pendingSelectedVideoTrack = undefined;
this._resetStateFlags();
this._playbackAttributesState = {};
if (this._engine) {
this._engine.destroy();
}
if (this._engineDecoratorManager) {
this._engineDecoratorManager.destroy();
}
this._resizeWatcher.destroy();
if (this._el) {
Utils.Dom.removeChild(this._el.parentNode, this._el);
}
this._destroyed = true;
this.dispatchEvent(new FakeEvent(CustomEventType.PLAYER_DESTROY));
this._eventManager.destroy();
}
/**
* Hides the black cover div.
* @public
* @returns {void}
*/
public hideBlackCover(): void {
if (this._blackCoverEl) {
this._blackCoverEl.style.visibility = 'hidden';
}
}
/**
* Shows the black cover div.
* @public
* @returns {void}
*/
public showBlackCover(): void {
if (this._blackCoverEl) {
this._blackCoverEl.style.visibility = 'visible';
}
}
/**
* Attach the engine's media source
* @public
* @returns {void}
*/
public attachMediaSource(): void {
if (this._engine) {
this._shouldLoadAfterAttach = true;
this._engine.attachMediaSource();
this._eventManager.listenOnce(this, Html5EventType.CAN_PLAY, () => {
if (typeof this._playbackAttributesState.rate === 'number') {
this.playbackRate = this._playbackAttributesState.rate;
}
});
}
}
/**
* detach the engine's media source
* @public
* @returns {void}
*/
public detachMediaSource(): void {
if (this._engine) {
this.pause();
this.hideTextTrack();
this._shouldLoadAfterAttach = false;
this._createReadyPromise();
this._engine.detachMediaSource();
}
}
/**
* detach the engine's media source
* @public
* @returns {void}
* @param {IEngineDecoratorProvider} engineDecoratorProvider - function to create the decorator
*/
public registerEngineDecoratorProvider(engineDecoratorProvider: IEngineDecoratorProvider): void {
if (!this._engineDecoratorManager) {
this._engineDecoratorManager = new EngineDecoratorManager();
}
if (engineDecoratorProvider) {
this._engineDecoratorManager.register(engineDecoratorProvider);
}
}
/**
* Get the first buffered range of the engine.
* @returns {TimeRanges} - First buffered range of the engine in seconds.
* @public
*/
public get buffered(): TimeRanges | null {
if (this._engine) {
return this._engine.buffered;
}
return null;
}
public get stats(): PKStatsObject {
const statsObject: PKStatsObject = {
targetBuffer: NaN,
availableBuffer: NaN
};
if (this._engine) {
statsObject.targetBuffer = this._engine.targetBuffer;
statsObject.availableBuffer = this._engine.availableBuffer;
}
return statsObject;
}
/**
* Set the current time in seconds.
* @param {Number} to - The number to set in seconds.
* @public
*/
public set currentTime(to: number) {
this._playbackMiddleware.setCurrentTime(to, this._setCurrentTime.bind(this));
}
/**
* Get the current time in seconds.
* @returns {?Number} - The playback current time.
* @public
*/
public get currentTime(): number | null {
if (this._engine) {
return this._engine.currentTime;
}
return null;
}
/**
* Get the duration in seconds.
* @returns {?Number} - The playback duration.
* @public
*/
public get duration(): number | null {
if (this._engine) {
return this._engine.duration;
}
return null;
}
/**
* Get the live duration in seconds.
* @returns {?Number} - The live duration.
* @public
*/
public get liveDuration(): number | null {
if (this._engine) {
return this._engine.liveDuration;
}
return null;
}
/**
* Set playback volume.
* @param {Number} vol - The volume to set.
* @returns {void}
* @public
*/
public set volume(vol: number) {
if (this._engine) {
if (Utils.Number.isFloat(vol) || vol === 0 || vol === 1) {
let boundedVol = vol;
if (boundedVol < 0) {
boundedVol = 0;
}
if (boundedVol > 1) {
boundedVol = 1;
}
this._engine.volume = boundedVol;
}
}
}
/**
* Get playback volume.
* @returns {?Number} - The playback volume.
* @public
*/
public get volume(): number | null {
if (this._engine) {
return this._engine.volume;
}
return null;
}
/**
* Get paused state.
* @returns {?boolean} - Whether the video is paused or not.
* @public
*/
public get paused(): boolean | null {
if (this._engine) {
return this._engine.paused;
}
return null;
}
/**
* Get seeking state.
* @returns {?boolean} - Whether the video is seeking or not.
* @public
*/
public get seeking(): boolean | null {
if (this._engine) {
return this._engine.seeking;
}
return null;
}
/**
* Set playsinline attribute.
* Relevant for iOS 10 and up:
* Elements will now be allowed to play inline, and will not automatically enter fullscreen mode when playback begins.
* @param {boolean} playsinline - Whether the video should plays in line.
*/
public set playsinline(playsinline: boolean) {
if (this._engine) {
this._engine.playsinline = playsinline;
}
}
/**
* Get playsinline attribute.
* Relevant for iOS 10 and up:
* Elements will now be allowed to play inline, and will not automatically enter fullscreen mode when playback begins.
* @returns {boolean} - Whether the video plays in line.
*/
public get playsinline(): boolean | null {
if (this._engine) {
return this._engine.playsinline;
}
return null;
}
/**
* Set player muted state.
* @param {boolean} mute - The mute value.
* @returns {void}
* @public
*/
public set muted(mute: boolean) {
if (this._engine) {
this._engine.muted = mute;
this.dispatchEvent(new FakeEvent(CustomEventType.MUTE_CHANGE, { mute: mute }));
if (mute === false) {
this._fallbackToMutedAutoPlay = mute;
}
}
}
/**
* Get player muted state.
* @returns {?boolean} - Whether the video is muted or not.
* @public
*/
public get muted(): boolean | null {
if (this._engine) {
return this._engine.muted;
}
return null;
}
/**
* Get the player source.
* @returns {?string} - The current source of the player.
* @public
*/
public get src(): string | null {
if (this._engine) {
return this._engine.src;
}
return null;
}
/**
* Sets the dimensions of the player.
* @param {PKDimensionsConfig} dimensions - the player dimensions config.
* @returns {void}
* @public
*/
public set dimensions(dimensions: PKDimensionsConfig) {
const targetElement = this._getTargetElement();
if (!dimensions || Utils.Object.isEmptyObject(dimensions)) {
this._aspectRatio = null;
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
targetElement.style.width = null;
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
targetElement.style.height = null;
} else {
const { height, width } = Utils.Object.mergeDeep(this.dimensions, dimensions);
targetElement.style.width = typeof width === 'number' ? `${width}px` : width;