From f578f49e3265bfed6bd7e37391e66506f9a4bc91 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Thu, 11 May 2006 20:14:38 +0000 Subject: * implement device probing for GstPolypMixer git-svn-id: file:///home/lennart/svn/public/gst-pulse/trunk@25 bb39ca4e-bce3-0310-b5d4-eea78a553289 --- src/Makefile.am | 3 +- src/polypmixer.c | 60 +++++++++- src/polypmixer.h | 5 +- src/polypmixerctrl.h | 16 +-- src/polypprobe.c | 322 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/polypprobe.h | 115 ++++++++++++++++++ 6 files changed, 501 insertions(+), 20 deletions(-) create mode 100644 src/polypprobe.c create mode 100644 src/polypprobe.h diff --git a/src/Makefile.am b/src/Makefile.am index 1010c5f..6449aba 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -26,7 +26,8 @@ libgstpolyp_la_SOURCES = \ polyputil.c polyputil.h \ polypmixer.c polypmixer.h \ polypmixerctrl.c polypmixerctrl.h \ - polypmixertrack.c polypmixertrack.h + polypmixertrack.c polypmixertrack.h \ + polypprobe.c polypprobe.h libgstpolyp_la_CFLAGS = $(GST_CFLAGS) $(POLYP_CFLAGS) libgstpolyp_la_LIBADD = $(POLYP_LIBS) $(GST_LIBS) -lgstaudio-0.10 diff --git a/src/polypmixer.c b/src/polypmixer.c index ef5a846..0b18325 100644 --- a/src/polypmixer.c +++ b/src/polypmixer.c @@ -37,14 +37,55 @@ enum { GST_DEBUG_CATEGORY_EXTERN(polyp_debug); #define GST_CAT_DEFAULT polyp_debug -GST_IMPLEMENT_POLYPMIXER_CTRL_METHODS(GstPolypMixer, gst_polypmixer) -GST_BOILERPLATE_WITH_INTERFACE(GstPolypMixer, gst_polypmixer, GstElement, GST_TYPE_ELEMENT, GstMixer, GST_TYPE_MIXER, gst_polypmixer) - static void gst_polypmixer_set_property(GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec); static void gst_polypmixer_get_property(GObject *object, guint prop_id, GValue *value, GParamSpec *pspec); static void gst_polypmixer_finalize(GObject *object); static GstStateChangeReturn gst_polypmixer_change_state(GstElement *element, GstStateChange transition); +static void gst_polypmixer_init_interfaces(GType type); + +GST_IMPLEMENT_POLYPMIXER_CTRL_METHODS(GstPolypMixer, gst_polypmixer) +GST_IMPLEMENT_POLYPPROBE_METHODS(GstPolypMixer, gst_polypmixer) +GST_BOILERPLATE_FULL(GstPolypMixer, gst_polypmixer, GstElement, GST_TYPE_ELEMENT, gst_polypmixer_init_interfaces) + +static gboolean gst_polypmixer_interface_supported(GstImplementsInterface* iface, GType interface_type) { + GstPolypMixer *this = GST_POLYPMIXER(iface); + + if (interface_type == GST_TYPE_MIXER && this->mixer) + return TRUE; + + if (interface_type == GST_TYPE_PROPERTY_PROBE && this->probe) + return TRUE; + + return FALSE; +} + +static void gst_polypmixer_implements_interface_init(GstImplementsInterfaceClass* klass) { + klass->supported = gst_polypmixer_interface_supported; +} + +static void gst_polypmixer_init_interfaces(GType type) { + static const GInterfaceInfo implements_iface_info = { + (GInterfaceInitFunc) gst_polypmixer_implements_interface_init, + NULL, + NULL, + }; + static const GInterfaceInfo mixer_iface_info = { + (GInterfaceInitFunc) gst_polypmixer_mixer_interface_init, + NULL, + NULL, + }; + static const GInterfaceInfo probe_iface_info = { + (GInterfaceInitFunc) gst_polypmixer_property_probe_interface_init, + NULL, + NULL, + }; + + g_type_add_interface_static(type, GST_TYPE_IMPLEMENTS_INTERFACE, &implements_iface_info); + g_type_add_interface_static(type, GST_TYPE_MIXER, &mixer_iface_info); + g_type_add_interface_static(type, GST_TYPE_PROPERTY_PROBE, &probe_iface_info); +} + static void gst_polypmixer_base_init(gpointer g_class) { static const GstElementDetails details = @@ -87,6 +128,8 @@ static void gst_polypmixer_init(GstPolypMixer *this, GstPolypMixerClass *g_class this->mixer = NULL; this->server = NULL; this->device = NULL; + + this->probe = gst_polypprobe_new(G_OBJECT_GET_CLASS(this), PROP_DEVICE, this->device, TRUE, TRUE); } static void gst_polypmixer_finalize(GObject *object) { @@ -99,6 +142,11 @@ static void gst_polypmixer_finalize(GObject *object) { gst_polypmixer_ctrl_free(this->mixer); this->mixer = NULL; } + + if (this->probe) { + gst_polypprobe_free(this->probe); + this->probe = NULL; + } G_OBJECT_CLASS(parent_class)->finalize(object); } @@ -120,6 +168,10 @@ static void gst_polypmixer_set_property( case PROP_DEVICE: g_free(this->device); this->device = g_value_dup_string(value); + + if (this->probe) + gst_polypprobe_set_server(this->probe, this->device); + break; default: @@ -149,7 +201,7 @@ static void gst_polypmixer_get_property( case PROP_DEVICE_NAME: if (this->mixer) { - char *t = g_strdup_printf("[%s] %s", this->mixer->name, this->mixer->description); + char *t = g_strdup_printf("%s - %s [%s]", this->mixer->type == GST_POLYPMIXER_SINK ? "Playback" : "Capture", this->mixer->description, this->mixer->name); g_value_set_string(value, t); g_free(t); } else diff --git a/src/polypmixer.h b/src/polypmixer.h index 63d0609..8fb951e 100644 --- a/src/polypmixer.h +++ b/src/polypmixer.h @@ -28,6 +28,7 @@ #include #include "polypmixerctrl.h" +#include "polypprobe.h" G_BEGIN_DECLS @@ -48,8 +49,10 @@ typedef struct _GstPolypMixerClass GstPolypMixerClass; struct _GstPolypMixer { GstElement parent; - GstPolypMixerCtrl *mixer; gchar *server, *device; + + GstPolypMixerCtrl *mixer; + GstPolypProbe *probe; }; struct _GstPolypMixerClass { diff --git a/src/polypmixerctrl.h b/src/polypmixerctrl.h index 18313c6..4833c6e 100644 --- a/src/polypmixerctrl.h +++ b/src/polypmixerctrl.h @@ -56,7 +56,7 @@ struct _GstPolypMixerCtrl { }; GstPolypMixerCtrl* gst_polypmixer_ctrl_new(const gchar *server, const gchar *device); -void gst_polypmixer_ctrl_free(GstPolypMixerCtrl *mixer); +void gst_polypmixer_ctrl_free(GstPolypMixerCtrl*mixer); const GList* gst_polypmixer_ctrl_list_tracks(GstPolypMixerCtrl *mixer); void gst_polypmixer_ctrl_set_volume(GstPolypMixerCtrl *mixer, GstMixerTrack *track, gint *volumes); @@ -65,14 +65,6 @@ void gst_polypmixer_ctrl_set_mute(GstPolypMixerCtrl *mixer, GstMixerTrack *track void gst_polypmixer_ctrl_set_record(GstPolypMixerCtrl *mixer, GstMixerTrack *track, gboolean record); #define GST_IMPLEMENT_POLYPMIXER_CTRL_METHODS(Type, interface_as_function) \ -static gboolean \ -interface_as_function ## _supported (Type *this, GType iface_type) \ -{ \ - g_assert (iface_type == GST_TYPE_MIXER); \ - \ - return (this->mixer != NULL); \ -} \ - \ static const GList* \ interface_as_function ## _list_tracks (GstMixer * mixer) \ { \ @@ -105,7 +97,6 @@ interface_as_function ## _get_volume (GstMixer * mixer, GstMixerTrack * track, \ gst_polypmixer_ctrl_get_volume (this->mixer, track, volumes); \ } \ - \ static void \ interface_as_function ## _set_record (GstMixer * mixer, GstMixerTrack * track, \ gboolean record) \ @@ -117,7 +108,6 @@ interface_as_function ## _set_record (GstMixer * mixer, GstMixerTrack * track, \ gst_polypmixer_ctrl_set_record (this->mixer, track, record); \ } \ - \ static void \ interface_as_function ## _set_mute (GstMixer * mixer, GstMixerTrack * track, \ gboolean mute) \ @@ -129,13 +119,11 @@ interface_as_function ## _set_mute (GstMixer * mixer, GstMixerTrack * track, \ gst_polypmixer_ctrl_set_mute (this->mixer, track, mute); \ } \ - \ static void \ -interface_as_function ## _interface_init (GstMixerClass * klass) \ +interface_as_function ## _mixer_interface_init (GstMixerClass * klass) \ { \ GST_MIXER_TYPE (klass) = GST_MIXER_HARDWARE; \ \ - /* set up the interface hooks */ \ klass->list_tracks = interface_as_function ## _list_tracks; \ klass->set_volume = interface_as_function ## _set_volume; \ klass->get_volume = interface_as_function ## _get_volume; \ diff --git a/src/polypprobe.c b/src/polypprobe.c new file mode 100644 index 0000000..cd057e0 --- /dev/null +++ b/src/polypprobe.c @@ -0,0 +1,322 @@ +/* $Id$ */ + +/*** + This file is part of gst-polyp. + + gst-polyp is free software; you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as + published by the Free Software Foundation; either version 2.1 of the + License, or (at your option) any later version. + + gst-polyp is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with gst-polyp; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 + USA. +***/ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "polypprobe.h" +#include "polyputil.h" + +GST_DEBUG_CATEGORY_EXTERN(polyp_debug); +#define GST_CAT_DEFAULT polyp_debug + +static void gst_polypprobe_context_state_cb(pa_context *context, void *userdata) { + GstPolypProbe *c = (GstPolypProbe*) userdata; + + /* Called from the background thread! */ + + switch (pa_context_get_state(context)) { + case PA_CONTEXT_READY: + case PA_CONTEXT_TERMINATED: + case PA_CONTEXT_FAILED: + pa_threaded_mainloop_signal(c->mainloop, 0); + break; + + case PA_CONTEXT_UNCONNECTED: + case PA_CONTEXT_CONNECTING: + case PA_CONTEXT_AUTHORIZING: + case PA_CONTEXT_SETTING_NAME: + break; + } +} + +static void gst_polypprobe_sink_info_cb(pa_context *context, const pa_sink_info *i, int eol, void *userdata) { + GstPolypProbe *c = (GstPolypProbe*) userdata; + + /* Called from the background thread! */ + + if (eol || !i) { + c->operation_success = eol > 0; + pa_threaded_mainloop_signal(c->mainloop, 0); + } + + if (i) + c->devices = g_list_append(c->devices, g_strdup(i->name)); + +} + +static void gst_polypprobe_source_info_cb(pa_context *context, const pa_source_info *i, int eol, void *userdata) { + GstPolypProbe *c = (GstPolypProbe*) userdata; + + /* Called from the background thread! */ + + if (eol || !i) { + c->operation_success = eol > 0; + pa_threaded_mainloop_signal(c->mainloop, 0); + } + + if (i) + c->devices = g_list_append(c->devices, g_strdup(i->name)); +} + +static void gst_polypprobe_invalidate(GstPolypProbe *c) { + g_list_foreach(c->devices, (GFunc) g_free, NULL); + g_list_free(c->devices); + c->devices = NULL; + c->devices_valid = 0; +} + +static gboolean gst_polypprobe_open(GstPolypProbe *c) { + int e; + gchar *name = gst_polyp_client_name(); + + g_assert(c); + + c->mainloop = pa_threaded_mainloop_new(); + g_assert(c->mainloop); + + e = pa_threaded_mainloop_start(c->mainloop); + g_assert(e == 0); + + pa_threaded_mainloop_lock(c->mainloop); + + if (!(c->context = pa_context_new(pa_threaded_mainloop_get_api(c->mainloop), name))) { + GST_WARNING("Failed to create context"); + goto unlock_and_fail; + } + + pa_context_set_state_callback(c->context, gst_polypprobe_context_state_cb, c); + + if (pa_context_connect(c->context, c->server, 0, NULL) < 0) { + GST_WARNING("Failed to connect context: %s", pa_strerror(pa_context_errno(c->context))); + goto unlock_and_fail; + } + + /* Wait until the context is ready */ + pa_threaded_mainloop_wait(c->mainloop); + + if (pa_context_get_state(c->context) != PA_CONTEXT_READY) { + GST_WARNING("Failed to connect context: %s", pa_strerror(pa_context_errno(c->context))); + goto unlock_and_fail; + } + + pa_threaded_mainloop_unlock(c->mainloop); + g_free(name); + + gst_polypprobe_invalidate(c); + + return TRUE; + +unlock_and_fail: + + if (c->mainloop) + pa_threaded_mainloop_unlock(c->mainloop); + + g_free(name); + + return FALSE; +} + +#define CHECK_DEAD_GOTO(c, label) do { \ +if (!(c)->context || pa_context_get_state((c)->context) != PA_CONTEXT_READY) { \ + GST_WARNING("Not connected: %s", (c)->context ? pa_strerror(pa_context_errno((c)->context)) : "NULL"); \ + goto label; \ +} \ +} while(0); + +static gboolean gst_polypprobe_enumerate(GstPolypProbe *c) { + pa_operation *o = NULL; + + pa_threaded_mainloop_lock(c->mainloop); + + if (c->enumerate_sinks) { + /* Get sink info */ + + if (!(o = pa_context_get_sink_info_list(c->context, gst_polypprobe_sink_info_cb, c))) { + GST_WARNING("Failed to get sink info: %s", pa_strerror(pa_context_errno(c->context))); + goto unlock_and_fail; + } + + c->operation_success = 0; + while (pa_operation_get_state(o) != PA_OPERATION_DONE) { + pa_threaded_mainloop_wait(c->mainloop); + CHECK_DEAD_GOTO(c, unlock_and_fail); + } + + if (!c->operation_success) { + GST_WARNING("Failed to get sink info: %s", pa_strerror(pa_context_errno(c->context))); + goto unlock_and_fail; + } + + pa_operation_unref(o); + o = NULL; + } + + if (c->enumerate_sources) { + /* Get source info */ + + if (!(o = pa_context_get_source_info_list(c->context, gst_polypprobe_source_info_cb, c))) { + GST_WARNING("Failed to get source info: %s", pa_strerror(pa_context_errno(c->context))); + goto unlock_and_fail; + } + + c->operation_success = 0; + while (pa_operation_get_state(o) != PA_OPERATION_DONE) { + pa_threaded_mainloop_wait(c->mainloop); + CHECK_DEAD_GOTO(c, unlock_and_fail); + } + + if (!c->operation_success) { + GST_WARNING("Failed to get sink info: %s", pa_strerror(pa_context_errno(c->context))); + goto unlock_and_fail; + } + + pa_operation_unref(o); + o = NULL; + } + + c->devices_valid = 1; + + pa_threaded_mainloop_unlock(c->mainloop); + + return TRUE; + +unlock_and_fail: + + if (o) + pa_operation_unref(o); + + pa_threaded_mainloop_unlock(c->mainloop); + + return FALSE; +} + +static void gst_polypprobe_close(GstPolypProbe *c) { + g_assert(c); + + if (c->mainloop) + pa_threaded_mainloop_stop(c->mainloop); + + if (c->context) { + pa_context_disconnect(c->context); + pa_context_unref(c->context); + c->context = NULL; + } + + if (c->mainloop) { + pa_threaded_mainloop_free(c->mainloop); + c->mainloop = NULL; + } +} + +GstPolypProbe* gst_polypprobe_new(GObjectClass *klass, guint prop_id, const gchar *server, gboolean sinks, gboolean sources) { + GstPolypProbe *c = NULL; + + c = g_new(GstPolypProbe, 1); + c->server = g_strdup(server); + c->enumerate_sinks = sinks; + c->enumerate_sources = sources; + + c->mainloop = NULL; + c->context = NULL; + + c->prop_id = prop_id; + c->properties = g_list_append(NULL, g_object_class_find_property(klass, "device")); + c->devices = NULL; + c->devices_valid = 0; + + return c; +} + +void gst_polypprobe_free(GstPolypProbe* c) { + g_assert(c); + + gst_polypprobe_close(c); + + g_list_free(c->properties); + g_free(c->server); + + g_list_foreach(c->devices, (GFunc) g_free, NULL); + g_list_free(c->devices); + + g_free(c); +} + +const GList* gst_polypprobe_get_properties(GstPolypProbe *c) { + return c->properties; +} + +gboolean gst_polypprobe_needs_probe(GstPolypProbe *c, guint prop_id, const GParamSpec *pspec) { + + if (prop_id == c->prop_id) + return !c->devices_valid; + + G_OBJECT_WARN_INVALID_PROPERTY_ID(c, prop_id, pspec); + return FALSE; +} + +void gst_polypprobe_probe_property(GstPolypProbe *c, guint prop_id, const GParamSpec *pspec) { + + if (prop_id != c->prop_id) { + G_OBJECT_WARN_INVALID_PROPERTY_ID(c, prop_id, pspec); + return; + } + + if (gst_polypprobe_open(c)) { + gst_polypprobe_enumerate(c); + gst_polypprobe_close(c); + } +} + +GValueArray *gst_polypprobe_get_values(GstPolypProbe *c, guint prop_id, const GParamSpec *pspec) { + GValueArray *array; + GValue value = { 0 }; + GList *item; + + if (prop_id != c->prop_id) { + G_OBJECT_WARN_INVALID_PROPERTY_ID(c, prop_id, pspec); + return NULL; + } + + if (!c->devices_valid) + return NULL; + + array = g_value_array_new(g_list_length(c->devices)); + g_value_init(&value, G_TYPE_STRING); + for (item = c->devices; item != NULL; item = item->next) { + GST_WARNING("device found: %s", (const gchar *) item->data); + g_value_set_string(&value, (const gchar *) item->data); + g_value_array_append(array, &value); + } + g_value_unset (&value); + + return array; +} + +void gst_polypprobe_set_server(GstPolypProbe *c, const gchar *server) { + g_assert(c); + + gst_polypprobe_invalidate(c); + + g_free(c->server); + c->server = g_strdup(server); +} diff --git a/src/polypprobe.h b/src/polypprobe.h new file mode 100644 index 0000000..face7cb --- /dev/null +++ b/src/polypprobe.h @@ -0,0 +1,115 @@ +#ifndef __GST_POLYPPROBE_H__ +#define __GST_POLYPPROBE_H__ + +/* $Id$ */ + +/*** + This file is part of gst-polyp. + + gst-polyp is free software; you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as + published by the Free Software Foundation; either version 2.1 of the + License, or (at your option) any later version. + + gst-polyp is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with gst-polyp; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 + USA. +***/ + +#include + +G_BEGIN_DECLS + +#include +#include +#include + +typedef struct _GstPolypProbe GstPolypProbe; + +struct _GstPolypProbe { + gchar *server; + GList *devices; + int devices_valid; + + pa_threaded_mainloop *mainloop; + pa_context *context; + + GList *properties; + guint prop_id; + + int enumerate_sinks, enumerate_sources; + int operation_success; +}; + +GstPolypProbe* gst_polypprobe_new(GObjectClass *klass, guint prop_id, const gchar *server, gboolean sinks, gboolean sources); +void gst_polypprobe_free(GstPolypProbe* probe); + +const GList* gst_polypprobe_get_properties(GstPolypProbe *probe); +gboolean gst_polypprobe_needs_probe(GstPolypProbe *probe, guint prop_id, const GParamSpec *pspec); +void gst_polypprobe_probe_property(GstPolypProbe *probe, guint prop_id, const GParamSpec *pspec); +GValueArray *gst_polypprobe_get_values(GstPolypProbe *probe, guint prop_id, const GParamSpec *pspec); + +void gst_polypprobe_set_server(GstPolypProbe *c, const gchar *server); + +#define GST_IMPLEMENT_POLYPPROBE_METHODS(Type, interface_as_function) \ +static const GList* \ +interface_as_function ## _get_properties(GstPropertyProbe * probe) \ +{ \ + Type *this = (Type*) probe; \ + \ + g_return_val_if_fail(this != NULL, NULL); \ + g_return_val_if_fail(this->probe != NULL, NULL); \ + \ + return gst_polypprobe_get_properties(this->probe); \ +} \ +static gboolean \ +interface_as_function ## _needs_probe(GstPropertyProbe *probe, guint prop_id, \ + const GParamSpec *pspec) \ +{ \ + Type *this = (Type*) probe; \ + \ + g_return_val_if_fail(this != NULL, FALSE); \ + g_return_val_if_fail(this->probe != NULL, FALSE); \ + \ + return gst_polypprobe_needs_probe(this->probe, prop_id, pspec); \ +} \ +static void \ +interface_as_function ## _probe_property(GstPropertyProbe *probe, \ + guint prop_id, const GParamSpec *pspec) \ +{ \ + Type *this = (Type*) probe; \ + \ + g_return_if_fail(this != NULL); \ + g_return_if_fail(this->probe != NULL); \ + \ + gst_polypprobe_probe_property(this->probe, prop_id, pspec); \ +} \ +static GValueArray* \ +interface_as_function ## _get_values(GstPropertyProbe *probe, guint prop_id, \ + const GParamSpec *pspec) \ +{ \ + Type *this = (Type*) probe; \ + \ + g_return_val_if_fail(this != NULL, NULL); \ + g_return_val_if_fail(this->probe != NULL, NULL); \ + \ + return gst_polypprobe_get_values(this->probe, prop_id, pspec); \ +} \ +static void \ +interface_as_function ## _property_probe_interface_init(GstPropertyProbeInterface *iface)\ +{ \ + iface->get_properties = interface_as_function ## _get_properties; \ + iface->needs_probe = interface_as_function ## _needs_probe; \ + iface->probe_property = interface_as_function ## _probe_property; \ + iface->get_values = interface_as_function ## _get_values; \ +} + +G_END_DECLS + +#endif -- cgit