fa05d4e2ddf8826b623178b8a5e1883525d9d0cf
[staging/agl-audio-plugin.git] / router.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 "router.h"
23 #include "switch.h"
24 #include "zone.h"
25 #include "utils.h"
26
27 agl_router *agl_router_init (struct userdata *u)
28 {
29         agl_router *router;
30         size_t num_classes;
31
32         num_classes = agl_application_class_end;
33
34         router = pa_xnew0 (agl_router, 1);
35         router->rtgroups.input = pa_hashmap_new (pa_idxset_string_hash_func,
36                                                  pa_idxset_string_compare_func);
37         router->rtgroups.output = pa_hashmap_new (pa_idxset_string_hash_func,
38                                                   pa_idxset_string_compare_func);
39         router->maplen = num_classes;
40         router->priormap = pa_xnew0 (int, num_classes);
41
42         AGL_DLIST_INIT (router->nodlist);
43         AGL_DLIST_INIT (router->connlist);
44
45         return router;
46 }
47
48 void agl_router_done (struct userdata *u)
49 {
50         agl_router *router;
51         agl_node *e,*n;
52         agl_connection *conn, *c;
53         agl_rtgroup *rtg;
54         agl_rtgroup **map;
55         int i;
56
57         if (u && (router = u->router)) {
58                 AGL_DLIST_FOR_EACH_SAFE(agl_node, rtprilist, e,n, &router->nodlist)
59                         AGL_DLIST_UNLINK(agl_node, rtprilist, e);
60                 AGL_DLIST_FOR_EACH_SAFE(agl_connection, link, conn,c, &router->connlist) {
61                         AGL_DLIST_UNLINK(agl_connection, link, conn);
62                         pa_xfree (conn);
63                 }
64                 /*
65                 PA_HASHMAP_FOREACH(rtg, router->rtgroups.input, state) {
66                         rtgroup_destroy(u, rtg);
67                 }
68                 PA_HASHMAP_FOREACH(rtg, router->rtgroups.output, state) {
69                         rtgroup_destroy(u, rtg);
70                 }*/
71                 pa_hashmap_free (router->rtgroups.input);
72                 pa_hashmap_free (router->rtgroups.output);
73
74                 for (i = 0;  i < AGL_ZONE_MAX;  i++) {
75                         if ((map = router->classmap.input[i]))
76                                 pa_xfree(map);
77                         if ((map = router->classmap.output[i]))
78                                 pa_xfree(map);
79                 }
80
81                 pa_xfree (router->priormap);
82                 pa_xfree (router);
83
84                 u->router = NULL;
85         }
86 }
87
88 bool agl_router_default_accept (struct userdata *u, agl_rtgroup *rtg, agl_node *node)
89 {
90         /* TODO */
91         return true;
92 }
93
94 bool agl_router_phone_accept (struct userdata *u, agl_rtgroup *rtg, agl_node *node)
95 {
96         /* TODO */
97         return true;
98 }
99
100 int agl_router_default_compare (struct userdata *u, agl_rtgroup *rtg, agl_node *n1, agl_node *n2)
101 {
102         /* TODO */
103         return 1;
104 }
105
106 int agl_router_phone_compare (struct userdata *u, agl_rtgroup *rtg, agl_node *n1, agl_node *n2)
107 {
108         /* TODO */
109         return 1;
110 }
111
112 agl_rtgroup *agl_router_create_rtgroup (struct userdata *u, agl_direction type, const char *name, agl_rtgroup_accept_t accept, agl_rtgroup_compare_t compare)
113 {
114         agl_router *router;
115         agl_rtgroup *rtg;
116         pa_hashmap *table;
117
118         pa_assert (u);
119         pa_assert (type == agl_input || type == agl_output);
120         pa_assert (name);
121         pa_assert (accept);
122         pa_assert (compare);
123         pa_assert_se (router = u->router);
124
125         if (type == agl_input)
126                 table = router->rtgroups.input;
127         else
128                 table = router->rtgroups.output;
129         pa_assert (table);
130
131         rtg = pa_xnew0 (agl_rtgroup, 1);
132         rtg->name = pa_xstrdup (name);
133         rtg->accept = accept;
134         rtg->compare = compare;
135         AGL_DLIST_INIT(rtg->entries);
136
137         pa_hashmap_put (table, rtg->name, rtg);
138
139         pa_log_debug ("routing group '%s' created", name);
140
141         return rtg;
142 }
143
144 void agl_router_destroy_rtgroup (struct userdata *u, agl_direction type, const char *name)
145 {
146         agl_router *router;
147         agl_rtgroup *rtg;
148         pa_hashmap *table;
149
150         pa_assert (u);
151         pa_assert (name);
152         pa_assert_se (router = u->router);
153
154         if (type == agl_input)
155                 table = router->rtgroups.input;
156         else
157                 table = router->rtgroups.output;
158         pa_assert (table);
159
160         rtg = pa_hashmap_remove (table, name);
161         if (!rtg) {
162                 pa_log_debug ("can't destroy routing group '%s': not found", name);
163         } else {
164                 //rtgroup_destroy (u, rtg);
165                 pa_log_debug ("routing group '%s' destroyed", name);
166         }
167 }
168
169 bool agl_router_assign_class_to_rtgroup (struct userdata *u, agl_node_type class, uint32_t zone, agl_direction type, const char *name)
170 {
171         agl_router *router;
172         pa_hashmap *rtable;
173         agl_rtgroup ***classmap;
174         agl_rtgroup **zonemap;
175         const char *classname;
176         const char *direction;
177         agl_rtgroup *rtg;
178         agl_zone *rzone;
179
180         pa_assert (u);
181         pa_assert (zone < AGL_ZONE_MAX);        
182         pa_assert (type == agl_input || type == agl_output);
183         pa_assert (name);
184         pa_assert_se (router = u->router);
185
186         if (type == agl_input) {
187                 rtable = router->rtgroups.input;
188                 classmap = router->classmap.input;
189         } else {
190                 rtable = router->rtgroups.output;
191                 classmap = router->classmap.output;
192         }
193
194         if (class < 0 || class >= router->maplen) {
195                 pa_log_debug ("Cannot assign class to routing group '%s': "
196                               "id %d out of range (0 - %d)",
197                               name, class, router->maplen);
198                 return false;
199         }
200
201         classname = agl_node_type_str (class); /* "Player", "Radio"... */
202         direction = agl_node_direction_str (type); /* "input", "output" */
203
204         rtg = pa_hashmap_get (rtable, name);
205         if (!rtg) {
206                 pa_log_debug ("Cannot assign class to routing group '%s': "
207                               "router group not found", name);
208                 return false;
209         }
210
211         zonemap = classmap[zone];
212         if (!zonemap) { /* THIS LOOKS LIKE A HACK TO IGNORE THE ERROR... */
213                 zonemap = pa_xnew0 (agl_rtgroup *, router->maplen);
214                 classmap[zone] = zonemap;
215         }
216
217         zonemap[class] = rtg;
218
219          /* try to get zone name for logging, if fails, only print id number */
220         rzone = agl_zoneset_get_zone_by_index (u, zone);
221         if (rzone) {
222                 pa_log_debug ("class '%s'@'%s' assigned to routing group '%s'",
223                               classname, rzone->name, name); 
224         } else {
225                 pa_log_debug ("class '%s'@zone%d assigned to routing group '%s'",
226                               classname, zone, name);
227         }
228
229         return true;
230 }
231
232 void agl_router_assign_class_priority (struct userdata *u, agl_node_type class, int priority)
233 {
234         agl_router *router;
235         int *priormap;
236
237         pa_assert (u);
238         pa_assert_se (router = u->router);
239         pa_assert_se (priormap = router->priormap);
240
241         if (class > 0 && class < router->maplen) {
242                 pa_log_debug ("assigning priority %d to class '%s'",
243                               priority, agl_node_type_str (class));
244                 priormap[class] = priority;
245         }
246 }
247
248 void agl_router_register_node (struct userdata *u, agl_node *node)
249 {
250         pa_assert (u);
251         pa_assert (node);
252
253         implement_default_route (u, node, NULL, agl_utils_new_stamp ());
254 }
255
256 void agl_router_unregister_node (struct userdata *u, agl_node *node)
257 {
258         pa_assert (u);
259         pa_assert (node);
260
261         remove_routes (u, node, NULL, agl_utils_new_stamp ());
262 }
263
264 agl_node *agl_router_make_prerouting (struct userdata *u, agl_node *data)
265 {
266         agl_router *router;
267         int priority;
268         static bool done_prerouting;
269         uint32_t stamp;
270         agl_node *start, *end;
271         agl_node *target;
272
273         pa_assert (u);
274         pa_assert_se (router = u->router);
275         pa_assert_se (data->implement == agl_stream);
276
277         //priority = node_priority (u, data);
278
279         done_prerouting = false;
280         target = NULL;
281         stamp = agl_utils_new_stamp ();
282
283         //make_explicit_routes (u, stamp);
284
285         //pa_audiomgr_delete_default_routes(u);
286
287         AGL_DLIST_FOR_EACH_BACKWARDS(agl_node, rtprilist, start, &router->nodlist) {
288                 //if ((start->implement == agl_device) &&
289                 //    (!start->loop))   /* only manage looped real devices */
290                 //      continue;
291
292                 /*if (priority >= node_priority (u, start)) {
293                         target = find_default_route (u, data, stamp);
294                         if (target)
295                                 implement_preroute (u, data, target, stamp);
296                         else
297                                 done_prerouting = true;
298                 }*/
299
300                 if (start->stamp >= stamp)
301                         continue;
302
303                 //end = find_default_route (u, start, stamp);
304                 //if (end)
305                 //      implement_default_route(u, start, end, stamp);
306         }
307
308         if (!done_prerouting) {
309                 pa_log_debug ("Prerouting failed, trying to find default route as last resort");
310
311                 //target = find_default_route (u, data, stamp);
312                 //if (target)
313                 //      implement_preroute (u, data, target, stamp);
314         }
315
316         return target;
317 }
318
319 void agl_router_make_routing (struct userdata *u)
320 {
321         agl_router *router;
322         static bool ongoing_routing;    /* true while we are actively routing */
323         uint32_t stamp;
324         agl_node *start, *end;
325
326         pa_assert (u);
327         pa_assert_se (router = u->router);
328
329         if (ongoing_routing)            /* already routing, canceling */
330                 return;
331         ongoing_routing = true;
332         stamp = agl_utils_new_stamp ();
333
334         pa_log_debug("stamp for routing: %d", stamp);
335
336         // make_explicit_routes (u, stamp);
337
338         // pa_audiomgr_delete_default_routes (u);
339
340         AGL_DLIST_FOR_EACH_BACKWARDS(agl_node, rtprilist, start, &router->nodlist) {
341                 //if ((start->implement == agl_device) &&
342                 //   (!start->loop))    /* only manage looped real devices */
343                 //      continue;
344
345                 if (start->stamp >= stamp)
346                         continue;
347
348                 end = find_default_route (u, start, stamp);
349                 if (end)
350                         implement_default_route (u, start, end, stamp);
351         }
352
353         // pa_audiomgr_send_default_routes (u);
354
355         ongoing_routing = false;
356 }
357
358 void implement_default_route (struct userdata *u,
359                               agl_node *start, agl_node *end,
360                               uint32_t stamp)
361 {
362         if (start->direction == agl_input) {
363                 agl_switch_setup_link (u, start, end, false);
364                 //agl_volume_add_limiting_class(u, end, volume_class(start), stamp);
365         } else {
366                 agl_switch_setup_link (u, end, start, false);
367         }
368 }
369
370 agl_node *find_default_route (struct userdata *u, agl_node *start, uint32_t stamp)
371 {
372         /* TODO */
373
374         return NULL;
375 }
376
377 void remove_routes (struct userdata *u, agl_node *start, agl_node *end, uint32_t stamp)
378 {
379         if (start->direction == agl_input) {
380                 agl_switch_teardown_link (u, start, end);
381         } else {
382                 agl_switch_teardown_link (u, end, start);
383         }
384 }