]> code.delx.au - pulseaudio/blob - src/modules/macosx/module-coreaudio-detect.c
osx: re-order module locations
[pulseaudio] / src / modules / macosx / module-coreaudio-detect.c
1 /***
2 This file is part of PulseAudio.
3
4 Copyright 2009,2010 Daniel Mack <daniel@caiaq.de>
5
6 PulseAudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published
8 by the Free Software Foundation; either version 2.1 of the License,
9 or (at your option) any later version.
10
11 PulseAudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with PulseAudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19 USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <pulse/xmalloc.h>
27
28 #include <pulsecore/module.h>
29 #include <pulsecore/core-util.h>
30 #include <pulsecore/modargs.h>
31 #include <pulsecore/log.h>
32 #include <pulsecore/llist.h>
33
34 #include <CoreAudio/CoreAudio.h>
35
36 #include "module-coreaudio-detect-symdef.h"
37
38 #define DEVICE_MODULE_NAME "module-coreaudio-device"
39
40 PA_MODULE_AUTHOR("Daniel Mack");
41 PA_MODULE_DESCRIPTION("CoreAudio device detection");
42 PA_MODULE_VERSION(PACKAGE_VERSION);
43 PA_MODULE_LOAD_ONCE(TRUE);
44 PA_MODULE_USAGE("");
45
46 typedef struct ca_device ca_device;
47
48 struct ca_device {
49 AudioObjectID id;
50 unsigned int module_index;
51 PA_LLIST_FIELDS(ca_device);
52 };
53
54 struct userdata {
55 int detect_fds[2];
56 pa_io_event *detect_io;
57
58 PA_LLIST_HEAD(ca_device, devices);
59 };
60
61 static int ca_device_added(struct pa_module *m, AudioObjectID id) {
62 AudioObjectPropertyAddress property_address;
63 OSStatus err;
64 pa_module *mod;
65 struct userdata *u;
66 struct ca_device *dev;
67 char *args, tmp[64];
68 UInt32 size;
69
70 pa_assert(m);
71 pa_assert_se(u = m->userdata);
72
73 /* To prevent generating a black hole that will suck us in,
74 don't create sources/sinks for PulseAudio virtual devices */
75
76 property_address.mSelector = kAudioDevicePropertyDeviceManufacturer;
77 property_address.mScope = kAudioObjectPropertyScopeGlobal;
78 property_address.mElement = kAudioObjectPropertyElementMaster;
79
80 size = sizeof(tmp);
81 err = AudioObjectGetPropertyData(id, &property_address, 0, NULL, &size, tmp);
82
83 if (!err && strcmp(tmp, "pulseaudio.org") == 0)
84 return 0;
85
86 args = pa_sprintf_malloc("object_id=%d", (int) id);
87 pa_log_debug("Loading %s with arguments '%s'", DEVICE_MODULE_NAME, args);
88 mod = pa_module_load(m->core, DEVICE_MODULE_NAME, args);
89 pa_xfree(args);
90
91 if (!mod) {
92 pa_log_info("Failed to load module %s with arguments '%s'", DEVICE_MODULE_NAME, args);
93 return -1;
94 }
95
96 dev = pa_xnew0(ca_device, 1);
97 dev->module_index = mod->index;
98 dev->id = id;
99
100 PA_LLIST_INIT(ca_device, dev);
101 PA_LLIST_PREPEND(ca_device, u->devices, dev);
102
103 return 0;
104 }
105
106 static int ca_update_device_list(struct pa_module *m) {
107 AudioObjectPropertyAddress property_address;
108 OSStatus err;
109 UInt32 i, size, num_devices;
110 AudioDeviceID *device_id;
111 struct ca_device *dev;
112 struct userdata *u;
113
114 pa_assert(m);
115 pa_assert_se(u = m->userdata);
116
117 property_address.mSelector = kAudioHardwarePropertyDevices;
118 property_address.mScope = kAudioObjectPropertyScopeGlobal;
119 property_address.mElement = kAudioObjectPropertyElementMaster;
120
121 /* get the number of currently available audio devices */
122 err = AudioObjectGetPropertyDataSize(kAudioObjectSystemObject, &property_address, 0, NULL, &size);
123 if (err) {
124 pa_log("Unable to get data size for kAudioHardwarePropertyDevices.");
125 return -1;
126 }
127
128 num_devices = size / sizeof(AudioDeviceID);
129 device_id = pa_xnew(AudioDeviceID, num_devices);
130
131 err = AudioObjectGetPropertyData(kAudioObjectSystemObject, &property_address, 0, NULL, &size, device_id);
132 if (err) {
133 pa_log("Unable to get kAudioHardwarePropertyDevices.");
134 pa_xfree(device_id);
135 return -1;
136 }
137
138 /* scan for devices which are reported but not in our cached list */
139 for (i = 0; i < num_devices; i++) {
140 bool found = FALSE;
141
142 PA_LLIST_FOREACH(dev, u->devices)
143 if (dev->id == device_id[i]) {
144 found = TRUE;
145 break;
146 }
147
148 if (!found)
149 ca_device_added(m, device_id[i]);
150 }
151
152 /* scan for devices which are in our cached list but are not reported */
153 scan_removed:
154
155 PA_LLIST_FOREACH(dev, u->devices) {
156 bool found = FALSE;
157
158 for (i = 0; i < num_devices; i++)
159 if (dev->id == device_id[i]) {
160 found = TRUE;
161 break;
162 }
163
164 if (!found) {
165 pa_log_debug("object id %d has been removed (module index %d) %p", (unsigned int) dev->id, dev->module_index, dev);
166 pa_module_unload_request_by_index(m->core, dev->module_index, TRUE);
167 PA_LLIST_REMOVE(ca_device, u->devices, dev);
168 pa_xfree(dev);
169 /* the current list item pointer is not valid anymore, so start over. */
170 goto scan_removed;
171 }
172 }
173
174 pa_xfree(device_id);
175 return 0;
176 }
177
178 static OSStatus property_listener_proc(AudioObjectID objectID, UInt32 numberAddresses,
179 const AudioObjectPropertyAddress inAddresses[],
180 void *clientData)
181 {
182 struct userdata *u = clientData;
183 char dummy = 1;
184
185 pa_assert(u);
186
187 /* dispatch module load/unload operations in main thread */
188 write(u->detect_fds[1], &dummy, 1);
189
190 return 0;
191 }
192
193 static void detect_handle(pa_mainloop_api *a, pa_io_event *e, int fd, pa_io_event_flags_t events, void *userdata) {
194 pa_module *m = userdata;
195 char dummy;
196
197 pa_assert(m);
198
199 read(fd, &dummy, 1);
200 ca_update_device_list(m);
201 }
202
203 int pa__init(pa_module *m) {
204 struct userdata *u = pa_xnew0(struct userdata, 1);
205 AudioObjectPropertyAddress property_address;
206
207 pa_assert(m);
208
209 m->userdata = u;
210
211 property_address.mSelector = kAudioHardwarePropertyDevices;
212 property_address.mScope = kAudioObjectPropertyScopeGlobal;
213 property_address.mElement = kAudioObjectPropertyElementMaster;
214
215 if (AudioObjectAddPropertyListener(kAudioObjectSystemObject, &property_address, property_listener_proc, u)) {
216 pa_log("AudioObjectAddPropertyListener() failed.");
217 goto fail;
218 }
219
220 if (ca_update_device_list(m))
221 goto fail;
222
223 pa_assert_se(pipe(u->detect_fds) == 0);
224 pa_assert_se(u->detect_io = m->core->mainloop->io_new(m->core->mainloop, u->detect_fds[0], PA_IO_EVENT_INPUT, detect_handle, m));
225
226 return 0;
227
228 fail:
229 pa_xfree(u);
230 return -1;
231 }
232
233 void pa__done(pa_module *m) {
234 struct userdata *u;
235 struct ca_device *dev;
236 AudioObjectPropertyAddress property_address;
237
238 pa_assert(m);
239 pa_assert_se(u = m->userdata);
240
241 dev = u->devices;
242
243 property_address.mSelector = kAudioHardwarePropertyDevices;
244 property_address.mScope = kAudioObjectPropertyScopeGlobal;
245 property_address.mElement = kAudioObjectPropertyElementMaster;
246
247 AudioObjectRemovePropertyListener(kAudioObjectSystemObject, &property_address, property_listener_proc, u);
248
249 while (dev) {
250 struct ca_device *next = dev->next;
251
252 pa_module_unload_request_by_index(m->core, dev->module_index, TRUE);
253 pa_xfree(dev);
254
255 dev = next;
256 }
257
258 if (u->detect_fds[0] >= 0)
259 close(u->detect_fds[0]);
260
261 if (u->detect_fds[1] >= 0)
262 close(u->detect_fds[1]);
263
264 if (u->detect_io)
265 m->core->mainloop->io_free(u->detect_io);
266
267 pa_xfree(u);
268 }