blob: 23ecf7a5901996e128419d48c811ddaa731673ea [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.
Kogara Naveen Kumar65828fe2022-10-14 16:41:04 +053018 *
sasevsdec27fa2023-04-12 10:50:47 +053019 * Changes from Qualcomm Innovation Center are provided under the following license:
Kogara Naveen Kumar65828fe2022-10-14 16:41:04 +053020 * Copyright (c) 2023 Qualcomm Innovation Center, Inc. All rights reserved.
sasevsdec27fa2023-04-12 10:50:47 +053021 * SPDX-License-Identifier: BSD-3-Clause-Clear
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070022 */
23
24#define LOG_TAG "audio_hw_utils"
25/* #define LOG_NDEBUG 0 */
26
Manish Dewangan27346042017-03-01 12:56:12 +053027#include <inttypes.h>
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070028#include <errno.h>
29#include <cutils/properties.h>
30#include <cutils/config_utils.h>
31#include <stdlib.h>
32#include <dlfcn.h>
33#include <cutils/str_parms.h>
Aalique Grahame22e49102018-12-18 14:23:57 -080034#include <log/log.h>
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070035#include <cutils/misc.h>
Vinay Vermaaddfa4a2018-04-29 14:03:38 +053036#include <unistd.h>
Surendar Karka287348c2019-04-10 18:31:46 +053037#include <sys/ioctl.h>
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070038
Manish Dewangan07de2142017-02-27 19:27:20 +053039
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070040#include "audio_hw.h"
41#include "platform.h"
42#include "platform_api.h"
43#include "audio_extn.h"
Deeraj Soman14230922019-01-30 16:39:30 +053044#include "voice_extn.h"
Narsinga Rao Chella212e2542014-11-17 19:57:04 -080045#include "voice.h"
Manish Dewangan07de2142017-02-27 19:27:20 +053046#include <sound/compress_params.h>
47#include <sound/compress_offload.h>
Surendar Karka287348c2019-04-10 18:31:46 +053048#include <sound/devdep_params.h>
Manish Dewangan07de2142017-02-27 19:27:20 +053049#include <tinycompress/tinycompress.h>
Revathi Uddaraju1eac8b02017-05-18 17:13:33 +053050
51#ifdef DYNAMIC_LOG_ENABLED
52#include <log_xml_parser.h>
53#define LOG_MASK HAL_MOD_FILE_UTILS
54#include <log_utils.h>
55#endif
56
Ashish Jain81eb2a82015-05-13 10:52:34 +053057#ifdef AUDIO_EXTERNAL_HDMI_ENABLED
Ashish Jain81eb2a82015-05-13 10:52:34 +053058#include "audio_parsers.h"
59#endif
Ashish Jain81eb2a82015-05-13 10:52:34 +053060
Roopesh Nataraja0f68be72020-04-28 12:32:02 -070061#define AUDIO_IO_POLICY_VENDOR_CONFIG_FILE_NAME "audio_io_policy.conf"
62#define AUDIO_OUTPUT_POLICY_VENDOR_CONFIG_FILE_NAME "audio_output_policy.conf"
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070063
64#define OUTPUTS_TAG "outputs"
Dhananjay Kumard6d32152016-10-13 16:11:03 +053065#define INPUTS_TAG "inputs"
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070066
67#define DYNAMIC_VALUE_TAG "dynamic"
68#define FLAGS_TAG "flags"
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +053069#define PROFILES_TAG "profile"
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070070#define FORMATS_TAG "formats"
71#define SAMPLING_RATES_TAG "sampling_rates"
72#define BIT_WIDTH_TAG "bit_width"
73#define APP_TYPE_TAG "app_type"
74
75#define STRING_TO_ENUM(string) { #string, string }
76#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
77
Ben Rombergera04fabc2014-11-14 12:16:03 -080078#define BASE_TABLE_SIZE 64
79#define MAX_BASEINDEX_LEN 256
80
Ben Romberger1aaaf862017-04-06 17:49:46 -070081#ifndef SND_AUDIOCODEC_TRUEHD
82#define SND_AUDIOCODEC_TRUEHD 0x00000023
83#endif
84
Vikram Panduranga93f080e2017-06-07 18:16:14 -070085#define APP_TYPE_VOIP_AUDIO 0x1113A
86
Ashish Jain81eb2a82015-05-13 10:52:34 +053087#ifdef AUDIO_EXTERNAL_HDMI_ENABLED
88#define PROFESSIONAL (1<<0) /* 0 = consumer, 1 = professional */
89#define NON_LPCM (1<<1) /* 0 = audio, 1 = non-audio */
90#define SR_44100 (0<<0) /* 44.1kHz */
91#define SR_NOTID (1<<0) /* non indicated */
92#define SR_48000 (2<<0) /* 48kHz */
93#define SR_32000 (3<<0) /* 32kHz */
94#define SR_22050 (4<<0) /* 22.05kHz */
95#define SR_24000 (6<<0) /* 24kHz */
96#define SR_88200 (8<<0) /* 88.2kHz */
97#define SR_96000 (10<<0) /* 96kHz */
98#define SR_176400 (12<<0) /* 176.4kHz */
99#define SR_192000 (14<<0) /* 192kHz */
100
101#endif
Manish Dewangan07de2142017-02-27 19:27:20 +0530102
103/* ToDo: Check and update a proper value in msec */
Weiyin Jiangc49a3b52018-08-17 16:16:08 +0800104#define COMPRESS_OFFLOAD_PLAYBACK_LATENCY 50
105#define PCM_OFFLOAD_PLAYBACK_DSP_PATHDELAY 62
Samyak Jain2546f8a2019-08-29 19:24:21 +0530106#define PCM_OFFLOAD_PLAYBACK_LATENCY PCM_OFFLOAD_PLAYBACK_DSP_PATHDELAY
Manish Dewangan07de2142017-02-27 19:27:20 +0530107
Varun Balaraje49253e2017-07-06 19:48:56 +0530108#ifndef MAX_CHANNELS_SUPPORTED
109#define MAX_CHANNELS_SUPPORTED 8
110#endif
111
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -0700112#ifdef __LP64__
113#define VNDK_FWK_LIB_PATH "/vendor/lib64/libqti_vndfwk_detect.so"
114#else
115#define VNDK_FWK_LIB_PATH "/vendor/lib/libqti_vndfwk_detect.so"
116#endif
117
Krishna Kishor Jhac1542b22022-08-22 13:12:09 +0530118#ifdef PLATFORM_AUTO
Kogara Naveen Kumar51bb57d2022-02-17 16:40:06 +0530119/* 24 KHz ECNR support */
120#define ECNS_USE_CASE_ACDB_DEV_ID 95
121#define ECNS_UNSUPPORTED_CAPTURE_SAMPLE_RATE_FOR_ADM 24000
122#define ECNS_SUPPORTED_CAPTURE_SAMPLE_RATE_FOR_ADM 48000
Krishna Kishor Jhac1542b22022-08-22 13:12:09 +0530123#endif
Kogara Naveen Kumar51bb57d2022-02-17 16:40:06 +0530124
Weiyin Jiangd5718bd2019-09-04 16:52:08 +0800125typedef struct vndkfwk_s {
126 void *lib_handle;
127 int (*isVendorEnhancedFwk)(void);
128 int (*getVendorEnhancedInfo)(void);
129 const char *lib_name;
130} vndkfwk_t;
131
132static vndkfwk_t mVndkFwk = {
133 NULL, NULL, NULL, VNDK_FWK_LIB_PATH};
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -0700134
`Deeraj Soman676c2702017-09-18 19:25:53 +0530135typedef struct {
136 const char *id_string;
137 const int value;
138} mixer_config_lookup;
139
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700140struct string_to_enum {
141 const char *name;
142 uint32_t value;
143};
144
145const struct string_to_enum s_flag_name_to_enum_table[] = {
146 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_DIRECT),
147 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_PRIMARY),
148 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_FAST),
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800149 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_RAW),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700150 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_DEEP_BUFFER),
151 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD),
152 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_NON_BLOCKING),
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700153 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_HW_AV_SYNC),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700154 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_INCALL_MUSIC),
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700155 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH),
Naresh Tanniruee3499a2017-01-05 14:05:35 +0530156 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_TIMESTAMP),
Vikram Panduranga93f080e2017-06-07 18:16:14 -0700157 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_VOIP_RX),
Ben Romberger6c4d3812017-06-13 17:46:45 -0700158 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_BD),
Varun Balaraje49253e2017-07-06 19:48:56 +0530159 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_INTERACTIVE),
Derek Chen090dfc52018-03-22 22:15:29 -0400160 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_MEDIA),
161 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_SYS_NOTIFICATION),
162 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_NAV_GUIDANCE),
163 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_PHONE),
Susan Wang117cf6f2022-04-06 20:11:46 -0400164 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_ALERTS),
Derek Chen1bcdc6b2020-02-06 22:44:53 -0800165 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_FRONT_PASSENGER),
Derek Chendf05eea2019-08-01 13:57:49 -0700166 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_REAR_SEAT),
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530167 STRING_TO_ENUM(AUDIO_INPUT_FLAG_NONE),
168 STRING_TO_ENUM(AUDIO_INPUT_FLAG_FAST),
169 STRING_TO_ENUM(AUDIO_INPUT_FLAG_HW_HOTWORD),
170 STRING_TO_ENUM(AUDIO_INPUT_FLAG_RAW),
171 STRING_TO_ENUM(AUDIO_INPUT_FLAG_SYNC),
Dhananjay Kumaree4d2002016-10-25 18:02:58 +0530172 STRING_TO_ENUM(AUDIO_INPUT_FLAG_TIMESTAMP),
Dhananjay Kumar704ce6f2017-09-28 22:08:00 +0530173 STRING_TO_ENUM(AUDIO_INPUT_FLAG_COMPRESS),
Ralf Herzaec80262018-07-03 07:08:49 +0200174 STRING_TO_ENUM(AUDIO_INPUT_FLAG_PASSTHROUGH),
George Gao3d477082020-09-17 22:29:07 -0700175 STRING_TO_ENUM(AUDIO_INPUT_FLAG_MMAP_NOIRQ),
176 STRING_TO_ENUM(AUDIO_INPUT_FLAG_VOIP_TX),
177 STRING_TO_ENUM(AUDIO_INPUT_FLAG_HW_AV_SYNC),
178 STRING_TO_ENUM(AUDIO_INPUT_FLAG_DIRECT),
Huicheng Liu1404ba12020-09-11 01:03:25 -0400179 STRING_TO_ENUM(AUDIO_INPUT_FLAG_PRIMARY),
180 STRING_TO_ENUM(AUDIO_INPUT_FLAG_FRONT_PASSENGER),
181 STRING_TO_ENUM(AUDIO_INPUT_FLAG_REAR_SEAT),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700182};
183
184const struct string_to_enum s_format_name_to_enum_table[] = {
Ashish Jain83a6cc22016-06-28 14:34:17 +0530185 STRING_TO_ENUM(AUDIO_FORMAT_PCM_8_BIT),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700186 STRING_TO_ENUM(AUDIO_FORMAT_PCM_16_BIT),
Ashish Jain5106d362016-05-11 19:23:33 +0530187 STRING_TO_ENUM(AUDIO_FORMAT_PCM_24_BIT_PACKED),
188 STRING_TO_ENUM(AUDIO_FORMAT_PCM_8_24_BIT),
Ashish Jainf1eaa582016-05-23 20:54:24 +0530189 STRING_TO_ENUM(AUDIO_FORMAT_PCM_32_BIT),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700190 STRING_TO_ENUM(AUDIO_FORMAT_MP3),
191 STRING_TO_ENUM(AUDIO_FORMAT_AAC),
192 STRING_TO_ENUM(AUDIO_FORMAT_VORBIS),
Mingming Yinae3530f2014-07-03 16:50:18 -0700193 STRING_TO_ENUM(AUDIO_FORMAT_AMR_NB),
194 STRING_TO_ENUM(AUDIO_FORMAT_AMR_WB),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700195 STRING_TO_ENUM(AUDIO_FORMAT_AC3),
Mingming Yinae3530f2014-07-03 16:50:18 -0700196 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700197 STRING_TO_ENUM(AUDIO_FORMAT_DTS),
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530198 STRING_TO_ENUM(AUDIO_FORMAT_DTS_HD),
Ben Romberger1aaaf862017-04-06 17:49:46 -0700199 STRING_TO_ENUM(AUDIO_FORMAT_DOLBY_TRUEHD),
Naresh Tanniru928f0862017-04-07 16:44:23 -0700200 STRING_TO_ENUM(AUDIO_FORMAT_IEC61937),
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530201 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3_JOC),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700202 STRING_TO_ENUM(AUDIO_FORMAT_WMA),
203 STRING_TO_ENUM(AUDIO_FORMAT_WMA_PRO),
204 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADIF),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700205 STRING_TO_ENUM(AUDIO_FORMAT_AMR_WB_PLUS),
206 STRING_TO_ENUM(AUDIO_FORMAT_EVRC),
207 STRING_TO_ENUM(AUDIO_FORMAT_EVRCB),
208 STRING_TO_ENUM(AUDIO_FORMAT_EVRCWB),
209 STRING_TO_ENUM(AUDIO_FORMAT_QCELP),
210 STRING_TO_ENUM(AUDIO_FORMAT_MP2),
211 STRING_TO_ENUM(AUDIO_FORMAT_EVRCNW),
Amit Shekhar6f461b12014-08-01 14:52:58 -0700212 STRING_TO_ENUM(AUDIO_FORMAT_FLAC),
Satya Krishna Pindiproli70471602015-04-24 19:12:43 +0530213 STRING_TO_ENUM(AUDIO_FORMAT_ALAC),
214 STRING_TO_ENUM(AUDIO_FORMAT_APE),
Alexy Josephcd8eaed2014-12-11 12:46:53 -0800215 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LC),
216 STRING_TO_ENUM(AUDIO_FORMAT_AAC_HE_V1),
217 STRING_TO_ENUM(AUDIO_FORMAT_AAC_HE_V2),
Manish Dewangana6fc5442015-08-24 20:30:31 +0530218 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS),
Ashish Jaine513a872015-11-19 17:00:56 +0530219 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_LC),
220 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_HE_V1),
221 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_HE_V2),
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +0530222 STRING_TO_ENUM(AUDIO_FORMAT_DSD),
Arun Kumar Dasari3b174182016-12-27 13:01:14 +0530223 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM),
224 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM_LC),
225 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM_HE_V1),
226 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM_HE_V2),
Dhanalakshmi Siddani18737932016-11-29 17:33:17 +0530227 STRING_TO_ENUM(AUDIO_FORMAT_APTX),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700228};
229
Manish Dewangan3ccdea52017-02-13 19:31:54 +0530230/* payload structure avt_device drift query */
231struct audio_avt_device_drift_stats {
232 uint32_t minor_version;
Manish Dewangan338c50a2017-09-12 15:22:03 +0530233
Manish Dewangan3ccdea52017-02-13 19:31:54 +0530234 /* Indicates the device interface direction as either
235 * source (Tx) or sink (Rx).
236 */
237 uint16_t device_direction;
Manish Dewangan338c50a2017-09-12 15:22:03 +0530238
239 /* Reference timer for drift accumulation and time stamp information.
240 * currently it only support AFE_REF_TIMER_TYPE_AVTIMER
241 */
242 uint16_t reference_timer;
Manish Dewangan3ccdea52017-02-13 19:31:54 +0530243 struct audio_avt_device_drift_param drift_param;
Manish Dewangan338c50a2017-09-12 15:22:03 +0530244} __attribute__((packed));
Manish Dewangan3ccdea52017-02-13 19:31:54 +0530245
Ben Rombergera04fabc2014-11-14 12:16:03 -0800246static char bTable[BASE_TABLE_SIZE] = {
247 'A','B','C','D','E','F','G','H','I','J','K','L',
248 'M','N','O','P','Q','R','S','T','U','V','W','X',
249 'Y','Z','a','b','c','d','e','f','g','h','i','j',
250 'k','l','m','n','o','p','q','r','s','t','u','v',
251 'w','x','y','z','0','1','2','3','4','5','6','7',
252 '8','9','+','/'
253};
254
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700255static uint32_t string_to_enum(const struct string_to_enum *table, size_t size,
256 const char *name)
257{
258 size_t i;
259 for (i = 0; i < size; i++) {
260 if (strcmp(table[i].name, name) == 0) {
261 ALOGV("%s found %s", __func__, table[i].name);
262 return table[i].value;
263 }
264 }
George Gao3d477082020-09-17 22:29:07 -0700265 ALOGE("%s cound not find %s", __func__, name);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700266 return 0;
267}
268
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530269static audio_io_flags_t parse_flag_names(char *name)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700270{
271 uint32_t flag = 0;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530272 audio_io_flags_t io_flags;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800273 char *last_r;
274 char *flag_name = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700275 while (flag_name != NULL) {
276 if (strlen(flag_name) != 0) {
277 flag |= string_to_enum(s_flag_name_to_enum_table,
278 ARRAY_SIZE(s_flag_name_to_enum_table),
279 flag_name);
280 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800281 flag_name = strtok_r(NULL, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700282 }
283
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800284 ALOGV("parse_flag_names: flag - %x", flag);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530285 io_flags.in_flags = (audio_input_flags_t)flag;
286 io_flags.out_flags = (audio_output_flags_t)flag;
287 return io_flags;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700288}
289
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530290static void parse_format_names(char *name, struct streams_io_cfg *s_info)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700291{
292 struct stream_format *sf_info = NULL;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800293 char *last_r;
294 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700295
296 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG) == 0)
297 return;
298
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530299 list_init(&s_info->format_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700300 while (str != NULL) {
301 audio_format_t format = (audio_format_t)string_to_enum(s_format_name_to_enum_table,
302 ARRAY_SIZE(s_format_name_to_enum_table), str);
303 ALOGV("%s: format - %d", __func__, format);
304 if (format != 0) {
305 sf_info = (struct stream_format *)calloc(1, sizeof(struct stream_format));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700306 if (sf_info == NULL)
307 break; /* return whatever was parsed */
308
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700309 sf_info->format = format;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530310 list_add_tail(&s_info->format_list, &sf_info->list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700311 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800312 str = strtok_r(NULL, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700313 }
314}
315
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530316static void parse_sample_rate_names(char *name, struct streams_io_cfg *s_info)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700317{
Amit Shekhar6f461b12014-08-01 14:52:58 -0700318 struct stream_sample_rate *ss_info = NULL;
319 uint32_t sample_rate = 48000;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800320 char *last_r;
321 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700322
Amit Shekhar6f461b12014-08-01 14:52:58 -0700323 if (str != NULL && 0 == strcmp(str, DYNAMIC_VALUE_TAG))
324 return;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700325
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530326 list_init(&s_info->sample_rate_list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700327 while (str != NULL) {
328 sample_rate = (uint32_t)strtol(str, (char **)NULL, 10);
329 ALOGV("%s: sample_rate - %d", __func__, sample_rate);
330 if (0 != sample_rate) {
331 ss_info = (struct stream_sample_rate *)calloc(1, sizeof(struct stream_sample_rate));
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800332 if (!ss_info) {
333 ALOGE("%s: memory allocation failure", __func__);
334 return;
335 }
Amit Shekhar6f461b12014-08-01 14:52:58 -0700336 ss_info->sample_rate = sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530337 list_add_tail(&s_info->sample_rate_list, &ss_info->list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700338 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800339 str = strtok_r(NULL, "|", &last_r);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700340 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700341}
342
343static int parse_bit_width_names(char *name)
344{
345 int bit_width = 16;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800346 char *last_r;
347 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700348
349 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG))
350 bit_width = (int)strtol(str, (char **)NULL, 10);
351
352 ALOGV("%s: bit_width - %d", __func__, bit_width);
353 return bit_width;
354}
355
356static int parse_app_type_names(void *platform, char *name)
357{
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700358 int app_type = platform_get_default_app_type(platform);
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800359 char *last_r;
360 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700361
362 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG))
363 app_type = (int)strtol(str, (char **)NULL, 10);
364
365 ALOGV("%s: app_type - %d", __func__, app_type);
366 return app_type;
367}
368
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530369static void update_streams_cfg_list(cnode *root, void *platform,
370 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700371{
372 cnode *node = root->first_child;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530373 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700374
375 ALOGV("%s", __func__);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530376 s_info = (struct streams_io_cfg *)calloc(1, sizeof(struct streams_io_cfg));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700377
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530378 if (!s_info) {
379 ALOGE("failed to allocate mem for s_info list element");
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700380 return;
381 }
382
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700383 while (node) {
384 if (strcmp(node->name, FLAGS_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530385 s_info->flags = parse_flag_names((char *)node->value);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530386 } else if (strcmp(node->name, PROFILES_TAG) == 0) {
387 strlcpy(s_info->profile, (char *)node->value, sizeof(s_info->profile));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700388 } else if (strcmp(node->name, FORMATS_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530389 parse_format_names((char *)node->value, s_info);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700390 } else if (strcmp(node->name, SAMPLING_RATES_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530391 s_info->app_type_cfg.sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
392 parse_sample_rate_names((char *)node->value, s_info);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700393 } else if (strcmp(node->name, BIT_WIDTH_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530394 s_info->app_type_cfg.bit_width = parse_bit_width_names((char *)node->value);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700395 } else if (strcmp(node->name, APP_TYPE_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530396 s_info->app_type_cfg.app_type = parse_app_type_names(platform, (char *)node->value);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700397 }
398 node = node->next;
399 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530400 list_add_tail(streams_cfg_list, &s_info->list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700401}
402
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530403static void load_cfg_list(cnode *root, void *platform,
404 struct listnode *streams_output_cfg_list,
405 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700406{
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530407 cnode *node = NULL;
408
409 node = config_find(root, OUTPUTS_TAG);
410 if (node != NULL) {
411 node = node->first_child;
412 while (node) {
413 ALOGV("%s: loading output %s", __func__, node->name);
414 update_streams_cfg_list(node, platform, streams_output_cfg_list);
415 node = node->next;
416 }
417 } else {
418 ALOGI("%s: could not load output, node is NULL", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700419 }
420
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530421 node = config_find(root, INPUTS_TAG);
422 if (node != NULL) {
423 node = node->first_child;
424 while (node) {
425 ALOGV("%s: loading input %s", __func__, node->name);
426 update_streams_cfg_list(node, platform, streams_input_cfg_list);
427 node = node->next;
428 }
429 } else {
430 ALOGI("%s: could not load input, node is NULL", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700431 }
432}
433
434static void send_app_type_cfg(void *platform, struct mixer *mixer,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530435 struct listnode *streams_output_cfg_list,
436 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700437{
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530438 size_t app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT] = {0};
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700439 int length = 0, i, num_app_types = 0;
440 struct listnode *node;
441 bool update;
442 struct mixer_ctl *ctl = NULL;
443 const char *mixer_ctl_name = "App Type Config";
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530444 struct streams_io_cfg *s_info = NULL;
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800445 uint32_t target_bit_width = 0;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700446
447 if (!mixer) {
448 ALOGE("%s: mixer is null",__func__);
449 return;
450 }
451 ctl = mixer_get_ctl_by_name(mixer, mixer_ctl_name);
452 if (!ctl) {
453 ALOGE("%s: Could not get ctl for mixer cmd - %s",__func__, mixer_ctl_name);
454 return;
455 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530456 app_type_cfg[length++] = num_app_types;
457
458 if (list_empty(streams_output_cfg_list)) {
459 app_type_cfg[length++] = platform_get_default_app_type_v2(platform, PCM_PLAYBACK);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700460 app_type_cfg[length++] = 48000;
461 app_type_cfg[length++] = 16;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530462 num_app_types += 1;
463 }
464 if (list_empty(streams_input_cfg_list)) {
465 app_type_cfg[length++] = platform_get_default_app_type_v2(platform, PCM_CAPTURE);
466 app_type_cfg[length++] = 48000;
467 app_type_cfg[length++] = 16;
468 num_app_types += 1;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700469 }
470
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800471 /* get target bit width for ADM enforce mode */
472 target_bit_width = adev_get_dsp_bit_width_enforce_mode();
473
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700474 list_for_each(node, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530475 s_info = node_to_item(node, struct streams_io_cfg, list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700476 update = true;
477 for (i=0; i<length; i=i+3) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530478 if (app_type_cfg[i+1] == 0)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700479 break;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530480 else if (app_type_cfg[i+1] == (size_t)s_info->app_type_cfg.app_type) {
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530481 if (app_type_cfg[i+2] < (size_t)s_info->app_type_cfg.sample_rate)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530482 app_type_cfg[i+2] = s_info->app_type_cfg.sample_rate;
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530483 if (app_type_cfg[i+3] < (size_t)s_info->app_type_cfg.bit_width)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530484 app_type_cfg[i+3] = s_info->app_type_cfg.bit_width;
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800485 /* ADM bit width = max(enforce_bit_width, bit_width from s_info */
486 if (audio_extn_is_dsp_bit_width_enforce_mode_supported(s_info->flags.out_flags) &&
487 (target_bit_width > app_type_cfg[i+3]))
488 app_type_cfg[i+3] = target_bit_width;
489
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700490 update = false;
491 break;
492 }
493 }
494 if (update && ((length + 3) <= MAX_LENGTH_MIXER_CONTROL_IN_INT)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530495 num_app_types += 1;
496 app_type_cfg[length++] = s_info->app_type_cfg.app_type;
497 app_type_cfg[length++] = s_info->app_type_cfg.sample_rate;
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800498 app_type_cfg[length] = s_info->app_type_cfg.bit_width;
499 if (audio_extn_is_dsp_bit_width_enforce_mode_supported(s_info->flags.out_flags) &&
500 (target_bit_width > app_type_cfg[length]))
501 app_type_cfg[length] = target_bit_width;
502
503 length++;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530504 }
505 }
506 list_for_each(node, streams_input_cfg_list) {
507 s_info = node_to_item(node, struct streams_io_cfg, list);
508 update = true;
509 for (i=0; i<length; i=i+3) {
510 if (app_type_cfg[i+1] == 0)
511 break;
512 else if (app_type_cfg[i+1] == (size_t)s_info->app_type_cfg.app_type) {
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530513 if (app_type_cfg[i+2] < (size_t)s_info->app_type_cfg.sample_rate)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530514 app_type_cfg[i+2] = s_info->app_type_cfg.sample_rate;
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530515 if (app_type_cfg[i+3] < (size_t)s_info->app_type_cfg.bit_width)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530516 app_type_cfg[i+3] = s_info->app_type_cfg.bit_width;
517 update = false;
518 break;
519 }
520 }
521 if (update && ((length + 3) <= MAX_LENGTH_MIXER_CONTROL_IN_INT)) {
522 num_app_types += 1;
523 app_type_cfg[length++] = s_info->app_type_cfg.app_type;
524 app_type_cfg[length++] = s_info->app_type_cfg.sample_rate;
525 app_type_cfg[length++] = s_info->app_type_cfg.bit_width;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700526 }
527 }
528 ALOGV("%s: num_app_types: %d", __func__, num_app_types);
529 if (num_app_types) {
530 app_type_cfg[0] = num_app_types;
531 mixer_ctl_set_array(ctl, app_type_cfg, length);
532 }
533}
534
Roopesh Nataraja0f68be72020-04-28 12:32:02 -0700535/* Function to retrieve audio vendor configs path */
536void audio_get_vendor_config_path (char* config_file_path, int path_size)
537{
538 char vendor_sku[PROPERTY_VALUE_MAX] = {'\0'};
539 if (property_get("ro.boot.product.vendor.sku", vendor_sku, "") <= 0) {
540#ifdef LINUX_ENABLED
541 /* Audio configs are stored in /etc */
542 snprintf(config_file_path, path_size, "%s", "/etc");
543#else
544 /* Audio configs are stored in /vendor/etc */
545 snprintf(config_file_path, path_size, "%s", "/vendor/etc");
546#endif
547 } else {
548 /* Audio configs are stored in /vendor/etc/audio/sku_${vendor_sku} */
549 snprintf(config_file_path, path_size,
550 "%s%s", "/vendor/etc/audio/sku_", vendor_sku);
551 }
552}
553
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530554void audio_extn_utils_update_streams_cfg_lists(void *platform,
555 struct mixer *mixer,
556 struct listnode *streams_output_cfg_list,
557 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700558{
559 cnode *root;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530560 char *data = NULL;
Roopesh Nataraja0f68be72020-04-28 12:32:02 -0700561 char vendor_config_path[VENDOR_CONFIG_PATH_MAX_LENGTH];
562 char audio_io_policy_file[VENDOR_CONFIG_FILE_MAX_LENGTH];
563 char audio_output_policy_file[VENDOR_CONFIG_FILE_MAX_LENGTH];
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700564
565 ALOGV("%s", __func__);
566 list_init(streams_output_cfg_list);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530567 list_init(streams_input_cfg_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700568
569 root = config_node("", "");
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700570 if (root == NULL) {
571 ALOGE("cfg_list, NULL config root");
572 return;
573 }
574
Roopesh Nataraja0f68be72020-04-28 12:32:02 -0700575 /* Get path for audio configuration files in vendor */
576 audio_get_vendor_config_path(vendor_config_path,
577 sizeof(vendor_config_path));
578
579 /* Get path for audio_io_policy_file in vendor */
580 snprintf(audio_io_policy_file, sizeof(audio_io_policy_file),
581 "%s/%s", vendor_config_path, AUDIO_IO_POLICY_VENDOR_CONFIG_FILE_NAME);
582
583 /* Load audio_io_policy_file from vendor */
584 data = (char *)load_file(audio_io_policy_file, NULL);
585
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530586 if (data == NULL) {
587 ALOGD("%s: failed to open io config file(%s), trying older config file",
Roopesh Nataraja0f68be72020-04-28 12:32:02 -0700588 __func__, audio_io_policy_file);
589
590 /* Get path for audio_output_policy_file in vendor */
591 snprintf(audio_output_policy_file, sizeof(audio_output_policy_file),
592 "%s/%s", vendor_config_path,
593 AUDIO_OUTPUT_POLICY_VENDOR_CONFIG_FILE_NAME);
594
595 /* Load audio_output_policy_file from vendor */
596 data = (char *)load_file(audio_output_policy_file, NULL);
597
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530598 if (data == NULL) {
599 send_app_type_cfg(platform, mixer,
600 streams_output_cfg_list,
601 streams_input_cfg_list);
602 ALOGE("%s: could not load io policy config!", __func__);
yidongh6eb4f752017-04-19 18:20:57 +0800603 free(root);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530604 return;
605 }
606 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700607
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530608 config_load(root, data);
609 load_cfg_list(root, platform, streams_output_cfg_list,
610 streams_input_cfg_list);
611
612 send_app_type_cfg(platform, mixer, streams_output_cfg_list,
613 streams_input_cfg_list);
Alexy Josephaee4fdd2016-01-29 13:02:07 -0800614
615 config_free(root);
yidongh6eb4f752017-04-19 18:20:57 +0800616 free(root);
Alexy Josephaee4fdd2016-01-29 13:02:07 -0800617 free(data);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700618}
619
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530620static void audio_extn_utils_dump_streams_cfg_list(
621 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700622{
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700623 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530624 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700625 struct stream_format *sf_info;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700626 struct stream_sample_rate *ss_info;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530627
628 list_for_each(node_i, streams_cfg_list) {
629 s_info = node_to_item(node_i, struct streams_io_cfg, list);
630 ALOGV("%s: flags-%d, sample_rate-%d, bit_width-%d, app_type-%d",
631 __func__, s_info->flags.out_flags, s_info->app_type_cfg.sample_rate,
632 s_info->app_type_cfg.bit_width, s_info->app_type_cfg.app_type);
633 list_for_each(node_j, &s_info->format_list) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700634 sf_info = node_to_item(node_j, struct stream_format, list);
635 ALOGV("format-%x", sf_info->format);
636 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530637 list_for_each(node_j, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700638 ss_info = node_to_item(node_j, struct stream_sample_rate, list);
639 ALOGV("sample rate-%d", ss_info->sample_rate);
640 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700641 }
642}
643
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530644void audio_extn_utils_dump_streams_cfg_lists(
645 struct listnode *streams_output_cfg_list,
646 struct listnode *streams_input_cfg_list)
647{
648 ALOGV("%s", __func__);
649 audio_extn_utils_dump_streams_cfg_list(streams_output_cfg_list);
650 audio_extn_utils_dump_streams_cfg_list(streams_input_cfg_list);
651}
652
653static void audio_extn_utils_release_streams_cfg_list(
654 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700655{
656 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530657 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700658
659 ALOGV("%s", __func__);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530660
661 while (!list_empty(streams_cfg_list)) {
662 node_i = list_head(streams_cfg_list);
663 s_info = node_to_item(node_i, struct streams_io_cfg, list);
664 while (!list_empty(&s_info->format_list)) {
665 node_j = list_head(&s_info->format_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700666 list_remove(node_j);
667 free(node_to_item(node_j, struct stream_format, list));
668 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530669 while (!list_empty(&s_info->sample_rate_list)) {
670 node_j = list_head(&s_info->sample_rate_list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700671 list_remove(node_j);
672 free(node_to_item(node_j, struct stream_sample_rate, list));
673 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700674 list_remove(node_i);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530675 free(node_to_item(node_i, struct streams_io_cfg, list));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700676 }
677}
678
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530679void audio_extn_utils_release_streams_cfg_lists(
680 struct listnode *streams_output_cfg_list,
681 struct listnode *streams_input_cfg_list)
682{
683 ALOGV("%s", __func__);
684 audio_extn_utils_release_streams_cfg_list(streams_output_cfg_list);
685 audio_extn_utils_release_streams_cfg_list(streams_input_cfg_list);
686}
687
688static bool set_app_type_cfg(struct streams_io_cfg *s_info,
689 struct stream_app_type_cfg *app_type_cfg,
690 uint32_t sample_rate, uint32_t bit_width)
Amit Shekhar6f461b12014-08-01 14:52:58 -0700691 {
692 struct listnode *node_i;
693 struct stream_sample_rate *ss_info;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530694 list_for_each(node_i, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700695 ss_info = node_to_item(node_i, struct stream_sample_rate, list);
696 if ((sample_rate <= ss_info->sample_rate) &&
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530697 (bit_width == s_info->app_type_cfg.bit_width)) {
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700698
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530699 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700700 app_type_cfg->sample_rate = ss_info->sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530701 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700702 ALOGV("%s app_type_cfg->app_type %d, app_type_cfg->sample_rate %d, app_type_cfg->bit_width %d",
703 __func__, app_type_cfg->app_type, app_type_cfg->sample_rate, app_type_cfg->bit_width);
704 return true;
705 }
706 }
707 /*
708 * Reiterate through the list assuming dafault sample rate.
709 * Handles scenario where input sample rate is higher
710 * than all sample rates in list for the input bit width.
711 */
712 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
Apoorv Raghuvanshif59bb222015-02-18 12:23:23 -0800713
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530714 list_for_each(node_i, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700715 ss_info = node_to_item(node_i, struct stream_sample_rate, list);
716 if ((sample_rate <= ss_info->sample_rate) &&
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530717 (bit_width == s_info->app_type_cfg.bit_width)) {
718 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700719 app_type_cfg->sample_rate = sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530720 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Apoorv Raghuvanshif59bb222015-02-18 12:23:23 -0800721 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 -0700722 __func__, app_type_cfg->app_type, app_type_cfg->sample_rate, app_type_cfg->bit_width);
723 return true;
724 }
725 }
726 return false;
727}
728
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530729void audio_extn_utils_update_stream_input_app_type_cfg(void *platform,
730 struct listnode *streams_input_cfg_list,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -0800731 struct listnode *devices __unused,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530732 audio_input_flags_t flags,
733 audio_format_t format,
734 uint32_t sample_rate,
735 uint32_t bit_width,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530736 char* profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530737 struct stream_app_type_cfg *app_type_cfg)
738{
739 struct listnode *node_i, *node_j;
740 struct streams_io_cfg *s_info;
741 struct stream_format *sf_info;
742
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530743 ALOGV("%s: flags: 0x%x, format: 0x%x sample_rate %d, profile %s",
744 __func__, flags, format, sample_rate, profile);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530745
746 list_for_each(node_i, streams_input_cfg_list) {
747 s_info = node_to_item(node_i, struct streams_io_cfg, list);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530748 /* Along with flags do profile matching if set at either end.*/
749 if (s_info->flags.in_flags == flags &&
750 ((profile[0] == '\0' && s_info->profile[0] == '\0') ||
751 strncmp(s_info->profile, profile, sizeof(s_info->profile)) == 0)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530752 list_for_each(node_j, &s_info->format_list) {
753 sf_info = node_to_item(node_j, struct stream_format, list);
754 if (sf_info->format == format) {
755 if (set_app_type_cfg(s_info, app_type_cfg, sample_rate, bit_width))
756 return;
757 }
758 }
759 }
760 }
761 ALOGW("%s: App type could not be selected. Falling back to default", __func__);
762 app_type_cfg->app_type = platform_get_default_app_type_v2(platform, PCM_CAPTURE);
763 app_type_cfg->sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
764 app_type_cfg->bit_width = 16;
Krishna Kishor Jhac1542b22022-08-22 13:12:09 +0530765#ifdef PLATFORM_AUTO
Kogara Naveen Kumar51bb57d2022-02-17 16:40:06 +0530766 if ((flags & AUDIO_INPUT_FLAG_TIMESTAMP) == 0 &&
767 (flags & AUDIO_INPUT_FLAG_COMPRESS) == 0 &&
768 (flags & AUDIO_INPUT_FLAG_FAST) != 0) {
769 // Support low latency record for different sample rates
770 app_type_cfg->sample_rate = sample_rate;
771 }
Krishna Kishor Jhac1542b22022-08-22 13:12:09 +0530772#endif
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530773}
774
775void audio_extn_utils_update_stream_output_app_type_cfg(void *platform,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700776 struct listnode *streams_output_cfg_list,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -0800777 struct listnode *devices,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700778 audio_output_flags_t flags,
779 audio_format_t format,
Amit Shekhar6f461b12014-08-01 14:52:58 -0700780 uint32_t sample_rate,
781 uint32_t bit_width,
Manish Dewangan837dc462015-05-27 10:17:41 +0530782 audio_channel_mask_t channel_mask,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530783 char *profile,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700784 struct stream_app_type_cfg *app_type_cfg)
785{
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +0530786 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530787 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700788 struct stream_format *sf_info;
Manish Dewangan837dc462015-05-27 10:17:41 +0530789 char value[PROPERTY_VALUE_MAX] = {0};
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700790
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -0800791 if (compare_device_type(devices, AUDIO_DEVICE_OUT_SPEAKER)) {
Ramu Gottipati074fa4d2018-09-11 20:05:51 +0530792 int bw = platform_get_snd_device_bit_width(SND_DEVICE_OUT_SPEAKER);
793 if ((-ENOSYS != bw) && (bit_width > (uint32_t)bw))
Amit Shekhar5a39c912014-10-14 15:39:30 -0700794 bit_width = (uint32_t)bw;
Ramu Gottipati074fa4d2018-09-11 20:05:51 +0530795 else if (-ENOSYS == bw)
796 bit_width = CODEC_BACKEND_DEFAULT_BIT_WIDTH;
Amit Shekhar1d896042014-10-03 13:16:09 -0700797 sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Aniket Kumar Latad13758f2020-08-06 15:11:36 -0700798 ALOGV("%s Allowing 24 and above bits playback on speaker \
799 ONLY at default sampling rate", __func__);
Amit Shekhar1d896042014-10-03 13:16:09 -0700800 }
801
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -0700802 property_get("vendor.audio.playback.mch.downsample",value,"");
Manish Dewangan837dc462015-05-27 10:17:41 +0530803 if (!strncmp("true", value, sizeof("true"))) {
804 if ((popcount(channel_mask) > 2) &&
805 (sample_rate > CODEC_BACKEND_DEFAULT_SAMPLE_RATE) &&
Aalique Grahame5cd12ff2017-10-19 10:57:00 -0700806 !(flags & (audio_output_flags_t)AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH)) {
Manish Dewangan837dc462015-05-27 10:17:41 +0530807 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
808 ALOGD("%s: MCH session defaulting sample rate to %d",
809 __func__, sample_rate);
810 }
811 }
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +0530812
813 /* Set sampling rate to 176.4 for DSD64
814 * and 352.8Khz for DSD128.
815 * Set Bit Width to 16. output will be 16 bit
816 * post DoP in ASM.
817 */
Aalique Grahame5cd12ff2017-10-19 10:57:00 -0700818 if ((flags & (audio_output_flags_t)AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH) &&
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +0530819 (format == AUDIO_FORMAT_DSD)) {
820 bit_width = 16;
821 if (sample_rate == INPUT_SAMPLING_RATE_DSD64)
822 sample_rate = OUTPUT_SAMPLING_RATE_DSD64;
823 else if (sample_rate == INPUT_SAMPLING_RATE_DSD128)
824 sample_rate = OUTPUT_SAMPLING_RATE_DSD128;
825 }
826
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +0530827
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800828 ALOGV("%s: flags: %x, format: %x sample_rate %d, profile %s, app_type %d",
829 __func__, flags, format, sample_rate, profile, app_type_cfg->app_type);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700830 list_for_each(node_i, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530831 s_info = node_to_item(node_i, struct streams_io_cfg, list);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530832 /* Along with flags do profile matching if set at either end.*/
833 if (s_info->flags.out_flags == flags &&
834 ((profile[0] == '\0' && s_info->profile[0] == '\0') ||
835 strncmp(s_info->profile, profile, sizeof(s_info->profile)) == 0)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530836 list_for_each(node_j, &s_info->format_list) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700837 sf_info = node_to_item(node_j, struct stream_format, list);
838 if (sf_info->format == format) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530839 if (set_app_type_cfg(s_info, app_type_cfg, sample_rate, bit_width))
Amit Shekhar6f461b12014-08-01 14:52:58 -0700840 return;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700841 }
842 }
843 }
844 }
845 list_for_each(node_i, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530846 s_info = node_to_item(node_i, struct streams_io_cfg, list);
847 if (s_info->flags.out_flags == AUDIO_OUTPUT_FLAG_PRIMARY) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700848 ALOGV("Compatible output profile not found.");
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530849 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
850 app_type_cfg->sample_rate = s_info->app_type_cfg.sample_rate;
851 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700852 ALOGV("%s Default to primary output: App type: %d sample_rate %d",
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530853 __func__, s_info->app_type_cfg.app_type, app_type_cfg->sample_rate);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700854 return;
855 }
856 }
857 ALOGW("%s: App type could not be selected. Falling back to default", __func__);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700858 app_type_cfg->app_type = platform_get_default_app_type(platform);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700859 app_type_cfg->sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700860 app_type_cfg->bit_width = 16;
Kogara Naveen Kumar51bb57d2022-02-17 16:40:06 +0530861 if (compare_device_type(devices, AUDIO_DEVICE_OUT_BUS) && (flags &
862 (audio_output_flags_t)AUDIO_OUTPUT_FLAG_FAST)) {
863 // Support low latency playback for different sample rates
864 app_type_cfg->sample_rate = sample_rate;
865 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700866}
867
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +0530868static bool audio_is_this_native_usecase(struct audio_usecase *uc)
869{
870 bool native_usecase = false;
871 struct stream_out *out = (struct stream_out*) uc->stream.out;
872
873 if (PCM_PLAYBACK == uc->type && out != NULL &&
874 NATIVE_AUDIO_MODE_INVALID != platform_get_native_support() &&
875 is_offload_usecase(uc->id) &&
876 (out->sample_rate == OUTPUT_SAMPLING_RATE_44100))
877 native_usecase = true;
878
879 return native_usecase;
880}
881
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800882bool audio_extn_is_dsp_bit_width_enforce_mode_supported(audio_output_flags_t flags)
883{
884 /* DSP bitwidth enforce mode for ADM and AFE:
885 * includes:
886 * deep buffer, low latency, direct pcm and offload.
887 * excludes:
888 * ull(raw+fast), VOIP.
889 */
890 if ((flags & AUDIO_OUTPUT_FLAG_VOIP_RX) ||
891 ((flags & AUDIO_OUTPUT_FLAG_RAW) &&
892 (flags & AUDIO_OUTPUT_FLAG_FAST)))
893 return false;
894
895
896 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) ||
897 (flags & AUDIO_OUTPUT_FLAG_DIRECT) ||
898 (flags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) ||
899 (flags & AUDIO_OUTPUT_FLAG_PRIMARY))
900 return true;
901 else
902 return false;
903}
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800904
905static inline bool audio_is_vr_mode_on(struct audio_device *(__attribute__((unused)) adev))
906{
907 return adev->vr_audio_mode_enabled;
908}
909
Derek Chenf7092792017-05-23 12:23:53 -0400910static void audio_extn_btsco_get_sample_rate(int snd_device, int *sample_rate)
911{
912 switch (snd_device) {
913 case SND_DEVICE_OUT_BT_SCO:
914 case SND_DEVICE_IN_BT_SCO_MIC:
915 case SND_DEVICE_IN_BT_SCO_MIC_NREC:
916 *sample_rate = 8000;
917 break;
918 case SND_DEVICE_OUT_BT_SCO_WB:
919 case SND_DEVICE_IN_BT_SCO_MIC_WB:
920 case SND_DEVICE_IN_BT_SCO_MIC_WB_NREC:
921 *sample_rate = 16000;
922 break;
923 default:
Aniket Kumar Latad13758f2020-08-06 15:11:36 -0700924 ALOGV("%s:Not a BT SCO device, need not update sampling rate\n", __func__);
Derek Chenf7092792017-05-23 12:23:53 -0400925 break;
926 }
927}
928
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530929void audio_extn_utils_update_stream_app_type_cfg_for_usecase(
930 struct audio_device *adev,
931 struct audio_usecase *usecase)
932{
933 ALOGV("%s", __func__);
934
935 switch(usecase->type) {
936 case PCM_PLAYBACK:
937 audio_extn_utils_update_stream_output_app_type_cfg(adev->platform,
938 &adev->streams_output_cfg_list,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -0800939 &usecase->stream.out->device_list,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530940 usecase->stream.out->flags,
Aalique Grahame65780b52017-09-27 14:59:56 -0700941 usecase->stream.out->hal_op_format,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530942 usecase->stream.out->sample_rate,
943 usecase->stream.out->bit_width,
944 usecase->stream.out->channel_mask,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530945 usecase->stream.out->profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530946 &usecase->stream.out->app_type_cfg);
947 ALOGV("%s Selected apptype: %d", __func__, usecase->stream.out->app_type_cfg.app_type);
948 break;
949 case PCM_CAPTURE:
Vikram Panduranga93f080e2017-06-07 18:16:14 -0700950 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 &
Kogara Naveen Kumare88ee942022-12-22 17:01:12 +05301371 (audio_output_flags_t)AUDIO_OUTPUT_FLAG_PHONE) || (usecase->stream.out->flags &
1372 (audio_output_flags_t)AUDIO_OUTPUT_FLAG_NAV_GUIDANCE) || (usecase->stream.out->flags &
1373 (audio_output_flags_t)AUDIO_OUTPUT_FLAG_ALERTS)))) {
Kogara Naveen Kumar51bb57d2022-02-17 16:40:06 +05301374 /* Reset to default if no native stream is active or default device is speaker*/
1375 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
1376 }
Zhou Song06761dd2019-04-28 18:08:17 +08001377 }
1378 audio_extn_btsco_get_sample_rate(snd_device, &usecase->stream.out->app_type_cfg.sample_rate);
1379 sample_rate = usecase->stream.out->app_type_cfg.sample_rate;
1380
1381 if (((usecase->stream.out->format == AUDIO_FORMAT_E_AC3) ||
1382 (usecase->stream.out->format == AUDIO_FORMAT_E_AC3_JOC) ||
1383 (usecase->stream.out->format == AUDIO_FORMAT_DOLBY_TRUEHD))
1384 && audio_extn_passthru_is_passthrough_stream(usecase->stream.out)
1385 && !audio_extn_passthru_is_convert_supported(adev, usecase->stream.out)) {
1386 sample_rate = sample_rate * 4;
1387 if (sample_rate > HDMI_PASSTHROUGH_MAX_SAMPLE_RATE)
1388 sample_rate = HDMI_PASSTHROUGH_MAX_SAMPLE_RATE;
1389 }
1390 } else if (usecase->type == PCM_CAPTURE) {
1391 if (usecase->stream.in != NULL) {
Revathi Uddarajud9f23d92020-07-27 10:55:06 +05301392 if (usecase->id == USECASE_AUDIO_RECORD_VOIP
1393 || usecase->id == USECASE_AUDIO_RECORD_VOIP_LOW_LATENCY)
Zhou Song06761dd2019-04-28 18:08:17 +08001394 usecase->stream.in->app_type_cfg.sample_rate = usecase->stream.in->sample_rate;
1395 if (voice_is_in_call_rec_stream(usecase->stream.in)) {
1396 audio_extn_btsco_get_sample_rate(usecase->in_snd_device,
1397 &usecase->stream.in->app_type_cfg.sample_rate);
Zhou Songab94f062021-05-12 13:11:17 +08001398 } if (SND_DEVICE_IN_BT_A2DP == snd_device) {
1399 audio_extn_a2dp_get_dec_sample_rate(&usecase->stream.in->app_type_cfg.sample_rate);
Zhou Song06761dd2019-04-28 18:08:17 +08001400 } else {
1401 audio_extn_btsco_get_sample_rate(snd_device,
1402 &usecase->stream.in->app_type_cfg.sample_rate);
1403 }
1404 sample_rate = usecase->stream.in->app_type_cfg.sample_rate;
1405 } else if (usecase->id == USECASE_AUDIO_SPKR_CALIB_TX) {
Sujin Panickerb6d76262019-09-09 10:55:38 +05301406 if ((property_get("vendor.audio.spkr_prot.tx.sampling_rate", value, NULL) > 0))
1407 sample_rate = atoi(value);
1408 else
1409 sample_rate = SAMPLE_RATE_8000;
Zhou Song06761dd2019-04-28 18:08:17 +08001410 }
Kogara Naveen Kumar51bb57d2022-02-17 16:40:06 +05301411
1412 /* ECNR module in DSP does not support 24 KHz sample rate. As a workaround,
1413 run ADM at 48 KHz when ECNR is enabled in ACDB topology (e.g. device id = 95)
1414 */
Krishna Kishor Jhac1542b22022-08-22 13:12:09 +05301415#ifdef PLATFORM_AUTO
Kogara Naveen Kumar51bb57d2022-02-17 16:40:06 +05301416 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
1417 if (sample_rate == ECNS_UNSUPPORTED_CAPTURE_SAMPLE_RATE_FOR_ADM && acdb_dev_id == ECNS_USE_CASE_ACDB_DEV_ID) {
1418 sample_rate = ECNS_SUPPORTED_CAPTURE_SAMPLE_RATE_FOR_ADM;
1419 ALOGD("%s: update sample rate from 24K to 48K to support ECNR in PCM_CAPTURE, sample_rate=%d",__func__,sample_rate);
1420 }
Krishna Kishor Jhac1542b22022-08-22 13:12:09 +05301421#endif
Zhou Song06761dd2019-04-28 18:08:17 +08001422 } else if (usecase->type == TRANSCODE_LOOPBACK_RX) {
1423 sample_rate = usecase->stream.inout->out_config.sample_rate;
1424 }
1425 return sample_rate;
1426}
1427
Siena Richard7c2db772016-12-21 11:32:34 -08001428static int send_app_type_cfg_for_device(struct audio_device *adev,
1429 struct audio_usecase *usecase,
1430 int split_snd_device)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001431{
1432 char mixer_ctl_name[MAX_LENGTH_MIXER_CONTROL_IN_INT];
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301433 size_t app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT] = {0};
1434 int len = 0, rc;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001435 struct mixer_ctl *ctl;
Siena Richard7c2db772016-12-21 11:32:34 -08001436 int pcm_device_id = 0, acdb_dev_id, app_type;
1437 int snd_device = split_snd_device, snd_device_be_idx = -1;
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +05301438 int32_t sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Varun Balaraje49253e2017-07-06 19:48:56 +05301439 struct streams_io_cfg *s_info = NULL;
1440 struct listnode *node = NULL;
Nikhil Laturkarac4a7492017-08-31 18:27:19 +05301441 int bd_app_type = 0;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001442
Siena Richard7c2db772016-12-21 11:32:34 -08001443 ALOGV("%s: usecase->out_snd_device %s, usecase->in_snd_device %s, split_snd_device %s",
1444 __func__, platform_get_snd_device_name(usecase->out_snd_device),
1445 platform_get_snd_device_name(usecase->in_snd_device),
1446 platform_get_snd_device_name(split_snd_device));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001447
Siddartha Shaik343abc62017-08-08 11:15:25 +05301448 if (usecase->type != PCM_PLAYBACK && usecase->type != PCM_CAPTURE &&
Surendar Karka93cd25a2018-08-28 14:21:37 +05301449 usecase->type != TRANSCODE_LOOPBACK_RX) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301450 ALOGE("%s: not a playback/capture path, no need to cfg app type", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001451 rc = 0;
1452 goto exit_send_app_type_cfg;
1453 }
1454 if ((usecase->id != USECASE_AUDIO_PLAYBACK_DEEP_BUFFER) &&
Vignesh Kulothungan0a2eff02019-07-11 16:30:13 -07001455 (usecase->id != USECASE_AUDIO_PLAYBACK_WITH_HAPTICS) &&
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001456 (usecase->id != USECASE_AUDIO_PLAYBACK_LOW_LATENCY) &&
1457 (usecase->id != USECASE_AUDIO_PLAYBACK_MULTI_CH) &&
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -08001458 (usecase->id != USECASE_AUDIO_PLAYBACK_ULL) &&
Vikram Panduranga93f080e2017-06-07 18:16:14 -07001459 (usecase->id != USECASE_AUDIO_PLAYBACK_VOIP) &&
Surendar Karka93cd25a2018-08-28 14:21:37 +05301460 (usecase->id != USECASE_AUDIO_TRANSCODE_LOOPBACK_RX) &&
Varun Balaraje49253e2017-07-06 19:48:56 +05301461 (!is_interactive_usecase(usecase->id)) &&
Srikanth Uyyala9d551402015-08-25 16:03:42 +05301462 (!is_offload_usecase(usecase->id)) &&
Derek Chenf6318be2017-06-12 17:16:24 -04001463 (usecase->type != PCM_CAPTURE) &&
1464 (!audio_extn_auto_hal_is_bus_device_usecase(usecase->id))) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301465 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 -07001466 rc = 0;
1467 goto exit_send_app_type_cfg;
1468 }
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -08001469
1470 //if VR is active then only send the mixer control
1471 if (usecase->id == USECASE_AUDIO_PLAYBACK_ULL && !audio_is_vr_mode_on(adev)) {
1472 ALOGI("ULL doesnt need sending app type cfg, returning");
1473 rc = 0;
1474 goto exit_send_app_type_cfg;
1475 }
1476
Surendar Karka93cd25a2018-08-28 14:21:37 +05301477 if (usecase->type == PCM_PLAYBACK || usecase->type == TRANSCODE_LOOPBACK_RX) {
Ben Romberger1fafdde2015-09-09 19:43:15 -07001478 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_PLAYBACK);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301479 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
1480 "Audio Stream %d App Type Cfg", pcm_device_id);
Ben Romberger1fafdde2015-09-09 19:43:15 -07001481 } else if (usecase->type == PCM_CAPTURE) {
Ben Romberger1fafdde2015-09-09 19:43:15 -07001482 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_CAPTURE);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301483 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
1484 "Audio Stream Capture %d App Type Cfg", pcm_device_id);
Srikanth Uyyala9d551402015-08-25 16:03:42 +05301485 }
Siena Richard7c2db772016-12-21 11:32:34 -08001486
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001487 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
1488 if (!ctl) {
1489 ALOGE("%s: Could not get ctl for mixer cmd - %s", __func__,
1490 mixer_ctl_name);
1491 rc = -EINVAL;
1492 goto exit_send_app_type_cfg;
1493 }
Aditya Bavanari701a6992017-03-30 19:17:16 +05301494 snd_device = platform_get_spkr_prot_snd_device(snd_device);
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301495 if (voice_is_in_call_rec_stream(usecase->stream.in) && usecase->type == PCM_CAPTURE) {
1496 snd_device_t voice_device = voice_get_incall_rec_snd_device(usecase->in_snd_device);
1497 acdb_dev_id = platform_get_snd_device_acdb_id(voice_device);
1498 ALOGV("acdb id for voice call use case %d", acdb_dev_id);
1499 } else {
1500 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
1501 }
Rohit Kumar1181b292017-01-31 18:07:17 +05301502 if (acdb_dev_id <= 0) {
1503 ALOGE("%s: Couldn't get the acdb dev id", __func__);
1504 rc = -EINVAL;
1505 goto exit_send_app_type_cfg;
1506 }
1507
Siena Richard7c2db772016-12-21 11:32:34 -08001508 snd_device_be_idx = platform_get_snd_device_backend_index(snd_device);
1509 if (snd_device_be_idx < 0) {
1510 ALOGE("%s: Couldn't get the backend index for snd device %s ret=%d",
1511 __func__, platform_get_snd_device_name(snd_device),
1512 snd_device_be_idx);
1513 }
1514
Zhou Song06761dd2019-04-28 18:08:17 +08001515 sample_rate = audio_extn_utils_get_app_sample_rate_for_device(adev, usecase, snd_device);
1516
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301517 if ((usecase->type == PCM_PLAYBACK) && (usecase->stream.out != NULL)) {
Varun Balaraje49253e2017-07-06 19:48:56 +05301518 /* Interactive streams are supported with only direct app type id.
1519 * Get Direct profile app type and use it for interactive streams
1520 */
1521 list_for_each(node, &adev->streams_output_cfg_list) {
1522 s_info = node_to_item(node, struct streams_io_cfg, list);
Aalique Grahame5cd12ff2017-10-19 10:57:00 -07001523 if (s_info->flags.out_flags == (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_BD |
Nikhil Laturkarac4a7492017-08-31 18:27:19 +05301524 AUDIO_OUTPUT_FLAG_DIRECT_PCM |
1525 AUDIO_OUTPUT_FLAG_DIRECT))
1526 bd_app_type = s_info->app_type_cfg.app_type;
Varun Balaraje49253e2017-07-06 19:48:56 +05301527 }
Aalique Grahame5cd12ff2017-10-19 10:57:00 -07001528 if (usecase->stream.out->flags == (audio_output_flags_t)AUDIO_OUTPUT_FLAG_INTERACTIVE)
Nikhil Laturkarac4a7492017-08-31 18:27:19 +05301529 app_type = bd_app_type;
Varun Balaraje49253e2017-07-06 19:48:56 +05301530 else
1531 app_type = usecase->stream.out->app_type_cfg.app_type;
Siena Richard7c2db772016-12-21 11:32:34 -08001532 app_type_cfg[len++] = app_type;
Mingming Yin21854652016-04-13 11:54:02 -07001533 app_type_cfg[len++] = acdb_dev_id;
Naresh Tanniru3a406772017-05-10 13:09:05 -07001534 app_type_cfg[len++] = sample_rate;
1535
Siena Richard7c2db772016-12-21 11:32:34 -08001536 if (snd_device_be_idx > 0)
1537 app_type_cfg[len++] = snd_device_be_idx;
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301538
Siena Richard7c2db772016-12-21 11:32:34 -08001539 ALOGI("%s PLAYBACK app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
1540 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301541
1542 } else if ((usecase->type == PCM_CAPTURE) && (usecase->stream.in != NULL)) {
Siena Richard7c2db772016-12-21 11:32:34 -08001543 app_type = usecase->stream.in->app_type_cfg.app_type;
1544 app_type_cfg[len++] = app_type;
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301545 app_type_cfg[len++] = acdb_dev_id;
Siena Richard7c2db772016-12-21 11:32:34 -08001546 app_type_cfg[len++] = sample_rate;
1547 if (snd_device_be_idx > 0)
1548 app_type_cfg[len++] = snd_device_be_idx;
1549 ALOGI("%s CAPTURE app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
1550 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301551 } else {
Siena Richard7c2db772016-12-21 11:32:34 -08001552 app_type = platform_get_default_app_type_v2(adev->platform, usecase->type);
Surendar Karka93cd25a2018-08-28 14:21:37 +05301553 if(usecase->type == TRANSCODE_LOOPBACK_RX) {
Siddartha Shaik343abc62017-08-08 11:15:25 +05301554 app_type = usecase->stream.inout->out_app_type_cfg.app_type;
1555 }
Siena Richard7c2db772016-12-21 11:32:34 -08001556 app_type_cfg[len++] = app_type;
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301557 app_type_cfg[len++] = acdb_dev_id;
1558 app_type_cfg[len++] = sample_rate;
Siena Richard7c2db772016-12-21 11:32:34 -08001559 if (snd_device_be_idx > 0)
1560 app_type_cfg[len++] = snd_device_be_idx;
1561 ALOGI("%s default app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
1562 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Ashish Jainc02430d2016-01-04 10:42:43 +05301563 }
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301564
Siddartha Shaik343abc62017-08-08 11:15:25 +05301565 if(ctl)
1566 mixer_ctl_set_array(ctl, app_type_cfg, len);
Zhou Songd9bd9302020-08-04 16:34:45 +08001567
1568 /* send app type cfg for haptics */
1569 if (usecase->id == USECASE_AUDIO_PLAYBACK_WITH_HAPTICS) {
1570 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
1571 "Audio Stream %d App Type Cfg", adev->haptic_pcm_device_id );
1572 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
1573 if (!ctl) {
1574 ALOGE("%s: Could not get ctl for mixer cmd - %s", __func__,
1575 mixer_ctl_name);
1576 rc = -EINVAL;
1577 goto exit_send_app_type_cfg;
1578 }
1579 acdb_dev_id = platform_get_snd_device_acdb_id(SND_DEVICE_OUT_HAPTICS);
1580 snd_device_be_idx = platform_get_snd_device_backend_index(SND_DEVICE_OUT_HAPTICS);
1581 /* haptics acdb id */
1582 app_type_cfg[1] = acdb_dev_id;
1583 /* haptics be index */
1584 app_type_cfg[3] = snd_device_be_idx;
1585 mixer_ctl_set_array(ctl, app_type_cfg, len);
1586 }
1587
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001588 rc = 0;
1589exit_send_app_type_cfg:
1590 return rc;
1591}
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001592
Deeraj Soman14230922019-01-30 16:39:30 +05301593static int audio_extn_utils_check_input_parameters(uint32_t sample_rate,
1594 audio_format_t format,
1595 int channel_count)
1596{
1597 int ret = 0;
1598
1599 if (((format != AUDIO_FORMAT_PCM_16_BIT) && (format != AUDIO_FORMAT_PCM_8_24_BIT) &&
1600 (format != AUDIO_FORMAT_PCM_24_BIT_PACKED) && (format != AUDIO_FORMAT_PCM_32_BIT) &&
1601 (format != AUDIO_FORMAT_PCM_FLOAT)) &&
1602 !voice_extn_compress_voip_is_format_supported(format) &&
1603 !audio_extn_compr_cap_format_supported(format) &&
1604 !audio_extn_cin_format_supported(format))
1605 ret = -EINVAL;
1606
1607 switch (channel_count) {
1608 case 1:
1609 case 2:
1610 case 3:
1611 case 4:
1612 case 6:
1613 case 8:
1614 break;
1615 default:
1616 ret = -EINVAL;
1617 }
1618
1619 switch (sample_rate) {
1620 case 8000:
1621 case 11025:
1622 case 12000:
1623 case 16000:
1624 case 22050:
1625 case 24000:
1626 case 32000:
1627 case 44100:
1628 case 48000:
1629 case 88200:
1630 case 96000:
1631 case 176400:
1632 case 192000:
1633 break;
1634 default:
1635 ret = -EINVAL;
1636 }
1637
1638 return ret;
1639}
1640
1641static inline uint32_t audio_extn_utils_nearest_multiple(uint32_t num, uint32_t multiplier)
1642{
1643 uint32_t remainder = 0;
1644
1645 if (!multiplier)
1646 return num;
1647
1648 remainder = num % multiplier;
1649 if (remainder)
1650 num += (multiplier - remainder);
1651
1652 return num;
1653}
1654
1655static inline uint32_t audio_extn_utils_lcm(uint32_t num1, uint32_t num2)
1656{
1657 uint32_t high = num1, low = num2, temp = 0;
1658
1659 if (!num1 || !num2)
1660 return 0;
1661
1662 if (num1 < num2) {
1663 high = num2;
1664 low = num1;
1665 }
1666
1667 while (low != 0) {
1668 temp = low;
1669 low = high % low;
1670 high = temp;
1671 }
1672 return (num1 * num2)/high;
1673}
1674
Siena Richard7c2db772016-12-21 11:32:34 -08001675int audio_extn_utils_send_app_type_cfg(struct audio_device *adev,
1676 struct audio_usecase *usecase)
1677{
1678 int i, num_devices = 0;
1679 snd_device_t new_snd_devices[SND_DEVICE_OUT_END] = {0};
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301680 snd_device_t in_snd_device = usecase->in_snd_device;
Siena Richard7c2db772016-12-21 11:32:34 -08001681 int rc = 0;
1682
Aalique Grahame22e49102018-12-18 14:23:57 -08001683 if (usecase->type == PCM_HFP_CALL) {
1684 return audio_extn_utils_send_app_type_cfg_hfp(adev, usecase);
Derek Chena30a5f42019-12-03 11:17:09 -05001685 } else if (usecase->type == ICC_CALL) {
1686 return audio_extn_utils_send_app_type_cfg_icc(adev, usecase);
Fei Tongaffdf732020-02-20 20:39:05 +08001687 } else if (usecase->type == SYNTH_LOOPBACK) {
1688 return audio_extn_utils_send_app_type_cfg_synth(adev, usecase);
Aalique Grahame22e49102018-12-18 14:23:57 -08001689 }
1690
Siena Richard7c2db772016-12-21 11:32:34 -08001691 switch (usecase->type) {
1692 case PCM_PLAYBACK:
Surendar Karka93cd25a2018-08-28 14:21:37 +05301693 case TRANSCODE_LOOPBACK_RX:
Siena Richard7c2db772016-12-21 11:32:34 -08001694 ALOGD("%s: usecase->out_snd_device %s",
1695 __func__, platform_get_snd_device_name(usecase->out_snd_device));
1696 /* check for out combo device */
1697 if (platform_split_snd_device(adev->platform,
1698 usecase->out_snd_device,
1699 &num_devices, new_snd_devices)) {
1700 new_snd_devices[0] = usecase->out_snd_device;
1701 num_devices = 1;
1702 }
1703 break;
1704 case PCM_CAPTURE:
1705 ALOGD("%s: usecase->in_snd_device %s",
1706 __func__, platform_get_snd_device_name(usecase->in_snd_device));
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301707 if (voice_is_in_call_rec_stream(usecase->stream.in)) {
1708 in_snd_device = voice_get_incall_rec_backend_device(usecase->stream.in);
1709 }
Siena Richard7c2db772016-12-21 11:32:34 -08001710 /* check for in combo device */
1711 if (platform_split_snd_device(adev->platform,
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301712 in_snd_device,
Siena Richard7c2db772016-12-21 11:32:34 -08001713 &num_devices, new_snd_devices)) {
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301714 new_snd_devices[0] = in_snd_device;
Siena Richard7c2db772016-12-21 11:32:34 -08001715 num_devices = 1;
1716 }
1717 break;
Sriram Kumar9c836482020-10-27 20:34:35 +05301718 case PCM_HFP_CALL:
1719 rc = audio_extn_utils_send_app_type_cfg_hfp(adev,usecase);
1720 return rc;
Siena Richard7c2db772016-12-21 11:32:34 -08001721 default:
1722 ALOGI("%s: not a playback/capture path, no need to cfg app type", __func__);
1723 rc = 0;
1724 break;
1725 }
1726
1727 for (i = 0; i < num_devices; i++) {
1728 rc = send_app_type_cfg_for_device(adev, usecase, new_snd_devices[i]);
1729 if (rc)
1730 break;
1731 }
1732
1733 return rc;
1734}
1735
Preetam Singh Ranawat9519e9c2015-11-18 16:05:55 +05301736int read_line_from_file(const char *path, char *buf, size_t count)
1737{
1738 char * fgets_ret;
1739 FILE * fd;
1740 int rv;
1741
1742 fd = fopen(path, "r");
1743 if (fd == NULL)
1744 return -1;
1745
1746 fgets_ret = fgets(buf, (int)count, fd);
1747 if (NULL != fgets_ret) {
1748 rv = (int)strlen(buf);
1749 } else {
1750 rv = ferror(fd);
1751 }
1752 fclose(fd);
1753
1754 return rv;
1755}
1756
Ashish Jainf1eaa582016-05-23 20:54:24 +05301757/*Translates ALSA formats to AOSP PCM formats*/
1758audio_format_t alsa_format_to_hal(uint32_t alsa_format)
1759{
1760 audio_format_t format;
1761
1762 switch(alsa_format) {
1763 case SNDRV_PCM_FORMAT_S16_LE:
1764 format = AUDIO_FORMAT_PCM_16_BIT;
1765 break;
1766 case SNDRV_PCM_FORMAT_S24_3LE:
1767 format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
1768 break;
1769 case SNDRV_PCM_FORMAT_S24_LE:
1770 format = AUDIO_FORMAT_PCM_8_24_BIT;
1771 break;
1772 case SNDRV_PCM_FORMAT_S32_LE:
1773 format = AUDIO_FORMAT_PCM_32_BIT;
1774 break;
1775 default:
1776 ALOGW("Incorrect ALSA format");
1777 format = AUDIO_FORMAT_INVALID;
1778 }
1779 return format;
1780}
1781
1782/*Translates hal format (AOSP) to alsa formats*/
1783uint32_t hal_format_to_alsa(audio_format_t hal_format)
1784{
1785 uint32_t alsa_format;
1786
1787 switch (hal_format) {
1788 case AUDIO_FORMAT_PCM_32_BIT: {
1789 if (platform_supports_true_32bit())
1790 alsa_format = SNDRV_PCM_FORMAT_S32_LE;
1791 else
1792 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1793 }
1794 break;
1795 case AUDIO_FORMAT_PCM_8_BIT:
1796 alsa_format = SNDRV_PCM_FORMAT_S8;
1797 break;
1798 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
1799 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1800 break;
1801 case AUDIO_FORMAT_PCM_8_24_BIT: {
1802 if (platform_supports_true_32bit())
1803 alsa_format = SNDRV_PCM_FORMAT_S32_LE;
1804 else
1805 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1806 }
1807 break;
1808 case AUDIO_FORMAT_PCM_FLOAT:
1809 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1810 break;
1811 default:
1812 case AUDIO_FORMAT_PCM_16_BIT:
1813 alsa_format = SNDRV_PCM_FORMAT_S16_LE;
1814 break;
1815 }
1816 return alsa_format;
1817}
1818
Ashish Jain83a6cc22016-06-28 14:34:17 +05301819/*Translates PCM formats to AOSP formats*/
1820audio_format_t pcm_format_to_hal(uint32_t pcm_format)
1821{
1822 audio_format_t format = AUDIO_FORMAT_INVALID;
1823
1824 switch(pcm_format) {
1825 case PCM_FORMAT_S16_LE:
1826 format = AUDIO_FORMAT_PCM_16_BIT;
1827 break;
1828 case PCM_FORMAT_S24_3LE:
1829 format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
1830 break;
1831 case PCM_FORMAT_S24_LE:
1832 format = AUDIO_FORMAT_PCM_8_24_BIT;
1833 break;
1834 case PCM_FORMAT_S32_LE:
1835 format = AUDIO_FORMAT_PCM_32_BIT;
1836 break;
1837 default:
1838 ALOGW("Incorrect PCM format");
1839 format = AUDIO_FORMAT_INVALID;
1840 }
1841 return format;
1842}
1843
1844/*Translates hal format (AOSP) to alsa formats*/
1845uint32_t hal_format_to_pcm(audio_format_t hal_format)
1846{
1847 uint32_t pcm_format;
1848
1849 switch (hal_format) {
1850 case AUDIO_FORMAT_PCM_32_BIT:
1851 case AUDIO_FORMAT_PCM_8_24_BIT:
1852 case AUDIO_FORMAT_PCM_FLOAT: {
1853 if (platform_supports_true_32bit())
1854 pcm_format = PCM_FORMAT_S32_LE;
1855 else
1856 pcm_format = PCM_FORMAT_S24_3LE;
1857 }
1858 break;
1859 case AUDIO_FORMAT_PCM_8_BIT:
1860 pcm_format = PCM_FORMAT_S8;
1861 break;
1862 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
1863 pcm_format = PCM_FORMAT_S24_3LE;
1864 break;
1865 default:
1866 case AUDIO_FORMAT_PCM_16_BIT:
1867 pcm_format = PCM_FORMAT_S16_LE;
1868 break;
1869 }
1870 return pcm_format;
1871}
1872
Ashish Jainf1eaa582016-05-23 20:54:24 +05301873uint32_t get_alsa_fragment_size(uint32_t bytes_per_sample,
1874 uint32_t sample_rate,
Deeraj Soman65358ab2019-02-07 15:40:49 +05301875 uint32_t noOfChannels,
1876 int64_t duration_ms)
Ashish Jainf1eaa582016-05-23 20:54:24 +05301877{
1878 uint32_t fragment_size = 0;
1879 uint32_t pcm_offload_time = PCM_OFFLOAD_BUFFER_DURATION;
1880
Deeraj Soman65358ab2019-02-07 15:40:49 +05301881 if (duration_ms >= MIN_OFFLOAD_BUFFER_DURATION_MS && duration_ms <= MAX_OFFLOAD_BUFFER_DURATION_MS)
1882 pcm_offload_time = duration_ms;
1883
Ashish Jainf1eaa582016-05-23 20:54:24 +05301884 fragment_size = (pcm_offload_time
1885 * sample_rate
1886 * bytes_per_sample
1887 * noOfChannels)/1000;
1888 if (fragment_size < MIN_PCM_OFFLOAD_FRAGMENT_SIZE)
1889 fragment_size = MIN_PCM_OFFLOAD_FRAGMENT_SIZE;
1890 else if (fragment_size > MAX_PCM_OFFLOAD_FRAGMENT_SIZE)
1891 fragment_size = MAX_PCM_OFFLOAD_FRAGMENT_SIZE;
1892 /*To have same PCM samples for all channels, the buffer size requires to
1893 *be multiple of (number of channels * bytes per sample)
1894 *For writes to succeed, the buffer must be written at address which is multiple of 32
1895 */
Aalique Grahameb85f2d92017-10-16 17:53:23 -07001896 fragment_size = ALIGN(fragment_size, (bytes_per_sample * noOfChannels * 32));
Ashish Jainf1eaa582016-05-23 20:54:24 +05301897
1898 ALOGI("PCM offload Fragment size to %d bytes", fragment_size);
1899 return fragment_size;
1900}
1901
1902/* Calculates the fragment size required to configure compress session.
1903 * Based on the alsa format selected, decide if conversion is needed in
1904
1905 * HAL ( e.g. convert AUDIO_FORMAT_PCM_FLOAT input format to
1906 * AUDIO_FORMAT_PCM_24_BIT_PACKED before writing to the compress driver.
1907 */
1908void audio_extn_utils_update_direct_pcm_fragment_size(struct stream_out *out)
1909{
Ashish Jain83a6cc22016-06-28 14:34:17 +05301910 audio_format_t dst_format = out->hal_op_format;
1911 audio_format_t src_format = out->hal_ip_format;
Ashish Jainf1eaa582016-05-23 20:54:24 +05301912 uint32_t hal_op_bytes_per_sample = audio_bytes_per_sample(dst_format);
1913 uint32_t hal_ip_bytes_per_sample = audio_bytes_per_sample(src_format);
1914
1915 out->compr_config.fragment_size =
1916 get_alsa_fragment_size(hal_op_bytes_per_sample,
1917 out->sample_rate,
Deeraj Soman65358ab2019-02-07 15:40:49 +05301918 popcount(out->channel_mask),
1919 out->info.duration_us / 1000);
Ashish Jainf1eaa582016-05-23 20:54:24 +05301920
1921 if ((src_format != dst_format) &&
1922 hal_op_bytes_per_sample != hal_ip_bytes_per_sample) {
1923
Ashish Jain83a6cc22016-06-28 14:34:17 +05301924 out->hal_fragment_size =
Ashish Jainf1eaa582016-05-23 20:54:24 +05301925 ((out->compr_config.fragment_size * hal_ip_bytes_per_sample) /
1926 hal_op_bytes_per_sample);
1927 ALOGI("enable conversion hal_input_fragment_size is %d src_format %x dst_format %x",
Ashish Jain83a6cc22016-06-28 14:34:17 +05301928 out->hal_fragment_size, src_format, dst_format);
Ashish Jainf1eaa582016-05-23 20:54:24 +05301929 } else {
Ashish Jain83a6cc22016-06-28 14:34:17 +05301930 out->hal_fragment_size = out->compr_config.fragment_size;
Ashish Jainf1eaa582016-05-23 20:54:24 +05301931 }
1932}
1933
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301934/* converts pcm format 24_8 to 8_24 inplace */
1935size_t audio_extn_utils_convert_format_24_8_to_8_24(void *buf, size_t bytes)
1936{
1937 size_t i = 0;
1938 int *int_buf_stream = buf;
1939
1940 if ((bytes % 4) != 0) {
1941 ALOGE("%s: wrong inout buffer! ... is not 32 bit aligned ", __func__);
1942 return -EINVAL;
1943 }
1944
1945 for (; i < (bytes / 4); i++)
1946 int_buf_stream[i] >>= 8;
1947
1948 return bytes;
1949}
1950
Xiaojun Sang782e5b12020-06-29 21:13:06 +08001951#ifdef AUDIO_GKI_ENABLED
1952int get_snd_codec_id(audio_format_t format)
1953{
1954 int id = 0;
1955
1956 switch (format & AUDIO_FORMAT_MAIN_MASK) {
1957 case AUDIO_FORMAT_MP3:
1958 id = SND_AUDIOCODEC_MP3;
1959 break;
1960 case AUDIO_FORMAT_AAC:
1961 id = SND_AUDIOCODEC_AAC;
1962 break;
1963 case AUDIO_FORMAT_AAC_ADTS:
1964 id = SND_AUDIOCODEC_AAC;
1965 break;
1966 case AUDIO_FORMAT_AAC_LATM:
1967 id = SND_AUDIOCODEC_AAC;
1968 break;
1969 case AUDIO_FORMAT_PCM:
1970 id = SND_AUDIOCODEC_PCM;
1971 break;
1972 case AUDIO_FORMAT_FLAC:
1973 case AUDIO_FORMAT_ALAC:
1974 case AUDIO_FORMAT_APE:
1975 case AUDIO_FORMAT_VORBIS:
1976 case AUDIO_FORMAT_WMA:
1977 case AUDIO_FORMAT_WMA_PRO:
1978 case AUDIO_FORMAT_DSD:
1979 case AUDIO_FORMAT_APTX:
1980 id = SND_AUDIOCODEC_BESPOKE;
1981 break;
1982 case AUDIO_FORMAT_MP2:
1983 id = SND_AUDIOCODEC_MP2;
1984 break;
1985 case AUDIO_FORMAT_AC3:
1986 id = SND_AUDIOCODEC_AC3;
1987 break;
1988 case AUDIO_FORMAT_E_AC3:
1989 case AUDIO_FORMAT_E_AC3_JOC:
1990 id = SND_AUDIOCODEC_EAC3;
1991 break;
1992 case AUDIO_FORMAT_DTS:
1993 case AUDIO_FORMAT_DTS_HD:
1994 id = SND_AUDIOCODEC_DTS;
1995 break;
1996 case AUDIO_FORMAT_DOLBY_TRUEHD:
1997 id = SND_AUDIOCODEC_TRUEHD;
1998 break;
1999 case AUDIO_FORMAT_IEC61937:
2000 id = SND_AUDIOCODEC_IEC61937;
2001 break;
2002 default:
2003 ALOGE("%s: Unsupported audio format :%x", __func__, format);
2004 }
2005
2006 return id;
2007}
2008#else
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05302009int get_snd_codec_id(audio_format_t format)
2010{
2011 int id = 0;
2012
2013 switch (format & AUDIO_FORMAT_MAIN_MASK) {
2014 case AUDIO_FORMAT_MP3:
2015 id = SND_AUDIOCODEC_MP3;
2016 break;
2017 case AUDIO_FORMAT_AAC:
2018 id = SND_AUDIOCODEC_AAC;
2019 break;
2020 case AUDIO_FORMAT_AAC_ADTS:
2021 id = SND_AUDIOCODEC_AAC;
2022 break;
Arun Kumar Dasari3b174182016-12-27 13:01:14 +05302023 case AUDIO_FORMAT_AAC_LATM:
2024 id = SND_AUDIOCODEC_AAC;
2025 break;
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05302026 case AUDIO_FORMAT_PCM:
2027 id = SND_AUDIOCODEC_PCM;
2028 break;
2029 case AUDIO_FORMAT_FLAC:
2030 id = SND_AUDIOCODEC_FLAC;
2031 break;
2032 case AUDIO_FORMAT_ALAC:
2033 id = SND_AUDIOCODEC_ALAC;
2034 break;
2035 case AUDIO_FORMAT_APE:
2036 id = SND_AUDIOCODEC_APE;
2037 break;
2038 case AUDIO_FORMAT_VORBIS:
2039 id = SND_AUDIOCODEC_VORBIS;
2040 break;
2041 case AUDIO_FORMAT_WMA:
2042 id = SND_AUDIOCODEC_WMA;
2043 break;
Dechen Chai4a575252021-07-30 09:21:28 +05302044#ifndef AUDIO_DISABLE_COMPRESS_FORMAT
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05302045 case AUDIO_FORMAT_WMA_PRO:
2046 id = SND_AUDIOCODEC_WMA_PRO;
2047 break;
Dechen Chai4a575252021-07-30 09:21:28 +05302048#endif
Satish Babu Patakokila0c313922016-12-08 12:07:08 +05302049 case AUDIO_FORMAT_MP2:
2050 id = SND_AUDIOCODEC_MP2;
2051 break;
Satish Babu Patakokila915ecba2017-01-10 17:43:56 +05302052 case AUDIO_FORMAT_AC3:
2053 id = SND_AUDIOCODEC_AC3;
2054 break;
2055 case AUDIO_FORMAT_E_AC3:
2056 case AUDIO_FORMAT_E_AC3_JOC:
2057 id = SND_AUDIOCODEC_EAC3;
2058 break;
2059 case AUDIO_FORMAT_DTS:
2060 case AUDIO_FORMAT_DTS_HD:
2061 id = SND_AUDIOCODEC_DTS;
2062 break;
Ben Romberger1aaaf862017-04-06 17:49:46 -07002063 case AUDIO_FORMAT_DOLBY_TRUEHD:
2064 id = SND_AUDIOCODEC_TRUEHD;
2065 break;
Naresh Tanniru928f0862017-04-07 16:44:23 -07002066 case AUDIO_FORMAT_IEC61937:
2067 id = SND_AUDIOCODEC_IEC61937;
2068 break;
Dechen Chai4a575252021-07-30 09:21:28 +05302069#ifndef AUDIO_DISABLE_COMPRESS_FORMAT
Preetam Singh Ranawat4277a5a2017-01-18 19:02:24 +05302070 case AUDIO_FORMAT_DSD:
2071 id = SND_AUDIOCODEC_DSD;
2072 break;
Dhanalakshmi Siddani18737932016-11-29 17:33:17 +05302073 case AUDIO_FORMAT_APTX:
2074 id = SND_AUDIOCODEC_APTX;
2075 break;
Dechen Chai4a575252021-07-30 09:21:28 +05302076#endif
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05302077 default:
2078 ALOGE("%s: Unsupported audio format :%x", __func__, format);
2079 }
2080
2081 return id;
2082}
Xiaojun Sang782e5b12020-06-29 21:13:06 +08002083#endif
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05302084
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07002085void audio_extn_utils_send_audio_calibration(struct audio_device *adev,
2086 struct audio_usecase *usecase)
2087{
2088 int type = usecase->type;
2089
Dhananjay Kumar14245982017-01-16 20:21:00 +05302090 if (type == PCM_PLAYBACK && usecase->stream.out != NULL) {
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05302091 platform_send_audio_calibration(adev->platform, usecase,
Zhou Song06761dd2019-04-28 18:08:17 +08002092 usecase->stream.out->app_type_cfg.app_type);
Dhananjay Kumar14245982017-01-16 20:21:00 +05302093 } else if (type == PCM_CAPTURE && usecase->stream.in != NULL) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +05302094 platform_send_audio_calibration(adev->platform, usecase,
Zhou Song06761dd2019-04-28 18:08:17 +08002095 usecase->stream.in->app_type_cfg.app_type);
Sriram Kumar9c836482020-10-27 20:34:35 +05302096 } else if (type == PCM_HFP_CALL) {
2097 /* when app type is default. the sample rate is not used to send cal */
2098#ifdef ENABLE_HFP_CALIBRATION
2099 platform_send_audio_calibration_hfp(adev->platform, usecase->in_snd_device);
2100#else
2101 platform_send_audio_calibration(adev->platform, usecase,
2102 platform_get_default_app_type_v2(adev->platform, usecase->type));
2103#endif
2104 } else if ((type == PCM_CAPTURE) ||
Derek Chena30a5f42019-12-03 11:17:09 -05002105 (type == TRANSCODE_LOOPBACK_RX && usecase->stream.inout != NULL) ||
Fei Tongaffdf732020-02-20 20:39:05 +08002106 (type == ICC_CALL) || (type == SYNTH_LOOPBACK)) {
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05302107 platform_send_audio_calibration(adev->platform, usecase,
Zhou Song06761dd2019-04-28 18:08:17 +08002108 platform_get_default_app_type_v2(adev->platform, usecase->type));
Vidyakumar Athotae57f6002017-03-01 13:13:16 -08002109 } else {
2110 /* No need to send audio calibration for voice and voip call usecases */
2111 if ((type != VOICE_CALL) && (type != VOIP_CALL))
2112 ALOGW("%s: No audio calibration for usecase type = %d", __func__, type);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07002113 }
2114}
2115
Ben Rombergera04fabc2014-11-14 12:16:03 -08002116// Base64 Encode and Decode
2117// Not all features supported. This must be used only with following conditions.
2118// Decode Modes: Support with and without padding
2119// CRLF not handling. So no CRLF in string to decode.
2120// Encode Modes: Supports only padding
2121int b64decode(char *inp, int ilen, uint8_t* outp)
2122{
2123 int i, j, k, ii, num;
2124 int rem, pcnt;
2125 uint32_t res=0;
2126 uint8_t getIndex[MAX_BASEINDEX_LEN];
2127 uint8_t tmp, cflag;
2128
2129 if(inp == NULL || outp == NULL || ilen <= 0) {
2130 ALOGE("[%s] received NULL pointer or zero length",__func__);
2131 return -1;
2132 }
2133
2134 memset(getIndex, MAX_BASEINDEX_LEN-1, sizeof(getIndex));
2135 for(i=0;i<BASE_TABLE_SIZE;i++) {
2136 getIndex[(uint8_t)bTable[i]] = (uint8_t)i;
2137 }
2138 getIndex[(uint8_t)'=']=0;
2139
2140 j=0;k=0;
2141 num = ilen/4;
2142 rem = ilen%4;
2143 if(rem==0)
2144 num = num-1;
2145 cflag=0;
2146 for(i=0; i<num; i++) {
2147 res=0;
2148 for(ii=0;ii<4;ii++) {
2149 res = res << 6;
2150 tmp = getIndex[(uint8_t)inp[j++]];
2151 res = res | tmp;
2152 cflag = cflag | tmp;
2153 }
2154 outp[k++] = (res >> 16)&0xFF;
2155 outp[k++] = (res >> 8)&0xFF;
2156 outp[k++] = res & 0xFF;
2157 }
2158
2159 // Handle last bytes special
2160 pcnt=0;
2161 if(rem == 0) {
2162 //With padding or full data
2163 res = 0;
2164 for(ii=0;ii<4;ii++) {
2165 if(inp[j] == '=')
2166 pcnt++;
2167 res = res << 6;
2168 tmp = getIndex[(uint8_t)inp[j++]];
2169 res = res | tmp;
2170 cflag = cflag | tmp;
2171 }
2172 outp[k++] = res >> 16;
2173 if(pcnt == 2)
2174 goto done;
2175 outp[k++] = (res>>8)&0xFF;
2176 if(pcnt == 1)
2177 goto done;
2178 outp[k++] = res&0xFF;
2179 } else {
2180 //without padding
2181 res = 0;
2182 for(i=0;i<rem;i++) {
2183 res = res << 6;
2184 tmp = getIndex[(uint8_t)inp[j++]];
2185 res = res | tmp;
2186 cflag = cflag | tmp;
2187 }
2188 for(i=rem;i<4;i++) {
2189 res = res << 6;
2190 pcnt++;
2191 }
2192 outp[k++] = res >> 16;
2193 if(pcnt == 2)
2194 goto done;
2195 outp[k++] = (res>>8)&0xFF;
2196 if(pcnt == 1)
2197 goto done;
2198 outp[k++] = res&0xFF;
2199 }
2200done:
2201 if(cflag == 0xFF) {
2202 ALOGE("[%s] base64 decode failed. Invalid character found %s",
2203 __func__, inp);
2204 return 0;
2205 }
2206 return k;
2207}
2208
2209int b64encode(uint8_t *inp, int ilen, char* outp)
2210{
2211 int i,j,k, num;
2212 int rem=0;
2213 uint32_t res=0;
2214
2215 if(inp == NULL || outp == NULL || ilen<=0) {
2216 ALOGE("[%s] received NULL pointer or zero input length",__func__);
2217 return -1;
2218 }
2219
2220 num = ilen/3;
2221 rem = ilen%3;
2222 j=0;k=0;
2223 for(i=0; i<num; i++) {
2224 //prepare index
2225 res = inp[j++]<<16;
2226 res = res | inp[j++]<<8;
2227 res = res | inp[j++];
2228 //get output map from index
2229 outp[k++] = (char) bTable[(res>>18)&0x3F];
2230 outp[k++] = (char) bTable[(res>>12)&0x3F];
2231 outp[k++] = (char) bTable[(res>>6)&0x3F];
2232 outp[k++] = (char) bTable[res&0x3F];
2233 }
2234
2235 switch(rem) {
2236 case 1:
2237 res = inp[j++]<<16;
2238 outp[k++] = (char) bTable[res>>18];
2239 outp[k++] = (char) bTable[(res>>12)&0x3F];
2240 //outp[k++] = '=';
2241 //outp[k++] = '=';
2242 break;
2243 case 2:
2244 res = inp[j++]<<16;
2245 res = res | inp[j++]<<8;
2246 outp[k++] = (char) bTable[res>>18];
2247 outp[k++] = (char) bTable[(res>>12)&0x3F];
2248 outp[k++] = (char) bTable[(res>>6)&0x3F];
2249 //outp[k++] = '=';
2250 break;
2251 default:
2252 break;
2253 }
Ben Rombergera04fabc2014-11-14 12:16:03 -08002254 outp[k] = '\0';
2255 return k;
2256}
Ashish Jain81eb2a82015-05-13 10:52:34 +05302257
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05302258
2259int audio_extn_utils_get_codec_version(const char *snd_card_name,
2260 int card_num,
2261 char *codec_version)
2262{
2263 char procfs_path[50];
2264 FILE *fp;
2265
2266 if (strstr(snd_card_name, "tasha")) {
2267 snprintf(procfs_path, sizeof(procfs_path),
2268 "/proc/asound/card%d/codecs/tasha/version", card_num);
2269 if ((fp = fopen(procfs_path, "r")) != NULL) {
2270 fgets(codec_version, CODEC_VERSION_MAX_LENGTH, fp);
2271 fclose(fp);
2272 } else {
2273 ALOGE("%s: ERROR. cannot open %s", __func__, procfs_path);
2274 return -ENOENT;
2275 }
2276 ALOGD("%s: codec version %s", __func__, codec_version);
2277 }
2278
2279 return 0;
2280}
2281
Preetam Singh Ranawat9d0d8e42019-07-15 12:27:25 +05302282int audio_extn_utils_get_codec_variant(int card_num,
2283 char *codec_variant)
2284{
2285 char procfs_path[50];
2286 FILE *fp;
2287 snprintf(procfs_path, sizeof(procfs_path),
2288 "/proc/asound/card%d/codecs/wcd938x/variant", card_num);
2289 if ((fp = fopen(procfs_path, "r")) == NULL) {
2290 snprintf(procfs_path, sizeof(procfs_path),
2291 "/proc/asound/card%d/codecs/wcd937x/variant", card_num);
2292 if ((fp = fopen(procfs_path, "r")) == NULL) {
2293 ALOGE("%s: ERROR. cannot open %s", __func__, procfs_path);
2294 return -ENOENT;
2295 }
2296 }
2297 fgets(codec_variant, CODEC_VARIANT_MAX_LENGTH, fp);
2298 fclose(fp);
2299 ALOGD("%s: codec variant is %s", __func__, codec_variant);
2300 return 0;
2301}
2302
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05302303
Ashish Jain81eb2a82015-05-13 10:52:34 +05302304#ifdef AUDIO_EXTERNAL_HDMI_ENABLED
2305
2306void get_default_compressed_channel_status(
2307 unsigned char *channel_status)
2308{
Ashish Jain81eb2a82015-05-13 10:52:34 +05302309 memset(channel_status,0,24);
2310
2311 /* block start bit in preamble bit 3 */
2312 channel_status[0] |= PROFESSIONAL;
2313 //compre out
2314 channel_status[0] |= NON_LPCM;
2315 // sample rate; fixed 48K for default/transcode
2316 channel_status[3] |= SR_48000;
2317}
2318
Ashish Jain81eb2a82015-05-13 10:52:34 +05302319int32_t get_compressed_channel_status(void *audio_stream_data,
2320 uint32_t audio_frame_size,
2321 unsigned char *channel_status,
2322 enum audio_parser_code_type codec_type)
2323 // codec_type - AUDIO_PARSER_CODEC_AC3
2324 // - AUDIO_PARSER_CODEC_DTS
2325{
2326 unsigned char *stream;
2327 int ret = 0;
2328 stream = (unsigned char *)audio_stream_data;
2329
2330 if (audio_stream_data == NULL || audio_frame_size == 0) {
2331 ALOGW("no buffer to get channel status, return default for compress");
2332 get_default_compressed_channel_status(channel_status);
2333 return ret;
2334 }
2335
2336 memset(channel_status,0,24);
2337 if(init_audio_parser(stream, audio_frame_size, codec_type) == -1)
2338 {
2339 ALOGE("init audio parser failed");
2340 return -1;
2341 }
2342 ret = get_channel_status(channel_status, codec_type);
2343 return ret;
2344
2345}
2346
Ashish Jain81eb2a82015-05-13 10:52:34 +05302347void get_lpcm_channel_status(uint32_t sampleRate,
2348 unsigned char *channel_status)
2349{
2350 int32_t status = 0;
Ashish Jain81eb2a82015-05-13 10:52:34 +05302351 memset(channel_status,0,24);
2352 /* block start bit in preamble bit 3 */
2353 channel_status[0] |= PROFESSIONAL;
2354 //LPCM OUT
2355 channel_status[0] &= ~NON_LPCM;
2356
2357 switch (sampleRate) {
2358 case 8000:
2359 case 11025:
2360 case 12000:
2361 case 16000:
2362 case 22050:
2363 channel_status[3] |= SR_NOTID;
Preetam Singh Ranawat61716b12015-12-14 11:55:24 +05302364 break;
Ashish Jain81eb2a82015-05-13 10:52:34 +05302365 case 24000:
2366 channel_status[3] |= SR_24000;
2367 break;
2368 case 32000:
2369 channel_status[3] |= SR_32000;
2370 break;
2371 case 44100:
2372 channel_status[3] |= SR_44100;
2373 break;
2374 case 48000:
2375 channel_status[3] |= SR_48000;
2376 break;
2377 case 88200:
2378 channel_status[3] |= SR_88200;
2379 break;
2380 case 96000:
2381 channel_status[3] |= SR_96000;
2382 break;
2383 case 176400:
2384 channel_status[3] |= SR_176400;
2385 break;
2386 case 192000:
2387 channel_status[3] |= SR_192000;
2388 break;
2389 default:
2390 ALOGV("Invalid sample_rate %u\n", sampleRate);
2391 status = -1;
2392 break;
2393 }
2394}
2395
2396void audio_utils_set_hdmi_channel_status(struct stream_out *out, char * buffer, size_t bytes)
2397{
2398 unsigned char channel_status[24]={0};
2399 struct snd_aes_iec958 iec958;
2400 const char *mixer_ctl_name = "IEC958 Playback PCM Stream";
2401 struct mixer_ctl *ctl;
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +05302402 ALOGV("%s: buffer %s bytes %zd", __func__, buffer, bytes);
Arun Mirpurie008ed22019-03-21 11:21:04 -07002403
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05302404 if (audio_extn_utils_is_dolby_format(out->format) &&
Ashish Jain81eb2a82015-05-13 10:52:34 +05302405 /*TODO:Extend code to support DTS passthrough*/
2406 /*set compressed channel status bits*/
Arun Mirpurie008ed22019-03-21 11:21:04 -07002407 audio_extn_passthru_is_passthrough_stream(out) &&
2408 audio_extn_is_hdmi_passthru_enabled()) {
Ashish Jain81eb2a82015-05-13 10:52:34 +05302409 get_compressed_channel_status(buffer, bytes, channel_status, AUDIO_PARSER_CODEC_AC3);
Arun Mirpurie008ed22019-03-21 11:21:04 -07002410 } else {
Ashish Jain81eb2a82015-05-13 10:52:34 +05302411 /*set channel status bit for LPCM*/
2412 get_lpcm_channel_status(out->sample_rate, channel_status);
2413 }
2414
2415 memcpy(iec958.status, channel_status,sizeof(iec958.status));
2416 ctl = mixer_get_ctl_by_name(out->dev->mixer, mixer_ctl_name);
2417 if (!ctl) {
2418 ALOGE("%s: Could not get ctl for mixer cmd - %s",
2419 __func__, mixer_ctl_name);
2420 return;
2421 }
2422 if (mixer_ctl_set_array(ctl, &iec958, sizeof(iec958)) < 0) {
2423 ALOGE("%s: Could not set channel status for ext HDMI ",
2424 __func__);
2425 return;
2426 }
2427
2428}
2429#endif
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302430
2431int audio_extn_utils_get_avt_device_drift(
2432 struct audio_usecase *usecase,
2433 struct audio_avt_device_drift_param *drift_param)
2434{
2435 int ret = 0, count = 0;
2436 char avt_device_drift_mixer_ctl_name[MIXER_PATH_MAX_LENGTH] = {0};
Naresh Tanniru6160c712017-04-17 15:43:48 +05302437 const char *backend = NULL;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302438 struct mixer_ctl *ctl = NULL;
2439 struct audio_avt_device_drift_stats drift_stats;
2440 struct audio_device *adev = NULL;
2441
2442 if (usecase != NULL && usecase->type == PCM_PLAYBACK) {
Naresh Tanniru6160c712017-04-17 15:43:48 +05302443 backend = platform_get_snd_device_backend_interface(usecase->out_snd_device);
2444 if (!backend) {
2445 ALOGE("%s: Unsupported device %d", __func__,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08002446 get_device_types(&usecase->stream.out->device_list));
Naresh Tanniru6160c712017-04-17 15:43:48 +05302447 ret = -EINVAL;
2448 goto done;
2449 }
2450 strlcpy(avt_device_drift_mixer_ctl_name,
2451 backend,
2452 MIXER_PATH_MAX_LENGTH);
2453
2454 count = strlen(backend);
2455 if (MIXER_PATH_MAX_LENGTH - count > 0) {
2456 strlcat(&avt_device_drift_mixer_ctl_name[count],
2457 " DRIFT",
2458 MIXER_PATH_MAX_LENGTH - count);
2459 } else {
2460 ret = -EINVAL;
2461 goto done;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302462 }
2463 } else {
Naresh Tanniru7586e292017-04-13 10:38:13 +05302464 ALOGE("%s: Invalid usecase",__func__);
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302465 ret = -EINVAL;
Naresh Tanniru6160c712017-04-17 15:43:48 +05302466 goto done;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302467 }
2468
Naresh Tanniru6160c712017-04-17 15:43:48 +05302469 adev = usecase->stream.out->dev;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302470 ctl = mixer_get_ctl_by_name(adev->mixer, avt_device_drift_mixer_ctl_name);
2471 if (!ctl) {
2472 ALOGE("%s: Could not get ctl for mixer cmd - %s",
2473 __func__, avt_device_drift_mixer_ctl_name);
2474
2475 ret = -EINVAL;
2476 goto done;
2477 }
2478
2479 ALOGV("%s: Getting AV Timer vs Device Drift mixer ctrl name %s", __func__,
2480 avt_device_drift_mixer_ctl_name);
2481
2482 mixer_ctl_update(ctl);
2483 count = mixer_ctl_get_num_values(ctl);
2484 if (count != sizeof(struct audio_avt_device_drift_stats)) {
2485 ALOGE("%s: mixer_ctl_get_num_values() invalid drift_stats data size",
2486 __func__);
2487
2488 ret = -EINVAL;
2489 goto done;
2490 }
2491
2492 ret = mixer_ctl_get_array(ctl, (void *)&drift_stats, count);
2493 if (ret != 0) {
2494 ALOGE("%s: mixer_ctl_get_array() failed to get drift_stats Params",
2495 __func__);
2496
2497 ret = -EINVAL;
2498 goto done;
2499 }
2500 memcpy(drift_param, &drift_stats.drift_param,
2501 sizeof(struct audio_avt_device_drift_param));
2502done:
2503 return ret;
2504}
Manish Dewangan07de2142017-02-27 19:27:20 +05302505
2506#ifdef SNDRV_COMPRESS_PATH_DELAY
2507int audio_extn_utils_compress_get_dsp_latency(struct stream_out *out)
2508{
2509 int ret = -EINVAL;
2510 struct snd_compr_metadata metadata;
2511 int delay_ms = COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
2512
Weiyin Jiangc49a3b52018-08-17 16:16:08 +08002513 /* override the latency for pcm offload use case */
2514 if ((out->flags & AUDIO_OUTPUT_FLAG_DIRECT) &&
2515 !(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)) {
2516 delay_ms = PCM_OFFLOAD_PLAYBACK_LATENCY;
2517 }
2518
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -07002519 if (property_get_bool("vendor.audio.playback.dsp.pathdelay", false)) {
Manish Dewangan07de2142017-02-27 19:27:20 +05302520 ALOGD("%s:: Quering DSP delay %d",__func__, __LINE__);
2521 if (!(is_offload_usecase(out->usecase))) {
2522 ALOGE("%s:: not supported for non offload session", __func__);
2523 goto exit;
2524 }
2525
2526 if (!out->compr) {
2527 ALOGD("%s:: Invalid compress handle,returning default dsp latency",
2528 __func__);
2529 goto exit;
2530 }
2531
2532 metadata.key = SNDRV_COMPRESS_PATH_DELAY;
2533 ret = compress_get_metadata(out->compr, &metadata);
2534 if(ret) {
2535 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2536 goto exit;
2537 }
2538 delay_ms = metadata.value[0] / 1000; /*convert to ms*/
2539 } else {
2540 ALOGD("%s:: Using Fix DSP delay",__func__);
2541 }
2542
2543exit:
2544 ALOGD("%s:: delay in ms is %d",__func__, delay_ms);
2545 return delay_ms;
2546}
2547#else
2548int audio_extn_utils_compress_get_dsp_latency(struct stream_out *out __unused)
2549{
2550 return COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
2551}
2552#endif
Manish Dewangan69426c82017-01-30 17:35:36 +05302553
2554#ifdef SNDRV_COMPRESS_RENDER_MODE
2555int audio_extn_utils_compress_set_render_mode(struct stream_out *out)
2556{
2557 struct snd_compr_metadata metadata;
2558 int ret = -EINVAL;
2559
2560 if (!(is_offload_usecase(out->usecase))) {
2561 ALOGE("%s:: not supported for non offload session", __func__);
2562 goto exit;
2563 }
2564
2565 if (!out->compr) {
2566 ALOGD("%s:: Invalid compress handle",
2567 __func__);
2568 goto exit;
2569 }
2570
2571 ALOGD("%s:: render mode %d", __func__, out->render_mode);
2572
2573 metadata.key = SNDRV_COMPRESS_RENDER_MODE;
2574 if (out->render_mode == RENDER_MODE_AUDIO_MASTER) {
2575 metadata.value[0] = SNDRV_COMPRESS_RENDER_MODE_AUDIO_MASTER;
2576 } else if (out->render_mode == RENDER_MODE_AUDIO_STC_MASTER) {
2577 metadata.value[0] = SNDRV_COMPRESS_RENDER_MODE_STC_MASTER;
2578 } else {
2579 ret = 0;
2580 goto exit;
2581 }
2582 ret = compress_set_metadata(out->compr, &metadata);
2583 if(ret) {
2584 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2585 }
2586exit:
2587 return ret;
2588}
2589#else
2590int audio_extn_utils_compress_set_render_mode(struct stream_out *out __unused)
2591{
2592 ALOGD("%s:: configuring render mode not supported", __func__);
2593 return 0;
2594}
2595#endif
Manish Dewangan58229382017-02-02 15:48:41 +05302596
2597#ifdef SNDRV_COMPRESS_CLK_REC_MODE
2598int audio_extn_utils_compress_set_clk_rec_mode(
2599 struct audio_usecase *usecase)
2600{
2601 struct snd_compr_metadata metadata;
2602 struct stream_out *out = NULL;
2603 int ret = -EINVAL;
2604
Naresh Tanniru7586e292017-04-13 10:38:13 +05302605 if (usecase == NULL || usecase->type != PCM_PLAYBACK) {
Manish Dewangan58229382017-02-02 15:48:41 +05302606 ALOGE("%s:: Invalid use case", __func__);
2607 goto exit;
2608 }
2609
2610 out = usecase->stream.out;
2611 if (!out) {
2612 ALOGE("%s:: invalid stream", __func__);
2613 goto exit;
2614 }
2615
2616 if (!is_offload_usecase(out->usecase)) {
2617 ALOGE("%s:: not supported for non offload session", __func__);
2618 goto exit;
2619 }
2620
2621 if (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER) {
2622 ALOGD("%s:: clk recovery is only supported in STC render mode",
2623 __func__);
2624 ret = 0;
2625 goto exit;
2626 }
2627
2628 if (!out->compr) {
2629 ALOGD("%s:: Invalid compress handle",
2630 __func__);
2631 goto exit;
2632 }
2633 metadata.key = SNDRV_COMPRESS_CLK_REC_MODE;
2634 switch(usecase->out_snd_device) {
2635 case SND_DEVICE_OUT_HDMI:
2636 case SND_DEVICE_OUT_SPEAKER_AND_HDMI:
2637 case SND_DEVICE_OUT_DISPLAY_PORT:
2638 case SND_DEVICE_OUT_SPEAKER_AND_DISPLAY_PORT:
2639 metadata.value[0] = SNDRV_COMPRESS_CLK_REC_MODE_NONE;
2640 break;
2641 default:
2642 metadata.value[0] = SNDRV_COMPRESS_CLK_REC_MODE_AUTO;
2643 break;
2644 }
2645
2646 ALOGD("%s:: clk recovery mode %d",__func__, metadata.value[0]);
2647
2648 ret = compress_set_metadata(out->compr, &metadata);
2649 if(ret) {
2650 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2651 }
2652
2653exit:
2654 return ret;
2655}
2656#else
2657int audio_extn_utils_compress_set_clk_rec_mode(
2658 struct audio_usecase *usecase __unused)
2659{
2660 ALOGD("%s:: configuring render mode not supported", __func__);
2661 return 0;
2662}
2663#endif
Manish Dewangan27346042017-03-01 12:56:12 +05302664
2665#ifdef SNDRV_COMPRESS_RENDER_WINDOW
2666int audio_extn_utils_compress_set_render_window(
2667 struct stream_out *out,
2668 struct audio_out_render_window_param *render_window)
2669{
2670 struct snd_compr_metadata metadata;
2671 int ret = -EINVAL;
2672
Manish Dewangan27346042017-03-01 12:56:12 +05302673 if(render_window == NULL) {
2674 ALOGE("%s:: Invalid render_window", __func__);
2675 goto exit;
2676 }
2677
Aniket Kumar Lata1e1d3662017-06-01 18:45:48 -07002678 ALOGD("%s:: render window start 0x%"PRIx64" end 0x%"PRIx64"",
2679 __func__,render_window->render_ws, render_window->render_we);
2680
Manish Dewangan27346042017-03-01 12:56:12 +05302681 if (!is_offload_usecase(out->usecase)) {
2682 ALOGE("%s:: not supported for non offload session", __func__);
2683 goto exit;
2684 }
2685
Naresh Tanniru6160c712017-04-17 15:43:48 +05302686 if ((out->render_mode != RENDER_MODE_AUDIO_MASTER) &&
2687 (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER)) {
Manish Dewangan27346042017-03-01 12:56:12 +05302688 ALOGD("%s:: only supported in timestamp mode, current "
2689 "render mode mode %d", __func__, out->render_mode);
2690 goto exit;
2691 }
2692
2693 if (!out->compr) {
2694 ALOGW("%s:: offload session not yet opened,"
2695 "render window will be configure later", __func__);
2696 /* store render window to reconfigure in start_output_stream() */
2697 goto exit;
2698 }
2699
2700 metadata.key = SNDRV_COMPRESS_RENDER_WINDOW;
2701 /*render window start value */
2702 metadata.value[0] = 0xFFFFFFFF & render_window->render_ws; /* lsb */
2703 metadata.value[1] = \
2704 (0xFFFFFFFF00000000 & render_window->render_ws) >> 32; /* msb*/
2705 /*render window end value */
2706 metadata.value[2] = 0xFFFFFFFF & render_window->render_we; /* lsb */
2707 metadata.value[3] = \
2708 (0xFFFFFFFF00000000 & render_window->render_we) >> 32; /* msb*/
2709
2710 ret = compress_set_metadata(out->compr, &metadata);
2711 if(ret) {
2712 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2713 }
2714
2715exit:
2716 return ret;
2717}
2718#else
2719int audio_extn_utils_compress_set_render_window(
2720 struct stream_out *out __unused,
2721 struct audio_out_render_window_param *render_window __unused)
2722{
2723 ALOGD("%s:: configuring render window not supported", __func__);
2724 return 0;
2725}
2726#endif
Manish Dewangan14956cc2017-02-14 18:54:42 +05302727
2728#ifdef SNDRV_COMPRESS_START_DELAY
2729int audio_extn_utils_compress_set_start_delay(
2730 struct stream_out *out,
2731 struct audio_out_start_delay_param *delay_param)
2732{
2733 struct snd_compr_metadata metadata;
2734 int ret = -EINVAL;
2735
2736 if(delay_param == NULL) {
2737 ALOGE("%s:: Invalid delay_param", __func__);
2738 goto exit;
2739 }
2740
2741 ALOGD("%s:: render start delay 0x%"PRIx64" ", __func__,
2742 delay_param->start_delay);
2743
2744 if (!is_offload_usecase(out->usecase)) {
2745 ALOGE("%s:: not supported for non offload session", __func__);
2746 goto exit;
2747 }
2748
Naresh Tanniru6160c712017-04-17 15:43:48 +05302749 if ((out->render_mode != RENDER_MODE_AUDIO_MASTER) &&
2750 (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER)) {
Manish Dewangan14956cc2017-02-14 18:54:42 +05302751 ALOGD("%s:: only supported in timestamp mode, current "
2752 "render mode mode %d", __func__, out->render_mode);
2753 goto exit;
2754 }
2755
2756 if (!out->compr) {
2757 ALOGW("%s:: offload session not yet opened,"
2758 "start delay will be configure later", __func__);
2759 goto exit;
2760 }
2761
2762 metadata.key = SNDRV_COMPRESS_START_DELAY;
2763 metadata.value[0] = 0xFFFFFFFF & delay_param->start_delay; /* lsb */
2764 metadata.value[1] = \
2765 (0xFFFFFFFF00000000 & delay_param->start_delay) >> 32; /* msb*/
2766
2767 ret = compress_set_metadata(out->compr, &metadata);
2768 if(ret) {
2769 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2770 }
2771
2772exit:
2773 return ret;
2774}
2775#else
2776int audio_extn_utils_compress_set_start_delay(
2777 struct stream_out *out __unused,
2778 struct audio_out_start_delay_param *delay_param __unused)
2779{
2780 ALOGD("%s:: configuring render window not supported", __func__);
2781 return 0;
2782}
2783#endif
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002784
Surendar Karka287348c2019-04-10 18:31:46 +05302785#ifdef SNDRV_COMPRESS_DSP_POSITION
2786int audio_extn_utils_compress_get_dsp_presentation_pos(struct stream_out *out,
2787 uint64_t *frames, struct timespec *timestamp, int32_t clock_id)
2788{
2789 int ret = -EINVAL;
2790 uint64_t *val = NULL;
2791 uint64_t time = 0;
2792 struct snd_compr_metadata metadata;
2793
2794 ALOGV("%s:: Quering DSP position with clock id %d",__func__, clock_id);
2795 metadata.key = SNDRV_COMPRESS_DSP_POSITION;
2796 metadata.value[0] = clock_id;
2797 ret = compress_get_metadata(out->compr, &metadata);
2798 if (ret) {
2799 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2800 ret = -errno;
2801 goto exit;
2802 }
2803 val = (uint64_t *)&metadata.value[1];
2804 *frames = *val;
2805 time = *(val + 1);
2806 timestamp->tv_sec = time / 1000000;
2807 timestamp->tv_nsec = (time % 1000000)*1000;
2808
2809exit:
2810 return ret;
2811}
2812#else
2813int audio_extn_utils_compress_get_dsp_presentation_pos(struct stream_out *out __unused,
2814 uint64_t *frames __unused, struct timespec *timestamp __unused,
2815 int32_t clock_id __unused)
2816{
2817 ALOGD("%s:: dsp presentation position not supported", __func__);
2818 return 0;
2819
2820}
2821#endif
2822
2823#ifdef SNDRV_PCM_IOCTL_DSP_POSITION
2824int audio_extn_utils_pcm_get_dsp_presentation_pos(struct stream_out *out,
2825 uint64_t *frames, struct timespec *timestamp, int32_t clock_id)
2826{
2827 int ret = -EINVAL;
2828 uint64_t time = 0;
2829 struct snd_pcm_prsnt_position prsnt_position;
Krishna Kishor Jhac1542b22022-08-22 13:12:09 +05302830 memset(&prsnt_position, 0, sizeof(struct snd_pcm_prsnt_position));
Surendar Karka287348c2019-04-10 18:31:46 +05302831
2832 ALOGV("%s:: Quering DSP position with clock id %d",__func__, clock_id);
2833 prsnt_position.clock_id = clock_id;
2834 ret = pcm_ioctl(out->pcm, SNDRV_PCM_IOCTL_DSP_POSITION, &prsnt_position);
2835 if (ret) {
2836 ALOGE("%s::error %d", __func__, ret);
2837 ret = -EIO;
2838 goto exit;
2839 }
2840
2841 *frames = prsnt_position.frames;
2842 time = prsnt_position.timestamp;
2843 timestamp->tv_sec = time / 1000000;
2844 timestamp->tv_nsec = (time % 1000000)*1000;
2845
2846exit:
2847 return ret;
2848}
2849#else
2850int audio_extn_utils_pcm_get_dsp_presentation_pos(struct stream_out *out __unused,
2851 uint64_t *frames __unused, struct timespec *timestamp __unused,
2852 int32_t clock_id __unused)
2853{
2854 ALOGD("%s:: dsp presentation position not supported", __func__);
2855 return 0;
2856
2857}
2858#endif
2859
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002860#define MAX_SND_CARD 8
Jaideep Sharmadf948882020-10-22 14:13:52 +05302861#define RETRY_US 400000
2862#define RETRY_NUMBER 100
Aalique Grahame22e49102018-12-18 14:23:57 -08002863#define PLATFORM_INFO_XML_PATH "audio_platform_info.xml"
2864#define PLATFORM_INFO_XML_BASE_STRING "audio_platform_info"
2865
Aalique Grahame22e49102018-12-18 14:23:57 -08002866bool audio_extn_utils_resolve_config_file(char file_name[MIXER_PATH_MAX_LENGTH])
2867{
2868 char full_config_path[MIXER_PATH_MAX_LENGTH];
Saurav Kumarf5d2c342020-07-29 19:31:49 +05302869 char vendor_config_path[VENDOR_CONFIG_PATH_MAX_LENGTH];
2870
2871 /* Get path for audio configuration files in vendor */
2872 audio_get_vendor_config_path(vendor_config_path,
2873 sizeof(vendor_config_path));
2874 snprintf(full_config_path, sizeof(full_config_path),
2875 "%s/%s", vendor_config_path, file_name);
2876 if (F_OK == access(full_config_path, 0)) {
2877 strlcpy(file_name, full_config_path, MIXER_PATH_MAX_LENGTH);
2878 return true;
Aalique Grahame22e49102018-12-18 14:23:57 -08002879 }
2880 return false;
2881}
2882
2883/* platform_info_file should be size 'MIXER_PATH_MAX_LENGTH' */
2884int audio_extn_utils_get_platform_info(const char* snd_card_name, char* platform_info_file)
2885{
2886 if (NULL == snd_card_name) {
2887 return -1;
2888 }
2889
2890 struct snd_card_split *snd_split_handle = NULL;
2891 int ret = 0;
2892 audio_extn_set_snd_card_split(snd_card_name);
2893 snd_split_handle = audio_extn_get_snd_card_split();
2894
2895 snprintf(platform_info_file, MIXER_PATH_MAX_LENGTH, "%s_%s_%s.xml",
2896 PLATFORM_INFO_XML_BASE_STRING, snd_split_handle->snd_card,
2897 snd_split_handle->form_factor);
2898
2899 if (!audio_extn_utils_resolve_config_file(platform_info_file)) {
2900 memset(platform_info_file, 0, MIXER_PATH_MAX_LENGTH);
2901 snprintf(platform_info_file, MIXER_PATH_MAX_LENGTH, "%s_%s.xml",
2902 PLATFORM_INFO_XML_BASE_STRING, snd_split_handle->snd_card);
2903
2904 if (!audio_extn_utils_resolve_config_file(platform_info_file)) {
2905 memset(platform_info_file, 0, MIXER_PATH_MAX_LENGTH);
2906 strlcpy(platform_info_file, PLATFORM_INFO_XML_PATH, MIXER_PATH_MAX_LENGTH);
2907 ret = audio_extn_utils_resolve_config_file(platform_info_file) ? 0 : -1;
2908 }
2909 }
2910
2911 return ret;
2912}
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002913
2914int audio_extn_utils_get_snd_card_num()
2915{
Soumya Managoli9fee7c62018-04-06 16:21:50 +05302916 int snd_card_num = 0;
2917 struct mixer *mixer = NULL;
2918
2919 snd_card_num = audio_extn_utils_open_snd_mixer(&mixer);
2920 if (mixer)
2921 mixer_close(mixer);
2922 return snd_card_num;
2923}
2924
2925int audio_extn_utils_open_snd_mixer(struct mixer **mixer_handle)
2926{
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002927
2928 void *hw_info = NULL;
2929 struct mixer *mixer = NULL;
2930 int retry_num = 0;
mpangfaa7bae2019-01-15 16:38:13 +08002931 int snd_card_num = 0;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002932 char* snd_card_name = NULL;
mpangfaa7bae2019-01-15 16:38:13 +08002933 int snd_card_detection_info[MAX_SND_CARD] = {0};
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002934
Soumya Managoli9fee7c62018-04-06 16:21:50 +05302935 if (!mixer_handle) {
2936 ALOGE("invalid mixer handle");
2937 return -1;
2938 }
2939 *mixer_handle = NULL;
Ashish Jain30ae8942017-11-05 17:21:23 +05302940 /*
mpangfaa7bae2019-01-15 16:38:13 +08002941 * Try with all the sound cards ( 0 to 7 ) and if none of them were detected
Ashish Jain30ae8942017-11-05 17:21:23 +05302942 * sleep for 1 sec and try detections with sound card 0 again.
2943 * If sound card gets detected, check if it is relevant, if not check with the
2944 * other sound cards. To ensure that the irrelevant sound card is not check again,
2945 * we maintain it in min_snd_card_num.
2946 */
2947 while (retry_num < RETRY_NUMBER) {
mpangfaa7bae2019-01-15 16:38:13 +08002948 snd_card_num = 0;
2949 while (snd_card_num < MAX_SND_CARD) {
2950 if (snd_card_detection_info[snd_card_num] == 0) {
2951 mixer = mixer_open(snd_card_num);
2952 if (!mixer)
2953 snd_card_num++;
2954 else
2955 break;
2956 } else
2957 snd_card_num++;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002958 }
2959
2960 if (!mixer) {
Ashish Jain30ae8942017-11-05 17:21:23 +05302961 usleep(RETRY_US);
Ashish Jain30ae8942017-11-05 17:21:23 +05302962 retry_num++;
mpangfaa7bae2019-01-15 16:38:13 +08002963 ALOGD("%s: retry, retry_num %d", __func__, retry_num);
Ashish Jain30ae8942017-11-05 17:21:23 +05302964 continue;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002965 }
2966
2967 snd_card_name = strdup(mixer_get_name(mixer));
2968 if (!snd_card_name) {
2969 ALOGE("failed to allocate memory for snd_card_name\n");
2970 mixer_close(mixer);
2971 return -1;
2972 }
2973 ALOGD("%s: snd_card_name: %s", __func__, snd_card_name);
mpangfaa7bae2019-01-15 16:38:13 +08002974 snd_card_detection_info[snd_card_num] = 1;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002975 hw_info = hw_info_init(snd_card_name);
2976 if (hw_info) {
2977 ALOGD("%s: Opened sound card:%d", __func__, snd_card_num);
2978 break;
2979 }
mpangfaa7bae2019-01-15 16:38:13 +08002980 ALOGE("%s: Failed to init hardware info, snd_card_num:%d", __func__, snd_card_num);
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002981
Dhananjay Kumara7e27832017-07-11 15:40:39 +05302982 free(snd_card_name);
2983 snd_card_name = NULL;
2984
2985 mixer_close(mixer);
2986 mixer = NULL;
2987 }
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002988 if (snd_card_name)
2989 free(snd_card_name);
Ashish Jain30ae8942017-11-05 17:21:23 +05302990
Dhananjay Kumara7e27832017-07-11 15:40:39 +05302991 if (hw_info)
2992 hw_info_deinit(hw_info);
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002993
mpangfaa7bae2019-01-15 16:38:13 +08002994 if (retry_num >= RETRY_NUMBER) {
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002995 ALOGE("%s: Unable to find correct sound card, aborting.", __func__);
2996 return -1;
2997 }
2998
Soumya Managoli9fee7c62018-04-06 16:21:50 +05302999 if (mixer)
3000 *mixer_handle = mixer;
3001
Vignesh Kulothungan55396882017-04-20 14:37:02 -07003002 return snd_card_num;
3003}
Naresh Tanniru6160c712017-04-17 15:43:48 +05303004
Soumya Managoli9fee7c62018-04-06 16:21:50 +05303005void audio_extn_utils_close_snd_mixer(struct mixer *mixer)
3006{
3007 if (mixer)
3008 mixer_close(mixer);
3009}
3010
Naresh Tanniru6160c712017-04-17 15:43:48 +05303011#ifdef SNDRV_COMPRESS_ENABLE_ADJUST_SESSION_CLOCK
3012int audio_extn_utils_compress_enable_drift_correction(
3013 struct stream_out *out,
3014 struct audio_out_enable_drift_correction *drift)
3015{
3016 struct snd_compr_metadata metadata;
3017 int ret = -EINVAL;
3018
3019 if(drift == NULL) {
3020 ALOGE("%s:: Invalid param", __func__);
3021 goto exit;
3022 }
3023
3024 ALOGD("%s:: drift enable %d", __func__,drift->enable);
3025
3026 if (!is_offload_usecase(out->usecase)) {
3027 ALOGE("%s:: not supported for non offload session", __func__);
3028 goto exit;
3029 }
3030
3031 if (!out->compr) {
3032 ALOGW("%s:: offload session not yet opened,"
3033 "start delay will be configure later", __func__);
3034 goto exit;
3035 }
3036
3037 metadata.key = SNDRV_COMPRESS_ENABLE_ADJUST_SESSION_CLOCK;
3038 metadata.value[0] = drift->enable;
3039 out->drift_correction_enabled = drift->enable;
3040
3041 ret = compress_set_metadata(out->compr, &metadata);
3042 if(ret) {
3043 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
3044 out->drift_correction_enabled = false;
3045 }
3046
3047exit:
3048 return ret;
3049}
3050#else
3051int audio_extn_utils_compress_enable_drift_correction(
3052 struct stream_out *out __unused,
3053 struct audio_out_enable_drift_correction *drift __unused)
3054{
3055 ALOGD("%s:: configuring drift enablement not supported", __func__);
3056 return 0;
3057}
3058#endif
3059
3060#ifdef SNDRV_COMPRESS_ADJUST_SESSION_CLOCK
3061int audio_extn_utils_compress_correct_drift(
3062 struct stream_out *out,
3063 struct audio_out_correct_drift *drift_param)
3064{
3065 struct snd_compr_metadata metadata;
3066 int ret = -EINVAL;
3067
3068 if (drift_param == NULL) {
3069 ALOGE("%s:: Invalid drift_param", __func__);
3070 goto exit;
3071 }
3072
3073 ALOGD("%s:: adjust time 0x%"PRIx64" ", __func__,
3074 drift_param->adjust_time);
3075
3076 if (!is_offload_usecase(out->usecase)) {
3077 ALOGE("%s:: not supported for non offload session", __func__);
3078 goto exit;
3079 }
3080
3081 if (!out->compr) {
3082 ALOGW("%s:: offload session not yet opened", __func__);
3083 goto exit;
3084 }
3085
3086 if (!out->drift_correction_enabled) {
3087 ALOGE("%s:: drift correction not enabled", __func__);
3088 goto exit;
3089 }
3090
3091 metadata.key = SNDRV_COMPRESS_ADJUST_SESSION_CLOCK;
3092 metadata.value[0] = 0xFFFFFFFF & drift_param->adjust_time; /* lsb */
3093 metadata.value[1] = \
3094 (0xFFFFFFFF00000000 & drift_param->adjust_time) >> 32; /* msb*/
3095
3096 ret = compress_set_metadata(out->compr, &metadata);
3097 if(ret)
3098 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
3099exit:
3100 return ret;
3101}
3102#else
3103int audio_extn_utils_compress_correct_drift(
3104 struct stream_out *out __unused,
3105 struct audio_out_correct_drift *drift_param __unused)
3106{
3107 ALOGD("%s:: setting adjust clock not supported", __func__);
3108 return 0;
3109}
3110#endif
Naresh Tanniru29bce4e2017-04-27 17:54:30 +05303111
3112int audio_extn_utils_set_channel_map(
3113 struct stream_out *out,
3114 struct audio_out_channel_map_param *channel_map_param)
3115{
3116 int ret = -EINVAL, i = 0;
3117 int channels = audio_channel_count_from_out_mask(out->channel_mask);
3118
3119 if (channel_map_param == NULL) {
3120 ALOGE("%s:: Invalid channel_map", __func__);
3121 goto exit;
3122 }
3123
3124 if (channel_map_param->channels != channels) {
3125 ALOGE("%s:: Channels(%d) does not match stream channels(%d)",
3126 __func__, channel_map_param->channels, channels);
3127 goto exit;
3128 }
3129
3130 for ( i = 0; i < channels; i++) {
3131 ALOGV("%s:: channel_map[%d]- %d", __func__, i, channel_map_param->channel_map[i]);
3132 out->channel_map_param.channel_map[i] = channel_map_param->channel_map[i];
3133 }
3134 ret = 0;
3135exit:
3136 return ret;
3137}
Varun Balaraje49253e2017-07-06 19:48:56 +05303138
3139int audio_extn_utils_set_pan_scale_params(
3140 struct stream_out *out,
3141 struct mix_matrix_params *mm_params)
3142{
3143 int ret = -EINVAL, i = 0, j = 0;
3144
Varun Balaraj003ee752017-08-07 17:54:55 +05303145 if (mm_params == NULL || out == NULL) {
3146 ALOGE("%s:: Invalid mix matrix or out param", __func__);
Varun Balaraje49253e2017-07-06 19:48:56 +05303147 goto exit;
3148 }
3149
3150 if (mm_params->num_output_channels > MAX_CHANNELS_SUPPORTED ||
3151 mm_params->num_output_channels <= 0 ||
3152 mm_params->num_input_channels > MAX_CHANNELS_SUPPORTED ||
3153 mm_params->num_input_channels <= 0)
3154 goto exit;
3155
3156 out->pan_scale_params.num_output_channels = mm_params->num_output_channels;
3157 out->pan_scale_params.num_input_channels = mm_params->num_input_channels;
3158 out->pan_scale_params.has_output_channel_map =
3159 mm_params->has_output_channel_map;
3160 for (i = 0; i < mm_params->num_output_channels; i++)
3161 out->pan_scale_params.output_channel_map[i] =
3162 mm_params->output_channel_map[i];
3163
3164 out->pan_scale_params.has_input_channel_map =
3165 mm_params->has_input_channel_map;
3166 for (i = 0; i < mm_params->num_input_channels; i++)
3167 out->pan_scale_params.input_channel_map[i] =
3168 mm_params->input_channel_map[i];
3169
3170 out->pan_scale_params.has_mixer_coeffs = mm_params->has_mixer_coeffs;
3171 for (i = 0; i < mm_params->num_output_channels; i++)
3172 for (j = 0; j < mm_params->num_input_channels; j++) {
3173 //Convert the channel coefficient gains in Q14 format
3174 out->pan_scale_params.mixer_coeffs[i][j] =
3175 mm_params->mixer_coeffs[i][j] * (2 << 13);
3176 }
3177
3178 ret = platform_set_stream_pan_scale_params(out->dev->platform,
3179 out->pcm_device_id,
3180 out->pan_scale_params);
3181
3182exit:
3183 return ret;
3184}
3185
3186int audio_extn_utils_set_downmix_params(
3187 struct stream_out *out,
3188 struct mix_matrix_params *mm_params)
3189{
3190 int ret = -EINVAL, i = 0, j = 0;
3191 struct audio_usecase *usecase = NULL;
3192
Aniket Kumar Lataf9f246e2017-09-15 15:20:16 -07003193 if (mm_params == NULL || out == NULL) {
Varun Balaraj003ee752017-08-07 17:54:55 +05303194 ALOGE("%s:: Invalid mix matrix or out param", __func__);
Varun Balaraje49253e2017-07-06 19:48:56 +05303195 goto exit;
3196 }
3197
3198 if (mm_params->num_output_channels > MAX_CHANNELS_SUPPORTED ||
3199 mm_params->num_output_channels <= 0 ||
3200 mm_params->num_input_channels > MAX_CHANNELS_SUPPORTED ||
3201 mm_params->num_input_channels <= 0)
3202 goto exit;
3203
3204 usecase = get_usecase_from_list(out->dev, out->usecase);
Varun Balaraj003ee752017-08-07 17:54:55 +05303205 if (!usecase) {
3206 ALOGE("%s: Get usecase list failed!", __func__);
Aniket Kumar Lataf9f246e2017-09-15 15:20:16 -07003207 goto exit;
3208 }
Varun Balaraje49253e2017-07-06 19:48:56 +05303209 out->downmix_params.num_output_channels = mm_params->num_output_channels;
3210 out->downmix_params.num_input_channels = mm_params->num_input_channels;
3211
3212 out->downmix_params.has_output_channel_map =
3213 mm_params->has_output_channel_map;
3214 for (i = 0; i < mm_params->num_output_channels; i++) {
3215 out->downmix_params.output_channel_map[i] =
3216 mm_params->output_channel_map[i];
3217 }
3218
3219 out->downmix_params.has_input_channel_map =
3220 mm_params->has_input_channel_map;
3221 for (i = 0; i < mm_params->num_input_channels; i++)
3222 out->downmix_params.input_channel_map[i] =
3223 mm_params->input_channel_map[i];
3224
3225 out->downmix_params.has_mixer_coeffs = mm_params->has_mixer_coeffs;
3226 for (i = 0; i < mm_params->num_output_channels; i++)
Nikhil Latukar2e6f6242017-08-15 13:37:07 +05303227 for (j = 0; j < mm_params->num_input_channels; j++) {
3228 //Convert the channel coefficient gains in Q14 format
Varun Balaraje49253e2017-07-06 19:48:56 +05303229 out->downmix_params.mixer_coeffs[i][j] =
Nikhil Latukar2e6f6242017-08-15 13:37:07 +05303230 mm_params->mixer_coeffs[i][j] * (2 << 13);
3231 }
Varun Balaraje49253e2017-07-06 19:48:56 +05303232
3233 ret = platform_set_stream_downmix_params(out->dev->platform,
3234 out->pcm_device_id,
3235 usecase->out_snd_device,
3236 out->downmix_params);
3237
3238exit:
3239 return ret;
3240}
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05303241
3242bool audio_extn_utils_is_dolby_format(audio_format_t format)
3243{
3244 if (format == AUDIO_FORMAT_AC3 ||
3245 format == AUDIO_FORMAT_E_AC3 ||
3246 format == AUDIO_FORMAT_E_AC3_JOC)
3247 return true;
3248 else
3249 return false;
3250}
3251
`Deeraj Soman676c2702017-09-18 19:25:53 +05303252int audio_extn_utils_get_bit_width_from_string(const char *id_string)
3253{
3254 int i;
3255 const mixer_config_lookup mixer_bitwidth_config[] = {{"S24_3LE", 24},
3256 {"S32_LE", 32},
3257 {"S24_LE", 24},
3258 {"S16_LE", 16}};
3259 int num_configs = sizeof(mixer_bitwidth_config) / sizeof(mixer_bitwidth_config[0]);
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05303260
`Deeraj Soman676c2702017-09-18 19:25:53 +05303261 for (i = 0; i < num_configs; i++) {
3262 if (!strcmp(id_string, mixer_bitwidth_config[i].id_string))
3263 return mixer_bitwidth_config[i].value;
3264 }
3265
3266 return -EINVAL;
3267}
3268
3269int audio_extn_utils_get_sample_rate_from_string(const char *id_string)
3270{
3271 int i;
Weiyin Jiang30f9ab52020-11-11 11:37:21 +08003272 const mixer_config_lookup mixer_samplerate_config[] = {{"KHZ_8", 8000},
3273 {"KHZ_11P025", 11025},
3274 {"KHZ_16", 16000},
3275 {"KHZ_22P05", 22050},
3276 {"KHZ_32", 32000},
`Deeraj Soman676c2702017-09-18 19:25:53 +05303277 {"KHZ_48", 48000},
3278 {"KHZ_96", 96000},
3279 {"KHZ_144", 144000},
3280 {"KHZ_192", 192000},
3281 {"KHZ_384", 384000},
3282 {"KHZ_44P1", 44100},
3283 {"KHZ_88P2", 88200},
3284 {"KHZ_176P4", 176400},
3285 {"KHZ_352P8", 352800}};
3286 int num_configs = sizeof(mixer_samplerate_config) / sizeof(mixer_samplerate_config[0]);
3287
3288 for (i = 0; i < num_configs; i++) {
3289 if (!strcmp(id_string, mixer_samplerate_config[i].id_string))
3290 return mixer_samplerate_config[i].value;
3291 }
3292
3293 return -EINVAL;
3294}
3295
3296int audio_extn_utils_get_channels_from_string(const char *id_string)
3297{
3298 int i;
3299 const mixer_config_lookup mixer_channels_config[] = {{"One", 1},
3300 {"Two", 2},
3301 {"Three",3},
3302 {"Four", 4},
3303 {"Five", 5},
3304 {"Six", 6},
3305 {"Seven", 7},
3306 {"Eight", 8}};
3307 int num_configs = sizeof(mixer_channels_config) / sizeof(mixer_channels_config[0]);
3308
3309 for (i = 0; i < num_configs; i++) {
3310 if (!strcmp(id_string, mixer_channels_config[i].id_string))
3311 return mixer_channels_config[i].value;
3312 }
3313
3314 return -EINVAL;
3315}
Surendar karka30569792018-05-08 12:02:21 +05303316
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08003317void audio_extn_utils_release_snd_device(snd_device_t snd_device)
3318{
3319 audio_extn_dev_arbi_release(snd_device);
3320 audio_extn_sound_trigger_update_device_status(snd_device,
3321 ST_EVENT_SND_DEVICE_FREE);
3322 audio_extn_listen_update_device_status(snd_device,
3323 LISTEN_EVENT_SND_DEVICE_FREE);
3324}
3325
Weiyin Jiangd5718bd2019-09-04 16:52:08 +08003326int audio_extn_utils_get_license_params(
3327 const struct audio_device *adev,
3328 struct audio_license_params *license_params)
Surendar karka30569792018-05-08 12:02:21 +05303329{
3330 if(!license_params)
3331 return -EINVAL;
Weiyin Jiangd5718bd2019-09-04 16:52:08 +08003332
3333 return platform_get_license_by_product(adev->platform,
3334 (const char*)license_params->product, &license_params->key, license_params->license);
Surendar karka30569792018-05-08 12:02:21 +05303335}
3336
Aalique Grahame22e49102018-12-18 14:23:57 -08003337int audio_extn_utils_send_app_type_gain(struct audio_device *adev,
3338 int app_type,
3339 int *gain)
3340{
3341 int gain_cfg[4];
3342 const char *mixer_ctl_name = "App Type Gain";
3343 struct mixer_ctl *ctl;
3344 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
3345 if (!ctl) {
3346 ALOGE("%s: Could not get volume ctl mixer %s", __func__,
3347 mixer_ctl_name);
3348 return -EINVAL;
3349 }
3350 gain_cfg[0] = 0;
3351 gain_cfg[1] = app_type;
3352 gain_cfg[2] = gain[0];
3353 gain_cfg[3] = gain[1];
3354 ALOGV("%s app_type %d l(%d) r(%d)", __func__, app_type, gain[0], gain[1]);
3355 return mixer_ctl_set_array(ctl, gain_cfg,
3356 sizeof(gain_cfg)/sizeof(gain_cfg[0]));
3357}
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -07003358
Weiyin Jiangd5718bd2019-09-04 16:52:08 +08003359static void vndk_fwk_init()
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -07003360{
Weiyin Jiangd5718bd2019-09-04 16:52:08 +08003361 if (mVndkFwk.lib_handle != NULL)
3362 return;
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -07003363
Weiyin Jiangd5718bd2019-09-04 16:52:08 +08003364 mVndkFwk.lib_handle = dlopen(VNDK_FWK_LIB_PATH, RTLD_NOW);
3365 if (mVndkFwk.lib_handle == NULL) {
3366 ALOGW("%s: failed to dlopen VNDK_FWK_LIB %s", __func__, strerror(errno));
3367 return;
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -07003368 }
3369
Weiyin Jiangd5718bd2019-09-04 16:52:08 +08003370 *(void **)(&mVndkFwk.isVendorEnhancedFwk) =
3371 dlsym(mVndkFwk.lib_handle, "isRunningWithVendorEnhancedFramework");
3372 if (mVndkFwk.isVendorEnhancedFwk == NULL) {
3373 ALOGW("%s: dlsym failed %s", __func__, strerror(errno));
3374 if (mVndkFwk.lib_handle) {
3375 dlclose(mVndkFwk.lib_handle);
3376 mVndkFwk.lib_handle = NULL;
3377 }
3378 return;
3379 }
3380
3381
3382 *(void **)(&mVndkFwk.getVendorEnhancedInfo) =
3383 dlsym(mVndkFwk.lib_handle, "getVendorEnhancedInfo");
3384 if (mVndkFwk.getVendorEnhancedInfo == NULL) {
3385 ALOGW("%s: dlsym failed %s", __func__, strerror(errno));
3386 if (mVndkFwk.lib_handle) {
3387 dlclose(mVndkFwk.lib_handle);
3388 mVndkFwk.lib_handle = NULL;
3389 }
3390 }
3391
3392 return;
3393}
3394
3395bool audio_extn_utils_is_vendor_enhanced_fwk()
3396{
3397 static int is_vendor_enhanced_fwk = -EINVAL;
3398 if (is_vendor_enhanced_fwk != -EINVAL)
3399 return (bool)is_vendor_enhanced_fwk;
3400
3401 vndk_fwk_init();
3402
3403 if (mVndkFwk.isVendorEnhancedFwk != NULL) {
3404 is_vendor_enhanced_fwk = mVndkFwk.isVendorEnhancedFwk();
3405 ALOGW("%s: is_vendor_enhanced_fwk %d", __func__, is_vendor_enhanced_fwk);
3406 } else {
3407 is_vendor_enhanced_fwk = 0;
3408 ALOGW("%s: default to non enhanced_fwk config", __func__);
3409 }
3410
3411 return (bool)is_vendor_enhanced_fwk;
3412}
3413
3414int audio_extn_utils_get_vendor_enhanced_info()
3415{
3416 static int vendor_enhanced_info = -EINVAL;
3417 if (vendor_enhanced_info != -EINVAL)
3418 return vendor_enhanced_info;
3419
3420 vndk_fwk_init();
3421
3422 if (mVndkFwk.getVendorEnhancedInfo != NULL) {
3423 vendor_enhanced_info = mVndkFwk.getVendorEnhancedInfo();
3424 ALOGW("%s: vendor_enhanced_info 0x%x", __func__, vendor_enhanced_info);
3425 } else {
3426 vendor_enhanced_info = 0x0;
3427 ALOGW("%s: default to vendor_enhanced_info 0x0", __func__);
3428 }
3429
3430 return vendor_enhanced_info;
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -07003431}
Deeraj Soman14230922019-01-30 16:39:30 +05303432
Deeraj Somanfa377bf2019-02-06 12:57:59 +05303433int audio_extn_utils_get_perf_mode_flag(void)
3434{
3435#ifdef COMPRESSED_PERF_MODE_FLAG
3436 return COMPRESSED_PERF_MODE_FLAG;
3437#else
3438 return 0;
3439#endif
3440}
3441
Deeraj Soman14230922019-01-30 16:39:30 +05303442size_t audio_extn_utils_get_input_buffer_size(uint32_t sample_rate,
3443 audio_format_t format,
3444 int channel_count,
3445 int64_t duration_ms,
3446 bool is_low_latency)
3447{
3448 size_t size = 0;
3449 size_t capture_duration = AUDIO_CAPTURE_PERIOD_DURATION_MSEC;
3450 uint32_t bytes_per_period_sample = 0;
3451
3452
3453 if (audio_extn_utils_check_input_parameters(sample_rate, format, channel_count) != 0)
3454 return 0;
3455
3456 if (duration_ms >= MIN_OFFLOAD_BUFFER_DURATION_MS && duration_ms <= MAX_OFFLOAD_BUFFER_DURATION_MS)
3457 capture_duration = duration_ms;
3458
3459 size = (sample_rate * capture_duration) / 1000;
3460 if (is_low_latency)
3461 size = LOW_LATENCY_CAPTURE_PERIOD_SIZE;
3462
3463
3464 bytes_per_period_sample = audio_bytes_per_sample(format) * channel_count;
3465 size *= bytes_per_period_sample;
3466
3467 /* make sure the size is multiple of 32 bytes and additionally multiple of
3468 * the frame_size (required for 24bit samples and non-power-of-2 channel counts)
3469 * At 48 kHz mono 16-bit PCM:
3470 * 5.000 ms = 240 frames = 15*16*1*2 = 480, a whole multiple of 32 (15)
3471 * 3.333 ms = 160 frames = 10*16*1*2 = 320, a whole multiple of 32 (10)
3472 *
3473 * The loop reaches result within 32 iterations, as initial size is
3474 * already a multiple of frame_size
3475 */
3476 size = audio_extn_utils_nearest_multiple(size, audio_extn_utils_lcm(32, bytes_per_period_sample));
3477
3478 return size;
3479}
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07003480
3481int audio_extn_utils_hash_fn(void *key)
3482{
3483 return (int)key;
3484}
3485
3486bool audio_extn_utils_hash_eq(void *key1, void *key2)
3487{
3488 return (key1 == key2);
3489}