Implement routing groups and volume ramp up/down
[staging/agl-audio-plugin.git] / node.c
1 /*
2  * module-agl-audio -- PulseAudio module for providing audio routing support
3  * (forked from "module-murphy-ivi" - https://github.com/otcshare )
4  * Copyright (c) 2012, Intel Corporation.
5  * Copyright (c) 2016, IoT.bzh
6  *
7  * This program is free software; you can redistribute it and/or modify it
8  * under the terms and conditions of the GNU Lesser General Public License,
9  * version 2.1, as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.
14  * See the GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this program; if not, write to the
18  * Free Software Foundation, Inc., 51 Franklin St - Fifth Floor, Boston,
19  * MA 02110-1301 USA.
20  *
21  */
22 #include "node.h"
23 #include "router.h"
24
25 #include <pulsecore/idxset.h>
26
27 agl_nodeset *agl_nodeset_init (struct userdata *u)
28 {
29         agl_nodeset *ns;
30
31         pa_assert (u);
32
33         ns = pa_xnew0 (agl_nodeset, 1);
34         ns->nodes = pa_idxset_new (pa_idxset_trivial_hash_func,
35                                    pa_idxset_trivial_compare_func);
36         ns->roles = pa_hashmap_new (pa_idxset_string_hash_func,
37                                     pa_idxset_string_compare_func);
38         ns->binaries = pa_hashmap_new (pa_idxset_string_hash_func,
39                                        pa_idxset_string_compare_func);
40         return ns;
41 }
42
43 void agl_nodeset_done(struct userdata *u)
44 {
45         agl_nodeset *ns;
46         agl_nodeset_map *role, *binary;
47         void *state;
48         int i;
49
50         if (u && (ns = u->nodeset)) {
51                 pa_idxset_free (ns->nodes, NULL);
52
53                 PA_HASHMAP_FOREACH(role, ns->roles, state) {
54                         pa_xfree ((void *)role->name);
55                         pa_xfree ((void *)role->resdef);
56                 }
57                 pa_hashmap_free (ns->roles);
58
59                 PA_HASHMAP_FOREACH(binary, ns->binaries, state) {
60                         pa_xfree ((void *)binary->name);
61                         pa_xfree ((void *)binary->resdef);
62                 }
63                 pa_hashmap_free (ns->binaries);
64
65                 for (i = 0;  i < APCLASS_DIM;  i++)
66                         pa_xfree((void *)ns->class_name[i]);
67
68                 free(ns);
69         }
70 }
71
72 int agl_nodeset_add_role (struct userdata *u, const char *role, agl_node_type type, agl_nodeset_resdef *resdef)
73 {
74         agl_nodeset *ns;
75         agl_nodeset_map *map;
76
77         pa_assert (u);
78         pa_assert_se (ns = u->nodeset);
79
80         map = pa_xnew0 (agl_nodeset_map, 1);
81         map->name = pa_xstrdup (role);
82         map->type = type;
83         map->role = pa_xstrdup (role);
84
85         if (resdef) {
86                 map->resdef = pa_xnew (agl_nodeset_resdef, 1);
87                 memcpy (map->resdef, resdef, sizeof(agl_nodeset_resdef));
88         }
89
90         return pa_hashmap_put (ns->roles, (void *)map->name, map);
91 }
92
93 agl_node *agl_node_create (struct userdata *u, agl_node *data)
94 {
95         agl_nodeset *ns;
96         agl_node *node;
97
98         pa_assert (u);
99         pa_assert_se (ns = u->nodeset);
100
101         node = pa_xnew0 (agl_node, 1);
102
103         pa_idxset_put (ns->nodes, node, &node->index);
104
105         if (data) {
106                 node->key = pa_xstrdup (data->key);
107                 node->direction = data->direction;
108                 node->implement = data->implement;
109                 node->channels = data->channels;
110                 node->location = data->location;
111                 node->privacy = data->privacy;
112                 node->type = data->type;
113                 node->zone = pa_xstrdup (data->zone);
114                 node->visible = data->visible;
115                 node->available = data->available;
116                 node->amname = pa_xstrdup (data->amname ? data->amname : data->paname);
117                 node->amdescr = pa_xstrdup(data->amdescr ? data->amdescr : "");
118                 node->amid = data->amid;
119                 node->paname = pa_xstrdup (data->paname);
120                 node->stamp = data->stamp;
121                 node->rset.id = data->rset.id ? pa_xstrdup(data->rset.id) : NULL;
122                 node->rset.grant = data->rset.grant;
123
124                 if (node->implement == agl_device) {
125                         node->pacard.index = data->pacard.index;
126                         if (data->pacard.profile)
127                                 node->pacard.profile = pa_xstrdup (data->pacard.profile);
128                         if (data->paport)
129                                 node->paport = data->paport;
130                 }
131         }
132
133         return node;
134 }
135
136 void agl_node_destroy (struct userdata *u, agl_node *node)
137 {
138         agl_nodeset *ns;
139
140         pa_assert (u);
141         pa_assert (node);
142         pa_assert_se (ns = u->nodeset);
143
144         pa_idxset_remove_by_index (ns->nodes, node->index);
145
146         pa_xfree (node);
147 }
148
149 const char *agl_node_type_str (agl_node_type type)
150 {
151         switch (type) {
152                 case agl_node_type_unknown: return "Unknown";
153                 case agl_radio:             return "Radio";
154                 case agl_player:            return "Player";
155                 case agl_navigator:         return "Navigator";
156                 case agl_game:              return "Game";
157                 case agl_browser:           return "Browser";
158                 case agl_camera:            return "Camera";
159                 case agl_phone:             return "Phone";
160                 case agl_alert:             return "Alert";
161                 case agl_event:             return "Event";
162                 case agl_system:            return "System";
163                 default:                    return "default";
164         }
165 }
166
167 const char *agl_node_direction_str (agl_direction direction)
168 {
169         switch (direction) {
170                 case agl_direction_unknown: return "unknown";
171                 case agl_input:             return "input";
172                 case agl_output:            return "output";
173                 default:                    return "< ??? >";
174         }
175 }
176
177 agl_node *agl_node_get_from_data (struct userdata *u, agl_direction type, void *data)
178 {
179         pa_sink_input_new_data *sinp_data;
180         pa_source_output_new_data *sout_data;
181         agl_nodeset *nodeset;
182         agl_node *node;
183         uint32_t index;
184
185         pa_assert (u);
186         pa_assert (data);
187         pa_assert (nodeset = u->nodeset);
188
189         pa_assert (type == agl_input || type == agl_output);
190
191         /* input (= sink_input) */
192         if (type == agl_input) {
193                 sinp_data = (pa_sink_input_new_data *) data;
194                 PA_IDXSET_FOREACH(node, nodeset->nodes, index) {
195                         if (node->client == sinp_data->client)
196                                 return node;
197                 }
198         /* output (= source_output) TODO */
199         /*} else {*/
200         }
201
202         return NULL;
203 }
204
205 agl_node *agl_node_get_from_client (struct userdata *u, pa_client *client)
206 {
207         agl_nodeset *nodeset;
208         agl_node *node;
209         uint32_t index;
210
211         pa_assert (u);
212         pa_assert (client);
213         pa_assert (nodeset = u->nodeset);
214
215         PA_IDXSET_FOREACH(node, nodeset->nodes, index) {
216                 if (node->client == client)
217                         return node;
218         }
219
220         return NULL;
221 }
222
223 bool agl_node_has_highest_priority (struct userdata *u, agl_node *node)
224 {
225         agl_nodeset *nodeset;
226         agl_node *n;
227         int priority;
228         uint32_t index;
229
230         pa_assert (u);
231         pa_assert (node);
232         pa_assert (nodeset = u->nodeset);
233
234         priority = agl_router_get_node_priority (u, node);
235
236         PA_IDXSET_FOREACH(n, nodeset->nodes, index) {
237                 if ((n != node) && (agl_router_get_node_priority (u, n) >= priority))
238                         return false;
239         }
240
241         return true;
242 }