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