| 1 |
use crate::dispatch::*; |
| 2 |
use coreaudio_sys::*; |
| 3 |
use std::fmt; |
| 4 |
use std::os::raw::c_void; |
| 5 |
use std::ptr; |
| 6 |
|
| 7 |
pub fn audio_object_has_property(id: AudioObjectID, address: &AudioObjectPropertyAddress) -> bool { |
| 8 |
unsafe { AudioObjectHasProperty(id, address) != 0 } |
| 9 |
} |
| 10 |
|
| 11 |
pub fn audio_object_get_property_data<T>( |
| 12 |
id: AudioObjectID, |
| 13 |
address: &AudioObjectPropertyAddress, |
| 14 |
size: *mut usize, |
| 15 |
data: *mut T, |
| 16 |
) -> OSStatus { |
| 17 |
unsafe { |
| 18 |
AudioObjectGetPropertyData( |
| 19 |
id, |
| 20 |
address, |
| 21 |
0, |
| 22 |
ptr::null(), |
| 23 |
size as *mut UInt32, |
| 24 |
data as *mut c_void, |
| 25 |
) |
| 26 |
} |
| 27 |
} |
| 28 |
|
| 29 |
pub fn audio_object_get_property_data_with_qualifier<T, Q>( |
| 30 |
id: AudioObjectID, |
| 31 |
address: &AudioObjectPropertyAddress, |
| 32 |
qualifier_size: usize, |
| 33 |
qualifier_data: *const Q, |
| 34 |
size: *mut usize, |
| 35 |
data: *mut T, |
| 36 |
) -> OSStatus { |
| 37 |
unsafe { |
| 38 |
AudioObjectGetPropertyData( |
| 39 |
id, |
| 40 |
address, |
| 41 |
qualifier_size as UInt32, |
| 42 |
qualifier_data as *const c_void, |
| 43 |
size as *mut UInt32, |
| 44 |
data as *mut c_void, |
| 45 |
) |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
pub fn audio_object_get_property_data_size( |
| 50 |
id: AudioObjectID, |
| 51 |
address: &AudioObjectPropertyAddress, |
| 52 |
size: *mut usize, |
| 53 |
) -> OSStatus { |
| 54 |
unsafe { AudioObjectGetPropertyDataSize(id, address, 0, ptr::null(), size as *mut UInt32) } |
| 55 |
} |
| 56 |
|
| 57 |
pub fn audio_object_get_property_data_size_with_qualifier<Q>( |
| 58 |
id: AudioObjectID, |
| 59 |
address: &AudioObjectPropertyAddress, |
| 60 |
qualifier_size: usize, |
| 61 |
qualifier_data: *const Q, |
| 62 |
size: *mut usize, |
| 63 |
) -> OSStatus { |
| 64 |
unsafe { |
| 65 |
AudioObjectGetPropertyDataSize( |
| 66 |
id, |
| 67 |
address, |
| 68 |
qualifier_size as UInt32, |
| 69 |
qualifier_data as *const c_void, |
| 70 |
size as *mut UInt32, |
| 71 |
) |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
pub fn audio_object_set_property_data<T>( |
| 76 |
id: AudioObjectID, |
| 77 |
address: &AudioObjectPropertyAddress, |
| 78 |
size: usize, |
| 79 |
data: *const T, |
| 80 |
) -> OSStatus { |
| 81 |
debug_assert_running_serially(); |
| 82 |
unsafe { |
| 83 |
AudioObjectSetPropertyData( |
| 84 |
id, |
| 85 |
address, |
| 86 |
0, |
| 87 |
ptr::null(), |
| 88 |
size as UInt32, |
| 89 |
data as *const c_void, |
| 90 |
) |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
#[allow(non_camel_case_types)] |
| 95 |
pub type audio_object_property_listener_proc = |
| 96 |
extern "C" fn(AudioObjectID, u32, *const AudioObjectPropertyAddress, *mut c_void) -> OSStatus; |
| 97 |
|
| 98 |
pub fn audio_object_add_property_listener<T>( |
| 99 |
id: AudioObjectID, |
| 100 |
address: &AudioObjectPropertyAddress, |
| 101 |
listener: audio_object_property_listener_proc, |
| 102 |
data: *mut T, |
| 103 |
) -> OSStatus { |
| 104 |
debug_assert_running_serially(); |
| 105 |
unsafe { AudioObjectAddPropertyListener(id, address, Some(listener), data as *mut c_void) } |
| 106 |
} |
| 107 |
|
| 108 |
pub fn audio_object_remove_property_listener<T>( |
| 109 |
id: AudioObjectID, |
| 110 |
address: &AudioObjectPropertyAddress, |
| 111 |
listener: audio_object_property_listener_proc, |
| 112 |
data: *mut T, |
| 113 |
) -> OSStatus { |
| 114 |
debug_assert_running_serially(); |
| 115 |
unsafe { AudioObjectRemovePropertyListener(id, address, Some(listener), data as *mut c_void) } |
| 116 |
} |
| 117 |
|
| 118 |
#[derive(Debug, PartialEq)] |
| 119 |
pub enum PropertySelector { |
| 120 |
DefaultOutputDevice, |
| 121 |
DefaultInputDevice, |
| 122 |
DeviceIsAlive, |
| 123 |
DataSource, |
| 124 |
Unknown, |
| 125 |
} |
| 126 |
|
| 127 |
impl From<AudioObjectPropertySelector> for PropertySelector { |
| 128 |
fn from(p: AudioObjectPropertySelector) -> Self { |
| 129 |
use coreaudio_sys as sys; |
| 130 |
match p { |
| 131 |
sys::kAudioHardwarePropertyDefaultOutputDevice => Self::DefaultOutputDevice, |
| 132 |
sys::kAudioHardwarePropertyDefaultInputDevice => Self::DefaultInputDevice, |
| 133 |
sys::kAudioDevicePropertyDeviceIsAlive => Self::DeviceIsAlive, |
| 134 |
sys::kAudioDevicePropertyDataSource => Self::DataSource, |
| 135 |
_ => Self::Unknown, |
| 136 |
} |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
impl fmt::Display for PropertySelector { |
| 141 |
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 142 |
let s = match self { |
| 143 |
Self::DefaultOutputDevice => "kAudioHardwarePropertyDefaultOutputDevice", |
| 144 |
Self::DefaultInputDevice => "kAudioHardwarePropertyDefaultInputDevice", |
| 145 |
Self::DeviceIsAlive => "kAudioDevicePropertyDeviceIsAlive", |
| 146 |
Self::DataSource => "kAudioDevicePropertyDataSource", |
| 147 |
_ => "Unknown", |
| 148 |
}; |
| 149 |
write!(f, "{}", s) |
| 150 |
} |
| 151 |
} |