Repository Public
1import { apiBase } from "./config";23// Client of the MediaMTX control API (loopback only).4export interface MtxPath { name: string; ready: boolean; tracks: string[]; bytesReceived: number; bytesSent: number; readers: { type: string; id: string }[]; source: { type: string; id: string } | null }5export interface MtxHlsSession { id: string; path: string; created: string; outboundBytes: number }67async function call<T>(path: string, init?: RequestInit): Promise<T> {8 const res = await fetch(apiBase() + path, { ...init, signal: AbortSignal.timeout(4000) });9 if (!res.ok) throw new Error(`MediaMTX ${res.status}`);10 const text = await res.text(); return (text ? JSON.parse(text) : {}) as T;11}12async function pages<T>(path: string): Promise<T[]> {13 const out: T[] = [];14 for (let page = 0; page < 20; page++) {15 const r = await call<{ pageCount: number; items: T[] }>(`${path}?itemsPerPage=100&page=${page}`);16 out.push(...(r.items ?? [])); if (page + 1 >= (r.pageCount ?? 1)) break;17 }18 return out;19}20export const paths = () => pages<MtxPath>("/v3/paths/list");21export const hlsSessions = () => pages<MtxHlsSession>("/v3/hlssessions/list");22export const info = () => call<{ version: string; started: string }>("/v3/info");23export async function up(): Promise<{ ok: boolean; version: string | null }> {24 try { const i = await info(); return { ok: true, version: i.version ?? null }; }25 catch { try { await call("/v3/paths/list?itemsPerPage=1"); return { ok: true, version: null }; } catch { return { ok: false, version: null }; } }26}27/** Ends the publisher of a path: the ingest process then stops or reconnects by itself. */28export async function kickPublisher(p: MtxPath): Promise<void> {29 if (p.source?.type === "rtspSession") await call(`/v3/rtspsessions/kick/${p.source.id}`, { method: "POST" }).catch(() => {});30}31