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