use serde::{Deserialize, Serialize};
use std::fmt;
#[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
pub struct StreamId(String);
impl StreamId {
pub fn new(s: &str) -> Self {
StreamId(s.to_string())
}
pub fn as_str(&self) -> &str {
&self.0
}
}
impl fmt::Display for StreamId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.0)
}
}
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct CodecId(pub String);
pub const PCM_F32LE: &str = "pcm/f32le";
pub const PCM_S16LE: &str = "pcm/s16le";
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub enum StreamKind {
Live,
Recorded {
duration_ticks: u64,
},
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct StreamInfo {
pub id: StreamId,
pub name: String,
pub kind: StreamKind,
pub codec: CodecId,
pub params: Vec<(String, String)>,
pub clock_rate: u32,
pub meta: Option<Vec<u8>>,
}
impl StreamInfo {
pub fn pcm_f32(id: &str, name: &str, sample_rate: u32, channels: u16) -> Self {
StreamInfo {
id: StreamId::new(id),
name: name.to_string(),
kind: StreamKind::Live,
codec: CodecId(PCM_F32LE.into()),
params: vec![("sample_rate".into(), sample_rate.to_string()), ("channels".into(), channels.to_string())],
clock_rate: sample_rate,
meta: None,
}
}
pub fn with_meta(mut self, meta: Vec<u8>) -> Self {
self.meta = Some(meta);
self
}
pub fn param(&self, key: &str) -> Option<&str> {
self.params.iter().find(|(k, _)| k == key).map(|(_, v)| v.as_str())
}
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct Frame {
pub seq: u64,
pub pts: u64,
pub duration: u32,
pub key: bool,
pub payload: Vec<u8>,
pub meta: Option<Vec<u8>>,
}
impl Frame {
pub fn new(seq: u64, pts: u64, duration: u32, payload: Vec<u8>) -> Self {
Frame { seq, pts, duration, key: true, payload, meta: None }
}
pub fn pcm_f32(seq: u64, pts: u64, channels: u16, samples: &[f32]) -> Self {
let payload = samples.iter().flat_map(|s| s.to_le_bytes()).collect();
Frame::new(seq, pts, (samples.len() / channels.max(1) as usize) as u32, payload)
}
pub fn f32_samples(&self) -> Vec<f32> {
self.payload.as_chunks::<4>().0.iter().map(|c| f32::from_le_bytes(*c)).collect()
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum StreamEvent {
Frame(Frame),
Gap {
missed: u64,
},
InfoChanged(StreamInfo),
Ended,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub enum Overflow {
DropOldest,
SkipToKey,
Disconnect,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct BufferPolicy {
pub max_frames: usize,
pub overflow: Overflow,
}
impl Default for BufferPolicy {
fn default() -> Self {
BufferPolicy { max_frames: 256, overflow: Overflow::DropOldest }
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub enum Start {
Live,
At(u64),
FromStart,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct SubscribeOptions {
pub start: Start,
pub buffer: BufferPolicy,
}
impl Default for SubscribeOptions {
fn default() -> Self {
SubscribeOptions { start: Start::Live, buffer: BufferPolicy::default() }
}
}
#[derive(Clone, Debug, PartialEq, Eq, thiserror::Error)]
pub enum StreamError {
#[error("the stream has ended")]
Ended,
#[error("refused: {0}")]
Refused(String),
#[error("unknown stream")]
UnknownStream,
#[error("disconnected: the listener fell too far behind")]
Overrun,
#[error("network: {0}")]
Net(String),
#[error("protocol: {0}")]
Protocol(String),
}