2013-06-20 11:57:05 +00:00
|
|
|
/*
|
|
|
|
* This file is part of mpv.
|
|
|
|
*
|
|
|
|
* mpv 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.
|
|
|
|
*
|
|
|
|
* mpv 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.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with mpv. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file contains functions interacting with the CoreAudio framework
|
|
|
|
* that are not specific to the AUHAL. These are split in a separate file for
|
|
|
|
* the sake of readability. In the future the could be used by other AOs based
|
|
|
|
* on CoreAudio but not the AUHAL (such as using AudioQueue services).
|
|
|
|
*/
|
|
|
|
|
2013-07-13 07:48:10 +00:00
|
|
|
#include "audio/out/ao_coreaudio_utils.h"
|
|
|
|
#include "audio/out/ao_coreaudio_properties.h"
|
2013-06-26 06:16:34 +00:00
|
|
|
#include "osdep/timer.h"
|
2014-09-23 19:04:37 +00:00
|
|
|
#include "osdep/endian.h"
|
2014-07-02 06:02:00 +00:00
|
|
|
#include "audio/format.h"
|
2013-06-20 11:57:05 +00:00
|
|
|
|
2014-10-12 10:11:32 +00:00
|
|
|
void ca_get_device_list(struct ao *ao, struct ao_device_list *list)
|
2014-06-30 17:09:03 +00:00
|
|
|
{
|
|
|
|
AudioDeviceID *devs;
|
|
|
|
size_t n_devs;
|
|
|
|
OSStatus err =
|
|
|
|
CA_GET_ARY(kAudioObjectSystemObject, kAudioHardwarePropertyDevices,
|
|
|
|
&devs, &n_devs);
|
|
|
|
CHECK_CA_ERROR("Failed to get list of output devices.");
|
|
|
|
for (int i = 0; i < n_devs; i++) {
|
2014-10-12 10:11:32 +00:00
|
|
|
char name[32];
|
|
|
|
char *desc;
|
|
|
|
sprintf(name, "%d", devs[i]);
|
|
|
|
err = CA_GET_STR(devs[i], kAudioObjectPropertyName, &desc);
|
|
|
|
if (err != noErr)
|
|
|
|
desc = "Unknown";
|
|
|
|
ao_device_list_add(list, ao, &(struct ao_device_desc){name, desc});
|
2014-06-30 17:09:03 +00:00
|
|
|
}
|
|
|
|
talloc_free(devs);
|
|
|
|
coreaudio_error:
|
2014-10-12 10:11:32 +00:00
|
|
|
return;
|
2014-06-30 17:09:03 +00:00
|
|
|
}
|
|
|
|
|
2014-10-12 10:11:32 +00:00
|
|
|
OSStatus ca_select_device(struct ao *ao, char* name, AudioDeviceID *device)
|
2014-06-30 17:09:03 +00:00
|
|
|
{
|
|
|
|
OSStatus err = noErr;
|
2014-10-12 10:11:32 +00:00
|
|
|
int selection = name ? strtol(name, (char **)NULL, 10) : -1;
|
|
|
|
if (errno == EINVAL || errno == ERANGE) {
|
|
|
|
selection = -1;
|
|
|
|
MP_ERR(ao, "device identifier '%s' is invalid\n", name);
|
|
|
|
}
|
2014-06-30 17:09:03 +00:00
|
|
|
*device = 0;
|
|
|
|
if (selection < 0) {
|
|
|
|
// device not set by user, get the default one
|
|
|
|
err = CA_GET(kAudioObjectSystemObject,
|
|
|
|
kAudioHardwarePropertyDefaultOutputDevice,
|
|
|
|
device);
|
|
|
|
CHECK_CA_ERROR("could not get default audio device");
|
|
|
|
} else {
|
|
|
|
*device = selection;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mp_msg_test(ao->log, MSGL_V)) {
|
2014-10-12 10:11:32 +00:00
|
|
|
char *desc;
|
|
|
|
err = CA_GET_STR(*device, kAudioObjectPropertyName, &desc);
|
2014-06-30 17:09:03 +00:00
|
|
|
CHECK_CA_ERROR("could not get selected audio device name");
|
|
|
|
|
|
|
|
MP_VERBOSE(ao, "selected audio output device: %s (%" PRIu32 ")\n",
|
2014-10-12 10:11:32 +00:00
|
|
|
desc, *device);
|
2014-06-30 17:09:03 +00:00
|
|
|
|
2014-10-12 10:11:32 +00:00
|
|
|
talloc_free(desc);
|
2014-06-30 17:09:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
coreaudio_error:
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2013-07-13 07:48:10 +00:00
|
|
|
char *fourcc_repr(void *talloc_ctx, uint32_t code)
|
2013-06-20 11:57:05 +00:00
|
|
|
{
|
|
|
|
// Extract FourCC letters from the uint32_t and finde out if it's a valid
|
|
|
|
// code that is made of letters.
|
2014-07-01 21:10:38 +00:00
|
|
|
unsigned char fcc[4] = {
|
2013-06-20 11:57:05 +00:00
|
|
|
(code >> 24) & 0xFF,
|
|
|
|
(code >> 16) & 0xFF,
|
|
|
|
(code >> 8) & 0xFF,
|
|
|
|
code & 0xFF,
|
|
|
|
};
|
|
|
|
|
|
|
|
bool valid_fourcc = true;
|
|
|
|
for (int i = 0; i < 4; i++)
|
2014-07-01 21:10:38 +00:00
|
|
|
if (fcc[i] >= 32 && fcc[i] < 128)
|
2013-06-20 11:57:05 +00:00
|
|
|
valid_fourcc = false;
|
|
|
|
|
|
|
|
char *repr;
|
|
|
|
if (valid_fourcc)
|
|
|
|
repr = talloc_asprintf(talloc_ctx, "'%c%c%c%c'",
|
|
|
|
fcc[0], fcc[1], fcc[2], fcc[3]);
|
|
|
|
else
|
|
|
|
repr = talloc_asprintf(NULL, "%d", code);
|
|
|
|
|
|
|
|
return repr;
|
|
|
|
}
|
|
|
|
|
2013-08-01 14:47:36 +00:00
|
|
|
bool check_ca_st(struct ao *ao, int level, OSStatus code, const char *message)
|
2013-06-20 11:57:05 +00:00
|
|
|
{
|
|
|
|
if (code == noErr) return true;
|
|
|
|
|
|
|
|
char *error_string = fourcc_repr(NULL, code);
|
2013-12-21 20:49:13 +00:00
|
|
|
mp_msg(ao->log, level, "%s (%s)\n", message, error_string);
|
2013-06-20 11:57:05 +00:00
|
|
|
talloc_free(error_string);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-07-02 06:02:00 +00:00
|
|
|
void ca_fill_asbd(struct ao *ao, AudioStreamBasicDescription *asbd)
|
|
|
|
{
|
|
|
|
asbd->mSampleRate = ao->samplerate;
|
audio: cleanup spdif format definitions
Before this commit, there was AF_FORMAT_AC3 (the original spdif format,
used for AC3 and DTS core), and AF_FORMAT_IEC61937 (used for AC3, DTS
and DTS-HD), which was handled as some sort of superset for
AF_FORMAT_AC3. There also was AF_FORMAT_MPEG2, which used
IEC61937-framing, but still was handled as something "separate".
Technically, all of them are pretty similar, but may use different
bitrates. Since digital passthrough pretends to be PCM (just with
special headers that wrap digital packets), this is easily detectable by
the higher samplerate or higher number of channels, so I don't know why
you'd need a separate "class" of sample formats (AF_FORMAT_AC3 vs.
AF_FORMAT_IEC61937) to distinguish them. Actually, this whole thing is
just a mess.
Simplify this by handling all these formats the same way.
AF_FORMAT_IS_IEC61937() now returns 1 for all spdif formats (even MP3).
All AOs just accept all spdif formats now - whether that works or not is
not really clear (seems inconsistent due to earlier attempts to make
DTS-HD work). But on the other hand, enabling spdif requires manual user
interaction, so it doesn't matter much if initialization fails in
slightly less graceful ways if it can't work at all.
At a later point, we will support passthrough with ao_pulse. It seems
the PulseAudio API wants to know the codec type (or maybe not - feeding
it DTS while telling it it's AC3 works), add separate formats for each
codecs. While this reminds of the earlier chaos, it's stricter, and most
code just uses AF_FORMAT_IS_IEC61937().
Also, modify AF_FORMAT_TYPE_MASK (renamed from AF_FORMAT_POINT_MASK) to
include special formats, so that it always describes the fundamental
sample format type. This also ensures valid AF formats are never 0 (this
was probably broken in one of the earlier commits from today).
2014-09-23 20:44:54 +00:00
|
|
|
// Set "AC3" for other spdif formats too - unknown if that works.
|
|
|
|
asbd->mFormatID = AF_FORMAT_IS_IEC61937(ao->format) ?
|
2014-07-02 06:02:00 +00:00
|
|
|
kAudioFormat60958AC3 :
|
|
|
|
kAudioFormatLinearPCM;
|
|
|
|
asbd->mChannelsPerFrame = ao->channels.num;
|
|
|
|
asbd->mBitsPerChannel = af_fmt2bits(ao->format);
|
|
|
|
asbd->mFormatFlags = kAudioFormatFlagIsPacked;
|
|
|
|
|
audio: cleanup spdif format definitions
Before this commit, there was AF_FORMAT_AC3 (the original spdif format,
used for AC3 and DTS core), and AF_FORMAT_IEC61937 (used for AC3, DTS
and DTS-HD), which was handled as some sort of superset for
AF_FORMAT_AC3. There also was AF_FORMAT_MPEG2, which used
IEC61937-framing, but still was handled as something "separate".
Technically, all of them are pretty similar, but may use different
bitrates. Since digital passthrough pretends to be PCM (just with
special headers that wrap digital packets), this is easily detectable by
the higher samplerate or higher number of channels, so I don't know why
you'd need a separate "class" of sample formats (AF_FORMAT_AC3 vs.
AF_FORMAT_IEC61937) to distinguish them. Actually, this whole thing is
just a mess.
Simplify this by handling all these formats the same way.
AF_FORMAT_IS_IEC61937() now returns 1 for all spdif formats (even MP3).
All AOs just accept all spdif formats now - whether that works or not is
not really clear (seems inconsistent due to earlier attempts to make
DTS-HD work). But on the other hand, enabling spdif requires manual user
interaction, so it doesn't matter much if initialization fails in
slightly less graceful ways if it can't work at all.
At a later point, we will support passthrough with ao_pulse. It seems
the PulseAudio API wants to know the codec type (or maybe not - feeding
it DTS while telling it it's AC3 works), add separate formats for each
codecs. While this reminds of the earlier chaos, it's stricter, and most
code just uses AF_FORMAT_IS_IEC61937().
Also, modify AF_FORMAT_TYPE_MASK (renamed from AF_FORMAT_POINT_MASK) to
include special formats, so that it always describes the fundamental
sample format type. This also ensures valid AF formats are never 0 (this
was probably broken in one of the earlier commits from today).
2014-09-23 20:44:54 +00:00
|
|
|
if ((ao->format & AF_FORMAT_TYPE_MASK) == AF_FORMAT_F)
|
2014-07-02 06:02:00 +00:00
|
|
|
asbd->mFormatFlags |= kAudioFormatFlagIsFloat;
|
|
|
|
|
|
|
|
if ((ao->format & AF_FORMAT_SIGN_MASK) == AF_FORMAT_SI)
|
|
|
|
asbd->mFormatFlags |= kAudioFormatFlagIsSignedInteger;
|
|
|
|
|
2014-09-23 19:04:37 +00:00
|
|
|
if (BYTE_ORDER == BIG_ENDIAN)
|
2014-07-02 06:02:00 +00:00
|
|
|
asbd->mFormatFlags |= kAudioFormatFlagIsBigEndian;
|
|
|
|
|
|
|
|
asbd->mFramesPerPacket = 1;
|
|
|
|
asbd->mBytesPerPacket = asbd->mBytesPerFrame =
|
|
|
|
asbd->mFramesPerPacket * asbd->mChannelsPerFrame *
|
|
|
|
(asbd->mBitsPerChannel / 8);
|
|
|
|
}
|
|
|
|
|
2013-08-01 14:47:36 +00:00
|
|
|
void ca_print_asbd(struct ao *ao, const char *description,
|
2013-07-13 07:48:10 +00:00
|
|
|
const AudioStreamBasicDescription *asbd)
|
2013-06-20 11:57:05 +00:00
|
|
|
{
|
|
|
|
uint32_t flags = asbd->mFormatFlags;
|
|
|
|
char *format = fourcc_repr(NULL, asbd->mFormatID);
|
|
|
|
|
2013-08-01 14:47:36 +00:00
|
|
|
MP_VERBOSE(ao,
|
2013-06-20 11:57:05 +00:00
|
|
|
"%s %7.1fHz %" PRIu32 "bit [%s]"
|
|
|
|
"[%" PRIu32 "][%" PRIu32 "][%" PRIu32 "]"
|
|
|
|
"[%" PRIu32 "][%" PRIu32 "] "
|
|
|
|
"%s %s %s%s%s%s\n",
|
|
|
|
description, asbd->mSampleRate, asbd->mBitsPerChannel, format,
|
|
|
|
asbd->mFormatFlags, asbd->mBytesPerPacket, asbd->mFramesPerPacket,
|
|
|
|
asbd->mBytesPerFrame, asbd->mChannelsPerFrame,
|
|
|
|
(flags & kAudioFormatFlagIsFloat) ? "float" : "int",
|
|
|
|
(flags & kAudioFormatFlagIsBigEndian) ? "BE" : "LE",
|
|
|
|
(flags & kAudioFormatFlagIsSignedInteger) ? "S" : "U",
|
|
|
|
(flags & kAudioFormatFlagIsPacked) ? " packed" : "",
|
|
|
|
(flags & kAudioFormatFlagIsAlignedHigh) ? " aligned" : "",
|
|
|
|
(flags & kAudioFormatFlagIsNonInterleaved) ? " P" : "");
|
|
|
|
|
|
|
|
talloc_free(format);
|
|
|
|
}
|
|
|
|
|