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