| Lift freeq's AV media plane out of sleek 90f8b89 nandi 19d ago | 1 | use num_derive::FromPrimitive; |
| 2 | |
| 3 | use crate::{DeviceDirection, SampleFormat}; |
| 4 | |
| 5 | pub(crate) struct Context; |
| 6 | |
| 7 | impl Context { |
| 8 | pub const AUDIO_SERVICE: &'static str = "audio"; |
| 9 | } |
| 10 | |
| 11 | pub(crate) struct PackageManager; |
| 12 | |
| 13 | impl PackageManager { |
| 14 | pub const FEATURE_AUDIO_LOW_LATENCY: &'static str = "android.hardware.audio.low_latency"; |
| 15 | pub const FEATURE_AUDIO_OUTPUT: &'static str = "android.hardware.audio.output"; |
| 16 | pub const FEATURE_AUDIO_PRO: &'static str = "android.hardware.audio.pro"; |
| 17 | pub const FEATURE_MICROPHONE: &'static str = "android.hardware.microphone"; |
| 18 | pub const FEATURE_MIDI: &'static str = "android.software.midi"; |
| 19 | } |
| 20 | |
| 21 | pub(crate) struct AudioManager; |
| 22 | |
| 23 | impl AudioManager { |
| 24 | pub const PROPERTY_OUTPUT_FRAMES_PER_BUFFER: &'static str = |
| 25 | "android.media.property.OUTPUT_FRAMES_PER_BUFFER"; |
| 26 | |
| 27 | pub const GET_DEVICES_INPUTS: i32 = 1 << 0; |
| 28 | pub const GET_DEVICES_OUTPUTS: i32 = 1 << 1; |
| 29 | pub const GET_DEVICES_ALL: i32 = Self::GET_DEVICES_INPUTS | Self::GET_DEVICES_OUTPUTS; |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * The Android audio device info |
| 34 | */ |
| 35 | #[derive(Debug, Clone)] |
| 36 | pub struct AudioDeviceInfo { |
| 37 | /** |
| 38 | * Device identifier |
| 39 | */ |
| 40 | pub id: i32, |
| 41 | |
| 42 | /** |
| 43 | * The type of device |
| 44 | */ |
| 45 | pub device_type: AudioDeviceType, |
| 46 | |
| 47 | /** |
| 48 | * The device can be used for playback and/or capture |
| 49 | */ |
| 50 | pub direction: DeviceDirection, |
| 51 | |
| 52 | /** |
| 53 | * Device address |
| 54 | */ |
| 55 | pub address: String, |
| 56 | |
| 57 | /** |
| 58 | * Device product name |
| 59 | */ |
| 60 | pub product_name: String, |
| 61 | |
| 62 | /** |
| 63 | * Available channel configurations |
| 64 | */ |
| 65 | pub channel_counts: Vec<i32>, |
| 66 | |
| 67 | /** |
| 68 | * Supported sample rates |
| 69 | */ |
| 70 | pub sample_rates: Vec<i32>, |
| 71 | |
| 72 | /** |
| 73 | * Supported audio formats |
| 74 | */ |
| 75 | pub formats: Vec<SampleFormat>, |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * The type of audio device |
| 80 | */ |
| 81 | #[derive(Debug, Clone, Copy, FromPrimitive)] |
| 82 | #[non_exhaustive] |
| 83 | #[repr(i32)] |
| 84 | pub enum AudioDeviceType { |
| 85 | Unknown = 0, |
| 86 | AuxLine = 19, |
| 87 | BleBroadcast = 30, |
| 88 | BleHeadset = 26, |
| 89 | BleSpeaker = 27, |
| 90 | BluetoothA2DP = 8, |
| 91 | BluetoothSCO = 7, |
| 92 | BuiltinEarpiece = 1, |
| 93 | BuiltinMic = 15, |
| 94 | BuiltinSpeaker = 2, |
| 95 | BuiltinSpeakerSafe = 24, |
| 96 | Bus = 21, |
| 97 | Dock = 13, |
| 98 | Fm = 14, |
| 99 | FmTuner = 16, |
| 100 | Hdmi = 9, |
| 101 | HdmiArc = 10, |
| 102 | HdmiEarc = 29, |
| 103 | HearingAid = 23, |
| 104 | Ip = 20, |
| 105 | LineAnalog = 5, |
| 106 | LineDigital = 6, |
| 107 | RemoteSubmix = 25, |
| 108 | Telephony = 18, |
| 109 | TvTuner = 17, |
| 110 | UsbAccessory = 12, |
| 111 | UsbDevice = 11, |
| 112 | UsbHeadset = 22, |
| 113 | WiredHeadphones = 4, |
| 114 | WiredHeadset = 3, |
| 115 | Unsupported = -1, |
| 116 | } |
| 117 | |
| 118 | /// Converts DeviceDirection to Android AudioManager device flags. |
| 119 | pub(super) fn android_device_flags(direction: DeviceDirection) -> i32 { |
| 120 | match direction { |
| 121 | DeviceDirection::Input => AudioManager::GET_DEVICES_INPUTS, |
| 122 | DeviceDirection::Output => AudioManager::GET_DEVICES_OUTPUTS, |
| 123 | _ => AudioManager::GET_DEVICES_ALL, |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | impl SampleFormat { |
| 128 | pub(crate) const ENCODING_PCM_16BIT: i32 = 2; |
| 129 | //pub(crate) const ENCODING_PCM_8BIT: i32 = 3; |
| 130 | pub(crate) const ENCODING_PCM_FLOAT: i32 = 4; |
| 131 | |
| 132 | pub(crate) fn from_encoding(encoding: i32) -> Option<SampleFormat> { |
| 133 | match encoding { |
| 134 | SampleFormat::ENCODING_PCM_16BIT => Some(SampleFormat::I16), |
| 135 | SampleFormat::ENCODING_PCM_FLOAT => Some(SampleFormat::F32), |
| 136 | _ => None, |
| 137 | } |
| 138 | } |
| 139 | } |