blob: 96722cc17d309967d56b7efe60f43c6abbfa8b00 [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),
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700103 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3_JOC),
Alexy Josephcd8eaed2014-12-11 12:46:53 -0800104 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LC),
105 STRING_TO_ENUM(AUDIO_FORMAT_AAC_HE_V1),
106 STRING_TO_ENUM(AUDIO_FORMAT_AAC_HE_V2),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700107#endif
108};
109
Ben Rombergera04fabc2014-11-14 12:16:03 -0800110static char bTable[BASE_TABLE_SIZE] = {
111 'A','B','C','D','E','F','G','H','I','J','K','L',
112 'M','N','O','P','Q','R','S','T','U','V','W','X',
113 'Y','Z','a','b','c','d','e','f','g','h','i','j',
114 'k','l','m','n','o','p','q','r','s','t','u','v',
115 'w','x','y','z','0','1','2','3','4','5','6','7',
116 '8','9','+','/'
117};
118
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700119static uint32_t string_to_enum(const struct string_to_enum *table, size_t size,
120 const char *name)
121{
122 size_t i;
123 for (i = 0; i < size; i++) {
124 if (strcmp(table[i].name, name) == 0) {
125 ALOGV("%s found %s", __func__, table[i].name);
126 return table[i].value;
127 }
128 }
129 return 0;
130}
131
132static audio_output_flags_t parse_flag_names(char *name)
133{
134 uint32_t flag = 0;
135 char *flag_name = strtok(name, "|");
136 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 }
142 flag_name = strtok(NULL, "|");
143 }
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;
152 char *str = strtok(name, "|");
153
154 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG) == 0)
155 return;
156
157 list_init(&so_info->format_list);
158 while (str != NULL) {
159 audio_format_t format = (audio_format_t)string_to_enum(s_format_name_to_enum_table,
160 ARRAY_SIZE(s_format_name_to_enum_table), str);
161 ALOGV("%s: format - %d", __func__, format);
162 if (format != 0) {
163 sf_info = (struct stream_format *)calloc(1, sizeof(struct stream_format));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700164 if (sf_info == NULL)
165 break; /* return whatever was parsed */
166
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700167 sf_info->format = format;
168 list_add_tail(&so_info->format_list, &sf_info->list);
169 }
170 str = strtok(NULL, "|");
171 }
172}
173
Amit Shekhar6f461b12014-08-01 14:52:58 -0700174static void parse_sample_rate_names(char *name, struct streams_output_cfg *so_info)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700175{
Amit Shekhar6f461b12014-08-01 14:52:58 -0700176 struct stream_sample_rate *ss_info = NULL;
177 uint32_t sample_rate = 48000;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700178 char *str = strtok(name, "|");
179
Amit Shekhar6f461b12014-08-01 14:52:58 -0700180 if (str != NULL && 0 == strcmp(str, DYNAMIC_VALUE_TAG))
181 return;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700182
Amit Shekhar6f461b12014-08-01 14:52:58 -0700183 list_init(&so_info->sample_rate_list);
184 while (str != NULL) {
185 sample_rate = (uint32_t)strtol(str, (char **)NULL, 10);
186 ALOGV("%s: sample_rate - %d", __func__, sample_rate);
187 if (0 != sample_rate) {
188 ss_info = (struct stream_sample_rate *)calloc(1, sizeof(struct stream_sample_rate));
189 ss_info->sample_rate = sample_rate;
190 list_add_tail(&so_info->sample_rate_list, &ss_info->list);
191 }
192 str = strtok(NULL, "|");
193 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700194}
195
196static int parse_bit_width_names(char *name)
197{
198 int bit_width = 16;
199 char *str = strtok(name, "|");
200
201 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG))
202 bit_width = (int)strtol(str, (char **)NULL, 10);
203
204 ALOGV("%s: bit_width - %d", __func__, bit_width);
205 return bit_width;
206}
207
208static int parse_app_type_names(void *platform, char *name)
209{
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700210 int app_type = platform_get_default_app_type(platform);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700211 char *str = strtok(name, "|");
212
213 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG))
214 app_type = (int)strtol(str, (char **)NULL, 10);
215
216 ALOGV("%s: app_type - %d", __func__, app_type);
217 return app_type;
218}
219
220static void update_streams_output_cfg_list(cnode *root, void *platform,
221 struct listnode *streams_output_cfg_list)
222{
223 cnode *node = root->first_child;
224 struct streams_output_cfg *so_info;
225
226 ALOGV("%s", __func__);
227 so_info = (struct streams_output_cfg *)calloc(1, sizeof(struct streams_output_cfg));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700228
229 if (!so_info) {
230 ALOGE("failed to allocate mem for so_info list element");
231 return;
232 }
233
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700234 while (node) {
235 if (strcmp(node->name, FLAGS_TAG) == 0) {
236 so_info->flags = parse_flag_names((char *)node->value);
237 } else if (strcmp(node->name, FORMATS_TAG) == 0) {
238 parse_format_names((char *)node->value, so_info);
239 } else if (strcmp(node->name, SAMPLING_RATES_TAG) == 0) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700240 so_info->app_type_cfg.sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
241 parse_sample_rate_names((char *)node->value, so_info);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700242 } else if (strcmp(node->name, BIT_WIDTH_TAG) == 0) {
243 so_info->app_type_cfg.bit_width = parse_bit_width_names((char *)node->value);
244 } else if (strcmp(node->name, APP_TYPE_TAG) == 0) {
245 so_info->app_type_cfg.app_type = parse_app_type_names(platform, (char *)node->value);
246 }
247 node = node->next;
248 }
249 list_add_tail(streams_output_cfg_list, &so_info->list);
250}
251
252static void load_output(cnode *root, void *platform,
253 struct listnode *streams_output_cfg_list)
254{
255 cnode *node = config_find(root, OUTPUTS_TAG);
256 if (node == NULL) {
257 ALOGE("%s: could not load output, node is NULL", __func__);
258 return;
259 }
260
261 node = node->first_child;
262 while (node) {
263 ALOGV("%s: loading output %s", __func__, node->name);
264 update_streams_output_cfg_list(node, platform, streams_output_cfg_list);
265 node = node->next;
266 }
267}
268
269static void send_app_type_cfg(void *platform, struct mixer *mixer,
270 struct listnode *streams_output_cfg_list)
271{
272 int app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT] = {-1};
273 int length = 0, i, num_app_types = 0;
274 struct listnode *node;
275 bool update;
276 struct mixer_ctl *ctl = NULL;
277 const char *mixer_ctl_name = "App Type Config";
278 struct streams_output_cfg *so_info;
279
280 if (!mixer) {
281 ALOGE("%s: mixer is null",__func__);
282 return;
283 }
284 ctl = mixer_get_ctl_by_name(mixer, mixer_ctl_name);
285 if (!ctl) {
286 ALOGE("%s: Could not get ctl for mixer cmd - %s",__func__, mixer_ctl_name);
287 return;
288 }
289 if (streams_output_cfg_list == NULL) {
290 app_type_cfg[length++] = 1;
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700291 app_type_cfg[length++] = platform_get_default_app_type(platform);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700292 app_type_cfg[length++] = 48000;
293 app_type_cfg[length++] = 16;
294 mixer_ctl_set_array(ctl, app_type_cfg, length);
295 return;
296 }
297
298 app_type_cfg[length++] = num_app_types;
299 list_for_each(node, streams_output_cfg_list) {
300 so_info = node_to_item(node, struct streams_output_cfg, list);
301 update = true;
302 for (i=0; i<length; i=i+3) {
303 if (app_type_cfg[i+1] == -1)
304 break;
305 else if (app_type_cfg[i+1] == so_info->app_type_cfg.app_type) {
306 update = false;
307 break;
308 }
309 }
310 if (update && ((length + 3) <= MAX_LENGTH_MIXER_CONTROL_IN_INT)) {
311 num_app_types += 1 ;
312 app_type_cfg[length++] = so_info->app_type_cfg.app_type;
313 app_type_cfg[length++] = so_info->app_type_cfg.sample_rate;
314 app_type_cfg[length++] = so_info->app_type_cfg.bit_width;
315 }
316 }
317 ALOGV("%s: num_app_types: %d", __func__, num_app_types);
318 if (num_app_types) {
319 app_type_cfg[0] = num_app_types;
320 mixer_ctl_set_array(ctl, app_type_cfg, length);
321 }
322}
323
324void audio_extn_utils_update_streams_output_cfg_list(void *platform,
325 struct mixer *mixer,
326 struct listnode *streams_output_cfg_list)
327{
328 cnode *root;
329 char *data;
330
331 ALOGV("%s", __func__);
332 list_init(streams_output_cfg_list);
333 data = (char *)load_file(AUDIO_OUTPUT_POLICY_VENDOR_CONFIG_FILE, NULL);
334 if (data == NULL) {
335 send_app_type_cfg(platform, mixer, NULL);
336 ALOGE("%s: could not load output policy config file", __func__);
337 return;
338 }
339
340 root = config_node("", "");
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700341 if (root == NULL) {
342 ALOGE("cfg_list, NULL config root");
343 return;
344 }
345
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700346 config_load(root, data);
347 load_output(root, platform, streams_output_cfg_list);
348
349 send_app_type_cfg(platform, mixer, streams_output_cfg_list);
350}
351
352void audio_extn_utils_dump_streams_output_cfg_list(
353 struct listnode *streams_output_cfg_list)
354{
355 int i=0;
356 struct listnode *node_i, *node_j;
357 struct streams_output_cfg *so_info;
358 struct stream_format *sf_info;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700359 struct stream_sample_rate *ss_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700360 ALOGV("%s", __func__);
361 list_for_each(node_i, streams_output_cfg_list) {
362 so_info = node_to_item(node_i, struct streams_output_cfg, list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700363 ALOGV("%s: flags-%d, output_sample_rate-%d, output_bit_width-%d, app_type-%d",
364 __func__, so_info->flags, so_info->app_type_cfg.sample_rate,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700365 so_info->app_type_cfg.bit_width, so_info->app_type_cfg.app_type);
366 list_for_each(node_j, &so_info->format_list) {
367 sf_info = node_to_item(node_j, struct stream_format, list);
368 ALOGV("format-%x", sf_info->format);
369 }
Amit Shekhar6f461b12014-08-01 14:52:58 -0700370 list_for_each(node_j, &so_info->sample_rate_list) {
371 ss_info = node_to_item(node_j, struct stream_sample_rate, list);
372 ALOGV("sample rate-%d", ss_info->sample_rate);
373 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700374 }
375}
376
377void audio_extn_utils_release_streams_output_cfg_list(
378 struct listnode *streams_output_cfg_list)
379{
380 struct listnode *node_i, *node_j;
381 struct streams_output_cfg *so_info;
382 struct stream_format *sf_info;
383
384 ALOGV("%s", __func__);
385 while (!list_empty(streams_output_cfg_list)) {
386 node_i = list_head(streams_output_cfg_list);
387 so_info = node_to_item(node_i, struct streams_output_cfg, list);
388 while (!list_empty(&so_info->format_list)) {
389 node_j = list_head(&so_info->format_list);
390 list_remove(node_j);
391 free(node_to_item(node_j, struct stream_format, list));
392 }
Amit Shekhar6f461b12014-08-01 14:52:58 -0700393 while (!list_empty(&so_info->sample_rate_list)) {
394 node_j = list_head(&so_info->sample_rate_list);
395 list_remove(node_j);
396 free(node_to_item(node_j, struct stream_sample_rate, list));
397 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700398 list_remove(node_i);
399 free(node_to_item(node_i, struct streams_output_cfg, list));
400 }
401}
402
Amit Shekhar6f461b12014-08-01 14:52:58 -0700403static bool set_output_cfg(struct streams_output_cfg *so_info,
404 struct stream_app_type_cfg *app_type_cfg,
405 uint32_t sample_rate, uint32_t bit_width)
406 {
407 struct listnode *node_i;
408 struct stream_sample_rate *ss_info;
409 list_for_each(node_i, &so_info->sample_rate_list) {
410 ss_info = node_to_item(node_i, struct stream_sample_rate, list);
411 if ((sample_rate <= ss_info->sample_rate) &&
412 (bit_width == so_info->app_type_cfg.bit_width)) {
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700413
Amit Shekhar6f461b12014-08-01 14:52:58 -0700414 app_type_cfg->app_type = so_info->app_type_cfg.app_type;
415 app_type_cfg->sample_rate = ss_info->sample_rate;
416 app_type_cfg->bit_width = so_info->app_type_cfg.bit_width;
417 ALOGV("%s app_type_cfg->app_type %d, app_type_cfg->sample_rate %d, app_type_cfg->bit_width %d",
418 __func__, app_type_cfg->app_type, app_type_cfg->sample_rate, app_type_cfg->bit_width);
419 return true;
420 }
421 }
422 /*
423 * Reiterate through the list assuming dafault sample rate.
424 * Handles scenario where input sample rate is higher
425 * than all sample rates in list for the input bit width.
426 */
427 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
428 list_for_each(node_i, &so_info->sample_rate_list) {
429 ss_info = node_to_item(node_i, struct stream_sample_rate, list);
430 if ((sample_rate <= ss_info->sample_rate) &&
431 (bit_width == so_info->app_type_cfg.bit_width)) {
432 app_type_cfg->app_type = so_info->app_type_cfg.app_type;
433 app_type_cfg->sample_rate = sample_rate;
434 app_type_cfg->bit_width = so_info->app_type_cfg.bit_width;
435 ALOGV("%s Assuming default sample rate. app_type_cfg->app_type %d, app_type_cfg->sample_rate %d, app_type_cfg->bit_width %d",
436 __func__, app_type_cfg->app_type, app_type_cfg->sample_rate, app_type_cfg->bit_width);
437 return true;
438 }
439 }
440 return false;
441}
442
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700443void audio_extn_utils_update_stream_app_type_cfg(void *platform,
444 struct listnode *streams_output_cfg_list,
Amit Shekhar1d896042014-10-03 13:16:09 -0700445 audio_devices_t devices,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700446 audio_output_flags_t flags,
447 audio_format_t format,
Amit Shekhar6f461b12014-08-01 14:52:58 -0700448 uint32_t sample_rate,
449 uint32_t bit_width,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700450 struct stream_app_type_cfg *app_type_cfg)
451{
Amit Shekhar6f461b12014-08-01 14:52:58 -0700452 struct listnode *node_i, *node_j, *node_k;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700453 struct streams_output_cfg *so_info;
454 struct stream_format *sf_info;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700455 struct stream_sample_rate *ss_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700456
Amit Shekhar1d896042014-10-03 13:16:09 -0700457 if ((24 == bit_width) &&
458 (devices & AUDIO_DEVICE_OUT_SPEAKER)) {
Amit Shekhar5a39c912014-10-14 15:39:30 -0700459 int32_t bw = platform_get_snd_device_bit_width(SND_DEVICE_OUT_SPEAKER);
460 if (-ENOSYS != bw)
461 bit_width = (uint32_t)bw;
Amit Shekhar1d896042014-10-03 13:16:09 -0700462 sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
463 ALOGI("%s Allowing 24-bit playback on speaker ONLY at default sampling rate", __func__);
464 }
465
466 ALOGV("%s: flags: %x, format: %x sample_rate %d",
467 __func__, flags, format, sample_rate);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700468 list_for_each(node_i, streams_output_cfg_list) {
469 so_info = node_to_item(node_i, struct streams_output_cfg, list);
470 if (so_info->flags == flags) {
471 list_for_each(node_j, &so_info->format_list) {
472 sf_info = node_to_item(node_j, struct stream_format, list);
473 if (sf_info->format == format) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700474 if (set_output_cfg(so_info, app_type_cfg, sample_rate, bit_width))
475 return;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700476 }
477 }
478 }
479 }
480 list_for_each(node_i, streams_output_cfg_list) {
481 so_info = node_to_item(node_i, struct streams_output_cfg, list);
482 if (so_info->flags == AUDIO_OUTPUT_FLAG_PRIMARY) {
483 ALOGV("Compatible output profile not found.");
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700484 app_type_cfg->app_type = so_info->app_type_cfg.app_type;
485 app_type_cfg->sample_rate = so_info->app_type_cfg.sample_rate;
486 app_type_cfg->bit_width = so_info->app_type_cfg.bit_width;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700487 ALOGV("%s Default to primary output: App type: %d sample_rate %d",
488 __func__, so_info->app_type_cfg.app_type, app_type_cfg->sample_rate);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700489 return;
490 }
491 }
492 ALOGW("%s: App type could not be selected. Falling back to default", __func__);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700493 app_type_cfg->app_type = platform_get_default_app_type(platform);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700494 app_type_cfg->sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700495 app_type_cfg->bit_width = 16;
496}
497
498int audio_extn_utils_send_app_type_cfg(struct audio_usecase *usecase)
499{
500 char mixer_ctl_name[MAX_LENGTH_MIXER_CONTROL_IN_INT];
501 int app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT], len = 0, rc;
Anish Kumarc8599f22014-07-20 09:36:07 -0700502 struct stream_out *out;
503 struct audio_device *adev;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700504 struct mixer_ctl *ctl;
505 int pcm_device_id, acdb_dev_id, snd_device = usecase->out_snd_device;
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +0530506 int32_t sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700507
508 ALOGV("%s", __func__);
509
510 if (usecase->type != PCM_PLAYBACK) {
511 ALOGV("%s: not a playback path, no need to cfg app type", __func__);
512 rc = 0;
513 goto exit_send_app_type_cfg;
514 }
515 if ((usecase->id != USECASE_AUDIO_PLAYBACK_DEEP_BUFFER) &&
516 (usecase->id != USECASE_AUDIO_PLAYBACK_LOW_LATENCY) &&
517 (usecase->id != USECASE_AUDIO_PLAYBACK_MULTI_CH) &&
Alexy Joseph2f89cfa2014-10-06 12:15:01 -0700518 (!is_offload_usecase(usecase->id))) {
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700519 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 -0700520 rc = 0;
521 goto exit_send_app_type_cfg;
522 }
Anish Kumarc8599f22014-07-20 09:36:07 -0700523 out = usecase->stream.out;
524 adev = out->dev;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700525
526 snd_device = usecase->out_snd_device;
527
528 pcm_device_id = platform_get_pcm_device_id(out->usecase, PCM_PLAYBACK);
529
530 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
531 "Audio Stream %d App Type Cfg", pcm_device_id);
532
533 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
534 if (!ctl) {
535 ALOGE("%s: Could not get ctl for mixer cmd - %s", __func__,
536 mixer_ctl_name);
537 rc = -EINVAL;
538 goto exit_send_app_type_cfg;
539 }
540 snd_device = (snd_device == SND_DEVICE_OUT_SPEAKER) ?
541 audio_extn_get_spkr_prot_snd_device(snd_device) : snd_device;
542 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
543 if (acdb_dev_id < 0) {
544 ALOGE("%s: Couldn't get the acdb dev id", __func__);
545 rc = -EINVAL;
546 goto exit_send_app_type_cfg;
547 }
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +0530548
549 if ((24 == usecase->stream.out->bit_width) &&
Amit Shekhar1d896042014-10-03 13:16:09 -0700550 (usecase->stream.out->devices & AUDIO_DEVICE_OUT_SPEAKER)) {
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +0530551 sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
552 } else {
553 sample_rate = out->app_type_cfg.sample_rate;
554 }
555
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700556 app_type_cfg[len++] = out->app_type_cfg.app_type;
557 app_type_cfg[len++] = acdb_dev_id;
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700558 if (((out->format == AUDIO_FORMAT_E_AC3) ||
559 (out->format == AUDIO_FORMAT_E_AC3_JOC)) &&
560 (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH))
561 app_type_cfg[len++] = sample_rate * 4;
562 else
563 app_type_cfg[len++] = sample_rate;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700564 mixer_ctl_set_array(ctl, app_type_cfg, len);
Amit Shekhar278e3362014-09-08 14:08:19 -0700565 ALOGI("%s app_type %d, acdb_dev_id %d, sample_rate %d",
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +0530566 __func__, out->app_type_cfg.app_type, acdb_dev_id, sample_rate);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700567 rc = 0;
568exit_send_app_type_cfg:
569 return rc;
570}
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700571
572void audio_extn_utils_send_audio_calibration(struct audio_device *adev,
573 struct audio_usecase *usecase)
574{
575 int type = usecase->type;
576
577 if (type == PCM_PLAYBACK) {
578 struct stream_out *out = usecase->stream.out;
579 int snd_device = usecase->out_snd_device;
580 snd_device = (snd_device == SND_DEVICE_OUT_SPEAKER) ?
581 audio_extn_get_spkr_prot_snd_device(snd_device) : snd_device;
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +0530582 platform_send_audio_calibration(adev->platform, usecase,
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700583 out->app_type_cfg.app_type,
584 out->app_type_cfg.sample_rate);
585 }
586 if ((type == PCM_HFP_CALL) || (type == PCM_CAPTURE)) {
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +0530587 /* when app type is default. the sample rate is not used to send cal */
588 platform_send_audio_calibration(adev->platform, usecase,
589 platform_get_default_app_type(adev->platform),
590 48000);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700591 }
592}
593
Ben Rombergera04fabc2014-11-14 12:16:03 -0800594// Base64 Encode and Decode
595// Not all features supported. This must be used only with following conditions.
596// Decode Modes: Support with and without padding
597// CRLF not handling. So no CRLF in string to decode.
598// Encode Modes: Supports only padding
599int b64decode(char *inp, int ilen, uint8_t* outp)
600{
601 int i, j, k, ii, num;
602 int rem, pcnt;
603 uint32_t res=0;
604 uint8_t getIndex[MAX_BASEINDEX_LEN];
605 uint8_t tmp, cflag;
606
607 if(inp == NULL || outp == NULL || ilen <= 0) {
608 ALOGE("[%s] received NULL pointer or zero length",__func__);
609 return -1;
610 }
611
612 memset(getIndex, MAX_BASEINDEX_LEN-1, sizeof(getIndex));
613 for(i=0;i<BASE_TABLE_SIZE;i++) {
614 getIndex[(uint8_t)bTable[i]] = (uint8_t)i;
615 }
616 getIndex[(uint8_t)'=']=0;
617
618 j=0;k=0;
619 num = ilen/4;
620 rem = ilen%4;
621 if(rem==0)
622 num = num-1;
623 cflag=0;
624 for(i=0; i<num; i++) {
625 res=0;
626 for(ii=0;ii<4;ii++) {
627 res = res << 6;
628 tmp = getIndex[(uint8_t)inp[j++]];
629 res = res | tmp;
630 cflag = cflag | tmp;
631 }
632 outp[k++] = (res >> 16)&0xFF;
633 outp[k++] = (res >> 8)&0xFF;
634 outp[k++] = res & 0xFF;
635 }
636
637 // Handle last bytes special
638 pcnt=0;
639 if(rem == 0) {
640 //With padding or full data
641 res = 0;
642 for(ii=0;ii<4;ii++) {
643 if(inp[j] == '=')
644 pcnt++;
645 res = res << 6;
646 tmp = getIndex[(uint8_t)inp[j++]];
647 res = res | tmp;
648 cflag = cflag | tmp;
649 }
650 outp[k++] = res >> 16;
651 if(pcnt == 2)
652 goto done;
653 outp[k++] = (res>>8)&0xFF;
654 if(pcnt == 1)
655 goto done;
656 outp[k++] = res&0xFF;
657 } else {
658 //without padding
659 res = 0;
660 for(i=0;i<rem;i++) {
661 res = res << 6;
662 tmp = getIndex[(uint8_t)inp[j++]];
663 res = res | tmp;
664 cflag = cflag | tmp;
665 }
666 for(i=rem;i<4;i++) {
667 res = res << 6;
668 pcnt++;
669 }
670 outp[k++] = res >> 16;
671 if(pcnt == 2)
672 goto done;
673 outp[k++] = (res>>8)&0xFF;
674 if(pcnt == 1)
675 goto done;
676 outp[k++] = res&0xFF;
677 }
678done:
679 if(cflag == 0xFF) {
680 ALOGE("[%s] base64 decode failed. Invalid character found %s",
681 __func__, inp);
682 return 0;
683 }
684 return k;
685}
686
687int b64encode(uint8_t *inp, int ilen, char* outp)
688{
689 int i,j,k, num;
690 int rem=0;
691 uint32_t res=0;
692
693 if(inp == NULL || outp == NULL || ilen<=0) {
694 ALOGE("[%s] received NULL pointer or zero input length",__func__);
695 return -1;
696 }
697
698 num = ilen/3;
699 rem = ilen%3;
700 j=0;k=0;
701 for(i=0; i<num; i++) {
702 //prepare index
703 res = inp[j++]<<16;
704 res = res | inp[j++]<<8;
705 res = res | inp[j++];
706 //get output map from index
707 outp[k++] = (char) bTable[(res>>18)&0x3F];
708 outp[k++] = (char) bTable[(res>>12)&0x3F];
709 outp[k++] = (char) bTable[(res>>6)&0x3F];
710 outp[k++] = (char) bTable[res&0x3F];
711 }
712
713 switch(rem) {
714 case 1:
715 res = inp[j++]<<16;
716 outp[k++] = (char) bTable[res>>18];
717 outp[k++] = (char) bTable[(res>>12)&0x3F];
718 //outp[k++] = '=';
719 //outp[k++] = '=';
720 break;
721 case 2:
722 res = inp[j++]<<16;
723 res = res | inp[j++]<<8;
724 outp[k++] = (char) bTable[res>>18];
725 outp[k++] = (char) bTable[(res>>12)&0x3F];
726 outp[k++] = (char) bTable[(res>>6)&0x3F];
727 //outp[k++] = '=';
728 break;
729 default:
730 break;
731 }
732done:
733 outp[k] = '\0';
734 return k;
735}