2 * Copyright (C) 2012 Olivier Crete <olivier.crete@collabora.com>
4 * gstv4l2devicemonitor.c: V4l2 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 "pulsedevicemonitor.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);
45 G_DEFINE_TYPE (GstPulseDeviceMonitor, gst_pulse_device_monitor,
46 GST_TYPE_DEVICE_MONITOR);
48 static void gst_pulse_device_monitor_finalize (GObject * object);
49 static void gst_pulse_device_monitor_set_property (GObject * object,
50 guint prop_id, const GValue * value, GParamSpec * pspec);
51 static void gst_pulse_device_monitor_get_property (GObject * object,
52 guint prop_id, GValue * value, GParamSpec * pspec);
55 static GList *gst_pulse_device_monitor_probe (GstDeviceMonitor * monitor);
56 static gboolean gst_pulse_device_monitor_start (GstDeviceMonitor * monitor);
57 static void gst_pulse_device_monitor_stop (GstDeviceMonitor * monitor);
69 gst_pulse_device_monitor_class_init (GstPulseDeviceMonitorClass * klass)
71 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
72 GstDeviceMonitorClass *dm_class = GST_DEVICE_MONITOR_CLASS (klass);
75 gobject_class->set_property = gst_pulse_device_monitor_set_property;
76 gobject_class->get_property = gst_pulse_device_monitor_get_property;
77 gobject_class->finalize = gst_pulse_device_monitor_finalize;
79 dm_class->probe = gst_pulse_device_monitor_probe;
80 dm_class->start = gst_pulse_device_monitor_start;
81 dm_class->stop = gst_pulse_device_monitor_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_monitor_class_set_static_metadata (dm_class,
99 "PulseAudio Device Monitor", "Sink/Source/Audio",
100 "List and monitor PulseAudio source and sink devices",
101 "Olivier Crete <olivier.crete@collabora.com>");
105 gst_pulse_device_monitor_init (GstPulseDeviceMonitor * self)
107 self->client_name = gst_pulse_client_name ();
111 gst_pulse_device_monitor_finalize (GObject * object)
113 GstPulseDeviceMonitor *self = GST_PULSE_DEVICE_MONITOR (object);
115 g_free (self->client_name);
116 g_free (self->server);
118 G_OBJECT_CLASS (gst_pulse_device_monitor_parent_class)->finalize (object);
123 gst_pulse_device_monitor_set_property (GObject * object,
124 guint prop_id, const GValue * value, GParamSpec * pspec)
126 GstPulseDeviceMonitor *self = GST_PULSE_DEVICE_MONITOR (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_monitor_get_property (GObject * object,
151 guint prop_id, GValue * value, GParamSpec * pspec)
153 GstPulseDeviceMonitor *self = GST_PULSE_DEVICE_MONITOR (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 GstPulseDeviceMonitor *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)
194 caps = gst_caps_new_empty ();
196 for (i = 0; i < info->n_formats; i++)
197 gst_caps_append (caps, gst_pulse_format_info_to_caps (info->formats[i]));
199 return gst_pulse_device_new (info->index, info->description,
200 caps, info->name, GST_PULSE_DEVICE_TYPE_SOURCE);
204 new_sink (const pa_sink_info * info)
209 caps = gst_caps_new_empty ();
211 for (i = 0; i < info->n_formats; i++)
212 gst_caps_append (caps, gst_pulse_format_info_to_caps (info->formats[i]));
214 return gst_pulse_device_new (info->index, info->description,
215 caps, info->name, GST_PULSE_DEVICE_TYPE_SINK);
219 get_source_info_cb (pa_context * context,
220 const pa_source_info * info, int eol, void *userdata)
222 GstPulseDeviceMonitor *self = userdata;
226 pa_threaded_mainloop_signal (self->mainloop, 0);
230 dev = new_source (info);
233 gst_device_monitor_device_add (GST_DEVICE_MONITOR (self), dev);
237 get_sink_info_cb (pa_context * context,
238 const pa_sink_info * info, int eol, void *userdata)
240 GstPulseDeviceMonitor *self = userdata;
244 pa_threaded_mainloop_signal (self->mainloop, 0);
248 dev = new_sink (info);
251 gst_device_monitor_device_add (GST_DEVICE_MONITOR (self), dev);
255 context_subscribe_cb (pa_context * context, pa_subscription_event_type_t type,
256 uint32_t idx, void *userdata)
258 GstPulseDeviceMonitor *self = userdata;
259 GstDeviceMonitor *monitor = userdata;
260 pa_subscription_event_type_t facility =
261 type & PA_SUBSCRIPTION_EVENT_FACILITY_MASK;
262 pa_subscription_event_type_t event_type =
263 type & PA_SUBSCRIPTION_EVENT_TYPE_MASK;
265 if (facility != PA_SUBSCRIPTION_EVENT_SOURCE &&
266 facility != PA_SUBSCRIPTION_EVENT_SINK)
269 if (event_type == PA_SUBSCRIPTION_EVENT_NEW) {
270 /* Microphone in the source output has changed */
272 if (facility == PA_SUBSCRIPTION_EVENT_SOURCE)
273 pa_context_get_source_info_by_index (context, idx, get_source_info_cb,
275 else if (facility == PA_SUBSCRIPTION_EVENT_SINK)
276 pa_context_get_sink_info_by_index (context, idx, get_sink_info_cb, self);
277 } else if (event_type == PA_SUBSCRIPTION_EVENT_REMOVE) {
278 GstPulseDevice *dev = NULL;
281 GST_OBJECT_LOCK (self);
282 for (item = monitor->devices; item; item = item->next) {
285 if (dev->device_index == idx) {
286 gst_object_ref (dev);
291 GST_OBJECT_UNLOCK (self);
294 gst_device_monitor_device_remove (GST_DEVICE_MONITOR (self),
296 gst_object_unref (dev);
302 get_source_info_list_cb (pa_context * context, const pa_source_info * info,
303 int eol, void *userdata)
305 GList **devices = userdata;
310 *devices = g_list_prepend (*devices, gst_object_ref_sink (new_source (info)));
314 get_sink_info_list_cb (pa_context * context, const pa_sink_info * info,
315 int eol, void *userdata)
317 GList **devices = userdata;
322 *devices = g_list_prepend (*devices, gst_object_ref_sink (new_sink (info)));
326 gst_pulse_device_monitor_probe (GstDeviceMonitor * monitor)
328 GstPulseDeviceMonitor *self = GST_PULSE_DEVICE_MONITOR (monitor);
329 GList *devices = NULL;
330 pa_mainloop *m = NULL;
331 pa_context *c = NULL;
334 if (!(m = pa_mainloop_new ()))
337 if (!(c = pa_context_new (pa_mainloop_get_api (m), self->client_name))) {
338 GST_ERROR_OBJECT (self, "Failed to create context");
342 if (pa_context_connect (c, self->server, 0, NULL) < 0) {
343 GST_ERROR_OBJECT (self, "Failed to connect: %s",
344 pa_strerror (pa_context_errno (self->context)));
349 pa_context_state_t state;
351 state = pa_context_get_state (c);
353 if (!PA_CONTEXT_IS_GOOD (state)) {
354 GST_ELEMENT_ERROR (self, RESOURCE, FAILED, ("Failed to connect: %s",
355 pa_strerror (pa_context_errno (c))), (NULL));
359 if (state == PA_CONTEXT_READY)
362 /* Wait until the context is ready */
363 if (pa_mainloop_iterate (m, TRUE, NULL) < 0)
367 GST_DEBUG_OBJECT (self, "connected");
369 o = pa_context_get_sink_info_list (c, get_sink_info_list_cb, &devices);
370 while (pa_operation_get_state (o) == PA_OPERATION_RUNNING &&
371 pa_operation_get_state (o) == PA_OPERATION_RUNNING) {
372 if (pa_mainloop_iterate (m, TRUE, NULL) < 0)
375 pa_operation_unref (o);
377 o = pa_context_get_source_info_list (c, get_source_info_list_cb, &devices);
378 while (pa_operation_get_state (o) == PA_OPERATION_RUNNING &&
379 pa_operation_get_state (o) == PA_OPERATION_RUNNING) {
380 if (pa_mainloop_iterate (m, TRUE, NULL) < 0)
383 pa_operation_unref (o);
385 pa_context_disconnect (c);
386 pa_mainloop_free (m);
396 gst_pulse_device_monitor_start (GstDeviceMonitor * monitor)
398 GstPulseDeviceMonitor *self = GST_PULSE_DEVICE_MONITOR (monitor);
399 pa_operation *initial_operation;
401 if (!(self->mainloop = pa_threaded_mainloop_new ())) {
402 GST_ERROR_OBJECT (self, "Could not create pulseaudio mainloop");
403 goto mainloop_failed;
405 if (pa_threaded_mainloop_start (self->mainloop) < 0) {
406 GST_ERROR_OBJECT (self, "Could not start pulseaudio mainloop");
407 pa_threaded_mainloop_free (self->mainloop);
408 self->mainloop = NULL;
409 goto mainloop_failed;
412 pa_threaded_mainloop_lock (self->mainloop);
414 if (!(self->context =
415 pa_context_new (pa_threaded_mainloop_get_api (self->mainloop),
416 self->client_name))) {
417 GST_ERROR_OBJECT (self, "Failed to create context");
418 goto unlock_and_fail;
421 pa_context_set_state_callback (self->context, context_state_cb, self);
422 pa_context_set_subscribe_callback (self->context, context_subscribe_cb, self);
425 GST_DEBUG_OBJECT (self, "connect to server %s", GST_STR_NULL (self->server));
427 if (pa_context_connect (self->context, self->server, 0, NULL) < 0) {
428 GST_ERROR_OBJECT (self, "Failed to connect: %s",
429 pa_strerror (pa_context_errno (self->context)));
430 goto unlock_and_fail;
434 pa_context_state_t state;
436 state = pa_context_get_state (self->context);
438 if (!PA_CONTEXT_IS_GOOD (state)) {
439 GST_ERROR_OBJECT (self, "Failed to connect: %s",
440 pa_strerror (pa_context_errno (self->context)));
441 goto unlock_and_fail;
444 if (state == PA_CONTEXT_READY)
447 /* Wait until the context is ready */
448 pa_threaded_mainloop_wait (self->mainloop);
450 GST_DEBUG_OBJECT (self, "connected");
452 pa_context_subscribe (self->context,
453 PA_SUBSCRIPTION_MASK_SOURCE | PA_SUBSCRIPTION_MASK_SINK, NULL, NULL);
455 initial_operation = pa_context_get_source_info_list (self->context,
456 get_source_info_cb, self);
457 while (pa_operation_get_state (initial_operation) == PA_OPERATION_RUNNING) {
458 if (!PA_CONTEXT_IS_GOOD (pa_context_get_state ((self->context))))
459 goto cancel_and_fail;
461 pa_threaded_mainloop_wait (self->mainloop);
463 pa_operation_unref (initial_operation);
465 initial_operation = pa_context_get_sink_info_list (self->context,
466 get_sink_info_cb, self);
467 if (!initial_operation)
468 goto unlock_and_fail;
469 while (pa_operation_get_state (initial_operation) == PA_OPERATION_RUNNING) {
470 if (!PA_CONTEXT_IS_GOOD (pa_context_get_state ((self->context))))
471 goto cancel_and_fail;
473 pa_threaded_mainloop_wait (self->mainloop);
475 pa_operation_unref (initial_operation);
477 pa_threaded_mainloop_unlock (self->mainloop);
482 pa_threaded_mainloop_unlock (self->mainloop);
483 gst_pulse_device_monitor_stop (monitor);
490 pa_operation_cancel (initial_operation);
491 pa_operation_unref (initial_operation);
492 goto unlock_and_fail;
496 gst_pulse_device_monitor_stop (GstDeviceMonitor * monitor)
498 GstPulseDeviceMonitor *self = GST_PULSE_DEVICE_MONITOR (monitor);
500 pa_threaded_mainloop_stop (self->mainloop);
503 pa_context_disconnect (self->context);
505 /* Make sure we don't get any further callbacks */
506 pa_context_set_state_callback (self->context, NULL, NULL);
507 pa_context_set_subscribe_callback (self->context, NULL, NULL);
509 pa_context_unref (self->context);
510 self->context = NULL;
513 pa_threaded_mainloop_free (self->mainloop);
514 self->mainloop = NULL;
519 PROP_INTERNAL_NAME = 1,
522 G_DEFINE_TYPE (GstPulseDevice, gst_pulse_device, GST_TYPE_DEVICE);
524 static void gst_pulse_device_get_property (GObject * object, guint prop_id,
525 GValue * value, GParamSpec * pspec);
526 static void gst_pulse_device_set_property (GObject * object, guint prop_id,
527 const GValue * value, GParamSpec * pspec);
528 static void gst_pulse_device_finalize (GObject * object);
529 static GstElement *gst_pulse_device_create_element (GstDevice * device,
531 static gboolean gst_pulse_device_reconfigure_element (GstDevice * device,
532 GstElement * element);
535 gst_pulse_device_class_init (GstPulseDeviceClass * klass)
537 GstDeviceClass *dev_class = GST_DEVICE_CLASS (klass);
538 GObjectClass *object_class = G_OBJECT_CLASS (klass);
540 dev_class->create_element = gst_pulse_device_create_element;
541 dev_class->reconfigure_element = gst_pulse_device_reconfigure_element;
543 object_class->get_property = gst_pulse_device_get_property;
544 object_class->set_property = gst_pulse_device_set_property;
545 object_class->finalize = gst_pulse_device_finalize;
547 g_object_class_install_property (object_class, PROP_INTERNAL_NAME,
548 g_param_spec_string ("internal-name", "Internal PulseAudio device name",
549 "The internal name of the PulseAudio device", "",
550 G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
554 gst_pulse_device_init (GstPulseDevice * device)
559 gst_pulse_device_finalize (GObject * object)
561 GstPulseDevice *device = GST_PULSE_DEVICE (object);
563 g_free (device->internal_name);
565 G_OBJECT_CLASS (gst_pulse_device_parent_class)->finalize (object);
569 gst_pulse_device_create_element (GstDevice * device, const gchar * name)
571 GstPulseDevice *pulse_dev = GST_PULSE_DEVICE (device);
574 elem = gst_element_factory_make (pulse_dev->element, name);
575 g_object_set (elem, "device", pulse_dev->internal_name, NULL);
581 gst_pulse_device_reconfigure_element (GstDevice * device, GstElement * element)
583 GstPulseDevice *pulse_dev = GST_PULSE_DEVICE (device);
585 if (!strcmp (pulse_dev->element, "pulsesrc")) {
586 if (!GST_IS_PULSESRC (element))
588 } else if (!strcmp (pulse_dev->element, "pulsesink")) {
589 if (!GST_IS_PULSESINK (element))
592 g_assert_not_reached ();
595 g_object_set (element, "device", pulse_dev->internal_name, NULL);
601 gst_pulse_device_new (guint device_index, const gchar * device_name,
602 GstCaps * caps, const gchar * internal_name, GstPulseDeviceType type)
604 GstPulseDevice *gstdev;
605 const gchar *element;
608 g_return_val_if_fail (device_name, NULL);
609 g_return_val_if_fail (internal_name, NULL);
610 g_return_val_if_fail (caps, NULL);
614 case GST_PULSE_DEVICE_TYPE_SOURCE:
615 element = "pulsesrc";
616 klass = "Audio/Source";
618 case GST_PULSE_DEVICE_TYPE_SINK:
619 element = "pulsesink";
620 klass = "Audio/Sink";
623 g_assert_not_reached ();
628 gstdev = g_object_new (GST_TYPE_PULSE_DEVICE,
629 "display-name", device_name, "caps", caps, "klass", klass,
630 "internal-name", internal_name, NULL);
632 gstdev->device_index = device_index;
633 gstdev->element = element;
635 return GST_DEVICE (gstdev);
640 gst_pulse_device_get_property (GObject * object, guint prop_id,
641 GValue * value, GParamSpec * pspec)
643 GstPulseDevice *device;
645 device = GST_PULSE_DEVICE_CAST (object);
648 case PROP_INTERNAL_NAME:
649 g_value_set_string (value, device->internal_name);
652 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
659 gst_pulse_device_set_property (GObject * object, guint prop_id,
660 const GValue * value, GParamSpec * pspec)
662 GstPulseDevice *device;
664 device = GST_PULSE_DEVICE_CAST (object);
667 case PROP_INTERNAL_NAME:
668 device->internal_name = g_value_dup_string (value);
671 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);