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