probe videos in background

This commit is contained in:
Simon
2026-06-23 07:54:35 +00:00
parent a251b274db
commit 785b991d01
2 changed files with 106 additions and 7 deletions

View File

@@ -75,6 +75,19 @@ App.player = App.player || {};
return;
}
// Expand the candidate sources into an ordered playback plan. When a
// source has been proven (in the background) to play directly, try the
// raw upstream URL first and keep the proxy as the immediate fallback;
// unproven sources go straight through the proxy.
const directProven = (url) => !!(App.videos && App.videos.isDirectProven && App.videos.isDirectProven(url));
const playbackPlan = [];
sources.forEach((resolved) => {
if (directProven(resolved.url)) {
playbackPlan.push({ resolved, direct: true });
}
playbackPlan.push({ resolved, direct: false });
});
if (useMobileFullscreen) {
const host = getMobileVideoHost();
if (video.parentElement !== host) {
@@ -120,8 +133,9 @@ App.player = App.player || {};
// Attempts to play a single source. On a fatal failure it advances to
// the next candidate, or reports an error once the list is exhausted.
const attempt = async (index) => {
const resolved = sources[index];
const hasNext = index + 1 < sources.length;
const entry = playbackPlan[index];
const resolved = entry.resolved;
const hasNext = index + 1 < playbackPlan.length;
let playbackStarted = false;
let settled = false;
@@ -137,10 +151,17 @@ App.player = App.player || {};
}
};
const refererParam = resolved.referer ? `&referer=${encodeURIComponent(resolved.referer)}` : '';
const userAgentParam = resolved.userAgent ? `&User-Agent=${encodeURIComponent(resolved.userAgent)}` : '';
const liveParam = resolved.isLive ? '&live=1' : '';
const streamUrl = `/api/stream?url=${encodeURIComponent(resolved.url)}${refererParam}${userAgentParam}${liveParam}`;
// Proven-direct entries hit the upstream URL straight from the
// browser; everything else is wrapped in the backend stream proxy.
let streamUrl;
if (entry.direct) {
streamUrl = resolved.url;
} else {
const refererParam = resolved.referer ? `&referer=${encodeURIComponent(resolved.referer)}` : '';
const userAgentParam = resolved.userAgent ? `&User-Agent=${encodeURIComponent(resolved.userAgent)}` : '';
const liveParam = resolved.isLive ? '&live=1' : '';
streamUrl = `/api/stream?url=${encodeURIComponent(resolved.url)}${refererParam}${userAgentParam}${liveParam}`;
}
let isHls = /\.m3u8($|\?)/i.test(resolved.url);
let isDirectMedia = /\.(mp4|m4v|m4s|webm|ts|mov)($|\?)/i.test(resolved.url);
// Live cam streams resolve (server-side) to HLS; treat them as HLS up
@@ -165,7 +186,7 @@ App.player = App.player || {};
video.removeAttribute('src');
video.load();
if (!isHls) {
if (!isHls && !entry.direct) {
try {
const headResp = await fetch(streamUrl, { method: 'HEAD' });
const contentType = headResp.headers.get('Content-Type') || '';