Skip to content

Commit 41154ba

Browse files
authored
allow apm >=10ms frames (#843)
1 parent 1594343 commit 41154ba

File tree

1 file changed

+48
-40
lines changed

1 file changed

+48
-40
lines changed

libwebrtc/src/native/apm.rs

Lines changed: 48 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -46,28 +46,32 @@ impl AudioProcessingModule {
4646
sample_rate: i32,
4747
num_channels: i32,
4848
) -> Result<(), RtcError> {
49-
let samples_count = (sample_rate as usize / 100) * num_channels as usize;
50-
assert_eq!(data.len(), samples_count, "slice must have 10ms worth of samples");
49+
let samples_per_10ms = (sample_rate as usize / 100) * num_channels as usize;
50+
assert!(
51+
data.len() % samples_per_10ms == 0 && data.len() >= samples_per_10ms,
52+
"slice must have a multiple of 10ms worth of samples"
53+
);
5154

52-
if unsafe {
53-
// using the same slice for src and dst is safe
54-
self.sys_handle.pin_mut().process_stream(
55-
data.as_mut_ptr(),
56-
data.len(),
57-
data.as_mut_ptr(),
58-
data.len(),
59-
sample_rate,
60-
num_channels,
61-
)
62-
} == 0
63-
{
64-
Ok(())
65-
} else {
66-
Err(RtcError {
67-
error_type: RtcErrorType::Internal,
68-
message: "Failed to process stream".to_string(),
69-
})
55+
for chunk in data.chunks_mut(samples_per_10ms) {
56+
if unsafe {
57+
self.sys_handle.pin_mut().process_stream(
58+
chunk.as_mut_ptr(),
59+
chunk.len(),
60+
chunk.as_mut_ptr(),
61+
chunk.len(),
62+
sample_rate,
63+
num_channels,
64+
)
65+
} != 0
66+
{
67+
return Err(RtcError {
68+
error_type: RtcErrorType::Internal,
69+
message: "Failed to process stream".to_string(),
70+
});
71+
}
7072
}
73+
74+
Ok(())
7175
}
7276

7377
pub fn process_reverse_stream(
@@ -76,28 +80,32 @@ impl AudioProcessingModule {
7680
sample_rate: i32,
7781
num_channels: i32,
7882
) -> Result<(), RtcError> {
79-
let samples_count = (sample_rate as usize / 100) * num_channels as usize;
80-
assert_eq!(data.len(), samples_count, "slice must have 10ms worth of samples");
83+
let samples_per_10ms = (sample_rate as usize / 100) * num_channels as usize;
84+
assert!(
85+
data.len() % samples_per_10ms == 0 && data.len() >= samples_per_10ms,
86+
"slice must have a multiple of 10ms worth of samples"
87+
);
8188

82-
if unsafe {
83-
// using the same slice for src and dst is safe
84-
self.sys_handle.pin_mut().process_reverse_stream(
85-
data.as_mut_ptr(),
86-
data.len(),
87-
data.as_mut_ptr(),
88-
data.len(),
89-
sample_rate,
90-
num_channels,
91-
)
92-
} == 0
93-
{
94-
Ok(())
95-
} else {
96-
Err(RtcError {
97-
error_type: RtcErrorType::Internal,
98-
message: "Failed to process reverse stream".to_string(),
99-
})
89+
for chunk in data.chunks_mut(samples_per_10ms) {
90+
if unsafe {
91+
self.sys_handle.pin_mut().process_reverse_stream(
92+
chunk.as_mut_ptr(),
93+
chunk.len(),
94+
chunk.as_mut_ptr(),
95+
chunk.len(),
96+
sample_rate,
97+
num_channels,
98+
)
99+
} != 0
100+
{
101+
return Err(RtcError {
102+
error_type: RtcErrorType::Internal,
103+
message: "Failed to process reverse stream".to_string(),
104+
});
105+
}
100106
}
107+
108+
Ok(())
101109
}
102110

103111
pub fn set_stream_delay_ms(&mut self, delay_ms: i32) -> Result<(), RtcError> {

0 commit comments

Comments
 (0)