1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
|
/*
* module-agl-audio -- PulseAudio module for providing audio routing support
* (forked from "module-murphy-ivi" - https://github.com/otcshare )
* Copyright (c) 2012, Intel Corporation.
* Copyright (c) 2016, IoT.bzh
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU Lesser General Public License,
* version 2.1, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St - Fifth Floor, Boston,
* MA 02110-1301 USA.
*
*/
#include "router.h"
#include "switch.h"
#include "zone.h"
#include "utils.h"
agl_router *agl_router_init (struct userdata *u)
{
agl_router *router;
size_t num_classes;
num_classes = agl_application_class_end;
router = pa_xnew0 (agl_router, 1);
router->rtgroups.input = pa_hashmap_new (pa_idxset_string_hash_func,
pa_idxset_string_compare_func);
router->rtgroups.output = pa_hashmap_new (pa_idxset_string_hash_func,
pa_idxset_string_compare_func);
router->maplen = num_classes;
router->priormap = pa_xnew0 (int, num_classes);
AGL_DLIST_INIT (router->nodlist);
AGL_DLIST_INIT (router->connlist);
return router;
}
void agl_router_done (struct userdata *u)
{
agl_router *router;
agl_node *e,*n;
agl_connection *conn, *c;
agl_rtgroup *rtg;
agl_rtgroup **map;
int i;
if (u && (router = u->router)) {
AGL_DLIST_FOR_EACH_SAFE(agl_node, rtprilist, e,n, &router->nodlist)
AGL_DLIST_UNLINK(agl_node, rtprilist, e);
AGL_DLIST_FOR_EACH_SAFE(agl_connection, link, conn,c, &router->connlist) {
AGL_DLIST_UNLINK(agl_connection, link, conn);
pa_xfree (conn);
}
/*
PA_HASHMAP_FOREACH(rtg, router->rtgroups.input, state) {
rtgroup_destroy(u, rtg);
}
PA_HASHMAP_FOREACH(rtg, router->rtgroups.output, state) {
rtgroup_destroy(u, rtg);
}*/
pa_hashmap_free (router->rtgroups.input);
pa_hashmap_free (router->rtgroups.output);
for (i = 0; i < AGL_ZONE_MAX; i++) {
if ((map = router->classmap.input[i]))
pa_xfree(map);
if ((map = router->classmap.output[i]))
pa_xfree(map);
}
pa_xfree (router->priormap);
pa_xfree (router);
u->router = NULL;
}
}
bool agl_router_default_accept (struct userdata *u, agl_rtgroup *rtg, agl_node *node)
{
/* TODO */
return true;
}
bool agl_router_phone_accept (struct userdata *u, agl_rtgroup *rtg, agl_node *node)
{
/* TODO */
return true;
}
int agl_router_default_compare (struct userdata *u, agl_rtgroup *rtg, agl_node *n1, agl_node *n2)
{
/* TODO */
return 1;
}
int agl_router_phone_compare (struct userdata *u, agl_rtgroup *rtg, agl_node *n1, agl_node *n2)
{
/* TODO */
return 1;
}
agl_rtgroup *agl_router_create_rtgroup (struct userdata *u, agl_direction type, const char *name, const char *node_desc, agl_rtgroup_accept_t accept, agl_rtgroup_compare_t compare)
{
agl_router *router;
agl_rtgroup *rtg;
agl_nodeset *nodeset;
agl_node *node;
pa_hashmap *table;
pa_assert (u);
pa_assert (type == agl_input || type == agl_output);
pa_assert (name);
pa_assert_se (router = u->router);
pa_assert_se (nodeset = u->nodeset);
if (type == agl_input)
table = router->rtgroups.input;
else
table = router->rtgroups.output;
pa_assert (table);
rtg = pa_xnew0 (agl_rtgroup, 1);
rtg->name = pa_xstrdup (name);
rtg->accept = accept;
rtg->compare = compare;
AGL_DLIST_INIT(rtg->entries);
/* associate an agl_output node for an agl_input routing group */
if (type == agl_input) {
node = agl_node_create (u, NULL);
node->direction = agl_output;
node->implement = agl_device;
node->visible = true;
node->available = true;
node->paname = pa_xstrdup (node_desc);
/* add to global nodeset */
pa_idxset_put (nodeset->nodes, node, &node->index);
rtg->node = node;
} else {
rtg->node = NULL;
}
pa_hashmap_put (table, rtg->name, rtg);
pa_log_debug ("routing group '%s' created", name);
return rtg;
}
void agl_router_destroy_rtgroup (struct userdata *u, agl_direction type, const char *name)
{
agl_router *router;
agl_rtgroup *rtg;
pa_hashmap *table;
pa_assert (u);
pa_assert (name);
pa_assert_se (router = u->router);
if (type == agl_input)
table = router->rtgroups.input;
else
table = router->rtgroups.output;
pa_assert (table);
rtg = pa_hashmap_remove (table, name);
if (!rtg) {
pa_log_debug ("can't destroy routing group '%s': not found", name);
} else {
//rtgroup_destroy (u, rtg);
pa_log_debug ("routing group '%s' destroyed", name);
}
}
bool agl_router_assign_class_to_rtgroup (struct userdata *u, agl_node_type class, uint32_t zone, agl_direction type, const char *name)
{
agl_router *router;
pa_hashmap *rtable;
agl_rtgroup ***classmap;
agl_rtgroup **zonemap;
const char *classname;
const char *direction;
agl_rtgroup *rtg;
agl_zone *rzone;
pa_assert (u);
pa_assert (zone < AGL_ZONE_MAX);
pa_assert (type == agl_input || type == agl_output);
pa_assert (name);
pa_assert_se (router = u->router);
if (type == agl_input) {
rtable = router->rtgroups.input;
classmap = router->classmap.input;
} else {
rtable = router->rtgroups.output;
classmap = router->classmap.output;
}
if (class < 0 || class >= router->maplen) {
pa_log_debug ("Cannot assign class to routing group '%s': "
"id %d out of range (0 - %d)",
name, class, router->maplen);
return false;
}
classname = agl_node_type_str (class); /* "Player", "Radio"... */
direction = agl_node_direction_str (type); /* "input", "output" */
rtg = pa_hashmap_get (rtable, name);
if (!rtg) {
pa_log_debug ("Cannot assign class to routing group '%s': "
"router group not found", name);
return false;
}
zonemap = classmap[zone];
if (!zonemap) { /* THIS LOOKS LIKE A HACK TO IGNORE THE ERROR... */
zonemap = pa_xnew0 (agl_rtgroup *, router->maplen);
classmap[zone] = zonemap;
}
zonemap[class] = rtg;
/* try to get zone name for logging, if fails, only print id number */
rzone = agl_zoneset_get_zone_by_index (u, zone);
if (rzone) {
pa_log_debug ("class '%s'@'%s' assigned to routing group '%s'",
classname, rzone->name, name);
} else {
pa_log_debug ("class '%s'@zone%d assigned to routing group '%s'",
classname, zone, name);
}
return true;
}
void agl_router_assign_class_priority (struct userdata *u, agl_node_type class, int priority)
{
agl_router *router;
int *priormap;
pa_assert (u);
pa_assert_se (router = u->router);
pa_assert_se (priormap = router->priormap);
if (class > 0 && class < router->maplen) {
pa_log_debug ("assigning priority %d to class '%s'",
priority, agl_node_type_str (class));
priormap[class] = priority;
}
}
void agl_router_register_node (struct userdata *u, agl_node *node)
{
agl_router *router;
agl_rtgroup *rtg;
pa_assert (u);
pa_assert (node);
pa_assert_se (router = u->router);
/* we try to discover node routing group from the configuration, "Phone" for instance,
* see defaults in "config.c. Otherwise we just say NULL, a.k.a. default */
if (node->direction == agl_input) {
rtg = pa_hashmap_get (router->rtgroups.input, agl_node_type_str (node->type));
if (rtg)
implement_default_route (u, node, rtg->node, agl_utils_new_stamp ());
else
implement_default_route (u, node, NULL, agl_utils_new_stamp ());
} else {
rtg = pa_hashmap_get (router->rtgroups.output, agl_node_type_str (node->type));
if (rtg)
implement_default_route (u, rtg->node, node, agl_utils_new_stamp ());
else
implement_default_route (u, NULL, node, agl_utils_new_stamp ());
}
}
void agl_router_unregister_node (struct userdata *u, agl_node *node)
{
pa_assert (u);
pa_assert (node);
remove_routes (u, node, NULL, agl_utils_new_stamp ());
}
agl_node *agl_router_make_prerouting (struct userdata *u, agl_node *data)
{
agl_router *router;
int priority;
static bool done_prerouting;
uint32_t stamp;
agl_node *start, *end;
agl_node *target;
pa_assert (u);
pa_assert_se (router = u->router);
pa_assert_se (data->implement == agl_stream);
//priority = node_priority (u, data);
done_prerouting = false;
target = NULL;
stamp = agl_utils_new_stamp ();
//make_explicit_routes (u, stamp);
//pa_audiomgr_delete_default_routes(u);
AGL_DLIST_FOR_EACH_BACKWARDS(agl_node, rtprilist, start, &router->nodlist) {
//if ((start->implement == agl_device) &&
// (!start->loop)) /* only manage looped real devices */
// continue;
/*if (priority >= node_priority (u, start)) {
target = find_default_route (u, data, stamp);
if (target)
implement_preroute (u, data, target, stamp);
else
done_prerouting = true;
}*/
if (start->stamp >= stamp)
continue;
//end = find_default_route (u, start, stamp);
//if (end)
// implement_default_route(u, start, end, stamp);
}
if (!done_prerouting) {
pa_log_debug ("Prerouting failed, trying to find default route as last resort");
//target = find_default_route (u, data, stamp);
//if (target)
// implement_preroute (u, data, target, stamp);
}
return target;
}
void agl_router_make_routing (struct userdata *u)
{
agl_router *router;
static bool ongoing_routing; /* true while we are actively routing */
uint32_t stamp;
agl_node *start, *end;
pa_assert (u);
pa_assert_se (router = u->router);
if (ongoing_routing) /* already routing, canceling */
return;
ongoing_routing = true;
stamp = agl_utils_new_stamp ();
pa_log_debug("stamp for routing: %d", stamp);
// make_explicit_routes (u, stamp);
// pa_audiomgr_delete_default_routes (u);
AGL_DLIST_FOR_EACH_BACKWARDS(agl_node, rtprilist, start, &router->nodlist) {
//if ((start->implement == agl_device) &&
// (!start->loop)) /* only manage looped real devices */
// continue;
if (start->stamp >= stamp)
continue;
end = find_default_route (u, start, stamp);
if (end)
implement_default_route (u, start, end, stamp);
}
// pa_audiomgr_send_default_routes (u);
ongoing_routing = false;
}
void implement_default_route (struct userdata *u,
agl_node *start, agl_node *end,
uint32_t stamp)
{
if (start->direction == agl_input) {
agl_switch_setup_link (u, start, end, false);
//agl_volume_add_limiting_class(u, end, volume_class(start), stamp);
} else {
agl_switch_setup_link (u, end, start, false);
}
}
agl_node *find_default_route (struct userdata *u, agl_node *start, uint32_t stamp)
{
/* TODO */
return NULL;
}
void remove_routes (struct userdata *u, agl_node *start, agl_node *end, uint32_t stamp)
{
if (start->direction == agl_input) {
agl_switch_teardown_link (u, start, end);
} else {
agl_switch_teardown_link (u, end, start);
}
}
|