Split bc and bcmedia into seperate modules

This commit is contained in:
QuantumEntangledAndy committed 2021-08-03 15:53:25 +07:00
1 parent 9ba2a59eda
commit 4ecb2a1ae4
11 files changed
+512 -419

No files matched your search

+8 -243
View File
@@ -1,18 +1,11 @@
use super::model::BcMediaIframe;
use super::model::*;
use super::xml::{BcPayloads, BcXml};
use super::xml_crypto;
use err_derive::Error;
use log::*;
use nom::IResult;
use nom::{
bytes::streaming::take, combinator::*, number::streaming::*, sequence::*, take, take_str,
};
use nom::{bytes::streaming::take, combinator::*, number::streaming::*, sequence::*};
use std::io::Read;
// PAD_SIZE: Media packets use 8 byte padding
const PAD_SIZE: u32 = 8;
/// The error types used during deserialisation
#[derive(Debug, Error)]
pub enum Error {
@@ -23,7 +16,6 @@ pub enum Error {
#[error(display = "I/O error")]
IoError(#[error(source)] std::io::Error),
}
type NomErrorType<'a> = nom::error::Error<&'a [u8]>;
impl<'a> From<nom::Err<NomErrorType<'a>>> for Error {
@@ -37,13 +29,6 @@ impl<'a> From<nom::Err<NomErrorType<'a>>> for Error {
}
}
impl Bc {
pub(crate) fn deserialize<R: Read>(context: &mut BcContext, r: R) -> Result<Bc, Error> {
// Throw away the nom-specific return types
read_from_reader(|reader| bc_msg(context, reader), r)
}
}
fn read_from_reader<P, O, E, R>(mut parser: P, mut rdr: R) -> Result<O, E>
where
R: Read,
@@ -76,6 +61,13 @@ where
}
}
impl Bc {
pub(crate) fn deserialize<R: Read>(context: &mut BcContext, r: R) -> Result<Bc, Error> {
// Throw away the nom-specific return types
read_from_reader(|reader| bc_msg(context, reader), r)
}
}
fn bc_msg<'a, 'b>(context: &'a mut BcContext, buf: &'b [u8]) -> IResult<&'b [u8], Bc> {
let (buf, header) = bc_header(buf)?;
let (buf, body) = bc_body(context, &header, buf)?;
@@ -228,233 +220,6 @@ fn bc_header(buf: &[u8]) -> IResult<&[u8], BcHeader> {
))
}
impl BcMedia {
pub(crate) fn deserialize<R: Read>(r: R) -> Result<BcMedia, Error> {
// Throw away the nom-specific return types
read_from_reader(|reader| bcmedia(reader), r)
}
}
fn bcmedia(buf: &[u8]) -> IResult<&[u8], BcMedia> {
let (buf, magic) = verify(le_u32, |x| {
matches!(
*x,
MAGIC_HEADER_BCMEDIA_INFO_V1
| MAGIC_HEADER_BCMEDIA_INFO_V2
| MAGIC_HEADER_BCMEDIA_IFRAME
| MAGIC_HEADER_BCMEDIA_PFRAME
| MAGIC_HEADER_BCMEDIA_AAC
| MAGIC_HEADER_BCMEDIA_ADPCM
)
})(buf)?;
match magic {
MAGIC_HEADER_BCMEDIA_INFO_V1 => {
let (buf, payload) = bcmedia_info_v1(buf)?;
Ok((buf, BcMedia::InfoV1(payload)))
}
MAGIC_HEADER_BCMEDIA_INFO_V2 => {
let (buf, payload) = bcmedia_info_v2(buf)?;
Ok((buf, BcMedia::InfoV2(payload)))
}
MAGIC_HEADER_BCMEDIA_IFRAME => {
let (buf, payload) = bcmedia_iframe(buf)?;
Ok((buf, BcMedia::Iframe(payload)))
}
MAGIC_HEADER_BCMEDIA_PFRAME => {
let (buf, payload) = bcmedia_pframe(buf)?;
Ok((buf, BcMedia::Pframe(payload)))
}
MAGIC_HEADER_BCMEDIA_AAC => {
let (buf, payload) = bcmedia_aac(buf)?;
Ok((buf, BcMedia::Aac(payload)))
}
MAGIC_HEADER_BCMEDIA_ADPCM => {
let (buf, payload) = bcmedia_adpcm(buf)?;
Ok((buf, BcMedia::Adpcm(payload)))
}
_ => unreachable!(),
}
}
fn bcmedia_info_v1(buf: &[u8]) -> IResult<&[u8], BcMediaInfoV1> {
let (buf, header_size) = verify(le_u32, |x| *x == 32)(buf)?;
let (buf, video_width) = le_u32(buf)?;
let (buf, video_height) = le_u32(buf)?;
let (buf, _unknown) = le_u8(buf)?;
let (buf, fps) = le_u8(buf)?;
let (buf, start_year) = le_u8(buf)?;
let (buf, start_month) = le_u8(buf)?;
let (buf, start_day) = le_u8(buf)?;
let (buf, start_hour) = le_u8(buf)?;
let (buf, start_min) = le_u8(buf)?;
let (buf, start_seconds) = le_u8(buf)?;
let (buf, end_year) = le_u8(buf)?;
let (buf, end_month) = le_u8(buf)?;
let (buf, end_day) = le_u8(buf)?;
let (buf, end_hour) = le_u8(buf)?;
let (buf, end_min) = le_u8(buf)?;
let (buf, end_seconds) = le_u8(buf)?;
let (buf, _unknown_b) = le_u16(buf)?;
Ok((
buf,
BcMediaInfoV1 {
header_size,
video_width,
video_height,
fps,
start_year,
start_month,
start_day,
start_hour,
start_min,
start_seconds,
end_year,
end_month,
end_day,
end_hour,
end_min,
end_seconds,
},
))
}
fn bcmedia_info_v2(buf: &[u8]) -> IResult<&[u8], BcMediaInfoV2> {
let (buf, header_size) = verify(le_u32, |x| *x == 32)(buf)?;
let (buf, video_width) = le_u32(buf)?;
let (buf, video_height) = le_u32(buf)?;
let (buf, _unknown) = le_u8(buf)?;
let (buf, fps) = le_u8(buf)?;
let (buf, start_year) = le_u8(buf)?;
let (buf, start_month) = le_u8(buf)?;
let (buf, start_day) = le_u8(buf)?;
let (buf, start_hour) = le_u8(buf)?;
let (buf, start_min) = le_u8(buf)?;
let (buf, start_seconds) = le_u8(buf)?;
let (buf, end_year) = le_u8(buf)?;
let (buf, end_month) = le_u8(buf)?;
let (buf, end_day) = le_u8(buf)?;
let (buf, end_hour) = le_u8(buf)?;
let (buf, end_min) = le_u8(buf)?;
let (buf, end_seconds) = le_u8(buf)?;
let (buf, _unknown_b) = le_u16(buf)?;
Ok((
buf,
BcMediaInfoV2 {
header_size,
video_width,
video_height,
fps,
start_year,
start_month,
start_day,
start_hour,
start_min,
start_seconds,
end_year,
end_month,
end_day,
end_hour,
end_min,
end_seconds,
},
))
}
fn bcmedia_iframe(buf: &[u8]) -> IResult<&[u8], BcMediaIframe> {
let (buf, video_type_str) = take_str!(buf, 4)?;
let (buf, payload_size) = le_u32(buf)?;
let (buf, _unknown_a) = le_u32(buf)?;
let (buf, microseconds) = le_u32(buf)?;
let (buf, _unknown_b) = le_u32(buf)?;
let (buf, time) = le_u32(buf)?;
let (buf, _unknown_c) = le_u32(buf)?;
let (buf, data_slice) = take!(buf, payload_size)?;
let pad_size = match payload_size % PAD_SIZE {
0 => 0,
n => PAD_SIZE - n,
};
let (buf, _padding) = take!(buf, pad_size)?;
Ok((
buf,
BcMediaIframe {
video_type: video_type_str.to_string(),
payload_size,
microseconds,
time,
data: data_slice.to_vec(),
},
))
}
fn bcmedia_pframe(buf: &[u8]) -> IResult<&[u8], BcMediaPframe> {
let (buf, video_type_str) = take_str!(buf, 4)?;
let (buf, payload_size) = le_u32(buf)?;
let (buf, _unknown_a) = le_u32(buf)?;
let (buf, microseconds) = le_u32(buf)?;
let (buf, _unknown_b) = le_u32(buf)?;
let (buf, data_slice) = take!(buf, payload_size)?;
let pad_size = match payload_size % PAD_SIZE {
0 => 0,
n => PAD_SIZE - n,
};
let (buf, _padding) = take!(buf, pad_size)?;
Ok((
buf,
BcMediaPframe {
video_type: video_type_str.to_string(),
payload_size,
microseconds,
data: data_slice.to_vec(),
},
))
}
fn bcmedia_aac(buf: &[u8]) -> IResult<&[u8], BcMediaAac> {
let (buf, payload_size) = le_u16(buf)?;
let (buf, _payload_size_b) = le_u16(buf)?;
let (buf, data_slice) = take!(buf, payload_size)?;
let pad_size = match payload_size as u32 % PAD_SIZE {
0 => 0,
n => PAD_SIZE - n,
};
let (buf, _padding) = take!(buf, pad_size)?;
Ok((
buf,
BcMediaAac {
payload_size,
data: data_slice.to_vec(),
},
))
}
fn bcmedia_adpcm(buf: &[u8]) -> IResult<&[u8], BcMediaAdpcm> {
let (buf, payload_size) = le_u16(buf)?;
let (buf, _payload_size_b) = le_u16(buf)?;
let (buf, _magic) = verify(le_u16, |x| *x == MAGIC_HEADER_BCMEDIA_ADPCM_DATA)(buf)?;
let (buf, block_size) = le_u16(buf)?;
let (buf, data_slice) = take!(buf, block_size)?;
let pad_size = match payload_size as u32 % PAD_SIZE {
0 => 0,
n => PAD_SIZE - n,
};
let (buf, _padding) = take!(buf, pad_size)?;
Ok((
buf,
BcMediaAdpcm {
payload_size,
block_size,
data: data_slice.to_vec(),
},
))
}
#[test]
fn test_bc_modern_login() {
let sample = include_bytes!("samples/model_sample_modern_login.bin");
-170
View File
@@ -278,173 +278,3 @@ pub(super) fn has_payload_offset(class: u16) -> bool {
// See BcHeader::is_modern() for a description of which packets have the bin offset
class == 0x6414 || class == 0x0000
}
/// Video streams encapsulate a stream of BcMedia
pub enum BcMedia {
/// Holds info on the stream
InfoV1(BcMediaInfoV1),
/// Holds info on the stream
InfoV2(BcMediaInfoV2),
/// Holds an IFrame either H264 or H265
Iframe(BcMediaIframe),
/// Holds a PFrame either H264 or H265
Pframe(BcMediaPframe),
/// Holds AAC audio
Aac(BcMediaAac),
/// Holds ADPCM audio
Adpcm(BcMediaAdpcm),
}
//
pub(super) const MAGIC_HEADER_BCMEDIA_INFO_V1: u32 = 0x31303031;
/// The start of a BcMedia stream contains this message
/// which describes the data to follow
pub struct BcMediaInfoV1 {
/// This is the size of the header so it's actually a fixed value
/// The other messages have body size here so maybe that's why
/// it's included
pub header_size: u32,
/// Width of the video
pub video_width: u32,
/// Height of the video
pub video_height: u32,
// pub unknown: u8,
/// Frames per second. On older cameras this seems to be an index of the FPS on a lookup table
pub fps: u8,
/// Start year of the stream
pub start_year: u8,
/// Start month of the stream
pub start_month: u8,
/// Start day of the stream
pub start_day: u8,
/// Start hour of the stream
pub start_hour: u8,
/// Start minute of the stream
pub start_min: u8,
/// Start seconds of the stream
pub start_seconds: u8,
/// End year of the video probably only useful for the recorded files on the SD card
pub end_year: u8,
/// End month of the video probably only useful for the recorded files on the SD card
pub end_month: u8,
/// End day of the video probably only useful for the recorded files on the SD card
pub end_day: u8,
/// End hour of the video probably only useful for the recorded files on the SD card
pub end_hour: u8,
/// End min of the video probably only useful for the recorded files on the SD card
pub end_min: u8,
/// End seconds of the video probably only useful for the recorded files on the SD card
pub end_seconds: u8,
// unknown: u16
}
//
pub(super) const MAGIC_HEADER_BCMEDIA_INFO_V2: u32 = 0x32303031;
/// The start of a BcMedia stream contains this message
/// which describes the data to follow
pub struct BcMediaInfoV2 {
/// This is the size of the header so it's actually a fixed value
/// The other messages have body size here so maybe that's why
/// it's included
pub header_size: u32,
/// Width of the video
pub video_width: u32,
/// Height of the video
pub video_height: u32,
// pub unknown: u8,
/// Frames per second. On older cameras this seems to be an index of the FPS on a lookup table
pub fps: u8,
/// Start year of the stream
pub start_year: u8,
/// Start month of the stream
pub start_month: u8,
/// Start day of the stream
pub start_day: u8,
/// Start hour of the stream
pub start_hour: u8,
/// Start minute of the stream
pub start_min: u8,
/// Start seconds of the stream
pub start_seconds: u8,
/// End year of the video probably only useful for the recorded files on the SD card
pub end_year: u8,
/// End month of the video probably only useful for the recorded files on the SD card
pub end_month: u8,
/// End day of the video probably only useful for the recorded files on the SD card
pub end_day: u8,
/// End hour of the video probably only useful for the recorded files on the SD card
pub end_hour: u8,
/// End min of the video probably only useful for the recorded files on the SD card
pub end_min: u8,
/// End seconds of the video probably only useful for the recorded files on the SD card
pub end_seconds: u8,
// unknown: u16
}
pub(super) const MAGIC_HEADER_BCMEDIA_IFRAME: u32 = 0x63643030;
/// This is a BcMedia video IFrame.
pub struct BcMediaIframe {
/// ASCII of either "H264", or "H265"
pub video_type: String,
/// Size of payload after header in bytes
pub payload_size: u32,
// unknown: u32, // NVR channel count? Known values 1-00/08 2-00 3-00 4-00
/// Timestamp in microseconds
pub microseconds: u32,
// unknown: u32, // Known values 1-00/23/5A 2-00 3-00 4-00
/// POSIX time (seconds since 00:00:00 Jan 1 1970)
pub time: u32,
//unknown: u32, // Known values 1-00/06/29 2-00/01 3-00/C3 4-00
/// Raw IFrame data
pub data: Vec<u8>,
}
pub(super) const MAGIC_HEADER_BCMEDIA_PFRAME: u32 = 0x63643130;
/// This is a BcMedia video PFrame.
pub struct BcMediaPframe {
/// ASCII of either "H264", or "H265"
pub video_type: String,
/// Size of payload after header in bytes
pub payload_size: u32,
// unknown: u32, // NVR channel count? Known values 1-00/08 2-00 3-00 4-00
/// Timestamp in microseconds
pub microseconds: u32,
// unknown: u32, // Known values 1-00/23/5A 2-00 3-00 4-00
/// Raw PFrame data
pub data: Vec<u8>,
}
pub(super) const MAGIC_HEADER_BCMEDIA_AAC: u32 = 0x62773530;
/// This contains BcMedia audio data in AAC format
pub struct BcMediaAac {
/// Size of payload after header in bytes
pub payload_size: u16,
// Size of payload after header in bytes exactly the same as before
// pub payload_size_b: u16,
/// Raw AAC data
pub data: Vec<u8>,
}
pub(super) const MAGIC_HEADER_BCMEDIA_ADPCM: u32 = 0x62773130;
pub(super) const MAGIC_HEADER_BCMEDIA_ADPCM_DATA: u16 = 0x0100;
/// This contains BcMedia audio data in ADPCM format
pub struct BcMediaAdpcm {
/// Size of payload after header in bytes
pub payload_size: u16,
// Size of payload after header in bytes exactly the same as before
// pub payload_size_b: u16,
// more_magic: MAGIC_HEADER_BCMEDIA_ADPCM_DATA
/// Adpcm block size in bytes (there are twice this in samples)
///
/// These bytes (and the MAGIC_HEADER_BCMEDIA_ADPCM_DATA) are included as
/// part of the payload_size. It may be more prudent to sealise them to
/// another structure.
pub block_size: u16,
/// The raw adpcm data in DVI-4 layout. Size should be [block_size] + 4
pub data: Vec<u8>,
}
+1 -1
View File
@@ -1,5 +1,5 @@
use self::connection::BcConnection;
use crate::bc;
use crate::{bc, bcmedia};
use log::*;
use std::convert::TryInto;
use std::net::ToSocketAddrs;
+8
View File
@@ -17,6 +17,14 @@ pub enum Error {
#[error(display = "Serialization error")]
Serialization(#[error(source)] super::bc::ser::Error),
/// Error raised during deserlization
#[error(display = "Media Deserialization error")]
MediaDeserialization(#[error(source)] super::bcmedia::de::Error),
/// Error raised during serlization
#[error(display = "Media Serialization error")]
MediaSerialization(#[error(source)] super::bcmedia::ser::Error),
/// A connection error such as Simultaneous subscription
#[error(display = "Connection error")]
ConnectionError(#[error(source)] super::connection::Error),
+2 -4
View File
@@ -1,6 +1,7 @@
use super::{BcCamera, BinarySubscriber, Result};
use crate::{
bc::{model::*, xml::*},
bcmedia::model::*,
Never,
};
@@ -69,10 +70,7 @@ impl BcCamera {
let mut media_sub = BinarySubscriber::from_bc_sub(&sub_video);
loop {
let bc_media = BcMedia::deserialize(&mut media_sub).map_err(|e| {
log::error!("{:?}", e);
e
})?;
let bc_media = BcMedia::deserialize(&mut media_sub)?;
// We now have a complete interesting packet. Send it to on the callback
data_outs.write(bc_media)?;
}
+291
View File
@@ -0,0 +1,291 @@
use super::model::BcMediaIframe;
use super::model::*;
use err_derive::Error;
use nom::IResult;
use nom::{combinator::*, number::streaming::*, take, take_str};
use std::io::Read;
// PAD_SIZE: Media packets use 8 byte padding
const PAD_SIZE: u32 = 8;
/// The error types used during deserialisation
#[derive(Debug, Error)]
pub enum Error {
/// A Nom parsing error usually a malformed packet
#[error(display = "Parsing error")]
NomError(String),
/// An IO error such as the stream being dropped
#[error(display = "I/O error")]
IoError(#[error(source)] std::io::Error),
}
type NomErrorType<'a> = nom::error::Error<&'a [u8]>;
impl<'a> From<nom::Err<NomErrorType<'a>>> for Error {
fn from(k: nom::Err<NomErrorType<'a>>) -> Self {
let reason = match k {
nom::Err::Error(e) => format!("Nom Error: {:?}", e),
nom::Err::Failure(e) => format!("Nom Error: {:?}", e),
_ => "Unknown Nom error".to_string(),
};
Error::NomError(reason)
}
}
fn read_from_reader<P, O, E, R>(mut parser: P, mut rdr: R) -> Result<O, E>
where
R: Read,
E: for<'a> From<nom::Err<NomErrorType<'a>>> + From<std::io::Error>,
P: FnMut(&[u8]) -> nom::IResult<&[u8], O>,
{
let mut input: Vec<u8> = Vec::new();
loop {
let to_read = match parser(&input) {
Ok((_, parsed)) => return Ok(parsed),
Err(nom::Err::Incomplete(needed)) => {
match needed {
nom::Needed::Unknown => std::num::NonZeroUsize::new(1).unwrap(), // read one byte
nom::Needed::Size(len) => len,
}
}
Err(e) => return Err(e.into()),
};
if 0 == (&mut rdr)
.take(to_read.get() as u64)
.read_to_end(&mut input)?
{
return Err(std::io::Error::new(
std::io::ErrorKind::UnexpectedEof,
"Read returned 0 bytes",
)
.into());
}
}
}
impl BcMedia {
pub(crate) fn deserialize<R: Read>(r: R) -> Result<BcMedia, Error> {
// Throw away the nom-specific return types
read_from_reader(|reader| bcmedia(reader), r)
}
}
fn bcmedia(buf: &[u8]) -> IResult<&[u8], BcMedia> {
let (buf, magic) = verify(le_u32, |x| {
matches!(
*x,
MAGIC_HEADER_BCMEDIA_INFO_V1
| MAGIC_HEADER_BCMEDIA_INFO_V2
| MAGIC_HEADER_BCMEDIA_IFRAME
| MAGIC_HEADER_BCMEDIA_PFRAME
| MAGIC_HEADER_BCMEDIA_AAC
| MAGIC_HEADER_BCMEDIA_ADPCM
)
})(buf)?;
match magic {
MAGIC_HEADER_BCMEDIA_INFO_V1 => {
let (buf, payload) = bcmedia_info_v1(buf)?;
Ok((buf, BcMedia::InfoV1(payload)))
}
MAGIC_HEADER_BCMEDIA_INFO_V2 => {
let (buf, payload) = bcmedia_info_v2(buf)?;
Ok((buf, BcMedia::InfoV2(payload)))
}
MAGIC_HEADER_BCMEDIA_IFRAME => {
let (buf, payload) = bcmedia_iframe(buf)?;
Ok((buf, BcMedia::Iframe(payload)))
}
MAGIC_HEADER_BCMEDIA_PFRAME => {
let (buf, payload) = bcmedia_pframe(buf)?;
Ok((buf, BcMedia::Pframe(payload)))
}
MAGIC_HEADER_BCMEDIA_AAC => {
let (buf, payload) = bcmedia_aac(buf)?;
Ok((buf, BcMedia::Aac(payload)))
}
MAGIC_HEADER_BCMEDIA_ADPCM => {
let (buf, payload) = bcmedia_adpcm(buf)?;
Ok((buf, BcMedia::Adpcm(payload)))
}
_ => unreachable!(),
}
}
fn bcmedia_info_v1(buf: &[u8]) -> IResult<&[u8], BcMediaInfoV1> {
let (buf, header_size) = verify(le_u32, |x| *x == 32)(buf)?;
let (buf, video_width) = le_u32(buf)?;
let (buf, video_height) = le_u32(buf)?;
let (buf, _unknown) = le_u8(buf)?;
let (buf, fps) = le_u8(buf)?;
let (buf, start_year) = le_u8(buf)?;
let (buf, start_month) = le_u8(buf)?;
let (buf, start_day) = le_u8(buf)?;
let (buf, start_hour) = le_u8(buf)?;
let (buf, start_min) = le_u8(buf)?;
let (buf, start_seconds) = le_u8(buf)?;
let (buf, end_year) = le_u8(buf)?;
let (buf, end_month) = le_u8(buf)?;
let (buf, end_day) = le_u8(buf)?;
let (buf, end_hour) = le_u8(buf)?;
let (buf, end_min) = le_u8(buf)?;
let (buf, end_seconds) = le_u8(buf)?;
let (buf, _unknown_b) = le_u16(buf)?;
Ok((
buf,
BcMediaInfoV1 {
header_size,
video_width,
video_height,
fps,
start_year,
start_month,
start_day,
start_hour,
start_min,
start_seconds,
end_year,
end_month,
end_day,
end_hour,
end_min,
end_seconds,
},
))
}
fn bcmedia_info_v2(buf: &[u8]) -> IResult<&[u8], BcMediaInfoV2> {
let (buf, header_size) = verify(le_u32, |x| *x == 32)(buf)?;
let (buf, video_width) = le_u32(buf)?;
let (buf, video_height) = le_u32(buf)?;
let (buf, _unknown) = le_u8(buf)?;
let (buf, fps) = le_u8(buf)?;
let (buf, start_year) = le_u8(buf)?;
let (buf, start_month) = le_u8(buf)?;
let (buf, start_day) = le_u8(buf)?;
let (buf, start_hour) = le_u8(buf)?;
let (buf, start_min) = le_u8(buf)?;
let (buf, start_seconds) = le_u8(buf)?;
let (buf, end_year) = le_u8(buf)?;
let (buf, end_month) = le_u8(buf)?;
let (buf, end_day) = le_u8(buf)?;
let (buf, end_hour) = le_u8(buf)?;
let (buf, end_min) = le_u8(buf)?;
let (buf, end_seconds) = le_u8(buf)?;
let (buf, _unknown_b) = le_u16(buf)?;
Ok((
buf,
BcMediaInfoV2 {
header_size,
video_width,
video_height,
fps,
start_year,
start_month,
start_day,
start_hour,
start_min,
start_seconds,
end_year,
end_month,
end_day,
end_hour,
end_min,
end_seconds,
},
))
}
fn bcmedia_iframe(buf: &[u8]) -> IResult<&[u8], BcMediaIframe> {
let (buf, video_type_str) = take_str!(buf, 4)?;
let (buf, payload_size) = le_u32(buf)?;
let (buf, _unknown_a) = le_u32(buf)?;
let (buf, microseconds) = le_u32(buf)?;
let (buf, _unknown_b) = le_u32(buf)?;
let (buf, time) = le_u32(buf)?;
let (buf, _unknown_c) = le_u32(buf)?;
let (buf, data_slice) = take!(buf, payload_size)?;
let pad_size = match payload_size % PAD_SIZE {
0 => 0,
n => PAD_SIZE - n,
};
let (buf, _padding) = take!(buf, pad_size)?;
Ok((
buf,
BcMediaIframe {
video_type: video_type_str.to_string(),
payload_size,
microseconds,
time,
data: data_slice.to_vec(),
},
))
}
fn bcmedia_pframe(buf: &[u8]) -> IResult<&[u8], BcMediaPframe> {
let (buf, video_type_str) = take_str!(buf, 4)?;
let (buf, payload_size) = le_u32(buf)?;
let (buf, _unknown_a) = le_u32(buf)?;
let (buf, microseconds) = le_u32(buf)?;
let (buf, _unknown_b) = le_u32(buf)?;
let (buf, data_slice) = take!(buf, payload_size)?;
let pad_size = match payload_size % PAD_SIZE {
0 => 0,
n => PAD_SIZE - n,
};
let (buf, _padding) = take!(buf, pad_size)?;
Ok((
buf,
BcMediaPframe {
video_type: video_type_str.to_string(),
payload_size,
microseconds,
data: data_slice.to_vec(),
},
))
}
fn bcmedia_aac(buf: &[u8]) -> IResult<&[u8], BcMediaAac> {
let (buf, payload_size) = le_u16(buf)?;
let (buf, _payload_size_b) = le_u16(buf)?;
let (buf, data_slice) = take!(buf, payload_size)?;
let pad_size = match payload_size as u32 % PAD_SIZE {
0 => 0,
n => PAD_SIZE - n,
};
let (buf, _padding) = take!(buf, pad_size)?;
Ok((
buf,
BcMediaAac {
payload_size,
data: data_slice.to_vec(),
},
))
}
fn bcmedia_adpcm(buf: &[u8]) -> IResult<&[u8], BcMediaAdpcm> {
let (buf, payload_size) = le_u16(buf)?;
let (buf, _payload_size_b) = le_u16(buf)?;
let (buf, _magic) = verify(le_u16, |x| *x == MAGIC_HEADER_BCMEDIA_ADPCM_DATA)(buf)?;
let (buf, block_size) = le_u16(buf)?;
let (buf, data_slice) = take!(buf, block_size)?;
let pad_size = match payload_size as u32 % PAD_SIZE {
0 => 0,
n => PAD_SIZE - n,
};
let (buf, _padding) = take!(buf, pad_size)?;
Ok((
buf,
BcMediaAdpcm {
payload_size,
block_size,
data: data_slice.to_vec(),
},
))
}
+6
View File
@@ -0,0 +1,6 @@
/// Deserlizer for BCMedia
pub mod de;
/// Structure model for BCMedia
pub mod model;
/// Serlizer for BCMedia
pub mod ser;
+169
View File
@@ -0,0 +1,169 @@
/// Video streams encapsulate a stream of BcMedia
pub enum BcMedia {
/// Holds info on the stream
InfoV1(BcMediaInfoV1),
/// Holds info on the stream
InfoV2(BcMediaInfoV2),
/// Holds an IFrame either H264 or H265
Iframe(BcMediaIframe),
/// Holds a PFrame either H264 or H265
Pframe(BcMediaPframe),
/// Holds AAC audio
Aac(BcMediaAac),
/// Holds ADPCM audio
Adpcm(BcMediaAdpcm),
}
//
pub(super) const MAGIC_HEADER_BCMEDIA_INFO_V1: u32 = 0x31303031;
/// The start of a BcMedia stream contains this message
/// which describes the data to follow
pub struct BcMediaInfoV1 {
/// This is the size of the header so it's actually a fixed value
/// The other messages have body size here so maybe that's why
/// it's included
pub header_size: u32,
/// Width of the video
pub video_width: u32,
/// Height of the video
pub video_height: u32,
// pub unknown: u8,
/// Frames per second. On older cameras this seems to be an index of the FPS on a lookup table
pub fps: u8,
/// Start year of the stream
pub start_year: u8,
/// Start month of the stream
pub start_month: u8,
/// Start day of the stream
pub start_day: u8,
/// Start hour of the stream
pub start_hour: u8,
/// Start minute of the stream
pub start_min: u8,
/// Start seconds of the stream
pub start_seconds: u8,
/// End year of the video probably only useful for the recorded files on the SD card
pub end_year: u8,
/// End month of the video probably only useful for the recorded files on the SD card
pub end_month: u8,
/// End day of the video probably only useful for the recorded files on the SD card
pub end_day: u8,
/// End hour of the video probably only useful for the recorded files on the SD card
pub end_hour: u8,
/// End min of the video probably only useful for the recorded files on the SD card
pub end_min: u8,
/// End seconds of the video probably only useful for the recorded files on the SD card
pub end_seconds: u8,
// unknown: u16
}
//
pub(super) const MAGIC_HEADER_BCMEDIA_INFO_V2: u32 = 0x32303031;
/// The start of a BcMedia stream contains this message
/// which describes the data to follow
pub struct BcMediaInfoV2 {
/// This is the size of the header so it's actually a fixed value
/// The other messages have body size here so maybe that's why
/// it's included
pub header_size: u32,
/// Width of the video
pub video_width: u32,
/// Height of the video
pub video_height: u32,
// pub unknown: u8,
/// Frames per second. On older cameras this seems to be an index of the FPS on a lookup table
pub fps: u8,
/// Start year of the stream
pub start_year: u8,
/// Start month of the stream
pub start_month: u8,
/// Start day of the stream
pub start_day: u8,
/// Start hour of the stream
pub start_hour: u8,
/// Start minute of the stream
pub start_min: u8,
/// Start seconds of the stream
pub start_seconds: u8,
/// End year of the video probably only useful for the recorded files on the SD card
pub end_year: u8,
/// End month of the video probably only useful for the recorded files on the SD card
pub end_month: u8,
/// End day of the video probably only useful for the recorded files on the SD card
pub end_day: u8,
/// End hour of the video probably only useful for the recorded files on the SD card
pub end_hour: u8,
/// End min of the video probably only useful for the recorded files on the SD card
pub end_min: u8,
/// End seconds of the video probably only useful for the recorded files on the SD card
pub end_seconds: u8,
// unknown: u16
}
pub(super) const MAGIC_HEADER_BCMEDIA_IFRAME: u32 = 0x63643030;
/// This is a BcMedia video IFrame.
pub struct BcMediaIframe {
/// ASCII of either "H264", or "H265"
pub video_type: String,
/// Size of payload after header in bytes
pub payload_size: u32,
// unknown: u32, // NVR channel count? Known values 1-00/08 2-00 3-00 4-00
/// Timestamp in microseconds
pub microseconds: u32,
// unknown: u32, // Known values 1-00/23/5A 2-00 3-00 4-00
/// POSIX time (seconds since 00:00:00 Jan 1 1970)
pub time: u32,
//unknown: u32, // Known values 1-00/06/29 2-00/01 3-00/C3 4-00
/// Raw IFrame data
pub data: Vec<u8>,
}
pub(super) const MAGIC_HEADER_BCMEDIA_PFRAME: u32 = 0x63643130;
/// This is a BcMedia video PFrame.
pub struct BcMediaPframe {
/// ASCII of either "H264", or "H265"
pub video_type: String,
/// Size of payload after header in bytes
pub payload_size: u32,
// unknown: u32, // NVR channel count? Known values 1-00/08 2-00 3-00 4-00
/// Timestamp in microseconds
pub microseconds: u32,
// unknown: u32, // Known values 1-00/23/5A 2-00 3-00 4-00
/// Raw PFrame data
pub data: Vec<u8>,
}
pub(super) const MAGIC_HEADER_BCMEDIA_AAC: u32 = 0x62773530;
/// This contains BcMedia audio data in AAC format
pub struct BcMediaAac {
/// Size of payload after header in bytes
pub payload_size: u16,
// Size of payload after header in bytes exactly the same as before
// pub payload_size_b: u16,
/// Raw AAC data
pub data: Vec<u8>,
}
pub(super) const MAGIC_HEADER_BCMEDIA_ADPCM: u32 = 0x62773130;
pub(super) const MAGIC_HEADER_BCMEDIA_ADPCM_DATA: u16 = 0x0100;
/// This contains BcMedia audio data in ADPCM format
pub struct BcMediaAdpcm {
/// Size of payload after header in bytes
pub payload_size: u16,
// Size of payload after header in bytes exactly the same as before
// pub payload_size_b: u16,
// more_magic: MAGIC_HEADER_BCMEDIA_ADPCM_DATA
/// Adpcm block size in bytes (there are twice this in samples)
///
/// These bytes (and the MAGIC_HEADER_BCMEDIA_ADPCM_DATA) are included as
/// part of the payload_size. It may be more prudent to sealise them to
/// another structure.
pub block_size: u16,
/// The raw adpcm data in DVI-4 layout. Size should be [block_size] + 4
pub data: Vec<u8>,
}
+24
View File
@@ -0,0 +1,24 @@
use super::model::*;
use cookie_factory::bytes::*;
use cookie_factory::sequence::tuple;
use cookie_factory::{combinator::*, gen};
use cookie_factory::{GenError, SerializeFn, WriteContext};
use err_derive::Error;
use log::error;
use std::io::Write;
/// The error types used during serialisation
#[derive(Debug, Error)]
pub enum Error {
/// A Cookie Factor GenError
#[error(display = "Cookie GenError")]
GenError(#[error(source)] GenError),
}
impl BcMedia {
pub(crate) fn serialize<W: Write>(&self, buf: W) -> Result<W, Error> {
// match &self {}
Ok(buf)
}
}
+2
View File
@@ -26,6 +26,8 @@
pub mod bc;
/// Contains high level interfaces for the camera
pub mod bc_protocol;
/// Contains low level structures and formats for the media substream
pub mod bcmedia;
#[derive(Debug)]
/// Certain method just as `start_video` will block forever or return an error
+1 -1
View File
@@ -17,8 +17,8 @@ use gstreamer_rtsp_server::{
};
use log::*;
use neolink_core::{
bc::model::*,
bc_protocol::{StreamOutput, StreamOutputError},
bcmedia::model::*,
};
use std::collections::HashSet;
use std::fs;