-
Notifications
You must be signed in to change notification settings - Fork 139
Expand file tree
/
Copy pathquality.js
More file actions
484 lines (437 loc) · 14.9 KB
/
quality.js
File metadata and controls
484 lines (437 loc) · 14.9 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
'use strict';
/**
* Qualities feature
*
* This feature allows the generation of a menu with different video/audio qualities, depending of the elements set
* in the <source> tags, such as `title` and `data-quality`
*/
// Translations (English required)
mejs.i18n.en['mejs.quality-chooser'] = 'Quality Chooser';
// Feature configuration
Object.assign(mejs.MepDefaults, {
/**
* @type {String}
*/
defaultQuality: 'auto',
/**
* @type {String}
*/
qualityText: null,
/**
* @type {boolean}
*/
autoGenerate: false,
/**
* @type {String}
*/
hslQualityChangeStrategy: 'nextLevel',
/**
* @type {boolean}
*/
autoDash: false,
/**
* @type {boolean}
*/
autoHLS: false,
/**
* @type Function
*/
qualityChangeCallback: null,
/**
* The path where the icon file is located
* @type {String}
*/
iconPathQuality: 'mejs-quality.svg',
});
Object.assign(MediaElementPlayer.prototype, {
/**
* Feature constructor.
*
* Always has to be prefixed with `build` and the name that will be used in MepDefaults.features list
* @param {MediaElementPlayer} player
* @param {HTMLElement} controls
* @param {HTMLElement} layers
* @param {HTMLElement} media
*/
buildquality (player, controls, layers, media) {
const
t = this,
children = t.mediaFiles ? t.mediaFiles : t.node.children,
qualityMap = new Map()
;
for (let i = 0, total = children.length; i < total; i++) {
const mediaNode = children[i];
let quality = mediaNode instanceof HTMLElement ? mediaNode.getAttribute('data-quality') : mediaNode['data-quality'];
if (quality === 'undefined') {
quality = 'Auto';
t.options.autoGenerate = true;
}
if (t.mediaFiles) {
const source = document.createElement('source');
source.src = mediaNode['src'];
source.type = mediaNode['type'];
t.addValueToKey(qualityMap, quality, source);
} else if (mediaNode.nodeName === 'SOURCE') {
t.addValueToKey(qualityMap, quality, mediaNode);
}
}
if (qualityMap.size <= 1) {
return;
}
let
currentQuality = '',
sourceIndex = 0
;
media.addEventListener('error', function(e) {
if (e.message === 'No renderer found' &&
qualityMap.get(currentQuality).length > sourceIndex + 1 ) {
sourceIndex = sourceIndex + 1;
const nextSource = qualityMap.get(currentQuality)[sourceIndex].src;
media.setSrc(nextSource); // ensure the default sources to set to play
media.load();
}
});
media.addEventListener('loadedmetadata', function () {
// eslint-disable-next-line
if (!!media.hlsPlayer) {
const levels = media.hlsPlayer.levels;
if (t.options.autoGenerate && levels.length > 1) {
levels.forEach(function (level) {
const height = level.height;
const quality = t.getQualityFromHeight(height);
t.addValueToKey(qualityMap, quality, '');
});
t.options.autoHLS = true;
t.generateQualityButton(t, player, media, qualityMap, currentQuality);
}
// eslint-disable-next-line
} else if (!!media.dashPlayer) {
const bitrates = media.dashPlayer.getBitrateInfoListFor("video");
if (t.options.autoGenerate && bitrates.length > 1) {
bitrates.forEach(function (level) {
const height = level.height;
const quality = t.getQualityFromHeight(height);
t.addValueToKey(qualityMap, quality, '');
});
t.options.autoDash = true;
t.generateQualityButton(t, player, media, qualityMap, currentQuality);
}
}
});
t.generateQualityButton(t, player, media, qualityMap, currentQuality);
},
generateQualityButton (t, player, media, qualityMap, currentQuality) {
t.cleanquality(player);
const
qualityTitle = mejs.Utils.isString(t.options.qualityText) ? t.options.qualityText : mejs.i18n.t('mejs.quality-chooser'),
getQualityNameFromValue = (value) => {
let label;
if (value === 'auto') {
const keyExist = t.keyExist(qualityMap, value);
if (keyExist) {
label = value;
} else {
let keyValue = t.getMapIndex(qualityMap, 0);
label = keyValue.key;
}
} else {
label = value;
}
return label;
},
defaultValue = getQualityNameFromValue(t.options.defaultQuality)
;
currentQuality = defaultValue;
// Get initial quality
const generateId = Math.floor(Math.random() * 100);
const iconHtml = `<svg xmlns="http://www.w3.org/2000/svg" id="${generateId}" class="${t.options.classPrefix}" aria-hidden="true" focusable="false">
<use xlink:href="${t.options.iconPathQuality}#default-icon"></use></svg>`;
player.qualitiesContainer = document.createElement('div');
player.qualitiesContainer.className = `${t.options.classPrefix}button ${t.options.classPrefix}qualities-button`;
player.qualitiesContainer.innerHTML = `<button type="button" title="${qualityTitle}" aria-label="${qualityTitle}" aria-controls="qualitieslist-${generateId}" aria-expanded="false">
${t.options.iconPathQuality ? iconHtml : defaultValue}</button>` +
`<div class="${t.options.classPrefix}qualities-selector ${t.options.classPrefix}offscreen">` +
`<ul class="${t.options.classPrefix}qualities-selector-list" id="qualitieslist-${generateId}" tabindex="-1"></ul></div>`;
t.addControlElement(player.qualitiesContainer, 'qualities');
qualityMap.forEach(function (value, key) {
if (key !== 'map_keys_1') {
const
src = value[0],
quality = key,
inputId = `${t.id}-qualities-${quality}`
;
player.qualitiesContainer.querySelector('ul').innerHTML += `<li class="${t.options.classPrefix}qualities-selector-list-item">` +
`<input class="${t.options.classPrefix}qualities-selector-input ${(quality === defaultValue ? `${t.options.classPrefix}qualities-selected-input` : '')}" type="radio" name="${t.id}_qualities" disabled="disabled" ` +
`value="${quality}" id="${inputId}" ${(quality === defaultValue ? ' checked="checked"' : '')} />` +
`<label for="${inputId}" class="${t.options.classPrefix}qualities-selector-label ${(quality === defaultValue ? ` ${t.options.classPrefix}qualities-selected` : '')}">` +
`${src.title || quality} </label></li>`;
}
});
let isHidden = true;
const
qualityContainer = player.qualitiesContainer,
qualityButton = player.qualitiesContainer.querySelector(`button`),
qualitiesSelector = player.qualitiesContainer.querySelector(`.${t.options.classPrefix}qualities-selector`),
qualitiesList = player.qualitiesContainer.querySelector(`.${t.options.classPrefix}qualities-selector-list`),
// Enable inputs after they have been appended to controls to avoid tab and up/down arrow focus issues
radios = player.qualitiesContainer.querySelectorAll('input[type="radio"]'),
labels = player.qualitiesContainer.querySelectorAll(`.${t.options.classPrefix}qualities-selector-label`)
;
function hideSelector() {
mejs.Utils.addClass(qualitiesSelector, `${t.options.classPrefix}offscreen`);
qualityButton.setAttribute('aria-expanded', 'false');
qualityButton.focus();
isHidden = true;
}
function showSelector() {
mejs.Utils.removeClass(qualitiesSelector, `${t.options.classPrefix}offscreen`);
qualitiesSelector.style.height = `${qualitiesSelector.querySelector('ul').offsetHeight}px`;
qualitiesSelector.style.top = `${(-1 * parseFloat(qualitiesSelector.offsetHeight))}px`;
qualityButton.setAttribute('aria-expanded', 'true');
qualitiesSelector.querySelector('.' + t.options.classPrefix + 'qualities-selected-input').focus();
isHidden = false;
}
qualityButton.addEventListener('click', () => {
if (isHidden === true) {
showSelector();
} else {
hideSelector();
}
});
qualitiesList.addEventListener('focusout', (event) =>{
if (!qualityContainer.contains(event.relatedTarget)) {
hideSelector();
}
});
qualityButton.addEventListener('mouseenter', () =>{
showSelector();
});
qualityContainer.addEventListener('mouseleave', () =>{
hideSelector();
});
// Close with Escape key.
// Allow up/down arrow to change the selected radio without changing the volume.
qualityContainer.addEventListener('keydown', (event) => {
if(event.key === "Escape"){
hideSelector();
}
event.stopPropagation();
});
for (let i = 0, total = radios.length; i < total; i++) {
const radio = radios[i];
radio.disabled = false;
radio.addEventListener('change', function () {
if (t.options.autoDash) {
t.updateQualityButton(this, player, currentQuality);
t.switchDashQuality(player, media);
} else if (t.options.autoHLS) {
t.updateQualityButton(this, player, currentQuality);
t.switchHLSQuality(player, media);
} else {
currentQuality = t.updateQualityButton(this, player, currentQuality);
let currentTime = media.currentTime;
const paused = media.paused;
if (!paused) {
media.pause();
}
t.updateVideoSource(media, qualityMap, currentQuality);
media.setSrc(qualityMap.get(currentQuality)[0].src);
media.load();
media.dispatchEvent(mejs.Utils.createEvent('seeking', media));
if (!paused) {
media.play();
}
media.addEventListener('canplay', function canPlayAfterSourceSwitchHandler () {
media.setCurrentTime(currentTime);
media.removeEventListener('canplay', canPlayAfterSourceSwitchHandler);
});
}
if (t.options.qualityChangeCallback) {
t.options.qualityChangeCallback(media, media.originalNode, newQuality)
}
});
}
for (let i = 0, total = labels.length; i < total; i++) {
labels[i].addEventListener('click', function () {
const
radio = mejs.Utils.siblings(this, (el) => el.tagName === 'INPUT')[0],
event = mejs.Utils.createEvent('click', radio)
;
radio.dispatchEvent(event);
});
}
},
/**
* Feature destructor.
*
* Always has to be prefixed with `clean` and the name that was used in MepDefaults.features list
* @param {MediaElementPlayer} player
*/
cleanquality (player) {
if (player) {
if (player.qualitiesContainer) {
player.qualitiesContainer.remove();
}
}
},
/**
* Populate the source map
* @param {Map} map the map the quality source is being added
* @param {String} key the key to the quality source (value stored in data-quality)
* @param {String} value the the video.source tag
*/
addValueToKey (map, key, value) {
if (map.has('map_keys_1')) {
map.get('map_keys_1').push(key);
} else {
map.set('map_keys_1', []);
}
if (map.has(key)) {
map.get(key).push(value);
} else {
map.set(key, []);
map.get(key).push(value);
}
},
/**
* Set the source tag for the mejs player
* @param {MediaElement} media
* @param {Map} map the map containing the video quality source tags
* @param {String} key the user selected quality
*/
updateVideoSource (media, map, key) {
this.cleanMediaSource(media);
let sources = map.get(key);
for (let i = 0; i < media.children.length; i++) {
let mediaNode = media.children[i];
if (mediaNode.tagName === 'VIDEO') {
sources.forEach(function (sourceElement) {
mediaNode.appendChild(sourceElement);
});
}
}
},
/**
* Remove all the source tag for the mejs player
* @param {MediaElement} media
*/
cleanMediaSource (media) {
for (let i = 0; i < media.children.length; i++) {
let mediaNode = media.children[i];
if (mediaNode.tagName === 'VIDEO') {
const sourceNodes = mediaNode.querySelectorAll('source');
Array.from(sourceNodes).forEach((sourceNode) => {
mediaNode.removeChild(sourceNode);
});
}
}
},
/**
* Search a map for a value key pair stored at a specified index
* @param {Map} map the map being searched
* @param {Number} index the index of the requested key value pair
* @return {Object} a key:value object
*/
getMapIndex (map, index) {
let counter = -1;
let keyValue = {};
map.forEach(function (value, key) {
if (counter === index) {
keyValue.key = key;
keyValue.value = value;
}
counter++;
});
return keyValue;
},
/**
* Returns flag for whether or not a given key exist in a give map
* @param {Map} map the map being searched
* @param {String} searchKey the map searching being searched
* @return {boolean}
*/
keyExist (map, searchKey) {
return -1 < map.get('map_keys_1').indexOf(searchKey);
},
/**
* Responsible for switching the video source when quality source was auto created from dash manifest
* @param {MediaElementPlayer} player
* @param {MediaElement} media
*/
switchDashQuality (player, media) {
const radios = player.qualitiesContainer.querySelectorAll('input[type="radio"]');
for (let index = 0; index < radios.length; index++) {
if (radios[index].checked) {
if (index === 0 ) {
media.dashPlayer.setAutoSwitchQuality(true);
} else {
media.dashPlayer.setAutoSwitchQuality(false);
media.dashPlayer.setQualityFor("video", index - 1);
}
}
}
},
/**
* Responsible for switching the video source when quality source was auto created from hls manifest
* @param {MediaElementPlayer} player
* @param {MediaElement} media
*/
switchHLSQuality (player, media) {
const t = this;
const radios = player.qualitiesContainer.querySelectorAll('input[type="radio"]');
for (let index = 0; index < radios.length; index++) {
if (radios[index].checked) {
if (index === 0 ) {
media.hlsPlayer[t.options.hslQualityChangeStrategy] = -1;
} else {
media.hlsPlayer[t.options.hslQualityChangeStrategy] = index - 1;
}
}
}
},
/**
* Responsible for switching the video source when quality source was auto created from dash manifest
* @param {Element} self the check quality radio button
* @param {MediaElementPlayer} player
*/
updateQualityButton (self, player) {
const t = this;
const
newQuality = self.value
;
const formerSelected = player.qualitiesContainer.querySelectorAll(`.${t.options.classPrefix}qualities-selected`);
for (let i = 0, total = formerSelected.length; i < total; i++) {
mejs.Utils.removeClass(formerSelected[i], `${t.options.classPrefix}qualities-selected`);
formerSelected[i].parentElement.querySelector('input').classList.remove(`${t.options.classPrefix}qualities-selected-input`);
}
self.checked = true;
const currentSelected = mejs.Utils.siblings(self, (el) => mejs.Utils.hasClass(el, `${t.options.classPrefix}qualities-selector-label`));
for (let j = 0, total = currentSelected.length; j < total; j++) {
mejs.Utils.addClass(currentSelected[j], `${t.options.classPrefix}qualities-selected`);
currentSelected[j].parentElement.querySelector('input').classList.add(`${t.options.classPrefix}qualities-selected-input`);
}
if (!t.options.iconPathQuality) {
player.qualitiesContainer.querySelector('button').innerHTML = newQuality;
}
return newQuality;
},
/**
* Returns the quality represnetaion base on the height of the loaded video
* @param {Number} height the pixel height of the video
**/
getQualityFromHeight (height) {
if (height >= 4320) {
return "8K UHD";
} else if (height >= 2160) {
return "UHD";
} else if (height >= 1440) {
return "QHD";
} else if (height >= 1080) {
return "FHD";
} else if (height >= 720) {
return "HD";
} else {
return "SD";
}
}
});