diff options
Diffstat (limited to 'client/src/assets/player/webtorrent')
-rw-r--r-- | client/src/assets/player/webtorrent/webtorrent-plugin.ts | 111 |
1 files changed, 48 insertions, 63 deletions
diff --git a/client/src/assets/player/webtorrent/webtorrent-plugin.ts b/client/src/assets/player/webtorrent/webtorrent-plugin.ts index 0587ddee6..1a1cd7f1a 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,11 +175,10 @@ 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 | updateEngineResolution (resolutionId: number, delay = 0) { |
183 | // Remember player state | 182 | // Remember player state |
184 | const currentTime = this.player.currentTime() | 183 | const currentTime = this.player.currentTime() |
185 | const isPaused = this.player.paused() | 184 | const isPaused = this.player.paused() |
@@ -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 () { |
@@ -244,6 +236,22 @@ class WebTorrentPlugin extends Plugin { | |||
244 | return this.currentVideoFile | 236 | return this.currentVideoFile |
245 | } | 237 | } |
246 | 238 | ||
239 | changeQuality (id: number) { | ||
240 | if (id === -1) { | ||
241 | if (this.autoResolutionPossible === true) { | ||
242 | this.autoResolution = true | ||
243 | |||
244 | this.selectAppropriateResolution(false) | ||
245 | } | ||
246 | |||
247 | return | ||
248 | } | ||
249 | |||
250 | this.autoResolution = false | ||
251 | this.updateEngineResolution(id) | ||
252 | this.selectAppropriateResolution(false) | ||
253 | } | ||
254 | |||
247 | private addTorrent ( | 255 | private addTorrent ( |
248 | magnetOrTorrentUrl: string, | 256 | magnetOrTorrentUrl: string, |
249 | previousVideoFile: VideoFile, | 257 | previousVideoFile: VideoFile, |
@@ -466,7 +474,7 @@ class WebTorrentPlugin extends Plugin { | |||
466 | } | 474 | } |
467 | 475 | ||
468 | if (changeResolution === true) { | 476 | if (changeResolution === true) { |
469 | this.updateResolution(file.resolution.id, changeResolutionDelay) | 477 | this.updateEngineResolution(file.resolution.id, changeResolutionDelay) |
470 | 478 | ||
471 | // Wait some seconds in observation of our new resolution | 479 | // Wait some seconds in observation of our new resolution |
472 | this.isAutoResolutionObservation = true | 480 | this.isAutoResolutionObservation = true |
@@ -516,7 +524,7 @@ class WebTorrentPlugin extends Plugin { | |||
516 | private fallbackToHttp (options: PlayOptions, done?: (err?: Error) => void) { | 524 | private fallbackToHttp (options: PlayOptions, done?: (err?: Error) => void) { |
517 | const paused = this.player.paused() | 525 | const paused = this.player.paused() |
518 | 526 | ||
519 | this.disableAutoResolution(true) | 527 | this.disableAutoResolution() |
520 | 528 | ||
521 | this.flushVideoFile(this.currentVideoFile, true) | 529 | this.flushVideoFile(this.currentVideoFile, true) |
522 | this.torrent = null | 530 | this.torrent = null |
@@ -528,7 +536,7 @@ class WebTorrentPlugin extends Plugin { | |||
528 | this.player.src = this.savePlayerSrcFunction | 536 | this.player.src = this.savePlayerSrcFunction |
529 | this.player.src(httpUrl) | 537 | this.player.src(httpUrl) |
530 | 538 | ||
531 | this.changeQuality() | 539 | this.selectAppropriateResolution(true) |
532 | 540 | ||
533 | // We changed the source, so reinit captions | 541 | // We changed the source, so reinit captions |
534 | this.player.trigger('sourcechange') | 542 | this.player.trigger('sourcechange') |
@@ -601,32 +609,22 @@ class WebTorrentPlugin extends Plugin { | |||
601 | } | 609 | } |
602 | 610 | ||
603 | private buildQualities () { | 611 | private buildQualities () { |
604 | const qualityLevelsPayload = [] | 612 | const resolutions: PeerTubeResolution[] = this.videoFiles.map(file => ({ |
605 | 613 | id: file.resolution.id, | |
606 | for (const file of this.videoFiles) { | 614 | label: this.buildQualityLabel(file), |
607 | const representation = { | 615 | height: file.resolution.id, |
608 | id: file.resolution.id, | 616 | selected: false, |
609 | label: this.buildQualityLabel(file), | 617 | selectCallback: () => this.changeQuality(file.resolution.id) |
610 | height: file.resolution.id, | 618 | })) |
611 | _enabled: true | 619 | |
612 | } | 620 | resolutions.push({ |
613 | 621 | id: -1, | |
614 | this.player.qualityLevels().addQualityLevel(representation) | 622 | label: this.player.localize('Auto'), |
615 | 623 | selected: true, | |
616 | qualityLevelsPayload.push({ | 624 | selectCallback: () => this.changeQuality(-1) |
617 | id: representation.id, | 625 | }) |
618 | label: representation.label, | ||
619 | selected: false | ||
620 | }) | ||
621 | } | ||
622 | 626 | ||
623 | const payload: LoadedQualityData = { | 627 | 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 | } | 628 | } |
631 | 629 | ||
632 | private buildQualityLabel (file: VideoFile) { | 630 | private buildQualityLabel (file: VideoFile) { |
@@ -639,29 +637,16 @@ class WebTorrentPlugin extends Plugin { | |||
639 | return label | 637 | return label |
640 | } | 638 | } |
641 | 639 | ||
642 | private qualitySwitchCallback (id: number) { | 640 | private selectAppropriateResolution (byEngine: boolean) { |
643 | if (id === -1) { | 641 | const resolution = this.autoResolution |
644 | if (this.autoResolutionPossible === true) this.enableAutoResolution() | 642 | ? -1 |
645 | return | 643 | : this.getCurrentResolutionId() |
646 | } | ||
647 | |||
648 | this.disableAutoResolution() | ||
649 | this.updateResolution(id) | ||
650 | } | ||
651 | |||
652 | private changeQuality () { | ||
653 | const resolutionId = this.currentVideoFile.resolution.id as number | ||
654 | const qualityLevels = this.player.qualityLevels() | ||
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 | ||