From e367da949bb97c3db8c2f9a28ea09eef93abb2f5 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Mon, 6 Sep 2021 16:08:59 +0200 Subject: Cleanup player quality change --- .../assets/player/peertube-resolutions-plugin.ts | 88 ++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 client/src/assets/player/peertube-resolutions-plugin.ts (limited to 'client/src/assets/player/peertube-resolutions-plugin.ts') diff --git a/client/src/assets/player/peertube-resolutions-plugin.ts b/client/src/assets/player/peertube-resolutions-plugin.ts new file mode 100644 index 000000000..cc36f18f3 --- /dev/null +++ b/client/src/assets/player/peertube-resolutions-plugin.ts @@ -0,0 +1,88 @@ +import videojs from 'video.js' +import { PeerTubeResolution } from './peertube-videojs-typings' + +const Plugin = videojs.getPlugin('plugin') + +class PeerTubeResolutionsPlugin extends Plugin { + private currentSelection: PeerTubeResolution + private resolutions: PeerTubeResolution[] = [] + + private autoResolutionChosenId: number + private autoResolutionEnabled = true + + add (resolutions: PeerTubeResolution[]) { + for (const r of resolutions) { + this.resolutions.push(r) + } + + this.currentSelection = this.getSelected() + + this.sort() + this.trigger('resolutionsAdded') + } + + getResolutions () { + return this.resolutions + } + + getSelected () { + return this.resolutions.find(r => r.selected) + } + + getAutoResolutionChosen () { + return this.resolutions.find(r => r.id === this.autoResolutionChosenId) + } + + select (options: { + id: number + byEngine: boolean + autoResolutionChosenId?: number + }) { + const { id, autoResolutionChosenId, byEngine } = options + + if (this.currentSelection?.id === id && this.autoResolutionChosenId === autoResolutionChosenId) return + + this.autoResolutionChosenId = autoResolutionChosenId + + for (const r of this.resolutions) { + r.selected = r.id === id + + if (r.selected) { + this.currentSelection = r + + if (!byEngine) r.selectCallback() + } + } + + this.trigger('resolutionChanged') + } + + disableAutoResolution () { + this.autoResolutionEnabled = false + this.trigger('autoResolutionEnabledChanged') + } + + enabledAutoResolution () { + this.autoResolutionEnabled = true + this.trigger('autoResolutionEnabledChanged') + } + + isAutoResolutionEnabeld () { + return this.autoResolutionEnabled + } + + private sort () { + this.resolutions.sort((a, b) => { + if (a.id === -1) return 1 + if (b.id === -1) return -1 + + if (a.height > b.height) return -1 + if (a.height === b.height) return 0 + return 1 + }) + } + +} + +videojs.registerPlugin('peertubeResolutions', PeerTubeResolutionsPlugin) +export { PeerTubeResolutionsPlugin } -- cgit v1.2.3