+ isAutoResolutionOn () {
+ return this.autoResolution
+ }
+
+ enableAutoResolution () {
+ this.autoResolution = true
+ this.trigger('autoResolutionUpdate')
+ }
+
+ disableAutoResolution () {
+ this.autoResolution = false
+ this.trigger('autoResolutionUpdate')
+ }
+
+ 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.torrent && this.torrent.progress === 1) return this.currentVideoFile
+
+ if (!averageDownloadSpeed) averageDownloadSpeed = this.getActualDownloadSpeed()
+
+ // Filter videos we can play according to our bandwidth
+ const filteredFiles = this.videoFiles.filter(f => {
+ const fileBitrate = (f.size / this.videoDuration)
+ let threshold = fileBitrate
+
+ // If this is for a higher resolution, or an initial load -> add a upper margin
+ if (!this.currentVideoFile || f.resolution.id > this.currentVideoFile.resolution.id) {
+ threshold += ((fileBitrate * this.CONSTANTS.AUTO_QUALITY_THRESHOLD_PERCENT) / 100)
+ }
+
+ return averageDownloadSpeed > threshold
+ })
+
+ // If the download speed is too bad, return the lowest resolution we have
+ if (filteredFiles.length === 0) return minBy(this.videoFiles, 'resolution.id')
+
+ return maxBy(filteredFiles, 'resolution.id')
+ }
+
+ private getActualDownloadSpeed () {
+ const start = Math.max(this.downloadSpeeds.length - this.CONSTANTS.BANDWIDTH_AVERAGE_NUMBER_OF_VALUES, 0)
+ const lastDownloadSpeeds = this.downloadSpeeds.slice(start, this.downloadSpeeds.length)
+ if (lastDownloadSpeeds.length === 0) return -1
+
+ const sum = lastDownloadSpeeds.reduce((a, b) => a + b)
+ const averageBandwidth = Math.round(sum / lastDownloadSpeeds.length)
+
+ // Save the average bandwidth for future use
+ saveAverageBandwidth(averageBandwidth)