-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPlayer.ts
957 lines (854 loc) · 25.7 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
import { clpp } from '@castlabs/prestoplay'
import { EventEmitter, EventListener, EventType } from './EventEmitter'
import { Controls, ControlsVisibilityMode } from './services/controls'
import {
fromPrestoTrack,
getAbrTrack,
getActiveTrack,
getDisabledTrack,
getTracks,
getUnavailableTrack,
Track,
TrackType,
} from './Track'
import { defaultTrackLabel, defaultTrackSorter, TrackLabeler, TrackLabelerOptions, TrackSorter } from './TrackLabeler'
import { Cue, Disposer } from './types'
/**
* The player initializer is a function that receives the presto play instance
* once it is created and can be used to configure and initialize the core
* player further. This allows you to, for instance, add components to the
* player, configure request and response modifiers and interact with the
* presto API just after player initialization.
*/
export type PlayerInitializer = (presto: clpp.Player) => void
/**
* Internal helper to queue prestoplay function calls while the player is not
* yet initialized
*/
type Action = () => Promise<void>
type Range = {
start: number
end: number
}
/**
* Player states
*/
export enum State {
/**
* 0 - when player has been created or released
*/
Idle = clpp.Player.State.IDLE,
/**
* 1 - when player receives the api command to load the movie until it starts
* requesting the first fragment
*/
Preparing = clpp.Player.State.PREPARING,
/**
* 2 - when player doesn't have enough data to play the content for any
* reasons
*/
Buffering = clpp.Player.State.BUFFERING,
/**
* 3 - when player starts playing content
*/
Playing = clpp.Player.State.PLAYING,
/**
* 4 - when player is stopped
*/
Paused = clpp.Player.State.PAUSED,
/**
* 5 - when video is ended
*/
Ended = clpp.Player.State.ENDED,
/**
* 6 - when player encounters an error
*/
Error = clpp.Player.State.ERROR,
/**
* 7 - Used exclusively to indicate previous state, when it has no state yet
*/
Unset = clpp.Player.State.UNSET,
}
const toState = (state: number): State => {
switch (state) {
case clpp.Player.State.IDLE:
return State.Idle
case clpp.Player.State.PREPARING:
return State.Preparing
case clpp.Player.State.BUFFERING:
return State.Buffering
case clpp.Player.State.PLAYING:
return State.Playing
case clpp.Player.State.PAUSED:
return State.Paused
case clpp.Player.State.ENDED:
return State.Ended
case clpp.Player.State.ERROR:
return State.Error
case clpp.Player.State.UNSET:
return State.Unset
default:
return State.Unset
}
}
export interface UIEvents {
/**
* Triggered when the slide in menu state changes
*/
slideInMenuVisible: boolean
/**
* Triggered when the `PlayerControls` visibility changes
*/
controlsVisible: boolean
/**
* Triggered when a track selection changes
*/
trackSelected: Track
/**
* Triggered when the selected video track changes
*/
videoTrackChanged: Track
/**
* Triggered when the selected audio track changes
*/
audioTrackChanged: Track
/**
* Triggered when the selected text track changes
*/
textTrackChanged: Track
/**
* Triggered when the available text track change
*/
textTracksAvailable: Track[]
/**
* Triggered when the available audio track change
*/
audioTracksAvailable: Track[]
/**
* Triggered when the available video track change
*/
videoTracksAvailable: Track[]
/**
* Triggered when the currently playing video rendition changes, i.e.
* a quality change
*/
playingVideoTrackChanged: Track | undefined
/**
* Triggered on a user interaction (i.e. mouse hover) on top of the
* player surface
*/
surfaceInteraction: undefined
/**
* Triggered when the hover position changes
*/
hoverPosition: { position: number; percent: number }
/**
* Position changes are posted using this event. The event posts the current
* position. This event is emitted when the underlying video element emits a
* timeupdate event or when a seek is issued.
*/
position: number
/**
* Duration changes are exposed through this event
*/
durationchange: number
/**
* Volume change event that is triggered when the volume or the muted
* state is changed
*/
volumechange: { volume: number; muted: boolean }
/**
* State change events are triggered when the player state changes
*/
statechanged: {
currentState: State
previousState: State
timeSinceLastStateChangeMS: number
reason?: clpp.events.BufferingReasons
}
/**
* Event triggered when the playback rate changed
*/
ratechange: number
/**
* Helper event that is triggered when the player enters (or leaves) a state
* where controls should be enabled. The player is considered disabled in
* Unset, Idle, and Error states.
*/
enabled: boolean
/**
* Event triggered when timeline/seek bar cues change.
*/
cuesChanged: Cue[]
}
/**
* Helper that returns true if the given state is considered to be one of the
* enabled states
* @param state
*/
const isEnabledState = (state: State): boolean => {
return state !== State.Idle && state !== State.Unset && state !== State.Error
}
/**
* The player class provides to top level interface to interact with the core
* player. It will create the PRESTOplay instance, and it offers access to the
* related apis as well as dedicated events that are important for UI interactions.
*/
export class Player {
/**
* The player instance
*/
protected pp_: clpp.Player | null = null
/**
* We maintain a queue of actions that will be posted towards the player
* instance once it is initialized
*/
private _actionQueue_: Action[] = []
/**
* Function that resolves the player initialization
*/
private _actionQueueResolved?: () => void
/**
* A promise that resolves once the player is initialized
*/
private readonly _actionQueuePromise: Promise<void>
/**
* The player initializer
*/
private readonly _initializer?: PlayerInitializer
/**
* Internal state that indicates that the "controls" are visible
*/
private _controlsVisible = false
/**
* Internal controls that indicate that the slide in menu is visible
*/
private _slideInMenuVisible = false
/**
* The currently playing video track
*/
private _playingVideoTrack: Track | undefined
/**
* The currently selected video track
*/
private _videoTrack: Track = getUnavailableTrack('video')
/**
* The currently selected audio track
*/
private _audioTrack: Track = getUnavailableTrack('audio')
/**
* The currently selected text track
*/
private _textTrack: Track = getUnavailableTrack('text')
/**
* All available video tracks
*/
private _videoTracks: Track[] = []
/**
* All available audio tracks
*/
private _audioTracks: Track[] = []
/**
* All available text tracks
*/
private _textTracks: Track[] = []
/**
* The track sorter
*/
private _trackSorter: TrackSorter = defaultTrackSorter
/**
* The track labeler
*/
private _trackLabeler: TrackLabeler = defaultTrackLabel
/**
* The track labeler options
*/
private _trackLabelerOptions?: TrackLabelerOptions
/**
* The event emitter for UI related events
*/
private readonly _eventEmitter = new EventEmitter<UIEvents>()
/**
* This is true while we are waiting for a user initiated seek even to
* complete
*/
private _isUserSeeking = false
/**
* The target of the last user initiated seek event. We use this in case
* there were more seek events while we were waiting for the last event
* to complete
*/
private _userSeekingTarget = -1
/**
* Proxy the playback rate
*/
private _rate = 1
/**
* The current player configuration
*/
private _config: clpp.PlayerConfiguration | null = null
/**
* Indicate that the config was loaded
*/
protected _configLoaded = false
/**
* UI control visibility manager
*/
private _controls = new Controls()
/**
* Disposers of listeners on the PRESTOplay player instance
*/
private _prestoDisposers: Disposer[] = []
/**
* The last playback state
*/
private _lastPlaybackState: State = State.Unset
/**
* Timeline cues
*/
private _cues: Cue[] = []
constructor(initializer?: PlayerInitializer) {
this._initializer = initializer
this._actionQueuePromise = new Promise<void>((resolve) => {
this._actionQueueResolved = resolve
})
this._controls.onChange = (visible) => {
this.emitUIEvent('controlsVisible', visible)
}
}
/**
* Initializer that is called with the video element (or the id of the
* video element) once it is available. The video element is used to create
* the presto play instance. Once the instance is created, the initializer
* is executed and all pending action are triggered.
*
* If the player is already initialized, this function does not do anything.
*
* @param element The video element or the ID of the video element
* @param baseConfig PRESTOplay config to initialize the player with
*/
async init(element: HTMLVideoElement | string, baseConfig?: clpp.PlayerConfiguration) {
if (this.pp_) {return}
this.pp_ = new clpp.Player(element, baseConfig)
this.attachPrestoListeners_(this.pp_)
if (this._initializer) {
this._initializer(this.pp_)
}
for (let i = 0; i < this._actionQueue_.length; i++) {
await this._actionQueue_[i]()
}
this._actionQueue_ = []
if (this._actionQueueResolved) {
this._actionQueueResolved()
}
}
/**
* Set timeline cues
*/
setCues(cues: Cue[]) {
this._cues = cues
this.emitUIEvent('cuesChanged', cues)
}
/**
* Get timeline cues
*/
getCues(): Cue[] {
return this._cues
}
/**
* Attach listeners to PRESTOplay events
*/
protected attachPrestoListeners_(player: clpp.Player) {
const createTrackChangeHandler = (type?: TrackType) => {
return () => {
const trackManager = this.trackManager
if (!trackManager) {return}
if (!type || type === 'video') {
this.videoTrack = getActiveTrack(trackManager, 'video')
this.videoTracks = getTracks(trackManager, 'video')
}
if (!type || type === 'audio') {
this.audioTrack = getActiveTrack(trackManager, 'audio')
this.audioTracks = getTracks(trackManager, 'audio')
}
if (!type || type === 'text') {
this.textTrack = getActiveTrack(trackManager, 'text')
this.textTracks = getTracks(trackManager, 'text')
}
}
}
const onTracksAdded = createTrackChangeHandler()
const onAudioTrackChanged = createTrackChangeHandler('audio')
const onVideoTrackChanged = createTrackChangeHandler('video')
const onTextTrackChanged = createTrackChangeHandler('text')
player.on(clpp.events.TRACKS_ADDED, onTracksAdded)
player.on(clpp.events.AUDIO_TRACK_CHANGED, onAudioTrackChanged)
player.on(clpp.events.VIDEO_TRACK_CHANGED, onVideoTrackChanged)
player.on(clpp.events.TEXT_TRACK_CHANGED, onTextTrackChanged)
const onStateChanged = (event: any) => {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const e = event
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
const currentState = toState(e.detail.currentState)
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
const previousState = toState(e.detail.previousState)
if (currentState === State.Ended) {
return
}
this.emitUIEvent('statechanged', {
currentState,
previousState,
reason: e.detail.reason,
timeSinceLastStateChangeMS: e.detail.timeSinceLastStateChangeMS,
})
if (
isEnabledState(currentState) !== isEnabledState(previousState)
|| isEnabledState(currentState) !== isEnabledState(this._lastPlaybackState)
) {
this.emitUIEvent('enabled', isEnabledState(currentState))
}
if (!isEnabledState(currentState) || currentState === State.Paused) {
this._controls.pin()
} else {
this._controls.unpin()
}
this._lastPlaybackState = currentState
}
player.on(clpp.events.STATE_CHANGED, onStateChanged)
const onTimeupdate = () => {
const position = player.getPosition()
if (position != null) {
this.emitUIEvent('position', position)
}
}
player.on('timeupdate', onTimeupdate)
this._rate = player.getPlaybackRate()
const onRateChange = () => {
const ppRate = player.getPlaybackRate()
if (ppRate != null && this.state !== State.Buffering) {
this._rate = ppRate
this.emitUIEvent('ratechange', this.rate)
}
}
player.on('ratechange', onRateChange)
const onDurationChange = () => {
const duration = player.getDuration()
if (duration != null) {
this.emitUIEvent('durationchange', duration)
}
}
player.on('durationchange', onDurationChange)
const onVolumeChange = () => {
this.emitUIEvent('volumechange', {
volume: this.volume,
muted: this.muted,
})
}
player.on('volumechange', onVolumeChange)
const onBitrateChange = (e: any) => {
const tm = this.trackManager
if (tm) {
this.playingVideoTrack = fromPrestoTrack(tm, e.detail.rendition as clpp.Rendition, 'video')
} else {
this.playingVideoTrack = undefined
}
}
player.on(clpp.events.BITRATE_CHANGED, onBitrateChange)
this._prestoDisposers.push(() => {
player.off(clpp.events.TRACKS_ADDED, onTracksAdded)
player.off(clpp.events.AUDIO_TRACK_CHANGED, onAudioTrackChanged)
player.off(clpp.events.VIDEO_TRACK_CHANGED, onVideoTrackChanged)
player.off(clpp.events.TEXT_TRACK_CHANGED, onTextTrackChanged)
player.off(clpp.events.STATE_CHANGED, onStateChanged)
player.off('timeupdate', onTimeupdate)
player.off('ratechange', onRateChange)
player.off('durationchange', onDurationChange)
player.off('volumechange', onVolumeChange)
player.off(clpp.events.BITRATE_CHANGED, onBitrateChange)
})
}
protected refreshPrestoState_ (player: clpp.Player) {
const state = toState(player.getState())
if (isEnabledState(state)) {
this.emitUIEvent('enabled', true)
}
this.emitUIEvent('statechanged', {
currentState: state,
previousState: this._lastPlaybackState,
timeSinceLastStateChangeMS: 10,
})
const position = player.getPosition() ?? 0
this.emitUIEvent('position', position)
const duration = player.getDuration()
if (duration != null) {
this.emitUIEvent('durationchange', duration)
}
this.emitUIEvent('volumechange', {
volume: this.volume,
muted: this.muted,
})
}
/**
* Remove listeners to PRESTOplay events
*/
protected removePrestoListeners_ () {
this._prestoDisposers.forEach(dispose => dispose())
this._prestoDisposers = []
}
get trackManager() {
return this.pp_?.getTrackManager() ?? null
}
/**
* Seek to the given position. Calling this function has no effect unless the
* presto instance is already initialized.
*
* @param position The target position in seconds
*/
set position(position: number) {
if (!this.pp_) {return}
if (!this._isUserSeeking) {
// issue a user seek to the target position and
// listen for the completion.
const handleSeekCompleted = () => {
this._isUserSeeking = false
if (this._userSeekingTarget !== position) {
// we received another seek in between and have to execute that now
this.position = this._userSeekingTarget
} else {
this._userSeekingTarget = -1
const position = this.pp_?.getPosition()
if (position) {
this.emitUIEvent('position', position)
}
}
}
this._isUserSeeking = true
this._userSeekingTarget = position
this.pp_.seek(position).finally(handleSeekCompleted)
this.emitUIEvent('position', position)
} else {
// we are already seeking. Update the target. When the current
// seek operation is completed, we will seek again to the final target
this._userSeekingTarget = position
this.emitUIEvent('position', position)
}
}
get position() {
if (!this.pp_) {return 0}
if (this._isUserSeeking) {
return this._userSeekingTarget
}
return this.pp_.getPosition()
}
get duration(): number {
return (this.pp_ && this.pp_.getDuration() > 0) ? this.pp_.getDuration() : 0
}
get live(): boolean {
return this.pp_ ? this.pp_.isLive() : false
}
get seekRange(): Range {
return this.pp_ ? this.pp_.getSeekRange() as Range : { start: 0, end: 0 }
}
get volume(): number {
return this.pp_ ? this.pp_.getVolume() ?? 0 : 1
}
set volume(volume: number) {
if (this.pp_) {
if (this.muted && volume > 0) {
this.muted = false
}
this.pp_.setVolume(volume)
}
}
get muted() {
return this.pp_ ? this.pp_.isMuted() ?? false : false
}
set muted(muted: boolean) {
if (this.pp_) {
this.pp_.setMuted(muted)
}
}
get state(): State {
return this.pp_ ? toState(this.pp_.getState()) : State.Unset
}
get rate() {
if (!this.pp_) {return 1}
return this._rate
}
set rate(value: number) {
if (this.pp_) {
this._rate = value
this.pp_.setPlaybackRate(value)
if (this.state === State.Buffering) {
this.emitUIEvent('ratechange', value)
}
}
}
get playing() {
if (!this.pp_) {return false}
if (this.state === State.Idle) {return false}
return !this.pp_.isPaused()
}
set playing(value: boolean) {
if (this.pp_) {
if (!this._configLoaded && value) {
this.load().then(() => {
value ? this.pp_?.play() : this.pp_?.pause()
}).catch(() => {})
} else {
if (this._configLoaded) {
value ? this.pp_?.play() : this.pp_?.pause()
}
}
}
}
load(config?: clpp.PlayerConfiguration, autoload = false) {
if (config) {
this._config = config
return this.action(async () => {
await this.reset_()
if (config && autoload) {
this._configLoaded = true
await this.pp_?.load(config)
}
})
} else {
return this.action(async () => {
await this.pp_?.release()
if (this._config) {
this._configLoaded = true
await this.pp_?.load(this._config)
}
})
}
}
get config() {
return this._config
}
async release() {
this._config = null
await this.reset_()
}
private async reset_() {
if (this.pp_) {
await this.pp_.release()
}
this._configLoaded = false
this.emitUIEvent('position', 0)
this.emitUIEvent('durationchange', 0)
this.videoTracks = []
this.audioTracks = []
this.textTracks = []
this.videoTrack = getDisabledTrack('video', true)
this.audioTrack = getDisabledTrack('audio', true)
this.textTrack = getDisabledTrack('text', true)
}
setHoverPosition(position: number, percent: number) {
this.emitUIEvent('hoverPosition', {
position, percent,
})
}
get controlsVisible(): boolean {
return this._controls.visible
}
set controlsVisible(value: boolean) {
this._controls.setVisible(value)
}
set controlsAutoHideDelayMs(value: number) {
this._controls.hideDelayMs = value
}
set controlsVisibilityMode(value: ControlsVisibilityMode) {
this._controls.mode = value
}
get slideInMenuVisible(): boolean {
return this._slideInMenuVisible
}
set slideInMenuVisible(value: boolean) {
if (value !== this._slideInMenuVisible) {
this._slideInMenuVisible = value
this.emitUIEvent('slideInMenuVisible', value)
}
this.controlsVisible = !value
}
/**
* This method registers a surface interaction. We consider any user
* interaction with the interface or the controls here. This can be used for
* instance to show overlays for a short period of time and hide them
* if no interaction was registered.
*/
surfaceInteraction() {
this.emitUIEvent('surfaceInteraction', undefined)
if (!this.slideInMenuVisible) {
this.controlsVisible = true
}
}
get playingVideoTrack(): Track | undefined {
return this._playingVideoTrack
}
set playingVideoTrack(value: Track | undefined) {
if (isSameTrack(value, this._playingVideoTrack)) {return}
this._playingVideoTrack = value
this.emitUIEvent('playingVideoTrackChanged', value)
}
async presto(): Promise<clpp.Player> {
await this._actionQueuePromise
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
return (this.pp_ as clpp.Player)
}
/**
* Execute an action or queue it if the player is not yet available
*
* @param a The action
* @private
*/
private async action(a: Action) {
this.pp_ ? await a() : new Promise((resolve) => {
this._actionQueue_.push(async () => {
resolve(await a())
})
})
}
get videoTrack(): Track {
return this._videoTrack
}
private set videoTrack(track: Track) {
if (isSameTrack(this._videoTrack, track)) {return}
this._videoTrack = track
// this._videoTracks.forEach(t => t.selected = t.id === track.id)
this.emitUIEvent('videoTrackChanged', track)
}
get audioTrack(): Track {
return this._audioTrack
}
private set audioTrack(track: Track) {
if (isSameTrack(this._audioTrack, track)) {return}
this._audioTrack = track
this.emitUIEvent('audioTrackChanged', track)
}
get textTrack(): Track {
return this._textTrack
}
private set textTrack(track: Track) {
if (isSameTrack(this._textTrack, track)) {return}
this._textTrack = track
this.emitUIEvent('textTrackChanged', track)
}
get textTracks(): Track[] {
return this._textTracks
}
set textTracks(value: Track[]) {
if (value.length > 0 && !value.find(t => t.id === 'abr')) {
// since we have text tracks available, we add the off track
const hasSelected = value.find(t => t.selected)
const offTrack = getDisabledTrack('text', !hasSelected)
value.push(offTrack)
}
if (isSameTrackList(this.textTracks, value)) {return}
this._textTracks = value.sort(this._trackSorter)
this.emitUIEvent('textTracksAvailable', value)
}
get audioTracks(): Track[] {
return this._audioTracks
}
set audioTracks(value: Track[]) {
if (isSameTrackList(this.audioTracks, value)) {return}
this._audioTracks = value.sort(this._trackSorter)
this.emitUIEvent('audioTracksAvailable', value)
}
get videoTracks(): Track[] {
return this._videoTracks
}
set videoTracks(value: Track[]) {
value = value.filter(v => {
return v.ppTrack && v.ppTrack.height && v.ppTrack.width
})
if (value.length > 0 && !value.find(t => t.id === 'abr')) {
// since we have video tracks available, we add the ABR track
const hasSelected = value.find(t => t.selected)
const abrTrack = getAbrTrack()
abrTrack.selected = !hasSelected
value.push(abrTrack)
}
if (isSameTrackList(this._videoTracks, value)) {
return
}
this._videoTracks = value.sort(this._trackSorter)
this.emitUIEvent('videoTracksAvailable', value)
}
activeTrack(type: TrackType): Track {
switch (type) {
case 'video':
return this.videoTrack
case 'text':
return this.textTrack
case 'audio':
return this.audioTrack
}
}
selectTrack(track: Track) {
const tm = this.trackManager
if (!tm) {return}
try {
if (track.id === 'abr') {
tm.setVideoTrack(null)
this.videoTrack = getActiveTrack(tm, 'video')
this.videoTracks = getTracks(tm, 'video')
} else {
track.selected = true
if (track.type === 'audio') {
tm.setAudioTrack(track.ppTrack)
this.audioTrack = getActiveTrack(tm, 'audio')
this.audioTracks = getTracks(tm, 'audio')
} else if (track.type === 'text') {
tm.setTextTrack(track.ppTrack)
this.textTrack = getActiveTrack(tm, 'text')
this.textTracks = getTracks(tm, 'text')
} else if (track.type === 'video') {
tm.setVideoRendition(track.ppTrack, true)
this.videoTrack = getActiveTrack(tm, 'video')
this.videoTracks = getTracks(tm, 'video')
}
}
this.emitUIEvent('trackSelected', track)
} catch (error) {
console.warn('unable to select track', error, track)
}
}
get enabled() {
return isEnabledState(this.state)
}
getTrackLabel(track: Track, labelTrack: TrackLabeler | undefined, options?: TrackLabelerOptions) {
options = options || this._trackLabelerOptions
labelTrack = labelTrack || this._trackLabeler
return labelTrack(track, this, options)
}
set trackLabeler(value: TrackLabeler) {
this._trackLabeler = value
}
set trackLabelerOptions(value: TrackLabelerOptions) {
this._trackLabelerOptions = value
}
emitUIEvent<K extends EventType<UIEvents>>(type: K, data: UIEvents[K]): void {
this._eventEmitter.emit(type, data)
}
offUIEvent<K extends EventType<UIEvents>>(type: K, listener: EventListener<UIEvents[K]>): void {
this._eventEmitter.off(type, listener)
}
onUIEvent<K extends EventType<UIEvents>>(type: K, listener: EventListener<UIEvents[K]>): void {
this._eventEmitter.on(type, listener)
}
}
const isSameTrack = (a?: Track, b?: Track): boolean => {
if (a && b) {
return a.type === b.type &&
a.ppTrack === b.ppTrack &&
a.selected === b.selected &&
a.id === b.id
}
return !a && !b
}
const isSameTrackList = (a: Track[], b: Track[]): boolean => {
return a.length === b.length
&& a.every((at) => b.find(bt => isSameTrack(at, bt)))
&& b.every((bt) => a.find(at => isSameTrack(bt, at)))
}