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
27 #include <gst/audio/streamvolume.h>
29 #include <gst/interfaces/streamvolume.h>
32 #include <libempathy/empathy-utils.h>
33 #include "empathy-audio-utils.h"
35 #include "empathy-audio-src.h"
37 #include "empathy-mic-monitor.h"
39 #define DEBUG_FLAG EMPATHY_DEBUG_VOIP
40 #include <libempathy/empathy-debug.h>
42 G_DEFINE_TYPE(EmpathyGstAudioSrc, empathy_audio_src, GST_TYPE_BIN)
50 /* private structure */
51 struct _EmpathyGstAudioSrcPrivate
53 gboolean dispose_has_run;
55 GstElement *volume_element;
57 EmpathyMicMonitor *mic_monitor;
59 /* 0 if not known yet */
60 guint source_output_idx;
61 /* G_MAXUINT if not known yet */
66 gboolean have_stream_volume;
72 #define EMPATHY_GST_AUDIO_SRC_GET_PRIVATE(o) \
73 (G_TYPE_INSTANCE_GET_PRIVATE ((o), EMPATHY_TYPE_GST_AUDIO_SRC, \
74 EmpathyGstAudioSrcPrivate))
78 empathy_audio_src_volume_changed (GObject *object,
83 empathy_audio_set_hw_mute (EmpathyGstAudioSrc *self, gboolean mute)
85 if (mute == self->priv->mute)
88 if (self->priv->have_stream_volume)
89 g_object_set (self->priv->src, "mute", mute, NULL);
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);
97 self->priv->mute = mute;
101 empathy_audio_src_get_hw_mute (EmpathyGstAudioSrc *self)
104 g_object_get (self->priv->src, "mute", &result, NULL);
110 empathy_audio_src_set_hw_volume (EmpathyGstAudioSrc *self,
113 if (volume == self->priv->volume)
116 if (self->priv->have_stream_volume)
117 g_object_set (self->priv->src, "volume", volume, NULL);
118 self->priv->volume = volume;
122 empathy_audio_src_get_hw_volume (EmpathyGstAudioSrc *self)
125 g_object_get (self->priv->src, "volume", &result, NULL);
132 empathy_audio_src_supports_changing_mic (EmpathyGstAudioSrc *self)
134 EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (self);
135 GObjectClass *object_class;
137 object_class = G_OBJECT_GET_CLASS (priv->src);
139 return (g_object_class_find_property (object_class,
140 "source-output-index") != NULL);
144 empathy_audio_src_get_mic_index (EmpathyGstAudioSrc *self)
146 EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (self);
147 guint audio_src_idx = PA_INVALID_INDEX;
149 if (empathy_audio_src_supports_changing_mic (self))
150 g_object_get (priv->src,
151 "source-output-index", &audio_src_idx,
154 return audio_src_idx;
158 empathy_audio_src_microphone_changed_cb (EmpathyMicMonitor *monitor,
159 guint source_output_idx,
163 EmpathyGstAudioSrc *self = user_data;
164 EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (self);
167 audio_src_idx = empathy_audio_src_get_mic_index (self);
169 if (source_output_idx == PA_INVALID_INDEX
170 || source_output_idx != audio_src_idx)
173 if (priv->source_idx == source_idx)
176 priv->source_idx = source_idx;
177 g_object_notify (G_OBJECT (self), "microphone");
181 empathy_audio_src_get_current_mic_cb (GObject *source_object,
182 GAsyncResult *result,
185 EmpathyMicMonitor *monitor = EMPATHY_MIC_MONITOR (source_object);
186 EmpathyGstAudioSrc *self = user_data;
187 EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (self);
189 GError *error = NULL;
191 source_idx = empathy_mic_monitor_get_current_mic_finish (monitor, result, &error);
195 DEBUG ("Failed to get current mic: %s", error->message);
196 g_clear_error (&error);
200 if (priv->source_idx == source_idx)
203 priv->source_idx = source_idx;
204 g_object_notify (G_OBJECT (self), "microphone");
208 empathy_audio_src_source_output_index_notify (GObject *object,
210 EmpathyGstAudioSrc *self)
212 EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (self);
213 guint source_output_idx;
215 source_output_idx = empathy_audio_src_get_mic_index (self);
217 if (source_output_idx == PA_INVALID_INDEX)
220 if (priv->source_output_idx == source_output_idx)
223 /* It's actually changed. */
224 priv->source_output_idx = source_output_idx;
226 empathy_mic_monitor_get_current_mic_async (priv->mic_monitor,
227 source_output_idx, empathy_audio_src_get_current_mic_cb, self);
234 const gchar *description;
236 description = g_getenv ("EMPATHY_AUDIO_SRC");
238 if (description != NULL)
240 GError *error = NULL;
242 src = gst_parse_bin_from_description (description, TRUE, &error);
245 DEBUG ("Failed to create bin %s: %s", description, error->message);
246 g_error_free (error);
252 /* Use pulsesrc as default */
253 src = gst_element_factory_make ("pulsesrc", NULL);
256 g_warning ("Missing 'pulsesrc' element");
260 empathy_audio_set_stream_properties (src, TRUE);
262 /* Set latency (buffering on the PulseAudio side) of 20ms */
263 g_object_set (src, "buffer-time", (gint64) 20000, NULL);
269 empathy_audio_src_init (EmpathyGstAudioSrc *obj)
271 EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (obj);
275 g_mutex_init (&priv->lock);
279 priv->src = create_src ();
280 if (priv->src == NULL)
283 if (GST_IS_STREAM_VOLUME (priv->src))
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
292 g_object_bind_property (obj, "volume", priv->src, "volume",
294 g_object_bind_property (obj, "mute", priv->src, "mute",
297 /* sync and callback for bouncing */
298 g_object_get (priv->src, "volume", &volume, NULL);
299 g_object_set (obj, "volume", volume, NULL);
301 g_object_get (priv->src, "mute", &mute, NULL);
302 g_object_set (obj, "mute", mute, NULL);
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);
311 g_message ("No stream volume available :(, mute will work though");
312 priv->have_stream_volume = FALSE;
315 gst_bin_add (GST_BIN (obj), priv->src);
317 priv->volume_element = gst_element_factory_make ("volume", NULL);
318 gst_bin_add (GST_BIN (obj), priv->volume_element);
322 GstElement *capsfilter;
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,
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);
343 gst_element_link (priv->src, priv->volume_element);
346 src = gst_element_get_static_pad (priv->volume_element, "src");
348 ghost = gst_ghost_pad_new ("src", src);
349 gst_element_add_pad (GST_ELEMENT (obj), ghost);
351 gst_object_unref (G_OBJECT (src));
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),
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);
364 priv->source_idx = PA_INVALID_INDEX;
367 static void empathy_audio_src_dispose (GObject *object);
368 static void empathy_audio_src_finalize (GObject *object);
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 empathy_audio_set_hw_mute (EMPATHY_GST_AUDIO_SRC (object),
382 g_value_get_boolean (value));
385 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
390 empathy_audio_src_get_property (GObject *object,
391 guint property_id, GValue *value, GParamSpec *pspec)
393 EmpathyGstAudioSrc *self = EMPATHY_GST_AUDIO_SRC (object);
394 EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (self);
399 g_value_set_double (value, priv->volume);
402 g_value_set_boolean (value, priv->mute);
404 case PROP_MICROPHONE:
405 g_value_set_uint (value, priv->source_idx);
408 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
413 empathy_audio_src_class_init (EmpathyGstAudioSrcClass
414 *empathy_audio_src_class)
416 GObjectClass *object_class = G_OBJECT_CLASS (empathy_audio_src_class);
417 GParamSpec *param_spec;
419 g_type_class_add_private (empathy_audio_src_class,
420 sizeof (EmpathyGstAudioSrcPrivate));
422 object_class->dispose = empathy_audio_src_dispose;
423 object_class->finalize = empathy_audio_src_finalize;
425 object_class->set_property = empathy_audio_src_set_property;
426 object_class->get_property = empathy_audio_src_get_property;
428 param_spec = g_param_spec_double ("volume", "Volume", "volume contol",
430 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
431 g_object_class_install_property (object_class, PROP_VOLUME, param_spec);
433 param_spec = g_param_spec_boolean ("mute", "Mute", "mute contol",
435 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
436 g_object_class_install_property (object_class, PROP_MUTE, param_spec);
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);
445 empathy_audio_src_dispose (GObject *object)
447 EmpathyGstAudioSrc *self = EMPATHY_GST_AUDIO_SRC (object);
448 EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (self);
450 if (priv->dispose_has_run)
453 priv->dispose_has_run = TRUE;
455 if (priv->volume_idle_id != 0)
456 g_source_remove (priv->volume_idle_id);
457 priv->volume_idle_id = 0;
459 tp_clear_object (&priv->mic_monitor);
461 /* release any references held by the object here */
463 if (G_OBJECT_CLASS (empathy_audio_src_parent_class)->dispose)
464 G_OBJECT_CLASS (empathy_audio_src_parent_class)->dispose (object);
468 empathy_audio_src_finalize (GObject *object)
470 EmpathyGstAudioSrc *self = EMPATHY_GST_AUDIO_SRC (object);
471 EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (self);
473 /* free any data held directly by the object here */
474 g_mutex_clear (&priv->lock);
476 G_OBJECT_CLASS (empathy_audio_src_parent_class)->finalize (object);
480 empathy_audio_src_volume_changed_idle (gpointer user_data)
482 EmpathyGstAudioSrc *self = EMPATHY_GST_AUDIO_SRC (user_data);
483 EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (self);
487 g_mutex_lock (&priv->lock);
488 priv->volume_idle_id = 0;
489 g_mutex_unlock (&priv->lock);
491 volume = empathy_audio_src_get_hw_volume (self);
493 if (volume != priv->volume)
495 priv->volume = volume;
496 g_object_notify (G_OBJECT (self), "volume");
499 mute = empathy_audio_src_get_hw_mute (self);
500 if (mute != priv->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");
512 empathy_audio_src_volume_changed (GObject *object,
516 EmpathyGstAudioSrc *self = EMPATHY_GST_AUDIO_SRC (user_data);
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);
528 empathy_audio_src_new (void)
530 static gboolean registered = FALSE;
533 if (!gst_element_register (NULL, "empathyaudiosrc",
534 GST_RANK_NONE, EMPATHY_TYPE_GST_AUDIO_SRC))
538 return gst_element_factory_make ("empathyaudiosrc", NULL);
542 empathy_audio_src_set_echo_cancel (EmpathyGstAudioSrc *src,
545 DEBUG ("Src echo cancellation setting: %s", enable ? "on" : "off");
546 empathy_audio_set_stream_properties (src->priv->src, enable);
550 empathy_audio_src_set_volume (EmpathyGstAudioSrc *src, gdouble volume)
552 g_object_set (src, "volume", volume, NULL);
556 empathy_audio_src_get_volume (EmpathyGstAudioSrc *src)
558 return src->priv->volume;
562 empathy_audio_src_get_microphone (EmpathyGstAudioSrc *src)
564 EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (src);
566 return priv->source_idx;
570 empathy_audio_src_change_microphone_cb (GObject *source_object,
571 GAsyncResult *result,
574 EmpathyMicMonitor *monitor = EMPATHY_MIC_MONITOR (source_object);
575 GSimpleAsyncResult *simple = user_data;
576 GError *error = NULL;
578 if (!empathy_mic_monitor_change_microphone_finish (monitor,
581 g_simple_async_result_take_error (simple, error);
584 g_simple_async_result_complete (simple);
585 g_object_unref (simple);
589 empathy_audio_src_change_microphone_async (EmpathyGstAudioSrc *src,
591 GAsyncReadyCallback callback,
594 EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (src);
595 guint source_output_idx;
596 GSimpleAsyncResult *simple;
598 simple = g_simple_async_result_new (G_OBJECT (src), callback, user_data,
599 empathy_audio_src_change_microphone_async);
601 if (!empathy_audio_src_supports_changing_mic (src))
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);
610 source_output_idx = empathy_audio_src_get_mic_index (src);
612 if (source_output_idx == PA_INVALID_INDEX)
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);
621 empathy_mic_monitor_change_microphone_async (priv->mic_monitor,
622 source_output_idx, microphone, empathy_audio_src_change_microphone_cb,
627 empathy_audio_src_change_microphone_finish (EmpathyGstAudioSrc *src,
628 GAsyncResult *result,
631 empathy_implement_finish_void (src,
632 empathy_audio_src_change_microphone_async);
636 empathy_audio_src_set_mute (EmpathyGstAudioSrc *self,
639 empathy_audio_set_hw_mute (self, mute);
641 g_object_notify (G_OBJECT (self), "mute");