]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/assets/player/peertube-player.ts
add chevron hotkeys to change playback rate
[github/Chocobozzz/PeerTube.git] / client / src / assets / player / peertube-player.ts
CommitLineData
c6352f2c
C
1import { VideoFile } from '../../../../shared/models/videos'
2
3import 'videojs-hotkeys'
c7b0dacb 4import 'videojs-dock'
960a11e8 5import 'videojs-contextmenu-ui'
c6352f2c
C
6import './peertube-link-button'
7import './resolution-menu-button'
8import './settings-menu-button'
9import './webtorrent-info-button'
10import './peertube-videojs-plugin'
77728efa 11import './peertube-load-progress-bar'
054a103b 12import './theater-button'
16f7022b 13import { VideoJSCaption, videojsUntyped } from './peertube-videojs-typings'
960a11e8 14import { buildVideoEmbed, buildVideoLink, copyToClipboard } from './utils'
9f164722 15import { getCompleteLocale, getShortLocale, is18nLocale, isDefaultLocale } from '../../../../shared/models/i18n/i18n'
c6352f2c
C
16
17// Change 'Playback Rate' to 'Speed' (smaller for our settings menu)
18videojsUntyped.getComponent('PlaybackRateMenuButton').prototype.controlText_ = 'Speed'
16f7022b
C
19// Change Captions to Subtitles/CC
20videojsUntyped.getComponent('CaptionsButton').prototype.controlText_ = 'Subtitles/CC'
21// We just want to display 'Off' instead of 'captions off', keep a space so the variable == true (hacky I know)
22videojsUntyped.getComponent('CaptionsButton').prototype.label_ = ' '
c6352f2c
C
23
24function getVideojsOptions (options: {
25 autoplay: boolean,
26 playerElement: HTMLVideoElement,
27 videoViewUrl: string,
28 videoDuration: number,
29 videoFiles: VideoFile[],
30 enableHotkeys: boolean,
31 inactivityTimeout: number,
33d78552 32 peertubeLink: boolean,
f37bad63 33 poster: string,
1f6824c9 34 startTime: number | string
99941732 35 theaterMode: boolean,
16f7022b 36 videoCaptions: VideoJSCaption[],
95d51135 37 language?: string,
99941732
WL
38 controls?: boolean,
39 muted?: boolean,
40 loop?: boolean
c6352f2c
C
41}) {
42 const videojsOptions = {
16f7022b
C
43 // We don't use text track settings for now
44 textTrackSettings: false,
99941732
WL
45 controls: options.controls !== undefined ? options.controls : true,
46 muted: options.controls !== undefined ? options.muted : false,
47 loop: options.loop !== undefined ? options.loop : false,
33d78552 48 poster: options.poster,
80109b2d 49 autoplay: false,
c6352f2c 50 inactivityTimeout: options.inactivityTimeout,
4ccb6c08 51 playbackRates: [ 0.5, 0.75, 1, 1.25, 1.5, 2 ],
c6352f2c
C
52 plugins: {
53 peertube: {
80109b2d 54 autoplay: options.autoplay, // Use peertube plugin autoplay because we get the file by webtorrent
16f7022b 55 videoCaptions: options.videoCaptions,
c6352f2c
C
56 videoFiles: options.videoFiles,
57 playerElement: options.playerElement,
58 videoViewUrl: options.videoViewUrl,
f37bad63
C
59 videoDuration: options.videoDuration,
60 startTime: options.startTime
c6352f2c
C
61 }
62 },
63 controlBar: {
64 children: getControlBarChildren(options)
65 }
66 }
67
68 if (options.enableHotkeys === true) {
69 Object.assign(videojsOptions.plugins, {
70 hotkeys: {
c4ccb08a 71 enableVolumeScroll: false,
52b1ba03
RK
72 enableModifiersForNumbers: false,
73 customKeys: {
74 increasePlaybackRateKey: {
75 key: function (event) {
76 // use '>'
77 return event.which === 51
78 },
79 handler: function (player, options, event) {
80 player.playbackRate(player.playbackRate() + 0.1)
81 }
82 },
83 decreasePlaybackRateKey: {
84 key: function (event) {
85 // use '<'
86 return event.which === 50
87 },
88 handler: function (player, options, event) {
89 player.playbackRate(player.playbackRate() - 0.1)
90 }
91 }
92 }
c6352f2c
C
93 }
94 })
95 }
96
95d51135
C
97 if (options.language && !isDefaultLocale(options.language)) {
98 Object.assign(videojsOptions, { language: options.language })
99 }
100
c6352f2c
C
101 return videojsOptions
102}
103
104function getControlBarChildren (options: {
105 peertubeLink: boolean
16f7022b
C
106 theaterMode: boolean,
107 videoCaptions: VideoJSCaption[]
c6352f2c 108}) {
16f7022b
C
109 const settingEntries = []
110
111 // Keep an order
112 settingEntries.push('playbackRateMenuButton')
113 if (options.videoCaptions.length !== 0) settingEntries.push('captionsButton')
114 settingEntries.push('resolutionMenuButton')
115
c6352f2c
C
116 const children = {
117 'playToggle': {},
118 'currentTimeDisplay': {},
119 'timeDivider': {},
120 'durationDisplay': {},
121 'liveDisplay': {},
122
123 'flexibleWidthSpacer': {},
77728efa
C
124 'progressControl': {
125 children: {
126 'seekBar': {
127 children: {
128 'peerTubeLoadProgressBar': {},
db5529f5 129 'mouseTimeDisplay': {},
77728efa
C
130 'playProgressBar': {}
131 }
132 }
133 }
134 },
c6352f2c
C
135
136 'webTorrentButton': {},
137
138 'muteToggle': {},
139 'volumeControl': {},
140
141 'settingsButton': {
142 setup: {
143 maxHeightOffset: 40
144 },
16f7022b 145 entries: settingEntries
c6352f2c
C
146 }
147 }
148
149 if (options.peertubeLink === true) {
150 Object.assign(children, {
151 'peerTubeLinkButton': {}
152 })
153 }
154
054a103b
C
155 if (options.theaterMode === true) {
156 Object.assign(children, {
157 'theaterButton': {}
158 })
159 }
160
c6352f2c
C
161 Object.assign(children, {
162 'fullscreenToggle': {}
163 })
164
165 return children
166}
167
e945b184 168function addContextMenu (player: any, videoEmbedUrl: string) {
e945b184
C
169 player.contextmenuUI({
170 content: [
171 {
172 label: player.localize('Copy the video URL'),
173 listener: function () {
174 copyToClipboard(buildVideoLink())
175 }
176 },
177 {
178 label: player.localize('Copy the video URL at the current time'),
179 listener: function () {
180 const player = this
181 copyToClipboard(buildVideoLink(player.currentTime()))
182 }
183 },
184 {
185 label: player.localize('Copy embed code'),
186 listener: () => {
187 copyToClipboard(buildVideoEmbed(videoEmbedUrl))
188 }
5511da62
RK
189 },
190 {
191 label: player.localize('Copy magnet URI'),
192 listener: function () {
193 const player = this
194 copyToClipboard(player.peertube().getCurrentVideoFile().magnetUri)
195 }
e945b184
C
196 }
197 ]
198 })
199}
200
3dfa8494
C
201function loadLocaleInVideoJS (serverUrl: string, videojs: any, locale: string) {
202 const path = getLocalePath(serverUrl, locale)
203 // It is the default locale, nothing to translate
204 if (!path) return Promise.resolve(undefined)
e945b184 205
5d128505
C
206 let p: Promise<any>
207
208 if (loadLocaleInVideoJS.cache[path]) {
209 p = Promise.resolve(loadLocaleInVideoJS.cache[path])
210 } else {
211 p = fetch(path + '/player.json')
212 .then(res => res.json())
213 .then(json => {
214 loadLocaleInVideoJS.cache[path] = json
215 return json
216 })
217 }
74b7c6d4 218
5d128505
C
219 const completeLocale = getCompleteLocale(locale)
220 return p.then(json => videojs.addLanguage(getShortLocale(completeLocale), json))
221}
222namespace loadLocaleInVideoJS {
223 export const cache: { [ path: string ]: any } = {}
e945b184
C
224}
225
3dfa8494
C
226function getServerTranslations (serverUrl: string, locale: string) {
227 const path = getLocalePath(serverUrl, locale)
228 // It is the default locale, nothing to translate
229 if (!path) return Promise.resolve(undefined)
230
231 return fetch(path + '/server.json')
232 .then(res => res.json())
233}
234
235// ############################################################################
236
e945b184 237export {
3dfa8494
C
238 getServerTranslations,
239 loadLocaleInVideoJS,
e945b184
C
240 getVideojsOptions,
241 addContextMenu
242}
3dfa8494
C
243
244// ############################################################################
245
246function getLocalePath (serverUrl: string, locale: string) {
247 const completeLocale = getCompleteLocale(locale)
248
249 if (!is18nLocale(completeLocale) || isDefaultLocale(completeLocale)) return undefined
250
251 return serverUrl + '/client/locales/' + completeLocale
252}