Use nom to deserialise BcMedia
This commit is contained in:
10 files changed
+588
-404
No files matched your search
+239
-6
@@ -1,18 +1,24 @@
|
||||
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::*};
|
||||
use nom::{
|
||||
bytes::streaming::take, combinator::*, number::streaming::*, sequence::*, 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(&'static str),
|
||||
NomError(String),
|
||||
/// An IO error such as the stream being dropped
|
||||
#[error(display = "I/O error")]
|
||||
IoError(#[error(source)] std::io::Error),
|
||||
@@ -23,9 +29,9 @@ 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(_) => "Nom Error",
|
||||
nom::Err::Failure(_) => "Nom Failure",
|
||||
_ => "Unknown Nom error",
|
||||
nom::Err::Error(e) => format!("Nom Error: {:?}", e),
|
||||
nom::Err::Failure(e) => format!("Nom Error: {:?}", e),
|
||||
_ => "Unknown Nom error".to_string(),
|
||||
};
|
||||
Error::NomError(reason)
|
||||
}
|
||||
@@ -210,8 +216,8 @@ fn bc_header(buf: &[u8]) -> IResult<&[u8], BcHeader> {
|
||||
Ok((
|
||||
buf,
|
||||
BcHeader {
|
||||
msg_id,
|
||||
body_len,
|
||||
msg_id,
|
||||
channel_id,
|
||||
stream_type,
|
||||
msg_num,
|
||||
@@ -222,6 +228,233 @@ 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");
|
||||
|
||||
@@ -278,3 +278,173 @@ 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>,
|
||||
}
|
||||
@@ -8,21 +8,22 @@ use std::time::Duration;
|
||||
|
||||
use Md5Trunc::*;
|
||||
|
||||
mod binarysub;
|
||||
mod connection;
|
||||
mod errors;
|
||||
mod ledstate;
|
||||
mod login;
|
||||
mod logout;
|
||||
mod media_packet;
|
||||
mod ping;
|
||||
mod reboot;
|
||||
mod stream;
|
||||
mod time;
|
||||
mod version;
|
||||
|
||||
pub use binarysub::BinarySubscriber;
|
||||
pub use errors::Error;
|
||||
pub use ledstate::LightState;
|
||||
pub use stream::{StreamFormat, StreamOutput, StreamOutputError};
|
||||
pub use stream::{StreamOutput, StreamOutputError};
|
||||
|
||||
type Result<T> = std::result::Result<T, Error>;
|
||||
|
||||
|
||||
@@ -0,0 +1,92 @@
|
||||
use super::{connection::BcSubscription, RX_TIMEOUT};
|
||||
use crate::bc::model::*;
|
||||
use std::{
|
||||
io::{BufRead, Error, ErrorKind, Read},
|
||||
sync::mpsc::RecvTimeoutError,
|
||||
};
|
||||
|
||||
type Result<T> = std::result::Result<T, Error>;
|
||||
|
||||
/// A `BinarySubscriber` is a helper util to read the binary stream
|
||||
/// of a [`BcSubscription`]
|
||||
///
|
||||
/// Some messgaes like video streams contain a substream in their
|
||||
/// payload that may be broken at any point into multiple bc packets.
|
||||
/// This subscriber will create a stream reader for these binary streams.
|
||||
///
|
||||
/// This stream should be accessed via the [`BufRead`] and [`Read`] trait
|
||||
pub struct BinarySubscriber<'a> {
|
||||
bc_sub: &'a BcSubscription<'a>,
|
||||
buffer: Vec<u8>,
|
||||
consumed: usize,
|
||||
}
|
||||
|
||||
impl<'a> BinarySubscriber<'a> {
|
||||
/// Creates a binary subsciber from a BcSubscrption.
|
||||
/// When reading the next packet it will skip over multiple
|
||||
/// Bc packets to fill the binary buffer so ensure you
|
||||
/// only want binary packets when calling read
|
||||
pub fn from_bc_sub<'b>(bc_sub: &'b BcSubscription) -> BinarySubscriber<'b> {
|
||||
BinarySubscriber {
|
||||
bc_sub,
|
||||
buffer: vec![],
|
||||
consumed: 0,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> Read for BinarySubscriber<'a> {
|
||||
fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
|
||||
let buffer = self.fill_buf()?;
|
||||
let amt = std::cmp::min(buf.len(), buffer.len());
|
||||
|
||||
// First check if the amount of bytes we want to read is small:
|
||||
// `copy_from_slice` will generally expand to a call to `memcpy`, and
|
||||
// for a single byte the overhead is significant.
|
||||
if amt == 1 {
|
||||
buf[0] = buffer[0];
|
||||
} else {
|
||||
buf[..amt].copy_from_slice(&buffer[..amt]);
|
||||
}
|
||||
|
||||
self.consume(amt);
|
||||
|
||||
Ok(amt)
|
||||
}
|
||||
}
|
||||
impl<'a> BufRead for BinarySubscriber<'a> {
|
||||
fn fill_buf(&mut self) -> Result<&[u8]> {
|
||||
const CLEAR_CONSUMED_AT: usize = 1024;
|
||||
// This is a trade off between caching too much dead memory
|
||||
// and calling the drain method too often
|
||||
if self.consumed > CLEAR_CONSUMED_AT {
|
||||
let _ = self.buffer.drain(0..self.consumed).collect::<Vec<u8>>();
|
||||
self.consumed = 0;
|
||||
}
|
||||
while self.buffer.len() <= self.consumed {
|
||||
let msg = self
|
||||
.bc_sub
|
||||
.rx
|
||||
.recv_timeout(RX_TIMEOUT)
|
||||
.map_err(|err| match err {
|
||||
RecvTimeoutError::Timeout => Error::new(ErrorKind::TimedOut, err),
|
||||
RecvTimeoutError::Disconnected => Error::new(ErrorKind::ConnectionReset, err),
|
||||
})?;
|
||||
if let BcBody::ModernMsg(ModernMsg {
|
||||
payload: Some(BcPayloads::Binary(binary)),
|
||||
..
|
||||
}) = msg.body
|
||||
{
|
||||
// Add the new binary to the buffer and return
|
||||
self.buffer.extend(binary);
|
||||
}
|
||||
}
|
||||
|
||||
Ok(&self.buffer.as_slice()[self.consumed..])
|
||||
}
|
||||
|
||||
fn consume(&mut self, amt: usize) {
|
||||
assert!(self.consumed + amt <= self.buffer.len());
|
||||
self.consumed += amt;
|
||||
}
|
||||
}
|
||||
@@ -70,8 +70,7 @@ impl BcConnection {
|
||||
if poll_abort_rx.load(Ordering::Relaxed) {
|
||||
break; // Poll has been aborted by request usally during disconnect
|
||||
}
|
||||
if result.is_err() {
|
||||
let e = result.unwrap_err();
|
||||
if let Err(e) = result {
|
||||
error!("Deserialization error: {}", e);
|
||||
let mut cause = e.source();
|
||||
while let Some(e) = cause {
|
||||
|
||||
@@ -1,308 +0,0 @@
|
||||
use super::{Error, Result, StreamFormat, RX_TIMEOUT};
|
||||
use crate::bc::model::*;
|
||||
use crate::bc_protocol::connection::BcSubscription;
|
||||
use log::trace;
|
||||
use log::*;
|
||||
use std::collections::VecDeque;
|
||||
use std::convert::TryInto;
|
||||
|
||||
const INVALID_MEDIA_PACKETS: &[MediaDataKind] = &[MediaDataKind::Unknown];
|
||||
|
||||
// MAGIC_SIZE: Number of bytes needed to get magic header type, represets minimum bytes to pull from the
|
||||
// stream
|
||||
const MAGIC_SIZE: usize = 4;
|
||||
// PAD_SIZE: Media packets use 8 byte padding
|
||||
const PAD_SIZE: usize = 8;
|
||||
|
||||
#[derive(Debug, PartialEq, Eq, Hash, Copy, Clone)]
|
||||
pub enum MediaDataKind {
|
||||
VideoDataIframe,
|
||||
VideoDataPframe,
|
||||
AudioDataAac,
|
||||
AudioDataAdpcm,
|
||||
InfoData,
|
||||
Unknown,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
pub struct MediaData {
|
||||
data: Vec<u8>,
|
||||
}
|
||||
|
||||
impl MediaData {
|
||||
pub fn body(&self) -> &[u8] {
|
||||
let lower_limit = self.header_size();
|
||||
let upper_limit = self.data_size() + lower_limit;
|
||||
&self.data[lower_limit..upper_limit]
|
||||
}
|
||||
|
||||
fn header_size_from_kind(kind: MediaDataKind) -> usize {
|
||||
match kind {
|
||||
MediaDataKind::VideoDataIframe => 32,
|
||||
MediaDataKind::VideoDataPframe => 24,
|
||||
MediaDataKind::AudioDataAac => 8,
|
||||
MediaDataKind::AudioDataAdpcm => 8,
|
||||
MediaDataKind::InfoData => 32,
|
||||
MediaDataKind::Unknown => 0,
|
||||
}
|
||||
}
|
||||
|
||||
fn header_size_from_raw(data: &[u8]) -> usize {
|
||||
let kind = MediaData::kind_from_raw(data);
|
||||
MediaData::header_size_from_kind(kind)
|
||||
}
|
||||
|
||||
// Unused was part of debug code
|
||||
#[allow(dead_code)]
|
||||
pub fn header(&self) -> &[u8] {
|
||||
&self.data[0..self.header_size()]
|
||||
}
|
||||
|
||||
// Unused was part of debug code
|
||||
#[allow(dead_code)]
|
||||
pub fn header_dump(&self) {
|
||||
info!("{:?}-hex: {:02?}", self.kind(), self.header());
|
||||
let mut result = vec![];
|
||||
for four in self.header().chunks(4) {
|
||||
result.push(u32::from_le_bytes(four.try_into().unwrap()));
|
||||
}
|
||||
info!("{:?}-32: {:?}", self.kind(), result);
|
||||
let mut result = vec![];
|
||||
for two in self.header().chunks(2) {
|
||||
result.push(u16::from_le_bytes(two.try_into().unwrap()));
|
||||
}
|
||||
info!("{:?}-16: {:?}", self.kind(), result);
|
||||
let mut result = vec![];
|
||||
for one in self.header().chunks(1) {
|
||||
result.push(u8::from_le_bytes(one.try_into().unwrap()));
|
||||
}
|
||||
info!("{:?}-8: {:?}", self.kind(), result);
|
||||
let mut result = vec![];
|
||||
for four in self.header().chunks(4) {
|
||||
result.push(f32::from_le_bytes(four.try_into().unwrap()));
|
||||
}
|
||||
info!("{:?}-f32: {:?}", self.kind(), result);
|
||||
let mut result = vec![];
|
||||
for four in self.header().chunks(4) {
|
||||
result.push(String::from_utf8_lossy(four));
|
||||
}
|
||||
info!("{:?}-utf8: {:?}", self.kind(), result);
|
||||
}
|
||||
|
||||
fn header_size(&self) -> usize {
|
||||
MediaData::header_size_from_raw(&self.data)
|
||||
}
|
||||
|
||||
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 => 0, // The bytes in MediaData::bytes_to_size(&data[4..8]) seem to be the size of the header
|
||||
MediaDataKind::Unknown => data.len(),
|
||||
}
|
||||
}
|
||||
|
||||
fn data_size(&self) -> usize {
|
||||
MediaData::data_size_from_raw(&self.data)
|
||||
}
|
||||
|
||||
fn pad_size_from_raw(data: &[u8]) -> usize {
|
||||
let data_size = MediaData::data_size_from_raw(data);
|
||||
match data_size % PAD_SIZE {
|
||||
0 => 0,
|
||||
n => PAD_SIZE - n,
|
||||
}
|
||||
}
|
||||
|
||||
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!(),
|
||||
}
|
||||
}
|
||||
|
||||
fn kind_from_raw(data: &[u8]) -> MediaDataKind {
|
||||
// When calling this ensure you have enough data for header_size +2
|
||||
// Else full_header_check_from_kind will fail because we check the
|
||||
// First two bytes after the header for the audio stream
|
||||
// Since AAC and ADMPC streams start in a predicatble manner
|
||||
assert!(
|
||||
data.len() >= MAGIC_SIZE,
|
||||
"At least four bytes needed to get media packet type"
|
||||
);
|
||||
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[..MAGIC_SIZE];
|
||||
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,
|
||||
_ => {
|
||||
//trace!("Unknown magic kind: {:x?}", &magic);
|
||||
MediaDataKind::Unknown
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn kind(&self) -> MediaDataKind {
|
||||
MediaData::kind_from_raw(&self.data)
|
||||
}
|
||||
|
||||
pub fn media_format(&self) -> Option<StreamFormat> {
|
||||
let kind = self.kind();
|
||||
match kind {
|
||||
MediaDataKind::VideoDataIframe | MediaDataKind::VideoDataPframe => {
|
||||
let stream_type = &self.data[4..8];
|
||||
const H264_STR_UPPER: &[u8] = &[0x48, 0x32, 0x36, 0x34];
|
||||
const H264_STR_LOWER: &[u8] = &[0x68, 0x32, 0x36, 0x34];
|
||||
const H265_STR_UPPER: &[u8] = &[0x48, 0x32, 0x36, 0x35];
|
||||
const H265_STR_LOWER: &[u8] = &[0x68, 0x32, 0x36, 0x35];
|
||||
match stream_type {
|
||||
H264_STR_UPPER | H264_STR_LOWER => Some(StreamFormat::H264), // Offically it should be "H264" not "h264" but covering all cases
|
||||
H265_STR_UPPER | H265_STR_LOWER => Some(StreamFormat::H265),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
MediaDataKind::AudioDataAac => Some(StreamFormat::AAC),
|
||||
MediaDataKind::AudioDataAdpcm => Some(StreamFormat::ADPCM),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
// Unused. TODO: Use this to time stamp video streams correctly
|
||||
#[allow(dead_code)]
|
||||
pub fn timestamp(&self) -> Option<u64> {
|
||||
let kind = self.kind();
|
||||
match kind {
|
||||
MediaDataKind::VideoDataIframe | MediaDataKind::VideoDataPframe => Some(
|
||||
Self::bytes_to_size(&self.data[16..20])
|
||||
.try_into()
|
||||
.expect("usize wont fit into u64"),
|
||||
),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct MediaDataSubscriber<'a> {
|
||||
binary_buffer: VecDeque<u8>,
|
||||
bc_sub: &'a BcSubscription<'a>,
|
||||
}
|
||||
|
||||
impl<'a> MediaDataSubscriber<'a> {
|
||||
pub fn from_bc_sub<'b>(bc_sub: &'b BcSubscription) -> MediaDataSubscriber<'b> {
|
||||
MediaDataSubscriber {
|
||||
binary_buffer: VecDeque::new(),
|
||||
bc_sub,
|
||||
}
|
||||
}
|
||||
|
||||
fn fill_binary_buffer(&mut self) -> Result<()> {
|
||||
// Loop messages until we get binary add that data and return
|
||||
loop {
|
||||
let msg = self.bc_sub.rx.recv_timeout(RX_TIMEOUT)?;
|
||||
if let BcBody::ModernMsg(ModernMsg {
|
||||
payload: Some(BcPayloads::Binary(binary)),
|
||||
..
|
||||
}) = msg.body
|
||||
{
|
||||
// Add the new binary to the buffer and return
|
||||
self.binary_buffer.extend(binary);
|
||||
break;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn advance_to_media_packet(&mut self) -> Result<()> {
|
||||
// In the event we get an unknown packet we advance by brute force
|
||||
// reading of bytes to the next valid magic
|
||||
while self.binary_buffer.len() < MAGIC_SIZE {
|
||||
self.fill_binary_buffer()?;
|
||||
}
|
||||
|
||||
// Check the kind, if its invalid use pop a byte and try again
|
||||
let mut magic = MediaDataSubscriber::get_first_n_deque(&self.binary_buffer, MAGIC_SIZE);
|
||||
if INVALID_MEDIA_PACKETS.contains(&MediaData::kind_from_raw(&magic)) {
|
||||
warn!("Possibly truncated packet or unknown magic in stream");
|
||||
trace!("Unknown magic was: {:x?}", &magic);
|
||||
}
|
||||
while INVALID_MEDIA_PACKETS.contains(&MediaData::kind_from_raw(&magic)) {
|
||||
self.binary_buffer.pop_front();
|
||||
while self.binary_buffer.len() < MAGIC_SIZE {
|
||||
self.fill_binary_buffer()?;
|
||||
}
|
||||
magic = MediaDataSubscriber::get_first_n_deque(&self.binary_buffer, MAGIC_SIZE);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn get_first_n_deque<T: std::clone::Clone>(deque: &VecDeque<T>, n: usize) -> Vec<T> {
|
||||
// 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 about 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 {
|
||||
slice0[0..n].to_vec()
|
||||
} else {
|
||||
let remain = n - slice0.len();
|
||||
slice0.iter().chain(&slice1[0..remain]).cloned().collect()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn next_media_packet(&mut self) -> std::result::Result<MediaData, Error> {
|
||||
// Find the first packet (does nothing if already at one)
|
||||
self.advance_to_media_packet()?;
|
||||
|
||||
// Get the magic bytes (guaranteed by advance_to_media_packet)
|
||||
let magic = MediaDataSubscriber::get_first_n_deque(&self.binary_buffer, MAGIC_SIZE);
|
||||
|
||||
// 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()?;
|
||||
}
|
||||
|
||||
// Get enough for the full data + 8 byte buffer
|
||||
let header = MediaDataSubscriber::get_first_n_deque(&self.binary_buffer, header_size);
|
||||
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()?;
|
||||
}
|
||||
|
||||
// Pop the full binary buffer
|
||||
let binary = self.binary_buffer.drain(..full_size);
|
||||
|
||||
Ok(MediaData {
|
||||
data: binary.collect(),
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -1,15 +1,15 @@
|
||||
use super::{BcCamera, Error, Result, RX_TIMEOUT};
|
||||
use super::{BcCamera, Result, RX_TIMEOUT};
|
||||
use crate::bc::model::*;
|
||||
|
||||
impl BcCamera {
|
||||
/// Reboot the camera
|
||||
pub fn reboot(&self) -> Result<()> {
|
||||
let connection = self.connection.as_ref().expect("Must be connected to ping");
|
||||
let sub = connection.subscribe(MSG_ID_REBOOT)?;
|
||||
let sub_ping = connection.subscribe(MSG_ID_PING)?;
|
||||
|
||||
let msg = Bc {
|
||||
let ping = Bc {
|
||||
meta: BcMeta {
|
||||
msg_id: MSG_ID_REBOOT,
|
||||
msg_id: MSG_ID_PING,
|
||||
channel_id: self.channel_id,
|
||||
msg_num: self.new_message_num(),
|
||||
stream_type: 0,
|
||||
@@ -21,20 +21,10 @@ impl BcCamera {
|
||||
}),
|
||||
};
|
||||
|
||||
sub.send(msg)?;
|
||||
sub_ping.send(ping)?;
|
||||
|
||||
let msg = sub.rx.recv_timeout(RX_TIMEOUT)?;
|
||||
sub_ping.rx.recv_timeout(RX_TIMEOUT)?;
|
||||
|
||||
if let BcMeta {
|
||||
response_code: 200, ..
|
||||
} = msg.meta
|
||||
{
|
||||
Ok(())
|
||||
} else {
|
||||
Err(Error::UnintelligibleReply {
|
||||
reply: msg,
|
||||
why: "The camera did not accept the reboot request",
|
||||
})
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@@ -1,38 +1,17 @@
|
||||
use super::{
|
||||
media_packet::{MediaDataKind, MediaDataSubscriber},
|
||||
BcCamera, Result,
|
||||
};
|
||||
use super::{BcCamera, BinarySubscriber, Result};
|
||||
use crate::{
|
||||
bc::{model::*, xml::*},
|
||||
Never,
|
||||
};
|
||||
|
||||
/// The stream from the camera will be using one of these formats
|
||||
///
|
||||
/// This is used as part of `StreamOutput` to give hints about
|
||||
/// the format of the stream
|
||||
#[derive(Debug, PartialEq, Eq, Hash, Copy, Clone)]
|
||||
pub enum StreamFormat {
|
||||
/// H264 (AVC) video format
|
||||
H264,
|
||||
/// H265 (HEVC) video format
|
||||
H265,
|
||||
/// AAC audio
|
||||
AAC,
|
||||
/// ADPCM in DVI-4 format
|
||||
ADPCM,
|
||||
}
|
||||
|
||||
/// Convience type for the error raised by the [StreamOutput] trait
|
||||
pub type StreamOutputError = Result<()>;
|
||||
|
||||
/// The method [`BcCamera::start_video()`] requires a structure with this trait to pass the
|
||||
/// audio and video data back to
|
||||
pub trait StreamOutput {
|
||||
/// This is the callback raised when audio data is received
|
||||
fn write_audio(&mut self, data: &[u8], format: StreamFormat) -> StreamOutputError;
|
||||
/// This is the callback raised when video data is received
|
||||
fn write_video(&mut self, data: &[u8], format: StreamFormat) -> StreamOutputError;
|
||||
/// This is the callback raised a complete media packet is received
|
||||
fn write(&mut self, media: BcMedia) -> StreamOutputError;
|
||||
}
|
||||
|
||||
impl BcCamera {
|
||||
@@ -87,27 +66,15 @@ impl BcCamera {
|
||||
|
||||
sub_video.send(start_video)?;
|
||||
|
||||
let mut media_sub = MediaDataSubscriber::from_bc_sub(&sub_video);
|
||||
let mut media_sub = BinarySubscriber::from_bc_sub(&sub_video);
|
||||
|
||||
loop {
|
||||
let binary_data = media_sub.next_media_packet()?;
|
||||
// We now have a complete interesting packet. Send it to gst.
|
||||
// Process the packet
|
||||
match (binary_data.kind(), binary_data.media_format()) {
|
||||
(
|
||||
MediaDataKind::VideoDataIframe | MediaDataKind::VideoDataPframe,
|
||||
Some(media_format),
|
||||
) => {
|
||||
data_outs.write_video(binary_data.body(), media_format)?;
|
||||
}
|
||||
(MediaDataKind::AudioDataAac, Some(media_format)) => {
|
||||
data_outs.write_audio(binary_data.body(), media_format)?;
|
||||
}
|
||||
(MediaDataKind::AudioDataAdpcm, Some(media_format)) => {
|
||||
data_outs.write_audio(binary_data.body(), media_format)?;
|
||||
}
|
||||
_ => {}
|
||||
};
|
||||
let bc_media = BcMedia::deserialize(&mut media_sub).map_err(|e| {
|
||||
log::error!("{:?}", e);
|
||||
e
|
||||
})?;
|
||||
// We now have a complete interesting packet. Send it to on the callback
|
||||
data_outs.write(bc_media)?;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -98,9 +98,9 @@ Most of this information comes from the work of @twisteddx at his
|
||||
After the ADPCM header is another header of the form
|
||||
|
||||
- 2 bytes Magic either 0x00 0x01 or 0x00 0x7a
|
||||
- 2 Bytes DVI-4 Block size
|
||||
- 2 Bytes DVI-4 Block size divided in bytes.
|
||||
|
||||
After this header the adpcm DVI-4 data follows.
|
||||
After this header the adpcm DVI-4 data follows should be 4+Block size bytes.
|
||||
|
||||
## Processing
|
||||
|
||||
|
||||
+64
-24
@@ -16,7 +16,10 @@ use gstreamer_rtsp_server::{
|
||||
RTSP_TOKEN_MEDIA_FACTORY_ROLE,
|
||||
};
|
||||
use log::*;
|
||||
use neolink_core::bc_protocol::{StreamFormat, StreamOutput, StreamOutputError};
|
||||
use neolink_core::{
|
||||
bc::model::*,
|
||||
bc_protocol::{StreamOutput, StreamOutputError},
|
||||
};
|
||||
use std::collections::HashSet;
|
||||
use std::fs;
|
||||
use std::io;
|
||||
@@ -36,26 +39,63 @@ pub(crate) struct GstOutputs {
|
||||
factory: RTSPMediaFactory,
|
||||
}
|
||||
|
||||
// The stream from the camera will be using one of these formats
|
||||
//
|
||||
// This is used as part of `StreamOutput` to give hints about
|
||||
// the format of the stream
|
||||
#[derive(Debug, PartialEq, Eq, Hash, Copy, Clone)]
|
||||
enum StreamFormat {
|
||||
// H264 (AVC) video format
|
||||
H264,
|
||||
// H265 (HEVC) video format
|
||||
H265,
|
||||
// AAC audio
|
||||
Aac,
|
||||
// ADPCM in DVI-4 format
|
||||
Adpcm(u16),
|
||||
}
|
||||
|
||||
impl StreamOutput for GstOutputs {
|
||||
fn write_audio(&mut self, data: &[u8], format: StreamFormat) -> StreamOutputError {
|
||||
self.set_format(Some(format));
|
||||
if let StreamFormat::ADPCM = format {
|
||||
let pcm = adpcm_to_pcm(data).map_err(|e| {
|
||||
if let Error::AdpcmDecoding(msg) = e {
|
||||
neolink_core::Error::OtherString(format!("ADPCM decoding error: {}", msg))
|
||||
} else {
|
||||
neolink_core::Error::Other("Generic error during ADPCM decoding")
|
||||
}
|
||||
})?;
|
||||
self.vidsrc.write_all(&pcm)?;
|
||||
} else {
|
||||
self.vidsrc.write_all(data)?;
|
||||
fn write(&mut self, media: BcMedia) -> StreamOutputError {
|
||||
match media {
|
||||
BcMedia::Iframe(payload) => {
|
||||
let video_type = match payload.video_type.as_str() {
|
||||
"H264" | "h264" => StreamFormat::H264,
|
||||
"H265" | "h265" => StreamFormat::H265,
|
||||
_ => unreachable!(),
|
||||
};
|
||||
self.set_format(Some(video_type));
|
||||
self.vidsrc.write_all(&payload.data)?;
|
||||
}
|
||||
BcMedia::Pframe(payload) => {
|
||||
let video_type = match payload.video_type.as_str() {
|
||||
"H264" | "h264" => StreamFormat::H264,
|
||||
"H265" | "h265" => StreamFormat::H265,
|
||||
_ => unreachable!(),
|
||||
};
|
||||
self.set_format(Some(video_type));
|
||||
self.vidsrc.write_all(&payload.data)?;
|
||||
}
|
||||
BcMedia::Aac(payload) => {
|
||||
self.set_format(Some(StreamFormat::Aac));
|
||||
self.audsrc.write_all(&payload.data)?;
|
||||
}
|
||||
BcMedia::Adpcm(payload) => {
|
||||
self.set_format(Some(StreamFormat::Adpcm(payload.block_size)));
|
||||
let pcm = adpcm_to_pcm(&payload.data).map_err(|e| {
|
||||
if let Error::AdpcmDecoding(msg) = e {
|
||||
neolink_core::Error::OtherString(format!("ADPCM decoding error: {}", msg))
|
||||
} else {
|
||||
neolink_core::Error::Other("Generic error during ADPCM decoding")
|
||||
}
|
||||
})?;
|
||||
self.audsrc.write_all(&pcm)?;
|
||||
}
|
||||
_ => {
|
||||
//Ignore other BcMedia like InfoV1 and InfoV2
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
fn write_video(&mut self, data: &[u8], format: StreamFormat) -> StreamOutputError {
|
||||
self.set_format(Some(format));
|
||||
self.vidsrc.write_all(data)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@@ -81,7 +121,7 @@ impl GstOutputs {
|
||||
self.apply_format();
|
||||
}
|
||||
}
|
||||
Some(StreamFormat::AAC) | Some(StreamFormat::ADPCM) => {
|
||||
Some(StreamFormat::Aac) | Some(StreamFormat::Adpcm(_)) => {
|
||||
if format != self.audio_format {
|
||||
self.audio_format = format;
|
||||
self.apply_format();
|
||||
@@ -103,9 +143,9 @@ impl GstOutputs {
|
||||
};
|
||||
|
||||
let launch_aud = match self.audio_format {
|
||||
Some(StreamFormat::ADPCM) => "! queue silent=true max-size-bytes=10485760 min-threshold-bytes=1024 ! rawaudioparse format=pcm pcm-format=s16le sample-rate=8000 num-channels=1 interleaved=true ! audioconvert ! rtpL16pay name=pay1", // DVI4 is converted to pcm in the appsrc
|
||||
Some(StreamFormat::AAC) => "! queue silent=true max-size-bytes=10485760 min-threshold-bytes=1024 ! aacparse ! decodebin ! audioconvert ! rtpL16pay name=pay1 name=pay1",
|
||||
_ => "! fakesink",
|
||||
Some(StreamFormat::Adpcm(block_size)) => format!("caps=audio/x-adpcm,layout=dvi,block_align={},channels=1,rate=8000 ! queue silent=true max-size-bytes=10485760 min-threshold-bytes=1024 ! adpcmdec ! audioconvert ! rtpL16pay name=pay1", block_size), // DVI4 is converted to pcm in the appsrc
|
||||
Some(StreamFormat::Aac) => "! queue silent=true max-size-bytes=10485760 min-threshold-bytes=1024 ! aacparse ! decodebin ! audioconvert ! rtpL16pay name=pay1 name=pay1".to_string(),
|
||||
_ => "! fakesink".to_string(),
|
||||
};
|
||||
|
||||
self.factory.set_launch(
|
||||
@@ -114,7 +154,7 @@ impl GstOutputs {
|
||||
"appsrc name=vidsrc is-live=true block=true emit-signals=false max-bytes=52428800 do-timestamp=true format=GST_FORMAT_TIME", // 50MB max size so that it won't grow to infinite if the queue blocks
|
||||
launch_vid,
|
||||
"appsrc name=audsrc is-live=true block=true emit-signals=false max-bytes=52428800 do-timestamp=true format=GST_FORMAT_TIME", // 50MB max size so that it won't grow to infinite if the queue blocks
|
||||
launch_aud,
|
||||
&launch_aud,
|
||||
")"
|
||||
]
|
||||
.join(" "),
|
||||
|
||||
Reference in new issue
Block a user