Debian Codegraph @

src/cad-pulse.c

raw
1 /*
2 * Copyright (C) 2018, 2019 Purism SPC
3 * Copyright (C) 2020 Arnaud Ferraris <[email protected]>
4 *
5 * SPDX-License-Identifier: GPL-3.0-or-later
6 */
7
8 #define G_LOG_DOMAIN "callaudiod-pulse"
9
10 #include "cad-manager.h"
11 #include "cad-pulse.h"
12
13 #include <glib/gi18n.h>
14 #include <glib-object.h>
15 #include <pulse/pulseaudio.h>
16 #include <pulse/glib-mainloop.h>
17 #include <alsa/use-case.h>
18 #include <appstream.h>
19
20 #include <dlfcn.h>
21 #include <string.h>
22 #include <stdio.h>
23 #include <stdint.h>
24
25 #define APPLICATION_NAME "CallAudio"
26 #define APPLICATION_ID "org.mobian-project.CallAudio"
27
28 #define SINK_CLASS "sound"
29 #define CARD_FORM_FACTOR "internal"
30 #define CARD_MODEM_CLASS "modem"
31 #define CARD_MODEM_NAME "Modem"
32
33 struct _CadPulse
34 {
35 GObject parent_instance;
36
37 GObject *manager;
38
39 pa_glib_mainloop *loop;
40 pa_context *ctx;
41
42 int card_id;
43 int sink_id;
44 int source_id;
45
46 gboolean has_voice_profile;
47 gchar *speaker_port;
48 gchar *earpiece_port;
49
50 GHashTable *sink_ports;
51 GHashTable *source_ports;
52
53 CallAudioMode audio_mode;
54 CallAudioSpeakerState speaker_state;
55 CallAudioMicState mic_state;
56 };
57
58 G_DEFINE_TYPE(CadPulse, cad_pulse, G_TYPE_OBJECT);
59
60 typedef struct _CadPulseOperation {
61 CadPulse *pulse;
62 CadOperation *op;
63 guint value;
64 } CadPulseOperation;
65
66 static void pulseaudio_cleanup(CadPulse *self);
67 static gboolean pulseaudio_connect(CadPulse *self);
68 static gboolean init_pulseaudio_objects(gpointer data);
69
70 /******************************************************************************
71 * Source management
72 *
73 * The following functions take care of monitoring and configuring the default
74 * source (input)
75 ******************************************************************************/
76
77 static const gchar *get_available_source_port(const pa_source_info *source, const gchar *exclude)
78 {
79 pa_source_port_info *available_port = NULL;
80 guint i;
81
82 g_debug("looking for available input excluding '%s'", exclude);
83
84 for (i = 0; i < source->n_ports; i++) {
85 pa_source_port_info *port = source->ports[i];
86
87 if ((exclude && g_strcmp0(port->name, exclude) == 0) ||
88 port->available == PA_PORT_AVAILABLE_NO) {
89 continue;
90 }
91
92 if (!available_port || port->priority > available_port->priority)
93 available_port = port;
94 }
95
96 if (available_port) {
97 g_debug("found available input '%s'", available_port->name);
98 return available_port->name;
99 }
100
101 g_warning("no available input found!");
102
103 return NULL;
104 }
105
106 static void change_source_info(pa_context *ctx, const pa_source_info *info, int eol, void *data)
107 {
108 CadPulse *self = data;
109 const gchar *target_port;
110 pa_operation *op;
111 gboolean change = FALSE;
112 guint i;
113
114 if (eol != 0)
115 return;
116
117 if (!info) {
118 g_critical("PA returned no source info (eol=%d)", eol);
119 return;
120 }
121
122 if (info->index != self->source_id)
123 return;
124
125 for (i = 0; i < info->n_ports; i++) {
126 pa_source_port_info *port = info->ports[i];
127
128 if (port->available != PA_PORT_AVAILABLE_UNKNOWN) {
129 enum pa_port_available available;
130 available = GPOINTER_TO_INT(g_hash_table_lookup(self->source_ports, port->name));
131 if (available != port->available) {
132 g_hash_table_insert(self->source_ports, g_strdup(port->name),
133 GINT_TO_POINTER(port->available));
134 change = TRUE;
135 }
136 }
137 }
138
139 if (change) {
140 target_port = get_available_source_port(info, NULL);
141 if (target_port) {
142 op = pa_context_set_source_port_by_index(ctx, self->source_id,
143 target_port, NULL, NULL);
144 if (op)
145 pa_operation_unref(op);
146 }
147 }
148 }
149
150 static void process_new_source(CadPulse *self, const pa_source_info *info)
151 {
152 const gchar *prop;
153 int i;
154
155 prop = pa_proplist_gets(info->proplist, PA_PROP_DEVICE_CLASS);
156 if (prop && g_strcmp0(prop, SINK_CLASS) != 0)
157 return;
158 if (info->monitor_of_sink != PA_INVALID_INDEX)
159 return;
160 if (info->card != self->card_id || self->source_id != -1)
161 return;
162
163 self->source_id = info->index;
164 if (self->source_ports)
165 g_hash_table_destroy(self->source_ports);
166 self->source_ports = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL);
167
168 for (i = 0; i < info->n_ports; i++) {
169 pa_source_port_info *port = info->ports[i];
170
171 if (port->available != PA_PORT_AVAILABLE_UNKNOWN) {
172 g_hash_table_insert (self->source_ports,
173 g_strdup(port->name),
174 GINT_TO_POINTER(port->available));
175 }
176 }
177
178 g_debug("SOURCE: idx=%u name='%s'", info->index, info->name);
179 }
180
181 static void init_source_info(pa_context *ctx, const pa_source_info *info, int eol, void *data)
182 {
183 CadPulse *self = data;
184 const gchar *target_port;
185 pa_operation *op;
186
187 if (eol != 0)
188 return;
189
190 if (!info) {
191 g_critical("PA returned no source info (eol=%d)", eol);
192 return;
193 }
194
195 process_new_source(self, info);
196 if (self->source_id < 0 || self->source_id != info->index)
197 return;
198
199 op = pa_context_set_default_source(ctx, info->name, NULL, NULL);
200 if (op)
201 pa_operation_unref(op);
202
203 if (self->mic_state == CALL_AUDIO_MIC_UNKNOWN) {
204 if (info->mute)
205 self->mic_state = CALL_AUDIO_MIC_OFF;
206 else
207 self->mic_state = CALL_AUDIO_MIC_ON;
208 g_object_set(self->manager, "mic-state", self->mic_state, NULL);
209 }
210
211 target_port = get_available_source_port(info, NULL);
212 if (target_port) {
213 op = pa_context_set_source_port_by_index(ctx, self->source_id,
214 target_port, NULL, NULL);
215 if (op)
216 pa_operation_unref(op);
217 }
218 }
219
220 /******************************************************************************
221 * Sink management
222 *
223 * The following functions take care of monitoring and configuring the default
224 * sink (output)
225 ******************************************************************************/
226
227 static const gchar *get_available_sink_port(const pa_sink_info *sink, const gchar *exclude)
228 {
229 pa_sink_port_info *available_port = NULL;
230 guint i;
231
232 g_debug("looking for available output excluding '%s'", exclude);
233
234 for (i = 0; i < sink->n_ports; i++) {
235 pa_sink_port_info *port = sink->ports[i];
236
237 if ((exclude && g_strcmp0(port->name, exclude) == 0) ||
238 port->available == PA_PORT_AVAILABLE_NO) {
239 continue;
240 }
241
242 if (!available_port || port->priority > available_port->priority)
243 available_port = port;
244 }
245
246 if (available_port) {
247 g_debug("found available output '%s'", available_port->name);
248 return available_port->name;
249 }
250
251 g_warning("no available output found!");
252
253 return NULL;
254 }
255
256 static void change_sink_info(pa_context *ctx, const pa_sink_info *info, int eol, void *data)
257 {
258 CadPulse *self = data;
259 const gchar *target_port;
260 pa_operation *op;
261 gboolean change = FALSE;
262 guint i;
263
264 if (eol != 0)
265 return;
266
267 if (!info) {
268 g_critical("PA returned no sink info (eol=%d)", eol);
269 return;
270 }
271
272 if (info->index != self->sink_id)
273 return;
274
275 for (i = 0; i < info->n_ports; i++) {
276 pa_sink_port_info *port = info->ports[i];
277
278 if (port->available != PA_PORT_AVAILABLE_UNKNOWN) {
279 enum pa_port_available available;
280 available = GPOINTER_TO_INT(g_hash_table_lookup(self->sink_ports, port->name));
281 if (available != port->available) {
282 g_hash_table_insert(self->sink_ports, g_strdup(port->name),
283 GINT_TO_POINTER(port->available));
284 change = TRUE;
285 }
286 }
287 }
288
289 if (change) {
290 target_port = get_available_sink_port(info, NULL);
291 if (target_port) {
292 op = pa_context_set_sink_port_by_index(ctx, self->sink_id,
293 target_port, NULL, NULL);
294 if (op)
295 pa_operation_unref(op);
296 }
297 }
298 }
299
300 static void process_new_sink(CadPulse *self, const pa_sink_info *info)
301 {
302 const gchar *prop;
303 guint i;
304
305 prop = pa_proplist_gets(info->proplist, PA_PROP_DEVICE_CLASS);
306 if (prop && g_strcmp0(prop, SINK_CLASS) != 0)
307 return;
308 if (info->card != self->card_id || self->sink_id != -1)
309 return;
310
311 self->sink_id = info->index;
312 if (self->sink_ports)
313 g_hash_table_destroy(self->sink_ports);
314 self->sink_ports = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL);
315
316 g_debug("SINK: idx=%u name='%s'", info->index, info->name);
317
318 for (i = 0; i < info->n_ports; i++) {
319 pa_sink_port_info *port = info->ports[i];
320
321 switch (port->type) {
322 case PA_DEVICE_PORT_TYPE_SPEAKER:
323 if (self->speaker_port) {
324 if (g_strcmp0(port->name, self->speaker_port) != 0) {
325 g_free(self->speaker_port);
326 self->speaker_port = g_strdup(port->name);
327 }
328 } else {
329 self->speaker_port = g_strdup(port->name);
330 }
331 break;
332 case PA_DEVICE_PORT_TYPE_EARPIECE:
333 case PA_DEVICE_PORT_TYPE_HANDSET:
334 case PA_DEVICE_PORT_TYPE_HEADPHONES:
335 if (self->earpiece_port) {
336 if (g_strcmp0(port->name, self->earpiece_port) != 0) {
337 g_free(self->earpiece_port);
338 self->earpiece_port = g_strdup(port->name);
339 }
340 } else {
341 self->earpiece_port = g_strdup(port->name);
342 }
343 break;
344 default:
345 break;
346 }
347
348 if (port->available != PA_PORT_AVAILABLE_UNKNOWN) {
349 g_hash_table_insert (self->sink_ports,
350 g_strdup(port->name),
351 GINT_TO_POINTER(port->available));
352 }
353 }
354
355 g_debug("SINK: speaker_port='%s'", self->speaker_port);
356 g_debug("SINK: earpiece_port='%s'", self->earpiece_port);
357 }
358
359 static void init_sink_info(pa_context *ctx, const pa_sink_info *info, int eol, void *data)
360 {
361 CadPulse *self = data;
362 const gchar *target_port;
363 pa_operation *op;
364
365 if (eol != 0)
366 return;
367
368 if (!info) {
369 g_critical("PA returned no sink info (eol=%d)", eol);
370 return;
371 }
372
373 process_new_sink(self, info);
374 if (self->sink_id < 0 || self->sink_id != info->index)
375 return;
376
377 op = pa_context_set_default_sink(ctx, info->name, NULL, NULL);
378 if (op)
379 pa_operation_unref(op);
380
381 if (self->speaker_state == CALL_AUDIO_SPEAKER_UNKNOWN) {
382 self->speaker_state = CALL_AUDIO_SPEAKER_OFF;
383
384 switch (self->audio_mode) {
385 case CALL_AUDIO_MODE_CALL:
386 if (g_strcmp0(info->active_port->name, self->speaker_port) == 0) {
387 self->speaker_state = CALL_AUDIO_SPEAKER_ON;
388 g_object_set(self->manager, "speaker-state", self->speaker_state, NULL);
389 /*
390 * callaudiod likely restarted after being killed during a call
391 * during which the speaker was enabled. End processing here so
392 * we keep the current routing and don't unexpectedly mess with
393 * the call setup.
394 */
395 return;
396 }
397 break;
398 case CALL_AUDIO_MODE_UNKNOWN:
399 /*
400 * Let's see if we can guess the current mode:
401 * - if current port is earpiece, we're likely in call mode
402 * - otherwise we're either in default mode, or call mode with
403 * speaker enabled. Let's settle for the former as both situations
404 * are technically equivalent.
405 *
406 * Note: this code path is only used when the card doesn't have a
407 * voice profile, otherwise things are easier to deal with.
408 */
409 if (g_strcmp0(info->active_port->name, self->earpiece_port) == 0) {
410 self->audio_mode = CALL_AUDIO_MODE_CALL;
411 g_object_set(self->manager, "audio-mode", self->audio_mode, NULL);
412 /*
413 * Don't touch routing as we're likely in the middle of a call,
414 * see above.
415 */
416 return;
417 } else {
418 self->audio_mode = CALL_AUDIO_MODE_DEFAULT;
419 g_object_set(self->manager, "audio-mode", self->audio_mode, NULL);
420 }
421 break;
422 case CALL_AUDIO_MODE_DEFAULT:
423 default:
424 break;
425 }
426
427 g_object_set(self->manager, "speaker-state", self->speaker_state, NULL);
428 }
429
430 target_port = get_available_sink_port(info, NULL);
431 if (target_port) {
432 g_debug(" Using sink port '%s'", target_port);
433 op = pa_context_set_sink_port_by_index(ctx, self->sink_id,
434 target_port, NULL, NULL);
435 if (op)
436 pa_operation_unref(op);
437 }
438 }
439
440 /******************************************************************************
441 * Card management
442 *
443 * The following functions take care of gathering information about the default
444 * sound card
445 ******************************************************************************/
446
447 static void init_card_info(pa_context *ctx, const pa_card_info *info, int eol, void *data)
448 {
449 CadPulse *self = data;
450 pa_operation *op;
451 const gchar *prop;
452 gboolean has_speaker = FALSE;
453 gboolean has_earpiece = FALSE;
454 guint i;
455
456 if (eol != 0) {
457 if (self->card_id < 0) {
458 g_critical("No suitable card found, stopping here...");
459 }
460 return;
461 }
462
463 if (!info) {
464 g_critical("PA returned no card info (eol=%d)", eol);
465 return;
466 }
467
468 prop = pa_proplist_gets(info->proplist, "alsa.card_name");
469 g_debug("CARD: prop %s = %s", "alsa.card_name", prop);
470 if (prop && g_str_has_prefix(prop, CARD_MODEM_NAME))
471 return;
472 prop = pa_proplist_gets(info->proplist, PA_PROP_DEVICE_CLASS);
473 g_debug("CARD: prop %s = %s", PA_PROP_DEVICE_CLASS, prop);
474 if (prop && g_strcmp0(prop, CARD_MODEM_CLASS) == 0)
475 return;
476
477 for (i = 0; i < info->n_ports; i++) {
478 pa_card_port_info *port = info->ports[i];
479 g_debug("CARD: port %d/%d, name=%s", i, info->n_ports-1, port->name);
480
481 switch (port->type) {
482 case PA_DEVICE_PORT_TYPE_SPEAKER:
483 has_speaker = TRUE;
484 break;
485 case PA_DEVICE_PORT_TYPE_EARPIECE:
486 case PA_DEVICE_PORT_TYPE_HANDSET:
487 case PA_DEVICE_PORT_TYPE_HEADPHONES:
488 has_earpiece = TRUE;
489 break;
490 default:
491 break;
492 }
493 }
494
495 if (!has_speaker || !has_earpiece) {
496 g_message("Card '%s' lacks speaker and/or earpiece port, skipping...",
497 info->name);
498 return;
499 }
500
501 self->card_id = info->index;
502
503 g_debug("CARD: idx=%u name='%s'", info->index, info->name);
504
505 for (i = 0; i < info->n_profiles; i++) {
506 pa_card_profile_info2 *profile = info->profiles2[i];
507
508 if (g_str_has_prefix(profile->name, SND_USE_CASE_VERB_VOICECALL)) {
509 self->has_voice_profile = TRUE;
510 if (info->active_profile2 == profile)
511 self->audio_mode = CALL_AUDIO_MODE_CALL;
512 else
513 self->audio_mode = CALL_AUDIO_MODE_DEFAULT;
514 break;
515 }
516 }
517
518 // We were able determine the current mode, set the corresponding D-Bus property
519 if (self->audio_mode != CALL_AUDIO_MODE_UNKNOWN)
520 g_object_set(self->manager, "audio-mode", self->audio_mode, NULL);
521
522 g_debug("CARD: %s voice profile", self->has_voice_profile ? "has" : "doesn't have");
523
524 /* Found a suitable card, let's prepare the sink/source */
525 op = pa_context_get_sink_info_list(self->ctx, init_sink_info, self);
526 if (op)
527 pa_operation_unref(op);
528 op = pa_context_get_source_info_list(self->ctx, init_source_info, self);
529 if (op)
530 pa_operation_unref(op);
531 }
532
533 /******************************************************************************
534 * PulseAudio management
535 *
536 * The following functions configure the PulseAudio connection and monitor the
537 * state of PulseAudio objects
538 ******************************************************************************/
539
540 static void init_module_info(pa_context *ctx, const pa_module_info *info, int eol, void *data)
541 {
542 pa_operation *op;
543
544 if (eol != 0)
545 return;
546
547 if (!info) {
548 g_critical("PA returned no module info (eol=%d)", eol);
549 return;
550 }
551
552 g_debug("MODULE: idx=%u name='%s'", info->index, info->name);
553
554 if (g_strcmp0(info->name, "module-switch-on-port-available") == 0) {
555 g_debug("MODULE: unloading '%s'", info->name);
556 op = pa_context_unload_module(ctx, info->index, NULL, NULL);
557 if (op)
558 pa_operation_unref(op);
559 }
560 }
561
562 static void init_server_info(pa_context *ctx, const pa_server_info *info, void *data)
563 {
564 CadPulse *self = data;
565 static void *wireplumber_dll;
566
567 if (g_strrstr(info->server_name, "Pipewire") == NULL)
568 return;
569
570 if (!wireplumber_dll)
571 wireplumber_dll = dlopen("libwireplumber-0.5.so.0", RTLD_LAZY|RTLD_NODELETE);
572
573 if (wireplumber_dll) {
574 const char *(*wp_ver_sym)(void);
575
576 wp_ver_sym = dlsym(wireplumber_dll, "wp_get_library_version");
577 // 0.5.10 is the last release without routing support
578 // If the version is greater, and we're using Pipewire, assume WP
579 // is in charge of the routing.
580 if (wp_ver_sym && (as_vercmp_simple(wp_ver_sym(), "0.5.10") > 0)) {
581 cad_manager_disable_routing(CAD_MANAGER (self->manager));
582 }
583 }
584 }
585
586 static gboolean init_pulseaudio_objects(gpointer data)
587 {
588 CadPulse *self = data;
589 pa_operation *op;
590
591 self->card_id = self->sink_id = self->source_id = -1;
592 self->sink_ports = self->source_ports = NULL;
593
594 op = pa_context_get_card_info_list(self->ctx, init_card_info, self);
595 if (op)
596 pa_operation_unref(op);
597 op = pa_context_get_module_info_list(self->ctx, init_module_info, self);
598 if (op)
599 pa_operation_unref(op);
600 op = pa_context_get_server_info(self->ctx, init_server_info, self);
601 if (op)
602 pa_operation_unref(op);
603
604 return G_SOURCE_REMOVE;
605 }
606
607 static void changed_cb(pa_context *ctx, pa_subscription_event_type_t type, uint32_t idx, void *data)
608 {
609 CadPulse *self = data;
610 pa_subscription_event_type_t kind = type & PA_SUBSCRIPTION_EVENT_TYPE_MASK;
611 pa_operation *op = NULL;
612
613 switch (type & PA_SUBSCRIPTION_EVENT_FACILITY_MASK) {
614 case PA_SUBSCRIPTION_EVENT_SINK:
615 if (idx == self->sink_id && kind == PA_SUBSCRIPTION_EVENT_REMOVE) {
616 g_debug("sink %u removed", idx);
617 self->sink_id = -1;
618 g_hash_table_destroy(self->sink_ports);
619 self->sink_ports = NULL;
620 } else if (kind == PA_SUBSCRIPTION_EVENT_NEW) {
621 g_debug("new sink %u", idx);
622 op = pa_context_get_sink_info_by_index(ctx, idx, init_sink_info, self);
623 if (op)
624 pa_operation_unref(op);
625 }
626 break;
627 case PA_SUBSCRIPTION_EVENT_SOURCE:
628 if (idx == self->source_id && kind == PA_SUBSCRIPTION_EVENT_REMOVE) {
629 g_debug("source %u removed", idx);
630 self->source_id = -1;
631 g_hash_table_destroy(self->source_ports);
632 self->source_ports = NULL;
633 } else if (kind == PA_SUBSCRIPTION_EVENT_NEW) {
634 g_debug("new source %u", idx);
635 op = pa_context_get_source_info_by_index(ctx, idx, init_source_info, self);
636 if (op)
637 pa_operation_unref(op);
638 }
639 break;
640 case PA_SUBSCRIPTION_EVENT_CARD:
641 if (self->card_id < 0 && kind == PA_SUBSCRIPTION_EVENT_NEW) {
642 g_debug("card %u added, but no valid card detected yet, retrying...", idx);
643 g_idle_add(init_pulseaudio_objects, self);
644 } else if (idx == self->card_id && kind == PA_SUBSCRIPTION_EVENT_CHANGE) {
645 g_debug("card %u changed", idx);
646 if (self->sink_id != -1) {
647 op = pa_context_get_sink_info_by_index(ctx, self->sink_id,
648 change_sink_info, self);
649 if (op)
650 pa_operation_unref(op);
651 }
652 if (self->source_id != -1) {
653 op = pa_context_get_source_info_by_index(ctx, self->source_id,
654 change_source_info, self);
655 if (op)
656 pa_operation_unref(op);
657 }
658 }
659 break;
660 default:
661 break;
662 }
663 }
664
665 static void pulse_state_cb(pa_context *ctx, void *data)
666 {
667 CadPulse *self = data;
668 pa_context_state_t state;
669
670 state = pa_context_get_state(ctx);
671 switch (state) {
672 case PA_CONTEXT_UNCONNECTED:
673 case PA_CONTEXT_CONNECTING:
674 case PA_CONTEXT_AUTHORIZING:
675 case PA_CONTEXT_SETTING_NAME:
676 g_debug("PA not ready");
677 break;
678 case PA_CONTEXT_FAILED:
679 g_critical("Error in PulseAudio context: %s", pa_strerror(pa_context_errno(ctx)));
680 pulseaudio_cleanup(self);
681 g_idle_add(G_SOURCE_FUNC(pulseaudio_connect), self);
682 break;
683 case PA_CONTEXT_TERMINATED:
684 case PA_CONTEXT_READY:
685 pa_context_set_subscribe_callback(ctx, changed_cb, self);
686 pa_context_subscribe(ctx,
687 PA_SUBSCRIPTION_MASK_SINK | PA_SUBSCRIPTION_MASK_SOURCE | PA_SUBSCRIPTION_MASK_CARD,
688 NULL, self);
689 g_debug("PA is ready, initializing cards list");
690 g_idle_add(init_pulseaudio_objects, self);
691 break;
692 default:
693 g_return_if_reached();
694 }
695 }
696
697 static void pulseaudio_cleanup(CadPulse *self)
698 {
699 if (self->ctx) {
700 pa_context_disconnect(self->ctx);
701 pa_context_unref(self->ctx);
702 self->ctx = NULL;
703 }
704 }
705
706 static gboolean pulseaudio_connect(CadPulse *self)
707 {
708 pa_proplist *props;
709 int err;
710
711 /* Meta data */
712 props = pa_proplist_new();
713 g_assert(props != NULL);
714
715 err = pa_proplist_sets(props, PA_PROP_APPLICATION_NAME, APPLICATION_NAME);
716 err = pa_proplist_sets(props, PA_PROP_APPLICATION_ID, APPLICATION_ID);
717
718 if (!self->loop)
719 self->loop = pa_glib_mainloop_new(NULL);
720 if (!self->loop)
721 g_error ("Error creating PulseAudio main loop");
722
723 if (!self->ctx)
724 self->ctx = pa_context_new(pa_glib_mainloop_get_api(self->loop), APPLICATION_NAME);
725 if (!self->ctx)
726 g_error ("Error creating PulseAudio context");
727
728 pa_context_set_state_callback(self->ctx, (pa_context_notify_cb_t)pulse_state_cb, self);
729 err = pa_context_connect(self->ctx, NULL, PA_CONTEXT_NOFAIL, 0);
730 if (err < 0)
731 g_error ("Error connecting to PulseAudio context: %s", pa_strerror(err));
732
733 return G_SOURCE_REMOVE;
734 }
735
736 /******************************************************************************
737 * GObject base functions
738 ******************************************************************************/
739
740 static void constructed(GObject *object)
741 {
742 GObjectClass *parent_class = g_type_class_peek(G_TYPE_OBJECT);
743 CadPulse *self = CAD_PULSE(object);
744
745 pulseaudio_connect(self);
746
747 parent_class->constructed(object);
748 }
749
750
751 static void dispose(GObject *object)
752 {
753 GObjectClass *parent_class = g_type_class_peek(G_TYPE_OBJECT);
754 CadPulse *self = CAD_PULSE(object);
755
756 g_clear_pointer(&self->speaker_port, g_free);
757 g_clear_pointer(&self->earpiece_port, g_free);
758
759 pulseaudio_cleanup(self);
760
761 if (self->loop) {
762 pa_glib_mainloop_free(self->loop);
763 self->loop = NULL;
764 }
765
766 parent_class->dispose(object);
767 }
768
769 static void cad_pulse_class_init(CadPulseClass *klass)
770 {
771 GObjectClass *object_class = G_OBJECT_CLASS(klass);
772
773 object_class->constructed = constructed;
774 object_class->dispose = dispose;
775 }
776
777 static void cad_pulse_init(CadPulse *self)
778 {
779 self->manager = G_OBJECT(cad_manager_get_default());
780 self->audio_mode = CALL_AUDIO_MODE_UNKNOWN;
781 self->speaker_state = CALL_AUDIO_SPEAKER_UNKNOWN;
782 self->mic_state = CALL_AUDIO_MIC_UNKNOWN;
783 }
784
785 CadPulse *cad_pulse_get_default(void)
786 {
787 static CadPulse *pulse = NULL;
788
789 if (pulse == NULL) {
790 g_debug("initializing pulseaudio backend...");
791 pulse = g_object_new(CAD_TYPE_PULSE, NULL);
792 g_object_add_weak_pointer(G_OBJECT(pulse), (gpointer *)&pulse);
793 }
794
795 return pulse;
796 }
797
798 /******************************************************************************
799 * Commands management
800 *
801 * The following functions handle external requests to switch mode, output port
802 * or microphone status
803 ******************************************************************************/
804
805 static void operation_complete_cb(pa_context *ctx, int success, void *data)
806 {
807 CadPulseOperation *operation = data;
808
809 g_debug("operation returned %d", success);
810
811 if (operation) {
812 if (operation->op) {
813 operation->op->success = (gboolean)!!success;
814 if (operation->op->callback)
815 operation->op->callback(operation->op);
816
817 if (operation->op->success) {
818 guint new_value = GPOINTER_TO_UINT(operation->value);
819
820 switch (operation->op->type) {
821 case CAD_OPERATION_SELECT_MODE:
822 if (operation->pulse->audio_mode != new_value) {
823 operation->pulse->audio_mode = new_value;
824 g_object_set(operation->pulse->manager, "audio-mode", new_value, NULL);
825 }
826 break;
827 case CAD_OPERATION_ENABLE_SPEAKER:
828 if (operation->pulse->speaker_state != new_value) {
829 operation->pulse->speaker_state = new_value;
830 g_object_set(operation->pulse->manager, "speaker-state", new_value, NULL);
831 }
832 break;
833 case CAD_OPERATION_MUTE_MIC:
834 /*
835 * "Mute mic" operation's value is TRUE (1) for muting the mic,
836 * so ensure mic_state carries the right value.
837 */
838 new_value = new_value ? CALL_AUDIO_MIC_OFF : CALL_AUDIO_MIC_ON;
839 if (operation->pulse->mic_state != new_value) {
840 operation->pulse->mic_state = new_value;
841 g_object_set(operation->pulse->manager, "mic-state", new_value, NULL);
842 }
843 break;
844 default:
845 break;
846 }
847 }
848
849 free(operation->op);
850 }
851
852 free(operation);
853 }
854 }
855
856 static void set_card_profile(pa_context *ctx, const pa_card_info *info, int eol, void *data)
857 {
858 CadPulseOperation *operation = data;
859 pa_card_profile_info2 *profile;
860 pa_operation *op = NULL;
861 const char *target_verb = NULL;
862
863 if (eol != 0)
864 return;
865
866 if (!info) {
867 g_critical("PA returned no card info (eol=%d)", eol);
868 return;
869 }
870
871 if (info->index != operation->pulse->card_id)
872 return;
873
874 profile = info->active_profile2;
875
876 if (g_str_has_prefix(profile->name, SND_USE_CASE_VERB_VOICECALL) && operation->value == 0) {
877 g_debug("switching to default profile");
878 target_verb = SND_USE_CASE_VERB_HIFI;
879 } else if (g_str_has_prefix(profile->name, SND_USE_CASE_VERB_HIFI) && operation->value == 1) {
880 g_debug("switching to voice profile");
881 target_verb = SND_USE_CASE_VERB_VOICECALL;
882 }
883
884 if (target_verb) {
885 uint32_t target_priority = 0;
886 const char *target_profile = NULL;
887
888 for (int i = 0; i < info->n_profiles; i++) {
889 profile = info->profiles2[i];
890
891 if (profile->available &&
892 g_str_has_prefix(profile->name, target_verb) &&
893 profile->priority > target_priority) {
894 target_profile = profile->name;
895 target_priority = profile->priority;
896 }
897 }
898
899 if (target_profile) {
900 g_debug("target profile: '%s'", target_profile);
901 op = pa_context_set_card_profile_by_index(ctx, operation->pulse->card_id,
902 target_profile,
903 operation_complete_cb, operation);
904 }
905 }
906
907 if (op) {
908 pa_operation_unref(op);
909 } else {
910 g_debug("%s: nothing to be done", __func__);
911 operation_complete_cb(ctx, 1, operation);
912 }
913 }
914
915 static void set_output_port(pa_context *ctx, const pa_sink_info *info, int eol, void *data)
916 {
917 CadPulseOperation *operation = data;
918 pa_operation *op = NULL;
919 const gchar *target_port;
920
921 if (eol != 0)
922 return;
923
924 if (!info) {
925 g_critical("PA returned no sink info (eol=%d)", eol);
926 return;
927 }
928
929 if (info->card != operation->pulse->card_id || info->index != operation->pulse->sink_id)
930 return;
931
932 if (operation->op && operation->op->type == CAD_OPERATION_SELECT_MODE) {
933 /*
934 * When switching to voice call mode, we want to switch to any port
935 * other than the speaker; this makes sure we use the headphones if they
936 * are connected, and the earpiece otherwise.
937 * When switching back to normal mode, the highest priority port is to
938 * be selected anyway.
939 */
940 if (operation->value == CALL_AUDIO_MODE_CALL)
941 target_port = get_available_sink_port(info, operation->pulse->speaker_port);
942 else
943 target_port = get_available_sink_port(info, NULL);
944 } else {
945 /*
946 * When forcing speaker output, we simply select the speaker port.
947 * When disabling speaker output, we want the highest priority port
948 * other than the speaker, so that we use the headphones if connected,
949 * and the earpiece otherwise.
950 */
951 if (operation->value)
952 target_port = operation->pulse->speaker_port;
953 else
954 target_port = get_available_sink_port(info, operation->pulse->speaker_port);
955 }
956
957 g_debug("active port is '%s', target port is '%s'", info->active_port->name, target_port);
958
959 if (target_port && g_strcmp0(info->active_port->name, target_port) != 0) {
960 g_debug("switching to target port '%s'", target_port);
961 op = pa_context_set_sink_port_by_index(ctx, operation->pulse->sink_id,
962 target_port,
963 operation_complete_cb, operation);
964 }
965
966 if (op) {
967 pa_operation_unref(op);
968 } else {
969 g_debug("%s: nothing to be done", __func__);
970 operation_complete_cb(ctx, 1, operation);
971 }
972 }
973
974 /**
975 * cad_pulse_select_mode:
976 * @mode:
977 * @cad_op:
978 *
979 */
980 void cad_pulse_select_mode(CallAudioMode mode, CadOperation *cad_op)
981 {
982 CadPulseOperation *operation = g_new(CadPulseOperation, 1);
983 pa_operation *op = NULL;
984
985 if (!cad_op) {
986 g_critical("%s: no callaudiod operation", __func__);
987 goto error;
988 }
989
990 /*
991 * Make sure cad_op is of the correct type!
992 */
993 g_assert(cad_op->type == CAD_OPERATION_SELECT_MODE);
994
995 operation->pulse = cad_pulse_get_default();
996 operation->op = cad_op;
997 operation->value = mode;
998
999 if (mode != CALL_AUDIO_MODE_CALL) {
1000 /*
1001 * When ending a call, we want to make sure the mic doesn't stay muted
1002 */
1003 CadOperation *unmute_op = g_new0(CadOperation, 1);
1004 unmute_op->type = CAD_OPERATION_MUTE_MIC;
1005
1006 cad_pulse_mute_mic(FALSE, unmute_op);
1007
1008 /*
1009 * If the card has a dedicated voice profile, disable speaker so it
1010 * doesn't get automatically enabled for next call.
1011 */
1012 if (operation->pulse->has_voice_profile) {
1013 CadOperation *disable_speaker_op = g_new0(CadOperation, 1);
1014 disable_speaker_op->type = CAD_OPERATION_ENABLE_SPEAKER;
1015
1016 cad_pulse_enable_speaker(FALSE, disable_speaker_op);
1017 }
1018 }
1019
1020 if (operation->pulse->has_voice_profile) {
1021 /*
1022 * The pinephone f.e. has a voice profile
1023 */
1024 g_debug("card has voice profile, using it");
1025 op = pa_context_get_card_info_by_index(operation->pulse->ctx,
1026 operation->pulse->card_id,
1027 set_card_profile, operation);
1028 } else {
1029 if (operation->pulse->sink_id < 0) {
1030 g_warning("card has no voice profile and no usable sink");
1031 goto error;
1032 }
1033 g_debug("card doesn't have voice profile, switching output port");
1034
1035 op = pa_context_get_sink_info_by_index(operation->pulse->ctx,
1036 operation->pulse->sink_id,
1037 set_output_port, operation);
1038 }
1039
1040 if (op)
1041 pa_operation_unref(op);
1042
1043 return;
1044
1045 error:
1046 if (cad_op) {
1047 cad_op->success = FALSE;
1048 if (cad_op->callback)
1049 cad_op->callback(cad_op);
1050 }
1051 if (operation)
1052 free(operation);
1053 }
1054
1055 void cad_pulse_enable_speaker(gboolean enable, CadOperation *cad_op)
1056 {
1057 CadPulseOperation *operation = g_new(CadPulseOperation, 1);
1058 pa_operation *op = NULL;
1059
1060 if (!cad_op) {
1061 g_critical("%s: no callaudiod operation", __func__);
1062 goto error;
1063 }
1064
1065 /*
1066 * Make sure cad_op is of the correct type!
1067 */
1068 g_assert(cad_op->type == CAD_OPERATION_ENABLE_SPEAKER);
1069
1070 operation->pulse = cad_pulse_get_default();
1071
1072 if (operation->pulse->sink_id < 0) {
1073 g_warning("card has no usable sink");
1074 goto error;
1075 }
1076
1077 operation->op = cad_op;
1078 operation->value = (guint)enable;
1079
1080 op = pa_context_get_sink_info_by_index(operation->pulse->ctx,
1081 operation->pulse->sink_id,
1082 set_output_port, operation);
1083 if (op)
1084 pa_operation_unref(op);
1085
1086 return;
1087
1088 error:
1089 if (cad_op) {
1090 cad_op->success = FALSE;
1091 if (cad_op->callback)
1092 cad_op->callback(cad_op);
1093 }
1094 if (operation)
1095 free(operation);
1096 }
1097
1098 void cad_pulse_mute_mic(gboolean mute, CadOperation *cad_op)
1099 {
1100 CadPulseOperation *operation = g_new(CadPulseOperation, 1);
1101 pa_operation *op = NULL;
1102
1103 if (!cad_op) {
1104 g_critical("%s: no callaudiod operation", __func__);
1105 goto error;
1106 }
1107
1108 /*
1109 * Make sure cad_op is of the correct type!
1110 */
1111 g_assert(cad_op->type == CAD_OPERATION_MUTE_MIC);
1112
1113 operation->pulse = cad_pulse_get_default();
1114
1115 if (operation->pulse->source_id < 0) {
1116 g_warning("card has no usable source");
1117 goto error;
1118 }
1119
1120 operation->op = cad_op;
1121 operation->value = (guint)mute;
1122
1123 if (operation->pulse->mic_state == CALL_AUDIO_MIC_OFF && !operation->value) {
1124 g_debug("mic is muted, unmuting...");
1125 op = pa_context_set_source_mute_by_index(operation->pulse->ctx,
1126 operation->pulse->source_id, 0,
1127 operation_complete_cb, operation);
1128 } else if (operation->pulse->mic_state == CALL_AUDIO_MIC_ON && operation->value) {
1129 g_debug("mic is active, muting...");
1130 op = pa_context_set_source_mute_by_index(operation->pulse->ctx,
1131 operation->pulse->source_id, 1,
1132 operation_complete_cb, operation);
1133 }
1134
1135 if (op) {
1136 pa_operation_unref(op);
1137 } else {
1138 g_debug("%s: nothing to be done", __func__);
1139 operation_complete_cb(operation->pulse->ctx, 1, operation);
1140 }
1141
1142 return;
1143
1144 error:
1145 if (cad_op) {
1146 cad_op->success = FALSE;
1147 if (cad_op->callback)
1148 cad_op->callback(cad_op);
1149 }
1150 if (operation)
1151 free(operation);
1152 }
1153
1154 CallAudioMode cad_pulse_get_audio_mode(void)
1155 {
1156 CadPulse *self = cad_pulse_get_default();
1157 return self->audio_mode;
1158 }
1159
1160 CallAudioSpeakerState cad_pulse_get_speaker_state(void)
1161 {
1162 CadPulse *self = cad_pulse_get_default();
1163 return self->speaker_state;
1164 }
1165
1166 CallAudioMicState cad_pulse_get_mic_state(void)
1167 {
1168 CadPulse *self = cad_pulse_get_default();
1169 return self->mic_state;
1170 }