blob: b12ab8ea0153fdce6a901ddff405e54b6051cd27 [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)) {
Manish Dewangan837dc462015-05-27 10:17:41 +0530833
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530834 property_get("audio.playback.mch.downsample",value,"");
835 if (!strncmp("true", value, sizeof("true"))) {
836 if ((popcount(usecase->stream.out->channel_mask) > 2) &&
837 (usecase->stream.out->app_type_cfg.sample_rate > CODEC_BACKEND_DEFAULT_SAMPLE_RATE) &&
838 !(usecase->stream.out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH))
839 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
840 }
Ashish Jainc02430d2016-01-04 10:42:43 +0530841
Ashish Jain4826f6c2017-02-06 13:33:20 +0530842 if (usecase->stream.out->devices & AUDIO_DEVICE_OUT_SPEAKER) {
Mingming Yin21854652016-04-13 11:54:02 -0700843 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Ashish Jaina052e572016-11-07 16:41:07 +0530844 } else if ((snd_device == SND_DEVICE_OUT_HDMI ||
845 snd_device == SND_DEVICE_OUT_USB_HEADSET ||
846 snd_device == SND_DEVICE_OUT_DISPLAY_PORT) &&
847 (usecase->stream.out->sample_rate >= OUTPUT_SAMPLING_RATE_44100)) {
848 /*
849 * To best utlize DSP, check if the stream sample rate is supported/multiple of
850 * configured device sample rate, if not update the COPP rate to be equal to the
851 * device sample rate, else open COPP at stream sample rate
852 */
853 platform_check_and_update_copp_sample_rate(adev->platform, snd_device,
854 usecase->stream.out->sample_rate,
855 &usecase->stream.out->app_type_cfg.sample_rate);
Ashish Jain4826f6c2017-02-06 13:33:20 +0530856 } else if (((snd_device != SND_DEVICE_OUT_HEADPHONES_44_1 &&
857 !audio_is_this_native_usecase(usecase)) &&
Mingming Yin21854652016-04-13 11:54:02 -0700858 usecase->stream.out->sample_rate == OUTPUT_SAMPLING_RATE_44100) ||
859 (usecase->stream.out->sample_rate < OUTPUT_SAMPLING_RATE_44100)) {
Ashish Jain4826f6c2017-02-06 13:33:20 +0530860 /* Reset to default if no native stream is active*/
Mingming Yin21854652016-04-13 11:54:02 -0700861 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
862 }
863 sample_rate = usecase->stream.out->app_type_cfg.sample_rate;
864
Siena Richard7c2db772016-12-21 11:32:34 -0800865 app_type = usecase->stream.out->app_type_cfg.app_type;
866 app_type_cfg[len++] = app_type;
Mingming Yin21854652016-04-13 11:54:02 -0700867 app_type_cfg[len++] = acdb_dev_id;
868 if (((usecase->stream.out->format == AUDIO_FORMAT_E_AC3) ||
869 (usecase->stream.out->format == AUDIO_FORMAT_E_AC3_JOC))
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530870 && audio_extn_passthru_is_passthrough_stream(usecase->stream.out)) {
Mingming Yin21854652016-04-13 11:54:02 -0700871 app_type_cfg[len++] = sample_rate * 4;
872 } else {
873 app_type_cfg[len++] = sample_rate;
874 }
Siena Richard7c2db772016-12-21 11:32:34 -0800875 if (snd_device_be_idx > 0)
876 app_type_cfg[len++] = snd_device_be_idx;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530877
Siena Richard7c2db772016-12-21 11:32:34 -0800878 ALOGI("%s PLAYBACK app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
879 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530880
881 } else if ((usecase->type == PCM_CAPTURE) && (usecase->stream.in != NULL)) {
Siena Richard7c2db772016-12-21 11:32:34 -0800882 app_type = usecase->stream.in->app_type_cfg.app_type;
883 app_type_cfg[len++] = app_type;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530884 app_type_cfg[len++] = acdb_dev_id;
Siena Richard7c2db772016-12-21 11:32:34 -0800885 sample_rate = usecase->stream.in->app_type_cfg.sample_rate;
886 app_type_cfg[len++] = sample_rate;
887 if (snd_device_be_idx > 0)
888 app_type_cfg[len++] = snd_device_be_idx;
889 ALOGI("%s CAPTURE app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
890 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530891 } else {
Siena Richard7c2db772016-12-21 11:32:34 -0800892 app_type = platform_get_default_app_type_v2(adev->platform, usecase->type);
893 app_type_cfg[len++] = app_type;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530894 app_type_cfg[len++] = acdb_dev_id;
895 app_type_cfg[len++] = sample_rate;
Siena Richard7c2db772016-12-21 11:32:34 -0800896 if (snd_device_be_idx > 0)
897 app_type_cfg[len++] = snd_device_be_idx;
898 ALOGI("%s default app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
899 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Ashish Jainc02430d2016-01-04 10:42:43 +0530900 }
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +0530901
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700902 mixer_ctl_set_array(ctl, app_type_cfg, len);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700903 rc = 0;
904exit_send_app_type_cfg:
905 return rc;
906}
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700907
Siena Richard7c2db772016-12-21 11:32:34 -0800908int audio_extn_utils_send_app_type_cfg(struct audio_device *adev,
909 struct audio_usecase *usecase)
910{
911 int i, num_devices = 0;
912 snd_device_t new_snd_devices[SND_DEVICE_OUT_END] = {0};
913 int rc = 0;
914
915 switch (usecase->type) {
916 case PCM_PLAYBACK:
917 ALOGD("%s: usecase->out_snd_device %s",
918 __func__, platform_get_snd_device_name(usecase->out_snd_device));
919 /* check for out combo device */
920 if (platform_split_snd_device(adev->platform,
921 usecase->out_snd_device,
922 &num_devices, new_snd_devices)) {
923 new_snd_devices[0] = usecase->out_snd_device;
924 num_devices = 1;
925 }
926 break;
927 case PCM_CAPTURE:
928 ALOGD("%s: usecase->in_snd_device %s",
929 __func__, platform_get_snd_device_name(usecase->in_snd_device));
930 /* check for in combo device */
931 if (platform_split_snd_device(adev->platform,
932 usecase->in_snd_device,
933 &num_devices, new_snd_devices)) {
934 new_snd_devices[0] = usecase->in_snd_device;
935 num_devices = 1;
936 }
937 break;
938 default:
939 ALOGI("%s: not a playback/capture path, no need to cfg app type", __func__);
940 rc = 0;
941 break;
942 }
943
944 for (i = 0; i < num_devices; i++) {
945 rc = send_app_type_cfg_for_device(adev, usecase, new_snd_devices[i]);
946 if (rc)
947 break;
948 }
949
950 return rc;
951}
952
Preetam Singh Ranawat9519e9c2015-11-18 16:05:55 +0530953int read_line_from_file(const char *path, char *buf, size_t count)
954{
955 char * fgets_ret;
956 FILE * fd;
957 int rv;
958
959 fd = fopen(path, "r");
960 if (fd == NULL)
961 return -1;
962
963 fgets_ret = fgets(buf, (int)count, fd);
964 if (NULL != fgets_ret) {
965 rv = (int)strlen(buf);
966 } else {
967 rv = ferror(fd);
968 }
969 fclose(fd);
970
971 return rv;
972}
973
Ashish Jainf1eaa582016-05-23 20:54:24 +0530974/*Translates ALSA formats to AOSP PCM formats*/
975audio_format_t alsa_format_to_hal(uint32_t alsa_format)
976{
977 audio_format_t format;
978
979 switch(alsa_format) {
980 case SNDRV_PCM_FORMAT_S16_LE:
981 format = AUDIO_FORMAT_PCM_16_BIT;
982 break;
983 case SNDRV_PCM_FORMAT_S24_3LE:
984 format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
985 break;
986 case SNDRV_PCM_FORMAT_S24_LE:
987 format = AUDIO_FORMAT_PCM_8_24_BIT;
988 break;
989 case SNDRV_PCM_FORMAT_S32_LE:
990 format = AUDIO_FORMAT_PCM_32_BIT;
991 break;
992 default:
993 ALOGW("Incorrect ALSA 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_alsa(audio_format_t hal_format)
1001{
1002 uint32_t alsa_format;
1003
1004 switch (hal_format) {
1005 case AUDIO_FORMAT_PCM_32_BIT: {
1006 if (platform_supports_true_32bit())
1007 alsa_format = SNDRV_PCM_FORMAT_S32_LE;
1008 else
1009 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1010 }
1011 break;
1012 case AUDIO_FORMAT_PCM_8_BIT:
1013 alsa_format = SNDRV_PCM_FORMAT_S8;
1014 break;
1015 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
1016 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1017 break;
1018 case AUDIO_FORMAT_PCM_8_24_BIT: {
1019 if (platform_supports_true_32bit())
1020 alsa_format = SNDRV_PCM_FORMAT_S32_LE;
1021 else
1022 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1023 }
1024 break;
1025 case AUDIO_FORMAT_PCM_FLOAT:
1026 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1027 break;
1028 default:
1029 case AUDIO_FORMAT_PCM_16_BIT:
1030 alsa_format = SNDRV_PCM_FORMAT_S16_LE;
1031 break;
1032 }
1033 return alsa_format;
1034}
1035
Ashish Jain83a6cc22016-06-28 14:34:17 +05301036/*Translates PCM formats to AOSP formats*/
1037audio_format_t pcm_format_to_hal(uint32_t pcm_format)
1038{
1039 audio_format_t format = AUDIO_FORMAT_INVALID;
1040
1041 switch(pcm_format) {
1042 case PCM_FORMAT_S16_LE:
1043 format = AUDIO_FORMAT_PCM_16_BIT;
1044 break;
1045 case PCM_FORMAT_S24_3LE:
1046 format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
1047 break;
1048 case PCM_FORMAT_S24_LE:
1049 format = AUDIO_FORMAT_PCM_8_24_BIT;
1050 break;
1051 case PCM_FORMAT_S32_LE:
1052 format = AUDIO_FORMAT_PCM_32_BIT;
1053 break;
1054 default:
1055 ALOGW("Incorrect PCM format");
1056 format = AUDIO_FORMAT_INVALID;
1057 }
1058 return format;
1059}
1060
1061/*Translates hal format (AOSP) to alsa formats*/
1062uint32_t hal_format_to_pcm(audio_format_t hal_format)
1063{
1064 uint32_t pcm_format;
1065
1066 switch (hal_format) {
1067 case AUDIO_FORMAT_PCM_32_BIT:
1068 case AUDIO_FORMAT_PCM_8_24_BIT:
1069 case AUDIO_FORMAT_PCM_FLOAT: {
1070 if (platform_supports_true_32bit())
1071 pcm_format = PCM_FORMAT_S32_LE;
1072 else
1073 pcm_format = PCM_FORMAT_S24_3LE;
1074 }
1075 break;
1076 case AUDIO_FORMAT_PCM_8_BIT:
1077 pcm_format = PCM_FORMAT_S8;
1078 break;
1079 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
1080 pcm_format = PCM_FORMAT_S24_3LE;
1081 break;
1082 default:
1083 case AUDIO_FORMAT_PCM_16_BIT:
1084 pcm_format = PCM_FORMAT_S16_LE;
1085 break;
1086 }
1087 return pcm_format;
1088}
1089
Ashish Jainf1eaa582016-05-23 20:54:24 +05301090uint32_t get_alsa_fragment_size(uint32_t bytes_per_sample,
1091 uint32_t sample_rate,
1092 uint32_t noOfChannels)
1093{
1094 uint32_t fragment_size = 0;
1095 uint32_t pcm_offload_time = PCM_OFFLOAD_BUFFER_DURATION;
1096
1097 fragment_size = (pcm_offload_time
1098 * sample_rate
1099 * bytes_per_sample
1100 * noOfChannels)/1000;
1101 if (fragment_size < MIN_PCM_OFFLOAD_FRAGMENT_SIZE)
1102 fragment_size = MIN_PCM_OFFLOAD_FRAGMENT_SIZE;
1103 else if (fragment_size > MAX_PCM_OFFLOAD_FRAGMENT_SIZE)
1104 fragment_size = MAX_PCM_OFFLOAD_FRAGMENT_SIZE;
1105 /*To have same PCM samples for all channels, the buffer size requires to
1106 *be multiple of (number of channels * bytes per sample)
1107 *For writes to succeed, the buffer must be written at address which is multiple of 32
1108 */
1109 fragment_size = ALIGN(fragment_size, (bytes_per_sample * noOfChannels * 32));
1110
1111 ALOGI("PCM offload Fragment size to %d bytes", fragment_size);
1112 return fragment_size;
1113}
1114
1115/* Calculates the fragment size required to configure compress session.
1116 * Based on the alsa format selected, decide if conversion is needed in
1117
1118 * HAL ( e.g. convert AUDIO_FORMAT_PCM_FLOAT input format to
1119 * AUDIO_FORMAT_PCM_24_BIT_PACKED before writing to the compress driver.
1120 */
1121void audio_extn_utils_update_direct_pcm_fragment_size(struct stream_out *out)
1122{
Ashish Jain83a6cc22016-06-28 14:34:17 +05301123 audio_format_t dst_format = out->hal_op_format;
1124 audio_format_t src_format = out->hal_ip_format;
Ashish Jainf1eaa582016-05-23 20:54:24 +05301125 uint32_t hal_op_bytes_per_sample = audio_bytes_per_sample(dst_format);
1126 uint32_t hal_ip_bytes_per_sample = audio_bytes_per_sample(src_format);
1127
1128 out->compr_config.fragment_size =
1129 get_alsa_fragment_size(hal_op_bytes_per_sample,
1130 out->sample_rate,
1131 popcount(out->channel_mask));
1132
1133 if ((src_format != dst_format) &&
1134 hal_op_bytes_per_sample != hal_ip_bytes_per_sample) {
1135
Ashish Jain83a6cc22016-06-28 14:34:17 +05301136 out->hal_fragment_size =
Ashish Jainf1eaa582016-05-23 20:54:24 +05301137 ((out->compr_config.fragment_size * hal_ip_bytes_per_sample) /
1138 hal_op_bytes_per_sample);
1139 ALOGI("enable conversion hal_input_fragment_size is %d src_format %x dst_format %x",
Ashish Jain83a6cc22016-06-28 14:34:17 +05301140 out->hal_fragment_size, src_format, dst_format);
Ashish Jainf1eaa582016-05-23 20:54:24 +05301141 } else {
Ashish Jain83a6cc22016-06-28 14:34:17 +05301142 out->hal_fragment_size = out->compr_config.fragment_size;
Ashish Jainf1eaa582016-05-23 20:54:24 +05301143 }
1144}
1145
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301146/* converts pcm format 24_8 to 8_24 inplace */
1147size_t audio_extn_utils_convert_format_24_8_to_8_24(void *buf, size_t bytes)
1148{
1149 size_t i = 0;
1150 int *int_buf_stream = buf;
1151
1152 if ((bytes % 4) != 0) {
1153 ALOGE("%s: wrong inout buffer! ... is not 32 bit aligned ", __func__);
1154 return -EINVAL;
1155 }
1156
1157 for (; i < (bytes / 4); i++)
1158 int_buf_stream[i] >>= 8;
1159
1160 return bytes;
1161}
1162
1163int get_snd_codec_id(audio_format_t format)
1164{
1165 int id = 0;
1166
1167 switch (format & AUDIO_FORMAT_MAIN_MASK) {
1168 case AUDIO_FORMAT_MP3:
1169 id = SND_AUDIOCODEC_MP3;
1170 break;
1171 case AUDIO_FORMAT_AAC:
1172 id = SND_AUDIOCODEC_AAC;
1173 break;
1174 case AUDIO_FORMAT_AAC_ADTS:
1175 id = SND_AUDIOCODEC_AAC;
1176 break;
Arun Kumar Dasari3b174182016-12-27 13:01:14 +05301177 case AUDIO_FORMAT_AAC_LATM:
1178 id = SND_AUDIOCODEC_AAC;
1179 break;
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301180 case AUDIO_FORMAT_PCM_OFFLOAD:
1181 case AUDIO_FORMAT_PCM:
1182 id = SND_AUDIOCODEC_PCM;
1183 break;
1184 case AUDIO_FORMAT_FLAC:
1185 id = SND_AUDIOCODEC_FLAC;
1186 break;
1187 case AUDIO_FORMAT_ALAC:
1188 id = SND_AUDIOCODEC_ALAC;
1189 break;
1190 case AUDIO_FORMAT_APE:
1191 id = SND_AUDIOCODEC_APE;
1192 break;
1193 case AUDIO_FORMAT_VORBIS:
1194 id = SND_AUDIOCODEC_VORBIS;
1195 break;
1196 case AUDIO_FORMAT_WMA:
1197 id = SND_AUDIOCODEC_WMA;
1198 break;
1199 case AUDIO_FORMAT_WMA_PRO:
1200 id = SND_AUDIOCODEC_WMA_PRO;
1201 break;
Satish Babu Patakokila0c313922016-12-08 12:07:08 +05301202 case AUDIO_FORMAT_MP2:
1203 id = SND_AUDIOCODEC_MP2;
1204 break;
Satish Babu Patakokila915ecba2017-01-10 17:43:56 +05301205 case AUDIO_FORMAT_AC3:
1206 id = SND_AUDIOCODEC_AC3;
1207 break;
1208 case AUDIO_FORMAT_E_AC3:
1209 case AUDIO_FORMAT_E_AC3_JOC:
1210 id = SND_AUDIOCODEC_EAC3;
1211 break;
1212 case AUDIO_FORMAT_DTS:
1213 case AUDIO_FORMAT_DTS_HD:
1214 id = SND_AUDIOCODEC_DTS;
1215 break;
Preetam Singh Ranawat4277a5a2017-01-18 19:02:24 +05301216 case AUDIO_FORMAT_DSD:
1217 id = SND_AUDIOCODEC_DSD;
1218 break;
Dhanalakshmi Siddani18737932016-11-29 17:33:17 +05301219 case AUDIO_FORMAT_APTX:
1220 id = SND_AUDIOCODEC_APTX;
1221 break;
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301222 default:
1223 ALOGE("%s: Unsupported audio format :%x", __func__, format);
1224 }
1225
1226 return id;
1227}
1228
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001229void audio_extn_utils_send_audio_calibration(struct audio_device *adev,
1230 struct audio_usecase *usecase)
1231{
1232 int type = usecase->type;
1233
Dhananjay Kumar14245982017-01-16 20:21:00 +05301234 if (type == PCM_PLAYBACK && usecase->stream.out != NULL) {
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001235 struct stream_out *out = usecase->stream.out;
1236 int snd_device = usecase->out_snd_device;
1237 snd_device = (snd_device == SND_DEVICE_OUT_SPEAKER) ?
Xiaojun Sang040cc9f2015-08-03 19:38:28 +08001238 platform_get_spkr_prot_snd_device(snd_device) : snd_device;
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05301239 platform_send_audio_calibration(adev->platform, usecase,
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001240 out->app_type_cfg.app_type,
Preetam Singh Ranawat61716b12015-12-14 11:55:24 +05301241 usecase->stream.out->app_type_cfg.sample_rate);
Dhananjay Kumar14245982017-01-16 20:21:00 +05301242 } else if (type == PCM_CAPTURE && usecase->stream.in != NULL) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301243 platform_send_audio_calibration(adev->platform, usecase,
1244 usecase->stream.in->app_type_cfg.app_type,
1245 usecase->stream.in->app_type_cfg.sample_rate);
1246 } else {
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05301247 /* when app type is default. the sample rate is not used to send cal */
1248 platform_send_audio_calibration(adev->platform, usecase,
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301249 platform_get_default_app_type_v2(adev->platform, usecase->type),
1250 48000);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001251 }
1252}
1253
Ben Rombergera04fabc2014-11-14 12:16:03 -08001254// Base64 Encode and Decode
1255// Not all features supported. This must be used only with following conditions.
1256// Decode Modes: Support with and without padding
1257// CRLF not handling. So no CRLF in string to decode.
1258// Encode Modes: Supports only padding
1259int b64decode(char *inp, int ilen, uint8_t* outp)
1260{
1261 int i, j, k, ii, num;
1262 int rem, pcnt;
1263 uint32_t res=0;
1264 uint8_t getIndex[MAX_BASEINDEX_LEN];
1265 uint8_t tmp, cflag;
1266
1267 if(inp == NULL || outp == NULL || ilen <= 0) {
1268 ALOGE("[%s] received NULL pointer or zero length",__func__);
1269 return -1;
1270 }
1271
1272 memset(getIndex, MAX_BASEINDEX_LEN-1, sizeof(getIndex));
1273 for(i=0;i<BASE_TABLE_SIZE;i++) {
1274 getIndex[(uint8_t)bTable[i]] = (uint8_t)i;
1275 }
1276 getIndex[(uint8_t)'=']=0;
1277
1278 j=0;k=0;
1279 num = ilen/4;
1280 rem = ilen%4;
1281 if(rem==0)
1282 num = num-1;
1283 cflag=0;
1284 for(i=0; i<num; i++) {
1285 res=0;
1286 for(ii=0;ii<4;ii++) {
1287 res = res << 6;
1288 tmp = getIndex[(uint8_t)inp[j++]];
1289 res = res | tmp;
1290 cflag = cflag | tmp;
1291 }
1292 outp[k++] = (res >> 16)&0xFF;
1293 outp[k++] = (res >> 8)&0xFF;
1294 outp[k++] = res & 0xFF;
1295 }
1296
1297 // Handle last bytes special
1298 pcnt=0;
1299 if(rem == 0) {
1300 //With padding or full data
1301 res = 0;
1302 for(ii=0;ii<4;ii++) {
1303 if(inp[j] == '=')
1304 pcnt++;
1305 res = res << 6;
1306 tmp = getIndex[(uint8_t)inp[j++]];
1307 res = res | tmp;
1308 cflag = cflag | tmp;
1309 }
1310 outp[k++] = res >> 16;
1311 if(pcnt == 2)
1312 goto done;
1313 outp[k++] = (res>>8)&0xFF;
1314 if(pcnt == 1)
1315 goto done;
1316 outp[k++] = res&0xFF;
1317 } else {
1318 //without padding
1319 res = 0;
1320 for(i=0;i<rem;i++) {
1321 res = res << 6;
1322 tmp = getIndex[(uint8_t)inp[j++]];
1323 res = res | tmp;
1324 cflag = cflag | tmp;
1325 }
1326 for(i=rem;i<4;i++) {
1327 res = res << 6;
1328 pcnt++;
1329 }
1330 outp[k++] = res >> 16;
1331 if(pcnt == 2)
1332 goto done;
1333 outp[k++] = (res>>8)&0xFF;
1334 if(pcnt == 1)
1335 goto done;
1336 outp[k++] = res&0xFF;
1337 }
1338done:
1339 if(cflag == 0xFF) {
1340 ALOGE("[%s] base64 decode failed. Invalid character found %s",
1341 __func__, inp);
1342 return 0;
1343 }
1344 return k;
1345}
1346
1347int b64encode(uint8_t *inp, int ilen, char* outp)
1348{
1349 int i,j,k, num;
1350 int rem=0;
1351 uint32_t res=0;
1352
1353 if(inp == NULL || outp == NULL || ilen<=0) {
1354 ALOGE("[%s] received NULL pointer or zero input length",__func__);
1355 return -1;
1356 }
1357
1358 num = ilen/3;
1359 rem = ilen%3;
1360 j=0;k=0;
1361 for(i=0; i<num; i++) {
1362 //prepare index
1363 res = inp[j++]<<16;
1364 res = res | inp[j++]<<8;
1365 res = res | inp[j++];
1366 //get output map from index
1367 outp[k++] = (char) bTable[(res>>18)&0x3F];
1368 outp[k++] = (char) bTable[(res>>12)&0x3F];
1369 outp[k++] = (char) bTable[(res>>6)&0x3F];
1370 outp[k++] = (char) bTable[res&0x3F];
1371 }
1372
1373 switch(rem) {
1374 case 1:
1375 res = inp[j++]<<16;
1376 outp[k++] = (char) bTable[res>>18];
1377 outp[k++] = (char) bTable[(res>>12)&0x3F];
1378 //outp[k++] = '=';
1379 //outp[k++] = '=';
1380 break;
1381 case 2:
1382 res = inp[j++]<<16;
1383 res = res | inp[j++]<<8;
1384 outp[k++] = (char) bTable[res>>18];
1385 outp[k++] = (char) bTable[(res>>12)&0x3F];
1386 outp[k++] = (char) bTable[(res>>6)&0x3F];
1387 //outp[k++] = '=';
1388 break;
1389 default:
1390 break;
1391 }
Ben Rombergera04fabc2014-11-14 12:16:03 -08001392 outp[k] = '\0';
1393 return k;
1394}
Ashish Jain81eb2a82015-05-13 10:52:34 +05301395
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301396
1397int audio_extn_utils_get_codec_version(const char *snd_card_name,
1398 int card_num,
1399 char *codec_version)
1400{
1401 char procfs_path[50];
1402 FILE *fp;
1403
1404 if (strstr(snd_card_name, "tasha")) {
1405 snprintf(procfs_path, sizeof(procfs_path),
1406 "/proc/asound/card%d/codecs/tasha/version", card_num);
1407 if ((fp = fopen(procfs_path, "r")) != NULL) {
1408 fgets(codec_version, CODEC_VERSION_MAX_LENGTH, fp);
1409 fclose(fp);
1410 } else {
1411 ALOGE("%s: ERROR. cannot open %s", __func__, procfs_path);
1412 return -ENOENT;
1413 }
1414 ALOGD("%s: codec version %s", __func__, codec_version);
1415 }
1416
1417 return 0;
1418}
1419
1420
Ashish Jain81eb2a82015-05-13 10:52:34 +05301421#ifdef AUDIO_EXTERNAL_HDMI_ENABLED
1422
1423void get_default_compressed_channel_status(
1424 unsigned char *channel_status)
1425{
Ashish Jain81eb2a82015-05-13 10:52:34 +05301426 memset(channel_status,0,24);
1427
1428 /* block start bit in preamble bit 3 */
1429 channel_status[0] |= PROFESSIONAL;
1430 //compre out
1431 channel_status[0] |= NON_LPCM;
1432 // sample rate; fixed 48K for default/transcode
1433 channel_status[3] |= SR_48000;
1434}
1435
1436#ifdef HDMI_PASSTHROUGH_ENABLED
1437int32_t get_compressed_channel_status(void *audio_stream_data,
1438 uint32_t audio_frame_size,
1439 unsigned char *channel_status,
1440 enum audio_parser_code_type codec_type)
1441 // codec_type - AUDIO_PARSER_CODEC_AC3
1442 // - AUDIO_PARSER_CODEC_DTS
1443{
1444 unsigned char *stream;
1445 int ret = 0;
1446 stream = (unsigned char *)audio_stream_data;
1447
1448 if (audio_stream_data == NULL || audio_frame_size == 0) {
1449 ALOGW("no buffer to get channel status, return default for compress");
1450 get_default_compressed_channel_status(channel_status);
1451 return ret;
1452 }
1453
1454 memset(channel_status,0,24);
1455 if(init_audio_parser(stream, audio_frame_size, codec_type) == -1)
1456 {
1457 ALOGE("init audio parser failed");
1458 return -1;
1459 }
1460 ret = get_channel_status(channel_status, codec_type);
1461 return ret;
1462
1463}
1464
1465#endif
1466
1467void get_lpcm_channel_status(uint32_t sampleRate,
1468 unsigned char *channel_status)
1469{
1470 int32_t status = 0;
Ashish Jain81eb2a82015-05-13 10:52:34 +05301471 memset(channel_status,0,24);
1472 /* block start bit in preamble bit 3 */
1473 channel_status[0] |= PROFESSIONAL;
1474 //LPCM OUT
1475 channel_status[0] &= ~NON_LPCM;
1476
1477 switch (sampleRate) {
1478 case 8000:
1479 case 11025:
1480 case 12000:
1481 case 16000:
1482 case 22050:
1483 channel_status[3] |= SR_NOTID;
Preetam Singh Ranawat61716b12015-12-14 11:55:24 +05301484 break;
Ashish Jain81eb2a82015-05-13 10:52:34 +05301485 case 24000:
1486 channel_status[3] |= SR_24000;
1487 break;
1488 case 32000:
1489 channel_status[3] |= SR_32000;
1490 break;
1491 case 44100:
1492 channel_status[3] |= SR_44100;
1493 break;
1494 case 48000:
1495 channel_status[3] |= SR_48000;
1496 break;
1497 case 88200:
1498 channel_status[3] |= SR_88200;
1499 break;
1500 case 96000:
1501 channel_status[3] |= SR_96000;
1502 break;
1503 case 176400:
1504 channel_status[3] |= SR_176400;
1505 break;
1506 case 192000:
1507 channel_status[3] |= SR_192000;
1508 break;
1509 default:
1510 ALOGV("Invalid sample_rate %u\n", sampleRate);
1511 status = -1;
1512 break;
1513 }
1514}
1515
1516void audio_utils_set_hdmi_channel_status(struct stream_out *out, char * buffer, size_t bytes)
1517{
1518 unsigned char channel_status[24]={0};
1519 struct snd_aes_iec958 iec958;
1520 const char *mixer_ctl_name = "IEC958 Playback PCM Stream";
1521 struct mixer_ctl *ctl;
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +05301522 ALOGV("%s: buffer %s bytes %zd", __func__, buffer, bytes);
Ashish Jain81eb2a82015-05-13 10:52:34 +05301523#ifdef HDMI_PASSTHROUGH_ENABLED
1524 if (audio_extn_is_dolby_format(out->format) &&
1525 /*TODO:Extend code to support DTS passthrough*/
1526 /*set compressed channel status bits*/
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05301527 audio_extn_passthru_is_passthrough_stream(out)){
Ashish Jain81eb2a82015-05-13 10:52:34 +05301528 get_compressed_channel_status(buffer, bytes, channel_status, AUDIO_PARSER_CODEC_AC3);
1529 } else
1530#endif
1531 {
1532 /*set channel status bit for LPCM*/
1533 get_lpcm_channel_status(out->sample_rate, channel_status);
1534 }
1535
1536 memcpy(iec958.status, channel_status,sizeof(iec958.status));
1537 ctl = mixer_get_ctl_by_name(out->dev->mixer, mixer_ctl_name);
1538 if (!ctl) {
1539 ALOGE("%s: Could not get ctl for mixer cmd - %s",
1540 __func__, mixer_ctl_name);
1541 return;
1542 }
1543 if (mixer_ctl_set_array(ctl, &iec958, sizeof(iec958)) < 0) {
1544 ALOGE("%s: Could not set channel status for ext HDMI ",
1545 __func__);
1546 return;
1547 }
1548
1549}
1550#endif