fix: stabilize local subtitle startup and pause release

This commit is contained in:
2026-04-03 01:12:31 -07:00
parent 8a5805550f
commit 61ab1b76fc
18 changed files with 515 additions and 48 deletions

View File

@@ -2,7 +2,7 @@ import assert from 'node:assert/strict';
import test from 'node:test';
import { createAutoplayReadyGate } from './autoplay-ready-gate';
test('autoplay ready gate suppresses duplicate media signals unless forced while paused', async () => {
test('autoplay ready gate suppresses duplicate media signals for the same media', async () => {
const commands: Array<Array<string | boolean>> = [];
const scheduled: Array<() => void> = [];
@@ -31,7 +31,6 @@ test('autoplay ready gate suppresses duplicate media signals unless forced while
gate.maybeSignalPluginAutoplayReady({ text: '字幕', tokens: null });
gate.maybeSignalPluginAutoplayReady({ text: '字幕', tokens: null });
gate.maybeSignalPluginAutoplayReady({ text: '字幕', tokens: null }, { forceWhilePaused: true });
await new Promise((resolve) => setTimeout(resolve, 0));
const firstScheduled = scheduled.shift();
@@ -96,3 +95,49 @@ test('autoplay ready gate retry loop does not re-signal plugin readiness', async
true,
);
});
test('autoplay ready gate does not unpause again after a later manual pause on the same media', async () => {
const commands: Array<Array<string | boolean>> = [];
let playbackPaused = true;
const gate = createAutoplayReadyGate({
isAppOwnedFlowInFlight: () => false,
getCurrentMediaPath: () => '/media/video.mkv',
getCurrentVideoPath: () => null,
getPlaybackPaused: () => playbackPaused,
getMpvClient: () =>
({
connected: true,
requestProperty: async () => playbackPaused,
send: ({ command }: { command: Array<string | boolean> }) => {
commands.push(command);
if (command[0] === 'set_property' && command[1] === 'pause' && command[2] === false) {
playbackPaused = false;
}
},
}) as never,
signalPluginAutoplayReady: () => {
commands.push(['script-message', 'subminer-autoplay-ready']);
},
schedule: (callback) => {
queueMicrotask(callback);
return 1 as never;
},
logDebug: () => {},
});
gate.maybeSignalPluginAutoplayReady({ text: '字幕', tokens: null }, { forceWhilePaused: true });
await new Promise((resolve) => setTimeout(resolve, 0));
playbackPaused = true;
gate.maybeSignalPluginAutoplayReady({ text: '字幕その2', tokens: null }, { forceWhilePaused: true });
await new Promise((resolve) => setTimeout(resolve, 0));
assert.equal(
commands.filter(
(command) =>
command[0] === 'set_property' && command[1] === 'pause' && command[2] === false,
).length,
1,
);
});

View File

@@ -44,8 +44,6 @@ export function createAutoplayReadyGate(deps: AutoplayReadyGateDeps) {
deps.getCurrentVideoPath()?.trim() ||
'__unknown__';
const duplicateMediaSignal = autoPlayReadySignalMediaPath === mediaPath;
const allowDuplicateWhilePaused =
options?.forceWhilePaused === true && deps.getPlaybackPaused() !== false;
const releaseRetryDelayMs = 200;
const maxReleaseAttempts = resolveAutoplayReadyMaxReleaseAttempts({
forceWhilePaused: options?.forceWhilePaused === true,
@@ -104,19 +102,13 @@ export function createAutoplayReadyGate(deps: AutoplayReadyGateDeps) {
})();
};
if (duplicateMediaSignal && !allowDuplicateWhilePaused) {
return;
}
if (!duplicateMediaSignal) {
autoPlayReadySignalMediaPath = mediaPath;
const playbackGeneration = ++autoPlayReadySignalGeneration;
deps.signalPluginAutoplayReady();
attemptRelease(playbackGeneration, 0);
if (duplicateMediaSignal) {
return;
}
autoPlayReadySignalMediaPath = mediaPath;
const playbackGeneration = ++autoPlayReadySignalGeneration;
deps.signalPluginAutoplayReady();
attemptRelease(playbackGeneration, 0);
};

View File

@@ -0,0 +1,77 @@
import assert from 'node:assert/strict';
import test from 'node:test';
import {
createManagedLocalSubtitleSelectionRuntime,
resolveManagedLocalSubtitleSelection,
} from './local-subtitle-selection';
const mixedLanguageTrackList = [
{ type: 'sub', id: 1, lang: 'pt', title: '[Infinite]', external: false, selected: true },
{ type: 'sub', id: 2, lang: 'pt', title: '[Moshi Moshi]', external: false },
{ type: 'sub', id: 3, lang: 'en', title: '(Vivid)', external: false },
{ type: 'sub', id: 9, lang: 'en', title: 'English(US)', external: false },
{ type: 'sub', id: 11, lang: 'en', title: 'en.srt', external: true },
{ type: 'sub', id: 12, lang: 'ja', title: 'ja.srt', external: true },
];
test('resolveManagedLocalSubtitleSelection prefers default Japanese primary and English secondary tracks', () => {
const result = resolveManagedLocalSubtitleSelection({
trackList: mixedLanguageTrackList,
primaryLanguages: [],
secondaryLanguages: [],
});
assert.equal(result.primaryTrackId, 12);
assert.equal(result.secondaryTrackId, 11);
});
test('resolveManagedLocalSubtitleSelection respects configured language overrides', () => {
const result = resolveManagedLocalSubtitleSelection({
trackList: mixedLanguageTrackList,
primaryLanguages: ['pt'],
secondaryLanguages: ['ja'],
});
assert.equal(result.primaryTrackId, 1);
assert.equal(result.secondaryTrackId, 12);
});
test('managed local subtitle selection runtime applies preferred tracks once for a local media path', async () => {
const commands: Array<Array<string | number>> = [];
const scheduled: Array<() => void> = [];
const runtime = createManagedLocalSubtitleSelectionRuntime({
getCurrentMediaPath: () => '/videos/example.mkv',
getMpvClient: () =>
({
connected: true,
requestProperty: async (name: string) => {
if (name === 'track-list') {
return mixedLanguageTrackList;
}
throw new Error(`Unexpected property: ${name}`);
},
}) as never,
getPrimarySubtitleLanguages: () => [],
getSecondarySubtitleLanguages: () => [],
sendMpvCommand: (command) => {
commands.push(command);
},
schedule: (callback) => {
scheduled.push(callback);
return 1 as never;
},
clearScheduled: () => {},
});
runtime.handleMediaPathChange('/videos/example.mkv');
scheduled.shift()?.();
await new Promise((resolve) => setTimeout(resolve, 0));
runtime.handleSubtitleTrackListChange(mixedLanguageTrackList);
assert.deepEqual(commands, [
['set_property', 'sid', 12],
['set_property', 'secondary-sid', 11],
]);
});

View File

@@ -0,0 +1,261 @@
import path from 'node:path';
import { isRemoteMediaPath } from '../../jimaku/utils';
import { normalizeYoutubeLangCode } from '../../core/services/youtube/labels';
const DEFAULT_PRIMARY_SUBTITLE_LANGUAGES = ['ja', 'jpn'];
const DEFAULT_SECONDARY_SUBTITLE_LANGUAGES = ['en', 'eng', 'english', 'enus', 'en-us'];
const HEARING_IMPAIRED_PATTERN = /\b(hearing impaired|sdh|closed captions?|cc)\b/i;
type SubtitleTrackLike = {
type?: unknown;
id?: unknown;
lang?: unknown;
title?: unknown;
external?: unknown;
selected?: unknown;
};
type NormalizedSubtitleTrack = {
id: number;
lang: string;
title: string;
external: boolean;
selected: boolean;
};
export type ManagedLocalSubtitleSelection = {
primaryTrackId: number | null;
secondaryTrackId: number | null;
hasPrimaryMatch: boolean;
hasSecondaryMatch: boolean;
};
function parseTrackId(value: unknown): number | null {
if (typeof value === 'number' && Number.isInteger(value)) {
return value;
}
if (typeof value === 'string') {
const parsed = Number(value.trim());
return Number.isInteger(parsed) ? parsed : null;
}
return null;
}
function normalizeTrack(entry: unknown): NormalizedSubtitleTrack | null {
if (!entry || typeof entry !== 'object') {
return null;
}
const track = entry as SubtitleTrackLike;
const id = parseTrackId(track.id);
if (id === null || (track.type !== undefined && track.type !== 'sub')) {
return null;
}
return {
id,
lang: String(track.lang || '').trim(),
title: String(track.title || '').trim(),
external: track.external === true,
selected: track.selected === true,
};
}
function normalizeLanguageList(values: string[], fallback: string[]): string[] {
const normalized = values
.map((value) => normalizeYoutubeLangCode(value))
.filter((value, index, items) => value.length > 0 && items.indexOf(value) === index);
if (normalized.length > 0) {
return normalized;
}
return fallback
.map((value) => normalizeYoutubeLangCode(value))
.filter((value, index, items) => value.length > 0 && items.indexOf(value) === index);
}
function resolveLanguageRank(language: string, preferredLanguages: string[]): number {
const normalized = normalizeYoutubeLangCode(language);
if (!normalized) {
return Number.POSITIVE_INFINITY;
}
const directIndex = preferredLanguages.indexOf(normalized);
if (directIndex >= 0) {
return directIndex;
}
const base = normalized.split('-')[0] || normalized;
const baseIndex = preferredLanguages.indexOf(base);
return baseIndex >= 0 ? baseIndex : Number.POSITIVE_INFINITY;
}
function isLikelyHearingImpaired(title: string): boolean {
return HEARING_IMPAIRED_PATTERN.test(title);
}
function pickBestTrackId(
tracks: NormalizedSubtitleTrack[],
preferredLanguages: string[],
excludeId: number | null = null,
): { trackId: number | null; hasMatch: boolean } {
const ranked = tracks
.filter((track) => track.id !== excludeId)
.map((track) => ({
track,
languageRank: resolveLanguageRank(track.lang, preferredLanguages),
}))
.filter(({ languageRank }) => Number.isFinite(languageRank))
.sort((left, right) => {
if (left.languageRank !== right.languageRank) {
return left.languageRank - right.languageRank;
}
if (left.track.external !== right.track.external) {
return left.track.external ? -1 : 1;
}
if (isLikelyHearingImpaired(left.track.title) !== isLikelyHearingImpaired(right.track.title)) {
return isLikelyHearingImpaired(left.track.title) ? 1 : -1;
}
if (/\bdefault\b/i.test(left.track.title) !== /\bdefault\b/i.test(right.track.title)) {
return /\bdefault\b/i.test(left.track.title) ? -1 : 1;
}
return left.track.id - right.track.id;
});
return {
trackId: ranked[0]?.track.id ?? null,
hasMatch: ranked.length > 0,
};
}
export function resolveManagedLocalSubtitleSelection(input: {
trackList: unknown[] | null;
primaryLanguages: string[];
secondaryLanguages: string[];
}): ManagedLocalSubtitleSelection {
const tracks = Array.isArray(input.trackList)
? input.trackList.map(normalizeTrack).filter((track): track is NormalizedSubtitleTrack => track !== null)
: [];
const preferredPrimaryLanguages = normalizeLanguageList(
input.primaryLanguages,
DEFAULT_PRIMARY_SUBTITLE_LANGUAGES,
);
const preferredSecondaryLanguages = normalizeLanguageList(
input.secondaryLanguages,
DEFAULT_SECONDARY_SUBTITLE_LANGUAGES,
);
const primary = pickBestTrackId(tracks, preferredPrimaryLanguages);
const secondary = pickBestTrackId(tracks, preferredSecondaryLanguages, primary.trackId);
return {
primaryTrackId: primary.trackId,
secondaryTrackId: secondary.trackId,
hasPrimaryMatch: primary.hasMatch,
hasSecondaryMatch: secondary.hasMatch,
};
}
function normalizeLocalMediaPath(mediaPath: string | null | undefined): string | null {
if (typeof mediaPath !== 'string') {
return null;
}
const trimmed = mediaPath.trim();
if (!trimmed || isRemoteMediaPath(trimmed)) {
return null;
}
return path.resolve(trimmed);
}
export function createManagedLocalSubtitleSelectionRuntime(deps: {
getCurrentMediaPath: () => string | null;
getMpvClient: () =>
| {
connected?: boolean;
requestProperty?: (name: string) => Promise<unknown>;
}
| null;
getPrimarySubtitleLanguages: () => string[];
getSecondarySubtitleLanguages: () => string[];
sendMpvCommand: (command: ['set_property', 'sid' | 'secondary-sid', number]) => void;
schedule: (callback: () => void, delayMs: number) => ReturnType<typeof setTimeout>;
clearScheduled: (timer: ReturnType<typeof setTimeout>) => void;
delayMs?: number;
}) {
const delayMs = deps.delayMs ?? 400;
let currentMediaPath: string | null = null;
let appliedMediaPath: string | null = null;
let pendingTimer: ReturnType<typeof setTimeout> | null = null;
const clearPendingTimer = (): void => {
if (!pendingTimer) {
return;
}
deps.clearScheduled(pendingTimer);
pendingTimer = null;
};
const maybeApplySelection = (trackList: unknown[] | null): void => {
if (!currentMediaPath || appliedMediaPath === currentMediaPath) {
return;
}
const selection = resolveManagedLocalSubtitleSelection({
trackList,
primaryLanguages: deps.getPrimarySubtitleLanguages(),
secondaryLanguages: deps.getSecondarySubtitleLanguages(),
});
if (!selection.hasPrimaryMatch && !selection.hasSecondaryMatch) {
return;
}
if (selection.primaryTrackId !== null) {
deps.sendMpvCommand(['set_property', 'sid', selection.primaryTrackId]);
}
if (selection.secondaryTrackId !== null) {
deps.sendMpvCommand(['set_property', 'secondary-sid', selection.secondaryTrackId]);
}
appliedMediaPath = currentMediaPath;
clearPendingTimer();
};
const refreshFromMpv = async (): Promise<void> => {
const client = deps.getMpvClient();
if (!client?.connected || !client.requestProperty) {
return;
}
const mediaPath = normalizeLocalMediaPath(deps.getCurrentMediaPath());
if (!mediaPath || mediaPath !== currentMediaPath) {
return;
}
try {
const trackList = await client.requestProperty('track-list');
maybeApplySelection(Array.isArray(trackList) ? trackList : null);
} catch {
// Skip selection when mpv track inspection fails.
}
};
const scheduleRefresh = (): void => {
clearPendingTimer();
if (!currentMediaPath || appliedMediaPath === currentMediaPath) {
return;
}
pendingTimer = deps.schedule(() => {
pendingTimer = null;
void refreshFromMpv();
}, delayMs);
};
return {
handleMediaPathChange: (mediaPath: string | null | undefined): void => {
const normalizedPath = normalizeLocalMediaPath(mediaPath);
if (normalizedPath !== currentMediaPath) {
appliedMediaPath = null;
}
currentMediaPath = normalizedPath;
if (!currentMediaPath) {
clearPendingTimer();
return;
}
scheduleRefresh();
},
handleSubtitleTrackListChange: (trackList: unknown[] | null): void => {
maybeApplySelection(trackList);
},
};
}

View File

@@ -24,9 +24,15 @@ export type PlaylistBrowserIpcRuntime = {
export function createPlaylistBrowserIpcRuntime(
getMpvClient: PlaylistBrowserRuntimeDeps['getMpvClient'],
options?: Pick<
PlaylistBrowserRuntimeDeps,
'getPrimarySubtitleLanguages' | 'getSecondarySubtitleLanguages'
>,
): PlaylistBrowserIpcRuntime {
const playlistBrowserRuntimeDeps: PlaylistBrowserRuntimeDeps = {
getMpvClient,
getPrimarySubtitleLanguages: options?.getPrimarySubtitleLanguages,
getSecondarySubtitleLanguages: options?.getSecondarySubtitleLanguages,
};
return {

View File

@@ -267,6 +267,7 @@ test('playlist-browser mutation runtimes mutate queue and return refreshed snaps
]);
assert.deepEqual(scheduled.map((entry) => entry.delayMs), [400]);
scheduled[0]?.callback();
await new Promise((resolve) => setTimeout(resolve, 0));
assert.deepEqual(mpvClient.getCommands().slice(-2), [
['set_property', 'sid', 'auto'],
['set_property', 'secondary-sid', 'auto'],
@@ -472,6 +473,7 @@ test('playPlaylistBrowserIndexRuntime ignores superseded local subtitle rearm ca
scheduled[0]?.();
scheduled[1]?.();
await new Promise((resolve) => setTimeout(resolve, 0));
assert.deepEqual(
mpvClient.getCommands().slice(-6),
@@ -485,3 +487,52 @@ test('playPlaylistBrowserIndexRuntime ignores superseded local subtitle rearm ca
],
);
});
test('playlist-browser playback reapplies configured preferred subtitle tracks when track metadata is available', async (t) => {
const dir = createTempVideoDir(t);
const episode1 = path.join(dir, 'Show - S01E01.mkv');
const episode2 = path.join(dir, 'Show - S01E02.mkv');
fs.writeFileSync(episode1, '');
fs.writeFileSync(episode2, '');
const mpvClient = createFakeMpvClient({
currentVideoPath: episode1,
playlist: [
{ filename: episode1, current: true, title: 'Episode 1' },
{ filename: episode2, title: 'Episode 2' },
],
});
const requestProperty = mpvClient.requestProperty.bind(mpvClient);
mpvClient.requestProperty = async (name: string): Promise<unknown> => {
if (name === 'track-list') {
return [
{ type: 'sub', id: 1, lang: 'pt', title: '[Infinite]', external: false, selected: true },
{ type: 'sub', id: 3, lang: 'en', title: 'English', external: false },
{ type: 'sub', id: 11, lang: 'en', title: 'en.srt', external: true },
{ type: 'sub', id: 12, lang: 'ja', title: 'ja.srt', external: true },
];
}
return requestProperty(name);
};
const scheduled: Array<() => void> = [];
const deps = {
getMpvClient: () => mpvClient,
getPrimarySubtitleLanguages: () => [],
getSecondarySubtitleLanguages: () => [],
schedule: (callback: () => void) => {
scheduled.push(callback);
},
};
const result = await playPlaylistBrowserIndexRuntime(deps, 1);
assert.equal(result.ok, true);
scheduled[0]?.();
await new Promise((resolve) => setTimeout(resolve, 0));
assert.deepEqual(mpvClient.getCommands().slice(-2), [
['set_property', 'sid', 12],
['set_property', 'secondary-sid', 11],
]);
});

View File

@@ -8,6 +8,7 @@ import type {
} from '../../types';
import { isRemoteMediaPath } from '../../jimaku/utils';
import { hasVideoExtension } from '../../shared/video-extensions';
import { resolveManagedLocalSubtitleSelection } from './local-subtitle-selection';
import { sortPlaylistBrowserDirectoryItems } from './playlist-browser-sort';
type PlaylistLike = {
@@ -28,6 +29,8 @@ type MpvPlaylistBrowserClientLike = {
export type PlaylistBrowserRuntimeDeps = {
getMpvClient: () => MpvPlaylistBrowserClientLike | null;
schedule?: (callback: () => void, delayMs: number) => void;
getPrimarySubtitleLanguages?: () => string[];
getSecondarySubtitleLanguages?: () => string[];
};
const pendingLocalSubtitleSelectionRearms = new WeakMap<MpvPlaylistBrowserClientLike, number>();
@@ -229,9 +232,20 @@ async function buildMutationResult(
};
}
function rearmLocalSubtitleSelection(client: MpvPlaylistBrowserClientLike): void {
client.send({ command: ['set_property', 'sid', 'auto'] });
client.send({ command: ['set_property', 'secondary-sid', 'auto'] });
async function rearmLocalSubtitleSelection(
client: MpvPlaylistBrowserClientLike,
deps: PlaylistBrowserRuntimeDeps,
): Promise<void> {
const trackList = await readProperty(client, 'track-list');
const selection = resolveManagedLocalSubtitleSelection({
trackList: Array.isArray(trackList) ? trackList : null,
primaryLanguages: deps.getPrimarySubtitleLanguages?.() ?? [],
secondaryLanguages: deps.getSecondarySubtitleLanguages?.() ?? [],
});
client.send({ command: ['set_property', 'sid', selection.primaryTrackId ?? 'auto'] });
client.send({
command: ['set_property', 'secondary-sid', selection.secondaryTrackId ?? 'auto'],
});
}
function prepareLocalSubtitleAutoload(client: MpvPlaylistBrowserClientLike): void {
@@ -258,7 +272,7 @@ function scheduleLocalSubtitleSelectionRearm(
if (currentPath && path.resolve(currentPath) !== expectedPath) {
return;
}
rearmLocalSubtitleSelection(client);
void rearmLocalSubtitleSelection(client, deps);
}, 400);
}