Skip to main content

heldar_kernel/routes/
liveview.rs

1use axum::extract::{Path, State};
2use axum::http::{header::HOST, HeaderMap};
3use axum::routing::get;
4use axum::{Json, Router};
5
6use crate::auth::Principal;
7use crate::error::AppResult;
8use crate::services::mediamtx::{self, LiveUrls};
9use crate::state::AppState;
10
11pub fn router() -> Router<AppState> {
12    Router::new().route(
13        "/api/v1/cameras/{id}/liveview",
14        get(liveview).post(liveview),
15    )
16}
17
18/// Ensure a MediaMTX path exists for the camera and return live playback URLs.
19async fn liveview(
20    State(st): State<AppState>,
21    principal: Principal,
22    headers: HeaderMap,
23    Path(id): Path<String>,
24) -> AppResult<Json<LiveUrls>> {
25    // Operational action (viewer+); the extractor enforces auth when it is enabled.
26    principal.require(principal.can_view(), "view live streams")?;
27    // The Host the client used lets us hand back stream URLs reachable over the tunnel / LAN.
28    let host = headers.get(HOST).and_then(|v| v.to_str().ok());
29    Ok(Json(mediamtx::ensure_live(&st, &id, host).await?))
30}