use crate::Error;
use labstream_wire::Format;
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct Channel {
pub label: String,
pub unit: String,
pub kind: String,
}
impl Channel {
pub fn new(label: &str) -> Channel {
Channel {
label: label.to_string(),
..Default::default()
}
}
pub fn unit(mut self, unit: &str) -> Channel {
self.unit = unit.to_string();
self
}
pub fn kind(mut self, kind: &str) -> Channel {
self.kind = kind.to_string();
self
}
}
#[derive(Debug, Clone)]
pub struct StreamInfo {
pub(crate) inner: labstream_net::StreamInfo,
}
impl StreamInfo {
pub fn builder(name: &str, stream_type: &str, format: Format) -> Builder {
Builder {
name: name.to_string(),
stream_type: stream_type.to_string(),
format,
rate: 0.0,
source_id: String::new(),
channels: Vec::new(),
count: None,
device_delay_s: None,
}
}
pub fn name(&self) -> &str {
&self.inner.name
}
pub fn stream_type(&self) -> &str {
&self.inner.stream_type
}
pub fn source_id(&self) -> &str {
&self.inner.source_id
}
pub fn hostname(&self) -> &str {
&self.inner.hostname
}
pub fn format(&self) -> Format {
self.inner.format
}
pub fn channel_count(&self) -> usize {
self.inner.channel_count as usize
}
pub fn rate(&self) -> f64 {
self.inner.nominal_srate
}
pub fn is_regular(&self) -> bool {
self.inner.nominal_srate > 0.0
}
pub fn channels(&self) -> Vec<Channel> {
let n = self.channel_count();
let mut out: Vec<Channel> = Vec::with_capacity(n);
if let Some(list) = self.inner.desc.child("channels") {
for node in list.children.iter().filter(|c| c.name == "channel").take(n) {
let mut label = node.child_value_of("label").trim();
if label.is_empty() {
label = node.child_value_of("name").trim();
}
out.push(Channel {
label: label.to_string(),
unit: node.child_value_of("unit").trim().to_string(),
kind: node.child_value_of("type").trim().to_string(),
});
}
}
while out.len() < n {
out.push(Channel::new(&format!("Ch{}", out.len() + 1)));
}
for (i, c) in out.iter_mut().enumerate() {
if c.label.is_empty() {
c.label = format!("Ch{}", i + 1);
}
}
out
}
pub fn fetch(&self, timeout: std::time::Duration) -> Result<StreamInfo, Error> {
let inner = labstream_net::read_fullinfo(&self.inner, timeout)?;
Ok(StreamInfo { inner })
}
pub fn desc(&self) -> &labstream_net::desc::Node {
&self.inner.desc
}
pub fn device_delay(&self) -> Option<f64> {
self.inner.device_delay()
}
}
#[derive(Debug, Clone)]
pub struct Builder {
name: String,
stream_type: String,
format: Format,
rate: f64,
source_id: String,
channels: Vec<Channel>,
count: Option<usize>,
device_delay_s: Option<f64>,
}
impl Builder {
pub fn rate(mut self, hz: f64) -> Builder {
self.rate = hz;
self
}
pub fn irregular(mut self) -> Builder {
self.rate = 0.0;
self
}
pub fn source_id(mut self, id: &str) -> Builder {
self.source_id = id.to_string();
self
}
pub fn channel_count(mut self, n: usize) -> Builder {
self.count = Some(n);
self
}
pub fn channel_labels<I, S>(mut self, labels: I) -> Builder
where
I: IntoIterator<Item = S>,
S: AsRef<str>,
{
self.channels = labels
.into_iter()
.map(|l| Channel::new(l.as_ref()))
.collect();
self
}
pub fn channels<I>(mut self, channels: I) -> Builder
where
I: IntoIterator<Item = Channel>,
{
self.channels = channels.into_iter().collect();
self
}
pub fn device_delay(mut self, delay_s: f64) -> Builder {
if delay_s.is_finite() {
self.device_delay_s = Some(delay_s);
}
self
}
pub fn build(self) -> Result<StreamInfo, Error> {
if self.name.is_empty() {
return Err(Error::Invalid("a stream needs a name"));
}
if self.rate < 0.0 {
return Err(Error::Invalid("a rate cannot be negative"));
}
let n = match (self.count, self.channels.len()) {
(Some(a), 0) => a,
(Some(a), b) if a != b => {
return Err(Error::Invalid(
"the channel count and the channel list disagree",
))
}
(_, b) => b,
};
if n == 0 {
return Err(Error::Invalid("a stream needs at least one channel"));
}
let mut inner = labstream_net::StreamInfo::new(
&self.name,
&self.stream_type,
n as u32,
self.format,
self.rate,
);
inner.source_id = self.source_id;
if !self.channels.is_empty() {
let list = inner.desc.append_child("channels");
for c in &self.channels {
let node = list.append_child("channel");
node.append_child_value("label", &c.label);
if !c.unit.is_empty() {
node.append_child_value("unit", &c.unit);
}
if !c.kind.is_empty() {
node.append_child_value("type", &c.kind);
}
}
}
if let Some(delay) = self.device_delay_s {
inner.set_device_delay(delay, None);
}
Ok(StreamInfo { inner })
}
}