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