| 1 | // Copyright 2025 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 | |
| 15 | use crate::impl_thread_safety; |
| 16 | |
| 17 | #[cxx::bridge(namespace = "livekit_ffi")] |
| 18 | pub mod ffi { |
| 19 | unsafe extern "C++" { |
| 20 | include!("livekit/apm.h"); |
| 21 | |
| 22 | type AudioProcessingModule; |
| 23 | |
| 24 | unsafe fn process_stream( |
| 25 | self: Pin<&mut AudioProcessingModule>, |
| 26 | src: *const i16, |
| 27 | src_len: usize, |
| 28 | dst: *mut i16, |
| 29 | dst_len: usize, |
| 30 | sample_rate: i32, |
| 31 | num_channels: i32, |
| 32 | ) -> i32; |
| 33 | |
| 34 | unsafe fn process_reverse_stream( |
| 35 | self: Pin<&mut AudioProcessingModule>, |
| 36 | src: *const i16, |
| 37 | src_len: usize, |
| 38 | dst: *mut i16, |
| 39 | dst_len: usize, |
| 40 | sample_rate: i32, |
| 41 | num_channels: i32, |
| 42 | ) -> i32; |
| 43 | |
| 44 | fn set_stream_delay_ms(self: Pin<&mut AudioProcessingModule>, delay: i32) -> i32; |
| 45 | |
| 46 | fn create_apm( |
| 47 | echo_canceller_enabled: bool, |
| 48 | gain_controller_enabled: bool, |
| 49 | high_pass_filter_enabled: bool, |
| 50 | noise_suppression_enabled: bool, |
| 51 | ) -> UniquePtr<AudioProcessingModule>; |
| 52 | |
| 53 | fn create_gain_apm( |
| 54 | adaptive: bool, |
| 55 | initial_gain_db: f32, |
| 56 | fixed_gain_db: f32, |
| 57 | max_gain_change_db_per_second: f32, |
| 58 | ) -> UniquePtr<AudioProcessingModule>; |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | impl_thread_safety!(ffi::AudioProcessingModule, Send + Sync); |
| 63 | |
| 64 | /// A gain-only AGC2 module, so callers do not need the cxx crate themselves. |
| 65 | pub struct GainProcessor { |
| 66 | handle: cxx::UniquePtr<ffi::AudioProcessingModule>, |
| 67 | } |
| 68 | |
| 69 | impl GainProcessor { |
| 70 | pub fn new(adaptive: bool, initial_gain_db: f32, fixed_gain_db: f32, max_gain_change_db_per_second: f32) -> Self { |
| 71 | Self { handle: ffi::create_gain_apm(adaptive, initial_gain_db, fixed_gain_db, max_gain_change_db_per_second) } |
| 72 | } |
| 73 | |
| 74 | /// Processes one 10 ms mono frame in place; returns false when the module is missing or rejects the frame. |
| 75 | pub fn process(&mut self, frame: &mut [i16], sample_rate: i32) -> bool { |
| 76 | if self.handle.is_null() { |
| 77 | return false; |
| 78 | } |
| 79 | let length = frame.len(); |
| 80 | let pointer = frame.as_mut_ptr(); |
| 81 | unsafe { self.handle.pin_mut().process_stream(pointer, length, pointer, length, sample_rate, 1) == 0 } |
| 82 | } |
| 83 | } |