diff options
Diffstat (limited to 'audio/out/ao_coreaudio.c')
-rw-r--r-- | audio/out/ao_coreaudio.c | 1320 |
1 files changed, 676 insertions, 644 deletions
diff --git a/audio/out/ao_coreaudio.c b/audio/out/ao_coreaudio.c index bec849d8ca..38c7c7fc29 100644 --- a/audio/out/ao_coreaudio.c +++ b/audio/out/ao_coreaudio.c @@ -30,9 +30,6 @@ * simpler frameworks do. This is due to the fact that CoreAudio pulls * audio samples rather than having them pushed at it (which is nice * when you are wanting to do good buffering of audio). - * - * AC-3 and MPEG audio passthrough is possible, but has never been tested - * due to lack of a soundcard that supports it. */ #include <CoreServices/CoreServices.h> @@ -49,95 +46,58 @@ #include "core/mp_msg.h" #include "ao.h" -#include "audio_out_internal.h" #include "audio/format.h" #include "osdep/timer.h" -#include "libavutil/fifo.h" #include "core/subopt-helper.h" +#include "core/mp_ring.h" -static const ao_info_t info = - { - "Darwin/Mac OS X native audio output", - "coreaudio", - "Timothy J. Wood & Dan Christiansen & Chris Roccati", - "" - }; - -LIBAO_EXTERN(coreaudio) - -/* Prefix for all mp_msg() calls */ -#define ao_msg(a, b, c...) mp_msg(a, b, "AO: [coreaudio] " c) - -#if MAC_OS_X_VERSION_MAX_ALLOWED <= 1040 -/* AudioDeviceIOProcID does not exist in Mac OS X 10.4. We can emulate - * this by using AudioDeviceAddIOProc() and AudioDeviceRemoveIOProc(). */ -#define AudioDeviceIOProcID AudioDeviceIOProc -#define AudioDeviceDestroyIOProcID AudioDeviceRemoveIOProc -static OSStatus AudioDeviceCreateIOProcID(AudioDeviceID dev, - AudioDeviceIOProc proc, - void *data, - AudioDeviceIOProcID *procid) -{ - *procid = proc; - return AudioDeviceAddIOProc(dev, proc, data); -} -#endif +#define ca_msg(a, b, c ...) mp_msg(a, b, "AO: [coreaudio] " c) -typedef struct ao_coreaudio_s +static void audio_pause(struct ao *ao); +static void audio_resume(struct ao *ao); +static void reset(struct ao *ao); + +static void print_buffer(struct mp_ring *buffer) { - AudioDeviceID i_selected_dev; /* Keeps DeviceID of the selected device. */ - int b_supports_digital; /* Does the currently selected device support digital mode? */ - int b_digital; /* Are we running in digital mode? */ - int b_muted; /* Are we muted in digital mode? */ - - AudioDeviceIOProcID renderCallback; /* Render callback used for SPDIF */ - - /* AudioUnit */ - AudioUnit theOutputUnit; - - /* CoreAudio SPDIF mode specific */ - pid_t i_hog_pid; /* Keeps the pid of our hog status. */ - AudioStreamID i_stream_id; /* The StreamID that has a cac3 streamformat */ - int i_stream_index; /* The index of i_stream_id in an AudioBufferList */ - AudioStreamBasicDescription stream_format;/* The format we changed the stream to */ - AudioStreamBasicDescription sfmt_revert; /* The original format of the stream */ - int b_revert; /* Whether we need to revert the stream format */ - int b_changed_mixing; /* Whether we need to set the mixing mode back */ - int b_stream_format_changed; /* Flag for main thread to reset stream's format to digital and reset buffer */ - - /* Original common part */ - int packetSize; - int paused; - - /* Ring-buffer */ - AVFifoBuffer *buffer; - unsigned int buffer_len; ///< must always be num_chunks * chunk_size - unsigned int num_chunks; - unsigned int chunk_size; -} ao_coreaudio_t; - -static ao_coreaudio_t *ao = NULL; - -/** - * \brief add data to ringbuffer - */ -static int write_buffer(unsigned char* data, int len){ - int free = ao->buffer_len - av_fifo_size(ao->buffer); - if (len > free) len = free; - return av_fifo_generic_write(ao->buffer, data, len, NULL); + void *tctx = talloc_new(NULL); + ca_msg(MSGT_AO, MSGL_V, "%s\n", mp_ring_repr(buffer, tctx)); + talloc_free(tctx); } -/** - * \brief remove data from ringbuffer - */ -static int read_buffer(unsigned char* data,int len){ - int buffered = av_fifo_size(ao->buffer); - if (len > buffered) len = buffered; - if (data) - av_fifo_generic_read(ao->buffer, data, len, NULL); - else - av_fifo_drain(ao->buffer, len); - return len; + +struct priv +{ + AudioDeviceID i_selected_dev; /* Keeps DeviceID of the selected device. */ + int b_supports_digital; /* Does the currently selected device support digital mode? */ + int b_digital; /* Are we running in digital mode? */ + int b_muted; /* Are we muted in digital mode? */ + + AudioDeviceIOProcID renderCallback; /* Render callback used for SPDIF */ + + /* AudioUnit */ + AudioUnit theOutputUnit; + + /* CoreAudio SPDIF mode specific */ + pid_t i_hog_pid; /* Keeps the pid of our hog status. */ + AudioStreamID i_stream_id; /* The StreamID that has a cac3 streamformat */ + int i_stream_index; /* The index of i_stream_id in an AudioBufferList */ + AudioStreamBasicDescription stream_format; /* The format we changed the stream to */ + AudioStreamBasicDescription sfmt_revert; /* The original format of the stream */ + int b_revert; /* Whether we need to revert the stream format */ + int b_changed_mixing; /* Whether we need to set the mixing mode back */ + int b_stream_format_changed; /* Flag for main thread to reset stream's format to digital and reset buffer */ + + /* Original common part */ + int packetSize; + int paused; + + struct mp_ring *buffer; +}; + +static int get_ring_size(struct ao *ao) +{ + return af_fmt_seconds_to_bytes( + ao->format, 0.5, ao->channels.num, ao->samplerate); } static OSStatus theRenderProc(void *inRefCon, @@ -146,100 +106,115 @@ static OSStatus theRenderProc(void *inRefCon, UInt32 inBusNumber, UInt32 inNumFrames, AudioBufferList *ioData) { -int amt=av_fifo_size(ao->buffer); -int req=(inNumFrames)*ao->packetSize; + struct ao *ao = inRefCon; + struct priv *p = ao->priv; + + int buffered = mp_ring_buffered(p->buffer); + int requested = inNumFrames * p->packetSize; - if(amt>req) - amt=req; + if (buffered > requested) + buffered = requested; + + if (buffered) { + mp_ring_read(p->buffer, + (unsigned char *)ioData->mBuffers[0].mData, + buffered); + } else { + audio_pause(ao); + } - if(amt) - read_buffer((unsigned char *)ioData->mBuffers[0].mData, amt); - else audio_pause(); - ioData->mBuffers[0].mDataByteSize = amt; + ioData->mBuffers[0].mDataByteSize = buffered; - return noErr; + return noErr; } -static int control(int cmd,void *arg){ -ao_control_vol_t *control_vol; -OSStatus err; -Float32 vol; - switch (cmd) { - case AOCONTROL_GET_VOLUME: - control_vol = (ao_control_vol_t*)arg; - if (ao->b_digital) { - // Digital output has no volume adjust. - int vol = ao->b_muted ? 0 : 100; - *control_vol = (ao_control_vol_t) { - .left = vol, .right = vol, - }; - return CONTROL_TRUE; - } - err = AudioUnitGetParameter(ao->theOutputUnit, kHALOutputParam_Volume, kAudioUnitScope_Global, 0, &vol); - - if(err==0) { - // printf("GET VOL=%f\n", vol); - control_vol->left=control_vol->right=vol*100.0/4.0; - return CONTROL_TRUE; - } - else { - ao_msg(MSGT_AO, MSGL_WARN, "could not get HAL output volume: [%4.4s]\n", (char *)&err); - return CONTROL_FALSE; - } - - case AOCONTROL_SET_VOLUME: - control_vol = (ao_control_vol_t*)arg; - - if (ao->b_digital) { - // Digital output can not set volume. Here we have to return true - // to make mixer forget it. Else mixer will add a soft filter, - // that's not we expected and the filter not support ac3 stream - // will cause mplayer die. - - // Although not support set volume, but at least we support mute. - // MPlayer set mute by set volume to zero, we handle it. - if (control_vol->left == 0 && control_vol->right == 0) - ao->b_muted = 1; - else - ao->b_muted = 0; - return CONTROL_TRUE; - } - - vol=(control_vol->left+control_vol->right)*4.0/200.0; - err = AudioUnitSetParameter(ao->theOutputUnit, kHALOutputParam_Volume, kAudioUnitScope_Global, 0, vol, 0); - if(err==0) { - // printf("SET VOL=%f\n", vol); - return CONTROL_TRUE; - } - else { - ao_msg(MSGT_AO, MSGL_WARN, "could not set HAL output volume: [%4.4s]\n", (char *)&err); - return CONTROL_FALSE; - } - /* Everything is currently unimplemented */ - default: - return CONTROL_FALSE; - } +static int control(struct ao *ao, enum aocontrol cmd, void *arg) +{ + struct priv *p = ao->priv; + ao_control_vol_t *control_vol; + OSStatus err; + Float32 vol; + switch (cmd) { + case AOCONTROL_GET_VOLUME: + control_vol = (ao_control_vol_t *)arg; + if (p->b_digital) { + // Digital output has no volume adjust. + int vol = p->b_muted ? 0 : 100; + *control_vol = (ao_control_vol_t) { + .left = vol, .right = vol, + }; + return CONTROL_TRUE; + } + err = AudioUnitGetParameter(p->theOutputUnit, kHALOutputParam_Volume, + kAudioUnitScope_Global, 0, &vol); + + if (err == 0) { + // printf("GET VOL=%f\n", vol); + control_vol->left = control_vol->right = vol * 100.0 / 4.0; + return CONTROL_TRUE; + } else { + ca_msg(MSGT_AO, MSGL_WARN, + "could not get HAL output volume: [%4.4s]\n", (char *)&err); + return CONTROL_FALSE; + } + + case AOCONTROL_SET_VOLUME: + control_vol = (ao_control_vol_t *)arg; + + if (p->b_digital) { + // Digital output can not set volume. Here we have to return true + // to make mixer forget it. Else mixer will add a soft filter, + // that's not we expected and the filter not support ac3 stream + // will cause mplayer die. + + // Although not support set volume, but at least we support mute. + // MPlayer set mute by set volume to zero, we handle it. + if (control_vol->left == 0 && control_vol->right == 0) + p->b_muted = 1; + else + p->b_muted = 0; + return CONTROL_TRUE; + } + + vol = (control_vol->left + control_vol->right) * 4.0 / 200.0; + err = AudioUnitSetParameter(p->theOutputUnit, kHALOutputParam_Volume, + kAudioUnitScope_Global, 0, vol, 0); + if (err == 0) { + // printf("SET VOL=%f\n", vol); + return CONTROL_TRUE; + } else { + ca_msg(MSGT_AO, MSGL_WARN, + "could not set HAL output volume: [%4.4s]\n", (char *)&err); + return CONTROL_FALSE; + } + /* Everything is currently unimplemented */ + default: + return CONTROL_FALSE; + } } -static void print_format(int lev, const char* str, const AudioStreamBasicDescription *f){ - uint32_t flags=(uint32_t) f->mFormatFlags; - ao_msg(MSGT_AO,lev, "%s %7.1fHz %"PRIu32"bit [%c%c%c%c][%"PRIu32"][%"PRIu32"][%"PRIu32"][%"PRIu32"][%"PRIu32"] %s %s %s%s%s%s\n", - str, f->mSampleRate, f->mBitsPerChannel, - (int)(f->mFormatID & 0xff000000) >> 24, - (int)(f->mFormatID & 0x00ff0000) >> 16, - (int)(f->mFormatID & 0x0000ff00) >> 8, - (int)(f->mFormatID & 0x000000ff) >> 0, - f->mFormatFlags, f->mBytesPerPacket, - f->mFramesPerPacket, f->mBytesPerFrame, - f->mChannelsPerFrame, - (flags&kAudioFormatFlagIsFloat) ? "float" : "int", - (flags&kAudioFormatFlagIsBigEndian) ? "BE" : "LE", - (flags&kAudioFormatFlagIsSignedInteger) ? "S" : "U", - (flags&kAudioFormatFlagIsPacked) ? " packed" : "", - (flags&kAudioFormatFlagIsAlignedHigh) ? " aligned" : "", - (flags&kAudioFormatFlagIsNonInterleaved) ? " ni" : "" ); +static void print_format(int lev, const char *str, + const AudioStreamBasicDescription *f) +{ + uint32_t flags = (uint32_t) f->mFormatFlags; + ca_msg(MSGT_AO, lev, + "%s %7.1fHz %" PRIu32 "bit [%c%c%c%c][%" PRIu32 "][%" PRIu32 "][%" PRIu32 "][%" PRIu32 "][%" PRIu32 "] %s %s %s%s%s%s\n", + str, f->mSampleRate, f->mBitsPerChannel, + (int)(f->mFormatID & 0xff000000) >> 24, + (int)(f->mFormatID & 0x00ff0000) >> 16, + (int)(f->mFormatID & 0x0000ff00) >> 8, + (int)(f->mFormatID & 0x000000ff) >> 0, + f->mFormatFlags, f->mBytesPerPacket, + f->mFramesPerPacket, f->mBytesPerFrame, + f->mChannelsPerFrame, + (flags & kAudioFormatFlagIsFloat) ? "float" : "int", + (flags & kAudioFormatFlagIsBigEndian) ? "BE" : "LE", + (flags & kAudioFormatFlagIsSignedInteger) ? "S" : "U", + (flags & kAudioFormatFlagIsPacked) ? " packed" : "", + (flags & kAudioFormatFlagIsAlignedHigh) ? " aligned" : "", + (flags & kAudioFormatFlagIsNonInterleaved) ? " ni" : ""); } static OSStatus GetAudioProperty(AudioObjectID id, @@ -252,7 +227,8 @@ static OSStatus GetAudioProperty(AudioObjectID id, property_address.mScope = kAudioObjectPropertyScopeGlobal; property_address.mElement = kAudioObjectPropertyElementMaster; - return AudioObjectGetPropertyData(id, &property_address, 0, NULL, &outSize, outData); + return AudioObjectGetPropertyData(id, &property_address, 0, NULL, &outSize, + outData); } static UInt32 GetAudioPropertyArray(AudioObjectID id, @@ -268,7 +244,8 @@ static UInt32 GetAudioPropertyArray(AudioObjectID id, property_address.mScope = scope; property_address.mElement = kAudioObjectPropertyElementMaster; - err = AudioObjectGetPropertyDataSize(id, &property_address, 0, NULL, &i_param_size); + err = AudioObjectGetPropertyDataSize(id, &property_address, 0, NULL, + &i_param_size); if (err != noErr) return 0; @@ -276,7 +253,8 @@ static UInt32 GetAudioPropertyArray(AudioObjectID id, *outData = malloc(i_param_size); - err = AudioObjectGetPropertyData(id, &property_address, 0, NULL, &i_param_size, *outData); + err = AudioObjectGetPropertyData(id, &property_address, 0, NULL, + &i_param_size, *outData); if (err != noErr) { free(*outData); @@ -290,7 +268,8 @@ static UInt32 GetGlobalAudioPropertyArray(AudioObjectID id, AudioObjectPropertySelector selector, void **outData) { - return GetAudioPropertyArray(id, selector, kAudioObjectPropertyScopeGlobal, outData); + return GetAudioPropertyArray(id, selector, kAudioObjectPropertyScopeGlobal, + outData); } static OSStatus GetAudioPropertyString(AudioObjectID id, @@ -308,14 +287,16 @@ static OSStatus GetAudioPropertyString(AudioObjectID id, property_address.mElement = kAudioObjectPropertyElementMaster; i_param_size = sizeof(CFStringRef); - err = AudioObjectGetPropertyData(id, &property_address, 0, NULL, &i_param_size, &string); + err = AudioObjectGetPropertyData(id, &property_address, 0, NULL, + &i_param_size, &string); if (err != noErr) return err; string_length = CFStringGetMaximumSizeForEncoding(CFStringGetLength(string), kCFStringEncodingASCII); *outData = malloc(string_length + 1); - CFStringGetCString(string, *outData, string_length + 1, kCFStringEncodingASCII); + CFStringGetCString(string, *outData, string_length + 1, + kCFStringEncodingASCII); CFRelease(string); @@ -332,7 +313,8 @@ static OSStatus SetAudioProperty(AudioObjectID id, property_address.mScope = kAudioObjectPropertyScopeGlobal; property_address.mElement = kAudioObjectPropertyElementMaster; - return AudioObjectSetPropertyData(id, &property_address, 0, NULL, inDataSize, inData); + return AudioObjectSetPropertyData(id, &property_address, 0, NULL, + inDataSize, inData); } static Boolean IsAudioPropertySettable(AudioObjectID id, @@ -348,25 +330,26 @@ static Boolean IsAudioPropertySettable(AudioObjectID id, return AudioObjectIsPropertySettable(id, &property_address, outData); } -static int AudioDeviceSupportsDigital( AudioDeviceID i_dev_id ); -static int AudioStreamSupportsDigital( AudioStreamID i_stream_id ); -static int OpenSPDIF(void); -static int AudioStreamChangeFormat( AudioStreamID i_stream_id, AudioStreamBasicDescription change_format ); -static OSStatus RenderCallbackSPDIF( AudioDeviceID inDevice, - const AudioTimeStamp * inNow, - const void * inInputData, - const AudioTimeStamp * inInputTime, - AudioBufferList * outOutputData, - const AudioTimeStamp * inOutputTime, - void * threadGlobals ); -static OSStatus StreamListener( AudioObjectID inObjectID, - UInt32 inNumberAddresses, - const AudioObjectPropertyAddress inAddresses[], - void *inClientData ); -static OSStatus DeviceListener( AudioObjectID inObjectID, - UInt32 inNumberAddresses, - const AudioObjectPropertyAddress inAddresses[], - void *inClientData ); +static int AudioDeviceSupportsDigital(AudioDeviceID i_dev_id); +static int AudioStreamSupportsDigital(AudioStreamID i_stream_id); +static int OpenSPDIF(struct ao *ao); +static int AudioStreamChangeFormat(AudioStreamID i_stream_id, + AudioStreamBasicDescription change_format); +static OSStatus RenderCallbackSPDIF(AudioDeviceID inDevice, + const AudioTimeStamp *inNow, + const void *inInputData, + const AudioTimeStamp *inInputTime, + AudioBufferList *outOutputData, + const AudioTimeStamp *inOutputTime, + void *threadGlobals); +static OSStatus StreamListener(AudioObjectID inObjectID, + UInt32 inNumberAddresses, + const AudioObjectPropertyAddress inAddresses[], + void *inClientData); +static OSStatus DeviceListener(AudioObjectID inObjectID, + UInt32 inNumberAddresses, + const AudioObjectPropertyAddress inAddresses[], + void *inClientData); static void print_help(void) { @@ -388,7 +371,9 @@ static void print_help(void) "\n" "Available output devices:\n"); - i_param_size = GetGlobalAudioPropertyArray(kAudioObjectSystemObject, kAudioHardwarePropertyDevices, (void **)&devids); + i_param_size = GetGlobalAudioPropertyArray(kAudioObjectSystemObject, + kAudioHardwarePropertyDevices, + (void **)&devids); if (!i_param_size) { mp_msg(MSGT_AO, MSGL_FATAL, "Failed to get list of output devices.\n"); @@ -398,13 +383,16 @@ static void print_help(void) num_devices = i_param_size / sizeof(AudioDeviceID); for (int i = 0; i < num_devices; ++i) { - err = GetAudioPropertyString(devids[i], kAudioObjectPropertyName, &device_name); + err = GetAudioPropertyString(devids[i], kAudioObjectPropertyName, + &device_name); if (err == noErr) { - mp_msg(MSGT_AO, MSGL_FATAL, "%s (id: %"PRIu32")\n", device_name, devids[i]); + mp_msg(MSGT_AO, MSGL_FATAL, "%s (id: %" PRIu32 ")\n", device_name, + devids[i]); free(device_name); } else - mp_msg(MSGT_AO, MSGL_FATAL, "Unknown (id: %"PRIu32")\n", devids[i]); + mp_msg(MSGT_AO, MSGL_FATAL, "Unknown (id: %" PRIu32 ")\n", + devids[i]); } mp_msg(MSGT_AO, MSGL_FATAL, "\n"); @@ -412,21 +400,25 @@ static void print_help(void) free(devids); } -static int init(int rate,const struct mp_chmap *channels,int format,int flags) +static int init(struct ao *ao, char *params) { -AudioStreamBasicDescription inDesc; -AudioComponentDescription desc; -AudioComponent comp; -AURenderCallbackStruct renderCallback; -OSStatus err; -UInt32 size, maxFrames, b_alive; -char *psz_name; -AudioDeviceID devid_def = 0; -int device_id, display_help = 0; + // int rate, int channels, int format, int flags) + struct priv *p = talloc_zero(ao, struct priv); + ao->priv = p; + + AudioStreamBasicDescription inDesc; + AudioComponentDescription desc; + AudioComponent comp; + AURenderCallbackStruct renderCallback; + OSStatus err; + UInt32 size, maxFrames, b_alive; + char *psz_name; + AudioDeviceID devid_def = 0; + int device_id, display_help = 0; const opt_t subopts[] = { - {"device_id", OPT_ARG_INT, &device_id, NULL}, - {"help", OPT_ARG_BOOL, &display_help, NULL}, + {"device_id", OPT_ARG_INT, &device_id, NULL}, + {"help", OPT_ARG_BOOL, &display_help, NULL}, {NULL} }; @@ -439,32 +431,32 @@ int device_id, display_help = 0; return 0; } - ao_msg(MSGT_AO,MSGL_V, "init([%dHz][%dch][%s][%d])\n", rate, ao_data.channels.num, af_fmt2str_short(format), flags); + ca_msg(MSGT_AO, MSGL_V, "init([%dHz][%dch][%s][%d])\n", + ao->samplerate, ao->channels.num, af_fmt2str_short(ao->format), 0); - ao = calloc(1, sizeof(ao_coreaudio_t)); + p->i_selected_dev = 0; + p->b_supports_digital = 0; + p->b_digital = 0; + p->b_muted = 0; + p->b_stream_format_changed = 0; + p->i_hog_pid = -1; + p->i_stream_id = 0; + p->i_stream_index = -1; + p->b_revert = 0; + p->b_changed_mixing = 0; - ao->i_selected_dev = 0; - ao->b_supports_digital = 0; - ao->b_digital = 0; - ao->b_muted = 0; - ao->b_stream_format_changed = 0; - ao->i_hog_pid = -1; - ao->i_stream_id = 0; - ao->i_stream_index = -1; - ao->b_revert = 0; - ao->b_changed_mixing = 0; - - global_ao->per_application_mixer = true; - global_ao->no_persistent_volume = true; + ao->per_application_mixer = true; + ao->no_persistent_volume = true; if (device_id == 0) { /* Find the ID of the default Device. */ err = GetAudioProperty(kAudioObjectSystemObject, kAudioHardwarePropertyDefaultOutputDevice, sizeof(UInt32), &devid_def); - if (err != noErr) - { - ao_msg(MSGT_AO, MSGL_WARN, "could not get default audio device: [%4.4s]\n", (char *)&err); + if (err != noErr) { + ca_msg(MSGT_AO, MSGL_WARN, + "could not get default audio device: [%4.4s]\n", + (char *)&err); goto err_out; } } else { @@ -475,201 +467,219 @@ int device_id, display_help = 0; err = GetAudioPropertyString(devid_def, kAudioObjectPropertyName, &psz_name); - if (err != noErr) - { - ao_msg(MSGT_AO, MSGL_WARN, "could not get default audio device name: [%4.4s]\n", (char *)&err); + if (err != noErr) { + ca_msg(MSGT_AO, MSGL_WARN, + "could not get default audio device name: [%4.4s]\n", + (char *)&err); goto err_out; } - ao_msg(MSGT_AO,MSGL_V, "got audio output device ID: %"PRIu32" Name: %s\n", devid_def, psz_name ); + ca_msg(MSGT_AO, MSGL_V, + "got audio output device ID: %" PRIu32 " Name: %s\n", devid_def, + psz_name); /* Probe whether device support S/PDIF stream output if input is AC3 stream. */ - if (AF_FORMAT_IS_AC3(format)) { + if (AF_FORMAT_IS_AC3(ao->format)) { if (AudioDeviceSupportsDigital(devid_def)) - { - ao->b_supports_digital = 1; - } - ao_msg(MSGT_AO, MSGL_V, + p->b_supports_digital = 1; + ca_msg(MSGT_AO, MSGL_V, "probe default audio output device about support for digital s/pdif output: %d\n", - ao->b_supports_digital ); + p->b_supports_digital); } free(psz_name); // Save selected device id - ao->i_selected_dev = devid_def; + p->i_selected_dev = devid_def; struct mp_chmap_sel chmap_sel = {0}; mp_chmap_sel_add_waveext(&chmap_sel); - if (!ao_chmap_sel_adjust(&ao_data, &chmap_sel, &ao_data.channels)) + if (!ao_chmap_sel_adjust(ao, &chmap_sel, &ao->channels)) goto err_out; - // Build Description for the input format - inDesc.mSampleRate=rate; - inDesc.mFormatID=ao->b_supports_digital ? kAudioFormat60958AC3 : kAudioFormatLinearPCM; - inDesc.mChannelsPerFrame=ao_data.channels.num; - inDesc.mBitsPerChannel=af_fmt2bits(format); - - if((format&AF_FORMAT_POINT_MASK)==AF_FORMAT_F) { - // float - inDesc.mFormatFlags = kAudioFormatFlagIsFloat|kAudioFormatFlagIsPacked; - } - else if((format&AF_FORMAT_SIGN_MASK)==AF_FORMAT_SI) { - // signed int - inDesc.mFormatFlags = kAudioFormatFlagIsSignedInteger|kAudioFormatFlagIsPacked; - } - else { - // unsigned int - inDesc.mFormatFlags = kAudioFormatFlagIsPacked; + // Build Description for the input format + inDesc.mSampleRate = ao->samplerate; + inDesc.mFormatID = + p->b_supports_digital ? kAudioFormat60958AC3 : kAudioFormatLinearPCM; + inDesc.mChannelsPerFrame = ao->channels.num; + inDesc.mBitsPerChannel = af_fmt2bits(ao->format); + + if ((ao->format & AF_FORMAT_POINT_MASK) == AF_FORMAT_F) { + // float + inDesc.mFormatFlags = kAudioFormatFlagIsFloat | + kAudioFormatFlagIsPacked; + } else if ((ao->format & AF_FORMAT_SIGN_MASK) == AF_FORMAT_SI) { + // signed int + inDesc.mFormatFlags = kAudioFormatFlagIsSignedInteger | + kAudioFormatFlagIsPacked; + } else { + // unsigned int + inDesc.mFormatFlags = kAudioFormatFlagIsPacked; } - if ((format & AF_FORMAT_END_MASK) == AF_FORMAT_BE) + if ((ao->format & AF_FORMAT_END_MASK) == AF_FORMAT_BE) inDesc.mFormatFlags |= kAudioFormatFlagIsBigEndian; inDesc.mFramesPerPacket = 1; - ao->packetSize = inDesc.mBytesPerPacket = inDesc.mBytesPerFrame = inDesc.mFramesPerPacket*ao_data.channels.num*(inDesc.mBitsPerChannel/8); - print_format(MSGL_V, "source:",&inDesc); + p->packetSize = inDesc.mBytesPerPacket = inDesc.mBytesPerFrame = + inDesc.mFramesPerPacket * + ao->channels.num * + (inDesc.mBitsPerChannel / 8); + print_format(MSGL_V, "source:", &inDesc); - if (ao->b_supports_digital) - { + if (p->b_supports_digital) { b_alive = 1; - err = GetAudioProperty(ao->i_selected_dev, + err = GetAudioProperty(p->i_selected_dev, kAudioDevicePropertyDeviceIsAlive, sizeof(UInt32), &b_alive); if (err != noErr) - ao_msg(MSGT_AO, MSGL_WARN, "could not check whether device is alive: [%4.4s]\n", (char *)&err); + ca_msg(MSGT_AO, MSGL_WARN, + "could not check whether device is alive: [%4.4s]\n", + (char *)&err); if (!b_alive) - ao_msg(MSGT_AO, MSGL_WARN, "device is not alive\n" ); + ca_msg(MSGT_AO, MSGL_WARN, "device is not alive\n"); /* S/PDIF output need device in HogMode. */ - err = GetAudioProperty(ao->i_selected_dev, + err = GetAudioProperty(p->i_selected_dev, kAudioDevicePropertyHogMode, - sizeof(pid_t), &ao->i_hog_pid); - if (err != noErr) - { + sizeof(pid_t), &p->i_hog_pid); + if (err != noErr) { /* This is not a fatal error. Some drivers simply don't support this property. */ - ao_msg(MSGT_AO, MSGL_WARN, "could not check whether device is hogged: [%4.4s]\n", - (char *)&err); - ao->i_hog_pid = -1; + ca_msg(MSGT_AO, MSGL_WARN, + "could not check whether device is hogged: [%4.4s]\n", + (char *)&err); + p->i_hog_pid = -1; } - if (ao->i_hog_pid != -1 && ao->i_hog_pid != getpid()) - { - ao_msg(MSGT_AO, MSGL_WARN, "Selected audio device is exclusively in use by another program.\n" ); + if (p->i_hog_pid != -1 && p->i_hog_pid != getpid()) { + ca_msg(MSGT_AO, MSGL_WARN, + "Selected audio device is exclusively in use by another program.\n"); goto err_out; } - ao->stream_format = inDesc; - return OpenSPDIF(); + p->stream_format = inDesc; + return OpenSPDIF(ao); + } + + /* original analog output code */ + desc.componentType = kAudioUnitType_Output; + desc.componentSubType = + (device_id == + 0) ? kAudioUnitSubType_DefaultOutput : kAudioUnitSubType_HALOutput; + desc.componentManufacturer = kAudioUnitManufacturer_Apple; + desc.componentFlags = 0; + desc.componentFlagsMask = 0; + + comp = AudioComponentFindNext(NULL, &desc); //Finds an component that meets the desc spec's + if (comp == NULL) { + ca_msg(MSGT_AO, MSGL_WARN, "Unable to find Output Unit component\n"); + goto err_out; + } + + err = AudioComponentInstanceNew(comp, &(p->theOutputUnit)); //gains access to the services provided by the component + if (err) { + ca_msg(MSGT_AO, MSGL_WARN, + "Unable to open Output Unit component: [%4.4s]\n", (char *)&err); + goto err_out; + } + + // Initialize AudioUnit + err = AudioUnitInitialize(p->theOutputUnit); + if (err) { + ca_msg(MSGT_AO, MSGL_WARN, + "Unable to initialize Output Unit component: [%4.4s]\n", + (char *)&err); + goto err_out1; + } + + size = sizeof(AudioStreamBasicDescription); + err = AudioUnitSetProperty(p->theOutputUnit, + kAudioUnitProperty_StreamFormat, + kAudioUnitScope_Input, 0, &inDesc, size); + + if (err) { + ca_msg(MSGT_AO, MSGL_WARN, "Unable to set the input format: [%4.4s]\n", + (char *)&err); + goto err_out2; } - /* original analog output code */ - desc.componentType = kAudioUnitType_Output; - desc.componentSubType = (device_id == 0) ? kAudioUnitSubType_DefaultOutput : kAudioUnitSubType_HALOutput; - desc.componentManufacturer = kAudioUnitManufacturer_Apple; - desc.componentFlags = 0; - desc.componentFlagsMask = 0; - - comp = AudioComponentFindNext(NULL, &desc); //Finds an component that meets the desc spec's - if (comp == NULL) { - ao_msg(MSGT_AO, MSGL_WARN, "Unable to find Output Unit component\n"); - goto err_out; - } - - err = AudioComponentInstanceNew(comp, &(ao->theOutputUnit)); //gains access to the services provided by the component - if (err) { - ao_msg(MSGT_AO, MSGL_WARN, "Unable to open Output Unit component: [%4.4s]\n", (char *)&err); - goto err_out; - } - - // Initialize AudioUnit - err = AudioUnitInitialize(ao->theOutputUnit); - if (err) { - ao_msg(MSGT_AO, MSGL_WARN, "Unable to initialize Output Unit component: [%4.4s]\n", (char *)&err); - goto err_out1; - } - - size = sizeof(AudioStreamBasicDescription); - err = AudioUnitSetProperty(ao->theOutputUnit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Input, 0, &inDesc, size); - - if (err) { - ao_msg(MSGT_AO, MSGL_WARN, "Unable to set the input format: [%4.4s]\n", (char *)&err); - goto err_out2; - } - - size = sizeof(UInt32); - err = AudioUnitGetProperty(ao->theOutputUnit, kAudioDevicePropertyBufferSize, kAudioUnitScope_Input, 0, &maxFrames, &size); - - if (err) - { - ao_msg(MSGT_AO,MSGL_WARN, "AudioUnitGetProperty returned [%4.4s] when getting kAudioDevicePropertyBufferSize\n", (char *)&err); - goto err_out2; - } - - //Set the Current Device to the Default Output Unit. - err = AudioUnitSetProperty(ao->theOutputUnit, kAudioOutputUnitProperty_CurrentDevice, kAudioUnitScope_Global, 0, &ao->i_selected_dev, sizeof(ao->i_selected_dev)); - - ao->chunk_size = maxFrames;//*inDesc.mBytesPerFrame; - - ao_data.samplerate = inDesc.mSampleRate; - if (!ao_chmap_sel_get_def(&ao_data, &chmap_sel, &ao_data.channels, inDesc.mChannelsPerFrame)) - goto err_out2; - ao_data.bps = ao_data.samplerate * inDesc.mBytesPerFrame; - ao_data.outburst = ao->chunk_size; - ao_data.buffersize = ao_data.bps; - - ao->num_chunks = (ao_data.bps+ao->chunk_size-1)/ao->chunk_size; - ao->buffer_len = ao->num_chunks * ao->chunk_size; - ao->buffer = av_fifo_alloc(ao->buffer_len); - - ao_msg(MSGT_AO,MSGL_V, "using %5d chunks of %d bytes (buffer len %d bytes)\n", (int)ao->num_chunks, (int)ao->chunk_size, (int)ao->buffer_len); + size = sizeof(UInt32); + err = AudioUnitGetProperty(p->theOutputUnit, + kAudioDevicePropertyBufferSize, + kAudioUnitScope_Input, 0, &maxFrames, &size); + + if (err) { + ca_msg(MSGT_AO, MSGL_WARN, + "AudioUnitGetProperty returned [%4.4s] when getting kAudioDevicePropertyBufferSize\n", + (char *)&err); + goto err_out2; + } + + //Set the Current Device to the Default Output Unit. + err = AudioUnitSetProperty(p->theOutputUnit, + kAudioOutputUnitProperty_CurrentDevice, + kAudioUnitScope_Global, 0, &p->i_selected_dev, + sizeof(p->i_selected_dev)); + + ao->samplerate = inDesc.mSampleRate; + + if (!ao_chmap_sel_get_def(ao, &chmap_sel, &ao->channels, + inDesc.mChannelsPerFrame)) + goto err_out2; + + ao->bps = ao->samplerate * inDesc.mBytesPerFrame; + p->buffer = mp_ring_new(p, get_ring_size(ao)); + + print_buffer(p->buffer); renderCallback.inputProc = theRenderProc; - renderCallback.inputProcRefCon = 0; - err = AudioUnitSetProperty(ao->theOutputUnit, kAudioUnitProperty_SetRenderCallback, kAudioUnitScope_Input, 0, &renderCallback, sizeof(AURenderCallbackStruct)); - if (err) { - ao_msg(MSGT_AO, MSGL_WARN, "Unable to set the render callback: [%4.4s]\n", (char *)&err); - goto err_out2; - } + renderCallback.inputProcRefCon = ao; + err = AudioUnitSetProperty(p->theOutputUnit, + kAudioUnitProperty_SetRenderCallback, + kAudioUnitScope_Input, 0, &renderCallback, + sizeof(AURenderCallbackStruct)); + if (err) { + ca_msg(MSGT_AO, MSGL_WARN, + "Unable to set the render callback: [%4.4s]\n", (char *)&err); + goto err_out2; + } - reset(); + reset(ao); return CONTROL_OK; err_out2: - AudioUnitUninitialize(ao->theOutputUnit); + AudioUnitUninitialize(p->theOutputUnit); err_out1: - AudioComponentInstanceDispose(ao->theOutputUnit); + AudioComponentInstanceDispose(p->theOutputUnit); err_out: - av_fifo_free(ao->buffer); - free(ao); - ao = NULL; return CONTROL_FALSE; } /***************************************************************************** - * Setup a encoded digital stream (SPDIF) - *****************************************************************************/ -static int OpenSPDIF(void) |