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