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')
25 return this.resolutions
29 return this.resolutions.find(r => r.selected)
32 getAutoResolutionChosen () {
33 return this.resolutions.find(r => r.id === this.autoResolutionChosenId)
39 autoResolutionChosenId?: number
41 const { id, autoResolutionChosenId, byEngine } = options
43 if (this.currentSelection?.id === id && this.autoResolutionChosenId === autoResolutionChosenId) return
45 this.autoResolutionChosenId = autoResolutionChosenId
47 for (const r of this.resolutions) {
48 r.selected = r.id === id
51 this.currentSelection = r
53 if (!byEngine) r.selectCallback()
57 this.trigger('resolutionChanged')
60 disableAutoResolution () {
61 this.autoResolutionEnabled = false
62 this.trigger('autoResolutionEnabledChanged')
65 enabledAutoResolution () {
66 this.autoResolutionEnabled = true
67 this.trigger('autoResolutionEnabledChanged')
70 isAutoResolutionEnabeld () {
71 return this.autoResolutionEnabled
75 this.resolutions.sort((a, b) => {
76 if (a.id === -1) return 1
77 if (b.id === -1) return -1
79 if (a.height > b.height) return -1
80 if (a.height === b.height) return 0
87 videojs.registerPlugin('peertubeResolutions', PeerTubeResolutionsPlugin)
88 export { PeerTubeResolutionsPlugin }