]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - client/src/assets/player/webtorrent/webtorrent-plugin.ts
Hide schedule privacy if private does not exist
[github/Chocobozzz/PeerTube.git] / client / src / assets / player / webtorrent / webtorrent-plugin.ts
index 4eaaf51ecec01e67d862169b00e0526f3546a154..b648b29e8105eb73a39484c2d0c7d61dd2239eb6 100644 (file)
@@ -122,15 +122,8 @@ class WebTorrentPlugin extends Plugin {
     return this.currentVideoFile ? this.currentVideoFile.resolution.id : -1
   }
 
-  private getVideoFile(): VideoFile {
-    const savedAverageBandwidth = getAverageBandwidthInStore()
-    return savedAverageBandwidth
-      ? this.getAppropriateFile(savedAverageBandwidth)
-      : this.pickAverageVideoFile()
-}
-
   updateVideoFile (
-    videoFile: VideoFile,
+    videoFile?: VideoFile,
     options: {
       forcePlay?: boolean,
       seek?: number,
@@ -138,7 +131,15 @@ class WebTorrentPlugin extends Plugin {
     } = {},
     done: () => void = () => { /* empty */ }
   ) {
-    if (videoFile === undefined) {
+    // Automatically choose the adapted video file
+    if (!videoFile) {
+      const savedAverageBandwidth = getAverageBandwidthInStore()
+      videoFile = savedAverageBandwidth
+        ? this.getAppropriateFile(savedAverageBandwidth)
+        : this.pickAverageVideoFile()
+    }
+
+    if (!videoFile) {
       throw Error(`Can't update video file since videoFile is undefined.`)
     }
 
@@ -248,6 +249,8 @@ class WebTorrentPlugin extends Plugin {
     options: PlayOptions,
     done: Function
   ) {
+    if (!magnetOrTorrentUrl) return this.fallbackToHttp(options, done)
+
     console.log('Adding ' + magnetOrTorrentUrl + '.')
 
     const oldTorrent = this.torrent
@@ -426,7 +429,7 @@ class WebTorrentPlugin extends Plugin {
     if (this.autoplay) {
       this.player.posterImage.hide()
 
-      return this.updateVideoFile(this.getVideoFile(), { forcePlay: true, seek: this.startTime })
+      return this.updateVideoFile(undefined, { forcePlay: true, seek: this.startTime })
     }
 
     // Proxy first play
@@ -435,7 +438,7 @@ class WebTorrentPlugin extends Plugin {
       this.player.addClass('vjs-has-big-play-button-clicked')
       this.player.play = oldPlay
 
-      this.updateVideoFile(this.getVideoFile(), { forcePlay: true, seek: this.startTime })
+      this.updateVideoFile(undefined, { forcePlay: true, seek: this.startTime })
     }
   }
 
@@ -503,7 +506,8 @@ class WebTorrentPlugin extends Plugin {
           uploadSpeed: this.torrent.uploadSpeed,
           downloaded: this.torrent.downloaded,
           uploaded: this.torrent.uploaded
-        }
+        },
+        bandwidthEstimate: this.webtorrent.downloadSpeed
       } as PlayerNetworkInfo)
     }, this.CONSTANTS.INFO_SCHEDULER)
   }
@@ -553,7 +557,8 @@ class WebTorrentPlugin extends Plugin {
   private pickAverageVideoFile () {
     if (this.videoFiles.length === 1) return this.videoFiles[0]
 
-    return this.videoFiles[Math.floor(this.videoFiles.length / 2)]
+    const files = this.videoFiles.filter(f => f.resolution.id !== 0)
+    return files[Math.floor(files.length / 2)]
   }
 
   private stopTorrent (torrent: WebTorrent.Torrent) {
@@ -644,7 +649,7 @@ class WebTorrentPlugin extends Plugin {
   }
 
   private changeQuality () {
-    const resolutionId = this.currentVideoFile.resolution.id
+    const resolutionId = this.currentVideoFile.resolution.id as number
     const qualityLevels = this.player.qualityLevels()
 
     if (resolutionId === -1) {