Debian Codegraph @

sys/osxaudio/gstosxcoreaudiohal.c

raw
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 <unistd.h> /* for getpid */
25 #include "gstosxaudiosink.h"
26
27 #if defined(MAC_OS_X_VERSION_MAX_ALLOWED) && MAC_OS_X_VERSION_MAX_ALLOWED < 120000
28 #define kAudioObjectPropertyElementMain kAudioObjectPropertyElementMaster
29 #endif
30
31 static inline gboolean
32 _audio_system_set_runloop (CFRunLoopRef runLoop)
33 {
34 OSStatus status = noErr;
35
36 gboolean res = FALSE;
37
38 AudioObjectPropertyAddress runloopAddress = {
39 kAudioHardwarePropertyRunLoop,
40 kAudioObjectPropertyScopeGlobal,
41 kAudioObjectPropertyElementMain
42 };
43
44 status = AudioObjectSetPropertyData (kAudioObjectSystemObject,
45 &runloopAddress, 0, NULL, sizeof (CFRunLoopRef), &runLoop);
46 if (status == noErr) {
47 res = TRUE;
48 } else {
49 GST_ERROR ("failed to set runloop to %p: %d", runLoop, (int) status);
50 }
51
52 return res;
53 }
54
55 typedef struct
56 {
57 AudioDeviceID id;
58 char *unique_id;
59 } GstOsxAudioDevice;
60
61 static void
62 _free_gst_osx_audio_device (gpointer data)
63 {
64 GstOsxAudioDevice *d = data;
65 g_free (d->unique_id);
66 g_free (d);
67 }
68
69 static inline GstOsxAudioDevice *
70 _audio_system_get_default_device (gboolean output, const char *audio_type)
71 {
72 GstOsxAudioDevice *ret = NULL;
73 AudioDeviceID device_id = kAudioDeviceUnknown;
74
75 device_id = gst_core_audio_device_get_prop_uint32 (kAudioObjectSystemObject,
76 output ? kAudioHardwarePropertyDefaultOutputDevice
77 : kAudioHardwarePropertyDefaultInputDevice);
78 if (device_id == kAudioDeviceUnknown) {
79 GST_ERROR ("Failed to get default %s device", audio_type);
80 return NULL;
81 }
82
83 ret = g_new0 (GstOsxAudioDevice, 1);
84 ret->id = device_id;
85 ret->unique_id = gst_core_audio_device_get_prop_str (device_id,
86 kAudioDevicePropertyDeviceUID);
87
88 GST_DEBUG ("Default audio %s device: %d: %s", audio_type, ret->id,
89 ret->unique_id);
90
91 return ret;
92 }
93
94 static inline GPtrArray *
95 _audio_system_get_devices (gint * ndevices)
96 {
97 OSStatus status = noErr;
98 UInt32 propertySize = 0;
99 AudioDeviceID *devices = NULL;
100 GPtrArray *ret = g_ptr_array_new_with_free_func (_free_gst_osx_audio_device);
101
102 AudioObjectPropertyAddress audioDevicesAddress = {
103 kAudioHardwarePropertyDevices,
104 kAudioObjectPropertyScopeGlobal,
105 kAudioObjectPropertyElementMain
106 };
107
108 status = AudioObjectGetPropertyDataSize (kAudioObjectSystemObject,
109 &audioDevicesAddress, 0, NULL, &propertySize);
110 if (status != noErr) {
111 GST_WARNING ("failed getting number of devices: %d", (int) status);
112 return NULL;
113 }
114
115 *ndevices = propertySize / sizeof (AudioDeviceID);
116
117 devices = (AudioDeviceID *) g_malloc (propertySize);
118
119 status = AudioObjectGetPropertyData (kAudioObjectSystemObject,
120 &audioDevicesAddress, 0, NULL, &propertySize, devices);
121 if (status != noErr) {
122 GST_WARNING ("failed getting the list of devices: %d", (int) status);
123 g_free (devices);
124 *ndevices = 0;
125 return ret;
126 }
127
128 for (int i = 0; i < *ndevices; i++) {
129 GstOsxAudioDevice *d = g_new0 (GstOsxAudioDevice, 1);
130 d->id = devices[i];
131 d->unique_id = gst_core_audio_device_get_prop_str (d->id,
132 kAudioDevicePropertyDeviceUID);
133 g_ptr_array_add (ret, d);
134 GST_DEBUG ("Found device '%s' id %i", d->unique_id, d->id);
135 }
136
137 g_free (devices);
138
139 return ret;
140 }
141
142 static inline gboolean
143 _audio_device_is_alive (AudioDeviceID device_id, gboolean output)
144 {
145 OSStatus status = noErr;
146 int alive = FALSE;
147 UInt32 propertySize = sizeof (alive);
148 AudioObjectPropertyScope prop_scope;
149
150 prop_scope = output ? kAudioDevicePropertyScopeOutput :
151 kAudioDevicePropertyScopeInput;
152
153 AudioObjectPropertyAddress audioDeviceAliveAddress = {
154 kAudioDevicePropertyDeviceIsAlive,
155 prop_scope,
156 kAudioObjectPropertyElementMain
157 };
158
159 status = AudioObjectGetPropertyData (device_id,
160 &audioDeviceAliveAddress, 0, NULL, &propertySize, &alive);
161 if (status != noErr) {
162 alive = FALSE;
163 }
164
165 return alive;
166 }
167
168 static inline gboolean
169 _audio_device_is_hidden (AudioDeviceID device_id)
170 {
171 OSStatus status = noErr;
172 UInt32 hidden = FALSE;
173 UInt32 property_size = sizeof (hidden);
174 AudioObjectPropertyAddress property_address;
175
176 property_address.mSelector = kAudioDevicePropertyIsHidden;
177
178 status = AudioObjectGetPropertyData (device_id,
179 &property_address, 0, NULL, &property_size, &hidden);
180 if (status != noErr) {
181 return FALSE;
182 }
183
184 return hidden;
185 }
186
187 static inline guint
188 _audio_device_get_latency (AudioDeviceID device_id)
189 {
190 OSStatus status = noErr;
191 UInt32 latency = 0;
192 UInt32 propertySize = sizeof (latency);
193
194 AudioObjectPropertyAddress audioDeviceLatencyAddress = {
195 kAudioDevicePropertyLatency,
196 kAudioDevicePropertyScopeOutput,
197 kAudioObjectPropertyElementMain
198 };
199
200 status = AudioObjectGetPropertyData (device_id,
201 &audioDeviceLatencyAddress, 0, NULL, &propertySize, &latency);
202 if (status != noErr) {
203 GST_ERROR ("failed to get latency: %d", (int) status);
204 latency = -1;
205 }
206
207 return latency;
208 }
209
210 static inline pid_t
211 _audio_device_get_hog (AudioDeviceID device_id)
212 {
213 OSStatus status = noErr;
214 pid_t hog_pid;
215 UInt32 propertySize = sizeof (hog_pid);
216
217 AudioObjectPropertyAddress audioDeviceHogModeAddress = {
218 kAudioDevicePropertyHogMode,
219 kAudioDevicePropertyScopeOutput,
220 kAudioObjectPropertyElementMain
221 };
222
223 status = AudioObjectGetPropertyData (device_id,
224 &audioDeviceHogModeAddress, 0, NULL, &propertySize, &hog_pid);
225 if (status != noErr) {
226 GST_ERROR ("failed to get hog: %d", (int) status);
227 hog_pid = -1;
228 }
229
230 return hog_pid;
231 }
232
233 static inline gboolean
234 _audio_device_set_hog (AudioDeviceID device_id, pid_t hog_pid)
235 {
236 OSStatus status = noErr;
237 UInt32 propertySize = sizeof (hog_pid);
238 gboolean res = FALSE;
239
240 AudioObjectPropertyAddress audioDeviceHogModeAddress = {
241 kAudioDevicePropertyHogMode,
242 kAudioDevicePropertyScopeOutput,
243 kAudioObjectPropertyElementMain
244 };
245
246 status = AudioObjectSetPropertyData (device_id,
247 &audioDeviceHogModeAddress, 0, NULL, propertySize, &hog_pid);
248
249 if (status == noErr) {
250 res = TRUE;
251 } else {
252 GST_ERROR ("failed to set hog: %d", (int) status);
253 }
254
255 return res;
256 }
257
258 static inline gboolean
259 _audio_device_set_mixing (AudioDeviceID device_id, gboolean enable_mix)
260 {
261 OSStatus status = noErr;
262 UInt32 propertySize = 0, can_mix = enable_mix;
263 Boolean writable = FALSE;
264 gboolean res = FALSE;
265
266 AudioObjectPropertyAddress audioDeviceSupportsMixingAddress = {
267 kAudioDevicePropertySupportsMixing,
268 kAudioObjectPropertyScopeGlobal,
269 kAudioObjectPropertyElementMain
270 };
271
272 if (AudioObjectHasProperty (device_id, &audioDeviceSupportsMixingAddress)) {
273 /* Set mixable to false if we are allowed to */
274 status = AudioObjectIsPropertySettable (device_id,
275 &audioDeviceSupportsMixingAddress, &writable);
276 if (status) {
277 GST_DEBUG ("AudioObjectIsPropertySettable: %d", (int) status);
278 }
279 status = AudioObjectGetPropertyDataSize (device_id,
280 &audioDeviceSupportsMixingAddress, 0, NULL, &propertySize);
281 if (status) {
282 GST_DEBUG ("AudioObjectGetPropertyDataSize: %d", (int) status);
283 }
284 status = AudioObjectGetPropertyData (device_id,
285 &audioDeviceSupportsMixingAddress, 0, NULL, &propertySize, &can_mix);
286 if (status) {
287 GST_DEBUG ("AudioObjectGetPropertyData: %d", (int) status);
288 }
289
290 if (status == noErr && writable) {
291 can_mix = enable_mix;
292 status = AudioObjectSetPropertyData (device_id,
293 &audioDeviceSupportsMixingAddress, 0, NULL, propertySize, &can_mix);
294 res = TRUE;
295 }
296
297 if (status != noErr) {
298 GST_ERROR ("failed to set mixmode: %d", (int) status);
299 }
300 } else {
301 GST_DEBUG ("property not found, mixing couldn't be changed");
302 }
303
304 return res;
305 }
306
307 static inline gboolean
308 _audio_device_has_output (AudioDeviceID device_id)
309 {
310 OSStatus status = noErr;
311 UInt32 propertySize;
312
313 AudioObjectPropertyAddress streamsAddress = {
314 kAudioDevicePropertyStreams,
315 kAudioDevicePropertyScopeOutput,
316 kAudioObjectPropertyElementMain
317 };
318
319 status = AudioObjectGetPropertyDataSize (device_id,
320 &streamsAddress, 0, NULL, &propertySize);
321 if (status != noErr) {
322 return FALSE;
323 }
324 if (propertySize == 0) {
325 return FALSE;
326 }
327
328 return TRUE;
329 }
330
331 static inline AudioStreamID *
332 _audio_device_get_streams (AudioDeviceID device_id, gint * nstreams)
333 {
334 OSStatus status = noErr;
335 UInt32 propertySize = 0;
336 AudioStreamID *streams = NULL;
337
338 AudioObjectPropertyAddress streamsAddress = {
339 kAudioDevicePropertyStreams,
340 kAudioDevicePropertyScopeOutput,
341 kAudioObjectPropertyElementMain
342 };
343
344 status = AudioObjectGetPropertyDataSize (device_id,
345 &streamsAddress, 0, NULL, &propertySize);
346 if (status != noErr) {
347 GST_WARNING ("failed getting number of streams: %d", (int) status);
348 return NULL;
349 }
350
351 *nstreams = propertySize / sizeof (AudioStreamID);
352 streams = (AudioStreamID *) g_malloc (propertySize);
353
354 if (streams) {
355 status = AudioObjectGetPropertyData (device_id,
356 &streamsAddress, 0, NULL, &propertySize, streams);
357 if (status != noErr) {
358 GST_WARNING ("failed getting the list of streams: %d", (int) status);
359 g_free (streams);
360 *nstreams = 0;
361 return NULL;
362 }
363 }
364
365 return streams;
366 }
367
368 static inline guint
369 _audio_stream_get_latency (AudioStreamID stream_id)
370 {
371 OSStatus status = noErr;
372 UInt32 latency;
373 UInt32 propertySize = sizeof (latency);
374
375 AudioObjectPropertyAddress latencyAddress = {
376 kAudioStreamPropertyLatency,
377 kAudioObjectPropertyScopeGlobal,
378 kAudioObjectPropertyElementMain
379 };
380
381 status = AudioObjectGetPropertyData (stream_id,
382 &latencyAddress, 0, NULL, &propertySize, &latency);
383 if (status != noErr) {
384 GST_ERROR ("failed to get latency: %d", (int) status);
385 latency = -1;
386 }
387
388 return latency;
389 }
390
391 static inline gboolean
392 _audio_stream_get_current_format (AudioStreamID stream_id,
393 AudioStreamBasicDescription * format)
394 {
395 OSStatus status = noErr;
396 UInt32 propertySize = sizeof (AudioStreamBasicDescription);
397
398 AudioObjectPropertyAddress formatAddress = {
399 kAudioStreamPropertyPhysicalFormat,
400 kAudioObjectPropertyScopeGlobal,
401 kAudioObjectPropertyElementMain
402 };
403
404 status = AudioObjectGetPropertyData (stream_id,
405 &formatAddress, 0, NULL, &propertySize, format);
406 if (status != noErr) {
407 GST_ERROR ("failed to get current format: %d", (int) status);
408 return FALSE;
409 }
410
411 return TRUE;
412 }
413
414 static inline gboolean
415 _audio_stream_set_current_format (AudioStreamID stream_id,
416 AudioStreamBasicDescription format)
417 {
418 OSStatus status = noErr;
419 UInt32 propertySize = sizeof (AudioStreamBasicDescription);
420
421 AudioObjectPropertyAddress formatAddress = {
422 kAudioStreamPropertyPhysicalFormat,
423 kAudioObjectPropertyScopeGlobal,
424 kAudioObjectPropertyElementMain
425 };
426
427 status = AudioObjectSetPropertyData (stream_id,
428 &formatAddress, 0, NULL, propertySize, &format);
429 if (status != noErr) {
430 GST_ERROR ("failed to set current format: %d", (int) status);
431 return FALSE;
432 }
433
434 return TRUE;
435 }
436
437 static inline AudioStreamRangedDescription *
438 _audio_stream_get_formats (AudioStreamID stream_id, gint * nformats)
439 {
440 OSStatus status = noErr;
441 UInt32 propertySize = 0;
442 AudioStreamRangedDescription *formats = NULL;
443
444 AudioObjectPropertyAddress formatsAddress = {
445 kAudioStreamPropertyAvailablePhysicalFormats,
446 kAudioObjectPropertyScopeGlobal,
447 kAudioObjectPropertyElementMain
448 };
449
450 status = AudioObjectGetPropertyDataSize (stream_id,
451 &formatsAddress, 0, NULL, &propertySize);
452 if (status != noErr) {
453 GST_WARNING ("failed getting number of stream formats: %d", (int) status);
454 return NULL;
455 }
456
457 *nformats = propertySize / sizeof (AudioStreamRangedDescription);
458
459 formats = (AudioStreamRangedDescription *) g_malloc (propertySize);
460 if (formats) {
461 status = AudioObjectGetPropertyData (stream_id,
462 &formatsAddress, 0, NULL, &propertySize, formats);
463 if (status != noErr) {
464 GST_WARNING ("failed getting the list of stream formats: %d",
465 (int) status);
466 g_free (formats);
467 *nformats = 0;
468 return NULL;
469 }
470 }
471 return formats;
472 }
473
474 static inline gboolean
475 _audio_stream_is_spdif_avail (AudioStreamID stream_id)
476 {
477 AudioStreamRangedDescription *formats;
478 gint i, nformats = 0;
479 gboolean res = FALSE;
480
481 formats = _audio_stream_get_formats (stream_id, &nformats);
482 GST_DEBUG ("found %d stream formats", nformats);
483
484 if (formats) {
485 GST_DEBUG ("formats supported on stream ID: %u", (unsigned) stream_id);
486
487 for (i = 0; i < nformats; i++) {
488 GST_DEBUG (" " CORE_AUDIO_FORMAT,
489 CORE_AUDIO_FORMAT_ARGS (formats[i].mFormat));
490
491 if (CORE_AUDIO_FORMAT_IS_SPDIF (formats[i])) {
492 res = TRUE;
493 }
494 }
495 g_free (formats);
496 }
497
498 return res;
499 }
500
501 static OSStatus
502 _audio_stream_format_listener (AudioObjectID inObjectID,
503 UInt32 inNumberAddresses,
504 const AudioObjectPropertyAddress inAddresses[], void *inClientData)
505 {
506 OSStatus status = noErr;
507 guint i;
508 PropertyMutex *prop_mutex = inClientData;
509
510 for (i = 0; i < inNumberAddresses; i++) {
511 if (inAddresses[i].mSelector == kAudioStreamPropertyPhysicalFormat) {
512 g_mutex_lock (&prop_mutex->lock);
513 g_cond_signal (&prop_mutex->cond);
514 g_mutex_unlock (&prop_mutex->lock);
515 break;
516 }
517 }
518 return (status);
519 }
520
521 static gboolean
522 _audio_stream_change_format (AudioStreamID stream_id,
523 AudioStreamBasicDescription format)
524 {
525 OSStatus status = noErr;
526 gint i;
527 gboolean ret = FALSE;
528 AudioStreamBasicDescription cformat;
529 PropertyMutex prop_mutex;
530
531 AudioObjectPropertyAddress formatAddress = {
532 kAudioStreamPropertyPhysicalFormat,
533 kAudioObjectPropertyScopeGlobal,
534 kAudioObjectPropertyElementMain
535 };
536
537 GST_DEBUG ("setting stream format: " CORE_AUDIO_FORMAT,
538 CORE_AUDIO_FORMAT_ARGS (format));
539
540 /* Condition because SetProperty is asynchronous */
541 g_mutex_init (&prop_mutex.lock);
542 g_cond_init (&prop_mutex.cond);
543
544 g_mutex_lock (&prop_mutex.lock);
545
546 /* Install the property listener to serialize the operations */
547 status = AudioObjectAddPropertyListener (stream_id, &formatAddress,
548 _audio_stream_format_listener, (void *) &prop_mutex);
549 if (status != noErr) {
550 GST_ERROR ("AudioObjectAddPropertyListener failed: %d", (int) status);
551 goto done;
552 }
553
554 /* Change the format */
555 if (!_audio_stream_set_current_format (stream_id, format)) {
556 goto done;
557 }
558
559 /* The AudioObjectSetProperty is not only asynchronous
560 * it is also not atomic in its behaviour.
561 * Therefore we check 4 times before we really give up. */
562 for (i = 0; i < 4; i++) {
563 gint64 timeout;
564
565 timeout = g_get_monotonic_time () + 250000;
566
567 if (!g_cond_wait_until (&prop_mutex.cond, &prop_mutex.lock, timeout)) {
568 GST_LOG ("timeout...");
569 }
570
571 if (_audio_stream_get_current_format (stream_id, &cformat)) {
572 GST_DEBUG ("current stream format: " CORE_AUDIO_FORMAT,
573 CORE_AUDIO_FORMAT_ARGS (cformat));
574
575 if (cformat.mSampleRate == format.mSampleRate &&
576 cformat.mFormatID == format.mFormatID &&
577 cformat.mFramesPerPacket == format.mFramesPerPacket) {
578 /* The right format is now active */
579 break;
580 }
581 }
582 }
583
584 if (cformat.mSampleRate != format.mSampleRate ||
585 cformat.mFormatID != format.mFormatID ||
586 cformat.mFramesPerPacket != format.mFramesPerPacket) {
587 goto done;
588 }
589
590 ret = TRUE;
591
592 done:
593 /* Removing the property listener */
594 status = AudioObjectRemovePropertyListener (stream_id,
595 &formatAddress, _audio_stream_format_listener, (void *) &prop_mutex);
596 if (status != noErr) {
597 GST_ERROR ("AudioObjectRemovePropertyListener failed: %d", (int) status);
598 }
599 /* Destroy the lock and condition */
600 g_mutex_unlock (&prop_mutex.lock);
601 g_mutex_clear (&prop_mutex.lock);
602 g_cond_clear (&prop_mutex.cond);
603
604 return ret;
605 }
606
607 static OSStatus
608 _audio_stream_hardware_changed_listener (AudioObjectID inObjectID,
609 UInt32 inNumberAddresses,
610 const AudioObjectPropertyAddress inAddresses[], void *inClientData)
611 {
612 OSStatus status = noErr;
613 guint i;
614 GstCoreAudio *core_audio = inClientData;
615
616 for (i = 0; i < inNumberAddresses; i++) {
617 if (inAddresses[i].mSelector == kAudioDevicePropertyDeviceHasChanged) {
618 if (!gst_core_audio_audio_device_is_spdif_avail (core_audio->device_id)) {
619 GstOsxAudioSink *sink =
620 GST_OSX_AUDIO_SINK (GST_OBJECT_PARENT (core_audio->osxbuf));
621 GST_ELEMENT_ERROR (sink, RESOURCE, FAILED,
622 ("SPDIF output no longer available"),
623 ("Audio device is reporting that SPDIF output isn't available"));
624 }
625 break;
626 }
627 }
628 return (status);
629 }
630
631 static inline gboolean
632 _monitorize_spdif (GstCoreAudio * core_audio)
633 {
634 OSStatus status = noErr;
635 gboolean ret = TRUE;
636
637 AudioObjectPropertyAddress propAddress = {
638 kAudioDevicePropertyDeviceHasChanged,
639 kAudioObjectPropertyScopeGlobal,
640 kAudioObjectPropertyElementMain
641 };
642
643 /* Install the property listener */
644 status = AudioObjectAddPropertyListener (core_audio->device_id,
645 &propAddress, _audio_stream_hardware_changed_listener,
646 (void *) core_audio);
647 if (status != noErr) {
648 GST_ERROR_OBJECT (core_audio->osxbuf,
649 "AudioObjectAddPropertyListener failed: %d", (int) status);
650 ret = FALSE;
651 }
652
653 return ret;
654 }
655
656 static inline gboolean
657 _unmonitorize_spdif (GstCoreAudio * core_audio)
658 {
659 OSStatus status = noErr;
660 gboolean ret = TRUE;
661
662 AudioObjectPropertyAddress propAddress = {
663 kAudioDevicePropertyDeviceHasChanged,
664 kAudioObjectPropertyScopeGlobal,
665 kAudioObjectPropertyElementMain
666 };
667
668 /* Remove the property listener */
669 status = AudioObjectRemovePropertyListener (core_audio->device_id,
670 &propAddress, _audio_stream_hardware_changed_listener,
671 (void *) core_audio);
672 if (status != noErr) {
673 GST_ERROR_OBJECT (core_audio->osxbuf,
674 "AudioObjectRemovePropertyListener failed: %d", (int) status);
675 ret = FALSE;
676 }
677
678 return ret;
679 }
680
681 static inline gboolean
682 _open_spdif (GstCoreAudio * core_audio)
683 {
684 gboolean res = FALSE;
685 pid_t hog_pid, own_pid = getpid ();
686
687 /* We need the device in exclusive and disable the mixing */
688 hog_pid = _audio_device_get_hog (core_audio->device_id);
689
690 if (hog_pid != -1 && hog_pid != own_pid) {
691 GST_DEBUG_OBJECT (core_audio,
692 "device is currently in use by another application");
693 goto done;
694 }
695
696 if (_audio_device_set_hog (core_audio->device_id, own_pid)) {
697 core_audio->hog_pid = own_pid;
698 }
699
700 if (_audio_device_set_mixing (core_audio->device_id, FALSE)) {
701 GST_DEBUG_OBJECT (core_audio, "disabled mixing on the device");
702 core_audio->disabled_mixing = TRUE;
703 }
704
705 res = TRUE;
706 done:
707 return res;
708 }
709
710 static inline gboolean
711 _close_spdif (GstCoreAudio * core_audio)
712 {
713 pid_t hog_pid;
714
715 _unmonitorize_spdif (core_audio);
716
717 if (core_audio->revert_format) {
718 if (!_audio_stream_change_format (core_audio->stream_id,
719 core_audio->original_format)) {
720 GST_WARNING_OBJECT (core_audio->osxbuf, "Format revert failed");
721 }
722 core_audio->revert_format = FALSE;
723 }
724
725 if (core_audio->disabled_mixing) {
726 _audio_device_set_mixing (core_audio->device_id, TRUE);
727 core_audio->disabled_mixing = FALSE;
728 }
729
730 if (core_audio->hog_pid != -1) {
731 hog_pid = _audio_device_get_hog (core_audio->device_id);
732 if (hog_pid == getpid ()) {
733 if (_audio_device_set_hog (core_audio->device_id, -1)) {
734 core_audio->hog_pid = -1;
735 }
736 }
737 }
738
739 return TRUE;
740 }
741
742 static OSStatus
743 _io_proc_spdif (AudioDeviceID inDevice,
744 const AudioTimeStamp * inNow,
745 const void *inInputData,
746 const AudioTimeStamp * inTimestamp,
747 AudioBufferList * bufferList,
748 const AudioTimeStamp * inOutputTime, GstCoreAudio * core_audio)
749 {
750 OSStatus status;
751
752 status = core_audio->element->io_proc (core_audio->osxbuf, NULL, inTimestamp,
753 0, 0, bufferList);
754
755 return status;
756 }
757
758 static inline gboolean
759 _acquire_spdif (GstCoreAudio * core_audio, AudioStreamBasicDescription format)
760 {
761 AudioStreamID *streams = NULL;
762 gint i, j, nstreams = 0;
763 gboolean ret = FALSE;
764
765 if (!_open_spdif (core_audio))
766 goto done;
767
768 streams = _audio_device_get_streams (core_audio->device_id, &nstreams);
769
770 for (i = 0; i < nstreams; i++) {
771 AudioStreamRangedDescription *formats = NULL;
772 gint nformats = 0;
773
774 formats = _audio_stream_get_formats (streams[i], &nformats);
775
776 if (formats) {
777 gboolean is_spdif = FALSE;
778
779 /* Check if one of the supported formats is a digital format */
780 for (j = 0; j < nformats; j++) {
781 if (CORE_AUDIO_FORMAT_IS_SPDIF (formats[j])) {
782 is_spdif = TRUE;
783 break;
784 }
785 }
786
787 if (is_spdif) {
788 /* if this stream supports a digital (cac3) format,
789 * then go set it. */
790 gint requested_rate_format = -1;
791 gint current_rate_format = -1;
792 gint backup_rate_format = -1;
793
794 core_audio->stream_id = streams[i];
795 core_audio->stream_idx = i;
796
797 if (!core_audio->revert_format) {
798 if (!_audio_stream_get_current_format (core_audio->stream_id,
799 &core_audio->original_format)) {
800 GST_WARNING_OBJECT (core_audio->osxbuf,
801 "format could not be saved");
802 g_free (formats);
803 continue;
804 }
805 core_audio->revert_format = TRUE;
806 }
807
808 for (j = 0; j < nformats; j++) {
809 if (CORE_AUDIO_FORMAT_IS_SPDIF (formats[j])) {
810 GST_LOG_OBJECT (core_audio->osxbuf,
811 "found stream format: " CORE_AUDIO_FORMAT,
812 CORE_AUDIO_FORMAT_ARGS (formats[j].mFormat));
813
814 if (formats[j].mFormat.mSampleRate == format.mSampleRate) {
815 requested_rate_format = j;
816 break;
817 } else if (formats[j].mFormat.mSampleRate ==
818 core_audio->original_format.mSampleRate) {
819 current_rate_format = j;
820 } else {
821 if (backup_rate_format < 0 ||
822 formats[j].mFormat.mSampleRate >
823 formats[backup_rate_format].mFormat.mSampleRate) {
824 backup_rate_format = j;
825 }
826 }
827 }
828 }
829
830 if (requested_rate_format >= 0) {
831 /* We prefer to output at the rate of the original audio */
832 core_audio->stream_format = formats[requested_rate_format].mFormat;
833 } else if (current_rate_format >= 0) {
834 /* If not possible, we will try to use the current rate */
835 core_audio->stream_format = formats[current_rate_format].mFormat;
836 } else {
837 /* And if we have to, any digital format will be just
838 * fine (highest rate possible) */
839 core_audio->stream_format = formats[backup_rate_format].mFormat;
840 }
841 }
842 g_free (formats);
843 }
844 }
845 g_free (streams);
846
847 GST_DEBUG_OBJECT (core_audio,
848 "original stream format: " CORE_AUDIO_FORMAT,
849 CORE_AUDIO_FORMAT_ARGS (core_audio->original_format));
850
851 if (!_audio_stream_change_format (core_audio->stream_id,
852 core_audio->stream_format))
853 goto done;
854
855 ret = TRUE;
856
857 done:
858 return ret;
859 }
860
861 static inline void
862 _remove_render_spdif_callback (GstCoreAudio * core_audio)
863 {
864 OSStatus status;
865
866 /* Deactivate the render callback by calling
867 * AudioDeviceDestroyIOProcID */
868 status =
869 AudioDeviceDestroyIOProcID (core_audio->device_id, core_audio->procID);
870 if (status != noErr) {
871 GST_ERROR_OBJECT (core_audio->osxbuf,
872 "AudioDeviceDestroyIOProcID failed: %d", (int) status);
873 }
874
875 GST_DEBUG_OBJECT (core_audio,
876 "osx ring buffer removed ioproc ID: %p device_id %lu",
877 core_audio->procID, (gulong) core_audio->device_id);
878
879 /* We're deactivated.. */
880 core_audio->procID = 0;
881 g_atomic_int_set (&core_audio->io_proc_dropping, FALSE);
882 core_audio->io_proc_active = FALSE;
883 }
884
885 static inline gboolean
886 _io_proc_spdif_start (GstCoreAudio * core_audio)
887 {
888 OSErr status;
889
890 GST_DEBUG_OBJECT (core_audio,
891 "osx ring buffer start ioproc ID: %p device_id %lu",
892 core_audio->procID, (gulong) core_audio->device_id);
893
894 if (!core_audio->io_proc_active) {
895 /* Add IOProc callback */
896 status = AudioDeviceCreateIOProcID (core_audio->device_id,
897 (AudioDeviceIOProc) _io_proc_spdif,
898 (void *) core_audio, &core_audio->procID);
899 if (status != noErr) {
900 GST_ERROR_OBJECT (core_audio->osxbuf,
901 ":AudioDeviceCreateIOProcID failed: %d", (int) status);
902 return FALSE;
903 }
904 core_audio->io_proc_active = TRUE;
905 }
906
907 g_atomic_int_set (&core_audio->io_proc_dropping, FALSE);
908
909 /* Start device */
910 status = AudioDeviceStart (core_audio->device_id, core_audio->procID);
911 if (status != noErr) {
912 GST_ERROR_OBJECT (core_audio->osxbuf,
913 "AudioDeviceStart failed: %d", (int) status);
914 return FALSE;
915 }
916 return TRUE;
917 }
918
919 static inline gboolean
920 _io_proc_spdif_stop (GstCoreAudio * core_audio)
921 {
922 OSErr status;
923
924 /* Stop device */
925 status = AudioDeviceStop (core_audio->device_id, core_audio->procID);
926 if (status != noErr) {
927 GST_ERROR_OBJECT (core_audio->osxbuf,
928 "AudioDeviceStop failed: %d", (int) status);
929 }
930
931 GST_DEBUG_OBJECT (core_audio,
932 "osx ring buffer stop ioproc ID: %p device_id %lu",
933 core_audio->procID, (gulong) core_audio->device_id);
934
935 if (core_audio->io_proc_active) {
936 _remove_render_spdif_callback (core_audio);
937 }
938
939 _close_spdif (core_audio);
940
941 return TRUE;
942 }
943
944
945 /***********************
946 * Implementation *
947 **********************/
948
949 static gboolean
950 gst_core_audio_open_impl (GstCoreAudio * core_audio)
951 {
952 gboolean ret;
953
954 /* The following is needed to instruct HAL to create their own
955 * thread to handle the notifications. */
956 _audio_system_set_runloop (NULL);
957
958 /* Create a HALOutput AudioUnit.
959 * This is the lowest-level output API that is actually sensibly
960 * usable (the lower level ones require that you do
961 * channel-remapping yourself, and the CoreAudio channel mapping
962 * is sufficiently complex that doing so would be very difficult)
963 *
964 * Note that for input we request an output unit even though
965 * we will do input with it.
966 * http://developer.apple.com/technotes/tn2002/tn2091.html
967 */
968 ret = gst_core_audio_open_device (core_audio, kAudioUnitSubType_HALOutput,
969 "HALOutput");
970 if (!ret) {
971 GST_DEBUG ("Could not open device");
972 goto done;
973 }
974
975 ret = gst_core_audio_bind_device (core_audio);
976 if (!ret) {
977 GST_DEBUG ("Could not bind device");
978 goto done;
979 }
980
981 done:
982 return ret;
983 }
984
985 static gboolean
986 gst_core_audio_start_processing_impl (GstCoreAudio * core_audio)
987 {
988 if (core_audio->is_passthrough) {
989 return _io_proc_spdif_start (core_audio);
990 } else {
991 return gst_core_audio_io_proc_start (core_audio);
992 }
993 }
994
995 static gboolean
996 gst_core_audio_pause_processing_impl (GstCoreAudio * core_audio)
997 {
998 if (core_audio->is_passthrough) {
999 GST_DEBUG_OBJECT (core_audio,
1000 "osx ring buffer pause ioproc ID: %p device_id %lu",
1001 core_audio->procID, (gulong) core_audio->device_id);
1002
1003 if (core_audio->io_proc_active) {
1004 _remove_render_spdif_callback (core_audio);
1005 }
1006 } else {
1007 GST_DEBUG_OBJECT (core_audio,
1008 "osx ring buffer pause ioproc: %p device_id %lu",
1009 core_audio->element->io_proc, (gulong) core_audio->device_id);
1010 if (core_audio->io_proc_active) {
1011 /* CoreAudio isn't threadsafe enough to remove the render callback in the
1012 * render notify callback, so we pause by having the callback drop
1013 * samples or feed silence. See:
1014 * http://lists.apple.com/archives/Coreaudio-api/2006/Mar/msg00010.html
1015 * https://gitlab.freedesktop.org/gstreamer/gstreamer/-/issues/4155
1016 */
1017 g_atomic_int_set (&core_audio->io_proc_dropping, TRUE);
1018 }
1019 }
1020 return TRUE;
1021 }
1022
1023 static gboolean
1024 gst_core_audio_stop_processing_impl (GstCoreAudio * core_audio)
1025 {
1026 if (core_audio->is_passthrough) {
1027 _io_proc_spdif_stop (core_audio);
1028 } else {
1029 gst_core_audio_io_proc_stop (core_audio);
1030 }
1031
1032 return TRUE;
1033 }
1034
1035 static gboolean
1036 gst_core_audio_get_samples_and_latency_impl (GstCoreAudio * core_audio,
1037 gdouble rate, guint * samples, gdouble * latency)
1038 {
1039 OSStatus status;
1040 UInt32 size = sizeof (double);
1041
1042 if (core_audio->is_passthrough) {
1043 *samples = _audio_device_get_latency (core_audio->device_id);
1044 *samples += _audio_stream_get_latency (core_audio->stream_id);
1045 *latency = (double) *samples / rate;
1046 } else {
1047 status = AudioUnitGetProperty (core_audio->audiounit, kAudioUnitProperty_Latency, kAudioUnitScope_Global, 0, /* N/A for global */
1048 latency, &size);
1049
1050 if (status) {
1051 GST_WARNING_OBJECT (core_audio->osxbuf, "Failed to get latency: %d",
1052 (int) status);
1053 *samples = 0;
1054 return FALSE;
1055 }
1056
1057 *samples = *latency * rate;
1058 }
1059 return TRUE;
1060 }
1061
1062 static gboolean
1063 gst_core_audio_initialize_impl (GstCoreAudio * core_audio,
1064 AudioStreamBasicDescription format, GstCaps * caps,
1065 gboolean is_passthrough, guint32 * frame_size)
1066 {
1067 gboolean ret = FALSE;
1068 OSStatus status;
1069
1070 /* Uninitialize the AudioUnit before changing formats */
1071 status = AudioUnitUninitialize (core_audio->audiounit);
1072 if (status) {
1073 GST_ERROR_OBJECT (core_audio, "Failed to uninitialize AudioUnit: %d",
1074 (int) status);
1075 return FALSE;
1076 }
1077
1078 core_audio->is_passthrough = is_passthrough;
1079 if (is_passthrough) {
1080 if (!_acquire_spdif (core_audio, format))
1081 goto done;
1082 _monitorize_spdif (core_audio);
1083 } else {
1084 OSStatus status;
1085 UInt32 propertySize = sizeof (*frame_size);
1086
1087 core_audio->stream_idx = 0;
1088 if (!gst_core_audio_set_format (core_audio, format))
1089 goto done;
1090
1091 if (!gst_core_audio_set_channel_layout (core_audio,
1092 format.mChannelsPerFrame, caps))
1093 goto done;
1094
1095 // Attempt to configure the requested frame size if smaller than the device's
1096 // This will apparently modify the size for all audio devices in the current process so should
1097 // be done conservatively
1098 if (frame_size != 0) {
1099 guint32 cur_frame_size;
1100 status = AudioUnitGetProperty (core_audio->audiounit, kAudioDevicePropertyBufferFrameSize, kAudioUnitScope_Global, 0, /* N/A for global */
1101 &cur_frame_size, &propertySize);
1102 if (!status && *frame_size < cur_frame_size) {
1103 status = AudioUnitSetProperty (core_audio->audiounit, kAudioDevicePropertyBufferFrameSize, kAudioUnitScope_Global, 0, /* N/A for global */
1104 frame_size, propertySize);
1105 if (status) {
1106 GST_WARNING_OBJECT (core_audio->osxbuf,
1107 "Failed to set desired frame size of %u: %d", *frame_size,
1108 (int) status);
1109 }
1110 }
1111 }
1112 status = AudioUnitGetProperty (core_audio->audiounit, kAudioDevicePropertyBufferFrameSize, kAudioUnitScope_Global, 0, /* N/A for global */
1113 frame_size, &propertySize);
1114
1115 if (status) {
1116 GST_WARNING_OBJECT (core_audio->osxbuf, "Failed to get frame size: %d",
1117 (int) status);
1118 goto done;
1119 }
1120 }
1121
1122 ret = TRUE;
1123
1124 done:
1125 /* Format changed, initialise the AudioUnit again */
1126 status = AudioUnitInitialize (core_audio->audiounit);
1127 if (status) {
1128 GST_ERROR_OBJECT (core_audio, "Failed to initialize AudioUnit: %d",
1129 (int) status);
1130 ret = FALSE;
1131 }
1132
1133 if (ret) {
1134 GST_DEBUG_OBJECT (core_audio, "osxbuf ring buffer acquired");
1135 }
1136
1137 return ret;
1138 }
1139
1140 static gboolean
1141 gst_core_audio_select_device_impl (GstCoreAudio * core_audio)
1142 {
1143 AudioDeviceID device_id = core_audio->device_id;
1144 char *unique_id = core_audio->unique_id;
1145 gboolean output = !core_audio->is_src;
1146 const char *audio_type = output ? "output" : "input";
1147 int i, ndevices = 0;
1148 GPtrArray *devices = _audio_system_get_devices (&ndevices);
1149 GstOsxAudioDevice *default_device =
1150 _audio_system_get_default_device (output, audio_type);
1151 gboolean res = FALSE;
1152
1153 if (ndevices < 1) {
1154 GST_ERROR ("No audio devices found");
1155 goto done;
1156 }
1157 GST_DEBUG ("Found %d audio device(s)", ndevices);
1158
1159 /* Prefer unique-id since that is more likely to be correct */
1160 if (unique_id) {
1161 device_id = kAudioDeviceUnknown;
1162 for (i = 0; i < ndevices; i++) {
1163 GstOsxAudioDevice *d = g_ptr_array_index (devices, i);
1164 if (g_strcmp0 (unique_id, d->unique_id) == 0) {
1165 device_id = d->id;
1166 res = TRUE;
1167 break;
1168 }
1169 }
1170 if (res == FALSE) {
1171 GST_ERROR ("No audio %s device with unique-id %s", audio_type, unique_id);
1172 }
1173 /* Skip the checks below for device_id being valid and just return */
1174 goto done;
1175 }
1176
1177 /* Here we decide if selected device_id is valid or autoselect
1178 * the default one when required */
1179 if (device_id == kAudioDeviceUnknown) {
1180 if (default_device == NULL) {
1181 GST_ERROR ("Cannot auto-select %s device, no default", audio_type);
1182 res = FALSE;
1183 } else if (default_device->id != kAudioDeviceUnknown) {
1184 device_id = default_device->id;
1185 unique_id = default_device->unique_id;
1186 default_device->unique_id = NULL;
1187 res = TRUE;
1188 } else {
1189 GST_ERROR ("No device of required type available");
1190 res = FALSE;
1191 }
1192 } else if (_audio_device_is_hidden (device_id)) {
1193 if (_audio_device_is_alive (device_id, output)) {
1194 for (i = 0; i < ndevices; i++) {
1195 GstOsxAudioDevice *d = g_ptr_array_index (devices, i);
1196 if (device_id == d->id) {
1197 unique_id = d->unique_id;
1198 d->unique_id = NULL;
1199 res = TRUE;
1200 break;
1201 }
1202 }
1203
1204 if (res == FALSE) {
1205 GST_ERROR ("Requested hidden device with id %u not found", device_id);
1206 }
1207 } else {
1208 GST_ERROR ("Requested hidden device not usable");
1209 res = FALSE;
1210 }
1211 } else {
1212 for (i = 0; i < ndevices; i++) {
1213 GstOsxAudioDevice *d = g_ptr_array_index (devices, i);
1214 if (device_id == d->id) {
1215 unique_id = d->unique_id;
1216 d->unique_id = NULL;
1217 res = TRUE;
1218 break;
1219 }
1220 }
1221
1222 if (res && !_audio_device_is_alive (device_id, output)) {
1223 GST_ERROR ("Requested device not usable");
1224 res = FALSE;
1225 }
1226 }
1227
1228 done:
1229 if (res) {
1230 g_assert_cmpint (device_id, !=, kAudioDeviceUnknown);
1231 g_assert (unique_id != NULL);
1232 core_audio->device_id = device_id;
1233 core_audio->is_default = (default_device && (device_id ==
1234 default_device->id));
1235 if (unique_id != core_audio->unique_id) {
1236 g_free (core_audio->unique_id);
1237 core_audio->unique_id = unique_id;
1238 }
1239 }
1240
1241 g_ptr_array_unref (devices);
1242 if (default_device)
1243 g_free (default_device->unique_id);
1244 g_free (default_device);
1245
1246 return res;
1247 }
1248
1249 static gboolean
1250 gst_core_audio_audio_device_is_spdif_avail_impl (AudioDeviceID device_id)
1251 {
1252 AudioStreamID *streams = NULL;
1253 gint i, nstreams = 0;
1254 gboolean res = FALSE;
1255
1256 streams = _audio_device_get_streams (device_id, &nstreams);
1257 GST_DEBUG ("found %d streams", nstreams);
1258 if (streams) {
1259 for (i = 0; i < nstreams; i++) {
1260 if (_audio_stream_is_spdif_avail (streams[i])) {
1261 res = TRUE;
1262 }
1263 }
1264
1265 g_free (streams);
1266 }
1267
1268 return res;
1269 }