moq-audio 0.0.22

Native audio encoding/decoding for Media over QUIC
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
//! Preallocated handoff from the microphone callback to the async capture loop.

use std::sync::Arc;
use std::sync::atomic::{AtomicU64, Ordering};
use std::time::{Duration, Instant};

use ringbuf::traits::{Consumer, Observer, Producer, Split};
use ringbuf::{CachingCons, CachingProd, HeapCons, HeapProd, HeapRb};
use tokio::sync::mpsc;

use super::Samples;

/// Roughly 80 ms at the common 10 ms callback cadence.
const DEPTH: usize = 8;

/// Process unusually large host buffers in fixed pieces rather than growing a
/// scratch allocation on the realtime thread.
const CHUNK_FRAMES: usize = 4096;

/// The relay polls so the callback never participates in a blocking wakeup.
const RELAY_POLL_INTERVAL: Duration = Duration::from_millis(1);

/// Drops are logged at most this often, so a sustained stall does not spam.
const REPORT_INTERVAL: Duration = Duration::from_secs(1);

/// Create a pool and its bounded filled-buffer queue.
pub(super) fn channel(channels: usize, #[cfg(feature = "aec")] aec: Option<crate::aec::Canceller>) -> (Writer, Reader) {
	let samples = CHUNK_FRAMES * channels;
	let (filled, pending) = HeapRb::<Filled>::new(DEPTH).split();
	let (tx, rx) = mpsc::channel(1);

	let mut recycle = Vec::with_capacity(DEPTH);
	let mut free = Vec::with_capacity(DEPTH);
	for slot in 0..DEPTH {
		let ring = Arc::new(HeapRb::new(1));
		let recycled = CachingCons::new(ring.clone());
		recycle.push(RecycleSlot { ring, recycled });
		free.push(Buffer {
			data: Vec::with_capacity(samples),
			slot,
		});
	}

	// Polling isolates Tokio's wakeup and linked-block allocations from the
	// callback, which only produces into a fixed lock-free SPSC ring.
	std::thread::Builder::new()
		.name("moq-audio-capture".into())
		.spawn(move || relay(pending, tx))
		.expect("failed to spawn audio capture handoff");

	let dropped = Arc::new(AtomicU64::new(0));
	let writer = Writer {
		filled,
		recycle,
		free,
		pending_gap: false,
		dropped: dropped.clone(),
		channels,
		#[cfg(feature = "aec")]
		aec,
	};
	let reader = Reader {
		rx,
		dropped,
		unreported: 0,
		last_report: None,
	};

	(writer, reader)
}

/// Move filled buffers into Tokio without waking from the callback.
fn relay(mut pending: HeapCons<Filled>, tx: mpsc::Sender<Filled>) {
	loop {
		if let Some(filled) = pending.try_pop() {
			if tx.blocking_send(filled).is_err() {
				return;
			}
			continue;
		}

		if !pending.write_is_held() {
			return;
		}
		std::thread::sleep(RELAY_POLL_INTERVAL);
	}
}

/// One callback allocation and the recycle slot that owns it.
struct Buffer {
	data: Vec<f32>,
	slot: usize,
}

/// One buffer submitted to the async capture pipeline.
struct Filled {
	data: Vec<f32>,
	gap: bool,
	slot: usize,
	recycle: HeapProd<Vec<f32>>,
}

/// A dedicated SPSC return path for one pool allocation.
struct RecycleSlot {
	ring: Arc<HeapRb<Vec<f32>>>,
	recycled: HeapCons<Vec<f32>>,
}

/// The callback-owned half of the pool.
pub(super) struct Writer {
	filled: HeapProd<Filled>,
	recycle: Vec<RecycleSlot>,
	free: Vec<Buffer>,
	pending_gap: bool,
	dropped: Arc<AtomicU64>,
	channels: usize,
	#[cfg(feature = "aec")]
	aec: Option<crate::aec::Canceller>,
}

impl Writer {
	/// Copy native `f32` samples into preallocated buffers.
	pub(super) fn write_f32(&mut self, input: &[f32]) {
		self.write(input, |sample| sample);
	}

	/// Convert native signed samples into preallocated buffers.
	pub(super) fn write_i16(&mut self, input: &[i16]) {
		self.write(input, |sample| sample as f32 / 32768.0);
	}

	/// Convert native unsigned samples into preallocated buffers.
	pub(super) fn write_u16(&mut self, input: &[u16]) {
		self.write(input, |sample| (sample as f32 - 32768.0) / 32768.0);
	}

	/// Convert and submit one host callback without touching the allocator.
	fn write<T: Copy>(&mut self, input: &[T], convert: impl Fn(T) -> f32) {
		let chunk_samples = CHUNK_FRAMES * self.channels;
		let complete = input.len() - input.len() % self.channels;

		for input in input[..complete].chunks(chunk_samples) {
			let Some(mut output) = self.take() else {
				self.drop_one();
				continue;
			};

			debug_assert!(output.data.capacity() >= input.len());
			output.data.extend(input.iter().copied().map(&convert));

			#[cfg(feature = "aec")]
			if let Some(aec) = &self.aec {
				aec.process(&mut output.data);
			}

			let recycle = CachingProd::new(self.recycle[output.slot].ring.clone());
			let filled = Filled {
				data: output.data,
				gap: self.pending_gap,
				slot: output.slot,
				recycle,
			};

			match self.filled.try_push(filled) {
				Ok(()) => self.pending_gap = false,
				Err(filled) => {
					self.restore(filled);
					if !self.filled.read_is_held() {
						return;
					}
					self.drop_one();
				}
			}
		}

		if complete != input.len() {
			self.drop_one();
		}
	}

	/// Take an empty buffer without waiting for the async reader.
	fn take(&mut self) -> Option<Buffer> {
		self.reclaim();
		let mut output = self.free.pop()?;
		output.data.clear();
		Some(output)
	}

	/// Recover buffers whose downstream borrower has released them.
	fn reclaim(&mut self) {
		for (slot, recycle) in self.recycle.iter_mut().enumerate() {
			if recycle.recycled.write_is_held() {
				continue;
			}
			if let Some(mut data) = recycle.recycled.try_pop() {
				data.clear();
				self.free.push(Buffer { data, slot });
			}
		}
	}

	/// Put a failed submission directly back in the callback-owned pool.
	fn restore(&mut self, filled: Filled) {
		let Filled {
			mut data,
			slot,
			recycle,
			..
		} = filled;
		drop(recycle);
		data.clear();
		self.free.push(Buffer { data, slot });
	}

	fn drop_one(&mut self) {
		self.pending_gap = true;
		#[cfg(feature = "aec")]
		if let Some(aec) = &self.aec {
			aec.mark_discontinuous();
		}
		self.dropped.fetch_add(1, Ordering::Relaxed);
	}
}

/// The async half of the pool.
pub(super) struct Reader {
	rx: mpsc::Receiver<Filled>,
	dropped: Arc<AtomicU64>,
	unreported: u64,
	last_report: Option<Instant>,
}

impl Reader {
	/// Await one filled buffer and arrange to recycle it when the samples leave
	/// the capture pipeline.
	pub(super) async fn recv(&mut self) -> Option<Samples> {
		let filled = self.rx.recv().await?;
		self.observe();
		Some(Samples::pooled(filled.data, filled.gap, filled.recycle))
	}

	/// Fold callback drops into a throttled log.
	fn observe(&mut self) {
		let dropped = self.dropped.swap(0, Ordering::Relaxed);
		if dropped == 0 {
			return;
		}
		self.unreported += dropped;

		let now = Instant::now();
		if self
			.last_report
			.is_none_or(|last| now.duration_since(last) >= REPORT_INTERVAL)
		{
			tracing::warn!(
				dropped = self.unreported,
				capacity = DEPTH,
				"dropped audio capture buffers"
			);
			self.last_report = Some(now);
			self.unreported = 0;
		}
	}
}

#[cfg(test)]
mod tests {
	use std::alloc::{GlobalAlloc, Layout, System};
	use std::cell::Cell;
	use std::sync::mpsc::sync_channel;

	use super::*;

	struct TrackingAllocator;

	thread_local! {
		static ACTIVITY: Cell<Option<usize>> = const { Cell::new(None) };
	}

	unsafe impl GlobalAlloc for TrackingAllocator {
		unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
			ACTIVITY.with(|activity| {
				if let Some(count) = activity.get() {
					activity.set(Some(count + 1));
				}
			});
			unsafe { System.alloc(layout) }
		}

		unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
			ACTIVITY.with(|activity| {
				if let Some(count) = activity.get() {
					activity.set(Some(count + 1));
				}
			});
			unsafe { System.dealloc(ptr, layout) };
		}
	}

	#[global_allocator]
	static ALLOCATOR: TrackingAllocator = TrackingAllocator;

	fn activity(f: impl FnOnce()) -> usize {
		ACTIVITY.with(|activity| activity.set(Some(0)));
		f();
		ACTIVITY.with(|activity| activity.replace(None).unwrap())
	}

	fn create(channels: usize) -> (Writer, Reader) {
		channel(
			channels,
			#[cfg(feature = "aec")]
			None,
		)
	}

	#[cfg(feature = "aec")]
	fn create_with_aec(channels: usize) -> (Writer, Reader, crate::aec::Canceller) {
		let aec = crate::aec::Canceller::new(
			Arc::new(crate::playback::Shared::default()),
			crate::aec::Config::default(),
		);
		aec.open(48_000, channels as u32).unwrap();
		let (writer, reader) = channel(channels, Some(aec.clone()));
		(writer, reader, aec)
	}

	#[test]
	fn every_sample_format_uses_the_preallocated_pool() {
		let (mut writer, _reader) = create(2);
		let f32s = vec![0.25f32; 960 * 2];
		let i16s = vec![8192i16; 960 * 2];
		let u16s = vec![40960u16; 960 * 2];

		assert_eq!(activity(|| writer.write_f32(&f32s)), 0);
		assert_eq!(activity(|| writer.write_i16(&i16s)), 0);
		assert_eq!(activity(|| writer.write_u16(&u16s)), 0);
	}

	#[tokio::test]
	async fn sustained_handoff_never_allocates_on_the_writer() {
		let (mut writer, mut reader) = create(1);
		let input = vec![0.25f32; 480];

		for _ in 0..128 {
			assert_eq!(activity(|| writer.write_f32(&input)), 0);
			drop(reader.recv().await.unwrap());
		}
	}

	#[test]
	fn stalled_reader_never_blocks_or_allocates_on_the_writer() {
		let (mut writer, _reader) = create(1);
		let input = vec![0.25f32; 480];
		let (done, wait) = sync_channel(1);
		let thread = std::thread::spawn(move || {
			let activity = activity(|| {
				for _ in 0..DEPTH * 4 {
					writer.write_f32(&input);
				}
			});
			done.send(activity).unwrap();
		});

		assert_eq!(wait.recv_timeout(Duration::from_secs(1)).unwrap(), 0);
		thread.join().unwrap();
	}

	#[tokio::test]
	async fn recycles_buffers_after_the_pipeline_releases_them() {
		let (mut writer, mut reader) = create(1);
		let input = vec![0.5; 480];

		writer.write_f32(&input);
		let released = reader.recv().await.unwrap();
		let address = released.data.as_ptr();
		drop(released);

		writer.write_f32(&input);
		let reused = reader.recv().await.unwrap();
		assert_eq!(reused.data.as_ptr(), address);
	}

	#[tokio::test]
	async fn overflow_marks_the_first_recovery_buffer_as_a_gap() {
		let (mut writer, mut reader) = create(1);
		let backlog = vec![0.5; 480];

		for _ in 0..DEPTH {
			writer.write_f32(&backlog);
		}
		writer.write_f32(&[0.75; 480]);

		let first = reader.recv().await.unwrap();
		assert!(!first.gap);
		assert_eq!(first.data[0], 0.5);
		drop(first);
		writer.write_f32(&[1.0; 480]);

		for _ in 1..DEPTH {
			let backlog = reader.recv().await.unwrap();
			assert!(!backlog.gap);
			assert_eq!(backlog.data[0], 0.5);
		}
		let recovered = reader.recv().await.unwrap();
		assert!(recovered.gap);
		assert_eq!(recovered.data[0], 1.0);
	}

	#[cfg(feature = "aec")]
	#[tokio::test]
	async fn overflow_discards_partial_aec_frames() {
		let (mut writer, mut reader, aec) = create_with_aec(1);

		writer.write_f32(&[0.5; 512]);
		for _ in 1..DEPTH {
			writer.write_f32(&[0.5; 480]);
		}
		assert_eq!(aec.pending_samples(), 32);

		writer.write_f32(&[0.75; 512]);
		drop(reader.recv().await.unwrap());
		writer.write_f32(&[1.0; 448]);

		assert_eq!(
			aec.pending_samples(),
			448,
			"samples buffered before overflow leaked into the recovery callback"
		);
	}

	#[test]
	fn oversized_callbacks_are_chunked_without_allocating() {
		let (mut writer, _reader) = create(2);
		let input = vec![0.25f32; (CHUNK_FRAMES * 2) + 960];

		assert_eq!(activity(|| writer.write_f32(&input)), 0);
	}
}