* Use usize for various frame counters * Fix logs * Address review comments * cargo fmt
This commit is contained in:
Родитель
ef5f58b382
Коммит
3860bb3f95
|
@ -42,7 +42,7 @@ use std::mem;
|
|||
use std::os::raw::c_void;
|
||||
use std::ptr;
|
||||
use std::slice;
|
||||
use std::sync::atomic::{AtomicBool, AtomicI64, AtomicU32, AtomicU64, Ordering};
|
||||
use std::sync::atomic::{AtomicBool, AtomicU32, AtomicU64, AtomicUsize, Ordering};
|
||||
use std::sync::{Arc, Condvar, Mutex};
|
||||
use std::time::Duration;
|
||||
|
||||
|
@ -210,12 +210,12 @@ fn create_device_info(id: AudioDeviceID, devtype: DeviceType) -> Result<device_i
|
|||
|
||||
if id == kAudioObjectUnknown {
|
||||
info.id = default_device_id;
|
||||
cubeb_log!("Falling back to default device, after having requested a specific device.");
|
||||
cubeb_log!("Creating a default device info.");
|
||||
info.flags |= device_flags::DEV_SELECTED_DEFAULT;
|
||||
}
|
||||
|
||||
if info.id == default_device_id {
|
||||
cubeb_log!("Requesting default input device.");
|
||||
cubeb_log!("Requesting default system device.");
|
||||
info.flags |= device_flags::DEV_SYSTEM_DEFAULT;
|
||||
}
|
||||
|
||||
|
@ -330,13 +330,17 @@ fn get_volume(unit: AudioUnit) -> Result<f32> {
|
|||
}
|
||||
}
|
||||
|
||||
fn minimum_resampling_input_frames(input_rate: f64, output_rate: f64, output_frames: i64) -> i64 {
|
||||
fn minimum_resampling_input_frames(
|
||||
input_rate: f64,
|
||||
output_rate: f64,
|
||||
output_frames: usize,
|
||||
) -> usize {
|
||||
assert!(!approx_eq!(f64, input_rate, 0_f64));
|
||||
assert!(!approx_eq!(f64, output_rate, 0_f64));
|
||||
if approx_eq!(f64, input_rate, output_rate) {
|
||||
return output_frames;
|
||||
}
|
||||
(input_rate * output_frames as f64 / output_rate).ceil() as i64
|
||||
(input_rate * output_frames as f64 / output_rate).ceil() as usize
|
||||
}
|
||||
|
||||
fn audiounit_make_silent(io_data: &mut AudioBuffer) {
|
||||
|
@ -450,7 +454,7 @@ extern "C" fn audiounit_input_callback(
|
|||
|
||||
// Advance input frame counter.
|
||||
stm.frames_read
|
||||
.fetch_add(i64::from(input_frames), atomic::Ordering::SeqCst);
|
||||
.fetch_add(input_frames as usize, atomic::Ordering::SeqCst);
|
||||
|
||||
cubeb_logv!(
|
||||
"({:p}) input: buffers {}, size {}, channels {}, rendered frames {}, total frames {}.",
|
||||
|
@ -645,12 +649,13 @@ extern "C" fn audiounit_output_callback(
|
|||
let prev_frames_written = stm.frames_written.load(Ordering::SeqCst);
|
||||
|
||||
stm.frames_written
|
||||
.fetch_add(i64::from(output_frames), Ordering::SeqCst);
|
||||
.fetch_add(output_frames as usize, Ordering::SeqCst);
|
||||
|
||||
// Also get the input buffer if the stream is duplex
|
||||
let (input_buffer, mut input_frames) = if !stm.core_stream_data.input_unit.is_null() {
|
||||
assert!(stm.core_stream_data.input_linear_buffer.is_some());
|
||||
assert_ne!(stm.core_stream_data.input_desc.mChannelsPerFrame, 0);
|
||||
let input_channels = stm.core_stream_data.input_desc.mChannelsPerFrame 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
|
||||
|
@ -659,12 +664,8 @@ extern "C" fn audiounit_output_callback(
|
|||
let input_frames_needed = minimum_resampling_input_frames(
|
||||
stm.core_stream_data.input_hw_rate,
|
||||
f64::from(stm.core_stream_data.output_stream_params.rate()),
|
||||
output_frames as i64,
|
||||
output_frames as usize,
|
||||
);
|
||||
let missing_frames = input_frames_needed - stm.frames_read.load(Ordering::SeqCst);
|
||||
// Else if the input has buffered a lot already because the output started late, we
|
||||
// need to trim the input buffer
|
||||
let input_channels = stm.core_stream_data.input_desc.mChannelsPerFrame as usize;
|
||||
let buffered_input_frames = stm
|
||||
.core_stream_data
|
||||
.input_linear_buffer
|
||||
|
@ -672,6 +673,8 @@ extern "C" fn audiounit_output_callback(
|
|||
.unwrap()
|
||||
.elements()
|
||||
/ input_channels;
|
||||
// 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 {
|
||||
let samples_to_pop =
|
||||
(buffered_input_frames - input_frames_needed as usize) * input_channels;
|
||||
|
@ -681,30 +684,30 @@ extern "C" fn audiounit_output_callback(
|
|||
.unwrap()
|
||||
.pop(samples_to_pop);
|
||||
stm.frames_read
|
||||
.fetch_sub((samples_to_pop / input_channels) as i64, Ordering::SeqCst);
|
||||
.fetch_sub((samples_to_pop / input_channels) as usize, Ordering::SeqCst);
|
||||
}
|
||||
|
||||
let elements = (missing_frames
|
||||
* i64::from(stm.core_stream_data.input_desc.mChannelsPerFrame))
|
||||
as usize;
|
||||
if missing_frames > 0 {
|
||||
if input_frames_needed > buffered_input_frames {
|
||||
let silent_frames_to_push = input_frames_needed - buffered_input_frames;
|
||||
let silent_samples_to_push = silent_frames_to_push * input_channels;
|
||||
stm.core_stream_data
|
||||
.input_linear_buffer
|
||||
.as_mut()
|
||||
.unwrap()
|
||||
.push_zeros(elements);
|
||||
stm.frames_read.store(input_frames_needed, Ordering::SeqCst);
|
||||
.push_zeros(silent_samples_to_push);
|
||||
stm.frames_read
|
||||
.fetch_add(input_frames_needed, Ordering::SeqCst);
|
||||
cubeb_log!(
|
||||
"({:p}) Missing Frames {} pushed {} frames of input silence.",
|
||||
"({:p}) Missing Frames: {} pushed {} frames of input silence.",
|
||||
stm.core_stream_data.stm_ptr,
|
||||
if stm.frames_read.load(Ordering::SeqCst) == 0 {
|
||||
"Input hasn't started,"
|
||||
"input hasn't started,"
|
||||
} else if stm.switching_device.load(Ordering::SeqCst) {
|
||||
"Device switching,"
|
||||
"device switching,"
|
||||
} else {
|
||||
"Drop out,"
|
||||
"drop out,"
|
||||
},
|
||||
missing_frames
|
||||
silent_frames_to_push
|
||||
);
|
||||
}
|
||||
let input_frames = stm
|
||||
|
@ -3112,9 +3115,9 @@ struct AudioUnitStream<'ctx> {
|
|||
frames_queued: u64,
|
||||
// How many frames got read from the input since the stream started (includes
|
||||
// padded silence)
|
||||
frames_read: AtomicI64,
|
||||
frames_read: AtomicUsize,
|
||||
// How many frames got written to the output device since the stream started
|
||||
frames_written: AtomicI64,
|
||||
frames_written: AtomicUsize,
|
||||
shutdown: AtomicBool,
|
||||
draining: AtomicBool,
|
||||
reinit_pending: AtomicBool,
|
||||
|
@ -3145,8 +3148,8 @@ impl<'ctx> AudioUnitStream<'ctx> {
|
|||
device_changed_callback: Mutex::new(None),
|
||||
frames_played: AtomicU64::new(0),
|
||||
frames_queued: 0,
|
||||
frames_read: AtomicI64::new(0),
|
||||
frames_written: AtomicI64::new(0),
|
||||
frames_read: AtomicUsize::new(0),
|
||||
frames_written: AtomicUsize::new(0),
|
||||
shutdown: AtomicBool::new(true),
|
||||
draining: AtomicBool::new(false),
|
||||
reinit_pending: AtomicBool::new(false),
|
||||
|
|
|
@ -117,7 +117,7 @@ fn test_minimum_resampling_input_frames() {
|
|||
|
||||
let frames = 100;
|
||||
let times = input_rate / output_rate;
|
||||
let expected = (frames as f64 * times).ceil() as i64;
|
||||
let expected = (frames as f64 * times).ceil() as usize;
|
||||
|
||||
assert_eq!(
|
||||
minimum_resampling_input_frames(input_rate, output_rate, frames),
|
||||
|
|
Загрузка…
Ссылка в новой задаче