libwebrtc/
audio_source.rs

1// Copyright 2023 LiveKit, Inc.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use livekit_protocol::enum_dispatch;
16
17use crate::imp::audio_source as imp_as;
18
19#[derive(Default, Debug)]
20pub struct AudioSourceOptions {
21    pub echo_cancellation: bool,
22    pub noise_suppression: bool,
23    pub auto_gain_control: bool,
24}
25
26#[non_exhaustive]
27#[derive(Debug, Clone)]
28pub enum RtcAudioSource {
29    #[cfg(not(target_arch = "wasm32"))]
30    Native(native::NativeAudioSource),
31}
32
33impl RtcAudioSource {
34    enum_dispatch!(
35        [Native];
36        fn set_audio_options(self: &Self, options: AudioSourceOptions) -> ();
37        fn audio_options(self: &Self) -> AudioSourceOptions;
38        fn sample_rate(self: &Self) -> u32;
39        fn num_channels(self: &Self) -> u32;
40    );
41}
42
43#[cfg(not(target_arch = "wasm32"))]
44pub mod native {
45    use std::fmt::{Debug, Formatter};
46
47    use super::*;
48    use crate::{audio_frame::AudioFrame, RtcError};
49
50    #[derive(Clone)]
51    pub struct NativeAudioSource {
52        pub(crate) handle: imp_as::NativeAudioSource,
53    }
54
55    impl Debug for NativeAudioSource {
56        fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
57            f.debug_struct("NativeAudioSource").finish()
58        }
59    }
60
61    impl NativeAudioSource {
62        pub fn new(
63            options: AudioSourceOptions,
64            sample_rate: u32,
65            num_channels: u32,
66            queue_size_ms: u32,
67        ) -> NativeAudioSource {
68            Self {
69                handle: imp_as::NativeAudioSource::new(
70                    options,
71                    sample_rate,
72                    num_channels,
73                    queue_size_ms,
74                ),
75            }
76        }
77
78        pub fn clear_buffer(&self) {
79            self.handle.clear_buffer()
80        }
81
82        pub async fn capture_frame(&self, frame: &AudioFrame<'_>) -> Result<(), RtcError> {
83            self.handle.capture_frame(frame).await
84        }
85
86        pub fn set_audio_options(&self, options: AudioSourceOptions) {
87            self.handle.set_audio_options(options)
88        }
89
90        pub fn audio_options(&self) -> AudioSourceOptions {
91            self.handle.audio_options()
92        }
93
94        pub fn sample_rate(&self) -> u32 {
95            self.handle.sample_rate()
96        }
97
98        pub fn num_channels(&self) -> u32 {
99            self.handle.num_channels()
100        }
101    }
102}