Tiny Ogg stream manipulator
Ogg is a stream capsulation, It's not just for Vorbis or Opus audio files, it can be used on everythings that's a stream or protocol for transmitting, etc.
Personally I think Ogg is a perfect binary data transmission protocol for UART/USART.
Overview
The OggPacket have these functions:
fn new(stream_id: u32, packet_type: OggPacketType, packet_index: u32) -> Self;
fn write(&mut self, data: &[u8]) -> usize;
fn clear(&mut self);
fn get_segments(&self) -> Vec<Vec<u8>>;
fn get_inner_data_size(&self) -> usize;
fn get_inner_data(&self) -> Vec<u8>;
fn get_checksum(ogg_packet: &[u8]) -> io::Result<u32>;
fn fill_checksum_field(ogg_packet: &mut [u8]) -> io::Result<()>;
fn into_bytes(self) -> Vec<u8>;
fn get_length(ogg_packet: &[u8]) -> io::Result<usize>;
fn from_bytes(ogg_packet: &[u8], packet_length: &mut usize) -> io::Result<Self>;
fn from_cursor(cursor: &mut Cursor<Vec<u8>>) -> Vec<OggPacket>;
The OggStreamWriter have these functions:
fn set_granule_position(&mut self, position: u64);
fn get_granule_position(&self) -> u64;
fn mark_cur_packet_as_end_of_stream(&mut self);
fn get_bytes_written(&self) -> u64;
fn set_on_seal_callback(&mut self, on_seal: Box<dyn FnMut(usize) -> u64>);
fn reset(&mut self);
fn seal_packet(&mut self, granule_position: u64, is_end_of_stream: bool) -> io::Result<()>;
For more information about each function please read the documentations.
#[derive(Debug, Clone, Copy)]
pub enum OggPacketType {
Continuation = 0,
BeginOfStream = 2,
EndOfStream = 4,
}
#[derive(Debug, Clone)]
pub struct OggPacket {
pub version: u8,
pub packet_type: OggPacketType,
pub granule_position: u64,
pub stream_id: u32,
pub packet_index: u32,
pub checksum: u32,
pub segment_table: Vec<u8>,
pub data: Vec<u8>,
}
pub struct OggStreamWriter<W>
where
W: Write + Debug {
pub writer: W,
pub stream_id: u32,
pub packet_index: u32,
pub cur_packet: OggPacket,
pub granule_position: u64,
pub on_seal: Box<dyn FnMut(usize) -> u64>,
pub bytes_written: u64,
}