-
Couldn't load subscription status.
- Fork 445
fix(aaudio): Correctly detect minimum buffer size #1039
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
…UT_FRAMES_PER_BUFFER on the AudioManager not using AudioTrack
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the contribution!
| min_buffer_size | ||
| } else { | ||
| get_audio_record_min_buffer_size(sample_rate, channel_mask, android_format) | ||
| 0 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this could be refactored into something like:
if let Ok(min_buffer_size) = AudioManager::get_frames_per_buffer() {
SupportedBufferSize::Range {
min: min_buffer_size as u32,
max: i32::MAX as u32,
}
} else {
SupportedBufferSize::Unknown
}| android_media::ENCODING_PCM_FLOAT | ||
| }; | ||
| for (mask_idx, channel_mask) in CHANNEL_MASKS.iter().enumerate() { | ||
| for (mask_idx, _) in CHANNEL_MASKS.iter().enumerate() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we still need CHANNEL_MASKS? Because currently the elements that that array contains aren't used anymore.
| }; | ||
| for (mask_idx, channel_mask) in CHANNEL_MASKS.iter().enumerate() { | ||
| for (mask_idx, _) in CHANNEL_MASKS.iter().enumerate() { | ||
| let channel_count = mask_idx + 1; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
...but maybe they should because while this line works, it's kind of hackish (I know, not your code, nor was it mine 😉). It at some point a 5.1 or whatever mode is introduced, we should use a mapping instead of doing + 1.
|
|
||
| impl AudioManager { | ||
| /** | ||
| * Get the frames per buffer using Android Java API |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Style nitpick: please use Rustdoc-standard // comments instead of Javadoc-style /** .. */.
| let frames_per_buffer = get_property( | ||
| env, | ||
| &audio_manager, | ||
| AudioManager::PROPERTY_OUTPUT_FRAMES_PER_BUFFER, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Question: is this also valid for input devices? Though I see that AudioManager::PROPERTY_INPUT_FRAMES_PER_BUFFER does not exist.
| let mut output = Vec::with_capacity(SAMPLE_RATES.len() * CHANNEL_MASKS.len() * FORMATS.len()); | ||
| for sample_format in &FORMATS { | ||
| let android_format = if *sample_format == SampleFormat::I16 { | ||
| android_media::ENCODING_PCM_16BIT |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In android_media.rs can these constants now be removed?
Thanks for the review - will address all feedback in the morning. The Q around input is an interesting one, I couldn't see anything different for input in their docs but also I agree the property is named after output so... |
AAudio buffer size minimum should be detected using the PROPERTY_OUTPUT_FRAMES_PER_BUFFER on the AudioManager not using AudioTrack.
Android documentation reference: https://developer.android.com/ndk/guides/audio/audio-latency#buffer-size
This allows for a sensible buffer size minimum to be used in comparison to the 1 second+ buffer sizes reported by AudioTrack.
Tested on a Samsung Galaxy Tab A9:
SupportedStreamConfigRange { channels: 1, min_sample_rate: SampleRate(5512), max_sample_rate: SampleRate(5512), buffer_size: Range { min: 256, max: 2147483647 }, sample_format: I16 }Closes: #890