Wire Android endpoint classification into JNI enumeration
android-ci / android-arm64 (push) Successful in 16m54s

This commit is contained in:
bit9992 committed 2026-11-13 18:27:37 +03:00
1 parent 5060f5ec3c
commit 493f09aadf
2 files changed
+40 -80

No files matched your search

@@ -1,4 +1,4 @@
use num_derive::FromPrimitive;
pub use super::super::device_catalog::AudioDeviceType;
use crate::{DeviceDirection, SampleFormat};
@@ -72,47 +72,6 @@ pub struct AudioDeviceInfo {
pub formats: Box<[SampleFormat]>,
}
/**
* The type of audio device
*/
#[derive(Debug, Clone, Copy, Default, FromPrimitive)]
#[non_exhaustive]
#[repr(i32)]
pub enum AudioDeviceType {
#[default]
Unknown = 0,
AuxLine = 19,
BleBroadcast = 30,
BleHeadset = 26,
BleSpeaker = 27,
BluetoothA2DP = 8,
BluetoothSCO = 7,
BuiltinEarpiece = 1,
BuiltinMic = 15,
BuiltinSpeaker = 2,
BuiltinSpeakerSafe = 24,
Bus = 21,
Dock = 13,
Fm = 14,
FmTuner = 16,
Hdmi = 9,
HdmiArc = 10,
HdmiEarc = 29,
HearingAid = 23,
Ip = 20,
LineAnalog = 5,
LineDigital = 6,
RemoteSubmix = 25,
Telephony = 18,
TvTuner = 17,
UsbAccessory = 12,
UsbDevice = 11,
UsbHeadset = 22,
WiredHeadphones = 4,
WiredHeadset = 3,
Unsupported = -1,
}
/// Converts DeviceDirection to Android AudioManager device flags.
pub(super) fn android_device_flags(direction: DeviceDirection) -> i32 {
match direction {
@@ -47,47 +47,48 @@ fn try_request_devices_info<'j>(
direction: DeviceDirection,
) -> JResult<Vec<AudioDeviceInfo>> {
let audio_manager = get_system_service(env, context, Context::AUDIO_SERVICE)?;
let devices = get_devices(env, &audio_manager, android_device_flags(direction))?;
let length = devices.len(env)?;
let mut result = Vec::with_capacity(length as usize);
(0..length)
.map(|index| {
let device = devices.get_element(env, index)?;
let id = call_method_no_args_ret_int(env, &device, "getId")?;
let address = call_method_no_args_ret_string(env, &device, "getAddress")?;
let address = String::from(address.mutf8_chars(env)?);
let product_name =
call_method_no_args_ret_char_sequence(env, &device, "getProductName")?;
let product_name = String::from(product_name.mutf8_chars(env)?);
let device_type =
FromPrimitive::from_i32(call_method_no_args_ret_int(env, &device, "getType")?)
.unwrap_or(AudioDeviceType::Unsupported);
let is_source = call_method_no_args_ret_bool(env, &device, "isSource")?;
let is_sink = call_method_no_args_ret_bool(env, &device, "isSink")?;
let direction = crate::device_description::direction_from_caps(is_source, is_sink);
let channel_counts =
call_method_no_args_ret_int_array(env, &device, "getChannelCounts")?
.into_boxed_slice();
let sample_rates = call_method_no_args_ret_int_array(env, &device, "getSampleRates")?
for index in 0..length {
let device = devices.get_element(env, index)?;
let id = call_method_no_args_ret_int(env, &device, "getId")?;
let device_type =
FromPrimitive::from_i32(call_method_no_args_ret_int(env, &device, "getType")?)
.unwrap_or(AudioDeviceType::Unsupported);
let is_source = call_method_no_args_ret_bool(env, &device, "isSource")?;
let is_sink = call_method_no_args_ret_bool(env, &device, "isSink")?;
let (is_source, is_sink) = device_type.selectable_caps(is_source, is_sink);
if !is_source && !is_sink {
continue;
}
let direction = crate::device_description::direction_from_caps(is_source, is_sink);
let address = call_method_no_args_ret_string(env, &device, "getAddress")?;
let address = String::from(address.mutf8_chars(env)?);
let product_name =
call_method_no_args_ret_char_sequence(env, &device, "getProductName")?;
let product_name = String::from(product_name.mutf8_chars(env)?);
let channel_counts =
call_method_no_args_ret_int_array(env, &device, "getChannelCounts")?
.into_boxed_slice();
let formats = call_method_no_args_ret_int_array(env, &device, "getEncodings")?
.into_iter()
.filter_map(SampleFormat::from_encoding)
.collect::<Box<[_]>>();
let sample_rates = call_method_no_args_ret_int_array(env, &device, "getSampleRates")?
.into_boxed_slice();
let formats = call_method_no_args_ret_int_array(env, &device, "getEncodings")?
.into_iter()
.filter_map(SampleFormat::from_encoding)
.collect::<Box<[_]>>();
Ok(AudioDeviceInfo {
id,
address,
product_name,
device_type,
direction,
channel_counts,
sample_rates,
formats,
})
})
.collect::<Result<Vec<_>, _>>()
result.push(AudioDeviceInfo {
id,
address,
product_name,
device_type,
direction,
channel_counts,
sample_rates,
formats,
});
}
Ok(result)
}