blob: 001e21acffd23a7f8917ec936e514ea626f330db [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
Ashish Jain5a97ddd2015-05-13 10:52:34 +053038#ifdef AUDIO_EXTERNAL_HDMI_ENABLED
39#include "audio_parsers.h"
40#endif
41
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070042#define AUDIO_OUTPUT_POLICY_VENDOR_CONFIG_FILE "/vendor/etc/audio_output_policy.conf"
43
44#define OUTPUTS_TAG "outputs"
45
46#define DYNAMIC_VALUE_TAG "dynamic"
47#define FLAGS_TAG "flags"
48#define FORMATS_TAG "formats"
49#define SAMPLING_RATES_TAG "sampling_rates"
50#define BIT_WIDTH_TAG "bit_width"
51#define APP_TYPE_TAG "app_type"
52
53#define STRING_TO_ENUM(string) { #string, string }
54#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
55
Ben Rombergera04fabc2014-11-14 12:16:03 -080056#define BASE_TABLE_SIZE 64
57#define MAX_BASEINDEX_LEN 256
58
Ashish Jain5a97ddd2015-05-13 10:52:34 +053059#ifdef AUDIO_EXTERNAL_HDMI_ENABLED
60#define PROFESSIONAL (1<<0) /* 0 = consumer, 1 = professional */
61#define NON_LPCM (1<<1) /* 0 = audio, 1 = non-audio */
62#define SR_44100 (0<<0) /* 44.1kHz */
63#define SR_NOTID (1<<0) /* non indicated */
64#define SR_48000 (2<<0) /* 48kHz */
65#define SR_32000 (3<<0) /* 32kHz */
66#define SR_22050 (4<<0) /* 22.05kHz */
67#define SR_24000 (6<<0) /* 24kHz */
68#define SR_88200 (8<<0) /* 88.2kHz */
69#define SR_96000 (10<<0) /* 96kHz */
70#define SR_176400 (12<<0) /* 176.4kHz */
71#define SR_192000 (14<<0) /* 192kHz */
72
73#endif
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070074struct string_to_enum {
75 const char *name;
76 uint32_t value;
77};
78
79const struct string_to_enum s_flag_name_to_enum_table[] = {
80 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_DIRECT),
81 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_PRIMARY),
82 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_FAST),
83 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_DEEP_BUFFER),
84 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD),
85 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_NON_BLOCKING),
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -070086 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_HW_AV_SYNC),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070087#ifdef INCALL_MUSIC_ENABLED
88 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_INCALL_MUSIC),
89#endif
90#ifdef COMPRESS_VOIP_ENABLED
91 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_VOIP_RX),
92#endif
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -070093 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070094};
95
96const struct string_to_enum s_format_name_to_enum_table[] = {
97 STRING_TO_ENUM(AUDIO_FORMAT_PCM_16_BIT),
98 STRING_TO_ENUM(AUDIO_FORMAT_PCM_8_BIT),
99 STRING_TO_ENUM(AUDIO_FORMAT_MP3),
100 STRING_TO_ENUM(AUDIO_FORMAT_AAC),
101 STRING_TO_ENUM(AUDIO_FORMAT_VORBIS),
Mingming Yinae3530f2014-07-03 16:50:18 -0700102 STRING_TO_ENUM(AUDIO_FORMAT_AMR_NB),
103 STRING_TO_ENUM(AUDIO_FORMAT_AMR_WB),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700104 STRING_TO_ENUM(AUDIO_FORMAT_AC3),
Mingming Yinae3530f2014-07-03 16:50:18 -0700105 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3),
106#ifdef FORMATS_ENABLED
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700107 STRING_TO_ENUM(AUDIO_FORMAT_DTS),
108 STRING_TO_ENUM(AUDIO_FORMAT_DTS_LBR),
109 STRING_TO_ENUM(AUDIO_FORMAT_WMA),
110 STRING_TO_ENUM(AUDIO_FORMAT_WMA_PRO),
111 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADIF),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700112 STRING_TO_ENUM(AUDIO_FORMAT_AMR_WB_PLUS),
113 STRING_TO_ENUM(AUDIO_FORMAT_EVRC),
114 STRING_TO_ENUM(AUDIO_FORMAT_EVRCB),
115 STRING_TO_ENUM(AUDIO_FORMAT_EVRCWB),
116 STRING_TO_ENUM(AUDIO_FORMAT_QCELP),
117 STRING_TO_ENUM(AUDIO_FORMAT_MP2),
118 STRING_TO_ENUM(AUDIO_FORMAT_EVRCNW),
119 STRING_TO_ENUM(AUDIO_FORMAT_PCM_16_BIT_OFFLOAD),
120 STRING_TO_ENUM(AUDIO_FORMAT_PCM_24_BIT_OFFLOAD),
Amit Shekhar6f461b12014-08-01 14:52:58 -0700121 STRING_TO_ENUM(AUDIO_FORMAT_FLAC),
Satya Krishna Pindiproli70471602015-04-24 19:12:43 +0530122 STRING_TO_ENUM(AUDIO_FORMAT_ALAC),
123 STRING_TO_ENUM(AUDIO_FORMAT_APE),
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700124 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3_JOC),
Alexy Josephcd8eaed2014-12-11 12:46:53 -0800125 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LC),
126 STRING_TO_ENUM(AUDIO_FORMAT_AAC_HE_V1),
127 STRING_TO_ENUM(AUDIO_FORMAT_AAC_HE_V2),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700128#endif
129};
130
Ben Rombergera04fabc2014-11-14 12:16:03 -0800131static char bTable[BASE_TABLE_SIZE] = {
132 'A','B','C','D','E','F','G','H','I','J','K','L',
133 'M','N','O','P','Q','R','S','T','U','V','W','X',
134 'Y','Z','a','b','c','d','e','f','g','h','i','j',
135 'k','l','m','n','o','p','q','r','s','t','u','v',
136 'w','x','y','z','0','1','2','3','4','5','6','7',
137 '8','9','+','/'
138};
139
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700140static uint32_t string_to_enum(const struct string_to_enum *table, size_t size,
141 const char *name)
142{
143 size_t i;
144 for (i = 0; i < size; i++) {
145 if (strcmp(table[i].name, name) == 0) {
146 ALOGV("%s found %s", __func__, table[i].name);
147 return table[i].value;
148 }
149 }
150 return 0;
151}
152
153static audio_output_flags_t parse_flag_names(char *name)
154{
155 uint32_t flag = 0;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800156 char *last_r;
157 char *flag_name = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700158 while (flag_name != NULL) {
159 if (strlen(flag_name) != 0) {
160 flag |= string_to_enum(s_flag_name_to_enum_table,
161 ARRAY_SIZE(s_flag_name_to_enum_table),
162 flag_name);
163 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800164 flag_name = strtok_r(NULL, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700165 }
166
167 ALOGV("parse_flag_names: flag - %d", flag);
168 return (audio_output_flags_t)flag;
169}
170
171static void parse_format_names(char *name, struct streams_output_cfg *so_info)
172{
173 struct stream_format *sf_info = NULL;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800174 char *last_r;
175 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700176
177 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG) == 0)
178 return;
179
180 list_init(&so_info->format_list);
181 while (str != NULL) {
182 audio_format_t format = (audio_format_t)string_to_enum(s_format_name_to_enum_table,
183 ARRAY_SIZE(s_format_name_to_enum_table), str);
184 ALOGV("%s: format - %d", __func__, format);
185 if (format != 0) {
186 sf_info = (struct stream_format *)calloc(1, sizeof(struct stream_format));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700187 if (sf_info == NULL)
188 break; /* return whatever was parsed */
189
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700190 sf_info->format = format;
191 list_add_tail(&so_info->format_list, &sf_info->list);
192 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800193 str = strtok_r(NULL, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700194 }
195}
196
Amit Shekhar6f461b12014-08-01 14:52:58 -0700197static void parse_sample_rate_names(char *name, struct streams_output_cfg *so_info)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700198{
Amit Shekhar6f461b12014-08-01 14:52:58 -0700199 struct stream_sample_rate *ss_info = NULL;
200 uint32_t sample_rate = 48000;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800201 char *last_r;
202 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700203
Amit Shekhar6f461b12014-08-01 14:52:58 -0700204 if (str != NULL && 0 == strcmp(str, DYNAMIC_VALUE_TAG))
205 return;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700206
Amit Shekhar6f461b12014-08-01 14:52:58 -0700207 list_init(&so_info->sample_rate_list);
208 while (str != NULL) {
209 sample_rate = (uint32_t)strtol(str, (char **)NULL, 10);
210 ALOGV("%s: sample_rate - %d", __func__, sample_rate);
211 if (0 != sample_rate) {
212 ss_info = (struct stream_sample_rate *)calloc(1, sizeof(struct stream_sample_rate));
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800213 if (!ss_info) {
214 ALOGE("%s: memory allocation failure", __func__);
215 return;
216 }
Amit Shekhar6f461b12014-08-01 14:52:58 -0700217 ss_info->sample_rate = sample_rate;
218 list_add_tail(&so_info->sample_rate_list, &ss_info->list);
219 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800220 str = strtok_r(NULL, "|", &last_r);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700221 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700222}
223
224static int parse_bit_width_names(char *name)
225{
226 int bit_width = 16;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800227 char *last_r;
228 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700229
230 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG))
231 bit_width = (int)strtol(str, (char **)NULL, 10);
232
233 ALOGV("%s: bit_width - %d", __func__, bit_width);
234 return bit_width;
235}
236
237static int parse_app_type_names(void *platform, char *name)
238{
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700239 int app_type = platform_get_default_app_type(platform);
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800240 char *last_r;
241 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700242
243 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG))
244 app_type = (int)strtol(str, (char **)NULL, 10);
245
246 ALOGV("%s: app_type - %d", __func__, app_type);
247 return app_type;
248}
249
250static void update_streams_output_cfg_list(cnode *root, void *platform,
251 struct listnode *streams_output_cfg_list)
252{
253 cnode *node = root->first_child;
254 struct streams_output_cfg *so_info;
255
256 ALOGV("%s", __func__);
257 so_info = (struct streams_output_cfg *)calloc(1, sizeof(struct streams_output_cfg));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700258
259 if (!so_info) {
260 ALOGE("failed to allocate mem for so_info list element");
261 return;
262 }
263
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700264 while (node) {
265 if (strcmp(node->name, FLAGS_TAG) == 0) {
266 so_info->flags = parse_flag_names((char *)node->value);
267 } else if (strcmp(node->name, FORMATS_TAG) == 0) {
268 parse_format_names((char *)node->value, so_info);
269 } else if (strcmp(node->name, SAMPLING_RATES_TAG) == 0) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700270 so_info->app_type_cfg.sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
271 parse_sample_rate_names((char *)node->value, so_info);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700272 } else if (strcmp(node->name, BIT_WIDTH_TAG) == 0) {
273 so_info->app_type_cfg.bit_width = parse_bit_width_names((char *)node->value);
274 } else if (strcmp(node->name, APP_TYPE_TAG) == 0) {
275 so_info->app_type_cfg.app_type = parse_app_type_names(platform, (char *)node->value);
276 }
277 node = node->next;
278 }
279 list_add_tail(streams_output_cfg_list, &so_info->list);
280}
281
282static void load_output(cnode *root, void *platform,
283 struct listnode *streams_output_cfg_list)
284{
285 cnode *node = config_find(root, OUTPUTS_TAG);
286 if (node == NULL) {
287 ALOGE("%s: could not load output, node is NULL", __func__);
288 return;
289 }
290
291 node = node->first_child;
292 while (node) {
293 ALOGV("%s: loading output %s", __func__, node->name);
294 update_streams_output_cfg_list(node, platform, streams_output_cfg_list);
295 node = node->next;
296 }
297}
298
299static void send_app_type_cfg(void *platform, struct mixer *mixer,
300 struct listnode *streams_output_cfg_list)
301{
302 int app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT] = {-1};
303 int length = 0, i, num_app_types = 0;
304 struct listnode *node;
305 bool update;
306 struct mixer_ctl *ctl = NULL;
307 const char *mixer_ctl_name = "App Type Config";
308 struct streams_output_cfg *so_info;
309
310 if (!mixer) {
311 ALOGE("%s: mixer is null",__func__);
312 return;
313 }
314 ctl = mixer_get_ctl_by_name(mixer, mixer_ctl_name);
315 if (!ctl) {
316 ALOGE("%s: Could not get ctl for mixer cmd - %s",__func__, mixer_ctl_name);
317 return;
318 }
319 if (streams_output_cfg_list == NULL) {
320 app_type_cfg[length++] = 1;
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700321 app_type_cfg[length++] = platform_get_default_app_type(platform);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700322 app_type_cfg[length++] = 48000;
323 app_type_cfg[length++] = 16;
324 mixer_ctl_set_array(ctl, app_type_cfg, length);
325 return;
326 }
327
328 app_type_cfg[length++] = num_app_types;
329 list_for_each(node, streams_output_cfg_list) {
330 so_info = node_to_item(node, struct streams_output_cfg, list);
331 update = true;
332 for (i=0; i<length; i=i+3) {
333 if (app_type_cfg[i+1] == -1)
334 break;
335 else if (app_type_cfg[i+1] == so_info->app_type_cfg.app_type) {
336 update = false;
337 break;
338 }
339 }
340 if (update && ((length + 3) <= MAX_LENGTH_MIXER_CONTROL_IN_INT)) {
341 num_app_types += 1 ;
342 app_type_cfg[length++] = so_info->app_type_cfg.app_type;
343 app_type_cfg[length++] = so_info->app_type_cfg.sample_rate;
344 app_type_cfg[length++] = so_info->app_type_cfg.bit_width;
345 }
346 }
347 ALOGV("%s: num_app_types: %d", __func__, num_app_types);
348 if (num_app_types) {
349 app_type_cfg[0] = num_app_types;
350 mixer_ctl_set_array(ctl, app_type_cfg, length);
351 }
352}
353
354void audio_extn_utils_update_streams_output_cfg_list(void *platform,
355 struct mixer *mixer,
356 struct listnode *streams_output_cfg_list)
357{
358 cnode *root;
359 char *data;
360
361 ALOGV("%s", __func__);
362 list_init(streams_output_cfg_list);
363 data = (char *)load_file(AUDIO_OUTPUT_POLICY_VENDOR_CONFIG_FILE, NULL);
364 if (data == NULL) {
365 send_app_type_cfg(platform, mixer, NULL);
366 ALOGE("%s: could not load output policy config file", __func__);
367 return;
368 }
369
370 root = config_node("", "");
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700371 if (root == NULL) {
372 ALOGE("cfg_list, NULL config root");
373 return;
374 }
375
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700376 config_load(root, data);
377 load_output(root, platform, streams_output_cfg_list);
378
379 send_app_type_cfg(platform, mixer, streams_output_cfg_list);
380}
381
382void audio_extn_utils_dump_streams_output_cfg_list(
383 struct listnode *streams_output_cfg_list)
384{
385 int i=0;
386 struct listnode *node_i, *node_j;
387 struct streams_output_cfg *so_info;
388 struct stream_format *sf_info;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700389 struct stream_sample_rate *ss_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700390 ALOGV("%s", __func__);
391 list_for_each(node_i, streams_output_cfg_list) {
392 so_info = node_to_item(node_i, struct streams_output_cfg, list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700393 ALOGV("%s: flags-%d, output_sample_rate-%d, output_bit_width-%d, app_type-%d",
394 __func__, so_info->flags, so_info->app_type_cfg.sample_rate,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700395 so_info->app_type_cfg.bit_width, so_info->app_type_cfg.app_type);
396 list_for_each(node_j, &so_info->format_list) {
397 sf_info = node_to_item(node_j, struct stream_format, list);
398 ALOGV("format-%x", sf_info->format);
399 }
Amit Shekhar6f461b12014-08-01 14:52:58 -0700400 list_for_each(node_j, &so_info->sample_rate_list) {
401 ss_info = node_to_item(node_j, struct stream_sample_rate, list);
402 ALOGV("sample rate-%d", ss_info->sample_rate);
403 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700404 }
405}
406
407void audio_extn_utils_release_streams_output_cfg_list(
408 struct listnode *streams_output_cfg_list)
409{
410 struct listnode *node_i, *node_j;
411 struct streams_output_cfg *so_info;
412 struct stream_format *sf_info;
413
414 ALOGV("%s", __func__);
415 while (!list_empty(streams_output_cfg_list)) {
416 node_i = list_head(streams_output_cfg_list);
417 so_info = node_to_item(node_i, struct streams_output_cfg, list);
418 while (!list_empty(&so_info->format_list)) {
419 node_j = list_head(&so_info->format_list);
420 list_remove(node_j);
421 free(node_to_item(node_j, struct stream_format, list));
422 }
Amit Shekhar6f461b12014-08-01 14:52:58 -0700423 while (!list_empty(&so_info->sample_rate_list)) {
424 node_j = list_head(&so_info->sample_rate_list);
425 list_remove(node_j);
426 free(node_to_item(node_j, struct stream_sample_rate, list));
427 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700428 list_remove(node_i);
429 free(node_to_item(node_i, struct streams_output_cfg, list));
430 }
431}
432
Amit Shekhar6f461b12014-08-01 14:52:58 -0700433static bool set_output_cfg(struct streams_output_cfg *so_info,
434 struct stream_app_type_cfg *app_type_cfg,
435 uint32_t sample_rate, uint32_t bit_width)
436 {
437 struct listnode *node_i;
438 struct stream_sample_rate *ss_info;
439 list_for_each(node_i, &so_info->sample_rate_list) {
440 ss_info = node_to_item(node_i, struct stream_sample_rate, list);
441 if ((sample_rate <= ss_info->sample_rate) &&
442 (bit_width == so_info->app_type_cfg.bit_width)) {
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700443
Amit Shekhar6f461b12014-08-01 14:52:58 -0700444 app_type_cfg->app_type = so_info->app_type_cfg.app_type;
445 app_type_cfg->sample_rate = ss_info->sample_rate;
446 app_type_cfg->bit_width = so_info->app_type_cfg.bit_width;
447 ALOGV("%s app_type_cfg->app_type %d, app_type_cfg->sample_rate %d, app_type_cfg->bit_width %d",
448 __func__, app_type_cfg->app_type, app_type_cfg->sample_rate, app_type_cfg->bit_width);
449 return true;
450 }
451 }
452 /*
453 * Reiterate through the list assuming dafault sample rate.
454 * Handles scenario where input sample rate is higher
455 * than all sample rates in list for the input bit width.
456 */
457 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
Preetam Singh Ranawat1871ddb2015-02-18 12:23:23 -0800458
Amit Shekhar6f461b12014-08-01 14:52:58 -0700459 list_for_each(node_i, &so_info->sample_rate_list) {
460 ss_info = node_to_item(node_i, struct stream_sample_rate, list);
461 if ((sample_rate <= ss_info->sample_rate) &&
462 (bit_width == so_info->app_type_cfg.bit_width)) {
463 app_type_cfg->app_type = so_info->app_type_cfg.app_type;
464 app_type_cfg->sample_rate = sample_rate;
465 app_type_cfg->bit_width = so_info->app_type_cfg.bit_width;
Preetam Singh Ranawat1871ddb2015-02-18 12:23:23 -0800466 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 -0700467 __func__, app_type_cfg->app_type, app_type_cfg->sample_rate, app_type_cfg->bit_width);
468 return true;
469 }
470 }
471 return false;
472}
473
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700474void audio_extn_utils_update_stream_app_type_cfg(void *platform,
475 struct listnode *streams_output_cfg_list,
Amit Shekhar1d896042014-10-03 13:16:09 -0700476 audio_devices_t devices,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700477 audio_output_flags_t flags,
478 audio_format_t format,
Amit Shekhar6f461b12014-08-01 14:52:58 -0700479 uint32_t sample_rate,
480 uint32_t bit_width,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700481 struct stream_app_type_cfg *app_type_cfg)
482{
Amit Shekhar6f461b12014-08-01 14:52:58 -0700483 struct listnode *node_i, *node_j, *node_k;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700484 struct streams_output_cfg *so_info;
485 struct stream_format *sf_info;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700486 struct stream_sample_rate *ss_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700487
Amit Shekhar1d896042014-10-03 13:16:09 -0700488 if ((24 == bit_width) &&
489 (devices & AUDIO_DEVICE_OUT_SPEAKER)) {
Amit Shekhar5a39c912014-10-14 15:39:30 -0700490 int32_t bw = platform_get_snd_device_bit_width(SND_DEVICE_OUT_SPEAKER);
491 if (-ENOSYS != bw)
492 bit_width = (uint32_t)bw;
Amit Shekhar1d896042014-10-03 13:16:09 -0700493 sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
494 ALOGI("%s Allowing 24-bit playback on speaker ONLY at default sampling rate", __func__);
495 }
496
497 ALOGV("%s: flags: %x, format: %x sample_rate %d",
498 __func__, flags, format, sample_rate);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700499 list_for_each(node_i, streams_output_cfg_list) {
500 so_info = node_to_item(node_i, struct streams_output_cfg, list);
501 if (so_info->flags == flags) {
502 list_for_each(node_j, &so_info->format_list) {
503 sf_info = node_to_item(node_j, struct stream_format, list);
504 if (sf_info->format == format) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700505 if (set_output_cfg(so_info, app_type_cfg, sample_rate, bit_width))
506 return;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700507 }
508 }
509 }
510 }
511 list_for_each(node_i, streams_output_cfg_list) {
512 so_info = node_to_item(node_i, struct streams_output_cfg, list);
513 if (so_info->flags == AUDIO_OUTPUT_FLAG_PRIMARY) {
514 ALOGV("Compatible output profile not found.");
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700515 app_type_cfg->app_type = so_info->app_type_cfg.app_type;
516 app_type_cfg->sample_rate = so_info->app_type_cfg.sample_rate;
517 app_type_cfg->bit_width = so_info->app_type_cfg.bit_width;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700518 ALOGV("%s Default to primary output: App type: %d sample_rate %d",
519 __func__, so_info->app_type_cfg.app_type, app_type_cfg->sample_rate);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700520 return;
521 }
522 }
523 ALOGW("%s: App type could not be selected. Falling back to default", __func__);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700524 app_type_cfg->app_type = platform_get_default_app_type(platform);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700525 app_type_cfg->sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700526 app_type_cfg->bit_width = 16;
527}
528
529int audio_extn_utils_send_app_type_cfg(struct audio_usecase *usecase)
530{
531 char mixer_ctl_name[MAX_LENGTH_MIXER_CONTROL_IN_INT];
532 int app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT], len = 0, rc;
Anish Kumarc8599f22014-07-20 09:36:07 -0700533 struct stream_out *out;
534 struct audio_device *adev;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700535 struct mixer_ctl *ctl;
536 int pcm_device_id, acdb_dev_id, snd_device = usecase->out_snd_device;
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +0530537 int32_t sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700538
539 ALOGV("%s", __func__);
540
541 if (usecase->type != PCM_PLAYBACK) {
542 ALOGV("%s: not a playback path, no need to cfg app type", __func__);
543 rc = 0;
544 goto exit_send_app_type_cfg;
545 }
546 if ((usecase->id != USECASE_AUDIO_PLAYBACK_DEEP_BUFFER) &&
547 (usecase->id != USECASE_AUDIO_PLAYBACK_LOW_LATENCY) &&
548 (usecase->id != USECASE_AUDIO_PLAYBACK_MULTI_CH) &&
Alexy Joseph2f89cfa2014-10-06 12:15:01 -0700549 (!is_offload_usecase(usecase->id))) {
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700550 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 -0700551 rc = 0;
552 goto exit_send_app_type_cfg;
553 }
Anish Kumarc8599f22014-07-20 09:36:07 -0700554 out = usecase->stream.out;
555 adev = out->dev;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700556
557 snd_device = usecase->out_snd_device;
558
559 pcm_device_id = platform_get_pcm_device_id(out->usecase, PCM_PLAYBACK);
560
561 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
562 "Audio Stream %d App Type Cfg", pcm_device_id);
563
564 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
565 if (!ctl) {
566 ALOGE("%s: Could not get ctl for mixer cmd - %s", __func__,
567 mixer_ctl_name);
568 rc = -EINVAL;
569 goto exit_send_app_type_cfg;
570 }
571 snd_device = (snd_device == SND_DEVICE_OUT_SPEAKER) ?
572 audio_extn_get_spkr_prot_snd_device(snd_device) : snd_device;
573 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
574 if (acdb_dev_id < 0) {
575 ALOGE("%s: Couldn't get the acdb dev id", __func__);
576 rc = -EINVAL;
577 goto exit_send_app_type_cfg;
578 }
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +0530579
580 if ((24 == usecase->stream.out->bit_width) &&
Amit Shekhar1d896042014-10-03 13:16:09 -0700581 (usecase->stream.out->devices & AUDIO_DEVICE_OUT_SPEAKER)) {
Dhananjay Kumarcb419212015-08-28 13:24:16 +0530582 out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Preetam Singh Ranawat36f3d372015-06-18 22:59:53 +0530583 } else if ((snd_device != SND_DEVICE_OUT_HEADPHONES_44_1 &&
584 usecase->stream.out->sample_rate == OUTPUT_SAMPLING_RATE_44100) ||
585 (usecase->stream.out->sample_rate < OUTPUT_SAMPLING_RATE_44100)) {
Dhananjay Kumarcb419212015-08-28 13:24:16 +0530586 out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +0530587 }
Dhananjay Kumarcb419212015-08-28 13:24:16 +0530588 sample_rate = out->app_type_cfg.sample_rate;
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +0530589
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700590 app_type_cfg[len++] = out->app_type_cfg.app_type;
591 app_type_cfg[len++] = acdb_dev_id;
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700592 if (((out->format == AUDIO_FORMAT_E_AC3) ||
593 (out->format == AUDIO_FORMAT_E_AC3_JOC)) &&
594 (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH))
595 app_type_cfg[len++] = sample_rate * 4;
596 else
597 app_type_cfg[len++] = sample_rate;
Preetam Singh Ranawat36f3d372015-06-18 22:59:53 +0530598
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700599 mixer_ctl_set_array(ctl, app_type_cfg, len);
Amit Shekhar278e3362014-09-08 14:08:19 -0700600 ALOGI("%s app_type %d, acdb_dev_id %d, sample_rate %d",
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +0530601 __func__, out->app_type_cfg.app_type, acdb_dev_id, sample_rate);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700602 rc = 0;
603exit_send_app_type_cfg:
604 return rc;
605}
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700606
Laxminath Kasam9e938362015-08-11 12:44:47 +0530607int read_line_from_file(const char *path, char *buf, size_t count)
608{
609 char * fgets_ret;
610 FILE * fd;
611 int rv;
612
613 fd = fopen(path, "r");
614 if (fd == NULL)
615 return -1;
616
617 fgets_ret = fgets(buf, (int)count, fd);
618 if (NULL != fgets_ret) {
619 rv = (int)strlen(buf);
620 } else {
621 rv = ferror(fd);
622 }
623 fclose(fd);
624
625 return rv;
626}
627
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700628void audio_extn_utils_send_audio_calibration(struct audio_device *adev,
629 struct audio_usecase *usecase)
630{
631 int type = usecase->type;
632
633 if (type == PCM_PLAYBACK) {
634 struct stream_out *out = usecase->stream.out;
635 int snd_device = usecase->out_snd_device;
636 snd_device = (snd_device == SND_DEVICE_OUT_SPEAKER) ?
637 audio_extn_get_spkr_prot_snd_device(snd_device) : snd_device;
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +0530638 platform_send_audio_calibration(adev->platform, usecase,
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700639 out->app_type_cfg.app_type,
640 out->app_type_cfg.sample_rate);
641 }
642 if ((type == PCM_HFP_CALL) || (type == PCM_CAPTURE)) {
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +0530643 /* when app type is default. the sample rate is not used to send cal */
644 platform_send_audio_calibration(adev->platform, usecase,
645 platform_get_default_app_type(adev->platform),
646 48000);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700647 }
648}
649
Ben Rombergera04fabc2014-11-14 12:16:03 -0800650// Base64 Encode and Decode
651// Not all features supported. This must be used only with following conditions.
652// Decode Modes: Support with and without padding
653// CRLF not handling. So no CRLF in string to decode.
654// Encode Modes: Supports only padding
655int b64decode(char *inp, int ilen, uint8_t* outp)
656{
657 int i, j, k, ii, num;
658 int rem, pcnt;
659 uint32_t res=0;
660 uint8_t getIndex[MAX_BASEINDEX_LEN];
661 uint8_t tmp, cflag;
662
663 if(inp == NULL || outp == NULL || ilen <= 0) {
664 ALOGE("[%s] received NULL pointer or zero length",__func__);
665 return -1;
666 }
667
668 memset(getIndex, MAX_BASEINDEX_LEN-1, sizeof(getIndex));
669 for(i=0;i<BASE_TABLE_SIZE;i++) {
670 getIndex[(uint8_t)bTable[i]] = (uint8_t)i;
671 }
672 getIndex[(uint8_t)'=']=0;
673
674 j=0;k=0;
675 num = ilen/4;
676 rem = ilen%4;
677 if(rem==0)
678 num = num-1;
679 cflag=0;
680 for(i=0; i<num; i++) {
681 res=0;
682 for(ii=0;ii<4;ii++) {
683 res = res << 6;
684 tmp = getIndex[(uint8_t)inp[j++]];
685 res = res | tmp;
686 cflag = cflag | tmp;
687 }
688 outp[k++] = (res >> 16)&0xFF;
689 outp[k++] = (res >> 8)&0xFF;
690 outp[k++] = res & 0xFF;
691 }
692
693 // Handle last bytes special
694 pcnt=0;
695 if(rem == 0) {
696 //With padding or full data
697 res = 0;
698 for(ii=0;ii<4;ii++) {
699 if(inp[j] == '=')
700 pcnt++;
701 res = res << 6;
702 tmp = getIndex[(uint8_t)inp[j++]];
703 res = res | tmp;
704 cflag = cflag | tmp;
705 }
706 outp[k++] = res >> 16;
707 if(pcnt == 2)
708 goto done;
709 outp[k++] = (res>>8)&0xFF;
710 if(pcnt == 1)
711 goto done;
712 outp[k++] = res&0xFF;
713 } else {
714 //without padding
715 res = 0;
716 for(i=0;i<rem;i++) {
717 res = res << 6;
718 tmp = getIndex[(uint8_t)inp[j++]];
719 res = res | tmp;
720 cflag = cflag | tmp;
721 }
722 for(i=rem;i<4;i++) {
723 res = res << 6;
724 pcnt++;
725 }
726 outp[k++] = res >> 16;
727 if(pcnt == 2)
728 goto done;
729 outp[k++] = (res>>8)&0xFF;
730 if(pcnt == 1)
731 goto done;
732 outp[k++] = res&0xFF;
733 }
734done:
735 if(cflag == 0xFF) {
736 ALOGE("[%s] base64 decode failed. Invalid character found %s",
737 __func__, inp);
738 return 0;
739 }
740 return k;
741}
742
743int b64encode(uint8_t *inp, int ilen, char* outp)
744{
745 int i,j,k, num;
746 int rem=0;
747 uint32_t res=0;
748
749 if(inp == NULL || outp == NULL || ilen<=0) {
750 ALOGE("[%s] received NULL pointer or zero input length",__func__);
751 return -1;
752 }
753
754 num = ilen/3;
755 rem = ilen%3;
756 j=0;k=0;
757 for(i=0; i<num; i++) {
758 //prepare index
759 res = inp[j++]<<16;
760 res = res | inp[j++]<<8;
761 res = res | inp[j++];
762 //get output map from index
763 outp[k++] = (char) bTable[(res>>18)&0x3F];
764 outp[k++] = (char) bTable[(res>>12)&0x3F];
765 outp[k++] = (char) bTable[(res>>6)&0x3F];
766 outp[k++] = (char) bTable[res&0x3F];
767 }
768
769 switch(rem) {
770 case 1:
771 res = inp[j++]<<16;
772 outp[k++] = (char) bTable[res>>18];
773 outp[k++] = (char) bTable[(res>>12)&0x3F];
774 //outp[k++] = '=';
775 //outp[k++] = '=';
776 break;
777 case 2:
778 res = inp[j++]<<16;
779 res = res | inp[j++]<<8;
780 outp[k++] = (char) bTable[res>>18];
781 outp[k++] = (char) bTable[(res>>12)&0x3F];
782 outp[k++] = (char) bTable[(res>>6)&0x3F];
783 //outp[k++] = '=';
784 break;
785 default:
786 break;
787 }
788done:
789 outp[k] = '\0';
790 return k;
791}
Ashish Jain5a97ddd2015-05-13 10:52:34 +0530792
793#ifdef AUDIO_EXTERNAL_HDMI_ENABLED
794
795void get_default_compressed_channel_status(
796 unsigned char *channel_status)
797{
798 int32_t status = 0;
799 unsigned char bit_index;
800 memset(channel_status,0,24);
801
802 /* block start bit in preamble bit 3 */
803 channel_status[0] |= PROFESSIONAL;
804 //compre out
805 channel_status[0] |= NON_LPCM;
806 // sample rate; fixed 48K for default/transcode
807 channel_status[3] |= SR_48000;
808}
809
810int32_t get_compressed_channel_status(void *audio_stream_data,
811 uint32_t audio_frame_size,
812 unsigned char *channel_status,
813 enum audio_parser_code_type codec_type)
814 // codec_type - AUDIO_PARSER_CODEC_AC3
815 // - AUDIO_PARSER_CODEC_DTS
816{
817 unsigned char *streamPtr;
818 int ret = 0;
819 streamPtr = (unsigned char *)audio_stream_data;
820
821 if (audio_stream_data == NULL || audio_frame_size == 0) {
822 ALOGW("no buffer to get channel status, return default for compress");
823 get_default_compressed_channel_status(channel_status);
824 return ret;
825 }
826
827 memset(channel_status,0,24);
828 if(init_audio_parser(streamPtr, audio_frame_size, codec_type) == -1)
829 {
830 ALOGE("init audio parser failed");
831 return -1;
832 }
833 ret = get_channel_status(channel_status, codec_type);
834 return ret;
835
836}
837
838void get_linearpcm_channel_status(uint32_t sampleRate,
839 unsigned char *channel_status)
840{
841 int32_t status = 0;
842 unsigned char bit_index;
843 memset(channel_status,0,24);
844 /* block start bit in preamble bit 3 */
845 channel_status[0] |= PROFESSIONAL;
846 //LPCM OUT
847 channel_status[0] &= ~NON_LPCM;
848
849 switch (sampleRate) {
850 case 8000:
851 case 11025:
852 case 12000:
853 case 16000:
854 case 22050:
855 channel_status[3] |= SR_NOTID;
856 case 24000:
857 channel_status[3] |= SR_24000;
858 break;
859 case 32000:
860 channel_status[3] |= SR_32000;
861 break;
862 case 44100:
863 channel_status[3] |= SR_44100;
864 break;
865 case 48000:
866 channel_status[3] |= SR_48000;
867 break;
868 case 88200:
869 channel_status[3] |= SR_88200;
870 break;
871 case 96000:
872 channel_status[3] |= SR_96000;
873 break;
874 case 176400:
875 channel_status[3] |= SR_176400;
876 break;
877 case 192000:
878 channel_status[3] |= SR_192000;
879 break;
880 default:
881 ALOGV("Invalid sample_rate %u\n", sampleRate);
882 status = -1;
883 break;
884 }
885}
886
887void setChannelStatus(struct stream_out *out, char * buffer, size_t bytes)
888{
889 unsigned char channel_status[24]={0};
890 struct snd_aes_iec958 iec958;
891 const char *mixer_ctl_name = "IEC958 Playback PCM Stream";
892 struct mixer_ctl *ctl;
893 int i=0;
894 if (audio_extn_is_dolby_format(out->format) &&
895 /*TODO:Extend code to support DTS passthrough*/
896 /*set compressed channel status bits*/
897 audio_extn_dolby_is_passthrough_stream(out->flags)){
898 get_compressed_channel_status(buffer, bytes, channel_status, AUDIO_PARSER_CODEC_AC3);
899 } else {
900 /*set channel status bit for LPCM*/
901 get_linearpcm_channel_status(out->sample_rate, channel_status);
902 }
903
904 memcpy(iec958.status, channel_status,sizeof(iec958.status));
905 ctl = mixer_get_ctl_by_name(out->dev->mixer, mixer_ctl_name);
906 if (!ctl) {
907 ALOGE("%s: Could not get ctl for mixer cmd - %s",
908 __func__, mixer_ctl_name);
909 return;
910 }
911 if (mixer_ctl_set_array(ctl, &iec958, sizeof(iec958)) < 0) {
912 ALOGE("%s: Could not set channel status for ext HDMI ",
913 __func__);
914 return;
915 }
916
917}
918#endif