2 * Copyright (C) 2012 Olivier Crete <olivier.crete@collabora.com>
4 * pulsedeviceprovider.c: pulseaudio device probing and monitoring
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.
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.
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.
26 #include "pulsedeviceprovider.h"
33 #include "pulsesink.h"
34 #include "pulseutil.h"
37 GST_DEBUG_CATEGORY_EXTERN (pulse_debug);
38 #define GST_CAT_DEFAULT pulse_debug
41 static GstDevice *gst_pulse_device_new (guint id,
42 const gchar * device_name, GstCaps * caps, const gchar * internal_name,
43 GstPulseDeviceType type, GstStructure * properties);
45 G_DEFINE_TYPE (GstPulseDeviceProvider, gst_pulse_device_provider,
46 GST_TYPE_DEVICE_PROVIDER);
48 static void gst_pulse_device_provider_finalize (GObject * object);
49 static void gst_pulse_device_provider_set_property (GObject * object,
50 guint prop_id, const GValue * value, GParamSpec * pspec);
51 static void gst_pulse_device_provider_get_property (GObject * object,
52 guint prop_id, GValue * value, GParamSpec * pspec);
55 static GList *gst_pulse_device_provider_probe (GstDeviceProvider * provider);
56 static gboolean gst_pulse_device_provider_start (GstDeviceProvider * provider);
57 static void gst_pulse_device_provider_stop (GstDeviceProvider * provider);
69 gst_pulse_device_provider_class_init (GstPulseDeviceProviderClass * klass)
71 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
72 GstDeviceProviderClass *dm_class = GST_DEVICE_PROVIDER_CLASS (klass);
75 gobject_class->set_property = gst_pulse_device_provider_set_property;
76 gobject_class->get_property = gst_pulse_device_provider_get_property;
77 gobject_class->finalize = gst_pulse_device_provider_finalize;
79 dm_class->probe = gst_pulse_device_provider_probe;
80 dm_class->start = gst_pulse_device_provider_start;
81 dm_class->stop = gst_pulse_device_provider_stop;
83 g_object_class_install_property (gobject_class,
85 g_param_spec_string ("server", "Server",
86 "The PulseAudio server to connect to", NULL,
87 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
89 client_name = gst_pulse_client_name ();
90 g_object_class_install_property (gobject_class,
92 g_param_spec_string ("client-name", "Client Name",
93 "The PulseAudio client_name_to_use", client_name,
94 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
95 GST_PARAM_MUTABLE_READY));
98 gst_device_provider_class_set_static_metadata (dm_class,
99 "PulseAudio Device Provider", "Sink/Source/Audio",
100 "List and provider PulseAudio source and sink devices",
101 "Olivier Crete <olivier.crete@collabora.com>");
105 gst_pulse_device_provider_init (GstPulseDeviceProvider * self)
107 self->client_name = gst_pulse_client_name ();
111 gst_pulse_device_provider_finalize (GObject * object)
113 GstPulseDeviceProvider *self = GST_PULSE_DEVICE_PROVIDER (object);
115 g_free (self->client_name);
116 g_free (self->server);
118 G_OBJECT_CLASS (gst_pulse_device_provider_parent_class)->finalize (object);
123 gst_pulse_device_provider_set_property (GObject * object,
124 guint prop_id, const GValue * value, GParamSpec * pspec)
126 GstPulseDeviceProvider *self = GST_PULSE_DEVICE_PROVIDER (object);
130 g_free (self->server);
131 self->server = g_value_dup_string (value);
133 case PROP_CLIENT_NAME:
134 g_free (self->client_name);
135 if (!g_value_get_string (value)) {
136 GST_WARNING_OBJECT (self,
137 "Empty PulseAudio client name not allowed. "
138 "Resetting to default value");
139 self->client_name = gst_pulse_client_name ();
141 self->client_name = g_value_dup_string (value);
144 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
150 gst_pulse_device_provider_get_property (GObject * object,
151 guint prop_id, GValue * value, GParamSpec * pspec)
153 GstPulseDeviceProvider *self = GST_PULSE_DEVICE_PROVIDER (object);
157 g_value_set_string (value, self->server);
159 case PROP_CLIENT_NAME:
160 g_value_set_string (value, self->client_name);
163 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
169 context_state_cb (pa_context * c, void *userdata)
171 GstPulseDeviceProvider *self = userdata;
173 switch (pa_context_get_state (c)) {
174 case PA_CONTEXT_READY:
175 case PA_CONTEXT_TERMINATED:
176 case PA_CONTEXT_FAILED:
177 pa_threaded_mainloop_signal (self->mainloop, 0);
180 case PA_CONTEXT_UNCONNECTED:
181 case PA_CONTEXT_CONNECTING:
182 case PA_CONTEXT_AUTHORIZING:
183 case PA_CONTEXT_SETTING_NAME:
189 new_source (const pa_source_info * info)
195 caps = gst_caps_new_empty ();
197 for (i = 0; i < info->n_formats; i++)
198 gst_caps_append (caps, gst_pulse_format_info_to_caps (info->formats[i]));
200 props = gst_pulse_make_structure (info->proplist);
202 return gst_pulse_device_new (info->index, info->description,
203 caps, info->name, GST_PULSE_DEVICE_TYPE_SOURCE, props);
207 new_sink (const pa_sink_info * info)
213 caps = gst_caps_new_empty ();
215 for (i = 0; i < info->n_formats; i++)
216 gst_caps_append (caps, gst_pulse_format_info_to_caps (info->formats[i]));
218 props = gst_pulse_make_structure (info->proplist);
220 return gst_pulse_device_new (info->index, info->description,
221 caps, info->name, GST_PULSE_DEVICE_TYPE_SINK, props);
225 get_source_info_cb (pa_context * context,
226 const pa_source_info * info, int eol, void *userdata)
228 GstPulseDeviceProvider *self = userdata;
232 pa_threaded_mainloop_signal (self->mainloop, 0);
236 dev = new_source (info);
239 gst_device_provider_device_add (GST_DEVICE_PROVIDER (self), dev);
243 get_sink_info_cb (pa_context * context,
244 const pa_sink_info * info, int eol, void *userdata)
246 GstPulseDeviceProvider *self = userdata;
250 pa_threaded_mainloop_signal (self->mainloop, 0);
254 dev = new_sink (info);
257 gst_device_provider_device_add (GST_DEVICE_PROVIDER (self), dev);
261 context_subscribe_cb (pa_context * context, pa_subscription_event_type_t type,
262 uint32_t idx, void *userdata)
264 GstPulseDeviceProvider *self = userdata;
265 GstDeviceProvider *provider = userdata;
266 pa_subscription_event_type_t facility =
267 type & PA_SUBSCRIPTION_EVENT_FACILITY_MASK;
268 pa_subscription_event_type_t event_type =
269 type & PA_SUBSCRIPTION_EVENT_TYPE_MASK;
271 if (facility != PA_SUBSCRIPTION_EVENT_SOURCE &&
272 facility != PA_SUBSCRIPTION_EVENT_SINK)
275 if (event_type == PA_SUBSCRIPTION_EVENT_NEW) {
276 /* Microphone in the source output has changed */
278 if (facility == PA_SUBSCRIPTION_EVENT_SOURCE)
279 pa_context_get_source_info_by_index (context, idx, get_source_info_cb,
281 else if (facility == PA_SUBSCRIPTION_EVENT_SINK)
282 pa_context_get_sink_info_by_index (context, idx, get_sink_info_cb, self);
283 } else if (event_type == PA_SUBSCRIPTION_EVENT_REMOVE) {
284 GstPulseDevice *dev = NULL;
287 GST_OBJECT_LOCK (self);
288 for (item = provider->devices; item; item = item->next) {
291 if (((facility == PA_SUBSCRIPTION_EVENT_SOURCE &&
292 dev->type == GST_PULSE_DEVICE_TYPE_SOURCE) ||
293 (facility == PA_SUBSCRIPTION_EVENT_SINK &&
294 dev->type == GST_PULSE_DEVICE_TYPE_SINK)) &&
295 dev->device_index == idx) {
296 gst_object_ref (dev);
301 GST_OBJECT_UNLOCK (self);
304 gst_device_provider_device_remove (GST_DEVICE_PROVIDER (self),
306 gst_object_unref (dev);
312 get_source_info_list_cb (pa_context * context, const pa_source_info * info,
313 int eol, void *userdata)
315 GList **devices = userdata;
320 *devices = g_list_prepend (*devices, gst_object_ref_sink (new_source (info)));
324 get_sink_info_list_cb (pa_context * context, const pa_sink_info * info,
325 int eol, void *userdata)
327 GList **devices = userdata;
332 *devices = g_list_prepend (*devices, gst_object_ref_sink (new_sink (info)));
336 gst_pulse_device_provider_probe (GstDeviceProvider * provider)
338 GstPulseDeviceProvider *self = GST_PULSE_DEVICE_PROVIDER (provider);
339 GList *devices = NULL;
340 pa_mainloop *m = NULL;
341 pa_context *c = NULL;
344 if (!(m = pa_mainloop_new ()))
347 if (!(c = pa_context_new (pa_mainloop_get_api (m), self->client_name))) {
348 GST_ERROR_OBJECT (self, "Failed to create context");
352 if (pa_context_connect (c, self->server, 0, NULL) < 0) {
353 GST_ERROR_OBJECT (self, "Failed to connect: %s",
354 pa_strerror (pa_context_errno (self->context)));
359 pa_context_state_t state;
361 state = pa_context_get_state (c);
363 if (!PA_CONTEXT_IS_GOOD (state)) {
364 GST_ELEMENT_ERROR (self, RESOURCE, FAILED, ("Failed to connect: %s",
365 pa_strerror (pa_context_errno (c))), (NULL));
369 if (state == PA_CONTEXT_READY)
372 /* Wait until the context is ready */
373 if (pa_mainloop_iterate (m, TRUE, NULL) < 0)
377 GST_DEBUG_OBJECT (self, "connected");
379 o = pa_context_get_sink_info_list (c, get_sink_info_list_cb, &devices);
380 while (pa_operation_get_state (o) == PA_OPERATION_RUNNING &&
381 pa_operation_get_state (o) == PA_OPERATION_RUNNING) {
382 if (pa_mainloop_iterate (m, TRUE, NULL) < 0)
385 pa_operation_unref (o);
387 o = pa_context_get_source_info_list (c, get_source_info_list_cb, &devices);
388 while (pa_operation_get_state (o) == PA_OPERATION_RUNNING &&
389 pa_operation_get_state (o) == PA_OPERATION_RUNNING) {
390 if (pa_mainloop_iterate (m, TRUE, NULL) < 0)
393 pa_operation_unref (o);
395 pa_context_disconnect (c);
396 pa_mainloop_free (m);
406 gst_pulse_device_provider_start (GstDeviceProvider * provider)
408 GstPulseDeviceProvider *self = GST_PULSE_DEVICE_PROVIDER (provider);
409 pa_operation *initial_operation;
411 if (!(self->mainloop = pa_threaded_mainloop_new ())) {
412 GST_ERROR_OBJECT (self, "Could not create pulseaudio mainloop");
413 goto mainloop_failed;
415 if (pa_threaded_mainloop_start (self->mainloop) < 0) {
416 GST_ERROR_OBJECT (self, "Could not start pulseaudio mainloop");
417 pa_threaded_mainloop_free (self->mainloop);
418 self->mainloop = NULL;
419 goto mainloop_failed;
422 pa_threaded_mainloop_lock (self->mainloop);
424 if (!(self->context =
425 pa_context_new (pa_threaded_mainloop_get_api (self->mainloop),
426 self->client_name))) {
427 GST_ERROR_OBJECT (self, "Failed to create context");
428 goto unlock_and_fail;
431 pa_context_set_state_callback (self->context, context_state_cb, self);
432 pa_context_set_subscribe_callback (self->context, context_subscribe_cb, self);
435 GST_DEBUG_OBJECT (self, "connect to server %s", GST_STR_NULL (self->server));
437 if (pa_context_connect (self->context, self->server, 0, NULL) < 0) {
438 GST_ERROR_OBJECT (self, "Failed to connect: %s",
439 pa_strerror (pa_context_errno (self->context)));
440 goto unlock_and_fail;
444 pa_context_state_t state;
446 state = pa_context_get_state (self->context);
448 if (!PA_CONTEXT_IS_GOOD (state)) {
449 GST_ERROR_OBJECT (self, "Failed to connect: %s",
450 pa_strerror (pa_context_errno (self->context)));
451 goto unlock_and_fail;
454 if (state == PA_CONTEXT_READY)
457 /* Wait until the context is ready */
458 pa_threaded_mainloop_wait (self->mainloop);
460 GST_DEBUG_OBJECT (self, "connected");
462 pa_context_subscribe (self->context,
463 PA_SUBSCRIPTION_MASK_SOURCE | PA_SUBSCRIPTION_MASK_SINK, NULL, NULL);
465 initial_operation = pa_context_get_source_info_list (self->context,
466 get_source_info_cb, self);
467 while (pa_operation_get_state (initial_operation) == PA_OPERATION_RUNNING) {
468 if (!PA_CONTEXT_IS_GOOD (pa_context_get_state ((self->context))))
469 goto cancel_and_fail;
471 pa_threaded_mainloop_wait (self->mainloop);
473 pa_operation_unref (initial_operation);
475 initial_operation = pa_context_get_sink_info_list (self->context,
476 get_sink_info_cb, self);
477 if (!initial_operation)
478 goto unlock_and_fail;
479 while (pa_operation_get_state (initial_operation) == PA_OPERATION_RUNNING) {
480 if (!PA_CONTEXT_IS_GOOD (pa_context_get_state ((self->context))))
481 goto cancel_and_fail;
483 pa_threaded_mainloop_wait (self->mainloop);
485 pa_operation_unref (initial_operation);
487 pa_threaded_mainloop_unlock (self->mainloop);
492 pa_threaded_mainloop_unlock (self->mainloop);
493 gst_pulse_device_provider_stop (provider);
500 pa_operation_cancel (initial_operation);
501 pa_operation_unref (initial_operation);
502 goto unlock_and_fail;
506 gst_pulse_device_provider_stop (GstDeviceProvider * provider)
508 GstPulseDeviceProvider *self = GST_PULSE_DEVICE_PROVIDER (provider);
510 pa_threaded_mainloop_stop (self->mainloop);
513 pa_context_disconnect (self->context);
515 /* Make sure we don't get any further callbacks */
516 pa_context_set_state_callback (self->context, NULL, NULL);
517 pa_context_set_subscribe_callback (self->context, NULL, NULL);
519 pa_context_unref (self->context);
520 self->context = NULL;
523 pa_threaded_mainloop_free (self->mainloop);
524 self->mainloop = NULL;
529 PROP_INTERNAL_NAME = 1,
532 G_DEFINE_TYPE (GstPulseDevice, gst_pulse_device, GST_TYPE_DEVICE);
534 static void gst_pulse_device_get_property (GObject * object, guint prop_id,
535 GValue * value, GParamSpec * pspec);
536 static void gst_pulse_device_set_property (GObject * object, guint prop_id,
537 const GValue * value, GParamSpec * pspec);
538 static void gst_pulse_device_finalize (GObject * object);
539 static GstElement *gst_pulse_device_create_element (GstDevice * device,
541 static gboolean gst_pulse_device_reconfigure_element (GstDevice * device,
542 GstElement * element);
545 gst_pulse_device_class_init (GstPulseDeviceClass * klass)
547 GstDeviceClass *dev_class = GST_DEVICE_CLASS (klass);
548 GObjectClass *object_class = G_OBJECT_CLASS (klass);
550 dev_class->create_element = gst_pulse_device_create_element;
551 dev_class->reconfigure_element = gst_pulse_device_reconfigure_element;
553 object_class->get_property = gst_pulse_device_get_property;
554 object_class->set_property = gst_pulse_device_set_property;
555 object_class->finalize = gst_pulse_device_finalize;
557 g_object_class_install_property (object_class, PROP_INTERNAL_NAME,
558 g_param_spec_string ("internal-name", "Internal PulseAudio device name",
559 "The internal name of the PulseAudio device", "",
560 G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
564 gst_pulse_device_init (GstPulseDevice * device)
569 gst_pulse_device_finalize (GObject * object)
571 GstPulseDevice *device = GST_PULSE_DEVICE (object);
573 g_free (device->internal_name);
575 G_OBJECT_CLASS (gst_pulse_device_parent_class)->finalize (object);
579 gst_pulse_device_create_element (GstDevice * device, const gchar * name)
581 GstPulseDevice *pulse_dev = GST_PULSE_DEVICE (device);
584 elem = gst_element_factory_make (pulse_dev->element, name);
585 g_object_set (elem, "device", pulse_dev->internal_name, NULL);
591 gst_pulse_device_reconfigure_element (GstDevice * device, GstElement * element)
593 GstPulseDevice *pulse_dev = GST_PULSE_DEVICE (device);
595 if (!strcmp (pulse_dev->element, "pulsesrc")) {
596 if (!GST_IS_PULSESRC (element))
598 } else if (!strcmp (pulse_dev->element, "pulsesink")) {
599 if (!GST_IS_PULSESINK (element))
602 g_assert_not_reached ();
605 g_object_set (element, "device", pulse_dev->internal_name, NULL);
610 /* Takes ownership of @caps and @props */
612 gst_pulse_device_new (guint device_index, const gchar * device_name,
613 GstCaps * caps, const gchar * internal_name, GstPulseDeviceType type,
614 GstStructure * props)
616 GstPulseDevice *gstdev;
617 const gchar *element = NULL;
618 const gchar *klass = NULL;
620 g_return_val_if_fail (device_name, NULL);
621 g_return_val_if_fail (internal_name, NULL);
622 g_return_val_if_fail (caps, NULL);
626 case GST_PULSE_DEVICE_TYPE_SOURCE:
627 element = "pulsesrc";
628 klass = "Audio/Source";
630 case GST_PULSE_DEVICE_TYPE_SINK:
631 element = "pulsesink";
632 klass = "Audio/Sink";
635 g_assert_not_reached ();
640 gstdev = g_object_new (GST_TYPE_PULSE_DEVICE,
641 "display-name", device_name, "caps", caps, "device-class", klass,
642 "internal-name", internal_name, "properties", props, NULL);
645 gstdev->device_index = device_index;
646 gstdev->element = element;
648 gst_structure_free (props);
649 gst_caps_unref (caps);
651 return GST_DEVICE (gstdev);
656 gst_pulse_device_get_property (GObject * object, guint prop_id,
657 GValue * value, GParamSpec * pspec)
659 GstPulseDevice *device;
661 device = GST_PULSE_DEVICE_CAST (object);
664 case PROP_INTERNAL_NAME:
665 g_value_set_string (value, device->internal_name);
668 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
675 gst_pulse_device_set_property (GObject * object, guint prop_id,
676 const GValue * value, GParamSpec * pspec)
678 GstPulseDevice *device;
680 device = GST_PULSE_DEVICE_CAST (object);
683 case PROP_INTERNAL_NAME:
684 device->internal_name = g_value_dup_string (value);
687 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);