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
21 #include "empathy-mic-monitor.h"
23 #include <pulse/glib-mainloop.h>
25 #include "empathy-utils.h"
27 #define DEBUG_FLAG EMPATHY_DEBUG_VOIP
28 #include "empathy-debug.h"
38 static guint signals[LAST_SIGNAL] = {0};
40 struct _EmpathyMicMonitorPrivate
42 pa_glib_mainloop *loop;
47 G_DEFINE_TYPE (EmpathyMicMonitor, empathy_mic_monitor, G_TYPE_OBJECT);
49 typedef void (*OperationFunc) (EmpathyMicMonitor *, GSimpleAsyncResult *);
54 GSimpleAsyncResult *result;
58 operation_new (OperationFunc func,
59 GSimpleAsyncResult *result)
61 Operation *o = g_slice_new0 (Operation);
70 operation_free (Operation *o,
75 g_simple_async_result_set_error (o->result,
76 G_IO_ERROR, G_IO_ERROR_CANCELLED,
77 "The microphone monitor was disposed");
78 g_simple_async_result_complete (o->result);
79 g_object_unref (o->result);
82 g_slice_free (Operation, o);
86 operations_run (EmpathyMicMonitor *self)
88 EmpathyMicMonitorPrivate *priv = self->priv;
89 pa_context_state_t state = pa_context_get_state (priv->context);
92 if (state != PA_CONTEXT_READY)
95 for (l = priv->operations->head; l != NULL; l = l->next)
97 Operation *o = l->data;
99 o->func (self, o->result);
101 operation_free (o, FALSE);
104 g_queue_clear (priv->operations);
108 empathy_mic_monitor_source_output_info_cb (pa_context *context,
109 const pa_source_output_info *info,
113 EmpathyMicMonitor *self = userdata;
118 g_signal_emit (self, signals[MICROPHONE_CHANGED], 0,
119 info->index, info->source);
123 empathy_mic_monitor_source_info_cb (pa_context *context,
124 const pa_source_info *info,
128 EmpathyMicMonitor *self = userdata;
134 is_monitor = (info->monitor_of_sink != PA_INVALID_INDEX);
136 g_signal_emit (self, signals[MICROPHONE_ADDED], 0,
137 info->index, info->name, info->description, is_monitor);
141 empathy_mic_monitor_pa_event_cb (pa_context *context,
142 pa_subscription_event_type_t type,
146 EmpathyMicMonitor *self = userdata;
148 if ((type & PA_SUBSCRIPTION_EVENT_FACILITY_MASK) == PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT
149 && (type & PA_SUBSCRIPTION_EVENT_TYPE_MASK) == PA_SUBSCRIPTION_EVENT_CHANGE)
151 /* Microphone in the source output has changed */
152 pa_context_get_source_output_info (context, idx,
153 empathy_mic_monitor_source_output_info_cb, self);
155 else if ((type & PA_SUBSCRIPTION_EVENT_FACILITY_MASK) == PA_SUBSCRIPTION_EVENT_SOURCE
156 && (type & PA_SUBSCRIPTION_EVENT_TYPE_MASK) == PA_SUBSCRIPTION_EVENT_REMOVE)
158 /* A mic has been removed */
159 g_signal_emit (self, signals[MICROPHONE_REMOVED], 0, idx);
161 else if ((type & PA_SUBSCRIPTION_EVENT_FACILITY_MASK) == PA_SUBSCRIPTION_EVENT_SOURCE
162 && (type & PA_SUBSCRIPTION_EVENT_TYPE_MASK) == PA_SUBSCRIPTION_EVENT_NEW)
164 /* A mic has been plugged in */
165 pa_context_get_source_info_by_index (context, idx,
166 empathy_mic_monitor_source_info_cb, self);
171 empathy_mic_monitor_pa_subscribe_cb (pa_context *context,
176 DEBUG ("Failed to subscribe to PulseAudio events");
180 empathy_mic_monitor_pa_state_change_cb (pa_context *context,
183 EmpathyMicMonitor *self = userdata;
184 EmpathyMicMonitorPrivate *priv = self->priv;
185 pa_context_state_t state = pa_context_get_state (priv->context);
187 if (state == PA_CONTEXT_READY)
189 /* Listen to pulseaudio events so we know when sources are
190 * added and when the microphone is changed. */
191 pa_context_set_subscribe_callback (priv->context,
192 empathy_mic_monitor_pa_event_cb, self);
193 pa_context_subscribe (priv->context,
194 PA_SUBSCRIPTION_MASK_SOURCE | PA_SUBSCRIPTION_MASK_SOURCE_OUTPUT,
195 empathy_mic_monitor_pa_subscribe_cb, NULL);
197 operations_run (self);
202 empathy_mic_monitor_init (EmpathyMicMonitor *self)
204 EmpathyMicMonitorPrivate *priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
205 EMPATHY_TYPE_MIC_MONITOR, EmpathyMicMonitorPrivate);
211 empathy_mic_monitor_constructed (GObject *obj)
213 EmpathyMicMonitor *self = EMPATHY_MIC_MONITOR (obj);
214 EmpathyMicMonitorPrivate *priv = self->priv;
216 /* PulseAudio stuff: We need to create a dummy pa_glib_mainloop* so
217 * Pulse can use the mainloop that GTK has created for us. */
218 priv->loop = pa_glib_mainloop_new (NULL);
219 priv->context = pa_context_new (pa_glib_mainloop_get_api (priv->loop),
220 "EmpathyMicMonitor");
222 /* Finally listen for state changes so we know when we've
224 pa_context_set_state_callback (priv->context,
225 empathy_mic_monitor_pa_state_change_cb, obj);
226 pa_context_connect (priv->context, NULL, 0, NULL);
228 priv->operations = g_queue_new ();
232 empathy_mic_monitor_dispose (GObject *obj)
234 EmpathyMicMonitor *self = EMPATHY_MIC_MONITOR (obj);
235 EmpathyMicMonitorPrivate *priv = self->priv;
237 g_queue_foreach (priv->operations, (GFunc) operation_free,
238 GUINT_TO_POINTER (TRUE));
239 g_queue_free (priv->operations);
241 if (priv->context != NULL)
242 pa_context_unref (priv->context);
243 priv->context = NULL;
245 if (priv->loop != NULL)
246 pa_glib_mainloop_free (priv->loop);
249 G_OBJECT_CLASS (empathy_mic_monitor_parent_class)->dispose (obj);
253 empathy_mic_monitor_class_init (EmpathyMicMonitorClass *klass)
255 GObjectClass *object_class = G_OBJECT_CLASS (klass);
257 object_class->constructed = empathy_mic_monitor_constructed;
258 object_class->dispose = empathy_mic_monitor_dispose;
260 signals[MICROPHONE_ADDED] = g_signal_new ("microphone-added",
261 G_TYPE_FROM_CLASS (klass),
265 g_cclosure_marshal_generic,
266 G_TYPE_NONE, 4, G_TYPE_UINT, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_BOOLEAN);
268 signals[MICROPHONE_REMOVED] = g_signal_new ("microphone-removed",
269 G_TYPE_FROM_CLASS (klass),
273 g_cclosure_marshal_generic,
274 G_TYPE_NONE, 1, G_TYPE_UINT);
276 signals[MICROPHONE_CHANGED] = g_signal_new ("microphone-changed",
277 G_TYPE_FROM_CLASS (klass),
281 g_cclosure_marshal_generic,
282 G_TYPE_NONE, 2, G_TYPE_UINT, G_TYPE_UINT);
284 g_type_class_add_private (object_class, sizeof (EmpathyMicMonitorPrivate));
288 empathy_mic_monitor_new (void)
290 return g_object_new (EMPATHY_TYPE_MIC_MONITOR,
294 /* operation: list microphones */
296 operation_list_microphones_free (gpointer data)
298 GQueue *queue = data;
301 for (l = queue->head; l != NULL; l = l->next)
303 EmpathyMicrophone *mic = l->data;
306 g_free (mic->description);
307 g_slice_free (EmpathyMicrophone, mic);
310 g_queue_free (queue);
314 operation_list_microphones_cb (pa_context *context,
315 const pa_source_info *info,
319 GSimpleAsyncResult *result = userdata;
320 EmpathyMicrophone *mic;
325 g_simple_async_result_complete (result);
326 g_object_unref (result);
330 mic = g_slice_new0 (EmpathyMicrophone);
331 mic->index = info->index;
332 mic->name = g_strdup (info->name);
333 mic->description = g_strdup (info->description);
334 mic->is_monitor = (info->monitor_of_sink != PA_INVALID_INDEX);
336 /* add it to the queue */
337 queue = g_simple_async_result_get_op_res_gpointer (result);
338 g_queue_push_tail (queue, mic);
342 operation_list_microphones (EmpathyMicMonitor *self,
343 GSimpleAsyncResult *result)
345 EmpathyMicMonitorPrivate *priv = self->priv;
347 g_assert_cmpuint (pa_context_get_state (priv->context), ==, PA_CONTEXT_READY);
349 g_simple_async_result_set_op_res_gpointer (result, g_queue_new (),
350 operation_list_microphones_free);
352 pa_context_get_source_info_list (priv->context,
353 operation_list_microphones_cb, result);
357 empathy_mic_monitor_list_microphones_async (EmpathyMicMonitor *self,
358 GAsyncReadyCallback callback,
361 EmpathyMicMonitorPrivate *priv = self->priv;
362 Operation *operation;
363 GSimpleAsyncResult *simple;
365 simple = g_simple_async_result_new (G_OBJECT (self), callback, user_data,
366 empathy_mic_monitor_list_microphones_async);
368 operation = operation_new (operation_list_microphones, simple);
369 g_queue_push_tail (priv->operations, operation);
372 operations_run (self);
376 empathy_mic_monitor_list_microphones_finish (EmpathyMicMonitor *src,
377 GAsyncResult *result,
380 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
383 if (g_simple_async_result_propagate_error (simple, error))
386 g_return_val_if_fail (g_simple_async_result_is_valid (result,
387 G_OBJECT (src), empathy_mic_monitor_list_microphones_async),
390 queue = g_simple_async_result_get_op_res_gpointer (simple);
394 /* operation: change microphone */
397 guint source_output_idx;
399 } ChangeMicrophoneData;
402 operation_change_microphone_cb (pa_context *context,
406 GSimpleAsyncResult *result = userdata;
410 g_simple_async_result_set_error (result, G_IO_ERROR, G_IO_ERROR_FAILED,
411 "Failed to change microphone. Reason unknown.");
414 g_simple_async_result_complete (result);
415 g_object_unref (result);
419 operation_change_microphone (EmpathyMicMonitor *self,
420 GSimpleAsyncResult *result)
422 EmpathyMicMonitorPrivate *priv = self->priv;
423 ChangeMicrophoneData *data;
425 g_assert_cmpuint (pa_context_get_state (priv->context), ==, PA_CONTEXT_READY);
427 data = g_simple_async_result_get_op_res_gpointer (result);
429 pa_context_move_source_output_by_index (priv->context,
430 data->source_output_idx, data->source_idx,
431 operation_change_microphone_cb, result);
433 g_simple_async_result_set_op_res_gpointer (result, NULL, NULL);
434 g_slice_free (ChangeMicrophoneData, data);
438 empathy_mic_monitor_change_microphone_async (EmpathyMicMonitor *self,
439 guint source_output_idx,
441 GAsyncReadyCallback callback,
444 EmpathyMicMonitorPrivate *priv = self->priv;
445 GSimpleAsyncResult *simple;
446 Operation *operation;
447 ChangeMicrophoneData *data;
449 simple = g_simple_async_result_new (G_OBJECT (self), callback, user_data,
450 empathy_mic_monitor_change_microphone_async);
452 if (source_output_idx == PA_INVALID_INDEX)
454 g_simple_async_result_set_error (simple, G_IO_ERROR, G_IO_ERROR_FAILED,
455 "Invalid source output index");
456 g_simple_async_result_complete_in_idle (simple);
457 g_object_unref (simple);
461 data = g_slice_new0 (ChangeMicrophoneData);
462 data->source_idx = source_idx;
463 data->source_output_idx = source_output_idx;
464 g_simple_async_result_set_op_res_gpointer (simple, data, NULL);
466 operation = operation_new (operation_change_microphone, simple);
467 g_queue_push_tail (priv->operations, operation);
470 operations_run (self);
474 empathy_mic_monitor_change_microphone_finish (EmpathyMicMonitor *self,
475 GAsyncResult *result,
478 empathy_implement_finish_void (self,
479 empathy_mic_monitor_change_microphone_async);
482 /* operation: get current mic */
484 empathy_mic_monitor_get_current_mic_cb (pa_context *context,
485 const pa_source_output_info *info,
489 GSimpleAsyncResult *result = userdata;
494 if (g_simple_async_result_get_op_res_gpointer (result) != NULL)
497 g_simple_async_result_set_op_res_gpointer (result,
498 GUINT_TO_POINTER (info->source), NULL);
499 g_simple_async_result_complete (result);
500 g_object_unref (result);
504 operation_get_current_mic (EmpathyMicMonitor *self,
505 GSimpleAsyncResult *result)
507 EmpathyMicMonitorPrivate *priv = self->priv;
508 guint source_output_idx;
510 g_assert_cmpuint (pa_context_get_state (priv->context), ==, PA_CONTEXT_READY);
512 source_output_idx = GPOINTER_TO_UINT (
513 g_simple_async_result_get_op_res_gpointer (result));
515 /* unset this so we can use it in the cb */
516 g_simple_async_result_set_op_res_gpointer (result, NULL, NULL);
518 pa_context_get_source_output_info (priv->context, source_output_idx,
519 empathy_mic_monitor_get_current_mic_cb, result);
523 empathy_mic_monitor_get_current_mic_async (EmpathyMicMonitor *self,
524 guint source_output_idx,
525 GAsyncReadyCallback callback,
528 EmpathyMicMonitorPrivate *priv = self->priv;
529 Operation *operation;
530 GSimpleAsyncResult *simple;
532 simple = g_simple_async_result_new (G_OBJECT (self), callback, user_data,
533 empathy_mic_monitor_get_current_mic_async);
535 g_simple_async_result_set_op_res_gpointer (simple,
536 GUINT_TO_POINTER (source_output_idx), NULL);
538 operation = operation_new (operation_get_current_mic, simple);
539 g_queue_push_tail (priv->operations, operation);
541 operations_run (self);
545 empathy_mic_monitor_get_current_mic_finish (EmpathyMicMonitor *self,
546 GAsyncResult *result,
549 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
551 if (g_simple_async_result_propagate_error (simple, error))
552 return PA_INVALID_INDEX;
554 g_return_val_if_fail (g_simple_async_result_is_valid (result,
555 G_OBJECT (self), empathy_mic_monitor_get_current_mic_async),
558 return GPOINTER_TO_UINT (
559 g_simple_async_result_get_op_res_gpointer (simple));
562 /* operation: get default */
564 empathy_mic_monitor_get_default_cb (pa_context *context,
565 const pa_server_info *info,
568 GSimpleAsyncResult *result = userdata;
570 /* TODO: it would be nice in future, for consistency, if this gave
571 * the source idx instead of the name. */
572 g_simple_async_result_set_op_res_gpointer (result,
573 g_strdup (info->default_source_name), g_free);
574 g_simple_async_result_complete (result);
575 g_object_unref (result);
579 operation_get_default (EmpathyMicMonitor *self,
580 GSimpleAsyncResult *result)
582 EmpathyMicMonitorPrivate *priv = self->priv;
584 g_assert_cmpuint (pa_context_get_state (priv->context), ==, PA_CONTEXT_READY);
586 /* unset this so we can use it in the cb */
587 g_simple_async_result_set_op_res_gpointer (result, NULL, NULL);
589 pa_context_get_server_info (priv->context, empathy_mic_monitor_get_default_cb,
594 empathy_mic_monitor_get_default_async (EmpathyMicMonitor *self,
595 GAsyncReadyCallback callback,
598 EmpathyMicMonitorPrivate *priv = self->priv;
599 Operation *operation;
600 GSimpleAsyncResult *simple;
602 simple = g_simple_async_result_new (G_OBJECT (self), callback, user_data,
603 empathy_mic_monitor_get_default_async);
605 operation = operation_new (operation_get_default, simple);
606 g_queue_push_tail (priv->operations, operation);
608 operations_run (self);
612 empathy_mic_monitor_get_default_finish (EmpathyMicMonitor *self,
613 GAsyncResult *result,
616 empathy_implement_finish_return_pointer (self,
617 empathy_mic_monitor_get_default_async);
620 /* operation: set default */
622 empathy_mic_monitor_set_default_cb (pa_context *c,
626 GSimpleAsyncResult *result = userdata;
630 g_simple_async_result_set_error (result,
631 G_IO_ERROR, G_IO_ERROR_FAILED,
632 "The operation failed for an unknown reason");
635 g_simple_async_result_complete (result);
636 g_object_unref (result);
640 operation_set_default (EmpathyMicMonitor *self,
641 GSimpleAsyncResult *result)
643 EmpathyMicMonitorPrivate *priv = self->priv;
646 g_assert_cmpuint (pa_context_get_state (priv->context), ==, PA_CONTEXT_READY);
648 name = g_simple_async_result_get_op_res_gpointer (result);
650 pa_context_set_default_source (priv->context, name,
651 empathy_mic_monitor_set_default_cb, result);
655 empathy_mic_monitor_set_default_async (EmpathyMicMonitor *self,
657 GAsyncReadyCallback callback,
660 EmpathyMicMonitorPrivate *priv = self->priv;
661 Operation *operation;
662 GSimpleAsyncResult *simple;
664 simple = g_simple_async_result_new (G_OBJECT (self), callback, user_data,
665 empathy_mic_monitor_set_default_async);
667 g_simple_async_result_set_op_res_gpointer (simple, g_strdup (name), g_free);
669 operation = operation_new (operation_set_default, simple);
670 g_queue_push_tail (priv->operations, operation);
672 operations_run (self);
676 empathy_mic_monitor_set_default_finish (EmpathyMicMonitor *self,
677 GAsyncResult *result,
680 empathy_implement_finish_void (self,
681 empathy_mic_monitor_set_default_async);