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