]> git.0d.be Git - empathy.git/blob - src/empathy-audio-src.c
Port to gstreamer 1.0
[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 "empathy-audio-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 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     return NULL;
252
253   empathy_audio_set_stream_properties (src, TRUE);
254
255   /* Set latency (buffering on the PulseAudio side) of 20ms */
256   g_object_set (src, "buffer-time", (gint64) 20000, NULL);
257
258   return src;
259 }
260
261 static void
262 empathy_audio_src_init (EmpathyGstAudioSrc *obj)
263 {
264   EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (obj);
265   GstPad *ghost, *src;
266
267   obj->priv = priv;
268   g_mutex_init (&priv->lock);
269
270   priv->volume = 1.0;
271
272   priv->src = create_src ();
273   if (priv->src == NULL)
274     return;
275
276   if (GST_IS_STREAM_VOLUME (priv->src))
277     {
278       gdouble volume;
279       gboolean mute;
280
281       priv->have_stream_volume = TRUE;
282       /* We can't do a bidirection bind as the ::notify comes from another
283        * thread, for other bits of empathy it's most simpler if it comes from
284        * the main thread */
285       g_object_bind_property (obj, "volume", priv->src, "volume",
286         G_BINDING_DEFAULT);
287       g_object_bind_property (obj, "mute", priv->src, "mute",
288         G_BINDING_DEFAULT);
289
290       /* sync and callback for bouncing */
291       g_object_get (priv->src, "volume", &volume, NULL);
292       g_object_set (obj, "volume", volume, NULL);
293
294       g_object_get (priv->src, "mute", &mute, NULL);
295       g_object_set (obj, "mute", mute, NULL);
296
297       g_signal_connect (priv->src, "notify::volume",
298         G_CALLBACK (empathy_audio_src_volume_changed), obj);
299       g_signal_connect (priv->src, "notify::mute",
300         G_CALLBACK (empathy_audio_src_volume_changed), obj);
301     }
302   else
303     {
304       g_message ("No stream volume available :(, mute will work though");
305       priv->have_stream_volume = FALSE;
306     }
307
308   gst_bin_add (GST_BIN (obj), priv->src);
309
310 #ifndef HAVE_GST1
311   {
312     GstElement *capsfilter;
313     GstCaps *caps;
314
315     /* Explicitly state what format we want from pulsesrc. This pushes resampling
316      * and format conversion as early as possible, lowering the amount of data
317      * transferred and thus improving performance. When moving to GStreamer
318      * 0.11/1.0, this should change so that we actually request what the encoder
319      * wants downstream. */
320     caps = gst_caps_new_simple ("audio/x-raw-int",
321         "channels", G_TYPE_INT, 1,
322         "width", G_TYPE_INT, 16,
323         "depth", G_TYPE_INT, 16,
324         "rate", G_TYPE_INT, 32000,
325         NULL);
326     capsfilter = gst_element_factory_make ("capsfilter", NULL);
327     g_object_set (G_OBJECT (capsfilter), "caps", caps, NULL);
328     gst_bin_add (GST_BIN (obj), capsfilter);
329     gst_element_link (priv->src, capsfilter);
330   }
331 #endif
332
333   priv->volume_element = gst_element_factory_make ("volume", NULL);
334   gst_bin_add (GST_BIN (obj), priv->volume_element);
335   gst_element_link (priv->src, priv->volume_element);
336
337   src = gst_element_get_static_pad (priv->volume_element, "src");
338
339   ghost = gst_ghost_pad_new ("src", src);
340   gst_element_add_pad (GST_ELEMENT (obj), ghost);
341
342   gst_object_unref (G_OBJECT (src));
343
344   /* Listen to changes to GstPulseSrc:source-output-index so we know when
345    * it's no longer PA_INVALID_INDEX (starting for the first time) or if it
346    * changes (READY->NULL->READY...) */
347   g_signal_connect (priv->src, "notify::source-output-index",
348       G_CALLBACK (empathy_audio_src_source_output_index_notify),
349       obj);
350
351   priv->mic_monitor = empathy_mic_monitor_new ();
352   g_signal_connect (priv->mic_monitor, "microphone-changed",
353       G_CALLBACK (empathy_audio_src_microphone_changed_cb), obj);
354
355   priv->source_idx = PA_INVALID_INDEX;
356 }
357
358 static void empathy_audio_src_dispose (GObject *object);
359 static void empathy_audio_src_finalize (GObject *object);
360
361 static void
362 empathy_audio_src_set_property (GObject *object,
363   guint property_id, const GValue *value, GParamSpec *pspec)
364 {
365   switch (property_id)
366     {
367       case PROP_VOLUME:
368         empathy_audio_src_set_hw_volume (EMPATHY_GST_AUDIO_SRC (object),
369           g_value_get_double (value));
370         break;
371       case PROP_MUTE:
372         empathy_audio_set_hw_mute (EMPATHY_GST_AUDIO_SRC (object),
373           g_value_get_boolean (value));
374         break;
375       default:
376         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
377     }
378 }
379
380 static void
381 empathy_audio_src_get_property (GObject *object,
382   guint property_id, GValue *value, GParamSpec *pspec)
383 {
384   EmpathyGstAudioSrc *self = EMPATHY_GST_AUDIO_SRC (object);
385   EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (self);
386
387   switch (property_id)
388     {
389       case PROP_VOLUME:
390         g_value_set_double (value, priv->volume);
391         break;
392       case PROP_MUTE:
393         g_value_set_boolean (value, priv->mute);
394         break;
395       case PROP_MICROPHONE:
396         g_value_set_uint (value, priv->source_idx);
397         break;
398       default:
399         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
400     }
401 }
402
403 static void
404 empathy_audio_src_class_init (EmpathyGstAudioSrcClass
405   *empathy_audio_src_class)
406 {
407   GObjectClass *object_class = G_OBJECT_CLASS (empathy_audio_src_class);
408   GParamSpec *param_spec;
409
410   g_type_class_add_private (empathy_audio_src_class,
411     sizeof (EmpathyGstAudioSrcPrivate));
412
413   object_class->dispose = empathy_audio_src_dispose;
414   object_class->finalize = empathy_audio_src_finalize;
415
416   object_class->set_property = empathy_audio_src_set_property;
417   object_class->get_property = empathy_audio_src_get_property;
418
419   param_spec = g_param_spec_double ("volume", "Volume", "volume contol",
420     0.0, 5.0, 1.0,
421     G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
422   g_object_class_install_property (object_class, PROP_VOLUME, param_spec);
423
424   param_spec = g_param_spec_boolean ("mute", "Mute", "mute contol",
425     FALSE,
426     G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
427   g_object_class_install_property (object_class, PROP_MUTE, param_spec);
428
429   param_spec = g_param_spec_uint ("microphone", "microphone", "microphone",
430     0, G_MAXUINT, G_MAXUINT,
431     G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
432   g_object_class_install_property (object_class, PROP_MICROPHONE, param_spec);
433 }
434
435 void
436 empathy_audio_src_dispose (GObject *object)
437 {
438   EmpathyGstAudioSrc *self = EMPATHY_GST_AUDIO_SRC (object);
439   EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (self);
440
441   if (priv->dispose_has_run)
442     return;
443
444   priv->dispose_has_run = TRUE;
445
446   if (priv->volume_idle_id != 0)
447     g_source_remove (priv->volume_idle_id);
448   priv->volume_idle_id = 0;
449
450   tp_clear_object (&priv->mic_monitor);
451
452   /* release any references held by the object here */
453
454   if (G_OBJECT_CLASS (empathy_audio_src_parent_class)->dispose)
455     G_OBJECT_CLASS (empathy_audio_src_parent_class)->dispose (object);
456 }
457
458 void
459 empathy_audio_src_finalize (GObject *object)
460 {
461   EmpathyGstAudioSrc *self = EMPATHY_GST_AUDIO_SRC (object);
462   EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (self);
463
464   /* free any data held directly by the object here */
465   g_mutex_clear (&priv->lock);
466
467   G_OBJECT_CLASS (empathy_audio_src_parent_class)->finalize (object);
468 }
469
470 static gboolean
471 empathy_audio_src_volume_changed_idle (gpointer user_data)
472 {
473   EmpathyGstAudioSrc *self = EMPATHY_GST_AUDIO_SRC (user_data);
474   EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (self);
475   gdouble volume;
476   gboolean mute;
477
478   g_mutex_lock (&priv->lock);
479   priv->volume_idle_id = 0;
480   g_mutex_unlock (&priv->lock);
481
482   volume = empathy_audio_src_get_hw_volume (self);
483
484   if (volume != priv->volume)
485     {
486       priv->volume = volume;
487       g_object_notify (G_OBJECT (self), "volume");
488     }
489
490   mute = empathy_audio_src_get_hw_mute (self);
491   if (mute != priv->mute)
492     {
493       priv->mute = mute;
494       /* hw mute changed, follow with own volume */
495       g_object_set (self->priv->volume_element, "mute", mute, NULL);
496       g_object_notify (G_OBJECT (self), "mute");
497     }
498
499   return FALSE;
500 }
501
502 static gboolean
503 empathy_audio_src_volume_changed (GObject *object,
504   GParamSpec *pspec,
505   gpointer user_data)
506 {
507   EmpathyGstAudioSrc *self = EMPATHY_GST_AUDIO_SRC (user_data);
508
509   g_mutex_lock (&self->priv->lock);
510   if (self->priv->volume_idle_id == 0)
511     self->priv->volume_idle_id = g_idle_add (
512       empathy_audio_src_volume_changed_idle, self);
513   g_mutex_unlock (&self->priv->lock);
514
515   return FALSE;
516 }
517
518 GstElement *
519 empathy_audio_src_new (void)
520 {
521   static gboolean registered = FALSE;
522
523   if (!registered) {
524     if (!gst_element_register (NULL, "empathyaudiosrc",
525             GST_RANK_NONE, EMPATHY_TYPE_GST_AUDIO_SRC))
526       return NULL;
527     registered = TRUE;
528   }
529   return gst_element_factory_make ("empathyaudiosrc", NULL);
530 }
531
532 void
533 empathy_audio_src_set_echo_cancel (EmpathyGstAudioSrc *src,
534   gboolean enable)
535 {
536   DEBUG ("Src echo cancellation setting: %s", enable ? "on" : "off");
537   empathy_audio_set_stream_properties (src->priv->src, enable);
538 }
539
540 void
541 empathy_audio_src_set_volume (EmpathyGstAudioSrc *src, gdouble volume)
542 {
543   g_object_set (src, "volume", volume, NULL);
544 }
545
546 gdouble
547 empathy_audio_src_get_volume (EmpathyGstAudioSrc *src)
548 {
549   return src->priv->volume;
550 }
551
552 guint
553 empathy_audio_src_get_microphone (EmpathyGstAudioSrc *src)
554 {
555   EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (src);
556
557   return priv->source_idx;
558 }
559
560 static void
561 empathy_audio_src_change_microphone_cb (GObject *source_object,
562     GAsyncResult *result,
563     gpointer user_data)
564 {
565   EmpathyMicMonitor *monitor = EMPATHY_MIC_MONITOR (source_object);
566   GSimpleAsyncResult *simple = user_data;
567   GError *error = NULL;
568
569   if (!empathy_mic_monitor_change_microphone_finish (monitor,
570           result, &error))
571     {
572       g_simple_async_result_take_error (simple, error);
573     }
574
575   g_simple_async_result_complete (simple);
576   g_object_unref (simple);
577 }
578
579 void
580 empathy_audio_src_change_microphone_async (EmpathyGstAudioSrc *src,
581     guint microphone,
582     GAsyncReadyCallback callback,
583     gpointer user_data)
584 {
585   EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (src);
586   guint source_output_idx;
587   GSimpleAsyncResult *simple;
588
589   simple = g_simple_async_result_new (G_OBJECT (src), callback, user_data,
590       empathy_audio_src_change_microphone_async);
591
592   if (!empathy_audio_src_supports_changing_mic (src))
593     {
594       g_simple_async_result_set_error (simple, G_IO_ERROR, G_IO_ERROR_FAILED,
595           "pulsesrc is not new enough to support changing microphone");
596       g_simple_async_result_complete_in_idle (simple);
597       g_object_unref (simple);
598       return;
599     }
600
601   source_output_idx = empathy_audio_src_get_mic_index (src);
602
603   if (source_output_idx == PA_INVALID_INDEX)
604     {
605       g_simple_async_result_set_error (simple, G_IO_ERROR, G_IO_ERROR_FAILED,
606           "pulsesrc is not yet PLAYING");
607       g_simple_async_result_complete_in_idle (simple);
608       g_object_unref (simple);
609       return;
610     }
611
612   empathy_mic_monitor_change_microphone_async (priv->mic_monitor,
613       source_output_idx, microphone, empathy_audio_src_change_microphone_cb,
614       simple);
615 }
616
617 gboolean
618 empathy_audio_src_change_microphone_finish (EmpathyGstAudioSrc *src,
619     GAsyncResult *result,
620     GError **error)
621 {
622   empathy_implement_finish_void (src,
623       empathy_audio_src_change_microphone_async);
624 }
625
626 void
627 empathy_audio_src_set_mute (EmpathyGstAudioSrc *self,
628     gboolean mute)
629 {
630   empathy_audio_set_hw_mute (self, mute);
631
632   g_object_notify (G_OBJECT (self), "mute");
633 }