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