Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 48 additions & 40 deletions libwebrtc/src/native/apm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,28 +46,32 @@ impl AudioProcessingModule {
sample_rate: i32,
num_channels: i32,
) -> Result<(), RtcError> {
let samples_count = (sample_rate as usize / 100) * num_channels as usize;
assert_eq!(data.len(), samples_count, "slice must have 10ms worth of samples");
let samples_per_10ms = (sample_rate as usize / 100) * num_channels as usize;
assert!(
data.len() % samples_per_10ms == 0 && data.len() >= samples_per_10ms,
"slice must have a multiple of 10ms worth of samples"
);

if unsafe {
// using the same slice for src and dst is safe
self.sys_handle.pin_mut().process_stream(
data.as_mut_ptr(),
data.len(),
data.as_mut_ptr(),
data.len(),
sample_rate,
num_channels,
)
} == 0
{
Ok(())
} else {
Err(RtcError {
error_type: RtcErrorType::Internal,
message: "Failed to process stream".to_string(),
})
for chunk in data.chunks_mut(samples_per_10ms) {
if unsafe {
self.sys_handle.pin_mut().process_stream(
chunk.as_mut_ptr(),
chunk.len(),
chunk.as_mut_ptr(),
chunk.len(),
sample_rate,
num_channels,
)
} != 0
{
return Err(RtcError {
error_type: RtcErrorType::Internal,
message: "Failed to process stream".to_string(),
});
}
}

Ok(())
}

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

if unsafe {
// using the same slice for src and dst is safe
self.sys_handle.pin_mut().process_reverse_stream(
data.as_mut_ptr(),
data.len(),
data.as_mut_ptr(),
data.len(),
sample_rate,
num_channels,
)
} == 0
{
Ok(())
} else {
Err(RtcError {
error_type: RtcErrorType::Internal,
message: "Failed to process reverse stream".to_string(),
})
for chunk in data.chunks_mut(samples_per_10ms) {
if unsafe {
self.sys_handle.pin_mut().process_reverse_stream(
chunk.as_mut_ptr(),
chunk.len(),
chunk.as_mut_ptr(),
chunk.len(),
sample_rate,
num_channels,
)
} != 0
{
return Err(RtcError {
error_type: RtcErrorType::Internal,
message: "Failed to process reverse stream".to_string(),
});
}
}

Ok(())
}

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