1 import videojs from 'video.js'
2 import { copyToClipboard } from '@root-helpers/utils'
3 import { buildVideoOrPlaylistEmbed } from '@root-helpers/video'
4 import { isIOS, isSafari } from '@root-helpers/web-browser'
5 import { buildVideoLink, decorateVideoLink, pick } from '@shared/core-utils'
6 import { isDefaultLocale } from '@shared/core-utils/i18n'
7 import { VideoJSPluginOptions } from '../../types'
8 import { CommonOptions, PeertubePlayerManagerOptions, PlayerMode } from '../../types/manager-options'
9 import { ControlBarOptionsBuilder } from './control-bar-options-builder'
10 import { HLSOptionsBuilder } from './hls-options-builder'
11 import { WebTorrentOptionsBuilder } from './webtorrent-options-builder'
13 export class ManagerOptionsBuilder {
16 private mode: PlayerMode,
17 private options: PeertubePlayerManagerOptions,
18 private p2pMediaLoaderModule?: any
23 async getVideojsOptions (alreadyPlayed: boolean): Promise<videojs.PlayerOptions> {
24 const commonOptions = this.options.common
26 let autoplay = this.getAutoPlayValue(commonOptions.autoplay, alreadyPlayed)
28 preloadTextTracks: false
31 const plugins: VideoJSPluginOptions = {
34 autoplay, // Use peertube plugin autoplay because we could get the file by webtorrent
36 ...pick(commonOptions, [
38 'authorizationHeader',
51 ...pick(commonOptions, [
58 if (commonOptions.playlist) {
59 plugins.playlist = commonOptions.playlist
62 if (this.mode === 'p2p-media-loader') {
63 const hlsOptionsBuilder = new HLSOptionsBuilder(this.options, this.p2pMediaLoaderModule)
64 const options = await hlsOptionsBuilder.getPluginOptions()
66 Object.assign(plugins, pick(options, [ 'hlsjs', 'p2pMediaLoader' ]))
67 Object.assign(html5, options.html5)
68 } else if (this.mode === 'webtorrent') {
69 const webtorrentOptionsBuilder = new WebTorrentOptionsBuilder(this.options, this.getAutoPlayValue(autoplay, alreadyPlayed))
71 Object.assign(plugins, webtorrentOptionsBuilder.getPluginOptions())
73 // WebTorrent plugin handles autoplay, because we do some hackish stuff in there
77 const controlBarOptionsBuilder = new ControlBarOptionsBuilder(this.options, this.mode)
79 const videojsOptions = {
82 // We don't use text track settings for now
83 textTrackSettings: false as any, // FIXME: typings
84 controls: commonOptions.controls !== undefined ? commonOptions.controls : true,
85 loop: commonOptions.loop !== undefined ? commonOptions.loop : false,
87 muted: commonOptions.muted !== undefined
89 : undefined, // Undefined so the player knows it has to check the local storage
91 autoplay: this.getAutoPlayValue(autoplay, alreadyPlayed),
93 poster: commonOptions.poster,
94 inactivityTimeout: commonOptions.inactivityTimeout,
95 playbackRates: [ 0.5, 0.75, 1, 1.25, 1.5, 1.75, 2 ],
100 children: controlBarOptionsBuilder.getChildrenOptions() as any // FIXME: typings
104 if (commonOptions.language && !isDefaultLocale(commonOptions.language)) {
105 Object.assign(videojsOptions, { language: commonOptions.language })
108 return videojsOptions
111 private getAutoPlayValue (autoplay: videojs.Autoplay, alreadyPlayed: boolean) {
112 if (autoplay !== true) return autoplay
114 // On first play, disable autoplay to avoid issues
115 // But if the player already played videos, we can safely autoplay next ones
116 if (isIOS() || isSafari()) {
117 return alreadyPlayed ? 'play' : false
120 return this.options.common.forceAutoplay
125 getContextMenuOptions (player: videojs.Player, commonOptions: CommonOptions) {
126 const content = () => {
127 const isLoopEnabled = player.options_['loop']
132 label: player.localize('Play in loop') + (isLoopEnabled ? '<span class="vjs-icon-tick-white"></span>' : ''),
133 listener: function () {
134 player.options_['loop'] = !isLoopEnabled
138 label: player.localize('Copy the video URL'),
139 listener: function () {
140 copyToClipboard(buildVideoLink({ shortUUID: commonOptions.videoShortUUID }))
144 label: player.localize('Copy the video URL at the current time'),
145 listener: function (this: videojs.Player) {
146 const url = buildVideoLink({ shortUUID: commonOptions.videoShortUUID })
148 copyToClipboard(decorateVideoLink({ url, startTime: this.currentTime() }))
153 label: player.localize('Copy embed code'),
155 copyToClipboard(buildVideoOrPlaylistEmbed({ embedUrl: commonOptions.embedUrl, embedTitle: commonOptions.embedTitle }))
160 if (this.mode === 'webtorrent') {
162 label: player.localize('Copy magnet URI'),
163 listener: function (this: videojs.Player) {
164 copyToClipboard(this.webtorrent().getCurrentVideoFile().magnetUri)
171 label: player.localize('Stats for nerds'),
173 player.stats().show()
177 return items.map(i => ({
179 label: `<span class="vjs-icon-${i.icon || 'link-2'}"></span>` + i.label