multimux 0.9.0

Multi-input (RTSP/RTP/TS-UDP/TS-HTTP/SRT/HLS-pull/DASH-pull/Smooth-pull/RTMP), multi-output (LL-HLS/DASH/LL-DASH) just-in-time repackaging HTTP origin (library: tokio + axum), with shared output auth and an external scheme plugin registry.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
//! `CatchupOutput`: the HTTP surface for catch-up / time-shift /
//! VOD-from-live serving over the DVR durable archive (issue #900) — a
//! thin axum adapter over `crate::catchup`'s pure archive-scan/merge/
//! render logic, mounted and gated exactly like every other
//! [`crate::output::Output`] (so it shares the same output auth — Basic/
//! Digest/Bearer/Forwarded — every other output gets from
//! `crate::origin::router`'s `output_auth_gate`, rather than inventing a
//! second HTTP surface with its own auth path).
//!
//! Requires the route's DVR archive to be enabled
//! (`crate::config::Route::dvr.enabled`) — `crate::config::Route::validate_standalone`
//! rejects a `"catchup"` output configured without it, so a mounted
//! `CatchupOutput` in a validated config always has an archive to read.
//! The handlers below still check defensively (`RouteHandle::dvr_config`
//! returning `None`), in case that invariant is ever bypassed (e.g. the
//! runtime admin API adding a route from an unvalidated source).
//!
//! # Three routes, one archive
//!
//! - `GET /catchup.m3u8[?window_secs=N]` — a live-continuing playlist
//!   spanning the archive plus whatever the live `Trunk` has closed since
//!   the archive was last polled (the straddle boundary — see
//!   `crate::catchup`'s own module doc). `window_secs` bounds it to the
//!   trailing N seconds (the "catch-up window"); omitted, the whole
//!   archive plus live tail (RFC 8216 §4.4.3.5 `EVENT` semantics — segments
//!   only ever get appended, never removed, since nothing here evicts
//!   archived history the way a live rolling window does).
//! - `GET /vod/p{N}.m3u8` — exactly one archived period's segments,
//!   `#EXT-X-ENDLIST` and `PLAYLIST-TYPE:VOD` once that period is
//!   definitively finished (a later period exists on disk); otherwise the
//!   same still-growing shape as `catchup.m3u8`, restricted to that one
//!   period. This is "VOD-from-live": a finished recorded programme served
//!   as a complete, immutable asset.
//! - `GET /catchup/seg-{seq}.{ext}` — the resource route both playlists
//!   above reference: archived bytes read straight off disk when `seq` is
//!   in the archive, or (the still-unarchived tail) the same
//!   `hls_runtime::server::HlsOrigin` every other output resolves against,
//!   when it is not. One endpoint serves both sources — the client never
//!   needs to know which one held a given segment.
//!
//! Mounted under `/catchup*`/`/vod/*` (two-segment paths), never at the
//! same single-segment `/:file` shape `crate::origin::resource`'s shared
//! catch-all owns — axum's router cannot have two routes claim the exact
//! same wildcard segment, so this module deliberately nests one level
//! deeper instead of teaching the shared resource route a new filename
//! grammar.

use std::path::PathBuf;
use std::sync::Arc;

use axum::Router;
use axum::extract::{Path, Query, State};
use axum::http::{StatusCode, header};
use axum::response::{IntoResponse, Response};
use axum::routing::get;
use broadcast_common::Timestamp;
use broadcast_hls::PlaylistType;
use hls_runtime::server::{Container, DEFAULT_TRACK_ID, HlsBody, HlsRequest};
use media_plane::egress::{AwaitPolicy, EgressResponse, ServedEgress};
use serde::Deserialize;

use crate::catchup;
use crate::http;
use crate::origin::resource::cors_preflight;
use crate::output::{Output, OutputKind};
use crate::route::RouteHandle;

const MEDIA_PLAYLIST_CONTENT_TYPE: &str = "application/vnd.apple.mpegurl";
const TS_SEGMENT_CONTENT_TYPE: &str = "video/mp2t";
const FMP4_SEGMENT_CONTENT_TYPE: &str = "video/mp4";

/// The catch-up/VOD-from-live [`Output`] — see the module docs.
#[derive(Debug, Default)]
pub struct CatchupOutput;

impl Output for CatchupOutput {
    fn kind(&self) -> OutputKind {
        OutputKind::Catchup
    }

    /// Routes (relative — mounted by the origin under `/{stream}/`):
    /// - `GET /catchup.m3u8`
    /// - `GET /vod/:period` (`period` is `p{N}.m3u8`)
    /// - `GET /catchup/:file` (`file` is `seg-{seq}.{ext}`)
    fn manifest_routes(&self, route: Arc<RouteHandle>) -> Router {
        Router::new()
            .route(
                "/catchup.m3u8",
                get(catchup_playlist).options(cors_preflight),
            )
            .route("/vod/:period", get(vod_playlist).options(cors_preflight))
            .route(
                "/catchup/:file",
                get(catchup_resource).options(cors_preflight),
            )
            .with_state(route)
    }
}

/// The dynamic-filename extension (without the leading `.`) for `route`'s
/// configured container — mirrors `crate::route::ProgramServing::new`'s own
/// mapping (`Container` is `#[non_exhaustive]`, hence the catch-all).
fn container_ext(container: Container) -> &'static str {
    match container {
        Container::MpegTs => "ts",
        Container::Fmp4 => "m4s",
        _ => "m4s",
    }
}

fn segment_content_type(ext: &str) -> &'static str {
    if ext == "ts" {
        TS_SEGMENT_CONTENT_TYPE
    } else {
        FMP4_SEGMENT_CONTENT_TYPE
    }
}

/// `#EXT-X-MAP` URI for `container`'s live init resource (served by the
/// shared resource route, `crate::origin::resource`), or `None` for
/// [`Container::MpegTs`] (no init resource exists — see
/// `hls_runtime::server::Container`'s own doc). Relative to this output's
/// own playlist paths, both of which are top-level under `/{stream}/`,
/// same as `init-{track}.mp4` itself.
fn map_uri(container: Container) -> Option<String> {
    matches!(container, Container::Fmp4).then(|| format!("init-{DEFAULT_TRACK_ID}.mp4"))
}

#[derive(Debug, Default, Deserialize)]
pub struct CatchupPlaylistQuery {
    /// Bound the catch-up window to this many trailing seconds of
    /// `crate::dvr::IndexEntry::start_pts_ns` — see
    /// `crate::catchup::apply_window`. Omitted or `0`: the whole archive
    /// plus the live tail.
    window_secs: Option<u64>,
}

/// `GET /catchup.m3u8` — see the module docs.
async fn catchup_playlist(
    State(route): State<Arc<RouteHandle>>,
    Query(q): Query<CatchupPlaylistQuery>,
) -> Response {
    let serving = match http::resolve_route_program(&route) {
        Ok(serving) => serving,
        Err(resp) => return *resp,
    };
    let Some(dvr) = route.dvr_config() else {
        return StatusCode::NOT_FOUND.into_response();
    };
    let ext = container_ext(route.container());
    let dir = catchup::archive_dir(dvr, route.name());
    let archived = catchup::scan_archive(&dir);
    let live = serving.ll_hls().closed_segments();
    let combined = catchup::merge_segments(&archived, &live);
    let windowed = catchup::apply_window(&combined, q.window_secs);
    let body = catchup::render_playlist(
        &windowed,
        ext,
        map_uri(route.container()).as_deref(),
        PlaylistType::Event,
        false,
    );
    ([(header::CONTENT_TYPE, MEDIA_PLAYLIST_CONTENT_TYPE)], body).into_response()
}

/// `GET /vod/p{N}.m3u8` — see the module docs.
async fn vod_playlist(
    State(route): State<Arc<RouteHandle>>,
    Path(period_file): Path<String>,
) -> Response {
    let Some(dvr) = route.dvr_config() else {
        return StatusCode::NOT_FOUND.into_response();
    };
    let Some(period_num) = parse_period_filename(&period_file) else {
        return StatusCode::BAD_REQUEST.into_response();
    };
    let ext = container_ext(route.container());
    let dir = catchup::archive_dir(dvr, route.name());
    let segments = catchup::read_period_segments(&dir, period_num);
    if segments.is_empty() {
        return StatusCode::NOT_FOUND.into_response();
    }
    // Definitively finished iff a later period exists on disk —
    // `crate::dvr::DvrRecorder::start_period` never opens period N+1 until
    // period N is closed, so this is exact, not a guess.
    let finished = catchup::list_period_nums(&dir)
        .iter()
        .any(|&n| n > period_num);
    let combined: Vec<catchup::CatchupSegment> = segments
        .iter()
        .map(|s| catchup::CatchupSegment {
            seq: s.seq,
            start_pts_ns: s.start_pts_ns,
            duration_secs: s.duration_secs,
            discontinuous: s.discontinuous,
        })
        .collect();
    let playlist_type = if finished {
        PlaylistType::Vod
    } else {
        PlaylistType::Event
    };
    let body = catchup::render_playlist(
        &combined,
        ext,
        map_uri(route.container()).as_deref(),
        playlist_type,
        finished,
    );
    ([(header::CONTENT_TYPE, MEDIA_PLAYLIST_CONTENT_TYPE)], body).into_response()
}

fn parse_period_filename(file: &str) -> Option<u32> {
    file.strip_prefix('p')?.strip_suffix(".m3u8")?.parse().ok()
}

/// `GET /catchup/seg-{seq}.{ext}` — archived bytes read straight from disk
/// when `seq` is archived; otherwise the live `Trunk`'s still-unarchived
/// tail, resolved through the exact same `HlsOrigin` the shared resource
/// route (`crate::origin::resource`) uses for `seg-{track}-{seq}.{ext}`.
/// This dispatch — not a second cache of anything — is what makes the
/// straddle boundary invisible to the client: one filename grammar, either
/// source.
async fn catchup_resource(
    State(route): State<Arc<RouteHandle>>,
    Path(file): Path<String>,
) -> Response {
    let ext = container_ext(route.container());
    let Some(seq) = parse_seg_filename(&file, ext) else {
        return StatusCode::NOT_FOUND.into_response();
    };
    let Some(dvr) = route.dvr_config() else {
        return StatusCode::NOT_FOUND.into_response();
    };
    let dir: PathBuf = catchup::archive_dir(dvr, route.name());
    if let Some(seg) = catchup::find_archived_segment(&dir, seq) {
        return match catchup::read_archived_bytes(
            &dir,
            ext,
            seg.period_num,
            seg.byte_offset,
            seg.byte_len,
        ) {
            Ok(bytes) => {
                ([(header::CONTENT_TYPE, segment_content_type(ext))], bytes).into_response()
            }
            Err(e) => {
                tracing::error!(error = %e, seq, "catch-up: failed reading archived segment bytes");
                StatusCode::INTERNAL_SERVER_ERROR.into_response()
            }
        };
    }

    // Not archived (yet) — the still-live tail. A one-shot, non-blocking
    // resolve (deadline == now): the client only ever requests a segment a
    // playlist just told it exists, so there is nothing to wait for here —
    // unlike the live playlist's own blocking-reload semantics.
    let serving = match http::resolve_route_program(&route) {
        Ok(serving) => serving,
        Err(resp) => return *resp,
    };
    let ll_hls = serving.ll_hls();
    let now = Timestamp::from_nanos(0);
    let policy = AwaitPolicy::new(now);
    let request = HlsRequest::Resource {
        name: format!("seg-{DEFAULT_TRACK_ID}-{seq}.{ext}"),
    };
    match ll_hls.resolve(request, now, policy) {
        EgressResponse::Ready {
            body: HlsBody::Resource(bytes),
            ..
        } => ([(header::CONTENT_TYPE, segment_content_type(ext))], bytes).into_response(),
        _ => StatusCode::NOT_FOUND.into_response(),
    }
}

fn parse_seg_filename(file: &str, ext: &str) -> Option<u32> {
    let suffix = format!(".{ext}");
    file.strip_prefix("seg-")?
        .strip_suffix(&suffix)?
        .parse()
        .ok()
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::dvr::{ArchiveOverrunSerde, DvrConfig};
    use crate::route::SPTS_PROGRAM_ID;

    fn temp_dir() -> PathBuf {
        use std::sync::atomic::{AtomicU64, Ordering};
        static COUNTER: AtomicU64 = AtomicU64::new(0);
        let n = COUNTER.fetch_add(1, Ordering::SeqCst);
        let dir = std::env::temp_dir().join(format!(
            "multimux-catchup-output-{}-{}",
            std::process::id(),
            n
        ));
        let _ = std::fs::create_dir_all(&dir);
        dir
    }

    fn cleanup(dir: &std::path::Path) {
        let _ = std::fs::remove_dir_all(dir);
    }

    fn dvr_config(tmp: &std::path::Path) -> DvrConfig {
        DvrConfig {
            enabled: true,
            archive_root: tmp.to_string_lossy().to_string(),
            retention_periods: 10,
            retention_bytes: 0,
            period_duration_secs: 3600,
            overrun: ArchiveOverrunSerde::Gap,
            dvb_service_id: None,
        }
    }

    fn seg_bytes(seq: u32, byte: u8) -> transmux::ll_hls::SegmentInfo {
        transmux::ll_hls::SegmentInfo {
            bytes: vec![byte; 24],
            duration: 3.0,
            segment_seq: seq,
            part_count: 1,
        }
    }

    async fn body_string(resp: Response) -> String {
        let bytes = axum::body::to_bytes(resp.into_body(), usize::MAX)
            .await
            .unwrap();
        String::from_utf8(bytes.to_vec()).unwrap()
    }

    async fn body_bytes(resp: Response) -> Vec<u8> {
        axum::body::to_bytes(resp.into_body(), usize::MAX)
            .await
            .unwrap()
            .to_vec()
    }

    /// The end-to-end straddle bite test (issue #900's whole point): three
    /// segments are archived (drained via `drain_dvr`), a fourth is
    /// published to the live `Trunk` only — never drained to disk. The
    /// combined catch-up playlist must show all four, continuously, and
    /// BOTH the archived segments and the live-only segment must be
    /// fetchable byte-exact through the ONE `catchup/seg-*` endpoint.
    ///
    /// This would fail under two disjoint playlists (the exact failure
    /// mode #900 exists to prevent): a naive "archive playlist" alone
    /// would omit segment 4 entirely, and a naive "live playlist" alone
    /// would use `MEDIA-SEQUENCE` starting wherever the live window
    /// happens to begin, not the true first archived segment.
    #[tokio::test]
    async fn catchup_playlist_straddles_archive_and_live_tail_continuously() {
        let tmp = temp_dir();
        let route = Arc::new(
            RouteHandle::new(4.0, 500, 8)
                .with_name("straddle")
                .with_dvr(dvr_config(&tmp)),
        );
        route.publish_new_program(SPTS_PROGRAM_ID);
        route.set_init(SPTS_PROGRAM_ID, vec![0xAA; 4]);

        // Segments 1..=3: published, then drained to the archive.
        for (seq, byte) in [(1u32, 0x11u8), (2, 0x22), (3, 0x33)] {
            route.add_segment(SPTS_PROGRAM_ID, seg_bytes(seq, byte));
        }
        route.drain_dvr();

        // Segment 4: published, but NEVER drained -- lives only in the
        // live Trunk/HlsOrigin window.
        route.add_segment(SPTS_PROGRAM_ID, seg_bytes(4, 0x44));

        let resp =
            catchup_playlist(State(route.clone()), Query(CatchupPlaylistQuery::default())).await;
        assert_eq!(resp.status(), StatusCode::OK);
        let body = body_string(resp).await;
        assert!(body.contains("#EXT-X-MEDIA-SEQUENCE:1"), "body: {body}");
        for seq in 1..=4 {
            assert!(
                body.contains(&format!("catchup/seg-{seq}.m4s")),
                "seq {seq} missing from combined playlist: {body}"
            );
        }
        // No duplicate entries: exactly 4 EXTINF lines.
        assert_eq!(
            body.matches("#EXTINF").count(),
            4,
            "must show each segment exactly once: {body}"
        );

        // Archived segment: served straight from disk.
        let archived_resp =
            catchup_resource(State(route.clone()), Path("seg-2.m4s".to_string())).await;
        assert_eq!(archived_resp.status(), StatusCode::OK);
        assert_eq!(body_bytes(archived_resp).await, vec![0x22u8; 24]);

        // Live-only segment: served through the SAME endpoint, resolved
        // via the live HlsOrigin.
        let live_resp = catchup_resource(State(route), Path("seg-4.m4s".to_string())).await;
        assert_eq!(live_resp.status(), StatusCode::OK);
        assert_eq!(body_bytes(live_resp).await, vec![0x44u8; 24]);

        cleanup(&tmp);
    }

    #[tokio::test]
    async fn catchup_playlist_without_dvr_is_404() {
        let route = Arc::new(RouteHandle::new(4.0, 500, 8).with_name("no-dvr"));
        route.publish_new_program(SPTS_PROGRAM_ID);
        route.set_init(SPTS_PROGRAM_ID, vec![0xAA; 4]);
        let resp = catchup_playlist(State(route), Query(CatchupPlaylistQuery::default())).await;
        assert_eq!(resp.status(), StatusCode::NOT_FOUND);
    }

    #[tokio::test]
    async fn window_secs_bounds_the_playlist() {
        let tmp = temp_dir();
        let route = Arc::new(
            RouteHandle::new(4.0, 500, 8)
                .with_name("windowed")
                .with_dvr(dvr_config(&tmp)),
        );
        route.publish_new_program(SPTS_PROGRAM_ID);
        route.set_init(SPTS_PROGRAM_ID, vec![0xAA; 4]);
        for (seq, byte) in [(1u32, 0x11u8), (2, 0x22), (3, 0x33)] {
            route.add_segment(SPTS_PROGRAM_ID, seg_bytes(seq, byte));
        }
        route.drain_dvr();

        // Segments start at 0s, 3s, 6s (each 3s long); the live edge is
        // segment 3's start (6s). A 2s window reaches back to 4s, which
        // excludes segment 2 (starts at 3s) and keeps only segment 3.
        let resp = catchup_playlist(
            State(route),
            Query(CatchupPlaylistQuery {
                window_secs: Some(2),
            }),
        )
        .await;
        assert_eq!(resp.status(), StatusCode::OK);
        let body = body_string(resp).await;
        assert!(body.contains("catchup/seg-3.m4s"), "body: {body}");
        assert!(!body.contains("catchup/seg-2.m4s"), "body: {body}");
        assert!(!body.contains("catchup/seg-1.m4s"), "body: {body}");
        assert_eq!(body.matches("#EXTINF").count(), 1, "body: {body}");
    }

    #[tokio::test]
    async fn vod_playlist_finished_period_has_endlist() {
        let tmp = temp_dir();
        let route = Arc::new(
            RouteHandle::new(4.0, 500, 8)
                .with_name("vod")
                .with_dvr(dvr_config(&tmp)),
        );
        route.publish_new_program(SPTS_PROGRAM_ID);
        route.set_init(SPTS_PROGRAM_ID, vec![0xAA; 4]);

        route.add_segment(SPTS_PROGRAM_ID, seg_bytes(1, 0x11));
        route.drain_dvr(); // opens + writes period 0 with init A

        // Changing the init rolls the period (crate::dvr::DvrRecorder's
        // mid-stream init-change rollover) — a real, reliable way to force
        // period 1 to open without waiting out `period_duration_secs`.
        route.set_init(SPTS_PROGRAM_ID, vec![0xBB; 4]);
        route.add_segment(SPTS_PROGRAM_ID, seg_bytes(2, 0x22));
        route.drain_dvr();

        let resp = vod_playlist(State(route.clone()), Path("p0.m3u8".to_string())).await;
        assert_eq!(resp.status(), StatusCode::OK);
        let body = body_string(resp).await;
        assert!(
            body.contains("#EXT-X-ENDLIST"),
            "period 0 has a successor (period 1) so it must be finished: {body}"
        );
        assert!(body.contains("#EXT-X-PLAYLIST-TYPE:VOD"), "body: {body}");

        cleanup(&tmp);
    }

    #[tokio::test]
    async fn vod_playlist_unknown_period_is_404() {
        let tmp = temp_dir();
        let route = Arc::new(
            RouteHandle::new(4.0, 500, 8)
                .with_name("vod-missing")
                .with_dvr(dvr_config(&tmp)),
        );
        route.publish_new_program(SPTS_PROGRAM_ID);
        let resp = vod_playlist(State(route), Path("p99.m3u8".to_string())).await;
        assert_eq!(resp.status(), StatusCode::NOT_FOUND);
        cleanup(&tmp);
    }

    #[tokio::test]
    async fn catchup_resource_unmatched_filename_404() {
        let route = Arc::new(RouteHandle::new(4.0, 500, 8).with_name("bad-name"));
        let resp = catchup_resource(State(route), Path("not-a-segment.txt".to_string())).await;
        assert_eq!(resp.status(), StatusCode::NOT_FOUND);
    }
}