From e367da949bb97c3db8c2f9a28ea09eef93abb2f5 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Mon, 6 Sep 2021 16:08:59 +0200 Subject: Cleanup player quality change --- .../assets/player/webtorrent/webtorrent-plugin.ts | 91 +++++++++------------- 1 file changed, 38 insertions(+), 53 deletions(-) (limited to 'client/src/assets/player/webtorrent/webtorrent-plugin.ts') diff --git a/client/src/assets/player/webtorrent/webtorrent-plugin.ts b/client/src/assets/player/webtorrent/webtorrent-plugin.ts index 0587ddee6..a464f02d5 100644 --- a/client/src/assets/player/webtorrent/webtorrent-plugin.ts +++ b/client/src/assets/player/webtorrent/webtorrent-plugin.ts @@ -9,7 +9,7 @@ import { getStoredVolume, saveAverageBandwidth } from '../peertube-player-local-storage' -import { LoadedQualityData, PlayerNetworkInfo, WebtorrentPluginOptions } from '../peertube-videojs-typings' +import { PeerTubeResolution, PlayerNetworkInfo, WebtorrentPluginOptions } from '../peertube-videojs-typings' import { getRtcConfig, isIOS, videoFileMaxByResolution, videoFileMinByResolution } from '../utils' import { PeertubeChunkStore } from './peertube-chunk-store' import { renderVideo } from './video-renderer' @@ -175,8 +175,7 @@ class WebTorrentPlugin extends Plugin { return done() }) - this.changeQuality() - this.trigger('resolutionChange', { auto: this.autoResolution, resolutionId: this.currentVideoFile.resolution.id }) + this.selectAppropriateResolution(true) } updateResolution (resolutionId: number, delay = 0) { @@ -219,17 +218,10 @@ class WebTorrentPlugin extends Plugin { } } - enableAutoResolution () { - this.autoResolution = true - this.trigger('resolutionChange', { auto: this.autoResolution, resolutionId: this.getCurrentResolutionId() }) - } - - disableAutoResolution (forbid = false) { - if (forbid === true) this.autoResolutionPossible = false - + disableAutoResolution () { this.autoResolution = false - this.trigger('autoResolutionChange', { possible: this.autoResolutionPossible }) - this.trigger('resolutionChange', { auto: this.autoResolution, resolutionId: this.getCurrentResolutionId() }) + this.autoResolutionPossible = false + this.player.peertubeResolutions().disableAutoResolution() } isAutoResolutionPossible () { @@ -516,7 +508,7 @@ class WebTorrentPlugin extends Plugin { private fallbackToHttp (options: PlayOptions, done?: (err?: Error) => void) { const paused = this.player.paused() - this.disableAutoResolution(true) + this.disableAutoResolution() this.flushVideoFile(this.currentVideoFile, true) this.torrent = null @@ -528,7 +520,7 @@ class WebTorrentPlugin extends Plugin { this.player.src = this.savePlayerSrcFunction this.player.src(httpUrl) - this.changeQuality() + this.selectAppropriateResolution(true) // We changed the source, so reinit captions this.player.trigger('sourcechange') @@ -601,32 +593,22 @@ class WebTorrentPlugin extends Plugin { } private buildQualities () { - const qualityLevelsPayload = [] - - for (const file of this.videoFiles) { - const representation = { - id: file.resolution.id, - label: this.buildQualityLabel(file), - height: file.resolution.id, - _enabled: true - } - - this.player.qualityLevels().addQualityLevel(representation) - - qualityLevelsPayload.push({ - id: representation.id, - label: representation.label, - selected: false - }) - } + const resolutions: PeerTubeResolution[] = this.videoFiles.map(file => ({ + id: file.resolution.id, + label: this.buildQualityLabel(file), + height: file.resolution.id, + selected: false, + selectCallback: () => this.qualitySwitchCallback(file.resolution.id) + })) + + resolutions.push({ + id: -1, + label: this.player.localize('Auto'), + selected: true, + selectCallback: () => this.qualitySwitchCallback(-1) + }) - const payload: LoadedQualityData = { - qualitySwitchCallback: (d: any) => this.qualitySwitchCallback(d), - qualityData: { - video: qualityLevelsPayload - } - } - this.player.tech(true).trigger('loadedqualitydata', payload) + this.player.peertubeResolutions().add(resolutions) } private buildQualityLabel (file: VideoFile) { @@ -641,27 +623,30 @@ class WebTorrentPlugin extends Plugin { private qualitySwitchCallback (id: number) { if (id === -1) { - if (this.autoResolutionPossible === true) this.enableAutoResolution() + if (this.autoResolutionPossible === true) { + this.autoResolution = true + + this.selectAppropriateResolution(false) + } + return } - this.disableAutoResolution() + this.autoResolution = false this.updateResolution(id) + this.selectAppropriateResolution(false) } - private changeQuality () { - const resolutionId = this.currentVideoFile.resolution.id as number - const qualityLevels = this.player.qualityLevels() + private selectAppropriateResolution (byEngine: boolean) { + const resolution = this.autoResolution + ? -1 + : this.getCurrentResolutionId() - if (resolutionId === -1) { - qualityLevels.selectedIndex = -1 - return - } + const autoResolutionChosen = this.autoResolution + ? this.getCurrentResolutionId() + : undefined - for (let i = 0; i < qualityLevels.length; i++) { - const q = qualityLevels[i] - if (q.height === resolutionId) qualityLevels.selectedIndex_ = i - } + this.player.peertubeResolutions().select({ id: resolution, autoResolutionChosenId: autoResolutionChosen, byEngine }) } } -- cgit v1.2.3 From 89ac282e0437ae839e3d71566f25321f9b9384e9 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 10 Sep 2021 15:43:15 +0200 Subject: Fix embed api --- .../assets/player/webtorrent/webtorrent-plugin.ts | 40 +++++++++++----------- 1 file changed, 20 insertions(+), 20 deletions(-) (limited to 'client/src/assets/player/webtorrent/webtorrent-plugin.ts') diff --git a/client/src/assets/player/webtorrent/webtorrent-plugin.ts b/client/src/assets/player/webtorrent/webtorrent-plugin.ts index a464f02d5..1a1cd7f1a 100644 --- a/client/src/assets/player/webtorrent/webtorrent-plugin.ts +++ b/client/src/assets/player/webtorrent/webtorrent-plugin.ts @@ -178,7 +178,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 +236,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, @@ -458,7 +474,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 @@ -598,14 +614,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 +637,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 -- cgit v1.2.3