deltacast-sdi-recorder/frontend/src/hooks/useRecorder.ts

109 lines
3.4 KiB
TypeScript

import { useState } from 'react';
import { RecorderConfig, SCTE35Marker } from '../types';
const API_BASE = '/api';
export function useRecorder() {
const [loading, setLoading] = useState(false);
const [error, setError] = useState<string | null>(null);
const startRecording = async (portIndex: number, config: RecorderConfig): Promise<void> => {
setLoading(true);
setError(null);
try {
const response = await fetch(`${API_BASE}/ports/${portIndex}/start`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(config),
});
if (!response.ok) throw new Error(`Failed to start recording: ${response.statusText}`);
} catch (err) {
const msg = err instanceof Error ? err.message : 'Unknown error';
setError(msg);
throw err;
} finally {
setLoading(false);
}
};
const stopRecording = async (portIndex: number): Promise<void> => {
setLoading(true);
setError(null);
try {
const response = await fetch(`${API_BASE}/ports/${portIndex}/stop`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
});
if (!response.ok) throw new Error(`Failed to stop recording: ${response.statusText}`);
} catch (err) {
const msg = err instanceof Error ? err.message : 'Unknown error';
setError(msg);
throw err;
} finally {
setLoading(false);
}
};
/** Global SCTE35 inject (hits all SRT outputs) */
const injectSCTE35 = async (
eventId: number,
durationSeconds: number,
webhookUrl?: string
): Promise<SCTE35Marker | null> => {
setLoading(true);
setError(null);
try {
const response = await fetch(`${API_BASE}/scte35/inject`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
event_id: eventId,
duration_seconds: durationSeconds,
webhook_url: webhookUrl || null,
}),
});
if (!response.ok) throw new Error(`Failed to inject SCTE35: ${response.statusText}`);
return await response.json() as SCTE35Marker;
} catch (err) {
const msg = err instanceof Error ? err.message : 'Unknown error';
setError(msg);
throw err;
} finally {
setLoading(false);
}
};
/** Per-port SCTE35 inject — targets a specific port's SRT output(s) */
const injectSCTE35ForPort = async (
portIndex: number,
eventId: number,
durationSeconds: number,
srtDestinationUrl?: string,
webhookUrl?: string
): Promise<SCTE35Marker | null> => {
setLoading(true);
setError(null);
try {
const response = await fetch(`${API_BASE}/ports/${portIndex}/scte35/inject`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
event_id: eventId,
duration_seconds: durationSeconds,
webhook_url: webhookUrl || null,
srt_destination_url: srtDestinationUrl || null,
}),
});
if (!response.ok) throw new Error(`Failed to inject SCTE35 on port ${portIndex}: ${response.statusText}`);
return await response.json() as SCTE35Marker;
} catch (err) {
const msg = err instanceof Error ? err.message : 'Unknown error';
setError(msg);
throw err;
} finally {
setLoading(false);
}
};
return { startRecording, stopRecording, injectSCTE35, injectSCTE35ForPort, loading, error };
}