Kaleido-Desktop/src/player/store.ts
Elijah 624f01f745 Add a stop button to the player and resume after a server outage
The full player had no way to stop: Back only moved playback to the
miniplayer. A stop button now sits with the right-hand controls and
closes the player outright, as the miniplayer's close button does.

When the server went away mid-stream, mpv played out its buffer and then
took the cut-off stream for the end of the file: the title counted as
ended (opening Up Next for episodes), and Play restarted it from 0. A
stream ending more than 15 s before its duration is now treated as a
lost connection: the player shows "Lost connection to <server>", checks
/identity every few seconds, and reloads in place at the same position
(staying in the miniplayer) once the server answers; Play tries at once.
A second cut-off at the same spot counts as the real end of the file.

A file left paused at its end (keep-open) re-paused as soon as it was
unpaused, so the reload is unpaused again after loadfile.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-17 10:41:52 -07:00

1194 lines
45 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { create } from 'zustand';
import * as api from '../plex/api';
import { directUrl, previewUrl, reportTimeline, stopTranscode, transcodeUrl, type Delivery } from '../plex/playback';
import type { Item, Stream } from '../plex/types';
import type { PlexServer } from '../plex/client';
import { useSession } from '../app/session';
import { useDownloads, type Download } from '../downloads/store';
import { useSettings, SUBTITLE_MARGIN, type Preferences, type Quality, type Upscaling } from '../app/settings';
import { followVideoSize, noteMpvLog, requestEnhancement, watchPerformance } from './enhance';
import { channelLabel } from '../app/format';
import { invalidateLibrary } from '../app/queries';
import { invoke, keepAwake, isNative, win } from '../platform/native';
import { mpv, type MpvEvent, type MpvTrack } from './engine';
import { toast } from '../ui/overlays';
import { stopThemeMusic } from './theme';
import { bridgeMiniWindow, hideMiniWindow, separateMini, showMiniWindow } from './miniWindow';
export interface Track {
key: string; // stable UI key
mpvId: number | null; // null until loaded (external subs)
stream?: Stream; // Plex stream, when known
label: string;
detail: string;
language: string;
forced: boolean;
external: boolean;
}
export interface PlayOptions {
/** Start position in ms. Defaults to the saved resume point when `resume` is true. */
offsetMs?: number;
resume?: boolean;
mediaIndex?: number;
/** A local file path (offline download). */
localFile?: string;
quality?: Quality;
/** Keep these track choices (UI keys) instead of applying the preferences again. */
tracks?: { audioKey: string; subKey: string };
/** Reloading the same title in place (after an outage): stay in the miniplayer if it's there. */
keepMini?: boolean;
}
interface PlayerState {
open: boolean;
item: Item | null;
delivery: Delivery | null;
mediaIndex: number;
quality: Quality;
firstFrame: boolean;
error: string;
/** The server owner stopped this stream (error holds their message). */
terminated: boolean;
/** The stream was cut off before the end (the server went away); waiting for it to come back. */
reconnecting: boolean;
position: number;
duration: number;
cacheTime: number;
paused: boolean;
buffering: boolean;
seeking: boolean;
/** When the last seek was asked for (Date.now()), so a pending auto-skip can step aside. */
lastSeek: number;
volume: number;
muted: boolean;
speed: number;
subDelay: number;
audioTracks: Track[];
subTracks: Track[];
audioKey: string;
subKey: string; // '' when off
chapters: { title: string; time: number }[];
video: { width: number; height: number; hwdec: string; codec: string } | null;
ended: boolean;
nextUp: Item | null;
/** The episode before this one, for the previous-episode button. */
prevUp: Item | null;
episodeContextReady: boolean;
countdown: number | null;
countdownTotal: number;
postPlay: boolean;
postPlayDismissed: boolean;
fullscreen: boolean;
/** Playing in the corner while browsing. */
mini: boolean;
audioDelay: number;
/** The upscaling tier actually running (may be below the chosen one), and the before/after bypass. */
enhance: { active: Upscaling; bypass: boolean; preparing: boolean };
/** Transfer function of the decoded video ('pq', 'hlg', 'bt.1886'…), once known. */
videoGamma: string;
play(item: Item, opts?: PlayOptions): Promise<void>;
close(): Promise<void>;
togglePause(): void;
seek(seconds: number): void;
seekBy(delta: number): void;
setVolume(v: number): void;
toggleMute(): void;
setSpeed(s: number): void;
setSubDelay(s: number): void;
setAudioDelay(s: number): void;
minimize(): Promise<void>;
expand(): void;
selectAudio(key: string): Promise<void>;
selectSubtitle(key: string): Promise<void>;
setQuality(q: Quality): Promise<void>;
selectVersion(index: number): Promise<void>;
playNext(): Promise<void>;
playPrevious(): Promise<void>;
cancelCountdown(): void;
showPostPlay(): void;
dismissPostPlay(): void;
toggleFullscreen(): Promise<void>;
previewAt(seconds: number): string;
}
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 unwatchCountdown: (() => void) | null = null;
let scrobbled = false;
let unlisten: (() => void) | null = null;
let loadToken = 0;
let wasFullscreenBeforePlay = false;
let pendingExternal: Track | null = null;
/**
* The server this playback belongs to, fixed when it starts. Progress, watched state
* and transcodes always go there, never to whichever server is selected later. A
* download plays without one unless its own server is the one connected.
*/
let playServer: PlexServer | null = null;
/** The download being played, whose saved metadata keeps the offline resume point. */
let playDownload: Download | null = null;
const server = () => playServer;
/**
* mpv applies its volume on a cubic curve, so halfway on a linear slider was
* only an eighth of the amplitude. The slider follows a square law instead
* (halfway is about -12 dB, close to how loud it sounds).
*/
export const sliderToMpv = (v: number) => 100 * Math.pow(Math.max(0, v) / 100, 2 / 3);
const mpvToSlider = (v: number) => 100 * Math.pow(Math.max(0, v) / 100, 3 / 2);
function languageName(code: string, fallback: string) {
if (!code) return fallback;
try {
return new Intl.DisplayNames([navigator.language || 'en'], { type: 'language' }).of(code) || fallback;
} catch {
return fallback;
}
}
function audioTrackFromStream(s: Stream): Track {
const lang = s.language || languageName(s.languageCode, 'Unknown');
const parts = [s.codec.toUpperCase().replace('DCA', 'DTS'), channelLabel(s.channels) || s.audioChannelLayout].filter(Boolean);
const title = s.title && !s.title.toLowerCase().includes(lang.toLowerCase()) ? s.title : '';
return {
key: `p${s.id}`,
mpvId: null,
stream: s,
label: lang,
detail: title || parts.join(' '),
language: s.languageCode,
forced: false,
external: false,
};
}
function subTrackFromStream(s: Stream): Track {
const lang = s.language || languageName(s.languageCode, 'Unknown');
const flags = [s.forced ? 'Forced' : '', s.hearingImpaired ? 'SDH' : ''].filter(Boolean);
return {
key: `p${s.id}`,
mpvId: null,
stream: s,
label: [lang, ...flags].join(' · '),
detail: [s.title && s.title !== lang ? s.title : '', s.codec.toUpperCase(), s.key ? 'External' : ''].filter(Boolean).join(' · '),
language: s.languageCode,
forced: s.forced,
external: !!s.key,
};
}
function mpvTrackLabel(t: MpvTrack): Track {
const lang = t.lang ? languageName(t.lang, t.lang) : t.type === 'sub' ? 'Subtitles' : 'Audio';
return {
key: `m${t.id}`,
mpvId: t.id,
label: t.title ? `${lang} · ${t.title}` : lang,
detail: [t.codec?.toUpperCase(), t['demux-channel-count'] ? channelLabel(t['demux-channel-count']) : ''].filter(Boolean).join(' '),
language: t.lang ?? '',
forced: !!t.forced,
external: !!t.external,
};
}
/** Sets the first property name this libmpv knows (some were renamed in 0.38). */
async function setAny(names: string[], value: unknown) {
for (const name of names) {
try {
await mpv.set(name, value);
return true;
} catch {
/* try the older name */
}
}
return false;
}
/** mpv colours are #AARRGGBB. */
const mpvColor = (hex: string, alpha = 1) => `#${Math.round(alpha * 255).toString(16).padStart(2, '0')}${hex.replace('#', '')}`.toUpperCase();
/** Applies the text subtitle look from preferences (image subtitles keep their own). */
async function applySubtitleStyle(prefs: Preferences = useSettings.getState()) {
const edge = prefs.subtitleEdge;
await mpv.set('sub-scale', prefs.subtitleScale).catch(() => {});
await mpv.set('sub-color', mpvColor(prefs.subtitleColor)).catch(() => {});
await mpv.set('sub-bold', prefs.subtitleBold).catch(() => {});
await mpv.set('sub-margin-y', SUBTITLE_MARGIN[prefs.subtitlePosition]).catch(() => {});
await setAny(['sub-outline-color', 'sub-border-color'], '#FF000000');
await setAny(['sub-outline-size', 'sub-border-size'], edge === 'outline' ? 1.8 : edge === 'raised' ? 1.3 : 0);
await mpv.set('sub-shadow-offset', edge === 'shadow' ? 2.2 : edge === 'raised' ? 1.6 : 0).catch(() => {});
await mpv.set('sub-shadow-color', '#B4000000').catch(() => {});
if (prefs.subtitleBox === 'none') {
await mpv.set('sub-border-style', 'outline-and-shadow').catch(() => {});
await mpv.set('sub-back-color', '#B4000000').catch(() => {});
return;
}
const box = prefs.subtitleBox === 'solid' ? '#FF000000' : '#A0000000';
await mpv.set('sub-back-color', box).catch(() => {});
if (!(await setAny(['sub-border-style'], 'background-box'))) {
// Before background-box, an opaque box is drawn in the outline colour.
await mpv.set('sub-border-style', 'opaque-box').catch(() => {});
await setAny(['sub-outline-color', 'sub-border-color'], box);
await setAny(['sub-outline-size', 'sub-border-size'], 3);
}
}
/** A download's watch was counted this session; its resume point stays cleared. */
let localScrobbled = false;
/**
* Keeps a download's resume point (or, once watched, its watch count) in its saved
* metadata, which is all an offline title has to resume from.
*/
function saveLocalProgress(watched = false) {
const d = playDownload;
const { item, position, delivery, firstFrame } = usePlayer.getState();
// Before the first frame the position is only where it was asked to start.
if (!d || !item || delivery?.mode !== 'local' || !firstFrame || (localScrobbled && !watched)) return;
const raw = { ...(d.meta.raw ?? {}) };
if (watched) {
if (localScrobbled) return;
localScrobbled = true;
raw.viewOffset = 0;
raw.viewCount = (Number(raw.viewCount) || 0) + 1;
raw.lastViewedAt = Math.floor(Date.now() / 1000);
} else raw.viewOffset = Math.max(0, Math.round(position * 1000));
const meta = { ...d.meta, raw };
playDownload = { ...d, meta };
invoke('download_write_meta', { id: d.id, meta }).catch(() => {});
}
/** Reports progress to Plex; resolves true if the reply says the stream was stopped. */
function report(state: 'playing' | 'paused' | 'buffering' | 'stopped'): Promise<boolean> {
if (state === 'paused' || state === 'stopped') saveLocalProgress();
const s = server();
const { item, position, delivery } = usePlayer.getState();
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;
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() {
const { item, position, duration } = usePlayer.getState();
const s = server();
if (!item || scrobbled || !duration || item.type === 'clip') return;
const percent = useSettings.getState().watchedPercent;
if (position / duration >= percent / 100) {
saveLocalProgress(true);
if (!s) return;
scrobbled = true;
api
.setWatched(s, item.id, true)
.then(() => invalidateLibrary())
.catch(() => (scrobbled = false));
}
}
/** The episodes either side of this one in the show. */
async function findNeighbours(item: Item): Promise<{ prevUp: Item | null; nextUp: Item | null }> {
const none = { prevUp: null, nextUp: null };
const s = server();
if (!s || item.type !== 'episode' || !item.grandparentId) return none;
try {
const leaves = await api.allLeaves(s, item.grandparentId);
const i = leaves.findIndex((e) => e.id === item.id);
return i >= 0 ? { prevUp: leaves[i - 1] ?? null, nextUp: leaves[i + 1] ?? null } : none;
} catch {
return none;
}
}
function openPostPlay() {
const st = usePlayer.getState();
if (!st.open || st.error || st.item?.type !== 'episode' || st.postPlay || st.postPlayDismissed) return;
usePlayer.setState({ postPlay: true });
startCountdown();
}
function startCountdown() {
const st = usePlayer.getState();
if (!st.open || !st.postPlay || st.error || !st.nextUp || st.postPlayDismissed || st.countdown !== null) return;
const settings = useSettings.getState();
const total = settings.autoplayNext ? settings.countdownSeconds : 0;
if (total <= 0) return;
stopCountdown();
usePlayer.setState({ countdown: total, countdownTotal: total });
let lastTick = performance.now();
const visibilityChanged = () => { lastTick = performance.now(); };
document.addEventListener('visibilitychange', visibilityChanged);
unwatchCountdown = () => document.removeEventListener('visibilitychange', visibilityChanged);
countdownTimer = setInterval(() => {
const now = performance.now();
const elapsed = Math.max(0, (now - lastTick) / 1000);
lastTick = now;
const current = usePlayer.getState();
if (!current.open || !current.postPlay || current.error || current.countdown === null) return stopCountdown();
// Native video can own keyboard focus while the webview is still on screen.
if (document.hidden || current.mini) return;
const remaining = Math.max(0, current.countdown - elapsed);
usePlayer.setState({ countdown: remaining });
if (remaining === 0) {
stopCountdown();
current.playNext();
}
}, 50);
}
function stopCountdown() {
if (countdownTimer) clearInterval(countdownTimer);
countdownTimer = null;
unwatchCountdown?.();
unwatchCountdown = null;
}
/**
* The tracks to play: a choice already made is kept ('' audio and '__init' subtitles
* mean none yet), otherwise the preferred audio language, then Plex's selection, then
* the file default; subtitles follow the subtitle mode.
*/
function preferredTracks(audio: Track[], subs: Track[], prefs: Preferences, audioKey = '', subKey = '__init') {
if (!audioKey) {
const lang = prefs.audioLanguage;
audioKey =
(lang && audio.find((t) => t.language === lang)?.key) ||
audio.find((t) => t.stream?.selected)?.key ||
audio.find((t) => t.stream?.default)?.key ||
audio[0]?.key ||
'';
}
if (subKey === '__init') {
const lang = prefs.subtitleLanguage || audio.find((t) => t.key === audioKey)?.language || '';
const inLang = subs.filter((t) => !lang || t.language === lang);
switch (prefs.subtitleMode) {
case 'off':
subKey = '';
break;
case 'always':
subKey = (inLang.find((t) => !t.forced) ?? inLang[0] ?? subs[0])?.key ?? '';
break;
case 'forced':
subKey = inLang.find((t) => t.forced)?.key ?? '';
break;
default:
subKey = subs.find((t) => t.stream?.selected)?.key ?? inLang.find((t) => t.forced)?.key ?? '';
}
}
return { audioKey, subKey };
}
type Selection = { audioStreamID?: string; subtitleStreamID?: string };
/** The item with Plex's stream selection updated locally to match `sel`. */
function withSelection(item: Item, mediaIndex: number, sel: Selection): Item {
const media = item.media.map((m, i) =>
i !== mediaIndex
? m
: {
...m,
parts: m.parts.map((p) => ({
...p,
streams: p.streams.map((x) =>
x.streamType === 2 && sel.audioStreamID !== undefined
? { ...x, selected: x.id === sel.audioStreamID }
: x.streamType === 3 && sel.subtitleStreamID !== undefined
? { ...x, selected: x.id === sel.subtitleStreamID }
: x,
),
})),
},
);
return { ...item, media };
}
const streamId = (tracks: Track[], key: string) => tracks.find((t) => t.key === key)?.stream?.id;
/**
* What Plex must be told so its selection on this media matches the given streams
* (subtitle '' is none, undefined leaves either as it is). Empty if it already does.
*/
function selectionFor(item: Item, mediaIndex: number, audioId: string | undefined, subId: string | undefined): Selection {
const streams = item.media[mediaIndex]?.parts.flatMap((p) => p.streams) ?? [];
const sel: Selection = {};
if (audioId && !streams.some((x) => x.streamType === 2 && x.selected && x.id === audioId)) sel.audioStreamID = audioId;
if (subId !== undefined && (streams.find((x) => x.streamType === 3 && x.selected)?.id ?? '') !== subId) sel.subtitleStreamID = subId || '0';
return sel;
}
/** Saves a track choice on the server; resolves to the item with that selection. */
async function saveSelection(s: PlexServer, item: Item, mediaIndex: number, sel: Selection): Promise<Item> {
const part = item.media[mediaIndex]?.parts[0];
if (!part || (sel.audioStreamID === undefined && sel.subtitleStreamID === undefined)) return item;
await api.selectStreams(s, part.id, sel);
return withSelection(item, mediaIndex, sel);
}
/** Saves a choice made during direct play, so other Plex apps (and a later conversion) use it too. */
function rememberSelection(st: PlayerState, audioId: string | undefined, subId: string | undefined) {
const s = server();
const { item, mediaIndex } = st;
if (!s || !item) return;
const sel = selectionFor(item, mediaIndex, audioId, subId);
saveSelection(s, item, mediaIndex, sel)
.then(() => {
const now = usePlayer.getState();
if (now.item?.id === item.id && now.mediaIndex === mediaIndex) usePlayer.setState({ item: withSelection(now.item, mediaIndex, sel) });
})
.catch(() => {});
}
/** Applies Plex/user track preferences once mpv has loaded the file. */
async function applyInitialTracks(tracks: MpvTrack[]) {
const st = usePlayer.getState();
const prefs = useSettings.getState();
const direct = st.delivery?.mode !== 'transcode';
const byFF = (type: string, idx: number) => tracks.find((t) => t.type === type && t['ff-index'] === idx && !t.external);
let audio = st.audioTracks;
let subs = st.subTracks;
if (direct) {
audio = audio.map((t) => ({ ...t, mpvId: t.stream ? (byFF('audio', t.stream.index)?.id ?? null) : t.mpvId }));
subs = subs.map((t) => ({ ...t, mpvId: t.stream && !t.external ? (byFF('sub', t.stream.index)?.id ?? null) : t.mpvId }));
// Files without Plex stream info (offline) use mpv's own list.
if (!audio.length) audio = tracks.filter((t) => t.type === 'audio').map(mpvTrackLabel);
if (!subs.length) subs = tracks.filter((t) => t.type === 'sub').map(mpvTrackLabel);
}
const { audioKey, subKey } = preferredTracks(audio, subs, prefs, st.audioKey, st.subKey);
usePlayer.setState({ audioTracks: audio, subTracks: subs, audioKey, subKey });
if (direct) {
const a = audio.find((t) => t.key === audioKey);
if (a?.mpvId) mpv.set('aid', a.mpvId).catch(() => {});
await setMpvSubtitle(subKey);
}
}
async function setMpvSubtitle(key: string) {
const st = usePlayer.getState();
const t = st.subTracks.find((x) => x.key === key);
if (!t) {
await mpv.set('sid', 'no').catch(() => {});
return;
}
if (t.mpvId) {
await mpv.set('sid', t.mpvId).catch(() => {});
} else if (t.external && t.stream?.key && server()) {
pendingExternal = t;
const url = server()!.signed(t.stream.key);
await mpv.command('sub-add', url, 'select', t.label, t.language || 'und').catch(() => toast('This subtitle could not be loaded.', 'error'));
}
}
function onEvent(e: MpvEvent) {
const st = usePlayer.getState();
if (!st.open) return;
switch (e.type) {
case 'property': {
const v = e.value;
switch (e.name) {
case 'time-pos':
if (typeof v === 'number') {
usePlayer.setState({ position: v });
checkScrobble();
if (Date.now() - lastReport > 10000) report(st.paused ? 'paused' : 'playing');
maybeCredits(v);
}
break;
case 'duration':
if (typeof v === 'number' && v > 0) usePlayer.setState({ duration: v });
break;
case 'pause':
usePlayer.setState({ paused: !!v });
report(v ? 'paused' : 'playing');
keepAwake(!v);
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 });
break;
case 'demuxer-cache-time':
if (typeof v === 'number') usePlayer.setState({ cacheTime: v });
break;
case 'volume':
if (typeof v === 'number') usePlayer.setState({ volume: Math.round(mpvToSlider(v)) });
break;
case 'mute':
usePlayer.setState({ muted: !!v });
break;
case 'speed':
if (typeof v === 'number') usePlayer.setState({ speed: v });
break;
case 'sub-delay':
if (typeof v === 'number') usePlayer.setState({ subDelay: v });
break;
case 'audio-delay':
if (typeof v === 'number') usePlayer.setState({ audioDelay: v });
break;
case 'chapter-list':
if (Array.isArray(v) && v.length > 1 && !st.chapters.length)
usePlayer.setState({ chapters: v.map((c: any, i: number) => ({ title: c.title && !/^\d\d:\d\d/.test(c.title) ? c.title : `Chapter ${i + 1}`, time: c.time })) });
break;
case 'video-params':
if (v?.w) usePlayer.setState({ video: { ...(st.video ?? { hwdec: '', codec: '' }), width: v.w, height: v.h } });
// Enhancement follows the decoded video's transfer (HDR skips SDR-tuned stages).
if (v?.gamma && v.gamma !== st.videoGamma) {
usePlayer.setState({ videoGamma: v.gamma });
requestEnhancement();
}
break;
case 'hwdec-current':
usePlayer.setState({ video: { ...(st.video ?? { width: 0, height: 0, codec: '' }), hwdec: v ?? '' } });
break;
case 'track-list':
if (Array.isArray(v)) {
if (pendingExternal) {
const ext = (v as MpvTrack[]).filter((t) => t.type === 'sub' && t.external).pop();
if (ext) {
const key = pendingExternal.key;
pendingExternal = null;
usePlayer.setState({ subTracks: usePlayer.getState().subTracks.map((t) => (t.key === key ? { ...t, mpvId: ext.id } : t)) });
}
}
const video = (v as MpvTrack[]).find((t) => t.type === 'video' && t.selected);
if (video) usePlayer.setState({ video: { ...(usePlayer.getState().video ?? { width: 0, height: 0, hwdec: '' }), codec: video.codec ?? '' } });
}
break;
case 'eof-reached':
if (v === true) onStreamEnd();
break;
}
break;
}
case 'file-loaded':
mpv
.get<MpvTrack[]>('track-list')
.then(applyInitialTracks)
.catch(() => {});
break;
case 'playback-restart':
if (!st.firstFrame) usePlayer.setState({ firstFrame: true });
usePlayer.setState({ buffering: false });
break;
case 'end-file':
if (e.reason === 'error') {
// 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) => {
const now = usePlayer.getState();
if (terminated || !now.open || session !== failed) return;
// Partway through a stream that was playing, it's the connection that failed.
if (cutShort(now)) return onStreamEnd();
usePlayer.setState({ error: e.error ? `Playback failed: ${e.error}` : 'This video could not be played.' });
fallbackAfterError();
});
} else if (e.reason === 'eof') onStreamEnd();
break;
case 'log':
noteMpvLog(e.level, e.prefix, e.text);
if (e.level === 'error' && /http|stream|open/i.test(e.prefix + e.text) && !st.firstFrame) console.warn('[mpv]', e.prefix, e.text);
break;
}
}
/** The tracks playing now, to carry over when the same media restarts (a choice not made yet stays open). */
const currentTracks = (st: PlayerState) => ({ audioKey: st.audioKey, subKey: st.subKey });
/** Direct play failed: retry once through Plex's converter. */
let triedFallback = false;
function fallbackAfterError() {
const st = usePlayer.getState();
if (triedFallback || !st.item || st.delivery?.mode !== 'direct') return;
triedFallback = true;
toast('Direct playback failed. Asking Plex to convert this video.');
usePlayer.setState({ error: '' });
st.play(st.item, { offsetMs: st.position * 1000 || startOffsetMs, mediaIndex: st.mediaIndex, quality: '1080', tracks: currentTracks(st) });
}
function maybeCredits(pos: number) {
const st = usePlayer.getState();
if (st.item?.type !== 'episode' || !st.firstFrame) return;
const inCredits = st.item.markers.some((m) => m.type === 'credits' && pos * 1000 >= m.start && pos * 1000 < m.end);
// Leaving the credits (seeking back into the episode) re-arms Up Next for the next time they start.
if (!inCredits) {
if (st.postPlayDismissed && !st.postPlay && !st.ended) usePlayer.setState({ postPlayDismissed: false });
} else if (!st.postPlayDismissed) openPostPlay();
}
/**
* A streamed title stopping well short of its end means the connection went
* (mpv plays out its buffer, then sees the cut-off stream as the end of the file).
*/
const CUT_SHORT_MARGIN = 15;
let reconnectTimer: ReturnType<typeof setTimeout> | null = null;
/** Where the last cut-off happened, so a file that really does end early isn't retried forever. */
let lastCutAt: number | null = null;
function cutShort(st: PlayerState) {
return st.delivery?.mode !== 'local' && st.firstFrame && st.duration > 0 && st.position < st.duration - CUT_SHORT_MARGIN;
}
/** The stream ran out: a real ending, or a lost connection to wait out. */
function onStreamEnd() {
const st = usePlayer.getState();
if (!cutShort(st)) return onEnded();
if (st.reconnecting || st.ended) return;
// Cut off at the same place again once the server answered: the file itself ends there.
if (lastCutAt !== null && Math.abs(st.position - lastCutAt) < 5) return onEnded();
lastCutAt = st.position;
usePlayer.setState({ reconnecting: true, buffering: false });
keepAwake(false);
scheduleReconnect(2000);
}
function stopReconnecting() {
if (reconnectTimer) clearTimeout(reconnectTimer);
reconnectTimer = null;
}
function scheduleReconnect(delay: number) {
stopReconnecting();
reconnectTimer = setTimeout(() => void tryReconnect(), delay);
}
/** Resumes where the stream was cut off if the server answers; otherwise checks again shortly. */
async function tryReconnect(): Promise<boolean> {
stopReconnecting();
const st = usePlayer.getState();
const s = server();
if (!st.open || !st.reconnecting || !st.item || !s) return false;
const reachable = await s.get('/identity', undefined, { timeout: 4000 }).then(
() => true,
() => false,
);
const now = usePlayer.getState();
if (!now.open || !now.reconnecting || now.item?.id !== st.item.id) return false;
if (!reachable) {
scheduleReconnect(3000);
return false;
}
usePlayer.setState({ reconnecting: false });
await now.play(now.item, { offsetMs: now.position * 1000, mediaIndex: now.mediaIndex, quality: now.quality, tracks: currentTracks(now), keepMini: true });
return true;
}
function onEnded() {
const st = usePlayer.getState();
if (st.ended) return;
usePlayer.setState({ ended: true });
report('stopped');
checkScrobble();
// Reaching the end always offers Up Next, as on Android, even after watching the credits.
if (st.item?.type === 'episode' && !st.postPlay) {
usePlayer.setState({ postPlayDismissed: false });
openPostPlay();
}
}
export const usePlayer = create<PlayerState>((set, get) => ({
open: false,
item: null,
delivery: null,
mediaIndex: 0,
quality: 'original',
firstFrame: false,
error: '',
terminated: false,
reconnecting: false,
position: 0,
duration: 0,
cacheTime: 0,
paused: false,
buffering: false,
seeking: false,
lastSeek: 0,
volume: 100,
muted: false,
speed: 1,
subDelay: 0,
audioTracks: [],
subTracks: [],
audioKey: '',
subKey: '',
chapters: [],
video: null,
ended: false,
nextUp: null,
prevUp: null,
episodeContextReady: false,
countdown: null,
countdownTotal: 0,
postPlay: false,
postPlayDismissed: false,
fullscreen: false,
mini: false,
audioDelay: 0,
enhance: { active: 'off', bypass: false, preparing: false },
videoGamma: '',
async play(input, opts = {}) {
const token = ++loadToken;
// Closing or starting something else while this one is still setting up wins.
const stale = () => token !== loadToken;
const prefs = useSettings.getState();
stopThemeMusic();
stopCountdown();
if (!isNative) {
toast('Video playback is available in the Kaleido desktop app.', 'error');
return;
}
const previous = get();
stopReconnecting();
if (previous.item?.id !== input.id) lastCutAt = null;
// The previous title's reporting ends here, against its own server.
if (reportTimer) clearInterval(reportTimer);
reportTimer = null;
if (previous.open && previous.item) {
report('stopped');
const ps = server();
if (previous.delivery?.session && ps) stopTranscode(ps, previous.delivery.session);
}
session = '';
const current = useSession.getState().server;
const download = opts.localFile ? (useDownloads.getState().items.find((d) => d.filePath === opts.localFile) ?? null) : null;
const s = opts.localFile ? (current && download && current.machineId === download.meta.serverId ? current : null) : current;
playServer = s;
playDownload = download;
localScrobbled = false;
// From the miniplayer the window may have gone full screen (or left it) since.
const fullscreenNow = await win.isFullscreen().catch(() => false);
if (stale()) return;
if (!previous.open) {
triedFallback = false;
wasFullscreenBeforePlay = fullscreenNow;
}
set({
open: true,
item: input,
firstFrame: false,
error: '',
terminated: false,
reconnecting: false,
position: (opts.offsetMs ?? 0) / 1000,
lastSeek: 0,
duration: input.duration / 1000,
cacheTime: 0,
paused: false,
buffering: true,
ended: false,
nextUp: null,
prevUp: null,
episodeContextReady: false,
countdown: null,
countdownTotal: 0,
postPlay: false,
postPlayDismissed: false,
chapters: [],
audioTracks: [],
subTracks: [],
audioKey: opts.tracks?.audioKey ?? '',
subKey: opts.tracks?.subKey ?? '__init',
video: null,
fullscreen: fullscreenNow,
mini: opts.keepMini ? previous.open && previous.mini : false,
videoGamma: '',
enhance: { ...previous.enhance, bypass: false, preparing: false },
});
try {
// Full metadata: streams, markers and chapters.
let item = input;
if (!opts.localFile && s && (!input.media.length || !input.media[0].parts[0]?.streams.length || !input.raw?.Marker)) {
item = await api.metadata(s, input.id).catch(() => input);
}
if (stale()) return;
const mediaIndex = Math.min(opts.mediaIndex ?? 0, Math.max(0, item.media.length - 1));
let media = item.media[mediaIndex];
let offsetMs = opts.offsetMs ?? (opts.resume !== false && item.viewOffset && item.viewOffset < item.duration * 0.97 ? item.viewOffset : 0);
const local = useSession.getState().connection?.local ?? true;
const quality: Quality = opts.quality ?? (local ? prefs.quality : prefs.remoteQuality);
const streams = media?.parts.flatMap((p) => p.streams) ?? [];
const audioTracks = streams.filter((x) => x.streamType === 2).map(audioTrackFromStream);
const subTracks = streams.filter((x) => x.streamType === 3).map(subTrackFromStream);
let tracks: { audioKey: string; subKey: string } | null = null;
let delivery: Delivery;
if (opts.localFile) {
delivery = { url: opts.localFile, mode: 'local', label: 'Downloaded', absoluteTime: false };
} else if (!s || !media) {
throw new Error('This title has no playable media.');
} else if (quality === 'original') {
delivery = { url: directUrl(s, media), mode: 'direct', label: 'Original', absoluteTime: false };
} else {
// Plex converts (and burns in) the tracks selected on the server, so the chosen
// ones are saved there before the conversion starts, not after.
tracks = preferredTracks(audioTracks, subTracks, prefs, opts.tracks?.audioKey, opts.tracks?.subKey);
const audioStreamID = streamId(audioTracks, tracks.audioKey);
const subtitleStreamID = streamId(subTracks, tracks.subKey) ?? '';
const base = item;
item = await saveSelection(s, base, mediaIndex, selectionFor(base, mediaIndex, audioStreamID, subtitleStreamID)).catch(() => base);
if (stale()) return;
media = item.media[mediaIndex];
const tsession = crypto.randomUUID();
delivery = {
url: transcodeUrl(s, item, { mediaIndex, quality, offsetMs, session: tsession, local, audioStreamID, subtitleStreamID: subtitleStreamID || undefined }),
mode: 'transcode',
session: tsession,
label: quality === '2160' ? '4K' : `${quality}p`,
absoluteTime: true,
};
}
startOffsetMs = offsetMs;
session = crypto.randomUUID();
sessionAnswered = false;
scrobbled = false;
set({
item,
delivery,
mediaIndex,
quality,
duration: (item.duration || media?.duration || 0) / 1000,
audioTracks,
subTracks,
...(tracks ?? {}),
});
findNeighbours(item).then((n) => {
if (token !== loadToken) return;
set({ ...n, episodeContextReady: true });
const current = get();
if (current.postPlay) startCountdown();
else if (current.ended) openPostPlay();
else maybeCredits(current.position);
});
await mpv.init({ hwdec: prefs.hwdec, hdr: prefs.hdrPassthrough });
if (!unlisten) unlisten = await mpv.on(onEvent);
if (stale()) 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);
if (stale()) return;
followVideoSize();
await requestEnhancement();
if (stale()) return;
watchPerformance(true);
await mpv.set('force-media-title', item.type === 'episode' ? `${item.grandparentTitle} — ${item.title}` : item.title);
if (stale()) return;
await mpv.show(true);
if (stale()) return;
const fileOpts = [
delivery.mode === 'transcode' ? 'rebase-start-time=no' : 'rebase-start-time=yes',
offsetMs > 0 && delivery.mode !== 'transcode' ? `start=${(offsetMs / 1000).toFixed(2)}` : 'start=none',
].join(',');
await mpv.command('loadfile', delivery.url, 'replace', -1, fileOpts);
// A file left paused at its end (keep-open) re-pauses as soon as it's unpaused
// above, so a reload in place (after an outage) would open paused: unpause again.
await mpv.set('pause', false);
if (stale()) return;
keepAwake(true);
report('buffering');
reportTimer && clearInterval(reportTimer);
reportTimer = setInterval(() => {
const st = get();
if (st.open && !st.ended) report(st.paused ? 'paused' : st.buffering ? 'buffering' : 'playing');
}, 10000);
} catch (e: any) {
if (token !== loadToken) return;
set({ error: e?.message ?? String(e), buffering: false });
}
},
async close() {
loadToken++;
const st = get();
stopCountdown();
stopReconnecting();
lastCutAt = null;
if (reportTimer) clearInterval(reportTimer);
reportTimer = null;
if (st.item && !st.error) report('stopped');
const s = server();
if (st.delivery?.session && s) stopTranscode(s, st.delivery.session);
// Nothing more is reported for this playback, whatever mpv says while it stops.
session = '';
playServer = null;
playDownload = null;
keepAwake(false);
watchPerformance(false);
if (isNative) {
// From the miniplayer window, the video goes home first (and stays hidden).
if (separateMini && st.mini) await hideMiniWindow();
await mpv.command('stop').catch(() => {});
await mpv.show(false).catch(() => {});
}
set({ open: false, mini: false, postPlay: false, item: null, delivery: null, error: '', terminated: false, reconnecting: false, countdown: null, nextUp: null, prevUp: null, firstFrame: false });
// Closing the full player hands back the window it found; closing the
// miniplayer leaves browsing as it is, full screen or not.
if (!st.mini) {
const fs = await win.isFullscreen().catch(() => false);
if (fs && !wasFullscreenBeforePlay) win.setFullscreen(false);
}
if (st.item) invalidateLibrary();
},
togglePause() {
const st = get();
// Play while the server is away: try it now rather than at the next check.
if (st.reconnecting) {
tryReconnect().then((resumed) => {
if (!resumed && get().reconnecting) toast(`Still can’t reach ${server()?.name ?? 'the server'}.`);
});
return;
}
if (st.ended) {
st.seek(0);
set({ ended: false });
}
mpv.command('cycle', 'pause').catch(() => {});
},
seek(seconds) {
const st = get();
const target = Math.max(0, Math.min(seconds, (st.duration || seconds) - 0.5));
set({ position: target, ended: false, lastSeek: Date.now() });
if (st.postPlay) st.dismissPostPlay();
mpv.command('seek', target.toFixed(3), 'absolute+exact').catch(() => {});
},
seekBy(delta) {
const st = get();
st.seek(st.position + delta);
},
setVolume(v) {
const volume = Math.max(0, Math.min(150, Math.round(v)));
set({ volume });
useSettings.getState().set('volume', Math.min(100, volume));
mpv.set('volume', sliderToMpv(volume)).catch(() => {});
if (get().muted && volume > 0) mpv.set('mute', false).catch(() => {});
},
toggleMute() {
mpv.command('cycle', 'mute').catch(() => {});
},
setSpeed(speed) {
mpv.set('speed', speed).catch(() => {});
},
setSubDelay(delay) {
mpv.set('sub-delay', delay).catch(() => {});
},
setAudioDelay(delay) {
mpv.set('audio-delay', delay).catch(() => {});
},
async minimize() {
const st = get();
if (!st.open || st.mini) return;
if (st.postPlay) st.dismissPostPlay();
// Full screen stays for browsing; Esc or the window controls leave it.
set({ mini: true });
if (separateMini) {
// Once the page is opaque again (no video in it), the video moves into
// the miniplayer's own window (miniWindow.ts).
await new Promise((r) => requestAnimationFrame(() => requestAnimationFrame(r)));
if (get().open && get().mini) await showMiniWindow().catch((e) => console.warn('[mini]', e));
}
},
expand() {
if (!get().mini) return;
// The video comes back behind the still-opaque page before it turns transparent.
if (separateMini) hideMiniWindow().finally(() => set({ mini: false }));
else set({ mini: false });
},
async selectAudio(key) {
const st = get();
const t = st.audioTracks.find((x) => x.key === key);
if (!t) return;
set({ audioKey: key });
if (st.delivery?.mode === 'transcode' && st.item) {
// The restart saves the choice on the server before converting with it.
await st.play(st.item, { offsetMs: st.position * 1000, mediaIndex: st.mediaIndex, quality: st.quality, tracks: { ...currentTracks(st), audioKey: key } });
return;
}
if (t.mpvId) await mpv.set('aid', t.mpvId).catch(() => {});
rememberSelection(st, t.stream?.id, undefined);
},
async selectSubtitle(key) {
const st = get();
set({ subKey: key });
if (st.delivery?.mode === 'transcode' && st.item) {
await st.play(st.item, { offsetMs: st.position * 1000, mediaIndex: st.mediaIndex, quality: st.quality, tracks: { ...currentTracks(st), subKey: key } });
return;
}
await setMpvSubtitle(key);
rememberSelection(st, undefined, streamId(st.subTracks, key) ?? '');
},
async setQuality(q) {
const st = get();
if (!st.item || q === st.quality) return;
await st.play(st.item, { offsetMs: st.position * 1000, mediaIndex: st.mediaIndex, quality: q, tracks: currentTracks(st) });
},
async selectVersion(index) {
const st = get();
if (!st.item || index === st.mediaIndex) return;
await st.play(st.item, { offsetMs: st.position * 1000, mediaIndex: index, quality: st.quality });
},
async playNext() {
const st = get();
stopCountdown();
if (!st.nextUp) return;
// Watching on through a season starts each episode from the top, never at a
// stale position left by an earlier, unfinished viewing.
await st.play(st.nextUp, { resume: false, quality: st.quality });
},
async playPrevious() {
const st = get();
if (!st.prevUp) return;
await st.play(st.prevUp, { resume: false, quality: st.quality });
},
cancelCountdown() {
stopCountdown();
set({ countdown: null, postPlayDismissed: true });
},
showPostPlay() {
// A deliberate Skip credits can reopen a screen previously dismissed.
set({ postPlayDismissed: false });
openPostPlay();
},
dismissPostPlay() {
stopCountdown();
set({ countdown: null, postPlay: false, postPlayDismissed: true });
},
async toggleFullscreen() {
const fs = await win.isFullscreen().catch(() => false);
await win.setFullscreen(!fs);
set({ fullscreen: !fs });
},
previewAt(seconds) {
const st = get();
const s = server();
if (!s || !st.item || st.delivery?.mode === 'local') return '';
return previewUrl(s, st.item.media[st.mediaIndex], seconds * 1000);
},
}));
/**
* Shown once the first frame is ready, the page turns transparent over the
* video. In the miniplayer the interface stays, with a hole for the video.
*/
usePlayer.subscribe((st, prev) => {
if (st.firstFrame !== prev.firstFrame || st.open !== prev.open || st.error !== prev.error || st.mini !== prev.mini) {
const html = document.documentElement.classList;
// With a miniplayer window, the video has left this page entirely.
html.toggle('video-active', st.open && st.firstFrame && !st.error && !(st.mini && separateMini));
html.toggle('player-open', st.open && !st.mini);
html.toggle('player-mini', st.open && st.mini);
}
// However the miniplayer is left (expanding, playing another title, closing),
// the video returns to the whole window.
if (prev.mini && !st.mini && isNative) {
if (separateMini) hideMiniWindow();
else mpv.rect(null).catch(() => {});
}
});
bridgeMiniWindow(usePlayer);
// Full screen can change outside the player (Esc or the window controls while
// browsing with the miniplayer, the OS): the player's controls follow it.
win.onResized(() => {
if (!usePlayer.getState().open) return;
win.isFullscreen().then((fs) => usePlayer.getState().fullscreen !== fs && usePlayer.setState({ fullscreen: fs }), () => {});
});
/**
* Signing out, or switching server or profile, ends a stream from the old one: its
* progress belongs to that account. A download keeps playing, without reporting.
* A reconnect to the same server with the same credentials carries on.
*/
useSession.subscribe((st, prev) => {
const old = prev.server;
if (!old || st.server === old) return;
const same = !!st.server && st.server.machineId === old.machineId && st.server.token === old.token;
if (playServer !== old) return;
if (same) {
playServer = st.server;
return;
}
if (usePlayer.getState().open && !playDownload) usePlayer.getState().close();
playServer = null;
});
/** Subtitle look changes apply to the playing video straight away. */
const SUBTITLE_PREFS = ['subtitleScale', 'subtitleColor', 'subtitleEdge', 'subtitleBox', 'subtitleBold', 'subtitlePosition'] as const;
useSettings.subscribe((prefs, prev) => {
if (isNative && usePlayer.getState().open && SUBTITLE_PREFS.some((k) => prefs[k] !== prev[k])) applySubtitleStyle(prefs);
});