owncast/web/components/video/settings-menu.ts

116 lines
3.4 KiB
TypeScript
Raw Normal View History

2022-11-13 10:28:49 +03:00
/* eslint-disable max-classes-per-file */
export function createVideoSettingsMenuButton(
player,
videojs,
qualities,
latencyItemPressed: () => boolean,
): any {
const VjsMenuItem = videojs.getComponent('MenuItem');
2022-06-19 23:49:42 +03:00
const MenuItem = videojs.getComponent('MenuItem');
const MenuButtonClass = videojs.getComponent('MenuButton');
class MenuSeparator extends VjsMenuItem {
// eslint-disable-next-line no-useless-constructor
constructor(p: any, options: { selectable: boolean }) {
super(p, options);
}
createEl(tag = 'button', props = {}, attributes = {}) {
const el = super.createEl(tag, props, attributes);
el.innerHTML = '<hr style="opacity: 0.3; margin-left: 10px; margin-right: 10px;" />';
return el;
}
}
2022-06-19 23:49:42 +03:00
const lowLatencyItem = new MenuItem(player, {
selectable: true,
label: 'minimize latency (experimental)',
2022-06-19 23:49:42 +03:00
});
lowLatencyItem.on('click', () => {
const enabled: boolean = latencyItemPressed();
lowLatencyItem.selected(enabled);
2022-06-19 23:49:42 +03:00
});
const separator = new MenuSeparator(player, {
selectable: false,
});
2022-06-19 23:49:42 +03:00
2022-11-13 10:28:49 +03:00
class MenuButton extends MenuButtonClass {
2022-06-19 23:49:42 +03:00
constructor() {
2022-11-13 10:28:49 +03:00
super(player);
}
2022-06-19 23:49:42 +03:00
2022-11-13 10:28:49 +03:00
// eslint-disable-next-line class-methods-use-this
2022-06-19 23:49:42 +03:00
createItems() {
const tech = player.tech({ IWillNotUseThisInPlugins: true });
const defaultAutoItem = new MenuItem(player, {
selectable: true,
selected: true,
2022-06-19 23:49:42 +03:00
label: 'Auto',
});
const items = Array(qualities.length);
qualities.forEach(item => {
items[item.index] = new MenuItem(player, {
2022-06-19 23:49:42 +03:00
selectable: true,
label: item.name,
});
});
2022-06-19 23:49:42 +03:00
for (let i = 0; i < items.length; i += 1) {
const item = items[i];
2022-06-19 23:49:42 +03:00
// Quality selected
item.on('click', () => {
2022-06-19 23:49:42 +03:00
// If for some reason tech doesn't exist, then don't do anything
if (!tech) {
console.warn('Invalid attempt to access null player tech');
return;
}
// Only enable and highlight this single, selected representation.
2022-06-19 23:49:42 +03:00
tech.vhs.representations().forEach((rep, index) => {
const isCurrent: boolean = index === i;
rep.enabled(isCurrent);
items[index].selected(isCurrent);
2022-06-19 23:49:42 +03:00
});
defaultAutoItem.selected(false);
2022-06-19 23:49:42 +03:00
});
}
2022-06-19 23:49:42 +03:00
defaultAutoItem.on('click', () => {
// Re-enable all representations.
tech.vhs.representations().forEach(rep => {
rep.enabled(true);
});
// Only highlight "Auto"
items.forEach(item => item.selected(false));
defaultAutoItem.selected(true);
2022-06-19 23:49:42 +03:00
});
const supportsLatencyCompensator = !!tech && !!tech.vhs;
2022-06-19 23:49:42 +03:00
// Only show the quality selector if there is more than one option.
if (qualities.length < 2 && supportsLatencyCompensator) {
return [lowLatencyItem];
}
2022-06-19 23:49:42 +03:00
if (qualities.length > 1 && supportsLatencyCompensator) {
return [defaultAutoItem, ...items, separator, lowLatencyItem];
}
2022-06-19 23:49:42 +03:00
if (!supportsLatencyCompensator && qualities.length === 1) {
return [];
}
return [defaultAutoItem, ...items];
2022-11-13 10:28:49 +03:00
}
}
2022-06-19 23:49:42 +03:00
const menuButton = new MenuButton();
menuButton.el().setAttribute('aria-label', 'Settings');
menuButton.addClass('vjs-quality-selector');
videojs.registerComponent('MenuButton', MenuButton);
2022-06-19 23:49:42 +03:00
return menuButton;
}