blob: f1a6dfa75efac7b14bd6baadb1a7603e163b0470 [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),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700108#endif
109};
110
Ben Rombergera04fabc2014-11-14 12:16:03 -0800111static char bTable[BASE_TABLE_SIZE] = {
112 'A','B','C','D','E','F','G','H','I','J','K','L',
113 'M','N','O','P','Q','R','S','T','U','V','W','X',
114 'Y','Z','a','b','c','d','e','f','g','h','i','j',
115 'k','l','m','n','o','p','q','r','s','t','u','v',
116 'w','x','y','z','0','1','2','3','4','5','6','7',
117 '8','9','+','/'
118};
119
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700120static uint32_t string_to_enum(const struct string_to_enum *table, size_t size,
121 const char *name)
122{
123 size_t i;
124 for (i = 0; i < size; i++) {
125 if (strcmp(table[i].name, name) == 0) {
126 ALOGV("%s found %s", __func__, table[i].name);
127 return table[i].value;
128 }
129 }
130 return 0;
131}
132
133static audio_output_flags_t parse_flag_names(char *name)
134{
135 uint32_t flag = 0;
136 char *flag_name = strtok(name, "|");
137 while (flag_name != NULL) {
138 if (strlen(flag_name) != 0) {
139 flag |= string_to_enum(s_flag_name_to_enum_table,
140 ARRAY_SIZE(s_flag_name_to_enum_table),
141 flag_name);
142 }
143 flag_name = strtok(NULL, "|");
144 }
145
146 ALOGV("parse_flag_names: flag - %d", flag);
147 return (audio_output_flags_t)flag;
148}
149
150static void parse_format_names(char *name, struct streams_output_cfg *so_info)
151{
152 struct stream_format *sf_info = NULL;
153 char *str = strtok(name, "|");
154
155 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG) == 0)
156 return;
157
158 list_init(&so_info->format_list);
159 while (str != NULL) {
160 audio_format_t format = (audio_format_t)string_to_enum(s_format_name_to_enum_table,
161 ARRAY_SIZE(s_format_name_to_enum_table), str);
162 ALOGV("%s: format - %d", __func__, format);
163 if (format != 0) {
164 sf_info = (struct stream_format *)calloc(1, sizeof(struct stream_format));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700165 if (sf_info == NULL)
166 break; /* return whatever was parsed */
167
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700168 sf_info->format = format;
169 list_add_tail(&so_info->format_list, &sf_info->list);
170 }
171 str = strtok(NULL, "|");
172 }
173}
174
Amit Shekhar6f461b12014-08-01 14:52:58 -0700175static void parse_sample_rate_names(char *name, struct streams_output_cfg *so_info)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700176{
Amit Shekhar6f461b12014-08-01 14:52:58 -0700177 struct stream_sample_rate *ss_info = NULL;
178 uint32_t sample_rate = 48000;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700179 char *str = strtok(name, "|");
180
Amit Shekhar6f461b12014-08-01 14:52:58 -0700181 if (str != NULL && 0 == strcmp(str, DYNAMIC_VALUE_TAG))
182 return;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700183
Amit Shekhar6f461b12014-08-01 14:52:58 -0700184 list_init(&so_info->sample_rate_list);
185 while (str != NULL) {
186 sample_rate = (uint32_t)strtol(str, (char **)NULL, 10);
187 ALOGV("%s: sample_rate - %d", __func__, sample_rate);
188 if (0 != sample_rate) {
189 ss_info = (struct stream_sample_rate *)calloc(1, sizeof(struct stream_sample_rate));
Chaithanya Krishna Bacharajudb6236a2015-01-30 10:08:33 +0530190 if (ss_info == NULL)
191 break; /* return whatever was parsed */
192
Amit Shekhar6f461b12014-08-01 14:52:58 -0700193 ss_info->sample_rate = sample_rate;
194 list_add_tail(&so_info->sample_rate_list, &ss_info->list);
195 }
196 str = strtok(NULL, "|");
197 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700198}
199
200static int parse_bit_width_names(char *name)
201{
202 int bit_width = 16;
203 char *str = strtok(name, "|");
204
205 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG))
206 bit_width = (int)strtol(str, (char **)NULL, 10);
207
208 ALOGV("%s: bit_width - %d", __func__, bit_width);
209 return bit_width;
210}
211
212static int parse_app_type_names(void *platform, char *name)
213{
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700214 int app_type = platform_get_default_app_type(platform);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700215 char *str = strtok(name, "|");
216
217 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG))
218 app_type = (int)strtol(str, (char **)NULL, 10);
219
220 ALOGV("%s: app_type - %d", __func__, app_type);
221 return app_type;
222}
223
224static void update_streams_output_cfg_list(cnode *root, void *platform,
225 struct listnode *streams_output_cfg_list)
226{
227 cnode *node = root->first_child;
228 struct streams_output_cfg *so_info;
229
230 ALOGV("%s", __func__);
231 so_info = (struct streams_output_cfg *)calloc(1, sizeof(struct streams_output_cfg));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700232
233 if (!so_info) {
234 ALOGE("failed to allocate mem for so_info list element");
235 return;
236 }
237
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700238 while (node) {
239 if (strcmp(node->name, FLAGS_TAG) == 0) {
240 so_info->flags = parse_flag_names((char *)node->value);
241 } else if (strcmp(node->name, FORMATS_TAG) == 0) {
242 parse_format_names((char *)node->value, so_info);
243 } else if (strcmp(node->name, SAMPLING_RATES_TAG) == 0) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700244 so_info->app_type_cfg.sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
245 parse_sample_rate_names((char *)node->value, so_info);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700246 } else if (strcmp(node->name, BIT_WIDTH_TAG) == 0) {
247 so_info->app_type_cfg.bit_width = parse_bit_width_names((char *)node->value);
248 } else if (strcmp(node->name, APP_TYPE_TAG) == 0) {
249 so_info->app_type_cfg.app_type = parse_app_type_names(platform, (char *)node->value);
250 }
251 node = node->next;
252 }
253 list_add_tail(streams_output_cfg_list, &so_info->list);
254}
255
256static void load_output(cnode *root, void *platform,
257 struct listnode *streams_output_cfg_list)
258{
259 cnode *node = config_find(root, OUTPUTS_TAG);
260 if (node == NULL) {
261 ALOGE("%s: could not load output, node is NULL", __func__);
262 return;
263 }
264
265 node = node->first_child;
266 while (node) {
267 ALOGV("%s: loading output %s", __func__, node->name);
268 update_streams_output_cfg_list(node, platform, streams_output_cfg_list);
269 node = node->next;
270 }
271}
272
273static void send_app_type_cfg(void *platform, struct mixer *mixer,
274 struct listnode *streams_output_cfg_list)
275{
276 int app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT] = {-1};
277 int length = 0, i, num_app_types = 0;
278 struct listnode *node;
279 bool update;
280 struct mixer_ctl *ctl = NULL;
281 const char *mixer_ctl_name = "App Type Config";
282 struct streams_output_cfg *so_info;
283
284 if (!mixer) {
285 ALOGE("%s: mixer is null",__func__);
286 return;
287 }
288 ctl = mixer_get_ctl_by_name(mixer, mixer_ctl_name);
289 if (!ctl) {
290 ALOGE("%s: Could not get ctl for mixer cmd - %s",__func__, mixer_ctl_name);
291 return;
292 }
293 if (streams_output_cfg_list == NULL) {
294 app_type_cfg[length++] = 1;
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700295 app_type_cfg[length++] = platform_get_default_app_type(platform);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700296 app_type_cfg[length++] = 48000;
297 app_type_cfg[length++] = 16;
298 mixer_ctl_set_array(ctl, app_type_cfg, length);
299 return;
300 }
301
302 app_type_cfg[length++] = num_app_types;
303 list_for_each(node, streams_output_cfg_list) {
304 so_info = node_to_item(node, struct streams_output_cfg, list);
305 update = true;
306 for (i=0; i<length; i=i+3) {
307 if (app_type_cfg[i+1] == -1)
308 break;
309 else if (app_type_cfg[i+1] == so_info->app_type_cfg.app_type) {
310 update = false;
311 break;
312 }
313 }
314 if (update && ((length + 3) <= MAX_LENGTH_MIXER_CONTROL_IN_INT)) {
315 num_app_types += 1 ;
316 app_type_cfg[length++] = so_info->app_type_cfg.app_type;
317 app_type_cfg[length++] = so_info->app_type_cfg.sample_rate;
318 app_type_cfg[length++] = so_info->app_type_cfg.bit_width;
319 }
320 }
321 ALOGV("%s: num_app_types: %d", __func__, num_app_types);
322 if (num_app_types) {
323 app_type_cfg[0] = num_app_types;
324 mixer_ctl_set_array(ctl, app_type_cfg, length);
325 }
326}
327
328void audio_extn_utils_update_streams_output_cfg_list(void *platform,
329 struct mixer *mixer,
330 struct listnode *streams_output_cfg_list)
331{
332 cnode *root;
333 char *data;
334
335 ALOGV("%s", __func__);
336 list_init(streams_output_cfg_list);
337 data = (char *)load_file(AUDIO_OUTPUT_POLICY_VENDOR_CONFIG_FILE, NULL);
338 if (data == NULL) {
339 send_app_type_cfg(platform, mixer, NULL);
340 ALOGE("%s: could not load output policy config file", __func__);
341 return;
342 }
343
344 root = config_node("", "");
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700345 if (root == NULL) {
346 ALOGE("cfg_list, NULL config root");
347 return;
348 }
349
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700350 config_load(root, data);
351 load_output(root, platform, streams_output_cfg_list);
352
353 send_app_type_cfg(platform, mixer, streams_output_cfg_list);
354}
355
356void audio_extn_utils_dump_streams_output_cfg_list(
357 struct listnode *streams_output_cfg_list)
358{
359 int i=0;
360 struct listnode *node_i, *node_j;
361 struct streams_output_cfg *so_info;
362 struct stream_format *sf_info;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700363 struct stream_sample_rate *ss_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700364 ALOGV("%s", __func__);
365 list_for_each(node_i, streams_output_cfg_list) {
366 so_info = node_to_item(node_i, struct streams_output_cfg, list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700367 ALOGV("%s: flags-%d, output_sample_rate-%d, output_bit_width-%d, app_type-%d",
368 __func__, so_info->flags, so_info->app_type_cfg.sample_rate,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700369 so_info->app_type_cfg.bit_width, so_info->app_type_cfg.app_type);
370 list_for_each(node_j, &so_info->format_list) {
371 sf_info = node_to_item(node_j, struct stream_format, list);
372 ALOGV("format-%x", sf_info->format);
373 }
Amit Shekhar6f461b12014-08-01 14:52:58 -0700374 list_for_each(node_j, &so_info->sample_rate_list) {
375 ss_info = node_to_item(node_j, struct stream_sample_rate, list);
376 ALOGV("sample rate-%d", ss_info->sample_rate);
377 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700378 }
379}
380
381void audio_extn_utils_release_streams_output_cfg_list(
382 struct listnode *streams_output_cfg_list)
383{
384 struct listnode *node_i, *node_j;
385 struct streams_output_cfg *so_info;
386 struct stream_format *sf_info;
387
388 ALOGV("%s", __func__);
389 while (!list_empty(streams_output_cfg_list)) {
390 node_i = list_head(streams_output_cfg_list);
391 so_info = node_to_item(node_i, struct streams_output_cfg, list);
392 while (!list_empty(&so_info->format_list)) {
393 node_j = list_head(&so_info->format_list);
394 list_remove(node_j);
395 free(node_to_item(node_j, struct stream_format, list));
396 }
Amit Shekhar6f461b12014-08-01 14:52:58 -0700397 while (!list_empty(&so_info->sample_rate_list)) {
398 node_j = list_head(&so_info->sample_rate_list);
399 list_remove(node_j);
400 free(node_to_item(node_j, struct stream_sample_rate, list));
401 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700402 list_remove(node_i);
403 free(node_to_item(node_i, struct streams_output_cfg, list));
404 }
405}
406
Amit Shekhar6f461b12014-08-01 14:52:58 -0700407static bool set_output_cfg(struct streams_output_cfg *so_info,
408 struct stream_app_type_cfg *app_type_cfg,
409 uint32_t sample_rate, uint32_t bit_width)
410 {
411 struct listnode *node_i;
412 struct stream_sample_rate *ss_info;
413 list_for_each(node_i, &so_info->sample_rate_list) {
414 ss_info = node_to_item(node_i, struct stream_sample_rate, list);
415 if ((sample_rate <= ss_info->sample_rate) &&
416 (bit_width == so_info->app_type_cfg.bit_width)) {
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700417
Amit Shekhar6f461b12014-08-01 14:52:58 -0700418 app_type_cfg->app_type = so_info->app_type_cfg.app_type;
419 app_type_cfg->sample_rate = ss_info->sample_rate;
420 app_type_cfg->bit_width = so_info->app_type_cfg.bit_width;
421 ALOGV("%s app_type_cfg->app_type %d, app_type_cfg->sample_rate %d, app_type_cfg->bit_width %d",
422 __func__, app_type_cfg->app_type, app_type_cfg->sample_rate, app_type_cfg->bit_width);
423 return true;
424 }
425 }
426 /*
427 * Reiterate through the list assuming dafault sample rate.
428 * Handles scenario where input sample rate is higher
429 * than all sample rates in list for the input bit width.
430 */
431 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
Apoorv Raghuvanshif59bb222015-02-18 12:23:23 -0800432
Amit Shekhar6f461b12014-08-01 14:52:58 -0700433 list_for_each(node_i, &so_info->sample_rate_list) {
434 ss_info = node_to_item(node_i, struct stream_sample_rate, list);
435 if ((sample_rate <= ss_info->sample_rate) &&
436 (bit_width == so_info->app_type_cfg.bit_width)) {
437 app_type_cfg->app_type = so_info->app_type_cfg.app_type;
438 app_type_cfg->sample_rate = sample_rate;
439 app_type_cfg->bit_width = so_info->app_type_cfg.bit_width;
Apoorv Raghuvanshif59bb222015-02-18 12:23:23 -0800440 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 -0700441 __func__, app_type_cfg->app_type, app_type_cfg->sample_rate, app_type_cfg->bit_width);
442 return true;
443 }
444 }
445 return false;
446}
447
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700448void audio_extn_utils_update_stream_app_type_cfg(void *platform,
449 struct listnode *streams_output_cfg_list,
Amit Shekhar1d896042014-10-03 13:16:09 -0700450 audio_devices_t devices,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700451 audio_output_flags_t flags,
452 audio_format_t format,
Amit Shekhar6f461b12014-08-01 14:52:58 -0700453 uint32_t sample_rate,
454 uint32_t bit_width,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700455 struct stream_app_type_cfg *app_type_cfg)
456{
Amit Shekhar6f461b12014-08-01 14:52:58 -0700457 struct listnode *node_i, *node_j, *node_k;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700458 struct streams_output_cfg *so_info;
459 struct stream_format *sf_info;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700460 struct stream_sample_rate *ss_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700461
Amit Shekhar1d896042014-10-03 13:16:09 -0700462 if ((24 == bit_width) &&
463 (devices & AUDIO_DEVICE_OUT_SPEAKER)) {
Amit Shekhar5a39c912014-10-14 15:39:30 -0700464 int32_t bw = platform_get_snd_device_bit_width(SND_DEVICE_OUT_SPEAKER);
465 if (-ENOSYS != bw)
466 bit_width = (uint32_t)bw;
Amit Shekhar1d896042014-10-03 13:16:09 -0700467 sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
468 ALOGI("%s Allowing 24-bit playback on speaker ONLY at default sampling rate", __func__);
469 }
470
471 ALOGV("%s: flags: %x, format: %x sample_rate %d",
472 __func__, flags, format, sample_rate);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700473 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 == flags) {
476 list_for_each(node_j, &so_info->format_list) {
477 sf_info = node_to_item(node_j, struct stream_format, list);
478 if (sf_info->format == format) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700479 if (set_output_cfg(so_info, app_type_cfg, sample_rate, bit_width))
480 return;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700481 }
482 }
483 }
484 }
485 list_for_each(node_i, streams_output_cfg_list) {
486 so_info = node_to_item(node_i, struct streams_output_cfg, list);
487 if (so_info->flags == AUDIO_OUTPUT_FLAG_PRIMARY) {
488 ALOGV("Compatible output profile not found.");
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700489 app_type_cfg->app_type = so_info->app_type_cfg.app_type;
490 app_type_cfg->sample_rate = so_info->app_type_cfg.sample_rate;
491 app_type_cfg->bit_width = so_info->app_type_cfg.bit_width;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700492 ALOGV("%s Default to primary output: App type: %d sample_rate %d",
493 __func__, so_info->app_type_cfg.app_type, app_type_cfg->sample_rate);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700494 return;
495 }
496 }
497 ALOGW("%s: App type could not be selected. Falling back to default", __func__);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700498 app_type_cfg->app_type = platform_get_default_app_type(platform);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700499 app_type_cfg->sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700500 app_type_cfg->bit_width = 16;
501}
502
Srikanth Uyyala9d551402015-08-25 16:03:42 +0530503#define GENERAL_RECORDING_APP_TYPE 0x11132
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700504int 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;
Srikanth Uyyala9d551402015-08-25 16:03:42 +0530509 struct stream_in *in;
Anish Kumarc8599f22014-07-20 09:36:07 -0700510 struct audio_device *adev;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700511 struct mixer_ctl *ctl;
512 int pcm_device_id, acdb_dev_id, snd_device = usecase->out_snd_device;
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +0530513 int32_t sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700514
515 ALOGV("%s", __func__);
516
Srikanth Uyyala9d551402015-08-25 16:03:42 +0530517 if (usecase->type != PCM_PLAYBACK && usecase->type != PCM_CAPTURE) {
518 ALOGE("%s: not a playback or capture path, no need to cfg app type", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700519 rc = 0;
520 goto exit_send_app_type_cfg;
521 }
522 if ((usecase->id != USECASE_AUDIO_PLAYBACK_DEEP_BUFFER) &&
523 (usecase->id != USECASE_AUDIO_PLAYBACK_LOW_LATENCY) &&
524 (usecase->id != USECASE_AUDIO_PLAYBACK_MULTI_CH) &&
Srikanth Uyyala9d551402015-08-25 16:03:42 +0530525 (!is_offload_usecase(usecase->id)) &&
526 (usecase->type != PCM_CAPTURE)) {
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700527 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 -0700528 rc = 0;
529 goto exit_send_app_type_cfg;
530 }
Srikanth Uyyala9d551402015-08-25 16:03:42 +0530531 if (usecase->type == PCM_PLAYBACK) {
532 out = usecase->stream.out;
533 adev = out->dev;
534 snd_device = usecase->out_snd_device;
535 pcm_device_id = platform_get_pcm_device_id(out->usecase, PCM_PLAYBACK);
536 }
537 if (usecase->type == PCM_CAPTURE) {
538 in = usecase->stream.in;
539 adev = in->dev;
540 snd_device = usecase->in_snd_device;
541 pcm_device_id = platform_get_pcm_device_id(in->usecase, PCM_CAPTURE);
542 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700543
544 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
545 "Audio Stream %d App Type Cfg", pcm_device_id);
546
547 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
548 if (!ctl) {
549 ALOGE("%s: Could not get ctl for mixer cmd - %s", __func__,
550 mixer_ctl_name);
551 rc = -EINVAL;
552 goto exit_send_app_type_cfg;
553 }
554 snd_device = (snd_device == SND_DEVICE_OUT_SPEAKER) ?
555 audio_extn_get_spkr_prot_snd_device(snd_device) : snd_device;
556 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
557 if (acdb_dev_id < 0) {
558 ALOGE("%s: Couldn't get the acdb dev id", __func__);
559 rc = -EINVAL;
560 goto exit_send_app_type_cfg;
561 }
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +0530562
Srikanth Uyyala9d551402015-08-25 16:03:42 +0530563 if (usecase->type == PCM_PLAYBACK) {
564 if ((24 == usecase->stream.out->bit_width) &&
565 (usecase->stream.out->devices & AUDIO_DEVICE_OUT_SPEAKER)) {
566 sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
567 } else if ((snd_device != SND_DEVICE_OUT_HEADPHONES_44_1 &&
568 usecase->stream.out->sample_rate == OUTPUT_SAMPLING_RATE_44100) ||
569 (usecase->stream.out->sample_rate < OUTPUT_SAMPLING_RATE_44100)) {
570 sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
571 } else {
572 sample_rate = out->app_type_cfg.sample_rate;
573 }
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +0530574
Srikanth Uyyala9d551402015-08-25 16:03:42 +0530575 app_type_cfg[len++] = out->app_type_cfg.app_type;
576 app_type_cfg[len++] = acdb_dev_id;
577 if (((out->format == AUDIO_FORMAT_E_AC3) ||
578 (out->format == AUDIO_FORMAT_E_AC3_JOC))
Mingming Yin497419f2015-07-01 16:57:32 -0700579#ifdef HDMI_PASSTHROUGH_ENABLED
Srikanth Uyyala9d551402015-08-25 16:03:42 +0530580 && (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH)
Mingming Yin497419f2015-07-01 16:57:32 -0700581#endif
Srikanth Uyyala9d551402015-08-25 16:03:42 +0530582 )
583 app_type_cfg[len++] = sample_rate * 4;
584 else
585 app_type_cfg[len++] = sample_rate;
586 ALOGI("%s PLAYBACK app_type %d, acdb_dev_id %d, sample_rate %d",
587 __func__, out->app_type_cfg.app_type, acdb_dev_id, sample_rate);
588 }
589 else if (usecase->type == PCM_CAPTURE) {
590 app_type_cfg[len++] = GENERAL_RECORDING_APP_TYPE;
591 app_type_cfg[len++] = acdb_dev_id;
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700592 app_type_cfg[len++] = sample_rate;
Srikanth Uyyala9d551402015-08-25 16:03:42 +0530593 ALOGI("%s CAPTURE app_type %d, acdb_dev_id %d, sample_rate %d",
594 __func__, platform_get_default_app_type(adev->platform), acdb_dev_id, sample_rate);
595 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700596 mixer_ctl_set_array(ctl, app_type_cfg, len);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700597 rc = 0;
598exit_send_app_type_cfg:
599 return rc;
600}
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700601
602void audio_extn_utils_send_audio_calibration(struct audio_device *adev,
603 struct audio_usecase *usecase)
604{
605 int type = usecase->type;
606
607 if (type == PCM_PLAYBACK) {
608 struct stream_out *out = usecase->stream.out;
609 int snd_device = usecase->out_snd_device;
610 snd_device = (snd_device == SND_DEVICE_OUT_SPEAKER) ?
611 audio_extn_get_spkr_prot_snd_device(snd_device) : snd_device;
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +0530612 platform_send_audio_calibration(adev->platform, usecase,
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700613 out->app_type_cfg.app_type,
614 out->app_type_cfg.sample_rate);
615 }
616 if ((type == PCM_HFP_CALL) || (type == PCM_CAPTURE)) {
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +0530617 /* when app type is default. the sample rate is not used to send cal */
618 platform_send_audio_calibration(adev->platform, usecase,
619 platform_get_default_app_type(adev->platform),
620 48000);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700621 }
622}
623
Ben Rombergera04fabc2014-11-14 12:16:03 -0800624// Base64 Encode and Decode
625// Not all features supported. This must be used only with following conditions.
626// Decode Modes: Support with and without padding
627// CRLF not handling. So no CRLF in string to decode.
628// Encode Modes: Supports only padding
629int b64decode(char *inp, int ilen, uint8_t* outp)
630{
631 int i, j, k, ii, num;
632 int rem, pcnt;
633 uint32_t res=0;
634 uint8_t getIndex[MAX_BASEINDEX_LEN];
635 uint8_t tmp, cflag;
636
637 if(inp == NULL || outp == NULL || ilen <= 0) {
638 ALOGE("[%s] received NULL pointer or zero length",__func__);
639 return -1;
640 }
641
642 memset(getIndex, MAX_BASEINDEX_LEN-1, sizeof(getIndex));
643 for(i=0;i<BASE_TABLE_SIZE;i++) {
644 getIndex[(uint8_t)bTable[i]] = (uint8_t)i;
645 }
646 getIndex[(uint8_t)'=']=0;
647
648 j=0;k=0;
649 num = ilen/4;
650 rem = ilen%4;
651 if(rem==0)
652 num = num-1;
653 cflag=0;
654 for(i=0; i<num; i++) {
655 res=0;
656 for(ii=0;ii<4;ii++) {
657 res = res << 6;
658 tmp = getIndex[(uint8_t)inp[j++]];
659 res = res | tmp;
660 cflag = cflag | tmp;
661 }
662 outp[k++] = (res >> 16)&0xFF;
663 outp[k++] = (res >> 8)&0xFF;
664 outp[k++] = res & 0xFF;
665 }
666
667 // Handle last bytes special
668 pcnt=0;
669 if(rem == 0) {
670 //With padding or full data
671 res = 0;
672 for(ii=0;ii<4;ii++) {
673 if(inp[j] == '=')
674 pcnt++;
675 res = res << 6;
676 tmp = getIndex[(uint8_t)inp[j++]];
677 res = res | tmp;
678 cflag = cflag | tmp;
679 }
680 outp[k++] = res >> 16;
681 if(pcnt == 2)
682 goto done;
683 outp[k++] = (res>>8)&0xFF;
684 if(pcnt == 1)
685 goto done;
686 outp[k++] = res&0xFF;
687 } else {
688 //without padding
689 res = 0;
690 for(i=0;i<rem;i++) {
691 res = res << 6;
692 tmp = getIndex[(uint8_t)inp[j++]];
693 res = res | tmp;
694 cflag = cflag | tmp;
695 }
696 for(i=rem;i<4;i++) {
697 res = res << 6;
698 pcnt++;
699 }
700 outp[k++] = res >> 16;
701 if(pcnt == 2)
702 goto done;
703 outp[k++] = (res>>8)&0xFF;
704 if(pcnt == 1)
705 goto done;
706 outp[k++] = res&0xFF;
707 }
708done:
709 if(cflag == 0xFF) {
710 ALOGE("[%s] base64 decode failed. Invalid character found %s",
711 __func__, inp);
712 return 0;
713 }
714 return k;
715}
716
717int b64encode(uint8_t *inp, int ilen, char* outp)
718{
719 int i,j,k, num;
720 int rem=0;
721 uint32_t res=0;
722
723 if(inp == NULL || outp == NULL || ilen<=0) {
724 ALOGE("[%s] received NULL pointer or zero input length",__func__);
725 return -1;
726 }
727
728 num = ilen/3;
729 rem = ilen%3;
730 j=0;k=0;
731 for(i=0; i<num; i++) {
732 //prepare index
733 res = inp[j++]<<16;
734 res = res | inp[j++]<<8;
735 res = res | inp[j++];
736 //get output map from index
737 outp[k++] = (char) bTable[(res>>18)&0x3F];
738 outp[k++] = (char) bTable[(res>>12)&0x3F];
739 outp[k++] = (char) bTable[(res>>6)&0x3F];
740 outp[k++] = (char) bTable[res&0x3F];
741 }
742
743 switch(rem) {
744 case 1:
745 res = inp[j++]<<16;
746 outp[k++] = (char) bTable[res>>18];
747 outp[k++] = (char) bTable[(res>>12)&0x3F];
748 //outp[k++] = '=';
749 //outp[k++] = '=';
750 break;
751 case 2:
752 res = inp[j++]<<16;
753 res = res | inp[j++]<<8;
754 outp[k++] = (char) bTable[res>>18];
755 outp[k++] = (char) bTable[(res>>12)&0x3F];
756 outp[k++] = (char) bTable[(res>>6)&0x3F];
757 //outp[k++] = '=';
758 break;
759 default:
760 break;
761 }
762done:
763 outp[k] = '\0';
764 return k;
765}