-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathVideoHandler.ts
More file actions
599 lines (548 loc) · 19.7 KB
/
Copy pathVideoHandler.ts
File metadata and controls
599 lines (548 loc) · 19.7 KB
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
import { IComponentInstance } from '@amazon-devices/react-native-kepler';
import { VideoPlayer } from '@amazon-devices/react-native-w3cmedia';
import React, { useCallback } from 'react';
import { Platform } from 'react-native';
import { VideoFileTypes } from '../data/videos';
import { TitleData } from '../types/TitleData';
import {
ShakaPlayer,
ShakaPlayerSettings,
} from '../w3cmedia/shakaplayer/ShakaPlayer';
import { AppOverrideMediaControlHandler } from './AppOverrideMediaControlHandler';
import { SKIP_INTERVAL_SECONDS } from './videoPlayerValues';
// Default video resolution settings based on platform (TV vs mobile/web)
const DEFAULT_ABR_WIDTH: number = Platform.isTV ? 3840 : 1919;
const DEFAULT_ABR_HEIGHT: number = Platform.isTV ? 2160 : 1079;
// Status types returned when deinitializing the media player
type MediaPlayerDeInitStatus = 'success' | 'timedout' | 'invalid';
/**
* VideoHandler Class
*
* This class manages video playback functionality including:
* - Video player initialization and destruction
* - Loading different video formats (MP4, HLS, DASH)
* - Handling video events (play, pause, seek, error, etc.)
* - Managing subtitles and text tracks
* - Buffering state management
* - Integration with Kepler Media Controls (KMC)
*
* The class supports both static media (MP4) and adaptive streaming (HLS/DASH)
* using the Shaka Player for adaptive content.
*/
export class VideoHandler {
// Core video player references
/** Reference to the main video player instance */
public videoRef: React.MutableRefObject<VideoPlayer | null>;
/** Reference to the Shaka player for adaptive streaming content */
public player: React.MutableRefObject<ShakaPlayer | null>;
/** Current video format type (MP4, HLS, DASH, etc.) */
public selectedFileType: string;
/** Video metadata and source information */
public data: TitleData;
// State management functions passed from React components
/** Updates video initialization status */
public setIsVideoInitialized: React.Dispatch<React.SetStateAction<boolean>>;
/** Updates video ended status */
public setIsVideoEnded: React.Dispatch<React.SetStateAction<boolean>>;
/** Updates video error status */
public setIsVideoError: React.Dispatch<React.SetStateAction<boolean>>;
/** Updates elapsed playback time in minutes */
public setVideoPlayElapsedTimeM: React.Dispatch<React.SetStateAction<number>>;
/** Updates buffering indicator visibility */
public setShowBuffering: React.Dispatch<React.SetStateAction<boolean>>;
/**
* Constructor - Initializes the VideoHandler with required dependencies
*
* @param videoRef - Reference to the video player instance
* @param player - Reference to the Shaka player for adaptive streaming
* @param data - Video metadata including URI, format, and text tracks
* @param setIsVideoInitialized - Function to update video initialization state
* @param setIsVideoEnded - Function to update video ended state
* @param setIsVideoError - Function to update video error state
* @param setVideoPlayElapsedTimeM - Function to update elapsed time in minutes
* @param setShowBuffering - Function to update buffering indicator state
*/
constructor(
videoRef: React.MutableRefObject<VideoPlayer | null>,
player: React.MutableRefObject<ShakaPlayer | null>,
data: TitleData,
setIsVideoInitialized: React.Dispatch<React.SetStateAction<boolean>>,
setIsVideoEnded: React.Dispatch<React.SetStateAction<boolean>>,
setIsVideoError: React.Dispatch<React.SetStateAction<boolean>>,
setVideoPlayElapsedTimeM: React.Dispatch<React.SetStateAction<number>>,
setShowBuffering: React.Dispatch<React.SetStateAction<boolean>>,
) {
this.videoRef = videoRef;
this.player = player;
this.data = data;
this.selectedFileType = data.format;
this.setIsVideoInitialized = setIsVideoInitialized;
this.setIsVideoEnded = setIsVideoEnded;
this.setIsVideoError = setIsVideoError;
this.setVideoPlayElapsedTimeM = setVideoPlayElapsedTimeM;
this.setShowBuffering = setShowBuffering;
}
/**
* Updates the media data and file type for the current video
* Used when switching between different video sources
*
* @param mediaData - New video metadata to set
*/
setMediaData = (mediaData: TitleData) => {
this.data = mediaData;
this.selectedFileType = mediaData.format;
};
/**
* Prepares and initializes the video player for playback
*
* This method:
* 1. Destroys any existing video elements
* 2. Creates a new VideoPlayer instance if needed
* 3. Sets up Kepler Media Controls (KMC) integration
* 4. Initializes the video player
* 5. Sets up event listeners
* 6. Loads the video content
*
* @param componentInstance - Optional Kepler component for media control integration
*/
preBufferVideo = useCallback(
async (componentInstance?: IComponentInstance) => {
await this.destroyVideoElements();
console.info(
'[VideoHandler.ts] - preBufferVideo - Attempt to prebuffer Video Player - ComponentInstance',
componentInstance,
);
if (this.videoRef.current == null) {
console.info(
'[VideoHandler.ts] - preBufferVideo - Current video is null, create new Video Player',
);
this.videoRef.current = new VideoPlayer();
}
(global as any).gmedia = this.videoRef.current;
// KMC (Kepler Media Controls) integration
try {
if (componentInstance) {
console.info(
'[VideoHandler.ts] - preBufferVideo - KMC : set Media Control Focus',
);
await this.videoRef.current.setMediaControlFocus(
componentInstance,
new AppOverrideMediaControlHandler(
this.videoRef.current as VideoPlayer,
false,
),
);
} else {
console.log(
'[VideoHandler.ts] - preBufferVideo - KMC : Skipped setting KMC',
);
}
} catch (error) {
console.error('Error during KMC execution', error);
}
// Initialize the video player and set up content loading
this.videoRef.current
.initialize()
.then(() => {
console.log(
'[VideoHandler.ts] - preBufferVideo - Video Player Initialized',
);
this.setupEventListeners();
this.videoRef!!.current!!.autoplay = false;
this.loadVideoElements();
})
.catch((error) => {
console.error(
'[VideoHandler.ts] - preBufferVideo - Failed to initialize Video',
);
console.error(error);
});
},
[],
);
/**
* Loads video content and configures player settings
*
* This method:
* 1. Loads subtitles if available
* 2. Configures player settings (autoplay, seek interval)
* 3. Chooses appropriate player based on video format:
* - Static player for MP4 files
* - Adaptive player (Shaka) for streaming formats (HLS, DASH)
*/
loadVideoElements = useCallback(() => {
if (this.videoRef !== null && this.videoRef.current !== null) {
try {
this.loadSubtitles();
} catch (error) {
console.error(
'[VideoHandler.ts] - loadVideoElements - Failed to load subtitles',
);
console.error(error);
}
this.videoRef.current.autoplay = false;
this.videoRef.current.defaultSeekIntervalInSec = SKIP_INTERVAL_SECONDS;
// Choose player type based on video format
if (this.selectedFileType === VideoFileTypes.MP4) {
this.loadStaticMediaPlayer(this.videoRef.current);
} else {
this.loadAdaptiveMediaPlayer();
}
}
}, []);
/**
* Loads static media content (MP4 files)
*
* Used for simple video files that don't require adaptive streaming.
* Sets the video source, pauses playback, and loads the content.
*
* @param video - Video player instance with basic playback controls
*/
loadStaticMediaPlayer = useCallback(
(video: {
src: string;
autoplay: boolean;
pause: () => void;
load: () => void;
}) => {
video.src = this.data.uri;
video.pause();
console.log(
`[VideoHandler.ts] - loadStaticMediaPlayer - Loading static player with: ${video.src}`,
);
video.load();
},
[],
);
/**
* Loads adaptive streaming content (HLS, DASH, etc.) using Shaka Player
*
* This method:
* 1. Configures Shaka Player settings (ABR, resolution limits)
* 2. Initializes the Shaka Player instance
* 3. Loads the streaming content
* 4. Handles any loading errors
*
* Used for streaming formats that support adaptive bitrate streaming.
*/
loadAdaptiveMediaPlayer = useCallback(() => {
if (this.videoRef?.current) {
try {
// Configure Shaka Player settings
const settings: ShakaPlayerSettings = {
secure: false,
abrEnabled: false, // Adaptive bitrate disabled
abrMaxWidth: DEFAULT_ABR_WIDTH,
abrMaxHeight: DEFAULT_ABR_HEIGHT,
};
this.player.current = new ShakaPlayer(this.videoRef.current, settings);
console.log(
'[VideoHandler.ts] - Shaka player initialized successfully',
);
if (this.player.current) {
try {
this.player.current.load(this.data, false);
console.log(
`[VideoHandler.ts] - loadStaticMediaPlayer - Loading Adaptive Media player (Shaka) with: ${this.data}`,
);
} catch (loadError) {
console.error(
'[VideoHandler.ts] - Error during Shaka player load:',
loadError,
);
this.setIsVideoError(true);
}
}
} catch (error) {
console.error(
'[VideoHandler.ts] -Error initializing Shaka player:',
error,
);
this.setIsVideoError(true);
}
} else {
console.error('[VideoHandler.ts] - Video reference is not initialized');
this.setIsVideoError(true);
}
}, []);
/**
* Loads subtitle tracks for the video
*
* Iterates through available text tracks in the video data and adds them
* to the video player. Each subtitle track includes language, label, URI,
* and MIME type information.
*/
loadSubtitles = useCallback(() => {
const source = this.data;
if (source?.textTrack && source.textTrack.length > 0) {
for (let i: number = 0; i < source.textTrack.length; i++) {
console.log(
`[VideoHandler.ts] - loadSubtitles - Loading subtitles from: ${source.uri} [${source.textTrack[i].language}]`,
);
this.videoRef.current?.addTextTrack(
'subtitles',
source.textTrack[i].label,
source.textTrack[i].language,
source.textTrack[i].uri,
source.textTrack[i].mimeType,
);
console.log(
`[VideoHandler.ts] - loadSubtitles - Loaded ${
this.videoRef.current?.textTracks.length || 0
} subtitles:`,
);
}
}
}, []);
// === VIDEO EVENT HANDLERS ===
// These methods handle various video player events and update component state accordingly
/**
* Handles the 'loadedmetadata' event
* Fired when video metadata (duration, dimensions, etc.) has been loaded
*/
onLoadeMetaData = () => {
if (this.videoRef.current) {
console.log('[VideoHandler.ts] - onLoadeMetaData');
this.setIsVideoInitialized(true);
}
};
/**
* Handles the 'ended' event
* Fired when video playback has reached the end
*/
onEnded = () => {
if (this.videoRef.current?.ended) {
console.log('[VideoHandler.ts] - onEnded');
this.setIsVideoEnded(true);
}
};
/**
* Handles video playback errors
*
* Categorizes errors into different types (DRM, FORMAT, NETWORK, DECODE)
* and logs detailed error information for debugging. Updates the error state
* to notify the UI of playback issues.
*
* @param event - Error event from the video player
*/
onError = (event: any) => {
if (!event?.target) {
console.error('[VideoHandler.ts] - Invalid error event');
this.setIsVideoError(true);
return;
}
const target = event.target;
const error = target.mediaControlStateUtil?.mError;
const errorMessage = error?.message_ || '';
// Categorize the error type for better debugging
let errorCategory = 'UNKNOWN';
if (
errorMessage.includes('kepler_viddec_hw_h264') ||
errorMessage.includes('mediaxform')
) {
errorCategory = 'DRM';
} else if (errorMessage.includes('not-negotiated')) {
errorCategory = 'FORMAT';
} else if (error?.code_ === 2) {
errorCategory = 'NETWORK';
} else if (error?.code_ === 3) {
errorCategory = 'DECODE';
}
// Log comprehensive error information
console.error(
'[VideoHandler.ts] - Streaming Error:\n' +
`Category: ${errorCategory}\n` +
`Format: ${this.selectedFileType}\n` +
`Position: ${target.mediaControlStateUtil?.currentPosition || 0}\n` +
`DRM Blocked: ${
target.decryptionBlockedOnKey_ || target.playbackBlockedOnKey_
}\n` +
`Has DRM Keys: ${!!target.mediaKeys}\n` +
`Details: ${errorMessage}\n` +
`uri: ${this.data?.uri}`,
);
this.setIsVideoError(true);
};
/**
* Handles the 'timeupdate' event
*
* Updates elapsed time in minutes and manages buffering state for static content.
* For MP4 files, this event is used to hide buffering since other events
* may not fire reliably.
*/
onTimeUpdate = () => {
// Update elapsed time in minutes
if (this.videoRef.current) {
const currentTimeInMinutes = Math.floor(
this.videoRef.current.currentTime / 60,
);
this.setVideoPlayElapsedTimeM(currentTimeInMinutes);
}
// For static media (MP4), use timeupdate to hide buffering
// since seeked/playing events may not fire reliably
if (this.isStaticMediaContent()) {
this.setShowBuffering(false);
}
};
/**
* Checks if the current content is static media (MP4)
* @returns true if content is MP4, false for streaming formats
*/
isStaticMediaContent = () => {
return this.selectedFileType === VideoFileTypes.MP4;
};
/**
* Handles the 'waiting' event - shows buffering indicator
* Fired when playback stops due to lack of data
*/
onWaiting = () => {
this.setShowBuffering(true);
};
/**
* Handles the 'seeking' event - shows buffering indicator
* Fired when user starts seeking to a new position
*/
onSeeking = () => {
this.setShowBuffering(true);
};
/**
* Handles the 'seeked' event - hides buffering indicator
* Fired when seeking operation completes
*/
onSeeked = () => {
this.setShowBuffering(false);
};
/**
* Handles the 'playing' event - hides buffering indicator
* Fired when playback starts after being paused or delayed due to lack of data
*/
onPlaying = () => {
this.setShowBuffering(false);
};
// === EVENT LISTENER MANAGEMENT ===
/**
* Sets up all video event listeners
*
* Attaches event handlers for:
* - loadedmetadata: Video metadata loaded
* - timeupdate: Playback time changes
* - ended: Video playback finished
* - error: Playback errors
* - waiting: Buffering started
* - seeking: Seek operation started
* - seeked: Seek operation completed
* - playing: Playback resumed
*/
setupEventListeners = useCallback(() => {
this.videoRef.current?.addEventListener(
'loadedmetadata',
this.onLoadeMetaData,
);
this.videoRef.current?.addEventListener('timeupdate', this.onTimeUpdate);
this.videoRef.current?.addEventListener('ended', this.onEnded);
this.videoRef.current?.addEventListener('error', this.onError);
this.videoRef.current?.addEventListener('waiting', this.onWaiting);
this.videoRef.current?.addEventListener('seeking', this.onSeeking);
this.videoRef.current?.addEventListener('seeked', this.onSeeked);
this.videoRef.current?.addEventListener('playing', this.onPlaying);
}, []);
/**
* Removes all video event listeners
*
* Cleans up event listeners to prevent memory leaks when the video
* player is destroyed or when switching to new content.
*/
removeEventListeners = useCallback(() => {
this.videoRef.current?.removeEventListener(
'loadedmetadata',
this.onLoadeMetaData,
);
this.videoRef.current?.removeEventListener('timeupdate', this.onTimeUpdate);
this.videoRef.current?.removeEventListener('ended', this.onEnded);
this.videoRef.current?.removeEventListener('error', this.onError);
this.videoRef.current?.removeEventListener('waiting', this.onWaiting);
this.videoRef.current?.removeEventListener('seeking', this.onSeeking);
this.videoRef.current?.removeEventListener('seeked', this.onSeeked);
this.videoRef.current?.removeEventListener('playing', this.onPlaying);
}, []);
// === CLEANUP AND DESTRUCTION ===
/**
* Synchronously destroys the media player and cleans up resources
*
* This method performs a complete cleanup:
* 1. Pauses current playback
* 2. Removes all event listeners
* 3. Unloads and destroys Shaka player (for streaming content)
* 4. Deinitializes the video player with timeout
* 5. Clears global references
*
* @param timeout - Maximum time to wait for deinitialization (default: 1500ms)
* @returns true if destruction was successful, false otherwise
*/
destroyMediaPlayerSync = (timeout: number = 1500): boolean => {
console.log(
'[VideoHandler.ts] - Starting synchronous media player destruction',
);
try {
// Pause current playback
if (this.videoRef.current) {
console.log('[VideoHandler.ts]: pausing media');
this.videoRef.current.pause();
}
// Remove all event listeners to prevent memory leaks
this.removeEventListeners();
// Destroy Shaka player for streaming content
if (this.player.current) {
console.log('[VideoHandler.ts] - destroying player');
if (this.selectedFileType !== VideoFileTypes.MP4) {
console.log(
'[VideoHandler.ts] - destroying Adaptive Media Player (shaka)',
);
this.player.current.unload();
}
this.player.current = null;
console.log('[VideoHandler.ts] - player destroyed successfully');
}
// Deinitialize the main video player
if (this.videoRef.current) {
console.log('[VideoHandler.ts] - deinitializing media synchronously');
const result: MediaPlayerDeInitStatus =
this.videoRef.current.deinitializeSync(timeout);
if (result !== 'success') {
console.error(
`[VideoHandler.ts] - Deinitialize sync failed - ${result}`,
);
return false;
}
console.log(
'[VideoHandler.ts] - Deinitialize sync completed successfully',
);
(global as any).gmedia = null;
this.videoRef.current = null;
}
console.log('[VideoHandler.ts] - Sync destruction completed');
return true;
} catch (err) {
console.error('[VideoHandler.ts] - Error during destruction: ', err);
return false;
}
};
/**
* Destroys video elements and cleans up resources
*
* Public method to trigger video player cleanup. Checks if a video
* reference exists before attempting destruction.
*
* @returns true if destruction was successful or no video to destroy, false on error
*/
destroyVideoElements = (): boolean => {
if (!this.videoRef.current) {
console.log('[VideoHandler.ts] - No video reference to destroy');
return false;
}
console.log(
'[VideoHandler.ts] - Triggering synchronous video elements destruction',
);
return this.destroyMediaPlayerSync();
};
}