From 999417328bde0e60cd59318fc1c18672356254ce Mon Sep 17 00:00:00 2001 From: William Lahti Date: Tue, 10 Jul 2018 08:47:56 -0700 Subject: Ability to programmatically control embeds (#776) * first stab at jschannel based player api * semicolon purge * more method-level docs; consolidate definitions * missing definitions * better match peertube's class conventions * styling for embed tester * basic docs * add `getVolume` * document the test-embed feature --- client/src/standalone/videos/embed.ts | 306 ++++++++++++++++++++++----- client/src/standalone/videos/test-embed.html | 51 +++++ client/src/standalone/videos/test-embed.scss | 149 +++++++++++++ client/src/standalone/videos/test-embed.ts | 98 +++++++++ 4 files changed, 549 insertions(+), 55 deletions(-) create mode 100644 client/src/standalone/videos/test-embed.html create mode 100644 client/src/standalone/videos/test-embed.scss create mode 100644 client/src/standalone/videos/test-embed.ts (limited to 'client/src/standalone/videos') diff --git a/client/src/standalone/videos/embed.ts b/client/src/standalone/videos/embed.ts index d8db2a119..e9baf64d0 100644 --- a/client/src/standalone/videos/embed.ts +++ b/client/src/standalone/videos/embed.ts @@ -17,100 +17,296 @@ import 'core-js/es6/set' // For google bot that uses Chrome 41 and does not understand fetch import 'whatwg-fetch' -import * as videojs from 'video.js' +import * as vjs from 'video.js' +import * as Channel from 'jschannel' import { VideoDetails } from '../../../../shared' import { addContextMenu, getVideojsOptions, loadLocale } from '../../assets/player/peertube-player' +import { PeerTubeResolution } from '../player/definitions'; -function getVideoUrl (id: string) { - return window.location.origin + '/api/v1/videos/' + id -} +/** + * Embed API exposes control of the embed player to the outside world via + * JSChannels and window.postMessage + */ +class PeerTubeEmbedApi { + constructor( + private embed : PeerTubeEmbed + ) { + } -function loadVideoInfo (videoId: string): Promise { - return fetch(getVideoUrl(videoId)) -} + private channel : Channel.MessagingChannel + private isReady = false + private resolutions : PeerTubeResolution[] = null -function removeElement (element: HTMLElement) { - element.parentElement.removeChild(element) -} + initialize() { + this.constructChannel() + this.setupStateTracking() -function displayError (videoElement: HTMLVideoElement, text: string) { - // Remove video element - removeElement(videoElement) + // We're ready! - document.title = 'Sorry - ' + text + this.notifyReady() + } + + private get element() { + return this.embed.videoElement + } - const errorBlock = document.getElementById('error-block') - errorBlock.style.display = 'flex' + 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)) + channel.bind('setVolume', (txn, value) => this.embed.player.volume(value)) + channel.bind('getVolume', (txn, value) => this.embed.player.volume()) + channel.bind('isReady', (txn, params) => this.isReady) + channel.bind('setResolution', (txn, resolutionId) => this.setResolution(resolutionId)) + channel.bind('getResolutions', (txn, params) => this.resolutions) + channel.bind('setPlaybackRate', (txn, playbackRate) => this.embed.player.playbackRate(playbackRate)) + channel.bind('getPlaybackRate', (txn, params) => this.embed.player.playbackRate()) + channel.bind('getPlaybackRates', (txn, params) => this.embed.playerOptions.playbackRates) - const errorText = document.getElementById('error-content') - errorText.innerHTML = text -} + this.channel = channel + } -function videoNotFound (videoElement: HTMLVideoElement) { - const text = 'This video does not exist.' - displayError(videoElement, text) -} + private setResolution(resolutionId : number) { + if (resolutionId === -1 && this.embed.player.peertube().isAutoResolutionForbidden()) + return + + // Auto resolution + if (resolutionId === -1) { + this.embed.player.peertube().enableAutoResolution() + return + } + + this.embed.player.peertube().disableAutoResolution() + this.embed.player.peertube().updateResolution(resolutionId) + } + + /** + * Let the host know that we're ready to go! + */ + private notifyReady() { + this.isReady = true + this.channel.notify({ method: 'ready', params: true }) + } + + private setupStateTracking() { + + let currentState : 'playing' | 'paused' | 'unstarted' = 'unstarted' + + setInterval(() => { + let position = this.element.currentTime + let volume = this.element.volume + + this.channel.notify({ + method: 'playbackStatusUpdate', + params: { + position, + volume, + playbackState: currentState, + } + }) + }, 500) + + this.element.addEventListener('play', ev => { + currentState = 'playing' + this.channel.notify({ method: 'playbackStatusChange', params: 'playing' }) + }) + + this.element.addEventListener('pause', ev => { + currentState = 'paused' + this.channel.notify({ method: 'playbackStatusChange', params: 'paused' }) + }) + + // PeerTube specific capabilities + + this.embed.player.peertube().on('autoResolutionUpdate', () => this.loadResolutions()) + this.embed.player.peertube().on('videoFileUpdate', () => this.loadResolutions()) + } + + private loadResolutions() { + let resolutions = [] + let currentResolutionId = this.embed.player.peertube().getCurrentResolutionId() + + for (const videoFile of this.embed.player.peertube().videoFiles) { + let label = videoFile.resolution.label + if (videoFile.fps && videoFile.fps >= 50) { + label += videoFile.fps + } -function videoFetchError (videoElement: HTMLVideoElement) { - const text = 'We cannot fetch the video. Please try again later.' - displayError(videoElement, text) + resolutions.push({ + id: videoFile.resolution.id, + label, + src: videoFile.magnetUri, + active: videoFile.resolution.id === currentResolutionId + }) + } + + this.resolutions = resolutions + this.channel.notify({ + method: 'resolutionUpdate', + params: this.resolutions + }) + } } -const urlParts = window.location.href.split('/') -const lastPart = urlParts[urlParts.length - 1] -const videoId = lastPart.indexOf('?') === -1 ? lastPart : lastPart.split('?')[0] +class PeerTubeEmbed { + constructor( + private videoContainerId : string + ) { + this.videoElement = document.getElementById(videoContainerId) as HTMLVideoElement + } -loadLocale(window.location.origin, videojs, navigator.language) - .then(() => loadVideoInfo(videoId)) - .then(async response => { + 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() { const videoContainerId = 'video-container' - const videoElement = document.getElementById(videoContainerId) as HTMLVideoElement + const embed = new PeerTubeEmbed(videoContainerId) + await embed.init() + } + + getVideoUrl (id: string) { + return window.location.origin + '/api/v1/videos/' + id + } - if (!response.ok) { - if (response.status === 404) return videoNotFound(videoElement) + loadVideoInfo (videoId: string): Promise { + return fetch(this.getVideoUrl(videoId)) + } - return videoFetchError(videoElement) - } + removeElement (element: HTMLElement) { + element.parentElement.removeChild(element) + } - const videoInfo: VideoDetails = await response.json() + displayError (videoElement: HTMLVideoElement, text: string) { + // Remove video element + this.removeElement(videoElement) + + document.title = 'Sorry - ' + text + + const errorBlock = document.getElementById('error-block') + errorBlock.style.display = 'flex' + + const errorText = document.getElementById('error-content') + errorText.innerHTML = text + } + + videoNotFound (videoElement: HTMLVideoElement) { + const text = 'This video does not exist.' + this.displayError(videoElement, text) + } + + videoFetchError (videoElement: HTMLVideoElement) { + const text = 'We cannot fetch the video. Please try again later.' + this.displayError(videoElement, text) + } + + getParamToggle (params: URLSearchParams, name: string, defaultValue: boolean) { + return params.has(name) ? (params.get(name) === '1' || params.get(name) === 'true') : defaultValue + } - let autoplay = false - let startTime = 0 + getParamString (params: URLSearchParams, name: string, defaultValue: string) { + return params.has(name) ? params.get(name) : defaultValue + } + private initializeApi() { + if (!this.enableApi) + return + + this.api = new PeerTubeEmbedApi(this) + this.api.initialize() + } + + async init() { + try { + await this.initCore() + } catch (e) { + console.error(e) + } + } + + private loadParams() { try { let params = new URL(window.location.toString()).searchParams - autoplay = params.has('autoplay') && (params.get('autoplay') === '1' || params.get('autoplay') === 'true') + + this.autoplay = this.getParamToggle(params, 'autoplay', this.autoplay) + this.controls = this.getParamToggle(params, 'controls', this.controls) + this.muted = this.getParamToggle(params, 'muted', this.muted) + this.loop = this.getParamToggle(params, 'loop', this.loop) + this.enableApi = this.getParamToggle(params, 'api', this.enableApi) + this.scope = this.getParamString(params, 'scope', this.scope) const startTimeParamString = params.get('start') const startTimeParamNumber = parseInt(startTimeParamString, 10) - if (isNaN(startTimeParamNumber) === false) startTime = startTimeParamNumber + if (isNaN(startTimeParamNumber) === false) + this.startTime = startTimeParamNumber } catch (err) { console.error('Cannot get params from URL.', err) } + } + + private async initCore() { + const urlParts = window.location.href.split('/') + 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) + + return this.videoFetchError(this.videoElement) + } + + const videoInfo: VideoDetails = await response.json() + + this.loadParams() const videojsOptions = getVideojsOptions({ - autoplay, + autoplay: this.autoplay, + controls: this.controls, + muted: this.muted, + loop: this.loop, + startTime : this.startTime, + inactivityTimeout: 1500, - videoViewUrl: getVideoUrl(videoId) + '/views', - playerElement: videoElement, + videoViewUrl: this.getVideoUrl(videoId) + '/views', + playerElement: this.videoElement, videoFiles: videoInfo.files, videoDuration: videoInfo.duration, enableHotkeys: true, peertubeLink: true, poster: window.location.origin + videoInfo.previewPath, - startTime, theaterMode: false }) - videojs(videoContainerId, videojsOptions, function () { - const player = this - player.dock({ - title: videoInfo.name, - description: player.localize('Uses P2P, others may know your IP is downloading this video.') - }) + this.playerOptions = videojsOptions + this.player = vjs(this.videoContainerId, videojsOptions, () => { - addContextMenu(player, window.location.origin + videoInfo.embedPath) + window['videojsPlayer'] = this.player + + if (this.controls) { + (this.player as any).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() }) - }) - .catch(err => console.error(err)) + } +} + +PeerTubeEmbed.main() diff --git a/client/src/standalone/videos/test-embed.html b/client/src/standalone/videos/test-embed.html new file mode 100644 index 000000000..a60ba2899 --- /dev/null +++ b/client/src/standalone/videos/test-embed.html @@ -0,0 +1,51 @@ + + + + + +
+ + +
+

Embed Playground

+
+
+ +
+
+ + + +
+
+ +
+
+ Resolutions: +
+
+
+ +
+ Rates: +
+
+
+ +
+
+ + + + + + diff --git a/client/src/standalone/videos/test-embed.scss b/client/src/standalone/videos/test-embed.scss new file mode 100644 index 000000000..df3d69f21 --- /dev/null +++ b/client/src/standalone/videos/test-embed.scss @@ -0,0 +1,149 @@ + +* { + font-family: sans-serif; +} + +html { + width: 100%; + overflow-x: hidden; + overflow-y: auto; +} + +body { + margin: 0; + padding: 0; +} + +iframe { + border: none; + border-radius: 8px; + min-width: 200px; + width: 100%; + height: 100%; + pointer-events: none; +} + +aside { + width: 33vw; + margin: 0 .5em .5em 0; + height: calc(33vw * 0.5625); +} + +.logo { + font-size: 150%; + height: 100%; + font-weight: bold; + display: flex; + flex-direction: row; + align-items: center; + margin-right: 0.5em; + + .icon { + height: 100%; + padding: 0 18px 0 32px; + background: white; + display: flex; + align-items: center; + margin-right: 0.5em; + } +} + +main { + padding: 0 1em; + display: flex; + align-items: flex-start; +} + +.spacer { + flex: 1; +} + +header { + width: 100%; + height: 3.2em; + background-color: #F1680D; + color: white; + //background-image: url(../../assets/images/backdrop/network-o.png); + display: flex; + flex-direction: row; + align-items: center; + margin-bottom: 1em; + box-shadow: 1px 0px 10px rgba(0,0,0,0.6); + background-size: 50%; + background-position: top left; + padding-right: 1em; + + h1 { + margin: 0; + padding: 0 1em 0 0; + font-size: inherit; + font-weight: 100; + position: relative; + top: 2px; + } +} + +#options { + display: flex; + flex-wrap: wrap; + + & > * { + flex-grow: 0; + } +} + +fieldset { + border: none; + min-width: 8em; + legend { + border-bottom: 1px solid #ccc; + width: 100%; + } +} + +button { + background: #F1680D; + color: white; + font-weight: bold; + border-radius: 5px; + margin: 0; + padding: 1em 1.25em; + border: none; +} + +a { + text-decoration: none; + + &:hover { + text-decoration: underline; + } + + &, &:hover, &:focus, &:visited, &:active { + color: #F44336; + } +} + +@media (max-width: 900px) { + aside { + width: 50vw; + height: calc(50vw * 0.5625); + } +} + +@media (max-width: 600px) { + main { + flex-direction: column; + } + + aside { + width: calc(100vw - 2em); + height: calc(56.25vw - 2em * 0.5625); + } +} + +@media (min-width: 1800px) { + aside { + width: 50vw; + height: calc(50vw * 0.5625); + } +} \ No newline at end of file diff --git a/client/src/standalone/videos/test-embed.ts b/client/src/standalone/videos/test-embed.ts new file mode 100644 index 000000000..721514488 --- /dev/null +++ b/client/src/standalone/videos/test-embed.ts @@ -0,0 +1,98 @@ +import './test-embed.scss' +import { PeerTubePlayer } from '../player/player'; +import { PlayerEventType } from '../player/definitions'; + +window.addEventListener('load', async () => { + + const urlParts = window.location.href.split('/') + const lastPart = urlParts[urlParts.length - 1] + const videoId = lastPart.indexOf('?') === -1 ? lastPart : lastPart.split('?')[0] + + let iframe = document.createElement('iframe') + iframe.src = `/videos/embed/${videoId}?autoplay=1&controls=0&api=1` + let mainElement = document.querySelector('#host') + mainElement.appendChild(iframe); + + console.log(`Document finished loading.`) + let player = new PeerTubePlayer(document.querySelector('iframe')) + + window['player'] = player + + console.log(`Awaiting player ready...`) + await player.ready + console.log(`Player is ready.`) + + let monitoredEvents = [ + 'pause', 'play', + 'playbackStatusUpdate', + 'playbackStatusChange' + ] + + monitoredEvents.forEach(e => { + player.addEventListener(e, () => console.log(`PLAYER: event '${e}' received`)) + console.log(`PLAYER: now listening for event '${e}'`) + }) + + let playbackRates = [] + let activeRate = 1 + let currentRate = await player.getPlaybackRate() + + let updateRates = async () => { + + let rateListEl = document.querySelector('#rate-list') + rateListEl.innerHTML = '' + + playbackRates.forEach(rate => { + if (currentRate == rate) { + let itemEl = document.createElement('strong') + itemEl.innerText = `${rate} (active)` + itemEl.style.display = 'block' + rateListEl.appendChild(itemEl) + } else { + let itemEl = document.createElement('a') + itemEl.href = 'javascript:;' + itemEl.innerText = rate + itemEl.addEventListener('click', () => { + player.setPlaybackRate(rate) + currentRate = rate + updateRates() + }) + itemEl.style.display = 'block' + rateListEl.appendChild(itemEl) + } + }) + } + + player.getPlaybackRates().then(rates => { + playbackRates = rates + updateRates() + }) + + let updateResolutions = resolutions => { + let resolutionListEl = document.querySelector('#resolution-list') + resolutionListEl.innerHTML = '' + + resolutions.forEach(resolution => { + if (resolution.active) { + let itemEl = document.createElement('strong') + itemEl.innerText = `${resolution.label} (active)` + itemEl.style.display = 'block' + resolutionListEl.appendChild(itemEl) + } else { + let itemEl = document.createElement('a') + itemEl.href = 'javascript:;' + itemEl.innerText = resolution.label + itemEl.addEventListener('click', () => { + player.setResolution(resolution.id) + }) + itemEl.style.display = 'block' + resolutionListEl.appendChild(itemEl) + } + }) + } + + player.getResolutions().then( + resolutions => updateResolutions(resolutions)) + player.addEventListener('resolutionUpdate', + resolutions => updateResolutions(resolutions)) +}) \ No newline at end of file -- cgit v1.2.3