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 const DRIFT: Post = Post(16);
pub const DEVICE_DELAY: Post = Post(32);
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::PackedSample>,
}
pub struct Builder<'a> {
info: &'a StreamInfo,
buffer: Buffer,
recover: bool,
post: Post,
channels: Vec<u16>,
decimate: u32,
target_rate: Option<f64>,
}
impl Inlet {
pub fn builder(info: &StreamInfo) -> Builder<'_> {
Builder {
info,
buffer: Buffer::DEFAULT,
recover: true,
post: Post::NONE,
channels: Vec::new(),
decimate: 1,
target_rate: None,
}
}
pub fn info(&self) -> &StreamInfo {
&self.info
}
pub fn selection(&self) -> Option<labstream_net::SelectParam> {
self.inner.selection()
}
pub fn pull<T: Scalar>(&mut self, timeout: Duration) -> Result<Option<(f64, Vec<T>)>, Error> {
match self.inner.pull_packed(timeout)? {
Some(s) => {
let mut values = Vec::with_capacity(s.channel_count());
T::unpack_into(&s, &mut values);
Ok(Some((s.timestamp, values)))
}
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_packed(&mut self.scratch, room, timeout)?;
for s in &self.scratch {
chunk.push_packed(s);
}
Ok(got)
}
pub fn pull_chunk_f32(
&mut self,
data: &mut Vec<f32>,
timestamps: &mut Vec<f64>,
max_samples: usize,
timeout: Duration,
) -> Result<usize, Error> {
Ok(self
.inner
.pull_chunk_f32(data, timestamps, max_samples, timeout)?)
}
pub fn pull_chunk_f64(
&mut self,
data: &mut Vec<f64>,
timestamps: &mut Vec<f64>,
max_samples: usize,
timeout: Duration,
) -> Result<usize, Error> {
Ok(self
.inner
.pull_chunk_f64(data, timestamps, max_samples, timeout)?)
}
pub fn pull_chunk_i32(
&mut self,
data: &mut Vec<i32>,
timestamps: &mut Vec<f64>,
max_samples: usize,
timeout: Duration,
) -> Result<usize, Error> {
Ok(self
.inner
.pull_chunk_i32(data, timestamps, max_samples, timeout)?)
}
pub fn pull_chunk_i16(
&mut self,
data: &mut Vec<i16>,
timestamps: &mut Vec<f64>,
max_samples: usize,
timeout: Duration,
) -> Result<usize, Error> {
Ok(self
.inner
.pull_chunk_i16(data, timestamps, max_samples, timeout)?)
}
pub fn pull_chunk_i8(
&mut self,
data: &mut Vec<i8>,
timestamps: &mut Vec<f64>,
max_samples: usize,
timeout: Duration,
) -> Result<usize, Error> {
Ok(self
.inner
.pull_chunk_i8(data, timestamps, max_samples, timeout)?)
}
pub fn pull_chunk_i64(
&mut self,
data: &mut Vec<i64>,
timestamps: &mut Vec<f64>,
max_samples: usize,
timeout: Duration,
) -> Result<usize, Error> {
Ok(self
.inner
.pull_chunk_i64(data, timestamps, max_samples, timeout)?)
}
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 lost(&self) -> Option<u64> {
self.inner.samples_lost()
}
pub fn resent(&self) -> Option<u64> {
self.inner.samples_resent()
}
pub fn time_uncertainty(&self) -> Option<f64> {
self.inner.time_uncertainty()
}
pub fn clock_drift(&self) -> Option<f64> {
self.inner.clock_drift()
}
pub fn smooth_time_correction(&self, remote_ts: f64) -> Option<f64> {
self.inner.smooth_time_correction(remote_ts)
}
pub fn set_device_delay(&mut self, delay_s: f64) {
self.inner.set_device_delay(delay_s);
}
pub fn device_delay(&self) -> f64 {
self.inner.device_delay()
}
pub fn recoveries(&self) -> u64 {
self.inner.recoveries()
}
pub fn on_udp(&self) -> bool {
self.inner.on_udp()
}
pub fn on_shm(&self) -> bool {
self.inner.on_shm()
}
pub fn is_compressed(&self) -> bool {
self.inner.is_compressed()
}
pub fn is_encrypted(&self) -> bool {
self.inner.is_encrypted()
}
pub fn compression_ratio(&self) -> Option<f64> {
self.inner.compression_ratio()
}
pub fn has_live_metadata(&self) -> bool {
self.inner.has_live_metadata()
}
pub fn pull_metadata(&self) -> Vec<crate::MetadataUpdate> {
self.inner.pull_metadata()
}
pub fn metadata(&self) -> std::collections::BTreeMap<String, crate::MetadataUpdate> {
self.inner.metadata()
}
pub fn health(&self) -> crate::InletHealth {
self.inner.health()
}
pub fn gaps(&self) -> Vec<crate::Gap> {
self.inner.gaps()
}
pub fn take_gaps(&self) -> Vec<crate::Gap> {
self.inner.take_gaps()
}
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 channels<I>(mut self, indices: I) -> Builder<'a>
where
I: IntoIterator<Item = usize>,
{
self.channels = indices
.into_iter()
.filter_map(|i| u16::try_from(i).ok())
.collect();
self
}
pub fn decimate(mut self, factor: u32) -> Builder<'a> {
self.decimate = factor.max(1);
self.target_rate = None;
self
}
pub fn rate(mut self, target_hz: f64) -> Builder<'a> {
if target_hz.is_finite() && target_hz > 0.0 {
self.target_rate = Some(target_hz);
}
self
}
pub fn open(self, timeout: Duration) -> Result<Inlet, Error> {
let decimate = match self.target_rate {
Some(target) if self.info.rate() > 0.0 => {
(self.info.rate() / target).round().max(1.0) as u32
}
_ => self.decimate.max(1),
};
let sel = labstream_net::SelectParam {
decimate,
channels: self.channels,
};
let effective_rate = sel.output_srate(self.info.rate());
let samples = self.buffer.samples(effective_rate);
let buffered = samples.min(i32::MAX as usize) as i32;
let mut inner = if sel.is_active() {
labstream_net::Inlet::open_selected(
&self.info.inner,
timeout,
buffered,
self.recover,
sel,
)?
} else 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(),
})
}
}