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'
17 // For google bot that uses Chrome 41 and does not understand fetch
20 import * as vjs from 'video.js'
21 import * as Channel from 'jschannel'
23 import { VideoDetails } from '../../../../shared'
24 import { addContextMenu, getVideojsOptions, loadLocale } from '../../assets/player/peertube-player'
25 import { PeerTubeResolution } from '../player/definitions'
28 * Embed API exposes control of the embed player to the outside world via
29 * JSChannels and window.postMessage
31 class PeerTubeEmbedApi {
32 private channel: Channel.MessagingChannel
33 private isReady = false
34 private resolutions: PeerTubeResolution[] = null
36 constructor (private embed: PeerTubeEmbed) {
40 this.constructChannel()
41 this.setupStateTracking()
48 private get element () {
49 return this.embed.videoElement
52 private constructChannel () {
53 let channel = Channel.build({ window: window.parent, origin: '*', scope: this.embed.scope })
55 channel.bind('play', (txn, params) => this.embed.player.play())
56 channel.bind('pause', (txn, params) => this.embed.player.pause())
57 channel.bind('seek', (txn, time) => this.embed.player.currentTime(time))
58 channel.bind('setVolume', (txn, value) => this.embed.player.volume(value))
59 channel.bind('getVolume', (txn, value) => this.embed.player.volume())
60 channel.bind('isReady', (txn, params) => this.isReady)
61 channel.bind('setResolution', (txn, resolutionId) => this.setResolution(resolutionId))
62 channel.bind('getResolutions', (txn, params) => this.resolutions)
63 channel.bind('setPlaybackRate', (txn, playbackRate) => this.embed.player.playbackRate(playbackRate))
64 channel.bind('getPlaybackRate', (txn, params) => this.embed.player.playbackRate())
65 channel.bind('getPlaybackRates', (txn, params) => this.embed.playerOptions.playbackRates)
67 this.channel = channel
70 private setResolution (resolutionId: number) {
71 if (resolutionId === -1 && this.embed.player.peertube().isAutoResolutionForbidden()) return
74 if (resolutionId === -1) {
75 this.embed.player.peertube().enableAutoResolution()
79 this.embed.player.peertube().disableAutoResolution()
80 this.embed.player.peertube().updateResolution(resolutionId)
84 * Let the host know that we're ready to go!
86 private notifyReady () {
88 this.channel.notify({ method: 'ready', params: true })
91 private setupStateTracking () {
92 let currentState: 'playing' | 'paused' | 'unstarted' = 'unstarted'
95 let position = this.element.currentTime
96 let volume = this.element.volume
99 method: 'playbackStatusUpdate',
103 playbackState: currentState
108 this.element.addEventListener('play', ev => {
109 currentState = 'playing'
110 this.channel.notify({ method: 'playbackStatusChange', params: 'playing' })
113 this.element.addEventListener('pause', ev => {
114 currentState = 'paused'
115 this.channel.notify({ method: 'playbackStatusChange', params: 'paused' })
118 // PeerTube specific capabilities
120 this.embed.player.peertube().on('autoResolutionUpdate', () => this.loadResolutions())
121 this.embed.player.peertube().on('videoFileUpdate', () => this.loadResolutions())
124 private loadResolutions () {
126 let currentResolutionId = this.embed.player.peertube().getCurrentResolutionId()
128 for (const videoFile of this.embed.player.peertube().videoFiles) {
129 let label = videoFile.resolution.label
130 if (videoFile.fps && videoFile.fps >= 50) {
131 label += videoFile.fps
135 id: videoFile.resolution.id,
137 src: videoFile.magnetUri,
138 active: videoFile.resolution.id === currentResolutionId
142 this.resolutions = resolutions
143 this.channel.notify({
144 method: 'resolutionUpdate',
145 params: this.resolutions
150 class PeerTubeEmbed {
151 videoElement: HTMLVideoElement
154 api: PeerTubeEmbedApi = null
163 static async main () {
164 const videoContainerId = 'video-container'
165 const embed = new PeerTubeEmbed(videoContainerId)
169 constructor (private videoContainerId: string) {
170 this.videoElement = document.getElementById(videoContainerId) as HTMLVideoElement
173 getVideoUrl (id: string) {
174 return window.location.origin + '/api/v1/videos/' + id
177 loadVideoInfo (videoId: string): Promise<Response> {
178 return fetch(this.getVideoUrl(videoId))
181 removeElement (element: HTMLElement) {
182 element.parentElement.removeChild(element)
185 displayError (videoElement: HTMLVideoElement, text: string) {
186 // Remove video element
187 this.removeElement(videoElement)
189 document.title = 'Sorry - ' + text
191 const errorBlock = document.getElementById('error-block')
192 errorBlock.style.display = 'flex'
194 const errorText = document.getElementById('error-content')
195 errorText.innerHTML = text
198 videoNotFound (videoElement: HTMLVideoElement) {
199 const text = 'This video does not exist.'
200 this.displayError(videoElement, text)
203 videoFetchError (videoElement: HTMLVideoElement) {
204 const text = 'We cannot fetch the video. Please try again later.'
205 this.displayError(videoElement, text)
208 getParamToggle (params: URLSearchParams, name: string, defaultValue: boolean) {
209 return params.has(name) ? (params.get(name) === '1' || params.get(name) === 'true') : defaultValue
212 getParamString (params: URLSearchParams, name: string, defaultValue: string) {
213 return params.has(name) ? params.get(name) : defaultValue
218 await this.initCore()
224 private initializeApi () {
225 if (!this.enableApi) return
227 this.api = new PeerTubeEmbedApi(this)
228 this.api.initialize()
231 private loadParams () {
233 let params = new URL(window.location.toString()).searchParams
235 this.autoplay = this.getParamToggle(params, 'autoplay', this.autoplay)
236 this.controls = this.getParamToggle(params, 'controls', this.controls)
237 this.muted = this.getParamToggle(params, 'muted', this.muted)
238 this.loop = this.getParamToggle(params, 'loop', this.loop)
239 this.enableApi = this.getParamToggle(params, 'api', this.enableApi)
240 this.scope = this.getParamString(params, 'scope', this.scope)
242 const startTimeParamString = params.get('start')
243 const startTimeParamNumber = parseInt(startTimeParamString, 10)
245 if (isNaN(startTimeParamNumber) === false) this.startTime = startTimeParamNumber
247 console.error('Cannot get params from URL.', err)
251 private async initCore () {
252 const urlParts = window.location.href.split('/')
253 const lastPart = urlParts[ urlParts.length - 1 ]
254 const videoId = lastPart.indexOf('?') === -1 ? lastPart : lastPart.split('?')[ 0 ]
256 await loadLocale(window.location.origin, vjs, navigator.language)
257 let response = await this.loadVideoInfo(videoId)
260 if (response.status === 404) return this.videoNotFound(this.videoElement)
262 return this.videoFetchError(this.videoElement)
265 const videoInfo: VideoDetails = await response.json()
269 const videojsOptions = getVideojsOptions({
270 autoplay: this.autoplay,
271 controls: this.controls,
274 startTime: this.startTime,
276 inactivityTimeout: 1500,
277 videoViewUrl: this.getVideoUrl(videoId) + '/views',
278 playerElement: this.videoElement,
279 videoFiles: videoInfo.files,
280 videoDuration: videoInfo.duration,
283 poster: window.location.origin + videoInfo.previewPath,
287 this.playerOptions = videojsOptions
288 this.player = vjs(this.videoContainerId, videojsOptions, () => {
290 window[ 'videojsPlayer' ] = this.player
294 title: videoInfo.name,
295 description: this.player.localize('Uses P2P, others may know your IP is downloading this video.')
299 addContextMenu(this.player, window.location.origin + videoInfo.embedPath)
306 .catch(err => console.error('Cannot init embed.', err))