blob: dbfac87f99ef908777537b26b019a9b31b9a5615 [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
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -070074 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070075};
76
77const struct string_to_enum s_format_name_to_enum_table[] = {
78 STRING_TO_ENUM(AUDIO_FORMAT_PCM_16_BIT),
79 STRING_TO_ENUM(AUDIO_FORMAT_PCM_8_BIT),
80 STRING_TO_ENUM(AUDIO_FORMAT_MP3),
81 STRING_TO_ENUM(AUDIO_FORMAT_AAC),
82 STRING_TO_ENUM(AUDIO_FORMAT_VORBIS),
Mingming Yinae3530f2014-07-03 16:50:18 -070083 STRING_TO_ENUM(AUDIO_FORMAT_AMR_NB),
84 STRING_TO_ENUM(AUDIO_FORMAT_AMR_WB),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070085 STRING_TO_ENUM(AUDIO_FORMAT_AC3),
Mingming Yinae3530f2014-07-03 16:50:18 -070086 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3),
87#ifdef FORMATS_ENABLED
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070088 STRING_TO_ENUM(AUDIO_FORMAT_DTS),
89 STRING_TO_ENUM(AUDIO_FORMAT_DTS_LBR),
90 STRING_TO_ENUM(AUDIO_FORMAT_WMA),
91 STRING_TO_ENUM(AUDIO_FORMAT_WMA_PRO),
92 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADIF),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070093 STRING_TO_ENUM(AUDIO_FORMAT_AMR_WB_PLUS),
94 STRING_TO_ENUM(AUDIO_FORMAT_EVRC),
95 STRING_TO_ENUM(AUDIO_FORMAT_EVRCB),
96 STRING_TO_ENUM(AUDIO_FORMAT_EVRCWB),
97 STRING_TO_ENUM(AUDIO_FORMAT_QCELP),
98 STRING_TO_ENUM(AUDIO_FORMAT_MP2),
99 STRING_TO_ENUM(AUDIO_FORMAT_EVRCNW),
100 STRING_TO_ENUM(AUDIO_FORMAT_PCM_16_BIT_OFFLOAD),
101 STRING_TO_ENUM(AUDIO_FORMAT_PCM_24_BIT_OFFLOAD),
Amit Shekhar6f461b12014-08-01 14:52:58 -0700102 STRING_TO_ENUM(AUDIO_FORMAT_FLAC),
Satya Krishna Pindiproli70471602015-04-24 19:12:43 +0530103 STRING_TO_ENUM(AUDIO_FORMAT_ALAC),
104 STRING_TO_ENUM(AUDIO_FORMAT_APE),
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700105 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3_JOC),
Alexy Josephcd8eaed2014-12-11 12:46:53 -0800106 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LC),
107 STRING_TO_ENUM(AUDIO_FORMAT_AAC_HE_V1),
108 STRING_TO_ENUM(AUDIO_FORMAT_AAC_HE_V2),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700109#endif
110};
111
Ben Rombergera04fabc2014-11-14 12:16:03 -0800112static char bTable[BASE_TABLE_SIZE] = {
113 'A','B','C','D','E','F','G','H','I','J','K','L',
114 'M','N','O','P','Q','R','S','T','U','V','W','X',
115 'Y','Z','a','b','c','d','e','f','g','h','i','j',
116 'k','l','m','n','o','p','q','r','s','t','u','v',
117 'w','x','y','z','0','1','2','3','4','5','6','7',
118 '8','9','+','/'
119};
120
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700121static uint32_t string_to_enum(const struct string_to_enum *table, size_t size,
122 const char *name)
123{
124 size_t i;
125 for (i = 0; i < size; i++) {
126 if (strcmp(table[i].name, name) == 0) {
127 ALOGV("%s found %s", __func__, table[i].name);
128 return table[i].value;
129 }
130 }
131 return 0;
132}
133
134static audio_output_flags_t parse_flag_names(char *name)
135{
136 uint32_t flag = 0;
137 char *flag_name = strtok(name, "|");
138 while (flag_name != NULL) {
139 if (strlen(flag_name) != 0) {
140 flag |= string_to_enum(s_flag_name_to_enum_table,
141 ARRAY_SIZE(s_flag_name_to_enum_table),
142 flag_name);
143 }
144 flag_name = strtok(NULL, "|");
145 }
146
147 ALOGV("parse_flag_names: flag - %d", flag);
148 return (audio_output_flags_t)flag;
149}
150
151static void parse_format_names(char *name, struct streams_output_cfg *so_info)
152{
153 struct stream_format *sf_info = NULL;
154 char *str = strtok(name, "|");
155
156 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG) == 0)
157 return;
158
159 list_init(&so_info->format_list);
160 while (str != NULL) {
161 audio_format_t format = (audio_format_t)string_to_enum(s_format_name_to_enum_table,
162 ARRAY_SIZE(s_format_name_to_enum_table), str);
163 ALOGV("%s: format - %d", __func__, format);
164 if (format != 0) {
165 sf_info = (struct stream_format *)calloc(1, sizeof(struct stream_format));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700166 if (sf_info == NULL)
167 break; /* return whatever was parsed */
168
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700169 sf_info->format = format;
170 list_add_tail(&so_info->format_list, &sf_info->list);
171 }
172 str = strtok(NULL, "|");
173 }
174}
175
Amit Shekhar6f461b12014-08-01 14:52:58 -0700176static void parse_sample_rate_names(char *name, struct streams_output_cfg *so_info)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700177{
Amit Shekhar6f461b12014-08-01 14:52:58 -0700178 struct stream_sample_rate *ss_info = NULL;
179 uint32_t sample_rate = 48000;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700180 char *str = strtok(name, "|");
181
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));
Chaithanya Krishna Bacharajudb6236a2015-01-30 10:08:33 +0530191 if (ss_info == NULL)
192 break; /* return whatever was parsed */
193
Amit Shekhar6f461b12014-08-01 14:52:58 -0700194 ss_info->sample_rate = sample_rate;
195 list_add_tail(&so_info->sample_rate_list, &ss_info->list);
196 }
197 str = strtok(NULL, "|");
198 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700199}
200
201static int parse_bit_width_names(char *name)
202{
203 int bit_width = 16;
204 char *str = strtok(name, "|");
205
206 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG))
207 bit_width = (int)strtol(str, (char **)NULL, 10);
208
209 ALOGV("%s: bit_width - %d", __func__, bit_width);
210 return bit_width;
211}
212
213static int parse_app_type_names(void *platform, char *name)
214{
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700215 int app_type = platform_get_default_app_type(platform);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700216 char *str = strtok(name, "|");
217
218 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG))
219 app_type = (int)strtol(str, (char **)NULL, 10);
220
221 ALOGV("%s: app_type - %d", __func__, app_type);
222 return app_type;
223}
224
225static void update_streams_output_cfg_list(cnode *root, void *platform,
226 struct listnode *streams_output_cfg_list)
227{
228 cnode *node = root->first_child;
229 struct streams_output_cfg *so_info;
230
231 ALOGV("%s", __func__);
232 so_info = (struct streams_output_cfg *)calloc(1, sizeof(struct streams_output_cfg));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700233
234 if (!so_info) {
235 ALOGE("failed to allocate mem for so_info list element");
236 return;
237 }
238
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700239 while (node) {
240 if (strcmp(node->name, FLAGS_TAG) == 0) {
241 so_info->flags = parse_flag_names((char *)node->value);
242 } else if (strcmp(node->name, FORMATS_TAG) == 0) {
243 parse_format_names((char *)node->value, so_info);
244 } else if (strcmp(node->name, SAMPLING_RATES_TAG) == 0) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700245 so_info->app_type_cfg.sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
246 parse_sample_rate_names((char *)node->value, so_info);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700247 } else if (strcmp(node->name, BIT_WIDTH_TAG) == 0) {
248 so_info->app_type_cfg.bit_width = parse_bit_width_names((char *)node->value);
249 } else if (strcmp(node->name, APP_TYPE_TAG) == 0) {
250 so_info->app_type_cfg.app_type = parse_app_type_names(platform, (char *)node->value);
251 }
252 node = node->next;
253 }
254 list_add_tail(streams_output_cfg_list, &so_info->list);
255}
256
257static void load_output(cnode *root, void *platform,
258 struct listnode *streams_output_cfg_list)
259{
260 cnode *node = config_find(root, OUTPUTS_TAG);
261 if (node == NULL) {
262 ALOGE("%s: could not load output, node is NULL", __func__);
263 return;
264 }
265
266 node = node->first_child;
267 while (node) {
268 ALOGV("%s: loading output %s", __func__, node->name);
269 update_streams_output_cfg_list(node, platform, streams_output_cfg_list);
270 node = node->next;
271 }
272}
273
274static void send_app_type_cfg(void *platform, struct mixer *mixer,
275 struct listnode *streams_output_cfg_list)
276{
277 int app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT] = {-1};
278 int length = 0, i, num_app_types = 0;
279 struct listnode *node;
280 bool update;
281 struct mixer_ctl *ctl = NULL;
282 const char *mixer_ctl_name = "App Type Config";
283 struct streams_output_cfg *so_info;
284
285 if (!mixer) {
286 ALOGE("%s: mixer is null",__func__);
287 return;
288 }
289 ctl = mixer_get_ctl_by_name(mixer, mixer_ctl_name);
290 if (!ctl) {
291 ALOGE("%s: Could not get ctl for mixer cmd - %s",__func__, mixer_ctl_name);
292 return;
293 }
294 if (streams_output_cfg_list == NULL) {
295 app_type_cfg[length++] = 1;
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700296 app_type_cfg[length++] = platform_get_default_app_type(platform);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700297 app_type_cfg[length++] = 48000;
298 app_type_cfg[length++] = 16;
299 mixer_ctl_set_array(ctl, app_type_cfg, length);
300 return;
301 }
302
303 app_type_cfg[length++] = num_app_types;
304 list_for_each(node, streams_output_cfg_list) {
305 so_info = node_to_item(node, struct streams_output_cfg, list);
306 update = true;
307 for (i=0; i<length; i=i+3) {
308 if (app_type_cfg[i+1] == -1)
309 break;
310 else if (app_type_cfg[i+1] == so_info->app_type_cfg.app_type) {
311 update = false;
312 break;
313 }
314 }
315 if (update && ((length + 3) <= MAX_LENGTH_MIXER_CONTROL_IN_INT)) {
316 num_app_types += 1 ;
317 app_type_cfg[length++] = so_info->app_type_cfg.app_type;
318 app_type_cfg[length++] = so_info->app_type_cfg.sample_rate;
319 app_type_cfg[length++] = so_info->app_type_cfg.bit_width;
320 }
321 }
322 ALOGV("%s: num_app_types: %d", __func__, num_app_types);
323 if (num_app_types) {
324 app_type_cfg[0] = num_app_types;
325 mixer_ctl_set_array(ctl, app_type_cfg, length);
326 }
327}
328
329void audio_extn_utils_update_streams_output_cfg_list(void *platform,
330 struct mixer *mixer,
331 struct listnode *streams_output_cfg_list)
332{
333 cnode *root;
334 char *data;
335
336 ALOGV("%s", __func__);
337 list_init(streams_output_cfg_list);
338 data = (char *)load_file(AUDIO_OUTPUT_POLICY_VENDOR_CONFIG_FILE, NULL);
339 if (data == NULL) {
340 send_app_type_cfg(platform, mixer, NULL);
341 ALOGE("%s: could not load output policy config file", __func__);
342 return;
343 }
344
345 root = config_node("", "");
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700346 if (root == NULL) {
347 ALOGE("cfg_list, NULL config root");
348 return;
349 }
350
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700351 config_load(root, data);
352 load_output(root, platform, streams_output_cfg_list);
353
354 send_app_type_cfg(platform, mixer, streams_output_cfg_list);
355}
356
357void audio_extn_utils_dump_streams_output_cfg_list(
358 struct listnode *streams_output_cfg_list)
359{
360 int i=0;
361 struct listnode *node_i, *node_j;
362 struct streams_output_cfg *so_info;
363 struct stream_format *sf_info;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700364 struct stream_sample_rate *ss_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700365 ALOGV("%s", __func__);
366 list_for_each(node_i, streams_output_cfg_list) {
367 so_info = node_to_item(node_i, struct streams_output_cfg, list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700368 ALOGV("%s: flags-%d, output_sample_rate-%d, output_bit_width-%d, app_type-%d",
369 __func__, so_info->flags, so_info->app_type_cfg.sample_rate,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700370 so_info->app_type_cfg.bit_width, so_info->app_type_cfg.app_type);
371 list_for_each(node_j, &so_info->format_list) {
372 sf_info = node_to_item(node_j, struct stream_format, list);
373 ALOGV("format-%x", sf_info->format);
374 }
Amit Shekhar6f461b12014-08-01 14:52:58 -0700375 list_for_each(node_j, &so_info->sample_rate_list) {
376 ss_info = node_to_item(node_j, struct stream_sample_rate, list);
377 ALOGV("sample rate-%d", ss_info->sample_rate);
378 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700379 }
380}
381
382void audio_extn_utils_release_streams_output_cfg_list(
383 struct listnode *streams_output_cfg_list)
384{
385 struct listnode *node_i, *node_j;
386 struct streams_output_cfg *so_info;
387 struct stream_format *sf_info;
388
389 ALOGV("%s", __func__);
390 while (!list_empty(streams_output_cfg_list)) {
391 node_i = list_head(streams_output_cfg_list);
392 so_info = node_to_item(node_i, struct streams_output_cfg, list);
393 while (!list_empty(&so_info->format_list)) {
394 node_j = list_head(&so_info->format_list);
395 list_remove(node_j);
396 free(node_to_item(node_j, struct stream_format, list));
397 }
Amit Shekhar6f461b12014-08-01 14:52:58 -0700398 while (!list_empty(&so_info->sample_rate_list)) {
399 node_j = list_head(&so_info->sample_rate_list);
400 list_remove(node_j);
401 free(node_to_item(node_j, struct stream_sample_rate, list));
402 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700403 list_remove(node_i);
404 free(node_to_item(node_i, struct streams_output_cfg, list));
405 }
406}
407
Amit Shekhar6f461b12014-08-01 14:52:58 -0700408static bool set_output_cfg(struct streams_output_cfg *so_info,
409 struct stream_app_type_cfg *app_type_cfg,
410 uint32_t sample_rate, uint32_t bit_width)
411 {
412 struct listnode *node_i;
413 struct stream_sample_rate *ss_info;
414 list_for_each(node_i, &so_info->sample_rate_list) {
415 ss_info = node_to_item(node_i, struct stream_sample_rate, list);
416 if ((sample_rate <= ss_info->sample_rate) &&
417 (bit_width == so_info->app_type_cfg.bit_width)) {
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700418
Amit Shekhar6f461b12014-08-01 14:52:58 -0700419 app_type_cfg->app_type = so_info->app_type_cfg.app_type;
420 app_type_cfg->sample_rate = ss_info->sample_rate;
421 app_type_cfg->bit_width = so_info->app_type_cfg.bit_width;
422 ALOGV("%s app_type_cfg->app_type %d, app_type_cfg->sample_rate %d, app_type_cfg->bit_width %d",
423 __func__, app_type_cfg->app_type, app_type_cfg->sample_rate, app_type_cfg->bit_width);
424 return true;
425 }
426 }
427 /*
428 * Reiterate through the list assuming dafault sample rate.
429 * Handles scenario where input sample rate is higher
430 * than all sample rates in list for the input bit width.
431 */
432 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
Apoorv Raghuvanshif59bb222015-02-18 12:23:23 -0800433
Amit Shekhar6f461b12014-08-01 14:52:58 -0700434 list_for_each(node_i, &so_info->sample_rate_list) {
435 ss_info = node_to_item(node_i, struct stream_sample_rate, list);
436 if ((sample_rate <= ss_info->sample_rate) &&
437 (bit_width == so_info->app_type_cfg.bit_width)) {
438 app_type_cfg->app_type = so_info->app_type_cfg.app_type;
439 app_type_cfg->sample_rate = sample_rate;
440 app_type_cfg->bit_width = so_info->app_type_cfg.bit_width;
Apoorv Raghuvanshif59bb222015-02-18 12:23:23 -0800441 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 -0700442 __func__, app_type_cfg->app_type, app_type_cfg->sample_rate, app_type_cfg->bit_width);
443 return true;
444 }
445 }
446 return false;
447}
448
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700449void audio_extn_utils_update_stream_app_type_cfg(void *platform,
450 struct listnode *streams_output_cfg_list,
Amit Shekhar1d896042014-10-03 13:16:09 -0700451 audio_devices_t devices,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700452 audio_output_flags_t flags,
453 audio_format_t format,
Amit Shekhar6f461b12014-08-01 14:52:58 -0700454 uint32_t sample_rate,
455 uint32_t bit_width,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700456 struct stream_app_type_cfg *app_type_cfg)
457{
Amit Shekhar6f461b12014-08-01 14:52:58 -0700458 struct listnode *node_i, *node_j, *node_k;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700459 struct streams_output_cfg *so_info;
460 struct stream_format *sf_info;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700461 struct stream_sample_rate *ss_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700462
Amit Shekhar1d896042014-10-03 13:16:09 -0700463 if ((24 == bit_width) &&
464 (devices & AUDIO_DEVICE_OUT_SPEAKER)) {
Amit Shekhar5a39c912014-10-14 15:39:30 -0700465 int32_t bw = platform_get_snd_device_bit_width(SND_DEVICE_OUT_SPEAKER);
466 if (-ENOSYS != bw)
467 bit_width = (uint32_t)bw;
Amit Shekhar1d896042014-10-03 13:16:09 -0700468 sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
469 ALOGI("%s Allowing 24-bit playback on speaker ONLY at default sampling rate", __func__);
470 }
471
472 ALOGV("%s: flags: %x, format: %x sample_rate %d",
473 __func__, flags, format, sample_rate);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700474 list_for_each(node_i, streams_output_cfg_list) {
475 so_info = node_to_item(node_i, struct streams_output_cfg, list);
476 if (so_info->flags == flags) {
477 list_for_each(node_j, &so_info->format_list) {
478 sf_info = node_to_item(node_j, struct stream_format, list);
479 if (sf_info->format == format) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700480 if (set_output_cfg(so_info, app_type_cfg, sample_rate, bit_width))
481 return;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700482 }
483 }
484 }
485 }
486 list_for_each(node_i, streams_output_cfg_list) {
487 so_info = node_to_item(node_i, struct streams_output_cfg, list);
488 if (so_info->flags == AUDIO_OUTPUT_FLAG_PRIMARY) {
489 ALOGV("Compatible output profile not found.");
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700490 app_type_cfg->app_type = so_info->app_type_cfg.app_type;
491 app_type_cfg->sample_rate = so_info->app_type_cfg.sample_rate;
492 app_type_cfg->bit_width = so_info->app_type_cfg.bit_width;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700493 ALOGV("%s Default to primary output: App type: %d sample_rate %d",
494 __func__, so_info->app_type_cfg.app_type, app_type_cfg->sample_rate);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700495 return;
496 }
497 }
498 ALOGW("%s: App type could not be selected. Falling back to default", __func__);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700499 app_type_cfg->app_type = platform_get_default_app_type(platform);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700500 app_type_cfg->sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700501 app_type_cfg->bit_width = 16;
502}
503
504int audio_extn_utils_send_app_type_cfg(struct audio_usecase *usecase)
505{
506 char mixer_ctl_name[MAX_LENGTH_MIXER_CONTROL_IN_INT];
507 int app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT], len = 0, rc;
Anish Kumarc8599f22014-07-20 09:36:07 -0700508 struct stream_out *out;
509 struct audio_device *adev;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700510 struct mixer_ctl *ctl;
511 int pcm_device_id, acdb_dev_id, snd_device = usecase->out_snd_device;
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +0530512 int32_t sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700513
514 ALOGV("%s", __func__);
515
516 if (usecase->type != PCM_PLAYBACK) {
517 ALOGV("%s: not a playback path, no need to cfg app type", __func__);
518 rc = 0;
519 goto exit_send_app_type_cfg;
520 }
521 if ((usecase->id != USECASE_AUDIO_PLAYBACK_DEEP_BUFFER) &&
522 (usecase->id != USECASE_AUDIO_PLAYBACK_LOW_LATENCY) &&
523 (usecase->id != USECASE_AUDIO_PLAYBACK_MULTI_CH) &&
Alexy Joseph2f89cfa2014-10-06 12:15:01 -0700524 (!is_offload_usecase(usecase->id))) {
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700525 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 -0700526 rc = 0;
527 goto exit_send_app_type_cfg;
528 }
Anish Kumarc8599f22014-07-20 09:36:07 -0700529 out = usecase->stream.out;
530 adev = out->dev;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700531
532 snd_device = usecase->out_snd_device;
533
534 pcm_device_id = platform_get_pcm_device_id(out->usecase, PCM_PLAYBACK);
535
536 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
537 "Audio Stream %d App Type Cfg", pcm_device_id);
538
539 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
540 if (!ctl) {
541 ALOGE("%s: Could not get ctl for mixer cmd - %s", __func__,
542 mixer_ctl_name);
543 rc = -EINVAL;
544 goto exit_send_app_type_cfg;
545 }
546 snd_device = (snd_device == SND_DEVICE_OUT_SPEAKER) ?
547 audio_extn_get_spkr_prot_snd_device(snd_device) : snd_device;
548 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
549 if (acdb_dev_id < 0) {
550 ALOGE("%s: Couldn't get the acdb dev id", __func__);
551 rc = -EINVAL;
552 goto exit_send_app_type_cfg;
553 }
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +0530554
555 if ((24 == usecase->stream.out->bit_width) &&
Amit Shekhar1d896042014-10-03 13:16:09 -0700556 (usecase->stream.out->devices & AUDIO_DEVICE_OUT_SPEAKER)) {
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +0530557 sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
558 } else {
559 sample_rate = out->app_type_cfg.sample_rate;
560 }
561
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700562 app_type_cfg[len++] = out->app_type_cfg.app_type;
563 app_type_cfg[len++] = acdb_dev_id;
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700564 if (((out->format == AUDIO_FORMAT_E_AC3) ||
565 (out->format == AUDIO_FORMAT_E_AC3_JOC)) &&
566 (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH))
567 app_type_cfg[len++] = sample_rate * 4;
568 else
569 app_type_cfg[len++] = sample_rate;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700570 mixer_ctl_set_array(ctl, app_type_cfg, len);
Amit Shekhar278e3362014-09-08 14:08:19 -0700571 ALOGI("%s app_type %d, acdb_dev_id %d, sample_rate %d",
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +0530572 __func__, out->app_type_cfg.app_type, acdb_dev_id, sample_rate);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700573 rc = 0;
574exit_send_app_type_cfg:
575 return rc;
576}
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700577
578void audio_extn_utils_send_audio_calibration(struct audio_device *adev,
579 struct audio_usecase *usecase)
580{
581 int type = usecase->type;
582
583 if (type == PCM_PLAYBACK) {
584 struct stream_out *out = usecase->stream.out;
585 int snd_device = usecase->out_snd_device;
586 snd_device = (snd_device == SND_DEVICE_OUT_SPEAKER) ?
587 audio_extn_get_spkr_prot_snd_device(snd_device) : snd_device;
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +0530588 platform_send_audio_calibration(adev->platform, usecase,
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700589 out->app_type_cfg.app_type,
590 out->app_type_cfg.sample_rate);
591 }
592 if ((type == PCM_HFP_CALL) || (type == PCM_CAPTURE)) {
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +0530593 /* when app type is default. the sample rate is not used to send cal */
594 platform_send_audio_calibration(adev->platform, usecase,
595 platform_get_default_app_type(adev->platform),
596 48000);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700597 }
598}
599
Ben Rombergera04fabc2014-11-14 12:16:03 -0800600// Base64 Encode and Decode
601// Not all features supported. This must be used only with following conditions.
602// Decode Modes: Support with and without padding
603// CRLF not handling. So no CRLF in string to decode.
604// Encode Modes: Supports only padding
605int b64decode(char *inp, int ilen, uint8_t* outp)
606{
607 int i, j, k, ii, num;
608 int rem, pcnt;
609 uint32_t res=0;
610 uint8_t getIndex[MAX_BASEINDEX_LEN];
611 uint8_t tmp, cflag;
612
613 if(inp == NULL || outp == NULL || ilen <= 0) {
614 ALOGE("[%s] received NULL pointer or zero length",__func__);
615 return -1;
616 }
617
618 memset(getIndex, MAX_BASEINDEX_LEN-1, sizeof(getIndex));
619 for(i=0;i<BASE_TABLE_SIZE;i++) {
620 getIndex[(uint8_t)bTable[i]] = (uint8_t)i;
621 }
622 getIndex[(uint8_t)'=']=0;
623
624 j=0;k=0;
625 num = ilen/4;
626 rem = ilen%4;
627 if(rem==0)
628 num = num-1;
629 cflag=0;
630 for(i=0; i<num; i++) {
631 res=0;
632 for(ii=0;ii<4;ii++) {
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)&0xFF;
639 outp[k++] = (res >> 8)&0xFF;
640 outp[k++] = res & 0xFF;
641 }
642
643 // Handle last bytes special
644 pcnt=0;
645 if(rem == 0) {
646 //With padding or full data
647 res = 0;
648 for(ii=0;ii<4;ii++) {
649 if(inp[j] == '=')
650 pcnt++;
651 res = res << 6;
652 tmp = getIndex[(uint8_t)inp[j++]];
653 res = res | tmp;
654 cflag = cflag | tmp;
655 }
656 outp[k++] = res >> 16;
657 if(pcnt == 2)
658 goto done;
659 outp[k++] = (res>>8)&0xFF;
660 if(pcnt == 1)
661 goto done;
662 outp[k++] = res&0xFF;
663 } else {
664 //without padding
665 res = 0;
666 for(i=0;i<rem;i++) {
667 res = res << 6;
668 tmp = getIndex[(uint8_t)inp[j++]];
669 res = res | tmp;
670 cflag = cflag | tmp;
671 }
672 for(i=rem;i<4;i++) {
673 res = res << 6;
674 pcnt++;
675 }
676 outp[k++] = res >> 16;
677 if(pcnt == 2)
678 goto done;
679 outp[k++] = (res>>8)&0xFF;
680 if(pcnt == 1)
681 goto done;
682 outp[k++] = res&0xFF;
683 }
684done:
685 if(cflag == 0xFF) {
686 ALOGE("[%s] base64 decode failed. Invalid character found %s",
687 __func__, inp);
688 return 0;
689 }
690 return k;
691}
692
693int b64encode(uint8_t *inp, int ilen, char* outp)
694{
695 int i,j,k, num;
696 int rem=0;
697 uint32_t res=0;
698
699 if(inp == NULL || outp == NULL || ilen<=0) {
700 ALOGE("[%s] received NULL pointer or zero input length",__func__);
701 return -1;
702 }
703
704 num = ilen/3;
705 rem = ilen%3;
706 j=0;k=0;
707 for(i=0; i<num; i++) {
708 //prepare index
709 res = inp[j++]<<16;
710 res = res | inp[j++]<<8;
711 res = res | inp[j++];
712 //get output map from index
713 outp[k++] = (char) bTable[(res>>18)&0x3F];
714 outp[k++] = (char) bTable[(res>>12)&0x3F];
715 outp[k++] = (char) bTable[(res>>6)&0x3F];
716 outp[k++] = (char) bTable[res&0x3F];
717 }
718
719 switch(rem) {
720 case 1:
721 res = inp[j++]<<16;
722 outp[k++] = (char) bTable[res>>18];
723 outp[k++] = (char) bTable[(res>>12)&0x3F];
724 //outp[k++] = '=';
725 //outp[k++] = '=';
726 break;
727 case 2:
728 res = inp[j++]<<16;
729 res = res | inp[j++]<<8;
730 outp[k++] = (char) bTable[res>>18];
731 outp[k++] = (char) bTable[(res>>12)&0x3F];
732 outp[k++] = (char) bTable[(res>>6)&0x3F];
733 //outp[k++] = '=';
734 break;
735 default:
736 break;
737 }
738done:
739 outp[k] = '\0';
740 return k;
741}