1 // Big thanks to: https://github.com/kmoskwiak/videojs-resolution-switcher
3 import * as videojs from 'video.js'
4 import * as WebTorrent from 'webtorrent'
5 import { VideoConstant, VideoResolution } from '../../../../shared/models/videos'
6 import { VideoFile } from '../../../../shared/models/videos/video.model'
7 import { renderVideo } from './video-renderer'
9 declare module 'video.js' {
11 peertube (): PeerTubePlugin
15 interface VideoJSComponentInterface {
16 _player: videojs.Player
18 new (player: videojs.Player, options?: any)
20 registerComponent (name: string, obj: any)
23 type PeertubePluginOptions = {
24 videoFiles: VideoFile[]
25 playerElement: HTMLVideoElement
30 // https://github.com/danrevah/ngx-pipes/blob/master/src/pipes/math/bytes.ts
31 // Don't import all Angular stuff, just copy the code with shame
32 const dictionaryBytes: Array<{max: number, type: string}> = [
33 { max: 1024, type: 'B' },
34 { max: 1048576, type: 'KB' },
35 { max: 1073741824, type: 'MB' },
36 { max: 1.0995116e12, type: 'GB' }
38 function bytes (value) {
39 const format = dictionaryBytes.find(d => value < d.max) || dictionaryBytes[dictionaryBytes.length - 1]
40 const calc = Math.floor(value / (format.max / 1024)).toString()
42 return [ calc, format.type ]
45 // videojs typings don't have some method we need
46 const videojsUntyped = videojs as any
47 const webtorrent = new WebTorrent({
52 urls: 'stun:stun.stunprotocol.org'
55 urls: 'stun:stun.framasoft.org'
63 const MenuItem: VideoJSComponentInterface = videojsUntyped.getComponent('MenuItem')
64 class ResolutionMenuItem extends MenuItem {
66 constructor (player: videojs.Player, options) {
67 options.selectable = true
68 super(player, options)
70 const currentResolutionId = this.player_.peertube().getCurrentResolutionId()
71 this.selected(this.options_.id === currentResolutionId)
75 super.handleClick(event)
77 this.player_.peertube().updateResolution(this.options_.id)
80 MenuItem.registerComponent('ResolutionMenuItem', ResolutionMenuItem)
82 const MenuButton: VideoJSComponentInterface = videojsUntyped.getComponent('MenuButton')
83 class ResolutionMenuButton extends MenuButton {
86 constructor (player: videojs.Player, options) {
87 options.label = 'Quality'
88 super(player, options)
90 this.label = document.createElement('span')
92 this.el().setAttribute('aria-label', 'Quality')
93 this.controlText('Quality')
95 videojsUntyped.dom.addClass(this.label, 'vjs-resolution-button-label')
96 this.el().appendChild(this.label)
98 player.peertube().on('videoFileUpdate', () => this.update())
103 for (const videoFile of this.player_.peertube().videoFiles) {
104 menuItems.push(new ResolutionMenuItem(
107 id: videoFile.resolution.id,
108 label: videoFile.resolution.label,
109 src: videoFile.magnetUri,
110 selected: videoFile.resolution.id === this.currentSelectionId
119 if (!this.label) return
121 this.label.innerHTML = this.player_.peertube().getCurrentResolutionLabel()
123 return super.update()
127 return super.buildCSSClass() + ' vjs-resolution-button'
130 MenuButton.registerComponent('ResolutionMenuButton', ResolutionMenuButton)
132 const Button: VideoJSComponentInterface = videojsUntyped.getComponent('Button')
133 class PeerTubeLinkButton extends Button {
136 const link = document.createElement('a')
137 link.href = window.location.href.replace('embed', 'watch')
138 link.innerHTML = 'PeerTube'
139 link.title = 'Go to the video page'
140 link.className = 'vjs-peertube-link'
141 link.target = '_blank'
150 Button.registerComponent('PeerTubeLinkButton', PeerTubeLinkButton)
152 class WebTorrentButton extends Button {
154 const div = document.createElement('div')
155 const subDivWebtorrent = document.createElement('div')
156 div.appendChild(subDivWebtorrent)
158 const downloadIcon = document.createElement('span')
159 downloadIcon.classList.add('icon', 'icon-download')
160 subDivWebtorrent.appendChild(downloadIcon)
162 const downloadSpeedText = document.createElement('span')
163 downloadSpeedText.classList.add('download-speed-text')
164 const downloadSpeedNumber = document.createElement('span')
165 downloadSpeedNumber.classList.add('download-speed-number')
166 const downloadSpeedUnit = document.createElement('span')
167 downloadSpeedText.appendChild(downloadSpeedNumber)
168 downloadSpeedText.appendChild(downloadSpeedUnit)
169 subDivWebtorrent.appendChild(downloadSpeedText)
171 const uploadIcon = document.createElement('span')
172 uploadIcon.classList.add('icon', 'icon-upload')
173 subDivWebtorrent.appendChild(uploadIcon)
175 const uploadSpeedText = document.createElement('span')
176 uploadSpeedText.classList.add('upload-speed-text')
177 const uploadSpeedNumber = document.createElement('span')
178 uploadSpeedNumber.classList.add('upload-speed-number')
179 const uploadSpeedUnit = document.createElement('span')
180 uploadSpeedText.appendChild(uploadSpeedNumber)
181 uploadSpeedText.appendChild(uploadSpeedUnit)
182 subDivWebtorrent.appendChild(uploadSpeedText)
184 const peersText = document.createElement('span')
185 peersText.classList.add('peers-text')
186 const peersNumber = document.createElement('span')
187 peersNumber.classList.add('peers-number')
188 subDivWebtorrent.appendChild(peersNumber)
189 subDivWebtorrent.appendChild(peersText)
191 div.className = 'vjs-peertube'
192 // Hide the stats before we get the info
193 subDivWebtorrent.className = 'vjs-peertube-hidden'
195 const subDivHttp = document.createElement('div')
196 subDivHttp.className = 'vjs-peertube-hidden'
197 const subDivHttpText = document.createElement('span')
198 subDivHttpText.classList.add('peers-number')
199 subDivHttpText.textContent = 'HTTP'
200 const subDivFallbackText = document.createElement('span')
201 subDivFallbackText.classList.add('peers-text')
202 subDivFallbackText.textContent = ' fallback'
204 subDivHttp.appendChild(subDivHttpText)
205 subDivHttp.appendChild(subDivFallbackText)
206 div.appendChild(subDivHttp)
208 this.player_.peertube().on('torrentInfo', (event, data) => {
209 // We are in HTTP fallback
211 subDivHttp.className = 'vjs-peertube-displayed'
212 subDivWebtorrent.className = 'vjs-peertube-hidden'
217 const downloadSpeed = bytes(data.downloadSpeed)
218 const uploadSpeed = bytes(data.uploadSpeed)
219 const numPeers = data.numPeers
221 downloadSpeedNumber.textContent = downloadSpeed[ 0 ]
222 downloadSpeedUnit.textContent = ' ' + downloadSpeed[ 1 ]
224 uploadSpeedNumber.textContent = uploadSpeed[ 0 ]
225 uploadSpeedUnit.textContent = ' ' + uploadSpeed[ 1 ]
227 peersNumber.textContent = numPeers
228 peersText.textContent = ' peers'
230 subDivHttp.className = 'vjs-peertube-hidden'
231 subDivWebtorrent.className = 'vjs-peertube-displayed'
237 Button.registerComponent('WebTorrentButton', WebTorrentButton)
239 const Plugin: VideoJSComponentInterface = videojsUntyped.getPlugin('plugin')
240 class PeerTubePlugin extends Plugin {
242 private currentVideoFile: VideoFile
243 private playerElement: HTMLVideoElement
244 private videoFiles: VideoFile[]
245 private torrent: WebTorrent.Torrent
246 private autoplay = false
247 private videoViewUrl: string
248 private videoDuration: number
249 private videoViewInterval
250 private torrentInfoInterval
251 private savePlayerSrcFunction: Function
253 constructor (player: videojs.Player, options: PeertubePluginOptions) {
254 super(player, options)
256 // Fix canplay event on google chrome by disabling default videojs autoplay
257 this.autoplay = this.player.options_.autoplay
258 this.player.options_.autoplay = false
260 this.videoFiles = options.videoFiles
261 this.videoViewUrl = options.videoViewUrl
262 this.videoDuration = options.videoDuration
264 this.savePlayerSrcFunction = this.player.src
265 // Hack to "simulate" src link in video.js >= 6
266 // Without this, we can't play the video after pausing it
267 // https://github.com/videojs/video.js/blob/master/src/js/player.js#L1633
268 this.player.src = () => true
270 this.playerElement = options.playerElement
272 this.player.ready(() => {
273 this.initializePlayer()
274 this.runTorrentInfoScheduler()
280 clearInterval(this.videoViewInterval)
281 clearInterval(this.torrentInfoInterval)
283 // Don't need to destroy renderer, video player will be destroyed
284 this.flushVideoFile(this.currentVideoFile, false)
287 getCurrentResolutionId () {
288 return this.currentVideoFile ? this.currentVideoFile.resolution.id : -1
291 getCurrentResolutionLabel () {
292 return this.currentVideoFile ? this.currentVideoFile.resolution.label : ''
295 updateVideoFile (videoFile?: VideoFile, done?: () => void) {
296 if (done === undefined) {
297 done = () => { /* empty */ }
300 // Pick the first one
301 if (videoFile === undefined) {
302 videoFile = this.videoFiles[0]
305 // Don't add the same video file once again
306 if (this.currentVideoFile !== undefined && this.currentVideoFile.magnetUri === videoFile.magnetUri) {
310 // Do not display error to user because we will have multiple fallbacks
311 this.disableErrorDisplay()
313 this.player.src = () => true
314 this.player.playbackRate(1)
316 const previousVideoFile = this.currentVideoFile
317 this.currentVideoFile = videoFile
319 this.addTorrent(this.currentVideoFile.magnetUri, previousVideoFile, done)
321 this.trigger('videoFileUpdate')
324 addTorrent (magnetOrTorrentUrl: string, previousVideoFile: VideoFile, done: Function) {
325 console.log('Adding ' + magnetOrTorrentUrl + '.')
327 this.torrent = webtorrent.add(magnetOrTorrentUrl, torrent => {
328 console.log('Added ' + magnetOrTorrentUrl + '.')
330 this.flushVideoFile(previousVideoFile)
332 const options = { autoplay: true, controls: true }
333 renderVideo(torrent.files[0], this.playerElement, options,(err, renderer) => {
334 this.renderer = renderer
336 if (err) return this.fallbackToHttp()
338 if (!this.player.paused()) {
339 const playPromise = this.player.play()
340 if (playPromise !== undefined) return playPromise.then(done)
349 this.torrent.on('error', err => this.handleError(err))
351 this.torrent.on('warning', (err: any) => {
352 // We don't support HTTP tracker but we don't care -> we use the web socket tracker
353 if (err.message.indexOf('Unsupported tracker protocol') !== -1) return
355 // Users don't care about issues with WebRTC, but developers do so log it in the console
356 if (err.message.indexOf('Ice connection failed') !== -1) {
361 // Magnet hash is not up to date with the torrent file, add directly the torrent file
362 if (err.message.indexOf('incorrect info hash') !== -1) {
363 console.error('Incorrect info hash detected, falling back to torrent file.')
364 return this.addTorrent(this.torrent['xs'], previousVideoFile, done)
367 return this.handleError(err)
371 updateResolution (resolutionId: number) {
372 // Remember player state
373 const currentTime = this.player.currentTime()
374 const isPaused = this.player.paused()
376 // Remove poster to have black background
377 this.playerElement.poster = ''
379 // Hide bigPlayButton
381 this.player.bigPlayButton.hide()
384 const newVideoFile = this.videoFiles.find(f => f.resolution.id === resolutionId)
385 this.updateVideoFile(newVideoFile, () => {
386 this.player.currentTime(currentTime)
387 this.player.handleTechSeeked_()
391 flushVideoFile (videoFile: VideoFile, destroyRenderer = true) {
392 if (videoFile !== undefined && webtorrent.get(videoFile.magnetUri)) {
393 if (destroyRenderer === true && this.renderer && this.renderer.destroy) this.renderer.destroy()
395 webtorrent.remove(videoFile.magnetUri)
396 console.log('Removed ' + videoFile.magnetUri)
400 setVideoFiles (files: VideoFile[], videoViewUrl: string, videoDuration: number) {
401 this.videoViewUrl = videoViewUrl
402 this.videoDuration = videoDuration
403 this.videoFiles = files
405 // Re run view add for the new video
407 this.updateVideoFile(undefined, () => this.player.play())
410 private initializePlayer () {
411 if (this.autoplay === true) {
412 this.updateVideoFile(undefined, () => this.player.play())
414 this.player.one('play', () => {
416 this.updateVideoFile(undefined, () => this.player.play())
421 private runTorrentInfoScheduler () {
422 this.torrentInfoInterval = setInterval(() => {
423 // Not initialized yet
424 if (this.torrent === undefined) return
427 if (this.torrent === null) return this.trigger('torrentInfo', false)
429 return this.trigger('torrentInfo', {
430 downloadSpeed: this.torrent.downloadSpeed,
431 numPeers: this.torrent.numPeers,
432 uploadSpeed: this.torrent.uploadSpeed
437 private runViewAdd () {
438 this.clearVideoViewInterval()
440 // After 30 seconds (or 3/4 of the video), add a view to the video
441 let minSecondsToView = 30
443 if (this.videoDuration < minSecondsToView) minSecondsToView = (this.videoDuration * 3) / 4
445 let secondsViewed = 0
446 this.videoViewInterval = setInterval(() => {
447 if (this.player && !this.player.paused()) {
450 if (secondsViewed > minSecondsToView) {
451 this.clearVideoViewInterval()
453 this.addViewToVideo().catch(err => console.error(err))
459 private clearVideoViewInterval () {
460 if (this.videoViewInterval !== undefined) {
461 clearInterval(this.videoViewInterval)
462 this.videoViewInterval = undefined
466 private addViewToVideo () {
467 return fetch(this.videoViewUrl, { method: 'POST' })
470 private fallbackToHttp () {
471 this.flushVideoFile(this.currentVideoFile, true)
474 // Enable error display now this is our last fallback
475 this.player.one('error', () => this.enableErrorDisplay())
477 const httpUrl = this.currentVideoFile.fileUrl
478 this.player.src = this.savePlayerSrcFunction
479 this.player.src(httpUrl)
483 private handleError (err: Error | string) {
484 return this.player.trigger('customError', { err })
487 private enableErrorDisplay () {
488 this.player.addClass('vjs-error-display-enabled')
491 private disableErrorDisplay () {
492 this.player.removeClass('vjs-error-display-enabled')
495 videojsUntyped.registerPlugin('peertube', PeerTubePlugin)