]>
Commit | Line | Data |
---|---|---|
202e7223 C |
1 | import './embed.scss' |
2 | ||
3f9c4955 C |
3 | import { |
4 | getCompleteLocale, | |
5 | is18nLocale, | |
6 | isDefaultLocale, | |
7 | peertubeTranslate, | |
8 | ResultList, | |
9 | ServerConfig, | |
10 | VideoDetails | |
11 | } from '../../../../shared' | |
16f7022b | 12 | import { VideoJSCaption } from '../../assets/player/peertube-videojs-typings' |
59c76ffa | 13 | import { VideoCaption } from '../../../../shared/models/videos/caption/video-caption.model' |
09209296 C |
14 | import { |
15 | P2PMediaLoaderOptions, | |
09209296 C |
16 | PeertubePlayerManagerOptions, |
17 | PlayerMode | |
18 | } from '../../assets/player/peertube-player-manager' | |
19 | import { VideoStreamingPlaylistType } from '../../../../shared/models/videos/video-streaming-playlist.type' | |
5efab546 | 20 | import { PeerTubeEmbedApi } from './embed-api' |
3f9c4955 | 21 | import { TranslationsManager } from '../../assets/player/translations-manager' |
202e7223 | 22 | |
5efab546 | 23 | export class PeerTubeEmbed { |
902aa3a0 C |
24 | videoElement: HTMLVideoElement |
25 | player: any | |
26 | playerOptions: any | |
27 | api: PeerTubeEmbedApi = null | |
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 | |
39 | bigPlayBackgroundColor: string | |
40 | foregroundColor: string | |
41 | ||
3b6f205c | 42 | mode: PlayerMode |
902aa3a0 C |
43 | scope = 'peertube' |
44 | ||
45 | static async main () { | |
c6352f2c | 46 | const videoContainerId = 'video-container' |
99941732 WL |
47 | const embed = new PeerTubeEmbed(videoContainerId) |
48 | await embed.init() | |
49 | } | |
902aa3a0 C |
50 | |
51 | constructor (private videoContainerId: string) { | |
52 | this.videoElement = document.getElementById(videoContainerId) as HTMLVideoElement | |
53 | } | |
54 | ||
99941732 WL |
55 | getVideoUrl (id: string) { |
56 | return window.location.origin + '/api/v1/videos/' + id | |
57 | } | |
d4f3fea6 | 58 | |
99941732 WL |
59 | loadVideoInfo (videoId: string): Promise<Response> { |
60 | return fetch(this.getVideoUrl(videoId)) | |
61 | } | |
d4f3fea6 | 62 | |
16f7022b C |
63 | loadVideoCaptions (videoId: string): Promise<Response> { |
64 | return fetch(this.getVideoUrl(videoId) + '/captions') | |
65 | } | |
66 | ||
31b6ddf8 C |
67 | loadConfig (): Promise<Response> { |
68 | return fetch('/api/v1/config') | |
69 | } | |
70 | ||
99941732 WL |
71 | removeElement (element: HTMLElement) { |
72 | element.parentElement.removeChild(element) | |
73 | } | |
d4f3fea6 | 74 | |
ad3fa0c5 | 75 | displayError (text: string, translations?: { [ id: string ]: string }) { |
99941732 | 76 | // Remove video element |
6d88de72 | 77 | if (this.videoElement) this.removeElement(this.videoElement) |
99941732 | 78 | |
ad3fa0c5 C |
79 | const translatedText = peertubeTranslate(text, translations) |
80 | const translatedSorry = peertubeTranslate('Sorry', translations) | |
81 | ||
82 | document.title = translatedSorry + ' - ' + translatedText | |
99941732 WL |
83 | |
84 | const errorBlock = document.getElementById('error-block') | |
85 | errorBlock.style.display = 'flex' | |
86 | ||
ad3fa0c5 C |
87 | const errorTitle = document.getElementById('error-title') |
88 | errorTitle.innerHTML = peertubeTranslate('Sorry', translations) | |
89 | ||
99941732 | 90 | const errorText = document.getElementById('error-content') |
ad3fa0c5 | 91 | errorText.innerHTML = translatedText |
99941732 WL |
92 | } |
93 | ||
ad3fa0c5 | 94 | videoNotFound (translations?: { [ id: string ]: string }) { |
99941732 | 95 | const text = 'This video does not exist.' |
ad3fa0c5 | 96 | this.displayError(text, translations) |
99941732 WL |
97 | } |
98 | ||
ad3fa0c5 | 99 | videoFetchError (translations?: { [ id: string ]: string }) { |
99941732 | 100 | const text = 'We cannot fetch the video. Please try again later.' |
ad3fa0c5 | 101 | this.displayError(text, translations) |
99941732 WL |
102 | } |
103 | ||
3b019808 | 104 | getParamToggle (params: URLSearchParams, name: string, defaultValue?: boolean) { |
99941732 WL |
105 | return params.has(name) ? (params.get(name) === '1' || params.get(name) === 'true') : defaultValue |
106 | } | |
d4f3fea6 | 107 | |
3b019808 | 108 | getParamString (params: URLSearchParams, name: string, defaultValue?: string) { |
99941732 WL |
109 | return params.has(name) ? params.get(name) : defaultValue |
110 | } | |
da99ccf2 | 111 | |
902aa3a0 | 112 | async init () { |
99941732 WL |
113 | try { |
114 | await this.initCore() | |
115 | } catch (e) { | |
116 | console.error(e) | |
117 | } | |
118 | } | |
119 | ||
902aa3a0 C |
120 | private initializeApi () { |
121 | if (!this.enableApi) return | |
122 | ||
123 | this.api = new PeerTubeEmbedApi(this) | |
124 | this.api.initialize() | |
125 | } | |
126 | ||
0f2f274c | 127 | private loadParams (video: VideoDetails) { |
da99ccf2 | 128 | try { |
c4710631 | 129 | const params = new URL(window.location.toString()).searchParams |
99941732 | 130 | |
31b6ddf8 C |
131 | this.autoplay = this.getParamToggle(params, 'autoplay', false) |
132 | this.controls = this.getParamToggle(params, 'controls', true) | |
133 | this.muted = this.getParamToggle(params, 'muted', false) | |
134 | this.loop = this.getParamToggle(params, 'loop', false) | |
5efab546 | 135 | this.title = this.getParamToggle(params, 'title', true) |
99941732 | 136 | this.enableApi = this.getParamToggle(params, 'api', this.enableApi) |
5efab546 | 137 | this.warningTitle = this.getParamToggle(params, 'warningTitle', true) |
f37bad63 | 138 | |
3b019808 C |
139 | this.scope = this.getParamString(params, 'scope', this.scope) |
140 | this.subtitle = this.getParamString(params, 'subtitle') | |
141 | this.startTime = this.getParamString(params, 'start') | |
f0a39880 | 142 | this.stopTime = this.getParamString(params, 'stop') |
3b6f205c | 143 | |
5efab546 C |
144 | this.bigPlayBackgroundColor = this.getParamString(params, 'bigPlayBackgroundColor') |
145 | this.foregroundColor = this.getParamString(params, 'foregroundColor') | |
146 | ||
0f2f274c C |
147 | const modeParam = this.getParamString(params, 'mode') |
148 | ||
149 | if (modeParam) { | |
150 | if (modeParam === 'p2p-media-loader') this.mode = 'p2p-media-loader' | |
151 | else this.mode = 'webtorrent' | |
152 | } else { | |
153 | if (Array.isArray(video.streamingPlaylists) && video.streamingPlaylists.length !== 0) this.mode = 'p2p-media-loader' | |
154 | else this.mode = 'webtorrent' | |
155 | } | |
da99ccf2 C |
156 | } catch (err) { |
157 | console.error('Cannot get params from URL.', err) | |
158 | } | |
99941732 WL |
159 | } |
160 | ||
902aa3a0 | 161 | private async initCore () { |
6385c0cb C |
162 | const urlParts = window.location.pathname.split('/') |
163 | const videoId = urlParts[ urlParts.length - 1 ] | |
99941732 | 164 | |
3f9c4955 C |
165 | const videoPromise = this.loadVideoInfo(videoId) |
166 | const captionsPromise = this.loadVideoCaptions(videoId) | |
167 | const configPromise = this.loadConfig() | |
168 | ||
169 | const translationsPromise = TranslationsManager.getServerTranslations(window.location.origin, navigator.language) | |
170 | const videoResponse = await videoPromise | |
99941732 | 171 | |
16f7022b | 172 | if (!videoResponse.ok) { |
3f9c4955 C |
173 | const serverTranslations = await translationsPromise |
174 | ||
ad3fa0c5 | 175 | if (videoResponse.status === 404) return this.videoNotFound(serverTranslations) |
99941732 | 176 | |
ad3fa0c5 | 177 | return this.videoFetchError(serverTranslations) |
99941732 WL |
178 | } |
179 | ||
16f7022b | 180 | const videoInfo: VideoDetails = await videoResponse.json() |
3f9c4955 C |
181 | this.loadPlaceholder(videoInfo) |
182 | ||
183 | const PeertubePlayerManagerModulePromise = import('../../assets/player/peertube-player-manager') | |
184 | ||
185 | const promises = [ translationsPromise, captionsPromise, configPromise, PeertubePlayerManagerModulePromise ] | |
186 | const [ serverTranslations, captionsResponse, configResponse, PeertubePlayerManagerModule ] = await Promise.all(promises) | |
187 | ||
188 | const PeertubePlayerManager = PeertubePlayerManagerModule.PeertubePlayerManager | |
5efab546 | 189 | const videoCaptions = await this.buildCaptions(serverTranslations, captionsResponse) |
99941732 | 190 | |
0f2f274c | 191 | this.loadParams(videoInfo) |
da99ccf2 | 192 | |
2adfc7ea C |
193 | const options: PeertubePlayerManagerOptions = { |
194 | common: { | |
195 | autoplay: this.autoplay, | |
196 | controls: this.controls, | |
197 | muted: this.muted, | |
198 | loop: this.loop, | |
199 | captions: videoCaptions.length !== 0, | |
200 | startTime: this.startTime, | |
f0a39880 | 201 | stopTime: this.stopTime, |
2adfc7ea C |
202 | subtitle: this.subtitle, |
203 | ||
204 | videoCaptions, | |
205 | inactivityTimeout: 1500, | |
206 | videoViewUrl: this.getVideoUrl(videoId) + '/views', | |
6ec0b75b | 207 | |
2adfc7ea | 208 | playerElement: this.videoElement, |
6ec0b75b C |
209 | onPlayerElementChange: (element: HTMLVideoElement) => this.videoElement = element, |
210 | ||
2adfc7ea C |
211 | videoDuration: videoInfo.duration, |
212 | enableHotkeys: true, | |
213 | peertubeLink: true, | |
214 | poster: window.location.origin + videoInfo.previewPath, | |
3d9a63d3 | 215 | theaterButton: false, |
2adfc7ea C |
216 | |
217 | serverUrl: window.location.origin, | |
218 | language: navigator.language, | |
219 | embedUrl: window.location.origin + videoInfo.embedPath | |
6ec0b75b C |
220 | }, |
221 | ||
222 | webtorrent: { | |
223 | videoFiles: videoInfo.files | |
2adfc7ea | 224 | } |
3b6f205c | 225 | } |
2adfc7ea | 226 | |
3b6f205c | 227 | if (this.mode === 'p2p-media-loader') { |
09209296 C |
228 | const hlsPlaylist = videoInfo.streamingPlaylists.find(p => p.type === VideoStreamingPlaylistType.HLS) |
229 | ||
3b6f205c C |
230 | Object.assign(options, { |
231 | p2pMediaLoader: { | |
09209296 C |
232 | playlistUrl: hlsPlaylist.playlistUrl, |
233 | segmentsSha256Url: hlsPlaylist.segmentsSha256Url, | |
234 | redundancyBaseUrls: hlsPlaylist.redundancies.map(r => r.baseUrl), | |
235 | trackerAnnounce: videoInfo.trackerUrls, | |
5a71acd2 | 236 | videoFiles: hlsPlaylist.files |
09209296 | 237 | } as P2PMediaLoaderOptions |
3b6f205c | 238 | }) |
2adfc7ea | 239 | } |
202e7223 | 240 | |
3f9c4955 | 241 | this.player = await PeertubePlayerManager.initialize(this.mode, options, (player: any) => this.player = player) |
2adfc7ea | 242 | this.player.on('customError', (event: any, data: any) => this.handleError(data.err, serverTranslations)) |
99941732 | 243 | |
2adfc7ea | 244 | window[ 'videojsPlayer' ] = this.player |
902aa3a0 | 245 | |
5efab546 C |
246 | this.buildCSS() |
247 | ||
248 | await this.buildDock(videoInfo, configResponse) | |
249 | ||
250 | this.initializeApi() | |
3f9c4955 C |
251 | |
252 | this.removePlaceholder() | |
5efab546 C |
253 | } |
254 | ||
255 | private handleError (err: Error, translations?: { [ id: string ]: string }) { | |
256 | if (err.message.indexOf('from xs param') !== -1) { | |
257 | this.player.dispose() | |
258 | this.videoElement = null | |
259 | this.displayError('This video is not available because the remote instance is not responding.', translations) | |
260 | return | |
261 | } | |
262 | } | |
263 | ||
264 | private async buildDock (videoInfo: VideoDetails, configResponse: Response) { | |
2adfc7ea | 265 | if (this.controls) { |
5efab546 C |
266 | const title = this.title ? videoInfo.name : undefined |
267 | ||
31b6ddf8 | 268 | const config: ServerConfig = await configResponse.json() |
5efab546 | 269 | const description = config.tracker.enabled && this.warningTitle |
13176a07 | 270 | ? '<span class="text">' + this.player.localize('Watching this video may reveal your IP address to others.') + '</span>' |
31b6ddf8 C |
271 | : undefined |
272 | ||
2adfc7ea | 273 | this.player.dock({ |
5efab546 | 274 | title, |
31b6ddf8 | 275 | description |
2adfc7ea C |
276 | }) |
277 | } | |
5efab546 | 278 | } |
16f7022b | 279 | |
5efab546 C |
280 | private buildCSS () { |
281 | const body = document.getElementById('custom-css') | |
282 | ||
283 | if (this.bigPlayBackgroundColor) { | |
284 | body.style.setProperty('--embedBigPlayBackgroundColor', this.bigPlayBackgroundColor) | |
285 | } | |
286 | ||
287 | if (this.foregroundColor) { | |
288 | body.style.setProperty('--embedForegroundColor', this.foregroundColor) | |
289 | } | |
99941732 | 290 | } |
6d88de72 | 291 | |
5efab546 C |
292 | private async buildCaptions (serverTranslations: any, captionsResponse: Response): Promise<VideoJSCaption[]> { |
293 | if (captionsResponse.ok) { | |
294 | const { data } = (await captionsResponse.json()) as ResultList<VideoCaption> | |
295 | ||
296 | return data.map(c => ({ | |
297 | label: peertubeTranslate(c.language.label, serverTranslations), | |
298 | language: c.language.id, | |
299 | src: window.location.origin + c.captionPath | |
300 | })) | |
6d88de72 | 301 | } |
5efab546 C |
302 | |
303 | return [] | |
6d88de72 | 304 | } |
3f9c4955 C |
305 | |
306 | private loadPlaceholder (video: VideoDetails) { | |
307 | const placeholder = this.getPlaceholderElement() | |
308 | ||
309 | const url = window.location.origin + video.previewPath | |
310 | placeholder.style.backgroundImage = `url("${url}")` | |
311 | } | |
312 | ||
313 | private removePlaceholder () { | |
314 | const placeholder = this.getPlaceholderElement() | |
315 | placeholder.parentElement.removeChild(placeholder) | |
316 | } | |
317 | ||
318 | private getPlaceholderElement () { | |
319 | return document.getElementById('placeholder-preview') | |
320 | } | |
99941732 WL |
321 | } |
322 | ||
323 | PeerTubeEmbed.main() | |
902aa3a0 | 324 | .catch(err => console.error('Cannot init embed.', err)) |