From 3a960141b1be3a15756695db8dece80532a013a9 Mon Sep 17 00:00:00 2001 From: QuantumEntangledAndy Date: Wed, 4 Aug 2021 20:01:39 +0700 Subject: [PATCH] Use correct adpcm block size --- crates/core/src/bc_protocol/talk.rs | 24 ++++++++++++++---------- crates/core/src/bcmedia/de.rs | 7 ++++--- crates/core/src/bcmedia/model.rs | 4 ++-- crates/core/src/bcmedia/ser.rs | 2 +- src/rtsp/gst.rs | 2 +- src/talk/mod.rs | 2 +- 6 files changed, 23 insertions(+), 18 deletions(-) diff --git a/crates/core/src/bc_protocol/talk.rs b/crates/core/src/bc_protocol/talk.rs index bb8b501..092e919 100644 --- a/crates/core/src/bc_protocol/talk.rs +++ b/crates/core/src/bc_protocol/talk.rs @@ -243,7 +243,7 @@ impl BcCamera { let full_block_size = block_size + 4; // Block size + predictor state let sub = connection.subscribe(MSG_ID_TALK)?; - const BLOCK_PER_PAYLOAD: usize = 4; + const BLOCK_PER_PAYLOAD: usize = 1; const BLOCK_HEADER_SIZE: usize = 4; const SAMPLES_PER_BYTE: usize = 2; @@ -255,7 +255,7 @@ impl BcCamera { let mut end_of_stream = false; while !end_of_stream { while payload_bytes.len() < target_chunks { - let mut buffer = vec![0; target_chunks - payload_bytes.len()]; + let mut buffer = vec![255; target_chunks - payload_bytes.len()]; if let Ok(read) = buffered_recv.read(&mut buffer) { payload_bytes.extend(&buffer[..read]); } else { @@ -269,15 +269,13 @@ impl BcCamera { } let mut payload = vec![]; - for bytes in payload_bytes.chunks(full_block_size as usize) { - let bcmedia_adpcm = BcMedia::Adpcm(BcMediaAdpcm { - data: bytes.to_vec(), - }); + for block_bytes in payload_bytes.chunks(full_block_size as usize) { + let bytes: Vec = block_bytes.to_vec(); + let bcmedia_adpcm = BcMedia::Adpcm(BcMediaAdpcm { data: bytes }); payload = bcmedia_adpcm.serialize(payload)?; } let adpcm_len = payload_bytes.len(); - payload_bytes = vec![]; // There are two samples per byte // @@ -285,9 +283,15 @@ impl BcCamera { // // There is 1 initial sample stored in the block header so we add that in the end // - let samples_sent = (adpcm_len - BLOCK_HEADER_SIZE * BLOCK_PER_PAYLOAD) - * SAMPLES_PER_BYTE - + BLOCK_PER_PAYLOAD; + let samples_sent = if adpcm_len >= BLOCK_HEADER_SIZE * BLOCK_PER_PAYLOAD { + (adpcm_len - BLOCK_HEADER_SIZE * BLOCK_PER_PAYLOAD) * SAMPLES_PER_BYTE + + BLOCK_PER_PAYLOAD + } else { + // Zero samples in this block + break; + }; + + payload_bytes = vec![]; // Time to play the sample in seconds let play_length = samples_sent as f32 / sample_rate as f32; diff --git a/crates/core/src/bcmedia/de.rs b/crates/core/src/bcmedia/de.rs index 8b3d07e..e57bc2b 100644 --- a/crates/core/src/bcmedia/de.rs +++ b/crates/core/src/bcmedia/de.rs @@ -287,9 +287,10 @@ 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)?; - // This `block_size` is bigendian for some reason. It is block size - let (buf, block_size) = be_u16(buf)?; - let (buf, data_slice) = take!(buf, block_size + BLOCK_HEADER_SIZE)?; + // This `sample_block_size` is bigendian for some reason. It is block size - BLOCK_HEADER_SIZE + let (buf, sample_block_size) = be_u16(buf)?; + let block_size = sample_block_size + BLOCK_HEADER_SIZE; + let (buf, data_slice) = take!(buf, block_size)?; let pad_size = match payload_size as u32 % PAD_SIZE { 0 => 0, n => PAD_SIZE - n, diff --git a/crates/core/src/bcmedia/model.rs b/crates/core/src/bcmedia/model.rs index f657e7d..0127b67 100644 --- a/crates/core/src/bcmedia/model.rs +++ b/crates/core/src/bcmedia/model.rs @@ -166,12 +166,12 @@ pub struct BcMediaAdpcm { // 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) + // Adpcm sample_block_size in bytes // // 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, + // pub sample_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 diff --git a/crates/core/src/bcmedia/ser.rs b/crates/core/src/bcmedia/ser.rs index 2e8771c..9173589 100644 --- a/crates/core/src/bcmedia/ser.rs +++ b/crates/core/src/bcmedia/ser.rs @@ -183,6 +183,6 @@ fn bcmedia_adpcm(payload: &BcMediaAdpcm) -> impl SerializeFn { 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 - be_u16((payload.data.len() - 4) as u16), // block size = len - predictor state size (4bytes) + be_u16((payload.data.len() - 4) as u16), // sample_block_size = len - predictor state size (4bytes) )) } diff --git a/src/rtsp/gst.rs b/src/rtsp/gst.rs index 7b4fb0b..2955c07 100644 --- a/src/rtsp/gst.rs +++ b/src/rtsp/gst.rs @@ -80,7 +80,7 @@ impl StreamOutput for GstOutputs { self.audsrc.write_all(&payload.data)?; } BcMedia::Adpcm(payload) => { - self.set_format(Some(StreamFormat::Adpcm((payload.data.len() - 4) as u16))); + self.set_format(Some(StreamFormat::Adpcm(payload.data.len() as u16))); self.audsrc.write_all(&payload.data)?; } _ => { diff --git a/src/talk/mod.rs b/src/talk/mod.rs index 3d4e43b..ef1131e 100644 --- a/src/talk/mod.rs +++ b/src/talk/mod.rs @@ -72,7 +72,7 @@ pub fn main(opt: Opt) -> Result<(), Error> { ..Default::default() }; - let block_size = talk_config.audio_config.length_per_encoder / 2; + let block_size = (talk_config.audio_config.length_per_encoder / 2) + 4; let sample_rate = talk_config.audio_config.sample_rate; if block_size == 0 || sample_rate == 0 { return Err(Error::TalkUnsupported);