use crate::{enum_dispatch, imp::audio_source as imp_as};
pub const DEFAULT_SAMPLE_RATE: u32 = 48000;
pub const DEFAULT_NUM_CHANNELS: u32 = 1;
#[derive(Default, Debug)]
pub struct AudioSourceOptions {
pub echo_cancellation: bool,
pub noise_suppression: bool,
pub auto_gain_control: bool,
}
#[non_exhaustive]
#[derive(Debug, Clone)]
pub enum RtcAudioSource {
#[cfg(not(target_arch = "wasm32"))]
Native(native::NativeAudioSource),
#[cfg(not(target_arch = "wasm32"))]
Device,
}
impl RtcAudioSource {
pub fn set_audio_options(&self, options: AudioSourceOptions) {
match self {
#[cfg(not(target_arch = "wasm32"))]
RtcAudioSource::Native(source) => source.set_audio_options(options),
#[cfg(not(target_arch = "wasm32"))]
RtcAudioSource::Device => {
}
}
}
pub fn audio_options(&self) -> AudioSourceOptions {
match self {
#[cfg(not(target_arch = "wasm32"))]
RtcAudioSource::Native(source) => source.audio_options(),
#[cfg(not(target_arch = "wasm32"))]
RtcAudioSource::Device => AudioSourceOptions::default(),
}
}
pub fn sample_rate(&self) -> u32 {
match self {
#[cfg(not(target_arch = "wasm32"))]
RtcAudioSource::Native(source) => source.sample_rate(),
#[cfg(not(target_arch = "wasm32"))]
RtcAudioSource::Device => DEFAULT_SAMPLE_RATE,
}
}
pub fn num_channels(&self) -> u32 {
match self {
#[cfg(not(target_arch = "wasm32"))]
RtcAudioSource::Native(source) => source.num_channels(),
#[cfg(not(target_arch = "wasm32"))]
RtcAudioSource::Device => DEFAULT_NUM_CHANNELS,
}
}
}
#[cfg(not(target_arch = "wasm32"))]
pub mod native {
use std::fmt::{Debug, Formatter};
use super::*;
use crate::{audio_frame::AudioFrame, RtcError};
#[derive(Clone)]
pub struct NativeAudioSource {
pub(crate) handle: imp_as::NativeAudioSource,
}
impl Debug for NativeAudioSource {
fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
f.debug_struct("NativeAudioSource").finish()
}
}
impl NativeAudioSource {
pub fn new(
options: AudioSourceOptions,
sample_rate: u32,
num_channels: u32,
queue_size_ms: u32,
) -> NativeAudioSource {
Self {
handle: imp_as::NativeAudioSource::new(
options,
sample_rate,
num_channels,
queue_size_ms,
),
}
}
pub fn clear_buffer(&self) {
self.handle.clear_buffer()
}
pub async fn capture_frame(&self, frame: &AudioFrame<'_>) -> Result<(), RtcError> {
self.handle.capture_frame(frame).await
}
pub fn set_audio_options(&self, options: AudioSourceOptions) {
self.handle.set_audio_options(options)
}
pub fn audio_options(&self) -> AudioSourceOptions {
self.handle.audio_options()
}
pub fn sample_rate(&self) -> u32 {
self.handle.sample_rate()
}
pub fn num_channels(&self) -> u32 {
self.handle.num_channels()
}
}
}