rtsp-runtime 0.2.0

Sans-IO RTSP 1.0 (RFC 2326) session engine — driveable client + server state machines, interleaved RTP/RTCP framing, Basic/Digest auth, over the rtsp-types + sdp-types codecs; optional tokio (+ rustls) socket adapter.
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
//! Async real-socket IO adapter over the sans-IO engine — RFC 2326 transport.
//!
//! The sans-IO [`ClientSession`] / [`ServerSession`] engines never touch a
//! socket: they turn method calls into request/response *bytes* and consume
//! inbound *bytes* into typed events. This module is the thin layer that
//! actually moves those bytes over a [`tokio`] socket — it owns the stream,
//! writes what the session produces, reads the peer's reply (buffering partial
//! reads until a full RTSP message or interleaved `$`-frame parses, per §10.12),
//! feeds it back through [`ClientSession::handle_data`] /
//! [`ServerSession::handle_request`], and returns the resulting events. The
//! engine stays pure; the adapter is pure plumbing.
//!
//! Both the client and server are generic over the stream type
//! (`S: AsyncRead + AsyncWrite + Unpin`), so the identical driver logic runs over
//! a plain [`tokio::net::TcpStream`] and over a TLS stream.
//!
//! # `rtsp://` vs `rtsps://`
//!
//! Plain RTSP (`rtsp://`) is carried over TCP on default port **554**
//! ([`RTSP_DEFAULT_PORT`]). RTSP-over-TLS (`rtsps://`) wraps the TCP stream in a
//! TLS session *before* any RTSP is exchanged and uses default port **322**
//! ([`RTSPS_DEFAULT_PORT`], per the IANA `rtsps` assignment). The TLS entry
//! points ([`AsyncRtspClient::connect_tls`], [`AsyncRtspServer::accept_tls`]) are
//! gated behind the `tls` feature; everything else is behind `tokio`.

use tokio::io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt};
use tokio::net::TcpStream;

use rtsp_types::Message;

use crate::client::{ClientEvent, ClientSession};
use crate::error::{Error, Result};
use crate::interleaved::MAGIC;
use crate::server::{ServerEvent, ServerSession};
use crate::transport::Transport;

/// A message body type: owned bytes.
type Body = Vec<u8>;

/// Default TCP port for `rtsp://` (RFC 2326 §1 / IANA `rtsp`).
pub const RTSP_DEFAULT_PORT: u16 = 554;

/// Default TCP port for `rtsps://` (RTSP over TLS; IANA `rtsps`).
pub const RTSPS_DEFAULT_PORT: u16 = 322;

/// Size of one socket read chunk. Reads accumulate into an internal buffer, so
/// this only bounds a single `read` syscall, not a message.
const READ_CHUNK: usize = 8192;

/// Maps a tokio IO error into the crate error type.
fn io_err(context: &str, e: std::io::Error) -> Error {
    Error::Io(format!("{context}: {e}"))
}

// ===========================================================================
// Client
// ===========================================================================

/// An async RTSP client that owns a socket and drives a [`ClientSession`].
///
/// Each request method (`options`/`describe`/`setup`/`play`/`pause`/`teardown`)
/// writes the request bytes the session produces, reads the response off the
/// socket, feeds it through the sans-IO engine, and returns the resulting
/// [`ClientEvent`]s. A Digest `401` is answered transparently: when the engine
/// emits [`ClientEvent::AuthRetry`], the adapter writes the retried request and
/// reads its response before returning, so the caller only sees the final
/// [`ClientEvent::Response`].
///
/// Interleaved media (`$`-framed RTP/RTCP, §10.12) is pulled with
/// [`recv_interleaved`](Self::recv_interleaved).
#[derive(Debug)]
pub struct AsyncRtspClient<S> {
    stream: S,
    session: ClientSession,
    /// Bytes read from the socket but not yet fully parsed (partial message or
    /// `$`-frame tail), plus any already-decoded events not yet drained.
    read_buf: Vec<u8>,
    /// Media events surfaced while awaiting a response (e.g. interleaved frames
    /// arriving between control messages), buffered for `recv_interleaved`.
    pending_media: std::collections::VecDeque<ClientEvent>,
}

impl AsyncRtspClient<TcpStream> {
    /// Connects a plain-TCP (`rtsp://`) client to `addr`.
    ///
    /// `addr` is any [`tokio::net::ToSocketAddrs`]; for the RTSP default port use
    /// `(host, RTSP_DEFAULT_PORT)`.
    pub async fn connect<A: tokio::net::ToSocketAddrs>(addr: A) -> Result<Self> {
        let stream = TcpStream::connect(addr)
            .await
            .map_err(|e| io_err("connect", e))?;
        Ok(Self::with_stream(stream, ClientSession::new()))
    }

    /// Connects a plain-TCP client to `addr` using a pre-configured session
    /// (e.g. one carrying [`Credentials`](crate::Credentials)).
    pub async fn connect_with<A: tokio::net::ToSocketAddrs>(
        addr: A,
        session: ClientSession,
    ) -> Result<Self> {
        let stream = TcpStream::connect(addr)
            .await
            .map_err(|e| io_err("connect", e))?;
        Ok(Self::with_stream(stream, session))
    }
}

impl<S> AsyncRtspClient<S>
where
    S: AsyncRead + AsyncWrite + Unpin,
{
    /// Wraps an already-connected stream (plain or TLS) and a session.
    pub fn with_stream(stream: S, session: ClientSession) -> Self {
        AsyncRtspClient {
            stream,
            session,
            read_buf: Vec::new(),
            pending_media: std::collections::VecDeque::new(),
        }
    }

    /// The current session state.
    pub fn state(&self) -> crate::SessionState {
        self.session.state()
    }

    /// The negotiated session id, once a SETUP response has been processed.
    pub fn session_id(&self) -> Option<&str> {
        self.session.session_id()
    }

    /// Borrows the underlying sans-IO session (read-only inspection).
    pub fn session(&self) -> &ClientSession {
        &self.session
    }

    /// Sends `OPTIONS` and awaits the response.
    pub async fn options(&mut self, uri: &str) -> Result<ClientEvent> {
        let bytes = self.session.options(uri)?;
        self.exchange(bytes).await
    }

    /// Sends `DESCRIBE` (with `Accept: application/sdp`) and awaits the response.
    pub async fn describe(&mut self, uri: &str) -> Result<ClientEvent> {
        let bytes = self.session.describe(uri)?;
        self.exchange(bytes).await
    }

    /// Sends `SETUP` carrying `transport` and awaits the response.
    pub async fn setup(&mut self, uri: &str, transport: &Transport) -> Result<ClientEvent> {
        let bytes = self.session.setup(uri, transport)?;
        self.exchange(bytes).await
    }

    /// Sends `PLAY` and awaits the response.
    pub async fn play(&mut self, uri: &str) -> Result<ClientEvent> {
        let bytes = self.session.play(uri)?;
        self.exchange(bytes).await
    }

    /// Sends `PAUSE` and awaits the response.
    pub async fn pause(&mut self, uri: &str) -> Result<ClientEvent> {
        let bytes = self.session.pause(uri)?;
        self.exchange(bytes).await
    }

    /// Sends `TEARDOWN` and awaits the response.
    pub async fn teardown(&mut self, uri: &str) -> Result<ClientEvent> {
        let bytes = self.session.teardown(uri)?;
        self.exchange(bytes).await
    }

    /// Sends `GET_PARAMETER` (empty body = liveness ping) and awaits the response.
    pub async fn get_parameter(&mut self, uri: &str, body: &[u8]) -> Result<ClientEvent> {
        let bytes = self.session.get_parameter(uri, body)?;
        self.exchange(bytes).await
    }

    /// Writes an outbound request and reads until the correlated response
    /// arrives, transparently completing any Digest `AuthRetry` round-trip.
    ///
    /// Interleaved media frames that arrive before the response are buffered and
    /// later returned by [`recv_interleaved`](Self::recv_interleaved).
    async fn exchange(&mut self, request: Vec<u8>) -> Result<ClientEvent> {
        self.stream
            .write_all(&request)
            .await
            .map_err(|e| io_err("write request", e))?;
        self.stream.flush().await.map_err(|e| io_err("flush", e))?;

        loop {
            // Drain any events already decoded from buffered bytes first.
            let events = self
                .session
                .handle_data(&std::mem::take(&mut self.read_buf))?;
            let mut response = None;
            for event in events {
                match event {
                    // Hold the response until every event decoded from this same
                    // read has been processed: interleaved media frames can arrive
                    // coalesced *after* the response in one TCP segment, and must be
                    // buffered rather than dropped by an early return (§10.12).
                    ClientEvent::Response { .. } => response = Some(event),
                    ClientEvent::AuthRetry { ref request, .. } => {
                        // Write the retried (now-authenticated) request and keep
                        // reading for its response.
                        let retry = request.clone();
                        self.stream
                            .write_all(&retry)
                            .await
                            .map_err(|e| io_err("write auth retry", e))?;
                        self.stream.flush().await.map_err(|e| io_err("flush", e))?;
                    }
                    ClientEvent::MediaData { .. } => self.pending_media.push_back(event),
                }
            }
            if let Some(response) = response {
                return Ok(response);
            }
            // Need more bytes from the socket.
            self.fill_from_socket().await?;
        }
    }

    /// Receives the next interleaved media frame ([`ClientEvent::MediaData`]),
    /// driving the socket until one is available (§10.12).
    ///
    /// Returns `Ok(None)` if the peer closes the connection cleanly before a
    /// frame arrives. Any control responses interleaved with media are consumed
    /// and their state transitions applied, but not returned here.
    pub async fn recv_interleaved(&mut self) -> Result<Option<ClientEvent>> {
        loop {
            if let Some(event) = self.pending_media.pop_front() {
                return Ok(Some(event));
            }
            let events = self
                .session
                .handle_data(&std::mem::take(&mut self.read_buf))?;
            for event in events {
                if matches!(event, ClientEvent::MediaData { .. }) {
                    self.pending_media.push_back(event);
                }
            }
            if let Some(event) = self.pending_media.pop_front() {
                return Ok(Some(event));
            }
            // No frame decoded yet; read more, treating clean EOF as end-of-stream.
            let n = self.read_once().await?;
            if n == 0 {
                return Ok(None);
            }
        }
    }

    /// Reads one chunk from the socket into `read_buf`, erroring on EOF (used
    /// where a response is *required*).
    async fn fill_from_socket(&mut self) -> Result<()> {
        let n = self.read_once().await?;
        if n == 0 {
            return Err(Error::Io("peer closed connection before response".into()));
        }
        Ok(())
    }

    /// Reads one chunk from the socket into `read_buf`; returns the byte count
    /// (`0` = clean EOF).
    async fn read_once(&mut self) -> Result<usize> {
        let mut chunk = [0u8; READ_CHUNK];
        let n = self
            .stream
            .read(&mut chunk)
            .await
            .map_err(|e| io_err("read", e))?;
        self.read_buf.extend_from_slice(&chunk[..n]);
        Ok(n)
    }
}

#[cfg(feature = "tls")]
impl AsyncRtspClient<tokio_rustls::client::TlsStream<TcpStream>> {
    /// Connects an `rtsps://` (TLS) client to `addr`, verifying the server
    /// against the given `config` and presenting `server_name` for SNI/cert
    /// validation.
    ///
    /// For the public-CA default trust store, build `config` with
    /// [`default_tls_client_config`]. For a self-signed camera cert, build a
    /// [`rustls::ClientConfig`] whose root store contains that cert. For the
    /// `rtsps` default port use `(host, RTSPS_DEFAULT_PORT)`.
    pub async fn connect_tls<A: tokio::net::ToSocketAddrs>(
        addr: A,
        server_name: &str,
        config: rustls::ClientConfig,
    ) -> Result<Self> {
        use std::sync::Arc;
        use tokio_rustls::TlsConnector;

        let tcp = TcpStream::connect(addr)
            .await
            .map_err(|e| io_err("connect", e))?;
        let connector = TlsConnector::from(Arc::new(config));
        let dns = rustls::pki_types::ServerName::try_from(server_name.to_string())
            .map_err(|e| Error::Tls(format!("invalid server name {server_name:?}: {e}")))?;
        let stream = connector
            .connect(dns, tcp)
            .await
            .map_err(|e| io_err("TLS handshake", e))?;
        Ok(Self::with_stream(stream, ClientSession::new()))
    }
}

/// Builds a [`rustls::ClientConfig`] trusting the `webpki-roots` public-CA
/// bundle (the default trust store for `rtsps://` to a well-known server).
///
/// For a self-signed camera cert, construct the config directly with a root
/// store containing that cert and pass it to
/// [`AsyncRtspClient::connect_tls`].
#[cfg(feature = "tls")]
pub fn default_tls_client_config() -> rustls::ClientConfig {
    let mut roots = rustls::RootCertStore::empty();
    roots.extend(webpki_roots::TLS_SERVER_ROOTS.iter().cloned());
    rustls::ClientConfig::builder()
        .with_root_certificates(roots)
        .with_no_client_auth()
}

// ===========================================================================
// Server
// ===========================================================================

/// An async RTSP server connection that owns a socket and drives a
/// [`ServerSession`].
///
/// Reads requests off the socket (buffering partial reads until a full RTSP
/// message parses), calls [`ServerSession::handle_request`], writes the response
/// bytes back, and returns the [`ServerEvent`]s.
#[derive(Debug)]
pub struct AsyncRtspServer<S> {
    stream: S,
    session: ServerSession,
    read_buf: Vec<u8>,
}

impl AsyncRtspServer<TcpStream> {
    /// Wraps an accepted plain-TCP connection with a fresh [`ServerSession`].
    pub fn accept(stream: TcpStream) -> Self {
        Self::with_stream(stream, ServerSession::new())
    }

    /// Wraps an accepted plain-TCP connection with a pre-configured session.
    pub fn accept_with(stream: TcpStream, session: ServerSession) -> Self {
        Self::with_stream(stream, session)
    }
}

#[cfg(feature = "tls")]
impl AsyncRtspServer<tokio_rustls::server::TlsStream<TcpStream>> {
    /// Performs the TLS handshake over an accepted TCP connection (an
    /// `rtsps://` server), then wraps the TLS stream with a fresh session.
    pub async fn accept_tls(stream: TcpStream, config: rustls::ServerConfig) -> Result<Self> {
        use std::sync::Arc;
        use tokio_rustls::TlsAcceptor;

        let acceptor = TlsAcceptor::from(Arc::new(config));
        let tls = acceptor
            .accept(stream)
            .await
            .map_err(|e| io_err("TLS handshake", e))?;
        Ok(Self::with_stream(tls, ServerSession::new()))
    }
}

impl<S> AsyncRtspServer<S>
where
    S: AsyncRead + AsyncWrite + Unpin,
{
    /// Wraps an already-connected stream (plain or TLS) and a session.
    pub fn with_stream(stream: S, session: ServerSession) -> Self {
        AsyncRtspServer {
            stream,
            session,
            read_buf: Vec::new(),
        }
    }

    /// The current session state.
    pub fn state(&self) -> crate::SessionState {
        self.session.state()
    }

    /// The allocated session id, once a SETUP has been handled.
    pub fn session_id(&self) -> Option<&str> {
        self.session.session_id()
    }

    /// Mutable access to the underlying stream, for writing raw bytes (e.g.
    /// deliberately fragmenting an interleaved frame, or sending several frames
    /// back-to-back) alongside the framed [`send_interleaved`](Self::send_interleaved)
    /// helper.
    pub fn stream_mut(&mut self) -> &mut S {
        &mut self.stream
    }

    /// Reads the next complete request, handles it (writing the response back),
    /// and returns the produced events.
    ///
    /// Returns `Ok(None)` when the peer closes the connection cleanly before a
    /// full request arrives.
    pub async fn next_request(&mut self) -> Result<Option<Vec<ServerEvent>>> {
        loop {
            // Do we already hold a complete request in the buffer?
            if let Some(consumed) = complete_request_len(&self.read_buf)? {
                let request: Vec<u8> = self.read_buf.drain(..consumed).collect();
                let (response, events) = self.session.handle_request(&request)?;
                self.stream
                    .write_all(&response)
                    .await
                    .map_err(|e| io_err("write response", e))?;
                self.stream.flush().await.map_err(|e| io_err("flush", e))?;
                return Ok(Some(events));
            }
            // Need more bytes.
            let mut chunk = [0u8; READ_CHUNK];
            let n = self
                .stream
                .read(&mut chunk)
                .await
                .map_err(|e| io_err("read", e))?;
            if n == 0 {
                if self.read_buf.is_empty() {
                    return Ok(None);
                }
                return Err(Error::Io("peer closed connection mid-request".into()));
            }
            self.read_buf.extend_from_slice(&chunk[..n]);
        }
    }

    /// Sends an interleaved (`$`-framed) media frame to the client on `channel`
    /// (§10.12), e.g. an RTP or RTCP packet during PLAY.
    pub async fn send_interleaved(&mut self, channel: u8, payload: &[u8]) -> Result<()> {
        let frame = crate::interleaved::InterleavedFrame::new(channel, payload.to_vec());
        let bytes = frame.to_bytes()?;
        self.stream
            .write_all(&bytes)
            .await
            .map_err(|e| io_err("write interleaved frame", e))?;
        self.stream.flush().await.map_err(|e| io_err("flush", e))?;
        Ok(())
    }
}

/// Returns the byte length of a complete RTSP request at the front of `buf`, or
/// `None` if more bytes are needed. Errors on a malformed message.
///
/// A leading `$` (interleaved frame) is not a request; this returns an error so
/// the caller does not silently spin.
fn complete_request_len(buf: &[u8]) -> Result<Option<usize>> {
    if buf.is_empty() {
        return Ok(None);
    }
    if buf[0] == MAGIC {
        return Err(Error::MessageParse(
            "interleaved '$' frame received where a request was expected".into(),
        ));
    }
    match Message::<Body>::parse(buf) {
        Ok((_, consumed)) => Ok(Some(consumed)),
        Err(rtsp_types::ParseError::Incomplete(_)) => Ok(None),
        Err(rtsp_types::ParseError::Error) => {
            Err(Error::MessageParse("malformed RTSP request".into()))
        }
    }
}