2021-01-31 21:55:25 -08:00
|
|
|
// This file is part of Moonfire NVR, a security camera network video recorder.
|
|
|
|
// Copyright (C) 2021 The Moonfire NVR Authors; see AUTHORS and LICENSE.txt.
|
|
|
|
// SPDX-License-Identifier: GPL-v3.0-or-later WITH GPL-3.0-linking-exception
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file Convenience wrapper around the Moonfire NVR API layer.
|
|
|
|
*
|
|
|
|
* See <tt>design/api.md</tt> for a description of the API.
|
|
|
|
*
|
|
|
|
* The functions here return a Typescript discriminating union of status.
|
|
|
|
* This seems convenient for ensuring the caller handles all possibilities.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import { Camera, Session } from "./types";
|
|
|
|
|
2021-03-05 16:56:51 -08:00
|
|
|
export type StreamType = "main" | "sub";
|
|
|
|
|
|
|
|
export interface FetchSuccess<T> {
|
2021-01-31 21:55:25 -08:00
|
|
|
status: "success";
|
|
|
|
response: T;
|
|
|
|
}
|
|
|
|
|
2021-03-05 16:56:51 -08:00
|
|
|
export interface FetchAborted {
|
2021-01-31 21:55:25 -08:00
|
|
|
status: "aborted";
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface FetchError {
|
|
|
|
status: "error";
|
|
|
|
message: string;
|
|
|
|
httpStatus?: number;
|
|
|
|
}
|
|
|
|
|
2021-03-05 16:56:51 -08:00
|
|
|
export type FetchResult<T> = FetchSuccess<T> | FetchAborted | FetchError;
|
2021-01-31 21:55:25 -08:00
|
|
|
|
|
|
|
async function myfetch(
|
|
|
|
url: string,
|
|
|
|
init: RequestInit
|
|
|
|
): Promise<FetchResult<Response>> {
|
|
|
|
let response;
|
|
|
|
try {
|
|
|
|
response = await fetch(url, init);
|
|
|
|
} catch (e) {
|
|
|
|
if (e.name === "AbortError") {
|
|
|
|
return { status: "aborted" };
|
|
|
|
} else {
|
|
|
|
return {
|
|
|
|
status: "error",
|
|
|
|
message: `network error: ${e.message}`,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!response.ok) {
|
|
|
|
let text;
|
|
|
|
try {
|
|
|
|
text = await response.text();
|
|
|
|
} catch (e) {
|
|
|
|
console.warn(
|
|
|
|
`${url}: ${response.status}: unable to read body: ${e.message}`
|
|
|
|
);
|
|
|
|
return {
|
|
|
|
status: "error",
|
|
|
|
httpStatus: response.status,
|
|
|
|
message: `unable to read body: ${e.message}`,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
status: "error",
|
|
|
|
httpStatus: response.status,
|
|
|
|
message: text,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
console.debug(`${url}: ${response.status}`);
|
|
|
|
return {
|
|
|
|
status: "success",
|
|
|
|
response,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Fetches an initialization segment. */
|
|
|
|
export async function init(
|
|
|
|
hash: string,
|
|
|
|
init: RequestInit
|
|
|
|
): Promise<FetchResult<ArrayBuffer>> {
|
|
|
|
const url = `/api/init/${hash}.mp4`;
|
|
|
|
const fetchRes = await myfetch(url, init);
|
|
|
|
if (fetchRes.status !== "success") {
|
|
|
|
return fetchRes;
|
|
|
|
}
|
|
|
|
let body;
|
|
|
|
try {
|
|
|
|
body = await fetchRes.response.arrayBuffer();
|
|
|
|
} catch (e) {
|
|
|
|
console.warn(`${url}: unable to read body: ${e.message}`);
|
|
|
|
return {
|
|
|
|
status: "error",
|
|
|
|
message: `unable to read body: ${e.message}`,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
status: "success",
|
|
|
|
response: body,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
async function json<T>(
|
|
|
|
url: string,
|
|
|
|
init: RequestInit
|
|
|
|
): Promise<FetchResult<T>> {
|
|
|
|
const fetchRes = await myfetch(url, init);
|
|
|
|
if (fetchRes.status !== "success") {
|
|
|
|
return fetchRes;
|
|
|
|
}
|
|
|
|
let body;
|
|
|
|
try {
|
|
|
|
body = await fetchRes.response.json();
|
|
|
|
} catch (e) {
|
|
|
|
console.warn(`${url}: unable to read body: ${e.message}`);
|
|
|
|
return {
|
|
|
|
status: "error",
|
|
|
|
message: `unable to read body: ${e.message}`,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
status: "success",
|
|
|
|
response: body,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-03-05 16:56:51 -08:00
|
|
|
export interface ToplevelResponse {
|
2021-01-31 21:55:25 -08:00
|
|
|
timeZoneName: string;
|
|
|
|
cameras: Camera[];
|
|
|
|
session: Session | undefined;
|
2021-03-05 16:56:51 -08:00
|
|
|
}
|
2021-01-31 21:55:25 -08:00
|
|
|
|
|
|
|
/** Fetches the top-level API data. */
|
|
|
|
export async function toplevel(init: RequestInit) {
|
2021-03-05 16:56:51 -08:00
|
|
|
const resp = await json<ToplevelResponse>("/api/?days=true", init);
|
|
|
|
if (resp.status === "success") {
|
|
|
|
resp.response.cameras.forEach((c) => {
|
|
|
|
for (const key in c.streams) {
|
|
|
|
const s = c.streams[key as StreamType];
|
|
|
|
s.camera = c;
|
|
|
|
s.streamType = key as StreamType;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return resp;
|
2021-01-31 21:55:25 -08:00
|
|
|
}
|
|
|
|
|
2021-03-05 16:56:51 -08:00
|
|
|
export interface LoginRequest {
|
2021-01-31 21:55:25 -08:00
|
|
|
username: string;
|
|
|
|
password: string;
|
2021-03-05 16:56:51 -08:00
|
|
|
}
|
2021-01-31 21:55:25 -08:00
|
|
|
|
|
|
|
/** Logs in. */
|
|
|
|
export async function login(req: LoginRequest, init: RequestInit) {
|
|
|
|
return await myfetch("/api/login", {
|
|
|
|
method: "POST",
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
},
|
|
|
|
body: JSON.stringify(req),
|
|
|
|
...init,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-03-05 16:56:51 -08:00
|
|
|
export interface LogoutRequest {
|
2021-01-31 21:55:25 -08:00
|
|
|
csrf: string;
|
2021-03-05 16:56:51 -08:00
|
|
|
}
|
2021-01-31 21:55:25 -08:00
|
|
|
|
|
|
|
/** Logs out. */
|
|
|
|
export async function logout(req: LogoutRequest, init: RequestInit) {
|
|
|
|
return await myfetch("/api/logout", {
|
|
|
|
method: "POST",
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
},
|
|
|
|
body: JSON.stringify(req),
|
|
|
|
...init,
|
|
|
|
});
|
|
|
|
}
|
2021-03-05 16:56:51 -08:00
|
|
|
|
|
|
|
export interface Recording {
|
|
|
|
startId: number;
|
|
|
|
endId?: number;
|
2021-03-13 21:51:59 -08:00
|
|
|
firstUncommitted?: number;
|
2021-03-05 16:56:51 -08:00
|
|
|
growing?: boolean;
|
|
|
|
openId: number;
|
|
|
|
startTime90k: number;
|
|
|
|
endTime90k: number;
|
|
|
|
videoSampleEntryId: number;
|
|
|
|
videoSamples: number;
|
|
|
|
sampleFileBytes: number;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface VideoSampleEntry {
|
|
|
|
width: number;
|
|
|
|
height: number;
|
|
|
|
pixelHSpacing?: number;
|
|
|
|
pixelVSpacing?: number;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface RecordingsRequest {
|
|
|
|
cameraUuid: string;
|
|
|
|
stream: StreamType;
|
|
|
|
startTime90k?: number;
|
|
|
|
endTime90k?: number;
|
|
|
|
split90k?: number;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface RecordingsResponse {
|
|
|
|
recordings: Recording[];
|
|
|
|
videoSampleEntries: { [id: number]: VideoSampleEntry };
|
|
|
|
}
|
|
|
|
|
|
|
|
function withQuery(baseUrl: string, params: { [key: string]: any }): string {
|
|
|
|
const p = new URLSearchParams();
|
|
|
|
for (const k in params) {
|
|
|
|
const v = params[k];
|
|
|
|
if (v !== undefined) {
|
|
|
|
p.append(k, v.toString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const ps = p.toString();
|
|
|
|
return ps !== "" ? `${baseUrl}?${ps}` : baseUrl;
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function recordings(req: RecordingsRequest, init: RequestInit) {
|
|
|
|
const p = new URLSearchParams();
|
|
|
|
if (req.startTime90k !== undefined) {
|
|
|
|
p.append("startTime90k", req.startTime90k.toString());
|
|
|
|
}
|
|
|
|
if (req.endTime90k !== undefined) {
|
|
|
|
p.append("endTime90k", req.endTime90k.toString());
|
|
|
|
}
|
|
|
|
if (req.split90k !== undefined) {
|
|
|
|
p.append("split90k", req.split90k.toString());
|
|
|
|
}
|
|
|
|
const url = withQuery(
|
|
|
|
`/api/cameras/${req.cameraUuid}/${req.stream}/recordings`,
|
|
|
|
{
|
|
|
|
startTime90k: req.startTime90k,
|
|
|
|
endTime90k: req.endTime90k,
|
|
|
|
split90k: req.split90k,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
return await json<RecordingsResponse>(url, init);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function recordingUrl(
|
|
|
|
cameraUuid: string,
|
|
|
|
stream: StreamType,
|
|
|
|
r: Recording
|
|
|
|
): string {
|
|
|
|
let s = `${r.startId}`;
|
|
|
|
if (r.endId !== undefined) {
|
|
|
|
s += `-${r.endId}`;
|
|
|
|
}
|
2021-03-13 21:51:59 -08:00
|
|
|
if (r.firstUncommitted !== undefined) {
|
2021-03-05 16:56:51 -08:00
|
|
|
s += `@${r.openId}`;
|
|
|
|
}
|
|
|
|
return withQuery(`/api/cameras/${cameraUuid}/${stream}/view.mp4`, {
|
|
|
|
s,
|
|
|
|
ts: true,
|
|
|
|
});
|
|
|
|
}
|