2003-05-21 23:15:46 +02:00
|
|
|
/*
|
2009-05-04 16:53:47 +02:00
|
|
|
* CoreAudio audio output driver for Mac OS X
|
2003-05-21 23:15:46 +02:00
|
|
|
*
|
2008-06-06 02:45:35 +02:00
|
|
|
* original copyright (C) Timothy J. Wood - Aug 2000
|
|
|
|
* ported to MPlayer libao2 by Dan Christiansen
|
2003-05-21 23:15:46 +02:00
|
|
|
*
|
2008-06-06 02:45:35 +02:00
|
|
|
* The S/PDIF part of the code is based on the auhal audio output
|
|
|
|
* module from VideoLAN:
|
|
|
|
* Copyright (c) 2006 Derk-Jan Hartman <hartman at videolan dot org>
|
2003-05-21 23:15:46 +02:00
|
|
|
*
|
2008-06-06 02:45:35 +02:00
|
|
|
* This file is part of MPlayer.
|
2007-12-22 18:33:28 +01:00
|
|
|
*
|
2008-06-06 02:45:35 +02:00
|
|
|
* MPlayer is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
2003-05-21 23:15:46 +02:00
|
|
|
*
|
2008-06-06 02:45:35 +02:00
|
|
|
* MPlayer is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
2003-05-21 23:15:46 +02:00
|
|
|
*
|
2008-06-06 02:45:35 +02:00
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* along with MPlayer; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
2003-05-21 23:15:46 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The MacOS X CoreAudio framework doesn't mesh as simply as some
|
|
|
|
* simpler frameworks do. This is due to the fact that CoreAudio pulls
|
|
|
|
* audio samples rather than having them pushed at it (which is nice
|
2008-06-06 02:45:35 +02:00
|
|
|
* when you are wanting to do good buffering of audio).
|
2003-05-21 23:15:46 +02:00
|
|
|
*/
|
|
|
|
|
2005-06-05 16:47:26 +02:00
|
|
|
#include <CoreServices/CoreServices.h>
|
|
|
|
#include <AudioUnit/AudioUnit.h>
|
|
|
|
#include <AudioToolbox/AudioToolbox.h>
|
2003-05-21 23:15:46 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2005-03-03 14:46:54 +01:00
|
|
|
#include <stdlib.h>
|
2003-05-21 23:15:46 +02:00
|
|
|
#include <inttypes.h>
|
2007-10-11 04:00:05 +02:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
2003-05-21 23:15:46 +02:00
|
|
|
|
2005-01-12 23:00:02 +01:00
|
|
|
#include "config.h"
|
2012-11-09 01:06:43 +01:00
|
|
|
#include "core/mp_msg.h"
|
2003-05-21 23:15:46 +02:00
|
|
|
|
2012-11-09 01:06:43 +01:00
|
|
|
#include "ao.h"
|
|
|
|
#include "audio/format.h"
|
2007-10-11 04:00:05 +02:00
|
|
|
#include "osdep/timer.h"
|
2012-11-09 01:06:43 +01:00
|
|
|
#include "core/subopt-helper.h"
|
2013-05-23 21:23:32 +02:00
|
|
|
#include "core/mp_ring.h"
|
2013-04-20 10:02:23 +02:00
|
|
|
|
2013-06-18 21:46:32 +02:00
|
|
|
#define ca_msg(a, b ...) mp_msg(MSGT_AO, a, "AO: [coreaudio] " b)
|
2003-05-21 23:15:46 +02:00
|
|
|
|
2013-04-19 23:04:11 +02:00
|
|
|
static void audio_pause(struct ao *ao);
|
|
|
|
static void audio_resume(struct ao *ao);
|
|
|
|
static void reset(struct ao *ao);
|
2003-05-22 18:12:54 +02:00
|
|
|
|
2013-05-23 21:23:32 +02:00
|
|
|
static void print_buffer(struct mp_ring *buffer)
|
2013-04-20 10:02:23 +02:00
|
|
|
{
|
|
|
|
void *tctx = talloc_new(NULL);
|
2013-06-18 21:46:32 +02:00
|
|
|
ca_msg(MSGL_V, "%s\n", mp_ring_repr(buffer, tctx));
|
2013-04-20 10:02:23 +02:00
|
|
|
talloc_free(tctx);
|
|
|
|
}
|
|
|
|
|
2011-01-07 22:30:14 +01:00
|
|
|
|
2013-04-19 23:04:11 +02:00
|
|
|
struct priv
|
2003-05-21 23:15:46 +02:00
|
|
|
{
|
2013-04-18 21:25:02 +02:00
|
|
|
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;
|
|
|
|
|
2013-05-23 21:23:32 +02:00
|
|
|
struct mp_ring *buffer;
|
2013-04-19 23:04:11 +02:00
|
|
|
};
|
2003-05-21 23:15:46 +02:00
|
|
|
|
2013-06-16 19:25:10 +02:00
|
|
|
static int get_ring_size(struct ao *ao)
|
|
|
|
{
|
|
|
|
return af_fmt_seconds_to_bytes(
|
|
|
|
ao->format, 0.5, ao->channels.num, ao->samplerate);
|
|
|
|
}
|
|
|
|
|
2013-06-17 23:03:19 +02:00
|
|
|
static OSStatus render_cb_lpcm(void *ctx, AudioUnitRenderActionFlags *aflags,
|
|
|
|
const AudioTimeStamp *ts, UInt32 bus,
|
|
|
|
UInt32 frames, AudioBufferList *buffer_list)
|
2003-05-21 23:15:46 +02:00
|
|
|
{
|
2013-06-17 23:03:19 +02:00
|
|
|
struct ao *ao = ctx;
|
|
|
|
struct priv *p = ao->priv;
|
|
|
|
int requested = frames * p->packetSize;
|
|
|
|
AudioBuffer buf = buffer_list->mBuffers[0];
|
2013-04-19 23:04:11 +02:00
|
|
|
|
2013-06-17 23:03:19 +02:00
|
|
|
buf.mDataByteSize = mp_ring_read(p->buffer, buf.mData, requested);
|
2005-06-05 16:47:26 +02:00
|
|
|
|
2013-04-18 21:25:02 +02:00
|
|
|
return noErr;
|
2003-05-21 23:15:46 +02:00
|
|
|
}
|
|
|
|
|
2013-06-20 23:22:56 +02:00
|
|
|
static OSStatus render_cb_digital(
|
|
|
|
AudioDeviceID device, const AudioTimeStamp *ts,
|
|
|
|
const void *in_data, const AudioTimeStamp *in_ts,
|
|
|
|
AudioBufferList *out_data, const AudioTimeStamp *out_ts, void *ctx)
|
|
|
|
{
|
|
|
|
struct ao *ao = ctx;
|
|
|
|
struct priv *p = ao->priv;
|
|
|
|
AudioBuffer buf = out_data->mBuffers[p->i_stream_index];
|
|
|
|
int requested = buf.mDataByteSize;
|
|
|
|
|
2013-06-21 13:43:59 +02:00
|
|
|
if (p->b_muted)
|
2013-06-20 23:22:56 +02:00
|
|
|
mp_ring_drain(p->buffer, requested);
|
2013-06-21 13:43:59 +02:00
|
|
|
else
|
|
|
|
mp_ring_read(p->buffer, buf.mData, requested);
|
2013-06-20 23:22:56 +02:00
|
|
|
|
|
|
|
return noErr;
|
|
|
|
}
|
|
|
|
|
2013-04-19 23:04:11 +02:00
|
|
|
static int control(struct ao *ao, enum aocontrol cmd, void *arg)
|
2013-04-18 21:25:02 +02:00
|
|
|
{
|
2013-04-19 23:04:11 +02:00
|
|
|
struct priv *p = ao->priv;
|
2013-04-18 21:25:02 +02:00
|
|
|
ao_control_vol_t *control_vol;
|
|
|
|
OSStatus err;
|
|
|
|
Float32 vol;
|
|
|
|
switch (cmd) {
|
|
|
|
case AOCONTROL_GET_VOLUME:
|
|
|
|
control_vol = (ao_control_vol_t *)arg;
|
2013-04-19 23:04:11 +02:00
|
|
|
if (p->b_digital) {
|
2013-04-18 21:25:02 +02:00
|
|
|
// Digital output has no volume adjust.
|
2013-04-19 23:04:11 +02:00
|
|
|
int vol = p->b_muted ? 0 : 100;
|
2013-04-18 21:25:02 +02:00
|
|
|
*control_vol = (ao_control_vol_t) {
|
|
|
|
.left = vol, .right = vol,
|
|
|
|
};
|
|
|
|
return CONTROL_TRUE;
|
|
|
|
}
|
2013-04-19 23:04:11 +02:00
|
|
|
err = AudioUnitGetParameter(p->theOutputUnit, kHALOutputParam_Volume,
|
2013-04-18 21:25:02 +02:00
|
|
|
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 {
|
2013-06-18 21:46:32 +02:00
|
|
|
ca_msg(MSGL_WARN,
|
2013-04-18 21:25:02 +02:00
|
|
|
"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;
|
|
|
|
|
2013-04-19 23:04:11 +02:00
|
|
|
if (p->b_digital) {
|
2013-04-18 21:25:02 +02:00
|
|
|
// 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)
|
2013-04-19 23:04:11 +02:00
|
|
|
p->b_muted = 1;
|
2013-04-18 21:25:02 +02:00
|
|
|
else
|
2013-04-19 23:04:11 +02:00
|
|
|
p->b_muted = 0;
|
2013-04-18 21:25:02 +02:00
|
|
|
return CONTROL_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
vol = (control_vol->left + control_vol->right) * 4.0 / 200.0;
|
2013-04-19 23:04:11 +02:00
|
|
|
err = AudioUnitSetParameter(p->theOutputUnit, kHALOutputParam_Volume,
|
2013-04-18 21:25:02 +02:00
|
|
|
kAudioUnitScope_Global, 0, vol, 0);
|
|
|
|
if (err == 0) {
|
|
|
|
// printf("SET VOL=%f\n", vol);
|
|
|
|
return CONTROL_TRUE;
|
|
|
|
} else {
|
2013-06-18 21:46:32 +02:00
|
|
|
ca_msg(MSGL_WARN,
|
2013-04-18 21:25:02 +02:00
|
|
|
"could not set HAL output volume: [%4.4s]\n", (char *)&err);
|
|
|
|
return CONTROL_FALSE;
|
|
|
|
}
|
|
|
|
/* Everything is currently unimplemented */
|
|
|
|
default:
|
|
|
|
return CONTROL_FALSE;
|
|
|
|
}
|
2009-05-13 04:58:57 +02:00
|
|
|
|
2003-05-21 23:15:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-04-18 21:25:02 +02:00
|
|
|
static void print_format(int lev, const char *str,
|
|
|
|
const AudioStreamBasicDescription *f)
|
|
|
|
{
|
|
|
|
uint32_t flags = (uint32_t) f->mFormatFlags;
|
2013-06-18 21:46:32 +02:00
|
|
|
ca_msg(lev,
|
2013-04-18 21:25:02 +02:00
|
|
|
"%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" : "");
|
2005-01-01 04:51:08 +01:00
|
|
|
}
|
|
|
|
|
2010-07-11 23:00:37 +02:00
|
|
|
static OSStatus GetAudioProperty(AudioObjectID id,
|
|
|
|
AudioObjectPropertySelector selector,
|
|
|
|
UInt32 outSize, void *outData)
|
|
|
|
{
|
|
|
|
AudioObjectPropertyAddress property_address;
|
|
|
|
|
|
|
|
property_address.mSelector = selector;
|
|
|
|
property_address.mScope = kAudioObjectPropertyScopeGlobal;
|
|
|
|
property_address.mElement = kAudioObjectPropertyElementMaster;
|
|
|
|
|
2013-04-18 21:25:02 +02:00
|
|
|
return AudioObjectGetPropertyData(id, &property_address, 0, NULL, &outSize,
|
|
|
|
outData);
|
2010-07-11 23:00:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static UInt32 GetAudioPropertyArray(AudioObjectID id,
|
|
|
|
AudioObjectPropertySelector selector,
|
|
|
|
AudioObjectPropertyScope scope,
|
|
|
|
void **outData)
|
|
|
|
{
|
|
|
|
OSStatus err;
|
|
|
|
AudioObjectPropertyAddress property_address;
|
|
|
|
UInt32 i_param_size;
|
|
|
|
|
|
|
|
property_address.mSelector = selector;
|
|
|
|
property_address.mScope = scope;
|
|
|
|
property_address.mElement = kAudioObjectPropertyElementMaster;
|
|
|
|
|
2013-04-18 21:25:02 +02:00
|
|
|
err = AudioObjectGetPropertyDataSize(id, &property_address, 0, NULL,
|
|
|
|
&i_param_size);
|
2010-07-11 23:00:37 +02:00
|
|
|
|
|
|
|
if (err != noErr)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
*outData = malloc(i_param_size);
|
|
|
|
|
|
|
|
|
2013-04-18 21:25:02 +02:00
|
|
|
err = AudioObjectGetPropertyData(id, &property_address, 0, NULL,
|
|
|
|
&i_param_size, *outData);
|
2010-07-11 23:00:37 +02:00
|
|
|
|
|
|
|
if (err != noErr) {
|
|
|
|
free(*outData);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return i_param_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
static UInt32 GetGlobalAudioPropertyArray(AudioObjectID id,
|
|
|
|
AudioObjectPropertySelector selector,
|
|
|
|
void **outData)
|
|
|
|
{
|
2013-04-18 21:25:02 +02:00
|
|
|
return GetAudioPropertyArray(id, selector, kAudioObjectPropertyScopeGlobal,
|
|
|
|
outData);
|
2010-07-11 23:00:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static OSStatus GetAudioPropertyString(AudioObjectID id,
|
|
|
|
AudioObjectPropertySelector selector,
|
|
|
|
char **outData)
|
|
|
|
{
|
|
|
|
OSStatus err;
|
|
|
|
AudioObjectPropertyAddress property_address;
|
|
|
|
UInt32 i_param_size;
|
|
|
|
CFStringRef string;
|
|
|
|
CFIndex string_length;
|
|
|
|
|
|
|
|
property_address.mSelector = selector;
|
|
|
|
property_address.mScope = kAudioObjectPropertyScopeGlobal;
|
|
|
|
property_address.mElement = kAudioObjectPropertyElementMaster;
|
|
|
|
|
|
|
|
i_param_size = sizeof(CFStringRef);
|
2013-04-18 21:25:02 +02:00
|
|
|
err = AudioObjectGetPropertyData(id, &property_address, 0, NULL,
|
|
|
|
&i_param_size, &string);
|
2010-07-11 23:00:37 +02:00
|
|
|
if (err != noErr)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
string_length = CFStringGetMaximumSizeForEncoding(CFStringGetLength(string),
|
|
|
|
kCFStringEncodingASCII);
|
|
|
|
*outData = malloc(string_length + 1);
|
2013-04-18 21:25:02 +02:00
|
|
|
CFStringGetCString(string, *outData, string_length + 1,
|
|
|
|
kCFStringEncodingASCII);
|
2010-07-11 23:00:37 +02:00
|
|
|
|
|
|
|
CFRelease(string);
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
static OSStatus SetAudioProperty(AudioObjectID id,
|
|
|
|
AudioObjectPropertySelector selector,
|
|
|
|
UInt32 inDataSize, void *inData)
|
|
|
|
{
|
|
|
|
AudioObjectPropertyAddress property_address;
|
|
|
|
|
|
|
|
property_address.mSelector = selector;
|
|
|
|
property_address.mScope = kAudioObjectPropertyScopeGlobal;
|
|
|
|
property_address.mElement = kAudioObjectPropertyElementMaster;
|
|
|
|
|
2013-04-18 21:25:02 +02:00
|
|
|
return AudioObjectSetPropertyData(id, &property_address, 0, NULL,
|
|
|
|
inDataSize, inData);
|
2010-07-11 23:00:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static Boolean IsAudioPropertySettable(AudioObjectID id,
|
|
|
|
AudioObjectPropertySelector selector,
|
|
|
|
Boolean *outData)
|
|
|
|
{
|
|
|
|
AudioObjectPropertyAddress property_address;
|
|
|
|
|
|
|
|
property_address.mSelector = selector;
|
|
|
|
property_address.mScope = kAudioObjectPropertyScopeGlobal;
|
|
|
|
property_address.mElement = kAudioObjectPropertyElementMaster;
|
|
|
|
|
|
|
|
return AudioObjectIsPropertySettable(id, &property_address, outData);
|
|
|
|
}
|
2005-01-01 04:51:08 +01:00
|
|
|
|
2013-04-18 21:25:02 +02:00
|
|
|
static int AudioDeviceSupportsDigital(AudioDeviceID i_dev_id);
|
|
|
|
static int AudioStreamSupportsDigital(AudioStreamID i_stream_id);
|
2013-04-19 23:04:11 +02:00
|
|
|
static int OpenSPDIF(struct ao *ao);
|
2013-04-18 21:25:02 +02:00
|
|
|
static int AudioStreamChangeFormat(AudioStreamID i_stream_id,
|
|
|
|
AudioStreamBasicDescription change_format);
|
|
|
|
static OSStatus StreamListener(AudioObjectID inObjectID,
|
|
|
|
UInt32 inNumberAddresses,
|
|
|
|
const AudioObjectPropertyAddress inAddresses[],
|
|
|
|
void *inClientData);
|
|
|
|
static OSStatus DeviceListener(AudioObjectID inObjectID,
|
|
|
|
UInt32 inNumberAddresses,
|
|
|
|
const AudioObjectPropertyAddress inAddresses[],
|
|
|
|
void *inClientData);
|
2007-10-11 04:00:05 +02:00
|
|
|
|
2010-07-11 23:08:57 +02:00
|
|
|
static void print_help(void)
|
|
|
|
{
|
|
|
|
OSStatus err;
|
|
|
|
UInt32 i_param_size;
|
|
|
|
int num_devices;
|
|
|
|
AudioDeviceID *devids;
|
|
|
|
char *device_name;
|
|
|
|
|
2013-06-18 21:46:32 +02:00
|
|
|
ca_msg(MSGL_FATAL,
|
2010-07-11 23:08:57 +02:00
|
|
|
"\n-ao coreaudio commandline help:\n"
|
2012-10-11 02:04:08 +02:00
|
|
|
"Example: mpv -ao coreaudio:device_id=266\n"
|
2010-07-11 23:08:57 +02:00
|
|
|
" open Core Audio with output device ID 266.\n"
|
|
|
|
"\nOptions:\n"
|
|
|
|
" device_id\n"
|
|
|
|
" ID of output device to use (0 = default device)\n"
|
|
|
|
" help\n"
|
|
|
|
" This help including list of available devices.\n"
|
|
|
|
"\n"
|
|
|
|
"Available output devices:\n");
|
|
|
|
|
2013-04-18 21:25:02 +02:00
|
|
|
i_param_size = GetGlobalAudioPropertyArray(kAudioObjectSystemObject,
|
|
|
|
kAudioHardwarePropertyDevices,
|
|
|
|
(void **)&devids);
|
2010-07-11 23:08:57 +02:00
|
|
|
|
|
|
|
if (!i_param_size) {
|
2013-06-18 21:46:32 +02:00
|
|
|
ca_msg(MSGL_FATAL, "Failed to get list of output devices.\n");
|
2010-07-11 23:08:57 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
num_devices = i_param_size / sizeof(AudioDeviceID);
|
|
|
|
|
|
|
|
for (int i = 0; i < num_devices; ++i) {
|
2013-04-18 21:25:02 +02:00
|
|
|
err = GetAudioPropertyString(devids[i], kAudioObjectPropertyName,
|
|
|
|
&device_name);
|
2010-07-11 23:08:57 +02:00
|
|
|
|
|
|
|
if (err == noErr) {
|
2013-06-18 21:46:32 +02:00
|
|
|
ca_msg(MSGL_FATAL, "%s (id: %" PRIu32 ")\n", device_name, devids[i]);
|
2010-07-11 23:08:57 +02:00
|
|
|
free(device_name);
|
|
|
|
} else
|
2013-06-18 21:46:32 +02:00
|
|
|
ca_msg(MSGL_FATAL, "Unknown (id: %" PRIu32 ")\n", devids[i]);
|
2010-07-11 23:08:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
free(devids);
|
|
|
|
}
|
|
|
|
|
2013-04-19 23:04:11 +02:00
|
|
|
static int init(struct ao *ao, char *params)
|
2003-05-21 23:15:46 +02:00
|
|
|
{
|
2013-04-19 23:04:11 +02:00
|
|
|
// int rate, int channels, int format, int flags)
|
|
|
|
struct priv *p = talloc_zero(ao, struct priv);
|
|
|
|
ao->priv = p;
|
|
|
|
|
2013-04-18 21:25:02 +02:00
|
|
|
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;
|
2010-07-11 23:08:57 +02:00
|
|
|
|
|
|
|
const opt_t subopts[] = {
|
2013-04-18 21:25:02 +02:00
|
|
|
{"device_id", OPT_ARG_INT, &device_id, NULL},
|
|
|
|
{"help", OPT_ARG_BOOL, &display_help, NULL},
|
2010-07-11 23:08:57 +02:00
|
|
|
{NULL}
|
|
|
|
};
|
|
|
|
|
|
|
|
// set defaults
|
|
|
|
device_id = 0;
|
|
|
|
|
2010-07-12 22:19:09 +02:00
|
|
|
if (subopt_parse(ao_subdevice, subopts) != 0 || display_help) {
|
2010-07-11 23:08:57 +02:00
|
|
|
print_help();
|
2010-07-12 22:19:09 +02:00
|
|
|
if (!display_help)
|
|
|
|
return 0;
|
2010-07-11 23:08:57 +02:00
|
|
|
}
|
2007-10-11 04:00:05 +02:00
|
|
|
|
2013-06-18 21:46:32 +02:00
|
|
|
ca_msg(MSGL_V, "init([%dHz][%dch][%s][%d])\n",
|
2013-04-19 23:04:11 +02:00
|
|
|
ao->samplerate, ao->channels.num, af_fmt2str_short(ao->format), 0);
|
2005-06-05 16:47:26 +02:00
|
|
|
|
2013-04-19 23:04:11 +02:00
|
|
|
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;
|
2007-10-11 04:00:05 +02:00
|
|
|
|
2013-04-19 23:04:11 +02:00
|
|
|
ao->per_application_mixer = true;
|
|
|
|
ao->no_persistent_volume = true;
|
2012-01-06 16:25:52 +01:00
|
|
|
|
2010-07-11 23:08:57 +02:00
|
|
|
if (device_id == 0) {
|
2007-10-11 04:00:05 +02:00
|
|
|
/* Find the ID of the default Device. */
|
2010-07-11 23:00:37 +02:00
|
|
|
err = GetAudioProperty(kAudioObjectSystemObject,
|
|
|
|
kAudioHardwarePropertyDefaultOutputDevice,
|
|
|
|
sizeof(UInt32), &devid_def);
|
2013-04-18 21:25:02 +02:00
|
|
|
if (err != noErr) {
|
2013-06-18 21:46:32 +02:00
|
|
|
ca_msg(MSGL_WARN,
|
2013-04-18 21:25:02 +02:00
|
|
|
"could not get default audio device: [%4.4s]\n",
|
|
|
|
(char *)&err);
|
2007-12-04 13:38:31 +01:00
|
|
|
goto err_out;
|
2007-10-11 04:00:05 +02:00
|
|
|
}
|
2013-04-19 23:04:11 +02:00
|
|
|
} else {
|
2010-07-11 23:08:57 +02:00
|
|
|
devid_def = device_id;
|
2013-04-19 23:04:11 +02:00
|
|
|
}
|
2007-10-11 04:00:05 +02:00
|
|
|
|
2010-07-11 23:08:57 +02:00
|
|
|
/* Retrieve the name of the device. */
|
|
|
|
err = GetAudioPropertyString(devid_def,
|
|
|
|
kAudioObjectPropertyName,
|
|
|
|
&psz_name);
|
2013-04-18 21:25:02 +02:00
|
|
|
if (err != noErr) {
|
2013-06-18 21:46:32 +02:00
|
|
|
ca_msg(MSGL_WARN,
|
2013-04-18 21:25:02 +02:00
|
|
|
"could not get default audio device name: [%4.4s]\n",
|
|
|
|
(char *)&err);
|
2010-07-11 23:08:57 +02:00
|
|
|
goto err_out;
|
|
|
|
}
|
2007-10-11 04:00:05 +02:00
|
|
|
|
2013-06-18 21:46:32 +02:00
|
|
|
ca_msg(MSGL_V,
|
2013-04-18 21:25:02 +02:00
|
|
|
"got audio output device ID: %" PRIu32 " Name: %s\n", devid_def,
|
|
|
|
psz_name);
|
2007-10-11 04:00:05 +02:00
|
|
|
|
2010-07-11 23:08:57 +02:00
|
|
|
/* Probe whether device support S/PDIF stream output if input is AC3 stream. */
|
2013-04-19 23:04:11 +02:00
|
|
|
if (AF_FORMAT_IS_AC3(ao->format)) {
|
2007-10-11 04:00:05 +02:00
|
|
|
if (AudioDeviceSupportsDigital(devid_def))
|
2013-04-19 23:04:11 +02:00
|
|
|
p->b_supports_digital = 1;
|
2013-06-18 21:46:32 +02:00
|
|
|
ca_msg(MSGL_V,
|
2010-08-18 00:39:24 +02:00
|
|
|
"probe default audio output device about support for digital s/pdif output: %d\n",
|
2013-04-19 23:04:11 +02:00
|
|
|
p->b_supports_digital);
|
2007-10-11 04:00:05 +02:00
|
|
|
}
|
2005-06-05 16:47:26 +02:00
|
|
|
|
2010-07-11 23:08:57 +02:00
|
|
|
free(psz_name);
|
|
|
|
|
|
|
|
// Save selected device id
|
2013-04-19 23:04:11 +02:00
|
|
|
p->i_selected_dev = devid_def;
|
2010-07-11 23:08:57 +02:00
|
|
|
|
2013-05-09 18:06:26 +02:00
|
|
|
struct mp_chmap_sel chmap_sel = {0};
|
|
|
|
mp_chmap_sel_add_waveext(&chmap_sel);
|
2013-04-19 23:04:11 +02:00
|
|
|
if (!ao_chmap_sel_adjust(ao, &chmap_sel, &ao->channels))
|
2013-05-09 18:06:26 +02:00
|
|
|
goto err_out;
|
|
|
|
|
2013-04-18 21:25:02 +02:00
|
|
|
// Build Description for the input format
|
2013-04-19 23:04:11 +02:00
|
|
|
inDesc.mSampleRate = ao->samplerate;
|
2013-04-18 21:25:02 +02:00
|
|
|
inDesc.mFormatID =
|
2013-04-19 23:04:11 +02:00
|
|
|
p->b_supports_digital ? kAudioFormat60958AC3 : kAudioFormatLinearPCM;
|
|
|
|
inDesc.mChannelsPerFrame = ao->channels.num;
|
|
|
|
inDesc.mBitsPerChannel = af_fmt2bits(ao->format);
|
2013-04-18 21:25:02 +02:00
|
|
|
|
2013-04-19 23:04:11 +02:00
|
|
|
if ((ao->format & AF_FORMAT_POINT_MASK) == AF_FORMAT_F) {
|
2013-04-18 21:25:02 +02:00
|
|
|
// float
|
|
|
|
inDesc.mFormatFlags = kAudioFormatFlagIsFloat |
|
|
|
|
kAudioFormatFlagIsPacked;
|
2013-04-19 23:04:11 +02:00
|
|
|
} else if ((ao->format & AF_FORMAT_SIGN_MASK) == AF_FORMAT_SI) {
|
2013-04-18 21:25:02 +02:00
|
|
|
// signed int
|
|
|
|
inDesc.mFormatFlags = kAudioFormatFlagIsSignedInteger |
|
|
|
|
kAudioFormatFlagIsPacked;
|
|
|
|
} else {
|
|
|
|
// unsigned int
|
|
|
|
inDesc.mFormatFlags = kAudioFormatFlagIsPacked;
|
2005-06-05 16:47:26 +02:00
|
|
|
}
|
2013-04-19 23:04:11 +02:00
|
|
|
if ((ao->format & AF_FORMAT_END_MASK) == AF_FORMAT_BE)
|
2007-10-11 04:00:05 +02:00
|
|
|
inDesc.mFormatFlags |= kAudioFormatFlagIsBigEndian;
|
2005-06-05 16:47:26 +02:00
|
|
|
|
|
|
|
inDesc.mFramesPerPacket = 1;
|
2013-04-19 23:04:11 +02:00
|
|
|
p->packetSize = inDesc.mBytesPerPacket = inDesc.mBytesPerFrame =
|
2013-04-18 21:25:02 +02:00
|
|
|
inDesc.mFramesPerPacket *
|
2013-04-19 23:04:11 +02:00
|
|
|
ao->channels.num *
|
2013-04-18 21:25:02 +02:00
|
|
|
(inDesc.mBitsPerChannel / 8);
|
|
|
|
print_format(MSGL_V, "source:", &inDesc);
|
2007-10-11 04:00:05 +02:00
|
|
|
|
2013-04-19 23:04:11 +02:00
|
|
|
if (p->b_supports_digital) {
|
2007-10-11 04:00:05 +02:00
|
|
|
b_alive = 1;
|
2013-04-19 23:04:11 +02:00
|
|
|
err = GetAudioProperty(p->i_selected_dev,
|
2010-07-11 23:00:37 +02:00
|
|
|
kAudioDevicePropertyDeviceIsAlive,
|
|
|
|
sizeof(UInt32), &b_alive);
|
2007-10-11 04:00:05 +02:00
|
|
|
if (err != noErr)
|
2013-06-18 21:46:32 +02:00
|
|
|
ca_msg(MSGL_WARN,
|
2013-04-18 21:25:02 +02:00
|
|
|
"could not check whether device is alive: [%4.4s]\n",
|
|
|
|
(char *)&err);
|
2007-10-11 04:00:05 +02:00
|
|
|
if (!b_alive)
|
2013-06-18 21:46:32 +02:00
|
|
|
ca_msg(MSGL_WARN, "device is not alive\n");
|
2010-07-11 23:08:57 +02:00
|
|
|
|
2007-10-11 04:00:05 +02:00
|
|
|
/* S/PDIF output need device in HogMode. */
|
2013-04-19 23:04:11 +02:00
|
|
|
err = GetAudioProperty(p->i_selected_dev,
|
2010-07-11 23:00:37 +02:00
|
|
|
kAudioDevicePropertyHogMode,
|
2013-04-19 23:04:11 +02:00
|
|
|
sizeof(pid_t), &p->i_hog_pid);
|
2013-04-18 21:25:02 +02:00
|
|
|
if (err != noErr) {
|
2007-10-11 04:00:05 +02:00
|
|
|
/* This is not a fatal error. Some drivers simply don't support this property. */
|
2013-06-18 21:46:32 +02:00
|
|
|
ca_msg(MSGL_WARN,
|
2013-04-18 21:25:02 +02:00
|
|
|
"could not check whether device is hogged: [%4.4s]\n",
|
|
|
|
(char *)&err);
|
2013-04-19 23:04:11 +02:00
|
|
|
p->i_hog_pid = -1;
|
2007-10-11 04:00:05 +02:00
|
|
|
}
|
|
|
|
|
2013-04-19 23:04:11 +02:00
|
|
|
if (p->i_hog_pid != -1 && p->i_hog_pid != getpid()) {
|
2013-06-18 21:46:32 +02:00
|
|
|
ca_msg(MSGL_WARN,
|
2013-04-18 21:25:02 +02:00
|
|
|
"Selected audio device is exclusively in use by another program.\n");
|
2007-12-04 13:38:31 +01:00
|
|
|
goto err_out;
|
2007-10-11 04:00:05 +02:00
|
|
|
}
|
2013-04-19 23:04:11 +02:00
|
|
|
p->stream_format = inDesc;
|
|
|
|
return OpenSPDIF(ao);
|
2007-10-11 04:00:05 +02:00
|
|
|
}
|
2005-01-01 04:51:08 +01:00
|
|
|
|
2013-04-18 21:25:02 +02:00
|
|
|
/* 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) {
|
2013-06-18 21:46:32 +02:00
|
|
|
ca_msg(MSGL_WARN, "Unable to find Output Unit component\n");
|
2013-04-18 21:25:02 +02:00
|
|
|
goto err_out;
|
|
|
|
}
|
|
|
|
|
2013-04-19 23:04:11 +02:00
|
|
|
err = AudioComponentInstanceNew(comp, &(p->theOutputUnit)); //gains access to the services provided by the component
|
2013-04-18 21:25:02 +02:00
|
|
|
if (err) {
|
2013-06-18 21:46:32 +02:00
|
|
|
ca_msg(MSGL_WARN,
|
2013-04-18 21:25:02 +02:00
|
|
|
"Unable to open Output Unit component: [%4.4s]\n", (char *)&err);
|
|
|
|
goto err_out;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize AudioUnit
|
2013-04-19 23:04:11 +02:00
|
|
|
err = AudioUnitInitialize(p->theOutputUnit);
|
2013-04-18 21:25:02 +02:00
|
|
|
if (err) {
|
2013-06-18 21:46:32 +02:00
|
|
|
ca_msg(MSGL_WARN,
|
2013-04-18 21:25:02 +02:00
|
|
|
"Unable to initialize Output Unit component: [%4.4s]\n",
|
|
|
|
(char *)&err);
|
|
|
|
goto err_out1;
|
|
|
|
}
|
|
|
|
|
|
|
|
size = sizeof(AudioStreamBasicDescription);
|
2013-04-19 23:04:11 +02:00
|
|
|
err = AudioUnitSetProperty(p->theOutputUnit,
|
2013-04-18 21:25:02 +02:00
|
|
|
kAudioUnitProperty_StreamFormat,
|
|
|
|
kAudioUnitScope_Input, 0, &inDesc, size);
|
|
|
|
|
|
|
|
if (err) {
|
2013-06-18 21:46:32 +02:00
|
|
|
ca_msg(MSGL_WARN, "Unable to set the input format: [%4.4s]\n",
|
2013-04-18 21:25:02 +02:00
|
|
|
(char *)&err);
|
|
|
|
goto err_out2;
|
|
|
|
}
|
|
|
|
|
|
|
|
size = sizeof(UInt32);
|
2013-04-19 23:04:11 +02:00
|
|
|
err = AudioUnitGetProperty(p->theOutputUnit,
|
2013-04-18 21:25:02 +02:00
|
|
|
kAudioDevicePropertyBufferSize,
|
|
|
|
kAudioUnitScope_Input, 0, &maxFrames, &size);
|
|
|
|
|
|
|
|
if (err) {
|
2013-06-18 21:46:32 +02:00
|
|
|
ca_msg(MSGL_WARN,
|
2013-04-18 21:25:02 +02:00
|
|
|
"AudioUnitGetProperty returned [%4.4s] when getting kAudioDevicePropertyBufferSize\n",
|
|
|
|
(char *)&err);
|
|
|
|
goto err_out2;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Set the Current Device to the Default Output Unit.
|
2013-04-19 23:04:11 +02:00
|
|
|
err = AudioUnitSetProperty(p->theOutputUnit,
|
2013-04-18 21:25:02 +02:00
|
|
|
kAudioOutputUnitProperty_CurrentDevice,
|
2013-04-19 23:04:11 +02:00
|
|
|
kAudioUnitScope_Global, 0, &p->i_selected_dev,
|
|
|
|
sizeof(p->i_selected_dev));
|
2013-04-18 21:25:02 +02:00
|
|
|
|
2013-04-19 23:04:11 +02:00
|
|
|
ao->samplerate = inDesc.mSampleRate;
|
2013-04-18 21:25:02 +02:00
|
|
|
|
2013-04-19 23:04:11 +02:00
|
|
|
if (!ao_chmap_sel_get_def(ao, &chmap_sel, &ao->channels,
|
2013-04-18 21:25:02 +02:00
|
|
|
inDesc.mChannelsPerFrame))
|
|
|
|
goto err_out2;
|
2006-08-18 03:19:19 +02:00
|
|
|
|
2013-04-19 23:04:11 +02:00
|
|
|
ao->bps = ao->samplerate * inDesc.mBytesPerFrame;
|
2013-06-16 19:25:10 +02:00
|
|
|
p->buffer = mp_ring_new(p, get_ring_size(ao));
|
2009-05-13 04:58:57 +02:00
|
|
|
|
2013-04-20 10:02:23 +02:00
|
|
|
print_buffer(p->buffer);
|
2005-06-05 16:47:26 +02:00
|
|
|
|
2013-06-17 23:03:19 +02:00
|
|
|
renderCallback.inputProc = render_cb_lpcm;
|
2013-04-19 23:04:11 +02:00
|
|
|
renderCallback.inputProcRefCon = ao;
|
|
|
|
err = AudioUnitSetProperty(p->theOutputUnit,
|
2013-04-18 21:25:02 +02:00
|
|
|
kAudioUnitProperty_SetRenderCallback,
|
|
|
|
kAudioUnitScope_Input, 0, &renderCallback,
|
|
|
|
sizeof(AURenderCallbackStruct));
|
|
|
|
if (err) {
|
2013-06-18 21:46:32 +02:00
|
|
|
ca_msg(MSGL_WARN,
|
2013-04-18 21:25:02 +02:00
|
|
|
"Unable to set the render callback: [%4.4s]\n", (char *)&err);
|
|
|
|
goto err_out2;
|
|
|
|
}
|
2003-05-21 23:15:46 +02:00
|
|
|
|
2013-04-19 23:04:11 +02:00
|
|
|
reset(ao);
|
2009-05-13 04:58:57 +02:00
|
|
|
|
2003-05-21 23:15:46 +02:00
|
|
|
return CONTROL_OK;
|
2007-12-04 13:38:31 +01:00
|
|
|
|
|
|
|
err_out2:
|
2013-04-19 23:04:11 +02:00
|
|
|
AudioUnitUninitialize(p->theOutputUnit);
|
2007-12-04 13:38:31 +01:00
|
|
|
err_out1:
|
2013-04-19 23:04:11 +02:00
|
|
|
AudioComponentInstanceDispose(p->theOutputUnit);
|
2007-12-04 13:38:31 +01:00
|
|
|
err_out:
|
2009-05-13 04:58:57 +02:00
|
|
|
return CONTROL_FALSE;
|
2003-05-21 23:15:46 +02:00
|
|
|
}
|
|
|
|
|
2007-10-11 04:00:05 +02:00
|
|
|
/*****************************************************************************
|
2013-04-18 21:25:02 +02:00
|
|
|
* Setup a encoded digital stream (SPDIF)
|
|
|
|
*****************************************************************************/
|
2013-04-19 23:04:11 +02:00
|
|
|
static int OpenSPDIF(struct ao *ao)
|
2007-10-11 04:00:05 +02:00
|
|
|
{
|
2013-04-19 23:04:11 +02:00
|
|
|
struct priv *p = ao->priv;
|
2013-04-18 21:25:02 +02:00
|
|
|
OSStatus err = noErr;
|
|
|
|
UInt32 i_param_size, b_mix = 0;
|
|
|
|
Boolean b_writeable = 0;
|
|
|
|
AudioStreamID *p_streams = NULL;
|
|
|
|
int i, i_streams = 0;
|
|
|
|
AudioObjectPropertyAddress property_address;
|
2007-10-11 04:00:05 +02:00
|
|
|
|
|
|
|
/* Start doing the SPDIF setup process. */
|
2013-04-19 23:04:11 +02:00
|
|
|
p->b_digital = 1;
|
2007-10-11 04:00:05 +02:00
|
|
|
|
|
|
|
/* Hog the device. */
|
2013-04-19 23:04:11 +02:00
|
|
|
p->i_hog_pid = getpid();
|
2007-10-11 04:00:05 +02:00
|
|
|
|
2013-04-19 23:04:11 +02:00
|
|
|
err = SetAudioProperty(p->i_selected_dev,
|
2010-07-11 23:00:37 +02:00
|
|
|
kAudioDevicePropertyHogMode,
|
2013-04-19 23:04:11 +02:00
|
|
|
sizeof(p->i_hog_pid), &p->i_hog_pid);
|
2013-04-18 21:25:02 +02:00
|
|
|
if (err != noErr) {
|
2013-06-18 21:46:32 +02:00
|
|
|
ca_msg(MSGL_WARN, "failed to set hogmode: [%4.4s]\n",
|
2013-04-18 21:25:02 +02:00
|
|
|
(char *)&err);
|
2013-04-19 23:04:11 +02:00
|
|
|
p->i_hog_pid = -1;
|
2007-12-04 13:38:31 +01:00
|
|
|
goto err_out;
|
2007-10-11 04:00:05 +02:00
|
|
|
}
|
|
|
|
|
2011-08-19 15:20:43 +02:00
|
|
|
property_address.mSelector = kAudioDevicePropertySupportsMixing;
|
|
|
|
property_address.mScope = kAudioObjectPropertyScopeGlobal;
|
|
|
|
property_address.mElement = kAudioObjectPropertyElementMaster;
|
|
|
|
|
2007-10-11 04:00:05 +02:00
|
|
|
/* Set mixable to false if we are allowed to. */
|
2013-04-19 23:04:11 +02:00
|
|
|
if (AudioObjectHasProperty(p->i_selected_dev, &property_address)) {
|
2011-08-19 15:20:43 +02:00
|
|
|
/* Set mixable to false if we are allowed to. */
|
2013-04-19 23:04:11 +02:00
|
|
|
err = IsAudioPropertySettable(p->i_selected_dev,
|
2011-08-19 15:20:43 +02:00
|
|
|
kAudioDevicePropertySupportsMixing,
|
|
|
|
&b_writeable);
|
2013-04-19 23:04:11 +02:00
|
|
|
err = GetAudioProperty(p->i_selected_dev,
|
2010-07-11 23:00:37 +02:00
|
|
|
kAudioDevicePropertySupportsMixing,
|
|
|
|
sizeof(UInt32), &b_mix);
|
2013-04-18 21:25:02 +02:00
|
|
|
if (err == noErr && b_writeable) {
|
2011-08-19 15:20:43 +02:00
|
|
|
b_mix = 0;
|
2013-04-19 23:04:11 +02:00
|
|
|
err = SetAudioProperty(p->i_selected_dev,
|
2011-08-19 15:20:43 +02:00
|
|
|
kAudioDevicePropertySupportsMixing,
|
|
|
|
sizeof(UInt32), &b_mix);
|
2013-04-19 23:04:11 +02:00
|
|
|
p->b_changed_mixing = 1;
|
2011-08-19 15:20:43 +02:00
|
|
|
}
|
2013-04-18 21:25:02 +02:00
|
|
|
if (err != noErr) {
|
2013-06-18 21:46:32 +02:00
|
|
|
ca_msg(MSGL_WARN, "failed to set mixmode: [%4.4s]\n",
|
2013-04-18 21:25:02 +02:00
|
|
|
(char *)&err);
|
2011-08-19 15:20:43 +02:00
|
|
|
goto err_out;
|
|
|
|
}
|
2007-10-11 04:00:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Get a list of all the streams on this device. */
|
2013-04-19 23:04:11 +02:00
|
|
|
i_param_size = GetAudioPropertyArray(p->i_selected_dev,
|
2010-07-11 23:00:37 +02:00
|
|
|
kAudioDevicePropertyStreams,
|
|
|
|
kAudioDevicePropertyScopeOutput,
|
|
|
|
(void **)&p_streams);
|
2007-10-11 04:00:05 +02:00
|
|
|
|
2010-07-11 23:00:37 +02:00
|
|
|
if (!i_param_size) {
|
2013-06-18 21:46:32 +02:00
|
|
|
ca_msg(MSGL_WARN, "could not get number of streams.\n");
|
2007-12-04 13:38:31 +01:00
|
|
|
goto err_out;
|
2007-10-11 04:00:05 +02:00
|
|
|
}
|
|
|
|
|
2010-07-11 23:00:37 +02:00
|
|
|
i_streams = i_param_size / sizeof(AudioStreamID);
|
2007-10-11 04:00:05 +02:00
|
|
|
|
2013-06-18 21:46:32 +02:00
|
|
|
ca_msg(MSGL_V, "current device stream number: %d\n", i_streams);
|
2007-10-11 04:00:05 +02:00
|
|
|
|
2013-04-19 23:04:11 +02:00
|
|
|
for (i = 0; i < i_streams && p->i_stream_index < 0; ++i) {
|
2007-10-11 04:00:05 +02:00
|
|
|
/* Find a stream with a cac3 stream. */
|
2011-08-19 15:20:43 +02:00
|
|
|
AudioStreamRangedDescription *p_format_list = NULL;
|
2007-10-11 04:00:05 +02:00
|
|
|
int i_formats = 0, j = 0, b_digital = 0;
|
|
|
|
|
2010-07-11 23:00:37 +02:00
|
|
|
i_param_size = GetGlobalAudioPropertyArray(p_streams[i],
|
2011-08-19 15:20:43 +02:00
|
|
|
kAudioStreamPropertyAvailablePhysicalFormats,
|
2010-07-11 23:00:37 +02:00
|
|
|
(void **)&p_format_list);
|
2007-10-11 04:00:05 +02:00
|
|
|
|
2010-07-11 23:00:37 +02:00
|
|
|
if (!i_param_size) {
|
2013-06-18 21:46:32 +02:00
|
|
|
ca_msg(MSGL_WARN,
|
2010-08-18 00:39:24 +02:00
|
|
|
"Could not get number of stream formats.\n");
|
2007-10-11 04:00:05 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2011-08-19 15:20:43 +02:00
|
|
|
i_formats = i_param_size / sizeof(AudioStreamRangedDescription);
|
2007-10-11 04:00:05 +02:00
|
|
|
|
|
|
|
/* Check if one of the supported formats is a digital format. */
|
2013-04-18 21:25:02 +02:00
|
|
|
for (j = 0; j < i_formats; ++j) {
|
|
|
|
if (p_format_list[j].mFormat.mFormatID == 'IAC3' ||
|
|
|
|
p_format_list[j].mFormat.mFormatID == 'iac3' ||
|
2011-08-19 15:20:43 +02:00
|
|
|
p_format_list[j].mFormat.mFormatID == kAudioFormat60958AC3 ||
|
2013-04-18 21:25:02 +02:00
|
|
|
p_format_list[j].mFormat.mFormatID == kAudioFormatAC3) {
|
2007-10-11 04:00:05 +02:00
|
|
|
b_digital = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-18 21:25:02 +02:00
|
|
|
if (b_digital) {
|
2007-10-11 04:00:05 +02:00
|
|
|
/* If this stream supports a digital (cac3) format, then set it. */
|
|
|
|
int i_requested_rate_format = -1;
|
|
|
|
int i_current_rate_format = -1;
|
|
|
|
int i_backup_rate_format = -1;
|
|
|
|
|
2013-04-19 23:04:11 +02:00
|
|
|
p->i_stream_id = p_streams[i];
|
|
|
|
p->i_stream_index = i;
|
2007-10-11 04:00:05 +02:00
|
|
|
|
2013-04-19 23:04:11 +02:00
|
|
|
if (p->b_revert == 0) {
|
2007-10-11 04:00:05 +02:00
|
|
|
/* Retrieve the original format of this stream first if not done so already. */
|
2013-04-19 23:04:11 +02:00
|
|
|
err = GetAudioProperty(p->i_stream_id,
|
2010-07-11 23:00:37 +02:00
|
|
|
kAudioStreamPropertyPhysicalFormat,
|
2013-04-19 23:04:11 +02:00
|
|
|
sizeof(p->sfmt_revert),
|
|
|
|
&p->sfmt_revert);
|
2013-04-18 21:25:02 +02:00
|
|
|
if (err != noErr) {
|
2013-06-18 21:46:32 +02:00
|
|
|
ca_msg(MSGL_WARN,
|
2010-08-18 00:39:24 +02:00
|
|
|
"Could not retrieve the original stream format: [%4.4s]\n",
|
|
|
|
(char *)&err);
|
2010-11-14 10:12:34 +01:00
|
|
|
free(p_format_list);
|
2007-10-11 04:00:05 +02:00
|
|
|
continue;
|
|
|
|
}
|
2013-04-19 23:04:11 +02:00
|
|
|
p->b_revert = 1;
|
2007-10-11 04:00:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for (j = 0; j < i_formats; ++j)
|
2013-04-18 21:25:02 +02:00
|
|
|
if (p_format_list[j].mFormat.mFormatID == 'IAC3' ||
|
|
|
|
p_format_list[j].mFormat.mFormatID == 'iac3' ||
|
|
|
|
p_format_list[j].mFormat.mFormatID ==
|
|
|
|
kAudioFormat60958AC3 ||
|
|
|
|
p_format_list[j].mFormat.mFormatID == kAudioFormatAC3) {
|
|
|
|
if (p_format_list[j].mFormat.mSampleRate ==
|
2013-04-19 23:04:11 +02:00
|
|
|
p->stream_format.mSampleRate) {
|
2007-10-11 04:00:05 +02:00
|
|
|
i_requested_rate_format = j;
|
|
|
|
break;
|
|
|
|
}
|
2013-04-18 21:25:02 +02:00
|
|
|
if (p_format_list[j].mFormat.mSampleRate ==
|
2013-04-19 23:04:11 +02:00
|
|
|
p->sfmt_revert.mSampleRate)
|
2007-10-11 04:00:05 +02:00
|
|
|
i_current_rate_format = j;
|
2013-04-18 21:25:02 +02:00
|
|
|
else if (i_backup_rate_format < 0 ||
|
|
|
|
p_format_list[j].mFormat.mSampleRate >
|
|
|
|
p_format_list[i_backup_rate_format].mFormat.
|
|
|
|
mSampleRate)
|
2007-10-11 04:00:05 +02:00
|
|
|
i_backup_rate_format = j;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i_requested_rate_format >= 0) /* We prefer to output at the samplerate of the original audio. */
|
2013-04-19 23:04:11 +02:00
|
|
|
p->stream_format =
|
2013-04-18 21:25:02 +02:00
|
|
|
p_format_list[i_requested_rate_format].mFormat;
|
2007-10-11 04:00:05 +02:00
|
|
|
else if (i_current_rate_format >= 0) /* If not possible, we will try to use the current samplerate of the device. */
|
2013-04-19 23:04:11 +02:00
|
|
|
p->stream_format =
|
2013-04-18 21:25:02 +02:00
|
|
|
p_format_list[i_current_rate_format].mFormat;
|
|
|
|
else
|
2013-04-19 23:04:11 +02:00
|
|
|
p->stream_format = p_format_list[i_backup_rate_format].mFormat;
|
2013-04-18 21:25:02 +02:00
|
|
|
/* And if we have to, any digital format will be just fine (highest rate possible). */
|
2007-10-11 04:00:05 +02:00
|
|
|
}
|
2010-11-14 10:12:34 +01:00
|
|
|
free(p_format_list);
|
2007-10-11 04:00:05 +02:00
|
|
|
}
|
2010-11-14 10:12:34 +01:00
|
|
|
free(p_streams);
|
2007-10-11 04:00:05 +02:00
|
|
|
|
2013-04-19 23:04:11 +02:00
|
|
|
if (p->i_stream_index < 0) {
|
2013-06-18 21:46:32 +02:00
|
|
|
ca_msg(MSGL_WARN,
|
2010-08-18 00:39:24 +02:00
|
|
|
"Cannot find any digital output stream format when OpenSPDIF().\n");
|
2007-12-04 13:38:31 +01:00
|
|
|
goto err_out;
|
2007-10-11 04:00:05 +02:00
|
|
|
}
|
|
|
|
|
2013-04-19 23:04:11 +02:00
|
|
|
print_format(MSGL_V, "original stream format:", &p->sfmt_revert);
|
2007-10-11 04:00:05 +02:00
|
|
|
|
2013-04-19 23:04:11 +02:00
|
|
|
if (!AudioStreamChangeFormat(p->i_stream_id, p->stream_format))
|
2007-12-04 13:38:31 +01:00
|
|
|
goto err_out;
|
2007-10-11 04:00:05 +02:00
|
|
|
|
2010-07-11 23:00:37 +02:00
|
|
|
property_address.mSelector = kAudioDevicePropertyDeviceHasChanged;
|
|
|
|
property_address.mScope = kAudioObjectPropertyScopeGlobal;
|
|
|
|
property_address.mElement = kAudioObjectPropertyElementMaster;
|
|
|
|
|
2013-04-19 23:04:11 +02:00
|
|
|
err = AudioObjectAddPropertyListener(p->i_selected_dev,
|
2010-07-11 23:00:37 +02:00
|
|
|
&property_address,
|
2007-10-11 04:00:05 +02:00
|
|
|
DeviceListener,
|
|
|
|
NULL);
|
|
|
|
if (err != noErr)
|
2013-06-18 21:46:32 +02:00
|
|
|
ca_msg(MSGL_WARN,
|
2013-04-18 21:25:02 +02:00
|
|
|
"AudioDeviceAddPropertyListener for kAudioDevicePropertyDeviceHasChanged failed: [%4.4s]\n",
|
|
|
|
(char *)&err);
|
2007-10-11 04:00:05 +02:00
|
|
|
|
|
|
|
|
|
|
|
/* FIXME: If output stream is not native byte-order, we need change endian somewhere. */
|
|
|
|
/* Although there's no such case reported. */
|
Remove compile time/runtime CPU detection, and drop some platforms
mplayer had three ways of enabling CPU specific assembler routines:
a) Enable them at compile time; crash if the CPU can't handle it.
b) Enable them at compile time, but let the configure script detect
your CPU. Your binary will only crash if you try to run it on a
different system that has less features than yours.
This was the default, I think.
c) Runtime detection.
The implementation of b) and c) suck. a) is not really feasible (it
sucks for users). Remove all code related to this, and use libav's CPU
detection instead. Now the configure script will always enable CPU
specific features, and disable them at runtime if libav reports them
not as available.
One implication is that now the compiler is always expected to handle
SSE (etc.) inline assembly at runtime, unless it's explicitly disabled.
Only checks for x86 CPU specific features are kept, the rest is either
unused or barely used.
Get rid of all the dump -mpcu, -march etc. flags. Trust the compiler
to select decent settings.
Get rid of support for the following operating systems:
- BSD/OS (some ancient BSD fork)
- QNX (don't care)
- BeOS (dead, Haiku support is still welcome)
- AIX (don't care)
- HP-UX (don't care)
- OS/2 (dead, actual support has been removed a while ago)
Remove the configure code for detecting the endianness. Instead, use
the standard header <endian.h>, which can be used if _GNU_SOURCE or
_BSD_SOURCE is defined. (Maybe these changes should have been in a
separate commit.)
Since this is a quite violent code removal orgy, and I'm testing only
on x86 32 bit Linux, expect regressions.
2012-07-29 17:20:57 +02:00
|
|
|
#if BYTE_ORDER == BIG_ENDIAN
|
2013-04-19 23:04:11 +02:00
|
|
|
if (!(p->stream_format.mFormatFlags & kAudioFormatFlagIsBigEndian))
|
2007-10-11 04:00:05 +02:00
|
|
|
#else
|
2010-10-08 10:56:20 +02:00
|
|
|
/* tell mplayer that we need a byteswap on AC3 streams, */
|
2013-04-19 23:04:11 +02:00
|
|
|
if (p->stream_format.mFormatID & kAudioFormat60958AC3)
|
|
|
|
ao->format = AF_FORMAT_AC3_LE;
|
2010-10-08 10:56:20 +02:00
|
|
|
|
2013-04-19 23:04:11 +02:00
|
|
|
if (p->stream_format.mFormatFlags & kAudioFormatFlagIsBigEndian)
|
2007-10-11 04:00:05 +02:00
|
|
|
#endif
|
2013-06-18 21:46:32 +02:00
|
|
|
ca_msg(MSGL_WARN,
|
2010-08-18 00:39:24 +02:00
|
|
|
"Output stream has non-native byte order, digital output may fail.\n");
|
2007-10-11 04:00:05 +02:00
|
|
|
|
2013-04-18 21:25:02 +02:00
|
|
|
|
2013-04-19 23:04:11 +02:00
|
|
|
ao->samplerate = p->stream_format.mSampleRate;
|
|
|
|
mp_chmap_from_channels(&ao->channels, p->stream_format.mChannelsPerFrame);
|
|
|
|
ao->bps = ao->samplerate *
|
|
|
|
(p->stream_format.mBytesPerPacket /
|
|
|
|
p->stream_format.mFramesPerPacket);
|
2007-10-11 04:00:05 +02:00
|
|
|
|
2013-06-16 19:25:10 +02:00
|
|
|
p->buffer = mp_ring_new(p, get_ring_size(ao));
|
2007-10-11 04:00:05 +02:00
|
|
|
|
2013-04-20 10:02:23 +02:00
|
|
|
print_buffer(p->buffer);
|
2007-10-11 04:00:05 +02:00
|
|
|
|
2010-07-11 23:00:37 +02:00
|
|
|
/* Create IOProc callback. */
|
2013-04-19 23:04:11 +02:00
|
|
|
err = AudioDeviceCreateIOProcID(p->i_selected_dev,
|
2013-06-20 23:22:56 +02:00
|
|
|
(AudioDeviceIOProc)render_cb_digital,
|
2010-07-11 23:00:37 +02:00
|
|
|
(void *)ao,
|
2013-04-19 23:04:11 +02:00
|
|
|
&p->renderCallback);
|
2010-07-11 23:00:37 +02:00
|
|
|
|
2013-04-19 23:04:11 +02:00
|
|
|
if (err != noErr || p->renderCallback == NULL) {
|
2013-06-18 21:46:32 +02:00
|
|
|
ca_msg(MSGL_WARN, "AudioDeviceAddIOProc failed: [%4.4s]\n",
|
2013-04-18 21:25:02 +02:00
|
|
|
(char *)&err);
|
2007-12-04 13:38:31 +01:00
|
|
|
goto err_out1;
|
2007-10-11 04:00:05 +02:00
|
|
|
}
|
|
|
|
|
2013-04-19 23:04:11 +02:00
|
|
|
reset(ao);
|
2007-10-11 04:00:05 +02:00
|
|
|
|
|
|
|
return CONTROL_TRUE;
|
2007-12-04 13:38:31 +01:00
|
|
|
|
|
|
|
err_out1:
|
2013-04-19 23:04:11 +02:00
|
|
|
if (p->b_revert)
|
|
|
|
AudioStreamChangeFormat(p->i_stream_id, p->sfmt_revert);
|
2007-12-04 13:38:31 +01:00
|
|
|
err_out:
|
2013-04-19 23:04:11 +02:00
|
|
|
if (p->b_changed_mixing && p->sfmt_revert.mFormatID !=
|
2013-04-18 21:25:02 +02:00
|
|
|
kAudioFormat60958AC3) {
|
2007-12-04 13:38:31 +01:00
|
|
|
int b_mix = 1;
|
2013-04-19 23:04:11 +02:00
|
|
|
err = SetAudioProperty(p->i_selected_dev,
|
2010-07-11 23:00:37 +02:00
|
|
|
kAudioDevicePropertySupportsMixing,
|
|
|
|
sizeof(int), &b_mix);
|
2007-12-04 13:38:31 +01:00
|
|
|
if (err != noErr)
|
2013-06-18 21:46:32 +02:00
|
|
|
ca_msg(MSGL_WARN, "failed to set mixmode: [%4.4s]\n",
|
2007-12-04 13:38:31 +01:00
|
|
|
(char *)&err);
|
|
|
|
}
|
2013-04-19 23:04:11 +02:00
|
|
|
if (p->i_hog_pid == getpid()) {
|
|
|
|
p->i_hog_pid = -1;
|
|
|
|
err = SetAudioProperty(p->i_selected_dev,
|
2010-07-11 23:00:37 +02:00
|
|
|
kAudioDevicePropertyHogMode,
|
2013-04-19 23:04:11 +02:00
|
|
|
sizeof(p->i_hog_pid), &p->i_hog_pid);
|
2007-12-04 13:38:31 +01:00
|
|
|
if (err != noErr)
|
2013-06-18 21:46:32 +02:00
|
|
|
ca_msg(MSGL_WARN, "Could not release hogmode: [%4.4s]\n",
|
2007-12-04 13:38:31 +01:00
|
|
|
(char *)&err);
|
|
|
|
}
|
2009-05-13 04:58:57 +02:00
|
|
|
return CONTROL_FALSE;
|
2007-10-11 04:00:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*****************************************************************************
|
2013-04-18 21:25:02 +02:00
|
|
|
* AudioDeviceSupportsDigital: Check i_dev_id for digital stream support.
|
|
|
|
*****************************************************************************/
|
|
|
|
static int AudioDeviceSupportsDigital(AudioDeviceID i_dev_id)
|
2007-10-11 04:00:05 +02:00
|
|
|
{
|
2013-04-18 21:25:02 +02:00
|
|
|
UInt32 i_param_size = 0;
|
|
|
|
AudioStreamID *p_streams = NULL;
|
|
|
|
int i = 0, i_streams = 0;
|
|
|
|
int b_return = CONTROL_FALSE;
|
2007-10-11 04:00:05 +02:00
|
|
|
|
|
|
|
/* Retrieve all the output streams. */
|
2010-07-11 23:00:37 +02:00
|
|
|
i_param_size = GetAudioPropertyArray(i_dev_id,
|
|
|
|
kAudioDevicePropertyStreams,
|
|
|
|
kAudioDevicePropertyScopeOutput,
|
|
|
|
(void **)&p_streams);
|
2007-10-11 04:00:05 +02:00
|
|
|
|
2010-07-11 23:00:37 +02:00
|
|
|
if (!i_param_size) {
|
2013-06-18 21:46:32 +02:00
|
|
|
ca_msg(MSGL_WARN, "could not get number of streams.\n");
|
2007-10-11 04:00:05 +02:00
|
|
|
return CONTROL_FALSE;
|
|
|
|
}
|
|
|
|
|
2010-07-11 23:00:37 +02:00
|
|
|
i_streams = i_param_size / sizeof(AudioStreamID);
|
2007-10-11 04:00:05 +02:00
|
|
|
|
2013-04-18 21:25:02 +02:00
|
|
|
for (i = 0; i < i_streams; ++i) {
|
2007-10-11 04:00:05 +02:00
|
|
|
if (AudioStreamSupportsDigital(p_streams[i]))
|
|
|
|
b_return = CONTROL_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
free(p_streams);
|
|
|
|
return b_return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*****************************************************************************
|
2013-04-18 21:25:02 +02:00
|
|
|
* AudioStreamSupportsDigital: Check i_stream_id for digital stream support.
|
|
|
|
*****************************************************************************/
|
|
|
|
static int AudioStreamSupportsDigital(AudioStreamID i_stream_id)
|
2007-10-11 04:00:05 +02:00
|
|
|
{
|
|
|
|
UInt32 i_param_size;
|
2011-08-19 15:20:43 +02:00
|
|
|
AudioStreamRangedDescription *p_format_list = NULL;
|
2007-10-11 04:00:05 +02:00
|
|
|
int i, i_formats, b_return = CONTROL_FALSE;
|
|
|
|
|
|
|
|
/* Retrieve all the stream formats supported by each output stream. */
|
2010-07-11 23:00:37 +02:00
|
|
|
i_param_size = GetGlobalAudioPropertyArray(i_stream_id,
|
2011-08-19 15:20:43 +02:00
|
|
|
kAudioStreamPropertyAvailablePhysicalFormats,
|
2010-07-11 23:00:37 +02:00
|
|
|
(void **)&p_format_list);
|
2007-10-11 04:00:05 +02:00
|
|
|
|
2010-07-11 23:00:37 +02:00
|
|
|
if (!i_param_size) {
|
2013-06-18 21:46:32 +02:00
|
|
|
ca_msg(MSGL_WARN, "Could not get number of stream formats.\n");
|
2007-10-11 04:00:05 +02:00
|
|
|
return CONTROL_FALSE;
|
|
|
|
}
|
|
|
|
|
2011-08-19 15:20:43 +02:00
|
|
|
i_formats = i_param_size / sizeof(AudioStreamRangedDescription);
|
2007-10-11 04:00:05 +02:00
|
|
|
|
2013-04-18 21:25:02 +02:00
|
|
|
for (i = 0; i < i_formats; ++i) {
|
2011-08-19 15:20:43 +02:00
|
|
|
print_format(MSGL_V, "supported format:", &(p_format_list[i].mFormat));
|
2007-10-11 04:00:05 +02:00
|
|
|
|
2013-04-18 21:25:02 +02:00
|
|
|
if (p_format_list[i].mFormat.mFormatID == 'IAC3' ||
|
|
|
|
p_format_list[i].mFormat.mFormatID == 'iac3' ||
|
2011-08-19 15:20:43 +02:00
|
|
|
p_format_list[i].mFormat.mFormatID == kAudioFormat60958AC3 ||
|
|
|
|
p_format_list[i].mFormat.mFormatID == kAudioFormatAC3)
|
2007-10-11 04:00:05 +02:00
|
|
|
b_return = CONTROL_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
free(p_format_list);
|
|
|
|
return b_return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*****************************************************************************
|
2013-04-18 21:25:02 +02:00
|
|
|
* AudioStreamChangeFormat: Change i_stream_id to change_format
|
|
|
|
*****************************************************************************/
|
|
|
|
static int AudioStreamChangeFormat(AudioStreamID i_stream_id,
|
|
|
|
AudioStreamBasicDescription change_format)
|
2007-10-11 04:00:05 +02:00
|
|
|
{
|
|
|
|
OSStatus err = noErr;
|
|
|
|
int i;
|
2010-07-11 23:00:37 +02:00
|
|
|
AudioObjectPropertyAddress property_address;
|
2007-10-11 04:00:05 +02:00
|
|
|
|
2007-11-10 03:07:34 +01:00
|
|
|
static volatile int stream_format_changed;
|
|
|
|
stream_format_changed = 0;
|
2007-10-11 04:00:05 +02:00
|
|
|
|
|
|
|
print_format(MSGL_V, "setting stream format:", &change_format);
|
|
|
|
|
|
|
|
/* Install the callback. */
|
2010-07-11 23:00:37 +02:00
|
|
|
property_address.mSelector = kAudioStreamPropertyPhysicalFormat;
|
|
|
|
property_address.mScope = kAudioObjectPropertyScopeGlobal;
|
|
|
|
property_address.mElement = kAudioObjectPropertyElementMaster;
|
|
|
|
|
|
|
|
err = AudioObjectAddPropertyListener(i_stream_id,
|
|
|
|
&property_address,
|
2007-11-10 03:07:34 +01:00
|
|
|
StreamListener,
|
|
|
|
(void *)&stream_format_changed);
|
2013-04-18 21:25:02 +02:00
|
|
|
if (err != noErr) {
|
2013-06-18 21:46:32 +02:00
|
|
|
ca_msg(MSGL_WARN,
|
2013-04-18 21:25:02 +02:00
|
|
|
"AudioStreamAddPropertyListener failed: [%4.4s]\n",
|
|
|
|
(char *)&err);
|
2007-10-11 04:00:05 +02:00
|
|
|
return CONTROL_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Change the format. */
|
2010-07-11 23:00:37 +02:00
|
|
|
err = SetAudioProperty(i_stream_id,
|
|
|
|
kAudioStreamPropertyPhysicalFormat,
|
|
|
|
sizeof(AudioStreamBasicDescription), &change_format);
|
2013-04-18 21:25:02 +02:00
|
|
|
if (err != noErr) {
|
2013-06-18 21:46:32 +02:00
|
|
|
ca_msg(MSGL_WARN, "could not set the stream format: [%4.4s]\n",
|
2013-04-18 21:25:02 +02:00
|
|
|
(char *)&err);
|
2007-10-11 04:00:05 +02:00
|
|
|
return CONTROL_FALSE;
|
|
|
|
}
|
|
|
|
|
2007-11-10 03:07:34 +01:00
|
|
|
/* The AudioStreamSetProperty is not only asynchronious,
|
2007-10-11 04:00:05 +02:00
|
|
|
* it is also not Atomic, in its behaviour.
|
|
|
|
* Therefore we check 5 times before we really give up.
|
|
|
|
* FIXME: failing isn't actually implemented yet. */
|
2013-04-18 21:25:02 +02:00
|
|
|
for (i = 0; i < 5; ++i) {
|
2007-10-11 04:00:05 +02:00
|
|
|
AudioStreamBasicDescription actual_format;
|
2007-11-10 03:07:34 +01:00
|
|
|
int j;
|
|
|
|
for (j = 0; !stream_format_changed && j < 50; ++j)
|
2013-05-25 19:56:52 +02:00
|
|
|
mp_sleep_us(10000);
|
2007-11-10 03:07:34 +01:00
|
|
|
if (stream_format_changed)
|
|
|
|
stream_format_changed = 0;
|
|
|
|
else
|
2013-06-18 21:46:32 +02:00
|
|
|
ca_msg(MSGL_V, "reached timeout\n");
|
2007-10-11 04:00:05 +02:00
|
|
|
|
2010-07-11 23:00:37 +02:00
|
|
|
err = GetAudioProperty(i_stream_id,
|
|
|
|
kAudioStreamPropertyPhysicalFormat,
|
2013-04-18 21:25:02 +02:00
|
|
|
sizeof(AudioStreamBasicDescription),
|
|
|
|
&actual_format);
|
2007-10-11 04:00:05 +02:00
|
|
|
|
|
|
|
print_format(MSGL_V, "actual format in use:", &actual_format);
|
|
|
|
if (actual_format.mSampleRate == change_format.mSampleRate &&
|
|
|
|
actual_format.mFormatID == change_format.mFormatID &&
|
2013-04-18 21:25:02 +02:00
|
|
|
actual_format.mFramesPerPacket == change_format.mFramesPerPacket) {
|
2007-10-11 04:00:05 +02:00
|
|
|
/* The right format is now active. */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* We need to check again. */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Removing the property listener. */
|
2010-07-11 23:00:37 +02:00
|
|
|
err = AudioObjectRemovePropertyListener(i_stream_id,
|
|
|
|
&property_address,
|
|
|
|
StreamListener,
|
|
|
|
(void *)&stream_format_changed);
|
2013-04-18 21:25:02 +02:00
|
|
|
if (err != noErr) {
|
2013-06-18 21:46:32 +02:00
|
|
|
ca_msg(MSGL_WARN,
|
2013-04-18 21:25:02 +02:00
|
|
|
"AudioStreamRemovePropertyListener failed: [%4.4s]\n",
|
|
|
|
(char *)&err);
|
2007-10-11 04:00:05 +02:00
|
|
|
return CONTROL_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return CONTROL_TRUE;
|
|
|
|
}
|
|
|
|
|
2013-04-19 23:04:11 +02:00
|
|
|
static int play(struct ao *ao, void *output_samples, int num_bytes, int flags)
|
2009-05-13 04:58:57 +02:00
|
|
|
{
|
2013-04-19 23:04:11 +02:00
|
|
|
struct priv *p = ao->priv;
|
2007-10-11 04:00:05 +02:00
|
|
|
int wrote, b_digital;
|
|
|
|
|
|
|
|
// Check whether we need to reset the digital output stream.
|
2013-04-19 23:04:11 +02:00
|
|
|
if (p->b_digital && p->b_stream_format_changed) {
|
|
|
|
p->b_stream_format_changed = 0;
|
|
|
|
b_digital = AudioStreamSupportsDigital(p->i_stream_id);
|
2013-04-18 21:25:02 +02:00
|
|
|
if (b_digital) {
|
2010-08-18 00:39:24 +02:00
|
|
|
/* Current stream supports digital format output, let's set it. */
|
2013-06-18 21:46:32 +02:00
|
|
|
ca_msg(MSGL_V,
|
2010-08-18 00:39:24 +02:00
|
|
|
"Detected current stream supports digital, try to restore digital output...\n");
|
2007-10-11 04:00:05 +02:00
|
|
|
|
2013-04-19 23:04:11 +02:00
|
|
|
if (!AudioStreamChangeFormat(p->i_stream_id, p->stream_format))
|
2013-06-18 21:46:32 +02:00
|
|
|
ca_msg(MSGL_WARN,
|
2013-04-18 21:25:02 +02:00
|
|
|
"Restoring digital output failed.\n");
|
|
|
|
else {
|
2013-06-18 21:46:32 +02:00
|
|
|
ca_msg(MSGL_WARN,
|
2013-04-18 21:25:02 +02:00
|
|
|
"Restoring digital output succeeded.\n");
|
2013-04-19 23:04:11 +02:00
|
|
|
reset(ao);
|
2007-10-11 04:00:05 +02:00
|
|
|
}
|
2013-04-18 21:25:02 +02:00
|
|
|
} else
|
2013-06-18 21:46:32 +02:00
|
|
|
ca_msg(MSGL_V,
|
2013-04-18 21:25:02 +02:00
|
|
|
"Detected current stream does not support digital.\n");
|
2007-10-11 04:00:05 +02:00
|
|
|
}
|
2006-08-18 03:19:19 +02:00
|
|
|
|
2013-05-23 21:23:32 +02:00
|
|
|
wrote = mp_ring_write(p->buffer, output_samples, num_bytes);
|
2013-04-19 23:04:11 +02:00
|
|
|
audio_resume(ao);
|
2010-07-11 23:14:48 +02:00
|
|
|
|
2007-10-11 04:00:05 +02:00
|
|
|
return wrote;
|
2003-05-21 23:15:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* set variables and buffer to initial state */
|
2013-04-19 23:04:11 +02:00
|
|
|
static void reset(struct ao *ao)
|
2003-05-21 23:15:46 +02:00
|
|
|
{
|
2013-04-19 23:04:11 +02:00
|
|
|
struct priv *p = ao->priv;
|
|
|
|
audio_pause(ao);
|
2013-05-23 21:23:32 +02:00
|
|
|
mp_ring_reset(p->buffer);
|
2003-05-21 23:15:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* return available space */
|
2013-04-19 23:04:11 +02:00
|
|
|
static int get_space(struct ao *ao)
|
2003-05-21 23:15:46 +02:00
|
|
|
{
|
2013-04-19 23:04:11 +02:00
|
|
|
struct priv *p = ao->priv;
|
2013-05-23 21:23:32 +02:00
|
|
|
return mp_ring_available(p->buffer);
|
2003-05-21 23:15:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-05-22 18:12:54 +02:00
|
|
|
/* return delay until audio is played */
|
2013-04-19 23:04:11 +02:00
|
|
|
static float get_delay(struct ao *ao)
|
2003-05-21 23:15:46 +02:00
|
|
|
{
|
2013-04-18 21:25:02 +02:00
|
|
|
// inaccurate, should also contain the data buffered e.g. by the OS
|
2013-04-19 23:04:11 +02:00
|
|
|
struct priv *p = ao->priv;
|
2013-05-23 21:23:32 +02:00
|
|
|
return mp_ring_buffered(p->buffer) / (float)ao->bps;
|
2003-05-21 23:15:46 +02:00
|
|
|
}
|
|
|
|
|
2013-04-19 23:04:11 +02:00
|
|
|
static void uninit(struct ao *ao, bool immed)
|
2003-05-21 23:15:46 +02:00
|
|
|
{
|
2013-04-19 23:04:11 +02:00
|
|
|
struct priv *p = ao->priv;
|
2013-04-18 21:25:02 +02:00
|
|
|
OSStatus err = noErr;
|
|
|
|
|
|
|
|
if (!immed) {
|
|
|
|
long long timeleft =
|
2013-05-23 21:23:32 +02:00
|
|
|
(1000000LL * mp_ring_buffered(p->buffer)) / ao->bps;
|
2013-06-18 21:46:32 +02:00
|
|
|
ca_msg(MSGL_DBG2, "%d bytes left @%d bps (%d usec)\n",
|
2013-05-23 21:23:32 +02:00
|
|
|
mp_ring_buffered(p->buffer), ao->bps, (int)timeleft);
|
2013-04-18 21:25:02 +02:00
|
|
|
mp_sleep_us((int)timeleft);
|
|
|
|
}
|
|
|
|
|
2013-04-19 23:04:11 +02:00
|
|
|
if (!p->b_digital) {
|
|
|
|
AudioOutputUnitStop(p->theOutputUnit);
|
|
|
|
AudioUnitUninitialize(p->theOutputUnit);
|
|
|
|
AudioComponentInstanceDispose(p->theOutputUnit);
|
2013-04-18 21:25:02 +02:00
|
|
|
} else {
|
|
|
|
/* Stop device. */
|
2013-04-19 23:04:11 +02:00
|
|
|
err = AudioDeviceStop(p->i_selected_dev, p->renderCallback);
|
2013-04-18 21:25:02 +02:00
|
|
|
if (err != noErr)
|
2013-06-18 21:46:32 +02:00
|
|
|
ca_msg(MSGL_WARN, "AudioDeviceStop failed: [%4.4s]\n",
|
2013-04-18 21:25:02 +02:00
|
|
|
(char *)&err);
|
|
|
|
|
|
|
|
/* Remove IOProc callback. */
|
|
|
|
err =
|
2013-04-19 23:04:11 +02:00
|
|
|
AudioDeviceDestroyIOProcID(p->i_selected_dev, p->renderCallback);
|
2013-04-18 21:25:02 +02:00
|
|
|
if (err != noErr)
|
2013-06-18 21:46:32 +02:00
|
|
|
ca_msg(MSGL_WARN,
|
2013-04-18 21:25:02 +02:00
|
|
|
"AudioDeviceRemoveIOProc failed: [%4.4s]\n", (char *)&err);
|
|
|
|
|
2013-04-19 23:04:11 +02:00
|
|
|
if (p->b_revert)
|
|
|
|
AudioStreamChangeFormat(p->i_stream_id, p->sfmt_revert);
|
2013-04-18 21:25:02 +02:00
|
|
|
|
2013-04-19 23:04:11 +02:00
|
|
|
if (p->b_changed_mixing && p->sfmt_revert.mFormatID !=
|
2013-04-18 21:25:02 +02:00
|
|
|
kAudioFormat60958AC3) {
|
|
|
|
UInt32 b_mix;
|
|
|
|
Boolean b_writeable = 0;
|
|
|
|
/* Revert mixable to true if we are allowed to. */
|
2013-04-19 23:04:11 +02:00
|
|
|
err = IsAudioPropertySettable(p->i_selected_dev,
|
2013-04-18 21:25:02 +02:00
|
|
|
kAudioDevicePropertySupportsMixing,
|
|
|
|
&b_writeable);
|
2013-04-19 23:04:11 +02:00
|
|
|
err = GetAudioProperty(p->i_selected_dev,
|
2013-04-18 21:25:02 +02:00
|
|
|
kAudioDevicePropertySupportsMixing,
|
|
|
|
sizeof(UInt32), &b_mix);
|
|
|
|
if (err == noErr && b_writeable) {
|
|
|
|
b_mix = 1;
|
2013-04-19 23:04:11 +02:00
|
|
|
err = SetAudioProperty(p->i_selected_dev,
|
2013-04-18 21:25:02 +02:00
|
|
|
kAudioDevicePropertySupportsMixing,
|
|
|
|
sizeof(UInt32), &b_mix);
|
|
|
|
}
|
|
|
|
if (err != noErr)
|
2013-06-18 21:46:32 +02:00
|
|
|
ca_msg(MSGL_WARN, "failed to set mixmode: [%4.4s]\n",
|
2013-04-18 21:25:02 +02:00
|
|
|
(char *)&err);
|
|
|
|
}
|
2013-04-19 23:04:11 +02:00
|
|
|
if (p->i_hog_pid == getpid()) {
|
|
|
|
p->i_hog_pid = -1;
|
|
|
|
err = SetAudioProperty(p->i_selected_dev,
|
2013-04-18 21:25:02 +02:00
|
|
|
kAudioDevicePropertyHogMode,
|
2013-04-19 23:04:11 +02:00
|
|
|
sizeof(p->i_hog_pid), &p->i_hog_pid);
|
2013-04-18 21:25:02 +02:00
|
|
|
if (err != noErr)
|
2013-06-18 21:46:32 +02:00
|
|
|
ca_msg(MSGL_WARN,
|
2013-04-18 21:25:02 +02:00
|
|
|
"Could not release hogmode: [%4.4s]\n", (char *)&err);
|
|
|
|
}
|
|
|
|
}
|
2003-05-21 23:15:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* stop playing, keep buffers (for pause) */
|
2013-04-19 23:04:11 +02:00
|
|
|
static void audio_pause(struct ao *ao)
|
2003-05-21 23:15:46 +02:00
|
|
|
{
|
2013-04-19 23:04:11 +02:00
|
|
|
struct priv *p = ao->priv;
|
2013-04-18 21:25:02 +02:00
|
|
|
OSErr err = noErr;
|
2007-10-11 04:00:05 +02:00
|
|
|
|
|
|
|
/* Stop callback. */
|
2013-04-19 23:04:11 +02:00
|
|
|
if (!p->b_digital) {
|
|
|
|
err = AudioOutputUnitStop(p->theOutputUnit);
|
2007-10-11 04:00:05 +02:00
|
|
|
if (err != noErr)
|
2013-06-18 21:46:32 +02:00
|
|
|
ca_msg(MSGL_WARN, "AudioOutputUnitStop returned [%4.4s]\n",
|
2013-04-18 21:25:02 +02:00
|
|
|
(char *)&err);
|
|
|
|
} else {
|
2013-04-19 23:04:11 +02:00
|
|
|
err = AudioDeviceStop(p->i_selected_dev, p->renderCallback);
|
2007-10-11 04:00:05 +02:00
|
|
|
if (err != noErr)
|
2013-06-18 21:46:32 +02:00
|
|
|
ca_msg(MSGL_WARN, "AudioDeviceStop failed: [%4.4s]\n",
|
2013-04-18 21:25:02 +02:00
|
|
|
(char *)&err);
|
2007-10-11 04:00:05 +02:00
|
|
|
}
|
2013-04-19 23:04:11 +02:00
|
|
|
p->paused = 1;
|
2003-05-21 23:15:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* resume playing, after audio_pause() */
|
2013-04-19 23:04:11 +02:00
|
|
|
static void audio_resume(struct ao *ao)
|
2003-05-21 23:15:46 +02:00
|
|
|
{
|
2013-04-19 23:04:11 +02:00
|
|
|
struct priv *p = ao->priv;
|
2013-04-18 21:25:02 +02:00
|
|
|
OSErr err = noErr;
|
2007-10-11 04:00:05 +02:00
|
|
|
|
2013-04-19 23:04:11 +02:00
|
|
|
if (!p->paused)
|
2007-10-11 04:00:05 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
/* Start callback. */
|
2013-04-19 23:04:11 +02:00
|
|
|
if (!p->b_digital) {
|
|
|
|
err = AudioOutputUnitStart(p->theOutputUnit);
|
2007-10-11 04:00:05 +02:00
|
|
|
if (err != noErr)
|
2013-06-18 21:46:32 +02:00
|
|
|
ca_msg(MSGL_WARN,
|
2013-04-18 21:25:02 +02:00
|
|
|
"AudioOutputUnitStart returned [%4.4s]\n", (char *)&err);
|
|
|
|
} else {
|
2013-04-19 23:04:11 +02:00
|
|
|
err = AudioDeviceStart(p->i_selected_dev, p->renderCallback);
|
2007-10-11 04:00:05 +02:00
|
|
|
if (err != noErr)
|
2013-06-18 21:46:32 +02:00
|
|
|
ca_msg(MSGL_WARN, "AudioDeviceStart failed: [%4.4s]\n",
|
2013-04-18 21:25:02 +02:00
|
|
|
(char *)&err);
|
2007-10-11 04:00:05 +02:00
|
|
|
}
|
2013-04-19 23:04:11 +02:00
|
|
|
p->paused = 0;
|
2007-10-11 04:00:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*****************************************************************************
|
2013-04-18 21:25:02 +02:00
|
|
|
* StreamListener
|
|
|
|
*****************************************************************************/
|
|
|
|
static OSStatus StreamListener(AudioObjectID inObjectID,
|
|
|
|
UInt32 inNumberAddresses,
|
|
|
|
const AudioObjectPropertyAddress inAddresses[],
|
|
|
|
void *inClientData)
|
2007-10-11 04:00:05 +02:00
|
|
|
{
|
2013-04-18 21:25:02 +02:00
|
|
|
for (int i = 0; i < inNumberAddresses; ++i) {
|
2010-07-11 23:00:37 +02:00
|
|
|
if (inAddresses[i].mSelector == kAudioStreamPropertyPhysicalFormat) {
|
2013-06-18 21:46:32 +02:00
|
|
|
ca_msg(MSGL_WARN,
|
2013-04-18 21:25:02 +02:00
|
|
|
"got notify kAudioStreamPropertyPhysicalFormat changed.\n");
|
2007-11-10 03:07:34 +01:00
|
|
|
if (inClientData)
|
|
|
|
*(volatile int *)inClientData = 1;
|
2007-10-11 04:00:05 +02:00
|
|
|
break;
|
2010-07-11 23:00:37 +02:00
|
|
|
}
|
2007-10-11 04:00:05 +02:00
|
|
|
}
|
|
|
|
return noErr;
|
|
|
|
}
|
|
|
|
|
2013-04-18 21:25:02 +02:00
|
|
|
static OSStatus DeviceListener(AudioObjectID inObjectID,
|
|
|
|
UInt32 inNumberAddresses,
|
|
|
|
const AudioObjectPropertyAddress inAddresses[],
|
|
|
|
void *inClientData)
|
2007-10-11 04:00:05 +02:00
|
|
|
{
|
2013-04-19 23:04:11 +02:00
|
|
|
struct ao *ao = inClientData;
|
|
|
|
struct priv *p = ao->priv;
|
|
|
|
|
2013-04-18 21:25:02 +02:00
|
|
|
for (int i = 0; i < inNumberAddresses; ++i) {
|
2010-07-11 23:00:37 +02:00
|
|
|
if (inAddresses[i].mSelector == kAudioDevicePropertyDeviceHasChanged) {
|
2013-06-18 21:46:32 +02:00
|
|
|
ca_msg(MSGL_WARN,
|
2013-04-18 21:25:02 +02:00
|
|
|
"got notify kAudioDevicePropertyDeviceHasChanged.\n");
|
2013-04-19 23:04:11 +02:00
|
|
|
p->b_stream_format_changed = 1;
|
2007-10-11 04:00:05 +02:00
|
|
|
break;
|
2010-07-11 23:00:37 +02:00
|
|
|
}
|
2007-10-11 04:00:05 +02:00
|
|
|
}
|
|
|
|
return noErr;
|
2003-05-21 23:15:46 +02:00
|
|
|
}
|
2013-04-19 23:04:11 +02:00
|
|
|
|
|
|
|
const struct ao_driver audio_out_coreaudio = {
|
|
|
|
.info = &(const struct ao_info) {
|
|
|
|
"CoreAudio (Native OS X Audio Output)",
|
|
|
|
"coreaudio",
|
|
|
|
"Timothy J. Wood, Dan Christiansen, Chris Roccati & Stefano Pigozzi",
|
|
|
|
"",
|
|
|
|
},
|
|
|
|
.uninit = uninit,
|
|
|
|
.init = init,
|
|
|
|
.play = play,
|
|
|
|
.control = control,
|
|
|
|
.get_space = get_space,
|
|
|
|
.get_delay = get_delay,
|
|
|
|
.reset = reset,
|
|
|
|
.pause = audio_pause,
|
|
|
|
.resume = audio_resume,
|
|
|
|
};
|