summaryrefslogtreecommitdiffstats
path: root/third_party/rust/coreaudio-sys-utils/src/audio_object.rs
blob: 368d6caadc93fa28ac08d868e2c50745f2da0d75 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
use coreaudio_sys::*;
use std::fmt;
use std::os::raw::c_void;
use std::ptr;

pub fn audio_object_has_property(id: AudioObjectID, address: &AudioObjectPropertyAddress) -> bool {
    unsafe { AudioObjectHasProperty(id, address) != 0 }
}

pub fn audio_object_get_property_data<T>(
    id: AudioObjectID,
    address: &AudioObjectPropertyAddress,
    size: *mut usize,
    data: *mut T,
) -> OSStatus {
    unsafe {
        AudioObjectGetPropertyData(
            id,
            address,
            0,
            ptr::null(),
            size as *mut UInt32,
            data as *mut c_void,
        )
    }
}

pub fn audio_object_get_property_data_with_qualifier<T, Q>(
    id: AudioObjectID,
    address: &AudioObjectPropertyAddress,
    qualifier_size: usize,
    qualifier_data: *const Q,
    size: *mut usize,
    data: *mut T,
) -> OSStatus {
    unsafe {
        AudioObjectGetPropertyData(
            id,
            address,
            qualifier_size as UInt32,
            qualifier_data as *const c_void,
            size as *mut UInt32,
            data as *mut c_void,
        )
    }
}

pub fn audio_object_get_property_data_size(
    id: AudioObjectID,
    address: &AudioObjectPropertyAddress,
    size: *mut usize,
) -> OSStatus {
    unsafe { AudioObjectGetPropertyDataSize(id, address, 0, ptr::null(), size as *mut UInt32) }
}

pub fn audio_object_get_property_data_size_with_qualifier<Q>(
    id: AudioObjectID,
    address: &AudioObjectPropertyAddress,
    qualifier_size: usize,
    qualifier_data: *const Q,
    size: *mut usize,
) -> OSStatus {
    unsafe {
        AudioObjectGetPropertyDataSize(
            id,
            address,
            qualifier_size as UInt32,
            qualifier_data as *const c_void,
            size as *mut UInt32,
        )
    }
}

pub fn audio_object_set_property_data<T>(
    id: AudioObjectID,
    address: &AudioObjectPropertyAddress,
    size: usize,
    data: *const T,
) -> OSStatus {
    unsafe {
        AudioObjectSetPropertyData(
            id,
            address,
            0,
            ptr::null(),
            size as UInt32,
            data as *const c_void,
        )
    }
}

#[allow(non_camel_case_types)]
pub type audio_object_property_listener_proc =
    extern "C" fn(AudioObjectID, u32, *const AudioObjectPropertyAddress, *mut c_void) -> OSStatus;

pub fn audio_object_add_property_listener<T>(
    id: AudioObjectID,
    address: &AudioObjectPropertyAddress,
    listener: audio_object_property_listener_proc,
    data: *mut T,
) -> OSStatus {
    unsafe { AudioObjectAddPropertyListener(id, address, Some(listener), data as *mut c_void) }
}

pub fn audio_object_remove_property_listener<T>(
    id: AudioObjectID,
    address: &AudioObjectPropertyAddress,
    listener: audio_object_property_listener_proc,
    data: *mut T,
) -> OSStatus {
    unsafe { AudioObjectRemovePropertyListener(id, address, Some(listener), data as *mut c_void) }
}

#[derive(Debug, PartialEq)]
pub enum PropertySelector {
    DefaultOutputDevice,
    DefaultInputDevice,
    DeviceIsAlive,
    DataSource,
    Unknown,
}

impl From<AudioObjectPropertySelector> for PropertySelector {
    fn from(p: AudioObjectPropertySelector) -> Self {
        use coreaudio_sys as sys;
        match p {
            sys::kAudioHardwarePropertyDefaultOutputDevice => Self::DefaultOutputDevice,
            sys::kAudioHardwarePropertyDefaultInputDevice => Self::DefaultInputDevice,
            sys::kAudioDevicePropertyDeviceIsAlive => Self::DeviceIsAlive,
            sys::kAudioDevicePropertyDataSource => Self::DataSource,
            _ => Self::Unknown,
        }
    }
}

impl fmt::Display for PropertySelector {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let s = match self {
            Self::DefaultOutputDevice => "kAudioHardwarePropertyDefaultOutputDevice",
            Self::DefaultInputDevice => "kAudioHardwarePropertyDefaultInputDevice",
            Self::DeviceIsAlive => "kAudioDevicePropertyDeviceIsAlive",
            Self::DataSource => "kAudioDevicePropertyDataSource",
            _ => "Unknown",
        };
        write!(f, "{}", s)
    }
}