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