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/mixer.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};
57 /* private structure */
58 struct _EmpathyGstAudioSrcPrivate
60 gboolean dispose_has_run;
64 EmpathyMicMonitor *mic_monitor;
66 /* 0 if not known yet */
67 guint source_output_idx;
68 /* G_MAXUINT if not known yet */
75 /* the mixer track on src we follow and adjust */
83 #define EMPATHY_GST_AUDIO_SRC_GET_PRIVATE(o) \
84 (G_TYPE_INSTANCE_GET_PRIVATE ((o), EMPATHY_TYPE_GST_AUDIO_SRC, \
85 EmpathyGstAudioSrcPrivate))
87 /* There is no predefined maximum channels by gstreamer, just pick 32, which is
88 * the same as the pulseaudio maximum */
89 #define MAX_MIC_CHANNELS 32
92 empathy_audio_src_set_hw_volume (EmpathyGstAudioSrc *self, gdouble volume)
94 gint volumes[MAX_MIC_CHANNELS];
97 g_mutex_lock (self->priv->lock);
98 /* If there is no mixer available ignore the setting */
99 if (self->priv->track == NULL)
102 for (i = 0; i < MAX_MIC_CHANNELS; i++)
103 volumes[i] = self->priv->track->max_volume * volume;
105 gst_mixer_set_volume (GST_MIXER (self->priv->src),
106 self->priv->track, volumes);
109 g_mutex_unlock (self->priv->lock);
111 self->priv->volume = volume;
115 empathy_audio_src_get_hw_volume (EmpathyGstAudioSrc *self)
117 gint volumes[MAX_MIC_CHANNELS];
118 gdouble result = self->priv->volume;
120 g_mutex_lock (self->priv->lock);
121 if (self->priv->track == NULL)
124 gst_mixer_get_volume (GST_MIXER (self->priv->src),
125 self->priv->track, volumes);
126 result = volumes[0]/(gdouble)self->priv->track->max_volume;
129 g_mutex_unlock (self->priv->lock);
136 empathy_audio_src_supports_changing_mic (EmpathyGstAudioSrc *self)
138 EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (self);
139 GObjectClass *object_class;
141 object_class = G_OBJECT_GET_CLASS (priv->src);
143 return (g_object_class_find_property (object_class,
144 "source-output-index") != NULL);
148 empathy_audio_src_get_mic_index (EmpathyGstAudioSrc *self)
150 EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (self);
151 guint audio_src_idx = PA_INVALID_INDEX;
153 if (empathy_audio_src_supports_changing_mic (self))
154 g_object_get (priv->src,
155 "source-output-index", &audio_src_idx,
158 return audio_src_idx;
162 empathy_audio_src_microphone_changed_cb (EmpathyMicMonitor *monitor,
163 guint source_output_idx,
167 EmpathyGstAudioSrc *self = user_data;
168 EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (self);
171 audio_src_idx = empathy_audio_src_get_mic_index (self);
173 if (source_output_idx == PA_INVALID_INDEX
174 || source_output_idx != audio_src_idx)
177 if (priv->source_idx == source_idx)
180 priv->source_idx = source_idx;
181 g_object_notify (G_OBJECT (self), "microphone");
185 empathy_audio_src_get_current_mic_cb (GObject *source_object,
186 GAsyncResult *result,
189 EmpathyMicMonitor *monitor = EMPATHY_MIC_MONITOR (source_object);
190 EmpathyGstAudioSrc *self = user_data;
191 EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (self);
193 GError *error = NULL;
195 source_idx = empathy_mic_monitor_get_current_mic_finish (monitor, result, &error);
199 DEBUG ("Failed to get current mic: %s", error->message);
200 g_clear_error (&error);
204 if (priv->source_idx == source_idx)
207 priv->source_idx = source_idx;
208 g_object_notify (G_OBJECT (self), "microphone");
212 empathy_audio_src_source_output_index_notify (GObject *object,
214 EmpathyGstAudioSrc *self)
216 EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (self);
217 guint source_output_idx;
219 source_output_idx = empathy_audio_src_get_mic_index (self);
221 if (source_output_idx == PA_INVALID_INDEX)
224 if (priv->source_output_idx == source_output_idx)
227 /* It's actually changed. */
228 priv->source_output_idx = source_output_idx;
230 empathy_mic_monitor_get_current_mic_async (priv->mic_monitor,
231 source_output_idx, empathy_audio_src_get_current_mic_cb, self);
234 static GstMixerTrack *
235 empathy_audio_src_get_track (GstElement *src)
238 GstMixerTrack *track = NULL;
240 if (!gst_element_implements_interface (src, GST_TYPE_MIXER))
242 g_warning ("No mixer interface implementation, can't control volume");
246 for (t = gst_mixer_list_tracks (GST_MIXER (src));
247 t != NULL; t = g_list_next (t))
249 GstMixerTrack *tr = t->data;
250 if (!tp_strdiff (tr->label, "Master"))
259 g_warning ("No suitable track found");
261 else if (track->num_channels > MAX_MIC_CHANNELS)
263 g_warning ("Microphones with more then %d channels not supported ",
275 const gchar *description;
277 description = g_getenv ("EMPATHY_AUDIO_SRC");
279 if (description != NULL)
281 GError *error = NULL;
283 src = gst_parse_bin_from_description (description, TRUE, &error);
286 DEBUG ("Failed to create bin %s: %s", description, error->message);
287 g_error_free (error);
293 /* Use pulsesrc as default */
294 src = gst_element_factory_make ("pulsesrc", NULL);
298 empathy_call_set_stream_properties (src, TRUE);
304 empathy_audio_src_init (EmpathyGstAudioSrc *obj)
306 EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (obj);
308 GstElement *capsfilter;
312 priv->peak_level = -G_MAXDOUBLE;
313 priv->lock = g_mutex_new ();
316 priv->src = create_src ();
317 if (priv->src == NULL)
320 gst_bin_add (GST_BIN (obj), priv->src);
322 /* Explicitly state what format we want from pulsesrc. This pushes resampling
323 * and format conversion as early as possible, lowering the amount of data
324 * transferred and thus improving performance. When moving to GStreamer
325 * 0.11/1.0, this should change so that we actually request what the encoder
326 * wants downstream. */
327 caps = gst_caps_new_simple ("audio/x-raw-int",
328 "channels", G_TYPE_INT, 1,
329 "width", G_TYPE_INT, 16,
330 "depth", G_TYPE_INT, 16,
331 "rate", G_TYPE_INT, 32000,
333 capsfilter = gst_element_factory_make ("capsfilter", NULL);
334 g_object_set (G_OBJECT (capsfilter), "caps", caps, NULL);
335 gst_bin_add (GST_BIN (obj), capsfilter);
336 gst_element_link (priv->src, capsfilter);
338 priv->level = gst_element_factory_make ("level", NULL);
339 gst_bin_add (GST_BIN (obj), priv->level);
340 gst_element_link (capsfilter, priv->level);
342 src = gst_element_get_static_pad (priv->level, "src");
344 ghost = gst_ghost_pad_new ("src", src);
345 gst_element_add_pad (GST_ELEMENT (obj), ghost);
347 gst_object_unref (G_OBJECT (src));
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),
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);
360 priv->source_idx = PA_INVALID_INDEX;
363 static void empathy_audio_src_dispose (GObject *object);
364 static void empathy_audio_src_finalize (GObject *object);
365 static void empathy_audio_src_handle_message (GstBin *bin,
366 GstMessage *message);
368 static gboolean empathy_audio_src_levels_updated (gpointer user_data);
371 empathy_audio_src_set_property (GObject *object,
372 guint property_id, const GValue *value, GParamSpec *pspec)
377 empathy_audio_src_set_hw_volume (EMPATHY_GST_AUDIO_SRC (object),
378 g_value_get_double (value));
381 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
386 empathy_audio_src_get_property (GObject *object,
387 guint property_id, GValue *value, GParamSpec *pspec)
389 EmpathyGstAudioSrc *self = EMPATHY_GST_AUDIO_SRC (object);
390 EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (self);
395 g_value_set_double (value, priv->volume);
397 case PROP_PEAK_LEVEL:
398 g_mutex_lock (priv->lock);
399 g_value_set_double (value, priv->peak_level);
400 g_mutex_unlock (priv->lock);
403 g_mutex_lock (priv->lock);
404 g_value_set_double (value, priv->rms_level);
405 g_mutex_unlock (priv->lock);
407 case PROP_MICROPHONE:
408 g_value_set_uint (value, priv->source_idx);
411 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
416 empathy_audio_src_class_init (EmpathyGstAudioSrcClass
417 *empathy_audio_src_class)
419 GObjectClass *object_class = G_OBJECT_CLASS (empathy_audio_src_class);
420 GstBinClass *gstbin_class = GST_BIN_CLASS (empathy_audio_src_class);
421 GParamSpec *param_spec;
423 g_type_class_add_private (empathy_audio_src_class,
424 sizeof (EmpathyGstAudioSrcPrivate));
426 object_class->dispose = empathy_audio_src_dispose;
427 object_class->finalize = empathy_audio_src_finalize;
429 object_class->set_property = empathy_audio_src_set_property;
430 object_class->get_property = empathy_audio_src_get_property;
432 gstbin_class->handle_message =
433 GST_DEBUG_FUNCPTR (empathy_audio_src_handle_message);
435 param_spec = g_param_spec_double ("volume", "Volume", "volume contol",
437 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
438 g_object_class_install_property (object_class, PROP_VOLUME, param_spec);
440 param_spec = g_param_spec_double ("peak-level", "peak level", "peak level",
441 -G_MAXDOUBLE, G_MAXDOUBLE, 0,
442 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
443 g_object_class_install_property (object_class, PROP_PEAK_LEVEL, param_spec);
445 param_spec = g_param_spec_uint ("microphone", "microphone", "microphone",
446 0, G_MAXUINT, G_MAXUINT,
447 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
448 g_object_class_install_property (object_class, PROP_MICROPHONE, param_spec);
450 signals[PEAK_LEVEL_CHANGED] = g_signal_new ("peak-level-changed",
451 G_TYPE_FROM_CLASS (empathy_audio_src_class),
455 g_cclosure_marshal_generic,
456 G_TYPE_NONE, 1, G_TYPE_DOUBLE);
458 param_spec = g_param_spec_double ("rms-level", "RMS level", "RMS level",
459 -G_MAXDOUBLE, G_MAXDOUBLE, 0,
460 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
461 g_object_class_install_property (object_class, PROP_RMS_LEVEL, param_spec);
463 signals[RMS_LEVEL_CHANGED] = g_signal_new ("rms-level-changed",
464 G_TYPE_FROM_CLASS (empathy_audio_src_class),
468 g_cclosure_marshal_generic,
469 G_TYPE_NONE, 1, G_TYPE_DOUBLE);
473 empathy_audio_src_dispose (GObject *object)
475 EmpathyGstAudioSrc *self = EMPATHY_GST_AUDIO_SRC (object);
476 EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (self);
478 if (priv->dispose_has_run)
481 priv->dispose_has_run = TRUE;
483 if (priv->level_idle_id != 0)
484 g_source_remove (priv->level_idle_id);
485 priv->level_idle_id = 0;
487 if (priv->volume_idle_id != 0)
488 g_source_remove (priv->volume_idle_id);
489 priv->volume_idle_id = 0;
491 tp_clear_object (&priv->mic_monitor);
493 /* release any references held by the object here */
495 if (G_OBJECT_CLASS (empathy_audio_src_parent_class)->dispose)
496 G_OBJECT_CLASS (empathy_audio_src_parent_class)->dispose (object);
500 empathy_audio_src_finalize (GObject *object)
502 EmpathyGstAudioSrc *self = EMPATHY_GST_AUDIO_SRC (object);
503 EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (self);
505 /* free any data held directly by the object here */
506 g_mutex_free (priv->lock);
508 G_OBJECT_CLASS (empathy_audio_src_parent_class)->finalize (object);
512 empathy_audio_src_levels_updated (gpointer user_data)
514 EmpathyGstAudioSrc *self = EMPATHY_GST_AUDIO_SRC (user_data);
515 EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (self);
517 g_mutex_lock (priv->lock);
519 g_signal_emit (self, signals[PEAK_LEVEL_CHANGED], 0, priv->peak_level);
520 g_signal_emit (self, signals[RMS_LEVEL_CHANGED], 0, priv->rms_level);
521 priv->level_idle_id = 0;
523 g_mutex_unlock (priv->lock);
529 empathy_audio_src_volume_changed (gpointer user_data)
531 EmpathyGstAudioSrc *self = EMPATHY_GST_AUDIO_SRC (user_data);
532 EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (self);
535 g_mutex_lock (priv->lock);
536 priv->volume_idle_id = 0;
537 g_mutex_unlock (priv->lock);
539 volume = empathy_audio_src_get_hw_volume (self);
541 if (volume != priv->volume)
543 priv->volume = volume;
544 g_object_notify (G_OBJECT (self), "volume");
551 empathy_audio_src_handle_message (GstBin *bin, GstMessage *message)
553 EmpathyGstAudioSrc *self = EMPATHY_GST_AUDIO_SRC (bin);
554 EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (self);
556 if (GST_MESSAGE_TYPE (message) == GST_MESSAGE_ELEMENT &&
557 GST_MESSAGE_SRC (message) == GST_OBJECT (priv->level))
559 const GstStructure *s;
563 gdouble peak = -G_MAXDOUBLE;
564 gdouble rms = -G_MAXDOUBLE;
566 s = gst_message_get_structure (message);
567 name = gst_structure_get_name (s);
569 if (g_strcmp0 ("level", name) != 0)
572 list = gst_structure_get_value (s, "peak");
573 len = gst_value_list_get_size (list);
575 for (i =0 ; i < len; i++)
580 value = gst_value_list_get_value (list, i);
581 db = g_value_get_double (value);
582 peak = MAX (db, peak);
585 list = gst_structure_get_value (s, "rms");
586 len = gst_value_list_get_size (list);
588 for (i =0 ; i < len; i++)
593 value = gst_value_list_get_value (list, i);
594 db = g_value_get_double (value);
598 g_mutex_lock (priv->lock);
600 priv->peak_level = peak;
601 priv->rms_level = rms;
602 if (priv->level_idle_id == 0)
603 priv->level_idle_id = g_idle_add (
604 empathy_audio_src_levels_updated, self);
606 g_mutex_unlock (priv->lock);
608 else if (GST_MESSAGE_TYPE (message) == GST_MESSAGE_ELEMENT &&
609 GST_MESSAGE_SRC (message) == GST_OBJECT (priv->src))
611 /* Listen for volume changes on the src element */
612 if (gst_mixer_message_get_type (message) ==
613 GST_MIXER_MESSAGE_VOLUME_CHANGED)
615 GstMixerTrack *track;
617 gst_mixer_message_parse_volume_changed (message, &track,
620 g_mutex_lock (priv->lock);
622 if (track == priv->track && priv->volume_idle_id == 0)
623 priv->volume_idle_id = g_idle_add (
624 empathy_audio_src_volume_changed, self);
625 g_mutex_unlock (priv->lock);
628 else if (GST_MESSAGE_TYPE (message) == GST_MESSAGE_STATE_CHANGED &&
629 GST_MESSAGE_SRC (message) == GST_OBJECT (priv->src))
633 gst_message_parse_state_changed (message, &old, &new, NULL);
635 /* GstMixer is only available in state >= READY, so only start
636 * controlling the source element when going to ready state and stop
637 * doing so when going below ready. Furthermore once we have mixer read
638 * the current volume level from it and remove the settings done by
639 * Empathy. We want to pick up the level pulseaudio saved */
640 if (old == GST_STATE_NULL && new == GST_STATE_READY)
642 g_mutex_lock (priv->lock);
643 priv->track = empathy_audio_src_get_track (priv->src);
644 if (priv->track != NULL)
645 priv->volume_idle_id = g_idle_add (
646 empathy_audio_src_volume_changed, self);
647 g_mutex_unlock (priv->lock);
649 else if (old == GST_STATE_READY && new == GST_STATE_NULL)
651 g_mutex_lock (priv->lock);
653 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);