blob: cebba3f62870c18806cb67f2a6d5d92acaa7c5b8 [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),
67#ifdef INCALL_MUSIC_ENABLED
68 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_INCALL_MUSIC),
69#endif
70#ifdef COMPRESS_VOIP_ENABLED
71 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_VOIP_RX),
72#endif
73};
74
75const struct string_to_enum s_format_name_to_enum_table[] = {
76 STRING_TO_ENUM(AUDIO_FORMAT_PCM_16_BIT),
77 STRING_TO_ENUM(AUDIO_FORMAT_PCM_8_BIT),
78 STRING_TO_ENUM(AUDIO_FORMAT_MP3),
79 STRING_TO_ENUM(AUDIO_FORMAT_AAC),
80 STRING_TO_ENUM(AUDIO_FORMAT_VORBIS),
Mingming Yinae3530f2014-07-03 16:50:18 -070081 STRING_TO_ENUM(AUDIO_FORMAT_AMR_NB),
82 STRING_TO_ENUM(AUDIO_FORMAT_AMR_WB),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070083 STRING_TO_ENUM(AUDIO_FORMAT_AC3),
Mingming Yinae3530f2014-07-03 16:50:18 -070084 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3),
85#ifdef FORMATS_ENABLED
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070086 STRING_TO_ENUM(AUDIO_FORMAT_DTS),
87 STRING_TO_ENUM(AUDIO_FORMAT_DTS_LBR),
88 STRING_TO_ENUM(AUDIO_FORMAT_WMA),
89 STRING_TO_ENUM(AUDIO_FORMAT_WMA_PRO),
90 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADIF),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070091 STRING_TO_ENUM(AUDIO_FORMAT_AMR_WB_PLUS),
92 STRING_TO_ENUM(AUDIO_FORMAT_EVRC),
93 STRING_TO_ENUM(AUDIO_FORMAT_EVRCB),
94 STRING_TO_ENUM(AUDIO_FORMAT_EVRCWB),
95 STRING_TO_ENUM(AUDIO_FORMAT_QCELP),
96 STRING_TO_ENUM(AUDIO_FORMAT_MP2),
97 STRING_TO_ENUM(AUDIO_FORMAT_EVRCNW),
98 STRING_TO_ENUM(AUDIO_FORMAT_PCM_16_BIT_OFFLOAD),
99 STRING_TO_ENUM(AUDIO_FORMAT_PCM_24_BIT_OFFLOAD),
Amit Shekhar6f461b12014-08-01 14:52:58 -0700100 STRING_TO_ENUM(AUDIO_FORMAT_FLAC),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700101#endif
102};
103
Ben Rombergera04fabc2014-11-14 12:16:03 -0800104static char bTable[BASE_TABLE_SIZE] = {
105 'A','B','C','D','E','F','G','H','I','J','K','L',
106 'M','N','O','P','Q','R','S','T','U','V','W','X',
107 'Y','Z','a','b','c','d','e','f','g','h','i','j',
108 'k','l','m','n','o','p','q','r','s','t','u','v',
109 'w','x','y','z','0','1','2','3','4','5','6','7',
110 '8','9','+','/'
111};
112
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700113static uint32_t string_to_enum(const struct string_to_enum *table, size_t size,
114 const char *name)
115{
116 size_t i;
117 for (i = 0; i < size; i++) {
118 if (strcmp(table[i].name, name) == 0) {
119 ALOGV("%s found %s", __func__, table[i].name);
120 return table[i].value;
121 }
122 }
123 return 0;
124}
125
126static audio_output_flags_t parse_flag_names(char *name)
127{
128 uint32_t flag = 0;
129 char *flag_name = strtok(name, "|");
130 while (flag_name != NULL) {
131 if (strlen(flag_name) != 0) {
132 flag |= string_to_enum(s_flag_name_to_enum_table,
133 ARRAY_SIZE(s_flag_name_to_enum_table),
134 flag_name);
135 }
136 flag_name = strtok(NULL, "|");
137 }
138
139 ALOGV("parse_flag_names: flag - %d", flag);
140 return (audio_output_flags_t)flag;
141}
142
143static void parse_format_names(char *name, struct streams_output_cfg *so_info)
144{
145 struct stream_format *sf_info = NULL;
146 char *str = strtok(name, "|");
147
148 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG) == 0)
149 return;
150
151 list_init(&so_info->format_list);
152 while (str != NULL) {
153 audio_format_t format = (audio_format_t)string_to_enum(s_format_name_to_enum_table,
154 ARRAY_SIZE(s_format_name_to_enum_table), str);
155 ALOGV("%s: format - %d", __func__, format);
156 if (format != 0) {
157 sf_info = (struct stream_format *)calloc(1, sizeof(struct stream_format));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700158 if (sf_info == NULL)
159 break; /* return whatever was parsed */
160
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700161 sf_info->format = format;
162 list_add_tail(&so_info->format_list, &sf_info->list);
163 }
164 str = strtok(NULL, "|");
165 }
166}
167
Amit Shekhar6f461b12014-08-01 14:52:58 -0700168static void parse_sample_rate_names(char *name, struct streams_output_cfg *so_info)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700169{
Amit Shekhar6f461b12014-08-01 14:52:58 -0700170 struct stream_sample_rate *ss_info = NULL;
171 uint32_t sample_rate = 48000;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700172 char *str = strtok(name, "|");
173
Amit Shekhar6f461b12014-08-01 14:52:58 -0700174 if (str != NULL && 0 == strcmp(str, DYNAMIC_VALUE_TAG))
175 return;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700176
Amit Shekhar6f461b12014-08-01 14:52:58 -0700177 list_init(&so_info->sample_rate_list);
178 while (str != NULL) {
179 sample_rate = (uint32_t)strtol(str, (char **)NULL, 10);
180 ALOGV("%s: sample_rate - %d", __func__, sample_rate);
181 if (0 != sample_rate) {
182 ss_info = (struct stream_sample_rate *)calloc(1, sizeof(struct stream_sample_rate));
183 ss_info->sample_rate = sample_rate;
184 list_add_tail(&so_info->sample_rate_list, &ss_info->list);
185 }
186 str = strtok(NULL, "|");
187 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700188}
189
190static int parse_bit_width_names(char *name)
191{
192 int bit_width = 16;
193 char *str = strtok(name, "|");
194
195 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG))
196 bit_width = (int)strtol(str, (char **)NULL, 10);
197
198 ALOGV("%s: bit_width - %d", __func__, bit_width);
199 return bit_width;
200}
201
202static int parse_app_type_names(void *platform, char *name)
203{
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700204 int app_type = platform_get_default_app_type(platform);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700205 char *str = strtok(name, "|");
206
207 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG))
208 app_type = (int)strtol(str, (char **)NULL, 10);
209
210 ALOGV("%s: app_type - %d", __func__, app_type);
211 return app_type;
212}
213
214static void update_streams_output_cfg_list(cnode *root, void *platform,
215 struct listnode *streams_output_cfg_list)
216{
217 cnode *node = root->first_child;
218 struct streams_output_cfg *so_info;
219
220 ALOGV("%s", __func__);
221 so_info = (struct streams_output_cfg *)calloc(1, sizeof(struct streams_output_cfg));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700222
223 if (!so_info) {
224 ALOGE("failed to allocate mem for so_info list element");
225 return;
226 }
227
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700228 while (node) {
229 if (strcmp(node->name, FLAGS_TAG) == 0) {
230 so_info->flags = parse_flag_names((char *)node->value);
231 } else if (strcmp(node->name, FORMATS_TAG) == 0) {
232 parse_format_names((char *)node->value, so_info);
233 } else if (strcmp(node->name, SAMPLING_RATES_TAG) == 0) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700234 so_info->app_type_cfg.sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
235 parse_sample_rate_names((char *)node->value, so_info);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700236 } else if (strcmp(node->name, BIT_WIDTH_TAG) == 0) {
237 so_info->app_type_cfg.bit_width = parse_bit_width_names((char *)node->value);
238 } else if (strcmp(node->name, APP_TYPE_TAG) == 0) {
239 so_info->app_type_cfg.app_type = parse_app_type_names(platform, (char *)node->value);
240 }
241 node = node->next;
242 }
243 list_add_tail(streams_output_cfg_list, &so_info->list);
244}
245
246static void load_output(cnode *root, void *platform,
247 struct listnode *streams_output_cfg_list)
248{
249 cnode *node = config_find(root, OUTPUTS_TAG);
250 if (node == NULL) {
251 ALOGE("%s: could not load output, node is NULL", __func__);
252 return;
253 }
254
255 node = node->first_child;
256 while (node) {
257 ALOGV("%s: loading output %s", __func__, node->name);
258 update_streams_output_cfg_list(node, platform, streams_output_cfg_list);
259 node = node->next;
260 }
261}
262
263static void send_app_type_cfg(void *platform, struct mixer *mixer,
264 struct listnode *streams_output_cfg_list)
265{
266 int app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT] = {-1};
267 int length = 0, i, num_app_types = 0;
268 struct listnode *node;
269 bool update;
270 struct mixer_ctl *ctl = NULL;
271 const char *mixer_ctl_name = "App Type Config";
272 struct streams_output_cfg *so_info;
273
274 if (!mixer) {
275 ALOGE("%s: mixer is null",__func__);
276 return;
277 }
278 ctl = mixer_get_ctl_by_name(mixer, mixer_ctl_name);
279 if (!ctl) {
280 ALOGE("%s: Could not get ctl for mixer cmd - %s",__func__, mixer_ctl_name);
281 return;
282 }
283 if (streams_output_cfg_list == NULL) {
284 app_type_cfg[length++] = 1;
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700285 app_type_cfg[length++] = platform_get_default_app_type(platform);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700286 app_type_cfg[length++] = 48000;
287 app_type_cfg[length++] = 16;
288 mixer_ctl_set_array(ctl, app_type_cfg, length);
289 return;
290 }
291
292 app_type_cfg[length++] = num_app_types;
293 list_for_each(node, streams_output_cfg_list) {
294 so_info = node_to_item(node, struct streams_output_cfg, list);
295 update = true;
296 for (i=0; i<length; i=i+3) {
297 if (app_type_cfg[i+1] == -1)
298 break;
299 else if (app_type_cfg[i+1] == so_info->app_type_cfg.app_type) {
300 update = false;
301 break;
302 }
303 }
304 if (update && ((length + 3) <= MAX_LENGTH_MIXER_CONTROL_IN_INT)) {
305 num_app_types += 1 ;
306 app_type_cfg[length++] = so_info->app_type_cfg.app_type;
307 app_type_cfg[length++] = so_info->app_type_cfg.sample_rate;
308 app_type_cfg[length++] = so_info->app_type_cfg.bit_width;
309 }
310 }
311 ALOGV("%s: num_app_types: %d", __func__, num_app_types);
312 if (num_app_types) {
313 app_type_cfg[0] = num_app_types;
314 mixer_ctl_set_array(ctl, app_type_cfg, length);
315 }
316}
317
318void audio_extn_utils_update_streams_output_cfg_list(void *platform,
319 struct mixer *mixer,
320 struct listnode *streams_output_cfg_list)
321{
322 cnode *root;
323 char *data;
324
325 ALOGV("%s", __func__);
326 list_init(streams_output_cfg_list);
327 data = (char *)load_file(AUDIO_OUTPUT_POLICY_VENDOR_CONFIG_FILE, NULL);
328 if (data == NULL) {
329 send_app_type_cfg(platform, mixer, NULL);
330 ALOGE("%s: could not load output policy config file", __func__);
331 return;
332 }
333
334 root = config_node("", "");
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700335 if (root == NULL) {
336 ALOGE("cfg_list, NULL config root");
337 return;
338 }
339
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700340 config_load(root, data);
341 load_output(root, platform, streams_output_cfg_list);
342
343 send_app_type_cfg(platform, mixer, streams_output_cfg_list);
344}
345
346void audio_extn_utils_dump_streams_output_cfg_list(
347 struct listnode *streams_output_cfg_list)
348{
349 int i=0;
350 struct listnode *node_i, *node_j;
351 struct streams_output_cfg *so_info;
352 struct stream_format *sf_info;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700353 struct stream_sample_rate *ss_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700354 ALOGV("%s", __func__);
355 list_for_each(node_i, streams_output_cfg_list) {
356 so_info = node_to_item(node_i, struct streams_output_cfg, list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700357 ALOGV("%s: flags-%d, output_sample_rate-%d, output_bit_width-%d, app_type-%d",
358 __func__, so_info->flags, so_info->app_type_cfg.sample_rate,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700359 so_info->app_type_cfg.bit_width, so_info->app_type_cfg.app_type);
360 list_for_each(node_j, &so_info->format_list) {
361 sf_info = node_to_item(node_j, struct stream_format, list);
362 ALOGV("format-%x", sf_info->format);
363 }
Amit Shekhar6f461b12014-08-01 14:52:58 -0700364 list_for_each(node_j, &so_info->sample_rate_list) {
365 ss_info = node_to_item(node_j, struct stream_sample_rate, list);
366 ALOGV("sample rate-%d", ss_info->sample_rate);
367 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700368 }
369}
370
371void audio_extn_utils_release_streams_output_cfg_list(
372 struct listnode *streams_output_cfg_list)
373{
374 struct listnode *node_i, *node_j;
375 struct streams_output_cfg *so_info;
376 struct stream_format *sf_info;
377
378 ALOGV("%s", __func__);
379 while (!list_empty(streams_output_cfg_list)) {
380 node_i = list_head(streams_output_cfg_list);
381 so_info = node_to_item(node_i, struct streams_output_cfg, list);
382 while (!list_empty(&so_info->format_list)) {
383 node_j = list_head(&so_info->format_list);
384 list_remove(node_j);
385 free(node_to_item(node_j, struct stream_format, list));
386 }
Amit Shekhar6f461b12014-08-01 14:52:58 -0700387 while (!list_empty(&so_info->sample_rate_list)) {
388 node_j = list_head(&so_info->sample_rate_list);
389 list_remove(node_j);
390 free(node_to_item(node_j, struct stream_sample_rate, list));
391 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700392 list_remove(node_i);
393 free(node_to_item(node_i, struct streams_output_cfg, list));
394 }
395}
396
Amit Shekhar6f461b12014-08-01 14:52:58 -0700397static bool set_output_cfg(struct streams_output_cfg *so_info,
398 struct stream_app_type_cfg *app_type_cfg,
399 uint32_t sample_rate, uint32_t bit_width)
400 {
401 struct listnode *node_i;
402 struct stream_sample_rate *ss_info;
403 list_for_each(node_i, &so_info->sample_rate_list) {
404 ss_info = node_to_item(node_i, struct stream_sample_rate, list);
405 if ((sample_rate <= ss_info->sample_rate) &&
406 (bit_width == so_info->app_type_cfg.bit_width)) {
407 app_type_cfg->app_type = so_info->app_type_cfg.app_type;
408 app_type_cfg->sample_rate = ss_info->sample_rate;
409 app_type_cfg->bit_width = so_info->app_type_cfg.bit_width;
410 ALOGV("%s app_type_cfg->app_type %d, app_type_cfg->sample_rate %d, app_type_cfg->bit_width %d",
411 __func__, app_type_cfg->app_type, app_type_cfg->sample_rate, app_type_cfg->bit_width);
412 return true;
413 }
414 }
415 /*
416 * Reiterate through the list assuming dafault sample rate.
417 * Handles scenario where input sample rate is higher
418 * than all sample rates in list for the input bit width.
419 */
420 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
421 list_for_each(node_i, &so_info->sample_rate_list) {
422 ss_info = node_to_item(node_i, struct stream_sample_rate, list);
423 if ((sample_rate <= ss_info->sample_rate) &&
424 (bit_width == so_info->app_type_cfg.bit_width)) {
425 app_type_cfg->app_type = so_info->app_type_cfg.app_type;
426 app_type_cfg->sample_rate = sample_rate;
427 app_type_cfg->bit_width = so_info->app_type_cfg.bit_width;
428 ALOGV("%s Assuming default sample rate. app_type_cfg->app_type %d, app_type_cfg->sample_rate %d, app_type_cfg->bit_width %d",
429 __func__, app_type_cfg->app_type, app_type_cfg->sample_rate, app_type_cfg->bit_width);
430 return true;
431 }
432 }
433 return false;
434}
435
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700436void audio_extn_utils_update_stream_app_type_cfg(void *platform,
437 struct listnode *streams_output_cfg_list,
Amit Shekhar1d896042014-10-03 13:16:09 -0700438 audio_devices_t devices,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700439 audio_output_flags_t flags,
440 audio_format_t format,
Amit Shekhar6f461b12014-08-01 14:52:58 -0700441 uint32_t sample_rate,
442 uint32_t bit_width,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700443 struct stream_app_type_cfg *app_type_cfg)
444{
Amit Shekhar6f461b12014-08-01 14:52:58 -0700445 struct listnode *node_i, *node_j, *node_k;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700446 struct streams_output_cfg *so_info;
447 struct stream_format *sf_info;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700448 struct stream_sample_rate *ss_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700449
Amit Shekhar1d896042014-10-03 13:16:09 -0700450 if ((24 == bit_width) &&
451 (devices & AUDIO_DEVICE_OUT_SPEAKER)) {
Amit Shekhar5a39c912014-10-14 15:39:30 -0700452 int32_t bw = platform_get_snd_device_bit_width(SND_DEVICE_OUT_SPEAKER);
453 if (-ENOSYS != bw)
454 bit_width = (uint32_t)bw;
Amit Shekhar1d896042014-10-03 13:16:09 -0700455 sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
456 ALOGI("%s Allowing 24-bit playback on speaker ONLY at default sampling rate", __func__);
457 }
458
459 ALOGV("%s: flags: %x, format: %x sample_rate %d",
460 __func__, flags, format, sample_rate);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700461 list_for_each(node_i, streams_output_cfg_list) {
462 so_info = node_to_item(node_i, struct streams_output_cfg, list);
463 if (so_info->flags == flags) {
464 list_for_each(node_j, &so_info->format_list) {
465 sf_info = node_to_item(node_j, struct stream_format, list);
466 if (sf_info->format == format) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700467 if (set_output_cfg(so_info, app_type_cfg, sample_rate, bit_width))
468 return;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700469 }
470 }
471 }
472 }
473 list_for_each(node_i, streams_output_cfg_list) {
474 so_info = node_to_item(node_i, struct streams_output_cfg, list);
475 if (so_info->flags == AUDIO_OUTPUT_FLAG_PRIMARY) {
476 ALOGV("Compatible output profile not found.");
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700477 app_type_cfg->app_type = so_info->app_type_cfg.app_type;
478 app_type_cfg->sample_rate = so_info->app_type_cfg.sample_rate;
479 app_type_cfg->bit_width = so_info->app_type_cfg.bit_width;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700480 ALOGV("%s Default to primary output: App type: %d sample_rate %d",
481 __func__, so_info->app_type_cfg.app_type, app_type_cfg->sample_rate);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700482 return;
483 }
484 }
485 ALOGW("%s: App type could not be selected. Falling back to default", __func__);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700486 app_type_cfg->app_type = platform_get_default_app_type(platform);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700487 app_type_cfg->sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700488 app_type_cfg->bit_width = 16;
489}
490
491int audio_extn_utils_send_app_type_cfg(struct audio_usecase *usecase)
492{
493 char mixer_ctl_name[MAX_LENGTH_MIXER_CONTROL_IN_INT];
494 int app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT], len = 0, rc;
Anish Kumarc8599f22014-07-20 09:36:07 -0700495 struct stream_out *out;
496 struct audio_device *adev;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700497 struct mixer_ctl *ctl;
498 int pcm_device_id, acdb_dev_id, snd_device = usecase->out_snd_device;
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +0530499 int32_t sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700500
501 ALOGV("%s", __func__);
502
503 if (usecase->type != PCM_PLAYBACK) {
504 ALOGV("%s: not a playback path, no need to cfg app type", __func__);
505 rc = 0;
506 goto exit_send_app_type_cfg;
507 }
508 if ((usecase->id != USECASE_AUDIO_PLAYBACK_DEEP_BUFFER) &&
509 (usecase->id != USECASE_AUDIO_PLAYBACK_LOW_LATENCY) &&
510 (usecase->id != USECASE_AUDIO_PLAYBACK_MULTI_CH) &&
Alexy Joseph2f89cfa2014-10-06 12:15:01 -0700511 (!is_offload_usecase(usecase->id))) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700512 ALOGV("%s: a playback path where app type cfg is not required", __func__);
513 rc = 0;
514 goto exit_send_app_type_cfg;
515 }
Anish Kumarc8599f22014-07-20 09:36:07 -0700516 out = usecase->stream.out;
517 adev = out->dev;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700518
519 snd_device = usecase->out_snd_device;
520
521 pcm_device_id = platform_get_pcm_device_id(out->usecase, PCM_PLAYBACK);
522
523 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
524 "Audio Stream %d App Type Cfg", pcm_device_id);
525
526 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
527 if (!ctl) {
528 ALOGE("%s: Could not get ctl for mixer cmd - %s", __func__,
529 mixer_ctl_name);
530 rc = -EINVAL;
531 goto exit_send_app_type_cfg;
532 }
533 snd_device = (snd_device == SND_DEVICE_OUT_SPEAKER) ?
534 audio_extn_get_spkr_prot_snd_device(snd_device) : snd_device;
535 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
536 if (acdb_dev_id < 0) {
537 ALOGE("%s: Couldn't get the acdb dev id", __func__);
538 rc = -EINVAL;
539 goto exit_send_app_type_cfg;
540 }
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +0530541
542 if ((24 == usecase->stream.out->bit_width) &&
Amit Shekhar1d896042014-10-03 13:16:09 -0700543 (usecase->stream.out->devices & AUDIO_DEVICE_OUT_SPEAKER)) {
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +0530544 sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
545 } else {
546 sample_rate = out->app_type_cfg.sample_rate;
547 }
548
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700549 app_type_cfg[len++] = out->app_type_cfg.app_type;
550 app_type_cfg[len++] = acdb_dev_id;
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +0530551 app_type_cfg[len++] = sample_rate;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700552
553 mixer_ctl_set_array(ctl, app_type_cfg, len);
Amit Shekhar278e3362014-09-08 14:08:19 -0700554 ALOGI("%s app_type %d, acdb_dev_id %d, sample_rate %d",
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +0530555 __func__, out->app_type_cfg.app_type, acdb_dev_id, sample_rate);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700556 rc = 0;
557exit_send_app_type_cfg:
558 return rc;
559}
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700560
561void audio_extn_utils_send_audio_calibration(struct audio_device *adev,
562 struct audio_usecase *usecase)
563{
564 int type = usecase->type;
565
566 if (type == PCM_PLAYBACK) {
567 struct stream_out *out = usecase->stream.out;
568 int snd_device = usecase->out_snd_device;
569 snd_device = (snd_device == SND_DEVICE_OUT_SPEAKER) ?
570 audio_extn_get_spkr_prot_snd_device(snd_device) : snd_device;
571 platform_send_audio_calibration(adev->platform, usecase->out_snd_device,
572 out->app_type_cfg.app_type,
573 out->app_type_cfg.sample_rate);
574 }
575 if ((type == PCM_HFP_CALL) || (type == PCM_CAPTURE)) {
Narsinga Rao Chella212e2542014-11-17 19:57:04 -0800576 if ((type == PCM_CAPTURE) & voice_is_in_call_rec_stream(usecase->stream.in)) {
577 snd_device_t incall_record_snd_device =
578 voice_get_incall_rec_snd_device(usecase->in_snd_device);
579 platform_send_audio_calibration(adev->platform, incall_record_snd_device,
580 platform_get_default_app_type(adev->platform),
581 48000);
582 } else {
583 /* when app type is default. the sample rate is not used to send cal */
584 platform_send_audio_calibration(adev->platform, usecase->in_snd_device,
585 platform_get_default_app_type(adev->platform),
586 48000);
587 }
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700588 }
589}
590
Ben Rombergera04fabc2014-11-14 12:16:03 -0800591// Base64 Encode and Decode
592// Not all features supported. This must be used only with following conditions.
593// Decode Modes: Support with and without padding
594// CRLF not handling. So no CRLF in string to decode.
595// Encode Modes: Supports only padding
596int b64decode(char *inp, int ilen, uint8_t* outp)
597{
598 int i, j, k, ii, num;
599 int rem, pcnt;
600 uint32_t res=0;
601 uint8_t getIndex[MAX_BASEINDEX_LEN];
602 uint8_t tmp, cflag;
603
604 if(inp == NULL || outp == NULL || ilen <= 0) {
605 ALOGE("[%s] received NULL pointer or zero length",__func__);
606 return -1;
607 }
608
609 memset(getIndex, MAX_BASEINDEX_LEN-1, sizeof(getIndex));
610 for(i=0;i<BASE_TABLE_SIZE;i++) {
611 getIndex[(uint8_t)bTable[i]] = (uint8_t)i;
612 }
613 getIndex[(uint8_t)'=']=0;
614
615 j=0;k=0;
616 num = ilen/4;
617 rem = ilen%4;
618 if(rem==0)
619 num = num-1;
620 cflag=0;
621 for(i=0; i<num; i++) {
622 res=0;
623 for(ii=0;ii<4;ii++) {
624 res = res << 6;
625 tmp = getIndex[(uint8_t)inp[j++]];
626 res = res | tmp;
627 cflag = cflag | tmp;
628 }
629 outp[k++] = (res >> 16)&0xFF;
630 outp[k++] = (res >> 8)&0xFF;
631 outp[k++] = res & 0xFF;
632 }
633
634 // Handle last bytes special
635 pcnt=0;
636 if(rem == 0) {
637 //With padding or full data
638 res = 0;
639 for(ii=0;ii<4;ii++) {
640 if(inp[j] == '=')
641 pcnt++;
642 res = res << 6;
643 tmp = getIndex[(uint8_t)inp[j++]];
644 res = res | tmp;
645 cflag = cflag | tmp;
646 }
647 outp[k++] = res >> 16;
648 if(pcnt == 2)
649 goto done;
650 outp[k++] = (res>>8)&0xFF;
651 if(pcnt == 1)
652 goto done;
653 outp[k++] = res&0xFF;
654 } else {
655 //without padding
656 res = 0;
657 for(i=0;i<rem;i++) {
658 res = res << 6;
659 tmp = getIndex[(uint8_t)inp[j++]];
660 res = res | tmp;
661 cflag = cflag | tmp;
662 }
663 for(i=rem;i<4;i++) {
664 res = res << 6;
665 pcnt++;
666 }
667 outp[k++] = res >> 16;
668 if(pcnt == 2)
669 goto done;
670 outp[k++] = (res>>8)&0xFF;
671 if(pcnt == 1)
672 goto done;
673 outp[k++] = res&0xFF;
674 }
675done:
676 if(cflag == 0xFF) {
677 ALOGE("[%s] base64 decode failed. Invalid character found %s",
678 __func__, inp);
679 return 0;
680 }
681 return k;
682}
683
684int b64encode(uint8_t *inp, int ilen, char* outp)
685{
686 int i,j,k, num;
687 int rem=0;
688 uint32_t res=0;
689
690 if(inp == NULL || outp == NULL || ilen<=0) {
691 ALOGE("[%s] received NULL pointer or zero input length",__func__);
692 return -1;
693 }
694
695 num = ilen/3;
696 rem = ilen%3;
697 j=0;k=0;
698 for(i=0; i<num; i++) {
699 //prepare index
700 res = inp[j++]<<16;
701 res = res | inp[j++]<<8;
702 res = res | inp[j++];
703 //get output map from index
704 outp[k++] = (char) bTable[(res>>18)&0x3F];
705 outp[k++] = (char) bTable[(res>>12)&0x3F];
706 outp[k++] = (char) bTable[(res>>6)&0x3F];
707 outp[k++] = (char) bTable[res&0x3F];
708 }
709
710 switch(rem) {
711 case 1:
712 res = inp[j++]<<16;
713 outp[k++] = (char) bTable[res>>18];
714 outp[k++] = (char) bTable[(res>>12)&0x3F];
715 //outp[k++] = '=';
716 //outp[k++] = '=';
717 break;
718 case 2:
719 res = inp[j++]<<16;
720 res = res | inp[j++]<<8;
721 outp[k++] = (char) bTable[res>>18];
722 outp[k++] = (char) bTable[(res>>12)&0x3F];
723 outp[k++] = (char) bTable[(res>>6)&0x3F];
724 //outp[k++] = '=';
725 break;
726 default:
727 break;
728 }
729done:
730 outp[k] = '\0';
731 return k;
732}