blob: 81dfbd1c5d9308bdf152fc928f64fd68263a714c [file] [log] [blame]
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001/*
Dhananjay Kumar14245982017-01-16 20:21:00 +05302 * Copyright (c) 2014-2017, 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"
Dhananjay Kumaree4d2002016-10-25 18:02:58 +053037#include "sound/compress_params.h"
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070038
Ashish Jain81eb2a82015-05-13 10:52:34 +053039#ifdef AUDIO_EXTERNAL_HDMI_ENABLED
40#ifdef HDMI_PASSTHROUGH_ENABLED
41#include "audio_parsers.h"
42#endif
43#endif
44
Yamit Mehtaa0d653a2016-11-25 20:33:25 +053045#ifdef LINUX_ENABLED
46#define AUDIO_OUTPUT_POLICY_VENDOR_CONFIG_FILE "/etc/audio_output_policy.conf"
Dhananjay Kumard6d32152016-10-13 16:11:03 +053047#define AUDIO_IO_POLICY_VENDOR_CONFIG_FILE "/etc/audio_io_policy.conf"
Yamit Mehtaa0d653a2016-11-25 20:33:25 +053048#else
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070049#define AUDIO_OUTPUT_POLICY_VENDOR_CONFIG_FILE "/vendor/etc/audio_output_policy.conf"
Dhananjay Kumard6d32152016-10-13 16:11:03 +053050#define AUDIO_IO_POLICY_VENDOR_CONFIG_FILE "/vendor/etc/audio_io_policy.conf"
Yamit Mehtaa0d653a2016-11-25 20:33:25 +053051#endif
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070052
53#define OUTPUTS_TAG "outputs"
Dhananjay Kumard6d32152016-10-13 16:11:03 +053054#define INPUTS_TAG "inputs"
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070055
56#define DYNAMIC_VALUE_TAG "dynamic"
57#define FLAGS_TAG "flags"
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +053058#define PROFILES_TAG "profile"
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070059#define FORMATS_TAG "formats"
60#define SAMPLING_RATES_TAG "sampling_rates"
61#define BIT_WIDTH_TAG "bit_width"
62#define APP_TYPE_TAG "app_type"
63
64#define STRING_TO_ENUM(string) { #string, string }
65#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
66
Ben Rombergera04fabc2014-11-14 12:16:03 -080067#define BASE_TABLE_SIZE 64
68#define MAX_BASEINDEX_LEN 256
69
Ashish Jain81eb2a82015-05-13 10:52:34 +053070#ifdef AUDIO_EXTERNAL_HDMI_ENABLED
71#define PROFESSIONAL (1<<0) /* 0 = consumer, 1 = professional */
72#define NON_LPCM (1<<1) /* 0 = audio, 1 = non-audio */
73#define SR_44100 (0<<0) /* 44.1kHz */
74#define SR_NOTID (1<<0) /* non indicated */
75#define SR_48000 (2<<0) /* 48kHz */
76#define SR_32000 (3<<0) /* 32kHz */
77#define SR_22050 (4<<0) /* 22.05kHz */
78#define SR_24000 (6<<0) /* 24kHz */
79#define SR_88200 (8<<0) /* 88.2kHz */
80#define SR_96000 (10<<0) /* 96kHz */
81#define SR_176400 (12<<0) /* 176.4kHz */
82#define SR_192000 (14<<0) /* 192kHz */
83
84#endif
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070085struct string_to_enum {
86 const char *name;
87 uint32_t value;
88};
89
90const struct string_to_enum s_flag_name_to_enum_table[] = {
91 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_DIRECT),
vivek mehta0ea887a2015-08-26 14:01:20 -070092 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_DIRECT_PCM),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070093 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_PRIMARY),
94 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_FAST),
95 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_DEEP_BUFFER),
96 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD),
97 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_NON_BLOCKING),
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -070098 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_HW_AV_SYNC),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070099#ifdef INCALL_MUSIC_ENABLED
100 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_INCALL_MUSIC),
101#endif
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700102 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH),
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530103 STRING_TO_ENUM(AUDIO_INPUT_FLAG_NONE),
104 STRING_TO_ENUM(AUDIO_INPUT_FLAG_FAST),
105 STRING_TO_ENUM(AUDIO_INPUT_FLAG_HW_HOTWORD),
106 STRING_TO_ENUM(AUDIO_INPUT_FLAG_RAW),
107 STRING_TO_ENUM(AUDIO_INPUT_FLAG_SYNC),
Dhananjay Kumaree4d2002016-10-25 18:02:58 +0530108 STRING_TO_ENUM(AUDIO_INPUT_FLAG_TIMESTAMP),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700109};
110
111const struct string_to_enum s_format_name_to_enum_table[] = {
Ashish Jain83a6cc22016-06-28 14:34:17 +0530112 STRING_TO_ENUM(AUDIO_FORMAT_PCM_8_BIT),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700113 STRING_TO_ENUM(AUDIO_FORMAT_PCM_16_BIT),
Ashish Jain5106d362016-05-11 19:23:33 +0530114 STRING_TO_ENUM(AUDIO_FORMAT_PCM_24_BIT_PACKED),
115 STRING_TO_ENUM(AUDIO_FORMAT_PCM_8_24_BIT),
Ashish Jainf1eaa582016-05-23 20:54:24 +0530116 STRING_TO_ENUM(AUDIO_FORMAT_PCM_32_BIT),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700117 STRING_TO_ENUM(AUDIO_FORMAT_MP3),
118 STRING_TO_ENUM(AUDIO_FORMAT_AAC),
119 STRING_TO_ENUM(AUDIO_FORMAT_VORBIS),
Mingming Yinae3530f2014-07-03 16:50:18 -0700120 STRING_TO_ENUM(AUDIO_FORMAT_AMR_NB),
121 STRING_TO_ENUM(AUDIO_FORMAT_AMR_WB),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700122 STRING_TO_ENUM(AUDIO_FORMAT_AC3),
Mingming Yinae3530f2014-07-03 16:50:18 -0700123 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700124 STRING_TO_ENUM(AUDIO_FORMAT_DTS),
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530125 STRING_TO_ENUM(AUDIO_FORMAT_DTS_HD),
126#ifdef AUDIO_EXTN_FORMATS_ENABLED
127 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3_JOC),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700128 STRING_TO_ENUM(AUDIO_FORMAT_WMA),
129 STRING_TO_ENUM(AUDIO_FORMAT_WMA_PRO),
130 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADIF),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700131 STRING_TO_ENUM(AUDIO_FORMAT_AMR_WB_PLUS),
132 STRING_TO_ENUM(AUDIO_FORMAT_EVRC),
133 STRING_TO_ENUM(AUDIO_FORMAT_EVRCB),
134 STRING_TO_ENUM(AUDIO_FORMAT_EVRCWB),
135 STRING_TO_ENUM(AUDIO_FORMAT_QCELP),
136 STRING_TO_ENUM(AUDIO_FORMAT_MP2),
137 STRING_TO_ENUM(AUDIO_FORMAT_EVRCNW),
Amit Shekhar6f461b12014-08-01 14:52:58 -0700138 STRING_TO_ENUM(AUDIO_FORMAT_FLAC),
Satya Krishna Pindiproli70471602015-04-24 19:12:43 +0530139 STRING_TO_ENUM(AUDIO_FORMAT_ALAC),
140 STRING_TO_ENUM(AUDIO_FORMAT_APE),
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700141 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3_JOC),
Alexy Josephcd8eaed2014-12-11 12:46:53 -0800142 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LC),
143 STRING_TO_ENUM(AUDIO_FORMAT_AAC_HE_V1),
144 STRING_TO_ENUM(AUDIO_FORMAT_AAC_HE_V2),
Manish Dewangana6fc5442015-08-24 20:30:31 +0530145 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS),
Ashish Jaine513a872015-11-19 17:00:56 +0530146 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_LC),
147 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_HE_V1),
148 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_HE_V2),
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +0530149 STRING_TO_ENUM(AUDIO_FORMAT_DSD),
Arun Kumar Dasari3b174182016-12-27 13:01:14 +0530150 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM),
151 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM_LC),
152 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM_HE_V1),
153 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM_HE_V2),
Dhanalakshmi Siddani18737932016-11-29 17:33:17 +0530154 STRING_TO_ENUM(AUDIO_FORMAT_APTX),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700155#endif
156};
157
Ben Rombergera04fabc2014-11-14 12:16:03 -0800158static char bTable[BASE_TABLE_SIZE] = {
159 'A','B','C','D','E','F','G','H','I','J','K','L',
160 'M','N','O','P','Q','R','S','T','U','V','W','X',
161 'Y','Z','a','b','c','d','e','f','g','h','i','j',
162 'k','l','m','n','o','p','q','r','s','t','u','v',
163 'w','x','y','z','0','1','2','3','4','5','6','7',
164 '8','9','+','/'
165};
166
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700167static uint32_t string_to_enum(const struct string_to_enum *table, size_t size,
168 const char *name)
169{
170 size_t i;
171 for (i = 0; i < size; i++) {
172 if (strcmp(table[i].name, name) == 0) {
173 ALOGV("%s found %s", __func__, table[i].name);
174 return table[i].value;
175 }
176 }
177 return 0;
178}
179
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530180static audio_io_flags_t parse_flag_names(char *name)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700181{
182 uint32_t flag = 0;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530183 audio_io_flags_t io_flags;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800184 char *last_r;
185 char *flag_name = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700186 while (flag_name != NULL) {
187 if (strlen(flag_name) != 0) {
188 flag |= string_to_enum(s_flag_name_to_enum_table,
189 ARRAY_SIZE(s_flag_name_to_enum_table),
190 flag_name);
191 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800192 flag_name = strtok_r(NULL, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700193 }
194
195 ALOGV("parse_flag_names: flag - %d", flag);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530196 io_flags.in_flags = (audio_input_flags_t)flag;
197 io_flags.out_flags = (audio_output_flags_t)flag;
198 return io_flags;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700199}
200
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530201static void parse_format_names(char *name, struct streams_io_cfg *s_info)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700202{
203 struct stream_format *sf_info = NULL;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800204 char *last_r;
205 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700206
207 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG) == 0)
208 return;
209
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530210 list_init(&s_info->format_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700211 while (str != NULL) {
212 audio_format_t format = (audio_format_t)string_to_enum(s_format_name_to_enum_table,
213 ARRAY_SIZE(s_format_name_to_enum_table), str);
214 ALOGV("%s: format - %d", __func__, format);
215 if (format != 0) {
216 sf_info = (struct stream_format *)calloc(1, sizeof(struct stream_format));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700217 if (sf_info == NULL)
218 break; /* return whatever was parsed */
219
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700220 sf_info->format = format;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530221 list_add_tail(&s_info->format_list, &sf_info->list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700222 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800223 str = strtok_r(NULL, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700224 }
225}
226
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530227static void parse_sample_rate_names(char *name, struct streams_io_cfg *s_info)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700228{
Amit Shekhar6f461b12014-08-01 14:52:58 -0700229 struct stream_sample_rate *ss_info = NULL;
230 uint32_t sample_rate = 48000;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800231 char *last_r;
232 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700233
Amit Shekhar6f461b12014-08-01 14:52:58 -0700234 if (str != NULL && 0 == strcmp(str, DYNAMIC_VALUE_TAG))
235 return;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700236
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530237 list_init(&s_info->sample_rate_list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700238 while (str != NULL) {
239 sample_rate = (uint32_t)strtol(str, (char **)NULL, 10);
240 ALOGV("%s: sample_rate - %d", __func__, sample_rate);
241 if (0 != sample_rate) {
242 ss_info = (struct stream_sample_rate *)calloc(1, sizeof(struct stream_sample_rate));
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800243 if (!ss_info) {
244 ALOGE("%s: memory allocation failure", __func__);
245 return;
246 }
Amit Shekhar6f461b12014-08-01 14:52:58 -0700247 ss_info->sample_rate = sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530248 list_add_tail(&s_info->sample_rate_list, &ss_info->list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700249 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800250 str = strtok_r(NULL, "|", &last_r);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700251 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700252}
253
254static int parse_bit_width_names(char *name)
255{
256 int bit_width = 16;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800257 char *last_r;
258 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700259
260 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG))
261 bit_width = (int)strtol(str, (char **)NULL, 10);
262
263 ALOGV("%s: bit_width - %d", __func__, bit_width);
264 return bit_width;
265}
266
267static int parse_app_type_names(void *platform, char *name)
268{
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700269 int app_type = platform_get_default_app_type(platform);
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800270 char *last_r;
271 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700272
273 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG))
274 app_type = (int)strtol(str, (char **)NULL, 10);
275
276 ALOGV("%s: app_type - %d", __func__, app_type);
277 return app_type;
278}
279
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530280static void update_streams_cfg_list(cnode *root, void *platform,
281 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700282{
283 cnode *node = root->first_child;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530284 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700285
286 ALOGV("%s", __func__);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530287 s_info = (struct streams_io_cfg *)calloc(1, sizeof(struct streams_io_cfg));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700288
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530289 if (!s_info) {
290 ALOGE("failed to allocate mem for s_info list element");
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700291 return;
292 }
293
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700294 while (node) {
295 if (strcmp(node->name, FLAGS_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530296 s_info->flags = parse_flag_names((char *)node->value);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530297 } else if (strcmp(node->name, PROFILES_TAG) == 0) {
298 strlcpy(s_info->profile, (char *)node->value, sizeof(s_info->profile));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700299 } else if (strcmp(node->name, FORMATS_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530300 parse_format_names((char *)node->value, s_info);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700301 } else if (strcmp(node->name, SAMPLING_RATES_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530302 s_info->app_type_cfg.sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
303 parse_sample_rate_names((char *)node->value, s_info);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700304 } else if (strcmp(node->name, BIT_WIDTH_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530305 s_info->app_type_cfg.bit_width = parse_bit_width_names((char *)node->value);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700306 } else if (strcmp(node->name, APP_TYPE_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530307 s_info->app_type_cfg.app_type = parse_app_type_names(platform, (char *)node->value);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700308 }
309 node = node->next;
310 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530311 list_add_tail(streams_cfg_list, &s_info->list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700312}
313
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530314static void load_cfg_list(cnode *root, void *platform,
315 struct listnode *streams_output_cfg_list,
316 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700317{
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530318 cnode *node = NULL;
319
320 node = config_find(root, OUTPUTS_TAG);
321 if (node != NULL) {
322 node = node->first_child;
323 while (node) {
324 ALOGV("%s: loading output %s", __func__, node->name);
325 update_streams_cfg_list(node, platform, streams_output_cfg_list);
326 node = node->next;
327 }
328 } else {
329 ALOGI("%s: could not load output, node is NULL", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700330 }
331
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530332 node = config_find(root, INPUTS_TAG);
333 if (node != NULL) {
334 node = node->first_child;
335 while (node) {
336 ALOGV("%s: loading input %s", __func__, node->name);
337 update_streams_cfg_list(node, platform, streams_input_cfg_list);
338 node = node->next;
339 }
340 } else {
341 ALOGI("%s: could not load input, node is NULL", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700342 }
343}
344
345static void send_app_type_cfg(void *platform, struct mixer *mixer,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530346 struct listnode *streams_output_cfg_list,
347 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700348{
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530349 size_t app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT] = {0};
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700350 int length = 0, i, num_app_types = 0;
351 struct listnode *node;
352 bool update;
353 struct mixer_ctl *ctl = NULL;
354 const char *mixer_ctl_name = "App Type Config";
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530355 struct streams_io_cfg *s_info = NULL;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700356
357 if (!mixer) {
358 ALOGE("%s: mixer is null",__func__);
359 return;
360 }
361 ctl = mixer_get_ctl_by_name(mixer, mixer_ctl_name);
362 if (!ctl) {
363 ALOGE("%s: Could not get ctl for mixer cmd - %s",__func__, mixer_ctl_name);
364 return;
365 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530366 app_type_cfg[length++] = num_app_types;
367
368 if (list_empty(streams_output_cfg_list)) {
369 app_type_cfg[length++] = platform_get_default_app_type_v2(platform, PCM_PLAYBACK);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700370 app_type_cfg[length++] = 48000;
371 app_type_cfg[length++] = 16;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530372 num_app_types += 1;
373 }
374 if (list_empty(streams_input_cfg_list)) {
375 app_type_cfg[length++] = platform_get_default_app_type_v2(platform, PCM_CAPTURE);
376 app_type_cfg[length++] = 48000;
377 app_type_cfg[length++] = 16;
378 num_app_types += 1;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700379 }
380
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700381 list_for_each(node, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530382 s_info = node_to_item(node, struct streams_io_cfg, list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700383 update = true;
384 for (i=0; i<length; i=i+3) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530385 if (app_type_cfg[i+1] == 0)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700386 break;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530387 else if (app_type_cfg[i+1] == (size_t)s_info->app_type_cfg.app_type) {
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530388 if (app_type_cfg[i+2] < (size_t)s_info->app_type_cfg.sample_rate)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530389 app_type_cfg[i+2] = s_info->app_type_cfg.sample_rate;
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530390 if (app_type_cfg[i+3] < (size_t)s_info->app_type_cfg.bit_width)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530391 app_type_cfg[i+3] = s_info->app_type_cfg.bit_width;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700392 update = false;
393 break;
394 }
395 }
396 if (update && ((length + 3) <= MAX_LENGTH_MIXER_CONTROL_IN_INT)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530397 num_app_types += 1;
398 app_type_cfg[length++] = s_info->app_type_cfg.app_type;
399 app_type_cfg[length++] = s_info->app_type_cfg.sample_rate;
400 app_type_cfg[length++] = s_info->app_type_cfg.bit_width;
401 }
402 }
403 list_for_each(node, streams_input_cfg_list) {
404 s_info = node_to_item(node, struct streams_io_cfg, list);
405 update = true;
406 for (i=0; i<length; i=i+3) {
407 if (app_type_cfg[i+1] == 0)
408 break;
409 else if (app_type_cfg[i+1] == (size_t)s_info->app_type_cfg.app_type) {
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530410 if (app_type_cfg[i+2] < (size_t)s_info->app_type_cfg.sample_rate)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530411 app_type_cfg[i+2] = s_info->app_type_cfg.sample_rate;
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530412 if (app_type_cfg[i+3] < (size_t)s_info->app_type_cfg.bit_width)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530413 app_type_cfg[i+3] = s_info->app_type_cfg.bit_width;
414 update = false;
415 break;
416 }
417 }
418 if (update && ((length + 3) <= MAX_LENGTH_MIXER_CONTROL_IN_INT)) {
419 num_app_types += 1;
420 app_type_cfg[length++] = s_info->app_type_cfg.app_type;
421 app_type_cfg[length++] = s_info->app_type_cfg.sample_rate;
422 app_type_cfg[length++] = s_info->app_type_cfg.bit_width;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700423 }
424 }
425 ALOGV("%s: num_app_types: %d", __func__, num_app_types);
426 if (num_app_types) {
427 app_type_cfg[0] = num_app_types;
428 mixer_ctl_set_array(ctl, app_type_cfg, length);
429 }
430}
431
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530432void audio_extn_utils_update_streams_cfg_lists(void *platform,
433 struct mixer *mixer,
434 struct listnode *streams_output_cfg_list,
435 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700436{
437 cnode *root;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530438 char *data = NULL;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700439
440 ALOGV("%s", __func__);
441 list_init(streams_output_cfg_list);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530442 list_init(streams_input_cfg_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700443
444 root = config_node("", "");
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700445 if (root == NULL) {
446 ALOGE("cfg_list, NULL config root");
447 return;
448 }
449
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530450 data = (char *)load_file(AUDIO_IO_POLICY_VENDOR_CONFIG_FILE, NULL);
451 if (data == NULL) {
452 ALOGD("%s: failed to open io config file(%s), trying older config file",
453 __func__, AUDIO_IO_POLICY_VENDOR_CONFIG_FILE);
454 data = (char *)load_file(AUDIO_OUTPUT_POLICY_VENDOR_CONFIG_FILE, NULL);
455 if (data == NULL) {
456 send_app_type_cfg(platform, mixer,
457 streams_output_cfg_list,
458 streams_input_cfg_list);
459 ALOGE("%s: could not load io policy config!", __func__);
460 return;
461 }
462 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700463
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530464 config_load(root, data);
465 load_cfg_list(root, platform, streams_output_cfg_list,
466 streams_input_cfg_list);
467
468 send_app_type_cfg(platform, mixer, streams_output_cfg_list,
469 streams_input_cfg_list);
Alexy Josephaee4fdd2016-01-29 13:02:07 -0800470
471 config_free(root);
472 free(data);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700473}
474
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530475static void audio_extn_utils_dump_streams_cfg_list(
476 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700477{
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700478 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530479 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700480 struct stream_format *sf_info;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700481 struct stream_sample_rate *ss_info;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530482
483 list_for_each(node_i, streams_cfg_list) {
484 s_info = node_to_item(node_i, struct streams_io_cfg, list);
485 ALOGV("%s: flags-%d, sample_rate-%d, bit_width-%d, app_type-%d",
486 __func__, s_info->flags.out_flags, s_info->app_type_cfg.sample_rate,
487 s_info->app_type_cfg.bit_width, s_info->app_type_cfg.app_type);
488 list_for_each(node_j, &s_info->format_list) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700489 sf_info = node_to_item(node_j, struct stream_format, list);
490 ALOGV("format-%x", sf_info->format);
491 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530492 list_for_each(node_j, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700493 ss_info = node_to_item(node_j, struct stream_sample_rate, list);
494 ALOGV("sample rate-%d", ss_info->sample_rate);
495 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700496 }
497}
498
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530499void audio_extn_utils_dump_streams_cfg_lists(
500 struct listnode *streams_output_cfg_list,
501 struct listnode *streams_input_cfg_list)
502{
503 ALOGV("%s", __func__);
504 audio_extn_utils_dump_streams_cfg_list(streams_output_cfg_list);
505 audio_extn_utils_dump_streams_cfg_list(streams_input_cfg_list);
506}
507
508static void audio_extn_utils_release_streams_cfg_list(
509 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700510{
511 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530512 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700513
514 ALOGV("%s", __func__);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530515
516 while (!list_empty(streams_cfg_list)) {
517 node_i = list_head(streams_cfg_list);
518 s_info = node_to_item(node_i, struct streams_io_cfg, list);
519 while (!list_empty(&s_info->format_list)) {
520 node_j = list_head(&s_info->format_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700521 list_remove(node_j);
522 free(node_to_item(node_j, struct stream_format, list));
523 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530524 while (!list_empty(&s_info->sample_rate_list)) {
525 node_j = list_head(&s_info->sample_rate_list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700526 list_remove(node_j);
527 free(node_to_item(node_j, struct stream_sample_rate, list));
528 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700529 list_remove(node_i);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530530 free(node_to_item(node_i, struct streams_io_cfg, list));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700531 }
532}
533
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530534void audio_extn_utils_release_streams_cfg_lists(
535 struct listnode *streams_output_cfg_list,
536 struct listnode *streams_input_cfg_list)
537{
538 ALOGV("%s", __func__);
539 audio_extn_utils_release_streams_cfg_list(streams_output_cfg_list);
540 audio_extn_utils_release_streams_cfg_list(streams_input_cfg_list);
541}
542
543static bool set_app_type_cfg(struct streams_io_cfg *s_info,
544 struct stream_app_type_cfg *app_type_cfg,
545 uint32_t sample_rate, uint32_t bit_width)
Amit Shekhar6f461b12014-08-01 14:52:58 -0700546 {
547 struct listnode *node_i;
548 struct stream_sample_rate *ss_info;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530549 list_for_each(node_i, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700550 ss_info = node_to_item(node_i, struct stream_sample_rate, list);
551 if ((sample_rate <= ss_info->sample_rate) &&
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530552 (bit_width == s_info->app_type_cfg.bit_width)) {
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700553
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530554 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700555 app_type_cfg->sample_rate = ss_info->sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530556 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700557 ALOGV("%s app_type_cfg->app_type %d, app_type_cfg->sample_rate %d, app_type_cfg->bit_width %d",
558 __func__, app_type_cfg->app_type, app_type_cfg->sample_rate, app_type_cfg->bit_width);
559 return true;
560 }
561 }
562 /*
563 * Reiterate through the list assuming dafault sample rate.
564 * Handles scenario where input sample rate is higher
565 * than all sample rates in list for the input bit width.
566 */
567 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
Apoorv Raghuvanshif59bb222015-02-18 12:23:23 -0800568
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530569 list_for_each(node_i, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700570 ss_info = node_to_item(node_i, struct stream_sample_rate, list);
571 if ((sample_rate <= ss_info->sample_rate) &&
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530572 (bit_width == s_info->app_type_cfg.bit_width)) {
573 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700574 app_type_cfg->sample_rate = sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530575 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Apoorv Raghuvanshif59bb222015-02-18 12:23:23 -0800576 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 -0700577 __func__, app_type_cfg->app_type, app_type_cfg->sample_rate, app_type_cfg->bit_width);
578 return true;
579 }
580 }
581 return false;
582}
583
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530584void audio_extn_utils_update_stream_input_app_type_cfg(void *platform,
585 struct listnode *streams_input_cfg_list,
586 audio_devices_t devices __unused,
587 audio_input_flags_t flags,
588 audio_format_t format,
589 uint32_t sample_rate,
590 uint32_t bit_width,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530591 char* profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530592 struct stream_app_type_cfg *app_type_cfg)
593{
594 struct listnode *node_i, *node_j;
595 struct streams_io_cfg *s_info;
596 struct stream_format *sf_info;
597
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530598 ALOGV("%s: flags: 0x%x, format: 0x%x sample_rate %d, profile %s",
599 __func__, flags, format, sample_rate, profile);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530600
601 list_for_each(node_i, streams_input_cfg_list) {
602 s_info = node_to_item(node_i, struct streams_io_cfg, list);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530603 /* Along with flags do profile matching if set at either end.*/
604 if (s_info->flags.in_flags == flags &&
605 ((profile[0] == '\0' && s_info->profile[0] == '\0') ||
606 strncmp(s_info->profile, profile, sizeof(s_info->profile)) == 0)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530607 list_for_each(node_j, &s_info->format_list) {
608 sf_info = node_to_item(node_j, struct stream_format, list);
609 if (sf_info->format == format) {
610 if (set_app_type_cfg(s_info, app_type_cfg, sample_rate, bit_width))
611 return;
612 }
613 }
614 }
615 }
616 ALOGW("%s: App type could not be selected. Falling back to default", __func__);
617 app_type_cfg->app_type = platform_get_default_app_type_v2(platform, PCM_CAPTURE);
618 app_type_cfg->sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
619 app_type_cfg->bit_width = 16;
620}
621
622void audio_extn_utils_update_stream_output_app_type_cfg(void *platform,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700623 struct listnode *streams_output_cfg_list,
Amit Shekhar1d896042014-10-03 13:16:09 -0700624 audio_devices_t devices,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700625 audio_output_flags_t flags,
626 audio_format_t format,
Amit Shekhar6f461b12014-08-01 14:52:58 -0700627 uint32_t sample_rate,
628 uint32_t bit_width,
Manish Dewangan837dc462015-05-27 10:17:41 +0530629 audio_channel_mask_t channel_mask,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530630 char *profile,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700631 struct stream_app_type_cfg *app_type_cfg)
632{
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +0530633 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530634 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700635 struct stream_format *sf_info;
Manish Dewangan837dc462015-05-27 10:17:41 +0530636 char value[PROPERTY_VALUE_MAX] = {0};
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700637
Ashish Jain058165c2016-09-28 23:18:48 +0530638 if ((bit_width >= 24) &&
Amit Shekhar1d896042014-10-03 13:16:09 -0700639 (devices & AUDIO_DEVICE_OUT_SPEAKER)) {
Amit Shekhar5a39c912014-10-14 15:39:30 -0700640 int32_t bw = platform_get_snd_device_bit_width(SND_DEVICE_OUT_SPEAKER);
641 if (-ENOSYS != bw)
642 bit_width = (uint32_t)bw;
Amit Shekhar1d896042014-10-03 13:16:09 -0700643 sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
644 ALOGI("%s Allowing 24-bit playback on speaker ONLY at default sampling rate", __func__);
645 }
646
Manish Dewangan837dc462015-05-27 10:17:41 +0530647 property_get("audio.playback.mch.downsample",value,"");
648 if (!strncmp("true", value, sizeof("true"))) {
649 if ((popcount(channel_mask) > 2) &&
650 (sample_rate > CODEC_BACKEND_DEFAULT_SAMPLE_RATE) &&
651 !(flags & AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH)) {
652 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
653 ALOGD("%s: MCH session defaulting sample rate to %d",
654 __func__, sample_rate);
655 }
656 }
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +0530657
658 /* Set sampling rate to 176.4 for DSD64
659 * and 352.8Khz for DSD128.
660 * Set Bit Width to 16. output will be 16 bit
661 * post DoP in ASM.
662 */
663 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH) &&
664 (format == AUDIO_FORMAT_DSD)) {
665 bit_width = 16;
666 if (sample_rate == INPUT_SAMPLING_RATE_DSD64)
667 sample_rate = OUTPUT_SAMPLING_RATE_DSD64;
668 else if (sample_rate == INPUT_SAMPLING_RATE_DSD128)
669 sample_rate = OUTPUT_SAMPLING_RATE_DSD128;
670 }
671
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +0530672 if(devices & AUDIO_DEVICE_OUT_ALL_A2DP) {
673 //TODO: Handle fractional sampling rate configuration for LL
674 audio_extn_a2dp_get_apptype_params(&sample_rate, &bit_width);
675 ALOGI("%s using %d sampling rate %d bit width for A2DP CoPP",
676 __func__, sample_rate, bit_width);
677 }
678
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530679 ALOGV("%s: flags: %x, format: %x sample_rate %d, profile %s",
680 __func__, flags, format, sample_rate, profile);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700681 list_for_each(node_i, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530682 s_info = node_to_item(node_i, struct streams_io_cfg, list);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530683 /* Along with flags do profile matching if set at either end.*/
684 if (s_info->flags.out_flags == flags &&
685 ((profile[0] == '\0' && s_info->profile[0] == '\0') ||
686 strncmp(s_info->profile, profile, sizeof(s_info->profile)) == 0)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530687 list_for_each(node_j, &s_info->format_list) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700688 sf_info = node_to_item(node_j, struct stream_format, list);
689 if (sf_info->format == format) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530690 if (set_app_type_cfg(s_info, app_type_cfg, sample_rate, bit_width))
Amit Shekhar6f461b12014-08-01 14:52:58 -0700691 return;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700692 }
693 }
694 }
695 }
696 list_for_each(node_i, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530697 s_info = node_to_item(node_i, struct streams_io_cfg, list);
698 if (s_info->flags.out_flags == AUDIO_OUTPUT_FLAG_PRIMARY) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700699 ALOGV("Compatible output profile not found.");
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530700 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
701 app_type_cfg->sample_rate = s_info->app_type_cfg.sample_rate;
702 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700703 ALOGV("%s Default to primary output: App type: %d sample_rate %d",
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530704 __func__, s_info->app_type_cfg.app_type, app_type_cfg->sample_rate);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700705 return;
706 }
707 }
708 ALOGW("%s: App type could not be selected. Falling back to default", __func__);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700709 app_type_cfg->app_type = platform_get_default_app_type(platform);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700710 app_type_cfg->sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700711 app_type_cfg->bit_width = 16;
712}
713
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +0530714static bool audio_is_this_native_usecase(struct audio_usecase *uc)
715{
716 bool native_usecase = false;
717 struct stream_out *out = (struct stream_out*) uc->stream.out;
718
719 if (PCM_PLAYBACK == uc->type && out != NULL &&
720 NATIVE_AUDIO_MODE_INVALID != platform_get_native_support() &&
721 is_offload_usecase(uc->id) &&
722 (out->sample_rate == OUTPUT_SAMPLING_RATE_44100))
723 native_usecase = true;
724
725 return native_usecase;
726}
727
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530728void audio_extn_utils_update_stream_app_type_cfg_for_usecase(
729 struct audio_device *adev,
730 struct audio_usecase *usecase)
731{
732 ALOGV("%s", __func__);
733
734 switch(usecase->type) {
735 case PCM_PLAYBACK:
736 audio_extn_utils_update_stream_output_app_type_cfg(adev->platform,
737 &adev->streams_output_cfg_list,
738 usecase->stream.out->devices,
739 usecase->stream.out->flags,
740 usecase->stream.out->format,
741 usecase->stream.out->sample_rate,
742 usecase->stream.out->bit_width,
743 usecase->stream.out->channel_mask,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530744 usecase->stream.out->profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530745 &usecase->stream.out->app_type_cfg);
746 ALOGV("%s Selected apptype: %d", __func__, usecase->stream.out->app_type_cfg.app_type);
747 break;
748 case PCM_CAPTURE:
749 audio_extn_utils_update_stream_input_app_type_cfg(adev->platform,
750 &adev->streams_input_cfg_list,
751 usecase->stream.in->device,
752 usecase->stream.in->flags,
753 usecase->stream.in->format,
754 usecase->stream.in->sample_rate,
755 usecase->stream.in->bit_width,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530756 usecase->stream.in->profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530757 &usecase->stream.in->app_type_cfg);
758 ALOGV("%s Selected apptype: %d", __func__, usecase->stream.in->app_type_cfg.app_type);
759 break;
760 default:
761 ALOGE("%s: app type cfg not supported for usecase type (%d)",
762 __func__, usecase->type);
763 }
764}
765
Siena Richard7c2db772016-12-21 11:32:34 -0800766static int send_app_type_cfg_for_device(struct audio_device *adev,
767 struct audio_usecase *usecase,
768 int split_snd_device)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700769{
770 char mixer_ctl_name[MAX_LENGTH_MIXER_CONTROL_IN_INT];
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530771 size_t app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT] = {0};
772 int len = 0, rc;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700773 struct mixer_ctl *ctl;
Siena Richard7c2db772016-12-21 11:32:34 -0800774 int pcm_device_id = 0, acdb_dev_id, app_type;
775 int snd_device = split_snd_device, snd_device_be_idx = -1;
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +0530776 int32_t sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Manish Dewangan837dc462015-05-27 10:17:41 +0530777 char value[PROPERTY_VALUE_MAX] = {0};
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700778
Siena Richard7c2db772016-12-21 11:32:34 -0800779 ALOGV("%s: usecase->out_snd_device %s, usecase->in_snd_device %s, split_snd_device %s",
780 __func__, platform_get_snd_device_name(usecase->out_snd_device),
781 platform_get_snd_device_name(usecase->in_snd_device),
782 platform_get_snd_device_name(split_snd_device));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700783
Srikanth Uyyala9d551402015-08-25 16:03:42 +0530784 if (usecase->type != PCM_PLAYBACK && usecase->type != PCM_CAPTURE) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530785 ALOGE("%s: not a playback/capture path, no need to cfg app type", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700786 rc = 0;
787 goto exit_send_app_type_cfg;
788 }
789 if ((usecase->id != USECASE_AUDIO_PLAYBACK_DEEP_BUFFER) &&
790 (usecase->id != USECASE_AUDIO_PLAYBACK_LOW_LATENCY) &&
791 (usecase->id != USECASE_AUDIO_PLAYBACK_MULTI_CH) &&
Srikanth Uyyala9d551402015-08-25 16:03:42 +0530792 (!is_offload_usecase(usecase->id)) &&
793 (usecase->type != PCM_CAPTURE)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530794 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 -0700795 rc = 0;
796 goto exit_send_app_type_cfg;
797 }
Srikanth Uyyala9d551402015-08-25 16:03:42 +0530798 if (usecase->type == PCM_PLAYBACK) {
Ben Romberger1fafdde2015-09-09 19:43:15 -0700799 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_PLAYBACK);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530800 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
801 "Audio Stream %d App Type Cfg", pcm_device_id);
Ben Romberger1fafdde2015-09-09 19:43:15 -0700802 } else if (usecase->type == PCM_CAPTURE) {
Ben Romberger1fafdde2015-09-09 19:43:15 -0700803 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_CAPTURE);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530804 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
805 "Audio Stream Capture %d App Type Cfg", pcm_device_id);
Srikanth Uyyala9d551402015-08-25 16:03:42 +0530806 }
Siena Richard7c2db772016-12-21 11:32:34 -0800807
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700808 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
809 if (!ctl) {
810 ALOGE("%s: Could not get ctl for mixer cmd - %s", __func__,
811 mixer_ctl_name);
812 rc = -EINVAL;
813 goto exit_send_app_type_cfg;
814 }
815 snd_device = (snd_device == SND_DEVICE_OUT_SPEAKER) ?
Xiaojun Sang040cc9f2015-08-03 19:38:28 +0800816 platform_get_spkr_prot_snd_device(snd_device) : snd_device;
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +0530817
Rohit Kumar1181b292017-01-31 18:07:17 +0530818 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
819 if (acdb_dev_id <= 0) {
820 ALOGE("%s: Couldn't get the acdb dev id", __func__);
821 rc = -EINVAL;
822 goto exit_send_app_type_cfg;
823 }
824
Siena Richard7c2db772016-12-21 11:32:34 -0800825 snd_device_be_idx = platform_get_snd_device_backend_index(snd_device);
826 if (snd_device_be_idx < 0) {
827 ALOGE("%s: Couldn't get the backend index for snd device %s ret=%d",
828 __func__, platform_get_snd_device_name(snd_device),
829 snd_device_be_idx);
830 }
831
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530832 if ((usecase->type == PCM_PLAYBACK) && (usecase->stream.out != NULL)) {
833 if (usecase->stream.out->devices & AUDIO_DEVICE_OUT_SPEAKER) {
834 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
835 } else if ((usecase->stream.out->app_type_cfg.sample_rate == OUTPUT_SAMPLING_RATE_44100 &&
836 !(audio_is_this_native_usecase(usecase))) ||
837 (usecase->stream.out->sample_rate < OUTPUT_SAMPLING_RATE_44100)) {
838 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
839 }
Venkata Narendra Kumar Gutta4bd09d02016-01-29 15:31:04 +0530840
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530841 sample_rate = usecase->stream.out->app_type_cfg.sample_rate;
Manish Dewangan837dc462015-05-27 10:17:41 +0530842
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530843 property_get("audio.playback.mch.downsample",value,"");
844 if (!strncmp("true", value, sizeof("true"))) {
845 if ((popcount(usecase->stream.out->channel_mask) > 2) &&
846 (usecase->stream.out->app_type_cfg.sample_rate > CODEC_BACKEND_DEFAULT_SAMPLE_RATE) &&
847 !(usecase->stream.out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH))
848 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
849 }
Ashish Jainc02430d2016-01-04 10:42:43 +0530850
Mingming Yin21854652016-04-13 11:54:02 -0700851 if ((24 == usecase->stream.out->bit_width) &&
852 (usecase->stream.out->devices & AUDIO_DEVICE_OUT_SPEAKER)) {
853 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Ashish Jaina052e572016-11-07 16:41:07 +0530854 } else if ((snd_device == SND_DEVICE_OUT_HDMI ||
855 snd_device == SND_DEVICE_OUT_USB_HEADSET ||
856 snd_device == SND_DEVICE_OUT_DISPLAY_PORT) &&
857 (usecase->stream.out->sample_rate >= OUTPUT_SAMPLING_RATE_44100)) {
858 /*
859 * To best utlize DSP, check if the stream sample rate is supported/multiple of
860 * configured device sample rate, if not update the COPP rate to be equal to the
861 * device sample rate, else open COPP at stream sample rate
862 */
863 platform_check_and_update_copp_sample_rate(adev->platform, snd_device,
864 usecase->stream.out->sample_rate,
865 &usecase->stream.out->app_type_cfg.sample_rate);
Mingming Yin21854652016-04-13 11:54:02 -0700866 } else if ((snd_device != SND_DEVICE_OUT_HEADPHONES_44_1 &&
867 usecase->stream.out->sample_rate == OUTPUT_SAMPLING_RATE_44100) ||
868 (usecase->stream.out->sample_rate < OUTPUT_SAMPLING_RATE_44100)) {
869 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
870 }
871 sample_rate = usecase->stream.out->app_type_cfg.sample_rate;
872
Siena Richard7c2db772016-12-21 11:32:34 -0800873 app_type = usecase->stream.out->app_type_cfg.app_type;
874 app_type_cfg[len++] = app_type;
Mingming Yin21854652016-04-13 11:54:02 -0700875 app_type_cfg[len++] = acdb_dev_id;
876 if (((usecase->stream.out->format == AUDIO_FORMAT_E_AC3) ||
877 (usecase->stream.out->format == AUDIO_FORMAT_E_AC3_JOC))
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530878 && audio_extn_passthru_is_passthrough_stream(usecase->stream.out)) {
Mingming Yin21854652016-04-13 11:54:02 -0700879 app_type_cfg[len++] = sample_rate * 4;
880 } else {
881 app_type_cfg[len++] = sample_rate;
882 }
Siena Richard7c2db772016-12-21 11:32:34 -0800883 if (snd_device_be_idx > 0)
884 app_type_cfg[len++] = snd_device_be_idx;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530885
Siena Richard7c2db772016-12-21 11:32:34 -0800886 ALOGI("%s PLAYBACK app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
887 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530888
889 } else if ((usecase->type == PCM_CAPTURE) && (usecase->stream.in != NULL)) {
Siena Richard7c2db772016-12-21 11:32:34 -0800890 app_type = usecase->stream.in->app_type_cfg.app_type;
891 app_type_cfg[len++] = app_type;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530892 app_type_cfg[len++] = acdb_dev_id;
Siena Richard7c2db772016-12-21 11:32:34 -0800893 sample_rate = usecase->stream.in->app_type_cfg.sample_rate;
894 app_type_cfg[len++] = sample_rate;
895 if (snd_device_be_idx > 0)
896 app_type_cfg[len++] = snd_device_be_idx;
897 ALOGI("%s CAPTURE app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
898 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530899 } else {
Siena Richard7c2db772016-12-21 11:32:34 -0800900 app_type = platform_get_default_app_type_v2(adev->platform, usecase->type);
901 app_type_cfg[len++] = app_type;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530902 app_type_cfg[len++] = acdb_dev_id;
903 app_type_cfg[len++] = sample_rate;
Siena Richard7c2db772016-12-21 11:32:34 -0800904 if (snd_device_be_idx > 0)
905 app_type_cfg[len++] = snd_device_be_idx;
906 ALOGI("%s default app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
907 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Ashish Jainc02430d2016-01-04 10:42:43 +0530908 }
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +0530909
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700910 mixer_ctl_set_array(ctl, app_type_cfg, len);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700911 rc = 0;
912exit_send_app_type_cfg:
913 return rc;
914}
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700915
Siena Richard7c2db772016-12-21 11:32:34 -0800916int audio_extn_utils_send_app_type_cfg(struct audio_device *adev,
917 struct audio_usecase *usecase)
918{
919 int i, num_devices = 0;
920 snd_device_t new_snd_devices[SND_DEVICE_OUT_END] = {0};
921 int rc = 0;
922
923 switch (usecase->type) {
924 case PCM_PLAYBACK:
925 ALOGD("%s: usecase->out_snd_device %s",
926 __func__, platform_get_snd_device_name(usecase->out_snd_device));
927 /* check for out combo device */
928 if (platform_split_snd_device(adev->platform,
929 usecase->out_snd_device,
930 &num_devices, new_snd_devices)) {
931 new_snd_devices[0] = usecase->out_snd_device;
932 num_devices = 1;
933 }
934 break;
935 case PCM_CAPTURE:
936 ALOGD("%s: usecase->in_snd_device %s",
937 __func__, platform_get_snd_device_name(usecase->in_snd_device));
938 /* check for in combo device */
939 if (platform_split_snd_device(adev->platform,
940 usecase->in_snd_device,
941 &num_devices, new_snd_devices)) {
942 new_snd_devices[0] = usecase->in_snd_device;
943 num_devices = 1;
944 }
945 break;
946 default:
947 ALOGI("%s: not a playback/capture path, no need to cfg app type", __func__);
948 rc = 0;
949 break;
950 }
951
952 for (i = 0; i < num_devices; i++) {
953 rc = send_app_type_cfg_for_device(adev, usecase, new_snd_devices[i]);
954 if (rc)
955 break;
956 }
957
958 return rc;
959}
960
Preetam Singh Ranawat9519e9c2015-11-18 16:05:55 +0530961int read_line_from_file(const char *path, char *buf, size_t count)
962{
963 char * fgets_ret;
964 FILE * fd;
965 int rv;
966
967 fd = fopen(path, "r");
968 if (fd == NULL)
969 return -1;
970
971 fgets_ret = fgets(buf, (int)count, fd);
972 if (NULL != fgets_ret) {
973 rv = (int)strlen(buf);
974 } else {
975 rv = ferror(fd);
976 }
977 fclose(fd);
978
979 return rv;
980}
981
Ashish Jainf1eaa582016-05-23 20:54:24 +0530982/*Translates ALSA formats to AOSP PCM formats*/
983audio_format_t alsa_format_to_hal(uint32_t alsa_format)
984{
985 audio_format_t format;
986
987 switch(alsa_format) {
988 case SNDRV_PCM_FORMAT_S16_LE:
989 format = AUDIO_FORMAT_PCM_16_BIT;
990 break;
991 case SNDRV_PCM_FORMAT_S24_3LE:
992 format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
993 break;
994 case SNDRV_PCM_FORMAT_S24_LE:
995 format = AUDIO_FORMAT_PCM_8_24_BIT;
996 break;
997 case SNDRV_PCM_FORMAT_S32_LE:
998 format = AUDIO_FORMAT_PCM_32_BIT;
999 break;
1000 default:
1001 ALOGW("Incorrect ALSA format");
1002 format = AUDIO_FORMAT_INVALID;
1003 }
1004 return format;
1005}
1006
1007/*Translates hal format (AOSP) to alsa formats*/
1008uint32_t hal_format_to_alsa(audio_format_t hal_format)
1009{
1010 uint32_t alsa_format;
1011
1012 switch (hal_format) {
1013 case AUDIO_FORMAT_PCM_32_BIT: {
1014 if (platform_supports_true_32bit())
1015 alsa_format = SNDRV_PCM_FORMAT_S32_LE;
1016 else
1017 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1018 }
1019 break;
1020 case AUDIO_FORMAT_PCM_8_BIT:
1021 alsa_format = SNDRV_PCM_FORMAT_S8;
1022 break;
1023 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
1024 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1025 break;
1026 case AUDIO_FORMAT_PCM_8_24_BIT: {
1027 if (platform_supports_true_32bit())
1028 alsa_format = SNDRV_PCM_FORMAT_S32_LE;
1029 else
1030 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1031 }
1032 break;
1033 case AUDIO_FORMAT_PCM_FLOAT:
1034 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1035 break;
1036 default:
1037 case AUDIO_FORMAT_PCM_16_BIT:
1038 alsa_format = SNDRV_PCM_FORMAT_S16_LE;
1039 break;
1040 }
1041 return alsa_format;
1042}
1043
Ashish Jain83a6cc22016-06-28 14:34:17 +05301044/*Translates PCM formats to AOSP formats*/
1045audio_format_t pcm_format_to_hal(uint32_t pcm_format)
1046{
1047 audio_format_t format = AUDIO_FORMAT_INVALID;
1048
1049 switch(pcm_format) {
1050 case PCM_FORMAT_S16_LE:
1051 format = AUDIO_FORMAT_PCM_16_BIT;
1052 break;
1053 case PCM_FORMAT_S24_3LE:
1054 format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
1055 break;
1056 case PCM_FORMAT_S24_LE:
1057 format = AUDIO_FORMAT_PCM_8_24_BIT;
1058 break;
1059 case PCM_FORMAT_S32_LE:
1060 format = AUDIO_FORMAT_PCM_32_BIT;
1061 break;
1062 default:
1063 ALOGW("Incorrect PCM format");
1064 format = AUDIO_FORMAT_INVALID;
1065 }
1066 return format;
1067}
1068
1069/*Translates hal format (AOSP) to alsa formats*/
1070uint32_t hal_format_to_pcm(audio_format_t hal_format)
1071{
1072 uint32_t pcm_format;
1073
1074 switch (hal_format) {
1075 case AUDIO_FORMAT_PCM_32_BIT:
1076 case AUDIO_FORMAT_PCM_8_24_BIT:
1077 case AUDIO_FORMAT_PCM_FLOAT: {
1078 if (platform_supports_true_32bit())
1079 pcm_format = PCM_FORMAT_S32_LE;
1080 else
1081 pcm_format = PCM_FORMAT_S24_3LE;
1082 }
1083 break;
1084 case AUDIO_FORMAT_PCM_8_BIT:
1085 pcm_format = PCM_FORMAT_S8;
1086 break;
1087 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
1088 pcm_format = PCM_FORMAT_S24_3LE;
1089 break;
1090 default:
1091 case AUDIO_FORMAT_PCM_16_BIT:
1092 pcm_format = PCM_FORMAT_S16_LE;
1093 break;
1094 }
1095 return pcm_format;
1096}
1097
Ashish Jainf1eaa582016-05-23 20:54:24 +05301098uint32_t get_alsa_fragment_size(uint32_t bytes_per_sample,
1099 uint32_t sample_rate,
1100 uint32_t noOfChannels)
1101{
1102 uint32_t fragment_size = 0;
1103 uint32_t pcm_offload_time = PCM_OFFLOAD_BUFFER_DURATION;
1104
1105 fragment_size = (pcm_offload_time
1106 * sample_rate
1107 * bytes_per_sample
1108 * noOfChannels)/1000;
1109 if (fragment_size < MIN_PCM_OFFLOAD_FRAGMENT_SIZE)
1110 fragment_size = MIN_PCM_OFFLOAD_FRAGMENT_SIZE;
1111 else if (fragment_size > MAX_PCM_OFFLOAD_FRAGMENT_SIZE)
1112 fragment_size = MAX_PCM_OFFLOAD_FRAGMENT_SIZE;
1113 /*To have same PCM samples for all channels, the buffer size requires to
1114 *be multiple of (number of channels * bytes per sample)
1115 *For writes to succeed, the buffer must be written at address which is multiple of 32
1116 */
1117 fragment_size = ALIGN(fragment_size, (bytes_per_sample * noOfChannels * 32));
1118
1119 ALOGI("PCM offload Fragment size to %d bytes", fragment_size);
1120 return fragment_size;
1121}
1122
1123/* Calculates the fragment size required to configure compress session.
1124 * Based on the alsa format selected, decide if conversion is needed in
1125
1126 * HAL ( e.g. convert AUDIO_FORMAT_PCM_FLOAT input format to
1127 * AUDIO_FORMAT_PCM_24_BIT_PACKED before writing to the compress driver.
1128 */
1129void audio_extn_utils_update_direct_pcm_fragment_size(struct stream_out *out)
1130{
Ashish Jain83a6cc22016-06-28 14:34:17 +05301131 audio_format_t dst_format = out->hal_op_format;
1132 audio_format_t src_format = out->hal_ip_format;
Ashish Jainf1eaa582016-05-23 20:54:24 +05301133 uint32_t hal_op_bytes_per_sample = audio_bytes_per_sample(dst_format);
1134 uint32_t hal_ip_bytes_per_sample = audio_bytes_per_sample(src_format);
1135
1136 out->compr_config.fragment_size =
1137 get_alsa_fragment_size(hal_op_bytes_per_sample,
1138 out->sample_rate,
1139 popcount(out->channel_mask));
1140
1141 if ((src_format != dst_format) &&
1142 hal_op_bytes_per_sample != hal_ip_bytes_per_sample) {
1143
Ashish Jain83a6cc22016-06-28 14:34:17 +05301144 out->hal_fragment_size =
Ashish Jainf1eaa582016-05-23 20:54:24 +05301145 ((out->compr_config.fragment_size * hal_ip_bytes_per_sample) /
1146 hal_op_bytes_per_sample);
1147 ALOGI("enable conversion hal_input_fragment_size is %d src_format %x dst_format %x",
Ashish Jain83a6cc22016-06-28 14:34:17 +05301148 out->hal_fragment_size, src_format, dst_format);
Ashish Jainf1eaa582016-05-23 20:54:24 +05301149 } else {
Ashish Jain83a6cc22016-06-28 14:34:17 +05301150 out->hal_fragment_size = out->compr_config.fragment_size;
Ashish Jainf1eaa582016-05-23 20:54:24 +05301151 }
1152}
1153
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301154/* converts pcm format 24_8 to 8_24 inplace */
1155size_t audio_extn_utils_convert_format_24_8_to_8_24(void *buf, size_t bytes)
1156{
1157 size_t i = 0;
1158 int *int_buf_stream = buf;
1159
1160 if ((bytes % 4) != 0) {
1161 ALOGE("%s: wrong inout buffer! ... is not 32 bit aligned ", __func__);
1162 return -EINVAL;
1163 }
1164
1165 for (; i < (bytes / 4); i++)
1166 int_buf_stream[i] >>= 8;
1167
1168 return bytes;
1169}
1170
1171int get_snd_codec_id(audio_format_t format)
1172{
1173 int id = 0;
1174
1175 switch (format & AUDIO_FORMAT_MAIN_MASK) {
1176 case AUDIO_FORMAT_MP3:
1177 id = SND_AUDIOCODEC_MP3;
1178 break;
1179 case AUDIO_FORMAT_AAC:
1180 id = SND_AUDIOCODEC_AAC;
1181 break;
1182 case AUDIO_FORMAT_AAC_ADTS:
1183 id = SND_AUDIOCODEC_AAC;
1184 break;
Arun Kumar Dasari3b174182016-12-27 13:01:14 +05301185 case AUDIO_FORMAT_AAC_LATM:
1186 id = SND_AUDIOCODEC_AAC;
1187 break;
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301188 case AUDIO_FORMAT_PCM_OFFLOAD:
1189 case AUDIO_FORMAT_PCM:
1190 id = SND_AUDIOCODEC_PCM;
1191 break;
1192 case AUDIO_FORMAT_FLAC:
1193 id = SND_AUDIOCODEC_FLAC;
1194 break;
1195 case AUDIO_FORMAT_ALAC:
1196 id = SND_AUDIOCODEC_ALAC;
1197 break;
1198 case AUDIO_FORMAT_APE:
1199 id = SND_AUDIOCODEC_APE;
1200 break;
1201 case AUDIO_FORMAT_VORBIS:
1202 id = SND_AUDIOCODEC_VORBIS;
1203 break;
1204 case AUDIO_FORMAT_WMA:
1205 id = SND_AUDIOCODEC_WMA;
1206 break;
1207 case AUDIO_FORMAT_WMA_PRO:
1208 id = SND_AUDIOCODEC_WMA_PRO;
1209 break;
Satish Babu Patakokila0c313922016-12-08 12:07:08 +05301210 case AUDIO_FORMAT_MP2:
1211 id = SND_AUDIOCODEC_MP2;
1212 break;
Satish Babu Patakokila915ecba2017-01-10 17:43:56 +05301213 case AUDIO_FORMAT_AC3:
1214 id = SND_AUDIOCODEC_AC3;
1215 break;
1216 case AUDIO_FORMAT_E_AC3:
1217 case AUDIO_FORMAT_E_AC3_JOC:
1218 id = SND_AUDIOCODEC_EAC3;
1219 break;
1220 case AUDIO_FORMAT_DTS:
1221 case AUDIO_FORMAT_DTS_HD:
1222 id = SND_AUDIOCODEC_DTS;
1223 break;
Preetam Singh Ranawat4277a5a2017-01-18 19:02:24 +05301224 case AUDIO_FORMAT_DSD:
1225 id = SND_AUDIOCODEC_DSD;
1226 break;
Dhanalakshmi Siddani18737932016-11-29 17:33:17 +05301227 case AUDIO_FORMAT_APTX:
1228 id = SND_AUDIOCODEC_APTX;
1229 break;
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301230 default:
1231 ALOGE("%s: Unsupported audio format :%x", __func__, format);
1232 }
1233
1234 return id;
1235}
1236
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001237void audio_extn_utils_send_audio_calibration(struct audio_device *adev,
1238 struct audio_usecase *usecase)
1239{
1240 int type = usecase->type;
1241
Dhananjay Kumar14245982017-01-16 20:21:00 +05301242 if (type == PCM_PLAYBACK && usecase->stream.out != NULL) {
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001243 struct stream_out *out = usecase->stream.out;
1244 int snd_device = usecase->out_snd_device;
1245 snd_device = (snd_device == SND_DEVICE_OUT_SPEAKER) ?
Xiaojun Sang040cc9f2015-08-03 19:38:28 +08001246 platform_get_spkr_prot_snd_device(snd_device) : snd_device;
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05301247 platform_send_audio_calibration(adev->platform, usecase,
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001248 out->app_type_cfg.app_type,
Preetam Singh Ranawat61716b12015-12-14 11:55:24 +05301249 usecase->stream.out->app_type_cfg.sample_rate);
Dhananjay Kumar14245982017-01-16 20:21:00 +05301250 } else if (type == PCM_CAPTURE && usecase->stream.in != NULL) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301251 platform_send_audio_calibration(adev->platform, usecase,
1252 usecase->stream.in->app_type_cfg.app_type,
1253 usecase->stream.in->app_type_cfg.sample_rate);
1254 } else {
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05301255 /* when app type is default. the sample rate is not used to send cal */
1256 platform_send_audio_calibration(adev->platform, usecase,
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301257 platform_get_default_app_type_v2(adev->platform, usecase->type),
1258 48000);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001259 }
1260}
1261
Ben Rombergera04fabc2014-11-14 12:16:03 -08001262// Base64 Encode and Decode
1263// Not all features supported. This must be used only with following conditions.
1264// Decode Modes: Support with and without padding
1265// CRLF not handling. So no CRLF in string to decode.
1266// Encode Modes: Supports only padding
1267int b64decode(char *inp, int ilen, uint8_t* outp)
1268{
1269 int i, j, k, ii, num;
1270 int rem, pcnt;
1271 uint32_t res=0;
1272 uint8_t getIndex[MAX_BASEINDEX_LEN];
1273 uint8_t tmp, cflag;
1274
1275 if(inp == NULL || outp == NULL || ilen <= 0) {
1276 ALOGE("[%s] received NULL pointer or zero length",__func__);
1277 return -1;
1278 }
1279
1280 memset(getIndex, MAX_BASEINDEX_LEN-1, sizeof(getIndex));
1281 for(i=0;i<BASE_TABLE_SIZE;i++) {
1282 getIndex[(uint8_t)bTable[i]] = (uint8_t)i;
1283 }
1284 getIndex[(uint8_t)'=']=0;
1285
1286 j=0;k=0;
1287 num = ilen/4;
1288 rem = ilen%4;
1289 if(rem==0)
1290 num = num-1;
1291 cflag=0;
1292 for(i=0; i<num; i++) {
1293 res=0;
1294 for(ii=0;ii<4;ii++) {
1295 res = res << 6;
1296 tmp = getIndex[(uint8_t)inp[j++]];
1297 res = res | tmp;
1298 cflag = cflag | tmp;
1299 }
1300 outp[k++] = (res >> 16)&0xFF;
1301 outp[k++] = (res >> 8)&0xFF;
1302 outp[k++] = res & 0xFF;
1303 }
1304
1305 // Handle last bytes special
1306 pcnt=0;
1307 if(rem == 0) {
1308 //With padding or full data
1309 res = 0;
1310 for(ii=0;ii<4;ii++) {
1311 if(inp[j] == '=')
1312 pcnt++;
1313 res = res << 6;
1314 tmp = getIndex[(uint8_t)inp[j++]];
1315 res = res | tmp;
1316 cflag = cflag | tmp;
1317 }
1318 outp[k++] = res >> 16;
1319 if(pcnt == 2)
1320 goto done;
1321 outp[k++] = (res>>8)&0xFF;
1322 if(pcnt == 1)
1323 goto done;
1324 outp[k++] = res&0xFF;
1325 } else {
1326 //without padding
1327 res = 0;
1328 for(i=0;i<rem;i++) {
1329 res = res << 6;
1330 tmp = getIndex[(uint8_t)inp[j++]];
1331 res = res | tmp;
1332 cflag = cflag | tmp;
1333 }
1334 for(i=rem;i<4;i++) {
1335 res = res << 6;
1336 pcnt++;
1337 }
1338 outp[k++] = res >> 16;
1339 if(pcnt == 2)
1340 goto done;
1341 outp[k++] = (res>>8)&0xFF;
1342 if(pcnt == 1)
1343 goto done;
1344 outp[k++] = res&0xFF;
1345 }
1346done:
1347 if(cflag == 0xFF) {
1348 ALOGE("[%s] base64 decode failed. Invalid character found %s",
1349 __func__, inp);
1350 return 0;
1351 }
1352 return k;
1353}
1354
1355int b64encode(uint8_t *inp, int ilen, char* outp)
1356{
1357 int i,j,k, num;
1358 int rem=0;
1359 uint32_t res=0;
1360
1361 if(inp == NULL || outp == NULL || ilen<=0) {
1362 ALOGE("[%s] received NULL pointer or zero input length",__func__);
1363 return -1;
1364 }
1365
1366 num = ilen/3;
1367 rem = ilen%3;
1368 j=0;k=0;
1369 for(i=0; i<num; i++) {
1370 //prepare index
1371 res = inp[j++]<<16;
1372 res = res | inp[j++]<<8;
1373 res = res | inp[j++];
1374 //get output map from index
1375 outp[k++] = (char) bTable[(res>>18)&0x3F];
1376 outp[k++] = (char) bTable[(res>>12)&0x3F];
1377 outp[k++] = (char) bTable[(res>>6)&0x3F];
1378 outp[k++] = (char) bTable[res&0x3F];
1379 }
1380
1381 switch(rem) {
1382 case 1:
1383 res = inp[j++]<<16;
1384 outp[k++] = (char) bTable[res>>18];
1385 outp[k++] = (char) bTable[(res>>12)&0x3F];
1386 //outp[k++] = '=';
1387 //outp[k++] = '=';
1388 break;
1389 case 2:
1390 res = inp[j++]<<16;
1391 res = res | inp[j++]<<8;
1392 outp[k++] = (char) bTable[res>>18];
1393 outp[k++] = (char) bTable[(res>>12)&0x3F];
1394 outp[k++] = (char) bTable[(res>>6)&0x3F];
1395 //outp[k++] = '=';
1396 break;
1397 default:
1398 break;
1399 }
Ben Rombergera04fabc2014-11-14 12:16:03 -08001400 outp[k] = '\0';
1401 return k;
1402}
Ashish Jain81eb2a82015-05-13 10:52:34 +05301403
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301404
1405int audio_extn_utils_get_codec_version(const char *snd_card_name,
1406 int card_num,
1407 char *codec_version)
1408{
1409 char procfs_path[50];
1410 FILE *fp;
1411
1412 if (strstr(snd_card_name, "tasha")) {
1413 snprintf(procfs_path, sizeof(procfs_path),
1414 "/proc/asound/card%d/codecs/tasha/version", card_num);
1415 if ((fp = fopen(procfs_path, "r")) != NULL) {
1416 fgets(codec_version, CODEC_VERSION_MAX_LENGTH, fp);
1417 fclose(fp);
1418 } else {
1419 ALOGE("%s: ERROR. cannot open %s", __func__, procfs_path);
1420 return -ENOENT;
1421 }
1422 ALOGD("%s: codec version %s", __func__, codec_version);
1423 }
1424
1425 return 0;
1426}
1427
1428
Ashish Jain81eb2a82015-05-13 10:52:34 +05301429#ifdef AUDIO_EXTERNAL_HDMI_ENABLED
1430
1431void get_default_compressed_channel_status(
1432 unsigned char *channel_status)
1433{
Ashish Jain81eb2a82015-05-13 10:52:34 +05301434 memset(channel_status,0,24);
1435
1436 /* block start bit in preamble bit 3 */
1437 channel_status[0] |= PROFESSIONAL;
1438 //compre out
1439 channel_status[0] |= NON_LPCM;
1440 // sample rate; fixed 48K for default/transcode
1441 channel_status[3] |= SR_48000;
1442}
1443
1444#ifdef HDMI_PASSTHROUGH_ENABLED
1445int32_t get_compressed_channel_status(void *audio_stream_data,
1446 uint32_t audio_frame_size,
1447 unsigned char *channel_status,
1448 enum audio_parser_code_type codec_type)
1449 // codec_type - AUDIO_PARSER_CODEC_AC3
1450 // - AUDIO_PARSER_CODEC_DTS
1451{
1452 unsigned char *stream;
1453 int ret = 0;
1454 stream = (unsigned char *)audio_stream_data;
1455
1456 if (audio_stream_data == NULL || audio_frame_size == 0) {
1457 ALOGW("no buffer to get channel status, return default for compress");
1458 get_default_compressed_channel_status(channel_status);
1459 return ret;
1460 }
1461
1462 memset(channel_status,0,24);
1463 if(init_audio_parser(stream, audio_frame_size, codec_type) == -1)
1464 {
1465 ALOGE("init audio parser failed");
1466 return -1;
1467 }
1468 ret = get_channel_status(channel_status, codec_type);
1469 return ret;
1470
1471}
1472
1473#endif
1474
1475void get_lpcm_channel_status(uint32_t sampleRate,
1476 unsigned char *channel_status)
1477{
1478 int32_t status = 0;
Ashish Jain81eb2a82015-05-13 10:52:34 +05301479 memset(channel_status,0,24);
1480 /* block start bit in preamble bit 3 */
1481 channel_status[0] |= PROFESSIONAL;
1482 //LPCM OUT
1483 channel_status[0] &= ~NON_LPCM;
1484
1485 switch (sampleRate) {
1486 case 8000:
1487 case 11025:
1488 case 12000:
1489 case 16000:
1490 case 22050:
1491 channel_status[3] |= SR_NOTID;
Preetam Singh Ranawat61716b12015-12-14 11:55:24 +05301492 break;
Ashish Jain81eb2a82015-05-13 10:52:34 +05301493 case 24000:
1494 channel_status[3] |= SR_24000;
1495 break;
1496 case 32000:
1497 channel_status[3] |= SR_32000;
1498 break;
1499 case 44100:
1500 channel_status[3] |= SR_44100;
1501 break;
1502 case 48000:
1503 channel_status[3] |= SR_48000;
1504 break;
1505 case 88200:
1506 channel_status[3] |= SR_88200;
1507 break;
1508 case 96000:
1509 channel_status[3] |= SR_96000;
1510 break;
1511 case 176400:
1512 channel_status[3] |= SR_176400;
1513 break;
1514 case 192000:
1515 channel_status[3] |= SR_192000;
1516 break;
1517 default:
1518 ALOGV("Invalid sample_rate %u\n", sampleRate);
1519 status = -1;
1520 break;
1521 }
1522}
1523
1524void audio_utils_set_hdmi_channel_status(struct stream_out *out, char * buffer, size_t bytes)
1525{
1526 unsigned char channel_status[24]={0};
1527 struct snd_aes_iec958 iec958;
1528 const char *mixer_ctl_name = "IEC958 Playback PCM Stream";
1529 struct mixer_ctl *ctl;
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +05301530 ALOGV("%s: buffer %s bytes %zd", __func__, buffer, bytes);
Ashish Jain81eb2a82015-05-13 10:52:34 +05301531#ifdef HDMI_PASSTHROUGH_ENABLED
1532 if (audio_extn_is_dolby_format(out->format) &&
1533 /*TODO:Extend code to support DTS passthrough*/
1534 /*set compressed channel status bits*/
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05301535 audio_extn_passthru_is_passthrough_stream(out)){
Ashish Jain81eb2a82015-05-13 10:52:34 +05301536 get_compressed_channel_status(buffer, bytes, channel_status, AUDIO_PARSER_CODEC_AC3);
1537 } else
1538#endif
1539 {
1540 /*set channel status bit for LPCM*/
1541 get_lpcm_channel_status(out->sample_rate, channel_status);
1542 }
1543
1544 memcpy(iec958.status, channel_status,sizeof(iec958.status));
1545 ctl = mixer_get_ctl_by_name(out->dev->mixer, mixer_ctl_name);
1546 if (!ctl) {
1547 ALOGE("%s: Could not get ctl for mixer cmd - %s",
1548 __func__, mixer_ctl_name);
1549 return;
1550 }
1551 if (mixer_ctl_set_array(ctl, &iec958, sizeof(iec958)) < 0) {
1552 ALOGE("%s: Could not set channel status for ext HDMI ",
1553 __func__);
1554 return;
1555 }
1556
1557}
1558#endif