@livepeer-frameworks/player-core 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (120) hide show
  1. package/dist/cjs/index.js +19493 -0
  2. package/dist/cjs/index.js.map +1 -0
  3. package/dist/esm/index.js +19398 -0
  4. package/dist/esm/index.js.map +1 -0
  5. package/dist/player.css +2140 -0
  6. package/dist/types/core/ABRController.d.ts +164 -0
  7. package/dist/types/core/CodecUtils.d.ts +54 -0
  8. package/dist/types/core/Disposable.d.ts +61 -0
  9. package/dist/types/core/EventEmitter.d.ts +73 -0
  10. package/dist/types/core/GatewayClient.d.ts +144 -0
  11. package/dist/types/core/InteractionController.d.ts +121 -0
  12. package/dist/types/core/LiveDurationProxy.d.ts +102 -0
  13. package/dist/types/core/MetaTrackManager.d.ts +220 -0
  14. package/dist/types/core/MistReporter.d.ts +163 -0
  15. package/dist/types/core/MistSignaling.d.ts +148 -0
  16. package/dist/types/core/PlayerController.d.ts +665 -0
  17. package/dist/types/core/PlayerInterface.d.ts +230 -0
  18. package/dist/types/core/PlayerManager.d.ts +182 -0
  19. package/dist/types/core/PlayerRegistry.d.ts +27 -0
  20. package/dist/types/core/QualityMonitor.d.ts +184 -0
  21. package/dist/types/core/ScreenWakeLockManager.d.ts +70 -0
  22. package/dist/types/core/SeekingUtils.d.ts +142 -0
  23. package/dist/types/core/StreamStateClient.d.ts +108 -0
  24. package/dist/types/core/SubtitleManager.d.ts +111 -0
  25. package/dist/types/core/TelemetryReporter.d.ts +79 -0
  26. package/dist/types/core/TimeFormat.d.ts +97 -0
  27. package/dist/types/core/TimerManager.d.ts +83 -0
  28. package/dist/types/core/UrlUtils.d.ts +81 -0
  29. package/dist/types/core/detector.d.ts +149 -0
  30. package/dist/types/core/index.d.ts +49 -0
  31. package/dist/types/core/scorer.d.ts +167 -0
  32. package/dist/types/core/selector.d.ts +9 -0
  33. package/dist/types/index.d.ts +45 -0
  34. package/dist/types/lib/utils.d.ts +2 -0
  35. package/dist/types/players/DashJsPlayer.d.ts +102 -0
  36. package/dist/types/players/HlsJsPlayer.d.ts +70 -0
  37. package/dist/types/players/MewsWsPlayer/SourceBufferManager.d.ts +119 -0
  38. package/dist/types/players/MewsWsPlayer/WebSocketManager.d.ts +60 -0
  39. package/dist/types/players/MewsWsPlayer/index.d.ts +220 -0
  40. package/dist/types/players/MewsWsPlayer/types.d.ts +89 -0
  41. package/dist/types/players/MistPlayer.d.ts +25 -0
  42. package/dist/types/players/MistWebRTCPlayer/index.d.ts +133 -0
  43. package/dist/types/players/NativePlayer.d.ts +143 -0
  44. package/dist/types/players/VideoJsPlayer.d.ts +59 -0
  45. package/dist/types/players/WebCodecsPlayer/JitterBuffer.d.ts +118 -0
  46. package/dist/types/players/WebCodecsPlayer/LatencyProfiles.d.ts +64 -0
  47. package/dist/types/players/WebCodecsPlayer/RawChunkParser.d.ts +63 -0
  48. package/dist/types/players/WebCodecsPlayer/SyncController.d.ts +174 -0
  49. package/dist/types/players/WebCodecsPlayer/WebSocketController.d.ts +164 -0
  50. package/dist/types/players/WebCodecsPlayer/index.d.ts +149 -0
  51. package/dist/types/players/WebCodecsPlayer/polyfills/MediaStreamTrackGenerator.d.ts +105 -0
  52. package/dist/types/players/WebCodecsPlayer/types.d.ts +395 -0
  53. package/dist/types/players/WebCodecsPlayer/worker/decoder.worker.d.ts +13 -0
  54. package/dist/types/players/WebCodecsPlayer/worker/types.d.ts +197 -0
  55. package/dist/types/players/index.d.ts +14 -0
  56. package/dist/types/styles/index.d.ts +11 -0
  57. package/dist/types/types.d.ts +363 -0
  58. package/dist/types/vanilla/FrameWorksPlayer.d.ts +143 -0
  59. package/dist/types/vanilla/index.d.ts +19 -0
  60. package/dist/workers/decoder.worker.js +989 -0
  61. package/dist/workers/decoder.worker.js.map +1 -0
  62. package/package.json +80 -0
  63. package/src/core/ABRController.ts +550 -0
  64. package/src/core/CodecUtils.ts +257 -0
  65. package/src/core/Disposable.ts +120 -0
  66. package/src/core/EventEmitter.ts +113 -0
  67. package/src/core/GatewayClient.ts +439 -0
  68. package/src/core/InteractionController.ts +712 -0
  69. package/src/core/LiveDurationProxy.ts +270 -0
  70. package/src/core/MetaTrackManager.ts +753 -0
  71. package/src/core/MistReporter.ts +543 -0
  72. package/src/core/MistSignaling.ts +346 -0
  73. package/src/core/PlayerController.ts +2829 -0
  74. package/src/core/PlayerInterface.ts +432 -0
  75. package/src/core/PlayerManager.ts +900 -0
  76. package/src/core/PlayerRegistry.ts +149 -0
  77. package/src/core/QualityMonitor.ts +597 -0
  78. package/src/core/ScreenWakeLockManager.ts +163 -0
  79. package/src/core/SeekingUtils.ts +364 -0
  80. package/src/core/StreamStateClient.ts +457 -0
  81. package/src/core/SubtitleManager.ts +297 -0
  82. package/src/core/TelemetryReporter.ts +308 -0
  83. package/src/core/TimeFormat.ts +205 -0
  84. package/src/core/TimerManager.ts +209 -0
  85. package/src/core/UrlUtils.ts +179 -0
  86. package/src/core/detector.ts +382 -0
  87. package/src/core/index.ts +140 -0
  88. package/src/core/scorer.ts +553 -0
  89. package/src/core/selector.ts +16 -0
  90. package/src/global.d.ts +11 -0
  91. package/src/index.ts +75 -0
  92. package/src/lib/utils.ts +6 -0
  93. package/src/players/DashJsPlayer.ts +642 -0
  94. package/src/players/HlsJsPlayer.ts +483 -0
  95. package/src/players/MewsWsPlayer/SourceBufferManager.ts +572 -0
  96. package/src/players/MewsWsPlayer/WebSocketManager.ts +241 -0
  97. package/src/players/MewsWsPlayer/index.ts +1065 -0
  98. package/src/players/MewsWsPlayer/types.ts +106 -0
  99. package/src/players/MistPlayer.ts +188 -0
  100. package/src/players/MistWebRTCPlayer/index.ts +703 -0
  101. package/src/players/NativePlayer.ts +820 -0
  102. package/src/players/VideoJsPlayer.ts +643 -0
  103. package/src/players/WebCodecsPlayer/JitterBuffer.ts +299 -0
  104. package/src/players/WebCodecsPlayer/LatencyProfiles.ts +151 -0
  105. package/src/players/WebCodecsPlayer/RawChunkParser.ts +151 -0
  106. package/src/players/WebCodecsPlayer/SyncController.ts +456 -0
  107. package/src/players/WebCodecsPlayer/WebSocketController.ts +564 -0
  108. package/src/players/WebCodecsPlayer/index.ts +1650 -0
  109. package/src/players/WebCodecsPlayer/polyfills/MediaStreamTrackGenerator.ts +379 -0
  110. package/src/players/WebCodecsPlayer/types.ts +542 -0
  111. package/src/players/WebCodecsPlayer/worker/decoder.worker.ts +1360 -0
  112. package/src/players/WebCodecsPlayer/worker/types.ts +276 -0
  113. package/src/players/index.ts +22 -0
  114. package/src/styles/animations.css +21 -0
  115. package/src/styles/index.ts +52 -0
  116. package/src/styles/player.css +2126 -0
  117. package/src/styles/tailwind.css +1015 -0
  118. package/src/types.ts +421 -0
  119. package/src/vanilla/FrameWorksPlayer.ts +367 -0
  120. package/src/vanilla/index.ts +22 -0
@@ -0,0 +1,642 @@
1
+ import { BasePlayer } from '../core/PlayerInterface';
2
+ import { checkProtocolMismatch, getBrowserInfo, isFileProtocol } from '../core/detector';
3
+ import { translateCodec } from '../core/CodecUtils';
4
+ import type { StreamSource, StreamInfo, PlayerOptions, PlayerCapability } from '../core/PlayerInterface';
5
+
6
+ // Player implementation class
7
+ export class DashJsPlayerImpl extends BasePlayer {
8
+ readonly capability: PlayerCapability = {
9
+ name: "Dash.js Player",
10
+ shortname: "dashjs",
11
+ priority: 100, // Below legacy (99) - DASH support is experimental
12
+ mimes: ["dash/video/mp4"]
13
+ };
14
+
15
+ private dashPlayer: any = null;
16
+ private container: HTMLElement | null = null;
17
+ private destroyed = false;
18
+ private debugging = false;
19
+
20
+ // Live duration proxy state (ported from reference dashjs.js:81-122)
21
+ private lastProgress = Date.now();
22
+ private videoProxy: HTMLVideoElement | null = null;
23
+ private streamType: 'live' | 'vod' | 'unknown' = 'unknown';
24
+
25
+ // Subtitle deferred loading (ported from reference dashjs.js:173-197)
26
+ private subsLoaded = false;
27
+ private pendingSubtitleId: string | null = null;
28
+
29
+ isMimeSupported(mimetype: string): boolean {
30
+ return this.capability.mimes.includes(mimetype);
31
+ }
32
+
33
+ isBrowserSupported(mimetype: string, source: StreamSource, streamInfo: StreamInfo): boolean | string[] {
34
+ // Check protocol mismatch
35
+ if (checkProtocolMismatch(source.url)) {
36
+ return false;
37
+ }
38
+
39
+ // Don't use DASH.js if loaded via file://
40
+ if (isFileProtocol()) {
41
+ return false;
42
+ }
43
+
44
+ const browser = getBrowserInfo();
45
+
46
+ // Check MediaSource support (required for DASH.js)
47
+ if (!browser.supportsMediaSource) {
48
+ return false;
49
+ }
50
+
51
+ // Check codec compatibility
52
+ const playableTracks: string[] = [];
53
+ const tracksByType: Record<string, typeof streamInfo.meta.tracks> = {};
54
+
55
+ // Group tracks by type
56
+ for (const track of streamInfo.meta.tracks) {
57
+ if (track.type === 'meta') {
58
+ if (track.codec === 'subtitle') {
59
+ // Check for WebVTT subtitle support
60
+ for (const src of streamInfo.source) {
61
+ if (src.type === 'html5/text/vtt') {
62
+ playableTracks.push('subtitle');
63
+ break;
64
+ }
65
+ }
66
+ }
67
+ continue;
68
+ }
69
+
70
+ if (!tracksByType[track.type]) {
71
+ tracksByType[track.type] = [];
72
+ }
73
+ tracksByType[track.type].push(track);
74
+ }
75
+
76
+ // DASH-incompatible audio codecs for fMP4 segments (even if browser MSE supports them)
77
+ // Standard DASH audio: AAC, MP3, AC-3/E-AC-3. OPUS only works in WebM DASH (not fMP4)
78
+ const DASH_INCOMPATIBLE_AUDIO = ['OPUS', 'Opus', 'opus', 'VORBIS', 'Vorbis'];
79
+
80
+ // Test codec support for video/audio tracks
81
+ for (const [trackType, tracks] of Object.entries(tracksByType)) {
82
+ let hasPlayableTrack = false;
83
+
84
+ for (const track of tracks) {
85
+ // Explicit DASH codec filtering - OPUS in fMP4 DASH doesn't work reliably
86
+ if (trackType === 'audio' && DASH_INCOMPATIBLE_AUDIO.includes(track.codec)) {
87
+ console.debug(`[DashJS] Codec incompatible with DASH fMP4: ${track.codec}`);
88
+ continue;
89
+ }
90
+
91
+ const codecString = translateCodec(track);
92
+ // Use correct container type for audio vs video tracks
93
+ const container = trackType === 'audio' ? 'audio/mp4' : 'video/mp4';
94
+ const mimeType = `${container};codecs="${codecString}"`;
95
+
96
+ if (MediaSource.isTypeSupported && MediaSource.isTypeSupported(mimeType)) {
97
+ hasPlayableTrack = true;
98
+ break;
99
+ } else {
100
+ console.debug(`[DashJS] Codec not supported: ${mimeType}`);
101
+ }
102
+ }
103
+
104
+ if (hasPlayableTrack) {
105
+ playableTracks.push(trackType);
106
+ }
107
+ }
108
+
109
+ return playableTracks.length > 0 ? playableTracks : false;
110
+ }
111
+
112
+ /**
113
+ * Check if current stream is live.
114
+ * Ported from reference dashjs.js live detection.
115
+ */
116
+ private isLiveStream(): boolean {
117
+ if (this.streamType === 'live') return true;
118
+ if (this.streamType === 'vod') return false;
119
+ // Fallback: check video duration
120
+ const v = this.videoElement;
121
+ if (v && (v.duration === Infinity || !isFinite(v.duration))) {
122
+ return true;
123
+ }
124
+ return false;
125
+ }
126
+
127
+ /**
128
+ * Create a Proxy wrapper for the video element that intercepts duration for live streams.
129
+ * Ported from reference dashjs.js:81-122.
130
+ *
131
+ * For live streams, returns synthetic duration = buffer_end + time_since_last_progress
132
+ * This makes the seek bar usable for live content.
133
+ */
134
+ private createVideoProxy(video: HTMLVideoElement): HTMLVideoElement {
135
+ if (!('Proxy' in window)) {
136
+ // Fallback for older browsers
137
+ return video;
138
+ }
139
+
140
+ // Track buffer progress for duration extrapolation
141
+ video.addEventListener('progress', () => {
142
+ this.lastProgress = Date.now();
143
+ });
144
+
145
+ const self = this;
146
+ return new Proxy(video, {
147
+ get(target, key, receiver) {
148
+ // Override duration for live streams (reference dashjs.js:108-116)
149
+ if (key === 'duration' && self.isLiveStream()) {
150
+ const buffered = target.buffered;
151
+ if (buffered.length > 0) {
152
+ const bufferEnd = buffered.end(buffered.length - 1);
153
+ const timeSinceBuffer = (Date.now() - self.lastProgress) / 1000;
154
+ return bufferEnd + timeSinceBuffer;
155
+ }
156
+ }
157
+ const value = Reflect.get(target, key, receiver);
158
+ // Bind functions to the original target
159
+ if (typeof value === 'function') {
160
+ return value.bind(target);
161
+ }
162
+ return value;
163
+ },
164
+ set(target, key, value) {
165
+ return Reflect.set(target, key, value);
166
+ }
167
+ }) as HTMLVideoElement;
168
+ }
169
+
170
+ /**
171
+ * Set up comprehensive event logging.
172
+ * Ported from reference dashjs.js:152-160.
173
+ */
174
+ private setupEventLogging(dashjs: any): void {
175
+ const skipEvents = [
176
+ 'METRIC_ADDED', 'METRIC_UPDATED', 'METRIC_CHANGED', 'METRICS_CHANGED',
177
+ 'FRAGMENT_LOADING_STARTED', 'FRAGMENT_LOADING_COMPLETED',
178
+ 'LOG', 'PLAYBACK_TIME_UPDATED', 'PLAYBACK_PROGRESS'
179
+ ];
180
+
181
+ const events = dashjs.MediaPlayer?.events || {};
182
+ for (const eventKey of Object.keys(events)) {
183
+ if (!skipEvents.includes(eventKey)) {
184
+ this.dashPlayer.on(events[eventKey], (e: any) => {
185
+ if (this.destroyed) return;
186
+ if (this.debugging) {
187
+ console.log('DASH event:', e.type);
188
+ }
189
+ });
190
+ }
191
+ }
192
+ }
193
+
194
+ /**
195
+ * Set up subtitle deferred loading.
196
+ * Ported from reference dashjs.js:173-197.
197
+ */
198
+ private setupSubtitleHandling(): void {
199
+ this.dashPlayer.on('allTextTracksAdded', () => {
200
+ if (this.destroyed) return;
201
+ this.subsLoaded = true;
202
+ if (this.pendingSubtitleId !== null) {
203
+ this.selectTextTrack(this.pendingSubtitleId);
204
+ this.pendingSubtitleId = null;
205
+ }
206
+ });
207
+ }
208
+
209
+ /**
210
+ * Set up stalled indicator handling.
211
+ * Ported from reference dashjs.js:207-211.
212
+ */
213
+ private setupStalledHandling(): void {
214
+ this.videoElement?.addEventListener('progress', () => {
215
+ // Clear any stalled state when buffer advances
216
+ // This integrates with the loading indicator system
217
+ });
218
+ }
219
+
220
+ async initialize(container: HTMLElement, source: StreamSource, options: PlayerOptions): Promise<HTMLVideoElement> {
221
+ this.destroyed = false;
222
+ this.container = container;
223
+ this.subsLoaded = false;
224
+ this.pendingSubtitleId = null;
225
+ container.classList.add('fw-player-container');
226
+
227
+ // Detect stream type from source if available (reference dashjs.js live detection)
228
+ const sourceType = (source as any).type;
229
+ if (sourceType === 'live') {
230
+ this.streamType = 'live';
231
+ } else if (sourceType === 'vod') {
232
+ this.streamType = 'vod';
233
+ } else {
234
+ this.streamType = 'unknown';
235
+ }
236
+
237
+ // Create video element
238
+ const video = document.createElement('video');
239
+ video.classList.add('fw-player-video');
240
+ video.setAttribute('playsinline', '');
241
+ video.setAttribute('crossorigin', 'anonymous');
242
+
243
+ // Apply options (ported from reference dashjs.js:129-142)
244
+ if (options.autoplay) video.autoplay = true;
245
+ if (options.muted) video.muted = true;
246
+ video.controls = options.controls === true;
247
+ // Loop only for VoD (reference dashjs.js: live streams don't loop)
248
+ if (options.loop && this.streamType !== 'live') video.loop = true;
249
+ if (options.poster) video.poster = options.poster;
250
+
251
+ // Create proxy for live duration handling (reference dashjs.js:81-122)
252
+ this.videoProxy = this.createVideoProxy(video);
253
+ this.videoElement = video;
254
+ container.appendChild(video);
255
+
256
+ // Set up event listeners
257
+ this.setupVideoEventListeners(video, options);
258
+ this.setupStalledHandling();
259
+
260
+ try {
261
+ // Dynamic import of DASH.js
262
+ console.debug('[DashJS] Importing dashjs module...');
263
+ const mod = await import('dashjs');
264
+ const dashjs = (mod as any).default || (mod as any);
265
+ console.debug('[DashJS] Module imported:', dashjs);
266
+
267
+ this.dashPlayer = dashjs.MediaPlayer().create();
268
+ console.debug('[DashJS] MediaPlayer created');
269
+
270
+ // Set up event logging (reference dashjs.js:152-160)
271
+ this.setupEventLogging(dashjs);
272
+
273
+ // Set up subtitle handling (reference dashjs.js:173-197)
274
+ this.setupSubtitleHandling();
275
+
276
+ this.dashPlayer.on('error', (e: any) => {
277
+ if (this.destroyed) return;
278
+ const error = `DASH error: ${e?.event?.message || e?.message || 'unknown'}`;
279
+ console.error('[DashJS] Error event:', e);
280
+ this.emit('error', error);
281
+ });
282
+
283
+ // Log key dashjs events for debugging
284
+ this.dashPlayer.on('manifestLoaded', (e: any) => {
285
+ console.debug('[DashJS] manifestLoaded:', e);
286
+ });
287
+ this.dashPlayer.on('canPlay', () => {
288
+ console.debug('[DashJS] canPlay event');
289
+ });
290
+
291
+ // Log stream initialization for debugging
292
+ this.dashPlayer.on('streamInitialized', () => {
293
+ if (this.destroyed) return;
294
+ const isDynamic = this.dashPlayer.isDynamic?.() ?? false;
295
+ console.debug('[DashJS v5] streamInitialized - isDynamic:', isDynamic);
296
+ });
297
+
298
+ // Configure dashjs v5 streaming settings BEFORE initialization
299
+ this.dashPlayer.updateSettings({
300
+ streaming: {
301
+ // Buffer settings
302
+ buffer: {
303
+ fastSwitchEnabled: true,
304
+ stableBufferTime: 16,
305
+ bufferTimeAtTopQuality: 30,
306
+ bufferTimeAtTopQualityLongForm: 60,
307
+ bufferToKeep: 30,
308
+ bufferPruningInterval: 30,
309
+ },
310
+ // Gaps/stall handling
311
+ gaps: {
312
+ jumpGaps: true,
313
+ jumpLargeGaps: true,
314
+ smallGapLimit: 1.5,
315
+ threshold: 0.3,
316
+ },
317
+ // ABR - try disabling to isolate the issue
318
+ abr: {
319
+ autoSwitchBitrate: { video: true, audio: true },
320
+ limitBitrateByPortal: false,
321
+ useDefaultABRRules: true,
322
+ initialBitrate: { video: -1, audio: -1 }, // Let dashjs choose
323
+ },
324
+ // Retry settings - more aggressive
325
+ retryAttempts: {
326
+ MPD: 5,
327
+ MediaSegment: 5,
328
+ InitializationSegment: 5,
329
+ BitstreamSwitchingSegment: 5,
330
+ IndexSegment: 5,
331
+ XLinkExpansion: 3,
332
+ other: 3,
333
+ },
334
+ retryIntervals: {
335
+ MPD: 1000,
336
+ MediaSegment: 1000,
337
+ InitializationSegment: 1000,
338
+ BitstreamSwitchingSegment: 1000,
339
+ IndexSegment: 1000,
340
+ XLinkExpansion: 1000,
341
+ other: 1000,
342
+ },
343
+ // Timeout settings - faster abandonment of slow segments
344
+ timeoutAttempts: {
345
+ MPD: 2,
346
+ MediaSegment: 2, // Abandon after 2 timeout attempts
347
+ InitializationSegment: 2,
348
+ BitstreamSwitchingSegment: 2,
349
+ IndexSegment: 2,
350
+ XLinkExpansion: 1,
351
+ other: 1,
352
+ },
353
+ // Abandon slow segment downloads more quickly
354
+ abandonLoadTimeout: 5000, // 5 seconds instead of default 10
355
+ xhrWithCredentials: false,
356
+ text: { defaultEnabled: false },
357
+ // Live delay settings for live streams
358
+ delay: {
359
+ liveDelay: 4, // Target 4 seconds behind live edge
360
+ liveDelayFragmentCount: null,
361
+ useSuggestedPresentationDelay: true,
362
+ },
363
+ },
364
+ debug: {
365
+ logLevel: 4, // Always debug for now to see what's happening
366
+ },
367
+ });
368
+
369
+ // Add fragment loading event listeners to debug the pending issue
370
+ this.dashPlayer.on('fragmentLoadingStarted', (e: any) => {
371
+ console.debug('[DashJS] Fragment loading started:', e.request?.url?.split('/').pop());
372
+ });
373
+ this.dashPlayer.on('fragmentLoadingCompleted', (e: any) => {
374
+ console.debug('[DashJS] Fragment loading completed:', e.request?.url?.split('/').pop());
375
+ });
376
+ this.dashPlayer.on('fragmentLoadingAbandoned', (e: any) => {
377
+ console.warn('[DashJS] Fragment loading ABANDONED:', e.request?.url?.split('/').pop(), e);
378
+ });
379
+ this.dashPlayer.on('fragmentLoadingFailed', (e: any) => {
380
+ console.error('[DashJS] Fragment loading FAILED:', e.request?.url?.split('/').pop(), e);
381
+ });
382
+
383
+ // dashjs v5: Initialize with URL
384
+ console.debug('[DashJS v5] Initializing with URL:', source.url);
385
+ this.dashPlayer.initialize(video, source.url, options.autoplay ?? false);
386
+ console.debug('[DashJS v5] Initialize called');
387
+
388
+ // Optional subtitle tracks helper from source extras (external tracks)
389
+ try {
390
+ const subs = (source as any).subtitles as Array<{ label: string; lang: string; src: string }>;
391
+ if (Array.isArray(subs)) {
392
+ subs.forEach((s, idx) => {
393
+ const track = document.createElement('track');
394
+ track.kind = 'subtitles';
395
+ track.label = s.label;
396
+ track.srclang = s.lang;
397
+ track.src = s.src;
398
+ if (idx === 0) track.default = true;
399
+ video.appendChild(track);
400
+ });
401
+ }
402
+ } catch {}
403
+
404
+ return video;
405
+
406
+ } catch (error: any) {
407
+ this.emit('error', error.message || String(error));
408
+ throw error;
409
+ }
410
+ }
411
+
412
+ /**
413
+ * Get DASH.js-specific stats for ABR and playback monitoring
414
+ * Updated for dashjs v5 API
415
+ */
416
+ async getStats(): Promise<{
417
+ type: 'dash';
418
+ currentQuality: number;
419
+ bufferLevel: number;
420
+ bitrateInfoList: Array<{ bitrate: number; width: number; height: number }>;
421
+ currentBitrate: number;
422
+ playbackRate: number;
423
+ } | undefined> {
424
+ if (!this.dashPlayer || !this.videoElement) return undefined;
425
+
426
+ try {
427
+ // dashjs v5: getCurrentRepresentationForType returns Representation object
428
+ const currentRep = this.dashPlayer.getCurrentRepresentationForType?.('video');
429
+ // dashjs v5: getRepresentationsByType returns Representation[] (bandwidth instead of bitrate)
430
+ const representations = this.dashPlayer.getRepresentationsByType?.('video') || [];
431
+ const bufferLevel = this.dashPlayer.getBufferLength('video') || 0;
432
+
433
+ // Find current quality index
434
+ const currentIndex = currentRep ? representations.findIndex((r: any) => r.id === currentRep.id) : 0;
435
+
436
+ return {
437
+ type: 'dash',
438
+ currentQuality: currentIndex >= 0 ? currentIndex : 0,
439
+ bufferLevel,
440
+ bitrateInfoList: representations.map((r: any) => ({
441
+ bitrate: r.bandwidth || 0, // v5 uses 'bandwidth' not 'bitrate'
442
+ width: r.width || 0,
443
+ height: r.height || 0,
444
+ })),
445
+ currentBitrate: currentRep?.bandwidth || 0,
446
+ playbackRate: this.videoElement.playbackRate,
447
+ };
448
+ } catch {
449
+ return undefined;
450
+ }
451
+ }
452
+
453
+ /**
454
+ * Set playback rate
455
+ */
456
+ setPlaybackRate(rate: number): void {
457
+ if (this.videoElement) {
458
+ this.videoElement.playbackRate = rate;
459
+ }
460
+ }
461
+
462
+ /**
463
+ * Set source URL for seamless source switching.
464
+ * Ported from reference dashjs.js:166-168.
465
+ */
466
+ setSource(url: string): void {
467
+ if (this.dashPlayer) {
468
+ this.dashPlayer.attachSource(url);
469
+ }
470
+ }
471
+
472
+ /**
473
+ * Get duration using proxy for live streams.
474
+ * Returns synthetic growing duration for live content.
475
+ */
476
+ getDuration(): number {
477
+ // Use proxy if available for live duration handling
478
+ if (this.videoProxy && this.isLiveStream()) {
479
+ return (this.videoProxy as any).duration ?? 0;
480
+ }
481
+ return this.videoElement?.duration ?? 0;
482
+ }
483
+
484
+ /**
485
+ * Jump to live edge for live streams.
486
+ * Uses DASH.js seekToLive API when available.
487
+ */
488
+ jumpToLive(): void {
489
+ const video = this.videoElement;
490
+ if (!video || !this.isLiveStream()) return;
491
+
492
+ // DASH.js has a seekToLive method for live streams
493
+ if (this.dashPlayer && typeof this.dashPlayer.seekToLive === 'function') {
494
+ console.debug('[DashJS] jumpToLive using seekToLive()');
495
+ this.dashPlayer.seekToLive();
496
+ return;
497
+ }
498
+
499
+ // Fallback: seek to end of seekable range
500
+ if (video.seekable && video.seekable.length > 0) {
501
+ const liveEdge = video.seekable.end(video.seekable.length - 1);
502
+ if (isFinite(liveEdge) && liveEdge > 0) {
503
+ console.debug('[DashJS] jumpToLive using seekable.end:', liveEdge);
504
+ video.currentTime = liveEdge;
505
+ }
506
+ }
507
+ }
508
+
509
+ /**
510
+ * Get latency from live edge (for live streams)
511
+ */
512
+ getLiveLatency(): number {
513
+ const video = this.videoElement;
514
+ if (!video || !this.isLiveStream()) return 0;
515
+
516
+ // DASH.js provides live delay metrics
517
+ if (this.dashPlayer && typeof this.dashPlayer.getCurrentLiveLatency === 'function') {
518
+ return this.dashPlayer.getCurrentLiveLatency() * 1000;
519
+ }
520
+
521
+ // Fallback: calculate from seekable end
522
+ if (video.seekable && video.seekable.length > 0) {
523
+ const liveEdge = video.seekable.end(video.seekable.length - 1);
524
+ if (isFinite(liveEdge)) {
525
+ return Math.max(0, (liveEdge - video.currentTime) * 1000);
526
+ }
527
+ }
528
+
529
+ return 0;
530
+ }
531
+
532
+ async destroy(): Promise<void> {
533
+ this.destroyed = true;
534
+ this.subsLoaded = false;
535
+ this.pendingSubtitleId = null;
536
+ this.videoProxy = null;
537
+
538
+ if (this.dashPlayer) {
539
+ try {
540
+ this.dashPlayer.reset();
541
+ } catch (e) {
542
+ console.warn('Error destroying DASH.js:', e);
543
+ }
544
+ this.dashPlayer = null;
545
+ }
546
+
547
+ if (this.videoElement && this.container) {
548
+ try { this.container.removeChild(this.videoElement); } catch {}
549
+ }
550
+
551
+ this.videoElement = null;
552
+ this.container = null;
553
+ this.listeners.clear();
554
+ }
555
+
556
+ getQualities(): Array<{ id: string; label: string; bitrate?: number; width?: number; height?: number; isAuto?: boolean; active?: boolean }> {
557
+ const out: any[] = [];
558
+ const v = this.videoElement;
559
+ if (!this.dashPlayer || !v) return out;
560
+ try {
561
+ // dashjs v5: getRepresentationsByType returns Representation[] (bandwidth instead of bitrate)
562
+ const representations = this.dashPlayer.getRepresentationsByType?.('video') || [];
563
+ const settings = this.dashPlayer.getSettings?.();
564
+ const isAutoEnabled = settings?.streaming?.abr?.autoSwitchBitrate?.video !== false;
565
+
566
+ out.push({ id: 'auto', label: 'Auto', isAuto: true, active: isAutoEnabled });
567
+ representations.forEach((rep: any, i: number) => {
568
+ out.push({
569
+ id: String(i),
570
+ label: rep.height ? `${rep.height}p` : `${Math.round((rep.bandwidth || 0) / 1000)}kbps`,
571
+ bitrate: rep.bandwidth, // v5 uses 'bandwidth'
572
+ width: rep.width,
573
+ height: rep.height
574
+ });
575
+ });
576
+ } catch {}
577
+ return out;
578
+ }
579
+
580
+ selectQuality(id: string): void {
581
+ if (!this.dashPlayer) return;
582
+ if (id === 'auto') {
583
+ this.dashPlayer.updateSettings({ streaming: { abr: { autoSwitchBitrate: { video: true } } } });
584
+ return;
585
+ }
586
+ const idx = parseInt(id, 10);
587
+ if (!isNaN(idx)) {
588
+ this.dashPlayer.updateSettings({ streaming: { abr: { autoSwitchBitrate: { video: false } } } });
589
+ // dashjs v5: setRepresentationForTypeByIndex instead of setQualityFor
590
+ try { this.dashPlayer.setRepresentationForTypeByIndex?.('video', idx); } catch {}
591
+ }
592
+ }
593
+
594
+ // Captions via native text tracks or dash.js API
595
+ getTextTracks(): Array<{ id: string; label: string; lang?: string; active: boolean }> {
596
+ const v = this.videoElement;
597
+ if (!this.dashPlayer || !v) return [];
598
+ const out: any[] = [];
599
+ try {
600
+ const textTracks = (v.textTracks || []) as any;
601
+ for (let i = 0; i < textTracks.length; i++) {
602
+ const tt = textTracks[i];
603
+ out.push({ id: String(i), label: tt.label || `CC ${i+1}`, lang: (tt as any).language, active: tt.mode === 'showing' });
604
+ }
605
+ } catch {}
606
+ return out;
607
+ }
608
+
609
+ selectTextTrack(id: string | null): void {
610
+ const v = this.videoElement;
611
+ if (!this.dashPlayer || !v) return;
612
+
613
+ // Deferred loading: wait for allTextTracksAdded (reference dashjs.js:180-186)
614
+ if (!this.subsLoaded) {
615
+ this.pendingSubtitleId = id;
616
+ return;
617
+ }
618
+
619
+ // Try dash.js API first (reference dashjs.js:193-197)
620
+ try {
621
+ const dashTracks = this.dashPlayer.getTracksFor('text');
622
+ if (dashTracks && dashTracks.length > 0) {
623
+ const idx = id === null ? -1 : parseInt(id, 10);
624
+ if (idx >= 0 && idx < dashTracks.length) {
625
+ this.dashPlayer.setTextTrack(idx);
626
+ return;
627
+ } else if (id === null || idx < 0) {
628
+ // Disable all dash.js text tracks
629
+ this.dashPlayer.setTextTrack(-1);
630
+ return;
631
+ }
632
+ }
633
+ } catch {}
634
+
635
+ // Fallback to native text tracks
636
+ const list = v.textTracks as TextTrackList;
637
+ for (let i = 0; i < list.length; i++) {
638
+ const tt = list[i];
639
+ if (id !== null && String(i) === id) tt.mode = 'showing'; else tt.mode = 'disabled';
640
+ }
641
+ }
642
+ }