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,
23 #include <pulsecore/pulsecore-config.h>
25 #include <pulsecore/hashmap.h>
26 #include <pulsecore/idxset.h>
27 #include <pulsecore/client.h>
28 #include <pulsecore/core-util.h>
29 #include <pulsecore/log.h>
30 #include <pulsecore/card.h>
31 #include <pulsecore/device-port.h>
32 #include <pulsecore/sink-input.h>
33 #include <pulsecore/source-output.h>
34 #include <pulsecore/strbuf.h>
41 #include "constrain.h"
42 #include "multiplex.h"
48 #include "stream-state.h"
51 #define MAX_CARD_TARGET 4
52 #define MAX_NAME_LENGTH 256
54 #define ACTIVE_PORT NULL
56 /* Bluetooth service class */
57 #define BIT(x) (1U << (x))
59 #define BT_SERVICE_MASK 0xffe
60 #define BT_SERVICE_INFORMATION BIT(23) /**< WEB-server, WAP-server, etc */
61 #define BT_SERVICE_TELEPHONY BIT(22) /**< Modem, Headset, etc*/
62 #define BT_SERVICE_AUDIO BIT(21) /**< Speaker, Microphone, Headset */
63 #define BT_SERVICE_OBJECT_XFER BIT(20) /**< v-Inbox, v-Folder, etc */
64 #define BT_SERVICE_CAPTURING BIT(19) /**< Scanner, Microphone, etc */
65 #define BT_SERVICE_RENDERING BIT(18) /**< Printing, Speaker, etc */
66 #define BT_SERVICE_NETWORKING BIT(17) /**< LAN, Ad hoc, etc */
67 #define BT_SERVICE_POSITIONING BIT(16) /**< Location identification */
86 static const char combine_pattern[] = "Simultaneous output on ";
87 static const char loopback_outpatrn[] = "Loopback from ";
88 static const char loopback_inpatrn[] = "Loopback to ";
90 static void handle_alsa_card(struct userdata *, pa_card *);
91 static void handle_bluetooth_card(struct userdata *, pa_card *);
92 static pa_bool_t get_bluetooth_port_availability(mir_node *, pa_device_port *);
94 static void handle_udev_loaded_card(struct userdata *, pa_card *,
96 static void handle_card_ports(struct userdata *, mir_node *,
97 pa_card *, pa_card_profile *);
99 static mir_node *create_node(struct userdata *, mir_node *, pa_bool_t *);
100 static void destroy_node(struct userdata *, mir_node *);
101 static pa_bool_t update_node_availability(struct userdata *, mir_node *,
103 static pa_bool_t update_node_availability_by_device(struct userdata *,
105 void *, pa_device_port *,
108 static void parse_profile_name(pa_card_profile *,
109 char **, char **, char *, int);
111 static char *node_key(struct userdata *, mir_direction,
112 void *, pa_device_port *, char *, size_t);
114 static pa_sink *make_output_prerouting(struct userdata *, mir_node *,
115 pa_channel_map *, const char *,
117 static pa_source *make_input_prerouting(struct userdata *, mir_node *,
118 const char *, mir_node **);
120 static mir_node_type get_stream_routing_class(pa_proplist *);
122 static void set_bluetooth_profile(struct userdata *, pa_card *, pa_direction_t);
125 static void schedule_deferred_routing(struct userdata *);
126 static void schedule_card_check(struct userdata *, pa_card *);
127 static void schedule_source_cleanup(struct userdata *, mir_node *);
129 static void schedule_stream_uncorking(struct userdata *, pa_sink *);
132 static void pa_hashmap_node_free(void *node, void *u)
134 mir_node_destroy(u, node);
138 struct pa_discover *pa_discover_init(struct userdata *u)
140 pa_discover *discover = pa_xnew0(pa_discover, 1);
144 discover->selected = TRUE;
146 discover->nodes.byname = pa_hashmap_new(pa_idxset_string_hash_func,
147 pa_idxset_string_compare_func);
148 discover->nodes.byptr = pa_hashmap_new(pa_idxset_trivial_hash_func,
149 pa_idxset_trivial_compare_func);
153 void pa_discover_done(struct userdata *u)
155 pa_discover *discover;
159 if (u && (discover = u->discover)) {
160 PA_HASHMAP_FOREACH(node, discover->nodes.byname, state) {
161 mir_node_destroy(u, node);
163 pa_hashmap_free(discover->nodes.byname, NULL);
164 pa_hashmap_free(discover->nodes.byptr, NULL);
170 void pa_discover_domain_up(struct userdata *u)
172 pa_discover *discover;
177 pa_assert_se((discover = u->discover));
179 PA_HASHMAP_FOREACH(node, discover->nodes.byname, state) {
180 node->amid = AM_ID_INVALID;
182 if (node->visible && node->available) {
183 pa_audiomgr_register_node(u, node);
184 extapi_signal_node_change(u);
189 void pa_discover_domain_down(struct userdata *u)
193 void pa_discover_add_card(struct userdata *u, pa_card *card)
200 if (!(bus = pa_utils_get_card_bus(card))) {
201 pa_log_debug("ignoring card '%s' due to lack of '%s' property",
202 pa_utils_get_card_name(card), PA_PROP_DEVICE_BUS);
206 if (pa_streq(bus, "pci") || pa_streq(bus, "usb")) {
207 handle_alsa_card(u, card);
210 else if (pa_streq(bus, "bluetooth")) {
211 handle_bluetooth_card(u, card);
215 pa_log_debug("ignoring card '%s' due to unsupported bus type '%s'",
216 pa_utils_get_card_name(card), bus);
219 void pa_discover_remove_card(struct userdata *u, pa_card *card)
222 pa_discover *discover;
228 pa_assert_se((discover = u->discover));
230 if (!(bus = pa_utils_get_card_bus(card)))
234 PA_HASHMAP_FOREACH(node, discover->nodes.byname, state) {
235 if (node->implement == mir_device &&
236 node->pacard.index == card->index)
238 if (pa_streq(bus, "pci") || pa_streq(bus, "usb"))
239 mir_constrain_destroy(u, node->paname);
241 destroy_node(u, node);
245 if (pa_streq(bus, "bluetooth"))
246 mir_constrain_destroy(u, card->name);
249 void pa_discover_profile_changed(struct userdata *u, pa_card *card)
252 pa_card_profile *prof;
255 pa_discover *discover;
264 pa_bool_t need_routing;
268 pa_assert_se((core = u->core));
269 pa_assert_se((discover = u->discover));
272 if ((bus = pa_utils_get_card_bus(card)) == NULL) {
273 pa_log_debug("ignoring profile change on card '%s' due to lack of '%s'"
274 "property", pa_utils_get_card_name(card),
279 pci = pa_streq(bus, "pci");
280 usb = pa_streq(bus, "usb");
281 bluetooth = pa_streq(bus, "bluetooth");
283 if (!pci && !usb && !bluetooth) {
284 pa_log_debug("ignoring profile change on card '%s' due to unsupported "
285 "bus type '%s'", pa_utils_get_card_name(card), bus);
286 u->state.sink = u->state.source = PA_IDXSET_INVALID;
290 if ((index = u->state.sink) != PA_IDXSET_INVALID) {
291 if ((sink = pa_idxset_get_by_index(core->sinks, index)))
292 pa_discover_add_sink(u, sink, TRUE);
294 pa_log_debug("sink.%u is gone", index);
295 u->state.sink = PA_IDXSET_INVALID;
298 if ((index = u->state.source) != PA_IDXSET_INVALID) {
299 if ((source = pa_idxset_get_by_index(core->sources, index)))
300 pa_discover_add_source(u, source);
302 pa_log_debug("source.%u is gone", index);
303 u->state.source = PA_IDXSET_INVALID;
307 pa_assert_se((prof = card->active_profile));
309 pa_log_debug("bluetooth profile changed to '%s' on card '%s'",
310 prof->name, card->name);
312 if (!prof->n_sinks && !prof->n_sources) {
313 /* switched off but not unloaded yet */
314 need_routing = FALSE;
316 PA_HASHMAP_FOREACH(node, discover->nodes.byname, state) {
317 if (node->implement == mir_device &&
318 node->pacard.index == card->index)
320 if (node->type != mir_bluetooth_a2dp &&
321 node->type != mir_bluetooth_sco)
323 if (node->available) {
324 node->available = FALSE;
332 schedule_deferred_routing(u);
336 pa_log_debug("alsa profile changed to '%s' on card '%s'",
337 card->active_profile->name, card->name);
339 stamp = pa_utils_get_stamp();
341 handle_alsa_card(u, card);
343 PA_HASHMAP_FOREACH(node, discover->nodes.byname, state) {
344 if (node->implement == mir_device &&
345 node->pacard.index == card->index &&
348 destroy_node(u, node);
355 void pa_discover_port_available_changed(struct userdata *u,
356 pa_device_port *port)
367 pa_direction_t direction;
372 pa_assert_se((core = u->core));
374 switch (port->available) {
375 case PA_PORT_AVAILABLE_NO: state = "not available"; break;
376 case PA_PORT_AVAILABLE_YES: state = "available"; break;
377 default: state = "unknown"; break;
380 pa_log_debug("port '%s' availabilty changed to %s. Updating",
388 while ((node = pa_utils_get_node_from_port(u, port, &iter))) {
390 available = get_bluetooth_port_availability(node, port);
391 route |= update_node_availability(u, node, available);
392 direction |= (node->direction == mir_input) ? PA_DIRECTION_INPUT : PA_DIRECTION_OUTPUT;
396 set_bluetooth_profile(u, port->card, direction);
398 switch (port->available) {
399 case PA_PORT_AVAILABLE_NO: available = FALSE; break;
400 case PA_PORT_AVAILABLE_YES: available = TRUE; break;
401 default: /* do nothing */ return;
404 if (port->direction == PA_DIRECTION_OUTPUT) {
405 PA_IDXSET_FOREACH(sink, core->sinks, idx) {
407 if (port == pa_hashmap_get(sink->ports, port->name)) {
408 pa_log_debug(" sink '%s'", sink->name);
409 route |= update_node_availability_by_device(
418 if (port->direction == PA_DIRECTION_INPUT) {
419 PA_IDXSET_FOREACH(source, core->sources, idx) {
421 if (port == pa_hashmap_get(source->ports, port->name)) {
422 pa_log_debug(" source '%s'", source->name);
423 route |= update_node_availability_by_device(
434 mir_router_make_routing(u);
437 void pa_discover_add_sink(struct userdata *u, pa_sink *sink, pa_bool_t route)
439 static pa_nodeset_resdef def_resdef = {0, {0, 0}};
442 pa_discover *discover;
449 const char *loopback_role;
451 pa_nodeset_resdef *resdef;
453 pa_nodeset_resdef rdbuf;
460 pa_assert_se((core = u->core));
461 pa_assert_se((discover = u->discover));
463 module = sink->module;
465 if ((card = sink->card)) {
466 if (!(key = node_key(u, mir_output,sink,ACTIVE_PORT, kbf,sizeof(kbf))))
468 if (!(node = pa_discover_find_node_by_key(u, key))) {
469 if (u->state.profile)
470 pa_log_debug("can't find node for sink (key '%s')", key);
472 u->state.sink = sink->index;
475 pa_log_debug("node for '%s' found (key %s). Updating with sink data",
476 node->paname, node->key);
477 node->paidx = sink->index;
478 node->available = TRUE;
479 pa_discover_add_node_to_ptr_hash(u, sink, node);
481 if ((loopback_role = pa_classify_loopback_stream(node))) {
482 if (!(ns = pa_utils_get_null_source(u))) {
483 pa_log("Can't load loopback module: no initial null source");
487 map = pa_nodeset_get_map_by_role(u, loopback_role);
488 make_rset = (map && map->resdef);
489 resdef = make_rset ? map->resdef : &def_resdef;
491 node->loop = pa_loopback_create(u->loopback, core,
492 PA_LOOPBACK_SINK, node->index,
493 ns->index, sink->index,
497 resdef->flags.audio);
499 mir_node_print(node, nbf, sizeof(nbf));
500 pa_log_debug("updated node:\n%s", nbf);
503 pa_murphyif_create_resource_set(u, node, resdef);
509 if (type != mir_bluetooth_a2dp && type != mir_bluetooth_sco)
510 mir_router_make_routing(u);
512 if (!u->state.profile)
513 schedule_deferred_routing(u);
517 else if (!module || !pa_streq(module->name, "module-combine-sink")) {
518 memset(&data, 0, sizeof(data));
519 data.key = pa_xstrdup(sink->name);
520 data.direction = mir_output;
521 data.implement = mir_device;
522 data.channels = sink->channel_map.channels;
523 data.available = TRUE;
524 data.paidx = sink->index;
526 if (sink == pa_utils_get_null_sink(u)) {
527 data.visible = FALSE;
528 data.type = mir_null;
529 data.amname = pa_xstrdup("Silent");
530 data.amid = AM_ID_INVALID;
531 data.paname = pa_xstrdup(sink->name);
533 else if (pa_classify_node_by_property(&data, sink->proplist)) {
534 data.privacy = mir_public;
536 data.amname = pa_xstrdup(mir_node_type_str(data.type));
537 data.amid = AM_ID_INVALID;
538 data.paname = pa_xstrdup(sink->name);
541 pa_xfree(data.key); /* for now */
542 pa_log_info("currently we do not support statically loaded "
543 "sinks without " PA_PROP_NODE_TYPE " property");
547 create_node(u, &data, NULL);
552 void pa_discover_remove_sink(struct userdata *u, pa_sink *sink)
554 pa_discover *discover;
561 pa_assert_se((discover = u->discover));
563 name = pa_utils_get_sink_name(sink);
565 if (!(node = pa_hashmap_get(discover->nodes.byptr, sink)))
566 pa_log_debug("can't find node for sink (name '%s')", name);
568 pa_log_debug("node found for '%s'. Reseting sink data", name);
569 pa_murphyif_destroy_resource_set(u, node);
570 schedule_source_cleanup(u, node);
571 node->paidx = PA_IDXSET_INVALID;
572 pa_hashmap_remove(discover->nodes.byptr, sink);
577 if (type != mir_bluetooth_a2dp && type != mir_bluetooth_sco)
578 node->available = FALSE;
580 if (!u->state.profile)
581 schedule_deferred_routing(u);
585 pa_log_info("currently we do not support statically loaded sinks");
591 void pa_discover_add_source(struct userdata *u, pa_source *source)
593 static pa_nodeset_resdef def_resdef = {0, {0, 0}};
596 pa_discover *discover;
602 const char *loopback_role;
604 pa_nodeset_resdef *resdef;
612 pa_assert_se((core = u->core));
613 pa_assert_se((discover = u->discover));
615 if ((card = source->card)) {
616 if (!(key = node_key(u,mir_input,source,ACTIVE_PORT,kbf,sizeof(kbf))))
618 if (!(node = pa_discover_find_node_by_key(u, key))) {
619 if (u->state.profile)
620 pa_log_debug("can't find node for source (key '%s')", key);
622 u->state.source = source->index;
625 pa_log_debug("node for '%s' found. Updating with source data",
627 node->paidx = source->index;
628 node->available = TRUE;
629 pa_discover_add_node_to_ptr_hash(u, source, node);
630 if ((loopback_role = pa_classify_loopback_stream(node))) {
631 if (!(ns = pa_utils_get_null_sink(u))) {
632 pa_log("Can't load loopback module: no initial null sink");
636 map = pa_nodeset_get_map_by_role(u, loopback_role);
637 make_rset = (map && map->resdef);
638 resdef = make_rset ? map->resdef : &def_resdef;
640 node->loop = pa_loopback_create(u->loopback, core,
641 PA_LOOPBACK_SOURCE, node->index,
642 source->index, ns->index,
646 resdef->flags.audio);
648 sink_index = pa_loopback_get_sink_index(core, node->loop);
649 node->mux = pa_multiplex_find_by_sink(u->multiplex,sink_index);
652 mir_node_print(node, nbf, sizeof(nbf));
653 pa_log_debug("updated node:\n%s", nbf);
656 pa_murphyif_create_resource_set(u, node, resdef);
660 memset(&data, 0, sizeof(data));
661 data.key = pa_xstrdup(source->name);
662 data.direction = mir_input;
663 data.implement = mir_device;
664 data.channels = source->channel_map.channels;
665 data.available = TRUE;
667 if (source == pa_utils_get_null_source(u)) {
668 data.visible = FALSE;
669 data.type = mir_null;
670 data.amname = pa_xstrdup("Silent");
671 data.amid = AM_ID_INVALID;
672 data.paname = pa_xstrdup(source->name);
673 data.paidx = source->index;
675 else if (pa_classify_node_by_property(&data, source->proplist)) {
677 data.amname = pa_xstrdup(mir_node_type_str(data.type));
678 data.amid = AM_ID_INVALID;
679 data.paname = pa_xstrdup(source->name);
682 pa_xfree(data.key); /* for now */
683 pa_log_info("currently we do not support statically loaded "
684 "sources without " PA_PROP_NODE_TYPE " property");
688 create_node(u, &data, NULL);
693 void pa_discover_remove_source(struct userdata *u, pa_source *source)
695 pa_discover *discover;
702 pa_assert_se((discover = u->discover));
704 name = pa_utils_get_source_name(source);
706 if (!(node = pa_hashmap_get(discover->nodes.byptr, source)))
707 pa_log_debug("can't find node for source (name '%s')", name);
709 pa_log_debug("node found. Reseting source data");
710 pa_murphyif_destroy_resource_set(u, node);
711 schedule_source_cleanup(u, node);
712 node->paidx = PA_IDXSET_INVALID;
713 pa_hashmap_remove(discover->nodes.byptr, source);
718 if (type != mir_bluetooth_sco)
719 node->available = FALSE;
721 if (!u->state.profile)
722 schedule_deferred_routing(u);
726 pa_log_info("currently we do not support statically "
733 void pa_discover_register_sink_input(struct userdata *u, pa_sink_input *sinp)
736 pa_discover *discover;
747 pa_nodeset_resdef *resdef;
751 pa_assert_se((core = u->core));
752 pa_assert_se((discover = u->discover));
753 pa_assert_se((pl = sinp->proplist));
755 if ((media = pa_proplist_gets(sinp->proplist, PA_PROP_MEDIA_NAME))) {
756 if (!strncmp(media, combine_pattern, sizeof(combine_pattern)-1)) {
757 pa_log_debug("Seems to be a combine stream. Nothing to do ...");
760 if (!strncmp(media, loopback_outpatrn, sizeof(loopback_outpatrn)-1)) {
761 pa_log_debug("Seems to be a loopback stream. Nothing to do ...");
766 name = pa_utils_get_sink_input_name(sinp);
768 pa_log_debug("registering input stream '%s'", name);
770 if (!(type = pa_classify_guess_stream_node_type(u, pl, &resdef))) {
771 pa_log_debug("cant find stream class for '%s'. "
772 "Leaving it alone", name);
776 pa_utils_set_stream_routing_properties(pl, type, NULL);
778 snprintf(key, sizeof(key), "stream_input.%d", sinp->index);
780 memset(&data, 0, sizeof(data));
782 data.direction = mir_input;
783 data.implement = mir_stream;
784 data.channels = sinp->channel_map.channels;
786 data.zone = pa_utils_get_zone(sinp->proplist);
788 data.available = TRUE;
790 data.amdescr = (char *)pa_proplist_gets(pl, PA_PROP_MEDIA_NAME);
791 data.amid = AM_ID_INVALID;
793 data.paidx = sinp->index;
794 data.rsetid = (char *)pa_proplist_gets(pl, PA_PROP_RESOURCE_SET_ID);
797 * here we can't guess whether the application requested an explicit
798 * route by sepcifying the target sink @ stream creation time.
800 * the brute force solution: we make a default route for this stream
801 * possibly overwiriting the orginal app request :(
803 /* this will set data.mux */
804 role = pa_proplist_gets(sinp->proplist, PA_PROP_MEDIA_ROLE);
805 sink = make_output_prerouting(u, &data, &sinp->channel_map, role, &target);
807 node = create_node(u, &data, NULL);
809 pa_discover_add_node_to_ptr_hash(u, sinp, node);
811 if (sink && target) {
812 pa_log_debug("move stream to sink %u (%s)", sink->index, sink->name);
814 if (pa_sink_input_move_to(sinp, sink, FALSE) < 0)
815 pa_log("failed to route '%s' => '%s'",node->amname,target->amname);
817 pa_log_debug("register route '%s' => '%s'",
818 node->amname, target->amname);
819 /* FIXME: and actually do it ... */
824 void pa_discover_preroute_sink_input(struct userdata *u,
825 pa_sink_input_new_data *data)
830 pa_discover *discover;
831 pa_multiplex *multiplex;
840 pa_nodeset_resdef *resdef;
844 pa_assert_se((core = u->core));
845 pa_assert_se((discover = u->discover));
846 pa_assert_se((multiplex = u->multiplex));
847 pa_assert_se((pl = data->proplist));
849 mnam = (m = data->module) ? m->name : "";
851 if (pa_streq(mnam, "module-combine-sink")) {
852 type = mir_node_type_unknown;
854 if (!(mux = pa_multiplex_find_by_module(multiplex, m)) ||
855 !(sink = pa_idxset_get_by_index(core->sinks, mux->sink_index)) ||
856 !(sinp = pa_idxset_first(sink->inputs, NULL)) ||
857 !(type = pa_utils_get_stream_class(sinp->proplist)))
859 pa_log_debug("can't figure out the type of multiplex stream");
862 pa_utils_set_stream_routing_properties(data->proplist, type, NULL);
866 if (pa_streq(mnam, "module-loopback")) {
867 if (!(node = pa_utils_get_node_from_data(u, mir_input, data))) {
868 pa_log_debug("can't find loopback node for sink-input");
872 if (node->direction == mir_output) {
873 pa_log_debug("refuse to preroute loopback sink-input "
874 "(current route: sink %u @ %p)", data->sink ?
875 data->sink->index : PA_IDXSET_INVALID,data->sink);
881 type = pa_classify_guess_stream_node_type(u, pl, NULL);
884 type = pa_classify_guess_stream_node_type(u, pl, &resdef);
886 pa_utils_set_resource_properties(pl, resdef);
888 if (pa_stream_state_start_corked(u, data, resdef)) {
889 pa_log_debug("start corked");
892 pa_utils_set_stream_routing_properties(pl, type, data->sink);
896 memset(&fake, 0, sizeof(fake));
897 fake.direction = mir_input;
898 fake.implement = mir_stream;
899 fake.channels = data->channel_map.channels;
901 fake.zone = pa_utils_get_zone(data->proplist);
903 fake.available = TRUE;
904 fake.amname = "<preroute sink-input>";
906 role = pa_proplist_gets(data->proplist, PA_PROP_MEDIA_ROLE);
907 sink = make_output_prerouting(u, &fake, &data->channel_map, role,NULL);
911 if (fake.mux && !(data->flags & PA_SINK_INPUT_START_CORKED)) {
912 data->flags |= PA_SINK_INPUT_START_CORKED;
913 schedule_stream_uncorking(u, sink);
917 if (pa_sink_input_new_data_set_sink(data, sink, FALSE))
918 pa_log_debug("set sink %u for new sink-input", sink->index);
920 pa_log("can't set sink %u for new sink-input", sink->index);
926 void pa_discover_add_sink_input(struct userdata *u, pa_sink_input *sinp)
930 pa_sink_input *csinp;
932 pa_discover *discover;
933 pa_multiplex *multiplex;
943 pa_nodeset_resdef *resdef;
944 pa_nodeset_resdef rdbuf;
948 pa_assert_se((core = u->core));
949 pa_assert_se((discover = u->discover));
950 pa_assert_se((multiplex = u->multiplex));
951 pa_assert_se((pl = sinp->proplist));
955 if (!(media = pa_proplist_gets(sinp->proplist, PA_PROP_MEDIA_NAME)))
958 if (!strncmp(media, combine_pattern, sizeof(combine_pattern)-1)) {
959 if (!pa_utils_stream_has_default_route(sinp->proplist) ||
960 !(mux = pa_multiplex_find_by_module(multiplex, sinp->module)) ||
961 mux->defstream_index != PA_IDXSET_INVALID)
963 pa_log_debug("New stream is a combine stream. Nothing to do ...");
966 pa_log_debug("New stream is a combine stream. Setting as default");
967 mux->defstream_index = sinp->index;
968 mir_router_make_routing(u);
971 } else if (!strncmp(media, loopback_outpatrn,sizeof(loopback_outpatrn)-1)){
972 pa_log_debug("New stream is a loopback output stream");
974 if ((node = pa_utils_get_node_from_stream(u, mir_input, sinp))) {
975 if (node->direction == mir_input)
976 pa_log_debug("loopback stream node '%s' found", node->amname);
978 pa_log_debug("ignoring it");
983 pa_log_debug("can't find node for the loopback stream");
990 name = pa_utils_get_sink_input_name(sinp);
992 pa_log_debug("dealing with new input stream '%s'", name);
994 if ((type = get_stream_routing_class(pl)))
995 resdef = pa_utils_get_resource_properties(pl, &rdbuf);
997 if (!(type = pa_classify_guess_stream_node_type(u, pl, &resdef))) {
998 pa_log_debug("cant find stream class for '%s'. "
999 "Leaving it alone", name);
1003 pa_utils_set_stream_routing_properties(pl, type, NULL);
1005 /* if needed, make some post-routing here */
1008 /* we need to add this to main hashmap as that is used for loop
1009 through on all nodes. */
1010 snprintf(key, sizeof(key), "stream_input.%d", sinp->index);
1012 memset(&data, 0, sizeof(data));
1014 data.direction = mir_input;
1015 data.implement = mir_stream;
1016 data.channels = sinp->channel_map.channels;
1018 data.zone = pa_utils_get_zone(pl);
1019 data.visible = TRUE;
1020 data.available = TRUE;
1022 data.amdescr = (char *)pa_proplist_gets(pl, PA_PROP_MEDIA_NAME);
1023 data.amid = AM_ID_INVALID;
1025 data.paidx = sinp->index;
1026 data.mux = pa_multiplex_find_by_sink(u->multiplex,
1028 data.rsetid = (char *)pa_proplist_gets(pl, PA_PROP_RESOURCE_SET_ID);
1029 node = create_node(u, &data, &created);
1034 pa_log("%s: confused with stream. '%s' did exists",
1035 __FILE__, node->amname);
1040 pa_murphyif_add_node(u, node);
1042 pa_murphyif_create_resource_set(u, node, resdef);
1044 pa_discover_add_node_to_ptr_hash(u, sinp, node);
1049 csinp = pa_idxset_get_by_index(core->sink_inputs,
1050 data.mux->defstream_index);
1051 s = csinp ? csinp->sink : NULL;
1056 pa_log_debug("routing target candidate is %u (%s)", s->index, s->name);
1058 if (!s || !(snod = pa_hashmap_get(discover->nodes.byptr, s)))
1059 pa_log_debug("can't figure out where this stream is routed");
1061 pa_log_debug("register route '%s' => '%s'",
1062 node->amname, snod->amname);
1063 /* FIXME: and actually do it ... */
1065 pa_fader_apply_volume_limits(u, pa_utils_get_stamp());
1070 void pa_discover_remove_sink_input(struct userdata *u, pa_sink_input *sinp)
1072 pa_discover *discover;
1076 pa_bool_t had_properties;
1080 pa_assert_se((discover = u->discover));
1082 name = pa_utils_get_sink_input_name(sinp);
1084 pa_log_debug("sink-input '%s' going to be destroyed", name);
1086 had_properties = pa_utils_unset_stream_routing_properties(sinp->proplist);
1088 if (!(node = pa_discover_remove_node_from_ptr_hash(u, sinp))) {
1089 if (!pa_multiplex_sink_input_remove(u->multiplex, sinp))
1090 pa_log_debug("nothing to do for sink-input (name '%s')", name);
1093 pa_log_debug("node found for '%s'. After clearing routes "
1094 "it will be destroyed", name);
1096 if (!(sinknod = pa_hashmap_get(discover->nodes.byptr, sinp->sink)))
1097 pa_log_debug("can't figure out where this stream is routed");
1099 pa_log_debug("clear route '%s' => '%s'",
1100 node->amname, sinknod->amname);
1102 /* FIXME: and actually do it ... */
1106 destroy_node(u, node);
1109 if (node || had_properties)
1110 mir_router_make_routing(u);
1114 void pa_discover_register_source_output(struct userdata *u,
1115 pa_source_output *sout)
1118 pa_discover *discover;
1129 pa_nodeset_resdef *resdef;
1133 pa_assert_se((core = u->core));
1134 pa_assert_se((discover = u->discover));
1135 pa_assert_se((pl = sout->proplist));
1137 if ((media = pa_proplist_gets(sout->proplist, PA_PROP_MEDIA_NAME))) {
1138 if (!strncmp(media, loopback_inpatrn, sizeof(loopback_inpatrn)-1)) {
1139 pa_log_debug("Seems to be a loopback stream. Nothing to do ...");
1144 name = pa_utils_get_source_output_name(sout);
1146 pa_log_debug("registering output stream '%s'", name);
1148 if (!(type = pa_classify_guess_stream_node_type(u, pl, &resdef))) {
1149 pa_log_debug("cant find stream class for '%s'. "
1150 "Leaving it alone", name);
1154 pa_utils_set_stream_routing_properties(pl, type, NULL);
1156 snprintf(key, sizeof(key), "stream_output.%d", sout->index);
1158 memset(&data, 0, sizeof(data));
1160 data.direction = mir_output;
1161 data.implement = mir_stream;
1162 data.channels = sout->channel_map.channels;
1164 data.zone = pa_utils_get_zone(sout->proplist);
1165 data.visible = TRUE;
1166 data.available = TRUE;
1168 data.amdescr = (char *)pa_proplist_gets(pl, PA_PROP_MEDIA_NAME);
1169 data.amid = AM_ID_INVALID;
1171 data.paidx = sout->index;
1172 data.rsetid = (char *)pa_proplist_gets(pl, PA_PROP_RESOURCE_SET_ID);
1175 * here we can't guess whether the application requested an explicit
1176 * route by sepcifying the target source @ stream creation time.
1178 * the brute force solution: we make a default route for this stream
1179 * possibly overwiriting the orginal app request :(
1181 role = pa_proplist_gets(sout->proplist, PA_PROP_MEDIA_ROLE);
1182 source = make_input_prerouting(u, &data, role, &target);
1184 node = create_node(u, &data, NULL);
1186 pa_discover_add_node_to_ptr_hash(u, sout, node);
1188 if (source && target) {
1189 pa_log_debug("move stream to source %u (%s)",
1190 source->index, source->name);
1192 if (pa_source_output_move_to(sout, source, FALSE) < 0)
1193 pa_log("failed to route '%s' => '%s'",node->amname,target->amname);
1195 pa_log_debug("register route '%s' => '%s'",
1196 node->amname, target->amname);
1197 /* FIXME: and actually do it ... */
1202 void pa_discover_preroute_source_output(struct userdata *u,
1203 pa_source_output_new_data *data)
1208 pa_discover *discover;
1215 pa_nodeset_resdef *resdef;
1219 pa_assert_se((core = u->core));
1220 pa_assert_se((discover = u->discover));
1221 pa_assert_se((pl = data->proplist));
1223 mnam = (m = data->module) ? m->name : "";
1225 if (pa_streq(mnam, "module-loopback")) {
1226 if (!(node = pa_utils_get_node_from_data(u, mir_output, data))) {
1227 pa_log_debug("can't find loopback node for source-output");
1231 if (node->direction == mir_input) {
1232 pa_log_debug("refuse to preroute loopback source-output "
1233 "(current route: source %u @ %p)", data->source ?
1234 data->source->index : PA_IDXSET_INVALID,data->source);
1238 data->source = NULL;
1240 type = pa_classify_guess_stream_node_type(u, pl, NULL);
1243 type = pa_classify_guess_stream_node_type(u, pl, &resdef);
1245 pa_utils_set_resource_properties(pl, resdef);
1248 pa_utils_set_stream_routing_properties(pl, type, data->source);
1250 if (!data->source) {
1251 memset(&fake, 0, sizeof(fake));
1252 fake.direction = mir_output;
1253 fake.implement = mir_stream;
1254 fake.channels = data->channel_map.channels;
1256 fake.zone = pa_utils_get_zone(data->proplist);
1257 fake.visible = TRUE;
1258 fake.available = TRUE;
1259 fake.amname = "<preroute source-output>";
1261 role = pa_proplist_gets(data->proplist, PA_PROP_MEDIA_ROLE);
1262 source = make_input_prerouting(u, &fake, role, NULL);
1265 if (pa_source_output_new_data_set_source(data, source, FALSE)) {
1266 pa_log_debug("set source %u for new source-output",
1270 pa_log("can't set source %u for new source-output",
1278 void pa_discover_add_source_output(struct userdata *u, pa_source_output *sout)
1283 pa_discover *discover;
1292 pa_nodeset_resdef *resdef;
1293 pa_nodeset_resdef rdbuf;
1297 pa_assert_se((core = u->core));
1298 pa_assert_se((discover = u->discover));
1299 pa_assert_se((pl = sout->proplist));
1303 if (!(media = pa_proplist_gets(sout->proplist, PA_PROP_MEDIA_NAME)))
1304 media = "<unknown>";
1306 if (!strncmp(media, loopback_inpatrn, sizeof(loopback_inpatrn)-1)) {
1307 pa_log_debug("New stream is a loopback input stream");
1309 if ((node = pa_utils_get_node_from_stream(u, mir_output, sout))) {
1310 if (node->direction == mir_output)
1311 pa_log_debug("loopback stream node '%s' found", node->amname);
1313 pa_log_debug("ignoring it");
1318 pa_log_debug("can't find node for the loopback stream");
1323 name = pa_utils_get_source_output_name(sout);
1325 pa_log_debug("dealing with new output stream '%s'", name);
1327 if ((type = get_stream_routing_class(pl)))
1328 resdef = pa_utils_get_resource_properties(pl, &rdbuf);
1330 if (!(type = pa_classify_guess_stream_node_type(u, pl, &resdef))) {
1331 pa_log_debug("cant find stream class for '%s'. "
1332 "Leaving it alone", name);
1336 pa_utils_set_stream_routing_properties(pl, type, NULL);
1338 /* if needed, make some post-routing here */
1341 /* we need to add this to main hashmap as that is used for loop
1342 through on all nodes. */
1343 snprintf(key, sizeof(key), "stream_output.%d", sout->index);
1345 memset(&data, 0, sizeof(data));
1347 data.direction = mir_output;
1348 data.implement = mir_stream;
1349 data.channels = sout->channel_map.channels;
1351 data.zone = pa_utils_get_zone(pl);
1352 data.visible = TRUE;
1353 data.available = TRUE;
1355 data.amdescr = (char *)pa_proplist_gets(pl, PA_PROP_MEDIA_NAME);
1356 data.amid = AM_ID_INVALID;
1358 data.paidx = sout->index;
1359 data.rsetid = (char *)pa_proplist_gets(pl, PA_PROP_RESOURCE_SET_ID);
1361 node = create_node(u, &data, &created);
1366 pa_log("%s: confused with stream. '%s' did exists",
1367 __FILE__, node->amname);
1372 pa_murphyif_add_node(u, node);
1374 pa_murphyif_create_resource_set(u, node, resdef);
1376 pa_discover_add_node_to_ptr_hash(u, sout, node);
1379 if ((s = sout->source))
1380 pa_log_debug("routing target candidate is %u (%s)", s->index, s->name);
1382 if (!s || !(snod = pa_hashmap_get(discover->nodes.byptr, s)))
1383 pa_log_debug("can't figure out where this stream is routed");
1385 pa_log_debug("register route '%s' => '%s'",
1386 snod->amname, node->amname);
1387 /* FIXME: and actually do it ... */
1393 void pa_discover_remove_source_output(struct userdata *u,
1394 pa_source_output *sout)
1396 pa_discover *discover;
1403 pa_assert_se((discover = u->discover));
1405 name = pa_utils_get_source_output_name(sout);
1407 pa_log_debug("source-output '%s' going to be destroyed", name);
1409 if (!(node = pa_discover_remove_node_from_ptr_hash(u, sout)))
1410 pa_log_debug("can't find node for source-output (name '%s')", name);
1412 pa_log_debug("node found for '%s'. After clearing routes "
1413 "it will be destroyed", name);
1415 if (!(srcnod = pa_hashmap_get(discover->nodes.byptr, sout->source)))
1416 pa_log_debug("can't figure out where this stream is routed");
1418 pa_log_debug("clear route '%s' => '%s'",
1419 node->amname, srcnod->amname);
1421 /* FIXME: and actually do it ... */
1425 destroy_node(u, node);
1427 mir_router_make_routing(u);
1432 mir_node *pa_discover_find_node_by_key(struct userdata *u, const char *key)
1434 pa_discover *discover;
1438 pa_assert_se((discover = u->discover));
1441 node = pa_hashmap_get(discover->nodes.byname, key);
1448 mir_node *pa_discover_find_node_by_ptr(struct userdata *u, void *ptr)
1450 pa_discover *discover;
1454 pa_assert_se((discover = u->discover));
1457 node = pa_hashmap_get(discover->nodes.byptr, ptr);
1464 void pa_discover_add_node_to_ptr_hash(struct userdata *u,
1468 pa_discover *discover;
1473 pa_assert_se((discover = u->discover));
1475 pa_hashmap_put(discover->nodes.byptr, ptr, node);
1478 mir_node *pa_discover_remove_node_from_ptr_hash(struct userdata *u, void *ptr)
1480 pa_discover *discover;
1484 pa_assert_se((discover = u->discover));
1486 return pa_hashmap_remove(discover->nodes.byptr, ptr);
1490 static void handle_alsa_card(struct userdata *u, pa_card *card)
1497 memset(&data, 0, sizeof(data));
1498 data.zone = pa_utils_get_zone(card->proplist);
1499 data.visible = TRUE;
1500 data.amid = AM_ID_INVALID;
1501 data.implement = mir_device;
1502 data.paidx = PA_IDXSET_INVALID;
1503 data.stamp = pa_utils_get_stamp();
1505 cnam = pa_utils_get_card_name(card);
1506 udd = pa_proplist_gets(card->proplist, "module-udev-detect.discovered");
1508 if (udd && pa_streq(udd, "1")) {
1509 /* udev loaded alsa card */
1510 if (!strncmp(cnam, "alsa_card.", 10)) {
1512 handle_udev_loaded_card(u, card, &data, cid);
1517 /* statically loaded pci or usb card */
1520 pa_log_debug("ignoring unrecognized pci card '%s'", cnam);
1525 static void handle_bluetooth_card(struct userdata *u, pa_card *card)
1527 pa_discover *discover;
1528 pa_card_profile *prof;
1536 char paname[MAX_NAME_LENGTH+1];
1537 char amname[MAX_NAME_LENGTH+1];
1538 char key[MAX_NAME_LENGTH+1];
1540 pa_assert_se((discover = u->discover));
1542 cdescr = pa_proplist_gets(card->proplist, PA_PROP_DEVICE_DESCRIPTION);
1545 memset(paname, 0, sizeof(paname));
1546 memset(amname, 0, sizeof(amname));
1547 memset(key , 0, sizeof(key) );
1549 memset(&data, 0, sizeof(data));
1551 data.visible = TRUE;
1552 data.amid = AM_ID_INVALID;
1553 data.implement = mir_device;
1554 data.paidx = PA_IDXSET_INVALID;
1555 data.paname = paname;
1556 data.amname = amname;
1557 data.amdescr = (char *)cdescr;
1558 data.pacard.index = card->index;
1559 data.stamp = pa_utils_get_stamp();
1561 cnam = pa_utils_get_card_name(card);
1563 if (!strncmp(cnam, "bluez_card.", 11)) {
1566 pa_assert(card->ports);
1568 cd = mir_constrain_create(u, "profile", mir_constrain_profile, cnam);
1570 PA_HASHMAP_FOREACH(prof, card->profiles, cstate) {
1571 data.available = FALSE;
1572 data.pacard.profile = prof->name;
1574 if (prof->n_sinks > 0) {
1575 data.direction = mir_output;
1576 data.channels = prof->max_sink_channels;
1577 data.amname = amname;
1579 snprintf(paname, sizeof(paname), "bluez_sink.%s", cid);
1580 snprintf(key, sizeof(key), "%s@%s", paname, prof->name);
1581 pa_classify_node_by_card(&data, card, prof, NULL);
1582 node = create_node(u, &data, NULL);
1583 mir_constrain_add_node(u, cd, node);
1586 if (prof->n_sources > 0) {
1587 data.direction = mir_input;
1588 data.channels = prof->max_source_channels;
1589 data.amname = amname;
1591 snprintf(paname, sizeof(paname), "bluez_source.%s", cid);
1592 snprintf(key, sizeof(key), "%s@%s", paname, prof->name);
1593 pa_classify_node_by_card(&data, card, prof, NULL);
1594 node = create_node(u, &data, NULL);
1595 mir_constrain_add_node(u, cd, node);
1599 if (!(prof = card->active_profile))
1600 pa_log("card '%s' has no active profile", card->name);
1602 pa_log_debug("card '%s' default profile '%s'",
1603 card->name, prof->name);
1606 schedule_card_check(u, card);
1612 static void handle_bluetooth_card(struct userdata *u, pa_card *card)
1614 pa_discover *discover;
1615 pa_card_profile *prof;
1616 pa_device_port *port;
1623 void *state0, *state1;
1624 char paname[MAX_NAME_LENGTH+1];
1625 char amname[MAX_NAME_LENGTH+1];
1626 char key[MAX_NAME_LENGTH+1];
1631 pa_assert_se((discover = u->discover));
1633 cdescr = pa_proplist_gets(card->proplist, PA_PROP_DEVICE_DESCRIPTION);
1636 memset(paname, 0, sizeof(paname));
1637 memset(amname, 0, sizeof(amname));
1638 memset(key , 0, sizeof(key) );
1640 memset(&data, 0, sizeof(data));
1642 data.zone = pa_utils_get_zone(card->proplist);
1643 data.visible = TRUE;
1644 data.amid = AM_ID_INVALID;
1645 data.implement = mir_device;
1646 data.paidx = PA_IDXSET_INVALID;
1647 data.paname = paname;
1648 data.amname = amname;
1649 data.amdescr = (char *)cdescr;
1650 data.pacard.index = card->index;
1651 data.stamp = pa_utils_get_stamp();
1653 cnam = pa_utils_get_card_name(card);
1655 if (!strncmp(cnam, "bluez_card.", 11)) {
1658 pa_assert(card->ports);
1660 cd = mir_constrain_create(u, "profile", mir_constrain_profile, cnam);
1662 PA_HASHMAP_FOREACH(port, card->ports, state0) {
1663 pa_assert(port->profiles);
1666 input = output = TRUE;
1667 len = strlen(port->name);
1668 if (len >= 6 && !strcmp("-input", port->name + (len-6)))
1670 else if (len >= 7 && !strcmp("-output", port->name + (len-7)))
1674 PA_HASHMAP_FOREACH(prof, port->profiles, state1) {
1675 data.pacard.profile = prof->name;
1676 data.available = get_bluetooth_port_availability(&data, port);
1678 if (output && prof->n_sinks > 0) {
1679 data.direction = mir_output;
1680 data.channels = prof->max_sink_channels;
1681 data.amname = amname;
1683 snprintf(paname, sizeof(paname), "bluez_sink.%s", cid);
1684 snprintf(key, sizeof(key), "%s@%s.%s", paname, port->name, prof->name);
1685 pa_classify_node_by_card(&data, card, prof, NULL);
1686 node = create_node(u, &data, NULL);
1687 mir_constrain_add_node(u, cd, node);
1688 pa_utils_set_port_properties(port, node);
1691 if (input && prof->n_sources > 0) {
1692 data.direction = mir_input;
1693 data.channels = prof->max_source_channels;
1694 data.amname = amname;
1696 snprintf(paname, sizeof(paname), "bluez_source.%s", cid);
1697 snprintf(key, sizeof(key), "%s@%s.%s", paname, port->name, prof->name);
1698 pa_classify_node_by_card(&data, card, prof, NULL);
1699 node = create_node(u, &data, NULL);
1700 mir_constrain_add_node(u, cd, node);
1701 pa_utils_set_port_properties(port, node);
1706 if (!(prof = card->active_profile))
1707 pa_log("card '%s' has no active profile", card->name);
1709 pa_log_debug("card '%s' default profile '%s'",
1710 card->name, prof->name);
1713 schedule_card_check(u, card);
1717 static pa_bool_t get_bluetooth_port_availability(mir_node *node,
1718 pa_device_port *port)
1720 pa_bool_t available = FALSE;
1726 if ((prof = node->pacard.profile)) {
1727 if (!strcmp(prof, "hfgw") ||
1728 !strcmp(prof, "a2dp_source") ||
1729 !strcmp(prof, "a2dp_sink"))
1730 available = (port->available != PA_PORT_AVAILABLE_NO);
1738 static void handle_udev_loaded_card(struct userdata *u, pa_card *card,
1739 mir_node *data, char *cardid)
1741 pa_discover *discover;
1742 pa_card_profile *prof;
1743 pa_card_profile *active;
1745 const char *alsanam;
1747 char *sinks[MAX_CARD_TARGET+1];
1748 char *sources[MAX_CARD_TARGET+1];
1749 char buf[MAX_NAME_LENGTH+1];
1750 char paname[MAX_NAME_LENGTH+1];
1751 char amname[MAX_NAME_LENGTH+1];
1755 pa_assert(card->profiles);
1756 pa_assert_se((discover = u->discover));
1758 alsanam = pa_proplist_gets(card->proplist, "alsa.card_name");
1760 memset(amname, 0, sizeof(amname));
1762 data->paname = paname;
1763 data->amname = amname;
1764 data->amdescr = (char *)alsanam;
1766 data->pacard.index = card->index;
1768 active = card->active_profile;
1770 PA_HASHMAP_FOREACH(prof, card->profiles, state) {
1771 /* filtering: deal with selected profiles if requested so */
1772 if (discover->selected && (!active || (active && prof != active)))
1775 /* filtering: skip the 'off' profiles */
1776 if (!prof->n_sinks && !prof->n_sources)
1779 /* filtering: consider sinks with suitable amount channels */
1780 if (prof->n_sinks &&
1781 (prof->max_sink_channels < discover->chmin ||
1782 prof->max_sink_channels > discover->chmax ))
1785 /* filtering: consider sources with suitable amount channels */
1786 if (prof->n_sources &&
1787 (prof->max_source_channels < discover->chmin ||
1788 prof->max_source_channels > discover->chmax ))
1791 data->pacard.profile = prof->name;
1793 parse_profile_name(prof, sinks,sources, buf,sizeof(buf));
1795 data->direction = mir_output;
1796 data->channels = prof->max_sink_channels;
1797 for (i = 0; (sid = sinks[i]); i++) {
1798 snprintf(paname, sizeof(paname), "alsa_output.%s.%s", cardid, sid);
1799 handle_card_ports(u, data, card, prof);
1802 data->direction = mir_input;
1803 data->channels = prof->max_source_channels;
1804 for (i = 0; (sid = sources[i]); i++) {
1805 snprintf(paname, sizeof(paname), "alsa_input.%s.%s", cardid, sid);
1806 handle_card_ports(u, data, card, prof);
1812 static void handle_card_ports(struct userdata *u, mir_node *data,
1813 pa_card *card, pa_card_profile *prof)
1815 mir_node *node = NULL;
1816 pa_bool_t have_ports = FALSE;
1817 mir_constr_def *cd = NULL;
1818 char *amname = data->amname;
1819 pa_device_port *port;
1822 char key[MAX_NAME_LENGTH+1];
1830 PA_HASHMAP_FOREACH(port, card->ports, state) {
1832 * if this port did not belong to any profile
1833 * (ie. prof->profiles == NULL) we assume that this port
1834 * does works with all the profiles
1836 if (port->profiles && pa_hashmap_get(port->profiles, prof->name) &&
1837 ((port->direction == PA_DIRECTION_INPUT && data->direction == mir_input)||
1838 (port->direction == PA_DIRECTION_OUTPUT && data->direction == mir_output)))
1843 snprintf(key, sizeof(key), "%s@%s", data->paname, port->name);
1846 data->available = (port->available != PA_PORT_AVAILABLE_NO);
1848 data->amname = amname;
1849 data->paport = port->name;
1851 pa_classify_node_by_card(data, card, prof, port);
1853 node = create_node(u, data, &created);
1856 node->stamp = data->stamp;
1858 cd = mir_constrain_create(u, "port", mir_constrain_port,
1860 mir_constrain_add_node(u, cd, node);
1867 data->key = data->paname;
1868 data->available = TRUE;
1870 pa_classify_node_by_card(data, card, prof, NULL);
1872 node = create_node(u, data, &created);
1875 node->stamp = data->stamp;
1878 data->amname = amname;
1883 static mir_node *create_node(struct userdata *u, mir_node *data,
1884 pa_bool_t *created_ret)
1886 pa_discover *discover;
1893 pa_assert(data->key);
1894 pa_assert(data->paname);
1895 pa_assert_se((discover = u->discover));
1897 if ((node = pa_hashmap_get(discover->nodes.byname, data->key)))
1902 node = mir_node_create(u, data);
1903 pa_hashmap_put(discover->nodes.byname, node->key, node);
1905 mir_node_print(node, buf, sizeof(buf));
1906 pa_log_debug("new node:\n%s", buf);
1908 if (node->available)
1909 pa_audiomgr_register_node(u, node);
1913 *created_ret = created;
1918 static void destroy_node(struct userdata *u, mir_node *node)
1920 pa_discover *discover;
1924 pa_assert_se((discover = u->discover));
1927 removed = pa_hashmap_remove(discover->nodes.byname, node->key);
1929 if (node != removed) {
1931 pa_log("%s: confused with data structures: key mismatch. "
1932 " attempted to destroy '%s'; actually destroyed '%s'",
1933 __FILE__, node->key, removed->key);
1935 pa_log("%s: confused with data structures: node '%s' "
1936 "is not in the hash table", __FILE__, node->key);
1940 pa_log_debug("destroying node: %s / '%s'", node->key, node->amname);
1942 if (node->implement == mir_stream) {
1943 if (node->direction == mir_input) {
1945 pa_log_debug("removing multiplexer");
1950 pa_audiomgr_unregister_node(u, node);
1952 extapi_signal_node_change(u);
1954 mir_constrain_remove_node(u, node);
1956 pa_loopback_destroy(u->loopback, u->core, node->loop);
1957 pa_multiplex_destroy(u->multiplex, u->core, node->mux);
1959 mir_node_destroy(u, node);
1963 static pa_bool_t update_node_availability(struct userdata *u,
1965 pa_bool_t available)
1970 if ((!available && node->available) ||
1971 ( available && !node->available) )
1973 node->available = available;
1976 pa_audiomgr_register_node(u, node);
1978 pa_audiomgr_unregister_node(u, node);
1980 extapi_signal_node_change(u);
1982 return TRUE; /* routing needed */
1988 static pa_bool_t update_node_availability_by_device(struct userdata *u,
1989 mir_direction direction,
1991 pa_device_port *port,
1992 pa_bool_t available)
2001 pa_assert(direction == mir_input || direction == mir_output);
2003 if ((key = node_key(u, direction, data, port, buf, sizeof(buf)))) {
2004 if (!(node = pa_discover_find_node_by_key(u, key)))
2005 pa_log_debug(" can't find node (key '%s')", key);
2007 pa_log_debug(" node for '%s' found (key %s)",
2008 node->paname, node->key);
2010 return update_node_availability(u, node, available);
2014 return FALSE; /* no routing needed */
2017 static char *get_name(char **string_ptr, int offs)
2019 char c, *name, *end;
2021 name = *string_ptr + offs;
2023 for (end = name; (c = *end); end++) {
2035 static void parse_profile_name(pa_card_profile *prof,
2045 pa_assert(prof->name);
2047 strncpy(buf, prof->name, buflen);
2048 buf[buflen-1] = '\0';
2050 memset(sinks, 0, sizeof(char *) * (MAX_CARD_TARGET+1));
2051 memset(sources, 0, sizeof(char *) * (MAX_CARD_TARGET+1));
2054 if (!strncmp(p, "output:", 7)) {
2055 if (i >= MAX_CARD_TARGET) {
2056 pa_log_debug("number of outputs exeeds the maximum %d in "
2057 "profile name '%s'", MAX_CARD_TARGET, prof->name);
2060 sinks[i++] = get_name(&p, 7);
2062 else if (!strncmp(p, "input:", 6)) {
2063 if (j >= MAX_CARD_TARGET) {
2064 pa_log_debug("number of inputs exeeds the maximum %d in "
2065 "profile name '%s'", MAX_CARD_TARGET, prof->name);
2068 sources[j++] = get_name(&p, 6);
2071 pa_log("%s: failed to parse profile name '%s'",
2072 __FILE__, prof->name);
2079 static char *node_key(struct userdata *u, mir_direction direction,
2080 void *data, pa_device_port *port, char *buf, size_t len)
2083 pa_card_profile *profile;
2087 pa_bool_t bluetooth;
2090 const char *profile_name;
2096 pa_assert(direction == mir_input || direction == mir_output);
2098 if (direction == mir_output) {
2099 pa_sink *sink = data;
2101 name = pa_utils_get_sink_name(sink);
2104 port = sink->active_port;
2107 pa_source *source = data;
2109 name = pa_utils_get_source_name(source);
2110 card = source->card;
2112 port = source->active_port;
2118 pa_assert_se((profile = card->active_profile));
2120 if (!u->state.profile)
2121 profile_name = profile->name;
2123 pa_log_debug("state.profile is not null. '%s' supresses '%s'",
2124 u->state.profile, profile->name);
2125 profile_name = u->state.profile;
2129 if (!(bus = pa_utils_get_card_bus(card))) {
2130 pa_log_debug("ignoring %s '%s' due to lack of '%s' property "
2131 "on its card", type, name, PA_PROP_DEVICE_BUS);
2135 pci = pa_streq(bus, "pci");
2136 usb = pa_streq(bus, "usb");
2137 bluetooth = pa_streq(bus, "bluetooth");
2139 if (!pci && !usb && !bluetooth) {
2140 pa_log_debug("ignoring %s '%s' due to unsupported bus type '%s' "
2141 "of its card", type, name, bus);
2150 snprintf(buf, len, "%s@%s.%s", name, port->name, profile_name);
2158 snprintf(buf, len, "%s@%s", name, port->name);
2165 static pa_sink *make_output_prerouting(struct userdata *u,
2167 pa_channel_map *chmap,
2168 const char *media_role,
2169 mir_node **target_ret)
2173 pa_sink *sink = NULL;
2178 pa_assert_se((core = u->core));
2181 target = mir_router_make_prerouting(u, data);
2184 pa_log("there is no default route for the stream '%s'", data->amname);
2185 else if (target->paidx == PA_IDXSET_INVALID)
2186 pa_log("can't route to default '%s': no sink", target->amname);
2188 if (!(sink = pa_idxset_get_by_index(core->sinks, target->paidx)))
2189 pa_log("no route to default '%s': sink is gone", target->amname);
2191 if (pa_classify_multiplex_stream(data)) {
2192 data->mux = pa_multiplex_create(u->multiplex, core,
2193 sink->index, chmap, NULL,
2194 media_role, data->type);
2196 sink = pa_idxset_get_by_index(core->sinks,
2197 data->mux->sink_index);
2205 *target_ret = target;
2211 static pa_source *make_input_prerouting(struct userdata *u,
2213 const char *media_role,
2214 mir_node **target_ret)
2218 pa_source *source = NULL;
2222 pa_assert_se((core = u->core));
2224 target = mir_router_make_prerouting(u, data);
2227 pa_log("there is no default route for the stream '%s'", data->amname);
2228 else if (target->paidx == PA_IDXSET_INVALID)
2229 pa_log("can't route to default '%s': no source", target->amname);
2231 if (!(source = pa_idxset_get_by_index(core->sources, target->paidx)))
2232 pa_log("no route to default '%s': source is gone",target->amname);
2236 *target_ret = target;
2241 static mir_node_type get_stream_routing_class(pa_proplist *pl)
2247 t = pa_utils_get_stream_class(pl);
2249 if (t >= mir_application_class_begin && t < mir_application_class_end)
2252 return mir_node_type_unknown;
2256 static void set_bluetooth_profile(struct userdata *u,
2258 pa_direction_t direction)
2261 pa_device_port *port;
2262 pa_card_profile *prof, *make_active;
2263 void *state0, *state1;
2264 pa_bool_t port_available;
2265 pa_bool_t switch_off;
2270 pa_assert_se((core = u->core));
2276 pa_log_debug("which profile to make active:");
2278 PA_HASHMAP_FOREACH(prof, card->profiles, state0) {
2279 if (!prof->n_sinks && !prof->n_sources) {
2281 pa_log_debug(" considering %s", prof->name);
2287 port_available = FALSE;
2289 PA_HASHMAP_FOREACH(port, card->ports, state1) {
2290 if ((direction & port->direction) &&
2291 pa_hashmap_get(port->profiles, prof->name))
2293 port_available = (port->available != PA_PORT_AVAILABLE_NO);
2298 if (!port_available)
2299 pa_log_debug(" ruling out %s (port not available)", prof->name);
2300 else if (prof->available != PA_AVAILABLE_YES)
2301 pa_log_debug(" ruling out %s (profile not available)", prof->name);
2305 if (((direction & PA_DIRECTION_INPUT) && prof->n_sources > 0) ||
2306 ((direction & PA_DIRECTION_OUTPUT) && prof->n_sinks > 0) ) {
2307 if (make_active && prof->priority < make_active->priority)
2308 pa_log_debug(" ruling out %s (low priority)", prof->name);
2310 pa_log_debug(" considering %s", prof->name);
2315 pa_log_debug(" ruling out %s (direction)", prof->name);
2322 pa_log_debug("No suitable profile found. Frustrated and do nothing");
2324 if (make_active == card->active_profile)
2325 pa_log_debug("Profile %s already set. Do nothing", make_active->name);
2327 if (switch_off && nport) {
2328 pa_log_debug("Do not switch to %s as active ports are existing "
2329 "to the other direction", make_active->name);
2332 pa_log_debug("Set profile %s", make_active->name);
2334 if (pa_card_set_profile(card, make_active->name, FALSE) < 0) {
2335 pa_log_debug("Failed to change profile to %s",
2343 static void deferred_routing_cb(pa_mainloop_api *m, void *d)
2345 struct userdata *u = d;
2351 pa_log_debug("deferred routing starts");
2353 mir_router_make_routing(u);
2357 static void schedule_deferred_routing(struct userdata *u)
2362 pa_assert_se((core = u->core));
2364 pa_log_debug("scheduling deferred routing");
2366 pa_mainloop_api_once(core->mainloop, deferred_routing_cb, u);
2370 static void card_check_cb(pa_mainloop_api *m, void *d)
2372 card_check_t *cc = d;
2378 int n_sink, n_source;
2384 pa_assert((u = cc->u));
2385 pa_assert((core = u->core));
2387 pa_log_debug("card check starts");
2389 if (!(card = pa_idxset_get_by_index(core->cards, cc->index)))
2390 pa_log_debug("card %u is gone", cc->index);
2392 n_sink = n_source = 0;
2394 PA_IDXSET_FOREACH(sink, core->sinks, idx) {
2395 if ((sink->card) && sink->card->index == card->index)
2399 PA_IDXSET_FOREACH(source, core->sources, idx) {
2400 if ((source->card) && source->card->index == card->index)
2404 if (n_sink || n_source) {
2405 pa_log_debug("found %u sinks and %u sources belonging to "
2406 "'%s' card", n_sink, n_source, card->name);
2407 pa_log_debug("nothing to do");
2410 pa_log_debug("card '%s' has no sinks/sources. Do routing ...",
2412 mir_router_make_routing(u);
2420 static void schedule_card_check(struct userdata *u, pa_card *card)
2427 pa_assert_se((core = u->core));
2429 pa_log_debug("scheduling card check");
2431 cc = pa_xnew0(card_check_t, 1);
2433 cc->index = card->index;
2435 pa_mainloop_api_once(core->mainloop, card_check_cb, cc);
2439 static void source_cleanup_cb(pa_mainloop_api *m, void *d)
2441 source_cleanup_t *sc = d;
2448 pa_assert((u = sc->u));
2449 pa_assert((core = u->core));
2451 pa_log_debug("source cleanup starts");
2453 pa_loopback_destroy(u->loopback, u->core, sc->loop);
2454 pa_multiplex_destroy(u->multiplex, u->core, sc->mux);
2456 pa_log_debug("source cleanup ends");
2462 static void schedule_source_cleanup(struct userdata *u, mir_node *node)
2465 source_cleanup_t *sc;
2469 pa_assert_se((core = u->core));
2471 pa_log_debug("scheduling source cleanup");
2473 sc = pa_xnew0(source_cleanup_t, 1);
2475 sc->mux = node->mux;
2476 sc->loop = node->loop;
2481 pa_mainloop_api_once(core->mainloop, source_cleanup_cb, sc);
2486 static void stream_uncork_cb(pa_mainloop_api *m, void *d)
2488 stream_uncork_t *suc = d;
2492 pa_sink_input *sinp;
2498 pa_assert((u = suc->u));
2499 pa_assert((core = u->core));
2501 pa_log_debug("start uncorking stream");
2503 if (!(sink = pa_idxset_get_by_index(core->sinks, suc->index))) {
2504 pa_log_debug("sink.%d gone", suc->index);
2508 if (!(sinp = pa_idxset_first(core->sink_inputs, &index))) {
2509 pa_log_debug("sink_input is gone");
2513 pa_sink_input_cork(sinp, FALSE);
2515 pa_log_debug("stream.%u uncorked", sinp->index);
2523 static void schedule_stream_uncorking(struct userdata *u, pa_sink *sink)
2526 stream_uncork_t *suc;
2530 pa_assert_se((core = u->core));
2532 pa_log_debug("scheduling stream uncorking");
2534 suc = pa_xnew0(stream_uncork_t, 1);
2536 suc->index = sink->index;
2538 pa_mainloop_api_once(core->mainloop, stream_uncork_cb, suc);
2545 * indent-tabs-mode: nil