blob: be20e129530e0724fe60bf3dd7fa629606f640a8 [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;
sasevsc55b3f32023-06-14 15:26:19 +05301053 size_t app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT] = {0};
1054 int len = 0, rc = 0;
Aalique Grahame22e49102018-12-18 14:23:57 -08001055 int snd_device_be_idx = -1;
1056
1057 if (stream_type == PCM_PLAYBACK) {
1058 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
1059 "Audio Stream %d App Type Cfg", pcm_device_id);
1060 } else if (stream_type == PCM_CAPTURE) {
1061 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
1062 "Audio Stream Capture %d App Type Cfg", pcm_device_id);
1063 }
1064
1065 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
1066 if (!ctl) {
1067 ALOGE("%s: Could not get ctl for mixer cmd - %s",
1068 __func__, mixer_ctl_name);
1069 rc = -EINVAL;
1070 goto exit;
1071 }
1072 app_type_cfg[len++] = app_type;
1073 app_type_cfg[len++] = acdb_dev_id;
1074 app_type_cfg[len++] = sample_rate;
1075
1076 snd_device_be_idx = platform_get_snd_device_backend_index(snd_device);
1077 if (snd_device_be_idx > 0)
1078 app_type_cfg[len++] = snd_device_be_idx;
1079 ALOGV("%s: stream type %d app_type %d, acdb_dev_id %d "
1080 "sample rate %d, snd_device_be_idx %d",
1081 __func__, stream_type, app_type, acdb_dev_id, sample_rate,
1082 snd_device_be_idx);
1083 mixer_ctl_set_array(ctl, app_type_cfg, len);
1084
1085exit:
1086 return rc;
1087}
1088
1089static int audio_extn_utils_send_app_type_cfg_hfp(struct audio_device *adev,
1090 struct audio_usecase *usecase)
1091{
1092 int pcm_device_id, acdb_dev_id = 0, snd_device = usecase->out_snd_device;
1093 int32_t sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
1094 int app_type = 0, rc = 0;
Derek Chenf7092792017-05-23 12:23:53 -04001095 bool is_bus_dev_usecase = false;
Aalique Grahame22e49102018-12-18 14:23:57 -08001096
1097 ALOGV("%s", __func__);
1098
1099 if (usecase->type != PCM_HFP_CALL) {
Derek Chenf7092792017-05-23 12:23:53 -04001100 ALOGV("%s: not a HFP path, no need to cfg app type", __func__);
Aalique Grahame22e49102018-12-18 14:23:57 -08001101 rc = 0;
1102 goto exit_send_app_type_cfg;
1103 }
1104 if ((usecase->id != USECASE_AUDIO_HFP_SCO) &&
Derek Chenf7092792017-05-23 12:23:53 -04001105 (usecase->id != USECASE_AUDIO_HFP_SCO_WB) &&
1106 (usecase->id != USECASE_AUDIO_HFP_SCO_DOWNLINK) &&
1107 (usecase->id != USECASE_AUDIO_HFP_SCO_WB_DOWNLINK)) {
1108 ALOGV("%s: a usecase where app type cfg is not required", __func__);
Aalique Grahame22e49102018-12-18 14:23:57 -08001109 rc = 0;
1110 goto exit_send_app_type_cfg;
1111 }
1112
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08001113 if (compare_device_type(&usecase->device_list, AUDIO_DEVICE_OUT_BUS))
Derek Chenf7092792017-05-23 12:23:53 -04001114 is_bus_dev_usecase = true;
1115
Aalique Grahame22e49102018-12-18 14:23:57 -08001116 snd_device = usecase->out_snd_device;
1117 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_PLAYBACK);
1118
1119 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
1120 if (acdb_dev_id < 0) {
1121 ALOGE("%s: Couldn't get the acdb dev id", __func__);
1122 rc = -EINVAL;
1123 goto exit_send_app_type_cfg;
1124 }
1125
sriram kumar53c4c972021-02-18 16:49:33 +05301126 /*
1127 * Value of afe_loopback gets read based on the property defined in
1128 * audio_platform_info.xml. If afe loopback is set then do not execute
1129 * session 1 path as app type mixer control will not be created for
1130 * afe loopback
1131 */
1132
Aalique Grahame22e49102018-12-18 14:23:57 -08001133 if (usecase->type == PCM_HFP_CALL) {
sriram kumar53c4c972021-02-18 16:49:33 +05301134 if (!(platform_get_is_afe_loopback_enabled(adev->platform))) {
1135 /* config HFP session:1 playback path */
1136 if (is_bus_dev_usecase) {
1137 app_type = usecase->out_app_type_cfg.app_type;
1138 sample_rate= usecase->out_app_type_cfg.sample_rate;
1139 } else {
1140 snd_device = SND_DEVICE_NONE; // use legacy behavior
1141 app_type = platform_get_default_app_type_v2(adev->platform, PCM_PLAYBACK);
1142 sample_rate= CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
Derek Chenf7092792017-05-23 12:23:53 -04001143 }
sriram kumar53c4c972021-02-18 16:49:33 +05301144 rc = set_stream_app_type_mixer_ctrl(adev, pcm_device_id, app_type,
1145 acdb_dev_id, sample_rate,
1146 PCM_PLAYBACK,
1147 snd_device);
1148 if (rc < 0)
1149 goto exit_send_app_type_cfg;
1150
1151 /* config HFP session:1 capture path */
1152 if (is_bus_dev_usecase) {
1153 snd_device = usecase->in_snd_device;
1154 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_CAPTURE);
1155 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
1156 if (acdb_dev_id < 0) {
1157 ALOGE("%s: Couldn't get the acdb dev id", __func__);
1158 rc = -EINVAL;
1159 goto exit_send_app_type_cfg;
1160 }
1161 app_type = usecase->in_app_type_cfg.app_type;
1162 sample_rate= usecase->in_app_type_cfg.sample_rate;
1163 } else {
1164 snd_device = SND_DEVICE_NONE; // use legacy behavior
1165 app_type = platform_get_default_app_type_v2(adev->platform, PCM_CAPTURE);
1166 }
1167 rc = set_stream_app_type_mixer_ctrl(adev, pcm_device_id, app_type,
1168 acdb_dev_id, sample_rate,
1169 PCM_CAPTURE,
1170 snd_device);
1171 if (rc < 0)
1172 goto exit_send_app_type_cfg;
1173
1174 if (is_bus_dev_usecase)
1175 goto exit_send_app_type_cfg;
Derek Chenf7092792017-05-23 12:23:53 -04001176 }
Aalique Grahame22e49102018-12-18 14:23:57 -08001177 /* config HFP session:2 capture path */
1178 pcm_device_id = HFP_ASM_RX_TX;
1179 snd_device = usecase->in_snd_device;
1180 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
1181 if (acdb_dev_id <= 0) {
1182 ALOGE("%s: Couldn't get the acdb dev id", __func__);
1183 rc = -EINVAL;
1184 goto exit_send_app_type_cfg;
1185 }
1186 app_type = platform_get_default_app_type_v2(adev->platform, PCM_CAPTURE);
1187 rc = set_stream_app_type_mixer_ctrl(adev, pcm_device_id, app_type,
1188 acdb_dev_id, sample_rate, PCM_CAPTURE,
sriram kumar53c4c972021-02-18 16:49:33 +05301189 usecase->in_snd_device);
Aalique Grahame22e49102018-12-18 14:23:57 -08001190 if (rc < 0)
1191 goto exit_send_app_type_cfg;
1192
1193 /* config HFP session:2 playback path */
1194 app_type = platform_get_default_app_type_v2(adev->platform, PCM_PLAYBACK);
1195 rc = set_stream_app_type_mixer_ctrl(adev, pcm_device_id, app_type,
1196 acdb_dev_id, sample_rate,
sriram kumar53c4c972021-02-18 16:49:33 +05301197 PCM_PLAYBACK, usecase->out_snd_device);
Aalique Grahame22e49102018-12-18 14:23:57 -08001198 if (rc < 0)
1199 goto exit_send_app_type_cfg;
1200 }
1201
1202 rc = 0;
1203exit_send_app_type_cfg:
1204 return rc;
1205}
1206
Derek Chena30a5f42019-12-03 11:17:09 -05001207int audio_extn_utils_send_app_type_cfg_icc(struct audio_device *adev,
1208 struct audio_usecase *usecase)
1209{
1210 int pcm_device_id, acdb_dev_id = 0, snd_device = usecase->out_snd_device;
1211 int32_t sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
1212 int app_type = 0, rc = 0;
1213
1214 ALOGV("%s", __func__);
1215
1216 if (usecase->type != ICC_CALL) {
1217 ALOGV("%s: not an ICC path, no need to cfg app type", __func__);
1218 rc = 0;
1219 goto exit_send_app_type_cfg;
1220 }
1221 if (usecase->id != USECASE_ICC_CALL) {
1222 ALOGV("%s: a usecase where app type cfg is not required", __func__);
1223 rc = 0;
1224 goto exit_send_app_type_cfg;
1225 }
1226
1227 snd_device = usecase->out_snd_device;
1228 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_PLAYBACK);
1229 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
1230 if (acdb_dev_id < 0) {
1231 ALOGE("%s: Couldn't get the acdb dev id", __func__);
1232 rc = -EINVAL;
1233 goto exit_send_app_type_cfg;
1234 }
1235 /* config ICC session: playback path */
1236 app_type = usecase->out_app_type_cfg.app_type;
1237 sample_rate= usecase->out_app_type_cfg.sample_rate;
1238
1239 rc = set_stream_app_type_mixer_ctrl(adev, pcm_device_id, app_type,
1240 acdb_dev_id, sample_rate,
1241 PCM_PLAYBACK,
1242 snd_device);
1243 if (rc < 0)
1244 goto exit_send_app_type_cfg;
1245
1246 /* config ICC session: capture path */
1247 snd_device = usecase->in_snd_device;
1248 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_CAPTURE);
1249 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
1250 if (acdb_dev_id < 0) {
1251 ALOGE("%s: Couldn't get the acdb dev id", __func__);
1252 rc = -EINVAL;
1253 goto exit_send_app_type_cfg;
1254 }
1255 app_type = usecase->in_app_type_cfg.app_type;
1256 sample_rate= usecase->in_app_type_cfg.sample_rate;
1257 rc = set_stream_app_type_mixer_ctrl(adev, pcm_device_id, app_type,
1258 acdb_dev_id, sample_rate,
1259 PCM_CAPTURE,
1260 snd_device);
1261exit_send_app_type_cfg:
1262 return rc;
1263}
1264
Fei Tongaffdf732020-02-20 20:39:05 +08001265static int audio_extn_utils_send_app_type_cfg_synth(struct audio_device *adev,
1266 struct audio_usecase *usecase)
1267{
1268 int pcm_device_id, acdb_dev_id = 0, snd_device = usecase->out_snd_device;
1269 int32_t sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
1270 int app_type = 0, rc = 0;
1271 bool is_bus_dev_usecase = false;
1272
1273 ALOGV("%s", __func__);
1274
1275 if (usecase->type != SYNTH_LOOPBACK) {
1276 ALOGV("%s: not a SYNTH path, no need to cfg app type", __func__);
1277 rc = 0;
1278 goto exit_send_app_type_cfg;
1279 }
1280 if (usecase->id != USECASE_AUDIO_PLAYBACK_SYNTHESIZER) {
1281 ALOGV("%s: a usecase where app type cfg is not required", __func__);
1282 rc = 0;
1283 goto exit_send_app_type_cfg;
1284 }
1285
1286 if (compare_device_type(&usecase->device_list, AUDIO_DEVICE_OUT_BUS)) {
1287 is_bus_dev_usecase = true;
1288 }
1289
1290 snd_device = usecase->out_snd_device;
1291 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_PLAYBACK);
1292
1293 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
1294 if (acdb_dev_id < 0) {
1295 ALOGE("%s: Couldn't get the acdb dev id", __func__);
1296 rc = -EINVAL;
1297 goto exit_send_app_type_cfg;
1298 }
1299
1300 if (usecase->type == SYNTH_LOOPBACK) {
1301 /* config SYNTH session: playback path */
1302 if (is_bus_dev_usecase) {
1303 app_type = usecase->out_app_type_cfg.app_type;
1304 sample_rate= usecase->out_app_type_cfg.sample_rate;
1305 } else {
1306 snd_device = SND_DEVICE_NONE; // use legacy behavior
1307 app_type = platform_get_default_app_type_v2(adev->platform, PCM_PLAYBACK);
1308 sample_rate= CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
1309 }
1310 rc = set_stream_app_type_mixer_ctrl(adev, pcm_device_id, app_type,
1311 acdb_dev_id, sample_rate,
1312 PCM_PLAYBACK,
1313 snd_device);
1314 if (rc < 0)
1315 goto exit_send_app_type_cfg;
1316 }
1317
1318 rc = 0;
1319exit_send_app_type_cfg:
1320 return rc;
1321}
1322
Zhou Song06761dd2019-04-28 18:08:17 +08001323int audio_extn_utils_get_app_sample_rate_for_device(
1324 struct audio_device *adev,
1325 struct audio_usecase *usecase, int snd_device)
1326{
1327 char value[PROPERTY_VALUE_MAX] = {0};
1328 int sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Krishna Kishor Jhac1542b22022-08-22 13:12:09 +05301329#ifdef PLATFORM_AUTO
Kogara Naveen Kumar51bb57d2022-02-17 16:40:06 +05301330 int acdb_dev_id;
Krishna Kishor Jhac1542b22022-08-22 13:12:09 +05301331#endif
Zhou Song06761dd2019-04-28 18:08:17 +08001332
1333 if ((usecase->type == PCM_PLAYBACK) && (usecase->stream.out != NULL)) {
1334 property_get("vendor.audio.playback.mch.downsample",value,"");
1335 if (!strncmp("true", value, sizeof("true"))) {
1336 if ((popcount(usecase->stream.out->channel_mask) > 2) &&
1337 (usecase->stream.out->app_type_cfg.sample_rate > CODEC_BACKEND_DEFAULT_SAMPLE_RATE) &&
1338 !(usecase->stream.out->flags &
1339 (audio_output_flags_t)AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH))
1340 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
1341 }
1342
1343 if (usecase->id == USECASE_AUDIO_PLAYBACK_VOIP) {
1344 usecase->stream.out->app_type_cfg.sample_rate = usecase->stream.out->sample_rate;
Zhou Song06761dd2019-04-28 18:08:17 +08001345 } else if ((snd_device == SND_DEVICE_OUT_HDMI ||
1346 snd_device == SND_DEVICE_OUT_USB_HEADSET ||
1347 snd_device == SND_DEVICE_OUT_DISPLAY_PORT) &&
1348 (usecase->stream.out->sample_rate >= OUTPUT_SAMPLING_RATE_44100)) {
1349 /*
1350 * To best utlize DSP, check if the stream sample rate is supported/multiple of
1351 * configured device sample rate, if not update the COPP rate to be equal to the
1352 * device sample rate, else open COPP at stream sample rate
1353 */
1354 platform_check_and_update_copp_sample_rate(adev->platform, snd_device,
1355 usecase->stream.out->sample_rate,
1356 &usecase->stream.out->app_type_cfg.sample_rate);
Lakshman Chaluvaraju5d0894e2021-01-27 23:22:58 +05301357 } else if (snd_device == SND_DEVICE_OUT_BT_A2DP) {
Zhou Song06761dd2019-04-28 18:08:17 +08001358 /*
1359 * For a2dp playback get encoder sampling rate and set copp sampling rate,
1360 * for bit width use the stream param only.
1361 */
1362 audio_extn_a2dp_get_enc_sample_rate(&usecase->stream.out->app_type_cfg.sample_rate);
1363 ALOGI("%s using %d sample rate rate for A2DP CoPP",
1364 __func__, usecase->stream.out->app_type_cfg.sample_rate);
sriram kumar68e21132021-06-25 19:27:20 +05301365 } else if ((((snd_device != SND_DEVICE_OUT_HEADPHONES_44_1 &&
1366 !audio_is_this_native_usecase(usecase)) &&
1367 usecase->stream.out->sample_rate == OUTPUT_SAMPLING_RATE_44100) ||
1368 (usecase->stream.out->sample_rate < OUTPUT_SAMPLING_RATE_44100)) ||
1369 (compare_device_type(&usecase->stream.out->device_list,AUDIO_DEVICE_OUT_SPEAKER))) {
Kogara Naveen Kumar51bb57d2022-02-17 16:40:06 +05301370 if (!((compare_device_type(&usecase->device_list, AUDIO_DEVICE_OUT_BUS)) && ((usecase->stream.out->flags &
1371 (audio_output_flags_t)AUDIO_OUTPUT_FLAG_SYS_NOTIFICATION) || (usecase->stream.out->flags &
Kogara Naveen Kumare88ee942022-12-22 17:01:12 +05301372 (audio_output_flags_t)AUDIO_OUTPUT_FLAG_PHONE) || (usecase->stream.out->flags &
1373 (audio_output_flags_t)AUDIO_OUTPUT_FLAG_NAV_GUIDANCE) || (usecase->stream.out->flags &
1374 (audio_output_flags_t)AUDIO_OUTPUT_FLAG_ALERTS)))) {
Kogara Naveen Kumar51bb57d2022-02-17 16:40:06 +05301375 /* Reset to default if no native stream is active or default device is speaker*/
1376 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
1377 }
Zhou Song06761dd2019-04-28 18:08:17 +08001378 }
1379 audio_extn_btsco_get_sample_rate(snd_device, &usecase->stream.out->app_type_cfg.sample_rate);
1380 sample_rate = usecase->stream.out->app_type_cfg.sample_rate;
1381
1382 if (((usecase->stream.out->format == AUDIO_FORMAT_E_AC3) ||
1383 (usecase->stream.out->format == AUDIO_FORMAT_E_AC3_JOC) ||
1384 (usecase->stream.out->format == AUDIO_FORMAT_DOLBY_TRUEHD))
1385 && audio_extn_passthru_is_passthrough_stream(usecase->stream.out)
1386 && !audio_extn_passthru_is_convert_supported(adev, usecase->stream.out)) {
1387 sample_rate = sample_rate * 4;
1388 if (sample_rate > HDMI_PASSTHROUGH_MAX_SAMPLE_RATE)
1389 sample_rate = HDMI_PASSTHROUGH_MAX_SAMPLE_RATE;
1390 }
1391 } else if (usecase->type == PCM_CAPTURE) {
1392 if (usecase->stream.in != NULL) {
Revathi Uddarajud9f23d92020-07-27 10:55:06 +05301393 if (usecase->id == USECASE_AUDIO_RECORD_VOIP
1394 || usecase->id == USECASE_AUDIO_RECORD_VOIP_LOW_LATENCY)
Zhou Song06761dd2019-04-28 18:08:17 +08001395 usecase->stream.in->app_type_cfg.sample_rate = usecase->stream.in->sample_rate;
1396 if (voice_is_in_call_rec_stream(usecase->stream.in)) {
1397 audio_extn_btsco_get_sample_rate(usecase->in_snd_device,
1398 &usecase->stream.in->app_type_cfg.sample_rate);
Zhou Songab94f062021-05-12 13:11:17 +08001399 } if (SND_DEVICE_IN_BT_A2DP == snd_device) {
1400 audio_extn_a2dp_get_dec_sample_rate(&usecase->stream.in->app_type_cfg.sample_rate);
Zhou Song06761dd2019-04-28 18:08:17 +08001401 } else {
1402 audio_extn_btsco_get_sample_rate(snd_device,
1403 &usecase->stream.in->app_type_cfg.sample_rate);
1404 }
1405 sample_rate = usecase->stream.in->app_type_cfg.sample_rate;
1406 } else if (usecase->id == USECASE_AUDIO_SPKR_CALIB_TX) {
Sujin Panickerb6d76262019-09-09 10:55:38 +05301407 if ((property_get("vendor.audio.spkr_prot.tx.sampling_rate", value, NULL) > 0))
1408 sample_rate = atoi(value);
1409 else
1410 sample_rate = SAMPLE_RATE_8000;
Zhou Song06761dd2019-04-28 18:08:17 +08001411 }
Kogara Naveen Kumar51bb57d2022-02-17 16:40:06 +05301412
1413 /* ECNR module in DSP does not support 24 KHz sample rate. As a workaround,
1414 run ADM at 48 KHz when ECNR is enabled in ACDB topology (e.g. device id = 95)
1415 */
Krishna Kishor Jhac1542b22022-08-22 13:12:09 +05301416#ifdef PLATFORM_AUTO
Kogara Naveen Kumar51bb57d2022-02-17 16:40:06 +05301417 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
1418 if (sample_rate == ECNS_UNSUPPORTED_CAPTURE_SAMPLE_RATE_FOR_ADM && acdb_dev_id == ECNS_USE_CASE_ACDB_DEV_ID) {
1419 sample_rate = ECNS_SUPPORTED_CAPTURE_SAMPLE_RATE_FOR_ADM;
1420 ALOGD("%s: update sample rate from 24K to 48K to support ECNR in PCM_CAPTURE, sample_rate=%d",__func__,sample_rate);
1421 }
Krishna Kishor Jhac1542b22022-08-22 13:12:09 +05301422#endif
Zhou Song06761dd2019-04-28 18:08:17 +08001423 } else if (usecase->type == TRANSCODE_LOOPBACK_RX) {
1424 sample_rate = usecase->stream.inout->out_config.sample_rate;
1425 }
1426 return sample_rate;
1427}
1428
Siena Richard7c2db772016-12-21 11:32:34 -08001429static int send_app_type_cfg_for_device(struct audio_device *adev,
1430 struct audio_usecase *usecase,
1431 int split_snd_device)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001432{
1433 char mixer_ctl_name[MAX_LENGTH_MIXER_CONTROL_IN_INT];
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301434 size_t app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT] = {0};
1435 int len = 0, rc;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001436 struct mixer_ctl *ctl;
Siena Richard7c2db772016-12-21 11:32:34 -08001437 int pcm_device_id = 0, acdb_dev_id, app_type;
1438 int snd_device = split_snd_device, snd_device_be_idx = -1;
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +05301439 int32_t sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Varun Balaraje49253e2017-07-06 19:48:56 +05301440 struct streams_io_cfg *s_info = NULL;
1441 struct listnode *node = NULL;
Nikhil Laturkarac4a7492017-08-31 18:27:19 +05301442 int bd_app_type = 0;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001443
Siena Richard7c2db772016-12-21 11:32:34 -08001444 ALOGV("%s: usecase->out_snd_device %s, usecase->in_snd_device %s, split_snd_device %s",
1445 __func__, platform_get_snd_device_name(usecase->out_snd_device),
1446 platform_get_snd_device_name(usecase->in_snd_device),
1447 platform_get_snd_device_name(split_snd_device));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001448
Siddartha Shaik343abc62017-08-08 11:15:25 +05301449 if (usecase->type != PCM_PLAYBACK && usecase->type != PCM_CAPTURE &&
Surendar Karka93cd25a2018-08-28 14:21:37 +05301450 usecase->type != TRANSCODE_LOOPBACK_RX) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301451 ALOGE("%s: not a playback/capture path, no need to cfg app type", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001452 rc = 0;
1453 goto exit_send_app_type_cfg;
1454 }
1455 if ((usecase->id != USECASE_AUDIO_PLAYBACK_DEEP_BUFFER) &&
Vignesh Kulothungan0a2eff02019-07-11 16:30:13 -07001456 (usecase->id != USECASE_AUDIO_PLAYBACK_WITH_HAPTICS) &&
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001457 (usecase->id != USECASE_AUDIO_PLAYBACK_LOW_LATENCY) &&
1458 (usecase->id != USECASE_AUDIO_PLAYBACK_MULTI_CH) &&
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -08001459 (usecase->id != USECASE_AUDIO_PLAYBACK_ULL) &&
Vikram Panduranga93f080e2017-06-07 18:16:14 -07001460 (usecase->id != USECASE_AUDIO_PLAYBACK_VOIP) &&
Surendar Karka93cd25a2018-08-28 14:21:37 +05301461 (usecase->id != USECASE_AUDIO_TRANSCODE_LOOPBACK_RX) &&
Varun Balaraje49253e2017-07-06 19:48:56 +05301462 (!is_interactive_usecase(usecase->id)) &&
Srikanth Uyyala9d551402015-08-25 16:03:42 +05301463 (!is_offload_usecase(usecase->id)) &&
Derek Chenf6318be2017-06-12 17:16:24 -04001464 (usecase->type != PCM_CAPTURE) &&
1465 (!audio_extn_auto_hal_is_bus_device_usecase(usecase->id))) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301466 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 -07001467 rc = 0;
1468 goto exit_send_app_type_cfg;
1469 }
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -08001470
1471 //if VR is active then only send the mixer control
1472 if (usecase->id == USECASE_AUDIO_PLAYBACK_ULL && !audio_is_vr_mode_on(adev)) {
1473 ALOGI("ULL doesnt need sending app type cfg, returning");
1474 rc = 0;
1475 goto exit_send_app_type_cfg;
1476 }
1477
Surendar Karka93cd25a2018-08-28 14:21:37 +05301478 if (usecase->type == PCM_PLAYBACK || usecase->type == TRANSCODE_LOOPBACK_RX) {
Ben Romberger1fafdde2015-09-09 19:43:15 -07001479 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_PLAYBACK);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301480 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
1481 "Audio Stream %d App Type Cfg", pcm_device_id);
Ben Romberger1fafdde2015-09-09 19:43:15 -07001482 } else if (usecase->type == PCM_CAPTURE) {
Ben Romberger1fafdde2015-09-09 19:43:15 -07001483 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_CAPTURE);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301484 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
1485 "Audio Stream Capture %d App Type Cfg", pcm_device_id);
Srikanth Uyyala9d551402015-08-25 16:03:42 +05301486 }
Siena Richard7c2db772016-12-21 11:32:34 -08001487
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001488 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
1489 if (!ctl) {
1490 ALOGE("%s: Could not get ctl for mixer cmd - %s", __func__,
1491 mixer_ctl_name);
1492 rc = -EINVAL;
1493 goto exit_send_app_type_cfg;
1494 }
Aditya Bavanari701a6992017-03-30 19:17:16 +05301495 snd_device = platform_get_spkr_prot_snd_device(snd_device);
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301496 if (voice_is_in_call_rec_stream(usecase->stream.in) && usecase->type == PCM_CAPTURE) {
1497 snd_device_t voice_device = voice_get_incall_rec_snd_device(usecase->in_snd_device);
1498 acdb_dev_id = platform_get_snd_device_acdb_id(voice_device);
1499 ALOGV("acdb id for voice call use case %d", acdb_dev_id);
1500 } else {
1501 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
1502 }
Rohit Kumar1181b292017-01-31 18:07:17 +05301503 if (acdb_dev_id <= 0) {
1504 ALOGE("%s: Couldn't get the acdb dev id", __func__);
1505 rc = -EINVAL;
1506 goto exit_send_app_type_cfg;
1507 }
1508
Siena Richard7c2db772016-12-21 11:32:34 -08001509 snd_device_be_idx = platform_get_snd_device_backend_index(snd_device);
1510 if (snd_device_be_idx < 0) {
1511 ALOGE("%s: Couldn't get the backend index for snd device %s ret=%d",
1512 __func__, platform_get_snd_device_name(snd_device),
1513 snd_device_be_idx);
1514 }
1515
Zhou Song06761dd2019-04-28 18:08:17 +08001516 sample_rate = audio_extn_utils_get_app_sample_rate_for_device(adev, usecase, snd_device);
1517
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301518 if ((usecase->type == PCM_PLAYBACK) && (usecase->stream.out != NULL)) {
Varun Balaraje49253e2017-07-06 19:48:56 +05301519 /* Interactive streams are supported with only direct app type id.
1520 * Get Direct profile app type and use it for interactive streams
1521 */
1522 list_for_each(node, &adev->streams_output_cfg_list) {
1523 s_info = node_to_item(node, struct streams_io_cfg, list);
Aalique Grahame5cd12ff2017-10-19 10:57:00 -07001524 if (s_info->flags.out_flags == (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_BD |
Nikhil Laturkarac4a7492017-08-31 18:27:19 +05301525 AUDIO_OUTPUT_FLAG_DIRECT_PCM |
1526 AUDIO_OUTPUT_FLAG_DIRECT))
1527 bd_app_type = s_info->app_type_cfg.app_type;
Varun Balaraje49253e2017-07-06 19:48:56 +05301528 }
Aalique Grahame5cd12ff2017-10-19 10:57:00 -07001529 if (usecase->stream.out->flags == (audio_output_flags_t)AUDIO_OUTPUT_FLAG_INTERACTIVE)
Nikhil Laturkarac4a7492017-08-31 18:27:19 +05301530 app_type = bd_app_type;
Varun Balaraje49253e2017-07-06 19:48:56 +05301531 else
1532 app_type = usecase->stream.out->app_type_cfg.app_type;
Siena Richard7c2db772016-12-21 11:32:34 -08001533 app_type_cfg[len++] = app_type;
Mingming Yin21854652016-04-13 11:54:02 -07001534 app_type_cfg[len++] = acdb_dev_id;
Naresh Tanniru3a406772017-05-10 13:09:05 -07001535 app_type_cfg[len++] = sample_rate;
1536
Siena Richard7c2db772016-12-21 11:32:34 -08001537 if (snd_device_be_idx > 0)
1538 app_type_cfg[len++] = snd_device_be_idx;
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301539
Siena Richard7c2db772016-12-21 11:32:34 -08001540 ALOGI("%s PLAYBACK app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
1541 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301542
1543 } else if ((usecase->type == PCM_CAPTURE) && (usecase->stream.in != NULL)) {
Siena Richard7c2db772016-12-21 11:32:34 -08001544 app_type = usecase->stream.in->app_type_cfg.app_type;
1545 app_type_cfg[len++] = app_type;
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301546 app_type_cfg[len++] = acdb_dev_id;
Siena Richard7c2db772016-12-21 11:32:34 -08001547 app_type_cfg[len++] = sample_rate;
1548 if (snd_device_be_idx > 0)
1549 app_type_cfg[len++] = snd_device_be_idx;
1550 ALOGI("%s CAPTURE app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
1551 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301552 } else {
Siena Richard7c2db772016-12-21 11:32:34 -08001553 app_type = platform_get_default_app_type_v2(adev->platform, usecase->type);
Surendar Karka93cd25a2018-08-28 14:21:37 +05301554 if(usecase->type == TRANSCODE_LOOPBACK_RX) {
Siddartha Shaik343abc62017-08-08 11:15:25 +05301555 app_type = usecase->stream.inout->out_app_type_cfg.app_type;
1556 }
Siena Richard7c2db772016-12-21 11:32:34 -08001557 app_type_cfg[len++] = app_type;
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301558 app_type_cfg[len++] = acdb_dev_id;
1559 app_type_cfg[len++] = sample_rate;
Siena Richard7c2db772016-12-21 11:32:34 -08001560 if (snd_device_be_idx > 0)
1561 app_type_cfg[len++] = snd_device_be_idx;
1562 ALOGI("%s default app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
1563 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Ashish Jainc02430d2016-01-04 10:42:43 +05301564 }
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301565
Siddartha Shaik343abc62017-08-08 11:15:25 +05301566 if(ctl)
1567 mixer_ctl_set_array(ctl, app_type_cfg, len);
Zhou Songd9bd9302020-08-04 16:34:45 +08001568
1569 /* send app type cfg for haptics */
1570 if (usecase->id == USECASE_AUDIO_PLAYBACK_WITH_HAPTICS) {
1571 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
1572 "Audio Stream %d App Type Cfg", adev->haptic_pcm_device_id );
1573 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
1574 if (!ctl) {
1575 ALOGE("%s: Could not get ctl for mixer cmd - %s", __func__,
1576 mixer_ctl_name);
1577 rc = -EINVAL;
1578 goto exit_send_app_type_cfg;
1579 }
1580 acdb_dev_id = platform_get_snd_device_acdb_id(SND_DEVICE_OUT_HAPTICS);
1581 snd_device_be_idx = platform_get_snd_device_backend_index(SND_DEVICE_OUT_HAPTICS);
1582 /* haptics acdb id */
1583 app_type_cfg[1] = acdb_dev_id;
1584 /* haptics be index */
1585 app_type_cfg[3] = snd_device_be_idx;
1586 mixer_ctl_set_array(ctl, app_type_cfg, len);
1587 }
1588
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001589 rc = 0;
1590exit_send_app_type_cfg:
1591 return rc;
1592}
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001593
Deeraj Soman14230922019-01-30 16:39:30 +05301594static int audio_extn_utils_check_input_parameters(uint32_t sample_rate,
1595 audio_format_t format,
1596 int channel_count)
1597{
1598 int ret = 0;
1599
1600 if (((format != AUDIO_FORMAT_PCM_16_BIT) && (format != AUDIO_FORMAT_PCM_8_24_BIT) &&
1601 (format != AUDIO_FORMAT_PCM_24_BIT_PACKED) && (format != AUDIO_FORMAT_PCM_32_BIT) &&
1602 (format != AUDIO_FORMAT_PCM_FLOAT)) &&
1603 !voice_extn_compress_voip_is_format_supported(format) &&
1604 !audio_extn_compr_cap_format_supported(format) &&
1605 !audio_extn_cin_format_supported(format))
1606 ret = -EINVAL;
1607
1608 switch (channel_count) {
1609 case 1:
1610 case 2:
1611 case 3:
1612 case 4:
1613 case 6:
1614 case 8:
1615 break;
1616 default:
1617 ret = -EINVAL;
1618 }
1619
1620 switch (sample_rate) {
1621 case 8000:
1622 case 11025:
1623 case 12000:
1624 case 16000:
1625 case 22050:
1626 case 24000:
1627 case 32000:
1628 case 44100:
1629 case 48000:
1630 case 88200:
1631 case 96000:
1632 case 176400:
1633 case 192000:
1634 break;
1635 default:
1636 ret = -EINVAL;
1637 }
1638
1639 return ret;
1640}
1641
1642static inline uint32_t audio_extn_utils_nearest_multiple(uint32_t num, uint32_t multiplier)
1643{
1644 uint32_t remainder = 0;
1645
1646 if (!multiplier)
1647 return num;
1648
1649 remainder = num % multiplier;
1650 if (remainder)
1651 num += (multiplier - remainder);
1652
1653 return num;
1654}
1655
1656static inline uint32_t audio_extn_utils_lcm(uint32_t num1, uint32_t num2)
1657{
1658 uint32_t high = num1, low = num2, temp = 0;
1659
1660 if (!num1 || !num2)
1661 return 0;
1662
1663 if (num1 < num2) {
1664 high = num2;
1665 low = num1;
1666 }
1667
1668 while (low != 0) {
1669 temp = low;
1670 low = high % low;
1671 high = temp;
1672 }
1673 return (num1 * num2)/high;
1674}
1675
Siena Richard7c2db772016-12-21 11:32:34 -08001676int audio_extn_utils_send_app_type_cfg(struct audio_device *adev,
1677 struct audio_usecase *usecase)
1678{
1679 int i, num_devices = 0;
1680 snd_device_t new_snd_devices[SND_DEVICE_OUT_END] = {0};
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301681 snd_device_t in_snd_device = usecase->in_snd_device;
Siena Richard7c2db772016-12-21 11:32:34 -08001682 int rc = 0;
1683
Aalique Grahame22e49102018-12-18 14:23:57 -08001684 if (usecase->type == PCM_HFP_CALL) {
1685 return audio_extn_utils_send_app_type_cfg_hfp(adev, usecase);
Derek Chena30a5f42019-12-03 11:17:09 -05001686 } else if (usecase->type == ICC_CALL) {
1687 return audio_extn_utils_send_app_type_cfg_icc(adev, usecase);
Fei Tongaffdf732020-02-20 20:39:05 +08001688 } else if (usecase->type == SYNTH_LOOPBACK) {
1689 return audio_extn_utils_send_app_type_cfg_synth(adev, usecase);
Aalique Grahame22e49102018-12-18 14:23:57 -08001690 }
1691
Siena Richard7c2db772016-12-21 11:32:34 -08001692 switch (usecase->type) {
1693 case PCM_PLAYBACK:
Surendar Karka93cd25a2018-08-28 14:21:37 +05301694 case TRANSCODE_LOOPBACK_RX:
Siena Richard7c2db772016-12-21 11:32:34 -08001695 ALOGD("%s: usecase->out_snd_device %s",
1696 __func__, platform_get_snd_device_name(usecase->out_snd_device));
1697 /* check for out combo device */
1698 if (platform_split_snd_device(adev->platform,
1699 usecase->out_snd_device,
1700 &num_devices, new_snd_devices)) {
1701 new_snd_devices[0] = usecase->out_snd_device;
1702 num_devices = 1;
1703 }
1704 break;
1705 case PCM_CAPTURE:
1706 ALOGD("%s: usecase->in_snd_device %s",
1707 __func__, platform_get_snd_device_name(usecase->in_snd_device));
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301708 if (voice_is_in_call_rec_stream(usecase->stream.in)) {
1709 in_snd_device = voice_get_incall_rec_backend_device(usecase->stream.in);
1710 }
Siena Richard7c2db772016-12-21 11:32:34 -08001711 /* check for in combo device */
1712 if (platform_split_snd_device(adev->platform,
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301713 in_snd_device,
Siena Richard7c2db772016-12-21 11:32:34 -08001714 &num_devices, new_snd_devices)) {
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301715 new_snd_devices[0] = in_snd_device;
Siena Richard7c2db772016-12-21 11:32:34 -08001716 num_devices = 1;
1717 }
1718 break;
Sriram Kumar9c836482020-10-27 20:34:35 +05301719 case PCM_HFP_CALL:
1720 rc = audio_extn_utils_send_app_type_cfg_hfp(adev,usecase);
1721 return rc;
Siena Richard7c2db772016-12-21 11:32:34 -08001722 default:
1723 ALOGI("%s: not a playback/capture path, no need to cfg app type", __func__);
1724 rc = 0;
1725 break;
1726 }
1727
1728 for (i = 0; i < num_devices; i++) {
1729 rc = send_app_type_cfg_for_device(adev, usecase, new_snd_devices[i]);
1730 if (rc)
1731 break;
1732 }
1733
1734 return rc;
1735}
1736
Preetam Singh Ranawat9519e9c2015-11-18 16:05:55 +05301737int read_line_from_file(const char *path, char *buf, size_t count)
1738{
1739 char * fgets_ret;
1740 FILE * fd;
1741 int rv;
1742
1743 fd = fopen(path, "r");
1744 if (fd == NULL)
1745 return -1;
1746
1747 fgets_ret = fgets(buf, (int)count, fd);
1748 if (NULL != fgets_ret) {
1749 rv = (int)strlen(buf);
1750 } else {
1751 rv = ferror(fd);
1752 }
1753 fclose(fd);
1754
1755 return rv;
1756}
1757
Ashish Jainf1eaa582016-05-23 20:54:24 +05301758/*Translates ALSA formats to AOSP PCM formats*/
1759audio_format_t alsa_format_to_hal(uint32_t alsa_format)
1760{
1761 audio_format_t format;
1762
1763 switch(alsa_format) {
1764 case SNDRV_PCM_FORMAT_S16_LE:
1765 format = AUDIO_FORMAT_PCM_16_BIT;
1766 break;
1767 case SNDRV_PCM_FORMAT_S24_3LE:
1768 format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
1769 break;
1770 case SNDRV_PCM_FORMAT_S24_LE:
1771 format = AUDIO_FORMAT_PCM_8_24_BIT;
1772 break;
1773 case SNDRV_PCM_FORMAT_S32_LE:
1774 format = AUDIO_FORMAT_PCM_32_BIT;
1775 break;
1776 default:
1777 ALOGW("Incorrect ALSA format");
1778 format = AUDIO_FORMAT_INVALID;
1779 }
1780 return format;
1781}
1782
1783/*Translates hal format (AOSP) to alsa formats*/
1784uint32_t hal_format_to_alsa(audio_format_t hal_format)
1785{
1786 uint32_t alsa_format;
1787
1788 switch (hal_format) {
1789 case AUDIO_FORMAT_PCM_32_BIT: {
1790 if (platform_supports_true_32bit())
1791 alsa_format = SNDRV_PCM_FORMAT_S32_LE;
1792 else
1793 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1794 }
1795 break;
1796 case AUDIO_FORMAT_PCM_8_BIT:
1797 alsa_format = SNDRV_PCM_FORMAT_S8;
1798 break;
1799 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
1800 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1801 break;
1802 case AUDIO_FORMAT_PCM_8_24_BIT: {
1803 if (platform_supports_true_32bit())
1804 alsa_format = SNDRV_PCM_FORMAT_S32_LE;
1805 else
1806 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1807 }
1808 break;
1809 case AUDIO_FORMAT_PCM_FLOAT:
1810 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1811 break;
1812 default:
1813 case AUDIO_FORMAT_PCM_16_BIT:
1814 alsa_format = SNDRV_PCM_FORMAT_S16_LE;
1815 break;
1816 }
1817 return alsa_format;
1818}
1819
Ashish Jain83a6cc22016-06-28 14:34:17 +05301820/*Translates PCM formats to AOSP formats*/
1821audio_format_t pcm_format_to_hal(uint32_t pcm_format)
1822{
1823 audio_format_t format = AUDIO_FORMAT_INVALID;
1824
1825 switch(pcm_format) {
1826 case PCM_FORMAT_S16_LE:
1827 format = AUDIO_FORMAT_PCM_16_BIT;
1828 break;
1829 case PCM_FORMAT_S24_3LE:
1830 format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
1831 break;
1832 case PCM_FORMAT_S24_LE:
1833 format = AUDIO_FORMAT_PCM_8_24_BIT;
1834 break;
1835 case PCM_FORMAT_S32_LE:
1836 format = AUDIO_FORMAT_PCM_32_BIT;
1837 break;
1838 default:
1839 ALOGW("Incorrect PCM format");
1840 format = AUDIO_FORMAT_INVALID;
1841 }
1842 return format;
1843}
1844
1845/*Translates hal format (AOSP) to alsa formats*/
1846uint32_t hal_format_to_pcm(audio_format_t hal_format)
1847{
1848 uint32_t pcm_format;
1849
1850 switch (hal_format) {
1851 case AUDIO_FORMAT_PCM_32_BIT:
1852 case AUDIO_FORMAT_PCM_8_24_BIT:
1853 case AUDIO_FORMAT_PCM_FLOAT: {
1854 if (platform_supports_true_32bit())
1855 pcm_format = PCM_FORMAT_S32_LE;
1856 else
1857 pcm_format = PCM_FORMAT_S24_3LE;
1858 }
1859 break;
1860 case AUDIO_FORMAT_PCM_8_BIT:
1861 pcm_format = PCM_FORMAT_S8;
1862 break;
1863 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
1864 pcm_format = PCM_FORMAT_S24_3LE;
1865 break;
1866 default:
1867 case AUDIO_FORMAT_PCM_16_BIT:
1868 pcm_format = PCM_FORMAT_S16_LE;
1869 break;
1870 }
1871 return pcm_format;
1872}
1873
Ashish Jainf1eaa582016-05-23 20:54:24 +05301874uint32_t get_alsa_fragment_size(uint32_t bytes_per_sample,
1875 uint32_t sample_rate,
Deeraj Soman65358ab2019-02-07 15:40:49 +05301876 uint32_t noOfChannels,
1877 int64_t duration_ms)
Ashish Jainf1eaa582016-05-23 20:54:24 +05301878{
1879 uint32_t fragment_size = 0;
1880 uint32_t pcm_offload_time = PCM_OFFLOAD_BUFFER_DURATION;
1881
Deeraj Soman65358ab2019-02-07 15:40:49 +05301882 if (duration_ms >= MIN_OFFLOAD_BUFFER_DURATION_MS && duration_ms <= MAX_OFFLOAD_BUFFER_DURATION_MS)
1883 pcm_offload_time = duration_ms;
1884
Ashish Jainf1eaa582016-05-23 20:54:24 +05301885 fragment_size = (pcm_offload_time
1886 * sample_rate
1887 * bytes_per_sample
1888 * noOfChannels)/1000;
1889 if (fragment_size < MIN_PCM_OFFLOAD_FRAGMENT_SIZE)
1890 fragment_size = MIN_PCM_OFFLOAD_FRAGMENT_SIZE;
1891 else if (fragment_size > MAX_PCM_OFFLOAD_FRAGMENT_SIZE)
1892 fragment_size = MAX_PCM_OFFLOAD_FRAGMENT_SIZE;
1893 /*To have same PCM samples for all channels, the buffer size requires to
1894 *be multiple of (number of channels * bytes per sample)
1895 *For writes to succeed, the buffer must be written at address which is multiple of 32
1896 */
Aalique Grahameb85f2d92017-10-16 17:53:23 -07001897 fragment_size = ALIGN(fragment_size, (bytes_per_sample * noOfChannels * 32));
Ashish Jainf1eaa582016-05-23 20:54:24 +05301898
1899 ALOGI("PCM offload Fragment size to %d bytes", fragment_size);
1900 return fragment_size;
1901}
1902
1903/* Calculates the fragment size required to configure compress session.
1904 * Based on the alsa format selected, decide if conversion is needed in
1905
1906 * HAL ( e.g. convert AUDIO_FORMAT_PCM_FLOAT input format to
1907 * AUDIO_FORMAT_PCM_24_BIT_PACKED before writing to the compress driver.
1908 */
1909void audio_extn_utils_update_direct_pcm_fragment_size(struct stream_out *out)
1910{
Ashish Jain83a6cc22016-06-28 14:34:17 +05301911 audio_format_t dst_format = out->hal_op_format;
1912 audio_format_t src_format = out->hal_ip_format;
Ashish Jainf1eaa582016-05-23 20:54:24 +05301913 uint32_t hal_op_bytes_per_sample = audio_bytes_per_sample(dst_format);
1914 uint32_t hal_ip_bytes_per_sample = audio_bytes_per_sample(src_format);
1915
1916 out->compr_config.fragment_size =
1917 get_alsa_fragment_size(hal_op_bytes_per_sample,
1918 out->sample_rate,
Deeraj Soman65358ab2019-02-07 15:40:49 +05301919 popcount(out->channel_mask),
1920 out->info.duration_us / 1000);
Ashish Jainf1eaa582016-05-23 20:54:24 +05301921
1922 if ((src_format != dst_format) &&
1923 hal_op_bytes_per_sample != hal_ip_bytes_per_sample) {
1924
Ashish Jain83a6cc22016-06-28 14:34:17 +05301925 out->hal_fragment_size =
Ashish Jainf1eaa582016-05-23 20:54:24 +05301926 ((out->compr_config.fragment_size * hal_ip_bytes_per_sample) /
1927 hal_op_bytes_per_sample);
1928 ALOGI("enable conversion hal_input_fragment_size is %d src_format %x dst_format %x",
Ashish Jain83a6cc22016-06-28 14:34:17 +05301929 out->hal_fragment_size, src_format, dst_format);
Ashish Jainf1eaa582016-05-23 20:54:24 +05301930 } else {
Ashish Jain83a6cc22016-06-28 14:34:17 +05301931 out->hal_fragment_size = out->compr_config.fragment_size;
Ashish Jainf1eaa582016-05-23 20:54:24 +05301932 }
1933}
1934
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301935/* converts pcm format 24_8 to 8_24 inplace */
1936size_t audio_extn_utils_convert_format_24_8_to_8_24(void *buf, size_t bytes)
1937{
1938 size_t i = 0;
1939 int *int_buf_stream = buf;
1940
1941 if ((bytes % 4) != 0) {
1942 ALOGE("%s: wrong inout buffer! ... is not 32 bit aligned ", __func__);
1943 return -EINVAL;
1944 }
1945
1946 for (; i < (bytes / 4); i++)
1947 int_buf_stream[i] >>= 8;
1948
1949 return bytes;
1950}
1951
Xiaojun Sang782e5b12020-06-29 21:13:06 +08001952#ifdef AUDIO_GKI_ENABLED
1953int get_snd_codec_id(audio_format_t format)
1954{
1955 int id = 0;
1956
1957 switch (format & AUDIO_FORMAT_MAIN_MASK) {
1958 case AUDIO_FORMAT_MP3:
1959 id = SND_AUDIOCODEC_MP3;
1960 break;
1961 case AUDIO_FORMAT_AAC:
1962 id = SND_AUDIOCODEC_AAC;
1963 break;
1964 case AUDIO_FORMAT_AAC_ADTS:
1965 id = SND_AUDIOCODEC_AAC;
1966 break;
1967 case AUDIO_FORMAT_AAC_LATM:
1968 id = SND_AUDIOCODEC_AAC;
1969 break;
1970 case AUDIO_FORMAT_PCM:
1971 id = SND_AUDIOCODEC_PCM;
1972 break;
1973 case AUDIO_FORMAT_FLAC:
1974 case AUDIO_FORMAT_ALAC:
1975 case AUDIO_FORMAT_APE:
1976 case AUDIO_FORMAT_VORBIS:
1977 case AUDIO_FORMAT_WMA:
1978 case AUDIO_FORMAT_WMA_PRO:
1979 case AUDIO_FORMAT_DSD:
1980 case AUDIO_FORMAT_APTX:
1981 id = SND_AUDIOCODEC_BESPOKE;
1982 break;
1983 case AUDIO_FORMAT_MP2:
1984 id = SND_AUDIOCODEC_MP2;
1985 break;
1986 case AUDIO_FORMAT_AC3:
1987 id = SND_AUDIOCODEC_AC3;
1988 break;
1989 case AUDIO_FORMAT_E_AC3:
1990 case AUDIO_FORMAT_E_AC3_JOC:
1991 id = SND_AUDIOCODEC_EAC3;
1992 break;
1993 case AUDIO_FORMAT_DTS:
1994 case AUDIO_FORMAT_DTS_HD:
1995 id = SND_AUDIOCODEC_DTS;
1996 break;
1997 case AUDIO_FORMAT_DOLBY_TRUEHD:
1998 id = SND_AUDIOCODEC_TRUEHD;
1999 break;
2000 case AUDIO_FORMAT_IEC61937:
2001 id = SND_AUDIOCODEC_IEC61937;
2002 break;
2003 default:
2004 ALOGE("%s: Unsupported audio format :%x", __func__, format);
2005 }
2006
2007 return id;
2008}
2009#else
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05302010int get_snd_codec_id(audio_format_t format)
2011{
2012 int id = 0;
2013
2014 switch (format & AUDIO_FORMAT_MAIN_MASK) {
2015 case AUDIO_FORMAT_MP3:
2016 id = SND_AUDIOCODEC_MP3;
2017 break;
2018 case AUDIO_FORMAT_AAC:
2019 id = SND_AUDIOCODEC_AAC;
2020 break;
2021 case AUDIO_FORMAT_AAC_ADTS:
2022 id = SND_AUDIOCODEC_AAC;
2023 break;
Arun Kumar Dasari3b174182016-12-27 13:01:14 +05302024 case AUDIO_FORMAT_AAC_LATM:
2025 id = SND_AUDIOCODEC_AAC;
2026 break;
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05302027 case AUDIO_FORMAT_PCM:
2028 id = SND_AUDIOCODEC_PCM;
2029 break;
2030 case AUDIO_FORMAT_FLAC:
2031 id = SND_AUDIOCODEC_FLAC;
2032 break;
2033 case AUDIO_FORMAT_ALAC:
2034 id = SND_AUDIOCODEC_ALAC;
2035 break;
2036 case AUDIO_FORMAT_APE:
2037 id = SND_AUDIOCODEC_APE;
2038 break;
2039 case AUDIO_FORMAT_VORBIS:
2040 id = SND_AUDIOCODEC_VORBIS;
2041 break;
2042 case AUDIO_FORMAT_WMA:
2043 id = SND_AUDIOCODEC_WMA;
2044 break;
Dechen Chai4a575252021-07-30 09:21:28 +05302045#ifndef AUDIO_DISABLE_COMPRESS_FORMAT
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05302046 case AUDIO_FORMAT_WMA_PRO:
2047 id = SND_AUDIOCODEC_WMA_PRO;
2048 break;
Dechen Chai4a575252021-07-30 09:21:28 +05302049#endif
Satish Babu Patakokila0c313922016-12-08 12:07:08 +05302050 case AUDIO_FORMAT_MP2:
2051 id = SND_AUDIOCODEC_MP2;
2052 break;
Satish Babu Patakokila915ecba2017-01-10 17:43:56 +05302053 case AUDIO_FORMAT_AC3:
2054 id = SND_AUDIOCODEC_AC3;
2055 break;
2056 case AUDIO_FORMAT_E_AC3:
2057 case AUDIO_FORMAT_E_AC3_JOC:
2058 id = SND_AUDIOCODEC_EAC3;
2059 break;
2060 case AUDIO_FORMAT_DTS:
2061 case AUDIO_FORMAT_DTS_HD:
2062 id = SND_AUDIOCODEC_DTS;
2063 break;
Ben Romberger1aaaf862017-04-06 17:49:46 -07002064 case AUDIO_FORMAT_DOLBY_TRUEHD:
2065 id = SND_AUDIOCODEC_TRUEHD;
2066 break;
Naresh Tanniru928f0862017-04-07 16:44:23 -07002067 case AUDIO_FORMAT_IEC61937:
2068 id = SND_AUDIOCODEC_IEC61937;
2069 break;
Dechen Chai4a575252021-07-30 09:21:28 +05302070#ifndef AUDIO_DISABLE_COMPRESS_FORMAT
Preetam Singh Ranawat4277a5a2017-01-18 19:02:24 +05302071 case AUDIO_FORMAT_DSD:
2072 id = SND_AUDIOCODEC_DSD;
2073 break;
Dhanalakshmi Siddani18737932016-11-29 17:33:17 +05302074 case AUDIO_FORMAT_APTX:
2075 id = SND_AUDIOCODEC_APTX;
2076 break;
Dechen Chai4a575252021-07-30 09:21:28 +05302077#endif
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05302078 default:
2079 ALOGE("%s: Unsupported audio format :%x", __func__, format);
2080 }
2081
2082 return id;
2083}
Xiaojun Sang782e5b12020-06-29 21:13:06 +08002084#endif
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05302085
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07002086void audio_extn_utils_send_audio_calibration(struct audio_device *adev,
2087 struct audio_usecase *usecase)
2088{
2089 int type = usecase->type;
2090
Dhananjay Kumar14245982017-01-16 20:21:00 +05302091 if (type == PCM_PLAYBACK && usecase->stream.out != NULL) {
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05302092 platform_send_audio_calibration(adev->platform, usecase,
Zhou Song06761dd2019-04-28 18:08:17 +08002093 usecase->stream.out->app_type_cfg.app_type);
Dhananjay Kumar14245982017-01-16 20:21:00 +05302094 } else if (type == PCM_CAPTURE && usecase->stream.in != NULL) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +05302095 platform_send_audio_calibration(adev->platform, usecase,
Zhou Song06761dd2019-04-28 18:08:17 +08002096 usecase->stream.in->app_type_cfg.app_type);
Sriram Kumar9c836482020-10-27 20:34:35 +05302097 } else if (type == PCM_HFP_CALL) {
2098 /* when app type is default. the sample rate is not used to send cal */
2099#ifdef ENABLE_HFP_CALIBRATION
2100 platform_send_audio_calibration_hfp(adev->platform, usecase->in_snd_device);
2101#else
2102 platform_send_audio_calibration(adev->platform, usecase,
2103 platform_get_default_app_type_v2(adev->platform, usecase->type));
2104#endif
2105 } else if ((type == PCM_CAPTURE) ||
Derek Chena30a5f42019-12-03 11:17:09 -05002106 (type == TRANSCODE_LOOPBACK_RX && usecase->stream.inout != NULL) ||
Fei Tongaffdf732020-02-20 20:39:05 +08002107 (type == ICC_CALL) || (type == SYNTH_LOOPBACK)) {
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05302108 platform_send_audio_calibration(adev->platform, usecase,
Zhou Song06761dd2019-04-28 18:08:17 +08002109 platform_get_default_app_type_v2(adev->platform, usecase->type));
Vidyakumar Athotae57f6002017-03-01 13:13:16 -08002110 } else {
2111 /* No need to send audio calibration for voice and voip call usecases */
2112 if ((type != VOICE_CALL) && (type != VOIP_CALL))
2113 ALOGW("%s: No audio calibration for usecase type = %d", __func__, type);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07002114 }
2115}
2116
Ben Rombergera04fabc2014-11-14 12:16:03 -08002117// Base64 Encode and Decode
2118// Not all features supported. This must be used only with following conditions.
2119// Decode Modes: Support with and without padding
2120// CRLF not handling. So no CRLF in string to decode.
2121// Encode Modes: Supports only padding
2122int b64decode(char *inp, int ilen, uint8_t* outp)
2123{
2124 int i, j, k, ii, num;
2125 int rem, pcnt;
2126 uint32_t res=0;
2127 uint8_t getIndex[MAX_BASEINDEX_LEN];
2128 uint8_t tmp, cflag;
2129
2130 if(inp == NULL || outp == NULL || ilen <= 0) {
2131 ALOGE("[%s] received NULL pointer or zero length",__func__);
2132 return -1;
2133 }
2134
2135 memset(getIndex, MAX_BASEINDEX_LEN-1, sizeof(getIndex));
2136 for(i=0;i<BASE_TABLE_SIZE;i++) {
2137 getIndex[(uint8_t)bTable[i]] = (uint8_t)i;
2138 }
2139 getIndex[(uint8_t)'=']=0;
2140
2141 j=0;k=0;
2142 num = ilen/4;
2143 rem = ilen%4;
2144 if(rem==0)
2145 num = num-1;
2146 cflag=0;
2147 for(i=0; i<num; i++) {
2148 res=0;
2149 for(ii=0;ii<4;ii++) {
2150 res = res << 6;
2151 tmp = getIndex[(uint8_t)inp[j++]];
2152 res = res | tmp;
2153 cflag = cflag | tmp;
2154 }
2155 outp[k++] = (res >> 16)&0xFF;
2156 outp[k++] = (res >> 8)&0xFF;
2157 outp[k++] = res & 0xFF;
2158 }
2159
2160 // Handle last bytes special
2161 pcnt=0;
2162 if(rem == 0) {
2163 //With padding or full data
2164 res = 0;
2165 for(ii=0;ii<4;ii++) {
2166 if(inp[j] == '=')
2167 pcnt++;
2168 res = res << 6;
2169 tmp = getIndex[(uint8_t)inp[j++]];
2170 res = res | tmp;
2171 cflag = cflag | tmp;
2172 }
2173 outp[k++] = res >> 16;
2174 if(pcnt == 2)
2175 goto done;
2176 outp[k++] = (res>>8)&0xFF;
2177 if(pcnt == 1)
2178 goto done;
2179 outp[k++] = res&0xFF;
2180 } else {
2181 //without padding
2182 res = 0;
2183 for(i=0;i<rem;i++) {
2184 res = res << 6;
2185 tmp = getIndex[(uint8_t)inp[j++]];
2186 res = res | tmp;
2187 cflag = cflag | tmp;
2188 }
2189 for(i=rem;i<4;i++) {
2190 res = res << 6;
2191 pcnt++;
2192 }
2193 outp[k++] = res >> 16;
2194 if(pcnt == 2)
2195 goto done;
2196 outp[k++] = (res>>8)&0xFF;
2197 if(pcnt == 1)
2198 goto done;
2199 outp[k++] = res&0xFF;
2200 }
2201done:
2202 if(cflag == 0xFF) {
2203 ALOGE("[%s] base64 decode failed. Invalid character found %s",
2204 __func__, inp);
2205 return 0;
2206 }
2207 return k;
2208}
2209
2210int b64encode(uint8_t *inp, int ilen, char* outp)
2211{
2212 int i,j,k, num;
2213 int rem=0;
2214 uint32_t res=0;
2215
2216 if(inp == NULL || outp == NULL || ilen<=0) {
2217 ALOGE("[%s] received NULL pointer or zero input length",__func__);
2218 return -1;
2219 }
2220
2221 num = ilen/3;
2222 rem = ilen%3;
2223 j=0;k=0;
2224 for(i=0; i<num; i++) {
2225 //prepare index
2226 res = inp[j++]<<16;
2227 res = res | inp[j++]<<8;
2228 res = res | inp[j++];
2229 //get output map from index
2230 outp[k++] = (char) bTable[(res>>18)&0x3F];
2231 outp[k++] = (char) bTable[(res>>12)&0x3F];
2232 outp[k++] = (char) bTable[(res>>6)&0x3F];
2233 outp[k++] = (char) bTable[res&0x3F];
2234 }
2235
2236 switch(rem) {
2237 case 1:
2238 res = inp[j++]<<16;
2239 outp[k++] = (char) bTable[res>>18];
2240 outp[k++] = (char) bTable[(res>>12)&0x3F];
2241 //outp[k++] = '=';
2242 //outp[k++] = '=';
2243 break;
2244 case 2:
2245 res = inp[j++]<<16;
2246 res = res | inp[j++]<<8;
2247 outp[k++] = (char) bTable[res>>18];
2248 outp[k++] = (char) bTable[(res>>12)&0x3F];
2249 outp[k++] = (char) bTable[(res>>6)&0x3F];
2250 //outp[k++] = '=';
2251 break;
2252 default:
2253 break;
2254 }
Ben Rombergera04fabc2014-11-14 12:16:03 -08002255 outp[k] = '\0';
2256 return k;
2257}
Ashish Jain81eb2a82015-05-13 10:52:34 +05302258
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05302259
2260int audio_extn_utils_get_codec_version(const char *snd_card_name,
2261 int card_num,
2262 char *codec_version)
2263{
2264 char procfs_path[50];
2265 FILE *fp;
2266
2267 if (strstr(snd_card_name, "tasha")) {
2268 snprintf(procfs_path, sizeof(procfs_path),
2269 "/proc/asound/card%d/codecs/tasha/version", card_num);
2270 if ((fp = fopen(procfs_path, "r")) != NULL) {
2271 fgets(codec_version, CODEC_VERSION_MAX_LENGTH, fp);
2272 fclose(fp);
2273 } else {
2274 ALOGE("%s: ERROR. cannot open %s", __func__, procfs_path);
2275 return -ENOENT;
2276 }
2277 ALOGD("%s: codec version %s", __func__, codec_version);
2278 }
2279
2280 return 0;
2281}
2282
Preetam Singh Ranawat9d0d8e42019-07-15 12:27:25 +05302283int audio_extn_utils_get_codec_variant(int card_num,
2284 char *codec_variant)
2285{
2286 char procfs_path[50];
2287 FILE *fp;
2288 snprintf(procfs_path, sizeof(procfs_path),
2289 "/proc/asound/card%d/codecs/wcd938x/variant", card_num);
2290 if ((fp = fopen(procfs_path, "r")) == NULL) {
2291 snprintf(procfs_path, sizeof(procfs_path),
2292 "/proc/asound/card%d/codecs/wcd937x/variant", card_num);
2293 if ((fp = fopen(procfs_path, "r")) == NULL) {
2294 ALOGE("%s: ERROR. cannot open %s", __func__, procfs_path);
2295 return -ENOENT;
2296 }
2297 }
2298 fgets(codec_variant, CODEC_VARIANT_MAX_LENGTH, fp);
2299 fclose(fp);
2300 ALOGD("%s: codec variant is %s", __func__, codec_variant);
2301 return 0;
2302}
2303
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05302304
Ashish Jain81eb2a82015-05-13 10:52:34 +05302305#ifdef AUDIO_EXTERNAL_HDMI_ENABLED
2306
2307void get_default_compressed_channel_status(
2308 unsigned char *channel_status)
2309{
Ashish Jain81eb2a82015-05-13 10:52:34 +05302310 memset(channel_status,0,24);
2311
2312 /* block start bit in preamble bit 3 */
2313 channel_status[0] |= PROFESSIONAL;
2314 //compre out
2315 channel_status[0] |= NON_LPCM;
2316 // sample rate; fixed 48K for default/transcode
2317 channel_status[3] |= SR_48000;
2318}
2319
Ashish Jain81eb2a82015-05-13 10:52:34 +05302320int32_t get_compressed_channel_status(void *audio_stream_data,
2321 uint32_t audio_frame_size,
2322 unsigned char *channel_status,
2323 enum audio_parser_code_type codec_type)
2324 // codec_type - AUDIO_PARSER_CODEC_AC3
2325 // - AUDIO_PARSER_CODEC_DTS
2326{
2327 unsigned char *stream;
2328 int ret = 0;
2329 stream = (unsigned char *)audio_stream_data;
2330
2331 if (audio_stream_data == NULL || audio_frame_size == 0) {
2332 ALOGW("no buffer to get channel status, return default for compress");
2333 get_default_compressed_channel_status(channel_status);
2334 return ret;
2335 }
2336
2337 memset(channel_status,0,24);
2338 if(init_audio_parser(stream, audio_frame_size, codec_type) == -1)
2339 {
2340 ALOGE("init audio parser failed");
2341 return -1;
2342 }
2343 ret = get_channel_status(channel_status, codec_type);
2344 return ret;
2345
2346}
2347
Ashish Jain81eb2a82015-05-13 10:52:34 +05302348void get_lpcm_channel_status(uint32_t sampleRate,
2349 unsigned char *channel_status)
2350{
2351 int32_t status = 0;
Ashish Jain81eb2a82015-05-13 10:52:34 +05302352 memset(channel_status,0,24);
2353 /* block start bit in preamble bit 3 */
2354 channel_status[0] |= PROFESSIONAL;
2355 //LPCM OUT
2356 channel_status[0] &= ~NON_LPCM;
2357
2358 switch (sampleRate) {
2359 case 8000:
2360 case 11025:
2361 case 12000:
2362 case 16000:
2363 case 22050:
2364 channel_status[3] |= SR_NOTID;
Preetam Singh Ranawat61716b12015-12-14 11:55:24 +05302365 break;
Ashish Jain81eb2a82015-05-13 10:52:34 +05302366 case 24000:
2367 channel_status[3] |= SR_24000;
2368 break;
2369 case 32000:
2370 channel_status[3] |= SR_32000;
2371 break;
2372 case 44100:
2373 channel_status[3] |= SR_44100;
2374 break;
2375 case 48000:
2376 channel_status[3] |= SR_48000;
2377 break;
2378 case 88200:
2379 channel_status[3] |= SR_88200;
2380 break;
2381 case 96000:
2382 channel_status[3] |= SR_96000;
2383 break;
2384 case 176400:
2385 channel_status[3] |= SR_176400;
2386 break;
2387 case 192000:
2388 channel_status[3] |= SR_192000;
2389 break;
2390 default:
2391 ALOGV("Invalid sample_rate %u\n", sampleRate);
2392 status = -1;
2393 break;
2394 }
2395}
2396
2397void audio_utils_set_hdmi_channel_status(struct stream_out *out, char * buffer, size_t bytes)
2398{
2399 unsigned char channel_status[24]={0};
2400 struct snd_aes_iec958 iec958;
2401 const char *mixer_ctl_name = "IEC958 Playback PCM Stream";
2402 struct mixer_ctl *ctl;
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +05302403 ALOGV("%s: buffer %s bytes %zd", __func__, buffer, bytes);
Arun Mirpurie008ed22019-03-21 11:21:04 -07002404
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05302405 if (audio_extn_utils_is_dolby_format(out->format) &&
Ashish Jain81eb2a82015-05-13 10:52:34 +05302406 /*TODO:Extend code to support DTS passthrough*/
2407 /*set compressed channel status bits*/
Arun Mirpurie008ed22019-03-21 11:21:04 -07002408 audio_extn_passthru_is_passthrough_stream(out) &&
2409 audio_extn_is_hdmi_passthru_enabled()) {
Ashish Jain81eb2a82015-05-13 10:52:34 +05302410 get_compressed_channel_status(buffer, bytes, channel_status, AUDIO_PARSER_CODEC_AC3);
Arun Mirpurie008ed22019-03-21 11:21:04 -07002411 } else {
Ashish Jain81eb2a82015-05-13 10:52:34 +05302412 /*set channel status bit for LPCM*/
2413 get_lpcm_channel_status(out->sample_rate, channel_status);
2414 }
2415
2416 memcpy(iec958.status, channel_status,sizeof(iec958.status));
2417 ctl = mixer_get_ctl_by_name(out->dev->mixer, mixer_ctl_name);
2418 if (!ctl) {
2419 ALOGE("%s: Could not get ctl for mixer cmd - %s",
2420 __func__, mixer_ctl_name);
2421 return;
2422 }
2423 if (mixer_ctl_set_array(ctl, &iec958, sizeof(iec958)) < 0) {
2424 ALOGE("%s: Could not set channel status for ext HDMI ",
2425 __func__);
2426 return;
2427 }
2428
2429}
2430#endif
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302431
2432int audio_extn_utils_get_avt_device_drift(
2433 struct audio_usecase *usecase,
2434 struct audio_avt_device_drift_param *drift_param)
2435{
2436 int ret = 0, count = 0;
2437 char avt_device_drift_mixer_ctl_name[MIXER_PATH_MAX_LENGTH] = {0};
Naresh Tanniru6160c712017-04-17 15:43:48 +05302438 const char *backend = NULL;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302439 struct mixer_ctl *ctl = NULL;
2440 struct audio_avt_device_drift_stats drift_stats;
2441 struct audio_device *adev = NULL;
2442
2443 if (usecase != NULL && usecase->type == PCM_PLAYBACK) {
Naresh Tanniru6160c712017-04-17 15:43:48 +05302444 backend = platform_get_snd_device_backend_interface(usecase->out_snd_device);
2445 if (!backend) {
2446 ALOGE("%s: Unsupported device %d", __func__,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08002447 get_device_types(&usecase->stream.out->device_list));
Naresh Tanniru6160c712017-04-17 15:43:48 +05302448 ret = -EINVAL;
2449 goto done;
2450 }
2451 strlcpy(avt_device_drift_mixer_ctl_name,
2452 backend,
2453 MIXER_PATH_MAX_LENGTH);
2454
2455 count = strlen(backend);
2456 if (MIXER_PATH_MAX_LENGTH - count > 0) {
2457 strlcat(&avt_device_drift_mixer_ctl_name[count],
2458 " DRIFT",
2459 MIXER_PATH_MAX_LENGTH - count);
2460 } else {
2461 ret = -EINVAL;
2462 goto done;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302463 }
2464 } else {
Naresh Tanniru7586e292017-04-13 10:38:13 +05302465 ALOGE("%s: Invalid usecase",__func__);
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302466 ret = -EINVAL;
Naresh Tanniru6160c712017-04-17 15:43:48 +05302467 goto done;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302468 }
2469
Naresh Tanniru6160c712017-04-17 15:43:48 +05302470 adev = usecase->stream.out->dev;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302471 ctl = mixer_get_ctl_by_name(adev->mixer, avt_device_drift_mixer_ctl_name);
2472 if (!ctl) {
2473 ALOGE("%s: Could not get ctl for mixer cmd - %s",
2474 __func__, avt_device_drift_mixer_ctl_name);
2475
2476 ret = -EINVAL;
2477 goto done;
2478 }
2479
2480 ALOGV("%s: Getting AV Timer vs Device Drift mixer ctrl name %s", __func__,
2481 avt_device_drift_mixer_ctl_name);
2482
2483 mixer_ctl_update(ctl);
2484 count = mixer_ctl_get_num_values(ctl);
2485 if (count != sizeof(struct audio_avt_device_drift_stats)) {
2486 ALOGE("%s: mixer_ctl_get_num_values() invalid drift_stats data size",
2487 __func__);
2488
2489 ret = -EINVAL;
2490 goto done;
2491 }
2492
2493 ret = mixer_ctl_get_array(ctl, (void *)&drift_stats, count);
2494 if (ret != 0) {
2495 ALOGE("%s: mixer_ctl_get_array() failed to get drift_stats Params",
2496 __func__);
2497
2498 ret = -EINVAL;
2499 goto done;
2500 }
2501 memcpy(drift_param, &drift_stats.drift_param,
2502 sizeof(struct audio_avt_device_drift_param));
2503done:
2504 return ret;
2505}
Manish Dewangan07de2142017-02-27 19:27:20 +05302506
2507#ifdef SNDRV_COMPRESS_PATH_DELAY
2508int audio_extn_utils_compress_get_dsp_latency(struct stream_out *out)
2509{
2510 int ret = -EINVAL;
2511 struct snd_compr_metadata metadata;
2512 int delay_ms = COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
2513
Weiyin Jiangc49a3b52018-08-17 16:16:08 +08002514 /* override the latency for pcm offload use case */
2515 if ((out->flags & AUDIO_OUTPUT_FLAG_DIRECT) &&
2516 !(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)) {
2517 delay_ms = PCM_OFFLOAD_PLAYBACK_LATENCY;
2518 }
2519
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -07002520 if (property_get_bool("vendor.audio.playback.dsp.pathdelay", false)) {
Manish Dewangan07de2142017-02-27 19:27:20 +05302521 ALOGD("%s:: Quering DSP delay %d",__func__, __LINE__);
2522 if (!(is_offload_usecase(out->usecase))) {
2523 ALOGE("%s:: not supported for non offload session", __func__);
2524 goto exit;
2525 }
2526
2527 if (!out->compr) {
2528 ALOGD("%s:: Invalid compress handle,returning default dsp latency",
2529 __func__);
2530 goto exit;
2531 }
2532
2533 metadata.key = SNDRV_COMPRESS_PATH_DELAY;
2534 ret = compress_get_metadata(out->compr, &metadata);
2535 if(ret) {
2536 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2537 goto exit;
2538 }
2539 delay_ms = metadata.value[0] / 1000; /*convert to ms*/
2540 } else {
2541 ALOGD("%s:: Using Fix DSP delay",__func__);
2542 }
2543
2544exit:
2545 ALOGD("%s:: delay in ms is %d",__func__, delay_ms);
2546 return delay_ms;
2547}
2548#else
2549int audio_extn_utils_compress_get_dsp_latency(struct stream_out *out __unused)
2550{
2551 return COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
2552}
2553#endif
Manish Dewangan69426c82017-01-30 17:35:36 +05302554
2555#ifdef SNDRV_COMPRESS_RENDER_MODE
2556int audio_extn_utils_compress_set_render_mode(struct stream_out *out)
2557{
2558 struct snd_compr_metadata metadata;
2559 int ret = -EINVAL;
2560
2561 if (!(is_offload_usecase(out->usecase))) {
2562 ALOGE("%s:: not supported for non offload session", __func__);
2563 goto exit;
2564 }
2565
2566 if (!out->compr) {
2567 ALOGD("%s:: Invalid compress handle",
2568 __func__);
2569 goto exit;
2570 }
2571
2572 ALOGD("%s:: render mode %d", __func__, out->render_mode);
2573
2574 metadata.key = SNDRV_COMPRESS_RENDER_MODE;
2575 if (out->render_mode == RENDER_MODE_AUDIO_MASTER) {
2576 metadata.value[0] = SNDRV_COMPRESS_RENDER_MODE_AUDIO_MASTER;
2577 } else if (out->render_mode == RENDER_MODE_AUDIO_STC_MASTER) {
2578 metadata.value[0] = SNDRV_COMPRESS_RENDER_MODE_STC_MASTER;
2579 } else {
2580 ret = 0;
2581 goto exit;
2582 }
2583 ret = compress_set_metadata(out->compr, &metadata);
2584 if(ret) {
2585 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2586 }
2587exit:
2588 return ret;
2589}
2590#else
2591int audio_extn_utils_compress_set_render_mode(struct stream_out *out __unused)
2592{
2593 ALOGD("%s:: configuring render mode not supported", __func__);
2594 return 0;
2595}
2596#endif
Manish Dewangan58229382017-02-02 15:48:41 +05302597
2598#ifdef SNDRV_COMPRESS_CLK_REC_MODE
2599int audio_extn_utils_compress_set_clk_rec_mode(
2600 struct audio_usecase *usecase)
2601{
2602 struct snd_compr_metadata metadata;
2603 struct stream_out *out = NULL;
2604 int ret = -EINVAL;
2605
Naresh Tanniru7586e292017-04-13 10:38:13 +05302606 if (usecase == NULL || usecase->type != PCM_PLAYBACK) {
Manish Dewangan58229382017-02-02 15:48:41 +05302607 ALOGE("%s:: Invalid use case", __func__);
2608 goto exit;
2609 }
2610
2611 out = usecase->stream.out;
2612 if (!out) {
2613 ALOGE("%s:: invalid stream", __func__);
2614 goto exit;
2615 }
2616
2617 if (!is_offload_usecase(out->usecase)) {
2618 ALOGE("%s:: not supported for non offload session", __func__);
2619 goto exit;
2620 }
2621
2622 if (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER) {
2623 ALOGD("%s:: clk recovery is only supported in STC render mode",
2624 __func__);
2625 ret = 0;
2626 goto exit;
2627 }
2628
2629 if (!out->compr) {
2630 ALOGD("%s:: Invalid compress handle",
2631 __func__);
2632 goto exit;
2633 }
2634 metadata.key = SNDRV_COMPRESS_CLK_REC_MODE;
2635 switch(usecase->out_snd_device) {
2636 case SND_DEVICE_OUT_HDMI:
2637 case SND_DEVICE_OUT_SPEAKER_AND_HDMI:
2638 case SND_DEVICE_OUT_DISPLAY_PORT:
2639 case SND_DEVICE_OUT_SPEAKER_AND_DISPLAY_PORT:
2640 metadata.value[0] = SNDRV_COMPRESS_CLK_REC_MODE_NONE;
2641 break;
2642 default:
2643 metadata.value[0] = SNDRV_COMPRESS_CLK_REC_MODE_AUTO;
2644 break;
2645 }
2646
2647 ALOGD("%s:: clk recovery mode %d",__func__, metadata.value[0]);
2648
2649 ret = compress_set_metadata(out->compr, &metadata);
2650 if(ret) {
2651 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2652 }
2653
2654exit:
2655 return ret;
2656}
2657#else
2658int audio_extn_utils_compress_set_clk_rec_mode(
2659 struct audio_usecase *usecase __unused)
2660{
2661 ALOGD("%s:: configuring render mode not supported", __func__);
2662 return 0;
2663}
2664#endif
Manish Dewangan27346042017-03-01 12:56:12 +05302665
2666#ifdef SNDRV_COMPRESS_RENDER_WINDOW
2667int audio_extn_utils_compress_set_render_window(
2668 struct stream_out *out,
2669 struct audio_out_render_window_param *render_window)
2670{
2671 struct snd_compr_metadata metadata;
2672 int ret = -EINVAL;
2673
Manish Dewangan27346042017-03-01 12:56:12 +05302674 if(render_window == NULL) {
2675 ALOGE("%s:: Invalid render_window", __func__);
2676 goto exit;
2677 }
2678
Aniket Kumar Lata1e1d3662017-06-01 18:45:48 -07002679 ALOGD("%s:: render window start 0x%"PRIx64" end 0x%"PRIx64"",
2680 __func__,render_window->render_ws, render_window->render_we);
2681
Manish Dewangan27346042017-03-01 12:56:12 +05302682 if (!is_offload_usecase(out->usecase)) {
2683 ALOGE("%s:: not supported for non offload session", __func__);
2684 goto exit;
2685 }
2686
Naresh Tanniru6160c712017-04-17 15:43:48 +05302687 if ((out->render_mode != RENDER_MODE_AUDIO_MASTER) &&
2688 (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER)) {
Manish Dewangan27346042017-03-01 12:56:12 +05302689 ALOGD("%s:: only supported in timestamp mode, current "
2690 "render mode mode %d", __func__, out->render_mode);
2691 goto exit;
2692 }
2693
2694 if (!out->compr) {
2695 ALOGW("%s:: offload session not yet opened,"
2696 "render window will be configure later", __func__);
2697 /* store render window to reconfigure in start_output_stream() */
2698 goto exit;
2699 }
2700
2701 metadata.key = SNDRV_COMPRESS_RENDER_WINDOW;
2702 /*render window start value */
2703 metadata.value[0] = 0xFFFFFFFF & render_window->render_ws; /* lsb */
2704 metadata.value[1] = \
2705 (0xFFFFFFFF00000000 & render_window->render_ws) >> 32; /* msb*/
2706 /*render window end value */
2707 metadata.value[2] = 0xFFFFFFFF & render_window->render_we; /* lsb */
2708 metadata.value[3] = \
2709 (0xFFFFFFFF00000000 & render_window->render_we) >> 32; /* msb*/
2710
2711 ret = compress_set_metadata(out->compr, &metadata);
2712 if(ret) {
2713 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2714 }
2715
2716exit:
2717 return ret;
2718}
2719#else
2720int audio_extn_utils_compress_set_render_window(
2721 struct stream_out *out __unused,
2722 struct audio_out_render_window_param *render_window __unused)
2723{
2724 ALOGD("%s:: configuring render window not supported", __func__);
2725 return 0;
2726}
2727#endif
Manish Dewangan14956cc2017-02-14 18:54:42 +05302728
2729#ifdef SNDRV_COMPRESS_START_DELAY
2730int audio_extn_utils_compress_set_start_delay(
2731 struct stream_out *out,
2732 struct audio_out_start_delay_param *delay_param)
2733{
2734 struct snd_compr_metadata metadata;
2735 int ret = -EINVAL;
2736
2737 if(delay_param == NULL) {
2738 ALOGE("%s:: Invalid delay_param", __func__);
2739 goto exit;
2740 }
2741
2742 ALOGD("%s:: render start delay 0x%"PRIx64" ", __func__,
2743 delay_param->start_delay);
2744
2745 if (!is_offload_usecase(out->usecase)) {
2746 ALOGE("%s:: not supported for non offload session", __func__);
2747 goto exit;
2748 }
2749
Naresh Tanniru6160c712017-04-17 15:43:48 +05302750 if ((out->render_mode != RENDER_MODE_AUDIO_MASTER) &&
2751 (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER)) {
Manish Dewangan14956cc2017-02-14 18:54:42 +05302752 ALOGD("%s:: only supported in timestamp mode, current "
2753 "render mode mode %d", __func__, out->render_mode);
2754 goto exit;
2755 }
2756
2757 if (!out->compr) {
2758 ALOGW("%s:: offload session not yet opened,"
2759 "start delay will be configure later", __func__);
2760 goto exit;
2761 }
2762
2763 metadata.key = SNDRV_COMPRESS_START_DELAY;
2764 metadata.value[0] = 0xFFFFFFFF & delay_param->start_delay; /* lsb */
2765 metadata.value[1] = \
2766 (0xFFFFFFFF00000000 & delay_param->start_delay) >> 32; /* msb*/
2767
2768 ret = compress_set_metadata(out->compr, &metadata);
2769 if(ret) {
2770 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2771 }
2772
2773exit:
2774 return ret;
2775}
2776#else
2777int audio_extn_utils_compress_set_start_delay(
2778 struct stream_out *out __unused,
2779 struct audio_out_start_delay_param *delay_param __unused)
2780{
2781 ALOGD("%s:: configuring render window not supported", __func__);
2782 return 0;
2783}
2784#endif
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002785
Surendar Karka287348c2019-04-10 18:31:46 +05302786#ifdef SNDRV_COMPRESS_DSP_POSITION
2787int audio_extn_utils_compress_get_dsp_presentation_pos(struct stream_out *out,
2788 uint64_t *frames, struct timespec *timestamp, int32_t clock_id)
2789{
2790 int ret = -EINVAL;
2791 uint64_t *val = NULL;
2792 uint64_t time = 0;
2793 struct snd_compr_metadata metadata;
2794
2795 ALOGV("%s:: Quering DSP position with clock id %d",__func__, clock_id);
2796 metadata.key = SNDRV_COMPRESS_DSP_POSITION;
2797 metadata.value[0] = clock_id;
2798 ret = compress_get_metadata(out->compr, &metadata);
2799 if (ret) {
2800 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2801 ret = -errno;
2802 goto exit;
2803 }
2804 val = (uint64_t *)&metadata.value[1];
2805 *frames = *val;
2806 time = *(val + 1);
2807 timestamp->tv_sec = time / 1000000;
2808 timestamp->tv_nsec = (time % 1000000)*1000;
2809
2810exit:
2811 return ret;
2812}
2813#else
2814int audio_extn_utils_compress_get_dsp_presentation_pos(struct stream_out *out __unused,
2815 uint64_t *frames __unused, struct timespec *timestamp __unused,
2816 int32_t clock_id __unused)
2817{
2818 ALOGD("%s:: dsp presentation position not supported", __func__);
2819 return 0;
2820
2821}
2822#endif
2823
2824#ifdef SNDRV_PCM_IOCTL_DSP_POSITION
2825int audio_extn_utils_pcm_get_dsp_presentation_pos(struct stream_out *out,
2826 uint64_t *frames, struct timespec *timestamp, int32_t clock_id)
2827{
2828 int ret = -EINVAL;
2829 uint64_t time = 0;
2830 struct snd_pcm_prsnt_position prsnt_position;
Krishna Kishor Jhac1542b22022-08-22 13:12:09 +05302831 memset(&prsnt_position, 0, sizeof(struct snd_pcm_prsnt_position));
Surendar Karka287348c2019-04-10 18:31:46 +05302832
2833 ALOGV("%s:: Quering DSP position with clock id %d",__func__, clock_id);
2834 prsnt_position.clock_id = clock_id;
2835 ret = pcm_ioctl(out->pcm, SNDRV_PCM_IOCTL_DSP_POSITION, &prsnt_position);
2836 if (ret) {
2837 ALOGE("%s::error %d", __func__, ret);
2838 ret = -EIO;
2839 goto exit;
2840 }
2841
2842 *frames = prsnt_position.frames;
2843 time = prsnt_position.timestamp;
2844 timestamp->tv_sec = time / 1000000;
2845 timestamp->tv_nsec = (time % 1000000)*1000;
2846
2847exit:
2848 return ret;
2849}
2850#else
2851int audio_extn_utils_pcm_get_dsp_presentation_pos(struct stream_out *out __unused,
2852 uint64_t *frames __unused, struct timespec *timestamp __unused,
2853 int32_t clock_id __unused)
2854{
2855 ALOGD("%s:: dsp presentation position not supported", __func__);
2856 return 0;
2857
2858}
2859#endif
2860
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002861#define MAX_SND_CARD 8
Jaideep Sharmadf948882020-10-22 14:13:52 +05302862#define RETRY_US 400000
2863#define RETRY_NUMBER 100
Aalique Grahame22e49102018-12-18 14:23:57 -08002864#define PLATFORM_INFO_XML_PATH "audio_platform_info.xml"
2865#define PLATFORM_INFO_XML_BASE_STRING "audio_platform_info"
2866
Aalique Grahame22e49102018-12-18 14:23:57 -08002867bool audio_extn_utils_resolve_config_file(char file_name[MIXER_PATH_MAX_LENGTH])
2868{
2869 char full_config_path[MIXER_PATH_MAX_LENGTH];
Saurav Kumarf5d2c342020-07-29 19:31:49 +05302870 char vendor_config_path[VENDOR_CONFIG_PATH_MAX_LENGTH];
2871
2872 /* Get path for audio configuration files in vendor */
2873 audio_get_vendor_config_path(vendor_config_path,
2874 sizeof(vendor_config_path));
2875 snprintf(full_config_path, sizeof(full_config_path),
2876 "%s/%s", vendor_config_path, file_name);
2877 if (F_OK == access(full_config_path, 0)) {
2878 strlcpy(file_name, full_config_path, MIXER_PATH_MAX_LENGTH);
2879 return true;
Aalique Grahame22e49102018-12-18 14:23:57 -08002880 }
2881 return false;
2882}
2883
2884/* platform_info_file should be size 'MIXER_PATH_MAX_LENGTH' */
2885int audio_extn_utils_get_platform_info(const char* snd_card_name, char* platform_info_file)
2886{
2887 if (NULL == snd_card_name) {
2888 return -1;
2889 }
2890
2891 struct snd_card_split *snd_split_handle = NULL;
2892 int ret = 0;
2893 audio_extn_set_snd_card_split(snd_card_name);
2894 snd_split_handle = audio_extn_get_snd_card_split();
2895
2896 snprintf(platform_info_file, MIXER_PATH_MAX_LENGTH, "%s_%s_%s.xml",
2897 PLATFORM_INFO_XML_BASE_STRING, snd_split_handle->snd_card,
2898 snd_split_handle->form_factor);
2899
2900 if (!audio_extn_utils_resolve_config_file(platform_info_file)) {
2901 memset(platform_info_file, 0, MIXER_PATH_MAX_LENGTH);
2902 snprintf(platform_info_file, MIXER_PATH_MAX_LENGTH, "%s_%s.xml",
2903 PLATFORM_INFO_XML_BASE_STRING, snd_split_handle->snd_card);
2904
2905 if (!audio_extn_utils_resolve_config_file(platform_info_file)) {
2906 memset(platform_info_file, 0, MIXER_PATH_MAX_LENGTH);
2907 strlcpy(platform_info_file, PLATFORM_INFO_XML_PATH, MIXER_PATH_MAX_LENGTH);
2908 ret = audio_extn_utils_resolve_config_file(platform_info_file) ? 0 : -1;
2909 }
2910 }
2911
2912 return ret;
2913}
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002914
2915int audio_extn_utils_get_snd_card_num()
2916{
Soumya Managoli9fee7c62018-04-06 16:21:50 +05302917 int snd_card_num = 0;
2918 struct mixer *mixer = NULL;
2919
2920 snd_card_num = audio_extn_utils_open_snd_mixer(&mixer);
2921 if (mixer)
2922 mixer_close(mixer);
2923 return snd_card_num;
2924}
2925
2926int audio_extn_utils_open_snd_mixer(struct mixer **mixer_handle)
2927{
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002928
2929 void *hw_info = NULL;
2930 struct mixer *mixer = NULL;
2931 int retry_num = 0;
mpangfaa7bae2019-01-15 16:38:13 +08002932 int snd_card_num = 0;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002933 char* snd_card_name = NULL;
mpangfaa7bae2019-01-15 16:38:13 +08002934 int snd_card_detection_info[MAX_SND_CARD] = {0};
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002935
Soumya Managoli9fee7c62018-04-06 16:21:50 +05302936 if (!mixer_handle) {
2937 ALOGE("invalid mixer handle");
2938 return -1;
2939 }
2940 *mixer_handle = NULL;
Ashish Jain30ae8942017-11-05 17:21:23 +05302941 /*
mpangfaa7bae2019-01-15 16:38:13 +08002942 * Try with all the sound cards ( 0 to 7 ) and if none of them were detected
Ashish Jain30ae8942017-11-05 17:21:23 +05302943 * sleep for 1 sec and try detections with sound card 0 again.
2944 * If sound card gets detected, check if it is relevant, if not check with the
2945 * other sound cards. To ensure that the irrelevant sound card is not check again,
2946 * we maintain it in min_snd_card_num.
2947 */
2948 while (retry_num < RETRY_NUMBER) {
mpangfaa7bae2019-01-15 16:38:13 +08002949 snd_card_num = 0;
2950 while (snd_card_num < MAX_SND_CARD) {
2951 if (snd_card_detection_info[snd_card_num] == 0) {
2952 mixer = mixer_open(snd_card_num);
2953 if (!mixer)
2954 snd_card_num++;
2955 else
2956 break;
2957 } else
2958 snd_card_num++;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002959 }
2960
2961 if (!mixer) {
Ashish Jain30ae8942017-11-05 17:21:23 +05302962 usleep(RETRY_US);
Ashish Jain30ae8942017-11-05 17:21:23 +05302963 retry_num++;
mpangfaa7bae2019-01-15 16:38:13 +08002964 ALOGD("%s: retry, retry_num %d", __func__, retry_num);
Ashish Jain30ae8942017-11-05 17:21:23 +05302965 continue;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002966 }
2967
2968 snd_card_name = strdup(mixer_get_name(mixer));
2969 if (!snd_card_name) {
2970 ALOGE("failed to allocate memory for snd_card_name\n");
2971 mixer_close(mixer);
2972 return -1;
2973 }
2974 ALOGD("%s: snd_card_name: %s", __func__, snd_card_name);
mpangfaa7bae2019-01-15 16:38:13 +08002975 snd_card_detection_info[snd_card_num] = 1;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002976 hw_info = hw_info_init(snd_card_name);
2977 if (hw_info) {
2978 ALOGD("%s: Opened sound card:%d", __func__, snd_card_num);
2979 break;
2980 }
mpangfaa7bae2019-01-15 16:38:13 +08002981 ALOGE("%s: Failed to init hardware info, snd_card_num:%d", __func__, snd_card_num);
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002982
Dhananjay Kumara7e27832017-07-11 15:40:39 +05302983 free(snd_card_name);
2984 snd_card_name = NULL;
2985
2986 mixer_close(mixer);
2987 mixer = NULL;
2988 }
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002989 if (snd_card_name)
2990 free(snd_card_name);
Ashish Jain30ae8942017-11-05 17:21:23 +05302991
Dhananjay Kumara7e27832017-07-11 15:40:39 +05302992 if (hw_info)
2993 hw_info_deinit(hw_info);
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002994
mpangfaa7bae2019-01-15 16:38:13 +08002995 if (retry_num >= RETRY_NUMBER) {
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002996 ALOGE("%s: Unable to find correct sound card, aborting.", __func__);
2997 return -1;
2998 }
2999
Soumya Managoli9fee7c62018-04-06 16:21:50 +05303000 if (mixer)
3001 *mixer_handle = mixer;
3002
Vignesh Kulothungan55396882017-04-20 14:37:02 -07003003 return snd_card_num;
3004}
Naresh Tanniru6160c712017-04-17 15:43:48 +05303005
Soumya Managoli9fee7c62018-04-06 16:21:50 +05303006void audio_extn_utils_close_snd_mixer(struct mixer *mixer)
3007{
3008 if (mixer)
3009 mixer_close(mixer);
3010}
3011
Naresh Tanniru6160c712017-04-17 15:43:48 +05303012#ifdef SNDRV_COMPRESS_ENABLE_ADJUST_SESSION_CLOCK
3013int audio_extn_utils_compress_enable_drift_correction(
3014 struct stream_out *out,
3015 struct audio_out_enable_drift_correction *drift)
3016{
3017 struct snd_compr_metadata metadata;
3018 int ret = -EINVAL;
3019
3020 if(drift == NULL) {
3021 ALOGE("%s:: Invalid param", __func__);
3022 goto exit;
3023 }
3024
3025 ALOGD("%s:: drift enable %d", __func__,drift->enable);
3026
3027 if (!is_offload_usecase(out->usecase)) {
3028 ALOGE("%s:: not supported for non offload session", __func__);
3029 goto exit;
3030 }
3031
3032 if (!out->compr) {
3033 ALOGW("%s:: offload session not yet opened,"
3034 "start delay will be configure later", __func__);
3035 goto exit;
3036 }
3037
3038 metadata.key = SNDRV_COMPRESS_ENABLE_ADJUST_SESSION_CLOCK;
3039 metadata.value[0] = drift->enable;
3040 out->drift_correction_enabled = drift->enable;
3041
3042 ret = compress_set_metadata(out->compr, &metadata);
3043 if(ret) {
3044 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
3045 out->drift_correction_enabled = false;
3046 }
3047
3048exit:
3049 return ret;
3050}
3051#else
3052int audio_extn_utils_compress_enable_drift_correction(
3053 struct stream_out *out __unused,
3054 struct audio_out_enable_drift_correction *drift __unused)
3055{
3056 ALOGD("%s:: configuring drift enablement not supported", __func__);
3057 return 0;
3058}
3059#endif
3060
3061#ifdef SNDRV_COMPRESS_ADJUST_SESSION_CLOCK
3062int audio_extn_utils_compress_correct_drift(
3063 struct stream_out *out,
3064 struct audio_out_correct_drift *drift_param)
3065{
3066 struct snd_compr_metadata metadata;
3067 int ret = -EINVAL;
3068
3069 if (drift_param == NULL) {
3070 ALOGE("%s:: Invalid drift_param", __func__);
3071 goto exit;
3072 }
3073
3074 ALOGD("%s:: adjust time 0x%"PRIx64" ", __func__,
3075 drift_param->adjust_time);
3076
3077 if (!is_offload_usecase(out->usecase)) {
3078 ALOGE("%s:: not supported for non offload session", __func__);
3079 goto exit;
3080 }
3081
3082 if (!out->compr) {
3083 ALOGW("%s:: offload session not yet opened", __func__);
3084 goto exit;
3085 }
3086
3087 if (!out->drift_correction_enabled) {
3088 ALOGE("%s:: drift correction not enabled", __func__);
3089 goto exit;
3090 }
3091
3092 metadata.key = SNDRV_COMPRESS_ADJUST_SESSION_CLOCK;
3093 metadata.value[0] = 0xFFFFFFFF & drift_param->adjust_time; /* lsb */
3094 metadata.value[1] = \
3095 (0xFFFFFFFF00000000 & drift_param->adjust_time) >> 32; /* msb*/
3096
3097 ret = compress_set_metadata(out->compr, &metadata);
3098 if(ret)
3099 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
3100exit:
3101 return ret;
3102}
3103#else
3104int audio_extn_utils_compress_correct_drift(
3105 struct stream_out *out __unused,
3106 struct audio_out_correct_drift *drift_param __unused)
3107{
3108 ALOGD("%s:: setting adjust clock not supported", __func__);
3109 return 0;
3110}
3111#endif
Naresh Tanniru29bce4e2017-04-27 17:54:30 +05303112
3113int audio_extn_utils_set_channel_map(
3114 struct stream_out *out,
3115 struct audio_out_channel_map_param *channel_map_param)
3116{
3117 int ret = -EINVAL, i = 0;
3118 int channels = audio_channel_count_from_out_mask(out->channel_mask);
3119
3120 if (channel_map_param == NULL) {
3121 ALOGE("%s:: Invalid channel_map", __func__);
3122 goto exit;
3123 }
3124
3125 if (channel_map_param->channels != channels) {
3126 ALOGE("%s:: Channels(%d) does not match stream channels(%d)",
3127 __func__, channel_map_param->channels, channels);
3128 goto exit;
3129 }
3130
3131 for ( i = 0; i < channels; i++) {
3132 ALOGV("%s:: channel_map[%d]- %d", __func__, i, channel_map_param->channel_map[i]);
3133 out->channel_map_param.channel_map[i] = channel_map_param->channel_map[i];
3134 }
3135 ret = 0;
3136exit:
3137 return ret;
3138}
Varun Balaraje49253e2017-07-06 19:48:56 +05303139
3140int audio_extn_utils_set_pan_scale_params(
3141 struct stream_out *out,
3142 struct mix_matrix_params *mm_params)
3143{
3144 int ret = -EINVAL, i = 0, j = 0;
3145
Varun Balaraj003ee752017-08-07 17:54:55 +05303146 if (mm_params == NULL || out == NULL) {
3147 ALOGE("%s:: Invalid mix matrix or out param", __func__);
Varun Balaraje49253e2017-07-06 19:48:56 +05303148 goto exit;
3149 }
3150
3151 if (mm_params->num_output_channels > MAX_CHANNELS_SUPPORTED ||
3152 mm_params->num_output_channels <= 0 ||
3153 mm_params->num_input_channels > MAX_CHANNELS_SUPPORTED ||
3154 mm_params->num_input_channels <= 0)
3155 goto exit;
3156
3157 out->pan_scale_params.num_output_channels = mm_params->num_output_channels;
3158 out->pan_scale_params.num_input_channels = mm_params->num_input_channels;
3159 out->pan_scale_params.has_output_channel_map =
3160 mm_params->has_output_channel_map;
3161 for (i = 0; i < mm_params->num_output_channels; i++)
3162 out->pan_scale_params.output_channel_map[i] =
3163 mm_params->output_channel_map[i];
3164
3165 out->pan_scale_params.has_input_channel_map =
3166 mm_params->has_input_channel_map;
3167 for (i = 0; i < mm_params->num_input_channels; i++)
3168 out->pan_scale_params.input_channel_map[i] =
3169 mm_params->input_channel_map[i];
3170
3171 out->pan_scale_params.has_mixer_coeffs = mm_params->has_mixer_coeffs;
3172 for (i = 0; i < mm_params->num_output_channels; i++)
3173 for (j = 0; j < mm_params->num_input_channels; j++) {
3174 //Convert the channel coefficient gains in Q14 format
3175 out->pan_scale_params.mixer_coeffs[i][j] =
3176 mm_params->mixer_coeffs[i][j] * (2 << 13);
3177 }
3178
3179 ret = platform_set_stream_pan_scale_params(out->dev->platform,
3180 out->pcm_device_id,
3181 out->pan_scale_params);
3182
3183exit:
3184 return ret;
3185}
3186
3187int audio_extn_utils_set_downmix_params(
3188 struct stream_out *out,
3189 struct mix_matrix_params *mm_params)
3190{
3191 int ret = -EINVAL, i = 0, j = 0;
3192 struct audio_usecase *usecase = NULL;
3193
Aniket Kumar Lataf9f246e2017-09-15 15:20:16 -07003194 if (mm_params == NULL || out == NULL) {
Varun Balaraj003ee752017-08-07 17:54:55 +05303195 ALOGE("%s:: Invalid mix matrix or out param", __func__);
Varun Balaraje49253e2017-07-06 19:48:56 +05303196 goto exit;
3197 }
3198
3199 if (mm_params->num_output_channels > MAX_CHANNELS_SUPPORTED ||
3200 mm_params->num_output_channels <= 0 ||
3201 mm_params->num_input_channels > MAX_CHANNELS_SUPPORTED ||
3202 mm_params->num_input_channels <= 0)
3203 goto exit;
3204
3205 usecase = get_usecase_from_list(out->dev, out->usecase);
Varun Balaraj003ee752017-08-07 17:54:55 +05303206 if (!usecase) {
3207 ALOGE("%s: Get usecase list failed!", __func__);
Aniket Kumar Lataf9f246e2017-09-15 15:20:16 -07003208 goto exit;
3209 }
Varun Balaraje49253e2017-07-06 19:48:56 +05303210 out->downmix_params.num_output_channels = mm_params->num_output_channels;
3211 out->downmix_params.num_input_channels = mm_params->num_input_channels;
3212
3213 out->downmix_params.has_output_channel_map =
3214 mm_params->has_output_channel_map;
3215 for (i = 0; i < mm_params->num_output_channels; i++) {
3216 out->downmix_params.output_channel_map[i] =
3217 mm_params->output_channel_map[i];
3218 }
3219
3220 out->downmix_params.has_input_channel_map =
3221 mm_params->has_input_channel_map;
3222 for (i = 0; i < mm_params->num_input_channels; i++)
3223 out->downmix_params.input_channel_map[i] =
3224 mm_params->input_channel_map[i];
3225
3226 out->downmix_params.has_mixer_coeffs = mm_params->has_mixer_coeffs;
3227 for (i = 0; i < mm_params->num_output_channels; i++)
Nikhil Latukar2e6f6242017-08-15 13:37:07 +05303228 for (j = 0; j < mm_params->num_input_channels; j++) {
3229 //Convert the channel coefficient gains in Q14 format
Varun Balaraje49253e2017-07-06 19:48:56 +05303230 out->downmix_params.mixer_coeffs[i][j] =
Nikhil Latukar2e6f6242017-08-15 13:37:07 +05303231 mm_params->mixer_coeffs[i][j] * (2 << 13);
3232 }
Varun Balaraje49253e2017-07-06 19:48:56 +05303233
3234 ret = platform_set_stream_downmix_params(out->dev->platform,
3235 out->pcm_device_id,
3236 usecase->out_snd_device,
3237 out->downmix_params);
3238
3239exit:
3240 return ret;
3241}
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05303242
3243bool audio_extn_utils_is_dolby_format(audio_format_t format)
3244{
3245 if (format == AUDIO_FORMAT_AC3 ||
3246 format == AUDIO_FORMAT_E_AC3 ||
3247 format == AUDIO_FORMAT_E_AC3_JOC)
3248 return true;
3249 else
3250 return false;
3251}
3252
`Deeraj Soman676c2702017-09-18 19:25:53 +05303253int audio_extn_utils_get_bit_width_from_string(const char *id_string)
3254{
3255 int i;
3256 const mixer_config_lookup mixer_bitwidth_config[] = {{"S24_3LE", 24},
3257 {"S32_LE", 32},
3258 {"S24_LE", 24},
3259 {"S16_LE", 16}};
3260 int num_configs = sizeof(mixer_bitwidth_config) / sizeof(mixer_bitwidth_config[0]);
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05303261
`Deeraj Soman676c2702017-09-18 19:25:53 +05303262 for (i = 0; i < num_configs; i++) {
3263 if (!strcmp(id_string, mixer_bitwidth_config[i].id_string))
3264 return mixer_bitwidth_config[i].value;
3265 }
3266
3267 return -EINVAL;
3268}
3269
3270int audio_extn_utils_get_sample_rate_from_string(const char *id_string)
3271{
3272 int i;
Weiyin Jiang30f9ab52020-11-11 11:37:21 +08003273 const mixer_config_lookup mixer_samplerate_config[] = {{"KHZ_8", 8000},
3274 {"KHZ_11P025", 11025},
3275 {"KHZ_16", 16000},
3276 {"KHZ_22P05", 22050},
3277 {"KHZ_32", 32000},
`Deeraj Soman676c2702017-09-18 19:25:53 +05303278 {"KHZ_48", 48000},
3279 {"KHZ_96", 96000},
3280 {"KHZ_144", 144000},
3281 {"KHZ_192", 192000},
3282 {"KHZ_384", 384000},
3283 {"KHZ_44P1", 44100},
3284 {"KHZ_88P2", 88200},
3285 {"KHZ_176P4", 176400},
3286 {"KHZ_352P8", 352800}};
3287 int num_configs = sizeof(mixer_samplerate_config) / sizeof(mixer_samplerate_config[0]);
3288
3289 for (i = 0; i < num_configs; i++) {
3290 if (!strcmp(id_string, mixer_samplerate_config[i].id_string))
3291 return mixer_samplerate_config[i].value;
3292 }
3293
3294 return -EINVAL;
3295}
3296
3297int audio_extn_utils_get_channels_from_string(const char *id_string)
3298{
3299 int i;
3300 const mixer_config_lookup mixer_channels_config[] = {{"One", 1},
3301 {"Two", 2},
3302 {"Three",3},
3303 {"Four", 4},
3304 {"Five", 5},
3305 {"Six", 6},
3306 {"Seven", 7},
3307 {"Eight", 8}};
3308 int num_configs = sizeof(mixer_channels_config) / sizeof(mixer_channels_config[0]);
3309
3310 for (i = 0; i < num_configs; i++) {
3311 if (!strcmp(id_string, mixer_channels_config[i].id_string))
3312 return mixer_channels_config[i].value;
3313 }
3314
3315 return -EINVAL;
3316}
Surendar karka30569792018-05-08 12:02:21 +05303317
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08003318void audio_extn_utils_release_snd_device(snd_device_t snd_device)
3319{
3320 audio_extn_dev_arbi_release(snd_device);
3321 audio_extn_sound_trigger_update_device_status(snd_device,
3322 ST_EVENT_SND_DEVICE_FREE);
3323 audio_extn_listen_update_device_status(snd_device,
3324 LISTEN_EVENT_SND_DEVICE_FREE);
3325}
3326
Weiyin Jiangd5718bd2019-09-04 16:52:08 +08003327int audio_extn_utils_get_license_params(
3328 const struct audio_device *adev,
3329 struct audio_license_params *license_params)
Surendar karka30569792018-05-08 12:02:21 +05303330{
3331 if(!license_params)
3332 return -EINVAL;
Weiyin Jiangd5718bd2019-09-04 16:52:08 +08003333
3334 return platform_get_license_by_product(adev->platform,
3335 (const char*)license_params->product, &license_params->key, license_params->license);
Surendar karka30569792018-05-08 12:02:21 +05303336}
3337
Aalique Grahame22e49102018-12-18 14:23:57 -08003338int audio_extn_utils_send_app_type_gain(struct audio_device *adev,
3339 int app_type,
3340 int *gain)
3341{
3342 int gain_cfg[4];
3343 const char *mixer_ctl_name = "App Type Gain";
3344 struct mixer_ctl *ctl;
3345 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
3346 if (!ctl) {
3347 ALOGE("%s: Could not get volume ctl mixer %s", __func__,
3348 mixer_ctl_name);
3349 return -EINVAL;
3350 }
3351 gain_cfg[0] = 0;
3352 gain_cfg[1] = app_type;
3353 gain_cfg[2] = gain[0];
3354 gain_cfg[3] = gain[1];
3355 ALOGV("%s app_type %d l(%d) r(%d)", __func__, app_type, gain[0], gain[1]);
3356 return mixer_ctl_set_array(ctl, gain_cfg,
3357 sizeof(gain_cfg)/sizeof(gain_cfg[0]));
3358}
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -07003359
Weiyin Jiangd5718bd2019-09-04 16:52:08 +08003360static void vndk_fwk_init()
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -07003361{
Weiyin Jiangd5718bd2019-09-04 16:52:08 +08003362 if (mVndkFwk.lib_handle != NULL)
3363 return;
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -07003364
Weiyin Jiangd5718bd2019-09-04 16:52:08 +08003365 mVndkFwk.lib_handle = dlopen(VNDK_FWK_LIB_PATH, RTLD_NOW);
3366 if (mVndkFwk.lib_handle == NULL) {
3367 ALOGW("%s: failed to dlopen VNDK_FWK_LIB %s", __func__, strerror(errno));
3368 return;
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -07003369 }
3370
Weiyin Jiangd5718bd2019-09-04 16:52:08 +08003371 *(void **)(&mVndkFwk.isVendorEnhancedFwk) =
3372 dlsym(mVndkFwk.lib_handle, "isRunningWithVendorEnhancedFramework");
3373 if (mVndkFwk.isVendorEnhancedFwk == NULL) {
3374 ALOGW("%s: dlsym failed %s", __func__, strerror(errno));
3375 if (mVndkFwk.lib_handle) {
3376 dlclose(mVndkFwk.lib_handle);
3377 mVndkFwk.lib_handle = NULL;
3378 }
3379 return;
3380 }
3381
3382
3383 *(void **)(&mVndkFwk.getVendorEnhancedInfo) =
3384 dlsym(mVndkFwk.lib_handle, "getVendorEnhancedInfo");
3385 if (mVndkFwk.getVendorEnhancedInfo == NULL) {
3386 ALOGW("%s: dlsym failed %s", __func__, strerror(errno));
3387 if (mVndkFwk.lib_handle) {
3388 dlclose(mVndkFwk.lib_handle);
3389 mVndkFwk.lib_handle = NULL;
3390 }
3391 }
3392
3393 return;
3394}
3395
3396bool audio_extn_utils_is_vendor_enhanced_fwk()
3397{
3398 static int is_vendor_enhanced_fwk = -EINVAL;
3399 if (is_vendor_enhanced_fwk != -EINVAL)
3400 return (bool)is_vendor_enhanced_fwk;
3401
3402 vndk_fwk_init();
3403
3404 if (mVndkFwk.isVendorEnhancedFwk != NULL) {
3405 is_vendor_enhanced_fwk = mVndkFwk.isVendorEnhancedFwk();
3406 ALOGW("%s: is_vendor_enhanced_fwk %d", __func__, is_vendor_enhanced_fwk);
3407 } else {
3408 is_vendor_enhanced_fwk = 0;
3409 ALOGW("%s: default to non enhanced_fwk config", __func__);
3410 }
3411
3412 return (bool)is_vendor_enhanced_fwk;
3413}
3414
3415int audio_extn_utils_get_vendor_enhanced_info()
3416{
3417 static int vendor_enhanced_info = -EINVAL;
3418 if (vendor_enhanced_info != -EINVAL)
3419 return vendor_enhanced_info;
3420
3421 vndk_fwk_init();
3422
3423 if (mVndkFwk.getVendorEnhancedInfo != NULL) {
3424 vendor_enhanced_info = mVndkFwk.getVendorEnhancedInfo();
3425 ALOGW("%s: vendor_enhanced_info 0x%x", __func__, vendor_enhanced_info);
3426 } else {
3427 vendor_enhanced_info = 0x0;
3428 ALOGW("%s: default to vendor_enhanced_info 0x0", __func__);
3429 }
3430
3431 return vendor_enhanced_info;
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -07003432}
Deeraj Soman14230922019-01-30 16:39:30 +05303433
Deeraj Somanfa377bf2019-02-06 12:57:59 +05303434int audio_extn_utils_get_perf_mode_flag(void)
3435{
3436#ifdef COMPRESSED_PERF_MODE_FLAG
3437 return COMPRESSED_PERF_MODE_FLAG;
3438#else
3439 return 0;
3440#endif
3441}
3442
Deeraj Soman14230922019-01-30 16:39:30 +05303443size_t audio_extn_utils_get_input_buffer_size(uint32_t sample_rate,
3444 audio_format_t format,
3445 int channel_count,
3446 int64_t duration_ms,
3447 bool is_low_latency)
3448{
3449 size_t size = 0;
3450 size_t capture_duration = AUDIO_CAPTURE_PERIOD_DURATION_MSEC;
3451 uint32_t bytes_per_period_sample = 0;
3452
3453
3454 if (audio_extn_utils_check_input_parameters(sample_rate, format, channel_count) != 0)
3455 return 0;
3456
3457 if (duration_ms >= MIN_OFFLOAD_BUFFER_DURATION_MS && duration_ms <= MAX_OFFLOAD_BUFFER_DURATION_MS)
3458 capture_duration = duration_ms;
3459
3460 size = (sample_rate * capture_duration) / 1000;
3461 if (is_low_latency)
3462 size = LOW_LATENCY_CAPTURE_PERIOD_SIZE;
3463
3464
3465 bytes_per_period_sample = audio_bytes_per_sample(format) * channel_count;
3466 size *= bytes_per_period_sample;
3467
3468 /* make sure the size is multiple of 32 bytes and additionally multiple of
3469 * the frame_size (required for 24bit samples and non-power-of-2 channel counts)
3470 * At 48 kHz mono 16-bit PCM:
3471 * 5.000 ms = 240 frames = 15*16*1*2 = 480, a whole multiple of 32 (15)
3472 * 3.333 ms = 160 frames = 10*16*1*2 = 320, a whole multiple of 32 (10)
3473 *
3474 * The loop reaches result within 32 iterations, as initial size is
3475 * already a multiple of frame_size
3476 */
3477 size = audio_extn_utils_nearest_multiple(size, audio_extn_utils_lcm(32, bytes_per_period_sample));
3478
3479 return size;
3480}
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07003481
3482int audio_extn_utils_hash_fn(void *key)
3483{
3484 return (int)key;
3485}
3486
3487bool audio_extn_utils_hash_eq(void *key1, void *key2)
3488{
3489 return (key1 == key2);
3490}