From dc62c13c2188aa3efab33e3c35c35d0e26d5d2bd Mon Sep 17 00:00:00 2001 From: redphx <96280+redphx@users.noreply.github.com> Date: Sun, 19 May 2024 11:42:40 +0700 Subject: [PATCH] Optimize video player --- src/assets/css/stream.styl | 25 +++++++++---- src/modules/ui/ui.ts | 72 +++++++++++++++++++++++++------------- 2 files changed, 65 insertions(+), 32 deletions(-) diff --git a/src/assets/css/stream.styl b/src/assets/css/stream.styl index fa29cbb..0fcd405 100644 --- a/src/assets/css/stream.styl +++ b/src/assets/css/stream.styl @@ -34,11 +34,22 @@ body[data-media-type=tv] .bx-stream-refresh-button { } } -div[data-testid=media-container].bx-taking-screenshot:before { - animation: bx-anim-taking-screenshot 0.5s ease; - content: ' '; - position: absolute; - width: 100%; - height: 100%; - z-index: var(--bx-screenshot-animation-z-index); +div[data-testid=media-container] { + display: flex; + + &.bx-taking-screenshot:before { + animation: bx-anim-taking-screenshot 0.5s ease; + content: ' '; + position: absolute; + width: 100%; + height: 100%; + z-index: var(--bx-screenshot-animation-z-index); + } +} + + +#game-stream video { + margin: auto; + align-self: center; + background: #000; } diff --git a/src/modules/ui/ui.ts b/src/modules/ui/ui.ts index 2469950..0a02724 100644 --- a/src/modules/ui/ui.ts +++ b/src/modules/ui/ui.ts @@ -431,43 +431,65 @@ export function updateVideoPlayerCss() { Screenshot.updateCanvasFilters(filters); } - const PREF_RATIO = getPref(PrefKey.VIDEO_RATIO); - if (PREF_RATIO && PREF_RATIO !== '16:9') { - if (PREF_RATIO.includes(':')) { - videoCss += `aspect-ratio: ${PREF_RATIO.replace(':', '/')}; object-fit: unset !important;`; - - const tmp = PREF_RATIO.split(':'); - const ratio = parseFloat(tmp[0]) / parseFloat(tmp[1]); - const maxRatio = window.innerWidth / window.innerHeight; - if (ratio < maxRatio) { - videoCss += 'width: fit-content !important;' - } else { - videoCss += 'height: fit-content !important;' - } - } else { - videoCss += `object-fit: ${PREF_RATIO} !important;`; - } - } - let css = ''; if (videoCss) { css = ` -div[data-testid="media-container"] { - display: flex; -} - #game-stream video { - margin: 0 auto; - align-self: center; - background: #000; ${videoCss} } `; } $elm.textContent = css; + + resizeVideoPlayer(); } +function resizeVideoPlayer() { + const $video = STATES.currentStream.$video; + if (!$video || !$video.parentElement) { + return; + } + + const PREF_RATIO = getPref(PrefKey.VIDEO_RATIO); + if (PREF_RATIO.includes(':')) { + const tmp = PREF_RATIO.split(':'); + + // Get preferred ratio + const videoRatio = parseFloat(tmp[0]) / parseFloat(tmp[1]); + + let width = 0; + let height = 0; + + // Get parent's ratio + const parentRect = $video.parentElement.getBoundingClientRect(); + const parentRatio = parentRect.width / parentRect.height; + + // Get target width & height + if (parentRatio > videoRatio) { + height = parentRect.height; + width = height * videoRatio; + } else { + width = parentRect.width; + height = width / videoRatio; + } + + // Prevent floating points + width = Math.floor(width); + height = Math.floor(height); + + // Update size + $video.style.width = `${width}px`; + $video.style.height = `${height}px`; + $video.style.objectFit = 'fill'; + } else { + $video.style.width = '100%'; + $video.style.height = '100%'; + $video.style.objectFit = PREF_RATIO; + } +} + + export function setupStreamUi() { // Prevent initializing multiple times if (!document.querySelector('.bx-quick-settings-bar')) {