diff options
author | Chocobozzz <me@florianbigard.com> | 2021-09-06 16:08:59 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2021-09-10 15:06:57 +0200 |
commit | e367da949bb97c3db8c2f9a28ea09eef93abb2f5 (patch) | |
tree | 49efec8135840525eedeee45fd914b77542534f9 /client/src/assets/player/webtorrent | |
parent | 4d3e611dd2764d1d5d0a7e777312631e1e7005d4 (diff) | |
download | PeerTube-e367da949bb97c3db8c2f9a28ea09eef93abb2f5.tar.gz PeerTube-e367da949bb97c3db8c2f9a28ea09eef93abb2f5.tar.zst PeerTube-e367da949bb97c3db8c2f9a28ea09eef93abb2f5.zip |
Cleanup player quality change
Diffstat (limited to 'client/src/assets/player/webtorrent')
-rw-r--r-- | client/src/assets/player/webtorrent/webtorrent-plugin.ts | 91 |
1 files changed, 38 insertions, 53 deletions
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 { | |||
9 | getStoredVolume, | 9 | getStoredVolume, |
10 | saveAverageBandwidth | 10 | saveAverageBandwidth |
11 | } from '../peertube-player-local-storage' | 11 | } from '../peertube-player-local-storage' |
12 | import { LoadedQualityData, PlayerNetworkInfo, WebtorrentPluginOptions } from '../peertube-videojs-typings' | 12 | import { PeerTubeResolution, PlayerNetworkInfo, WebtorrentPluginOptions } from '../peertube-videojs-typings' |
13 | import { getRtcConfig, isIOS, videoFileMaxByResolution, videoFileMinByResolution } from '../utils' | 13 | import { getRtcConfig, isIOS, videoFileMaxByResolution, videoFileMinByResolution } from '../utils' |
14 | import { PeertubeChunkStore } from './peertube-chunk-store' | 14 | import { PeertubeChunkStore } from './peertube-chunk-store' |
15 | import { renderVideo } from './video-renderer' | 15 | import { renderVideo } from './video-renderer' |
@@ -175,8 +175,7 @@ class WebTorrentPlugin extends Plugin { | |||
175 | return done() | 175 | return done() |
176 | }) | 176 | }) |
177 | 177 | ||
178 | this.changeQuality() | 178 | this.selectAppropriateResolution(true) |
179 | this.trigger('resolutionChange', { auto: this.autoResolution, resolutionId: this.currentVideoFile.resolution.id }) | ||
180 | } | 179 | } |
181 | 180 | ||
182 | updateResolution (resolutionId: number, delay = 0) { | 181 | updateResolution (resolutionId: number, delay = 0) { |
@@ -219,17 +218,10 @@ class WebTorrentPlugin extends Plugin { | |||
219 | } | 218 | } |
220 | } | 219 | } |
221 | 220 | ||
222 | enableAutoResolution () { | 221 | disableAutoResolution () { |
223 | this.autoResolution = true | ||
224 | this.trigger('resolutionChange', { auto: this.autoResolution, resolutionId: this.getCurrentResolutionId() }) | ||
225 | } | ||
226 | |||
227 | disableAutoResolution (forbid = false) { | ||
228 | if (forbid === true) this.autoResolutionPossible = false | ||
229 | |||
230 | this.autoResolution = false | 222 | this.autoResolution = false |
231 | this.trigger('autoResolutionChange', { possible: this.autoResolutionPossible }) | 223 | this.autoResolutionPossible = false |
232 | this.trigger('resolutionChange', { auto: this.autoResolution, resolutionId: this.getCurrentResolutionId() }) | 224 | this.player.peertubeResolutions().disableAutoResolution() |
233 | } | 225 | } |
234 | 226 | ||
235 | isAutoResolutionPossible () { | 227 | isAutoResolutionPossible () { |
@@ -516,7 +508,7 @@ class WebTorrentPlugin extends Plugin { | |||
516 | private fallbackToHttp (options: PlayOptions, done?: (err?: Error) => void) { | 508 | private fallbackToHttp (options: PlayOptions, done?: (err?: Error) => void) { |
517 | const paused = this.player.paused() | 509 | const paused = this.player.paused() |
518 | 510 | ||
519 | this.disableAutoResolution(true) | 511 | this.disableAutoResolution() |
520 | 512 | ||
521 | this.flushVideoFile(this.currentVideoFile, true) | 513 | this.flushVideoFile(this.currentVideoFile, true) |
522 | this.torrent = null | 514 | this.torrent = null |
@@ -528,7 +520,7 @@ class WebTorrentPlugin extends Plugin { | |||
528 | this.player.src = this.savePlayerSrcFunction | 520 | this.player.src = this.savePlayerSrcFunction |
529 | this.player.src(httpUrl) | 521 | this.player.src(httpUrl) |
530 | 522 | ||
531 | this.changeQuality() | 523 | this.selectAppropriateResolution(true) |
532 | 524 | ||
533 | // We changed the source, so reinit captions | 525 | // We changed the source, so reinit captions |
534 | this.player.trigger('sourcechange') | 526 | this.player.trigger('sourcechange') |
@@ -601,32 +593,22 @@ class WebTorrentPlugin extends Plugin { | |||
601 | } | 593 | } |
602 | 594 | ||
603 | private buildQualities () { | 595 | private buildQualities () { |
604 | const qualityLevelsPayload = [] | 596 | const resolutions: PeerTubeResolution[] = this.videoFiles.map(file => ({ |
605 | 597 | id: file.resolution.id, | |
606 | for (const file of this.videoFiles) { | 598 | label: this.buildQualityLabel(file), |
607 | const representation = { | 599 | height: file.resolution.id, |
608 | id: file.resolution.id, | 600 | selected: false, |
609 | label: this.buildQualityLabel(file), | 601 | selectCallback: () => this.qualitySwitchCallback(file.resolution.id) |
610 | height: file.resolution.id, | 602 | })) |
611 | _enabled: true | 603 | |
612 | } | 604 | resolutions.push({ |
613 | 605 | id: -1, | |
614 | this.player.qualityLevels().addQualityLevel(representation) | 606 | label: this.player.localize('Auto'), |
615 | 607 | selected: true, | |
616 | qualityLevelsPayload.push({ | 608 | selectCallback: () => this.qualitySwitchCallback(-1) |
617 | id: representation.id, | 609 | }) |
618 | label: representation.label, | ||
619 | selected: false | ||
620 | }) | ||
621 | } | ||
622 | 610 | ||
623 | const payload: LoadedQualityData = { | 611 | this.player.peertubeResolutions().add(resolutions) |
624 | qualitySwitchCallback: (d: any) => this.qualitySwitchCallback(d), | ||
625 | qualityData: { | ||
626 | video: qualityLevelsPayload | ||
627 | } | ||
628 | } | ||
629 | this.player.tech(true).trigger('loadedqualitydata', payload) | ||
630 | } | 612 | } |
631 | 613 | ||
632 | private buildQualityLabel (file: VideoFile) { | 614 | private buildQualityLabel (file: VideoFile) { |
@@ -641,27 +623,30 @@ class WebTorrentPlugin extends Plugin { | |||
641 | 623 | ||
642 | private qualitySwitchCallback (id: number) { | 624 | private qualitySwitchCallback (id: number) { |
643 | if (id === -1) { | 625 | if (id === -1) { |
644 | if (this.autoResolutionPossible === true) this.enableAutoResolution() | 626 | if (this.autoResolutionPossible === true) { |
627 | this.autoResolution = true | ||
628 | |||
629 | this.selectAppropriateResolution(false) | ||
630 | } | ||
631 | |||
645 | return | 632 | return |
646 | } | 633 | } |
647 | 634 | ||
648 | this.disableAutoResolution() | 635 | this.autoResolution = false |
649 | this.updateResolution(id) | 636 | this.updateResolution(id) |
637 | this.selectAppropriateResolution(false) | ||
650 | } | 638 | } |
651 | 639 | ||
652 | private changeQuality () { | 640 | private selectAppropriateResolution (byEngine: boolean) { |
653 | const resolutionId = this.currentVideoFile.resolution.id as number | 641 | const resolution = this.autoResolution |
654 | const qualityLevels = this.player.qualityLevels() | 642 | ? -1 |
643 | : this.getCurrentResolutionId() | ||
655 | 644 | ||
656 | if (resolutionId === -1) { | 645 | const autoResolutionChosen = this.autoResolution |
657 | qualityLevels.selectedIndex = -1 | 646 | ? this.getCurrentResolutionId() |
658 | return | 647 | : undefined |
659 | } | ||
660 | 648 | ||
661 | for (let i = 0; i < qualityLevels.length; i++) { | 649 | this.player.peertubeResolutions().select({ id: resolution, autoResolutionChosenId: autoResolutionChosen, byEngine }) |
662 | const q = qualityLevels[i] | ||
663 | if (q.height === resolutionId) qualityLevels.selectedIndex_ = i | ||
664 | } | ||
665 | } | 650 | } |
666 | } | 651 | } |
667 | 652 | ||