Always use frame count to read and write BufferManager

This commit is contained in:
Chun-Min Chang 2022-04-12 17:32:09 -07:00 коммит произвёл Paul Adenot
Родитель 6b29728958
Коммит a5a21ccbcc
2 изменённых файлов: 17 добавлений и 17 удалений

Просмотреть файл

@ -179,7 +179,7 @@ impl BufferManager {
}
}
}
pub fn channel_count(&self) -> usize {
fn channel_count(&self) -> usize {
self.output_channel_count
}
pub fn push_data(&mut self, data: *mut c_void, frame_count: usize) {
@ -239,22 +239,23 @@ impl BufferManager {
}
}
}
pub fn get_linear_data(&mut self, nsamples: usize) -> *mut c_void {
pub fn get_linear_data(&mut self, frame_count: usize) -> *mut c_void {
let sample_count = frame_count * self.channel_count();
let p = match &mut self.linear_buffer {
LinearBuffer::IntegerLinearBuffer(b) => {
b.resize(nsamples, 0);
b.resize(sample_count, 0);
b.as_mut_ptr() as *mut c_void
}
LinearBuffer::FloatLinearBuffer(b) => {
b.resize(nsamples, 0.);
b.resize(sample_count, 0.);
b.as_mut_ptr() as *mut c_void
}
};
self.pull_data(p, nsamples);
self.pull_data(p, sample_count);
p
}
pub fn available_samples(&self) -> usize {
fn available_samples(&self) -> usize {
match &self.consumer {
IntegerRingBufferConsumer(p) => p.len(),
FloatRingBufferConsumer(p) => p.len(),
@ -264,18 +265,19 @@ impl BufferManager {
assert_ne!(self.channel_count(), 0);
self.available_samples() / self.channel_count()
}
pub fn trim(&mut self, final_size: usize) {
pub fn trim(&mut self, final_frame_count: usize) {
let final_sample_count = final_frame_count * self.channel_count();
match &mut self.consumer {
IntegerRingBufferConsumer(c) => {
let available = c.len();
assert!(available >= final_size);
let to_pop = available - final_size;
assert!(available >= final_sample_count);
let to_pop = available - final_sample_count;
c.discard(to_pop);
}
FloatRingBufferConsumer(c) => {
let available = c.len();
assert!(available >= final_size);
let to_pop = available - final_size;
assert!(available >= final_sample_count);
let to_pop = available - final_sample_count;
c.discard(to_pop);
}
}

Просмотреть файл

@ -426,7 +426,7 @@ extern "C" fn audiounit_input_callback(
);
let mut total_input_frames = input_buffer_manager.available_frames() as i64;
let input_buffer =
input_buffer_manager.get_linear_data(input_buffer_manager.available_samples());
input_buffer_manager.get_linear_data(input_buffer_manager.available_frames());
let outframes = stm.core_stream_data.resampler.fill(
input_buffer,
&mut total_input_frames,
@ -583,7 +583,6 @@ extern "C" fn audiounit_output_callback(
let (input_buffer, mut input_frames) = if !stm.core_stream_data.input_unit.is_null() {
let input_buffer_manager = stm.core_stream_data.input_buffer_manager.as_mut().unwrap();
assert_ne!(stm.core_stream_data.input_dev_desc.mChannelsPerFrame, 0);
let input_channels = input_buffer_manager.channel_count() as usize;
// If the output callback came first and this is a duplex stream, we need to
// fill in some additional silence in the resampler.
// Otherwise, if we had more than expected callbacks in a row, or we're
@ -594,11 +593,11 @@ extern "C" fn audiounit_output_callback(
f64::from(stm.core_stream_data.output_stream_params.rate()),
output_frames as usize,
);
let buffered_input_frames = input_buffer_manager.available_samples() / input_channels;
let buffered_input_frames = input_buffer_manager.available_frames();
// Else if the input has buffered a lot already because the output started late, we
// need to trim the input buffer
if prev_frames_written == 0 && buffered_input_frames > input_frames_needed as usize {
input_buffer_manager.trim(input_frames_needed * input_channels);
input_buffer_manager.trim(input_frames_needed);
let popped_frames = buffered_input_frames - input_frames_needed as usize;
cubeb_log!("Dropping {} frames in input buffer.", popped_frames);
}
@ -627,10 +626,9 @@ extern "C" fn audiounit_output_callback(
buffered_input_frames
};
let input_samples_needed = input_frames * input_channels;
stm.frames_read.fetch_add(input_frames, Ordering::SeqCst);
(
input_buffer_manager.get_linear_data(input_samples_needed),
input_buffer_manager.get_linear_data(input_frames),
input_frames as i64,
)
} else {