diff --git a/src/bc/model.rs b/src/bc/model.rs index 8df5284..d920295 100644 --- a/src/bc/model.rs +++ b/src/bc/model.rs @@ -1,5 +1,6 @@ use super::xml::BcXml; use std::collections::HashSet; +use std::convert::TryInto; pub(super) const MAGIC_HEADER: u32 = 0xabcdef0; @@ -56,8 +57,8 @@ impl VideoIFrame { &self.raw[4..8] } - pub fn data_size(&self) -> &[u8] { - &self.raw[8..12] + pub fn data_size(&self) -> usize { + u32::from_be_bytes(self.raw[8..12].try_into().expect("slice with incorrect length")).try_into().expect("u32 won't fit into usize") } pub fn unknowna(&self) -> &[u8] { @@ -113,8 +114,8 @@ impl VideoPFrame { &self.raw[4..8] } - pub fn data_size(&self) -> &[u8] { - &self.raw[8..12] + pub fn data_size(&self) -> usize { + u32::from_be_bytes(self.raw[8..12].try_into().expect("slice with incorrect length")).try_into().expect("u32 won't fit into usize") } pub fn unknowna(&self) -> &[u8] { diff --git a/src/bc_protocol.rs b/src/bc_protocol.rs index 8f9dded..69eca8c 100644 --- a/src/bc_protocol.rs +++ b/src/bc_protocol.rs @@ -17,6 +17,7 @@ pub struct BcCamera { connection: Option, logged_in: bool, rx_timeout: Duration, + byte_debt: usize, } use crate::Never; @@ -71,6 +72,7 @@ impl BcCamera { connection: None, logged_in: false, rx_timeout: Duration::from_secs(1), + byte_debt: 0, }) } @@ -242,7 +244,7 @@ impl BcCamera { Ok(()) } - pub fn start_video(&self, data_out: &mut dyn Write, stream_name: &str) -> Result { + pub fn start_video(&mut self, data_out: &mut dyn Write, stream_name: &str) -> Result { let connection = self .connection .as_ref() @@ -279,19 +281,68 @@ impl BcCamera { }) = msg.body { trace!("Got {} bytes of video data", binary.len()); - match binary { - BinaryData::VideoData(binary) => { - data_out.write_all(binary.as_slice())? - }, - BinaryData::AudioData(binary) => { - () - }, - BinaryData::InfoData(binary) => { - () - }, - BinaryData::Unknown(binary) => { - data_out.write_all(binary.as_slice())? - }, + if self.byte_debt <= 0 { + match binary { + BinaryData::VideoData(binary) => { + match binary { + VideoFrame::IFrame(frame) => { + let expected_bytes = frame.data_size(); + let actual_size = frame.video_data().len(); + if expected_bytes > actual_size { + self.byte_debt = expected_bytes - actual_size; + } + trace!("Expected {} bytes in video package", expected_bytes); + trace!("Actual {} bytes in video package", actual_size); + trace!("Remaining bytes {}", (expected_bytes - actual_size)); + data_out.write_all(frame.as_slice())? + }, + VideoFrame::PFrame(frame) => { + let expected_bytes = frame.data_size(); + let actual_size = frame.video_data().len(); + if expected_bytes > actual_size { + self.byte_debt = expected_bytes - actual_size; + } + trace!("Expected {} bytes in video package", expected_bytes); + trace!("Actual {} bytes in video package", actual_size); + trace!("Remaining bytes {}", (expected_bytes - actual_size)); + data_out.write_all(frame.as_slice())? + } + }; + }, + BinaryData::AudioData(binary) => { + () + }, + BinaryData::InfoData(binary) => { + () + }, + BinaryData::Unknown(binary) => { + () + }, + }; + } else { + match binary { + BinaryData::VideoData(binary) => { + trace!("Video magic detected but expected video data for chunked frame {}", self.byte_debt); + self.byte_debt -= binary.len(); + data_out.write_all(binary.as_slice())? + }, + BinaryData::AudioData(binary) => { + trace!("Audio magic detected but expected video data for chunked frame {}", self.byte_debt); + self.byte_debt -= binary.len(); + data_out.write_all(binary.as_slice())? + }, + BinaryData::InfoData(binary) => { + trace!("Info magic detected but expected video data for chunked frame {}", self.byte_debt); + self.byte_debt -= binary.len(); + data_out.write_all(binary.as_slice())? + }, + BinaryData::Unknown(binary) => { + trace!("Fulfilling expected video data for chunked frame {}", self.byte_debt); + self.byte_debt -= binary.len(); + data_out.write_all(binary.as_slice())? + }, + }; + trace!("Remaning data expected for chunked frame {}", self.byte_debt); } } else {