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