2 * Copyright (C) 2011 Collabora Ltd.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 #include <pulse/glib-mainloop.h>
25 #include "empathy-mic-monitor.h"
27 #include "libempathy/empathy-utils.h"
29 #define DEBUG_FLAG EMPATHY_DEBUG_VOIP
30 #include "libempathy/empathy-debug.h"
40 static guint signals[LAST_SIGNAL] = {0};
42 struct _EmpathyMicMonitorPrivate
44 pa_glib_mainloop *loop;
49 G_DEFINE_TYPE (EmpathyMicMonitor, empathy_mic_monitor, G_TYPE_OBJECT);
51 typedef void (*OperationFunc) (EmpathyMicMonitor *, GSimpleAsyncResult *);
56 GSimpleAsyncResult *result;
60 operation_new (OperationFunc func,
61 GSimpleAsyncResult *result)
63 Operation *o = g_slice_new0 (Operation);
72 operation_free (Operation *o,
77 g_simple_async_result_set_error (o->result,
78 G_IO_ERROR, G_IO_ERROR_CANCELLED,
79 "The microphone monitor was disposed");
80 g_simple_async_result_complete (o->result);
81 g_object_unref (o->result);
84 g_slice_free (Operation, o);
88 operations_run (EmpathyMicMonitor *self)
90 EmpathyMicMonitorPrivate *priv = self->priv;
91 pa_context_state_t state = pa_context_get_state (priv->context);
94 if (state != PA_CONTEXT_READY)
97 for (l = priv->operations->head; l != NULL; l = l->next)
99 Operation *o = l->data;
101 o->func (self, o->result);
103 operation_free (o, FALSE);
106 g_queue_clear (priv->operations);
110 empathy_mic_monitor_source_output_info_cb (pa_context *context,
111 const pa_source_output_info *info,
115 EmpathyMicMonitor *self = userdata;
120 g_signal_emit (self, signals[MICROPHONE_CHANGED], 0,
121 info->index, info->source);
125 empathy_mic_monitor_source_info_cb (pa_context *context,
126 const pa_source_info *info,
130 EmpathyMicMonitor *self = userdata;
136 is_monitor = (info->monitor_of_sink != PA_INVALID_INDEX);
138 g_signal_emit (self, signals[MICROPHONE_ADDED], 0,
139 info->index, info->name, info->description, is_monitor);
143 empathy_mic_monitor_pa_event_cb (pa_context *context,
144 pa_subscription_event_type_t type,
148 EmpathyMicMonitor *self = userdata;
150 if ((type & PA_SUBSCRIPTION_EVENT_FACILITY_MASK) == PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT
151 && (type & PA_SUBSCRIPTION_EVENT_TYPE_MASK) == PA_SUBSCRIPTION_EVENT_CHANGE)
153 /* Microphone in the source output has changed */
154 pa_context_get_source_output_info (context, idx,
155 empathy_mic_monitor_source_output_info_cb, self);
157 else if ((type & PA_SUBSCRIPTION_EVENT_FACILITY_MASK) == PA_SUBSCRIPTION_EVENT_SOURCE
158 && (type & PA_SUBSCRIPTION_EVENT_TYPE_MASK) == PA_SUBSCRIPTION_EVENT_REMOVE)
160 /* A mic has been removed */
161 g_signal_emit (self, signals[MICROPHONE_REMOVED], 0, idx);
163 else if ((type & PA_SUBSCRIPTION_EVENT_FACILITY_MASK) == PA_SUBSCRIPTION_EVENT_SOURCE
164 && (type & PA_SUBSCRIPTION_EVENT_TYPE_MASK) == PA_SUBSCRIPTION_EVENT_NEW)
166 /* A mic has been plugged in */
167 pa_context_get_source_info_by_index (context, idx,
168 empathy_mic_monitor_source_info_cb, self);
173 empathy_mic_monitor_pa_subscribe_cb (pa_context *context,
178 DEBUG ("Failed to subscribe to PulseAudio events");
182 empathy_mic_monitor_pa_state_change_cb (pa_context *context,
185 EmpathyMicMonitor *self = userdata;
186 EmpathyMicMonitorPrivate *priv = self->priv;
187 pa_context_state_t state = pa_context_get_state (priv->context);
189 if (state == PA_CONTEXT_READY)
191 /* Listen to pulseaudio events so we know when sources are
192 * added and when the microphone is changed. */
193 pa_context_set_subscribe_callback (priv->context,
194 empathy_mic_monitor_pa_event_cb, self);
195 pa_context_subscribe (priv->context,
196 PA_SUBSCRIPTION_MASK_SOURCE | PA_SUBSCRIPTION_MASK_SOURCE_OUTPUT,
197 empathy_mic_monitor_pa_subscribe_cb, NULL);
199 operations_run (self);
204 empathy_mic_monitor_init (EmpathyMicMonitor *self)
206 EmpathyMicMonitorPrivate *priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
207 EMPATHY_TYPE_MIC_MONITOR, EmpathyMicMonitorPrivate);
213 empathy_mic_monitor_constructed (GObject *obj)
215 EmpathyMicMonitor *self = EMPATHY_MIC_MONITOR (obj);
216 EmpathyMicMonitorPrivate *priv = self->priv;
218 /* PulseAudio stuff: We need to create a dummy pa_glib_mainloop* so
219 * Pulse can use the mainloop that GTK has created for us. */
220 priv->loop = pa_glib_mainloop_new (NULL);
221 priv->context = pa_context_new (pa_glib_mainloop_get_api (priv->loop),
222 "EmpathyMicMonitor");
224 /* Finally listen for state changes so we know when we've
226 pa_context_set_state_callback (priv->context,
227 empathy_mic_monitor_pa_state_change_cb, obj);
228 pa_context_connect (priv->context, NULL, 0, NULL);
230 priv->operations = g_queue_new ();
234 empathy_mic_monitor_dispose (GObject *obj)
236 EmpathyMicMonitor *self = EMPATHY_MIC_MONITOR (obj);
237 EmpathyMicMonitorPrivate *priv = self->priv;
239 g_queue_foreach (priv->operations, (GFunc) operation_free,
240 GUINT_TO_POINTER (TRUE));
241 g_queue_free (priv->operations);
243 if (priv->context != NULL)
244 pa_context_unref (priv->context);
245 priv->context = NULL;
247 if (priv->loop != NULL)
248 pa_glib_mainloop_free (priv->loop);
251 G_OBJECT_CLASS (empathy_mic_monitor_parent_class)->dispose (obj);
255 empathy_mic_monitor_class_init (EmpathyMicMonitorClass *klass)
257 GObjectClass *object_class = G_OBJECT_CLASS (klass);
259 object_class->constructed = empathy_mic_monitor_constructed;
260 object_class->dispose = empathy_mic_monitor_dispose;
262 signals[MICROPHONE_ADDED] = g_signal_new ("microphone-added",
263 G_TYPE_FROM_CLASS (klass),
267 g_cclosure_marshal_generic,
268 G_TYPE_NONE, 4, G_TYPE_UINT, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_BOOLEAN);
270 signals[MICROPHONE_REMOVED] = g_signal_new ("microphone-removed",
271 G_TYPE_FROM_CLASS (klass),
275 g_cclosure_marshal_generic,
276 G_TYPE_NONE, 1, G_TYPE_UINT);
278 signals[MICROPHONE_CHANGED] = g_signal_new ("microphone-changed",
279 G_TYPE_FROM_CLASS (klass),
283 g_cclosure_marshal_generic,
284 G_TYPE_NONE, 2, G_TYPE_UINT, G_TYPE_UINT);
286 g_type_class_add_private (object_class, sizeof (EmpathyMicMonitorPrivate));
290 empathy_mic_monitor_new (void)
292 return g_object_new (EMPATHY_TYPE_MIC_MONITOR,
296 /* operation: list microphones */
298 operation_list_microphones_free (gpointer data)
300 GQueue *queue = data;
303 for (l = queue->head; l != NULL; l = l->next)
305 EmpathyMicrophone *mic = l->data;
308 g_free (mic->description);
309 g_slice_free (EmpathyMicrophone, mic);
312 g_queue_free (queue);
316 operation_list_microphones_cb (pa_context *context,
317 const pa_source_info *info,
321 GSimpleAsyncResult *result = userdata;
322 EmpathyMicrophone *mic;
327 g_simple_async_result_complete (result);
328 g_object_unref (result);
332 mic = g_slice_new0 (EmpathyMicrophone);
333 mic->index = info->index;
334 mic->name = g_strdup (info->name);
335 mic->description = g_strdup (info->description);
336 mic->is_monitor = (info->monitor_of_sink != PA_INVALID_INDEX);
338 /* add it to the queue */
339 queue = g_simple_async_result_get_op_res_gpointer (result);
340 g_queue_push_tail (queue, mic);
344 operation_list_microphones (EmpathyMicMonitor *self,
345 GSimpleAsyncResult *result)
347 EmpathyMicMonitorPrivate *priv = self->priv;
349 g_assert_cmpuint (pa_context_get_state (priv->context), ==, PA_CONTEXT_READY);
351 g_simple_async_result_set_op_res_gpointer (result, g_queue_new (),
352 operation_list_microphones_free);
354 pa_context_get_source_info_list (priv->context,
355 operation_list_microphones_cb, result);
359 empathy_mic_monitor_list_microphones_async (EmpathyMicMonitor *self,
360 GAsyncReadyCallback callback,
363 EmpathyMicMonitorPrivate *priv = self->priv;
364 Operation *operation;
365 GSimpleAsyncResult *simple;
367 simple = g_simple_async_result_new (G_OBJECT (self), callback, user_data,
368 empathy_mic_monitor_list_microphones_async);
370 operation = operation_new (operation_list_microphones, simple);
371 g_queue_push_tail (priv->operations, operation);
374 operations_run (self);
378 empathy_mic_monitor_list_microphones_finish (EmpathyMicMonitor *src,
379 GAsyncResult *result,
382 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
385 if (g_simple_async_result_propagate_error (simple, error))
388 g_return_val_if_fail (g_simple_async_result_is_valid (result,
389 G_OBJECT (src), empathy_mic_monitor_list_microphones_async),
392 queue = g_simple_async_result_get_op_res_gpointer (simple);
396 /* operation: change microphone */
399 guint source_output_idx;
401 } ChangeMicrophoneData;
404 operation_change_microphone_cb (pa_context *context,
408 GSimpleAsyncResult *result = userdata;
412 g_simple_async_result_set_error (result, G_IO_ERROR, G_IO_ERROR_FAILED,
413 "Failed to change microphone. Reason unknown.");
416 g_simple_async_result_complete (result);
417 g_object_unref (result);
421 operation_change_microphone (EmpathyMicMonitor *self,
422 GSimpleAsyncResult *result)
424 EmpathyMicMonitorPrivate *priv = self->priv;
425 ChangeMicrophoneData *data;
427 g_assert_cmpuint (pa_context_get_state (priv->context), ==, PA_CONTEXT_READY);
429 data = g_simple_async_result_get_op_res_gpointer (result);
431 pa_context_move_source_output_by_index (priv->context,
432 data->source_output_idx, data->source_idx,
433 operation_change_microphone_cb, result);
435 g_simple_async_result_set_op_res_gpointer (result, NULL, NULL);
436 g_slice_free (ChangeMicrophoneData, data);
440 empathy_mic_monitor_change_microphone_async (EmpathyMicMonitor *self,
441 guint source_output_idx,
443 GAsyncReadyCallback callback,
446 EmpathyMicMonitorPrivate *priv = self->priv;
447 GSimpleAsyncResult *simple;
448 Operation *operation;
449 ChangeMicrophoneData *data;
451 simple = g_simple_async_result_new (G_OBJECT (self), callback, user_data,
452 empathy_mic_monitor_change_microphone_async);
454 if (source_output_idx == PA_INVALID_INDEX)
456 g_simple_async_result_set_error (simple, G_IO_ERROR, G_IO_ERROR_FAILED,
457 "Invalid source output index");
458 g_simple_async_result_complete_in_idle (simple);
459 g_object_unref (simple);
463 data = g_slice_new0 (ChangeMicrophoneData);
464 data->source_idx = source_idx;
465 data->source_output_idx = source_output_idx;
466 g_simple_async_result_set_op_res_gpointer (simple, data, NULL);
468 operation = operation_new (operation_change_microphone, simple);
469 g_queue_push_tail (priv->operations, operation);
472 operations_run (self);
476 empathy_mic_monitor_change_microphone_finish (EmpathyMicMonitor *self,
477 GAsyncResult *result,
480 empathy_implement_finish_void (self,
481 empathy_mic_monitor_change_microphone_async);
484 /* operation: get current mic */
486 empathy_mic_monitor_get_current_mic_cb (pa_context *context,
487 const pa_source_output_info *info,
491 GSimpleAsyncResult *result = userdata;
496 if (g_simple_async_result_get_op_res_gpointer (result) != NULL)
499 g_simple_async_result_set_op_res_gpointer (result,
500 GUINT_TO_POINTER (info->source), NULL);
501 g_simple_async_result_complete (result);
502 g_object_unref (result);
506 operation_get_current_mic (EmpathyMicMonitor *self,
507 GSimpleAsyncResult *result)
509 EmpathyMicMonitorPrivate *priv = self->priv;
510 guint source_output_idx;
512 g_assert_cmpuint (pa_context_get_state (priv->context), ==, PA_CONTEXT_READY);
514 source_output_idx = GPOINTER_TO_UINT (
515 g_simple_async_result_get_op_res_gpointer (result));
517 /* unset this so we can use it in the cb */
518 g_simple_async_result_set_op_res_gpointer (result, NULL, NULL);
520 pa_context_get_source_output_info (priv->context, source_output_idx,
521 empathy_mic_monitor_get_current_mic_cb, result);
525 empathy_mic_monitor_get_current_mic_async (EmpathyMicMonitor *self,
526 guint source_output_idx,
527 GAsyncReadyCallback callback,
530 EmpathyMicMonitorPrivate *priv = self->priv;
531 Operation *operation;
532 GSimpleAsyncResult *simple;
534 simple = g_simple_async_result_new (G_OBJECT (self), callback, user_data,
535 empathy_mic_monitor_get_current_mic_async);
537 g_simple_async_result_set_op_res_gpointer (simple,
538 GUINT_TO_POINTER (source_output_idx), NULL);
540 operation = operation_new (operation_get_current_mic, simple);
541 g_queue_push_tail (priv->operations, operation);
543 operations_run (self);
547 empathy_mic_monitor_get_current_mic_finish (EmpathyMicMonitor *self,
548 GAsyncResult *result,
551 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
553 if (g_simple_async_result_propagate_error (simple, error))
554 return PA_INVALID_INDEX;
556 g_return_val_if_fail (g_simple_async_result_is_valid (result,
557 G_OBJECT (self), empathy_mic_monitor_get_current_mic_async),
560 return GPOINTER_TO_UINT (
561 g_simple_async_result_get_op_res_gpointer (simple));
564 /* operation: get default */
566 empathy_mic_monitor_get_default_cb (pa_context *context,
567 const pa_server_info *info,
570 GSimpleAsyncResult *result = userdata;
572 /* TODO: it would be nice in future, for consistency, if this gave
573 * the source idx instead of the name. */
574 g_simple_async_result_set_op_res_gpointer (result,
575 g_strdup (info->default_source_name), g_free);
576 g_simple_async_result_complete (result);
577 g_object_unref (result);
581 operation_get_default (EmpathyMicMonitor *self,
582 GSimpleAsyncResult *result)
584 EmpathyMicMonitorPrivate *priv = self->priv;
586 g_assert_cmpuint (pa_context_get_state (priv->context), ==, PA_CONTEXT_READY);
588 /* unset this so we can use it in the cb */
589 g_simple_async_result_set_op_res_gpointer (result, NULL, NULL);
591 pa_context_get_server_info (priv->context, empathy_mic_monitor_get_default_cb,
596 empathy_mic_monitor_get_default_async (EmpathyMicMonitor *self,
597 GAsyncReadyCallback callback,
600 EmpathyMicMonitorPrivate *priv = self->priv;
601 Operation *operation;
602 GSimpleAsyncResult *simple;
604 simple = g_simple_async_result_new (G_OBJECT (self), callback, user_data,
605 empathy_mic_monitor_get_default_async);
607 operation = operation_new (operation_get_default, simple);
608 g_queue_push_tail (priv->operations, operation);
610 operations_run (self);
614 empathy_mic_monitor_get_default_finish (EmpathyMicMonitor *self,
615 GAsyncResult *result,
618 empathy_implement_finish_return_pointer (self,
619 empathy_mic_monitor_get_default_async);
622 /* operation: set default */
624 empathy_mic_monitor_set_default_cb (pa_context *c,
628 GSimpleAsyncResult *result = userdata;
632 g_simple_async_result_set_error (result,
633 G_IO_ERROR, G_IO_ERROR_FAILED,
634 "The operation failed for an unknown reason");
637 g_simple_async_result_complete (result);
638 g_object_unref (result);
642 operation_set_default (EmpathyMicMonitor *self,
643 GSimpleAsyncResult *result)
645 EmpathyMicMonitorPrivate *priv = self->priv;
648 g_assert_cmpuint (pa_context_get_state (priv->context), ==, PA_CONTEXT_READY);
650 name = g_simple_async_result_get_op_res_gpointer (result);
652 pa_context_set_default_source (priv->context, name,
653 empathy_mic_monitor_set_default_cb, result);
657 empathy_mic_monitor_set_default_async (EmpathyMicMonitor *self,
659 GAsyncReadyCallback callback,
662 EmpathyMicMonitorPrivate *priv = self->priv;
663 Operation *operation;
664 GSimpleAsyncResult *simple;
666 simple = g_simple_async_result_new (G_OBJECT (self), callback, user_data,
667 empathy_mic_monitor_set_default_async);
669 g_simple_async_result_set_op_res_gpointer (simple, g_strdup (name), g_free);
671 operation = operation_new (operation_set_default, simple);
672 g_queue_push_tail (priv->operations, operation);
674 operations_run (self);
678 empathy_mic_monitor_set_default_finish (EmpathyMicMonitor *self,
679 GAsyncResult *result,
682 empathy_implement_finish_void (self,
683 empathy_mic_monitor_set_default_async);