]>
Commit | Line | Data |
---|---|---|
202e7223 C |
1 | import './embed.scss' |
2 | ||
d1bd87e0 C |
3 | import 'core-js/es6/symbol' |
4 | import 'core-js/es6/object' | |
5 | import 'core-js/es6/function' | |
6 | import 'core-js/es6/parse-int' | |
7 | import 'core-js/es6/parse-float' | |
8 | import 'core-js/es6/number' | |
9 | import 'core-js/es6/math' | |
10 | import 'core-js/es6/string' | |
11 | import 'core-js/es6/date' | |
12 | import 'core-js/es6/array' | |
13 | import 'core-js/es6/regexp' | |
14 | import 'core-js/es6/map' | |
15 | import 'core-js/es6/weak-map' | |
16 | import 'core-js/es6/set' | |
cd4d7a2c C |
17 | // For google bot that uses Chrome 41 and does not understand fetch |
18 | import 'whatwg-fetch' | |
19 | ||
99941732 | 20 | import * as Channel from 'jschannel' |
c6352f2c | 21 | |
3dfa8494 | 22 | import { peertubeTranslate, ResultList, VideoDetails } from '../../../../shared' |
902aa3a0 | 23 | import { PeerTubeResolution } from '../player/definitions' |
16f7022b | 24 | import { VideoJSCaption } from '../../assets/player/peertube-videojs-typings' |
59c76ffa | 25 | import { VideoCaption } from '../../../../shared/models/videos/caption/video-caption.model' |
09209296 C |
26 | import { |
27 | P2PMediaLoaderOptions, | |
28 | PeertubePlayerManager, | |
29 | PeertubePlayerManagerOptions, | |
30 | PlayerMode | |
31 | } from '../../assets/player/peertube-player-manager' | |
32 | import { VideoStreamingPlaylistType } from '../../../../shared/models/videos/video-streaming-playlist.type' | |
202e7223 | 33 | |
99941732 | 34 | /** |
902aa3a0 | 35 | * Embed API exposes control of the embed player to the outside world via |
99941732 WL |
36 | * JSChannels and window.postMessage |
37 | */ | |
38 | class PeerTubeEmbedApi { | |
902aa3a0 | 39 | private channel: Channel.MessagingChannel |
99941732 | 40 | private isReady = false |
902aa3a0 C |
41 | private resolutions: PeerTubeResolution[] = null |
42 | ||
43 | constructor (private embed: PeerTubeEmbed) { | |
44 | } | |
d4f3fea6 | 45 | |
902aa3a0 | 46 | initialize () { |
99941732 WL |
47 | this.constructChannel() |
48 | this.setupStateTracking() | |
d4f3fea6 | 49 | |
99941732 | 50 | // We're ready! |
d4f3fea6 | 51 | |
99941732 WL |
52 | this.notifyReady() |
53 | } | |
902aa3a0 C |
54 | |
55 | private get element () { | |
99941732 WL |
56 | return this.embed.videoElement |
57 | } | |
d4f3fea6 | 58 | |
902aa3a0 | 59 | private constructChannel () { |
99941732 | 60 | let channel = Channel.build({ window: window.parent, origin: '*', scope: this.embed.scope }) |
902aa3a0 | 61 | |
99941732 WL |
62 | channel.bind('play', (txn, params) => this.embed.player.play()) |
63 | channel.bind('pause', (txn, params) => this.embed.player.pause()) | |
64 | channel.bind('seek', (txn, time) => this.embed.player.currentTime(time)) | |
65 | channel.bind('setVolume', (txn, value) => this.embed.player.volume(value)) | |
66 | channel.bind('getVolume', (txn, value) => this.embed.player.volume()) | |
67 | channel.bind('isReady', (txn, params) => this.isReady) | |
68 | channel.bind('setResolution', (txn, resolutionId) => this.setResolution(resolutionId)) | |
69 | channel.bind('getResolutions', (txn, params) => this.resolutions) | |
70 | channel.bind('setPlaybackRate', (txn, playbackRate) => this.embed.player.playbackRate(playbackRate)) | |
71 | channel.bind('getPlaybackRate', (txn, params) => this.embed.player.playbackRate()) | |
72 | channel.bind('getPlaybackRates', (txn, params) => this.embed.playerOptions.playbackRates) | |
d4f3fea6 | 73 | |
99941732 WL |
74 | this.channel = channel |
75 | } | |
d4f3fea6 | 76 | |
902aa3a0 | 77 | private setResolution (resolutionId: number) { |
2adfc7ea | 78 | if (resolutionId === -1 && this.embed.player.webtorrent().isAutoResolutionForbidden()) return |
99941732 WL |
79 | |
80 | // Auto resolution | |
81 | if (resolutionId === -1) { | |
2adfc7ea | 82 | this.embed.player.webtorrent().enableAutoResolution() |
99941732 WL |
83 | return |
84 | } | |
85 | ||
2adfc7ea C |
86 | this.embed.player.webtorrent().disableAutoResolution() |
87 | this.embed.player.webtorrent().updateResolution(resolutionId) | |
99941732 WL |
88 | } |
89 | ||
90 | /** | |
91 | * Let the host know that we're ready to go! | |
92 | */ | |
902aa3a0 | 93 | private notifyReady () { |
99941732 WL |
94 | this.isReady = true |
95 | this.channel.notify({ method: 'ready', params: true }) | |
96 | } | |
97 | ||
902aa3a0 C |
98 | private setupStateTracking () { |
99 | let currentState: 'playing' | 'paused' | 'unstarted' = 'unstarted' | |
99941732 WL |
100 | |
101 | setInterval(() => { | |
102 | let position = this.element.currentTime | |
103 | let volume = this.element.volume | |
104 | ||
105 | this.channel.notify({ | |
106 | method: 'playbackStatusUpdate', | |
107 | params: { | |
108 | position, | |
109 | volume, | |
902aa3a0 | 110 | playbackState: currentState |
99941732 WL |
111 | } |
112 | }) | |
113 | }, 500) | |
114 | ||
115 | this.element.addEventListener('play', ev => { | |
116 | currentState = 'playing' | |
117 | this.channel.notify({ method: 'playbackStatusChange', params: 'playing' }) | |
118 | }) | |
119 | ||
120 | this.element.addEventListener('pause', ev => { | |
121 | currentState = 'paused' | |
122 | this.channel.notify({ method: 'playbackStatusChange', params: 'paused' }) | |
123 | }) | |
124 | ||
125 | // PeerTube specific capabilities | |
126 | ||
2adfc7ea C |
127 | if (this.embed.player.webtorrent) { |
128 | this.embed.player.webtorrent().on('autoResolutionUpdate', () => this.loadWebTorrentResolutions()) | |
129 | this.embed.player.webtorrent().on('videoFileUpdate', () => this.loadWebTorrentResolutions()) | |
130 | } | |
99941732 WL |
131 | } |
132 | ||
2adfc7ea | 133 | private loadWebTorrentResolutions () { |
99941732 | 134 | let resolutions = [] |
2adfc7ea | 135 | let currentResolutionId = this.embed.player.webtorrent().getCurrentResolutionId() |
99941732 | 136 | |
2adfc7ea | 137 | for (const videoFile of this.embed.player.webtorrent().videoFiles) { |
99941732 WL |
138 | let label = videoFile.resolution.label |
139 | if (videoFile.fps && videoFile.fps >= 50) { | |
140 | label += videoFile.fps | |
141 | } | |
d4f3fea6 | 142 | |
99941732 WL |
143 | resolutions.push({ |
144 | id: videoFile.resolution.id, | |
145 | label, | |
146 | src: videoFile.magnetUri, | |
147 | active: videoFile.resolution.id === currentResolutionId | |
148 | }) | |
149 | } | |
150 | ||
151 | this.resolutions = resolutions | |
152 | this.channel.notify({ | |
153 | method: 'resolutionUpdate', | |
154 | params: this.resolutions | |
155 | }) | |
156 | } | |
202e7223 C |
157 | } |
158 | ||
99941732 | 159 | class PeerTubeEmbed { |
902aa3a0 C |
160 | videoElement: HTMLVideoElement |
161 | player: any | |
162 | playerOptions: any | |
163 | api: PeerTubeEmbedApi = null | |
3b019808 C |
164 | autoplay: boolean |
165 | controls: boolean | |
166 | muted: boolean | |
167 | loop: boolean | |
168 | subtitle: string | |
902aa3a0 | 169 | enableApi = false |
1f6824c9 | 170 | startTime: number | string = 0 |
3b6f205c | 171 | mode: PlayerMode |
902aa3a0 C |
172 | scope = 'peertube' |
173 | ||
174 | static async main () { | |
c6352f2c | 175 | const videoContainerId = 'video-container' |
99941732 WL |
176 | const embed = new PeerTubeEmbed(videoContainerId) |
177 | await embed.init() | |
178 | } | |
902aa3a0 C |
179 | |
180 | constructor (private videoContainerId: string) { | |
181 | this.videoElement = document.getElementById(videoContainerId) as HTMLVideoElement | |
182 | } | |
183 | ||
99941732 WL |
184 | getVideoUrl (id: string) { |
185 | return window.location.origin + '/api/v1/videos/' + id | |
186 | } | |
d4f3fea6 | 187 | |
99941732 WL |
188 | loadVideoInfo (videoId: string): Promise<Response> { |
189 | return fetch(this.getVideoUrl(videoId)) | |
190 | } | |
d4f3fea6 | 191 | |
16f7022b C |
192 | loadVideoCaptions (videoId: string): Promise<Response> { |
193 | return fetch(this.getVideoUrl(videoId) + '/captions') | |
194 | } | |
195 | ||
99941732 WL |
196 | removeElement (element: HTMLElement) { |
197 | element.parentElement.removeChild(element) | |
198 | } | |
d4f3fea6 | 199 | |
ad3fa0c5 | 200 | displayError (text: string, translations?: { [ id: string ]: string }) { |
99941732 | 201 | // Remove video element |
6d88de72 | 202 | if (this.videoElement) this.removeElement(this.videoElement) |
99941732 | 203 | |
ad3fa0c5 C |
204 | const translatedText = peertubeTranslate(text, translations) |
205 | const translatedSorry = peertubeTranslate('Sorry', translations) | |
206 | ||
207 | document.title = translatedSorry + ' - ' + translatedText | |
99941732 WL |
208 | |
209 | const errorBlock = document.getElementById('error-block') | |
210 | errorBlock.style.display = 'flex' | |
211 | ||
ad3fa0c5 C |
212 | const errorTitle = document.getElementById('error-title') |
213 | errorTitle.innerHTML = peertubeTranslate('Sorry', translations) | |
214 | ||
99941732 | 215 | const errorText = document.getElementById('error-content') |
ad3fa0c5 | 216 | errorText.innerHTML = translatedText |
99941732 WL |
217 | } |
218 | ||
ad3fa0c5 | 219 | videoNotFound (translations?: { [ id: string ]: string }) { |
99941732 | 220 | const text = 'This video does not exist.' |
ad3fa0c5 | 221 | this.displayError(text, translations) |
99941732 WL |
222 | } |
223 | ||
ad3fa0c5 | 224 | videoFetchError (translations?: { [ id: string ]: string }) { |
99941732 | 225 | const text = 'We cannot fetch the video. Please try again later.' |
ad3fa0c5 | 226 | this.displayError(text, translations) |
99941732 WL |
227 | } |
228 | ||
3b019808 | 229 | getParamToggle (params: URLSearchParams, name: string, defaultValue?: boolean) { |
99941732 WL |
230 | return params.has(name) ? (params.get(name) === '1' || params.get(name) === 'true') : defaultValue |
231 | } | |
d4f3fea6 | 232 | |
3b019808 | 233 | getParamString (params: URLSearchParams, name: string, defaultValue?: string) { |
99941732 WL |
234 | return params.has(name) ? params.get(name) : defaultValue |
235 | } | |
da99ccf2 | 236 | |
902aa3a0 | 237 | async init () { |
99941732 WL |
238 | try { |
239 | await this.initCore() | |
240 | } catch (e) { | |
241 | console.error(e) | |
242 | } | |
243 | } | |
244 | ||
902aa3a0 C |
245 | private initializeApi () { |
246 | if (!this.enableApi) return | |
247 | ||
248 | this.api = new PeerTubeEmbedApi(this) | |
249 | this.api.initialize() | |
250 | } | |
251 | ||
252 | private loadParams () { | |
da99ccf2 C |
253 | try { |
254 | let params = new URL(window.location.toString()).searchParams | |
99941732 | 255 | |
3b019808 C |
256 | this.autoplay = this.getParamToggle(params, 'autoplay') |
257 | this.controls = this.getParamToggle(params, 'controls') | |
258 | this.muted = this.getParamToggle(params, 'muted') | |
259 | this.loop = this.getParamToggle(params, 'loop') | |
99941732 | 260 | this.enableApi = this.getParamToggle(params, 'api', this.enableApi) |
f37bad63 | 261 | |
3b019808 C |
262 | this.scope = this.getParamString(params, 'scope', this.scope) |
263 | this.subtitle = this.getParamString(params, 'subtitle') | |
264 | this.startTime = this.getParamString(params, 'start') | |
3b6f205c C |
265 | |
266 | this.mode = this.getParamToggle(params, 'p2p-media-loader') ? 'p2p-media-loader' : 'webtorrent' | |
da99ccf2 C |
267 | } catch (err) { |
268 | console.error('Cannot get params from URL.', err) | |
269 | } | |
99941732 WL |
270 | } |
271 | ||
902aa3a0 | 272 | private async initCore () { |
6385c0cb C |
273 | const urlParts = window.location.pathname.split('/') |
274 | const videoId = urlParts[ urlParts.length - 1 ] | |
99941732 | 275 | |
2adfc7ea C |
276 | const [ serverTranslations, videoResponse, captionsResponse ] = await Promise.all([ |
277 | PeertubePlayerManager.getServerTranslations(window.location.origin, navigator.language), | |
16f7022b C |
278 | this.loadVideoInfo(videoId), |
279 | this.loadVideoCaptions(videoId) | |
280 | ]) | |
99941732 | 281 | |
16f7022b | 282 | if (!videoResponse.ok) { |
ad3fa0c5 | 283 | if (videoResponse.status === 404) return this.videoNotFound(serverTranslations) |
99941732 | 284 | |
ad3fa0c5 | 285 | return this.videoFetchError(serverTranslations) |
99941732 WL |
286 | } |
287 | ||
16f7022b C |
288 | const videoInfo: VideoDetails = await videoResponse.json() |
289 | let videoCaptions: VideoJSCaption[] = [] | |
290 | if (captionsResponse.ok) { | |
291 | const { data } = (await captionsResponse.json()) as ResultList<VideoCaption> | |
292 | videoCaptions = data.map(c => ({ | |
3dfa8494 | 293 | label: peertubeTranslate(c.language.label, serverTranslations), |
16f7022b C |
294 | language: c.language.id, |
295 | src: window.location.origin + c.captionPath | |
296 | })) | |
297 | } | |
99941732 WL |
298 | |
299 | this.loadParams() | |
da99ccf2 | 300 | |
2adfc7ea C |
301 | const options: PeertubePlayerManagerOptions = { |
302 | common: { | |
303 | autoplay: this.autoplay, | |
304 | controls: this.controls, | |
305 | muted: this.muted, | |
306 | loop: this.loop, | |
307 | captions: videoCaptions.length !== 0, | |
308 | startTime: this.startTime, | |
309 | subtitle: this.subtitle, | |
310 | ||
311 | videoCaptions, | |
312 | inactivityTimeout: 1500, | |
313 | videoViewUrl: this.getVideoUrl(videoId) + '/views', | |
6ec0b75b | 314 | |
2adfc7ea | 315 | playerElement: this.videoElement, |
6ec0b75b C |
316 | onPlayerElementChange: (element: HTMLVideoElement) => this.videoElement = element, |
317 | ||
2adfc7ea C |
318 | videoDuration: videoInfo.duration, |
319 | enableHotkeys: true, | |
320 | peertubeLink: true, | |
321 | poster: window.location.origin + videoInfo.previewPath, | |
322 | theaterMode: false, | |
323 | ||
324 | serverUrl: window.location.origin, | |
325 | language: navigator.language, | |
326 | embedUrl: window.location.origin + videoInfo.embedPath | |
6ec0b75b C |
327 | }, |
328 | ||
329 | webtorrent: { | |
330 | videoFiles: videoInfo.files | |
2adfc7ea | 331 | } |
3b6f205c | 332 | } |
2adfc7ea | 333 | |
3b6f205c | 334 | if (this.mode === 'p2p-media-loader') { |
09209296 C |
335 | const hlsPlaylist = videoInfo.streamingPlaylists.find(p => p.type === VideoStreamingPlaylistType.HLS) |
336 | ||
3b6f205c C |
337 | Object.assign(options, { |
338 | p2pMediaLoader: { | |
09209296 C |
339 | playlistUrl: hlsPlaylist.playlistUrl, |
340 | segmentsSha256Url: hlsPlaylist.segmentsSha256Url, | |
341 | redundancyBaseUrls: hlsPlaylist.redundancies.map(r => r.baseUrl), | |
342 | trackerAnnounce: videoInfo.trackerUrls, | |
343 | videoFiles: videoInfo.files | |
344 | } as P2PMediaLoaderOptions | |
3b6f205c | 345 | }) |
2adfc7ea | 346 | } |
202e7223 | 347 | |
3b6f205c | 348 | this.player = await PeertubePlayerManager.initialize(this.mode, options) |
e945b184 | 349 | |
2adfc7ea | 350 | this.player.on('customError', (event: any, data: any) => this.handleError(data.err, serverTranslations)) |
99941732 | 351 | |
2adfc7ea | 352 | window[ 'videojsPlayer' ] = this.player |
902aa3a0 | 353 | |
2adfc7ea C |
354 | if (this.controls) { |
355 | this.player.dock({ | |
356 | title: videoInfo.name, | |
357 | description: this.player.localize('Uses P2P, others may know your IP is downloading this video.') | |
358 | }) | |
359 | } | |
16f7022b | 360 | |
2adfc7ea | 361 | this.initializeApi() |
99941732 | 362 | } |
6d88de72 | 363 | |
ad3fa0c5 | 364 | private handleError (err: Error, translations?: { [ id: string ]: string }) { |
0f7fedc3 | 365 | if (err.message.indexOf('from xs param') !== -1) { |
6d88de72 C |
366 | this.player.dispose() |
367 | this.videoElement = null | |
ad3fa0c5 | 368 | this.displayError('This video is not available because the remote instance is not responding.', translations) |
6d88de72 C |
369 | return |
370 | } | |
371 | } | |
99941732 WL |
372 | } |
373 | ||
374 | PeerTubeEmbed.main() | |
902aa3a0 | 375 | .catch(err => console.error('Cannot init embed.', err)) |