| 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 "gstosxcoreaudiocommon.h" |
| 25 |
|
| 26 |
GST_DEBUG_CATEGORY_EXTERN (osx_coreaudio_debug); |
| 27 |
#define GST_CAT_DEFAULT osx_coreaudio_debug |
| 28 |
|
| 29 |
void |
| 30 |
gst_core_audio_remove_render_callback (GstCoreAudio * core_audio) |
| 31 |
{ |
| 32 |
AURenderCallbackStruct input; |
| 33 |
OSStatus status; |
| 34 |
AudioUnitPropertyID callback_type; |
| 35 |
|
| 36 |
/* Deactivate the render callback by calling SetRenderCallback |
| 37 |
* with a NULL inputProc. |
| 38 |
*/ |
| 39 |
input.inputProc = NULL; |
| 40 |
input.inputProcRefCon = NULL; |
| 41 |
callback_type = core_audio->is_src ? |
| 42 |
kAudioOutputUnitProperty_SetInputCallback : |
| 43 |
kAudioUnitProperty_SetRenderCallback; |
| 44 |
|
| 45 |
status = AudioUnitSetProperty (core_audio->audiounit, callback_type, kAudioUnitScope_Global, 0, /* N/A for global */ |
| 46 |
&input, sizeof (input)); |
| 47 |
|
| 48 |
if (status) { |
| 49 |
GST_WARNING_OBJECT (core_audio->osxbuf, |
| 50 |
"Failed to remove render callback %d", (int) status); |
| 51 |
} |
| 52 |
|
| 53 |
/* We're deactivated.. */ |
| 54 |
g_atomic_int_set (&core_audio->io_proc_dropping, FALSE); |
| 55 |
core_audio->io_proc_active = FALSE; |
| 56 |
} |
| 57 |
|
| 58 |
gboolean |
| 59 |
gst_core_audio_io_proc_start (GstCoreAudio * core_audio) |
| 60 |
{ |
| 61 |
OSStatus status; |
| 62 |
AURenderCallbackStruct input; |
| 63 |
AudioUnitPropertyID callback_type; |
| 64 |
|
| 65 |
GST_DEBUG_OBJECT (core_audio->osxbuf, |
| 66 |
"osx ring buffer start ioproc: %p device_id %lu", |
| 67 |
core_audio->element->io_proc, (gulong) core_audio->device_id); |
| 68 |
if (!core_audio->io_proc_active) { |
| 69 |
callback_type = core_audio->is_src ? |
| 70 |
kAudioOutputUnitProperty_SetInputCallback : |
| 71 |
kAudioUnitProperty_SetRenderCallback; |
| 72 |
|
| 73 |
input.inputProc = (AURenderCallback) core_audio->element->io_proc; |
| 74 |
input.inputProcRefCon = core_audio->osxbuf; |
| 75 |
|
| 76 |
status = AudioUnitSetProperty (core_audio->audiounit, callback_type, kAudioUnitScope_Global, 0, /* N/A for global */ |
| 77 |
&input, sizeof (input)); |
| 78 |
|
| 79 |
if (status) { |
| 80 |
GST_ERROR_OBJECT (core_audio->osxbuf, |
| 81 |
"AudioUnitSetProperty failed: %d", (int) status); |
| 82 |
return FALSE; |
| 83 |
} |
| 84 |
|
| 85 |
core_audio->io_proc_active = TRUE; |
| 86 |
} |
| 87 |
|
| 88 |
g_atomic_int_set (&core_audio->io_proc_dropping, FALSE); |
| 89 |
|
| 90 |
// AudioOutputUnitStart on iOS can wait for the render callback to finish, |
| 91 |
// where in our case we set the ringbuffer timestamp, which also needs the ringbuf lock. |
| 92 |
GST_OBJECT_UNLOCK (core_audio->osxbuf); |
| 93 |
status = AudioOutputUnitStart (core_audio->audiounit); |
| 94 |
GST_OBJECT_LOCK (core_audio->osxbuf); |
| 95 |
if (status) { |
| 96 |
GST_ERROR_OBJECT (core_audio->osxbuf, "AudioOutputUnitStart failed: %d", |
| 97 |
(int) status); |
| 98 |
return FALSE; |
| 99 |
} |
| 100 |
return TRUE; |
| 101 |
} |
| 102 |
|
| 103 |
gboolean |
| 104 |
gst_core_audio_io_proc_stop (GstCoreAudio * core_audio) |
| 105 |
{ |
| 106 |
OSErr status; |
| 107 |
|
| 108 |
GST_DEBUG_OBJECT (core_audio->osxbuf, |
| 109 |
"osx ring buffer stop ioproc: %p device_id %lu", |
| 110 |
core_audio->element->io_proc, (gulong) core_audio->device_id); |
| 111 |
|
| 112 |
status = AudioOutputUnitStop (core_audio->audiounit); |
| 113 |
if (status) { |
| 114 |
GST_WARNING_OBJECT (core_audio->osxbuf, |
| 115 |
"AudioOutputUnitStop failed: %d", (int) status); |
| 116 |
} |
| 117 |
// Ok to remove directly here because we stopped the AudioUnit already |
| 118 |
if (core_audio->io_proc_active) { |
| 119 |
gst_core_audio_remove_render_callback (core_audio); |
| 120 |
} |
| 121 |
return TRUE; |
| 122 |
} |
| 123 |
|
| 124 |
AudioBufferList * |
| 125 |
buffer_list_alloc (UInt32 channels, UInt32 size, gboolean interleaved) |
| 126 |
{ |
| 127 |
AudioBufferList *list; |
| 128 |
gsize list_size; |
| 129 |
UInt32 num_buffers, n; |
| 130 |
|
| 131 |
num_buffers = interleaved ? 1 : channels; |
| 132 |
/* AudioBufferList member mBuffers is variable-length array */ |
| 133 |
list_size = G_STRUCT_OFFSET (AudioBufferList, mBuffers[num_buffers]); |
| 134 |
list = (AudioBufferList *) g_malloc (list_size); |
| 135 |
|
| 136 |
list->mNumberBuffers = num_buffers; |
| 137 |
for (n = 0; n < num_buffers; ++n) { |
| 138 |
/* See http://lists.apple.com/archives/coreaudio-api/2015/Feb/msg00027.html */ |
| 139 |
list->mBuffers[n].mNumberChannels = interleaved ? channels : 1; |
| 140 |
/* AudioUnitRender will keep overwriting mDataByteSize */ |
| 141 |
list->mBuffers[n].mDataByteSize = size; |
| 142 |
list->mBuffers[n].mData = g_malloc (size); |
| 143 |
} |
| 144 |
|
| 145 |
return list; |
| 146 |
} |
| 147 |
|
| 148 |
void |
| 149 |
buffer_list_free (AudioBufferList * list) |
| 150 |
{ |
| 151 |
UInt32 n; |
| 152 |
|
| 153 |
if (list == NULL) |
| 154 |
return; |
| 155 |
|
| 156 |
for (n = 0; n < list->mNumberBuffers; ++n) { |
| 157 |
g_free (list->mBuffers[n].mData); |
| 158 |
} |
| 159 |
|
| 160 |
g_free (list); |
| 161 |
} |
| 162 |
|
| 163 |
gboolean |
| 164 |
gst_core_audio_bind_device (GstCoreAudio * core_audio) |
| 165 |
{ |
| 166 |
OSStatus status; |
| 167 |
|
| 168 |
/* Specify which device we're using. */ |
| 169 |
GST_DEBUG_OBJECT (core_audio->osxbuf, "Bind AudioUnit to device %s", |
| 170 |
core_audio->unique_id); |
| 171 |
status = AudioUnitSetProperty (core_audio->audiounit, |
| 172 |
kAudioOutputUnitProperty_CurrentDevice, kAudioUnitScope_Global, 0, |
| 173 |
&core_audio->device_id, sizeof (AudioDeviceID)); |
| 174 |
if (status) { |
| 175 |
GST_ERROR_OBJECT (core_audio->osxbuf, "Failed binding to device: %d", |
| 176 |
(int) status); |
| 177 |
goto audiounit_error; |
| 178 |
} |
| 179 |
return TRUE; |
| 180 |
|
| 181 |
audiounit_error: |
| 182 |
if (core_audio->recBufferList) { |
| 183 |
buffer_list_free (core_audio->recBufferList); |
| 184 |
core_audio->recBufferList = NULL; |
| 185 |
} |
| 186 |
return FALSE; |
| 187 |
} |
| 188 |
|
| 189 |
static gboolean |
| 190 |
_core_audio_set_property (GstCoreAudio * core_audio, AudioUnitPropertyID inID, |
| 191 |
void *inData, UInt32 inDataSize) |
| 192 |
{ |
| 193 |
OSStatus status; |
| 194 |
AudioUnitScope scope; |
| 195 |
AudioUnitElement element; |
| 196 |
|
| 197 |
scope = CORE_AUDIO_INNER_SCOPE (core_audio); |
| 198 |
element = CORE_AUDIO_ELEMENT (core_audio); |
| 199 |
|
| 200 |
status = |
| 201 |
AudioUnitSetProperty (core_audio->audiounit, inID, scope, element, inData, |
| 202 |
inDataSize); |
| 203 |
|
| 204 |
if (status != noErr) { |
| 205 |
GST_WARNING_OBJECT (core_audio->osxbuf, |
| 206 |
"Failed to set Audio Unit property: %d", (int) status); |
| 207 |
return FALSE;; |
| 208 |
} |
| 209 |
|
| 210 |
return TRUE; |
| 211 |
} |
| 212 |
|
| 213 |
/* The AudioUnit must be uninitialized before calling this */ |
| 214 |
gboolean |
| 215 |
gst_core_audio_set_channel_layout (GstCoreAudio * core_audio, |
| 216 |
gint channels, GstCaps * caps) |
| 217 |
{ |
| 218 |
AudioChannelLayout *layout = NULL; |
| 219 |
gboolean ret; |
| 220 |
gsize layoutSize; |
| 221 |
gint i; |
| 222 |
GstStructure *structure; |
| 223 |
GstAudioChannelPosition positions[GST_OSX_AUDIO_MAX_CHANNEL]; |
| 224 |
guint64 channel_mask; |
| 225 |
|
| 226 |
g_return_val_if_fail (channels <= GST_OSX_AUDIO_MAX_CHANNEL, FALSE); |
| 227 |
|
| 228 |
/* Determine the channel positions */ |
| 229 |
structure = gst_caps_get_structure (caps, 0); |
| 230 |
channel_mask = 0; |
| 231 |
gst_structure_get (structure, "channel-mask", GST_TYPE_BITMASK, &channel_mask, |
| 232 |
NULL); |
| 233 |
|
| 234 |
if (channel_mask != 0) |
| 235 |
gst_audio_channel_positions_from_mask (channels, channel_mask, positions); |
| 236 |
|
| 237 |
/* AudioChannelLayout member mChannelDescriptions is variable-length array */ |
| 238 |
layoutSize = |
| 239 |
G_STRUCT_OFFSET (AudioChannelLayout, mChannelDescriptions[channels]); |
| 240 |
layout = g_malloc (layoutSize); |
| 241 |
|
| 242 |
/* Fill out the AudioChannelLayout */ |
| 243 |
layout->mChannelLayoutTag = kAudioChannelLayoutTag_UseChannelDescriptions; |
| 244 |
layout->mChannelBitmap = 0; /* Not used */ |
| 245 |
layout->mNumberChannelDescriptions = channels; |
| 246 |
for (i = 0; i < channels; i++) { |
| 247 |
if (channel_mask != 0) { |
| 248 |
layout->mChannelDescriptions[i].mChannelLabel = |
| 249 |
gst_audio_channel_position_to_core_audio (positions[i], i); |
| 250 |
} else { |
| 251 |
/* Discrete channel numbers are ORed into this */ |
| 252 |
layout->mChannelDescriptions[i].mChannelLabel = |
| 253 |
kAudioChannelLabel_Discrete_0 | i; |
| 254 |
} |
| 255 |
|
| 256 |
/* Others unused */ |
| 257 |
layout->mChannelDescriptions[i].mChannelFlags = kAudioChannelFlags_AllOff; |
| 258 |
layout->mChannelDescriptions[i].mCoordinates[0] = 0.f; |
| 259 |
layout->mChannelDescriptions[i].mCoordinates[1] = 0.f; |
| 260 |
layout->mChannelDescriptions[i].mCoordinates[2] = 0.f; |
| 261 |
} |
| 262 |
|
| 263 |
/* Sets GStreamer-ordered channel layout on the inner scope. |
| 264 |
* Reordering between the inner scope and outer scope is handled |
| 265 |
* by the Audio Unit itself. */ |
| 266 |
ret = _core_audio_set_property (core_audio, |
| 267 |
kAudioUnitProperty_AudioChannelLayout, layout, layoutSize); |
| 268 |
|
| 269 |
g_free (layout); |
| 270 |
return ret; |
| 271 |
} |
| 272 |
|
| 273 |
/* The AudioUnit must be uninitialized before calling this */ |
| 274 |
gboolean |
| 275 |
gst_core_audio_set_format (GstCoreAudio * core_audio, |
| 276 |
AudioStreamBasicDescription format) |
| 277 |
{ |
| 278 |
GST_DEBUG_OBJECT (core_audio->osxbuf, "Setting format for AudioUnit"); |
| 279 |
|
| 280 |
return _core_audio_set_property (core_audio, kAudioUnitProperty_StreamFormat, |
| 281 |
&format, sizeof (AudioStreamBasicDescription)); |
| 282 |
} |
| 283 |
|
| 284 |
gboolean |
| 285 |
gst_core_audio_open_device (GstCoreAudio * core_audio, OSType sub_type, |
| 286 |
const gchar * adesc) |
| 287 |
{ |
| 288 |
AudioComponentDescription desc; |
| 289 |
AudioComponent comp; |
| 290 |
OSStatus status; |
| 291 |
AudioUnit unit; |
| 292 |
UInt32 enableIO; |
| 293 |
|
| 294 |
desc.componentType = kAudioUnitType_Output; |
| 295 |
desc.componentSubType = sub_type; |
| 296 |
desc.componentManufacturer = kAudioUnitManufacturer_Apple; |
| 297 |
desc.componentFlags = 0; |
| 298 |
desc.componentFlagsMask = 0; |
| 299 |
|
| 300 |
comp = AudioComponentFindNext (NULL, &desc); |
| 301 |
|
| 302 |
if (comp == NULL) { |
| 303 |
GST_WARNING_OBJECT (core_audio->osxbuf, "Couldn't find %s component", |
| 304 |
adesc); |
| 305 |
return FALSE; |
| 306 |
} |
| 307 |
|
| 308 |
status = AudioComponentInstanceNew (comp, &unit); |
| 309 |
|
| 310 |
if (status) { |
| 311 |
GST_ERROR_OBJECT (core_audio->osxbuf, "Couldn't open %s component %d", |
| 312 |
adesc, (int) status); |
| 313 |
return FALSE; |
| 314 |
} |
| 315 |
|
| 316 |
if (core_audio->is_src) { |
| 317 |
/* enable input */ |
| 318 |
enableIO = 1; |
| 319 |
status = AudioUnitSetProperty (unit, kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Input, 1, /* 1 = input element */ |
| 320 |
&enableIO, sizeof (enableIO)); |
| 321 |
|
| 322 |
if (status) { |
| 323 |
AudioComponentInstanceDispose (unit); |
| 324 |
GST_WARNING_OBJECT (core_audio->osxbuf, "Failed to enable input: %d", |
| 325 |
(int) status); |
| 326 |
return FALSE; |
| 327 |
} |
| 328 |
|
| 329 |
/* disable output */ |
| 330 |
enableIO = 0; |
| 331 |
status = AudioUnitSetProperty (unit, kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Output, 0, /* 0 = output element */ |
| 332 |
&enableIO, sizeof (enableIO)); |
| 333 |
|
| 334 |
if (status) { |
| 335 |
AudioComponentInstanceDispose (unit); |
| 336 |
GST_WARNING_OBJECT (core_audio->osxbuf, "Failed to disable output: %d", |
| 337 |
(int) status); |
| 338 |
return FALSE; |
| 339 |
} |
| 340 |
} |
| 341 |
|
| 342 |
GST_DEBUG_OBJECT (core_audio->osxbuf, "Created %s AudioUnit: %p", adesc, |
| 343 |
unit); |
| 344 |
core_audio->audiounit = unit; |
| 345 |
return TRUE; |
| 346 |
} |
| 347 |
|
| 348 |
AudioChannelLabel |
| 349 |
gst_audio_channel_position_to_core_audio (GstAudioChannelPosition |
| 350 |
position, int channel) |
| 351 |
{ |
| 352 |
switch (position) { |
| 353 |
case GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT: |
| 354 |
return kAudioChannelLabel_Left; |
| 355 |
case GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT: |
| 356 |
return kAudioChannelLabel_Right; |
| 357 |
case GST_AUDIO_CHANNEL_POSITION_REAR_CENTER: |
| 358 |
return kAudioChannelLabel_CenterSurround; |
| 359 |
case GST_AUDIO_CHANNEL_POSITION_REAR_LEFT: |
| 360 |
return kAudioChannelLabel_RearSurroundLeft; |
| 361 |
case GST_AUDIO_CHANNEL_POSITION_REAR_RIGHT: |
| 362 |
return kAudioChannelLabel_RearSurroundRight; |
| 363 |
case GST_AUDIO_CHANNEL_POSITION_LFE1: |
| 364 |
return kAudioChannelLabel_LFEScreen; |
| 365 |
case GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER: |
| 366 |
return kAudioChannelLabel_Center; |
| 367 |
case GST_AUDIO_CHANNEL_POSITION_SIDE_LEFT: |
| 368 |
return kAudioChannelLabel_LeftSurroundDirect; |
| 369 |
case GST_AUDIO_CHANNEL_POSITION_SIDE_RIGHT: |
| 370 |
return kAudioChannelLabel_RightSurroundDirect; |
| 371 |
case GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER: |
| 372 |
return kAudioChannelLabel_LeftCenter; |
| 373 |
case GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER: |
| 374 |
return kAudioChannelLabel_RightCenter; |
| 375 |
case GST_AUDIO_CHANNEL_POSITION_TOP_REAR_LEFT: |
| 376 |
return kAudioChannelLabel_TopBackLeft; |
| 377 |
case GST_AUDIO_CHANNEL_POSITION_TOP_REAR_CENTER: |
| 378 |
return kAudioChannelLabel_TopBackCenter; |
| 379 |
case GST_AUDIO_CHANNEL_POSITION_TOP_REAR_RIGHT: |
| 380 |
return kAudioChannelLabel_TopBackRight; |
| 381 |
case GST_AUDIO_CHANNEL_POSITION_WIDE_LEFT: |
| 382 |
return kAudioChannelLabel_LeftWide; |
| 383 |
case GST_AUDIO_CHANNEL_POSITION_WIDE_RIGHT: |
| 384 |
return kAudioChannelLabel_RightWide; |
| 385 |
case GST_AUDIO_CHANNEL_POSITION_LFE2: |
| 386 |
return kAudioChannelLabel_LFE2; |
| 387 |
case GST_AUDIO_CHANNEL_POSITION_TOP_FRONT_LEFT: |
| 388 |
return kAudioChannelLabel_VerticalHeightLeft; |
| 389 |
case GST_AUDIO_CHANNEL_POSITION_TOP_FRONT_RIGHT: |
| 390 |
return kAudioChannelLabel_VerticalHeightRight; |
| 391 |
case GST_AUDIO_CHANNEL_POSITION_TOP_FRONT_CENTER: |
| 392 |
return kAudioChannelLabel_VerticalHeightCenter; |
| 393 |
case GST_AUDIO_CHANNEL_POSITION_TOP_CENTER: |
| 394 |
return kAudioChannelLabel_TopCenterSurround; |
| 395 |
case GST_AUDIO_CHANNEL_POSITION_SURROUND_LEFT: |
| 396 |
return kAudioChannelLabel_LeftSurround; |
| 397 |
case GST_AUDIO_CHANNEL_POSITION_SURROUND_RIGHT: |
| 398 |
return kAudioChannelLabel_RightSurround; |
| 399 |
|
| 400 |
/* Special position values */ |
| 401 |
case GST_AUDIO_CHANNEL_POSITION_NONE: |
| 402 |
return kAudioChannelLabel_Discrete_0 | channel; |
| 403 |
case GST_AUDIO_CHANNEL_POSITION_MONO: |
| 404 |
return kAudioChannelLabel_Mono; |
| 405 |
|
| 406 |
/* Following positions are unmapped -- |
| 407 |
* i.e. mapped to kAudioChannelLabel_Unknown: */ |
| 408 |
case GST_AUDIO_CHANNEL_POSITION_TOP_SIDE_LEFT: |
| 409 |
case GST_AUDIO_CHANNEL_POSITION_TOP_SIDE_RIGHT: |
| 410 |
case GST_AUDIO_CHANNEL_POSITION_BOTTOM_FRONT_CENTER: |
| 411 |
case GST_AUDIO_CHANNEL_POSITION_BOTTOM_FRONT_LEFT: |
| 412 |
case GST_AUDIO_CHANNEL_POSITION_BOTTOM_FRONT_RIGHT: |
| 413 |
default: |
| 414 |
return kAudioChannelLabel_Unknown; |
| 415 |
} |
| 416 |
} |
| 417 |
|
| 418 |
/* Performs a best-effort conversion. 'channel' is used for warnings only. */ |
| 419 |
GstAudioChannelPosition |
| 420 |
gst_core_audio_channel_label_to_gst (AudioChannelLabel label, |
| 421 |
int channel, gboolean warn, AudioDeviceID device_id) |
| 422 |
{ |
| 423 |
switch (label) { |
| 424 |
case kAudioChannelLabel_Left: |
| 425 |
return GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT; |
| 426 |
case kAudioChannelLabel_Right: |
| 427 |
return GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT; |
| 428 |
case kAudioChannelLabel_Center: |
| 429 |
return GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER; |
| 430 |
case kAudioChannelLabel_LFEScreen: |
| 431 |
return GST_AUDIO_CHANNEL_POSITION_LFE1; |
| 432 |
case kAudioChannelLabel_LeftSurround: |
| 433 |
return GST_AUDIO_CHANNEL_POSITION_SURROUND_LEFT; |
| 434 |
case kAudioChannelLabel_RightSurround: |
| 435 |
return GST_AUDIO_CHANNEL_POSITION_SURROUND_RIGHT; |
| 436 |
case kAudioChannelLabel_LeftSurroundDirect: |
| 437 |
return GST_AUDIO_CHANNEL_POSITION_SIDE_LEFT; |
| 438 |
case kAudioChannelLabel_RightSurroundDirect: |
| 439 |
return GST_AUDIO_CHANNEL_POSITION_SIDE_RIGHT; |
| 440 |
case kAudioChannelLabel_CenterSurround: |
| 441 |
return GST_AUDIO_CHANNEL_POSITION_REAR_CENTER; |
| 442 |
case kAudioChannelLabel_LeftCenter: |
| 443 |
return GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER; |
| 444 |
case kAudioChannelLabel_RightCenter: |
| 445 |
return GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER; |
| 446 |
case kAudioChannelLabel_TopBackLeft: |
| 447 |
return GST_AUDIO_CHANNEL_POSITION_TOP_REAR_LEFT; |
| 448 |
case kAudioChannelLabel_TopBackCenter: |
| 449 |
return GST_AUDIO_CHANNEL_POSITION_TOP_REAR_CENTER; |
| 450 |
case kAudioChannelLabel_TopBackRight: |
| 451 |
return GST_AUDIO_CHANNEL_POSITION_TOP_REAR_RIGHT; |
| 452 |
case kAudioChannelLabel_LeftWide: |
| 453 |
return GST_AUDIO_CHANNEL_POSITION_WIDE_LEFT; |
| 454 |
case kAudioChannelLabel_RightWide: |
| 455 |
return GST_AUDIO_CHANNEL_POSITION_WIDE_RIGHT; |
| 456 |
case kAudioChannelLabel_LFE2: |
| 457 |
return GST_AUDIO_CHANNEL_POSITION_LFE2; |
| 458 |
case kAudioChannelLabel_VerticalHeightLeft: |
| 459 |
return GST_AUDIO_CHANNEL_POSITION_TOP_FRONT_LEFT; |
| 460 |
case kAudioChannelLabel_VerticalHeightRight: |
| 461 |
return GST_AUDIO_CHANNEL_POSITION_TOP_FRONT_RIGHT; |
| 462 |
case kAudioChannelLabel_VerticalHeightCenter: |
| 463 |
return GST_AUDIO_CHANNEL_POSITION_TOP_FRONT_CENTER; |
| 464 |
case kAudioChannelLabel_RearSurroundLeft: |
| 465 |
return GST_AUDIO_CHANNEL_POSITION_REAR_LEFT; |
| 466 |
case kAudioChannelLabel_RearSurroundRight: |
| 467 |
return GST_AUDIO_CHANNEL_POSITION_REAR_RIGHT; |
| 468 |
case kAudioChannelLabel_TopCenterSurround: |
| 469 |
return GST_AUDIO_CHANNEL_POSITION_TOP_CENTER; |
| 470 |
|
| 471 |
/* Special position values */ |
| 472 |
|
| 473 |
case kAudioChannelLabel_Mono: |
| 474 |
/* GST_AUDIO_CHANNEL_POSITION_MONO is only for 1-channel layouts */ |
| 475 |
return GST_AUDIO_CHANNEL_POSITION_INVALID; |
| 476 |
case kAudioChannelLabel_Discrete: |
| 477 |
return GST_AUDIO_CHANNEL_POSITION_NONE; |
| 478 |
|
| 479 |
/* |
| 480 |
Following labels are unmapped -- |
| 481 |
i.e. mapped to GST_AUDIO_CHANNEL_POSITION_INVALID: |
| 482 |
*/ |
| 483 |
case kAudioChannelLabel_LeftTotal: |
| 484 |
case kAudioChannelLabel_RightTotal: |
| 485 |
case kAudioChannelLabel_HearingImpaired: |
| 486 |
case kAudioChannelLabel_Narration: |
| 487 |
case kAudioChannelLabel_DialogCentricMix: |
| 488 |
case kAudioChannelLabel_CenterSurroundDirect: |
| 489 |
case kAudioChannelLabel_Haptic: |
| 490 |
default: |
| 491 |
if (label >> 16 != 0) { /* kAudioChannelLabel_Discrete_N */ |
| 492 |
/* no way to store discrete channel order */ |
| 493 |
if (warn) |
| 494 |
GST_WARNING |
| 495 |
("Device %d has kAudioChannelLabel_Discrete_N channels, order will be lost", |
| 496 |
device_id); |
| 497 |
return GST_AUDIO_CHANNEL_POSITION_NONE; |
| 498 |
} else { |
| 499 |
if (warn) |
| 500 |
GST_WARNING |
| 501 |
("Device %d channel %u has unsupported label %d, skipping", |
| 502 |
device_id, channel, (int) label); |
| 503 |
return GST_AUDIO_CHANNEL_POSITION_INVALID; |
| 504 |
} |
| 505 |
} |
| 506 |
} |
| 507 |
|
| 508 |
void |
| 509 |
gst_core_audio_dump_channel_layout (AudioChannelLayout * channel_layout) |
| 510 |
{ |
| 511 |
UInt32 i; |
| 512 |
|
| 513 |
GST_DEBUG ("mChannelLayoutTag: 0x%lx", |
| 514 |
(unsigned long) channel_layout->mChannelLayoutTag); |
| 515 |
GST_DEBUG ("mChannelBitmap: 0x%lx", |
| 516 |
(unsigned long) channel_layout->mChannelBitmap); |
| 517 |
GST_DEBUG ("mNumberChannelDescriptions: %lu", |
| 518 |
(unsigned long) channel_layout->mNumberChannelDescriptions); |
| 519 |
for (i = 0; i < channel_layout->mNumberChannelDescriptions; i++) { |
| 520 |
AudioChannelDescription *channel_desc = |
| 521 |
&channel_layout->mChannelDescriptions[i]; |
| 522 |
GST_DEBUG (" mChannelLabel: 0x%lx mChannelFlags: 0x%lx " |
| 523 |
"mCoordinates[0]: %f mCoordinates[1]: %f " |
| 524 |
"mCoordinates[2]: %f", |
| 525 |
(unsigned long) channel_desc->mChannelLabel, |
| 526 |
(unsigned long) channel_desc->mChannelFlags, |
| 527 |
channel_desc->mCoordinates[0], channel_desc->mCoordinates[1], |
| 528 |
channel_desc->mCoordinates[2]); |
| 529 |
} |
| 530 |
} |
| 531 |
|
| 532 |
#if TARGET_OS_OSX |
| 533 |
char * |
| 534 |
gst_core_audio_device_get_prop_str (AudioDeviceID device_id, |
| 535 |
AudioObjectPropertyElement prop_id) |
| 536 |
{ |
| 537 |
OSStatus status = noErr; |
| 538 |
UInt32 propertySize = 0; |
| 539 |
CFStringRef prop_val; |
| 540 |
gchar *result = NULL; |
| 541 |
|
| 542 |
AudioObjectPropertyAddress propAddress = { |
| 543 |
prop_id, |
| 544 |
kAudioDevicePropertyScopeOutput, |
| 545 |
kAudioObjectPropertyElementMain |
| 546 |
}; |
| 547 |
|
| 548 |
propAddress.mScope = kAudioObjectPropertyScopeGlobal; |
| 549 |
|
| 550 |
/* Get the length of the device name */ |
| 551 |
status = AudioObjectGetPropertyDataSize (device_id, |
| 552 |
&propAddress, 0, NULL, &propertySize); |
| 553 |
if (status != noErr) { |
| 554 |
goto beach; |
| 555 |
} |
| 556 |
|
| 557 |
/* Get the requested property */ |
| 558 |
status = AudioObjectGetPropertyData (device_id, |
| 559 |
&propAddress, 0, NULL, &propertySize, &prop_val); |
| 560 |
if (status != noErr) { |
| 561 |
goto beach; |
| 562 |
} |
| 563 |
|
| 564 |
/* Convert to UTF-8 C String */ |
| 565 |
CFIndex prop_len = CFStringGetLength (prop_val); |
| 566 |
CFIndex max_size = |
| 567 |
CFStringGetMaximumSizeForEncoding (prop_len, kCFStringEncodingUTF8) + 1; |
| 568 |
result = g_malloc (max_size); |
| 569 |
|
| 570 |
if (!CFStringGetCString (prop_val, result, max_size, kCFStringEncodingUTF8)) { |
| 571 |
g_free (result); |
| 572 |
result = NULL; |
| 573 |
} |
| 574 |
|
| 575 |
CFRelease (prop_val); |
| 576 |
|
| 577 |
beach: |
| 578 |
return result; |
| 579 |
} |
| 580 |
|
| 581 |
UInt32 |
| 582 |
gst_core_audio_device_get_prop_uint32 (AudioDeviceID device_id, |
| 583 |
AudioObjectPropertyElement prop_id) |
| 584 |
{ |
| 585 |
OSStatus status = noErr; |
| 586 |
UInt32 propertySize = sizeof (UInt32); |
| 587 |
UInt32 result = UINT32_MAX; |
| 588 |
|
| 589 |
AudioObjectPropertyAddress propAddress = { |
| 590 |
.mSelector = prop_id, |
| 591 |
.mScope = kAudioObjectPropertyScopeGlobal, |
| 592 |
.mElement = kAudioObjectPropertyElementMain, |
| 593 |
}; |
| 594 |
|
| 595 |
/* Get the requested property */ |
| 596 |
status = AudioObjectGetPropertyData (device_id, |
| 597 |
&propAddress, 0, NULL, &propertySize, &result); |
| 598 |
if (status != noErr) { |
| 599 |
GST_WARNING ("Device %i, error code %i while fetching property %" |
| 600 |
GST_FOURCC_FORMAT, device_id, status, |
| 601 |
GST_FOURCC_ARGS (GUINT32_FROM_BE (prop_id))); |
| 602 |
} |
| 603 |
|
| 604 |
return result; |
| 605 |
} |
| 606 |
#endif |