pub struct StreamInfo {Show 17 fields
pub name: String,
pub stream_type: String,
pub channel_count: u32,
pub format: Format,
pub nominal_srate: f64,
pub source_id: String,
pub session_id: String,
pub hostname: String,
pub uid: String,
pub created_at: f64,
pub v4address: String,
pub v6address: String,
pub v4data_port: u16,
pub v4service_port: u16,
pub v6data_port: u16,
pub v6service_port: u16,
pub desc: Node,
/* private fields */
}Expand description
The description of one stream.
Fields§
§name: StringThe stream name.
stream_type: StringThe content type.
channel_count: u32The channel count.
format: FormatThe channel format.
nominal_srate: f64The nominal rate. Zero means an irregular rate.
source_id: StringThe source identifier. An empty value blocks automatic recovery.
session_id: StringThe session identifier. A resolver query always tests this field.
hostname: StringThe host name of the sender.
uid: StringThe stream instance identifier.
created_at: f64The clock reading at creation.
v4address: StringThe address that the answer to a query came from.
An outlet leaves this empty, and the resolver of the peer fills it in
from the sender of the answer (src/resolve_attempt_udp.cpp:135-140).
An inlet connects to this address, so a description with an empty one
can only be reached on the machine that published it.
v6address: StringThe IPv6 address that the answer came from. Same rule as
StreamInfo::v4address.
v4data_port: u16The TCP port that carries the data.
v4service_port: u16The UDP port that answers a query and a time probe.
v6data_port: u16The TCP port that carries the data over IPv6.
v6service_port: u16The UDP port that answers over IPv6.
desc: NodeThe description tree.
A discovery answer never carries this. It travels on the data port, in
answer to LSL:fullinfo. The node itself is the <desc> element, so
its children are what an application added.
Implementations§
Source§impl StreamInfo
impl StreamInfo
Sourcepub fn new(
name: &str,
stream_type: &str,
channel_count: u32,
format: Format,
srate: f64,
) -> Self
pub fn new( name: &str, stream_type: &str, channel_count: u32, format: Format, srate: f64, ) -> Self
Build a description with the usual values.
Examples found in repository?
106fn main() {
107 let a = parse();
108 let stream_name = tagged(&a.name, &a.tag);
109 let marker_name = tagged(&format!("{}-Markers", a.name), &a.tag);
110 let suffix = if a.tag.is_empty() {
111 String::new()
112 } else {
113 format!("_{}", a.tag)
114 };
115
116 let mut info = StreamInfo::new(
117 &stream_name,
118 &a.stream_type,
119 a.channels as u32,
120 Format::Float32,
121 a.rate,
122 );
123 // A source identifier lets a consumer find the stream again after a
124 // restart. Without one, no recovery is possible. SPEC.md 10.1.
125 //
126 // The identifier and the tree below match `oracle/publish.c` exactly, so
127 // the three publishers of `oracle/labrecorder.sh` describe one stream and
128 // a plot of one can be compared with a plot of another.
129 info.source_id = format!("{}_publish{suffix}", a.name);
130
131 {
132 let chns = info.desc.append_child("channels");
133 for k in 0..a.channels {
134 let c = chns.append_child("channel");
135 c.append_child_value("label", &label(k));
136 c.append_child_value("unit", "microvolts");
137 c.append_child_value("type", &a.stream_type);
138 }
139 }
140 info.desc
141 .append_child_value("manufacturer", "conformance publisher");
142 let acq = info.desc.append_child("acquisition");
143 acq.append_child_value("model", "publish example");
144
145 let outlet = match Outlet::new(info) {
146 Ok(o) => o,
147 Err(e) => {
148 eprintln!("cannot publish: {e}");
149 std::process::exit(1);
150 }
151 };
152
153 // A marker stream is a second stream: one channel of text, and no rate.
154 // A recorder treats it as events rather than as a signal.
155 let markers = if a.markers {
156 let mut m = StreamInfo::new(&marker_name, "Markers", 1, Format::String, 0.0);
157 m.source_id = format!("{}_markers{suffix}", a.name);
158 match Outlet::new(m) {
159 Ok(o) => Some(o),
160 Err(e) => {
161 eprintln!("cannot publish the markers: {e}");
162 None
163 }
164 }
165 } else {
166 None
167 };
168
169 let published = outlet.info();
170 println!("=== the stream is published");
171 println!(" name {}", published.name);
172 println!(" type {}", published.stream_type);
173 println!(" channels {} float32", published.channel_count);
174 println!(" rate {} Hz", published.nominal_srate);
175 println!(" source id {}", published.source_id);
176 println!(" session {}", published.session_id);
177 println!(" host {}", published.hostname);
178 println!(" uid {}", published.uid);
179 let cfg = labstream_net::config::get();
180 let port = |n: u16| {
181 if n == 0 {
182 "none".to_string()
183 } else {
184 n.to_string()
185 }
186 };
187 println!(
188 " IPv4 data {} service {}",
189 port(published.v4data_port),
190 port(published.v4service_port)
191 );
192 println!(
193 " IPv6 data {} service {}",
194 port(published.v6data_port),
195 port(published.v6service_port)
196 );
197 let which = match (cfg.allow_ipv4, cfg.allow_ipv6) {
198 (true, true) => "IPv4 and IPv6",
199 (true, false) => "IPv4 only",
200 (false, true) => "IPv6 only. A consumer has no other way to reach this stream.",
201 (false, false) => "none. Nothing can reach this stream.",
202 };
203 println!(" protocols {which}");
204 match &markers {
205 Some(m) => {
206 println!(
207 " marker stream {} ({})",
208 m.info().name,
209 m.info().source_id
210 );
211 println!(" one every second, on a zero crossing of channel one");
212 if !m.holds_multicast_port() {
213 println!(" WARNING: the marker stream does not hold the multicast port.");
214 }
215 }
216 None => println!(" marker stream none"),
217 }
218
219 // A query from another machine arrives by multicast or by broadcast. The
220 // outlet joins each group on each local address, so this list is the one
221 // that decides whether another machine can find the stream at all.
222 let addresses = labstream_net::local_ipv4_addresses();
223 println!("\n=== the addresses that carry a query");
224 if addresses.is_empty() {
225 println!(" none found. Another machine will not see this stream.");
226 }
227 for addr in &addresses {
228 println!(" {addr}");
229 }
230 println!(" multicast port {}", cfg.multicast_port);
231 println!(" groups {}", cfg.multicast_addresses.join(", "));
232 println!(" configuration {}", cfg.source);
233 if outlet.holds_multicast_port() {
234 println!(" multicast port held by this outlet");
235 } else {
236 println!(
237 " WARNING: port {} is held by another process on this machine.",
238 cfg.multicast_port
239 );
240 println!(" A multicast query will not reach this outlet. Stop the other");
241 println!(" program, or expect the other machine to find nothing.");
242 }
243
244 println!("\n=== open LabRecorder on the other machine and press Update");
245 println!(" The count below rises when a recorder links the stream.\n");
246
247 let period = if a.rate > 0.0 { 1.0 / a.rate } else { 0.01 };
248 let start = Instant::now();
249 let mut n: u64 = 0;
250 let mut consumers = 0;
251 let mut reported = Instant::now() - Duration::from_secs(10);
252 let mut block: Vec<Sample> = Vec::new();
253
254 loop {
255 if a.seconds > 0.0 && start.elapsed().as_secs_f64() >= a.seconds {
256 println!("\n{n} samples sent. Stopping.");
257 return;
258 }
259
260 let now = outlet.consumer_count();
261 if now != consumers {
262 println!(
263 " consumers {consumers} -> {now} after {:.1} s, {n} samples",
264 start.elapsed().as_secs_f64()
265 );
266 consumers = now;
267 }
268 if reported.elapsed() >= Duration::from_secs(5) {
269 println!(
270 " {:.0} s, {n} samples, {consumers} consumer(s)",
271 start.elapsed().as_secs_f64()
272 );
273 reported = Instant::now();
274 }
275
276 // The value of a channel comes from the sample number, so a recording
277 // can be tested later. The last channel holds the number itself.
278 let t = n as f64 / a.rate.max(1.0);
279 let values: Vec<Value> = (0..a.channels)
280 .map(|k| {
281 if k + 1 == a.channels {
282 Value::F32(n as f32)
283 } else {
284 let hz = (k + 1) as f64;
285 Value::F32((100.0 * (2.0 * std::f64::consts::PI * hz * t).sin()) as f32)
286 }
287 })
288 .collect();
289 let sample = Sample {
290 // A timestamp of zero means the current clock. SPEC.md 8.3.
291 timestamp: 0.0,
292 values,
293 };
294
295 if a.chunk > 1 {
296 block.push(sample);
297 if block.len() == a.chunk {
298 // The timestamp names the last sample of the chunk. SPEC.md 9.2.
299 outlet.push_chunk(&block, 0.0);
300 block.clear();
301 }
302 } else {
303 outlet.push(&sample);
304 }
305
306 // The marker carries the same rule for its timestamp as the sample: a
307 // value of zero means the current clock. The two therefore sit within
308 // a fraction of one period of each other. SPEC.md 8.3.
309 if let (Some(m), Some(text)) = (&markers, marker_for(n, a.rate)) {
310 m.push(&Sample {
311 timestamp: 0.0,
312 values: vec![Value::Str(text.into_bytes())],
313 });
314 }
315 n += 1;
316
317 std::thread::sleep(Duration::from_secs_f64(period));
318 }
319}Sourcepub fn to_shortinfo_xml(&self) -> String
pub fn to_shortinfo_xml(&self) -> String
Write the short description that a discovery answer carries.
The field order and the number format follow the capture in
captures/discover.json. A real inlet reads this with pugixml, so it
needs valid XML and the exact field names.
Sourcepub fn to_fullinfo_xml(&self) -> String
pub fn to_fullinfo_xml(&self) -> String
Write the whole document, with the description tree.
This is what a data port returns for LSL:fullinfo
(src/tcp_server.cpp:508), and what lsl_get_xml returns
(src/lsl_streaminfo_c.cpp:63). The only difference from the short form
is the content of <desc>.
Sourcepub fn from_shortinfo_xml(xml: &str) -> Option<Self>
pub fn from_shortinfo_xml(xml: &str) -> Option<Self>
Read a short description.
Sourcepub fn from_fullinfo_xml(xml: &str) -> Result<Self, String>
pub fn from_fullinfo_xml(xml: &str) -> Result<Self, String>
Read a whole document, with the description tree.
The checks and their order follow read_xml
(src/stream_info_impl.cpp:99-155). The text of the error is the text
that liblsl throws, because the C ABI shows it: a rejected document
leaves a blank description whose name reads (invalid: TEXT).
captures/desc/desc_nouid.name holds one measured example.
Sourcepub fn document(&self) -> Node
pub fn document(&self) -> Node
The description as a tree, with the <info> element at the root.
A query is evaluated against this (src/stream_info_impl.cpp:214). The
text of every field is the text that the same field carries on the
wire, so a query that names a number reads the same value that a peer
would read.
Sourcepub fn matches(&self, query: &str) -> bool
pub fn matches(&self, query: &str) -> bool
Test a query against this description.
The query is XPath, the way liblsl reads it
(src/stream_info_impl.cpp:214). See crate::xpath for what the
reader covers and what it refuses.
Sourcepub fn apply_selection(&self, sel: &SelectParam) -> Self
pub fn apply_selection(&self, sel: &SelectParam) -> Self
Return a copy of this description updated for an active select/1
channel subset and decimation factor (docs/roadmap.md, step 6).
Updates channel_count, nominal_srate, and any <desc><channels>
<channel> elements to match sel.channels in order.
Sourcepub fn set_device_delay(&mut self, delay_s: f64, rms_s: Option<f64>)
pub fn set_device_delay(&mut self, delay_s: f64, rms_s: Option<f64>)
Store a calibrated device delay (and optional jitter RMS) in
<desc><synchronization><offset_mean> (docs/roadmap.md, step 15).
liblsl tools and LabRecorder keep <desc> verbatim in XDF files.
Sourcepub fn set_device_delay_stats(
&mut self,
mean_s: f64,
rms_s: Option<f64>,
median_s: Option<f64>,
p5_s: Option<f64>,
p95_s: Option<f64>,
)
pub fn set_device_delay_stats( &mut self, mean_s: f64, rms_s: Option<f64>, median_s: Option<f64>, p5_s: Option<f64>, p95_s: Option<f64>, )
Store full device-delay calibration statistics in
<desc><synchronization> (docs/roadmap.md, step 15).
Sourcepub fn device_delay(&self) -> Option<f64>
pub fn device_delay(&self) -> Option<f64>
Read the calibrated device delay (in seconds) from
<desc><synchronization><offset_mean>, if present and finite.
Sourcepub fn device_delay_rms(&self) -> Option<f64>
pub fn device_delay_rms(&self) -> Option<f64>
Read the calibrated device delay RMS jitter (in seconds) from
<desc><synchronization><offset_rms>, if present and finite.
Source§impl StreamInfo
impl StreamInfo
Sourcepub fn buffer_samples(&self, requested: i32, flags: u32) -> i32
pub fn buffer_samples(&self, requested: i32, flags: u32) -> i32
Convert a requested buffer length into a count of samples.
This is calc_transport_buf_samples (src/stream_info_impl.cpp:254-270),
and liblsl runs it at the C boundary (src/lsl_inlet_c.cpp:20). The
wire only ever carries samples. SPEC.md 8.6.
An implementation that skips this step builds a buffer wrong by a factor of the rate. At 1000 Hz a request of 20 means 20,000 samples.
Source§impl StreamInfo
impl StreamInfo
Sourcepub fn recovery_query(&self) -> String
pub fn recovery_query(&self) -> String
The query that an inlet uses to find a stream again after it is lost.
src/inlet_connection.cpp:154-173. The fields are the ones that survive
a restart of the source. A stream keeps its name, its type, its source
identifier, its channel count, and its channel format. It receives a new
instance identifier.
The nominal rate is left out on purpose. liblsl explains why in a comment: a rate written as text and read back is often not the same number, and a resolver then never finds the stream.
The session identifier is also left out, unlike the query that a plain
resolve builds (src/resolver_impl.cpp:66-73).
Sourcepub fn is_recoverable(&self) -> bool
pub fn is_recoverable(&self) -> bool
True when this stream can be found again after its source restarts.
A stream with no source identifier cannot. liblsl warns about it at
connection time (src/inlet_connection.cpp:40).