Refractor peertube videojs plugin

pull/200/head
Chocobozzz 2018-01-09 15:29:05 +01:00
parent 378557ef25
commit a22bfc3ebc
No known key found for this signature in database
GPG Key ID: 583A612D890159BE
6 changed files with 166 additions and 137 deletions

1
.gitignore vendored
View File

@ -22,3 +22,4 @@
/.idea /.idea
/PeerTube.iml /PeerTube.iml
peertube.zip peertube.zip
/profiling/

View File

@ -95,8 +95,10 @@ export class VideoAddComponent extends FormReactive implements OnInit {
uploadFirstStep () { uploadFirstStep () {
const videofile = this.videofileInput.nativeElement.files[0] const videofile = this.videofileInput.nativeElement.files[0]
if (!videofile) return
const videoQuota = this.authService.getUser().videoQuota const videoQuota = this.authService.getUser().videoQuota
if ((this.userVideoQuotaUsed + videofile.size) > videoQuota) { if (videoQuota !== -1 && (this.userVideoQuotaUsed + videofile.size) > videoQuota) {
const bytePipes = new BytesPipe() const bytePipes = new BytesPipe()
const msg = 'Your video quota is exceeded with this video ' + const msg = 'Your video quota is exceeded with this video ' +

View File

@ -326,7 +326,6 @@ export class VideoWatchComponent implements OnInit, OnDestroy {
peertube: { peertube: {
videoFiles: this.video.files, videoFiles: this.video.files,
playerElement: this.playerElement, playerElement: this.playerElement,
autoplay: this.isAutoplay(),
peerTubeLink: false peerTubeLink: false
} }
} }

View File

@ -6,6 +6,24 @@ import { VideoFile } from '../../../../shared/models/videos/video.model'
import { renderVideo } from './video-renderer' import { renderVideo } from './video-renderer'
interface VideoJSComponentInterface {
_player: VideoJSPlayer
new (player: VideoJSPlayer, options?: any)
registerComponent (name: string, obj: any)
}
interface VideoJSPlayer extends videojs.Player {
peertube (): PeerTubePlugin
}
type PeertubePluginOptions = {
videoFiles: VideoFile[]
playerElement: HTMLVideoElement
peerTubeLink: boolean
}
// https://github.com/danrevah/ngx-pipes/blob/master/src/pipes/math/bytes.ts // https://github.com/danrevah/ngx-pipes/blob/master/src/pipes/math/bytes.ts
// Don't import all Angular stuff, just copy the code with shame // Don't import all Angular stuff, just copy the code with shame
const dictionaryBytes: Array<{max: number, type: string}> = [ const dictionaryBytes: Array<{max: number, type: string}> = [
@ -25,42 +43,46 @@ function bytes (value) {
const videojsUntyped = videojs as any const videojsUntyped = videojs as any
const webtorrent = new WebTorrent({ dht: false }) const webtorrent = new WebTorrent({ dht: false })
const MenuItem = videojsUntyped.getComponent('MenuItem') const MenuItem: VideoJSComponentInterface = videojsUntyped.getComponent('MenuItem')
const ResolutionMenuItem = videojsUntyped.extend(MenuItem, { class ResolutionMenuItem extends MenuItem {
constructor: function (player: videojs.Player, options) {
constructor (player: VideoJSPlayer, options) {
options.selectable = true options.selectable = true
MenuItem.call(this, player, options) super(player, options)
const currentResolution = this.player_.getCurrentResolution() const currentResolution = this.player_.peertube().getCurrentResolution()
this.selected(this.options_.id === currentResolution) this.selected(this.options_.id === currentResolution)
},
handleClick: function (event) {
MenuItem.prototype.handleClick.call(this, event)
this.player_.updateResolution(this.options_.id)
} }
})
handleClick (event) {
MenuItem.prototype.handleClick.call(this, event)
this.player_.peertube().updateResolution(this.options_.id)
}
}
MenuItem.registerComponent('ResolutionMenuItem', ResolutionMenuItem) MenuItem.registerComponent('ResolutionMenuItem', ResolutionMenuItem)
const MenuButton = videojsUntyped.getComponent('MenuButton') const MenuButton: VideoJSComponentInterface = videojsUntyped.getComponent('MenuButton')
const ResolutionMenuButton = videojsUntyped.extend(MenuButton, { class ResolutionMenuButton extends MenuButton {
constructor: function (player, options) { label: HTMLElement
this.label = document.createElement('span')
options.label = 'Quality' constructor (player: VideoJSPlayer, options) {
options.label = 'Quality'
super(player, options)
this.label = document.createElement('span')
MenuButton.call(this, player, options)
this.el().setAttribute('aria-label', 'Quality') this.el().setAttribute('aria-label', 'Quality')
this.controlText('Quality') this.controlText('Quality')
videojsUntyped.dom.addClass(this.label, 'vjs-resolution-button-label') videojsUntyped.dom.addClass(this.label, 'vjs-resolution-button-label')
this.el().appendChild(this.label) this.el().appendChild(this.label)
player.on('videoFileUpdate', videojs.bind(this, this.update)) player.peertube().on('videoFileUpdate', () => this.update())
}, }
createItems: function () { createItems () {
const menuItems = [] const menuItems = []
for (const videoFile of this.player_.videoFiles) { for (const videoFile of this.player_.peertube().videoFiles) {
menuItems.push(new ResolutionMenuItem( menuItems.push(new ResolutionMenuItem(
this.player_, this.player_,
{ {
@ -73,27 +95,30 @@ const ResolutionMenuButton = videojsUntyped.extend(MenuButton, {
} }
return menuItems return menuItems
},
update: function () {
this.label.innerHTML = this.player_.getCurrentResolutionLabel()
this.hide()
return MenuButton.prototype.update.call(this)
},
buildCSSClass: function () {
return MenuButton.prototype.buildCSSClass.call(this) + ' vjs-resolution-button'
} }
})
update () {
if (!this.label) return
this.label.innerHTML = this.player_.peertube().getCurrentResolutionLabel()
this.hide()
return super.update()
}
buildCSSClass () {
return super.buildCSSClass() + ' vjs-resolution-button'
}
dispose () {
this.parentNode.removeChild(this)
}
}
MenuButton.registerComponent('ResolutionMenuButton', ResolutionMenuButton) MenuButton.registerComponent('ResolutionMenuButton', ResolutionMenuButton)
const Button = videojsUntyped.getComponent('Button') const Button: VideoJSComponentInterface = videojsUntyped.getComponent('Button')
const PeertubeLinkButton = videojsUntyped.extend(Button, { class PeertubeLinkButton extends Button {
constructor: function (player) {
Button.call(this, player)
},
createEl: function () { createEl () {
const link = document.createElement('a') const link = document.createElement('a')
link.href = window.location.href.replace('embed', 'watch') link.href = window.location.href.replace('embed', 'watch')
link.innerHTML = 'PeerTube' link.innerHTML = 'PeerTube'
@ -102,20 +127,20 @@ const PeertubeLinkButton = videojsUntyped.extend(Button, {
link.target = '_blank' link.target = '_blank'
return link return link
}, }
handleClick: function () { handleClick () {
this.player_.pause() this.player_.pause()
} }
})
dispose () {
this.parentNode.removeChild(this)
}
}
Button.registerComponent('PeerTubeLinkButton', PeertubeLinkButton) Button.registerComponent('PeerTubeLinkButton', PeertubeLinkButton)
const WebTorrentButton = videojsUntyped.extend(Button, { class WebTorrentButton extends Button {
constructor: function (player) { createEl () {
Button.call(this, player)
},
createEl: function () {
const div = document.createElement('div') const div = document.createElement('div')
const subDiv = document.createElement('div') const subDiv = document.createElement('div')
div.appendChild(subDiv) div.appendChild(subDiv)
@ -158,7 +183,7 @@ const WebTorrentButton = videojsUntyped.extend(Button, {
// Hide the stats before we get the info // Hide the stats before we get the info
subDiv.className = 'vjs-webtorrent-hidden' subDiv.className = 'vjs-webtorrent-hidden'
this.player_.on('torrentInfo', (event, data) => { this.player_.peertube().on('torrentInfo', (event, data) => {
const downloadSpeed = bytes(data.downloadSpeed) const downloadSpeed = bytes(data.downloadSpeed)
const uploadSpeed = bytes(data.uploadSpeed) const uploadSpeed = bytes(data.uploadSpeed)
const numPeers = data.numPeers const numPeers = data.numPeers
@ -176,71 +201,89 @@ const WebTorrentButton = videojsUntyped.extend(Button, {
return div return div
} }
})
dispose () {
this.parentNode.removeChild(this)
}
}
Button.registerComponent('WebTorrentButton', WebTorrentButton) Button.registerComponent('WebTorrentButton', WebTorrentButton)
type PeertubePluginOptions = { const Plugin: VideoJSComponentInterface = videojsUntyped.getPlugin('plugin')
videoFiles: VideoFile[] class PeerTubePlugin extends Plugin {
playerElement: HTMLVideoElement private player: any
autoplay: boolean private currentVideoFile: VideoFile
peerTubeLink: boolean private playerElement: HTMLVideoElement
} private videoFiles: VideoFile[]
const peertubePlugin = function (options: PeertubePluginOptions) { private torrent: WebTorrent.Torrent
const player = this
let currentVideoFile: VideoFile = undefined
const playerElement = options.playerElement
player.videoFiles = options.videoFiles
// Hack to "simulate" src link in video.js >= 6 constructor (player: VideoJSPlayer, options: PeertubePluginOptions) {
// Without this, we can't play the video after pausing it super(player, options)
// https://github.com/videojs/video.js/blob/master/src/js/player.js#L1633
player.src = function () { this.videoFiles = options.videoFiles
return true
// Hack to "simulate" src link in video.js >= 6
// Without this, we can't play the video after pausing it
// https://github.com/videojs/video.js/blob/master/src/js/player.js#L1633
this.player.src = function () {
return true
}
this.playerElement = options.playerElement
this.player.ready(() => {
this.initializePlayer(options)
this.runTorrentInfoScheduler()
})
} }
player.getCurrentResolution = function () { dispose () {
return currentVideoFile ? currentVideoFile.resolution : -1 // Don't need to destroy renderer, video player will be destroyed
this.flushVideoFile(this.currentVideoFile, false)
} }
player.getCurrentResolutionLabel = function () { getCurrentResolution () {
return currentVideoFile ? currentVideoFile.resolutionLabel : '' return this.currentVideoFile ? this.currentVideoFile.resolution : -1
} }
player.updateVideoFile = function (videoFile: VideoFile, done: () => void) { getCurrentResolutionLabel () {
return this.currentVideoFile ? this.currentVideoFile.resolutionLabel : ''
}
updateVideoFile (videoFile?: VideoFile, done?: () => void) {
if (done === undefined) { if (done === undefined) {
done = () => { /* empty */ } done = () => { /* empty */ }
} }
// Pick the first one // Pick the first one
if (videoFile === undefined) { if (videoFile === undefined) {
videoFile = player.videoFiles[0] videoFile = this.videoFiles[0]
} }
// Don't add the same video file once again // Don't add the same video file once again
if (currentVideoFile !== undefined && currentVideoFile.magnetUri === videoFile.magnetUri) { if (this.currentVideoFile !== undefined && this.currentVideoFile.magnetUri === videoFile.magnetUri) {
return return
} }
const previousVideoFile = currentVideoFile const previousVideoFile = this.currentVideoFile
currentVideoFile = videoFile this.currentVideoFile = videoFile
console.log('Adding ' + videoFile.magnetUri + '.') console.log('Adding ' + videoFile.magnetUri + '.')
player.torrent = webtorrent.add(videoFile.magnetUri, torrent => { this.torrent = webtorrent.add(videoFile.magnetUri, torrent => {
console.log('Added ' + videoFile.magnetUri + '.') console.log('Added ' + videoFile.magnetUri + '.')
this.flushVideoFile(previousVideoFile) this.flushVideoFile(previousVideoFile)
const options = { autoplay: true, controls: true } const options = { autoplay: true, controls: true }
renderVideo(torrent.files[0], playerElement, options,(err, renderer) => { renderVideo(torrent.files[0], this.playerElement, options,(err, renderer) => {
if (err) return handleError(err) if (err) return this.handleError(err)
this.renderer = renderer this.renderer = renderer
player.play().then(done) this.player.play().then(done)
}) })
}) })
player.torrent.on('error', err => handleError(err)) this.torrent.on('error', err => this.handleError(err))
player.torrent.on('warning', err => { this.torrent.on('warning', (err: any) => {
// We don't support HTTP tracker but we don't care -> we use the web socket tracker // We don't support HTTP tracker but we don't care -> we use the web socket tracker
if (err.message.indexOf('Unsupported tracker protocol: http') !== -1) return if (err.message.indexOf('Unsupported tracker protocol: http') !== -1) return
// Users don't care about issues with WebRTC, but developers do so log it in the console // Users don't care about issues with WebRTC, but developers do so log it in the console
@ -249,103 +292,88 @@ const peertubePlugin = function (options: PeertubePluginOptions) {
return return
} }
return handleError(err) return this.handleError(err)
}) })
player.trigger('videoFileUpdate') this.trigger('videoFileUpdate')
return player
} }
player.updateResolution = function (resolution) { updateResolution (resolution) {
// Remember player state // Remember player state
const currentTime = player.currentTime() const currentTime = this.player.currentTime()
const isPaused = player.paused() const isPaused = this.player.paused()
// Hide bigPlayButton // Hide bigPlayButton
if (!isPaused) { if (!isPaused) {
this.player_.bigPlayButton.hide() this.player.bigPlayButton.hide()
} }
const newVideoFile = player.videoFiles.find(f => f.resolution === resolution) const newVideoFile = this.videoFiles.find(f => f.resolution === resolution)
player.updateVideoFile(newVideoFile, () => { this.updateVideoFile(newVideoFile, () => {
player.currentTime(currentTime) this.player.currentTime(currentTime)
player.handleTechSeeked_() this.player.handleTechSeeked_()
}) })
} }
player.flushVideoFile = function (videoFile: VideoFile, destroyRenderer = true) { flushVideoFile (videoFile: VideoFile, destroyRenderer = true) {
if (videoFile !== undefined && webtorrent.get(videoFile.magnetUri)) { if (videoFile !== undefined && webtorrent.get(videoFile.magnetUri)) {
if (destroyRenderer === true) this.renderer.destroy() if (destroyRenderer === true) this.renderer.destroy()
webtorrent.remove(videoFile.magnetUri) webtorrent.remove(videoFile.magnetUri)
console.log('Removed ' + videoFile.magnetUri)
} }
} }
player.setVideoFiles = function (files: VideoFile[]) { setVideoFiles (files: VideoFile[]) {
player.videoFiles = files this.videoFiles = files
player.updateVideoFile(undefined, () => player.play()) this.updateVideoFile(undefined, () => this.player.play())
} }
player.ready(function () { private initializePlayer (options: PeertubePluginOptions) {
const controlBar = player.controlBar const controlBar = this.player.controlBar
const menuButton = new ResolutionMenuButton(player, options) const menuButton = new ResolutionMenuButton(this.player, options)
const fullscreenElement = controlBar.fullscreenToggle.el() const fullscreenElement = controlBar.fullscreenToggle.el()
controlBar.resolutionSwitcher = controlBar.el().insertBefore(menuButton.el(), fullscreenElement) controlBar.resolutionSwitcher = controlBar.el().insertBefore(menuButton.el(), fullscreenElement)
controlBar.resolutionSwitcher.dispose = function () {
this.parentNode.removeChild(this)
}
player.dispose = function () {
// Don't need to destroy renderer, video player will be destroyed
player.flushVideoFile(currentVideoFile, false)
}
if (options.peerTubeLink === true) { if (options.peerTubeLink === true) {
const peerTubeLinkButton = new PeertubeLinkButton(player) const peerTubeLinkButton = new PeertubeLinkButton(this.player)
controlBar.peerTubeLink = controlBar.el().insertBefore(peerTubeLinkButton.el(), fullscreenElement) controlBar.peerTubeLink = controlBar.el().insertBefore(peerTubeLinkButton.el(), fullscreenElement)
controlBar.peerTubeLink.dispose = function () {
this.parentNode.removeChild(this)
}
} }
const webTorrentButton = new WebTorrentButton(player) const webTorrentButton = new WebTorrentButton(this.player)
controlBar.webTorrent = controlBar.el().insertBefore(webTorrentButton.el(), controlBar.progressControl.el()) controlBar.webTorrent = controlBar.el().insertBefore(webTorrentButton.el(), controlBar.progressControl.el())
controlBar.webTorrent.dispose = function () {
this.parentNode.removeChild(this)
}
if (options.autoplay === true) { if (this.player.options_.autoplay === true) {
player.updateVideoFile() this.updateVideoFile()
} else { } else {
player.one('play', () => { this.player.one('play', () => {
// On firefox, we need to wait to load the video before playing // On firefox, we need to wait to load the video before playing
if (navigator.userAgent.toLowerCase().indexOf('firefox') !== -1) { if (navigator.userAgent.toLowerCase().indexOf('firefox') !== -1) {
player.pause() this.player.pause()
player.updateVideoFile(undefined, () => player.play()) this.updateVideoFile(undefined, () => this.player.play())
return return
} }
player.updateVideoFile(undefined) this.updateVideoFile(undefined)
}) })
} }
}
private runTorrentInfoScheduler () {
setInterval(() => { setInterval(() => {
if (player.torrent !== undefined) { if (this.torrent !== undefined) {
player.trigger('torrentInfo', { this.trigger('torrentInfo', {
downloadSpeed: player.torrent.downloadSpeed, downloadSpeed: this.torrent.downloadSpeed,
numPeers: player.torrent.numPeers, numPeers: this.torrent.numPeers,
uploadSpeed: player.torrent.uploadSpeed uploadSpeed: this.torrent.uploadSpeed
}) })
} }
}, 1000) }, 1000)
}) }
function handleError (err: Error | string) { private handleError (err: Error | string) {
return player.trigger('customError', { err }) return this.player.trigger('customError', { err })
} }
} }
videojsUntyped.registerPlugin('peertube', PeerTubePlugin)
videojsUntyped.registerPlugin('peertube', peertubePlugin)

View File

@ -41,7 +41,6 @@ loadVideoInfo(videoId, (err, videoInfo) => {
peertube: { peertube: {
videoFiles: videoInfo.files, videoFiles: videoInfo.files,
playerElement: videoElement, playerElement: videoElement,
autoplay: false,
peerTubeLink: true peerTubeLink: true
} }
} }

View File

@ -12,5 +12,5 @@ signup:
enabled: true enabled: true
transcoding: transcoding:
enabled: false enabled: true
threads: 4 threads: 2