]> git.0d.be Git - empathy.git/blob - src/empathy-audio-src.c
Merge branch 'gnome-3-8'
[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 #include "empathy-audio-src.h"
23
24 #include <tp-account-widgets/tpaw-utils.h>
25
26 #ifdef HAVE_GST1
27 #include <gst/audio/streamvolume.h>
28 #else
29 #include <gst/interfaces/streamvolume.h>
30 #endif
31
32 #include "empathy-audio-utils.h"
33 #include "empathy-mic-monitor.h"
34 #include "empathy-utils.h"
35
36 #define DEBUG_FLAG EMPATHY_DEBUG_VOIP
37 #include "empathy-debug.h"
38
39 G_DEFINE_TYPE(EmpathyGstAudioSrc, empathy_audio_src, GST_TYPE_BIN)
40
41 enum {
42     PROP_VOLUME = 1,
43     PROP_MUTE,
44     PROP_MICROPHONE,
45 };
46
47 /* private structure */
48 struct _EmpathyGstAudioSrcPrivate
49 {
50   gboolean dispose_has_run;
51   GstElement *src;
52   GstElement *volume_element;
53
54   EmpathyMicMonitor *mic_monitor;
55
56   /* 0 if not known yet */
57   guint source_output_idx;
58   /* G_MAXUINT if not known yet */
59   guint source_idx;
60
61   gdouble volume;
62   gboolean mute;
63   gboolean have_stream_volume;
64
65   GMutex lock;
66   guint volume_idle_id;
67 };
68
69 #define EMPATHY_GST_AUDIO_SRC_GET_PRIVATE(o) \
70   (G_TYPE_INSTANCE_GET_PRIVATE ((o), EMPATHY_TYPE_GST_AUDIO_SRC, \
71   EmpathyGstAudioSrcPrivate))
72
73
74 static gboolean
75 empathy_audio_src_volume_changed (GObject *object,
76   GParamSpec *pspec,
77   gpointer user_data);
78
79 static void
80 empathy_audio_set_hw_mute (EmpathyGstAudioSrc *self, gboolean mute)
81 {
82   if (mute == self->priv->mute)
83     return;
84
85   if (self->priv->have_stream_volume)
86     g_object_set (self->priv->src, "mute", mute, NULL);
87
88   /* Belt and braces: If for some reason the underlying src doesn't mute
89    * correctly or doesn't update us when it unmutes correctly enforce it using
90    * our own volume element. Our UI can in no circumstances be made to think
91    * the input is muted while it's not */
92   g_object_set (self->priv->volume_element, "mute", mute, NULL);
93
94   self->priv->mute = mute;
95 }
96
97 static gboolean
98 empathy_audio_src_get_hw_mute (EmpathyGstAudioSrc *self)
99 {
100   gboolean result;
101   g_object_get (self->priv->src, "mute", &result, NULL);
102
103   return result;
104 }
105
106 static void
107 empathy_audio_src_set_hw_volume (EmpathyGstAudioSrc *self,
108     gdouble volume)
109 {
110   if (volume == self->priv->volume)
111     return;
112
113   if (self->priv->have_stream_volume)
114     g_object_set (self->priv->src, "volume", volume, NULL);
115   self->priv->volume = volume;
116 }
117
118 static gdouble
119 empathy_audio_src_get_hw_volume (EmpathyGstAudioSrc *self)
120 {
121   gdouble result;
122   g_object_get (self->priv->src, "volume", &result, NULL);
123
124   return result;
125 }
126
127
128 gboolean
129 empathy_audio_src_supports_changing_mic (EmpathyGstAudioSrc *self)
130 {
131   EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (self);
132   GObjectClass *object_class;
133
134   object_class = G_OBJECT_GET_CLASS (priv->src);
135
136   return (g_object_class_find_property (object_class,
137           "source-output-index") != NULL);
138 }
139
140 static guint
141 empathy_audio_src_get_mic_index (EmpathyGstAudioSrc *self)
142 {
143   EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (self);
144   guint audio_src_idx = PA_INVALID_INDEX;
145
146   if (empathy_audio_src_supports_changing_mic (self))
147     g_object_get (priv->src,
148       "source-output-index", &audio_src_idx,
149       NULL);
150
151   return audio_src_idx;
152 }
153
154 static void
155 empathy_audio_src_microphone_changed_cb (EmpathyMicMonitor *monitor,
156     guint source_output_idx,
157     guint source_idx,
158     gpointer user_data)
159 {
160   EmpathyGstAudioSrc *self = user_data;
161   EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (self);
162   guint audio_src_idx;
163
164   audio_src_idx = empathy_audio_src_get_mic_index (self);
165
166   if (source_output_idx == PA_INVALID_INDEX
167       || source_output_idx != audio_src_idx)
168     return;
169
170   if (priv->source_idx == source_idx)
171     return;
172
173   priv->source_idx = source_idx;
174   g_object_notify (G_OBJECT (self), "microphone");
175 }
176
177 static void
178 empathy_audio_src_get_current_mic_cb (GObject *source_object,
179     GAsyncResult *result,
180     gpointer user_data)
181 {
182   EmpathyMicMonitor *monitor = EMPATHY_MIC_MONITOR (source_object);
183   EmpathyGstAudioSrc *self = user_data;
184   EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (self);
185   guint source_idx;
186   GError *error = NULL;
187
188   source_idx = empathy_mic_monitor_get_current_mic_finish (monitor, result, &error);
189
190   if (error != NULL)
191     {
192       DEBUG ("Failed to get current mic: %s", error->message);
193       g_clear_error (&error);
194       return;
195     }
196
197   if (priv->source_idx == source_idx)
198     return;
199
200   priv->source_idx = source_idx;
201   g_object_notify (G_OBJECT (self), "microphone");
202 }
203
204 static void
205 empathy_audio_src_source_output_index_notify (GObject *object,
206     GParamSpec *pspec,
207     EmpathyGstAudioSrc *self)
208 {
209   EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (self);
210   guint source_output_idx;
211
212   source_output_idx = empathy_audio_src_get_mic_index (self);
213
214   if (source_output_idx == PA_INVALID_INDEX)
215     return;
216
217   if (priv->source_output_idx == source_output_idx)
218     return;
219
220   /* It's actually changed. */
221   priv->source_output_idx = source_output_idx;
222
223   empathy_mic_monitor_get_current_mic_async (priv->mic_monitor,
224       source_output_idx, empathy_audio_src_get_current_mic_cb, self);
225 }
226
227 static GstElement *
228 create_src (void)
229 {
230   GstElement *src;
231   const gchar *description;
232
233   description = g_getenv ("EMPATHY_AUDIO_SRC");
234
235   if (description != NULL)
236     {
237       GError *error = NULL;
238
239       src = gst_parse_bin_from_description (description, TRUE, &error);
240       if (src == NULL)
241         {
242           DEBUG ("Failed to create bin %s: %s", description, error->message);
243           g_error_free (error);
244         }
245
246       return src;
247     }
248
249   /* Use pulsesrc as default */
250   src = gst_element_factory_make ("pulsesrc", NULL);
251   if (src == NULL)
252     {
253       g_warning ("Missing 'pulsesrc' element");
254       return NULL;
255     }
256
257   empathy_audio_set_stream_properties (src, TRUE);
258
259   /* Set latency (buffering on the PulseAudio side) of 20ms */
260   g_object_set (src, "buffer-time", (gint64) 20000, NULL);
261
262   return src;
263 }
264
265 static void
266 empathy_audio_src_init (EmpathyGstAudioSrc *obj)
267 {
268   EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (obj);
269   GstPad *ghost, *src;
270
271   obj->priv = priv;
272   g_mutex_init (&priv->lock);
273
274   priv->volume = 1.0;
275
276   priv->src = create_src ();
277   if (priv->src == NULL)
278     return;
279
280   if (GST_IS_STREAM_VOLUME (priv->src))
281     {
282       gdouble volume;
283       gboolean mute;
284
285       priv->have_stream_volume = TRUE;
286       /* We can't do a bidirection bind as the ::notify comes from another
287        * thread, for other bits of empathy it's most simpler if it comes from
288        * the main thread */
289       g_object_bind_property (obj, "volume", priv->src, "volume",
290         G_BINDING_DEFAULT);
291       g_object_bind_property (obj, "mute", priv->src, "mute",
292         G_BINDING_DEFAULT);
293
294       /* sync and callback for bouncing */
295       g_object_get (priv->src, "volume", &volume, NULL);
296       g_object_set (obj, "volume", volume, NULL);
297
298       g_object_get (priv->src, "mute", &mute, NULL);
299       g_object_set (obj, "mute", mute, NULL);
300
301       g_signal_connect (priv->src, "notify::volume",
302         G_CALLBACK (empathy_audio_src_volume_changed), obj);
303       g_signal_connect (priv->src, "notify::mute",
304         G_CALLBACK (empathy_audio_src_volume_changed), obj);
305     }
306   else
307     {
308       g_message ("No stream volume available :(, mute will work though");
309       priv->have_stream_volume = FALSE;
310     }
311
312   gst_bin_add (GST_BIN (obj), priv->src);
313
314   priv->volume_element = gst_element_factory_make ("volume", NULL);
315   gst_bin_add (GST_BIN (obj), priv->volume_element);
316
317 #ifndef HAVE_GST1
318   {
319     GstElement *capsfilter;
320     GstCaps *caps;
321
322     /* Explicitly state what format we want from pulsesrc. This pushes resampling
323      * and format conversion as early as possible, lowering the amount of data
324      * transferred and thus improving performance. When moving to GStreamer
325      * 0.11/1.0, this should change so that we actually request what the encoder
326      * wants downstream. */
327     caps = gst_caps_new_simple ("audio/x-raw-int",
328         "channels", G_TYPE_INT, 1,
329         "width", G_TYPE_INT, 16,
330         "depth", G_TYPE_INT, 16,
331         "rate", G_TYPE_INT, 32000,
332         NULL);
333     capsfilter = gst_element_factory_make ("capsfilter", NULL);
334     g_object_set (G_OBJECT (capsfilter), "caps", caps, NULL);
335     gst_bin_add (GST_BIN (obj), capsfilter);
336     gst_element_link (priv->src, capsfilter);
337     gst_element_link (capsfilter, priv->volume_element);
338   }
339 #else
340   gst_element_link (priv->src, priv->volume_element);
341 #endif
342
343   src = gst_element_get_static_pad (priv->volume_element, "src");
344
345   ghost = gst_ghost_pad_new ("src", src);
346   gst_element_add_pad (GST_ELEMENT (obj), ghost);
347
348   gst_object_unref (G_OBJECT (src));
349
350   /* Listen to changes to GstPulseSrc:source-output-index so we know when
351    * it's no longer PA_INVALID_INDEX (starting for the first time) or if it
352    * changes (READY->NULL->READY...) */
353   g_signal_connect (priv->src, "notify::source-output-index",
354       G_CALLBACK (empathy_audio_src_source_output_index_notify),
355       obj);
356
357   priv->mic_monitor = empathy_mic_monitor_new ();
358   g_signal_connect (priv->mic_monitor, "microphone-changed",
359       G_CALLBACK (empathy_audio_src_microphone_changed_cb), obj);
360
361   priv->source_idx = PA_INVALID_INDEX;
362 }
363
364 static void empathy_audio_src_dispose (GObject *object);
365 static void empathy_audio_src_finalize (GObject *object);
366
367 static void
368 empathy_audio_src_set_property (GObject *object,
369   guint property_id, const GValue *value, GParamSpec *pspec)
370 {
371   switch (property_id)
372     {
373       case PROP_VOLUME:
374         empathy_audio_src_set_hw_volume (EMPATHY_GST_AUDIO_SRC (object),
375           g_value_get_double (value));
376         break;
377       case PROP_MUTE:
378         empathy_audio_set_hw_mute (EMPATHY_GST_AUDIO_SRC (object),
379           g_value_get_boolean (value));
380         break;
381       default:
382         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
383     }
384 }
385
386 static void
387 empathy_audio_src_get_property (GObject *object,
388   guint property_id, GValue *value, GParamSpec *pspec)
389 {
390   EmpathyGstAudioSrc *self = EMPATHY_GST_AUDIO_SRC (object);
391   EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (self);
392
393   switch (property_id)
394     {
395       case PROP_VOLUME:
396         g_value_set_double (value, priv->volume);
397         break;
398       case PROP_MUTE:
399         g_value_set_boolean (value, priv->mute);
400         break;
401       case PROP_MICROPHONE:
402         g_value_set_uint (value, priv->source_idx);
403         break;
404       default:
405         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
406     }
407 }
408
409 static void
410 empathy_audio_src_class_init (EmpathyGstAudioSrcClass
411   *empathy_audio_src_class)
412 {
413   GObjectClass *object_class = G_OBJECT_CLASS (empathy_audio_src_class);
414   GParamSpec *param_spec;
415
416   g_type_class_add_private (empathy_audio_src_class,
417     sizeof (EmpathyGstAudioSrcPrivate));
418
419   object_class->dispose = empathy_audio_src_dispose;
420   object_class->finalize = empathy_audio_src_finalize;
421
422   object_class->set_property = empathy_audio_src_set_property;
423   object_class->get_property = empathy_audio_src_get_property;
424
425   param_spec = g_param_spec_double ("volume", "Volume", "volume contol",
426     0.0, 5.0, 1.0,
427     G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
428   g_object_class_install_property (object_class, PROP_VOLUME, param_spec);
429
430   param_spec = g_param_spec_boolean ("mute", "Mute", "mute contol",
431     FALSE,
432     G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
433   g_object_class_install_property (object_class, PROP_MUTE, param_spec);
434
435   param_spec = g_param_spec_uint ("microphone", "microphone", "microphone",
436     0, G_MAXUINT, G_MAXUINT,
437     G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
438   g_object_class_install_property (object_class, PROP_MICROPHONE, param_spec);
439 }
440
441 void
442 empathy_audio_src_dispose (GObject *object)
443 {
444   EmpathyGstAudioSrc *self = EMPATHY_GST_AUDIO_SRC (object);
445   EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (self);
446
447   if (priv->dispose_has_run)
448     return;
449
450   priv->dispose_has_run = TRUE;
451
452   if (priv->volume_idle_id != 0)
453     g_source_remove (priv->volume_idle_id);
454   priv->volume_idle_id = 0;
455
456   tp_clear_object (&priv->mic_monitor);
457
458   /* release any references held by the object here */
459
460   if (G_OBJECT_CLASS (empathy_audio_src_parent_class)->dispose)
461     G_OBJECT_CLASS (empathy_audio_src_parent_class)->dispose (object);
462 }
463
464 void
465 empathy_audio_src_finalize (GObject *object)
466 {
467   EmpathyGstAudioSrc *self = EMPATHY_GST_AUDIO_SRC (object);
468   EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (self);
469
470   /* free any data held directly by the object here */
471   g_mutex_clear (&priv->lock);
472
473   G_OBJECT_CLASS (empathy_audio_src_parent_class)->finalize (object);
474 }
475
476 static gboolean
477 empathy_audio_src_volume_changed_idle (gpointer user_data)
478 {
479   EmpathyGstAudioSrc *self = EMPATHY_GST_AUDIO_SRC (user_data);
480   EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (self);
481   gdouble volume;
482   gboolean mute;
483
484   g_mutex_lock (&priv->lock);
485   priv->volume_idle_id = 0;
486   g_mutex_unlock (&priv->lock);
487
488   volume = empathy_audio_src_get_hw_volume (self);
489
490   if (volume != priv->volume)
491     {
492       priv->volume = volume;
493       g_object_notify (G_OBJECT (self), "volume");
494     }
495
496   mute = empathy_audio_src_get_hw_mute (self);
497   if (mute != priv->mute)
498     {
499       priv->mute = mute;
500       /* hw mute changed, follow with own volume */
501       g_object_set (self->priv->volume_element, "mute", mute, NULL);
502       g_object_notify (G_OBJECT (self), "mute");
503     }
504
505   return FALSE;
506 }
507
508 static gboolean
509 empathy_audio_src_volume_changed (GObject *object,
510   GParamSpec *pspec,
511   gpointer user_data)
512 {
513   EmpathyGstAudioSrc *self = EMPATHY_GST_AUDIO_SRC (user_data);
514
515   g_mutex_lock (&self->priv->lock);
516   if (self->priv->volume_idle_id == 0)
517     self->priv->volume_idle_id = g_idle_add (
518       empathy_audio_src_volume_changed_idle, self);
519   g_mutex_unlock (&self->priv->lock);
520
521   return FALSE;
522 }
523
524 GstElement *
525 empathy_audio_src_new (void)
526 {
527   static gboolean registered = FALSE;
528
529   if (!registered) {
530     if (!gst_element_register (NULL, "empathyaudiosrc",
531             GST_RANK_NONE, EMPATHY_TYPE_GST_AUDIO_SRC))
532       return NULL;
533     registered = TRUE;
534   }
535   return gst_element_factory_make ("empathyaudiosrc", NULL);
536 }
537
538 void
539 empathy_audio_src_set_echo_cancel (EmpathyGstAudioSrc *src,
540   gboolean enable)
541 {
542   DEBUG ("Src echo cancellation setting: %s", enable ? "on" : "off");
543   empathy_audio_set_stream_properties (src->priv->src, enable);
544 }
545
546 void
547 empathy_audio_src_set_volume (EmpathyGstAudioSrc *src, gdouble volume)
548 {
549   g_object_set (src, "volume", volume, NULL);
550 }
551
552 gdouble
553 empathy_audio_src_get_volume (EmpathyGstAudioSrc *src)
554 {
555   return src->priv->volume;
556 }
557
558 guint
559 empathy_audio_src_get_microphone (EmpathyGstAudioSrc *src)
560 {
561   EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (src);
562
563   return priv->source_idx;
564 }
565
566 static void
567 empathy_audio_src_change_microphone_cb (GObject *source_object,
568     GAsyncResult *result,
569     gpointer user_data)
570 {
571   EmpathyMicMonitor *monitor = EMPATHY_MIC_MONITOR (source_object);
572   GSimpleAsyncResult *simple = user_data;
573   GError *error = NULL;
574
575   if (!empathy_mic_monitor_change_microphone_finish (monitor,
576           result, &error))
577     {
578       g_simple_async_result_take_error (simple, error);
579     }
580
581   g_simple_async_result_complete (simple);
582   g_object_unref (simple);
583 }
584
585 void
586 empathy_audio_src_change_microphone_async (EmpathyGstAudioSrc *src,
587     guint microphone,
588     GAsyncReadyCallback callback,
589     gpointer user_data)
590 {
591   EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (src);
592   guint source_output_idx;
593   GSimpleAsyncResult *simple;
594
595   simple = g_simple_async_result_new (G_OBJECT (src), callback, user_data,
596       empathy_audio_src_change_microphone_async);
597
598   if (!empathy_audio_src_supports_changing_mic (src))
599     {
600       g_simple_async_result_set_error (simple, G_IO_ERROR, G_IO_ERROR_FAILED,
601           "pulsesrc is not new enough to support changing microphone");
602       g_simple_async_result_complete_in_idle (simple);
603       g_object_unref (simple);
604       return;
605     }
606
607   source_output_idx = empathy_audio_src_get_mic_index (src);
608
609   if (source_output_idx == PA_INVALID_INDEX)
610     {
611       g_simple_async_result_set_error (simple, G_IO_ERROR, G_IO_ERROR_FAILED,
612           "pulsesrc is not yet PLAYING");
613       g_simple_async_result_complete_in_idle (simple);
614       g_object_unref (simple);
615       return;
616     }
617
618   empathy_mic_monitor_change_microphone_async (priv->mic_monitor,
619       source_output_idx, microphone, empathy_audio_src_change_microphone_cb,
620       simple);
621 }
622
623 gboolean
624 empathy_audio_src_change_microphone_finish (EmpathyGstAudioSrc *src,
625     GAsyncResult *result,
626     GError **error)
627 {
628   tpaw_implement_finish_void (src,
629       empathy_audio_src_change_microphone_async);
630 }
631
632 void
633 empathy_audio_src_set_mute (EmpathyGstAudioSrc *self,
634     gboolean mute)
635 {
636   empathy_audio_set_hw_mute (self, mute);
637
638   g_object_notify (G_OBJECT (self), "mute");
639 }