]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - client/src/assets/player/webtorrent/webtorrent-plugin.ts
Better display redundancy pies
[github/Chocobozzz/PeerTube.git] / client / src / assets / player / webtorrent / webtorrent-plugin.ts
index a464f02d5fde39164f5d8284fc67b78fe5d9bf42..4bcb2766a66d673f37e713d6b5836f0ec59004fd 100644 (file)
@@ -2,13 +2,7 @@ import videojs from 'video.js'
 import * as WebTorrent from 'webtorrent'
 import { timeToInt } from '@shared/core-utils'
 import { VideoFile } from '@shared/models'
-import {
-  getAverageBandwidthInStore,
-  getStoredMute,
-  getStoredP2PEnabled,
-  getStoredVolume,
-  saveAverageBandwidth
-} from '../peertube-player-local-storage'
+import { getAverageBandwidthInStore, getStoredMute, getStoredVolume, saveAverageBandwidth } from '../peertube-player-local-storage'
 import { PeerTubeResolution, PlayerNetworkInfo, WebtorrentPluginOptions } from '../peertube-videojs-typings'
 import { getRtcConfig, isIOS, videoFileMaxByResolution, videoFileMinByResolution } from '../utils'
 import { PeertubeChunkStore } from './peertube-chunk-store'
@@ -74,9 +68,10 @@ class WebTorrentPlugin extends Plugin {
 
     this.startTime = timeToInt(options.startTime)
 
-    // Disable auto play on iOS
+    // Custom autoplay handled by webtorrent because we lazy play the video
     this.autoplay = options.autoplay
-    this.playerRefusedP2P = !getStoredP2PEnabled()
+
+    this.playerRefusedP2P = options.playerRefusedP2P
 
     this.videoFiles = options.videoFiles
     this.videoDuration = options.videoDuration
@@ -150,7 +145,7 @@ class WebTorrentPlugin extends Plugin {
     }
 
     // Do not display error to user because we will have multiple fallback
-    this.disableErrorDisplay();
+    this.player.peertube().hideFatalError();
 
     // Hack to "simulate" src link in video.js >= 6
     // Without this, we can't play the video after pausing it
@@ -178,7 +173,7 @@ class WebTorrentPlugin extends Plugin {
     this.selectAppropriateResolution(true)
   }
 
-  updateResolution (resolutionId: number, delay = 0) {
+  updateEngineResolution (resolutionId: number, delay = 0) {
     // Remember player state
     const currentTime = this.player.currentTime()
     const isPaused = this.player.paused()
@@ -236,6 +231,22 @@ class WebTorrentPlugin extends Plugin {
     return this.currentVideoFile
   }
 
+  changeQuality (id: number) {
+    if (id === -1) {
+      if (this.autoResolutionPossible === true) {
+        this.autoResolution = true
+
+        this.selectAppropriateResolution(false)
+      }
+
+      return
+    }
+
+    this.autoResolution = false
+    this.updateEngineResolution(id)
+    this.selectAppropriateResolution(false)
+  }
+
   private addTorrent (
     magnetOrTorrentUrl: string,
     previousVideoFile: VideoFile,
@@ -357,11 +368,10 @@ class WebTorrentPlugin extends Plugin {
 
   private getAppropriateFile (averageDownloadSpeed?: number): VideoFile {
     if (this.videoFiles === undefined) return undefined
+    if (this.videoFiles.length === 1) return this.videoFiles[0]
 
     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 if the player ended
     if (this.torrent && this.torrent.progress === 1 && this.player.ended()) return this.currentVideoFile
@@ -458,7 +468,7 @@ class WebTorrentPlugin extends Plugin {
       }
 
       if (changeResolution === true) {
-        this.updateResolution(file.resolution.id, changeResolutionDelay)
+        this.updateEngineResolution(file.resolution.id, changeResolutionDelay)
 
         // Wait some seconds in observation of our new resolution
         this.isAutoResolutionObservation = true
@@ -514,7 +524,7 @@ class WebTorrentPlugin extends Plugin {
     this.torrent = null
 
     // Enable error display now this is our last fallback
-    this.player.one('error', () => this.enableErrorDisplay())
+    this.player.one('error', () => this.player.peertube().displayFatalError())
 
     const httpUrl = this.currentVideoFile.fileUrl
     this.player.src = this.savePlayerSrcFunction
@@ -539,14 +549,6 @@ class WebTorrentPlugin extends Plugin {
     return this.player.trigger('customError', { err })
   }
 
-  private enableErrorDisplay () {
-    this.player.addClass('vjs-error-display-enabled')
-  }
-
-  private disableErrorDisplay () {
-    this.player.removeClass('vjs-error-display-enabled')
-  }
-
   private pickAverageVideoFile () {
     if (this.videoFiles.length === 1) return this.videoFiles[0]
 
@@ -598,14 +600,14 @@ class WebTorrentPlugin extends Plugin {
       label: this.buildQualityLabel(file),
       height: file.resolution.id,
       selected: false,
-      selectCallback: () => this.qualitySwitchCallback(file.resolution.id)
+      selectCallback: () => this.changeQuality(file.resolution.id)
     }))
 
     resolutions.push({
       id: -1,
       label: this.player.localize('Auto'),
       selected: true,
-      selectCallback: () => this.qualitySwitchCallback(-1)
+      selectCallback: () => this.changeQuality(-1)
     })
 
     this.player.peertubeResolutions().add(resolutions)
@@ -621,22 +623,6 @@ class WebTorrentPlugin extends Plugin {
     return label
   }
 
-  private qualitySwitchCallback (id: number) {
-    if (id === -1) {
-      if (this.autoResolutionPossible === true) {
-        this.autoResolution = true
-
-        this.selectAppropriateResolution(false)
-      }
-
-      return
-    }
-
-    this.autoResolution = false
-    this.updateResolution(id)
-    this.selectAppropriateResolution(false)
-  }
-
   private selectAppropriateResolution (byEngine: boolean) {
     const resolution = this.autoResolution
       ? -1