BlueGit

ict rusty-teams

Files

rusty-teams / vendor / libwebrtc / src / native / apm.rs

apm.rs

apm.rs 3.7 KiB Download Raw
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
15use cxx::UniquePtr;
16use webrtc_sys::apm::ffi as sys_apm;
17
18use crate::{RtcError, RtcErrorType};
19
20pub struct AudioProcessingModule {
21 sys_handle: UniquePtr<sys_apm::AudioProcessingModule>,
22}
23
24impl 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_per_10ms = (sample_rate as usize / 100) * num_channels as usize;
48 assert!(
49 data.len() % samples_per_10ms == 0 && data.len() >= samples_per_10ms,
50 "slice must have a multiple of 10ms worth of samples"
51 );
52
53 for chunk in data.chunks_mut(samples_per_10ms) {
54 if unsafe {
55 self.sys_handle.pin_mut().process_stream(
56 chunk.as_mut_ptr(),
57 chunk.len(),
58 chunk.as_mut_ptr(),
59 chunk.len(),
60 sample_rate,
61 num_channels,
62 )
63 } != 0
64 {
65 return Err(RtcError {
66 error_type: RtcErrorType::Internal,
67 message: "Failed to process stream".to_string(),
68 });
69 }
70 }
71
72 Ok(())
73 }
74
75 pub fn process_reverse_stream(
76 &mut self,
77 data: &mut [i16],
78 sample_rate: i32,
79 num_channels: i32,
80 ) -> Result<(), RtcError> {
81 let samples_per_10ms = (sample_rate as usize / 100) * num_channels as usize;
82 assert!(
83 data.len() % samples_per_10ms == 0 && data.len() >= samples_per_10ms,
84 "slice must have a multiple of 10ms worth of samples"
85 );
86
87 for chunk in data.chunks_mut(samples_per_10ms) {
88 if unsafe {
89 self.sys_handle.pin_mut().process_reverse_stream(
90 chunk.as_mut_ptr(),
91 chunk.len(),
92 chunk.as_mut_ptr(),
93 chunk.len(),
94 sample_rate,
95 num_channels,
96 )
97 } != 0
98 {
99 return Err(RtcError {
100 error_type: RtcErrorType::Internal,
101 message: "Failed to process reverse stream".to_string(),
102 });
103 }
104 }
105
106 Ok(())
107 }
108
109 pub fn set_stream_delay_ms(&mut self, delay_ms: i32) -> Result<(), RtcError> {
110 if self.sys_handle.pin_mut().set_stream_delay_ms(delay_ms) == 0 {
111 Ok(())
112 } else {
113 Err(RtcError {
114 error_type: RtcErrorType::Internal,
115 message: "Failed to set stream delay".to_string(),
116 })
117 }
118 }
119}