Wait longer for media reacquire

This commit is contained in:
Max Goodhart
2026-01-25 22:02:56 -08:00
parent 9a2a7c45e7
commit ce1d8e6713

View File

@@ -3,6 +3,8 @@ import throttle from 'lodash/throttle'
import { ContentDisplayOptions } from 'streamwall-shared' import { ContentDisplayOptions } from 'streamwall-shared'
const SCAN_THROTTLE = 500 const SCAN_THROTTLE = 500
const INITIAL_TIMEOUT = 10 * 1000
const REACQUIRE_ELEMENT_TIMEOUT = 60 * 1000
const VIDEO_OVERRIDE_STYLE = ` const VIDEO_OVERRIDE_STYLE = `
* { * {
@@ -142,7 +144,10 @@ async function waitForQuery(query: string): Promise<Element> {
}) })
} }
async function waitForVideo(kind: 'video' | 'audio'): Promise<{ async function waitForVideo(
kind: 'video' | 'audio',
timeoutMs = INITIAL_TIMEOUT,
): Promise<{
video?: HTMLMediaElement video?: HTMLMediaElement
iframe?: HTMLIFrameElement iframe?: HTMLIFrameElement
}> { }> {
@@ -150,7 +155,7 @@ async function waitForVideo(kind: 'video' | 'audio'): Promise<{
let video: Element | null | void = await Promise.race([ let video: Element | null | void = await Promise.race([
waitForQuery(kind), waitForQuery(kind),
sleep(10 * 1000), sleep(timeoutMs),
]) ])
if (video instanceof HTMLMediaElement) { if (video instanceof HTMLMediaElement) {
return { video } return { video }
@@ -186,12 +191,15 @@ const igHacks = {
}, },
} }
async function findMedia(kind: 'video' | 'audio') { async function findMedia(
kind: 'video' | 'audio',
elementTimeout = INITIAL_TIMEOUT,
) {
if (igHacks.isMatch()) { if (igHacks.isMatch()) {
await igHacks.onLoad() await igHacks.onLoad()
} }
const { video, iframe } = await waitForVideo(kind) const { video, iframe } = await waitForVideo(kind, elementTimeout)
if (!video) { if (!video) {
throw new Error('could not find video') throw new Error('could not find video')
} }
@@ -218,7 +226,7 @@ async function findMedia(kind: 'video' | 'audio') {
const videoReady = new Promise((resolve) => const videoReady = new Promise((resolve) =>
video.addEventListener('playing', resolve, { once: true }), video.addEventListener('playing', resolve, { once: true }),
) )
await Promise.race([videoReady, sleep(10 * 1000)]) await Promise.race([videoReady, sleep(INITIAL_TIMEOUT)])
if (!video.videoWidth) { if (!video.videoWidth) {
throw new Error('timeout waiting for video to start') throw new Error('timeout waiting for video to start')
} }
@@ -240,8 +248,8 @@ async function main() {
]) ])
let rotationController: RotationController | undefined let rotationController: RotationController | undefined
async function acquireMedia() { async function acquireMedia(elementTimeout: number) {
const media = await findMedia(content.kind) const media = await findMedia(content.kind, elementTimeout)
console.log('media acquired', media) console.log('media acquired', media)
if (content.kind === 'video' && media instanceof HTMLVideoElement) { if (content.kind === 'video' && media instanceof HTMLVideoElement) {
rotationController = new RotationController(media) rotationController = new RotationController(media)
@@ -250,7 +258,7 @@ async function main() {
'emptied', 'emptied',
async () => { async () => {
console.warn('media emptied, re-acquiring', media) console.warn('media emptied, re-acquiring', media)
const newMedia = await acquireMedia() const newMedia = await acquireMedia(REACQUIRE_ELEMENT_TIMEOUT)
if (newMedia !== media) { if (newMedia !== media) {
media.remove() media.remove()
} }
@@ -262,7 +270,7 @@ async function main() {
if (content.kind === 'video' || content.kind === 'audio') { if (content.kind === 'video' || content.kind === 'audio') {
webFrame.insertCSS(VIDEO_OVERRIDE_STYLE, { cssOrigin: 'user' }) webFrame.insertCSS(VIDEO_OVERRIDE_STYLE, { cssOrigin: 'user' })
acquireMedia() acquireMedia(INITIAL_TIMEOUT)
ipcRenderer.send('view-info', { ipcRenderer.send('view-info', {
info: { info: {
title: document.title, title: document.title,