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,
20 #include <sys/types.h>
26 #include <pulse/utf8.h>
27 #include <pulse/timeval.h>
28 #include <pulsecore/pulsecore-config.h>
29 #include <pulsecore/module.h>
30 #include <pulsecore/llist.h>
31 #include <pulsecore/idxset.h>
32 #include <pulsecore/hashmap.h>
33 #include <pulsecore/core-util.h>
34 #include <pulsecore/sink-input.h>
35 #include <pulsecore/source-output.h>
39 #define WITH_RESOURCES
42 #if defined(WITH_DOMCTL) || defined(WITH_RESOURCES)
43 #include <murphy/common/macros.h>
44 #include <murphy/common/mainloop.h>
45 #include <murphy/pulse/pulse-glue.h>
49 #include <murphy/resource/protocol.h>
50 #include <murphy/common/transport.h>
51 #include <murphy/resource/protocol.h>
52 #include <murphy/resource/data-types.h>
57 #include "stream-state.h"
61 #define INVALID_ID (~(uint32_t)0)
62 #define INVALID_INDEX (~(uint32_t)0)
63 #define INVALID_SEQNO (~(uint32_t)0)
64 #define INVALID_REQUEST (~(uint16_t)0)
66 #define DISCONNECTED -1
70 #define RESCOL_NAMES "rsetid,autorel,state,grant,pid,policy"
71 #define RESCOL_RSETID 0
72 #define RESCOL_AUTOREL 1
73 #define RESCOL_STATE 2
74 #define RESCOL_GRANT 3
76 #define RESCOL_POLICY 5
78 #define RSET_RELEASE 1
79 #define RSET_ACQUIRE 2
81 #define PUSH_VALUE(msg, tag, typ, val) \
82 mrp_msg_append(msg, MRP_MSG_TAG_##typ(RESPROTO_##tag, val))
84 #define PUSH_ATTRS(msg, rif, proplist) \
85 resource_push_attributes(msg, rif, proplist)
87 typedef struct resource_attribute resource_attribute;
88 typedef struct resource_request resource_request;
90 struct resource_attribute {
91 PA_LLIST_FIELDS(resource_attribute);
96 struct resource_request {
97 PA_LLIST_FIELDS(resource_request);
110 mrp_domctl_table_t *tables;
112 mrp_domctl_watch_t *watches;
113 pa_murphyif_watch_cb watchcb;
124 audio_resource_t inpres;
125 audio_resource_t outres;
126 #ifdef WITH_RESOURCES
127 mrp_transport_t *transp;
128 mrp_sockaddr_t saddr;
144 PA_LLIST_HEAD(resource_attribute, attrs);
145 PA_LLIST_HEAD(resource_request, reqs);
147 } resource_interface;
151 #if defined(WITH_DOMCTL) || defined(WITH_RESOURCES)
154 domctl_interface domctl;
155 resource_interface resource;
158 #ifdef WITH_RESOURCES
183 static void domctl_connect_notify(mrp_domctl_t *,int,int,const char *,void *);
184 static void domctl_watch_notify(mrp_domctl_t *,mrp_domctl_data_t *,int,void *);
185 static void domctl_dump_data(mrp_domctl_data_t *);
188 #ifdef WITH_RESOURCES
189 static void resource_attribute_destroy(resource_interface *,
190 resource_attribute *);
191 static int resource_transport_connect(resource_interface *);
192 static void resource_xport_closed_evt(mrp_transport_t *, int, void *);
194 static mrp_msg_t *resource_create_request(uint32_t, mrp_resproto_request_t);
195 static bool resource_send_message(resource_interface *, mrp_msg_t *,
196 uint32_t, uint16_t, uint32_t);
197 static bool resource_set_create_node(struct userdata *, mir_node *,
198 pa_nodeset_resdef *, bool);
199 static bool resource_set_create_all(struct userdata *);
200 static bool resource_set_destroy_node(struct userdata *, uint32_t);
201 static bool resource_set_destroy_all(struct userdata *);
202 static void resource_set_notification(struct userdata *, const char *,
203 int, mrp_domctl_value_t **);
205 static bool resource_push_attributes(mrp_msg_t *, resource_interface *,
208 static void resource_recv_msg(mrp_transport_t *, mrp_msg_t *, void *);
209 static void resource_recvfrom_msg(mrp_transport_t *, mrp_msg_t *,
210 mrp_sockaddr_t *, socklen_t, void *);
211 static void resource_set_create_response(struct userdata *, mir_node *,
212 mrp_msg_t *, void **);
213 static void resource_set_create_response_abort(struct userdata *,
214 mrp_msg_t *, void **);
216 static bool resource_fetch_seqno(mrp_msg_t *, void **, uint32_t *);
217 static bool resource_fetch_request(mrp_msg_t *, void **, uint16_t *);
218 static bool resource_fetch_status(mrp_msg_t *, void **, int *);
219 static bool resource_fetch_rset_id(mrp_msg_t *, void **, uint32_t*);
220 static bool resource_fetch_rset_state(mrp_msg_t *, void **,
221 mrp_resproto_state_t *);
222 static bool resource_fetch_rset_mask(mrp_msg_t *, void **,
223 mrp_resproto_state_t *);
225 static bool resource_transport_create(struct userdata *, pa_murphyif *);
226 static void resource_transport_destroy(pa_murphyif *);
228 static void connect_attempt(pa_mainloop_api *, pa_time_event *,
229 const struct timeval *, void *);
230 static void schedule_connect(struct userdata *, resource_interface *);
231 static void cancel_schedule(struct userdata *, resource_interface *);
233 static rset_hash *node_put_rset(struct userdata *, mir_node *, rset_data *);
234 static void node_enforce_resource_policy(struct userdata *, mir_node *,
236 static rset_data *rset_data_dup(rset_data *);
237 static void rset_data_copy(rset_data *, rset_data *);
238 static void rset_data_update(rset_data *, rset_data *);
239 static void rset_data_free(rset_data *);
241 static void pid_hashmap_free(void *, void *);
242 static int pid_hashmap_put(struct userdata *, const char *,
243 mir_node *, rset_data *);
244 static mir_node *pid_hashmap_get_node(struct userdata *, const char *);
245 static rset_data *pid_hashmap_get_rset(struct userdata *, const char *);
246 static mir_node *pid_hashmap_remove_node(struct userdata *, const char *);
247 static rset_data *pid_hashmap_remove_rset(struct userdata *, const char *);
249 static void rset_hashmap_free(void *, void *);
250 static rset_hash *rset_hashmap_put(struct userdata *, const char *, mir_node *);
251 static rset_hash *rset_hashmap_get(struct userdata *u, const char *rsetid);
252 static int rset_hashmap_remove(struct userdata *,const char *,mir_node*);
256 static pa_proplist *get_node_proplist(struct userdata *, mir_node *);
257 static const char *get_node_pid(struct userdata *, mir_node *);
260 pa_murphyif *pa_murphyif_init(struct userdata *u,
261 const char *ctl_addr,
262 const char *res_addr)
264 pa_murphyif *murphyif;
265 domctl_interface *dif;
266 resource_interface *rif;
267 #if defined(WITH_DOMCTL) || defined(WITH_RESOURCES)
270 if (!(ml = mrp_mainloop_pulse_get(u->core->mainloop))) {
271 pa_log_error("Failed to set up murphy mainloop.");
275 #ifdef WITH_RESOURCES
278 murphyif = pa_xnew0(pa_murphyif, 1);
279 dif = &murphyif->domctl;
280 rif = &murphyif->resource;
282 #if defined(WITH_DOMCTL) || defined(WITH_RESOURCES)
286 dif->addr = pa_xstrdup(ctl_addr ? ctl_addr:MRP_DEFAULT_DOMCTL_ADDRESS);
290 rif->addr = pa_xstrdup(res_addr ? res_addr:RESPROTO_DEFAULT_ADDRESS);
291 #ifdef WITH_RESOURCES
292 rif->alen = mrp_transport_resolve(NULL, rif->addr, &rif->saddr,
293 sizeof(rif->saddr), &rif->atype);
294 if (rif->alen <= 0) {
295 pa_log("can't resolve resource transport address '%s'", rif->addr);
298 rif->inpres.tblidx = -1;
299 rif->outres.tblidx = -1;
300 rif->connect.period = 1 * PA_USEC_PER_SEC;
302 if (!resource_transport_create(u, murphyif)) {
303 pa_log("failed to create resource transport");
304 schedule_connect(u, rif);
307 if (resource_transport_connect(rif) == DISCONNECTED)
308 schedule_connect(u, rif);
312 rif->seqno.request = 1;
313 rif->nodes.rsetid = pa_hashmap_new(pa_idxset_string_hash_func,
314 pa_idxset_string_compare_func);
315 rif->nodes.pid = pa_hashmap_new(pa_idxset_string_hash_func,
316 pa_idxset_string_compare_func);
317 PA_LLIST_HEAD_INIT(resource_attribute, rif->attrs);
318 PA_LLIST_HEAD_INIT(resource_request, rif->reqs);
325 void pa_murphyif_done(struct userdata *u)
327 pa_murphyif *murphyif;
328 domctl_interface *dif;
329 resource_interface *rif;
330 #ifdef WITH_RESOURCES
331 resource_attribute *attr, *a;
332 resource_request *req, *r;
338 if (u && (murphyif = u->murphyif)) {
340 mrp_domctl_table_t *t;
341 mrp_domctl_watch_t *w;
344 dif = &murphyif->domctl;
346 mrp_domctl_destroy(dif->ctl);
348 if (dif->ntable > 0 && dif->tables) {
349 for (i = 0; i < dif->ntable; i++) {
351 pa_xfree((void *)t->table);
352 pa_xfree((void *)t->mql_columns);
353 pa_xfree((void *)t->mql_index);
355 pa_xfree(dif->tables);
358 if (dif->nwatch > 0 && dif->watches) {
359 for (i = 0; i < dif->nwatch; i++) {
360 w = dif->watches + i;
361 pa_xfree((void *)w->table);
362 pa_xfree((void *)w->mql_columns);
363 pa_xfree((void *)w->mql_where);
365 pa_xfree(dif->watches);
368 pa_xfree((void *)dif->addr);
371 #ifdef WITH_RESOURCES
372 rif = &murphyif->resource;
374 resource_transport_destroy(murphyif);
376 PA_HASHMAP_FOREACH(rh, rif->nodes.rsetid, state) {
379 rset_data_free(rh->rset);
384 PA_HASHMAP_FOREACH(ph, rif->nodes.pid, state) {
386 pa_xfree((void *)ph->pid);
387 rset_data_free(ph->rset);
392 pa_hashmap_free(rif->nodes.rsetid);
393 pa_hashmap_free(rif->nodes.pid);
395 PA_LLIST_FOREACH_SAFE(attr, a, rif->attrs)
396 resource_attribute_destroy(rif, attr);
398 PA_LLIST_FOREACH_SAFE(req, r, rif->reqs)
401 pa_xfree((void *)rif->addr);
402 pa_xfree((void *)rif->inpres.name);
403 pa_xfree((void *)rif->outres.name);
405 mrp_mainloop_destroy(murphyif->ml);
412 void pa_murphyif_add_table(struct userdata *u,
417 pa_murphyif *murphyif;
418 domctl_interface *dif;
419 mrp_domctl_table_t *t;
426 pa_assert_se((murphyif = u->murphyif));
428 dif = &murphyif->domctl;
431 size = sizeof(mrp_domctl_table_t) * dif->ntable;
432 t = (dif->tables = pa_xrealloc(dif->tables, size)) + idx;
434 t->table = pa_xstrdup(table);
435 t->mql_columns = pa_xstrdup(columns);
436 t->mql_index = index ? pa_xstrdup(index) : NULL;
439 int pa_murphyif_add_watch(struct userdata *u,
445 pa_murphyif *murphyif;
446 domctl_interface *dif;
447 mrp_domctl_watch_t *w;
454 pa_assert(max_rows > 0 && max_rows < MQI_QUERY_RESULT_MAX);
455 pa_assert_se((murphyif = u->murphyif));
457 dif = &murphyif->domctl;
460 size = sizeof(mrp_domctl_watch_t) * dif->nwatch;
461 w = (dif->watches = pa_xrealloc(dif->watches, size)) + idx;
463 w->table = pa_xstrdup(table);
464 w->mql_columns = pa_xstrdup(columns);
465 w->mql_where = where ? pa_xstrdup(where) : NULL;
466 w->max_rows = max_rows;
471 void pa_murphyif_setup_domainctl(struct userdata *u, pa_murphyif_watch_cb wcb)
473 static const char *name = "pulse";
475 pa_murphyif *murphyif;
476 domctl_interface *dif;
480 pa_assert_se((murphyif = u->murphyif));
482 dif = &murphyif->domctl;
485 if (dif->ntable || dif->nwatch) {
486 dif->ctl = mrp_domctl_create(name, murphyif->ml,
487 dif->tables, dif->ntable,
488 dif->watches, dif->nwatch,
489 domctl_connect_notify,
490 domctl_watch_notify, u);
492 pa_log("failed to create '%s' domain controller", name);
496 if (!mrp_domctl_connect(dif->ctl, dif->addr, 0)) {
497 pa_log("failed to conect to murphyd");
502 pa_log_info("'%s' domain controller sucessfully created", name);
507 void pa_murphyif_add_audio_resource(struct userdata *u,
512 static const char *columns = RESCOL_NAMES;
513 static int maxrow = MQI_QUERY_RESULT_MAX - 1;
515 pa_murphyif *murphyif;
516 resource_interface *rif;
517 audio_resource_t *res;
521 pa_assert(dir == mir_input || dir == mir_output);
524 pa_assert_se((murphyif = u->murphyif));
525 rif = &murphyif->resource;
528 if (dir == mir_input) {
529 if (rif->inpres.name)
530 pa_log("attempt to register playback resource multiple time");
535 if (rif->outres.name)
536 pa_log("attempt to register recording resource multiple time");
542 res->name = pa_xstrdup(name);
544 snprintf(table, sizeof(table), "%s_users", name);
545 res->tblidx = pa_murphyif_add_watch(u, table, columns, NULL, maxrow);
550 void pa_murphyif_add_audio_attribute(struct userdata *u,
553 mqi_data_type_t type,
554 ... ) /* default value */
556 #ifdef WITH_RESOURCES
557 pa_murphyif *murphyif;
558 resource_interface *rif;
559 resource_attribute *attr;
560 mrp_attr_value_t *val;
566 pa_assert(type == mqi_string || type == mqi_integer ||
567 type == mqi_unsignd || type == mqi_floating);
569 pa_assert_se((murphyif = u->murphyif));
570 rif = &murphyif->resource;
572 attr = pa_xnew0(resource_attribute, 1);
573 val = &attr->def.value;
575 attr->prop = pa_xstrdup(propnam);
576 attr->def.name = pa_xstrdup(attrnam);
577 attr->def.type = type;
582 case mqi_string: val->string = pa_xstrdup(va_arg(ap, char *)); break;
583 case mqi_integer: val->integer = va_arg(ap, int32_t); break;
584 case mqi_unsignd: val->unsignd = va_arg(ap, uint32_t); break;
585 case mqi_floating: val->floating = va_arg(ap, double); break;
586 default: attr->def.type = mqi_error; break;
591 if (attr->def.type == mqi_error)
592 resource_attribute_destroy(rif, attr);
594 PA_LLIST_PREPEND(resource_attribute, rif->attrs, attr);
598 void pa_murphyif_create_resource_set(struct userdata *u,
600 pa_nodeset_resdef *resdef)
603 pa_murphyif *murphyif;
604 resource_interface *rif;
609 pa_assert((!node->loop && node->implement == mir_stream) ||
610 ( node->loop && node->implement == mir_device) );
611 pa_assert(node->direction == mir_input || node->direction == mir_output);
612 pa_assert(node->zone);
613 pa_assert(!node->rsetid);
615 pa_assert_se((core = u->core));
617 pa_assert_se((murphyif = u->murphyif));
618 rif = &murphyif->resource;
620 state = resource_transport_connect(rif);
625 resource_set_create_all(u);
629 node->localrset = resource_set_create_node(u, node, resdef, true);
637 void pa_murphyif_destroy_resource_set(struct userdata *u, mir_node *node)
639 pa_murphyif *murphyif;
645 pa_assert_se((murphyif = u->murphyif));
647 if (node->localrset && node->rsetid) {
649 pa_murphyif_delete_node(u, node);
651 rsetid = strtoul(node->rsetid, &e, 10);
653 if (e == node->rsetid || *e) {
654 pa_log("can't destroy resource set: invalid rsetid '%s'",
658 if (rset_hashmap_remove(u, node->rsetid, node) < 0) {
659 pa_log_debug("failed to remove resource set %s from hashmap",
663 if (resource_set_destroy_node(u, rsetid))
664 pa_log_debug("sent resource set %u destruction request", rsetid);
666 pa_log("failed to destroy resourse set %u for node '%s'",
667 rsetid, node->amname);
670 pa_xfree(node->rsetid);
672 node->localrset = false;
678 int pa_murphyif_add_node(struct userdata *u, mir_node *node)
680 #ifdef WITH_RESOURCES
681 pa_murphyif *murphyif;
682 resource_interface *rif;
691 pa_assert_se((murphyif = u->murphyif));
693 rif = &murphyif->resource;
696 pa_log("can't register resource set for node %u '%s'.: missing rsetid",
697 node->paidx, node->amname);
699 else if (pa_streq(node->rsetid, PA_RESOURCE_SET_ID_PID)) {
700 if (!(pid = get_node_pid(u,node)))
701 pa_log("can't obtain PID for node '%s'", node->amname);
703 if (pid_hashmap_put(u, pid, node, NULL) == 0)
706 if ((rset = pid_hashmap_remove_rset(u, pid))) {
707 pa_log_debug("found resource-set %s for node '%s'",
708 rset->id, node->amname);
710 if (node_put_rset(u, node, rset)) {
711 node_enforce_resource_policy(u, node, rset);
712 rset_data_free(rset);
716 pa_log("can't register resource set for node '%s': "
717 "failed to set rsetid", node->amname);
719 rset_data_free(rset);
722 pa_log("can't register resource set for node '%s': "
723 "conflicting pid", node->amname);
728 if ((rh = rset_hashmap_put(u, node->rsetid, node))) {
731 pa_log_debug("enforce policies on node %u '%s' rsetid:%s autorel:%s "
732 "state:%s grant:%s policy:%s", node->paidx, node->amname,
733 rset->id, rset->autorel ? "yes":"no",
734 rset->state == RSET_ACQUIRE ? "acquire":"release",
735 rset->grant ? "yes":"no", rset->policy);
737 node_enforce_resource_policy(u, node, rset);
748 void pa_murphyif_delete_node(struct userdata *u, mir_node *node)
750 #ifdef WITH_RESOURCES
751 pa_murphyif *murphyif;
752 resource_interface *rif;
759 pa_assert_se((murphyif = u->murphyif));
761 rif = &murphyif->resource;
764 if (pa_streq(node->rsetid, PA_RESOURCE_SET_ID_PID)) {
765 if ((pid = get_node_pid(u, node))) {
766 if (node == pid_hashmap_get_node(u, pid))
767 pid_hashmap_remove_node(u, pid);
769 pa_log("pid %s seems to have multiple resource sets. "
770 "Refuse to delete node %u (%s) from hashmap",
771 pid, node->index, node->amname);
776 if (rset_hashmap_remove(u, node->rsetid, node) < 0) {
777 pa_log("failed to remove node '%s' from rset hash", node->amname);
786 static void domctl_connect_notify(mrp_domctl_t *dc, int connected, int errcode,
787 const char *errmsg, void *user_data)
790 MRP_UNUSED(user_data);
793 pa_log_info("Successfully registered to Murphy.");
795 pa_log_error("Domain control Connection to Murphy failed (%d: %s).",
800 static void domctl_watch_notify(mrp_domctl_t *dc, mrp_domctl_data_t *tables,
801 int ntable, void *user_data)
803 struct userdata *u = (struct userdata *)user_data;
804 pa_murphyif *murphyif;
805 domctl_interface *dif;
806 resource_interface *rif;
807 mrp_domctl_data_t *t;
808 mrp_domctl_watch_t *w;
814 pa_assert(ntable > 0);
816 pa_assert_se((murphyif = u->murphyif));
818 dif = &murphyif->domctl;
819 rif = &murphyif->resource;
821 pa_log_info("Received change notification for %d tables.", ntable);
823 for (i = 0; i < ntable; i++) {
828 pa_assert(t->id >= 0);
829 pa_assert(t->id < dif->nwatch);
831 w = dif->watches + t->id;
833 #ifdef WITH_RESOURCES
834 if (t->id == rif->inpres.tblidx || t->id == rif->outres.tblidx) {
835 resource_set_notification(u, w->table, t->nrow, t->rows);
840 dif->watchcb(u, w->table, t->nrow, t->rows);
844 static void domctl_dump_data(mrp_domctl_data_t *table)
846 mrp_domctl_value_t *row;
852 pa_log_debug("Table #%d: %d rows x %d columns", table->id,
853 table->nrow, table->ncolumn);
855 for (i = 0; i < table->nrow; i++) {
856 row = table->rows[i];
860 for (j = 0, t = ""; j < table->ncolumn; j++, t = ", ") {
861 switch (row[j].type) {
862 case MRP_DOMCTL_STRING:
863 l = snprintf(p, n, "%s'%s'", t, row[j].str);
867 case MRP_DOMCTL_INTEGER:
868 l = snprintf(p, n, "%s%d", t, row[j].s32);
872 case MRP_DOMCTL_UNSIGNED:
873 l = snprintf(p, n, "%s%u", t, row[j].u32);
877 case MRP_DOMCTL_DOUBLE:
878 l = snprintf(p, n, "%s%f", t, row[j].dbl);
883 l = snprintf(p, n, "%s<invalid column 0x%x>",
890 pa_log_debug("row #%d: { %s }", i, buf);
895 #ifdef WITH_RESOURCES
896 static void resource_attribute_destroy(resource_interface *rif,
897 resource_attribute *attr)
901 PA_LLIST_REMOVE(resource_attribute, rif->attrs, attr);
903 pa_xfree((void *)attr->prop);
904 pa_xfree((void *)attr->def.name);
906 if (attr->def.type == mqi_string)
907 pa_xfree((void *)attr->def.value.string);
913 static int resource_transport_connect(resource_interface *rif)
922 if (!mrp_transport_connect(rif->transp, &rif->saddr, rif->alen))
923 status = DISCONNECTED;
925 pa_log_info("resource transport connected to '%s'", rif->addr);
926 rif->connected = true;
934 static void resource_xport_closed_evt(mrp_transport_t *transp, int error,
937 struct userdata *u = (struct userdata *)void_u;
938 pa_murphyif *murphyif;
939 resource_interface *rif;
944 pa_assert_se((murphyif = u->murphyif));
946 rif = &murphyif->resource;
949 pa_log("Resource transport connection closed by peer");
951 pa_log("Resource transport connection closed with error %d (%s)",
952 error, strerror(error));
955 resource_transport_destroy(murphyif);
956 resource_set_destroy_all(u);
957 schedule_connect(u, rif);
960 static mrp_msg_t *resource_create_request(uint32_t seqno,
961 mrp_resproto_request_t req)
966 msg = mrp_msg_create(RESPROTO_SEQUENCE_NO , MRP_MSG_FIELD_UINT32, seqno,
967 RESPROTO_REQUEST_TYPE, MRP_MSG_FIELD_UINT16, type ,
968 RESPROTO_MESSAGE_END );
971 pa_log("can't to create new resource message");
976 static bool resource_send_message(resource_interface *rif,
982 resource_request *req;
985 if (!mrp_transport_send(rif->transp, msg)) {
986 pa_log("failed to send resource message");
990 req = pa_xnew0(resource_request, 1);
991 req->nodidx = nodidx;
995 PA_LLIST_PREPEND(resource_request, rif->reqs, req);
1003 static bool resource_set_create_node(struct userdata *u,
1005 pa_nodeset_resdef *resdef,
1009 pa_murphyif *murphyif;
1010 resource_interface *rif;
1011 resource_request *req;
1015 uint32_t rset_flags;
1017 pa_nodeset_map *map;
1020 pa_sink_input *sinp;
1021 pa_source_output *sout;
1022 audio_resource_t *res;
1024 mir_node_type type = 0;
1025 uint32_t audio_flags = 0;
1027 pa_proplist *proplist = NULL;
1028 bool success = true;
1032 pa_assert(node->index != PA_IDXSET_INVALID);
1033 pa_assert((!node->loop && node->implement == mir_stream) ||
1034 ( node->loop && node->implement == mir_device) );
1035 pa_assert(node->direction == mir_input || node->direction == mir_output);
1036 pa_assert(node->zone);
1037 pa_assert(!node->rsetid);
1039 pa_assert_se((core = u->core));
1041 if ((loop = node->loop)) {
1042 if (node->direction == mir_input) {
1043 sout = pa_idxset_get_by_index(core->source_outputs,
1044 loop->source_output_index);
1046 proplist = sout->proplist;
1049 sinp = pa_idxset_get_by_index(core->sink_inputs,
1050 loop->sink_input_index);
1052 proplist = sinp->proplist;
1054 if (proplist && (role = pa_proplist_gets(proplist, PA_PROP_MEDIA_ROLE))) {
1055 if ((map = pa_nodeset_get_map_by_role(u, role)))
1060 if (node->direction == mir_output) {
1061 if ((sout = pa_idxset_get_by_index(core->source_outputs, node->paidx)))
1062 proplist = sout->proplist;
1065 if ((sinp = pa_idxset_get_by_index(core->sink_inputs, node->paidx)))
1066 proplist = sinp->proplist;
1071 pa_assert_se((class = pa_nodeset_get_class(u, type)));
1072 pa_assert_se((murphyif = u->murphyif));
1073 rif = &murphyif->resource;
1075 reqid = RESPROTO_CREATE_RESOURCE_SET;
1076 seqno = rif->seqno.request++;
1077 res = (node->direction == mir_input) ? &rif->inpres : &rif->outres;
1079 pa_assert_se((resnam = res->name));
1081 rset_flags = RESPROTO_RSETFLAG_NOEVENTS;
1082 rset_flags |= (acquire ? RESPROTO_RSETFLAG_AUTOACQUIRE : 0);
1083 rset_flags |= (resdef ? resdef->flags.rset : 0);
1085 audio_flags = (resdef ? resdef->flags.audio : 0);
1087 priority = (resdef ? resdef->priority : 0);
1089 if (!(msg = resource_create_request(seqno, reqid)))
1092 if (PUSH_VALUE(msg, RESOURCE_FLAGS , UINT32, rset_flags) &&
1093 PUSH_VALUE(msg, RESOURCE_PRIORITY, UINT32, priority) &&
1094 PUSH_VALUE(msg, CLASS_NAME , STRING, class) &&
1095 PUSH_VALUE(msg, ZONE_NAME , STRING, node->zone) &&
1096 PUSH_VALUE(msg, RESOURCE_NAME , STRING, resnam) &&
1097 PUSH_VALUE(msg, RESOURCE_FLAGS , UINT32, audio_flags) &&
1098 PUSH_VALUE(msg, ATTRIBUTE_NAME , STRING, "policy") &&
1099 PUSH_VALUE(msg, ATTRIBUTE_VALUE , STRING, "strict") &&
1100 PUSH_ATTRS(msg, rif, proplist) &&
1101 PUSH_VALUE(msg, SECTION_END , UINT8 , 0) )
1103 success = resource_send_message(rif, msg, node->index, reqid, seqno);
1111 pa_log_debug("requested resource set for '%s'", node->amname);
1113 pa_log_debug("failed to create resource set for '%s'", node->amname);
1118 static bool resource_set_create_all(struct userdata *u)
1128 idx = PA_IDXSET_INVALID;
1130 while ((node = pa_nodeset_iterate_nodes(u, &idx))) {
1131 if ((node->implement == mir_stream && !node->loop) ||
1132 (node->implement == mir_device && node->loop) )
1134 if (!node->rsetid) {
1135 node->localrset = resource_set_create_node(u, node, NULL, false);
1136 success &= node->localrset;
1144 static bool resource_set_destroy_node(struct userdata *u, uint32_t rsetid)
1146 pa_murphyif *murphyif;
1147 resource_interface *rif;
1156 pa_assert_se((murphyif = u->murphyif));
1157 rif = &murphyif->resource;
1159 reqid = RESPROTO_DESTROY_RESOURCE_SET;
1160 seqno = rif->seqno.request++;
1161 nodidx = PA_IDXSET_INVALID;
1162 msg = resource_create_request(seqno, reqid);
1164 if (PUSH_VALUE(msg, RESOURCE_SET_ID, UINT32, rsetid))
1165 success = resource_send_message(rif, msg, nodidx, reqid, seqno);
1174 static bool resource_set_destroy_all(struct userdata *u)
1176 pa_murphyif *murphyif;
1177 resource_interface *rif;
1185 pa_assert_se((murphyif = u->murphyif));
1187 rif = &murphyif->resource;
1191 idx = PA_IDXSET_INVALID;
1193 while ((node = pa_nodeset_iterate_nodes(u, &idx))) {
1194 if (node->implement == mir_stream && node->localrset) {
1195 pa_log_debug("destroying resource set for '%s'", node->amname);
1197 if (rif->connected && node->rsetid) {
1198 rsetid = strtoul(node->rsetid, &e, 10);
1200 if (e == node->rsetid || *e)
1203 rset_hashmap_remove(u, node->rsetid, node);
1204 success &= resource_set_destroy_node(u, rsetid);
1208 pa_xfree(node->rsetid);
1210 node->localrset = false;
1211 node->rsetid = NULL;
1218 static void resource_set_notification(struct userdata *u,
1221 mrp_domctl_value_t **values)
1223 pa_murphyif *murphyif;
1224 resource_interface *rif;
1226 mrp_domctl_value_t *row;
1227 mrp_domctl_value_t *crsetid;
1228 mrp_domctl_value_t *cautorel;
1229 mrp_domctl_value_t *cstate;
1230 mrp_domctl_value_t *cgrant;
1231 mrp_domctl_value_t *cpid;
1232 mrp_domctl_value_t *cpolicy;
1235 mir_node *node, **nodes;
1237 rset_data rset, *rs;
1243 pa_assert_se((murphyif = u->murphyif));
1244 rif = &murphyif->resource;
1246 for (r = 0; r < nrow; r++) {
1248 crsetid = row + RESCOL_RSETID;
1249 cautorel = row + RESCOL_AUTOREL;
1250 cstate = row + RESCOL_STATE;
1251 cgrant = row + RESCOL_GRANT;
1252 cpid = row + RESCOL_PID;
1253 cpolicy = row + RESCOL_POLICY;
1255 if (crsetid->type != MRP_DOMCTL_UNSIGNED ||
1256 cautorel->type != MRP_DOMCTL_INTEGER ||
1257 cstate->type != MRP_DOMCTL_INTEGER ||
1258 cgrant->type != MRP_DOMCTL_INTEGER ||
1259 cpid->type != MRP_DOMCTL_STRING ||
1260 cpolicy->type != MRP_DOMCTL_STRING )
1262 pa_log("invalid field type in '%s' (%d|%d|%d|%d|%d|%d)", table,
1263 crsetid->type, cautorel->type, cstate->type,
1264 cgrant->type, cpid->type, cpolicy->type);
1268 snprintf(rsetid, sizeof(rsetid), "%d", crsetid->s32);
1272 rset.autorel = cautorel->s32;
1273 rset.state = cstate->s32;
1274 rset.grant = cgrant->s32;
1275 rset.policy = cpolicy->str;
1278 if (rset.autorel != 0 && rset.autorel != 1) {
1279 pa_log_debug("invalid autorel %d in table '%s'",
1280 rset.autorel, table);
1283 if (rset.state != RSET_RELEASE && rset.state != RSET_ACQUIRE) {
1284 pa_log_debug("invalid state %d in table '%s'", rset.state, table);
1287 if (rset.grant != 0 && rset.grant != 1) {
1288 pa_log_debug("invalid grant %d in table '%s'", rset.grant, table);
1292 if (!(rh = rset_hashmap_get(u, rset.id))) {
1294 pa_log_debug("can't find node for resource set %s "
1295 "(pid in resource set unknown)", rset.id);
1299 if ((node = pid_hashmap_remove_node(u, pid))) {
1300 pa_log_debug("found node %s for resource-set '%s'",
1301 node->amname, rset.id);
1303 if (!(rh = node_put_rset(u, node, &rset))) {
1304 pa_log("can't register resource set for node '%s': "
1305 "failed to set rsetid", node->amname);
1310 if (pid_hashmap_put(u, pid, NULL, rset_data_dup(&rset)) < 0) {
1311 if (!(rs = pid_hashmap_get_rset(u, pid)))
1312 pa_log("failed to add resource set to pid hash");
1314 if (!pa_streq(rs->id, rset.id)) {
1315 pa_log("process %s appears to have multiple resour"
1316 "ce sets (%s and %s)", pid, rs->id,rset.id);
1318 pa_log_debug("update resource-set %s data in "
1319 "pid hash (pid %s)", rs->id, pid);
1320 rset_data_copy(rs, &rset);
1324 pa_log_debug("can't find node for resource set %s. "
1325 "Beleive the stream will appear later on",
1333 rset_data_update(rh->rset, &rset);
1335 /* we need to make a copy of this as node_enforce_resource_policy()
1336 will delete/modify it */
1337 size = sizeof(mir_node *) * (rh->nnode + 1);
1338 nodes = alloca(size);
1339 memcpy(nodes, rh->nodes, size);
1341 for (i = 0; (node = nodes[i]); i++) {
1342 pa_log_debug("%u: resource notification for node '%s' autorel:%s "
1343 "state:%s grant:%s pid:%s policy:%s", i,
1344 node->amname, rset.autorel ? "yes":"no",
1345 rset.state == RSET_ACQUIRE ? "acquire":"release",
1346 rset.grant ? "yes":"no", pid, rset.policy);
1348 node_enforce_resource_policy(u, node, &rset);
1354 static bool resource_push_attributes(mrp_msg_t *msg,
1355 resource_interface *rif,
1356 pa_proplist *proplist)
1358 resource_attribute *attr;
1372 PA_LLIST_FOREACH(attr, rif->attrs) {
1373 if (!PUSH_VALUE(msg, ATTRIBUTE_NAME, STRING, attr->def.name))
1377 sts = pa_proplist_get(proplist, attr->prop, &v.ptr, &size);
1381 switch (attr->def.type) {
1384 v.str = attr->def.value.string;
1385 else if (v.str[size-1] != '\0' || strlen(v.str) != (size-1) ||
1386 !pa_utf8_valid(v.str))
1388 if (!PUSH_VALUE(msg, ATTRIBUTE_VALUE, STRING, v.str))
1394 v.i32 = &attr->def.value.integer;
1395 else if (size != sizeof(*v.i32))
1397 if (!PUSH_VALUE(msg, ATTRIBUTE_VALUE, SINT8, *v.i32))
1403 v.u32 = &attr->def.value.unsignd;
1404 else if (size != sizeof(*v.u32))
1406 if (!PUSH_VALUE(msg, ATTRIBUTE_VALUE, SINT8, *v.u32))
1412 v.dbl = &attr->def.value.floating;
1413 else if (size != sizeof(*v.dbl))
1415 if (!PUSH_VALUE(msg, ATTRIBUTE_VALUE, SINT8, *v.dbl))
1419 default: /* we should never get here */
1429 static void resource_recv_msg(mrp_transport_t *t, mrp_msg_t *msg, void *void_u)
1431 return resource_recvfrom_msg(t, msg, NULL, 0, void_u);
1434 static void resource_recvfrom_msg(mrp_transport_t *transp, mrp_msg_t *msg,
1435 mrp_sockaddr_t *addr, socklen_t addrlen,
1438 struct userdata *u = (struct userdata *)void_u;
1440 pa_murphyif *murphyif;
1441 resource_interface *rif;
1446 resource_request *req, *n;
1451 MRP_UNUSED(addrlen);
1454 pa_assert_se((core = u->core));
1455 pa_assert_se((murphyif = u->murphyif));
1457 rif = &murphyif->resource;
1459 if (!resource_fetch_seqno (msg, &curs, &seqno) ||
1460 !resource_fetch_request (msg, &curs, &reqid) )
1462 pa_log("ignoring malformed message");
1466 PA_LLIST_FOREACH_SAFE(req, n, rif->reqs) {
1467 if (req->seqno <= seqno) {
1468 nodidx = req->nodidx;
1470 if (req->reqid == reqid) {
1471 PA_LLIST_REMOVE(resource_request, rif->reqs, req);
1475 if (!(node = mir_node_find_by_index(u, nodidx))) {
1476 if (reqid != RESPROTO_DESTROY_RESOURCE_SET) {
1477 pa_log("got response (reqid:%u seqno:%u) but can't "
1478 "find the corresponding node", reqid, seqno);
1479 resource_set_create_response_abort(u, msg, &curs);
1483 if (req->seqno < seqno) {
1484 pa_log("unanswered request %d", req->seqno);
1487 pa_log_debug("got response (reqid:%u seqno:%u "
1488 "node:'%s')", reqid, seqno,
1489 node ? node->amname : "<unknown>");
1492 case RESPROTO_CREATE_RESOURCE_SET:
1493 resource_set_create_response(u, node, msg, &curs);
1495 case RESPROTO_DESTROY_RESOURCE_SET:
1498 pa_log("ignoring unsupported resource request "
1504 } /* PA_LLIST_FOREACH_SAFE */
1509 static void resource_set_create_response(struct userdata *u, mir_node *node,
1510 mrp_msg_t *msg, void **pcursor)
1521 if (!resource_fetch_status(msg, pcursor, &status) || (status == 0 &&
1522 !resource_fetch_rset_id(msg, pcursor, &rsetid)))
1524 pa_log("ignoring malformed response to resource set creation");
1529 pa_log("creation of resource set failed. error code %u", status);
1533 node->rsetid = pa_sprintf_malloc("%d", rsetid);
1535 if (pa_murphyif_add_node(u, node) == 0) {
1536 pa_log_debug("resource set was successfully created");
1537 mir_node_print(node, buf, sizeof(buf));
1538 pa_log_debug("modified node:\n%s", buf);
1541 pa_log("failed to create resource set: "
1542 "conflicting resource set id");
1546 static void resource_set_create_response_abort(struct userdata *u,
1547 mrp_msg_t *msg, void **pcursor)
1556 if (!resource_fetch_status(msg, pcursor, &status) || (status == 0 &&
1557 !resource_fetch_rset_id(msg, pcursor, &rsetid)))
1559 pa_log("ignoring malformed response to resource set creation");
1564 pa_log("creation of resource set failed. error code %u", status);
1568 if (resource_set_destroy_node(u, rsetid))
1569 pa_log_debug("destroying resource set %u", rsetid);
1571 pa_log("attempt to destroy resource set %u failed", rsetid);
1575 static bool resource_fetch_seqno(mrp_msg_t *msg,
1581 mrp_msg_value_t value;
1584 if (!mrp_msg_iterate(msg, pcursor, &tag, &type, &value, &size) ||
1585 tag != RESPROTO_SEQUENCE_NO || type != MRP_MSG_FIELD_UINT32)
1587 *pseqno = INVALID_SEQNO;
1591 *pseqno = value.u32;
1596 static bool resource_fetch_request(mrp_msg_t *msg,
1602 mrp_msg_value_t value;
1605 if (!mrp_msg_iterate(msg, pcursor, &tag, &type, &value, &size) ||
1606 tag != RESPROTO_REQUEST_TYPE || type != MRP_MSG_FIELD_UINT16)
1608 *preqtype = INVALID_REQUEST;
1612 *preqtype = value.u16;
1616 static bool resource_fetch_status(mrp_msg_t *msg,
1622 mrp_msg_value_t value;
1625 if (!mrp_msg_iterate(msg, pcursor, &tag, &type, &value, &size) ||
1626 tag != RESPROTO_REQUEST_STATUS || type != MRP_MSG_FIELD_SINT16)
1632 *pstatus = value.s16;
1636 static bool resource_fetch_rset_id(mrp_msg_t *msg,
1642 mrp_msg_value_t value;
1645 if (!mrp_msg_iterate(msg, pcursor, &tag, &type, &value, &size) ||
1646 tag != RESPROTO_RESOURCE_SET_ID || type != MRP_MSG_FIELD_UINT32)
1656 static bool resource_fetch_rset_state(mrp_msg_t *msg,
1658 mrp_resproto_state_t *pstate)
1662 mrp_msg_value_t value;
1665 if (!mrp_msg_iterate(msg, pcursor, &tag, &type, &value, &size) ||
1666 tag != RESPROTO_RESOURCE_STATE || type != MRP_MSG_FIELD_UINT16)
1672 *pstate = value.u16;
1677 static bool resource_fetch_rset_mask(mrp_msg_t *msg,
1679 mrp_resproto_state_t *pmask)
1683 mrp_msg_value_t value;
1686 if (!mrp_msg_iterate(msg, pcursor, &tag, &type, &value, &size) ||
1687 tag != RESPROTO_RESOURCE_GRANT || type != MRP_MSG_FIELD_UINT32)
1697 static bool resource_transport_create(struct userdata *u,
1698 pa_murphyif *murphyif)
1700 static mrp_transport_evt_t ev = {
1701 { .recvmsg = resource_recv_msg },
1702 { .recvmsgfrom = resource_recvfrom_msg },
1703 .closed = resource_xport_closed_evt,
1707 resource_interface *rif;
1710 pa_assert(murphyif);
1712 rif = &murphyif->resource;
1715 rif->transp = mrp_transport_create(murphyif->ml, rif->atype, &ev, u,0);
1717 return rif->transp ? true : false;
1720 static void resource_transport_destroy(pa_murphyif *murphyif)
1722 resource_interface *rif;
1724 pa_assert(murphyif);
1725 rif = &murphyif->resource;
1728 mrp_transport_destroy(rif->transp);
1731 rif->connected = false;
1734 static void connect_attempt(pa_mainloop_api *a,
1736 const struct timeval *t,
1739 struct userdata *u = (struct userdata *)data;
1740 pa_murphyif *murphyif;
1741 resource_interface *rif;
1746 pa_assert_se((murphyif = u->murphyif));
1748 rif = &murphyif->resource;
1750 if (!resource_transport_create(u, murphyif))
1751 schedule_connect(u, rif);
1753 state = resource_transport_connect(rif);
1758 resource_set_create_all(u);
1759 cancel_schedule(u, rif);
1763 cancel_schedule(u, rif);
1767 schedule_connect(u, rif);
1773 static void schedule_connect(struct userdata *u, resource_interface *rif)
1776 pa_mainloop_api *mainloop;
1777 struct timeval when;
1782 pa_assert_se((core = u->core));
1783 pa_assert_se((mainloop = core->mainloop));
1785 pa_gettimeofday(&when);
1786 pa_timeval_add(&when, rif->connect.period);
1788 if ((tev = rif->connect.evt))
1789 mainloop->time_restart(tev, &when);
1791 rif->connect.evt = mainloop->time_new(mainloop, &when,
1792 connect_attempt, u);
1796 static void cancel_schedule(struct userdata *u, resource_interface *rif)
1799 pa_mainloop_api *mainloop;
1804 pa_assert_se((core = u->core));
1805 pa_assert_se((mainloop = core->mainloop));
1807 if ((tev = rif->connect.evt)) {
1808 mainloop->time_free(tev);
1809 rif->connect.evt = NULL;
1813 static rset_hash *node_put_rset(struct userdata *u, mir_node *node, rset_data *rset)
1815 pa_murphyif *murphyif;
1816 resource_interface *rif;
1823 pa_assert(rset->id);
1825 pa_assert(node->implement == mir_stream);
1826 pa_assert(node->direction == mir_input || node->direction == mir_output);
1828 pa_assert_se((murphyif = u->murphyif));
1829 rif = &murphyif->resource;
1831 pa_log_debug("setting rsetid %s for node %s", rset->id, node->amname);
1834 pa_xfree(node->rsetid);
1836 node->rsetid = pa_xstrdup(rset->id);
1838 if (!(pl = get_node_proplist(u, node))) {
1839 pa_log("can't obtain property list for node %s", node->amname);
1843 if ((pa_proplist_sets(pl, PA_PROP_RESOURCE_SET_ID, node->rsetid) < 0)) {
1844 pa_log("failed to set '" PA_PROP_RESOURCE_SET_ID "' property "
1845 "of '%s' node", node->amname);
1849 if (!(rh = rset_hashmap_put(u, node->rsetid, node))) {
1850 pa_log("conflicting rsetid %s for %s", node->rsetid, node->amname);
1857 static void node_enforce_resource_policy(struct userdata *u,
1865 pa_assert(rset->policy);
1868 if (pa_streq(rset->policy, "relaxed"))
1869 req = PA_STREAM_RUN;
1870 else if (pa_streq(rset->policy, "strict")) {
1871 if (rset->state == RSET_RELEASE && rset->autorel)
1872 req = PA_STREAM_KILL;
1875 req = PA_STREAM_RUN;
1877 req = PA_STREAM_BLOCK;
1881 req = PA_STREAM_BLOCK;
1884 pa_stream_state_change(u, node, req);
1887 static rset_data *rset_data_dup(rset_data *orig)
1892 pa_assert(orig->id);
1893 pa_assert(orig->policy);
1895 dup = pa_xnew0(rset_data, 1);
1897 dup->id = pa_xstrdup(orig->id);
1898 dup->autorel = orig->autorel;
1899 dup->state = orig->state;
1900 dup->grant = orig->grant;
1901 dup->policy = pa_xstrdup(orig->policy);
1906 static void rset_data_copy(rset_data *dst, rset_data *src)
1913 pa_assert(src->policy);
1915 pa_xfree((void *)dst->id);
1916 pa_xfree((void *)dst->policy);
1918 dst->id = pa_xstrdup(src->id);
1919 dst->autorel = src->autorel;
1920 dst->state = src->state;
1921 dst->grant = src->grant;
1922 dst->policy = pa_xstrdup(src->policy);
1926 static void rset_data_update(rset_data *dst, rset_data *src)
1934 pa_assert(src->policy);
1936 pa_assert_se(pa_streq(src->id, dst->id));
1938 pa_xfree((void *)dst->policy);
1940 dst->autorel = src->autorel;
1941 dst->state = src->state;
1942 dst->grant = src->grant;
1943 dst->policy = pa_xstrdup(src->policy);
1947 static void rset_data_free(rset_data *rset)
1950 pa_xfree((void *)rset->id);
1951 pa_xfree((void *)rset->policy);
1956 static void pid_hashmap_free(void *p, void *userdata)
1958 pid_hash *ph = (pid_hash *)p;
1963 pa_xfree((void *)ph->pid);
1964 rset_data_free(ph->rset);
1969 static int pid_hashmap_put(struct userdata *u, const char *pid,
1970 mir_node *node, rset_data *rset)
1972 pa_murphyif *murphyif;
1973 resource_interface *rif;
1978 pa_assert(node || rset);
1979 pa_assert_se((murphyif = u->murphyif));
1981 rif = &murphyif->resource;
1983 ph = pa_xnew0(pid_hash, 1);
1984 ph->pid = pa_xstrdup(pid);
1988 if (pa_hashmap_put(rif->nodes.pid, (void *)ph->pid, ph) == 0)
1991 pid_hashmap_free(ph, NULL);
1996 static mir_node *pid_hashmap_get_node(struct userdata *u, const char *pid)
1998 pa_murphyif *murphyif;
1999 resource_interface *rif;
2004 pa_assert(murphyif = u->murphyif);
2006 rif = &murphyif->resource;
2008 if ((ph = pa_hashmap_get(rif->nodes.pid, pid)))
2014 static rset_data *pid_hashmap_get_rset(struct userdata *u, const char *pid)
2016 pa_murphyif *murphyif;
2017 resource_interface *rif;
2022 pa_assert(murphyif = u->murphyif);
2024 rif = &murphyif->resource;
2026 if ((ph = pa_hashmap_get(rif->nodes.pid, pid)))
2032 static mir_node *pid_hashmap_remove_node(struct userdata *u, const char *pid)
2034 pa_murphyif *murphyif;
2035 resource_interface *rif;
2040 pa_assert_se((murphyif = u->murphyif));
2042 rif = &murphyif->resource;
2044 if (!(ph = pa_hashmap_remove(rif->nodes.pid, pid)))
2046 else if (!(node = ph->node))
2047 pa_hashmap_put(rif->nodes.pid, (void *)ph->pid, ph);
2049 pid_hashmap_free(ph, NULL);
2054 static rset_data *pid_hashmap_remove_rset(struct userdata *u, const char *pid)
2056 pa_murphyif *murphyif;
2057 resource_interface *rif;
2064 pa_assert_se((murphyif = u->murphyif));
2066 rif = &murphyif->resource;
2068 if (!(ph = pa_hashmap_remove(rif->nodes.pid, pid)))
2070 else if (!(rset = ph->rset))
2071 pa_hashmap_put(rif->nodes.pid, (void *)ph->pid, ph);
2074 pid_hashmap_free(ph, NULL);
2081 static void rset_hashmap_free(void *r, void *userdata)
2083 rset_hash *rh = (rset_hash *)r;
2088 pa_xfree(rh->nodes);
2089 rset_data_free(rh->rset);
2094 static rset_hash *rset_hashmap_put(struct userdata *u,
2098 pa_murphyif *murphyif;
2099 resource_interface *rif;
2107 pa_assert_se((murphyif = u->murphyif));
2109 rif = &murphyif->resource;
2111 if ((rh = pa_hashmap_get(rif->nodes.rsetid, rsetid))) {
2112 for (i = 0; i < rh->nnode; i++) {
2113 if (rh->nodes[i] == node)
2118 rh->nodes = pa_xrealloc(rh->nodes, sizeof(mir_node *) * (rh->nnode+1));
2121 rset = pa_xnew0(rset_data, 1);
2123 rset->id = pa_xstrdup(rsetid);
2124 rset->policy = pa_xstrdup("unknown");
2126 rh = pa_xnew0(rset_hash, 1);
2129 rh->nodes = pa_xnew0(mir_node *, 2);
2132 pa_hashmap_put(rif->nodes.rsetid, (void *)rh->rset->id, rh);
2138 rh->nodes[i+0] = node;
2139 rh->nodes[i+1] = NULL;
2144 static rset_hash *rset_hashmap_get(struct userdata *u, const char *rsetid)
2146 pa_murphyif *murphyif;
2147 resource_interface *rif;
2152 pa_assert(murphyif = u->murphyif);
2154 rif = &murphyif->resource;
2156 if ((rh = pa_hashmap_get(rif->nodes.rsetid, rsetid)))
2162 static int rset_hashmap_remove(struct userdata *u,
2166 pa_murphyif *murphyif;
2167 resource_interface *rif;
2172 pa_assert_se((murphyif = u->murphyif));
2174 rif = &murphyif->resource;
2176 if ((rh = pa_hashmap_get(rif->nodes.rsetid, rsetid))) {
2178 for (i = 0; i < rh->nnode; i++) {
2179 if (node == rh->nodes[i]) {
2180 if (rh->nnode <= 1) {
2181 pa_hashmap_remove(rif->nodes.rsetid, rsetid);
2182 rset_hashmap_free(rh, NULL);
2186 for (j = i; j < rh->nnode; j++)
2187 rh->nodes[j] = rh->nodes[j+1];
2202 static pa_proplist *get_node_proplist(struct userdata *u, mir_node *node)
2206 pa_source_output *o;
2210 pa_assert_se((core = u->core));
2212 if (node->implement == mir_stream && node->paidx != PA_IDXSET_INVALID) {
2213 if (node->direction == mir_input) {
2214 if ((i = pa_idxset_get_by_index(core->sink_inputs, node->paidx)))
2217 else if (node->direction == mir_output) {
2218 if ((o = pa_idxset_get_by_index(core->source_outputs,node->paidx)))
2226 static const char *get_node_pid(struct userdata *u, mir_node *node)
2232 if (node && (pl = get_node_proplist(u, node)))
2233 return pa_proplist_gets(pl, PA_PROP_APPLICATION_PROCESS_ID);
2241 * indent-tabs-mode: nil