]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/standalone/videos/shared/player-manager-options.ts
Add ability to use docker on local
[github/Chocobozzz/PeerTube.git] / client / src / standalone / videos / shared / player-manager-options.ts
CommitLineData
f1a0f3b7
C
1import { peertubeTranslate } from '../../../../../shared/core-utils/i18n'
2import {
3 HTMLServerConfig,
4 LiveVideo,
5 Video,
6 VideoCaption,
7 VideoDetails,
8 VideoPlaylistElement,
9 VideoStreamingPlaylistType
10} from '../../../../../shared/models'
11import { P2PMediaLoaderOptions, PeertubePlayerManagerOptions, PlayerMode, VideoJSCaption } from '../../../assets/player'
12import {
13 getBoolOrDefault,
14 getParamString,
15 getParamToggle,
16 isP2PEnabled,
42b40636 17 logger,
f1a0f3b7
C
18 peertubeLocalStorage,
19 UserLocalStorageKeys
20} from '../../../root-helpers'
21import { PeerTubePlugin } from './peertube-plugin'
22import { PlayerHTML } from './player-html'
23import { PlaylistTracker } from './playlist-tracker'
24import { Translations } from './translations'
25import { VideoFetcher } from './video-fetcher'
26
27export class PlayerManagerOptions {
28 private autoplay: boolean
29
30 private controls: boolean
31 private controlBar: boolean
32
33 private muted: boolean
34 private loop: boolean
35 private subtitle: string
36 private enableApi = false
37 private startTime: number | string = 0
38 private stopTime: number | string
39
40 private title: boolean
41 private warningTitle: boolean
42 private peertubeLink: boolean
43 private p2pEnabled: boolean
44 private bigPlayBackgroundColor: string
45 private foregroundColor: string
46
47 private mode: PlayerMode
48 private scope = 'peertube'
49
50 constructor (
51 private readonly playerHTML: PlayerHTML,
52 private readonly videoFetcher: VideoFetcher,
53 private readonly peertubePlugin: PeerTubePlugin
54 ) {}
55
56 hasAPIEnabled () {
57 return this.enableApi
58 }
59
60 hasAutoplay () {
61 return this.autoplay
62 }
63
64 hasControls () {
65 return this.controls
66 }
67
68 hasTitle () {
69 return this.title
70 }
71
72 hasWarningTitle () {
73 return this.warningTitle
74 }
75
76 hasP2PEnabled () {
77 return !!this.p2pEnabled
78 }
79
80 hasBigPlayBackgroundColor () {
81 return !!this.bigPlayBackgroundColor
82 }
83
84 getBigPlayBackgroundColor () {
85 return this.bigPlayBackgroundColor
86 }
87
88 hasForegroundColor () {
89 return !!this.foregroundColor
90 }
91
92 getForegroundColor () {
93 return this.foregroundColor
94 }
95
96 getMode () {
97 return this.mode
98 }
99
100 getScope () {
101 return this.scope
102 }
103
104 // ---------------------------------------------------------------------------
105
106 loadParams (config: HTMLServerConfig, video: VideoDetails) {
107 try {
108 const params = new URL(window.location.toString()).searchParams
109
110 this.autoplay = getParamToggle(params, 'autoplay', false)
111
112 this.controls = getParamToggle(params, 'controls', true)
113 this.controlBar = getParamToggle(params, 'controlBar', true)
114
115 this.muted = getParamToggle(params, 'muted', undefined)
116 this.loop = getParamToggle(params, 'loop', false)
117 this.title = getParamToggle(params, 'title', true)
118 this.enableApi = getParamToggle(params, 'api', this.enableApi)
119 this.warningTitle = getParamToggle(params, 'warningTitle', true)
120 this.peertubeLink = getParamToggle(params, 'peertubeLink', true)
121 this.p2pEnabled = getParamToggle(params, 'p2p', this.isP2PEnabled(config, video))
122
123 this.scope = getParamString(params, 'scope', this.scope)
124 this.subtitle = getParamString(params, 'subtitle')
125 this.startTime = getParamString(params, 'start')
126 this.stopTime = getParamString(params, 'stop')
127
128 this.bigPlayBackgroundColor = getParamString(params, 'bigPlayBackgroundColor')
129 this.foregroundColor = getParamString(params, 'foregroundColor')
130
131 const modeParam = getParamString(params, 'mode')
132
133 if (modeParam) {
134 if (modeParam === 'p2p-media-loader') this.mode = 'p2p-media-loader'
135 else this.mode = 'webtorrent'
136 } else {
137 if (Array.isArray(video.streamingPlaylists) && video.streamingPlaylists.length !== 0) this.mode = 'p2p-media-loader'
138 else this.mode = 'webtorrent'
139 }
140 } catch (err) {
42b40636 141 logger.error('Cannot get params from URL.', err)
f1a0f3b7
C
142 }
143 }
144
145 // ---------------------------------------------------------------------------
146
147 async getPlayerOptions (options: {
148 video: VideoDetails
149 captionsResponse: Response
150 live?: LiveVideo
151
bd2b51be
C
152 serverConfig: HTMLServerConfig
153
f1a0f3b7
C
154 alreadyHadPlayer: boolean
155
156 translations: Translations
157
158 playlistTracker?: PlaylistTracker
159 playNextPlaylistVideo?: () => any
160 playPreviousPlaylistVideo?: () => any
161 onVideoUpdate?: (uuid: string) => any
162 }) {
163 const {
164 video,
165 captionsResponse,
166 alreadyHadPlayer,
167 translations,
168 playlistTracker,
bd2b51be
C
169 live,
170 serverConfig
f1a0f3b7
C
171 } = options
172
173 const videoCaptions = await this.buildCaptions(captionsResponse, translations)
174
175 const playerOptions: PeertubePlayerManagerOptions = {
176 common: {
177 // Autoplay in playlist mode
178 autoplay: alreadyHadPlayer ? true : this.autoplay,
179
180 controls: this.controls,
181 controlBar: this.controlBar,
182
183 muted: this.muted,
184 loop: this.loop,
185
186 p2pEnabled: this.p2pEnabled,
187
188 captions: videoCaptions.length !== 0,
189 subtitle: this.subtitle,
190
191 startTime: playlistTracker
192 ? playlistTracker.getCurrentElement().startTimestamp
193 : this.startTime,
194 stopTime: playlistTracker
195 ? playlistTracker.getCurrentElement().stopTimestamp
196 : this.stopTime,
197
198 videoCaptions,
199 inactivityTimeout: 2500,
200 videoViewUrl: this.videoFetcher.getVideoViewsUrl(video.uuid),
201
202 videoShortUUID: video.shortUUID,
203 videoUUID: video.uuid,
204
205 playerElement: this.playerHTML.getPlayerElement(),
206 onPlayerElementChange: (element: HTMLVideoElement) => {
207 this.playerHTML.setPlayerElement(element)
208 },
209
210 videoDuration: video.duration,
211 enableHotkeys: true,
bd2b51be 212
f1a0f3b7 213 peertubeLink: this.peertubeLink,
bd2b51be
C
214 instanceName: serverConfig.instance.name,
215
f1a0f3b7
C
216 poster: window.location.origin + video.previewPath,
217 theaterButton: false,
218
219 serverUrl: window.location.origin,
220 language: navigator.language,
221 embedUrl: window.location.origin + video.embedPath,
222 embedTitle: video.name,
223
224 errorNotifier: () => {
225 // Empty, we don't have a notifier in the embed
226 },
227
228 ...this.buildLiveOptions(video, live),
229
230 ...this.buildPlaylistOptions(options)
231 },
232
233 webtorrent: {
234 videoFiles: video.files
235 },
236
237 ...this.buildP2PMediaLoaderOptions(video),
238
239 pluginsManager: this.peertubePlugin.getPluginsManager()
240 }
241
242 return playerOptions
243 }
244
245 private buildLiveOptions (video: VideoDetails, live: LiveVideo) {
246 if (!video.isLive) return { isLive: false }
247
248 return {
249 isLive: true,
250 liveOptions: {
251 latencyMode: live.latencyMode
252 }
253 }
254 }
255
256 private buildPlaylistOptions (options: {
257 playlistTracker?: PlaylistTracker
258 playNextPlaylistVideo?: () => any
259 playPreviousPlaylistVideo?: () => any
260 onVideoUpdate?: (uuid: string) => any
261 }) {
262 const { playlistTracker, playNextPlaylistVideo, playPreviousPlaylistVideo, onVideoUpdate } = options
263
264 if (!playlistTracker) return {}
265
266 return {
267 playlist: {
268 elements: playlistTracker.getPlaylistElements(),
269 playlist: playlistTracker.getPlaylist(),
270
271 getCurrentPosition: () => playlistTracker.getCurrentPosition(),
272
273 onItemClicked: (videoPlaylistElement: VideoPlaylistElement) => {
274 playlistTracker.setCurrentElement(videoPlaylistElement)
275
276 onVideoUpdate(videoPlaylistElement.video.uuid)
277 }
278 },
279
280 nextVideo: () => playNextPlaylistVideo(),
281 hasNextVideo: () => playlistTracker.hasNextPlaylistElement(),
282
283 previousVideo: () => playPreviousPlaylistVideo(),
284 hasPreviousVideo: () => playlistTracker.hasPreviousPlaylistElement()
285 }
286 }
287
288 private buildP2PMediaLoaderOptions (video: VideoDetails) {
289 if (this.mode !== 'p2p-media-loader') return {}
290
291 const hlsPlaylist = video.streamingPlaylists.find(p => p.type === VideoStreamingPlaylistType.HLS)
292
293 return {
294 p2pMediaLoader: {
295 playlistUrl: hlsPlaylist.playlistUrl,
296 segmentsSha256Url: hlsPlaylist.segmentsSha256Url,
297 redundancyBaseUrls: hlsPlaylist.redundancies.map(r => r.baseUrl),
298 trackerAnnounce: video.trackerUrls,
299 videoFiles: hlsPlaylist.files
300 } as P2PMediaLoaderOptions
301 }
302 }
303
304 // ---------------------------------------------------------------------------
305
306 private async buildCaptions (captionsResponse: Response, translations: Translations): Promise<VideoJSCaption[]> {
307 if (captionsResponse.ok) {
308 const { data } = await captionsResponse.json()
309
310 return data.map((c: VideoCaption) => ({
311 label: peertubeTranslate(c.language.label, translations),
312 language: c.language.id,
313 src: window.location.origin + c.captionPath
314 }))
315 }
316
317 return []
318 }
319
320 // ---------------------------------------------------------------------------
321
322 private isP2PEnabled (config: HTMLServerConfig, video: Video) {
323 const userP2PEnabled = getBoolOrDefault(
324 peertubeLocalStorage.getItem(UserLocalStorageKeys.P2P_ENABLED),
325 config.defaults.p2p.embed.enabled
326 )
327
328 return isP2PEnabled(video, config, userP2PEnabled)
329 }
330}