2013-03-05 20:17:52 +00:00
|
|
|
/*
|
|
|
|
* GStreamer
|
|
|
|
* Copyright (C) 2012-2013 Fluendo S.A. <support@fluendo.com>
|
|
|
|
* Authors: Josep Torra Vallès <josep@fluendo.com>
|
|
|
|
* Andoni Morales Alastruey <amorales@fluendo.com>
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Library General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library 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
|
|
|
|
* Library General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this library; if not, write to the
|
|
|
|
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
* Boston, MA 02111-1307, USA.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "gstosxcoreaudio.h"
|
|
|
|
#include "gstosxcoreaudiocommon.h"
|
|
|
|
|
|
|
|
GST_DEBUG_CATEGORY_STATIC (osx_audio_debug);
|
|
|
|
#define GST_CAT_DEFAULT osx_audio_debug
|
|
|
|
|
|
|
|
G_DEFINE_TYPE (GstCoreAudio, gst_core_audio, G_TYPE_OBJECT);
|
|
|
|
|
2013-03-05 20:22:18 +00:00
|
|
|
#ifdef HAVE_IOS
|
|
|
|
#include "gstosxcoreaudioremoteio.c"
|
|
|
|
#else
|
2013-03-05 20:17:52 +00:00
|
|
|
#include "gstosxcoreaudiohal.c"
|
2013-03-05 20:22:18 +00:00
|
|
|
#endif
|
2013-03-05 20:17:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_core_audio_class_init (GstCoreAudioClass * klass)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_core_audio_init (GstCoreAudio * core_audio)
|
|
|
|
{
|
|
|
|
core_audio->is_passthrough = FALSE;
|
|
|
|
core_audio->device_id = kAudioDeviceUnknown;
|
|
|
|
core_audio->is_src = FALSE;
|
|
|
|
core_audio->audiounit = NULL;
|
2015-03-09 21:34:06 +00:00
|
|
|
core_audio->cached_caps = NULL;
|
2015-03-21 18:34:25 +00:00
|
|
|
core_audio->cached_caps_valid = FALSE;
|
2013-03-05 20:17:52 +00:00
|
|
|
#ifndef HAVE_IOS
|
|
|
|
core_audio->hog_pid = -1;
|
|
|
|
core_audio->disabled_mixing = FALSE;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2015-03-12 10:15:12 +00:00
|
|
|
static gboolean
|
|
|
|
_is_outer_scope (AudioUnitScope scope, AudioUnitElement element)
|
|
|
|
{
|
|
|
|
return
|
|
|
|
(scope == kAudioUnitScope_Input && element == 1) ||
|
|
|
|
(scope == kAudioUnitScope_Output && element == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
_audio_unit_property_listener (void *inRefCon, AudioUnit inUnit,
|
|
|
|
AudioUnitPropertyID inID, AudioUnitScope inScope,
|
|
|
|
AudioUnitElement inElement)
|
|
|
|
{
|
|
|
|
GstCoreAudio *core_audio;
|
|
|
|
|
|
|
|
core_audio = GST_CORE_AUDIO (inRefCon);
|
|
|
|
g_assert (inUnit == core_audio->audiounit);
|
|
|
|
|
|
|
|
switch (inID) {
|
|
|
|
case kAudioUnitProperty_AudioChannelLayout:
|
|
|
|
case kAudioUnitProperty_StreamFormat:
|
|
|
|
if (_is_outer_scope (inScope, inElement)) {
|
|
|
|
/* We don't push gst_event_new_caps here (for src),
|
|
|
|
* nor gst_event_new_reconfigure (for sink), since Core Audio continues
|
|
|
|
* to happily function with the old format, doing conversion/resampling
|
|
|
|
* as needed.
|
|
|
|
* This merely "refreshes" our PREFERRED caps. */
|
|
|
|
|
2015-03-21 18:34:25 +00:00
|
|
|
/* This function is called either from a Core Audio thread
|
|
|
|
* or as a result of a Core Audio API (e.g. AudioUnitInitialize)
|
|
|
|
* from our own thread. In the latter case, osxbuf can be
|
|
|
|
* already locked (GStreamer's mutex is not recursive).
|
|
|
|
* For this reason we use a boolean flag instead of nullifying
|
|
|
|
* cached_caps. */
|
|
|
|
core_audio->cached_caps_valid = FALSE;
|
2015-03-12 10:15:12 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-05 20:17:52 +00:00
|
|
|
/**************************
|
|
|
|
* Public API *
|
|
|
|
*************************/
|
|
|
|
|
|
|
|
GstCoreAudio *
|
|
|
|
gst_core_audio_new (GstObject * osxbuf)
|
|
|
|
{
|
|
|
|
GstCoreAudio *core_audio;
|
|
|
|
|
|
|
|
core_audio = g_object_new (GST_TYPE_CORE_AUDIO, NULL);
|
|
|
|
core_audio->osxbuf = osxbuf;
|
2015-03-09 21:34:06 +00:00
|
|
|
core_audio->cached_caps = NULL;
|
2013-03-05 20:17:52 +00:00
|
|
|
return core_audio;
|
|
|
|
}
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
gst_core_audio_close (GstCoreAudio * core_audio)
|
|
|
|
{
|
2015-03-09 21:15:56 +00:00
|
|
|
OSStatus status;
|
|
|
|
|
|
|
|
/* Uninitialize the AudioUnit */
|
|
|
|
status = AudioUnitUninitialize (core_audio->audiounit);
|
|
|
|
if (status) {
|
|
|
|
GST_ERROR_OBJECT (core_audio, "Failed to uninitialize AudioUnit: %d",
|
|
|
|
(int) status);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2015-03-12 10:15:12 +00:00
|
|
|
AudioUnitRemovePropertyListenerWithUserData (core_audio->audiounit,
|
|
|
|
kAudioUnitProperty_AudioChannelLayout, _audio_unit_property_listener,
|
|
|
|
core_audio);
|
|
|
|
AudioUnitRemovePropertyListenerWithUserData (core_audio->audiounit,
|
|
|
|
kAudioUnitProperty_StreamFormat, _audio_unit_property_listener,
|
|
|
|
core_audio);
|
|
|
|
|
2015-03-09 21:34:06 +00:00
|
|
|
/* core_audio->osxbuf is already locked at this point */
|
2015-03-21 18:34:25 +00:00
|
|
|
core_audio->cached_caps_valid = FALSE;
|
2015-03-09 21:34:06 +00:00
|
|
|
gst_caps_replace (&core_audio->cached_caps, NULL);
|
|
|
|
|
2013-03-05 20:17:52 +00:00
|
|
|
AudioComponentInstanceDispose (core_audio->audiounit);
|
|
|
|
core_audio->audiounit = NULL;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
gst_core_audio_open (GstCoreAudio * core_audio)
|
|
|
|
{
|
2015-03-09 21:15:56 +00:00
|
|
|
OSStatus status;
|
|
|
|
|
2015-03-21 18:34:25 +00:00
|
|
|
/* core_audio->osxbuf is already locked at this point */
|
|
|
|
core_audio->cached_caps_valid = FALSE;
|
|
|
|
gst_caps_replace (&core_audio->cached_caps, NULL);
|
|
|
|
|
2015-03-09 21:15:56 +00:00
|
|
|
if (!gst_core_audio_open_impl (core_audio))
|
|
|
|
return FALSE;
|
|
|
|
|
2015-03-12 10:15:12 +00:00
|
|
|
/* Add property listener */
|
|
|
|
status = AudioUnitAddPropertyListener (core_audio->audiounit,
|
|
|
|
kAudioUnitProperty_AudioChannelLayout, _audio_unit_property_listener,
|
|
|
|
core_audio);
|
|
|
|
if (status != noErr) {
|
|
|
|
GST_ERROR_OBJECT (core_audio, "Failed to add audio channel layout property "
|
|
|
|
"listener for AudioUnit: %d", (int) status);
|
|
|
|
}
|
|
|
|
status = AudioUnitAddPropertyListener (core_audio->audiounit,
|
|
|
|
kAudioUnitProperty_StreamFormat, _audio_unit_property_listener,
|
|
|
|
core_audio);
|
|
|
|
if (status != noErr) {
|
|
|
|
GST_ERROR_OBJECT (core_audio, "Failed to add stream format property "
|
|
|
|
"listener for AudioUnit: %d", (int) status);
|
|
|
|
}
|
|
|
|
|
2015-04-06 10:52:34 +00:00
|
|
|
/* Initialize the AudioUnit. We keep the audio unit initialized early so that
|
|
|
|
* we can probe the underlying device. */
|
2015-03-09 21:15:56 +00:00
|
|
|
status = AudioUnitInitialize (core_audio->audiounit);
|
|
|
|
if (status) {
|
|
|
|
GST_ERROR_OBJECT (core_audio, "Failed to initialize AudioUnit: %d",
|
|
|
|
(int) status);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
2013-03-05 20:17:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
gst_core_audio_start_processing (GstCoreAudio * core_audio)
|
|
|
|
{
|
|
|
|
return gst_core_audio_start_processing_impl (core_audio);
|
|
|
|
}
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
gst_core_audio_pause_processing (GstCoreAudio * core_audio)
|
|
|
|
{
|
|
|
|
return gst_core_audio_pause_processing_impl (core_audio);
|
|
|
|
}
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
gst_core_audio_stop_processing (GstCoreAudio * core_audio)
|
|
|
|
{
|
|
|
|
return gst_core_audio_stop_processing_impl (core_audio);
|
|
|
|
}
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
gst_core_audio_get_samples_and_latency (GstCoreAudio * core_audio,
|
|
|
|
gdouble rate, guint * samples, gdouble * latency)
|
|
|
|
{
|
|
|
|
return gst_core_audio_get_samples_and_latency_impl (core_audio, rate,
|
|
|
|
samples, latency);
|
|
|
|
}
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
gst_core_audio_initialize (GstCoreAudio * core_audio,
|
|
|
|
AudioStreamBasicDescription format, GstCaps * caps, gboolean is_passthrough)
|
|
|
|
{
|
|
|
|
guint32 frame_size;
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (core_audio,
|
|
|
|
"Initializing: passthrough:%d caps:%" GST_PTR_FORMAT, is_passthrough,
|
|
|
|
caps);
|
|
|
|
|
|
|
|
if (!gst_core_audio_initialize_impl (core_audio, format, caps,
|
|
|
|
is_passthrough, &frame_size)) {
|
2015-03-09 21:15:56 +00:00
|
|
|
return FALSE;
|
2013-03-05 20:17:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (core_audio->is_src) {
|
|
|
|
/* create AudioBufferList needed for recording */
|
2015-02-24 03:57:24 +00:00
|
|
|
core_audio->recBufferSize = frame_size * format.mBytesPerFrame;
|
2013-03-05 20:17:52 +00:00
|
|
|
core_audio->recBufferList =
|
2015-02-24 03:57:24 +00:00
|
|
|
buffer_list_alloc (format.mChannelsPerFrame, core_audio->recBufferSize,
|
|
|
|
/* Currently always TRUE (i.e. interleaved) */
|
|
|
|
!(format.mFormatFlags & kAudioFormatFlagIsNonInterleaved));
|
2013-03-05 20:17:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-04-06 10:25:59 +00:00
|
|
|
gst_core_audio_uninitialize (GstCoreAudio * core_audio)
|
2013-03-05 20:17:52 +00:00
|
|
|
{
|
2015-02-24 03:57:24 +00:00
|
|
|
buffer_list_free (core_audio->recBufferList);
|
|
|
|
core_audio->recBufferList = NULL;
|
2013-03-05 20:17:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gst_core_audio_set_volume (GstCoreAudio * core_audio, gfloat volume)
|
|
|
|
{
|
|
|
|
AudioUnitSetParameter (core_audio->audiounit, kHALOutputParam_Volume,
|
|
|
|
kAudioUnitScope_Global, 0, (float) volume, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
gboolean
|
2014-11-28 16:53:17 +00:00
|
|
|
gst_core_audio_select_device (GstCoreAudio * core_audio)
|
2013-03-05 20:17:52 +00:00
|
|
|
{
|
2014-11-28 16:53:17 +00:00
|
|
|
return gst_core_audio_select_device_impl (core_audio);
|
2013-03-05 20:17:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gst_core_audio_init_debug (void)
|
|
|
|
{
|
|
|
|
GST_DEBUG_CATEGORY_INIT (osx_audio_debug, "osxaudio", 0,
|
|
|
|
"OSX Audio Elements");
|
|
|
|
}
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
gst_core_audio_audio_device_is_spdif_avail (AudioDeviceID device_id)
|
|
|
|
{
|
|
|
|
return gst_core_audio_audio_device_is_spdif_avail_impl (device_id);
|
|
|
|
}
|
2014-12-01 14:11:35 +00:00
|
|
|
|
2015-03-09 21:34:06 +00:00
|
|
|
/* Does the channel have at least one positioned channel?
|
|
|
|
* (GStreamer is more strict than Core Audio, in that it requires either
|
|
|
|
* all channels to be positioned, or all unpositioned.) */
|
|
|
|
static gboolean
|
|
|
|
_is_core_audio_layout_positioned (AudioChannelLayout * layout)
|
|
|
|
{
|
|
|
|
guint i;
|
|
|
|
|
|
|
|
g_assert (layout->mChannelLayoutTag ==
|
|
|
|
kAudioChannelLayoutTag_UseChannelDescriptions);
|
|
|
|
|
|
|
|
for (i = 0; i < layout->mNumberChannelDescriptions; ++i) {
|
|
|
|
GstAudioChannelPosition p =
|
|
|
|
gst_core_audio_channel_label_to_gst
|
|
|
|
(layout->mChannelDescriptions[i].mChannelLabel, i, FALSE);
|
|
|
|
|
|
|
|
if (p >= 0) /* not special positition */
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
_core_audio_parse_channel_descriptions (AudioChannelLayout * layout,
|
|
|
|
guint * channels, guint64 * channel_mask, GstAudioChannelPosition * pos)
|
|
|
|
{
|
|
|
|
gboolean positioned;
|
|
|
|
guint i;
|
|
|
|
|
|
|
|
g_assert (layout->mChannelLayoutTag ==
|
|
|
|
kAudioChannelLayoutTag_UseChannelDescriptions);
|
|
|
|
|
|
|
|
positioned = _is_core_audio_layout_positioned (layout);
|
|
|
|
*channel_mask = 0;
|
|
|
|
|
|
|
|
/* Go over all labels, either taking only positioned or only
|
|
|
|
* unpositioned channels, up to GST_OSX_AUDIO_MAX_CHANNEL channels.
|
|
|
|
*
|
|
|
|
* The resulting 'pos' array will contain either:
|
|
|
|
* - only regular (>= 0) positions
|
|
|
|
* - only GST_AUDIO_CHANNEL_POSITION_NONE positions
|
|
|
|
* in a compact form, skipping over all unsupported positions.
|
|
|
|
*/
|
|
|
|
*channels = 0;
|
|
|
|
for (i = 0; i < layout->mNumberChannelDescriptions; ++i) {
|
|
|
|
GstAudioChannelPosition p =
|
|
|
|
gst_core_audio_channel_label_to_gst
|
|
|
|
(layout->mChannelDescriptions[i].mChannelLabel, i, TRUE);
|
|
|
|
|
|
|
|
/* In positioned layouts, skip all unpositioned channels.
|
|
|
|
* In unpositioned layouts, skip all invalid channels. */
|
|
|
|
if ((positioned && p >= 0) ||
|
|
|
|
(!positioned && p == GST_AUDIO_CHANNEL_POSITION_NONE)) {
|
|
|
|
|
|
|
|
if (pos)
|
|
|
|
pos[*channels] = p;
|
|
|
|
*channel_mask |= G_GUINT64_CONSTANT (1) << p;
|
|
|
|
++(*channels);
|
|
|
|
|
|
|
|
if (*channels == GST_OSX_AUDIO_MAX_CHANNEL)
|
|
|
|
break; /* not to overflow */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-01 15:36:27 +00:00
|
|
|
gboolean
|
|
|
|
gst_core_audio_parse_channel_layout (AudioChannelLayout * layout,
|
2015-03-09 21:34:06 +00:00
|
|
|
guint * channels, guint64 * channel_mask, GstAudioChannelPosition * pos)
|
2014-12-01 15:36:27 +00:00
|
|
|
{
|
2015-03-09 21:34:06 +00:00
|
|
|
g_assert (channels != NULL);
|
|
|
|
g_assert (channel_mask != NULL);
|
|
|
|
g_assert (layout != NULL);
|
|
|
|
|
|
|
|
if (layout->mChannelLayoutTag !=
|
|
|
|
kAudioChannelLayoutTag_UseChannelDescriptions) {
|
|
|
|
GST_ERROR
|
|
|
|
("Only kAudioChannelLayoutTag_UseChannelDescriptions is supported.");
|
|
|
|
*channels = 0;
|
|
|
|
*channel_mask = 0;
|
|
|
|
return FALSE;
|
|
|
|
}
|
2014-12-01 15:36:27 +00:00
|
|
|
|
2015-03-09 21:34:06 +00:00
|
|
|
switch (layout->mNumberChannelDescriptions) {
|
2014-12-01 15:36:27 +00:00
|
|
|
case 0:
|
2015-03-09 21:34:06 +00:00
|
|
|
if (pos)
|
|
|
|
pos[0] = GST_AUDIO_CHANNEL_POSITION_NONE;
|
|
|
|
*channels = 0;
|
|
|
|
*channel_mask = 0;
|
|
|
|
return TRUE;
|
2014-12-01 15:36:27 +00:00
|
|
|
case 1:
|
2015-03-09 21:34:06 +00:00
|
|
|
if (pos)
|
|
|
|
pos[0] = GST_AUDIO_CHANNEL_POSITION_MONO;
|
|
|
|
*channels = 1;
|
|
|
|
*channel_mask = 0;
|
|
|
|
return TRUE;
|
2014-12-01 15:36:27 +00:00
|
|
|
case 2:
|
2015-03-09 21:34:06 +00:00
|
|
|
if (pos) {
|
|
|
|
pos[0] = GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT;
|
|
|
|
pos[1] = GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT;
|
2014-12-01 15:36:27 +00:00
|
|
|
}
|
2015-03-09 21:34:06 +00:00
|
|
|
*channels = 2;
|
|
|
|
*channel_mask =
|
|
|
|
GST_AUDIO_CHANNEL_POSITION_MASK (FRONT_LEFT) |
|
|
|
|
GST_AUDIO_CHANNEL_POSITION_MASK (FRONT_RIGHT);
|
|
|
|
return TRUE;
|
|
|
|
default:
|
|
|
|
_core_audio_parse_channel_descriptions (layout, channels, channel_mask,
|
|
|
|
pos);
|
|
|
|
return TRUE;
|
2014-12-01 15:36:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-09 21:34:06 +00:00
|
|
|
/* Converts an AudioStreamBasicDescription to preferred caps.
|
|
|
|
*
|
|
|
|
* These caps will indicate the AU element's canonical format, which won't
|
|
|
|
* make Core Audio resample nor convert.
|
|
|
|
*
|
|
|
|
* NOTE ON MULTI-CHANNEL AUDIO:
|
|
|
|
*
|
|
|
|
* If layout is not NULL, resulting caps will only include the subset
|
|
|
|
* of channels supported by GStreamer. If the Core Audio layout contained
|
|
|
|
* ANY positioned channels, then ONLY positioned channels will be included
|
|
|
|
* in the resulting caps. Otherwise, resulting caps will be unpositioned,
|
|
|
|
* and include only unpositioned channels.
|
|
|
|
* (Channels with unsupported AudioChannelLabel will be skipped either way.)
|
|
|
|
*
|
|
|
|
* Naturally, the number of channels indicated by 'channels' can be lower
|
|
|
|
* than the AU element's total number of channels.
|
|
|
|
*/
|
2014-12-01 14:11:35 +00:00
|
|
|
GstCaps *
|
2014-12-01 15:36:27 +00:00
|
|
|
gst_core_audio_asbd_to_caps (AudioStreamBasicDescription * asbd,
|
|
|
|
AudioChannelLayout * layout)
|
2014-12-01 14:11:35 +00:00
|
|
|
{
|
|
|
|
GstAudioInfo info;
|
|
|
|
GstAudioFormat format = GST_AUDIO_FORMAT_UNKNOWN;
|
2015-03-09 21:34:06 +00:00
|
|
|
guint rate, channels, bps, endianness;
|
2014-12-01 15:36:27 +00:00
|
|
|
guint64 channel_mask;
|
2014-12-01 14:11:35 +00:00
|
|
|
gboolean sign, interleaved;
|
|
|
|
|
|
|
|
if (asbd->mFormatID != kAudioFormatLinearPCM) {
|
|
|
|
GST_WARNING ("Only linear PCM is supported");
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(asbd->mFormatFlags & kAudioFormatFlagIsPacked)) {
|
|
|
|
GST_WARNING ("Only packed formats supported");
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (asbd->mFormatFlags & kLinearPCMFormatFlagsSampleFractionMask) {
|
|
|
|
GST_WARNING ("Fixed point audio is unsupported");
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
rate = asbd->mSampleRate;
|
2015-03-09 21:34:06 +00:00
|
|
|
if (rate == kAudioStreamAnyRate) {
|
|
|
|
GST_WARNING ("No sample rate");
|
|
|
|
goto error;
|
2014-12-01 14:11:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bps = asbd->mBitsPerChannel;
|
|
|
|
endianness = asbd->mFormatFlags & kAudioFormatFlagIsBigEndian ?
|
|
|
|
G_BIG_ENDIAN : G_LITTLE_ENDIAN;
|
|
|
|
sign = asbd->mFormatID & kAudioFormatFlagIsSignedInteger ? TRUE : FALSE;
|
|
|
|
interleaved = asbd->mFormatFlags & kAudioFormatFlagIsNonInterleaved ?
|
|
|
|
TRUE : FALSE;
|
|
|
|
|
|
|
|
if (asbd->mFormatFlags & kAudioFormatFlagIsFloat) {
|
|
|
|
if (bps == 32) {
|
|
|
|
if (endianness == G_LITTLE_ENDIAN)
|
|
|
|
format = GST_AUDIO_FORMAT_F32LE;
|
|
|
|
else
|
|
|
|
format = GST_AUDIO_FORMAT_F32BE;
|
|
|
|
|
|
|
|
} else if (bps == 64) {
|
|
|
|
if (endianness == G_LITTLE_ENDIAN)
|
|
|
|
format = GST_AUDIO_FORMAT_F64LE;
|
|
|
|
else
|
|
|
|
format = GST_AUDIO_FORMAT_F64BE;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
format = gst_audio_format_build_integer (sign, endianness, bps, bps);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (format == GST_AUDIO_FORMAT_UNKNOWN) {
|
|
|
|
GST_WARNING ("Unsupported sample format");
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2015-03-09 21:34:06 +00:00
|
|
|
if (layout) {
|
|
|
|
GstAudioChannelPosition pos[GST_OSX_AUDIO_MAX_CHANNEL];
|
|
|
|
|
|
|
|
if (!gst_core_audio_parse_channel_layout (layout, &channels, &channel_mask,
|
|
|
|
pos)) {
|
|
|
|
GST_WARNING ("Failed to parse channel layout");
|
|
|
|
goto error;
|
|
|
|
}
|
2014-12-01 15:36:27 +00:00
|
|
|
|
2015-03-09 21:34:06 +00:00
|
|
|
/* The AU can have arbitrary channel order, but we're using GstAudioInfo
|
|
|
|
* which supports only the GStreamer channel order.
|
|
|
|
* Also, we're eventually producing caps, which only have channel-mask
|
|
|
|
* (whose implied order is the GStreamer channel order). */
|
|
|
|
gst_audio_channel_positions_to_valid_order (pos, channels);
|
|
|
|
|
|
|
|
gst_audio_info_set_format (&info, format, rate, channels, pos);
|
|
|
|
} else {
|
|
|
|
channels = MIN (asbd->mChannelsPerFrame, GST_OSX_AUDIO_MAX_CHANNEL);
|
|
|
|
gst_audio_info_set_format (&info, format, rate, channels, NULL);
|
|
|
|
}
|
2014-12-01 14:11:35 +00:00
|
|
|
|
|
|
|
return gst_audio_info_to_caps (&info);
|
|
|
|
|
|
|
|
error:
|
|
|
|
return NULL;
|
|
|
|
}
|
2015-03-09 21:34:06 +00:00
|
|
|
|
|
|
|
static gboolean
|
|
|
|
_core_audio_get_property (GstCoreAudio * core_audio, gboolean outer,
|
|
|
|
AudioUnitPropertyID inID, void *inData, UInt32 * inDataSize)
|
|
|
|
{
|
|
|
|
OSStatus status;
|
|
|
|
AudioUnitScope scope;
|
|
|
|
AudioUnitElement element;
|
|
|
|
|
|
|
|
scope = outer ?
|
|
|
|
CORE_AUDIO_OUTER_SCOPE (core_audio) : CORE_AUDIO_INNER_SCOPE (core_audio);
|
|
|
|
element = CORE_AUDIO_ELEMENT (core_audio);
|
|
|
|
|
|
|
|
status =
|
|
|
|
AudioUnitGetProperty (core_audio->audiounit, inID, scope, element, inData,
|
|
|
|
inDataSize);
|
|
|
|
|
|
|
|
return status == noErr;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
_core_audio_get_stream_format (GstCoreAudio * core_audio,
|
|
|
|
AudioStreamBasicDescription * asbd, gboolean outer)
|
|
|
|
{
|
|
|
|
UInt32 size;
|
|
|
|
|
|
|
|
size = sizeof (AudioStreamBasicDescription);
|
|
|
|
return _core_audio_get_property (core_audio, outer,
|
|
|
|
kAudioUnitProperty_StreamFormat, asbd, &size);
|
|
|
|
}
|
|
|
|
|
|
|
|
AudioChannelLayout *
|
|
|
|
gst_core_audio_get_channel_layout (GstCoreAudio * core_audio, gboolean outer)
|
|
|
|
{
|
|
|
|
UInt32 size;
|
|
|
|
AudioChannelLayout *layout;
|
|
|
|
|
|
|
|
if (core_audio->is_src) {
|
|
|
|
GST_WARNING_OBJECT (core_audio,
|
|
|
|
"gst_core_audio_get_channel_layout not supported on source.");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!_core_audio_get_property (core_audio, outer,
|
|
|
|
kAudioUnitProperty_AudioChannelLayout, NULL, &size)) {
|
|
|
|
GST_WARNING_OBJECT (core_audio, "unable to get channel layout");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
layout = g_malloc (size);
|
|
|
|
if (!_core_audio_get_property (core_audio, outer,
|
|
|
|
kAudioUnitProperty_AudioChannelLayout, layout, &size)) {
|
|
|
|
GST_WARNING_OBJECT (core_audio, "unable to get channel layout");
|
|
|
|
g_free (layout);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return layout;
|
|
|
|
}
|
|
|
|
|
2015-04-07 10:10:14 +00:00
|
|
|
#define STEREO_CHANNEL_MASK \
|
|
|
|
(GST_AUDIO_CHANNEL_POSITION_MASK (FRONT_LEFT) | \
|
|
|
|
GST_AUDIO_CHANNEL_POSITION_MASK (FRONT_RIGHT))
|
|
|
|
|
2015-03-09 21:34:06 +00:00
|
|
|
GstCaps *
|
|
|
|
gst_core_audio_probe_caps (GstCoreAudio * core_audio, GstCaps * in_caps)
|
|
|
|
{
|
|
|
|
guint i, channels;
|
|
|
|
gboolean spdif_allowed;
|
|
|
|
AudioChannelLayout *layout;
|
|
|
|
AudioStreamBasicDescription outer_asbd;
|
|
|
|
gboolean got_outer_asbd;
|
|
|
|
GstCaps *caps = NULL;
|
|
|
|
guint64 channel_mask;
|
|
|
|
|
|
|
|
/* Get the ASBD of the outer scope (i.e. input scope of Input,
|
|
|
|
* output scope of Output).
|
|
|
|
* This ASBD indicates the hardware format. */
|
|
|
|
got_outer_asbd =
|
|
|
|
_core_audio_get_stream_format (core_audio, &outer_asbd, TRUE);
|
|
|
|
|
|
|
|
/* Collect info about the HW capabilites and preferences */
|
|
|
|
spdif_allowed =
|
|
|
|
gst_core_audio_audio_device_is_spdif_avail (core_audio->device_id);
|
2015-04-15 15:29:14 +00:00
|
|
|
if (!core_audio->is_src)
|
|
|
|
layout = gst_core_audio_get_channel_layout (core_audio, TRUE);
|
|
|
|
else
|
|
|
|
layout = NULL; /* no supported for sources */
|
2015-03-09 21:34:06 +00:00
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (core_audio, "Selected device ID: %u SPDIF allowed: %d",
|
|
|
|
(unsigned) core_audio->device_id, spdif_allowed);
|
|
|
|
|
|
|
|
if (layout) {
|
|
|
|
if (!gst_core_audio_parse_channel_layout (layout, &channels, &channel_mask,
|
|
|
|
NULL)) {
|
|
|
|
GST_WARNING_OBJECT (core_audio, "Failed to parse channel layout");
|
|
|
|
channel_mask = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If available, start with the preferred caps. */
|
|
|
|
if (got_outer_asbd)
|
|
|
|
caps = gst_core_audio_asbd_to_caps (&outer_asbd, layout);
|
|
|
|
|
|
|
|
g_free (layout);
|
|
|
|
} else if (got_outer_asbd) {
|
|
|
|
channels = outer_asbd.mChannelsPerFrame;
|
|
|
|
channel_mask = 0;
|
|
|
|
/* If available, start with the preferred caps */
|
|
|
|
caps = gst_core_audio_asbd_to_caps (&outer_asbd, NULL);
|
|
|
|
} else {
|
|
|
|
GST_ERROR_OBJECT (core_audio,
|
|
|
|
"Unable to get any information about hardware");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Append the allowed subset based on the template caps */
|
|
|
|
if (!caps)
|
|
|
|
caps = gst_caps_new_empty ();
|
|
|
|
for (i = 0; i < gst_caps_get_size (in_caps); i++) {
|
|
|
|
GstStructure *in_s;
|
|
|
|
|
|
|
|
in_s = gst_caps_get_structure (in_caps, i);
|
|
|
|
|
|
|
|
if (gst_structure_has_name (in_s, "audio/x-ac3") ||
|
|
|
|
gst_structure_has_name (in_s, "audio/x-dts")) {
|
|
|
|
if (spdif_allowed) {
|
|
|
|
gst_caps_append_structure (caps, gst_structure_copy (in_s));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
GstStructure *out_s;
|
|
|
|
|
|
|
|
out_s = gst_structure_copy (in_s);
|
|
|
|
gst_structure_set (out_s, "channels", G_TYPE_INT, channels, NULL);
|
|
|
|
if (channel_mask != 0) {
|
|
|
|
/* positioned layout */
|
|
|
|
gst_structure_set (out_s,
|
|
|
|
"channel-mask", GST_TYPE_BITMASK, channel_mask, NULL);
|
|
|
|
} else {
|
|
|
|
/* unpositioned layout */
|
|
|
|
gst_structure_remove_field (out_s, "channel-mask");
|
|
|
|
}
|
|
|
|
|
2015-04-15 17:13:27 +00:00
|
|
|
#ifndef HAVE_IOS
|
|
|
|
if (core_audio->is_src && got_outer_asbd
|
|
|
|
&& outer_asbd.mSampleRate != kAudioStreamAnyRate) {
|
|
|
|
/* According to Core Audio engineer, AUHAL does not support sample rate conversion.
|
|
|
|
* on sources. Therefore, we fixate the sample rate.
|
|
|
|
*
|
|
|
|
* "You definitely cannot do rate conversion as part of getting input from AUHAL.
|
|
|
|
* That's the most common cause of those "cannot do in current context" errors."
|
|
|
|
* http://lists.apple.com/archives/coreaudio-api/2006/Sep/msg00088.html
|
|
|
|
*/
|
|
|
|
gst_structure_set (out_s, "rate", G_TYPE_INT,
|
|
|
|
(gint) outer_asbd.mSampleRate, NULL);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-03-09 21:34:06 +00:00
|
|
|
/* Special cases for upmixing and downmixing.
|
|
|
|
* Other than that, the AUs don't upmix or downmix multi-channel audio,
|
|
|
|
* e.g. if you push 5.1-surround audio to a stereo configuration,
|
|
|
|
* the left and right channels will be played accordingly,
|
|
|
|
* and the rest will be dropped. */
|
|
|
|
|
2015-04-07 10:10:14 +00:00
|
|
|
if (channels == 1 || (channels == 2 &&
|
|
|
|
(channel_mask == 0 || channel_mask == STEREO_CHANNEL_MASK))) {
|
2015-03-09 21:34:06 +00:00
|
|
|
|
|
|
|
/* If have stereo channels, then also offer mono since CoreAudio
|
2015-04-07 10:10:14 +00:00
|
|
|
* upmixes it. If mono, then also offer stereo since CoreAudio
|
|
|
|
* downmixes to it */
|
2015-03-09 21:34:06 +00:00
|
|
|
|
2015-04-07 10:10:14 +00:00
|
|
|
gst_structure_set (out_s, "channels", GST_TYPE_INT_RANGE, 1, 2, NULL);
|
2015-03-09 21:34:06 +00:00
|
|
|
|
2015-04-07 10:10:14 +00:00
|
|
|
if (channels == 1)
|
|
|
|
gst_structure_set (out_s, "channel-mask", GST_TYPE_BITMASK,
|
|
|
|
STEREO_CHANNEL_MASK, NULL);
|
2015-03-09 21:34:06 +00:00
|
|
|
}
|
2015-04-07 10:10:14 +00:00
|
|
|
|
|
|
|
gst_caps_append_structure (caps, out_s);
|
2015-03-09 21:34:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return caps;
|
|
|
|
}
|