2005-10-30 21:58:22 +03:00
|
|
|
/*
|
|
|
|
* QEMU OS X CoreAudio audio driver
|
|
|
|
*
|
|
|
|
* Copyright (c) 2005 Mike Kronenberg
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2016-01-18 20:33:52 +03:00
|
|
|
#include "qemu/osdep.h"
|
2005-10-30 21:58:22 +03:00
|
|
|
#include <CoreAudio/CoreAudio.h>
|
|
|
|
#include <pthread.h> /* pthread_X */
|
|
|
|
|
2019-05-23 17:35:07 +03:00
|
|
|
#include "qemu/module.h"
|
2007-11-17 20:35:54 +03:00
|
|
|
#include "audio.h"
|
2005-10-30 21:58:22 +03:00
|
|
|
|
|
|
|
#define AUDIO_CAP "coreaudio"
|
|
|
|
#include "audio_int.h"
|
|
|
|
|
2015-11-29 00:55:21 +03:00
|
|
|
#ifndef MAC_OS_X_VERSION_10_6
|
|
|
|
#define MAC_OS_X_VERSION_10_6 1060
|
|
|
|
#endif
|
|
|
|
|
2005-10-30 21:58:22 +03:00
|
|
|
typedef struct coreaudioVoiceOut {
|
|
|
|
HWVoiceOut hw;
|
|
|
|
pthread_mutex_t mutex;
|
|
|
|
AudioDeviceID outputDeviceID;
|
2005-11-20 19:24:09 +03:00
|
|
|
UInt32 audioDevicePropertyBufferFrameSize;
|
2005-10-30 21:58:22 +03:00
|
|
|
AudioStreamBasicDescription outputStreamBasicDescription;
|
2015-11-29 00:55:24 +03:00
|
|
|
AudioDeviceIOProcID ioprocid;
|
2005-10-30 21:58:22 +03:00
|
|
|
} coreaudioVoiceOut;
|
|
|
|
|
2015-11-29 00:55:21 +03:00
|
|
|
#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6
|
|
|
|
/* The APIs used here only become available from 10.6 */
|
|
|
|
|
|
|
|
static OSStatus coreaudio_get_voice(AudioDeviceID *id)
|
|
|
|
{
|
|
|
|
UInt32 size = sizeof(*id);
|
|
|
|
AudioObjectPropertyAddress addr = {
|
|
|
|
kAudioHardwarePropertyDefaultOutputDevice,
|
|
|
|
kAudioObjectPropertyScopeGlobal,
|
|
|
|
kAudioObjectPropertyElementMaster
|
|
|
|
};
|
|
|
|
|
|
|
|
return AudioObjectGetPropertyData(kAudioObjectSystemObject,
|
|
|
|
&addr,
|
|
|
|
0,
|
|
|
|
NULL,
|
|
|
|
&size,
|
|
|
|
id);
|
|
|
|
}
|
2015-11-29 00:55:23 +03:00
|
|
|
|
|
|
|
static OSStatus coreaudio_get_framesizerange(AudioDeviceID id,
|
|
|
|
AudioValueRange *framerange)
|
|
|
|
{
|
|
|
|
UInt32 size = sizeof(*framerange);
|
|
|
|
AudioObjectPropertyAddress addr = {
|
|
|
|
kAudioDevicePropertyBufferFrameSizeRange,
|
|
|
|
kAudioDevicePropertyScopeOutput,
|
|
|
|
kAudioObjectPropertyElementMaster
|
|
|
|
};
|
|
|
|
|
|
|
|
return AudioObjectGetPropertyData(id,
|
|
|
|
&addr,
|
|
|
|
0,
|
|
|
|
NULL,
|
|
|
|
&size,
|
|
|
|
framerange);
|
|
|
|
}
|
|
|
|
|
|
|
|
static OSStatus coreaudio_get_framesize(AudioDeviceID id, UInt32 *framesize)
|
|
|
|
{
|
|
|
|
UInt32 size = sizeof(*framesize);
|
|
|
|
AudioObjectPropertyAddress addr = {
|
|
|
|
kAudioDevicePropertyBufferFrameSize,
|
|
|
|
kAudioDevicePropertyScopeOutput,
|
|
|
|
kAudioObjectPropertyElementMaster
|
|
|
|
};
|
|
|
|
|
|
|
|
return AudioObjectGetPropertyData(id,
|
|
|
|
&addr,
|
|
|
|
0,
|
|
|
|
NULL,
|
|
|
|
&size,
|
|
|
|
framesize);
|
|
|
|
}
|
|
|
|
|
|
|
|
static OSStatus coreaudio_set_framesize(AudioDeviceID id, UInt32 *framesize)
|
|
|
|
{
|
|
|
|
UInt32 size = sizeof(*framesize);
|
|
|
|
AudioObjectPropertyAddress addr = {
|
|
|
|
kAudioDevicePropertyBufferFrameSize,
|
|
|
|
kAudioDevicePropertyScopeOutput,
|
|
|
|
kAudioObjectPropertyElementMaster
|
|
|
|
};
|
|
|
|
|
|
|
|
return AudioObjectSetPropertyData(id,
|
|
|
|
&addr,
|
|
|
|
0,
|
|
|
|
NULL,
|
|
|
|
size,
|
|
|
|
framesize);
|
|
|
|
}
|
|
|
|
|
|
|
|
static OSStatus coreaudio_get_streamformat(AudioDeviceID id,
|
|
|
|
AudioStreamBasicDescription *d)
|
|
|
|
{
|
|
|
|
UInt32 size = sizeof(*d);
|
|
|
|
AudioObjectPropertyAddress addr = {
|
|
|
|
kAudioDevicePropertyStreamFormat,
|
|
|
|
kAudioDevicePropertyScopeOutput,
|
|
|
|
kAudioObjectPropertyElementMaster
|
|
|
|
};
|
|
|
|
|
|
|
|
return AudioObjectGetPropertyData(id,
|
|
|
|
&addr,
|
|
|
|
0,
|
|
|
|
NULL,
|
|
|
|
&size,
|
|
|
|
d);
|
|
|
|
}
|
|
|
|
|
|
|
|
static OSStatus coreaudio_set_streamformat(AudioDeviceID id,
|
|
|
|
AudioStreamBasicDescription *d)
|
|
|
|
{
|
|
|
|
UInt32 size = sizeof(*d);
|
|
|
|
AudioObjectPropertyAddress addr = {
|
|
|
|
kAudioDevicePropertyStreamFormat,
|
|
|
|
kAudioDevicePropertyScopeOutput,
|
|
|
|
kAudioObjectPropertyElementMaster
|
|
|
|
};
|
|
|
|
|
|
|
|
return AudioObjectSetPropertyData(id,
|
|
|
|
&addr,
|
|
|
|
0,
|
|
|
|
NULL,
|
|
|
|
size,
|
|
|
|
d);
|
|
|
|
}
|
|
|
|
|
|
|
|
static OSStatus coreaudio_get_isrunning(AudioDeviceID id, UInt32 *result)
|
|
|
|
{
|
|
|
|
UInt32 size = sizeof(*result);
|
|
|
|
AudioObjectPropertyAddress addr = {
|
|
|
|
kAudioDevicePropertyDeviceIsRunning,
|
|
|
|
kAudioDevicePropertyScopeOutput,
|
|
|
|
kAudioObjectPropertyElementMaster
|
|
|
|
};
|
|
|
|
|
|
|
|
return AudioObjectGetPropertyData(id,
|
|
|
|
&addr,
|
|
|
|
0,
|
|
|
|
NULL,
|
|
|
|
&size,
|
|
|
|
result);
|
|
|
|
}
|
2015-11-29 00:55:21 +03:00
|
|
|
#else
|
|
|
|
/* Legacy versions of functions using deprecated APIs */
|
|
|
|
|
2015-11-29 00:55:20 +03:00
|
|
|
static OSStatus coreaudio_get_voice(AudioDeviceID *id)
|
|
|
|
{
|
|
|
|
UInt32 size = sizeof(*id);
|
|
|
|
|
|
|
|
return AudioHardwareGetProperty(
|
|
|
|
kAudioHardwarePropertyDefaultOutputDevice,
|
|
|
|
&size,
|
|
|
|
id);
|
|
|
|
}
|
|
|
|
|
2015-11-29 00:55:22 +03:00
|
|
|
static OSStatus coreaudio_get_framesizerange(AudioDeviceID id,
|
|
|
|
AudioValueRange *framerange)
|
|
|
|
{
|
|
|
|
UInt32 size = sizeof(*framerange);
|
|
|
|
|
|
|
|
return AudioDeviceGetProperty(
|
|
|
|
id,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
kAudioDevicePropertyBufferFrameSizeRange,
|
|
|
|
&size,
|
|
|
|
framerange);
|
|
|
|
}
|
|
|
|
|
|
|
|
static OSStatus coreaudio_get_framesize(AudioDeviceID id, UInt32 *framesize)
|
|
|
|
{
|
|
|
|
UInt32 size = sizeof(*framesize);
|
|
|
|
|
|
|
|
return AudioDeviceGetProperty(
|
|
|
|
id,
|
|
|
|
0,
|
|
|
|
false,
|
|
|
|
kAudioDevicePropertyBufferFrameSize,
|
|
|
|
&size,
|
|
|
|
framesize);
|
|
|
|
}
|
|
|
|
|
|
|
|
static OSStatus coreaudio_set_framesize(AudioDeviceID id, UInt32 *framesize)
|
|
|
|
{
|
|
|
|
UInt32 size = sizeof(*framesize);
|
|
|
|
|
|
|
|
return AudioDeviceSetProperty(
|
|
|
|
id,
|
|
|
|
NULL,
|
|
|
|
0,
|
|
|
|
false,
|
|
|
|
kAudioDevicePropertyBufferFrameSize,
|
|
|
|
size,
|
|
|
|
framesize);
|
|
|
|
}
|
|
|
|
|
|
|
|
static OSStatus coreaudio_get_streamformat(AudioDeviceID id,
|
|
|
|
AudioStreamBasicDescription *d)
|
|
|
|
{
|
|
|
|
UInt32 size = sizeof(*d);
|
|
|
|
|
|
|
|
return AudioDeviceGetProperty(
|
|
|
|
id,
|
|
|
|
0,
|
|
|
|
false,
|
|
|
|
kAudioDevicePropertyStreamFormat,
|
|
|
|
&size,
|
|
|
|
d);
|
|
|
|
}
|
|
|
|
|
|
|
|
static OSStatus coreaudio_set_streamformat(AudioDeviceID id,
|
|
|
|
AudioStreamBasicDescription *d)
|
|
|
|
{
|
|
|
|
UInt32 size = sizeof(*d);
|
|
|
|
|
|
|
|
return AudioDeviceSetProperty(
|
|
|
|
id,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
kAudioDevicePropertyStreamFormat,
|
|
|
|
size,
|
|
|
|
d);
|
|
|
|
}
|
|
|
|
|
|
|
|
static OSStatus coreaudio_get_isrunning(AudioDeviceID id, UInt32 *result)
|
|
|
|
{
|
|
|
|
UInt32 size = sizeof(*result);
|
|
|
|
|
|
|
|
return AudioDeviceGetProperty(
|
|
|
|
id,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
kAudioDevicePropertyDeviceIsRunning,
|
|
|
|
&size,
|
|
|
|
result);
|
|
|
|
}
|
2015-11-29 00:55:23 +03:00
|
|
|
#endif
|
2015-11-29 00:55:22 +03:00
|
|
|
|
2005-10-30 21:58:22 +03:00
|
|
|
static void coreaudio_logstatus (OSStatus status)
|
|
|
|
{
|
2011-05-27 21:22:28 +04:00
|
|
|
const char *str = "BUG";
|
2005-10-30 21:58:22 +03:00
|
|
|
|
|
|
|
switch(status) {
|
|
|
|
case kAudioHardwareNoError:
|
|
|
|
str = "kAudioHardwareNoError";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case kAudioHardwareNotRunningError:
|
|
|
|
str = "kAudioHardwareNotRunningError";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case kAudioHardwareUnspecifiedError:
|
|
|
|
str = "kAudioHardwareUnspecifiedError";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case kAudioHardwareUnknownPropertyError:
|
|
|
|
str = "kAudioHardwareUnknownPropertyError";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case kAudioHardwareBadPropertySizeError:
|
|
|
|
str = "kAudioHardwareBadPropertySizeError";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case kAudioHardwareIllegalOperationError:
|
|
|
|
str = "kAudioHardwareIllegalOperationError";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case kAudioHardwareBadDeviceError:
|
|
|
|
str = "kAudioHardwareBadDeviceError";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case kAudioHardwareBadStreamError:
|
|
|
|
str = "kAudioHardwareBadStreamError";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case kAudioHardwareUnsupportedOperationError:
|
|
|
|
str = "kAudioHardwareUnsupportedOperationError";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case kAudioDeviceUnsupportedFormatError:
|
|
|
|
str = "kAudioDeviceUnsupportedFormatError";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case kAudioDevicePermissionsError:
|
|
|
|
str = "kAudioDevicePermissionsError";
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2011-06-23 18:24:45 +04:00
|
|
|
AUD_log (AUDIO_CAP, "Reason: status code %" PRId32 "\n", (int32_t)status);
|
2005-10-30 21:58:22 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
AUD_log (AUDIO_CAP, "Reason: %s\n", str);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void GCC_FMT_ATTR (2, 3) coreaudio_logerr (
|
|
|
|
OSStatus status,
|
|
|
|
const char *fmt,
|
|
|
|
...
|
|
|
|
)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start (ap, fmt);
|
|
|
|
AUD_log (AUDIO_CAP, fmt, ap);
|
|
|
|
va_end (ap);
|
|
|
|
|
|
|
|
coreaudio_logstatus (status);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void GCC_FMT_ATTR (3, 4) coreaudio_logerr2 (
|
|
|
|
OSStatus status,
|
|
|
|
const char *typ,
|
|
|
|
const char *fmt,
|
|
|
|
...
|
|
|
|
)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
2005-11-05 21:55:28 +03:00
|
|
|
AUD_log (AUDIO_CAP, "Could not initialize %s\n", typ);
|
2005-10-30 21:58:22 +03:00
|
|
|
|
|
|
|
va_start (ap, fmt);
|
|
|
|
AUD_vlog (AUDIO_CAP, fmt, ap);
|
|
|
|
va_end (ap);
|
|
|
|
|
|
|
|
coreaudio_logstatus (status);
|
|
|
|
}
|
|
|
|
|
2005-11-20 19:24:09 +03:00
|
|
|
static inline UInt32 isPlaying (AudioDeviceID outputDeviceID)
|
|
|
|
{
|
|
|
|
OSStatus status;
|
|
|
|
UInt32 result = 0;
|
2015-11-29 00:55:22 +03:00
|
|
|
status = coreaudio_get_isrunning(outputDeviceID, &result);
|
2005-11-20 19:24:09 +03:00
|
|
|
if (status != kAudioHardwareNoError) {
|
|
|
|
coreaudio_logerr(status,
|
|
|
|
"Could not determine whether Device is playing\n");
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2005-10-30 21:58:22 +03:00
|
|
|
static int coreaudio_lock (coreaudioVoiceOut *core, const char *fn_name)
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
|
|
|
err = pthread_mutex_lock (&core->mutex);
|
|
|
|
if (err) {
|
2005-11-05 21:55:28 +03:00
|
|
|
dolog ("Could not lock voice for %s\nReason: %s\n",
|
2005-10-30 21:58:22 +03:00
|
|
|
fn_name, strerror (err));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int coreaudio_unlock (coreaudioVoiceOut *core, const char *fn_name)
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
|
|
|
err = pthread_mutex_unlock (&core->mutex);
|
|
|
|
if (err) {
|
2005-11-05 21:55:28 +03:00
|
|
|
dolog ("Could not unlock voice for %s\nReason: %s\n",
|
2005-10-30 21:58:22 +03:00
|
|
|
fn_name, strerror (err));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-09-20 00:24:11 +03:00
|
|
|
#define COREAUDIO_WRAPPER_FUNC(name, ret_type, args_decl, args) \
|
|
|
|
static ret_type glue(coreaudio_, name)args_decl \
|
|
|
|
{ \
|
|
|
|
coreaudioVoiceOut *core = (coreaudioVoiceOut *) hw; \
|
|
|
|
ret_type ret; \
|
|
|
|
\
|
|
|
|
if (coreaudio_lock(core, "coreaudio_" #name)) { \
|
|
|
|
return 0; \
|
|
|
|
} \
|
|
|
|
\
|
|
|
|
ret = glue(audio_generic_, name)args; \
|
|
|
|
\
|
|
|
|
coreaudio_unlock(core, "coreaudio_" #name); \
|
|
|
|
return ret; \
|
2005-10-30 21:58:22 +03:00
|
|
|
}
|
2019-09-20 00:24:11 +03:00
|
|
|
COREAUDIO_WRAPPER_FUNC(get_buffer_out, void *, (HWVoiceOut *hw, size_t *size),
|
|
|
|
(hw, size))
|
|
|
|
COREAUDIO_WRAPPER_FUNC(put_buffer_out_nowrite, size_t,
|
|
|
|
(HWVoiceOut *hw, void *buf, size_t size),
|
|
|
|
(hw, buf, size))
|
|
|
|
COREAUDIO_WRAPPER_FUNC(write, size_t, (HWVoiceOut *hw, void *buf, size_t size),
|
|
|
|
(hw, buf, size))
|
|
|
|
#undef COREAUDIO_WRAPPER_FUNC
|
2005-10-30 21:58:22 +03:00
|
|
|
|
|
|
|
/* callback to feed audiooutput buffer */
|
|
|
|
static OSStatus audioDeviceIOProc(
|
|
|
|
AudioDeviceID inDevice,
|
|
|
|
const AudioTimeStamp* inNow,
|
|
|
|
const AudioBufferList* inInputData,
|
|
|
|
const AudioTimeStamp* inInputTime,
|
|
|
|
AudioBufferList* outOutputData,
|
|
|
|
const AudioTimeStamp* inOutputTime,
|
|
|
|
void* hwptr)
|
|
|
|
{
|
2019-09-20 00:24:11 +03:00
|
|
|
UInt32 frameCount, pending_frames;
|
|
|
|
void *out = outOutputData->mBuffers[0].mData;
|
2005-10-30 21:58:22 +03:00
|
|
|
HWVoiceOut *hw = hwptr;
|
|
|
|
coreaudioVoiceOut *core = (coreaudioVoiceOut *) hwptr;
|
2019-09-20 00:24:11 +03:00
|
|
|
size_t len;
|
2005-10-30 21:58:22 +03:00
|
|
|
|
|
|
|
if (coreaudio_lock (core, "audioDeviceIOProc")) {
|
|
|
|
inInputTime = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-11-20 19:24:09 +03:00
|
|
|
frameCount = core->audioDevicePropertyBufferFrameSize;
|
2019-10-13 22:58:02 +03:00
|
|
|
pending_frames = hw->pending_emul / hw->info.bytes_per_frame;
|
2005-10-30 21:58:22 +03:00
|
|
|
|
|
|
|
/* if there are not enough samples, set signal and return */
|
2019-09-20 00:24:11 +03:00
|
|
|
if (pending_frames < frameCount) {
|
2005-10-30 21:58:22 +03:00
|
|
|
inInputTime = 0;
|
|
|
|
coreaudio_unlock (core, "audioDeviceIOProc(empty)");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-10-13 22:58:02 +03:00
|
|
|
len = frameCount * hw->info.bytes_per_frame;
|
2019-09-20 00:24:11 +03:00
|
|
|
while (len) {
|
|
|
|
size_t write_len;
|
|
|
|
ssize_t start = ((ssize_t) hw->pos_emul) - hw->pending_emul;
|
|
|
|
if (start < 0) {
|
|
|
|
start += hw->size_emul;
|
|
|
|
}
|
|
|
|
assert(start >= 0 && start < hw->size_emul);
|
2005-10-30 21:58:22 +03:00
|
|
|
|
2019-09-20 00:24:11 +03:00
|
|
|
write_len = MIN(MIN(hw->pending_emul, len),
|
|
|
|
hw->size_emul - start);
|
2005-10-30 21:58:22 +03:00
|
|
|
|
2019-09-20 00:24:11 +03:00
|
|
|
memcpy(out, hw->buf_emul + start, write_len);
|
|
|
|
hw->pending_emul -= write_len;
|
|
|
|
len -= write_len;
|
|
|
|
out += write_len;
|
|
|
|
}
|
2005-10-30 21:58:22 +03:00
|
|
|
|
|
|
|
coreaudio_unlock (core, "audioDeviceIOProc");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-09-20 00:24:11 +03:00
|
|
|
static UInt32 coreaudio_get_flags(struct audio_pcm_info *info,
|
|
|
|
struct audsettings *as)
|
|
|
|
{
|
|
|
|
UInt32 flags = info->sign ? kAudioFormatFlagIsSignedInteger : 0;
|
|
|
|
if (as->endianness) { /* 0 = little, 1 = big */
|
|
|
|
flags |= kAudioFormatFlagIsBigEndian;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (flags == 0) { /* must not be 0 */
|
|
|
|
flags = kAudioFormatFlagsAreAllClear;
|
|
|
|
}
|
|
|
|
return flags;
|
|
|
|
}
|
|
|
|
|
2015-06-04 00:03:47 +03:00
|
|
|
static int coreaudio_init_out(HWVoiceOut *hw, struct audsettings *as,
|
|
|
|
void *drv_opaque)
|
2005-10-30 21:58:22 +03:00
|
|
|
{
|
|
|
|
OSStatus status;
|
|
|
|
coreaudioVoiceOut *core = (coreaudioVoiceOut *) hw;
|
|
|
|
int err;
|
2005-11-20 19:24:09 +03:00
|
|
|
const char *typ = "playback";
|
|
|
|
AudioValueRange frameRange;
|
2019-03-09 01:34:17 +03:00
|
|
|
Audiodev *dev = drv_opaque;
|
|
|
|
AudiodevCoreaudioPerDirectionOptions *cpdo = dev->u.coreaudio.out;
|
|
|
|
int frames;
|
2005-10-30 21:58:22 +03:00
|
|
|
|
|
|
|
/* create mutex */
|
|
|
|
err = pthread_mutex_init(&core->mutex, NULL);
|
|
|
|
if (err) {
|
2005-11-05 21:55:28 +03:00
|
|
|
dolog("Could not create mutex\nReason: %s\n", strerror (err));
|
2005-10-30 21:58:22 +03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2006-07-05 01:47:22 +04:00
|
|
|
audio_pcm_init_info (&hw->info, as);
|
2005-10-30 21:58:22 +03:00
|
|
|
|
2015-11-29 00:55:20 +03:00
|
|
|
status = coreaudio_get_voice(&core->outputDeviceID);
|
2005-10-30 21:58:22 +03:00
|
|
|
if (status != kAudioHardwareNoError) {
|
|
|
|
coreaudio_logerr2 (status, typ,
|
2005-11-05 21:55:28 +03:00
|
|
|
"Could not get default output Device\n");
|
2005-10-30 21:58:22 +03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (core->outputDeviceID == kAudioDeviceUnknown) {
|
2005-11-05 21:55:28 +03:00
|
|
|
dolog ("Could not initialize %s - Unknown Audiodevice\n", typ);
|
2005-10-30 21:58:22 +03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2005-11-20 19:24:09 +03:00
|
|
|
/* get minimum and maximum buffer frame sizes */
|
2015-11-29 00:55:22 +03:00
|
|
|
status = coreaudio_get_framesizerange(core->outputDeviceID,
|
|
|
|
&frameRange);
|
2005-11-20 19:24:09 +03:00
|
|
|
if (status != kAudioHardwareNoError) {
|
|
|
|
coreaudio_logerr2 (status, typ,
|
|
|
|
"Could not get device buffer frame range\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2019-03-09 01:34:17 +03:00
|
|
|
frames = audio_buffer_frames(
|
|
|
|
qapi_AudiodevCoreaudioPerDirectionOptions_base(cpdo), as, 11610);
|
|
|
|
if (frameRange.mMinimum > frames) {
|
2005-11-20 19:24:09 +03:00
|
|
|
core->audioDevicePropertyBufferFrameSize = (UInt32) frameRange.mMinimum;
|
|
|
|
dolog ("warning: Upsizing Buffer Frames to %f\n", frameRange.mMinimum);
|
2019-03-09 01:34:17 +03:00
|
|
|
} else if (frameRange.mMaximum < frames) {
|
2005-11-20 19:24:09 +03:00
|
|
|
core->audioDevicePropertyBufferFrameSize = (UInt32) frameRange.mMaximum;
|
|
|
|
dolog ("warning: Downsizing Buffer Frames to %f\n", frameRange.mMaximum);
|
|
|
|
}
|
|
|
|
else {
|
2019-03-09 01:34:17 +03:00
|
|
|
core->audioDevicePropertyBufferFrameSize = frames;
|
2005-11-20 19:24:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* set Buffer Frame Size */
|
2015-11-29 00:55:22 +03:00
|
|
|
status = coreaudio_set_framesize(core->outputDeviceID,
|
|
|
|
&core->audioDevicePropertyBufferFrameSize);
|
2005-10-30 21:58:22 +03:00
|
|
|
if (status != kAudioHardwareNoError) {
|
|
|
|
coreaudio_logerr2 (status, typ,
|
2011-06-23 18:24:46 +04:00
|
|
|
"Could not set device buffer frame size %" PRIu32 "\n",
|
|
|
|
(uint32_t)core->audioDevicePropertyBufferFrameSize);
|
2005-10-30 21:58:22 +03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2005-11-20 19:24:09 +03:00
|
|
|
/* get Buffer Frame Size */
|
2015-11-29 00:55:22 +03:00
|
|
|
status = coreaudio_get_framesize(core->outputDeviceID,
|
|
|
|
&core->audioDevicePropertyBufferFrameSize);
|
2005-10-30 21:58:22 +03:00
|
|
|
if (status != kAudioHardwareNoError) {
|
2005-11-20 19:24:09 +03:00
|
|
|
coreaudio_logerr2 (status, typ,
|
|
|
|
"Could not get device buffer frame size\n");
|
2005-10-30 21:58:22 +03:00
|
|
|
return -1;
|
|
|
|
}
|
2019-03-09 01:34:17 +03:00
|
|
|
hw->samples = (cpdo->has_buffer_count ? cpdo->buffer_count : 4) *
|
|
|
|
core->audioDevicePropertyBufferFrameSize;
|
2005-10-30 21:58:22 +03:00
|
|
|
|
|
|
|
/* get StreamFormat */
|
2015-11-29 00:55:22 +03:00
|
|
|
status = coreaudio_get_streamformat(core->outputDeviceID,
|
|
|
|
&core->outputStreamBasicDescription);
|
2005-10-30 21:58:22 +03:00
|
|
|
if (status != kAudioHardwareNoError) {
|
|
|
|
coreaudio_logerr2 (status, typ,
|
2005-11-05 21:55:28 +03:00
|
|
|
"Could not get Device Stream properties\n");
|
2005-10-30 21:58:22 +03:00
|
|
|
core->outputDeviceID = kAudioDeviceUnknown;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* set Samplerate */
|
2005-11-20 19:24:09 +03:00
|
|
|
core->outputStreamBasicDescription.mSampleRate = (Float64) as->freq;
|
2019-09-20 00:24:11 +03:00
|
|
|
core->outputStreamBasicDescription.mFormatID = kAudioFormatLinearPCM;
|
|
|
|
core->outputStreamBasicDescription.mFormatFlags =
|
|
|
|
coreaudio_get_flags(&hw->info, as);
|
|
|
|
core->outputStreamBasicDescription.mBytesPerPacket =
|
|
|
|
core->outputStreamBasicDescription.mBytesPerFrame =
|
|
|
|
hw->info.nchannels * hw->info.bits / 8;
|
|
|
|
core->outputStreamBasicDescription.mFramesPerPacket = 1;
|
|
|
|
core->outputStreamBasicDescription.mChannelsPerFrame = hw->info.nchannels;
|
|
|
|
core->outputStreamBasicDescription.mBitsPerChannel = hw->info.bits;
|
|
|
|
|
2015-11-29 00:55:22 +03:00
|
|
|
status = coreaudio_set_streamformat(core->outputDeviceID,
|
|
|
|
&core->outputStreamBasicDescription);
|
2005-10-30 21:58:22 +03:00
|
|
|
if (status != kAudioHardwareNoError) {
|
2005-11-11 03:03:20 +03:00
|
|
|
coreaudio_logerr2 (status, typ, "Could not set samplerate %d\n",
|
|
|
|
as->freq);
|
2005-10-30 21:58:22 +03:00
|
|
|
core->outputDeviceID = kAudioDeviceUnknown;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* set Callback */
|
2015-11-29 00:55:24 +03:00
|
|
|
core->ioprocid = NULL;
|
|
|
|
status = AudioDeviceCreateIOProcID(core->outputDeviceID,
|
|
|
|
audioDeviceIOProc,
|
|
|
|
hw,
|
|
|
|
&core->ioprocid);
|
|
|
|
if (status != kAudioHardwareNoError || core->ioprocid == NULL) {
|
2005-11-05 21:55:28 +03:00
|
|
|
coreaudio_logerr2 (status, typ, "Could not set IOProc\n");
|
2005-10-30 21:58:22 +03:00
|
|
|
core->outputDeviceID = kAudioDeviceUnknown;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* start Playback */
|
2005-11-20 19:24:09 +03:00
|
|
|
if (!isPlaying(core->outputDeviceID)) {
|
2015-11-29 00:55:24 +03:00
|
|
|
status = AudioDeviceStart(core->outputDeviceID, core->ioprocid);
|
2005-10-30 21:58:22 +03:00
|
|
|
if (status != kAudioHardwareNoError) {
|
2005-11-05 21:55:28 +03:00
|
|
|
coreaudio_logerr2 (status, typ, "Could not start playback\n");
|
2015-11-29 00:55:24 +03:00
|
|
|
AudioDeviceDestroyIOProcID(core->outputDeviceID, core->ioprocid);
|
2005-10-30 21:58:22 +03:00
|
|
|
core->outputDeviceID = kAudioDeviceUnknown;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void coreaudio_fini_out (HWVoiceOut *hw)
|
|
|
|
{
|
|
|
|
OSStatus status;
|
|
|
|
int err;
|
|
|
|
coreaudioVoiceOut *core = (coreaudioVoiceOut *) hw;
|
|
|
|
|
2016-08-01 14:23:43 +03:00
|
|
|
if (!audio_is_cleaning_up()) {
|
2005-11-20 19:24:09 +03:00
|
|
|
/* stop playback */
|
|
|
|
if (isPlaying(core->outputDeviceID)) {
|
2015-11-29 00:55:24 +03:00
|
|
|
status = AudioDeviceStop(core->outputDeviceID, core->ioprocid);
|
2005-11-20 19:24:09 +03:00
|
|
|
if (status != kAudioHardwareNoError) {
|
|
|
|
coreaudio_logerr (status, "Could not stop playback\n");
|
|
|
|
}
|
2005-10-30 21:58:22 +03:00
|
|
|
}
|
|
|
|
|
2005-11-20 19:24:09 +03:00
|
|
|
/* remove callback */
|
2015-11-29 00:55:24 +03:00
|
|
|
status = AudioDeviceDestroyIOProcID(core->outputDeviceID,
|
|
|
|
core->ioprocid);
|
2005-11-20 19:24:09 +03:00
|
|
|
if (status != kAudioHardwareNoError) {
|
|
|
|
coreaudio_logerr (status, "Could not remove IOProc\n");
|
|
|
|
}
|
2005-10-30 21:58:22 +03:00
|
|
|
}
|
|
|
|
core->outputDeviceID = kAudioDeviceUnknown;
|
|
|
|
|
|
|
|
/* destroy mutex */
|
|
|
|
err = pthread_mutex_destroy(&core->mutex);
|
|
|
|
if (err) {
|
2005-11-05 21:55:28 +03:00
|
|
|
dolog("Could not destroy mutex\nReason: %s\n", strerror (err));
|
2005-10-30 21:58:22 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-20 00:24:22 +03:00
|
|
|
static void coreaudio_enable_out(HWVoiceOut *hw, bool enable)
|
2005-10-30 21:58:22 +03:00
|
|
|
{
|
|
|
|
OSStatus status;
|
|
|
|
coreaudioVoiceOut *core = (coreaudioVoiceOut *) hw;
|
|
|
|
|
2019-09-20 00:24:22 +03:00
|
|
|
if (enable) {
|
2005-10-30 21:58:22 +03:00
|
|
|
/* start playback */
|
2005-11-20 19:24:09 +03:00
|
|
|
if (!isPlaying(core->outputDeviceID)) {
|
2015-11-29 00:55:24 +03:00
|
|
|
status = AudioDeviceStart(core->outputDeviceID, core->ioprocid);
|
2005-10-30 21:58:22 +03:00
|
|
|
if (status != kAudioHardwareNoError) {
|
2005-11-20 19:24:09 +03:00
|
|
|
coreaudio_logerr (status, "Could not resume playback\n");
|
2005-10-30 21:58:22 +03:00
|
|
|
}
|
|
|
|
}
|
2019-09-20 00:24:22 +03:00
|
|
|
} else {
|
2005-10-30 21:58:22 +03:00
|
|
|
/* stop playback */
|
2016-08-01 14:23:43 +03:00
|
|
|
if (!audio_is_cleaning_up()) {
|
2005-11-20 19:24:09 +03:00
|
|
|
if (isPlaying(core->outputDeviceID)) {
|
2015-11-29 00:55:24 +03:00
|
|
|
status = AudioDeviceStop(core->outputDeviceID,
|
|
|
|
core->ioprocid);
|
2005-11-20 19:24:09 +03:00
|
|
|
if (status != kAudioHardwareNoError) {
|
|
|
|
coreaudio_logerr (status, "Could not pause playback\n");
|
|
|
|
}
|
2005-10-30 21:58:22 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-09 01:34:15 +03:00
|
|
|
static void *coreaudio_audio_init(Audiodev *dev)
|
2005-10-30 21:58:22 +03:00
|
|
|
{
|
2019-03-09 01:34:17 +03:00
|
|
|
return dev;
|
2005-10-30 21:58:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void coreaudio_audio_fini (void *opaque)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2008-10-06 22:08:30 +04:00
|
|
|
static struct audio_pcm_ops coreaudio_pcm_ops = {
|
2009-08-11 04:31:15 +04:00
|
|
|
.init_out = coreaudio_init_out,
|
|
|
|
.fini_out = coreaudio_fini_out,
|
2019-09-20 00:24:11 +03:00
|
|
|
.write = coreaudio_write,
|
|
|
|
.get_buffer_out = coreaudio_get_buffer_out,
|
|
|
|
.put_buffer_out = coreaudio_put_buffer_out_nowrite,
|
2019-09-20 00:24:22 +03:00
|
|
|
.enable_out = coreaudio_enable_out
|
2005-10-30 21:58:22 +03:00
|
|
|
};
|
|
|
|
|
2018-03-06 10:40:47 +03:00
|
|
|
static struct audio_driver coreaudio_audio_driver = {
|
2009-08-11 04:31:14 +04:00
|
|
|
.name = "coreaudio",
|
|
|
|
.descr = "CoreAudio http://developer.apple.com/audio/coreaudio.html",
|
|
|
|
.init = coreaudio_audio_init,
|
|
|
|
.fini = coreaudio_audio_fini,
|
|
|
|
.pcm_ops = &coreaudio_pcm_ops,
|
|
|
|
.can_be_default = 1,
|
|
|
|
.max_voices_out = 1,
|
|
|
|
.max_voices_in = 0,
|
|
|
|
.voice_size_out = sizeof (coreaudioVoiceOut),
|
|
|
|
.voice_size_in = 0
|
2005-10-30 21:58:22 +03:00
|
|
|
};
|
2018-03-06 10:40:47 +03:00
|
|
|
|
|
|
|
static void register_audio_coreaudio(void)
|
|
|
|
{
|
|
|
|
audio_driver_register(&coreaudio_audio_driver);
|
|
|
|
}
|
|
|
|
type_init(register_audio_coreaudio);
|