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
24 #include <pulse/pulseaudio.h>
25 #include <pulse/glib-mainloop.h>
27 #include "empathy-mic-monitor.h"
29 #include "src-marshal.h"
31 #include <libempathy/empathy-utils.h>
33 #define DEBUG_FLAG EMPATHY_DEBUG_VOIP
34 #include <libempathy/empathy-debug.h>
44 static guint signals[LAST_SIGNAL] = {0};
46 struct _EmpathyMicMonitorPrivate
48 pa_glib_mainloop *loop;
53 G_DEFINE_TYPE (EmpathyMicMonitor, empathy_mic_monitor, G_TYPE_OBJECT);
55 typedef void (*OperationFunc) (EmpathyMicMonitor *, GSimpleAsyncResult *);
60 GSimpleAsyncResult *result;
64 operation_new (OperationFunc func,
65 GSimpleAsyncResult *result)
67 Operation *o = g_slice_new0 (Operation);
76 operation_free (Operation *o,
81 g_simple_async_result_set_error (o->result,
82 G_IO_ERROR, G_IO_ERROR_CANCELLED,
83 "The microphone monitor was disposed");
84 g_simple_async_result_complete (o->result);
85 g_object_unref (o->result);
88 g_slice_free (Operation, o);
92 operations_run (EmpathyMicMonitor *self)
94 EmpathyMicMonitorPrivate *priv = self->priv;
95 pa_context_state_t state = pa_context_get_state (priv->context);
98 if (state != PA_CONTEXT_READY)
101 for (l = priv->operations->head; l != NULL; l = l->next)
103 Operation *o = l->data;
105 o->func (self, o->result);
107 operation_free (o, FALSE);
110 g_queue_clear (priv->operations);
114 empathy_mic_monitor_source_output_info_cb (pa_context *context,
115 const pa_source_output_info *info,
119 EmpathyMicMonitor *self = userdata;
124 g_signal_emit (self, signals[MICROPHONE_CHANGED], 0,
125 info->index, info->source);
129 empathy_mic_monitor_source_info_cb (pa_context *context,
130 const pa_source_info *info,
134 EmpathyMicMonitor *self = userdata;
140 is_monitor = (info->monitor_of_sink != PA_INVALID_INDEX);
142 g_signal_emit (self, signals[MICROPHONE_ADDED], 0,
143 info->index, info->name, info->description, is_monitor);
147 empathy_mic_monitor_pa_event_cb (pa_context *context,
148 pa_subscription_event_type_t type,
152 EmpathyMicMonitor *self = userdata;
154 if ((type & PA_SUBSCRIPTION_EVENT_FACILITY_MASK) == PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT
155 && (type & PA_SUBSCRIPTION_EVENT_TYPE_MASK) == PA_SUBSCRIPTION_EVENT_CHANGE)
157 /* Microphone in the source output has changed */
158 pa_context_get_source_output_info (context, idx,
159 empathy_mic_monitor_source_output_info_cb, self);
161 else if ((type & PA_SUBSCRIPTION_EVENT_FACILITY_MASK) == PA_SUBSCRIPTION_EVENT_SOURCE
162 && (type & PA_SUBSCRIPTION_EVENT_TYPE_MASK) == PA_SUBSCRIPTION_EVENT_REMOVE)
164 /* A mic has been removed */
165 g_signal_emit (self, signals[MICROPHONE_REMOVED], 0, idx);
167 else if ((type & PA_SUBSCRIPTION_EVENT_FACILITY_MASK) == PA_SUBSCRIPTION_EVENT_SOURCE
168 && (type & PA_SUBSCRIPTION_EVENT_TYPE_MASK) == PA_SUBSCRIPTION_EVENT_NEW)
170 /* A mic has been plugged in */
171 pa_context_get_source_info_by_index (context, idx,
172 empathy_mic_monitor_source_info_cb, self);
177 empathy_mic_monitor_pa_subscribe_cb (pa_context *context,
182 DEBUG ("Failed to subscribe to PulseAudio events");
186 empathy_mic_monitor_pa_state_change_cb (pa_context *context,
189 EmpathyMicMonitor *self = userdata;
190 EmpathyMicMonitorPrivate *priv = self->priv;
191 pa_context_state_t state = pa_context_get_state (priv->context);
193 if (state == PA_CONTEXT_READY)
195 /* Listen to pulseaudio events so we know when sources are
196 * added and when the microphone is changed. */
197 pa_context_set_subscribe_callback (priv->context,
198 empathy_mic_monitor_pa_event_cb, self);
199 pa_context_subscribe (priv->context,
200 PA_SUBSCRIPTION_MASK_SOURCE | PA_SUBSCRIPTION_MASK_SOURCE_OUTPUT,
201 empathy_mic_monitor_pa_subscribe_cb, NULL);
203 operations_run (self);
208 empathy_mic_monitor_init (EmpathyMicMonitor *self)
210 EmpathyMicMonitorPrivate *priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
211 EMPATHY_TYPE_MIC_MONITOR, EmpathyMicMonitorPrivate);
217 empathy_mic_monitor_constructed (GObject *obj)
219 EmpathyMicMonitor *self = EMPATHY_MIC_MONITOR (obj);
220 EmpathyMicMonitorPrivate *priv = self->priv;
222 /* PulseAudio stuff: We need to create a dummy pa_glib_mainloop* so
223 * Pulse can use the mainloop that GTK has created for us. */
224 priv->loop = pa_glib_mainloop_new (NULL);
225 priv->context = pa_context_new (pa_glib_mainloop_get_api (priv->loop),
226 "EmpathyMicMonitor");
228 /* Finally listen for state changes so we know when we've
230 pa_context_set_state_callback (priv->context,
231 empathy_mic_monitor_pa_state_change_cb, obj);
232 pa_context_connect (priv->context, NULL, 0, NULL);
234 priv->operations = g_queue_new ();
238 empathy_mic_monitor_dispose (GObject *obj)
240 EmpathyMicMonitor *self = EMPATHY_MIC_MONITOR (obj);
241 EmpathyMicMonitorPrivate *priv = self->priv;
243 g_queue_foreach (priv->operations, (GFunc) operation_free,
244 GUINT_TO_POINTER (TRUE));
245 g_queue_free (priv->operations);
247 if (priv->context != NULL)
248 pa_context_unref (priv->context);
249 priv->context = NULL;
251 if (priv->loop != NULL)
252 pa_glib_mainloop_free (priv->loop);
255 G_OBJECT_CLASS (empathy_mic_monitor_parent_class)->dispose (obj);
259 empathy_mic_monitor_class_init (EmpathyMicMonitorClass *klass)
261 GObjectClass *object_class = G_OBJECT_CLASS (klass);
263 object_class->constructed = empathy_mic_monitor_constructed;
264 object_class->dispose = empathy_mic_monitor_dispose;
266 signals[MICROPHONE_ADDED] = g_signal_new ("microphone-added",
267 G_TYPE_FROM_CLASS (klass),
271 _src_marshal_VOID__UINT_STRING_STRING_BOOLEAN,
272 G_TYPE_NONE, 4, G_TYPE_UINT, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_BOOLEAN);
274 signals[MICROPHONE_REMOVED] = g_signal_new ("microphone-removed",
275 G_TYPE_FROM_CLASS (klass),
279 g_cclosure_marshal_VOID__UINT,
280 G_TYPE_NONE, 1, G_TYPE_UINT);
282 signals[MICROPHONE_CHANGED] = g_signal_new ("microphone-changed",
283 G_TYPE_FROM_CLASS (klass),
287 _src_marshal_VOID__UINT_UINT,
288 G_TYPE_NONE, 2, G_TYPE_UINT, G_TYPE_UINT);
290 g_type_class_add_private (object_class, sizeof (EmpathyMicMonitorPrivate));
294 empathy_mic_monitor_new (void)
296 return g_object_new (EMPATHY_TYPE_MIC_MONITOR,
300 /* operation: list microphones */
302 operation_list_microphones_free (gpointer data)
304 GQueue *queue = data;
307 for (l = queue->head; l != NULL; l = l->next)
309 EmpathyMicrophone *mic = l->data;
312 g_free (mic->description);
313 g_slice_free (EmpathyMicrophone, mic);
316 g_queue_free (queue);
320 operation_list_microphones_cb (pa_context *context,
321 const pa_source_info *info,
325 GSimpleAsyncResult *result = userdata;
326 EmpathyMicrophone *mic;
331 g_simple_async_result_complete (result);
332 g_object_unref (result);
336 mic = g_slice_new0 (EmpathyMicrophone);
337 mic->index = info->index;
338 mic->name = g_strdup (info->name);
339 mic->description = g_strdup (info->description);
340 mic->is_monitor = (info->monitor_of_sink != PA_INVALID_INDEX);
342 /* add it to the queue */
343 queue = g_simple_async_result_get_op_res_gpointer (result);
344 g_queue_push_tail (queue, mic);
348 operation_list_microphones (EmpathyMicMonitor *self,
349 GSimpleAsyncResult *result)
351 EmpathyMicMonitorPrivate *priv = self->priv;
353 g_assert_cmpuint (pa_context_get_state (priv->context), ==, PA_CONTEXT_READY);
355 g_simple_async_result_set_op_res_gpointer (result, g_queue_new (),
356 operation_list_microphones_free);
358 pa_context_get_source_info_list (priv->context,
359 operation_list_microphones_cb, result);
363 empathy_mic_monitor_list_microphones_async (EmpathyMicMonitor *self,
364 GAsyncReadyCallback callback,
367 EmpathyMicMonitorPrivate *priv = self->priv;
368 Operation *operation;
369 GSimpleAsyncResult *simple;
371 simple = g_simple_async_result_new (G_OBJECT (self), callback, user_data,
372 empathy_mic_monitor_list_microphones_async);
374 operation = operation_new (operation_list_microphones, simple);
375 g_queue_push_tail (priv->operations, operation);
378 operations_run (self);
382 empathy_mic_monitor_list_microphones_finish (EmpathyMicMonitor *src,
383 GAsyncResult *result,
386 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
389 if (g_simple_async_result_propagate_error (simple, error))
392 g_return_val_if_fail (g_simple_async_result_is_valid (result,
393 G_OBJECT (src), empathy_mic_monitor_list_microphones_async),
396 queue = g_simple_async_result_get_op_res_gpointer (simple);
400 /* operation: change microphone */
403 guint source_output_idx;
405 } ChangeMicrophoneData;
408 operation_change_microphone_cb (pa_context *context,
412 GSimpleAsyncResult *result = userdata;
416 g_simple_async_result_set_error (result, G_IO_ERROR, G_IO_ERROR_FAILED,
417 "Failed to change microphone. Reason unknown.");
420 g_simple_async_result_complete (result);
421 g_object_unref (result);
425 operation_change_microphone (EmpathyMicMonitor *self,
426 GSimpleAsyncResult *result)
428 EmpathyMicMonitorPrivate *priv = self->priv;
429 ChangeMicrophoneData *data;
431 g_assert_cmpuint (pa_context_get_state (priv->context), ==, PA_CONTEXT_READY);
433 data = g_simple_async_result_get_op_res_gpointer (result);
435 pa_context_move_source_output_by_index (priv->context,
436 data->source_output_idx, data->source_idx,
437 operation_change_microphone_cb, result);
439 g_simple_async_result_set_op_res_gpointer (result, NULL, NULL);
440 g_slice_free (ChangeMicrophoneData, data);
444 empathy_mic_monitor_change_microphone_async (EmpathyMicMonitor *self,
445 guint source_output_idx,
447 GAsyncReadyCallback callback,
450 EmpathyMicMonitorPrivate *priv = self->priv;
451 GSimpleAsyncResult *simple;
452 Operation *operation;
453 ChangeMicrophoneData *data;
455 simple = g_simple_async_result_new (G_OBJECT (self), callback, user_data,
456 empathy_mic_monitor_change_microphone_async);
458 if (source_output_idx == PA_INVALID_INDEX)
460 g_simple_async_result_set_error (simple, G_IO_ERROR, G_IO_ERROR_FAILED,
461 "Invalid source output index");
462 g_simple_async_result_complete_in_idle (simple);
463 g_object_unref (simple);
467 data = g_slice_new0 (ChangeMicrophoneData);
468 data->source_idx = source_idx;
469 data->source_output_idx = source_output_idx;
470 g_simple_async_result_set_op_res_gpointer (simple, data, NULL);
472 operation = operation_new (operation_change_microphone, simple);
473 g_queue_push_tail (priv->operations, operation);
476 operations_run (self);
480 empathy_mic_monitor_change_microphone_finish (EmpathyMicMonitor *self,
481 GAsyncResult *result,
484 empathy_implement_finish_void (self,
485 empathy_mic_monitor_change_microphone_async);
488 /* operation: get current mic */
490 empathy_mic_monitor_get_current_mic_cb (pa_context *context,
491 const pa_source_output_info *info,
495 GSimpleAsyncResult *result = userdata;
500 if (g_simple_async_result_get_op_res_gpointer (result) != NULL)
503 g_simple_async_result_set_op_res_gpointer (result,
504 GUINT_TO_POINTER (info->source), NULL);
505 g_simple_async_result_complete (result);
506 g_object_unref (result);
510 operation_get_current_mic (EmpathyMicMonitor *self,
511 GSimpleAsyncResult *result)
513 EmpathyMicMonitorPrivate *priv = self->priv;
514 guint source_output_idx;
516 g_assert_cmpuint (pa_context_get_state (priv->context), ==, PA_CONTEXT_READY);
518 source_output_idx = GPOINTER_TO_UINT (
519 g_simple_async_result_get_op_res_gpointer (result));
521 /* unset this so we can use it in the cb */
522 g_simple_async_result_set_op_res_gpointer (result, NULL, NULL);
524 pa_context_get_source_output_info (priv->context, source_output_idx,
525 empathy_mic_monitor_get_current_mic_cb, result);
529 empathy_mic_monitor_get_current_mic_async (EmpathyMicMonitor *self,
530 guint source_output_idx,
531 GAsyncReadyCallback callback,
534 EmpathyMicMonitorPrivate *priv = self->priv;
535 Operation *operation;
536 GSimpleAsyncResult *simple;
538 simple = g_simple_async_result_new (G_OBJECT (self), callback, user_data,
539 empathy_mic_monitor_get_current_mic_async);
541 g_simple_async_result_set_op_res_gpointer (simple,
542 GUINT_TO_POINTER (source_output_idx), NULL);
544 operation = operation_new (operation_get_current_mic, simple);
545 g_queue_push_tail (priv->operations, operation);
547 operations_run (self);
551 empathy_mic_monitor_get_current_mic_finish (EmpathyMicMonitor *self,
552 GAsyncResult *result,
555 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
557 if (g_simple_async_result_propagate_error (simple, error))
558 return PA_INVALID_INDEX;
560 g_return_val_if_fail (g_simple_async_result_is_valid (result,
561 G_OBJECT (self), empathy_mic_monitor_get_current_mic_async),
564 return GPOINTER_TO_UINT (
565 g_simple_async_result_get_op_res_gpointer (simple));
568 /* operation: get default */
570 empathy_mic_monitor_get_default_cb (pa_context *context,
571 const pa_server_info *info,
574 GSimpleAsyncResult *result = userdata;
576 /* TODO: it would be nice in future, for consistency, if this gave
577 * the source idx instead of the name. */
578 g_simple_async_result_set_op_res_gpointer (result,
579 g_strdup (info->default_source_name), g_free);
580 g_simple_async_result_complete (result);
581 g_object_unref (result);
585 operation_get_default (EmpathyMicMonitor *self,
586 GSimpleAsyncResult *result)
588 EmpathyMicMonitorPrivate *priv = self->priv;
590 g_assert_cmpuint (pa_context_get_state (priv->context), ==, PA_CONTEXT_READY);
592 /* unset this so we can use it in the cb */
593 g_simple_async_result_set_op_res_gpointer (result, NULL, NULL);
595 pa_context_get_server_info (priv->context, empathy_mic_monitor_get_default_cb,
600 empathy_mic_monitor_get_default_async (EmpathyMicMonitor *self,
601 GAsyncReadyCallback callback,
604 EmpathyMicMonitorPrivate *priv = self->priv;
605 Operation *operation;
606 GSimpleAsyncResult *simple;
608 simple = g_simple_async_result_new (G_OBJECT (self), callback, user_data,
609 empathy_mic_monitor_get_default_async);
611 operation = operation_new (operation_get_default, simple);
612 g_queue_push_tail (priv->operations, operation);
614 operations_run (self);
618 empathy_mic_monitor_get_default_finish (EmpathyMicMonitor *self,
619 GAsyncResult *result,
622 empathy_implement_finish_return_pointer (self,
623 empathy_mic_monitor_get_default_async);
626 /* operation: set default */
628 empathy_mic_monitor_set_default_cb (pa_context *c,
632 GSimpleAsyncResult *result = userdata;
636 g_simple_async_result_set_error (result,
637 G_IO_ERROR, G_IO_ERROR_FAILED,
638 "The operation failed for an unknown reason");
641 g_simple_async_result_complete (result);
642 g_object_unref (result);
646 operation_set_default (EmpathyMicMonitor *self,
647 GSimpleAsyncResult *result)
649 EmpathyMicMonitorPrivate *priv = self->priv;
652 g_assert_cmpuint (pa_context_get_state (priv->context), ==, PA_CONTEXT_READY);
654 name = g_simple_async_result_get_op_res_gpointer (result);
656 pa_context_set_default_source (priv->context, name,
657 empathy_mic_monitor_set_default_cb, result);
661 empathy_mic_monitor_set_default_async (EmpathyMicMonitor *self,
663 GAsyncReadyCallback callback,
666 EmpathyMicMonitorPrivate *priv = self->priv;
667 Operation *operation;
668 GSimpleAsyncResult *simple;
670 simple = g_simple_async_result_new (G_OBJECT (self), callback, user_data,
671 empathy_mic_monitor_set_default_async);
673 g_simple_async_result_set_op_res_gpointer (simple, g_strdup (name), g_free);
675 operation = operation_new (operation_set_default, simple);
676 g_queue_push_tail (priv->operations, operation);
678 operations_run (self);
682 empathy_mic_monitor_set_default_finish (EmpathyMicMonitor *self,
683 GAsyncResult *result,
686 empathy_implement_finish_void (self,
687 empathy_mic_monitor_set_default_async);