Handle stopped streams, live bandwidth, and rework the upscaling tiers
When the server owner stops a stream, Plex cuts the connection and repeats the termination in every timeline reply to the client until it reports a stop. The player now reads those replies (also when playback stalls or fails), stops mpv, shows the owner's message and acknowledges it; a termination on a session's first reply is left over from an earlier stream and is cleared instead of cancelling the new one. Each title also starts unpaused, as mpv keeps pause across files. The dashboard's bandwidth graph refreshes every second. Plex's real-time samples are bytes per second with idle seconds omitted, so they are laid on a gap-free per-second axis (they were divided by six before). Upscaling: Balanced is now the strongest shader chain, light nlmeans, FSRCNNX 16, KrigBilateral and debanding. Measured against 1080p references shrunk to 480p it beat FSRCNNX 8, two network passes, RAVU and SSimSuperRes. The denoiser is AN3223's light profile with luma strength at 60%, which keeps texture the default profile smoothed away. Quality is now NVIDIA RTX Video Super Resolution through mpv's d3d11vpp filter, sized to the window and re-applied when it changes; it is offered only on Windows with an NVIDIA RTX GPU (app_info now lists the adapters), runs Balanced for HDR or at native size, and drops a tier if the driver refuses. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
ac5048f0b2
commit
d9e22dde2e
17 changed files with 316 additions and 524 deletions
|
|
@ -4,7 +4,7 @@ import { directUrl, previewUrl, reportTimeline, stopTranscode, transcodeUrl, typ
|
|||
import type { Item, Stream } from '../plex/types';
|
||||
import { useSession } from '../app/session';
|
||||
import { useSettings, SUBTITLE_MARGIN, type Preferences, type Quality, type Upscaling } from '../app/settings';
|
||||
import { noteMpvLog, requestEnhancement, watchPerformance } from './enhance';
|
||||
import { followVideoSize, noteMpvLog, requestEnhancement, watchPerformance } from './enhance';
|
||||
import { channelLabel } from '../app/format';
|
||||
import { invalidateLibrary } from '../app/queries';
|
||||
import { keepAwake, isNative, win } from '../platform/native';
|
||||
|
|
@ -41,6 +41,8 @@ interface PlayerState {
|
|||
quality: Quality;
|
||||
firstFrame: boolean;
|
||||
error: string;
|
||||
/** The server owner stopped this stream (error holds their message). */
|
||||
terminated: boolean;
|
||||
position: number;
|
||||
duration: number;
|
||||
cacheTime: number;
|
||||
|
|
@ -95,6 +97,8 @@ interface PlayerState {
|
|||
let session = '';
|
||||
let startOffsetMs = 0;
|
||||
let lastReport = 0;
|
||||
/** Plex has answered a report for this session without a termination. */
|
||||
let sessionAnswered = false;
|
||||
let reportTimer: ReturnType<typeof setInterval> | null = null;
|
||||
let countdownTimer: ReturnType<typeof setInterval> | null = null;
|
||||
let scrobbled = false;
|
||||
|
|
@ -208,14 +212,49 @@ async function applySubtitleStyle(prefs: Preferences = useSettings.getState()) {
|
|||
}
|
||||
}
|
||||
|
||||
function report(state: 'playing' | 'paused' | 'buffering' | 'stopped') {
|
||||
/** Reports progress to Plex; resolves true if the reply says the stream was stopped. */
|
||||
function report(state: 'playing' | 'paused' | 'buffering' | 'stopped'): Promise<boolean> {
|
||||
const s = server();
|
||||
const { item, position, delivery } = usePlayer.getState();
|
||||
if (!s || !item || !session || item.type === 'clip') return;
|
||||
if (!s || !item || !session || item.type === 'clip') return Promise.resolve(false);
|
||||
lastReport = Date.now();
|
||||
const extra: Record<string, string | number> = {};
|
||||
if (delivery?.mode === 'transcode' && delivery.session) extra['X-Plex-Session-Identifier'] = delivery.session;
|
||||
reportTimeline(s, item, state, position * 1000, session, extra).catch(() => {});
|
||||
const reported = session;
|
||||
return reportTimeline(s, item, state, position * 1000, session, extra)
|
||||
.then((reply) => {
|
||||
if (reported !== session) return false;
|
||||
if (!reply.terminationText) {
|
||||
sessionAnswered = true;
|
||||
return false;
|
||||
}
|
||||
// Plex repeats a termination to this client until it reports a stop.
|
||||
// On a session's first reply it is left over from an earlier stream
|
||||
// (this one can't have been stopped yet): clear it and carry on.
|
||||
reportTimeline(s, item, 'stopped', position * 1000, session, extra).catch(() => {});
|
||||
if (!sessionAnswered) return false;
|
||||
onTerminated(reply.terminationText);
|
||||
return true;
|
||||
})
|
||||
.catch(() => false);
|
||||
}
|
||||
|
||||
/**
|
||||
* The server owner stopped this stream. Plex cuts the connection, so mpv would
|
||||
* play out its buffer and then wait forever; stop it and show their message.
|
||||
*/
|
||||
function onTerminated(text: string) {
|
||||
const st = usePlayer.getState();
|
||||
if (!st.open || st.terminated) return;
|
||||
if (reportTimer) clearInterval(reportTimer);
|
||||
reportTimer = null;
|
||||
stopCountdown();
|
||||
keepAwake(false);
|
||||
watchPerformance(false);
|
||||
const s = server();
|
||||
if (st.delivery?.session && s) stopTranscode(s, st.delivery.session);
|
||||
usePlayer.setState({ error: text, terminated: true, buffering: false, countdown: null });
|
||||
if (isNative) mpv.command('stop').catch(() => {});
|
||||
}
|
||||
|
||||
function checkScrobble() {
|
||||
|
|
@ -366,6 +405,8 @@ function onEvent(e: MpvEvent) {
|
|||
break;
|
||||
case 'paused-for-cache':
|
||||
usePlayer.setState({ buffering: !!v });
|
||||
// A stall can mean the server stopped the stream; its reply says so.
|
||||
if (v && Date.now() - lastReport > 2000) report('buffering');
|
||||
break;
|
||||
case 'seeking':
|
||||
usePlayer.setState({ seeking: !!v });
|
||||
|
|
@ -435,8 +476,14 @@ function onEvent(e: MpvEvent) {
|
|||
break;
|
||||
case 'end-file':
|
||||
if (e.reason === 'error') {
|
||||
usePlayer.setState({ error: e.error ? `Playback failed: ${e.error}` : 'This video could not be played.' });
|
||||
fallbackAfterError();
|
||||
// A stream the server owner stopped can fail outright; don't retry that.
|
||||
// (Briefly: an unreachable server shouldn't hold up the error.)
|
||||
const failed = session;
|
||||
Promise.race([report('buffering'), new Promise<boolean>((r) => setTimeout(() => r(false), 3000))]).then((terminated) => {
|
||||
if (terminated || !usePlayer.getState().open || session !== failed) return;
|
||||
usePlayer.setState({ error: e.error ? `Playback failed: ${e.error}` : 'This video could not be played.' });
|
||||
fallbackAfterError();
|
||||
});
|
||||
} else if (e.reason === 'eof') onEnded();
|
||||
break;
|
||||
case 'log':
|
||||
|
|
@ -481,6 +528,7 @@ export const usePlayer = create<PlayerState>((set, get) => ({
|
|||
quality: 'original',
|
||||
firstFrame: false,
|
||||
error: '',
|
||||
terminated: false,
|
||||
position: 0,
|
||||
duration: 0,
|
||||
cacheTime: 0,
|
||||
|
|
@ -531,6 +579,7 @@ export const usePlayer = create<PlayerState>((set, get) => ({
|
|||
item: input,
|
||||
firstFrame: false,
|
||||
error: '',
|
||||
terminated: false,
|
||||
position: (opts.offsetMs ?? 0) / 1000,
|
||||
duration: input.duration / 1000,
|
||||
cacheTime: 0,
|
||||
|
|
@ -566,6 +615,7 @@ export const usePlayer = create<PlayerState>((set, get) => ({
|
|||
const local = useSession.getState().connection?.local ?? true;
|
||||
const quality: Quality = opts.quality ?? (local ? prefs.quality : prefs.remoteQuality);
|
||||
session = crypto.randomUUID();
|
||||
sessionAnswered = false;
|
||||
scrobbled = false;
|
||||
|
||||
let delivery: Delivery;
|
||||
|
|
@ -600,11 +650,14 @@ export const usePlayer = create<PlayerState>((set, get) => ({
|
|||
await mpv.init({ hwdec: prefs.hwdec, hdr: prefs.hdrPassthrough });
|
||||
if (!unlisten) unlisten = await mpv.on(onEvent);
|
||||
if (token !== loadToken) return;
|
||||
// mpv keeps pause across files: a title paused and closed would start paused.
|
||||
await mpv.set('pause', false);
|
||||
await mpv.set('mute', false);
|
||||
await mpv.set('volume', sliderToMpv(prefs.volume));
|
||||
await mpv.set('speed', 1);
|
||||
await mpv.set('audio-delay', 0);
|
||||
await applySubtitleStyle(prefs);
|
||||
followVideoSize();
|
||||
await requestEnhancement();
|
||||
watchPerformance(true);
|
||||
await mpv.set('force-media-title', item.type === 'episode' ? `${item.grandparentTitle} — ${item.title}` : item.title);
|
||||
|
|
@ -643,7 +696,7 @@ export const usePlayer = create<PlayerState>((set, get) => ({
|
|||
await mpv.command('stop').catch(() => {});
|
||||
await mpv.show(false).catch(() => {});
|
||||
}
|
||||
set({ open: false, mini: false, item: null, delivery: null, error: '', countdown: null, nextUp: null, firstFrame: false });
|
||||
set({ open: false, mini: false, item: null, delivery: null, error: '', terminated: false, countdown: null, nextUp: null, firstFrame: false });
|
||||
const fs = await win.isFullscreen().catch(() => false);
|
||||
if (fs && !wasFullscreenBeforePlay) win.setFullscreen(false);
|
||||
if (st.item) invalidateLibrary();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue