blob: 7ad48115d1cf37526be86d7ce5b0d9b942caef9f [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"
Narsinga Rao Chella212e2542014-11-17 19:57:04 -080036#include "voice.h"
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070037
38#define AUDIO_OUTPUT_POLICY_VENDOR_CONFIG_FILE "/vendor/etc/audio_output_policy.conf"
39
40#define OUTPUTS_TAG "outputs"
41
42#define DYNAMIC_VALUE_TAG "dynamic"
43#define FLAGS_TAG "flags"
44#define FORMATS_TAG "formats"
45#define SAMPLING_RATES_TAG "sampling_rates"
46#define BIT_WIDTH_TAG "bit_width"
47#define APP_TYPE_TAG "app_type"
48
49#define STRING_TO_ENUM(string) { #string, string }
50#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
51
Ben Rombergera04fabc2014-11-14 12:16:03 -080052#define BASE_TABLE_SIZE 64
53#define MAX_BASEINDEX_LEN 256
54
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070055struct string_to_enum {
56 const char *name;
57 uint32_t value;
58};
59
60const struct string_to_enum s_flag_name_to_enum_table[] = {
61 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_DIRECT),
62 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_PRIMARY),
63 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_FAST),
64 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_DEEP_BUFFER),
65 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD),
66 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_NON_BLOCKING),
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -070067 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_HW_AV_SYNC),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070068#ifdef INCALL_MUSIC_ENABLED
69 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_INCALL_MUSIC),
70#endif
71#ifdef COMPRESS_VOIP_ENABLED
72 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_VOIP_RX),
73#endif
Mingming Yin497419f2015-07-01 16:57:32 -070074#ifdef HDMI_PASSTHROUGH_ENABLED
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -070075 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH),
Mingming Yin497419f2015-07-01 16:57:32 -070076#endif
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070077};
78
79const struct string_to_enum s_format_name_to_enum_table[] = {
80 STRING_TO_ENUM(AUDIO_FORMAT_PCM_16_BIT),
81 STRING_TO_ENUM(AUDIO_FORMAT_PCM_8_BIT),
82 STRING_TO_ENUM(AUDIO_FORMAT_MP3),
83 STRING_TO_ENUM(AUDIO_FORMAT_AAC),
84 STRING_TO_ENUM(AUDIO_FORMAT_VORBIS),
Mingming Yinae3530f2014-07-03 16:50:18 -070085 STRING_TO_ENUM(AUDIO_FORMAT_AMR_NB),
86 STRING_TO_ENUM(AUDIO_FORMAT_AMR_WB),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070087 STRING_TO_ENUM(AUDIO_FORMAT_AC3),
Mingming Yinae3530f2014-07-03 16:50:18 -070088 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3),
89#ifdef FORMATS_ENABLED
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070090 STRING_TO_ENUM(AUDIO_FORMAT_DTS),
91 STRING_TO_ENUM(AUDIO_FORMAT_DTS_LBR),
92 STRING_TO_ENUM(AUDIO_FORMAT_WMA),
93 STRING_TO_ENUM(AUDIO_FORMAT_WMA_PRO),
94 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADIF),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070095 STRING_TO_ENUM(AUDIO_FORMAT_AMR_WB_PLUS),
96 STRING_TO_ENUM(AUDIO_FORMAT_EVRC),
97 STRING_TO_ENUM(AUDIO_FORMAT_EVRCB),
98 STRING_TO_ENUM(AUDIO_FORMAT_EVRCWB),
99 STRING_TO_ENUM(AUDIO_FORMAT_QCELP),
100 STRING_TO_ENUM(AUDIO_FORMAT_MP2),
101 STRING_TO_ENUM(AUDIO_FORMAT_EVRCNW),
102 STRING_TO_ENUM(AUDIO_FORMAT_PCM_16_BIT_OFFLOAD),
103 STRING_TO_ENUM(AUDIO_FORMAT_PCM_24_BIT_OFFLOAD),
Amit Shekhar6f461b12014-08-01 14:52:58 -0700104 STRING_TO_ENUM(AUDIO_FORMAT_FLAC),
Satya Krishna Pindiproli70471602015-04-24 19:12:43 +0530105 STRING_TO_ENUM(AUDIO_FORMAT_ALAC),
106 STRING_TO_ENUM(AUDIO_FORMAT_APE),
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700107 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3_JOC),
Alexy Josephcd8eaed2014-12-11 12:46:53 -0800108 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LC),
109 STRING_TO_ENUM(AUDIO_FORMAT_AAC_HE_V1),
110 STRING_TO_ENUM(AUDIO_FORMAT_AAC_HE_V2),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700111#endif
112};
113
Ben Rombergera04fabc2014-11-14 12:16:03 -0800114static char bTable[BASE_TABLE_SIZE] = {
115 'A','B','C','D','E','F','G','H','I','J','K','L',
116 'M','N','O','P','Q','R','S','T','U','V','W','X',
117 'Y','Z','a','b','c','d','e','f','g','h','i','j',
118 'k','l','m','n','o','p','q','r','s','t','u','v',
119 'w','x','y','z','0','1','2','3','4','5','6','7',
120 '8','9','+','/'
121};
122
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700123static uint32_t string_to_enum(const struct string_to_enum *table, size_t size,
124 const char *name)
125{
126 size_t i;
127 for (i = 0; i < size; i++) {
128 if (strcmp(table[i].name, name) == 0) {
129 ALOGV("%s found %s", __func__, table[i].name);
130 return table[i].value;
131 }
132 }
133 return 0;
134}
135
136static audio_output_flags_t parse_flag_names(char *name)
137{
138 uint32_t flag = 0;
139 char *flag_name = strtok(name, "|");
140 while (flag_name != NULL) {
141 if (strlen(flag_name) != 0) {
142 flag |= string_to_enum(s_flag_name_to_enum_table,
143 ARRAY_SIZE(s_flag_name_to_enum_table),
144 flag_name);
145 }
146 flag_name = strtok(NULL, "|");
147 }
148
149 ALOGV("parse_flag_names: flag - %d", flag);
150 return (audio_output_flags_t)flag;
151}
152
153static void parse_format_names(char *name, struct streams_output_cfg *so_info)
154{
155 struct stream_format *sf_info = NULL;
156 char *str = strtok(name, "|");
157
158 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG) == 0)
159 return;
160
161 list_init(&so_info->format_list);
162 while (str != NULL) {
163 audio_format_t format = (audio_format_t)string_to_enum(s_format_name_to_enum_table,
164 ARRAY_SIZE(s_format_name_to_enum_table), str);
165 ALOGV("%s: format - %d", __func__, format);
166 if (format != 0) {
167 sf_info = (struct stream_format *)calloc(1, sizeof(struct stream_format));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700168 if (sf_info == NULL)
169 break; /* return whatever was parsed */
170
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700171 sf_info->format = format;
172 list_add_tail(&so_info->format_list, &sf_info->list);
173 }
174 str = strtok(NULL, "|");
175 }
176}
177
Amit Shekhar6f461b12014-08-01 14:52:58 -0700178static void parse_sample_rate_names(char *name, struct streams_output_cfg *so_info)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700179{
Amit Shekhar6f461b12014-08-01 14:52:58 -0700180 struct stream_sample_rate *ss_info = NULL;
181 uint32_t sample_rate = 48000;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700182 char *str = strtok(name, "|");
183
Amit Shekhar6f461b12014-08-01 14:52:58 -0700184 if (str != NULL && 0 == strcmp(str, DYNAMIC_VALUE_TAG))
185 return;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700186
Amit Shekhar6f461b12014-08-01 14:52:58 -0700187 list_init(&so_info->sample_rate_list);
188 while (str != NULL) {
189 sample_rate = (uint32_t)strtol(str, (char **)NULL, 10);
190 ALOGV("%s: sample_rate - %d", __func__, sample_rate);
191 if (0 != sample_rate) {
192 ss_info = (struct stream_sample_rate *)calloc(1, sizeof(struct stream_sample_rate));
Chaithanya Krishna Bacharajudb6236a2015-01-30 10:08:33 +0530193 if (ss_info == NULL)
194 break; /* return whatever was parsed */
195
Amit Shekhar6f461b12014-08-01 14:52:58 -0700196 ss_info->sample_rate = sample_rate;
197 list_add_tail(&so_info->sample_rate_list, &ss_info->list);
198 }
199 str = strtok(NULL, "|");
200 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700201}
202
203static int parse_bit_width_names(char *name)
204{
205 int bit_width = 16;
206 char *str = strtok(name, "|");
207
208 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG))
209 bit_width = (int)strtol(str, (char **)NULL, 10);
210
211 ALOGV("%s: bit_width - %d", __func__, bit_width);
212 return bit_width;
213}
214
215static int parse_app_type_names(void *platform, char *name)
216{
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700217 int app_type = platform_get_default_app_type(platform);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700218 char *str = strtok(name, "|");
219
220 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG))
221 app_type = (int)strtol(str, (char **)NULL, 10);
222
223 ALOGV("%s: app_type - %d", __func__, app_type);
224 return app_type;
225}
226
227static void update_streams_output_cfg_list(cnode *root, void *platform,
228 struct listnode *streams_output_cfg_list)
229{
230 cnode *node = root->first_child;
231 struct streams_output_cfg *so_info;
232
233 ALOGV("%s", __func__);
234 so_info = (struct streams_output_cfg *)calloc(1, sizeof(struct streams_output_cfg));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700235
236 if (!so_info) {
237 ALOGE("failed to allocate mem for so_info list element");
238 return;
239 }
240
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700241 while (node) {
242 if (strcmp(node->name, FLAGS_TAG) == 0) {
243 so_info->flags = parse_flag_names((char *)node->value);
244 } else if (strcmp(node->name, FORMATS_TAG) == 0) {
245 parse_format_names((char *)node->value, so_info);
246 } else if (strcmp(node->name, SAMPLING_RATES_TAG) == 0) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700247 so_info->app_type_cfg.sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
248 parse_sample_rate_names((char *)node->value, so_info);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700249 } else if (strcmp(node->name, BIT_WIDTH_TAG) == 0) {
250 so_info->app_type_cfg.bit_width = parse_bit_width_names((char *)node->value);
251 } else if (strcmp(node->name, APP_TYPE_TAG) == 0) {
252 so_info->app_type_cfg.app_type = parse_app_type_names(platform, (char *)node->value);
253 }
254 node = node->next;
255 }
256 list_add_tail(streams_output_cfg_list, &so_info->list);
257}
258
259static void load_output(cnode *root, void *platform,
260 struct listnode *streams_output_cfg_list)
261{
262 cnode *node = config_find(root, OUTPUTS_TAG);
263 if (node == NULL) {
264 ALOGE("%s: could not load output, node is NULL", __func__);
265 return;
266 }
267
268 node = node->first_child;
269 while (node) {
270 ALOGV("%s: loading output %s", __func__, node->name);
271 update_streams_output_cfg_list(node, platform, streams_output_cfg_list);
272 node = node->next;
273 }
274}
275
276static void send_app_type_cfg(void *platform, struct mixer *mixer,
277 struct listnode *streams_output_cfg_list)
278{
279 int app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT] = {-1};
280 int length = 0, i, num_app_types = 0;
281 struct listnode *node;
282 bool update;
283 struct mixer_ctl *ctl = NULL;
284 const char *mixer_ctl_name = "App Type Config";
285 struct streams_output_cfg *so_info;
286
287 if (!mixer) {
288 ALOGE("%s: mixer is null",__func__);
289 return;
290 }
291 ctl = mixer_get_ctl_by_name(mixer, mixer_ctl_name);
292 if (!ctl) {
293 ALOGE("%s: Could not get ctl for mixer cmd - %s",__func__, mixer_ctl_name);
294 return;
295 }
296 if (streams_output_cfg_list == NULL) {
297 app_type_cfg[length++] = 1;
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700298 app_type_cfg[length++] = platform_get_default_app_type(platform);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700299 app_type_cfg[length++] = 48000;
300 app_type_cfg[length++] = 16;
301 mixer_ctl_set_array(ctl, app_type_cfg, length);
302 return;
303 }
304
305 app_type_cfg[length++] = num_app_types;
306 list_for_each(node, streams_output_cfg_list) {
307 so_info = node_to_item(node, struct streams_output_cfg, list);
308 update = true;
309 for (i=0; i<length; i=i+3) {
310 if (app_type_cfg[i+1] == -1)
311 break;
312 else if (app_type_cfg[i+1] == so_info->app_type_cfg.app_type) {
313 update = false;
314 break;
315 }
316 }
317 if (update && ((length + 3) <= MAX_LENGTH_MIXER_CONTROL_IN_INT)) {
318 num_app_types += 1 ;
319 app_type_cfg[length++] = so_info->app_type_cfg.app_type;
320 app_type_cfg[length++] = so_info->app_type_cfg.sample_rate;
321 app_type_cfg[length++] = so_info->app_type_cfg.bit_width;
322 }
323 }
324 ALOGV("%s: num_app_types: %d", __func__, num_app_types);
325 if (num_app_types) {
326 app_type_cfg[0] = num_app_types;
327 mixer_ctl_set_array(ctl, app_type_cfg, length);
328 }
329}
330
331void audio_extn_utils_update_streams_output_cfg_list(void *platform,
332 struct mixer *mixer,
333 struct listnode *streams_output_cfg_list)
334{
335 cnode *root;
336 char *data;
337
338 ALOGV("%s", __func__);
339 list_init(streams_output_cfg_list);
340 data = (char *)load_file(AUDIO_OUTPUT_POLICY_VENDOR_CONFIG_FILE, NULL);
341 if (data == NULL) {
342 send_app_type_cfg(platform, mixer, NULL);
343 ALOGE("%s: could not load output policy config file", __func__);
344 return;
345 }
346
347 root = config_node("", "");
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700348 if (root == NULL) {
349 ALOGE("cfg_list, NULL config root");
350 return;
351 }
352
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700353 config_load(root, data);
354 load_output(root, platform, streams_output_cfg_list);
355
356 send_app_type_cfg(platform, mixer, streams_output_cfg_list);
357}
358
359void audio_extn_utils_dump_streams_output_cfg_list(
360 struct listnode *streams_output_cfg_list)
361{
362 int i=0;
363 struct listnode *node_i, *node_j;
364 struct streams_output_cfg *so_info;
365 struct stream_format *sf_info;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700366 struct stream_sample_rate *ss_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700367 ALOGV("%s", __func__);
368 list_for_each(node_i, streams_output_cfg_list) {
369 so_info = node_to_item(node_i, struct streams_output_cfg, list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700370 ALOGV("%s: flags-%d, output_sample_rate-%d, output_bit_width-%d, app_type-%d",
371 __func__, so_info->flags, so_info->app_type_cfg.sample_rate,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700372 so_info->app_type_cfg.bit_width, so_info->app_type_cfg.app_type);
373 list_for_each(node_j, &so_info->format_list) {
374 sf_info = node_to_item(node_j, struct stream_format, list);
375 ALOGV("format-%x", sf_info->format);
376 }
Amit Shekhar6f461b12014-08-01 14:52:58 -0700377 list_for_each(node_j, &so_info->sample_rate_list) {
378 ss_info = node_to_item(node_j, struct stream_sample_rate, list);
379 ALOGV("sample rate-%d", ss_info->sample_rate);
380 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700381 }
382}
383
384void audio_extn_utils_release_streams_output_cfg_list(
385 struct listnode *streams_output_cfg_list)
386{
387 struct listnode *node_i, *node_j;
388 struct streams_output_cfg *so_info;
389 struct stream_format *sf_info;
390
391 ALOGV("%s", __func__);
392 while (!list_empty(streams_output_cfg_list)) {
393 node_i = list_head(streams_output_cfg_list);
394 so_info = node_to_item(node_i, struct streams_output_cfg, list);
395 while (!list_empty(&so_info->format_list)) {
396 node_j = list_head(&so_info->format_list);
397 list_remove(node_j);
398 free(node_to_item(node_j, struct stream_format, list));
399 }
Amit Shekhar6f461b12014-08-01 14:52:58 -0700400 while (!list_empty(&so_info->sample_rate_list)) {
401 node_j = list_head(&so_info->sample_rate_list);
402 list_remove(node_j);
403 free(node_to_item(node_j, struct stream_sample_rate, list));
404 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700405 list_remove(node_i);
406 free(node_to_item(node_i, struct streams_output_cfg, list));
407 }
408}
409
Amit Shekhar6f461b12014-08-01 14:52:58 -0700410static bool set_output_cfg(struct streams_output_cfg *so_info,
411 struct stream_app_type_cfg *app_type_cfg,
412 uint32_t sample_rate, uint32_t bit_width)
413 {
414 struct listnode *node_i;
415 struct stream_sample_rate *ss_info;
416 list_for_each(node_i, &so_info->sample_rate_list) {
417 ss_info = node_to_item(node_i, struct stream_sample_rate, list);
418 if ((sample_rate <= ss_info->sample_rate) &&
419 (bit_width == so_info->app_type_cfg.bit_width)) {
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700420
Amit Shekhar6f461b12014-08-01 14:52:58 -0700421 app_type_cfg->app_type = so_info->app_type_cfg.app_type;
422 app_type_cfg->sample_rate = ss_info->sample_rate;
423 app_type_cfg->bit_width = so_info->app_type_cfg.bit_width;
424 ALOGV("%s app_type_cfg->app_type %d, app_type_cfg->sample_rate %d, app_type_cfg->bit_width %d",
425 __func__, app_type_cfg->app_type, app_type_cfg->sample_rate, app_type_cfg->bit_width);
426 return true;
427 }
428 }
429 /*
430 * Reiterate through the list assuming dafault sample rate.
431 * Handles scenario where input sample rate is higher
432 * than all sample rates in list for the input bit width.
433 */
434 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
Apoorv Raghuvanshif59bb222015-02-18 12:23:23 -0800435
Amit Shekhar6f461b12014-08-01 14:52:58 -0700436 list_for_each(node_i, &so_info->sample_rate_list) {
437 ss_info = node_to_item(node_i, struct stream_sample_rate, list);
438 if ((sample_rate <= ss_info->sample_rate) &&
439 (bit_width == so_info->app_type_cfg.bit_width)) {
440 app_type_cfg->app_type = so_info->app_type_cfg.app_type;
441 app_type_cfg->sample_rate = sample_rate;
442 app_type_cfg->bit_width = so_info->app_type_cfg.bit_width;
Apoorv Raghuvanshif59bb222015-02-18 12:23:23 -0800443 ALOGV("%s Assuming sample rate. app_type_cfg->app_type %d, app_type_cfg->sample_rate %d, app_type_cfg->bit_width %d",
Amit Shekhar6f461b12014-08-01 14:52:58 -0700444 __func__, app_type_cfg->app_type, app_type_cfg->sample_rate, app_type_cfg->bit_width);
445 return true;
446 }
447 }
448 return false;
449}
450
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700451void audio_extn_utils_update_stream_app_type_cfg(void *platform,
452 struct listnode *streams_output_cfg_list,
Amit Shekhar1d896042014-10-03 13:16:09 -0700453 audio_devices_t devices,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700454 audio_output_flags_t flags,
455 audio_format_t format,
Amit Shekhar6f461b12014-08-01 14:52:58 -0700456 uint32_t sample_rate,
457 uint32_t bit_width,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700458 struct stream_app_type_cfg *app_type_cfg)
459{
Amit Shekhar6f461b12014-08-01 14:52:58 -0700460 struct listnode *node_i, *node_j, *node_k;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700461 struct streams_output_cfg *so_info;
462 struct stream_format *sf_info;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700463 struct stream_sample_rate *ss_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700464
Amit Shekhar1d896042014-10-03 13:16:09 -0700465 if ((24 == bit_width) &&
466 (devices & AUDIO_DEVICE_OUT_SPEAKER)) {
Amit Shekhar5a39c912014-10-14 15:39:30 -0700467 int32_t bw = platform_get_snd_device_bit_width(SND_DEVICE_OUT_SPEAKER);
468 if (-ENOSYS != bw)
469 bit_width = (uint32_t)bw;
Amit Shekhar1d896042014-10-03 13:16:09 -0700470 sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
471 ALOGI("%s Allowing 24-bit playback on speaker ONLY at default sampling rate", __func__);
472 }
473
474 ALOGV("%s: flags: %x, format: %x sample_rate %d",
475 __func__, flags, format, sample_rate);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700476 list_for_each(node_i, streams_output_cfg_list) {
477 so_info = node_to_item(node_i, struct streams_output_cfg, list);
478 if (so_info->flags == flags) {
479 list_for_each(node_j, &so_info->format_list) {
480 sf_info = node_to_item(node_j, struct stream_format, list);
481 if (sf_info->format == format) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700482 if (set_output_cfg(so_info, app_type_cfg, sample_rate, bit_width))
483 return;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700484 }
485 }
486 }
487 }
488 list_for_each(node_i, streams_output_cfg_list) {
489 so_info = node_to_item(node_i, struct streams_output_cfg, list);
490 if (so_info->flags == AUDIO_OUTPUT_FLAG_PRIMARY) {
491 ALOGV("Compatible output profile not found.");
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700492 app_type_cfg->app_type = so_info->app_type_cfg.app_type;
493 app_type_cfg->sample_rate = so_info->app_type_cfg.sample_rate;
494 app_type_cfg->bit_width = so_info->app_type_cfg.bit_width;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700495 ALOGV("%s Default to primary output: App type: %d sample_rate %d",
496 __func__, so_info->app_type_cfg.app_type, app_type_cfg->sample_rate);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700497 return;
498 }
499 }
500 ALOGW("%s: App type could not be selected. Falling back to default", __func__);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700501 app_type_cfg->app_type = platform_get_default_app_type(platform);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700502 app_type_cfg->sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700503 app_type_cfg->bit_width = 16;
504}
505
506int audio_extn_utils_send_app_type_cfg(struct audio_usecase *usecase)
507{
508 char mixer_ctl_name[MAX_LENGTH_MIXER_CONTROL_IN_INT];
509 int app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT], len = 0, rc;
Anish Kumarc8599f22014-07-20 09:36:07 -0700510 struct stream_out *out;
511 struct audio_device *adev;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700512 struct mixer_ctl *ctl;
513 int pcm_device_id, acdb_dev_id, snd_device = usecase->out_snd_device;
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +0530514 int32_t sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700515
516 ALOGV("%s", __func__);
517
518 if (usecase->type != PCM_PLAYBACK) {
519 ALOGV("%s: not a playback path, no need to cfg app type", __func__);
520 rc = 0;
521 goto exit_send_app_type_cfg;
522 }
523 if ((usecase->id != USECASE_AUDIO_PLAYBACK_DEEP_BUFFER) &&
524 (usecase->id != USECASE_AUDIO_PLAYBACK_LOW_LATENCY) &&
525 (usecase->id != USECASE_AUDIO_PLAYBACK_MULTI_CH) &&
Alexy Joseph2f89cfa2014-10-06 12:15:01 -0700526 (!is_offload_usecase(usecase->id))) {
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700527 ALOGV("%s: a playback path where app type cfg is not required %d", __func__, usecase->id);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700528 rc = 0;
529 goto exit_send_app_type_cfg;
530 }
Anish Kumarc8599f22014-07-20 09:36:07 -0700531 out = usecase->stream.out;
532 adev = out->dev;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700533
534 snd_device = usecase->out_snd_device;
535
536 pcm_device_id = platform_get_pcm_device_id(out->usecase, PCM_PLAYBACK);
537
538 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
539 "Audio Stream %d App Type Cfg", pcm_device_id);
540
541 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
542 if (!ctl) {
543 ALOGE("%s: Could not get ctl for mixer cmd - %s", __func__,
544 mixer_ctl_name);
545 rc = -EINVAL;
546 goto exit_send_app_type_cfg;
547 }
548 snd_device = (snd_device == SND_DEVICE_OUT_SPEAKER) ?
549 audio_extn_get_spkr_prot_snd_device(snd_device) : snd_device;
550 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
551 if (acdb_dev_id < 0) {
552 ALOGE("%s: Couldn't get the acdb dev id", __func__);
553 rc = -EINVAL;
554 goto exit_send_app_type_cfg;
555 }
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +0530556
557 if ((24 == usecase->stream.out->bit_width) &&
Amit Shekhar1d896042014-10-03 13:16:09 -0700558 (usecase->stream.out->devices & AUDIO_DEVICE_OUT_SPEAKER)) {
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +0530559 sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Apoorv Raghuvanshi21492162015-02-19 18:19:36 -0800560 } else if ((snd_device != SND_DEVICE_OUT_HEADPHONES_44_1 &&
561 usecase->stream.out->sample_rate == OUTPUT_SAMPLING_RATE_44100) ||
562 (usecase->stream.out->sample_rate < OUTPUT_SAMPLING_RATE_44100)) {
563 sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +0530564 } else {
565 sample_rate = out->app_type_cfg.sample_rate;
566 }
567
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700568 app_type_cfg[len++] = out->app_type_cfg.app_type;
569 app_type_cfg[len++] = acdb_dev_id;
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700570 if (((out->format == AUDIO_FORMAT_E_AC3) ||
Mingming Yin497419f2015-07-01 16:57:32 -0700571 (out->format == AUDIO_FORMAT_E_AC3_JOC))
572#ifdef HDMI_PASSTHROUGH_ENABLED
573 && (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH)
574#endif
575 )
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700576 app_type_cfg[len++] = sample_rate * 4;
577 else
578 app_type_cfg[len++] = sample_rate;
Apoorv Raghuvanshi21492162015-02-19 18:19:36 -0800579
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700580 mixer_ctl_set_array(ctl, app_type_cfg, len);
Amit Shekhar278e3362014-09-08 14:08:19 -0700581 ALOGI("%s app_type %d, acdb_dev_id %d, sample_rate %d",
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +0530582 __func__, out->app_type_cfg.app_type, acdb_dev_id, sample_rate);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700583 rc = 0;
584exit_send_app_type_cfg:
585 return rc;
586}
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700587
588void audio_extn_utils_send_audio_calibration(struct audio_device *adev,
589 struct audio_usecase *usecase)
590{
591 int type = usecase->type;
592
593 if (type == PCM_PLAYBACK) {
594 struct stream_out *out = usecase->stream.out;
595 int snd_device = usecase->out_snd_device;
596 snd_device = (snd_device == SND_DEVICE_OUT_SPEAKER) ?
597 audio_extn_get_spkr_prot_snd_device(snd_device) : snd_device;
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +0530598 platform_send_audio_calibration(adev->platform, usecase,
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700599 out->app_type_cfg.app_type,
600 out->app_type_cfg.sample_rate);
601 }
602 if ((type == PCM_HFP_CALL) || (type == PCM_CAPTURE)) {
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +0530603 /* when app type is default. the sample rate is not used to send cal */
604 platform_send_audio_calibration(adev->platform, usecase,
605 platform_get_default_app_type(adev->platform),
606 48000);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700607 }
608}
609
Ben Rombergera04fabc2014-11-14 12:16:03 -0800610// Base64 Encode and Decode
611// Not all features supported. This must be used only with following conditions.
612// Decode Modes: Support with and without padding
613// CRLF not handling. So no CRLF in string to decode.
614// Encode Modes: Supports only padding
615int b64decode(char *inp, int ilen, uint8_t* outp)
616{
617 int i, j, k, ii, num;
618 int rem, pcnt;
619 uint32_t res=0;
620 uint8_t getIndex[MAX_BASEINDEX_LEN];
621 uint8_t tmp, cflag;
622
623 if(inp == NULL || outp == NULL || ilen <= 0) {
624 ALOGE("[%s] received NULL pointer or zero length",__func__);
625 return -1;
626 }
627
628 memset(getIndex, MAX_BASEINDEX_LEN-1, sizeof(getIndex));
629 for(i=0;i<BASE_TABLE_SIZE;i++) {
630 getIndex[(uint8_t)bTable[i]] = (uint8_t)i;
631 }
632 getIndex[(uint8_t)'=']=0;
633
634 j=0;k=0;
635 num = ilen/4;
636 rem = ilen%4;
637 if(rem==0)
638 num = num-1;
639 cflag=0;
640 for(i=0; i<num; i++) {
641 res=0;
642 for(ii=0;ii<4;ii++) {
643 res = res << 6;
644 tmp = getIndex[(uint8_t)inp[j++]];
645 res = res | tmp;
646 cflag = cflag | tmp;
647 }
648 outp[k++] = (res >> 16)&0xFF;
649 outp[k++] = (res >> 8)&0xFF;
650 outp[k++] = res & 0xFF;
651 }
652
653 // Handle last bytes special
654 pcnt=0;
655 if(rem == 0) {
656 //With padding or full data
657 res = 0;
658 for(ii=0;ii<4;ii++) {
659 if(inp[j] == '=')
660 pcnt++;
661 res = res << 6;
662 tmp = getIndex[(uint8_t)inp[j++]];
663 res = res | tmp;
664 cflag = cflag | tmp;
665 }
666 outp[k++] = res >> 16;
667 if(pcnt == 2)
668 goto done;
669 outp[k++] = (res>>8)&0xFF;
670 if(pcnt == 1)
671 goto done;
672 outp[k++] = res&0xFF;
673 } else {
674 //without padding
675 res = 0;
676 for(i=0;i<rem;i++) {
677 res = res << 6;
678 tmp = getIndex[(uint8_t)inp[j++]];
679 res = res | tmp;
680 cflag = cflag | tmp;
681 }
682 for(i=rem;i<4;i++) {
683 res = res << 6;
684 pcnt++;
685 }
686 outp[k++] = res >> 16;
687 if(pcnt == 2)
688 goto done;
689 outp[k++] = (res>>8)&0xFF;
690 if(pcnt == 1)
691 goto done;
692 outp[k++] = res&0xFF;
693 }
694done:
695 if(cflag == 0xFF) {
696 ALOGE("[%s] base64 decode failed. Invalid character found %s",
697 __func__, inp);
698 return 0;
699 }
700 return k;
701}
702
703int b64encode(uint8_t *inp, int ilen, char* outp)
704{
705 int i,j,k, num;
706 int rem=0;
707 uint32_t res=0;
708
709 if(inp == NULL || outp == NULL || ilen<=0) {
710 ALOGE("[%s] received NULL pointer or zero input length",__func__);
711 return -1;
712 }
713
714 num = ilen/3;
715 rem = ilen%3;
716 j=0;k=0;
717 for(i=0; i<num; i++) {
718 //prepare index
719 res = inp[j++]<<16;
720 res = res | inp[j++]<<8;
721 res = res | inp[j++];
722 //get output map from index
723 outp[k++] = (char) bTable[(res>>18)&0x3F];
724 outp[k++] = (char) bTable[(res>>12)&0x3F];
725 outp[k++] = (char) bTable[(res>>6)&0x3F];
726 outp[k++] = (char) bTable[res&0x3F];
727 }
728
729 switch(rem) {
730 case 1:
731 res = inp[j++]<<16;
732 outp[k++] = (char) bTable[res>>18];
733 outp[k++] = (char) bTable[(res>>12)&0x3F];
734 //outp[k++] = '=';
735 //outp[k++] = '=';
736 break;
737 case 2:
738 res = inp[j++]<<16;
739 res = res | inp[j++]<<8;
740 outp[k++] = (char) bTable[res>>18];
741 outp[k++] = (char) bTable[(res>>12)&0x3F];
742 outp[k++] = (char) bTable[(res>>6)&0x3F];
743 //outp[k++] = '=';
744 break;
745 default:
746 break;
747 }
748done:
749 outp[k] = '\0';
750 return k;
751}