1use serde::{Deserialize, Serialize};
3use std::fmt;
4
5#[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
7pub struct StreamId(String);
8
9impl StreamId {
10 pub fn new(s: &str) -> Self {
12 StreamId(s.to_string())
13 }
14 pub fn as_str(&self) -> &str {
16 &self.0
17 }
18}
19
20impl fmt::Display for StreamId {
21 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
22 f.write_str(&self.0)
23 }
24}
25
26#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
28pub struct CodecId(pub String);
29
30pub const PCM_F32LE: &str = "pcm/f32le";
32pub const PCM_S16LE: &str = "pcm/s16le";
34
35#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
37pub enum StreamKind {
38 Live,
40 Recorded {
42 duration_ticks: u64,
44 },
45}
46
47#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
49pub struct StreamInfo {
50 pub id: StreamId,
52 pub name: String,
54 pub kind: StreamKind,
56 pub codec: CodecId,
58 pub params: Vec<(String, String)>,
60 pub clock_rate: u32,
62 pub meta: Option<Vec<u8>>,
64}
65
66impl StreamInfo {
67 pub fn pcm_f32(id: &str, name: &str, sample_rate: u32, channels: u16) -> Self {
69 StreamInfo {
70 id: StreamId::new(id),
71 name: name.to_string(),
72 kind: StreamKind::Live,
73 codec: CodecId(PCM_F32LE.into()),
74 params: vec![("sample_rate".into(), sample_rate.to_string()), ("channels".into(), channels.to_string())],
75 clock_rate: sample_rate,
76 meta: None,
77 }
78 }
79
80 pub fn with_meta(mut self, meta: Vec<u8>) -> Self {
82 self.meta = Some(meta);
83 self
84 }
85
86 pub fn param(&self, key: &str) -> Option<&str> {
88 self.params.iter().find(|(k, _)| k == key).map(|(_, v)| v.as_str())
89 }
90}
91
92#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
94pub struct Frame {
95 pub seq: u64,
97 pub pts: u64,
99 pub duration: u32,
101 pub key: bool,
103 pub payload: Vec<u8>,
105 pub meta: Option<Vec<u8>>,
107}
108
109impl Frame {
110 pub fn new(seq: u64, pts: u64, duration: u32, payload: Vec<u8>) -> Self {
112 Frame { seq, pts, duration, key: true, payload, meta: None }
113 }
114
115 pub fn pcm_f32(seq: u64, pts: u64, channels: u16, samples: &[f32]) -> Self {
117 let payload = samples.iter().flat_map(|s| s.to_le_bytes()).collect();
118 Frame::new(seq, pts, (samples.len() / channels.max(1) as usize) as u32, payload)
119 }
120
121 pub fn f32_samples(&self) -> Vec<f32> {
123 self.payload.as_chunks::<4>().0.iter().map(|c| f32::from_le_bytes(*c)).collect()
124 }
125}
126
127#[derive(Clone, Debug, PartialEq, Eq)]
129pub enum StreamEvent {
130 Frame(Frame),
132 Gap {
134 missed: u64,
136 },
137 InfoChanged(StreamInfo),
139 Ended,
141}
142
143#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
145pub enum Overflow {
146 DropOldest,
148 SkipToKey,
150 Disconnect,
152}
153
154#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
156pub struct BufferPolicy {
157 pub max_frames: usize,
159 pub overflow: Overflow,
161}
162
163impl Default for BufferPolicy {
164 fn default() -> Self {
165 BufferPolicy { max_frames: 256, overflow: Overflow::DropOldest }
166 }
167}
168
169#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
171pub enum Start {
172 Live,
174 At(u64),
176 FromStart,
178}
179
180#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
182pub struct SubscribeOptions {
183 pub start: Start,
185 pub buffer: BufferPolicy,
187}
188
189impl Default for SubscribeOptions {
190 fn default() -> Self {
191 SubscribeOptions { start: Start::Live, buffer: BufferPolicy::default() }
192 }
193}
194
195#[derive(Clone, Debug, PartialEq, Eq, thiserror::Error)]
197pub enum StreamError {
198 #[error("the stream has ended")]
200 Ended,
201 #[error("refused: {0}")]
203 Refused(String),
204 #[error("unknown stream")]
206 UnknownStream,
207 #[error("disconnected: the listener fell too far behind")]
209 Overrun,
210 #[error("network: {0}")]
212 Net(String),
213 #[error("protocol: {0}")]
215 Protocol(String),
216}