diff --git a/src/bc/de.rs b/src/bc/de.rs index b34f8d5..af9a37c 100644 --- a/src/bc/de.rs +++ b/src/bc/de.rs @@ -116,7 +116,7 @@ fn bc_modern_msg<'a, 'b, 'c>( Err, }; - let mut in_bin_mode = context.last_binary_kind.contains_key(&header.msg_id); + let mut in_bin_mode = context.in_bin_mode.contains(&header.msg_id); // We'd like to know where the XML stops, but we haven't parsed the XML yet to see if the // binaryData offset in the header is valid @@ -173,64 +173,11 @@ fn bc_modern_msg<'a, 'b, 'c>( // behavior of future passes of this function even if we didn't yet consume the // message). - let mut binary_data = BinaryData { + context.in_bin_mode.insert(header.msg_id); + + let binary_data = BinaryData { data: payload, - continuation_of: None, }; - let binary_kind = binary_data.kind(); - let body_size = binary_data.body_size(); - let data_size = binary_data.data_size(); - - let remaining_binary_bytes = match context.remaining_binary_bytes.get(&header.msg_id) { - Some(remaining_binary_bytes) => remaining_binary_bytes.clone(), - None => 0, - }; - let last_binary_kind = match context.last_binary_kind.get(&header.msg_id) { - Some(last_binary_kind) => last_binary_kind.clone(), - None => None, - }; - - // Handle continuation - if binary_kind != BinaryDataKind::Unknown && data_size > body_size { - // If it is a known type and the size is larger than the bodysize expect more - // packets to follow - context - .remaining_binary_bytes - .insert(header.msg_id, data_size - body_size); - context - .last_binary_kind - .insert(header.msg_id, Some(binary_kind)); - } else if binary_kind == BinaryDataKind::Unknown - && remaining_binary_bytes > 0 - && remaining_binary_bytes >= body_size - { - // If the data is unknown and we are expecting packets then proccess them - // Ensure however that consuming this packet as binary will not result in - // negative remaining_binary_bytes - if body_size == CHUNK_SIZE { - // All known continuation packets are of size CHUNK_SIZE - binary_data.continuation_of = last_binary_kind; - context - .remaining_binary_bytes - .insert(header.msg_id, remaining_binary_bytes - body_size); - } else { - // If its not of chunk size this may be an unknown magic code - // Reset but suggest that this might be an unknown magic code - trace!( - "Possibly new magic code: {:x?}", - &binary_data.body()[..std::cmp::min(body_size, 32)] - ); - context.remaining_binary_bytes.insert(header.msg_id, 0); - context.last_binary_kind.insert(header.msg_id, None); - } - } else { - // In all other cases just proccess data as normal non-continuation packets - // We also reset the remaining_binary_bytes which could be non zero if - // The continuation was inturrepted by an audio frame - // Reset - context.remaining_binary_bytes.insert(header.msg_id, 0); - context.last_binary_kind.insert(header.msg_id, None); - } binary = Some(binary_data); buf = buf_after; diff --git a/src/bc/model.rs b/src/bc/model.rs index 7abd350..d08fe8c 100644 --- a/src/bc/model.rs +++ b/src/bc/model.rs @@ -1,7 +1,8 @@ use super::xml::BcXml; use log::trace; -use std::collections::HashMap; +use std::collections::HashSet; use std::convert::TryInto; +use std::cmp::min; pub(super) const MAGIC_HEADER: u32 = 0xabcdef0; @@ -30,28 +31,49 @@ pub enum BcBody { pub enum BinaryDataKind { VideoDataIframe, VideoDataPframe, - VideoCont, AudioDataAac, AudioDataAdpcm, - AudioCont, InfoData, - InfoDataCont, + Invalid, + Continue, Unknown, } #[derive(Debug, PartialEq, Eq)] pub struct BinaryData { pub data: Vec, - pub continuation_of: Option, } impl BinaryData { pub fn body(&self) -> &[u8] { let lower_limit = self.header_size(); - let upper_limit = self.body_size() + lower_limit; + let upper_limit = self.data_size() + lower_limit; + let len = self.len(); + if ! self.complete() { + unreachable!(); // An incomplete packet should be discarded not read, panic if we try + } + trace!("Sending data from {} to {} of {}", lower_limit, upper_limit, len); &self.data[lower_limit..upper_limit] } + pub fn complete(&self) -> bool { + if self.len() < self.header_size() { + return false; + } + let full_data_len = self.len() - self.header_size(); + full_data_len >= self.data_size() && full_data_len < self.data_size() + 8 + } + + pub fn expected_num_packet(&self) -> usize { + self.data_size() / CHUNK_SIZE + 1 + } + + pub fn header(&self) -> &[u8] { + let lower_limit = 0; + let upper_limit = self.header_size() + lower_limit; + &self.data[min(self.len(),lower_limit)..min(self.len(), upper_limit)] + } + pub fn header_size(&self) -> usize { match self.kind() { BinaryDataKind::VideoDataIframe => 32, @@ -59,10 +81,7 @@ impl BinaryData { BinaryDataKind::AudioDataAac => 8, BinaryDataKind::AudioDataAdpcm => 16, BinaryDataKind::InfoData => 32, - BinaryDataKind::Unknown - | BinaryDataKind::VideoCont - | BinaryDataKind::AudioCont - | BinaryDataKind::InfoDataCont => 0, + BinaryDataKind::Unknown | BinaryDataKind::Invalid | BinaryDataKind::Continue => 0, } } @@ -73,17 +92,7 @@ impl BinaryData { BinaryDataKind::AudioDataAac => BinaryData::bytes_to_size(&self.data[4..6]), BinaryDataKind::AudioDataAdpcm => BinaryData::bytes_to_size(&self.data[4..6]), BinaryDataKind::InfoData => BinaryData::bytes_to_size(&self.data[4..8]), - BinaryDataKind::Unknown - | BinaryDataKind::VideoCont - | BinaryDataKind::AudioCont - | BinaryDataKind::InfoDataCont => self.data.len(), - } - } - - pub fn body_size(&self) -> usize { - match self.data_size() % CHUNK_SIZE { - 0 => CHUNK_SIZE, // Modulo quirk that 39999 -> 39999, 40000 -> 0, 40001 -> 1 - n => n, + BinaryDataKind::Unknown | BinaryDataKind::Invalid | BinaryDataKind::Continue => self.data.len(), } } @@ -110,48 +119,27 @@ impl BinaryData { const MAGIC_IFRAME: &[u8] = &[0x30, 0x30, 0x64, 0x63]; const MAGIC_PFRAME: &[u8] = &[0x30, 0x31, 0x64, 0x63]; - if let Some(continuation_of) = &self.continuation_of { - return match continuation_of { - BinaryDataKind::VideoDataIframe - | BinaryDataKind::VideoDataPframe - | BinaryDataKind::VideoCont => BinaryDataKind::VideoCont, - BinaryDataKind::AudioDataAac - | BinaryDataKind::AudioDataAdpcm - | BinaryDataKind::AudioCont => BinaryDataKind::AudioCont, - BinaryDataKind::InfoData | BinaryDataKind::InfoDataCont => { - BinaryDataKind::InfoDataCont - } - BinaryDataKind::Unknown => BinaryDataKind::Unknown, - }; - } - let magic = &self.data[..4]; - trace!("Magic is: {:x?}", &magic); match magic { MAGIC_VIDEO_INFO => { - trace!("Video info magic type"); BinaryDataKind::InfoData } MAGIC_AAC => { - trace!("AAC magic type"); BinaryDataKind::AudioDataAac } MAGIC_ADPCM => { - trace!("ADPCM magic type"); BinaryDataKind::AudioDataAdpcm } MAGIC_IFRAME => { - trace!("IFrame magic type"); BinaryDataKind::VideoDataIframe } MAGIC_PFRAME => { - trace!("PFrame magic type"); BinaryDataKind::VideoDataPframe } + _ if self.len() == CHUNK_SIZE => { + BinaryDataKind::Continue + } _ => { - // When large data is chunked it goes here - // We work out whether or not it is a continued chunked in the deserialization - trace!("Unknown magic type"); BinaryDataKind::Unknown } } @@ -214,8 +202,7 @@ pub(super) struct BcSendInfo { #[derive(Debug)] pub struct BcContext { - pub(super) last_binary_kind: HashMap>, - pub(super) remaining_binary_bytes: HashMap, + pub(super) in_bin_mode: HashSet, } impl Bc { @@ -235,8 +222,7 @@ impl Bc { impl BcContext { pub fn new() -> BcContext { BcContext { - last_binary_kind: HashMap::new(), - remaining_binary_bytes: HashMap::new(), + in_bin_mode: HashSet::new(), } } } diff --git a/src/bc_protocol.rs b/src/bc_protocol.rs index b54221d..d817bfb 100644 --- a/src/bc_protocol.rs +++ b/src/bc_protocol.rs @@ -270,36 +270,42 @@ impl BcCamera { sub_video.send(start_video)?; loop { - trace!("Getting video message..."); - let msg = sub_video.rx.recv_timeout(self.rx_timeout)?; - if let BcBody::ModernMsg(ModernMsg { - binary: Some(binary), - .. - }) = msg.body - { - trace!("Got {} bytes of video data", binary.len()); - match binary.kind() { - BinaryDataKind::VideoDataIframe - | BinaryDataKind::VideoDataPframe - | BinaryDataKind::VideoCont => { - data_out.write_all(binary.body())?; - } - BinaryDataKind::AudioDataAac - | BinaryDataKind::AudioDataAdpcm - | BinaryDataKind::AudioCont => { - (); - } - BinaryDataKind::InfoData | BinaryDataKind::InfoDataCont => { - (); - } - BinaryDataKind::Unknown => { - (); - } - }; - } else { - warn!("Ignoring weird video message"); - debug!("Contents: {:?}", msg); - } + + let binary_data = sub_video.get_binary_data_of_kind(&vec![BinaryDataKind::VideoDataIframe, BinaryDataKind::VideoDataPframe], self.rx_timeout)?; + // We now have a complete interesting packet. Send it to gst. + // Process the packet + match binary_data.kind() { + BinaryDataKind::VideoDataIframe + | BinaryDataKind::VideoDataPframe => { + data_out.write_all(binary_data.body())?; + }, + BinaryDataKind::AudioDataAac + | BinaryDataKind::AudioDataAdpcm => { + // We should not have a packet of this kind as we skip them until we get + // a packet of interest + unreachable!(); + }, + BinaryDataKind::InfoData => { + // We should not have a packet of this kind as we skip them until we get + // a packet of interest + unreachable!(); + }, + BinaryDataKind::Unknown => { + // We should not have a packet of this kind as we skip them until we get + // a packet of interest + unreachable!(); + }, + BinaryDataKind::Invalid => { + // We should not have a packet of this kind as we skip them until we get + // a packet of interest + unreachable!(); + }, + BinaryDataKind::Continue => { + // We should not have a packet of this kind as it would be attached + // to a packet of interest + unreachable!(); + } + }; } } } diff --git a/src/bc_protocol/connection.rs b/src/bc_protocol/connection.rs index 16a19a1..a055892 100644 --- a/src/bc_protocol/connection.rs +++ b/src/bc_protocol/connection.rs @@ -43,6 +43,9 @@ pub enum Error { #[error(display = "Simultaneous subscription")] SimultaneousSubscription { msg_id: u32 }, + + #[error(display = "Timeout")] + Timeout(#[error(source)] std::sync::mpsc::RecvTimeoutError), } impl BcConnection { @@ -139,6 +142,74 @@ impl<'a> BcSubscription<'a> { bc.serialize(&*self.conn.connection.lock().unwrap())?; Ok(()) } + + pub fn get_binary_data_of_kind(&self, interested_kinds: &[BinaryDataKind], rx_timeout: Duration) -> std::result::Result { + trace!("Finding binary message of interest..."); + let mut binary_data: BinaryData; + // This loop is just for restarting (could have done with nesting too) + 'outer: loop { + // Loop over the messages until we find one we want + loop { + let msg = self.rx.recv_timeout(rx_timeout)?; + if let BcBody::ModernMsg(ModernMsg { + binary: Some(binary), + .. + }) = msg.body + { + match binary.kind() { + n if interested_kinds.contains(&n) => { + binary_data = binary; + break; + }, + _ => { + trace!("Ignoring uninteresting binary data kind"); + }, + }; + } else { + warn!("Ignoring weird binary message"); + debug!("Contents: {:?}", msg); + } + } + + trace!("Found binary message of interest..."); + // If the binary date is not complete get more packets to complete it + while ! binary_data.complete() { + let msg = self.rx.recv_timeout(rx_timeout)?; + if let BcBody::ModernMsg(ModernMsg { + binary: Some(binary), + .. + }) = msg.body + { + match binary.kind() { + BinaryDataKind::Continue => { + // If its a continuation add it to our binary data + binary_data.data.extend(binary.data); + }, + n if interested_kinds.contains(&n) => { + // If its another packet we are interested in + // Give up on current packet and try to complete + // This new one + // We are assuming that the camera has dropped that frame + trace!("Binary data was unfinished, found new interesting data"); + binary_data = binary; + } + _ => { + // If we find something else then give up on the procees and restart + trace!("Binary data was unfinished, found uninteresting data"); + continue 'outer; + }, + } + } else { + warn!("Ignoring weird binary message"); + debug!("Contents: {:?}", msg); + } + } + + trace!("Have complete binary message."); + + return Ok(binary_data); + } + } } /// Makes it difficult to avoid unsubscribing when you're finished