|
| 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 | + |
| 15 | +use cxx::UniquePtr; |
| 16 | +use webrtc_sys::apm::ffi as sys_apm; |
| 17 | + |
| 18 | +use crate::{RtcError, RtcErrorType}; |
| 19 | + |
| 20 | +pub struct AudioProcessingModule { |
| 21 | + sys_handle: UniquePtr<sys_apm::AudioProcessingModule>, |
| 22 | +} |
| 23 | + |
| 24 | +impl AudioProcessingModule { |
| 25 | + pub fn new( |
| 26 | + echo_canceller_enabled: bool, |
| 27 | + gain_controller_enabled: bool, |
| 28 | + high_pass_filter_enabled: bool, |
| 29 | + noise_suppression_enabled: bool, |
| 30 | + ) -> Self { |
| 31 | + Self { |
| 32 | + sys_handle: sys_apm::create_apm( |
| 33 | + echo_canceller_enabled, |
| 34 | + gain_controller_enabled, |
| 35 | + high_pass_filter_enabled, |
| 36 | + noise_suppression_enabled, |
| 37 | + ), |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + pub fn process_stream( |
| 42 | + &mut self, |
| 43 | + data: &mut [i16], |
| 44 | + sample_rate: i32, |
| 45 | + num_channels: i32, |
| 46 | + ) -> Result<(), RtcError> { |
| 47 | + let samples_count = (sample_rate as usize / 100) * num_channels as usize; |
| 48 | + assert_eq!(data.len(), samples_count, "slice must have 10ms worth of samples"); |
| 49 | + |
| 50 | + if unsafe { |
| 51 | + // using the same slice for src and dst is safe |
| 52 | + self.sys_handle.pin_mut().process_stream( |
| 53 | + data.as_mut_ptr(), |
| 54 | + data.len(), |
| 55 | + data.as_mut_ptr(), |
| 56 | + data.len(), |
| 57 | + sample_rate, |
| 58 | + num_channels, |
| 59 | + ) |
| 60 | + } == 0 |
| 61 | + { |
| 62 | + Ok(()) |
| 63 | + } else { |
| 64 | + Err(RtcError { |
| 65 | + error_type: RtcErrorType::Internal, |
| 66 | + message: "Failed to process stream".to_string(), |
| 67 | + }) |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + pub fn process_reverse_stream( |
| 72 | + &mut self, |
| 73 | + data: &mut [i16], |
| 74 | + sample_rate: i32, |
| 75 | + num_channels: i32, |
| 76 | + ) -> Result<(), RtcError> { |
| 77 | + let samples_count = (sample_rate as usize / 100) * num_channels as usize; |
| 78 | + assert_eq!(data.len(), samples_count, "slice must have 10ms worth of samples"); |
| 79 | + |
| 80 | + if unsafe { |
| 81 | + // using the same slice for src and dst is safe |
| 82 | + self.sys_handle.pin_mut().process_reverse_stream( |
| 83 | + data.as_mut_ptr(), |
| 84 | + data.len(), |
| 85 | + data.as_mut_ptr(), |
| 86 | + data.len(), |
| 87 | + sample_rate, |
| 88 | + num_channels, |
| 89 | + ) |
| 90 | + } == 0 |
| 91 | + { |
| 92 | + Ok(()) |
| 93 | + } else { |
| 94 | + Err(RtcError { |
| 95 | + error_type: RtcErrorType::Internal, |
| 96 | + message: "Failed to process reverse stream".to_string(), |
| 97 | + }) |
| 98 | + } |
| 99 | + } |
| 100 | +} |
0 commit comments