forked from BlissRoms-x86/hardware-libaudio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaudio_route.c
548 lines (454 loc) · 14.2 KB
/
audio_route.c
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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
/*
* Copyright (C) 2012 The Android Open Source Project
* Inspired by TinyHW, written by Mark Brown at Wolfson Micro
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#define LOG_TAG "audio_hw_primary"
/*#define LOG_NDEBUG 0*/
#include <errno.h>
#include <expat.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <cutils/log.h>
#include <sound/asound.h>
#include <tinyalsa/asoundlib.h>
#define BUF_SIZE 1024
#define MIXER_XML_PATH "/system/etc/mixer_paths.xml"
#define INITIAL_MIXER_PATH_SIZE 8
struct snd_pcm_info *select_card(unsigned int device __unused, unsigned int flags);
struct mixer_state {
struct mixer_ctl *ctl;
int old_value;
int new_value;
int reset_value;
};
struct mixer_setting {
struct mixer_ctl *ctl;
int value;
};
struct mixer_path {
char *name;
unsigned int size;
unsigned int length;
struct mixer_setting *setting;
};
struct audio_route {
struct mixer *mixer;
unsigned int num_mixer_ctls;
struct mixer_state *mixer_state;
unsigned int mixer_path_size;
unsigned int num_mixer_paths;
struct mixer_path *mixer_path;
};
struct config_parse_state {
struct audio_route *ar;
struct mixer_path *path;
int level;
};
/* path functions */
static void path_free(struct audio_route *ar)
{
unsigned int i;
if (!ar) {
ALOGE("%s: invalid audio_route", __FUNCTION__);
return;
}
for (i = 0; i < ar->num_mixer_paths; i++) {
if (ar->mixer_path[i].name)
free(ar->mixer_path[i].name);
if (ar->mixer_path[i].setting)
free(ar->mixer_path[i].setting);
}
free(ar->mixer_path);
}
static struct mixer_path *path_get_by_name(struct audio_route *ar,
const char *name)
{
unsigned int i;
if (!ar) {
ALOGE("%s: invalid audio_route", __FUNCTION__);
return NULL;
}
for (i = 0; i < ar->num_mixer_paths; i++)
if (strcmp(ar->mixer_path[i].name, name) == 0)
return &ar->mixer_path[i];
return NULL;
}
static struct mixer_path *path_create(struct audio_route *ar, const char *name)
{
struct mixer_path *new_mixer_path = NULL;
if (!ar) {
ALOGE("%s: invalid audio_route", __FUNCTION__);
return NULL;
}
if (path_get_by_name(ar, name)) {
ALOGE("Path name '%s' already exists", name);
return NULL;
}
/* check if we need to allocate more space for mixer paths */
if (ar->mixer_path_size <= ar->num_mixer_paths) {
if (ar->mixer_path_size == 0)
ar->mixer_path_size = INITIAL_MIXER_PATH_SIZE;
else
ar->mixer_path_size *= 2;
new_mixer_path = realloc(ar->mixer_path, ar->mixer_path_size *
sizeof(struct mixer_path));
if (new_mixer_path == NULL) {
ALOGE("Unable to allocate more paths");
return NULL;
} else {
ar->mixer_path = new_mixer_path;
}
}
/* initialise the new mixer path */
ar->mixer_path[ar->num_mixer_paths].name = strdup(name);
ar->mixer_path[ar->num_mixer_paths].size = 0;
ar->mixer_path[ar->num_mixer_paths].length = 0;
ar->mixer_path[ar->num_mixer_paths].setting = NULL;
/* return the mixer path just added, then increment number of them */
return &ar->mixer_path[ar->num_mixer_paths++];
}
static bool path_setting_exists(struct mixer_path *path,
struct mixer_setting *setting)
{
unsigned int i;
for (i = 0; i < path->length; i++)
if (path->setting[i].ctl == setting->ctl)
return true;
return false;
}
static int path_add_setting(struct mixer_path *path,
struct mixer_setting *setting)
{
struct mixer_setting *new_path_setting;
if (path_setting_exists(path, setting)) {
ALOGE("Duplicate path setting '%s'",
mixer_ctl_get_name(setting->ctl));
return -1;
}
/* check if we need to allocate more space for path settings */
if (path->size <= path->length) {
if (path->size == 0)
path->size = INITIAL_MIXER_PATH_SIZE;
else
path->size *= 2;
new_path_setting = realloc(path->setting,
path->size * sizeof(struct mixer_setting));
if (new_path_setting == NULL) {
ALOGE("Unable to allocate more path settings");
return -1;
} else {
path->setting = new_path_setting;
}
}
/* initialise the new path setting */
path->setting[path->length].ctl = setting->ctl;
path->setting[path->length].value = setting->value;
path->length++;
return 0;
}
static int path_add_path(struct mixer_path *path, struct mixer_path *sub_path)
{
unsigned int i;
for (i = 0; i < sub_path->length; i++)
if (path_add_setting(path, &sub_path->setting[i]) < 0)
return -1;
return 0;
}
static void path_print(struct mixer_path *path)
{
unsigned int i;
ALOGV("Path: %s, length: %d", path->name, path->length);
for (i = 0; i < path->length; i++)
ALOGV(" %d: %s -> %d", i, mixer_ctl_get_name(path->setting[i].ctl),
path->setting[i].value);
}
static int path_apply(struct audio_route *ar, struct mixer_path *path)
{
unsigned int i;
unsigned int j;
if (!ar) {
ALOGE("%s: invalid audio_route", __FUNCTION__);
return -1;
}
for (i = 0; i < path->length; i++) {
struct mixer_ctl *ctl = path->setting[i].ctl;
/* locate the mixer ctl in the list */
for (j = 0; j < ar->num_mixer_ctls; j++) {
if (ar->mixer_state[j].ctl == ctl)
break;
}
/* apply the new value */
ar->mixer_state[j].new_value = path->setting[i].value;
}
return 0;
}
/* mixer helper function */
static int mixer_enum_string_to_value(struct mixer_ctl *ctl, const char *string)
{
unsigned int i;
/* Search the enum strings for a particular one */
for (i = 0; i < mixer_ctl_get_num_enums(ctl); i++) {
if (strcmp(mixer_ctl_get_enum_string(ctl, i), string) == 0)
break;
}
return i;
}
static void start_tag(void *data, const XML_Char *tag_name,
const XML_Char **attr)
{
const XML_Char *attr_name = NULL;
const XML_Char *attr_value = NULL;
struct config_parse_state *state = data;
struct audio_route *ar = state->ar;
unsigned int i;
struct mixer_ctl *ctl;
int value;
struct mixer_setting mixer_setting;
/* Get name, type and value attributes (these may be empty) */
for (i = 0; attr[i]; i += 2) {
if (strcmp(attr[i], "name") == 0)
attr_name = attr[i + 1];
else if (strcmp(attr[i], "value") == 0)
attr_value = attr[i + 1];
}
/* Look at tags */
if (strcmp(tag_name, "path") == 0) {
if (attr_name == NULL) {
ALOGE("Unnamed path!");
} else {
if (state->level == 1) {
/* top level path: create and stash the path */
state->path = path_create(ar, (char *)attr_name);
} else {
/* nested path */
struct mixer_path *sub_path = path_get_by_name(ar, attr_name);
path_add_path(state->path, sub_path);
}
}
}
else if (strcmp(tag_name, "ctl") == 0) {
/* Obtain the mixer ctl and value */
ctl = mixer_get_ctl_by_name(ar->mixer, attr_name);
switch (mixer_ctl_get_type(ctl)) {
case MIXER_CTL_TYPE_BOOL:
case MIXER_CTL_TYPE_INT:
value = atoi((char *)attr_value);
break;
case MIXER_CTL_TYPE_ENUM:
value = mixer_enum_string_to_value(ctl, (char *)attr_value);
break;
default:
value = 0;
break;
}
if (state->level == 1) {
/* top level ctl (initial setting) */
/* locate the mixer ctl in the list */
for (i = 0; i < ar->num_mixer_ctls; i++) {
if (ar->mixer_state[i].ctl == ctl)
break;
}
/* apply the new value */
ar->mixer_state[i].new_value = value;
} else {
/* nested ctl (within a path) */
mixer_setting.ctl = ctl;
mixer_setting.value = value;
path_add_setting(state->path, &mixer_setting);
}
}
state->level++;
}
static void end_tag(void *data, const XML_Char *tag_name __attribute__ ((__unused__)))
{
struct config_parse_state *state = data;
state->level--;
}
static int alloc_mixer_state(struct audio_route *ar)
{
unsigned int i;
if (!ar) {
ALOGE("%s: invalid audio_route", __FUNCTION__);
return -1;
}
ar->num_mixer_ctls = mixer_get_num_ctls(ar->mixer);
ar->mixer_state = malloc(ar->num_mixer_ctls * sizeof(struct mixer_state));
if (!ar->mixer_state)
return -1;
for (i = 0; i < ar->num_mixer_ctls; i++) {
ar->mixer_state[i].ctl = mixer_get_ctl(ar->mixer, i);
/* only get value 0, assume multiple ctl values are the same */
ar->mixer_state[i].old_value = mixer_ctl_get_value(ar->mixer_state[i].ctl, 0);
ar->mixer_state[i].new_value = ar->mixer_state[i].old_value;
}
return 0;
}
static void free_mixer_state(struct audio_route *ar)
{
if (!ar) {
ALOGE("%s: invalid audio_route", __FUNCTION__);
return;
}
free(ar->mixer_state);
ar->mixer_state = NULL;
}
void update_mixer_state(struct audio_route *ar)
{
unsigned int i;
unsigned int j;
if (!ar) {
ALOGE("%s: invalid audio_route", __FUNCTION__);
return;
}
for (i = 0; i < ar->num_mixer_ctls; i++) {
/* if the value has changed, update the mixer */
if (ar->mixer_state[i].old_value != ar->mixer_state[i].new_value) {
/* set all ctl values the same */
for (j = 0; j < mixer_ctl_get_num_values(ar->mixer_state[i].ctl); j++)
mixer_ctl_set_value(ar->mixer_state[i].ctl, j,
ar->mixer_state[i].new_value);
ar->mixer_state[i].old_value = ar->mixer_state[i].new_value;
}
}
}
/* saves the current state of the mixer, for resetting all controls */
static void save_mixer_state(struct audio_route *ar)
{
unsigned int i;
if (!ar) {
ALOGE("%s: invalid audio_route", __FUNCTION__);
return;
}
for (i = 0; i < ar->num_mixer_ctls; i++) {
/* only get value 0, assume multiple ctl values are the same */
ar->mixer_state[i].reset_value = mixer_ctl_get_value(ar->mixer_state[i].ctl, 0);
}
}
/* this resets all mixer settings to the saved values */
void reset_mixer_state(struct audio_route *ar)
{
unsigned int i;
if (!ar) {
ALOGE("%s: invalid audio_route", __FUNCTION__);
return;
}
/* load all of the saved values */
for (i = 0; i < ar->num_mixer_ctls; i++)
ar->mixer_state[i].new_value = ar->mixer_state[i].reset_value;
}
void audio_route_apply_path(struct audio_route *ar, const char *name)
{
struct mixer_path *path;
if (!ar) {
ALOGE("%s: invalid audio_route", __FUNCTION__);
return;
}
path = path_get_by_name(ar, name);
if (!path) {
ALOGE("unable to find path '%s'", name);
return;
}
path_apply(ar, path);
}
struct audio_route *audio_route_init(void)
{
struct config_parse_state state;
XML_Parser parser;
FILE *file;
int bytes_read;
void *buf;
int i;
struct mixer_path *path;
struct audio_route *ar;
ar = calloc(1, sizeof(struct audio_route));
if (!ar)
goto err_calloc;
struct snd_pcm_info *info = select_card(0, PCM_OUT);
if (!info) {
ALOGW("Unable to find the mixer");
goto err_mixer_open;
}
ar->mixer = mixer_open(info->card);
if (!ar->mixer) {
ALOGE("Unable to open the mixer, aborting.");
goto err_mixer_open;
}
ar->mixer_path = NULL;
ar->mixer_path_size = 0;
ar->num_mixer_paths = 0;
/* allocate space for and read current mixer settings */
if (alloc_mixer_state(ar) < 0)
goto err_mixer_state;
file = fopen(MIXER_XML_PATH, "r");
if (!file) {
ALOGE("Failed to open %s", MIXER_XML_PATH);
goto err_fopen;
}
parser = XML_ParserCreate(NULL);
if (!parser) {
ALOGE("Failed to create XML parser");
goto err_parser_create;
}
memset(&state, 0, sizeof(state));
state.ar = ar;
XML_SetUserData(parser, &state);
XML_SetElementHandler(parser, start_tag, end_tag);
for (;;) {
buf = XML_GetBuffer(parser, BUF_SIZE);
if (buf == NULL)
goto err_parse;
bytes_read = fread(buf, 1, BUF_SIZE, file);
if (bytes_read < 0)
goto err_parse;
if (XML_ParseBuffer(parser, bytes_read,
bytes_read == 0) == XML_STATUS_ERROR) {
ALOGE("Error in mixer xml (%s)", MIXER_XML_PATH);
goto err_parse;
}
if (bytes_read == 0)
break;
}
/* apply the initial mixer values, and save them so we can reset the
mixer to the original values */
update_mixer_state(ar);
save_mixer_state(ar);
XML_ParserFree(parser);
fclose(file);
return ar;
err_parse:
XML_ParserFree(parser);
err_parser_create:
fclose(file);
err_fopen:
free_mixer_state(ar);
err_mixer_state:
mixer_close(ar->mixer);
err_mixer_open:
free(ar);
ar = NULL;
err_calloc:
return NULL;
}
void audio_route_free(struct audio_route *ar)
{
if (!ar) {
ALOGE("%s: invalid audio_route", __FUNCTION__);
return;
}
free_mixer_state(ar);
mixer_close(ar->mixer);
free(ar);
}