Debian Codegraph @

sys/osxaudio/gstosxaudiodeviceprovider.c

raw
1 /* GStreamer
2 * Copyright (C) 2016 Hyunjun Ko <[email protected]>
3 *
4 * gstosxaudiodeviceprovider.c: OSX audio device probing and monitoring
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
15 *
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
20 */
21
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25
26 #include <gst/gst.h>
27 #include <gst/audio/audio.h>
28 #include "gstosxaudiosrc.h"
29 #include "gstosxaudiosink.h"
30 #include "gstosxaudiodeviceprovider.h"
31
32 GST_DEBUG_CATEGORY_STATIC (gst_osxaudio_device_debug);
33 #define GST_CAT_DEFAULT gst_osxaudio_device_debug
34
35 #if defined(MAC_OS_X_VERSION_MAX_ALLOWED) && MAC_OS_X_VERSION_MAX_ALLOWED < 120000
36 #define kAudioObjectPropertyElementMain kAudioObjectPropertyElementMaster
37 #endif
38
39 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
40 GST_PAD_SRC,
41 GST_PAD_ALWAYS,
42 GST_STATIC_CAPS (GST_OSX_AUDIO_SRC_CAPS)
43 );
44
45 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
46 GST_PAD_SINK,
47 GST_PAD_ALWAYS,
48 GST_STATIC_CAPS (GST_OSX_AUDIO_SINK_CAPS)
49 );
50
51 static GstOsxAudioDevice *gst_osx_audio_device_new (AudioDeviceID device_id,
52 const gchar * device_name, UInt32 transport_type,
53 GstOsxAudioDeviceType type, GstCoreAudio * core_audio);
54
55 G_DEFINE_TYPE (GstOsxAudioDeviceProvider, gst_osx_audio_device_provider,
56 GST_TYPE_DEVICE_PROVIDER);
57 GST_DEVICE_PROVIDER_REGISTER_DEFINE (osxaudiodeviceprovider,
58 "osxaudiodeviceprovider", GST_RANK_PRIMARY,
59 GST_TYPE_OSX_AUDIO_DEVICE_PROVIDER);
60
61 static gboolean gst_osx_audio_device_provider_start (GstDeviceProvider *
62 provider);
63 static void gst_osx_audio_device_provider_stop (GstDeviceProvider * provider);
64 static GList *gst_osx_audio_device_provider_probe (GstDeviceProvider *
65 provider);
66 static void
67 gst_osx_audio_device_provider_update_devices (GstOsxAudioDeviceProvider * self);
68
69 static void
70 gst_osx_audio_device_provider_class_init (GstOsxAudioDeviceProviderClass *
71 klass)
72 {
73 GstDeviceProviderClass *dm_class = GST_DEVICE_PROVIDER_CLASS (klass);
74
75 dm_class->start = gst_osx_audio_device_provider_start;
76 dm_class->stop = gst_osx_audio_device_provider_stop;
77 dm_class->probe = gst_osx_audio_device_provider_probe;
78
79 gst_device_provider_class_set_static_metadata (dm_class,
80 "OSX Audio Device Provider", "Source/Sink/Audio",
81 "List and monitor OSX audio source and sink devices",
82 "Hyunjun Ko <[email protected]>");
83
84 GST_DEBUG_CATEGORY_INIT (gst_osxaudio_device_debug, "osxaudio_device", 0,
85 "Audio device provider for macOS");
86 }
87
88 static void
89 gst_osx_audio_device_provider_init (GstOsxAudioDeviceProvider * provider)
90 {
91 }
92
93 static GstOsxAudioDevice *
94 gst_osx_audio_device_provider_probe_device (GstOsxAudioDeviceProvider *
95 provider, AudioDeviceID device_id, const gchar * device_name,
96 UInt32 transport_type, GstOsxAudioDeviceType type)
97 {
98 GstOsxAudioDevice *device = NULL;
99 GstCoreAudio *core_audio;
100 gboolean is_src = type == GST_OSX_AUDIO_DEVICE_TYPE_SOURCE ? TRUE : FALSE;
101
102 core_audio = g_object_new (GST_TYPE_CORE_AUDIO,
103 "is-src", is_src, "device", device_id, NULL);
104
105 if (!gst_core_audio_select_device (core_audio)) {
106 GST_ERROR ("CoreAudio device coult not be selected");
107 goto done;
108 }
109
110 if (!gst_core_audio_open (core_audio)) {
111 GST_ERROR ("CoreAudio device could not be opened");
112 goto done;
113 }
114
115 device =
116 gst_osx_audio_device_new (device_id, device_name, transport_type, type,
117 core_audio);
118
119 gst_core_audio_close (core_audio);
120
121 done:
122 g_object_unref (core_audio);
123
124 return device;
125 }
126
127 static inline gboolean
128 _audio_device_has_output (AudioDeviceID device_id)
129 {
130 OSStatus status = noErr;
131 UInt32 propertySize;
132
133 AudioObjectPropertyAddress streamsAddress = {
134 kAudioDevicePropertyStreams,
135 kAudioDevicePropertyScopeOutput,
136 kAudioObjectPropertyElementMain
137 };
138
139 status = AudioObjectGetPropertyDataSize (device_id,
140 &streamsAddress, 0, NULL, &propertySize);
141
142 if (status != noErr) {
143 GST_WARNING ("failed getting device property: %d", (int) status);
144 return FALSE;
145 }
146 if (propertySize == 0) {
147 GST_DEBUG ("property size was 0; device has no output channels");
148 return FALSE;
149 }
150
151 return TRUE;
152 }
153
154 static inline gboolean
155 _audio_device_has_input (AudioDeviceID device_id)
156 {
157 OSStatus status = noErr;
158 UInt32 propertySize;
159
160 AudioObjectPropertyAddress streamsAddress = {
161 kAudioDevicePropertyStreams,
162 kAudioDevicePropertyScopeInput,
163 kAudioObjectPropertyElementMain
164 };
165
166 status = AudioObjectGetPropertyDataSize (device_id,
167 &streamsAddress, 0, NULL, &propertySize);
168
169 if (status != noErr) {
170 GST_WARNING ("failed getting device property: %d", (int) status);
171 return FALSE;
172 }
173 if (propertySize == 0) {
174 GST_DEBUG ("property size was 0; device has no input channels");
175 return FALSE;
176 }
177
178 return TRUE;
179 }
180
181 static inline AudioDeviceID *
182 _audio_system_get_devices (gint * ndevices)
183 {
184 OSStatus status = noErr;
185 UInt32 propertySize = 0;
186 AudioDeviceID *devices = NULL;
187
188 AudioObjectPropertyAddress audioDevicesAddress = {
189 kAudioHardwarePropertyDevices,
190 kAudioObjectPropertyScopeGlobal,
191 kAudioObjectPropertyElementMain
192 };
193
194 status = AudioObjectGetPropertyDataSize (kAudioObjectSystemObject,
195 &audioDevicesAddress, 0, NULL, &propertySize);
196 if (status != noErr) {
197 GST_WARNING ("failed getting number of devices: %d", (int) status);
198 return NULL;
199 }
200
201 *ndevices = propertySize / sizeof (AudioDeviceID);
202
203 devices = (AudioDeviceID *) g_malloc (propertySize);
204 if (devices) {
205 status = AudioObjectGetPropertyData (kAudioObjectSystemObject,
206 &audioDevicesAddress, 0, NULL, &propertySize, devices);
207 if (status != noErr) {
208 GST_WARNING ("failed getting the list of devices: %d", (int) status);
209 g_free (devices);
210 *ndevices = 0;
211 return NULL;
212 }
213 }
214
215 return devices;
216 }
217
218 static OSStatus
219 _audio_devices_changed_cb (AudioObjectID inObjectID, UInt32 inNumberAddresses,
220 const AudioObjectPropertyAddress * inAddresses,
221 void *__nullable inClientData)
222 {
223 GstOsxAudioDeviceProvider *self =
224 GST_OSX_AUDIO_DEVICE_PROVIDER (inClientData);
225
226 GST_DEBUG ("Audio devices changed");
227 gst_osx_audio_device_provider_update_devices (self);
228
229 return noErr;
230 }
231
232 static AudioObjectPropertyAddress
233 _get_devices_list_address ()
234 {
235 AudioObjectPropertyAddress address = {
236 .mSelector = kAudioHardwarePropertyDevices,
237 .mScope = kAudioObjectPropertyScopeGlobal,
238 .mElement = kAudioObjectPropertyElementMain
239 };
240
241 return address;
242 }
243
244 static gboolean
245 _start_audio_device_watcher (GstOsxAudioDeviceProvider * self)
246 {
247 AudioObjectPropertyAddress propertyAddress = _get_devices_list_address ();
248 OSStatus result = AudioObjectAddPropertyListener (kAudioObjectSystemObject,
249 &propertyAddress, _audio_devices_changed_cb, self);
250
251 if (result != noErr) {
252 GST_WARNING ("Failed to add device list change listener: %d", result);
253 return FALSE;
254 }
255
256 GST_DEBUG ("Audio device watcher started");
257 return TRUE;
258 }
259
260 static gboolean
261 _stop_audio_device_watcher (GstOsxAudioDeviceProvider * self)
262 {
263 AudioObjectPropertyAddress propertyAddress = _get_devices_list_address ();
264 OSStatus result = AudioObjectRemovePropertyListener (kAudioObjectSystemObject,
265 &propertyAddress, _audio_devices_changed_cb, self);
266
267 if (result != noErr) {
268 GST_WARNING ("Failed to remove device list change listener: %d", result);
269 return FALSE;
270 }
271
272 GST_DEBUG ("Audio device watcher stopped");
273 return TRUE;
274 }
275
276 static void
277 gst_osx_audio_device_provider_probe_internal (GstOsxAudioDeviceProvider * self,
278 AudioDeviceID * osx_devices, gint ndevices, GList ** devices)
279 {
280 for (int i = 0; i < ndevices; i++) {
281 UInt32 transport_type;
282 char *device_name;
283 GstOsxAudioDevice *device;
284
285 device_name = gst_core_audio_device_get_prop_str (osx_devices[i],
286 kAudioObjectPropertyName);
287 if (!device_name)
288 continue;
289
290 transport_type = gst_core_audio_device_get_prop_uint32 (osx_devices[i],
291 kAudioDevicePropertyTransportType);
292 if (transport_type == UINT_MAX)
293 transport_type = kAudioDeviceTransportTypeUnknown;
294
295 if (_audio_device_has_input (osx_devices[i])) {
296 device =
297 gst_osx_audio_device_provider_probe_device (self, osx_devices[i],
298 device_name, transport_type, GST_OSX_AUDIO_DEVICE_TYPE_SOURCE);
299 if (device) {
300 GST_DEBUG ("Input Device ID: %u, Name: %s, Transport Type: %"
301 GST_FOURCC_FORMAT, (unsigned) osx_devices[i], device_name,
302 GST_FOURCC_ARGS (GUINT32_FROM_BE (transport_type)));
303 *devices = g_list_prepend (*devices, device);
304 }
305 }
306
307 if (_audio_device_has_output (osx_devices[i])) {
308 device =
309 gst_osx_audio_device_provider_probe_device (self, osx_devices[i],
310 device_name, transport_type, GST_OSX_AUDIO_DEVICE_TYPE_SINK);
311 if (device) {
312 GST_DEBUG ("Output Device ID: %u, Name: %s, Transport Type: %"
313 GST_FOURCC_FORMAT, (unsigned) osx_devices[i], device_name,
314 GST_FOURCC_ARGS (GUINT32_FROM_BE (transport_type)));
315 *devices = g_list_prepend (*devices, device);
316 }
317 }
318
319 g_free (device_name);
320 }
321 }
322
323 static GList *
324 gst_osx_audio_device_provider_probe (GstDeviceProvider * provider)
325 {
326 GstOsxAudioDeviceProvider *self = GST_OSX_AUDIO_DEVICE_PROVIDER (provider);
327 GList *devices = NULL;
328 AudioDeviceID *osx_devices = NULL;
329 gint ndevices = 0;
330
331 osx_devices = _audio_system_get_devices (&ndevices);
332
333 if (ndevices < 1) {
334 GST_WARNING ("No audio devices found");
335 goto done;
336 }
337
338 GST_INFO ("Found %d audio device(s)", ndevices);
339
340 gst_osx_audio_device_provider_probe_internal (self, osx_devices, ndevices,
341 &devices);
342
343 done:
344 g_free (osx_devices);
345
346 return devices;
347 }
348
349 static gboolean
350 gst_osx_audio_device_provider_start (GstDeviceProvider * provider)
351 {
352 GstOsxAudioDeviceProvider *self = GST_OSX_AUDIO_DEVICE_PROVIDER (provider);
353 GList *devices = NULL;
354 GList *iter;
355
356 devices = gst_osx_audio_device_provider_probe (provider);
357
358 for (iter = devices; iter; iter = iter->next) {
359 gst_device_provider_device_add (provider, GST_DEVICE (iter->data));
360 }
361
362 /* Device references were floating, so were transferred in
363 * gst_device_provider_device_add() */
364 g_list_free (devices);
365
366 return _start_audio_device_watcher (self);
367 }
368
369 static void
370 gst_osx_audio_device_provider_stop (GstDeviceProvider * provider)
371 {
372 GstOsxAudioDeviceProvider *self = GST_OSX_AUDIO_DEVICE_PROVIDER (provider);
373 _stop_audio_device_watcher (self);
374 }
375
376 static gboolean
377 gst_osx_audio_device_is_in_list (GList * list, GstDevice * gst_device)
378 {
379 GstOsxAudioDevice *osx_device = GST_OSX_AUDIO_DEVICE (gst_device);
380 gchar *name = gst_device_get_display_name (gst_device);
381 GList *iter;
382 gboolean found = FALSE;
383
384 for (iter = list; iter; iter = g_list_next (iter)) {
385 GstOsxAudioDevice *other_osx = GST_OSX_AUDIO_DEVICE (iter->data);
386 gchar *other_name = gst_device_get_display_name (GST_DEVICE (iter->data));
387
388 /* Only checking ID + class for now.
389 * Should be enough to pick up changes when an existing output device
390 * adds an input or vice versa */
391 if (g_ascii_strcasecmp (name, other_name) == 0
392 && osx_device->device_id == other_osx->device_id) {
393 found = TRUE;
394 }
395
396 g_free (other_name);
397
398 if (found)
399 break;
400 }
401
402 g_free (name);
403 return found;
404 }
405
406 static void
407 gst_osx_audio_device_provider_update_devices (GstOsxAudioDeviceProvider * self)
408 {
409 GstDeviceProvider *provider = GST_DEVICE_PROVIDER_CAST (self);
410 GList *prev_devices = NULL;
411 GList *new_devices = NULL;
412 GList *to_add = NULL;
413 GList *to_remove = NULL;
414 GList *iter;
415
416 GST_OBJECT_LOCK (self);
417 prev_devices = g_list_copy_deep (provider->devices,
418 (GCopyFunc) gst_object_ref, NULL);
419 GST_OBJECT_UNLOCK (self);
420
421 new_devices = gst_osx_audio_device_provider_probe (provider);
422 if (!new_devices)
423 goto done;
424
425 /* Ownership of GstDevice for gst_device_provider_device_add()
426 * and gst_device_provider_device_remove() is a bit complicated.
427 * Remove floating reference here for things to be clear */
428 for (iter = new_devices; iter; iter = g_list_next (iter))
429 gst_object_ref_sink (iter->data);
430
431 /* Check added devices */
432 for (iter = new_devices; iter; iter = g_list_next (iter)) {
433 if (!gst_osx_audio_device_is_in_list
434 (prev_devices, GST_DEVICE (iter->data))) {
435 to_add = g_list_prepend (to_add, gst_object_ref (iter->data));
436 }
437 }
438
439 /* Check removed devices */
440 for (iter = prev_devices; iter; iter = g_list_next (iter)) {
441 if (!gst_osx_audio_device_is_in_list (new_devices, GST_DEVICE (iter->data))) {
442 to_remove = g_list_prepend (to_remove, gst_object_ref (iter->data));
443 }
444 }
445
446 for (iter = to_remove; iter; iter = g_list_next (iter))
447 gst_device_provider_device_remove (provider, GST_DEVICE (iter->data));
448
449 for (iter = to_add; iter; iter = g_list_next (iter))
450 gst_device_provider_device_add (provider, GST_DEVICE (iter->data));
451
452 done:
453 if (prev_devices)
454 g_list_free_full (prev_devices, (GDestroyNotify) gst_object_unref);
455
456 if (to_add)
457 g_list_free_full (to_add, (GDestroyNotify) gst_object_unref);
458
459 if (to_remove)
460 g_list_free_full (to_remove, (GDestroyNotify) gst_object_unref);
461
462 if (new_devices)
463 g_list_free (new_devices);
464 }
465
466 enum
467 {
468 PROP_DEVICE_ID = 1,
469 PROP_UNIQUE_ID = 2,
470 };
471
472 #define gst_osx_audio_device_parent_class parent_class
473 G_DEFINE_TYPE (GstOsxAudioDevice, gst_osx_audio_device, GST_TYPE_DEVICE);
474
475 static void gst_osx_audio_device_get_property (GObject * object, guint prop_id,
476 GValue * value, GParamSpec * pspec);
477 static void gst_osx_audio_device_set_property (GObject * object, guint prop_id,
478 const GValue * value, GParamSpec * pspec);
479 static void gst_osx_audio_device_finalize (GObject * object);
480 static GstElement *gst_osx_audio_device_create_element (GstDevice * device,
481 const gchar * name);
482
483 static void
484 gst_osx_audio_device_class_init (GstOsxAudioDeviceClass * klass)
485 {
486 GstDeviceClass *dev_class = GST_DEVICE_CLASS (klass);
487 GObjectClass *object_class = G_OBJECT_CLASS (klass);
488
489 dev_class->create_element = gst_osx_audio_device_create_element;
490
491 object_class->get_property = gst_osx_audio_device_get_property;
492 object_class->set_property = gst_osx_audio_device_set_property;
493 object_class->finalize = gst_osx_audio_device_finalize;
494
495 g_object_class_install_property (object_class, PROP_DEVICE_ID,
496 g_param_spec_int ("device-id", "Device ID", "Device ID of audio device",
497 0, G_MAXINT, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
498
499 g_object_class_install_property (object_class, PROP_UNIQUE_ID,
500 g_param_spec_string ("unique-id", "Unique ID",
501 "Unique ID of audio device", NULL,
502 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
503 }
504
505 static void
506 gst_osx_audio_device_init (GstOsxAudioDevice * device)
507 {
508 }
509
510 static void
511 gst_osx_audio_device_finalize (GObject * object)
512 {
513 GstOsxAudioDevice *osxdev = GST_OSX_AUDIO_DEVICE (object);
514 g_clear_pointer (&osxdev->unique_id, g_free);
515 G_OBJECT_CLASS (parent_class)->finalize (object);
516 }
517
518 static GstElement *
519 gst_osx_audio_device_create_element (GstDevice * device, const gchar * name)
520 {
521 GstOsxAudioDevice *osxdev = GST_OSX_AUDIO_DEVICE (device);
522 GstElement *elem;
523
524 elem = gst_element_factory_make (osxdev->element, name);
525 g_object_set (elem, "unique-id", osxdev->unique_id, NULL);
526
527 return elem;
528 }
529
530 static GstOsxAudioDevice *
531 gst_osx_audio_device_new (AudioDeviceID device_id, const gchar * device_name,
532 UInt32 transport_type, GstOsxAudioDeviceType type,
533 GstCoreAudio * core_audio)
534 {
535 GstOsxAudioDevice *gstdev;
536 const gchar *element_name = NULL;
537 const gchar *klass = NULL;
538 GstCaps *template_caps, *caps;
539 GstStructure *props = gst_structure_new_empty ("properties");
540 char *transport_name = g_strdup_printf ("%" GST_FOURCC_FORMAT,
541 GST_FOURCC_ARGS (GUINT32_FROM_BE (transport_type)));
542
543 g_return_val_if_fail (device_id > 0, NULL);
544 g_return_val_if_fail (device_name, NULL);
545
546 gst_structure_set (props,
547 "is-default", G_TYPE_BOOLEAN, core_audio->is_default,
548 "transport", G_TYPE_STRING, transport_name, NULL);
549
550 g_free (transport_name);
551
552 if (core_audio->unique_id != NULL) {
553 gst_structure_set (props, "unique-id", G_TYPE_STRING,
554 core_audio->unique_id, NULL);
555 }
556
557 switch (type) {
558 case GST_OSX_AUDIO_DEVICE_TYPE_SOURCE:
559 element_name = "osxaudiosrc";
560 klass = "Audio/Source";
561
562 template_caps = gst_static_pad_template_get_caps (&src_factory);
563 caps = gst_core_audio_probe_caps (core_audio, template_caps);
564 gst_caps_unref (template_caps);
565
566 break;
567 case GST_OSX_AUDIO_DEVICE_TYPE_SINK:
568 element_name = "osxaudiosink";
569 klass = "Audio/Sink";
570
571 template_caps = gst_static_pad_template_get_caps (&sink_factory);
572 caps = gst_core_audio_probe_caps (core_audio, template_caps);
573 gst_caps_unref (template_caps);
574
575 break;
576 default:
577 g_assert_not_reached ();
578 break;
579 }
580
581 gstdev = g_object_new (GST_TYPE_OSX_AUDIO_DEVICE, "device-id", device_id,
582 "unique-id", core_audio->unique_id, "display-name", device_name, "caps",
583 caps, "properties", props, "device-class", klass, NULL);
584 gst_structure_free (props);
585 gst_caps_unref (caps);
586
587 gstdev->element = element_name;
588
589
590 return gstdev;
591 }
592
593 static void
594 gst_osx_audio_device_get_property (GObject * object, guint prop_id,
595 GValue * value, GParamSpec * pspec)
596 {
597 GstOsxAudioDevice *device;
598
599 device = GST_OSX_AUDIO_DEVICE_CAST (object);
600
601 switch (prop_id) {
602 case PROP_DEVICE_ID:
603 g_value_set_int (value, device->device_id);
604 break;
605 case PROP_UNIQUE_ID:
606 g_value_set_string (value, device->unique_id);
607 break;
608 default:
609 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
610 break;
611 }
612 }
613
614
615 static void
616 gst_osx_audio_device_set_property (GObject * object, guint prop_id,
617 const GValue * value, GParamSpec * pspec)
618 {
619 GstOsxAudioDevice *device;
620
621 device = GST_OSX_AUDIO_DEVICE_CAST (object);
622
623 switch (prop_id) {
624 case PROP_DEVICE_ID:
625 device->device_id = g_value_get_int (value);
626 break;
627 case PROP_UNIQUE_ID:
628 device->unique_id = g_value_dup_string (value);
629 break;
630 default:
631 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
632 break;
633 }
634 }