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