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