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 bool 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 *, bool *);
100 static void destroy_node(struct userdata *, mir_node *);
101 static bool update_node_availability(struct userdata *, mir_node *,
103 static bool 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 *);
121 static char *get_stream_amname(mir_node_type, char *, pa_proplist *);
123 static void set_bluetooth_profile(struct userdata *, pa_card *, pa_direction_t);
126 static void schedule_deferred_routing(struct userdata *);
127 static void schedule_card_check(struct userdata *, pa_card *);
128 static void schedule_source_cleanup(struct userdata *, mir_node *);
130 static void schedule_stream_uncorking(struct userdata *, pa_sink *);
133 static void pa_hashmap_node_free(void *node, void *u)
135 mir_node_destroy(u, node);
139 struct pa_discover *pa_discover_init(struct userdata *u)
141 pa_discover *discover = pa_xnew0(pa_discover, 1);
145 discover->selected = true;
147 discover->nodes.byname = pa_hashmap_new(pa_idxset_string_hash_func,
148 pa_idxset_string_compare_func);
149 discover->nodes.byptr = pa_hashmap_new(pa_idxset_trivial_hash_func,
150 pa_idxset_trivial_compare_func);
154 void pa_discover_done(struct userdata *u)
156 pa_discover *discover;
160 if (u && (discover = u->discover)) {
161 PA_HASHMAP_FOREACH(node, discover->nodes.byname, state) {
162 mir_node_destroy(u, node);
164 pa_hashmap_free(discover->nodes.byname);
165 pa_hashmap_free(discover->nodes.byptr);
171 void pa_discover_domain_up(struct userdata *u)
173 pa_discover *discover;
178 pa_assert_se((discover = u->discover));
180 PA_HASHMAP_FOREACH(node, discover->nodes.byname, state) {
181 node->amid = AM_ID_INVALID;
183 if ((node->visible && node->available) ||
184 (node->type == mir_gateway_sink ||
185 node->type == mir_gateway_source)) {
186 pa_audiomgr_register_node(u, node);
187 extapi_signal_node_change(u);
192 void pa_discover_domain_down(struct userdata *u)
196 void pa_discover_add_card(struct userdata *u, pa_card *card)
203 if (!(bus = pa_utils_get_card_bus(card))) {
204 pa_log_debug("ignoring card '%s' due to lack of '%s' property",
205 pa_utils_get_card_name(card), PA_PROP_DEVICE_BUS);
209 if (pa_streq(bus, "pci") || pa_streq(bus, "usb") || pa_streq(bus, "platform")) {
210 handle_alsa_card(u, card);
213 else if (pa_streq(bus, "bluetooth")) {
214 handle_bluetooth_card(u, card);
218 pa_log_debug("ignoring card '%s' due to unsupported bus type '%s'",
219 pa_utils_get_card_name(card), bus);
222 void pa_discover_remove_card(struct userdata *u, pa_card *card)
225 pa_discover *discover;
231 pa_assert_se((discover = u->discover));
233 if (!(bus = pa_utils_get_card_bus(card)))
237 PA_HASHMAP_FOREACH(node, discover->nodes.byname, state) {
238 if (node->implement == mir_device &&
239 node->pacard.index == card->index)
241 if (pa_streq(bus, "pci") || pa_streq(bus, "usb") || pa_streq(bus, "platform"))
242 mir_constrain_destroy(u, node->paname);
244 destroy_node(u, node);
248 if (pa_streq(bus, "bluetooth"))
249 mir_constrain_destroy(u, card->name);
252 void pa_discover_profile_changed(struct userdata *u, pa_card *card)
255 pa_card_profile *prof;
258 pa_discover *discover;
272 pa_assert_se((core = u->core));
273 pa_assert_se((discover = u->discover));
276 if ((bus = pa_utils_get_card_bus(card)) == NULL) {
277 pa_log_debug("ignoring profile change on card '%s' due to lack of '%s'"
278 "property", pa_utils_get_card_name(card),
283 pci = pa_streq(bus, "pci");
284 usb = pa_streq(bus, "usb");
285 bluetooth = pa_streq(bus, "bluetooth");
286 platform = pa_streq(bus, "platform");
288 if (!pci && !usb && !bluetooth && !platform) {
289 pa_log_debug("ignoring profile change on card '%s' due to unsupported "
290 "bus type '%s'", pa_utils_get_card_name(card), bus);
291 u->state.sink = u->state.source = PA_IDXSET_INVALID;
295 if ((index = u->state.sink) != PA_IDXSET_INVALID) {
296 if ((sink = pa_idxset_get_by_index(core->sinks, index)))
297 pa_discover_add_sink(u, sink, true);
299 pa_log_debug("sink.%u is gone", index);
300 u->state.sink = PA_IDXSET_INVALID;
303 if ((index = u->state.source) != PA_IDXSET_INVALID) {
304 if ((source = pa_idxset_get_by_index(core->sources, index)))
305 pa_discover_add_source(u, source);
307 pa_log_debug("source.%u is gone", index);
308 u->state.source = PA_IDXSET_INVALID;
312 pa_assert_se((prof = card->active_profile));
314 pa_log_debug("bluetooth profile changed to '%s' on card '%s'",
315 prof->name, card->name);
317 if (!prof->n_sinks && !prof->n_sources) {
318 /* switched off but not unloaded yet */
319 need_routing = false;
321 PA_HASHMAP_FOREACH(node, discover->nodes.byname, state) {
322 if (node->implement == mir_device &&
323 node->pacard.index == card->index)
325 if (node->type != mir_bluetooth_a2dp &&
326 node->type != mir_bluetooth_sco)
328 if (node->available) {
329 node->available = false;
337 schedule_deferred_routing(u);
341 pa_log_debug("alsa profile changed to '%s' on card '%s'",
342 card->active_profile->name, card->name);
344 stamp = pa_utils_get_stamp();
346 handle_alsa_card(u, card);
348 PA_HASHMAP_FOREACH(node, discover->nodes.byname, state) {
349 if (node->implement == mir_device &&
350 node->pacard.index == card->index &&
353 destroy_node(u, node);
360 void pa_discover_port_available_changed(struct userdata *u,
361 pa_device_port *port)
372 pa_direction_t direction;
377 pa_assert_se((core = u->core));
379 switch (port->available) {
380 case PA_AVAILABLE_NO: state = "not available"; break;
381 case PA_AVAILABLE_YES: state = "available"; break;
382 default: state = "unknown"; break;
385 pa_log_debug("port '%s' availabilty changed to %s. Updating",
393 while ((node = pa_utils_get_node_from_port(u, port, &iter))) {
395 available = get_bluetooth_port_availability(node, port);
396 route |= update_node_availability(u, node, available);
397 direction |= (node->direction == mir_input) ? PA_DIRECTION_INPUT : PA_DIRECTION_OUTPUT;
401 set_bluetooth_profile(u, port->card, direction);
403 switch (port->available) {
404 case PA_AVAILABLE_NO: available = false; break;
405 case PA_AVAILABLE_YES: available = true; break;
406 default: /* do nothing */ return;
409 if (port->direction == PA_DIRECTION_OUTPUT) {
410 PA_IDXSET_FOREACH(sink, core->sinks, idx) {
412 if (port == pa_hashmap_get(sink->ports, port->name)) {
413 pa_log_debug(" sink '%s'", sink->name);
414 route |= update_node_availability_by_device(
423 if (port->direction == PA_DIRECTION_INPUT) {
424 PA_IDXSET_FOREACH(source, core->sources, idx) {
426 if (port == pa_hashmap_get(source->ports, port->name)) {
427 pa_log_debug(" source '%s'", source->name);
428 route |= update_node_availability_by_device(
439 mir_router_make_routing(u);
442 void pa_discover_add_sink(struct userdata *u, pa_sink *sink, bool route)
444 static pa_nodeset_resdef def_resdef = {0, {0, 0}};
447 pa_discover *discover;
454 const char *loopback_role;
456 pa_nodeset_resdef *resdef;
458 pa_nodeset_resdef rdbuf;
466 pa_assert_se((core = u->core));
467 pa_assert_se((discover = u->discover));
469 module = sink->module;
471 if ((card = sink->card)) {
472 if (!(key = node_key(u, mir_output,sink,ACTIVE_PORT, kbf,sizeof(kbf))))
474 if (!(node = pa_discover_find_node_by_key(u, key))) {
475 if (u->state.profile)
476 pa_log_debug("can't find node for sink (key '%s')", key);
478 u->state.sink = sink->index;
481 pa_log_debug("node for '%s' found (key %s). Updating with sink data",
482 node->paname, node->key);
483 node->paidx = sink->index;
484 node->available = true;
485 pa_discover_add_node_to_ptr_hash(u, sink, node);
487 if ((loopback_role = pa_classify_loopback_stream(node))) {
488 if (!(ns = pa_utils_get_null_source(u))) {
489 pa_log("Can't load loopback module: no initial null source");
493 map = pa_nodeset_get_map_by_role(u, loopback_role);
494 make_rset = (map && map->resdef);
495 resdef = make_rset ? map->resdef : &def_resdef;
497 node->loop = pa_loopback_create(u->loopback, core,
498 PA_LOOPBACK_SINK, node->index,
499 ns->index, sink->index,
503 resdef->flags.audio);
505 mir_node_print(node, nbf, sizeof(nbf));
506 pa_log_debug("updated node:\n%s", nbf);
509 pa_murphyif_create_resource_set(u, node, resdef);
515 if (type != mir_bluetooth_a2dp && type != mir_bluetooth_sco)
516 mir_router_make_routing(u);
518 if (!u->state.profile)
519 schedule_deferred_routing(u);
523 else if (!module || !pa_streq(module->name, "module-combine-sink")) {
526 memset(&data, 0, sizeof(data));
527 data.key = pa_xstrdup(sink->name);
528 data.direction = mir_output;
529 data.implement = mir_device;
530 data.channels = sink->channel_map.channels;
531 data.available = true;
532 data.paidx = sink->index;
534 if (sink == pa_utils_get_null_sink(u)) {
535 data.visible = false;
536 data.type = mir_null;
537 data.amname = pa_xstrdup("Silent");
538 data.amid = AM_ID_INVALID;
539 data.paname = pa_xstrdup(sink->name);
541 else if (pa_classify_node_by_property(&data, sink->proplist)) {
542 if (data.type == mir_gateway_sink) {
543 data.privacy = mir_private;
544 data.visible = false;
545 data.amname = pa_xstrdup(sink->name);
546 data.amid = AM_ID_INVALID;
547 data.paname = pa_xstrdup(sink->name);
550 data.privacy = mir_public;
552 data.amname = pa_xstrdup(mir_node_type_str(data.type));
553 data.amid = AM_ID_INVALID;
554 data.paname = pa_xstrdup(sink->name);
560 pa_xfree(data.key); /* for now */
561 pa_log_info("currently we do not support statically loaded "
562 "sinks without " PA_PROP_NODE_TYPE " property");
566 node = create_node(u, &data, NULL);
569 pa_discover_add_node_to_ptr_hash(u, sink, node);
574 void pa_discover_remove_sink(struct userdata *u, pa_sink *sink)
576 pa_discover *discover;
583 pa_assert_se((discover = u->discover));
585 name = pa_utils_get_sink_name(sink);
587 if (!(node = pa_hashmap_get(discover->nodes.byptr, sink)))
588 pa_log_debug("can't find node for sink (name '%s')", name);
590 pa_log_debug("node found for '%s'. Reseting sink data", name);
591 pa_murphyif_destroy_resource_set(u, node);
592 schedule_source_cleanup(u, node);
593 node->paidx = PA_IDXSET_INVALID;
594 pa_hashmap_remove(discover->nodes.byptr, sink);
599 if (type != mir_bluetooth_a2dp && type != mir_bluetooth_sco)
600 node->available = false;
602 if (!u->state.profile)
603 schedule_deferred_routing(u);
607 pa_log_info("currently we do not support statically loaded sinks");
613 void pa_discover_add_source(struct userdata *u, pa_source *source)
615 static pa_nodeset_resdef def_resdef = {0, {0, 0}};
618 pa_discover *discover;
624 const char *loopback_role;
626 pa_nodeset_resdef *resdef;
634 pa_assert_se((core = u->core));
635 pa_assert_se((discover = u->discover));
637 if ((card = source->card)) {
638 if (!(key = node_key(u,mir_input,source,ACTIVE_PORT,kbf,sizeof(kbf))))
640 if (!(node = pa_discover_find_node_by_key(u, key))) {
641 if (u->state.profile)
642 pa_log_debug("can't find node for source (key '%s')", key);
644 u->state.source = source->index;
647 pa_log_debug("node for '%s' found. Updating with source data",
649 node->paidx = source->index;
650 node->available = true;
651 pa_discover_add_node_to_ptr_hash(u, source, node);
652 if ((loopback_role = pa_classify_loopback_stream(node))) {
653 if (!(ns = pa_utils_get_null_sink(u))) {
654 pa_log("Can't load loopback module: no initial null sink");
658 map = pa_nodeset_get_map_by_role(u, loopback_role);
659 make_rset = (map && map->resdef);
660 resdef = make_rset ? map->resdef : &def_resdef;
662 node->loop = pa_loopback_create(u->loopback, core,
663 PA_LOOPBACK_SOURCE, node->index,
664 source->index, ns->index,
668 resdef->flags.audio);
670 sink_index = pa_loopback_get_sink_index(core, node->loop);
671 node->mux = pa_multiplex_find_by_sink(u->multiplex,sink_index);
674 mir_node_print(node, nbf, sizeof(nbf));
675 pa_log_debug("updated node:\n%s", nbf);
678 pa_murphyif_create_resource_set(u, node, resdef);
680 pa_fader_apply_volume_limits(u, node->stamp);
684 memset(&data, 0, sizeof(data));
685 data.key = pa_xstrdup(source->name);
686 data.direction = mir_input;
687 data.implement = mir_device;
688 data.channels = source->channel_map.channels;
689 data.available = true;
691 if (source == pa_utils_get_null_source(u)) {
692 data.visible = false;
693 data.type = mir_null;
694 data.amname = pa_xstrdup("Silent");
695 data.amid = AM_ID_INVALID;
696 data.paname = pa_xstrdup(source->name);
697 data.paidx = source->index;
699 else if (pa_classify_node_by_property(&data, source->proplist)) {
700 if (data.type == mir_gateway_source) {
701 data.privacy = mir_private;
702 data.visible = false;
703 data.amname = pa_xstrdup(source->name);
704 data.amid = AM_ID_INVALID;
705 data.paname = pa_xstrdup(source->name);
708 data.privacy = mir_public;
710 data.amname = pa_xstrdup(mir_node_type_str(data.type));
711 data.amid = AM_ID_INVALID;
712 data.paname = pa_xstrdup(source->name);
716 pa_xfree(data.key); /* for now */
717 pa_log_info("currently we do not support statically loaded "
718 "sources without " PA_PROP_NODE_TYPE " property");
722 create_node(u, &data, NULL);
726 void pa_discover_remove_source(struct userdata *u, pa_source *source)
728 pa_discover *discover;
735 pa_assert_se((discover = u->discover));
737 name = pa_utils_get_source_name(source);
739 if (!(node = pa_hashmap_get(discover->nodes.byptr, source)))
740 pa_log_debug("can't find node for source (name '%s')", name);
742 pa_log_debug("node found. Reseting source data");
743 pa_murphyif_destroy_resource_set(u, node);
744 schedule_source_cleanup(u, node);
745 node->paidx = PA_IDXSET_INVALID;
746 pa_hashmap_remove(discover->nodes.byptr, source);
751 if (type != mir_bluetooth_sco)
752 node->available = false;
754 if (!u->state.profile)
755 schedule_deferred_routing(u);
759 pa_log_info("currently we do not support statically "
766 void pa_discover_register_sink_input(struct userdata *u, pa_sink_input *sinp)
769 pa_discover *discover;
780 pa_nodeset_resdef *resdef;
785 pa_assert_se((core = u->core));
786 pa_assert_se((discover = u->discover));
787 pa_assert_se((pl = sinp->proplist));
789 if ((media = pa_proplist_gets(sinp->proplist, PA_PROP_MEDIA_NAME))) {
790 if (!strncmp(media, combine_pattern, sizeof(combine_pattern)-1)) {
791 pa_log_debug("Seems to be a combine stream. Nothing to do ...");
794 if (!strncmp(media, loopback_outpatrn, sizeof(loopback_outpatrn)-1)) {
795 pa_log_debug("Seems to be a loopback stream. Nothing to do ...");
800 name = pa_utils_get_sink_input_name(sinp);
802 pa_log_debug("registering input stream '%s'", name);
804 if (!(type = pa_classify_guess_stream_node_type(u, pl, &resdef))) {
805 pa_log_debug("cant find stream class for '%s'. "
806 "Leaving it alone", name);
810 pa_utils_set_stream_routing_properties(pl, type, NULL);
812 snprintf(key, sizeof(key), "stream_input.%d", sinp->index);
814 memset(&data, 0, sizeof(data));
816 data.direction = mir_input;
817 data.implement = mir_stream;
818 data.channels = sinp->channel_map.channels;
820 data.zone = pa_utils_get_zone(sinp->proplist);
822 data.available = true;
823 data.amname = get_stream_amname(type, name, pl);
824 data.amdescr = (char *)pa_proplist_gets(pl, PA_PROP_MEDIA_NAME);
825 data.amid = AM_ID_INVALID;
827 data.paidx = sinp->index;
828 data.rsetid = pa_utils_get_rsetid(pl, idbuf, sizeof(idbuf));
831 * here we can't guess whether the application requested an explicit
832 * route by sepcifying the target sink @ stream creation time.
834 * the brute force solution: we make a default route for this stream
835 * possibly overwiriting the orginal app request :(
837 /* this will set data.mux */
838 role = pa_proplist_gets(sinp->proplist, PA_PROP_MEDIA_ROLE);
839 sink = make_output_prerouting(u, &data, &sinp->channel_map, role, &target);
841 node = create_node(u, &data, NULL);
843 pa_discover_add_node_to_ptr_hash(u, sinp, node);
845 if (sink && target) {
846 pa_log_debug("move stream to sink %u (%s)", sink->index, sink->name);
848 if (pa_sink_input_move_to(sinp, sink, false) < 0)
849 pa_log("failed to route '%s' => '%s'",node->amname,target->amname);
851 pa_audiomgr_add_default_route(u, node, target);
855 bool pa_discover_preroute_sink_input(struct userdata *u,
856 pa_sink_input_new_data *data)
861 pa_discover *discover;
862 pa_multiplex *multiplex;
871 pa_nodeset_resdef *resdef;
877 pa_assert_se((core = u->core));
878 pa_assert_se((discover = u->discover));
879 pa_assert_se((multiplex = u->multiplex));
880 pa_assert_se((pl = data->proplist));
882 mnam = (m = data->module) ? m->name : "";
884 if (pa_streq(mnam, "module-combine-sink")) {
886 type = mir_node_type_unknown;
888 if (!(mux = pa_multiplex_find_by_module(multiplex, m)) ||
889 !(sink = pa_idxset_get_by_index(core->sinks, mux->sink_index)) ||
890 !(sinp = pa_idxset_first(sink->inputs, NULL)) ||
891 !(type = pa_utils_get_stream_class(sinp->proplist)))
893 pa_log_debug("can't figure out the type of multiplex stream");
896 pa_utils_set_stream_routing_properties(data->proplist, type, NULL);
900 loopback = pa_streq(mnam, "module-loopback");
904 if (!(node = pa_utils_get_node_from_data(u, mir_input, data))) {
905 pa_log_debug("can't find loopback node for sink-input");
909 if (node->direction == mir_output) {
910 pa_log_debug("refuse to preroute loopback sink-input "
911 "(current route: sink %u @ %p)", data->sink ?
912 data->sink->index : PA_IDXSET_INVALID,data->sink);
918 type = pa_classify_guess_stream_node_type(u, pl, NULL);
921 remap = pa_streq(mnam, "module-remap-sink");
922 type = pa_classify_guess_stream_node_type(u, pl, &resdef);
924 pa_utils_set_resource_properties(pl, resdef);
926 if (pa_stream_state_start_corked(u, data, resdef)) {
927 pa_log_debug("start corked");
931 pa_utils_set_stream_routing_properties(pl, type, data->sink);
934 memset(&fake, 0, sizeof(fake));
935 fake.direction = mir_input;
936 fake.implement = mir_stream;
940 fake.channels = data->channel_map.channels;
941 fake.zone = pa_utils_get_zone(data->proplist);
943 fake.available = true;
944 fake.amname = "<preroute sink-input>";
945 fake.amid = AM_ID_INVALID;
946 fake.paidx = PA_IDXSET_INVALID;
948 role = pa_proplist_gets(data->proplist, PA_PROP_MEDIA_ROLE);
949 sink = make_output_prerouting(u, &fake, &data->channel_map, role,NULL);
953 if (fake.mux && !(data->flags & PA_SINK_INPUT_START_CORKED)) {
954 data->flags |= PA_SINK_INPUT_START_CORKED;
955 schedule_stream_uncorking(u, sink);
959 if (pa_sink_input_new_data_set_sink(data, sink, false))
960 pa_log_debug("set sink %u for new sink-input", sink->index);
962 pa_log("can't set sink %u for new sink-input", sink->index);
963 /* copes wit NULL mux */
964 pa_multiplex_destroy(u->multiplex, core, fake.mux);
974 if (loopback && data->sink && data->sink->module) {
976 if (pa_streq(data->sink->module->name, "module-combine-sink"))
980 if (pa_classify_ramping_stream(&fake)) {
981 pa_log_debug("set sink-input ramp-muted");
982 data->flags |= PA_SINK_INPUT_START_RAMP_MUTED;
989 void pa_discover_add_sink_input(struct userdata *u, pa_sink_input *sinp)
993 pa_sink_input *csinp;
995 pa_discover *discover;
996 pa_multiplex *multiplex;
1006 pa_nodeset_resdef *resdef;
1007 pa_nodeset_resdef rdbuf;
1012 pa_assert_se((core = u->core));
1013 pa_assert_se((discover = u->discover));
1014 pa_assert_se((multiplex = u->multiplex));
1015 pa_assert_se((pl = sinp->proplist));
1019 if (!(media = pa_proplist_gets(sinp->proplist, PA_PROP_MEDIA_NAME)))
1020 media = "<unknown>";
1022 if (!strncmp(media, combine_pattern, sizeof(combine_pattern)-1)) {
1023 if (!pa_utils_stream_has_default_route(sinp->proplist) ||
1024 !(mux = pa_multiplex_find_by_module(multiplex, sinp->module)) ||
1025 mux->defstream_index != PA_IDXSET_INVALID)
1027 pa_log_debug("New stream is a combine stream. Nothing to do ...");
1030 pa_log_debug("New stream is a combine stream. Setting as default");
1031 mux->defstream_index = sinp->index;
1032 mir_router_make_routing(u);
1035 } else if (!strncmp(media, loopback_outpatrn,sizeof(loopback_outpatrn)-1)){
1036 pa_log_debug("New stream is a loopback output stream");
1038 if ((node = pa_utils_get_node_from_stream(u, mir_input, sinp))) {
1039 if (node->direction == mir_input)
1040 pa_log_debug("loopback stream node '%s' found", node->amname);
1042 pa_log_debug("ignoring it");
1047 pa_log_debug("can't find node for the loopback stream");
1054 name = pa_utils_get_sink_input_name(sinp);
1056 pa_log_debug("dealing with new input stream '%s'", name);
1058 if ((type = get_stream_routing_class(pl)))
1059 resdef = pa_utils_get_resource_properties(pl, &rdbuf);
1061 if (!(type = pa_classify_guess_stream_node_type(u, pl, &resdef))) {
1062 pa_log_debug("cant find stream class for '%s'. "
1063 "Leaving it alone", name);
1067 pa_utils_set_stream_routing_properties(pl, type, NULL);
1069 /* if needed, make some post-routing here */
1072 /* we need to add this to main hashmap as that is used for loop
1073 through on all nodes. */
1074 snprintf(key, sizeof(key), "stream_input.%d", sinp->index);
1076 memset(&data, 0, sizeof(data));
1078 data.direction = mir_input;
1079 data.implement = mir_stream;
1080 data.channels = sinp->channel_map.channels;
1082 data.zone = pa_utils_get_zone(pl);
1083 data.visible = true;
1084 data.available = true;
1085 data.amname = get_stream_amname(type, name, pl);
1086 data.amdescr = (char *)pa_proplist_gets(pl, PA_PROP_MEDIA_NAME);
1087 data.amid = AM_ID_INVALID;
1089 data.paidx = sinp->index;
1090 data.mux = pa_multiplex_find_by_sink(u->multiplex,
1092 data.rsetid = pa_utils_get_rsetid(pl, idbuf, sizeof(idbuf));
1093 node = create_node(u, &data, &created);
1098 pa_log("%s: confused with stream. '%s' did exists",
1099 __FILE__, node->amname);
1104 pa_murphyif_add_node(u, node);
1106 pa_murphyif_create_resource_set(u, node, resdef);
1108 pa_discover_add_node_to_ptr_hash(u, sinp, node);
1113 csinp = pa_idxset_get_by_index(core->sink_inputs,
1114 data.mux->defstream_index);
1115 s = csinp ? csinp->sink : NULL;
1117 if ((sinp->flags & PA_SINK_INPUT_START_RAMP_MUTED)) {
1118 pa_log_debug("ramp '%s' to 100%", media);
1119 pa_fader_ramp_volume(u, sinp, PA_VOLUME_NORM);
1125 pa_log_debug("routing target candidate is %u (%s)", s->index, s->name);
1127 if (!s || !(snod = pa_hashmap_get(discover->nodes.byptr, s)))
1128 pa_log_debug("can't figure out where this stream is routed");
1130 pa_log_debug("register route '%s' => '%s'",
1131 node->amname, snod->amname);
1133 if (pa_utils_stream_has_default_route(sinp->proplist))
1134 pa_audiomgr_add_default_route(u, node, snod);
1136 /* FIXME: register explicit routes */
1137 /* else pa_audiomgr_add/register_explicit_route() */
1140 pa_fader_apply_volume_limits(u, pa_utils_get_stamp());
1145 void pa_discover_remove_sink_input(struct userdata *u, pa_sink_input *sinp)
1147 pa_discover *discover;
1151 bool had_properties = false;
1155 pa_assert_se((discover = u->discover));
1157 name = pa_utils_get_sink_input_name(sinp);
1159 pa_log_debug("sink-input '%s' going to be destroyed", name);
1162 had_properties = pa_utils_unset_stream_routing_properties(sinp->proplist);
1164 if (!(node = pa_discover_remove_node_from_ptr_hash(u, sinp))) {
1165 if (!pa_multiplex_sink_input_remove(u->multiplex, sinp))
1166 pa_log_debug("nothing to do for sink-input (name '%s')", name);
1169 pa_log_debug("node found for '%s'. After clearing routes "
1170 "it will be destroyed", name);
1172 if (!(sinknod = pa_hashmap_get(discover->nodes.byptr, sinp->sink)))
1173 pa_log_debug("can't figure out where this stream is routed");
1175 pa_log_debug("clear route '%s' => '%s'",
1176 node->amname, sinknod->amname);
1178 /* FIXME: and actually do it ... */
1182 destroy_node(u, node);
1185 if (node || had_properties)
1186 mir_router_make_routing(u);
1190 void pa_discover_register_source_output(struct userdata *u,
1191 pa_source_output *sout)
1194 pa_discover *discover;
1205 pa_nodeset_resdef *resdef;
1210 pa_assert_se((core = u->core));
1211 pa_assert_se((discover = u->discover));
1212 pa_assert_se((pl = sout->proplist));
1214 if ((media = pa_proplist_gets(sout->proplist, PA_PROP_MEDIA_NAME))) {
1215 if (!strncmp(media, loopback_inpatrn, sizeof(loopback_inpatrn)-1)) {
1216 pa_log_debug("Seems to be a loopback stream. Nothing to do ...");
1221 name = pa_utils_get_source_output_name(sout);
1223 pa_log_debug("registering output stream '%s'", name);
1225 if (!(type = pa_classify_guess_stream_node_type(u, pl, &resdef))) {
1226 pa_log_debug("cant find stream class for '%s'. "
1227 "Leaving it alone", name);
1231 pa_utils_set_stream_routing_properties(pl, type, NULL);
1233 snprintf(key, sizeof(key), "stream_output.%d", sout->index);
1235 memset(&data, 0, sizeof(data));
1237 data.direction = mir_output;
1238 data.implement = mir_stream;
1239 data.channels = sout->channel_map.channels;
1241 data.zone = pa_utils_get_zone(sout->proplist);
1242 data.visible = true;
1243 data.available = true;
1245 data.amdescr = (char *)pa_proplist_gets(pl, PA_PROP_MEDIA_NAME);
1246 data.amid = AM_ID_INVALID;
1248 data.paidx = sout->index;
1249 data.rsetid = pa_utils_get_rsetid(pl, idbuf, sizeof(idbuf));
1252 * here we can't guess whether the application requested an explicit
1253 * route by sepcifying the target source @ stream creation time.
1255 * the brute force solution: we make a default route for this stream
1256 * possibly overwiriting the orginal app request :(
1258 role = pa_proplist_gets(sout->proplist, PA_PROP_MEDIA_ROLE);
1259 source = make_input_prerouting(u, &data, role, &target);
1261 node = create_node(u, &data, NULL);
1263 pa_discover_add_node_to_ptr_hash(u, sout, node);
1265 if (source && target) {
1266 pa_log_debug("move stream to source %u (%s)",
1267 source->index, source->name);
1269 if (pa_source_output_move_to(sout, source, false) < 0)
1270 pa_log("failed to route '%s' => '%s'",node->amname,target->amname);
1272 pa_log_debug("register route '%s' => '%s'",
1273 node->amname, target->amname);
1274 /* FIXME: and actually do it ... */
1279 bool pa_discover_preroute_source_output(struct userdata *u,
1280 pa_source_output_new_data *data)
1285 pa_discover *discover;
1292 pa_nodeset_resdef *resdef;
1296 pa_assert_se((core = u->core));
1297 pa_assert_se((discover = u->discover));
1298 pa_assert_se((pl = data->proplist));
1300 mnam = (m = data->module) ? m->name : "";
1302 if (pa_streq(mnam, "module-loopback")) {
1303 if (!(node = pa_utils_get_node_from_data(u, mir_output, data))) {
1304 pa_log_debug("can't find loopback node for source-output");
1308 if (node->direction == mir_input) {
1309 pa_log_debug("refuse to preroute loopback source-output "
1310 "(current route: source %u @ %p)", data->source ?
1311 data->source->index : PA_IDXSET_INVALID,data->source);
1315 data->source = NULL;
1317 type = pa_classify_guess_stream_node_type(u, pl, NULL);
1320 type = pa_classify_guess_stream_node_type(u, pl, &resdef);
1322 pa_utils_set_resource_properties(pl, resdef);
1325 pa_utils_set_stream_routing_properties(pl, type, data->source);
1327 if (!data->source) {
1328 memset(&fake, 0, sizeof(fake));
1329 fake.direction = mir_output;
1330 fake.implement = mir_stream;
1331 fake.channels = data->channel_map.channels;
1333 fake.zone = pa_utils_get_zone(data->proplist);
1334 fake.visible = true;
1335 fake.available = true;
1336 fake.amname = "<preroute source-output>";
1338 role = pa_proplist_gets(data->proplist, PA_PROP_MEDIA_ROLE);
1339 source = make_input_prerouting(u, &fake, role, NULL);
1342 if (pa_source_output_new_data_set_source(data, source, false)) {
1343 pa_log_debug("set source %u for new source-output",
1347 pa_log("can't set source %u for new source-output",
1357 void pa_discover_add_source_output(struct userdata *u, pa_source_output *sout)
1362 pa_discover *discover;
1371 pa_nodeset_resdef *resdef;
1372 pa_nodeset_resdef rdbuf;
1377 pa_assert_se((core = u->core));
1378 pa_assert_se((discover = u->discover));
1379 pa_assert_se((pl = sout->proplist));
1383 if (!(media = pa_proplist_gets(sout->proplist, PA_PROP_MEDIA_NAME)))
1384 media = "<unknown>";
1386 if (!strncmp(media, loopback_inpatrn, sizeof(loopback_inpatrn)-1)) {
1387 pa_log_debug("New stream is a loopback input stream");
1389 if ((node = pa_utils_get_node_from_stream(u, mir_output, sout))) {
1390 if (node->direction == mir_output)
1391 pa_log_debug("loopback stream node '%s' found", node->amname);
1393 pa_log_debug("ignoring it");
1398 pa_log_debug("can't find node for the loopback stream");
1403 name = pa_utils_get_source_output_name(sout);
1405 pa_log_debug("dealing with new output stream '%s'", name);
1407 if ((type = get_stream_routing_class(pl)))
1408 resdef = pa_utils_get_resource_properties(pl, &rdbuf);
1410 if (!(type = pa_classify_guess_stream_node_type(u, pl, &resdef))) {
1411 pa_log_debug("cant find stream class for '%s'. "
1412 "Leaving it alone", name);
1416 pa_utils_set_stream_routing_properties(pl, type, NULL);
1418 /* if needed, make some post-routing here */
1421 /* we need to add this to main hashmap as that is used for loop
1422 through on all nodes. */
1423 snprintf(key, sizeof(key), "stream_output.%d", sout->index);
1425 memset(&data, 0, sizeof(data));
1427 data.direction = mir_output;
1428 data.implement = mir_stream;
1429 data.channels = sout->channel_map.channels;
1431 data.zone = pa_utils_get_zone(pl);
1432 data.visible = true;
1433 data.available = true;
1435 data.amdescr = (char *)pa_proplist_gets(pl, PA_PROP_MEDIA_NAME);
1436 data.amid = AM_ID_INVALID;
1438 data.paidx = sout->index;
1439 data.rsetid = pa_utils_get_rsetid(pl, idbuf, sizeof(idbuf));
1441 node = create_node(u, &data, &created);
1446 pa_log("%s: confused with stream. '%s' did exists",
1447 __FILE__, node->amname);
1452 pa_murphyif_add_node(u, node);
1454 pa_murphyif_create_resource_set(u, node, resdef);
1456 pa_discover_add_node_to_ptr_hash(u, sout, node);
1459 if ((s = sout->source))
1460 pa_log_debug("routing target candidate is %u (%s)", s->index, s->name);
1462 if (!s || !(snod = pa_hashmap_get(discover->nodes.byptr, s)))
1463 pa_log_debug("can't figure out where this stream is routed");
1465 pa_log_debug("register route '%s' => '%s'",
1466 snod->amname, node->amname);
1467 pa_audiomgr_add_default_route(u, node, snod);
1472 void pa_discover_remove_source_output(struct userdata *u,
1473 pa_source_output *sout)
1475 pa_discover *discover;
1482 pa_assert_se((discover = u->discover));
1484 name = pa_utils_get_source_output_name(sout);
1486 pa_log_debug("source-output '%s' going to be destroyed", name);
1488 if (!(node = pa_discover_remove_node_from_ptr_hash(u, sout)))
1489 pa_log_debug("can't find node for source-output (name '%s')", name);
1491 pa_log_debug("node found for '%s'. After clearing routes "
1492 "it will be destroyed", name);
1494 if (!(srcnod = pa_hashmap_get(discover->nodes.byptr, sout->source)))
1495 pa_log_debug("can't figure out where this stream is routed");
1497 pa_log_debug("clear route '%s' => '%s'",
1498 node->amname, srcnod->amname);
1500 /* FIXME: and actually do it ... */
1504 destroy_node(u, node);
1506 mir_router_make_routing(u);
1511 mir_node *pa_discover_find_node_by_key(struct userdata *u, const char *key)
1513 pa_discover *discover;
1517 pa_assert_se((discover = u->discover));
1520 node = pa_hashmap_get(discover->nodes.byname, key);
1527 mir_node *pa_discover_find_node_by_ptr(struct userdata *u, void *ptr)
1529 pa_discover *discover;
1533 pa_assert_se((discover = u->discover));
1536 node = pa_hashmap_get(discover->nodes.byptr, ptr);
1543 void pa_discover_add_node_to_ptr_hash(struct userdata *u,
1547 pa_discover *discover;
1552 pa_assert_se((discover = u->discover));
1554 pa_hashmap_put(discover->nodes.byptr, ptr, node);
1557 mir_node *pa_discover_remove_node_from_ptr_hash(struct userdata *u, void *ptr)
1559 pa_discover *discover;
1563 pa_assert_se((discover = u->discover));
1565 return pa_hashmap_remove(discover->nodes.byptr, ptr);
1569 static void handle_alsa_card(struct userdata *u, pa_card *card)
1576 memset(&data, 0, sizeof(data));
1577 data.zone = pa_utils_get_zone(card->proplist);
1578 data.visible = true;
1579 data.amid = AM_ID_INVALID;
1580 data.implement = mir_device;
1581 data.paidx = PA_IDXSET_INVALID;
1582 data.stamp = pa_utils_get_stamp();
1584 cnam = pa_utils_get_card_name(card);
1585 udd = pa_proplist_gets(card->proplist, "module-udev-detect.discovered");
1587 if (udd && pa_streq(udd, "1")) {
1588 /* udev loaded alsa card */
1589 if (!strncmp(cnam, "alsa_card.", 10)) {
1591 handle_udev_loaded_card(u, card, &data, cid);
1596 /* statically loaded pci or usb card */
1599 pa_log_debug("ignoring unrecognized pci card '%s'", cnam);
1604 static void handle_bluetooth_card(struct userdata *u, pa_card *card)
1606 pa_discover *discover;
1607 pa_card_profile *prof;
1615 char paname[MAX_NAME_LENGTH+1];
1616 char amname[MAX_NAME_LENGTH+1];
1617 char key[MAX_NAME_LENGTH+1];
1619 pa_assert_se((discover = u->discover));
1621 cdescr = pa_proplist_gets(card->proplist, PA_PROP_DEVICE_DESCRIPTION);
1624 memset(paname, 0, sizeof(paname));
1625 memset(amname, 0, sizeof(amname));
1626 memset(key , 0, sizeof(key) );
1628 memset(&data, 0, sizeof(data));
1630 data.visible = true;
1631 data.amid = AM_ID_INVALID;
1632 data.implement = mir_device;
1633 data.paidx = PA_IDXSET_INVALID;
1634 data.paname = paname;
1635 data.amname = amname;
1636 data.amdescr = (char *)cdescr;
1637 data.pacard.index = card->index;
1638 data.stamp = pa_utils_get_stamp();
1640 cnam = pa_utils_get_card_name(card);
1642 if (!strncmp(cnam, "bluez_card.", 11)) {
1645 pa_assert(card->ports);
1647 cd = mir_constrain_create(u, "profile", mir_constrain_profile, cnam);
1649 PA_HASHMAP_FOREACH(prof, card->profiles, cstate) {
1650 data.available = false;
1651 data.pacard.profile = prof->name;
1653 if (prof->n_sinks > 0) {
1654 data.direction = mir_output;
1655 data.channels = prof->max_sink_channels;
1656 data.amname = amname;
1658 snprintf(paname, sizeof(paname), "bluez_sink.%s", cid);
1659 snprintf(key, sizeof(key), "%s@%s", paname, prof->name);
1660 pa_classify_node_by_card(&data, card, prof, NULL);
1661 node = create_node(u, &data, NULL);
1662 mir_constrain_add_node(u, cd, node);
1665 if (prof->n_sources > 0) {
1666 data.direction = mir_input;
1667 data.channels = prof->max_source_channels;
1668 data.amname = amname;
1670 snprintf(paname, sizeof(paname), "bluez_source.%s", cid);
1671 snprintf(key, sizeof(key), "%s@%s", paname, prof->name);
1672 pa_classify_node_by_card(&data, card, prof, NULL);
1673 node = create_node(u, &data, NULL);
1674 mir_constrain_add_node(u, cd, node);
1678 if (!(prof = card->active_profile))
1679 pa_log("card '%s' has no active profile", card->name);
1681 pa_log_debug("card '%s' default profile '%s'",
1682 card->name, prof->name);
1685 schedule_card_check(u, card);
1691 static void handle_bluetooth_card(struct userdata *u, pa_card *card)
1693 pa_discover *discover;
1694 pa_card_profile *prof;
1695 pa_device_port *port;
1702 void *state0, *state1;
1703 char paname[MAX_NAME_LENGTH+1];
1704 char amname[MAX_NAME_LENGTH+1];
1705 char key[MAX_NAME_LENGTH+1];
1710 pa_assert_se((discover = u->discover));
1712 cdescr = pa_proplist_gets(card->proplist, PA_PROP_DEVICE_DESCRIPTION);
1715 memset(paname, 0, sizeof(paname));
1716 memset(amname, 0, sizeof(amname));
1717 memset(key , 0, sizeof(key) );
1719 memset(&data, 0, sizeof(data));
1721 data.zone = pa_utils_get_zone(card->proplist);
1722 data.visible = true;
1723 data.amid = AM_ID_INVALID;
1724 data.implement = mir_device;
1725 data.paidx = PA_IDXSET_INVALID;
1726 data.paname = paname;
1727 data.amname = amname;
1728 data.amdescr = (char *)cdescr;
1729 data.pacard.index = card->index;
1730 data.stamp = pa_utils_get_stamp();
1732 cnam = pa_utils_get_card_name(card);
1734 if (!strncmp(cnam, "bluez_card.", 11)) {
1737 pa_assert(card->ports);
1739 cd = mir_constrain_create(u, "profile", mir_constrain_profile, cnam);
1741 PA_HASHMAP_FOREACH(port, card->ports, state0) {
1742 pa_assert(port->profiles);
1745 input = output = true;
1746 len = strlen(port->name);
1747 if (len >= 6 && !strcmp("-input", port->name + (len-6)))
1749 else if (len >= 7 && !strcmp("-output", port->name + (len-7)))
1753 PA_HASHMAP_FOREACH(prof, port->profiles, state1) {
1754 data.pacard.profile = prof->name;
1755 data.available = get_bluetooth_port_availability(&data, port);
1757 if (output && prof->n_sinks > 0) {
1758 data.direction = mir_output;
1759 data.channels = prof->max_sink_channels;
1760 data.amname = amname;
1762 snprintf(paname, sizeof(paname), "bluez_sink.%s", cid);
1763 snprintf(key, sizeof(key), "%s@%s.%s", paname, port->name, prof->name);
1764 pa_classify_node_by_card(&data, card, prof, NULL);
1765 node = create_node(u, &data, NULL);
1766 mir_constrain_add_node(u, cd, node);
1767 pa_utils_set_port_properties(port, node);
1770 if (input && prof->n_sources > 0) {
1771 data.direction = mir_input;
1772 data.channels = prof->max_source_channels;
1773 data.amname = amname;
1775 snprintf(paname, sizeof(paname), "bluez_source.%s", cid);
1776 snprintf(key, sizeof(key), "%s@%s.%s", paname, port->name, prof->name);
1777 pa_classify_node_by_card(&data, card, prof, NULL);
1778 node = create_node(u, &data, NULL);
1779 mir_constrain_add_node(u, cd, node);
1780 pa_utils_set_port_properties(port, node);
1785 if (!(prof = card->active_profile))
1786 pa_log("card '%s' has no active profile", card->name);
1788 pa_log_debug("card '%s' default profile '%s'",
1789 card->name, prof->name);
1792 schedule_card_check(u, card);
1796 static bool get_bluetooth_port_availability(mir_node *node,
1797 pa_device_port *port)
1799 bool available = false;
1805 if ((prof = node->pacard.profile)) {
1806 if (!strcmp(prof, "hfgw") ||
1807 !strcmp(prof, "a2dp_source") ||
1808 !strcmp(prof, "a2dp_sink"))
1809 available = (port->available != PA_AVAILABLE_NO);
1817 static void handle_udev_loaded_card(struct userdata *u, pa_card *card,
1818 mir_node *data, char *cardid)
1820 pa_discover *discover;
1821 pa_card_profile *prof;
1822 pa_card_profile *active;
1824 const char *alsanam;
1826 char *sinks[MAX_CARD_TARGET+1];
1827 char *sources[MAX_CARD_TARGET+1];
1828 char buf[MAX_NAME_LENGTH+1];
1829 char paname[MAX_NAME_LENGTH+1];
1830 char amname[MAX_NAME_LENGTH+1];
1834 pa_assert(card->profiles);
1835 pa_assert_se((discover = u->discover));
1837 alsanam = pa_proplist_gets(card->proplist, "alsa.card_name");
1839 memset(amname, 0, sizeof(amname));
1841 data->paname = paname;
1842 data->amname = amname;
1843 data->amdescr = (char *)alsanam;
1845 data->pacard.index = card->index;
1847 active = card->active_profile;
1849 PA_HASHMAP_FOREACH(prof, card->profiles, state) {
1850 /* filtering: deal with selected profiles if requested so */
1851 if (discover->selected && (!active || (active && prof != active)))
1854 /* filtering: skip the 'off' profiles */
1855 if (!prof->n_sinks && !prof->n_sources)
1858 /* filtering: consider sinks with suitable amount channels */
1859 if (prof->n_sinks &&
1860 (prof->max_sink_channels < discover->chmin ||
1861 prof->max_sink_channels > discover->chmax ))
1864 /* filtering: consider sources with suitable amount channels */
1865 if (prof->n_sources &&
1866 (prof->max_source_channels < discover->chmin ||
1867 prof->max_source_channels > discover->chmax ))
1870 data->pacard.profile = prof->name;
1872 parse_profile_name(prof, sinks,sources, buf,sizeof(buf));
1874 data->direction = mir_output;
1875 data->channels = prof->max_sink_channels;
1876 for (i = 0; (sid = sinks[i]); i++) {
1877 snprintf(paname, sizeof(paname), "alsa_output.%s.%s", cardid, sid);
1878 handle_card_ports(u, data, card, prof);
1881 data->direction = mir_input;
1882 data->channels = prof->max_source_channels;
1883 for (i = 0; (sid = sources[i]); i++) {
1884 snprintf(paname, sizeof(paname), "alsa_input.%s.%s", cardid, sid);
1885 handle_card_ports(u, data, card, prof);
1891 static void handle_card_ports(struct userdata *u, mir_node *data,
1892 pa_card *card, pa_card_profile *prof)
1894 mir_node *node = NULL;
1895 bool have_ports = false;
1896 mir_constr_def *cd = NULL;
1897 char *amname = data->amname;
1898 pa_device_port *port;
1901 char key[MAX_NAME_LENGTH+1];
1909 PA_HASHMAP_FOREACH(port, card->ports, state) {
1911 * if this port did not belong to any profile
1912 * (ie. prof->profiles == NULL) we assume that this port
1913 * does works with all the profiles
1915 if (port->profiles && pa_hashmap_get(port->profiles, prof->name) &&
1916 ((port->direction == PA_DIRECTION_INPUT && data->direction == mir_input)||
1917 (port->direction == PA_DIRECTION_OUTPUT && data->direction == mir_output)))
1922 snprintf(key, sizeof(key), "%s@%s", data->paname, port->name);
1925 data->available = (port->available != PA_AVAILABLE_NO);
1927 data->amname = amname;
1928 data->paport = port->name;
1930 pa_classify_node_by_card(data, card, prof, port);
1932 node = create_node(u, data, &created);
1935 node->stamp = data->stamp;
1937 cd = mir_constrain_create(u, "port", mir_constrain_port,
1939 mir_constrain_add_node(u, cd, node);
1946 data->key = data->paname;
1947 data->available = true;
1949 pa_classify_node_by_card(data, card, prof, NULL);
1951 node = create_node(u, data, &created);
1954 node->stamp = data->stamp;
1957 data->amname = amname;
1962 static mir_node *create_node(struct userdata *u, mir_node *data,
1965 pa_discover *discover;
1972 pa_assert(data->key);
1973 pa_assert(data->paname);
1974 pa_assert_se((discover = u->discover));
1976 if ((node = pa_hashmap_get(discover->nodes.byname, data->key)))
1981 node = mir_node_create(u, data);
1982 pa_hashmap_put(discover->nodes.byname, node->key, node);
1984 mir_node_print(node, buf, sizeof(buf));
1985 pa_log_debug("new node:\n%s", buf);
1987 if (node->available)
1988 pa_audiomgr_register_node(u, node);
1992 *created_ret = created;
1997 static void destroy_node(struct userdata *u, mir_node *node)
1999 pa_discover *discover;
2003 pa_assert_se((discover = u->discover));
2006 removed = pa_hashmap_remove(discover->nodes.byname, node->key);
2008 if (node != removed) {
2010 pa_log("%s: confused with data structures: key mismatch. "
2011 " attempted to destroy '%s'; actually destroyed '%s'",
2012 __FILE__, node->key, removed->key);
2014 pa_log("%s: confused with data structures: node '%s' "
2015 "is not in the hash table", __FILE__, node->key);
2019 pa_log_debug("destroying node: %s / '%s'", node->key, node->amname);
2021 if (node->implement == mir_stream) {
2022 if (node->direction == mir_input) {
2024 pa_log_debug("removing multiplexer");
2029 pa_audiomgr_unregister_node(u, node);
2031 extapi_signal_node_change(u);
2033 mir_constrain_remove_node(u, node);
2035 pa_loopback_destroy(u->loopback, u->core, node->loop);
2036 pa_multiplex_destroy(u->multiplex, u->core, node->mux);
2038 mir_node_destroy(u, node);
2042 static bool update_node_availability(struct userdata *u,
2049 if ((!available && node->available) ||
2050 ( available && !node->available) )
2052 node->available = available;
2055 pa_audiomgr_register_node(u, node);
2057 pa_audiomgr_unregister_node(u, node);
2059 extapi_signal_node_change(u);
2061 return true; /* routing needed */
2067 static bool update_node_availability_by_device(struct userdata *u,
2068 mir_direction direction,
2070 pa_device_port *port,
2080 pa_assert(direction == mir_input || direction == mir_output);
2082 if ((key = node_key(u, direction, data, port, buf, sizeof(buf)))) {
2083 if (!(node = pa_discover_find_node_by_key(u, key)))
2084 pa_log_debug(" can't find node (key '%s')", key);
2086 pa_log_debug(" node for '%s' found (key %s)",
2087 node->paname, node->key);
2089 return update_node_availability(u, node, available);
2093 return false; /* no routing needed */
2096 static char *get_name(char **string_ptr, int offs)
2098 char c, *name, *end;
2100 name = *string_ptr + offs;
2102 for (end = name; (c = *end); end++) {
2114 static void parse_profile_name(pa_card_profile *prof,
2124 pa_assert(prof->name);
2126 strncpy(buf, prof->name, buflen);
2127 buf[buflen-1] = '\0';
2129 memset(sinks, 0, sizeof(char *) * (MAX_CARD_TARGET+1));
2130 memset(sources, 0, sizeof(char *) * (MAX_CARD_TARGET+1));
2133 if (!strncmp(p, "output:", 7)) {
2134 if (i >= MAX_CARD_TARGET) {
2135 pa_log_debug("number of outputs exeeds the maximum %d in "
2136 "profile name '%s'", MAX_CARD_TARGET, prof->name);
2139 sinks[i++] = get_name(&p, 7);
2141 else if (!strncmp(p, "input:", 6)) {
2142 if (j >= MAX_CARD_TARGET) {
2143 pa_log_debug("number of inputs exeeds the maximum %d in "
2144 "profile name '%s'", MAX_CARD_TARGET, prof->name);
2147 sources[j++] = get_name(&p, 6);
2150 pa_log("%s: failed to parse profile name '%s'",
2151 __FILE__, prof->name);
2158 static char *node_key(struct userdata *u, mir_direction direction,
2159 void *data, pa_device_port *port, char *buf, size_t len)
2162 pa_card_profile *profile;
2170 const char *profile_name;
2176 pa_assert(direction == mir_input || direction == mir_output);
2178 if (direction == mir_output) {
2179 pa_sink *sink = data;
2181 name = pa_utils_get_sink_name(sink);
2184 port = sink->active_port;
2187 pa_source *source = data;
2189 name = pa_utils_get_source_name(source);
2190 card = source->card;
2192 port = source->active_port;
2198 pa_assert_se((profile = card->active_profile));
2200 if (!u->state.profile)
2201 profile_name = profile->name;
2203 pa_log_debug("state.profile is not null. '%s' supresses '%s'",
2204 u->state.profile, profile->name);
2205 profile_name = u->state.profile;
2209 if (!(bus = pa_utils_get_card_bus(card))) {
2210 pa_log_debug("ignoring %s '%s' due to lack of '%s' property "
2211 "on its card", type, name, PA_PROP_DEVICE_BUS);
2215 pci = pa_streq(bus, "pci");
2216 usb = pa_streq(bus, "usb");
2217 platform = pa_streq(bus, "platform");
2218 bluetooth = pa_streq(bus, "bluetooth");
2220 if (!pci && !usb && !bluetooth && !platform) {
2221 pa_log_debug("ignoring %s '%s' due to unsupported bus type '%s' "
2222 "of its card", type, name, bus);
2231 snprintf(buf, len, "%s@%s.%s", name, port->name, profile_name);
2239 snprintf(buf, len, "%s@%s", name, port->name);
2246 static pa_sink *make_output_prerouting(struct userdata *u,
2248 pa_channel_map *chmap,
2249 const char *media_role,
2250 mir_node **target_ret)
2254 pa_sink *sink = NULL;
2259 pa_assert_se((core = u->core));
2262 target = mir_router_make_prerouting(u, data);
2265 pa_log("there is no default route for the stream '%s'", data->amname);
2266 else if (target->paidx == PA_IDXSET_INVALID)
2267 pa_log("can't route to default '%s': no sink", target->amname);
2269 if (!(sink = pa_idxset_get_by_index(core->sinks, target->paidx)))
2270 pa_log("no route to default '%s': sink is gone", target->amname);
2272 if (u->enable_multiplex == true) {
2273 if (pa_classify_multiplex_stream(data)) {
2274 data->mux = pa_multiplex_create(u->multiplex, core,
2275 sink->index, chmap, NULL,
2276 media_role, data->type);
2278 sink = pa_idxset_get_by_index(core->sinks,
2279 data->mux->sink_index);
2288 *target_ret = target;
2294 static pa_source *make_input_prerouting(struct userdata *u,
2296 const char *media_role,
2297 mir_node **target_ret)
2301 pa_source *source = NULL;
2305 pa_assert_se((core = u->core));
2307 target = mir_router_make_prerouting(u, data);
2310 pa_log("there is no default route for the stream '%s'", data->amname);
2311 else if (target->paidx == PA_IDXSET_INVALID)
2312 pa_log("can't route to default '%s': no source", target->amname);
2314 if (!(source = pa_idxset_get_by_index(core->sources, target->paidx)))
2315 pa_log("no route to default '%s': source is gone",target->amname);
2319 *target_ret = target;
2324 static mir_node_type get_stream_routing_class(pa_proplist *pl)
2330 t = pa_utils_get_stream_class(pl);
2332 if (t >= mir_application_class_begin && t < mir_application_class_end)
2335 return mir_node_type_unknown;
2338 static char *get_stream_amname(mir_node_type type, char *name, pa_proplist *pl)
2351 appid = pa_utils_get_appid(pl);
2353 if (!strcmp(appid, "threaded-ml") ||
2354 !strcmp(appid, "WebProcess") ||
2355 !strcmp(appid,"wrt_launchpad_daemon") )
2357 return "wrtApplication";
2359 return "icoApplication";
2373 static void set_bluetooth_profile(struct userdata *u,
2375 pa_direction_t direction)
2378 pa_device_port *port;
2379 pa_card_profile *prof, *make_active;
2380 void *state0, *state1;
2381 bool port_available;
2387 pa_assert_se((core = u->core));
2393 pa_log_debug("which profile to make active:");
2395 PA_HASHMAP_FOREACH(prof, card->profiles, state0) {
2396 if (!prof->n_sinks && !prof->n_sources) {
2398 pa_log_debug(" considering %s", prof->name);
2404 port_available = false;
2406 PA_HASHMAP_FOREACH(port, card->ports, state1) {
2407 if ((direction & port->direction) &&
2408 pa_hashmap_get(port->profiles, prof->name))
2410 port_available = (port->available != PA_AVAILABLE_NO);
2415 if (!port_available)
2416 pa_log_debug(" ruling out %s (port not available)", prof->name);
2417 else if (prof->available != PA_AVAILABLE_YES)
2418 pa_log_debug(" ruling out %s (profile not available)", prof->name);
2422 if (((direction & PA_DIRECTION_INPUT) && prof->n_sources > 0) ||
2423 ((direction & PA_DIRECTION_OUTPUT) && prof->n_sinks > 0) ) {
2424 if (make_active && prof->priority < make_active->priority)
2425 pa_log_debug(" ruling out %s (low priority)", prof->name);
2427 pa_log_debug(" considering %s", prof->name);
2432 pa_log_debug(" ruling out %s (direction)", prof->name);
2439 pa_log_debug("No suitable profile found. Frustrated and do nothing");
2441 if (make_active == card->active_profile)
2442 pa_log_debug("Profile %s already set. Do nothing", make_active->name);
2444 if (switch_off && nport) {
2445 pa_log_debug("Do not switch to %s as active ports are existing "
2446 "to the other direction", make_active->name);
2449 pa_log_debug("Set profile %s", make_active->name);
2451 if ((prof = pa_hashmap_get(card->profiles, make_active->name)) != NULL &&
2452 pa_card_set_profile(card, prof, false) < 0) {
2453 pa_log_debug("Failed to change profile to %s",
2461 static void deferred_routing_cb(pa_mainloop_api *m, void *d)
2463 struct userdata *u = d;
2469 pa_log_debug("deferred routing starts");
2471 mir_router_make_routing(u);
2475 static void schedule_deferred_routing(struct userdata *u)
2480 pa_assert_se((core = u->core));
2482 pa_log_debug("scheduling deferred routing");
2484 pa_mainloop_api_once(core->mainloop, deferred_routing_cb, u);
2488 static void card_check_cb(pa_mainloop_api *m, void *d)
2490 card_check_t *cc = d;
2496 int n_sink, n_source;
2502 pa_assert((u = cc->u));
2503 pa_assert((core = u->core));
2505 pa_log_debug("card check starts");
2507 if (!(card = pa_idxset_get_by_index(core->cards, cc->index)))
2508 pa_log_debug("card %u is gone", cc->index);
2510 n_sink = n_source = 0;
2512 PA_IDXSET_FOREACH(sink, core->sinks, idx) {
2513 if ((sink->card) && sink->card->index == card->index)
2517 PA_IDXSET_FOREACH(source, core->sources, idx) {
2518 if ((source->card) && source->card->index == card->index)
2522 if (n_sink || n_source) {
2523 pa_log_debug("found %u sinks and %u sources belonging to "
2524 "'%s' card", n_sink, n_source, card->name);
2525 pa_log_debug("nothing to do");
2528 pa_log_debug("card '%s' has no sinks/sources. Do routing ...",
2530 mir_router_make_routing(u);
2538 static void schedule_card_check(struct userdata *u, pa_card *card)
2545 pa_assert_se((core = u->core));
2547 pa_log_debug("scheduling card check");
2549 cc = pa_xnew0(card_check_t, 1);
2551 cc->index = card->index;
2553 pa_mainloop_api_once(core->mainloop, card_check_cb, cc);
2557 static void source_cleanup_cb(pa_mainloop_api *m, void *d)
2559 source_cleanup_t *sc = d;
2566 pa_assert((u = sc->u));
2567 pa_assert((core = u->core));
2569 pa_log_debug("source cleanup starts");
2571 pa_loopback_destroy(u->loopback, u->core, sc->loop);
2572 pa_multiplex_destroy(u->multiplex, u->core, sc->mux);
2574 pa_log_debug("source cleanup ends");
2580 static void schedule_source_cleanup(struct userdata *u, mir_node *node)
2583 source_cleanup_t *sc;
2587 pa_assert_se((core = u->core));
2589 pa_log_debug("scheduling source cleanup");
2591 sc = pa_xnew0(source_cleanup_t, 1);
2593 sc->mux = node->mux;
2594 sc->loop = node->loop;
2599 pa_mainloop_api_once(core->mainloop, source_cleanup_cb, sc);
2604 static void stream_uncork_cb(pa_mainloop_api *m, void *d)
2606 stream_uncork_t *suc = d;
2610 pa_sink_input *sinp;
2616 pa_assert((u = suc->u));
2617 pa_assert((core = u->core));
2619 pa_log_debug("start uncorking stream");
2621 if (!(sink = pa_idxset_get_by_index(core->sinks, suc->index))) {
2622 pa_log_debug("sink.%d gone", suc->index);
2626 if (!(sinp = pa_idxset_first(core->sink_inputs, &index))) {
2627 pa_log_debug("sink_input is gone");
2631 pa_sink_input_cork(sinp, false);
2633 pa_log_debug("stream.%u uncorked", sinp->index);
2641 static void schedule_stream_uncorking(struct userdata *u, pa_sink *sink)
2644 stream_uncork_t *suc;
2648 pa_assert_se((core = u->core));
2650 pa_log_debug("scheduling stream uncorking");
2652 suc = pa_xnew0(stream_uncork_t, 1);
2654 suc->index = sink->index;
2656 pa_mainloop_api_once(core->mainloop, stream_uncork_cb, suc);
2663 * indent-tabs-mode: nil