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