Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

improve(audio): validate streams without copying discarded PCM

vedanth4789b104fdae
diff --git a/crates/audio/examples/audio-validation-benchmark.rs b/crates/audio/examples/audio-validation-benchmark.rs
new file mode 100644
index 000000000..685173923
--- /dev/null
+++ b/crates/audio/examples/audio-validation-benchmark.rs
@@ -0,0 +1,52 @@
+use cap_audio::{AudioStream, ChunkRead};
+use std::{
+ error::Error,
+ hint::black_box,
+ path::PathBuf,
+ sync::{Arc, atomic::AtomicBool},
+ time::Instant,
+};
+
+fn main() -> Result<(), Box<dyn Error>> {
+ ffmpeg::init()?;
+ let paths: Vec<PathBuf> = std::env::args_os().skip(1).map(PathBuf::from).collect();
+ if paths.is_empty() {
+ return Err("expected audio file paths".into());
+ }
+ println!("fixture,mode,prefix,sample,elapsed_ms,frames");
+ for (fixture, path) in paths.iter().enumerate() {
+ for (mode, prefix) in [("validate", 0), ("validate", 7), ("read", 0)] {
+ for iteration in 0..28 {
+ let mut stream = AudioStream::open(path, Arc::new(AtomicBool::new(false)))?;
+ if prefix != 0 {
+ assert!(matches!(stream.read_chunk(prefix)?, ChunkRead::Chunk(_)));
+ }
+ let started = Instant::now();
+ let frames = if mode == "validate" {
+ black_box(stream.validate_to_end()?)
+ } else {
+ loop {
+ match stream.read_chunk(48_000)? {
+ ChunkRead::Chunk(chunk) => {
+ black_box(chunk);
+ }
+ ChunkRead::Eof { next_sample } => break next_sample,
+ }
+ }
+ };
+ let elapsed_ms = started.elapsed().as_secs_f64() * 1_000.0;
+ assert_eq!(stream.position(), frames);
+ assert!(
+ matches!(stream.read_chunk(1)?, ChunkRead::Eof { next_sample } if next_sample == frames)
+ );
+ if iteration >= 3 {
+ println!(
+ "{fixture},{mode},{prefix},{},{elapsed_ms:.6},{frames}",
+ iteration - 3
+ );
+ }
+ }
+ }
+ }
+ Ok(())
+}
diff --git a/crates/audio/src/streaming.rs b/crates/audio/src/streaming.rs
index 59fe3e3ce..a720c807e 100644
--- a/crates/audio/src/streaming.rs
+++ b/crates/audio/src/streaming.rs
@@ -296,7 +296,29 @@ impl AudioStream {
}
let max_samples = max_frames * usize::from(self.channels);
let mut output = Vec::with_capacity(max_samples);
- while output.len() < max_samples {
+ let source_start_sample = self.position;
+ let samples =
+ self.read_chunk_samples(max_samples, |samples| output.extend_from_slice(samples))?;
+ if samples > 0 {
+ Ok(ChunkRead::Chunk(AudioChunk {
+ source_start_sample,
+ channels: self.channels,
+ samples: output,
+ }))
+ } else {
+ Ok(ChunkRead::Eof {
+ next_sample: self.position,
+ })
+ }
+ }
+
+ fn read_chunk_samples(
+ &mut self,
+ max_samples: usize,
+ mut consume: impl FnMut(&[f32]),
+ ) -> Result<usize, AudioStreamError> {
+ let mut samples = 0;
+ while samples < max_samples {
if self.failure.is_none() && self.cancellation.is_cancelled() {
self.failure = Some(Failure {
stage: "cancelled",
@@ -316,9 +338,9 @@ impl AudioStream {
next_sample: self.position,
})?;
if self.pending_offset < pending.len() {
- let count = (max_samples - output.len()).min(pending.len() - self.pending_offset);
- output
- .extend_from_slice(&pending[self.pending_offset..self.pending_offset + count]);
+ let count = (max_samples - samples).min(pending.len() - self.pending_offset);
+ consume(&pending[self.pending_offset..self.pending_offset + count]);
+ samples += count;
self.pending_offset += count;
continue;
}
@@ -332,14 +354,9 @@ impl AudioStream {
self.failure = Some(error);
}
}
- if !output.is_empty() {
- let source_start_sample = self.position;
- self.position += (output.len() / self.channels as usize) as u64;
- return Ok(ChunkRead::Chunk(AudioChunk {
- source_start_sample,
- channels: self.channels,
- samples: output,
- }));
+ if samples > 0 {
+ self.position += (samples / self.channels as usize) as u64;
+ return Ok(samples);
}
if let Some(error) = &self.failure {
return Err(AudioStreamError {
@@ -348,18 +365,13 @@ impl AudioStream {
next_sample: self.position,
});
}
- Ok(ChunkRead::Eof {
- next_sample: self.position,
- })
+ Ok(0)
}
pub fn validate_to_end(&mut self) -> Result<u64, AudioStreamError> {
- loop {
- match self.read_chunk(MAX_CHUNK_FRAMES)? {
- ChunkRead::Chunk(_) => {}
- ChunkRead::Eof { next_sample } => return Ok(next_sample),
- }
- }
+ let max_samples = MAX_CHUNK_FRAMES * usize::from(self.channels);
+ while self.read_chunk_samples(max_samples, |_| {})? != 0 {}
+ Ok(self.position)
}
fn produce_pcm(&mut self) -> Result<(), Failure> {
@@ -632,6 +644,80 @@ mod tests {
assert_eq!(stream.position(), reference.sample_count() as u64);
}
+ #[test]
+ fn validation_matches_chunk_drain_after_partial_reads_and_resampling() {
+ for (rate, channels, frames) in [
+ (48_000, 1, 0),
+ (48_000, 1, 1),
+ (48_000, 2, 96_017),
+ (44_100, 2, 10_001),
+ (96_000, 6, 12_345),
+ ] {
+ let file = pcm_wav(rate, channels, frames);
+ let expected = crate::AudioData::from_file(file.path())
+ .unwrap()
+ .sample_count() as u64;
+ for prefix in [0, 1, 7, 479, 997, 48_000] {
+ let open = || {
+ let mut stream =
+ AudioStream::open(file.path(), Arc::new(AtomicBool::new(false))).unwrap();
+ if prefix > 0 {
+ stream.read_chunk(prefix).unwrap();
+ }
+ stream
+ };
+ let mut reference = open();
+ loop {
+ match reference.read_chunk(48_000).unwrap() {
+ ChunkRead::Chunk(_) => {}
+ ChunkRead::Eof { next_sample } => {
+ assert_eq!(next_sample, expected);
+ break;
+ }
+ }
+ }
+ let mut actual = open();
+ assert_eq!(actual.validate_to_end().unwrap(), expected);
+ assert_eq!(actual.position(), reference.position());
+ assert_eq!(actual.validate_to_end().unwrap(), expected);
+ assert!(
+ matches!(actual.read_chunk(1).unwrap(), ChunkRead::Eof { next_sample } if next_sample == expected)
+ );
+ }
+ }
+ }
+
+ #[test]
+ fn validation_reports_pending_failure_after_the_same_frame_boundary() {
+ let file = pcm_wav(48_000, 2, 1);
+ for frames in [0, 1, 47_999, 48_000, 48_007, 96_001] {
+ let open = || {
+ let mut stream =
+ AudioStream::open(file.path(), Arc::new(AtomicBool::new(false))).unwrap();
+ stream.pending = vec![0.25; frames * 2];
+ stream.failure = Some(Failure {
+ stage: "send-packet",
+ detail: "injected failure".into(),
+ });
+ stream
+ };
+ let mut reference = open();
+ let expected = loop {
+ match reference.read_chunk(48_000) {
+ Ok(ChunkRead::Chunk(_)) => {}
+ Ok(ChunkRead::Eof { .. }) => panic!("failure became EOF"),
+ Err(error) => break error,
+ }
+ };
+ assert_eq!(expected.next_sample, frames as u64);
+ let mut actual = open();
+ assert_eq!(actual.validate_to_end().unwrap_err(), expected);
+ assert_eq!(actual.position(), reference.position());
+ assert_eq!(actual.validate_to_end().unwrap_err(), expected);
+ assert_eq!(actual.read_chunk(1).unwrap_err(), expected);
+ }
+ }
+
#[test]
fn cancellation_is_sticky_without_publishing_pending_samples() {
let file = pcm_wav(48_000, 2, 10_001);