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