1 /*-*- Mode: C; c-basic-offset: 2 -*-*/
4 * GStreamer pulseaudio plugin
6 * Copyright (c) 2004-2008 Lennart Poettering
8 * gst-pulse is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU Lesser General Public License as
10 * published by the Free Software Foundation; either version 2.1 of the
11 * License, or (at your option) any later version.
13 * gst-pulse is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with gst-pulse; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
24 /* FIXME 0.11: suppress warnings for deprecated API such as GValueArray
25 * with newer GLib versions (>= 2.31.0) */
26 #define GLIB_DISABLE_DEPRECATION_WARNINGS
32 #include "pulseprobe.h"
33 #include "pulseutil.h"
35 GST_DEBUG_CATEGORY_EXTERN (pulse_debug);
36 #define GST_CAT_DEFAULT pulse_debug
39 gst_pulseprobe_context_state_cb (pa_context * context, void *userdata)
41 GstPulseProbe *c = (GstPulseProbe *) userdata;
43 /* Called from the background thread! */
45 switch (pa_context_get_state (context)) {
46 case PA_CONTEXT_READY:
47 case PA_CONTEXT_TERMINATED:
48 case PA_CONTEXT_FAILED:
49 pa_threaded_mainloop_signal (c->mainloop, 0);
52 case PA_CONTEXT_UNCONNECTED:
53 case PA_CONTEXT_CONNECTING:
54 case PA_CONTEXT_AUTHORIZING:
55 case PA_CONTEXT_SETTING_NAME:
61 gst_pulseprobe_sink_info_cb (pa_context * context, const pa_sink_info * i,
62 int eol, void *userdata)
64 GstPulseProbe *c = (GstPulseProbe *) userdata;
66 /* Called from the background thread! */
69 c->operation_success = eol > 0;
70 pa_threaded_mainloop_signal (c->mainloop, 0);
74 c->devices = g_list_append (c->devices, g_strdup (i->name));
79 gst_pulseprobe_source_info_cb (pa_context * context, const pa_source_info * i,
80 int eol, void *userdata)
82 GstPulseProbe *c = (GstPulseProbe *) userdata;
84 /* Called from the background thread! */
87 c->operation_success = eol > 0;
88 pa_threaded_mainloop_signal (c->mainloop, 0);
92 c->devices = g_list_append (c->devices, g_strdup (i->name));
96 gst_pulseprobe_invalidate (GstPulseProbe * c)
98 g_list_foreach (c->devices, (GFunc) g_free, NULL);
99 g_list_free (c->devices);
101 c->devices_valid = FALSE;
105 gst_pulseprobe_open (GstPulseProbe * c)
112 GST_DEBUG_OBJECT (c->object, "probe open");
114 c->mainloop = pa_threaded_mainloop_new ();
118 e = pa_threaded_mainloop_start (c->mainloop);
122 name = gst_pulse_client_name ();
124 pa_threaded_mainloop_lock (c->mainloop);
127 pa_context_new (pa_threaded_mainloop_get_api (c->mainloop), name))) {
128 GST_WARNING_OBJECT (c->object, "Failed to create context");
129 goto unlock_and_fail;
132 pa_context_set_state_callback (c->context, gst_pulseprobe_context_state_cb,
135 if (pa_context_connect (c->context, c->server, 0, NULL) < 0) {
136 GST_WARNING_OBJECT (c->object, "Failed to connect context: %s",
137 pa_strerror (pa_context_errno (c->context)));
138 goto unlock_and_fail;
142 pa_context_state_t state;
144 state = pa_context_get_state (c->context);
146 if (!PA_CONTEXT_IS_GOOD (state)) {
147 GST_WARNING_OBJECT (c->object, "Failed to connect context: %s",
148 pa_strerror (pa_context_errno (c->context)));
149 goto unlock_and_fail;
152 if (state == PA_CONTEXT_READY)
155 /* Wait until the context is ready */
156 pa_threaded_mainloop_wait (c->mainloop);
159 pa_threaded_mainloop_unlock (c->mainloop);
162 gst_pulseprobe_invalidate (c);
169 pa_threaded_mainloop_unlock (c->mainloop);
177 gst_pulseprobe_is_dead (GstPulseProbe * pulseprobe)
180 if (!pulseprobe->context ||
181 !PA_CONTEXT_IS_GOOD (pa_context_get_state (pulseprobe->context))) {
182 const gchar *err_str =
183 pulseprobe->context ?
184 pa_strerror (pa_context_errno (pulseprobe->context)) : NULL;
186 GST_ELEMENT_ERROR ((pulseprobe), RESOURCE, FAILED,
187 ("Disconnected: %s", err_str), (NULL));
195 gst_pulseprobe_enumerate (GstPulseProbe * c)
197 pa_operation *o = NULL;
199 GST_DEBUG_OBJECT (c->object, "probe enumerate");
201 pa_threaded_mainloop_lock (c->mainloop);
203 if (c->enumerate_sinks) {
207 pa_context_get_sink_info_list (c->context,
208 gst_pulseprobe_sink_info_cb, c))) {
209 GST_WARNING_OBJECT (c->object, "Failed to get sink info: %s",
210 pa_strerror (pa_context_errno (c->context)));
211 goto unlock_and_fail;
214 c->operation_success = FALSE;
216 while (pa_operation_get_state (o) == PA_OPERATION_RUNNING) {
218 if (gst_pulseprobe_is_dead (c))
219 goto unlock_and_fail;
221 pa_threaded_mainloop_wait (c->mainloop);
224 if (!c->operation_success) {
225 GST_WARNING_OBJECT (c->object, "Failed to get sink info: %s",
226 pa_strerror (pa_context_errno (c->context)));
227 goto unlock_and_fail;
230 pa_operation_unref (o);
234 if (c->enumerate_sources) {
235 /* Get source info */
238 pa_context_get_source_info_list (c->context,
239 gst_pulseprobe_source_info_cb, c))) {
240 GST_WARNING_OBJECT (c->object, "Failed to get source info: %s",
241 pa_strerror (pa_context_errno (c->context)));
242 goto unlock_and_fail;
245 c->operation_success = FALSE;
246 while (pa_operation_get_state (o) == PA_OPERATION_RUNNING) {
248 if (gst_pulseprobe_is_dead (c))
249 goto unlock_and_fail;
251 pa_threaded_mainloop_wait (c->mainloop);
254 if (!c->operation_success) {
255 GST_WARNING_OBJECT (c->object, "Failed to get sink info: %s",
256 pa_strerror (pa_context_errno (c->context)));
257 goto unlock_and_fail;
260 pa_operation_unref (o);
264 c->devices_valid = TRUE;
266 pa_threaded_mainloop_unlock (c->mainloop);
273 pa_operation_unref (o);
275 pa_threaded_mainloop_unlock (c->mainloop);
281 gst_pulseprobe_close (GstPulseProbe * c)
285 GST_DEBUG_OBJECT (c->object, "probe close");
288 pa_threaded_mainloop_stop (c->mainloop);
291 pa_context_disconnect (c->context);
292 pa_context_set_state_callback (c->context, NULL, NULL);
293 pa_context_unref (c->context);
298 pa_threaded_mainloop_free (c->mainloop);
304 gst_pulseprobe_new (GObject * object, GObjectClass * klass,
305 guint prop_id, const gchar * server, gboolean sinks, gboolean sources)
307 GstPulseProbe *c = NULL;
309 c = g_new (GstPulseProbe, 1);
310 c->object = object; /* We don't inc the ref counter here to avoid a ref loop */
311 c->server = g_strdup (server);
312 c->enumerate_sinks = sinks;
313 c->enumerate_sources = sources;
318 c->prop_id = prop_id;
320 g_list_append (NULL, g_object_class_find_property (klass, "device"));
323 c->devices_valid = FALSE;
325 c->operation_success = FALSE;
331 gst_pulseprobe_free (GstPulseProbe * c)
335 gst_pulseprobe_close (c);
337 g_list_free (c->properties);
340 g_list_foreach (c->devices, (GFunc) g_free, NULL);
341 g_list_free (c->devices);
347 gst_pulseprobe_get_properties (GstPulseProbe * c)
349 return c->properties;
353 gst_pulseprobe_needs_probe (GstPulseProbe * c, guint prop_id,
354 const GParamSpec * pspec)
357 if (prop_id == c->prop_id)
358 return !c->devices_valid;
360 G_OBJECT_WARN_INVALID_PROPERTY_ID (c->object, prop_id, pspec);
365 gst_pulseprobe_probe_property (GstPulseProbe * c, guint prop_id,
366 const GParamSpec * pspec)
369 if (prop_id != c->prop_id) {
370 G_OBJECT_WARN_INVALID_PROPERTY_ID (c->object, prop_id, pspec);
374 if (gst_pulseprobe_open (c)) {
375 gst_pulseprobe_enumerate (c);
376 gst_pulseprobe_close (c);
382 gst_pulseprobe_get_values (GstPulseProbe * c, guint prop_id,
383 const GParamSpec * pspec)
391 if (prop_id != c->prop_id) {
392 G_OBJECT_WARN_INVALID_PROPERTY_ID (c->object, prop_id, pspec);
396 if (!c->devices_valid)
399 array = g_value_array_new (g_list_length (c->devices));
400 g_value_init (&value, G_TYPE_STRING);
401 for (item = c->devices; item != NULL; item = item->next) {
402 GST_WARNING_OBJECT (c->object, "device found: %s",
403 (const gchar *) item->data);
404 g_value_set_string (&value, (const gchar *) item->data);
405 g_value_array_append (array, &value);
407 g_value_unset (&value);
414 gst_pulseprobe_set_server (GstPulseProbe * c, const gchar * server)
418 gst_pulseprobe_invalidate (c);
421 c->server = g_strdup (server);