blob: d4ffadd3a773cf5a2929288f768c6761b763b613 [file] [log] [blame]
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001/*
Alexy Josephaee4fdd2016-01-29 13:02:07 -08002 * Copyright (c) 2014-2016, 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
Ashish Jain81eb2a82015-05-13 10:52:34 +053038#ifdef AUDIO_EXTERNAL_HDMI_ENABLED
39#ifdef HDMI_PASSTHROUGH_ENABLED
40#include "audio_parsers.h"
41#endif
42#endif
43
Yamit Mehtaa0d653a2016-11-25 20:33:25 +053044#ifdef LINUX_ENABLED
45#define AUDIO_OUTPUT_POLICY_VENDOR_CONFIG_FILE "/etc/audio_output_policy.conf"
Dhananjay Kumard6d32152016-10-13 16:11:03 +053046#define AUDIO_IO_POLICY_VENDOR_CONFIG_FILE "/etc/audio_io_policy.conf"
Yamit Mehtaa0d653a2016-11-25 20:33:25 +053047#else
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070048#define AUDIO_OUTPUT_POLICY_VENDOR_CONFIG_FILE "/vendor/etc/audio_output_policy.conf"
Dhananjay Kumard6d32152016-10-13 16:11:03 +053049#define AUDIO_IO_POLICY_VENDOR_CONFIG_FILE "/vendor/etc/audio_io_policy.conf"
Yamit Mehtaa0d653a2016-11-25 20:33:25 +053050#endif
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070051
52#define OUTPUTS_TAG "outputs"
Dhananjay Kumard6d32152016-10-13 16:11:03 +053053#define INPUTS_TAG "inputs"
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070054
55#define DYNAMIC_VALUE_TAG "dynamic"
56#define FLAGS_TAG "flags"
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +053057#define PROFILES_TAG "profile"
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070058#define FORMATS_TAG "formats"
59#define SAMPLING_RATES_TAG "sampling_rates"
60#define BIT_WIDTH_TAG "bit_width"
61#define APP_TYPE_TAG "app_type"
62
63#define STRING_TO_ENUM(string) { #string, string }
64#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
65
Ben Rombergera04fabc2014-11-14 12:16:03 -080066#define BASE_TABLE_SIZE 64
67#define MAX_BASEINDEX_LEN 256
68
Ashish Jain81eb2a82015-05-13 10:52:34 +053069#ifdef AUDIO_EXTERNAL_HDMI_ENABLED
70#define PROFESSIONAL (1<<0) /* 0 = consumer, 1 = professional */
71#define NON_LPCM (1<<1) /* 0 = audio, 1 = non-audio */
72#define SR_44100 (0<<0) /* 44.1kHz */
73#define SR_NOTID (1<<0) /* non indicated */
74#define SR_48000 (2<<0) /* 48kHz */
75#define SR_32000 (3<<0) /* 32kHz */
76#define SR_22050 (4<<0) /* 22.05kHz */
77#define SR_24000 (6<<0) /* 24kHz */
78#define SR_88200 (8<<0) /* 88.2kHz */
79#define SR_96000 (10<<0) /* 96kHz */
80#define SR_176400 (12<<0) /* 176.4kHz */
81#define SR_192000 (14<<0) /* 192kHz */
82
83#endif
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070084struct string_to_enum {
85 const char *name;
86 uint32_t value;
87};
88
89const struct string_to_enum s_flag_name_to_enum_table[] = {
90 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_DIRECT),
vivek mehta0ea887a2015-08-26 14:01:20 -070091 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_DIRECT_PCM),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070092 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_PRIMARY),
93 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_FAST),
94 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_DEEP_BUFFER),
95 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD),
96 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_NON_BLOCKING),
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -070097 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_HW_AV_SYNC),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070098#ifdef INCALL_MUSIC_ENABLED
99 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_INCALL_MUSIC),
100#endif
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700101 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH),
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530102 STRING_TO_ENUM(AUDIO_INPUT_FLAG_NONE),
103 STRING_TO_ENUM(AUDIO_INPUT_FLAG_FAST),
104 STRING_TO_ENUM(AUDIO_INPUT_FLAG_HW_HOTWORD),
105 STRING_TO_ENUM(AUDIO_INPUT_FLAG_RAW),
106 STRING_TO_ENUM(AUDIO_INPUT_FLAG_SYNC),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700107};
108
109const struct string_to_enum s_format_name_to_enum_table[] = {
Ashish Jain83a6cc22016-06-28 14:34:17 +0530110 STRING_TO_ENUM(AUDIO_FORMAT_PCM_8_BIT),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700111 STRING_TO_ENUM(AUDIO_FORMAT_PCM_16_BIT),
Ashish Jain5106d362016-05-11 19:23:33 +0530112 STRING_TO_ENUM(AUDIO_FORMAT_PCM_24_BIT_PACKED),
113 STRING_TO_ENUM(AUDIO_FORMAT_PCM_8_24_BIT),
Ashish Jainf1eaa582016-05-23 20:54:24 +0530114 STRING_TO_ENUM(AUDIO_FORMAT_PCM_32_BIT),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700115 STRING_TO_ENUM(AUDIO_FORMAT_MP3),
116 STRING_TO_ENUM(AUDIO_FORMAT_AAC),
117 STRING_TO_ENUM(AUDIO_FORMAT_VORBIS),
Mingming Yinae3530f2014-07-03 16:50:18 -0700118 STRING_TO_ENUM(AUDIO_FORMAT_AMR_NB),
119 STRING_TO_ENUM(AUDIO_FORMAT_AMR_WB),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700120 STRING_TO_ENUM(AUDIO_FORMAT_AC3),
Mingming Yinae3530f2014-07-03 16:50:18 -0700121 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700122 STRING_TO_ENUM(AUDIO_FORMAT_DTS),
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530123 STRING_TO_ENUM(AUDIO_FORMAT_DTS_HD),
124#ifdef AUDIO_EXTN_FORMATS_ENABLED
125 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3_JOC),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700126 STRING_TO_ENUM(AUDIO_FORMAT_WMA),
127 STRING_TO_ENUM(AUDIO_FORMAT_WMA_PRO),
128 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADIF),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700129 STRING_TO_ENUM(AUDIO_FORMAT_AMR_WB_PLUS),
130 STRING_TO_ENUM(AUDIO_FORMAT_EVRC),
131 STRING_TO_ENUM(AUDIO_FORMAT_EVRCB),
132 STRING_TO_ENUM(AUDIO_FORMAT_EVRCWB),
133 STRING_TO_ENUM(AUDIO_FORMAT_QCELP),
134 STRING_TO_ENUM(AUDIO_FORMAT_MP2),
135 STRING_TO_ENUM(AUDIO_FORMAT_EVRCNW),
Amit Shekhar6f461b12014-08-01 14:52:58 -0700136 STRING_TO_ENUM(AUDIO_FORMAT_FLAC),
Satya Krishna Pindiproli70471602015-04-24 19:12:43 +0530137 STRING_TO_ENUM(AUDIO_FORMAT_ALAC),
138 STRING_TO_ENUM(AUDIO_FORMAT_APE),
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700139 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3_JOC),
Alexy Josephcd8eaed2014-12-11 12:46:53 -0800140 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LC),
141 STRING_TO_ENUM(AUDIO_FORMAT_AAC_HE_V1),
142 STRING_TO_ENUM(AUDIO_FORMAT_AAC_HE_V2),
Manish Dewangana6fc5442015-08-24 20:30:31 +0530143 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS),
Ashish Jaine513a872015-11-19 17:00:56 +0530144 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_LC),
145 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_HE_V1),
146 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_HE_V2),
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +0530147 STRING_TO_ENUM(AUDIO_FORMAT_DSD),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700148#endif
149};
150
Ben Rombergera04fabc2014-11-14 12:16:03 -0800151static char bTable[BASE_TABLE_SIZE] = {
152 'A','B','C','D','E','F','G','H','I','J','K','L',
153 'M','N','O','P','Q','R','S','T','U','V','W','X',
154 'Y','Z','a','b','c','d','e','f','g','h','i','j',
155 'k','l','m','n','o','p','q','r','s','t','u','v',
156 'w','x','y','z','0','1','2','3','4','5','6','7',
157 '8','9','+','/'
158};
159
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700160static uint32_t string_to_enum(const struct string_to_enum *table, size_t size,
161 const char *name)
162{
163 size_t i;
164 for (i = 0; i < size; i++) {
165 if (strcmp(table[i].name, name) == 0) {
166 ALOGV("%s found %s", __func__, table[i].name);
167 return table[i].value;
168 }
169 }
170 return 0;
171}
172
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530173static audio_io_flags_t parse_flag_names(char *name)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700174{
175 uint32_t flag = 0;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530176 audio_io_flags_t io_flags;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800177 char *last_r;
178 char *flag_name = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700179 while (flag_name != NULL) {
180 if (strlen(flag_name) != 0) {
181 flag |= string_to_enum(s_flag_name_to_enum_table,
182 ARRAY_SIZE(s_flag_name_to_enum_table),
183 flag_name);
184 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800185 flag_name = strtok_r(NULL, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700186 }
187
188 ALOGV("parse_flag_names: flag - %d", flag);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530189 io_flags.in_flags = (audio_input_flags_t)flag;
190 io_flags.out_flags = (audio_output_flags_t)flag;
191 return io_flags;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700192}
193
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530194static void parse_format_names(char *name, struct streams_io_cfg *s_info)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700195{
196 struct stream_format *sf_info = NULL;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800197 char *last_r;
198 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700199
200 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG) == 0)
201 return;
202
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530203 list_init(&s_info->format_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700204 while (str != NULL) {
205 audio_format_t format = (audio_format_t)string_to_enum(s_format_name_to_enum_table,
206 ARRAY_SIZE(s_format_name_to_enum_table), str);
207 ALOGV("%s: format - %d", __func__, format);
208 if (format != 0) {
209 sf_info = (struct stream_format *)calloc(1, sizeof(struct stream_format));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700210 if (sf_info == NULL)
211 break; /* return whatever was parsed */
212
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700213 sf_info->format = format;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530214 list_add_tail(&s_info->format_list, &sf_info->list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700215 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800216 str = strtok_r(NULL, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700217 }
218}
219
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530220static void parse_sample_rate_names(char *name, struct streams_io_cfg *s_info)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700221{
Amit Shekhar6f461b12014-08-01 14:52:58 -0700222 struct stream_sample_rate *ss_info = NULL;
223 uint32_t sample_rate = 48000;
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
Amit Shekhar6f461b12014-08-01 14:52:58 -0700227 if (str != NULL && 0 == strcmp(str, DYNAMIC_VALUE_TAG))
228 return;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700229
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530230 list_init(&s_info->sample_rate_list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700231 while (str != NULL) {
232 sample_rate = (uint32_t)strtol(str, (char **)NULL, 10);
233 ALOGV("%s: sample_rate - %d", __func__, sample_rate);
234 if (0 != sample_rate) {
235 ss_info = (struct stream_sample_rate *)calloc(1, sizeof(struct stream_sample_rate));
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800236 if (!ss_info) {
237 ALOGE("%s: memory allocation failure", __func__);
238 return;
239 }
Amit Shekhar6f461b12014-08-01 14:52:58 -0700240 ss_info->sample_rate = sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530241 list_add_tail(&s_info->sample_rate_list, &ss_info->list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700242 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800243 str = strtok_r(NULL, "|", &last_r);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700244 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700245}
246
247static int parse_bit_width_names(char *name)
248{
249 int bit_width = 16;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800250 char *last_r;
251 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700252
253 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG))
254 bit_width = (int)strtol(str, (char **)NULL, 10);
255
256 ALOGV("%s: bit_width - %d", __func__, bit_width);
257 return bit_width;
258}
259
260static int parse_app_type_names(void *platform, char *name)
261{
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700262 int app_type = platform_get_default_app_type(platform);
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800263 char *last_r;
264 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700265
266 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG))
267 app_type = (int)strtol(str, (char **)NULL, 10);
268
269 ALOGV("%s: app_type - %d", __func__, app_type);
270 return app_type;
271}
272
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530273static void update_streams_cfg_list(cnode *root, void *platform,
274 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700275{
276 cnode *node = root->first_child;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530277 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700278
279 ALOGV("%s", __func__);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530280 s_info = (struct streams_io_cfg *)calloc(1, sizeof(struct streams_io_cfg));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700281
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530282 if (!s_info) {
283 ALOGE("failed to allocate mem for s_info list element");
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700284 return;
285 }
286
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700287 while (node) {
288 if (strcmp(node->name, FLAGS_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530289 s_info->flags = parse_flag_names((char *)node->value);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530290 } else if (strcmp(node->name, PROFILES_TAG) == 0) {
291 strlcpy(s_info->profile, (char *)node->value, sizeof(s_info->profile));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700292 } else if (strcmp(node->name, FORMATS_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530293 parse_format_names((char *)node->value, s_info);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700294 } else if (strcmp(node->name, SAMPLING_RATES_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530295 s_info->app_type_cfg.sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
296 parse_sample_rate_names((char *)node->value, s_info);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700297 } else if (strcmp(node->name, BIT_WIDTH_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530298 s_info->app_type_cfg.bit_width = parse_bit_width_names((char *)node->value);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700299 } else if (strcmp(node->name, APP_TYPE_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530300 s_info->app_type_cfg.app_type = parse_app_type_names(platform, (char *)node->value);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700301 }
302 node = node->next;
303 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530304 list_add_tail(streams_cfg_list, &s_info->list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700305}
306
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530307static void load_cfg_list(cnode *root, void *platform,
308 struct listnode *streams_output_cfg_list,
309 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700310{
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530311 cnode *node = NULL;
312
313 node = config_find(root, OUTPUTS_TAG);
314 if (node != NULL) {
315 node = node->first_child;
316 while (node) {
317 ALOGV("%s: loading output %s", __func__, node->name);
318 update_streams_cfg_list(node, platform, streams_output_cfg_list);
319 node = node->next;
320 }
321 } else {
322 ALOGI("%s: could not load output, node is NULL", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700323 }
324
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530325 node = config_find(root, INPUTS_TAG);
326 if (node != NULL) {
327 node = node->first_child;
328 while (node) {
329 ALOGV("%s: loading input %s", __func__, node->name);
330 update_streams_cfg_list(node, platform, streams_input_cfg_list);
331 node = node->next;
332 }
333 } else {
334 ALOGI("%s: could not load input, node is NULL", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700335 }
336}
337
338static void send_app_type_cfg(void *platform, struct mixer *mixer,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530339 struct listnode *streams_output_cfg_list,
340 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700341{
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530342 size_t app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT] = {0};
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700343 int length = 0, i, num_app_types = 0;
344 struct listnode *node;
345 bool update;
346 struct mixer_ctl *ctl = NULL;
347 const char *mixer_ctl_name = "App Type Config";
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530348 struct streams_io_cfg *s_info = NULL;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700349
350 if (!mixer) {
351 ALOGE("%s: mixer is null",__func__);
352 return;
353 }
354 ctl = mixer_get_ctl_by_name(mixer, mixer_ctl_name);
355 if (!ctl) {
356 ALOGE("%s: Could not get ctl for mixer cmd - %s",__func__, mixer_ctl_name);
357 return;
358 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530359 app_type_cfg[length++] = num_app_types;
360
361 if (list_empty(streams_output_cfg_list)) {
362 app_type_cfg[length++] = platform_get_default_app_type_v2(platform, PCM_PLAYBACK);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700363 app_type_cfg[length++] = 48000;
364 app_type_cfg[length++] = 16;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530365 num_app_types += 1;
366 }
367 if (list_empty(streams_input_cfg_list)) {
368 app_type_cfg[length++] = platform_get_default_app_type_v2(platform, PCM_CAPTURE);
369 app_type_cfg[length++] = 48000;
370 app_type_cfg[length++] = 16;
371 num_app_types += 1;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700372 }
373
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700374 list_for_each(node, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530375 s_info = node_to_item(node, struct streams_io_cfg, list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700376 update = true;
377 for (i=0; i<length; i=i+3) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530378 if (app_type_cfg[i+1] == 0)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700379 break;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530380 else if (app_type_cfg[i+1] == (size_t)s_info->app_type_cfg.app_type) {
381 if (app_type_cfg[i+2] < s_info->app_type_cfg.sample_rate)
382 app_type_cfg[i+2] = s_info->app_type_cfg.sample_rate;
383 if (app_type_cfg[i+3] < s_info->app_type_cfg.bit_width)
384 app_type_cfg[i+3] = s_info->app_type_cfg.bit_width;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700385 update = false;
386 break;
387 }
388 }
389 if (update && ((length + 3) <= MAX_LENGTH_MIXER_CONTROL_IN_INT)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530390 num_app_types += 1;
391 app_type_cfg[length++] = s_info->app_type_cfg.app_type;
392 app_type_cfg[length++] = s_info->app_type_cfg.sample_rate;
393 app_type_cfg[length++] = s_info->app_type_cfg.bit_width;
394 }
395 }
396 list_for_each(node, streams_input_cfg_list) {
397 s_info = node_to_item(node, struct streams_io_cfg, list);
398 update = true;
399 for (i=0; i<length; i=i+3) {
400 if (app_type_cfg[i+1] == 0)
401 break;
402 else if (app_type_cfg[i+1] == (size_t)s_info->app_type_cfg.app_type) {
403 if (app_type_cfg[i+2] < s_info->app_type_cfg.sample_rate)
404 app_type_cfg[i+2] = s_info->app_type_cfg.sample_rate;
405 if (app_type_cfg[i+3] < s_info->app_type_cfg.bit_width)
406 app_type_cfg[i+3] = s_info->app_type_cfg.bit_width;
407 update = false;
408 break;
409 }
410 }
411 if (update && ((length + 3) <= MAX_LENGTH_MIXER_CONTROL_IN_INT)) {
412 num_app_types += 1;
413 app_type_cfg[length++] = s_info->app_type_cfg.app_type;
414 app_type_cfg[length++] = s_info->app_type_cfg.sample_rate;
415 app_type_cfg[length++] = s_info->app_type_cfg.bit_width;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700416 }
417 }
418 ALOGV("%s: num_app_types: %d", __func__, num_app_types);
419 if (num_app_types) {
420 app_type_cfg[0] = num_app_types;
421 mixer_ctl_set_array(ctl, app_type_cfg, length);
422 }
423}
424
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530425void audio_extn_utils_update_streams_cfg_lists(void *platform,
426 struct mixer *mixer,
427 struct listnode *streams_output_cfg_list,
428 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700429{
430 cnode *root;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530431 char *data = NULL;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700432
433 ALOGV("%s", __func__);
434 list_init(streams_output_cfg_list);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530435 list_init(streams_input_cfg_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700436
437 root = config_node("", "");
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700438 if (root == NULL) {
439 ALOGE("cfg_list, NULL config root");
440 return;
441 }
442
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530443 data = (char *)load_file(AUDIO_IO_POLICY_VENDOR_CONFIG_FILE, NULL);
444 if (data == NULL) {
445 ALOGD("%s: failed to open io config file(%s), trying older config file",
446 __func__, AUDIO_IO_POLICY_VENDOR_CONFIG_FILE);
447 data = (char *)load_file(AUDIO_OUTPUT_POLICY_VENDOR_CONFIG_FILE, NULL);
448 if (data == NULL) {
449 send_app_type_cfg(platform, mixer,
450 streams_output_cfg_list,
451 streams_input_cfg_list);
452 ALOGE("%s: could not load io policy config!", __func__);
453 return;
454 }
455 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700456
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530457 config_load(root, data);
458 load_cfg_list(root, platform, streams_output_cfg_list,
459 streams_input_cfg_list);
460
461 send_app_type_cfg(platform, mixer, streams_output_cfg_list,
462 streams_input_cfg_list);
Alexy Josephaee4fdd2016-01-29 13:02:07 -0800463
464 config_free(root);
465 free(data);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700466}
467
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530468static void audio_extn_utils_dump_streams_cfg_list(
469 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700470{
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700471 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530472 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700473 struct stream_format *sf_info;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700474 struct stream_sample_rate *ss_info;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530475
476 list_for_each(node_i, streams_cfg_list) {
477 s_info = node_to_item(node_i, struct streams_io_cfg, list);
478 ALOGV("%s: flags-%d, sample_rate-%d, bit_width-%d, app_type-%d",
479 __func__, s_info->flags.out_flags, s_info->app_type_cfg.sample_rate,
480 s_info->app_type_cfg.bit_width, s_info->app_type_cfg.app_type);
481 list_for_each(node_j, &s_info->format_list) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700482 sf_info = node_to_item(node_j, struct stream_format, list);
483 ALOGV("format-%x", sf_info->format);
484 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530485 list_for_each(node_j, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700486 ss_info = node_to_item(node_j, struct stream_sample_rate, list);
487 ALOGV("sample rate-%d", ss_info->sample_rate);
488 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700489 }
490}
491
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530492void audio_extn_utils_dump_streams_cfg_lists(
493 struct listnode *streams_output_cfg_list,
494 struct listnode *streams_input_cfg_list)
495{
496 ALOGV("%s", __func__);
497 audio_extn_utils_dump_streams_cfg_list(streams_output_cfg_list);
498 audio_extn_utils_dump_streams_cfg_list(streams_input_cfg_list);
499}
500
501static void audio_extn_utils_release_streams_cfg_list(
502 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700503{
504 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530505 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700506
507 ALOGV("%s", __func__);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530508
509 while (!list_empty(streams_cfg_list)) {
510 node_i = list_head(streams_cfg_list);
511 s_info = node_to_item(node_i, struct streams_io_cfg, list);
512 while (!list_empty(&s_info->format_list)) {
513 node_j = list_head(&s_info->format_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700514 list_remove(node_j);
515 free(node_to_item(node_j, struct stream_format, list));
516 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530517 while (!list_empty(&s_info->sample_rate_list)) {
518 node_j = list_head(&s_info->sample_rate_list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700519 list_remove(node_j);
520 free(node_to_item(node_j, struct stream_sample_rate, list));
521 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700522 list_remove(node_i);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530523 free(node_to_item(node_i, struct streams_io_cfg, list));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700524 }
525}
526
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530527void audio_extn_utils_release_streams_cfg_lists(
528 struct listnode *streams_output_cfg_list,
529 struct listnode *streams_input_cfg_list)
530{
531 ALOGV("%s", __func__);
532 audio_extn_utils_release_streams_cfg_list(streams_output_cfg_list);
533 audio_extn_utils_release_streams_cfg_list(streams_input_cfg_list);
534}
535
536static bool set_app_type_cfg(struct streams_io_cfg *s_info,
537 struct stream_app_type_cfg *app_type_cfg,
538 uint32_t sample_rate, uint32_t bit_width)
Amit Shekhar6f461b12014-08-01 14:52:58 -0700539 {
540 struct listnode *node_i;
541 struct stream_sample_rate *ss_info;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530542 list_for_each(node_i, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700543 ss_info = node_to_item(node_i, struct stream_sample_rate, list);
544 if ((sample_rate <= ss_info->sample_rate) &&
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530545 (bit_width == s_info->app_type_cfg.bit_width)) {
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700546
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530547 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700548 app_type_cfg->sample_rate = ss_info->sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530549 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700550 ALOGV("%s app_type_cfg->app_type %d, app_type_cfg->sample_rate %d, app_type_cfg->bit_width %d",
551 __func__, app_type_cfg->app_type, app_type_cfg->sample_rate, app_type_cfg->bit_width);
552 return true;
553 }
554 }
555 /*
556 * Reiterate through the list assuming dafault sample rate.
557 * Handles scenario where input sample rate is higher
558 * than all sample rates in list for the input bit width.
559 */
560 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
Apoorv Raghuvanshif59bb222015-02-18 12:23:23 -0800561
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530562 list_for_each(node_i, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700563 ss_info = node_to_item(node_i, struct stream_sample_rate, list);
564 if ((sample_rate <= ss_info->sample_rate) &&
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530565 (bit_width == s_info->app_type_cfg.bit_width)) {
566 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700567 app_type_cfg->sample_rate = sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530568 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Apoorv Raghuvanshif59bb222015-02-18 12:23:23 -0800569 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 -0700570 __func__, app_type_cfg->app_type, app_type_cfg->sample_rate, app_type_cfg->bit_width);
571 return true;
572 }
573 }
574 return false;
575}
576
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530577void audio_extn_utils_update_stream_input_app_type_cfg(void *platform,
578 struct listnode *streams_input_cfg_list,
579 audio_devices_t devices __unused,
580 audio_input_flags_t flags,
581 audio_format_t format,
582 uint32_t sample_rate,
583 uint32_t bit_width,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530584 char* profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530585 struct stream_app_type_cfg *app_type_cfg)
586{
587 struct listnode *node_i, *node_j;
588 struct streams_io_cfg *s_info;
589 struct stream_format *sf_info;
590
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530591 ALOGV("%s: flags: 0x%x, format: 0x%x sample_rate %d, profile %s",
592 __func__, flags, format, sample_rate, profile);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530593
594 list_for_each(node_i, streams_input_cfg_list) {
595 s_info = node_to_item(node_i, struct streams_io_cfg, list);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530596 /* Along with flags do profile matching if set at either end.*/
597 if (s_info->flags.in_flags == flags &&
598 ((profile[0] == '\0' && s_info->profile[0] == '\0') ||
599 strncmp(s_info->profile, profile, sizeof(s_info->profile)) == 0)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530600 list_for_each(node_j, &s_info->format_list) {
601 sf_info = node_to_item(node_j, struct stream_format, list);
602 if (sf_info->format == format) {
603 if (set_app_type_cfg(s_info, app_type_cfg, sample_rate, bit_width))
604 return;
605 }
606 }
607 }
608 }
609 ALOGW("%s: App type could not be selected. Falling back to default", __func__);
610 app_type_cfg->app_type = platform_get_default_app_type_v2(platform, PCM_CAPTURE);
611 app_type_cfg->sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
612 app_type_cfg->bit_width = 16;
613}
614
615void audio_extn_utils_update_stream_output_app_type_cfg(void *platform,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700616 struct listnode *streams_output_cfg_list,
Amit Shekhar1d896042014-10-03 13:16:09 -0700617 audio_devices_t devices,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700618 audio_output_flags_t flags,
619 audio_format_t format,
Amit Shekhar6f461b12014-08-01 14:52:58 -0700620 uint32_t sample_rate,
621 uint32_t bit_width,
Manish Dewangan837dc462015-05-27 10:17:41 +0530622 audio_channel_mask_t channel_mask,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530623 char *profile,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700624 struct stream_app_type_cfg *app_type_cfg)
625{
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +0530626 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530627 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700628 struct stream_format *sf_info;
Manish Dewangan837dc462015-05-27 10:17:41 +0530629 char value[PROPERTY_VALUE_MAX] = {0};
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700630
Ashish Jain058165c2016-09-28 23:18:48 +0530631 if ((bit_width >= 24) &&
Amit Shekhar1d896042014-10-03 13:16:09 -0700632 (devices & AUDIO_DEVICE_OUT_SPEAKER)) {
Amit Shekhar5a39c912014-10-14 15:39:30 -0700633 int32_t bw = platform_get_snd_device_bit_width(SND_DEVICE_OUT_SPEAKER);
634 if (-ENOSYS != bw)
635 bit_width = (uint32_t)bw;
Amit Shekhar1d896042014-10-03 13:16:09 -0700636 sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
637 ALOGI("%s Allowing 24-bit playback on speaker ONLY at default sampling rate", __func__);
638 }
639
Manish Dewangan837dc462015-05-27 10:17:41 +0530640 property_get("audio.playback.mch.downsample",value,"");
641 if (!strncmp("true", value, sizeof("true"))) {
642 if ((popcount(channel_mask) > 2) &&
643 (sample_rate > CODEC_BACKEND_DEFAULT_SAMPLE_RATE) &&
644 !(flags & AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH)) {
645 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
646 ALOGD("%s: MCH session defaulting sample rate to %d",
647 __func__, sample_rate);
648 }
649 }
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +0530650
651 /* Set sampling rate to 176.4 for DSD64
652 * and 352.8Khz for DSD128.
653 * Set Bit Width to 16. output will be 16 bit
654 * post DoP in ASM.
655 */
656 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH) &&
657 (format == AUDIO_FORMAT_DSD)) {
658 bit_width = 16;
659 if (sample_rate == INPUT_SAMPLING_RATE_DSD64)
660 sample_rate = OUTPUT_SAMPLING_RATE_DSD64;
661 else if (sample_rate == INPUT_SAMPLING_RATE_DSD128)
662 sample_rate = OUTPUT_SAMPLING_RATE_DSD128;
663 }
664
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +0530665 if(devices & AUDIO_DEVICE_OUT_ALL_A2DP) {
666 //TODO: Handle fractional sampling rate configuration for LL
667 audio_extn_a2dp_get_apptype_params(&sample_rate, &bit_width);
668 ALOGI("%s using %d sampling rate %d bit width for A2DP CoPP",
669 __func__, sample_rate, bit_width);
670 }
671
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530672 ALOGV("%s: flags: %x, format: %x sample_rate %d, profile %s",
673 __func__, flags, format, sample_rate, profile);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700674 list_for_each(node_i, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530675 s_info = node_to_item(node_i, struct streams_io_cfg, list);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530676 /* Along with flags do profile matching if set at either end.*/
677 if (s_info->flags.out_flags == flags &&
678 ((profile[0] == '\0' && s_info->profile[0] == '\0') ||
679 strncmp(s_info->profile, profile, sizeof(s_info->profile)) == 0)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530680 list_for_each(node_j, &s_info->format_list) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700681 sf_info = node_to_item(node_j, struct stream_format, list);
682 if (sf_info->format == format) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530683 if (set_app_type_cfg(s_info, app_type_cfg, sample_rate, bit_width))
Amit Shekhar6f461b12014-08-01 14:52:58 -0700684 return;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700685 }
686 }
687 }
688 }
689 list_for_each(node_i, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530690 s_info = node_to_item(node_i, struct streams_io_cfg, list);
691 if (s_info->flags.out_flags == AUDIO_OUTPUT_FLAG_PRIMARY) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700692 ALOGV("Compatible output profile not found.");
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530693 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
694 app_type_cfg->sample_rate = s_info->app_type_cfg.sample_rate;
695 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700696 ALOGV("%s Default to primary output: App type: %d sample_rate %d",
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530697 __func__, s_info->app_type_cfg.app_type, app_type_cfg->sample_rate);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700698 return;
699 }
700 }
701 ALOGW("%s: App type could not be selected. Falling back to default", __func__);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700702 app_type_cfg->app_type = platform_get_default_app_type(platform);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700703 app_type_cfg->sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700704 app_type_cfg->bit_width = 16;
705}
706
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +0530707static bool audio_is_this_native_usecase(struct audio_usecase *uc)
708{
709 bool native_usecase = false;
710 struct stream_out *out = (struct stream_out*) uc->stream.out;
711
712 if (PCM_PLAYBACK == uc->type && out != NULL &&
713 NATIVE_AUDIO_MODE_INVALID != platform_get_native_support() &&
714 is_offload_usecase(uc->id) &&
715 (out->sample_rate == OUTPUT_SAMPLING_RATE_44100))
716 native_usecase = true;
717
718 return native_usecase;
719}
720
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530721void audio_extn_utils_update_stream_app_type_cfg_for_usecase(
722 struct audio_device *adev,
723 struct audio_usecase *usecase)
724{
725 ALOGV("%s", __func__);
726
727 switch(usecase->type) {
728 case PCM_PLAYBACK:
729 audio_extn_utils_update_stream_output_app_type_cfg(adev->platform,
730 &adev->streams_output_cfg_list,
731 usecase->stream.out->devices,
732 usecase->stream.out->flags,
733 usecase->stream.out->format,
734 usecase->stream.out->sample_rate,
735 usecase->stream.out->bit_width,
736 usecase->stream.out->channel_mask,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530737 usecase->stream.out->profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530738 &usecase->stream.out->app_type_cfg);
739 ALOGV("%s Selected apptype: %d", __func__, usecase->stream.out->app_type_cfg.app_type);
740 break;
741 case PCM_CAPTURE:
742 audio_extn_utils_update_stream_input_app_type_cfg(adev->platform,
743 &adev->streams_input_cfg_list,
744 usecase->stream.in->device,
745 usecase->stream.in->flags,
746 usecase->stream.in->format,
747 usecase->stream.in->sample_rate,
748 usecase->stream.in->bit_width,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530749 usecase->stream.in->profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530750 &usecase->stream.in->app_type_cfg);
751 ALOGV("%s Selected apptype: %d", __func__, usecase->stream.in->app_type_cfg.app_type);
752 break;
753 default:
754 ALOGE("%s: app type cfg not supported for usecase type (%d)",
755 __func__, usecase->type);
756 }
757}
758
Ben Romberger1fafdde2015-09-09 19:43:15 -0700759int audio_extn_utils_send_app_type_cfg(struct audio_device *adev,
760 struct audio_usecase *usecase)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700761{
762 char mixer_ctl_name[MAX_LENGTH_MIXER_CONTROL_IN_INT];
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530763 size_t app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT] = {0};
764 int len = 0, rc;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700765 struct mixer_ctl *ctl;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530766 int pcm_device_id, acdb_dev_id = 0, snd_device = usecase->out_snd_device;
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +0530767 int32_t sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Manish Dewangan837dc462015-05-27 10:17:41 +0530768 char value[PROPERTY_VALUE_MAX] = {0};
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700769
770 ALOGV("%s", __func__);
771
Srikanth Uyyala9d551402015-08-25 16:03:42 +0530772 if (usecase->type != PCM_PLAYBACK && usecase->type != PCM_CAPTURE) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530773 ALOGE("%s: not a playback/capture path, no need to cfg app type", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700774 rc = 0;
775 goto exit_send_app_type_cfg;
776 }
777 if ((usecase->id != USECASE_AUDIO_PLAYBACK_DEEP_BUFFER) &&
778 (usecase->id != USECASE_AUDIO_PLAYBACK_LOW_LATENCY) &&
779 (usecase->id != USECASE_AUDIO_PLAYBACK_MULTI_CH) &&
Srikanth Uyyala9d551402015-08-25 16:03:42 +0530780 (!is_offload_usecase(usecase->id)) &&
781 (usecase->type != PCM_CAPTURE)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530782 ALOGV("%s: a rx/tx/loopback path where app type cfg is not required %d", __func__, usecase->id);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700783 rc = 0;
784 goto exit_send_app_type_cfg;
785 }
Srikanth Uyyala9d551402015-08-25 16:03:42 +0530786 if (usecase->type == PCM_PLAYBACK) {
Srikanth Uyyala9d551402015-08-25 16:03:42 +0530787 snd_device = usecase->out_snd_device;
Ben Romberger1fafdde2015-09-09 19:43:15 -0700788 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_PLAYBACK);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530789 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
790 "Audio Stream %d App Type Cfg", pcm_device_id);
791 acdb_dev_id = platform_get_snd_device_acdb_id(usecase->out_snd_device);
Ben Romberger1fafdde2015-09-09 19:43:15 -0700792 } else if (usecase->type == PCM_CAPTURE) {
Srikanth Uyyala9d551402015-08-25 16:03:42 +0530793 snd_device = usecase->in_snd_device;
Ben Romberger1fafdde2015-09-09 19:43:15 -0700794 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_CAPTURE);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530795 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
796 "Audio Stream Capture %d App Type Cfg", pcm_device_id);
797 acdb_dev_id = platform_get_snd_device_acdb_id(usecase->in_snd_device);
Srikanth Uyyala9d551402015-08-25 16:03:42 +0530798 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530799 if (acdb_dev_id <= 0) {
800 ALOGE("%s: Couldn't get the acdb dev id", __func__);
801 rc = -EINVAL;
802 goto exit_send_app_type_cfg;
803 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700804
805 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
806 if (!ctl) {
807 ALOGE("%s: Could not get ctl for mixer cmd - %s", __func__,
808 mixer_ctl_name);
809 rc = -EINVAL;
810 goto exit_send_app_type_cfg;
811 }
812 snd_device = (snd_device == SND_DEVICE_OUT_SPEAKER) ?
Xiaojun Sang040cc9f2015-08-03 19:38:28 +0800813 platform_get_spkr_prot_snd_device(snd_device) : snd_device;
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +0530814
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530815 if ((usecase->type == PCM_PLAYBACK) && (usecase->stream.out != NULL)) {
816 if (usecase->stream.out->devices & AUDIO_DEVICE_OUT_SPEAKER) {
817 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
818 } else if ((usecase->stream.out->app_type_cfg.sample_rate == OUTPUT_SAMPLING_RATE_44100 &&
819 !(audio_is_this_native_usecase(usecase))) ||
820 (usecase->stream.out->sample_rate < OUTPUT_SAMPLING_RATE_44100)) {
821 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
822 }
Venkata Narendra Kumar Gutta4bd09d02016-01-29 15:31:04 +0530823
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530824 sample_rate = usecase->stream.out->app_type_cfg.sample_rate;
Manish Dewangan837dc462015-05-27 10:17:41 +0530825
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530826 property_get("audio.playback.mch.downsample",value,"");
827 if (!strncmp("true", value, sizeof("true"))) {
828 if ((popcount(usecase->stream.out->channel_mask) > 2) &&
829 (usecase->stream.out->app_type_cfg.sample_rate > CODEC_BACKEND_DEFAULT_SAMPLE_RATE) &&
830 !(usecase->stream.out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH))
831 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
832 }
Ashish Jainc02430d2016-01-04 10:42:43 +0530833
Mingming Yin21854652016-04-13 11:54:02 -0700834 if ((24 == usecase->stream.out->bit_width) &&
835 (usecase->stream.out->devices & AUDIO_DEVICE_OUT_SPEAKER)) {
836 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Ashish Jaina052e572016-11-07 16:41:07 +0530837 } else if ((snd_device == SND_DEVICE_OUT_HDMI ||
838 snd_device == SND_DEVICE_OUT_USB_HEADSET ||
839 snd_device == SND_DEVICE_OUT_DISPLAY_PORT) &&
840 (usecase->stream.out->sample_rate >= OUTPUT_SAMPLING_RATE_44100)) {
841 /*
842 * To best utlize DSP, check if the stream sample rate is supported/multiple of
843 * configured device sample rate, if not update the COPP rate to be equal to the
844 * device sample rate, else open COPP at stream sample rate
845 */
846 platform_check_and_update_copp_sample_rate(adev->platform, snd_device,
847 usecase->stream.out->sample_rate,
848 &usecase->stream.out->app_type_cfg.sample_rate);
Mingming Yin21854652016-04-13 11:54:02 -0700849 } else if ((snd_device != SND_DEVICE_OUT_HEADPHONES_44_1 &&
850 usecase->stream.out->sample_rate == OUTPUT_SAMPLING_RATE_44100) ||
851 (usecase->stream.out->sample_rate < OUTPUT_SAMPLING_RATE_44100)) {
852 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
853 }
854 sample_rate = usecase->stream.out->app_type_cfg.sample_rate;
855
856 app_type_cfg[len++] = usecase->stream.out->app_type_cfg.app_type;
857 app_type_cfg[len++] = acdb_dev_id;
858 if (((usecase->stream.out->format == AUDIO_FORMAT_E_AC3) ||
859 (usecase->stream.out->format == AUDIO_FORMAT_E_AC3_JOC))
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530860 && audio_extn_passthru_is_passthrough_stream(usecase->stream.out)) {
Mingming Yin21854652016-04-13 11:54:02 -0700861 app_type_cfg[len++] = sample_rate * 4;
862 } else {
863 app_type_cfg[len++] = sample_rate;
864 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530865
866 ALOGI("%s PLAYBACK app_type %d, acdb_dev_id %d, sample_rate %d",
867 __func__, usecase->stream.out->app_type_cfg.app_type, acdb_dev_id, sample_rate);
868
869 } else if ((usecase->type == PCM_CAPTURE) && (usecase->stream.in != NULL)) {
870 app_type_cfg[len++] = usecase->stream.in->app_type_cfg.app_type;
871 app_type_cfg[len++] = acdb_dev_id;
872 app_type_cfg[len++] = usecase->stream.in->app_type_cfg.sample_rate;
873 ALOGI("%s CAPTURE app_type %d, acdb_dev_id %d, sample_rate %d",
874 __func__, usecase->stream.in->app_type_cfg.app_type, acdb_dev_id,
875 usecase->stream.in->app_type_cfg.sample_rate);
876 } else {
877 app_type_cfg[len++] = platform_get_default_app_type_v2(adev->platform, usecase->type);
878 app_type_cfg[len++] = acdb_dev_id;
879 app_type_cfg[len++] = sample_rate;
880 ALOGI("%s default app_type %d, acdb_dev_id %d, sample_rate %d",
881 __func__, platform_get_default_app_type_v2(adev->platform, usecase->type),
882 acdb_dev_id, sample_rate);
Ashish Jainc02430d2016-01-04 10:42:43 +0530883 }
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +0530884
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700885 mixer_ctl_set_array(ctl, app_type_cfg, len);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700886 rc = 0;
887exit_send_app_type_cfg:
888 return rc;
889}
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700890
Preetam Singh Ranawat9519e9c2015-11-18 16:05:55 +0530891int read_line_from_file(const char *path, char *buf, size_t count)
892{
893 char * fgets_ret;
894 FILE * fd;
895 int rv;
896
897 fd = fopen(path, "r");
898 if (fd == NULL)
899 return -1;
900
901 fgets_ret = fgets(buf, (int)count, fd);
902 if (NULL != fgets_ret) {
903 rv = (int)strlen(buf);
904 } else {
905 rv = ferror(fd);
906 }
907 fclose(fd);
908
909 return rv;
910}
911
Ashish Jainf1eaa582016-05-23 20:54:24 +0530912/*Translates ALSA formats to AOSP PCM formats*/
913audio_format_t alsa_format_to_hal(uint32_t alsa_format)
914{
915 audio_format_t format;
916
917 switch(alsa_format) {
918 case SNDRV_PCM_FORMAT_S16_LE:
919 format = AUDIO_FORMAT_PCM_16_BIT;
920 break;
921 case SNDRV_PCM_FORMAT_S24_3LE:
922 format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
923 break;
924 case SNDRV_PCM_FORMAT_S24_LE:
925 format = AUDIO_FORMAT_PCM_8_24_BIT;
926 break;
927 case SNDRV_PCM_FORMAT_S32_LE:
928 format = AUDIO_FORMAT_PCM_32_BIT;
929 break;
930 default:
931 ALOGW("Incorrect ALSA format");
932 format = AUDIO_FORMAT_INVALID;
933 }
934 return format;
935}
936
937/*Translates hal format (AOSP) to alsa formats*/
938uint32_t hal_format_to_alsa(audio_format_t hal_format)
939{
940 uint32_t alsa_format;
941
942 switch (hal_format) {
943 case AUDIO_FORMAT_PCM_32_BIT: {
944 if (platform_supports_true_32bit())
945 alsa_format = SNDRV_PCM_FORMAT_S32_LE;
946 else
947 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
948 }
949 break;
950 case AUDIO_FORMAT_PCM_8_BIT:
951 alsa_format = SNDRV_PCM_FORMAT_S8;
952 break;
953 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
954 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
955 break;
956 case AUDIO_FORMAT_PCM_8_24_BIT: {
957 if (platform_supports_true_32bit())
958 alsa_format = SNDRV_PCM_FORMAT_S32_LE;
959 else
960 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
961 }
962 break;
963 case AUDIO_FORMAT_PCM_FLOAT:
964 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
965 break;
966 default:
967 case AUDIO_FORMAT_PCM_16_BIT:
968 alsa_format = SNDRV_PCM_FORMAT_S16_LE;
969 break;
970 }
971 return alsa_format;
972}
973
Ashish Jain83a6cc22016-06-28 14:34:17 +0530974/*Translates PCM formats to AOSP formats*/
975audio_format_t pcm_format_to_hal(uint32_t pcm_format)
976{
977 audio_format_t format = AUDIO_FORMAT_INVALID;
978
979 switch(pcm_format) {
980 case PCM_FORMAT_S16_LE:
981 format = AUDIO_FORMAT_PCM_16_BIT;
982 break;
983 case PCM_FORMAT_S24_3LE:
984 format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
985 break;
986 case PCM_FORMAT_S24_LE:
987 format = AUDIO_FORMAT_PCM_8_24_BIT;
988 break;
989 case PCM_FORMAT_S32_LE:
990 format = AUDIO_FORMAT_PCM_32_BIT;
991 break;
992 default:
993 ALOGW("Incorrect PCM format");
994 format = AUDIO_FORMAT_INVALID;
995 }
996 return format;
997}
998
999/*Translates hal format (AOSP) to alsa formats*/
1000uint32_t hal_format_to_pcm(audio_format_t hal_format)
1001{
1002 uint32_t pcm_format;
1003
1004 switch (hal_format) {
1005 case AUDIO_FORMAT_PCM_32_BIT:
1006 case AUDIO_FORMAT_PCM_8_24_BIT:
1007 case AUDIO_FORMAT_PCM_FLOAT: {
1008 if (platform_supports_true_32bit())
1009 pcm_format = PCM_FORMAT_S32_LE;
1010 else
1011 pcm_format = PCM_FORMAT_S24_3LE;
1012 }
1013 break;
1014 case AUDIO_FORMAT_PCM_8_BIT:
1015 pcm_format = PCM_FORMAT_S8;
1016 break;
1017 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
1018 pcm_format = PCM_FORMAT_S24_3LE;
1019 break;
1020 default:
1021 case AUDIO_FORMAT_PCM_16_BIT:
1022 pcm_format = PCM_FORMAT_S16_LE;
1023 break;
1024 }
1025 return pcm_format;
1026}
1027
Ashish Jainf1eaa582016-05-23 20:54:24 +05301028uint32_t get_alsa_fragment_size(uint32_t bytes_per_sample,
1029 uint32_t sample_rate,
1030 uint32_t noOfChannels)
1031{
1032 uint32_t fragment_size = 0;
1033 uint32_t pcm_offload_time = PCM_OFFLOAD_BUFFER_DURATION;
1034
1035 fragment_size = (pcm_offload_time
1036 * sample_rate
1037 * bytes_per_sample
1038 * noOfChannels)/1000;
1039 if (fragment_size < MIN_PCM_OFFLOAD_FRAGMENT_SIZE)
1040 fragment_size = MIN_PCM_OFFLOAD_FRAGMENT_SIZE;
1041 else if (fragment_size > MAX_PCM_OFFLOAD_FRAGMENT_SIZE)
1042 fragment_size = MAX_PCM_OFFLOAD_FRAGMENT_SIZE;
1043 /*To have same PCM samples for all channels, the buffer size requires to
1044 *be multiple of (number of channels * bytes per sample)
1045 *For writes to succeed, the buffer must be written at address which is multiple of 32
1046 */
1047 fragment_size = ALIGN(fragment_size, (bytes_per_sample * noOfChannels * 32));
1048
1049 ALOGI("PCM offload Fragment size to %d bytes", fragment_size);
1050 return fragment_size;
1051}
1052
1053/* Calculates the fragment size required to configure compress session.
1054 * Based on the alsa format selected, decide if conversion is needed in
1055
1056 * HAL ( e.g. convert AUDIO_FORMAT_PCM_FLOAT input format to
1057 * AUDIO_FORMAT_PCM_24_BIT_PACKED before writing to the compress driver.
1058 */
1059void audio_extn_utils_update_direct_pcm_fragment_size(struct stream_out *out)
1060{
Ashish Jain83a6cc22016-06-28 14:34:17 +05301061 audio_format_t dst_format = out->hal_op_format;
1062 audio_format_t src_format = out->hal_ip_format;
Ashish Jainf1eaa582016-05-23 20:54:24 +05301063 uint32_t hal_op_bytes_per_sample = audio_bytes_per_sample(dst_format);
1064 uint32_t hal_ip_bytes_per_sample = audio_bytes_per_sample(src_format);
1065
1066 out->compr_config.fragment_size =
1067 get_alsa_fragment_size(hal_op_bytes_per_sample,
1068 out->sample_rate,
1069 popcount(out->channel_mask));
1070
1071 if ((src_format != dst_format) &&
1072 hal_op_bytes_per_sample != hal_ip_bytes_per_sample) {
1073
Ashish Jain83a6cc22016-06-28 14:34:17 +05301074 out->hal_fragment_size =
Ashish Jainf1eaa582016-05-23 20:54:24 +05301075 ((out->compr_config.fragment_size * hal_ip_bytes_per_sample) /
1076 hal_op_bytes_per_sample);
1077 ALOGI("enable conversion hal_input_fragment_size is %d src_format %x dst_format %x",
Ashish Jain83a6cc22016-06-28 14:34:17 +05301078 out->hal_fragment_size, src_format, dst_format);
Ashish Jainf1eaa582016-05-23 20:54:24 +05301079 } else {
Ashish Jain83a6cc22016-06-28 14:34:17 +05301080 out->hal_fragment_size = out->compr_config.fragment_size;
Ashish Jainf1eaa582016-05-23 20:54:24 +05301081 }
1082}
1083
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001084void audio_extn_utils_send_audio_calibration(struct audio_device *adev,
1085 struct audio_usecase *usecase)
1086{
1087 int type = usecase->type;
1088
1089 if (type == PCM_PLAYBACK) {
1090 struct stream_out *out = usecase->stream.out;
1091 int snd_device = usecase->out_snd_device;
1092 snd_device = (snd_device == SND_DEVICE_OUT_SPEAKER) ?
Xiaojun Sang040cc9f2015-08-03 19:38:28 +08001093 platform_get_spkr_prot_snd_device(snd_device) : snd_device;
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05301094 platform_send_audio_calibration(adev->platform, usecase,
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001095 out->app_type_cfg.app_type,
Preetam Singh Ranawat61716b12015-12-14 11:55:24 +05301096 usecase->stream.out->app_type_cfg.sample_rate);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301097 } else if (type == PCM_CAPTURE) {
1098 platform_send_audio_calibration(adev->platform, usecase,
1099 usecase->stream.in->app_type_cfg.app_type,
1100 usecase->stream.in->app_type_cfg.sample_rate);
1101 } else {
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05301102 /* when app type is default. the sample rate is not used to send cal */
1103 platform_send_audio_calibration(adev->platform, usecase,
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301104 platform_get_default_app_type_v2(adev->platform, usecase->type),
1105 48000);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001106 }
1107}
1108
Ben Rombergera04fabc2014-11-14 12:16:03 -08001109// Base64 Encode and Decode
1110// Not all features supported. This must be used only with following conditions.
1111// Decode Modes: Support with and without padding
1112// CRLF not handling. So no CRLF in string to decode.
1113// Encode Modes: Supports only padding
1114int b64decode(char *inp, int ilen, uint8_t* outp)
1115{
1116 int i, j, k, ii, num;
1117 int rem, pcnt;
1118 uint32_t res=0;
1119 uint8_t getIndex[MAX_BASEINDEX_LEN];
1120 uint8_t tmp, cflag;
1121
1122 if(inp == NULL || outp == NULL || ilen <= 0) {
1123 ALOGE("[%s] received NULL pointer or zero length",__func__);
1124 return -1;
1125 }
1126
1127 memset(getIndex, MAX_BASEINDEX_LEN-1, sizeof(getIndex));
1128 for(i=0;i<BASE_TABLE_SIZE;i++) {
1129 getIndex[(uint8_t)bTable[i]] = (uint8_t)i;
1130 }
1131 getIndex[(uint8_t)'=']=0;
1132
1133 j=0;k=0;
1134 num = ilen/4;
1135 rem = ilen%4;
1136 if(rem==0)
1137 num = num-1;
1138 cflag=0;
1139 for(i=0; i<num; i++) {
1140 res=0;
1141 for(ii=0;ii<4;ii++) {
1142 res = res << 6;
1143 tmp = getIndex[(uint8_t)inp[j++]];
1144 res = res | tmp;
1145 cflag = cflag | tmp;
1146 }
1147 outp[k++] = (res >> 16)&0xFF;
1148 outp[k++] = (res >> 8)&0xFF;
1149 outp[k++] = res & 0xFF;
1150 }
1151
1152 // Handle last bytes special
1153 pcnt=0;
1154 if(rem == 0) {
1155 //With padding or full data
1156 res = 0;
1157 for(ii=0;ii<4;ii++) {
1158 if(inp[j] == '=')
1159 pcnt++;
1160 res = res << 6;
1161 tmp = getIndex[(uint8_t)inp[j++]];
1162 res = res | tmp;
1163 cflag = cflag | tmp;
1164 }
1165 outp[k++] = res >> 16;
1166 if(pcnt == 2)
1167 goto done;
1168 outp[k++] = (res>>8)&0xFF;
1169 if(pcnt == 1)
1170 goto done;
1171 outp[k++] = res&0xFF;
1172 } else {
1173 //without padding
1174 res = 0;
1175 for(i=0;i<rem;i++) {
1176 res = res << 6;
1177 tmp = getIndex[(uint8_t)inp[j++]];
1178 res = res | tmp;
1179 cflag = cflag | tmp;
1180 }
1181 for(i=rem;i<4;i++) {
1182 res = res << 6;
1183 pcnt++;
1184 }
1185 outp[k++] = res >> 16;
1186 if(pcnt == 2)
1187 goto done;
1188 outp[k++] = (res>>8)&0xFF;
1189 if(pcnt == 1)
1190 goto done;
1191 outp[k++] = res&0xFF;
1192 }
1193done:
1194 if(cflag == 0xFF) {
1195 ALOGE("[%s] base64 decode failed. Invalid character found %s",
1196 __func__, inp);
1197 return 0;
1198 }
1199 return k;
1200}
1201
1202int b64encode(uint8_t *inp, int ilen, char* outp)
1203{
1204 int i,j,k, num;
1205 int rem=0;
1206 uint32_t res=0;
1207
1208 if(inp == NULL || outp == NULL || ilen<=0) {
1209 ALOGE("[%s] received NULL pointer or zero input length",__func__);
1210 return -1;
1211 }
1212
1213 num = ilen/3;
1214 rem = ilen%3;
1215 j=0;k=0;
1216 for(i=0; i<num; i++) {
1217 //prepare index
1218 res = inp[j++]<<16;
1219 res = res | inp[j++]<<8;
1220 res = res | inp[j++];
1221 //get output map from index
1222 outp[k++] = (char) bTable[(res>>18)&0x3F];
1223 outp[k++] = (char) bTable[(res>>12)&0x3F];
1224 outp[k++] = (char) bTable[(res>>6)&0x3F];
1225 outp[k++] = (char) bTable[res&0x3F];
1226 }
1227
1228 switch(rem) {
1229 case 1:
1230 res = inp[j++]<<16;
1231 outp[k++] = (char) bTable[res>>18];
1232 outp[k++] = (char) bTable[(res>>12)&0x3F];
1233 //outp[k++] = '=';
1234 //outp[k++] = '=';
1235 break;
1236 case 2:
1237 res = inp[j++]<<16;
1238 res = res | inp[j++]<<8;
1239 outp[k++] = (char) bTable[res>>18];
1240 outp[k++] = (char) bTable[(res>>12)&0x3F];
1241 outp[k++] = (char) bTable[(res>>6)&0x3F];
1242 //outp[k++] = '=';
1243 break;
1244 default:
1245 break;
1246 }
Ben Rombergera04fabc2014-11-14 12:16:03 -08001247 outp[k] = '\0';
1248 return k;
1249}
Ashish Jain81eb2a82015-05-13 10:52:34 +05301250
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301251
1252int audio_extn_utils_get_codec_version(const char *snd_card_name,
1253 int card_num,
1254 char *codec_version)
1255{
1256 char procfs_path[50];
1257 FILE *fp;
1258
1259 if (strstr(snd_card_name, "tasha")) {
1260 snprintf(procfs_path, sizeof(procfs_path),
1261 "/proc/asound/card%d/codecs/tasha/version", card_num);
1262 if ((fp = fopen(procfs_path, "r")) != NULL) {
1263 fgets(codec_version, CODEC_VERSION_MAX_LENGTH, fp);
1264 fclose(fp);
1265 } else {
1266 ALOGE("%s: ERROR. cannot open %s", __func__, procfs_path);
1267 return -ENOENT;
1268 }
1269 ALOGD("%s: codec version %s", __func__, codec_version);
1270 }
1271
1272 return 0;
1273}
1274
1275
Ashish Jain81eb2a82015-05-13 10:52:34 +05301276#ifdef AUDIO_EXTERNAL_HDMI_ENABLED
1277
1278void get_default_compressed_channel_status(
1279 unsigned char *channel_status)
1280{
Ashish Jain81eb2a82015-05-13 10:52:34 +05301281 memset(channel_status,0,24);
1282
1283 /* block start bit in preamble bit 3 */
1284 channel_status[0] |= PROFESSIONAL;
1285 //compre out
1286 channel_status[0] |= NON_LPCM;
1287 // sample rate; fixed 48K for default/transcode
1288 channel_status[3] |= SR_48000;
1289}
1290
1291#ifdef HDMI_PASSTHROUGH_ENABLED
1292int32_t get_compressed_channel_status(void *audio_stream_data,
1293 uint32_t audio_frame_size,
1294 unsigned char *channel_status,
1295 enum audio_parser_code_type codec_type)
1296 // codec_type - AUDIO_PARSER_CODEC_AC3
1297 // - AUDIO_PARSER_CODEC_DTS
1298{
1299 unsigned char *stream;
1300 int ret = 0;
1301 stream = (unsigned char *)audio_stream_data;
1302
1303 if (audio_stream_data == NULL || audio_frame_size == 0) {
1304 ALOGW("no buffer to get channel status, return default for compress");
1305 get_default_compressed_channel_status(channel_status);
1306 return ret;
1307 }
1308
1309 memset(channel_status,0,24);
1310 if(init_audio_parser(stream, audio_frame_size, codec_type) == -1)
1311 {
1312 ALOGE("init audio parser failed");
1313 return -1;
1314 }
1315 ret = get_channel_status(channel_status, codec_type);
1316 return ret;
1317
1318}
1319
1320#endif
1321
1322void get_lpcm_channel_status(uint32_t sampleRate,
1323 unsigned char *channel_status)
1324{
1325 int32_t status = 0;
Ashish Jain81eb2a82015-05-13 10:52:34 +05301326 memset(channel_status,0,24);
1327 /* block start bit in preamble bit 3 */
1328 channel_status[0] |= PROFESSIONAL;
1329 //LPCM OUT
1330 channel_status[0] &= ~NON_LPCM;
1331
1332 switch (sampleRate) {
1333 case 8000:
1334 case 11025:
1335 case 12000:
1336 case 16000:
1337 case 22050:
1338 channel_status[3] |= SR_NOTID;
Preetam Singh Ranawat61716b12015-12-14 11:55:24 +05301339 break;
Ashish Jain81eb2a82015-05-13 10:52:34 +05301340 case 24000:
1341 channel_status[3] |= SR_24000;
1342 break;
1343 case 32000:
1344 channel_status[3] |= SR_32000;
1345 break;
1346 case 44100:
1347 channel_status[3] |= SR_44100;
1348 break;
1349 case 48000:
1350 channel_status[3] |= SR_48000;
1351 break;
1352 case 88200:
1353 channel_status[3] |= SR_88200;
1354 break;
1355 case 96000:
1356 channel_status[3] |= SR_96000;
1357 break;
1358 case 176400:
1359 channel_status[3] |= SR_176400;
1360 break;
1361 case 192000:
1362 channel_status[3] |= SR_192000;
1363 break;
1364 default:
1365 ALOGV("Invalid sample_rate %u\n", sampleRate);
1366 status = -1;
1367 break;
1368 }
1369}
1370
1371void audio_utils_set_hdmi_channel_status(struct stream_out *out, char * buffer, size_t bytes)
1372{
1373 unsigned char channel_status[24]={0};
1374 struct snd_aes_iec958 iec958;
1375 const char *mixer_ctl_name = "IEC958 Playback PCM Stream";
1376 struct mixer_ctl *ctl;
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +05301377 ALOGV("%s: buffer %s bytes %zd", __func__, buffer, bytes);
Ashish Jain81eb2a82015-05-13 10:52:34 +05301378#ifdef HDMI_PASSTHROUGH_ENABLED
1379 if (audio_extn_is_dolby_format(out->format) &&
1380 /*TODO:Extend code to support DTS passthrough*/
1381 /*set compressed channel status bits*/
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05301382 audio_extn_passthru_is_passthrough_stream(out)){
Ashish Jain81eb2a82015-05-13 10:52:34 +05301383 get_compressed_channel_status(buffer, bytes, channel_status, AUDIO_PARSER_CODEC_AC3);
1384 } else
1385#endif
1386 {
1387 /*set channel status bit for LPCM*/
1388 get_lpcm_channel_status(out->sample_rate, channel_status);
1389 }
1390
1391 memcpy(iec958.status, channel_status,sizeof(iec958.status));
1392 ctl = mixer_get_ctl_by_name(out->dev->mixer, mixer_ctl_name);
1393 if (!ctl) {
1394 ALOGE("%s: Could not get ctl for mixer cmd - %s",
1395 __func__, mixer_ctl_name);
1396 return;
1397 }
1398 if (mixer_ctl_set_array(ctl, &iec958, sizeof(iec958)) < 0) {
1399 ALOGE("%s: Could not set channel status for ext HDMI ",
1400 __func__);
1401 return;
1402 }
1403
1404}
1405#endif