2 * module-murphy-ivi -- PulseAudio module for providing audio routing support
3 * Copyright (c) 2012, Intel Corporation.
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU Lesser General Public License,
7 * version 2.1, as published by the Free Software Foundation.
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.
12 * See the GNU Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with this program; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin St - Fifth Floor, Boston,
25 #include <pulsecore/pulsecore-config.h>
27 #include <pulse/proplist.h>
28 #include <pulsecore/module.h>
34 #include "constrain.h"
41 static void rtgroup_destroy(struct userdata *, mir_rtgroup *);
42 static int rtgroup_print(mir_rtgroup *, char *, int);
43 static void rtgroup_update_module_property(struct userdata *, mir_direction,
46 static void add_rtentry(struct userdata *, mir_direction, mir_rtgroup *,
48 static void remove_rtentry(struct userdata *, mir_rtentry *);
50 static void make_explicit_routes(struct userdata *, uint32_t);
51 static mir_node *find_default_route(struct userdata *, mir_node *, uint32_t);
52 static void implement_preroute(struct userdata *, mir_node *, mir_node *,
54 static void implement_default_route(struct userdata *, mir_node *, mir_node *,
57 static int uint32_cmp(uint32_t, uint32_t);
59 static int node_priority(struct userdata *, mir_node *);
61 static int volume_class(mir_node *);
63 static int print_routing_table(pa_hashmap *, const char *, char *, int);
65 pa_router *pa_router_init(struct userdata *u)
67 size_t num_classes = mir_application_class_end;
68 pa_router *router = pa_xnew0(pa_router, 1);
70 router->rtgroups.input = pa_hashmap_new(pa_idxset_string_hash_func,
71 pa_idxset_string_compare_func);
72 router->rtgroups.output = pa_hashmap_new(pa_idxset_string_hash_func,
73 pa_idxset_string_compare_func);
75 router->maplen = num_classes;
77 router->priormap = pa_xnew0(int, num_classes);
79 MIR_DLIST_INIT(router->nodlist);
80 MIR_DLIST_INIT(router->connlist);
85 void pa_router_done(struct userdata *u)
88 mir_connection *conn, *c;
95 if (u && (router = u->router)) {
96 MIR_DLIST_FOR_EACH_SAFE(mir_node, rtprilist, e,n, &router->nodlist) {
97 MIR_DLIST_UNLINK(mir_node, rtprilist, e);
100 MIR_DLIST_FOR_EACH_SAFE(mir_connection,link, conn,c,&router->connlist){
101 MIR_DLIST_UNLINK(mir_connection, link, conn);
105 PA_HASHMAP_FOREACH(rtg, router->rtgroups.input, state) {
106 rtgroup_destroy(u, rtg);
109 PA_HASHMAP_FOREACH(rtg, router->rtgroups.output, state) {
110 rtgroup_destroy(u, rtg);
113 pa_hashmap_free(router->rtgroups.input);
114 pa_hashmap_free(router->rtgroups.output);
116 for (i = 0; i < MRP_ZONE_MAX; i++) {
117 if ((map = router->classmap.input[i]))
120 if ((map = router->classmap.output[i]))
124 pa_xfree(router->priormap);
132 void mir_router_assign_class_priority(struct userdata *u,
140 pa_assert_se((router = u->router));
141 pa_assert_se((priormap = router->priormap));
143 if (class >= 0 && class < router->maplen) {
144 pa_log_debug("assigning priority %d to class '%s'",
145 pri, mir_node_type_str(class));
146 priormap[class] = pri;
151 mir_rtgroup *mir_router_create_rtgroup(struct userdata *u,
154 mir_rtgroup_accept_t accept,
155 mir_rtgroup_compare_t compare)
162 pa_assert(type == mir_input || type == mir_output);
166 pa_assert_se((router = u->router));
168 if (type == mir_input)
169 table = router->rtgroups.input;
171 table = router->rtgroups.output;
175 rtg = pa_xnew0(mir_rtgroup, 1);
176 rtg->name = pa_xstrdup(name);
177 rtg->accept = accept;
178 rtg->compare = compare;
179 MIR_DLIST_INIT(rtg->entries);
181 if (pa_hashmap_put(table, rtg->name, rtg) < 0) {
187 pa_log_debug("%s routing group '%s' created",
188 mir_direction_str(type), name);
193 void mir_router_destroy_rtgroup(struct userdata *u,
203 pa_assert_se((router = u->router));
205 if (type == mir_input)
206 table = router->rtgroups.input;
208 table = router->rtgroups.output;
212 if (!(rtg = pa_hashmap_remove(table, name))) {
213 pa_log_debug("can't destroy %s routing group '%s': group not found",
214 mir_direction_str(type), name);
217 rtgroup_destroy(u, rtg);
218 pa_log_debug("routing group '%s' destroyed", name);
223 bool mir_router_assign_class_to_rtgroup(struct userdata *u,
227 const char *rtgrpnam)
231 mir_rtgroup ***classmap;
232 mir_rtgroup **zonemap;
235 const char *direction;
239 pa_assert(zone < MRP_ZONE_MAX);
240 pa_assert(type == mir_input || type == mir_output);
242 pa_assert_se((router = u->router));
244 if (type == mir_input) {
245 rtable = router->rtgroups.input;
246 classmap = router->classmap.input;
249 rtable = router->rtgroups.output;
250 classmap = router->classmap.output;
253 if (class < 0 || class >= router->maplen) {
254 pa_log_debug("can't assign class (%d) to routing group '%s': "
255 "class id is out of range (0 - %d)",
256 class, rtgrpnam, router->maplen);
260 clnam = mir_node_type_str(class);
261 direction = mir_direction_str(type);
263 if (!(rtg = pa_hashmap_get(rtable, rtgrpnam))) {
264 pa_log_debug("can't assign class '%s' to %s routing group '%s': "
265 "router group not found", clnam, direction, rtgrpnam);
268 if (!(zonemap = classmap[zone])) {
269 zonemap = pa_xnew0(mir_rtgroup *, router->maplen);
270 classmap[zone] = zonemap;
273 zonemap[class] = rtg;
275 if ((z = pa_zoneset_get_zone_by_index(u, zone))) {
276 pa_log_debug("class '%s'@'%s' assigned to %s routing group '%s'",
277 clnam, z->name, direction, rtgrpnam);
280 pa_log_debug("class '%s'@zone%u assigned to %s routing group '%s'",
281 clnam, zone, direction, rtgrpnam);
289 void mir_router_register_node(struct userdata *u, mir_node *node)
299 pa_assert_se((router = u->router));
301 if (node->direction == mir_output) {
302 if (node->implement == mir_device) {
303 PA_HASHMAP_FOREACH(rtg, router->rtgroups.output, state) {
304 add_rtentry(u, mir_output, rtg, node);
311 if (node->direction == mir_input) {
313 if (node->implement == mir_device) &&
314 !pa_classify_loopback_stream(node))
318 if (node->implement == mir_device) {
319 PA_HASHMAP_FOREACH(rtg, router->rtgroups.input, state) {
320 add_rtentry(u, mir_input, rtg, node);
323 if (!pa_classify_loopback_stream(node))
327 priority = node_priority(u, node);
329 MIR_DLIST_FOR_EACH(mir_node, rtprilist, before, &router->nodlist) {
330 if (priority < node_priority(u, before)) {
331 MIR_DLIST_INSERT_BEFORE(mir_node, rtprilist, node,
337 MIR_DLIST_APPEND(mir_node, rtprilist, node, &router->nodlist);
343 void mir_router_unregister_node(struct userdata *u, mir_node *node)
346 mir_rtentry *rte, *n;
350 pa_assert_se((router = u->router));
352 MIR_DLIST_FOR_EACH_SAFE(mir_rtentry,nodchain, rte,n, &node->rtentries) {
353 remove_rtentry(u, rte);
356 MIR_DLIST_UNLINK(mir_node, rtprilist, node);
359 mir_connection *mir_router_add_explicit_route(struct userdata *u,
365 mir_connection *conn;
370 pa_assert_se((router = u->router));
372 conn = pa_xnew0(mir_connection, 1);
373 MIR_DLIST_INIT(conn->link);
375 conn->from = from->index;
376 conn->to = to->index;
378 MIR_DLIST_APPEND(mir_connection, link, conn, &router->connlist);
380 mir_router_make_routing(u);
385 void mir_router_remove_explicit_route(struct userdata *u, mir_connection *conn)
394 pa_assert_se((core = u->core));
395 pa_assert_se((router = u->router));
397 MIR_DLIST_UNLINK(mir_connection, link, conn);
399 if (!(from = mir_node_find_by_index(u, conn->from)) ||
400 !(to = mir_node_find_by_index(u, conn->to)) )
402 pa_log_debug("can't remove explicit route: some node was not found");
405 pa_log_debug("tear down link '%s' => '%s'", from->amname, to->amname);
407 if (!mir_switch_teardown_link(u, from, to)) {
408 pa_log_debug("can't remove explicit route: "
409 "failed to teardown link");
413 mir_router_make_routing(u);
421 int mir_router_print_rtgroups(struct userdata *u, char *buf, int len)
429 pa_assert_se((router = u->router));
430 pa_assert(router->rtgroups.input);
431 pa_assert(router->rtgroups.output);
436 p += print_routing_table(router->rtgroups.input, "input", p, e-p);
439 p += print_routing_table(router->rtgroups.output, "output", p, e-p);
445 mir_node *mir_router_make_prerouting(struct userdata *u, mir_node *data)
456 pa_assert_se((router = u->router));
457 pa_assert_se((data->implement == mir_stream));
459 priority = node_priority(u, data);
462 stamp = pa_utils_new_stamp();
464 make_explicit_routes(u, stamp);
466 pa_audiomgr_delete_default_routes(u);
468 MIR_DLIST_FOR_EACH_BACKWARD(mir_node, rtprilist, start, &router->nodlist) {
469 if (start->implement == mir_device) {
471 if (start->direction == mir_output)
472 continue; /* we should never get here */
473 if (!start->mux && !start->loop)
474 continue; /* skip not looped back input nodes */
477 continue; /* only looped back devices routed here */
480 if (priority >= node_priority(u, start)) {
481 if ((target = find_default_route(u, data, stamp)))
482 implement_preroute(u, data, target, stamp);
486 if (start->stamp >= stamp)
489 if ((end = find_default_route(u, start, stamp)))
490 implement_default_route(u, start, end, stamp);
493 if (!done && (target = find_default_route(u, data, stamp)))
494 implement_preroute(u, data, target, stamp);
500 void mir_router_make_routing(struct userdata *u)
502 static bool ongoing_routing;
510 pa_assert_se((router = u->router));
515 ongoing_routing = true;
516 stamp = pa_utils_new_stamp();
518 make_explicit_routes(u, stamp);
520 pa_audiomgr_delete_default_routes(u);
522 MIR_DLIST_FOR_EACH_BACKWARD(mir_node,rtprilist, start, &router->nodlist) {
523 if (start->implement == mir_device) {
525 if (start->direction == mir_output)
526 continue; /* we should never get here */
527 if (!start->mux && !start->loop)
528 continue; /* skip not looped back input nodes */
531 continue; /* only looped back devices routed here */
534 if (start->stamp >= stamp)
537 if ((end = find_default_route(u, start, stamp)))
538 implement_default_route(u, start, end, stamp);
541 pa_audiomgr_send_default_routes(u);
543 pa_fader_apply_volume_limits(u, stamp);
545 ongoing_routing = false;
550 bool mir_router_default_accept(struct userdata *u, mir_rtgroup *rtg,
559 const char *role, *excluded_role;
567 if (class == mir_bluetooth_carkit)
569 else if (class == mir_jack || class == mir_hdmi) {
570 pa_assert_se((core = u->core));
572 if (node->direction == mir_input) {
573 source = pa_idxset_get_by_index(core->sources,node->paidx);
574 pl = source ? source->proplist : NULL;
575 excluded_role = "hfp_uplink";
578 sink = pa_idxset_get_by_index(core->sinks, node->paidx);
579 pl = sink ? sink->proplist : NULL;
580 excluded_role = "hfp_downlink";
582 role = pl ? pa_proplist_gets(pl, PA_PROP_NODE_ROLE) : NULL;
583 accept = role ? strcmp(role, excluded_role) : true;
586 accept = (class >= mir_device_class_begin &&
587 class < mir_device_class_end);
594 bool mir_router_phone_accept(struct userdata *u, mir_rtgroup *rtg,
605 if (class >= mir_device_class_begin && class < mir_device_class_end) {
606 if (class != mir_bluetooth_a2dp &&
607 class != mir_spdif &&
609 class != mir_bluetooth_source &&
610 class != mir_bluetooth_sink &&
611 class != mir_bluetooth_carkit )
621 int mir_router_default_compare(struct userdata *u, mir_rtgroup *rtg,
622 mir_node *n1, mir_node *n2)
632 if (n1->type == mir_null)
634 if (n2->type == mir_null)
637 p1 = ((((n1->channels & 31) << 5) + n1->privacy) << 2) + n1->location;
638 p2 = ((((n2->channels & 31) << 5) + n2->privacy) << 2) + n2->location;
640 p1 = (p1 << 8) + ((n1->type - mir_device_class_begin) & 0xff);
641 p2 = (p2 << 8) + ((n2->type - mir_device_class_begin) & 0xff);
643 return uint32_cmp(p1,p2);
647 int mir_router_phone_compare(struct userdata *u, mir_rtgroup *rtg,
648 mir_node *n1, mir_node *n2)
658 if (n1->type == mir_null)
660 if (n2->type == mir_null)
663 p1 = (n1->privacy << 8) + ((n1->type - mir_device_class_begin) & 0xff);
664 p2 = (n2->privacy << 8) + ((n2->type - mir_device_class_begin) & 0xff);
666 return uint32_cmp(p1,p2);
670 static void rtgroup_destroy(struct userdata *u, mir_rtgroup *rtg)
672 mir_rtentry *rte, *n;
677 MIR_DLIST_FOR_EACH_SAFE(mir_rtentry, link, rte,n, &rtg->entries) {
678 remove_rtentry(u, rte);
685 static int rtgroup_print(mir_rtgroup *rtg, char *buf, int len)
695 MIR_DLIST_FOR_EACH_BACKWARD(mir_rtentry, link, rte, &rtg->entries) {
699 p += snprintf(p, (size_t)(e-p), " '%s'", node->amname);
705 static void rtgroup_update_module_property(struct userdata *u,
716 pa_assert(rtg->name);
717 pa_assert_se((module = u->module));
719 snprintf(key, sizeof(key), PA_PROP_ROUTING_TABLE ".%s.%s",
720 mir_direction_str(type), rtg->name);
721 ret = rtgroup_print(rtg, value, sizeof(value));
726 pa_proplist_sets(module->proplist, key, value+1); /* skip ' '@beginning */
729 static void add_rtentry(struct userdata *u,
735 mir_rtentry *rte, *before;
740 pa_assert_se((router = u->router));
742 if (!rtg->accept(u, rtg, node)) {
743 pa_log_debug("refuse node '%s' registration to routing group '%s'",
744 node->amname, rtg->name);
748 rte = pa_xnew0(mir_rtentry, 1);
750 MIR_DLIST_APPEND(mir_rtentry, nodchain, rte, &node->rtentries);
754 MIR_DLIST_FOR_EACH(mir_rtentry, link, before, &rtg->entries) {
755 if (rtg->compare(u, rtg, node, before->node) < 0) {
756 MIR_DLIST_INSERT_BEFORE(mir_rtentry, link, rte, &before->link);
761 MIR_DLIST_APPEND(mir_rtentry, link, rte, &rtg->entries);
764 rtgroup_update_module_property(u, type, rtg);
765 pa_log_debug("node '%s' added to routing group '%s'",
766 node->amname, rtg->name);
769 static void remove_rtentry(struct userdata *u, mir_rtentry *rte)
776 pa_assert_se((rtg = rte->group));
777 pa_assert_se((node = rte->node));
779 MIR_DLIST_UNLINK(mir_rtentry, link, rte);
780 MIR_DLIST_UNLINK(mir_rtentry, nodchain, rte);
784 rtgroup_update_module_property(u, node->direction, rtg);
787 static void make_explicit_routes(struct userdata *u, uint32_t stamp)
790 mir_connection *conn;
795 pa_assert_se((router = u->router));
797 MIR_DLIST_FOR_EACH_BACKWARD(mir_connection,link, conn, &router->connlist) {
801 if (!(from = mir_node_find_by_index(u, conn->from)) ||
802 !(to = mir_node_find_by_index(u, conn->to)) )
804 pa_log_debug("ignoring explicit route %u: some of the nodes "
805 "not found", conn->amid);
809 if (!mir_switch_setup_link(u, from, to, true))
812 if (from->implement == mir_stream)
815 if (to->implement == mir_device)
816 mir_volume_add_limiting_class(u, to, volume_class(from), stamp);
821 static mir_node *find_default_route(struct userdata *u,
825 pa_router *router = u->router;
826 mir_node_type class = pa_classify_guess_application_class(start);
827 mir_zone *zone = pa_zoneset_get_zone_by_name(u, start->zone);
834 if (class < 0 || class > router->maplen) {
835 pa_log_debug("can't route '%s': class %d is out of range (0 - %d)",
836 start->amname, class, router->maplen);
841 pa_log_debug("can't route '%s': zone '%s' is unknown",
842 start->amname, start->zone);
846 switch (start->direction) {
847 case mir_input: cmap = router->classmap.output; break;
848 case mir_output: cmap = router->classmap.input; break;
849 default: cmap = NULL; break;
852 if (!cmap || !(zmap = cmap[zone->index]) || !(rtg = zmap[class])) {
853 pa_log_debug("node '%s' won't be routed beacuse its class '%s' "
854 "is not assigned to any router group",
855 start->amname, mir_node_type_str(class));
859 pa_log_debug("using '%s' router group when routing '%s'",
860 rtg->name, start->amname);
863 MIR_DLIST_FOR_EACH_BACKWARD(mir_rtentry, link, rte, &rtg->entries) {
864 if (!(end = rte->node)) {
865 pa_log(" node was null in mir_rtentry");
870 pa_log_debug(" '%s' ignored. Skipping...",end->amname);
874 if (!end->available) {
875 pa_log_debug(" '%s' not available. Skipping...", end->amname);
879 if (end->paidx == PA_IDXSET_INVALID && !end->paport) {
880 /* requires profile change. We do it only for BT headsets */
881 if (end->type != mir_bluetooth_a2dp &&
882 end->type != mir_bluetooth_sco )
884 pa_log_debug(" '%s' has no sink. Skipping...", end->amname);
889 if (rte->stamp < stamp)
890 mir_constrain_apply(u, end, stamp);
893 pa_log_debug(" '%s' is blocked by constraints. Skipping...",
899 pa_log_debug("routing '%s' => '%s'", start->amname, end->amname);
901 pa_audiomgr_add_default_route(u, start, end);
906 pa_log_debug("could not find route for '%s'", start->amname);
911 static void implement_preroute(struct userdata *u,
916 if (data->direction == mir_output)
917 mir_switch_setup_link(u, target, NULL, false);
919 mir_switch_setup_link(u, NULL, target, false);
920 mir_volume_add_limiting_class(u, target, data->type, stamp);
924 static void implement_default_route(struct userdata *u,
929 if (start->direction == mir_output)
930 mir_switch_setup_link(u, end, start, false);
932 mir_switch_setup_link(u, start, end, false);
933 mir_volume_add_limiting_class(u, end, volume_class(start), stamp);
938 static int uint32_cmp(uint32_t v1, uint32_t v2)
947 static int node_priority(struct userdata *u, mir_node *node)
954 pa_assert_se((router = u->router));
955 pa_assert(router->priormap);
957 class = pa_classify_guess_application_class(node);
959 if (class < 0 || class >= (int)router->maplen)
962 return router->priormap[class];
965 static int volume_class(mir_node *node)
967 int device_class[mir_device_class_end - mir_device_class_begin] = {
968 [ mir_bluetooth_carkit - mir_device_class_begin ] = mir_phone,
969 [ mir_bluetooth_source - mir_device_class_begin ] = mir_player,
978 if (t >= mir_application_class_begin && t < mir_application_class_end)
981 if (t >= mir_device_class_begin && t < mir_device_class_end)
982 return device_class[t - mir_device_class_begin];
984 return mir_node_type_unknown;
988 static int print_routing_table(pa_hashmap *table,
1002 e = (p = buf) + len;
1006 p += snprintf(p, (size_t)(e-p), "%s routing table:\n", type);
1011 PA_HASHMAP_FOREACH(rtg, table, state) {
1015 p += snprintf(p, (size_t)(e-p), " %s:", rtg->name);
1018 p += rtgroup_print(rtg, p, e-p);
1021 p += snprintf(p, (size_t)(e-p), "\n");
1025 p += snprintf(p, (size_t)(e-p), " <empty>\n");
1036 * indent-tabs-mode: nil