2 * empathy-gst-audio-src.c - Source for EmpathyGstAudioSrc
3 * Copyright (C) 2008 Collabora Ltd.
4 * @author Sjoerd Simons <sjoerd.simons@collabora.co.uk>
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.
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.
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
26 #include <gst/interfaces/streamvolume.h>
28 #include <libempathy/empathy-utils.h>
29 #include <libempathy-gtk/empathy-call-utils.h>
31 #include "empathy-audio-src.h"
33 #include "empathy-mic-monitor.h"
35 #define DEBUG_FLAG EMPATHY_DEBUG_VOIP
36 #include <libempathy/empathy-debug.h>
38 G_DEFINE_TYPE(EmpathyGstAudioSrc, empathy_audio_src, GST_TYPE_BIN)
48 static guint signals[LAST_SIGNAL] = {0};
58 /* private structure */
59 struct _EmpathyGstAudioSrcPrivate
61 gboolean dispose_has_run;
64 GstElement *volume_element;
66 EmpathyMicMonitor *mic_monitor;
68 /* 0 if not known yet */
69 guint source_output_idx;
70 /* G_MAXUINT if not known yet */
78 gboolean have_stream_volume;
85 #define EMPATHY_GST_AUDIO_SRC_GET_PRIVATE(o) \
86 (G_TYPE_INSTANCE_GET_PRIVATE ((o), EMPATHY_TYPE_GST_AUDIO_SRC, \
87 EmpathyGstAudioSrcPrivate))
91 empathy_audio_src_volume_changed (GObject *object,
96 empathy_audio_set_hw_mute (EmpathyGstAudioSrc *self, gboolean mute)
98 if (mute == self->priv->mute)
101 if (self->priv->have_stream_volume)
102 g_object_set (self->priv->src, "mute", mute, NULL);
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);
110 self->priv->mute = mute;
114 empathy_audio_src_get_hw_mute (EmpathyGstAudioSrc *self)
117 g_object_get (self->priv->src, "mute", &result, NULL);
123 empathy_audio_src_set_hw_volume (EmpathyGstAudioSrc *self,
126 if (volume == self->priv->volume)
129 if (self->priv->have_stream_volume)
130 g_object_set (self->priv->src, "volume", volume, NULL);
131 self->priv->volume = volume;
135 empathy_audio_src_get_hw_volume (EmpathyGstAudioSrc *self)
138 g_object_get (self->priv->src, "volume", &result, NULL);
145 empathy_audio_src_supports_changing_mic (EmpathyGstAudioSrc *self)
147 EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (self);
148 GObjectClass *object_class;
150 object_class = G_OBJECT_GET_CLASS (priv->src);
152 return (g_object_class_find_property (object_class,
153 "source-output-index") != NULL);
157 empathy_audio_src_get_mic_index (EmpathyGstAudioSrc *self)
159 EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (self);
160 guint audio_src_idx = PA_INVALID_INDEX;
162 if (empathy_audio_src_supports_changing_mic (self))
163 g_object_get (priv->src,
164 "source-output-index", &audio_src_idx,
167 return audio_src_idx;
171 empathy_audio_src_microphone_changed_cb (EmpathyMicMonitor *monitor,
172 guint source_output_idx,
176 EmpathyGstAudioSrc *self = user_data;
177 EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (self);
180 audio_src_idx = empathy_audio_src_get_mic_index (self);
182 if (source_output_idx == PA_INVALID_INDEX
183 || source_output_idx != audio_src_idx)
186 if (priv->source_idx == source_idx)
189 priv->source_idx = source_idx;
190 g_object_notify (G_OBJECT (self), "microphone");
194 empathy_audio_src_get_current_mic_cb (GObject *source_object,
195 GAsyncResult *result,
198 EmpathyMicMonitor *monitor = EMPATHY_MIC_MONITOR (source_object);
199 EmpathyGstAudioSrc *self = user_data;
200 EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (self);
202 GError *error = NULL;
204 source_idx = empathy_mic_monitor_get_current_mic_finish (monitor, result, &error);
208 DEBUG ("Failed to get current mic: %s", error->message);
209 g_clear_error (&error);
213 if (priv->source_idx == source_idx)
216 priv->source_idx = source_idx;
217 g_object_notify (G_OBJECT (self), "microphone");
221 empathy_audio_src_source_output_index_notify (GObject *object,
223 EmpathyGstAudioSrc *self)
225 EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (self);
226 guint source_output_idx;
228 source_output_idx = empathy_audio_src_get_mic_index (self);
230 if (source_output_idx == PA_INVALID_INDEX)
233 if (priv->source_output_idx == source_output_idx)
236 /* It's actually changed. */
237 priv->source_output_idx = source_output_idx;
239 empathy_mic_monitor_get_current_mic_async (priv->mic_monitor,
240 source_output_idx, empathy_audio_src_get_current_mic_cb, self);
247 const gchar *description;
249 description = g_getenv ("EMPATHY_AUDIO_SRC");
251 if (description != NULL)
253 GError *error = NULL;
255 src = gst_parse_bin_from_description (description, TRUE, &error);
258 DEBUG ("Failed to create bin %s: %s", description, error->message);
259 g_error_free (error);
265 /* Use pulsesrc as default */
266 src = gst_element_factory_make ("pulsesrc", NULL);
270 empathy_call_set_stream_properties (src, TRUE);
272 /* Set latency (buffering on the PulseAudio side) of 20ms */
273 g_object_set (src, "buffer-time", (gint64) 20000, NULL);
279 empathy_audio_src_init (EmpathyGstAudioSrc *obj)
281 EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (obj);
283 GstElement *capsfilter;
287 priv->peak_level = -G_MAXDOUBLE;
288 g_mutex_init (&priv->lock);
292 priv->src = create_src ();
293 if (priv->src == NULL)
296 if (GST_IS_STREAM_VOLUME (priv->src))
301 priv->have_stream_volume = TRUE;
302 /* We can't do a bidirection bind as the ::notify comes from another
303 * thread, for other bits of empathy it's most simpler if it comes from
305 g_object_bind_property (obj, "volume", priv->src, "volume",
307 g_object_bind_property (obj, "mute", priv->src, "mute",
310 /* sync and callback for bouncing */
311 g_object_get (priv->src, "volume", &volume, NULL);
312 g_object_set (obj, "volume", volume, NULL);
314 g_object_get (priv->src, "mute", &mute, NULL);
315 g_object_set (obj, "mute", mute, NULL);
317 g_signal_connect (priv->src, "notify::volume",
318 G_CALLBACK (empathy_audio_src_volume_changed), obj);
319 g_signal_connect (priv->src, "notify::mute",
320 G_CALLBACK (empathy_audio_src_volume_changed), obj);
324 g_message ("No stream volume available :(, mute will work though");
325 priv->have_stream_volume = FALSE;
328 gst_bin_add (GST_BIN (obj), priv->src);
330 /* Explicitly state what format we want from pulsesrc. This pushes resampling
331 * and format conversion as early as possible, lowering the amount of data
332 * transferred and thus improving performance. When moving to GStreamer
333 * 0.11/1.0, this should change so that we actually request what the encoder
334 * wants downstream. */
335 caps = gst_caps_new_simple ("audio/x-raw-int",
336 "channels", G_TYPE_INT, 1,
337 "width", G_TYPE_INT, 16,
338 "depth", G_TYPE_INT, 16,
339 "rate", G_TYPE_INT, 32000,
341 capsfilter = gst_element_factory_make ("capsfilter", NULL);
342 g_object_set (G_OBJECT (capsfilter), "caps", caps, NULL);
343 gst_bin_add (GST_BIN (obj), capsfilter);
344 gst_element_link (priv->src, capsfilter);
346 priv->volume_element = gst_element_factory_make ("volume", NULL);
347 gst_bin_add (GST_BIN (obj), priv->volume_element);
348 gst_element_link (capsfilter, priv->volume_element);
350 priv->level = gst_element_factory_make ("level", NULL);
351 gst_bin_add (GST_BIN (obj), priv->level);
352 gst_element_link (priv->volume_element, priv->level);
354 src = gst_element_get_static_pad (priv->level, "src");
356 ghost = gst_ghost_pad_new ("src", src);
357 gst_element_add_pad (GST_ELEMENT (obj), ghost);
359 gst_object_unref (G_OBJECT (src));
361 /* Listen to changes to GstPulseSrc:source-output-index so we know when
362 * it's no longer PA_INVALID_INDEX (starting for the first time) or if it
363 * changes (READY->NULL->READY...) */
364 g_signal_connect (priv->src, "notify::source-output-index",
365 G_CALLBACK (empathy_audio_src_source_output_index_notify),
368 priv->mic_monitor = empathy_mic_monitor_new ();
369 g_signal_connect (priv->mic_monitor, "microphone-changed",
370 G_CALLBACK (empathy_audio_src_microphone_changed_cb), obj);
372 priv->source_idx = PA_INVALID_INDEX;
375 static void empathy_audio_src_dispose (GObject *object);
376 static void empathy_audio_src_finalize (GObject *object);
377 static void empathy_audio_src_handle_message (GstBin *bin,
378 GstMessage *message);
380 static gboolean empathy_audio_src_levels_updated (gpointer user_data);
383 empathy_audio_src_set_property (GObject *object,
384 guint property_id, const GValue *value, GParamSpec *pspec)
389 empathy_audio_src_set_hw_volume (EMPATHY_GST_AUDIO_SRC (object),
390 g_value_get_double (value));
393 empathy_audio_set_hw_mute (EMPATHY_GST_AUDIO_SRC (object),
394 g_value_get_boolean (value));
397 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
402 empathy_audio_src_get_property (GObject *object,
403 guint property_id, GValue *value, GParamSpec *pspec)
405 EmpathyGstAudioSrc *self = EMPATHY_GST_AUDIO_SRC (object);
406 EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (self);
411 g_value_set_double (value, priv->volume);
414 g_value_set_boolean (value, priv->mute);
416 case PROP_PEAK_LEVEL:
417 g_mutex_lock (&priv->lock);
418 g_value_set_double (value, priv->peak_level);
419 g_mutex_unlock (&priv->lock);
422 g_mutex_lock (&priv->lock);
423 g_value_set_double (value, priv->rms_level);
424 g_mutex_unlock (&priv->lock);
426 case PROP_MICROPHONE:
427 g_value_set_uint (value, priv->source_idx);
430 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
435 empathy_audio_src_class_init (EmpathyGstAudioSrcClass
436 *empathy_audio_src_class)
438 GObjectClass *object_class = G_OBJECT_CLASS (empathy_audio_src_class);
439 GstBinClass *gstbin_class = GST_BIN_CLASS (empathy_audio_src_class);
440 GParamSpec *param_spec;
442 g_type_class_add_private (empathy_audio_src_class,
443 sizeof (EmpathyGstAudioSrcPrivate));
445 object_class->dispose = empathy_audio_src_dispose;
446 object_class->finalize = empathy_audio_src_finalize;
448 object_class->set_property = empathy_audio_src_set_property;
449 object_class->get_property = empathy_audio_src_get_property;
451 gstbin_class->handle_message =
452 GST_DEBUG_FUNCPTR (empathy_audio_src_handle_message);
454 param_spec = g_param_spec_double ("volume", "Volume", "volume contol",
456 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
457 g_object_class_install_property (object_class, PROP_VOLUME, param_spec);
459 param_spec = g_param_spec_boolean ("mute", "Mute", "mute contol",
461 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
462 g_object_class_install_property (object_class, PROP_MUTE, param_spec);
464 param_spec = g_param_spec_double ("peak-level", "peak level", "peak level",
465 -G_MAXDOUBLE, G_MAXDOUBLE, 0,
466 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
467 g_object_class_install_property (object_class, PROP_PEAK_LEVEL, param_spec);
469 param_spec = g_param_spec_uint ("microphone", "microphone", "microphone",
470 0, G_MAXUINT, G_MAXUINT,
471 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
472 g_object_class_install_property (object_class, PROP_MICROPHONE, param_spec);
474 signals[PEAK_LEVEL_CHANGED] = g_signal_new ("peak-level-changed",
475 G_TYPE_FROM_CLASS (empathy_audio_src_class),
479 g_cclosure_marshal_generic,
480 G_TYPE_NONE, 1, G_TYPE_DOUBLE);
482 param_spec = g_param_spec_double ("rms-level", "RMS level", "RMS level",
483 -G_MAXDOUBLE, G_MAXDOUBLE, 0,
484 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
485 g_object_class_install_property (object_class, PROP_RMS_LEVEL, param_spec);
487 signals[RMS_LEVEL_CHANGED] = g_signal_new ("rms-level-changed",
488 G_TYPE_FROM_CLASS (empathy_audio_src_class),
492 g_cclosure_marshal_generic,
493 G_TYPE_NONE, 1, G_TYPE_DOUBLE);
497 empathy_audio_src_dispose (GObject *object)
499 EmpathyGstAudioSrc *self = EMPATHY_GST_AUDIO_SRC (object);
500 EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (self);
502 if (priv->dispose_has_run)
505 priv->dispose_has_run = TRUE;
507 if (priv->level_idle_id != 0)
508 g_source_remove (priv->level_idle_id);
509 priv->level_idle_id = 0;
511 if (priv->volume_idle_id != 0)
512 g_source_remove (priv->volume_idle_id);
513 priv->volume_idle_id = 0;
515 tp_clear_object (&priv->mic_monitor);
517 /* release any references held by the object here */
519 if (G_OBJECT_CLASS (empathy_audio_src_parent_class)->dispose)
520 G_OBJECT_CLASS (empathy_audio_src_parent_class)->dispose (object);
524 empathy_audio_src_finalize (GObject *object)
526 EmpathyGstAudioSrc *self = EMPATHY_GST_AUDIO_SRC (object);
527 EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (self);
529 /* free any data held directly by the object here */
530 g_mutex_clear (&priv->lock);
532 G_OBJECT_CLASS (empathy_audio_src_parent_class)->finalize (object);
536 empathy_audio_src_levels_updated (gpointer user_data)
538 EmpathyGstAudioSrc *self = EMPATHY_GST_AUDIO_SRC (user_data);
539 EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (self);
541 g_mutex_lock (&priv->lock);
543 g_signal_emit (self, signals[PEAK_LEVEL_CHANGED], 0, priv->peak_level);
544 g_signal_emit (self, signals[RMS_LEVEL_CHANGED], 0, priv->rms_level);
545 priv->level_idle_id = 0;
547 g_mutex_unlock (&priv->lock);
553 empathy_audio_src_volume_changed_idle (gpointer user_data)
555 EmpathyGstAudioSrc *self = EMPATHY_GST_AUDIO_SRC (user_data);
556 EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (self);
560 g_mutex_lock (&priv->lock);
561 priv->volume_idle_id = 0;
562 g_mutex_unlock (&priv->lock);
564 volume = empathy_audio_src_get_hw_volume (self);
566 if (volume != priv->volume)
568 priv->volume = volume;
569 g_object_notify (G_OBJECT (self), "volume");
572 mute = empathy_audio_src_get_hw_mute (self);
573 if (mute != priv->mute)
576 /* hw mute changed, follow with own volume */
577 g_object_set (self->priv->volume_element, "mute", mute, NULL);
578 g_object_notify (G_OBJECT (self), "mute");
585 empathy_audio_src_volume_changed (GObject *object,
589 EmpathyGstAudioSrc *self = EMPATHY_GST_AUDIO_SRC (user_data);
591 g_mutex_lock (self->priv->lock);
592 if (self->priv->volume_idle_id == 0)
593 self->priv->volume_idle_id = g_idle_add (
594 empathy_audio_src_volume_changed_idle, self);
595 g_mutex_unlock (self->priv->lock);
601 empathy_audio_src_handle_message (GstBin *bin, GstMessage *message)
603 EmpathyGstAudioSrc *self = EMPATHY_GST_AUDIO_SRC (bin);
604 EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (self);
606 if (GST_MESSAGE_TYPE (message) == GST_MESSAGE_ELEMENT &&
607 GST_MESSAGE_SRC (message) == GST_OBJECT (priv->level))
609 const GstStructure *s;
613 gdouble peak = -G_MAXDOUBLE;
614 gdouble rms = -G_MAXDOUBLE;
616 s = gst_message_get_structure (message);
617 name = gst_structure_get_name (s);
619 if (g_strcmp0 ("level", name) != 0)
622 list = gst_structure_get_value (s, "peak");
623 len = gst_value_list_get_size (list);
625 for (i =0 ; i < len; i++)
630 value = gst_value_list_get_value (list, i);
631 db = g_value_get_double (value);
632 peak = MAX (db, peak);
635 list = gst_structure_get_value (s, "rms");
636 len = gst_value_list_get_size (list);
638 for (i =0 ; i < len; i++)
643 value = gst_value_list_get_value (list, i);
644 db = g_value_get_double (value);
648 g_mutex_lock (&priv->lock);
650 priv->peak_level = peak;
651 priv->rms_level = rms;
652 if (priv->level_idle_id == 0)
653 priv->level_idle_id = g_idle_add (
654 empathy_audio_src_levels_updated, self);
656 g_mutex_unlock (&priv->lock);
659 GST_BIN_CLASS (empathy_audio_src_parent_class)->handle_message (bin,
664 empathy_audio_src_new (void)
666 static gboolean registered = FALSE;
669 if (!gst_element_register (NULL, "empathyaudiosrc",
670 GST_RANK_NONE, EMPATHY_TYPE_GST_AUDIO_SRC))
674 return gst_element_factory_make ("empathyaudiosrc", NULL);
678 empathy_audio_src_set_echo_cancel (EmpathyGstAudioSrc *src,
681 DEBUG ("Src echo cancellation setting: %s", enable ? "on" : "off");
682 empathy_call_set_stream_properties (src->priv->src, enable);
686 empathy_audio_src_set_volume (EmpathyGstAudioSrc *src, gdouble volume)
688 g_object_set (src, "volume", volume, NULL);
692 empathy_audio_src_get_volume (EmpathyGstAudioSrc *src)
694 return src->priv->volume;
698 empathy_audio_src_get_microphone (EmpathyGstAudioSrc *src)
700 EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (src);
702 return priv->source_idx;
706 empathy_audio_src_change_microphone_cb (GObject *source_object,
707 GAsyncResult *result,
710 EmpathyMicMonitor *monitor = EMPATHY_MIC_MONITOR (source_object);
711 GSimpleAsyncResult *simple = user_data;
712 GError *error = NULL;
714 if (!empathy_mic_monitor_change_microphone_finish (monitor,
717 g_simple_async_result_take_error (simple, error);
720 g_simple_async_result_complete (simple);
721 g_object_unref (simple);
725 empathy_audio_src_change_microphone_async (EmpathyGstAudioSrc *src,
727 GAsyncReadyCallback callback,
730 EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (src);
731 guint source_output_idx;
732 GSimpleAsyncResult *simple;
734 simple = g_simple_async_result_new (G_OBJECT (src), callback, user_data,
735 empathy_audio_src_change_microphone_async);
737 if (!empathy_audio_src_supports_changing_mic (src))
739 g_simple_async_result_set_error (simple, G_IO_ERROR, G_IO_ERROR_FAILED,
740 "pulsesrc is not new enough to support changing microphone");
741 g_simple_async_result_complete_in_idle (simple);
742 g_object_unref (simple);
746 source_output_idx = empathy_audio_src_get_mic_index (src);
748 if (source_output_idx == PA_INVALID_INDEX)
750 g_simple_async_result_set_error (simple, G_IO_ERROR, G_IO_ERROR_FAILED,
751 "pulsesrc is not yet PLAYING");
752 g_simple_async_result_complete_in_idle (simple);
753 g_object_unref (simple);
757 empathy_mic_monitor_change_microphone_async (priv->mic_monitor,
758 source_output_idx, microphone, empathy_audio_src_change_microphone_cb,
763 empathy_audio_src_change_microphone_finish (EmpathyGstAudioSrc *src,
764 GAsyncResult *result,
767 empathy_implement_finish_void (src,
768 empathy_audio_src_change_microphone_async);