blob: b9e2a400acb8b03f5210908e0c6168fec9e0ba7c [file] [log] [blame]
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001/*
Karthik Reddy Kattad633c542015-07-15 18:18:03 +05302 * Copyright (c) 2014-2015, The Linux Foundation. All rights reserved.
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07003 * 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),
vivek mehta0ea887a2015-08-26 14:01:20 -070062 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_DIRECT_PCM),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070063 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_PRIMARY),
64 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_FAST),
65 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_DEEP_BUFFER),
66 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD),
67 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_NON_BLOCKING),
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -070068 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_HW_AV_SYNC),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070069#ifdef INCALL_MUSIC_ENABLED
70 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_INCALL_MUSIC),
71#endif
Mingming Yin497419f2015-07-01 16:57:32 -070072#ifdef HDMI_PASSTHROUGH_ENABLED
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -070073 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH),
Mingming Yin497419f2015-07-01 16:57:32 -070074#endif
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),
Satya Krishna Pindiproli5d82d012015-08-12 18:21:25 +053087#ifdef AUDIO_EXTN_FORMATS_ENABLED
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070088 STRING_TO_ENUM(AUDIO_FORMAT_DTS),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070089 STRING_TO_ENUM(AUDIO_FORMAT_WMA),
90 STRING_TO_ENUM(AUDIO_FORMAT_WMA_PRO),
91 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADIF),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070092 STRING_TO_ENUM(AUDIO_FORMAT_AMR_WB_PLUS),
93 STRING_TO_ENUM(AUDIO_FORMAT_EVRC),
94 STRING_TO_ENUM(AUDIO_FORMAT_EVRCB),
95 STRING_TO_ENUM(AUDIO_FORMAT_EVRCWB),
96 STRING_TO_ENUM(AUDIO_FORMAT_QCELP),
97 STRING_TO_ENUM(AUDIO_FORMAT_MP2),
98 STRING_TO_ENUM(AUDIO_FORMAT_EVRCNW),
99 STRING_TO_ENUM(AUDIO_FORMAT_PCM_16_BIT_OFFLOAD),
100 STRING_TO_ENUM(AUDIO_FORMAT_PCM_24_BIT_OFFLOAD),
Amit Shekhar6f461b12014-08-01 14:52:58 -0700101 STRING_TO_ENUM(AUDIO_FORMAT_FLAC),
Satya Krishna Pindiproli70471602015-04-24 19:12:43 +0530102 STRING_TO_ENUM(AUDIO_FORMAT_ALAC),
103 STRING_TO_ENUM(AUDIO_FORMAT_APE),
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700104 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3_JOC),
Alexy Josephcd8eaed2014-12-11 12:46:53 -0800105 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LC),
106 STRING_TO_ENUM(AUDIO_FORMAT_AAC_HE_V1),
107 STRING_TO_ENUM(AUDIO_FORMAT_AAC_HE_V2),
Manish Dewangana6fc5442015-08-24 20:30:31 +0530108 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS),
109 STRING_TO_ENUM(AUDIO_FORMAT_AAC_SUB_LC),
110 STRING_TO_ENUM(AUDIO_FORMAT_AAC_SUB_HE_V1),
111 STRING_TO_ENUM(AUDIO_FORMAT_AAC_SUB_HE_V2),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700112#endif
113};
114
Ben Rombergera04fabc2014-11-14 12:16:03 -0800115static char bTable[BASE_TABLE_SIZE] = {
116 'A','B','C','D','E','F','G','H','I','J','K','L',
117 'M','N','O','P','Q','R','S','T','U','V','W','X',
118 'Y','Z','a','b','c','d','e','f','g','h','i','j',
119 'k','l','m','n','o','p','q','r','s','t','u','v',
120 'w','x','y','z','0','1','2','3','4','5','6','7',
121 '8','9','+','/'
122};
123
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700124static uint32_t string_to_enum(const struct string_to_enum *table, size_t size,
125 const char *name)
126{
127 size_t i;
128 for (i = 0; i < size; i++) {
129 if (strcmp(table[i].name, name) == 0) {
130 ALOGV("%s found %s", __func__, table[i].name);
131 return table[i].value;
132 }
133 }
134 return 0;
135}
136
137static audio_output_flags_t parse_flag_names(char *name)
138{
139 uint32_t flag = 0;
140 char *flag_name = strtok(name, "|");
141 while (flag_name != NULL) {
142 if (strlen(flag_name) != 0) {
143 flag |= string_to_enum(s_flag_name_to_enum_table,
144 ARRAY_SIZE(s_flag_name_to_enum_table),
145 flag_name);
146 }
147 flag_name = strtok(NULL, "|");
148 }
149
150 ALOGV("parse_flag_names: flag - %d", flag);
151 return (audio_output_flags_t)flag;
152}
153
154static void parse_format_names(char *name, struct streams_output_cfg *so_info)
155{
156 struct stream_format *sf_info = NULL;
157 char *str = strtok(name, "|");
158
159 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG) == 0)
160 return;
161
162 list_init(&so_info->format_list);
163 while (str != NULL) {
164 audio_format_t format = (audio_format_t)string_to_enum(s_format_name_to_enum_table,
165 ARRAY_SIZE(s_format_name_to_enum_table), str);
166 ALOGV("%s: format - %d", __func__, format);
167 if (format != 0) {
168 sf_info = (struct stream_format *)calloc(1, sizeof(struct stream_format));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700169 if (sf_info == NULL)
170 break; /* return whatever was parsed */
171
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700172 sf_info->format = format;
173 list_add_tail(&so_info->format_list, &sf_info->list);
174 }
175 str = strtok(NULL, "|");
176 }
177}
178
Amit Shekhar6f461b12014-08-01 14:52:58 -0700179static void parse_sample_rate_names(char *name, struct streams_output_cfg *so_info)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700180{
Amit Shekhar6f461b12014-08-01 14:52:58 -0700181 struct stream_sample_rate *ss_info = NULL;
182 uint32_t sample_rate = 48000;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700183 char *str = strtok(name, "|");
184
Amit Shekhar6f461b12014-08-01 14:52:58 -0700185 if (str != NULL && 0 == strcmp(str, DYNAMIC_VALUE_TAG))
186 return;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700187
Amit Shekhar6f461b12014-08-01 14:52:58 -0700188 list_init(&so_info->sample_rate_list);
189 while (str != NULL) {
190 sample_rate = (uint32_t)strtol(str, (char **)NULL, 10);
191 ALOGV("%s: sample_rate - %d", __func__, sample_rate);
192 if (0 != sample_rate) {
193 ss_info = (struct stream_sample_rate *)calloc(1, sizeof(struct stream_sample_rate));
Chaithanya Krishna Bacharajudb6236a2015-01-30 10:08:33 +0530194 if (ss_info == NULL)
195 break; /* return whatever was parsed */
196
Amit Shekhar6f461b12014-08-01 14:52:58 -0700197 ss_info->sample_rate = sample_rate;
198 list_add_tail(&so_info->sample_rate_list, &ss_info->list);
199 }
200 str = strtok(NULL, "|");
201 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700202}
203
204static int parse_bit_width_names(char *name)
205{
206 int bit_width = 16;
207 char *str = strtok(name, "|");
208
209 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG))
210 bit_width = (int)strtol(str, (char **)NULL, 10);
211
212 ALOGV("%s: bit_width - %d", __func__, bit_width);
213 return bit_width;
214}
215
216static int parse_app_type_names(void *platform, char *name)
217{
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700218 int app_type = platform_get_default_app_type(platform);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700219 char *str = strtok(name, "|");
220
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;
Apoorv Raghuvanshif59bb222015-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;
Apoorv Raghuvanshif59bb222015-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
Ben Romberger1fafdde2015-09-09 19:43:15 -0700507int audio_extn_utils_send_app_type_cfg(struct audio_device *adev,
508 struct audio_usecase *usecase)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700509{
510 char mixer_ctl_name[MAX_LENGTH_MIXER_CONTROL_IN_INT];
511 int app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT], len = 0, rc;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700512 struct mixer_ctl *ctl;
513 int pcm_device_id, acdb_dev_id, snd_device = usecase->out_snd_device;
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +0530514 int32_t sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700515
516 ALOGV("%s", __func__);
517
Srikanth Uyyala9d551402015-08-25 16:03:42 +0530518 if (usecase->type != PCM_PLAYBACK && usecase->type != PCM_CAPTURE) {
519 ALOGE("%s: not a playback or capture path, no need to cfg app type", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700520 rc = 0;
521 goto exit_send_app_type_cfg;
522 }
523 if ((usecase->id != USECASE_AUDIO_PLAYBACK_DEEP_BUFFER) &&
524 (usecase->id != USECASE_AUDIO_PLAYBACK_LOW_LATENCY) &&
525 (usecase->id != USECASE_AUDIO_PLAYBACK_MULTI_CH) &&
Srikanth Uyyala9d551402015-08-25 16:03:42 +0530526 (!is_offload_usecase(usecase->id)) &&
527 (usecase->type != PCM_CAPTURE)) {
Ben Romberger1fafdde2015-09-09 19:43:15 -0700528 ALOGV("%s: a rx/tx 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 }
Srikanth Uyyala9d551402015-08-25 16:03:42 +0530532 if (usecase->type == PCM_PLAYBACK) {
Srikanth Uyyala9d551402015-08-25 16:03:42 +0530533 snd_device = usecase->out_snd_device;
Ben Romberger1fafdde2015-09-09 19:43:15 -0700534 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_PLAYBACK);
535 } else if (usecase->type == PCM_CAPTURE) {
Srikanth Uyyala9d551402015-08-25 16:03:42 +0530536 snd_device = usecase->in_snd_device;
Ben Romberger1fafdde2015-09-09 19:43:15 -0700537 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_CAPTURE);
Srikanth Uyyala9d551402015-08-25 16:03:42 +0530538 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700539
540 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
541 "Audio Stream %d App Type Cfg", pcm_device_id);
542
543 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
544 if (!ctl) {
545 ALOGE("%s: Could not get ctl for mixer cmd - %s", __func__,
546 mixer_ctl_name);
547 rc = -EINVAL;
548 goto exit_send_app_type_cfg;
549 }
550 snd_device = (snd_device == SND_DEVICE_OUT_SPEAKER) ?
551 audio_extn_get_spkr_prot_snd_device(snd_device) : snd_device;
552 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
553 if (acdb_dev_id < 0) {
554 ALOGE("%s: Couldn't get the acdb dev id", __func__);
555 rc = -EINVAL;
556 goto exit_send_app_type_cfg;
557 }
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +0530558
Ben Romberger1fafdde2015-09-09 19:43:15 -0700559 if ((usecase->type == PCM_PLAYBACK) && (usecase->stream.out == NULL)) {
560 sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
561 app_type_cfg[len++] = platform_get_default_app_type(adev->platform);
562 app_type_cfg[len++] = acdb_dev_id;
563 app_type_cfg[len++] = sample_rate;
564 ALOGI("%s PLAYBACK app_type %d, acdb_dev_id %d, sample_rate %d",
565 __func__, platform_get_default_app_type(adev->platform), acdb_dev_id, sample_rate);
566 } else if (usecase->type == PCM_PLAYBACK) {
Srikanth Uyyala9d551402015-08-25 16:03:42 +0530567 if ((24 == usecase->stream.out->bit_width) &&
568 (usecase->stream.out->devices & AUDIO_DEVICE_OUT_SPEAKER)) {
569 sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
570 } else if ((snd_device != SND_DEVICE_OUT_HEADPHONES_44_1 &&
571 usecase->stream.out->sample_rate == OUTPUT_SAMPLING_RATE_44100) ||
572 (usecase->stream.out->sample_rate < OUTPUT_SAMPLING_RATE_44100)) {
573 sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
574 } else {
Ben Romberger1fafdde2015-09-09 19:43:15 -0700575 sample_rate = usecase->stream.out->app_type_cfg.sample_rate;
Srikanth Uyyala9d551402015-08-25 16:03:42 +0530576 }
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +0530577
Ben Romberger1fafdde2015-09-09 19:43:15 -0700578 app_type_cfg[len++] = usecase->stream.out->app_type_cfg.app_type;
Srikanth Uyyala9d551402015-08-25 16:03:42 +0530579 app_type_cfg[len++] = acdb_dev_id;
Ben Romberger1fafdde2015-09-09 19:43:15 -0700580 if (((usecase->stream.out->format == AUDIO_FORMAT_E_AC3) ||
581 (usecase->stream.out->format == AUDIO_FORMAT_E_AC3_JOC))
Mingming Yin497419f2015-07-01 16:57:32 -0700582#ifdef HDMI_PASSTHROUGH_ENABLED
Srikanth Uyyala9d551402015-08-25 16:03:42 +0530583 && (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH)
Mingming Yin497419f2015-07-01 16:57:32 -0700584#endif
Srikanth Uyyala9d551402015-08-25 16:03:42 +0530585 )
586 app_type_cfg[len++] = sample_rate * 4;
587 else
588 app_type_cfg[len++] = sample_rate;
589 ALOGI("%s PLAYBACK app_type %d, acdb_dev_id %d, sample_rate %d",
Ben Romberger1fafdde2015-09-09 19:43:15 -0700590 __func__, usecase->stream.out->app_type_cfg.app_type, acdb_dev_id, sample_rate);
591 } else if (usecase->type == PCM_CAPTURE) {
592 app_type_cfg[len++] = platform_get_default_app_type(adev->platform);
Srikanth Uyyala9d551402015-08-25 16:03:42 +0530593 app_type_cfg[len++] = acdb_dev_id;
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700594 app_type_cfg[len++] = sample_rate;
Srikanth Uyyala9d551402015-08-25 16:03:42 +0530595 ALOGI("%s CAPTURE app_type %d, acdb_dev_id %d, sample_rate %d",
596 __func__, platform_get_default_app_type(adev->platform), acdb_dev_id, sample_rate);
597 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700598 mixer_ctl_set_array(ctl, app_type_cfg, len);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700599 rc = 0;
600exit_send_app_type_cfg:
601 return rc;
602}
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700603
604void audio_extn_utils_send_audio_calibration(struct audio_device *adev,
605 struct audio_usecase *usecase)
606{
607 int type = usecase->type;
608
609 if (type == PCM_PLAYBACK) {
610 struct stream_out *out = usecase->stream.out;
611 int snd_device = usecase->out_snd_device;
612 snd_device = (snd_device == SND_DEVICE_OUT_SPEAKER) ?
613 audio_extn_get_spkr_prot_snd_device(snd_device) : snd_device;
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +0530614 platform_send_audio_calibration(adev->platform, usecase,
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700615 out->app_type_cfg.app_type,
616 out->app_type_cfg.sample_rate);
617 }
618 if ((type == PCM_HFP_CALL) || (type == PCM_CAPTURE)) {
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +0530619 /* when app type is default. the sample rate is not used to send cal */
620 platform_send_audio_calibration(adev->platform, usecase,
621 platform_get_default_app_type(adev->platform),
622 48000);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700623 }
624}
625
Ben Rombergera04fabc2014-11-14 12:16:03 -0800626// Base64 Encode and Decode
627// Not all features supported. This must be used only with following conditions.
628// Decode Modes: Support with and without padding
629// CRLF not handling. So no CRLF in string to decode.
630// Encode Modes: Supports only padding
631int b64decode(char *inp, int ilen, uint8_t* outp)
632{
633 int i, j, k, ii, num;
634 int rem, pcnt;
635 uint32_t res=0;
636 uint8_t getIndex[MAX_BASEINDEX_LEN];
637 uint8_t tmp, cflag;
638
639 if(inp == NULL || outp == NULL || ilen <= 0) {
640 ALOGE("[%s] received NULL pointer or zero length",__func__);
641 return -1;
642 }
643
644 memset(getIndex, MAX_BASEINDEX_LEN-1, sizeof(getIndex));
645 for(i=0;i<BASE_TABLE_SIZE;i++) {
646 getIndex[(uint8_t)bTable[i]] = (uint8_t)i;
647 }
648 getIndex[(uint8_t)'=']=0;
649
650 j=0;k=0;
651 num = ilen/4;
652 rem = ilen%4;
653 if(rem==0)
654 num = num-1;
655 cflag=0;
656 for(i=0; i<num; i++) {
657 res=0;
658 for(ii=0;ii<4;ii++) {
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)&0xFF;
665 outp[k++] = (res >> 8)&0xFF;
666 outp[k++] = res & 0xFF;
667 }
668
669 // Handle last bytes special
670 pcnt=0;
671 if(rem == 0) {
672 //With padding or full data
673 res = 0;
674 for(ii=0;ii<4;ii++) {
675 if(inp[j] == '=')
676 pcnt++;
677 res = res << 6;
678 tmp = getIndex[(uint8_t)inp[j++]];
679 res = res | tmp;
680 cflag = cflag | tmp;
681 }
682 outp[k++] = res >> 16;
683 if(pcnt == 2)
684 goto done;
685 outp[k++] = (res>>8)&0xFF;
686 if(pcnt == 1)
687 goto done;
688 outp[k++] = res&0xFF;
689 } else {
690 //without padding
691 res = 0;
692 for(i=0;i<rem;i++) {
693 res = res << 6;
694 tmp = getIndex[(uint8_t)inp[j++]];
695 res = res | tmp;
696 cflag = cflag | tmp;
697 }
698 for(i=rem;i<4;i++) {
699 res = res << 6;
700 pcnt++;
701 }
702 outp[k++] = res >> 16;
703 if(pcnt == 2)
704 goto done;
705 outp[k++] = (res>>8)&0xFF;
706 if(pcnt == 1)
707 goto done;
708 outp[k++] = res&0xFF;
709 }
710done:
711 if(cflag == 0xFF) {
712 ALOGE("[%s] base64 decode failed. Invalid character found %s",
713 __func__, inp);
714 return 0;
715 }
716 return k;
717}
718
719int b64encode(uint8_t *inp, int ilen, char* outp)
720{
721 int i,j,k, num;
722 int rem=0;
723 uint32_t res=0;
724
725 if(inp == NULL || outp == NULL || ilen<=0) {
726 ALOGE("[%s] received NULL pointer or zero input length",__func__);
727 return -1;
728 }
729
730 num = ilen/3;
731 rem = ilen%3;
732 j=0;k=0;
733 for(i=0; i<num; i++) {
734 //prepare index
735 res = inp[j++]<<16;
736 res = res | inp[j++]<<8;
737 res = res | inp[j++];
738 //get output map from index
739 outp[k++] = (char) bTable[(res>>18)&0x3F];
740 outp[k++] = (char) bTable[(res>>12)&0x3F];
741 outp[k++] = (char) bTable[(res>>6)&0x3F];
742 outp[k++] = (char) bTable[res&0x3F];
743 }
744
745 switch(rem) {
746 case 1:
747 res = inp[j++]<<16;
748 outp[k++] = (char) bTable[res>>18];
749 outp[k++] = (char) bTable[(res>>12)&0x3F];
750 //outp[k++] = '=';
751 //outp[k++] = '=';
752 break;
753 case 2:
754 res = inp[j++]<<16;
755 res = res | inp[j++]<<8;
756 outp[k++] = (char) bTable[res>>18];
757 outp[k++] = (char) bTable[(res>>12)&0x3F];
758 outp[k++] = (char) bTable[(res>>6)&0x3F];
759 //outp[k++] = '=';
760 break;
761 default:
762 break;
763 }
764done:
765 outp[k] = '\0';
766 return k;
767}