blob: 174e43fa9663e5ad0d202370b63153078bf28f79 [file] [log] [blame]
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001/*
Sriram Kumar9c836482020-10-27 20:34:35 +05302 * Copyright (c) 2014-2021, 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
Manish Dewangan27346042017-03-01 12:56:12 +053023#include <inttypes.h>
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070024#include <errno.h>
25#include <cutils/properties.h>
26#include <cutils/config_utils.h>
27#include <stdlib.h>
28#include <dlfcn.h>
29#include <cutils/str_parms.h>
Aalique Grahame22e49102018-12-18 14:23:57 -080030#include <log/log.h>
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070031#include <cutils/misc.h>
Vinay Vermaaddfa4a2018-04-29 14:03:38 +053032#include <unistd.h>
Surendar Karka287348c2019-04-10 18:31:46 +053033#include <sys/ioctl.h>
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070034
Manish Dewangan07de2142017-02-27 19:27:20 +053035
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070036#include "audio_hw.h"
37#include "platform.h"
38#include "platform_api.h"
39#include "audio_extn.h"
Deeraj Soman14230922019-01-30 16:39:30 +053040#include "voice_extn.h"
Narsinga Rao Chella212e2542014-11-17 19:57:04 -080041#include "voice.h"
Manish Dewangan07de2142017-02-27 19:27:20 +053042#include <sound/compress_params.h>
43#include <sound/compress_offload.h>
Surendar Karka287348c2019-04-10 18:31:46 +053044#include <sound/devdep_params.h>
Manish Dewangan07de2142017-02-27 19:27:20 +053045#include <tinycompress/tinycompress.h>
Revathi Uddaraju1eac8b02017-05-18 17:13:33 +053046
47#ifdef DYNAMIC_LOG_ENABLED
48#include <log_xml_parser.h>
49#define LOG_MASK HAL_MOD_FILE_UTILS
50#include <log_utils.h>
51#endif
52
Ashish Jain81eb2a82015-05-13 10:52:34 +053053#ifdef AUDIO_EXTERNAL_HDMI_ENABLED
Ashish Jain81eb2a82015-05-13 10:52:34 +053054#include "audio_parsers.h"
55#endif
Ashish Jain81eb2a82015-05-13 10:52:34 +053056
Roopesh Nataraja0f68be72020-04-28 12:32:02 -070057#define AUDIO_IO_POLICY_VENDOR_CONFIG_FILE_NAME "audio_io_policy.conf"
58#define AUDIO_OUTPUT_POLICY_VENDOR_CONFIG_FILE_NAME "audio_output_policy.conf"
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070059
60#define OUTPUTS_TAG "outputs"
Dhananjay Kumard6d32152016-10-13 16:11:03 +053061#define INPUTS_TAG "inputs"
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070062
63#define DYNAMIC_VALUE_TAG "dynamic"
64#define FLAGS_TAG "flags"
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +053065#define PROFILES_TAG "profile"
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070066#define FORMATS_TAG "formats"
67#define SAMPLING_RATES_TAG "sampling_rates"
68#define BIT_WIDTH_TAG "bit_width"
69#define APP_TYPE_TAG "app_type"
70
71#define STRING_TO_ENUM(string) { #string, string }
72#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
73
Ben Rombergera04fabc2014-11-14 12:16:03 -080074#define BASE_TABLE_SIZE 64
75#define MAX_BASEINDEX_LEN 256
76
Ben Romberger1aaaf862017-04-06 17:49:46 -070077#ifndef SND_AUDIOCODEC_TRUEHD
78#define SND_AUDIOCODEC_TRUEHD 0x00000023
79#endif
80
Vikram Panduranga93f080e2017-06-07 18:16:14 -070081#define APP_TYPE_VOIP_AUDIO 0x1113A
82
Ashish Jain81eb2a82015-05-13 10:52:34 +053083#ifdef AUDIO_EXTERNAL_HDMI_ENABLED
84#define PROFESSIONAL (1<<0) /* 0 = consumer, 1 = professional */
85#define NON_LPCM (1<<1) /* 0 = audio, 1 = non-audio */
86#define SR_44100 (0<<0) /* 44.1kHz */
87#define SR_NOTID (1<<0) /* non indicated */
88#define SR_48000 (2<<0) /* 48kHz */
89#define SR_32000 (3<<0) /* 32kHz */
90#define SR_22050 (4<<0) /* 22.05kHz */
91#define SR_24000 (6<<0) /* 24kHz */
92#define SR_88200 (8<<0) /* 88.2kHz */
93#define SR_96000 (10<<0) /* 96kHz */
94#define SR_176400 (12<<0) /* 176.4kHz */
95#define SR_192000 (14<<0) /* 192kHz */
96
97#endif
Manish Dewangan07de2142017-02-27 19:27:20 +053098
99/* ToDo: Check and update a proper value in msec */
Weiyin Jiangc49a3b52018-08-17 16:16:08 +0800100#define COMPRESS_OFFLOAD_PLAYBACK_LATENCY 50
101#define PCM_OFFLOAD_PLAYBACK_DSP_PATHDELAY 62
Samyak Jain2546f8a2019-08-29 19:24:21 +0530102#define PCM_OFFLOAD_PLAYBACK_LATENCY PCM_OFFLOAD_PLAYBACK_DSP_PATHDELAY
Manish Dewangan07de2142017-02-27 19:27:20 +0530103
Varun Balaraje49253e2017-07-06 19:48:56 +0530104#ifndef MAX_CHANNELS_SUPPORTED
105#define MAX_CHANNELS_SUPPORTED 8
106#endif
107
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -0700108#ifdef __LP64__
109#define VNDK_FWK_LIB_PATH "/vendor/lib64/libqti_vndfwk_detect.so"
110#else
111#define VNDK_FWK_LIB_PATH "/vendor/lib/libqti_vndfwk_detect.so"
112#endif
113
Krishna Kishor Jhac1542b22022-08-22 13:12:09 +0530114#ifdef PLATFORM_AUTO
Kogara Naveen Kumar51bb57d2022-02-17 16:40:06 +0530115/* 24 KHz ECNR support */
116#define ECNS_USE_CASE_ACDB_DEV_ID 95
117#define ECNS_UNSUPPORTED_CAPTURE_SAMPLE_RATE_FOR_ADM 24000
118#define ECNS_SUPPORTED_CAPTURE_SAMPLE_RATE_FOR_ADM 48000
Krishna Kishor Jhac1542b22022-08-22 13:12:09 +0530119#endif
Kogara Naveen Kumar51bb57d2022-02-17 16:40:06 +0530120
Weiyin Jiangd5718bd2019-09-04 16:52:08 +0800121typedef struct vndkfwk_s {
122 void *lib_handle;
123 int (*isVendorEnhancedFwk)(void);
124 int (*getVendorEnhancedInfo)(void);
125 const char *lib_name;
126} vndkfwk_t;
127
128static vndkfwk_t mVndkFwk = {
129 NULL, NULL, NULL, VNDK_FWK_LIB_PATH};
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -0700130
`Deeraj Soman676c2702017-09-18 19:25:53 +0530131typedef struct {
132 const char *id_string;
133 const int value;
134} mixer_config_lookup;
135
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700136struct string_to_enum {
137 const char *name;
138 uint32_t value;
139};
140
141const struct string_to_enum s_flag_name_to_enum_table[] = {
142 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_DIRECT),
143 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_PRIMARY),
144 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_FAST),
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800145 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_RAW),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700146 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_DEEP_BUFFER),
147 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD),
148 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_NON_BLOCKING),
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700149 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_HW_AV_SYNC),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700150 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_INCALL_MUSIC),
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700151 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH),
Naresh Tanniruee3499a2017-01-05 14:05:35 +0530152 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_TIMESTAMP),
Vikram Panduranga93f080e2017-06-07 18:16:14 -0700153 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_VOIP_RX),
Ben Romberger6c4d3812017-06-13 17:46:45 -0700154 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_BD),
Varun Balaraje49253e2017-07-06 19:48:56 +0530155 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_INTERACTIVE),
Derek Chen090dfc52018-03-22 22:15:29 -0400156 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_MEDIA),
157 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_SYS_NOTIFICATION),
158 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_NAV_GUIDANCE),
159 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_PHONE),
Susan Wang117cf6f2022-04-06 20:11:46 -0400160 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_ALERTS),
Derek Chen1bcdc6b2020-02-06 22:44:53 -0800161 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_FRONT_PASSENGER),
Derek Chendf05eea2019-08-01 13:57:49 -0700162 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_REAR_SEAT),
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530163 STRING_TO_ENUM(AUDIO_INPUT_FLAG_NONE),
164 STRING_TO_ENUM(AUDIO_INPUT_FLAG_FAST),
165 STRING_TO_ENUM(AUDIO_INPUT_FLAG_HW_HOTWORD),
166 STRING_TO_ENUM(AUDIO_INPUT_FLAG_RAW),
167 STRING_TO_ENUM(AUDIO_INPUT_FLAG_SYNC),
Dhananjay Kumaree4d2002016-10-25 18:02:58 +0530168 STRING_TO_ENUM(AUDIO_INPUT_FLAG_TIMESTAMP),
Dhananjay Kumar704ce6f2017-09-28 22:08:00 +0530169 STRING_TO_ENUM(AUDIO_INPUT_FLAG_COMPRESS),
Ralf Herzaec80262018-07-03 07:08:49 +0200170 STRING_TO_ENUM(AUDIO_INPUT_FLAG_PASSTHROUGH),
George Gao3d477082020-09-17 22:29:07 -0700171 STRING_TO_ENUM(AUDIO_INPUT_FLAG_MMAP_NOIRQ),
172 STRING_TO_ENUM(AUDIO_INPUT_FLAG_VOIP_TX),
173 STRING_TO_ENUM(AUDIO_INPUT_FLAG_HW_AV_SYNC),
174 STRING_TO_ENUM(AUDIO_INPUT_FLAG_DIRECT),
Huicheng Liu1404ba12020-09-11 01:03:25 -0400175 STRING_TO_ENUM(AUDIO_INPUT_FLAG_PRIMARY),
176 STRING_TO_ENUM(AUDIO_INPUT_FLAG_FRONT_PASSENGER),
177 STRING_TO_ENUM(AUDIO_INPUT_FLAG_REAR_SEAT),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700178};
179
180const struct string_to_enum s_format_name_to_enum_table[] = {
Ashish Jain83a6cc22016-06-28 14:34:17 +0530181 STRING_TO_ENUM(AUDIO_FORMAT_PCM_8_BIT),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700182 STRING_TO_ENUM(AUDIO_FORMAT_PCM_16_BIT),
Ashish Jain5106d362016-05-11 19:23:33 +0530183 STRING_TO_ENUM(AUDIO_FORMAT_PCM_24_BIT_PACKED),
184 STRING_TO_ENUM(AUDIO_FORMAT_PCM_8_24_BIT),
Ashish Jainf1eaa582016-05-23 20:54:24 +0530185 STRING_TO_ENUM(AUDIO_FORMAT_PCM_32_BIT),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700186 STRING_TO_ENUM(AUDIO_FORMAT_MP3),
187 STRING_TO_ENUM(AUDIO_FORMAT_AAC),
188 STRING_TO_ENUM(AUDIO_FORMAT_VORBIS),
Mingming Yinae3530f2014-07-03 16:50:18 -0700189 STRING_TO_ENUM(AUDIO_FORMAT_AMR_NB),
190 STRING_TO_ENUM(AUDIO_FORMAT_AMR_WB),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700191 STRING_TO_ENUM(AUDIO_FORMAT_AC3),
Mingming Yinae3530f2014-07-03 16:50:18 -0700192 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700193 STRING_TO_ENUM(AUDIO_FORMAT_DTS),
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530194 STRING_TO_ENUM(AUDIO_FORMAT_DTS_HD),
Ben Romberger1aaaf862017-04-06 17:49:46 -0700195 STRING_TO_ENUM(AUDIO_FORMAT_DOLBY_TRUEHD),
Naresh Tanniru928f0862017-04-07 16:44:23 -0700196 STRING_TO_ENUM(AUDIO_FORMAT_IEC61937),
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530197 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3_JOC),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700198 STRING_TO_ENUM(AUDIO_FORMAT_WMA),
199 STRING_TO_ENUM(AUDIO_FORMAT_WMA_PRO),
200 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADIF),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700201 STRING_TO_ENUM(AUDIO_FORMAT_AMR_WB_PLUS),
202 STRING_TO_ENUM(AUDIO_FORMAT_EVRC),
203 STRING_TO_ENUM(AUDIO_FORMAT_EVRCB),
204 STRING_TO_ENUM(AUDIO_FORMAT_EVRCWB),
205 STRING_TO_ENUM(AUDIO_FORMAT_QCELP),
206 STRING_TO_ENUM(AUDIO_FORMAT_MP2),
207 STRING_TO_ENUM(AUDIO_FORMAT_EVRCNW),
Amit Shekhar6f461b12014-08-01 14:52:58 -0700208 STRING_TO_ENUM(AUDIO_FORMAT_FLAC),
Satya Krishna Pindiproli70471602015-04-24 19:12:43 +0530209 STRING_TO_ENUM(AUDIO_FORMAT_ALAC),
210 STRING_TO_ENUM(AUDIO_FORMAT_APE),
Alexy Josephcd8eaed2014-12-11 12:46:53 -0800211 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LC),
212 STRING_TO_ENUM(AUDIO_FORMAT_AAC_HE_V1),
213 STRING_TO_ENUM(AUDIO_FORMAT_AAC_HE_V2),
Manish Dewangana6fc5442015-08-24 20:30:31 +0530214 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS),
Ashish Jaine513a872015-11-19 17:00:56 +0530215 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_LC),
216 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_HE_V1),
217 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_HE_V2),
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +0530218 STRING_TO_ENUM(AUDIO_FORMAT_DSD),
Arun Kumar Dasari3b174182016-12-27 13:01:14 +0530219 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM),
220 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM_LC),
221 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM_HE_V1),
222 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM_HE_V2),
Dhanalakshmi Siddani18737932016-11-29 17:33:17 +0530223 STRING_TO_ENUM(AUDIO_FORMAT_APTX),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700224};
225
Manish Dewangan3ccdea52017-02-13 19:31:54 +0530226/* payload structure avt_device drift query */
227struct audio_avt_device_drift_stats {
228 uint32_t minor_version;
Manish Dewangan338c50a2017-09-12 15:22:03 +0530229
Manish Dewangan3ccdea52017-02-13 19:31:54 +0530230 /* Indicates the device interface direction as either
231 * source (Tx) or sink (Rx).
232 */
233 uint16_t device_direction;
Manish Dewangan338c50a2017-09-12 15:22:03 +0530234
235 /* Reference timer for drift accumulation and time stamp information.
236 * currently it only support AFE_REF_TIMER_TYPE_AVTIMER
237 */
238 uint16_t reference_timer;
Manish Dewangan3ccdea52017-02-13 19:31:54 +0530239 struct audio_avt_device_drift_param drift_param;
Manish Dewangan338c50a2017-09-12 15:22:03 +0530240} __attribute__((packed));
Manish Dewangan3ccdea52017-02-13 19:31:54 +0530241
Ben Rombergera04fabc2014-11-14 12:16:03 -0800242static char bTable[BASE_TABLE_SIZE] = {
243 'A','B','C','D','E','F','G','H','I','J','K','L',
244 'M','N','O','P','Q','R','S','T','U','V','W','X',
245 'Y','Z','a','b','c','d','e','f','g','h','i','j',
246 'k','l','m','n','o','p','q','r','s','t','u','v',
247 'w','x','y','z','0','1','2','3','4','5','6','7',
248 '8','9','+','/'
249};
250
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700251static uint32_t string_to_enum(const struct string_to_enum *table, size_t size,
252 const char *name)
253{
254 size_t i;
255 for (i = 0; i < size; i++) {
256 if (strcmp(table[i].name, name) == 0) {
257 ALOGV("%s found %s", __func__, table[i].name);
258 return table[i].value;
259 }
260 }
George Gao3d477082020-09-17 22:29:07 -0700261 ALOGE("%s cound not find %s", __func__, name);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700262 return 0;
263}
264
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530265static audio_io_flags_t parse_flag_names(char *name)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700266{
267 uint32_t flag = 0;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530268 audio_io_flags_t io_flags;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800269 char *last_r;
270 char *flag_name = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700271 while (flag_name != NULL) {
272 if (strlen(flag_name) != 0) {
273 flag |= string_to_enum(s_flag_name_to_enum_table,
274 ARRAY_SIZE(s_flag_name_to_enum_table),
275 flag_name);
276 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800277 flag_name = strtok_r(NULL, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700278 }
279
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800280 ALOGV("parse_flag_names: flag - %x", flag);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530281 io_flags.in_flags = (audio_input_flags_t)flag;
282 io_flags.out_flags = (audio_output_flags_t)flag;
283 return io_flags;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700284}
285
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530286static void parse_format_names(char *name, struct streams_io_cfg *s_info)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700287{
288 struct stream_format *sf_info = NULL;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800289 char *last_r;
290 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700291
292 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG) == 0)
293 return;
294
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530295 list_init(&s_info->format_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700296 while (str != NULL) {
297 audio_format_t format = (audio_format_t)string_to_enum(s_format_name_to_enum_table,
298 ARRAY_SIZE(s_format_name_to_enum_table), str);
299 ALOGV("%s: format - %d", __func__, format);
300 if (format != 0) {
301 sf_info = (struct stream_format *)calloc(1, sizeof(struct stream_format));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700302 if (sf_info == NULL)
303 break; /* return whatever was parsed */
304
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700305 sf_info->format = format;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530306 list_add_tail(&s_info->format_list, &sf_info->list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700307 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800308 str = strtok_r(NULL, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700309 }
310}
311
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530312static void parse_sample_rate_names(char *name, struct streams_io_cfg *s_info)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700313{
Amit Shekhar6f461b12014-08-01 14:52:58 -0700314 struct stream_sample_rate *ss_info = NULL;
315 uint32_t sample_rate = 48000;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800316 char *last_r;
317 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700318
Amit Shekhar6f461b12014-08-01 14:52:58 -0700319 if (str != NULL && 0 == strcmp(str, DYNAMIC_VALUE_TAG))
320 return;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700321
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530322 list_init(&s_info->sample_rate_list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700323 while (str != NULL) {
324 sample_rate = (uint32_t)strtol(str, (char **)NULL, 10);
325 ALOGV("%s: sample_rate - %d", __func__, sample_rate);
326 if (0 != sample_rate) {
327 ss_info = (struct stream_sample_rate *)calloc(1, sizeof(struct stream_sample_rate));
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800328 if (!ss_info) {
329 ALOGE("%s: memory allocation failure", __func__);
330 return;
331 }
Amit Shekhar6f461b12014-08-01 14:52:58 -0700332 ss_info->sample_rate = sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530333 list_add_tail(&s_info->sample_rate_list, &ss_info->list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700334 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800335 str = strtok_r(NULL, "|", &last_r);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700336 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700337}
338
339static int parse_bit_width_names(char *name)
340{
341 int bit_width = 16;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800342 char *last_r;
343 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700344
345 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG))
346 bit_width = (int)strtol(str, (char **)NULL, 10);
347
348 ALOGV("%s: bit_width - %d", __func__, bit_width);
349 return bit_width;
350}
351
352static int parse_app_type_names(void *platform, char *name)
353{
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700354 int app_type = platform_get_default_app_type(platform);
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800355 char *last_r;
356 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700357
358 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG))
359 app_type = (int)strtol(str, (char **)NULL, 10);
360
361 ALOGV("%s: app_type - %d", __func__, app_type);
362 return app_type;
363}
364
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530365static void update_streams_cfg_list(cnode *root, void *platform,
366 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700367{
368 cnode *node = root->first_child;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530369 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700370
371 ALOGV("%s", __func__);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530372 s_info = (struct streams_io_cfg *)calloc(1, sizeof(struct streams_io_cfg));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700373
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530374 if (!s_info) {
375 ALOGE("failed to allocate mem for s_info list element");
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700376 return;
377 }
378
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700379 while (node) {
380 if (strcmp(node->name, FLAGS_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530381 s_info->flags = parse_flag_names((char *)node->value);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530382 } else if (strcmp(node->name, PROFILES_TAG) == 0) {
383 strlcpy(s_info->profile, (char *)node->value, sizeof(s_info->profile));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700384 } else if (strcmp(node->name, FORMATS_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530385 parse_format_names((char *)node->value, s_info);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700386 } else if (strcmp(node->name, SAMPLING_RATES_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530387 s_info->app_type_cfg.sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
388 parse_sample_rate_names((char *)node->value, s_info);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700389 } else if (strcmp(node->name, BIT_WIDTH_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530390 s_info->app_type_cfg.bit_width = parse_bit_width_names((char *)node->value);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700391 } else if (strcmp(node->name, APP_TYPE_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530392 s_info->app_type_cfg.app_type = parse_app_type_names(platform, (char *)node->value);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700393 }
394 node = node->next;
395 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530396 list_add_tail(streams_cfg_list, &s_info->list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700397}
398
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530399static void load_cfg_list(cnode *root, void *platform,
400 struct listnode *streams_output_cfg_list,
401 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700402{
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530403 cnode *node = NULL;
404
405 node = config_find(root, OUTPUTS_TAG);
406 if (node != NULL) {
407 node = node->first_child;
408 while (node) {
409 ALOGV("%s: loading output %s", __func__, node->name);
410 update_streams_cfg_list(node, platform, streams_output_cfg_list);
411 node = node->next;
412 }
413 } else {
414 ALOGI("%s: could not load output, node is NULL", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700415 }
416
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530417 node = config_find(root, INPUTS_TAG);
418 if (node != NULL) {
419 node = node->first_child;
420 while (node) {
421 ALOGV("%s: loading input %s", __func__, node->name);
422 update_streams_cfg_list(node, platform, streams_input_cfg_list);
423 node = node->next;
424 }
425 } else {
426 ALOGI("%s: could not load input, node is NULL", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700427 }
428}
429
430static void send_app_type_cfg(void *platform, struct mixer *mixer,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530431 struct listnode *streams_output_cfg_list,
432 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700433{
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530434 size_t app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT] = {0};
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700435 int length = 0, i, num_app_types = 0;
436 struct listnode *node;
437 bool update;
438 struct mixer_ctl *ctl = NULL;
439 const char *mixer_ctl_name = "App Type Config";
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530440 struct streams_io_cfg *s_info = NULL;
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800441 uint32_t target_bit_width = 0;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700442
443 if (!mixer) {
444 ALOGE("%s: mixer is null",__func__);
445 return;
446 }
447 ctl = mixer_get_ctl_by_name(mixer, mixer_ctl_name);
448 if (!ctl) {
449 ALOGE("%s: Could not get ctl for mixer cmd - %s",__func__, mixer_ctl_name);
450 return;
451 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530452 app_type_cfg[length++] = num_app_types;
453
454 if (list_empty(streams_output_cfg_list)) {
455 app_type_cfg[length++] = platform_get_default_app_type_v2(platform, PCM_PLAYBACK);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700456 app_type_cfg[length++] = 48000;
457 app_type_cfg[length++] = 16;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530458 num_app_types += 1;
459 }
460 if (list_empty(streams_input_cfg_list)) {
461 app_type_cfg[length++] = platform_get_default_app_type_v2(platform, PCM_CAPTURE);
462 app_type_cfg[length++] = 48000;
463 app_type_cfg[length++] = 16;
464 num_app_types += 1;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700465 }
466
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800467 /* get target bit width for ADM enforce mode */
468 target_bit_width = adev_get_dsp_bit_width_enforce_mode();
469
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700470 list_for_each(node, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530471 s_info = node_to_item(node, struct streams_io_cfg, list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700472 update = true;
473 for (i=0; i<length; i=i+3) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530474 if (app_type_cfg[i+1] == 0)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700475 break;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530476 else if (app_type_cfg[i+1] == (size_t)s_info->app_type_cfg.app_type) {
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530477 if (app_type_cfg[i+2] < (size_t)s_info->app_type_cfg.sample_rate)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530478 app_type_cfg[i+2] = s_info->app_type_cfg.sample_rate;
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530479 if (app_type_cfg[i+3] < (size_t)s_info->app_type_cfg.bit_width)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530480 app_type_cfg[i+3] = s_info->app_type_cfg.bit_width;
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800481 /* ADM bit width = max(enforce_bit_width, bit_width from s_info */
482 if (audio_extn_is_dsp_bit_width_enforce_mode_supported(s_info->flags.out_flags) &&
483 (target_bit_width > app_type_cfg[i+3]))
484 app_type_cfg[i+3] = target_bit_width;
485
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700486 update = false;
487 break;
488 }
489 }
490 if (update && ((length + 3) <= MAX_LENGTH_MIXER_CONTROL_IN_INT)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530491 num_app_types += 1;
492 app_type_cfg[length++] = s_info->app_type_cfg.app_type;
493 app_type_cfg[length++] = s_info->app_type_cfg.sample_rate;
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800494 app_type_cfg[length] = s_info->app_type_cfg.bit_width;
495 if (audio_extn_is_dsp_bit_width_enforce_mode_supported(s_info->flags.out_flags) &&
496 (target_bit_width > app_type_cfg[length]))
497 app_type_cfg[length] = target_bit_width;
498
499 length++;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530500 }
501 }
502 list_for_each(node, streams_input_cfg_list) {
503 s_info = node_to_item(node, struct streams_io_cfg, list);
504 update = true;
505 for (i=0; i<length; i=i+3) {
506 if (app_type_cfg[i+1] == 0)
507 break;
508 else if (app_type_cfg[i+1] == (size_t)s_info->app_type_cfg.app_type) {
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530509 if (app_type_cfg[i+2] < (size_t)s_info->app_type_cfg.sample_rate)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530510 app_type_cfg[i+2] = s_info->app_type_cfg.sample_rate;
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530511 if (app_type_cfg[i+3] < (size_t)s_info->app_type_cfg.bit_width)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530512 app_type_cfg[i+3] = s_info->app_type_cfg.bit_width;
513 update = false;
514 break;
515 }
516 }
517 if (update && ((length + 3) <= MAX_LENGTH_MIXER_CONTROL_IN_INT)) {
518 num_app_types += 1;
519 app_type_cfg[length++] = s_info->app_type_cfg.app_type;
520 app_type_cfg[length++] = s_info->app_type_cfg.sample_rate;
521 app_type_cfg[length++] = s_info->app_type_cfg.bit_width;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700522 }
523 }
524 ALOGV("%s: num_app_types: %d", __func__, num_app_types);
525 if (num_app_types) {
526 app_type_cfg[0] = num_app_types;
527 mixer_ctl_set_array(ctl, app_type_cfg, length);
528 }
529}
530
Roopesh Nataraja0f68be72020-04-28 12:32:02 -0700531/* Function to retrieve audio vendor configs path */
532void audio_get_vendor_config_path (char* config_file_path, int path_size)
533{
534 char vendor_sku[PROPERTY_VALUE_MAX] = {'\0'};
535 if (property_get("ro.boot.product.vendor.sku", vendor_sku, "") <= 0) {
536#ifdef LINUX_ENABLED
537 /* Audio configs are stored in /etc */
538 snprintf(config_file_path, path_size, "%s", "/etc");
539#else
540 /* Audio configs are stored in /vendor/etc */
541 snprintf(config_file_path, path_size, "%s", "/vendor/etc");
542#endif
543 } else {
544 /* Audio configs are stored in /vendor/etc/audio/sku_${vendor_sku} */
545 snprintf(config_file_path, path_size,
546 "%s%s", "/vendor/etc/audio/sku_", vendor_sku);
547 }
548}
549
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530550void audio_extn_utils_update_streams_cfg_lists(void *platform,
551 struct mixer *mixer,
552 struct listnode *streams_output_cfg_list,
553 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700554{
555 cnode *root;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530556 char *data = NULL;
Roopesh Nataraja0f68be72020-04-28 12:32:02 -0700557 char vendor_config_path[VENDOR_CONFIG_PATH_MAX_LENGTH];
558 char audio_io_policy_file[VENDOR_CONFIG_FILE_MAX_LENGTH];
559 char audio_output_policy_file[VENDOR_CONFIG_FILE_MAX_LENGTH];
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700560
561 ALOGV("%s", __func__);
562 list_init(streams_output_cfg_list);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530563 list_init(streams_input_cfg_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700564
565 root = config_node("", "");
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700566 if (root == NULL) {
567 ALOGE("cfg_list, NULL config root");
568 return;
569 }
570
Roopesh Nataraja0f68be72020-04-28 12:32:02 -0700571 /* Get path for audio configuration files in vendor */
572 audio_get_vendor_config_path(vendor_config_path,
573 sizeof(vendor_config_path));
574
575 /* Get path for audio_io_policy_file in vendor */
576 snprintf(audio_io_policy_file, sizeof(audio_io_policy_file),
577 "%s/%s", vendor_config_path, AUDIO_IO_POLICY_VENDOR_CONFIG_FILE_NAME);
578
579 /* Load audio_io_policy_file from vendor */
580 data = (char *)load_file(audio_io_policy_file, NULL);
581
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530582 if (data == NULL) {
583 ALOGD("%s: failed to open io config file(%s), trying older config file",
Roopesh Nataraja0f68be72020-04-28 12:32:02 -0700584 __func__, audio_io_policy_file);
585
586 /* Get path for audio_output_policy_file in vendor */
587 snprintf(audio_output_policy_file, sizeof(audio_output_policy_file),
588 "%s/%s", vendor_config_path,
589 AUDIO_OUTPUT_POLICY_VENDOR_CONFIG_FILE_NAME);
590
591 /* Load audio_output_policy_file from vendor */
592 data = (char *)load_file(audio_output_policy_file, NULL);
593
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530594 if (data == NULL) {
595 send_app_type_cfg(platform, mixer,
596 streams_output_cfg_list,
597 streams_input_cfg_list);
598 ALOGE("%s: could not load io policy config!", __func__);
yidongh6eb4f752017-04-19 18:20:57 +0800599 free(root);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530600 return;
601 }
602 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700603
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530604 config_load(root, data);
605 load_cfg_list(root, platform, streams_output_cfg_list,
606 streams_input_cfg_list);
607
608 send_app_type_cfg(platform, mixer, streams_output_cfg_list,
609 streams_input_cfg_list);
Alexy Josephaee4fdd2016-01-29 13:02:07 -0800610
611 config_free(root);
yidongh6eb4f752017-04-19 18:20:57 +0800612 free(root);
Alexy Josephaee4fdd2016-01-29 13:02:07 -0800613 free(data);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700614}
615
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530616static void audio_extn_utils_dump_streams_cfg_list(
617 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700618{
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700619 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530620 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700621 struct stream_format *sf_info;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700622 struct stream_sample_rate *ss_info;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530623
624 list_for_each(node_i, streams_cfg_list) {
625 s_info = node_to_item(node_i, struct streams_io_cfg, list);
626 ALOGV("%s: flags-%d, sample_rate-%d, bit_width-%d, app_type-%d",
627 __func__, s_info->flags.out_flags, s_info->app_type_cfg.sample_rate,
628 s_info->app_type_cfg.bit_width, s_info->app_type_cfg.app_type);
629 list_for_each(node_j, &s_info->format_list) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700630 sf_info = node_to_item(node_j, struct stream_format, list);
631 ALOGV("format-%x", sf_info->format);
632 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530633 list_for_each(node_j, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700634 ss_info = node_to_item(node_j, struct stream_sample_rate, list);
635 ALOGV("sample rate-%d", ss_info->sample_rate);
636 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700637 }
638}
639
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530640void audio_extn_utils_dump_streams_cfg_lists(
641 struct listnode *streams_output_cfg_list,
642 struct listnode *streams_input_cfg_list)
643{
644 ALOGV("%s", __func__);
645 audio_extn_utils_dump_streams_cfg_list(streams_output_cfg_list);
646 audio_extn_utils_dump_streams_cfg_list(streams_input_cfg_list);
647}
648
649static void audio_extn_utils_release_streams_cfg_list(
650 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700651{
652 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530653 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700654
655 ALOGV("%s", __func__);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530656
657 while (!list_empty(streams_cfg_list)) {
658 node_i = list_head(streams_cfg_list);
659 s_info = node_to_item(node_i, struct streams_io_cfg, list);
660 while (!list_empty(&s_info->format_list)) {
661 node_j = list_head(&s_info->format_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700662 list_remove(node_j);
663 free(node_to_item(node_j, struct stream_format, list));
664 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530665 while (!list_empty(&s_info->sample_rate_list)) {
666 node_j = list_head(&s_info->sample_rate_list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700667 list_remove(node_j);
668 free(node_to_item(node_j, struct stream_sample_rate, list));
669 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700670 list_remove(node_i);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530671 free(node_to_item(node_i, struct streams_io_cfg, list));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700672 }
673}
674
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530675void audio_extn_utils_release_streams_cfg_lists(
676 struct listnode *streams_output_cfg_list,
677 struct listnode *streams_input_cfg_list)
678{
679 ALOGV("%s", __func__);
680 audio_extn_utils_release_streams_cfg_list(streams_output_cfg_list);
681 audio_extn_utils_release_streams_cfg_list(streams_input_cfg_list);
682}
683
684static bool set_app_type_cfg(struct streams_io_cfg *s_info,
685 struct stream_app_type_cfg *app_type_cfg,
686 uint32_t sample_rate, uint32_t bit_width)
Amit Shekhar6f461b12014-08-01 14:52:58 -0700687 {
688 struct listnode *node_i;
689 struct stream_sample_rate *ss_info;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530690 list_for_each(node_i, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700691 ss_info = node_to_item(node_i, struct stream_sample_rate, list);
692 if ((sample_rate <= ss_info->sample_rate) &&
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530693 (bit_width == s_info->app_type_cfg.bit_width)) {
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700694
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530695 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700696 app_type_cfg->sample_rate = ss_info->sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530697 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700698 ALOGV("%s app_type_cfg->app_type %d, app_type_cfg->sample_rate %d, app_type_cfg->bit_width %d",
699 __func__, app_type_cfg->app_type, app_type_cfg->sample_rate, app_type_cfg->bit_width);
700 return true;
701 }
702 }
703 /*
704 * Reiterate through the list assuming dafault sample rate.
705 * Handles scenario where input sample rate is higher
706 * than all sample rates in list for the input bit width.
707 */
708 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
Apoorv Raghuvanshif59bb222015-02-18 12:23:23 -0800709
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530710 list_for_each(node_i, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700711 ss_info = node_to_item(node_i, struct stream_sample_rate, list);
712 if ((sample_rate <= ss_info->sample_rate) &&
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530713 (bit_width == s_info->app_type_cfg.bit_width)) {
714 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700715 app_type_cfg->sample_rate = sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530716 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Apoorv Raghuvanshif59bb222015-02-18 12:23:23 -0800717 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 -0700718 __func__, app_type_cfg->app_type, app_type_cfg->sample_rate, app_type_cfg->bit_width);
719 return true;
720 }
721 }
722 return false;
723}
724
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530725void audio_extn_utils_update_stream_input_app_type_cfg(void *platform,
726 struct listnode *streams_input_cfg_list,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -0800727 struct listnode *devices __unused,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530728 audio_input_flags_t flags,
729 audio_format_t format,
730 uint32_t sample_rate,
731 uint32_t bit_width,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530732 char* profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530733 struct stream_app_type_cfg *app_type_cfg)
734{
735 struct listnode *node_i, *node_j;
736 struct streams_io_cfg *s_info;
737 struct stream_format *sf_info;
738
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530739 ALOGV("%s: flags: 0x%x, format: 0x%x sample_rate %d, profile %s",
740 __func__, flags, format, sample_rate, profile);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530741
742 list_for_each(node_i, streams_input_cfg_list) {
743 s_info = node_to_item(node_i, struct streams_io_cfg, list);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530744 /* Along with flags do profile matching if set at either end.*/
745 if (s_info->flags.in_flags == flags &&
746 ((profile[0] == '\0' && s_info->profile[0] == '\0') ||
747 strncmp(s_info->profile, profile, sizeof(s_info->profile)) == 0)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530748 list_for_each(node_j, &s_info->format_list) {
749 sf_info = node_to_item(node_j, struct stream_format, list);
750 if (sf_info->format == format) {
751 if (set_app_type_cfg(s_info, app_type_cfg, sample_rate, bit_width))
752 return;
753 }
754 }
755 }
756 }
757 ALOGW("%s: App type could not be selected. Falling back to default", __func__);
758 app_type_cfg->app_type = platform_get_default_app_type_v2(platform, PCM_CAPTURE);
759 app_type_cfg->sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
760 app_type_cfg->bit_width = 16;
Krishna Kishor Jhac1542b22022-08-22 13:12:09 +0530761#ifdef PLATFORM_AUTO
Kogara Naveen Kumar51bb57d2022-02-17 16:40:06 +0530762 if ((flags & AUDIO_INPUT_FLAG_TIMESTAMP) == 0 &&
763 (flags & AUDIO_INPUT_FLAG_COMPRESS) == 0 &&
764 (flags & AUDIO_INPUT_FLAG_FAST) != 0) {
765 // Support low latency record for different sample rates
766 app_type_cfg->sample_rate = sample_rate;
767 }
Krishna Kishor Jhac1542b22022-08-22 13:12:09 +0530768#endif
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530769}
770
771void audio_extn_utils_update_stream_output_app_type_cfg(void *platform,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700772 struct listnode *streams_output_cfg_list,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -0800773 struct listnode *devices,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700774 audio_output_flags_t flags,
775 audio_format_t format,
Amit Shekhar6f461b12014-08-01 14:52:58 -0700776 uint32_t sample_rate,
777 uint32_t bit_width,
Manish Dewangan837dc462015-05-27 10:17:41 +0530778 audio_channel_mask_t channel_mask,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530779 char *profile,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700780 struct stream_app_type_cfg *app_type_cfg)
781{
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +0530782 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530783 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700784 struct stream_format *sf_info;
Manish Dewangan837dc462015-05-27 10:17:41 +0530785 char value[PROPERTY_VALUE_MAX] = {0};
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700786
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -0800787 if (compare_device_type(devices, AUDIO_DEVICE_OUT_SPEAKER)) {
Ramu Gottipati074fa4d2018-09-11 20:05:51 +0530788 int bw = platform_get_snd_device_bit_width(SND_DEVICE_OUT_SPEAKER);
789 if ((-ENOSYS != bw) && (bit_width > (uint32_t)bw))
Amit Shekhar5a39c912014-10-14 15:39:30 -0700790 bit_width = (uint32_t)bw;
Ramu Gottipati074fa4d2018-09-11 20:05:51 +0530791 else if (-ENOSYS == bw)
792 bit_width = CODEC_BACKEND_DEFAULT_BIT_WIDTH;
Amit Shekhar1d896042014-10-03 13:16:09 -0700793 sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Aniket Kumar Latad13758f2020-08-06 15:11:36 -0700794 ALOGV("%s Allowing 24 and above bits playback on speaker \
795 ONLY at default sampling rate", __func__);
Amit Shekhar1d896042014-10-03 13:16:09 -0700796 }
797
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -0700798 property_get("vendor.audio.playback.mch.downsample",value,"");
Manish Dewangan837dc462015-05-27 10:17:41 +0530799 if (!strncmp("true", value, sizeof("true"))) {
800 if ((popcount(channel_mask) > 2) &&
801 (sample_rate > CODEC_BACKEND_DEFAULT_SAMPLE_RATE) &&
Aalique Grahame5cd12ff2017-10-19 10:57:00 -0700802 !(flags & (audio_output_flags_t)AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH)) {
Manish Dewangan837dc462015-05-27 10:17:41 +0530803 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
804 ALOGD("%s: MCH session defaulting sample rate to %d",
805 __func__, sample_rate);
806 }
807 }
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +0530808
809 /* Set sampling rate to 176.4 for DSD64
810 * and 352.8Khz for DSD128.
811 * Set Bit Width to 16. output will be 16 bit
812 * post DoP in ASM.
813 */
Aalique Grahame5cd12ff2017-10-19 10:57:00 -0700814 if ((flags & (audio_output_flags_t)AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH) &&
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +0530815 (format == AUDIO_FORMAT_DSD)) {
816 bit_width = 16;
817 if (sample_rate == INPUT_SAMPLING_RATE_DSD64)
818 sample_rate = OUTPUT_SAMPLING_RATE_DSD64;
819 else if (sample_rate == INPUT_SAMPLING_RATE_DSD128)
820 sample_rate = OUTPUT_SAMPLING_RATE_DSD128;
821 }
822
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +0530823
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800824 ALOGV("%s: flags: %x, format: %x sample_rate %d, profile %s, app_type %d",
825 __func__, flags, format, sample_rate, profile, app_type_cfg->app_type);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700826 list_for_each(node_i, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530827 s_info = node_to_item(node_i, struct streams_io_cfg, list);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530828 /* Along with flags do profile matching if set at either end.*/
829 if (s_info->flags.out_flags == flags &&
830 ((profile[0] == '\0' && s_info->profile[0] == '\0') ||
831 strncmp(s_info->profile, profile, sizeof(s_info->profile)) == 0)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530832 list_for_each(node_j, &s_info->format_list) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700833 sf_info = node_to_item(node_j, struct stream_format, list);
834 if (sf_info->format == format) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530835 if (set_app_type_cfg(s_info, app_type_cfg, sample_rate, bit_width))
Amit Shekhar6f461b12014-08-01 14:52:58 -0700836 return;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700837 }
838 }
839 }
840 }
841 list_for_each(node_i, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530842 s_info = node_to_item(node_i, struct streams_io_cfg, list);
843 if (s_info->flags.out_flags == AUDIO_OUTPUT_FLAG_PRIMARY) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700844 ALOGV("Compatible output profile not found.");
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530845 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
846 app_type_cfg->sample_rate = s_info->app_type_cfg.sample_rate;
847 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700848 ALOGV("%s Default to primary output: App type: %d sample_rate %d",
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530849 __func__, s_info->app_type_cfg.app_type, app_type_cfg->sample_rate);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700850 return;
851 }
852 }
853 ALOGW("%s: App type could not be selected. Falling back to default", __func__);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700854 app_type_cfg->app_type = platform_get_default_app_type(platform);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700855 app_type_cfg->sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700856 app_type_cfg->bit_width = 16;
Kogara Naveen Kumar51bb57d2022-02-17 16:40:06 +0530857 if (compare_device_type(devices, AUDIO_DEVICE_OUT_BUS) && (flags &
858 (audio_output_flags_t)AUDIO_OUTPUT_FLAG_FAST)) {
859 // Support low latency playback for different sample rates
860 app_type_cfg->sample_rate = sample_rate;
861 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700862}
863
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +0530864static bool audio_is_this_native_usecase(struct audio_usecase *uc)
865{
866 bool native_usecase = false;
867 struct stream_out *out = (struct stream_out*) uc->stream.out;
868
869 if (PCM_PLAYBACK == uc->type && out != NULL &&
870 NATIVE_AUDIO_MODE_INVALID != platform_get_native_support() &&
871 is_offload_usecase(uc->id) &&
872 (out->sample_rate == OUTPUT_SAMPLING_RATE_44100))
873 native_usecase = true;
874
875 return native_usecase;
876}
877
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800878bool audio_extn_is_dsp_bit_width_enforce_mode_supported(audio_output_flags_t flags)
879{
880 /* DSP bitwidth enforce mode for ADM and AFE:
881 * includes:
882 * deep buffer, low latency, direct pcm and offload.
883 * excludes:
884 * ull(raw+fast), VOIP.
885 */
886 if ((flags & AUDIO_OUTPUT_FLAG_VOIP_RX) ||
887 ((flags & AUDIO_OUTPUT_FLAG_RAW) &&
888 (flags & AUDIO_OUTPUT_FLAG_FAST)))
889 return false;
890
891
892 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) ||
893 (flags & AUDIO_OUTPUT_FLAG_DIRECT) ||
894 (flags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) ||
895 (flags & AUDIO_OUTPUT_FLAG_PRIMARY))
896 return true;
897 else
898 return false;
899}
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800900
901static inline bool audio_is_vr_mode_on(struct audio_device *(__attribute__((unused)) adev))
902{
903 return adev->vr_audio_mode_enabled;
904}
905
Derek Chenf7092792017-05-23 12:23:53 -0400906static void audio_extn_btsco_get_sample_rate(int snd_device, int *sample_rate)
907{
908 switch (snd_device) {
909 case SND_DEVICE_OUT_BT_SCO:
910 case SND_DEVICE_IN_BT_SCO_MIC:
911 case SND_DEVICE_IN_BT_SCO_MIC_NREC:
912 *sample_rate = 8000;
913 break;
914 case SND_DEVICE_OUT_BT_SCO_WB:
915 case SND_DEVICE_IN_BT_SCO_MIC_WB:
916 case SND_DEVICE_IN_BT_SCO_MIC_WB_NREC:
917 *sample_rate = 16000;
918 break;
919 default:
Aniket Kumar Latad13758f2020-08-06 15:11:36 -0700920 ALOGV("%s:Not a BT SCO device, need not update sampling rate\n", __func__);
Derek Chenf7092792017-05-23 12:23:53 -0400921 break;
922 }
923}
924
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530925void audio_extn_utils_update_stream_app_type_cfg_for_usecase(
926 struct audio_device *adev,
927 struct audio_usecase *usecase)
928{
929 ALOGV("%s", __func__);
930
931 switch(usecase->type) {
932 case PCM_PLAYBACK:
933 audio_extn_utils_update_stream_output_app_type_cfg(adev->platform,
934 &adev->streams_output_cfg_list,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -0800935 &usecase->stream.out->device_list,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530936 usecase->stream.out->flags,
Aalique Grahame65780b52017-09-27 14:59:56 -0700937 usecase->stream.out->hal_op_format,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530938 usecase->stream.out->sample_rate,
939 usecase->stream.out->bit_width,
940 usecase->stream.out->channel_mask,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530941 usecase->stream.out->profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530942 &usecase->stream.out->app_type_cfg);
943 ALOGV("%s Selected apptype: %d", __func__, usecase->stream.out->app_type_cfg.app_type);
944 break;
945 case PCM_CAPTURE:
Revathi Uddarajud9f23d92020-07-27 10:55:06 +0530946 if (usecase->id == USECASE_AUDIO_RECORD_VOIP
947 || usecase->id == USECASE_AUDIO_RECORD_VOIP_LOW_LATENCY)
Vikram Panduranga93f080e2017-06-07 18:16:14 -0700948 usecase->stream.in->app_type_cfg.app_type = APP_TYPE_VOIP_AUDIO;
949 else
950 audio_extn_utils_update_stream_input_app_type_cfg(adev->platform,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530951 &adev->streams_input_cfg_list,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -0800952 &usecase->stream.in->device_list,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530953 usecase->stream.in->flags,
954 usecase->stream.in->format,
955 usecase->stream.in->sample_rate,
956 usecase->stream.in->bit_width,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530957 usecase->stream.in->profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530958 &usecase->stream.in->app_type_cfg);
959 ALOGV("%s Selected apptype: %d", __func__, usecase->stream.in->app_type_cfg.app_type);
960 break;
Surendar Karka93cd25a2018-08-28 14:21:37 +0530961 case TRANSCODE_LOOPBACK_RX :
Siddartha Shaik343abc62017-08-08 11:15:25 +0530962 audio_extn_utils_update_stream_output_app_type_cfg(adev->platform,
963 &adev->streams_output_cfg_list,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -0800964 &usecase->stream.inout->out_config.device_list,
Siddartha Shaik343abc62017-08-08 11:15:25 +0530965 0,
966 usecase->stream.inout->out_config.format,
967 usecase->stream.inout->out_config.sample_rate,
968 usecase->stream.inout->out_config.bit_width,
969 usecase->stream.inout->out_config.channel_mask,
970 usecase->stream.inout->profile,
971 &usecase->stream.inout->out_app_type_cfg);
972 ALOGV("%s Selected apptype: %d", __func__, usecase->stream.inout->out_app_type_cfg.app_type);
973 break;
Derek Chenf7092792017-05-23 12:23:53 -0400974 case PCM_HFP_CALL:
975 switch (usecase->id) {
976 case USECASE_AUDIO_HFP_SCO:
977 case USECASE_AUDIO_HFP_SCO_WB:
978 audio_extn_btsco_get_sample_rate(usecase->out_snd_device,
979 &usecase->out_app_type_cfg.sample_rate);
Derek Chend7307012019-11-12 12:10:41 -0500980 usecase->in_app_type_cfg.sample_rate = usecase->out_app_type_cfg.sample_rate;
Derek Chenf7092792017-05-23 12:23:53 -0400981 break;
982 case USECASE_AUDIO_HFP_SCO_DOWNLINK:
983 case USECASE_AUDIO_HFP_SCO_WB_DOWNLINK:
984 audio_extn_btsco_get_sample_rate(usecase->in_snd_device,
985 &usecase->in_app_type_cfg.sample_rate);
Derek Chend7307012019-11-12 12:10:41 -0500986 usecase->out_app_type_cfg.sample_rate = usecase->in_app_type_cfg.sample_rate;
Derek Chenf7092792017-05-23 12:23:53 -0400987 break;
988 default:
989 ALOGE("%s: usecase id (%d) not supported, use default sample rate",
990 __func__, usecase->id);
991 usecase->in_app_type_cfg.sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
992 usecase->out_app_type_cfg.sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
993 break;
994 }
995 /* update out_app_type_cfg */
996 usecase->out_app_type_cfg.bit_width =
997 platform_get_snd_device_bit_width(usecase->out_snd_device);
998 usecase->out_app_type_cfg.app_type =
999 platform_get_default_app_type_v2(adev->platform, PCM_PLAYBACK);
1000 /* update in_app_type_cfg */
1001 usecase->in_app_type_cfg.bit_width =
1002 platform_get_snd_device_bit_width(usecase->in_snd_device);
1003 usecase->in_app_type_cfg.app_type =
1004 platform_get_default_app_type_v2(adev->platform, PCM_CAPTURE);
1005 ALOGV("%s Selected apptype: playback %d capture %d",
1006 __func__, usecase->out_app_type_cfg.app_type, usecase->in_app_type_cfg.app_type);
1007 break;
Derek Chena30a5f42019-12-03 11:17:09 -05001008 case ICC_CALL:
1009 /* ICC usecase: Loopback from TERT_TDM_TX to TERT_TDM_RX */
1010 /* update out_app_type_cfg */
1011 usecase->out_app_type_cfg.sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
1012 usecase->out_app_type_cfg.bit_width = platform_get_snd_device_bit_width(usecase->out_snd_device);
1013 usecase->out_app_type_cfg.app_type = platform_get_default_app_type_v2(adev->platform, PCM_PLAYBACK);
1014 /* update in_app_type_cfg */
1015 usecase->in_app_type_cfg.sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
1016 usecase->in_app_type_cfg.bit_width = platform_get_snd_device_bit_width(usecase->in_snd_device);
1017 usecase->in_app_type_cfg.app_type = platform_get_default_app_type_v2(adev->platform, PCM_CAPTURE);
1018
1019 ALOGV("%s Selected apptype: playback %d capture %d",
1020 __func__, usecase->out_app_type_cfg.app_type, usecase->in_app_type_cfg.app_type);
1021 break;
Fei Tongaffdf732020-02-20 20:39:05 +08001022 case SYNTH_LOOPBACK:
1023 /* update out_app_type_cfg */
1024 usecase->out_app_type_cfg.sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
1025 usecase->out_app_type_cfg.bit_width =
1026 platform_get_snd_device_bit_width(usecase->out_snd_device);
1027 usecase->out_app_type_cfg.app_type =
1028 platform_get_default_app_type_v2(adev->platform, PCM_PLAYBACK);
1029 /* update in_app_type_cfg */
1030 usecase->in_app_type_cfg.sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
1031 usecase->in_app_type_cfg.bit_width =
1032 platform_get_snd_device_bit_width(usecase->in_snd_device);
1033 usecase->in_app_type_cfg.app_type =
1034 platform_get_default_app_type_v2(adev->platform, PCM_CAPTURE);
1035 ALOGV("%s Selected apptype: playback %d capture %d",
1036 __func__, usecase->out_app_type_cfg.app_type, usecase->in_app_type_cfg.app_type);
1037 break;
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301038 default:
1039 ALOGE("%s: app type cfg not supported for usecase type (%d)",
1040 __func__, usecase->type);
1041 }
1042}
1043
Aalique Grahame22e49102018-12-18 14:23:57 -08001044static int set_stream_app_type_mixer_ctrl(struct audio_device *adev,
1045 int pcm_device_id, int app_type,
1046 int acdb_dev_id, int sample_rate,
1047 int stream_type,
1048 snd_device_t snd_device)
1049{
1050
1051 char mixer_ctl_name[MAX_LENGTH_MIXER_CONTROL_IN_INT];
1052 struct mixer_ctl *ctl;
1053 int app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT], len = 0, rc = 0;
1054 int snd_device_be_idx = -1;
1055
1056 if (stream_type == PCM_PLAYBACK) {
1057 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
1058 "Audio Stream %d App Type Cfg", pcm_device_id);
1059 } else if (stream_type == PCM_CAPTURE) {
1060 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
1061 "Audio Stream Capture %d App Type Cfg", pcm_device_id);
1062 }
1063
1064 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
1065 if (!ctl) {
1066 ALOGE("%s: Could not get ctl for mixer cmd - %s",
1067 __func__, mixer_ctl_name);
1068 rc = -EINVAL;
1069 goto exit;
1070 }
1071 app_type_cfg[len++] = app_type;
1072 app_type_cfg[len++] = acdb_dev_id;
1073 app_type_cfg[len++] = sample_rate;
1074
1075 snd_device_be_idx = platform_get_snd_device_backend_index(snd_device);
1076 if (snd_device_be_idx > 0)
1077 app_type_cfg[len++] = snd_device_be_idx;
1078 ALOGV("%s: stream type %d app_type %d, acdb_dev_id %d "
1079 "sample rate %d, snd_device_be_idx %d",
1080 __func__, stream_type, app_type, acdb_dev_id, sample_rate,
1081 snd_device_be_idx);
1082 mixer_ctl_set_array(ctl, app_type_cfg, len);
1083
1084exit:
1085 return rc;
1086}
1087
1088static int audio_extn_utils_send_app_type_cfg_hfp(struct audio_device *adev,
1089 struct audio_usecase *usecase)
1090{
1091 int pcm_device_id, acdb_dev_id = 0, snd_device = usecase->out_snd_device;
1092 int32_t sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
1093 int app_type = 0, rc = 0;
Derek Chenf7092792017-05-23 12:23:53 -04001094 bool is_bus_dev_usecase = false;
Aalique Grahame22e49102018-12-18 14:23:57 -08001095
1096 ALOGV("%s", __func__);
1097
1098 if (usecase->type != PCM_HFP_CALL) {
Derek Chenf7092792017-05-23 12:23:53 -04001099 ALOGV("%s: not a HFP path, no need to cfg app type", __func__);
Aalique Grahame22e49102018-12-18 14:23:57 -08001100 rc = 0;
1101 goto exit_send_app_type_cfg;
1102 }
1103 if ((usecase->id != USECASE_AUDIO_HFP_SCO) &&
Derek Chenf7092792017-05-23 12:23:53 -04001104 (usecase->id != USECASE_AUDIO_HFP_SCO_WB) &&
1105 (usecase->id != USECASE_AUDIO_HFP_SCO_DOWNLINK) &&
1106 (usecase->id != USECASE_AUDIO_HFP_SCO_WB_DOWNLINK)) {
1107 ALOGV("%s: a usecase where app type cfg is not required", __func__);
Aalique Grahame22e49102018-12-18 14:23:57 -08001108 rc = 0;
1109 goto exit_send_app_type_cfg;
1110 }
1111
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08001112 if (compare_device_type(&usecase->device_list, AUDIO_DEVICE_OUT_BUS))
Derek Chenf7092792017-05-23 12:23:53 -04001113 is_bus_dev_usecase = true;
1114
Aalique Grahame22e49102018-12-18 14:23:57 -08001115 snd_device = usecase->out_snd_device;
1116 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_PLAYBACK);
1117
1118 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
1119 if (acdb_dev_id < 0) {
1120 ALOGE("%s: Couldn't get the acdb dev id", __func__);
1121 rc = -EINVAL;
1122 goto exit_send_app_type_cfg;
1123 }
1124
sriram kumar53c4c972021-02-18 16:49:33 +05301125 /*
1126 * Value of afe_loopback gets read based on the property defined in
1127 * audio_platform_info.xml. If afe loopback is set then do not execute
1128 * session 1 path as app type mixer control will not be created for
1129 * afe loopback
1130 */
1131
Aalique Grahame22e49102018-12-18 14:23:57 -08001132 if (usecase->type == PCM_HFP_CALL) {
sriram kumar53c4c972021-02-18 16:49:33 +05301133 if (!(platform_get_is_afe_loopback_enabled(adev->platform))) {
1134 /* config HFP session:1 playback path */
1135 if (is_bus_dev_usecase) {
1136 app_type = usecase->out_app_type_cfg.app_type;
1137 sample_rate= usecase->out_app_type_cfg.sample_rate;
1138 } else {
1139 snd_device = SND_DEVICE_NONE; // use legacy behavior
1140 app_type = platform_get_default_app_type_v2(adev->platform, PCM_PLAYBACK);
1141 sample_rate= CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
Derek Chenf7092792017-05-23 12:23:53 -04001142 }
sriram kumar53c4c972021-02-18 16:49:33 +05301143 rc = set_stream_app_type_mixer_ctrl(adev, pcm_device_id, app_type,
1144 acdb_dev_id, sample_rate,
1145 PCM_PLAYBACK,
1146 snd_device);
1147 if (rc < 0)
1148 goto exit_send_app_type_cfg;
1149
1150 /* config HFP session:1 capture path */
1151 if (is_bus_dev_usecase) {
1152 snd_device = usecase->in_snd_device;
1153 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_CAPTURE);
1154 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
1155 if (acdb_dev_id < 0) {
1156 ALOGE("%s: Couldn't get the acdb dev id", __func__);
1157 rc = -EINVAL;
1158 goto exit_send_app_type_cfg;
1159 }
1160 app_type = usecase->in_app_type_cfg.app_type;
1161 sample_rate= usecase->in_app_type_cfg.sample_rate;
1162 } else {
1163 snd_device = SND_DEVICE_NONE; // use legacy behavior
1164 app_type = platform_get_default_app_type_v2(adev->platform, PCM_CAPTURE);
1165 }
1166 rc = set_stream_app_type_mixer_ctrl(adev, pcm_device_id, app_type,
1167 acdb_dev_id, sample_rate,
1168 PCM_CAPTURE,
1169 snd_device);
1170 if (rc < 0)
1171 goto exit_send_app_type_cfg;
1172
1173 if (is_bus_dev_usecase)
1174 goto exit_send_app_type_cfg;
Derek Chenf7092792017-05-23 12:23:53 -04001175 }
Aalique Grahame22e49102018-12-18 14:23:57 -08001176 /* config HFP session:2 capture path */
1177 pcm_device_id = HFP_ASM_RX_TX;
1178 snd_device = usecase->in_snd_device;
1179 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
1180 if (acdb_dev_id <= 0) {
1181 ALOGE("%s: Couldn't get the acdb dev id", __func__);
1182 rc = -EINVAL;
1183 goto exit_send_app_type_cfg;
1184 }
1185 app_type = platform_get_default_app_type_v2(adev->platform, PCM_CAPTURE);
1186 rc = set_stream_app_type_mixer_ctrl(adev, pcm_device_id, app_type,
1187 acdb_dev_id, sample_rate, PCM_CAPTURE,
sriram kumar53c4c972021-02-18 16:49:33 +05301188 usecase->in_snd_device);
Aalique Grahame22e49102018-12-18 14:23:57 -08001189 if (rc < 0)
1190 goto exit_send_app_type_cfg;
1191
1192 /* config HFP session:2 playback path */
1193 app_type = platform_get_default_app_type_v2(adev->platform, PCM_PLAYBACK);
1194 rc = set_stream_app_type_mixer_ctrl(adev, pcm_device_id, app_type,
1195 acdb_dev_id, sample_rate,
sriram kumar53c4c972021-02-18 16:49:33 +05301196 PCM_PLAYBACK, usecase->out_snd_device);
Aalique Grahame22e49102018-12-18 14:23:57 -08001197 if (rc < 0)
1198 goto exit_send_app_type_cfg;
1199 }
1200
1201 rc = 0;
1202exit_send_app_type_cfg:
1203 return rc;
1204}
1205
Derek Chena30a5f42019-12-03 11:17:09 -05001206int audio_extn_utils_send_app_type_cfg_icc(struct audio_device *adev,
1207 struct audio_usecase *usecase)
1208{
1209 int pcm_device_id, acdb_dev_id = 0, snd_device = usecase->out_snd_device;
1210 int32_t sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
1211 int app_type = 0, rc = 0;
1212
1213 ALOGV("%s", __func__);
1214
1215 if (usecase->type != ICC_CALL) {
1216 ALOGV("%s: not an ICC path, no need to cfg app type", __func__);
1217 rc = 0;
1218 goto exit_send_app_type_cfg;
1219 }
1220 if (usecase->id != USECASE_ICC_CALL) {
1221 ALOGV("%s: a usecase where app type cfg is not required", __func__);
1222 rc = 0;
1223 goto exit_send_app_type_cfg;
1224 }
1225
1226 snd_device = usecase->out_snd_device;
1227 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_PLAYBACK);
1228 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
1229 if (acdb_dev_id < 0) {
1230 ALOGE("%s: Couldn't get the acdb dev id", __func__);
1231 rc = -EINVAL;
1232 goto exit_send_app_type_cfg;
1233 }
1234 /* config ICC session: playback path */
1235 app_type = usecase->out_app_type_cfg.app_type;
1236 sample_rate= usecase->out_app_type_cfg.sample_rate;
1237
1238 rc = set_stream_app_type_mixer_ctrl(adev, pcm_device_id, app_type,
1239 acdb_dev_id, sample_rate,
1240 PCM_PLAYBACK,
1241 snd_device);
1242 if (rc < 0)
1243 goto exit_send_app_type_cfg;
1244
1245 /* config ICC session: capture path */
1246 snd_device = usecase->in_snd_device;
1247 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_CAPTURE);
1248 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
1249 if (acdb_dev_id < 0) {
1250 ALOGE("%s: Couldn't get the acdb dev id", __func__);
1251 rc = -EINVAL;
1252 goto exit_send_app_type_cfg;
1253 }
1254 app_type = usecase->in_app_type_cfg.app_type;
1255 sample_rate= usecase->in_app_type_cfg.sample_rate;
1256 rc = set_stream_app_type_mixer_ctrl(adev, pcm_device_id, app_type,
1257 acdb_dev_id, sample_rate,
1258 PCM_CAPTURE,
1259 snd_device);
1260exit_send_app_type_cfg:
1261 return rc;
1262}
1263
Fei Tongaffdf732020-02-20 20:39:05 +08001264static int audio_extn_utils_send_app_type_cfg_synth(struct audio_device *adev,
1265 struct audio_usecase *usecase)
1266{
1267 int pcm_device_id, acdb_dev_id = 0, snd_device = usecase->out_snd_device;
1268 int32_t sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
1269 int app_type = 0, rc = 0;
1270 bool is_bus_dev_usecase = false;
1271
1272 ALOGV("%s", __func__);
1273
1274 if (usecase->type != SYNTH_LOOPBACK) {
1275 ALOGV("%s: not a SYNTH path, no need to cfg app type", __func__);
1276 rc = 0;
1277 goto exit_send_app_type_cfg;
1278 }
1279 if (usecase->id != USECASE_AUDIO_PLAYBACK_SYNTHESIZER) {
1280 ALOGV("%s: a usecase where app type cfg is not required", __func__);
1281 rc = 0;
1282 goto exit_send_app_type_cfg;
1283 }
1284
1285 if (compare_device_type(&usecase->device_list, AUDIO_DEVICE_OUT_BUS)) {
1286 is_bus_dev_usecase = true;
1287 }
1288
1289 snd_device = usecase->out_snd_device;
1290 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_PLAYBACK);
1291
1292 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
1293 if (acdb_dev_id < 0) {
1294 ALOGE("%s: Couldn't get the acdb dev id", __func__);
1295 rc = -EINVAL;
1296 goto exit_send_app_type_cfg;
1297 }
1298
1299 if (usecase->type == SYNTH_LOOPBACK) {
1300 /* config SYNTH session: playback path */
1301 if (is_bus_dev_usecase) {
1302 app_type = usecase->out_app_type_cfg.app_type;
1303 sample_rate= usecase->out_app_type_cfg.sample_rate;
1304 } else {
1305 snd_device = SND_DEVICE_NONE; // use legacy behavior
1306 app_type = platform_get_default_app_type_v2(adev->platform, PCM_PLAYBACK);
1307 sample_rate= CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
1308 }
1309 rc = set_stream_app_type_mixer_ctrl(adev, pcm_device_id, app_type,
1310 acdb_dev_id, sample_rate,
1311 PCM_PLAYBACK,
1312 snd_device);
1313 if (rc < 0)
1314 goto exit_send_app_type_cfg;
1315 }
1316
1317 rc = 0;
1318exit_send_app_type_cfg:
1319 return rc;
1320}
1321
Zhou Song06761dd2019-04-28 18:08:17 +08001322int audio_extn_utils_get_app_sample_rate_for_device(
1323 struct audio_device *adev,
1324 struct audio_usecase *usecase, int snd_device)
1325{
1326 char value[PROPERTY_VALUE_MAX] = {0};
1327 int sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Krishna Kishor Jhac1542b22022-08-22 13:12:09 +05301328#ifdef PLATFORM_AUTO
Kogara Naveen Kumar51bb57d2022-02-17 16:40:06 +05301329 int acdb_dev_id;
Krishna Kishor Jhac1542b22022-08-22 13:12:09 +05301330#endif
Zhou Song06761dd2019-04-28 18:08:17 +08001331
1332 if ((usecase->type == PCM_PLAYBACK) && (usecase->stream.out != NULL)) {
1333 property_get("vendor.audio.playback.mch.downsample",value,"");
1334 if (!strncmp("true", value, sizeof("true"))) {
1335 if ((popcount(usecase->stream.out->channel_mask) > 2) &&
1336 (usecase->stream.out->app_type_cfg.sample_rate > CODEC_BACKEND_DEFAULT_SAMPLE_RATE) &&
1337 !(usecase->stream.out->flags &
1338 (audio_output_flags_t)AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH))
1339 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
1340 }
1341
1342 if (usecase->id == USECASE_AUDIO_PLAYBACK_VOIP) {
1343 usecase->stream.out->app_type_cfg.sample_rate = usecase->stream.out->sample_rate;
Zhou Song06761dd2019-04-28 18:08:17 +08001344 } else if ((snd_device == SND_DEVICE_OUT_HDMI ||
1345 snd_device == SND_DEVICE_OUT_USB_HEADSET ||
1346 snd_device == SND_DEVICE_OUT_DISPLAY_PORT) &&
1347 (usecase->stream.out->sample_rate >= OUTPUT_SAMPLING_RATE_44100)) {
1348 /*
1349 * To best utlize DSP, check if the stream sample rate is supported/multiple of
1350 * configured device sample rate, if not update the COPP rate to be equal to the
1351 * device sample rate, else open COPP at stream sample rate
1352 */
1353 platform_check_and_update_copp_sample_rate(adev->platform, snd_device,
1354 usecase->stream.out->sample_rate,
1355 &usecase->stream.out->app_type_cfg.sample_rate);
Lakshman Chaluvaraju5d0894e2021-01-27 23:22:58 +05301356 } else if (snd_device == SND_DEVICE_OUT_BT_A2DP) {
Zhou Song06761dd2019-04-28 18:08:17 +08001357 /*
1358 * For a2dp playback get encoder sampling rate and set copp sampling rate,
1359 * for bit width use the stream param only.
1360 */
1361 audio_extn_a2dp_get_enc_sample_rate(&usecase->stream.out->app_type_cfg.sample_rate);
1362 ALOGI("%s using %d sample rate rate for A2DP CoPP",
1363 __func__, usecase->stream.out->app_type_cfg.sample_rate);
sriram kumar68e21132021-06-25 19:27:20 +05301364 } else if ((((snd_device != SND_DEVICE_OUT_HEADPHONES_44_1 &&
1365 !audio_is_this_native_usecase(usecase)) &&
1366 usecase->stream.out->sample_rate == OUTPUT_SAMPLING_RATE_44100) ||
1367 (usecase->stream.out->sample_rate < OUTPUT_SAMPLING_RATE_44100)) ||
1368 (compare_device_type(&usecase->stream.out->device_list,AUDIO_DEVICE_OUT_SPEAKER))) {
Kogara Naveen Kumar51bb57d2022-02-17 16:40:06 +05301369 if (!((compare_device_type(&usecase->device_list, AUDIO_DEVICE_OUT_BUS)) && ((usecase->stream.out->flags &
1370 (audio_output_flags_t)AUDIO_OUTPUT_FLAG_SYS_NOTIFICATION) || (usecase->stream.out->flags &
1371 (audio_output_flags_t)AUDIO_OUTPUT_FLAG_PHONE)))) {
1372 /* Reset to default if no native stream is active or default device is speaker*/
1373 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
1374 }
Zhou Song06761dd2019-04-28 18:08:17 +08001375 }
1376 audio_extn_btsco_get_sample_rate(snd_device, &usecase->stream.out->app_type_cfg.sample_rate);
1377 sample_rate = usecase->stream.out->app_type_cfg.sample_rate;
1378
1379 if (((usecase->stream.out->format == AUDIO_FORMAT_E_AC3) ||
1380 (usecase->stream.out->format == AUDIO_FORMAT_E_AC3_JOC) ||
1381 (usecase->stream.out->format == AUDIO_FORMAT_DOLBY_TRUEHD))
1382 && audio_extn_passthru_is_passthrough_stream(usecase->stream.out)
1383 && !audio_extn_passthru_is_convert_supported(adev, usecase->stream.out)) {
1384 sample_rate = sample_rate * 4;
1385 if (sample_rate > HDMI_PASSTHROUGH_MAX_SAMPLE_RATE)
1386 sample_rate = HDMI_PASSTHROUGH_MAX_SAMPLE_RATE;
1387 }
1388 } else if (usecase->type == PCM_CAPTURE) {
1389 if (usecase->stream.in != NULL) {
Revathi Uddarajud9f23d92020-07-27 10:55:06 +05301390 if (usecase->id == USECASE_AUDIO_RECORD_VOIP
1391 || usecase->id == USECASE_AUDIO_RECORD_VOIP_LOW_LATENCY)
Zhou Song06761dd2019-04-28 18:08:17 +08001392 usecase->stream.in->app_type_cfg.sample_rate = usecase->stream.in->sample_rate;
1393 if (voice_is_in_call_rec_stream(usecase->stream.in)) {
1394 audio_extn_btsco_get_sample_rate(usecase->in_snd_device,
1395 &usecase->stream.in->app_type_cfg.sample_rate);
Zhou Songab94f062021-05-12 13:11:17 +08001396 } if (SND_DEVICE_IN_BT_A2DP == snd_device) {
1397 audio_extn_a2dp_get_dec_sample_rate(&usecase->stream.in->app_type_cfg.sample_rate);
Zhou Song06761dd2019-04-28 18:08:17 +08001398 } else {
1399 audio_extn_btsco_get_sample_rate(snd_device,
1400 &usecase->stream.in->app_type_cfg.sample_rate);
1401 }
1402 sample_rate = usecase->stream.in->app_type_cfg.sample_rate;
1403 } else if (usecase->id == USECASE_AUDIO_SPKR_CALIB_TX) {
Sujin Panickerb6d76262019-09-09 10:55:38 +05301404 if ((property_get("vendor.audio.spkr_prot.tx.sampling_rate", value, NULL) > 0))
1405 sample_rate = atoi(value);
1406 else
1407 sample_rate = SAMPLE_RATE_8000;
Zhou Song06761dd2019-04-28 18:08:17 +08001408 }
Kogara Naveen Kumar51bb57d2022-02-17 16:40:06 +05301409
1410 /* ECNR module in DSP does not support 24 KHz sample rate. As a workaround,
1411 run ADM at 48 KHz when ECNR is enabled in ACDB topology (e.g. device id = 95)
1412 */
Krishna Kishor Jhac1542b22022-08-22 13:12:09 +05301413#ifdef PLATFORM_AUTO
Kogara Naveen Kumar51bb57d2022-02-17 16:40:06 +05301414 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
1415 if (sample_rate == ECNS_UNSUPPORTED_CAPTURE_SAMPLE_RATE_FOR_ADM && acdb_dev_id == ECNS_USE_CASE_ACDB_DEV_ID) {
1416 sample_rate = ECNS_SUPPORTED_CAPTURE_SAMPLE_RATE_FOR_ADM;
1417 ALOGD("%s: update sample rate from 24K to 48K to support ECNR in PCM_CAPTURE, sample_rate=%d",__func__,sample_rate);
1418 }
Krishna Kishor Jhac1542b22022-08-22 13:12:09 +05301419#endif
Zhou Song06761dd2019-04-28 18:08:17 +08001420 } else if (usecase->type == TRANSCODE_LOOPBACK_RX) {
1421 sample_rate = usecase->stream.inout->out_config.sample_rate;
1422 }
1423 return sample_rate;
1424}
1425
Siena Richard7c2db772016-12-21 11:32:34 -08001426static int send_app_type_cfg_for_device(struct audio_device *adev,
1427 struct audio_usecase *usecase,
1428 int split_snd_device)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001429{
1430 char mixer_ctl_name[MAX_LENGTH_MIXER_CONTROL_IN_INT];
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301431 size_t app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT] = {0};
1432 int len = 0, rc;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001433 struct mixer_ctl *ctl;
Siena Richard7c2db772016-12-21 11:32:34 -08001434 int pcm_device_id = 0, acdb_dev_id, app_type;
1435 int snd_device = split_snd_device, snd_device_be_idx = -1;
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +05301436 int32_t sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Varun Balaraje49253e2017-07-06 19:48:56 +05301437 struct streams_io_cfg *s_info = NULL;
1438 struct listnode *node = NULL;
Nikhil Laturkarac4a7492017-08-31 18:27:19 +05301439 int bd_app_type = 0;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001440
Siena Richard7c2db772016-12-21 11:32:34 -08001441 ALOGV("%s: usecase->out_snd_device %s, usecase->in_snd_device %s, split_snd_device %s",
1442 __func__, platform_get_snd_device_name(usecase->out_snd_device),
1443 platform_get_snd_device_name(usecase->in_snd_device),
1444 platform_get_snd_device_name(split_snd_device));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001445
Siddartha Shaik343abc62017-08-08 11:15:25 +05301446 if (usecase->type != PCM_PLAYBACK && usecase->type != PCM_CAPTURE &&
Surendar Karka93cd25a2018-08-28 14:21:37 +05301447 usecase->type != TRANSCODE_LOOPBACK_RX) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301448 ALOGE("%s: not a playback/capture path, no need to cfg app type", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001449 rc = 0;
1450 goto exit_send_app_type_cfg;
1451 }
1452 if ((usecase->id != USECASE_AUDIO_PLAYBACK_DEEP_BUFFER) &&
Vignesh Kulothungan0a2eff02019-07-11 16:30:13 -07001453 (usecase->id != USECASE_AUDIO_PLAYBACK_WITH_HAPTICS) &&
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001454 (usecase->id != USECASE_AUDIO_PLAYBACK_LOW_LATENCY) &&
1455 (usecase->id != USECASE_AUDIO_PLAYBACK_MULTI_CH) &&
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -08001456 (usecase->id != USECASE_AUDIO_PLAYBACK_ULL) &&
Vikram Panduranga93f080e2017-06-07 18:16:14 -07001457 (usecase->id != USECASE_AUDIO_PLAYBACK_VOIP) &&
Surendar Karka93cd25a2018-08-28 14:21:37 +05301458 (usecase->id != USECASE_AUDIO_TRANSCODE_LOOPBACK_RX) &&
Varun Balaraje49253e2017-07-06 19:48:56 +05301459 (!is_interactive_usecase(usecase->id)) &&
Srikanth Uyyala9d551402015-08-25 16:03:42 +05301460 (!is_offload_usecase(usecase->id)) &&
Derek Chenf6318be2017-06-12 17:16:24 -04001461 (usecase->type != PCM_CAPTURE) &&
1462 (!audio_extn_auto_hal_is_bus_device_usecase(usecase->id))) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301463 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 -07001464 rc = 0;
1465 goto exit_send_app_type_cfg;
1466 }
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -08001467
1468 //if VR is active then only send the mixer control
1469 if (usecase->id == USECASE_AUDIO_PLAYBACK_ULL && !audio_is_vr_mode_on(adev)) {
1470 ALOGI("ULL doesnt need sending app type cfg, returning");
1471 rc = 0;
1472 goto exit_send_app_type_cfg;
1473 }
1474
Surendar Karka93cd25a2018-08-28 14:21:37 +05301475 if (usecase->type == PCM_PLAYBACK || usecase->type == TRANSCODE_LOOPBACK_RX) {
Ben Romberger1fafdde2015-09-09 19:43:15 -07001476 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_PLAYBACK);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301477 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
1478 "Audio Stream %d App Type Cfg", pcm_device_id);
Ben Romberger1fafdde2015-09-09 19:43:15 -07001479 } else if (usecase->type == PCM_CAPTURE) {
Ben Romberger1fafdde2015-09-09 19:43:15 -07001480 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_CAPTURE);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301481 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
1482 "Audio Stream Capture %d App Type Cfg", pcm_device_id);
Srikanth Uyyala9d551402015-08-25 16:03:42 +05301483 }
Siena Richard7c2db772016-12-21 11:32:34 -08001484
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001485 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
1486 if (!ctl) {
1487 ALOGE("%s: Could not get ctl for mixer cmd - %s", __func__,
1488 mixer_ctl_name);
1489 rc = -EINVAL;
1490 goto exit_send_app_type_cfg;
1491 }
Aditya Bavanari701a6992017-03-30 19:17:16 +05301492 snd_device = platform_get_spkr_prot_snd_device(snd_device);
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301493 if (voice_is_in_call_rec_stream(usecase->stream.in) && usecase->type == PCM_CAPTURE) {
1494 snd_device_t voice_device = voice_get_incall_rec_snd_device(usecase->in_snd_device);
1495 acdb_dev_id = platform_get_snd_device_acdb_id(voice_device);
1496 ALOGV("acdb id for voice call use case %d", acdb_dev_id);
1497 } else {
1498 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
1499 }
Rohit Kumar1181b292017-01-31 18:07:17 +05301500 if (acdb_dev_id <= 0) {
1501 ALOGE("%s: Couldn't get the acdb dev id", __func__);
1502 rc = -EINVAL;
1503 goto exit_send_app_type_cfg;
1504 }
1505
Siena Richard7c2db772016-12-21 11:32:34 -08001506 snd_device_be_idx = platform_get_snd_device_backend_index(snd_device);
1507 if (snd_device_be_idx < 0) {
1508 ALOGE("%s: Couldn't get the backend index for snd device %s ret=%d",
1509 __func__, platform_get_snd_device_name(snd_device),
1510 snd_device_be_idx);
1511 }
1512
Zhou Song06761dd2019-04-28 18:08:17 +08001513 sample_rate = audio_extn_utils_get_app_sample_rate_for_device(adev, usecase, snd_device);
1514
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301515 if ((usecase->type == PCM_PLAYBACK) && (usecase->stream.out != NULL)) {
Varun Balaraje49253e2017-07-06 19:48:56 +05301516 /* Interactive streams are supported with only direct app type id.
1517 * Get Direct profile app type and use it for interactive streams
1518 */
1519 list_for_each(node, &adev->streams_output_cfg_list) {
1520 s_info = node_to_item(node, struct streams_io_cfg, list);
Aalique Grahame5cd12ff2017-10-19 10:57:00 -07001521 if (s_info->flags.out_flags == (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_BD |
Nikhil Laturkarac4a7492017-08-31 18:27:19 +05301522 AUDIO_OUTPUT_FLAG_DIRECT_PCM |
1523 AUDIO_OUTPUT_FLAG_DIRECT))
1524 bd_app_type = s_info->app_type_cfg.app_type;
Varun Balaraje49253e2017-07-06 19:48:56 +05301525 }
Aalique Grahame5cd12ff2017-10-19 10:57:00 -07001526 if (usecase->stream.out->flags == (audio_output_flags_t)AUDIO_OUTPUT_FLAG_INTERACTIVE)
Nikhil Laturkarac4a7492017-08-31 18:27:19 +05301527 app_type = bd_app_type;
Varun Balaraje49253e2017-07-06 19:48:56 +05301528 else
1529 app_type = usecase->stream.out->app_type_cfg.app_type;
Siena Richard7c2db772016-12-21 11:32:34 -08001530 app_type_cfg[len++] = app_type;
Mingming Yin21854652016-04-13 11:54:02 -07001531 app_type_cfg[len++] = acdb_dev_id;
Naresh Tanniru3a406772017-05-10 13:09:05 -07001532 app_type_cfg[len++] = sample_rate;
1533
Siena Richard7c2db772016-12-21 11:32:34 -08001534 if (snd_device_be_idx > 0)
1535 app_type_cfg[len++] = snd_device_be_idx;
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301536
Siena Richard7c2db772016-12-21 11:32:34 -08001537 ALOGI("%s PLAYBACK app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
1538 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301539
1540 } else if ((usecase->type == PCM_CAPTURE) && (usecase->stream.in != NULL)) {
Siena Richard7c2db772016-12-21 11:32:34 -08001541 app_type = usecase->stream.in->app_type_cfg.app_type;
1542 app_type_cfg[len++] = app_type;
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301543 app_type_cfg[len++] = acdb_dev_id;
Siena Richard7c2db772016-12-21 11:32:34 -08001544 app_type_cfg[len++] = sample_rate;
1545 if (snd_device_be_idx > 0)
1546 app_type_cfg[len++] = snd_device_be_idx;
1547 ALOGI("%s CAPTURE app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
1548 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301549 } else {
Siena Richard7c2db772016-12-21 11:32:34 -08001550 app_type = platform_get_default_app_type_v2(adev->platform, usecase->type);
Surendar Karka93cd25a2018-08-28 14:21:37 +05301551 if(usecase->type == TRANSCODE_LOOPBACK_RX) {
Siddartha Shaik343abc62017-08-08 11:15:25 +05301552 app_type = usecase->stream.inout->out_app_type_cfg.app_type;
1553 }
Siena Richard7c2db772016-12-21 11:32:34 -08001554 app_type_cfg[len++] = app_type;
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301555 app_type_cfg[len++] = acdb_dev_id;
1556 app_type_cfg[len++] = sample_rate;
Siena Richard7c2db772016-12-21 11:32:34 -08001557 if (snd_device_be_idx > 0)
1558 app_type_cfg[len++] = snd_device_be_idx;
1559 ALOGI("%s default app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
1560 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Ashish Jainc02430d2016-01-04 10:42:43 +05301561 }
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301562
Siddartha Shaik343abc62017-08-08 11:15:25 +05301563 if(ctl)
1564 mixer_ctl_set_array(ctl, app_type_cfg, len);
Zhou Songd9bd9302020-08-04 16:34:45 +08001565
1566 /* send app type cfg for haptics */
1567 if (usecase->id == USECASE_AUDIO_PLAYBACK_WITH_HAPTICS) {
1568 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
1569 "Audio Stream %d App Type Cfg", adev->haptic_pcm_device_id );
1570 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
1571 if (!ctl) {
1572 ALOGE("%s: Could not get ctl for mixer cmd - %s", __func__,
1573 mixer_ctl_name);
1574 rc = -EINVAL;
1575 goto exit_send_app_type_cfg;
1576 }
1577 acdb_dev_id = platform_get_snd_device_acdb_id(SND_DEVICE_OUT_HAPTICS);
1578 snd_device_be_idx = platform_get_snd_device_backend_index(SND_DEVICE_OUT_HAPTICS);
1579 /* haptics acdb id */
1580 app_type_cfg[1] = acdb_dev_id;
1581 /* haptics be index */
1582 app_type_cfg[3] = snd_device_be_idx;
1583 mixer_ctl_set_array(ctl, app_type_cfg, len);
1584 }
1585
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001586 rc = 0;
1587exit_send_app_type_cfg:
1588 return rc;
1589}
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001590
Deeraj Soman14230922019-01-30 16:39:30 +05301591static int audio_extn_utils_check_input_parameters(uint32_t sample_rate,
1592 audio_format_t format,
1593 int channel_count)
1594{
1595 int ret = 0;
1596
1597 if (((format != AUDIO_FORMAT_PCM_16_BIT) && (format != AUDIO_FORMAT_PCM_8_24_BIT) &&
1598 (format != AUDIO_FORMAT_PCM_24_BIT_PACKED) && (format != AUDIO_FORMAT_PCM_32_BIT) &&
1599 (format != AUDIO_FORMAT_PCM_FLOAT)) &&
1600 !voice_extn_compress_voip_is_format_supported(format) &&
1601 !audio_extn_compr_cap_format_supported(format) &&
1602 !audio_extn_cin_format_supported(format))
1603 ret = -EINVAL;
1604
1605 switch (channel_count) {
1606 case 1:
1607 case 2:
1608 case 3:
1609 case 4:
1610 case 6:
1611 case 8:
1612 break;
1613 default:
1614 ret = -EINVAL;
1615 }
1616
1617 switch (sample_rate) {
1618 case 8000:
1619 case 11025:
1620 case 12000:
1621 case 16000:
1622 case 22050:
1623 case 24000:
1624 case 32000:
1625 case 44100:
1626 case 48000:
1627 case 88200:
1628 case 96000:
1629 case 176400:
1630 case 192000:
1631 break;
1632 default:
1633 ret = -EINVAL;
1634 }
1635
1636 return ret;
1637}
1638
1639static inline uint32_t audio_extn_utils_nearest_multiple(uint32_t num, uint32_t multiplier)
1640{
1641 uint32_t remainder = 0;
1642
1643 if (!multiplier)
1644 return num;
1645
1646 remainder = num % multiplier;
1647 if (remainder)
1648 num += (multiplier - remainder);
1649
1650 return num;
1651}
1652
1653static inline uint32_t audio_extn_utils_lcm(uint32_t num1, uint32_t num2)
1654{
1655 uint32_t high = num1, low = num2, temp = 0;
1656
1657 if (!num1 || !num2)
1658 return 0;
1659
1660 if (num1 < num2) {
1661 high = num2;
1662 low = num1;
1663 }
1664
1665 while (low != 0) {
1666 temp = low;
1667 low = high % low;
1668 high = temp;
1669 }
1670 return (num1 * num2)/high;
1671}
1672
Siena Richard7c2db772016-12-21 11:32:34 -08001673int audio_extn_utils_send_app_type_cfg(struct audio_device *adev,
1674 struct audio_usecase *usecase)
1675{
1676 int i, num_devices = 0;
1677 snd_device_t new_snd_devices[SND_DEVICE_OUT_END] = {0};
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301678 snd_device_t in_snd_device = usecase->in_snd_device;
Siena Richard7c2db772016-12-21 11:32:34 -08001679 int rc = 0;
1680
Aalique Grahame22e49102018-12-18 14:23:57 -08001681 if (usecase->type == PCM_HFP_CALL) {
1682 return audio_extn_utils_send_app_type_cfg_hfp(adev, usecase);
Derek Chena30a5f42019-12-03 11:17:09 -05001683 } else if (usecase->type == ICC_CALL) {
1684 return audio_extn_utils_send_app_type_cfg_icc(adev, usecase);
Fei Tongaffdf732020-02-20 20:39:05 +08001685 } else if (usecase->type == SYNTH_LOOPBACK) {
1686 return audio_extn_utils_send_app_type_cfg_synth(adev, usecase);
Aalique Grahame22e49102018-12-18 14:23:57 -08001687 }
1688
Siena Richard7c2db772016-12-21 11:32:34 -08001689 switch (usecase->type) {
1690 case PCM_PLAYBACK:
Surendar Karka93cd25a2018-08-28 14:21:37 +05301691 case TRANSCODE_LOOPBACK_RX:
Siena Richard7c2db772016-12-21 11:32:34 -08001692 ALOGD("%s: usecase->out_snd_device %s",
1693 __func__, platform_get_snd_device_name(usecase->out_snd_device));
1694 /* check for out combo device */
1695 if (platform_split_snd_device(adev->platform,
1696 usecase->out_snd_device,
1697 &num_devices, new_snd_devices)) {
1698 new_snd_devices[0] = usecase->out_snd_device;
1699 num_devices = 1;
1700 }
1701 break;
1702 case PCM_CAPTURE:
1703 ALOGD("%s: usecase->in_snd_device %s",
1704 __func__, platform_get_snd_device_name(usecase->in_snd_device));
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301705 if (voice_is_in_call_rec_stream(usecase->stream.in)) {
1706 in_snd_device = voice_get_incall_rec_backend_device(usecase->stream.in);
1707 }
Siena Richard7c2db772016-12-21 11:32:34 -08001708 /* check for in combo device */
1709 if (platform_split_snd_device(adev->platform,
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301710 in_snd_device,
Siena Richard7c2db772016-12-21 11:32:34 -08001711 &num_devices, new_snd_devices)) {
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301712 new_snd_devices[0] = in_snd_device;
Siena Richard7c2db772016-12-21 11:32:34 -08001713 num_devices = 1;
1714 }
1715 break;
Sriram Kumar9c836482020-10-27 20:34:35 +05301716 case PCM_HFP_CALL:
1717 rc = audio_extn_utils_send_app_type_cfg_hfp(adev,usecase);
1718 return rc;
Siena Richard7c2db772016-12-21 11:32:34 -08001719 default:
1720 ALOGI("%s: not a playback/capture path, no need to cfg app type", __func__);
1721 rc = 0;
1722 break;
1723 }
1724
1725 for (i = 0; i < num_devices; i++) {
1726 rc = send_app_type_cfg_for_device(adev, usecase, new_snd_devices[i]);
1727 if (rc)
1728 break;
1729 }
1730
1731 return rc;
1732}
1733
Preetam Singh Ranawat9519e9c2015-11-18 16:05:55 +05301734int read_line_from_file(const char *path, char *buf, size_t count)
1735{
1736 char * fgets_ret;
1737 FILE * fd;
1738 int rv;
1739
1740 fd = fopen(path, "r");
1741 if (fd == NULL)
1742 return -1;
1743
1744 fgets_ret = fgets(buf, (int)count, fd);
1745 if (NULL != fgets_ret) {
1746 rv = (int)strlen(buf);
1747 } else {
1748 rv = ferror(fd);
1749 }
1750 fclose(fd);
1751
1752 return rv;
1753}
1754
Ashish Jainf1eaa582016-05-23 20:54:24 +05301755/*Translates ALSA formats to AOSP PCM formats*/
1756audio_format_t alsa_format_to_hal(uint32_t alsa_format)
1757{
1758 audio_format_t format;
1759
1760 switch(alsa_format) {
1761 case SNDRV_PCM_FORMAT_S16_LE:
1762 format = AUDIO_FORMAT_PCM_16_BIT;
1763 break;
1764 case SNDRV_PCM_FORMAT_S24_3LE:
1765 format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
1766 break;
1767 case SNDRV_PCM_FORMAT_S24_LE:
1768 format = AUDIO_FORMAT_PCM_8_24_BIT;
1769 break;
1770 case SNDRV_PCM_FORMAT_S32_LE:
1771 format = AUDIO_FORMAT_PCM_32_BIT;
1772 break;
1773 default:
1774 ALOGW("Incorrect ALSA format");
1775 format = AUDIO_FORMAT_INVALID;
1776 }
1777 return format;
1778}
1779
1780/*Translates hal format (AOSP) to alsa formats*/
1781uint32_t hal_format_to_alsa(audio_format_t hal_format)
1782{
1783 uint32_t alsa_format;
1784
1785 switch (hal_format) {
1786 case AUDIO_FORMAT_PCM_32_BIT: {
1787 if (platform_supports_true_32bit())
1788 alsa_format = SNDRV_PCM_FORMAT_S32_LE;
1789 else
1790 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1791 }
1792 break;
1793 case AUDIO_FORMAT_PCM_8_BIT:
1794 alsa_format = SNDRV_PCM_FORMAT_S8;
1795 break;
1796 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
1797 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1798 break;
1799 case AUDIO_FORMAT_PCM_8_24_BIT: {
1800 if (platform_supports_true_32bit())
1801 alsa_format = SNDRV_PCM_FORMAT_S32_LE;
1802 else
1803 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1804 }
1805 break;
1806 case AUDIO_FORMAT_PCM_FLOAT:
1807 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1808 break;
1809 default:
1810 case AUDIO_FORMAT_PCM_16_BIT:
1811 alsa_format = SNDRV_PCM_FORMAT_S16_LE;
1812 break;
1813 }
1814 return alsa_format;
1815}
1816
Ashish Jain83a6cc22016-06-28 14:34:17 +05301817/*Translates PCM formats to AOSP formats*/
1818audio_format_t pcm_format_to_hal(uint32_t pcm_format)
1819{
1820 audio_format_t format = AUDIO_FORMAT_INVALID;
1821
1822 switch(pcm_format) {
1823 case PCM_FORMAT_S16_LE:
1824 format = AUDIO_FORMAT_PCM_16_BIT;
1825 break;
1826 case PCM_FORMAT_S24_3LE:
1827 format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
1828 break;
1829 case PCM_FORMAT_S24_LE:
1830 format = AUDIO_FORMAT_PCM_8_24_BIT;
1831 break;
1832 case PCM_FORMAT_S32_LE:
1833 format = AUDIO_FORMAT_PCM_32_BIT;
1834 break;
1835 default:
1836 ALOGW("Incorrect PCM format");
1837 format = AUDIO_FORMAT_INVALID;
1838 }
1839 return format;
1840}
1841
1842/*Translates hal format (AOSP) to alsa formats*/
1843uint32_t hal_format_to_pcm(audio_format_t hal_format)
1844{
1845 uint32_t pcm_format;
1846
1847 switch (hal_format) {
1848 case AUDIO_FORMAT_PCM_32_BIT:
1849 case AUDIO_FORMAT_PCM_8_24_BIT:
1850 case AUDIO_FORMAT_PCM_FLOAT: {
1851 if (platform_supports_true_32bit())
1852 pcm_format = PCM_FORMAT_S32_LE;
1853 else
1854 pcm_format = PCM_FORMAT_S24_3LE;
1855 }
1856 break;
1857 case AUDIO_FORMAT_PCM_8_BIT:
1858 pcm_format = PCM_FORMAT_S8;
1859 break;
1860 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
1861 pcm_format = PCM_FORMAT_S24_3LE;
1862 break;
1863 default:
1864 case AUDIO_FORMAT_PCM_16_BIT:
1865 pcm_format = PCM_FORMAT_S16_LE;
1866 break;
1867 }
1868 return pcm_format;
1869}
1870
Ashish Jainf1eaa582016-05-23 20:54:24 +05301871uint32_t get_alsa_fragment_size(uint32_t bytes_per_sample,
1872 uint32_t sample_rate,
Deeraj Soman65358ab2019-02-07 15:40:49 +05301873 uint32_t noOfChannels,
1874 int64_t duration_ms)
Ashish Jainf1eaa582016-05-23 20:54:24 +05301875{
1876 uint32_t fragment_size = 0;
1877 uint32_t pcm_offload_time = PCM_OFFLOAD_BUFFER_DURATION;
1878
Deeraj Soman65358ab2019-02-07 15:40:49 +05301879 if (duration_ms >= MIN_OFFLOAD_BUFFER_DURATION_MS && duration_ms <= MAX_OFFLOAD_BUFFER_DURATION_MS)
1880 pcm_offload_time = duration_ms;
1881
Ashish Jainf1eaa582016-05-23 20:54:24 +05301882 fragment_size = (pcm_offload_time
1883 * sample_rate
1884 * bytes_per_sample
1885 * noOfChannels)/1000;
1886 if (fragment_size < MIN_PCM_OFFLOAD_FRAGMENT_SIZE)
1887 fragment_size = MIN_PCM_OFFLOAD_FRAGMENT_SIZE;
1888 else if (fragment_size > MAX_PCM_OFFLOAD_FRAGMENT_SIZE)
1889 fragment_size = MAX_PCM_OFFLOAD_FRAGMENT_SIZE;
1890 /*To have same PCM samples for all channels, the buffer size requires to
1891 *be multiple of (number of channels * bytes per sample)
1892 *For writes to succeed, the buffer must be written at address which is multiple of 32
1893 */
Aalique Grahameb85f2d92017-10-16 17:53:23 -07001894 fragment_size = ALIGN(fragment_size, (bytes_per_sample * noOfChannels * 32));
Ashish Jainf1eaa582016-05-23 20:54:24 +05301895
1896 ALOGI("PCM offload Fragment size to %d bytes", fragment_size);
1897 return fragment_size;
1898}
1899
1900/* Calculates the fragment size required to configure compress session.
1901 * Based on the alsa format selected, decide if conversion is needed in
1902
1903 * HAL ( e.g. convert AUDIO_FORMAT_PCM_FLOAT input format to
1904 * AUDIO_FORMAT_PCM_24_BIT_PACKED before writing to the compress driver.
1905 */
1906void audio_extn_utils_update_direct_pcm_fragment_size(struct stream_out *out)
1907{
Ashish Jain83a6cc22016-06-28 14:34:17 +05301908 audio_format_t dst_format = out->hal_op_format;
1909 audio_format_t src_format = out->hal_ip_format;
Ashish Jainf1eaa582016-05-23 20:54:24 +05301910 uint32_t hal_op_bytes_per_sample = audio_bytes_per_sample(dst_format);
1911 uint32_t hal_ip_bytes_per_sample = audio_bytes_per_sample(src_format);
1912
1913 out->compr_config.fragment_size =
1914 get_alsa_fragment_size(hal_op_bytes_per_sample,
1915 out->sample_rate,
Deeraj Soman65358ab2019-02-07 15:40:49 +05301916 popcount(out->channel_mask),
1917 out->info.duration_us / 1000);
Ashish Jainf1eaa582016-05-23 20:54:24 +05301918
1919 if ((src_format != dst_format) &&
1920 hal_op_bytes_per_sample != hal_ip_bytes_per_sample) {
1921
Ashish Jain83a6cc22016-06-28 14:34:17 +05301922 out->hal_fragment_size =
Ashish Jainf1eaa582016-05-23 20:54:24 +05301923 ((out->compr_config.fragment_size * hal_ip_bytes_per_sample) /
1924 hal_op_bytes_per_sample);
1925 ALOGI("enable conversion hal_input_fragment_size is %d src_format %x dst_format %x",
Ashish Jain83a6cc22016-06-28 14:34:17 +05301926 out->hal_fragment_size, src_format, dst_format);
Ashish Jainf1eaa582016-05-23 20:54:24 +05301927 } else {
Ashish Jain83a6cc22016-06-28 14:34:17 +05301928 out->hal_fragment_size = out->compr_config.fragment_size;
Ashish Jainf1eaa582016-05-23 20:54:24 +05301929 }
1930}
1931
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301932/* converts pcm format 24_8 to 8_24 inplace */
1933size_t audio_extn_utils_convert_format_24_8_to_8_24(void *buf, size_t bytes)
1934{
1935 size_t i = 0;
1936 int *int_buf_stream = buf;
1937
1938 if ((bytes % 4) != 0) {
1939 ALOGE("%s: wrong inout buffer! ... is not 32 bit aligned ", __func__);
1940 return -EINVAL;
1941 }
1942
1943 for (; i < (bytes / 4); i++)
1944 int_buf_stream[i] >>= 8;
1945
1946 return bytes;
1947}
1948
Xiaojun Sang782e5b12020-06-29 21:13:06 +08001949#ifdef AUDIO_GKI_ENABLED
1950int get_snd_codec_id(audio_format_t format)
1951{
1952 int id = 0;
1953
1954 switch (format & AUDIO_FORMAT_MAIN_MASK) {
1955 case AUDIO_FORMAT_MP3:
1956 id = SND_AUDIOCODEC_MP3;
1957 break;
1958 case AUDIO_FORMAT_AAC:
1959 id = SND_AUDIOCODEC_AAC;
1960 break;
1961 case AUDIO_FORMAT_AAC_ADTS:
1962 id = SND_AUDIOCODEC_AAC;
1963 break;
1964 case AUDIO_FORMAT_AAC_LATM:
1965 id = SND_AUDIOCODEC_AAC;
1966 break;
1967 case AUDIO_FORMAT_PCM:
1968 id = SND_AUDIOCODEC_PCM;
1969 break;
1970 case AUDIO_FORMAT_FLAC:
1971 case AUDIO_FORMAT_ALAC:
1972 case AUDIO_FORMAT_APE:
1973 case AUDIO_FORMAT_VORBIS:
1974 case AUDIO_FORMAT_WMA:
1975 case AUDIO_FORMAT_WMA_PRO:
1976 case AUDIO_FORMAT_DSD:
1977 case AUDIO_FORMAT_APTX:
1978 id = SND_AUDIOCODEC_BESPOKE;
1979 break;
1980 case AUDIO_FORMAT_MP2:
1981 id = SND_AUDIOCODEC_MP2;
1982 break;
1983 case AUDIO_FORMAT_AC3:
1984 id = SND_AUDIOCODEC_AC3;
1985 break;
1986 case AUDIO_FORMAT_E_AC3:
1987 case AUDIO_FORMAT_E_AC3_JOC:
1988 id = SND_AUDIOCODEC_EAC3;
1989 break;
1990 case AUDIO_FORMAT_DTS:
1991 case AUDIO_FORMAT_DTS_HD:
1992 id = SND_AUDIOCODEC_DTS;
1993 break;
1994 case AUDIO_FORMAT_DOLBY_TRUEHD:
1995 id = SND_AUDIOCODEC_TRUEHD;
1996 break;
1997 case AUDIO_FORMAT_IEC61937:
1998 id = SND_AUDIOCODEC_IEC61937;
1999 break;
2000 default:
2001 ALOGE("%s: Unsupported audio format :%x", __func__, format);
2002 }
2003
2004 return id;
2005}
2006#else
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05302007int get_snd_codec_id(audio_format_t format)
2008{
2009 int id = 0;
2010
2011 switch (format & AUDIO_FORMAT_MAIN_MASK) {
2012 case AUDIO_FORMAT_MP3:
2013 id = SND_AUDIOCODEC_MP3;
2014 break;
2015 case AUDIO_FORMAT_AAC:
2016 id = SND_AUDIOCODEC_AAC;
2017 break;
2018 case AUDIO_FORMAT_AAC_ADTS:
2019 id = SND_AUDIOCODEC_AAC;
2020 break;
Arun Kumar Dasari3b174182016-12-27 13:01:14 +05302021 case AUDIO_FORMAT_AAC_LATM:
2022 id = SND_AUDIOCODEC_AAC;
2023 break;
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05302024 case AUDIO_FORMAT_PCM:
2025 id = SND_AUDIOCODEC_PCM;
2026 break;
2027 case AUDIO_FORMAT_FLAC:
2028 id = SND_AUDIOCODEC_FLAC;
2029 break;
2030 case AUDIO_FORMAT_ALAC:
2031 id = SND_AUDIOCODEC_ALAC;
2032 break;
2033 case AUDIO_FORMAT_APE:
2034 id = SND_AUDIOCODEC_APE;
2035 break;
2036 case AUDIO_FORMAT_VORBIS:
2037 id = SND_AUDIOCODEC_VORBIS;
2038 break;
2039 case AUDIO_FORMAT_WMA:
2040 id = SND_AUDIOCODEC_WMA;
2041 break;
Dechen Chai4a575252021-07-30 09:21:28 +05302042#ifndef AUDIO_DISABLE_COMPRESS_FORMAT
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05302043 case AUDIO_FORMAT_WMA_PRO:
2044 id = SND_AUDIOCODEC_WMA_PRO;
2045 break;
Dechen Chai4a575252021-07-30 09:21:28 +05302046#endif
Satish Babu Patakokila0c313922016-12-08 12:07:08 +05302047 case AUDIO_FORMAT_MP2:
2048 id = SND_AUDIOCODEC_MP2;
2049 break;
Satish Babu Patakokila915ecba2017-01-10 17:43:56 +05302050 case AUDIO_FORMAT_AC3:
2051 id = SND_AUDIOCODEC_AC3;
2052 break;
2053 case AUDIO_FORMAT_E_AC3:
2054 case AUDIO_FORMAT_E_AC3_JOC:
2055 id = SND_AUDIOCODEC_EAC3;
2056 break;
2057 case AUDIO_FORMAT_DTS:
2058 case AUDIO_FORMAT_DTS_HD:
2059 id = SND_AUDIOCODEC_DTS;
2060 break;
Ben Romberger1aaaf862017-04-06 17:49:46 -07002061 case AUDIO_FORMAT_DOLBY_TRUEHD:
2062 id = SND_AUDIOCODEC_TRUEHD;
2063 break;
Naresh Tanniru928f0862017-04-07 16:44:23 -07002064 case AUDIO_FORMAT_IEC61937:
2065 id = SND_AUDIOCODEC_IEC61937;
2066 break;
Dechen Chai4a575252021-07-30 09:21:28 +05302067#ifndef AUDIO_DISABLE_COMPRESS_FORMAT
Preetam Singh Ranawat4277a5a2017-01-18 19:02:24 +05302068 case AUDIO_FORMAT_DSD:
2069 id = SND_AUDIOCODEC_DSD;
2070 break;
Dhanalakshmi Siddani18737932016-11-29 17:33:17 +05302071 case AUDIO_FORMAT_APTX:
2072 id = SND_AUDIOCODEC_APTX;
2073 break;
Dechen Chai4a575252021-07-30 09:21:28 +05302074#endif
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05302075 default:
2076 ALOGE("%s: Unsupported audio format :%x", __func__, format);
2077 }
2078
2079 return id;
2080}
Xiaojun Sang782e5b12020-06-29 21:13:06 +08002081#endif
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05302082
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07002083void audio_extn_utils_send_audio_calibration(struct audio_device *adev,
2084 struct audio_usecase *usecase)
2085{
2086 int type = usecase->type;
2087
Dhananjay Kumar14245982017-01-16 20:21:00 +05302088 if (type == PCM_PLAYBACK && usecase->stream.out != NULL) {
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05302089 platform_send_audio_calibration(adev->platform, usecase,
Zhou Song06761dd2019-04-28 18:08:17 +08002090 usecase->stream.out->app_type_cfg.app_type);
Dhananjay Kumar14245982017-01-16 20:21:00 +05302091 } else if (type == PCM_CAPTURE && usecase->stream.in != NULL) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +05302092 platform_send_audio_calibration(adev->platform, usecase,
Zhou Song06761dd2019-04-28 18:08:17 +08002093 usecase->stream.in->app_type_cfg.app_type);
Sriram Kumar9c836482020-10-27 20:34:35 +05302094 } else if (type == PCM_HFP_CALL) {
2095 /* when app type is default. the sample rate is not used to send cal */
2096#ifdef ENABLE_HFP_CALIBRATION
2097 platform_send_audio_calibration_hfp(adev->platform, usecase->in_snd_device);
2098#else
2099 platform_send_audio_calibration(adev->platform, usecase,
2100 platform_get_default_app_type_v2(adev->platform, usecase->type));
2101#endif
2102 } else if ((type == PCM_CAPTURE) ||
Derek Chena30a5f42019-12-03 11:17:09 -05002103 (type == TRANSCODE_LOOPBACK_RX && usecase->stream.inout != NULL) ||
Fei Tongaffdf732020-02-20 20:39:05 +08002104 (type == ICC_CALL) || (type == SYNTH_LOOPBACK)) {
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05302105 platform_send_audio_calibration(adev->platform, usecase,
Zhou Song06761dd2019-04-28 18:08:17 +08002106 platform_get_default_app_type_v2(adev->platform, usecase->type));
Vidyakumar Athotae57f6002017-03-01 13:13:16 -08002107 } else {
2108 /* No need to send audio calibration for voice and voip call usecases */
2109 if ((type != VOICE_CALL) && (type != VOIP_CALL))
2110 ALOGW("%s: No audio calibration for usecase type = %d", __func__, type);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07002111 }
2112}
2113
Ben Rombergera04fabc2014-11-14 12:16:03 -08002114// Base64 Encode and Decode
2115// Not all features supported. This must be used only with following conditions.
2116// Decode Modes: Support with and without padding
2117// CRLF not handling. So no CRLF in string to decode.
2118// Encode Modes: Supports only padding
2119int b64decode(char *inp, int ilen, uint8_t* outp)
2120{
2121 int i, j, k, ii, num;
2122 int rem, pcnt;
2123 uint32_t res=0;
2124 uint8_t getIndex[MAX_BASEINDEX_LEN];
2125 uint8_t tmp, cflag;
2126
2127 if(inp == NULL || outp == NULL || ilen <= 0) {
2128 ALOGE("[%s] received NULL pointer or zero length",__func__);
2129 return -1;
2130 }
2131
2132 memset(getIndex, MAX_BASEINDEX_LEN-1, sizeof(getIndex));
2133 for(i=0;i<BASE_TABLE_SIZE;i++) {
2134 getIndex[(uint8_t)bTable[i]] = (uint8_t)i;
2135 }
2136 getIndex[(uint8_t)'=']=0;
2137
2138 j=0;k=0;
2139 num = ilen/4;
2140 rem = ilen%4;
2141 if(rem==0)
2142 num = num-1;
2143 cflag=0;
2144 for(i=0; i<num; i++) {
2145 res=0;
2146 for(ii=0;ii<4;ii++) {
2147 res = res << 6;
2148 tmp = getIndex[(uint8_t)inp[j++]];
2149 res = res | tmp;
2150 cflag = cflag | tmp;
2151 }
2152 outp[k++] = (res >> 16)&0xFF;
2153 outp[k++] = (res >> 8)&0xFF;
2154 outp[k++] = res & 0xFF;
2155 }
2156
2157 // Handle last bytes special
2158 pcnt=0;
2159 if(rem == 0) {
2160 //With padding or full data
2161 res = 0;
2162 for(ii=0;ii<4;ii++) {
2163 if(inp[j] == '=')
2164 pcnt++;
2165 res = res << 6;
2166 tmp = getIndex[(uint8_t)inp[j++]];
2167 res = res | tmp;
2168 cflag = cflag | tmp;
2169 }
2170 outp[k++] = res >> 16;
2171 if(pcnt == 2)
2172 goto done;
2173 outp[k++] = (res>>8)&0xFF;
2174 if(pcnt == 1)
2175 goto done;
2176 outp[k++] = res&0xFF;
2177 } else {
2178 //without padding
2179 res = 0;
2180 for(i=0;i<rem;i++) {
2181 res = res << 6;
2182 tmp = getIndex[(uint8_t)inp[j++]];
2183 res = res | tmp;
2184 cflag = cflag | tmp;
2185 }
2186 for(i=rem;i<4;i++) {
2187 res = res << 6;
2188 pcnt++;
2189 }
2190 outp[k++] = res >> 16;
2191 if(pcnt == 2)
2192 goto done;
2193 outp[k++] = (res>>8)&0xFF;
2194 if(pcnt == 1)
2195 goto done;
2196 outp[k++] = res&0xFF;
2197 }
2198done:
2199 if(cflag == 0xFF) {
2200 ALOGE("[%s] base64 decode failed. Invalid character found %s",
2201 __func__, inp);
2202 return 0;
2203 }
2204 return k;
2205}
2206
2207int b64encode(uint8_t *inp, int ilen, char* outp)
2208{
2209 int i,j,k, num;
2210 int rem=0;
2211 uint32_t res=0;
2212
2213 if(inp == NULL || outp == NULL || ilen<=0) {
2214 ALOGE("[%s] received NULL pointer or zero input length",__func__);
2215 return -1;
2216 }
2217
2218 num = ilen/3;
2219 rem = ilen%3;
2220 j=0;k=0;
2221 for(i=0; i<num; i++) {
2222 //prepare index
2223 res = inp[j++]<<16;
2224 res = res | inp[j++]<<8;
2225 res = res | inp[j++];
2226 //get output map from index
2227 outp[k++] = (char) bTable[(res>>18)&0x3F];
2228 outp[k++] = (char) bTable[(res>>12)&0x3F];
2229 outp[k++] = (char) bTable[(res>>6)&0x3F];
2230 outp[k++] = (char) bTable[res&0x3F];
2231 }
2232
2233 switch(rem) {
2234 case 1:
2235 res = inp[j++]<<16;
2236 outp[k++] = (char) bTable[res>>18];
2237 outp[k++] = (char) bTable[(res>>12)&0x3F];
2238 //outp[k++] = '=';
2239 //outp[k++] = '=';
2240 break;
2241 case 2:
2242 res = inp[j++]<<16;
2243 res = res | inp[j++]<<8;
2244 outp[k++] = (char) bTable[res>>18];
2245 outp[k++] = (char) bTable[(res>>12)&0x3F];
2246 outp[k++] = (char) bTable[(res>>6)&0x3F];
2247 //outp[k++] = '=';
2248 break;
2249 default:
2250 break;
2251 }
Ben Rombergera04fabc2014-11-14 12:16:03 -08002252 outp[k] = '\0';
2253 return k;
2254}
Ashish Jain81eb2a82015-05-13 10:52:34 +05302255
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05302256
2257int audio_extn_utils_get_codec_version(const char *snd_card_name,
2258 int card_num,
2259 char *codec_version)
2260{
2261 char procfs_path[50];
2262 FILE *fp;
2263
2264 if (strstr(snd_card_name, "tasha")) {
2265 snprintf(procfs_path, sizeof(procfs_path),
2266 "/proc/asound/card%d/codecs/tasha/version", card_num);
2267 if ((fp = fopen(procfs_path, "r")) != NULL) {
2268 fgets(codec_version, CODEC_VERSION_MAX_LENGTH, fp);
2269 fclose(fp);
2270 } else {
2271 ALOGE("%s: ERROR. cannot open %s", __func__, procfs_path);
2272 return -ENOENT;
2273 }
2274 ALOGD("%s: codec version %s", __func__, codec_version);
2275 }
2276
2277 return 0;
2278}
2279
Preetam Singh Ranawat9d0d8e42019-07-15 12:27:25 +05302280int audio_extn_utils_get_codec_variant(int card_num,
2281 char *codec_variant)
2282{
2283 char procfs_path[50];
2284 FILE *fp;
2285 snprintf(procfs_path, sizeof(procfs_path),
2286 "/proc/asound/card%d/codecs/wcd938x/variant", card_num);
2287 if ((fp = fopen(procfs_path, "r")) == NULL) {
2288 snprintf(procfs_path, sizeof(procfs_path),
2289 "/proc/asound/card%d/codecs/wcd937x/variant", card_num);
2290 if ((fp = fopen(procfs_path, "r")) == NULL) {
2291 ALOGE("%s: ERROR. cannot open %s", __func__, procfs_path);
2292 return -ENOENT;
2293 }
2294 }
2295 fgets(codec_variant, CODEC_VARIANT_MAX_LENGTH, fp);
2296 fclose(fp);
2297 ALOGD("%s: codec variant is %s", __func__, codec_variant);
2298 return 0;
2299}
2300
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05302301
Ashish Jain81eb2a82015-05-13 10:52:34 +05302302#ifdef AUDIO_EXTERNAL_HDMI_ENABLED
2303
2304void get_default_compressed_channel_status(
2305 unsigned char *channel_status)
2306{
Ashish Jain81eb2a82015-05-13 10:52:34 +05302307 memset(channel_status,0,24);
2308
2309 /* block start bit in preamble bit 3 */
2310 channel_status[0] |= PROFESSIONAL;
2311 //compre out
2312 channel_status[0] |= NON_LPCM;
2313 // sample rate; fixed 48K for default/transcode
2314 channel_status[3] |= SR_48000;
2315}
2316
Ashish Jain81eb2a82015-05-13 10:52:34 +05302317int32_t get_compressed_channel_status(void *audio_stream_data,
2318 uint32_t audio_frame_size,
2319 unsigned char *channel_status,
2320 enum audio_parser_code_type codec_type)
2321 // codec_type - AUDIO_PARSER_CODEC_AC3
2322 // - AUDIO_PARSER_CODEC_DTS
2323{
2324 unsigned char *stream;
2325 int ret = 0;
2326 stream = (unsigned char *)audio_stream_data;
2327
2328 if (audio_stream_data == NULL || audio_frame_size == 0) {
2329 ALOGW("no buffer to get channel status, return default for compress");
2330 get_default_compressed_channel_status(channel_status);
2331 return ret;
2332 }
2333
2334 memset(channel_status,0,24);
2335 if(init_audio_parser(stream, audio_frame_size, codec_type) == -1)
2336 {
2337 ALOGE("init audio parser failed");
2338 return -1;
2339 }
2340 ret = get_channel_status(channel_status, codec_type);
2341 return ret;
2342
2343}
2344
Ashish Jain81eb2a82015-05-13 10:52:34 +05302345void get_lpcm_channel_status(uint32_t sampleRate,
2346 unsigned char *channel_status)
2347{
2348 int32_t status = 0;
Ashish Jain81eb2a82015-05-13 10:52:34 +05302349 memset(channel_status,0,24);
2350 /* block start bit in preamble bit 3 */
2351 channel_status[0] |= PROFESSIONAL;
2352 //LPCM OUT
2353 channel_status[0] &= ~NON_LPCM;
2354
2355 switch (sampleRate) {
2356 case 8000:
2357 case 11025:
2358 case 12000:
2359 case 16000:
2360 case 22050:
2361 channel_status[3] |= SR_NOTID;
Preetam Singh Ranawat61716b12015-12-14 11:55:24 +05302362 break;
Ashish Jain81eb2a82015-05-13 10:52:34 +05302363 case 24000:
2364 channel_status[3] |= SR_24000;
2365 break;
2366 case 32000:
2367 channel_status[3] |= SR_32000;
2368 break;
2369 case 44100:
2370 channel_status[3] |= SR_44100;
2371 break;
2372 case 48000:
2373 channel_status[3] |= SR_48000;
2374 break;
2375 case 88200:
2376 channel_status[3] |= SR_88200;
2377 break;
2378 case 96000:
2379 channel_status[3] |= SR_96000;
2380 break;
2381 case 176400:
2382 channel_status[3] |= SR_176400;
2383 break;
2384 case 192000:
2385 channel_status[3] |= SR_192000;
2386 break;
2387 default:
2388 ALOGV("Invalid sample_rate %u\n", sampleRate);
2389 status = -1;
2390 break;
2391 }
2392}
2393
2394void audio_utils_set_hdmi_channel_status(struct stream_out *out, char * buffer, size_t bytes)
2395{
2396 unsigned char channel_status[24]={0};
2397 struct snd_aes_iec958 iec958;
2398 const char *mixer_ctl_name = "IEC958 Playback PCM Stream";
2399 struct mixer_ctl *ctl;
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +05302400 ALOGV("%s: buffer %s bytes %zd", __func__, buffer, bytes);
Arun Mirpurie008ed22019-03-21 11:21:04 -07002401
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05302402 if (audio_extn_utils_is_dolby_format(out->format) &&
Ashish Jain81eb2a82015-05-13 10:52:34 +05302403 /*TODO:Extend code to support DTS passthrough*/
2404 /*set compressed channel status bits*/
Arun Mirpurie008ed22019-03-21 11:21:04 -07002405 audio_extn_passthru_is_passthrough_stream(out) &&
2406 audio_extn_is_hdmi_passthru_enabled()) {
Ashish Jain81eb2a82015-05-13 10:52:34 +05302407 get_compressed_channel_status(buffer, bytes, channel_status, AUDIO_PARSER_CODEC_AC3);
Arun Mirpurie008ed22019-03-21 11:21:04 -07002408 } else {
Ashish Jain81eb2a82015-05-13 10:52:34 +05302409 /*set channel status bit for LPCM*/
2410 get_lpcm_channel_status(out->sample_rate, channel_status);
2411 }
2412
2413 memcpy(iec958.status, channel_status,sizeof(iec958.status));
2414 ctl = mixer_get_ctl_by_name(out->dev->mixer, mixer_ctl_name);
2415 if (!ctl) {
2416 ALOGE("%s: Could not get ctl for mixer cmd - %s",
2417 __func__, mixer_ctl_name);
2418 return;
2419 }
2420 if (mixer_ctl_set_array(ctl, &iec958, sizeof(iec958)) < 0) {
2421 ALOGE("%s: Could not set channel status for ext HDMI ",
2422 __func__);
2423 return;
2424 }
2425
2426}
2427#endif
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302428
2429int audio_extn_utils_get_avt_device_drift(
2430 struct audio_usecase *usecase,
2431 struct audio_avt_device_drift_param *drift_param)
2432{
2433 int ret = 0, count = 0;
2434 char avt_device_drift_mixer_ctl_name[MIXER_PATH_MAX_LENGTH] = {0};
Naresh Tanniru6160c712017-04-17 15:43:48 +05302435 const char *backend = NULL;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302436 struct mixer_ctl *ctl = NULL;
2437 struct audio_avt_device_drift_stats drift_stats;
2438 struct audio_device *adev = NULL;
2439
2440 if (usecase != NULL && usecase->type == PCM_PLAYBACK) {
Naresh Tanniru6160c712017-04-17 15:43:48 +05302441 backend = platform_get_snd_device_backend_interface(usecase->out_snd_device);
2442 if (!backend) {
2443 ALOGE("%s: Unsupported device %d", __func__,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08002444 get_device_types(&usecase->stream.out->device_list));
Naresh Tanniru6160c712017-04-17 15:43:48 +05302445 ret = -EINVAL;
2446 goto done;
2447 }
2448 strlcpy(avt_device_drift_mixer_ctl_name,
2449 backend,
2450 MIXER_PATH_MAX_LENGTH);
2451
2452 count = strlen(backend);
2453 if (MIXER_PATH_MAX_LENGTH - count > 0) {
2454 strlcat(&avt_device_drift_mixer_ctl_name[count],
2455 " DRIFT",
2456 MIXER_PATH_MAX_LENGTH - count);
2457 } else {
2458 ret = -EINVAL;
2459 goto done;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302460 }
2461 } else {
Naresh Tanniru7586e292017-04-13 10:38:13 +05302462 ALOGE("%s: Invalid usecase",__func__);
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302463 ret = -EINVAL;
Naresh Tanniru6160c712017-04-17 15:43:48 +05302464 goto done;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302465 }
2466
Naresh Tanniru6160c712017-04-17 15:43:48 +05302467 adev = usecase->stream.out->dev;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302468 ctl = mixer_get_ctl_by_name(adev->mixer, avt_device_drift_mixer_ctl_name);
2469 if (!ctl) {
2470 ALOGE("%s: Could not get ctl for mixer cmd - %s",
2471 __func__, avt_device_drift_mixer_ctl_name);
2472
2473 ret = -EINVAL;
2474 goto done;
2475 }
2476
2477 ALOGV("%s: Getting AV Timer vs Device Drift mixer ctrl name %s", __func__,
2478 avt_device_drift_mixer_ctl_name);
2479
2480 mixer_ctl_update(ctl);
2481 count = mixer_ctl_get_num_values(ctl);
2482 if (count != sizeof(struct audio_avt_device_drift_stats)) {
2483 ALOGE("%s: mixer_ctl_get_num_values() invalid drift_stats data size",
2484 __func__);
2485
2486 ret = -EINVAL;
2487 goto done;
2488 }
2489
2490 ret = mixer_ctl_get_array(ctl, (void *)&drift_stats, count);
2491 if (ret != 0) {
2492 ALOGE("%s: mixer_ctl_get_array() failed to get drift_stats Params",
2493 __func__);
2494
2495 ret = -EINVAL;
2496 goto done;
2497 }
2498 memcpy(drift_param, &drift_stats.drift_param,
2499 sizeof(struct audio_avt_device_drift_param));
2500done:
2501 return ret;
2502}
Manish Dewangan07de2142017-02-27 19:27:20 +05302503
2504#ifdef SNDRV_COMPRESS_PATH_DELAY
2505int audio_extn_utils_compress_get_dsp_latency(struct stream_out *out)
2506{
2507 int ret = -EINVAL;
2508 struct snd_compr_metadata metadata;
2509 int delay_ms = COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
2510
Weiyin Jiangc49a3b52018-08-17 16:16:08 +08002511 /* override the latency for pcm offload use case */
2512 if ((out->flags & AUDIO_OUTPUT_FLAG_DIRECT) &&
2513 !(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)) {
2514 delay_ms = PCM_OFFLOAD_PLAYBACK_LATENCY;
2515 }
2516
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -07002517 if (property_get_bool("vendor.audio.playback.dsp.pathdelay", false)) {
Manish Dewangan07de2142017-02-27 19:27:20 +05302518 ALOGD("%s:: Quering DSP delay %d",__func__, __LINE__);
2519 if (!(is_offload_usecase(out->usecase))) {
2520 ALOGE("%s:: not supported for non offload session", __func__);
2521 goto exit;
2522 }
2523
2524 if (!out->compr) {
2525 ALOGD("%s:: Invalid compress handle,returning default dsp latency",
2526 __func__);
2527 goto exit;
2528 }
2529
2530 metadata.key = SNDRV_COMPRESS_PATH_DELAY;
2531 ret = compress_get_metadata(out->compr, &metadata);
2532 if(ret) {
2533 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2534 goto exit;
2535 }
2536 delay_ms = metadata.value[0] / 1000; /*convert to ms*/
2537 } else {
2538 ALOGD("%s:: Using Fix DSP delay",__func__);
2539 }
2540
2541exit:
2542 ALOGD("%s:: delay in ms is %d",__func__, delay_ms);
2543 return delay_ms;
2544}
2545#else
2546int audio_extn_utils_compress_get_dsp_latency(struct stream_out *out __unused)
2547{
2548 return COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
2549}
2550#endif
Manish Dewangan69426c82017-01-30 17:35:36 +05302551
2552#ifdef SNDRV_COMPRESS_RENDER_MODE
2553int audio_extn_utils_compress_set_render_mode(struct stream_out *out)
2554{
2555 struct snd_compr_metadata metadata;
2556 int ret = -EINVAL;
2557
2558 if (!(is_offload_usecase(out->usecase))) {
2559 ALOGE("%s:: not supported for non offload session", __func__);
2560 goto exit;
2561 }
2562
2563 if (!out->compr) {
2564 ALOGD("%s:: Invalid compress handle",
2565 __func__);
2566 goto exit;
2567 }
2568
2569 ALOGD("%s:: render mode %d", __func__, out->render_mode);
2570
2571 metadata.key = SNDRV_COMPRESS_RENDER_MODE;
2572 if (out->render_mode == RENDER_MODE_AUDIO_MASTER) {
2573 metadata.value[0] = SNDRV_COMPRESS_RENDER_MODE_AUDIO_MASTER;
2574 } else if (out->render_mode == RENDER_MODE_AUDIO_STC_MASTER) {
2575 metadata.value[0] = SNDRV_COMPRESS_RENDER_MODE_STC_MASTER;
2576 } else {
2577 ret = 0;
2578 goto exit;
2579 }
2580 ret = compress_set_metadata(out->compr, &metadata);
2581 if(ret) {
2582 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2583 }
2584exit:
2585 return ret;
2586}
2587#else
2588int audio_extn_utils_compress_set_render_mode(struct stream_out *out __unused)
2589{
2590 ALOGD("%s:: configuring render mode not supported", __func__);
2591 return 0;
2592}
2593#endif
Manish Dewangan58229382017-02-02 15:48:41 +05302594
2595#ifdef SNDRV_COMPRESS_CLK_REC_MODE
2596int audio_extn_utils_compress_set_clk_rec_mode(
2597 struct audio_usecase *usecase)
2598{
2599 struct snd_compr_metadata metadata;
2600 struct stream_out *out = NULL;
2601 int ret = -EINVAL;
2602
Naresh Tanniru7586e292017-04-13 10:38:13 +05302603 if (usecase == NULL || usecase->type != PCM_PLAYBACK) {
Manish Dewangan58229382017-02-02 15:48:41 +05302604 ALOGE("%s:: Invalid use case", __func__);
2605 goto exit;
2606 }
2607
2608 out = usecase->stream.out;
2609 if (!out) {
2610 ALOGE("%s:: invalid stream", __func__);
2611 goto exit;
2612 }
2613
2614 if (!is_offload_usecase(out->usecase)) {
2615 ALOGE("%s:: not supported for non offload session", __func__);
2616 goto exit;
2617 }
2618
2619 if (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER) {
2620 ALOGD("%s:: clk recovery is only supported in STC render mode",
2621 __func__);
2622 ret = 0;
2623 goto exit;
2624 }
2625
2626 if (!out->compr) {
2627 ALOGD("%s:: Invalid compress handle",
2628 __func__);
2629 goto exit;
2630 }
2631 metadata.key = SNDRV_COMPRESS_CLK_REC_MODE;
2632 switch(usecase->out_snd_device) {
2633 case SND_DEVICE_OUT_HDMI:
2634 case SND_DEVICE_OUT_SPEAKER_AND_HDMI:
2635 case SND_DEVICE_OUT_DISPLAY_PORT:
2636 case SND_DEVICE_OUT_SPEAKER_AND_DISPLAY_PORT:
2637 metadata.value[0] = SNDRV_COMPRESS_CLK_REC_MODE_NONE;
2638 break;
2639 default:
2640 metadata.value[0] = SNDRV_COMPRESS_CLK_REC_MODE_AUTO;
2641 break;
2642 }
2643
2644 ALOGD("%s:: clk recovery mode %d",__func__, metadata.value[0]);
2645
2646 ret = compress_set_metadata(out->compr, &metadata);
2647 if(ret) {
2648 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2649 }
2650
2651exit:
2652 return ret;
2653}
2654#else
2655int audio_extn_utils_compress_set_clk_rec_mode(
2656 struct audio_usecase *usecase __unused)
2657{
2658 ALOGD("%s:: configuring render mode not supported", __func__);
2659 return 0;
2660}
2661#endif
Manish Dewangan27346042017-03-01 12:56:12 +05302662
2663#ifdef SNDRV_COMPRESS_RENDER_WINDOW
2664int audio_extn_utils_compress_set_render_window(
2665 struct stream_out *out,
2666 struct audio_out_render_window_param *render_window)
2667{
2668 struct snd_compr_metadata metadata;
2669 int ret = -EINVAL;
2670
Manish Dewangan27346042017-03-01 12:56:12 +05302671 if(render_window == NULL) {
2672 ALOGE("%s:: Invalid render_window", __func__);
2673 goto exit;
2674 }
2675
Aniket Kumar Lata1e1d3662017-06-01 18:45:48 -07002676 ALOGD("%s:: render window start 0x%"PRIx64" end 0x%"PRIx64"",
2677 __func__,render_window->render_ws, render_window->render_we);
2678
Manish Dewangan27346042017-03-01 12:56:12 +05302679 if (!is_offload_usecase(out->usecase)) {
2680 ALOGE("%s:: not supported for non offload session", __func__);
2681 goto exit;
2682 }
2683
Naresh Tanniru6160c712017-04-17 15:43:48 +05302684 if ((out->render_mode != RENDER_MODE_AUDIO_MASTER) &&
2685 (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER)) {
Manish Dewangan27346042017-03-01 12:56:12 +05302686 ALOGD("%s:: only supported in timestamp mode, current "
2687 "render mode mode %d", __func__, out->render_mode);
2688 goto exit;
2689 }
2690
2691 if (!out->compr) {
2692 ALOGW("%s:: offload session not yet opened,"
2693 "render window will be configure later", __func__);
2694 /* store render window to reconfigure in start_output_stream() */
2695 goto exit;
2696 }
2697
2698 metadata.key = SNDRV_COMPRESS_RENDER_WINDOW;
2699 /*render window start value */
2700 metadata.value[0] = 0xFFFFFFFF & render_window->render_ws; /* lsb */
2701 metadata.value[1] = \
2702 (0xFFFFFFFF00000000 & render_window->render_ws) >> 32; /* msb*/
2703 /*render window end value */
2704 metadata.value[2] = 0xFFFFFFFF & render_window->render_we; /* lsb */
2705 metadata.value[3] = \
2706 (0xFFFFFFFF00000000 & render_window->render_we) >> 32; /* msb*/
2707
2708 ret = compress_set_metadata(out->compr, &metadata);
2709 if(ret) {
2710 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2711 }
2712
2713exit:
2714 return ret;
2715}
2716#else
2717int audio_extn_utils_compress_set_render_window(
2718 struct stream_out *out __unused,
2719 struct audio_out_render_window_param *render_window __unused)
2720{
2721 ALOGD("%s:: configuring render window not supported", __func__);
2722 return 0;
2723}
2724#endif
Manish Dewangan14956cc2017-02-14 18:54:42 +05302725
2726#ifdef SNDRV_COMPRESS_START_DELAY
2727int audio_extn_utils_compress_set_start_delay(
2728 struct stream_out *out,
2729 struct audio_out_start_delay_param *delay_param)
2730{
2731 struct snd_compr_metadata metadata;
2732 int ret = -EINVAL;
2733
2734 if(delay_param == NULL) {
2735 ALOGE("%s:: Invalid delay_param", __func__);
2736 goto exit;
2737 }
2738
2739 ALOGD("%s:: render start delay 0x%"PRIx64" ", __func__,
2740 delay_param->start_delay);
2741
2742 if (!is_offload_usecase(out->usecase)) {
2743 ALOGE("%s:: not supported for non offload session", __func__);
2744 goto exit;
2745 }
2746
Naresh Tanniru6160c712017-04-17 15:43:48 +05302747 if ((out->render_mode != RENDER_MODE_AUDIO_MASTER) &&
2748 (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER)) {
Manish Dewangan14956cc2017-02-14 18:54:42 +05302749 ALOGD("%s:: only supported in timestamp mode, current "
2750 "render mode mode %d", __func__, out->render_mode);
2751 goto exit;
2752 }
2753
2754 if (!out->compr) {
2755 ALOGW("%s:: offload session not yet opened,"
2756 "start delay will be configure later", __func__);
2757 goto exit;
2758 }
2759
2760 metadata.key = SNDRV_COMPRESS_START_DELAY;
2761 metadata.value[0] = 0xFFFFFFFF & delay_param->start_delay; /* lsb */
2762 metadata.value[1] = \
2763 (0xFFFFFFFF00000000 & delay_param->start_delay) >> 32; /* msb*/
2764
2765 ret = compress_set_metadata(out->compr, &metadata);
2766 if(ret) {
2767 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2768 }
2769
2770exit:
2771 return ret;
2772}
2773#else
2774int audio_extn_utils_compress_set_start_delay(
2775 struct stream_out *out __unused,
2776 struct audio_out_start_delay_param *delay_param __unused)
2777{
2778 ALOGD("%s:: configuring render window not supported", __func__);
2779 return 0;
2780}
2781#endif
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002782
Surendar Karka287348c2019-04-10 18:31:46 +05302783#ifdef SNDRV_COMPRESS_DSP_POSITION
2784int audio_extn_utils_compress_get_dsp_presentation_pos(struct stream_out *out,
2785 uint64_t *frames, struct timespec *timestamp, int32_t clock_id)
2786{
2787 int ret = -EINVAL;
2788 uint64_t *val = NULL;
2789 uint64_t time = 0;
2790 struct snd_compr_metadata metadata;
2791
2792 ALOGV("%s:: Quering DSP position with clock id %d",__func__, clock_id);
2793 metadata.key = SNDRV_COMPRESS_DSP_POSITION;
2794 metadata.value[0] = clock_id;
2795 ret = compress_get_metadata(out->compr, &metadata);
2796 if (ret) {
2797 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2798 ret = -errno;
2799 goto exit;
2800 }
2801 val = (uint64_t *)&metadata.value[1];
2802 *frames = *val;
2803 time = *(val + 1);
2804 timestamp->tv_sec = time / 1000000;
2805 timestamp->tv_nsec = (time % 1000000)*1000;
2806
2807exit:
2808 return ret;
2809}
2810#else
2811int audio_extn_utils_compress_get_dsp_presentation_pos(struct stream_out *out __unused,
2812 uint64_t *frames __unused, struct timespec *timestamp __unused,
2813 int32_t clock_id __unused)
2814{
2815 ALOGD("%s:: dsp presentation position not supported", __func__);
2816 return 0;
2817
2818}
2819#endif
2820
2821#ifdef SNDRV_PCM_IOCTL_DSP_POSITION
2822int audio_extn_utils_pcm_get_dsp_presentation_pos(struct stream_out *out,
2823 uint64_t *frames, struct timespec *timestamp, int32_t clock_id)
2824{
2825 int ret = -EINVAL;
2826 uint64_t time = 0;
2827 struct snd_pcm_prsnt_position prsnt_position;
Krishna Kishor Jhac1542b22022-08-22 13:12:09 +05302828 memset(&prsnt_position, 0, sizeof(struct snd_pcm_prsnt_position));
Surendar Karka287348c2019-04-10 18:31:46 +05302829
2830 ALOGV("%s:: Quering DSP position with clock id %d",__func__, clock_id);
2831 prsnt_position.clock_id = clock_id;
2832 ret = pcm_ioctl(out->pcm, SNDRV_PCM_IOCTL_DSP_POSITION, &prsnt_position);
2833 if (ret) {
2834 ALOGE("%s::error %d", __func__, ret);
2835 ret = -EIO;
2836 goto exit;
2837 }
2838
2839 *frames = prsnt_position.frames;
2840 time = prsnt_position.timestamp;
2841 timestamp->tv_sec = time / 1000000;
2842 timestamp->tv_nsec = (time % 1000000)*1000;
2843
2844exit:
2845 return ret;
2846}
2847#else
2848int audio_extn_utils_pcm_get_dsp_presentation_pos(struct stream_out *out __unused,
2849 uint64_t *frames __unused, struct timespec *timestamp __unused,
2850 int32_t clock_id __unused)
2851{
2852 ALOGD("%s:: dsp presentation position not supported", __func__);
2853 return 0;
2854
2855}
2856#endif
2857
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002858#define MAX_SND_CARD 8
Jaideep Sharmadf948882020-10-22 14:13:52 +05302859#define RETRY_US 400000
2860#define RETRY_NUMBER 100
Aalique Grahame22e49102018-12-18 14:23:57 -08002861#define PLATFORM_INFO_XML_PATH "audio_platform_info.xml"
2862#define PLATFORM_INFO_XML_BASE_STRING "audio_platform_info"
2863
Aalique Grahame22e49102018-12-18 14:23:57 -08002864bool audio_extn_utils_resolve_config_file(char file_name[MIXER_PATH_MAX_LENGTH])
2865{
2866 char full_config_path[MIXER_PATH_MAX_LENGTH];
Saurav Kumarf5d2c342020-07-29 19:31:49 +05302867 char vendor_config_path[VENDOR_CONFIG_PATH_MAX_LENGTH];
2868
2869 /* Get path for audio configuration files in vendor */
2870 audio_get_vendor_config_path(vendor_config_path,
2871 sizeof(vendor_config_path));
2872 snprintf(full_config_path, sizeof(full_config_path),
2873 "%s/%s", vendor_config_path, file_name);
2874 if (F_OK == access(full_config_path, 0)) {
2875 strlcpy(file_name, full_config_path, MIXER_PATH_MAX_LENGTH);
2876 return true;
Aalique Grahame22e49102018-12-18 14:23:57 -08002877 }
2878 return false;
2879}
2880
2881/* platform_info_file should be size 'MIXER_PATH_MAX_LENGTH' */
2882int audio_extn_utils_get_platform_info(const char* snd_card_name, char* platform_info_file)
2883{
2884 if (NULL == snd_card_name) {
2885 return -1;
2886 }
2887
2888 struct snd_card_split *snd_split_handle = NULL;
2889 int ret = 0;
2890 audio_extn_set_snd_card_split(snd_card_name);
2891 snd_split_handle = audio_extn_get_snd_card_split();
2892
2893 snprintf(platform_info_file, MIXER_PATH_MAX_LENGTH, "%s_%s_%s.xml",
2894 PLATFORM_INFO_XML_BASE_STRING, snd_split_handle->snd_card,
2895 snd_split_handle->form_factor);
2896
2897 if (!audio_extn_utils_resolve_config_file(platform_info_file)) {
2898 memset(platform_info_file, 0, MIXER_PATH_MAX_LENGTH);
2899 snprintf(platform_info_file, MIXER_PATH_MAX_LENGTH, "%s_%s.xml",
2900 PLATFORM_INFO_XML_BASE_STRING, snd_split_handle->snd_card);
2901
2902 if (!audio_extn_utils_resolve_config_file(platform_info_file)) {
2903 memset(platform_info_file, 0, MIXER_PATH_MAX_LENGTH);
2904 strlcpy(platform_info_file, PLATFORM_INFO_XML_PATH, MIXER_PATH_MAX_LENGTH);
2905 ret = audio_extn_utils_resolve_config_file(platform_info_file) ? 0 : -1;
2906 }
2907 }
2908
2909 return ret;
2910}
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002911
2912int audio_extn_utils_get_snd_card_num()
2913{
Soumya Managoli9fee7c62018-04-06 16:21:50 +05302914 int snd_card_num = 0;
2915 struct mixer *mixer = NULL;
2916
2917 snd_card_num = audio_extn_utils_open_snd_mixer(&mixer);
2918 if (mixer)
2919 mixer_close(mixer);
2920 return snd_card_num;
2921}
2922
2923int audio_extn_utils_open_snd_mixer(struct mixer **mixer_handle)
2924{
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002925
2926 void *hw_info = NULL;
2927 struct mixer *mixer = NULL;
2928 int retry_num = 0;
mpangfaa7bae2019-01-15 16:38:13 +08002929 int snd_card_num = 0;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002930 char* snd_card_name = NULL;
mpangfaa7bae2019-01-15 16:38:13 +08002931 int snd_card_detection_info[MAX_SND_CARD] = {0};
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002932
Soumya Managoli9fee7c62018-04-06 16:21:50 +05302933 if (!mixer_handle) {
2934 ALOGE("invalid mixer handle");
2935 return -1;
2936 }
2937 *mixer_handle = NULL;
Ashish Jain30ae8942017-11-05 17:21:23 +05302938 /*
mpangfaa7bae2019-01-15 16:38:13 +08002939 * Try with all the sound cards ( 0 to 7 ) and if none of them were detected
Ashish Jain30ae8942017-11-05 17:21:23 +05302940 * sleep for 1 sec and try detections with sound card 0 again.
2941 * If sound card gets detected, check if it is relevant, if not check with the
2942 * other sound cards. To ensure that the irrelevant sound card is not check again,
2943 * we maintain it in min_snd_card_num.
2944 */
2945 while (retry_num < RETRY_NUMBER) {
mpangfaa7bae2019-01-15 16:38:13 +08002946 snd_card_num = 0;
2947 while (snd_card_num < MAX_SND_CARD) {
2948 if (snd_card_detection_info[snd_card_num] == 0) {
2949 mixer = mixer_open(snd_card_num);
2950 if (!mixer)
2951 snd_card_num++;
2952 else
2953 break;
2954 } else
2955 snd_card_num++;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002956 }
2957
2958 if (!mixer) {
Ashish Jain30ae8942017-11-05 17:21:23 +05302959 usleep(RETRY_US);
Ashish Jain30ae8942017-11-05 17:21:23 +05302960 retry_num++;
mpangfaa7bae2019-01-15 16:38:13 +08002961 ALOGD("%s: retry, retry_num %d", __func__, retry_num);
Ashish Jain30ae8942017-11-05 17:21:23 +05302962 continue;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002963 }
2964
2965 snd_card_name = strdup(mixer_get_name(mixer));
2966 if (!snd_card_name) {
2967 ALOGE("failed to allocate memory for snd_card_name\n");
2968 mixer_close(mixer);
2969 return -1;
2970 }
2971 ALOGD("%s: snd_card_name: %s", __func__, snd_card_name);
mpangfaa7bae2019-01-15 16:38:13 +08002972 snd_card_detection_info[snd_card_num] = 1;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002973 hw_info = hw_info_init(snd_card_name);
2974 if (hw_info) {
2975 ALOGD("%s: Opened sound card:%d", __func__, snd_card_num);
2976 break;
2977 }
mpangfaa7bae2019-01-15 16:38:13 +08002978 ALOGE("%s: Failed to init hardware info, snd_card_num:%d", __func__, snd_card_num);
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002979
Dhananjay Kumara7e27832017-07-11 15:40:39 +05302980 free(snd_card_name);
2981 snd_card_name = NULL;
2982
2983 mixer_close(mixer);
2984 mixer = NULL;
2985 }
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002986 if (snd_card_name)
2987 free(snd_card_name);
Ashish Jain30ae8942017-11-05 17:21:23 +05302988
Dhananjay Kumara7e27832017-07-11 15:40:39 +05302989 if (hw_info)
2990 hw_info_deinit(hw_info);
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002991
mpangfaa7bae2019-01-15 16:38:13 +08002992 if (retry_num >= RETRY_NUMBER) {
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002993 ALOGE("%s: Unable to find correct sound card, aborting.", __func__);
2994 return -1;
2995 }
2996
Soumya Managoli9fee7c62018-04-06 16:21:50 +05302997 if (mixer)
2998 *mixer_handle = mixer;
2999
Vignesh Kulothungan55396882017-04-20 14:37:02 -07003000 return snd_card_num;
3001}
Naresh Tanniru6160c712017-04-17 15:43:48 +05303002
Soumya Managoli9fee7c62018-04-06 16:21:50 +05303003void audio_extn_utils_close_snd_mixer(struct mixer *mixer)
3004{
3005 if (mixer)
3006 mixer_close(mixer);
3007}
3008
Naresh Tanniru6160c712017-04-17 15:43:48 +05303009#ifdef SNDRV_COMPRESS_ENABLE_ADJUST_SESSION_CLOCK
3010int audio_extn_utils_compress_enable_drift_correction(
3011 struct stream_out *out,
3012 struct audio_out_enable_drift_correction *drift)
3013{
3014 struct snd_compr_metadata metadata;
3015 int ret = -EINVAL;
3016
3017 if(drift == NULL) {
3018 ALOGE("%s:: Invalid param", __func__);
3019 goto exit;
3020 }
3021
3022 ALOGD("%s:: drift enable %d", __func__,drift->enable);
3023
3024 if (!is_offload_usecase(out->usecase)) {
3025 ALOGE("%s:: not supported for non offload session", __func__);
3026 goto exit;
3027 }
3028
3029 if (!out->compr) {
3030 ALOGW("%s:: offload session not yet opened,"
3031 "start delay will be configure later", __func__);
3032 goto exit;
3033 }
3034
3035 metadata.key = SNDRV_COMPRESS_ENABLE_ADJUST_SESSION_CLOCK;
3036 metadata.value[0] = drift->enable;
3037 out->drift_correction_enabled = drift->enable;
3038
3039 ret = compress_set_metadata(out->compr, &metadata);
3040 if(ret) {
3041 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
3042 out->drift_correction_enabled = false;
3043 }
3044
3045exit:
3046 return ret;
3047}
3048#else
3049int audio_extn_utils_compress_enable_drift_correction(
3050 struct stream_out *out __unused,
3051 struct audio_out_enable_drift_correction *drift __unused)
3052{
3053 ALOGD("%s:: configuring drift enablement not supported", __func__);
3054 return 0;
3055}
3056#endif
3057
3058#ifdef SNDRV_COMPRESS_ADJUST_SESSION_CLOCK
3059int audio_extn_utils_compress_correct_drift(
3060 struct stream_out *out,
3061 struct audio_out_correct_drift *drift_param)
3062{
3063 struct snd_compr_metadata metadata;
3064 int ret = -EINVAL;
3065
3066 if (drift_param == NULL) {
3067 ALOGE("%s:: Invalid drift_param", __func__);
3068 goto exit;
3069 }
3070
3071 ALOGD("%s:: adjust time 0x%"PRIx64" ", __func__,
3072 drift_param->adjust_time);
3073
3074 if (!is_offload_usecase(out->usecase)) {
3075 ALOGE("%s:: not supported for non offload session", __func__);
3076 goto exit;
3077 }
3078
3079 if (!out->compr) {
3080 ALOGW("%s:: offload session not yet opened", __func__);
3081 goto exit;
3082 }
3083
3084 if (!out->drift_correction_enabled) {
3085 ALOGE("%s:: drift correction not enabled", __func__);
3086 goto exit;
3087 }
3088
3089 metadata.key = SNDRV_COMPRESS_ADJUST_SESSION_CLOCK;
3090 metadata.value[0] = 0xFFFFFFFF & drift_param->adjust_time; /* lsb */
3091 metadata.value[1] = \
3092 (0xFFFFFFFF00000000 & drift_param->adjust_time) >> 32; /* msb*/
3093
3094 ret = compress_set_metadata(out->compr, &metadata);
3095 if(ret)
3096 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
3097exit:
3098 return ret;
3099}
3100#else
3101int audio_extn_utils_compress_correct_drift(
3102 struct stream_out *out __unused,
3103 struct audio_out_correct_drift *drift_param __unused)
3104{
3105 ALOGD("%s:: setting adjust clock not supported", __func__);
3106 return 0;
3107}
3108#endif
Naresh Tanniru29bce4e2017-04-27 17:54:30 +05303109
3110int audio_extn_utils_set_channel_map(
3111 struct stream_out *out,
3112 struct audio_out_channel_map_param *channel_map_param)
3113{
3114 int ret = -EINVAL, i = 0;
3115 int channels = audio_channel_count_from_out_mask(out->channel_mask);
3116
3117 if (channel_map_param == NULL) {
3118 ALOGE("%s:: Invalid channel_map", __func__);
3119 goto exit;
3120 }
3121
3122 if (channel_map_param->channels != channels) {
3123 ALOGE("%s:: Channels(%d) does not match stream channels(%d)",
3124 __func__, channel_map_param->channels, channels);
3125 goto exit;
3126 }
3127
3128 for ( i = 0; i < channels; i++) {
3129 ALOGV("%s:: channel_map[%d]- %d", __func__, i, channel_map_param->channel_map[i]);
3130 out->channel_map_param.channel_map[i] = channel_map_param->channel_map[i];
3131 }
3132 ret = 0;
3133exit:
3134 return ret;
3135}
Varun Balaraje49253e2017-07-06 19:48:56 +05303136
3137int audio_extn_utils_set_pan_scale_params(
3138 struct stream_out *out,
3139 struct mix_matrix_params *mm_params)
3140{
3141 int ret = -EINVAL, i = 0, j = 0;
3142
Varun Balaraj003ee752017-08-07 17:54:55 +05303143 if (mm_params == NULL || out == NULL) {
3144 ALOGE("%s:: Invalid mix matrix or out param", __func__);
Varun Balaraje49253e2017-07-06 19:48:56 +05303145 goto exit;
3146 }
3147
3148 if (mm_params->num_output_channels > MAX_CHANNELS_SUPPORTED ||
3149 mm_params->num_output_channels <= 0 ||
3150 mm_params->num_input_channels > MAX_CHANNELS_SUPPORTED ||
3151 mm_params->num_input_channels <= 0)
3152 goto exit;
3153
3154 out->pan_scale_params.num_output_channels = mm_params->num_output_channels;
3155 out->pan_scale_params.num_input_channels = mm_params->num_input_channels;
3156 out->pan_scale_params.has_output_channel_map =
3157 mm_params->has_output_channel_map;
3158 for (i = 0; i < mm_params->num_output_channels; i++)
3159 out->pan_scale_params.output_channel_map[i] =
3160 mm_params->output_channel_map[i];
3161
3162 out->pan_scale_params.has_input_channel_map =
3163 mm_params->has_input_channel_map;
3164 for (i = 0; i < mm_params->num_input_channels; i++)
3165 out->pan_scale_params.input_channel_map[i] =
3166 mm_params->input_channel_map[i];
3167
3168 out->pan_scale_params.has_mixer_coeffs = mm_params->has_mixer_coeffs;
3169 for (i = 0; i < mm_params->num_output_channels; i++)
3170 for (j = 0; j < mm_params->num_input_channels; j++) {
3171 //Convert the channel coefficient gains in Q14 format
3172 out->pan_scale_params.mixer_coeffs[i][j] =
3173 mm_params->mixer_coeffs[i][j] * (2 << 13);
3174 }
3175
3176 ret = platform_set_stream_pan_scale_params(out->dev->platform,
3177 out->pcm_device_id,
3178 out->pan_scale_params);
3179
3180exit:
3181 return ret;
3182}
3183
3184int audio_extn_utils_set_downmix_params(
3185 struct stream_out *out,
3186 struct mix_matrix_params *mm_params)
3187{
3188 int ret = -EINVAL, i = 0, j = 0;
3189 struct audio_usecase *usecase = NULL;
3190
Aniket Kumar Lataf9f246e2017-09-15 15:20:16 -07003191 if (mm_params == NULL || out == NULL) {
Varun Balaraj003ee752017-08-07 17:54:55 +05303192 ALOGE("%s:: Invalid mix matrix or out param", __func__);
Varun Balaraje49253e2017-07-06 19:48:56 +05303193 goto exit;
3194 }
3195
3196 if (mm_params->num_output_channels > MAX_CHANNELS_SUPPORTED ||
3197 mm_params->num_output_channels <= 0 ||
3198 mm_params->num_input_channels > MAX_CHANNELS_SUPPORTED ||
3199 mm_params->num_input_channels <= 0)
3200 goto exit;
3201
3202 usecase = get_usecase_from_list(out->dev, out->usecase);
Varun Balaraj003ee752017-08-07 17:54:55 +05303203 if (!usecase) {
3204 ALOGE("%s: Get usecase list failed!", __func__);
Aniket Kumar Lataf9f246e2017-09-15 15:20:16 -07003205 goto exit;
3206 }
Varun Balaraje49253e2017-07-06 19:48:56 +05303207 out->downmix_params.num_output_channels = mm_params->num_output_channels;
3208 out->downmix_params.num_input_channels = mm_params->num_input_channels;
3209
3210 out->downmix_params.has_output_channel_map =
3211 mm_params->has_output_channel_map;
3212 for (i = 0; i < mm_params->num_output_channels; i++) {
3213 out->downmix_params.output_channel_map[i] =
3214 mm_params->output_channel_map[i];
3215 }
3216
3217 out->downmix_params.has_input_channel_map =
3218 mm_params->has_input_channel_map;
3219 for (i = 0; i < mm_params->num_input_channels; i++)
3220 out->downmix_params.input_channel_map[i] =
3221 mm_params->input_channel_map[i];
3222
3223 out->downmix_params.has_mixer_coeffs = mm_params->has_mixer_coeffs;
3224 for (i = 0; i < mm_params->num_output_channels; i++)
Nikhil Latukar2e6f6242017-08-15 13:37:07 +05303225 for (j = 0; j < mm_params->num_input_channels; j++) {
3226 //Convert the channel coefficient gains in Q14 format
Varun Balaraje49253e2017-07-06 19:48:56 +05303227 out->downmix_params.mixer_coeffs[i][j] =
Nikhil Latukar2e6f6242017-08-15 13:37:07 +05303228 mm_params->mixer_coeffs[i][j] * (2 << 13);
3229 }
Varun Balaraje49253e2017-07-06 19:48:56 +05303230
3231 ret = platform_set_stream_downmix_params(out->dev->platform,
3232 out->pcm_device_id,
3233 usecase->out_snd_device,
3234 out->downmix_params);
3235
3236exit:
3237 return ret;
3238}
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05303239
3240bool audio_extn_utils_is_dolby_format(audio_format_t format)
3241{
3242 if (format == AUDIO_FORMAT_AC3 ||
3243 format == AUDIO_FORMAT_E_AC3 ||
3244 format == AUDIO_FORMAT_E_AC3_JOC)
3245 return true;
3246 else
3247 return false;
3248}
3249
`Deeraj Soman676c2702017-09-18 19:25:53 +05303250int audio_extn_utils_get_bit_width_from_string(const char *id_string)
3251{
3252 int i;
3253 const mixer_config_lookup mixer_bitwidth_config[] = {{"S24_3LE", 24},
3254 {"S32_LE", 32},
3255 {"S24_LE", 24},
3256 {"S16_LE", 16}};
3257 int num_configs = sizeof(mixer_bitwidth_config) / sizeof(mixer_bitwidth_config[0]);
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05303258
`Deeraj Soman676c2702017-09-18 19:25:53 +05303259 for (i = 0; i < num_configs; i++) {
3260 if (!strcmp(id_string, mixer_bitwidth_config[i].id_string))
3261 return mixer_bitwidth_config[i].value;
3262 }
3263
3264 return -EINVAL;
3265}
3266
3267int audio_extn_utils_get_sample_rate_from_string(const char *id_string)
3268{
3269 int i;
Weiyin Jiang30f9ab52020-11-11 11:37:21 +08003270 const mixer_config_lookup mixer_samplerate_config[] = {{"KHZ_8", 8000},
3271 {"KHZ_11P025", 11025},
3272 {"KHZ_16", 16000},
3273 {"KHZ_22P05", 22050},
3274 {"KHZ_32", 32000},
`Deeraj Soman676c2702017-09-18 19:25:53 +05303275 {"KHZ_48", 48000},
3276 {"KHZ_96", 96000},
3277 {"KHZ_144", 144000},
3278 {"KHZ_192", 192000},
3279 {"KHZ_384", 384000},
3280 {"KHZ_44P1", 44100},
3281 {"KHZ_88P2", 88200},
3282 {"KHZ_176P4", 176400},
3283 {"KHZ_352P8", 352800}};
3284 int num_configs = sizeof(mixer_samplerate_config) / sizeof(mixer_samplerate_config[0]);
3285
3286 for (i = 0; i < num_configs; i++) {
3287 if (!strcmp(id_string, mixer_samplerate_config[i].id_string))
3288 return mixer_samplerate_config[i].value;
3289 }
3290
3291 return -EINVAL;
3292}
3293
3294int audio_extn_utils_get_channels_from_string(const char *id_string)
3295{
3296 int i;
3297 const mixer_config_lookup mixer_channels_config[] = {{"One", 1},
3298 {"Two", 2},
3299 {"Three",3},
3300 {"Four", 4},
3301 {"Five", 5},
3302 {"Six", 6},
3303 {"Seven", 7},
3304 {"Eight", 8}};
3305 int num_configs = sizeof(mixer_channels_config) / sizeof(mixer_channels_config[0]);
3306
3307 for (i = 0; i < num_configs; i++) {
3308 if (!strcmp(id_string, mixer_channels_config[i].id_string))
3309 return mixer_channels_config[i].value;
3310 }
3311
3312 return -EINVAL;
3313}
Surendar karka30569792018-05-08 12:02:21 +05303314
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08003315void audio_extn_utils_release_snd_device(snd_device_t snd_device)
3316{
3317 audio_extn_dev_arbi_release(snd_device);
3318 audio_extn_sound_trigger_update_device_status(snd_device,
3319 ST_EVENT_SND_DEVICE_FREE);
3320 audio_extn_listen_update_device_status(snd_device,
3321 LISTEN_EVENT_SND_DEVICE_FREE);
3322}
3323
Weiyin Jiangd5718bd2019-09-04 16:52:08 +08003324int audio_extn_utils_get_license_params(
3325 const struct audio_device *adev,
3326 struct audio_license_params *license_params)
Surendar karka30569792018-05-08 12:02:21 +05303327{
3328 if(!license_params)
3329 return -EINVAL;
Weiyin Jiangd5718bd2019-09-04 16:52:08 +08003330
3331 return platform_get_license_by_product(adev->platform,
3332 (const char*)license_params->product, &license_params->key, license_params->license);
Surendar karka30569792018-05-08 12:02:21 +05303333}
3334
Aalique Grahame22e49102018-12-18 14:23:57 -08003335int audio_extn_utils_send_app_type_gain(struct audio_device *adev,
3336 int app_type,
3337 int *gain)
3338{
3339 int gain_cfg[4];
3340 const char *mixer_ctl_name = "App Type Gain";
3341 struct mixer_ctl *ctl;
3342 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
3343 if (!ctl) {
3344 ALOGE("%s: Could not get volume ctl mixer %s", __func__,
3345 mixer_ctl_name);
3346 return -EINVAL;
3347 }
3348 gain_cfg[0] = 0;
3349 gain_cfg[1] = app_type;
3350 gain_cfg[2] = gain[0];
3351 gain_cfg[3] = gain[1];
3352 ALOGV("%s app_type %d l(%d) r(%d)", __func__, app_type, gain[0], gain[1]);
3353 return mixer_ctl_set_array(ctl, gain_cfg,
3354 sizeof(gain_cfg)/sizeof(gain_cfg[0]));
3355}
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -07003356
Weiyin Jiangd5718bd2019-09-04 16:52:08 +08003357static void vndk_fwk_init()
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -07003358{
Weiyin Jiangd5718bd2019-09-04 16:52:08 +08003359 if (mVndkFwk.lib_handle != NULL)
3360 return;
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -07003361
Weiyin Jiangd5718bd2019-09-04 16:52:08 +08003362 mVndkFwk.lib_handle = dlopen(VNDK_FWK_LIB_PATH, RTLD_NOW);
3363 if (mVndkFwk.lib_handle == NULL) {
3364 ALOGW("%s: failed to dlopen VNDK_FWK_LIB %s", __func__, strerror(errno));
3365 return;
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -07003366 }
3367
Weiyin Jiangd5718bd2019-09-04 16:52:08 +08003368 *(void **)(&mVndkFwk.isVendorEnhancedFwk) =
3369 dlsym(mVndkFwk.lib_handle, "isRunningWithVendorEnhancedFramework");
3370 if (mVndkFwk.isVendorEnhancedFwk == NULL) {
3371 ALOGW("%s: dlsym failed %s", __func__, strerror(errno));
3372 if (mVndkFwk.lib_handle) {
3373 dlclose(mVndkFwk.lib_handle);
3374 mVndkFwk.lib_handle = NULL;
3375 }
3376 return;
3377 }
3378
3379
3380 *(void **)(&mVndkFwk.getVendorEnhancedInfo) =
3381 dlsym(mVndkFwk.lib_handle, "getVendorEnhancedInfo");
3382 if (mVndkFwk.getVendorEnhancedInfo == NULL) {
3383 ALOGW("%s: dlsym failed %s", __func__, strerror(errno));
3384 if (mVndkFwk.lib_handle) {
3385 dlclose(mVndkFwk.lib_handle);
3386 mVndkFwk.lib_handle = NULL;
3387 }
3388 }
3389
3390 return;
3391}
3392
3393bool audio_extn_utils_is_vendor_enhanced_fwk()
3394{
3395 static int is_vendor_enhanced_fwk = -EINVAL;
3396 if (is_vendor_enhanced_fwk != -EINVAL)
3397 return (bool)is_vendor_enhanced_fwk;
3398
3399 vndk_fwk_init();
3400
3401 if (mVndkFwk.isVendorEnhancedFwk != NULL) {
3402 is_vendor_enhanced_fwk = mVndkFwk.isVendorEnhancedFwk();
3403 ALOGW("%s: is_vendor_enhanced_fwk %d", __func__, is_vendor_enhanced_fwk);
3404 } else {
3405 is_vendor_enhanced_fwk = 0;
3406 ALOGW("%s: default to non enhanced_fwk config", __func__);
3407 }
3408
3409 return (bool)is_vendor_enhanced_fwk;
3410}
3411
3412int audio_extn_utils_get_vendor_enhanced_info()
3413{
3414 static int vendor_enhanced_info = -EINVAL;
3415 if (vendor_enhanced_info != -EINVAL)
3416 return vendor_enhanced_info;
3417
3418 vndk_fwk_init();
3419
3420 if (mVndkFwk.getVendorEnhancedInfo != NULL) {
3421 vendor_enhanced_info = mVndkFwk.getVendorEnhancedInfo();
3422 ALOGW("%s: vendor_enhanced_info 0x%x", __func__, vendor_enhanced_info);
3423 } else {
3424 vendor_enhanced_info = 0x0;
3425 ALOGW("%s: default to vendor_enhanced_info 0x0", __func__);
3426 }
3427
3428 return vendor_enhanced_info;
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -07003429}
Deeraj Soman14230922019-01-30 16:39:30 +05303430
Deeraj Somanfa377bf2019-02-06 12:57:59 +05303431int audio_extn_utils_get_perf_mode_flag(void)
3432{
3433#ifdef COMPRESSED_PERF_MODE_FLAG
3434 return COMPRESSED_PERF_MODE_FLAG;
3435#else
3436 return 0;
3437#endif
3438}
3439
Deeraj Soman14230922019-01-30 16:39:30 +05303440size_t audio_extn_utils_get_input_buffer_size(uint32_t sample_rate,
3441 audio_format_t format,
3442 int channel_count,
3443 int64_t duration_ms,
3444 bool is_low_latency)
3445{
3446 size_t size = 0;
3447 size_t capture_duration = AUDIO_CAPTURE_PERIOD_DURATION_MSEC;
3448 uint32_t bytes_per_period_sample = 0;
3449
3450
3451 if (audio_extn_utils_check_input_parameters(sample_rate, format, channel_count) != 0)
3452 return 0;
3453
3454 if (duration_ms >= MIN_OFFLOAD_BUFFER_DURATION_MS && duration_ms <= MAX_OFFLOAD_BUFFER_DURATION_MS)
3455 capture_duration = duration_ms;
3456
3457 size = (sample_rate * capture_duration) / 1000;
3458 if (is_low_latency)
3459 size = LOW_LATENCY_CAPTURE_PERIOD_SIZE;
3460
3461
3462 bytes_per_period_sample = audio_bytes_per_sample(format) * channel_count;
3463 size *= bytes_per_period_sample;
3464
3465 /* make sure the size is multiple of 32 bytes and additionally multiple of
3466 * the frame_size (required for 24bit samples and non-power-of-2 channel counts)
3467 * At 48 kHz mono 16-bit PCM:
3468 * 5.000 ms = 240 frames = 15*16*1*2 = 480, a whole multiple of 32 (15)
3469 * 3.333 ms = 160 frames = 10*16*1*2 = 320, a whole multiple of 32 (10)
3470 *
3471 * The loop reaches result within 32 iterations, as initial size is
3472 * already a multiple of frame_size
3473 */
3474 size = audio_extn_utils_nearest_multiple(size, audio_extn_utils_lcm(32, bytes_per_period_sample));
3475
3476 return size;
3477}
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07003478
3479int audio_extn_utils_hash_fn(void *key)
3480{
3481 return (int)key;
3482}
3483
3484bool audio_extn_utils_hash_eq(void *key1, void *key2)
3485{
3486 return (key1 == key2);
3487}