diff --git a/Cargo.lock b/Cargo.lock index 302d669a3a3..155cdfa41c7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1038,6 +1038,12 @@ dependencies = [ "bytes", ] +[[package]] +name = "c-enum" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cd17eb909a8c6a894926bfcc3400a4bb0e732f5a57d37b1f14e8b29e329bace8" + [[package]] name = "calloop" version = "0.13.0" @@ -4456,6 +4462,7 @@ dependencies = [ "objc2-core-media", "objc2-foundation 0.3.2", "objc2-screen-capture-kit", + "perf-event2", "rand 0.10.3", "ringbuf", "rubato", @@ -6661,6 +6668,41 @@ version = "2.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9b4f627cb1b25917193a259e49bdad08f671f8d9708acfd5fe0a8c1455d87220" +[[package]] +name = "perf-event-data" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "575828d9d7d205188048eb1508560607a03d21eafdbba47b8cade1736c1c28e1" +dependencies = [ + "bitflags 2.13.2", + "c-enum", + "perf-event-open-sys2", +] + +[[package]] +name = "perf-event-open-sys2" +version = "5.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9c25955321465255e437600b54296983fab1feac2cd0c38958adeb26dbae49e" +dependencies = [ + "libc", + "memoffset", +] + +[[package]] +name = "perf-event2" +version = "0.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0939b8fad77dfaeb29ebbd35faaeaadbf833167f30975f1b8993bbba09ea0a0f" +dependencies = [ + "bitflags 2.13.2", + "c-enum", + "libc", + "memmap2", + "perf-event-data", + "perf-event-open-sys2", +] + [[package]] name = "pest" version = "2.9.2" diff --git a/quest/m2/performance-survey/README.md b/quest/m2/performance-survey/README.md index 756655b7eb2..af36e1c973f 100644 --- a/quest/m2/performance-survey/README.md +++ b/quest/m2/performance-survey/README.md @@ -208,7 +208,6 @@ The `js/replay` nightly series and the browser smoke's `replay.json` protect it. - [Task capacity after bursts](/quest/m2/performance-survey/task-capacity.md) - memory a task set keeps after peak occupancy subsides - [Cache metadata](/quest/m2/performance-survey/cache-metadata.md) - bookkeeping capacity after cached groups expire -- [PCM batch processing](/quest/m2/performance-survey/audio-batches.md) - repeated tail movement during encoding and resampling - [HLS request fan-out](/quest/m2/performance-survey/hls-serving.md) - repeated fetch, remux and playlist construction per viewer - [Announcement snapshots](/quest/m2/performance-survey/announced-snapshots.md) - HTTP polling cost versus result size and unrelated routes - [Selective session operations](/quest/m2/performance-survey/session-selection.md) - exact-ID lookups and registry lock hold time diff --git a/quest/m2/performance-survey/audio-batches.md b/quest/m2/performance-survey/audio-batches.md deleted file mode 100644 index 0dd7c00361e..00000000000 --- a/quest/m2/performance-survey/audio-batches.md +++ /dev/null @@ -1,32 +0,0 @@ -# [M] PCM batching avoids repeated tail movement - -## Goal - -Encoding and resampling larger PCM batches does not repeatedly move the -same remaining samples, while preserving audio and timestamp behavior. - -## Plan - -[Producer::publish_full_frames](/rs/moq-audio/src/encode/producer.rs) -front-drains into a new Vec for each packet after appending the entire input. -[Resampler::convert](/rs/moq-audio/src/resample.rs) likewise -front-drains after every chunk. N chunks in a call can move a -triangular number of trailing samples. Capture and binding producers use these -paths; small normal capture periods may hide the cost. - -Benchmark a constant duration of audio delivered in different batch sizes, -channels and input/output rate pairs, also sweeping independent producers. -Separate buffering/resampling from codec CPU with a cheap supported codec -control. Measure instructions/sample, allocations, copied bytes, throughput -and worst call duration, including tiny steady batches and large bursts. -Verify PCM or decoded output as appropriate, sample counts, packet boundaries, -timestamps, partial tails, flush and discontinuities. - -Processing slices and compacting only the remainder is a candidate; do not -prescribe a queue type before measuring. This does not change capture period -policy or the [Opus grant](/quest/m2/2848-follow-the-bandwidth-grant-in-moq-audio-instead-of.md). - -Follow the [survey's benchmark contract](/quest/m2/performance-survey/README.md): -measure the baseline before optimizing, retain paired evidence, verify equivalent -work, schedule coverage nightly, and accept a measured no-win. Expected public -API and wire impact: none; report the actual impact in the implementation PR. diff --git a/rs/moq-audio/Cargo.toml b/rs/moq-audio/Cargo.toml index 27fa0ebceae..4faa70f5ff0 100644 --- a/rs/moq-audio/Cargo.toml +++ b/rs/moq-audio/Cargo.toml @@ -142,6 +142,14 @@ serde_json = { workspace = true } tokio = { workspace = true, features = ["macros", "rt", "test-util"] } workspace-hack = { path = "../workspace-hack" } +# The batching bench counts the instructions each case retires. +[target.'cfg(target_os = "linux")'.dev-dependencies] +perf-event2 = "0.7.4" + +[[bench]] +name = "batches" +harness = false + [[bench]] name = "bus" harness = false diff --git a/rs/moq-audio/benches/batches.rs b/rs/moq-audio/benches/batches.rs new file mode 100644 index 00000000000..10f36902de2 --- /dev/null +++ b/rs/moq-audio/benches/batches.rs @@ -0,0 +1,333 @@ +//! PCM batching cost through [`encode::Producer`]: one constant duration of audio, +//! delivered in batches from tiny steady writes to a single burst, swept over channels, +//! input rates (with and without resampling) and independent producers. +//! +//! PCM is the codec, so buffering and resampling show instead of codec CPU; four Opus +//! cases put that cost beside a real codec's. Each case builds fresh producers, writes +//! every batch and finishes the track, so the partial tail and resampler flush count too. +//! +//! Run with `cargo bench -p moq-audio --bench batches`. A table comes first: user-space +//! instructions per input sample (Linux `perf_event_open`, `-` where unavailable), +//! allocations and allocated bytes from an untimed pass, plus the slowest write from a +//! separate latency pass. Criterion then times each case without per-write timers. +#![expect( + clippy::disallowed_methods, + clippy::disallowed_types, + reason = "host time measures benchmark throughput" +)] + +use std::alloc::{GlobalAlloc, Layout as AllocLayout, System}; +use std::hint::black_box; +use std::sync::atomic::{AtomicBool, AtomicU64, Ordering}; +use std::time::{Duration, Instant}; + +use bytes::Bytes; +use criterion::{BatchSize, BenchmarkId, Criterion, Throughput, criterion_group}; +use moq_audio::encode::{Codec, Config, Input, Options, Producer}; +use moq_audio::{Frame, Layout}; +use moq_net::Timestamp; + +/// Every case delivers this much audio, however it is batched. +const SECONDS: usize = 4; +/// The codec rate every case publishes at. +const CODEC_RATE: u32 = 48_000; + +struct Counter; + +static COUNTING: AtomicBool = AtomicBool::new(false); +static ALLOCATIONS: AtomicU64 = AtomicU64::new(0); +static ALLOCATED: AtomicU64 = AtomicU64::new(0); + +#[global_allocator] +static ALLOCATOR: Counter = Counter; + +impl Counter { + fn count(size: usize) { + if COUNTING.load(Ordering::Relaxed) { + ALLOCATIONS.fetch_add(1, Ordering::Relaxed); + ALLOCATED.fetch_add(size as u64, Ordering::Relaxed); + } + } +} + +unsafe impl GlobalAlloc for Counter { + unsafe fn alloc(&self, layout: AllocLayout) -> *mut u8 { + Self::count(layout.size()); + unsafe { System.alloc(layout) } + } + + unsafe fn alloc_zeroed(&self, layout: AllocLayout) -> *mut u8 { + Self::count(layout.size()); + unsafe { System.alloc_zeroed(layout) } + } + + unsafe fn realloc(&self, ptr: *mut u8, layout: AllocLayout, new_size: usize) -> *mut u8 { + Self::count(new_size); + unsafe { System.realloc(ptr, layout, new_size) } + } + + unsafe fn dealloc(&self, ptr: *mut u8, layout: AllocLayout) { + unsafe { System.dealloc(ptr, layout) } + } +} + +/// User-space instructions retired by this thread, for a count that repeats run to run. +#[cfg(target_os = "linux")] +mod instructions { + use perf_event::events::Hardware; + + /// One counter per core PMU, because a hybrid CPU counts a generic event on one core + /// type only. Retired instructions are architectural, so the sum is exact wherever the + /// thread runs. + pub struct Counter(Vec); + + impl Counter { + pub fn new() -> Option { + let hybrid: Vec = ["cpu_core", "cpu_atom"] + .iter() + .filter_map(|pmu| std::fs::read_to_string(format!("/sys/devices/{pmu}/type")).ok()) + .map(|kind| kind.trim().parse().ok()) + .collect::>()?; + // The PMU type goes in the high bits, as `PERF_PMU_TYPE_SHIFT` describes. + let events = match hybrid.is_empty() { + true => vec![Hardware::INSTRUCTIONS], + false => hybrid + .into_iter() + .map(|pmu| Hardware(pmu << 32 | Hardware::INSTRUCTIONS.0)) + .collect(), + }; + let counters = events + .into_iter() + .map(|event| perf_event::Builder::new(event).build().ok()) + .collect::>()?; + Some(Self(counters)) + } + + pub fn start(&mut self) { + for counter in &mut self.0 { + counter.reset().unwrap(); + counter.enable().unwrap(); + } + } + + pub fn stop(&mut self) -> u64 { + self.0 + .iter_mut() + .map(|counter| { + counter.disable().unwrap(); + counter.read().unwrap() + }) + .sum() + } + } +} + +#[cfg(not(target_os = "linux"))] +mod instructions { + pub struct Counter; + + impl Counter { + pub fn new() -> Option { + None + } + pub fn start(&mut self) {} + pub fn stop(&mut self) -> u64 { + 0 + } + } +} + +#[derive(Clone, Copy)] +struct Case { + codec: Codec, + input_rate: u32, + layout: Layout, + /// Input frames (per channel) in each write; the last write may be shorter. + batch: usize, + producers: usize, +} + +impl Case { + fn frames(&self) -> usize { + SECONDS * self.input_rate as usize + } + + fn samples(&self) -> u64 { + (self.frames() * self.layout.channels() as usize * self.producers) as u64 + } + + fn name(&self) -> String { + let batch = if self.batch >= self.frames() { + "burst".to_string() + } else { + self.batch.to_string() + }; + format!( + "{}/{}hz/{}ch/batch_{batch}/producers_{}", + self.codec, + self.input_rate, + self.layout.channels(), + self.producers + ) + } + + /// The batches every producer writes, built outside the measured section. + fn input(&self) -> Vec { + let channels = self.layout.channels() as usize; + let samples: Vec = (0..self.frames() * channels) + .map(|index| (index as f32 * 0.01).sin() * 0.25) + .collect(); + samples + .chunks(self.batch * channels) + .enumerate() + .map(|(index, chunk)| { + let frames = (index * self.batch) as u64; + let timestamp = Timestamp::from_micros(frames * 1_000_000 / self.input_rate as u64).unwrap(); + let data: Vec = chunk.iter().flat_map(|sample| sample.to_le_bytes()).collect(); + Frame::new(Bytes::from(data), timestamp) + }) + .collect() + } + + /// Fresh producers on one broadcast, kept alive so teardown stays outside the measurement. + fn producers(&self) -> (moq_net::broadcast::Producer, Vec) { + let mut broadcast = moq_net::broadcast::Info::new().produce(); + let catalog = moq_mux::catalog::Producer::new(&mut broadcast, moq_mux::catalog::Config::default()).unwrap(); + let input = Input::new(self.input_rate, self.layout); + let mut options = Options::default(); + options.config = Config::from_input(self.codec, &input); + options.config.sample_rate = CODEC_RATE; + let producers = (0..self.producers) + .map(|_| Producer::new(&mut broadcast, catalog.clone(), input.clone(), &options).unwrap()) + .collect(); + (broadcast, producers) + } +} + +/// Time individual writes only in the separate latency pass, outside instruction and throughput measurements. +fn run(producers: &mut [Producer], input: &[Frame]) -> Duration { + let mut slowest = Duration::ZERO; + for frame in input { + for producer in producers.iter_mut() { + let start = TIMED.then(Instant::now); + producer.write(black_box(frame)).unwrap(); + if let Some(start) = start { + slowest = slowest.max(start.elapsed()); + } + } + } + for producer in producers.iter_mut() { + producer.finish().unwrap(); + } + slowest +} + +fn cases() -> Vec { + let case = |codec, input_rate: u32, layout, batch, producers| Case { + codec, + input_rate, + layout, + batch, + producers, + }; + let rates = [48_000, 44_100, 96_000]; + // 32 frames, 10 ms, 100 ms, one second, then all of it in one write. + let batches = |rate: u32| { + let rate = rate as usize; + [32, rate / 100, rate / 10, rate, SECONDS * rate] + }; + + let mut cases = Vec::new(); + for rate in rates { + for batch in batches(rate) { + cases.push(case(Codec::Pcm, rate, Layout::Stereo, batch, 1)); + } + } + for layout in [Layout::Mono, Layout::FivePointOne] { + for rate in rates { + let [_, steady, _, _, burst] = batches(rate); + for batch in [steady, burst] { + cases.push(case(Codec::Pcm, rate, layout, batch, 1)); + } + } + } + for rate in [48_000, 44_100] { + let [_, steady, _, _, burst] = batches(rate); + for batch in [steady, burst] { + cases.push(case(Codec::Pcm, rate, Layout::Stereo, batch, 8)); + cases.push(case(Codec::Opus, rate, Layout::Stereo, batch, 1)); + } + } + cases +} + +fn counts() { + let mut counter = instructions::Counter::new(); + println!( + "{:<44} {:>12} {:>10} {:>12} {:>12}", + "case", "insn/sample", "allocs", "alloc_bytes", "slowest_us" + ); + for case in cases() { + let input = case.input(); + let (broadcast, mut producers) = case.producers(); + ALLOCATIONS.store(0, Ordering::Relaxed); + ALLOCATED.store(0, Ordering::Relaxed); + COUNTING.store(true, Ordering::Relaxed); + if let Some(counter) = &mut counter { + counter.start(); + } + run::(&mut producers, &input); + let instructions = counter.as_mut().map(instructions::Counter::stop); + COUNTING.store(false, Ordering::Relaxed); + drop((producers, broadcast)); + let (broadcast, mut producers) = case.producers(); + let slowest = run::(&mut producers, &input); + drop((producers, broadcast)); + + let per_sample = match instructions { + Some(instructions) => format!("{:.1}", instructions as f64 / case.samples() as f64), + None => "-".to_string(), + }; + println!( + "{:<44} {per_sample:>12} {:>10} {:>12} {:>12.1}", + case.name(), + ALLOCATIONS.load(Ordering::Relaxed), + ALLOCATED.load(Ordering::Relaxed), + slowest.as_secs_f64() * 1e6, + ); + } +} + +fn bench_batches(c: &mut Criterion) { + let mut group = c.benchmark_group("pcm_batches"); + group.sample_size(10); + group.warm_up_time(Duration::from_millis(500)); + group.measurement_time(Duration::from_secs(2)); + for case in cases() { + let input = case.input(); + group.throughput(Throughput::Elements(case.samples())); + group.bench_function(BenchmarkId::from_parameter(case.name()), |b| { + b.iter_batched( + || case.producers(), + |(broadcast, mut producers)| { + run::(&mut producers, &input); + (broadcast, producers) + }, + BatchSize::PerIteration, + ); + }); + } + group.finish(); +} + +criterion_group!(benches, bench_batches); + +fn main() { + // Nextest lists all targets as potential test binaries. + if std::env::args().any(|arg| arg == "--list") { + return; + } + counts(); + benches(); + Criterion::default().configure_from_args().final_summary(); +} diff --git a/rs/moq-audio/src/encode/producer.rs b/rs/moq-audio/src/encode/producer.rs index c112a218ae2..816065c33e5 100644 --- a/rs/moq-audio/src/encode/producer.rs +++ b/rs/moq-audio/src/encode/producer.rs @@ -407,49 +407,72 @@ impl Producer { /// Encode and publish every full frame in `pending`, keeping any partial /// trailing frame for the next call. + /// + /// Frames are read in place and the remainder moves to the front once, so a + /// large batch costs linear rather than quadratic copying. fn publish_full_frames(&mut self, anchor_us: u64) -> Result<(), Error> { let frame_samples = self.encoder()?.frame_size() * self.encoder()?.codec_channels() as usize; - while self.pending.len() >= frame_samples { - let chunk: Vec = self.pending.drain(..frame_samples).collect(); - let timestamp = Self::timestamp( - anchor_us, - self.frames_produced, - self.encoder()?.folded_delay(), - self.encoder()?.codec_rate(), - )?; - self.frames_produced += self.encoder()?.frame_size() as u64; - let end = Self::timestamp(anchor_us, self.frames_produced, 0, self.encoder()?.codec_rate())?; - let raw = Raw { - samples: chunk, - timestamp, - end, - reset: std::mem::take(&mut self.reset_pending), - discontinuity: std::mem::take(&mut self.pending_discontinuity), - }; - if self.active && self.backlog.is_empty() { - self.encode_raw(raw)?; - } else { - self.backlog.push(raw); - } + // Taken so each frame can be borrowed while `self` encodes it; restored below on every path. + let mut pending = std::mem::take(&mut self.pending); + let mut consumed = 0; + let mut result = Ok(()); + while result.is_ok() && pending.len() - consumed >= frame_samples { + let frame = &pending[consumed..consumed + frame_samples]; + // A frame that fails is consumed too, so it is never encoded twice. + consumed += frame_samples; + result = self.publish_frame(anchor_us, frame); } + pending.drain(..consumed); + self.pending = pending; + result + } + /// Stamp one codec frame, then encode it now or keep it in the backlog. + fn publish_frame(&mut self, anchor_us: u64, samples: &[f32]) -> Result<(), Error> { + let timestamp = Self::timestamp( + anchor_us, + self.frames_produced, + self.encoder()?.folded_delay(), + self.encoder()?.codec_rate(), + )?; + self.frames_produced += self.encoder()?.frame_size() as u64; + let end = Self::timestamp(anchor_us, self.frames_produced, 0, self.encoder()?.codec_rate())?; + let reset = std::mem::take(&mut self.reset_pending); + let discontinuity = std::mem::take(&mut self.pending_discontinuity); + if self.active && self.backlog.is_empty() { + return self.encode_frame(samples, timestamp, reset, discontinuity); + } + // Only a frame that waits needs its own copy. + self.backlog.push(Raw { + samples: samples.to_vec(), + timestamp, + end, + reset, + discontinuity, + }); Ok(()) } - fn encode_raw(&mut self, raw: Raw) -> Result<(), Error> { + fn encode_frame( + &mut self, + samples: &[f32], + timestamp: Timestamp, + reset: bool, + discontinuity: bool, + ) -> Result<(), Error> { // A break before anything was published, such as history expiring before the first // subscriber, has nothing to separate. - if raw.discontinuity && !self.decoder_boundary { + if discontinuity && !self.decoder_boundary { self.track.discontinuity()?; self.decoder_boundary = true; } let encoder = self.encoder.as_mut().ok_or(moq_net::Error::Closed)?; - if raw.reset { + if reset { encoder.reset(); } - let packet = encoder.encode(&raw.samples)?; + let packet = encoder.encode(samples)?; self.activity = packet.activity; - self.publish(packet, raw.timestamp)?; + self.publish(packet, timestamp)?; self.decoder_boundary = false; Ok(()) } @@ -466,7 +489,7 @@ impl Producer { // One codec frame per turn lets capture keep draining while catching up. if active && let Some(raw) = self.backlog.pop() { self.track.history(raw.timestamp); - self.encode_raw(raw)?; + self.encode_frame(&raw.samples, raw.timestamp, raw.reset, raw.discontinuity)?; } Ok(()) } @@ -576,7 +599,7 @@ impl Producer { self.active = true; while let Some(raw) = self.backlog.pop() { self.track.history(raw.timestamp); - self.encode_raw(raw)?; + self.encode_frame(&raw.samples, raw.timestamp, raw.reset, raw.discontinuity)?; } if self.reset_pending && let Some(encoder) = self.encoder.as_mut() @@ -1192,6 +1215,82 @@ mod tests { } } + /// Batching cannot change what is published: every packet, timestamp, group boundary + /// and the flushed tail match whether PCM arrives a frame at a time or in one burst, + /// with or without resampling. + #[tokio::test] + async fn batch_size_does_not_change_the_published_track() { + for input_rate in [48_000u32, 44_100] { + let input = Input::new(input_rate, Layout::Stereo); + // Not a whole number of codec frames, so finish flushes a partial tail. + let frames = input_rate as usize + 123; + let samples: Vec = (0..frames * 2) + .map(|index| (index as f32 * 0.01).sin() * 0.25) + .collect(); + let mut reference = None; + for batch in [1, 7, 959, 961, 1921, 4410, frames] { + let mut broadcast = moq_net::broadcast::Info::new().produce(); + let catalog = + moq_mux::catalog::Producer::new(&mut broadcast, moq_mux::catalog::Config::default()).unwrap(); + let consumer = broadcast.consume(); + let mut options = Options { + track: Some("audio".to_string()), + config: Config::from_input(super::super::Codec::Pcm, &input), + ..Options::default() + }; + options.config.sample_rate = 48_000; + let mut producer = Producer::new(&mut broadcast, catalog, input.clone(), &options).unwrap(); + let track = consumer + .track("audio") + .unwrap() + .subscribe( + moq_net::track::Subscription::default() + .with_delay(moq_net::track::Delay::default().with_max(Duration::from_secs(60))), + ) + .await + .unwrap(); + let mut reader = moq_mux::container::Consumer::new( + track, + moq_mux::container::legacy::Wire(moq_mux::container::Kind::Audio), + ); + + for (index, chunk) in samples.chunks(batch * 2).enumerate() { + let at = (index * batch) as u64 * 1_000_000 / input_rate as u64; + producer.write(&pcm_frame(chunk, 1_000_000 + at)).unwrap(); + } + producer.finish().unwrap(); + + let mut published = Vec::new(); + while let Some(read) = reader.read().await.unwrap() { + published.push(match read { + moq_mux::container::Read::Frame { group, frame } => ( + format!("{group} {:?} {}", frame.timestamp, frame.keyframe), + frame.payload, + ), + other => (format!("{other:?}"), Bytes::new()), + }); + } + if input_rate == 48_000 { + // PCM at the codec rate publishes exactly the input, its final frame zero-padded. + let payload: Vec = published.iter().flat_map(|(_, payload)| payload.to_vec()).collect(); + let expected: Vec = samples.iter().flat_map(|sample| sample.to_le_bytes()).collect(); + assert_eq!( + payload.len(), + expected.len().next_multiple_of(1920 * 4), + "batch={batch}" + ); + assert_eq!(payload[..expected.len()], expected[..], "batch={batch}"); + assert!(payload[expected.len()..].iter().all(|&byte| byte == 0), "batch={batch}"); + } + assert_eq!( + &published, + reference.get_or_insert_with(|| published.clone()), + "{input_rate} Hz, batch={batch}" + ); + } + } + } + /// A resampled publisher used to end its track early: `finish` flushed the /// encoder's own buffer but left the resampler holding its last partial chunk, /// plus the audio its filter runs behind on. diff --git a/rs/moq-audio/src/resample.rs b/rs/moq-audio/src/resample.rs index 85f1f318022..8c8a9b0c4af 100644 --- a/rs/moq-audio/src/resample.rs +++ b/rs/moq-audio/src/resample.rs @@ -275,33 +275,23 @@ impl Resampler { } /// Convert every whole chunk that is buffered, keeping the remainder. + /// + /// Chunks are read in place and the remainder moves to the front once, so a + /// large batch costs linear rather than quadratic copying. fn convert(&mut self) -> Result, BackendError> { let chunk_samples = self.chunk_frames * self.channels; let mut out = Vec::new(); - while self.pending.len() >= chunk_samples { - for (frame_idx, frame) in self.pending[..chunk_samples].chunks_exact(self.channels).enumerate() { - for (ch, &sample) in frame.iter().enumerate() { - self.input_planar[ch][frame_idx] = sample; - } + let mut consumed = 0; + let mut result = Ok(()); + while result.is_ok() && self.pending.len() - consumed >= chunk_samples { + result = self.convert_chunk(consumed, &mut out); + // A chunk the filter refused stays buffered. + if result.is_ok() { + consumed += chunk_samples; } - - let input = SequentialSliceOfVecs::new(&self.input_planar, self.channels, self.chunk_frames) - .expect("resampler input buffer dimensions"); - let mut output = - SequentialSliceOfVecs::new_mut(&mut self.output_planar, self.channels, self.output_frames_max) - .expect("resampler output buffer dimensions"); - let (_, produced) = self.resampler.process_into_buffer(&input, &mut output, None)?; - - let prev_len = out.len(); - out.resize(prev_len + produced * self.channels, 0.0); - for frame_idx in 0..produced { - for ch in 0..self.channels { - out[prev_len + frame_idx * self.channels + ch] = self.output_planar[ch][frame_idx]; - } - } - - self.pending.drain(..chunk_samples); } + self.pending.drain(..consumed); + result?; // Drop the filter's startup silence rather than passing it on as audio. What // it costs is paid back by `flush`, which drains the same amount at the end, @@ -314,6 +304,31 @@ impl Resampler { Ok(out) } + + /// Convert the chunk starting at `start` in `pending`, appending its output to `out`. + fn convert_chunk(&mut self, start: usize, out: &mut Vec) -> Result<(), BackendError> { + let chunk = &self.pending[start..start + self.chunk_frames * self.channels]; + for (frame_idx, frame) in chunk.chunks_exact(self.channels).enumerate() { + for (ch, &sample) in frame.iter().enumerate() { + self.input_planar[ch][frame_idx] = sample; + } + } + + let input = SequentialSliceOfVecs::new(&self.input_planar, self.channels, self.chunk_frames) + .expect("resampler input buffer dimensions"); + let mut output = SequentialSliceOfVecs::new_mut(&mut self.output_planar, self.channels, self.output_frames_max) + .expect("resampler output buffer dimensions"); + let (_, produced) = self.resampler.process_into_buffer(&input, &mut output, None)?; + + let prev_len = out.len(); + out.resize(prev_len + produced * self.channels, 0.0); + for frame_idx in 0..produced { + for ch in 0..self.channels { + out[prev_len + frame_idx * self.channels + ch] = self.output_planar[ch][frame_idx]; + } + } + Ok(()) + } } /// A channel mix from one layout to another, each output channel a weighted sum diff --git a/sh/gh/nightly.sh b/sh/gh/nightly.sh index afdc1746b93..e8869fad6a2 100755 --- a/sh/gh/nightly.sh +++ b/sh/gh/nightly.sh @@ -139,13 +139,13 @@ case "$1" in ;; # Keep added coverage out of the rust-bench and rust-shared legs. rust-extra) - # The allocation/window/play-buffer/receive-bursts tables keep their stdout in the run; + # The allocation/window/play-buffer/batch/receive-bursts tables keep their stdout in the run; # only Criterion estimates enter the trend report. criterion extra --no-fail-fast -p moq-e2ee -p moq-sign -p moq-json -p moq-archive -p moq-cli -p moq-mux -p moq-audio \ -p moq-quic -p moq-sock -p moq-tokio -p moq-transcode --features moq-quic/fuzz \ --bench protect --bench sign --bench codec --bench table --bench allocations --bench window \ --bench producer --bench play_buffer --bench jitter --bench decode_clock --bench ts_programs --bench ts_video \ - --bench bus --bench parameter_sets --bench sent_packets --bench demux --bench receive_bursts --bench ladder --bench fanout + --bench bus --bench batches --bench parameter_sets --bench sent_packets --bench demux --bench receive_bursts --bench ladder --bench fanout # moq-auth's published release is its own dev-dependency, so `-p moq-auth` is ambiguous. # The concurrent cases' latency percentiles stay in the run's stdout. criterion auth --manifest-path rs/moq-auth/Cargo.toml --bench verify --bench jwt