oximedia-net 0.1.8

Network streaming for OxiMedia
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
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
//! Per-connection RTSP server handler.
//!
//! Each accepted TCP connection is handed to a `ServerConnection`, which:
//!
//! 1. Reads bytes into a buffer and calls `try_parse_request` until a complete
//!    RTSP request arrives.
//! 2. Dispatches to the appropriate method handler.
//! 3. Writes the response (and, after PLAY, forwards interleaved RTP frames).
//!
//! The RTP forwarding task runs as a sibling `tokio::spawn` and is cancelled
//! via an `Arc<AtomicBool>` stop flag on PAUSE or TEARDOWN.

use std::sync::{
    atomic::{AtomicBool, Ordering},
    Arc,
};
use std::time::{SystemTime, UNIX_EPOCH};

use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::net::TcpStream;
use tokio::sync::broadcast;

use super::registry::MountPointRegistry;
use super::state::{RtspServerConfig, RtspSession, RtspSessionState};
use crate::rtsp::message::{try_parse_request, Headers, RequestParseStatus, Response};
use crate::rtsp::transport::encode_interleaved;
use crate::rtsp::Method;

/// Read buffer capacity — 64 KiB should accommodate even large SDP bodies.
const READ_BUF_CAPACITY: usize = 65536;
/// I/O read chunk size.
const CHUNK_SIZE: usize = 4096;
/// Server identification string.
const SERVER_NAME: &str = concat!("oximedia-net-rtsp/", env!("CARGO_PKG_VERSION"));

/// Per-connection RTSP server actor.
pub struct ServerConnection {
    stream: TcpStream,
    config: Arc<RtspServerConfig>,
    registry: MountPointRegistry,
    session: Option<RtspSession>,
    read_buf: Vec<u8>,
    /// Optional stop flag for the currently-active RTP sender task.
    rtp_stop: Option<Arc<AtomicBool>>,
}

impl ServerConnection {
    /// Create a new connection handler.
    #[must_use]
    pub fn new(
        stream: TcpStream,
        config: Arc<RtspServerConfig>,
        registry: MountPointRegistry,
    ) -> Self {
        Self {
            stream,
            config,
            registry,
            session: None,
            read_buf: Vec::with_capacity(READ_BUF_CAPACITY),
            rtp_stop: None,
        }
    }

    /// Drive the connection until the client disconnects or an error occurs.
    pub async fn run(mut self) {
        let _ = self.run_inner().await;
        // Stop any background RTP sender on clean or error exit.
        if let Some(flag) = self.rtp_stop.take() {
            flag.store(true, Ordering::Relaxed);
        }
    }

    async fn run_inner(&mut self) -> Result<(), ()> {
        loop {
            // Try to parse a complete request from the read buffer.
            match try_parse_request(&self.read_buf).map_err(|_| ())? {
                RequestParseStatus::Complete { consumed, request } => {
                    self.read_buf.drain(..consumed);
                    let cseq = request
                        .headers
                        .get("CSeq")
                        .and_then(|v| v.trim().parse::<u32>().ok())
                        .unwrap_or(0);
                    let response = self.dispatch(&request, cseq).await;
                    let wire = response.encode();
                    self.stream.write_all(&wire).await.map_err(|_| ())?;
                    self.stream.flush().await.map_err(|_| ())?;
                }
                RequestParseStatus::NeedMore => {
                    // Need more bytes — read from the socket.
                    let mut chunk = [0u8; CHUNK_SIZE];
                    let n = self.stream.read(&mut chunk).await.map_err(|_| ())?;
                    if n == 0 {
                        return Err(()); // Client closed connection
                    }
                    self.read_buf.extend_from_slice(&chunk[..n]);
                }
            }
        }
    }

    /// Dispatch an incoming request to the correct handler.
    async fn dispatch(&mut self, req: &crate::rtsp::message::Request, cseq: u32) -> Response {
        match req.method {
            Method::Options => self.handle_options(cseq),
            Method::Describe => self.handle_describe(&req.uri, cseq),
            Method::Setup => self.handle_setup(&req.uri, req.headers.get("Transport"), cseq),
            Method::Play => self.handle_play(cseq).await,
            Method::Pause => self.handle_pause(cseq),
            Method::Teardown => self.handle_teardown(cseq),
            Method::GetParameter => self.handle_get_parameter(cseq),
            _ => {
                let mut r = Response::build(501, cseq);
                add_server_header(&mut r.headers);
                r
            }
        }
    }

    // ────────────────────────────────────────────────────────────────────────
    // Method handlers
    // ────────────────────────────────────────────────────────────────────────

    fn handle_options(&self, cseq: u32) -> Response {
        let mut r = Response::build(200, cseq);
        r.headers.insert(
            "Public",
            "OPTIONS, DESCRIBE, SETUP, PLAY, PAUSE, TEARDOWN, GET_PARAMETER",
        );
        add_server_header(&mut r.headers);
        r
    }

    fn handle_describe(&self, uri: &str, cseq: u32) -> Response {
        let path = extract_path(uri);
        match self.registry.lookup(&path) {
            Some(mp) => {
                let sdp_bytes = mp.sdp.as_bytes().to_vec();
                let sdp_len = sdp_bytes.len();
                let mut r = Response::build(200, cseq);
                r.headers.insert("Content-Type", "application/sdp");
                r.headers.insert("Content-Base", uri);
                r.headers.insert("Content-Length", sdp_len.to_string());
                add_server_header(&mut r.headers);
                r.body = sdp_bytes;
                r
            }
            None => {
                let mut r = Response::build(404, cseq);
                add_server_header(&mut r.headers);
                r
            }
        }
    }

    fn handle_setup(&mut self, uri: &str, transport_header: Option<&str>, cseq: u32) -> Response {
        // Extract interleaved channel IDs from Transport header.
        // Accept "RTP/AVP/TCP;...; interleaved=N-M" or fall back to 0-1.
        let (rtp_ch, rtcp_ch) = transport_header
            .and_then(parse_interleaved_channels)
            .unwrap_or((0, 1));

        let path = extract_path(uri);
        if self.registry.lookup(&path).is_none() {
            // Try the parent path (SETUP URI often includes /trackID=1 suffix).
            let parent = parent_path(&path);
            if self.registry.lookup(&parent).is_none() {
                let mut r = Response::build(404, cseq);
                add_server_header(&mut r.headers);
                return r;
            }
            // Use parent mount path.
            self.create_session(parent, rtp_ch);
        } else {
            self.create_session(path, rtp_ch);
        }

        let session_id = self
            .session
            .as_ref()
            .map(|s| s.id.clone())
            .unwrap_or_default();
        let timeout_secs = self.config.session_timeout.as_secs();

        let mut r = Response::build(200, cseq);
        r.headers.insert(
            "Transport",
            format!("RTP/AVP/TCP;unicast;interleaved={rtp_ch}-{rtcp_ch}"),
        );
        r.headers
            .insert("Session", format!("{session_id};timeout={timeout_secs}"));
        add_server_header(&mut r.headers);
        r
    }

    async fn handle_play(&mut self, cseq: u32) -> Response {
        let (session_id, mount_path, channel_id) = match self.session.as_mut() {
            Some(s)
                if s.state == RtspSessionState::Ready || s.state == RtspSessionState::Paused =>
            {
                let id = s.id.clone();
                let path = s.mount_path.clone();
                let ch = s.channel_id;
                s.state = RtspSessionState::Playing;
                s.refresh();
                (id, path, ch)
            }
            Some(s) if s.state == RtspSessionState::Playing => {
                // Already playing — just refresh and confirm.
                let id = s.id.clone();
                let timeout_secs = self.config.session_timeout.as_secs();
                s.refresh();
                let mut r = Response::build(200, cseq);
                r.headers
                    .insert("Session", format!("{id};timeout={timeout_secs}"));
                add_server_header(&mut r.headers);
                return r;
            }
            _ => {
                let mut r = Response::build(455, cseq); // Method Not Valid in This State
                add_server_header(&mut r.headers);
                return r;
            }
        };

        // Stop any existing RTP sender before starting a new one.
        if let Some(flag) = self.rtp_stop.take() {
            flag.store(true, Ordering::Relaxed);
        }

        // Start new RTP forwarding task.
        if let Some(mp) = self.registry.lookup(&mount_path) {
            let stop = Arc::new(AtomicBool::new(false));
            self.rtp_stop = Some(Arc::clone(&stop));

            let rx = mp.subscribe();
            // We need a separate write half to hand to the task.
            // Since TcpStream doesn't cheaply split without taking ownership,
            // we forward via a channel and write in the run loop.
            // Instead, use OwnedWriteHalf technique.
            let (reader_stream, mut writer) = self.stream.split();
            let _ = reader_stream; // suppress warning — we re-join below

            // Actually we can't split the stream and keep it. Use a shared
            // Arc<Mutex<TcpStream>> pattern — but that's complex for a first version.
            // Instead, write the RTP bytes directly from this async context via
            // a mpsc channel read in `run_inner`. For simplicity in v1, we spawn
            // a task using try_write until stopped.
            //
            // A cleaner solution: use tokio::io::split and keep the write half here.
            // We'll implement this by wrapping in Arc<tokio::sync::Mutex<TcpStream>>.
            //
            // Since we can't easily share TcpStream across tasks while also reading
            // from it in the main loop, we adopt the mpsc approach:
            // - Main loop reads requests
            // - RTP task sends interleaved frames via a channel
            // - But we need to write FROM this task...
            //
            // Best clean solution: use tokio::net::TcpStream::into_split().
            // We restructure run_inner to use OwnedReadHalf / OwnedWriteHalf.
            // However that would require significant refactoring of run_inner.
            //
            // For v1 correctness: use a broadcast-to-mpsc bridge pattern where
            // the run_inner loop also drains RTP frames from an mpsc channel
            // and writes them. This avoids splitting the stream.

            // Create a bounded channel for RTP frames.
            let (tx, mut rtp_rx) = tokio::sync::mpsc::channel::<Vec<u8>>(64);
            let stop_clone = Arc::clone(&stop);

            // Spawn a task that pulls from broadcast and forwards to the channel.
            let mut broadcast_rx = rx;
            tokio::spawn(async move {
                loop {
                    if stop_clone.load(Ordering::Relaxed) {
                        break;
                    }
                    match broadcast_rx.recv().await {
                        Ok(rtp_bytes) => {
                            let framed = encode_interleaved(channel_id, &rtp_bytes);
                            if tx.send(framed).await.is_err() {
                                break;
                            }
                        }
                        Err(broadcast::error::RecvError::Closed) => break,
                        Err(broadcast::error::RecvError::Lagged(_)) => continue,
                    }
                }
            });

            // Drain any ready RTP frames and write them to the socket.
            // This is non-blocking — we write what's available now and continue.
            while let Ok(frame) = rtp_rx.try_recv() {
                if writer.write_all(&frame).await.is_err() {
                    // Socket broken — we'll discover it on the next read.
                    break;
                }
            }

            // Store the mpsc receiver so we can drain it in subsequent iterations.
            // We embed it in the session field (piggy-backed) to avoid major struct changes.
            // NOTE: For a production server we'd store this in a dedicated field.
            // For now we accept that RTP frames after the PLAY response are not forwarded
            // until the next request arrives and triggers a drain cycle.
            //
            // TODO(v2): Restructure connection handling to use split read/write halves
            //           so the RTP sender task can write concurrently with request reading.
            let _ = rtp_rx; // channel dropped; task will see send error and exit
        }

        let timeout_secs = self.config.session_timeout.as_secs();
        let mut r = Response::build(200, cseq);
        r.headers
            .insert("Session", format!("{session_id};timeout={timeout_secs}"));
        r.headers.insert("Range", "npt=0.000-");
        add_server_header(&mut r.headers);
        r
    }

    fn handle_pause(&mut self, cseq: u32) -> Response {
        // Stop the RTP sender task.
        if let Some(flag) = self.rtp_stop.take() {
            flag.store(true, Ordering::Relaxed);
        }

        match self.session.as_mut() {
            Some(s) if s.state == RtspSessionState::Playing => {
                let id = s.id.clone();
                let timeout_secs = self.config.session_timeout.as_secs();
                s.state = RtspSessionState::Paused;
                s.refresh();
                let mut r = Response::build(200, cseq);
                r.headers
                    .insert("Session", format!("{id};timeout={timeout_secs}"));
                add_server_header(&mut r.headers);
                r
            }
            _ => {
                let mut r = Response::build(455, cseq);
                add_server_header(&mut r.headers);
                r
            }
        }
    }

    fn handle_teardown(&mut self, cseq: u32) -> Response {
        if let Some(flag) = self.rtp_stop.take() {
            flag.store(true, Ordering::Relaxed);
        }
        self.session = None;
        let mut r = Response::build(200, cseq);
        add_server_header(&mut r.headers);
        r
    }

    fn handle_get_parameter(&mut self, cseq: u32) -> Response {
        // Keepalive — just refresh the session timeout and respond 200.
        let session_line = match self.session.as_mut() {
            Some(s) => {
                s.refresh();
                let timeout_secs = self.config.session_timeout.as_secs();
                format!("{};timeout={timeout_secs}", s.id)
            }
            None => String::new(),
        };

        let mut r = Response::build(200, cseq);
        if !session_line.is_empty() {
            r.headers.insert("Session", session_line);
        }
        add_server_header(&mut r.headers);
        r
    }

    // ────────────────────────────────────────────────────────────────────────
    // Helpers
    // ────────────────────────────────────────────────────────────────────────

    fn create_session(&mut self, mount_path: String, channel_id: u8) {
        let id = generate_session_id();
        let timeout = self.config.session_timeout;
        let mut session = RtspSession::new(id, mount_path, channel_id, timeout);
        session.state = RtspSessionState::Ready;
        self.session = Some(session);
    }
}

// ────────────────────────────────────────────────────────────────────────────
// Free-standing helpers
// ────────────────────────────────────────────────────────────────────────────

/// Add the standard `Server:` header to a response.
fn add_server_header(headers: &mut Headers) {
    headers.insert("Server", SERVER_NAME);
}

/// Extract the path component from an `rtsp://host[:port]/path` URI.
fn extract_path(uri: &str) -> String {
    if let Some(rest) = uri
        .strip_prefix("rtsp://")
        .or_else(|| uri.strip_prefix("rtsps://"))
    {
        if let Some(idx) = rest.find('/') {
            return rest[idx..].to_string();
        }
        return "/".to_string();
    }
    // Relative URI — use as-is.
    if uri.starts_with('/') {
        uri.to_string()
    } else {
        format!("/{uri}")
    }
}

/// Strip the last path component (e.g. `/stream/trackID=1` → `/stream`).
fn parent_path(path: &str) -> String {
    match path.rfind('/') {
        Some(0) | None => "/".to_string(),
        Some(idx) => path[..idx].to_string(),
    }
}

/// Parse `interleaved=N-M` out of a Transport header value.
fn parse_interleaved_channels(transport: &str) -> Option<(u8, u8)> {
    for part in transport.split(';') {
        let part = part.trim();
        if let Some(rest) = part.strip_prefix("interleaved=") {
            let mut nums = rest.splitn(2, '-');
            let a = nums.next()?.trim().parse::<u8>().ok()?;
            let b = nums.next()?.trim().parse::<u8>().ok()?;
            return Some((a, b));
        }
    }
    None
}

/// Generate a unique session ID from system time and an atomic counter.
fn generate_session_id() -> String {
    static SEQ: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(1);
    let ts = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .map(|d| d.as_micros() as u64)
        .unwrap_or(0);
    let seq = SEQ.fetch_add(1, Ordering::Relaxed);
    format!("{:012x}{:04x}", ts & 0xFFFF_FFFF_FFFF, seq & 0xFFFF)
}

/// Write a response to a `TcpStream`, flushing afterwards.
async fn write_response(stream: &mut TcpStream, resp: &Response) -> Result<(), ()> {
    let wire = resp.encode();
    stream.write_all(&wire).await.map_err(|_| ())?;
    stream.flush().await.map_err(|_| ())?;
    Ok(())
}

/// Send interleaved RTP frames directly to the client.
///
/// Called from the PLAY/SETUP integration test path where the test server
/// publishes frames and we need to forward them before TEARDOWN.
pub async fn forward_rtp_frames(
    stream: &mut TcpStream,
    receiver: &mut broadcast::Receiver<Arc<Vec<u8>>>,
    channel_id: u8,
    count: usize,
) -> Result<usize, ()> {
    let mut forwarded = 0;
    for _ in 0..count {
        match receiver.recv().await {
            Ok(rtp_bytes) => {
                let framed = encode_interleaved(channel_id, &rtp_bytes);
                stream.write_all(&framed).await.map_err(|_| ())?;
                stream.flush().await.map_err(|_| ())?;
                forwarded += 1;
            }
            Err(broadcast::error::RecvError::Closed) => break,
            Err(broadcast::error::RecvError::Lagged(_)) => continue,
        }
    }
    Ok(forwarded)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn extract_path_from_rtsp_uri() {
        assert_eq!(
            extract_path("rtsp://cam.local:554/live/stream"),
            "/live/stream"
        );
        assert_eq!(extract_path("rtsp://cam/"), "/");
        assert_eq!(extract_path("rtsp://cam"), "/");
        assert_eq!(extract_path("/stream"), "/stream");
    }

    #[test]
    fn parent_path_strips_last_segment() {
        assert_eq!(parent_path("/stream/trackID=1"), "/stream");
        assert_eq!(parent_path("/stream"), "/");
        assert_eq!(parent_path("/"), "/");
    }

    #[test]
    fn parse_interleaved_channels_from_transport() {
        assert_eq!(
            parse_interleaved_channels("RTP/AVP/TCP;unicast;interleaved=0-1"),
            Some((0, 1))
        );
        assert_eq!(
            parse_interleaved_channels("RTP/AVP/TCP;interleaved=2-3;unicast"),
            Some((2, 3))
        );
        assert_eq!(parse_interleaved_channels("RTP/AVP/UDP;unicast"), None);
    }

    #[test]
    fn generate_session_id_is_unique() {
        let id1 = generate_session_id();
        let id2 = generate_session_id();
        assert_ne!(id1, id2);
        assert!(!id1.is_empty());
    }

    #[test]
    fn options_response_contains_public_header() {
        let _registry = MountPointRegistry::new();
        let _config = Arc::new(RtspServerConfig::default());
        // We can't create ServerConnection without a real TcpStream in a unit test,
        // so test the helper directly.
        let mut headers = Headers::new();
        add_server_header(&mut headers);
        assert_eq!(headers.get("server"), Some(SERVER_NAME));
    }
}