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