blob: a2b644ed8d93287e1771f64504be485b456cd00e [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
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -070071 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070072};
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),
Satya Krishna Pindiproli4cda5352015-08-12 18:21:25 +053084#ifdef AUDIO_EXTN_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),
Satya Krishna Pindiproli70471602015-04-24 19:12:43 +0530100 STRING_TO_ENUM(AUDIO_FORMAT_ALAC),
101 STRING_TO_ENUM(AUDIO_FORMAT_APE),
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700102 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3_JOC),
Alexy Josephcd8eaed2014-12-11 12:46:53 -0800103 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LC),
104 STRING_TO_ENUM(AUDIO_FORMAT_AAC_HE_V1),
105 STRING_TO_ENUM(AUDIO_FORMAT_AAC_HE_V2),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700106#endif
107};
108
Ben Rombergera04fabc2014-11-14 12:16:03 -0800109static char bTable[BASE_TABLE_SIZE] = {
110 'A','B','C','D','E','F','G','H','I','J','K','L',
111 'M','N','O','P','Q','R','S','T','U','V','W','X',
112 'Y','Z','a','b','c','d','e','f','g','h','i','j',
113 'k','l','m','n','o','p','q','r','s','t','u','v',
114 'w','x','y','z','0','1','2','3','4','5','6','7',
115 '8','9','+','/'
116};
117
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700118static uint32_t string_to_enum(const struct string_to_enum *table, size_t size,
119 const char *name)
120{
121 size_t i;
122 for (i = 0; i < size; i++) {
123 if (strcmp(table[i].name, name) == 0) {
124 ALOGV("%s found %s", __func__, table[i].name);
125 return table[i].value;
126 }
127 }
128 return 0;
129}
130
131static audio_output_flags_t parse_flag_names(char *name)
132{
133 uint32_t flag = 0;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800134 char *last_r;
135 char *flag_name = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700136 while (flag_name != NULL) {
137 if (strlen(flag_name) != 0) {
138 flag |= string_to_enum(s_flag_name_to_enum_table,
139 ARRAY_SIZE(s_flag_name_to_enum_table),
140 flag_name);
141 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800142 flag_name = strtok_r(NULL, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700143 }
144
145 ALOGV("parse_flag_names: flag - %d", flag);
146 return (audio_output_flags_t)flag;
147}
148
149static void parse_format_names(char *name, struct streams_output_cfg *so_info)
150{
151 struct stream_format *sf_info = NULL;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800152 char *last_r;
153 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700154
155 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG) == 0)
156 return;
157
158 list_init(&so_info->format_list);
159 while (str != NULL) {
160 audio_format_t format = (audio_format_t)string_to_enum(s_format_name_to_enum_table,
161 ARRAY_SIZE(s_format_name_to_enum_table), str);
162 ALOGV("%s: format - %d", __func__, format);
163 if (format != 0) {
164 sf_info = (struct stream_format *)calloc(1, sizeof(struct stream_format));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700165 if (sf_info == NULL)
166 break; /* return whatever was parsed */
167
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700168 sf_info->format = format;
169 list_add_tail(&so_info->format_list, &sf_info->list);
170 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800171 str = strtok_r(NULL, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700172 }
173}
174
Amit Shekhar6f461b12014-08-01 14:52:58 -0700175static void parse_sample_rate_names(char *name, struct streams_output_cfg *so_info)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700176{
Amit Shekhar6f461b12014-08-01 14:52:58 -0700177 struct stream_sample_rate *ss_info = NULL;
178 uint32_t sample_rate = 48000;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800179 char *last_r;
180 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700181
Amit Shekhar6f461b12014-08-01 14:52:58 -0700182 if (str != NULL && 0 == strcmp(str, DYNAMIC_VALUE_TAG))
183 return;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700184
Amit Shekhar6f461b12014-08-01 14:52:58 -0700185 list_init(&so_info->sample_rate_list);
186 while (str != NULL) {
187 sample_rate = (uint32_t)strtol(str, (char **)NULL, 10);
188 ALOGV("%s: sample_rate - %d", __func__, sample_rate);
189 if (0 != sample_rate) {
190 ss_info = (struct stream_sample_rate *)calloc(1, sizeof(struct stream_sample_rate));
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800191 if (!ss_info) {
192 ALOGE("%s: memory allocation failure", __func__);
193 return;
194 }
Amit Shekhar6f461b12014-08-01 14:52:58 -0700195 ss_info->sample_rate = sample_rate;
196 list_add_tail(&so_info->sample_rate_list, &ss_info->list);
197 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800198 str = strtok_r(NULL, "|", &last_r);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700199 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700200}
201
202static int parse_bit_width_names(char *name)
203{
204 int bit_width = 16;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800205 char *last_r;
206 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700207
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);
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800218 char *last_r;
219 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700220
221 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG))
222 app_type = (int)strtol(str, (char **)NULL, 10);
223
224 ALOGV("%s: app_type - %d", __func__, app_type);
225 return app_type;
226}
227
228static void update_streams_output_cfg_list(cnode *root, void *platform,
229 struct listnode *streams_output_cfg_list)
230{
231 cnode *node = root->first_child;
232 struct streams_output_cfg *so_info;
233
234 ALOGV("%s", __func__);
235 so_info = (struct streams_output_cfg *)calloc(1, sizeof(struct streams_output_cfg));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700236
237 if (!so_info) {
238 ALOGE("failed to allocate mem for so_info list element");
239 return;
240 }
241
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700242 while (node) {
243 if (strcmp(node->name, FLAGS_TAG) == 0) {
244 so_info->flags = parse_flag_names((char *)node->value);
245 } else if (strcmp(node->name, FORMATS_TAG) == 0) {
246 parse_format_names((char *)node->value, so_info);
247 } else if (strcmp(node->name, SAMPLING_RATES_TAG) == 0) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700248 so_info->app_type_cfg.sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
249 parse_sample_rate_names((char *)node->value, so_info);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700250 } else if (strcmp(node->name, BIT_WIDTH_TAG) == 0) {
251 so_info->app_type_cfg.bit_width = parse_bit_width_names((char *)node->value);
252 } else if (strcmp(node->name, APP_TYPE_TAG) == 0) {
253 so_info->app_type_cfg.app_type = parse_app_type_names(platform, (char *)node->value);
254 }
255 node = node->next;
256 }
257 list_add_tail(streams_output_cfg_list, &so_info->list);
258}
259
260static void load_output(cnode *root, void *platform,
261 struct listnode *streams_output_cfg_list)
262{
263 cnode *node = config_find(root, OUTPUTS_TAG);
264 if (node == NULL) {
265 ALOGE("%s: could not load output, node is NULL", __func__);
266 return;
267 }
268
269 node = node->first_child;
270 while (node) {
271 ALOGV("%s: loading output %s", __func__, node->name);
272 update_streams_output_cfg_list(node, platform, streams_output_cfg_list);
273 node = node->next;
274 }
275}
276
277static void send_app_type_cfg(void *platform, struct mixer *mixer,
278 struct listnode *streams_output_cfg_list)
279{
280 int app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT] = {-1};
281 int length = 0, i, num_app_types = 0;
282 struct listnode *node;
283 bool update;
284 struct mixer_ctl *ctl = NULL;
285 const char *mixer_ctl_name = "App Type Config";
286 struct streams_output_cfg *so_info;
287
288 if (!mixer) {
289 ALOGE("%s: mixer is null",__func__);
290 return;
291 }
292 ctl = mixer_get_ctl_by_name(mixer, mixer_ctl_name);
293 if (!ctl) {
294 ALOGE("%s: Could not get ctl for mixer cmd - %s",__func__, mixer_ctl_name);
295 return;
296 }
297 if (streams_output_cfg_list == NULL) {
298 app_type_cfg[length++] = 1;
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700299 app_type_cfg[length++] = platform_get_default_app_type(platform);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700300 app_type_cfg[length++] = 48000;
301 app_type_cfg[length++] = 16;
302 mixer_ctl_set_array(ctl, app_type_cfg, length);
303 return;
304 }
305
306 app_type_cfg[length++] = num_app_types;
307 list_for_each(node, streams_output_cfg_list) {
308 so_info = node_to_item(node, struct streams_output_cfg, list);
309 update = true;
310 for (i=0; i<length; i=i+3) {
311 if (app_type_cfg[i+1] == -1)
312 break;
313 else if (app_type_cfg[i+1] == so_info->app_type_cfg.app_type) {
314 update = false;
315 break;
316 }
317 }
318 if (update && ((length + 3) <= MAX_LENGTH_MIXER_CONTROL_IN_INT)) {
319 num_app_types += 1 ;
320 app_type_cfg[length++] = so_info->app_type_cfg.app_type;
321 app_type_cfg[length++] = so_info->app_type_cfg.sample_rate;
322 app_type_cfg[length++] = so_info->app_type_cfg.bit_width;
323 }
324 }
325 ALOGV("%s: num_app_types: %d", __func__, num_app_types);
326 if (num_app_types) {
327 app_type_cfg[0] = num_app_types;
328 mixer_ctl_set_array(ctl, app_type_cfg, length);
329 }
330}
331
332void audio_extn_utils_update_streams_output_cfg_list(void *platform,
333 struct mixer *mixer,
334 struct listnode *streams_output_cfg_list)
335{
336 cnode *root;
337 char *data;
338
339 ALOGV("%s", __func__);
340 list_init(streams_output_cfg_list);
341 data = (char *)load_file(AUDIO_OUTPUT_POLICY_VENDOR_CONFIG_FILE, NULL);
342 if (data == NULL) {
343 send_app_type_cfg(platform, mixer, NULL);
344 ALOGE("%s: could not load output policy config file", __func__);
345 return;
346 }
347
348 root = config_node("", "");
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700349 if (root == NULL) {
350 ALOGE("cfg_list, NULL config root");
351 return;
352 }
353
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700354 config_load(root, data);
355 load_output(root, platform, streams_output_cfg_list);
356
357 send_app_type_cfg(platform, mixer, streams_output_cfg_list);
358}
359
360void audio_extn_utils_dump_streams_output_cfg_list(
361 struct listnode *streams_output_cfg_list)
362{
363 int i=0;
364 struct listnode *node_i, *node_j;
365 struct streams_output_cfg *so_info;
366 struct stream_format *sf_info;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700367 struct stream_sample_rate *ss_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700368 ALOGV("%s", __func__);
369 list_for_each(node_i, streams_output_cfg_list) {
370 so_info = node_to_item(node_i, struct streams_output_cfg, list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700371 ALOGV("%s: flags-%d, output_sample_rate-%d, output_bit_width-%d, app_type-%d",
372 __func__, so_info->flags, so_info->app_type_cfg.sample_rate,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700373 so_info->app_type_cfg.bit_width, so_info->app_type_cfg.app_type);
374 list_for_each(node_j, &so_info->format_list) {
375 sf_info = node_to_item(node_j, struct stream_format, list);
376 ALOGV("format-%x", sf_info->format);
377 }
Amit Shekhar6f461b12014-08-01 14:52:58 -0700378 list_for_each(node_j, &so_info->sample_rate_list) {
379 ss_info = node_to_item(node_j, struct stream_sample_rate, list);
380 ALOGV("sample rate-%d", ss_info->sample_rate);
381 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700382 }
383}
384
385void audio_extn_utils_release_streams_output_cfg_list(
386 struct listnode *streams_output_cfg_list)
387{
388 struct listnode *node_i, *node_j;
389 struct streams_output_cfg *so_info;
390 struct stream_format *sf_info;
391
392 ALOGV("%s", __func__);
393 while (!list_empty(streams_output_cfg_list)) {
394 node_i = list_head(streams_output_cfg_list);
395 so_info = node_to_item(node_i, struct streams_output_cfg, list);
396 while (!list_empty(&so_info->format_list)) {
397 node_j = list_head(&so_info->format_list);
398 list_remove(node_j);
399 free(node_to_item(node_j, struct stream_format, list));
400 }
Amit Shekhar6f461b12014-08-01 14:52:58 -0700401 while (!list_empty(&so_info->sample_rate_list)) {
402 node_j = list_head(&so_info->sample_rate_list);
403 list_remove(node_j);
404 free(node_to_item(node_j, struct stream_sample_rate, list));
405 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700406 list_remove(node_i);
407 free(node_to_item(node_i, struct streams_output_cfg, list));
408 }
409}
410
Amit Shekhar6f461b12014-08-01 14:52:58 -0700411static bool set_output_cfg(struct streams_output_cfg *so_info,
412 struct stream_app_type_cfg *app_type_cfg,
413 uint32_t sample_rate, uint32_t bit_width)
414 {
415 struct listnode *node_i;
416 struct stream_sample_rate *ss_info;
417 list_for_each(node_i, &so_info->sample_rate_list) {
418 ss_info = node_to_item(node_i, struct stream_sample_rate, list);
419 if ((sample_rate <= ss_info->sample_rate) &&
420 (bit_width == so_info->app_type_cfg.bit_width)) {
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700421
Amit Shekhar6f461b12014-08-01 14:52:58 -0700422 app_type_cfg->app_type = so_info->app_type_cfg.app_type;
423 app_type_cfg->sample_rate = ss_info->sample_rate;
424 app_type_cfg->bit_width = so_info->app_type_cfg.bit_width;
425 ALOGV("%s app_type_cfg->app_type %d, app_type_cfg->sample_rate %d, app_type_cfg->bit_width %d",
426 __func__, app_type_cfg->app_type, app_type_cfg->sample_rate, app_type_cfg->bit_width);
427 return true;
428 }
429 }
430 /*
431 * Reiterate through the list assuming dafault sample rate.
432 * Handles scenario where input sample rate is higher
433 * than all sample rates in list for the input bit width.
434 */
435 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
Preetam Singh Ranawat1871ddb2015-02-18 12:23:23 -0800436
Amit Shekhar6f461b12014-08-01 14:52:58 -0700437 list_for_each(node_i, &so_info->sample_rate_list) {
438 ss_info = node_to_item(node_i, struct stream_sample_rate, list);
439 if ((sample_rate <= ss_info->sample_rate) &&
440 (bit_width == so_info->app_type_cfg.bit_width)) {
441 app_type_cfg->app_type = so_info->app_type_cfg.app_type;
442 app_type_cfg->sample_rate = sample_rate;
443 app_type_cfg->bit_width = so_info->app_type_cfg.bit_width;
Preetam Singh Ranawat1871ddb2015-02-18 12:23:23 -0800444 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 -0700445 __func__, app_type_cfg->app_type, app_type_cfg->sample_rate, app_type_cfg->bit_width);
446 return true;
447 }
448 }
449 return false;
450}
451
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700452void audio_extn_utils_update_stream_app_type_cfg(void *platform,
453 struct listnode *streams_output_cfg_list,
Amit Shekhar1d896042014-10-03 13:16:09 -0700454 audio_devices_t devices,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700455 audio_output_flags_t flags,
456 audio_format_t format,
Amit Shekhar6f461b12014-08-01 14:52:58 -0700457 uint32_t sample_rate,
458 uint32_t bit_width,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700459 struct stream_app_type_cfg *app_type_cfg)
460{
Amit Shekhar6f461b12014-08-01 14:52:58 -0700461 struct listnode *node_i, *node_j, *node_k;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700462 struct streams_output_cfg *so_info;
463 struct stream_format *sf_info;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700464 struct stream_sample_rate *ss_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700465
Amit Shekhar1d896042014-10-03 13:16:09 -0700466 if ((24 == bit_width) &&
467 (devices & AUDIO_DEVICE_OUT_SPEAKER)) {
Amit Shekhar5a39c912014-10-14 15:39:30 -0700468 int32_t bw = platform_get_snd_device_bit_width(SND_DEVICE_OUT_SPEAKER);
469 if (-ENOSYS != bw)
470 bit_width = (uint32_t)bw;
Amit Shekhar1d896042014-10-03 13:16:09 -0700471 sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
472 ALOGI("%s Allowing 24-bit playback on speaker ONLY at default sampling rate", __func__);
473 }
474
475 ALOGV("%s: flags: %x, format: %x sample_rate %d",
476 __func__, flags, format, sample_rate);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700477 list_for_each(node_i, streams_output_cfg_list) {
478 so_info = node_to_item(node_i, struct streams_output_cfg, list);
479 if (so_info->flags == flags) {
480 list_for_each(node_j, &so_info->format_list) {
481 sf_info = node_to_item(node_j, struct stream_format, list);
482 if (sf_info->format == format) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700483 if (set_output_cfg(so_info, app_type_cfg, sample_rate, bit_width))
484 return;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700485 }
486 }
487 }
488 }
489 list_for_each(node_i, streams_output_cfg_list) {
490 so_info = node_to_item(node_i, struct streams_output_cfg, list);
491 if (so_info->flags == AUDIO_OUTPUT_FLAG_PRIMARY) {
492 ALOGV("Compatible output profile not found.");
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700493 app_type_cfg->app_type = so_info->app_type_cfg.app_type;
494 app_type_cfg->sample_rate = so_info->app_type_cfg.sample_rate;
495 app_type_cfg->bit_width = so_info->app_type_cfg.bit_width;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700496 ALOGV("%s Default to primary output: App type: %d sample_rate %d",
497 __func__, so_info->app_type_cfg.app_type, app_type_cfg->sample_rate);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700498 return;
499 }
500 }
501 ALOGW("%s: App type could not be selected. Falling back to default", __func__);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700502 app_type_cfg->app_type = platform_get_default_app_type(platform);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700503 app_type_cfg->sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700504 app_type_cfg->bit_width = 16;
505}
506
507int audio_extn_utils_send_app_type_cfg(struct audio_usecase *usecase)
508{
509 char mixer_ctl_name[MAX_LENGTH_MIXER_CONTROL_IN_INT];
510 int app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT], len = 0, rc;
Anish Kumarc8599f22014-07-20 09:36:07 -0700511 struct stream_out *out;
512 struct audio_device *adev;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700513 struct mixer_ctl *ctl;
514 int pcm_device_id, acdb_dev_id, snd_device = usecase->out_snd_device;
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +0530515 int32_t sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700516
517 ALOGV("%s", __func__);
518
519 if (usecase->type != PCM_PLAYBACK) {
520 ALOGV("%s: not a playback path, no need to cfg app type", __func__);
521 rc = 0;
522 goto exit_send_app_type_cfg;
523 }
524 if ((usecase->id != USECASE_AUDIO_PLAYBACK_DEEP_BUFFER) &&
525 (usecase->id != USECASE_AUDIO_PLAYBACK_LOW_LATENCY) &&
526 (usecase->id != USECASE_AUDIO_PLAYBACK_MULTI_CH) &&
Alexy Joseph2f89cfa2014-10-06 12:15:01 -0700527 (!is_offload_usecase(usecase->id))) {
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700528 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 -0700529 rc = 0;
530 goto exit_send_app_type_cfg;
531 }
Anish Kumarc8599f22014-07-20 09:36:07 -0700532 out = usecase->stream.out;
533 adev = out->dev;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700534
535 snd_device = usecase->out_snd_device;
536
537 pcm_device_id = platform_get_pcm_device_id(out->usecase, PCM_PLAYBACK);
538
539 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
540 "Audio Stream %d App Type Cfg", pcm_device_id);
541
542 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
543 if (!ctl) {
544 ALOGE("%s: Could not get ctl for mixer cmd - %s", __func__,
545 mixer_ctl_name);
546 rc = -EINVAL;
547 goto exit_send_app_type_cfg;
548 }
549 snd_device = (snd_device == SND_DEVICE_OUT_SPEAKER) ?
550 audio_extn_get_spkr_prot_snd_device(snd_device) : snd_device;
551 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
552 if (acdb_dev_id < 0) {
553 ALOGE("%s: Couldn't get the acdb dev id", __func__);
554 rc = -EINVAL;
555 goto exit_send_app_type_cfg;
556 }
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +0530557
558 if ((24 == usecase->stream.out->bit_width) &&
Amit Shekhar1d896042014-10-03 13:16:09 -0700559 (usecase->stream.out->devices & AUDIO_DEVICE_OUT_SPEAKER)) {
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +0530560 sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Preetam Singh Ranawat36f3d372015-06-18 22:59:53 +0530561 } else if ((snd_device != SND_DEVICE_OUT_HEADPHONES_44_1 &&
562 usecase->stream.out->sample_rate == OUTPUT_SAMPLING_RATE_44100) ||
563 (usecase->stream.out->sample_rate < OUTPUT_SAMPLING_RATE_44100)) {
564 sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +0530565 } else {
566 sample_rate = out->app_type_cfg.sample_rate;
567 }
568
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700569 app_type_cfg[len++] = out->app_type_cfg.app_type;
570 app_type_cfg[len++] = acdb_dev_id;
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700571 if (((out->format == AUDIO_FORMAT_E_AC3) ||
572 (out->format == AUDIO_FORMAT_E_AC3_JOC)) &&
573 (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH))
574 app_type_cfg[len++] = sample_rate * 4;
575 else
576 app_type_cfg[len++] = sample_rate;
Preetam Singh Ranawat36f3d372015-06-18 22:59:53 +0530577
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700578 mixer_ctl_set_array(ctl, app_type_cfg, len);
Amit Shekhar278e3362014-09-08 14:08:19 -0700579 ALOGI("%s app_type %d, acdb_dev_id %d, sample_rate %d",
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +0530580 __func__, out->app_type_cfg.app_type, acdb_dev_id, sample_rate);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700581 rc = 0;
582exit_send_app_type_cfg:
583 return rc;
584}
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700585
586void audio_extn_utils_send_audio_calibration(struct audio_device *adev,
587 struct audio_usecase *usecase)
588{
589 int type = usecase->type;
590
591 if (type == PCM_PLAYBACK) {
592 struct stream_out *out = usecase->stream.out;
593 int snd_device = usecase->out_snd_device;
594 snd_device = (snd_device == SND_DEVICE_OUT_SPEAKER) ?
595 audio_extn_get_spkr_prot_snd_device(snd_device) : snd_device;
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +0530596 platform_send_audio_calibration(adev->platform, usecase,
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700597 out->app_type_cfg.app_type,
598 out->app_type_cfg.sample_rate);
599 }
600 if ((type == PCM_HFP_CALL) || (type == PCM_CAPTURE)) {
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +0530601 /* when app type is default. the sample rate is not used to send cal */
602 platform_send_audio_calibration(adev->platform, usecase,
603 platform_get_default_app_type(adev->platform),
604 48000);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700605 }
606}
607
Ben Rombergera04fabc2014-11-14 12:16:03 -0800608// Base64 Encode and Decode
609// Not all features supported. This must be used only with following conditions.
610// Decode Modes: Support with and without padding
611// CRLF not handling. So no CRLF in string to decode.
612// Encode Modes: Supports only padding
613int b64decode(char *inp, int ilen, uint8_t* outp)
614{
615 int i, j, k, ii, num;
616 int rem, pcnt;
617 uint32_t res=0;
618 uint8_t getIndex[MAX_BASEINDEX_LEN];
619 uint8_t tmp, cflag;
620
621 if(inp == NULL || outp == NULL || ilen <= 0) {
622 ALOGE("[%s] received NULL pointer or zero length",__func__);
623 return -1;
624 }
625
626 memset(getIndex, MAX_BASEINDEX_LEN-1, sizeof(getIndex));
627 for(i=0;i<BASE_TABLE_SIZE;i++) {
628 getIndex[(uint8_t)bTable[i]] = (uint8_t)i;
629 }
630 getIndex[(uint8_t)'=']=0;
631
632 j=0;k=0;
633 num = ilen/4;
634 rem = ilen%4;
635 if(rem==0)
636 num = num-1;
637 cflag=0;
638 for(i=0; i<num; i++) {
639 res=0;
640 for(ii=0;ii<4;ii++) {
641 res = res << 6;
642 tmp = getIndex[(uint8_t)inp[j++]];
643 res = res | tmp;
644 cflag = cflag | tmp;
645 }
646 outp[k++] = (res >> 16)&0xFF;
647 outp[k++] = (res >> 8)&0xFF;
648 outp[k++] = res & 0xFF;
649 }
650
651 // Handle last bytes special
652 pcnt=0;
653 if(rem == 0) {
654 //With padding or full data
655 res = 0;
656 for(ii=0;ii<4;ii++) {
657 if(inp[j] == '=')
658 pcnt++;
659 res = res << 6;
660 tmp = getIndex[(uint8_t)inp[j++]];
661 res = res | tmp;
662 cflag = cflag | tmp;
663 }
664 outp[k++] = res >> 16;
665 if(pcnt == 2)
666 goto done;
667 outp[k++] = (res>>8)&0xFF;
668 if(pcnt == 1)
669 goto done;
670 outp[k++] = res&0xFF;
671 } else {
672 //without padding
673 res = 0;
674 for(i=0;i<rem;i++) {
675 res = res << 6;
676 tmp = getIndex[(uint8_t)inp[j++]];
677 res = res | tmp;
678 cflag = cflag | tmp;
679 }
680 for(i=rem;i<4;i++) {
681 res = res << 6;
682 pcnt++;
683 }
684 outp[k++] = res >> 16;
685 if(pcnt == 2)
686 goto done;
687 outp[k++] = (res>>8)&0xFF;
688 if(pcnt == 1)
689 goto done;
690 outp[k++] = res&0xFF;
691 }
692done:
693 if(cflag == 0xFF) {
694 ALOGE("[%s] base64 decode failed. Invalid character found %s",
695 __func__, inp);
696 return 0;
697 }
698 return k;
699}
700
701int b64encode(uint8_t *inp, int ilen, char* outp)
702{
703 int i,j,k, num;
704 int rem=0;
705 uint32_t res=0;
706
707 if(inp == NULL || outp == NULL || ilen<=0) {
708 ALOGE("[%s] received NULL pointer or zero input length",__func__);
709 return -1;
710 }
711
712 num = ilen/3;
713 rem = ilen%3;
714 j=0;k=0;
715 for(i=0; i<num; i++) {
716 //prepare index
717 res = inp[j++]<<16;
718 res = res | inp[j++]<<8;
719 res = res | inp[j++];
720 //get output map from index
721 outp[k++] = (char) bTable[(res>>18)&0x3F];
722 outp[k++] = (char) bTable[(res>>12)&0x3F];
723 outp[k++] = (char) bTable[(res>>6)&0x3F];
724 outp[k++] = (char) bTable[res&0x3F];
725 }
726
727 switch(rem) {
728 case 1:
729 res = inp[j++]<<16;
730 outp[k++] = (char) bTable[res>>18];
731 outp[k++] = (char) bTable[(res>>12)&0x3F];
732 //outp[k++] = '=';
733 //outp[k++] = '=';
734 break;
735 case 2:
736 res = inp[j++]<<16;
737 res = res | inp[j++]<<8;
738 outp[k++] = (char) bTable[res>>18];
739 outp[k++] = (char) bTable[(res>>12)&0x3F];
740 outp[k++] = (char) bTable[(res>>6)&0x3F];
741 //outp[k++] = '=';
742 break;
743 default:
744 break;
745 }
746done:
747 outp[k] = '\0';
748 return k;
749}