diff options
author | Chocobozzz <me@florianbigard.com> | 2023-08-18 09:48:45 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2023-08-18 09:48:45 +0200 |
commit | 8e4fba97b26090e0c77ee9591058cd34ef9d2f55 (patch) | |
tree | aced2d2b805845f81075412ed841221dcb4d7fca /client/src/assets | |
parent | 276f5fa24f642fea7d7e6fb8812772e471070993 (diff) | |
download | PeerTube-8e4fba97b26090e0c77ee9591058cd34ef9d2f55.tar.gz PeerTube-8e4fba97b26090e0c77ee9591058cd34ef9d2f55.tar.zst PeerTube-8e4fba97b26090e0c77ee9591058cd34ef9d2f55.zip |
Automatically adapt player ratio
Diffstat (limited to 'client/src/assets')
7 files changed, 53 insertions, 4 deletions
diff --git a/client/src/assets/player/peertube-player.ts b/client/src/assets/player/peertube-player.ts index 4da681a08..111b4645b 100644 --- a/client/src/assets/player/peertube-player.ts +++ b/client/src/assets/player/peertube-player.ts | |||
@@ -364,7 +364,9 @@ export class PeerTubePlayer { | |||
364 | videoUUID: () => this.currentLoadOptions.videoUUID, | 364 | videoUUID: () => this.currentLoadOptions.videoUUID, |
365 | subtitle: () => this.currentLoadOptions.subtitle, | 365 | subtitle: () => this.currentLoadOptions.subtitle, |
366 | 366 | ||
367 | poster: () => this.currentLoadOptions.poster | 367 | poster: () => this.currentLoadOptions.poster, |
368 | |||
369 | autoPlayerRatio: this.options.autoPlayerRatio | ||
368 | }, | 370 | }, |
369 | metrics: { | 371 | metrics: { |
370 | mode: () => this.currentLoadOptions.mode, | 372 | mode: () => this.currentLoadOptions.mode, |
diff --git a/client/src/assets/player/shared/metrics/metrics-plugin.ts b/client/src/assets/player/shared/metrics/metrics-plugin.ts index 0ad16338c..a581d7931 100644 --- a/client/src/assets/player/shared/metrics/metrics-plugin.ts +++ b/client/src/assets/player/shared/metrics/metrics-plugin.ts | |||
@@ -140,10 +140,10 @@ class MetricsPlugin extends Plugin { | |||
140 | 140 | ||
141 | private trackBytes () { | 141 | private trackBytes () { |
142 | this.player.on('network-info', (_event, data: PlayerNetworkInfo) => { | 142 | this.player.on('network-info', (_event, data: PlayerNetworkInfo) => { |
143 | this.downloadedBytesHTTP += Math.round(data.http.downloaded - (this.lastPlayerNetworkInfo?.http.downloaded || 0)) | 143 | this.downloadedBytesHTTP += Math.max(Math.round(data.http.downloaded - (this.lastPlayerNetworkInfo?.http.downloaded || 0)), 0) |
144 | this.downloadedBytesP2P += Math.round((data.p2p?.downloaded || 0) - (this.lastPlayerNetworkInfo?.p2p?.downloaded || 0)) | 144 | this.downloadedBytesP2P += Math.max(Math.round((data.p2p?.downloaded || 0) - (this.lastPlayerNetworkInfo?.p2p?.downloaded || 0)), 0) |
145 | 145 | ||
146 | this.uploadedBytesP2P += Math.round((data.p2p?.uploaded || 0) - (this.lastPlayerNetworkInfo?.p2p?.uploaded || 0)) | 146 | this.uploadedBytesP2P += Math.max(Math.round((data.p2p?.uploaded || 0) - (this.lastPlayerNetworkInfo?.p2p?.uploaded || 0)), 0) |
147 | 147 | ||
148 | this.p2pPeers = data.p2p?.peersP2POnly | 148 | this.p2pPeers = data.p2p?.peersP2POnly |
149 | this.p2pEnabled = !!data.p2p | 149 | this.p2pEnabled = !!data.p2p |
diff --git a/client/src/assets/player/shared/p2p-media-loader/p2p-media-loader-plugin.ts b/client/src/assets/player/shared/p2p-media-loader/p2p-media-loader-plugin.ts index 1e47fe486..ee617ce8a 100644 --- a/client/src/assets/player/shared/p2p-media-loader/p2p-media-loader-plugin.ts +++ b/client/src/assets/player/shared/p2p-media-loader/p2p-media-loader-plugin.ts | |||
@@ -119,6 +119,14 @@ class P2pMediaLoaderPlugin extends Plugin { | |||
119 | this.runStats() | 119 | this.runStats() |
120 | 120 | ||
121 | this.hlsjs.on(Hlsjs.Events.LEVEL_SWITCHED, () => this.player.trigger('engine-resolution-change')) | 121 | this.hlsjs.on(Hlsjs.Events.LEVEL_SWITCHED, () => this.player.trigger('engine-resolution-change')) |
122 | |||
123 | this.hlsjs.on(Hlsjs.Events.MANIFEST_PARSED, (_event, data) => { | ||
124 | if (Array.isArray(data.levels) && data.levels.length > 1) { | ||
125 | const level = data.levels[0] | ||
126 | |||
127 | this.player.trigger('video-ratio-changed', { ratio: level.width / level.height }) | ||
128 | } | ||
129 | }) | ||
122 | } | 130 | } |
123 | 131 | ||
124 | private runStats () { | 132 | private runStats () { |
diff --git a/client/src/assets/player/shared/peertube/peertube-plugin.ts b/client/src/assets/player/shared/peertube/peertube-plugin.ts index cf866723c..7aeae2670 100644 --- a/client/src/assets/player/shared/peertube/peertube-plugin.ts +++ b/client/src/assets/player/shared/peertube/peertube-plugin.ts | |||
@@ -115,6 +115,8 @@ class PeerTubePlugin extends Plugin { | |||
115 | this.hideFatalError() | 115 | this.hideFatalError() |
116 | }) | 116 | }) |
117 | }) | 117 | }) |
118 | |||
119 | this.initOnRatioChange() | ||
118 | } | 120 | } |
119 | 121 | ||
120 | dispose () { | 122 | dispose () { |
@@ -210,6 +212,25 @@ class PeerTubePlugin extends Plugin { | |||
210 | this.runUserViewing() | 212 | this.runUserViewing() |
211 | } | 213 | } |
212 | 214 | ||
215 | private initOnRatioChange () { | ||
216 | if (!this.options.autoPlayerRatio) return | ||
217 | |||
218 | const defaultRatio = getComputedStyle(this.player.el()).getPropertyValue(this.options.autoPlayerRatio.cssRatioVariable) | ||
219 | |||
220 | this.player.on('video-ratio-changed', (_event, data: { ratio: number }) => { | ||
221 | const el = this.player.el() as HTMLElement | ||
222 | |||
223 | // In portrait screen mode, we allow player with bigger height size than width | ||
224 | const portraitMode = getComputedStyle(el).getPropertyValue(this.options.autoPlayerRatio.cssPlayerPortraitModeVariable) === '1' | ||
225 | |||
226 | const currentRatio = !portraitMode && data.ratio < 1 | ||
227 | ? defaultRatio | ||
228 | : data.ratio | ||
229 | |||
230 | el.style.setProperty('--player-ratio', currentRatio + '') | ||
231 | }) | ||
232 | } | ||
233 | |||
213 | // --------------------------------------------------------------------------- | 234 | // --------------------------------------------------------------------------- |
214 | 235 | ||
215 | private runUserViewing () { | 236 | private runUserViewing () { |
diff --git a/client/src/assets/player/shared/web-video/web-video-plugin.ts b/client/src/assets/player/shared/web-video/web-video-plugin.ts index 18d911108..8f4db0680 100644 --- a/client/src/assets/player/shared/web-video/web-video-plugin.ts +++ b/client/src/assets/player/shared/web-video/web-video-plugin.ts | |||
@@ -19,6 +19,7 @@ class WebVideoPlugin extends Plugin { | |||
19 | 19 | ||
20 | private onErrorHandler: () => void | 20 | private onErrorHandler: () => void |
21 | private onPlayHandler: () => void | 21 | private onPlayHandler: () => void |
22 | private onLoadedMetadata: () => void | ||
22 | 23 | ||
23 | constructor (player: videojs.Player, options?: WebVideoPluginOptions) { | 24 | constructor (player: videojs.Player, options?: WebVideoPluginOptions) { |
24 | super(player, options) | 25 | super(player, options) |
@@ -28,6 +29,12 @@ class WebVideoPlugin extends Plugin { | |||
28 | 29 | ||
29 | this.updateVideoFile({ videoFile: this.pickAverageVideoFile(), isUserResolutionChange: false }) | 30 | this.updateVideoFile({ videoFile: this.pickAverageVideoFile(), isUserResolutionChange: false }) |
30 | 31 | ||
32 | this.onLoadedMetadata = () => { | ||
33 | player.trigger('video-ratio-changed', { ratio: this.player.videoWidth() / this.player.videoHeight() }) | ||
34 | } | ||
35 | |||
36 | player.on('loadedmetadata', this.onLoadedMetadata) | ||
37 | |||
31 | player.ready(() => { | 38 | player.ready(() => { |
32 | this.buildQualities() | 39 | this.buildQualities() |
33 | 40 | ||
@@ -43,6 +50,7 @@ class WebVideoPlugin extends Plugin { | |||
43 | dispose () { | 50 | dispose () { |
44 | clearInterval(this.networkInfoInterval) | 51 | clearInterval(this.networkInfoInterval) |
45 | 52 | ||
53 | if (this.onLoadedMetadata) this.player.off('loadedmetadata', this.onLoadedMetadata) | ||
46 | if (this.onErrorHandler) this.player.off('error', this.onErrorHandler) | 54 | if (this.onErrorHandler) this.player.off('error', this.onErrorHandler) |
47 | if (this.onPlayHandler) this.player.off('canplay', this.onPlayHandler) | 55 | if (this.onPlayHandler) this.player.off('canplay', this.onPlayHandler) |
48 | 56 | ||
diff --git a/client/src/assets/player/types/peertube-player-options.ts b/client/src/assets/player/types/peertube-player-options.ts index 352f7d8dd..6fb2f7913 100644 --- a/client/src/assets/player/types/peertube-player-options.ts +++ b/client/src/assets/player/types/peertube-player-options.ts | |||
@@ -38,6 +38,11 @@ export type PeerTubePlayerContructorOptions = { | |||
38 | language: string | 38 | language: string |
39 | 39 | ||
40 | pluginsManager: PluginsManager | 40 | pluginsManager: PluginsManager |
41 | |||
42 | autoPlayerRatio?: { | ||
43 | cssRatioVariable: string | ||
44 | cssPlayerPortraitModeVariable: string | ||
45 | } | ||
41 | } | 46 | } |
42 | 47 | ||
43 | export type PeerTubePlayerLoadOptions = { | 48 | export type PeerTubePlayerLoadOptions = { |
diff --git a/client/src/assets/player/types/peertube-videojs-typings.ts b/client/src/assets/player/types/peertube-videojs-typings.ts index dae9e14c8..27fbda31d 100644 --- a/client/src/assets/player/types/peertube-videojs-typings.ts +++ b/client/src/assets/player/types/peertube-videojs-typings.ts | |||
@@ -104,6 +104,11 @@ type VideoJSStoryboard = { | |||
104 | } | 104 | } |
105 | 105 | ||
106 | type PeerTubePluginOptions = { | 106 | type PeerTubePluginOptions = { |
107 | autoPlayerRatio: { | ||
108 | cssRatioVariable: string | ||
109 | cssPlayerPortraitModeVariable: string | ||
110 | } | ||
111 | |||
107 | hasAutoplay: () => videojs.Autoplay | 112 | hasAutoplay: () => videojs.Autoplay |
108 | 113 | ||
109 | videoViewUrl: () => string | 114 | videoViewUrl: () => string |