diff options
Diffstat (limited to 'client/src/assets/player/peertube-resolutions-plugin.ts')
-rw-r--r-- | client/src/assets/player/peertube-resolutions-plugin.ts | 88 |
1 files changed, 88 insertions, 0 deletions
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 @@ | |||
1 | import videojs from 'video.js' | ||
2 | import { PeerTubeResolution } from './peertube-videojs-typings' | ||
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 | |||
24 | getResolutions () { | ||
25 | return this.resolutions | ||
26 | } | ||
27 | |||
28 | getSelected () { | ||
29 | return this.resolutions.find(r => r.selected) | ||
30 | } | ||
31 | |||
32 | getAutoResolutionChosen () { | ||
33 | return this.resolutions.find(r => r.id === this.autoResolutionChosenId) | ||
34 | } | ||
35 | |||
36 | select (options: { | ||
37 | id: number | ||
38 | byEngine: boolean | ||
39 | autoResolutionChosenId?: number | ||
40 | }) { | ||
41 | const { id, autoResolutionChosenId, byEngine } = options | ||
42 | |||
43 | if (this.currentSelection?.id === id && this.autoResolutionChosenId === autoResolutionChosenId) return | ||
44 | |||
45 | this.autoResolutionChosenId = autoResolutionChosenId | ||
46 | |||
47 | for (const r of this.resolutions) { | ||
48 | r.selected = r.id === id | ||
49 | |||
50 | if (r.selected) { | ||
51 | this.currentSelection = r | ||
52 | |||
53 | if (!byEngine) r.selectCallback() | ||
54 | } | ||
55 | } | ||
56 | |||
57 | this.trigger('resolutionChanged') | ||
58 | } | ||
59 | |||
60 | disableAutoResolution () { | ||
61 | this.autoResolutionEnabled = false | ||
62 | this.trigger('autoResolutionEnabledChanged') | ||
63 | } | ||
64 | |||
65 | enabledAutoResolution () { | ||
66 | this.autoResolutionEnabled = true | ||
67 | this.trigger('autoResolutionEnabledChanged') | ||
68 | } | ||
69 | |||
70 | isAutoResolutionEnabeld () { | ||
71 | return this.autoResolutionEnabled | ||
72 | } | ||
73 | |||
74 | private sort () { | ||
75 | this.resolutions.sort((a, b) => { | ||
76 | if (a.id === -1) return 1 | ||
77 | if (b.id === -1) return -1 | ||
78 | |||
79 | if (a.height > b.height) return -1 | ||
80 | if (a.height === b.height) return 0 | ||
81 | return 1 | ||
82 | }) | ||
83 | } | ||
84 | |||
85 | } | ||
86 | |||
87 | videojs.registerPlugin('peertubeResolutions', PeerTubeResolutionsPlugin) | ||
88 | export { PeerTubeResolutionsPlugin } | ||