1 import videojs from 'video.js'
2 import { PeerTubeResolution } from '../../types'
4 const Plugin = videojs.getPlugin('plugin')
6 class PeerTubeResolutionsPlugin extends Plugin {
7 private currentSelection: PeerTubeResolution
8 private resolutions: PeerTubeResolution[] = []
10 private autoResolutionChosenId: number
11 private autoResolutionEnabled = true
13 add (resolutions: PeerTubeResolution[]) {
14 for (const r of resolutions) {
15 this.resolutions.push(r)
18 this.currentSelection = this.getSelected()
21 this.trigger('resolutionsAdded')
24 remove (resolutionIndex: number) {
25 this.resolutions = this.resolutions.filter(r => r.id !== resolutionIndex)
26 this.trigger('resolutionRemoved')
30 return this.resolutions
34 return this.resolutions.find(r => r.selected)
37 getAutoResolutionChosen () {
38 return this.resolutions.find(r => r.id === this.autoResolutionChosenId)
44 autoResolutionChosenId?: number
46 const { id, autoResolutionChosenId, byEngine } = options
48 if (this.currentSelection?.id === id && this.autoResolutionChosenId === autoResolutionChosenId) return
50 this.autoResolutionChosenId = autoResolutionChosenId
52 for (const r of this.resolutions) {
53 r.selected = r.id === id
56 this.currentSelection = r
58 if (!byEngine) r.selectCallback()
62 this.trigger('resolutionChanged')
65 disableAutoResolution () {
66 this.autoResolutionEnabled = false
67 this.trigger('autoResolutionEnabledChanged')
70 enabledAutoResolution () {
71 this.autoResolutionEnabled = true
72 this.trigger('autoResolutionEnabledChanged')
75 isAutoResolutionEnabeld () {
76 return this.autoResolutionEnabled
80 this.resolutions.sort((a, b) => {
81 if (a.id === -1) return 1
82 if (b.id === -1) return -1
84 if (a.height > b.height) return -1
85 if (a.height === b.height) return 0
92 videojs.registerPlugin('peertubeResolutions', PeerTubeResolutionsPlugin)
93 export { PeerTubeResolutionsPlugin }