mirror of
https://github.com/ksyasuda/SubMiner.git
synced 2026-05-26 00:55:16 -07:00
b1bdeabca8
* fix(jellyfin): show overlay, inject plugin, and fix stats title on playb - Show visible overlay automatically during Jellyfin playback so subtitleStyle applies - Inject bundled mpv plugin on auto-launch so keybindings work without overlay focus - Group Jellyfin playback stats under item metadata (jellyfin://host/item/id) instead of stream URLs so episodes merge with matching local titles - Mark ffsubsync unavailable in subsync modal for remote media paths - Drain queued second-instance commands even when onReady throws * fix(overlay): stabilize macOS focus handoff and sidebar Yomitan pause - Keep overlay visible during macOS foreground probe after overlay blur - Hold sidebar hover-pause while a Yomitan lookup popup remains open * fix(jellyfin): fix discovery loop, device identity, tray state, and Disc - Derive device identity from OS hostname; remove legacy configurable client/device fields - Prevent discovery playback from reloading active item, misreporting pause state, and duplicate overlay restores - Restart stale tray discovery sessions without re-login when server drops SubMiner cast target - Sync tray discovery checkbox state on Linux after CLI/startup/remote-session changes - Stop Discord presence falling back to stream URLs; prime title before tokenized stream loads - Fix picker library discovery when log level is above info - Fix config.example.jsonc trailing commas and array formatting * docs(release): trim and consolidate prerelease notes for 0.15.0 - Remove breaking changes section and several redundant bullet points - Consolidate per-platform updater notes into a single entry - Normalize em-dash separators to hyphens in section headers * fix(config): remove trailing commas from config.example.jsonc - Strip trailing commas throughout both config.example.jsonc copies - Reformat inline arrays to multi-line for JSON strictness - Update Jellyfin subtitle preload and playback launch tests and impl * fix(tokenizer): preserve known-word highlight when POS filters suppress - Known-word cache matches now set isKnown=true even for tokens excluded by POS filters - POS exclusion gate suppresses N+1, frequency, and JLPT only; known status is computed before the gate - Jellyfin subtitle preload continues after cleanup failures instead of aborting - Update config docs and option description to document the known-word bypass behavior * fix(jellyfin): send explicit hide/show overlay instead of toggle - Track overlay visibility in plugin state; y-t uses explicit hide/show commands when state is known - Prevent paused Jellyfin playback from resuming on overlay hide - Fix subtitle cache cleanup to only remove dirs after successful cleanup * fix(jellyfin): fix remote progress sync, seek reporting, and startup sto - arm active playback before loadfile with loadedMediaPath: null to suppress premature stop events - force immediate progress report on seek-like position jumps at the mpv time-pos level - send positionTicks and failed=false in reportStopped payload - remove EventName from HTTP timeline payloads (websocket-only field) - add startup grace window to drop stop events before media finishes loading * fix(jellyfin): fix overlay toggle sync, redirect reload, and AppImage bi - Sync visible-overlay state back to plugin via script messages to avoid toggle/hide drift - Collapse duplicate toggle events within 250ms to prevent hide-then-show on single keypress - Preserve manual hide across Jellyfin path-changing redirects even when media-title drops - Rearm managed subtitle defaults on path-changing redirects - Route toggleVisibleOverlay session binding through plugin toggle instead of app-side IPC - Show Linux/Hyprland overlay passively (showInactive) to avoid stealing mpv keyboard focus - Fix AppImage binary resolution to prefer $APPIMAGE env over mounted inner binary - Add stats window layer management so delete/update dialogs appear above stats window - Fix Jellyfin remote progress sync during Linux websocket reconnect windows * Fix CodeRabbit review feedback * fix(jellyfin): subtitle timing, resume progress, and overlay sync - Add per-stream subtitle delay persistence and auto timeline-offset correction - Strip server-selected subtitle stream from mpv load URL; suppress plugin subtitle rearm and auto-start during app-managed preload - Fix resume position lost when mpv resets on stop; use last known position for final progress/stopped reports - Keep Play vs Resume distinct to avoid early seek race on normal play - Fix discovery resume when remote play sends StartPositionTicks=0 despite saved progress - Deduplicate show/hide overlay commands using recorded visibility state - Rewrite docs-site Jellyfin page around cast-to-device UX * test: update lifecycle cleanup assertion * fix: clear aborted playback state, fix overlay passthrough, and guard du - Reset app_managed_playback_pending on lifecycle cleanup to prevent state leak into next item - Record visible overlay action only after command succeeds, not before - Non-native passive overlay now always click-through on re-show (fix isNonNativePassiveOverlay ordering) - Defer activeParsedSubtitleMediaPath assignment until after prefetch completes - Move autoplay gate release into the hide branch of toggleVisibleOverlay - Clear active Jellyfin playback when stopping media that never loaded - Reset managed subtitle delay and delay key when no external tracks are available - Await async removeDir in subtitle cache cleanup - Guard duplicate delete clicks in MediaDetailView and SessionsTab with refs - Escape key in DeleteConfirmDialog now calls stopPropagation and stopImmediatePropagation
521 lines
20 KiB
TypeScript
521 lines
20 KiB
TypeScript
import { MpvSubtitleRenderMetrics } from '../../types';
|
|
|
|
export type MpvMessage = {
|
|
event?: string;
|
|
name?: string;
|
|
data?: unknown;
|
|
request_id?: number;
|
|
error?: string;
|
|
};
|
|
|
|
export const MPV_REQUEST_ID_SUBTEXT = 101;
|
|
export const MPV_REQUEST_ID_PATH = 102;
|
|
export const MPV_REQUEST_ID_SECONDARY_SUBTEXT = 103;
|
|
export const MPV_REQUEST_ID_SECONDARY_SUB_VISIBILITY = 104;
|
|
export const MPV_REQUEST_ID_AID = 105;
|
|
export const MPV_REQUEST_ID_SUB_POS = 106;
|
|
export const MPV_REQUEST_ID_SUB_FONT_SIZE = 107;
|
|
export const MPV_REQUEST_ID_SUB_SCALE = 108;
|
|
export const MPV_REQUEST_ID_SUB_MARGIN_Y = 109;
|
|
export const MPV_REQUEST_ID_SUB_MARGIN_X = 110;
|
|
export const MPV_REQUEST_ID_SUB_FONT = 111;
|
|
export const MPV_REQUEST_ID_SUB_SCALE_BY_WINDOW = 112;
|
|
export const MPV_REQUEST_ID_OSD_HEIGHT = 113;
|
|
export const MPV_REQUEST_ID_OSD_DIMENSIONS = 114;
|
|
export const MPV_REQUEST_ID_SUBTEXT_ASS = 115;
|
|
export const MPV_REQUEST_ID_SUB_SPACING = 116;
|
|
export const MPV_REQUEST_ID_SUB_BOLD = 117;
|
|
export const MPV_REQUEST_ID_SUB_ITALIC = 118;
|
|
export const MPV_REQUEST_ID_SUB_BORDER_SIZE = 119;
|
|
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_TRACK_LIST_SECONDARY = 200;
|
|
export const MPV_REQUEST_ID_TRACK_LIST_AUDIO = 201;
|
|
|
|
export type MpvMessageParser = (message: MpvMessage) => void;
|
|
export type MpvParseErrorHandler = (line: string, error: unknown) => void;
|
|
|
|
export interface MpvProtocolParseResult {
|
|
messages: MpvMessage[];
|
|
nextBuffer: string;
|
|
}
|
|
|
|
export interface MpvProtocolHandleMessageDeps {
|
|
getResolvedConfig: () => {
|
|
secondarySub?: { secondarySubLanguages?: Array<string> };
|
|
};
|
|
getSubtitleMetrics: () => MpvSubtitleRenderMetrics;
|
|
isVisibleOverlayVisible: () => boolean;
|
|
emitSubtitleChange: (payload: { text: string; isOverlayVisible: boolean }) => void;
|
|
emitSubtitleAssChange: (payload: { text: string }) => void;
|
|
emitSubtitleTiming: (payload: { text: string; start: number; end: number }) => void;
|
|
emitSecondarySubtitleChange: (payload: { text: string }) => void;
|
|
emitSubtitleTrackChange: (payload: { sid: number | null }) => void;
|
|
emitSubtitleTrackListChange: (payload: { trackList: unknown[] | null }) => void;
|
|
getCurrentSubText: () => string;
|
|
setCurrentSubText: (text: string) => void;
|
|
setCurrentSubStart: (value: number) => void;
|
|
getCurrentSubStart: () => number;
|
|
setCurrentSubEnd: (value: number) => void;
|
|
getCurrentSubEnd: () => number;
|
|
emitMediaPathChange: (payload: { path: string }) => void;
|
|
emitMediaTitleChange: (payload: { title: string | null }) => void;
|
|
emitTimePosChange: (payload: { time: number }) => void;
|
|
emitDurationChange: (payload: { duration: number }) => void;
|
|
emitPauseChange: (payload: { paused: boolean }) => void;
|
|
emitFullscreenChange: (payload: { fullscreen: boolean }) => void;
|
|
emitSubtitleMetricsChange: (payload: Partial<MpvSubtitleRenderMetrics>) => void;
|
|
setCurrentSecondarySubText: (text: string) => void;
|
|
resolvePendingRequest: (requestId: number, message: MpvMessage) => boolean;
|
|
setSecondarySubVisibility: (visible: boolean) => void;
|
|
syncCurrentAudioStreamIndex: () => void;
|
|
setCurrentAudioTrackId: (value: number | null) => void;
|
|
setCurrentTimePos: (value: number) => void;
|
|
getCurrentTimePos: () => number;
|
|
getPendingPauseAtSubEnd: () => boolean;
|
|
setPendingPauseAtSubEnd: (value: boolean) => void;
|
|
getPauseAtTime: () => number | null;
|
|
setPauseAtTime: (value: number | null) => void;
|
|
autoLoadSecondarySubTrack: (path: string) => void;
|
|
setCurrentVideoPath: (value: string) => void;
|
|
emitSecondarySubtitleVisibility: (payload: { visible: boolean }) => void;
|
|
setPreviousSecondarySubVisibility: (visible: boolean) => void;
|
|
setCurrentAudioStreamIndex: (
|
|
tracks: Array<{
|
|
type?: string;
|
|
id?: number;
|
|
selected?: boolean;
|
|
'ff-index'?: number;
|
|
}>,
|
|
) => void;
|
|
sendCommand: (payload: { command: unknown[]; request_id?: number }) => boolean;
|
|
restorePreviousSecondarySubVisibility: () => void;
|
|
shouldQuitOnMpvShutdown: () => boolean;
|
|
requestAppQuit: () => void;
|
|
}
|
|
|
|
type SubtitleTrackCandidate = {
|
|
id: number;
|
|
lang: string;
|
|
title: string;
|
|
selected: boolean;
|
|
external: boolean;
|
|
externalFilename: string | null;
|
|
};
|
|
|
|
function normalizeSubtitleTrackCandidate(
|
|
track: Record<string, unknown>,
|
|
): SubtitleTrackCandidate | null {
|
|
const id =
|
|
typeof track.id === 'number'
|
|
? track.id
|
|
: typeof track.id === 'string'
|
|
? Number(track.id.trim())
|
|
: Number.NaN;
|
|
if (!Number.isInteger(id)) {
|
|
return null;
|
|
}
|
|
|
|
const externalFilename =
|
|
typeof track['external-filename'] === 'string' && track['external-filename'].trim().length > 0
|
|
? track['external-filename'].trim()
|
|
: typeof track.external_filename === 'string' && track.external_filename.trim().length > 0
|
|
? track.external_filename.trim()
|
|
: null;
|
|
|
|
return {
|
|
id,
|
|
lang: String(track.lang || '')
|
|
.trim()
|
|
.toLowerCase(),
|
|
title: String(track.title || '')
|
|
.trim()
|
|
.toLowerCase(),
|
|
selected: track.selected === true,
|
|
external: track.external === true,
|
|
externalFilename,
|
|
};
|
|
}
|
|
|
|
function getSubtitleTrackIdentity(track: SubtitleTrackCandidate): string {
|
|
if (track.externalFilename) {
|
|
return `external:${track.externalFilename.toLowerCase()}`;
|
|
}
|
|
if (track.title.length > 0) {
|
|
return `title:${track.title}`;
|
|
}
|
|
return `id:${track.id}`;
|
|
}
|
|
|
|
function pickSecondarySubtitleTrackId(
|
|
tracks: Array<Record<string, unknown>>,
|
|
preferredLanguages: string[],
|
|
): number | null {
|
|
const normalizedLanguages = preferredLanguages
|
|
.map((language) => language.trim().toLowerCase())
|
|
.filter((language) => language.length > 0);
|
|
if (normalizedLanguages.length === 0) {
|
|
return null;
|
|
}
|
|
|
|
const subtitleTracks = tracks
|
|
.filter((track) => track.type === 'sub')
|
|
.map(normalizeSubtitleTrackCandidate)
|
|
.filter((track): track is SubtitleTrackCandidate => track !== null);
|
|
|
|
const dedupedTracks = new Map<string, SubtitleTrackCandidate>();
|
|
for (const track of subtitleTracks) {
|
|
const identity = getSubtitleTrackIdentity(track);
|
|
const existing = dedupedTracks.get(identity);
|
|
if (!existing || (track.selected && !existing.selected)) {
|
|
dedupedTracks.set(identity, track);
|
|
}
|
|
}
|
|
|
|
const uniqueTracks = [...dedupedTracks.values()];
|
|
|
|
for (const language of normalizedLanguages) {
|
|
const selectedMatch = uniqueTracks.find((track) => track.selected && track.lang === language);
|
|
if (selectedMatch) {
|
|
return selectedMatch.id;
|
|
}
|
|
|
|
const match = uniqueTracks.find((track) => track.lang === language);
|
|
if (match) {
|
|
return match.id;
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
export function splitMpvMessagesFromBuffer(
|
|
buffer: string,
|
|
onMessage?: MpvMessageParser,
|
|
onError?: MpvParseErrorHandler,
|
|
): MpvProtocolParseResult {
|
|
const lines = buffer.split('\n');
|
|
const nextBuffer = lines.pop() || '';
|
|
const messages: MpvMessage[] = [];
|
|
|
|
for (const line of lines) {
|
|
if (!line.trim()) continue;
|
|
try {
|
|
const message = JSON.parse(line) as MpvMessage;
|
|
messages.push(message);
|
|
if (onMessage) {
|
|
onMessage(message);
|
|
}
|
|
} catch (error) {
|
|
if (onError) {
|
|
onError(line, error);
|
|
}
|
|
}
|
|
}
|
|
|
|
return {
|
|
messages,
|
|
nextBuffer,
|
|
};
|
|
}
|
|
|
|
export async function dispatchMpvProtocolMessage(
|
|
msg: MpvMessage,
|
|
deps: MpvProtocolHandleMessageDeps,
|
|
): Promise<void> {
|
|
if (msg.event === 'property-change') {
|
|
if (msg.name === 'sub-text') {
|
|
const nextSubText = (msg.data as string) || '';
|
|
const overlayVisible = deps.isVisibleOverlayVisible();
|
|
deps.emitSubtitleChange({
|
|
text: nextSubText,
|
|
isOverlayVisible: overlayVisible,
|
|
});
|
|
deps.setCurrentSubText(nextSubText);
|
|
} else if (msg.name === 'sub-text-ass') {
|
|
deps.emitSubtitleAssChange({ text: (msg.data as string) || '' });
|
|
} else if (msg.name === 'sub-start') {
|
|
deps.setCurrentSubStart((msg.data as number) || 0);
|
|
deps.emitSubtitleTiming({
|
|
text: deps.getCurrentSubText(),
|
|
start: deps.getCurrentSubStart(),
|
|
end: deps.getCurrentSubEnd(),
|
|
});
|
|
} else if (msg.name === 'sub-end') {
|
|
const subEnd = (msg.data as number) || 0;
|
|
deps.setCurrentSubEnd(subEnd);
|
|
if (deps.getPendingPauseAtSubEnd() && subEnd > 0) {
|
|
deps.setPauseAtTime(subEnd);
|
|
deps.setPendingPauseAtSubEnd(false);
|
|
deps.sendCommand({ command: ['set_property', 'pause', false] });
|
|
}
|
|
deps.emitSubtitleTiming({
|
|
text: deps.getCurrentSubText(),
|
|
start: deps.getCurrentSubStart(),
|
|
end: deps.getCurrentSubEnd(),
|
|
});
|
|
} else if (msg.name === 'secondary-sub-text') {
|
|
const nextSubText = (msg.data as string) || '';
|
|
deps.setCurrentSecondarySubText(nextSubText);
|
|
deps.emitSecondarySubtitleChange({ text: nextSubText });
|
|
} else if (msg.name === 'sid') {
|
|
const sid =
|
|
typeof msg.data === 'number'
|
|
? msg.data
|
|
: typeof msg.data === 'string'
|
|
? Number(msg.data)
|
|
: null;
|
|
deps.emitSubtitleTrackChange({ sid: sid !== null && Number.isFinite(sid) ? sid : null });
|
|
} else if (msg.name === 'track-list') {
|
|
deps.emitSubtitleTrackListChange({
|
|
trackList: Array.isArray(msg.data) ? (msg.data as unknown[]) : null,
|
|
});
|
|
} else if (msg.name === 'aid') {
|
|
deps.setCurrentAudioTrackId(typeof msg.data === 'number' ? (msg.data as number) : null);
|
|
deps.syncCurrentAudioStreamIndex();
|
|
} else if (msg.name === 'time-pos') {
|
|
const timePos = (msg.data as number) || 0;
|
|
deps.setCurrentTimePos(timePos);
|
|
deps.emitTimePosChange({ time: timePos });
|
|
if (
|
|
deps.getPauseAtTime() !== null &&
|
|
deps.getCurrentTimePos() >= (deps.getPauseAtTime() as number)
|
|
) {
|
|
deps.setPauseAtTime(null);
|
|
deps.sendCommand({ command: ['set_property', 'pause', true] });
|
|
}
|
|
} else if (msg.name === 'duration') {
|
|
const duration = typeof msg.data === 'number' ? msg.data : 0;
|
|
if (duration > 0) {
|
|
deps.emitDurationChange({ duration });
|
|
}
|
|
} else if (msg.name === 'pause') {
|
|
deps.emitPauseChange({ paused: asBoolean(msg.data, false) });
|
|
} else if (msg.name === 'fullscreen') {
|
|
deps.emitFullscreenChange({ fullscreen: asBoolean(msg.data, false) });
|
|
} else if (msg.name === 'media-title') {
|
|
deps.emitMediaTitleChange({
|
|
title: typeof msg.data === 'string' ? msg.data.trim() : null,
|
|
});
|
|
} else if (msg.name === 'path') {
|
|
const path = (msg.data as string) || '';
|
|
deps.setCurrentVideoPath(path);
|
|
deps.emitMediaPathChange({ path });
|
|
deps.autoLoadSecondarySubTrack(path);
|
|
deps.syncCurrentAudioStreamIndex();
|
|
} else if (msg.name === 'sub-pos') {
|
|
deps.emitSubtitleMetricsChange({ subPos: msg.data as number });
|
|
} else if (msg.name === 'sub-font-size') {
|
|
deps.emitSubtitleMetricsChange({ subFontSize: msg.data as number });
|
|
} else if (msg.name === 'sub-scale') {
|
|
deps.emitSubtitleMetricsChange({ subScale: msg.data as number });
|
|
} else if (msg.name === 'sub-margin-y') {
|
|
deps.emitSubtitleMetricsChange({ subMarginY: msg.data as number });
|
|
} else if (msg.name === 'sub-margin-x') {
|
|
deps.emitSubtitleMetricsChange({ subMarginX: msg.data as number });
|
|
} else if (msg.name === 'sub-font') {
|
|
deps.emitSubtitleMetricsChange({ subFont: msg.data as string });
|
|
} else if (msg.name === 'sub-spacing') {
|
|
deps.emitSubtitleMetricsChange({ subSpacing: msg.data as number });
|
|
} else if (msg.name === 'sub-bold') {
|
|
deps.emitSubtitleMetricsChange({
|
|
subBold: asBoolean(msg.data, deps.getSubtitleMetrics().subBold),
|
|
});
|
|
} else if (msg.name === 'sub-italic') {
|
|
deps.emitSubtitleMetricsChange({
|
|
subItalic: asBoolean(msg.data, deps.getSubtitleMetrics().subItalic),
|
|
});
|
|
} else if (msg.name === 'sub-border-size') {
|
|
deps.emitSubtitleMetricsChange({ subBorderSize: msg.data as number });
|
|
} else if (msg.name === 'sub-shadow-offset') {
|
|
deps.emitSubtitleMetricsChange({ subShadowOffset: msg.data as number });
|
|
} else if (msg.name === 'sub-ass-override') {
|
|
deps.emitSubtitleMetricsChange({ subAssOverride: msg.data as string });
|
|
} else if (msg.name === 'sub-scale-by-window') {
|
|
deps.emitSubtitleMetricsChange({
|
|
subScaleByWindow: asBoolean(msg.data, deps.getSubtitleMetrics().subScaleByWindow),
|
|
});
|
|
} else if (msg.name === 'sub-visibility') {
|
|
if (deps.isVisibleOverlayVisible() && asBoolean(msg.data, false)) {
|
|
deps.sendCommand({ command: ['set_property', 'sub-visibility', false] });
|
|
}
|
|
} else if (msg.name === 'sub-use-margins') {
|
|
deps.emitSubtitleMetricsChange({
|
|
subUseMargins: asBoolean(msg.data, deps.getSubtitleMetrics().subUseMargins),
|
|
});
|
|
} else if (msg.name === 'osd-height') {
|
|
deps.emitSubtitleMetricsChange({ osdHeight: msg.data as number });
|
|
} else if (msg.name === 'osd-dimensions') {
|
|
const dims = msg.data as Record<string, unknown> | null;
|
|
if (!dims) {
|
|
deps.emitSubtitleMetricsChange({ osdDimensions: null });
|
|
} else {
|
|
deps.emitSubtitleMetricsChange({
|
|
osdDimensions: {
|
|
w: asFiniteNumber(dims.w, 0),
|
|
h: asFiniteNumber(dims.h, 0),
|
|
ml: asFiniteNumber(dims.ml, 0),
|
|
mr: asFiniteNumber(dims.mr, 0),
|
|
mt: asFiniteNumber(dims.mt, 0),
|
|
mb: asFiniteNumber(dims.mb, 0),
|
|
},
|
|
});
|
|
}
|
|
}
|
|
} else if (msg.event === 'shutdown') {
|
|
deps.restorePreviousSecondarySubVisibility();
|
|
if (deps.shouldQuitOnMpvShutdown()) {
|
|
deps.requestAppQuit();
|
|
}
|
|
} else if (msg.request_id) {
|
|
if (deps.resolvePendingRequest(msg.request_id, msg)) {
|
|
return;
|
|
}
|
|
|
|
if (msg.data === undefined) {
|
|
return;
|
|
}
|
|
|
|
if (msg.request_id === MPV_REQUEST_ID_TRACK_LIST_SECONDARY) {
|
|
const tracks = msg.data as Array<{
|
|
type: string;
|
|
lang?: string;
|
|
id: number;
|
|
}>;
|
|
if (Array.isArray(tracks)) {
|
|
const config = deps.getResolvedConfig();
|
|
const languages = config.secondarySub?.secondarySubLanguages || [];
|
|
const secondaryTrackId = pickSecondarySubtitleTrackId(tracks, languages);
|
|
if (secondaryTrackId !== null) {
|
|
deps.sendCommand({
|
|
command: ['set_property', 'secondary-sid', secondaryTrackId],
|
|
});
|
|
}
|
|
}
|
|
} else if (msg.request_id === MPV_REQUEST_ID_TRACK_LIST_AUDIO) {
|
|
deps.setCurrentAudioStreamIndex(
|
|
msg.data as Array<{
|
|
type?: string;
|
|
id?: number;
|
|
selected?: boolean;
|
|
'ff-index'?: number;
|
|
}>,
|
|
);
|
|
} else if (msg.request_id === MPV_REQUEST_ID_SUBTEXT) {
|
|
const nextSubText = (msg.data as string) || '';
|
|
deps.setCurrentSubText(nextSubText);
|
|
deps.emitSubtitleChange({
|
|
text: nextSubText,
|
|
isOverlayVisible: deps.isVisibleOverlayVisible(),
|
|
});
|
|
} else if (msg.request_id === MPV_REQUEST_ID_SUBTEXT_ASS) {
|
|
deps.emitSubtitleAssChange({ text: (msg.data as string) || '' });
|
|
} else if (msg.request_id === MPV_REQUEST_ID_PATH) {
|
|
deps.emitMediaPathChange({ path: (msg.data as string) || '' });
|
|
} else if (msg.request_id === MPV_REQUEST_ID_AID) {
|
|
deps.setCurrentAudioTrackId(typeof msg.data === 'number' ? (msg.data as number) : null);
|
|
deps.syncCurrentAudioStreamIndex();
|
|
} else if (msg.request_id === MPV_REQUEST_ID_SECONDARY_SUBTEXT) {
|
|
const nextSubText = (msg.data as string) || '';
|
|
deps.setCurrentSecondarySubText(nextSubText);
|
|
deps.emitSecondarySubtitleChange({ text: nextSubText });
|
|
} else if (msg.request_id === MPV_REQUEST_ID_SECONDARY_SUB_VISIBILITY) {
|
|
const previous = parseVisibilityProperty(msg.data);
|
|
if (previous !== null) {
|
|
deps.setPreviousSecondarySubVisibility(previous);
|
|
deps.emitSecondarySubtitleVisibility({ visible: previous });
|
|
}
|
|
deps.setSecondarySubVisibility(false);
|
|
} else if (msg.request_id === MPV_REQUEST_ID_SUB_POS) {
|
|
deps.emitSubtitleMetricsChange({ subPos: msg.data as number });
|
|
} else if (msg.request_id === MPV_REQUEST_ID_SUB_FONT_SIZE) {
|
|
deps.emitSubtitleMetricsChange({ subFontSize: msg.data as number });
|
|
} else if (msg.request_id === MPV_REQUEST_ID_SUB_SCALE) {
|
|
deps.emitSubtitleMetricsChange({ subScale: msg.data as number });
|
|
} else if (msg.request_id === MPV_REQUEST_ID_SUB_MARGIN_Y) {
|
|
deps.emitSubtitleMetricsChange({ subMarginY: msg.data as number });
|
|
} else if (msg.request_id === MPV_REQUEST_ID_SUB_MARGIN_X) {
|
|
deps.emitSubtitleMetricsChange({ subMarginX: msg.data as number });
|
|
} else if (msg.request_id === MPV_REQUEST_ID_SUB_FONT) {
|
|
deps.emitSubtitleMetricsChange({ subFont: msg.data as string });
|
|
} else if (msg.request_id === MPV_REQUEST_ID_SUB_SPACING) {
|
|
deps.emitSubtitleMetricsChange({ subSpacing: msg.data as number });
|
|
} else if (msg.request_id === MPV_REQUEST_ID_SUB_BOLD) {
|
|
deps.emitSubtitleMetricsChange({
|
|
subBold: asBoolean(msg.data, deps.getSubtitleMetrics().subBold),
|
|
});
|
|
} else if (msg.request_id === MPV_REQUEST_ID_SUB_ITALIC) {
|
|
deps.emitSubtitleMetricsChange({
|
|
subItalic: asBoolean(msg.data, deps.getSubtitleMetrics().subItalic),
|
|
});
|
|
} else if (msg.request_id === MPV_REQUEST_ID_SUB_BORDER_SIZE) {
|
|
deps.emitSubtitleMetricsChange({ subBorderSize: msg.data as number });
|
|
} else if (msg.request_id === MPV_REQUEST_ID_SUB_SHADOW_OFFSET) {
|
|
deps.emitSubtitleMetricsChange({ subShadowOffset: msg.data as number });
|
|
} else if (msg.request_id === MPV_REQUEST_ID_SUB_ASS_OVERRIDE) {
|
|
deps.emitSubtitleMetricsChange({ subAssOverride: msg.data as string });
|
|
} else if (msg.request_id === MPV_REQUEST_ID_SUB_SCALE_BY_WINDOW) {
|
|
deps.emitSubtitleMetricsChange({
|
|
subScaleByWindow: asBoolean(msg.data, deps.getSubtitleMetrics().subScaleByWindow),
|
|
});
|
|
} else if (msg.request_id === MPV_REQUEST_ID_SUB_USE_MARGINS) {
|
|
deps.emitSubtitleMetricsChange({
|
|
subUseMargins: asBoolean(msg.data, deps.getSubtitleMetrics().subUseMargins),
|
|
});
|
|
} else if (msg.request_id === MPV_REQUEST_ID_PAUSE) {
|
|
deps.emitPauseChange({ paused: asBoolean(msg.data, false) });
|
|
} else if (msg.request_id === MPV_REQUEST_ID_OSD_HEIGHT) {
|
|
deps.emitSubtitleMetricsChange({ osdHeight: msg.data as number });
|
|
} else if (msg.request_id === MPV_REQUEST_ID_OSD_DIMENSIONS) {
|
|
const dims = msg.data as Record<string, unknown> | null;
|
|
if (!dims) {
|
|
deps.emitSubtitleMetricsChange({ osdDimensions: null });
|
|
} else {
|
|
deps.emitSubtitleMetricsChange({
|
|
osdDimensions: {
|
|
w: asFiniteNumber(dims.w, 0),
|
|
h: asFiniteNumber(dims.h, 0),
|
|
ml: asFiniteNumber(dims.ml, 0),
|
|
mr: asFiniteNumber(dims.mr, 0),
|
|
mt: asFiniteNumber(dims.mt, 0),
|
|
mb: asFiniteNumber(dims.mb, 0),
|
|
},
|
|
});
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
export function asBoolean(value: unknown, fallback: boolean): boolean {
|
|
if (typeof value === 'boolean') return value;
|
|
if (typeof value === 'number') return value !== 0;
|
|
if (typeof value === 'string') {
|
|
const normalized = value.trim().toLowerCase();
|
|
if (['yes', 'true', '1'].includes(normalized)) return true;
|
|
if (['no', 'false', '0'].includes(normalized)) return false;
|
|
}
|
|
return fallback;
|
|
}
|
|
|
|
export function asFiniteNumber(value: unknown, fallback: number): number {
|
|
const nextValue = Number(value);
|
|
return Number.isFinite(nextValue) ? nextValue : fallback;
|
|
}
|
|
|
|
export function parseVisibilityProperty(value: unknown): boolean | null {
|
|
if (typeof value === 'boolean') return value;
|
|
if (typeof value !== 'string') return null;
|
|
|
|
const normalized = value.trim().toLowerCase();
|
|
if (normalized === 'yes' || normalized === 'true' || normalized === '1') {
|
|
return true;
|
|
}
|
|
if (normalized === 'no' || normalized === 'false' || normalized === '0') {
|
|
return false;
|
|
}
|
|
|
|
return null;
|
|
}
|