From 963e4799c6e5e2f93100df6c684bf6b8ab19d92f Mon Sep 17 00:00:00 2001 From: QuantumEntangledAndy Date: Mon, 3 Aug 2020 13:25:45 +0700 Subject: [PATCH] Use a deque and process media packets as a substream --- src/bc/de.rs | 4 +- src/bc/media_packet.rs | 155 +++++++++++++++++++++++++++ src/bc/mod.rs | 1 + src/bc/model.rs | 133 +---------------------- src/bc_protocol.rs | 21 ++-- src/bc_protocol/connection.rs | 194 ++++++++++++++++++++++------------ 6 files changed, 295 insertions(+), 213 deletions(-) create mode 100644 src/bc/media_packet.rs diff --git a/src/bc/de.rs b/src/bc/de.rs index f87b0a2..00103e6 100644 --- a/src/bc/de.rs +++ b/src/bc/de.rs @@ -175,9 +175,7 @@ fn bc_modern_msg<'a, 'b, 'c>( context.in_bin_mode.insert(header.msg_id); - let binary_data = BinaryData { data: payload }; - - binary = Some(binary_data); + binary = Some(payload); buf = buf_after; } else { // Seriously, Nom, what even is this diff --git a/src/bc/media_packet.rs b/src/bc/media_packet.rs new file mode 100644 index 0000000..f592ef2 --- /dev/null +++ b/src/bc/media_packet.rs @@ -0,0 +1,155 @@ +use log::trace; +use std::cmp::min; +use std::convert::TryInto; + +pub const CHUNK_SIZE: usize = 40000; + +#[derive(Debug, PartialEq, Eq, Hash, Copy, Clone)] +pub enum MediaDataKind { + VideoDataIframe, + VideoDataPframe, + AudioDataAac, + AudioDataAdpcm, + InfoData, + Invalid, + Continue, + Unknown, +} + +#[derive(Debug, PartialEq, Eq)] +pub struct MediaData { + pub data: Vec, +} + +impl MediaData { + pub fn body(&self) -> &[u8] { + let lower_limit = self.header_size(); + 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_from_raw(data: &[u8]) -> usize { + let kind = MediaData::kind_from_raw(data); + match kind { + MediaDataKind::VideoDataIframe => 32, + MediaDataKind::VideoDataPframe => 24, + MediaDataKind::AudioDataAac => 8, + MediaDataKind::AudioDataAdpcm => 16, + MediaDataKind::InfoData => 32, + MediaDataKind::Unknown | MediaDataKind::Invalid | MediaDataKind::Continue => 0, + } + } + + pub fn header_size(&self) -> usize { + MediaData::header_size_from_raw(&self.data) + } + + pub fn data_size_from_raw(data: &[u8]) -> usize { + let kind = MediaData::kind_from_raw(data); + match kind { + MediaDataKind::VideoDataIframe => MediaData::bytes_to_size(&data[8..12]), + MediaDataKind::VideoDataPframe => MediaData::bytes_to_size(&data[8..12]), + MediaDataKind::AudioDataAac => MediaData::bytes_to_size(&data[4..6]), + MediaDataKind::AudioDataAdpcm => MediaData::bytes_to_size(&data[4..6]), + MediaDataKind::InfoData => MediaData::bytes_to_size(&data[4..8]), + MediaDataKind::Unknown | MediaDataKind::Invalid | MediaDataKind::Continue => { + data.len() + } + } + } + + pub fn data_size(&self) -> usize { + MediaData::data_size_from_raw(&self.data) + } + + pub fn pad_size_from_raw(data: &[u8]) -> usize { + let data_size = MediaData::data_size_from_raw(data); + match data_size % 8 { + 0 => 0, + n => 8 - n, + } + } + + pub fn pad_size(&self) -> usize { + MediaData::data_size_from_raw(&self.data) + } + + fn bytes_to_size(bytes: &[u8]) -> usize { + match bytes.len() { + // 8 Won't fit into usize on a 32-bit machine + 4 => u32::from_le_bytes(bytes.try_into().expect("slice with incorrect length")) + .try_into() + .expect("u32 won't fit into usize"), + 2 => u16::from_le_bytes(bytes.try_into().expect("slice with incorrect length")) + .try_into() + .expect("u16 won't fit into usize"), + 1 => u8::from_le_bytes(bytes.try_into().expect("slice with incorrect length")) + .try_into() + .expect("u8 won't fit into usize"), + _ => unreachable!(), + } + } + + pub fn kind_from_raw(data: &[u8]) -> MediaDataKind { + if data.len() < 4 { + return MediaDataKind::Invalid; + } + const MAGIC_VIDEO_INFO_V1: &[u8] = &[0x31, 0x30, 0x30, 0x31]; + const MAGIC_VIDEO_INFO_V2: &[u8] = &[0x31, 0x30, 0x30, 0x32]; + const MAGIC_AAC: &[u8] = &[0x30, 0x35, 0x77, 0x62]; + const MAGIC_ADPCM: &[u8] = &[0x30, 0x31, 0x77, 0x62]; + const MAGIC_IFRAME: &[u8] = &[0x30, 0x30, 0x64, 0x63]; + const MAGIC_PFRAME: &[u8] = &[0x30, 0x31, 0x64, 0x63]; + + let magic = &data[..4]; + match magic { + MAGIC_VIDEO_INFO_V1 | MAGIC_VIDEO_INFO_V2 => MediaDataKind::InfoData, + MAGIC_AAC => MediaDataKind::AudioDataAac, + MAGIC_ADPCM => MediaDataKind::AudioDataAdpcm, + MAGIC_IFRAME => MediaDataKind::VideoDataIframe, + MAGIC_PFRAME => MediaDataKind::VideoDataPframe, + _ if data.len() == CHUNK_SIZE => MediaDataKind::Continue, + _ => MediaDataKind::Unknown, + } + } + + pub fn kind(&self) -> MediaDataKind { + MediaData::kind_from_raw(&self.data) + } + + pub fn len(&self) -> usize { + self.data.len() + } + + pub fn as_slice(&self) -> &[u8] { + self.data.as_slice() + } +} diff --git a/src/bc/mod.rs b/src/bc/mod.rs index 9a91158..15603e4 100644 --- a/src/bc/mod.rs +++ b/src/bc/mod.rs @@ -11,6 +11,7 @@ //! "Extension" message. pub mod model; +pub mod media_packet; pub mod de; pub mod ser; diff --git a/src/bc/model.rs b/src/bc/model.rs index 6f4ddcc..a94da36 100644 --- a/src/bc/model.rs +++ b/src/bc/model.rs @@ -1,8 +1,5 @@ use super::xml::BcXml; -use log::trace; -use std::cmp::min; use std::collections::HashSet; -use std::convert::TryInto; pub(super) const MAGIC_HEADER: u32 = 0xabcdef0; @@ -12,8 +9,6 @@ pub const MSG_ID_PING: u32 = 93; pub const MSG_ID_GET_GENERAL: u32 = 104; pub const MSG_ID_SET_GENERAL: u32 = 105; -pub const CHUNK_SIZE: usize = 40000; - pub const EMPTY_LEGACY_PASSWORD: &str = "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"; @@ -29,136 +24,10 @@ pub enum BcBody { ModernMsg(ModernMsg), } -#[derive(Debug, PartialEq, Eq, Hash, Copy, Clone)] -pub enum BinaryDataKind { - VideoDataIframe, - VideoDataPframe, - AudioDataAac, - AudioDataAdpcm, - InfoData, - Invalid, - Continue, - Unknown, -} - -#[derive(Debug, PartialEq, Eq)] -pub struct BinaryData { - pub data: Vec, -} - -impl BinaryData { - pub fn body(&self) -> &[u8] { - let lower_limit = self.header_size(); - 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, - BinaryDataKind::VideoDataPframe => 24, - BinaryDataKind::AudioDataAac => 8, - BinaryDataKind::AudioDataAdpcm => 16, - BinaryDataKind::InfoData => 32, - BinaryDataKind::Unknown | BinaryDataKind::Invalid | BinaryDataKind::Continue => 0, - } - } - - pub fn data_size(&self) -> usize { - match self.kind() { - BinaryDataKind::VideoDataIframe => BinaryData::bytes_to_size(&self.data[8..12]), - BinaryDataKind::VideoDataPframe => BinaryData::bytes_to_size(&self.data[8..12]), - 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::Invalid | BinaryDataKind::Continue => { - self.data.len() - } - } - } - - fn bytes_to_size(bytes: &[u8]) -> usize { - match bytes.len() { - // 8 Won't fit into usize on a 32-bit machine - 4 => u32::from_le_bytes(bytes.try_into().expect("slice with incorrect length")) - .try_into() - .expect("u32 won't fit into usize"), - 2 => u16::from_le_bytes(bytes.try_into().expect("slice with incorrect length")) - .try_into() - .expect("u16 won't fit into usize"), - 1 => u8::from_le_bytes(bytes.try_into().expect("slice with incorrect length")) - .try_into() - .expect("u8 won't fit into usize"), - _ => unreachable!(), - } - } - - pub fn kind(&self) -> BinaryDataKind { - const MAGIC_VIDEO_INFO: &[u8] = &[0x31, 0x30, 0x30, 0x31]; - const MAGIC_AAC: &[u8] = &[0x30, 0x35, 0x77, 0x62]; - const MAGIC_ADPCM: &[u8] = &[0x30, 0x31, 0x77, 0x62]; - const MAGIC_IFRAME: &[u8] = &[0x30, 0x30, 0x64, 0x63]; - const MAGIC_PFRAME: &[u8] = &[0x30, 0x31, 0x64, 0x63]; - - let magic = &self.data[..4]; - match magic { - MAGIC_VIDEO_INFO => BinaryDataKind::InfoData, - MAGIC_AAC => BinaryDataKind::AudioDataAac, - MAGIC_ADPCM => BinaryDataKind::AudioDataAdpcm, - MAGIC_IFRAME => BinaryDataKind::VideoDataIframe, - MAGIC_PFRAME => BinaryDataKind::VideoDataPframe, - _ if self.len() == CHUNK_SIZE => BinaryDataKind::Continue, - _ => BinaryDataKind::Unknown, - } - } - - pub fn len(&self) -> usize { - self.data.len() - } - - pub fn as_slice(&self) -> &[u8] { - self.data.as_slice() - } -} - -impl std::convert::AsRef<[u8]> for BinaryData { - fn as_ref(&self) -> &[u8] { - self.as_slice() - } -} - #[derive(Debug, Default, PartialEq, Eq)] pub struct ModernMsg { pub xml: Option, - pub binary: Option, + pub binary: Option>, } #[derive(Debug, PartialEq, Eq)] diff --git a/src/bc_protocol.rs b/src/bc_protocol.rs index 30dbdf6..1f55458 100644 --- a/src/bc_protocol.rs +++ b/src/bc_protocol.rs @@ -1,6 +1,7 @@ use self::connection::BcConnection; use crate::bc; use crate::bc::{model::*, xml::*}; +use crate::bc::media_packet::*; use err_derive::Error; use log::*; use md5; @@ -244,7 +245,7 @@ impl BcCamera { .connection .as_ref() .expect("Must be connected to start video"); - let sub_video = connection.subscribe(MSG_ID_VIDEO)?; + let mut sub_video = connection.subscribe(MSG_ID_VIDEO)?; let start_video = Bc::new_from_xml( BcMeta { @@ -267,40 +268,40 @@ impl BcCamera { sub_video.send(start_video)?; loop { - let binary_data = sub_video.get_binary_data_of_kind( + let binary_data = sub_video.get_media_packet( &vec![ - BinaryDataKind::VideoDataIframe, - BinaryDataKind::VideoDataPframe, + MediaDataKind::VideoDataIframe, + MediaDataKind::VideoDataPframe, ], RX_TIMEOUT, )?; // We now have a complete interesting packet. Send it to gst. // Process the packet match binary_data.kind() { - BinaryDataKind::VideoDataIframe | BinaryDataKind::VideoDataPframe => { + MediaDataKind::VideoDataIframe | MediaDataKind::VideoDataPframe => { data_out.write_all(binary_data.body())?; } - BinaryDataKind::AudioDataAac | BinaryDataKind::AudioDataAdpcm => { + MediaDataKind::AudioDataAac | MediaDataKind::AudioDataAdpcm => { // We should not have a packet of this kind as we skip them until we get // a packet of interest unreachable!(); } - BinaryDataKind::InfoData => { + MediaDataKind::InfoData => { // We should not have a packet of this kind as we skip them until we get // a packet of interest unreachable!(); } - BinaryDataKind::Unknown => { + MediaDataKind::Unknown => { // We should not have a packet of this kind as we skip them until we get // a packet of interest unreachable!(); } - BinaryDataKind::Invalid => { + MediaDataKind::Invalid => { // We should not have a packet of this kind as we skip them until we get // a packet of interest unreachable!(); } - BinaryDataKind::Continue => { + MediaDataKind::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 e53c643..aa7627c 100644 --- a/src/bc_protocol/connection.rs +++ b/src/bc_protocol/connection.rs @@ -1,5 +1,6 @@ use crate::bc; use crate::bc::model::*; +use crate::bc::media_packet::*; use err_derive::Error; use log::*; use socket2::{Domain, Socket, Type}; @@ -10,6 +11,9 @@ use std::sync::mpsc::{channel, Receiver, Sender}; use std::sync::{Arc, Mutex}; use std::thread::JoinHandle; use std::time::Duration; +use std::collections::VecDeque; + +const INVALID_MEDIA_PACKETS: &[MediaDataKind] = &[MediaDataKind::Invalid, MediaDataKind::Continue, MediaDataKind::Unknown]; /// A shareable connection to a camera. Handles serialization of messages. To send/receive, call /// .subscribe() with a message ID. You can use the BcSubscription to send or receive only @@ -26,6 +30,7 @@ pub struct BcSubscription<'a> { pub rx: Receiver, msg_id: u32, conn: &'a BcConnection, + binary_buffer: VecDeque, } type Result = std::result::Result; @@ -78,6 +83,7 @@ impl BcConnection { rx, conn: self, msg_id, + binary_buffer: VecDeque::new(), }) } @@ -143,76 +149,128 @@ impl<'a> BcSubscription<'a> { 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); - } + fn fill_binary_buffer(&mut self, rx_timeout: Duration) -> Result<()> { + // Loop messages until we get binary + loop { + let msg = self.rx.recv_timeout(rx_timeout)?; + if let BcBody::ModernMsg(ModernMsg { + binary: Some(binary), + .. + }) = msg.body + { + self.binary_buffer.extend(binary); + break; } - - 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); } + Ok(()) + } + + fn advance_to_media_packet(&mut self, rx_timeout: Duration) -> Result<()> { + // In the event we get an unknown packet we advance by brute force + // reading of bytes to the next valid magic + trace!("Advancing to next know packet header"); + while self.binary_buffer.len() < 4 { + self.fill_binary_buffer(rx_timeout)?; + } + + // Check the kind, if its invalid use pop a byte and try again + let mut magic = BcSubscription::get_first_n_deque(&self.binary_buffer, 4); + while INVALID_MEDIA_PACKETS.contains(&MediaData::kind_from_raw(&magic)) { + self.binary_buffer.pop_front(); + while self.binary_buffer.len() < 4 { + self.fill_binary_buffer(rx_timeout)?; + } + magic = BcSubscription::get_first_n_deque(&self.binary_buffer, 4); + } + + Ok(()) + } + + fn get_first_n_deque(deque: &VecDeque, n: usize) -> Vec { + // NOTE: I want to use make_contiguous + // This will make this func unneeded as we can use + // make_contiguous then as_slices.0 + // We won't need the clone in this case either. + // This is an experimental feature. + // It is able to be moved to stable though + // As can be seen from this PR + // https://github.com/rust-lang/rust/pull/74559 + let slice0 = deque.as_slices().0; + let slice1 = deque.as_slices().1; + if slice0.len() >= n { + return slice0[0..n].iter().cloned().collect(); + } else { + let remain = n - slice0.len(); + return slice0.iter().chain(&slice1[0..remain]).cloned().collect(); + } + } + + fn next_media_packet(&mut self, rx_timeout: Duration) -> std::result::Result { + // Get enough data for at least the magic + while self.binary_buffer.len() < 4 { + self.fill_binary_buffer(rx_timeout)?; + } + + // Check the kind, if its invalid use advance to get a valid + // If not just grab the kind from the buffer + let mut magic = BcSubscription::get_first_n_deque(&self.binary_buffer, 4); + trace!("Magic 1: {:x?}", magic); + trace!("Kind: {:?}", MediaData::kind_from_raw(&magic)); + if INVALID_MEDIA_PACKETS.contains(&MediaData::kind_from_raw(&magic)) { + // The if is because + // I presume this is expensive and only call when neccecary + self.advance_to_media_packet(rx_timeout)?; + magic = BcSubscription::get_first_n_deque(&self.binary_buffer, 4); + } + trace!("Magic 2: {:x?}", magic); + let kind = MediaData::kind_from_raw(&magic); + + // Get enough for the full header + let header_size = MediaData::header_size_from_raw(&magic); + while self.binary_buffer.len() < header_size { + self.fill_binary_buffer(rx_timeout)?; + } + + // Get enough for the full data + 8 byte buffer + let header = BcSubscription::get_first_n_deque(&self.binary_buffer, header_size); + trace!("Header: {:x?}", header); + let data_size = MediaData::data_size_from_raw(&header); + let pad_size = MediaData::pad_size_from_raw(&header); + let full_size = header_size + data_size + pad_size; + while self.binary_buffer.len() < full_size { + self.fill_binary_buffer(rx_timeout)?; + } + trace!("data_size: {}", data_size); + + // Pop the full binary buffer + let binary = self.binary_buffer.drain(..full_size); + + Ok(MediaData{ + data: binary.collect() + }) + } + + pub fn get_media_packet( + &mut self, + interested_kinds: &[MediaDataKind], + rx_timeout: Duration, + ) -> std::result::Result { + trace!("Finding binary message of interest..."); + let result_media_packet: MediaData; + // Loop over the messages until we find one we want + loop { + let media_packet = self.next_media_packet(rx_timeout)?; + match media_packet.kind() { + n if interested_kinds.contains(&n) => { + result_media_packet = media_packet; + break; + } + _ => { + trace!("Ignoring uninteresting binary data kind"); + } + }; + } + return Ok(result_media_packet); } }