]> git.0d.be Git - empathy.git/blob - src/empathy-audio-src.c
remove released flag
[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   priv->lock = g_mutex_new ();
289   priv->volume = 1.0;
290
291   priv->src = create_src ();
292   if (priv->src == NULL)
293     return;
294
295   if (GST_IS_STREAM_VOLUME (priv->src))
296     {
297       gdouble volume;
298       gboolean mute;
299
300       priv->have_stream_volume = TRUE;
301       /* We can't do a bidirection bind as the ::notify comes from another
302        * thread, for other bits of empathy it's most simpler if it comes from
303        * the main thread */
304       g_object_bind_property (obj, "volume", priv->src, "volume",
305         G_BINDING_DEFAULT);
306       g_object_bind_property (obj, "mute", priv->src, "mute",
307         G_BINDING_DEFAULT);
308
309       /* sync and callback for bouncing */
310       g_object_get (priv->src, "volume", &volume, NULL);
311       g_object_set (obj, "volume", volume, NULL);
312
313       g_object_get (priv->src, "mute", &mute, NULL);
314       g_object_set (obj, "mute", mute, NULL);
315
316       g_signal_connect (priv->src, "notify::volume",
317         G_CALLBACK (empathy_audio_src_volume_changed), obj);
318       g_signal_connect (priv->src, "notify::mute",
319         G_CALLBACK (empathy_audio_src_volume_changed), obj);
320     }
321   else
322     {
323       g_message ("No stream volume available :(, mute will work though");
324       priv->have_stream_volume = FALSE;
325     }
326
327   gst_bin_add (GST_BIN (obj), priv->src);
328
329   /* Explicitly state what format we want from pulsesrc. This pushes resampling
330    * and format conversion as early as possible, lowering the amount of data
331    * transferred and thus improving performance. When moving to GStreamer
332    * 0.11/1.0, this should change so that we actually request what the encoder
333    * wants downstream. */
334   caps = gst_caps_new_simple ("audio/x-raw-int",
335       "channels", G_TYPE_INT, 1,
336       "width", G_TYPE_INT, 16,
337       "depth", G_TYPE_INT, 16,
338       "rate", G_TYPE_INT, 32000,
339       NULL);
340   capsfilter = gst_element_factory_make ("capsfilter", NULL);
341   g_object_set (G_OBJECT (capsfilter), "caps", caps, NULL);
342   gst_bin_add (GST_BIN (obj), capsfilter);
343   gst_element_link (priv->src, capsfilter);
344
345   priv->volume_element = gst_element_factory_make ("volume", NULL);
346   gst_bin_add (GST_BIN (obj), priv->volume_element);
347   gst_element_link (capsfilter, priv->volume_element);
348
349   priv->level = gst_element_factory_make ("level", NULL);
350   gst_bin_add (GST_BIN (obj), priv->level);
351   gst_element_link (priv->volume_element, priv->level);
352
353   src = gst_element_get_static_pad (priv->level, "src");
354
355   ghost = gst_ghost_pad_new ("src", src);
356   gst_element_add_pad (GST_ELEMENT (obj), ghost);
357
358   gst_object_unref (G_OBJECT (src));
359
360   /* Listen to changes to GstPulseSrc:source-output-index so we know when
361    * it's no longer PA_INVALID_INDEX (starting for the first time) or if it
362    * changes (READY->NULL->READY...) */
363   g_signal_connect (priv->src, "notify::source-output-index",
364       G_CALLBACK (empathy_audio_src_source_output_index_notify),
365       obj);
366
367   priv->mic_monitor = empathy_mic_monitor_new ();
368   g_signal_connect (priv->mic_monitor, "microphone-changed",
369       G_CALLBACK (empathy_audio_src_microphone_changed_cb), obj);
370
371   priv->source_idx = PA_INVALID_INDEX;
372 }
373
374 static void empathy_audio_src_dispose (GObject *object);
375 static void empathy_audio_src_finalize (GObject *object);
376 static void empathy_audio_src_handle_message (GstBin *bin,
377   GstMessage *message);
378
379 static gboolean empathy_audio_src_levels_updated (gpointer user_data);
380
381 static void
382 empathy_audio_src_set_property (GObject *object,
383   guint property_id, const GValue *value, GParamSpec *pspec)
384 {
385   switch (property_id)
386     {
387       case PROP_VOLUME:
388         empathy_audio_src_set_hw_volume (EMPATHY_GST_AUDIO_SRC (object),
389           g_value_get_double (value));
390         break;
391       case PROP_MUTE:
392         empathy_audio_set_hw_mute (EMPATHY_GST_AUDIO_SRC (object),
393           g_value_get_boolean (value));
394         break;
395       default:
396         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
397     }
398 }
399
400 static void
401 empathy_audio_src_get_property (GObject *object,
402   guint property_id, GValue *value, GParamSpec *pspec)
403 {
404   EmpathyGstAudioSrc *self = EMPATHY_GST_AUDIO_SRC (object);
405   EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (self);
406
407   switch (property_id)
408     {
409       case PROP_VOLUME:
410         g_value_set_double (value, priv->volume);
411         break;
412       case PROP_MUTE:
413         g_value_set_boolean (value, priv->mute);
414         break;
415       case PROP_PEAK_LEVEL:
416         g_mutex_lock (priv->lock);
417         g_value_set_double (value, priv->peak_level);
418         g_mutex_unlock (priv->lock);
419         break;
420       case PROP_RMS_LEVEL:
421         g_mutex_lock (priv->lock);
422         g_value_set_double (value, priv->rms_level);
423         g_mutex_unlock (priv->lock);
424         break;
425       case PROP_MICROPHONE:
426         g_value_set_uint (value, priv->source_idx);
427         break;
428       default:
429         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
430     }
431 }
432
433 static void
434 empathy_audio_src_class_init (EmpathyGstAudioSrcClass
435   *empathy_audio_src_class)
436 {
437   GObjectClass *object_class = G_OBJECT_CLASS (empathy_audio_src_class);
438   GstBinClass *gstbin_class = GST_BIN_CLASS (empathy_audio_src_class);
439   GParamSpec *param_spec;
440
441   g_type_class_add_private (empathy_audio_src_class,
442     sizeof (EmpathyGstAudioSrcPrivate));
443
444   object_class->dispose = empathy_audio_src_dispose;
445   object_class->finalize = empathy_audio_src_finalize;
446
447   object_class->set_property = empathy_audio_src_set_property;
448   object_class->get_property = empathy_audio_src_get_property;
449
450   gstbin_class->handle_message =
451     GST_DEBUG_FUNCPTR (empathy_audio_src_handle_message);
452
453   param_spec = g_param_spec_double ("volume", "Volume", "volume contol",
454     0.0, 5.0, 1.0,
455     G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
456   g_object_class_install_property (object_class, PROP_VOLUME, param_spec);
457
458   param_spec = g_param_spec_boolean ("mute", "Mute", "mute contol",
459     FALSE,
460     G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
461   g_object_class_install_property (object_class, PROP_MUTE, param_spec);
462
463   param_spec = g_param_spec_double ("peak-level", "peak level", "peak level",
464     -G_MAXDOUBLE, G_MAXDOUBLE, 0,
465     G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
466   g_object_class_install_property (object_class, PROP_PEAK_LEVEL, param_spec);
467
468   param_spec = g_param_spec_uint ("microphone", "microphone", "microphone",
469     0, G_MAXUINT, G_MAXUINT,
470     G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
471   g_object_class_install_property (object_class, PROP_MICROPHONE, param_spec);
472
473   signals[PEAK_LEVEL_CHANGED] = g_signal_new ("peak-level-changed",
474     G_TYPE_FROM_CLASS (empathy_audio_src_class),
475     G_SIGNAL_RUN_LAST,
476     0,
477     NULL, NULL,
478     g_cclosure_marshal_generic,
479     G_TYPE_NONE, 1, G_TYPE_DOUBLE);
480
481   param_spec = g_param_spec_double ("rms-level", "RMS level", "RMS level",
482     -G_MAXDOUBLE, G_MAXDOUBLE, 0,
483     G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
484   g_object_class_install_property (object_class, PROP_RMS_LEVEL, param_spec);
485
486   signals[RMS_LEVEL_CHANGED] = g_signal_new ("rms-level-changed",
487     G_TYPE_FROM_CLASS (empathy_audio_src_class),
488     G_SIGNAL_RUN_LAST,
489     0,
490     NULL, NULL,
491     g_cclosure_marshal_generic,
492     G_TYPE_NONE, 1, G_TYPE_DOUBLE);
493 }
494
495 void
496 empathy_audio_src_dispose (GObject *object)
497 {
498   EmpathyGstAudioSrc *self = EMPATHY_GST_AUDIO_SRC (object);
499   EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (self);
500
501   if (priv->dispose_has_run)
502     return;
503
504   priv->dispose_has_run = TRUE;
505
506   if (priv->level_idle_id != 0)
507     g_source_remove (priv->level_idle_id);
508   priv->level_idle_id = 0;
509
510   if (priv->volume_idle_id != 0)
511     g_source_remove (priv->volume_idle_id);
512   priv->volume_idle_id = 0;
513
514   tp_clear_object (&priv->mic_monitor);
515
516   /* release any references held by the object here */
517
518   if (G_OBJECT_CLASS (empathy_audio_src_parent_class)->dispose)
519     G_OBJECT_CLASS (empathy_audio_src_parent_class)->dispose (object);
520 }
521
522 void
523 empathy_audio_src_finalize (GObject *object)
524 {
525   EmpathyGstAudioSrc *self = EMPATHY_GST_AUDIO_SRC (object);
526   EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (self);
527
528   /* free any data held directly by the object here */
529   g_mutex_free (priv->lock);
530
531   G_OBJECT_CLASS (empathy_audio_src_parent_class)->finalize (object);
532 }
533
534 static gboolean
535 empathy_audio_src_levels_updated (gpointer user_data)
536 {
537   EmpathyGstAudioSrc *self = EMPATHY_GST_AUDIO_SRC (user_data);
538   EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (self);
539
540   g_mutex_lock (priv->lock);
541
542   g_signal_emit (self, signals[PEAK_LEVEL_CHANGED], 0, priv->peak_level);
543   g_signal_emit (self, signals[RMS_LEVEL_CHANGED], 0, priv->rms_level);
544   priv->level_idle_id = 0;
545
546   g_mutex_unlock (priv->lock);
547
548   return FALSE;
549 }
550
551 static gboolean
552 empathy_audio_src_volume_changed_idle (gpointer user_data)
553 {
554   EmpathyGstAudioSrc *self = EMPATHY_GST_AUDIO_SRC (user_data);
555   EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (self);
556   gdouble volume;
557   gboolean mute;
558
559   g_mutex_lock (priv->lock);
560   priv->volume_idle_id = 0;
561   g_mutex_unlock (priv->lock);
562
563   volume = empathy_audio_src_get_hw_volume (self);
564
565   if (volume != priv->volume)
566     {
567       priv->volume = volume;
568       g_object_notify (G_OBJECT (self), "volume");
569     }
570
571   mute = empathy_audio_src_get_hw_mute (self);
572   if (mute != priv->mute)
573     {
574       priv->mute = mute;
575       /* hw mute changed, follow with own volume */
576       g_object_set (self->priv->volume_element, "mute", mute, NULL);
577       g_object_notify (G_OBJECT (self), "mute");
578     }
579
580   return FALSE;
581 }
582
583 static gboolean
584 empathy_audio_src_volume_changed (GObject *object,
585   GParamSpec *pspec,
586   gpointer user_data)
587 {
588   EmpathyGstAudioSrc *self = EMPATHY_GST_AUDIO_SRC (user_data);
589
590   g_mutex_lock (self->priv->lock);
591   if (self->priv->volume_idle_id == 0)
592     self->priv->volume_idle_id = g_idle_add (
593       empathy_audio_src_volume_changed_idle, self);
594   g_mutex_unlock (self->priv->lock);
595
596   return FALSE;
597 }
598
599 static void
600 empathy_audio_src_handle_message (GstBin *bin, GstMessage *message)
601 {
602   EmpathyGstAudioSrc *self = EMPATHY_GST_AUDIO_SRC (bin);
603   EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (self);
604
605   if (GST_MESSAGE_TYPE (message) == GST_MESSAGE_ELEMENT &&
606       GST_MESSAGE_SRC (message) == GST_OBJECT (priv->level))
607     {
608       const GstStructure *s;
609       const gchar *name;
610       const GValue *list;
611       guint i, len;
612       gdouble peak = -G_MAXDOUBLE;
613       gdouble rms = -G_MAXDOUBLE;
614
615       s = gst_message_get_structure (message);
616       name = gst_structure_get_name (s);
617
618       if (g_strcmp0 ("level", name) != 0)
619         goto out;
620
621       list = gst_structure_get_value (s, "peak");
622       len = gst_value_list_get_size (list);
623
624       for (i =0 ; i < len; i++)
625         {
626           const GValue *value;
627           gdouble db;
628
629           value = gst_value_list_get_value (list, i);
630           db = g_value_get_double (value);
631           peak = MAX (db, peak);
632         }
633
634       list = gst_structure_get_value (s, "rms");
635       len = gst_value_list_get_size (list);
636
637       for (i =0 ; i < len; i++)
638         {
639           const GValue *value;
640           gdouble db;
641
642           value = gst_value_list_get_value (list, i);
643           db = g_value_get_double (value);
644           rms = MAX (db, rms);
645         }
646
647       g_mutex_lock (priv->lock);
648
649       priv->peak_level = peak;
650       priv->rms_level = rms;
651       if (priv->level_idle_id == 0)
652         priv->level_idle_id = g_idle_add (
653           empathy_audio_src_levels_updated, self);
654
655       g_mutex_unlock (priv->lock);
656     }
657 out:
658    GST_BIN_CLASS (empathy_audio_src_parent_class)->handle_message (bin,
659     message);
660 }
661
662 GstElement *
663 empathy_audio_src_new (void)
664 {
665   static gboolean registered = FALSE;
666
667   if (!registered) {
668     if (!gst_element_register (NULL, "empathyaudiosrc",
669             GST_RANK_NONE, EMPATHY_TYPE_GST_AUDIO_SRC))
670       return NULL;
671     registered = TRUE;
672   }
673   return gst_element_factory_make ("empathyaudiosrc", NULL);
674 }
675
676 void
677 empathy_audio_src_set_echo_cancel (EmpathyGstAudioSrc *src,
678   gboolean enable)
679 {
680   DEBUG ("Src echo cancellation setting: %s", enable ? "on" : "off");
681   empathy_call_set_stream_properties (src->priv->src, enable);
682 }
683
684 void
685 empathy_audio_src_set_volume (EmpathyGstAudioSrc *src, gdouble volume)
686 {
687   g_object_set (src, "volume", volume, NULL);
688 }
689
690 gdouble
691 empathy_audio_src_get_volume (EmpathyGstAudioSrc *src)
692 {
693   return src->priv->volume;
694 }
695
696 guint
697 empathy_audio_src_get_microphone (EmpathyGstAudioSrc *src)
698 {
699   EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (src);
700
701   return priv->source_idx;
702 }
703
704 static void
705 empathy_audio_src_change_microphone_cb (GObject *source_object,
706     GAsyncResult *result,
707     gpointer user_data)
708 {
709   EmpathyMicMonitor *monitor = EMPATHY_MIC_MONITOR (source_object);
710   GSimpleAsyncResult *simple = user_data;
711   GError *error = NULL;
712
713   if (!empathy_mic_monitor_change_microphone_finish (monitor,
714           result, &error))
715     {
716       g_simple_async_result_take_error (simple, error);
717     }
718
719   g_simple_async_result_complete (simple);
720   g_object_unref (simple);
721 }
722
723 void
724 empathy_audio_src_change_microphone_async (EmpathyGstAudioSrc *src,
725     guint microphone,
726     GAsyncReadyCallback callback,
727     gpointer user_data)
728 {
729   EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (src);
730   guint source_output_idx;
731   GSimpleAsyncResult *simple;
732
733   simple = g_simple_async_result_new (G_OBJECT (src), callback, user_data,
734       empathy_audio_src_change_microphone_async);
735
736   if (!empathy_audio_src_supports_changing_mic (src))
737     {
738       g_simple_async_result_set_error (simple, G_IO_ERROR, G_IO_ERROR_FAILED,
739           "pulsesrc is not new enough to support changing microphone");
740       g_simple_async_result_complete_in_idle (simple);
741       g_object_unref (simple);
742       return;
743     }
744
745   source_output_idx = empathy_audio_src_get_mic_index (src);
746
747   if (source_output_idx == PA_INVALID_INDEX)
748     {
749       g_simple_async_result_set_error (simple, G_IO_ERROR, G_IO_ERROR_FAILED,
750           "pulsesrc is not yet PLAYING");
751       g_simple_async_result_complete_in_idle (simple);
752       g_object_unref (simple);
753       return;
754     }
755
756   empathy_mic_monitor_change_microphone_async (priv->mic_monitor,
757       source_output_idx, microphone, empathy_audio_src_change_microphone_cb,
758       simple);
759 }
760
761 gboolean
762 empathy_audio_src_change_microphone_finish (EmpathyGstAudioSrc *src,
763     GAsyncResult *result,
764     GError **error)
765 {
766   empathy_implement_finish_void (src,
767       empathy_audio_src_change_microphone_async);
768 }