]>
Commit | Line | Data |
---|---|---|
9597920e | 1 | import videojs from 'video.js' |
57d65032 C |
2 | import { copyToClipboard } from '@root-helpers/utils' |
3 | import { buildVideoOrPlaylistEmbed } from '@root-helpers/video' | |
4 | import { isIOS, isSafari } from '@root-helpers/web-browser' | |
e71e2d8a | 5 | import { buildVideoLink, decorateVideoLink, pick } from '@shared/core-utils' |
9597920e | 6 | import { isDefaultLocale } from '@shared/core-utils/i18n' |
57d65032 C |
7 | import { VideoJSPluginOptions } from '../../types' |
8 | import { CommonOptions, PeertubePlayerManagerOptions, PlayerMode } from '../../types/manager-options' | |
9597920e C |
9 | import { ControlBarOptionsBuilder } from './control-bar-options-builder' |
10 | import { HLSOptionsBuilder } from './hls-options-builder' | |
9597920e C |
11 | import { WebTorrentOptionsBuilder } from './webtorrent-options-builder' |
12 | ||
13 | export class ManagerOptionsBuilder { | |
14 | ||
15 | constructor ( | |
16 | private mode: PlayerMode, | |
17 | private options: PeertubePlayerManagerOptions, | |
18 | private p2pMediaLoaderModule?: any | |
19 | ) { | |
20 | ||
21 | } | |
22 | ||
9866921c | 23 | async getVideojsOptions (alreadyPlayed: boolean): Promise<videojs.PlayerOptions> { |
9597920e C |
24 | const commonOptions = this.options.common |
25 | ||
26 | let autoplay = this.getAutoPlayValue(commonOptions.autoplay, alreadyPlayed) | |
27 | const html5 = { | |
28 | preloadTextTracks: false | |
29 | } | |
30 | ||
31 | const plugins: VideoJSPluginOptions = { | |
32 | peertube: { | |
33 | mode: this.mode, | |
34 | autoplay, // Use peertube plugin autoplay because we could get the file by webtorrent | |
384ba8b7 C |
35 | |
36 | ...pick(commonOptions, [ | |
37 | 'videoViewUrl', | |
38 | 'authorizationHeader', | |
39 | 'startTime', | |
40 | 'videoDuration', | |
41 | 'subtitle', | |
42 | 'videoCaptions', | |
43 | 'stopTime', | |
44 | 'isLive', | |
45 | 'videoUUID' | |
46 | ]) | |
fd3c2e87 C |
47 | }, |
48 | metrics: { | |
49 | mode: this.mode, | |
50 | ||
51 | ...pick(commonOptions, [ | |
52 | 'metricsUrl', | |
53 | 'videoUUID' | |
54 | ]) | |
9597920e C |
55 | } |
56 | } | |
57 | ||
58 | if (commonOptions.playlist) { | |
59 | plugins.playlist = commonOptions.playlist | |
60 | } | |
61 | ||
62 | if (this.mode === 'p2p-media-loader') { | |
63 | const hlsOptionsBuilder = new HLSOptionsBuilder(this.options, this.p2pMediaLoaderModule) | |
9866921c | 64 | const options = await hlsOptionsBuilder.getPluginOptions() |
9597920e | 65 | |
e71e2d8a C |
66 | Object.assign(plugins, pick(options, [ 'hlsjs', 'p2pMediaLoader' ])) |
67 | Object.assign(html5, options.html5) | |
9597920e C |
68 | } else if (this.mode === 'webtorrent') { |
69 | const webtorrentOptionsBuilder = new WebTorrentOptionsBuilder(this.options, this.getAutoPlayValue(autoplay, alreadyPlayed)) | |
70 | ||
71 | Object.assign(plugins, webtorrentOptionsBuilder.getPluginOptions()) | |
72 | ||
73 | // WebTorrent plugin handles autoplay, because we do some hackish stuff in there | |
74 | autoplay = false | |
75 | } | |
76 | ||
77 | const controlBarOptionsBuilder = new ControlBarOptionsBuilder(this.options, this.mode) | |
78 | ||
79 | const videojsOptions = { | |
80 | html5, | |
81 | ||
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, | |
86 | ||
87 | muted: commonOptions.muted !== undefined | |
88 | ? commonOptions.muted | |
89 | : undefined, // Undefined so the player knows it has to check the local storage | |
90 | ||
91 | autoplay: this.getAutoPlayValue(autoplay, alreadyPlayed), | |
92 | ||
93 | poster: commonOptions.poster, | |
94 | inactivityTimeout: commonOptions.inactivityTimeout, | |
95 | playbackRates: [ 0.5, 0.75, 1, 1.25, 1.5, 1.75, 2 ], | |
96 | ||
97 | plugins, | |
98 | ||
99 | controlBar: { | |
100 | children: controlBarOptionsBuilder.getChildrenOptions() as any // FIXME: typings | |
101 | } | |
102 | } | |
103 | ||
104 | if (commonOptions.language && !isDefaultLocale(commonOptions.language)) { | |
105 | Object.assign(videojsOptions, { language: commonOptions.language }) | |
106 | } | |
107 | ||
59a643aa C |
108 | console.log(videojsOptions) |
109 | ||
9597920e C |
110 | return videojsOptions |
111 | } | |
112 | ||
59a643aa | 113 | private getAutoPlayValue (autoplay: videojs.Autoplay, alreadyPlayed: boolean) { |
9597920e C |
114 | if (autoplay !== true) return autoplay |
115 | ||
116 | // On first play, disable autoplay to avoid issues | |
117 | // But if the player already played videos, we can safely autoplay next ones | |
118 | if (isIOS() || isSafari()) { | |
119 | return alreadyPlayed ? 'play' : false | |
120 | } | |
121 | ||
59a643aa C |
122 | return this.options.common.forceAutoplay |
123 | ? 'any' | |
124 | : 'play' | |
9597920e C |
125 | } |
126 | ||
127 | getContextMenuOptions (player: videojs.Player, commonOptions: CommonOptions) { | |
128 | const content = () => { | |
129 | const isLoopEnabled = player.options_['loop'] | |
130 | ||
131 | const items = [ | |
132 | { | |
133 | icon: 'repeat', | |
134 | label: player.localize('Play in loop') + (isLoopEnabled ? '<span class="vjs-icon-tick-white"></span>' : ''), | |
135 | listener: function () { | |
136 | player.options_['loop'] = !isLoopEnabled | |
137 | } | |
138 | }, | |
139 | { | |
140 | label: player.localize('Copy the video URL'), | |
141 | listener: function () { | |
142 | copyToClipboard(buildVideoLink({ shortUUID: commonOptions.videoShortUUID })) | |
143 | } | |
144 | }, | |
145 | { | |
146 | label: player.localize('Copy the video URL at the current time'), | |
147 | listener: function (this: videojs.Player) { | |
148 | const url = buildVideoLink({ shortUUID: commonOptions.videoShortUUID }) | |
149 | ||
150 | copyToClipboard(decorateVideoLink({ url, startTime: this.currentTime() })) | |
151 | } | |
152 | }, | |
153 | { | |
154 | icon: 'code', | |
155 | label: player.localize('Copy embed code'), | |
156 | listener: () => { | |
de615445 | 157 | copyToClipboard(buildVideoOrPlaylistEmbed({ embedUrl: commonOptions.embedUrl, embedTitle: commonOptions.embedTitle })) |
9597920e C |
158 | } |
159 | } | |
160 | ] | |
161 | ||
162 | if (this.mode === 'webtorrent') { | |
163 | items.push({ | |
164 | label: player.localize('Copy magnet URI'), | |
165 | listener: function (this: videojs.Player) { | |
166 | copyToClipboard(this.webtorrent().getCurrentVideoFile().magnetUri) | |
167 | } | |
168 | }) | |
169 | } | |
170 | ||
171 | items.push({ | |
172 | icon: 'info', | |
173 | label: player.localize('Stats for nerds'), | |
174 | listener: () => { | |
175 | player.stats().show() | |
176 | } | |
177 | }) | |
178 | ||
179 | return items.map(i => ({ | |
180 | ...i, | |
181 | label: `<span class="vjs-icon-${i.icon || 'link-2'}"></span>` + i.label | |
182 | })) | |
183 | } | |
184 | ||
185 | return { content } | |
186 | } | |
187 | } |