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