]> code.delx.au - pulseaudio/blob - src/modules/macosx/module-coreaudio-device.c
Merge remote-tracking branch 'zonique/osx'
[pulseaudio] / src / modules / macosx / module-coreaudio-device.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 /* TODO:
23 - implement hardware volume controls
24 - handle audio device stream format changes (will require changes to the core)
25 */
26
27 #ifdef HAVE_CONFIG_H
28 #include <config.h>
29 #endif
30
31 #include <pulse/xmalloc.h>
32 #include <pulse/util.h>
33
34 #include <pulsecore/core-error.h>
35 #include <pulsecore/sink.h>
36 #include <pulsecore/source.h>
37 #include <pulsecore/module.h>
38 #include <pulsecore/sample-util.h>
39 #include <pulsecore/core-util.h>
40 #include <pulsecore/modargs.h>
41 #include <pulsecore/log.h>
42 #include <pulsecore/macro.h>
43 #include <pulsecore/llist.h>
44 #include <pulsecore/card.h>
45 #include <pulsecore/strbuf.h>
46 #include <pulsecore/thread.h>
47 #include <pulsecore/thread-mq.h>
48
49 #include <CoreAudio/CoreAudio.h>
50 #include <CoreAudio/CoreAudioTypes.h>
51 #include <CoreAudio/AudioHardware.h>
52
53 #include "module-coreaudio-device-symdef.h"
54
55 #define DEFAULT_FRAMES_PER_IOPROC 512
56
57 PA_MODULE_AUTHOR("Daniel Mack");
58 PA_MODULE_DESCRIPTION("CoreAudio device");
59 PA_MODULE_VERSION(PACKAGE_VERSION);
60 PA_MODULE_LOAD_ONCE(FALSE);
61 PA_MODULE_USAGE("object_id=<the CoreAudio device id> "
62 "ioproc_frames=<audio frames per IOProc call> ");
63
64 static const char* const valid_modargs[] = {
65 "object_id",
66 "ioproc_frames",
67 NULL
68 };
69
70 enum {
71 CA_MESSAGE_RENDER = PA_SINK_MESSAGE_MAX,
72 };
73
74 typedef struct coreaudio_sink coreaudio_sink;
75 typedef struct coreaudio_source coreaudio_source;
76
77 struct userdata {
78 AudioObjectID object_id;
79 AudioDeviceIOProcID proc_id;
80
81 pa_thread_mq thread_mq;
82 pa_asyncmsgq *async_msgq;
83
84 pa_rtpoll *rtpoll;
85 pa_thread *thread;
86
87 pa_module *module;
88 pa_card *card;
89 pa_bool_t running;
90
91 char *device_name, *vendor_name;
92
93 const AudioBufferList *render_input_data;
94 AudioBufferList *render_output_data;
95
96 AudioStreamBasicDescription stream_description;
97
98 PA_LLIST_HEAD(coreaudio_sink, sinks);
99 PA_LLIST_HEAD(coreaudio_source, sources);
100 };
101
102 struct coreaudio_sink {
103 pa_sink *pa_sink;
104 struct userdata *userdata;
105
106 char *name;
107 unsigned int channel_idx;
108 pa_bool_t active;
109
110 pa_channel_map map;
111 pa_sample_spec ss;
112
113 PA_LLIST_FIELDS(coreaudio_sink);
114 };
115
116 struct coreaudio_source {
117 pa_source *pa_source;
118 struct userdata *userdata;
119
120 char *name;
121 unsigned int channel_idx;
122 pa_bool_t active;
123
124 pa_channel_map map;
125 pa_sample_spec ss;
126
127 PA_LLIST_FIELDS(coreaudio_source);
128 };
129
130 static OSStatus io_render_proc (AudioDeviceID device,
131 const AudioTimeStamp *now,
132 const AudioBufferList *inputData,
133 const AudioTimeStamp *inputTime,
134 AudioBufferList *outputData,
135 const AudioTimeStamp *outputTime,
136 void *clientData)
137 {
138 struct userdata *u = clientData;
139
140 pa_assert(u);
141 pa_assert(device == u->object_id);
142
143 u->render_input_data = inputData;
144 u->render_output_data = outputData;
145
146 if (u->sinks)
147 pa_assert_se(pa_asyncmsgq_send(u->async_msgq, PA_MSGOBJECT(u->sinks->pa_sink),
148 CA_MESSAGE_RENDER, NULL, 0, NULL) == 0);
149
150 if (u->sources)
151 pa_assert_se(pa_asyncmsgq_send(u->async_msgq, PA_MSGOBJECT(u->sources->pa_source),
152 CA_MESSAGE_RENDER, NULL, 0, NULL) == 0);
153
154 return 0;
155 }
156
157 static OSStatus ca_stream_format_changed(AudioObjectID objectID,
158 UInt32 numberAddresses,
159 const AudioObjectPropertyAddress addresses[],
160 void *clientData)
161 {
162 struct userdata *u = clientData;
163 UInt32 i;
164
165 pa_assert(u);
166
167 /* REVISIT: PA can't currently handle external format change requests.
168 * Hence, we set the original format back in this callback to avoid horrible audio artefacts.
169 * The device settings will appear to be 'locked' for any application as long as the PA daemon is running.
170 * Once we're able to propagate such events up in the core, this needs to be changed. */
171
172 for (i = 0; i < numberAddresses; i++)
173 AudioObjectSetPropertyData(objectID, addresses + i, 0, NULL, sizeof(u->stream_description), &u->stream_description);
174
175 return 0;
176 }
177
178 static pa_usec_t get_latency_us(pa_object *o) {
179 struct userdata *u;
180 pa_sample_spec *ss;
181 bool is_source;
182 UInt32 v, total = 0;
183 UInt32 err, size = sizeof(v);
184 AudioObjectPropertyAddress property_address;
185 AudioObjectID stream_id;
186
187 if (pa_sink_isinstance(o)) {
188 coreaudio_sink *sink = PA_SINK(o)->userdata;
189
190 u = sink->userdata;
191 ss = &sink->ss;
192 is_source = FALSE;
193 } else if (pa_source_isinstance(o)) {
194 coreaudio_source *source = PA_SOURCE(o)->userdata;
195
196 u = source->userdata;
197 ss = &source->ss;
198 is_source = TRUE;
199 } else
200 pa_assert_not_reached();
201
202 pa_assert(u);
203
204 property_address.mScope = kAudioObjectPropertyScopeGlobal;
205 property_address.mElement = kAudioObjectPropertyElementMaster;
206
207 /* get the device latency */
208 property_address.mSelector = kAudioDevicePropertyLatency;
209 size = sizeof(total);
210 AudioObjectGetPropertyData(u->object_id, &property_address, 0, NULL, &size, &total);
211 total += v;
212
213 /* the the IOProc buffer size */
214 property_address.mSelector = kAudioDevicePropertyBufferFrameSize;
215 size = sizeof(v);
216 AudioObjectGetPropertyData(u->object_id, &property_address, 0, NULL, &size, &v);
217 total += v;
218
219 /* IOProc safety offset - this value is the same for both directions, hence we divide it by 2 */
220 property_address.mSelector = kAudioDevicePropertySafetyOffset;
221 size = sizeof(v);
222 AudioObjectGetPropertyData(u->object_id, &property_address, 0, NULL, &size, &v);
223 total += v / 2;
224
225 /* get the stream latency.
226 * FIXME: this assumes the stream latency is the same for all streams */
227 property_address.mSelector = kAudioDevicePropertyStreams;
228 size = sizeof(stream_id);
229 err = AudioObjectGetPropertyData(u->object_id, &property_address, 0, NULL, &size, &stream_id);
230 if (!err) {
231 property_address.mSelector = kAudioStreamPropertyLatency;
232 size = sizeof(v);
233 err = AudioObjectGetPropertyData(stream_id, &property_address, 0, NULL, &size, &v);
234 if (!err)
235 total += v;
236 }
237
238 return pa_bytes_to_usec(total * pa_frame_size(ss), ss);
239 }
240
241 static void ca_device_check_device_state(struct userdata *u) {
242 coreaudio_sink *ca_sink;
243 coreaudio_source *ca_source;
244 pa_bool_t active = FALSE;
245
246 pa_assert(u);
247
248 for (ca_sink = u->sinks; ca_sink; ca_sink = ca_sink->next)
249 if (ca_sink->active)
250 active = TRUE;
251
252 for (ca_source = u->sources; ca_source; ca_source = ca_source->next)
253 if (ca_source->active)
254 active = TRUE;
255
256 if (active && !u->running)
257 AudioDeviceStart(u->object_id, u->proc_id);
258 else if (!active && u->running)
259 AudioDeviceStop(u->object_id, u->proc_id);
260
261 u->running = active;
262 }
263
264 static int sink_process_msg(pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *chunk) {
265 coreaudio_sink *sink = PA_SINK(o)->userdata;
266 struct userdata *u = sink->userdata;
267 unsigned int i;
268 pa_memchunk audio_chunk;
269
270 switch (code) {
271 case CA_MESSAGE_RENDER: {
272 /* audio out */
273 for (i = 0; i < u->render_output_data->mNumberBuffers; i++) {
274 AudioBuffer *buf = u->render_output_data->mBuffers + i;
275
276 pa_assert(sink);
277
278 if (PA_SINK_IS_OPENED(sink->pa_sink->thread_info.state)) {
279 if (sink->pa_sink->thread_info.rewind_requested)
280 pa_sink_process_rewind(sink->pa_sink, 0);
281
282 audio_chunk.memblock = pa_memblock_new_fixed(u->module->core->mempool, buf->mData, buf->mDataByteSize, FALSE);
283 audio_chunk.length = buf->mDataByteSize;
284 audio_chunk.index = 0;
285
286 pa_sink_render_into_full(sink->pa_sink, &audio_chunk);
287 pa_memblock_unref_fixed(audio_chunk.memblock);
288 }
289
290 sink = sink->next;
291 }
292
293 return 0;
294 }
295
296 case PA_SINK_MESSAGE_GET_LATENCY: {
297 *((pa_usec_t *) data) = get_latency_us(PA_OBJECT(o));
298 return 0;
299 }
300
301 case PA_SINK_MESSAGE_SET_STATE:
302 switch ((pa_sink_state_t) PA_PTR_TO_UINT(data)) {
303 case PA_SINK_SUSPENDED:
304 case PA_SINK_IDLE:
305 sink->active = FALSE;
306 break;
307
308 case PA_SINK_RUNNING:
309 sink->active = TRUE;
310 break;
311
312 case PA_SINK_UNLINKED:
313 case PA_SINK_INIT:
314 case PA_SINK_INVALID_STATE:
315 ;
316 }
317
318 ca_device_check_device_state(sink->userdata);
319 break;
320 }
321
322 return pa_sink_process_msg(o, code, data, offset, chunk);
323 }
324
325 static int source_process_msg(pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *chunk) {
326 coreaudio_source *source = PA_SOURCE(o)->userdata;
327 struct userdata *u = source->userdata;
328 unsigned int i;
329 pa_memchunk audio_chunk;
330
331 switch (code) {
332 case CA_MESSAGE_RENDER: {
333 /* audio in */
334 for (i = 0; i < u->render_input_data->mNumberBuffers; i++) {
335 const AudioBuffer *buf = u->render_input_data->mBuffers + i;
336
337 pa_assert(source);
338
339 if (PA_SOURCE_IS_OPENED(source->pa_source->thread_info.state)) {
340 audio_chunk.memblock = pa_memblock_new_fixed(u->module->core->mempool, buf->mData, buf->mDataByteSize, TRUE);
341 audio_chunk.length = buf->mDataByteSize;
342 audio_chunk.index = 0;
343
344 pa_source_post(source->pa_source, &audio_chunk);
345 pa_memblock_unref_fixed(audio_chunk.memblock);
346 }
347
348 source = source->next;
349 }
350
351 return 0;
352 }
353
354 case PA_SOURCE_MESSAGE_GET_LATENCY: {
355 *((pa_usec_t *) data) = get_latency_us(PA_OBJECT(o));
356 return 0;
357 }
358
359 case PA_SOURCE_MESSAGE_SET_STATE:
360 switch ((pa_source_state_t) PA_PTR_TO_UINT(data)) {
361 case PA_SOURCE_SUSPENDED:
362 case PA_SOURCE_IDLE:
363 source->active = FALSE;
364 break;
365
366 case PA_SOURCE_RUNNING:
367 source->active = TRUE;
368 break;
369
370 case PA_SOURCE_UNLINKED:
371 case PA_SOURCE_INIT:
372 case PA_SOURCE_INVALID_STATE:
373 ;
374 }
375
376 ca_device_check_device_state(source->userdata);
377 break;
378 }
379
380 return pa_source_process_msg(o, code, data, offset, chunk);
381 }
382
383 static int ca_device_create_sink(pa_module *m, AudioBuffer *buf, int channel_idx) {
384 OSStatus err;
385 UInt32 size;
386 struct userdata *u = m->userdata;
387 pa_sink_new_data new_data;
388 pa_sink_flags_t flags = PA_SINK_LATENCY | PA_SINK_HARDWARE;
389 coreaudio_sink *ca_sink;
390 pa_sink *sink;
391 unsigned int i;
392 char tmp[255];
393 pa_strbuf *strbuf;
394 AudioObjectPropertyAddress property_address;
395
396 ca_sink = pa_xnew0(coreaudio_sink, 1);
397 ca_sink->map.channels = buf->mNumberChannels;
398 ca_sink->ss.channels = buf->mNumberChannels;
399 ca_sink->channel_idx = channel_idx;
400
401 /* build a name for this stream */
402 strbuf = pa_strbuf_new();
403
404 for (i = 0; i < buf->mNumberChannels; i++) {
405 property_address.mSelector = kAudioObjectPropertyElementName;
406 property_address.mScope = kAudioDevicePropertyScopeOutput;
407 property_address.mElement = channel_idx + i + 1;
408 size = sizeof(tmp);
409 err = AudioObjectGetPropertyData(u->object_id, &property_address, 0, NULL, &size, tmp);
410 if (err || !strlen(tmp))
411 snprintf(tmp, sizeof(tmp), "Channel %d", property_address.mElement);
412
413 if (i > 0)
414 pa_strbuf_puts(strbuf, ", ");
415
416 pa_strbuf_puts(strbuf, tmp);
417 }
418
419 ca_sink->name = pa_strbuf_tostring_free(strbuf);
420
421 pa_log_debug("Stream name is >%s<", ca_sink->name);
422
423 /* default to mono streams */
424 for (i = 0; i < ca_sink->map.channels; i++)
425 ca_sink->map.map[i] = PA_CHANNEL_POSITION_MONO;
426
427 if (buf->mNumberChannels == 2) {
428 ca_sink->map.map[0] = PA_CHANNEL_POSITION_LEFT;
429 ca_sink->map.map[1] = PA_CHANNEL_POSITION_RIGHT;
430 }
431
432 ca_sink->ss.rate = u->stream_description.mSampleRate;
433 ca_sink->ss.format = PA_SAMPLE_FLOAT32LE;
434
435 pa_sink_new_data_init(&new_data);
436 new_data.card = u->card;
437 new_data.driver = __FILE__;
438 new_data.module = u->module;
439 new_data.namereg_fail = FALSE;
440 pa_sink_new_data_set_name(&new_data, ca_sink->name);
441 pa_sink_new_data_set_channel_map(&new_data, &ca_sink->map);
442 pa_sink_new_data_set_sample_spec(&new_data, &ca_sink->ss);
443 pa_proplist_sets(new_data.proplist, PA_PROP_DEVICE_STRING, u->device_name);
444 pa_proplist_sets(new_data.proplist, PA_PROP_DEVICE_PRODUCT_NAME, u->device_name);
445 pa_proplist_sets(new_data.proplist, PA_PROP_DEVICE_DESCRIPTION, u->device_name);
446 pa_proplist_sets(new_data.proplist, PA_PROP_DEVICE_ACCESS_MODE, "mmap");
447 pa_proplist_sets(new_data.proplist, PA_PROP_DEVICE_CLASS, "sound");
448 pa_proplist_sets(new_data.proplist, PA_PROP_DEVICE_API, "CoreAudio");
449 pa_proplist_setf(new_data.proplist, PA_PROP_DEVICE_BUFFERING_BUFFER_SIZE, "%lu", (unsigned long) buf->mDataByteSize);
450
451 if (u->vendor_name)
452 pa_proplist_sets(new_data.proplist, PA_PROP_DEVICE_VENDOR_NAME, u->vendor_name);
453
454 sink = pa_sink_new(m->core, &new_data, flags);
455 pa_sink_new_data_done(&new_data);
456
457 if (!sink) {
458 pa_log("unable to create sink.");
459 return -1;
460 }
461
462 sink->parent.process_msg = sink_process_msg;
463 sink->userdata = ca_sink;
464
465 pa_sink_set_asyncmsgq(sink, u->thread_mq.inq);
466 pa_sink_set_rtpoll(sink, u->rtpoll);
467
468 ca_sink->pa_sink = sink;
469 ca_sink->userdata = u;
470
471 PA_LLIST_PREPEND(coreaudio_sink, u->sinks, ca_sink);
472
473 return 0;
474 }
475
476 static int ca_device_create_source(pa_module *m, AudioBuffer *buf, int channel_idx) {
477 OSStatus err;
478 UInt32 size;
479 struct userdata *u = m->userdata;
480 pa_source_new_data new_data;
481 pa_source_flags_t flags = PA_SOURCE_LATENCY | PA_SOURCE_HARDWARE;
482 coreaudio_source *ca_source;
483 pa_source *source;
484 unsigned int i;
485 char tmp[255];
486 pa_strbuf *strbuf;
487 AudioObjectPropertyAddress property_address;
488
489 ca_source = pa_xnew0(coreaudio_source, 1);
490 ca_source->map.channels = buf->mNumberChannels;
491 ca_source->ss.channels = buf->mNumberChannels;
492 ca_source->channel_idx = channel_idx;
493
494 /* build a name for this stream */
495 strbuf = pa_strbuf_new();
496
497 for (i = 0; i < buf->mNumberChannels; i++) {
498 property_address.mSelector = kAudioObjectPropertyElementName;
499 property_address.mScope = kAudioDevicePropertyScopeInput;
500 property_address.mElement = channel_idx + i + 1;
501 size = sizeof(tmp);
502 err = AudioObjectGetPropertyData(u->object_id, &property_address, 0, NULL, &size, tmp);
503 if (err || !strlen(tmp))
504 snprintf(tmp, sizeof(tmp), "Channel %d", property_address.mElement);
505
506 if (i > 0)
507 pa_strbuf_puts(strbuf, ", ");
508
509 pa_strbuf_puts(strbuf, tmp);
510 }
511
512 ca_source->name = pa_strbuf_tostring_free(strbuf);
513
514 pa_log_debug("Stream name is >%s<", ca_source->name);
515
516 /* default to mono streams */
517 for (i = 0; i < ca_source->map.channels; i++)
518 ca_source->map.map[i] = PA_CHANNEL_POSITION_MONO;
519
520 if (buf->mNumberChannels == 2) {
521 ca_source->map.map[0] = PA_CHANNEL_POSITION_LEFT;
522 ca_source->map.map[1] = PA_CHANNEL_POSITION_RIGHT;
523 }
524
525 ca_source->ss.rate = u->stream_description.mSampleRate;
526 ca_source->ss.format = PA_SAMPLE_FLOAT32LE;
527
528 pa_source_new_data_init(&new_data);
529 new_data.card = u->card;
530 new_data.driver = __FILE__;
531 new_data.module = u->module;
532 new_data.namereg_fail = FALSE;
533 pa_source_new_data_set_name(&new_data, ca_source->name);
534 pa_source_new_data_set_channel_map(&new_data, &ca_source->map);
535 pa_source_new_data_set_sample_spec(&new_data, &ca_source->ss);
536 pa_proplist_sets(new_data.proplist, PA_PROP_DEVICE_STRING, u->device_name);
537 pa_proplist_sets(new_data.proplist, PA_PROP_DEVICE_PRODUCT_NAME, u->device_name);
538 pa_proplist_sets(new_data.proplist, PA_PROP_DEVICE_DESCRIPTION, u->device_name);
539 pa_proplist_sets(new_data.proplist, PA_PROP_DEVICE_ACCESS_MODE, "mmap");
540 pa_proplist_sets(new_data.proplist, PA_PROP_DEVICE_CLASS, "sound");
541 pa_proplist_sets(new_data.proplist, PA_PROP_DEVICE_API, "CoreAudio");
542 pa_proplist_setf(new_data.proplist, PA_PROP_DEVICE_BUFFERING_BUFFER_SIZE, "%lu", (unsigned long) buf->mDataByteSize);
543
544 if (u->vendor_name)
545 pa_proplist_sets(new_data.proplist, PA_PROP_DEVICE_VENDOR_NAME, u->vendor_name);
546
547 source = pa_source_new(m->core, &new_data, flags);
548 pa_source_new_data_done(&new_data);
549
550 if (!source) {
551 pa_log("unable to create source.");
552 return -1;
553 }
554
555 source->parent.process_msg = source_process_msg;
556 source->userdata = ca_source;
557
558 pa_source_set_asyncmsgq(source, u->thread_mq.inq);
559 pa_source_set_rtpoll(source, u->rtpoll);
560
561 ca_source->pa_source = source;
562 ca_source->userdata = u;
563
564 PA_LLIST_PREPEND(coreaudio_source, u->sources, ca_source);
565
566 return 0;
567 }
568
569 static int ca_device_create_streams(pa_module *m, bool direction_in) {
570 OSStatus err;
571 UInt32 size, i, channel_idx;
572 struct userdata *u = m->userdata;
573 AudioBufferList *buffer_list;
574 AudioObjectPropertyAddress property_address;
575
576 property_address.mScope = direction_in ? kAudioDevicePropertyScopeInput : kAudioDevicePropertyScopeOutput;
577 property_address.mElement = kAudioObjectPropertyElementMaster;
578
579 /* get current stream format */
580 size = sizeof(AudioStreamBasicDescription);
581 property_address.mSelector = kAudioDevicePropertyStreamFormat;
582 err = AudioObjectGetPropertyData(u->object_id, &property_address, 0, NULL, &size, &u->stream_description);
583 if (err) {
584 /* no appropriate streams found - silently bail. */
585 return -1;
586 }
587
588 if (u->stream_description.mFormatID != kAudioFormatLinearPCM) {
589 pa_log("Unsupported audio format '%c%c%c%c'",
590 (char) (u->stream_description.mFormatID >> 24),
591 (char) (u->stream_description.mFormatID >> 16) & 0xff,
592 (char) (u->stream_description.mFormatID >> 8) & 0xff,
593 (char) (u->stream_description.mFormatID & 0xff));
594 return -1;
595 }
596
597 /* get stream configuration */
598 size = 0;
599 property_address.mSelector = kAudioDevicePropertyStreamConfiguration;
600 err = AudioObjectGetPropertyDataSize(u->object_id, &property_address, 0, NULL, &size);
601 if (err) {
602 pa_log("Failed to get kAudioDevicePropertyStreamConfiguration (%s).", direction_in ? "input" : "output");
603 return -1;
604 }
605
606 if (!size)
607 return 0;
608
609 buffer_list = (AudioBufferList *) pa_xmalloc(size);
610 err = AudioObjectGetPropertyData(u->object_id, &property_address, 0, NULL, &size, buffer_list);
611
612 if (!err) {
613 pa_log_debug("Sample rate: %f", u->stream_description.mSampleRate);
614 pa_log_debug("%d bytes per packet", (unsigned int) u->stream_description.mBytesPerPacket);
615 pa_log_debug("%d frames per packet", (unsigned int) u->stream_description.mFramesPerPacket);
616 pa_log_debug("%d bytes per frame", (unsigned int) u->stream_description.mBytesPerFrame);
617 pa_log_debug("%d channels per frame", (unsigned int) u->stream_description.mChannelsPerFrame);
618 pa_log_debug("%d bits per channel", (unsigned int) u->stream_description.mBitsPerChannel);
619
620 for (channel_idx = 0, i = 0; i < buffer_list->mNumberBuffers; i++) {
621 AudioBuffer *buf = buffer_list->mBuffers + i;
622
623 if (direction_in)
624 ca_device_create_source(m, buf, channel_idx);
625 else
626 ca_device_create_sink(m, buf, channel_idx);
627
628 channel_idx += buf->mNumberChannels;
629 }
630 }
631
632 pa_xfree(buffer_list);
633 return 0;
634 }
635
636 static void thread_func(void *userdata) {
637 struct userdata *u = userdata;
638
639 pa_assert(u);
640 pa_assert(u->module);
641 pa_assert(u->module->core);
642
643 pa_log_debug("Thread starting up");
644
645 if (u->module->core->realtime_scheduling)
646 pa_make_realtime(u->module->core->realtime_priority);
647
648 pa_thread_mq_install(&u->thread_mq);
649
650 for (;;) {
651 int ret;
652
653 ret = pa_rtpoll_run(u->rtpoll, TRUE);
654
655 if (ret < 0)
656 goto fail;
657
658 if (ret == 0)
659 goto finish;
660 }
661
662 fail:
663 /* If this was no regular exit from the loop we have to continue
664 * processing messages until we received PA_MESSAGE_SHUTDOWN */
665 pa_asyncmsgq_post(u->thread_mq.outq, PA_MSGOBJECT(u->module->core), PA_CORE_MESSAGE_UNLOAD_MODULE, u->module, 0, NULL, NULL);
666 pa_asyncmsgq_wait_for(u->thread_mq.inq, PA_MESSAGE_SHUTDOWN);
667
668 finish:
669 pa_log_debug("Thread shutting down");
670 }
671
672 int pa__init(pa_module *m) {
673 OSStatus err;
674 UInt32 size, frames;
675 struct userdata *u = NULL;
676 pa_modargs *ma = NULL;
677 char tmp[64];
678 pa_card_new_data card_new_data;
679 coreaudio_sink *ca_sink;
680 coreaudio_source *ca_source;
681 AudioObjectPropertyAddress property_address;
682
683 pa_assert(m);
684
685 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
686 pa_log("Failed to parse module arguments.");
687 goto fail;
688 }
689
690 u = pa_xnew0(struct userdata, 1);
691 u->module = m;
692 m->userdata = u;
693
694 if (pa_modargs_get_value_u32(ma, "object_id", (unsigned int *) &u->object_id) != 0) {
695 pa_log("Failed to parse object_id argument.");
696 goto fail;
697 }
698
699 property_address.mScope = kAudioObjectPropertyScopeGlobal;
700 property_address.mElement = kAudioObjectPropertyElementMaster;
701
702 /* get device product name */
703 property_address.mSelector = kAudioDevicePropertyDeviceName;
704 size = sizeof(tmp);
705 err = AudioObjectGetPropertyData(u->object_id, &property_address, 0, NULL, &size, tmp);
706 if (err) {
707 pa_log("Failed to get kAudioDevicePropertyDeviceName (err = %08x).", (int) err);
708 goto fail;
709 }
710
711 u->device_name = pa_xstrdup(tmp);
712
713 pa_card_new_data_init(&card_new_data);
714 pa_proplist_sets(card_new_data.proplist, PA_PROP_DEVICE_STRING, tmp);
715 card_new_data.driver = __FILE__;
716 pa_card_new_data_set_name(&card_new_data, tmp);
717 pa_log_info("Initializing module for CoreAudio device '%s' (id %d)", tmp, (unsigned int) u->object_id);
718
719 /* get device vendor name (may fail) */
720 property_address.mSelector = kAudioDevicePropertyDeviceManufacturer;
721 size = sizeof(tmp);
722 err = AudioObjectGetPropertyData(u->object_id, &property_address, 0, NULL, &size, tmp);
723 if (!err)
724 u->vendor_name = pa_xstrdup(tmp);
725
726 /* create the card object */
727 u->card = pa_card_new(m->core, &card_new_data);
728 if (!u->card) {
729 pa_log("Unable to create card.\n");
730 goto fail;
731 }
732
733 pa_card_new_data_done(&card_new_data);
734 u->card->userdata = u;
735
736 u->rtpoll = pa_rtpoll_new();
737 pa_thread_mq_init(&u->thread_mq, m->core->mainloop, u->rtpoll);
738 u->async_msgq = pa_asyncmsgq_new(0);
739 pa_rtpoll_item_new_asyncmsgq_read(u->rtpoll, PA_RTPOLL_EARLY-1, u->async_msgq);
740
741 PA_LLIST_HEAD_INIT(coreaudio_sink, u->sinks);
742
743 /* create sinks */
744 ca_device_create_streams(m, FALSE);
745
746 /* create sources */
747 ca_device_create_streams(m, TRUE);
748
749 /* create the message thread */
750 if (!(u->thread = pa_thread_new("coreaudio", thread_func, u))) {
751 pa_log("Failed to create thread.");
752 goto fail;
753 }
754
755 /* register notification callback for stream format changes */
756 property_address.mSelector = kAudioDevicePropertyStreamFormat;
757 property_address.mScope = kAudioObjectPropertyScopeGlobal;
758 property_address.mElement = kAudioObjectPropertyElementMaster;
759
760 AudioObjectAddPropertyListener(u->object_id, &property_address, ca_stream_format_changed, u);
761
762 /* set number of frames in IOProc */
763 frames = DEFAULT_FRAMES_PER_IOPROC;
764 pa_modargs_get_value_u32(ma, "ioproc_frames", (unsigned int *) &frames);
765
766 property_address.mSelector = kAudioDevicePropertyBufferFrameSize;
767 AudioObjectSetPropertyData(u->object_id, &property_address, 0, NULL, sizeof(frames), &frames);
768 pa_log_debug("%u frames per IOProc\n", (unsigned int) frames);
769
770 /* create one ioproc for both directions */
771 err = AudioDeviceCreateIOProcID(u->object_id, io_render_proc, u, &u->proc_id);
772 if (err) {
773 pa_log("AudioDeviceCreateIOProcID() failed (err = %08x\n).", (int) err);
774 goto fail;
775 }
776
777 for (ca_sink = u->sinks; ca_sink; ca_sink = ca_sink->next)
778 pa_sink_put(ca_sink->pa_sink);
779
780 for (ca_source = u->sources; ca_source; ca_source = ca_source->next)
781 pa_source_put(ca_source->pa_source);
782
783 pa_modargs_free(ma);
784
785 return 0;
786
787 fail:
788 if (u)
789 pa__done(m);
790
791 if (ma)
792 pa_modargs_free(ma);
793
794 return -1;
795 }
796
797 void pa__done(pa_module *m) {
798 struct userdata *u;
799 coreaudio_sink *ca_sink;
800 coreaudio_source *ca_source;
801 AudioObjectPropertyAddress property_address;
802
803 pa_assert(m);
804
805 u = m->userdata;
806 pa_assert(u);
807
808 /* unlink sinks */
809 for (ca_sink = u->sinks; ca_sink; ca_sink = ca_sink->next)
810 if (ca_sink->pa_sink)
811 pa_sink_unlink(ca_sink->pa_sink);
812
813 /* unlink sources */
814 for (ca_source = u->sources; ca_source; ca_source = ca_source->next)
815 if (ca_source->pa_source)
816 pa_source_unlink(ca_source->pa_source);
817
818 if (u->thread) {
819 pa_asyncmsgq_send(u->thread_mq.inq, NULL, PA_MESSAGE_SHUTDOWN, NULL, 0, NULL);
820 pa_thread_free(u->thread);
821 pa_thread_mq_done(&u->thread_mq);
822 pa_asyncmsgq_unref(u->async_msgq);
823 }
824
825 /* free sinks */
826 for (ca_sink = u->sinks; ca_sink;) {
827 coreaudio_sink *next = ca_sink->next;
828
829 if (ca_sink->pa_sink)
830 pa_sink_unref(ca_sink->pa_sink);
831
832 pa_xfree(ca_sink->name);
833 pa_xfree(ca_sink);
834 ca_sink = next;
835 }
836
837 /* free sources */
838 for (ca_source = u->sources; ca_source;) {
839 coreaudio_source *next = ca_source->next;
840
841 if (ca_source->pa_source)
842 pa_source_unref(ca_source->pa_source);
843
844 pa_xfree(ca_source->name);
845 pa_xfree(ca_source);
846 ca_source = next;
847 }
848
849 if (u->proc_id) {
850 AudioDeviceStop(u->object_id, u->proc_id);
851 AudioDeviceDestroyIOProcID(u->object_id, u->proc_id);
852 }
853
854 property_address.mSelector = kAudioDevicePropertyStreamFormat;
855 property_address.mScope = kAudioObjectPropertyScopeGlobal;
856 property_address.mElement = kAudioObjectPropertyElementMaster;
857
858 AudioObjectRemovePropertyListener(kAudioObjectSystemObject, &property_address, ca_stream_format_changed, u);
859
860 pa_xfree(u->device_name);
861 pa_xfree(u->vendor_name);
862 pa_rtpoll_free(u->rtpoll);
863 pa_card_free(u->card);
864
865 pa_xfree(u);
866 }