diff --git a/crates/core/src/bc/de.rs b/crates/core/src/bc/de.rs index f3f3897..f61db6e 100644 --- a/crates/core/src/bc/de.rs +++ b/crates/core/src/bc/de.rs @@ -2,7 +2,6 @@ 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 std::io::Read; @@ -12,32 +11,24 @@ use std::io::Read; 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), } - type NomErrorType<'a> = nom::error::Error<&'a [u8]>; impl<'a> From>> for Error { fn from(k: nom::Err>) -> 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) } } -impl Bc { - pub(crate) fn deserialize(context: &mut BcContext, r: R) -> Result { - // Throw away the nom-specific return types - read_from_reader(|reader| bc_msg(context, reader), r) - } -} - fn read_from_reader(mut parser: P, mut rdr: R) -> Result where R: Read, @@ -70,6 +61,13 @@ where } } +impl Bc { + pub(crate) fn deserialize(context: &mut BcContext, r: R) -> Result { + // 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)?; @@ -210,8 +208,8 @@ fn bc_header(buf: &[u8]) -> IResult<&[u8], BcHeader> { Ok(( buf, BcHeader { - msg_id, body_len, + msg_id, channel_id, stream_type, msg_num, diff --git a/crates/core/src/bc_protocol.rs b/crates/core/src/bc_protocol.rs index ede23b4..ee506a7 100644 --- a/crates/core/src/bc_protocol.rs +++ b/crates/core/src/bc_protocol.rs @@ -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,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 = std::result::Result; diff --git a/crates/core/src/bc_protocol/binarysub.rs b/crates/core/src/bc_protocol/binarysub.rs new file mode 100644 index 0000000..bc4618f --- /dev/null +++ b/crates/core/src/bc_protocol/binarysub.rs @@ -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 = std::result::Result; + +/// 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, + 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 { + 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::>(); + 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; + } +} diff --git a/crates/core/src/bc_protocol/connection.rs b/crates/core/src/bc_protocol/connection.rs index 2b853f3..a4a822e 100644 --- a/crates/core/src/bc_protocol/connection.rs +++ b/crates/core/src/bc_protocol/connection.rs @@ -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 { diff --git a/crates/core/src/bc_protocol/errors.rs b/crates/core/src/bc_protocol/errors.rs index 55c07c5..d0b532d 100644 --- a/crates/core/src/bc_protocol/errors.rs +++ b/crates/core/src/bc_protocol/errors.rs @@ -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), diff --git a/crates/core/src/bc_protocol/media_packet.rs b/crates/core/src/bc_protocol/media_packet.rs deleted file mode 100644 index abf8ba2..0000000 --- a/crates/core/src/bc_protocol/media_packet.rs +++ /dev/null @@ -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, -} - -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 { - 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 { - 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, - 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(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 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 { - // 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(), - }) - } -} diff --git a/crates/core/src/bc_protocol/reboot.rs b/crates/core/src/bc_protocol/reboot.rs index b9303ad..8b16f4f 100644 --- a/crates/core/src/bc_protocol/reboot.rs +++ b/crates/core/src/bc_protocol/reboot.rs @@ -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(()) } } diff --git a/crates/core/src/bc_protocol/stream.rs b/crates/core/src/bc_protocol/stream.rs index 39f0c01..ba4aebd 100644 --- a/crates/core/src/bc_protocol/stream.rs +++ b/crates/core/src/bc_protocol/stream.rs @@ -1,38 +1,18 @@ -use super::{ - media_packet::{MediaDataKind, MediaDataSubscriber}, - BcCamera, Result, -}; +use super::{BcCamera, BinarySubscriber, Result}; use crate::{ bc::{model::*, xml::*}, + bcmedia::model::*, 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 +67,12 @@ 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)?; + // We now have a complete interesting packet. Send it to on the callback + data_outs.write(bc_media)?; } } } diff --git a/crates/core/src/bcmedia/de.rs b/crates/core/src/bcmedia/de.rs new file mode 100644 index 0000000..6dca3ee --- /dev/null +++ b/crates/core/src/bcmedia/de.rs @@ -0,0 +1,304 @@ +use super::model::BcMediaIframe; +use super::model::*; +use err_derive::Error; +use nom::IResult; +use nom::{combinator::*, named, 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>> for Error { + fn from(k: nom::Err>) -> 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(mut parser: P, mut rdr: R) -> Result +where + R: Read, + E: for<'a> From>> + From, + P: FnMut(&[u8]) -> nom::IResult<&[u8], O>, +{ + let mut input: Vec = 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: R) -> Result { + // 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> { + named!(take4str( &[u8] ) -> &str, take_str!( 4 ) ); + let (buf, video_type_str) = verify(take4str, |x| matches!(x, "H264" | "H265"))(buf)?; + 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)?; + + let video_type = match video_type_str { + "H264" => VideoType::H264, + "H265" => VideoType::H265, + _ => unreachable!(), + }; + Ok(( + buf, + BcMediaIframe { + video_type, + // payload_size, + microseconds, + time, + data: data_slice.to_vec(), + }, + )) +} + +fn bcmedia_pframe(buf: &[u8]) -> IResult<&[u8], BcMediaPframe> { + named!(take4str( &[u8] ) -> &str, take_str!( 4 ) ); + let (buf, video_type_str) = verify(take4str, |x| matches!(x, "H264" | "H265"))(buf)?; + 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)?; + + let video_type = match video_type_str { + "H264" => VideoType::H264, + "H265" => VideoType::H265, + _ => unreachable!(), + }; + + Ok(( + buf, + BcMediaPframe { + video_type, + // 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(), + }, + )) +} diff --git a/crates/core/src/bcmedia/mod.rs b/crates/core/src/bcmedia/mod.rs new file mode 100644 index 0000000..7ae23f4 --- /dev/null +++ b/crates/core/src/bcmedia/mod.rs @@ -0,0 +1,6 @@ +/// Deserlizer for BCMedia +pub mod de; +/// Structure model for BCMedia +pub mod model; +/// Serlizer for BCMedia +pub mod ser; diff --git a/crates/core/src/bcmedia/model.rs b/crates/core/src/bcmedia/model.rs new file mode 100644 index 0000000..f657e7d --- /dev/null +++ b/crates/core/src/bcmedia/model.rs @@ -0,0 +1,182 @@ +/// 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; + +/// Video Types for I/PFrame +pub enum VideoType { + /// H264 video data + H264, + /// H265 video data + H265, +} + +/// This is a BcMedia video IFrame. +pub struct BcMediaIframe { + /// "H264", or "H265" + pub video_type: VideoType, + // 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, +} + +pub(super) const MAGIC_HEADER_BCMEDIA_PFRAME: u32 = 0x63643130; + +/// This is a BcMedia video PFrame. +pub struct BcMediaPframe { + /// "H264", or "H265" + pub video_type: VideoType, + // 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, +} + +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, +} + +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. + /// + /// One `data` should contain 4 bytes of the adpcm predictor state then one block + /// of adpcm samples + /// + /// To calculate the block-align size simply remove 4 from the `len()` + pub data: Vec, +} diff --git a/crates/core/src/bcmedia/ser.rs b/crates/core/src/bcmedia/ser.rs new file mode 100644 index 0000000..6da70e6 --- /dev/null +++ b/crates/core/src/bcmedia/ser.rs @@ -0,0 +1,189 @@ +use super::model::*; +use cookie_factory::bytes::*; +use cookie_factory::sequence::tuple; +use cookie_factory::{combinator::*, gen}; +use cookie_factory::{GenError, SerializeFn}; +use err_derive::Error; +use log::error; +use std::io::Write; + +// PAD_SIZE: Media packets use 8 byte padding +const PAD_SIZE: u32 = 8; + +/// 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 { + #[allow(dead_code)] + pub(crate) fn serialize(&self, buf: W) -> Result { + let (buf, _) = match &self { + BcMedia::InfoV1(payload) => gen(bcmedia_info_v1(payload), buf)?, + BcMedia::InfoV2(payload) => gen(bcmedia_info_v2(payload), buf)?, + BcMedia::Iframe(payload) => { + let pad_size = match payload.data.len() as u32 % PAD_SIZE { + 0 => 0, + n => PAD_SIZE - n, + }; + gen( + tuple(( + bcmedia_iframe(payload), + slice(&payload.data), + slice(&vec![0; pad_size as usize]), + )), + buf, + )? + } + BcMedia::Pframe(payload) => { + let pad_size = match payload.data.len() as u32 % PAD_SIZE { + 0 => 0, + n => PAD_SIZE - n, + }; + gen( + tuple(( + bcmedia_pframe(payload), + slice(&payload.data), + slice(&vec![0; pad_size as usize]), + )), + buf, + )? + } + BcMedia::Aac(payload) => { + let pad_size = match payload.data.len() as u32 % PAD_SIZE { + 0 => 0, + n => PAD_SIZE - n, + }; + gen( + tuple(( + bcmedia_aac(payload), + slice(&payload.data), + slice(&vec![0; pad_size as usize]), + )), + buf, + )? + } + BcMedia::Adpcm(payload) => { + let pad_size = match payload.data.len() as u32 % PAD_SIZE { + 0 => 0, + n => PAD_SIZE - n, + }; + gen( + tuple(( + bcmedia_adpcm(payload), + slice(&payload.data), + slice(&vec![0; pad_size as usize]), + )), + buf, + )? + } + }; + + Ok(buf) + } +} + +fn bcmedia_info_v1(payload: &BcMediaInfoV1) -> impl SerializeFn { + tuple(( + le_u32(MAGIC_HEADER_BCMEDIA_INFO_V1), + le_u32(32), + le_u32(payload.video_width), + le_u32(payload.video_height), + le_u8(0), // unknown. Known values 00/01 + le_u8(payload.fps), + le_u8(payload.start_year), + le_u8(payload.start_month), + le_u8(payload.start_day), + le_u8(payload.start_hour), + le_u8(payload.start_min), + le_u8(payload.start_seconds), + le_u8(payload.end_year), + le_u8(payload.end_month), + le_u8(payload.end_day), + le_u8(payload.end_hour), + le_u8(payload.end_min), + le_u8(payload.end_seconds), + le_u8(0), + le_u8(0), + )) +} + +fn bcmedia_info_v2(payload: &BcMediaInfoV2) -> impl SerializeFn { + tuple(( + le_u32(MAGIC_HEADER_BCMEDIA_INFO_V2), + le_u32(32), + le_u32(payload.video_width), + le_u32(payload.video_height), + le_u8(0), // unknown. Known values 00/01 + le_u8(payload.fps), + le_u8(payload.start_year), + le_u8(payload.start_month), + le_u8(payload.start_day), + le_u8(payload.start_hour), + le_u8(payload.start_min), + le_u8(payload.start_seconds), + le_u8(payload.end_year), + le_u8(payload.end_month), + le_u8(payload.end_day), + le_u8(payload.end_hour), + le_u8(payload.end_min), + le_u8(payload.end_seconds), + le_u8(0), + le_u8(0), + )) +} + +fn bcmedia_iframe(payload: &BcMediaIframe) -> impl SerializeFn { + // Cookie String needs a static lifetime + let vid_string = match payload.video_type { + VideoType::H264 => "H264", + VideoType::H265 => "H265", + }; + tuple(( + le_u32(MAGIC_HEADER_BCMEDIA_IFRAME), + string(vid_string), + le_u32(payload.data.len() as u32), + le_u32(0), // unknown. NVR channel count? Known values 1-00/08 2-00 3-00 4-00 + le_u32(payload.microseconds), + le_u32(0), // unknown. Known values 1-00/23/5A 2-00 3-00 4-00 + le_u32(payload.time), + le_u32(0), // unknown. Known values 1-00/06/29 2-00/01 3-00/C3 4-00 + )) +} + +fn bcmedia_pframe(payload: &BcMediaPframe) -> impl SerializeFn { + // Cookie String needs a static lifetime + let vid_string = match payload.video_type { + VideoType::H264 => "H264", + VideoType::H265 => "H265", + }; + tuple(( + le_u32(MAGIC_HEADER_BCMEDIA_PFRAME), + string(vid_string), + le_u32(payload.data.len() as u32), + le_u32(0), // unknown. NVR channel count? Known values 1-00/08 2-00 3-00 4-00 + le_u32(payload.microseconds), + le_u32(0), // unknown. Known values 1-00/23/5A 2-00 3-00 4-00 + )) +} + +fn bcmedia_aac(payload: &BcMediaAac) -> impl SerializeFn { + tuple(( + le_u32(MAGIC_HEADER_BCMEDIA_AAC), + le_u16(payload.data.len() as u16), + le_u16(payload.data.len() as u16), + )) +} + +fn bcmedia_adpcm(payload: &BcMediaAdpcm) -> impl SerializeFn { + tuple(( + le_u32(MAGIC_HEADER_BCMEDIA_ADPCM), + le_u16((payload.data.len() + 4) as u16), // Payload + 2 byte magic + 2byte block size + le_u16((payload.data.len() + 4) as u16), // Payload + 2 byte magic + 2byte block size + le_u16(MAGIC_HEADER_BCMEDIA_ADPCM_DATA), // magic + le_u16((payload.data.len()) as u16), // block size + predictor state size (4bytes) + )) +} diff --git a/crates/core/src/lib.rs b/crates/core/src/lib.rs index 551fe87..3c25ab8 100644 --- a/crates/core/src/lib.rs +++ b/crates/core/src/lib.rs @@ -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 diff --git a/dissector/mediapacket.md b/dissector/mediapacket.md index 26cf0b5..72adb50 100644 --- a/dissector/mediapacket.md +++ b/dissector/mediapacket.md @@ -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 diff --git a/src/rtsp/gst.rs b/src/rtsp/gst.rs index b2a7cce..7b69022 100644 --- a/src/rtsp/gst.rs +++ b/src/rtsp/gst.rs @@ -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_protocol::{StreamOutput, StreamOutputError}, + bcmedia::model::*, +}; use std::collections::HashSet; use std::fs; use std::io; @@ -36,26 +39,61 @@ 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 { + VideoType::H264 => StreamFormat::H264, + VideoType::H265 => StreamFormat::H265, + }; + self.set_format(Some(video_type)); + self.vidsrc.write_all(&payload.data)?; + } + BcMedia::Pframe(payload) => { + let video_type = match payload.video_type { + VideoType::H264 => StreamFormat::H264, + VideoType::H265 => StreamFormat::H265, + }; + 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.data.len() - 4) as u16))); + 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 +119,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 +141,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 +152,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(" "),