1 // FIXME: something weird with our path definition in tsconfig and typings
3 import * as videojs from 'video.js'
5 import * as WebTorrent from 'webtorrent'
6 import { renderVideo } from './video-renderer'
7 import { LoadedQualityData, PlayerNetworkInfo, VideoJSComponentInterface, WebtorrentPluginOptions } from '../peertube-videojs-typings'
8 import { getRtcConfig, timeToInt, videoFileMaxByResolution, videoFileMinByResolution } from '../utils'
9 import { PeertubeChunkStore } from './peertube-chunk-store'
11 getAverageBandwidthInStore,
16 } from '../peertube-player-local-storage'
17 import { VideoFile } from '@shared/models'
19 const CacheChunkStore = require('cache-chunk-store')
27 const Plugin: VideoJSComponentInterface = videojs.getPlugin('plugin')
28 class WebTorrentPlugin extends Plugin {
29 private readonly playerElement: HTMLVideoElement
31 private readonly autoplay: boolean = false
32 private readonly startTime: number = 0
33 private readonly savePlayerSrcFunction: Function
34 private readonly videoFiles: VideoFile[]
35 private readonly videoDuration: number
36 private readonly CONSTANTS = {
37 INFO_SCHEDULER: 1000, // Don't change this
38 AUTO_QUALITY_SCHEDULER: 3000, // Check quality every 3 seconds
39 AUTO_QUALITY_THRESHOLD_PERCENT: 30, // Bandwidth should be 30% more important than a resolution bitrate to change to it
40 AUTO_QUALITY_OBSERVATION_TIME: 10000, // Wait 10 seconds after having change the resolution before another check
41 AUTO_QUALITY_HIGHER_RESOLUTION_DELAY: 5000, // Buffering higher resolution during 5 seconds
42 BANDWIDTH_AVERAGE_NUMBER_OF_VALUES: 5 // Last 5 seconds to build average bandwidth
45 private readonly webtorrent = new WebTorrent({
47 rtcConfig: getRtcConfig()
53 private currentVideoFile: VideoFile
54 private torrent: WebTorrent.Torrent
57 private fakeRenderer: any
58 private destroyingFakeRenderer = false
60 private autoResolution = true
61 private autoResolutionPossible = true
62 private isAutoResolutionObservation = false
63 private playerRefusedP2P = false
65 private torrentInfoInterval: any
66 private autoQualityInterval: any
67 private addTorrentDelay: any
68 private qualityObservationTimer: any
69 private runAutoQualitySchedulerTimer: any
71 private downloadSpeeds: number[] = []
73 constructor (player: videojs.Player, options: WebtorrentPluginOptions) {
74 super(player, options)
76 this.startTime = timeToInt(options.startTime)
78 // Disable auto play on iOS
79 this.autoplay = options.autoplay && this.isIOS() === false
80 this.playerRefusedP2P = !getStoredP2PEnabled()
82 this.videoFiles = options.videoFiles
83 this.videoDuration = options.videoDuration
85 this.savePlayerSrcFunction = this.player.src
86 this.playerElement = options.playerElement
88 this.player.ready(() => {
89 const playerOptions = this.player.options_
91 const volume = getStoredVolume()
92 if (volume !== undefined) this.player.volume(volume)
94 const muted = playerOptions.muted !== undefined ? playerOptions.muted : getStoredMute()
95 if (muted !== undefined) this.player.muted(muted)
97 this.player.duration(options.videoDuration)
99 this.initializePlayer()
100 this.runTorrentInfoScheduler()
102 this.player.one('play', () => {
103 // Don't run immediately scheduler, wait some seconds the TCP connections are made
104 this.runAutoQualitySchedulerTimer = setTimeout(() => this.runAutoQualityScheduler(), this.CONSTANTS.AUTO_QUALITY_SCHEDULER)
110 clearTimeout(this.addTorrentDelay)
111 clearTimeout(this.qualityObservationTimer)
112 clearTimeout(this.runAutoQualitySchedulerTimer)
114 clearInterval(this.torrentInfoInterval)
115 clearInterval(this.autoQualityInterval)
117 // Don't need to destroy renderer, video player will be destroyed
118 this.flushVideoFile(this.currentVideoFile, false)
120 this.destroyFakeRenderer()
123 getCurrentResolutionId () {
124 return this.currentVideoFile ? this.currentVideoFile.resolution.id : -1
128 videoFile?: VideoFile,
134 done: () => void = () => { /* empty */ }
136 // Automatically choose the adapted video file
137 if (videoFile === undefined) {
138 const savedAverageBandwidth = getAverageBandwidthInStore()
139 videoFile = savedAverageBandwidth
140 ? this.getAppropriateFile(savedAverageBandwidth)
141 : this.pickAverageVideoFile()
144 // Don't add the same video file once again
145 if (this.currentVideoFile !== undefined && this.currentVideoFile.magnetUri === videoFile.magnetUri) {
149 // Do not display error to user because we will have multiple fallback
150 this.disableErrorDisplay()
152 // Hack to "simulate" src link in video.js >= 6
153 // Without this, we can't play the video after pausing it
154 // https://github.com/videojs/video.js/blob/master/src/js/player.js#L1633
155 this.player.src = () => true
156 const oldPlaybackRate = this.player.playbackRate()
158 const previousVideoFile = this.currentVideoFile
159 this.currentVideoFile = videoFile
161 // Don't try on iOS that does not support MediaSource
162 // Or don't use P2P if webtorrent is disabled
163 if (this.isIOS() || this.playerRefusedP2P) {
164 return this.fallbackToHttp(options, () => {
165 this.player.playbackRate(oldPlaybackRate)
170 this.addTorrent(this.currentVideoFile.magnetUri, previousVideoFile, options, () => {
171 this.player.playbackRate(oldPlaybackRate)
176 this.trigger('resolutionChange', { auto: this.autoResolution, resolutionId: this.currentVideoFile.resolution.id })
179 updateResolution (resolutionId: number, delay = 0) {
180 // Remember player state
181 const currentTime = this.player.currentTime()
182 const isPaused = this.player.paused()
184 // Hide bigPlayButton
186 this.player.bigPlayButton.hide()
189 // Audio-only (resolutionId === 0) gets special treatment
190 if (resolutionId === 0) {
191 // Audio-only: show poster, do not auto-hide controls
192 this.player.addClass('vjs-playing-audio-only-content')
193 this.player.posterImage.show()
195 // Hide poster to have black background
196 this.player.removeClass('vjs-playing-audio-only-content')
197 this.player.posterImage.hide()
200 const newVideoFile = this.videoFiles.find(f => f.resolution.id === resolutionId)
204 seek: currentTime + (delay / 1000)
207 this.updateVideoFile(newVideoFile, options)
210 flushVideoFile (videoFile: VideoFile, destroyRenderer = true) {
211 if (videoFile !== undefined && this.webtorrent.get(videoFile.magnetUri)) {
212 if (destroyRenderer === true && this.renderer && this.renderer.destroy) this.renderer.destroy()
214 this.webtorrent.remove(videoFile.magnetUri)
215 console.log('Removed ' + videoFile.magnetUri)
219 enableAutoResolution () {
220 this.autoResolution = true
221 this.trigger('resolutionChange', { auto: this.autoResolution, resolutionId: this.getCurrentResolutionId() })
224 disableAutoResolution (forbid = false) {
225 if (forbid === true) this.autoResolutionPossible = false
227 this.autoResolution = false
228 this.trigger('autoResolutionChange', { possible: this.autoResolutionPossible })
229 this.trigger('resolutionChange', { auto: this.autoResolution, resolutionId: this.getCurrentResolutionId() })
236 getCurrentVideoFile () {
237 return this.currentVideoFile
241 magnetOrTorrentUrl: string,
242 previousVideoFile: VideoFile,
243 options: PlayOptions,
246 console.log('Adding ' + magnetOrTorrentUrl + '.')
248 const oldTorrent = this.torrent
249 const torrentOptions = {
250 // Don't use arrow function: it breaks webtorrent (that uses `new` keyword)
251 store: function (chunkLength: number, storeOpts: any) {
252 return new CacheChunkStore(new PeertubeChunkStore(chunkLength, storeOpts), {
258 this.torrent = this.webtorrent.add(magnetOrTorrentUrl, torrentOptions, torrent => {
259 console.log('Added ' + magnetOrTorrentUrl + '.')
262 // Pause the old torrent
263 this.stopTorrent(oldTorrent)
265 // We use a fake renderer so we download correct pieces of the next file
266 if (options.delay) this.renderFileInFakeElement(torrent.files[ 0 ], options.delay)
269 // Render the video in a few seconds? (on resolution change for example, we wait some seconds of the new video resolution)
270 this.addTorrentDelay = setTimeout(() => {
271 // We don't need the fake renderer anymore
272 this.destroyFakeRenderer()
274 const paused = this.player.paused()
276 this.flushVideoFile(previousVideoFile)
278 // Update progress bar (just for the UI), do not wait rendering
279 if (options.seek) this.player.currentTime(options.seek)
281 const renderVideoOptions = { autoplay: false, controls: true }
282 renderVideo(torrent.files[ 0 ], this.playerElement, renderVideoOptions, (err, renderer) => {
283 this.renderer = renderer
285 if (err) return this.fallbackToHttp(options, done)
287 return this.tryToPlay(err => {
288 if (err) return done(err)
290 if (options.seek) this.seek(options.seek)
291 if (options.forcePlay === false && paused === true) this.player.pause()
296 }, options.delay || 0)
299 this.torrent.on('error', (err: any) => console.error(err))
301 this.torrent.on('warning', (err: any) => {
302 // We don't support HTTP tracker but we don't care -> we use the web socket tracker
303 if (err.message.indexOf('Unsupported tracker protocol') !== -1) return
305 // Users don't care about issues with WebRTC, but developers do so log it in the console
306 if (err.message.indexOf('Ice connection failed') !== -1) {
311 // Magnet hash is not up to date with the torrent file, add directly the torrent file
312 if (err.message.indexOf('incorrect info hash') !== -1) {
313 console.error('Incorrect info hash detected, falling back to torrent file.')
314 const newOptions = { forcePlay: true, seek: options.seek }
315 return this.addTorrent(this.torrent[ 'xs' ], previousVideoFile, newOptions, done)
318 // Remote instance is down
319 if (err.message.indexOf('from xs param') !== -1) {
320 this.handleError(err)
327 private tryToPlay (done?: (err?: Error) => void) {
328 if (!done) done = function () { /* empty */ }
330 const playPromise = this.player.play()
331 if (playPromise !== undefined) {
332 return playPromise.then(done)
333 .catch((err: Error) => {
334 if (err.message.indexOf('The play() request was interrupted by a call to pause()') !== -1) {
340 this.player.posterImage.show()
341 this.player.removeClass('vjs-has-autoplay')
342 this.player.removeClass('vjs-has-big-play-button-clicked')
343 this.player.removeClass('vjs-playing-audio-only-content')
352 private seek (time: number) {
353 this.player.currentTime(time)
354 this.player.handleTechSeeked_()
357 private getAppropriateFile (averageDownloadSpeed?: number): VideoFile {
358 if (this.videoFiles === undefined || this.videoFiles.length === 0) return undefined
359 if (this.videoFiles.length === 1) return this.videoFiles[0]
361 // Don't change the torrent is the play was ended
362 if (this.torrent && this.torrent.progress === 1 && this.player.ended()) return this.currentVideoFile
364 if (!averageDownloadSpeed) averageDownloadSpeed = this.getAndSaveActualDownloadSpeed()
366 // Limit resolution according to player height
367 const playerHeight = this.playerElement.offsetHeight
369 // We take the first resolution just above the player height
370 // Example: player height is 530px, we want the 720p file instead of 480p
371 let maxResolution = this.videoFiles[0].resolution.id
372 for (let i = this.videoFiles.length - 1; i >= 0; i--) {
373 const resolutionId = this.videoFiles[i].resolution.id
374 if (resolutionId >= playerHeight) {
375 maxResolution = resolutionId
380 // Filter videos we can play according to our screen resolution and bandwidth
381 const filteredFiles = this.videoFiles
382 .filter(f => f.resolution.id <= maxResolution)
384 const fileBitrate = (f.size / this.videoDuration)
385 let threshold = fileBitrate
387 // If this is for a higher resolution or an initial load: add a margin
388 if (!this.currentVideoFile || f.resolution.id > this.currentVideoFile.resolution.id) {
389 threshold += ((fileBitrate * this.CONSTANTS.AUTO_QUALITY_THRESHOLD_PERCENT) / 100)
392 return averageDownloadSpeed > threshold
395 // If the download speed is too bad, return the lowest resolution we have
396 if (filteredFiles.length === 0) return videoFileMinByResolution(this.videoFiles)
398 return videoFileMaxByResolution(filteredFiles)
401 private getAndSaveActualDownloadSpeed () {
402 const start = Math.max(this.downloadSpeeds.length - this.CONSTANTS.BANDWIDTH_AVERAGE_NUMBER_OF_VALUES, 0)
403 const lastDownloadSpeeds = this.downloadSpeeds.slice(start, this.downloadSpeeds.length)
404 if (lastDownloadSpeeds.length === 0) return -1
406 const sum = lastDownloadSpeeds.reduce((a, b) => a + b)
407 const averageBandwidth = Math.round(sum / lastDownloadSpeeds.length)
409 // Save the average bandwidth for future use
410 saveAverageBandwidth(averageBandwidth)
412 return averageBandwidth
415 private initializePlayer () {
416 this.buildQualities()
418 if (this.autoplay === true) {
419 this.player.posterImage.hide()
421 return this.updateVideoFile(undefined, { forcePlay: true, seek: this.startTime })
425 const oldPlay = this.player.play.bind(this.player)
426 this.player.play = () => {
427 this.player.addClass('vjs-has-big-play-button-clicked')
428 this.player.play = oldPlay
430 this.updateVideoFile(undefined, { forcePlay: true, seek: this.startTime })
434 private runAutoQualityScheduler () {
435 this.autoQualityInterval = setInterval(() => {
437 // Not initialized or in HTTP fallback
438 if (this.torrent === undefined || this.torrent === null) return
439 if (this.autoResolution === false) return
440 if (this.isAutoResolutionObservation === true) return
442 const file = this.getAppropriateFile()
443 let changeResolution = false
444 let changeResolutionDelay = 0
447 if (this.isPlayerWaiting() && file.resolution.id < this.currentVideoFile.resolution.id) {
448 console.log('Downgrading automatically the resolution to: %s', file.resolution.label)
449 changeResolution = true
450 } else if (file.resolution.id > this.currentVideoFile.resolution.id) { // Higher resolution
451 console.log('Upgrading automatically the resolution to: %s', file.resolution.label)
452 changeResolution = true
453 changeResolutionDelay = this.CONSTANTS.AUTO_QUALITY_HIGHER_RESOLUTION_DELAY
456 if (changeResolution === true) {
457 this.updateResolution(file.resolution.id, changeResolutionDelay)
459 // Wait some seconds in observation of our new resolution
460 this.isAutoResolutionObservation = true
462 this.qualityObservationTimer = setTimeout(() => {
463 this.isAutoResolutionObservation = false
464 }, this.CONSTANTS.AUTO_QUALITY_OBSERVATION_TIME)
466 }, this.CONSTANTS.AUTO_QUALITY_SCHEDULER)
469 private isPlayerWaiting () {
470 return this.player && this.player.hasClass('vjs-waiting')
473 private runTorrentInfoScheduler () {
474 this.torrentInfoInterval = setInterval(() => {
475 // Not initialized yet
476 if (this.torrent === undefined) return
479 if (this.torrent === null) return this.player.trigger('p2pInfo', false)
481 // this.webtorrent.downloadSpeed because we need to take into account the potential old torrent too
482 if (this.webtorrent.downloadSpeed !== 0) this.downloadSpeeds.push(this.webtorrent.downloadSpeed)
484 return this.player.trigger('p2pInfo', {
492 downloadSpeed: this.torrent.downloadSpeed,
493 numPeers: this.torrent.numPeers,
494 uploadSpeed: this.torrent.uploadSpeed,
495 downloaded: this.torrent.downloaded,
496 uploaded: this.torrent.uploaded
498 } as PlayerNetworkInfo)
499 }, this.CONSTANTS.INFO_SCHEDULER)
502 private fallbackToHttp (options: PlayOptions, done?: Function) {
503 const paused = this.player.paused()
505 this.disableAutoResolution(true)
507 this.flushVideoFile(this.currentVideoFile, true)
510 // Enable error display now this is our last fallback
511 this.player.one('error', () => this.enableErrorDisplay())
513 const httpUrl = this.currentVideoFile.fileUrl
514 this.player.src = this.savePlayerSrcFunction
515 this.player.src(httpUrl)
519 // We changed the source, so reinit captions
520 this.player.trigger('sourcechange')
522 return this.tryToPlay(err => {
523 if (err && done) return done(err)
525 if (options.seek) this.seek(options.seek)
526 if (options.forcePlay === false && paused === true) this.player.pause()
528 if (done) return done()
532 private handleError (err: Error | string) {
533 return this.player.trigger('customError', { err })
536 private enableErrorDisplay () {
537 this.player.addClass('vjs-error-display-enabled')
540 private disableErrorDisplay () {
541 this.player.removeClass('vjs-error-display-enabled')
545 return !!navigator.platform && /iPad|iPhone|iPod/.test(navigator.platform)
548 private pickAverageVideoFile () {
549 if (this.videoFiles.length === 1) return this.videoFiles[0]
551 return this.videoFiles[Math.floor(this.videoFiles.length / 2)]
554 private stopTorrent (torrent: WebTorrent.Torrent) {
556 // Pause does not remove actual peers (in particular the webseed peer)
557 torrent.removePeer(torrent[ 'ws' ])
560 private renderFileInFakeElement (file: WebTorrent.TorrentFile, delay: number) {
561 this.destroyingFakeRenderer = false
563 const fakeVideoElem = document.createElement('video')
564 renderVideo(file, fakeVideoElem, { autoplay: false, controls: false }, (err, renderer) => {
565 this.fakeRenderer = renderer
567 // The renderer returns an error when we destroy it, so skip them
568 if (this.destroyingFakeRenderer === false && err) {
569 console.error('Cannot render new torrent in fake video element.', err)
572 // Load the future file at the correct time (in delay MS - 2 seconds)
573 fakeVideoElem.currentTime = this.player.currentTime() + (delay - 2000)
577 private destroyFakeRenderer () {
578 if (this.fakeRenderer) {
579 this.destroyingFakeRenderer = true
581 if (this.fakeRenderer.destroy) {
583 this.fakeRenderer.destroy()
585 console.log('Cannot destroy correctly fake renderer.', err)
588 this.fakeRenderer = undefined
592 private buildQualities () {
593 const qualityLevelsPayload = []
595 for (const file of this.videoFiles) {
596 const representation = {
597 id: file.resolution.id,
598 label: this.buildQualityLabel(file),
599 height: file.resolution.id,
603 this.player.qualityLevels().addQualityLevel(representation)
605 qualityLevelsPayload.push({
606 id: representation.id,
607 label: representation.label,
612 const payload: LoadedQualityData = {
613 qualitySwitchCallback: (d: any) => this.qualitySwitchCallback(d),
615 video: qualityLevelsPayload
618 this.player.tech_.trigger('loadedqualitydata', payload)
621 private buildQualityLabel (file: VideoFile) {
622 let label = file.resolution.label
624 if (file.fps && file.fps >= 50) {
631 private qualitySwitchCallback (id: number) {
633 if (this.autoResolutionPossible === true) this.enableAutoResolution()
637 this.disableAutoResolution()
638 this.updateResolution(id)
641 private changeQuality () {
642 const resolutionId = this.currentVideoFile.resolution.id
643 const qualityLevels = this.player.qualityLevels()
645 if (resolutionId === -1) {
646 qualityLevels.selectedIndex = -1
650 for (let i = 0; i < qualityLevels; i++) {
651 const q = this.player.qualityLevels[i]
652 if (q.height === resolutionId) qualityLevels.selectedIndex = i
657 videojs.registerPlugin('webtorrent', WebTorrentPlugin)
658 export { WebTorrentPlugin }