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