]> git.0d.be Git - empathy.git/blob - src/empathy-mic-monitor.c
mic-monitor: add set_default function
[empathy.git] / src / empathy-mic-monitor.c
1 /*
2  * Copyright (C) 2011 Collabora Ltd.
3  *
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.
8  *
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.
13  *
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
17  *
18  */
19
20 #include <config.h>
21
22 #include <gtk/gtk.h>
23
24 #include <pulse/pulseaudio.h>
25 #include <pulse/glib-mainloop.h>
26
27 #include "empathy-mic-monitor.h"
28
29 #include "src-marshal.h"
30
31 #include <libempathy/empathy-utils.h>
32
33 #define DEBUG_FLAG EMPATHY_DEBUG_VOIP
34 #include <libempathy/empathy-debug.h>
35
36 enum
37 {
38   MICROPHONE_ADDED,
39   MICROPHONE_REMOVED,
40   MICROPHONE_CHANGED,
41   LAST_SIGNAL
42 };
43
44 static guint signals[LAST_SIGNAL] = {0};
45
46 struct _EmpathyMicMonitorPrivate
47 {
48   pa_glib_mainloop *loop;
49   pa_context *context;
50   GQueue *operations;
51 };
52
53 G_DEFINE_TYPE (EmpathyMicMonitor, empathy_mic_monitor, G_TYPE_OBJECT);
54
55 typedef void (*OperationFunc) (EmpathyMicMonitor *, GSimpleAsyncResult *);
56
57 typedef struct
58 {
59   OperationFunc func;
60   GSimpleAsyncResult *result;
61 } Operation;
62
63 static Operation *
64 operation_new (OperationFunc func,
65     GSimpleAsyncResult *result)
66 {
67   Operation *o = g_slice_new0 (Operation);
68
69   o->func = func;
70   o->result = result;
71
72   return o;
73 }
74
75 static void
76 operation_free (Operation *o,
77     gboolean cancelled)
78 {
79   if (cancelled)
80     {
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);
86     }
87
88   g_slice_free (Operation, o);
89 }
90
91 static void
92 operations_run (EmpathyMicMonitor *self)
93 {
94   EmpathyMicMonitorPrivate *priv = self->priv;
95   pa_context_state_t state = pa_context_get_state (priv->context);
96   GList *l;
97
98   if (state != PA_CONTEXT_READY)
99     return;
100
101   for (l = priv->operations->head; l != NULL; l = l->next)
102     {
103       Operation *o = l->data;
104
105       o->func (self, o->result);
106
107       operation_free (o, FALSE);
108     }
109
110   g_queue_clear (priv->operations);
111 }
112
113 static void
114 empathy_mic_monitor_source_output_info_cb (pa_context *context,
115     const pa_source_output_info *info,
116     int eol,
117     void *userdata)
118 {
119   EmpathyMicMonitor *self = userdata;
120
121   if (eol)
122     return;
123
124   g_signal_emit (self, signals[MICROPHONE_CHANGED], 0,
125       info->index, info->source);
126 }
127
128 static void
129 empathy_mic_monitor_source_info_cb (pa_context *context,
130     const pa_source_info *info,
131     int eol,
132     void *userdata)
133 {
134   EmpathyMicMonitor *self = userdata;
135   gboolean is_monitor;
136
137   if (eol)
138     return;
139
140   is_monitor = (info->monitor_of_sink != PA_INVALID_INDEX);
141
142   g_signal_emit (self, signals[MICROPHONE_ADDED], 0,
143       info->index, info->name, info->description, is_monitor);
144 }
145
146 static void
147 empathy_mic_monitor_pa_event_cb (pa_context *context,
148     pa_subscription_event_type_t type,
149     uint32_t idx,
150     void *userdata)
151 {
152   EmpathyMicMonitor *self = userdata;
153
154   if ((type & PA_SUBSCRIPTION_EVENT_FACILITY_MASK) == PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT
155       && (type & PA_SUBSCRIPTION_EVENT_TYPE_MASK) == PA_SUBSCRIPTION_EVENT_CHANGE)
156     {
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);
160     }
161   else if ((type & PA_SUBSCRIPTION_EVENT_FACILITY_MASK) == PA_SUBSCRIPTION_EVENT_SOURCE
162       && (type & PA_SUBSCRIPTION_EVENT_TYPE_MASK) == PA_SUBSCRIPTION_EVENT_REMOVE)
163     {
164       /* A mic has been removed */
165       g_signal_emit (self, signals[MICROPHONE_REMOVED], 0, idx);
166     }
167   else if ((type & PA_SUBSCRIPTION_EVENT_FACILITY_MASK) == PA_SUBSCRIPTION_EVENT_SOURCE
168       && (type & PA_SUBSCRIPTION_EVENT_TYPE_MASK) == PA_SUBSCRIPTION_EVENT_NEW)
169     {
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);
173     }
174 }
175
176 static void
177 empathy_mic_monitor_pa_subscribe_cb (pa_context *context,
178     int success,
179     void *userdata)
180 {
181   if (!success)
182     DEBUG ("Failed to subscribe to PulseAudio events");
183 }
184
185 static void
186 empathy_mic_monitor_pa_state_change_cb (pa_context *context,
187     void *userdata)
188 {
189   EmpathyMicMonitor *self = userdata;
190   EmpathyMicMonitorPrivate *priv = self->priv;
191   pa_context_state_t state = pa_context_get_state (priv->context);
192
193   if (state == PA_CONTEXT_READY)
194     {
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);
202
203       operations_run (self);
204     }
205 }
206
207 static void
208 empathy_mic_monitor_init (EmpathyMicMonitor *self)
209 {
210   EmpathyMicMonitorPrivate *priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
211     EMPATHY_TYPE_MIC_MONITOR, EmpathyMicMonitorPrivate);
212
213   self->priv = priv;
214 }
215
216 static void
217 empathy_mic_monitor_constructed (GObject *obj)
218 {
219   EmpathyMicMonitor *self = EMPATHY_MIC_MONITOR (obj);
220   EmpathyMicMonitorPrivate *priv = self->priv;
221
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");
227
228   /* Finally listen for state changes so we know when we've
229    * connected. */
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);
233
234   priv->operations = g_queue_new ();
235 }
236
237 static void
238 empathy_mic_monitor_dispose (GObject *obj)
239 {
240   EmpathyMicMonitor *self = EMPATHY_MIC_MONITOR (obj);
241   EmpathyMicMonitorPrivate *priv = self->priv;
242
243   g_queue_foreach (priv->operations, (GFunc) operation_free,
244       GUINT_TO_POINTER (TRUE));
245   g_queue_free (priv->operations);
246
247   if (priv->context != NULL)
248     pa_context_unref (priv->context);
249   priv->context = NULL;
250
251   if (priv->loop != NULL)
252     pa_glib_mainloop_free (priv->loop);
253   priv->loop = NULL;
254
255   G_OBJECT_CLASS (empathy_mic_monitor_parent_class)->dispose (obj);
256 }
257
258 static void
259 empathy_mic_monitor_class_init (EmpathyMicMonitorClass *klass)
260 {
261   GObjectClass *object_class = G_OBJECT_CLASS (klass);
262
263   object_class->constructed = empathy_mic_monitor_constructed;
264   object_class->dispose = empathy_mic_monitor_dispose;
265
266   signals[MICROPHONE_ADDED] = g_signal_new ("microphone-added",
267     G_TYPE_FROM_CLASS (klass),
268     G_SIGNAL_RUN_LAST,
269     0,
270     NULL, NULL,
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);
273
274   signals[MICROPHONE_REMOVED] = g_signal_new ("microphone-removed",
275     G_TYPE_FROM_CLASS (klass),
276     G_SIGNAL_RUN_LAST,
277     0,
278     NULL, NULL,
279     g_cclosure_marshal_VOID__UINT,
280     G_TYPE_NONE, 1, G_TYPE_UINT);
281
282   signals[MICROPHONE_CHANGED] = g_signal_new ("microphone-changed",
283     G_TYPE_FROM_CLASS (klass),
284     G_SIGNAL_RUN_LAST,
285     0,
286     NULL, NULL,
287     _src_marshal_VOID__UINT_UINT,
288     G_TYPE_NONE, 2, G_TYPE_UINT, G_TYPE_UINT);
289
290   g_type_class_add_private (object_class, sizeof (EmpathyMicMonitorPrivate));
291 }
292
293 EmpathyMicMonitor *
294 empathy_mic_monitor_new (void)
295 {
296   return g_object_new (EMPATHY_TYPE_MIC_MONITOR,
297       NULL);
298 }
299
300 /* operation: list microphones */
301 static void
302 operation_list_microphones_free (gpointer data)
303 {
304   GQueue *queue = data;
305   GList *l;
306
307   for (l = queue->head; l != NULL; l = l->next)
308     {
309       EmpathyMicrophone *mic = l->data;
310
311       g_free (mic->name);
312       g_free (mic->description);
313       g_slice_free (EmpathyMicrophone, mic);
314     }
315
316   g_queue_free (queue);
317 }
318
319 static void
320 operation_list_microphones_cb (pa_context *context,
321     const pa_source_info *info,
322     int eol,
323     void *userdata)
324 {
325   GSimpleAsyncResult *result = userdata;
326   EmpathyMicrophone *mic;
327   GQueue *queue;
328
329   if (eol)
330     {
331       g_simple_async_result_complete (result);
332       g_object_unref (result);
333       return;
334     }
335
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);
341
342   /* add it to the queue */
343   queue = g_simple_async_result_get_op_res_gpointer (result);
344   g_queue_push_tail (queue, mic);
345 }
346
347 static void
348 operation_list_microphones (EmpathyMicMonitor *self,
349     GSimpleAsyncResult *result)
350 {
351   EmpathyMicMonitorPrivate *priv = self->priv;
352
353   g_assert_cmpuint (pa_context_get_state (priv->context), ==, PA_CONTEXT_READY);
354
355   g_simple_async_result_set_op_res_gpointer (result, g_queue_new (),
356       operation_list_microphones_free);
357
358   pa_context_get_source_info_list (priv->context,
359       operation_list_microphones_cb, result);
360 }
361
362 void
363 empathy_mic_monitor_list_microphones_async (EmpathyMicMonitor *self,
364     GAsyncReadyCallback callback,
365     gpointer user_data)
366 {
367 EmpathyMicMonitorPrivate *priv = self->priv;
368   Operation *operation;
369   GSimpleAsyncResult *simple;
370
371   simple = g_simple_async_result_new (G_OBJECT (self), callback, user_data,
372       empathy_mic_monitor_list_microphones_async);
373
374   operation = operation_new (operation_list_microphones, simple);
375   g_queue_push_tail (priv->operations, operation);
376
377   /* gogogogo */
378   operations_run (self);
379 }
380
381 const GList *
382 empathy_mic_monitor_list_microphones_finish (EmpathyMicMonitor *src,
383     GAsyncResult *result,
384     GError **error)
385 {
386   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
387   GQueue *queue;
388
389   if (g_simple_async_result_propagate_error (simple, error))
390       return NULL;
391
392   g_return_val_if_fail (g_simple_async_result_is_valid (result,
393           G_OBJECT (src), empathy_mic_monitor_list_microphones_async),
394       NULL);
395
396   queue = g_simple_async_result_get_op_res_gpointer (simple);
397   return queue->head;
398 }
399
400 /* operation: change microphone */
401 typedef struct
402 {
403   guint source_output_idx;
404   guint source_idx;
405 } ChangeMicrophoneData;
406
407 static void
408 operation_change_microphone_cb (pa_context *context,
409     int success,
410     void *userdata)
411 {
412   GSimpleAsyncResult *result = userdata;
413
414   if (!success)
415     {
416       g_simple_async_result_set_error (result, G_IO_ERROR, G_IO_ERROR_FAILED,
417           "Failed to change microphone. Reason unknown.");
418     }
419
420   g_simple_async_result_complete (result);
421   g_object_unref (result);
422 }
423
424 static void
425 operation_change_microphone (EmpathyMicMonitor *self,
426     GSimpleAsyncResult *result)
427 {
428   EmpathyMicMonitorPrivate *priv = self->priv;
429   ChangeMicrophoneData *data;
430
431   g_assert_cmpuint (pa_context_get_state (priv->context), ==, PA_CONTEXT_READY);
432
433   data = g_simple_async_result_get_op_res_gpointer (result);
434
435   pa_context_move_source_output_by_index (priv->context,
436       data->source_output_idx, data->source_idx,
437       operation_change_microphone_cb, result);
438
439   g_simple_async_result_set_op_res_gpointer (result, NULL, NULL);
440   g_slice_free (ChangeMicrophoneData, data);
441 }
442
443 void
444 empathy_mic_monitor_change_microphone_async (EmpathyMicMonitor *self,
445     guint source_output_idx,
446     guint source_idx,
447     GAsyncReadyCallback callback,
448     gpointer user_data)
449 {
450   EmpathyMicMonitorPrivate *priv = self->priv;
451   GSimpleAsyncResult *simple;
452   Operation *operation;
453   ChangeMicrophoneData *data;
454
455   simple = g_simple_async_result_new (G_OBJECT (self), callback, user_data,
456       empathy_mic_monitor_change_microphone_async);
457
458   if (source_output_idx == PA_INVALID_INDEX)
459     {
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);
464       return;
465     }
466
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);
471
472   operation = operation_new (operation_change_microphone, simple);
473   g_queue_push_tail (priv->operations, operation);
474
475   /* gogogogo */
476   operations_run (self);
477 }
478
479 gboolean
480 empathy_mic_monitor_change_microphone_finish (EmpathyMicMonitor *self,
481     GAsyncResult *result,
482     GError **error)
483 {
484   empathy_implement_finish_void (self,
485       empathy_mic_monitor_change_microphone_async);
486 }
487
488 /* operation: get current mic */
489 static void
490 empathy_mic_monitor_get_current_mic_cb (pa_context *context,
491     const pa_source_output_info *info,
492     int eol,
493     void *userdata)
494 {
495   GSimpleAsyncResult *result = userdata;
496
497   if (eol)
498     return;
499
500   if (g_simple_async_result_get_op_res_gpointer (result) != NULL)
501     return;
502
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);
507 }
508
509 static void
510 operation_get_current_mic (EmpathyMicMonitor *self,
511     GSimpleAsyncResult *result)
512 {
513   EmpathyMicMonitorPrivate *priv = self->priv;
514   guint source_output_idx;
515
516   g_assert_cmpuint (pa_context_get_state (priv->context), ==, PA_CONTEXT_READY);
517
518   source_output_idx = GPOINTER_TO_UINT (
519       g_simple_async_result_get_op_res_gpointer (result));
520
521   /* unset this so we can use it in the cb */
522   g_simple_async_result_set_op_res_gpointer (result, NULL, NULL);
523
524   pa_context_get_source_output_info (priv->context, source_output_idx,
525       empathy_mic_monitor_get_current_mic_cb, result);
526 }
527
528 void
529 empathy_mic_monitor_get_current_mic_async (EmpathyMicMonitor *self,
530     guint source_output_idx,
531     GAsyncReadyCallback callback,
532     gpointer user_data)
533 {
534   EmpathyMicMonitorPrivate *priv = self->priv;
535   Operation *operation;
536   GSimpleAsyncResult *simple;
537
538   simple = g_simple_async_result_new (G_OBJECT (self), callback, user_data,
539       empathy_mic_monitor_get_current_mic_async);
540
541   g_simple_async_result_set_op_res_gpointer (simple,
542       GUINT_TO_POINTER (source_output_idx), NULL);
543
544   operation = operation_new (operation_get_current_mic, simple);
545   g_queue_push_tail (priv->operations, operation);
546
547   operations_run (self);
548 }
549
550 guint
551 empathy_mic_monitor_get_current_mic_finish (EmpathyMicMonitor *self,
552     GAsyncResult *result,
553     GError **error)
554 {
555   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
556
557   if (g_simple_async_result_propagate_error (simple, error))
558     return PA_INVALID_INDEX;
559
560   g_return_val_if_fail (g_simple_async_result_is_valid (result,
561           G_OBJECT (self), empathy_mic_monitor_get_current_mic_async),
562       PA_INVALID_INDEX);
563
564   return GPOINTER_TO_UINT (
565       g_simple_async_result_get_op_res_gpointer (simple));
566 }
567
568 /* operation: get default */
569 static void
570 empathy_mic_monitor_get_default_cb (pa_context *context,
571     const pa_server_info *info,
572     void *userdata)
573 {
574   GSimpleAsyncResult *result = userdata;
575
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);
582 }
583
584 static void
585 operation_get_default (EmpathyMicMonitor *self,
586     GSimpleAsyncResult *result)
587 {
588   EmpathyMicMonitorPrivate *priv = self->priv;
589
590   g_assert_cmpuint (pa_context_get_state (priv->context), ==, PA_CONTEXT_READY);
591
592   /* unset this so we can use it in the cb */
593   g_simple_async_result_set_op_res_gpointer (result, NULL, NULL);
594
595   pa_context_get_server_info (priv->context, empathy_mic_monitor_get_default_cb,
596       result);
597 }
598
599 void
600 empathy_mic_monitor_get_default_async (EmpathyMicMonitor *self,
601     GAsyncReadyCallback callback,
602     gpointer user_data)
603 {
604   EmpathyMicMonitorPrivate *priv = self->priv;
605   Operation *operation;
606   GSimpleAsyncResult *simple;
607
608   simple = g_simple_async_result_new (G_OBJECT (self), callback, user_data,
609       empathy_mic_monitor_get_default_async);
610
611   operation = operation_new (operation_get_default, simple);
612   g_queue_push_tail (priv->operations, operation);
613
614   operations_run (self);
615 }
616
617 const gchar *
618 empathy_mic_monitor_get_default_finish (EmpathyMicMonitor *self,
619     GAsyncResult *result,
620     GError **error)
621 {
622   empathy_implement_finish_return_pointer (self,
623       empathy_mic_monitor_get_default_async);
624 }
625
626 /* operation: set default */
627 static void
628 empathy_mic_monitor_set_default_cb (pa_context *c,
629     int success,
630     void *userdata)
631 {
632   GSimpleAsyncResult *result = userdata;
633
634   if (!success)
635     {
636       g_simple_async_result_set_error (result,
637           G_IO_ERROR, G_IO_ERROR_FAILED,
638           "The operation failed for an unknown reason");
639     }
640
641   g_simple_async_result_complete (result);
642   g_object_unref (result);
643 }
644
645 static void
646 operation_set_default (EmpathyMicMonitor *self,
647     GSimpleAsyncResult *result)
648 {
649   EmpathyMicMonitorPrivate *priv = self->priv;
650   gchar *name;
651
652   g_assert_cmpuint (pa_context_get_state (priv->context), ==, PA_CONTEXT_READY);
653
654   name = g_simple_async_result_get_op_res_gpointer (result);
655
656   pa_context_set_default_source (priv->context, name,
657       empathy_mic_monitor_set_default_cb, result);
658 }
659
660 void
661 empathy_mic_monitor_set_default_async (EmpathyMicMonitor *self,
662     const gchar *name,
663     GAsyncReadyCallback callback,
664     gpointer user_data)
665 {
666   EmpathyMicMonitorPrivate *priv = self->priv;
667   Operation *operation;
668   GSimpleAsyncResult *simple;
669
670   simple = g_simple_async_result_new (G_OBJECT (self), callback, user_data,
671       empathy_mic_monitor_set_default_async);
672
673   g_simple_async_result_set_op_res_gpointer (simple, g_strdup (name), g_free);
674
675   operation = operation_new (operation_set_default, simple);
676   g_queue_push_tail (priv->operations, operation);
677
678   operations_run (self);
679 }
680
681 gboolean
682 empathy_mic_monitor_set_default_finish (EmpathyMicMonitor *self,
683     GAsyncResult *result,
684     GError **error)
685 {
686   empathy_implement_finish_void (self,
687       empathy_mic_monitor_set_default_async);
688 }