Skip to main content

snapcast_server/stream/
mod.rs

1//! Stream management — encoding and distribution.
2
3pub mod manager;
4
5use snapcast_proto::SampleFormat;
6use tokio::sync::mpsc;
7use tokio::task::JoinHandle;
8
9/// A timestamped PCM chunk produced by a stream reader.
10#[derive(Debug, Clone)]
11pub struct PcmChunk {
12    /// Server timestamp in microseconds when this chunk was captured.
13    pub timestamp_usec: i64,
14    /// Raw PCM audio data.
15    pub data: Vec<u8>,
16}
17
18/// A running stream reader that produces PCM chunks.
19pub struct StreamReader {
20    /// Stream name.
21    pub name: String,
22    /// Sample format.
23    pub format: SampleFormat,
24    /// Receiver for PCM chunks.
25    pub rx: mpsc::Receiver<PcmChunk>,
26    /// Task handle for the reader.
27    handle: JoinHandle<()>,
28}
29
30impl StreamReader {
31    /// Create a StreamReader from components.
32    pub fn new(
33        name: String,
34        format: SampleFormat,
35        rx: mpsc::Receiver<PcmChunk>,
36        handle: JoinHandle<()>,
37    ) -> Self {
38        Self {
39            name,
40            format,
41            rx,
42            handle,
43        }
44    }
45
46    /// Stop the reader.
47    pub fn stop(&self) {
48        self.handle.abort();
49    }
50}
51
52impl Drop for StreamReader {
53    fn drop(&mut self) {
54        self.handle.abort();
55    }
56}