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 <CoreAudio/CoreAudio.h>
26 #include <string.h> /* strerror */
27 #include <pthread.h> /* pthread_X */
29 #include "qemu-common.h"
32 #define AUDIO_CAP "coreaudio"
33 #include "audio_int.h"
45 typedef struct coreaudioVoiceOut
{
47 pthread_mutex_t mutex
;
49 AudioDeviceID outputDeviceID
;
50 UInt32 audioDevicePropertyBufferFrameSize
;
51 AudioStreamBasicDescription outputStreamBasicDescription
;
57 static void coreaudio_logstatus (OSStatus status
)
59 const char *str
= "BUG";
62 case kAudioHardwareNoError
:
63 str
= "kAudioHardwareNoError";
66 case kAudioHardwareNotRunningError
:
67 str
= "kAudioHardwareNotRunningError";
70 case kAudioHardwareUnspecifiedError
:
71 str
= "kAudioHardwareUnspecifiedError";
74 case kAudioHardwareUnknownPropertyError
:
75 str
= "kAudioHardwareUnknownPropertyError";
78 case kAudioHardwareBadPropertySizeError
:
79 str
= "kAudioHardwareBadPropertySizeError";
82 case kAudioHardwareIllegalOperationError
:
83 str
= "kAudioHardwareIllegalOperationError";
86 case kAudioHardwareBadDeviceError
:
87 str
= "kAudioHardwareBadDeviceError";
90 case kAudioHardwareBadStreamError
:
91 str
= "kAudioHardwareBadStreamError";
94 case kAudioHardwareUnsupportedOperationError
:
95 str
= "kAudioHardwareUnsupportedOperationError";
98 case kAudioDeviceUnsupportedFormatError
:
99 str
= "kAudioDeviceUnsupportedFormatError";
102 case kAudioDevicePermissionsError
:
103 str
= "kAudioDevicePermissionsError";
107 AUD_log (AUDIO_CAP
, "Reason: status code %" PRId32
"\n", (int32_t)status
);
111 AUD_log (AUDIO_CAP
, "Reason: %s\n", str
);
114 static void GCC_FMT_ATTR (2, 3) coreaudio_logerr (
123 AUD_log (AUDIO_CAP
, fmt
, ap
);
126 coreaudio_logstatus (status
);
129 static void GCC_FMT_ATTR (3, 4) coreaudio_logerr2 (
138 AUD_log (AUDIO_CAP
, "Could not initialize %s\n", typ
);
141 AUD_vlog (AUDIO_CAP
, fmt
, ap
);
144 coreaudio_logstatus (status
);
147 static inline UInt32
isPlaying (AudioDeviceID outputDeviceID
)
151 UInt32 propertySize
= sizeof(outputDeviceID
);
152 status
= AudioDeviceGetProperty(
153 outputDeviceID
, 0, 0,
154 kAudioDevicePropertyDeviceIsRunning
, &propertySize
, &result
);
155 if (status
!= kAudioHardwareNoError
) {
156 coreaudio_logerr(status
,
157 "Could not determine whether Device is playing\n");
162 static void coreaudio_atexit (void)
167 static int coreaudio_lock (coreaudioVoiceOut
*core
, const char *fn_name
)
171 err
= pthread_mutex_lock (&core
->mutex
);
173 dolog ("Could not lock voice for %s\nReason: %s\n",
174 fn_name
, strerror (err
));
180 static int coreaudio_unlock (coreaudioVoiceOut
*core
, const char *fn_name
)
184 err
= pthread_mutex_unlock (&core
->mutex
);
186 dolog ("Could not unlock voice for %s\nReason: %s\n",
187 fn_name
, strerror (err
));
193 static int coreaudio_run_out (HWVoiceOut
*hw
, int live
)
196 coreaudioVoiceOut
*core
= (coreaudioVoiceOut
*) hw
;
198 if (coreaudio_lock (core
, "coreaudio_run_out")) {
202 if (core
->decr
> live
) {
203 ldebug ("core->decr %d live %d core->live %d\n",
209 decr
= audio_MIN (core
->decr
, live
);
212 core
->live
= live
- decr
;
213 hw
->rpos
= core
->rpos
;
215 coreaudio_unlock (core
, "coreaudio_run_out");
219 /* callback to feed audiooutput buffer */
220 static OSStatus
audioDeviceIOProc(
221 AudioDeviceID inDevice
,
222 const AudioTimeStamp
* inNow
,
223 const AudioBufferList
* inInputData
,
224 const AudioTimeStamp
* inInputTime
,
225 AudioBufferList
* outOutputData
,
226 const AudioTimeStamp
* inOutputTime
,
229 UInt32 frame
, frameCount
;
230 float *out
= outOutputData
->mBuffers
[0].mData
;
231 HWVoiceOut
*hw
= hwptr
;
232 coreaudioVoiceOut
*core
= (coreaudioVoiceOut
*) hwptr
;
234 struct st_sample
*src
;
237 const float scale
= 1.f
/ UINT_MAX
;
239 const float scale
= UINT_MAX
;
243 if (coreaudio_lock (core
, "audioDeviceIOProc")) {
248 frameCount
= core
->audioDevicePropertyBufferFrameSize
;
251 /* if there are not enough samples, set signal and return */
252 if (live
< frameCount
) {
254 coreaudio_unlock (core
, "audioDeviceIOProc(empty)");
259 src
= hw
->mix_buf
+ rpos
;
262 for (frame
= 0; frame
< frameCount
; frame
++) {
264 *out
++ = src
[frame
].l
; /* left channel */
265 *out
++ = src
[frame
].r
; /* right channel */
268 *out
++ = src
[frame
].l
* scale
; /* left channel */
269 *out
++ = src
[frame
].r
* scale
; /* right channel */
271 *out
++ = src
[frame
].l
/ scale
; /* left channel */
272 *out
++ = src
[frame
].r
/ scale
; /* right channel */
277 rpos
= (rpos
+ frameCount
) % hw
->samples
;
278 core
->decr
+= frameCount
;
281 coreaudio_unlock (core
, "audioDeviceIOProc");
285 static int coreaudio_write (SWVoiceOut
*sw
, void *buf
, int len
)
287 return audio_pcm_sw_write (sw
, buf
, len
);
290 static int coreaudio_init_out(HWVoiceOut
*hw
, struct audsettings
*as
,
294 coreaudioVoiceOut
*core
= (coreaudioVoiceOut
*) hw
;
297 const char *typ
= "playback";
298 AudioValueRange frameRange
;
301 err
= pthread_mutex_init(&core
->mutex
, NULL
);
303 dolog("Could not create mutex\nReason: %s\n", strerror (err
));
307 audio_pcm_init_info (&hw
->info
, as
);
309 /* open default output device */
310 propertySize
= sizeof(core
->outputDeviceID
);
311 status
= AudioHardwareGetProperty(
312 kAudioHardwarePropertyDefaultOutputDevice
,
314 &core
->outputDeviceID
);
315 if (status
!= kAudioHardwareNoError
) {
316 coreaudio_logerr2 (status
, typ
,
317 "Could not get default output Device\n");
320 if (core
->outputDeviceID
== kAudioDeviceUnknown
) {
321 dolog ("Could not initialize %s - Unknown Audiodevice\n", typ
);
325 /* get minimum and maximum buffer frame sizes */
326 propertySize
= sizeof(frameRange
);
327 status
= AudioDeviceGetProperty(
328 core
->outputDeviceID
,
331 kAudioDevicePropertyBufferFrameSizeRange
,
334 if (status
!= kAudioHardwareNoError
) {
335 coreaudio_logerr2 (status
, typ
,
336 "Could not get device buffer frame range\n");
340 if (frameRange
.mMinimum
> conf
.buffer_frames
) {
341 core
->audioDevicePropertyBufferFrameSize
= (UInt32
) frameRange
.mMinimum
;
342 dolog ("warning: Upsizing Buffer Frames to %f\n", frameRange
.mMinimum
);
344 else if (frameRange
.mMaximum
< conf
.buffer_frames
) {
345 core
->audioDevicePropertyBufferFrameSize
= (UInt32
) frameRange
.mMaximum
;
346 dolog ("warning: Downsizing Buffer Frames to %f\n", frameRange
.mMaximum
);
349 core
->audioDevicePropertyBufferFrameSize
= conf
.buffer_frames
;
352 /* set Buffer Frame Size */
353 propertySize
= sizeof(core
->audioDevicePropertyBufferFrameSize
);
354 status
= AudioDeviceSetProperty(
355 core
->outputDeviceID
,
359 kAudioDevicePropertyBufferFrameSize
,
361 &core
->audioDevicePropertyBufferFrameSize
);
362 if (status
!= kAudioHardwareNoError
) {
363 coreaudio_logerr2 (status
, typ
,
364 "Could not set device buffer frame size %" PRIu32
"\n",
365 (uint32_t)core
->audioDevicePropertyBufferFrameSize
);
369 /* get Buffer Frame Size */
370 propertySize
= sizeof(core
->audioDevicePropertyBufferFrameSize
);
371 status
= AudioDeviceGetProperty(
372 core
->outputDeviceID
,
375 kAudioDevicePropertyBufferFrameSize
,
377 &core
->audioDevicePropertyBufferFrameSize
);
378 if (status
!= kAudioHardwareNoError
) {
379 coreaudio_logerr2 (status
, typ
,
380 "Could not get device buffer frame size\n");
383 hw
->samples
= conf
.nbuffers
* core
->audioDevicePropertyBufferFrameSize
;
385 /* get StreamFormat */
386 propertySize
= sizeof(core
->outputStreamBasicDescription
);
387 status
= AudioDeviceGetProperty(
388 core
->outputDeviceID
,
391 kAudioDevicePropertyStreamFormat
,
393 &core
->outputStreamBasicDescription
);
394 if (status
!= kAudioHardwareNoError
) {
395 coreaudio_logerr2 (status
, typ
,
396 "Could not get Device Stream properties\n");
397 core
->outputDeviceID
= kAudioDeviceUnknown
;
402 core
->outputStreamBasicDescription
.mSampleRate
= (Float64
) as
->freq
;
403 propertySize
= sizeof(core
->outputStreamBasicDescription
);
404 status
= AudioDeviceSetProperty(
405 core
->outputDeviceID
,
409 kAudioDevicePropertyStreamFormat
,
411 &core
->outputStreamBasicDescription
);
412 if (status
!= kAudioHardwareNoError
) {
413 coreaudio_logerr2 (status
, typ
, "Could not set samplerate %d\n",
415 core
->outputDeviceID
= kAudioDeviceUnknown
;
420 status
= AudioDeviceAddIOProc(core
->outputDeviceID
, audioDeviceIOProc
, hw
);
421 if (status
!= kAudioHardwareNoError
) {
422 coreaudio_logerr2 (status
, typ
, "Could not set IOProc\n");
423 core
->outputDeviceID
= kAudioDeviceUnknown
;
428 if (!isPlaying(core
->outputDeviceID
)) {
429 status
= AudioDeviceStart(core
->outputDeviceID
, audioDeviceIOProc
);
430 if (status
!= kAudioHardwareNoError
) {
431 coreaudio_logerr2 (status
, typ
, "Could not start playback\n");
432 AudioDeviceRemoveIOProc(core
->outputDeviceID
, audioDeviceIOProc
);
433 core
->outputDeviceID
= kAudioDeviceUnknown
;
441 static void coreaudio_fini_out (HWVoiceOut
*hw
)
445 coreaudioVoiceOut
*core
= (coreaudioVoiceOut
*) hw
;
447 if (!conf
.isAtexit
) {
449 if (isPlaying(core
->outputDeviceID
)) {
450 status
= AudioDeviceStop(core
->outputDeviceID
, audioDeviceIOProc
);
451 if (status
!= kAudioHardwareNoError
) {
452 coreaudio_logerr (status
, "Could not stop playback\n");
456 /* remove callback */
457 status
= AudioDeviceRemoveIOProc(core
->outputDeviceID
,
459 if (status
!= kAudioHardwareNoError
) {
460 coreaudio_logerr (status
, "Could not remove IOProc\n");
463 core
->outputDeviceID
= kAudioDeviceUnknown
;
466 err
= pthread_mutex_destroy(&core
->mutex
);
468 dolog("Could not destroy mutex\nReason: %s\n", strerror (err
));
472 static int coreaudio_ctl_out (HWVoiceOut
*hw
, int cmd
, ...)
475 coreaudioVoiceOut
*core
= (coreaudioVoiceOut
*) hw
;
480 if (!isPlaying(core
->outputDeviceID
)) {
481 status
= AudioDeviceStart(core
->outputDeviceID
, audioDeviceIOProc
);
482 if (status
!= kAudioHardwareNoError
) {
483 coreaudio_logerr (status
, "Could not resume playback\n");
490 if (!conf
.isAtexit
) {
491 if (isPlaying(core
->outputDeviceID
)) {
492 status
= AudioDeviceStop(core
->outputDeviceID
, audioDeviceIOProc
);
493 if (status
!= kAudioHardwareNoError
) {
494 coreaudio_logerr (status
, "Could not pause playback\n");
503 static void *coreaudio_audio_init (void)
505 atexit(coreaudio_atexit
);
506 return &coreaudio_audio_init
;
509 static void coreaudio_audio_fini (void *opaque
)
514 static struct audio_option coreaudio_options
[] = {
516 .name
= "BUFFER_SIZE",
518 .valp
= &conf
.buffer_frames
,
519 .descr
= "Size of the buffer in frames"
522 .name
= "BUFFER_COUNT",
524 .valp
= &conf
.nbuffers
,
525 .descr
= "Number of buffers"
527 { /* End of list */ }
530 static struct audio_pcm_ops coreaudio_pcm_ops
= {
531 .init_out
= coreaudio_init_out
,
532 .fini_out
= coreaudio_fini_out
,
533 .run_out
= coreaudio_run_out
,
534 .write
= coreaudio_write
,
535 .ctl_out
= coreaudio_ctl_out
538 struct audio_driver coreaudio_audio_driver
= {
540 .descr
= "CoreAudio http://developer.apple.com/audio/coreaudio.html",
541 .options
= coreaudio_options
,
542 .init
= coreaudio_audio_init
,
543 .fini
= coreaudio_audio_fini
,
544 .pcm_ops
= &coreaudio_pcm_ops
,
548 .voice_size_out
= sizeof (coreaudioVoiceOut
),