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,
22 #include <pulsecore/pulsecore-config.h>
24 #include <pulsecore/hashmap.h>
25 #include <pulsecore/idxset.h>
26 #include <pulsecore/client.h>
27 #include <pulsecore/core-util.h>
28 #include <pulsecore/log.h>
29 #include <pulsecore/card.h>
30 #include <pulsecore/device-port.h>
31 #include <pulsecore/sink-input.h>
32 #include <pulsecore/source-output.h>
33 #include <pulsecore/strbuf.h>
40 #include "constrain.h"
41 #include "multiplex.h"
47 #define MAX_CARD_TARGET 4
48 #define MAX_NAME_LENGTH 256
50 #define ACTIVE_PORT NULL
52 /* Bluetooth service class */
53 #define BIT(x) (1U << (x))
55 #define BT_SERVICE_MASK 0xffe
56 #define BT_SERVICE_INFORMATION BIT(23) /**< WEB-server, WAP-server, etc */
57 #define BT_SERVICE_TELEPHONY BIT(22) /**< Modem, Headset, etc*/
58 #define BT_SERVICE_AUDIO BIT(21) /**< Speaker, Microphone, Headset */
59 #define BT_SERVICE_OBJECT_XFER BIT(20) /**< v-Inbox, v-Folder, etc */
60 #define BT_SERVICE_CAPTURING BIT(19) /**< Scanner, Microphone, etc */
61 #define BT_SERVICE_RENDERING BIT(18) /**< Printing, Speaker, etc */
62 #define BT_SERVICE_NETWORKING BIT(17) /**< LAN, Ad hoc, etc */
63 #define BT_SERVICE_POSITIONING BIT(16) /**< Location identification */
82 static const char combine_pattern[] = "Simultaneous output on ";
83 static const char loopback_pattern[] = "Loopback from ";
85 static void handle_alsa_card(struct userdata *, pa_card *);
86 static void handle_bluetooth_card(struct userdata *, pa_card *);
88 static void handle_udev_loaded_card(struct userdata *, pa_card *,
90 static void handle_card_ports(struct userdata *, mir_node *,
91 pa_card *, pa_card_profile *);
93 static mir_node *create_node(struct userdata *, mir_node *, pa_bool_t *);
94 static void destroy_node(struct userdata *, mir_node *);
95 static pa_bool_t update_node_availability(struct userdata *, mir_direction,
96 void *, pa_device_port *, pa_bool_t);
98 static void parse_profile_name(pa_card_profile *,
99 char **, char **, char *, int);
101 static char *node_key(struct userdata *, mir_direction,
102 void *, pa_device_port *, char *, size_t);
104 static pa_sink *make_output_prerouting(struct userdata *, mir_node *,
105 pa_channel_map *, const char *,
108 static mir_node_type get_stream_routing_class(pa_proplist *);
111 static void schedule_deferred_routing(struct userdata *);
112 static void schedule_card_check(struct userdata *, pa_card *);
113 static void schedule_source_cleanup(struct userdata *, mir_node *);
114 static void schedule_stream_uncorking(struct userdata *, pa_sink *);
116 static void pa_hashmap_node_free(void *node, void *u)
118 mir_node_destroy(u, node);
122 struct pa_discover *pa_discover_init(struct userdata *u)
124 pa_discover *discover = pa_xnew0(pa_discover, 1);
128 discover->selected = TRUE;
130 discover->nodes.byname = pa_hashmap_new(pa_idxset_string_hash_func,
131 pa_idxset_string_compare_func);
132 discover->nodes.byptr = pa_hashmap_new(pa_idxset_trivial_hash_func,
133 pa_idxset_trivial_compare_func);
137 void pa_discover_done(struct userdata *u)
139 pa_discover *discover;
141 if (u && (discover = u->discover)) {
142 pa_hashmap_free(discover->nodes.byname, pa_hashmap_node_free,u);
143 pa_hashmap_free(discover->nodes.byptr, NULL,NULL);
149 void pa_discover_domain_up(struct userdata *u)
151 pa_discover *discover;
156 pa_assert_se((discover = u->discover));
158 PA_HASHMAP_FOREACH(node, discover->nodes.byname, state) {
159 node->amid = AM_ID_INVALID;
161 if (node->visible && node->available)
162 pa_audiomgr_register_node(u, node);
166 void pa_discover_domain_down(struct userdata *u)
170 void pa_discover_add_card(struct userdata *u, pa_card *card)
177 if (!(bus = pa_proplist_gets(card->proplist, PA_PROP_DEVICE_BUS))) {
178 pa_log_debug("ignoring card '%s' due to lack of '%s' property",
179 pa_utils_get_card_name(card), PA_PROP_DEVICE_BUS);
183 if (pa_streq(bus, "pci") || pa_streq(bus, "usb")) {
184 handle_alsa_card(u, card);
187 else if (pa_streq(bus, "bluetooth")) {
188 handle_bluetooth_card(u, card);
192 pa_log_debug("ignoring card '%s' due to unsupported bus type '%s'",
193 pa_utils_get_card_name(card), bus);
196 void pa_discover_remove_card(struct userdata *u, pa_card *card)
199 pa_discover *discover;
205 pa_assert_se((discover = u->discover));
207 if (!(bus = pa_proplist_gets(card->proplist, PA_PROP_DEVICE_BUS)))
211 PA_HASHMAP_FOREACH(node, discover->nodes.byname, state) {
212 if (node->implement == mir_device &&
213 node->pacard.index == card->index)
215 if (pa_streq(bus, "pci") || pa_streq(bus, "usb"))
216 mir_constrain_destroy(u, node->paname);
218 destroy_node(u, node);
222 if (pa_streq(bus, "bluetooth"))
223 mir_constrain_destroy(u, card->name);
226 void pa_discover_profile_changed(struct userdata *u, pa_card *card)
228 pa_card_profile *prof;
229 pa_discover *discover;
240 pa_assert_se((discover = u->discover));
243 if ((bus = pa_proplist_gets(card->proplist, PA_PROP_DEVICE_BUS)) == NULL) {
244 pa_log_debug("ignoring profile change on card '%s' due to lack of '%s'"
245 "property", pa_utils_get_card_name(card),
250 pci = pa_streq(bus, "pci");
251 usb = pa_streq(bus, "usb");
252 bluetooth = pa_streq(bus, "bluetooth");
254 if (!pci && !usb && !bluetooth) {
255 pa_log_debug("ignoring profile change on card '%s' due to unsupported "
256 "bus type '%s'", pa_utils_get_card_name(card), bus);
260 pa_assert_se((prof = card->active_profile));
262 pa_log_debug("bluetooth profile changed to '%s' on card '%s'",
263 prof->name, card->name);
265 if (!prof->n_sinks && !prof->n_sources) {
266 /* switched of but not unloaded yet */
267 PA_HASHMAP_FOREACH(node, discover->nodes.byname, state) {
268 if (node->implement == mir_device &&
269 node->pacard.index == card->index)
271 node->available = FALSE;
277 pa_log_debug("alsa profile changed to '%s' on card '%s'",
278 card->active_profile->name, card->name);
280 stamp = pa_utils_get_stamp();
282 handle_alsa_card(u, card);
284 PA_HASHMAP_FOREACH(node, discover->nodes.byname, state) {
285 if (node->implement == mir_device &&
286 node->pacard.index == card->index &&
289 destroy_node(u, node);
296 void pa_discover_port_available_changed(struct userdata *u,
297 pa_device_port *port)
309 pa_assert_se((core = u->core));
311 switch (port->available) {
312 case PA_PORT_AVAILABLE_NO: state = "not "; available = FALSE; break;
313 case PA_PORT_AVAILABLE_YES: state = ""; available = TRUE; break;
314 default: /* do nothing */ return;
317 pa_log_debug("port '%s' availabilty changed to %savailable. Updating",
322 if (port->is_output) {
323 PA_IDXSET_FOREACH(sink, core->sinks, idx) {
325 if (port == pa_hashmap_get(sink->ports, port->name)) {
326 pa_log_debug(" sink '%s'", sink->name);
327 route |= update_node_availability(u, mir_output,sink,port,
334 if (port->is_input) {
335 PA_IDXSET_FOREACH(source, core->sources, idx) {
337 if (port == pa_hashmap_get(source->ports, port->name)) {
338 pa_log_debug(" source '%s'", source->name);
339 route |= update_node_availability(u, mir_input,source,port,
347 mir_router_make_routing(u);
350 void pa_discover_add_sink(struct userdata *u, pa_sink *sink, pa_bool_t route)
353 pa_discover *discover;
363 pa_assert_se((discover = u->discover));
365 module = sink->module;
367 if ((card = sink->card)) {
368 if (!(key = node_key(u, mir_output,sink,ACTIVE_PORT, buf,sizeof(buf))))
370 if (!(node = pa_discover_find_node_by_key(u, key))) {
371 pa_log_debug("can't find node for sink (key '%s')", key);
374 pa_log_debug("node for '%s' found (key %s). Updating with sink data",
375 node->paname, node->key);
376 node->paidx = sink->index;
377 pa_discover_add_node_to_ptr_hash(u, sink, node);
382 if (type != mir_bluetooth_a2dp && type != mir_bluetooth_sco)
383 mir_router_make_routing(u);
385 if (!u->state.profile)
386 schedule_deferred_routing(u);
390 else if (!module || !pa_streq(module->name, "module-combine-sink")) {
391 memset(&data, 0, sizeof(data));
392 data.key = pa_xstrdup(sink->name);
393 data.direction = mir_output;
394 data.implement = mir_device;
395 data.channels = sink->channel_map.channels;
397 if (sink == pa_utils_get_null_sink(u)) {
398 data.visible = FALSE;
399 data.available = TRUE;
400 data.type = mir_null;
401 data.amname = pa_xstrdup("Silent");
402 data.amid = AM_ID_INVALID;
403 data.paname = pa_xstrdup(sink->name);
404 data.paidx = sink->index;
407 pa_xfree(data.key); /* for now */
408 pa_log_info("currently we do not support statically loaded sinks");
412 create_node(u, &data, NULL);
417 void pa_discover_remove_sink(struct userdata *u, pa_sink *sink)
419 pa_discover *discover;
426 pa_assert_se((discover = u->discover));
428 name = pa_utils_get_sink_name(sink);
430 if (!(node = pa_hashmap_get(discover->nodes.byptr, sink)))
431 pa_log_debug("can't find node for sink (name '%s')", name);
433 pa_log_debug("node found for '%s'. Reseting sink data", name);
434 node->paidx = PA_IDXSET_INVALID;
435 pa_hashmap_remove(discover->nodes.byptr, sink);
440 if (type != mir_bluetooth_a2dp && type != mir_bluetooth_sco)
441 node->available = FALSE;
443 if (!u->state.profile)
444 schedule_deferred_routing(u);
448 pa_log_info("currently we do not support statically loaded sinks");
454 void pa_discover_add_source(struct userdata *u, pa_source *source)
456 pa_discover *discover;
462 const char *loopback_role;
468 pa_assert_se((discover = u->discover));
470 if ((card = source->card)) {
471 if (!(key = node_key(u,mir_input,source,ACTIVE_PORT,kbf,sizeof(kbf))))
473 if (!(node = pa_discover_find_node_by_key(u, key))) {
474 pa_log_debug("can't find node for source (key '%s')", key);
477 pa_log_debug("node for '%s' found. Updating with source data",
479 node->paidx = source->index;
480 pa_discover_add_node_to_ptr_hash(u, source, node);
481 if ((loopback_role = pa_classify_loopback_stream(node))) {
482 if (!(ns = pa_utils_get_null_sink(u))) {
483 pa_log("Can't load loopback module: no initial null sink");
486 node->loop = pa_loopback_create(u->loopback, u->core,
487 source->index, ns->index,
490 si = pa_loopback_get_sink_index(u->core, node->loop);
491 node->mux = pa_multiplex_find(u->multiplex, si);
494 mir_node_print(node, nbf, sizeof(nbf));
495 pa_log_debug("updated node:\n%s", nbf);
501 void pa_discover_remove_source(struct userdata *u, pa_source *source)
503 pa_discover *discover;
509 pa_assert_se((discover = u->discover));
511 name = pa_utils_get_source_name(source);
513 if (!(node = pa_hashmap_get(discover->nodes.byptr, source)))
514 pa_log_debug("can't find node for source (name '%s')", name);
516 pa_log_debug("node found. Reseting source data");
517 schedule_source_cleanup(u, node);
518 node->paidx = PA_IDXSET_INVALID;
519 pa_hashmap_remove(discover->nodes.byptr, source);
524 void pa_discover_register_sink_input(struct userdata *u, pa_sink_input *sinp)
527 pa_discover *discover;
541 pa_assert_se((core = u->core));
542 pa_assert_se((discover = u->discover));
543 pa_assert_se((pl = sinp->proplist));
545 if ((media = pa_proplist_gets(sinp->proplist, PA_PROP_MEDIA_NAME))) {
546 if (!strncmp(media, combine_pattern, sizeof(combine_pattern)-1)) {
547 pa_log_debug("Seems to be a combine stream. Nothing to do ...");
550 if (!strncmp(media, loopback_pattern, sizeof(loopback_pattern)-1)) {
551 pa_log_debug("Seems to be a loopback stream. Nothing to do ...");
556 name = pa_utils_get_sink_input_name(sinp);
558 pa_log_debug("registering stream '%s'", name);
560 if (!(type = pa_classify_guess_stream_node_type(pl))) {
561 pa_log_debug("cant find stream class for '%s'. "
562 "Leaving it alone", name);
566 pa_utils_set_stream_routing_properties(pl, type, NULL);
568 snprintf(key, sizeof(key), "stream_input.%d", sinp->index);
570 memset(&data, 0, sizeof(data));
572 data.direction = mir_input;
573 data.implement = mir_stream;
574 data.channels = sinp->channel_map.channels;
577 data.available = TRUE;
579 data.amdescr = (char *)pa_proplist_gets(pl, PA_PROP_MEDIA_NAME);
580 data.amid = AM_ID_INVALID;
582 data.paidx = sinp->index;
585 * here we can't guess whether the application requested an explicit
586 * route by sepcifying the target sink @ stream creation time.
588 * the brute force solution: we make a default route for this stream
589 * possibly overwiriting the orginal app request :(
591 /* this will set data.mux */
592 role = pa_proplist_gets(sinp->proplist, PA_PROP_MEDIA_ROLE);
593 sink = make_output_prerouting(u, &data, &sinp->channel_map, role, &target);
595 node = create_node(u, &data, NULL);
597 pa_discover_add_node_to_ptr_hash(u, sinp, node);
599 if (sink && target) {
600 pa_log_debug("move stream to sink %u (%s)", sink->index, sink->name);
602 if (pa_sink_input_move_to(sinp, sink, FALSE) < 0)
603 pa_log("failed to route '%s' => '%s'",node->amname,target->amname);
605 pa_log_debug("register route '%s' => '%s'",
606 node->amname, target->amname);
607 /* FIXME: and actually do it ... */
612 void pa_discover_preroute_sink_input(struct userdata *u,
613 pa_sink_input_new_data *data)
618 pa_discover *discover;
627 pa_assert_se((core = u->core));
628 pa_assert_se((discover = u->discover));
629 pa_assert_se((pl = data->proplist));
631 mnam = (m = data->module) ? m->name : "";
633 if (pa_streq(mnam, "module-combine-sink"))
634 type = mir_node_type_unknown;
636 if (pa_streq(mnam, "module-loopback"))
638 type = pa_classify_guess_stream_node_type(pl);
639 pa_utils_set_stream_routing_properties(pl, type, data->sink);
643 memset(&fake, 0, sizeof(fake));
644 fake.direction = mir_input;
645 fake.implement = mir_stream;
646 fake.channels = data->channel_map.channels;
649 fake.available = TRUE;
650 fake.amname = "<preroute>";
652 role = pa_proplist_gets(data->proplist, PA_PROP_MEDIA_ROLE);
653 sink = make_output_prerouting(u, &fake, &data->channel_map, role,NULL);
657 if (fake.mux && !(data->flags & PA_SINK_INPUT_START_CORKED)) {
658 data->flags |= PA_SINK_INPUT_START_CORKED;
659 schedule_stream_uncorking(u, sink);
663 if (!pa_sink_input_new_data_set_sink(data, sink, FALSE))
664 pa_log("can't set sink %d for new sink-input", sink->index);
670 void pa_discover_add_sink_input(struct userdata *u, pa_sink_input *sinp)
674 pa_sink_input *csinp;
676 pa_discover *discover;
688 pa_assert_se((core = u->core));
689 pa_assert_se((discover = u->discover));
690 pa_assert_se((pl = sinp->proplist));
693 if ((media = pa_proplist_gets(sinp->proplist, PA_PROP_MEDIA_NAME))) {
694 if (!strncmp(media, combine_pattern, sizeof(combine_pattern)-1)) {
695 pa_log_debug("New stream is a combine stream. Nothing to do ...");
698 if (!strncmp(media, loopback_pattern, sizeof(loopback_pattern)-1)) {
699 pa_log_debug("New stream is a loopback stream. Nothing to do ...");
704 name = pa_utils_get_sink_input_name(sinp);
706 pa_log_debug("dealing with new stream '%s'", name);
708 if ((type = get_stream_routing_class(pl)) == mir_node_type_unknown) {
709 if (!(type = pa_classify_guess_stream_node_type(pl))) {
710 pa_log_debug("cant find stream class for '%s'. "
711 "Leaving it alone", name);
715 pa_utils_set_stream_routing_properties(pl, type, NULL);
717 /* if needed, make some post-routing here */
720 /* we need to add this to main hashmap as that is used for loop
721 through on all nodes. */
722 snprintf(key, sizeof(key), "stream_input.%d", sinp->index);
724 memset(&data, 0, sizeof(data));
726 data.direction = mir_input;
727 data.implement = mir_stream;
728 data.channels = sinp->channel_map.channels;
731 data.available = TRUE;
733 data.amdescr = (char *)pa_proplist_gets(pl, PA_PROP_MEDIA_NAME);
734 data.amid = AM_ID_INVALID;
736 data.paidx = sinp->index;
737 data.mux = pa_multiplex_find(u->multiplex, sinp->sink->index);
739 node = create_node(u, &data, &created);
744 pa_log("%s: confused with stream. '%s' did exists",
745 __FILE__, node->amname);
749 pa_discover_add_node_to_ptr_hash(u, sinp, node);
754 csinp = pa_idxset_get_by_index(core->sink_inputs,
755 data.mux->defstream_index);
756 s = csinp ? csinp->sink : NULL;
760 pa_log_debug("routing target candidate is %u (%s)", s->index, s->name);
762 if (!s || !(snod = pa_hashmap_get(discover->nodes.byptr, s)))
763 pa_log_debug("can't figure out where this stream is routed");
765 pa_log_debug("register route '%s' => '%s'",
766 node->amname, snod->amname);
767 /* FIXME: and actually do it ... */
770 pa_fader_apply_volume_limits(u, pa_utils_get_stamp());
775 void pa_discover_remove_sink_input(struct userdata *u, pa_sink_input *sinp)
777 pa_discover *discover;
784 pa_assert_se((discover = u->discover));
786 name = pa_utils_get_sink_input_name(sinp);
788 if (!(node = pa_discover_remove_node_from_ptr_hash(u, sinp)))
789 pa_log_debug("can't find node for sink-input (name '%s')", name);
791 pa_log_debug("node found for '%s'. After clearing the route "
792 "it will be destroyed", name);
794 if (!(sinknod = pa_hashmap_get(discover->nodes.byptr, sinp->sink)))
795 pa_log_debug("can't figure out where this stream is routed");
797 pa_log_debug("clear route '%s' => '%s'",
798 node->amname, sinknod->amname);
800 /* FIXME: and actually do it ... */
804 destroy_node(u, node);
806 mir_router_make_routing(u);
811 mir_node *pa_discover_find_node_by_key(struct userdata *u, const char *key)
813 pa_discover *discover;
817 pa_assert_se((discover = u->discover));
820 node = pa_hashmap_get(discover->nodes.byname, key);
827 mir_node *pa_discover_find_node_by_ptr(struct userdata *u, void *ptr)
829 pa_discover *discover;
833 pa_assert_se((discover = u->discover));
836 node = pa_hashmap_get(discover->nodes.byptr, ptr);
843 void pa_discover_add_node_to_ptr_hash(struct userdata *u,
847 pa_discover *discover;
852 pa_assert_se((discover = u->discover));
854 pa_hashmap_put(discover->nodes.byptr, ptr, node);
857 mir_node *pa_discover_remove_node_from_ptr_hash(struct userdata *u, void *ptr)
859 pa_discover *discover;
863 pa_assert_se((discover = u->discover));
865 return pa_hashmap_remove(discover->nodes.byptr, ptr);
869 static void handle_alsa_card(struct userdata *u, pa_card *card)
876 memset(&data, 0, sizeof(data));
878 data.amid = AM_ID_INVALID;
879 data.implement = mir_device;
880 data.paidx = PA_IDXSET_INVALID;
881 data.stamp = pa_utils_get_stamp();
883 cnam = pa_utils_get_card_name(card);
884 udd = pa_proplist_gets(card->proplist, "module-udev-detect.discovered");
886 if (udd && pa_streq(udd, "1")) {
887 /* udev loaded alsa card */
888 if (!strncmp(cnam, "alsa_card.", 10)) {
890 handle_udev_loaded_card(u, card, &data, cid);
895 /* statically loaded pci or usb card */
898 pa_log_debug("ignoring unrecognized pci card '%s'", cnam);
902 static void handle_bluetooth_card(struct userdata *u, pa_card *card)
904 pa_discover *discover;
905 pa_card_profile *prof;
913 char paname[MAX_NAME_LENGTH+1];
914 char amname[MAX_NAME_LENGTH+1];
915 char key[MAX_NAME_LENGTH+1];
918 pa_assert_se((discover = u->discover));
920 cdescr = pa_proplist_gets(card->proplist, PA_PROP_DEVICE_DESCRIPTION);
923 memset(paname, 0, sizeof(paname));
924 memset(amname, 0, sizeof(amname));
925 memset(key , 0, sizeof(key) );
927 memset(&data, 0, sizeof(data));
930 data.amid = AM_ID_INVALID;
931 data.implement = mir_device;
932 data.paidx = PA_IDXSET_INVALID;
933 data.paname = paname;
934 data.amname = amname;
935 data.amdescr = (char *)cdescr;
936 data.pacard.index = card->index;
937 data.stamp = pa_utils_get_stamp();
939 cnam = pa_utils_get_card_name(card);
941 if (!strncmp(cnam, "bluez_card.", 11)) {
944 cd = mir_constrain_create(u, "profile", mir_constrain_profile, cnam);
946 PA_HASHMAP_FOREACH(prof, card->profiles, state) {
947 data.available = TRUE;
948 data.pacard.profile = prof->name;
950 if (prof->n_sinks > 0) {
951 data.direction = mir_output;
952 data.channels = prof->max_sink_channels;
953 data.amname = amname;
955 snprintf(paname, sizeof(paname), "bluez_sink.%s", cid);
956 snprintf(key, sizeof(key), "%s@%s", paname, prof->name);
957 pa_classify_node_by_card(&data, card, prof, NULL);
958 node = create_node(u, &data, NULL);
959 cd = mir_constrain_create(u, "profile", mir_constrain_profile,
961 mir_constrain_add_node(u, cd, node);
964 if (prof->n_sources > 0) {
965 data.direction = mir_input;
966 data.channels = prof->max_source_channels;
967 data.amname = amname;
969 snprintf(paname, sizeof(paname), "bluez_source.%s", cid);
970 snprintf(key, sizeof(key), "%s@%s", paname, prof->name);
971 pa_classify_node_by_card(&data, card, prof, NULL);
972 node = create_node(u, &data, NULL);
973 mir_constrain_add_node(u, cd, node);
977 if (!(prof = card->active_profile))
978 pa_log("card '%s' has no active profile", card->name);
980 pa_log_debug("card '%s' default profile '%s'",
981 card->name, prof->name);
984 schedule_card_check(u, card);
989 static void handle_udev_loaded_card(struct userdata *u, pa_card *card,
990 mir_node *data, char *cardid)
992 pa_discover *discover;
993 pa_card_profile *prof;
994 pa_card_profile *active;
998 char *sinks[MAX_CARD_TARGET+1];
999 char *sources[MAX_CARD_TARGET+1];
1000 char buf[MAX_NAME_LENGTH+1];
1001 char paname[MAX_NAME_LENGTH+1];
1002 char amname[MAX_NAME_LENGTH+1];
1006 pa_assert(card->profiles);
1007 pa_assert_se((discover = u->discover));
1009 alsanam = pa_proplist_gets(card->proplist, "alsa.card_name");
1011 memset(amname, 0, sizeof(amname));
1013 data->paname = paname;
1014 data->amname = amname;
1015 data->amdescr = (char *)alsanam;
1017 data->pacard.index = card->index;
1019 active = card->active_profile;
1021 PA_HASHMAP_FOREACH(prof, card->profiles, state) {
1022 /* filtering: deal with selected profiles if requested so */
1023 if (discover->selected && (!active || (active && prof != active)))
1026 /* filtering: skip the 'off' profiles */
1027 if (!prof->n_sinks && !prof->n_sources)
1030 /* filtering: consider sinks with suitable amount channels */
1031 if (prof->n_sinks &&
1032 (prof->max_sink_channels < discover->chmin ||
1033 prof->max_sink_channels > discover->chmax ))
1036 /* filtering: consider sources with suitable amount channels */
1037 if (prof->n_sources &&
1038 (prof->max_source_channels < discover->chmin ||
1039 prof->max_source_channels > discover->chmax ))
1042 data->pacard.profile = prof->name;
1044 parse_profile_name(prof, sinks,sources, buf,sizeof(buf));
1046 data->direction = mir_output;
1047 data->channels = prof->max_sink_channels;
1048 for (i = 0; (sid = sinks[i]); i++) {
1049 snprintf(paname, sizeof(paname), "alsa_output.%s.%s", cardid, sid);
1050 handle_card_ports(u, data, card, prof);
1053 data->direction = mir_input;
1054 data->channels = prof->max_source_channels;
1055 for (i = 0; (sid = sources[i]); i++) {
1056 snprintf(paname, sizeof(paname), "alsa_input.%s.%s", cardid, sid);
1057 handle_card_ports(u, data, card, prof);
1063 static void handle_card_ports(struct userdata *u, mir_node *data,
1064 pa_card *card, pa_card_profile *prof)
1066 mir_node *node = NULL;
1067 pa_bool_t have_ports = FALSE;
1068 mir_constr_def *cd = NULL;
1069 char *amname = data->amname;
1070 pa_device_port *port;
1073 char key[MAX_NAME_LENGTH+1];
1081 PA_HASHMAP_FOREACH(port, card->ports, state) {
1083 * if this port did not belong to any profile
1084 * (ie. prof->profiles == NULL) we assume that this port
1085 * does works with all the profiles
1087 if (port->profiles && pa_hashmap_get(port->profiles, prof->name) &&
1088 ((port->is_input && data->direction == mir_input)||
1089 (port->is_output && data->direction == mir_output)))
1094 snprintf(key, sizeof(key), "%s@%s", data->paname, port->name);
1097 data->available = (port->available != PA_PORT_AVAILABLE_NO);
1099 data->amname = amname;
1100 data->paport = port->name;
1102 pa_classify_node_by_card(data, card, prof, port);
1104 node = create_node(u, data, &created);
1107 node->stamp = data->stamp;
1109 cd = mir_constrain_create(u, "port", mir_constrain_port,
1111 mir_constrain_add_node(u, cd, node);
1118 data->key = data->paname;
1119 data->available = TRUE;
1121 pa_classify_node_by_card(data, card, prof, NULL);
1123 node = create_node(u, data, &created);
1126 node->stamp = data->stamp;
1129 data->amname = amname;
1134 static mir_node *create_node(struct userdata *u, mir_node *data,
1135 pa_bool_t *created_ret)
1137 pa_discover *discover;
1144 pa_assert(data->key);
1145 pa_assert(data->paname);
1146 pa_assert_se((discover = u->discover));
1148 if ((node = pa_hashmap_get(discover->nodes.byname, data->key)))
1153 node = mir_node_create(u, data);
1154 pa_hashmap_put(discover->nodes.byname, node->key, node);
1156 mir_node_print(node, buf, sizeof(buf));
1157 pa_log_debug("new node:\n%s", buf);
1159 if (node->available)
1160 pa_audiomgr_register_node(u, node);
1164 *created_ret = created;
1169 static void destroy_node(struct userdata *u, mir_node *node)
1171 pa_discover *discover;
1175 pa_assert_se((discover = u->discover));
1178 removed = pa_hashmap_remove(discover->nodes.byname, node->key);
1180 if (node != removed) {
1182 pa_log("%s: confused with data structures: key mismatch. "
1183 " attempted to destroy '%s'; actually destroyed '%s'",
1184 __FILE__, node->key, removed->key);
1186 pa_log("%s: confused with data structures: node '%s' "
1187 "is not in the hash table", __FILE__, node->key);
1191 pa_log_debug("destroying node: %s / '%s'", node->key, node->amname);
1193 if (node->implement == mir_stream) {
1194 if (node->direction == mir_input) {
1196 pa_log_debug("removing multiplexer");
1201 pa_audiomgr_unregister_node(u, node);
1203 mir_constrain_remove_node(u, node);
1205 pa_loopback_destroy(u->loopback, u->core, node->loop);
1206 pa_multiplex_destroy(u->multiplex, u->core, node->mux);
1208 mir_node_destroy(u, node);
1212 static pa_bool_t update_node_availability(struct userdata *u,
1213 mir_direction direction,
1214 void *data, pa_device_port *port,
1215 pa_bool_t available)
1224 pa_assert(direction == mir_input || direction == mir_output);
1226 if ((key = node_key(u, direction, data, port, buf, sizeof(buf)))) {
1227 if (!(node = pa_discover_find_node_by_key(u, key)))
1228 pa_log_debug(" can't find node for sink (key '%s')", key);
1230 pa_log_debug(" node for '%s' found (key %s)",
1231 node->paname, node->key);
1232 if ((!available && node->available) ||
1233 ( available && !node->available) )
1235 node->available = available;
1238 pa_audiomgr_register_node(u, node);
1240 pa_audiomgr_unregister_node(u, node);
1242 return TRUE; /* routing needed */
1247 return FALSE; /* no routing needed */
1250 static char *get_name(char **string_ptr, int offs)
1252 char c, *name, *end;
1254 name = *string_ptr + offs;
1256 for (end = name; (c = *end); end++) {
1268 static void parse_profile_name(pa_card_profile *prof,
1278 pa_assert(prof->name);
1280 strncpy(buf, prof->name, buflen);
1281 buf[buflen-1] = '\0';
1283 memset(sinks, 0, sizeof(char *) * (MAX_CARD_TARGET+1));
1284 memset(sources, 0, sizeof(char *) * (MAX_CARD_TARGET+1));
1287 if (!strncmp(p, "output:", 7)) {
1288 if (i >= MAX_CARD_TARGET) {
1289 pa_log_debug("number of outputs exeeds the maximum %d in "
1290 "profile name '%s'", MAX_CARD_TARGET, prof->name);
1293 sinks[i++] = get_name(&p, 7);
1295 else if (!strncmp(p, "input:", 6)) {
1296 if (j >= MAX_CARD_TARGET) {
1297 pa_log_debug("number of inputs exeeds the maximum %d in "
1298 "profile name '%s'", MAX_CARD_TARGET, prof->name);
1301 sources[j++] = get_name(&p, 6);
1304 pa_log("%s: failed to parse profile name '%s'",
1305 __FILE__, prof->name);
1312 static char *node_key(struct userdata *u, mir_direction direction,
1313 void *data, pa_device_port *port, char *buf, size_t len)
1316 pa_card_profile *profile;
1320 pa_bool_t bluetooth;
1323 const char *profile_name;
1328 pa_assert(direction == mir_input || direction == mir_output);
1330 if (direction == mir_input) {
1331 pa_sink *sink = data;
1333 name = pa_utils_get_sink_name(sink);
1336 port = sink->active_port;
1339 pa_source *source = data;
1341 name = pa_utils_get_source_name(source);
1342 card = source->card;
1344 port = source->active_port;
1350 pa_assert_se((profile = card->active_profile));
1352 if (!u->state.profile)
1353 profile_name = profile->name;
1355 pa_log_debug("state.profile is not null. '%s' supresses '%s'",
1356 u->state.profile, profile->name);
1357 profile_name = u->state.profile;
1361 if (!(bus = pa_proplist_gets(card->proplist, PA_PROP_DEVICE_BUS))) {
1362 pa_log_debug("ignoring %s '%s' due to lack of '%s' property "
1363 "on its card", type, name, PA_PROP_DEVICE_BUS);
1367 pci = pa_streq(bus, "pci");
1368 usb = pa_streq(bus, "usb");
1369 bluetooth = pa_streq(bus, "bluetooth");
1371 if (!pci && !usb && !bluetooth) {
1372 pa_log_debug("ignoring %s '%s' due to unsupported bus type '%s' "
1373 "of its card", type, name, bus);
1379 snprintf(buf, len, "%s@%s", name, profile_name);
1386 snprintf(buf, len, "%s@%s", name, port->name);
1393 static pa_sink *make_output_prerouting(struct userdata *u,
1395 pa_channel_map *chmap,
1396 const char *media_role,
1397 mir_node **target_ret)
1401 pa_sink *sink = NULL;
1406 pa_assert_se((core = u->core));
1409 target = mir_router_make_prerouting(u, data);
1412 pa_log("there is no default route for the stream '%s'", data->amname);
1413 else if (target->paidx == PA_IDXSET_INVALID)
1414 pa_log("can't route to default '%s': no sink", target->amname);
1416 if (!(sink = pa_idxset_get_by_index(core->sinks, target->paidx)))
1417 pa_log("can't route to default '%s': sink is gone",target->amname);
1419 if (pa_classify_multiplex_stream(data)) {
1420 data->mux = pa_multiplex_create(u->multiplex, core,
1421 sink->index, chmap, NULL,
1422 media_role, data->type);
1424 sink = pa_idxset_get_by_index(core->sinks,
1425 data->mux->sink_index);
1433 *target_ret = target;
1439 static mir_node_type get_stream_routing_class(pa_proplist *pl)
1447 if ((clid = pa_proplist_gets(pl, PA_PROP_ROUTING_CLASS_ID))) {
1448 type = strtol(clid, &e, 10);
1451 if (type >= mir_application_class_begin &&
1452 type < mir_application_class_end)
1459 return mir_node_type_unknown;
1464 static void deferred_routing_cb(pa_mainloop_api *m, void *d)
1466 struct userdata *u = d;
1472 pa_log_debug("deferred routing starts");
1474 mir_router_make_routing(u);
1478 static void schedule_deferred_routing(struct userdata *u)
1483 pa_assert_se((core = u->core));
1485 pa_log_debug("scheduling deferred routing");
1487 pa_mainloop_api_once(core->mainloop, deferred_routing_cb, u);
1491 static void card_check_cb(pa_mainloop_api *m, void *d)
1493 card_check_t *cc = d;
1499 int n_sink, n_source;
1505 pa_assert((u = cc->u));
1506 pa_assert((core = u->core));
1508 pa_log_debug("card check starts");
1510 if (!(card = pa_idxset_get_by_index(core->cards, cc->index)))
1511 pa_log_debug("card %u is gone", cc->index);
1513 n_sink = n_source = 0;
1515 PA_IDXSET_FOREACH(sink, core->sinks, idx) {
1516 if ((sink->card) && sink->card->index == card->index)
1520 PA_IDXSET_FOREACH(source, core->sources, idx) {
1521 if ((source->card) && source->card->index == card->index)
1525 if (n_sink || n_source) {
1526 pa_log_debug("found %u sinks and %u sources belonging to "
1527 "'%s' card", n_sink, n_source, card->name);
1528 pa_log_debug("nothing to do");
1531 pa_log_debug("card '%s' has no sinks/sources. Do routing ...",
1533 mir_router_make_routing(u);
1541 static void schedule_card_check(struct userdata *u, pa_card *card)
1548 pa_assert_se((core = u->core));
1550 pa_log_debug("scheduling card check");
1552 cc = pa_xnew0(card_check_t, 1);
1554 cc->index = card->index;
1556 pa_mainloop_api_once(core->mainloop, card_check_cb, cc);
1560 static void source_cleanup_cb(pa_mainloop_api *m, void *d)
1562 source_cleanup_t *sc = d;
1569 pa_assert((u = sc->u));
1570 pa_assert((core = u->core));
1572 pa_log_debug("source cleanup starts");
1574 pa_loopback_destroy(u->loopback, u->core, sc->loop);
1575 pa_multiplex_destroy(u->multiplex, u->core, sc->mux);
1577 pa_log_debug("source cleanup ends");
1583 static void schedule_source_cleanup(struct userdata *u, mir_node *node)
1586 source_cleanup_t *sc;
1590 pa_assert_se((core = u->core));
1592 pa_log_debug("scheduling source cleanup");
1594 sc = pa_xnew0(source_cleanup_t, 1);
1596 sc->mux = node->mux;
1597 sc->loop = node->loop;
1602 pa_mainloop_api_once(core->mainloop, source_cleanup_cb, sc);
1606 static void stream_uncork_cb(pa_mainloop_api *m, void *d)
1608 stream_uncork_t *suc = d;
1612 pa_sink_input *sinp;
1618 pa_assert((u = suc->u));
1619 pa_assert((core = u->core));
1621 pa_log_debug("start uncorking stream");
1623 if (!(sink = pa_idxset_get_by_index(core->sinks, suc->index))) {
1624 pa_log_debug("sink.%d gone", suc->index);
1628 if (!(sinp = pa_idxset_first(core->sink_inputs, &index))) {
1629 pa_log_debug("sink_input is gone");
1633 pa_sink_input_cork(sinp, FALSE);
1635 pa_log_debug("stream.%u uncorked", sinp->index);
1643 static void schedule_stream_uncorking(struct userdata *u, pa_sink *sink)
1646 stream_uncork_t *suc;
1650 pa_assert_se((core = u->core));
1652 pa_log_debug("scheduling stream uncorking");
1654 suc = pa_xnew0(stream_uncork_t, 1);
1656 suc->index = sink->index;
1658 pa_mainloop_api_once(core->mainloop, stream_uncork_cb, suc);
1665 * indent-tabs-mode: nil