]> 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 85636c5be37e8323ca8b2251b187856131241b98..b648b29e8105eb73a39484c2d0c7d61dd2239eb6 100644 (file)
@@ -74,7 +74,7 @@ class WebTorrentPlugin extends Plugin {
     this.startTime = timeToInt(options.startTime)
 
     // Disable auto play on iOS
-    this.autoplay = options.autoplay && isIOS() === false
+    this.autoplay = options.autoplay
     this.playerRefusedP2P = !getStoredP2PEnabled()
 
     this.videoFiles = options.videoFiles
@@ -132,13 +132,17 @@ class WebTorrentPlugin extends Plugin {
     done: () => void = () => { /* empty */ }
   ) {
     // Automatically choose the adapted video file
-    if (videoFile === undefined) {
+    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.`)
+    }
+
     // Don't add the same video file once again
     if (this.currentVideoFile !== undefined && this.currentVideoFile.magnetUri === videoFile.magnetUri) {
       return
@@ -245,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
@@ -329,11 +335,6 @@ class WebTorrentPlugin extends Plugin {
   private tryToPlay (done?: (err?: Error) => void) {
     if (!done) done = function () { /* empty */ }
 
-    // Try in mute mode because we have issues with Safari
-    if (isSafari() && this.player.muted() === false) {
-      this.player.muted(true)
-    }
-
     const playPromise = this.player.play()
     if (playPromise !== undefined) {
       return playPromise.then(() => done())
@@ -362,10 +363,14 @@ class WebTorrentPlugin extends Plugin {
   }
 
   private getAppropriateFile (averageDownloadSpeed?: number): VideoFile {
-    if (this.videoFiles === undefined || this.videoFiles.length === 0) return undefined
-    if (this.videoFiles.length === 1) return this.videoFiles[0]
+    if (this.videoFiles === undefined) return undefined
+
+    const files = this.videoFiles.filter(f => f.resolution.id !== 0)
+
+    if (files.length === 0) return undefined
+    if (files.length === 1) return files[0]
 
-    // Don't change the torrent is the play was ended
+    // Don't change the torrent if the player ended
     if (this.torrent && this.torrent.progress === 1 && this.player.ended()) return this.currentVideoFile
 
     if (!averageDownloadSpeed) averageDownloadSpeed = this.getAndSaveActualDownloadSpeed()
@@ -375,32 +380,31 @@ class WebTorrentPlugin extends Plugin {
 
     // We take the first resolution just above the player height
     // Example: player height is 530px, we want the 720p file instead of 480p
-    let maxResolution = this.videoFiles[0].resolution.id
-    for (let i = this.videoFiles.length - 1; i >= 0; i--) {
-      const resolutionId = this.videoFiles[i].resolution.id
-      if (resolutionId >= playerHeight) {
+    let maxResolution = files[0].resolution.id
+    for (let i = files.length - 1; i >= 0; i--) {
+      const resolutionId = files[i].resolution.id
+      if (resolutionId !== 0 && resolutionId >= playerHeight) {
         maxResolution = resolutionId
         break
       }
     }
 
     // Filter videos we can play according to our screen resolution and bandwidth
-    const filteredFiles = this.videoFiles
-                              .filter(f => f.resolution.id <= maxResolution)
-                              .filter(f => {
-                                const fileBitrate = (f.size / this.videoDuration)
-                                let threshold = fileBitrate
+    const filteredFiles = files.filter(f => f.resolution.id <= maxResolution)
+                               .filter(f => {
+                                 const fileBitrate = (f.size / this.videoDuration)
+                                 let threshold = fileBitrate
 
-                                // If this is for a higher resolution or an initial load: add a margin
-                                if (!this.currentVideoFile || f.resolution.id > this.currentVideoFile.resolution.id) {
-                                  threshold += ((fileBitrate * this.CONSTANTS.AUTO_QUALITY_THRESHOLD_PERCENT) / 100)
-                                }
+                                 // If this is for a higher resolution or an initial load: add a margin
+                                 if (!this.currentVideoFile || f.resolution.id > this.currentVideoFile.resolution.id) {
+                                   threshold += ((fileBitrate * this.CONSTANTS.AUTO_QUALITY_THRESHOLD_PERCENT) / 100)
+                                 }
 
-                                return averageDownloadSpeed > threshold
-                              })
+                                 return averageDownloadSpeed > threshold
+                               })
 
     // If the download speed is too bad, return the lowest resolution we have
-    if (filteredFiles.length === 0) return videoFileMinByResolution(this.videoFiles)
+    if (filteredFiles.length === 0) return videoFileMinByResolution(files)
 
     return videoFileMaxByResolution(filteredFiles)
   }
@@ -489,6 +493,7 @@ class WebTorrentPlugin extends Plugin {
       if (this.webtorrent.downloadSpeed !== 0) this.downloadSpeeds.push(this.webtorrent.downloadSpeed)
 
       return this.player.trigger('p2pInfo', {
+        source: 'webtorrent',
         http: {
           downloadSpeed: 0,
           uploadSpeed: 0,
@@ -501,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)
   }
@@ -551,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) {
@@ -642,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) {