1 import videojs from 'video.js'
2 import * as WebTorrent from 'webtorrent'
3 import { renderVideo } from './video-renderer'
4 import { LoadedQualityData, PlayerNetworkInfo, WebtorrentPluginOptions } from '../peertube-videojs-typings'
5 import { getRtcConfig, timeToInt, videoFileMaxByResolution, videoFileMinByResolution, isIOS, isSafari } from '../utils'
6 import { PeertubeChunkStore } from './peertube-chunk-store'
8 getAverageBandwidthInStore,
13 } from '../peertube-player-local-storage'
14 import { VideoFile } from '@shared/models'
16 const CacheChunkStore = require('cache-chunk-store')
24 const Plugin = videojs.getPlugin('plugin')
26 class WebTorrentPlugin extends Plugin {
27 readonly videoFiles: VideoFile[]
29 private readonly playerElement: HTMLVideoElement
31 private readonly autoplay: boolean = false
32 private readonly startTime: number = 0
33 private readonly savePlayerSrcFunction: videojs.Player['src']
34 private readonly videoDuration: number
35 private readonly CONSTANTS = {
36 INFO_SCHEDULER: 1000, // Don't change this
37 AUTO_QUALITY_SCHEDULER: 3000, // Check quality every 3 seconds
38 AUTO_QUALITY_THRESHOLD_PERCENT: 30, // Bandwidth should be 30% more important than a resolution bitrate to change to it
39 AUTO_QUALITY_OBSERVATION_TIME: 10000, // Wait 10 seconds after having change the resolution before another check
40 AUTO_QUALITY_HIGHER_RESOLUTION_DELAY: 5000, // Buffering higher resolution during 5 seconds
41 BANDWIDTH_AVERAGE_NUMBER_OF_VALUES: 5 // Last 5 seconds to build average bandwidth
44 private readonly webtorrent = new WebTorrent({
46 rtcConfig: getRtcConfig()
51 private currentVideoFile: VideoFile
52 private torrent: WebTorrent.Torrent
55 private fakeRenderer: any
56 private destroyingFakeRenderer = false
58 private autoResolution = true
59 private autoResolutionPossible = true
60 private isAutoResolutionObservation = false
61 private playerRefusedP2P = false
63 private torrentInfoInterval: any
64 private autoQualityInterval: any
65 private addTorrentDelay: any
66 private qualityObservationTimer: any
67 private runAutoQualitySchedulerTimer: any
69 private downloadSpeeds: number[] = []
71 constructor (player: videojs.Player, options?: WebtorrentPluginOptions) {
74 this.startTime = timeToInt(options.startTime)
76 // Disable auto play on iOS
77 this.autoplay = options.autoplay
78 this.playerRefusedP2P = !getStoredP2PEnabled()
80 this.videoFiles = options.videoFiles
81 this.videoDuration = options.videoDuration
83 this.savePlayerSrcFunction = this.player.src
84 this.playerElement = options.playerElement
86 this.player.ready(() => {
87 const playerOptions = this.player.options_
89 const volume = getStoredVolume()
90 if (volume !== undefined) this.player.volume(volume)
92 const muted = playerOptions.muted !== undefined ? playerOptions.muted : getStoredMute()
93 if (muted !== undefined) this.player.muted(muted)
95 this.player.duration(options.videoDuration)
97 this.initializePlayer()
98 this.runTorrentInfoScheduler()
100 this.player.one('play', () => {
101 // Don't run immediately scheduler, wait some seconds the TCP connections are made
102 this.runAutoQualitySchedulerTimer = setTimeout(() => this.runAutoQualityScheduler(), this.CONSTANTS.AUTO_QUALITY_SCHEDULER)
108 clearTimeout(this.addTorrentDelay)
109 clearTimeout(this.qualityObservationTimer)
110 clearTimeout(this.runAutoQualitySchedulerTimer)
112 clearInterval(this.torrentInfoInterval)
113 clearInterval(this.autoQualityInterval)
115 // Don't need to destroy renderer, video player will be destroyed
116 this.flushVideoFile(this.currentVideoFile, false)
118 this.destroyFakeRenderer()
121 getCurrentResolutionId () {
122 return this.currentVideoFile ? this.currentVideoFile.resolution.id : -1
125 private getVideoFile(): VideoFile {
126 const savedAverageBandwidth = getAverageBandwidthInStore()
127 return savedAverageBandwidth
128 ? this.getAppropriateFile(savedAverageBandwidth)
129 : this.pickAverageVideoFile()
133 videoFile: VideoFile,
139 done: () => void = () => { /* empty */ }
141 if (videoFile === undefined) {
142 throw Error(`Can't update video file since videoFile is undefined.`)
145 // Don't add the same video file once again
146 if (this.currentVideoFile !== undefined && this.currentVideoFile.magnetUri === videoFile.magnetUri) {
150 // Do not display error to user because we will have multiple fallback
151 this.disableErrorDisplay();
153 // Hack to "simulate" src link in video.js >= 6
154 // Without this, we can't play the video after pausing it
155 // https://github.com/videojs/video.js/blob/master/src/js/player.js#L1633
156 (this.player as any).src = () => true
157 const oldPlaybackRate = this.player.playbackRate()
159 const previousVideoFile = this.currentVideoFile
160 this.currentVideoFile = videoFile
162 // Don't try on iOS that does not support MediaSource
163 // Or don't use P2P if webtorrent is disabled
164 if (isIOS() || this.playerRefusedP2P) {
165 return this.fallbackToHttp(options, () => {
166 this.player.playbackRate(oldPlaybackRate)
171 this.addTorrent(this.currentVideoFile.magnetUri, previousVideoFile, options, () => {
172 this.player.playbackRate(oldPlaybackRate)
177 this.trigger('resolutionChange', { auto: this.autoResolution, resolutionId: this.currentVideoFile.resolution.id })
180 updateResolution (resolutionId: number, delay = 0) {
181 // Remember player state
182 const currentTime = this.player.currentTime()
183 const isPaused = this.player.paused()
185 // Hide bigPlayButton
187 this.player.bigPlayButton.hide()
190 // Audio-only (resolutionId === 0) gets special treatment
191 if (resolutionId === 0) {
192 // Audio-only: show poster, do not auto-hide controls
193 this.player.addClass('vjs-playing-audio-only-content')
194 this.player.posterImage.show()
196 // Hide poster to have black background
197 this.player.removeClass('vjs-playing-audio-only-content')
198 this.player.posterImage.hide()
201 const newVideoFile = this.videoFiles.find(f => f.resolution.id === resolutionId)
205 seek: currentTime + (delay / 1000)
208 this.updateVideoFile(newVideoFile, options)
211 flushVideoFile (videoFile: VideoFile, destroyRenderer = true) {
212 if (videoFile !== undefined && this.webtorrent.get(videoFile.magnetUri)) {
213 if (destroyRenderer === true && this.renderer && this.renderer.destroy) this.renderer.destroy()
215 this.webtorrent.remove(videoFile.magnetUri)
216 console.log('Removed ' + videoFile.magnetUri)
220 enableAutoResolution () {
221 this.autoResolution = true
222 this.trigger('resolutionChange', { auto: this.autoResolution, resolutionId: this.getCurrentResolutionId() })
225 disableAutoResolution (forbid = false) {
226 if (forbid === true) this.autoResolutionPossible = false
228 this.autoResolution = false
229 this.trigger('autoResolutionChange', { possible: this.autoResolutionPossible })
230 this.trigger('resolutionChange', { auto: this.autoResolution, resolutionId: this.getCurrentResolutionId() })
233 isAutoResolutionPossible () {
234 return this.autoResolutionPossible
241 getCurrentVideoFile () {
242 return this.currentVideoFile
246 magnetOrTorrentUrl: string,
247 previousVideoFile: VideoFile,
248 options: PlayOptions,
251 console.log('Adding ' + magnetOrTorrentUrl + '.')
253 const oldTorrent = this.torrent
254 const torrentOptions = {
255 // Don't use arrow function: it breaks webtorrent (that uses `new` keyword)
256 store: function (chunkLength: number, storeOpts: any) {
257 return new CacheChunkStore(new PeertubeChunkStore(chunkLength, storeOpts), {
263 this.torrent = this.webtorrent.add(magnetOrTorrentUrl, torrentOptions, torrent => {
264 console.log('Added ' + magnetOrTorrentUrl + '.')
267 // Pause the old torrent
268 this.stopTorrent(oldTorrent)
270 // We use a fake renderer so we download correct pieces of the next file
271 if (options.delay) this.renderFileInFakeElement(torrent.files[ 0 ], options.delay)
274 // Render the video in a few seconds? (on resolution change for example, we wait some seconds of the new video resolution)
275 this.addTorrentDelay = setTimeout(() => {
276 // We don't need the fake renderer anymore
277 this.destroyFakeRenderer()
279 const paused = this.player.paused()
281 this.flushVideoFile(previousVideoFile)
283 // Update progress bar (just for the UI), do not wait rendering
284 if (options.seek) this.player.currentTime(options.seek)
286 const renderVideoOptions = { autoplay: false, controls: true }
287 renderVideo(torrent.files[ 0 ], this.playerElement, renderVideoOptions, (err, renderer) => {
288 this.renderer = renderer
290 if (err) return this.fallbackToHttp(options, done)
292 return this.tryToPlay(err => {
293 if (err) return done(err)
295 if (options.seek) this.seek(options.seek)
296 if (options.forcePlay === false && paused === true) this.player.pause()
301 }, options.delay || 0)
304 this.torrent.on('error', (err: any) => console.error(err))
306 this.torrent.on('warning', (err: any) => {
307 // We don't support HTTP tracker but we don't care -> we use the web socket tracker
308 if (err.message.indexOf('Unsupported tracker protocol') !== -1) return
310 // Users don't care about issues with WebRTC, but developers do so log it in the console
311 if (err.message.indexOf('Ice connection failed') !== -1) {
316 // Magnet hash is not up to date with the torrent file, add directly the torrent file
317 if (err.message.indexOf('incorrect info hash') !== -1) {
318 console.error('Incorrect info hash detected, falling back to torrent file.')
319 const newOptions = { forcePlay: true, seek: options.seek }
320 return this.addTorrent(this.torrent[ 'xs' ], previousVideoFile, newOptions, done)
323 // Remote instance is down
324 if (err.message.indexOf('from xs param') !== -1) {
325 this.handleError(err)
332 private tryToPlay (done?: (err?: Error) => void) {
333 if (!done) done = function () { /* empty */ }
335 const playPromise = this.player.play()
336 if (playPromise !== undefined) {
337 return playPromise.then(() => done())
338 .catch((err: Error) => {
339 if (err.message.indexOf('The play() request was interrupted by a call to pause()') !== -1) {
345 this.player.posterImage.show()
346 this.player.removeClass('vjs-has-autoplay')
347 this.player.removeClass('vjs-has-big-play-button-clicked')
348 this.player.removeClass('vjs-playing-audio-only-content')
357 private seek (time: number) {
358 this.player.currentTime(time)
359 this.player.handleTechSeeked_()
362 private getAppropriateFile (averageDownloadSpeed?: number): VideoFile {
363 if (this.videoFiles === undefined) return undefined
365 const files = this.videoFiles.filter(f => f.resolution.id !== 0)
367 if (files.length === 0) return undefined
368 if (files.length === 1) return files[0]
370 // Don't change the torrent if the player ended
371 if (this.torrent && this.torrent.progress === 1 && this.player.ended()) return this.currentVideoFile
373 if (!averageDownloadSpeed) averageDownloadSpeed = this.getAndSaveActualDownloadSpeed()
375 // Limit resolution according to player height
376 const playerHeight = this.playerElement.offsetHeight
378 // We take the first resolution just above the player height
379 // Example: player height is 530px, we want the 720p file instead of 480p
380 let maxResolution = files[0].resolution.id
381 for (let i = files.length - 1; i >= 0; i--) {
382 const resolutionId = files[i].resolution.id
383 if (resolutionId !== 0 && resolutionId >= playerHeight) {
384 maxResolution = resolutionId
389 // Filter videos we can play according to our screen resolution and bandwidth
390 const filteredFiles = files.filter(f => f.resolution.id <= maxResolution)
392 const fileBitrate = (f.size / this.videoDuration)
393 let threshold = fileBitrate
395 // If this is for a higher resolution or an initial load: add a margin
396 if (!this.currentVideoFile || f.resolution.id > this.currentVideoFile.resolution.id) {
397 threshold += ((fileBitrate * this.CONSTANTS.AUTO_QUALITY_THRESHOLD_PERCENT) / 100)
400 return averageDownloadSpeed > threshold
403 // If the download speed is too bad, return the lowest resolution we have
404 if (filteredFiles.length === 0) return videoFileMinByResolution(files)
406 return videoFileMaxByResolution(filteredFiles)
409 private getAndSaveActualDownloadSpeed () {
410 const start = Math.max(this.downloadSpeeds.length - this.CONSTANTS.BANDWIDTH_AVERAGE_NUMBER_OF_VALUES, 0)
411 const lastDownloadSpeeds = this.downloadSpeeds.slice(start, this.downloadSpeeds.length)
412 if (lastDownloadSpeeds.length === 0) return -1
414 const sum = lastDownloadSpeeds.reduce((a, b) => a + b)
415 const averageBandwidth = Math.round(sum / lastDownloadSpeeds.length)
417 // Save the average bandwidth for future use
418 saveAverageBandwidth(averageBandwidth)
420 return averageBandwidth
423 private initializePlayer () {
424 this.buildQualities()
427 this.player.posterImage.hide()
429 return this.updateVideoFile(this.getVideoFile(), { forcePlay: true, seek: this.startTime })
433 const oldPlay = this.player.play.bind(this.player);
434 (this.player as any).play = () => {
435 this.player.addClass('vjs-has-big-play-button-clicked')
436 this.player.play = oldPlay
438 this.updateVideoFile(this.getVideoFile(), { forcePlay: true, seek: this.startTime })
442 private runAutoQualityScheduler () {
443 this.autoQualityInterval = setInterval(() => {
445 // Not initialized or in HTTP fallback
446 if (this.torrent === undefined || this.torrent === null) return
447 if (this.autoResolution === false) return
448 if (this.isAutoResolutionObservation === true) return
450 const file = this.getAppropriateFile()
451 let changeResolution = false
452 let changeResolutionDelay = 0
455 if (this.isPlayerWaiting() && file.resolution.id < this.currentVideoFile.resolution.id) {
456 console.log('Downgrading automatically the resolution to: %s', file.resolution.label)
457 changeResolution = true
458 } else if (file.resolution.id > this.currentVideoFile.resolution.id) { // Higher resolution
459 console.log('Upgrading automatically the resolution to: %s', file.resolution.label)
460 changeResolution = true
461 changeResolutionDelay = this.CONSTANTS.AUTO_QUALITY_HIGHER_RESOLUTION_DELAY
464 if (changeResolution === true) {
465 this.updateResolution(file.resolution.id, changeResolutionDelay)
467 // Wait some seconds in observation of our new resolution
468 this.isAutoResolutionObservation = true
470 this.qualityObservationTimer = setTimeout(() => {
471 this.isAutoResolutionObservation = false
472 }, this.CONSTANTS.AUTO_QUALITY_OBSERVATION_TIME)
474 }, this.CONSTANTS.AUTO_QUALITY_SCHEDULER)
477 private isPlayerWaiting () {
478 return this.player && this.player.hasClass('vjs-waiting')
481 private runTorrentInfoScheduler () {
482 this.torrentInfoInterval = setInterval(() => {
483 // Not initialized yet
484 if (this.torrent === undefined) return
487 if (this.torrent === null) return this.player.trigger('p2pInfo', false)
489 // this.webtorrent.downloadSpeed because we need to take into account the potential old torrent too
490 if (this.webtorrent.downloadSpeed !== 0) this.downloadSpeeds.push(this.webtorrent.downloadSpeed)
492 return this.player.trigger('p2pInfo', {
493 source: 'webtorrent',
501 downloadSpeed: this.torrent.downloadSpeed,
502 numPeers: this.torrent.numPeers,
503 uploadSpeed: this.torrent.uploadSpeed,
504 downloaded: this.torrent.downloaded,
505 uploaded: this.torrent.uploaded
507 } as PlayerNetworkInfo)
508 }, this.CONSTANTS.INFO_SCHEDULER)
511 private fallbackToHttp (options: PlayOptions, done?: Function) {
512 const paused = this.player.paused()
514 this.disableAutoResolution(true)
516 this.flushVideoFile(this.currentVideoFile, true)
519 // Enable error display now this is our last fallback
520 this.player.one('error', () => this.enableErrorDisplay())
522 const httpUrl = this.currentVideoFile.fileUrl
523 this.player.src = this.savePlayerSrcFunction
524 this.player.src(httpUrl)
528 // We changed the source, so reinit captions
529 this.player.trigger('sourcechange')
531 return this.tryToPlay(err => {
532 if (err && done) return done(err)
534 if (options.seek) this.seek(options.seek)
535 if (options.forcePlay === false && paused === true) this.player.pause()
537 if (done) return done()
541 private handleError (err: Error | string) {
542 return this.player.trigger('customError', { err })
545 private enableErrorDisplay () {
546 this.player.addClass('vjs-error-display-enabled')
549 private disableErrorDisplay () {
550 this.player.removeClass('vjs-error-display-enabled')
553 private pickAverageVideoFile () {
554 if (this.videoFiles.length === 1) return this.videoFiles[0]
556 return this.videoFiles[Math.floor(this.videoFiles.length / 2)]
559 private stopTorrent (torrent: WebTorrent.Torrent) {
561 // Pause does not remove actual peers (in particular the webseed peer)
562 torrent.removePeer(torrent[ 'ws' ])
565 private renderFileInFakeElement (file: WebTorrent.TorrentFile, delay: number) {
566 this.destroyingFakeRenderer = false
568 const fakeVideoElem = document.createElement('video')
569 renderVideo(file, fakeVideoElem, { autoplay: false, controls: false }, (err, renderer) => {
570 this.fakeRenderer = renderer
572 // The renderer returns an error when we destroy it, so skip them
573 if (this.destroyingFakeRenderer === false && err) {
574 console.error('Cannot render new torrent in fake video element.', err)
577 // Load the future file at the correct time (in delay MS - 2 seconds)
578 fakeVideoElem.currentTime = this.player.currentTime() + (delay - 2000)
582 private destroyFakeRenderer () {
583 if (this.fakeRenderer) {
584 this.destroyingFakeRenderer = true
586 if (this.fakeRenderer.destroy) {
588 this.fakeRenderer.destroy()
590 console.log('Cannot destroy correctly fake renderer.', err)
593 this.fakeRenderer = undefined
597 private buildQualities () {
598 const qualityLevelsPayload = []
600 for (const file of this.videoFiles) {
601 const representation = {
602 id: file.resolution.id,
603 label: this.buildQualityLabel(file),
604 height: file.resolution.id,
608 this.player.qualityLevels().addQualityLevel(representation)
610 qualityLevelsPayload.push({
611 id: representation.id,
612 label: representation.label,
617 const payload: LoadedQualityData = {
618 qualitySwitchCallback: (d: any) => this.qualitySwitchCallback(d),
620 video: qualityLevelsPayload
623 this.player.tech(true).trigger('loadedqualitydata', payload)
626 private buildQualityLabel (file: VideoFile) {
627 let label = file.resolution.label
629 if (file.fps && file.fps >= 50) {
636 private qualitySwitchCallback (id: number) {
638 if (this.autoResolutionPossible === true) this.enableAutoResolution()
642 this.disableAutoResolution()
643 this.updateResolution(id)
646 private changeQuality () {
647 const resolutionId = this.currentVideoFile.resolution.id
648 const qualityLevels = this.player.qualityLevels()
650 if (resolutionId === -1) {
651 qualityLevels.selectedIndex = -1
655 for (let i = 0; i < qualityLevels.length; i++) {
656 const q = qualityLevels[i]
657 if (q.height === resolutionId) qualityLevels.selectedIndex_ = i
662 videojs.registerPlugin('webtorrent', WebTorrentPlugin)
663 export { WebTorrentPlugin }