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