2 * QEMU OS X CoreAudio audio driver
4 * Copyright (c) 2005 Mike Kronenberg
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 #include "qemu/osdep.h"
26 #include <CoreAudio/CoreAudio.h>
27 #include <pthread.h> /* pthread_X */
29 #include "qemu/module.h"
32 #define AUDIO_CAP "coreaudio"
33 #include "audio_int.h"
35 typedef struct coreaudioVoiceOut
{
37 pthread_mutex_t mutex
;
38 AudioDeviceID outputDeviceID
;
41 UInt32 audioDevicePropertyBufferFrameSize
;
42 AudioStreamBasicDescription outputStreamBasicDescription
;
43 AudioDeviceIOProcID ioprocid
;
47 static const AudioObjectPropertyAddress voice_addr
= {
48 kAudioHardwarePropertyDefaultOutputDevice
,
49 kAudioObjectPropertyScopeGlobal
,
50 kAudioObjectPropertyElementMaster
53 static OSStatus
coreaudio_get_voice(AudioDeviceID
*id
)
55 UInt32 size
= sizeof(*id
);
57 return AudioObjectGetPropertyData(kAudioObjectSystemObject
,
65 static OSStatus
coreaudio_get_framesizerange(AudioDeviceID id
,
66 AudioValueRange
*framerange
)
68 UInt32 size
= sizeof(*framerange
);
69 AudioObjectPropertyAddress addr
= {
70 kAudioDevicePropertyBufferFrameSizeRange
,
71 kAudioDevicePropertyScopeOutput
,
72 kAudioObjectPropertyElementMaster
75 return AudioObjectGetPropertyData(id
,
83 static OSStatus
coreaudio_get_framesize(AudioDeviceID id
, UInt32
*framesize
)
85 UInt32 size
= sizeof(*framesize
);
86 AudioObjectPropertyAddress addr
= {
87 kAudioDevicePropertyBufferFrameSize
,
88 kAudioDevicePropertyScopeOutput
,
89 kAudioObjectPropertyElementMaster
92 return AudioObjectGetPropertyData(id
,
100 static OSStatus
coreaudio_set_framesize(AudioDeviceID id
, UInt32
*framesize
)
102 UInt32 size
= sizeof(*framesize
);
103 AudioObjectPropertyAddress addr
= {
104 kAudioDevicePropertyBufferFrameSize
,
105 kAudioDevicePropertyScopeOutput
,
106 kAudioObjectPropertyElementMaster
109 return AudioObjectSetPropertyData(id
,
117 static OSStatus
coreaudio_get_streamformat(AudioDeviceID id
,
118 AudioStreamBasicDescription
*d
)
120 UInt32 size
= sizeof(*d
);
121 AudioObjectPropertyAddress addr
= {
122 kAudioDevicePropertyStreamFormat
,
123 kAudioDevicePropertyScopeOutput
,
124 kAudioObjectPropertyElementMaster
127 return AudioObjectGetPropertyData(id
,
135 static OSStatus
coreaudio_set_streamformat(AudioDeviceID id
,
136 AudioStreamBasicDescription
*d
)
138 UInt32 size
= sizeof(*d
);
139 AudioObjectPropertyAddress addr
= {
140 kAudioDevicePropertyStreamFormat
,
141 kAudioDevicePropertyScopeOutput
,
142 kAudioObjectPropertyElementMaster
145 return AudioObjectSetPropertyData(id
,
153 static OSStatus
coreaudio_get_isrunning(AudioDeviceID id
, UInt32
*result
)
155 UInt32 size
= sizeof(*result
);
156 AudioObjectPropertyAddress addr
= {
157 kAudioDevicePropertyDeviceIsRunning
,
158 kAudioDevicePropertyScopeOutput
,
159 kAudioObjectPropertyElementMaster
162 return AudioObjectGetPropertyData(id
,
170 static void coreaudio_logstatus (OSStatus status
)
172 const char *str
= "BUG";
175 case kAudioHardwareNoError
:
176 str
= "kAudioHardwareNoError";
179 case kAudioHardwareNotRunningError
:
180 str
= "kAudioHardwareNotRunningError";
183 case kAudioHardwareUnspecifiedError
:
184 str
= "kAudioHardwareUnspecifiedError";
187 case kAudioHardwareUnknownPropertyError
:
188 str
= "kAudioHardwareUnknownPropertyError";
191 case kAudioHardwareBadPropertySizeError
:
192 str
= "kAudioHardwareBadPropertySizeError";
195 case kAudioHardwareIllegalOperationError
:
196 str
= "kAudioHardwareIllegalOperationError";
199 case kAudioHardwareBadDeviceError
:
200 str
= "kAudioHardwareBadDeviceError";
203 case kAudioHardwareBadStreamError
:
204 str
= "kAudioHardwareBadStreamError";
207 case kAudioHardwareUnsupportedOperationError
:
208 str
= "kAudioHardwareUnsupportedOperationError";
211 case kAudioDeviceUnsupportedFormatError
:
212 str
= "kAudioDeviceUnsupportedFormatError";
215 case kAudioDevicePermissionsError
:
216 str
= "kAudioDevicePermissionsError";
220 AUD_log (AUDIO_CAP
, "Reason: status code %" PRId32
"\n", (int32_t)status
);
224 AUD_log (AUDIO_CAP
, "Reason: %s\n", str
);
227 static void GCC_FMT_ATTR (2, 3) coreaudio_logerr (
236 AUD_log (AUDIO_CAP
, fmt
, ap
);
239 coreaudio_logstatus (status
);
242 static void GCC_FMT_ATTR (3, 4) coreaudio_logerr2 (
251 AUD_log (AUDIO_CAP
, "Could not initialize %s\n", typ
);
254 AUD_vlog (AUDIO_CAP
, fmt
, ap
);
257 coreaudio_logstatus (status
);
260 #define coreaudio_playback_logerr(status, ...) \
261 coreaudio_logerr2(status, "playback", __VA_ARGS__)
263 static int coreaudio_lock (coreaudioVoiceOut
*core
, const char *fn_name
)
267 err
= pthread_mutex_lock (&core
->mutex
);
269 dolog ("Could not lock voice for %s\nReason: %s\n",
270 fn_name
, strerror (err
));
276 static int coreaudio_unlock (coreaudioVoiceOut
*core
, const char *fn_name
)
280 err
= pthread_mutex_unlock (&core
->mutex
);
282 dolog ("Could not unlock voice for %s\nReason: %s\n",
283 fn_name
, strerror (err
));
289 #define COREAUDIO_WRAPPER_FUNC(name, ret_type, args_decl, args) \
290 static ret_type glue(coreaudio_, name)args_decl \
292 coreaudioVoiceOut *core = (coreaudioVoiceOut *) hw; \
295 if (coreaudio_lock(core, "coreaudio_" #name)) { \
299 ret = glue(audio_generic_, name)args; \
301 coreaudio_unlock(core, "coreaudio_" #name); \
304 COREAUDIO_WRAPPER_FUNC(get_buffer_out
, void *, (HWVoiceOut
*hw
, size_t *size
),
306 COREAUDIO_WRAPPER_FUNC(put_buffer_out
, size_t,
307 (HWVoiceOut
*hw
, void *buf
, size_t size
),
309 COREAUDIO_WRAPPER_FUNC(write
, size_t, (HWVoiceOut
*hw
, void *buf
, size_t size
),
311 #undef COREAUDIO_WRAPPER_FUNC
313 /* callback to feed audiooutput buffer */
314 static OSStatus
audioDeviceIOProc(
315 AudioDeviceID inDevice
,
316 const AudioTimeStamp
*inNow
,
317 const AudioBufferList
*inInputData
,
318 const AudioTimeStamp
*inInputTime
,
319 AudioBufferList
*outOutputData
,
320 const AudioTimeStamp
*inOutputTime
,
323 UInt32 frameCount
, pending_frames
;
324 void *out
= outOutputData
->mBuffers
[0].mData
;
325 HWVoiceOut
*hw
= hwptr
;
326 coreaudioVoiceOut
*core
= (coreaudioVoiceOut
*) hwptr
;
329 if (coreaudio_lock (core
, "audioDeviceIOProc")) {
334 if (inDevice
!= core
->outputDeviceID
) {
335 coreaudio_unlock (core
, "audioDeviceIOProc(old device)");
339 frameCount
= core
->audioDevicePropertyBufferFrameSize
;
340 pending_frames
= hw
->pending_emul
/ hw
->info
.bytes_per_frame
;
342 /* if there are not enough samples, set signal and return */
343 if (pending_frames
< frameCount
) {
345 coreaudio_unlock (core
, "audioDeviceIOProc(empty)");
349 len
= frameCount
* hw
->info
.bytes_per_frame
;
352 ssize_t start
= ((ssize_t
) hw
->pos_emul
) - hw
->pending_emul
;
354 start
+= hw
->size_emul
;
356 assert(start
>= 0 && start
< hw
->size_emul
);
358 write_len
= MIN(MIN(hw
->pending_emul
, len
),
359 hw
->size_emul
- start
);
361 memcpy(out
, hw
->buf_emul
+ start
, write_len
);
362 hw
->pending_emul
-= write_len
;
367 coreaudio_unlock (core
, "audioDeviceIOProc");
371 static OSStatus
init_out_device(coreaudioVoiceOut
*core
)
374 AudioValueRange frameRange
;
376 status
= coreaudio_get_voice(&core
->outputDeviceID
);
377 if (status
!= kAudioHardwareNoError
) {
378 coreaudio_playback_logerr (status
,
379 "Could not get default output Device\n");
382 if (core
->outputDeviceID
== kAudioDeviceUnknown
) {
383 dolog ("Could not initialize playback - Unknown Audiodevice\n");
387 /* get minimum and maximum buffer frame sizes */
388 status
= coreaudio_get_framesizerange(core
->outputDeviceID
,
390 if (status
== kAudioHardwareBadObjectError
) {
393 if (status
!= kAudioHardwareNoError
) {
394 coreaudio_playback_logerr (status
,
395 "Could not get device buffer frame range\n");
399 if (frameRange
.mMinimum
> core
->frameSizeSetting
) {
400 core
->audioDevicePropertyBufferFrameSize
= (UInt32
) frameRange
.mMinimum
;
401 dolog ("warning: Upsizing Buffer Frames to %f\n", frameRange
.mMinimum
);
402 } else if (frameRange
.mMaximum
< core
->frameSizeSetting
) {
403 core
->audioDevicePropertyBufferFrameSize
= (UInt32
) frameRange
.mMaximum
;
404 dolog ("warning: Downsizing Buffer Frames to %f\n", frameRange
.mMaximum
);
406 core
->audioDevicePropertyBufferFrameSize
= core
->frameSizeSetting
;
409 /* set Buffer Frame Size */
410 status
= coreaudio_set_framesize(core
->outputDeviceID
,
411 &core
->audioDevicePropertyBufferFrameSize
);
412 if (status
== kAudioHardwareBadObjectError
) {
415 if (status
!= kAudioHardwareNoError
) {
416 coreaudio_playback_logerr (status
,
417 "Could not set device buffer frame size %" PRIu32
"\n",
418 (uint32_t)core
->audioDevicePropertyBufferFrameSize
);
422 /* get Buffer Frame Size */
423 status
= coreaudio_get_framesize(core
->outputDeviceID
,
424 &core
->audioDevicePropertyBufferFrameSize
);
425 if (status
== kAudioHardwareBadObjectError
) {
428 if (status
!= kAudioHardwareNoError
) {
429 coreaudio_playback_logerr (status
,
430 "Could not get device buffer frame size\n");
433 core
->hw
.samples
= core
->bufferCount
* core
->audioDevicePropertyBufferFrameSize
;
435 /* get StreamFormat */
436 status
= coreaudio_get_streamformat(core
->outputDeviceID
,
437 &core
->outputStreamBasicDescription
);
438 if (status
== kAudioHardwareBadObjectError
) {
441 if (status
!= kAudioHardwareNoError
) {
442 coreaudio_playback_logerr (status
,
443 "Could not get Device Stream properties\n");
444 core
->outputDeviceID
= kAudioDeviceUnknown
;
449 status
= coreaudio_set_streamformat(core
->outputDeviceID
,
450 &core
->outputStreamBasicDescription
);
451 if (status
== kAudioHardwareBadObjectError
) {
454 if (status
!= kAudioHardwareNoError
) {
455 coreaudio_playback_logerr (status
,
456 "Could not set samplerate %lf\n",
457 core
->outputStreamBasicDescription
.mSampleRate
);
458 core
->outputDeviceID
= kAudioDeviceUnknown
;
463 core
->ioprocid
= NULL
;
464 status
= AudioDeviceCreateIOProcID(core
->outputDeviceID
,
468 if (status
== kAudioHardwareBadDeviceError
) {
471 if (status
!= kAudioHardwareNoError
|| core
->ioprocid
== NULL
) {
472 coreaudio_playback_logerr (status
, "Could not set IOProc\n");
473 core
->outputDeviceID
= kAudioDeviceUnknown
;
480 static void fini_out_device(coreaudioVoiceOut
*core
)
486 status
= coreaudio_get_isrunning(core
->outputDeviceID
, &isrunning
);
487 if (status
!= kAudioHardwareBadObjectError
) {
488 if (status
!= kAudioHardwareNoError
) {
489 coreaudio_logerr(status
,
490 "Could not determine whether Device is playing\n");
494 status
= AudioDeviceStop(core
->outputDeviceID
, core
->ioprocid
);
495 if (status
!= kAudioHardwareBadDeviceError
&& status
!= kAudioHardwareNoError
) {
496 coreaudio_logerr(status
, "Could not stop playback\n");
501 /* remove callback */
502 status
= AudioDeviceDestroyIOProcID(core
->outputDeviceID
,
504 if (status
!= kAudioHardwareBadDeviceError
&& status
!= kAudioHardwareNoError
) {
505 coreaudio_logerr(status
, "Could not remove IOProc\n");
507 core
->outputDeviceID
= kAudioDeviceUnknown
;
510 static void update_device_playback_state(coreaudioVoiceOut
*core
)
515 status
= coreaudio_get_isrunning(core
->outputDeviceID
, &isrunning
);
516 if (status
!= kAudioHardwareNoError
) {
517 if (status
!= kAudioHardwareBadObjectError
) {
518 coreaudio_logerr(status
,
519 "Could not determine whether Device is playing\n");
528 status
= AudioDeviceStart(core
->outputDeviceID
, core
->ioprocid
);
529 if (status
!= kAudioHardwareBadDeviceError
&& status
!= kAudioHardwareNoError
) {
530 coreaudio_logerr (status
, "Could not resume playback\n");
536 status
= AudioDeviceStop(core
->outputDeviceID
,
538 if (status
!= kAudioHardwareBadDeviceError
&& status
!= kAudioHardwareNoError
) {
539 coreaudio_logerr(status
, "Could not pause playback\n");
545 static OSStatus
handle_voice_change(
546 AudioObjectID in_object_id
,
547 UInt32 in_number_addresses
,
548 const AudioObjectPropertyAddress
*in_addresses
,
549 void *in_client_data
)
552 coreaudioVoiceOut
*core
= in_client_data
;
554 if (coreaudio_lock(core
, __func__
)) {
558 if (core
->outputDeviceID
) {
559 fini_out_device(core
);
562 status
= init_out_device(core
);
564 update_device_playback_state(core
);
567 coreaudio_unlock (core
, __func__
);
571 static int coreaudio_init_out(HWVoiceOut
*hw
, struct audsettings
*as
,
575 coreaudioVoiceOut
*core
= (coreaudioVoiceOut
*) hw
;
577 Audiodev
*dev
= drv_opaque
;
578 AudiodevCoreaudioPerDirectionOptions
*cpdo
= dev
->u
.coreaudio
.out
;
579 struct audsettings obt_as
;
582 err
= pthread_mutex_init(&core
->mutex
, NULL
);
584 dolog("Could not create mutex\nReason: %s\n", strerror (err
));
588 if (coreaudio_lock(core
, __func__
)) {
594 as
->fmt
= AUDIO_FORMAT_F32
;
595 audio_pcm_init_info (&hw
->info
, as
);
597 core
->frameSizeSetting
= audio_buffer_frames(
598 qapi_AudiodevCoreaudioPerDirectionOptions_base(cpdo
), as
, 11610);
600 core
->bufferCount
= cpdo
->has_buffer_count
? cpdo
->buffer_count
: 4;
601 core
->outputStreamBasicDescription
.mSampleRate
= (Float64
) as
->freq
;
603 status
= AudioObjectAddPropertyListener(kAudioObjectSystemObject
,
604 &voice_addr
, handle_voice_change
,
606 if (status
!= kAudioHardwareNoError
) {
607 coreaudio_playback_logerr (status
,
608 "Could not listen to voice property change\n");
612 if (init_out_device(core
)) {
616 coreaudio_unlock(core
, __func__
);
620 status
= AudioObjectRemovePropertyListener(kAudioObjectSystemObject
,
624 if (status
!= kAudioHardwareNoError
) {
625 coreaudio_playback_logerr(status
,
626 "Could not remove voice property change listener\n");
630 coreaudio_unlock(core
, __func__
);
633 err
= pthread_mutex_destroy(&core
->mutex
);
635 dolog("Could not destroy mutex\nReason: %s\n", strerror (err
));
642 static void coreaudio_fini_out (HWVoiceOut
*hw
)
646 coreaudioVoiceOut
*core
= (coreaudioVoiceOut
*) hw
;
648 if (coreaudio_lock(core
, __func__
)) {
652 status
= AudioObjectRemovePropertyListener(kAudioObjectSystemObject
,
656 if (status
!= kAudioHardwareNoError
) {
657 coreaudio_logerr(status
, "Could not remove voice property change listener\n");
660 fini_out_device(core
);
662 coreaudio_unlock(core
, __func__
);
665 err
= pthread_mutex_destroy(&core
->mutex
);
667 dolog("Could not destroy mutex\nReason: %s\n", strerror (err
));
671 static void coreaudio_enable_out(HWVoiceOut
*hw
, bool enable
)
673 coreaudioVoiceOut
*core
= (coreaudioVoiceOut
*) hw
;
675 if (coreaudio_lock(core
, __func__
)) {
679 core
->enabled
= enable
;
680 update_device_playback_state(core
);
682 coreaudio_unlock(core
, __func__
);
685 static void *coreaudio_audio_init(Audiodev
*dev
)
690 static void coreaudio_audio_fini (void *opaque
)
694 static struct audio_pcm_ops coreaudio_pcm_ops
= {
695 .init_out
= coreaudio_init_out
,
696 .fini_out
= coreaudio_fini_out
,
697 /* wrapper for audio_generic_write */
698 .write
= coreaudio_write
,
699 /* wrapper for audio_generic_get_buffer_out */
700 .get_buffer_out
= coreaudio_get_buffer_out
,
701 /* wrapper for audio_generic_put_buffer_out */
702 .put_buffer_out
= coreaudio_put_buffer_out
,
703 .enable_out
= coreaudio_enable_out
706 static struct audio_driver coreaudio_audio_driver
= {
708 .descr
= "CoreAudio http://developer.apple.com/audio/coreaudio.html",
709 .init
= coreaudio_audio_init
,
710 .fini
= coreaudio_audio_fini
,
711 .pcm_ops
= &coreaudio_pcm_ops
,
715 .voice_size_out
= sizeof (coreaudioVoiceOut
),
719 static void register_audio_coreaudio(void)
721 audio_driver_register(&coreaudio_audio_driver
);
723 type_init(register_audio_coreaudio
);