Skip to content

Commit 51db650

Browse files
committed
wip
1 parent 3c22dfc commit 51db650

File tree

22 files changed

+3677
-780
lines changed

22 files changed

+3677
-780
lines changed

libwebrtc/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ pub mod video_track;
6666
pub mod native {
6767
pub use webrtc_sys::webrtc::ffi::create_random_uuid;
6868

69-
pub use crate::imp::{aec, audio_resampler, frame_cryptor, yuv_helper};
69+
pub use crate::imp::{apm, audio_resampler, frame_cryptor, yuv_helper};
7070
}
7171

7272
#[cfg(target_os = "android")]

libwebrtc/src/native/aec.rs

Lines changed: 0 additions & 51 deletions
This file was deleted.

libwebrtc/src/native/apm.rs

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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+
}

libwebrtc/src/native/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
pub mod aec;
15+
pub mod apm;
1616
#[cfg(target_os = "android")]
1717
pub mod android;
1818
pub mod audio_resampler;

livekit-api/src/services/egress.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ impl EgressClient {
120120
segment_outputs,
121121
image_outputs,
122122
output: None, // Deprecated
123+
..Default::default()
123124
},
124125
self.base
125126
.auth_header(VideoGrants { room_record: true, ..Default::default() }, None)?,

livekit-api/src/services/sip.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,7 @@ impl SIPClient {
452452
headers: Default::default(),
453453
include_headers: Default::default(),
454454
media_encryption: Default::default(),
455+
..Default::default()
455456
},
456457
self.base.auth_header(
457458
Default::default(),

livekit-ffi/protocol/audio_frame.proto

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -94,24 +94,38 @@ message RemixAndResampleResponse {
9494
// AEC
9595

9696

97-
message NewAecRequest {
98-
required uint32 sample_rate = 1;
99-
required uint32 num_channels = 2;
97+
message NewApmRequest {
98+
required bool echo_canceller_enabled = 1;
99+
required bool gain_controller_enabled = 2;
100+
required bool high_pass_filter_enabled = 3;
101+
required bool noise_suppression_enabled = 4;
100102
}
101-
message NewAecResponse {
102-
required OwnedAec aec = 1;
103+
message NewApmResponse {
104+
required OwnedApm apm = 1;
103105
}
104106

105-
message CancelEchoRequest {
106-
required uint64 aec_handle = 1;
107-
required uint64 cap_ptr = 2; // *mut i16
108-
required uint32 cap_size = 3; // in bytes
109-
required uint64 rend_ptr = 4; // *const i16
110-
required uint32 rend_size = 5;
107+
message ApmProcessStreamRequest {
108+
required uint64 apm_handle = 1;
109+
required uint64 data_ptr = 2; // *mut i16
110+
required uint32 size = 3; // in bytes
111+
required uint32 sample_rate = 4;
112+
required uint32 num_channels = 5;
111113
}
112114

113-
message CancelEchoResponse {
114-
optional string error = 3;
115+
message ApmProcessStreamResponse {
116+
optional string error = 1;
117+
}
118+
119+
message ApmProcessReverseStreamRequest {
120+
required uint64 apm_handle = 1;
121+
required uint64 data_ptr = 2; // *mut i16
122+
required uint32 size = 3; // in bytes
123+
required uint32 sample_rate = 4;
124+
required uint32 num_channels = 5;
125+
}
126+
127+
message ApmProcessReverseStreamResponse {
128+
optional string error = 1;
115129
}
116130

117131
// New resampler using SoX (much better quality)
@@ -267,7 +281,7 @@ message OwnedAudioResampler {
267281
// AEC
268282
//
269283

270-
message OwnedAec {
284+
message OwnedApm {
271285
required FfiOwnedHandle handle = 1;
272286
}
273287

livekit-ffi/protocol/ffi.proto

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,12 @@ message FfiRequest {
122122

123123
// Audio Filter Plugin
124124
LoadAudioFilterPluginRequest load_audio_filter_plugin = 49;
125+
126+
NewApmRequest new_apm = 50;
127+
ApmProcessStreamRequest apm_process_stream = 51;
128+
ApmProcessReverseStreamRequest apm_process_reverse_stream = 52;
129+
130+
// NEXT_ID: 53
125131
}
126132
}
127133

@@ -191,6 +197,12 @@ message FfiResponse {
191197

192198
// Audio Filter Plugin
193199
LoadAudioFilterPluginResponse load_audio_filter_plugin = 48;
200+
201+
NewApmResponse new_apm = 49;
202+
ApmProcessStreamResponse apm_process_stream = 50;
203+
ApmProcessReverseStreamResponse apm_process_reverse_stream = 51;
204+
205+
// NEXT_ID: 52
194206
}
195207
}
196208

0 commit comments

Comments
 (0)