-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathquality-levels.js
More file actions
482 lines (443 loc) · 16 KB
/
quality-levels.js
File metadata and controls
482 lines (443 loc) · 16 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
import videojs from 'video.js';
import Hls from 'hls.js';
const qualityLevels = (player, options) => {
const levelToRenditionHls = level => {
let levelUrl =
Array.isArray(level.url) && level.url.length > 1 ? level.url[level.urlId] : level.url;
let rendition = {
id: levelUrl,
width: level.width,
height: level.height,
bandwidth: level.bitrate, // bitrate => bandwidth
frameRate: 0,
enabled: enableRendition => {
var tech = player.tech({ IWillNotUseThisInPlugins: true });
if (
typeof tech.sourceHandler_ != 'undefined' &&
typeof tech.sourceHandler_.hls != 'undefined' &&
tech.sourceHandler_.hls != null
) {
const hls = tech.sourceHandler_.hls;
const levelIndex = hls.levels.findIndex(
l => (Array.isArray(l.url) && l.url.length > 1 ? l.url[l.urlId] : l.url) === levelUrl
);
if (levelIndex >= 0 && enableRendition) {
hls.currentLevel = levelIndex;
}
}
return enableRendition;
}
};
return rendition;
};
const levelToRenditionDash = level => ({
id: level.id,
width: level.width,
height: level.height,
bandwidth: level.bandwidth,
enabled: enableRendition => {
const dash = player.dash;
if (dash && dash.mediaPlayer) {
if (enableRendition) {
dash.mediaPlayer.updateSettings({
streaming: { abr: { autoSwitchBitrate: { video: false, audio: false } } }
});
// Find the correct quality index by resolution
const targetQualityIndex = findDashQualityIndex(level.width, level.height);
if (targetQualityIndex >= 0) {
dash.mediaPlayer.setQualityFor('video', targetQualityIndex);
// Set audio quality if mapping exists
if (dash.audioMapper && dash.audioMapper[targetQualityIndex] !== undefined) {
dash.mediaPlayer.setQualityFor('audio', dash.audioMapper[targetQualityIndex]);
}
}
}
}
return enableRendition;
}
});
const getRenditionsDash = () => {
var dash = player.dash;
if (
typeof dash != 'undefined' &&
dash != null &&
typeof dash.mediaPlayer != 'undefined' &&
dash.mediaPlayer != null
) {
var streamInfo = dash.mediaPlayer.getActiveStream().getStreamInfo();
var dashAdapter = dash.mediaPlayer.getDashAdapter();
if (dashAdapter && streamInfo) {
const periodIdx = streamInfo.index;
var adaptation = dashAdapter.getAdaptationForType(periodIdx, 'video', streamInfo);
}
return adaptation.Representation_asArray;
}
return [];
};
// Helper function to find DASH quality index by resolution
const findDashQualityIndex = (targetWidth, targetHeight) => {
var dash = player.dash;
if (dash && dash.mediaPlayer) {
const availableQualities = dash.mediaPlayer.getBitrateInfoListFor('video');
const targetQuality = availableQualities.find(q =>
q.width === targetWidth && q.height === targetHeight
);
return targetQuality ? targetQuality.qualityIndex : -1;
}
return -1;
};
// Clean up quality levels and reset state for new source
const cleanupQualityLevels = () => {
let qualityLevels = player.qualityLevels;
if (typeof qualityLevels === 'function') {
qualityLevels = player.qualityLevels();
// Clear all existing quality levels
qualityLevels.dispose();
debugLog('Quality levels cleaned up for new source');
}
// Clean up audio tracks from previous source
const audioTrackList = player.audioTracks();
if (audioTrackList && audioTrackList.length > 0) {
// Remove all existing audio tracks
for (let i = audioTrackList.length - 1; i >= 0; i--) {
audioTrackList.removeTrack(audioTrackList[i]);
}
debugLog('Audio tracks cleaned up for new source');
}
// Clean up DASH-specific state
const dash = player.dash;
if (dash && dash.audioMapper) {
delete dash.audioMapper;
debugLog('DASH audio mapper cleaned up for new source');
}
// Reset previous resolution for qualitychanged events
previousResolution = null;
};
// Update the QualityLevels list of renditions
const populateLevels = (levels, abrType) => {
// Clean up existing quality levels before adding new ones
cleanupQualityLevels();
let qualityLevels = player.qualityLevels;
if (typeof qualityLevels === 'function') {
qualityLevels = player.qualityLevels();
debugLog('QualityLevels', qualityLevels);
switch (abrType) {
case 'hls':
for (let l = 0; l < levels.length; l++) {
let level = levels[l];
let rendition = levelToRenditionHls(level);
qualityLevels.addQualityLevel(rendition);
}
break;
case 'dash': {
// Set up audio mapping for DASH
const dash = player.dash;
if (!dash) break;
const videoRates = levels;
const audioRates = dash.mediaPlayer.getBitrateInfoListFor('audio') || [];
const normalizeFactor = videoRates.length > 0 ? videoRates[videoRates.length - 1].bandwidth : 1;
dash.audioMapper = videoRates.map((rate) =>
Math.round((rate.bandwidth / normalizeFactor) * (audioRates.length - 1))
);
for (let l = 0; l < levels.length; l++) {
let level = levels[l];
let rendition = levelToRenditionDash(level);
qualityLevels.addQualityLevel(rendition);
}
break;
}
default:
return;
}
} else {
console.warn('QualityLevels not supported');
}
};
let previousResolution = null; // for qualitychanged event data
// Update the selected rendition
const populateQualityLevelsChange = currentLevel => {
let qualityLevels = player.qualityLevels;
if (typeof qualityLevels === 'function') {
qualityLevels = player.qualityLevels();
if (qualityLevels.length == 0) {
console.warn('ERROR - no quality levels found! Patching populate levels first');
var tech = player.tech({ IWillNotUseThisInPlugins: true });
if (
typeof tech.sourceHandler_ != 'undefined' &&
typeof tech.sourceHandler_.hls != 'undefined' &&
tech.sourceHandler_.hls != null
) {
const hls = tech.sourceHandler_.hls;
populateLevels(hls.levels, 'hls');
}
}
qualityLevels.selectedIndex_ = currentLevel;
qualityLevels.trigger({ type: 'change', selectedIndex: currentLevel });
}
};
// Custom 'qualitychanged' event
const populateQualityChangedEvent = currentLevel => {
if (currentLevel < 0) {
return;
}
let qualityLevels = player.qualityLevels;
let level = null;
// Using videojs-contrib-quality-levels
if (typeof qualityLevels === 'function') {
qualityLevels = player.qualityLevels();
level = qualityLevels.levels_[currentLevel];
debugLog('Custom qualitychanged', 'using videojs-contrib-quality-levels');
} else {
// hls.js directly
var tech = player.tech({ IWillNotUseThisInPlugins: true });
if (
typeof tech.sourceHandler_ != 'undefined' &&
typeof tech.sourceHandler_.hls != 'undefined' &&
tech.sourceHandler_.hls != null
) {
const hls = tech.sourceHandler_.hls;
level = hls.levels[currentLevel];
debugLog('Custom qualitychanged', 'using hls.js directly');
if (currentLevel !== hls.currentLevel) {
debugLog('ERROR - new level differs from hls.js');
}
} else {
// dash.js directly
var dash = player.dash;
if (
typeof dash != 'undefined' &&
dash != null &&
typeof dash.mediaPlayer != 'undefined' &&
dash.mediaPlayer != null
) {
let renditions = getRenditionsDash();
level = renditions[currentLevel];
debugLog('Custom qualitychanged', 'using dash.js directly');
}
}
}
// Add null check for level
if (!level) {
debugLog('Warning: Level is undefined in populateQualityChangedEvent', { currentLevel });
return;
}
let currentRes = { width: level.width, height: level.height };
if (previousResolution !== currentRes) {
let data = {
from: previousResolution,
to: currentRes
};
// Trigger custom 'qualitychanged' event on videojs
player.trigger({ type: 'qualitychanged', eventData: data });
}
previousResolution = currentRes;
// Add detailed logging of current rendition
debugLog('Current Rendition', {
index: currentLevel,
resolution: `${level.width}x${level.height}`,
bitrate: `${Math.round(level.bitrate / 1000)} kbps`,
url: Array.isArray(level.url) ? level.url[level.urlId] : level.url,
details: level
});
};
const debugLog = (label, data) => {
if (options.debug) {
console.log(
`%c ${label}`,
'background: #3498db; color: white; padding: 2px 4px; border-radius: 2px;',
data
);
}
};
const logAudioTrackInfo = () => {
var tech = player.tech({ IWillNotUseThisInPlugins: true });
if (
typeof tech.sourceHandler_ != 'undefined' &&
typeof tech.sourceHandler_.hls != 'undefined' &&
tech.sourceHandler_.hls != null
) {
const hls = tech.sourceHandler_.hls;
const audioTrackId = hls.audioTrack;
const len = hls.audioTracks.length;
for (let i = 0; i < len; i++) {
if (audioTrackId === i) {
debugLog(`audio track [${i}] ${hls.audioTracks[i].name} - enabled`, hls.audioTracks[i]);
} else {
debugLog(`audio track [${i}] ${hls.audioTracks[i].name} - disabled`, hls.audioTracks[i]);
}
}
}
};
// Helper to synchronise Video.js AudioTrackList with hls.js active track
const updateVjsAudioTracksEnabled = activeIndex => {
const list = player.audioTracks();
if (!list) return;
for (let i = 0; i < list.length; i++) {
list[i].enabled = i === activeIndex;
}
// Sync complete – log for debug
logAudioTrackInfo();
};
// Flush buffered audio by performing a tiny seek outside current range
const flushBufferedAudio = () => {
try {
const cur = player.currentTime();
const delta = 0.5; // 500 ms gap to escape current buffer
const target = Math.min(cur + delta, player.duration() - 0.1);
player.currentTime(target);
player.currentTime(cur);
} catch (e) {
debugLog('Error while flushing buffered audio', e);
}
};
// Force hls.js to switch track and immediately start loading fragments for it
const selectAudioTrack = index => {
var tech = player.tech({ IWillNotUseThisInPlugins: true });
if (
typeof tech.sourceHandler_ != 'undefined' &&
typeof tech.sourceHandler_.hls != 'undefined' &&
tech.sourceHandler_.hls != null
) {
const hls = tech.sourceHandler_.hls;
if (hls.audioTrack === index) {
return;
}
hls.audioTrack = index;
// Quickly restart loading to fill the buffer of the new audio group
if (hls.media) {
try {
hls.stopLoad();
hls.startLoad(0);
} catch (e) {
debugLog('Error while reloading after audioTrack switch', e);
}
}
logAudioTrackInfo();
// will flush once switch is confirmed (AUDIO_TRACK_SWITCHED)
hls.once(Hls.Events.AUDIO_TRACK_SWITCHED, () => {
flushBufferedAudio();
});
}
};
// Audio track handling
const addAudioTrackVideojs = (track, hls) => {
const vjsTrack = new videojs.AudioTrack({
id: `${track.type}-id_${track.id}-groupId_${track.groupId}-${track.name}`,
kind: 'translation',
label: track.name,
language: track.lang,
enabled: hls.audioTrack === hls.audioTracks.indexOf(track),
default: track.default
});
// Add the track to the player's audio track list.
player.audioTracks().addTrack(vjsTrack);
};
const initAudioTrackInfo = () => {
var tech = player.tech({ IWillNotUseThisInPlugins: true });
if (
typeof tech.sourceHandler_ != 'undefined' &&
typeof tech.sourceHandler_.hls != 'undefined' &&
tech.sourceHandler_.hls != null
) {
const hls = tech.sourceHandler_.hls;
const len = hls.audioTracks.length;
for (let i = 0; i < len; i++) {
addAudioTrackVideojs(hls.audioTracks[i], hls);
}
}
// Listen to the "change" event.
var audioTrackList = player.audioTracks();
audioTrackList.addEventListener('change', function () {
var tech = player.tech({ IWillNotUseThisInPlugins: true });
if (
typeof tech.sourceHandler_ != 'undefined' &&
typeof tech.sourceHandler_.hls != 'undefined' &&
tech.sourceHandler_.hls != null
) {
for (var i = 0; i < audioTrackList.length; i++) {
var track = audioTrackList[i];
if (track.enabled) {
selectAudioTrack(i);
return;
}
}
}
});
};
// Map hls.js events to QualityLevels
const initQualityLevels = () => {
var tech = player.tech({ IWillNotUseThisInPlugins: true });
if (typeof tech == 'undefined') {
console.warn('ERROR - tech not found!');
}
// HLS
if (
typeof tech.sourceHandler_ != 'undefined' &&
typeof tech.sourceHandler_.hls != 'undefined' &&
tech.sourceHandler_.hls != null
) {
const hls = tech.sourceHandler_.hls;
const manifestLoadedHandler = (eventName, data) => {
debugLog(`HLS event: ${eventName}`, data);
populateLevels(hls.levels, 'hls');
initAudioTrackInfo();
hls.off(Hls.Events.MANIFEST_LOADED, manifestLoadedHandler);
};
// If manifest is already loaded, populate levels immediately.
if (hls.levels && hls.levels.length > 0) {
manifestLoadedHandler('MANUAL_MANIFEST_LOADED', {});
} else {
hls.on(Hls.Events.MANIFEST_LOADED, manifestLoadedHandler);
}
hls.on(Hls.Events.LEVEL_SWITCHED, (eventName, data) => {
debugLog(`HLS event: ${eventName}`, data);
populateQualityLevelsChange(data.level);
populateQualityChangedEvent(data.level);
});
hls.on(Hls.Events.AUDIO_TRACK_SWITCHED, (eventName, data) => {
debugLog(`HLS event: ${eventName}`, data);
// Make sure Video.js AudioTracks reflect the newly-selected HLS track
if (typeof data.id !== 'undefined') {
updateVjsAudioTracksEnabled(data.id);
}
// Ensure buffer flushed when switch originates from hls.js (e.g. default track)
flushBufferedAudio();
});
hls.on(Hls.Events.ERROR, (eventName, data) => {
debugLog(`HLS event: ${eventName}`, data);
if (data.fatal) {
player.trigger({ type: 'error', eventData: data });
}
});
} else {
// DASH
var dash = player.dash;
if (
typeof dash != 'undefined' &&
dash != null &&
typeof dash.mediaPlayer != 'undefined' &&
dash.mediaPlayer != null
) {
let renditions = getRenditionsDash();
populateLevels(renditions, 'dash');
dash.mediaPlayer.on('qualityChangeRendered', evt => {
const currentVideoQuality = dash.mediaPlayer.getQualityFor('video');
const availableQualities = dash.mediaPlayer.getBitrateInfoListFor('video');
const currentQualityInfo = availableQualities[currentVideoQuality];
const renditionIndex = findDashQualityIndex(currentQualityInfo.width, currentQualityInfo.height);
if (renditionIndex >= 0) {
debugLog(`DASH event: ${evt.type}`, evt, renditions[renditionIndex]);
populateQualityLevelsChange(renditionIndex);
populateQualityChangedEvent(renditionIndex);
} else {
console.warn('Could not find matching rendition for DASH quality change');
}
});
}
}
};
return {
init: initQualityLevels
};
};
export { qualityLevels };