]> git.0d.be Git - empathy.git/blob - src/empathy-audio-src.c
Merge branch 'gnome-3-4'
[empathy.git] / src / empathy-audio-src.c
1 /*
2  * empathy-gst-audio-src.c - Source for EmpathyGstAudioSrc
3  * Copyright (C) 2008 Collabora Ltd.
4  * @author Sjoerd Simons <sjoerd.simons@collabora.co.uk>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
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  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  */
20
21 #include "config.h"
22
23 #include <stdio.h>
24 #include <stdlib.h>
25
26 #include <gst/interfaces/streamvolume.h>
27
28 #include <libempathy/empathy-utils.h>
29 #include <libempathy-gtk/empathy-call-utils.h>
30
31 #include "empathy-audio-src.h"
32
33 #include "empathy-mic-monitor.h"
34
35 #define DEBUG_FLAG EMPATHY_DEBUG_VOIP
36 #include <libempathy/empathy-debug.h>
37
38 G_DEFINE_TYPE(EmpathyGstAudioSrc, empathy_audio_src, GST_TYPE_BIN)
39
40 /* signal enum */
41 enum
42 {
43     PEAK_LEVEL_CHANGED,
44     RMS_LEVEL_CHANGED,
45     LAST_SIGNAL
46 };
47
48 static guint signals[LAST_SIGNAL] = {0};
49
50 enum {
51     PROP_VOLUME = 1,
52     PROP_MUTE,
53     PROP_RMS_LEVEL,
54     PROP_PEAK_LEVEL,
55     PROP_MICROPHONE,
56 };
57
58 /* private structure */
59 struct _EmpathyGstAudioSrcPrivate
60 {
61   gboolean dispose_has_run;
62   GstElement *src;
63   GstElement *level;
64   GstElement *volume_element;
65
66   EmpathyMicMonitor *mic_monitor;
67
68   /* 0 if not known yet */
69   guint source_output_idx;
70   /* G_MAXUINT if not known yet */
71   guint source_idx;
72
73   gdouble peak_level;
74   gdouble rms_level;
75
76   gdouble volume;
77   gboolean mute;
78   gboolean have_stream_volume;
79
80   GMutex lock;
81   guint level_idle_id;
82   guint volume_idle_id;
83 };
84
85 #define EMPATHY_GST_AUDIO_SRC_GET_PRIVATE(o) \
86   (G_TYPE_INSTANCE_GET_PRIVATE ((o), EMPATHY_TYPE_GST_AUDIO_SRC, \
87   EmpathyGstAudioSrcPrivate))
88
89
90 static gboolean
91 empathy_audio_src_volume_changed (GObject *object,
92   GParamSpec *pspec,
93   gpointer user_data);
94
95 static void
96 empathy_audio_set_hw_mute (EmpathyGstAudioSrc *self, gboolean mute)
97 {
98   if (mute == self->priv->mute)
99     return;
100
101   if (self->priv->have_stream_volume)
102     g_object_set (self->priv->src, "mute", mute, NULL);
103
104   /* Belt and braces: If for some reason the underlying src doesn't mute
105    * correctly or doesn't update us when it unmutes correctly enforce it using
106    * our own volume element. Our UI can in no circumstances be made to think
107    * the input is muted while it's not */
108   g_object_set (self->priv->volume_element, "mute", mute, NULL);
109
110   self->priv->mute = mute;
111 }
112
113 static gboolean
114 empathy_audio_src_get_hw_mute (EmpathyGstAudioSrc *self)
115 {
116   gboolean result;
117   g_object_get (self->priv->src, "mute", &result, NULL);
118
119   return result;
120 }
121
122 static void
123 empathy_audio_src_set_hw_volume (EmpathyGstAudioSrc *self,
124     gdouble volume)
125 {
126   if (volume == self->priv->volume)
127     return;
128
129   if (self->priv->have_stream_volume)
130     g_object_set (self->priv->src, "volume", volume, NULL);
131   self->priv->volume = volume;
132 }
133
134 static gdouble
135 empathy_audio_src_get_hw_volume (EmpathyGstAudioSrc *self)
136 {
137   gdouble result;
138   g_object_get (self->priv->src, "volume", &result, NULL);
139
140   return result;
141 }
142
143
144 gboolean
145 empathy_audio_src_supports_changing_mic (EmpathyGstAudioSrc *self)
146 {
147   EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (self);
148   GObjectClass *object_class;
149
150   object_class = G_OBJECT_GET_CLASS (priv->src);
151
152   return (g_object_class_find_property (object_class,
153           "source-output-index") != NULL);
154 }
155
156 static guint
157 empathy_audio_src_get_mic_index (EmpathyGstAudioSrc *self)
158 {
159   EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (self);
160   guint audio_src_idx = PA_INVALID_INDEX;
161
162   if (empathy_audio_src_supports_changing_mic (self))
163     g_object_get (priv->src,
164       "source-output-index", &audio_src_idx,
165       NULL);
166
167   return audio_src_idx;
168 }
169
170 static void
171 empathy_audio_src_microphone_changed_cb (EmpathyMicMonitor *monitor,
172     guint source_output_idx,
173     guint source_idx,
174     gpointer user_data)
175 {
176   EmpathyGstAudioSrc *self = user_data;
177   EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (self);
178   guint audio_src_idx;
179
180   audio_src_idx = empathy_audio_src_get_mic_index (self);
181
182   if (source_output_idx == PA_INVALID_INDEX
183       || source_output_idx != audio_src_idx)
184     return;
185
186   if (priv->source_idx == source_idx)
187     return;
188
189   priv->source_idx = source_idx;
190   g_object_notify (G_OBJECT (self), "microphone");
191 }
192
193 static void
194 empathy_audio_src_get_current_mic_cb (GObject *source_object,
195     GAsyncResult *result,
196     gpointer user_data)
197 {
198   EmpathyMicMonitor *monitor = EMPATHY_MIC_MONITOR (source_object);
199   EmpathyGstAudioSrc *self = user_data;
200   EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (self);
201   guint source_idx;
202   GError *error = NULL;
203
204   source_idx = empathy_mic_monitor_get_current_mic_finish (monitor, result, &error);
205
206   if (error != NULL)
207     {
208       DEBUG ("Failed to get current mic: %s", error->message);
209       g_clear_error (&error);
210       return;
211     }
212
213   if (priv->source_idx == source_idx)
214     return;
215
216   priv->source_idx = source_idx;
217   g_object_notify (G_OBJECT (self), "microphone");
218 }
219
220 static void
221 empathy_audio_src_source_output_index_notify (GObject *object,
222     GParamSpec *pspec,
223     EmpathyGstAudioSrc *self)
224 {
225   EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (self);
226   guint source_output_idx;
227
228   source_output_idx = empathy_audio_src_get_mic_index (self);
229
230   if (source_output_idx == PA_INVALID_INDEX)
231     return;
232
233   if (priv->source_output_idx == source_output_idx)
234     return;
235
236   /* It's actually changed. */
237   priv->source_output_idx = source_output_idx;
238
239   empathy_mic_monitor_get_current_mic_async (priv->mic_monitor,
240       source_output_idx, empathy_audio_src_get_current_mic_cb, self);
241 }
242
243 static GstElement *
244 create_src (void)
245 {
246   GstElement *src;
247   const gchar *description;
248
249   description = g_getenv ("EMPATHY_AUDIO_SRC");
250
251   if (description != NULL)
252     {
253       GError *error = NULL;
254
255       src = gst_parse_bin_from_description (description, TRUE, &error);
256       if (src == NULL)
257         {
258           DEBUG ("Failed to create bin %s: %s", description, error->message);
259           g_error_free (error);
260         }
261
262       return src;
263     }
264
265   /* Use pulsesrc as default */
266   src = gst_element_factory_make ("pulsesrc", NULL);
267   if (src == NULL)
268     return NULL;
269
270   empathy_call_set_stream_properties (src, TRUE);
271
272   /* Set latency (buffering on the PulseAudio side) of 20ms */
273   g_object_set (src, "buffer-time", (gint64) 20000, NULL);
274
275   return src;
276 }
277
278 static void
279 empathy_audio_src_init (EmpathyGstAudioSrc *obj)
280 {
281   EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (obj);
282   GstPad *ghost, *src;
283   GstElement *capsfilter;
284   GstCaps *caps;
285
286   obj->priv = priv;
287   priv->peak_level = -G_MAXDOUBLE;
288   g_mutex_init (&priv->lock);
289
290   priv->volume = 1.0;
291
292   priv->src = create_src ();
293   if (priv->src == NULL)
294     return;
295
296   if (GST_IS_STREAM_VOLUME (priv->src))
297     {
298       gdouble volume;
299       gboolean mute;
300
301       priv->have_stream_volume = TRUE;
302       /* We can't do a bidirection bind as the ::notify comes from another
303        * thread, for other bits of empathy it's most simpler if it comes from
304        * the main thread */
305       g_object_bind_property (obj, "volume", priv->src, "volume",
306         G_BINDING_DEFAULT);
307       g_object_bind_property (obj, "mute", priv->src, "mute",
308         G_BINDING_DEFAULT);
309
310       /* sync and callback for bouncing */
311       g_object_get (priv->src, "volume", &volume, NULL);
312       g_object_set (obj, "volume", volume, NULL);
313
314       g_object_get (priv->src, "mute", &mute, NULL);
315       g_object_set (obj, "mute", mute, NULL);
316
317       g_signal_connect (priv->src, "notify::volume",
318         G_CALLBACK (empathy_audio_src_volume_changed), obj);
319       g_signal_connect (priv->src, "notify::mute",
320         G_CALLBACK (empathy_audio_src_volume_changed), obj);
321     }
322   else
323     {
324       g_message ("No stream volume available :(, mute will work though");
325       priv->have_stream_volume = FALSE;
326     }
327
328   gst_bin_add (GST_BIN (obj), priv->src);
329
330   /* Explicitly state what format we want from pulsesrc. This pushes resampling
331    * and format conversion as early as possible, lowering the amount of data
332    * transferred and thus improving performance. When moving to GStreamer
333    * 0.11/1.0, this should change so that we actually request what the encoder
334    * wants downstream. */
335   caps = gst_caps_new_simple ("audio/x-raw-int",
336       "channels", G_TYPE_INT, 1,
337       "width", G_TYPE_INT, 16,
338       "depth", G_TYPE_INT, 16,
339       "rate", G_TYPE_INT, 32000,
340       NULL);
341   capsfilter = gst_element_factory_make ("capsfilter", NULL);
342   g_object_set (G_OBJECT (capsfilter), "caps", caps, NULL);
343   gst_bin_add (GST_BIN (obj), capsfilter);
344   gst_element_link (priv->src, capsfilter);
345
346   priv->volume_element = gst_element_factory_make ("volume", NULL);
347   gst_bin_add (GST_BIN (obj), priv->volume_element);
348   gst_element_link (capsfilter, priv->volume_element);
349
350   priv->level = gst_element_factory_make ("level", NULL);
351   gst_bin_add (GST_BIN (obj), priv->level);
352   gst_element_link (priv->volume_element, priv->level);
353
354   src = gst_element_get_static_pad (priv->level, "src");
355
356   ghost = gst_ghost_pad_new ("src", src);
357   gst_element_add_pad (GST_ELEMENT (obj), ghost);
358
359   gst_object_unref (G_OBJECT (src));
360
361   /* Listen to changes to GstPulseSrc:source-output-index so we know when
362    * it's no longer PA_INVALID_INDEX (starting for the first time) or if it
363    * changes (READY->NULL->READY...) */
364   g_signal_connect (priv->src, "notify::source-output-index",
365       G_CALLBACK (empathy_audio_src_source_output_index_notify),
366       obj);
367
368   priv->mic_monitor = empathy_mic_monitor_new ();
369   g_signal_connect (priv->mic_monitor, "microphone-changed",
370       G_CALLBACK (empathy_audio_src_microphone_changed_cb), obj);
371
372   priv->source_idx = PA_INVALID_INDEX;
373 }
374
375 static void empathy_audio_src_dispose (GObject *object);
376 static void empathy_audio_src_finalize (GObject *object);
377 static void empathy_audio_src_handle_message (GstBin *bin,
378   GstMessage *message);
379
380 static gboolean empathy_audio_src_levels_updated (gpointer user_data);
381
382 static void
383 empathy_audio_src_set_property (GObject *object,
384   guint property_id, const GValue *value, GParamSpec *pspec)
385 {
386   switch (property_id)
387     {
388       case PROP_VOLUME:
389         empathy_audio_src_set_hw_volume (EMPATHY_GST_AUDIO_SRC (object),
390           g_value_get_double (value));
391         break;
392       case PROP_MUTE:
393         empathy_audio_set_hw_mute (EMPATHY_GST_AUDIO_SRC (object),
394           g_value_get_boolean (value));
395         break;
396       default:
397         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
398     }
399 }
400
401 static void
402 empathy_audio_src_get_property (GObject *object,
403   guint property_id, GValue *value, GParamSpec *pspec)
404 {
405   EmpathyGstAudioSrc *self = EMPATHY_GST_AUDIO_SRC (object);
406   EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (self);
407
408   switch (property_id)
409     {
410       case PROP_VOLUME:
411         g_value_set_double (value, priv->volume);
412         break;
413       case PROP_MUTE:
414         g_value_set_boolean (value, priv->mute);
415         break;
416       case PROP_PEAK_LEVEL:
417         g_mutex_lock (&priv->lock);
418         g_value_set_double (value, priv->peak_level);
419         g_mutex_unlock (&priv->lock);
420         break;
421       case PROP_RMS_LEVEL:
422         g_mutex_lock (&priv->lock);
423         g_value_set_double (value, priv->rms_level);
424         g_mutex_unlock (&priv->lock);
425         break;
426       case PROP_MICROPHONE:
427         g_value_set_uint (value, priv->source_idx);
428         break;
429       default:
430         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
431     }
432 }
433
434 static void
435 empathy_audio_src_class_init (EmpathyGstAudioSrcClass
436   *empathy_audio_src_class)
437 {
438   GObjectClass *object_class = G_OBJECT_CLASS (empathy_audio_src_class);
439   GstBinClass *gstbin_class = GST_BIN_CLASS (empathy_audio_src_class);
440   GParamSpec *param_spec;
441
442   g_type_class_add_private (empathy_audio_src_class,
443     sizeof (EmpathyGstAudioSrcPrivate));
444
445   object_class->dispose = empathy_audio_src_dispose;
446   object_class->finalize = empathy_audio_src_finalize;
447
448   object_class->set_property = empathy_audio_src_set_property;
449   object_class->get_property = empathy_audio_src_get_property;
450
451   gstbin_class->handle_message =
452     GST_DEBUG_FUNCPTR (empathy_audio_src_handle_message);
453
454   param_spec = g_param_spec_double ("volume", "Volume", "volume contol",
455     0.0, 5.0, 1.0,
456     G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
457   g_object_class_install_property (object_class, PROP_VOLUME, param_spec);
458
459   param_spec = g_param_spec_boolean ("mute", "Mute", "mute contol",
460     FALSE,
461     G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
462   g_object_class_install_property (object_class, PROP_MUTE, param_spec);
463
464   param_spec = g_param_spec_double ("peak-level", "peak level", "peak level",
465     -G_MAXDOUBLE, G_MAXDOUBLE, 0,
466     G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
467   g_object_class_install_property (object_class, PROP_PEAK_LEVEL, param_spec);
468
469   param_spec = g_param_spec_uint ("microphone", "microphone", "microphone",
470     0, G_MAXUINT, G_MAXUINT,
471     G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
472   g_object_class_install_property (object_class, PROP_MICROPHONE, param_spec);
473
474   signals[PEAK_LEVEL_CHANGED] = g_signal_new ("peak-level-changed",
475     G_TYPE_FROM_CLASS (empathy_audio_src_class),
476     G_SIGNAL_RUN_LAST,
477     0,
478     NULL, NULL,
479     g_cclosure_marshal_generic,
480     G_TYPE_NONE, 1, G_TYPE_DOUBLE);
481
482   param_spec = g_param_spec_double ("rms-level", "RMS level", "RMS level",
483     -G_MAXDOUBLE, G_MAXDOUBLE, 0,
484     G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
485   g_object_class_install_property (object_class, PROP_RMS_LEVEL, param_spec);
486
487   signals[RMS_LEVEL_CHANGED] = g_signal_new ("rms-level-changed",
488     G_TYPE_FROM_CLASS (empathy_audio_src_class),
489     G_SIGNAL_RUN_LAST,
490     0,
491     NULL, NULL,
492     g_cclosure_marshal_generic,
493     G_TYPE_NONE, 1, G_TYPE_DOUBLE);
494 }
495
496 void
497 empathy_audio_src_dispose (GObject *object)
498 {
499   EmpathyGstAudioSrc *self = EMPATHY_GST_AUDIO_SRC (object);
500   EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (self);
501
502   if (priv->dispose_has_run)
503     return;
504
505   priv->dispose_has_run = TRUE;
506
507   if (priv->level_idle_id != 0)
508     g_source_remove (priv->level_idle_id);
509   priv->level_idle_id = 0;
510
511   if (priv->volume_idle_id != 0)
512     g_source_remove (priv->volume_idle_id);
513   priv->volume_idle_id = 0;
514
515   tp_clear_object (&priv->mic_monitor);
516
517   /* release any references held by the object here */
518
519   if (G_OBJECT_CLASS (empathy_audio_src_parent_class)->dispose)
520     G_OBJECT_CLASS (empathy_audio_src_parent_class)->dispose (object);
521 }
522
523 void
524 empathy_audio_src_finalize (GObject *object)
525 {
526   EmpathyGstAudioSrc *self = EMPATHY_GST_AUDIO_SRC (object);
527   EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (self);
528
529   /* free any data held directly by the object here */
530   g_mutex_clear (&priv->lock);
531
532   G_OBJECT_CLASS (empathy_audio_src_parent_class)->finalize (object);
533 }
534
535 static gboolean
536 empathy_audio_src_levels_updated (gpointer user_data)
537 {
538   EmpathyGstAudioSrc *self = EMPATHY_GST_AUDIO_SRC (user_data);
539   EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (self);
540
541   g_mutex_lock (&priv->lock);
542
543   g_signal_emit (self, signals[PEAK_LEVEL_CHANGED], 0, priv->peak_level);
544   g_signal_emit (self, signals[RMS_LEVEL_CHANGED], 0, priv->rms_level);
545   priv->level_idle_id = 0;
546
547   g_mutex_unlock (&priv->lock);
548
549   return FALSE;
550 }
551
552 static gboolean
553 empathy_audio_src_volume_changed_idle (gpointer user_data)
554 {
555   EmpathyGstAudioSrc *self = EMPATHY_GST_AUDIO_SRC (user_data);
556   EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (self);
557   gdouble volume;
558   gboolean mute;
559
560   g_mutex_lock (&priv->lock);
561   priv->volume_idle_id = 0;
562   g_mutex_unlock (&priv->lock);
563
564   volume = empathy_audio_src_get_hw_volume (self);
565
566   if (volume != priv->volume)
567     {
568       priv->volume = volume;
569       g_object_notify (G_OBJECT (self), "volume");
570     }
571
572   mute = empathy_audio_src_get_hw_mute (self);
573   if (mute != priv->mute)
574     {
575       priv->mute = mute;
576       /* hw mute changed, follow with own volume */
577       g_object_set (self->priv->volume_element, "mute", mute, NULL);
578       g_object_notify (G_OBJECT (self), "mute");
579     }
580
581   return FALSE;
582 }
583
584 static gboolean
585 empathy_audio_src_volume_changed (GObject *object,
586   GParamSpec *pspec,
587   gpointer user_data)
588 {
589   EmpathyGstAudioSrc *self = EMPATHY_GST_AUDIO_SRC (user_data);
590
591   g_mutex_lock (self->priv->lock);
592   if (self->priv->volume_idle_id == 0)
593     self->priv->volume_idle_id = g_idle_add (
594       empathy_audio_src_volume_changed_idle, self);
595   g_mutex_unlock (self->priv->lock);
596
597   return FALSE;
598 }
599
600 static void
601 empathy_audio_src_handle_message (GstBin *bin, GstMessage *message)
602 {
603   EmpathyGstAudioSrc *self = EMPATHY_GST_AUDIO_SRC (bin);
604   EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (self);
605
606   if (GST_MESSAGE_TYPE (message) == GST_MESSAGE_ELEMENT &&
607       GST_MESSAGE_SRC (message) == GST_OBJECT (priv->level))
608     {
609       const GstStructure *s;
610       const gchar *name;
611       const GValue *list;
612       guint i, len;
613       gdouble peak = -G_MAXDOUBLE;
614       gdouble rms = -G_MAXDOUBLE;
615
616       s = gst_message_get_structure (message);
617       name = gst_structure_get_name (s);
618
619       if (g_strcmp0 ("level", name) != 0)
620         goto out;
621
622       list = gst_structure_get_value (s, "peak");
623       len = gst_value_list_get_size (list);
624
625       for (i =0 ; i < len; i++)
626         {
627           const GValue *value;
628           gdouble db;
629
630           value = gst_value_list_get_value (list, i);
631           db = g_value_get_double (value);
632           peak = MAX (db, peak);
633         }
634
635       list = gst_structure_get_value (s, "rms");
636       len = gst_value_list_get_size (list);
637
638       for (i =0 ; i < len; i++)
639         {
640           const GValue *value;
641           gdouble db;
642
643           value = gst_value_list_get_value (list, i);
644           db = g_value_get_double (value);
645           rms = MAX (db, rms);
646         }
647
648       g_mutex_lock (&priv->lock);
649
650       priv->peak_level = peak;
651       priv->rms_level = rms;
652       if (priv->level_idle_id == 0)
653         priv->level_idle_id = g_idle_add (
654           empathy_audio_src_levels_updated, self);
655
656       g_mutex_unlock (&priv->lock);
657     }
658 out:
659    GST_BIN_CLASS (empathy_audio_src_parent_class)->handle_message (bin,
660     message);
661 }
662
663 GstElement *
664 empathy_audio_src_new (void)
665 {
666   static gboolean registered = FALSE;
667
668   if (!registered) {
669     if (!gst_element_register (NULL, "empathyaudiosrc",
670             GST_RANK_NONE, EMPATHY_TYPE_GST_AUDIO_SRC))
671       return NULL;
672     registered = TRUE;
673   }
674   return gst_element_factory_make ("empathyaudiosrc", NULL);
675 }
676
677 void
678 empathy_audio_src_set_echo_cancel (EmpathyGstAudioSrc *src,
679   gboolean enable)
680 {
681   DEBUG ("Src echo cancellation setting: %s", enable ? "on" : "off");
682   empathy_call_set_stream_properties (src->priv->src, enable);
683 }
684
685 void
686 empathy_audio_src_set_volume (EmpathyGstAudioSrc *src, gdouble volume)
687 {
688   g_object_set (src, "volume", volume, NULL);
689 }
690
691 gdouble
692 empathy_audio_src_get_volume (EmpathyGstAudioSrc *src)
693 {
694   return src->priv->volume;
695 }
696
697 guint
698 empathy_audio_src_get_microphone (EmpathyGstAudioSrc *src)
699 {
700   EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (src);
701
702   return priv->source_idx;
703 }
704
705 static void
706 empathy_audio_src_change_microphone_cb (GObject *source_object,
707     GAsyncResult *result,
708     gpointer user_data)
709 {
710   EmpathyMicMonitor *monitor = EMPATHY_MIC_MONITOR (source_object);
711   GSimpleAsyncResult *simple = user_data;
712   GError *error = NULL;
713
714   if (!empathy_mic_monitor_change_microphone_finish (monitor,
715           result, &error))
716     {
717       g_simple_async_result_take_error (simple, error);
718     }
719
720   g_simple_async_result_complete (simple);
721   g_object_unref (simple);
722 }
723
724 void
725 empathy_audio_src_change_microphone_async (EmpathyGstAudioSrc *src,
726     guint microphone,
727     GAsyncReadyCallback callback,
728     gpointer user_data)
729 {
730   EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (src);
731   guint source_output_idx;
732   GSimpleAsyncResult *simple;
733
734   simple = g_simple_async_result_new (G_OBJECT (src), callback, user_data,
735       empathy_audio_src_change_microphone_async);
736
737   if (!empathy_audio_src_supports_changing_mic (src))
738     {
739       g_simple_async_result_set_error (simple, G_IO_ERROR, G_IO_ERROR_FAILED,
740           "pulsesrc is not new enough to support changing microphone");
741       g_simple_async_result_complete_in_idle (simple);
742       g_object_unref (simple);
743       return;
744     }
745
746   source_output_idx = empathy_audio_src_get_mic_index (src);
747
748   if (source_output_idx == PA_INVALID_INDEX)
749     {
750       g_simple_async_result_set_error (simple, G_IO_ERROR, G_IO_ERROR_FAILED,
751           "pulsesrc is not yet PLAYING");
752       g_simple_async_result_complete_in_idle (simple);
753       g_object_unref (simple);
754       return;
755     }
756
757   empathy_mic_monitor_change_microphone_async (priv->mic_monitor,
758       source_output_idx, microphone, empathy_audio_src_change_microphone_cb,
759       simple);
760 }
761
762 gboolean
763 empathy_audio_src_change_microphone_finish (EmpathyGstAudioSrc *src,
764     GAsyncResult *result,
765     GError **error)
766 {
767   empathy_implement_finish_void (src,
768       empathy_audio_src_change_microphone_async);
769 }