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
22 #include "empathy-audio-src.h"
24 #include <tp-account-widgets/tpaw-utils.h>
27 #include <gst/audio/streamvolume.h>
29 #include <gst/interfaces/streamvolume.h>
32 #include "empathy-audio-utils.h"
33 #include "empathy-mic-monitor.h"
34 #include "empathy-utils.h"
36 #define DEBUG_FLAG EMPATHY_DEBUG_VOIP
37 #include "empathy-debug.h"
39 G_DEFINE_TYPE(EmpathyGstAudioSrc, empathy_audio_src, GST_TYPE_BIN)
47 /* private structure */
48 struct _EmpathyGstAudioSrcPrivate
50 gboolean dispose_has_run;
52 GstElement *volume_element;
54 EmpathyMicMonitor *mic_monitor;
56 /* 0 if not known yet */
57 guint source_output_idx;
58 /* G_MAXUINT if not known yet */
63 gboolean have_stream_volume;
69 #define EMPATHY_GST_AUDIO_SRC_GET_PRIVATE(o) \
70 (G_TYPE_INSTANCE_GET_PRIVATE ((o), EMPATHY_TYPE_GST_AUDIO_SRC, \
71 EmpathyGstAudioSrcPrivate))
75 empathy_audio_src_volume_changed (GObject *object,
80 empathy_audio_set_hw_mute (EmpathyGstAudioSrc *self, gboolean mute)
82 if (mute == self->priv->mute)
85 if (self->priv->have_stream_volume)
86 g_object_set (self->priv->src, "mute", mute, NULL);
88 /* Belt and braces: If for some reason the underlying src doesn't mute
89 * correctly or doesn't update us when it unmutes correctly enforce it using
90 * our own volume element. Our UI can in no circumstances be made to think
91 * the input is muted while it's not */
92 g_object_set (self->priv->volume_element, "mute", mute, NULL);
94 self->priv->mute = mute;
98 empathy_audio_src_get_hw_mute (EmpathyGstAudioSrc *self)
101 g_object_get (self->priv->src, "mute", &result, NULL);
107 empathy_audio_src_set_hw_volume (EmpathyGstAudioSrc *self,
110 if (volume == self->priv->volume)
113 if (self->priv->have_stream_volume)
114 g_object_set (self->priv->src, "volume", volume, NULL);
115 self->priv->volume = volume;
119 empathy_audio_src_get_hw_volume (EmpathyGstAudioSrc *self)
122 g_object_get (self->priv->src, "volume", &result, NULL);
129 empathy_audio_src_supports_changing_mic (EmpathyGstAudioSrc *self)
131 EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (self);
132 GObjectClass *object_class;
134 object_class = G_OBJECT_GET_CLASS (priv->src);
136 return (g_object_class_find_property (object_class,
137 "source-output-index") != NULL);
141 empathy_audio_src_get_mic_index (EmpathyGstAudioSrc *self)
143 EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (self);
144 guint audio_src_idx = PA_INVALID_INDEX;
146 if (empathy_audio_src_supports_changing_mic (self))
147 g_object_get (priv->src,
148 "source-output-index", &audio_src_idx,
151 return audio_src_idx;
155 empathy_audio_src_microphone_changed_cb (EmpathyMicMonitor *monitor,
156 guint source_output_idx,
160 EmpathyGstAudioSrc *self = user_data;
161 EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (self);
164 audio_src_idx = empathy_audio_src_get_mic_index (self);
166 if (source_output_idx == PA_INVALID_INDEX
167 || source_output_idx != audio_src_idx)
170 if (priv->source_idx == source_idx)
173 priv->source_idx = source_idx;
174 g_object_notify (G_OBJECT (self), "microphone");
178 empathy_audio_src_get_current_mic_cb (GObject *source_object,
179 GAsyncResult *result,
182 EmpathyMicMonitor *monitor = EMPATHY_MIC_MONITOR (source_object);
183 EmpathyGstAudioSrc *self = user_data;
184 EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (self);
186 GError *error = NULL;
188 source_idx = empathy_mic_monitor_get_current_mic_finish (monitor, result, &error);
192 DEBUG ("Failed to get current mic: %s", error->message);
193 g_clear_error (&error);
197 if (priv->source_idx == source_idx)
200 priv->source_idx = source_idx;
201 g_object_notify (G_OBJECT (self), "microphone");
205 empathy_audio_src_source_output_index_notify (GObject *object,
207 EmpathyGstAudioSrc *self)
209 EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (self);
210 guint source_output_idx;
212 source_output_idx = empathy_audio_src_get_mic_index (self);
214 if (source_output_idx == PA_INVALID_INDEX)
217 if (priv->source_output_idx == source_output_idx)
220 /* It's actually changed. */
221 priv->source_output_idx = source_output_idx;
223 empathy_mic_monitor_get_current_mic_async (priv->mic_monitor,
224 source_output_idx, empathy_audio_src_get_current_mic_cb, self);
231 const gchar *description;
233 description = g_getenv ("EMPATHY_AUDIO_SRC");
235 if (description != NULL)
237 GError *error = NULL;
239 src = gst_parse_bin_from_description (description, TRUE, &error);
242 DEBUG ("Failed to create bin %s: %s", description, error->message);
243 g_error_free (error);
249 /* Use pulsesrc as default */
250 src = gst_element_factory_make ("pulsesrc", NULL);
253 g_warning ("Missing 'pulsesrc' element");
257 empathy_audio_set_stream_properties (src, TRUE);
259 /* Set latency (buffering on the PulseAudio side) of 20ms */
260 g_object_set (src, "buffer-time", (gint64) 20000, NULL);
266 empathy_audio_src_init (EmpathyGstAudioSrc *obj)
268 EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (obj);
272 g_mutex_init (&priv->lock);
276 priv->src = create_src ();
277 if (priv->src == NULL)
280 if (GST_IS_STREAM_VOLUME (priv->src))
285 priv->have_stream_volume = TRUE;
286 /* We can't do a bidirection bind as the ::notify comes from another
287 * thread, for other bits of empathy it's most simpler if it comes from
289 g_object_bind_property (obj, "volume", priv->src, "volume",
291 g_object_bind_property (obj, "mute", priv->src, "mute",
294 /* sync and callback for bouncing */
295 g_object_get (priv->src, "volume", &volume, NULL);
296 g_object_set (obj, "volume", volume, NULL);
298 g_object_get (priv->src, "mute", &mute, NULL);
299 g_object_set (obj, "mute", mute, NULL);
301 g_signal_connect (priv->src, "notify::volume",
302 G_CALLBACK (empathy_audio_src_volume_changed), obj);
303 g_signal_connect (priv->src, "notify::mute",
304 G_CALLBACK (empathy_audio_src_volume_changed), obj);
308 g_message ("No stream volume available :(, mute will work though");
309 priv->have_stream_volume = FALSE;
312 gst_bin_add (GST_BIN (obj), priv->src);
314 priv->volume_element = gst_element_factory_make ("volume", NULL);
315 gst_bin_add (GST_BIN (obj), priv->volume_element);
319 GstElement *capsfilter;
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);
337 gst_element_link (capsfilter, priv->volume_element);
340 gst_element_link (priv->src, priv->volume_element);
343 src = gst_element_get_static_pad (priv->volume_element, "src");
345 ghost = gst_ghost_pad_new ("src", src);
346 gst_element_add_pad (GST_ELEMENT (obj), ghost);
348 gst_object_unref (G_OBJECT (src));
350 /* Listen to changes to GstPulseSrc:source-output-index so we know when
351 * it's no longer PA_INVALID_INDEX (starting for the first time) or if it
352 * changes (READY->NULL->READY...) */
353 g_signal_connect (priv->src, "notify::source-output-index",
354 G_CALLBACK (empathy_audio_src_source_output_index_notify),
357 priv->mic_monitor = empathy_mic_monitor_new ();
358 g_signal_connect (priv->mic_monitor, "microphone-changed",
359 G_CALLBACK (empathy_audio_src_microphone_changed_cb), obj);
361 priv->source_idx = PA_INVALID_INDEX;
364 static void empathy_audio_src_dispose (GObject *object);
365 static void empathy_audio_src_finalize (GObject *object);
368 empathy_audio_src_set_property (GObject *object,
369 guint property_id, const GValue *value, GParamSpec *pspec)
374 empathy_audio_src_set_hw_volume (EMPATHY_GST_AUDIO_SRC (object),
375 g_value_get_double (value));
378 empathy_audio_set_hw_mute (EMPATHY_GST_AUDIO_SRC (object),
379 g_value_get_boolean (value));
382 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
387 empathy_audio_src_get_property (GObject *object,
388 guint property_id, GValue *value, GParamSpec *pspec)
390 EmpathyGstAudioSrc *self = EMPATHY_GST_AUDIO_SRC (object);
391 EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (self);
396 g_value_set_double (value, priv->volume);
399 g_value_set_boolean (value, priv->mute);
401 case PROP_MICROPHONE:
402 g_value_set_uint (value, priv->source_idx);
405 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
410 empathy_audio_src_class_init (EmpathyGstAudioSrcClass
411 *empathy_audio_src_class)
413 GObjectClass *object_class = G_OBJECT_CLASS (empathy_audio_src_class);
414 GParamSpec *param_spec;
416 g_type_class_add_private (empathy_audio_src_class,
417 sizeof (EmpathyGstAudioSrcPrivate));
419 object_class->dispose = empathy_audio_src_dispose;
420 object_class->finalize = empathy_audio_src_finalize;
422 object_class->set_property = empathy_audio_src_set_property;
423 object_class->get_property = empathy_audio_src_get_property;
425 param_spec = g_param_spec_double ("volume", "Volume", "volume contol",
427 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
428 g_object_class_install_property (object_class, PROP_VOLUME, param_spec);
430 param_spec = g_param_spec_boolean ("mute", "Mute", "mute contol",
432 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
433 g_object_class_install_property (object_class, PROP_MUTE, param_spec);
435 param_spec = g_param_spec_uint ("microphone", "microphone", "microphone",
436 0, G_MAXUINT, G_MAXUINT,
437 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
438 g_object_class_install_property (object_class, PROP_MICROPHONE, param_spec);
442 empathy_audio_src_dispose (GObject *object)
444 EmpathyGstAudioSrc *self = EMPATHY_GST_AUDIO_SRC (object);
445 EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (self);
447 if (priv->dispose_has_run)
450 priv->dispose_has_run = TRUE;
452 if (priv->volume_idle_id != 0)
453 g_source_remove (priv->volume_idle_id);
454 priv->volume_idle_id = 0;
456 tp_clear_object (&priv->mic_monitor);
458 /* release any references held by the object here */
460 if (G_OBJECT_CLASS (empathy_audio_src_parent_class)->dispose)
461 G_OBJECT_CLASS (empathy_audio_src_parent_class)->dispose (object);
465 empathy_audio_src_finalize (GObject *object)
467 EmpathyGstAudioSrc *self = EMPATHY_GST_AUDIO_SRC (object);
468 EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (self);
470 /* free any data held directly by the object here */
471 g_mutex_clear (&priv->lock);
473 G_OBJECT_CLASS (empathy_audio_src_parent_class)->finalize (object);
477 empathy_audio_src_volume_changed_idle (gpointer user_data)
479 EmpathyGstAudioSrc *self = EMPATHY_GST_AUDIO_SRC (user_data);
480 EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (self);
484 g_mutex_lock (&priv->lock);
485 priv->volume_idle_id = 0;
486 g_mutex_unlock (&priv->lock);
488 volume = empathy_audio_src_get_hw_volume (self);
490 if (volume != priv->volume)
492 priv->volume = volume;
493 g_object_notify (G_OBJECT (self), "volume");
496 mute = empathy_audio_src_get_hw_mute (self);
497 if (mute != priv->mute)
500 /* hw mute changed, follow with own volume */
501 g_object_set (self->priv->volume_element, "mute", mute, NULL);
502 g_object_notify (G_OBJECT (self), "mute");
509 empathy_audio_src_volume_changed (GObject *object,
513 EmpathyGstAudioSrc *self = EMPATHY_GST_AUDIO_SRC (user_data);
515 g_mutex_lock (&self->priv->lock);
516 if (self->priv->volume_idle_id == 0)
517 self->priv->volume_idle_id = g_idle_add (
518 empathy_audio_src_volume_changed_idle, self);
519 g_mutex_unlock (&self->priv->lock);
525 empathy_audio_src_new (void)
527 static gboolean registered = FALSE;
530 if (!gst_element_register (NULL, "empathyaudiosrc",
531 GST_RANK_NONE, EMPATHY_TYPE_GST_AUDIO_SRC))
535 return gst_element_factory_make ("empathyaudiosrc", NULL);
539 empathy_audio_src_set_echo_cancel (EmpathyGstAudioSrc *src,
542 DEBUG ("Src echo cancellation setting: %s", enable ? "on" : "off");
543 empathy_audio_set_stream_properties (src->priv->src, enable);
547 empathy_audio_src_set_volume (EmpathyGstAudioSrc *src, gdouble volume)
549 g_object_set (src, "volume", volume, NULL);
553 empathy_audio_src_get_volume (EmpathyGstAudioSrc *src)
555 return src->priv->volume;
559 empathy_audio_src_get_microphone (EmpathyGstAudioSrc *src)
561 EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (src);
563 return priv->source_idx;
567 empathy_audio_src_change_microphone_cb (GObject *source_object,
568 GAsyncResult *result,
571 EmpathyMicMonitor *monitor = EMPATHY_MIC_MONITOR (source_object);
572 GSimpleAsyncResult *simple = user_data;
573 GError *error = NULL;
575 if (!empathy_mic_monitor_change_microphone_finish (monitor,
578 g_simple_async_result_take_error (simple, error);
581 g_simple_async_result_complete (simple);
582 g_object_unref (simple);
586 empathy_audio_src_change_microphone_async (EmpathyGstAudioSrc *src,
588 GAsyncReadyCallback callback,
591 EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (src);
592 guint source_output_idx;
593 GSimpleAsyncResult *simple;
595 simple = g_simple_async_result_new (G_OBJECT (src), callback, user_data,
596 empathy_audio_src_change_microphone_async);
598 if (!empathy_audio_src_supports_changing_mic (src))
600 g_simple_async_result_set_error (simple, G_IO_ERROR, G_IO_ERROR_FAILED,
601 "pulsesrc is not new enough to support changing microphone");
602 g_simple_async_result_complete_in_idle (simple);
603 g_object_unref (simple);
607 source_output_idx = empathy_audio_src_get_mic_index (src);
609 if (source_output_idx == PA_INVALID_INDEX)
611 g_simple_async_result_set_error (simple, G_IO_ERROR, G_IO_ERROR_FAILED,
612 "pulsesrc is not yet PLAYING");
613 g_simple_async_result_complete_in_idle (simple);
614 g_object_unref (simple);
618 empathy_mic_monitor_change_microphone_async (priv->mic_monitor,
619 source_output_idx, microphone, empathy_audio_src_change_microphone_cb,
624 empathy_audio_src_change_microphone_finish (EmpathyGstAudioSrc *src,
625 GAsyncResult *result,
628 tpaw_implement_finish_void (src,
629 empathy_audio_src_change_microphone_async);
633 empathy_audio_src_set_mute (EmpathyGstAudioSrc *self,
636 empathy_audio_set_hw_mute (self, mute);
638 g_object_notify (G_OBJECT (self), "mute");