| 1 |
/* |
| 2 |
* GStreamer |
| 3 |
* Copyright (C) 2012-2013 Fluendo S.A. <[email protected]> |
| 4 |
* Authors: Josep Torra Vallès <[email protected]> |
| 5 |
* Andoni Morales Alastruey <[email protected]> |
| 6 |
* |
| 7 |
* This library is free software; you can redistribute it and/or |
| 8 |
* modify it under the terms of the GNU Library General Public |
| 9 |
* License as published by the Free Software Foundation; either |
| 10 |
* version 2 of the License, or (at your option) any later version. |
| 11 |
* |
| 12 |
* This library is distributed in the hope that it will be useful, |
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 |
* Library General Public License for more details. |
| 16 |
* |
| 17 |
* You should have received a copy of the GNU Library General Public |
| 18 |
* License along with this library; if not, write to the |
| 19 |
* Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 20 |
* Boston, MA 02111-1307, USA. |
| 21 |
* |
| 22 |
*/ |
| 23 |
|
| 24 |
#include "gstosxcoreaudio.h" |
| 25 |
#include "gstosxcoreaudiocommon.h" |
| 26 |
|
| 27 |
GST_DEBUG_CATEGORY (osx_coreaudio_debug); |
| 28 |
#define GST_CAT_DEFAULT osx_coreaudio_debug |
| 29 |
|
| 30 |
G_DEFINE_TYPE (GstCoreAudio, gst_core_audio, G_TYPE_OBJECT); |
| 31 |
|
| 32 |
#if TARGET_OS_OSX |
| 33 |
#include "gstosxcoreaudiohal.c" |
| 34 |
#include <CoreAudio/CoreAudio.h> |
| 35 |
#else |
| 36 |
#include "gstosxcoreaudioremoteio.c" |
| 37 |
#endif |
| 38 |
|
| 39 |
enum |
| 40 |
{ |
| 41 |
PROP_0, |
| 42 |
PROP_DEVICE, |
| 43 |
PROP_IS_SRC, |
| 44 |
PROP_CONFIGURE_SESSION, |
| 45 |
PROP_UNIQUE_ID, |
| 46 |
}; |
| 47 |
|
| 48 |
static void gst_core_audio_set_property (GObject * object, guint prop_id, |
| 49 |
const GValue * value, GParamSpec * pspec); |
| 50 |
static void gst_core_audio_get_property (GObject * object, guint prop_id, |
| 51 |
GValue * value, GParamSpec * pspec); |
| 52 |
|
| 53 |
static void |
| 54 |
gst_core_audio_finalize (GObject * object) |
| 55 |
{ |
| 56 |
GstCoreAudio *core_audio = GST_CORE_AUDIO (object); |
| 57 |
g_mutex_clear (&core_audio->timing_lock); |
| 58 |
g_clear_pointer (&core_audio->unique_id, g_free); |
| 59 |
|
| 60 |
G_OBJECT_CLASS (gst_core_audio_parent_class)->finalize (object); |
| 61 |
} |
| 62 |
|
| 63 |
static void |
| 64 |
gst_core_audio_class_init (GstCoreAudioClass * klass) |
| 65 |
{ |
| 66 |
GObjectClass *object_klass = G_OBJECT_CLASS (klass); |
| 67 |
object_klass->finalize = gst_core_audio_finalize; |
| 68 |
|
| 69 |
object_klass->set_property = gst_core_audio_set_property; |
| 70 |
object_klass->get_property = gst_core_audio_get_property; |
| 71 |
|
| 72 |
g_object_class_install_property (object_klass, PROP_UNIQUE_ID, |
| 73 |
g_param_spec_string ("unique-id", "Unique ID", |
| 74 |
"Unique ID of audio device", NULL, |
| 75 |
G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY)); |
| 76 |
|
| 77 |
g_object_class_install_property (object_klass, PROP_DEVICE, |
| 78 |
g_param_spec_int ("device", "Device ID", "Device ID of input device", |
| 79 |
0, G_MAXINT, 0, |
| 80 |
G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY)); |
| 81 |
|
| 82 |
g_object_class_install_property (object_klass, PROP_IS_SRC, |
| 83 |
g_param_spec_boolean ("is-src", "Is source", "Is a source device", |
| 84 |
FALSE, |
| 85 |
G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY)); |
| 86 |
|
| 87 |
#if !TARGET_OS_OSX |
| 88 |
g_object_class_install_property (object_klass, PROP_CONFIGURE_SESSION, |
| 89 |
g_param_spec_boolean ("configure-session", |
| 90 |
"Enable automatic AVAudioSession setup", |
| 91 |
"Auto-configure the AVAudioSession for audio playback/capture", FALSE, |
| 92 |
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY)); |
| 93 |
#endif |
| 94 |
} |
| 95 |
|
| 96 |
static void |
| 97 |
gst_core_audio_init (GstCoreAudio * core_audio) |
| 98 |
{ |
| 99 |
core_audio->is_passthrough = FALSE; |
| 100 |
core_audio->device_id = kAudioDeviceUnknown; |
| 101 |
core_audio->unique_id = NULL; |
| 102 |
core_audio->is_src = FALSE; |
| 103 |
core_audio->audiounit = NULL; |
| 104 |
core_audio->cached_caps = NULL; |
| 105 |
core_audio->cached_caps_valid = FALSE; |
| 106 |
#if TARGET_OS_OSX |
| 107 |
core_audio->hog_pid = -1; |
| 108 |
core_audio->disabled_mixing = FALSE; |
| 109 |
#else |
| 110 |
core_audio->configure_session = FALSE; |
| 111 |
#endif |
| 112 |
|
| 113 |
mach_timebase_info (&core_audio->timebase); |
| 114 |
g_mutex_init (&core_audio->timing_lock); |
| 115 |
} |
| 116 |
|
| 117 |
static void |
| 118 |
gst_core_audio_set_property (GObject * object, guint prop_id, |
| 119 |
const GValue * value, GParamSpec * pspec) |
| 120 |
{ |
| 121 |
GstCoreAudio *self = GST_CORE_AUDIO (object); |
| 122 |
|
| 123 |
switch (prop_id) { |
| 124 |
case PROP_IS_SRC: |
| 125 |
self->is_src = g_value_get_boolean (value); |
| 126 |
break; |
| 127 |
case PROP_UNIQUE_ID: |
| 128 |
self->unique_id = g_value_dup_string (value); |
| 129 |
break; |
| 130 |
case PROP_DEVICE: |
| 131 |
self->device_id = g_value_get_int (value); |
| 132 |
break; |
| 133 |
#if !TARGET_OS_OSX |
| 134 |
case PROP_CONFIGURE_SESSION: |
| 135 |
self->configure_session = g_value_get_boolean (value); |
| 136 |
break; |
| 137 |
#endif |
| 138 |
default: |
| 139 |
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); |
| 140 |
break; |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
static void |
| 145 |
gst_core_audio_get_property (GObject * object, guint prop_id, |
| 146 |
GValue * value, GParamSpec * pspec) |
| 147 |
{ |
| 148 |
GstCoreAudio *self = GST_CORE_AUDIO (object); |
| 149 |
|
| 150 |
switch (prop_id) { |
| 151 |
case PROP_IS_SRC: |
| 152 |
g_value_set_boolean (value, self->is_src); |
| 153 |
break; |
| 154 |
case PROP_UNIQUE_ID: |
| 155 |
g_value_set_string (value, self->unique_id); |
| 156 |
break; |
| 157 |
case PROP_DEVICE: |
| 158 |
g_value_set_int (value, self->device_id); |
| 159 |
break; |
| 160 |
#if !TARGET_OS_OSX |
| 161 |
case PROP_CONFIGURE_SESSION: |
| 162 |
g_value_set_boolean (value, self->configure_session); |
| 163 |
break; |
| 164 |
#endif |
| 165 |
default: |
| 166 |
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); |
| 167 |
break; |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
static gboolean |
| 172 |
_is_outer_scope (AudioUnitScope scope, AudioUnitElement element) |
| 173 |
{ |
| 174 |
return |
| 175 |
(scope == kAudioUnitScope_Input && element == 1) || |
| 176 |
(scope == kAudioUnitScope_Output && element == 0); |
| 177 |
} |
| 178 |
|
| 179 |
static void |
| 180 |
_audio_unit_property_listener (void *inRefCon, AudioUnit inUnit, |
| 181 |
AudioUnitPropertyID inID, AudioUnitScope inScope, |
| 182 |
AudioUnitElement inElement) |
| 183 |
{ |
| 184 |
GstCoreAudio *core_audio; |
| 185 |
|
| 186 |
core_audio = GST_CORE_AUDIO (inRefCon); |
| 187 |
g_assert (inUnit == core_audio->audiounit); |
| 188 |
|
| 189 |
switch (inID) { |
| 190 |
case kAudioUnitProperty_AudioChannelLayout: |
| 191 |
case kAudioUnitProperty_StreamFormat: |
| 192 |
if (_is_outer_scope (inScope, inElement)) { |
| 193 |
/* We don't push gst_event_new_caps here (for src), |
| 194 |
* nor gst_event_new_reconfigure (for sink), since Core Audio continues |
| 195 |
* to happily function with the old format, doing conversion/resampling |
| 196 |
* as needed. |
| 197 |
* This merely "refreshes" our PREFERRED caps. */ |
| 198 |
|
| 199 |
/* This function is called either from a Core Audio thread |
| 200 |
* or as a result of a Core Audio API (e.g. AudioUnitInitialize) |
| 201 |
* from our own thread. In the latter case, osxbuf can be |
| 202 |
* already locked (GStreamer's mutex is not recursive). |
| 203 |
* For this reason we use a boolean flag instead of nullifying |
| 204 |
* cached_caps. */ |
| 205 |
core_audio->cached_caps_valid = FALSE; |
| 206 |
} |
| 207 |
break; |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
static GstClockTime |
| 212 |
_current_time_ns (GstCoreAudio * core_audio) |
| 213 |
{ |
| 214 |
guint64 mach_t = mach_absolute_time (); |
| 215 |
return gst_util_uint64_scale (mach_t, core_audio->timebase.numer, |
| 216 |
core_audio->timebase.denom); |
| 217 |
} |
| 218 |
|
| 219 |
static GstClockTime |
| 220 |
_host_time_to_ns (GstCoreAudio * core_audio, uint64_t host_time) |
| 221 |
{ |
| 222 |
return gst_util_uint64_scale (host_time, core_audio->timebase.numer, |
| 223 |
core_audio->timebase.denom); |
| 224 |
} |
| 225 |
|
| 226 |
/************************** |
| 227 |
* Public API * |
| 228 |
*************************/ |
| 229 |
|
| 230 |
gboolean |
| 231 |
gst_core_audio_close (GstCoreAudio * core_audio) |
| 232 |
{ |
| 233 |
OSStatus status; |
| 234 |
|
| 235 |
/* Uninitialize the AudioUnit */ |
| 236 |
status = AudioUnitUninitialize (core_audio->audiounit); |
| 237 |
if (status) { |
| 238 |
GST_ERROR_OBJECT (core_audio, "Failed to uninitialize AudioUnit: %d", |
| 239 |
(int) status); |
| 240 |
return FALSE; |
| 241 |
} |
| 242 |
|
| 243 |
AudioUnitRemovePropertyListenerWithUserData (core_audio->audiounit, |
| 244 |
kAudioUnitProperty_AudioChannelLayout, _audio_unit_property_listener, |
| 245 |
core_audio); |
| 246 |
AudioUnitRemovePropertyListenerWithUserData (core_audio->audiounit, |
| 247 |
kAudioUnitProperty_StreamFormat, _audio_unit_property_listener, |
| 248 |
core_audio); |
| 249 |
|
| 250 |
/* core_audio->osxbuf is already locked at this point */ |
| 251 |
core_audio->cached_caps_valid = FALSE; |
| 252 |
gst_caps_replace (&core_audio->cached_caps, NULL); |
| 253 |
|
| 254 |
AudioComponentInstanceDispose (core_audio->audiounit); |
| 255 |
core_audio->audiounit = NULL; |
| 256 |
return TRUE; |
| 257 |
} |
| 258 |
|
| 259 |
gboolean |
| 260 |
gst_core_audio_open (GstCoreAudio * core_audio) |
| 261 |
{ |
| 262 |
OSStatus status; |
| 263 |
|
| 264 |
/* core_audio->osxbuf is already locked at this point */ |
| 265 |
core_audio->cached_caps_valid = FALSE; |
| 266 |
gst_caps_replace (&core_audio->cached_caps, NULL); |
| 267 |
|
| 268 |
if (!gst_core_audio_open_impl (core_audio)) |
| 269 |
return FALSE; |
| 270 |
|
| 271 |
/* Add property listener */ |
| 272 |
status = AudioUnitAddPropertyListener (core_audio->audiounit, |
| 273 |
kAudioUnitProperty_AudioChannelLayout, _audio_unit_property_listener, |
| 274 |
core_audio); |
| 275 |
if (status != noErr) { |
| 276 |
GST_ERROR_OBJECT (core_audio, "Failed to add audio channel layout property " |
| 277 |
"listener for AudioUnit: %d", (int) status); |
| 278 |
} |
| 279 |
status = AudioUnitAddPropertyListener (core_audio->audiounit, |
| 280 |
kAudioUnitProperty_StreamFormat, _audio_unit_property_listener, |
| 281 |
core_audio); |
| 282 |
if (status != noErr) { |
| 283 |
GST_ERROR_OBJECT (core_audio, "Failed to add stream format property " |
| 284 |
"listener for AudioUnit: %d", (int) status); |
| 285 |
} |
| 286 |
|
| 287 |
/* Initialize the AudioUnit. We keep the audio unit initialized early so that |
| 288 |
* we can probe the underlying device. */ |
| 289 |
status = AudioUnitInitialize (core_audio->audiounit); |
| 290 |
if (status) { |
| 291 |
GST_ERROR_OBJECT (core_audio, "Failed to initialize AudioUnit: %d", |
| 292 |
(int) status); |
| 293 |
return FALSE; |
| 294 |
} |
| 295 |
|
| 296 |
return TRUE; |
| 297 |
} |
| 298 |
|
| 299 |
gboolean |
| 300 |
gst_core_audio_start_processing (GstCoreAudio * core_audio) |
| 301 |
{ |
| 302 |
return gst_core_audio_start_processing_impl (core_audio); |
| 303 |
} |
| 304 |
|
| 305 |
gboolean |
| 306 |
gst_core_audio_pause_processing (GstCoreAudio * core_audio) |
| 307 |
{ |
| 308 |
return gst_core_audio_pause_processing_impl (core_audio); |
| 309 |
} |
| 310 |
|
| 311 |
gboolean |
| 312 |
gst_core_audio_stop_processing (GstCoreAudio * core_audio) |
| 313 |
{ |
| 314 |
return gst_core_audio_stop_processing_impl (core_audio); |
| 315 |
} |
| 316 |
|
| 317 |
gboolean |
| 318 |
gst_core_audio_get_samples_and_latency (GstCoreAudio * core_audio, |
| 319 |
gdouble rate, guint * samples, gdouble * latency) |
| 320 |
{ |
| 321 |
uint64_t now_ns = _current_time_ns (core_audio); |
| 322 |
gboolean ret = gst_core_audio_get_samples_and_latency_impl (core_audio, rate, |
| 323 |
samples, latency); |
| 324 |
|
| 325 |
if (!ret) |
| 326 |
return FALSE; |
| 327 |
|
| 328 |
CORE_AUDIO_TIMING_LOCK (core_audio); |
| 329 |
|
| 330 |
uint32_t samples_remain = 0; |
| 331 |
uint64_t anchor_ns = core_audio->anchor_hosttime_ns; |
| 332 |
|
| 333 |
if (core_audio->is_src) { |
| 334 |
int64_t captured_ns = |
| 335 |
core_audio->rate_scalar * (int64_t) (now_ns - anchor_ns); |
| 336 |
|
| 337 |
/* src, the anchor time is the timestamp of the first sample in the last |
| 338 |
* packet received, and we increment up from there, unless the device gets stopped. */ |
| 339 |
if (captured_ns > 0) { |
| 340 |
if (core_audio->io_proc_active) { |
| 341 |
samples_remain = (uint32_t) (captured_ns * rate / GST_SECOND); |
| 342 |
} else { |
| 343 |
samples_remain = core_audio->anchor_pend_samples; |
| 344 |
} |
| 345 |
} else { |
| 346 |
/* Time went backward. This shouldn't happen for sources, but report something anyway */ |
| 347 |
samples_remain = |
| 348 |
(uint32_t) (-captured_ns * rate / GST_SECOND) + |
| 349 |
core_audio->anchor_pend_samples; |
| 350 |
} |
| 351 |
|
| 352 |
GST_LOG_OBJECT (core_audio, |
| 353 |
"now_ns %" G_GUINT64_FORMAT " anchor %" G_GUINT64_FORMAT " elapsed ns %" |
| 354 |
G_GINT64_FORMAT " rate %f captured_ns %" G_GINT64_FORMAT |
| 355 |
" anchor_pend_samples %u samples_remain %u", now_ns, anchor_ns, |
| 356 |
now_ns - anchor_ns, rate, captured_ns, core_audio->anchor_pend_samples, |
| 357 |
samples_remain); |
| 358 |
} else { |
| 359 |
/* Sink, the anchor time is the time the most recent buffer will commence play out, |
| 360 |
* and we count down to 0 for unplayed samples beyond that */ |
| 361 |
int64_t unplayed_ns = |
| 362 |
core_audio->rate_scalar * (int64_t) (anchor_ns - now_ns); |
| 363 |
if (unplayed_ns > 0) { |
| 364 |
samples_remain = |
| 365 |
(uint32_t) (unplayed_ns * rate / GST_SECOND) + |
| 366 |
core_audio->anchor_pend_samples; |
| 367 |
} else { |
| 368 |
uint32_t samples_played = (uint32_t) (-unplayed_ns * rate / GST_SECOND); |
| 369 |
if (samples_played < core_audio->anchor_pend_samples) { |
| 370 |
samples_remain = core_audio->anchor_pend_samples - samples_played; |
| 371 |
} |
| 372 |
} |
| 373 |
|
| 374 |
GST_LOG_OBJECT (core_audio, |
| 375 |
"now_ns %" G_GUINT64_FORMAT " anchor %" G_GUINT64_FORMAT " elapsed ns %" |
| 376 |
G_GINT64_FORMAT " rate %f unplayed_ns %" G_GINT64_FORMAT |
| 377 |
" anchor_pend_samples %u", now_ns, anchor_ns, now_ns - anchor_ns, rate, |
| 378 |
unplayed_ns, core_audio->anchor_pend_samples); |
| 379 |
} |
| 380 |
|
| 381 |
CORE_AUDIO_TIMING_UNLOCK (core_audio); |
| 382 |
|
| 383 |
GST_DEBUG_OBJECT (core_audio, "samples = %u latency %f", samples_remain, |
| 384 |
*latency); |
| 385 |
|
| 386 |
*samples = samples_remain; |
| 387 |
return TRUE; |
| 388 |
} |
| 389 |
|
| 390 |
void |
| 391 |
gst_core_audio_update_timing (GstCoreAudio * core_audio, |
| 392 |
const AudioTimeStamp * inTimeStamp, unsigned int inNumberFrames) |
| 393 |
{ |
| 394 |
AudioTimeStampFlags target_flags = |
| 395 |
kAudioTimeStampSampleHostTimeValid | kAudioTimeStampRateScalarValid; |
| 396 |
|
| 397 |
if ((inTimeStamp->mFlags & target_flags) == target_flags) { |
| 398 |
core_audio->anchor_hosttime_ns = |
| 399 |
_host_time_to_ns (core_audio, inTimeStamp->mHostTime); |
| 400 |
core_audio->anchor_pend_samples = inNumberFrames; |
| 401 |
core_audio->rate_scalar = inTimeStamp->mRateScalar; |
| 402 |
|
| 403 |
GST_LOG_OBJECT (core_audio, |
| 404 |
"anchor hosttime_ns %" G_GUINT64_FORMAT |
| 405 |
" scalar_rate %f anchor_pend_samples %u", |
| 406 |
core_audio->anchor_hosttime_ns, |
| 407 |
core_audio->rate_scalar, core_audio->anchor_pend_samples); |
| 408 |
} |
| 409 |
} |
| 410 |
|
| 411 |
gboolean |
| 412 |
gst_core_audio_initialize (GstCoreAudio * core_audio, |
| 413 |
AudioStreamBasicDescription format, GstCaps * caps, |
| 414 |
guint32 frames_per_packet, gboolean is_passthrough) |
| 415 |
{ |
| 416 |
GST_DEBUG_OBJECT (core_audio, |
| 417 |
"Initializing: passthrough:%d caps:%" GST_PTR_FORMAT, is_passthrough, |
| 418 |
caps); |
| 419 |
|
| 420 |
if (!gst_core_audio_initialize_impl (core_audio, format, caps, |
| 421 |
is_passthrough, &frames_per_packet)) { |
| 422 |
return FALSE; |
| 423 |
} |
| 424 |
|
| 425 |
if (core_audio->is_src) { |
| 426 |
/* create AudioBufferList needed for recording */ |
| 427 |
core_audio->recBufferSize = frames_per_packet * format.mBytesPerFrame; |
| 428 |
|
| 429 |
GST_DEBUG_OBJECT (core_audio, |
| 430 |
"Allocating record buffers %u bytes %u frames", |
| 431 |
core_audio->recBufferSize, frames_per_packet); |
| 432 |
|
| 433 |
core_audio->recBufferList = |
| 434 |
buffer_list_alloc (format.mChannelsPerFrame, core_audio->recBufferSize, |
| 435 |
/* Currently always TRUE (i.e. interleaved) */ |
| 436 |
!(format.mFormatFlags & kAudioFormatFlagIsNonInterleaved)); |
| 437 |
} |
| 438 |
|
| 439 |
return TRUE; |
| 440 |
} |
| 441 |
|
| 442 |
void |
| 443 |
gst_core_audio_uninitialize (GstCoreAudio * core_audio) |
| 444 |
{ |
| 445 |
buffer_list_free (core_audio->recBufferList); |
| 446 |
core_audio->recBufferList = NULL; |
| 447 |
} |
| 448 |
|
| 449 |
void |
| 450 |
gst_core_audio_set_volume (GstCoreAudio * core_audio, gfloat volume) |
| 451 |
{ |
| 452 |
AudioUnitSetParameter (core_audio->audiounit, kHALOutputParam_Volume, |
| 453 |
kAudioUnitScope_Global, 0, (float) volume, 0); |
| 454 |
} |
| 455 |
|
| 456 |
gboolean |
| 457 |
gst_core_audio_select_device (GstCoreAudio * core_audio) |
| 458 |
{ |
| 459 |
gboolean ret = gst_core_audio_select_device_impl (core_audio); |
| 460 |
|
| 461 |
#if TARGET_OS_OSX |
| 462 |
if (core_audio->device_id != kAudioDeviceUnknown) |
| 463 |
core_audio->unique_id = |
| 464 |
gst_core_audio_device_get_prop_str (core_audio->device_id, |
| 465 |
kAudioDevicePropertyDeviceUID); |
| 466 |
#endif |
| 467 |
|
| 468 |
return ret; |
| 469 |
} |
| 470 |
|
| 471 |
void |
| 472 |
gst_core_audio_init_debug (void) |
| 473 |
{ |
| 474 |
GST_DEBUG_CATEGORY_INIT (osx_coreaudio_debug, "osxaudio", 0, |
| 475 |
"OSX Audio Elements"); |
| 476 |
} |
| 477 |
|
| 478 |
gboolean |
| 479 |
gst_core_audio_audio_device_is_spdif_avail (AudioDeviceID device_id) |
| 480 |
{ |
| 481 |
return gst_core_audio_audio_device_is_spdif_avail_impl (device_id); |
| 482 |
} |
| 483 |
|
| 484 |
/* Does the channel have at least one positioned channel? |
| 485 |
* (GStreamer is more strict than Core Audio, in that it requires either |
| 486 |
* all channels to be positioned, or all unpositioned.) */ |
| 487 |
static gboolean |
| 488 |
_is_core_audio_layout_positioned (AudioChannelLayout * layout) |
| 489 |
{ |
| 490 |
guint i; |
| 491 |
|
| 492 |
g_assert (layout->mChannelLayoutTag == |
| 493 |
kAudioChannelLayoutTag_UseChannelDescriptions); |
| 494 |
|
| 495 |
for (i = 0; i < layout->mNumberChannelDescriptions; ++i) { |
| 496 |
GstAudioChannelPosition p = |
| 497 |
gst_core_audio_channel_label_to_gst |
| 498 |
(layout->mChannelDescriptions[i].mChannelLabel, i, FALSE, 0); |
| 499 |
|
| 500 |
if (p >= 0) /* not special positition */ |
| 501 |
return TRUE; |
| 502 |
} |
| 503 |
|
| 504 |
return FALSE; |
| 505 |
} |
| 506 |
|
| 507 |
static gboolean |
| 508 |
_core_audio_has_invalid_channel_labels (AudioChannelLayout * layout) |
| 509 |
{ |
| 510 |
guint i; |
| 511 |
|
| 512 |
g_assert (layout->mChannelLayoutTag == |
| 513 |
kAudioChannelLayoutTag_UseChannelDescriptions); |
| 514 |
|
| 515 |
for (i = 0; i < layout->mNumberChannelDescriptions; ++i) { |
| 516 |
/* Let's use our mapping to judge whether the value is valid. |
| 517 |
* It doesn't support all of the defined positions, but the missing ones |
| 518 |
* aren't useful to us anyway. */ |
| 519 |
GstAudioChannelPosition p = |
| 520 |
gst_core_audio_channel_label_to_gst |
| 521 |
(layout->mChannelDescriptions[i].mChannelLabel, i, FALSE, 0); |
| 522 |
|
| 523 |
if (p == GST_AUDIO_CHANNEL_POSITION_INVALID) |
| 524 |
return TRUE; |
| 525 |
} |
| 526 |
|
| 527 |
return FALSE; |
| 528 |
} |
| 529 |
|
| 530 |
static void |
| 531 |
_core_audio_parse_channel_descriptions (AudioChannelLayout * layout, |
| 532 |
AudioDeviceID device_id, guint * channels, guint64 * channel_mask, |
| 533 |
GstAudioChannelPosition * pos) |
| 534 |
{ |
| 535 |
gboolean should_warn = TRUE; |
| 536 |
gboolean positioned; |
| 537 |
guint i; |
| 538 |
|
| 539 |
g_assert (layout->mChannelLayoutTag == |
| 540 |
kAudioChannelLayoutTag_UseChannelDescriptions); |
| 541 |
|
| 542 |
/* For >16ch devices, CoreAudio can give out completely incorrect |
| 543 |
* channel positions by default - instead of using kAudioChannelLabel_Discrete_X, |
| 544 |
* it just returns incrementing values starting from 0, some of which are not |
| 545 |
* valid if you check against the CoreAudioBaseTypes.h header. |
| 546 |
* If such case is detected, let's just swap all positions to Discrete, |
| 547 |
* which map to GST_AUDIO_CHANNEL_POSITION_NONE. */ |
| 548 |
if (_core_audio_has_invalid_channel_labels (layout)) { |
| 549 |
GST_DEBUG |
| 550 |
("Invalid channel positions given by CoreAudio, setting all to unpositioned"); |
| 551 |
*channels = |
| 552 |
MIN (layout->mNumberChannelDescriptions, GST_OSX_AUDIO_MAX_CHANNEL); |
| 553 |
*channel_mask = 0; |
| 554 |
if (pos) { |
| 555 |
for (i = 0; i < *channels; ++i) |
| 556 |
pos[i] = GST_AUDIO_CHANNEL_POSITION_NONE; |
| 557 |
} |
| 558 |
return; |
| 559 |
} |
| 560 |
|
| 561 |
positioned = _is_core_audio_layout_positioned (layout); |
| 562 |
*channel_mask = 0; |
| 563 |
|
| 564 |
/* Go over all labels, either taking only positioned or only |
| 565 |
* unpositioned channels, up to GST_OSX_AUDIO_MAX_CHANNEL channels. |
| 566 |
* |
| 567 |
* The resulting 'pos' array will contain either: |
| 568 |
* - only regular (>= 0) positions |
| 569 |
* - only GST_AUDIO_CHANNEL_POSITION_NONE positions |
| 570 |
* in a compact form, skipping over all unsupported positions. |
| 571 |
*/ |
| 572 |
*channels = 0; |
| 573 |
for (i = 0; i < layout->mNumberChannelDescriptions; ++i) { |
| 574 |
AudioChannelLabel label = layout->mChannelDescriptions[i].mChannelLabel; |
| 575 |
GstAudioChannelPosition p = |
| 576 |
gst_core_audio_channel_label_to_gst (label, i, should_warn, device_id); |
| 577 |
|
| 578 |
if (p == GST_AUDIO_CHANNEL_POSITION_NONE && label >> 16 != 0) { |
| 579 |
/* Avoid warning spam for kAudioChannelLabel_Discrete_N channels losing order */ |
| 580 |
should_warn = FALSE; |
| 581 |
} |
| 582 |
|
| 583 |
/* In positioned layouts, skip all unpositioned channels. |
| 584 |
* In unpositioned layouts, skip all invalid channels. */ |
| 585 |
if ((positioned && p >= 0) || |
| 586 |
(!positioned && p == GST_AUDIO_CHANNEL_POSITION_NONE)) { |
| 587 |
|
| 588 |
if (pos) |
| 589 |
pos[*channels] = p; |
| 590 |
*channel_mask |= G_GUINT64_CONSTANT (1) << p; |
| 591 |
++(*channels); |
| 592 |
|
| 593 |
if (*channels == GST_OSX_AUDIO_MAX_CHANNEL) |
| 594 |
break; /* not to overflow */ |
| 595 |
} |
| 596 |
} |
| 597 |
} |
| 598 |
|
| 599 |
gboolean |
| 600 |
gst_core_audio_parse_channel_layout (AudioChannelLayout * layout, |
| 601 |
AudioDeviceID device_id, guint * channels, guint64 * channel_mask, |
| 602 |
GstAudioChannelPosition * pos) |
| 603 |
{ |
| 604 |
g_assert (channels != NULL); |
| 605 |
g_assert (channel_mask != NULL); |
| 606 |
g_assert (layout != NULL); |
| 607 |
|
| 608 |
if (layout->mChannelLayoutTag == |
| 609 |
kAudioChannelLayoutTag_UseChannelDescriptions) { |
| 610 |
|
| 611 |
switch (layout->mNumberChannelDescriptions) { |
| 612 |
case 0: |
| 613 |
if (pos) |
| 614 |
pos[0] = GST_AUDIO_CHANNEL_POSITION_NONE; |
| 615 |
*channels = 0; |
| 616 |
*channel_mask = 0; |
| 617 |
return TRUE; |
| 618 |
case 1: |
| 619 |
if (pos) |
| 620 |
pos[0] = GST_AUDIO_CHANNEL_POSITION_MONO; |
| 621 |
*channels = 1; |
| 622 |
*channel_mask = 0; |
| 623 |
return TRUE; |
| 624 |
case 2: |
| 625 |
if (pos) { |
| 626 |
pos[0] = GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT; |
| 627 |
pos[1] = GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT; |
| 628 |
} |
| 629 |
*channels = 2; |
| 630 |
*channel_mask = |
| 631 |
GST_AUDIO_CHANNEL_POSITION_MASK (FRONT_LEFT) | |
| 632 |
GST_AUDIO_CHANNEL_POSITION_MASK (FRONT_RIGHT); |
| 633 |
return TRUE; |
| 634 |
default: |
| 635 |
_core_audio_parse_channel_descriptions (layout, device_id, channels, |
| 636 |
channel_mask, pos); |
| 637 |
return TRUE; |
| 638 |
} |
| 639 |
} else if (layout->mChannelLayoutTag == kAudioChannelLayoutTag_Mono) { |
| 640 |
if (pos) |
| 641 |
pos[0] = GST_AUDIO_CHANNEL_POSITION_MONO; |
| 642 |
*channels = 1; |
| 643 |
*channel_mask = 0; |
| 644 |
return TRUE; |
| 645 |
} else if (layout->mChannelLayoutTag == kAudioChannelLayoutTag_Stereo || |
| 646 |
layout->mChannelLayoutTag == kAudioChannelLayoutTag_StereoHeadphones || |
| 647 |
layout->mChannelLayoutTag == kAudioChannelLayoutTag_Binaural) { |
| 648 |
if (pos) { |
| 649 |
pos[0] = GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT; |
| 650 |
pos[1] = GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT; |
| 651 |
} |
| 652 |
*channels = 2; |
| 653 |
*channel_mask = |
| 654 |
GST_AUDIO_CHANNEL_POSITION_MASK (FRONT_LEFT) | |
| 655 |
GST_AUDIO_CHANNEL_POSITION_MASK (FRONT_RIGHT); |
| 656 |
return TRUE; |
| 657 |
} else if (layout->mChannelLayoutTag == kAudioChannelLayoutTag_Quadraphonic) { |
| 658 |
if (pos) { |
| 659 |
pos[0] = GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT; |
| 660 |
pos[1] = GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT; |
| 661 |
pos[2] = GST_AUDIO_CHANNEL_POSITION_SURROUND_LEFT; |
| 662 |
pos[3] = GST_AUDIO_CHANNEL_POSITION_SURROUND_RIGHT; |
| 663 |
} |
| 664 |
*channels = 4; |
| 665 |
*channel_mask = |
| 666 |
GST_AUDIO_CHANNEL_POSITION_MASK (FRONT_LEFT) | |
| 667 |
GST_AUDIO_CHANNEL_POSITION_MASK (FRONT_RIGHT) | |
| 668 |
GST_AUDIO_CHANNEL_POSITION_MASK (SURROUND_LEFT) | |
| 669 |
GST_AUDIO_CHANNEL_POSITION_MASK (SURROUND_RIGHT); |
| 670 |
return TRUE; |
| 671 |
} else if (layout->mChannelLayoutTag == kAudioChannelLayoutTag_Pentagonal) { |
| 672 |
if (pos) { |
| 673 |
pos[0] = GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT; |
| 674 |
pos[1] = GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT; |
| 675 |
pos[2] = GST_AUDIO_CHANNEL_POSITION_SURROUND_LEFT; |
| 676 |
pos[3] = GST_AUDIO_CHANNEL_POSITION_SURROUND_RIGHT; |
| 677 |
pos[4] = GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER; |
| 678 |
} |
| 679 |
*channels = 5; |
| 680 |
*channel_mask = |
| 681 |
GST_AUDIO_CHANNEL_POSITION_MASK (FRONT_LEFT) | |
| 682 |
GST_AUDIO_CHANNEL_POSITION_MASK (FRONT_RIGHT) | |
| 683 |
GST_AUDIO_CHANNEL_POSITION_MASK (SURROUND_LEFT) | |
| 684 |
GST_AUDIO_CHANNEL_POSITION_MASK (SURROUND_RIGHT) | |
| 685 |
GST_AUDIO_CHANNEL_POSITION_MASK (FRONT_CENTER); |
| 686 |
return TRUE; |
| 687 |
} else if (layout->mChannelLayoutTag == kAudioChannelLayoutTag_Cube) { |
| 688 |
if (pos) { |
| 689 |
pos[0] = GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT; |
| 690 |
pos[1] = GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT; |
| 691 |
pos[2] = GST_AUDIO_CHANNEL_POSITION_REAR_LEFT; |
| 692 |
pos[3] = GST_AUDIO_CHANNEL_POSITION_REAR_RIGHT; |
| 693 |
pos[4] = GST_AUDIO_CHANNEL_POSITION_TOP_FRONT_LEFT; |
| 694 |
pos[5] = GST_AUDIO_CHANNEL_POSITION_TOP_FRONT_RIGHT; |
| 695 |
pos[6] = GST_AUDIO_CHANNEL_POSITION_TOP_REAR_LEFT; |
| 696 |
pos[7] = GST_AUDIO_CHANNEL_POSITION_TOP_REAR_RIGHT; |
| 697 |
|
| 698 |
} |
| 699 |
*channels = 8; |
| 700 |
*channel_mask = |
| 701 |
GST_AUDIO_CHANNEL_POSITION_MASK (FRONT_LEFT) | |
| 702 |
GST_AUDIO_CHANNEL_POSITION_MASK (FRONT_RIGHT) | |
| 703 |
GST_AUDIO_CHANNEL_POSITION_MASK (REAR_LEFT) | |
| 704 |
GST_AUDIO_CHANNEL_POSITION_MASK (REAR_RIGHT) | |
| 705 |
GST_AUDIO_CHANNEL_POSITION_MASK (TOP_FRONT_LEFT) | |
| 706 |
GST_AUDIO_CHANNEL_POSITION_MASK (TOP_FRONT_RIGHT) | |
| 707 |
GST_AUDIO_CHANNEL_POSITION_MASK (TOP_REAR_LEFT) | |
| 708 |
GST_AUDIO_CHANNEL_POSITION_MASK (TOP_REAR_RIGHT); |
| 709 |
return TRUE; |
| 710 |
} else { |
| 711 |
GST_WARNING |
| 712 |
("AudioChannelLayoutTag: %u not yet supported", |
| 713 |
layout->mChannelLayoutTag); |
| 714 |
*channels = 0; |
| 715 |
*channel_mask = 0; |
| 716 |
return FALSE; |
| 717 |
} |
| 718 |
} |
| 719 |
|
| 720 |
/* Converts an AudioStreamBasicDescription to preferred caps. |
| 721 |
* |
| 722 |
* These caps will indicate the AU element's canonical format, which won't |
| 723 |
* make Core Audio resample nor convert. |
| 724 |
* |
| 725 |
* NOTE ON MULTI-CHANNEL AUDIO: |
| 726 |
* |
| 727 |
* If layout is not NULL, resulting caps will only include the subset |
| 728 |
* of channels supported by GStreamer. If the Core Audio layout contained |
| 729 |
* ANY positioned channels, then ONLY positioned channels will be included |
| 730 |
* in the resulting caps. Otherwise, resulting caps will be unpositioned, |
| 731 |
* and include only unpositioned channels. |
| 732 |
* (Channels with unsupported AudioChannelLabel will be skipped either way.) |
| 733 |
* |
| 734 |
* Naturally, the number of channels indicated by 'channels' can be lower |
| 735 |
* than the AU element's total number of channels. |
| 736 |
*/ |
| 737 |
GstCaps * |
| 738 |
gst_core_audio_asbd_to_caps (AudioStreamBasicDescription * asbd, |
| 739 |
AudioDeviceID device_id, AudioChannelLayout * layout) |
| 740 |
{ |
| 741 |
GstAudioInfo info; |
| 742 |
GstAudioFormat format = GST_AUDIO_FORMAT_UNKNOWN; |
| 743 |
guint rate, channels, bps, endianness; |
| 744 |
guint64 channel_mask; |
| 745 |
gboolean sign; |
| 746 |
GstAudioChannelPosition pos[GST_OSX_AUDIO_MAX_CHANNEL]; |
| 747 |
|
| 748 |
if (asbd->mFormatID != kAudioFormatLinearPCM) { |
| 749 |
GST_WARNING ("Only linear PCM is supported"); |
| 750 |
goto error; |
| 751 |
} |
| 752 |
|
| 753 |
if (!(asbd->mFormatFlags & kAudioFormatFlagIsPacked)) { |
| 754 |
GST_WARNING ("Only packed formats supported"); |
| 755 |
goto error; |
| 756 |
} |
| 757 |
|
| 758 |
if (asbd->mFormatFlags & kLinearPCMFormatFlagsSampleFractionMask) { |
| 759 |
GST_WARNING ("Fixed point audio is unsupported"); |
| 760 |
goto error; |
| 761 |
} |
| 762 |
|
| 763 |
rate = asbd->mSampleRate; |
| 764 |
if (rate == kAudioStreamAnyRate) { |
| 765 |
GST_WARNING ("No sample rate"); |
| 766 |
goto error; |
| 767 |
} |
| 768 |
|
| 769 |
bps = asbd->mBitsPerChannel; |
| 770 |
endianness = asbd->mFormatFlags & kAudioFormatFlagIsBigEndian ? |
| 771 |
G_BIG_ENDIAN : G_LITTLE_ENDIAN; |
| 772 |
sign = asbd->mFormatFlags & kAudioFormatFlagIsSignedInteger ? TRUE : FALSE; |
| 773 |
|
| 774 |
if (asbd->mFormatFlags & kAudioFormatFlagIsFloat) { |
| 775 |
if (bps == 32) { |
| 776 |
if (endianness == G_LITTLE_ENDIAN) |
| 777 |
format = GST_AUDIO_FORMAT_F32LE; |
| 778 |
else |
| 779 |
format = GST_AUDIO_FORMAT_F32BE; |
| 780 |
|
| 781 |
} else if (bps == 64) { |
| 782 |
if (endianness == G_LITTLE_ENDIAN) |
| 783 |
format = GST_AUDIO_FORMAT_F64LE; |
| 784 |
else |
| 785 |
format = GST_AUDIO_FORMAT_F64BE; |
| 786 |
} |
| 787 |
} else { |
| 788 |
format = gst_audio_format_build_integer (sign, endianness, bps, bps); |
| 789 |
} |
| 790 |
|
| 791 |
if (format == GST_AUDIO_FORMAT_UNKNOWN) { |
| 792 |
GST_WARNING ("Unsupported sample format"); |
| 793 |
goto error; |
| 794 |
} |
| 795 |
|
| 796 |
if (layout) { |
| 797 |
if (!gst_core_audio_parse_channel_layout (layout, device_id, &channels, |
| 798 |
&channel_mask, pos)) { |
| 799 |
GST_WARNING |
| 800 |
("Failed to parse channel layout, best effort channels layout mapping will be used"); |
| 801 |
layout = NULL; |
| 802 |
} |
| 803 |
} |
| 804 |
|
| 805 |
if (layout) { |
| 806 |
/* The AU can have arbitrary channel order, but we're using GstAudioInfo |
| 807 |
* which supports only the GStreamer channel order. |
| 808 |
* Also, we're eventually producing caps, which only have channel-mask |
| 809 |
* (whose implied order is the GStreamer channel order). */ |
| 810 |
gst_audio_channel_positions_to_valid_order (pos, channels); |
| 811 |
|
| 812 |
gst_audio_info_set_format (&info, format, rate, channels, pos); |
| 813 |
} else { |
| 814 |
channels = MIN (asbd->mChannelsPerFrame, GST_OSX_AUDIO_MAX_CHANNEL); |
| 815 |
gst_audio_info_set_format (&info, format, rate, channels, NULL); |
| 816 |
} |
| 817 |
|
| 818 |
return gst_audio_info_to_caps (&info); |
| 819 |
|
| 820 |
error: |
| 821 |
return NULL; |
| 822 |
} |
| 823 |
|
| 824 |
static gboolean |
| 825 |
_core_audio_get_property (GstCoreAudio * core_audio, gboolean outer, |
| 826 |
AudioUnitPropertyID inID, void *inData, UInt32 * inDataSize) |
| 827 |
{ |
| 828 |
OSStatus status; |
| 829 |
AudioUnitScope scope; |
| 830 |
AudioUnitElement element; |
| 831 |
|
| 832 |
scope = outer ? |
| 833 |
CORE_AUDIO_OUTER_SCOPE (core_audio) : CORE_AUDIO_INNER_SCOPE (core_audio); |
| 834 |
element = CORE_AUDIO_ELEMENT (core_audio); |
| 835 |
|
| 836 |
status = |
| 837 |
AudioUnitGetProperty (core_audio->audiounit, inID, scope, element, inData, |
| 838 |
inDataSize); |
| 839 |
|
| 840 |
return status == noErr; |
| 841 |
} |
| 842 |
|
| 843 |
static gboolean |
| 844 |
_core_audio_get_stream_format (GstCoreAudio * core_audio, |
| 845 |
AudioStreamBasicDescription * asbd, gboolean outer) |
| 846 |
{ |
| 847 |
UInt32 size; |
| 848 |
|
| 849 |
size = sizeof (AudioStreamBasicDescription); |
| 850 |
return _core_audio_get_property (core_audio, outer, |
| 851 |
kAudioUnitProperty_StreamFormat, asbd, &size); |
| 852 |
} |
| 853 |
|
| 854 |
AudioChannelLayout * |
| 855 |
gst_core_audio_get_channel_layout (GstCoreAudio * core_audio, gboolean outer) |
| 856 |
{ |
| 857 |
UInt32 size; |
| 858 |
AudioChannelLayout *layout; |
| 859 |
|
| 860 |
if (core_audio->is_src) { |
| 861 |
GST_WARNING_OBJECT (core_audio, |
| 862 |
"gst_core_audio_get_channel_layout not supported on source."); |
| 863 |
return NULL; |
| 864 |
} |
| 865 |
|
| 866 |
if (!_core_audio_get_property (core_audio, outer, |
| 867 |
kAudioUnitProperty_AudioChannelLayout, NULL, &size)) { |
| 868 |
GST_WARNING_OBJECT (core_audio, "unable to get channel layout"); |
| 869 |
return NULL; |
| 870 |
} |
| 871 |
|
| 872 |
layout = g_malloc (size); |
| 873 |
if (!_core_audio_get_property (core_audio, outer, |
| 874 |
kAudioUnitProperty_AudioChannelLayout, layout, &size)) { |
| 875 |
GST_WARNING_OBJECT (core_audio, "unable to get channel layout"); |
| 876 |
g_free (layout); |
| 877 |
return NULL; |
| 878 |
} |
| 879 |
|
| 880 |
return layout; |
| 881 |
} |
| 882 |
|
| 883 |
#define STEREO_CHANNEL_MASK \ |
| 884 |
(GST_AUDIO_CHANNEL_POSITION_MASK (FRONT_LEFT) | \ |
| 885 |
GST_AUDIO_CHANNEL_POSITION_MASK (FRONT_RIGHT)) |
| 886 |
|
| 887 |
GstCaps * |
| 888 |
gst_core_audio_probe_caps (GstCoreAudio * core_audio, GstCaps * in_caps) |
| 889 |
{ |
| 890 |
guint i, channels, channels_max = 0; |
| 891 |
gboolean spdif_allowed; |
| 892 |
AudioChannelLayout *layout; |
| 893 |
AudioStreamBasicDescription outer_asbd; |
| 894 |
gboolean got_outer_asbd; |
| 895 |
GstCaps *caps = NULL; |
| 896 |
guint64 channel_mask; |
| 897 |
|
| 898 |
/* Get the ASBD of the outer scope (i.e. input scope of Input, |
| 899 |
* output scope of Output). |
| 900 |
* This ASBD indicates the hardware format. */ |
| 901 |
got_outer_asbd = |
| 902 |
_core_audio_get_stream_format (core_audio, &outer_asbd, TRUE); |
| 903 |
|
| 904 |
/* Collect info about the HW capabilities and preferences */ |
| 905 |
spdif_allowed = |
| 906 |
gst_core_audio_audio_device_is_spdif_avail (core_audio->device_id); |
| 907 |
if (!core_audio->is_src) |
| 908 |
layout = gst_core_audio_get_channel_layout (core_audio, TRUE); |
| 909 |
else |
| 910 |
layout = NULL; /* no supported for sources */ |
| 911 |
|
| 912 |
GST_DEBUG_OBJECT (core_audio, "Selected device ID: %u SPDIF allowed: %d", |
| 913 |
(unsigned) core_audio->device_id, spdif_allowed); |
| 914 |
|
| 915 |
if (layout) { |
| 916 |
if (!gst_core_audio_parse_channel_layout (layout, core_audio->device_id, |
| 917 |
&channels, &channel_mask, NULL)) { |
| 918 |
GST_WARNING_OBJECT (core_audio, "Failed to parse channel layout"); |
| 919 |
channel_mask = 0; |
| 920 |
} |
| 921 |
|
| 922 |
if (channel_mask != 0 && channels > 2 && |
| 923 |
layout->mChannelLayoutTag == |
| 924 |
kAudioChannelLayoutTag_UseChannelDescriptions) { |
| 925 |
/* CoreAudio gave us a positioned layout, which might mean we're ignoring some unpositioned channels. |
| 926 |
* For example, with a 64ch output, macOS only allows assigning positions to 16 channels at most. |
| 927 |
* Let's make sure we also expose the actual maximum amount of channels in our caps, |
| 928 |
* without any positions assigned. */ |
| 929 |
channels_max = |
| 930 |
MIN (layout->mNumberChannelDescriptions, GST_OSX_AUDIO_MAX_CHANNEL); |
| 931 |
} |
| 932 |
|
| 933 |
/* If available, start with the preferred caps. */ |
| 934 |
if (got_outer_asbd) |
| 935 |
caps = |
| 936 |
gst_core_audio_asbd_to_caps (&outer_asbd, core_audio->device_id, |
| 937 |
layout); |
| 938 |
|
| 939 |
g_free (layout); |
| 940 |
} else if (got_outer_asbd) { |
| 941 |
channels = outer_asbd.mChannelsPerFrame; |
| 942 |
channel_mask = 0; |
| 943 |
/* If available, start with the preferred caps */ |
| 944 |
caps = |
| 945 |
gst_core_audio_asbd_to_caps (&outer_asbd, core_audio->device_id, NULL); |
| 946 |
} else { |
| 947 |
GST_ERROR_OBJECT (core_audio, |
| 948 |
"Unable to get any information about hardware"); |
| 949 |
return NULL; |
| 950 |
} |
| 951 |
|
| 952 |
/* Append the allowed subset based on the template caps */ |
| 953 |
if (!caps) |
| 954 |
caps = gst_caps_new_empty (); |
| 955 |
for (i = 0; i < gst_caps_get_size (in_caps); i++) { |
| 956 |
GstStructure *in_s; |
| 957 |
|
| 958 |
in_s = gst_caps_get_structure (in_caps, i); |
| 959 |
|
| 960 |
if (gst_structure_has_name (in_s, "audio/x-ac3") || |
| 961 |
gst_structure_has_name (in_s, "audio/x-dts")) { |
| 962 |
if (spdif_allowed) { |
| 963 |
gst_caps_append_structure (caps, gst_structure_copy (in_s)); |
| 964 |
} |
| 965 |
} else { |
| 966 |
GstStructure *out_s; |
| 967 |
|
| 968 |
out_s = gst_structure_copy (in_s); |
| 969 |
gst_structure_set (out_s, "channels", G_TYPE_INT, channels, NULL); |
| 970 |
if (channel_mask != 0) { |
| 971 |
/* positioned layout */ |
| 972 |
gst_structure_set (out_s, |
| 973 |
"channel-mask", GST_TYPE_BITMASK, channel_mask, NULL); |
| 974 |
} else { |
| 975 |
/* unpositioned layout */ |
| 976 |
gst_structure_remove_field (out_s, "channel-mask"); |
| 977 |
} |
| 978 |
|
| 979 |
#if TARGET_OS_OSX |
| 980 |
if (core_audio->is_src && got_outer_asbd |
| 981 |
&& outer_asbd.mSampleRate != kAudioStreamAnyRate) { |
| 982 |
/* According to Core Audio engineer, AUHAL does not support sample rate conversion. |
| 983 |
* on sources. Therefore, we fixate the sample rate. |
| 984 |
* |
| 985 |
* "You definitely cannot do rate conversion as part of getting input from AUHAL. |
| 986 |
* That's the most common cause of those "cannot do in current context" errors." |
| 987 |
* http://lists.apple.com/archives/coreaudio-api/2006/Sep/msg00088.html |
| 988 |
*/ |
| 989 |
gst_structure_set (out_s, "rate", G_TYPE_INT, |
| 990 |
(gint) outer_asbd.mSampleRate, NULL); |
| 991 |
} |
| 992 |
#endif |
| 993 |
|
| 994 |
/* Special cases for upmixing and downmixing. |
| 995 |
* Other than that, the AUs don't upmix or downmix multi-channel audio, |
| 996 |
* e.g. if you push 5.1-surround audio to a stereo configuration, |
| 997 |
* the left and right channels will be played accordingly, |
| 998 |
* and the rest will be dropped. */ |
| 999 |
if (channels == 1) { |
| 1000 |
/* If have mono, then also offer stereo since CoreAudio downmixes to it */ |
| 1001 |
GstStructure *stereo = gst_structure_copy (out_s); |
| 1002 |
gst_structure_remove_field (out_s, "channel-mask"); |
| 1003 |
gst_structure_set (stereo, "channels", G_TYPE_INT, 2, |
| 1004 |
"channel-mask", GST_TYPE_BITMASK, STEREO_CHANNEL_MASK, NULL); |
| 1005 |
gst_caps_append_structure (caps, stereo); |
| 1006 |
gst_caps_append_structure (caps, out_s); |
| 1007 |
} else if (channels == 2 && (channel_mask == 0 |
| 1008 |
|| channel_mask == STEREO_CHANNEL_MASK)) { |
| 1009 |
/* If have stereo channels, then also offer mono since CoreAudio |
| 1010 |
* upmixes it. */ |
| 1011 |
GstStructure *mono = gst_structure_copy (out_s); |
| 1012 |
gst_structure_set (mono, "channels", G_TYPE_INT, 1, NULL); |
| 1013 |
gst_structure_remove_field (mono, "channel-mask"); |
| 1014 |
gst_structure_set (out_s, "channel-mask", GST_TYPE_BITMASK, |
| 1015 |
STEREO_CHANNEL_MASK, NULL); |
| 1016 |
|
| 1017 |
gst_caps_append_structure (caps, out_s); |
| 1018 |
gst_caps_append_structure (caps, mono); |
| 1019 |
} else { |
| 1020 |
/* Otherwise, if needed, add an unpositioned max-channels variant ... */ |
| 1021 |
if (channels_max > 0) { |
| 1022 |
GstStructure *unpos_s = gst_structure_copy (in_s); |
| 1023 |
gst_structure_set (unpos_s, "channels", G_TYPE_INT, channels_max, |
| 1024 |
NULL); |
| 1025 |
gst_structure_set (unpos_s, "channel-mask", GST_TYPE_BITMASK, 0, |
| 1026 |
NULL); |
| 1027 |
gst_caps_append_structure (caps, unpos_s); |
| 1028 |
} |
| 1029 |
|
| 1030 |
/* ... and just add the caps */ |
| 1031 |
gst_caps_append_structure (caps, out_s); |
| 1032 |
} |
| 1033 |
} |
| 1034 |
} |
| 1035 |
|
| 1036 |
GST_DEBUG_OBJECT (core_audio, "Probed caps:%" GST_PTR_FORMAT, caps); |
| 1037 |
return caps; |
| 1038 |
} |