fix(mpv): reset per-file state when a same-file reload skips the path clear (#323)

mpv only sends the latest value of an observed property, and only after its
IPC thread has handled queued commands and events. A same-file
`loadfile replace` can therefore arrive with no `path=null`, so path
comparisons see no change, the autoplay gate stays released for that path and
drops every ready signal, and playback stays paused.

Track an `end-file` that mpv has not followed with a different path. If it is
still pending at `file-loaded`, replay the clear and read the path back, so
consumers see the same sequence as any other load. `file-loaded` waits for the
reply so plugins can read their media snapshot during the event.

Fixes #320
This commit is contained in:
sudacode authored and GitHub committed 2026-11-03 21:40:14 -08:00
1 parent 8e29cb3c55
commit 1c50d249e0
4 files changed
+167 -11

No files matched your search

+51 -11
View File
@@ -55,6 +55,7 @@ export const MPV_REQUEST_ID_SUB_SHADOW_OFFSET = 120;
export const MPV_REQUEST_ID_SUB_ASS_OVERRIDE = 121;
export const MPV_REQUEST_ID_SUB_USE_MARGINS = 122;
export const MPV_REQUEST_ID_PAUSE = 123;
export const MPV_REQUEST_ID_RELOADED_PATH = 124;
export const MPV_REQUEST_ID_TRACK_LIST_SECONDARY = 200;
export const MPV_REQUEST_ID_TRACK_LIST_AUDIO = 201;
export const MPV_REQUEST_ID_MEDIA_TITLE = 202;
@@ -109,7 +110,11 @@ export interface MpvProtocolHandleMessageDeps {
getPauseAtTime: () => number | null;
setPauseAtTime: (value: number | null) => void;
autoLoadSecondarySubTrack: (path: string) => void;
getCurrentVideoPath: () => string;
setCurrentVideoPath: (value: string) => void;
/** True from `end-file` until mpv reports a different path or the next `file-loaded`. */
getAwaitingPathChangeAfterEndFile: () => boolean;
setAwaitingPathChangeAfterEndFile: (value: boolean) => void;
emitSecondarySubtitleVisibility: (payload: { visible: boolean }) => void;
setPreviousSecondarySubVisibility: (visible: boolean) => void;
setCurrentAudioStreamIndex: (
@@ -380,18 +385,10 @@ export async function dispatchMpvProtocolMessage(
applyMediaTitle(deps, msg.data);
} else if (msg.name === 'path') {
const path = (msg.data as string) || '';
deps.setCurrentVideoPath(path);
// A forced title set before loadfile arrives ahead of the path change that clears the
// cached title and never fires again, so read it back once the new path is known.
if (path) {
deps.sendCommand({
command: ['get_property', 'media-title'],
request_id: MPV_REQUEST_ID_MEDIA_TITLE,
});
if (path !== deps.getCurrentVideoPath()) {
deps.setAwaitingPathChangeAfterEndFile(false);
}
deps.emitMediaPathChange({ path });
deps.autoLoadSecondarySubTrack(path);
deps.syncCurrentAudioStreamIndex();
applyMediaPath(deps, path);
} else if (msg.name === 'sub-pos') {
deps.emitSubtitleMetricsChange({ subPos: msg.data as number });
} else if (msg.name === 'sub-font-size') {
@@ -464,9 +461,26 @@ export async function dispatchMpvProtocolMessage(
deps.emitClientMessage?.({ args });
}
} else if (msg.event === 'file-loaded') {
if (deps.getAwaitingPathChangeAfterEndFile()) {
// mpv only sends the latest value of an observed property, once its IPC thread gets to it, so
// a same-file reload can skip the path clear. The reload then looks like nothing happened and
// per-file state (autoplay gate, subtitles) never resets. Replay the clear so consumers see
// the same sequence as any other load. Read the path back because the unchanged value may
// never be reported, and hold file-loaded until the reply so listeners can read the path.
deps.setAwaitingPathChangeAfterEndFile(false);
applyMediaPath(deps, '');
deps.sendCommand({
command: ['get_property', 'path'],
request_id: MPV_REQUEST_ID_RELOADED_PATH,
});
return;
}
deps.emitFileLoaded?.();
} else if (msg.event === 'end-file') {
deps.emitEndFile?.(parseEndFileEvent(msg));
if (deps.getCurrentVideoPath()) {
deps.setAwaitingPathChangeAfterEndFile(true);
}
} else if (msg.event === 'shutdown') {
deps.restorePreviousSecondarySubVisibility();
if (deps.shouldQuitOnMpvShutdown()) {
@@ -477,6 +491,17 @@ export async function dispatchMpvProtocolMessage(
return;
}
// Before the no-data check: an error reply still owes the file-loaded held for this read.
if (msg.request_id === MPV_REQUEST_ID_RELOADED_PATH) {
// The path property event may have restored the path before this reply arrived.
const path = (msg.data as string) || '';
if (path && !deps.getCurrentVideoPath()) {
applyMediaPath(deps, path);
}
deps.emitFileLoaded?.();
return;
}
if (msg.data === undefined) {
return;
}
@@ -618,6 +643,21 @@ function applyMediaTitle(
deps.emitMediaTitleChange({ title });
}
function applyMediaPath(deps: MpvProtocolHandleMessageDeps, path: string): void {
deps.setCurrentVideoPath(path);
// A forced title set before loadfile arrives ahead of the path change that clears the
// cached title and never fires again, so read it back once the new path is known.
if (path) {
deps.sendCommand({
command: ['get_property', 'media-title'],
request_id: MPV_REQUEST_ID_MEDIA_TITLE,
});
}
deps.emitMediaPathChange({ path });
deps.autoLoadSecondarySubTrack(path);
deps.syncCurrentAudioStreamIndex();
}
export function parseVisibilityProperty(value: unknown): boolean | null {
if (typeof value === 'boolean') return value;
if (typeof value !== 'string') return null;