blob: dac6c261b52a5a6f9489d04d025351ff5ec9c8a0 [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
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -070090 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070091};
92
93const struct string_to_enum s_format_name_to_enum_table[] = {
94 STRING_TO_ENUM(AUDIO_FORMAT_PCM_16_BIT),
95 STRING_TO_ENUM(AUDIO_FORMAT_PCM_8_BIT),
96 STRING_TO_ENUM(AUDIO_FORMAT_MP3),
97 STRING_TO_ENUM(AUDIO_FORMAT_AAC),
98 STRING_TO_ENUM(AUDIO_FORMAT_VORBIS),
Mingming Yinae3530f2014-07-03 16:50:18 -070099 STRING_TO_ENUM(AUDIO_FORMAT_AMR_NB),
100 STRING_TO_ENUM(AUDIO_FORMAT_AMR_WB),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700101 STRING_TO_ENUM(AUDIO_FORMAT_AC3),
Mingming Yinae3530f2014-07-03 16:50:18 -0700102 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3),
Satya Krishna Pindiproli4cda5352015-08-12 18:21:25 +0530103#ifdef AUDIO_EXTN_FORMATS_ENABLED
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700104 STRING_TO_ENUM(AUDIO_FORMAT_DTS),
105 STRING_TO_ENUM(AUDIO_FORMAT_DTS_LBR),
106 STRING_TO_ENUM(AUDIO_FORMAT_WMA),
107 STRING_TO_ENUM(AUDIO_FORMAT_WMA_PRO),
108 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADIF),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700109 STRING_TO_ENUM(AUDIO_FORMAT_AMR_WB_PLUS),
110 STRING_TO_ENUM(AUDIO_FORMAT_EVRC),
111 STRING_TO_ENUM(AUDIO_FORMAT_EVRCB),
112 STRING_TO_ENUM(AUDIO_FORMAT_EVRCWB),
113 STRING_TO_ENUM(AUDIO_FORMAT_QCELP),
114 STRING_TO_ENUM(AUDIO_FORMAT_MP2),
115 STRING_TO_ENUM(AUDIO_FORMAT_EVRCNW),
116 STRING_TO_ENUM(AUDIO_FORMAT_PCM_16_BIT_OFFLOAD),
117 STRING_TO_ENUM(AUDIO_FORMAT_PCM_24_BIT_OFFLOAD),
Amit Shekhar6f461b12014-08-01 14:52:58 -0700118 STRING_TO_ENUM(AUDIO_FORMAT_FLAC),
Satya Krishna Pindiproli70471602015-04-24 19:12:43 +0530119 STRING_TO_ENUM(AUDIO_FORMAT_ALAC),
120 STRING_TO_ENUM(AUDIO_FORMAT_APE),
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700121 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3_JOC),
Alexy Josephcd8eaed2014-12-11 12:46:53 -0800122 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LC),
123 STRING_TO_ENUM(AUDIO_FORMAT_AAC_HE_V1),
124 STRING_TO_ENUM(AUDIO_FORMAT_AAC_HE_V2),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700125#endif
126};
127
Ben Rombergera04fabc2014-11-14 12:16:03 -0800128static char bTable[BASE_TABLE_SIZE] = {
129 'A','B','C','D','E','F','G','H','I','J','K','L',
130 'M','N','O','P','Q','R','S','T','U','V','W','X',
131 'Y','Z','a','b','c','d','e','f','g','h','i','j',
132 'k','l','m','n','o','p','q','r','s','t','u','v',
133 'w','x','y','z','0','1','2','3','4','5','6','7',
134 '8','9','+','/'
135};
136
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700137static uint32_t string_to_enum(const struct string_to_enum *table, size_t size,
138 const char *name)
139{
140 size_t i;
141 for (i = 0; i < size; i++) {
142 if (strcmp(table[i].name, name) == 0) {
143 ALOGV("%s found %s", __func__, table[i].name);
144 return table[i].value;
145 }
146 }
147 return 0;
148}
149
150static audio_output_flags_t parse_flag_names(char *name)
151{
152 uint32_t flag = 0;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800153 char *last_r;
154 char *flag_name = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700155 while (flag_name != NULL) {
156 if (strlen(flag_name) != 0) {
157 flag |= string_to_enum(s_flag_name_to_enum_table,
158 ARRAY_SIZE(s_flag_name_to_enum_table),
159 flag_name);
160 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800161 flag_name = strtok_r(NULL, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700162 }
163
164 ALOGV("parse_flag_names: flag - %d", flag);
165 return (audio_output_flags_t)flag;
166}
167
168static void parse_format_names(char *name, struct streams_output_cfg *so_info)
169{
170 struct stream_format *sf_info = NULL;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800171 char *last_r;
172 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700173
174 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG) == 0)
175 return;
176
177 list_init(&so_info->format_list);
178 while (str != NULL) {
179 audio_format_t format = (audio_format_t)string_to_enum(s_format_name_to_enum_table,
180 ARRAY_SIZE(s_format_name_to_enum_table), str);
181 ALOGV("%s: format - %d", __func__, format);
182 if (format != 0) {
183 sf_info = (struct stream_format *)calloc(1, sizeof(struct stream_format));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700184 if (sf_info == NULL)
185 break; /* return whatever was parsed */
186
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700187 sf_info->format = format;
188 list_add_tail(&so_info->format_list, &sf_info->list);
189 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800190 str = strtok_r(NULL, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700191 }
192}
193
Amit Shekhar6f461b12014-08-01 14:52:58 -0700194static void parse_sample_rate_names(char *name, struct streams_output_cfg *so_info)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700195{
Amit Shekhar6f461b12014-08-01 14:52:58 -0700196 struct stream_sample_rate *ss_info = NULL;
197 uint32_t sample_rate = 48000;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800198 char *last_r;
199 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700200
Amit Shekhar6f461b12014-08-01 14:52:58 -0700201 if (str != NULL && 0 == strcmp(str, DYNAMIC_VALUE_TAG))
202 return;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700203
Amit Shekhar6f461b12014-08-01 14:52:58 -0700204 list_init(&so_info->sample_rate_list);
205 while (str != NULL) {
206 sample_rate = (uint32_t)strtol(str, (char **)NULL, 10);
207 ALOGV("%s: sample_rate - %d", __func__, sample_rate);
208 if (0 != sample_rate) {
209 ss_info = (struct stream_sample_rate *)calloc(1, sizeof(struct stream_sample_rate));
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800210 if (!ss_info) {
211 ALOGE("%s: memory allocation failure", __func__);
212 return;
213 }
Amit Shekhar6f461b12014-08-01 14:52:58 -0700214 ss_info->sample_rate = sample_rate;
215 list_add_tail(&so_info->sample_rate_list, &ss_info->list);
216 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800217 str = strtok_r(NULL, "|", &last_r);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700218 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700219}
220
221static int parse_bit_width_names(char *name)
222{
223 int bit_width = 16;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800224 char *last_r;
225 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700226
227 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG))
228 bit_width = (int)strtol(str, (char **)NULL, 10);
229
230 ALOGV("%s: bit_width - %d", __func__, bit_width);
231 return bit_width;
232}
233
234static int parse_app_type_names(void *platform, char *name)
235{
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700236 int app_type = platform_get_default_app_type(platform);
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800237 char *last_r;
238 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700239
240 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG))
241 app_type = (int)strtol(str, (char **)NULL, 10);
242
243 ALOGV("%s: app_type - %d", __func__, app_type);
244 return app_type;
245}
246
247static void update_streams_output_cfg_list(cnode *root, void *platform,
248 struct listnode *streams_output_cfg_list)
249{
250 cnode *node = root->first_child;
251 struct streams_output_cfg *so_info;
252
253 ALOGV("%s", __func__);
254 so_info = (struct streams_output_cfg *)calloc(1, sizeof(struct streams_output_cfg));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700255
256 if (!so_info) {
257 ALOGE("failed to allocate mem for so_info list element");
258 return;
259 }
260
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700261 while (node) {
262 if (strcmp(node->name, FLAGS_TAG) == 0) {
263 so_info->flags = parse_flag_names((char *)node->value);
264 } else if (strcmp(node->name, FORMATS_TAG) == 0) {
265 parse_format_names((char *)node->value, so_info);
266 } else if (strcmp(node->name, SAMPLING_RATES_TAG) == 0) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700267 so_info->app_type_cfg.sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
268 parse_sample_rate_names((char *)node->value, so_info);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700269 } else if (strcmp(node->name, BIT_WIDTH_TAG) == 0) {
270 so_info->app_type_cfg.bit_width = parse_bit_width_names((char *)node->value);
271 } else if (strcmp(node->name, APP_TYPE_TAG) == 0) {
272 so_info->app_type_cfg.app_type = parse_app_type_names(platform, (char *)node->value);
273 }
274 node = node->next;
275 }
276 list_add_tail(streams_output_cfg_list, &so_info->list);
277}
278
279static void load_output(cnode *root, void *platform,
280 struct listnode *streams_output_cfg_list)
281{
282 cnode *node = config_find(root, OUTPUTS_TAG);
283 if (node == NULL) {
284 ALOGE("%s: could not load output, node is NULL", __func__);
285 return;
286 }
287
288 node = node->first_child;
289 while (node) {
290 ALOGV("%s: loading output %s", __func__, node->name);
291 update_streams_output_cfg_list(node, platform, streams_output_cfg_list);
292 node = node->next;
293 }
294}
295
296static void send_app_type_cfg(void *platform, struct mixer *mixer,
297 struct listnode *streams_output_cfg_list)
298{
299 int app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT] = {-1};
300 int length = 0, i, num_app_types = 0;
301 struct listnode *node;
302 bool update;
303 struct mixer_ctl *ctl = NULL;
304 const char *mixer_ctl_name = "App Type Config";
305 struct streams_output_cfg *so_info;
306
307 if (!mixer) {
308 ALOGE("%s: mixer is null",__func__);
309 return;
310 }
311 ctl = mixer_get_ctl_by_name(mixer, mixer_ctl_name);
312 if (!ctl) {
313 ALOGE("%s: Could not get ctl for mixer cmd - %s",__func__, mixer_ctl_name);
314 return;
315 }
316 if (streams_output_cfg_list == NULL) {
317 app_type_cfg[length++] = 1;
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700318 app_type_cfg[length++] = platform_get_default_app_type(platform);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700319 app_type_cfg[length++] = 48000;
320 app_type_cfg[length++] = 16;
321 mixer_ctl_set_array(ctl, app_type_cfg, length);
322 return;
323 }
324
325 app_type_cfg[length++] = num_app_types;
326 list_for_each(node, streams_output_cfg_list) {
327 so_info = node_to_item(node, struct streams_output_cfg, list);
328 update = true;
329 for (i=0; i<length; i=i+3) {
330 if (app_type_cfg[i+1] == -1)
331 break;
332 else if (app_type_cfg[i+1] == so_info->app_type_cfg.app_type) {
333 update = false;
334 break;
335 }
336 }
337 if (update && ((length + 3) <= MAX_LENGTH_MIXER_CONTROL_IN_INT)) {
338 num_app_types += 1 ;
339 app_type_cfg[length++] = so_info->app_type_cfg.app_type;
340 app_type_cfg[length++] = so_info->app_type_cfg.sample_rate;
341 app_type_cfg[length++] = so_info->app_type_cfg.bit_width;
342 }
343 }
344 ALOGV("%s: num_app_types: %d", __func__, num_app_types);
345 if (num_app_types) {
346 app_type_cfg[0] = num_app_types;
347 mixer_ctl_set_array(ctl, app_type_cfg, length);
348 }
349}
350
351void audio_extn_utils_update_streams_output_cfg_list(void *platform,
352 struct mixer *mixer,
353 struct listnode *streams_output_cfg_list)
354{
355 cnode *root;
356 char *data;
357
358 ALOGV("%s", __func__);
359 list_init(streams_output_cfg_list);
360 data = (char *)load_file(AUDIO_OUTPUT_POLICY_VENDOR_CONFIG_FILE, NULL);
361 if (data == NULL) {
362 send_app_type_cfg(platform, mixer, NULL);
363 ALOGE("%s: could not load output policy config file", __func__);
364 return;
365 }
366
367 root = config_node("", "");
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700368 if (root == NULL) {
369 ALOGE("cfg_list, NULL config root");
370 return;
371 }
372
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700373 config_load(root, data);
374 load_output(root, platform, streams_output_cfg_list);
375
376 send_app_type_cfg(platform, mixer, streams_output_cfg_list);
377}
378
379void audio_extn_utils_dump_streams_output_cfg_list(
380 struct listnode *streams_output_cfg_list)
381{
382 int i=0;
383 struct listnode *node_i, *node_j;
384 struct streams_output_cfg *so_info;
385 struct stream_format *sf_info;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700386 struct stream_sample_rate *ss_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700387 ALOGV("%s", __func__);
388 list_for_each(node_i, streams_output_cfg_list) {
389 so_info = node_to_item(node_i, struct streams_output_cfg, list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700390 ALOGV("%s: flags-%d, output_sample_rate-%d, output_bit_width-%d, app_type-%d",
391 __func__, so_info->flags, so_info->app_type_cfg.sample_rate,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700392 so_info->app_type_cfg.bit_width, so_info->app_type_cfg.app_type);
393 list_for_each(node_j, &so_info->format_list) {
394 sf_info = node_to_item(node_j, struct stream_format, list);
395 ALOGV("format-%x", sf_info->format);
396 }
Amit Shekhar6f461b12014-08-01 14:52:58 -0700397 list_for_each(node_j, &so_info->sample_rate_list) {
398 ss_info = node_to_item(node_j, struct stream_sample_rate, list);
399 ALOGV("sample rate-%d", ss_info->sample_rate);
400 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700401 }
402}
403
404void audio_extn_utils_release_streams_output_cfg_list(
405 struct listnode *streams_output_cfg_list)
406{
407 struct listnode *node_i, *node_j;
408 struct streams_output_cfg *so_info;
409 struct stream_format *sf_info;
410
411 ALOGV("%s", __func__);
412 while (!list_empty(streams_output_cfg_list)) {
413 node_i = list_head(streams_output_cfg_list);
414 so_info = node_to_item(node_i, struct streams_output_cfg, list);
415 while (!list_empty(&so_info->format_list)) {
416 node_j = list_head(&so_info->format_list);
417 list_remove(node_j);
418 free(node_to_item(node_j, struct stream_format, list));
419 }
Amit Shekhar6f461b12014-08-01 14:52:58 -0700420 while (!list_empty(&so_info->sample_rate_list)) {
421 node_j = list_head(&so_info->sample_rate_list);
422 list_remove(node_j);
423 free(node_to_item(node_j, struct stream_sample_rate, list));
424 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700425 list_remove(node_i);
426 free(node_to_item(node_i, struct streams_output_cfg, list));
427 }
428}
429
Amit Shekhar6f461b12014-08-01 14:52:58 -0700430static bool set_output_cfg(struct streams_output_cfg *so_info,
431 struct stream_app_type_cfg *app_type_cfg,
432 uint32_t sample_rate, uint32_t bit_width)
433 {
434 struct listnode *node_i;
435 struct stream_sample_rate *ss_info;
436 list_for_each(node_i, &so_info->sample_rate_list) {
437 ss_info = node_to_item(node_i, struct stream_sample_rate, list);
438 if ((sample_rate <= ss_info->sample_rate) &&
439 (bit_width == so_info->app_type_cfg.bit_width)) {
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700440
Amit Shekhar6f461b12014-08-01 14:52:58 -0700441 app_type_cfg->app_type = so_info->app_type_cfg.app_type;
442 app_type_cfg->sample_rate = ss_info->sample_rate;
443 app_type_cfg->bit_width = so_info->app_type_cfg.bit_width;
444 ALOGV("%s app_type_cfg->app_type %d, app_type_cfg->sample_rate %d, app_type_cfg->bit_width %d",
445 __func__, app_type_cfg->app_type, app_type_cfg->sample_rate, app_type_cfg->bit_width);
446 return true;
447 }
448 }
449 /*
450 * Reiterate through the list assuming dafault sample rate.
451 * Handles scenario where input sample rate is higher
452 * than all sample rates in list for the input bit width.
453 */
454 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
Preetam Singh Ranawat1871ddb2015-02-18 12:23:23 -0800455
Amit Shekhar6f461b12014-08-01 14:52:58 -0700456 list_for_each(node_i, &so_info->sample_rate_list) {
457 ss_info = node_to_item(node_i, struct stream_sample_rate, list);
458 if ((sample_rate <= ss_info->sample_rate) &&
459 (bit_width == so_info->app_type_cfg.bit_width)) {
460 app_type_cfg->app_type = so_info->app_type_cfg.app_type;
461 app_type_cfg->sample_rate = sample_rate;
462 app_type_cfg->bit_width = so_info->app_type_cfg.bit_width;
Preetam Singh Ranawat1871ddb2015-02-18 12:23:23 -0800463 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 -0700464 __func__, app_type_cfg->app_type, app_type_cfg->sample_rate, app_type_cfg->bit_width);
465 return true;
466 }
467 }
468 return false;
469}
470
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700471void audio_extn_utils_update_stream_app_type_cfg(void *platform,
472 struct listnode *streams_output_cfg_list,
Amit Shekhar1d896042014-10-03 13:16:09 -0700473 audio_devices_t devices,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700474 audio_output_flags_t flags,
475 audio_format_t format,
Amit Shekhar6f461b12014-08-01 14:52:58 -0700476 uint32_t sample_rate,
477 uint32_t bit_width,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700478 struct stream_app_type_cfg *app_type_cfg)
479{
Amit Shekhar6f461b12014-08-01 14:52:58 -0700480 struct listnode *node_i, *node_j, *node_k;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700481 struct streams_output_cfg *so_info;
482 struct stream_format *sf_info;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700483 struct stream_sample_rate *ss_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700484
Amit Shekhar1d896042014-10-03 13:16:09 -0700485 if ((24 == bit_width) &&
486 (devices & AUDIO_DEVICE_OUT_SPEAKER)) {
Amit Shekhar5a39c912014-10-14 15:39:30 -0700487 int32_t bw = platform_get_snd_device_bit_width(SND_DEVICE_OUT_SPEAKER);
488 if (-ENOSYS != bw)
489 bit_width = (uint32_t)bw;
Amit Shekhar1d896042014-10-03 13:16:09 -0700490 sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
491 ALOGI("%s Allowing 24-bit playback on speaker ONLY at default sampling rate", __func__);
492 }
493
494 ALOGV("%s: flags: %x, format: %x sample_rate %d",
495 __func__, flags, format, sample_rate);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700496 list_for_each(node_i, streams_output_cfg_list) {
497 so_info = node_to_item(node_i, struct streams_output_cfg, list);
498 if (so_info->flags == flags) {
499 list_for_each(node_j, &so_info->format_list) {
500 sf_info = node_to_item(node_j, struct stream_format, list);
501 if (sf_info->format == format) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700502 if (set_output_cfg(so_info, app_type_cfg, sample_rate, bit_width))
503 return;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700504 }
505 }
506 }
507 }
508 list_for_each(node_i, streams_output_cfg_list) {
509 so_info = node_to_item(node_i, struct streams_output_cfg, list);
510 if (so_info->flags == AUDIO_OUTPUT_FLAG_PRIMARY) {
511 ALOGV("Compatible output profile not found.");
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700512 app_type_cfg->app_type = so_info->app_type_cfg.app_type;
513 app_type_cfg->sample_rate = so_info->app_type_cfg.sample_rate;
514 app_type_cfg->bit_width = so_info->app_type_cfg.bit_width;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700515 ALOGV("%s Default to primary output: App type: %d sample_rate %d",
516 __func__, so_info->app_type_cfg.app_type, app_type_cfg->sample_rate);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700517 return;
518 }
519 }
520 ALOGW("%s: App type could not be selected. Falling back to default", __func__);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700521 app_type_cfg->app_type = platform_get_default_app_type(platform);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700522 app_type_cfg->sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700523 app_type_cfg->bit_width = 16;
524}
525
526int audio_extn_utils_send_app_type_cfg(struct audio_usecase *usecase)
527{
528 char mixer_ctl_name[MAX_LENGTH_MIXER_CONTROL_IN_INT];
529 int app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT], len = 0, rc;
Anish Kumarc8599f22014-07-20 09:36:07 -0700530 struct stream_out *out;
531 struct audio_device *adev;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700532 struct mixer_ctl *ctl;
533 int pcm_device_id, acdb_dev_id, snd_device = usecase->out_snd_device;
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +0530534 int32_t sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700535
536 ALOGV("%s", __func__);
537
538 if (usecase->type != PCM_PLAYBACK) {
539 ALOGV("%s: not a playback path, no need to cfg app type", __func__);
540 rc = 0;
541 goto exit_send_app_type_cfg;
542 }
543 if ((usecase->id != USECASE_AUDIO_PLAYBACK_DEEP_BUFFER) &&
544 (usecase->id != USECASE_AUDIO_PLAYBACK_LOW_LATENCY) &&
545 (usecase->id != USECASE_AUDIO_PLAYBACK_MULTI_CH) &&
Alexy Joseph2f89cfa2014-10-06 12:15:01 -0700546 (!is_offload_usecase(usecase->id))) {
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700547 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 -0700548 rc = 0;
549 goto exit_send_app_type_cfg;
550 }
Anish Kumarc8599f22014-07-20 09:36:07 -0700551 out = usecase->stream.out;
552 adev = out->dev;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700553
554 snd_device = usecase->out_snd_device;
555
556 pcm_device_id = platform_get_pcm_device_id(out->usecase, PCM_PLAYBACK);
557
558 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
559 "Audio Stream %d App Type Cfg", pcm_device_id);
560
561 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
562 if (!ctl) {
563 ALOGE("%s: Could not get ctl for mixer cmd - %s", __func__,
564 mixer_ctl_name);
565 rc = -EINVAL;
566 goto exit_send_app_type_cfg;
567 }
568 snd_device = (snd_device == SND_DEVICE_OUT_SPEAKER) ?
569 audio_extn_get_spkr_prot_snd_device(snd_device) : snd_device;
570 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
571 if (acdb_dev_id < 0) {
572 ALOGE("%s: Couldn't get the acdb dev id", __func__);
573 rc = -EINVAL;
574 goto exit_send_app_type_cfg;
575 }
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +0530576
577 if ((24 == usecase->stream.out->bit_width) &&
Amit Shekhar1d896042014-10-03 13:16:09 -0700578 (usecase->stream.out->devices & AUDIO_DEVICE_OUT_SPEAKER)) {
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +0530579 sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Preetam Singh Ranawat36f3d372015-06-18 22:59:53 +0530580 } else if ((snd_device != SND_DEVICE_OUT_HEADPHONES_44_1 &&
581 usecase->stream.out->sample_rate == OUTPUT_SAMPLING_RATE_44100) ||
582 (usecase->stream.out->sample_rate < OUTPUT_SAMPLING_RATE_44100)) {
583 sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +0530584 } else {
585 sample_rate = out->app_type_cfg.sample_rate;
586 }
587
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700588 app_type_cfg[len++] = out->app_type_cfg.app_type;
589 app_type_cfg[len++] = acdb_dev_id;
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700590 if (((out->format == AUDIO_FORMAT_E_AC3) ||
591 (out->format == AUDIO_FORMAT_E_AC3_JOC)) &&
592 (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH))
593 app_type_cfg[len++] = sample_rate * 4;
594 else
595 app_type_cfg[len++] = sample_rate;
Preetam Singh Ranawat36f3d372015-06-18 22:59:53 +0530596
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700597 mixer_ctl_set_array(ctl, app_type_cfg, len);
Amit Shekhar278e3362014-09-08 14:08:19 -0700598 ALOGI("%s app_type %d, acdb_dev_id %d, sample_rate %d",
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +0530599 __func__, out->app_type_cfg.app_type, acdb_dev_id, sample_rate);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700600 rc = 0;
601exit_send_app_type_cfg:
602 return rc;
603}
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700604
Laxminath Kasam9e938362015-08-11 12:44:47 +0530605int read_line_from_file(const char *path, char *buf, size_t count)
606{
607 char * fgets_ret;
608 FILE * fd;
609 int rv;
610
611 fd = fopen(path, "r");
612 if (fd == NULL)
613 return -1;
614
615 fgets_ret = fgets(buf, (int)count, fd);
616 if (NULL != fgets_ret) {
617 rv = (int)strlen(buf);
618 } else {
619 rv = ferror(fd);
620 }
621 fclose(fd);
622
623 return rv;
624}
625
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700626void audio_extn_utils_send_audio_calibration(struct audio_device *adev,
627 struct audio_usecase *usecase)
628{
629 int type = usecase->type;
630
631 if (type == PCM_PLAYBACK) {
632 struct stream_out *out = usecase->stream.out;
633 int snd_device = usecase->out_snd_device;
634 snd_device = (snd_device == SND_DEVICE_OUT_SPEAKER) ?
635 audio_extn_get_spkr_prot_snd_device(snd_device) : snd_device;
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +0530636 platform_send_audio_calibration(adev->platform, usecase,
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700637 out->app_type_cfg.app_type,
638 out->app_type_cfg.sample_rate);
639 }
640 if ((type == PCM_HFP_CALL) || (type == PCM_CAPTURE)) {
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +0530641 /* when app type is default. the sample rate is not used to send cal */
642 platform_send_audio_calibration(adev->platform, usecase,
643 platform_get_default_app_type(adev->platform),
644 48000);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700645 }
646}
647
Ben Rombergera04fabc2014-11-14 12:16:03 -0800648// Base64 Encode and Decode
649// Not all features supported. This must be used only with following conditions.
650// Decode Modes: Support with and without padding
651// CRLF not handling. So no CRLF in string to decode.
652// Encode Modes: Supports only padding
653int b64decode(char *inp, int ilen, uint8_t* outp)
654{
655 int i, j, k, ii, num;
656 int rem, pcnt;
657 uint32_t res=0;
658 uint8_t getIndex[MAX_BASEINDEX_LEN];
659 uint8_t tmp, cflag;
660
661 if(inp == NULL || outp == NULL || ilen <= 0) {
662 ALOGE("[%s] received NULL pointer or zero length",__func__);
663 return -1;
664 }
665
666 memset(getIndex, MAX_BASEINDEX_LEN-1, sizeof(getIndex));
667 for(i=0;i<BASE_TABLE_SIZE;i++) {
668 getIndex[(uint8_t)bTable[i]] = (uint8_t)i;
669 }
670 getIndex[(uint8_t)'=']=0;
671
672 j=0;k=0;
673 num = ilen/4;
674 rem = ilen%4;
675 if(rem==0)
676 num = num-1;
677 cflag=0;
678 for(i=0; i<num; i++) {
679 res=0;
680 for(ii=0;ii<4;ii++) {
681 res = res << 6;
682 tmp = getIndex[(uint8_t)inp[j++]];
683 res = res | tmp;
684 cflag = cflag | tmp;
685 }
686 outp[k++] = (res >> 16)&0xFF;
687 outp[k++] = (res >> 8)&0xFF;
688 outp[k++] = res & 0xFF;
689 }
690
691 // Handle last bytes special
692 pcnt=0;
693 if(rem == 0) {
694 //With padding or full data
695 res = 0;
696 for(ii=0;ii<4;ii++) {
697 if(inp[j] == '=')
698 pcnt++;
699 res = res << 6;
700 tmp = getIndex[(uint8_t)inp[j++]];
701 res = res | tmp;
702 cflag = cflag | tmp;
703 }
704 outp[k++] = res >> 16;
705 if(pcnt == 2)
706 goto done;
707 outp[k++] = (res>>8)&0xFF;
708 if(pcnt == 1)
709 goto done;
710 outp[k++] = res&0xFF;
711 } else {
712 //without padding
713 res = 0;
714 for(i=0;i<rem;i++) {
715 res = res << 6;
716 tmp = getIndex[(uint8_t)inp[j++]];
717 res = res | tmp;
718 cflag = cflag | tmp;
719 }
720 for(i=rem;i<4;i++) {
721 res = res << 6;
722 pcnt++;
723 }
724 outp[k++] = res >> 16;
725 if(pcnt == 2)
726 goto done;
727 outp[k++] = (res>>8)&0xFF;
728 if(pcnt == 1)
729 goto done;
730 outp[k++] = res&0xFF;
731 }
732done:
733 if(cflag == 0xFF) {
734 ALOGE("[%s] base64 decode failed. Invalid character found %s",
735 __func__, inp);
736 return 0;
737 }
738 return k;
739}
740
741int b64encode(uint8_t *inp, int ilen, char* outp)
742{
743 int i,j,k, num;
744 int rem=0;
745 uint32_t res=0;
746
747 if(inp == NULL || outp == NULL || ilen<=0) {
748 ALOGE("[%s] received NULL pointer or zero input length",__func__);
749 return -1;
750 }
751
752 num = ilen/3;
753 rem = ilen%3;
754 j=0;k=0;
755 for(i=0; i<num; i++) {
756 //prepare index
757 res = inp[j++]<<16;
758 res = res | inp[j++]<<8;
759 res = res | inp[j++];
760 //get output map from index
761 outp[k++] = (char) bTable[(res>>18)&0x3F];
762 outp[k++] = (char) bTable[(res>>12)&0x3F];
763 outp[k++] = (char) bTable[(res>>6)&0x3F];
764 outp[k++] = (char) bTable[res&0x3F];
765 }
766
767 switch(rem) {
768 case 1:
769 res = inp[j++]<<16;
770 outp[k++] = (char) bTable[res>>18];
771 outp[k++] = (char) bTable[(res>>12)&0x3F];
772 //outp[k++] = '=';
773 //outp[k++] = '=';
774 break;
775 case 2:
776 res = inp[j++]<<16;
777 res = res | inp[j++]<<8;
778 outp[k++] = (char) bTable[res>>18];
779 outp[k++] = (char) bTable[(res>>12)&0x3F];
780 outp[k++] = (char) bTable[(res>>6)&0x3F];
781 //outp[k++] = '=';
782 break;
783 default:
784 break;
785 }
786done:
787 outp[k] = '\0';
788 return k;
789}
Ashish Jain5a97ddd2015-05-13 10:52:34 +0530790
791#ifdef AUDIO_EXTERNAL_HDMI_ENABLED
792
793void get_default_compressed_channel_status(
794 unsigned char *channel_status)
795{
796 int32_t status = 0;
797 unsigned char bit_index;
798 memset(channel_status,0,24);
799
800 /* block start bit in preamble bit 3 */
801 channel_status[0] |= PROFESSIONAL;
802 //compre out
803 channel_status[0] |= NON_LPCM;
804 // sample rate; fixed 48K for default/transcode
805 channel_status[3] |= SR_48000;
806}
807
808int32_t get_compressed_channel_status(void *audio_stream_data,
809 uint32_t audio_frame_size,
810 unsigned char *channel_status,
811 enum audio_parser_code_type codec_type)
812 // codec_type - AUDIO_PARSER_CODEC_AC3
813 // - AUDIO_PARSER_CODEC_DTS
814{
815 unsigned char *streamPtr;
816 int ret = 0;
817 streamPtr = (unsigned char *)audio_stream_data;
818
819 if (audio_stream_data == NULL || audio_frame_size == 0) {
820 ALOGW("no buffer to get channel status, return default for compress");
821 get_default_compressed_channel_status(channel_status);
822 return ret;
823 }
824
825 memset(channel_status,0,24);
826 if(init_audio_parser(streamPtr, audio_frame_size, codec_type) == -1)
827 {
828 ALOGE("init audio parser failed");
829 return -1;
830 }
831 ret = get_channel_status(channel_status, codec_type);
832 return ret;
833
834}
835
836void get_linearpcm_channel_status(uint32_t sampleRate,
837 unsigned char *channel_status)
838{
839 int32_t status = 0;
840 unsigned char bit_index;
841 memset(channel_status,0,24);
842 /* block start bit in preamble bit 3 */
843 channel_status[0] |= PROFESSIONAL;
844 //LPCM OUT
845 channel_status[0] &= ~NON_LPCM;
846
847 switch (sampleRate) {
848 case 8000:
849 case 11025:
850 case 12000:
851 case 16000:
852 case 22050:
853 channel_status[3] |= SR_NOTID;
854 case 24000:
855 channel_status[3] |= SR_24000;
856 break;
857 case 32000:
858 channel_status[3] |= SR_32000;
859 break;
860 case 44100:
861 channel_status[3] |= SR_44100;
862 break;
863 case 48000:
864 channel_status[3] |= SR_48000;
865 break;
866 case 88200:
867 channel_status[3] |= SR_88200;
868 break;
869 case 96000:
870 channel_status[3] |= SR_96000;
871 break;
872 case 176400:
873 channel_status[3] |= SR_176400;
874 break;
875 case 192000:
876 channel_status[3] |= SR_192000;
877 break;
878 default:
879 ALOGV("Invalid sample_rate %u\n", sampleRate);
880 status = -1;
881 break;
882 }
883}
884
885void setChannelStatus(struct stream_out *out, char * buffer, size_t bytes)
886{
887 unsigned char channel_status[24]={0};
888 struct snd_aes_iec958 iec958;
889 const char *mixer_ctl_name = "IEC958 Playback PCM Stream";
890 struct mixer_ctl *ctl;
891 int i=0;
892 if (audio_extn_is_dolby_format(out->format) &&
893 /*TODO:Extend code to support DTS passthrough*/
894 /*set compressed channel status bits*/
895 audio_extn_dolby_is_passthrough_stream(out->flags)){
896 get_compressed_channel_status(buffer, bytes, channel_status, AUDIO_PARSER_CODEC_AC3);
897 } else {
898 /*set channel status bit for LPCM*/
899 get_linearpcm_channel_status(out->sample_rate, channel_status);
900 }
901
902 memcpy(iec958.status, channel_status,sizeof(iec958.status));
903 ctl = mixer_get_ctl_by_name(out->dev->mixer, mixer_ctl_name);
904 if (!ctl) {
905 ALOGE("%s: Could not get ctl for mixer cmd - %s",
906 __func__, mixer_ctl_name);
907 return;
908 }
909 if (mixer_ctl_set_array(ctl, &iec958, sizeof(iec958)) < 0) {
910 ALOGE("%s: Could not set channel status for ext HDMI ",
911 __func__);
912 return;
913 }
914
915}
916#endif