Skip to main content

vector_calls/
share.rs

1//! A shared screen's sound on its way to the engine: interleaved samples at the
2//! capture's own rate. Lives on the call, not the engine, so a device change does
3//! not lose what was in flight.
4
5use std::sync::atomic::{AtomicBool, AtomicU32, Ordering};
6
7use crate::ring::SpscRing;
8
9pub struct ShareInput {
10    ring: SpscRing,
11    rate: AtomicU32,
12    channels: AtomicU32,
13    pub active: AtomicBool,
14}
15
16impl Default for ShareInput {
17    fn default() -> Self {
18        Self::new()
19    }
20}
21
22impl ShareInput {
23    pub fn new() -> Self {
24        Self {
25            // Two seconds of stereo at 48 kHz.
26            ring: SpscRing::new(192_000),
27            rate: AtomicU32::new(48_000),
28            channels: AtomicU32::new(2),
29            active: AtomicBool::new(false),
30        }
31    }
32
33    pub fn push(&self, rate: u32, channels: u8, samples: &[f32]) {
34        self.rate.store(rate, Ordering::Relaxed);
35        self.channels.store(channels as u32, Ordering::Relaxed);
36        self.ring.push(samples);
37    }
38
39    pub fn ring(&self) -> &SpscRing {
40        &self.ring
41    }
42
43    pub fn rate(&self) -> u32 {
44        self.rate.load(Ordering::Relaxed)
45    }
46
47    pub fn channels(&self) -> u32 {
48        self.channels.load(Ordering::Relaxed)
49    }
50}