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 priv->lock = g_mutex_new ();
291 priv->src = create_src ();
292 if (priv->src == NULL)
295 if (GST_IS_STREAM_VOLUME (priv->src))
300 priv->have_stream_volume = TRUE;
301 /* We can't do a bidirection bind as the ::notify comes from another
302 * thread, for other bits of empathy it's most simpler if it comes from
304 g_object_bind_property (obj, "volume", priv->src, "volume",
306 g_object_bind_property (obj, "mute", priv->src, "mute",
309 /* sync and callback for bouncing */
310 g_object_get (priv->src, "volume", &volume, NULL);
311 g_object_set (obj, "volume", volume, NULL);
313 g_object_get (priv->src, "mute", &mute, NULL);
314 g_object_set (obj, "mute", mute, NULL);
316 g_signal_connect (priv->src, "notify::volume",
317 G_CALLBACK (empathy_audio_src_volume_changed), obj);
318 g_signal_connect (priv->src, "notify::mute",
319 G_CALLBACK (empathy_audio_src_volume_changed), obj);
323 g_message ("No stream volume available :(, mute will work though");
324 priv->have_stream_volume = FALSE;
327 gst_bin_add (GST_BIN (obj), priv->src);
329 /* Explicitly state what format we want from pulsesrc. This pushes resampling
330 * and format conversion as early as possible, lowering the amount of data
331 * transferred and thus improving performance. When moving to GStreamer
332 * 0.11/1.0, this should change so that we actually request what the encoder
333 * wants downstream. */
334 caps = gst_caps_new_simple ("audio/x-raw-int",
335 "channels", G_TYPE_INT, 1,
336 "width", G_TYPE_INT, 16,
337 "depth", G_TYPE_INT, 16,
338 "rate", G_TYPE_INT, 32000,
340 capsfilter = gst_element_factory_make ("capsfilter", NULL);
341 g_object_set (G_OBJECT (capsfilter), "caps", caps, NULL);
342 gst_bin_add (GST_BIN (obj), capsfilter);
343 gst_element_link (priv->src, capsfilter);
345 priv->volume_element = gst_element_factory_make ("volume", NULL);
346 gst_bin_add (GST_BIN (obj), priv->volume_element);
347 gst_element_link (capsfilter, priv->volume_element);
349 priv->level = gst_element_factory_make ("level", NULL);
350 gst_bin_add (GST_BIN (obj), priv->level);
351 gst_element_link (priv->volume_element, priv->level);
353 src = gst_element_get_static_pad (priv->level, "src");
355 ghost = gst_ghost_pad_new ("src", src);
356 gst_element_add_pad (GST_ELEMENT (obj), ghost);
358 gst_object_unref (G_OBJECT (src));
360 /* Listen to changes to GstPulseSrc:source-output-index so we know when
361 * it's no longer PA_INVALID_INDEX (starting for the first time) or if it
362 * changes (READY->NULL->READY...) */
363 g_signal_connect (priv->src, "notify::source-output-index",
364 G_CALLBACK (empathy_audio_src_source_output_index_notify),
367 priv->mic_monitor = empathy_mic_monitor_new ();
368 g_signal_connect (priv->mic_monitor, "microphone-changed",
369 G_CALLBACK (empathy_audio_src_microphone_changed_cb), obj);
371 priv->source_idx = PA_INVALID_INDEX;
374 static void empathy_audio_src_dispose (GObject *object);
375 static void empathy_audio_src_finalize (GObject *object);
376 static void empathy_audio_src_handle_message (GstBin *bin,
377 GstMessage *message);
379 static gboolean empathy_audio_src_levels_updated (gpointer user_data);
382 empathy_audio_src_set_property (GObject *object,
383 guint property_id, const GValue *value, GParamSpec *pspec)
388 empathy_audio_src_set_hw_volume (EMPATHY_GST_AUDIO_SRC (object),
389 g_value_get_double (value));
392 empathy_audio_set_hw_mute (EMPATHY_GST_AUDIO_SRC (object),
393 g_value_get_boolean (value));
396 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
401 empathy_audio_src_get_property (GObject *object,
402 guint property_id, GValue *value, GParamSpec *pspec)
404 EmpathyGstAudioSrc *self = EMPATHY_GST_AUDIO_SRC (object);
405 EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (self);
410 g_value_set_double (value, priv->volume);
413 g_value_set_boolean (value, priv->mute);
415 case PROP_PEAK_LEVEL:
416 g_mutex_lock (priv->lock);
417 g_value_set_double (value, priv->peak_level);
418 g_mutex_unlock (priv->lock);
421 g_mutex_lock (priv->lock);
422 g_value_set_double (value, priv->rms_level);
423 g_mutex_unlock (priv->lock);
425 case PROP_MICROPHONE:
426 g_value_set_uint (value, priv->source_idx);
429 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
434 empathy_audio_src_class_init (EmpathyGstAudioSrcClass
435 *empathy_audio_src_class)
437 GObjectClass *object_class = G_OBJECT_CLASS (empathy_audio_src_class);
438 GstBinClass *gstbin_class = GST_BIN_CLASS (empathy_audio_src_class);
439 GParamSpec *param_spec;
441 g_type_class_add_private (empathy_audio_src_class,
442 sizeof (EmpathyGstAudioSrcPrivate));
444 object_class->dispose = empathy_audio_src_dispose;
445 object_class->finalize = empathy_audio_src_finalize;
447 object_class->set_property = empathy_audio_src_set_property;
448 object_class->get_property = empathy_audio_src_get_property;
450 gstbin_class->handle_message =
451 GST_DEBUG_FUNCPTR (empathy_audio_src_handle_message);
453 param_spec = g_param_spec_double ("volume", "Volume", "volume contol",
455 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
456 g_object_class_install_property (object_class, PROP_VOLUME, param_spec);
458 param_spec = g_param_spec_boolean ("mute", "Mute", "mute contol",
460 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
461 g_object_class_install_property (object_class, PROP_MUTE, param_spec);
463 param_spec = g_param_spec_double ("peak-level", "peak level", "peak level",
464 -G_MAXDOUBLE, G_MAXDOUBLE, 0,
465 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
466 g_object_class_install_property (object_class, PROP_PEAK_LEVEL, param_spec);
468 param_spec = g_param_spec_uint ("microphone", "microphone", "microphone",
469 0, G_MAXUINT, G_MAXUINT,
470 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
471 g_object_class_install_property (object_class, PROP_MICROPHONE, param_spec);
473 signals[PEAK_LEVEL_CHANGED] = g_signal_new ("peak-level-changed",
474 G_TYPE_FROM_CLASS (empathy_audio_src_class),
478 g_cclosure_marshal_generic,
479 G_TYPE_NONE, 1, G_TYPE_DOUBLE);
481 param_spec = g_param_spec_double ("rms-level", "RMS level", "RMS level",
482 -G_MAXDOUBLE, G_MAXDOUBLE, 0,
483 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
484 g_object_class_install_property (object_class, PROP_RMS_LEVEL, param_spec);
486 signals[RMS_LEVEL_CHANGED] = g_signal_new ("rms-level-changed",
487 G_TYPE_FROM_CLASS (empathy_audio_src_class),
491 g_cclosure_marshal_generic,
492 G_TYPE_NONE, 1, G_TYPE_DOUBLE);
496 empathy_audio_src_dispose (GObject *object)
498 EmpathyGstAudioSrc *self = EMPATHY_GST_AUDIO_SRC (object);
499 EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (self);
501 if (priv->dispose_has_run)
504 priv->dispose_has_run = TRUE;
506 if (priv->level_idle_id != 0)
507 g_source_remove (priv->level_idle_id);
508 priv->level_idle_id = 0;
510 if (priv->volume_idle_id != 0)
511 g_source_remove (priv->volume_idle_id);
512 priv->volume_idle_id = 0;
514 tp_clear_object (&priv->mic_monitor);
516 /* release any references held by the object here */
518 if (G_OBJECT_CLASS (empathy_audio_src_parent_class)->dispose)
519 G_OBJECT_CLASS (empathy_audio_src_parent_class)->dispose (object);
523 empathy_audio_src_finalize (GObject *object)
525 EmpathyGstAudioSrc *self = EMPATHY_GST_AUDIO_SRC (object);
526 EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (self);
528 /* free any data held directly by the object here */
529 g_mutex_free (priv->lock);
531 G_OBJECT_CLASS (empathy_audio_src_parent_class)->finalize (object);
535 empathy_audio_src_levels_updated (gpointer user_data)
537 EmpathyGstAudioSrc *self = EMPATHY_GST_AUDIO_SRC (user_data);
538 EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (self);
540 g_mutex_lock (priv->lock);
542 g_signal_emit (self, signals[PEAK_LEVEL_CHANGED], 0, priv->peak_level);
543 g_signal_emit (self, signals[RMS_LEVEL_CHANGED], 0, priv->rms_level);
544 priv->level_idle_id = 0;
546 g_mutex_unlock (priv->lock);
552 empathy_audio_src_volume_changed_idle (gpointer user_data)
554 EmpathyGstAudioSrc *self = EMPATHY_GST_AUDIO_SRC (user_data);
555 EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (self);
559 g_mutex_lock (priv->lock);
560 priv->volume_idle_id = 0;
561 g_mutex_unlock (priv->lock);
563 volume = empathy_audio_src_get_hw_volume (self);
565 if (volume != priv->volume)
567 priv->volume = volume;
568 g_object_notify (G_OBJECT (self), "volume");
571 mute = empathy_audio_src_get_hw_mute (self);
572 if (mute != priv->mute)
575 /* hw mute changed, follow with own volume */
576 g_object_set (self->priv->volume_element, "mute", mute, NULL);
577 g_object_notify (G_OBJECT (self), "mute");
584 empathy_audio_src_volume_changed (GObject *object,
588 EmpathyGstAudioSrc *self = EMPATHY_GST_AUDIO_SRC (user_data);
590 g_mutex_lock (self->priv->lock);
591 if (self->priv->volume_idle_id == 0)
592 self->priv->volume_idle_id = g_idle_add (
593 empathy_audio_src_volume_changed_idle, self);
594 g_mutex_unlock (self->priv->lock);
600 empathy_audio_src_handle_message (GstBin *bin, GstMessage *message)
602 EmpathyGstAudioSrc *self = EMPATHY_GST_AUDIO_SRC (bin);
603 EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (self);
605 if (GST_MESSAGE_TYPE (message) == GST_MESSAGE_ELEMENT &&
606 GST_MESSAGE_SRC (message) == GST_OBJECT (priv->level))
608 const GstStructure *s;
612 gdouble peak = -G_MAXDOUBLE;
613 gdouble rms = -G_MAXDOUBLE;
615 s = gst_message_get_structure (message);
616 name = gst_structure_get_name (s);
618 if (g_strcmp0 ("level", name) != 0)
621 list = gst_structure_get_value (s, "peak");
622 len = gst_value_list_get_size (list);
624 for (i =0 ; i < len; i++)
629 value = gst_value_list_get_value (list, i);
630 db = g_value_get_double (value);
631 peak = MAX (db, peak);
634 list = gst_structure_get_value (s, "rms");
635 len = gst_value_list_get_size (list);
637 for (i =0 ; i < len; i++)
642 value = gst_value_list_get_value (list, i);
643 db = g_value_get_double (value);
647 g_mutex_lock (priv->lock);
649 priv->peak_level = peak;
650 priv->rms_level = rms;
651 if (priv->level_idle_id == 0)
652 priv->level_idle_id = g_idle_add (
653 empathy_audio_src_levels_updated, self);
655 g_mutex_unlock (priv->lock);
658 GST_BIN_CLASS (empathy_audio_src_parent_class)->handle_message (bin,
663 empathy_audio_src_new (void)
665 static gboolean registered = FALSE;
668 if (!gst_element_register (NULL, "empathyaudiosrc",
669 GST_RANK_NONE, EMPATHY_TYPE_GST_AUDIO_SRC))
673 return gst_element_factory_make ("empathyaudiosrc", NULL);
677 empathy_audio_src_set_echo_cancel (EmpathyGstAudioSrc *src,
680 DEBUG ("Src echo cancellation setting: %s", enable ? "on" : "off");
681 empathy_call_set_stream_properties (src->priv->src, enable);
685 empathy_audio_src_set_volume (EmpathyGstAudioSrc *src, gdouble volume)
687 g_object_set (src, "volume", volume, NULL);
691 empathy_audio_src_get_volume (EmpathyGstAudioSrc *src)
693 return src->priv->volume;
697 empathy_audio_src_get_microphone (EmpathyGstAudioSrc *src)
699 EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (src);
701 return priv->source_idx;
705 empathy_audio_src_change_microphone_cb (GObject *source_object,
706 GAsyncResult *result,
709 EmpathyMicMonitor *monitor = EMPATHY_MIC_MONITOR (source_object);
710 GSimpleAsyncResult *simple = user_data;
711 GError *error = NULL;
713 if (!empathy_mic_monitor_change_microphone_finish (monitor,
716 g_simple_async_result_take_error (simple, error);
719 g_simple_async_result_complete (simple);
720 g_object_unref (simple);
724 empathy_audio_src_change_microphone_async (EmpathyGstAudioSrc *src,
726 GAsyncReadyCallback callback,
729 EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (src);
730 guint source_output_idx;
731 GSimpleAsyncResult *simple;
733 simple = g_simple_async_result_new (G_OBJECT (src), callback, user_data,
734 empathy_audio_src_change_microphone_async);
736 if (!empathy_audio_src_supports_changing_mic (src))
738 g_simple_async_result_set_error (simple, G_IO_ERROR, G_IO_ERROR_FAILED,
739 "pulsesrc is not new enough to support changing microphone");
740 g_simple_async_result_complete_in_idle (simple);
741 g_object_unref (simple);
745 source_output_idx = empathy_audio_src_get_mic_index (src);
747 if (source_output_idx == PA_INVALID_INDEX)
749 g_simple_async_result_set_error (simple, G_IO_ERROR, G_IO_ERROR_FAILED,
750 "pulsesrc is not yet PLAYING");
751 g_simple_async_result_complete_in_idle (simple);
752 g_object_unref (simple);
756 empathy_mic_monitor_change_microphone_async (priv->mic_monitor,
757 source_output_idx, microphone, empathy_audio_src_change_microphone_cb,
762 empathy_audio_src_change_microphone_finish (EmpathyGstAudioSrc *src,
763 GAsyncResult *result,
766 empathy_implement_finish_void (src,
767 empathy_audio_src_change_microphone_async);