]>
Commit | Line | Data |
---|---|---|
202e7223 | 1 | import './embed.scss' |
583eb04b | 2 | import videojs from 'video.js' |
a4ff3100 C |
3 | import { objectToUrlEncoded, peertubeLocalStorage } from '@root-helpers/index' |
4 | import { Tokens } from '@root-helpers/users' | |
bd45d503 | 5 | import { peertubeTranslate } from '../../../../shared/core-utils/i18n' |
3f9c4955 | 6 | import { |
3f9c4955 C |
7 | ResultList, |
8 | ServerConfig, | |
583eb04b C |
9 | UserRefreshToken, |
10 | VideoCaption, | |
4504f09f | 11 | VideoDetails, |
5abc96fc C |
12 | VideoPlaylist, |
13 | VideoPlaylistElement, | |
583eb04b C |
14 | VideoStreamingPlaylistType |
15 | } from '../../../../shared/models' | |
16 | import { P2PMediaLoaderOptions, PeertubePlayerManagerOptions, PlayerMode } from '../../assets/player/peertube-player-manager' | |
abb3097e | 17 | import { VideoJSCaption } from '../../assets/player/peertube-videojs-typings' |
583eb04b C |
18 | import { TranslationsManager } from '../../assets/player/translations-manager' |
19 | import { PeerTubeEmbedApi } from './embed-api' | |
abb3097e C |
20 | |
21 | type Translations = { [ id: string ]: string } | |
202e7223 | 22 | |
5efab546 | 23 | export class PeerTubeEmbed { |
5abc96fc | 24 | playerElement: HTMLVideoElement |
7e37e111 | 25 | player: videojs.Player |
902aa3a0 | 26 | api: PeerTubeEmbedApi = null |
5abc96fc | 27 | |
3b019808 C |
28 | autoplay: boolean |
29 | controls: boolean | |
30 | muted: boolean | |
31 | loop: boolean | |
32 | subtitle: string | |
902aa3a0 | 33 | enableApi = false |
1f6824c9 | 34 | startTime: number | string = 0 |
f0a39880 | 35 | stopTime: number | string |
5efab546 C |
36 | |
37 | title: boolean | |
38 | warningTitle: boolean | |
08d9ba0f | 39 | peertubeLink: boolean |
5efab546 C |
40 | bigPlayBackgroundColor: string |
41 | foregroundColor: string | |
42 | ||
3b6f205c | 43 | mode: PlayerMode |
902aa3a0 C |
44 | scope = 'peertube' |
45 | ||
a4ff3100 | 46 | userTokens: Tokens |
71ab65d0 | 47 | headers = new Headers() |
4504f09f RK |
48 | LOCAL_STORAGE_OAUTH_CLIENT_KEYS = { |
49 | CLIENT_ID: 'client_id', | |
50 | CLIENT_SECRET: 'client_secret' | |
51 | } | |
71ab65d0 | 52 | |
5abc96fc C |
53 | private translationsPromise: Promise<{ [id: string]: string }> |
54 | private configPromise: Promise<ServerConfig> | |
55 | private PeertubePlayerManagerModulePromise: Promise<any> | |
56 | ||
57 | private playlist: VideoPlaylist | |
58 | private playlistElements: VideoPlaylistElement[] | |
59 | private currentPlaylistElement: VideoPlaylistElement | |
60 | ||
61 | private wrapperElement: HTMLElement | |
62 | ||
902aa3a0 | 63 | static async main () { |
5abc96fc | 64 | const videoContainerId = 'video-wrapper' |
99941732 WL |
65 | const embed = new PeerTubeEmbed(videoContainerId) |
66 | await embed.init() | |
67 | } | |
902aa3a0 | 68 | |
5abc96fc C |
69 | constructor (private videoWrapperId: string) { |
70 | this.wrapperElement = document.getElementById(this.videoWrapperId) | |
902aa3a0 C |
71 | } |
72 | ||
99941732 WL |
73 | getVideoUrl (id: string) { |
74 | return window.location.origin + '/api/v1/videos/' + id | |
75 | } | |
d4f3fea6 | 76 | |
4504f09f RK |
77 | refreshFetch (url: string, options?: Object) { |
78 | return fetch(url, options) | |
79 | .then((res: Response) => { | |
80 | if (res.status !== 401) return res | |
81 | ||
82 | // 401 unauthorized is not catch-ed, but then-ed | |
83 | const error = res | |
84 | ||
85 | const refreshingTokenPromise = new Promise((resolve, reject) => { | |
86 | const clientId: string = peertubeLocalStorage.getItem(this.LOCAL_STORAGE_OAUTH_CLIENT_KEYS.CLIENT_ID) | |
87 | const clientSecret: string = peertubeLocalStorage.getItem(this.LOCAL_STORAGE_OAUTH_CLIENT_KEYS.CLIENT_SECRET) | |
88 | const headers = new Headers() | |
89 | headers.set('Content-Type', 'application/x-www-form-urlencoded') | |
90 | const data = { | |
a4ff3100 | 91 | refresh_token: this.userTokens.refreshToken, |
4504f09f RK |
92 | client_id: clientId, |
93 | client_secret: clientSecret, | |
94 | response_type: 'code', | |
95 | grant_type: 'refresh_token' | |
96 | } | |
97 | ||
98 | fetch('/api/v1/users/token', { | |
99 | headers, | |
100 | method: 'POST', | |
101 | body: objectToUrlEncoded(data) | |
102 | }) | |
103 | .then(res => res.json()) | |
104 | .then((obj: UserRefreshToken) => { | |
a4ff3100 C |
105 | this.userTokens.accessToken = obj.access_token |
106 | this.userTokens.refreshToken = obj.refresh_token | |
107 | this.userTokens.save() | |
108 | ||
109 | this.setHeadersFromTokens() | |
110 | ||
4504f09f RK |
111 | resolve() |
112 | }) | |
113 | .catch((refreshTokenError: any) => { | |
114 | reject(refreshTokenError) | |
115 | }) | |
116 | }) | |
117 | ||
118 | return refreshingTokenPromise | |
119 | .catch(() => { | |
120 | // If refreshing fails, continue with original error | |
121 | throw error | |
122 | }) | |
123 | .then(() => fetch(url, { | |
124 | ...options, | |
125 | headers: this.headers | |
126 | })) | |
127 | }) | |
128 | } | |
129 | ||
5abc96fc C |
130 | getPlaylistUrl (id: string) { |
131 | return window.location.origin + '/api/v1/video-playlists/' + id | |
132 | } | |
133 | ||
99941732 | 134 | loadVideoInfo (videoId: string): Promise<Response> { |
4504f09f | 135 | return this.refreshFetch(this.getVideoUrl(videoId), { headers: this.headers }) |
99941732 | 136 | } |
d4f3fea6 | 137 | |
16f7022b | 138 | loadVideoCaptions (videoId: string): Promise<Response> { |
4504f09f | 139 | return fetch(this.getVideoUrl(videoId) + '/captions') |
16f7022b C |
140 | } |
141 | ||
5abc96fc C |
142 | loadPlaylistInfo (playlistId: string): Promise<Response> { |
143 | return fetch(this.getPlaylistUrl(playlistId)) | |
144 | } | |
145 | ||
146 | loadPlaylistElements (playlistId: string): Promise<Response> { | |
147 | return fetch(this.getPlaylistUrl(playlistId) + '/videos') | |
148 | } | |
149 | ||
150 | loadConfig (): Promise<ServerConfig> { | |
31b6ddf8 | 151 | return fetch('/api/v1/config') |
5abc96fc | 152 | .then(res => res.json()) |
31b6ddf8 C |
153 | } |
154 | ||
99941732 WL |
155 | removeElement (element: HTMLElement) { |
156 | element.parentElement.removeChild(element) | |
157 | } | |
d4f3fea6 | 158 | |
abb3097e | 159 | displayError (text: string, translations?: Translations) { |
99941732 | 160 | // Remove video element |
5abc96fc C |
161 | if (this.playerElement) { |
162 | this.removeElement(this.playerElement) | |
163 | this.playerElement = undefined | |
164 | } | |
99941732 | 165 | |
ad3fa0c5 C |
166 | const translatedText = peertubeTranslate(text, translations) |
167 | const translatedSorry = peertubeTranslate('Sorry', translations) | |
168 | ||
169 | document.title = translatedSorry + ' - ' + translatedText | |
99941732 WL |
170 | |
171 | const errorBlock = document.getElementById('error-block') | |
172 | errorBlock.style.display = 'flex' | |
173 | ||
ad3fa0c5 C |
174 | const errorTitle = document.getElementById('error-title') |
175 | errorTitle.innerHTML = peertubeTranslate('Sorry', translations) | |
176 | ||
99941732 | 177 | const errorText = document.getElementById('error-content') |
ad3fa0c5 | 178 | errorText.innerHTML = translatedText |
99941732 WL |
179 | } |
180 | ||
abb3097e | 181 | videoNotFound (translations?: Translations) { |
99941732 | 182 | const text = 'This video does not exist.' |
ad3fa0c5 | 183 | this.displayError(text, translations) |
99941732 WL |
184 | } |
185 | ||
abb3097e | 186 | videoFetchError (translations?: Translations) { |
99941732 | 187 | const text = 'We cannot fetch the video. Please try again later.' |
ad3fa0c5 | 188 | this.displayError(text, translations) |
99941732 WL |
189 | } |
190 | ||
5abc96fc C |
191 | playlistNotFound (translations?: Translations) { |
192 | const text = 'This playlist does not exist.' | |
193 | this.displayError(text, translations) | |
194 | } | |
195 | ||
196 | playlistFetchError (translations?: Translations) { | |
197 | const text = 'We cannot fetch the playlist. Please try again later.' | |
198 | this.displayError(text, translations) | |
199 | } | |
200 | ||
3b019808 | 201 | getParamToggle (params: URLSearchParams, name: string, defaultValue?: boolean) { |
99941732 WL |
202 | return params.has(name) ? (params.get(name) === '1' || params.get(name) === 'true') : defaultValue |
203 | } | |
d4f3fea6 | 204 | |
3b019808 | 205 | getParamString (params: URLSearchParams, name: string, defaultValue?: string) { |
99941732 WL |
206 | return params.has(name) ? params.get(name) : defaultValue |
207 | } | |
da99ccf2 | 208 | |
902aa3a0 | 209 | async init () { |
99941732 | 210 | try { |
a4ff3100 | 211 | this.userTokens = Tokens.load() |
99941732 WL |
212 | await this.initCore() |
213 | } catch (e) { | |
214 | console.error(e) | |
215 | } | |
216 | } | |
217 | ||
902aa3a0 C |
218 | private initializeApi () { |
219 | if (!this.enableApi) return | |
220 | ||
221 | this.api = new PeerTubeEmbedApi(this) | |
222 | this.api.initialize() | |
223 | } | |
224 | ||
0f2f274c | 225 | private loadParams (video: VideoDetails) { |
da99ccf2 | 226 | try { |
c4710631 | 227 | const params = new URL(window.location.toString()).searchParams |
99941732 | 228 | |
31b6ddf8 C |
229 | this.autoplay = this.getParamToggle(params, 'autoplay', false) |
230 | this.controls = this.getParamToggle(params, 'controls', true) | |
64645512 | 231 | this.muted = this.getParamToggle(params, 'muted', undefined) |
31b6ddf8 | 232 | this.loop = this.getParamToggle(params, 'loop', false) |
5efab546 | 233 | this.title = this.getParamToggle(params, 'title', true) |
99941732 | 234 | this.enableApi = this.getParamToggle(params, 'api', this.enableApi) |
5efab546 | 235 | this.warningTitle = this.getParamToggle(params, 'warningTitle', true) |
08d9ba0f | 236 | this.peertubeLink = this.getParamToggle(params, 'peertubeLink', true) |
f37bad63 | 237 | |
3b019808 C |
238 | this.scope = this.getParamString(params, 'scope', this.scope) |
239 | this.subtitle = this.getParamString(params, 'subtitle') | |
240 | this.startTime = this.getParamString(params, 'start') | |
f0a39880 | 241 | this.stopTime = this.getParamString(params, 'stop') |
3b6f205c | 242 | |
5efab546 C |
243 | this.bigPlayBackgroundColor = this.getParamString(params, 'bigPlayBackgroundColor') |
244 | this.foregroundColor = this.getParamString(params, 'foregroundColor') | |
245 | ||
0f2f274c C |
246 | const modeParam = this.getParamString(params, 'mode') |
247 | ||
248 | if (modeParam) { | |
249 | if (modeParam === 'p2p-media-loader') this.mode = 'p2p-media-loader' | |
250 | else this.mode = 'webtorrent' | |
251 | } else { | |
252 | if (Array.isArray(video.streamingPlaylists) && video.streamingPlaylists.length !== 0) this.mode = 'p2p-media-loader' | |
253 | else this.mode = 'webtorrent' | |
254 | } | |
da99ccf2 C |
255 | } catch (err) { |
256 | console.error('Cannot get params from URL.', err) | |
257 | } | |
99941732 WL |
258 | } |
259 | ||
5abc96fc C |
260 | private async loadPlaylist (playlistId: string) { |
261 | const playlistPromise = this.loadPlaylistInfo(playlistId) | |
262 | const playlistElementsPromise = this.loadPlaylistElements(playlistId) | |
99941732 | 263 | |
5abc96fc C |
264 | const playlistResponse = await playlistPromise |
265 | ||
266 | if (!playlistResponse.ok) { | |
267 | const serverTranslations = await this.translationsPromise | |
71ab65d0 | 268 | |
5abc96fc C |
269 | if (playlistResponse.status === 404) { |
270 | this.playlistNotFound(serverTranslations) | |
271 | return undefined | |
272 | } | |
273 | ||
274 | this.playlistFetchError(serverTranslations) | |
275 | return undefined | |
276 | } | |
277 | ||
278 | return { playlistResponse, videosResponse: await playlistElementsPromise } | |
279 | } | |
280 | ||
281 | private async loadVideo (videoId: string) { | |
3f9c4955 | 282 | const videoPromise = this.loadVideoInfo(videoId) |
3f9c4955 | 283 | |
3f9c4955 | 284 | const videoResponse = await videoPromise |
99941732 | 285 | |
16f7022b | 286 | if (!videoResponse.ok) { |
5abc96fc C |
287 | const serverTranslations = await this.translationsPromise |
288 | ||
289 | if (videoResponse.status === 404) { | |
290 | this.videoNotFound(serverTranslations) | |
291 | return undefined | |
292 | } | |
293 | ||
294 | this.videoFetchError(serverTranslations) | |
295 | return undefined | |
296 | } | |
297 | ||
298 | const captionsPromise = this.loadVideoCaptions(videoId) | |
299 | ||
300 | return { captionsPromise, videoResponse } | |
301 | } | |
3f9c4955 | 302 | |
5abc96fc C |
303 | private async buildPlaylistManager () { |
304 | const translations = await this.translationsPromise | |
305 | ||
306 | this.player.upnext({ | |
307 | timeout: 10000, // 10s | |
308 | headText: peertubeTranslate('Up Next', translations), | |
309 | cancelText: peertubeTranslate('Cancel', translations), | |
310 | suspendedText: peertubeTranslate('Autoplay is suspended', translations), | |
311 | getTitle: () => this.nextVideoTitle(), | |
a950e4c8 | 312 | next: () => this.playNextVideo(), |
5abc96fc C |
313 | condition: () => !!this.getNextPlaylistElement(), |
314 | suspended: () => false | |
315 | }) | |
316 | } | |
99941732 | 317 | |
a950e4c8 | 318 | private async playNextVideo () { |
5abc96fc C |
319 | const next = this.getNextPlaylistElement() |
320 | if (!next) { | |
321 | console.log('Next element not found in playlist.') | |
322 | return | |
99941732 WL |
323 | } |
324 | ||
5abc96fc | 325 | this.currentPlaylistElement = next |
3f9c4955 | 326 | |
4572c3d0 C |
327 | return this.loadVideoAndBuildPlayer(this.currentPlaylistElement.video.uuid) |
328 | } | |
329 | ||
a950e4c8 C |
330 | private async playPreviousVideo () { |
331 | const previous = this.getPreviousPlaylistElement() | |
332 | if (!previous) { | |
333 | console.log('Previous element not found in playlist.') | |
334 | return | |
335 | } | |
336 | ||
337 | this.currentPlaylistElement = previous | |
338 | ||
339 | return this.loadVideoAndBuildPlayer(this.currentPlaylistElement.video.uuid) | |
340 | } | |
341 | ||
4572c3d0 C |
342 | private async loadVideoAndBuildPlayer (uuid: string) { |
343 | const res = await this.loadVideo(uuid) | |
5abc96fc C |
344 | if (res === undefined) return |
345 | ||
346 | return this.buildVideoPlayer(res.videoResponse, res.captionsPromise) | |
347 | } | |
3f9c4955 | 348 | |
5abc96fc C |
349 | private nextVideoTitle () { |
350 | const next = this.getNextPlaylistElement() | |
351 | if (!next) return '' | |
352 | ||
353 | return next.video.name | |
354 | } | |
355 | ||
356 | private getNextPlaylistElement (position?: number): VideoPlaylistElement { | |
357 | if (!position) position = this.currentPlaylistElement.position + 1 | |
358 | ||
359 | if (position > this.playlist.videosLength) { | |
360 | return undefined | |
361 | } | |
362 | ||
363 | const next = this.playlistElements.find(e => e.position === position) | |
364 | ||
365 | if (!next || !next.video) { | |
366 | return this.getNextPlaylistElement(position + 1) | |
367 | } | |
368 | ||
369 | return next | |
370 | } | |
371 | ||
a950e4c8 C |
372 | private getPreviousPlaylistElement (position?: number): VideoPlaylistElement { |
373 | if (!position) position = this.currentPlaylistElement.position -1 | |
374 | ||
375 | if (position < 1) { | |
376 | return undefined | |
377 | } | |
378 | ||
379 | const prev = this.playlistElements.find(e => e.position === position) | |
380 | ||
381 | if (!prev || !prev.video) { | |
382 | return this.getNextPlaylistElement(position - 1) | |
383 | } | |
384 | ||
385 | return prev | |
386 | } | |
387 | ||
5abc96fc C |
388 | private async buildVideoPlayer (videoResponse: Response, captionsPromise: Promise<Response>) { |
389 | let alreadyHadPlayer = false | |
390 | ||
391 | if (this.player) { | |
392 | this.player.dispose() | |
393 | alreadyHadPlayer = true | |
394 | } | |
395 | ||
396 | this.playerElement = document.createElement('video') | |
397 | this.playerElement.className = 'video-js vjs-peertube-skin' | |
398 | this.playerElement.setAttribute('playsinline', 'true') | |
399 | this.wrapperElement.appendChild(this.playerElement) | |
400 | ||
401 | const videoInfoPromise = videoResponse.json() | |
402 | .then((videoInfo: VideoDetails) => { | |
403 | if (!alreadyHadPlayer) this.loadPlaceholder(videoInfo) | |
404 | ||
405 | return videoInfo | |
406 | }) | |
407 | ||
408 | const [ videoInfo, serverTranslations, captionsResponse, config, PeertubePlayerManagerModule ] = await Promise.all([ | |
409 | videoInfoPromise, | |
410 | this.translationsPromise, | |
411 | captionsPromise, | |
412 | this.configPromise, | |
413 | this.PeertubePlayerManagerModulePromise | |
414 | ]) | |
3f9c4955 C |
415 | |
416 | const PeertubePlayerManager = PeertubePlayerManagerModule.PeertubePlayerManager | |
5efab546 | 417 | const videoCaptions = await this.buildCaptions(serverTranslations, captionsResponse) |
99941732 | 418 | |
0f2f274c | 419 | this.loadParams(videoInfo) |
da99ccf2 | 420 | |
4572c3d0 C |
421 | const playlistPlugin = this.currentPlaylistElement |
422 | ? { | |
423 | elements: this.playlistElements, | |
424 | playlist: this.playlist, | |
425 | ||
426 | getCurrentPosition: () => this.currentPlaylistElement.position, | |
427 | ||
428 | onItemClicked: (videoPlaylistElement: VideoPlaylistElement) => { | |
429 | this.currentPlaylistElement = videoPlaylistElement | |
430 | ||
431 | this.loadVideoAndBuildPlayer(this.currentPlaylistElement.video.uuid) | |
432 | .catch(err => console.error(err)) | |
433 | } | |
434 | } | |
435 | : undefined | |
436 | ||
2adfc7ea C |
437 | const options: PeertubePlayerManagerOptions = { |
438 | common: { | |
5abc96fc C |
439 | // Autoplay in playlist mode |
440 | autoplay: alreadyHadPlayer ? true : this.autoplay, | |
2adfc7ea C |
441 | controls: this.controls, |
442 | muted: this.muted, | |
443 | loop: this.loop, | |
444 | captions: videoCaptions.length !== 0, | |
445 | startTime: this.startTime, | |
f0a39880 | 446 | stopTime: this.stopTime, |
2adfc7ea C |
447 | subtitle: this.subtitle, |
448 | ||
a950e4c8 C |
449 | nextVideo: this.playlist ? () => this.playNextVideo() : undefined, |
450 | hasNextVideo: this.playlist ? () => !!this.getNextPlaylistElement() : undefined, | |
451 | ||
452 | previousVideo: this.playlist ? () => this.playPreviousVideo() : undefined, | |
453 | hasPreviousVideo: this.playlist ? () => !!this.getPreviousPlaylistElement() : undefined, | |
454 | ||
4572c3d0 | 455 | playlist: playlistPlugin, |
5abc96fc | 456 | |
2adfc7ea | 457 | videoCaptions, |
35f0a5e6 | 458 | inactivityTimeout: 2500, |
5abc96fc | 459 | videoViewUrl: this.getVideoUrl(videoInfo.uuid) + '/views', |
6ec0b75b | 460 | |
5abc96fc C |
461 | playerElement: this.playerElement, |
462 | onPlayerElementChange: (element: HTMLVideoElement) => this.playerElement = element, | |
6ec0b75b | 463 | |
2adfc7ea C |
464 | videoDuration: videoInfo.duration, |
465 | enableHotkeys: true, | |
08d9ba0f | 466 | peertubeLink: this.peertubeLink, |
2adfc7ea | 467 | poster: window.location.origin + videoInfo.previewPath, |
3d9a63d3 | 468 | theaterButton: false, |
2adfc7ea C |
469 | |
470 | serverUrl: window.location.origin, | |
471 | language: navigator.language, | |
472 | embedUrl: window.location.origin + videoInfo.embedPath | |
6ec0b75b C |
473 | }, |
474 | ||
475 | webtorrent: { | |
476 | videoFiles: videoInfo.files | |
2adfc7ea | 477 | } |
3b6f205c | 478 | } |
2adfc7ea | 479 | |
3b6f205c | 480 | if (this.mode === 'p2p-media-loader') { |
09209296 C |
481 | const hlsPlaylist = videoInfo.streamingPlaylists.find(p => p.type === VideoStreamingPlaylistType.HLS) |
482 | ||
3b6f205c C |
483 | Object.assign(options, { |
484 | p2pMediaLoader: { | |
09209296 C |
485 | playlistUrl: hlsPlaylist.playlistUrl, |
486 | segmentsSha256Url: hlsPlaylist.segmentsSha256Url, | |
487 | redundancyBaseUrls: hlsPlaylist.redundancies.map(r => r.baseUrl), | |
488 | trackerAnnounce: videoInfo.trackerUrls, | |
5a71acd2 | 489 | videoFiles: hlsPlaylist.files |
09209296 | 490 | } as P2PMediaLoaderOptions |
3b6f205c | 491 | }) |
2adfc7ea | 492 | } |
202e7223 | 493 | |
7e37e111 | 494 | this.player = await PeertubePlayerManager.initialize(this.mode, options, (player: videojs.Player) => this.player = player) |
2adfc7ea | 495 | this.player.on('customError', (event: any, data: any) => this.handleError(data.err, serverTranslations)) |
99941732 | 496 | |
2adfc7ea | 497 | window[ 'videojsPlayer' ] = this.player |
902aa3a0 | 498 | |
5efab546 C |
499 | this.buildCSS() |
500 | ||
5abc96fc | 501 | await this.buildDock(videoInfo, config) |
5efab546 C |
502 | |
503 | this.initializeApi() | |
3f9c4955 C |
504 | |
505 | this.removePlaceholder() | |
5abc96fc C |
506 | |
507 | if (this.isPlaylistEmbed()) { | |
508 | await this.buildPlaylistManager() | |
4572c3d0 | 509 | this.player.playlist().updateSelected() |
5abc96fc C |
510 | } |
511 | } | |
512 | ||
513 | private async initCore () { | |
514 | if (this.userTokens) this.setHeadersFromTokens() | |
515 | ||
516 | this.configPromise = this.loadConfig() | |
517 | this.translationsPromise = TranslationsManager.getServerTranslations(window.location.origin, navigator.language) | |
518 | this.PeertubePlayerManagerModulePromise = import('../../assets/player/peertube-player-manager') | |
519 | ||
520 | let videoId: string | |
521 | ||
522 | if (this.isPlaylistEmbed()) { | |
523 | const playlistId = this.getResourceId() | |
524 | const res = await this.loadPlaylist(playlistId) | |
525 | if (!res) return undefined | |
526 | ||
527 | this.playlist = await res.playlistResponse.json() | |
528 | ||
529 | const playlistElementResult = await res.videosResponse.json() | |
530 | this.playlistElements = playlistElementResult.data | |
531 | ||
532 | this.currentPlaylistElement = this.playlistElements[0] | |
533 | videoId = this.currentPlaylistElement.video.uuid | |
534 | } else { | |
535 | videoId = this.getResourceId() | |
536 | } | |
537 | ||
4572c3d0 | 538 | return this.loadVideoAndBuildPlayer(videoId) |
5efab546 C |
539 | } |
540 | ||
541 | private handleError (err: Error, translations?: { [ id: string ]: string }) { | |
542 | if (err.message.indexOf('from xs param') !== -1) { | |
543 | this.player.dispose() | |
5abc96fc | 544 | this.playerElement = null |
5efab546 C |
545 | this.displayError('This video is not available because the remote instance is not responding.', translations) |
546 | return | |
547 | } | |
548 | } | |
549 | ||
5abc96fc | 550 | private async buildDock (videoInfo: VideoDetails, config: ServerConfig) { |
abb3097e | 551 | if (!this.controls) return |
5efab546 | 552 | |
818c449b C |
553 | // On webtorrent fallback, player may have been disposed |
554 | if (!this.player.player_) return | |
5efab546 | 555 | |
abb3097e | 556 | const title = this.title ? videoInfo.name : undefined |
31b6ddf8 | 557 | |
abb3097e C |
558 | const description = config.tracker.enabled && this.warningTitle |
559 | ? '<span class="text">' + peertubeTranslate('Watching this video may reveal your IP address to others.') + '</span>' | |
560 | : undefined | |
561 | ||
562 | this.player.dock({ | |
563 | title, | |
564 | description | |
565 | }) | |
5efab546 | 566 | } |
16f7022b | 567 | |
5efab546 C |
568 | private buildCSS () { |
569 | const body = document.getElementById('custom-css') | |
570 | ||
571 | if (this.bigPlayBackgroundColor) { | |
572 | body.style.setProperty('--embedBigPlayBackgroundColor', this.bigPlayBackgroundColor) | |
573 | } | |
574 | ||
575 | if (this.foregroundColor) { | |
576 | body.style.setProperty('--embedForegroundColor', this.foregroundColor) | |
577 | } | |
99941732 | 578 | } |
6d88de72 | 579 | |
5efab546 C |
580 | private async buildCaptions (serverTranslations: any, captionsResponse: Response): Promise<VideoJSCaption[]> { |
581 | if (captionsResponse.ok) { | |
582 | const { data } = (await captionsResponse.json()) as ResultList<VideoCaption> | |
583 | ||
584 | return data.map(c => ({ | |
585 | label: peertubeTranslate(c.language.label, serverTranslations), | |
586 | language: c.language.id, | |
587 | src: window.location.origin + c.captionPath | |
588 | })) | |
6d88de72 | 589 | } |
5efab546 C |
590 | |
591 | return [] | |
6d88de72 | 592 | } |
3f9c4955 C |
593 | |
594 | private loadPlaceholder (video: VideoDetails) { | |
595 | const placeholder = this.getPlaceholderElement() | |
596 | ||
597 | const url = window.location.origin + video.previewPath | |
598 | placeholder.style.backgroundImage = `url("${url}")` | |
5abc96fc | 599 | placeholder.style.display = 'block' |
3f9c4955 C |
600 | } |
601 | ||
602 | private removePlaceholder () { | |
603 | const placeholder = this.getPlaceholderElement() | |
5abc96fc | 604 | placeholder.style.display = 'none' |
3f9c4955 C |
605 | } |
606 | ||
607 | private getPlaceholderElement () { | |
608 | return document.getElementById('placeholder-preview') | |
609 | } | |
a4ff3100 C |
610 | |
611 | private setHeadersFromTokens () { | |
612 | this.headers.set('Authorization', `${this.userTokens.tokenType} ${this.userTokens.accessToken}`) | |
613 | } | |
5abc96fc C |
614 | |
615 | private getResourceId () { | |
616 | const urlParts = window.location.pathname.split('/') | |
617 | return urlParts[ urlParts.length - 1 ] | |
618 | } | |
619 | ||
620 | private isPlaylistEmbed () { | |
621 | return window.location.pathname.split('/')[1] === 'video-playlists' | |
622 | } | |
99941732 WL |
623 | } |
624 | ||
625 | PeerTubeEmbed.main() | |
902aa3a0 | 626 | .catch(err => console.error('Cannot init embed.', err)) |