* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
+#include "config.h"
+#include "empathy-audio-src.h"
-#include <stdio.h>
-#include <stdlib.h>
-
-#include <pulse/pulseaudio.h>
-#include <pulse/glib-mainloop.h>
-
-#include <libempathy/empathy-utils.h>
+#include <tp-account-widgets/tpaw-utils.h>
-#include "empathy-audio-src.h"
+#include <gst/audio/streamvolume.h>
-#include "src-marshal.h"
+#include "empathy-audio-utils.h"
+#include "empathy-mic-monitor.h"
+#include "empathy-utils.h"
#define DEBUG_FLAG EMPATHY_DEBUG_VOIP
-#include <libempathy/empathy-debug.h>
+#include "empathy-debug.h"
G_DEFINE_TYPE(EmpathyGstAudioSrc, empathy_audio_src, GST_TYPE_BIN)
-/* signal enum */
-enum
-{
- PEAK_LEVEL_CHANGED,
- RMS_LEVEL_CHANGED,
- MICROPHONE_ADDED,
- MICROPHONE_REMOVED,
- LAST_SIGNAL
-};
-
-static guint signals[LAST_SIGNAL] = {0};
-
enum {
PROP_VOLUME = 1,
- PROP_RMS_LEVEL,
- PROP_PEAK_LEVEL,
+ PROP_MUTE,
PROP_MICROPHONE,
};
/* private structure */
-typedef struct _EmpathyGstAudioSrcPrivate EmpathyGstAudioSrcPrivate;
-
struct _EmpathyGstAudioSrcPrivate
{
gboolean dispose_has_run;
GstElement *src;
- GstElement *volume;
- GstElement *level;
+ GstElement *volume_element;
- pa_glib_mainloop *loop;
- pa_context *context;
- GQueue *operations;
+ EmpathyMicMonitor *mic_monitor;
/* 0 if not known yet */
guint source_output_idx;
/* G_MAXUINT if not known yet */
guint source_idx;
- gdouble peak_level;
- gdouble rms_level;
+ gdouble volume;
+ gboolean mute;
+ gboolean have_stream_volume;
- GMutex *lock;
- guint idle_id;
+ GMutex lock;
+ guint volume_idle_id;
};
#define EMPATHY_GST_AUDIO_SRC_GET_PRIVATE(o) \
(G_TYPE_INSTANCE_GET_PRIVATE ((o), EMPATHY_TYPE_GST_AUDIO_SRC, \
EmpathyGstAudioSrcPrivate))
-static gboolean
-empathy_audio_src_supports_changing_mic (EmpathyGstAudioSrc *self)
-{
- EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (self);
- GObjectClass *object_class;
- object_class = G_OBJECT_GET_CLASS (priv->src);
-
- return (g_object_class_find_property (object_class,
- "source-output-index") != NULL);
-}
-
-typedef void (*OperationFunc) (EmpathyGstAudioSrc *, GSimpleAsyncResult *);
+static gboolean
+empathy_audio_src_volume_changed (GObject *object,
+ GParamSpec *pspec,
+ gpointer user_data);
-typedef struct
+static void
+empathy_audio_set_hw_mute (EmpathyGstAudioSrc *self, gboolean mute)
{
- OperationFunc func;
- GSimpleAsyncResult *result;
-} Operation;
+ if (mute == self->priv->mute)
+ return;
-static Operation *
-operation_new (OperationFunc func,
- GSimpleAsyncResult *result)
-{
- Operation *o = g_slice_new0 (Operation);
+ if (self->priv->have_stream_volume)
+ g_object_set (self->priv->src, "mute", mute, NULL);
- o->func = func;
- o->result = result;
+ /* Belt and braces: If for some reason the underlying src doesn't mute
+ * correctly or doesn't update us when it unmutes correctly enforce it using
+ * our own volume element. Our UI can in no circumstances be made to think
+ * the input is muted while it's not */
+ g_object_set (self->priv->volume_element, "mute", mute, NULL);
- return o;
+ self->priv->mute = mute;
}
-static void
-operation_free (Operation *o,
- gboolean cancelled)
+static gboolean
+empathy_audio_src_get_hw_mute (EmpathyGstAudioSrc *self)
{
- if (cancelled)
- {
- g_simple_async_result_set_error (o->result,
- G_IO_ERROR, G_IO_ERROR_CANCELLED,
- "The audio source was disposed");
- g_simple_async_result_complete (o->result);
- g_object_unref (o->result);
- }
+ gboolean result;
+ g_object_get (self->priv->src, "mute", &result, NULL);
- g_slice_free (Operation, o);
+ return result;
}
static void
-operation_get_microphones_free (gpointer data)
+empathy_audio_src_set_hw_volume (EmpathyGstAudioSrc *self,
+ gdouble volume)
{
- GQueue *queue = data;
- GList *l;
-
- for (l = queue->head; l != NULL; l = l->next)
- {
- EmpathyAudioSrcMicrophone *mic = l->data;
-
- g_free (mic->name);
- g_free (mic->description);
- g_slice_free (EmpathyAudioSrcMicrophone, mic);
- }
+ if (volume == self->priv->volume)
+ return;
- g_queue_free (queue);
+ if (self->priv->have_stream_volume)
+ g_object_set (self->priv->src, "volume", volume, NULL);
+ self->priv->volume = volume;
}
-static void
-operation_get_microphones_cb (pa_context *context,
- const pa_source_info *info,
- int eol,
- void *userdata)
+static gdouble
+empathy_audio_src_get_hw_volume (EmpathyGstAudioSrc *self)
{
- GSimpleAsyncResult *result = userdata;
- EmpathyAudioSrcMicrophone *mic;
- GQueue *queue;
-
- if (eol)
- {
- g_simple_async_result_complete (result);
- g_object_unref (result);
- return;
- }
-
- mic = g_slice_new0 (EmpathyAudioSrcMicrophone);
- mic->index = info->index;
- mic->name = g_strdup (info->name);
- mic->description = g_strdup (info->description);
- mic->is_monitor = (info->monitor_of_sink != PA_INVALID_INDEX);
+ gdouble result;
+ g_object_get (self->priv->src, "volume", &result, NULL);
- /* add it to the queue */
- queue = g_simple_async_result_get_op_res_gpointer (result);
- g_queue_push_tail (queue, mic);
+ return result;
}
-static void
-operation_get_microphones (EmpathyGstAudioSrc *self,
- GSimpleAsyncResult *result)
+
+gboolean
+empathy_audio_src_supports_changing_mic (EmpathyGstAudioSrc *self)
{
EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (self);
+ GObjectClass *object_class;
- g_assert_cmpuint (pa_context_get_state (priv->context), ==, PA_CONTEXT_READY);
-
- g_simple_async_result_set_op_res_gpointer (result, g_queue_new (),
- operation_get_microphones_free);
+ object_class = G_OBJECT_GET_CLASS (priv->src);
- pa_context_get_source_info_list (priv->context,
- operation_get_microphones_cb, result);
+ return (g_object_class_find_property (object_class,
+ "source-output-index") != NULL);
}
-static void
-operation_change_microphone_cb (pa_context *context,
- int success,
- void *userdata)
+static guint
+empathy_audio_src_get_mic_index (EmpathyGstAudioSrc *self)
{
- GSimpleAsyncResult *result = userdata;
+ EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (self);
+ guint audio_src_idx = PA_INVALID_INDEX;
- if (!success)
- {
- g_simple_async_result_set_error (result, G_IO_ERROR, G_IO_ERROR_FAILED,
- "Failed to change microphone. Reason unknown.");
- }
+ if (empathy_audio_src_supports_changing_mic (self))
+ g_object_get (priv->src,
+ "source-output-index", &audio_src_idx,
+ NULL);
- g_simple_async_result_complete (result);
- g_object_unref (result);
+ return audio_src_idx;
}
static void
-operation_change_microphone (EmpathyGstAudioSrc *self,
- GSimpleAsyncResult *result)
+empathy_audio_src_microphone_changed_cb (EmpathyMicMonitor *monitor,
+ guint source_output_idx,
+ guint source_idx,
+ gpointer user_data)
{
+ EmpathyGstAudioSrc *self = user_data;
EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (self);
- guint source_output_idx, microphone;
+ guint audio_src_idx;
- g_object_get (priv->src, "source-output-index", &source_output_idx, NULL);
+ audio_src_idx = empathy_audio_src_get_mic_index (self);
- g_assert_cmpuint (pa_context_get_state (priv->context), ==, PA_CONTEXT_READY);
- g_assert_cmpuint (source_output_idx, !=, PA_INVALID_INDEX);
+ if (source_output_idx == PA_INVALID_INDEX
+ || source_output_idx != audio_src_idx)
+ return;
- microphone = GPOINTER_TO_UINT (
- g_simple_async_result_get_op_res_gpointer (result));
+ if (priv->source_idx == source_idx)
+ return;
- pa_context_move_source_output_by_index (priv->context, source_output_idx, microphone,
- operation_change_microphone_cb, result);
+ priv->source_idx = source_idx;
+ g_object_notify (G_OBJECT (self), "microphone");
}
static void
-operations_run (EmpathyGstAudioSrc *self)
+empathy_audio_src_get_current_mic_cb (GObject *source_object,
+ GAsyncResult *result,
+ gpointer user_data)
{
+ EmpathyMicMonitor *monitor = EMPATHY_MIC_MONITOR (source_object);
+ EmpathyGstAudioSrc *self = user_data;
EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (self);
- pa_context_state_t state = pa_context_get_state (priv->context);
- GList *l;
+ guint source_idx;
+ GError *error = NULL;
- if (state != PA_CONTEXT_READY)
- return;
+ source_idx = empathy_mic_monitor_get_current_mic_finish (monitor, result, &error);
- for (l = priv->operations->head; l != NULL; l = l->next)
+ if (error != NULL)
{
- Operation *o = l->data;
-
- o->func (self, o->result);
-
- operation_free (o, FALSE);
+ DEBUG ("Failed to get current mic: %s", error->message);
+ g_clear_error (&error);
+ return;
}
- g_queue_clear (priv->operations);
+ if (priv->source_idx == source_idx)
+ return;
+
+ priv->source_idx = source_idx;
+ g_object_notify (G_OBJECT (self), "microphone");
}
static void
-empathy_audio_src_source_output_info_cb (pa_context *context,
- const pa_source_output_info *info,
- int eol,
- void *userdata)
+empathy_audio_src_source_output_index_notify (GObject *object,
+ GParamSpec *pspec,
+ EmpathyGstAudioSrc *self)
{
- EmpathyGstAudioSrc *self = userdata;
EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (self);
+ guint source_output_idx;
- if (eol)
- return;
-
- /* There should only be one call here. */
+ source_output_idx = empathy_audio_src_get_mic_index (self);
- if (priv->source_idx == info->source)
+ if (source_output_idx == PA_INVALID_INDEX)
return;
- priv->source_idx = info->source;
- g_object_notify (G_OBJECT (self), "microphone");
-}
-
-static void
-empathy_audio_src_source_info_cb (pa_context *context,
- const pa_source_info *info,
- int eol,
- void *userdata)
-{
- EmpathyGstAudioSrc *self = userdata;
- gboolean is_monitor;
-
- if (eol)
+ if (priv->source_output_idx == source_output_idx)
return;
- is_monitor = (info->monitor_of_sink != PA_INVALID_INDEX);
+ /* It's actually changed. */
+ priv->source_output_idx = source_output_idx;
- g_signal_emit (self, signals[MICROPHONE_ADDED], 0,
- info->index, info->name, info->description, is_monitor);
+ empathy_mic_monitor_get_current_mic_async (priv->mic_monitor,
+ source_output_idx, empathy_audio_src_get_current_mic_cb, self);
}
-static void
-empathy_audio_src_pa_event_cb (pa_context *context,
- pa_subscription_event_type_t type,
- uint32_t idx,
- void *userdata)
+static GstElement *
+create_src (void)
{
- EmpathyGstAudioSrc *self = userdata;
- EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (self);
+ GstElement *src;
+ const gchar *description;
- if ((type & PA_SUBSCRIPTION_EVENT_FACILITY_MASK) == PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT
- && (type & PA_SUBSCRIPTION_EVENT_TYPE_MASK) == PA_SUBSCRIPTION_EVENT_CHANGE
- && idx == priv->source_output_idx)
- {
- /* Microphone in the source output has changed */
- pa_context_get_source_output_info (context, idx,
- empathy_audio_src_source_output_info_cb, self);
- }
- else if ((type & PA_SUBSCRIPTION_EVENT_FACILITY_MASK) == PA_SUBSCRIPTION_EVENT_SOURCE
- && (type & PA_SUBSCRIPTION_EVENT_TYPE_MASK) == PA_SUBSCRIPTION_EVENT_REMOVE)
- {
- /* A mic has been removed */
- g_signal_emit (self, signals[MICROPHONE_REMOVED], 0, idx);
- }
- else if ((type & PA_SUBSCRIPTION_EVENT_FACILITY_MASK) == PA_SUBSCRIPTION_EVENT_SOURCE
- && (type & PA_SUBSCRIPTION_EVENT_TYPE_MASK) == PA_SUBSCRIPTION_EVENT_NEW)
+ description = g_getenv ("EMPATHY_AUDIO_SRC");
+
+ if (description != NULL)
{
- /* A mic has been plugged in */
- pa_context_get_source_info_by_index (context, idx,
- empathy_audio_src_source_info_cb, self);
- }
-}
+ GError *error = NULL;
-static void
-empathy_audio_src_pa_subscribe_cb (pa_context *context,
- int success,
- void *userdata)
-{
- if (!success)
- DEBUG ("Failed to subscribe to PulseAudio events");
-}
+ src = gst_parse_bin_from_description (description, TRUE, &error);
+ if (src == NULL)
+ {
+ DEBUG ("Failed to create bin %s: %s", description, error->message);
+ g_error_free (error);
+ }
-static void
-empathy_audio_src_pa_state_change_cb (pa_context *context,
- void *userdata)
-{
- EmpathyGstAudioSrc *self = userdata;
- EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (self);
- pa_context_state_t state = pa_context_get_state (priv->context);
+ return src;
+ }
- if (state == PA_CONTEXT_READY)
+ /* Use pulsesrc as default */
+ src = gst_element_factory_make ("pulsesrc", NULL);
+ if (src == NULL)
{
- /* Listen to pulseaudio events so we know when sources are
- * added and when the microphone is changed. */
- pa_context_set_subscribe_callback (priv->context,
- empathy_audio_src_pa_event_cb, self);
- pa_context_subscribe (priv->context,
- PA_SUBSCRIPTION_MASK_SOURCE | PA_SUBSCRIPTION_MASK_SOURCE_OUTPUT,
- empathy_audio_src_pa_subscribe_cb, NULL);
-
- operations_run (self);
+ g_warning ("Missing 'pulsesrc' element");
+ return NULL;
}
-}
-static void
-empathy_audio_src_source_output_index_notify (GObject *object,
- GParamSpec *pspec,
- EmpathyGstAudioSrc *self)
-{
- EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (self);
- guint source_output_idx = PA_INVALID_INDEX;
+ empathy_audio_set_stream_properties (src, TRUE);
- g_object_get (priv->src, "source-output-index", &source_output_idx, NULL);
+ /* Set latency (buffering on the PulseAudio side) of 20ms */
+ g_object_set (src, "buffer-time", (gint64) 20000, NULL);
- if (source_output_idx == PA_INVALID_INDEX)
- return;
-
- if (priv->source_output_idx == source_output_idx)
- return;
-
- /* It's actually changed. */
- priv->source_output_idx = source_output_idx;
-
- pa_context_get_source_output_info (priv->context, source_output_idx,
- empathy_audio_src_source_output_info_cb, self);
+ return src;
}
static void
{
EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (obj);
GstPad *ghost, *src;
- const gchar *src_element;
- priv->peak_level = -G_MAXDOUBLE;
- priv->lock = g_mutex_new ();
+ obj->priv = priv;
+ g_mutex_init (&priv->lock);
- src_element = g_getenv ("EMPATHY_AUDIO_SRC");
- if (src_element == NULL)
- src_element = "pulsesrc";
+ priv->volume = 1.0;
- priv->src = gst_element_factory_make (src_element, NULL);
- gst_bin_add (GST_BIN (obj), priv->src);
+ priv->src = create_src ();
+ if (priv->src == NULL)
+ return;
- if (!tp_strdiff (src_element, "pulsesrc"))
+ if (GST_IS_STREAM_VOLUME (priv->src))
{
- GstStructure *props;
-
- props = gst_structure_from_string ("props,media.role=phone", NULL);
- g_object_set (priv->src, "stream-properties", props, NULL);
- gst_structure_free (props);
+ gdouble volume;
+ gboolean mute;
+
+ priv->have_stream_volume = TRUE;
+ /* We can't do a bidirection bind as the ::notify comes from another
+ * thread, for other bits of empathy it's most simpler if it comes from
+ * the main thread */
+ g_object_bind_property (obj, "volume", priv->src, "volume",
+ G_BINDING_DEFAULT);
+ g_object_bind_property (obj, "mute", priv->src, "mute",
+ G_BINDING_DEFAULT);
+
+ /* sync and callback for bouncing */
+ g_object_get (priv->src, "volume", &volume, NULL);
+ g_object_set (obj, "volume", volume, NULL);
+
+ g_object_get (priv->src, "mute", &mute, NULL);
+ g_object_set (obj, "mute", mute, NULL);
+
+ g_signal_connect (priv->src, "notify::volume",
+ G_CALLBACK (empathy_audio_src_volume_changed), obj);
+ g_signal_connect (priv->src, "notify::mute",
+ G_CALLBACK (empathy_audio_src_volume_changed), obj);
+ }
+ else
+ {
+ g_message ("No stream volume available :(, mute will work though");
+ priv->have_stream_volume = FALSE;
}
- priv->volume = gst_element_factory_make ("volume", NULL);
- g_object_ref (priv->volume);
-
- gst_bin_add (GST_BIN (obj), priv->volume);
- gst_element_link (priv->src, priv->volume);
+ gst_bin_add (GST_BIN (obj), priv->src);
- priv->level = gst_element_factory_make ("level", NULL);
- gst_bin_add (GST_BIN (obj), priv->level);
- gst_element_link (priv->volume, priv->level);
+ priv->volume_element = gst_element_factory_make ("volume", NULL);
+ gst_bin_add (GST_BIN (obj), priv->volume_element);
+
+ {
+ GstElement *capsfilter;
+ GstCaps *caps;
+
+ /* Explicitly state what format we want from pulsesrc. This pushes resampling
+ * and format conversion as early as possible, lowering the amount of data
+ * transferred and thus improving performance. When moving to GStreamer
+ * 0.11/1.0, this should change so that we actually request what the encoder
+ * wants downstream. */
+ caps = gst_caps_new_simple ("audio/x-raw",
+ "channels", G_TYPE_INT, 1,
+ "width", G_TYPE_INT, 16,
+ "depth", G_TYPE_INT, 16,
+ "rate", G_TYPE_INT, 32000,
+ NULL);
+ capsfilter = gst_element_factory_make ("capsfilter", NULL);
+ g_object_set (G_OBJECT (capsfilter), "caps", caps, NULL);
+ gst_bin_add (GST_BIN (obj), capsfilter);
+ gst_element_link (priv->src, capsfilter);
+ gst_element_link (capsfilter, priv->volume_element);
+ }
- src = gst_element_get_static_pad (priv->level, "src");
+ src = gst_element_get_static_pad (priv->volume_element, "src");
ghost = gst_ghost_pad_new ("src", src);
gst_element_add_pad (GST_ELEMENT (obj), ghost);
gst_object_unref (G_OBJECT (src));
- /* PulseAudio stuff: We need to create a dummy pa_glib_mainloop* so
- * Pulse can use the mainloop that GTK has created for us. */
- priv->loop = pa_glib_mainloop_new (NULL);
- priv->context = pa_context_new (pa_glib_mainloop_get_api (priv->loop),
- "EmpathyAudioSrc");
-
/* Listen to changes to GstPulseSrc:source-output-index so we know when
* it's no longer PA_INVALID_INDEX (starting for the first time) or if it
* changes (READY->NULL->READY...) */
G_CALLBACK (empathy_audio_src_source_output_index_notify),
obj);
- /* Finally listen for state changes so we know when we've
- * connected. */
- pa_context_set_state_callback (priv->context,
- empathy_audio_src_pa_state_change_cb, obj);
- pa_context_connect (priv->context, NULL, 0, NULL);
+ priv->mic_monitor = empathy_mic_monitor_new ();
+ g_signal_connect (priv->mic_monitor, "microphone-changed",
+ G_CALLBACK (empathy_audio_src_microphone_changed_cb), obj);
- priv->operations = g_queue_new ();
+ priv->source_idx = PA_INVALID_INDEX;
}
static void empathy_audio_src_dispose (GObject *object);
static void empathy_audio_src_finalize (GObject *object);
-static void empathy_audio_src_handle_message (GstBin *bin,
- GstMessage *message);
-
-static gboolean empathy_audio_src_levels_updated (gpointer user_data);
static void
empathy_audio_src_set_property (GObject *object,
switch (property_id)
{
case PROP_VOLUME:
- empathy_audio_src_set_volume (EMPATHY_GST_AUDIO_SRC (object),
+ empathy_audio_src_set_hw_volume (EMPATHY_GST_AUDIO_SRC (object),
g_value_get_double (value));
break;
+ case PROP_MUTE:
+ empathy_audio_set_hw_mute (EMPATHY_GST_AUDIO_SRC (object),
+ g_value_get_boolean (value));
+ break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
}
switch (property_id)
{
case PROP_VOLUME:
- g_value_set_double (value,
- empathy_audio_src_get_volume (self));
+ g_value_set_double (value, priv->volume);
break;
- case PROP_PEAK_LEVEL:
- g_mutex_lock (priv->lock);
- g_value_set_double (value, priv->peak_level);
- g_mutex_unlock (priv->lock);
- break;
- case PROP_RMS_LEVEL:
- g_mutex_lock (priv->lock);
- g_value_set_double (value, priv->rms_level);
- g_mutex_unlock (priv->lock);
+ case PROP_MUTE:
+ g_value_set_boolean (value, priv->mute);
break;
case PROP_MICROPHONE:
g_value_set_uint (value, priv->source_idx);
*empathy_audio_src_class)
{
GObjectClass *object_class = G_OBJECT_CLASS (empathy_audio_src_class);
- GstBinClass *gstbin_class = GST_BIN_CLASS (empathy_audio_src_class);
GParamSpec *param_spec;
g_type_class_add_private (empathy_audio_src_class,
object_class->set_property = empathy_audio_src_set_property;
object_class->get_property = empathy_audio_src_get_property;
- gstbin_class->handle_message =
- GST_DEBUG_FUNCPTR (empathy_audio_src_handle_message);
-
param_spec = g_param_spec_double ("volume", "Volume", "volume contol",
0.0, 5.0, 1.0,
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
g_object_class_install_property (object_class, PROP_VOLUME, param_spec);
- param_spec = g_param_spec_double ("peak-level", "peak level", "peak level",
- -G_MAXDOUBLE, G_MAXDOUBLE, 0,
- G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
- g_object_class_install_property (object_class, PROP_PEAK_LEVEL, param_spec);
+ param_spec = g_param_spec_boolean ("mute", "Mute", "mute contol",
+ FALSE,
+ G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
+ g_object_class_install_property (object_class, PROP_MUTE, param_spec);
param_spec = g_param_spec_uint ("microphone", "microphone", "microphone",
0, G_MAXUINT, G_MAXUINT,
G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
g_object_class_install_property (object_class, PROP_MICROPHONE, param_spec);
-
- signals[PEAK_LEVEL_CHANGED] = g_signal_new ("peak-level-changed",
- G_TYPE_FROM_CLASS (empathy_audio_src_class),
- G_SIGNAL_RUN_LAST,
- 0,
- NULL, NULL,
- g_cclosure_marshal_VOID__DOUBLE,
- G_TYPE_NONE, 1, G_TYPE_DOUBLE);
-
- param_spec = g_param_spec_double ("rms-level", "RMS level", "RMS level",
- -G_MAXDOUBLE, G_MAXDOUBLE, 0,
- G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
- g_object_class_install_property (object_class, PROP_RMS_LEVEL, param_spec);
-
-
- signals[RMS_LEVEL_CHANGED] = g_signal_new ("rms-level-changed",
- G_TYPE_FROM_CLASS (empathy_audio_src_class),
- G_SIGNAL_RUN_LAST,
- 0,
- NULL, NULL,
- g_cclosure_marshal_VOID__DOUBLE,
- G_TYPE_NONE, 1, G_TYPE_DOUBLE);
-
- signals[MICROPHONE_ADDED] = g_signal_new ("microphone-added",
- G_TYPE_FROM_CLASS (empathy_audio_src_class),
- G_SIGNAL_RUN_LAST,
- 0,
- NULL, NULL,
- _src_marshal_VOID__UINT_STRING_STRING_BOOLEAN,
- G_TYPE_NONE, 4, G_TYPE_UINT, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_BOOLEAN);
-
- signals[MICROPHONE_REMOVED] = g_signal_new ("microphone-removed",
- G_TYPE_FROM_CLASS (empathy_audio_src_class),
- G_SIGNAL_RUN_LAST,
- 0,
- NULL, NULL,
- g_cclosure_marshal_VOID__UINT,
- G_TYPE_NONE, 1, G_TYPE_UINT);
}
void
priv->dispose_has_run = TRUE;
- if (priv->idle_id != 0)
- g_source_remove (priv->idle_id);
+ if (priv->volume_idle_id != 0)
+ g_source_remove (priv->volume_idle_id);
+ priv->volume_idle_id = 0;
- priv->idle_id = 0;
-
- if (priv->context != NULL)
- pa_context_unref (priv->context);
- priv->context = NULL;
-
- if (priv->loop != NULL)
- pa_glib_mainloop_free (priv->loop);
- priv->loop = NULL;
+ tp_clear_object (&priv->mic_monitor);
/* release any references held by the object here */
EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (self);
/* free any data held directly by the object here */
- g_mutex_free (priv->lock);
-
- g_queue_foreach (priv->operations, (GFunc) operation_free,
- GUINT_TO_POINTER (TRUE));
- g_queue_free (priv->operations);
+ g_mutex_clear (&priv->lock);
G_OBJECT_CLASS (empathy_audio_src_parent_class)->finalize (object);
}
static gboolean
-empathy_audio_src_levels_updated (gpointer user_data)
+empathy_audio_src_volume_changed_idle (gpointer user_data)
{
EmpathyGstAudioSrc *self = EMPATHY_GST_AUDIO_SRC (user_data);
EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (self);
+ gdouble volume;
+ gboolean mute;
- g_mutex_lock (priv->lock);
+ g_mutex_lock (&priv->lock);
+ priv->volume_idle_id = 0;
+ g_mutex_unlock (&priv->lock);
- g_signal_emit (self, signals[PEAK_LEVEL_CHANGED], 0, priv->peak_level);
- g_signal_emit (self, signals[RMS_LEVEL_CHANGED], 0, priv->rms_level);
- priv->idle_id = 0;
+ volume = empathy_audio_src_get_hw_volume (self);
- g_mutex_unlock (priv->lock);
+ if (volume != priv->volume)
+ {
+ priv->volume = volume;
+ g_object_notify (G_OBJECT (self), "volume");
+ }
+
+ mute = empathy_audio_src_get_hw_mute (self);
+ if (mute != priv->mute)
+ {
+ priv->mute = mute;
+ /* hw mute changed, follow with own volume */
+ g_object_set (self->priv->volume_element, "mute", mute, NULL);
+ g_object_notify (G_OBJECT (self), "mute");
+ }
return FALSE;
}
-static void
-empathy_audio_src_handle_message (GstBin *bin, GstMessage *message)
+static gboolean
+empathy_audio_src_volume_changed (GObject *object,
+ GParamSpec *pspec,
+ gpointer user_data)
{
- EmpathyGstAudioSrc *self = EMPATHY_GST_AUDIO_SRC (bin);
- EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (self);
-
- if (GST_MESSAGE_TYPE (message) == GST_MESSAGE_ELEMENT &&
- GST_MESSAGE_SRC (message) == GST_OBJECT (priv->level))
- {
- const GstStructure *s;
- const gchar *name;
- const GValue *list;
- guint i, len;
- gdouble peak = -G_MAXDOUBLE;
- gdouble rms = -G_MAXDOUBLE;
-
- s = gst_message_get_structure (message);
- name = gst_structure_get_name (s);
-
- if (g_strcmp0 ("level", name) != 0)
- goto out;
-
- list = gst_structure_get_value (s, "peak");
- len = gst_value_list_get_size (list);
-
- for (i =0 ; i < len; i++)
- {
- const GValue *value;
- gdouble db;
-
- value = gst_value_list_get_value (list, i);
- db = g_value_get_double (value);
- peak = MAX (db, peak);
- }
-
- list = gst_structure_get_value (s, "rms");
- len = gst_value_list_get_size (list);
-
- for (i =0 ; i < len; i++)
- {
- const GValue *value;
- gdouble db;
-
- value = gst_value_list_get_value (list, i);
- db = g_value_get_double (value);
- rms = MAX (db, rms);
- }
-
- g_mutex_lock (priv->lock);
-
- priv->peak_level = peak;
- priv->rms_level = rms;
- if (priv->idle_id == 0)
- priv->idle_id = g_idle_add (empathy_audio_src_levels_updated, self);
+ EmpathyGstAudioSrc *self = EMPATHY_GST_AUDIO_SRC (user_data);
- g_mutex_unlock (priv->lock);
- }
+ g_mutex_lock (&self->priv->lock);
+ if (self->priv->volume_idle_id == 0)
+ self->priv->volume_idle_id = g_idle_add (
+ empathy_audio_src_volume_changed_idle, self);
+ g_mutex_unlock (&self->priv->lock);
-out:
- GST_BIN_CLASS (empathy_audio_src_parent_class)->handle_message (bin,
- message);
+ return FALSE;
}
GstElement *
}
void
-empathy_audio_src_set_volume (EmpathyGstAudioSrc *src, gdouble volume)
+empathy_audio_src_set_echo_cancel (EmpathyGstAudioSrc *src,
+ gboolean enable)
{
- EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (src);
- GParamSpec *pspec;
- GParamSpecDouble *pspec_double;
-
- pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (priv->volume),
- "volume");
-
- g_assert (pspec != NULL);
-
- pspec_double = G_PARAM_SPEC_DOUBLE (pspec);
-
- volume = CLAMP (volume, pspec_double->minimum, pspec_double->maximum);
+ DEBUG ("Src echo cancellation setting: %s", enable ? "on" : "off");
+ empathy_audio_set_stream_properties (src->priv->src, enable);
+}
- g_object_set (G_OBJECT (priv->volume), "volume", volume, NULL);
+void
+empathy_audio_src_set_volume (EmpathyGstAudioSrc *src, gdouble volume)
+{
+ g_object_set (src, "volume", volume, NULL);
}
gdouble
empathy_audio_src_get_volume (EmpathyGstAudioSrc *src)
{
- EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (src);
- gdouble volume;
-
- g_object_get (G_OBJECT (priv->volume), "volume", &volume, NULL);
-
- return volume;
+ return src->priv->volume;
}
-void
-empathy_audio_src_get_microphones_async (EmpathyGstAudioSrc *src,
- GAsyncReadyCallback callback,
- gpointer user_data)
+guint
+empathy_audio_src_get_microphone (EmpathyGstAudioSrc *src)
{
EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (src);
- Operation *operation;
- GSimpleAsyncResult *simple;
-
- simple = g_simple_async_result_new (G_OBJECT (src), callback, user_data,
- empathy_audio_src_get_microphones_async);
-
- /* If we can't change mic let's not pretend we can by returning the
- * list of available mics. */
- if (!empathy_audio_src_supports_changing_mic (src))
- {
- g_simple_async_result_set_error (simple, G_IO_ERROR, G_IO_ERROR_FAILED,
- "pulsesrc is not new enough to support changing microphone");
- g_simple_async_result_complete_in_idle (simple);
- g_object_unref (simple);
- return;
- }
-
- operation = operation_new (operation_get_microphones, simple);
- g_queue_push_tail (priv->operations, operation);
- /* gogogogo */
- operations_run (src);
+ return priv->source_idx;
}
-const GList *
-empathy_audio_src_get_microphones_finish (EmpathyGstAudioSrc *src,
+static void
+empathy_audio_src_change_microphone_cb (GObject *source_object,
GAsyncResult *result,
- GError **error)
+ gpointer user_data)
{
- GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
- GQueue *queue;
-
- if (g_simple_async_result_propagate_error (simple, error))
- return NULL;
-
- g_return_val_if_fail (g_simple_async_result_is_valid (result,
- G_OBJECT (src), empathy_audio_src_get_microphones_async),
- NULL);
-
- queue = g_simple_async_result_get_op_res_gpointer (simple);
- return queue->head;
-}
+ EmpathyMicMonitor *monitor = EMPATHY_MIC_MONITOR (source_object);
+ GSimpleAsyncResult *simple = user_data;
+ GError *error = NULL;
-guint
-empathy_audio_src_get_microphone (EmpathyGstAudioSrc *src)
-{
- EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (src);
+ if (!empathy_mic_monitor_change_microphone_finish (monitor,
+ result, &error))
+ {
+ g_simple_async_result_take_error (simple, error);
+ }
- return priv->source_idx;
+ g_simple_async_result_complete (simple);
+ g_object_unref (simple);
}
void
EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (src);
guint source_output_idx;
GSimpleAsyncResult *simple;
- Operation *operation;
simple = g_simple_async_result_new (G_OBJECT (src), callback, user_data,
empathy_audio_src_change_microphone_async);
return;
}
- g_object_get (priv->src, "source-output-index", &source_output_idx, NULL);
+ source_output_idx = empathy_audio_src_get_mic_index (src);
if (source_output_idx == PA_INVALID_INDEX)
{
return;
}
- g_simple_async_result_set_op_res_gpointer (simple,
- GUINT_TO_POINTER (microphone), NULL);
-
- operation = operation_new (operation_change_microphone, simple);
- g_queue_push_tail (priv->operations, operation);
-
- /* gogogogo */
- operations_run (src);
+ empathy_mic_monitor_change_microphone_async (priv->mic_monitor,
+ source_output_idx, microphone, empathy_audio_src_change_microphone_cb,
+ simple);
}
gboolean
GAsyncResult *result,
GError **error)
{
- empathy_implement_finish_void (src,
+ tpaw_implement_finish_void (src,
empathy_audio_src_change_microphone_async);
}
+
+void
+empathy_audio_src_set_mute (EmpathyGstAudioSrc *self,
+ gboolean mute)
+{
+ empathy_audio_set_hw_mute (self, mute);
+
+ g_object_notify (G_OBJECT (self), "mute");
+}