]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/assets/player/peertube-player-manager.ts
Fix player options
[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
05287a2e 404 : p2pMediaLoaderOptions?.videoFiles || [],
39aad8cc 405 startTime: commonOptions.startTime
2adfc7ea
C
406 }
407
39aad8cc 408 Object.assign(plugins, { webtorrent })
2adfc7ea
C
409 }
410
411 private static getControlBarChildren (mode: PlayerMode, options: {
412 peertubeLink: boolean
a950e4c8
C
413 theaterButton: boolean
414 captions: boolean
415
1dc240a9 416 nextVideo?: Function
a950e4c8
C
417 hasNextVideo?: () => boolean
418
419 previousVideo?: Function
420 hasPreviousVideo?: () => boolean
2adfc7ea
C
421 }) {
422 const settingEntries = []
423 const loadProgressBar = mode === 'webtorrent' ? 'peerTubeLoadProgressBar' : 'loadProgressBar'
424
425 // Keep an order
426 settingEntries.push('playbackRateMenuButton')
427 if (options.captions === true) settingEntries.push('captionsButton')
428 settingEntries.push('resolutionMenuButton')
429
a950e4c8
C
430 const children = {}
431
432 if (options.previousVideo) {
433 const buttonOptions: NextPreviousVideoButtonOptions = {
434 type: 'previous',
435 handler: options.previousVideo,
436 isDisabled: () => {
437 if (!options.hasPreviousVideo) return false
438
439 return !options.hasPreviousVideo()
440 }
441 }
442
443 Object.assign(children, {
444 'previousVideoButton': buttonOptions
445 })
1dc240a9
RK
446 }
447
a950e4c8
C
448 Object.assign(children, { playToggle: {} })
449
1dc240a9 450 if (options.nextVideo) {
a950e4c8
C
451 const buttonOptions: NextPreviousVideoButtonOptions = {
452 type: 'next',
453 handler: options.nextVideo,
454 isDisabled: () => {
455 if (!options.hasNextVideo) return false
456
457 return !options.hasNextVideo()
1dc240a9 458 }
a950e4c8
C
459 }
460
461 Object.assign(children, {
462 'nextVideoButton': buttonOptions
1dc240a9
RK
463 })
464 }
465
466 Object.assign(children, {
2adfc7ea
C
467 'currentTimeDisplay': {},
468 'timeDivider': {},
469 'durationDisplay': {},
470 'liveDisplay': {},
471
472 'flexibleWidthSpacer': {},
473 'progressControl': {
474 children: {
475 'seekBar': {
476 children: {
477 [loadProgressBar]: {},
478 'mouseTimeDisplay': {},
479 'playProgressBar': {}
480 }
481 }
482 }
483 },
484
485 'p2PInfoButton': {},
486
487 'muteToggle': {},
488 'volumeControl': {},
489
490 'settingsButton': {
491 setup: {
492 maxHeightOffset: 40
493 },
494 entries: settingEntries
495 }
1dc240a9 496 })
2adfc7ea
C
497
498 if (options.peertubeLink === true) {
499 Object.assign(children, {
500 'peerTubeLinkButton': {}
501 })
502 }
503
3d9a63d3 504 if (options.theaterButton === true) {
2adfc7ea
C
505 Object.assign(children, {
506 'theaterButton': {}
507 })
508 }
509
510 Object.assign(children, {
511 'fullscreenToggle': {}
512 })
513
514 return children
515 }
516
3e0e8d4a 517 private static addContextMenu (mode: PlayerMode, player: videojs.Player, videoEmbedUrl: string, videoEmbedTitle: string) {
a472cf03 518 const content = () => {
3e0e8d4a
C
519 const isLoopEnabled = player.options_['loop']
520 const items = [
521 {
522 icon: 'repeat',
523 label: player.localize('Play in loop') + (isLoopEnabled ? '<span class="vjs-icon-tick-white"></span>' : ''),
524 listener: function () {
525 player.options_['loop'] = !isLoopEnabled
a472cf03 526 }
3e0e8d4a
C
527 },
528 {
529 label: player.localize('Copy the video URL'),
530 listener: function () {
531 copyToClipboard(buildVideoLink())
532 }
533 },
534 {
535 label: player.localize('Copy the video URL at the current time'),
536 listener: function (this: videojs.Player) {
537 copyToClipboard(buildVideoLink({ startTime: this.currentTime() }))
538 }
539 },
540 {
541 icon: 'code',
542 label: player.localize('Copy embed code'),
543 listener: () => {
544 copyToClipboard(buildVideoOrPlaylistEmbed(videoEmbedUrl, videoEmbedTitle))
a472cf03 545 }
2adfc7ea 546 }
3e0e8d4a 547 ]
a472cf03
RK
548
549 if (mode === 'webtorrent') {
550 items.push({
551 label: player.localize('Copy magnet URI'),
552 listener: function (this: videojs.Player) {
553 copyToClipboard(this.webtorrent().getCurrentVideoFile().magnetUri)
554 }
555 })
2adfc7ea 556 }
2adfc7ea 557
ff563914
RK
558 items.push({
559 icon: 'info',
560 label: player.localize('Stats for nerds'),
561 listener: () => {
562 player.stats().show()
563 }
564 })
565
83ff5481
RK
566 return items.map(i => ({
567 ...i,
568 label: `<span class="vjs-icon-${i.icon || 'link-2'}"></span>` + i.label
569 }))
2adfc7ea
C
570 }
571
a472cf03 572 // adding the menu
2adfc7ea
C
573 player.contextmenuUI({ content })
574 }
575
39aad8cc 576 private static addHotkeysOptions (plugins: VideoJSPluginOptions) {
e0b59721 577 const isNaked = (event: KeyboardEvent, key: string) =>
578 (!event.ctrlKey && !event.altKey && !event.metaKey && !event.shiftKey && event.key === key)
579
39aad8cc
C
580 Object.assign(plugins, {
581 hotkeys: {
7ede74ad
C
582 skipInitialFocus: true,
583 enableInactiveFocus: false,
584 captureDocumentHotkeys: true,
585 documentHotkeysFocusElementFilter: (e: HTMLElement) => {
e85bfe96
C
586 const tagName = e.tagName.toLowerCase()
587 return e.id === 'content' || tagName === 'body' || tagName === 'video'
7ede74ad
C
588 },
589
39aad8cc
C
590 enableVolumeScroll: false,
591 enableModifiersForNumbers: false,
592
684cdacb 593 rewindKey: function (event: KeyboardEvent) {
594 return isNaked(event, 'ArrowLeft')
595 },
596
597 forwardKey: function (event: KeyboardEvent) {
598 return isNaked(event, 'ArrowRight')
599 },
600
39aad8cc
C
601 fullscreenKey: function (event: KeyboardEvent) {
602 // fullscreen with the f key or Ctrl+Enter
e0b59721 603 return isNaked(event, 'f') || (!event.altKey && event.ctrlKey && event.key === 'Enter')
39aad8cc
C
604 },
605
39aad8cc
C
606 customKeys: {
607 increasePlaybackRateKey: {
608 key: function (event: KeyboardEvent) {
e0b59721 609 return isNaked(event, '>')
39aad8cc
C
610 },
611 handler: function (player: videojs.Player) {
f5fcd9f7
C
612 const newValue = Math.min(player.playbackRate() + 0.1, 5)
613 player.playbackRate(parseFloat(newValue.toFixed(2)))
39aad8cc
C
614 }
615 },
616 decreasePlaybackRateKey: {
617 key: function (event: KeyboardEvent) {
e0b59721 618 return isNaked(event, '<')
39aad8cc
C
619 },
620 handler: function (player: videojs.Player) {
f5fcd9f7
C
621 const newValue = Math.max(player.playbackRate() - 0.1, 0.10)
622 player.playbackRate(parseFloat(newValue.toFixed(2)))
39aad8cc
C
623 }
624 },
625 frameByFrame: {
626 key: function (event: KeyboardEvent) {
e0b59721 627 return isNaked(event, '.')
39aad8cc
C
628 },
629 handler: function (player: videojs.Player) {
630 player.pause()
631 // Calculate movement distance (assuming 30 fps)
632 const dist = 1 / 30
633 player.currentTime(player.currentTime() + dist)
634 }
635 }
636 }
637 }
638 })
639 }
64228474 640
72efdda5
C
641 private static getAutoPlayValue (autoplay: any) {
642 if (autoplay !== true) return autoplay
643
ebc8dd52
C
644 // On first play, disable autoplay to avoid issues
645 // But if the player already played videos, we can safely autoplay next ones
646 if (isIOS() || isSafari()) {
9eccae74
C
647 return PeertubePlayerManager.alreadyPlayed ? 'play' : false
648 }
64228474
C
649
650 return 'play'
651 }
2adfc7ea
C
652}
653
654// ############################################################################
655
656export {
657 videojs
658}