]>
Commit | Line | Data |
---|---|---|
512decf3 | 1 | import 'videojs-hotkeys/videojs.hotkeys' |
2adfc7ea | 2 | import 'videojs-dock' |
950b3ce7 | 3 | import '@peertube/videojs-contextmenu' |
f5fcd9f7 | 4 | import './upnext/end-card' |
3bcb4fd7 | 5 | import './upnext/upnext-plugin' |
ff563914 RK |
6 | import './stats/stats-card' |
7 | import './stats/stats-plugin' | |
62ab565d | 8 | import './bezels/bezels-plugin' |
2adfc7ea | 9 | import './peertube-plugin' |
e367da94 | 10 | import './peertube-resolutions-plugin' |
a950e4c8 | 11 | import './videojs-components/next-previous-video-button' |
f5fcd9f7 | 12 | import './videojs-components/p2p-info-button' |
2adfc7ea | 13 | import './videojs-components/peertube-link-button' |
f5fcd9f7 | 14 | import './videojs-components/peertube-load-progress-bar' |
2adfc7ea | 15 | import './videojs-components/resolution-menu-button' |
f5fcd9f7 C |
16 | import './videojs-components/resolution-menu-item' |
17 | import './videojs-components/settings-dialog' | |
2adfc7ea | 18 | import './videojs-components/settings-menu-button' |
f5fcd9f7 C |
19 | import './videojs-components/settings-menu-item' |
20 | import './videojs-components/settings-panel' | |
21 | import './videojs-components/settings-panel-child' | |
2adfc7ea | 22 | import './videojs-components/theater-button' |
4572c3d0 | 23 | import './playlist/playlist-plugin' |
f1a0555a | 24 | import './mobile/peertube-mobile-plugin' |
2dd0a8a8 | 25 | import './mobile/peertube-mobile-buttons' |
3e2bc4ea | 26 | import videojs from 'video.js' |
3e254de8 | 27 | import { HlsJsEngineSettings } from '@peertube/p2p-media-loader-hlsjs' |
72f611ca | 28 | import { PluginsManager } from '@root-helpers/plugins-manager' |
15a7eafb | 29 | import { buildVideoLink, decorateVideoLink } from '@shared/core-utils' |
bd45d503 | 30 | import { isDefaultLocale } from '@shared/core-utils/i18n' |
4572c3d0 | 31 | import { VideoFile } from '@shared/models' |
72f611ca | 32 | import { copyToClipboard } from '../../root-helpers/utils' |
da332417 | 33 | import { RedundancyUrlManager } from './p2p-media-loader/redundancy-url-manager' |
3e2bc4ea C |
34 | import { segmentUrlBuilderFactory } from './p2p-media-loader/segment-url-builder' |
35 | import { segmentValidatorFactory } from './p2p-media-loader/segment-validator' | |
a9bfa85d | 36 | import { getAverageBandwidthInStore, saveAverageBandwidth } from './peertube-player-local-storage' |
4572c3d0 | 37 | import { |
a950e4c8 | 38 | NextPreviousVideoButtonOptions, |
4572c3d0 | 39 | P2PMediaLoaderPluginOptions, |
9162fdd3 | 40 | PeerTubeLinkButtonOptions, |
3e254de8 | 41 | PlayerNetworkInfo, |
4572c3d0 C |
42 | PlaylistPluginOptions, |
43 | UserWatching, | |
44 | VideoJSCaption, | |
45 | VideoJSPluginOptions | |
46 | } from './peertube-videojs-typings' | |
3f9c4955 | 47 | import { TranslationsManager } from './translations-manager' |
f1a0555a | 48 | import { buildVideoOrPlaylistEmbed, getRtcConfig, isIOS, isMobile, isSafari } from './utils' |
2adfc7ea C |
49 | |
50 | // Change 'Playback Rate' to 'Speed' (smaller for our settings menu) | |
f5fcd9f7 C |
51 | (videojs.getComponent('PlaybackRateMenuButton') as any).prototype.controlText_ = 'Speed' |
52 | ||
53 | const CaptionsButton = videojs.getComponent('CaptionsButton') as any | |
2adfc7ea | 54 | // Change Captions to Subtitles/CC |
f5fcd9f7 | 55 | CaptionsButton.prototype.controlText_ = 'Subtitles/CC' |
2adfc7ea | 56 | // We just want to display 'Off' instead of 'captions off', keep a space so the variable == true (hacky I know) |
f5fcd9f7 | 57 | CaptionsButton.prototype.label_ = ' ' |
2adfc7ea | 58 | |
3b6f205c | 59 | export type PlayerMode = 'webtorrent' | 'p2p-media-loader' |
2adfc7ea | 60 | |
3b6f205c | 61 | export type WebtorrentOptions = { |
2adfc7ea C |
62 | videoFiles: VideoFile[] |
63 | } | |
64 | ||
3b6f205c | 65 | export type P2PMediaLoaderOptions = { |
2adfc7ea | 66 | playlistUrl: string |
09209296 | 67 | segmentsSha256Url: string |
4348a27d | 68 | trackerAnnounce: string[] |
09209296 C |
69 | redundancyBaseUrls: string[] |
70 | videoFiles: VideoFile[] | |
2adfc7ea C |
71 | } |
72 | ||
5efab546 C |
73 | export interface CustomizationOptions { |
74 | startTime: number | string | |
75 | stopTime: number | string | |
76 | ||
77 | controls?: boolean | |
78 | muted?: boolean | |
79 | loop?: boolean | |
80 | subtitle?: string | |
96f6278f | 81 | resume?: string |
5efab546 C |
82 | |
83 | peertubeLink: boolean | |
84 | } | |
85 | ||
86 | export interface CommonOptions extends CustomizationOptions { | |
2adfc7ea | 87 | playerElement: HTMLVideoElement |
6ec0b75b | 88 | onPlayerElementChange: (element: HTMLVideoElement) => void |
2adfc7ea C |
89 | |
90 | autoplay: boolean | |
a9bfa85d | 91 | p2pEnabled: boolean |
a950e4c8 C |
92 | |
93 | nextVideo?: () => void | |
94 | hasNextVideo?: () => boolean | |
95 | ||
96 | previousVideo?: () => void | |
97 | hasPreviousVideo?: () => boolean | |
4572c3d0 C |
98 | |
99 | playlist?: PlaylistPluginOptions | |
100 | ||
2adfc7ea C |
101 | videoDuration: number |
102 | enableHotkeys: boolean | |
103 | inactivityTimeout: number | |
104 | poster: string | |
2adfc7ea | 105 | |
3d9a63d3 | 106 | theaterButton: boolean |
2adfc7ea | 107 | captions: boolean |
2adfc7ea C |
108 | |
109 | videoViewUrl: string | |
110 | embedUrl: string | |
4097c6d6 | 111 | embedTitle: string |
2adfc7ea | 112 | |
25b7c847 C |
113 | isLive: boolean |
114 | ||
2adfc7ea | 115 | language?: string |
2adfc7ea C |
116 | |
117 | videoCaptions: VideoJSCaption[] | |
118 | ||
58b9ce30 | 119 | videoUUID: string |
9162fdd3 | 120 | videoShortUUID: string |
58b9ce30 | 121 | |
2adfc7ea C |
122 | userWatching?: UserWatching |
123 | ||
124 | serverUrl: string | |
125 | } | |
126 | ||
127 | export type PeertubePlayerManagerOptions = { | |
72f611ca | 128 | common: CommonOptions |
129 | webtorrent: WebtorrentOptions | |
2adfc7ea | 130 | p2pMediaLoader?: P2PMediaLoaderOptions |
72f611ca | 131 | |
132 | pluginsManager: PluginsManager | |
2adfc7ea C |
133 | } |
134 | ||
135 | export class PeertubePlayerManager { | |
6ec0b75b | 136 | private static playerElementClassName: string |
7e37e111 | 137 | private static onPlayerChange: (player: videojs.Player) => void |
9eccae74 | 138 | private static alreadyPlayed = false |
72f611ca | 139 | private static pluginsManager: PluginsManager |
9eccae74 | 140 | |
1a568b6f C |
141 | static initState () { |
142 | PeertubePlayerManager.alreadyPlayed = false | |
143 | } | |
144 | ||
7e37e111 | 145 | static async initialize (mode: PlayerMode, options: PeertubePlayerManagerOptions, onPlayerChange: (player: videojs.Player) => void) { |
72f611ca | 146 | this.pluginsManager = options.pluginsManager |
147 | ||
4348a27d C |
148 | let p2pMediaLoader: any |
149 | ||
bfbd9128 | 150 | this.onPlayerChange = onPlayerChange |
6ec0b75b C |
151 | this.playerElementClassName = options.common.playerElement.className |
152 | ||
09209296 | 153 | if (mode === 'webtorrent') await import('./webtorrent/webtorrent-plugin') |
4348a27d C |
154 | if (mode === 'p2p-media-loader') { |
155 | [ p2pMediaLoader ] = await Promise.all([ | |
3e254de8 | 156 | import('@peertube/p2p-media-loader-hlsjs'), |
09209296 | 157 | import('./p2p-media-loader/p2p-media-loader-plugin') |
4348a27d C |
158 | ]) |
159 | } | |
2adfc7ea | 160 | |
72f611ca | 161 | const videojsOptions = await this.getVideojsOptions(mode, options, p2pMediaLoader) |
2adfc7ea | 162 | |
3f9c4955 | 163 | await TranslationsManager.loadLocaleInVideoJS(options.common.serverUrl, options.common.language, videojs) |
2adfc7ea C |
164 | |
165 | const self = this | |
166 | return new Promise(res => { | |
7e37e111 | 167 | videojs(options.common.playerElement, videojsOptions, function (this: videojs.Player) { |
2adfc7ea C |
168 | const player = this |
169 | ||
536598cf C |
170 | let alreadyFallback = false |
171 | ||
f5fcd9f7 | 172 | player.tech(true).one('error', () => { |
536598cf C |
173 | if (!alreadyFallback) self.maybeFallbackToWebTorrent(mode, player, options) |
174 | alreadyFallback = true | |
175 | }) | |
176 | ||
177 | player.one('error', () => { | |
178 | if (!alreadyFallback) self.maybeFallbackToWebTorrent(mode, player, options) | |
179 | alreadyFallback = true | |
180 | }) | |
6ec0b75b | 181 | |
9eccae74 C |
182 | player.one('play', () => { |
183 | PeertubePlayerManager.alreadyPlayed = true | |
184 | }) | |
185 | ||
9162fdd3 C |
186 | self.addContextMenu({ |
187 | mode, | |
188 | player, | |
189 | videoShortUUID: options.common.videoShortUUID, | |
190 | videoEmbedUrl: options.common.embedUrl, | |
191 | videoEmbedTitle: options.common.embedTitle | |
192 | }) | |
2adfc7ea | 193 | |
f1a0555a C |
194 | if (isMobile()) player.peertubeMobile() |
195 | ||
10475dea | 196 | player.bezels() |
f1a0555a | 197 | |
ff563914 RK |
198 | player.stats({ |
199 | videoUUID: options.common.videoUUID, | |
200 | videoIsLive: options.common.isLive, | |
95765067 C |
201 | mode, |
202 | p2pEnabled: options.common.p2pEnabled | |
ff563914 | 203 | }) |
10475dea | 204 | |
3e254de8 C |
205 | player.on('p2pInfo', (_, data: PlayerNetworkInfo) => { |
206 | if (data.source !== 'p2p-media-loader' || isNaN(data.bandwidthEstimate)) return | |
207 | ||
208 | saveAverageBandwidth(data.bandwidthEstimate) | |
209 | }) | |
210 | ||
2adfc7ea C |
211 | return res(player) |
212 | }) | |
213 | }) | |
214 | } | |
215 | ||
96cb4527 C |
216 | private static async maybeFallbackToWebTorrent (currentMode: PlayerMode, player: any, options: PeertubePlayerManagerOptions) { |
217 | if (currentMode === 'webtorrent') return | |
218 | ||
219 | console.log('Fallback to webtorrent.') | |
220 | ||
6ec0b75b C |
221 | const newVideoElement = document.createElement('video') |
222 | newVideoElement.className = this.playerElementClassName | |
223 | ||
224 | // VideoJS wraps our video element inside a div | |
96cb4527 C |
225 | let currentParentPlayerElement = options.common.playerElement.parentNode |
226 | // Fix on IOS, don't ask me why | |
227 | if (!currentParentPlayerElement) currentParentPlayerElement = document.getElementById(options.common.playerElement.id).parentNode | |
228 | ||
6ec0b75b C |
229 | currentParentPlayerElement.parentNode.insertBefore(newVideoElement, currentParentPlayerElement) |
230 | ||
231 | options.common.playerElement = newVideoElement | |
232 | options.common.onPlayerElementChange(newVideoElement) | |
233 | ||
234 | player.dispose() | |
235 | ||
236 | await import('./webtorrent/webtorrent-plugin') | |
237 | ||
238 | const mode = 'webtorrent' | |
72f611ca | 239 | const videojsOptions = await this.getVideojsOptions(mode, options) |
6ec0b75b C |
240 | |
241 | const self = this | |
7e37e111 | 242 | videojs(newVideoElement, videojsOptions, function (this: videojs.Player) { |
6ec0b75b C |
243 | const player = this |
244 | ||
9162fdd3 C |
245 | self.addContextMenu({ |
246 | mode, | |
247 | player, | |
248 | videoShortUUID: options.common.videoShortUUID, | |
249 | videoEmbedUrl: options.common.embedUrl, | |
250 | videoEmbedTitle: options.common.embedTitle | |
251 | }) | |
bfbd9128 C |
252 | |
253 | PeertubePlayerManager.onPlayerChange(player) | |
6ec0b75b C |
254 | }) |
255 | } | |
256 | ||
72f611ca | 257 | private static async getVideojsOptions ( |
f5fcd9f7 C |
258 | mode: PlayerMode, |
259 | options: PeertubePlayerManagerOptions, | |
260 | p2pMediaLoaderModule?: any | |
72f611ca | 261 | ): Promise<videojs.PlayerOptions> { |
2adfc7ea | 262 | const commonOptions = options.common |
9eccae74 | 263 | const isHLS = mode === 'p2p-media-loader' |
09209296 | 264 | |
72efdda5 | 265 | let autoplay = this.getAutoPlayValue(commonOptions.autoplay) |
72f611ca | 266 | const html5 = { |
93f30abf C |
267 | preloadTextTracks: false |
268 | } | |
2adfc7ea C |
269 | |
270 | const plugins: VideoJSPluginOptions = { | |
271 | peertube: { | |
09209296 | 272 | mode, |
cb5c2abc | 273 | autoplay, // Use peertube plugin autoplay because we could get the file by webtorrent |
2adfc7ea C |
274 | videoViewUrl: commonOptions.videoViewUrl, |
275 | videoDuration: commonOptions.videoDuration, | |
2adfc7ea C |
276 | userWatching: commonOptions.userWatching, |
277 | subtitle: commonOptions.subtitle, | |
f0a39880 | 278 | videoCaptions: commonOptions.videoCaptions, |
10f26f42 | 279 | stopTime: commonOptions.stopTime, |
58b9ce30 | 280 | isLive: commonOptions.isLive, |
281 | videoUUID: commonOptions.videoUUID | |
2adfc7ea C |
282 | } |
283 | } | |
284 | ||
3e0e8d4a | 285 | if (commonOptions.playlist) { |
4572c3d0 C |
286 | plugins.playlist = commonOptions.playlist |
287 | } | |
288 | ||
39aad8cc C |
289 | if (commonOptions.enableHotkeys === true) { |
290 | PeertubePlayerManager.addHotkeysOptions(plugins) | |
291 | } | |
09209296 | 292 | |
9eccae74 | 293 | if (isHLS) { |
83fcadac | 294 | const { hlsjs } = PeertubePlayerManager.addP2PMediaLoaderOptions(plugins, options, p2pMediaLoaderModule) |
2adfc7ea | 295 | |
93f30abf | 296 | Object.assign(html5, hlsjs.html5) |
2adfc7ea C |
297 | } |
298 | ||
6ec0b75b | 299 | if (mode === 'webtorrent') { |
39aad8cc | 300 | PeertubePlayerManager.addWebTorrentOptions(plugins, options) |
09209296 C |
301 | |
302 | // WebTorrent plugin handles autoplay, because we do some hackish stuff in there | |
303 | autoplay = false | |
2adfc7ea C |
304 | } |
305 | ||
306 | const videojsOptions = { | |
3b6f205c C |
307 | html5, |
308 | ||
2adfc7ea | 309 | // We don't use text track settings for now |
f5fcd9f7 | 310 | textTrackSettings: false as any, // FIXME: typings |
2adfc7ea C |
311 | controls: commonOptions.controls !== undefined ? commonOptions.controls : true, |
312 | loop: commonOptions.loop !== undefined ? commonOptions.loop : false, | |
313 | ||
314 | muted: commonOptions.muted !== undefined | |
315 | ? commonOptions.muted | |
316 | : undefined, // Undefined so the player knows it has to check the local storage | |
317 | ||
72efdda5 | 318 | autoplay: this.getAutoPlayValue(autoplay), |
39aad8cc | 319 | |
2adfc7ea | 320 | poster: commonOptions.poster, |
2adfc7ea | 321 | inactivityTimeout: commonOptions.inactivityTimeout, |
08844775 | 322 | playbackRates: [ 0.5, 0.75, 1, 1.25, 1.5, 1.75, 2 ], |
39aad8cc | 323 | |
2adfc7ea | 324 | plugins, |
39aad8cc | 325 | |
2adfc7ea C |
326 | controlBar: { |
327 | children: this.getControlBarChildren(mode, { | |
9162fdd3 | 328 | videoShortUUID: commonOptions.videoShortUUID, |
bf1c3c78 | 329 | p2pEnabled: commonOptions.p2pEnabled, |
9162fdd3 | 330 | |
2adfc7ea C |
331 | captions: commonOptions.captions, |
332 | peertubeLink: commonOptions.peertubeLink, | |
1dc240a9 | 333 | theaterButton: commonOptions.theaterButton, |
a950e4c8 C |
334 | |
335 | nextVideo: commonOptions.nextVideo, | |
336 | hasNextVideo: commonOptions.hasNextVideo, | |
337 | ||
338 | previousVideo: commonOptions.previousVideo, | |
339 | hasPreviousVideo: commonOptions.hasPreviousVideo | |
f5fcd9f7 | 340 | }) as any // FIXME: typings |
2adfc7ea C |
341 | } |
342 | } | |
343 | ||
39aad8cc C |
344 | if (commonOptions.language && !isDefaultLocale(commonOptions.language)) { |
345 | Object.assign(videojsOptions, { language: commonOptions.language }) | |
346 | } | |
2adfc7ea | 347 | |
72f611ca | 348 | return this.pluginsManager.runHook('filter:internal.player.videojs.options.result', videojsOptions) |
39aad8cc | 349 | } |
2adfc7ea | 350 | |
39aad8cc C |
351 | private static addP2PMediaLoaderOptions ( |
352 | plugins: VideoJSPluginOptions, | |
353 | options: PeertubePlayerManagerOptions, | |
354 | p2pMediaLoaderModule: any | |
355 | ) { | |
356 | const p2pMediaLoaderOptions = options.p2pMediaLoader | |
357 | const commonOptions = options.common | |
358 | ||
359 | const trackerAnnounce = p2pMediaLoaderOptions.trackerAnnounce | |
360 | .filter(t => t.startsWith('ws')) | |
361 | ||
362 | const redundancyUrlManager = new RedundancyUrlManager(options.p2pMediaLoader.redundancyBaseUrls) | |
363 | ||
364 | const p2pMediaLoader: P2PMediaLoaderPluginOptions = { | |
365 | redundancyUrlManager, | |
366 | type: 'application/x-mpegURL', | |
367 | startTime: commonOptions.startTime, | |
368 | src: p2pMediaLoaderOptions.playlistUrl | |
369 | } | |
370 | ||
371 | let consumeOnly = false | |
5196817c | 372 | if ((navigator as any)?.connection?.type === 'cellular') { |
39aad8cc C |
373 | console.log('We are on a cellular connection: disabling seeding.') |
374 | consumeOnly = true | |
375 | } | |
376 | ||
3e254de8 | 377 | const p2pMediaLoaderConfig: HlsJsEngineSettings = { |
39aad8cc C |
378 | loader: { |
379 | trackerAnnounce, | |
25b7c847 | 380 | segmentValidator: segmentValidatorFactory(options.p2pMediaLoader.segmentsSha256Url, options.common.isLive), |
39aad8cc | 381 | rtcConfig: getRtcConfig(), |
c6c0fa6c | 382 | requiredSegmentsPriority: 1, |
3e254de8 | 383 | simultaneousHttpDownloads: 1, |
0067a77d | 384 | segmentUrlBuilder: segmentUrlBuilderFactory(redundancyUrlManager, 1), |
a9bfa85d | 385 | useP2P: commonOptions.p2pEnabled, |
39aad8cc C |
386 | consumeOnly |
387 | }, | |
388 | segments: { | |
389 | swarmId: p2pMediaLoaderOptions.playlistUrl | |
390 | } | |
391 | } | |
3e254de8 | 392 | |
83fcadac | 393 | const hlsjs = { |
39aad8cc | 394 | levelLabelHandler: (level: { height: number, width: number }) => { |
dca0fe12 C |
395 | const resolution = Math.min(level.height || 0, level.width || 0) |
396 | ||
397 | const file = p2pMediaLoaderOptions.videoFiles.find(f => f.resolution.id === resolution) | |
053aed43 C |
398 | // We don't have files for live videos |
399 | if (!file) return level.height | |
39aad8cc C |
400 | |
401 | let label = file.resolution.label | |
402 | if (file.fps >= 50) label += file.fps | |
403 | ||
404 | return label | |
405 | }, | |
406 | html5: { | |
3e254de8 | 407 | hlsjsConfig: this.getHLSOptions(p2pMediaLoaderModule, p2pMediaLoaderConfig) |
39aad8cc | 408 | } |
2adfc7ea C |
409 | } |
410 | ||
83fcadac | 411 | const toAssign = { p2pMediaLoader, hlsjs } |
39aad8cc C |
412 | Object.assign(plugins, toAssign) |
413 | ||
414 | return toAssign | |
415 | } | |
416 | ||
3e254de8 C |
417 | private static getHLSOptions (p2pMediaLoaderModule: any, p2pMediaLoaderConfig: HlsJsEngineSettings) { |
418 | const base = { | |
419 | capLevelToPlayerSize: true, | |
420 | autoStartLoad: false, | |
421 | liveSyncDurationCount: 5, | |
422 | ||
423 | loader: new p2pMediaLoaderModule.Engine(p2pMediaLoaderConfig).createLoaderClass() | |
424 | } | |
425 | ||
426 | const averageBandwidth = getAverageBandwidthInStore() | |
427 | if (!averageBandwidth) return base | |
428 | ||
429 | return { | |
430 | ...base, | |
431 | ||
432 | abrEwmaDefaultEstimate: averageBandwidth * 8, // We want bit/s | |
433 | startLevel: -1, | |
434 | testBandwidth: false, | |
435 | debug: false | |
436 | } | |
437 | } | |
438 | ||
39aad8cc C |
439 | private static addWebTorrentOptions (plugins: VideoJSPluginOptions, options: PeertubePlayerManagerOptions) { |
440 | const commonOptions = options.common | |
441 | const webtorrentOptions = options.webtorrent | |
3e9cf564 | 442 | const p2pMediaLoaderOptions = options.p2pMediaLoader |
39aad8cc | 443 | |
ebc8dd52 | 444 | const autoplay = this.getAutoPlayValue(commonOptions.autoplay) === 'play' |
ebc8dd52 | 445 | |
39aad8cc | 446 | const webtorrent = { |
ebc8dd52 | 447 | autoplay, |
a9bfa85d | 448 | playerRefusedP2P: commonOptions.p2pEnabled === false, |
39aad8cc C |
449 | videoDuration: commonOptions.videoDuration, |
450 | playerElement: commonOptions.playerElement, | |
3e9cf564 C |
451 | videoFiles: webtorrentOptions.videoFiles.length !== 0 |
452 | ? webtorrentOptions.videoFiles | |
453 | // The WebTorrent plugin won't be able to play these files, but it will fallback to HTTP mode | |
05287a2e | 454 | : p2pMediaLoaderOptions?.videoFiles || [], |
39aad8cc | 455 | startTime: commonOptions.startTime |
2adfc7ea C |
456 | } |
457 | ||
39aad8cc | 458 | Object.assign(plugins, { webtorrent }) |
2adfc7ea C |
459 | } |
460 | ||
461 | private static getControlBarChildren (mode: PlayerMode, options: { | |
bf1c3c78 | 462 | p2pEnabled: boolean |
9162fdd3 C |
463 | videoShortUUID: string |
464 | ||
2adfc7ea | 465 | peertubeLink: boolean |
a950e4c8 C |
466 | theaterButton: boolean |
467 | captions: boolean | |
468 | ||
9df52d66 | 469 | nextVideo?: () => void |
a950e4c8 C |
470 | hasNextVideo?: () => boolean |
471 | ||
9df52d66 | 472 | previousVideo?: () => void |
a950e4c8 | 473 | hasPreviousVideo?: () => boolean |
2adfc7ea C |
474 | }) { |
475 | const settingEntries = [] | |
476 | const loadProgressBar = mode === 'webtorrent' ? 'peerTubeLoadProgressBar' : 'loadProgressBar' | |
477 | ||
478 | // Keep an order | |
479 | settingEntries.push('playbackRateMenuButton') | |
480 | if (options.captions === true) settingEntries.push('captionsButton') | |
481 | settingEntries.push('resolutionMenuButton') | |
482 | ||
a950e4c8 C |
483 | const children = {} |
484 | ||
485 | if (options.previousVideo) { | |
486 | const buttonOptions: NextPreviousVideoButtonOptions = { | |
487 | type: 'previous', | |
488 | handler: options.previousVideo, | |
489 | isDisabled: () => { | |
490 | if (!options.hasPreviousVideo) return false | |
491 | ||
492 | return !options.hasPreviousVideo() | |
493 | } | |
494 | } | |
495 | ||
496 | Object.assign(children, { | |
9df52d66 | 497 | previousVideoButton: buttonOptions |
a950e4c8 | 498 | }) |
1dc240a9 RK |
499 | } |
500 | ||
a950e4c8 C |
501 | Object.assign(children, { playToggle: {} }) |
502 | ||
1dc240a9 | 503 | if (options.nextVideo) { |
a950e4c8 C |
504 | const buttonOptions: NextPreviousVideoButtonOptions = { |
505 | type: 'next', | |
506 | handler: options.nextVideo, | |
507 | isDisabled: () => { | |
508 | if (!options.hasNextVideo) return false | |
509 | ||
510 | return !options.hasNextVideo() | |
1dc240a9 | 511 | } |
a950e4c8 C |
512 | } |
513 | ||
514 | Object.assign(children, { | |
9df52d66 | 515 | nextVideoButton: buttonOptions |
1dc240a9 RK |
516 | }) |
517 | } | |
518 | ||
519 | Object.assign(children, { | |
9df52d66 C |
520 | currentTimeDisplay: {}, |
521 | timeDivider: {}, | |
522 | durationDisplay: {}, | |
523 | liveDisplay: {}, | |
2adfc7ea | 524 | |
9df52d66 C |
525 | flexibleWidthSpacer: {}, |
526 | progressControl: { | |
2adfc7ea | 527 | children: { |
9df52d66 | 528 | seekBar: { |
2adfc7ea C |
529 | children: { |
530 | [loadProgressBar]: {}, | |
9df52d66 C |
531 | mouseTimeDisplay: {}, |
532 | playProgressBar: {} | |
2adfc7ea C |
533 | } |
534 | } | |
535 | } | |
536 | }, | |
537 | ||
bf1c3c78 C |
538 | p2PInfoButton: { |
539 | p2pEnabled: options.p2pEnabled | |
540 | }, | |
2adfc7ea | 541 | |
9df52d66 C |
542 | muteToggle: {}, |
543 | volumeControl: {}, | |
2adfc7ea | 544 | |
9df52d66 | 545 | settingsButton: { |
2adfc7ea C |
546 | setup: { |
547 | maxHeightOffset: 40 | |
548 | }, | |
549 | entries: settingEntries | |
550 | } | |
1dc240a9 | 551 | }) |
2adfc7ea C |
552 | |
553 | if (options.peertubeLink === true) { | |
554 | Object.assign(children, { | |
9df52d66 | 555 | peerTubeLinkButton: { shortUUID: options.videoShortUUID } as PeerTubeLinkButtonOptions |
2adfc7ea C |
556 | }) |
557 | } | |
558 | ||
3d9a63d3 | 559 | if (options.theaterButton === true) { |
2adfc7ea | 560 | Object.assign(children, { |
9df52d66 | 561 | theaterButton: {} |
2adfc7ea C |
562 | }) |
563 | } | |
564 | ||
565 | Object.assign(children, { | |
9df52d66 | 566 | fullscreenToggle: {} |
2adfc7ea C |
567 | }) |
568 | ||
569 | return children | |
570 | } | |
571 | ||
9162fdd3 C |
572 | private static addContextMenu (options: { |
573 | mode: PlayerMode | |
574 | player: videojs.Player | |
575 | videoShortUUID: string | |
576 | videoEmbedUrl: string | |
577 | videoEmbedTitle: string | |
578 | }) { | |
579 | const { mode, player, videoEmbedTitle, videoEmbedUrl, videoShortUUID } = options | |
580 | ||
a472cf03 | 581 | const content = () => { |
3e0e8d4a C |
582 | const isLoopEnabled = player.options_['loop'] |
583 | const items = [ | |
584 | { | |
585 | icon: 'repeat', | |
586 | label: player.localize('Play in loop') + (isLoopEnabled ? '<span class="vjs-icon-tick-white"></span>' : ''), | |
587 | listener: function () { | |
588 | player.options_['loop'] = !isLoopEnabled | |
a472cf03 | 589 | } |
3e0e8d4a C |
590 | }, |
591 | { | |
592 | label: player.localize('Copy the video URL'), | |
593 | listener: function () { | |
9162fdd3 | 594 | copyToClipboard(buildVideoLink({ shortUUID: videoShortUUID })) |
3e0e8d4a C |
595 | } |
596 | }, | |
597 | { | |
598 | label: player.localize('Copy the video URL at the current time'), | |
599 | listener: function (this: videojs.Player) { | |
9162fdd3 C |
600 | const url = buildVideoLink({ shortUUID: videoShortUUID }) |
601 | ||
602 | copyToClipboard(decorateVideoLink({ url, startTime: this.currentTime() })) | |
3e0e8d4a C |
603 | } |
604 | }, | |
605 | { | |
606 | icon: 'code', | |
607 | label: player.localize('Copy embed code'), | |
608 | listener: () => { | |
609 | copyToClipboard(buildVideoOrPlaylistEmbed(videoEmbedUrl, videoEmbedTitle)) | |
a472cf03 | 610 | } |
2adfc7ea | 611 | } |
3e0e8d4a | 612 | ] |
a472cf03 RK |
613 | |
614 | if (mode === 'webtorrent') { | |
615 | items.push({ | |
616 | label: player.localize('Copy magnet URI'), | |
617 | listener: function (this: videojs.Player) { | |
618 | copyToClipboard(this.webtorrent().getCurrentVideoFile().magnetUri) | |
619 | } | |
620 | }) | |
2adfc7ea | 621 | } |
2adfc7ea | 622 | |
ff563914 RK |
623 | items.push({ |
624 | icon: 'info', | |
625 | label: player.localize('Stats for nerds'), | |
626 | listener: () => { | |
627 | player.stats().show() | |
628 | } | |
629 | }) | |
630 | ||
83ff5481 RK |
631 | return items.map(i => ({ |
632 | ...i, | |
633 | label: `<span class="vjs-icon-${i.icon || 'link-2'}"></span>` + i.label | |
634 | })) | |
2adfc7ea C |
635 | } |
636 | ||
a472cf03 | 637 | // adding the menu |
2adfc7ea C |
638 | player.contextmenuUI({ content }) |
639 | } | |
640 | ||
39aad8cc | 641 | private static addHotkeysOptions (plugins: VideoJSPluginOptions) { |
e0b59721 | 642 | const isNaked = (event: KeyboardEvent, key: string) => |
643 | (!event.ctrlKey && !event.altKey && !event.metaKey && !event.shiftKey && event.key === key) | |
644 | ||
39aad8cc C |
645 | Object.assign(plugins, { |
646 | hotkeys: { | |
7ede74ad C |
647 | skipInitialFocus: true, |
648 | enableInactiveFocus: false, | |
649 | captureDocumentHotkeys: true, | |
650 | documentHotkeysFocusElementFilter: (e: HTMLElement) => { | |
e85bfe96 C |
651 | const tagName = e.tagName.toLowerCase() |
652 | return e.id === 'content' || tagName === 'body' || tagName === 'video' | |
7ede74ad C |
653 | }, |
654 | ||
39aad8cc C |
655 | enableVolumeScroll: false, |
656 | enableModifiersForNumbers: false, | |
657 | ||
684cdacb | 658 | rewindKey: function (event: KeyboardEvent) { |
659 | return isNaked(event, 'ArrowLeft') | |
660 | }, | |
661 | ||
662 | forwardKey: function (event: KeyboardEvent) { | |
663 | return isNaked(event, 'ArrowRight') | |
664 | }, | |
665 | ||
39aad8cc C |
666 | fullscreenKey: function (event: KeyboardEvent) { |
667 | // fullscreen with the f key or Ctrl+Enter | |
e0b59721 | 668 | return isNaked(event, 'f') || (!event.altKey && event.ctrlKey && event.key === 'Enter') |
39aad8cc C |
669 | }, |
670 | ||
39aad8cc C |
671 | customKeys: { |
672 | increasePlaybackRateKey: { | |
673 | key: function (event: KeyboardEvent) { | |
e0b59721 | 674 | return isNaked(event, '>') |
39aad8cc C |
675 | }, |
676 | handler: function (player: videojs.Player) { | |
f5fcd9f7 C |
677 | const newValue = Math.min(player.playbackRate() + 0.1, 5) |
678 | player.playbackRate(parseFloat(newValue.toFixed(2))) | |
39aad8cc C |
679 | } |
680 | }, | |
681 | decreasePlaybackRateKey: { | |
682 | key: function (event: KeyboardEvent) { | |
e0b59721 | 683 | return isNaked(event, '<') |
39aad8cc C |
684 | }, |
685 | handler: function (player: videojs.Player) { | |
f5fcd9f7 C |
686 | const newValue = Math.max(player.playbackRate() - 0.1, 0.10) |
687 | player.playbackRate(parseFloat(newValue.toFixed(2))) | |
39aad8cc C |
688 | } |
689 | }, | |
fc3412fd | 690 | previousFrame: { |
39aad8cc | 691 | key: function (event: KeyboardEvent) { |
fc3412fd C |
692 | return event.key === ',' |
693 | }, | |
694 | handler: function (player: videojs.Player) { | |
695 | player.pause() | |
696 | // Calculate movement distance (assuming 30 fps) | |
697 | const dist = 1 / 30 | |
698 | player.currentTime(player.currentTime() - dist) | |
699 | } | |
700 | }, | |
701 | nextFrame: { | |
702 | key: function (event: KeyboardEvent) { | |
703 | return event.key === '.' | |
39aad8cc C |
704 | }, |
705 | handler: function (player: videojs.Player) { | |
706 | player.pause() | |
707 | // Calculate movement distance (assuming 30 fps) | |
708 | const dist = 1 / 30 | |
709 | player.currentTime(player.currentTime() + dist) | |
710 | } | |
711 | } | |
712 | } | |
713 | } | |
714 | }) | |
715 | } | |
64228474 | 716 | |
72efdda5 C |
717 | private static getAutoPlayValue (autoplay: any) { |
718 | if (autoplay !== true) return autoplay | |
719 | ||
ebc8dd52 C |
720 | // On first play, disable autoplay to avoid issues |
721 | // But if the player already played videos, we can safely autoplay next ones | |
722 | if (isIOS() || isSafari()) { | |
9eccae74 C |
723 | return PeertubePlayerManager.alreadyPlayed ? 'play' : false |
724 | } | |
64228474 C |
725 | |
726 | return 'play' | |
727 | } | |
2adfc7ea C |
728 | } |
729 | ||
730 | // ############################################################################ | |
731 | ||
732 | export { | |
733 | videojs | |
734 | } |