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 cancel_schedule(u, rif);
403 pa_xfree((void *)rif->addr);
404 pa_xfree((void *)rif->inpres.name);
405 pa_xfree((void *)rif->outres.name);
407 mrp_mainloop_destroy(murphyif->ml);
414 void pa_murphyif_add_table(struct userdata *u,
419 pa_murphyif *murphyif;
420 domctl_interface *dif;
421 mrp_domctl_table_t *t;
428 pa_assert_se((murphyif = u->murphyif));
430 dif = &murphyif->domctl;
433 size = sizeof(mrp_domctl_table_t) * dif->ntable;
434 t = (dif->tables = pa_xrealloc(dif->tables, size)) + idx;
436 t->table = pa_xstrdup(table);
437 t->mql_columns = pa_xstrdup(columns);
438 t->mql_index = index ? pa_xstrdup(index) : NULL;
441 int pa_murphyif_add_watch(struct userdata *u,
447 pa_murphyif *murphyif;
448 domctl_interface *dif;
449 mrp_domctl_watch_t *w;
456 pa_assert(max_rows > 0 && max_rows < MQI_QUERY_RESULT_MAX);
457 pa_assert_se((murphyif = u->murphyif));
459 dif = &murphyif->domctl;
462 size = sizeof(mrp_domctl_watch_t) * dif->nwatch;
463 w = (dif->watches = pa_xrealloc(dif->watches, size)) + idx;
465 w->table = pa_xstrdup(table);
466 w->mql_columns = pa_xstrdup(columns);
467 w->mql_where = where ? pa_xstrdup(where) : NULL;
468 w->max_rows = max_rows;
473 void pa_murphyif_setup_domainctl(struct userdata *u, pa_murphyif_watch_cb wcb)
475 static const char *name = "pulse";
477 pa_murphyif *murphyif;
478 domctl_interface *dif;
482 pa_assert_se((murphyif = u->murphyif));
484 dif = &murphyif->domctl;
487 if (dif->ntable || dif->nwatch) {
488 dif->ctl = mrp_domctl_create(name, murphyif->ml,
489 dif->tables, dif->ntable,
490 dif->watches, dif->nwatch,
491 domctl_connect_notify,
492 domctl_watch_notify, u);
494 pa_log("failed to create '%s' domain controller", name);
498 if (!mrp_domctl_connect(dif->ctl, dif->addr, 0)) {
499 pa_log("failed to conect to murphyd");
504 pa_log_info("'%s' domain controller sucessfully created", name);
509 void pa_murphyif_add_audio_resource(struct userdata *u,
514 static const char *columns = RESCOL_NAMES;
515 static int maxrow = MQI_QUERY_RESULT_MAX - 1;
517 pa_murphyif *murphyif;
518 resource_interface *rif;
519 audio_resource_t *res;
523 pa_assert(dir == mir_input || dir == mir_output);
526 pa_assert_se((murphyif = u->murphyif));
527 rif = &murphyif->resource;
530 if (dir == mir_input) {
531 if (rif->inpres.name)
532 pa_log("attempt to register playback resource multiple time");
537 if (rif->outres.name)
538 pa_log("attempt to register recording resource multiple time");
544 res->name = pa_xstrdup(name);
546 snprintf(table, sizeof(table), "%s_users", name);
547 res->tblidx = pa_murphyif_add_watch(u, table, columns, NULL, maxrow);
552 void pa_murphyif_add_audio_attribute(struct userdata *u,
555 mqi_data_type_t type,
556 ... ) /* default value */
558 #ifdef WITH_RESOURCES
559 pa_murphyif *murphyif;
560 resource_interface *rif;
561 resource_attribute *attr;
562 mrp_attr_value_t *val;
568 pa_assert(type == mqi_string || type == mqi_integer ||
569 type == mqi_unsignd || type == mqi_floating);
571 pa_assert_se((murphyif = u->murphyif));
572 rif = &murphyif->resource;
574 attr = pa_xnew0(resource_attribute, 1);
575 val = &attr->def.value;
577 attr->prop = pa_xstrdup(propnam);
578 attr->def.name = pa_xstrdup(attrnam);
579 attr->def.type = type;
584 case mqi_string: val->string = pa_xstrdup(va_arg(ap, char *)); break;
585 case mqi_integer: val->integer = va_arg(ap, int32_t); break;
586 case mqi_unsignd: val->unsignd = va_arg(ap, uint32_t); break;
587 case mqi_floating: val->floating = va_arg(ap, double); break;
588 default: attr->def.type = mqi_error; break;
593 if (attr->def.type == mqi_error)
594 resource_attribute_destroy(rif, attr);
596 PA_LLIST_PREPEND(resource_attribute, rif->attrs, attr);
600 void pa_murphyif_create_resource_set(struct userdata *u,
602 pa_nodeset_resdef *resdef)
605 pa_murphyif *murphyif;
606 resource_interface *rif;
611 pa_assert((!node->loop && node->implement == mir_stream) ||
612 ( node->loop && node->implement == mir_device) );
613 pa_assert(node->direction == mir_input || node->direction == mir_output);
614 pa_assert(node->zone);
615 pa_assert(!node->rsetid);
617 pa_assert_se((core = u->core));
619 pa_assert_se((murphyif = u->murphyif));
620 rif = &murphyif->resource;
622 state = resource_transport_connect(rif);
627 resource_set_create_all(u);
631 node->localrset = resource_set_create_node(u, node, resdef, true);
639 void pa_murphyif_destroy_resource_set(struct userdata *u, mir_node *node)
641 pa_murphyif *murphyif;
647 pa_assert_se((murphyif = u->murphyif));
649 if (node->localrset && node->rsetid) {
651 pa_murphyif_delete_node(u, node);
653 rsetid = strtoul(node->rsetid, &e, 10);
655 if (e == node->rsetid || *e) {
656 pa_log("can't destroy resource set: invalid rsetid '%s'",
660 if (rset_hashmap_remove(u, node->rsetid, node) < 0) {
661 pa_log_debug("failed to remove resource set %s from hashmap",
665 if (resource_set_destroy_node(u, rsetid))
666 pa_log_debug("sent resource set %u destruction request", rsetid);
668 pa_log("failed to destroy resourse set %u for node '%s'",
669 rsetid, node->amname);
672 pa_xfree(node->rsetid);
674 node->localrset = false;
680 int pa_murphyif_add_node(struct userdata *u, mir_node *node)
682 #ifdef WITH_RESOURCES
683 pa_murphyif *murphyif;
684 resource_interface *rif;
693 pa_assert_se((murphyif = u->murphyif));
695 rif = &murphyif->resource;
698 pa_log("can't register resource set for node %u '%s'.: missing rsetid",
699 node->paidx, node->amname);
701 else if (pa_streq(node->rsetid, PA_RESOURCE_SET_ID_PID)) {
702 if (!(pid = get_node_pid(u,node)))
703 pa_log("can't obtain PID for node '%s'", node->amname);
705 if (pid_hashmap_put(u, pid, node, NULL) == 0)
708 if ((rset = pid_hashmap_remove_rset(u, pid))) {
709 pa_log_debug("found resource-set %s for node '%s'",
710 rset->id, node->amname);
712 if (node_put_rset(u, node, rset)) {
713 node_enforce_resource_policy(u, node, rset);
714 rset_data_free(rset);
718 pa_log("can't register resource set for node '%s': "
719 "failed to set rsetid", node->amname);
721 rset_data_free(rset);
724 pa_log("can't register resource set for node '%s': "
725 "conflicting pid", node->amname);
730 if ((rh = rset_hashmap_put(u, node->rsetid, node))) {
733 pa_log_debug("enforce policies on node %u '%s' rsetid:%s autorel:%s "
734 "state:%s grant:%s policy:%s", node->paidx, node->amname,
735 rset->id, rset->autorel ? "yes":"no",
736 rset->state == RSET_ACQUIRE ? "acquire":"release",
737 rset->grant ? "yes":"no", rset->policy);
739 node_enforce_resource_policy(u, node, rset);
750 void pa_murphyif_delete_node(struct userdata *u, mir_node *node)
752 #ifdef WITH_RESOURCES
753 pa_murphyif *murphyif;
754 resource_interface *rif;
761 pa_assert_se((murphyif = u->murphyif));
763 rif = &murphyif->resource;
766 if (pa_streq(node->rsetid, PA_RESOURCE_SET_ID_PID)) {
767 if ((pid = get_node_pid(u, node))) {
768 if (node == pid_hashmap_get_node(u, pid))
769 pid_hashmap_remove_node(u, pid);
771 pa_log("pid %s seems to have multiple resource sets. "
772 "Refuse to delete node %u (%s) from hashmap",
773 pid, node->index, node->amname);
778 if (rset_hashmap_remove(u, node->rsetid, node) < 0) {
779 pa_log("failed to remove node '%s' from rset hash", node->amname);
788 static void domctl_connect_notify(mrp_domctl_t *dc, int connected, int errcode,
789 const char *errmsg, void *user_data)
792 MRP_UNUSED(user_data);
795 pa_log_info("Successfully registered to Murphy.");
797 pa_log_error("Domain control Connection to Murphy failed (%d: %s).",
802 static void domctl_watch_notify(mrp_domctl_t *dc, mrp_domctl_data_t *tables,
803 int ntable, void *user_data)
805 struct userdata *u = (struct userdata *)user_data;
806 pa_murphyif *murphyif;
807 domctl_interface *dif;
808 resource_interface *rif;
809 mrp_domctl_data_t *t;
810 mrp_domctl_watch_t *w;
816 pa_assert(ntable > 0);
818 pa_assert_se((murphyif = u->murphyif));
820 dif = &murphyif->domctl;
821 rif = &murphyif->resource;
823 pa_log_info("Received change notification for %d tables.", ntable);
825 for (i = 0; i < ntable; i++) {
830 pa_assert(t->id >= 0);
831 pa_assert(t->id < dif->nwatch);
833 w = dif->watches + t->id;
835 #ifdef WITH_RESOURCES
836 if (t->id == rif->inpres.tblidx || t->id == rif->outres.tblidx) {
837 resource_set_notification(u, w->table, t->nrow, t->rows);
842 dif->watchcb(u, w->table, t->nrow, t->rows);
846 static void domctl_dump_data(mrp_domctl_data_t *table)
848 mrp_domctl_value_t *row;
854 pa_log_debug("Table #%d: %d rows x %d columns", table->id,
855 table->nrow, table->ncolumn);
857 for (i = 0; i < table->nrow; i++) {
858 row = table->rows[i];
862 for (j = 0, t = ""; j < table->ncolumn; j++, t = ", ") {
863 switch (row[j].type) {
864 case MRP_DOMCTL_STRING:
865 l = snprintf(p, n, "%s'%s'", t, row[j].str);
869 case MRP_DOMCTL_INTEGER:
870 l = snprintf(p, n, "%s%d", t, row[j].s32);
874 case MRP_DOMCTL_UNSIGNED:
875 l = snprintf(p, n, "%s%u", t, row[j].u32);
879 case MRP_DOMCTL_DOUBLE:
880 l = snprintf(p, n, "%s%f", t, row[j].dbl);
885 l = snprintf(p, n, "%s<invalid column 0x%x>",
892 pa_log_debug("row #%d: { %s }", i, buf);
897 #ifdef WITH_RESOURCES
898 static void resource_attribute_destroy(resource_interface *rif,
899 resource_attribute *attr)
903 PA_LLIST_REMOVE(resource_attribute, rif->attrs, attr);
905 pa_xfree((void *)attr->prop);
906 pa_xfree((void *)attr->def.name);
908 if (attr->def.type == mqi_string)
909 pa_xfree((void *)attr->def.value.string);
915 static int resource_transport_connect(resource_interface *rif)
924 if (!mrp_transport_connect(rif->transp, &rif->saddr, rif->alen))
925 status = DISCONNECTED;
927 pa_log_info("resource transport connected to '%s'", rif->addr);
928 rif->connected = true;
936 static void resource_xport_closed_evt(mrp_transport_t *transp, int error,
939 struct userdata *u = (struct userdata *)void_u;
940 pa_murphyif *murphyif;
941 resource_interface *rif;
946 pa_assert_se((murphyif = u->murphyif));
948 rif = &murphyif->resource;
951 pa_log("Resource transport connection closed by peer");
953 pa_log("Resource transport connection closed with error %d (%s)",
954 error, strerror(error));
957 resource_transport_destroy(murphyif);
958 resource_set_destroy_all(u);
959 schedule_connect(u, rif);
962 static mrp_msg_t *resource_create_request(uint32_t seqno,
963 mrp_resproto_request_t req)
968 msg = mrp_msg_create(RESPROTO_SEQUENCE_NO , MRP_MSG_FIELD_UINT32, seqno,
969 RESPROTO_REQUEST_TYPE, MRP_MSG_FIELD_UINT16, type ,
970 RESPROTO_MESSAGE_END );
973 pa_log("can't to create new resource message");
978 static bool resource_send_message(resource_interface *rif,
984 resource_request *req;
987 if (!mrp_transport_send(rif->transp, msg)) {
988 pa_log("failed to send resource message");
992 req = pa_xnew0(resource_request, 1);
993 req->nodidx = nodidx;
997 PA_LLIST_PREPEND(resource_request, rif->reqs, req);
1005 static bool resource_set_create_node(struct userdata *u,
1007 pa_nodeset_resdef *resdef,
1011 pa_murphyif *murphyif;
1012 resource_interface *rif;
1013 resource_request *req;
1017 uint32_t rset_flags;
1019 pa_nodeset_map *map;
1022 pa_sink_input *sinp;
1023 pa_source_output *sout;
1024 audio_resource_t *res;
1026 mir_node_type type = 0;
1027 uint32_t audio_flags = 0;
1029 pa_proplist *proplist = NULL;
1030 bool success = true;
1034 pa_assert(node->index != PA_IDXSET_INVALID);
1035 pa_assert((!node->loop && node->implement == mir_stream) ||
1036 ( node->loop && node->implement == mir_device) );
1037 pa_assert(node->direction == mir_input || node->direction == mir_output);
1038 pa_assert(node->zone);
1039 pa_assert(!node->rsetid);
1041 pa_assert_se((core = u->core));
1043 if ((loop = node->loop)) {
1044 if (node->direction == mir_input) {
1045 sout = pa_idxset_get_by_index(core->source_outputs,
1046 loop->source_output_index);
1048 proplist = sout->proplist;
1051 sinp = pa_idxset_get_by_index(core->sink_inputs,
1052 loop->sink_input_index);
1054 proplist = sinp->proplist;
1056 if (proplist && (role = pa_proplist_gets(proplist, PA_PROP_MEDIA_ROLE))) {
1057 if ((map = pa_nodeset_get_map_by_role(u, role)))
1062 if (node->direction == mir_output) {
1063 if ((sout = pa_idxset_get_by_index(core->source_outputs, node->paidx)))
1064 proplist = sout->proplist;
1067 if ((sinp = pa_idxset_get_by_index(core->sink_inputs, node->paidx)))
1068 proplist = sinp->proplist;
1073 pa_assert_se((class = pa_nodeset_get_class(u, type)));
1074 pa_assert_se((murphyif = u->murphyif));
1075 rif = &murphyif->resource;
1077 reqid = RESPROTO_CREATE_RESOURCE_SET;
1078 seqno = rif->seqno.request++;
1079 res = (node->direction == mir_input) ? &rif->inpres : &rif->outres;
1081 pa_assert_se((resnam = res->name));
1083 rset_flags = RESPROTO_RSETFLAG_NOEVENTS;
1084 rset_flags |= (acquire ? RESPROTO_RSETFLAG_AUTOACQUIRE : 0);
1085 rset_flags |= (resdef ? resdef->flags.rset : 0);
1087 audio_flags = (resdef ? resdef->flags.audio : 0);
1089 priority = (resdef ? resdef->priority : 0);
1091 if (!(msg = resource_create_request(seqno, reqid)))
1094 if (PUSH_VALUE(msg, RESOURCE_FLAGS , UINT32, rset_flags) &&
1095 PUSH_VALUE(msg, RESOURCE_PRIORITY, UINT32, priority) &&
1096 PUSH_VALUE(msg, CLASS_NAME , STRING, class) &&
1097 PUSH_VALUE(msg, ZONE_NAME , STRING, node->zone) &&
1098 PUSH_VALUE(msg, RESOURCE_NAME , STRING, resnam) &&
1099 PUSH_VALUE(msg, RESOURCE_FLAGS , UINT32, audio_flags) &&
1100 PUSH_VALUE(msg, ATTRIBUTE_NAME , STRING, "policy") &&
1101 PUSH_VALUE(msg, ATTRIBUTE_VALUE , STRING, "strict") &&
1102 PUSH_ATTRS(msg, rif, proplist) &&
1103 PUSH_VALUE(msg, SECTION_END , UINT8 , 0) )
1105 success = resource_send_message(rif, msg, node->index, reqid, seqno);
1113 pa_log_debug("requested resource set for '%s'", node->amname);
1115 pa_log_debug("failed to create resource set for '%s'", node->amname);
1120 static bool resource_set_create_all(struct userdata *u)
1130 idx = PA_IDXSET_INVALID;
1132 while ((node = pa_nodeset_iterate_nodes(u, &idx))) {
1133 if ((node->implement == mir_stream && !node->loop) ||
1134 (node->implement == mir_device && node->loop) )
1136 if (!node->rsetid) {
1137 node->localrset = resource_set_create_node(u, node, NULL, false);
1138 success &= node->localrset;
1146 static bool resource_set_destroy_node(struct userdata *u, uint32_t rsetid)
1148 pa_murphyif *murphyif;
1149 resource_interface *rif;
1158 pa_assert_se((murphyif = u->murphyif));
1159 rif = &murphyif->resource;
1161 reqid = RESPROTO_DESTROY_RESOURCE_SET;
1162 seqno = rif->seqno.request++;
1163 nodidx = PA_IDXSET_INVALID;
1164 msg = resource_create_request(seqno, reqid);
1166 if (PUSH_VALUE(msg, RESOURCE_SET_ID, UINT32, rsetid))
1167 success = resource_send_message(rif, msg, nodidx, reqid, seqno);
1176 static bool resource_set_destroy_all(struct userdata *u)
1178 pa_murphyif *murphyif;
1179 resource_interface *rif;
1187 pa_assert_se((murphyif = u->murphyif));
1189 rif = &murphyif->resource;
1193 idx = PA_IDXSET_INVALID;
1195 while ((node = pa_nodeset_iterate_nodes(u, &idx))) {
1196 if (node->implement == mir_stream && node->localrset) {
1197 pa_log_debug("destroying resource set for '%s'", node->amname);
1199 if (rif->connected && node->rsetid) {
1200 rsetid = strtoul(node->rsetid, &e, 10);
1202 if (e == node->rsetid || *e)
1205 rset_hashmap_remove(u, node->rsetid, node);
1206 success &= resource_set_destroy_node(u, rsetid);
1210 pa_xfree(node->rsetid);
1212 node->localrset = false;
1213 node->rsetid = NULL;
1220 static void resource_set_notification(struct userdata *u,
1223 mrp_domctl_value_t **values)
1225 pa_murphyif *murphyif;
1226 resource_interface *rif;
1228 mrp_domctl_value_t *row;
1229 mrp_domctl_value_t *crsetid;
1230 mrp_domctl_value_t *cautorel;
1231 mrp_domctl_value_t *cstate;
1232 mrp_domctl_value_t *cgrant;
1233 mrp_domctl_value_t *cpid;
1234 mrp_domctl_value_t *cpolicy;
1237 mir_node *node, **nodes;
1239 rset_data rset, *rs;
1245 pa_assert_se((murphyif = u->murphyif));
1246 rif = &murphyif->resource;
1248 for (r = 0; r < nrow; r++) {
1250 crsetid = row + RESCOL_RSETID;
1251 cautorel = row + RESCOL_AUTOREL;
1252 cstate = row + RESCOL_STATE;
1253 cgrant = row + RESCOL_GRANT;
1254 cpid = row + RESCOL_PID;
1255 cpolicy = row + RESCOL_POLICY;
1257 if (crsetid->type != MRP_DOMCTL_UNSIGNED ||
1258 cautorel->type != MRP_DOMCTL_INTEGER ||
1259 cstate->type != MRP_DOMCTL_INTEGER ||
1260 cgrant->type != MRP_DOMCTL_INTEGER ||
1261 cpid->type != MRP_DOMCTL_STRING ||
1262 cpolicy->type != MRP_DOMCTL_STRING )
1264 pa_log("invalid field type in '%s' (%d|%d|%d|%d|%d|%d)", table,
1265 crsetid->type, cautorel->type, cstate->type,
1266 cgrant->type, cpid->type, cpolicy->type);
1270 snprintf(rsetid, sizeof(rsetid), "%d", crsetid->s32);
1274 rset.autorel = cautorel->s32;
1275 rset.state = cstate->s32;
1276 rset.grant = cgrant->s32;
1277 rset.policy = cpolicy->str;
1280 if (rset.autorel != 0 && rset.autorel != 1) {
1281 pa_log_debug("invalid autorel %d in table '%s'",
1282 rset.autorel, table);
1285 if (rset.state != RSET_RELEASE && rset.state != RSET_ACQUIRE) {
1286 pa_log_debug("invalid state %d in table '%s'", rset.state, table);
1289 if (rset.grant != 0 && rset.grant != 1) {
1290 pa_log_debug("invalid grant %d in table '%s'", rset.grant, table);
1294 if (!(rh = rset_hashmap_get(u, rset.id))) {
1296 pa_log_debug("can't find node for resource set %s "
1297 "(pid in resource set unknown)", rset.id);
1301 if ((node = pid_hashmap_remove_node(u, pid))) {
1302 pa_log_debug("found node %s for resource-set '%s'",
1303 node->amname, rset.id);
1305 if (!(rh = node_put_rset(u, node, &rset))) {
1306 pa_log("can't register resource set for node '%s': "
1307 "failed to set rsetid", node->amname);
1312 if (pid_hashmap_put(u, pid, NULL, rset_data_dup(&rset)) < 0) {
1313 if (!(rs = pid_hashmap_get_rset(u, pid)))
1314 pa_log("failed to add resource set to pid hash");
1316 if (!pa_streq(rs->id, rset.id)) {
1317 pa_log("process %s appears to have multiple resour"
1318 "ce sets (%s and %s)", pid, rs->id,rset.id);
1320 pa_log_debug("update resource-set %s data in "
1321 "pid hash (pid %s)", rs->id, pid);
1322 rset_data_copy(rs, &rset);
1326 pa_log_debug("can't find node for resource set %s. "
1327 "Beleive the stream will appear later on",
1335 rset_data_update(rh->rset, &rset);
1337 /* we need to make a copy of this as node_enforce_resource_policy()
1338 will delete/modify it */
1339 size = sizeof(mir_node *) * (rh->nnode + 1);
1340 nodes = alloca(size);
1341 memcpy(nodes, rh->nodes, size);
1343 for (i = 0; (node = nodes[i]); i++) {
1344 pa_log_debug("%u: resource notification for node '%s' autorel:%s "
1345 "state:%s grant:%s pid:%s policy:%s", i,
1346 node->amname, rset.autorel ? "yes":"no",
1347 rset.state == RSET_ACQUIRE ? "acquire":"release",
1348 rset.grant ? "yes":"no", pid, rset.policy);
1350 node_enforce_resource_policy(u, node, &rset);
1356 static bool resource_push_attributes(mrp_msg_t *msg,
1357 resource_interface *rif,
1358 pa_proplist *proplist)
1360 resource_attribute *attr;
1374 PA_LLIST_FOREACH(attr, rif->attrs) {
1375 if (!PUSH_VALUE(msg, ATTRIBUTE_NAME, STRING, attr->def.name))
1379 sts = pa_proplist_get(proplist, attr->prop, &v.ptr, &size);
1383 switch (attr->def.type) {
1386 v.str = attr->def.value.string;
1387 else if (v.str[size-1] != '\0' || strlen(v.str) != (size-1) ||
1388 !pa_utf8_valid(v.str))
1390 if (!PUSH_VALUE(msg, ATTRIBUTE_VALUE, STRING, v.str))
1396 v.i32 = &attr->def.value.integer;
1397 else if (size != sizeof(*v.i32))
1399 if (!PUSH_VALUE(msg, ATTRIBUTE_VALUE, SINT8, *v.i32))
1405 v.u32 = &attr->def.value.unsignd;
1406 else if (size != sizeof(*v.u32))
1408 if (!PUSH_VALUE(msg, ATTRIBUTE_VALUE, SINT8, *v.u32))
1414 v.dbl = &attr->def.value.floating;
1415 else if (size != sizeof(*v.dbl))
1417 if (!PUSH_VALUE(msg, ATTRIBUTE_VALUE, SINT8, *v.dbl))
1421 default: /* we should never get here */
1431 static void resource_recv_msg(mrp_transport_t *t, mrp_msg_t *msg, void *void_u)
1433 return resource_recvfrom_msg(t, msg, NULL, 0, void_u);
1436 static void resource_recvfrom_msg(mrp_transport_t *transp, mrp_msg_t *msg,
1437 mrp_sockaddr_t *addr, socklen_t addrlen,
1440 struct userdata *u = (struct userdata *)void_u;
1442 pa_murphyif *murphyif;
1443 resource_interface *rif;
1448 resource_request *req, *n;
1453 MRP_UNUSED(addrlen);
1456 pa_assert_se((core = u->core));
1457 pa_assert_se((murphyif = u->murphyif));
1459 rif = &murphyif->resource;
1461 if (!resource_fetch_seqno (msg, &curs, &seqno) ||
1462 !resource_fetch_request (msg, &curs, &reqid) )
1464 pa_log("ignoring malformed message");
1468 PA_LLIST_FOREACH_SAFE(req, n, rif->reqs) {
1469 if (req->seqno <= seqno) {
1470 nodidx = req->nodidx;
1472 if (req->reqid == reqid) {
1473 PA_LLIST_REMOVE(resource_request, rif->reqs, req);
1477 if (!(node = mir_node_find_by_index(u, nodidx))) {
1478 if (reqid != RESPROTO_DESTROY_RESOURCE_SET) {
1479 pa_log("got response (reqid:%u seqno:%u) but can't "
1480 "find the corresponding node", reqid, seqno);
1481 resource_set_create_response_abort(u, msg, &curs);
1485 if (req->seqno < seqno) {
1486 pa_log("unanswered request %d", req->seqno);
1489 pa_log_debug("got response (reqid:%u seqno:%u "
1490 "node:'%s')", reqid, seqno,
1491 node ? node->amname : "<unknown>");
1494 case RESPROTO_CREATE_RESOURCE_SET:
1495 resource_set_create_response(u, node, msg, &curs);
1497 case RESPROTO_DESTROY_RESOURCE_SET:
1500 pa_log("ignoring unsupported resource request "
1506 } /* PA_LLIST_FOREACH_SAFE */
1511 static void resource_set_create_response(struct userdata *u, mir_node *node,
1512 mrp_msg_t *msg, void **pcursor)
1523 if (!resource_fetch_status(msg, pcursor, &status) || (status == 0 &&
1524 !resource_fetch_rset_id(msg, pcursor, &rsetid)))
1526 pa_log("ignoring malformed response to resource set creation");
1531 pa_log("creation of resource set failed. error code %u", status);
1535 node->rsetid = pa_sprintf_malloc("%d", rsetid);
1537 if (pa_murphyif_add_node(u, node) == 0) {
1538 pa_log_debug("resource set was successfully created");
1539 mir_node_print(node, buf, sizeof(buf));
1540 pa_log_debug("modified node:\n%s", buf);
1543 pa_log("failed to create resource set: "
1544 "conflicting resource set id");
1548 static void resource_set_create_response_abort(struct userdata *u,
1549 mrp_msg_t *msg, void **pcursor)
1558 if (!resource_fetch_status(msg, pcursor, &status) || (status == 0 &&
1559 !resource_fetch_rset_id(msg, pcursor, &rsetid)))
1561 pa_log("ignoring malformed response to resource set creation");
1566 pa_log("creation of resource set failed. error code %u", status);
1570 if (resource_set_destroy_node(u, rsetid))
1571 pa_log_debug("destroying resource set %u", rsetid);
1573 pa_log("attempt to destroy resource set %u failed", rsetid);
1577 static bool resource_fetch_seqno(mrp_msg_t *msg,
1583 mrp_msg_value_t value;
1586 if (!mrp_msg_iterate(msg, pcursor, &tag, &type, &value, &size) ||
1587 tag != RESPROTO_SEQUENCE_NO || type != MRP_MSG_FIELD_UINT32)
1589 *pseqno = INVALID_SEQNO;
1593 *pseqno = value.u32;
1598 static bool resource_fetch_request(mrp_msg_t *msg,
1604 mrp_msg_value_t value;
1607 if (!mrp_msg_iterate(msg, pcursor, &tag, &type, &value, &size) ||
1608 tag != RESPROTO_REQUEST_TYPE || type != MRP_MSG_FIELD_UINT16)
1610 *preqtype = INVALID_REQUEST;
1614 *preqtype = value.u16;
1618 static bool resource_fetch_status(mrp_msg_t *msg,
1624 mrp_msg_value_t value;
1627 if (!mrp_msg_iterate(msg, pcursor, &tag, &type, &value, &size) ||
1628 tag != RESPROTO_REQUEST_STATUS || type != MRP_MSG_FIELD_SINT16)
1634 *pstatus = value.s16;
1638 static bool resource_fetch_rset_id(mrp_msg_t *msg,
1644 mrp_msg_value_t value;
1647 if (!mrp_msg_iterate(msg, pcursor, &tag, &type, &value, &size) ||
1648 tag != RESPROTO_RESOURCE_SET_ID || type != MRP_MSG_FIELD_UINT32)
1658 static bool resource_fetch_rset_state(mrp_msg_t *msg,
1660 mrp_resproto_state_t *pstate)
1664 mrp_msg_value_t value;
1667 if (!mrp_msg_iterate(msg, pcursor, &tag, &type, &value, &size) ||
1668 tag != RESPROTO_RESOURCE_STATE || type != MRP_MSG_FIELD_UINT16)
1674 *pstate = value.u16;
1679 static bool resource_fetch_rset_mask(mrp_msg_t *msg,
1681 mrp_resproto_state_t *pmask)
1685 mrp_msg_value_t value;
1688 if (!mrp_msg_iterate(msg, pcursor, &tag, &type, &value, &size) ||
1689 tag != RESPROTO_RESOURCE_GRANT || type != MRP_MSG_FIELD_UINT32)
1699 static bool resource_transport_create(struct userdata *u,
1700 pa_murphyif *murphyif)
1702 static mrp_transport_evt_t ev = {
1703 { .recvmsg = resource_recv_msg },
1704 { .recvmsgfrom = resource_recvfrom_msg },
1705 .closed = resource_xport_closed_evt,
1709 resource_interface *rif;
1712 pa_assert(murphyif);
1714 rif = &murphyif->resource;
1717 rif->transp = mrp_transport_create(murphyif->ml, rif->atype, &ev, u,0);
1719 return rif->transp ? true : false;
1722 static void resource_transport_destroy(pa_murphyif *murphyif)
1724 resource_interface *rif;
1726 pa_assert(murphyif);
1727 rif = &murphyif->resource;
1730 mrp_transport_destroy(rif->transp);
1733 rif->connected = false;
1736 static void connect_attempt(pa_mainloop_api *a,
1738 const struct timeval *t,
1741 struct userdata *u = (struct userdata *)data;
1742 pa_murphyif *murphyif;
1743 resource_interface *rif;
1748 pa_assert_se((murphyif = u->murphyif));
1750 rif = &murphyif->resource;
1752 if (!resource_transport_create(u, murphyif))
1753 schedule_connect(u, rif);
1755 state = resource_transport_connect(rif);
1760 resource_set_create_all(u);
1761 cancel_schedule(u, rif);
1765 cancel_schedule(u, rif);
1769 schedule_connect(u, rif);
1775 static void schedule_connect(struct userdata *u, resource_interface *rif)
1778 pa_mainloop_api *mainloop;
1779 struct timeval when;
1784 pa_assert_se((core = u->core));
1785 pa_assert_se((mainloop = core->mainloop));
1787 pa_gettimeofday(&when);
1788 pa_timeval_add(&when, rif->connect.period);
1790 if ((tev = rif->connect.evt))
1791 mainloop->time_restart(tev, &when);
1793 rif->connect.evt = mainloop->time_new(mainloop, &when,
1794 connect_attempt, u);
1798 static void cancel_schedule(struct userdata *u, resource_interface *rif)
1801 pa_mainloop_api *mainloop;
1806 pa_assert_se((core = u->core));
1807 pa_assert_se((mainloop = core->mainloop));
1809 if ((tev = rif->connect.evt)) {
1810 mainloop->time_free(tev);
1811 rif->connect.evt = NULL;
1815 static rset_hash *node_put_rset(struct userdata *u, mir_node *node, rset_data *rset)
1817 pa_murphyif *murphyif;
1818 resource_interface *rif;
1825 pa_assert(rset->id);
1827 pa_assert(node->implement == mir_stream);
1828 pa_assert(node->direction == mir_input || node->direction == mir_output);
1830 pa_assert_se((murphyif = u->murphyif));
1831 rif = &murphyif->resource;
1833 pa_log_debug("setting rsetid %s for node %s", rset->id, node->amname);
1836 pa_xfree(node->rsetid);
1838 node->rsetid = pa_xstrdup(rset->id);
1840 if (!(pl = get_node_proplist(u, node))) {
1841 pa_log("can't obtain property list for node %s", node->amname);
1845 if ((pa_proplist_sets(pl, PA_PROP_RESOURCE_SET_ID, node->rsetid) < 0)) {
1846 pa_log("failed to set '" PA_PROP_RESOURCE_SET_ID "' property "
1847 "of '%s' node", node->amname);
1851 if (!(rh = rset_hashmap_put(u, node->rsetid, node))) {
1852 pa_log("conflicting rsetid %s for %s", node->rsetid, node->amname);
1859 static void node_enforce_resource_policy(struct userdata *u,
1867 pa_assert(rset->policy);
1870 if (pa_streq(rset->policy, "relaxed"))
1871 req = PA_STREAM_RUN;
1872 else if (pa_streq(rset->policy, "strict")) {
1873 if (rset->state == RSET_RELEASE && rset->autorel)
1874 req = PA_STREAM_KILL;
1877 req = PA_STREAM_RUN;
1879 req = PA_STREAM_BLOCK;
1883 req = PA_STREAM_BLOCK;
1886 pa_stream_state_change(u, node, req);
1889 static rset_data *rset_data_dup(rset_data *orig)
1894 pa_assert(orig->id);
1895 pa_assert(orig->policy);
1897 dup = pa_xnew0(rset_data, 1);
1899 dup->id = pa_xstrdup(orig->id);
1900 dup->autorel = orig->autorel;
1901 dup->state = orig->state;
1902 dup->grant = orig->grant;
1903 dup->policy = pa_xstrdup(orig->policy);
1908 static void rset_data_copy(rset_data *dst, rset_data *src)
1915 pa_assert(src->policy);
1917 pa_xfree((void *)dst->id);
1918 pa_xfree((void *)dst->policy);
1920 dst->id = pa_xstrdup(src->id);
1921 dst->autorel = src->autorel;
1922 dst->state = src->state;
1923 dst->grant = src->grant;
1924 dst->policy = pa_xstrdup(src->policy);
1928 static void rset_data_update(rset_data *dst, rset_data *src)
1936 pa_assert(src->policy);
1938 pa_assert_se(pa_streq(src->id, dst->id));
1940 pa_xfree((void *)dst->policy);
1942 dst->autorel = src->autorel;
1943 dst->state = src->state;
1944 dst->grant = src->grant;
1945 dst->policy = pa_xstrdup(src->policy);
1949 static void rset_data_free(rset_data *rset)
1952 pa_xfree((void *)rset->id);
1953 pa_xfree((void *)rset->policy);
1958 static void pid_hashmap_free(void *p, void *userdata)
1960 pid_hash *ph = (pid_hash *)p;
1965 pa_xfree((void *)ph->pid);
1966 rset_data_free(ph->rset);
1971 static int pid_hashmap_put(struct userdata *u, const char *pid,
1972 mir_node *node, rset_data *rset)
1974 pa_murphyif *murphyif;
1975 resource_interface *rif;
1980 pa_assert(node || rset);
1981 pa_assert_se((murphyif = u->murphyif));
1983 rif = &murphyif->resource;
1985 ph = pa_xnew0(pid_hash, 1);
1986 ph->pid = pa_xstrdup(pid);
1990 if (pa_hashmap_put(rif->nodes.pid, (void *)ph->pid, ph) == 0)
1993 pid_hashmap_free(ph, NULL);
1998 static mir_node *pid_hashmap_get_node(struct userdata *u, const char *pid)
2000 pa_murphyif *murphyif;
2001 resource_interface *rif;
2006 pa_assert(murphyif = u->murphyif);
2008 rif = &murphyif->resource;
2010 if ((ph = pa_hashmap_get(rif->nodes.pid, pid)))
2016 static rset_data *pid_hashmap_get_rset(struct userdata *u, const char *pid)
2018 pa_murphyif *murphyif;
2019 resource_interface *rif;
2024 pa_assert(murphyif = u->murphyif);
2026 rif = &murphyif->resource;
2028 if ((ph = pa_hashmap_get(rif->nodes.pid, pid)))
2034 static mir_node *pid_hashmap_remove_node(struct userdata *u, const char *pid)
2036 pa_murphyif *murphyif;
2037 resource_interface *rif;
2042 pa_assert_se((murphyif = u->murphyif));
2044 rif = &murphyif->resource;
2046 if (!(ph = pa_hashmap_remove(rif->nodes.pid, pid)))
2048 else if (!(node = ph->node))
2049 pa_hashmap_put(rif->nodes.pid, (void *)ph->pid, ph);
2051 pid_hashmap_free(ph, NULL);
2056 static rset_data *pid_hashmap_remove_rset(struct userdata *u, const char *pid)
2058 pa_murphyif *murphyif;
2059 resource_interface *rif;
2066 pa_assert_se((murphyif = u->murphyif));
2068 rif = &murphyif->resource;
2070 if (!(ph = pa_hashmap_remove(rif->nodes.pid, pid)))
2072 else if (!(rset = ph->rset))
2073 pa_hashmap_put(rif->nodes.pid, (void *)ph->pid, ph);
2076 pid_hashmap_free(ph, NULL);
2083 static void rset_hashmap_free(void *r, void *userdata)
2085 rset_hash *rh = (rset_hash *)r;
2090 pa_xfree(rh->nodes);
2091 rset_data_free(rh->rset);
2096 static rset_hash *rset_hashmap_put(struct userdata *u,
2100 pa_murphyif *murphyif;
2101 resource_interface *rif;
2109 pa_assert_se((murphyif = u->murphyif));
2111 rif = &murphyif->resource;
2113 if ((rh = pa_hashmap_get(rif->nodes.rsetid, rsetid))) {
2114 for (i = 0; i < rh->nnode; i++) {
2115 if (rh->nodes[i] == node)
2120 rh->nodes = pa_xrealloc(rh->nodes, sizeof(mir_node *) * (rh->nnode+1));
2123 rset = pa_xnew0(rset_data, 1);
2125 rset->id = pa_xstrdup(rsetid);
2126 rset->policy = pa_xstrdup("unknown");
2128 rh = pa_xnew0(rset_hash, 1);
2131 rh->nodes = pa_xnew0(mir_node *, 2);
2134 pa_hashmap_put(rif->nodes.rsetid, (void *)rh->rset->id, rh);
2140 rh->nodes[i+0] = node;
2141 rh->nodes[i+1] = NULL;
2146 static rset_hash *rset_hashmap_get(struct userdata *u, const char *rsetid)
2148 pa_murphyif *murphyif;
2149 resource_interface *rif;
2154 pa_assert(murphyif = u->murphyif);
2156 rif = &murphyif->resource;
2158 if ((rh = pa_hashmap_get(rif->nodes.rsetid, rsetid)))
2164 static int rset_hashmap_remove(struct userdata *u,
2168 pa_murphyif *murphyif;
2169 resource_interface *rif;
2174 pa_assert_se((murphyif = u->murphyif));
2176 rif = &murphyif->resource;
2178 if ((rh = pa_hashmap_get(rif->nodes.rsetid, rsetid))) {
2180 for (i = 0; i < rh->nnode; i++) {
2181 if (node == rh->nodes[i]) {
2182 if (rh->nnode <= 1) {
2183 pa_hashmap_remove(rif->nodes.rsetid, rsetid);
2184 rset_hashmap_free(rh, NULL);
2188 for (j = i; j < rh->nnode; j++)
2189 rh->nodes[j] = rh->nodes[j+1];
2204 static pa_proplist *get_node_proplist(struct userdata *u, mir_node *node)
2208 pa_source_output *o;
2212 pa_assert_se((core = u->core));
2214 if (node->implement == mir_stream && node->paidx != PA_IDXSET_INVALID) {
2215 if (node->direction == mir_input) {
2216 if ((i = pa_idxset_get_by_index(core->sink_inputs, node->paidx)))
2219 else if (node->direction == mir_output) {
2220 if ((o = pa_idxset_get_by_index(core->source_outputs,node->paidx)))
2228 static const char *get_node_pid(struct userdata *u, mir_node *node)
2234 if (node && (pl = get_node_proplist(u, node)))
2235 return pa_proplist_gets(pl, PA_PROP_APPLICATION_PROCESS_ID);
2243 * indent-tabs-mode: nil