import { VideoDetails } from '../../../../shared'
import { addContextMenu, getVideojsOptions, loadLocale } from '../../assets/player/peertube-player'
-import { PeerTubeResolution } from '../player/definitions';
+import { PeerTubeResolution } from '../player/definitions'
/**
- * Embed API exposes control of the embed player to the outside world via
+ * Embed API exposes control of the embed player to the outside world via
* JSChannels and window.postMessage
*/
class PeerTubeEmbedApi {
- constructor(
- private embed : PeerTubeEmbed
- ) {
- }
-
- private channel : Channel.MessagingChannel
+ private channel: Channel.MessagingChannel
private isReady = false
- private resolutions : PeerTubeResolution[] = null
+ private resolutions: PeerTubeResolution[] = null
+
+ constructor (private embed: PeerTubeEmbed) {
+ }
- initialize() {
+ initialize () {
this.constructChannel()
this.setupStateTracking()
this.notifyReady()
}
-
- private get element() {
+
+ private get element () {
return this.embed.videoElement
}
- private constructChannel() {
+ private constructChannel () {
let channel = Channel.build({ window: window.parent, origin: '*', scope: this.embed.scope })
-
+
channel.bind('play', (txn, params) => this.embed.player.play())
channel.bind('pause', (txn, params) => this.embed.player.pause())
channel.bind('seek', (txn, time) => this.embed.player.currentTime(time))
this.channel = channel
}
- private setResolution(resolutionId : number) {
- if (resolutionId === -1 && this.embed.player.peertube().isAutoResolutionForbidden())
- return
+ private setResolution (resolutionId: number) {
+ if (resolutionId === -1 && this.embed.player.peertube().isAutoResolutionForbidden()) return
// Auto resolution
if (resolutionId === -1) {
/**
* Let the host know that we're ready to go!
*/
- private notifyReady() {
+ private notifyReady () {
this.isReady = true
this.channel.notify({ method: 'ready', params: true })
}
- private setupStateTracking() {
-
- let currentState : 'playing' | 'paused' | 'unstarted' = 'unstarted'
+ private setupStateTracking () {
+ let currentState: 'playing' | 'paused' | 'unstarted' = 'unstarted'
setInterval(() => {
let position = this.element.currentTime
params: {
position,
volume,
- playbackState: currentState,
+ playbackState: currentState
}
})
}, 500)
this.embed.player.peertube().on('videoFileUpdate', () => this.loadResolutions())
}
- private loadResolutions() {
+ private loadResolutions () {
let resolutions = []
let currentResolutionId = this.embed.player.peertube().getCurrentResolutionId()
}
class PeerTubeEmbed {
- constructor(
- private videoContainerId : string
- ) {
- this.videoElement = document.getElementById(videoContainerId) as HTMLVideoElement
- }
-
- videoElement : HTMLVideoElement
- player : any
- playerOptions : any
- api : PeerTubeEmbedApi = null
- autoplay : boolean = false
- controls : boolean = true
- muted : boolean = false
- loop : boolean = false
- enableApi : boolean = false
- startTime : number = 0
- scope : string = 'peertube'
-
- static async main() {
+ videoElement: HTMLVideoElement
+ player: any
+ playerOptions: any
+ api: PeerTubeEmbedApi = null
+ autoplay = false
+ controls = true
+ muted = false
+ loop = false
+ enableApi = false
+ startTime = 0
+ scope = 'peertube'
+
+ static async main () {
const videoContainerId = 'video-container'
const embed = new PeerTubeEmbed(videoContainerId)
await embed.init()
}
-
+
+ constructor (private videoContainerId: string) {
+ this.videoElement = document.getElementById(videoContainerId) as HTMLVideoElement
+ }
+
getVideoUrl (id: string) {
return window.location.origin + '/api/v1/videos/' + id
}
return params.has(name) ? params.get(name) : defaultValue
}
- private initializeApi() {
- if (!this.enableApi)
- return
-
- this.api = new PeerTubeEmbedApi(this)
- this.api.initialize()
- }
-
- async init() {
+ async init () {
try {
await this.initCore()
} catch (e) {
}
}
- private loadParams() {
+ private initializeApi () {
+ if (!this.enableApi) return
+
+ this.api = new PeerTubeEmbedApi(this)
+ this.api.initialize()
+ }
+
+ private loadParams () {
try {
let params = new URL(window.location.toString()).searchParams
const startTimeParamString = params.get('start')
const startTimeParamNumber = parseInt(startTimeParamString, 10)
- if (isNaN(startTimeParamNumber) === false)
- this.startTime = startTimeParamNumber
+
+ if (isNaN(startTimeParamNumber) === false) this.startTime = startTimeParamNumber
} catch (err) {
console.error('Cannot get params from URL.', err)
}
}
- private async initCore() {
+ private async initCore () {
const urlParts = window.location.href.split('/')
- const lastPart = urlParts[urlParts.length - 1]
- const videoId = lastPart.indexOf('?') === -1 ? lastPart : lastPart.split('?')[0]
+ const lastPart = urlParts[ urlParts.length - 1 ]
+ const videoId = lastPart.indexOf('?') === -1 ? lastPart : lastPart.split('?')[ 0 ]
await loadLocale(window.location.origin, vjs, navigator.language)
let response = await this.loadVideoInfo(videoId)
if (!response.ok) {
- if (response.status === 404)
- return this.videoNotFound(this.videoElement)
+ if (response.status === 404) return this.videoNotFound(this.videoElement)
return this.videoFetchError(this.videoElement)
}
controls: this.controls,
muted: this.muted,
loop: this.loop,
- startTime : this.startTime,
+ startTime: this.startTime,
inactivityTimeout: 1500,
videoViewUrl: this.getVideoUrl(videoId) + '/views',
this.playerOptions = videojsOptions
this.player = vjs(this.videoContainerId, videojsOptions, () => {
- window['videojsPlayer'] = this.player
+ window[ 'videojsPlayer' ] = this.player
if (this.controls) {
- (this.player as any).dock({
+ this.player.dock({
title: videoInfo.name,
description: this.player.localize('Uses P2P, others may know your IP is downloading this video.')
})
}
+
addContextMenu(this.player, window.location.origin + videoInfo.embedPath)
this.initializeApi()
})
}
PeerTubeEmbed.main()
+ .catch(err => console.error('Cannot init embed.', err))