forked from castlabs/prestoplay-react-components
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayer.ts
589 lines (505 loc) · 15.8 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
// @ts-ignore
import {clpp} from '@castlabs/prestoplay'
import React, {useEffect} from "react";
import {
fromPrestoTrack, getAbrTrack,
getActiveTrack, getDisabledTrack, getTracks, getUnavailableTrack,
Track,
TrackType,
} from "./Track";
import {LanguageCodes} from "./LanguageCodes";
type Action = () => Promise<void>;
type PlayerInitializer = (presto: any) => void
export interface TrackLabelerOptions{}
export type TrackLabeler = (track: Track, player: Player, options?: TrackLabelerOptions) => string
export type TrackSorter = (a: Track, b: Track) => number
export interface DefaultTrackLabelerOptions extends TrackLabelerOptions {
usePlayingRenditionInAbrLabel?: boolean
useNativeLanguageNames?: boolean
abrLabel?: string
disabledTrackLabel?: string
unknownTrackLabel?: string
}
export const defaultTrackLabel: TrackLabeler = (t: Track, player: Player, _options?: DefaultTrackLabelerOptions) => {
const opts = _options || {
usePlayingRenditionInAbrLabel: false,
useNativeLanguageNames: false,
}
opts.abrLabel = opts.abrLabel || "Auto"
opts.disabledTrackLabel = opts.disabledTrackLabel || "Off"
opts.unknownTrackLabel = opts.unknownTrackLabel || "Unknown"
if (t.id == "abr") {
if (!player.playingVideoTrack ||
!player.playingVideoTrack.ppTrack ||
!player.playingVideoTrack.ppTrack.height ||
!opts.usePlayingRenditionInAbrLabel) {
return opts.abrLabel
} else {
return `${opts.abrLabel} (${player.playingVideoTrack.ppTrack.height}p)`
}
}
if (!t.ppTrack) {
return opts.disabledTrackLabel
}
if (t.type == "video") {
return t.ppTrack.height + "p"
} else {
if (t.label) {
return t.label
}
if (t.ppTrack.language) {
// @ts-ignore
let lang = LanguageCodes[t.ppTrack.language]
if(lang) {
if (opts.useNativeLanguageNames) {
return lang.native
}
return lang.name
}
}
let trackList = player[`${t.type}Tracks`];
let i = trackList.indexOf(t);
if(i >= 0 && trackList.length > 1) {
return `${opts.unknownTrackLabel} (${i+1})`
}
return `${opts.unknownTrackLabel}`
}
}
export const defaultTrackSorter:TrackSorter = (a: Track, b: Track) => {
if (!a.ppTrack) return -1
if (!b.ppTrack) return 1
if (a.ppTrack.height && b.ppTrack.height) {
return b.ppTrack.height - a.ppTrack.height
}
if (a.label && b.label) {
return a.label.localeCompare(b.label)
}
if(a.ppTrack.language && b.ppTrack.language) {
return a.ppTrack.language.localeCompare(b.ppTrack.language)
}
return 0
}
/**
* UI related events triggered by the Player
*/
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 }
}
type EventPayload<E extends keyof UIEvents> = UIEvents[E]
type EventListener<E extends keyof UIEvents> = (data: EventPayload<E>) => any
type EventListenerList = Record<string, EventListener<any>[]>
interface EventEmitter {
onUIEvent<E extends keyof UIEvents>(type: E, listener: EventListener<E>): void
offUIEvent<E extends keyof UIEvents>(type: E, listener: EventListener<E>): void
emitUIEvent<E extends keyof UIEvents>(type: E, data: UIEvents[E]): void
}
class UIEventEmitter implements EventEmitter {
private listeners: EventListenerList = {}
emitUIEvent<E extends keyof UIEvents>(type: E, data: EventPayload<E>): void {
let listeners = this.listeners[type];
if (listeners) {
listeners.forEach((listener) => {
listener.call(null, data)
})
}
}
offUIEvent<E extends keyof UIEvents>(type: E, listener: EventListener<E>): void {
let listeners = this.listeners[type];
if (listeners) {
let i
while ((i = listeners.indexOf(listener)) >= 0) {
listeners.splice(i, 1)
}
}
}
onUIEvent<E extends keyof UIEvents>(type: E, listener: (data: EventPayload<E>) => any): void {
let listeners = this.listeners[type];
if (!listeners) {
listeners = []
this.listeners[type] = listeners
}
listeners.push(listener)
}
}
const isSameTrack = (a: Track | undefined, b: Track | undefined): boolean => {
if (!a && !b) return true
if (!a && b) return false
if (a && !b) return false
return a!.type == b!.type &&
a!.ppTrack == b!.ppTrack &&
a!.selected == b!.selected &&
a!.id == b!.id
}
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)))
}
export class Player extends UIEventEmitter {
public static readonly slideInMenuVisibleEvent = "pp-ui-slide-in-menu-visible";
public static readonly controlsVisibleEvent = "pp-ui-controls-visible";
public static readonly surfaceInteractionEvent = "pp-ui-surface-interaction";
private pp_: any = null;
private actionQueue_: Action[] = []
private actionQueueResolved?: () => void
private readonly actionQueuePromise: Promise<void>
private readonly initializer?: PlayerInitializer
private _controlsVisible = false
private _slideInMenuVisible = false
private _playingVideoTrack: Track | undefined;
private _videoTrack: Track = getUnavailableTrack("video")
private _audioTrack: Track = getUnavailableTrack("audio")
private _textTrack: Track = getUnavailableTrack("text")
private _videoTracks: Track[] = []
private _audioTracks: Track[] = []
private _textTracks: Track[] = []
private _trackSorter:TrackSorter = defaultTrackSorter
private _trackLabeler:TrackLabeler = defaultTrackLabel
private _trackLabelerOptions?:TrackLabelerOptions
constructor(initializer?: PlayerInitializer) {
super()
this.initializer = initializer;
// noinspection JSIgnoredPromiseFromCall
this.actionQueuePromise = new Promise<void>((resolve) => {
this.actionQueueResolved = resolve
})
}
init(element: HTMLVideoElement | string) {
this.pp_ = new clpp.Player(element)
const handlePlayerTracksChanged = (type?: TrackType) => {
return () => {
if (!type || type == "video") {
this.videoTrack = getActiveTrack(this.pp_, "video")
this.videoTracks = getTracks(this.pp_, "video")
}
if (!type || type == "audio") {
this.audioTrack = getActiveTrack(this.pp_, "audio")
this.audioTracks = getTracks(this.pp_, "audio")
}
if (!type || type == "text") {
this.textTrack = getActiveTrack(this.pp_, "text")
this.textTracks = getTracks(this.pp_, "text")
}
}
}
this.pp_.on(clpp.events.TRACKS_ADDED, handlePlayerTracksChanged())
this.pp_.on(clpp.events.AUDIO_TRACK_CHANGED, handlePlayerTracksChanged("audio"))
this.pp_.on(clpp.events.VIDEO_TRACK_CHANGED, handlePlayerTracksChanged("video"))
this.pp_.on(clpp.events.TEXT_TRACK_CHANGED, handlePlayerTracksChanged("text"))
this.pp_.on(clpp.events.BITRATE_CHANGED, (e: any) => {
if (e && e.detail) {
this.playingVideoTrack = fromPrestoTrack(this.pp_, e.detail.rendition, 'video');
} else {
this.playingVideoTrack = undefined
}
handlePlayerTracksChanged("video")
})
if (this.initializer) {
this.initializer(this.pp_)
}
for (let i = 0; i < this.actionQueue_.length; i++) {
this.actionQueue_[i]()
}
this.actionQueue_ = []
if (this.actionQueueResolved) {
this.actionQueueResolved()
}
}
load(config: any) {
this.action(() => {
return this.pp_.load(config)
})
}
release() {
this.action(() => {
return this.pp_.release()
})
}
on(event: string, callback: (e: any) => void) {
this.action(() => {
this.pp_.on(event, callback)
return Promise.resolve()
})
}
off(event: string, callback: (e: any) => void) {
this.action(() => {
this.pp_.off(event, callback)
return Promise.resolve()
})
}
one(event: string, callback: (e: any) => void) {
this.action(() => {
this.pp_.one(event, callback)
return Promise.resolve()
})
}
use(component: any) {
this.action(() => {
this.pp_.use(component)
return Promise.resolve()
})
}
setHoverPosition(position: number, percent: number) {
this.emitUIEvent("hoverPosition", {
position, percent
})
}
get controlsVisible(): boolean {
return this._controlsVisible;
}
set controlsVisible(value: boolean) {
if (value != this._controlsVisible) {
this._controlsVisible = value;
this.emitUIEvent("controlsVisible", value)
}
}
get slideInMenuVisible(): boolean {
return this._slideInMenuVisible;
}
set slideInMenuVisible(value: boolean) {
if (value != this._slideInMenuVisible) {
this._slideInMenuVisible = value;
this.emitUIEvent("slideInMenuVisible", value)
}
}
surfaceInteraction() {
if (!this.controlsVisible && !this.slideInMenuVisible) {
this.emitUIEvent("surfaceInteraction", undefined)
}
}
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() {
await this.actionQueuePromise
return this.pp_
}
private action(a: Action) {
if (this.pp_) {
a()
} else {
this.actionQueue_.push(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
let hasSelected = value.find(t=> t.selected)
let 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[]) {
if (value.length > 0 && !value.find(t => t.id == 'abr')) {
// since we have video tracks available, we add the ABR track
let hasSelected = value.find(t=> t.selected)
let 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) {
if (!this.pp_) {
return
}
try {
const presto = this.pp_
const tm = presto.getTrackManager();
if (track.id == 'abr') {
tm.setVideoTrack(null)
this.videoTrack = getActiveTrack(this.pp_, "video")
this.videoTracks = getTracks(this.pp_, "video")
} else {
track.selected = true
if (track.type == "audio") {
tm.setAudioTrack(track.ppTrack)
this.audioTrack = getActiveTrack(this.pp_, "audio")
this.audioTracks = getTracks(this.pp_, "audio")
} else if (track.type == "text") {
tm.setTextTrack(track.ppTrack)
this.textTrack = getActiveTrack(this.pp_, "text")
this.textTracks = getTracks(this.pp_, "text")
} else if (track.type == "video") {
tm.setVideoRendition(track.ppTrack, true)
this.videoTrack = getActiveTrack(this.pp_, "video")
this.videoTracks = getTracks(this.pp_, "video")
}
}
this.emitUIEvent("trackSelected", track)
} catch (e) {
console.log(`unable to select track: ${e}`, track)
}
}
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;
}
}
export type EventHandler = (e: any, presto: any) => void
export type PrestoReceiver = (presto: any) => void
export function usePrestoEvent(eventName: string, player: Player, handler: EventHandler, dependencies?: any[]) {
async function handleEvent(e: any) {
let presto = await player.presto()
handler(e, presto)
}
dependencies = dependencies || []
useEffect(() => {
player.on(eventName, handleEvent);
return () => {
player.off(eventName, handleEvent);
}
}, [player, ...dependencies])
}
export function usePrestoUiEvent<E extends keyof UIEvents>(eventName: E, player: Player, handler: EventListener<E>) {
useEffect(() => {
player.onUIEvent(eventName, handler);
return () => {
player.offUIEvent(eventName, handler);
}
}, [player])
}
export function usePresto(player: Player, receiver: PrestoReceiver) {
useEffect(() => {
let completed = false
player.presto().then((presto) => {
if (!completed) {
receiver(presto)
}
})
return () => {
completed = true
}
}, [player])
}
export function useGlobalHide(ref: React.RefObject<Element>, hide: () => any) {
useEffect(() => {
let handleClick = async (event:MouseEvent) => {
if (ref.current && !ref.current.contains((event.target as Node))) {
await hide()
}
}
document.addEventListener("click", handleClick)
return () => {
document.removeEventListener("click", handleClick)
}
})
}
export default Player