From 67f42feabd09a7f844a0a8654b3fae6ad53cdd8c Mon Sep 17 00:00:00 2001 From: QuantumEntangledAndy Date: Wed, 29 Jul 2020 16:06:22 +0700 Subject: [PATCH] Refractor video magic bytes into BinaryData --- src/bc/de.rs | 30 +++++++++++++++++++++++++++- src/bc/model.rs | 50 +++++++++++++++++++++++++++++++++++++++++++++- src/bc_protocol.rs | 38 +++++++++-------------------------- 3 files changed, 87 insertions(+), 31 deletions(-) diff --git a/src/bc/de.rs b/src/bc/de.rs index d377157..006cf40 100644 --- a/src/bc/de.rs +++ b/src/bc/de.rs @@ -174,7 +174,35 @@ fn bc_modern_msg<'a, 'b, 'c>( // message). context.in_bin_mode.insert(header.msg_id); - binary = Some(payload); + let magic = &payload.as_slice()[..4]; + trace!("Magic is: {:x?}", &magic); + binary = match magic { + MAGIC_VIDEO_INFO => { + trace!("Video info magic type"); + Some(BinaryData::InfoData(payload)) + }, + MAGIC_AAC => { + trace!("AAC magic type"); + Some(BinaryData::AudioData(payload)) + }, + MAGIC_ADPCM => { + trace!("ADPCM magic type"); + Some(BinaryData::AudioData(payload)) + }, + MAGIC_IFRAME => { + trace!("IFrame magic type"); + Some(BinaryData::VideoData(payload)) + }, + MAGIC_PFRAME => { + trace!("PFrame magic type"); + Some(BinaryData::VideoData(payload)) + } + _ => { + trace!("Unknown magic type"); // When large video is chunked it goes here + Some(BinaryData::Unknown(payload)) + } + }; + buf = buf_after; } else { // Seriously, Nom, what even is this diff --git a/src/bc/model.rs b/src/bc/model.rs index e6c3115..686caa2 100644 --- a/src/bc/model.rs +++ b/src/bc/model.rs @@ -22,10 +22,58 @@ pub enum BcBody { ModernMsg(ModernMsg), } +pub const MAGIC_VIDEO_INFO: &[u8] = &[0x31, 0x30, 0x30, 0x31]; +pub const MAGIC_AAC: &[u8] = &[0x30, 0x35, 0x77, 0x62]; +pub const MAGIC_ADPCM: &[u8] = &[0x30, 0x31, 0x77, 0x62]; +pub const MAGIC_IFRAME: &[u8] = &[0x30, 0x30, 0x64, 0x63]; +pub const MAGIC_PFRAME: &[u8] = &[0x30, 0x31, 0x64, 0x63]; + +#[derive(Debug, PartialEq, Eq)] +pub enum BinaryData { + VideoData(Vec), + AudioData(Vec), + InfoData(Vec), + Unknown(Vec), +} + +impl std::convert::AsRef<[u8]> for BinaryData { + fn as_ref(&self) -> &[u8] { + let binary = match self { + BinaryData::VideoData(binary) => binary, + BinaryData::AudioData(binary) => binary, + BinaryData::InfoData(binary) => binary, + BinaryData::Unknown(binary) => binary, + }; + binary.as_slice() + } +} + +impl BinaryData { + pub fn len(&self) -> usize { + let binary = match self { + BinaryData::VideoData(binary) => binary, + BinaryData::AudioData(binary) => binary, + BinaryData::InfoData(binary) => binary, + BinaryData::Unknown(binary) => binary, + }; + binary.len() + } + + pub fn as_slice(&self) -> &[u8] { + let binary = match self { + BinaryData::VideoData(binary) => binary, + BinaryData::AudioData(binary) => binary, + BinaryData::InfoData(binary) => binary, + BinaryData::Unknown(binary) => binary, + }; + binary.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 eb18e97..8f9dded 100644 --- a/src/bc_protocol.rs +++ b/src/bc_protocol.rs @@ -269,11 +269,6 @@ impl BcCamera { sub_video.send(start_video)?; - const MAGIC_HEADER: &[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]; loop { trace!("Getting video message..."); @@ -284,34 +279,19 @@ impl BcCamera { }) = msg.body { trace!("Got {} bytes of video data", binary.len()); - let magic = &binary.as_slice()[..4]; - trace!("Magic is: {:x?}", &magic); - match magic { - MAGIC_HEADER => { - trace!("Header magic type"); - (); + match binary { + BinaryData::VideoData(binary) => { + data_out.write_all(binary.as_slice())? }, - MAGIC_AAC => { - trace!("AAC magic type"); - //data_out.write_all(&binary.as_slice()[4..])?; + BinaryData::AudioData(binary) => { + () }, - MAGIC_ADPCM => { - trace!("ADPCM magic type"); - (); + BinaryData::InfoData(binary) => { + () }, - MAGIC_IFRAME => { - trace!("IFrame magic type"); - data_out.write_all(binary.as_slice())?; + BinaryData::Unknown(binary) => { + data_out.write_all(binary.as_slice())? }, - MAGIC_PFRAME => { - trace!("PFrame magic type"); - data_out.write_all(binary.as_slice())?; - } - _ => { - trace!("Unknown magic type"); // When large video is chunked it goes here - data_out.write_all(binary.as_slice())?; - (); - } } } else {