diff --git a/crates/voelin-audio/src/lib.rs b/crates/voelin-audio/src/lib.rs index 80693e8..0494562 100644 --- a/crates/voelin-audio/src/lib.rs +++ b/crates/voelin-audio/src/lib.rs @@ -10,6 +10,8 @@ //! - [`mixer`]: per-client jitter buffer, decoder, volume and mixing (on the //! vendored `tsclientlib::audio::AudioHandler`) //! - [`settings`]: all user audio settings in one serde struct +//! - [`tap`]: the processed microphone for other consumers (the stream +//! mixer), free while nobody listens //! - `device` (feature `device`): capture and playback through cpal, device //! lists and loss detection @@ -20,6 +22,7 @@ pub mod pcm; pub mod process; pub mod resample; pub mod settings; +pub mod tap; pub mod vad; pub mod wav; diff --git a/crates/voelin-audio/src/tap.rs b/crates/voelin-audio/src/tap.rs new file mode 100644 index 0000000..53d0f54 --- /dev/null +++ b/crates/voelin-audio/src/tap.rs @@ -0,0 +1,217 @@ +//! Taps: the processed microphone for consumers besides the voice +//! connection, such as the audio mixer of our screen share. +//! +//! The voice pipeline publishes every processed chunk (48 kHz mono, after +//! echo cancellation, noise suppression and gain control) to +//! [`microphone()`]. Unused, that is one relaxed atomic load per chunk. +//! With consumers attached ([`Tap::attach`]) the publisher hands the chunk +//! to each [`TapSink`] on its own thread; it never waits: the consumer list +//! is taken with a try-lock that only contends with attaching and +//! detaching, and a chunk that meets such a change is skipped. +//! +//! Several pipelines can run at once (one per server, the microphone +//! test); the tap follows one of them at a time: the first that publishes, +//! until it has been silent for [`HANDOVER`], then whichever publishes next. + +use std::sync::atomic::{AtomicU64, AtomicUsize, Ordering}; +use std::sync::{Mutex, OnceLock, PoisonError}; +use std::time::{Duration, Instant}; + +/// A publisher silent this long loses the tap to another one. +pub const HANDOVER: Duration = Duration::from_millis(200); + +/// Receives tapped audio on the publisher's thread. Must not block. +pub trait TapSink: Send { + /// 48 kHz mono samples. + fn write(&mut self, samples: &[f32]); + + /// `frames` samples of silence (e.g. while the microphone is muted). + fn silence(&mut self, frames: usize); +} + +/// A publish point that any number of consumers can attach to. +pub struct Tap { + /// Attached sinks: the only thing publishers look at when unused. + active: AtomicUsize, + sinks: Mutex)>>, + next_sink: AtomicU64, + /// The publisher followed now (0: none) and when it last published + /// (milliseconds since `epoch`). + owner: AtomicU64, + owner_seen: AtomicU64, +} + +static MICROPHONE: Tap = Tap::new(); +static NEXT_PUBLISHER: AtomicU64 = AtomicU64::new(1); + +/// Processed microphone audio, 48 kHz mono. +pub fn microphone() -> &'static Tap { + &MICROPHONE +} + +/// An id for a publisher (one per voice pipeline). +pub fn publisher_id() -> u64 { + NEXT_PUBLISHER.fetch_add(1, Ordering::Relaxed) +} + +fn now_ms() -> u64 { + static EPOCH: OnceLock = OnceLock::new(); + EPOCH.get_or_init(Instant::now).elapsed().as_millis() as u64 +} + +impl Default for Tap { + fn default() -> Self { + Self::new() + } +} + +impl Tap { + pub const fn new() -> Self { + Self { + active: AtomicUsize::new(0), + sinks: Mutex::new(Vec::new()), + next_sink: AtomicU64::new(1), + owner: AtomicU64::new(0), + owner_seen: AtomicU64::new(0), + } + } + + /// Whether anyone listens: publishers skip all work otherwise. + #[inline] + pub fn is_active(&self) -> bool { + self.active.load(Ordering::Relaxed) != 0 + } + + /// Attach a consumer until the returned guard is dropped. + pub fn attach(&self, sink: Box) -> TapGuard<'_> { + let id = self.next_sink.fetch_add(1, Ordering::Relaxed); + let mut sinks = self.sinks.lock().unwrap_or_else(PoisonError::into_inner); + sinks.push((id, sink)); + self.active.store(sinks.len(), Ordering::Relaxed); + TapGuard { tap: self, id } + } + + fn detach(&self, id: u64) { + let mut sinks = self.sinks.lock().unwrap_or_else(PoisonError::into_inner); + sinks.retain(|(i, _)| *i != id); + self.active.store(sinks.len(), Ordering::Relaxed); + } + + /// Whether `publisher` is the one followed now (taking over from one + /// that went silent). + fn follows(&self, publisher: u64) -> bool { + let now = now_ms(); + let owner = self.owner.load(Ordering::Acquire); + if owner != publisher { + let stale = now.saturating_sub(self.owner_seen.load(Ordering::Relaxed)) + >= HANDOVER.as_millis() as u64; + if owner != 0 && !stale { + return false; + } + if self + .owner + .compare_exchange(owner, publisher, Ordering::AcqRel, Ordering::Relaxed) + .is_err() + { + return false; + } + } + self.owner_seen.store(now, Ordering::Relaxed); + true + } + + /// Hand `samples` to every consumer (no-op without consumers, or when + /// another publisher is followed). + pub fn publish(&self, publisher: u64, samples: &[f32]) { + if !self.is_active() || samples.is_empty() || !self.follows(publisher) { + return; + } + if let Ok(mut sinks) = self.sinks.try_lock() { + for (_, sink) in sinks.iter_mut() { + sink.write(samples); + } + } + } + + /// Hand `frames` samples of silence to every consumer. + pub fn publish_silence(&self, publisher: u64, frames: usize) { + if !self.is_active() || frames == 0 || !self.follows(publisher) { + return; + } + if let Ok(mut sinks) = self.sinks.try_lock() { + for (_, sink) in sinks.iter_mut() { + sink.silence(frames); + } + } + } +} + +/// Detaches its consumer when dropped. +pub struct TapGuard<'a> { + tap: &'a Tap, + id: u64, +} + +impl Drop for TapGuard<'_> { + fn drop(&mut self) { + self.tap.detach(self.id); + } +} + +#[cfg(test)] +mod tests { + use std::sync::Arc; + + use super::*; + + #[derive(Clone, Default)] + struct Collect(Arc>>); + + impl TapSink for Collect { + fn write(&mut self, samples: &[f32]) { + self.0.lock().unwrap().extend_from_slice(samples); + } + + fn silence(&mut self, frames: usize) { + self.0.lock().unwrap().extend(std::iter::repeat_n(0.0, frames)); + } + } + + #[test] + fn consumers_attach_and_detach() { + let tap = Tap::new(); + let (a, b) = (publisher_id(), publisher_id()); + assert!(!tap.is_active()); + tap.publish(a, &[1.0]); + let first = Collect::default(); + let guard = tap.attach(Box::new(first.clone())); + assert!(tap.is_active()); + tap.publish(a, &[0.5, 0.25]); + tap.publish_silence(a, 2); + // Another publisher is ignored while the first is live. + tap.publish(b, &[9.0]); + let second = Collect::default(); + let guard2 = tap.attach(Box::new(second.clone())); + tap.publish(a, &[0.125]); + drop(guard); + tap.publish(a, &[0.0625]); + assert_eq!(*first.0.lock().unwrap(), [0.5, 0.25, 0.0, 0.0, 0.125]); + assert_eq!(*second.0.lock().unwrap(), [0.125, 0.0625]); + drop(guard2); + assert!(!tap.is_active()); + } + + #[test] + fn a_silent_publisher_hands_over() { + let tap = Tap::new(); + let (a, b) = (publisher_id(), publisher_id()); + let sink = Collect::default(); + let _guard = tap.attach(Box::new(sink.clone())); + tap.publish(a, &[1.0]); + tap.publish(b, &[2.0]); + std::thread::sleep(HANDOVER + Duration::from_millis(20)); + tap.publish(b, &[3.0]); + tap.publish(a, &[4.0]); + assert_eq!(*sink.0.lock().unwrap(), [1.0, 3.0]); + } +} diff --git a/crates/voelin-core/src/audio.rs b/crates/voelin-core/src/audio.rs index 0bc55d2..5d4602f 100644 --- a/crates/voelin-core/src/audio.rs +++ b/crates/voelin-core/src/audio.rs @@ -20,6 +20,7 @@ use tsclientlib::ClientId; use tsproto_packets::packets::{AudioData, CodecType, Direction, InAudioBuf, OutAudio, OutPacket}; use voelin_audio::pcm::{self, FRAME_SAMPLES}; use voelin_audio::settings::TransmitMode; +use voelin_audio::tap; use voelin_audio::vad::SILENCE_DB; use voelin_audio::{AudioSettings, Framer, Mixer, Processor, Vad, VoiceCodec, VoiceEncoder}; @@ -145,6 +146,10 @@ struct Pipeline { /// Watched streams whose audio plays through the mixer. streams: HashMap, stream_volumes: HashMap, + /// Where processed microphone audio is published (for our screen + /// share's microphone source), and our id as its publisher. + mic_tap: &'static tap::Tap, + tap_id: u64, /// Processed microphone audio, for tests. #[cfg(test)] tap: Vec, @@ -168,6 +173,8 @@ impl Pipeline { level_peak: None, streams: HashMap::new(), stream_volumes: HashMap::new(), + mic_tap: tap::microphone(), + tap_id: tap::publisher_id(), settings, #[cfg(test)] tap: Vec::new(), @@ -276,6 +283,15 @@ impl Pipeline { self.processor.capture(samples, &mut self.processed); #[cfg(test)] self.tap.extend_from_slice(&self.processed); + // Our screen share's microphone source; one atomic load when unused. + let mic = self.mic_tap; + if mic.is_active() { + if self.input_muted { + mic.publish_silence(self.tap_id, self.processed.len()); + } else { + mic.publish(self.tap_id, &self.processed); + } + } let mut frames = Vec::new(); self.framer.push(&self.processed, |f| frames.push(f.to_vec())); for frame in frames { @@ -576,6 +592,41 @@ mod tests { assert_eq!(capture(&mut p, &tone), (0, 0)); } + /// The processed microphone reaches tap consumers (silence while muted, + /// whatever the transmit mode), and nothing is done without one. + #[test] + fn processed_microphone_reaches_the_tap() { + use std::sync::{Arc, Mutex}; + + static TAP: tap::Tap = tap::Tap::new(); + struct Sink(Arc>>); + impl tap::TapSink for Sink { + fn write(&mut self, samples: &[f32]) { + self.0.lock().unwrap().extend_from_slice(samples); + } + fn silence(&mut self, frames: usize) { + self.0.lock().unwrap().extend(std::iter::repeat_n(0.0, frames)); + } + } + + let mut p = pipeline(TransmitMode::PushToTalk); + p.mic_tap = &TAP; + let tone = sine(440.0, 0.1, 0.3); + capture(&mut p, &tone); + let got = Arc::new(Mutex::new(Vec::new())); + let guard = TAP.attach(Box::new(Sink(got.clone()))); + // Not transmitting (push-to-talk released): still tapped. + assert_eq!(capture(&mut p, &tone), (0, 0)); + assert_eq!(*got.lock().unwrap(), tone); + p.handle(AudioIn::InputMuted(true)); + capture(&mut p, &tone); + let got_now = got.lock().unwrap().clone(); + assert_eq!(got_now.len(), 2 * tone.len()); + assert!(got_now[tone.len()..].iter().all(|s| *s == 0.0)); + drop(guard); + assert!(!TAP.is_active()); + } + #[test] fn voice_activation_with_pre_roll() { let mut p = pipeline(TransmitMode::VoiceActivation);