2 This file is part of PulseAudio.
4 Copyright 2009 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>
32 #include <pulse/util.h>
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>
49 #include <CoreAudio/CoreAudio.h>
50 #include <CoreAudio/CoreAudioTypes.h>
51 #include <CoreAudio/AudioHardware.h>
53 #include "module-coreaudio-device-symdef.h"
55 #define DEFAULT_FRAMES_PER_IOPROC 512
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("device_id=<the CoreAudio device id> "
62 "ioproc_frames=<audio frames per IOProc call> ");
64 static const char* const valid_modargs
[] = {
71 CA_MESSAGE_RENDER
= PA_SINK_MESSAGE_MAX
,
74 typedef struct coreaudio_sink coreaudio_sink
;
75 typedef struct coreaudio_source coreaudio_source
;
78 AudioDeviceID device_id
;
79 AudioDeviceIOProcID proc_id
;
81 pa_thread_mq thread_mq
;
82 pa_asyncmsgq
*async_msgq
;
91 char *device_name
, *vendor_name
;
93 const AudioBufferList
*render_input_data
;
94 AudioBufferList
*render_output_data
;
96 AudioStreamBasicDescription stream_description
;
98 PA_LLIST_HEAD(coreaudio_sink
, sinks
);
99 PA_LLIST_HEAD(coreaudio_source
, sources
);
102 struct coreaudio_sink
{
104 struct userdata
*userdata
;
107 unsigned int channel_idx
;
113 PA_LLIST_FIELDS(coreaudio_sink
);
116 struct coreaudio_source
{
117 pa_source
*pa_source
;
118 struct userdata
*userdata
;
121 unsigned int channel_idx
;
127 PA_LLIST_FIELDS(coreaudio_source
);
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
,
138 struct userdata
*u
= clientData
;
141 pa_assert(device
== u
->device_id
);
143 u
->render_input_data
= inputData
;
144 u
->render_output_data
= outputData
;
147 pa_assert_se(pa_asyncmsgq_send(u
->async_msgq
, PA_MSGOBJECT(u
->sinks
->pa_sink
),
148 CA_MESSAGE_RENDER
, NULL
, 0, NULL
) == 0);
151 pa_assert_se(pa_asyncmsgq_send(u
->async_msgq
, PA_MSGOBJECT(u
->sources
->pa_source
),
152 CA_MESSAGE_RENDER
, NULL
, 0, NULL
) == 0);
157 static OSStatus
ca_stream_format_changed(AudioDeviceID inDevice
,
160 AudioDevicePropertyID inPropertyID
,
163 struct userdata
*u
= inClientData
;
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. */
172 return AudioDeviceSetProperty(inDevice
, NULL
, inChannel
, isInput
,
173 kAudioDevicePropertyStreamFormat
, sizeof(u
->stream_description
), &u
->stream_description
);
176 static pa_usec_t
get_latency_us(pa_object
*o
) {
181 UInt32 err
, size
= sizeof(v
);
183 if (pa_sink_isinstance(o
)) {
184 coreaudio_sink
*sink
= PA_SINK(o
)->userdata
;
189 } else if (pa_source_isinstance(o
)) {
190 coreaudio_source
*source
= PA_SOURCE(o
)->userdata
;
192 u
= source
->userdata
;
196 pa_assert_not_reached();
200 /* get the device latency */
201 size
= sizeof(total
);
202 AudioDeviceGetProperty(u
->device_id
, 0, is_source
, kAudioDevicePropertyLatency
, &size
, &v
);
205 /* the the IOProc buffer size */
207 AudioDeviceGetProperty(u
->device_id
, 0, is_source
, kAudioDevicePropertyBufferFrameSize
, &size
, &v
);
210 /* IOProc safety offset - this value is the same for both directions, hence we divide it by 2 */
212 AudioDeviceGetProperty(u
->device_id
, 0, is_source
, kAudioDevicePropertySafetyOffset
, &size
, &v
);
215 /* get the stream latency.
216 * FIXME: this assumes the stream latency is the same for all streams */
217 err
= AudioStreamGetProperty(0, is_source
, kAudioStreamPropertyLatency
, &size
, &v
);
221 return pa_bytes_to_usec(total
* pa_frame_size(ss
), ss
);
224 static void ca_device_check_device_state(struct userdata
*u
) {
225 coreaudio_sink
*ca_sink
;
226 coreaudio_source
*ca_source
;
227 pa_bool_t active
= FALSE
;
231 for (ca_sink
= u
->sinks
; ca_sink
; ca_sink
= ca_sink
->next
)
235 for (ca_source
= u
->sources
; ca_source
; ca_source
= ca_source
->next
)
236 if (ca_source
->active
)
239 if (active
&& !u
->running
)
240 AudioDeviceStart(u
->device_id
, u
->proc_id
);
241 else if (!active
&& u
->running
)
242 AudioDeviceStop(u
->device_id
, u
->proc_id
);
247 static int sink_process_msg(pa_msgobject
*o
, int code
, void *data
, int64_t offset
, pa_memchunk
*chunk
) {
248 coreaudio_sink
*sink
= PA_SINK(o
)->userdata
;
249 struct userdata
*u
= sink
->userdata
;
251 pa_memchunk audio_chunk
;
254 case CA_MESSAGE_RENDER
: {
256 for (i
= 0; i
< u
->render_output_data
->mNumberBuffers
; i
++) {
257 AudioBuffer
*buf
= u
->render_output_data
->mBuffers
+ i
;
261 if (PA_SINK_IS_OPENED(sink
->pa_sink
->thread_info
.state
)) {
262 if (sink
->pa_sink
->thread_info
.rewind_requested
)
263 pa_sink_process_rewind(sink
->pa_sink
, 0);
265 audio_chunk
.memblock
= pa_memblock_new_fixed(u
->module
->core
->mempool
, buf
->mData
, buf
->mDataByteSize
, FALSE
);
266 audio_chunk
.length
= buf
->mDataByteSize
;
267 audio_chunk
.index
= 0;
269 pa_sink_render_into_full(sink
->pa_sink
, &audio_chunk
);
270 pa_memblock_unref_fixed(audio_chunk
.memblock
);
279 case PA_SINK_MESSAGE_GET_LATENCY
: {
280 *((pa_usec_t
*) data
) = get_latency_us(PA_OBJECT(o
));
284 case PA_SINK_MESSAGE_SET_STATE
:
285 switch ((pa_sink_state_t
) PA_PTR_TO_UINT(data
)) {
286 case PA_SINK_SUSPENDED
:
288 sink
->active
= FALSE
;
291 case PA_SINK_RUNNING
:
295 case PA_SINK_UNLINKED
:
297 case PA_SINK_INVALID_STATE
:
301 ca_device_check_device_state(sink
->userdata
);
305 return pa_sink_process_msg(o
, code
, data
, offset
, chunk
);
308 static int source_process_msg(pa_msgobject
*o
, int code
, void *data
, int64_t offset
, pa_memchunk
*chunk
) {
309 coreaudio_source
*source
= PA_SOURCE(o
)->userdata
;
310 struct userdata
*u
= source
->userdata
;
312 pa_memchunk audio_chunk
;
315 case CA_MESSAGE_RENDER
: {
317 for (i
= 0; i
< u
->render_input_data
->mNumberBuffers
; i
++) {
318 const AudioBuffer
*buf
= u
->render_input_data
->mBuffers
+ i
;
322 if (PA_SOURCE_IS_OPENED(source
->pa_source
->thread_info
.state
)) {
323 audio_chunk
.memblock
= pa_memblock_new_fixed(u
->module
->core
->mempool
, buf
->mData
, buf
->mDataByteSize
, TRUE
);
324 audio_chunk
.length
= buf
->mDataByteSize
;
325 audio_chunk
.index
= 0;
327 pa_source_post(source
->pa_source
, &audio_chunk
);
328 pa_memblock_unref_fixed(audio_chunk
.memblock
);
331 source
= source
->next
;
337 case PA_SOURCE_MESSAGE_GET_LATENCY
: {
338 *((pa_usec_t
*) data
) = get_latency_us(PA_OBJECT(o
));
342 case PA_SOURCE_MESSAGE_SET_STATE
:
343 switch ((pa_source_state_t
) PA_PTR_TO_UINT(data
)) {
344 case PA_SOURCE_SUSPENDED
:
346 source
->active
= FALSE
;
349 case PA_SOURCE_RUNNING
:
350 source
->active
= TRUE
;
353 case PA_SOURCE_UNLINKED
:
355 case PA_SOURCE_INVALID_STATE
:
359 ca_device_check_device_state(source
->userdata
);
363 return pa_source_process_msg(o
, code
, data
, offset
, chunk
);
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
;
378 ca_sink
= pa_xnew0(coreaudio_sink
, 1);
379 ca_sink
->map
.channels
= buf
->mNumberChannels
;
380 ca_sink
->ss
.channels
= buf
->mNumberChannels
;
381 ca_sink
->channel_idx
= channel_idx
;
383 /* build a name for this stream */
384 strbuf
= pa_strbuf_new();
386 for (i
= 0; i
< buf
->mNumberChannels
; i
++) {
388 err
= AudioDeviceGetProperty(u
->device_id
, channel_idx
+ i
+ 1, 0, kAudioObjectPropertyElementName
, &size
, tmp
);
389 if (err
|| !strlen(tmp
))
390 snprintf(tmp
, sizeof(tmp
), "Channel %d", channel_idx
+ i
+ 1);
393 pa_strbuf_puts(strbuf
, ", ");
395 pa_strbuf_puts(strbuf
, tmp
);
398 ca_sink
->name
= pa_strbuf_tostring_free(strbuf
);
400 pa_log_debug("Stream name is >%s<", ca_sink
->name
);
402 /* default to mono streams */
403 for (i
= 0; i
< ca_sink
->map
.channels
; i
++)
404 ca_sink
->map
.map
[i
] = PA_CHANNEL_POSITION_MONO
;
406 if (buf
->mNumberChannels
== 2) {
407 ca_sink
->map
.map
[0] = PA_CHANNEL_POSITION_LEFT
;
408 ca_sink
->map
.map
[1] = PA_CHANNEL_POSITION_RIGHT
;
411 ca_sink
->ss
.rate
= u
->stream_description
.mSampleRate
;
412 ca_sink
->ss
.format
= PA_SAMPLE_FLOAT32LE
;
414 pa_sink_new_data_init(&new_data
);
415 new_data
.card
= u
->card
;
416 new_data
.driver
= __FILE__
;
417 new_data
.module
= u
->module
;
418 new_data
.namereg_fail
= FALSE
;
419 pa_sink_new_data_set_name(&new_data
, ca_sink
->name
);
420 pa_sink_new_data_set_channel_map(&new_data
, &ca_sink
->map
);
421 pa_sink_new_data_set_sample_spec(&new_data
, &ca_sink
->ss
);
422 pa_proplist_sets(new_data
.proplist
, PA_PROP_DEVICE_STRING
, u
->device_name
);
423 pa_proplist_sets(new_data
.proplist
, PA_PROP_DEVICE_PRODUCT_NAME
, u
->device_name
);
424 pa_proplist_sets(new_data
.proplist
, PA_PROP_DEVICE_DESCRIPTION
, u
->device_name
);
425 pa_proplist_sets(new_data
.proplist
, PA_PROP_DEVICE_ACCESS_MODE
, "mmap");
426 pa_proplist_sets(new_data
.proplist
, PA_PROP_DEVICE_CLASS
, "sound");
427 pa_proplist_sets(new_data
.proplist
, PA_PROP_DEVICE_API
, "CoreAudio");
428 pa_proplist_setf(new_data
.proplist
, PA_PROP_DEVICE_BUFFERING_BUFFER_SIZE
, "%lu", (unsigned long) buf
->mDataByteSize
);
431 pa_proplist_sets(new_data
.proplist
, PA_PROP_DEVICE_VENDOR_NAME
, u
->vendor_name
);
433 sink
= pa_sink_new(m
->core
, &new_data
, flags
);
434 pa_sink_new_data_done(&new_data
);
437 pa_log("unable to create sink.");
441 sink
->parent
.process_msg
= sink_process_msg
;
442 sink
->userdata
= ca_sink
;
444 pa_sink_set_asyncmsgq(sink
, u
->thread_mq
.inq
);
445 pa_sink_set_rtpoll(sink
, u
->rtpoll
);
447 ca_sink
->pa_sink
= sink
;
448 ca_sink
->userdata
= u
;
450 PA_LLIST_PREPEND(coreaudio_sink
, u
->sinks
, ca_sink
);
455 static int ca_device_create_source(pa_module
*m
, AudioBuffer
*buf
, int channel_idx
) {
458 struct userdata
*u
= m
->userdata
;
459 pa_source_new_data new_data
;
460 pa_source_flags_t flags
= PA_SOURCE_LATENCY
| PA_SOURCE_HARDWARE
;
461 coreaudio_source
*ca_source
;
467 ca_source
= pa_xnew0(coreaudio_source
, 1);
468 ca_source
->map
.channels
= buf
->mNumberChannels
;
469 ca_source
->ss
.channels
= buf
->mNumberChannels
;
470 ca_source
->channel_idx
= channel_idx
;
472 /* build a name for this stream */
473 strbuf
= pa_strbuf_new();
475 for (i
= 0; i
< buf
->mNumberChannels
; i
++) {
477 err
= AudioDeviceGetProperty(u
->device_id
, channel_idx
+ i
+ 1, 0, kAudioObjectPropertyElementName
, &size
, tmp
);
478 if (err
|| !strlen(tmp
))
479 snprintf(tmp
, sizeof(tmp
), "Channel %d", channel_idx
+ i
+ 1);
482 pa_strbuf_puts(strbuf
, ", ");
484 pa_strbuf_puts(strbuf
, tmp
);
487 ca_source
->name
= pa_strbuf_tostring_free(strbuf
);
489 pa_log_debug("Stream name is >%s<", ca_source
->name
);
491 /* default to mono streams */
492 for (i
= 0; i
< ca_source
->map
.channels
; i
++)
493 ca_source
->map
.map
[i
] = PA_CHANNEL_POSITION_MONO
;
495 if (buf
->mNumberChannels
== 2) {
496 ca_source
->map
.map
[0] = PA_CHANNEL_POSITION_LEFT
;
497 ca_source
->map
.map
[1] = PA_CHANNEL_POSITION_RIGHT
;
500 ca_source
->ss
.rate
= u
->stream_description
.mSampleRate
;
501 ca_source
->ss
.format
= PA_SAMPLE_FLOAT32LE
;
503 pa_source_new_data_init(&new_data
);
504 new_data
.card
= u
->card
;
505 new_data
.driver
= __FILE__
;
506 new_data
.module
= u
->module
;
507 new_data
.namereg_fail
= FALSE
;
508 pa_source_new_data_set_name(&new_data
, ca_source
->name
);
509 pa_source_new_data_set_channel_map(&new_data
, &ca_source
->map
);
510 pa_source_new_data_set_sample_spec(&new_data
, &ca_source
->ss
);
511 pa_proplist_sets(new_data
.proplist
, PA_PROP_DEVICE_STRING
, u
->device_name
);
512 pa_proplist_sets(new_data
.proplist
, PA_PROP_DEVICE_PRODUCT_NAME
, u
->device_name
);
513 pa_proplist_sets(new_data
.proplist
, PA_PROP_DEVICE_DESCRIPTION
, u
->device_name
);
514 pa_proplist_sets(new_data
.proplist
, PA_PROP_DEVICE_ACCESS_MODE
, "mmap");
515 pa_proplist_sets(new_data
.proplist
, PA_PROP_DEVICE_CLASS
, "sound");
516 pa_proplist_sets(new_data
.proplist
, PA_PROP_DEVICE_API
, "CoreAudio");
517 pa_proplist_setf(new_data
.proplist
, PA_PROP_DEVICE_BUFFERING_BUFFER_SIZE
, "%lu", (unsigned long) buf
->mDataByteSize
);
520 pa_proplist_sets(new_data
.proplist
, PA_PROP_DEVICE_VENDOR_NAME
, u
->vendor_name
);
522 source
= pa_source_new(m
->core
, &new_data
, flags
);
523 pa_source_new_data_done(&new_data
);
526 pa_log("unable to create source.");
530 source
->parent
.process_msg
= source_process_msg
;
531 source
->userdata
= ca_source
;
533 pa_source_set_asyncmsgq(source
, u
->thread_mq
.inq
);
534 pa_source_set_rtpoll(source
, u
->rtpoll
);
536 ca_source
->pa_source
= source
;
537 ca_source
->userdata
= u
;
539 PA_LLIST_PREPEND(coreaudio_source
, u
->sources
, ca_source
);
544 static int ca_device_create_streams(pa_module
*m
, bool direction_in
) {
546 UInt32 size
, i
, channel_idx
;
547 struct userdata
*u
= m
->userdata
;
548 int section
= direction_in
? 1 : 0;
549 AudioBufferList
*buffer_list
;
551 /* get current stream format */
552 size
= sizeof(AudioStreamBasicDescription
);
553 err
= AudioDeviceGetProperty(u
->device_id
, 0, section
, kAudioDevicePropertyStreamFormat
, &size
, &u
->stream_description
);
555 /* no appropriate streams found - silently bail. */
559 if (u
->stream_description
.mFormatID
!= kAudioFormatLinearPCM
) {
560 pa_log("Unsupported audio format '%c%c%c%c'",
561 (char) (u
->stream_description
.mFormatID
>> 24),
562 (char) (u
->stream_description
.mFormatID
>> 16) & 0xff,
563 (char) (u
->stream_description
.mFormatID
>> 8) & 0xff,
564 (char) (u
->stream_description
.mFormatID
& 0xff));
568 /* get stream configuration */
570 err
= AudioDeviceGetPropertyInfo(u
->device_id
, 0, section
, kAudioDevicePropertyStreamConfiguration
, &size
, NULL
);
572 pa_log("Failed to get kAudioDevicePropertyStreamConfiguration (%s).", direction_in
? "input" : "output");
579 buffer_list
= (AudioBufferList
*) pa_xmalloc(size
);
580 err
= AudioDeviceGetProperty(u
->device_id
, 0, section
, kAudioDevicePropertyStreamConfiguration
, &size
, buffer_list
);
583 pa_log_debug("Sample rate: %f", u
->stream_description
.mSampleRate
);
584 pa_log_debug("%d bytes per packet", (unsigned int) u
->stream_description
.mBytesPerPacket
);
585 pa_log_debug("%d frames per packet", (unsigned int) u
->stream_description
.mFramesPerPacket
);
586 pa_log_debug("%d bytes per frame", (unsigned int) u
->stream_description
.mBytesPerFrame
);
587 pa_log_debug("%d channels per frame", (unsigned int) u
->stream_description
.mChannelsPerFrame
);
588 pa_log_debug("%d bits per channel", (unsigned int) u
->stream_description
.mBitsPerChannel
);
590 for (channel_idx
= 0, i
= 0; i
< buffer_list
->mNumberBuffers
; i
++) {
591 AudioBuffer
*buf
= buffer_list
->mBuffers
+ i
;
594 ca_device_create_source(m
, buf
, channel_idx
);
596 ca_device_create_sink(m
, buf
, channel_idx
);
598 channel_idx
+= buf
->mNumberChannels
;
602 pa_xfree(buffer_list
);
606 static void thread_func(void *userdata
) {
607 struct userdata
*u
= userdata
;
610 pa_assert(u
->module
);
611 pa_assert(u
->module
->core
);
613 pa_log_debug("Thread starting up");
615 if (u
->module
->core
->realtime_scheduling
)
616 pa_make_realtime(u
->module
->core
->realtime_priority
);
618 pa_thread_mq_install(&u
->thread_mq
);
623 ret
= pa_rtpoll_run(u
->rtpoll
, TRUE
);
633 /* If this was no regular exit from the loop we have to continue
634 * processing messages until we received PA_MESSAGE_SHUTDOWN */
635 pa_asyncmsgq_post(u
->thread_mq
.outq
, PA_MSGOBJECT(u
->module
->core
), PA_CORE_MESSAGE_UNLOAD_MODULE
, u
->module
, 0, NULL
, NULL
);
636 pa_asyncmsgq_wait_for(u
->thread_mq
.inq
, PA_MESSAGE_SHUTDOWN
);
639 pa_log_debug("Thread shutting down");
642 int pa__init(pa_module
*m
) {
645 struct userdata
*u
= NULL
;
646 pa_modargs
*ma
= NULL
;
648 pa_card_new_data card_new_data
;
649 coreaudio_sink
*ca_sink
;
650 coreaudio_source
*ca_source
;
654 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
655 pa_log("Failed to parse module arguments.");
659 u
= pa_xnew0(struct userdata
, 1);
663 if (pa_modargs_get_value_u32(ma
, "device_id", (unsigned int *) &u
->device_id
) != 0) {
664 pa_log("Failed to parse device_id argument.");
668 /* get device product name */
670 err
= AudioDeviceGetProperty(u
->device_id
, 0, 0, kAudioDevicePropertyDeviceName
, &size
, &tmp
);
672 pa_log("Failed to get kAudioDevicePropertyDeviceName (err = %08x).", (int) err
);
676 u
->device_name
= pa_xstrdup(tmp
);
678 pa_card_new_data_init(&card_new_data
);
679 pa_proplist_sets(card_new_data
.proplist
, PA_PROP_DEVICE_STRING
, tmp
);
680 card_new_data
.driver
= __FILE__
;
681 pa_card_new_data_set_name(&card_new_data
, tmp
);
682 pa_log_info("Initializing module for CoreAudio device '%s' (id %d)", tmp
, (unsigned int) u
->device_id
);
684 /* get device vendor name (may fail) */
686 err
= AudioDeviceGetProperty(u
->device_id
, 0, 0, kAudioDevicePropertyDeviceManufacturer
, &size
, &tmp
);
688 u
->vendor_name
= pa_xstrdup(tmp
);
690 /* create the card object */
691 u
->card
= pa_card_new(m
->core
, &card_new_data
);
693 pa_log("Unable to create card.\n");
697 pa_card_new_data_done(&card_new_data
);
698 u
->card
->userdata
= u
;
700 u
->rtpoll
= pa_rtpoll_new();
701 pa_thread_mq_init(&u
->thread_mq
, m
->core
->mainloop
, u
->rtpoll
);
702 u
->async_msgq
= pa_asyncmsgq_new(0);
703 pa_rtpoll_item_new_asyncmsgq_read(u
->rtpoll
, PA_RTPOLL_EARLY
-1, u
->async_msgq
);
705 PA_LLIST_HEAD_INIT(coreaudio_sink
, u
->sinks
);
708 ca_device_create_streams(m
, FALSE
);
711 ca_device_create_streams(m
, TRUE
);
713 /* create the message thread */
714 if (!(u
->thread
= pa_thread_new(thread_func
, u
))) {
715 pa_log("Failed to create thread.");
719 /* register notification callback for stream format changes */
720 AudioDeviceAddPropertyListener(u
->device_id
, 0, 0, kAudioDevicePropertyStreamFormat
, ca_stream_format_changed
, u
);
722 /* set number of frames in IOProc */
723 frames
= DEFAULT_FRAMES_PER_IOPROC
;
724 pa_modargs_get_value_u32(ma
, "ioproc_frames", (unsigned int *) &frames
);
726 AudioDeviceSetProperty(u
->device_id
, NULL
, 0, 0, kAudioDevicePropertyBufferFrameSize
, sizeof(frames
), &frames
);
727 pa_log_debug("%u frames per IOProc\n", (unsigned int) frames
);
729 /* create one ioproc for both directions */
730 err
= AudioDeviceCreateIOProcID(u
->device_id
, io_render_proc
, u
, &u
->proc_id
);
732 pa_log("AudioDeviceCreateIOProcID() failed (err = %08x\n).", (int) err
);
736 for (ca_sink
= u
->sinks
; ca_sink
; ca_sink
= ca_sink
->next
)
737 pa_sink_put(ca_sink
->pa_sink
);
739 for (ca_source
= u
->sources
; ca_source
; ca_source
= ca_source
->next
)
740 pa_source_put(ca_source
->pa_source
);
756 void pa__done(pa_module
*m
) {
758 coreaudio_sink
*ca_sink
;
759 coreaudio_source
*ca_source
;
767 for (ca_sink
= u
->sinks
; ca_sink
; ca_sink
= ca_sink
->next
)
768 if (ca_sink
->pa_sink
)
769 pa_sink_unlink(ca_sink
->pa_sink
);
772 for (ca_source
= u
->sources
; ca_source
; ca_source
= ca_source
->next
)
773 if (ca_source
->pa_source
)
774 pa_source_unlink(ca_source
->pa_source
);
777 pa_asyncmsgq_send(u
->thread_mq
.inq
, NULL
, PA_MESSAGE_SHUTDOWN
, NULL
, 0, NULL
);
778 pa_thread_free(u
->thread
);
779 pa_thread_mq_done(&u
->thread_mq
);
780 pa_asyncmsgq_unref(u
->async_msgq
);
784 for (ca_sink
= u
->sinks
; ca_sink
;) {
785 coreaudio_sink
*next
= ca_sink
->next
;
787 if (ca_sink
->pa_sink
)
788 pa_sink_unref(ca_sink
->pa_sink
);
790 pa_xfree(ca_sink
->name
);
796 for (ca_source
= u
->sources
; ca_source
;) {
797 coreaudio_source
*next
= ca_source
->next
;
799 if (ca_source
->pa_source
)
800 pa_source_unref(ca_source
->pa_source
);
802 pa_xfree(ca_source
->name
);
808 AudioDeviceStop(u
->device_id
, u
->proc_id
);
809 AudioDeviceDestroyIOProcID(u
->device_id
, u
->proc_id
);
812 AudioDeviceRemovePropertyListener(u
->device_id
, 0, 0, kAudioDevicePropertyStreamFormat
, ca_stream_format_changed
);
814 pa_xfree(u
->device_name
);
815 pa_xfree(u
->vendor_name
);
816 pa_rtpoll_free(u
->rtpoll
);
817 pa_card_free(u
->card
);