1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
pub mod define;
pub mod errors;
pub mod packetizer;
pub mod unpacketizer;
// pub use chunk::{ChunkBasicHeader, ChunkHeader, ChunkInfo, ChunkMessageHeader};
use bytes::BytesMut;
use std::fmt;
//5.3.1.1
#[derive(Eq, PartialEq, Debug, Clone)]
pub struct ChunkBasicHeader {
pub format: u8,
pub chunk_stream_id: u32,
}
impl ChunkBasicHeader {
pub fn new(fmt: u8, csid: u32) -> ChunkBasicHeader {
ChunkBasicHeader {
format: fmt,
chunk_stream_id: csid,
}
}
}
#[derive(Eq, PartialEq, Debug, Clone)]
pub enum ExtendTimestampType {
//There is no extended timestamp
NONE,
//The extended timestamp field is read in format 0 chunk.
FORMAT0,
//The extended timestamp field is read in format 1 or 2 chunk.
FORMAT12,
}
//5.3.1.2
#[derive(Eq, PartialEq, Debug, Clone)]
pub struct ChunkMessageHeader {
//save the absolute timestamp of chunk type 0.
//or save the computed absolute timestamp of chunk type 1,2,3.
pub timestamp: u32,
pub msg_length: u32,
pub msg_type_id: u8,
pub msg_streamd_id: u32,
// Save the timestamp delta of chunk type 1,2.
// For chunk type 3, this field saves the timestamp
// delta inherited from the previous chunk type 1 or 2.
// NOTE: this value should be reset to 0 when the current chunk type is 0.
pub timestamp_delta: u32,
// This field will be set for type 0,1,2 .If the timestamp/timestamp delta >= 0xFFFFFF
// then set this value to FORMAT0/FORMAT12 else set it to NONE.
// Note that when the chunk format is 3, this value will be inherited from
// the most recent chunk 0, 1, or 2 chunk.(5.3.1.3 Extended Timestamp).
pub extended_timestamp_type: ExtendTimestampType,
}
impl ChunkMessageHeader {
pub fn new(timestamp: u32, msg_length: u32, msg_type_id: u8, msg_stream_id: u32) -> Self {
Self {
timestamp,
msg_length,
msg_type_id,
msg_streamd_id: msg_stream_id,
timestamp_delta: 0,
extended_timestamp_type: ExtendTimestampType::NONE,
}
}
}
pub struct ChunkHeader {
pub basic_header: ChunkBasicHeader,
pub message_header: ChunkMessageHeader,
}
impl Default for ChunkHeader {
fn default() -> Self {
Self::new()
}
}
impl ChunkHeader {
pub fn new() -> ChunkHeader {
ChunkHeader {
basic_header: ChunkBasicHeader::new(0, 0),
message_header: ChunkMessageHeader::new(0, 0, 0, 0),
}
}
}
// pub struct Chunk {
// basic_header: ChunkBasicHeader,
// message_header: ChunkMessageHeader,
// raw_data: BytesMut,
// }
#[derive(Eq, PartialEq, Clone)]
pub struct ChunkInfo {
pub basic_header: ChunkBasicHeader,
pub message_header: ChunkMessageHeader,
pub payload: BytesMut,
}
impl fmt::Debug for ChunkInfo {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let hex_payload = hex::encode(&self.payload);
let formatted_payload = hex_payload
.as_bytes()
.chunks(2)
.map(|chunk| format!("0x{}{}", chunk[0] as char, chunk[1] as char))
.collect::<Vec<_>>()
.join(", ");
write!(
f,
"ChunkInfo {{ basic_header: {:?}, message_header: {:?}, payload: {} }}",
self.basic_header, self.message_header, formatted_payload
)
}
}
impl Default for ChunkInfo {
fn default() -> Self {
Self::new(0, 0, 0, 0, 0, 0, BytesMut::new())
}
}
impl ChunkInfo {
pub fn new(
csid: u32,
format: u8,
timestamp: u32,
msg_length: u32,
msg_type_id: u8,
msg_stream_id: u32,
payload: BytesMut,
) -> Self {
Self {
basic_header: ChunkBasicHeader::new(format, csid),
message_header: ChunkMessageHeader::new(
timestamp,
msg_length,
msg_type_id,
msg_stream_id,
),
payload,
}
}
}
// impl Chunk {
// pub fn chunk_read(&mut self, bytes: &[u8]) -> Result {
// self.buffer.extend_from_slice(bytes);
// }
// pub fn read_basic_header(&mut self, bytes: &[u8]) -> Result<UnpackResult, ChunkUnpackError> {
// if self.buffer.len() < 1 {
// return Ok(UnpackResult::NotEnoughBytes);
// }
// }
// }