blob: a9b9a0d136ab0be13bb52708f670de188f02a423 [file] [log] [blame]
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001/*
2 * Copyright (c) 2014, The Linux Foundation. All rights reserved.
3 * Not a Contribution.
4 *
5 * Copyright (C) 2014 The Android Open Source Project
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19
20#define LOG_TAG "audio_hw_utils"
21/* #define LOG_NDEBUG 0 */
22
23#include <errno.h>
24#include <cutils/properties.h>
25#include <cutils/config_utils.h>
26#include <stdlib.h>
27#include <dlfcn.h>
28#include <cutils/str_parms.h>
29#include <cutils/log.h>
30#include <cutils/misc.h>
31
32#include "audio_hw.h"
33#include "platform.h"
34#include "platform_api.h"
35#include "audio_extn.h"
36
37#define AUDIO_OUTPUT_POLICY_VENDOR_CONFIG_FILE "/vendor/etc/audio_output_policy.conf"
38
39#define OUTPUTS_TAG "outputs"
40
41#define DYNAMIC_VALUE_TAG "dynamic"
42#define FLAGS_TAG "flags"
43#define FORMATS_TAG "formats"
44#define SAMPLING_RATES_TAG "sampling_rates"
45#define BIT_WIDTH_TAG "bit_width"
46#define APP_TYPE_TAG "app_type"
47
48#define STRING_TO_ENUM(string) { #string, string }
49#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
50
Ben Rombergera04fabc2014-11-14 12:16:03 -080051#define BASE_TABLE_SIZE 64
52#define MAX_BASEINDEX_LEN 256
53
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070054struct string_to_enum {
55 const char *name;
56 uint32_t value;
57};
58
59const struct string_to_enum s_flag_name_to_enum_table[] = {
60 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_DIRECT),
61 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_PRIMARY),
62 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_FAST),
63 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_DEEP_BUFFER),
64 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD),
65 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_NON_BLOCKING),
66#ifdef INCALL_MUSIC_ENABLED
67 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_INCALL_MUSIC),
68#endif
69#ifdef COMPRESS_VOIP_ENABLED
70 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_VOIP_RX),
71#endif
72};
73
74const struct string_to_enum s_format_name_to_enum_table[] = {
75 STRING_TO_ENUM(AUDIO_FORMAT_PCM_16_BIT),
76 STRING_TO_ENUM(AUDIO_FORMAT_PCM_8_BIT),
77 STRING_TO_ENUM(AUDIO_FORMAT_MP3),
78 STRING_TO_ENUM(AUDIO_FORMAT_AAC),
79 STRING_TO_ENUM(AUDIO_FORMAT_VORBIS),
Mingming Yinae3530f2014-07-03 16:50:18 -070080 STRING_TO_ENUM(AUDIO_FORMAT_AMR_NB),
81 STRING_TO_ENUM(AUDIO_FORMAT_AMR_WB),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070082 STRING_TO_ENUM(AUDIO_FORMAT_AC3),
Mingming Yinae3530f2014-07-03 16:50:18 -070083 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3),
84#ifdef FORMATS_ENABLED
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070085 STRING_TO_ENUM(AUDIO_FORMAT_DTS),
86 STRING_TO_ENUM(AUDIO_FORMAT_DTS_LBR),
87 STRING_TO_ENUM(AUDIO_FORMAT_WMA),
88 STRING_TO_ENUM(AUDIO_FORMAT_WMA_PRO),
89 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADIF),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070090 STRING_TO_ENUM(AUDIO_FORMAT_AMR_WB_PLUS),
91 STRING_TO_ENUM(AUDIO_FORMAT_EVRC),
92 STRING_TO_ENUM(AUDIO_FORMAT_EVRCB),
93 STRING_TO_ENUM(AUDIO_FORMAT_EVRCWB),
94 STRING_TO_ENUM(AUDIO_FORMAT_QCELP),
95 STRING_TO_ENUM(AUDIO_FORMAT_MP2),
96 STRING_TO_ENUM(AUDIO_FORMAT_EVRCNW),
97 STRING_TO_ENUM(AUDIO_FORMAT_PCM_16_BIT_OFFLOAD),
98 STRING_TO_ENUM(AUDIO_FORMAT_PCM_24_BIT_OFFLOAD),
Amit Shekhar6f461b12014-08-01 14:52:58 -070099 STRING_TO_ENUM(AUDIO_FORMAT_FLAC),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700100#endif
101};
102
Ben Rombergera04fabc2014-11-14 12:16:03 -0800103static char bTable[BASE_TABLE_SIZE] = {
104 'A','B','C','D','E','F','G','H','I','J','K','L',
105 'M','N','O','P','Q','R','S','T','U','V','W','X',
106 'Y','Z','a','b','c','d','e','f','g','h','i','j',
107 'k','l','m','n','o','p','q','r','s','t','u','v',
108 'w','x','y','z','0','1','2','3','4','5','6','7',
109 '8','9','+','/'
110};
111
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700112static uint32_t string_to_enum(const struct string_to_enum *table, size_t size,
113 const char *name)
114{
115 size_t i;
116 for (i = 0; i < size; i++) {
117 if (strcmp(table[i].name, name) == 0) {
118 ALOGV("%s found %s", __func__, table[i].name);
119 return table[i].value;
120 }
121 }
122 return 0;
123}
124
125static audio_output_flags_t parse_flag_names(char *name)
126{
127 uint32_t flag = 0;
128 char *flag_name = strtok(name, "|");
129 while (flag_name != NULL) {
130 if (strlen(flag_name) != 0) {
131 flag |= string_to_enum(s_flag_name_to_enum_table,
132 ARRAY_SIZE(s_flag_name_to_enum_table),
133 flag_name);
134 }
135 flag_name = strtok(NULL, "|");
136 }
137
138 ALOGV("parse_flag_names: flag - %d", flag);
139 return (audio_output_flags_t)flag;
140}
141
142static void parse_format_names(char *name, struct streams_output_cfg *so_info)
143{
144 struct stream_format *sf_info = NULL;
145 char *str = strtok(name, "|");
146
147 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG) == 0)
148 return;
149
150 list_init(&so_info->format_list);
151 while (str != NULL) {
152 audio_format_t format = (audio_format_t)string_to_enum(s_format_name_to_enum_table,
153 ARRAY_SIZE(s_format_name_to_enum_table), str);
154 ALOGV("%s: format - %d", __func__, format);
155 if (format != 0) {
156 sf_info = (struct stream_format *)calloc(1, sizeof(struct stream_format));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700157 if (sf_info == NULL)
158 break; /* return whatever was parsed */
159
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700160 sf_info->format = format;
161 list_add_tail(&so_info->format_list, &sf_info->list);
162 }
163 str = strtok(NULL, "|");
164 }
165}
166
Amit Shekhar6f461b12014-08-01 14:52:58 -0700167static void parse_sample_rate_names(char *name, struct streams_output_cfg *so_info)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700168{
Amit Shekhar6f461b12014-08-01 14:52:58 -0700169 struct stream_sample_rate *ss_info = NULL;
170 uint32_t sample_rate = 48000;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700171 char *str = strtok(name, "|");
172
Amit Shekhar6f461b12014-08-01 14:52:58 -0700173 if (str != NULL && 0 == strcmp(str, DYNAMIC_VALUE_TAG))
174 return;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700175
Amit Shekhar6f461b12014-08-01 14:52:58 -0700176 list_init(&so_info->sample_rate_list);
177 while (str != NULL) {
178 sample_rate = (uint32_t)strtol(str, (char **)NULL, 10);
179 ALOGV("%s: sample_rate - %d", __func__, sample_rate);
180 if (0 != sample_rate) {
181 ss_info = (struct stream_sample_rate *)calloc(1, sizeof(struct stream_sample_rate));
182 ss_info->sample_rate = sample_rate;
183 list_add_tail(&so_info->sample_rate_list, &ss_info->list);
184 }
185 str = strtok(NULL, "|");
186 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700187}
188
189static int parse_bit_width_names(char *name)
190{
191 int bit_width = 16;
192 char *str = strtok(name, "|");
193
194 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG))
195 bit_width = (int)strtol(str, (char **)NULL, 10);
196
197 ALOGV("%s: bit_width - %d", __func__, bit_width);
198 return bit_width;
199}
200
201static int parse_app_type_names(void *platform, char *name)
202{
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700203 int app_type = platform_get_default_app_type(platform);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700204 char *str = strtok(name, "|");
205
206 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG))
207 app_type = (int)strtol(str, (char **)NULL, 10);
208
209 ALOGV("%s: app_type - %d", __func__, app_type);
210 return app_type;
211}
212
213static void update_streams_output_cfg_list(cnode *root, void *platform,
214 struct listnode *streams_output_cfg_list)
215{
216 cnode *node = root->first_child;
217 struct streams_output_cfg *so_info;
218
219 ALOGV("%s", __func__);
220 so_info = (struct streams_output_cfg *)calloc(1, sizeof(struct streams_output_cfg));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700221
222 if (!so_info) {
223 ALOGE("failed to allocate mem for so_info list element");
224 return;
225 }
226
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700227 while (node) {
228 if (strcmp(node->name, FLAGS_TAG) == 0) {
229 so_info->flags = parse_flag_names((char *)node->value);
230 } else if (strcmp(node->name, FORMATS_TAG) == 0) {
231 parse_format_names((char *)node->value, so_info);
232 } else if (strcmp(node->name, SAMPLING_RATES_TAG) == 0) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700233 so_info->app_type_cfg.sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
234 parse_sample_rate_names((char *)node->value, so_info);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700235 } else if (strcmp(node->name, BIT_WIDTH_TAG) == 0) {
236 so_info->app_type_cfg.bit_width = parse_bit_width_names((char *)node->value);
237 } else if (strcmp(node->name, APP_TYPE_TAG) == 0) {
238 so_info->app_type_cfg.app_type = parse_app_type_names(platform, (char *)node->value);
239 }
240 node = node->next;
241 }
242 list_add_tail(streams_output_cfg_list, &so_info->list);
243}
244
245static void load_output(cnode *root, void *platform,
246 struct listnode *streams_output_cfg_list)
247{
248 cnode *node = config_find(root, OUTPUTS_TAG);
249 if (node == NULL) {
250 ALOGE("%s: could not load output, node is NULL", __func__);
251 return;
252 }
253
254 node = node->first_child;
255 while (node) {
256 ALOGV("%s: loading output %s", __func__, node->name);
257 update_streams_output_cfg_list(node, platform, streams_output_cfg_list);
258 node = node->next;
259 }
260}
261
262static void send_app_type_cfg(void *platform, struct mixer *mixer,
263 struct listnode *streams_output_cfg_list)
264{
265 int app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT] = {-1};
266 int length = 0, i, num_app_types = 0;
267 struct listnode *node;
268 bool update;
269 struct mixer_ctl *ctl = NULL;
270 const char *mixer_ctl_name = "App Type Config";
271 struct streams_output_cfg *so_info;
272
273 if (!mixer) {
274 ALOGE("%s: mixer is null",__func__);
275 return;
276 }
277 ctl = mixer_get_ctl_by_name(mixer, mixer_ctl_name);
278 if (!ctl) {
279 ALOGE("%s: Could not get ctl for mixer cmd - %s",__func__, mixer_ctl_name);
280 return;
281 }
282 if (streams_output_cfg_list == NULL) {
283 app_type_cfg[length++] = 1;
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700284 app_type_cfg[length++] = platform_get_default_app_type(platform);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700285 app_type_cfg[length++] = 48000;
286 app_type_cfg[length++] = 16;
287 mixer_ctl_set_array(ctl, app_type_cfg, length);
288 return;
289 }
290
291 app_type_cfg[length++] = num_app_types;
292 list_for_each(node, streams_output_cfg_list) {
293 so_info = node_to_item(node, struct streams_output_cfg, list);
294 update = true;
295 for (i=0; i<length; i=i+3) {
296 if (app_type_cfg[i+1] == -1)
297 break;
298 else if (app_type_cfg[i+1] == so_info->app_type_cfg.app_type) {
299 update = false;
300 break;
301 }
302 }
303 if (update && ((length + 3) <= MAX_LENGTH_MIXER_CONTROL_IN_INT)) {
304 num_app_types += 1 ;
305 app_type_cfg[length++] = so_info->app_type_cfg.app_type;
306 app_type_cfg[length++] = so_info->app_type_cfg.sample_rate;
307 app_type_cfg[length++] = so_info->app_type_cfg.bit_width;
308 }
309 }
310 ALOGV("%s: num_app_types: %d", __func__, num_app_types);
311 if (num_app_types) {
312 app_type_cfg[0] = num_app_types;
313 mixer_ctl_set_array(ctl, app_type_cfg, length);
314 }
315}
316
317void audio_extn_utils_update_streams_output_cfg_list(void *platform,
318 struct mixer *mixer,
319 struct listnode *streams_output_cfg_list)
320{
321 cnode *root;
322 char *data;
323
324 ALOGV("%s", __func__);
325 list_init(streams_output_cfg_list);
326 data = (char *)load_file(AUDIO_OUTPUT_POLICY_VENDOR_CONFIG_FILE, NULL);
327 if (data == NULL) {
328 send_app_type_cfg(platform, mixer, NULL);
329 ALOGE("%s: could not load output policy config file", __func__);
330 return;
331 }
332
333 root = config_node("", "");
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700334 if (root == NULL) {
335 ALOGE("cfg_list, NULL config root");
336 return;
337 }
338
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700339 config_load(root, data);
340 load_output(root, platform, streams_output_cfg_list);
341
342 send_app_type_cfg(platform, mixer, streams_output_cfg_list);
343}
344
345void audio_extn_utils_dump_streams_output_cfg_list(
346 struct listnode *streams_output_cfg_list)
347{
348 int i=0;
349 struct listnode *node_i, *node_j;
350 struct streams_output_cfg *so_info;
351 struct stream_format *sf_info;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700352 struct stream_sample_rate *ss_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700353 ALOGV("%s", __func__);
354 list_for_each(node_i, streams_output_cfg_list) {
355 so_info = node_to_item(node_i, struct streams_output_cfg, list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700356 ALOGV("%s: flags-%d, output_sample_rate-%d, output_bit_width-%d, app_type-%d",
357 __func__, so_info->flags, so_info->app_type_cfg.sample_rate,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700358 so_info->app_type_cfg.bit_width, so_info->app_type_cfg.app_type);
359 list_for_each(node_j, &so_info->format_list) {
360 sf_info = node_to_item(node_j, struct stream_format, list);
361 ALOGV("format-%x", sf_info->format);
362 }
Amit Shekhar6f461b12014-08-01 14:52:58 -0700363 list_for_each(node_j, &so_info->sample_rate_list) {
364 ss_info = node_to_item(node_j, struct stream_sample_rate, list);
365 ALOGV("sample rate-%d", ss_info->sample_rate);
366 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700367 }
368}
369
370void audio_extn_utils_release_streams_output_cfg_list(
371 struct listnode *streams_output_cfg_list)
372{
373 struct listnode *node_i, *node_j;
374 struct streams_output_cfg *so_info;
375 struct stream_format *sf_info;
376
377 ALOGV("%s", __func__);
378 while (!list_empty(streams_output_cfg_list)) {
379 node_i = list_head(streams_output_cfg_list);
380 so_info = node_to_item(node_i, struct streams_output_cfg, list);
381 while (!list_empty(&so_info->format_list)) {
382 node_j = list_head(&so_info->format_list);
383 list_remove(node_j);
384 free(node_to_item(node_j, struct stream_format, list));
385 }
Amit Shekhar6f461b12014-08-01 14:52:58 -0700386 while (!list_empty(&so_info->sample_rate_list)) {
387 node_j = list_head(&so_info->sample_rate_list);
388 list_remove(node_j);
389 free(node_to_item(node_j, struct stream_sample_rate, list));
390 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700391 list_remove(node_i);
392 free(node_to_item(node_i, struct streams_output_cfg, list));
393 }
394}
395
Amit Shekhar6f461b12014-08-01 14:52:58 -0700396static bool set_output_cfg(struct streams_output_cfg *so_info,
397 struct stream_app_type_cfg *app_type_cfg,
398 uint32_t sample_rate, uint32_t bit_width)
399 {
400 struct listnode *node_i;
401 struct stream_sample_rate *ss_info;
402 list_for_each(node_i, &so_info->sample_rate_list) {
403 ss_info = node_to_item(node_i, struct stream_sample_rate, list);
404 if ((sample_rate <= ss_info->sample_rate) &&
405 (bit_width == so_info->app_type_cfg.bit_width)) {
406 app_type_cfg->app_type = so_info->app_type_cfg.app_type;
407 app_type_cfg->sample_rate = ss_info->sample_rate;
408 app_type_cfg->bit_width = so_info->app_type_cfg.bit_width;
409 ALOGV("%s app_type_cfg->app_type %d, app_type_cfg->sample_rate %d, app_type_cfg->bit_width %d",
410 __func__, app_type_cfg->app_type, app_type_cfg->sample_rate, app_type_cfg->bit_width);
411 return true;
412 }
413 }
414 /*
415 * Reiterate through the list assuming dafault sample rate.
416 * Handles scenario where input sample rate is higher
417 * than all sample rates in list for the input bit width.
418 */
419 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
420 list_for_each(node_i, &so_info->sample_rate_list) {
421 ss_info = node_to_item(node_i, struct stream_sample_rate, list);
422 if ((sample_rate <= ss_info->sample_rate) &&
423 (bit_width == so_info->app_type_cfg.bit_width)) {
424 app_type_cfg->app_type = so_info->app_type_cfg.app_type;
425 app_type_cfg->sample_rate = sample_rate;
426 app_type_cfg->bit_width = so_info->app_type_cfg.bit_width;
427 ALOGV("%s Assuming default sample rate. app_type_cfg->app_type %d, app_type_cfg->sample_rate %d, app_type_cfg->bit_width %d",
428 __func__, app_type_cfg->app_type, app_type_cfg->sample_rate, app_type_cfg->bit_width);
429 return true;
430 }
431 }
432 return false;
433}
434
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700435void audio_extn_utils_update_stream_app_type_cfg(void *platform,
436 struct listnode *streams_output_cfg_list,
Amit Shekhar1d896042014-10-03 13:16:09 -0700437 audio_devices_t devices,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700438 audio_output_flags_t flags,
439 audio_format_t format,
Amit Shekhar6f461b12014-08-01 14:52:58 -0700440 uint32_t sample_rate,
441 uint32_t bit_width,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700442 struct stream_app_type_cfg *app_type_cfg)
443{
Amit Shekhar6f461b12014-08-01 14:52:58 -0700444 struct listnode *node_i, *node_j, *node_k;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700445 struct streams_output_cfg *so_info;
446 struct stream_format *sf_info;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700447 struct stream_sample_rate *ss_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700448
Amit Shekhar1d896042014-10-03 13:16:09 -0700449 if ((24 == bit_width) &&
450 (devices & AUDIO_DEVICE_OUT_SPEAKER)) {
Amit Shekhar5a39c912014-10-14 15:39:30 -0700451 int32_t bw = platform_get_snd_device_bit_width(SND_DEVICE_OUT_SPEAKER);
452 if (-ENOSYS != bw)
453 bit_width = (uint32_t)bw;
Amit Shekhar1d896042014-10-03 13:16:09 -0700454 sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
455 ALOGI("%s Allowing 24-bit playback on speaker ONLY at default sampling rate", __func__);
456 }
457
458 ALOGV("%s: flags: %x, format: %x sample_rate %d",
459 __func__, flags, format, sample_rate);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700460 list_for_each(node_i, streams_output_cfg_list) {
461 so_info = node_to_item(node_i, struct streams_output_cfg, list);
462 if (so_info->flags == flags) {
463 list_for_each(node_j, &so_info->format_list) {
464 sf_info = node_to_item(node_j, struct stream_format, list);
465 if (sf_info->format == format) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700466 if (set_output_cfg(so_info, app_type_cfg, sample_rate, bit_width))
467 return;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700468 }
469 }
470 }
471 }
472 list_for_each(node_i, streams_output_cfg_list) {
473 so_info = node_to_item(node_i, struct streams_output_cfg, list);
474 if (so_info->flags == AUDIO_OUTPUT_FLAG_PRIMARY) {
475 ALOGV("Compatible output profile not found.");
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700476 app_type_cfg->app_type = so_info->app_type_cfg.app_type;
477 app_type_cfg->sample_rate = so_info->app_type_cfg.sample_rate;
478 app_type_cfg->bit_width = so_info->app_type_cfg.bit_width;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700479 ALOGV("%s Default to primary output: App type: %d sample_rate %d",
480 __func__, so_info->app_type_cfg.app_type, app_type_cfg->sample_rate);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700481 return;
482 }
483 }
484 ALOGW("%s: App type could not be selected. Falling back to default", __func__);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700485 app_type_cfg->app_type = platform_get_default_app_type(platform);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700486 app_type_cfg->sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700487 app_type_cfg->bit_width = 16;
488}
489
490int audio_extn_utils_send_app_type_cfg(struct audio_usecase *usecase)
491{
492 char mixer_ctl_name[MAX_LENGTH_MIXER_CONTROL_IN_INT];
493 int app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT], len = 0, rc;
Anish Kumarc8599f22014-07-20 09:36:07 -0700494 struct stream_out *out;
495 struct audio_device *adev;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700496 struct mixer_ctl *ctl;
497 int pcm_device_id, acdb_dev_id, snd_device = usecase->out_snd_device;
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +0530498 int32_t sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700499
500 ALOGV("%s", __func__);
501
502 if (usecase->type != PCM_PLAYBACK) {
503 ALOGV("%s: not a playback path, no need to cfg app type", __func__);
504 rc = 0;
505 goto exit_send_app_type_cfg;
506 }
507 if ((usecase->id != USECASE_AUDIO_PLAYBACK_DEEP_BUFFER) &&
508 (usecase->id != USECASE_AUDIO_PLAYBACK_LOW_LATENCY) &&
509 (usecase->id != USECASE_AUDIO_PLAYBACK_MULTI_CH) &&
510 (usecase->id != USECASE_AUDIO_PLAYBACK_OFFLOAD)) {
511 ALOGV("%s: a playback path where app type cfg is not required", __func__);
512 rc = 0;
513 goto exit_send_app_type_cfg;
514 }
Anish Kumarc8599f22014-07-20 09:36:07 -0700515 out = usecase->stream.out;
516 adev = out->dev;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700517
518 snd_device = usecase->out_snd_device;
519
520 pcm_device_id = platform_get_pcm_device_id(out->usecase, PCM_PLAYBACK);
521
522 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
523 "Audio Stream %d App Type Cfg", pcm_device_id);
524
525 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
526 if (!ctl) {
527 ALOGE("%s: Could not get ctl for mixer cmd - %s", __func__,
528 mixer_ctl_name);
529 rc = -EINVAL;
530 goto exit_send_app_type_cfg;
531 }
532 snd_device = (snd_device == SND_DEVICE_OUT_SPEAKER) ?
533 audio_extn_get_spkr_prot_snd_device(snd_device) : snd_device;
534 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
535 if (acdb_dev_id < 0) {
536 ALOGE("%s: Couldn't get the acdb dev id", __func__);
537 rc = -EINVAL;
538 goto exit_send_app_type_cfg;
539 }
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +0530540
541 if ((24 == usecase->stream.out->bit_width) &&
Amit Shekhar1d896042014-10-03 13:16:09 -0700542 (usecase->stream.out->devices & AUDIO_DEVICE_OUT_SPEAKER)) {
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +0530543 sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
544 } else {
545 sample_rate = out->app_type_cfg.sample_rate;
546 }
547
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700548 app_type_cfg[len++] = out->app_type_cfg.app_type;
549 app_type_cfg[len++] = acdb_dev_id;
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +0530550 app_type_cfg[len++] = sample_rate;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700551
552 mixer_ctl_set_array(ctl, app_type_cfg, len);
Amit Shekhar278e3362014-09-08 14:08:19 -0700553 ALOGI("%s app_type %d, acdb_dev_id %d, sample_rate %d",
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +0530554 __func__, out->app_type_cfg.app_type, acdb_dev_id, sample_rate);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700555 rc = 0;
556exit_send_app_type_cfg:
557 return rc;
558}
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700559
560void audio_extn_utils_send_audio_calibration(struct audio_device *adev,
561 struct audio_usecase *usecase)
562{
563 int type = usecase->type;
564
565 if (type == PCM_PLAYBACK) {
566 struct stream_out *out = usecase->stream.out;
567 int snd_device = usecase->out_snd_device;
568 snd_device = (snd_device == SND_DEVICE_OUT_SPEAKER) ?
569 audio_extn_get_spkr_prot_snd_device(snd_device) : snd_device;
570 platform_send_audio_calibration(adev->platform, usecase->out_snd_device,
571 out->app_type_cfg.app_type,
572 out->app_type_cfg.sample_rate);
573 }
574 if ((type == PCM_HFP_CALL) || (type == PCM_CAPTURE)) {
575 /* when app type is default. the sample rate is not used to send cal */
576 platform_send_audio_calibration(adev->platform, usecase->in_snd_device,
577 platform_get_default_app_type(adev->platform),
578 48000);
579 }
580}
581
Ben Rombergera04fabc2014-11-14 12:16:03 -0800582// Base64 Encode and Decode
583// Not all features supported. This must be used only with following conditions.
584// Decode Modes: Support with and without padding
585// CRLF not handling. So no CRLF in string to decode.
586// Encode Modes: Supports only padding
587int b64decode(char *inp, int ilen, uint8_t* outp)
588{
589 int i, j, k, ii, num;
590 int rem, pcnt;
591 uint32_t res=0;
592 uint8_t getIndex[MAX_BASEINDEX_LEN];
593 uint8_t tmp, cflag;
594
595 if(inp == NULL || outp == NULL || ilen <= 0) {
596 ALOGE("[%s] received NULL pointer or zero length",__func__);
597 return -1;
598 }
599
600 memset(getIndex, MAX_BASEINDEX_LEN-1, sizeof(getIndex));
601 for(i=0;i<BASE_TABLE_SIZE;i++) {
602 getIndex[(uint8_t)bTable[i]] = (uint8_t)i;
603 }
604 getIndex[(uint8_t)'=']=0;
605
606 j=0;k=0;
607 num = ilen/4;
608 rem = ilen%4;
609 if(rem==0)
610 num = num-1;
611 cflag=0;
612 for(i=0; i<num; i++) {
613 res=0;
614 for(ii=0;ii<4;ii++) {
615 res = res << 6;
616 tmp = getIndex[(uint8_t)inp[j++]];
617 res = res | tmp;
618 cflag = cflag | tmp;
619 }
620 outp[k++] = (res >> 16)&0xFF;
621 outp[k++] = (res >> 8)&0xFF;
622 outp[k++] = res & 0xFF;
623 }
624
625 // Handle last bytes special
626 pcnt=0;
627 if(rem == 0) {
628 //With padding or full data
629 res = 0;
630 for(ii=0;ii<4;ii++) {
631 if(inp[j] == '=')
632 pcnt++;
633 res = res << 6;
634 tmp = getIndex[(uint8_t)inp[j++]];
635 res = res | tmp;
636 cflag = cflag | tmp;
637 }
638 outp[k++] = res >> 16;
639 if(pcnt == 2)
640 goto done;
641 outp[k++] = (res>>8)&0xFF;
642 if(pcnt == 1)
643 goto done;
644 outp[k++] = res&0xFF;
645 } else {
646 //without padding
647 res = 0;
648 for(i=0;i<rem;i++) {
649 res = res << 6;
650 tmp = getIndex[(uint8_t)inp[j++]];
651 res = res | tmp;
652 cflag = cflag | tmp;
653 }
654 for(i=rem;i<4;i++) {
655 res = res << 6;
656 pcnt++;
657 }
658 outp[k++] = res >> 16;
659 if(pcnt == 2)
660 goto done;
661 outp[k++] = (res>>8)&0xFF;
662 if(pcnt == 1)
663 goto done;
664 outp[k++] = res&0xFF;
665 }
666done:
667 if(cflag == 0xFF) {
668 ALOGE("[%s] base64 decode failed. Invalid character found %s",
669 __func__, inp);
670 return 0;
671 }
672 return k;
673}
674
675int b64encode(uint8_t *inp, int ilen, char* outp)
676{
677 int i,j,k, num;
678 int rem=0;
679 uint32_t res=0;
680
681 if(inp == NULL || outp == NULL || ilen<=0) {
682 ALOGE("[%s] received NULL pointer or zero input length",__func__);
683 return -1;
684 }
685
686 num = ilen/3;
687 rem = ilen%3;
688 j=0;k=0;
689 for(i=0; i<num; i++) {
690 //prepare index
691 res = inp[j++]<<16;
692 res = res | inp[j++]<<8;
693 res = res | inp[j++];
694 //get output map from index
695 outp[k++] = (char) bTable[(res>>18)&0x3F];
696 outp[k++] = (char) bTable[(res>>12)&0x3F];
697 outp[k++] = (char) bTable[(res>>6)&0x3F];
698 outp[k++] = (char) bTable[res&0x3F];
699 }
700
701 switch(rem) {
702 case 1:
703 res = inp[j++]<<16;
704 outp[k++] = (char) bTable[res>>18];
705 outp[k++] = (char) bTable[(res>>12)&0x3F];
706 //outp[k++] = '=';
707 //outp[k++] = '=';
708 break;
709 case 2:
710 res = inp[j++]<<16;
711 res = res | inp[j++]<<8;
712 outp[k++] = (char) bTable[res>>18];
713 outp[k++] = (char) bTable[(res>>12)&0x3F];
714 outp[k++] = (char) bTable[(res>>6)&0x3F];
715 //outp[k++] = '=';
716 break;
717 default:
718 break;
719 }
720done:
721 outp[k] = '\0';
722 return k;
723}