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 "empathy-audio-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)
46 /* private structure */
47 struct _EmpathyGstAudioSrcPrivate
49 gboolean dispose_has_run;
51 GstElement *volume_element;
53 EmpathyMicMonitor *mic_monitor;
55 /* 0 if not known yet */
56 guint source_output_idx;
57 /* G_MAXUINT if not known yet */
62 gboolean have_stream_volume;
68 #define EMPATHY_GST_AUDIO_SRC_GET_PRIVATE(o) \
69 (G_TYPE_INSTANCE_GET_PRIVATE ((o), EMPATHY_TYPE_GST_AUDIO_SRC, \
70 EmpathyGstAudioSrcPrivate))
74 empathy_audio_src_volume_changed (GObject *object,
79 empathy_audio_set_hw_mute (EmpathyGstAudioSrc *self, gboolean mute)
81 if (mute == self->priv->mute)
84 if (self->priv->have_stream_volume)
85 g_object_set (self->priv->src, "mute", mute, NULL);
87 /* Belt and braces: If for some reason the underlying src doesn't mute
88 * correctly or doesn't update us when it unmutes correctly enforce it using
89 * our own volume element. Our UI can in no circumstances be made to think
90 * the input is muted while it's not */
91 g_object_set (self->priv->volume_element, "mute", mute, NULL);
93 self->priv->mute = mute;
97 empathy_audio_src_get_hw_mute (EmpathyGstAudioSrc *self)
100 g_object_get (self->priv->src, "mute", &result, NULL);
106 empathy_audio_src_set_hw_volume (EmpathyGstAudioSrc *self,
109 if (volume == self->priv->volume)
112 if (self->priv->have_stream_volume)
113 g_object_set (self->priv->src, "volume", volume, NULL);
114 self->priv->volume = volume;
118 empathy_audio_src_get_hw_volume (EmpathyGstAudioSrc *self)
121 g_object_get (self->priv->src, "volume", &result, NULL);
128 empathy_audio_src_supports_changing_mic (EmpathyGstAudioSrc *self)
130 EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (self);
131 GObjectClass *object_class;
133 object_class = G_OBJECT_GET_CLASS (priv->src);
135 return (g_object_class_find_property (object_class,
136 "source-output-index") != NULL);
140 empathy_audio_src_get_mic_index (EmpathyGstAudioSrc *self)
142 EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (self);
143 guint audio_src_idx = PA_INVALID_INDEX;
145 if (empathy_audio_src_supports_changing_mic (self))
146 g_object_get (priv->src,
147 "source-output-index", &audio_src_idx,
150 return audio_src_idx;
154 empathy_audio_src_microphone_changed_cb (EmpathyMicMonitor *monitor,
155 guint source_output_idx,
159 EmpathyGstAudioSrc *self = user_data;
160 EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (self);
163 audio_src_idx = empathy_audio_src_get_mic_index (self);
165 if (source_output_idx == PA_INVALID_INDEX
166 || source_output_idx != audio_src_idx)
169 if (priv->source_idx == source_idx)
172 priv->source_idx = source_idx;
173 g_object_notify (G_OBJECT (self), "microphone");
177 empathy_audio_src_get_current_mic_cb (GObject *source_object,
178 GAsyncResult *result,
181 EmpathyMicMonitor *monitor = EMPATHY_MIC_MONITOR (source_object);
182 EmpathyGstAudioSrc *self = user_data;
183 EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (self);
185 GError *error = NULL;
187 source_idx = empathy_mic_monitor_get_current_mic_finish (monitor, result, &error);
191 DEBUG ("Failed to get current mic: %s", error->message);
192 g_clear_error (&error);
196 if (priv->source_idx == source_idx)
199 priv->source_idx = source_idx;
200 g_object_notify (G_OBJECT (self), "microphone");
204 empathy_audio_src_source_output_index_notify (GObject *object,
206 EmpathyGstAudioSrc *self)
208 EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (self);
209 guint source_output_idx;
211 source_output_idx = empathy_audio_src_get_mic_index (self);
213 if (source_output_idx == PA_INVALID_INDEX)
216 if (priv->source_output_idx == source_output_idx)
219 /* It's actually changed. */
220 priv->source_output_idx = source_output_idx;
222 empathy_mic_monitor_get_current_mic_async (priv->mic_monitor,
223 source_output_idx, empathy_audio_src_get_current_mic_cb, self);
230 const gchar *description;
232 description = g_getenv ("EMPATHY_AUDIO_SRC");
234 if (description != NULL)
236 GError *error = NULL;
238 src = gst_parse_bin_from_description (description, TRUE, &error);
241 DEBUG ("Failed to create bin %s: %s", description, error->message);
242 g_error_free (error);
248 /* Use pulsesrc as default */
249 src = gst_element_factory_make ("pulsesrc", NULL);
253 empathy_audio_set_stream_properties (src, TRUE);
255 /* Set latency (buffering on the PulseAudio side) of 20ms */
256 g_object_set (src, "buffer-time", (gint64) 20000, NULL);
262 empathy_audio_src_init (EmpathyGstAudioSrc *obj)
264 EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (obj);
268 g_mutex_init (&priv->lock);
272 priv->src = create_src ();
273 if (priv->src == NULL)
276 if (GST_IS_STREAM_VOLUME (priv->src))
281 priv->have_stream_volume = TRUE;
282 /* We can't do a bidirection bind as the ::notify comes from another
283 * thread, for other bits of empathy it's most simpler if it comes from
285 g_object_bind_property (obj, "volume", priv->src, "volume",
287 g_object_bind_property (obj, "mute", priv->src, "mute",
290 /* sync and callback for bouncing */
291 g_object_get (priv->src, "volume", &volume, NULL);
292 g_object_set (obj, "volume", volume, NULL);
294 g_object_get (priv->src, "mute", &mute, NULL);
295 g_object_set (obj, "mute", mute, NULL);
297 g_signal_connect (priv->src, "notify::volume",
298 G_CALLBACK (empathy_audio_src_volume_changed), obj);
299 g_signal_connect (priv->src, "notify::mute",
300 G_CALLBACK (empathy_audio_src_volume_changed), obj);
304 g_message ("No stream volume available :(, mute will work though");
305 priv->have_stream_volume = FALSE;
308 gst_bin_add (GST_BIN (obj), priv->src);
312 GstElement *capsfilter;
315 /* Explicitly state what format we want from pulsesrc. This pushes resampling
316 * and format conversion as early as possible, lowering the amount of data
317 * transferred and thus improving performance. When moving to GStreamer
318 * 0.11/1.0, this should change so that we actually request what the encoder
319 * wants downstream. */
320 caps = gst_caps_new_simple ("audio/x-raw-int",
321 "channels", G_TYPE_INT, 1,
322 "width", G_TYPE_INT, 16,
323 "depth", G_TYPE_INT, 16,
324 "rate", G_TYPE_INT, 32000,
326 capsfilter = gst_element_factory_make ("capsfilter", NULL);
327 g_object_set (G_OBJECT (capsfilter), "caps", caps, NULL);
328 gst_bin_add (GST_BIN (obj), capsfilter);
329 gst_element_link (priv->src, capsfilter);
333 priv->volume_element = gst_element_factory_make ("volume", NULL);
334 gst_bin_add (GST_BIN (obj), priv->volume_element);
335 gst_element_link (priv->src, priv->volume_element);
337 src = gst_element_get_static_pad (priv->volume_element, "src");
339 ghost = gst_ghost_pad_new ("src", src);
340 gst_element_add_pad (GST_ELEMENT (obj), ghost);
342 gst_object_unref (G_OBJECT (src));
344 /* Listen to changes to GstPulseSrc:source-output-index so we know when
345 * it's no longer PA_INVALID_INDEX (starting for the first time) or if it
346 * changes (READY->NULL->READY...) */
347 g_signal_connect (priv->src, "notify::source-output-index",
348 G_CALLBACK (empathy_audio_src_source_output_index_notify),
351 priv->mic_monitor = empathy_mic_monitor_new ();
352 g_signal_connect (priv->mic_monitor, "microphone-changed",
353 G_CALLBACK (empathy_audio_src_microphone_changed_cb), obj);
355 priv->source_idx = PA_INVALID_INDEX;
358 static void empathy_audio_src_dispose (GObject *object);
359 static void empathy_audio_src_finalize (GObject *object);
362 empathy_audio_src_set_property (GObject *object,
363 guint property_id, const GValue *value, GParamSpec *pspec)
368 empathy_audio_src_set_hw_volume (EMPATHY_GST_AUDIO_SRC (object),
369 g_value_get_double (value));
372 empathy_audio_set_hw_mute (EMPATHY_GST_AUDIO_SRC (object),
373 g_value_get_boolean (value));
376 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
381 empathy_audio_src_get_property (GObject *object,
382 guint property_id, GValue *value, GParamSpec *pspec)
384 EmpathyGstAudioSrc *self = EMPATHY_GST_AUDIO_SRC (object);
385 EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (self);
390 g_value_set_double (value, priv->volume);
393 g_value_set_boolean (value, priv->mute);
395 case PROP_MICROPHONE:
396 g_value_set_uint (value, priv->source_idx);
399 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
404 empathy_audio_src_class_init (EmpathyGstAudioSrcClass
405 *empathy_audio_src_class)
407 GObjectClass *object_class = G_OBJECT_CLASS (empathy_audio_src_class);
408 GParamSpec *param_spec;
410 g_type_class_add_private (empathy_audio_src_class,
411 sizeof (EmpathyGstAudioSrcPrivate));
413 object_class->dispose = empathy_audio_src_dispose;
414 object_class->finalize = empathy_audio_src_finalize;
416 object_class->set_property = empathy_audio_src_set_property;
417 object_class->get_property = empathy_audio_src_get_property;
419 param_spec = g_param_spec_double ("volume", "Volume", "volume contol",
421 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
422 g_object_class_install_property (object_class, PROP_VOLUME, param_spec);
424 param_spec = g_param_spec_boolean ("mute", "Mute", "mute contol",
426 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
427 g_object_class_install_property (object_class, PROP_MUTE, param_spec);
429 param_spec = g_param_spec_uint ("microphone", "microphone", "microphone",
430 0, G_MAXUINT, G_MAXUINT,
431 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
432 g_object_class_install_property (object_class, PROP_MICROPHONE, param_spec);
436 empathy_audio_src_dispose (GObject *object)
438 EmpathyGstAudioSrc *self = EMPATHY_GST_AUDIO_SRC (object);
439 EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (self);
441 if (priv->dispose_has_run)
444 priv->dispose_has_run = TRUE;
446 if (priv->volume_idle_id != 0)
447 g_source_remove (priv->volume_idle_id);
448 priv->volume_idle_id = 0;
450 tp_clear_object (&priv->mic_monitor);
452 /* release any references held by the object here */
454 if (G_OBJECT_CLASS (empathy_audio_src_parent_class)->dispose)
455 G_OBJECT_CLASS (empathy_audio_src_parent_class)->dispose (object);
459 empathy_audio_src_finalize (GObject *object)
461 EmpathyGstAudioSrc *self = EMPATHY_GST_AUDIO_SRC (object);
462 EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (self);
464 /* free any data held directly by the object here */
465 g_mutex_clear (&priv->lock);
467 G_OBJECT_CLASS (empathy_audio_src_parent_class)->finalize (object);
471 empathy_audio_src_volume_changed_idle (gpointer user_data)
473 EmpathyGstAudioSrc *self = EMPATHY_GST_AUDIO_SRC (user_data);
474 EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (self);
478 g_mutex_lock (&priv->lock);
479 priv->volume_idle_id = 0;
480 g_mutex_unlock (&priv->lock);
482 volume = empathy_audio_src_get_hw_volume (self);
484 if (volume != priv->volume)
486 priv->volume = volume;
487 g_object_notify (G_OBJECT (self), "volume");
490 mute = empathy_audio_src_get_hw_mute (self);
491 if (mute != priv->mute)
494 /* hw mute changed, follow with own volume */
495 g_object_set (self->priv->volume_element, "mute", mute, NULL);
496 g_object_notify (G_OBJECT (self), "mute");
503 empathy_audio_src_volume_changed (GObject *object,
507 EmpathyGstAudioSrc *self = EMPATHY_GST_AUDIO_SRC (user_data);
509 g_mutex_lock (&self->priv->lock);
510 if (self->priv->volume_idle_id == 0)
511 self->priv->volume_idle_id = g_idle_add (
512 empathy_audio_src_volume_changed_idle, self);
513 g_mutex_unlock (&self->priv->lock);
519 empathy_audio_src_new (void)
521 static gboolean registered = FALSE;
524 if (!gst_element_register (NULL, "empathyaudiosrc",
525 GST_RANK_NONE, EMPATHY_TYPE_GST_AUDIO_SRC))
529 return gst_element_factory_make ("empathyaudiosrc", NULL);
533 empathy_audio_src_set_echo_cancel (EmpathyGstAudioSrc *src,
536 DEBUG ("Src echo cancellation setting: %s", enable ? "on" : "off");
537 empathy_audio_set_stream_properties (src->priv->src, enable);
541 empathy_audio_src_set_volume (EmpathyGstAudioSrc *src, gdouble volume)
543 g_object_set (src, "volume", volume, NULL);
547 empathy_audio_src_get_volume (EmpathyGstAudioSrc *src)
549 return src->priv->volume;
553 empathy_audio_src_get_microphone (EmpathyGstAudioSrc *src)
555 EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (src);
557 return priv->source_idx;
561 empathy_audio_src_change_microphone_cb (GObject *source_object,
562 GAsyncResult *result,
565 EmpathyMicMonitor *monitor = EMPATHY_MIC_MONITOR (source_object);
566 GSimpleAsyncResult *simple = user_data;
567 GError *error = NULL;
569 if (!empathy_mic_monitor_change_microphone_finish (monitor,
572 g_simple_async_result_take_error (simple, error);
575 g_simple_async_result_complete (simple);
576 g_object_unref (simple);
580 empathy_audio_src_change_microphone_async (EmpathyGstAudioSrc *src,
582 GAsyncReadyCallback callback,
585 EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (src);
586 guint source_output_idx;
587 GSimpleAsyncResult *simple;
589 simple = g_simple_async_result_new (G_OBJECT (src), callback, user_data,
590 empathy_audio_src_change_microphone_async);
592 if (!empathy_audio_src_supports_changing_mic (src))
594 g_simple_async_result_set_error (simple, G_IO_ERROR, G_IO_ERROR_FAILED,
595 "pulsesrc is not new enough to support changing microphone");
596 g_simple_async_result_complete_in_idle (simple);
597 g_object_unref (simple);
601 source_output_idx = empathy_audio_src_get_mic_index (src);
603 if (source_output_idx == PA_INVALID_INDEX)
605 g_simple_async_result_set_error (simple, G_IO_ERROR, G_IO_ERROR_FAILED,
606 "pulsesrc is not yet PLAYING");
607 g_simple_async_result_complete_in_idle (simple);
608 g_object_unref (simple);
612 empathy_mic_monitor_change_microphone_async (priv->mic_monitor,
613 source_output_idx, microphone, empathy_audio_src_change_microphone_cb,
618 empathy_audio_src_change_microphone_finish (EmpathyGstAudioSrc *src,
619 GAsyncResult *result,
622 empathy_implement_finish_void (src,
623 empathy_audio_src_change_microphone_async);
627 empathy_audio_src_set_mute (EmpathyGstAudioSrc *self,
630 empathy_audio_set_hw_mute (self, mute);
632 g_object_notify (G_OBJECT (self), "mute");