Skip to content

Commit

Permalink
Change the video dimensions when playing
Browse files Browse the repository at this point in the history
  • Loading branch information
sergystepanov committed Sep 27, 2024
1 parent 2084d09 commit 0d8db25
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions web/js/stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const state = {
w: 0,
h: 0,
aspect: 4 / 3,
fit: 'contain',
ready: false
}

Expand All @@ -33,20 +34,28 @@ const play = () => {
.then(() => {
state.ready = true
videoEl.poster = ''
resize(state.w, state.h, state.aspect, state.fit)
useCustomScreen(options.mirrorMode === 'mirror')
})
.catch(error => log.error('Can\'t autoplay', error))
}

const toggle = (show) => state.screen.toggleAttribute('hidden', show === undefined ? show : !show)

const resize = (w, h, aspect, fit) => {
if (!state.ready) return;

state.screen.setAttribute('width', '' + w)
state.screen.setAttribute('height', '' + h)
aspect !== undefined && (state.screen.style.aspectRatio = '' + aspect)
fit !== undefined && (state.screen.style['object-fit'] = fit)
}

// Track resize even when the underlying media stream changes its video size
videoEl.addEventListener('resize', () => {
recalculateSize()
if (state.screen === videoEl) return

state.screen.setAttribute('width', videoEl.videoWidth)
state.screen.setAttribute('height', videoEl.videoHeight)
resize(videoEl.videoWidth, videoEl.videoHeight)
})

videoEl.addEventListener('loadstart', () => {
Expand Down Expand Up @@ -110,8 +119,7 @@ const useCustomScreen = (use) => {

toggle(false)
state.screen = mirrorEl
state.screen.setAttribute('width', videoEl.videoWidth)
state.screen.setAttribute('height', videoEl.videoHeight)
resize(videoEl.videoWidth, videoEl.videoHeight)

// stretch depending on the video orientation
const isPortrait = videoEl.videoWidth < videoEl.videoHeight
Expand Down Expand Up @@ -155,12 +163,9 @@ sub(APP_VIDEO_CHANGED, (payload) => {

const a2 = (ww / hh).toFixed(6)

state.screen.style['object-fit'] = a > 1 && a.toFixed(6) !== a2 ? 'fill' : 'contain'
state.h = hh
state.w = Math.floor(hh * a)
state.screen.setAttribute('width', '' + ww)
state.screen.setAttribute('height', '' + hh)
state.screen.style.aspectRatio = '' + state.aspect
resize(ww, hh, state.aspect, a > 1 && a.toFixed(6) !== a2 ? 'fill' : 'contain')
recalculateSize()
})

Expand Down

0 comments on commit 0d8db25

Please sign in to comment.