]>
Commit | Line | Data |
---|---|---|
512decf3 | 1 | import 'videojs-hotkeys/videojs.hotkeys' |
2adfc7ea C |
2 | import 'videojs-dock' |
3 | import 'videojs-contextmenu-ui' | |
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' |
afff310e RK |
38 | import { buildVideoOrPlaylistEmbed, buildVideoLink, getRtcConfig, isSafari, isIOS } from './utils' |
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 | |
90 | ||
2adfc7ea C |
91 | videoDuration: number |
92 | enableHotkeys: boolean | |
93 | inactivityTimeout: number | |
94 | poster: string | |
2adfc7ea | 95 | |
3d9a63d3 | 96 | theaterButton: boolean |
2adfc7ea | 97 | captions: boolean |
2adfc7ea C |
98 | |
99 | videoViewUrl: string | |
100 | embedUrl: string | |
4097c6d6 | 101 | embedTitle: string |
2adfc7ea | 102 | |
25b7c847 C |
103 | isLive: boolean |
104 | ||
2adfc7ea | 105 | language?: string |
2adfc7ea C |
106 | |
107 | videoCaptions: VideoJSCaption[] | |
108 | ||
58b9ce30 | 109 | videoUUID: string |
110 | ||
2adfc7ea C |
111 | userWatching?: UserWatching |
112 | ||
113 | serverUrl: string | |
114 | } | |
115 | ||
116 | export type PeertubePlayerManagerOptions = { | |
117 | common: CommonOptions, | |
6ec0b75b | 118 | webtorrent: WebtorrentOptions, |
2adfc7ea C |
119 | p2pMediaLoader?: P2PMediaLoaderOptions |
120 | } | |
121 | ||
122 | export class PeertubePlayerManager { | |
6ec0b75b | 123 | private static playerElementClassName: string |
7e37e111 | 124 | private static onPlayerChange: (player: videojs.Player) => void |
2adfc7ea | 125 | |
9eccae74 C |
126 | private static alreadyPlayed = false |
127 | ||
1a568b6f C |
128 | static initState () { |
129 | PeertubePlayerManager.alreadyPlayed = false | |
130 | } | |
131 | ||
7e37e111 | 132 | static async initialize (mode: PlayerMode, options: PeertubePlayerManagerOptions, onPlayerChange: (player: videojs.Player) => void) { |
4348a27d C |
133 | let p2pMediaLoader: any |
134 | ||
bfbd9128 | 135 | this.onPlayerChange = onPlayerChange |
6ec0b75b C |
136 | this.playerElementClassName = options.common.playerElement.className |
137 | ||
09209296 | 138 | if (mode === 'webtorrent') await import('./webtorrent/webtorrent-plugin') |
4348a27d C |
139 | if (mode === 'p2p-media-loader') { |
140 | [ p2pMediaLoader ] = await Promise.all([ | |
141 | import('p2p-media-loader-hlsjs'), | |
09209296 | 142 | import('./p2p-media-loader/p2p-media-loader-plugin') |
4348a27d C |
143 | ]) |
144 | } | |
2adfc7ea | 145 | |
4348a27d | 146 | const videojsOptions = this.getVideojsOptions(mode, options, p2pMediaLoader) |
2adfc7ea | 147 | |
3f9c4955 | 148 | await TranslationsManager.loadLocaleInVideoJS(options.common.serverUrl, options.common.language, videojs) |
2adfc7ea C |
149 | |
150 | const self = this | |
151 | return new Promise(res => { | |
7e37e111 | 152 | videojs(options.common.playerElement, videojsOptions, function (this: videojs.Player) { |
2adfc7ea C |
153 | const player = this |
154 | ||
536598cf C |
155 | let alreadyFallback = false |
156 | ||
f5fcd9f7 | 157 | player.tech(true).one('error', () => { |
536598cf C |
158 | if (!alreadyFallback) self.maybeFallbackToWebTorrent(mode, player, options) |
159 | alreadyFallback = true | |
160 | }) | |
161 | ||
162 | player.one('error', () => { | |
163 | if (!alreadyFallback) self.maybeFallbackToWebTorrent(mode, player, options) | |
164 | alreadyFallback = true | |
165 | }) | |
6ec0b75b | 166 | |
9eccae74 C |
167 | player.one('play', () => { |
168 | PeertubePlayerManager.alreadyPlayed = true | |
169 | }) | |
170 | ||
4097c6d6 | 171 | self.addContextMenu(mode, player, options.common.embedUrl, options.common.embedTitle) |
2adfc7ea | 172 | |
10475dea RK |
173 | player.bezels() |
174 | ||
2adfc7ea C |
175 | return res(player) |
176 | }) | |
177 | }) | |
178 | } | |
179 | ||
96cb4527 C |
180 | private static async maybeFallbackToWebTorrent (currentMode: PlayerMode, player: any, options: PeertubePlayerManagerOptions) { |
181 | if (currentMode === 'webtorrent') return | |
182 | ||
183 | console.log('Fallback to webtorrent.') | |
184 | ||
6ec0b75b C |
185 | const newVideoElement = document.createElement('video') |
186 | newVideoElement.className = this.playerElementClassName | |
187 | ||
188 | // VideoJS wraps our video element inside a div | |
96cb4527 C |
189 | let currentParentPlayerElement = options.common.playerElement.parentNode |
190 | // Fix on IOS, don't ask me why | |
191 | if (!currentParentPlayerElement) currentParentPlayerElement = document.getElementById(options.common.playerElement.id).parentNode | |
192 | ||
6ec0b75b C |
193 | currentParentPlayerElement.parentNode.insertBefore(newVideoElement, currentParentPlayerElement) |
194 | ||
195 | options.common.playerElement = newVideoElement | |
196 | options.common.onPlayerElementChange(newVideoElement) | |
197 | ||
198 | player.dispose() | |
199 | ||
200 | await import('./webtorrent/webtorrent-plugin') | |
201 | ||
202 | const mode = 'webtorrent' | |
203 | const videojsOptions = this.getVideojsOptions(mode, options) | |
204 | ||
205 | const self = this | |
7e37e111 | 206 | videojs(newVideoElement, videojsOptions, function (this: videojs.Player) { |
6ec0b75b C |
207 | const player = this |
208 | ||
4097c6d6 | 209 | self.addContextMenu(mode, player, options.common.embedUrl, options.common.embedTitle) |
bfbd9128 C |
210 | |
211 | PeertubePlayerManager.onPlayerChange(player) | |
6ec0b75b C |
212 | }) |
213 | } | |
214 | ||
f5fcd9f7 C |
215 | private static getVideojsOptions ( |
216 | mode: PlayerMode, | |
217 | options: PeertubePlayerManagerOptions, | |
218 | p2pMediaLoaderModule?: any | |
7e37e111 | 219 | ): videojs.PlayerOptions { |
2adfc7ea | 220 | const commonOptions = options.common |
9eccae74 | 221 | const isHLS = mode === 'p2p-media-loader' |
09209296 | 222 | |
72efdda5 | 223 | let autoplay = this.getAutoPlayValue(commonOptions.autoplay) |
3b6f205c | 224 | let html5 = {} |
2adfc7ea C |
225 | |
226 | const plugins: VideoJSPluginOptions = { | |
227 | peertube: { | |
09209296 | 228 | mode, |
cb5c2abc | 229 | autoplay, // Use peertube plugin autoplay because we could get the file by webtorrent |
2adfc7ea C |
230 | videoViewUrl: commonOptions.videoViewUrl, |
231 | videoDuration: commonOptions.videoDuration, | |
2adfc7ea C |
232 | userWatching: commonOptions.userWatching, |
233 | subtitle: commonOptions.subtitle, | |
f0a39880 | 234 | videoCaptions: commonOptions.videoCaptions, |
10f26f42 | 235 | stopTime: commonOptions.stopTime, |
58b9ce30 | 236 | isLive: commonOptions.isLive, |
237 | videoUUID: commonOptions.videoUUID | |
2adfc7ea C |
238 | } |
239 | } | |
240 | ||
4572c3d0 C |
241 | if (commonOptions.playlist) { |
242 | plugins.playlist = commonOptions.playlist | |
243 | } | |
244 | ||
39aad8cc C |
245 | if (commonOptions.enableHotkeys === true) { |
246 | PeertubePlayerManager.addHotkeysOptions(plugins) | |
247 | } | |
09209296 | 248 | |
9eccae74 | 249 | if (isHLS) { |
83fcadac | 250 | const { hlsjs } = PeertubePlayerManager.addP2PMediaLoaderOptions(plugins, options, p2pMediaLoaderModule) |
2adfc7ea | 251 | |
83fcadac | 252 | html5 = hlsjs.html5 |
2adfc7ea C |
253 | } |
254 | ||
6ec0b75b | 255 | if (mode === 'webtorrent') { |
39aad8cc | 256 | PeertubePlayerManager.addWebTorrentOptions(plugins, options) |
09209296 C |
257 | |
258 | // WebTorrent plugin handles autoplay, because we do some hackish stuff in there | |
259 | autoplay = false | |
2adfc7ea C |
260 | } |
261 | ||
262 | const videojsOptions = { | |
3b6f205c C |
263 | html5, |
264 | ||
2adfc7ea | 265 | // We don't use text track settings for now |
f5fcd9f7 | 266 | textTrackSettings: false as any, // FIXME: typings |
2adfc7ea C |
267 | controls: commonOptions.controls !== undefined ? commonOptions.controls : true, |
268 | loop: commonOptions.loop !== undefined ? commonOptions.loop : false, | |
269 | ||
270 | muted: commonOptions.muted !== undefined | |
271 | ? commonOptions.muted | |
272 | : undefined, // Undefined so the player knows it has to check the local storage | |
273 | ||
72efdda5 | 274 | autoplay: this.getAutoPlayValue(autoplay), |
39aad8cc | 275 | |
2adfc7ea | 276 | poster: commonOptions.poster, |
2adfc7ea | 277 | inactivityTimeout: commonOptions.inactivityTimeout, |
08844775 | 278 | playbackRates: [ 0.5, 0.75, 1, 1.25, 1.5, 1.75, 2 ], |
39aad8cc | 279 | |
2adfc7ea | 280 | plugins, |
39aad8cc | 281 | |
2adfc7ea C |
282 | controlBar: { |
283 | children: this.getControlBarChildren(mode, { | |
284 | captions: commonOptions.captions, | |
285 | peertubeLink: commonOptions.peertubeLink, | |
1dc240a9 | 286 | theaterButton: commonOptions.theaterButton, |
a950e4c8 C |
287 | |
288 | nextVideo: commonOptions.nextVideo, | |
289 | hasNextVideo: commonOptions.hasNextVideo, | |
290 | ||
291 | previousVideo: commonOptions.previousVideo, | |
292 | hasPreviousVideo: commonOptions.hasPreviousVideo | |
f5fcd9f7 | 293 | }) as any // FIXME: typings |
2adfc7ea C |
294 | } |
295 | } | |
296 | ||
39aad8cc C |
297 | if (commonOptions.language && !isDefaultLocale(commonOptions.language)) { |
298 | Object.assign(videojsOptions, { language: commonOptions.language }) | |
299 | } | |
2adfc7ea | 300 | |
39aad8cc C |
301 | return videojsOptions |
302 | } | |
2adfc7ea | 303 | |
39aad8cc C |
304 | private static addP2PMediaLoaderOptions ( |
305 | plugins: VideoJSPluginOptions, | |
306 | options: PeertubePlayerManagerOptions, | |
307 | p2pMediaLoaderModule: any | |
308 | ) { | |
309 | const p2pMediaLoaderOptions = options.p2pMediaLoader | |
310 | const commonOptions = options.common | |
311 | ||
312 | const trackerAnnounce = p2pMediaLoaderOptions.trackerAnnounce | |
313 | .filter(t => t.startsWith('ws')) | |
314 | ||
315 | const redundancyUrlManager = new RedundancyUrlManager(options.p2pMediaLoader.redundancyBaseUrls) | |
316 | ||
317 | const p2pMediaLoader: P2PMediaLoaderPluginOptions = { | |
318 | redundancyUrlManager, | |
319 | type: 'application/x-mpegURL', | |
320 | startTime: commonOptions.startTime, | |
321 | src: p2pMediaLoaderOptions.playlistUrl | |
322 | } | |
323 | ||
324 | let consumeOnly = false | |
325 | // FIXME: typings | |
fe9d0531 | 326 | if (navigator && (navigator as any).connection && (navigator as any).connection.type === 'cellular') { |
39aad8cc C |
327 | console.log('We are on a cellular connection: disabling seeding.') |
328 | consumeOnly = true | |
329 | } | |
330 | ||
331 | const p2pMediaLoaderConfig = { | |
332 | loader: { | |
333 | trackerAnnounce, | |
25b7c847 | 334 | segmentValidator: segmentValidatorFactory(options.p2pMediaLoader.segmentsSha256Url, options.common.isLive), |
39aad8cc | 335 | rtcConfig: getRtcConfig(), |
c6c0fa6c | 336 | requiredSegmentsPriority: 1, |
39aad8cc C |
337 | segmentUrlBuilder: segmentUrlBuilderFactory(redundancyUrlManager), |
338 | useP2P: getStoredP2PEnabled(), | |
339 | consumeOnly | |
340 | }, | |
341 | segments: { | |
342 | swarmId: p2pMediaLoaderOptions.playlistUrl | |
343 | } | |
344 | } | |
83fcadac | 345 | const hlsjs = { |
39aad8cc | 346 | levelLabelHandler: (level: { height: number, width: number }) => { |
dca0fe12 C |
347 | const resolution = Math.min(level.height || 0, level.width || 0) |
348 | ||
349 | const file = p2pMediaLoaderOptions.videoFiles.find(f => f.resolution.id === resolution) | |
053aed43 C |
350 | // We don't have files for live videos |
351 | if (!file) return level.height | |
39aad8cc C |
352 | |
353 | let label = file.resolution.label | |
354 | if (file.fps >= 50) label += file.fps | |
355 | ||
356 | return label | |
357 | }, | |
358 | html5: { | |
359 | hlsjsConfig: { | |
360 | capLevelToPlayerSize: true, | |
361 | autoStartLoad: false, | |
e14de000 | 362 | liveSyncDurationCount: 5, |
39aad8cc | 363 | loader: new p2pMediaLoaderModule.Engine(p2pMediaLoaderConfig).createLoaderClass() |
2adfc7ea | 364 | } |
39aad8cc | 365 | } |
2adfc7ea C |
366 | } |
367 | ||
83fcadac | 368 | const toAssign = { p2pMediaLoader, hlsjs } |
39aad8cc C |
369 | Object.assign(plugins, toAssign) |
370 | ||
371 | return toAssign | |
372 | } | |
373 | ||
374 | private static addWebTorrentOptions (plugins: VideoJSPluginOptions, options: PeertubePlayerManagerOptions) { | |
375 | const commonOptions = options.common | |
376 | const webtorrentOptions = options.webtorrent | |
377 | ||
ebc8dd52 C |
378 | const autoplay = this.getAutoPlayValue(commonOptions.autoplay) === 'play' |
379 | ? true | |
380 | : false | |
381 | ||
39aad8cc | 382 | const webtorrent = { |
ebc8dd52 | 383 | autoplay, |
39aad8cc C |
384 | videoDuration: commonOptions.videoDuration, |
385 | playerElement: commonOptions.playerElement, | |
386 | videoFiles: webtorrentOptions.videoFiles, | |
387 | startTime: commonOptions.startTime | |
2adfc7ea C |
388 | } |
389 | ||
39aad8cc | 390 | Object.assign(plugins, { webtorrent }) |
2adfc7ea C |
391 | } |
392 | ||
393 | private static getControlBarChildren (mode: PlayerMode, options: { | |
394 | peertubeLink: boolean | |
a950e4c8 C |
395 | theaterButton: boolean |
396 | captions: boolean | |
397 | ||
1dc240a9 | 398 | nextVideo?: Function |
a950e4c8 C |
399 | hasNextVideo?: () => boolean |
400 | ||
401 | previousVideo?: Function | |
402 | hasPreviousVideo?: () => boolean | |
2adfc7ea C |
403 | }) { |
404 | const settingEntries = [] | |
405 | const loadProgressBar = mode === 'webtorrent' ? 'peerTubeLoadProgressBar' : 'loadProgressBar' | |
406 | ||
407 | // Keep an order | |
408 | settingEntries.push('playbackRateMenuButton') | |
409 | if (options.captions === true) settingEntries.push('captionsButton') | |
410 | settingEntries.push('resolutionMenuButton') | |
411 | ||
a950e4c8 C |
412 | const children = {} |
413 | ||
414 | if (options.previousVideo) { | |
415 | const buttonOptions: NextPreviousVideoButtonOptions = { | |
416 | type: 'previous', | |
417 | handler: options.previousVideo, | |
418 | isDisabled: () => { | |
419 | if (!options.hasPreviousVideo) return false | |
420 | ||
421 | return !options.hasPreviousVideo() | |
422 | } | |
423 | } | |
424 | ||
425 | Object.assign(children, { | |
426 | 'previousVideoButton': buttonOptions | |
427 | }) | |
1dc240a9 RK |
428 | } |
429 | ||
a950e4c8 C |
430 | Object.assign(children, { playToggle: {} }) |
431 | ||
1dc240a9 | 432 | if (options.nextVideo) { |
a950e4c8 C |
433 | const buttonOptions: NextPreviousVideoButtonOptions = { |
434 | type: 'next', | |
435 | handler: options.nextVideo, | |
436 | isDisabled: () => { | |
437 | if (!options.hasNextVideo) return false | |
438 | ||
439 | return !options.hasNextVideo() | |
1dc240a9 | 440 | } |
a950e4c8 C |
441 | } |
442 | ||
443 | Object.assign(children, { | |
444 | 'nextVideoButton': buttonOptions | |
1dc240a9 RK |
445 | }) |
446 | } | |
447 | ||
448 | Object.assign(children, { | |
2adfc7ea C |
449 | 'currentTimeDisplay': {}, |
450 | 'timeDivider': {}, | |
451 | 'durationDisplay': {}, | |
452 | 'liveDisplay': {}, | |
453 | ||
454 | 'flexibleWidthSpacer': {}, | |
455 | 'progressControl': { | |
456 | children: { | |
457 | 'seekBar': { | |
458 | children: { | |
459 | [loadProgressBar]: {}, | |
460 | 'mouseTimeDisplay': {}, | |
461 | 'playProgressBar': {} | |
462 | } | |
463 | } | |
464 | } | |
465 | }, | |
466 | ||
467 | 'p2PInfoButton': {}, | |
468 | ||
469 | 'muteToggle': {}, | |
470 | 'volumeControl': {}, | |
471 | ||
472 | 'settingsButton': { | |
473 | setup: { | |
474 | maxHeightOffset: 40 | |
475 | }, | |
476 | entries: settingEntries | |
477 | } | |
1dc240a9 | 478 | }) |
2adfc7ea C |
479 | |
480 | if (options.peertubeLink === true) { | |
481 | Object.assign(children, { | |
482 | 'peerTubeLinkButton': {} | |
483 | }) | |
484 | } | |
485 | ||
3d9a63d3 | 486 | if (options.theaterButton === true) { |
2adfc7ea C |
487 | Object.assign(children, { |
488 | 'theaterButton': {} | |
489 | }) | |
490 | } | |
491 | ||
492 | Object.assign(children, { | |
493 | 'fullscreenToggle': {} | |
494 | }) | |
495 | ||
496 | return children | |
497 | } | |
498 | ||
4097c6d6 | 499 | private static addContextMenu (mode: PlayerMode, player: videojs.Player, videoEmbedUrl: string, videoEmbedTitle: string) { |
2adfc7ea C |
500 | const content = [ |
501 | { | |
502 | label: player.localize('Copy the video URL'), | |
503 | listener: function () { | |
504 | copyToClipboard(buildVideoLink()) | |
505 | } | |
506 | }, | |
507 | { | |
508 | label: player.localize('Copy the video URL at the current time'), | |
7e37e111 | 509 | listener: function (this: videojs.Player) { |
f5fcd9f7 | 510 | copyToClipboard(buildVideoLink({ startTime: this.currentTime() })) |
2adfc7ea C |
511 | } |
512 | }, | |
513 | { | |
514 | label: player.localize('Copy embed code'), | |
515 | listener: () => { | |
4097c6d6 | 516 | copyToClipboard(buildVideoOrPlaylistEmbed(videoEmbedUrl, videoEmbedTitle)) |
2adfc7ea C |
517 | } |
518 | } | |
519 | ] | |
520 | ||
521 | if (mode === 'webtorrent') { | |
522 | content.push({ | |
523 | label: player.localize('Copy magnet URI'), | |
7e37e111 | 524 | listener: function (this: videojs.Player) { |
f5fcd9f7 | 525 | copyToClipboard(this.webtorrent().getCurrentVideoFile().magnetUri) |
2adfc7ea C |
526 | } |
527 | }) | |
528 | } | |
529 | ||
530 | player.contextmenuUI({ content }) | |
531 | } | |
532 | ||
39aad8cc | 533 | private static addHotkeysOptions (plugins: VideoJSPluginOptions) { |
e0b59721 | 534 | const isNaked = (event: KeyboardEvent, key: string) => |
535 | (!event.ctrlKey && !event.altKey && !event.metaKey && !event.shiftKey && event.key === key) | |
536 | ||
39aad8cc C |
537 | Object.assign(plugins, { |
538 | hotkeys: { | |
7ede74ad C |
539 | skipInitialFocus: true, |
540 | enableInactiveFocus: false, | |
541 | captureDocumentHotkeys: true, | |
542 | documentHotkeysFocusElementFilter: (e: HTMLElement) => { | |
e85bfe96 C |
543 | const tagName = e.tagName.toLowerCase() |
544 | return e.id === 'content' || tagName === 'body' || tagName === 'video' | |
7ede74ad C |
545 | }, |
546 | ||
39aad8cc C |
547 | enableVolumeScroll: false, |
548 | enableModifiersForNumbers: false, | |
549 | ||
684cdacb | 550 | rewindKey: function (event: KeyboardEvent) { |
551 | return isNaked(event, 'ArrowLeft') | |
552 | }, | |
553 | ||
554 | forwardKey: function (event: KeyboardEvent) { | |
555 | return isNaked(event, 'ArrowRight') | |
556 | }, | |
557 | ||
39aad8cc C |
558 | fullscreenKey: function (event: KeyboardEvent) { |
559 | // fullscreen with the f key or Ctrl+Enter | |
e0b59721 | 560 | return isNaked(event, 'f') || (!event.altKey && event.ctrlKey && event.key === 'Enter') |
39aad8cc C |
561 | }, |
562 | ||
39aad8cc C |
563 | customKeys: { |
564 | increasePlaybackRateKey: { | |
565 | key: function (event: KeyboardEvent) { | |
e0b59721 | 566 | return isNaked(event, '>') |
39aad8cc C |
567 | }, |
568 | handler: function (player: videojs.Player) { | |
f5fcd9f7 C |
569 | const newValue = Math.min(player.playbackRate() + 0.1, 5) |
570 | player.playbackRate(parseFloat(newValue.toFixed(2))) | |
39aad8cc C |
571 | } |
572 | }, | |
573 | decreasePlaybackRateKey: { | |
574 | key: function (event: KeyboardEvent) { | |
e0b59721 | 575 | return isNaked(event, '<') |
39aad8cc C |
576 | }, |
577 | handler: function (player: videojs.Player) { | |
f5fcd9f7 C |
578 | const newValue = Math.max(player.playbackRate() - 0.1, 0.10) |
579 | player.playbackRate(parseFloat(newValue.toFixed(2))) | |
39aad8cc C |
580 | } |
581 | }, | |
582 | frameByFrame: { | |
583 | key: function (event: KeyboardEvent) { | |
e0b59721 | 584 | return isNaked(event, '.') |
39aad8cc C |
585 | }, |
586 | handler: function (player: videojs.Player) { | |
587 | player.pause() | |
588 | // Calculate movement distance (assuming 30 fps) | |
589 | const dist = 1 / 30 | |
590 | player.currentTime(player.currentTime() + dist) | |
591 | } | |
592 | } | |
593 | } | |
594 | } | |
595 | }) | |
596 | } | |
64228474 | 597 | |
72efdda5 C |
598 | private static getAutoPlayValue (autoplay: any) { |
599 | if (autoplay !== true) return autoplay | |
600 | ||
ebc8dd52 C |
601 | // On first play, disable autoplay to avoid issues |
602 | // But if the player already played videos, we can safely autoplay next ones | |
603 | if (isIOS() || isSafari()) { | |
9eccae74 C |
604 | return PeertubePlayerManager.alreadyPlayed ? 'play' : false |
605 | } | |
64228474 C |
606 | |
607 | return 'play' | |
608 | } | |
2adfc7ea C |
609 | } |
610 | ||
611 | // ############################################################################ | |
612 | ||
613 | export { | |
614 | videojs | |
615 | } |