]>
Commit | Line | Data |
---|---|---|
e367da94 | 1 | import videojs from 'video.js' |
57d65032 | 2 | import { PeerTubeResolution } from '../../types' |
e367da94 C |
3 | |
4 | const Plugin = videojs.getPlugin('plugin') | |
5 | ||
6 | class PeerTubeResolutionsPlugin extends Plugin { | |
7 | private currentSelection: PeerTubeResolution | |
8 | private resolutions: PeerTubeResolution[] = [] | |
9 | ||
10 | private autoResolutionChosenId: number | |
11 | private autoResolutionEnabled = true | |
12 | ||
13 | add (resolutions: PeerTubeResolution[]) { | |
14 | for (const r of resolutions) { | |
15 | this.resolutions.push(r) | |
16 | } | |
17 | ||
18 | this.currentSelection = this.getSelected() | |
19 | ||
20 | this.sort() | |
21 | this.trigger('resolutionsAdded') | |
22 | } | |
23 | ||
f2a16d93 | 24 | remove (resolutionIndex: number) { |
25 | this.resolutions = this.resolutions.filter(r => r.id !== resolutionIndex) | |
26 | this.trigger('resolutionRemoved') | |
27 | } | |
28 | ||
e367da94 C |
29 | getResolutions () { |
30 | return this.resolutions | |
31 | } | |
32 | ||
33 | getSelected () { | |
34 | return this.resolutions.find(r => r.selected) | |
35 | } | |
36 | ||
37 | getAutoResolutionChosen () { | |
38 | return this.resolutions.find(r => r.id === this.autoResolutionChosenId) | |
39 | } | |
40 | ||
41 | select (options: { | |
42 | id: number | |
43 | byEngine: boolean | |
44 | autoResolutionChosenId?: number | |
45 | }) { | |
46 | const { id, autoResolutionChosenId, byEngine } = options | |
47 | ||
48 | if (this.currentSelection?.id === id && this.autoResolutionChosenId === autoResolutionChosenId) return | |
49 | ||
50 | this.autoResolutionChosenId = autoResolutionChosenId | |
51 | ||
52 | for (const r of this.resolutions) { | |
53 | r.selected = r.id === id | |
54 | ||
55 | if (r.selected) { | |
56 | this.currentSelection = r | |
57 | ||
58 | if (!byEngine) r.selectCallback() | |
59 | } | |
60 | } | |
61 | ||
62 | this.trigger('resolutionChanged') | |
63 | } | |
64 | ||
65 | disableAutoResolution () { | |
66 | this.autoResolutionEnabled = false | |
67 | this.trigger('autoResolutionEnabledChanged') | |
68 | } | |
69 | ||
70 | enabledAutoResolution () { | |
71 | this.autoResolutionEnabled = true | |
72 | this.trigger('autoResolutionEnabledChanged') | |
73 | } | |
74 | ||
75 | isAutoResolutionEnabeld () { | |
76 | return this.autoResolutionEnabled | |
77 | } | |
78 | ||
79 | private sort () { | |
80 | this.resolutions.sort((a, b) => { | |
81 | if (a.id === -1) return 1 | |
82 | if (b.id === -1) return -1 | |
83 | ||
84 | if (a.height > b.height) return -1 | |
85 | if (a.height === b.height) return 0 | |
86 | return 1 | |
87 | }) | |
88 | } | |
89 | ||
90 | } | |
91 | ||
92 | videojs.registerPlugin('peertubeResolutions', PeerTubeResolutionsPlugin) | |
93 | export { PeerTubeResolutionsPlugin } |