blob: 91e83af0f220f0eccc5219b013aca148bd9dce6d [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
Manish Dewangan3ccdea52017-02-13 19:31:54 +0530158/* payload structure avt_device drift query */
159struct audio_avt_device_drift_stats {
160 uint32_t minor_version;
161 /* Indicates the device interface direction as either
162 * source (Tx) or sink (Rx).
163 */
164 uint16_t device_direction;
165 /*params exposed to client */
166 struct audio_avt_device_drift_param drift_param;
167};
168
Ben Rombergera04fabc2014-11-14 12:16:03 -0800169static char bTable[BASE_TABLE_SIZE] = {
170 'A','B','C','D','E','F','G','H','I','J','K','L',
171 'M','N','O','P','Q','R','S','T','U','V','W','X',
172 'Y','Z','a','b','c','d','e','f','g','h','i','j',
173 'k','l','m','n','o','p','q','r','s','t','u','v',
174 'w','x','y','z','0','1','2','3','4','5','6','7',
175 '8','9','+','/'
176};
177
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700178static uint32_t string_to_enum(const struct string_to_enum *table, size_t size,
179 const char *name)
180{
181 size_t i;
182 for (i = 0; i < size; i++) {
183 if (strcmp(table[i].name, name) == 0) {
184 ALOGV("%s found %s", __func__, table[i].name);
185 return table[i].value;
186 }
187 }
188 return 0;
189}
190
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530191static audio_io_flags_t parse_flag_names(char *name)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700192{
193 uint32_t flag = 0;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530194 audio_io_flags_t io_flags;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800195 char *last_r;
196 char *flag_name = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700197 while (flag_name != NULL) {
198 if (strlen(flag_name) != 0) {
199 flag |= string_to_enum(s_flag_name_to_enum_table,
200 ARRAY_SIZE(s_flag_name_to_enum_table),
201 flag_name);
202 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800203 flag_name = strtok_r(NULL, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700204 }
205
206 ALOGV("parse_flag_names: flag - %d", flag);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530207 io_flags.in_flags = (audio_input_flags_t)flag;
208 io_flags.out_flags = (audio_output_flags_t)flag;
209 return io_flags;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700210}
211
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530212static void parse_format_names(char *name, struct streams_io_cfg *s_info)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700213{
214 struct stream_format *sf_info = NULL;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800215 char *last_r;
216 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700217
218 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG) == 0)
219 return;
220
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530221 list_init(&s_info->format_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700222 while (str != NULL) {
223 audio_format_t format = (audio_format_t)string_to_enum(s_format_name_to_enum_table,
224 ARRAY_SIZE(s_format_name_to_enum_table), str);
225 ALOGV("%s: format - %d", __func__, format);
226 if (format != 0) {
227 sf_info = (struct stream_format *)calloc(1, sizeof(struct stream_format));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700228 if (sf_info == NULL)
229 break; /* return whatever was parsed */
230
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700231 sf_info->format = format;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530232 list_add_tail(&s_info->format_list, &sf_info->list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700233 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800234 str = strtok_r(NULL, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700235 }
236}
237
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530238static void parse_sample_rate_names(char *name, struct streams_io_cfg *s_info)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700239{
Amit Shekhar6f461b12014-08-01 14:52:58 -0700240 struct stream_sample_rate *ss_info = NULL;
241 uint32_t sample_rate = 48000;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800242 char *last_r;
243 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700244
Amit Shekhar6f461b12014-08-01 14:52:58 -0700245 if (str != NULL && 0 == strcmp(str, DYNAMIC_VALUE_TAG))
246 return;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700247
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530248 list_init(&s_info->sample_rate_list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700249 while (str != NULL) {
250 sample_rate = (uint32_t)strtol(str, (char **)NULL, 10);
251 ALOGV("%s: sample_rate - %d", __func__, sample_rate);
252 if (0 != sample_rate) {
253 ss_info = (struct stream_sample_rate *)calloc(1, sizeof(struct stream_sample_rate));
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800254 if (!ss_info) {
255 ALOGE("%s: memory allocation failure", __func__);
256 return;
257 }
Amit Shekhar6f461b12014-08-01 14:52:58 -0700258 ss_info->sample_rate = sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530259 list_add_tail(&s_info->sample_rate_list, &ss_info->list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700260 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800261 str = strtok_r(NULL, "|", &last_r);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700262 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700263}
264
265static int parse_bit_width_names(char *name)
266{
267 int bit_width = 16;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800268 char *last_r;
269 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700270
271 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG))
272 bit_width = (int)strtol(str, (char **)NULL, 10);
273
274 ALOGV("%s: bit_width - %d", __func__, bit_width);
275 return bit_width;
276}
277
278static int parse_app_type_names(void *platform, char *name)
279{
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700280 int app_type = platform_get_default_app_type(platform);
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800281 char *last_r;
282 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700283
284 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG))
285 app_type = (int)strtol(str, (char **)NULL, 10);
286
287 ALOGV("%s: app_type - %d", __func__, app_type);
288 return app_type;
289}
290
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530291static void update_streams_cfg_list(cnode *root, void *platform,
292 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700293{
294 cnode *node = root->first_child;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530295 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700296
297 ALOGV("%s", __func__);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530298 s_info = (struct streams_io_cfg *)calloc(1, sizeof(struct streams_io_cfg));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700299
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530300 if (!s_info) {
301 ALOGE("failed to allocate mem for s_info list element");
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700302 return;
303 }
304
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700305 while (node) {
306 if (strcmp(node->name, FLAGS_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530307 s_info->flags = parse_flag_names((char *)node->value);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530308 } else if (strcmp(node->name, PROFILES_TAG) == 0) {
309 strlcpy(s_info->profile, (char *)node->value, sizeof(s_info->profile));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700310 } else if (strcmp(node->name, FORMATS_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530311 parse_format_names((char *)node->value, s_info);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700312 } else if (strcmp(node->name, SAMPLING_RATES_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530313 s_info->app_type_cfg.sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
314 parse_sample_rate_names((char *)node->value, s_info);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700315 } else if (strcmp(node->name, BIT_WIDTH_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530316 s_info->app_type_cfg.bit_width = parse_bit_width_names((char *)node->value);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700317 } else if (strcmp(node->name, APP_TYPE_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530318 s_info->app_type_cfg.app_type = parse_app_type_names(platform, (char *)node->value);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700319 }
320 node = node->next;
321 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530322 list_add_tail(streams_cfg_list, &s_info->list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700323}
324
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530325static void load_cfg_list(cnode *root, void *platform,
326 struct listnode *streams_output_cfg_list,
327 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700328{
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530329 cnode *node = NULL;
330
331 node = config_find(root, OUTPUTS_TAG);
332 if (node != NULL) {
333 node = node->first_child;
334 while (node) {
335 ALOGV("%s: loading output %s", __func__, node->name);
336 update_streams_cfg_list(node, platform, streams_output_cfg_list);
337 node = node->next;
338 }
339 } else {
340 ALOGI("%s: could not load output, node is NULL", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700341 }
342
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530343 node = config_find(root, INPUTS_TAG);
344 if (node != NULL) {
345 node = node->first_child;
346 while (node) {
347 ALOGV("%s: loading input %s", __func__, node->name);
348 update_streams_cfg_list(node, platform, streams_input_cfg_list);
349 node = node->next;
350 }
351 } else {
352 ALOGI("%s: could not load input, node is NULL", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700353 }
354}
355
356static void send_app_type_cfg(void *platform, struct mixer *mixer,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530357 struct listnode *streams_output_cfg_list,
358 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700359{
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530360 size_t app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT] = {0};
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700361 int length = 0, i, num_app_types = 0;
362 struct listnode *node;
363 bool update;
364 struct mixer_ctl *ctl = NULL;
365 const char *mixer_ctl_name = "App Type Config";
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530366 struct streams_io_cfg *s_info = NULL;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700367
368 if (!mixer) {
369 ALOGE("%s: mixer is null",__func__);
370 return;
371 }
372 ctl = mixer_get_ctl_by_name(mixer, mixer_ctl_name);
373 if (!ctl) {
374 ALOGE("%s: Could not get ctl for mixer cmd - %s",__func__, mixer_ctl_name);
375 return;
376 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530377 app_type_cfg[length++] = num_app_types;
378
379 if (list_empty(streams_output_cfg_list)) {
380 app_type_cfg[length++] = platform_get_default_app_type_v2(platform, PCM_PLAYBACK);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700381 app_type_cfg[length++] = 48000;
382 app_type_cfg[length++] = 16;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530383 num_app_types += 1;
384 }
385 if (list_empty(streams_input_cfg_list)) {
386 app_type_cfg[length++] = platform_get_default_app_type_v2(platform, PCM_CAPTURE);
387 app_type_cfg[length++] = 48000;
388 app_type_cfg[length++] = 16;
389 num_app_types += 1;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700390 }
391
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700392 list_for_each(node, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530393 s_info = node_to_item(node, struct streams_io_cfg, list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700394 update = true;
395 for (i=0; i<length; i=i+3) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530396 if (app_type_cfg[i+1] == 0)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700397 break;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530398 else if (app_type_cfg[i+1] == (size_t)s_info->app_type_cfg.app_type) {
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530399 if (app_type_cfg[i+2] < (size_t)s_info->app_type_cfg.sample_rate)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530400 app_type_cfg[i+2] = s_info->app_type_cfg.sample_rate;
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530401 if (app_type_cfg[i+3] < (size_t)s_info->app_type_cfg.bit_width)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530402 app_type_cfg[i+3] = s_info->app_type_cfg.bit_width;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700403 update = false;
404 break;
405 }
406 }
407 if (update && ((length + 3) <= MAX_LENGTH_MIXER_CONTROL_IN_INT)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530408 num_app_types += 1;
409 app_type_cfg[length++] = s_info->app_type_cfg.app_type;
410 app_type_cfg[length++] = s_info->app_type_cfg.sample_rate;
411 app_type_cfg[length++] = s_info->app_type_cfg.bit_width;
412 }
413 }
414 list_for_each(node, streams_input_cfg_list) {
415 s_info = node_to_item(node, struct streams_io_cfg, list);
416 update = true;
417 for (i=0; i<length; i=i+3) {
418 if (app_type_cfg[i+1] == 0)
419 break;
420 else if (app_type_cfg[i+1] == (size_t)s_info->app_type_cfg.app_type) {
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530421 if (app_type_cfg[i+2] < (size_t)s_info->app_type_cfg.sample_rate)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530422 app_type_cfg[i+2] = s_info->app_type_cfg.sample_rate;
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530423 if (app_type_cfg[i+3] < (size_t)s_info->app_type_cfg.bit_width)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530424 app_type_cfg[i+3] = s_info->app_type_cfg.bit_width;
425 update = false;
426 break;
427 }
428 }
429 if (update && ((length + 3) <= MAX_LENGTH_MIXER_CONTROL_IN_INT)) {
430 num_app_types += 1;
431 app_type_cfg[length++] = s_info->app_type_cfg.app_type;
432 app_type_cfg[length++] = s_info->app_type_cfg.sample_rate;
433 app_type_cfg[length++] = s_info->app_type_cfg.bit_width;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700434 }
435 }
436 ALOGV("%s: num_app_types: %d", __func__, num_app_types);
437 if (num_app_types) {
438 app_type_cfg[0] = num_app_types;
439 mixer_ctl_set_array(ctl, app_type_cfg, length);
440 }
441}
442
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530443void audio_extn_utils_update_streams_cfg_lists(void *platform,
444 struct mixer *mixer,
445 struct listnode *streams_output_cfg_list,
446 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700447{
448 cnode *root;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530449 char *data = NULL;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700450
451 ALOGV("%s", __func__);
452 list_init(streams_output_cfg_list);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530453 list_init(streams_input_cfg_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700454
455 root = config_node("", "");
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700456 if (root == NULL) {
457 ALOGE("cfg_list, NULL config root");
458 return;
459 }
460
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530461 data = (char *)load_file(AUDIO_IO_POLICY_VENDOR_CONFIG_FILE, NULL);
462 if (data == NULL) {
463 ALOGD("%s: failed to open io config file(%s), trying older config file",
464 __func__, AUDIO_IO_POLICY_VENDOR_CONFIG_FILE);
465 data = (char *)load_file(AUDIO_OUTPUT_POLICY_VENDOR_CONFIG_FILE, NULL);
466 if (data == NULL) {
467 send_app_type_cfg(platform, mixer,
468 streams_output_cfg_list,
469 streams_input_cfg_list);
470 ALOGE("%s: could not load io policy config!", __func__);
471 return;
472 }
473 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700474
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530475 config_load(root, data);
476 load_cfg_list(root, platform, streams_output_cfg_list,
477 streams_input_cfg_list);
478
479 send_app_type_cfg(platform, mixer, streams_output_cfg_list,
480 streams_input_cfg_list);
Alexy Josephaee4fdd2016-01-29 13:02:07 -0800481
482 config_free(root);
483 free(data);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700484}
485
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530486static void audio_extn_utils_dump_streams_cfg_list(
487 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700488{
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700489 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530490 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700491 struct stream_format *sf_info;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700492 struct stream_sample_rate *ss_info;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530493
494 list_for_each(node_i, streams_cfg_list) {
495 s_info = node_to_item(node_i, struct streams_io_cfg, list);
496 ALOGV("%s: flags-%d, sample_rate-%d, bit_width-%d, app_type-%d",
497 __func__, s_info->flags.out_flags, s_info->app_type_cfg.sample_rate,
498 s_info->app_type_cfg.bit_width, s_info->app_type_cfg.app_type);
499 list_for_each(node_j, &s_info->format_list) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700500 sf_info = node_to_item(node_j, struct stream_format, list);
501 ALOGV("format-%x", sf_info->format);
502 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530503 list_for_each(node_j, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700504 ss_info = node_to_item(node_j, struct stream_sample_rate, list);
505 ALOGV("sample rate-%d", ss_info->sample_rate);
506 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700507 }
508}
509
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530510void audio_extn_utils_dump_streams_cfg_lists(
511 struct listnode *streams_output_cfg_list,
512 struct listnode *streams_input_cfg_list)
513{
514 ALOGV("%s", __func__);
515 audio_extn_utils_dump_streams_cfg_list(streams_output_cfg_list);
516 audio_extn_utils_dump_streams_cfg_list(streams_input_cfg_list);
517}
518
519static void audio_extn_utils_release_streams_cfg_list(
520 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700521{
522 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530523 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700524
525 ALOGV("%s", __func__);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530526
527 while (!list_empty(streams_cfg_list)) {
528 node_i = list_head(streams_cfg_list);
529 s_info = node_to_item(node_i, struct streams_io_cfg, list);
530 while (!list_empty(&s_info->format_list)) {
531 node_j = list_head(&s_info->format_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700532 list_remove(node_j);
533 free(node_to_item(node_j, struct stream_format, list));
534 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530535 while (!list_empty(&s_info->sample_rate_list)) {
536 node_j = list_head(&s_info->sample_rate_list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700537 list_remove(node_j);
538 free(node_to_item(node_j, struct stream_sample_rate, list));
539 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700540 list_remove(node_i);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530541 free(node_to_item(node_i, struct streams_io_cfg, list));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700542 }
543}
544
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530545void audio_extn_utils_release_streams_cfg_lists(
546 struct listnode *streams_output_cfg_list,
547 struct listnode *streams_input_cfg_list)
548{
549 ALOGV("%s", __func__);
550 audio_extn_utils_release_streams_cfg_list(streams_output_cfg_list);
551 audio_extn_utils_release_streams_cfg_list(streams_input_cfg_list);
552}
553
554static bool set_app_type_cfg(struct streams_io_cfg *s_info,
555 struct stream_app_type_cfg *app_type_cfg,
556 uint32_t sample_rate, uint32_t bit_width)
Amit Shekhar6f461b12014-08-01 14:52:58 -0700557 {
558 struct listnode *node_i;
559 struct stream_sample_rate *ss_info;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530560 list_for_each(node_i, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700561 ss_info = node_to_item(node_i, struct stream_sample_rate, list);
562 if ((sample_rate <= ss_info->sample_rate) &&
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530563 (bit_width == s_info->app_type_cfg.bit_width)) {
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700564
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530565 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700566 app_type_cfg->sample_rate = ss_info->sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530567 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700568 ALOGV("%s app_type_cfg->app_type %d, app_type_cfg->sample_rate %d, app_type_cfg->bit_width %d",
569 __func__, app_type_cfg->app_type, app_type_cfg->sample_rate, app_type_cfg->bit_width);
570 return true;
571 }
572 }
573 /*
574 * Reiterate through the list assuming dafault sample rate.
575 * Handles scenario where input sample rate is higher
576 * than all sample rates in list for the input bit width.
577 */
578 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
Apoorv Raghuvanshif59bb222015-02-18 12:23:23 -0800579
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530580 list_for_each(node_i, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700581 ss_info = node_to_item(node_i, struct stream_sample_rate, list);
582 if ((sample_rate <= ss_info->sample_rate) &&
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530583 (bit_width == s_info->app_type_cfg.bit_width)) {
584 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700585 app_type_cfg->sample_rate = sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530586 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Apoorv Raghuvanshif59bb222015-02-18 12:23:23 -0800587 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 -0700588 __func__, app_type_cfg->app_type, app_type_cfg->sample_rate, app_type_cfg->bit_width);
589 return true;
590 }
591 }
592 return false;
593}
594
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530595void audio_extn_utils_update_stream_input_app_type_cfg(void *platform,
596 struct listnode *streams_input_cfg_list,
597 audio_devices_t devices __unused,
598 audio_input_flags_t flags,
599 audio_format_t format,
600 uint32_t sample_rate,
601 uint32_t bit_width,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530602 char* profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530603 struct stream_app_type_cfg *app_type_cfg)
604{
605 struct listnode *node_i, *node_j;
606 struct streams_io_cfg *s_info;
607 struct stream_format *sf_info;
608
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530609 ALOGV("%s: flags: 0x%x, format: 0x%x sample_rate %d, profile %s",
610 __func__, flags, format, sample_rate, profile);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530611
612 list_for_each(node_i, streams_input_cfg_list) {
613 s_info = node_to_item(node_i, struct streams_io_cfg, list);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530614 /* Along with flags do profile matching if set at either end.*/
615 if (s_info->flags.in_flags == flags &&
616 ((profile[0] == '\0' && s_info->profile[0] == '\0') ||
617 strncmp(s_info->profile, profile, sizeof(s_info->profile)) == 0)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530618 list_for_each(node_j, &s_info->format_list) {
619 sf_info = node_to_item(node_j, struct stream_format, list);
620 if (sf_info->format == format) {
621 if (set_app_type_cfg(s_info, app_type_cfg, sample_rate, bit_width))
622 return;
623 }
624 }
625 }
626 }
627 ALOGW("%s: App type could not be selected. Falling back to default", __func__);
628 app_type_cfg->app_type = platform_get_default_app_type_v2(platform, PCM_CAPTURE);
629 app_type_cfg->sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
630 app_type_cfg->bit_width = 16;
631}
632
633void audio_extn_utils_update_stream_output_app_type_cfg(void *platform,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700634 struct listnode *streams_output_cfg_list,
Amit Shekhar1d896042014-10-03 13:16:09 -0700635 audio_devices_t devices,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700636 audio_output_flags_t flags,
637 audio_format_t format,
Amit Shekhar6f461b12014-08-01 14:52:58 -0700638 uint32_t sample_rate,
639 uint32_t bit_width,
Manish Dewangan837dc462015-05-27 10:17:41 +0530640 audio_channel_mask_t channel_mask,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530641 char *profile,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700642 struct stream_app_type_cfg *app_type_cfg)
643{
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +0530644 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530645 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700646 struct stream_format *sf_info;
Manish Dewangan837dc462015-05-27 10:17:41 +0530647 char value[PROPERTY_VALUE_MAX] = {0};
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700648
Ashish Jain058165c2016-09-28 23:18:48 +0530649 if ((bit_width >= 24) &&
Amit Shekhar1d896042014-10-03 13:16:09 -0700650 (devices & AUDIO_DEVICE_OUT_SPEAKER)) {
Amit Shekhar5a39c912014-10-14 15:39:30 -0700651 int32_t bw = platform_get_snd_device_bit_width(SND_DEVICE_OUT_SPEAKER);
652 if (-ENOSYS != bw)
653 bit_width = (uint32_t)bw;
Amit Shekhar1d896042014-10-03 13:16:09 -0700654 sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
655 ALOGI("%s Allowing 24-bit playback on speaker ONLY at default sampling rate", __func__);
656 }
657
Manish Dewangan837dc462015-05-27 10:17:41 +0530658 property_get("audio.playback.mch.downsample",value,"");
659 if (!strncmp("true", value, sizeof("true"))) {
660 if ((popcount(channel_mask) > 2) &&
661 (sample_rate > CODEC_BACKEND_DEFAULT_SAMPLE_RATE) &&
662 !(flags & AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH)) {
663 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
664 ALOGD("%s: MCH session defaulting sample rate to %d",
665 __func__, sample_rate);
666 }
667 }
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +0530668
669 /* Set sampling rate to 176.4 for DSD64
670 * and 352.8Khz for DSD128.
671 * Set Bit Width to 16. output will be 16 bit
672 * post DoP in ASM.
673 */
674 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH) &&
675 (format == AUDIO_FORMAT_DSD)) {
676 bit_width = 16;
677 if (sample_rate == INPUT_SAMPLING_RATE_DSD64)
678 sample_rate = OUTPUT_SAMPLING_RATE_DSD64;
679 else if (sample_rate == INPUT_SAMPLING_RATE_DSD128)
680 sample_rate = OUTPUT_SAMPLING_RATE_DSD128;
681 }
682
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +0530683 if(devices & AUDIO_DEVICE_OUT_ALL_A2DP) {
684 //TODO: Handle fractional sampling rate configuration for LL
685 audio_extn_a2dp_get_apptype_params(&sample_rate, &bit_width);
686 ALOGI("%s using %d sampling rate %d bit width for A2DP CoPP",
687 __func__, sample_rate, bit_width);
688 }
689
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530690 ALOGV("%s: flags: %x, format: %x sample_rate %d, profile %s",
691 __func__, flags, format, sample_rate, profile);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700692 list_for_each(node_i, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530693 s_info = node_to_item(node_i, struct streams_io_cfg, list);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530694 /* Along with flags do profile matching if set at either end.*/
695 if (s_info->flags.out_flags == flags &&
696 ((profile[0] == '\0' && s_info->profile[0] == '\0') ||
697 strncmp(s_info->profile, profile, sizeof(s_info->profile)) == 0)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530698 list_for_each(node_j, &s_info->format_list) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700699 sf_info = node_to_item(node_j, struct stream_format, list);
700 if (sf_info->format == format) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530701 if (set_app_type_cfg(s_info, app_type_cfg, sample_rate, bit_width))
Amit Shekhar6f461b12014-08-01 14:52:58 -0700702 return;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700703 }
704 }
705 }
706 }
707 list_for_each(node_i, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530708 s_info = node_to_item(node_i, struct streams_io_cfg, list);
709 if (s_info->flags.out_flags == AUDIO_OUTPUT_FLAG_PRIMARY) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700710 ALOGV("Compatible output profile not found.");
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530711 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
712 app_type_cfg->sample_rate = s_info->app_type_cfg.sample_rate;
713 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700714 ALOGV("%s Default to primary output: App type: %d sample_rate %d",
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530715 __func__, s_info->app_type_cfg.app_type, app_type_cfg->sample_rate);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700716 return;
717 }
718 }
719 ALOGW("%s: App type could not be selected. Falling back to default", __func__);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700720 app_type_cfg->app_type = platform_get_default_app_type(platform);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700721 app_type_cfg->sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700722 app_type_cfg->bit_width = 16;
723}
724
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +0530725static bool audio_is_this_native_usecase(struct audio_usecase *uc)
726{
727 bool native_usecase = false;
728 struct stream_out *out = (struct stream_out*) uc->stream.out;
729
730 if (PCM_PLAYBACK == uc->type && out != NULL &&
731 NATIVE_AUDIO_MODE_INVALID != platform_get_native_support() &&
732 is_offload_usecase(uc->id) &&
733 (out->sample_rate == OUTPUT_SAMPLING_RATE_44100))
734 native_usecase = true;
735
736 return native_usecase;
737}
738
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530739void audio_extn_utils_update_stream_app_type_cfg_for_usecase(
740 struct audio_device *adev,
741 struct audio_usecase *usecase)
742{
743 ALOGV("%s", __func__);
744
745 switch(usecase->type) {
746 case PCM_PLAYBACK:
747 audio_extn_utils_update_stream_output_app_type_cfg(adev->platform,
748 &adev->streams_output_cfg_list,
749 usecase->stream.out->devices,
750 usecase->stream.out->flags,
751 usecase->stream.out->format,
752 usecase->stream.out->sample_rate,
753 usecase->stream.out->bit_width,
754 usecase->stream.out->channel_mask,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530755 usecase->stream.out->profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530756 &usecase->stream.out->app_type_cfg);
757 ALOGV("%s Selected apptype: %d", __func__, usecase->stream.out->app_type_cfg.app_type);
758 break;
759 case PCM_CAPTURE:
760 audio_extn_utils_update_stream_input_app_type_cfg(adev->platform,
761 &adev->streams_input_cfg_list,
762 usecase->stream.in->device,
763 usecase->stream.in->flags,
764 usecase->stream.in->format,
765 usecase->stream.in->sample_rate,
766 usecase->stream.in->bit_width,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530767 usecase->stream.in->profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530768 &usecase->stream.in->app_type_cfg);
769 ALOGV("%s Selected apptype: %d", __func__, usecase->stream.in->app_type_cfg.app_type);
770 break;
771 default:
772 ALOGE("%s: app type cfg not supported for usecase type (%d)",
773 __func__, usecase->type);
774 }
775}
776
Siena Richard7c2db772016-12-21 11:32:34 -0800777static int send_app_type_cfg_for_device(struct audio_device *adev,
778 struct audio_usecase *usecase,
779 int split_snd_device)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700780{
781 char mixer_ctl_name[MAX_LENGTH_MIXER_CONTROL_IN_INT];
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530782 size_t app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT] = {0};
783 int len = 0, rc;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700784 struct mixer_ctl *ctl;
Siena Richard7c2db772016-12-21 11:32:34 -0800785 int pcm_device_id = 0, acdb_dev_id, app_type;
786 int snd_device = split_snd_device, snd_device_be_idx = -1;
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +0530787 int32_t sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Manish Dewangan837dc462015-05-27 10:17:41 +0530788 char value[PROPERTY_VALUE_MAX] = {0};
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700789
Siena Richard7c2db772016-12-21 11:32:34 -0800790 ALOGV("%s: usecase->out_snd_device %s, usecase->in_snd_device %s, split_snd_device %s",
791 __func__, platform_get_snd_device_name(usecase->out_snd_device),
792 platform_get_snd_device_name(usecase->in_snd_device),
793 platform_get_snd_device_name(split_snd_device));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700794
Srikanth Uyyala9d551402015-08-25 16:03:42 +0530795 if (usecase->type != PCM_PLAYBACK && usecase->type != PCM_CAPTURE) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530796 ALOGE("%s: not a playback/capture path, no need to cfg app type", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700797 rc = 0;
798 goto exit_send_app_type_cfg;
799 }
800 if ((usecase->id != USECASE_AUDIO_PLAYBACK_DEEP_BUFFER) &&
801 (usecase->id != USECASE_AUDIO_PLAYBACK_LOW_LATENCY) &&
802 (usecase->id != USECASE_AUDIO_PLAYBACK_MULTI_CH) &&
Srikanth Uyyala9d551402015-08-25 16:03:42 +0530803 (!is_offload_usecase(usecase->id)) &&
804 (usecase->type != PCM_CAPTURE)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530805 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 -0700806 rc = 0;
807 goto exit_send_app_type_cfg;
808 }
Srikanth Uyyala9d551402015-08-25 16:03:42 +0530809 if (usecase->type == PCM_PLAYBACK) {
Ben Romberger1fafdde2015-09-09 19:43:15 -0700810 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_PLAYBACK);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530811 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
812 "Audio Stream %d App Type Cfg", pcm_device_id);
Ben Romberger1fafdde2015-09-09 19:43:15 -0700813 } else if (usecase->type == PCM_CAPTURE) {
Ben Romberger1fafdde2015-09-09 19:43:15 -0700814 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_CAPTURE);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530815 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
816 "Audio Stream Capture %d App Type Cfg", pcm_device_id);
Srikanth Uyyala9d551402015-08-25 16:03:42 +0530817 }
Siena Richard7c2db772016-12-21 11:32:34 -0800818
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700819 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
820 if (!ctl) {
821 ALOGE("%s: Could not get ctl for mixer cmd - %s", __func__,
822 mixer_ctl_name);
823 rc = -EINVAL;
824 goto exit_send_app_type_cfg;
825 }
826 snd_device = (snd_device == SND_DEVICE_OUT_SPEAKER) ?
Xiaojun Sang040cc9f2015-08-03 19:38:28 +0800827 platform_get_spkr_prot_snd_device(snd_device) : snd_device;
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +0530828
Rohit Kumar1181b292017-01-31 18:07:17 +0530829 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
830 if (acdb_dev_id <= 0) {
831 ALOGE("%s: Couldn't get the acdb dev id", __func__);
832 rc = -EINVAL;
833 goto exit_send_app_type_cfg;
834 }
835
Siena Richard7c2db772016-12-21 11:32:34 -0800836 snd_device_be_idx = platform_get_snd_device_backend_index(snd_device);
837 if (snd_device_be_idx < 0) {
838 ALOGE("%s: Couldn't get the backend index for snd device %s ret=%d",
839 __func__, platform_get_snd_device_name(snd_device),
840 snd_device_be_idx);
841 }
842
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530843 if ((usecase->type == PCM_PLAYBACK) && (usecase->stream.out != NULL)) {
Manish Dewangan837dc462015-05-27 10:17:41 +0530844
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530845 property_get("audio.playback.mch.downsample",value,"");
846 if (!strncmp("true", value, sizeof("true"))) {
847 if ((popcount(usecase->stream.out->channel_mask) > 2) &&
848 (usecase->stream.out->app_type_cfg.sample_rate > CODEC_BACKEND_DEFAULT_SAMPLE_RATE) &&
849 !(usecase->stream.out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH))
850 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
851 }
Ashish Jainc02430d2016-01-04 10:42:43 +0530852
Ashish Jain4826f6c2017-02-06 13:33:20 +0530853 if (usecase->stream.out->devices & AUDIO_DEVICE_OUT_SPEAKER) {
Mingming Yin21854652016-04-13 11:54:02 -0700854 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Ashish Jaina052e572016-11-07 16:41:07 +0530855 } else if ((snd_device == SND_DEVICE_OUT_HDMI ||
856 snd_device == SND_DEVICE_OUT_USB_HEADSET ||
857 snd_device == SND_DEVICE_OUT_DISPLAY_PORT) &&
858 (usecase->stream.out->sample_rate >= OUTPUT_SAMPLING_RATE_44100)) {
859 /*
860 * To best utlize DSP, check if the stream sample rate is supported/multiple of
861 * configured device sample rate, if not update the COPP rate to be equal to the
862 * device sample rate, else open COPP at stream sample rate
863 */
864 platform_check_and_update_copp_sample_rate(adev->platform, snd_device,
865 usecase->stream.out->sample_rate,
866 &usecase->stream.out->app_type_cfg.sample_rate);
Ashish Jain4826f6c2017-02-06 13:33:20 +0530867 } else if (((snd_device != SND_DEVICE_OUT_HEADPHONES_44_1 &&
868 !audio_is_this_native_usecase(usecase)) &&
Mingming Yin21854652016-04-13 11:54:02 -0700869 usecase->stream.out->sample_rate == OUTPUT_SAMPLING_RATE_44100) ||
870 (usecase->stream.out->sample_rate < OUTPUT_SAMPLING_RATE_44100)) {
Ashish Jain4826f6c2017-02-06 13:33:20 +0530871 /* Reset to default if no native stream is active*/
Mingming Yin21854652016-04-13 11:54:02 -0700872 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
873 }
874 sample_rate = usecase->stream.out->app_type_cfg.sample_rate;
875
Siena Richard7c2db772016-12-21 11:32:34 -0800876 app_type = usecase->stream.out->app_type_cfg.app_type;
877 app_type_cfg[len++] = app_type;
Mingming Yin21854652016-04-13 11:54:02 -0700878 app_type_cfg[len++] = acdb_dev_id;
879 if (((usecase->stream.out->format == AUDIO_FORMAT_E_AC3) ||
880 (usecase->stream.out->format == AUDIO_FORMAT_E_AC3_JOC))
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530881 && audio_extn_passthru_is_passthrough_stream(usecase->stream.out)) {
Mingming Yin21854652016-04-13 11:54:02 -0700882 app_type_cfg[len++] = sample_rate * 4;
883 } else {
884 app_type_cfg[len++] = sample_rate;
885 }
Siena Richard7c2db772016-12-21 11:32:34 -0800886 if (snd_device_be_idx > 0)
887 app_type_cfg[len++] = snd_device_be_idx;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530888
Siena Richard7c2db772016-12-21 11:32:34 -0800889 ALOGI("%s PLAYBACK 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
892 } else if ((usecase->type == PCM_CAPTURE) && (usecase->stream.in != NULL)) {
Siena Richard7c2db772016-12-21 11:32:34 -0800893 app_type = usecase->stream.in->app_type_cfg.app_type;
894 app_type_cfg[len++] = app_type;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530895 app_type_cfg[len++] = acdb_dev_id;
Siena Richard7c2db772016-12-21 11:32:34 -0800896 sample_rate = usecase->stream.in->app_type_cfg.sample_rate;
897 app_type_cfg[len++] = sample_rate;
898 if (snd_device_be_idx > 0)
899 app_type_cfg[len++] = snd_device_be_idx;
900 ALOGI("%s CAPTURE app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
901 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530902 } else {
Siena Richard7c2db772016-12-21 11:32:34 -0800903 app_type = platform_get_default_app_type_v2(adev->platform, usecase->type);
904 app_type_cfg[len++] = app_type;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530905 app_type_cfg[len++] = acdb_dev_id;
906 app_type_cfg[len++] = sample_rate;
Siena Richard7c2db772016-12-21 11:32:34 -0800907 if (snd_device_be_idx > 0)
908 app_type_cfg[len++] = snd_device_be_idx;
909 ALOGI("%s default app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
910 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Ashish Jainc02430d2016-01-04 10:42:43 +0530911 }
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +0530912
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700913 mixer_ctl_set_array(ctl, app_type_cfg, len);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700914 rc = 0;
915exit_send_app_type_cfg:
916 return rc;
917}
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700918
Siena Richard7c2db772016-12-21 11:32:34 -0800919int audio_extn_utils_send_app_type_cfg(struct audio_device *adev,
920 struct audio_usecase *usecase)
921{
922 int i, num_devices = 0;
923 snd_device_t new_snd_devices[SND_DEVICE_OUT_END] = {0};
924 int rc = 0;
925
926 switch (usecase->type) {
927 case PCM_PLAYBACK:
928 ALOGD("%s: usecase->out_snd_device %s",
929 __func__, platform_get_snd_device_name(usecase->out_snd_device));
930 /* check for out combo device */
931 if (platform_split_snd_device(adev->platform,
932 usecase->out_snd_device,
933 &num_devices, new_snd_devices)) {
934 new_snd_devices[0] = usecase->out_snd_device;
935 num_devices = 1;
936 }
937 break;
938 case PCM_CAPTURE:
939 ALOGD("%s: usecase->in_snd_device %s",
940 __func__, platform_get_snd_device_name(usecase->in_snd_device));
941 /* check for in combo device */
942 if (platform_split_snd_device(adev->platform,
943 usecase->in_snd_device,
944 &num_devices, new_snd_devices)) {
945 new_snd_devices[0] = usecase->in_snd_device;
946 num_devices = 1;
947 }
948 break;
949 default:
950 ALOGI("%s: not a playback/capture path, no need to cfg app type", __func__);
951 rc = 0;
952 break;
953 }
954
955 for (i = 0; i < num_devices; i++) {
956 rc = send_app_type_cfg_for_device(adev, usecase, new_snd_devices[i]);
957 if (rc)
958 break;
959 }
960
961 return rc;
962}
963
Preetam Singh Ranawat9519e9c2015-11-18 16:05:55 +0530964int read_line_from_file(const char *path, char *buf, size_t count)
965{
966 char * fgets_ret;
967 FILE * fd;
968 int rv;
969
970 fd = fopen(path, "r");
971 if (fd == NULL)
972 return -1;
973
974 fgets_ret = fgets(buf, (int)count, fd);
975 if (NULL != fgets_ret) {
976 rv = (int)strlen(buf);
977 } else {
978 rv = ferror(fd);
979 }
980 fclose(fd);
981
982 return rv;
983}
984
Ashish Jainf1eaa582016-05-23 20:54:24 +0530985/*Translates ALSA formats to AOSP PCM formats*/
986audio_format_t alsa_format_to_hal(uint32_t alsa_format)
987{
988 audio_format_t format;
989
990 switch(alsa_format) {
991 case SNDRV_PCM_FORMAT_S16_LE:
992 format = AUDIO_FORMAT_PCM_16_BIT;
993 break;
994 case SNDRV_PCM_FORMAT_S24_3LE:
995 format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
996 break;
997 case SNDRV_PCM_FORMAT_S24_LE:
998 format = AUDIO_FORMAT_PCM_8_24_BIT;
999 break;
1000 case SNDRV_PCM_FORMAT_S32_LE:
1001 format = AUDIO_FORMAT_PCM_32_BIT;
1002 break;
1003 default:
1004 ALOGW("Incorrect ALSA format");
1005 format = AUDIO_FORMAT_INVALID;
1006 }
1007 return format;
1008}
1009
1010/*Translates hal format (AOSP) to alsa formats*/
1011uint32_t hal_format_to_alsa(audio_format_t hal_format)
1012{
1013 uint32_t alsa_format;
1014
1015 switch (hal_format) {
1016 case AUDIO_FORMAT_PCM_32_BIT: {
1017 if (platform_supports_true_32bit())
1018 alsa_format = SNDRV_PCM_FORMAT_S32_LE;
1019 else
1020 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1021 }
1022 break;
1023 case AUDIO_FORMAT_PCM_8_BIT:
1024 alsa_format = SNDRV_PCM_FORMAT_S8;
1025 break;
1026 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
1027 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1028 break;
1029 case AUDIO_FORMAT_PCM_8_24_BIT: {
1030 if (platform_supports_true_32bit())
1031 alsa_format = SNDRV_PCM_FORMAT_S32_LE;
1032 else
1033 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1034 }
1035 break;
1036 case AUDIO_FORMAT_PCM_FLOAT:
1037 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1038 break;
1039 default:
1040 case AUDIO_FORMAT_PCM_16_BIT:
1041 alsa_format = SNDRV_PCM_FORMAT_S16_LE;
1042 break;
1043 }
1044 return alsa_format;
1045}
1046
Ashish Jain83a6cc22016-06-28 14:34:17 +05301047/*Translates PCM formats to AOSP formats*/
1048audio_format_t pcm_format_to_hal(uint32_t pcm_format)
1049{
1050 audio_format_t format = AUDIO_FORMAT_INVALID;
1051
1052 switch(pcm_format) {
1053 case PCM_FORMAT_S16_LE:
1054 format = AUDIO_FORMAT_PCM_16_BIT;
1055 break;
1056 case PCM_FORMAT_S24_3LE:
1057 format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
1058 break;
1059 case PCM_FORMAT_S24_LE:
1060 format = AUDIO_FORMAT_PCM_8_24_BIT;
1061 break;
1062 case PCM_FORMAT_S32_LE:
1063 format = AUDIO_FORMAT_PCM_32_BIT;
1064 break;
1065 default:
1066 ALOGW("Incorrect PCM format");
1067 format = AUDIO_FORMAT_INVALID;
1068 }
1069 return format;
1070}
1071
1072/*Translates hal format (AOSP) to alsa formats*/
1073uint32_t hal_format_to_pcm(audio_format_t hal_format)
1074{
1075 uint32_t pcm_format;
1076
1077 switch (hal_format) {
1078 case AUDIO_FORMAT_PCM_32_BIT:
1079 case AUDIO_FORMAT_PCM_8_24_BIT:
1080 case AUDIO_FORMAT_PCM_FLOAT: {
1081 if (platform_supports_true_32bit())
1082 pcm_format = PCM_FORMAT_S32_LE;
1083 else
1084 pcm_format = PCM_FORMAT_S24_3LE;
1085 }
1086 break;
1087 case AUDIO_FORMAT_PCM_8_BIT:
1088 pcm_format = PCM_FORMAT_S8;
1089 break;
1090 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
1091 pcm_format = PCM_FORMAT_S24_3LE;
1092 break;
1093 default:
1094 case AUDIO_FORMAT_PCM_16_BIT:
1095 pcm_format = PCM_FORMAT_S16_LE;
1096 break;
1097 }
1098 return pcm_format;
1099}
1100
Ashish Jainf1eaa582016-05-23 20:54:24 +05301101uint32_t get_alsa_fragment_size(uint32_t bytes_per_sample,
1102 uint32_t sample_rate,
1103 uint32_t noOfChannels)
1104{
1105 uint32_t fragment_size = 0;
1106 uint32_t pcm_offload_time = PCM_OFFLOAD_BUFFER_DURATION;
1107
1108 fragment_size = (pcm_offload_time
1109 * sample_rate
1110 * bytes_per_sample
1111 * noOfChannels)/1000;
1112 if (fragment_size < MIN_PCM_OFFLOAD_FRAGMENT_SIZE)
1113 fragment_size = MIN_PCM_OFFLOAD_FRAGMENT_SIZE;
1114 else if (fragment_size > MAX_PCM_OFFLOAD_FRAGMENT_SIZE)
1115 fragment_size = MAX_PCM_OFFLOAD_FRAGMENT_SIZE;
1116 /*To have same PCM samples for all channels, the buffer size requires to
1117 *be multiple of (number of channels * bytes per sample)
1118 *For writes to succeed, the buffer must be written at address which is multiple of 32
1119 */
1120 fragment_size = ALIGN(fragment_size, (bytes_per_sample * noOfChannels * 32));
1121
1122 ALOGI("PCM offload Fragment size to %d bytes", fragment_size);
1123 return fragment_size;
1124}
1125
1126/* Calculates the fragment size required to configure compress session.
1127 * Based on the alsa format selected, decide if conversion is needed in
1128
1129 * HAL ( e.g. convert AUDIO_FORMAT_PCM_FLOAT input format to
1130 * AUDIO_FORMAT_PCM_24_BIT_PACKED before writing to the compress driver.
1131 */
1132void audio_extn_utils_update_direct_pcm_fragment_size(struct stream_out *out)
1133{
Ashish Jain83a6cc22016-06-28 14:34:17 +05301134 audio_format_t dst_format = out->hal_op_format;
1135 audio_format_t src_format = out->hal_ip_format;
Ashish Jainf1eaa582016-05-23 20:54:24 +05301136 uint32_t hal_op_bytes_per_sample = audio_bytes_per_sample(dst_format);
1137 uint32_t hal_ip_bytes_per_sample = audio_bytes_per_sample(src_format);
1138
1139 out->compr_config.fragment_size =
1140 get_alsa_fragment_size(hal_op_bytes_per_sample,
1141 out->sample_rate,
1142 popcount(out->channel_mask));
1143
1144 if ((src_format != dst_format) &&
1145 hal_op_bytes_per_sample != hal_ip_bytes_per_sample) {
1146
Ashish Jain83a6cc22016-06-28 14:34:17 +05301147 out->hal_fragment_size =
Ashish Jainf1eaa582016-05-23 20:54:24 +05301148 ((out->compr_config.fragment_size * hal_ip_bytes_per_sample) /
1149 hal_op_bytes_per_sample);
1150 ALOGI("enable conversion hal_input_fragment_size is %d src_format %x dst_format %x",
Ashish Jain83a6cc22016-06-28 14:34:17 +05301151 out->hal_fragment_size, src_format, dst_format);
Ashish Jainf1eaa582016-05-23 20:54:24 +05301152 } else {
Ashish Jain83a6cc22016-06-28 14:34:17 +05301153 out->hal_fragment_size = out->compr_config.fragment_size;
Ashish Jainf1eaa582016-05-23 20:54:24 +05301154 }
1155}
1156
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301157/* converts pcm format 24_8 to 8_24 inplace */
1158size_t audio_extn_utils_convert_format_24_8_to_8_24(void *buf, size_t bytes)
1159{
1160 size_t i = 0;
1161 int *int_buf_stream = buf;
1162
1163 if ((bytes % 4) != 0) {
1164 ALOGE("%s: wrong inout buffer! ... is not 32 bit aligned ", __func__);
1165 return -EINVAL;
1166 }
1167
1168 for (; i < (bytes / 4); i++)
1169 int_buf_stream[i] >>= 8;
1170
1171 return bytes;
1172}
1173
1174int get_snd_codec_id(audio_format_t format)
1175{
1176 int id = 0;
1177
1178 switch (format & AUDIO_FORMAT_MAIN_MASK) {
1179 case AUDIO_FORMAT_MP3:
1180 id = SND_AUDIOCODEC_MP3;
1181 break;
1182 case AUDIO_FORMAT_AAC:
1183 id = SND_AUDIOCODEC_AAC;
1184 break;
1185 case AUDIO_FORMAT_AAC_ADTS:
1186 id = SND_AUDIOCODEC_AAC;
1187 break;
Arun Kumar Dasari3b174182016-12-27 13:01:14 +05301188 case AUDIO_FORMAT_AAC_LATM:
1189 id = SND_AUDIOCODEC_AAC;
1190 break;
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301191 case AUDIO_FORMAT_PCM_OFFLOAD:
1192 case AUDIO_FORMAT_PCM:
1193 id = SND_AUDIOCODEC_PCM;
1194 break;
1195 case AUDIO_FORMAT_FLAC:
1196 id = SND_AUDIOCODEC_FLAC;
1197 break;
1198 case AUDIO_FORMAT_ALAC:
1199 id = SND_AUDIOCODEC_ALAC;
1200 break;
1201 case AUDIO_FORMAT_APE:
1202 id = SND_AUDIOCODEC_APE;
1203 break;
1204 case AUDIO_FORMAT_VORBIS:
1205 id = SND_AUDIOCODEC_VORBIS;
1206 break;
1207 case AUDIO_FORMAT_WMA:
1208 id = SND_AUDIOCODEC_WMA;
1209 break;
1210 case AUDIO_FORMAT_WMA_PRO:
1211 id = SND_AUDIOCODEC_WMA_PRO;
1212 break;
Satish Babu Patakokila0c313922016-12-08 12:07:08 +05301213 case AUDIO_FORMAT_MP2:
1214 id = SND_AUDIOCODEC_MP2;
1215 break;
Satish Babu Patakokila915ecba2017-01-10 17:43:56 +05301216 case AUDIO_FORMAT_AC3:
1217 id = SND_AUDIOCODEC_AC3;
1218 break;
1219 case AUDIO_FORMAT_E_AC3:
1220 case AUDIO_FORMAT_E_AC3_JOC:
1221 id = SND_AUDIOCODEC_EAC3;
1222 break;
1223 case AUDIO_FORMAT_DTS:
1224 case AUDIO_FORMAT_DTS_HD:
1225 id = SND_AUDIOCODEC_DTS;
1226 break;
Preetam Singh Ranawat4277a5a2017-01-18 19:02:24 +05301227 case AUDIO_FORMAT_DSD:
1228 id = SND_AUDIOCODEC_DSD;
1229 break;
Dhanalakshmi Siddani18737932016-11-29 17:33:17 +05301230 case AUDIO_FORMAT_APTX:
1231 id = SND_AUDIOCODEC_APTX;
1232 break;
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301233 default:
1234 ALOGE("%s: Unsupported audio format :%x", __func__, format);
1235 }
1236
1237 return id;
1238}
1239
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001240void audio_extn_utils_send_audio_calibration(struct audio_device *adev,
1241 struct audio_usecase *usecase)
1242{
1243 int type = usecase->type;
1244
Dhananjay Kumar14245982017-01-16 20:21:00 +05301245 if (type == PCM_PLAYBACK && usecase->stream.out != NULL) {
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001246 struct stream_out *out = usecase->stream.out;
1247 int snd_device = usecase->out_snd_device;
1248 snd_device = (snd_device == SND_DEVICE_OUT_SPEAKER) ?
Xiaojun Sang040cc9f2015-08-03 19:38:28 +08001249 platform_get_spkr_prot_snd_device(snd_device) : snd_device;
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05301250 platform_send_audio_calibration(adev->platform, usecase,
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001251 out->app_type_cfg.app_type,
Preetam Singh Ranawat61716b12015-12-14 11:55:24 +05301252 usecase->stream.out->app_type_cfg.sample_rate);
Dhananjay Kumar14245982017-01-16 20:21:00 +05301253 } else if (type == PCM_CAPTURE && usecase->stream.in != NULL) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301254 platform_send_audio_calibration(adev->platform, usecase,
1255 usecase->stream.in->app_type_cfg.app_type,
1256 usecase->stream.in->app_type_cfg.sample_rate);
1257 } else {
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05301258 /* when app type is default. the sample rate is not used to send cal */
1259 platform_send_audio_calibration(adev->platform, usecase,
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301260 platform_get_default_app_type_v2(adev->platform, usecase->type),
1261 48000);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001262 }
1263}
1264
Ben Rombergera04fabc2014-11-14 12:16:03 -08001265// Base64 Encode and Decode
1266// Not all features supported. This must be used only with following conditions.
1267// Decode Modes: Support with and without padding
1268// CRLF not handling. So no CRLF in string to decode.
1269// Encode Modes: Supports only padding
1270int b64decode(char *inp, int ilen, uint8_t* outp)
1271{
1272 int i, j, k, ii, num;
1273 int rem, pcnt;
1274 uint32_t res=0;
1275 uint8_t getIndex[MAX_BASEINDEX_LEN];
1276 uint8_t tmp, cflag;
1277
1278 if(inp == NULL || outp == NULL || ilen <= 0) {
1279 ALOGE("[%s] received NULL pointer or zero length",__func__);
1280 return -1;
1281 }
1282
1283 memset(getIndex, MAX_BASEINDEX_LEN-1, sizeof(getIndex));
1284 for(i=0;i<BASE_TABLE_SIZE;i++) {
1285 getIndex[(uint8_t)bTable[i]] = (uint8_t)i;
1286 }
1287 getIndex[(uint8_t)'=']=0;
1288
1289 j=0;k=0;
1290 num = ilen/4;
1291 rem = ilen%4;
1292 if(rem==0)
1293 num = num-1;
1294 cflag=0;
1295 for(i=0; i<num; i++) {
1296 res=0;
1297 for(ii=0;ii<4;ii++) {
1298 res = res << 6;
1299 tmp = getIndex[(uint8_t)inp[j++]];
1300 res = res | tmp;
1301 cflag = cflag | tmp;
1302 }
1303 outp[k++] = (res >> 16)&0xFF;
1304 outp[k++] = (res >> 8)&0xFF;
1305 outp[k++] = res & 0xFF;
1306 }
1307
1308 // Handle last bytes special
1309 pcnt=0;
1310 if(rem == 0) {
1311 //With padding or full data
1312 res = 0;
1313 for(ii=0;ii<4;ii++) {
1314 if(inp[j] == '=')
1315 pcnt++;
1316 res = res << 6;
1317 tmp = getIndex[(uint8_t)inp[j++]];
1318 res = res | tmp;
1319 cflag = cflag | tmp;
1320 }
1321 outp[k++] = res >> 16;
1322 if(pcnt == 2)
1323 goto done;
1324 outp[k++] = (res>>8)&0xFF;
1325 if(pcnt == 1)
1326 goto done;
1327 outp[k++] = res&0xFF;
1328 } else {
1329 //without padding
1330 res = 0;
1331 for(i=0;i<rem;i++) {
1332 res = res << 6;
1333 tmp = getIndex[(uint8_t)inp[j++]];
1334 res = res | tmp;
1335 cflag = cflag | tmp;
1336 }
1337 for(i=rem;i<4;i++) {
1338 res = res << 6;
1339 pcnt++;
1340 }
1341 outp[k++] = res >> 16;
1342 if(pcnt == 2)
1343 goto done;
1344 outp[k++] = (res>>8)&0xFF;
1345 if(pcnt == 1)
1346 goto done;
1347 outp[k++] = res&0xFF;
1348 }
1349done:
1350 if(cflag == 0xFF) {
1351 ALOGE("[%s] base64 decode failed. Invalid character found %s",
1352 __func__, inp);
1353 return 0;
1354 }
1355 return k;
1356}
1357
1358int b64encode(uint8_t *inp, int ilen, char* outp)
1359{
1360 int i,j,k, num;
1361 int rem=0;
1362 uint32_t res=0;
1363
1364 if(inp == NULL || outp == NULL || ilen<=0) {
1365 ALOGE("[%s] received NULL pointer or zero input length",__func__);
1366 return -1;
1367 }
1368
1369 num = ilen/3;
1370 rem = ilen%3;
1371 j=0;k=0;
1372 for(i=0; i<num; i++) {
1373 //prepare index
1374 res = inp[j++]<<16;
1375 res = res | inp[j++]<<8;
1376 res = res | inp[j++];
1377 //get output map from index
1378 outp[k++] = (char) bTable[(res>>18)&0x3F];
1379 outp[k++] = (char) bTable[(res>>12)&0x3F];
1380 outp[k++] = (char) bTable[(res>>6)&0x3F];
1381 outp[k++] = (char) bTable[res&0x3F];
1382 }
1383
1384 switch(rem) {
1385 case 1:
1386 res = inp[j++]<<16;
1387 outp[k++] = (char) bTable[res>>18];
1388 outp[k++] = (char) bTable[(res>>12)&0x3F];
1389 //outp[k++] = '=';
1390 //outp[k++] = '=';
1391 break;
1392 case 2:
1393 res = inp[j++]<<16;
1394 res = res | inp[j++]<<8;
1395 outp[k++] = (char) bTable[res>>18];
1396 outp[k++] = (char) bTable[(res>>12)&0x3F];
1397 outp[k++] = (char) bTable[(res>>6)&0x3F];
1398 //outp[k++] = '=';
1399 break;
1400 default:
1401 break;
1402 }
Ben Rombergera04fabc2014-11-14 12:16:03 -08001403 outp[k] = '\0';
1404 return k;
1405}
Ashish Jain81eb2a82015-05-13 10:52:34 +05301406
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301407
1408int audio_extn_utils_get_codec_version(const char *snd_card_name,
1409 int card_num,
1410 char *codec_version)
1411{
1412 char procfs_path[50];
1413 FILE *fp;
1414
1415 if (strstr(snd_card_name, "tasha")) {
1416 snprintf(procfs_path, sizeof(procfs_path),
1417 "/proc/asound/card%d/codecs/tasha/version", card_num);
1418 if ((fp = fopen(procfs_path, "r")) != NULL) {
1419 fgets(codec_version, CODEC_VERSION_MAX_LENGTH, fp);
1420 fclose(fp);
1421 } else {
1422 ALOGE("%s: ERROR. cannot open %s", __func__, procfs_path);
1423 return -ENOENT;
1424 }
1425 ALOGD("%s: codec version %s", __func__, codec_version);
1426 }
1427
1428 return 0;
1429}
1430
1431
Ashish Jain81eb2a82015-05-13 10:52:34 +05301432#ifdef AUDIO_EXTERNAL_HDMI_ENABLED
1433
1434void get_default_compressed_channel_status(
1435 unsigned char *channel_status)
1436{
Ashish Jain81eb2a82015-05-13 10:52:34 +05301437 memset(channel_status,0,24);
1438
1439 /* block start bit in preamble bit 3 */
1440 channel_status[0] |= PROFESSIONAL;
1441 //compre out
1442 channel_status[0] |= NON_LPCM;
1443 // sample rate; fixed 48K for default/transcode
1444 channel_status[3] |= SR_48000;
1445}
1446
1447#ifdef HDMI_PASSTHROUGH_ENABLED
1448int32_t get_compressed_channel_status(void *audio_stream_data,
1449 uint32_t audio_frame_size,
1450 unsigned char *channel_status,
1451 enum audio_parser_code_type codec_type)
1452 // codec_type - AUDIO_PARSER_CODEC_AC3
1453 // - AUDIO_PARSER_CODEC_DTS
1454{
1455 unsigned char *stream;
1456 int ret = 0;
1457 stream = (unsigned char *)audio_stream_data;
1458
1459 if (audio_stream_data == NULL || audio_frame_size == 0) {
1460 ALOGW("no buffer to get channel status, return default for compress");
1461 get_default_compressed_channel_status(channel_status);
1462 return ret;
1463 }
1464
1465 memset(channel_status,0,24);
1466 if(init_audio_parser(stream, audio_frame_size, codec_type) == -1)
1467 {
1468 ALOGE("init audio parser failed");
1469 return -1;
1470 }
1471 ret = get_channel_status(channel_status, codec_type);
1472 return ret;
1473
1474}
1475
1476#endif
1477
1478void get_lpcm_channel_status(uint32_t sampleRate,
1479 unsigned char *channel_status)
1480{
1481 int32_t status = 0;
Ashish Jain81eb2a82015-05-13 10:52:34 +05301482 memset(channel_status,0,24);
1483 /* block start bit in preamble bit 3 */
1484 channel_status[0] |= PROFESSIONAL;
1485 //LPCM OUT
1486 channel_status[0] &= ~NON_LPCM;
1487
1488 switch (sampleRate) {
1489 case 8000:
1490 case 11025:
1491 case 12000:
1492 case 16000:
1493 case 22050:
1494 channel_status[3] |= SR_NOTID;
Preetam Singh Ranawat61716b12015-12-14 11:55:24 +05301495 break;
Ashish Jain81eb2a82015-05-13 10:52:34 +05301496 case 24000:
1497 channel_status[3] |= SR_24000;
1498 break;
1499 case 32000:
1500 channel_status[3] |= SR_32000;
1501 break;
1502 case 44100:
1503 channel_status[3] |= SR_44100;
1504 break;
1505 case 48000:
1506 channel_status[3] |= SR_48000;
1507 break;
1508 case 88200:
1509 channel_status[3] |= SR_88200;
1510 break;
1511 case 96000:
1512 channel_status[3] |= SR_96000;
1513 break;
1514 case 176400:
1515 channel_status[3] |= SR_176400;
1516 break;
1517 case 192000:
1518 channel_status[3] |= SR_192000;
1519 break;
1520 default:
1521 ALOGV("Invalid sample_rate %u\n", sampleRate);
1522 status = -1;
1523 break;
1524 }
1525}
1526
1527void audio_utils_set_hdmi_channel_status(struct stream_out *out, char * buffer, size_t bytes)
1528{
1529 unsigned char channel_status[24]={0};
1530 struct snd_aes_iec958 iec958;
1531 const char *mixer_ctl_name = "IEC958 Playback PCM Stream";
1532 struct mixer_ctl *ctl;
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +05301533 ALOGV("%s: buffer %s bytes %zd", __func__, buffer, bytes);
Ashish Jain81eb2a82015-05-13 10:52:34 +05301534#ifdef HDMI_PASSTHROUGH_ENABLED
1535 if (audio_extn_is_dolby_format(out->format) &&
1536 /*TODO:Extend code to support DTS passthrough*/
1537 /*set compressed channel status bits*/
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05301538 audio_extn_passthru_is_passthrough_stream(out)){
Ashish Jain81eb2a82015-05-13 10:52:34 +05301539 get_compressed_channel_status(buffer, bytes, channel_status, AUDIO_PARSER_CODEC_AC3);
1540 } else
1541#endif
1542 {
1543 /*set channel status bit for LPCM*/
1544 get_lpcm_channel_status(out->sample_rate, channel_status);
1545 }
1546
1547 memcpy(iec958.status, channel_status,sizeof(iec958.status));
1548 ctl = mixer_get_ctl_by_name(out->dev->mixer, mixer_ctl_name);
1549 if (!ctl) {
1550 ALOGE("%s: Could not get ctl for mixer cmd - %s",
1551 __func__, mixer_ctl_name);
1552 return;
1553 }
1554 if (mixer_ctl_set_array(ctl, &iec958, sizeof(iec958)) < 0) {
1555 ALOGE("%s: Could not set channel status for ext HDMI ",
1556 __func__);
1557 return;
1558 }
1559
1560}
1561#endif
Manish Dewangan3ccdea52017-02-13 19:31:54 +05301562
1563int audio_extn_utils_get_avt_device_drift(
1564 struct audio_usecase *usecase,
1565 struct audio_avt_device_drift_param *drift_param)
1566{
1567 int ret = 0, count = 0;
1568 char avt_device_drift_mixer_ctl_name[MIXER_PATH_MAX_LENGTH] = {0};
1569 struct mixer_ctl *ctl = NULL;
1570 struct audio_avt_device_drift_stats drift_stats;
1571 struct audio_device *adev = NULL;
1572
1573 if (usecase != NULL && usecase->type == PCM_PLAYBACK) {
1574 adev = usecase->stream.out->dev;
1575 switch(usecase->out_snd_device) {
1576 case SND_DEVICE_OUT_HDMI:
1577 strlcpy(avt_device_drift_mixer_ctl_name,
1578 "HDMI RX Drift",
1579 MIXER_PATH_MAX_LENGTH);
1580 break;
1581 case SND_DEVICE_OUT_DISPLAY_PORT:
1582 strlcpy(avt_device_drift_mixer_ctl_name,
1583 "DISPLAY Port RX Drift",
1584 MIXER_PATH_MAX_LENGTH);
1585 break;
1586 default :
1587 ALOGE("%s: Unsupported device %d",__func__,
1588 usecase->stream.out->devices);
1589 ret = -EINVAL;
1590 }
1591 } else {
1592 ALOGE("%s: Invalid usecase %d ",__func__, usecase->type);
1593 ret = -EINVAL;
1594 }
1595
1596 if(ret)
1597 goto done;
1598
1599 ctl = mixer_get_ctl_by_name(adev->mixer, avt_device_drift_mixer_ctl_name);
1600 if (!ctl) {
1601 ALOGE("%s: Could not get ctl for mixer cmd - %s",
1602 __func__, avt_device_drift_mixer_ctl_name);
1603
1604 ret = -EINVAL;
1605 goto done;
1606 }
1607
1608 ALOGV("%s: Getting AV Timer vs Device Drift mixer ctrl name %s", __func__,
1609 avt_device_drift_mixer_ctl_name);
1610
1611 mixer_ctl_update(ctl);
1612 count = mixer_ctl_get_num_values(ctl);
1613 if (count != sizeof(struct audio_avt_device_drift_stats)) {
1614 ALOGE("%s: mixer_ctl_get_num_values() invalid drift_stats data size",
1615 __func__);
1616
1617 ret = -EINVAL;
1618 goto done;
1619 }
1620
1621 ret = mixer_ctl_get_array(ctl, (void *)&drift_stats, count);
1622 if (ret != 0) {
1623 ALOGE("%s: mixer_ctl_get_array() failed to get drift_stats Params",
1624 __func__);
1625
1626 ret = -EINVAL;
1627 goto done;
1628 }
1629 memcpy(drift_param, &drift_stats.drift_param,
1630 sizeof(struct audio_avt_device_drift_param));
1631done:
1632 return ret;
1633}