diff --git a/src/core/services/subsync.test.ts b/src/core/services/subsync.test.ts index b23a7112..f1e16550 100644 --- a/src/core/services/subsync.test.ts +++ b/src/core/services/subsync.test.ts @@ -2398,3 +2398,89 @@ test('runSubsyncManual refuses to write ambiguous Shift_JIS/EUC-JP output even w assert.deepEqual(fs.readFileSync(targetPath), ambiguousBytes); assert.equal(fs.existsSync(retimedPath), false); }); + +test('triggerSubsyncFromConfig checks playback session after reading sub-delay when already in sync', async () => { + const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'subsync-delay-session-race-')); + const videoA = path.join(tmpDir, 'ep01.mkv'); + const videoB = path.join(tmpDir, 'ep02.mkv'); + const targetPath = path.join(tmpDir, 'ep01.ja.srt'); + const refPath = path.join(tmpDir, 'ep01.en.srt'); + fs.writeFileSync(videoA, 'a'); + fs.writeFileSync(videoB, 'b'); + + let t = 5; + const spans: Array<[number, number]> = []; + for (let i = 0; i < 60; i += 1) { + const dur = 1.2 + ((i * 7) % 5) * 0.4; + const gap = 0.6 + ((i * 13) % 7) * 0.7; + spans.push([t, t + dur]); + t += dur + gap; + } + const fmt = (sec: number) => { + const h = Math.floor(sec / 3600); + const m = Math.floor((sec % 3600) / 60); + const s = Math.floor(sec % 60); + const ms = Math.round((sec - Math.floor(sec)) * 1000); + return `${String(h).padStart(2, '0')}:${String(m).padStart(2, '0')}:${String(s).padStart(2, '0')},${String(ms).padStart(3, '0')}`; + }; + const srt = spans + .map(([s, e], i) => `${i + 1}\n${fmt(s)} --> ${fmt(e)}\nLine ${i + 1}\n`) + .join('\n'); + fs.writeFileSync(refPath, srt, 'utf8'); + fs.writeFileSync(targetPath, srt, 'utf8'); + + let switched = false; + const sentCommands: Array<(string | number)[]> = []; + await triggerSubsyncFromConfig( + makeDeps({ + getMpvClient: () => ({ + connected: true, + currentAudioStreamIndex: null, + send: (payload) => { + sentCommands.push(payload.command); + }, + requestProperty: async (name: string) => { + if (name === 'sub-delay') { + switched = true; + return 2.5; + } + if (name === 'path') return switched ? videoB : videoA; + if (name === 'sid') return 1; + if (name === 'secondary-sid') return 2; + if (name === 'duration') return 600; + if (name === 'track-list') { + return [ + { + id: 1, + type: 'sub', + selected: true, + external: true, + lang: 'jpn', + 'external-filename': targetPath, + }, + { + id: 2, + type: 'sub', + selected: true, + external: true, + lang: 'eng', + 'external-filename': refPath, + }, + ]; + } + return null; + }, + }), + getResolvedConfig: () => ({ + mode: 'auto', + alassPath: '/missing/alass', + ffsubsyncPath: '/missing/ffsubsync', + ffmpegPath: '/missing/ffmpeg', + replace: true, + }), + }), + ); + + assert.equal(switched, true); + assert.deepEqual(sentCommands, []); +}); diff --git a/src/core/services/subsync.ts b/src/core/services/subsync.ts index 378e1963..ac41dde5 100644 --- a/src/core/services/subsync.ts +++ b/src/core/services/subsync.ts @@ -1589,13 +1589,15 @@ async function runAutoSubsync(deps: TriggerSubsyncFromConfigDeps): Promise alignment: AlignmentResult, sourceLabel: string, ): Promise => { + const alreadyInSync = isAlreadyInSync(alignment); + const delayProperty = targetSlot === 'secondary' ? 'secondary-sub-delay' : 'sub-delay'; + const activeDelay = alreadyInSync + ? parseOptionalNumber(await client.requestProperty(delayProperty).catch(() => null)) + : null; if (!(await isPlaybackSessionUnchanged(client, context, targetTrack, targetSlot))) { return; } - if (isAlreadyInSync(alignment)) { - const delayProperty = targetSlot === 'secondary' ? 'secondary-sub-delay' : 'sub-delay'; - const activeDelayRaw = await client.requestProperty(delayProperty).catch(() => null); - const activeDelay = parseOptionalNumber(activeDelayRaw); + if (alreadyInSync) { if (activeDelay !== null && Math.abs(activeDelay) > 0.005) { client.send({ command: ['set_property', delayProperty, 0] }); deps.showMpvOsd(`Reset subtitle delay to 0s (already in sync with ${sourceLabel})`);