2 This file is part of PulseAudio.
4 Copyright 2009,2010 Daniel Mack <daniel@caiaq.de>
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.
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.
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
23 - implement hardware volume controls
24 - handle audio device stream format changes (will require changes to the core)
31 #include <pulse/xmalloc.h>
33 #include <pulsecore/sink.h>
34 #include <pulsecore/source.h>
35 #include <pulsecore/module.h>
36 #include <pulsecore/sample-util.h>
37 #include <pulsecore/core-util.h>
38 #include <pulsecore/modargs.h>
39 #include <pulsecore/log.h>
40 #include <pulsecore/macro.h>
41 #include <pulsecore/llist.h>
42 #include <pulsecore/card.h>
43 #include <pulsecore/strbuf.h>
44 #include <pulsecore/thread.h>
45 #include <pulsecore/thread-mq.h>
47 #include <CoreAudio/CoreAudio.h>
48 #include <CoreAudio/CoreAudioTypes.h>
49 #include <CoreAudio/AudioHardware.h>
51 #include "module-coreaudio-device-symdef.h"
53 #define DEFAULT_FRAMES_PER_IOPROC 512
55 PA_MODULE_AUTHOR("Daniel Mack");
56 PA_MODULE_DESCRIPTION("CoreAudio device");
57 PA_MODULE_VERSION(PACKAGE_VERSION
);
58 PA_MODULE_LOAD_ONCE(FALSE
);
59 PA_MODULE_USAGE("object_id=<the CoreAudio device id> "
60 "ioproc_frames=<audio frames per IOProc call> ");
62 static const char* const valid_modargs
[] = {
69 CA_MESSAGE_RENDER
= PA_SINK_MESSAGE_MAX
,
72 typedef struct coreaudio_sink coreaudio_sink
;
73 typedef struct coreaudio_source coreaudio_source
;
76 AudioObjectID object_id
;
77 AudioDeviceIOProcID proc_id
;
79 pa_thread_mq thread_mq
;
80 pa_asyncmsgq
*async_msgq
;
89 char *device_name
, *vendor_name
;
91 const AudioBufferList
*render_input_data
;
92 AudioBufferList
*render_output_data
;
94 AudioStreamBasicDescription stream_description
;
96 PA_LLIST_HEAD(coreaudio_sink
, sinks
);
97 PA_LLIST_HEAD(coreaudio_source
, sources
);
100 struct coreaudio_sink
{
102 struct userdata
*userdata
;
105 unsigned int channel_idx
;
111 PA_LLIST_FIELDS(coreaudio_sink
);
114 struct coreaudio_source
{
115 pa_source
*pa_source
;
116 struct userdata
*userdata
;
119 unsigned int channel_idx
;
125 PA_LLIST_FIELDS(coreaudio_source
);
128 static OSStatus
io_render_proc (AudioDeviceID device
,
129 const AudioTimeStamp
*now
,
130 const AudioBufferList
*inputData
,
131 const AudioTimeStamp
*inputTime
,
132 AudioBufferList
*outputData
,
133 const AudioTimeStamp
*outputTime
,
136 struct userdata
*u
= clientData
;
139 pa_assert(device
== u
->object_id
);
141 u
->render_input_data
= inputData
;
142 u
->render_output_data
= outputData
;
145 pa_assert_se(pa_asyncmsgq_send(u
->async_msgq
, PA_MSGOBJECT(u
->sinks
->pa_sink
),
146 CA_MESSAGE_RENDER
, NULL
, 0, NULL
) == 0);
149 pa_assert_se(pa_asyncmsgq_send(u
->async_msgq
, PA_MSGOBJECT(u
->sources
->pa_source
),
150 CA_MESSAGE_RENDER
, NULL
, 0, NULL
) == 0);
155 static OSStatus
ca_stream_format_changed(AudioObjectID objectID
,
156 UInt32 numberAddresses
,
157 const AudioObjectPropertyAddress addresses
[],
160 struct userdata
*u
= clientData
;
165 /* REVISIT: PA can't currently handle external format change requests.
166 * Hence, we set the original format back in this callback to avoid horrible audio artefacts.
167 * The device settings will appear to be 'locked' for any application as long as the PA daemon is running.
168 * Once we're able to propagate such events up in the core, this needs to be changed. */
170 for (i
= 0; i
< numberAddresses
; i
++)
171 AudioObjectSetPropertyData(objectID
, addresses
+ i
, 0, NULL
, sizeof(u
->stream_description
), &u
->stream_description
);
176 static pa_usec_t
get_latency_us(pa_object
*o
) {
181 UInt32 err
, size
= sizeof(v
);
182 AudioObjectPropertyAddress property_address
;
183 AudioObjectID stream_id
;
185 if (pa_sink_isinstance(o
)) {
186 coreaudio_sink
*sink
= PA_SINK(o
)->userdata
;
191 } else if (pa_source_isinstance(o
)) {
192 coreaudio_source
*source
= PA_SOURCE(o
)->userdata
;
194 u
= source
->userdata
;
198 pa_assert_not_reached();
202 property_address
.mScope
= kAudioObjectPropertyScopeGlobal
;
203 property_address
.mElement
= kAudioObjectPropertyElementMaster
;
205 /* get the device latency */
206 property_address
.mSelector
= kAudioDevicePropertyLatency
;
207 size
= sizeof(total
);
208 AudioObjectGetPropertyData(u
->object_id
, &property_address
, 0, NULL
, &size
, &total
);
211 /* the the IOProc buffer size */
212 property_address
.mSelector
= kAudioDevicePropertyBufferFrameSize
;
214 AudioObjectGetPropertyData(u
->object_id
, &property_address
, 0, NULL
, &size
, &v
);
217 /* IOProc safety offset - this value is the same for both directions, hence we divide it by 2 */
218 property_address
.mSelector
= kAudioDevicePropertySafetyOffset
;
220 AudioObjectGetPropertyData(u
->object_id
, &property_address
, 0, NULL
, &size
, &v
);
223 /* get the stream latency.
224 * FIXME: this assumes the stream latency is the same for all streams */
225 property_address
.mSelector
= kAudioDevicePropertyStreams
;
226 size
= sizeof(stream_id
);
227 err
= AudioObjectGetPropertyData(u
->object_id
, &property_address
, 0, NULL
, &size
, &stream_id
);
229 property_address
.mSelector
= kAudioStreamPropertyLatency
;
231 err
= AudioObjectGetPropertyData(stream_id
, &property_address
, 0, NULL
, &size
, &v
);
236 return pa_bytes_to_usec(total
* pa_frame_size(ss
), ss
);
239 static void ca_device_check_device_state(struct userdata
*u
) {
240 coreaudio_sink
*ca_sink
;
241 coreaudio_source
*ca_source
;
242 pa_bool_t active
= FALSE
;
246 for (ca_sink
= u
->sinks
; ca_sink
; ca_sink
= ca_sink
->next
)
250 for (ca_source
= u
->sources
; ca_source
; ca_source
= ca_source
->next
)
251 if (ca_source
->active
)
254 if (active
&& !u
->running
)
255 AudioDeviceStart(u
->object_id
, u
->proc_id
);
256 else if (!active
&& u
->running
)
257 AudioDeviceStop(u
->object_id
, u
->proc_id
);
262 static int sink_process_msg(pa_msgobject
*o
, int code
, void *data
, int64_t offset
, pa_memchunk
*chunk
) {
263 coreaudio_sink
*sink
= PA_SINK(o
)->userdata
;
264 struct userdata
*u
= sink
->userdata
;
266 pa_memchunk audio_chunk
;
269 case CA_MESSAGE_RENDER
: {
271 for (i
= 0; i
< u
->render_output_data
->mNumberBuffers
; i
++) {
272 AudioBuffer
*buf
= u
->render_output_data
->mBuffers
+ i
;
276 if (PA_SINK_IS_OPENED(sink
->pa_sink
->thread_info
.state
)) {
277 if (sink
->pa_sink
->thread_info
.rewind_requested
)
278 pa_sink_process_rewind(sink
->pa_sink
, 0);
280 audio_chunk
.memblock
= pa_memblock_new_fixed(u
->module
->core
->mempool
, buf
->mData
, buf
->mDataByteSize
, FALSE
);
281 audio_chunk
.length
= buf
->mDataByteSize
;
282 audio_chunk
.index
= 0;
284 pa_sink_render_into_full(sink
->pa_sink
, &audio_chunk
);
285 pa_memblock_unref_fixed(audio_chunk
.memblock
);
294 case PA_SINK_MESSAGE_GET_LATENCY
: {
295 *((pa_usec_t
*) data
) = get_latency_us(PA_OBJECT(o
));
300 return pa_sink_process_msg(o
, code
, data
, offset
, chunk
);
303 static int source_process_msg(pa_msgobject
*o
, int code
, void *data
, int64_t offset
, pa_memchunk
*chunk
) {
304 coreaudio_source
*source
= PA_SOURCE(o
)->userdata
;
305 struct userdata
*u
= source
->userdata
;
307 pa_memchunk audio_chunk
;
310 case CA_MESSAGE_RENDER
: {
312 for (i
= 0; i
< u
->render_input_data
->mNumberBuffers
; i
++) {
313 const AudioBuffer
*buf
= u
->render_input_data
->mBuffers
+ i
;
317 if (PA_SOURCE_IS_OPENED(source
->pa_source
->thread_info
.state
)) {
318 audio_chunk
.memblock
= pa_memblock_new_fixed(u
->module
->core
->mempool
, buf
->mData
, buf
->mDataByteSize
, TRUE
);
319 audio_chunk
.length
= buf
->mDataByteSize
;
320 audio_chunk
.index
= 0;
322 pa_source_post(source
->pa_source
, &audio_chunk
);
323 pa_memblock_unref_fixed(audio_chunk
.memblock
);
326 source
= source
->next
;
332 case PA_SOURCE_MESSAGE_GET_LATENCY
: {
333 *((pa_usec_t
*) data
) = get_latency_us(PA_OBJECT(o
));
338 return pa_source_process_msg(o
, code
, data
, offset
, chunk
);;
341 static int ca_sink_set_state(pa_sink
*s
, pa_sink_state_t state
)
343 coreaudio_sink
*sink
= s
->userdata
;
346 case PA_SINK_SUSPENDED
:
348 sink
->active
= FALSE
;
351 case PA_SINK_RUNNING
:
355 case PA_SINK_UNLINKED
:
357 case PA_SINK_INVALID_STATE
:
361 ca_device_check_device_state(sink
->userdata
);
366 static int ca_device_create_sink(pa_module
*m
, AudioBuffer
*buf
, int channel_idx
) {
369 struct userdata
*u
= m
->userdata
;
370 pa_sink_new_data new_data
;
371 pa_sink_flags_t flags
= PA_SINK_LATENCY
| PA_SINK_HARDWARE
;
372 coreaudio_sink
*ca_sink
;
377 AudioObjectPropertyAddress property_address
;
379 ca_sink
= pa_xnew0(coreaudio_sink
, 1);
380 ca_sink
->map
.channels
= buf
->mNumberChannels
;
381 ca_sink
->ss
.channels
= buf
->mNumberChannels
;
382 ca_sink
->channel_idx
= channel_idx
;
384 /* build a name for this stream */
385 strbuf
= pa_strbuf_new();
387 for (i
= 0; i
< buf
->mNumberChannels
; i
++) {
388 property_address
.mSelector
= kAudioObjectPropertyElementName
;
389 property_address
.mScope
= kAudioDevicePropertyScopeOutput
;
390 property_address
.mElement
= channel_idx
+ i
+ 1;
392 err
= AudioObjectGetPropertyData(u
->object_id
, &property_address
, 0, NULL
, &size
, tmp
);
393 if (err
|| !strlen(tmp
))
394 snprintf(tmp
, sizeof(tmp
), "Channel %d", (int) property_address
.mElement
);
397 pa_strbuf_puts(strbuf
, ", ");
399 pa_strbuf_puts(strbuf
, tmp
);
402 ca_sink
->name
= pa_strbuf_tostring_free(strbuf
);
404 pa_log_debug("Stream name is >%s<", ca_sink
->name
);
406 /* default to mono streams */
407 for (i
= 0; i
< ca_sink
->map
.channels
; i
++)
408 ca_sink
->map
.map
[i
] = PA_CHANNEL_POSITION_MONO
;
410 if (buf
->mNumberChannels
== 2) {
411 ca_sink
->map
.map
[0] = PA_CHANNEL_POSITION_LEFT
;
412 ca_sink
->map
.map
[1] = PA_CHANNEL_POSITION_RIGHT
;
415 ca_sink
->ss
.rate
= u
->stream_description
.mSampleRate
;
416 ca_sink
->ss
.format
= PA_SAMPLE_FLOAT32LE
;
418 pa_sink_new_data_init(&new_data
);
419 new_data
.card
= u
->card
;
420 new_data
.driver
= __FILE__
;
421 new_data
.module
= u
->module
;
422 new_data
.namereg_fail
= FALSE
;
423 pa_sink_new_data_set_name(&new_data
, ca_sink
->name
);
424 pa_sink_new_data_set_channel_map(&new_data
, &ca_sink
->map
);
425 pa_sink_new_data_set_sample_spec(&new_data
, &ca_sink
->ss
);
426 pa_proplist_sets(new_data
.proplist
, PA_PROP_DEVICE_STRING
, u
->device_name
);
427 pa_proplist_sets(new_data
.proplist
, PA_PROP_DEVICE_PRODUCT_NAME
, u
->device_name
);
428 pa_proplist_sets(new_data
.proplist
, PA_PROP_DEVICE_DESCRIPTION
, u
->device_name
);
429 pa_proplist_sets(new_data
.proplist
, PA_PROP_DEVICE_ACCESS_MODE
, "mmap");
430 pa_proplist_sets(new_data
.proplist
, PA_PROP_DEVICE_CLASS
, "sound");
431 pa_proplist_sets(new_data
.proplist
, PA_PROP_DEVICE_API
, "CoreAudio");
432 pa_proplist_setf(new_data
.proplist
, PA_PROP_DEVICE_BUFFERING_BUFFER_SIZE
, "%lu", (unsigned long) buf
->mDataByteSize
);
435 pa_proplist_sets(new_data
.proplist
, PA_PROP_DEVICE_VENDOR_NAME
, u
->vendor_name
);
437 sink
= pa_sink_new(m
->core
, &new_data
, flags
);
438 pa_sink_new_data_done(&new_data
);
441 pa_log("unable to create sink.");
445 sink
->parent
.process_msg
= sink_process_msg
;
446 sink
->userdata
= ca_sink
;
447 sink
->set_state
= ca_sink_set_state
;
449 pa_sink_set_asyncmsgq(sink
, u
->thread_mq
.inq
);
450 pa_sink_set_rtpoll(sink
, u
->rtpoll
);
452 ca_sink
->pa_sink
= sink
;
453 ca_sink
->userdata
= u
;
455 PA_LLIST_PREPEND(coreaudio_sink
, u
->sinks
, ca_sink
);
460 static int ca_source_set_state(pa_source
*s
, pa_source_state_t state
)
462 coreaudio_source
*source
= s
->userdata
;
465 case PA_SOURCE_SUSPENDED
:
467 source
->active
= FALSE
;
470 case PA_SOURCE_RUNNING
:
471 source
->active
= TRUE
;
474 case PA_SOURCE_UNLINKED
:
476 case PA_SOURCE_INVALID_STATE
:
480 ca_device_check_device_state(source
->userdata
);
485 static int ca_device_create_source(pa_module
*m
, AudioBuffer
*buf
, int channel_idx
) {
488 struct userdata
*u
= m
->userdata
;
489 pa_source_new_data new_data
;
490 pa_source_flags_t flags
= PA_SOURCE_LATENCY
| PA_SOURCE_HARDWARE
;
491 coreaudio_source
*ca_source
;
496 AudioObjectPropertyAddress property_address
;
498 ca_source
= pa_xnew0(coreaudio_source
, 1);
499 ca_source
->map
.channels
= buf
->mNumberChannels
;
500 ca_source
->ss
.channels
= buf
->mNumberChannels
;
501 ca_source
->channel_idx
= channel_idx
;
503 /* build a name for this stream */
504 strbuf
= pa_strbuf_new();
506 for (i
= 0; i
< buf
->mNumberChannels
; i
++) {
507 property_address
.mSelector
= kAudioObjectPropertyElementName
;
508 property_address
.mScope
= kAudioDevicePropertyScopeInput
;
509 property_address
.mElement
= channel_idx
+ i
+ 1;
511 err
= AudioObjectGetPropertyData(u
->object_id
, &property_address
, 0, NULL
, &size
, tmp
);
512 if (err
|| !strlen(tmp
))
513 snprintf(tmp
, sizeof(tmp
), "Channel %d", (int) property_address
.mElement
);
516 pa_strbuf_puts(strbuf
, ", ");
518 pa_strbuf_puts(strbuf
, tmp
);
521 ca_source
->name
= pa_strbuf_tostring_free(strbuf
);
523 pa_log_debug("Stream name is >%s<", ca_source
->name
);
525 /* default to mono streams */
526 for (i
= 0; i
< ca_source
->map
.channels
; i
++)
527 ca_source
->map
.map
[i
] = PA_CHANNEL_POSITION_MONO
;
529 if (buf
->mNumberChannels
== 2) {
530 ca_source
->map
.map
[0] = PA_CHANNEL_POSITION_LEFT
;
531 ca_source
->map
.map
[1] = PA_CHANNEL_POSITION_RIGHT
;
534 ca_source
->ss
.rate
= u
->stream_description
.mSampleRate
;
535 ca_source
->ss
.format
= PA_SAMPLE_FLOAT32LE
;
537 pa_source_new_data_init(&new_data
);
538 new_data
.card
= u
->card
;
539 new_data
.driver
= __FILE__
;
540 new_data
.module
= u
->module
;
541 new_data
.namereg_fail
= FALSE
;
542 pa_source_new_data_set_name(&new_data
, ca_source
->name
);
543 pa_source_new_data_set_channel_map(&new_data
, &ca_source
->map
);
544 pa_source_new_data_set_sample_spec(&new_data
, &ca_source
->ss
);
545 pa_proplist_sets(new_data
.proplist
, PA_PROP_DEVICE_STRING
, u
->device_name
);
546 pa_proplist_sets(new_data
.proplist
, PA_PROP_DEVICE_PRODUCT_NAME
, u
->device_name
);
547 pa_proplist_sets(new_data
.proplist
, PA_PROP_DEVICE_DESCRIPTION
, u
->device_name
);
548 pa_proplist_sets(new_data
.proplist
, PA_PROP_DEVICE_ACCESS_MODE
, "mmap");
549 pa_proplist_sets(new_data
.proplist
, PA_PROP_DEVICE_CLASS
, "sound");
550 pa_proplist_sets(new_data
.proplist
, PA_PROP_DEVICE_API
, "CoreAudio");
551 pa_proplist_setf(new_data
.proplist
, PA_PROP_DEVICE_BUFFERING_BUFFER_SIZE
, "%lu", (unsigned long) buf
->mDataByteSize
);
554 pa_proplist_sets(new_data
.proplist
, PA_PROP_DEVICE_VENDOR_NAME
, u
->vendor_name
);
556 source
= pa_source_new(m
->core
, &new_data
, flags
);
557 pa_source_new_data_done(&new_data
);
560 pa_log("unable to create source.");
564 source
->parent
.process_msg
= source_process_msg
;
565 source
->userdata
= ca_source
;
566 source
->set_state
= ca_source_set_state
;
568 pa_source_set_asyncmsgq(source
, u
->thread_mq
.inq
);
569 pa_source_set_rtpoll(source
, u
->rtpoll
);
571 ca_source
->pa_source
= source
;
572 ca_source
->userdata
= u
;
574 PA_LLIST_PREPEND(coreaudio_source
, u
->sources
, ca_source
);
579 static int ca_device_create_streams(pa_module
*m
, bool direction_in
) {
581 UInt32 size
, i
, channel_idx
;
582 struct userdata
*u
= m
->userdata
;
583 AudioBufferList
*buffer_list
;
584 AudioObjectPropertyAddress property_address
;
586 property_address
.mScope
= direction_in
? kAudioDevicePropertyScopeInput
: kAudioDevicePropertyScopeOutput
;
587 property_address
.mElement
= kAudioObjectPropertyElementMaster
;
589 /* get current stream format */
590 size
= sizeof(AudioStreamBasicDescription
);
591 property_address
.mSelector
= kAudioDevicePropertyStreamFormat
;
592 err
= AudioObjectGetPropertyData(u
->object_id
, &property_address
, 0, NULL
, &size
, &u
->stream_description
);
594 /* no appropriate streams found - silently bail. */
598 if (u
->stream_description
.mFormatID
!= kAudioFormatLinearPCM
) {
599 pa_log("Unsupported audio format '%c%c%c%c'",
600 (char) (u
->stream_description
.mFormatID
>> 24),
601 (char) (u
->stream_description
.mFormatID
>> 16) & 0xff,
602 (char) (u
->stream_description
.mFormatID
>> 8) & 0xff,
603 (char) (u
->stream_description
.mFormatID
& 0xff));
607 /* get stream configuration */
609 property_address
.mSelector
= kAudioDevicePropertyStreamConfiguration
;
610 err
= AudioObjectGetPropertyDataSize(u
->object_id
, &property_address
, 0, NULL
, &size
);
612 pa_log("Failed to get kAudioDevicePropertyStreamConfiguration (%s).", direction_in
? "input" : "output");
619 buffer_list
= (AudioBufferList
*) pa_xmalloc(size
);
620 err
= AudioObjectGetPropertyData(u
->object_id
, &property_address
, 0, NULL
, &size
, buffer_list
);
623 pa_log_debug("Sample rate: %f", u
->stream_description
.mSampleRate
);
624 pa_log_debug("%d bytes per packet", (unsigned int) u
->stream_description
.mBytesPerPacket
);
625 pa_log_debug("%d frames per packet", (unsigned int) u
->stream_description
.mFramesPerPacket
);
626 pa_log_debug("%d bytes per frame", (unsigned int) u
->stream_description
.mBytesPerFrame
);
627 pa_log_debug("%d channels per frame", (unsigned int) u
->stream_description
.mChannelsPerFrame
);
628 pa_log_debug("%d bits per channel", (unsigned int) u
->stream_description
.mBitsPerChannel
);
630 for (channel_idx
= 0, i
= 0; i
< buffer_list
->mNumberBuffers
; i
++) {
631 AudioBuffer
*buf
= buffer_list
->mBuffers
+ i
;
634 ca_device_create_source(m
, buf
, channel_idx
);
636 ca_device_create_sink(m
, buf
, channel_idx
);
638 channel_idx
+= buf
->mNumberChannels
;
642 pa_xfree(buffer_list
);
646 static void thread_func(void *userdata
) {
647 struct userdata
*u
= userdata
;
650 pa_assert(u
->module
);
651 pa_assert(u
->module
->core
);
653 pa_log_debug("Thread starting up");
655 if (u
->module
->core
->realtime_scheduling
)
656 pa_make_realtime(u
->module
->core
->realtime_priority
);
658 pa_thread_mq_install(&u
->thread_mq
);
663 ret
= pa_rtpoll_run(u
->rtpoll
, TRUE
);
673 /* If this was no regular exit from the loop we have to continue
674 * processing messages until we received PA_MESSAGE_SHUTDOWN */
675 pa_asyncmsgq_post(u
->thread_mq
.outq
, PA_MSGOBJECT(u
->module
->core
), PA_CORE_MESSAGE_UNLOAD_MODULE
, u
->module
, 0, NULL
, NULL
);
676 pa_asyncmsgq_wait_for(u
->thread_mq
.inq
, PA_MESSAGE_SHUTDOWN
);
679 pa_log_debug("Thread shutting down");
682 int pa__init(pa_module
*m
) {
685 struct userdata
*u
= NULL
;
686 pa_modargs
*ma
= NULL
;
688 pa_card_new_data card_new_data
;
689 coreaudio_sink
*ca_sink
;
690 coreaudio_source
*ca_source
;
691 AudioObjectPropertyAddress property_address
;
695 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
696 pa_log("Failed to parse module arguments.");
700 u
= pa_xnew0(struct userdata
, 1);
704 if (pa_modargs_get_value_u32(ma
, "object_id", (unsigned int *) &u
->object_id
) != 0) {
705 pa_log("Failed to parse object_id argument.");
709 property_address
.mScope
= kAudioObjectPropertyScopeGlobal
;
710 property_address
.mElement
= kAudioObjectPropertyElementMaster
;
712 /* get device product name */
713 property_address
.mSelector
= kAudioDevicePropertyDeviceName
;
715 err
= AudioObjectGetPropertyData(u
->object_id
, &property_address
, 0, NULL
, &size
, tmp
);
717 pa_log("Failed to get kAudioDevicePropertyDeviceName (err = %08x).", (int) err
);
721 u
->device_name
= pa_xstrdup(tmp
);
723 pa_card_new_data_init(&card_new_data
);
724 pa_proplist_sets(card_new_data
.proplist
, PA_PROP_DEVICE_STRING
, tmp
);
725 card_new_data
.driver
= __FILE__
;
726 pa_card_new_data_set_name(&card_new_data
, tmp
);
727 pa_log_info("Initializing module for CoreAudio device '%s' (id %d)", tmp
, (unsigned int) u
->object_id
);
729 /* get device vendor name (may fail) */
730 property_address
.mSelector
= kAudioDevicePropertyDeviceManufacturer
;
732 err
= AudioObjectGetPropertyData(u
->object_id
, &property_address
, 0, NULL
, &size
, tmp
);
734 u
->vendor_name
= pa_xstrdup(tmp
);
736 /* create the card object */
737 u
->card
= pa_card_new(m
->core
, &card_new_data
);
739 pa_log("Unable to create card.\n");
743 pa_card_new_data_done(&card_new_data
);
744 u
->card
->userdata
= u
;
746 u
->rtpoll
= pa_rtpoll_new();
747 pa_thread_mq_init(&u
->thread_mq
, m
->core
->mainloop
, u
->rtpoll
);
748 u
->async_msgq
= pa_asyncmsgq_new(0);
749 pa_rtpoll_item_new_asyncmsgq_read(u
->rtpoll
, PA_RTPOLL_EARLY
-1, u
->async_msgq
);
751 PA_LLIST_HEAD_INIT(coreaudio_sink
, u
->sinks
);
754 ca_device_create_streams(m
, FALSE
);
757 ca_device_create_streams(m
, TRUE
);
759 /* create the message thread */
760 if (!(u
->thread
= pa_thread_new(u
->device_name
, thread_func
, u
))) {
761 pa_log("Failed to create thread.");
765 /* register notification callback for stream format changes */
766 property_address
.mSelector
= kAudioDevicePropertyStreamFormat
;
767 property_address
.mScope
= kAudioObjectPropertyScopeGlobal
;
768 property_address
.mElement
= kAudioObjectPropertyElementMaster
;
770 AudioObjectAddPropertyListener(u
->object_id
, &property_address
, ca_stream_format_changed
, u
);
772 /* set number of frames in IOProc */
773 frames
= DEFAULT_FRAMES_PER_IOPROC
;
774 pa_modargs_get_value_u32(ma
, "ioproc_frames", (unsigned int *) &frames
);
776 property_address
.mSelector
= kAudioDevicePropertyBufferFrameSize
;
777 AudioObjectSetPropertyData(u
->object_id
, &property_address
, 0, NULL
, sizeof(frames
), &frames
);
778 pa_log_debug("%u frames per IOProc\n", (unsigned int) frames
);
780 /* create one ioproc for both directions */
781 err
= AudioDeviceCreateIOProcID(u
->object_id
, io_render_proc
, u
, &u
->proc_id
);
783 pa_log("AudioDeviceCreateIOProcID() failed (err = %08x\n).", (int) err
);
787 for (ca_sink
= u
->sinks
; ca_sink
; ca_sink
= ca_sink
->next
)
788 pa_sink_put(ca_sink
->pa_sink
);
790 for (ca_source
= u
->sources
; ca_source
; ca_source
= ca_source
->next
)
791 pa_source_put(ca_source
->pa_source
);
807 void pa__done(pa_module
*m
) {
809 coreaudio_sink
*ca_sink
;
810 coreaudio_source
*ca_source
;
811 AudioObjectPropertyAddress property_address
;
819 for (ca_sink
= u
->sinks
; ca_sink
; ca_sink
= ca_sink
->next
)
820 if (ca_sink
->pa_sink
)
821 pa_sink_unlink(ca_sink
->pa_sink
);
824 for (ca_source
= u
->sources
; ca_source
; ca_source
= ca_source
->next
)
825 if (ca_source
->pa_source
)
826 pa_source_unlink(ca_source
->pa_source
);
829 pa_asyncmsgq_send(u
->thread_mq
.inq
, NULL
, PA_MESSAGE_SHUTDOWN
, NULL
, 0, NULL
);
830 pa_thread_free(u
->thread
);
831 pa_thread_mq_done(&u
->thread_mq
);
832 pa_asyncmsgq_unref(u
->async_msgq
);
836 for (ca_sink
= u
->sinks
; ca_sink
;) {
837 coreaudio_sink
*next
= ca_sink
->next
;
839 if (ca_sink
->pa_sink
)
840 pa_sink_unref(ca_sink
->pa_sink
);
842 pa_xfree(ca_sink
->name
);
848 for (ca_source
= u
->sources
; ca_source
;) {
849 coreaudio_source
*next
= ca_source
->next
;
851 if (ca_source
->pa_source
)
852 pa_source_unref(ca_source
->pa_source
);
854 pa_xfree(ca_source
->name
);
860 AudioDeviceStop(u
->object_id
, u
->proc_id
);
861 AudioDeviceDestroyIOProcID(u
->object_id
, u
->proc_id
);
864 property_address
.mSelector
= kAudioDevicePropertyStreamFormat
;
865 property_address
.mScope
= kAudioObjectPropertyScopeGlobal
;
866 property_address
.mElement
= kAudioObjectPropertyElementMaster
;
868 AudioObjectRemovePropertyListener(kAudioObjectSystemObject
, &property_address
, ca_stream_format_changed
, u
);
870 pa_xfree(u
->device_name
);
871 pa_xfree(u
->vendor_name
);
872 pa_rtpoll_free(u
->rtpoll
);
873 pa_card_free(u
->card
);