use crate::{Chunk, Error, Scalar, StreamInfo};
use std::time::Duration;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct Post(u32);
impl Post {
pub const NONE: Post = Post(0);
pub const CLOCK_SYNC: Post = Post(1);
pub const DEJITTER: Post = Post(2);
pub const MONOTONIZE: Post = Post(4);
pub const ALL: Post = Post(1 | 2 | 4);
pub fn bits(self) -> u32 {
self.0
}
}
impl std::ops::BitOr for Post {
type Output = Post;
fn bitor(self, rhs: Post) -> Post {
Post(self.0 | rhs.0)
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum Buffer {
Seconds(f64),
Samples(usize),
}
impl Buffer {
pub const DEFAULT: Buffer = Buffer::Seconds(360.0);
pub fn samples(self, rate: f64) -> usize {
match self {
Buffer::Samples(n) => n.max(1),
Buffer::Seconds(s) if rate > 0.0 => ((s * rate).ceil() as usize).max(1),
Buffer::Seconds(_) => 360,
}
}
}
pub struct Inlet {
inner: labstream_net::Inlet,
info: StreamInfo,
scratch: Vec<labstream_wire::Sample>,
}
pub struct Builder<'a> {
info: &'a StreamInfo,
buffer: Buffer,
recover: bool,
post: Post,
}
impl Inlet {
pub fn builder(info: &StreamInfo) -> Builder<'_> {
Builder {
info,
buffer: Buffer::DEFAULT,
recover: true,
post: Post::NONE,
}
}
pub fn info(&self) -> &StreamInfo {
&self.info
}
pub fn pull<T: Scalar>(&mut self, timeout: Duration) -> Result<Option<(f64, Vec<T>)>, Error> {
match self.inner.pull(timeout)? {
Some(s) => Ok(Some((
s.timestamp,
s.values.iter().map(T::from_value).collect(),
))),
None => Ok(None),
}
}
pub fn pull_text(&mut self, timeout: Duration) -> Result<Option<(f64, Vec<String>)>, Error> {
match self.inner.pull(timeout)? {
Some(s) => Ok(Some((
s.timestamp,
s.values.iter().map(crate::as_text).collect(),
))),
None => Ok(None),
}
}
pub fn pull_chunk<T: Scalar>(
&mut self,
chunk: &mut Chunk<T>,
timeout: Duration,
) -> Result<usize, Error> {
let room = chunk.capacity_samples() - chunk.len();
if room == 0 {
return Ok(0);
}
self.scratch.clear();
let got = self.inner.pull_chunk(&mut self.scratch, room, timeout)?;
for s in &self.scratch {
chunk.push(s.timestamp, s.values.iter().map(T::from_value));
}
Ok(got)
}
pub fn time_correction(&self) -> Option<f64> {
self.inner.time_correction()
}
pub fn wait_for_time_correction(&self, timeout: Duration) -> Option<f64> {
self.inner.wait_for_time_correction(timeout)
}
pub fn available(&self) -> usize {
self.inner.samples_available()
}
pub fn dropped(&self) -> u64 {
self.inner.samples_dropped()
}
pub fn is_finished(&self) -> bool {
self.inner.is_finished()
}
}
impl<'a> Builder<'a> {
pub fn buffer(mut self, buffer: Buffer) -> Builder<'a> {
self.buffer = buffer;
self
}
pub fn recover(mut self, recover: bool) -> Builder<'a> {
self.recover = recover;
self
}
pub fn postprocess(mut self, post: Post) -> Builder<'a> {
self.post = post;
self
}
pub fn open(self, timeout: Duration) -> Result<Inlet, Error> {
let samples = self.buffer.samples(self.info.rate());
let buffered = samples.min(i32::MAX as usize) as i32;
let mut inner = if self.recover {
labstream_net::Inlet::open_recovering(&self.info.inner, timeout, buffered)?
} else {
labstream_net::Inlet::open_with_buffer(&self.info.inner, timeout, buffered)?
};
inner.set_postprocessing(self.post.bits());
let info = StreamInfo {
inner: inner.info().clone(),
};
Ok(Inlet {
inner,
info,
scratch: Vec::new(),
})
}
}