forked from video-dev/hls.js
-
Notifications
You must be signed in to change notification settings - Fork 3
/
hls.ts
1178 lines (1068 loc) · 34.4 KB
/
hls.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 { buildAbsoluteURL } from 'url-toolkit';
import PlaylistLoader from './loader/playlist-loader';
import ID3TrackController from './controller/id3-track-controller';
import LatencyController from './controller/latency-controller';
import LevelController from './controller/level-controller';
import { FragmentTracker } from './controller/fragment-tracker';
import KeyLoader from './loader/key-loader';
import StreamController from './controller/stream-controller';
import { isMSESupported, isSupported } from './is-supported';
import { getMediaSource } from './utils/mediasource-helper';
import { logger, enableLogs } from './utils/logger';
import { enableStreamingMode, hlsDefaultConfig, mergeConfig } from './config';
import { EventEmitter } from 'eventemitter3';
import { Events } from './events';
import { ErrorTypes, ErrorDetails } from './errors';
import { isHdcpLevel, type HdcpLevel, type Level } from './types/level';
import type { HlsEventEmitter, HlsListeners } from './events';
import type AudioTrackController from './controller/audio-track-controller';
import type AbrController from './controller/abr-controller';
import type BufferController from './controller/buffer-controller';
import type CapLevelController from './controller/cap-level-controller';
import type CMCDController from './controller/cmcd-controller';
import type EMEController from './controller/eme-controller';
import type SubtitleTrackController from './controller/subtitle-track-controller';
import type {
AbrComponentAPI,
ComponentAPI,
NetworkComponentAPI,
} from './types/component-api';
import type {
AudioSelectionOption,
MediaPlaylist,
SubtitleSelectionOption,
VideoSelectionOption,
} from './types/media-playlist';
import type { HlsConfig } from './config';
import type { BufferInfo } from './utils/buffer-helper';
import type AudioStreamController from './controller/audio-stream-controller';
import type BasePlaylistController from './controller/base-playlist-controller';
import type BaseStreamController from './controller/base-stream-controller';
import type ContentSteeringController from './controller/content-steering-controller';
import type ErrorController from './controller/error-controller';
import type FPSController from './controller/fps-controller';
/**
* The `Hls` class is the core of the HLS.js library used to instantiate player instances.
* @public
*/
export default class Hls implements HlsEventEmitter {
private static defaultConfig: HlsConfig | undefined;
/**
* The runtime configuration used by the player. At instantiation this is combination of `hls.userConfig` merged over `Hls.DefaultConfig`.
*/
public readonly config: HlsConfig;
/**
* The configuration object provided on player instantiation.
*/
public readonly userConfig: Partial<HlsConfig>;
private coreComponents: ComponentAPI[];
private networkControllers: NetworkComponentAPI[];
private started: boolean = false;
private _emitter: HlsEventEmitter = new EventEmitter();
private _autoLevelCapping: number = -1;
private _maxHdcpLevel: HdcpLevel = null;
private abrController: AbrComponentAPI;
private bufferController: BufferController;
private capLevelController: CapLevelController;
private latencyController: LatencyController;
private levelController: LevelController;
private streamController: StreamController;
private audioTrackController: AudioTrackController;
private subtitleTrackController: SubtitleTrackController;
private emeController: EMEController;
private cmcdController: CMCDController;
private _media: HTMLMediaElement | null = null;
private url: string | null = null;
private triggeringException?: boolean;
/**
* Get the video-dev/hls.js package version.
*/
static get version(): string {
return __VERSION__;
}
/**
* Check if the required MediaSource Extensions are available.
*/
static isMSESupported(): boolean {
return isMSESupported();
}
/**
* Check if MediaSource Extensions are available and isTypeSupported checks pass for any baseline codecs.
*/
static isSupported(): boolean {
return isSupported();
}
/**
* Get the MediaSource global used for MSE playback (ManagedMediaSource, MediaSource, or WebKitMediaSource).
*/
static getMediaSource(): typeof MediaSource | undefined {
return getMediaSource();
}
static get Events(): typeof Events {
return Events;
}
static get ErrorTypes(): typeof ErrorTypes {
return ErrorTypes;
}
static get ErrorDetails(): typeof ErrorDetails {
return ErrorDetails;
}
/**
* Get the default configuration applied to new instances.
*/
static get DefaultConfig(): HlsConfig {
if (!Hls.defaultConfig) {
return hlsDefaultConfig;
}
return Hls.defaultConfig;
}
/**
* Replace the default configuration applied to new instances.
*/
static set DefaultConfig(defaultConfig: HlsConfig) {
Hls.defaultConfig = defaultConfig;
}
/**
* Creates an instance of an HLS client that can attach to exactly one `HTMLMediaElement`.
* @param userConfig - Configuration options applied over `Hls.DefaultConfig`
*/
constructor(userConfig: Partial<HlsConfig> = {}) {
enableLogs(userConfig.debug || false, 'Hls instance');
const config = (this.config = mergeConfig(Hls.DefaultConfig, userConfig));
this.userConfig = userConfig;
if (config.progressive) {
enableStreamingMode(config);
}
// core controllers and network loaders
const {
abrController: ConfigAbrController,
bufferController: ConfigBufferController,
capLevelController: ConfigCapLevelController,
errorController: ConfigErrorController,
fpsController: ConfigFpsController,
} = config;
const errorController = new ConfigErrorController(this);
const abrController = (this.abrController = new ConfigAbrController(this));
const bufferController = (this.bufferController =
new ConfigBufferController(this));
const capLevelController = (this.capLevelController =
new ConfigCapLevelController(this));
const fpsController = new ConfigFpsController(this);
const playListLoader = new PlaylistLoader(this);
const id3TrackController = new ID3TrackController(this);
const ConfigContentSteeringController = config.contentSteeringController;
// ConentSteeringController is defined before LevelController to receive Multivariant Playlist events first
const contentSteering = ConfigContentSteeringController
? new ConfigContentSteeringController(this)
: null;
const levelController = (this.levelController = new LevelController(
this,
contentSteering,
));
// FragmentTracker must be defined before StreamController because the order of event handling is important
const fragmentTracker = new FragmentTracker(this);
const keyLoader = new KeyLoader(this.config);
const streamController = (this.streamController = new StreamController(
this,
fragmentTracker,
keyLoader,
));
// Cap level controller uses streamController to flush the buffer
capLevelController.setStreamController(streamController);
// fpsController uses streamController to switch when frames are being dropped
fpsController.setStreamController(streamController);
const networkControllers: NetworkComponentAPI[] = [
playListLoader,
levelController,
streamController,
];
if (contentSteering) {
networkControllers.splice(1, 0, contentSteering);
}
this.networkControllers = networkControllers;
const coreComponents: ComponentAPI[] = [
abrController,
bufferController,
capLevelController,
fpsController,
id3TrackController,
fragmentTracker,
];
this.audioTrackController = this.createController(
config.audioTrackController,
networkControllers,
);
const AudioStreamControllerClass = config.audioStreamController;
if (AudioStreamControllerClass) {
networkControllers.push(
new AudioStreamControllerClass(this, fragmentTracker, keyLoader),
);
}
// subtitleTrackController must be defined before subtitleStreamController because the order of event handling is important
this.subtitleTrackController = this.createController(
config.subtitleTrackController,
networkControllers,
);
const SubtitleStreamControllerClass = config.subtitleStreamController;
if (SubtitleStreamControllerClass) {
networkControllers.push(
new SubtitleStreamControllerClass(this, fragmentTracker, keyLoader),
);
}
this.createController(config.timelineController, coreComponents);
keyLoader.emeController = this.emeController = this.createController(
config.emeController,
coreComponents,
);
this.cmcdController = this.createController(
config.cmcdController,
coreComponents,
);
this.latencyController = this.createController(
LatencyController,
coreComponents,
);
this.coreComponents = coreComponents;
// Error controller handles errors before and after all other controllers
// This listener will be invoked after all other controllers error listeners
networkControllers.push(errorController);
const onErrorOut = errorController.onErrorOut;
if (typeof onErrorOut === 'function') {
this.on(Events.ERROR, onErrorOut, errorController);
}
}
createController(ControllerClass, components) {
if (ControllerClass) {
const controllerInstance = new ControllerClass(this);
if (components) {
components.push(controllerInstance);
}
return controllerInstance;
}
return null;
}
// Delegate the EventEmitter through the public API of Hls.js
on<E extends keyof HlsListeners, Context = undefined>(
event: E,
listener: HlsListeners[E],
context: Context = this as any,
) {
this._emitter.on(event, listener, context);
}
once<E extends keyof HlsListeners, Context = undefined>(
event: E,
listener: HlsListeners[E],
context: Context = this as any,
) {
this._emitter.once(event, listener, context);
}
removeAllListeners<E extends keyof HlsListeners>(event?: E | undefined) {
this._emitter.removeAllListeners(event);
}
off<E extends keyof HlsListeners, Context = undefined>(
event: E,
listener?: HlsListeners[E] | undefined,
context: Context = this as any,
once?: boolean | undefined,
) {
this._emitter.off(event, listener, context, once);
}
listeners<E extends keyof HlsListeners>(event: E): HlsListeners[E][] {
return this._emitter.listeners(event);
}
emit<E extends keyof HlsListeners>(
event: E,
name: E,
eventObject: Parameters<HlsListeners[E]>[1],
): boolean {
return this._emitter.emit(event, name, eventObject);
}
trigger<E extends keyof HlsListeners>(
event: E,
eventObject: Parameters<HlsListeners[E]>[1],
): boolean {
if (this.config.debug) {
return this.emit(event, event, eventObject);
} else {
try {
return this.emit(event, event, eventObject);
} catch (error) {
logger.error(
'An internal error happened while handling event ' +
event +
'. Error message: "' +
error.message +
'". Here is a stacktrace:',
error,
);
// Prevent recursion in error event handlers that throw #5497
if (!this.triggeringException) {
this.triggeringException = true;
const fatal = event === Events.ERROR;
this.trigger(Events.ERROR, {
type: ErrorTypes.OTHER_ERROR,
details: ErrorDetails.INTERNAL_EXCEPTION,
fatal,
event,
error,
});
this.triggeringException = false;
}
}
}
return false;
}
listenerCount<E extends keyof HlsListeners>(event: E): number {
return this._emitter.listenerCount(event);
}
/**
* Dispose of the instance
*/
destroy() {
logger.log('destroy');
this.trigger(Events.DESTROYING, undefined);
this.detachMedia();
this.removeAllListeners();
this._autoLevelCapping = -1;
this.url = null;
this.networkControllers.forEach((component) => component.destroy());
this.networkControllers.length = 0;
this.coreComponents.forEach((component) => component.destroy());
this.coreComponents.length = 0;
// Remove any references that could be held in config options or callbacks
const config = this.config;
config.xhrSetup = config.fetchSetup = undefined;
// @ts-ignore
this.userConfig = null;
}
/**
* Attaches Hls.js to a media element
*/
attachMedia(media: HTMLMediaElement) {
logger.log('attachMedia');
this._media = media;
this.trigger(Events.MEDIA_ATTACHING, { media: media });
}
/**
* Detach Hls.js from the media
*/
detachMedia() {
logger.log('detachMedia');
this.trigger(Events.MEDIA_DETACHING, undefined);
this._media = null;
}
/**
* Set the source URL. Can be relative or absolute.
*/
loadSource(url: string) {
this.stopLoad();
const media = this.media;
const loadedSource = this.url;
const loadingSource = (this.url = buildAbsoluteURL(
self.location.href,
url,
{
alwaysNormalize: true,
},
));
this._autoLevelCapping = -1;
this._maxHdcpLevel = null;
logger.log(`loadSource:${loadingSource}`);
if (
media &&
loadedSource &&
(loadedSource !== loadingSource || this.bufferController.hasSourceTypes())
) {
this.detachMedia();
this.attachMedia(media);
}
// when attaching to a source URL, trigger a playlist load
this.trigger(Events.MANIFEST_LOADING, { url: url });
}
/**
* Start loading data from the stream source.
* Depending on default config, client starts loading automatically when a source is set.
*
* @param startPosition - Set the start position to stream from.
* Defaults to -1 (None: starts from earliest point)
*/
startLoad(startPosition: number = -1) {
logger.log(`startLoad(${startPosition})`);
this.started = true;
this.networkControllers.forEach((controller) => {
controller.startLoad(startPosition);
});
}
/**
* Stop loading of any stream data.
*/
stopLoad() {
logger.log('stopLoad');
this.started = false;
this.networkControllers.forEach((controller) => {
controller.stopLoad();
});
}
/**
* Resumes stream controller segment loading if previously started.
*/
resumeBuffering() {
if (this.started) {
this.networkControllers.forEach((controller) => {
if ('fragmentLoader' in controller) {
controller.startLoad(-1);
}
});
}
}
/**
* Stops stream controller segment loading without changing 'started' state like stopLoad().
* This allows for media buffering to be paused without interupting playlist loading.
*/
pauseBuffering() {
this.networkControllers.forEach((controller) => {
if ('fragmentLoader' in controller) {
controller.stopLoad();
}
});
}
/**
* Swap through possible audio codecs in the stream (for example to switch from stereo to 5.1)
*/
swapAudioCodec() {
logger.log('swapAudioCodec');
this.streamController.swapAudioCodec();
}
/**
* When the media-element fails, this allows to detach and then re-attach it
* as one call (convenience method).
*
* Automatic recovery of media-errors by this process is configurable.
*/
recoverMediaError() {
logger.log('recoverMediaError');
const media = this._media;
this.detachMedia();
if (media) {
this.attachMedia(media);
}
}
removeLevel(levelIndex: number) {
this.levelController.removeLevel(levelIndex);
}
/**
* @returns an array of levels (variants) sorted by HDCP-LEVEL, RESOLUTION (height), FRAME-RATE, CODECS, VIDEO-RANGE, and BANDWIDTH
*/
get levels(): Level[] {
const levels = this.levelController.levels;
return levels ? levels : [];
}
/**
* Index of quality level (variant) currently played
*/
get currentLevel(): number {
return this.streamController.currentLevel;
}
/**
* Set quality level index immediately. This will flush the current buffer to replace the quality asap. That means playback will interrupt at least shortly to re-buffer and re-sync eventually. Set to -1 for automatic level selection.
*/
set currentLevel(newLevel: number) {
logger.log(`set currentLevel:${newLevel}`);
this.levelController.manualLevel = newLevel;
this.streamController.immediateLevelSwitch();
}
/**
* Index of next quality level loaded as scheduled by stream controller.
*/
get nextLevel(): number {
return this.streamController.nextLevel;
}
/**
* Set quality level index for next loaded data.
* This will switch the video quality asap, without interrupting playback.
* May abort current loading of data, and flush parts of buffer (outside currently played fragment region).
* @param newLevel - Pass -1 for automatic level selection
*/
set nextLevel(newLevel: number) {
logger.log(`set nextLevel:${newLevel}`);
this.levelController.manualLevel = newLevel;
this.streamController.nextLevelSwitch();
}
/**
* Return the quality level of the currently or last (of none is loaded currently) segment
*/
get loadLevel(): number {
return this.levelController.level;
}
/**
* Set quality level index for next loaded data in a conservative way.
* This will switch the quality without flushing, but interrupt current loading.
* Thus the moment when the quality switch will appear in effect will only be after the already existing buffer.
* @param newLevel - Pass -1 for automatic level selection
*/
set loadLevel(newLevel: number) {
logger.log(`set loadLevel:${newLevel}`);
this.levelController.manualLevel = newLevel;
}
/**
* get next quality level loaded
*/
get nextLoadLevel(): number {
return this.levelController.nextLoadLevel;
}
/**
* Set quality level of next loaded segment in a fully "non-destructive" way.
* Same as `loadLevel` but will wait for next switch (until current loading is done).
*/
set nextLoadLevel(level: number) {
this.levelController.nextLoadLevel = level;
}
/**
* Return "first level": like a default level, if not set,
* falls back to index of first level referenced in manifest
*/
get firstLevel(): number {
return Math.max(this.levelController.firstLevel, this.minAutoLevel);
}
/**
* Sets "first-level", see getter.
*/
set firstLevel(newLevel: number) {
logger.log(`set firstLevel:${newLevel}`);
this.levelController.firstLevel = newLevel;
}
/**
* Return the desired start level for the first fragment that will be loaded.
* The default value of -1 indicates automatic start level selection.
* Setting hls.nextAutoLevel without setting a startLevel will result in
* the nextAutoLevel value being used for one fragment load.
*/
get startLevel(): number {
const startLevel = this.levelController.startLevel;
if (startLevel === -1 && this.abrController.forcedAutoLevel > -1) {
return this.abrController.forcedAutoLevel;
}
return startLevel;
}
/**
* set start level (level of first fragment that will be played back)
* if not overrided by user, first level appearing in manifest will be used as start level
* if -1 : automatic start level selection, playback will start from level matching download bandwidth
* (determined from download of first segment)
*/
set startLevel(newLevel: number) {
logger.log(`set startLevel:${newLevel}`);
// if not in automatic start level detection, ensure startLevel is greater than minAutoLevel
if (newLevel !== -1) {
newLevel = Math.max(newLevel, this.minAutoLevel);
}
this.levelController.startLevel = newLevel;
}
/**
* Whether level capping is enabled.
* Default value is set via `config.capLevelToPlayerSize`.
*/
get capLevelToPlayerSize(): boolean {
return this.config.capLevelToPlayerSize;
}
/**
* Enables or disables level capping. If disabled after previously enabled, `nextLevelSwitch` will be immediately called.
*/
set capLevelToPlayerSize(shouldStartCapping: boolean) {
const newCapLevelToPlayerSize = !!shouldStartCapping;
if (newCapLevelToPlayerSize !== this.config.capLevelToPlayerSize) {
if (newCapLevelToPlayerSize) {
this.capLevelController.startCapping(); // If capping occurs, nextLevelSwitch will happen based on size.
} else {
this.capLevelController.stopCapping();
this.autoLevelCapping = -1;
this.streamController.nextLevelSwitch(); // Now we're uncapped, get the next level asap.
}
this.config.capLevelToPlayerSize = newCapLevelToPlayerSize;
}
}
/**
* Capping/max level value that should be used by automatic level selection algorithm (`ABRController`)
*/
get autoLevelCapping(): number {
return this._autoLevelCapping;
}
/**
* Returns the current bandwidth estimate in bits per second, when available. Otherwise, `NaN` is returned.
*/
get bandwidthEstimate(): number {
const { bwEstimator } = this.abrController;
if (!bwEstimator) {
return NaN;
}
return bwEstimator.getEstimate();
}
set bandwidthEstimate(abrEwmaDefaultEstimate: number) {
this.abrController.resetEstimator(abrEwmaDefaultEstimate);
}
/**
* get time to first byte estimate
* @type {number}
*/
get ttfbEstimate(): number {
const { bwEstimator } = this.abrController;
if (!bwEstimator) {
return NaN;
}
return bwEstimator.getEstimateTTFB();
}
/**
* Capping/max level value that should be used by automatic level selection algorithm (`ABRController`)
*/
set autoLevelCapping(newLevel: number) {
if (this._autoLevelCapping !== newLevel) {
logger.log(`set autoLevelCapping:${newLevel}`);
this._autoLevelCapping = newLevel;
this.levelController.checkMaxAutoUpdated();
}
}
get maxHdcpLevel(): HdcpLevel {
return this._maxHdcpLevel;
}
set maxHdcpLevel(value: HdcpLevel) {
if (isHdcpLevel(value) && this._maxHdcpLevel !== value) {
this._maxHdcpLevel = value;
this.levelController.checkMaxAutoUpdated();
}
}
/**
* True when automatic level selection enabled
*/
get autoLevelEnabled(): boolean {
return this.levelController.manualLevel === -1;
}
/**
* Level set manually (if any)
*/
get manualLevel(): number {
return this.levelController.manualLevel;
}
/**
* min level selectable in auto mode according to config.minAutoBitrate
*/
get minAutoLevel(): number {
const {
levels,
config: { minAutoBitrate },
} = this;
if (!levels) return 0;
const len = levels.length;
for (let i = 0; i < len; i++) {
if (levels[i].maxBitrate >= minAutoBitrate) {
return i;
}
}
return 0;
}
/**
* max level selectable in auto mode according to autoLevelCapping
*/
get maxAutoLevel(): number {
const { levels, autoLevelCapping, maxHdcpLevel } = this;
let maxAutoLevel;
if (autoLevelCapping === -1 && levels?.length) {
maxAutoLevel = levels.length - 1;
} else {
maxAutoLevel = autoLevelCapping;
}
if (maxHdcpLevel) {
for (let i = maxAutoLevel; i--; ) {
const hdcpLevel = levels[i].attrs['HDCP-LEVEL'];
if (hdcpLevel && hdcpLevel <= maxHdcpLevel) {
return i;
}
}
}
return maxAutoLevel;
}
get firstAutoLevel(): number {
return this.abrController.firstAutoLevel;
}
/**
* next automatically selected quality level
*/
get nextAutoLevel(): number {
return this.abrController.nextAutoLevel;
}
/**
* this setter is used to force next auto level.
* this is useful to force a switch down in auto mode:
* in case of load error on level N, hls.js can set nextAutoLevel to N-1 for example)
* forced value is valid for one fragment. upon successful frag loading at forced level,
* this value will be resetted to -1 by ABR controller.
*/
set nextAutoLevel(nextLevel: number) {
this.abrController.nextAutoLevel = nextLevel;
}
/**
* get the datetime value relative to media.currentTime for the active level Program Date Time if present
*/
public get playingDate(): Date | null {
return this.streamController.currentProgramDateTime;
}
public get mainForwardBufferInfo(): BufferInfo | null {
return this.streamController.getMainFwdBufferInfo();
}
/**
* Find and select the best matching audio track, making a level switch when a Group change is necessary.
* Updates `hls.config.audioPreference`. Returns the selected track, or null when no matching track is found.
*/
public setAudioOption(
audioOption: MediaPlaylist | AudioSelectionOption | undefined,
): MediaPlaylist | null {
return this.audioTrackController?.setAudioOption(audioOption);
}
/**
* Find and select the best matching subtitle track, making a level switch when a Group change is necessary.
* Updates `hls.config.subtitlePreference`. Returns the selected track, or null when no matching track is found.
*/
public setSubtitleOption(
subtitleOption: MediaPlaylist | SubtitleSelectionOption | undefined,
): MediaPlaylist | null {
this.subtitleTrackController?.setSubtitleOption(subtitleOption);
return null;
}
/**
* Get the complete list of audio tracks across all media groups
*/
get allAudioTracks(): Array<MediaPlaylist> {
const audioTrackController = this.audioTrackController;
return audioTrackController ? audioTrackController.allAudioTracks : [];
}
/**
* Get the list of selectable audio tracks
*/
get audioTracks(): Array<MediaPlaylist> {
const audioTrackController = this.audioTrackController;
return audioTrackController ? audioTrackController.audioTracks : [];
}
/**
* index of the selected audio track (index in audio track lists)
*/
get audioTrack(): number {
const audioTrackController = this.audioTrackController;
return audioTrackController ? audioTrackController.audioTrack : -1;
}
/**
* selects an audio track, based on its index in audio track lists
*/
set audioTrack(audioTrackId: number) {
const audioTrackController = this.audioTrackController;
if (audioTrackController) {
audioTrackController.audioTrack = audioTrackId;
}
}
/**
* get the complete list of subtitle tracks across all media groups
*/
get allSubtitleTracks(): Array<MediaPlaylist> {
const subtitleTrackController = this.subtitleTrackController;
return subtitleTrackController
? subtitleTrackController.allSubtitleTracks
: [];
}
/**
* get alternate subtitle tracks list from playlist
*/
get subtitleTracks(): Array<MediaPlaylist> {
const subtitleTrackController = this.subtitleTrackController;
return subtitleTrackController
? subtitleTrackController.subtitleTracks
: [];
}
/**
* index of the selected subtitle track (index in subtitle track lists)
*/
get subtitleTrack(): number {
const subtitleTrackController = this.subtitleTrackController;
return subtitleTrackController ? subtitleTrackController.subtitleTrack : -1;
}
get media() {
return this._media;
}
/**
* select an subtitle track, based on its index in subtitle track lists
*/
set subtitleTrack(subtitleTrackId: number) {
const subtitleTrackController = this.subtitleTrackController;
if (subtitleTrackController) {
subtitleTrackController.subtitleTrack = subtitleTrackId;
}
}
/**
* Whether subtitle display is enabled or not
*/
get subtitleDisplay(): boolean {
const subtitleTrackController = this.subtitleTrackController;
return subtitleTrackController
? subtitleTrackController.subtitleDisplay
: false;
}
/**
* Enable/disable subtitle display rendering
*/
set subtitleDisplay(value: boolean) {
const subtitleTrackController = this.subtitleTrackController;
if (subtitleTrackController) {
subtitleTrackController.subtitleDisplay = value;
}
}
/**
* get mode for Low-Latency HLS loading
*/
get lowLatencyMode(): boolean {
return this.config.lowLatencyMode;
}
/**
* Enable/disable Low-Latency HLS part playlist and segment loading, and start live streams at playlist PART-HOLD-BACK rather than HOLD-BACK.
*/
set lowLatencyMode(mode: boolean) {
this.config.lowLatencyMode = mode;
}
/**
* Position (in seconds) of live sync point (ie edge of live position minus safety delay defined by ```hls.config.liveSyncDuration```)
* @returns null prior to loading live Playlist
*/
get liveSyncPosition(): number | null {
return this.latencyController.liveSyncPosition;
}
/**
* Estimated position (in seconds) of live edge (ie edge of live playlist plus time sync playlist advanced)
* @returns 0 before first playlist is loaded
*/
get latency(): number {
return this.latencyController.latency;
}
/**
* maximum distance from the edge before the player seeks forward to ```hls.liveSyncPosition```
* configured using ```liveMaxLatencyDurationCount``` (multiple of target duration) or ```liveMaxLatencyDuration```
* @returns 0 before first playlist is loaded
*/
get maxLatency(): number {
return this.latencyController.maxLatency;
}
/**
* target distance from the edge as calculated by the latency controller
*/
get targetLatency(): number | null {
return this.latencyController.targetLatency;
}
/**
* the rate at which the edge of the current live playlist is advancing or 1 if there is none
*/
get drift(): number | null {
return this.latencyController.drift;
}
/**
* set to true when startLoad is called before MANIFEST_PARSED event
*/
get forceStartLoad(): boolean {
return this.streamController.forceStartLoad;
}
get pathways(): string[] {
return this.levelController.pathways;
}
/**
* ContentSteering pathwayPriority getter/setter
*/
get pathwayPriority(): string[] | null {
return this.levelController.pathwayPriority;
}
set pathwayPriority(pathwayPriority: string[]) {
this.levelController.pathwayPriority = pathwayPriority;
}
}
export type {
AudioSelectionOption,
SubtitleSelectionOption,
VideoSelectionOption,
MediaPlaylist,
ErrorDetails,
ErrorTypes,
Events,
Level,
HlsListeners,
HlsEventEmitter,