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
25 #include <pulse/pulseaudio.h>
26 #include <pulse/glib-mainloop.h>
28 #include <libempathy/empathy-utils.h>
30 #include "empathy-audio-src.h"
32 #include "src-marshal.h"
34 #define DEBUG_FLAG EMPATHY_DEBUG_VOIP
35 #include <libempathy/empathy-debug.h>
37 G_DEFINE_TYPE(EmpathyGstAudioSrc, empathy_audio_src, GST_TYPE_BIN)
49 static guint signals[LAST_SIGNAL] = {0};
58 /* private structure */
59 typedef struct _EmpathyGstAudioSrcPrivate EmpathyGstAudioSrcPrivate;
61 struct _EmpathyGstAudioSrcPrivate
63 gboolean dispose_has_run;
68 pa_glib_mainloop *loop;
72 /* 0 if not known yet */
73 guint source_output_idx;
74 /* G_MAXUINT if not known yet */
84 #define EMPATHY_GST_AUDIO_SRC_GET_PRIVATE(o) \
85 (G_TYPE_INSTANCE_GET_PRIVATE ((o), EMPATHY_TYPE_GST_AUDIO_SRC, \
86 EmpathyGstAudioSrcPrivate))
88 typedef void (*OperationFunc) (EmpathyGstAudioSrc *, GSimpleAsyncResult *);
93 GSimpleAsyncResult *result;
97 operation_new (OperationFunc func,
98 GSimpleAsyncResult *result)
100 Operation *o = g_slice_new0 (Operation);
109 operation_free (Operation *o,
114 g_simple_async_result_set_error (o->result,
115 G_IO_ERROR, G_IO_ERROR_CANCELLED,
116 "The audio source was disposed");
117 g_simple_async_result_complete (o->result);
118 g_object_unref (o->result);
121 g_slice_free (Operation, o);
125 operation_get_microphones_free (gpointer data)
127 GQueue *queue = data;
130 for (l = queue->head; l != NULL; l = l->next)
132 EmpathyAudioSrcMicrophone *mic = l->data;
135 g_free (mic->description);
136 g_slice_free (EmpathyAudioSrcMicrophone, mic);
139 g_queue_free (queue);
143 operation_get_microphones_cb (pa_context *context,
144 const pa_source_info *info,
148 GSimpleAsyncResult *result = userdata;
149 EmpathyAudioSrcMicrophone *mic;
154 g_simple_async_result_complete (result);
155 g_object_unref (result);
159 mic = g_slice_new0 (EmpathyAudioSrcMicrophone);
160 mic->index = info->index;
161 mic->name = g_strdup (info->name);
162 mic->description = g_strdup (info->description);
163 mic->is_monitor = (info->monitor_of_sink != PA_INVALID_INDEX);
165 /* add it to the queue */
166 queue = g_simple_async_result_get_op_res_gpointer (result);
167 g_queue_push_tail (queue, mic);
171 operation_get_microphones (EmpathyGstAudioSrc *self,
172 GSimpleAsyncResult *result)
174 EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (self);
176 g_assert_cmpuint (pa_context_get_state (priv->context), ==, PA_CONTEXT_READY);
178 g_simple_async_result_set_op_res_gpointer (result, g_queue_new (),
179 operation_get_microphones_free);
181 pa_context_get_source_info_list (priv->context,
182 operation_get_microphones_cb, result);
186 operation_change_microphone_cb (pa_context *context,
190 GSimpleAsyncResult *result = userdata;
194 g_simple_async_result_set_error (result, G_IO_ERROR, G_IO_ERROR_FAILED,
195 "Failed to change microphone. Reason unknown.");
198 g_simple_async_result_complete (result);
199 g_object_unref (result);
203 operation_change_microphone (EmpathyGstAudioSrc *self,
204 GSimpleAsyncResult *result)
206 EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (self);
207 guint source_output_idx, microphone;
209 g_object_get (priv->src, "source-output-index", &source_output_idx, NULL);
211 g_assert_cmpuint (pa_context_get_state (priv->context), ==, PA_CONTEXT_READY);
212 g_assert_cmpuint (source_output_idx, !=, PA_INVALID_INDEX);
214 microphone = GPOINTER_TO_UINT (
215 g_simple_async_result_get_op_res_gpointer (result));
217 pa_context_move_source_output_by_index (priv->context, source_output_idx, microphone,
218 operation_change_microphone_cb, result);
222 operations_run (EmpathyGstAudioSrc *self)
224 EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (self);
225 pa_context_state_t state = pa_context_get_state (priv->context);
228 if (state != PA_CONTEXT_READY)
231 for (l = priv->operations->head; l != NULL; l = l->next)
233 Operation *o = l->data;
235 o->func (self, o->result);
237 operation_free (o, FALSE);
240 g_queue_clear (priv->operations);
244 empathy_audio_src_source_output_info_cb (pa_context *context,
245 const pa_source_output_info *info,
249 EmpathyGstAudioSrc *self = userdata;
250 EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (self);
255 /* There should only be one call here. */
257 if (priv->source_idx == info->source)
260 priv->source_idx = info->source;
261 g_object_notify (G_OBJECT (self), "microphone");
265 empathy_audio_src_source_info_cb (pa_context *context,
266 const pa_source_info *info,
270 EmpathyGstAudioSrc *self = userdata;
276 is_monitor = (info->monitor_of_sink != PA_INVALID_INDEX);
278 g_signal_emit (self, signals[MICROPHONE_ADDED], 0,
279 info->index, info->name, info->description, is_monitor);
283 empathy_audio_src_pa_event_cb (pa_context *context,
284 pa_subscription_event_type_t type,
288 EmpathyGstAudioSrc *self = userdata;
289 EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (self);
291 if ((type & PA_SUBSCRIPTION_EVENT_FACILITY_MASK) == PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT
292 && (type & PA_SUBSCRIPTION_EVENT_TYPE_MASK) == PA_SUBSCRIPTION_EVENT_CHANGE
293 && idx == priv->source_output_idx)
295 /* Microphone in the source output has changed */
296 pa_context_get_source_output_info (context, idx,
297 empathy_audio_src_source_output_info_cb, self);
299 else if ((type & PA_SUBSCRIPTION_EVENT_FACILITY_MASK) == PA_SUBSCRIPTION_EVENT_SOURCE
300 && (type & PA_SUBSCRIPTION_EVENT_TYPE_MASK) == PA_SUBSCRIPTION_EVENT_REMOVE)
302 /* A mic has been removed */
303 g_signal_emit (self, signals[MICROPHONE_REMOVED], 0, idx);
305 else if ((type & PA_SUBSCRIPTION_EVENT_FACILITY_MASK) == PA_SUBSCRIPTION_EVENT_SOURCE
306 && (type & PA_SUBSCRIPTION_EVENT_TYPE_MASK) == PA_SUBSCRIPTION_EVENT_NEW)
308 /* A mic has been plugged in */
309 pa_context_get_source_info_by_index (context, idx,
310 empathy_audio_src_source_info_cb, self);
315 empathy_audio_src_pa_subscribe_cb (pa_context *context,
320 DEBUG ("Failed to subscribe to PulseAudio events");
324 empathy_audio_src_pa_state_change_cb (pa_context *context,
327 EmpathyGstAudioSrc *self = userdata;
328 EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (self);
329 pa_context_state_t state = pa_context_get_state (priv->context);
331 if (state == PA_CONTEXT_READY)
333 /* Listen to pulseaudio events so we know when sources are
334 * added and when the microphone is changed. */
335 pa_context_set_subscribe_callback (priv->context,
336 empathy_audio_src_pa_event_cb, self);
337 pa_context_subscribe (priv->context,
338 PA_SUBSCRIPTION_MASK_SOURCE | PA_SUBSCRIPTION_MASK_SOURCE_OUTPUT,
339 empathy_audio_src_pa_subscribe_cb, NULL);
341 operations_run (self);
346 empathy_audio_src_source_output_index_notify (GObject *object,
348 EmpathyGstAudioSrc *self)
350 EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (self);
351 guint source_output_idx = PA_INVALID_INDEX;
353 g_object_get (priv->src, "source-output-index", &source_output_idx, NULL);
355 if (source_output_idx == PA_INVALID_INDEX)
358 if (priv->source_output_idx == source_output_idx)
361 /* It's actually changed. */
362 priv->source_output_idx = source_output_idx;
364 pa_context_get_source_output_info (priv->context, source_output_idx,
365 empathy_audio_src_source_output_info_cb, self);
369 empathy_audio_src_init (EmpathyGstAudioSrc *obj)
371 EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (obj);
373 const gchar *src_element;
375 priv->peak_level = -G_MAXDOUBLE;
376 priv->lock = g_mutex_new ();
378 src_element = g_getenv ("EMPATHY_AUDIO_SRC");
379 if (src_element == NULL)
380 src_element = "pulsesrc";
382 priv->src = gst_element_factory_make (src_element, NULL);
383 gst_bin_add (GST_BIN (obj), priv->src);
385 priv->volume = gst_element_factory_make ("volume", NULL);
386 g_object_ref (priv->volume);
388 gst_bin_add (GST_BIN (obj), priv->volume);
389 gst_element_link (priv->src, priv->volume);
391 priv->level = gst_element_factory_make ("level", NULL);
392 gst_bin_add (GST_BIN (obj), priv->level);
393 gst_element_link (priv->volume, priv->level);
395 src = gst_element_get_static_pad (priv->level, "src");
397 ghost = gst_ghost_pad_new ("src", src);
398 gst_element_add_pad (GST_ELEMENT (obj), ghost);
400 gst_object_unref (G_OBJECT (src));
402 /* PulseAudio stuff: We need to create a dummy pa_glib_mainloop* so
403 * Pulse can use the mainloop that GTK has created for us. */
404 priv->loop = pa_glib_mainloop_new (NULL);
405 priv->context = pa_context_new (pa_glib_mainloop_get_api (priv->loop),
408 /* Listen to changes to GstPulseSrc:source-output-index so we know when
409 * it's no longer PA_INVALID_INDEX (starting for the first time) or if it
410 * changes (READY->NULL->READY...) */
411 g_signal_connect (priv->src, "notify::source-output-index",
412 G_CALLBACK (empathy_audio_src_source_output_index_notify),
415 /* Finally listen for state changes so we know when we've
417 pa_context_set_state_callback (priv->context,
418 empathy_audio_src_pa_state_change_cb, obj);
419 pa_context_connect (priv->context, NULL, 0, NULL);
421 priv->operations = g_queue_new ();
424 static void empathy_audio_src_dispose (GObject *object);
425 static void empathy_audio_src_finalize (GObject *object);
426 static void empathy_audio_src_handle_message (GstBin *bin,
427 GstMessage *message);
429 static gboolean empathy_audio_src_levels_updated (gpointer user_data);
432 empathy_audio_src_set_property (GObject *object,
433 guint property_id, const GValue *value, GParamSpec *pspec)
438 empathy_audio_src_set_volume (EMPATHY_GST_AUDIO_SRC (object),
439 g_value_get_double (value));
442 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
447 empathy_audio_src_get_property (GObject *object,
448 guint property_id, GValue *value, GParamSpec *pspec)
450 EmpathyGstAudioSrc *self = EMPATHY_GST_AUDIO_SRC (object);
451 EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (self);
456 g_value_set_double (value,
457 empathy_audio_src_get_volume (self));
459 case PROP_PEAK_LEVEL:
460 g_mutex_lock (priv->lock);
461 g_value_set_double (value, priv->peak_level);
462 g_mutex_unlock (priv->lock);
465 g_mutex_lock (priv->lock);
466 g_value_set_double (value, priv->rms_level);
467 g_mutex_unlock (priv->lock);
469 case PROP_MICROPHONE:
470 g_value_set_uint (value, priv->source_idx);
473 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
478 empathy_audio_src_class_init (EmpathyGstAudioSrcClass
479 *empathy_audio_src_class)
481 GObjectClass *object_class = G_OBJECT_CLASS (empathy_audio_src_class);
482 GstBinClass *gstbin_class = GST_BIN_CLASS (empathy_audio_src_class);
483 GParamSpec *param_spec;
485 g_type_class_add_private (empathy_audio_src_class,
486 sizeof (EmpathyGstAudioSrcPrivate));
488 object_class->dispose = empathy_audio_src_dispose;
489 object_class->finalize = empathy_audio_src_finalize;
491 object_class->set_property = empathy_audio_src_set_property;
492 object_class->get_property = empathy_audio_src_get_property;
494 gstbin_class->handle_message =
495 GST_DEBUG_FUNCPTR (empathy_audio_src_handle_message);
497 param_spec = g_param_spec_double ("volume", "Volume", "volume contol",
499 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
500 g_object_class_install_property (object_class, PROP_VOLUME, param_spec);
502 param_spec = g_param_spec_double ("peak-level", "peak level", "peak level",
503 -G_MAXDOUBLE, G_MAXDOUBLE, 0,
504 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
505 g_object_class_install_property (object_class, PROP_PEAK_LEVEL, param_spec);
507 param_spec = g_param_spec_uint ("microphone", "microphone", "microphone",
508 0, G_MAXUINT, G_MAXUINT,
509 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
510 g_object_class_install_property (object_class, PROP_MICROPHONE, param_spec);
512 signals[PEAK_LEVEL_CHANGED] = g_signal_new ("peak-level-changed",
513 G_TYPE_FROM_CLASS (empathy_audio_src_class),
517 g_cclosure_marshal_VOID__DOUBLE,
518 G_TYPE_NONE, 1, G_TYPE_DOUBLE);
520 param_spec = g_param_spec_double ("rms-level", "RMS level", "RMS level",
521 -G_MAXDOUBLE, G_MAXDOUBLE, 0,
522 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
523 g_object_class_install_property (object_class, PROP_RMS_LEVEL, param_spec);
526 signals[RMS_LEVEL_CHANGED] = g_signal_new ("rms-level-changed",
527 G_TYPE_FROM_CLASS (empathy_audio_src_class),
531 g_cclosure_marshal_VOID__DOUBLE,
532 G_TYPE_NONE, 1, G_TYPE_DOUBLE);
534 signals[MICROPHONE_ADDED] = g_signal_new ("microphone-added",
535 G_TYPE_FROM_CLASS (empathy_audio_src_class),
539 _src_marshal_VOID__UINT_STRING_STRING_BOOLEAN,
540 G_TYPE_NONE, 4, G_TYPE_UINT, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_BOOLEAN);
542 signals[MICROPHONE_REMOVED] = g_signal_new ("microphone-removed",
543 G_TYPE_FROM_CLASS (empathy_audio_src_class),
547 g_cclosure_marshal_VOID__UINT,
548 G_TYPE_NONE, 1, G_TYPE_UINT);
552 empathy_audio_src_dispose (GObject *object)
554 EmpathyGstAudioSrc *self = EMPATHY_GST_AUDIO_SRC (object);
555 EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (self);
557 if (priv->dispose_has_run)
560 priv->dispose_has_run = TRUE;
562 if (priv->idle_id != 0)
563 g_source_remove (priv->idle_id);
567 if (priv->context != NULL)
568 pa_context_unref (priv->context);
569 priv->context = NULL;
571 if (priv->loop != NULL)
572 pa_glib_mainloop_free (priv->loop);
575 /* release any references held by the object here */
577 if (G_OBJECT_CLASS (empathy_audio_src_parent_class)->dispose)
578 G_OBJECT_CLASS (empathy_audio_src_parent_class)->dispose (object);
582 empathy_audio_src_finalize (GObject *object)
584 EmpathyGstAudioSrc *self = EMPATHY_GST_AUDIO_SRC (object);
585 EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (self);
587 /* free any data held directly by the object here */
588 g_mutex_free (priv->lock);
590 g_queue_foreach (priv->operations, (GFunc) operation_free,
591 GUINT_TO_POINTER (TRUE));
592 g_queue_free (priv->operations);
594 G_OBJECT_CLASS (empathy_audio_src_parent_class)->finalize (object);
598 empathy_audio_src_levels_updated (gpointer user_data)
600 EmpathyGstAudioSrc *self = EMPATHY_GST_AUDIO_SRC (user_data);
601 EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (self);
603 g_mutex_lock (priv->lock);
605 g_signal_emit (self, signals[PEAK_LEVEL_CHANGED], 0, priv->peak_level);
606 g_signal_emit (self, signals[RMS_LEVEL_CHANGED], 0, priv->rms_level);
609 g_mutex_unlock (priv->lock);
615 empathy_audio_src_handle_message (GstBin *bin, GstMessage *message)
617 EmpathyGstAudioSrc *self = EMPATHY_GST_AUDIO_SRC (bin);
618 EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (self);
620 if (GST_MESSAGE_TYPE (message) == GST_MESSAGE_ELEMENT &&
621 GST_MESSAGE_SRC (message) == GST_OBJECT (priv->level))
623 const GstStructure *s;
627 gdouble peak = -G_MAXDOUBLE;
628 gdouble rms = -G_MAXDOUBLE;
630 s = gst_message_get_structure (message);
631 name = gst_structure_get_name (s);
633 if (g_strcmp0 ("level", name) != 0)
636 list = gst_structure_get_value (s, "peak");
637 len = gst_value_list_get_size (list);
639 for (i =0 ; i < len; i++)
644 value = gst_value_list_get_value (list, i);
645 db = g_value_get_double (value);
646 peak = MAX (db, peak);
649 list = gst_structure_get_value (s, "rms");
650 len = gst_value_list_get_size (list);
652 for (i =0 ; i < len; i++)
657 value = gst_value_list_get_value (list, i);
658 db = g_value_get_double (value);
662 g_mutex_lock (priv->lock);
664 priv->peak_level = peak;
665 priv->rms_level = rms;
666 if (priv->idle_id == 0)
667 priv->idle_id = g_idle_add (empathy_audio_src_levels_updated, self);
669 g_mutex_unlock (priv->lock);
673 GST_BIN_CLASS (empathy_audio_src_parent_class)->handle_message (bin,
678 empathy_audio_src_new (void)
680 static gboolean registered = FALSE;
683 if (!gst_element_register (NULL, "empathyaudiosrc",
684 GST_RANK_NONE, EMPATHY_TYPE_GST_AUDIO_SRC))
688 return gst_element_factory_make ("empathyaudiosrc", NULL);
692 empathy_audio_src_set_volume (EmpathyGstAudioSrc *src, gdouble volume)
694 EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (src);
696 GParamSpecDouble *pspec_double;
698 pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (priv->volume),
701 g_assert (pspec != NULL);
703 pspec_double = G_PARAM_SPEC_DOUBLE (pspec);
705 volume = CLAMP (volume, pspec_double->minimum, pspec_double->maximum);
707 g_object_set (G_OBJECT (priv->volume), "volume", volume, NULL);
711 empathy_audio_src_get_volume (EmpathyGstAudioSrc *src)
713 EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (src);
716 g_object_get (G_OBJECT (priv->volume), "volume", &volume, NULL);
722 empathy_audio_src_get_microphones_async (EmpathyGstAudioSrc *src,
723 GAsyncReadyCallback callback,
726 EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (src);
727 Operation *operation;
728 GSimpleAsyncResult *simple;
730 simple = g_simple_async_result_new (G_OBJECT (src), callback, user_data,
731 empathy_audio_src_get_microphones_async);
733 operation = operation_new (operation_get_microphones, simple);
734 g_queue_push_tail (priv->operations, operation);
737 operations_run (src);
741 empathy_audio_src_get_microphones_finish (EmpathyGstAudioSrc *src,
742 GAsyncResult *result,
745 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
748 if (g_simple_async_result_propagate_error (simple, error))
751 g_return_val_if_fail (g_simple_async_result_is_valid (result,
752 G_OBJECT (src), empathy_audio_src_get_microphones_async),
755 queue = g_simple_async_result_get_op_res_gpointer (simple);
760 empathy_audio_src_get_microphone (EmpathyGstAudioSrc *src)
762 EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (src);
764 return priv->source_idx;
768 empathy_audio_src_change_microphone_async (EmpathyGstAudioSrc *src,
770 GAsyncReadyCallback callback,
773 EmpathyGstAudioSrcPrivate *priv = EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (src);
774 guint source_output_idx;
775 GSimpleAsyncResult *simple;
776 Operation *operation;
778 simple = g_simple_async_result_new (G_OBJECT (src), callback, user_data,
779 empathy_audio_src_change_microphone_async);
781 g_object_get (priv->src, "source-output-index", &source_output_idx, NULL);
783 if (source_output_idx == PA_INVALID_INDEX)
785 g_simple_async_result_set_error (simple, G_IO_ERROR, G_IO_ERROR_FAILED,
786 "pulsesrc is not yet PLAYING");
787 g_simple_async_result_complete_in_idle (simple);
788 g_object_unref (simple);
792 g_simple_async_result_set_op_res_gpointer (simple,
793 GUINT_TO_POINTER (microphone), NULL);
795 operation = operation_new (operation_change_microphone, simple);
796 g_queue_push_tail (priv->operations, operation);
799 operations_run (src);
803 empathy_audio_src_change_microphone_finish (EmpathyGstAudioSrc *src,
804 GAsyncResult *result,
807 empathy_implement_finish_void (src,
808 empathy_audio_src_change_microphone_async);