2021-07-15 19:28:19 +08:00
|
|
|
//! Stabilizer data stream capabilities
|
|
|
|
//!
|
|
|
|
//! # Design
|
|
|
|
//! Data streamining utilizes UDP packets to send live data streams at high throughput.
|
2021-07-23 21:30:38 +08:00
|
|
|
//! Packets are always sent in a best-effort fashion, and data may be dropped.
|
2021-07-15 19:28:19 +08:00
|
|
|
//!
|
2021-07-23 21:30:38 +08:00
|
|
|
//! Stabilizer organizes livestreamed data into batches within a "Frame" that will be sent as a UDP
|
|
|
|
//! packet. Each frame consits of a header followed by sequential batch serializations. The packet
|
|
|
|
//! header is constant for all streaming capabilities, but the serialization format after the header
|
|
|
|
//! is application-defined.
|
|
|
|
//!
|
|
|
|
//! ## Header Format
|
|
|
|
//! The header of each stream frame consists of 7 bytes. All data is stored in little-endian format.
|
|
|
|
//!
|
2021-07-26 18:24:36 +08:00
|
|
|
//! Elements appear sequentially as follows:
|
|
|
|
//! * Magic word 0x057B <u16>
|
|
|
|
//! * Format Code <u8>
|
|
|
|
//! * Batch Size <u8>
|
|
|
|
//! * Sequence Number <u32>
|
2021-07-23 21:30:38 +08:00
|
|
|
//!
|
2021-07-26 18:24:36 +08:00
|
|
|
//! The "Magic word" is a constant field for all packets. The value is alway 0x057B.
|
2021-07-23 21:30:38 +08:00
|
|
|
//!
|
|
|
|
//! The "Format Code" is a unique specifier that indicates the serialization format of each batch of
|
|
|
|
//! data in the frame. Refer to [StreamFormat] for further information.
|
|
|
|
//!
|
2021-07-26 18:24:36 +08:00
|
|
|
//! The "Batch size" is the value of [SAMPLE_BUFFER_SIZE].
|
2021-07-23 21:30:38 +08:00
|
|
|
//!
|
2021-07-26 18:24:36 +08:00
|
|
|
//! The "Sequence Number" is an identifier that increments for ever execution of the DSP process.
|
|
|
|
//! This can be used to determine if a stream frame was lost.
|
2021-07-15 19:28:19 +08:00
|
|
|
//!
|
|
|
|
//! # Example
|
|
|
|
//! A sample Python script is available in `scripts/stream_throughput.py` to demonstrate reception
|
|
|
|
//! of livestreamed data.
|
2021-06-09 19:26:41 +08:00
|
|
|
use heapless::spsc::{Consumer, Producer, Queue};
|
2021-06-09 21:25:59 +08:00
|
|
|
use miniconf::MiniconfAtomic;
|
|
|
|
use serde::Deserialize;
|
|
|
|
use smoltcp_nal::embedded_nal::{IpAddr, Ipv4Addr, SocketAddr, UdpClientStack};
|
2021-05-17 18:43:04 +08:00
|
|
|
|
2021-07-23 21:08:07 +08:00
|
|
|
use crate::hardware::design_parameters::SAMPLE_BUFFER_SIZE;
|
|
|
|
|
2021-07-22 20:45:58 +08:00
|
|
|
use heapless::pool::{Box, Init, Pool, Uninit};
|
|
|
|
|
2021-05-17 18:43:04 +08:00
|
|
|
use super::NetworkReference;
|
|
|
|
|
2021-07-26 18:24:36 +08:00
|
|
|
const MAGIC_WORD: u16 = 0x057B;
|
|
|
|
|
|
|
|
const HEADER_SIZE: usize = 8;
|
|
|
|
|
2021-07-23 21:30:38 +08:00
|
|
|
// The number of frames that can be buffered.
|
|
|
|
const FRAME_COUNT: usize = 4;
|
|
|
|
|
|
|
|
// The size of each livestream frame in bytes.
|
2021-07-26 18:26:10 +08:00
|
|
|
const FRAME_SIZE: usize = 1024 + HEADER_SIZE;
|
2021-05-17 18:43:04 +08:00
|
|
|
|
2021-07-23 21:30:38 +08:00
|
|
|
// Static storage used for a heapless::Pool of frame buffers.
|
2021-07-23 20:12:59 +08:00
|
|
|
static mut FRAME_DATA: [u8; FRAME_SIZE * FRAME_COUNT] =
|
|
|
|
[0; FRAME_SIZE * FRAME_COUNT];
|
2021-05-29 01:37:28 +08:00
|
|
|
|
2021-06-15 19:18:16 +08:00
|
|
|
/// Represents the destination for the UDP stream to send data to.
|
2021-07-15 19:28:19 +08:00
|
|
|
///
|
|
|
|
/// # Miniconf
|
|
|
|
/// `{"ip": <addr>, "port": <port>}`
|
|
|
|
///
|
|
|
|
/// * `<addr>` is an array of 4 bytes. E.g. `[192, 168, 0, 1]`
|
|
|
|
/// * `<port>` is any unsigned 16-bit value.
|
|
|
|
///
|
|
|
|
/// ## Example
|
|
|
|
/// `{"ip": [192, 168,0, 1], "port": 1111}`
|
2021-06-25 04:14:55 +08:00
|
|
|
#[derive(Copy, Clone, Debug, MiniconfAtomic, Deserialize, Default)]
|
2021-06-09 21:25:59 +08:00
|
|
|
pub struct StreamTarget {
|
|
|
|
pub ip: [u8; 4],
|
|
|
|
pub port: u16,
|
|
|
|
}
|
|
|
|
|
2021-07-23 21:08:07 +08:00
|
|
|
/// Specifies the format of streamed data
|
2021-07-26 18:24:36 +08:00
|
|
|
#[repr(u8)]
|
2021-07-23 21:08:07 +08:00
|
|
|
#[derive(Debug, Copy, Clone, PartialEq)]
|
|
|
|
pub enum StreamFormat {
|
2021-07-26 18:24:36 +08:00
|
|
|
/// Reserved, unused format specifier.
|
|
|
|
Unknown = 0,
|
|
|
|
|
2021-07-23 21:30:38 +08:00
|
|
|
/// Streamed data contains ADC0, ADC1, DAC0, and DAC1 sequentially in little-endian format.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
/// With a batch size of 2, the serialization would take the following form:
|
|
|
|
/// ```
|
|
|
|
/// <ADC0[0]> <ADC0[1]> <ADC1[0]> <ADC1[1]> <DAC0[0]> <DAC0[1]> <DAC1[0]> <DAC1[1]>
|
|
|
|
/// ```
|
2021-07-26 18:24:36 +08:00
|
|
|
AdcDacData = 1,
|
2021-07-23 21:08:07 +08:00
|
|
|
}
|
|
|
|
|
2021-06-15 19:46:39 +08:00
|
|
|
impl From<StreamTarget> for SocketAddr {
|
|
|
|
fn from(target: StreamTarget) -> SocketAddr {
|
2021-06-09 21:25:59 +08:00
|
|
|
SocketAddr::new(
|
|
|
|
IpAddr::V4(Ipv4Addr::new(
|
2021-06-15 19:46:39 +08:00
|
|
|
target.ip[0],
|
|
|
|
target.ip[1],
|
|
|
|
target.ip[2],
|
|
|
|
target.ip[3],
|
2021-06-09 21:25:59 +08:00
|
|
|
)),
|
2021-06-15 19:46:39 +08:00
|
|
|
target.port,
|
2021-06-09 21:25:59 +08:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-15 19:18:16 +08:00
|
|
|
/// Configure streaming on a device.
|
|
|
|
///
|
|
|
|
/// # Args
|
|
|
|
/// * `stack` - A reference to the shared network stack.
|
|
|
|
///
|
|
|
|
/// # Returns
|
|
|
|
/// (generator, stream) where `generator` can be used to enqueue "batches" for transmission. The
|
|
|
|
/// `stream` is the logically consumer (UDP transmitter) of the enqueued data.
|
2021-05-17 18:43:04 +08:00
|
|
|
pub fn setup_streaming(
|
|
|
|
stack: NetworkReference,
|
2021-07-22 20:45:58 +08:00
|
|
|
) -> (FrameGenerator, DataStream) {
|
|
|
|
let queue =
|
|
|
|
cortex_m::singleton!(: Queue<StreamFrame, FRAME_COUNT> = Queue::new())
|
|
|
|
.unwrap();
|
2021-05-17 18:43:04 +08:00
|
|
|
let (producer, consumer) = queue.split();
|
|
|
|
|
2021-07-22 20:45:58 +08:00
|
|
|
let frame_pool =
|
2021-07-23 20:12:59 +08:00
|
|
|
cortex_m::singleton!(: Pool<[u8; FRAME_SIZE]>= Pool::new()).unwrap();
|
2021-05-17 18:43:04 +08:00
|
|
|
|
2021-07-22 20:45:58 +08:00
|
|
|
// Note(unsafe): We guarantee that FRAME_DATA is only accessed once in this function.
|
|
|
|
let memory = unsafe { &mut FRAME_DATA };
|
|
|
|
frame_pool.grow(memory);
|
|
|
|
|
|
|
|
let generator = FrameGenerator::new(producer, frame_pool);
|
|
|
|
|
|
|
|
let stream = DataStream::new(stack, consumer, frame_pool);
|
2021-05-17 18:43:04 +08:00
|
|
|
|
|
|
|
(generator, stream)
|
|
|
|
}
|
|
|
|
|
2021-07-22 20:45:58 +08:00
|
|
|
struct StreamFrame {
|
2021-07-23 20:12:59 +08:00
|
|
|
buffer: Box<[u8; FRAME_SIZE], Init>,
|
2021-07-22 20:45:58 +08:00
|
|
|
offset: usize,
|
2021-05-17 18:43:04 +08:00
|
|
|
}
|
|
|
|
|
2021-07-22 20:45:58 +08:00
|
|
|
impl StreamFrame {
|
|
|
|
pub fn new(
|
2021-07-23 20:12:59 +08:00
|
|
|
buffer: Box<[u8; FRAME_SIZE], Uninit>,
|
2021-07-26 18:24:36 +08:00
|
|
|
format: u8,
|
|
|
|
buffer_size: u8,
|
|
|
|
sequence_number: u32,
|
2021-07-22 20:45:58 +08:00
|
|
|
) -> Self {
|
2021-07-26 18:24:36 +08:00
|
|
|
let mut buffer = unsafe { buffer.assume_init() };
|
|
|
|
buffer[0..2].copy_from_slice(&MAGIC_WORD.to_ne_bytes());
|
|
|
|
buffer[2] = format;
|
|
|
|
buffer[3] = buffer_size;
|
|
|
|
buffer[4..8].copy_from_slice(&sequence_number.to_ne_bytes());
|
2021-05-17 18:43:04 +08:00
|
|
|
Self {
|
2021-07-26 18:24:36 +08:00
|
|
|
buffer,
|
|
|
|
offset: HEADER_SIZE,
|
2021-05-17 18:43:04 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-22 20:45:58 +08:00
|
|
|
pub fn add_batch<F, const T: usize>(&mut self, mut f: F)
|
|
|
|
where
|
|
|
|
F: FnMut(&mut [u8]),
|
|
|
|
{
|
|
|
|
assert!(!self.is_full::<T>(), "Batch cannot be added to full frame");
|
|
|
|
|
|
|
|
let result = f(&mut self.buffer[self.offset..self.offset + T]);
|
|
|
|
|
|
|
|
self.offset += T;
|
|
|
|
|
|
|
|
result
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_full<const T: usize>(&self) -> bool {
|
|
|
|
self.offset + T >= self.buffer.len()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn finish(&mut self) -> &[u8] {
|
2021-07-26 18:24:36 +08:00
|
|
|
&self.buffer[..self.offset]
|
2021-05-17 18:43:04 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-22 20:45:58 +08:00
|
|
|
/// The data generator for a stream.
|
|
|
|
pub struct FrameGenerator {
|
|
|
|
queue: Producer<'static, StreamFrame, FRAME_COUNT>,
|
2021-07-23 20:12:59 +08:00
|
|
|
pool: &'static Pool<[u8; FRAME_SIZE]>,
|
2021-07-22 20:45:58 +08:00
|
|
|
current_frame: Option<StreamFrame>,
|
2021-07-26 18:24:36 +08:00
|
|
|
sequence_number: u32,
|
|
|
|
format: StreamFormat,
|
2021-05-17 18:43:04 +08:00
|
|
|
}
|
|
|
|
|
2021-07-22 20:45:58 +08:00
|
|
|
impl FrameGenerator {
|
|
|
|
fn new(
|
|
|
|
queue: Producer<'static, StreamFrame, FRAME_COUNT>,
|
2021-07-23 20:12:59 +08:00
|
|
|
pool: &'static Pool<[u8; FRAME_SIZE]>,
|
2021-07-22 20:45:58 +08:00
|
|
|
) -> Self {
|
2021-06-11 22:36:19 +08:00
|
|
|
Self {
|
2021-07-22 20:45:58 +08:00
|
|
|
queue,
|
|
|
|
pool,
|
2021-07-26 18:24:36 +08:00
|
|
|
format: StreamFormat::Unknown,
|
2021-07-22 20:45:58 +08:00
|
|
|
current_frame: None,
|
|
|
|
sequence_number: 0,
|
2021-06-11 22:36:19 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-26 18:24:36 +08:00
|
|
|
/// Specify the format of the stream.
|
|
|
|
///
|
|
|
|
/// # Note:
|
|
|
|
/// This function may only be called once upon initializing streaming
|
|
|
|
///
|
|
|
|
/// # Args
|
|
|
|
/// * `format` - The desired format of the stream.
|
|
|
|
#[doc(hidden)]
|
|
|
|
pub(crate) fn set_format(&mut self, format: StreamFormat) {
|
|
|
|
assert!(self.format == StreamFormat::Unknown);
|
|
|
|
assert!(format != StreamFormat::Unknown);
|
|
|
|
self.format = format;
|
|
|
|
}
|
|
|
|
|
2021-07-23 21:30:38 +08:00
|
|
|
/// Add a batch to the current stream frame.
|
|
|
|
///
|
|
|
|
/// # Args
|
|
|
|
/// * `f` - A closure that will be provided the buffer to write batch data into. The buffer will
|
|
|
|
/// be the size of the `T` template argument.
|
2021-07-26 18:24:36 +08:00
|
|
|
pub fn add<F, const T: usize>(&mut self, f: F)
|
2021-07-22 20:45:58 +08:00
|
|
|
where
|
|
|
|
F: FnMut(&mut [u8]),
|
|
|
|
{
|
|
|
|
let sequence_number = self.sequence_number;
|
|
|
|
self.sequence_number = self.sequence_number.wrapping_add(1);
|
|
|
|
|
|
|
|
if self.current_frame.is_none() {
|
|
|
|
if let Some(buffer) = self.pool.alloc() {
|
|
|
|
self.current_frame.replace(StreamFrame::new(
|
|
|
|
buffer,
|
2021-07-26 18:24:36 +08:00
|
|
|
self.format as u8,
|
|
|
|
SAMPLE_BUFFER_SIZE as u8,
|
2021-07-22 20:45:58 +08:00
|
|
|
sequence_number,
|
|
|
|
));
|
|
|
|
} else {
|
|
|
|
return;
|
2021-06-11 22:36:19 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-26 18:24:36 +08:00
|
|
|
let current_frame = self.current_frame.as_mut().unwrap();
|
2021-07-23 21:08:07 +08:00
|
|
|
|
2021-07-26 18:24:36 +08:00
|
|
|
current_frame.add_batch::<_, T>(f);
|
2021-05-29 00:57:23 +08:00
|
|
|
|
2021-07-26 18:24:36 +08:00
|
|
|
if current_frame.is_full::<T>() {
|
2021-07-23 20:12:59 +08:00
|
|
|
if self
|
|
|
|
.queue
|
|
|
|
.enqueue(self.current_frame.take().unwrap())
|
|
|
|
.is_err()
|
|
|
|
{
|
|
|
|
// Given that the queue is the same size as the number of frames available, this
|
|
|
|
// should never occur.
|
|
|
|
panic!("Frame enqueue failure")
|
|
|
|
}
|
2021-05-18 00:33:43 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-15 19:18:16 +08:00
|
|
|
/// The "consumer" portion of the data stream.
|
|
|
|
///
|
|
|
|
/// # Note
|
|
|
|
/// This is responsible for consuming data and sending it over UDP.
|
|
|
|
pub struct DataStream {
|
|
|
|
stack: NetworkReference,
|
|
|
|
socket: Option<<NetworkReference as UdpClientStack>::UdpSocket>,
|
2021-07-22 20:45:58 +08:00
|
|
|
queue: Consumer<'static, StreamFrame, FRAME_COUNT>,
|
2021-07-23 20:12:59 +08:00
|
|
|
frame_pool: &'static Pool<[u8; FRAME_SIZE]>,
|
2021-06-15 19:18:16 +08:00
|
|
|
remote: SocketAddr,
|
|
|
|
}
|
|
|
|
|
2021-05-17 18:43:04 +08:00
|
|
|
impl DataStream {
|
2021-06-15 19:18:16 +08:00
|
|
|
/// Construct a new data streamer.
|
|
|
|
///
|
|
|
|
/// # Args
|
|
|
|
/// * `stack` - A reference to the shared network stack.
|
|
|
|
/// * `consumer` - The read side of the queue containing data to transmit.
|
2021-07-22 20:45:58 +08:00
|
|
|
/// * `frame_pool` - The Pool to return stream frame objects into.
|
2021-06-15 19:18:16 +08:00
|
|
|
fn new(
|
2021-05-17 18:43:04 +08:00
|
|
|
stack: NetworkReference,
|
2021-07-22 20:45:58 +08:00
|
|
|
consumer: Consumer<'static, StreamFrame, FRAME_COUNT>,
|
2021-07-23 20:12:59 +08:00
|
|
|
frame_pool: &'static Pool<[u8; FRAME_SIZE]>,
|
2021-05-17 18:43:04 +08:00
|
|
|
) -> Self {
|
|
|
|
Self {
|
|
|
|
stack,
|
|
|
|
socket: None,
|
2021-06-15 19:18:16 +08:00
|
|
|
remote: StreamTarget::default().into(),
|
2021-05-17 18:43:04 +08:00
|
|
|
queue: consumer,
|
2021-07-22 20:45:58 +08:00
|
|
|
frame_pool,
|
2021-05-17 18:43:04 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-18 00:33:43 +08:00
|
|
|
fn close(&mut self) {
|
2021-06-25 00:16:27 +08:00
|
|
|
if let Some(socket) = self.socket.take() {
|
|
|
|
log::info!("Closing stream");
|
|
|
|
// Note(unwrap): We guarantee that the socket is available above.
|
|
|
|
self.stack.close(socket).unwrap();
|
2021-05-17 18:43:04 +08:00
|
|
|
}
|
2021-06-25 00:16:27 +08:00
|
|
|
}
|
2021-05-17 18:43:04 +08:00
|
|
|
|
2021-06-25 00:16:27 +08:00
|
|
|
// Open new socket.
|
|
|
|
fn open(&mut self) -> Result<(), ()> {
|
|
|
|
// If there is already a socket of if remote address is unspecified,
|
|
|
|
// do not open a new socket.
|
|
|
|
if self.socket.is_some() || self.remote.ip().is_unspecified() {
|
2021-06-09 21:25:59 +08:00
|
|
|
return Err(());
|
|
|
|
}
|
|
|
|
|
2021-06-24 22:36:17 +08:00
|
|
|
log::info!("Opening stream");
|
2021-06-25 00:16:27 +08:00
|
|
|
|
|
|
|
let mut socket = self.stack.socket().or(Err(()))?;
|
|
|
|
|
2021-06-15 20:19:28 +08:00
|
|
|
// Note(unwrap): We only connect with a new socket, so it is guaranteed to not already be
|
|
|
|
// bound.
|
2021-06-25 00:16:27 +08:00
|
|
|
self.stack.connect(&mut socket, self.remote).unwrap();
|
2021-05-17 18:43:04 +08:00
|
|
|
|
|
|
|
self.socket.replace(socket);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-06-15 19:18:16 +08:00
|
|
|
/// Configure the remote endpoint of the stream.
|
|
|
|
///
|
|
|
|
/// # Args
|
|
|
|
/// * `remote` - The destination to send stream data to.
|
2021-05-17 18:43:04 +08:00
|
|
|
pub fn set_remote(&mut self, remote: SocketAddr) {
|
2021-06-25 00:16:27 +08:00
|
|
|
// Close socket to be reopened if the remote has changed.
|
|
|
|
if remote != self.remote {
|
|
|
|
self.close();
|
2021-05-17 18:43:04 +08:00
|
|
|
}
|
2021-06-15 19:18:16 +08:00
|
|
|
self.remote = remote;
|
2021-05-17 18:43:04 +08:00
|
|
|
}
|
|
|
|
|
2021-06-15 19:18:16 +08:00
|
|
|
/// Process any data for transmission.
|
2021-05-29 00:57:23 +08:00
|
|
|
pub fn process(&mut self) {
|
2021-06-25 00:16:27 +08:00
|
|
|
match self.socket.as_mut() {
|
|
|
|
None => {
|
|
|
|
// If there's no socket available, try to connect to our remote.
|
|
|
|
if self.open().is_ok() {
|
|
|
|
// If we just successfully opened the socket, flush old data from queue.
|
2021-07-22 20:45:58 +08:00
|
|
|
while let Some(frame) = self.queue.dequeue() {
|
|
|
|
self.frame_pool.free(frame.buffer);
|
|
|
|
}
|
2021-05-26 23:56:44 +08:00
|
|
|
}
|
|
|
|
}
|
2021-06-25 00:16:27 +08:00
|
|
|
Some(handle) => {
|
2021-07-22 20:45:58 +08:00
|
|
|
if let Some(mut frame) = self.queue.dequeue() {
|
|
|
|
// Transmit the frame and return it to the pool.
|
|
|
|
self.stack.send(handle, frame.finish()).ok();
|
|
|
|
self.frame_pool.free(frame.buffer)
|
2021-06-11 22:36:19 +08:00
|
|
|
}
|
|
|
|
}
|
2021-05-17 18:43:04 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|