blob: d7eeb3538ac285603ffbb5745ad66dad3bd3d693 [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 *
19 * Copyright (c) 2023 Qualcomm Innovation Center, Inc. All rights reserved.
20 * Not a contribution.
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070021 */
22
23#define LOG_TAG "audio_hw_utils"
24/* #define LOG_NDEBUG 0 */
25
Manish Dewangan27346042017-03-01 12:56:12 +053026#include <inttypes.h>
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070027#include <errno.h>
28#include <cutils/properties.h>
29#include <cutils/config_utils.h>
30#include <stdlib.h>
31#include <dlfcn.h>
32#include <cutils/str_parms.h>
Aalique Grahame22e49102018-12-18 14:23:57 -080033#include <log/log.h>
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070034#include <cutils/misc.h>
Vinay Vermaaddfa4a2018-04-29 14:03:38 +053035#include <unistd.h>
Surendar Karka287348c2019-04-10 18:31:46 +053036#include <sys/ioctl.h>
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070037
Manish Dewangan07de2142017-02-27 19:27:20 +053038
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070039#include "audio_hw.h"
40#include "platform.h"
41#include "platform_api.h"
42#include "audio_extn.h"
Deeraj Soman14230922019-01-30 16:39:30 +053043#include "voice_extn.h"
Narsinga Rao Chella212e2542014-11-17 19:57:04 -080044#include "voice.h"
Manish Dewangan07de2142017-02-27 19:27:20 +053045#include <sound/compress_params.h>
46#include <sound/compress_offload.h>
Surendar Karka287348c2019-04-10 18:31:46 +053047#include <sound/devdep_params.h>
Manish Dewangan07de2142017-02-27 19:27:20 +053048#include <tinycompress/tinycompress.h>
Revathi Uddaraju1eac8b02017-05-18 17:13:33 +053049
50#ifdef DYNAMIC_LOG_ENABLED
51#include <log_xml_parser.h>
52#define LOG_MASK HAL_MOD_FILE_UTILS
53#include <log_utils.h>
54#endif
55
Ashish Jain81eb2a82015-05-13 10:52:34 +053056#ifdef AUDIO_EXTERNAL_HDMI_ENABLED
Ashish Jain81eb2a82015-05-13 10:52:34 +053057#include "audio_parsers.h"
58#endif
Ashish Jain81eb2a82015-05-13 10:52:34 +053059
Roopesh Nataraja0f68be72020-04-28 12:32:02 -070060#define AUDIO_IO_POLICY_VENDOR_CONFIG_FILE_NAME "audio_io_policy.conf"
61#define AUDIO_OUTPUT_POLICY_VENDOR_CONFIG_FILE_NAME "audio_output_policy.conf"
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070062
63#define OUTPUTS_TAG "outputs"
Dhananjay Kumard6d32152016-10-13 16:11:03 +053064#define INPUTS_TAG "inputs"
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070065
66#define DYNAMIC_VALUE_TAG "dynamic"
67#define FLAGS_TAG "flags"
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +053068#define PROFILES_TAG "profile"
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070069#define FORMATS_TAG "formats"
70#define SAMPLING_RATES_TAG "sampling_rates"
71#define BIT_WIDTH_TAG "bit_width"
72#define APP_TYPE_TAG "app_type"
73
74#define STRING_TO_ENUM(string) { #string, string }
75#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
76
Ben Rombergera04fabc2014-11-14 12:16:03 -080077#define BASE_TABLE_SIZE 64
78#define MAX_BASEINDEX_LEN 256
79
Ben Romberger1aaaf862017-04-06 17:49:46 -070080#ifndef SND_AUDIOCODEC_TRUEHD
81#define SND_AUDIOCODEC_TRUEHD 0x00000023
82#endif
83
Vikram Panduranga93f080e2017-06-07 18:16:14 -070084#define APP_TYPE_VOIP_AUDIO 0x1113A
85
Ashish Jain81eb2a82015-05-13 10:52:34 +053086#ifdef AUDIO_EXTERNAL_HDMI_ENABLED
87#define PROFESSIONAL (1<<0) /* 0 = consumer, 1 = professional */
88#define NON_LPCM (1<<1) /* 0 = audio, 1 = non-audio */
89#define SR_44100 (0<<0) /* 44.1kHz */
90#define SR_NOTID (1<<0) /* non indicated */
91#define SR_48000 (2<<0) /* 48kHz */
92#define SR_32000 (3<<0) /* 32kHz */
93#define SR_22050 (4<<0) /* 22.05kHz */
94#define SR_24000 (6<<0) /* 24kHz */
95#define SR_88200 (8<<0) /* 88.2kHz */
96#define SR_96000 (10<<0) /* 96kHz */
97#define SR_176400 (12<<0) /* 176.4kHz */
98#define SR_192000 (14<<0) /* 192kHz */
99
100#endif
Manish Dewangan07de2142017-02-27 19:27:20 +0530101
102/* ToDo: Check and update a proper value in msec */
Weiyin Jiangc49a3b52018-08-17 16:16:08 +0800103#define COMPRESS_OFFLOAD_PLAYBACK_LATENCY 50
104#define PCM_OFFLOAD_PLAYBACK_DSP_PATHDELAY 62
Samyak Jain2546f8a2019-08-29 19:24:21 +0530105#define PCM_OFFLOAD_PLAYBACK_LATENCY PCM_OFFLOAD_PLAYBACK_DSP_PATHDELAY
Manish Dewangan07de2142017-02-27 19:27:20 +0530106
Varun Balaraje49253e2017-07-06 19:48:56 +0530107#ifndef MAX_CHANNELS_SUPPORTED
108#define MAX_CHANNELS_SUPPORTED 8
109#endif
110
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -0700111#ifdef __LP64__
112#define VNDK_FWK_LIB_PATH "/vendor/lib64/libqti_vndfwk_detect.so"
113#else
114#define VNDK_FWK_LIB_PATH "/vendor/lib/libqti_vndfwk_detect.so"
115#endif
116
Krishna Kishor Jhac1542b22022-08-22 13:12:09 +0530117#ifdef PLATFORM_AUTO
Kogara Naveen Kumar51bb57d2022-02-17 16:40:06 +0530118/* 24 KHz ECNR support */
119#define ECNS_USE_CASE_ACDB_DEV_ID 95
120#define ECNS_UNSUPPORTED_CAPTURE_SAMPLE_RATE_FOR_ADM 24000
121#define ECNS_SUPPORTED_CAPTURE_SAMPLE_RATE_FOR_ADM 48000
Krishna Kishor Jhac1542b22022-08-22 13:12:09 +0530122#endif
Kogara Naveen Kumar51bb57d2022-02-17 16:40:06 +0530123
Weiyin Jiangd5718bd2019-09-04 16:52:08 +0800124typedef struct vndkfwk_s {
125 void *lib_handle;
126 int (*isVendorEnhancedFwk)(void);
127 int (*getVendorEnhancedInfo)(void);
128 const char *lib_name;
129} vndkfwk_t;
130
131static vndkfwk_t mVndkFwk = {
132 NULL, NULL, NULL, VNDK_FWK_LIB_PATH};
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -0700133
`Deeraj Soman676c2702017-09-18 19:25:53 +0530134typedef struct {
135 const char *id_string;
136 const int value;
137} mixer_config_lookup;
138
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700139struct string_to_enum {
140 const char *name;
141 uint32_t value;
142};
143
144const struct string_to_enum s_flag_name_to_enum_table[] = {
145 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_DIRECT),
146 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_PRIMARY),
147 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_FAST),
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800148 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_RAW),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700149 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_DEEP_BUFFER),
150 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD),
151 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_NON_BLOCKING),
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700152 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_HW_AV_SYNC),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700153 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_INCALL_MUSIC),
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700154 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH),
Naresh Tanniruee3499a2017-01-05 14:05:35 +0530155 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_TIMESTAMP),
Vikram Panduranga93f080e2017-06-07 18:16:14 -0700156 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_VOIP_RX),
Ben Romberger6c4d3812017-06-13 17:46:45 -0700157 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_BD),
Varun Balaraje49253e2017-07-06 19:48:56 +0530158 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_INTERACTIVE),
Derek Chen090dfc52018-03-22 22:15:29 -0400159 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_MEDIA),
160 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_SYS_NOTIFICATION),
161 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_NAV_GUIDANCE),
162 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_PHONE),
Susan Wang117cf6f2022-04-06 20:11:46 -0400163 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_ALERTS),
Derek Chen1bcdc6b2020-02-06 22:44:53 -0800164 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_FRONT_PASSENGER),
Derek Chendf05eea2019-08-01 13:57:49 -0700165 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_REAR_SEAT),
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530166 STRING_TO_ENUM(AUDIO_INPUT_FLAG_NONE),
167 STRING_TO_ENUM(AUDIO_INPUT_FLAG_FAST),
168 STRING_TO_ENUM(AUDIO_INPUT_FLAG_HW_HOTWORD),
169 STRING_TO_ENUM(AUDIO_INPUT_FLAG_RAW),
170 STRING_TO_ENUM(AUDIO_INPUT_FLAG_SYNC),
Dhananjay Kumaree4d2002016-10-25 18:02:58 +0530171 STRING_TO_ENUM(AUDIO_INPUT_FLAG_TIMESTAMP),
Dhananjay Kumar704ce6f2017-09-28 22:08:00 +0530172 STRING_TO_ENUM(AUDIO_INPUT_FLAG_COMPRESS),
Ralf Herzaec80262018-07-03 07:08:49 +0200173 STRING_TO_ENUM(AUDIO_INPUT_FLAG_PASSTHROUGH),
George Gao3d477082020-09-17 22:29:07 -0700174 STRING_TO_ENUM(AUDIO_INPUT_FLAG_MMAP_NOIRQ),
175 STRING_TO_ENUM(AUDIO_INPUT_FLAG_VOIP_TX),
176 STRING_TO_ENUM(AUDIO_INPUT_FLAG_HW_AV_SYNC),
177 STRING_TO_ENUM(AUDIO_INPUT_FLAG_DIRECT),
Huicheng Liu1404ba12020-09-11 01:03:25 -0400178 STRING_TO_ENUM(AUDIO_INPUT_FLAG_PRIMARY),
179 STRING_TO_ENUM(AUDIO_INPUT_FLAG_FRONT_PASSENGER),
180 STRING_TO_ENUM(AUDIO_INPUT_FLAG_REAR_SEAT),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700181};
182
183const struct string_to_enum s_format_name_to_enum_table[] = {
Ashish Jain83a6cc22016-06-28 14:34:17 +0530184 STRING_TO_ENUM(AUDIO_FORMAT_PCM_8_BIT),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700185 STRING_TO_ENUM(AUDIO_FORMAT_PCM_16_BIT),
Ashish Jain5106d362016-05-11 19:23:33 +0530186 STRING_TO_ENUM(AUDIO_FORMAT_PCM_24_BIT_PACKED),
187 STRING_TO_ENUM(AUDIO_FORMAT_PCM_8_24_BIT),
Ashish Jainf1eaa582016-05-23 20:54:24 +0530188 STRING_TO_ENUM(AUDIO_FORMAT_PCM_32_BIT),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700189 STRING_TO_ENUM(AUDIO_FORMAT_MP3),
190 STRING_TO_ENUM(AUDIO_FORMAT_AAC),
191 STRING_TO_ENUM(AUDIO_FORMAT_VORBIS),
Mingming Yinae3530f2014-07-03 16:50:18 -0700192 STRING_TO_ENUM(AUDIO_FORMAT_AMR_NB),
193 STRING_TO_ENUM(AUDIO_FORMAT_AMR_WB),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700194 STRING_TO_ENUM(AUDIO_FORMAT_AC3),
Mingming Yinae3530f2014-07-03 16:50:18 -0700195 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700196 STRING_TO_ENUM(AUDIO_FORMAT_DTS),
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530197 STRING_TO_ENUM(AUDIO_FORMAT_DTS_HD),
Ben Romberger1aaaf862017-04-06 17:49:46 -0700198 STRING_TO_ENUM(AUDIO_FORMAT_DOLBY_TRUEHD),
Naresh Tanniru928f0862017-04-07 16:44:23 -0700199 STRING_TO_ENUM(AUDIO_FORMAT_IEC61937),
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530200 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3_JOC),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700201 STRING_TO_ENUM(AUDIO_FORMAT_WMA),
202 STRING_TO_ENUM(AUDIO_FORMAT_WMA_PRO),
203 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADIF),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700204 STRING_TO_ENUM(AUDIO_FORMAT_AMR_WB_PLUS),
205 STRING_TO_ENUM(AUDIO_FORMAT_EVRC),
206 STRING_TO_ENUM(AUDIO_FORMAT_EVRCB),
207 STRING_TO_ENUM(AUDIO_FORMAT_EVRCWB),
208 STRING_TO_ENUM(AUDIO_FORMAT_QCELP),
209 STRING_TO_ENUM(AUDIO_FORMAT_MP2),
210 STRING_TO_ENUM(AUDIO_FORMAT_EVRCNW),
Amit Shekhar6f461b12014-08-01 14:52:58 -0700211 STRING_TO_ENUM(AUDIO_FORMAT_FLAC),
Satya Krishna Pindiproli70471602015-04-24 19:12:43 +0530212 STRING_TO_ENUM(AUDIO_FORMAT_ALAC),
213 STRING_TO_ENUM(AUDIO_FORMAT_APE),
Alexy Josephcd8eaed2014-12-11 12:46:53 -0800214 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LC),
215 STRING_TO_ENUM(AUDIO_FORMAT_AAC_HE_V1),
216 STRING_TO_ENUM(AUDIO_FORMAT_AAC_HE_V2),
Manish Dewangana6fc5442015-08-24 20:30:31 +0530217 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS),
Ashish Jaine513a872015-11-19 17:00:56 +0530218 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_LC),
219 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_HE_V1),
220 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_HE_V2),
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +0530221 STRING_TO_ENUM(AUDIO_FORMAT_DSD),
Arun Kumar Dasari3b174182016-12-27 13:01:14 +0530222 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM),
223 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM_LC),
224 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM_HE_V1),
225 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM_HE_V2),
Dhanalakshmi Siddani18737932016-11-29 17:33:17 +0530226 STRING_TO_ENUM(AUDIO_FORMAT_APTX),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700227};
228
Manish Dewangan3ccdea52017-02-13 19:31:54 +0530229/* payload structure avt_device drift query */
230struct audio_avt_device_drift_stats {
231 uint32_t minor_version;
Manish Dewangan338c50a2017-09-12 15:22:03 +0530232
Manish Dewangan3ccdea52017-02-13 19:31:54 +0530233 /* Indicates the device interface direction as either
234 * source (Tx) or sink (Rx).
235 */
236 uint16_t device_direction;
Manish Dewangan338c50a2017-09-12 15:22:03 +0530237
238 /* Reference timer for drift accumulation and time stamp information.
239 * currently it only support AFE_REF_TIMER_TYPE_AVTIMER
240 */
241 uint16_t reference_timer;
Manish Dewangan3ccdea52017-02-13 19:31:54 +0530242 struct audio_avt_device_drift_param drift_param;
Manish Dewangan338c50a2017-09-12 15:22:03 +0530243} __attribute__((packed));
Manish Dewangan3ccdea52017-02-13 19:31:54 +0530244
Ben Rombergera04fabc2014-11-14 12:16:03 -0800245static char bTable[BASE_TABLE_SIZE] = {
246 'A','B','C','D','E','F','G','H','I','J','K','L',
247 'M','N','O','P','Q','R','S','T','U','V','W','X',
248 'Y','Z','a','b','c','d','e','f','g','h','i','j',
249 'k','l','m','n','o','p','q','r','s','t','u','v',
250 'w','x','y','z','0','1','2','3','4','5','6','7',
251 '8','9','+','/'
252};
253
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700254static uint32_t string_to_enum(const struct string_to_enum *table, size_t size,
255 const char *name)
256{
257 size_t i;
258 for (i = 0; i < size; i++) {
259 if (strcmp(table[i].name, name) == 0) {
260 ALOGV("%s found %s", __func__, table[i].name);
261 return table[i].value;
262 }
263 }
George Gao3d477082020-09-17 22:29:07 -0700264 ALOGE("%s cound not find %s", __func__, name);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700265 return 0;
266}
267
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530268static audio_io_flags_t parse_flag_names(char *name)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700269{
270 uint32_t flag = 0;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530271 audio_io_flags_t io_flags;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800272 char *last_r;
273 char *flag_name = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700274 while (flag_name != NULL) {
275 if (strlen(flag_name) != 0) {
276 flag |= string_to_enum(s_flag_name_to_enum_table,
277 ARRAY_SIZE(s_flag_name_to_enum_table),
278 flag_name);
279 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800280 flag_name = strtok_r(NULL, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700281 }
282
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800283 ALOGV("parse_flag_names: flag - %x", flag);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530284 io_flags.in_flags = (audio_input_flags_t)flag;
285 io_flags.out_flags = (audio_output_flags_t)flag;
286 return io_flags;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700287}
288
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530289static void parse_format_names(char *name, struct streams_io_cfg *s_info)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700290{
291 struct stream_format *sf_info = NULL;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800292 char *last_r;
293 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700294
295 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG) == 0)
296 return;
297
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530298 list_init(&s_info->format_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700299 while (str != NULL) {
300 audio_format_t format = (audio_format_t)string_to_enum(s_format_name_to_enum_table,
301 ARRAY_SIZE(s_format_name_to_enum_table), str);
302 ALOGV("%s: format - %d", __func__, format);
303 if (format != 0) {
304 sf_info = (struct stream_format *)calloc(1, sizeof(struct stream_format));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700305 if (sf_info == NULL)
306 break; /* return whatever was parsed */
307
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700308 sf_info->format = format;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530309 list_add_tail(&s_info->format_list, &sf_info->list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700310 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800311 str = strtok_r(NULL, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700312 }
313}
314
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530315static void parse_sample_rate_names(char *name, struct streams_io_cfg *s_info)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700316{
Amit Shekhar6f461b12014-08-01 14:52:58 -0700317 struct stream_sample_rate *ss_info = NULL;
318 uint32_t sample_rate = 48000;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800319 char *last_r;
320 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700321
Amit Shekhar6f461b12014-08-01 14:52:58 -0700322 if (str != NULL && 0 == strcmp(str, DYNAMIC_VALUE_TAG))
323 return;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700324
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530325 list_init(&s_info->sample_rate_list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700326 while (str != NULL) {
327 sample_rate = (uint32_t)strtol(str, (char **)NULL, 10);
328 ALOGV("%s: sample_rate - %d", __func__, sample_rate);
329 if (0 != sample_rate) {
330 ss_info = (struct stream_sample_rate *)calloc(1, sizeof(struct stream_sample_rate));
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800331 if (!ss_info) {
332 ALOGE("%s: memory allocation failure", __func__);
333 return;
334 }
Amit Shekhar6f461b12014-08-01 14:52:58 -0700335 ss_info->sample_rate = sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530336 list_add_tail(&s_info->sample_rate_list, &ss_info->list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700337 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800338 str = strtok_r(NULL, "|", &last_r);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700339 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700340}
341
342static int parse_bit_width_names(char *name)
343{
344 int bit_width = 16;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800345 char *last_r;
346 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700347
348 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG))
349 bit_width = (int)strtol(str, (char **)NULL, 10);
350
351 ALOGV("%s: bit_width - %d", __func__, bit_width);
352 return bit_width;
353}
354
355static int parse_app_type_names(void *platform, char *name)
356{
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700357 int app_type = platform_get_default_app_type(platform);
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800358 char *last_r;
359 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700360
361 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG))
362 app_type = (int)strtol(str, (char **)NULL, 10);
363
364 ALOGV("%s: app_type - %d", __func__, app_type);
365 return app_type;
366}
367
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530368static void update_streams_cfg_list(cnode *root, void *platform,
369 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700370{
371 cnode *node = root->first_child;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530372 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700373
374 ALOGV("%s", __func__);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530375 s_info = (struct streams_io_cfg *)calloc(1, sizeof(struct streams_io_cfg));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700376
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530377 if (!s_info) {
378 ALOGE("failed to allocate mem for s_info list element");
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700379 return;
380 }
381
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700382 while (node) {
383 if (strcmp(node->name, FLAGS_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530384 s_info->flags = parse_flag_names((char *)node->value);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530385 } else if (strcmp(node->name, PROFILES_TAG) == 0) {
386 strlcpy(s_info->profile, (char *)node->value, sizeof(s_info->profile));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700387 } else if (strcmp(node->name, FORMATS_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530388 parse_format_names((char *)node->value, s_info);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700389 } else if (strcmp(node->name, SAMPLING_RATES_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530390 s_info->app_type_cfg.sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
391 parse_sample_rate_names((char *)node->value, s_info);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700392 } else if (strcmp(node->name, BIT_WIDTH_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530393 s_info->app_type_cfg.bit_width = parse_bit_width_names((char *)node->value);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700394 } else if (strcmp(node->name, APP_TYPE_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530395 s_info->app_type_cfg.app_type = parse_app_type_names(platform, (char *)node->value);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700396 }
397 node = node->next;
398 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530399 list_add_tail(streams_cfg_list, &s_info->list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700400}
401
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530402static void load_cfg_list(cnode *root, void *platform,
403 struct listnode *streams_output_cfg_list,
404 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700405{
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530406 cnode *node = NULL;
407
408 node = config_find(root, OUTPUTS_TAG);
409 if (node != NULL) {
410 node = node->first_child;
411 while (node) {
412 ALOGV("%s: loading output %s", __func__, node->name);
413 update_streams_cfg_list(node, platform, streams_output_cfg_list);
414 node = node->next;
415 }
416 } else {
417 ALOGI("%s: could not load output, node is NULL", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700418 }
419
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530420 node = config_find(root, INPUTS_TAG);
421 if (node != NULL) {
422 node = node->first_child;
423 while (node) {
424 ALOGV("%s: loading input %s", __func__, node->name);
425 update_streams_cfg_list(node, platform, streams_input_cfg_list);
426 node = node->next;
427 }
428 } else {
429 ALOGI("%s: could not load input, node is NULL", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700430 }
431}
432
433static void send_app_type_cfg(void *platform, struct mixer *mixer,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530434 struct listnode *streams_output_cfg_list,
435 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700436{
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530437 size_t app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT] = {0};
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700438 int length = 0, i, num_app_types = 0;
439 struct listnode *node;
440 bool update;
441 struct mixer_ctl *ctl = NULL;
442 const char *mixer_ctl_name = "App Type Config";
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530443 struct streams_io_cfg *s_info = NULL;
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800444 uint32_t target_bit_width = 0;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700445
446 if (!mixer) {
447 ALOGE("%s: mixer is null",__func__);
448 return;
449 }
450 ctl = mixer_get_ctl_by_name(mixer, mixer_ctl_name);
451 if (!ctl) {
452 ALOGE("%s: Could not get ctl for mixer cmd - %s",__func__, mixer_ctl_name);
453 return;
454 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530455 app_type_cfg[length++] = num_app_types;
456
457 if (list_empty(streams_output_cfg_list)) {
458 app_type_cfg[length++] = platform_get_default_app_type_v2(platform, PCM_PLAYBACK);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700459 app_type_cfg[length++] = 48000;
460 app_type_cfg[length++] = 16;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530461 num_app_types += 1;
462 }
463 if (list_empty(streams_input_cfg_list)) {
464 app_type_cfg[length++] = platform_get_default_app_type_v2(platform, PCM_CAPTURE);
465 app_type_cfg[length++] = 48000;
466 app_type_cfg[length++] = 16;
467 num_app_types += 1;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700468 }
469
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800470 /* get target bit width for ADM enforce mode */
471 target_bit_width = adev_get_dsp_bit_width_enforce_mode();
472
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700473 list_for_each(node, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530474 s_info = node_to_item(node, struct streams_io_cfg, list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700475 update = true;
476 for (i=0; i<length; i=i+3) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530477 if (app_type_cfg[i+1] == 0)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700478 break;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530479 else if (app_type_cfg[i+1] == (size_t)s_info->app_type_cfg.app_type) {
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530480 if (app_type_cfg[i+2] < (size_t)s_info->app_type_cfg.sample_rate)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530481 app_type_cfg[i+2] = s_info->app_type_cfg.sample_rate;
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530482 if (app_type_cfg[i+3] < (size_t)s_info->app_type_cfg.bit_width)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530483 app_type_cfg[i+3] = s_info->app_type_cfg.bit_width;
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800484 /* ADM bit width = max(enforce_bit_width, bit_width from s_info */
485 if (audio_extn_is_dsp_bit_width_enforce_mode_supported(s_info->flags.out_flags) &&
486 (target_bit_width > app_type_cfg[i+3]))
487 app_type_cfg[i+3] = target_bit_width;
488
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700489 update = false;
490 break;
491 }
492 }
493 if (update && ((length + 3) <= MAX_LENGTH_MIXER_CONTROL_IN_INT)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530494 num_app_types += 1;
495 app_type_cfg[length++] = s_info->app_type_cfg.app_type;
496 app_type_cfg[length++] = s_info->app_type_cfg.sample_rate;
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800497 app_type_cfg[length] = s_info->app_type_cfg.bit_width;
498 if (audio_extn_is_dsp_bit_width_enforce_mode_supported(s_info->flags.out_flags) &&
499 (target_bit_width > app_type_cfg[length]))
500 app_type_cfg[length] = target_bit_width;
501
502 length++;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530503 }
504 }
505 list_for_each(node, streams_input_cfg_list) {
506 s_info = node_to_item(node, struct streams_io_cfg, list);
507 update = true;
508 for (i=0; i<length; i=i+3) {
509 if (app_type_cfg[i+1] == 0)
510 break;
511 else if (app_type_cfg[i+1] == (size_t)s_info->app_type_cfg.app_type) {
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530512 if (app_type_cfg[i+2] < (size_t)s_info->app_type_cfg.sample_rate)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530513 app_type_cfg[i+2] = s_info->app_type_cfg.sample_rate;
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530514 if (app_type_cfg[i+3] < (size_t)s_info->app_type_cfg.bit_width)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530515 app_type_cfg[i+3] = s_info->app_type_cfg.bit_width;
516 update = false;
517 break;
518 }
519 }
520 if (update && ((length + 3) <= MAX_LENGTH_MIXER_CONTROL_IN_INT)) {
521 num_app_types += 1;
522 app_type_cfg[length++] = s_info->app_type_cfg.app_type;
523 app_type_cfg[length++] = s_info->app_type_cfg.sample_rate;
524 app_type_cfg[length++] = s_info->app_type_cfg.bit_width;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700525 }
526 }
527 ALOGV("%s: num_app_types: %d", __func__, num_app_types);
528 if (num_app_types) {
529 app_type_cfg[0] = num_app_types;
530 mixer_ctl_set_array(ctl, app_type_cfg, length);
531 }
532}
533
Roopesh Nataraja0f68be72020-04-28 12:32:02 -0700534/* Function to retrieve audio vendor configs path */
535void audio_get_vendor_config_path (char* config_file_path, int path_size)
536{
537 char vendor_sku[PROPERTY_VALUE_MAX] = {'\0'};
538 if (property_get("ro.boot.product.vendor.sku", vendor_sku, "") <= 0) {
539#ifdef LINUX_ENABLED
540 /* Audio configs are stored in /etc */
541 snprintf(config_file_path, path_size, "%s", "/etc");
542#else
543 /* Audio configs are stored in /vendor/etc */
544 snprintf(config_file_path, path_size, "%s", "/vendor/etc");
545#endif
546 } else {
547 /* Audio configs are stored in /vendor/etc/audio/sku_${vendor_sku} */
548 snprintf(config_file_path, path_size,
549 "%s%s", "/vendor/etc/audio/sku_", vendor_sku);
550 }
551}
552
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530553void audio_extn_utils_update_streams_cfg_lists(void *platform,
554 struct mixer *mixer,
555 struct listnode *streams_output_cfg_list,
556 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700557{
558 cnode *root;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530559 char *data = NULL;
Roopesh Nataraja0f68be72020-04-28 12:32:02 -0700560 char vendor_config_path[VENDOR_CONFIG_PATH_MAX_LENGTH];
561 char audio_io_policy_file[VENDOR_CONFIG_FILE_MAX_LENGTH];
562 char audio_output_policy_file[VENDOR_CONFIG_FILE_MAX_LENGTH];
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700563
564 ALOGV("%s", __func__);
565 list_init(streams_output_cfg_list);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530566 list_init(streams_input_cfg_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700567
568 root = config_node("", "");
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700569 if (root == NULL) {
570 ALOGE("cfg_list, NULL config root");
571 return;
572 }
573
Roopesh Nataraja0f68be72020-04-28 12:32:02 -0700574 /* Get path for audio configuration files in vendor */
575 audio_get_vendor_config_path(vendor_config_path,
576 sizeof(vendor_config_path));
577
578 /* Get path for audio_io_policy_file in vendor */
579 snprintf(audio_io_policy_file, sizeof(audio_io_policy_file),
580 "%s/%s", vendor_config_path, AUDIO_IO_POLICY_VENDOR_CONFIG_FILE_NAME);
581
582 /* Load audio_io_policy_file from vendor */
583 data = (char *)load_file(audio_io_policy_file, NULL);
584
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530585 if (data == NULL) {
586 ALOGD("%s: failed to open io config file(%s), trying older config file",
Roopesh Nataraja0f68be72020-04-28 12:32:02 -0700587 __func__, audio_io_policy_file);
588
589 /* Get path for audio_output_policy_file in vendor */
590 snprintf(audio_output_policy_file, sizeof(audio_output_policy_file),
591 "%s/%s", vendor_config_path,
592 AUDIO_OUTPUT_POLICY_VENDOR_CONFIG_FILE_NAME);
593
594 /* Load audio_output_policy_file from vendor */
595 data = (char *)load_file(audio_output_policy_file, NULL);
596
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530597 if (data == NULL) {
598 send_app_type_cfg(platform, mixer,
599 streams_output_cfg_list,
600 streams_input_cfg_list);
601 ALOGE("%s: could not load io policy config!", __func__);
yidongh6eb4f752017-04-19 18:20:57 +0800602 free(root);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530603 return;
604 }
605 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700606
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530607 config_load(root, data);
608 load_cfg_list(root, platform, streams_output_cfg_list,
609 streams_input_cfg_list);
610
611 send_app_type_cfg(platform, mixer, streams_output_cfg_list,
612 streams_input_cfg_list);
Alexy Josephaee4fdd2016-01-29 13:02:07 -0800613
614 config_free(root);
yidongh6eb4f752017-04-19 18:20:57 +0800615 free(root);
Alexy Josephaee4fdd2016-01-29 13:02:07 -0800616 free(data);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700617}
618
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530619static void audio_extn_utils_dump_streams_cfg_list(
620 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700621{
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700622 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530623 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700624 struct stream_format *sf_info;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700625 struct stream_sample_rate *ss_info;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530626
627 list_for_each(node_i, streams_cfg_list) {
628 s_info = node_to_item(node_i, struct streams_io_cfg, list);
629 ALOGV("%s: flags-%d, sample_rate-%d, bit_width-%d, app_type-%d",
630 __func__, s_info->flags.out_flags, s_info->app_type_cfg.sample_rate,
631 s_info->app_type_cfg.bit_width, s_info->app_type_cfg.app_type);
632 list_for_each(node_j, &s_info->format_list) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700633 sf_info = node_to_item(node_j, struct stream_format, list);
634 ALOGV("format-%x", sf_info->format);
635 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530636 list_for_each(node_j, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700637 ss_info = node_to_item(node_j, struct stream_sample_rate, list);
638 ALOGV("sample rate-%d", ss_info->sample_rate);
639 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700640 }
641}
642
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530643void audio_extn_utils_dump_streams_cfg_lists(
644 struct listnode *streams_output_cfg_list,
645 struct listnode *streams_input_cfg_list)
646{
647 ALOGV("%s", __func__);
648 audio_extn_utils_dump_streams_cfg_list(streams_output_cfg_list);
649 audio_extn_utils_dump_streams_cfg_list(streams_input_cfg_list);
650}
651
652static void audio_extn_utils_release_streams_cfg_list(
653 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700654{
655 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530656 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700657
658 ALOGV("%s", __func__);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530659
660 while (!list_empty(streams_cfg_list)) {
661 node_i = list_head(streams_cfg_list);
662 s_info = node_to_item(node_i, struct streams_io_cfg, list);
663 while (!list_empty(&s_info->format_list)) {
664 node_j = list_head(&s_info->format_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700665 list_remove(node_j);
666 free(node_to_item(node_j, struct stream_format, list));
667 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530668 while (!list_empty(&s_info->sample_rate_list)) {
669 node_j = list_head(&s_info->sample_rate_list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700670 list_remove(node_j);
671 free(node_to_item(node_j, struct stream_sample_rate, list));
672 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700673 list_remove(node_i);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530674 free(node_to_item(node_i, struct streams_io_cfg, list));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700675 }
676}
677
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530678void audio_extn_utils_release_streams_cfg_lists(
679 struct listnode *streams_output_cfg_list,
680 struct listnode *streams_input_cfg_list)
681{
682 ALOGV("%s", __func__);
683 audio_extn_utils_release_streams_cfg_list(streams_output_cfg_list);
684 audio_extn_utils_release_streams_cfg_list(streams_input_cfg_list);
685}
686
687static bool set_app_type_cfg(struct streams_io_cfg *s_info,
688 struct stream_app_type_cfg *app_type_cfg,
689 uint32_t sample_rate, uint32_t bit_width)
Amit Shekhar6f461b12014-08-01 14:52:58 -0700690 {
691 struct listnode *node_i;
692 struct stream_sample_rate *ss_info;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530693 list_for_each(node_i, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700694 ss_info = node_to_item(node_i, struct stream_sample_rate, list);
695 if ((sample_rate <= ss_info->sample_rate) &&
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530696 (bit_width == s_info->app_type_cfg.bit_width)) {
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700697
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530698 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700699 app_type_cfg->sample_rate = ss_info->sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530700 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700701 ALOGV("%s app_type_cfg->app_type %d, app_type_cfg->sample_rate %d, app_type_cfg->bit_width %d",
702 __func__, app_type_cfg->app_type, app_type_cfg->sample_rate, app_type_cfg->bit_width);
703 return true;
704 }
705 }
706 /*
707 * Reiterate through the list assuming dafault sample rate.
708 * Handles scenario where input sample rate is higher
709 * than all sample rates in list for the input bit width.
710 */
711 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
Apoorv Raghuvanshif59bb222015-02-18 12:23:23 -0800712
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530713 list_for_each(node_i, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700714 ss_info = node_to_item(node_i, struct stream_sample_rate, list);
715 if ((sample_rate <= ss_info->sample_rate) &&
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530716 (bit_width == s_info->app_type_cfg.bit_width)) {
717 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700718 app_type_cfg->sample_rate = sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530719 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Apoorv Raghuvanshif59bb222015-02-18 12:23:23 -0800720 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 -0700721 __func__, app_type_cfg->app_type, app_type_cfg->sample_rate, app_type_cfg->bit_width);
722 return true;
723 }
724 }
725 return false;
726}
727
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530728void audio_extn_utils_update_stream_input_app_type_cfg(void *platform,
729 struct listnode *streams_input_cfg_list,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -0800730 struct listnode *devices __unused,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530731 audio_input_flags_t flags,
732 audio_format_t format,
733 uint32_t sample_rate,
734 uint32_t bit_width,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530735 char* profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530736 struct stream_app_type_cfg *app_type_cfg)
737{
738 struct listnode *node_i, *node_j;
739 struct streams_io_cfg *s_info;
740 struct stream_format *sf_info;
741
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530742 ALOGV("%s: flags: 0x%x, format: 0x%x sample_rate %d, profile %s",
743 __func__, flags, format, sample_rate, profile);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530744
745 list_for_each(node_i, streams_input_cfg_list) {
746 s_info = node_to_item(node_i, struct streams_io_cfg, list);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530747 /* Along with flags do profile matching if set at either end.*/
748 if (s_info->flags.in_flags == flags &&
749 ((profile[0] == '\0' && s_info->profile[0] == '\0') ||
750 strncmp(s_info->profile, profile, sizeof(s_info->profile)) == 0)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530751 list_for_each(node_j, &s_info->format_list) {
752 sf_info = node_to_item(node_j, struct stream_format, list);
753 if (sf_info->format == format) {
754 if (set_app_type_cfg(s_info, app_type_cfg, sample_rate, bit_width))
755 return;
756 }
757 }
758 }
759 }
760 ALOGW("%s: App type could not be selected. Falling back to default", __func__);
761 app_type_cfg->app_type = platform_get_default_app_type_v2(platform, PCM_CAPTURE);
762 app_type_cfg->sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
763 app_type_cfg->bit_width = 16;
Krishna Kishor Jhac1542b22022-08-22 13:12:09 +0530764#ifdef PLATFORM_AUTO
Kogara Naveen Kumar51bb57d2022-02-17 16:40:06 +0530765 if ((flags & AUDIO_INPUT_FLAG_TIMESTAMP) == 0 &&
766 (flags & AUDIO_INPUT_FLAG_COMPRESS) == 0 &&
767 (flags & AUDIO_INPUT_FLAG_FAST) != 0) {
768 // Support low latency record for different sample rates
769 app_type_cfg->sample_rate = sample_rate;
770 }
Krishna Kishor Jhac1542b22022-08-22 13:12:09 +0530771#endif
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530772}
773
774void audio_extn_utils_update_stream_output_app_type_cfg(void *platform,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700775 struct listnode *streams_output_cfg_list,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -0800776 struct listnode *devices,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700777 audio_output_flags_t flags,
778 audio_format_t format,
Amit Shekhar6f461b12014-08-01 14:52:58 -0700779 uint32_t sample_rate,
780 uint32_t bit_width,
Manish Dewangan837dc462015-05-27 10:17:41 +0530781 audio_channel_mask_t channel_mask,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530782 char *profile,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700783 struct stream_app_type_cfg *app_type_cfg)
784{
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +0530785 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530786 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700787 struct stream_format *sf_info;
Manish Dewangan837dc462015-05-27 10:17:41 +0530788 char value[PROPERTY_VALUE_MAX] = {0};
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700789
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -0800790 if (compare_device_type(devices, AUDIO_DEVICE_OUT_SPEAKER)) {
Ramu Gottipati074fa4d2018-09-11 20:05:51 +0530791 int bw = platform_get_snd_device_bit_width(SND_DEVICE_OUT_SPEAKER);
792 if ((-ENOSYS != bw) && (bit_width > (uint32_t)bw))
Amit Shekhar5a39c912014-10-14 15:39:30 -0700793 bit_width = (uint32_t)bw;
Ramu Gottipati074fa4d2018-09-11 20:05:51 +0530794 else if (-ENOSYS == bw)
795 bit_width = CODEC_BACKEND_DEFAULT_BIT_WIDTH;
Amit Shekhar1d896042014-10-03 13:16:09 -0700796 sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Aniket Kumar Latad13758f2020-08-06 15:11:36 -0700797 ALOGV("%s Allowing 24 and above bits playback on speaker \
798 ONLY at default sampling rate", __func__);
Amit Shekhar1d896042014-10-03 13:16:09 -0700799 }
800
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -0700801 property_get("vendor.audio.playback.mch.downsample",value,"");
Manish Dewangan837dc462015-05-27 10:17:41 +0530802 if (!strncmp("true", value, sizeof("true"))) {
803 if ((popcount(channel_mask) > 2) &&
804 (sample_rate > CODEC_BACKEND_DEFAULT_SAMPLE_RATE) &&
Aalique Grahame5cd12ff2017-10-19 10:57:00 -0700805 !(flags & (audio_output_flags_t)AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH)) {
Manish Dewangan837dc462015-05-27 10:17:41 +0530806 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
807 ALOGD("%s: MCH session defaulting sample rate to %d",
808 __func__, sample_rate);
809 }
810 }
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +0530811
812 /* Set sampling rate to 176.4 for DSD64
813 * and 352.8Khz for DSD128.
814 * Set Bit Width to 16. output will be 16 bit
815 * post DoP in ASM.
816 */
Aalique Grahame5cd12ff2017-10-19 10:57:00 -0700817 if ((flags & (audio_output_flags_t)AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH) &&
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +0530818 (format == AUDIO_FORMAT_DSD)) {
819 bit_width = 16;
820 if (sample_rate == INPUT_SAMPLING_RATE_DSD64)
821 sample_rate = OUTPUT_SAMPLING_RATE_DSD64;
822 else if (sample_rate == INPUT_SAMPLING_RATE_DSD128)
823 sample_rate = OUTPUT_SAMPLING_RATE_DSD128;
824 }
825
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +0530826
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800827 ALOGV("%s: flags: %x, format: %x sample_rate %d, profile %s, app_type %d",
828 __func__, flags, format, sample_rate, profile, app_type_cfg->app_type);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700829 list_for_each(node_i, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530830 s_info = node_to_item(node_i, struct streams_io_cfg, list);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530831 /* Along with flags do profile matching if set at either end.*/
832 if (s_info->flags.out_flags == flags &&
833 ((profile[0] == '\0' && s_info->profile[0] == '\0') ||
834 strncmp(s_info->profile, profile, sizeof(s_info->profile)) == 0)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530835 list_for_each(node_j, &s_info->format_list) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700836 sf_info = node_to_item(node_j, struct stream_format, list);
837 if (sf_info->format == format) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530838 if (set_app_type_cfg(s_info, app_type_cfg, sample_rate, bit_width))
Amit Shekhar6f461b12014-08-01 14:52:58 -0700839 return;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700840 }
841 }
842 }
843 }
844 list_for_each(node_i, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530845 s_info = node_to_item(node_i, struct streams_io_cfg, list);
846 if (s_info->flags.out_flags == AUDIO_OUTPUT_FLAG_PRIMARY) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700847 ALOGV("Compatible output profile not found.");
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530848 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
849 app_type_cfg->sample_rate = s_info->app_type_cfg.sample_rate;
850 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700851 ALOGV("%s Default to primary output: App type: %d sample_rate %d",
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530852 __func__, s_info->app_type_cfg.app_type, app_type_cfg->sample_rate);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700853 return;
854 }
855 }
856 ALOGW("%s: App type could not be selected. Falling back to default", __func__);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700857 app_type_cfg->app_type = platform_get_default_app_type(platform);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700858 app_type_cfg->sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700859 app_type_cfg->bit_width = 16;
Kogara Naveen Kumar51bb57d2022-02-17 16:40:06 +0530860 if (compare_device_type(devices, AUDIO_DEVICE_OUT_BUS) && (flags &
861 (audio_output_flags_t)AUDIO_OUTPUT_FLAG_FAST)) {
862 // Support low latency playback for different sample rates
863 app_type_cfg->sample_rate = sample_rate;
864 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700865}
866
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +0530867static bool audio_is_this_native_usecase(struct audio_usecase *uc)
868{
869 bool native_usecase = false;
870 struct stream_out *out = (struct stream_out*) uc->stream.out;
871
872 if (PCM_PLAYBACK == uc->type && out != NULL &&
873 NATIVE_AUDIO_MODE_INVALID != platform_get_native_support() &&
874 is_offload_usecase(uc->id) &&
875 (out->sample_rate == OUTPUT_SAMPLING_RATE_44100))
876 native_usecase = true;
877
878 return native_usecase;
879}
880
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800881bool audio_extn_is_dsp_bit_width_enforce_mode_supported(audio_output_flags_t flags)
882{
883 /* DSP bitwidth enforce mode for ADM and AFE:
884 * includes:
885 * deep buffer, low latency, direct pcm and offload.
886 * excludes:
887 * ull(raw+fast), VOIP.
888 */
889 if ((flags & AUDIO_OUTPUT_FLAG_VOIP_RX) ||
890 ((flags & AUDIO_OUTPUT_FLAG_RAW) &&
891 (flags & AUDIO_OUTPUT_FLAG_FAST)))
892 return false;
893
894
895 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) ||
896 (flags & AUDIO_OUTPUT_FLAG_DIRECT) ||
897 (flags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) ||
898 (flags & AUDIO_OUTPUT_FLAG_PRIMARY))
899 return true;
900 else
901 return false;
902}
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800903
904static inline bool audio_is_vr_mode_on(struct audio_device *(__attribute__((unused)) adev))
905{
906 return adev->vr_audio_mode_enabled;
907}
908
Derek Chenf7092792017-05-23 12:23:53 -0400909static void audio_extn_btsco_get_sample_rate(int snd_device, int *sample_rate)
910{
911 switch (snd_device) {
912 case SND_DEVICE_OUT_BT_SCO:
913 case SND_DEVICE_IN_BT_SCO_MIC:
914 case SND_DEVICE_IN_BT_SCO_MIC_NREC:
915 *sample_rate = 8000;
916 break;
917 case SND_DEVICE_OUT_BT_SCO_WB:
918 case SND_DEVICE_IN_BT_SCO_MIC_WB:
919 case SND_DEVICE_IN_BT_SCO_MIC_WB_NREC:
920 *sample_rate = 16000;
921 break;
922 default:
Aniket Kumar Latad13758f2020-08-06 15:11:36 -0700923 ALOGV("%s:Not a BT SCO device, need not update sampling rate\n", __func__);
Derek Chenf7092792017-05-23 12:23:53 -0400924 break;
925 }
926}
927
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530928void audio_extn_utils_update_stream_app_type_cfg_for_usecase(
929 struct audio_device *adev,
930 struct audio_usecase *usecase)
931{
932 ALOGV("%s", __func__);
933
934 switch(usecase->type) {
935 case PCM_PLAYBACK:
936 audio_extn_utils_update_stream_output_app_type_cfg(adev->platform,
937 &adev->streams_output_cfg_list,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -0800938 &usecase->stream.out->device_list,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530939 usecase->stream.out->flags,
Aalique Grahame65780b52017-09-27 14:59:56 -0700940 usecase->stream.out->hal_op_format,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530941 usecase->stream.out->sample_rate,
942 usecase->stream.out->bit_width,
943 usecase->stream.out->channel_mask,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530944 usecase->stream.out->profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530945 &usecase->stream.out->app_type_cfg);
946 ALOGV("%s Selected apptype: %d", __func__, usecase->stream.out->app_type_cfg.app_type);
947 break;
948 case PCM_CAPTURE:
Vikram Panduranga93f080e2017-06-07 18:16:14 -0700949 audio_extn_utils_update_stream_input_app_type_cfg(adev->platform,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530950 &adev->streams_input_cfg_list,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -0800951 &usecase->stream.in->device_list,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530952 usecase->stream.in->flags,
953 usecase->stream.in->format,
954 usecase->stream.in->sample_rate,
955 usecase->stream.in->bit_width,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530956 usecase->stream.in->profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530957 &usecase->stream.in->app_type_cfg);
958 ALOGV("%s Selected apptype: %d", __func__, usecase->stream.in->app_type_cfg.app_type);
959 break;
Surendar Karka93cd25a2018-08-28 14:21:37 +0530960 case TRANSCODE_LOOPBACK_RX :
Siddartha Shaik343abc62017-08-08 11:15:25 +0530961 audio_extn_utils_update_stream_output_app_type_cfg(adev->platform,
962 &adev->streams_output_cfg_list,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -0800963 &usecase->stream.inout->out_config.device_list,
Siddartha Shaik343abc62017-08-08 11:15:25 +0530964 0,
965 usecase->stream.inout->out_config.format,
966 usecase->stream.inout->out_config.sample_rate,
967 usecase->stream.inout->out_config.bit_width,
968 usecase->stream.inout->out_config.channel_mask,
969 usecase->stream.inout->profile,
970 &usecase->stream.inout->out_app_type_cfg);
971 ALOGV("%s Selected apptype: %d", __func__, usecase->stream.inout->out_app_type_cfg.app_type);
972 break;
Derek Chenf7092792017-05-23 12:23:53 -0400973 case PCM_HFP_CALL:
974 switch (usecase->id) {
975 case USECASE_AUDIO_HFP_SCO:
976 case USECASE_AUDIO_HFP_SCO_WB:
977 audio_extn_btsco_get_sample_rate(usecase->out_snd_device,
978 &usecase->out_app_type_cfg.sample_rate);
Derek Chend7307012019-11-12 12:10:41 -0500979 usecase->in_app_type_cfg.sample_rate = usecase->out_app_type_cfg.sample_rate;
Derek Chenf7092792017-05-23 12:23:53 -0400980 break;
981 case USECASE_AUDIO_HFP_SCO_DOWNLINK:
982 case USECASE_AUDIO_HFP_SCO_WB_DOWNLINK:
983 audio_extn_btsco_get_sample_rate(usecase->in_snd_device,
984 &usecase->in_app_type_cfg.sample_rate);
Derek Chend7307012019-11-12 12:10:41 -0500985 usecase->out_app_type_cfg.sample_rate = usecase->in_app_type_cfg.sample_rate;
Derek Chenf7092792017-05-23 12:23:53 -0400986 break;
987 default:
988 ALOGE("%s: usecase id (%d) not supported, use default sample rate",
989 __func__, usecase->id);
990 usecase->in_app_type_cfg.sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
991 usecase->out_app_type_cfg.sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
992 break;
993 }
994 /* update out_app_type_cfg */
995 usecase->out_app_type_cfg.bit_width =
996 platform_get_snd_device_bit_width(usecase->out_snd_device);
997 usecase->out_app_type_cfg.app_type =
998 platform_get_default_app_type_v2(adev->platform, PCM_PLAYBACK);
999 /* update in_app_type_cfg */
1000 usecase->in_app_type_cfg.bit_width =
1001 platform_get_snd_device_bit_width(usecase->in_snd_device);
1002 usecase->in_app_type_cfg.app_type =
1003 platform_get_default_app_type_v2(adev->platform, PCM_CAPTURE);
1004 ALOGV("%s Selected apptype: playback %d capture %d",
1005 __func__, usecase->out_app_type_cfg.app_type, usecase->in_app_type_cfg.app_type);
1006 break;
Derek Chena30a5f42019-12-03 11:17:09 -05001007 case ICC_CALL:
1008 /* ICC usecase: Loopback from TERT_TDM_TX to TERT_TDM_RX */
1009 /* update out_app_type_cfg */
1010 usecase->out_app_type_cfg.sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
1011 usecase->out_app_type_cfg.bit_width = platform_get_snd_device_bit_width(usecase->out_snd_device);
1012 usecase->out_app_type_cfg.app_type = platform_get_default_app_type_v2(adev->platform, PCM_PLAYBACK);
1013 /* update in_app_type_cfg */
1014 usecase->in_app_type_cfg.sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
1015 usecase->in_app_type_cfg.bit_width = platform_get_snd_device_bit_width(usecase->in_snd_device);
1016 usecase->in_app_type_cfg.app_type = platform_get_default_app_type_v2(adev->platform, PCM_CAPTURE);
1017
1018 ALOGV("%s Selected apptype: playback %d capture %d",
1019 __func__, usecase->out_app_type_cfg.app_type, usecase->in_app_type_cfg.app_type);
1020 break;
Fei Tongaffdf732020-02-20 20:39:05 +08001021 case SYNTH_LOOPBACK:
1022 /* update out_app_type_cfg */
1023 usecase->out_app_type_cfg.sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
1024 usecase->out_app_type_cfg.bit_width =
1025 platform_get_snd_device_bit_width(usecase->out_snd_device);
1026 usecase->out_app_type_cfg.app_type =
1027 platform_get_default_app_type_v2(adev->platform, PCM_PLAYBACK);
1028 /* update in_app_type_cfg */
1029 usecase->in_app_type_cfg.sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
1030 usecase->in_app_type_cfg.bit_width =
1031 platform_get_snd_device_bit_width(usecase->in_snd_device);
1032 usecase->in_app_type_cfg.app_type =
1033 platform_get_default_app_type_v2(adev->platform, PCM_CAPTURE);
1034 ALOGV("%s Selected apptype: playback %d capture %d",
1035 __func__, usecase->out_app_type_cfg.app_type, usecase->in_app_type_cfg.app_type);
1036 break;
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301037 default:
1038 ALOGE("%s: app type cfg not supported for usecase type (%d)",
1039 __func__, usecase->type);
1040 }
1041}
1042
Aalique Grahame22e49102018-12-18 14:23:57 -08001043static int set_stream_app_type_mixer_ctrl(struct audio_device *adev,
1044 int pcm_device_id, int app_type,
1045 int acdb_dev_id, int sample_rate,
1046 int stream_type,
1047 snd_device_t snd_device)
1048{
1049
1050 char mixer_ctl_name[MAX_LENGTH_MIXER_CONTROL_IN_INT];
1051 struct mixer_ctl *ctl;
1052 int app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT], len = 0, rc = 0;
1053 int snd_device_be_idx = -1;
1054
1055 if (stream_type == PCM_PLAYBACK) {
1056 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
1057 "Audio Stream %d App Type Cfg", pcm_device_id);
1058 } else if (stream_type == PCM_CAPTURE) {
1059 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
1060 "Audio Stream Capture %d App Type Cfg", pcm_device_id);
1061 }
1062
1063 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
1064 if (!ctl) {
1065 ALOGE("%s: Could not get ctl for mixer cmd - %s",
1066 __func__, mixer_ctl_name);
1067 rc = -EINVAL;
1068 goto exit;
1069 }
1070 app_type_cfg[len++] = app_type;
1071 app_type_cfg[len++] = acdb_dev_id;
1072 app_type_cfg[len++] = sample_rate;
1073
1074 snd_device_be_idx = platform_get_snd_device_backend_index(snd_device);
1075 if (snd_device_be_idx > 0)
1076 app_type_cfg[len++] = snd_device_be_idx;
1077 ALOGV("%s: stream type %d app_type %d, acdb_dev_id %d "
1078 "sample rate %d, snd_device_be_idx %d",
1079 __func__, stream_type, app_type, acdb_dev_id, sample_rate,
1080 snd_device_be_idx);
1081 mixer_ctl_set_array(ctl, app_type_cfg, len);
1082
1083exit:
1084 return rc;
1085}
1086
1087static int audio_extn_utils_send_app_type_cfg_hfp(struct audio_device *adev,
1088 struct audio_usecase *usecase)
1089{
1090 int pcm_device_id, acdb_dev_id = 0, snd_device = usecase->out_snd_device;
1091 int32_t sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
1092 int app_type = 0, rc = 0;
Derek Chenf7092792017-05-23 12:23:53 -04001093 bool is_bus_dev_usecase = false;
Aalique Grahame22e49102018-12-18 14:23:57 -08001094
1095 ALOGV("%s", __func__);
1096
1097 if (usecase->type != PCM_HFP_CALL) {
Derek Chenf7092792017-05-23 12:23:53 -04001098 ALOGV("%s: not a HFP path, no need to cfg app type", __func__);
Aalique Grahame22e49102018-12-18 14:23:57 -08001099 rc = 0;
1100 goto exit_send_app_type_cfg;
1101 }
1102 if ((usecase->id != USECASE_AUDIO_HFP_SCO) &&
Derek Chenf7092792017-05-23 12:23:53 -04001103 (usecase->id != USECASE_AUDIO_HFP_SCO_WB) &&
1104 (usecase->id != USECASE_AUDIO_HFP_SCO_DOWNLINK) &&
1105 (usecase->id != USECASE_AUDIO_HFP_SCO_WB_DOWNLINK)) {
1106 ALOGV("%s: a usecase where app type cfg is not required", __func__);
Aalique Grahame22e49102018-12-18 14:23:57 -08001107 rc = 0;
1108 goto exit_send_app_type_cfg;
1109 }
1110
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08001111 if (compare_device_type(&usecase->device_list, AUDIO_DEVICE_OUT_BUS))
Derek Chenf7092792017-05-23 12:23:53 -04001112 is_bus_dev_usecase = true;
1113
Aalique Grahame22e49102018-12-18 14:23:57 -08001114 snd_device = usecase->out_snd_device;
1115 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_PLAYBACK);
1116
1117 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
1118 if (acdb_dev_id < 0) {
1119 ALOGE("%s: Couldn't get the acdb dev id", __func__);
1120 rc = -EINVAL;
1121 goto exit_send_app_type_cfg;
1122 }
1123
sriram kumar53c4c972021-02-18 16:49:33 +05301124 /*
1125 * Value of afe_loopback gets read based on the property defined in
1126 * audio_platform_info.xml. If afe loopback is set then do not execute
1127 * session 1 path as app type mixer control will not be created for
1128 * afe loopback
1129 */
1130
Aalique Grahame22e49102018-12-18 14:23:57 -08001131 if (usecase->type == PCM_HFP_CALL) {
sriram kumar53c4c972021-02-18 16:49:33 +05301132 if (!(platform_get_is_afe_loopback_enabled(adev->platform))) {
1133 /* config HFP session:1 playback path */
1134 if (is_bus_dev_usecase) {
1135 app_type = usecase->out_app_type_cfg.app_type;
1136 sample_rate= usecase->out_app_type_cfg.sample_rate;
1137 } else {
1138 snd_device = SND_DEVICE_NONE; // use legacy behavior
1139 app_type = platform_get_default_app_type_v2(adev->platform, PCM_PLAYBACK);
1140 sample_rate= CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
Derek Chenf7092792017-05-23 12:23:53 -04001141 }
sriram kumar53c4c972021-02-18 16:49:33 +05301142 rc = set_stream_app_type_mixer_ctrl(adev, pcm_device_id, app_type,
1143 acdb_dev_id, sample_rate,
1144 PCM_PLAYBACK,
1145 snd_device);
1146 if (rc < 0)
1147 goto exit_send_app_type_cfg;
1148
1149 /* config HFP session:1 capture path */
1150 if (is_bus_dev_usecase) {
1151 snd_device = usecase->in_snd_device;
1152 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_CAPTURE);
1153 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
1154 if (acdb_dev_id < 0) {
1155 ALOGE("%s: Couldn't get the acdb dev id", __func__);
1156 rc = -EINVAL;
1157 goto exit_send_app_type_cfg;
1158 }
1159 app_type = usecase->in_app_type_cfg.app_type;
1160 sample_rate= usecase->in_app_type_cfg.sample_rate;
1161 } else {
1162 snd_device = SND_DEVICE_NONE; // use legacy behavior
1163 app_type = platform_get_default_app_type_v2(adev->platform, PCM_CAPTURE);
1164 }
1165 rc = set_stream_app_type_mixer_ctrl(adev, pcm_device_id, app_type,
1166 acdb_dev_id, sample_rate,
1167 PCM_CAPTURE,
1168 snd_device);
1169 if (rc < 0)
1170 goto exit_send_app_type_cfg;
1171
1172 if (is_bus_dev_usecase)
1173 goto exit_send_app_type_cfg;
Derek Chenf7092792017-05-23 12:23:53 -04001174 }
Aalique Grahame22e49102018-12-18 14:23:57 -08001175 /* config HFP session:2 capture path */
1176 pcm_device_id = HFP_ASM_RX_TX;
1177 snd_device = usecase->in_snd_device;
1178 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
1179 if (acdb_dev_id <= 0) {
1180 ALOGE("%s: Couldn't get the acdb dev id", __func__);
1181 rc = -EINVAL;
1182 goto exit_send_app_type_cfg;
1183 }
1184 app_type = platform_get_default_app_type_v2(adev->platform, PCM_CAPTURE);
1185 rc = set_stream_app_type_mixer_ctrl(adev, pcm_device_id, app_type,
1186 acdb_dev_id, sample_rate, PCM_CAPTURE,
sriram kumar53c4c972021-02-18 16:49:33 +05301187 usecase->in_snd_device);
Aalique Grahame22e49102018-12-18 14:23:57 -08001188 if (rc < 0)
1189 goto exit_send_app_type_cfg;
1190
1191 /* config HFP session:2 playback path */
1192 app_type = platform_get_default_app_type_v2(adev->platform, PCM_PLAYBACK);
1193 rc = set_stream_app_type_mixer_ctrl(adev, pcm_device_id, app_type,
1194 acdb_dev_id, sample_rate,
sriram kumar53c4c972021-02-18 16:49:33 +05301195 PCM_PLAYBACK, usecase->out_snd_device);
Aalique Grahame22e49102018-12-18 14:23:57 -08001196 if (rc < 0)
1197 goto exit_send_app_type_cfg;
1198 }
1199
1200 rc = 0;
1201exit_send_app_type_cfg:
1202 return rc;
1203}
1204
Derek Chena30a5f42019-12-03 11:17:09 -05001205int audio_extn_utils_send_app_type_cfg_icc(struct audio_device *adev,
1206 struct audio_usecase *usecase)
1207{
1208 int pcm_device_id, acdb_dev_id = 0, snd_device = usecase->out_snd_device;
1209 int32_t sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
1210 int app_type = 0, rc = 0;
1211
1212 ALOGV("%s", __func__);
1213
1214 if (usecase->type != ICC_CALL) {
1215 ALOGV("%s: not an ICC path, no need to cfg app type", __func__);
1216 rc = 0;
1217 goto exit_send_app_type_cfg;
1218 }
1219 if (usecase->id != USECASE_ICC_CALL) {
1220 ALOGV("%s: a usecase where app type cfg is not required", __func__);
1221 rc = 0;
1222 goto exit_send_app_type_cfg;
1223 }
1224
1225 snd_device = usecase->out_snd_device;
1226 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_PLAYBACK);
1227 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
1228 if (acdb_dev_id < 0) {
1229 ALOGE("%s: Couldn't get the acdb dev id", __func__);
1230 rc = -EINVAL;
1231 goto exit_send_app_type_cfg;
1232 }
1233 /* config ICC session: playback path */
1234 app_type = usecase->out_app_type_cfg.app_type;
1235 sample_rate= usecase->out_app_type_cfg.sample_rate;
1236
1237 rc = set_stream_app_type_mixer_ctrl(adev, pcm_device_id, app_type,
1238 acdb_dev_id, sample_rate,
1239 PCM_PLAYBACK,
1240 snd_device);
1241 if (rc < 0)
1242 goto exit_send_app_type_cfg;
1243
1244 /* config ICC session: capture path */
1245 snd_device = usecase->in_snd_device;
1246 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_CAPTURE);
1247 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
1248 if (acdb_dev_id < 0) {
1249 ALOGE("%s: Couldn't get the acdb dev id", __func__);
1250 rc = -EINVAL;
1251 goto exit_send_app_type_cfg;
1252 }
1253 app_type = usecase->in_app_type_cfg.app_type;
1254 sample_rate= usecase->in_app_type_cfg.sample_rate;
1255 rc = set_stream_app_type_mixer_ctrl(adev, pcm_device_id, app_type,
1256 acdb_dev_id, sample_rate,
1257 PCM_CAPTURE,
1258 snd_device);
1259exit_send_app_type_cfg:
1260 return rc;
1261}
1262
Fei Tongaffdf732020-02-20 20:39:05 +08001263static int audio_extn_utils_send_app_type_cfg_synth(struct audio_device *adev,
1264 struct audio_usecase *usecase)
1265{
1266 int pcm_device_id, acdb_dev_id = 0, snd_device = usecase->out_snd_device;
1267 int32_t sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
1268 int app_type = 0, rc = 0;
1269 bool is_bus_dev_usecase = false;
1270
1271 ALOGV("%s", __func__);
1272
1273 if (usecase->type != SYNTH_LOOPBACK) {
1274 ALOGV("%s: not a SYNTH path, no need to cfg app type", __func__);
1275 rc = 0;
1276 goto exit_send_app_type_cfg;
1277 }
1278 if (usecase->id != USECASE_AUDIO_PLAYBACK_SYNTHESIZER) {
1279 ALOGV("%s: a usecase where app type cfg is not required", __func__);
1280 rc = 0;
1281 goto exit_send_app_type_cfg;
1282 }
1283
1284 if (compare_device_type(&usecase->device_list, AUDIO_DEVICE_OUT_BUS)) {
1285 is_bus_dev_usecase = true;
1286 }
1287
1288 snd_device = usecase->out_snd_device;
1289 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_PLAYBACK);
1290
1291 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
1292 if (acdb_dev_id < 0) {
1293 ALOGE("%s: Couldn't get the acdb dev id", __func__);
1294 rc = -EINVAL;
1295 goto exit_send_app_type_cfg;
1296 }
1297
1298 if (usecase->type == SYNTH_LOOPBACK) {
1299 /* config SYNTH session: playback path */
1300 if (is_bus_dev_usecase) {
1301 app_type = usecase->out_app_type_cfg.app_type;
1302 sample_rate= usecase->out_app_type_cfg.sample_rate;
1303 } else {
1304 snd_device = SND_DEVICE_NONE; // use legacy behavior
1305 app_type = platform_get_default_app_type_v2(adev->platform, PCM_PLAYBACK);
1306 sample_rate= CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
1307 }
1308 rc = set_stream_app_type_mixer_ctrl(adev, pcm_device_id, app_type,
1309 acdb_dev_id, sample_rate,
1310 PCM_PLAYBACK,
1311 snd_device);
1312 if (rc < 0)
1313 goto exit_send_app_type_cfg;
1314 }
1315
1316 rc = 0;
1317exit_send_app_type_cfg:
1318 return rc;
1319}
1320
Zhou Song06761dd2019-04-28 18:08:17 +08001321int audio_extn_utils_get_app_sample_rate_for_device(
1322 struct audio_device *adev,
1323 struct audio_usecase *usecase, int snd_device)
1324{
1325 char value[PROPERTY_VALUE_MAX] = {0};
1326 int sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Krishna Kishor Jhac1542b22022-08-22 13:12:09 +05301327#ifdef PLATFORM_AUTO
Kogara Naveen Kumar51bb57d2022-02-17 16:40:06 +05301328 int acdb_dev_id;
Krishna Kishor Jhac1542b22022-08-22 13:12:09 +05301329#endif
Zhou Song06761dd2019-04-28 18:08:17 +08001330
1331 if ((usecase->type == PCM_PLAYBACK) && (usecase->stream.out != NULL)) {
1332 property_get("vendor.audio.playback.mch.downsample",value,"");
1333 if (!strncmp("true", value, sizeof("true"))) {
1334 if ((popcount(usecase->stream.out->channel_mask) > 2) &&
1335 (usecase->stream.out->app_type_cfg.sample_rate > CODEC_BACKEND_DEFAULT_SAMPLE_RATE) &&
1336 !(usecase->stream.out->flags &
1337 (audio_output_flags_t)AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH))
1338 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
1339 }
1340
1341 if (usecase->id == USECASE_AUDIO_PLAYBACK_VOIP) {
1342 usecase->stream.out->app_type_cfg.sample_rate = usecase->stream.out->sample_rate;
Zhou Song06761dd2019-04-28 18:08:17 +08001343 } else if ((snd_device == SND_DEVICE_OUT_HDMI ||
1344 snd_device == SND_DEVICE_OUT_USB_HEADSET ||
1345 snd_device == SND_DEVICE_OUT_DISPLAY_PORT) &&
1346 (usecase->stream.out->sample_rate >= OUTPUT_SAMPLING_RATE_44100)) {
1347 /*
1348 * To best utlize DSP, check if the stream sample rate is supported/multiple of
1349 * configured device sample rate, if not update the COPP rate to be equal to the
1350 * device sample rate, else open COPP at stream sample rate
1351 */
1352 platform_check_and_update_copp_sample_rate(adev->platform, snd_device,
1353 usecase->stream.out->sample_rate,
1354 &usecase->stream.out->app_type_cfg.sample_rate);
Lakshman Chaluvaraju5d0894e2021-01-27 23:22:58 +05301355 } else if (snd_device == SND_DEVICE_OUT_BT_A2DP) {
Zhou Song06761dd2019-04-28 18:08:17 +08001356 /*
1357 * For a2dp playback get encoder sampling rate and set copp sampling rate,
1358 * for bit width use the stream param only.
1359 */
1360 audio_extn_a2dp_get_enc_sample_rate(&usecase->stream.out->app_type_cfg.sample_rate);
1361 ALOGI("%s using %d sample rate rate for A2DP CoPP",
1362 __func__, usecase->stream.out->app_type_cfg.sample_rate);
sriram kumar68e21132021-06-25 19:27:20 +05301363 } else if ((((snd_device != SND_DEVICE_OUT_HEADPHONES_44_1 &&
1364 !audio_is_this_native_usecase(usecase)) &&
1365 usecase->stream.out->sample_rate == OUTPUT_SAMPLING_RATE_44100) ||
1366 (usecase->stream.out->sample_rate < OUTPUT_SAMPLING_RATE_44100)) ||
1367 (compare_device_type(&usecase->stream.out->device_list,AUDIO_DEVICE_OUT_SPEAKER))) {
Kogara Naveen Kumar51bb57d2022-02-17 16:40:06 +05301368 if (!((compare_device_type(&usecase->device_list, AUDIO_DEVICE_OUT_BUS)) && ((usecase->stream.out->flags &
1369 (audio_output_flags_t)AUDIO_OUTPUT_FLAG_SYS_NOTIFICATION) || (usecase->stream.out->flags &
1370 (audio_output_flags_t)AUDIO_OUTPUT_FLAG_PHONE)))) {
1371 /* Reset to default if no native stream is active or default device is speaker*/
1372 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
1373 }
Zhou Song06761dd2019-04-28 18:08:17 +08001374 }
1375 audio_extn_btsco_get_sample_rate(snd_device, &usecase->stream.out->app_type_cfg.sample_rate);
1376 sample_rate = usecase->stream.out->app_type_cfg.sample_rate;
1377
1378 if (((usecase->stream.out->format == AUDIO_FORMAT_E_AC3) ||
1379 (usecase->stream.out->format == AUDIO_FORMAT_E_AC3_JOC) ||
1380 (usecase->stream.out->format == AUDIO_FORMAT_DOLBY_TRUEHD))
1381 && audio_extn_passthru_is_passthrough_stream(usecase->stream.out)
1382 && !audio_extn_passthru_is_convert_supported(adev, usecase->stream.out)) {
1383 sample_rate = sample_rate * 4;
1384 if (sample_rate > HDMI_PASSTHROUGH_MAX_SAMPLE_RATE)
1385 sample_rate = HDMI_PASSTHROUGH_MAX_SAMPLE_RATE;
1386 }
1387 } else if (usecase->type == PCM_CAPTURE) {
1388 if (usecase->stream.in != NULL) {
Revathi Uddarajud9f23d92020-07-27 10:55:06 +05301389 if (usecase->id == USECASE_AUDIO_RECORD_VOIP
1390 || usecase->id == USECASE_AUDIO_RECORD_VOIP_LOW_LATENCY)
Zhou Song06761dd2019-04-28 18:08:17 +08001391 usecase->stream.in->app_type_cfg.sample_rate = usecase->stream.in->sample_rate;
1392 if (voice_is_in_call_rec_stream(usecase->stream.in)) {
1393 audio_extn_btsco_get_sample_rate(usecase->in_snd_device,
1394 &usecase->stream.in->app_type_cfg.sample_rate);
Zhou Songab94f062021-05-12 13:11:17 +08001395 } if (SND_DEVICE_IN_BT_A2DP == snd_device) {
1396 audio_extn_a2dp_get_dec_sample_rate(&usecase->stream.in->app_type_cfg.sample_rate);
Zhou Song06761dd2019-04-28 18:08:17 +08001397 } else {
1398 audio_extn_btsco_get_sample_rate(snd_device,
1399 &usecase->stream.in->app_type_cfg.sample_rate);
1400 }
1401 sample_rate = usecase->stream.in->app_type_cfg.sample_rate;
1402 } else if (usecase->id == USECASE_AUDIO_SPKR_CALIB_TX) {
Sujin Panickerb6d76262019-09-09 10:55:38 +05301403 if ((property_get("vendor.audio.spkr_prot.tx.sampling_rate", value, NULL) > 0))
1404 sample_rate = atoi(value);
1405 else
1406 sample_rate = SAMPLE_RATE_8000;
Zhou Song06761dd2019-04-28 18:08:17 +08001407 }
Kogara Naveen Kumar51bb57d2022-02-17 16:40:06 +05301408
1409 /* ECNR module in DSP does not support 24 KHz sample rate. As a workaround,
1410 run ADM at 48 KHz when ECNR is enabled in ACDB topology (e.g. device id = 95)
1411 */
Krishna Kishor Jhac1542b22022-08-22 13:12:09 +05301412#ifdef PLATFORM_AUTO
Kogara Naveen Kumar51bb57d2022-02-17 16:40:06 +05301413 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
1414 if (sample_rate == ECNS_UNSUPPORTED_CAPTURE_SAMPLE_RATE_FOR_ADM && acdb_dev_id == ECNS_USE_CASE_ACDB_DEV_ID) {
1415 sample_rate = ECNS_SUPPORTED_CAPTURE_SAMPLE_RATE_FOR_ADM;
1416 ALOGD("%s: update sample rate from 24K to 48K to support ECNR in PCM_CAPTURE, sample_rate=%d",__func__,sample_rate);
1417 }
Krishna Kishor Jhac1542b22022-08-22 13:12:09 +05301418#endif
Zhou Song06761dd2019-04-28 18:08:17 +08001419 } else if (usecase->type == TRANSCODE_LOOPBACK_RX) {
1420 sample_rate = usecase->stream.inout->out_config.sample_rate;
1421 }
1422 return sample_rate;
1423}
1424
Siena Richard7c2db772016-12-21 11:32:34 -08001425static int send_app_type_cfg_for_device(struct audio_device *adev,
1426 struct audio_usecase *usecase,
1427 int split_snd_device)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001428{
1429 char mixer_ctl_name[MAX_LENGTH_MIXER_CONTROL_IN_INT];
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301430 size_t app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT] = {0};
1431 int len = 0, rc;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001432 struct mixer_ctl *ctl;
Siena Richard7c2db772016-12-21 11:32:34 -08001433 int pcm_device_id = 0, acdb_dev_id, app_type;
1434 int snd_device = split_snd_device, snd_device_be_idx = -1;
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +05301435 int32_t sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Varun Balaraje49253e2017-07-06 19:48:56 +05301436 struct streams_io_cfg *s_info = NULL;
1437 struct listnode *node = NULL;
Nikhil Laturkarac4a7492017-08-31 18:27:19 +05301438 int bd_app_type = 0;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001439
Siena Richard7c2db772016-12-21 11:32:34 -08001440 ALOGV("%s: usecase->out_snd_device %s, usecase->in_snd_device %s, split_snd_device %s",
1441 __func__, platform_get_snd_device_name(usecase->out_snd_device),
1442 platform_get_snd_device_name(usecase->in_snd_device),
1443 platform_get_snd_device_name(split_snd_device));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001444
Siddartha Shaik343abc62017-08-08 11:15:25 +05301445 if (usecase->type != PCM_PLAYBACK && usecase->type != PCM_CAPTURE &&
Surendar Karka93cd25a2018-08-28 14:21:37 +05301446 usecase->type != TRANSCODE_LOOPBACK_RX) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301447 ALOGE("%s: not a playback/capture path, no need to cfg app type", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001448 rc = 0;
1449 goto exit_send_app_type_cfg;
1450 }
1451 if ((usecase->id != USECASE_AUDIO_PLAYBACK_DEEP_BUFFER) &&
Vignesh Kulothungan0a2eff02019-07-11 16:30:13 -07001452 (usecase->id != USECASE_AUDIO_PLAYBACK_WITH_HAPTICS) &&
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001453 (usecase->id != USECASE_AUDIO_PLAYBACK_LOW_LATENCY) &&
1454 (usecase->id != USECASE_AUDIO_PLAYBACK_MULTI_CH) &&
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -08001455 (usecase->id != USECASE_AUDIO_PLAYBACK_ULL) &&
Vikram Panduranga93f080e2017-06-07 18:16:14 -07001456 (usecase->id != USECASE_AUDIO_PLAYBACK_VOIP) &&
Surendar Karka93cd25a2018-08-28 14:21:37 +05301457 (usecase->id != USECASE_AUDIO_TRANSCODE_LOOPBACK_RX) &&
Varun Balaraje49253e2017-07-06 19:48:56 +05301458 (!is_interactive_usecase(usecase->id)) &&
Srikanth Uyyala9d551402015-08-25 16:03:42 +05301459 (!is_offload_usecase(usecase->id)) &&
Derek Chenf6318be2017-06-12 17:16:24 -04001460 (usecase->type != PCM_CAPTURE) &&
1461 (!audio_extn_auto_hal_is_bus_device_usecase(usecase->id))) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301462 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 -07001463 rc = 0;
1464 goto exit_send_app_type_cfg;
1465 }
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -08001466
1467 //if VR is active then only send the mixer control
1468 if (usecase->id == USECASE_AUDIO_PLAYBACK_ULL && !audio_is_vr_mode_on(adev)) {
1469 ALOGI("ULL doesnt need sending app type cfg, returning");
1470 rc = 0;
1471 goto exit_send_app_type_cfg;
1472 }
1473
Surendar Karka93cd25a2018-08-28 14:21:37 +05301474 if (usecase->type == PCM_PLAYBACK || usecase->type == TRANSCODE_LOOPBACK_RX) {
Ben Romberger1fafdde2015-09-09 19:43:15 -07001475 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_PLAYBACK);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301476 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
1477 "Audio Stream %d App Type Cfg", pcm_device_id);
Ben Romberger1fafdde2015-09-09 19:43:15 -07001478 } else if (usecase->type == PCM_CAPTURE) {
Ben Romberger1fafdde2015-09-09 19:43:15 -07001479 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_CAPTURE);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301480 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
1481 "Audio Stream Capture %d App Type Cfg", pcm_device_id);
Srikanth Uyyala9d551402015-08-25 16:03:42 +05301482 }
Siena Richard7c2db772016-12-21 11:32:34 -08001483
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001484 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
1485 if (!ctl) {
1486 ALOGE("%s: Could not get ctl for mixer cmd - %s", __func__,
1487 mixer_ctl_name);
1488 rc = -EINVAL;
1489 goto exit_send_app_type_cfg;
1490 }
Aditya Bavanari701a6992017-03-30 19:17:16 +05301491 snd_device = platform_get_spkr_prot_snd_device(snd_device);
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301492 if (voice_is_in_call_rec_stream(usecase->stream.in) && usecase->type == PCM_CAPTURE) {
1493 snd_device_t voice_device = voice_get_incall_rec_snd_device(usecase->in_snd_device);
1494 acdb_dev_id = platform_get_snd_device_acdb_id(voice_device);
1495 ALOGV("acdb id for voice call use case %d", acdb_dev_id);
1496 } else {
1497 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
1498 }
Rohit Kumar1181b292017-01-31 18:07:17 +05301499 if (acdb_dev_id <= 0) {
1500 ALOGE("%s: Couldn't get the acdb dev id", __func__);
1501 rc = -EINVAL;
1502 goto exit_send_app_type_cfg;
1503 }
1504
Siena Richard7c2db772016-12-21 11:32:34 -08001505 snd_device_be_idx = platform_get_snd_device_backend_index(snd_device);
1506 if (snd_device_be_idx < 0) {
1507 ALOGE("%s: Couldn't get the backend index for snd device %s ret=%d",
1508 __func__, platform_get_snd_device_name(snd_device),
1509 snd_device_be_idx);
1510 }
1511
Zhou Song06761dd2019-04-28 18:08:17 +08001512 sample_rate = audio_extn_utils_get_app_sample_rate_for_device(adev, usecase, snd_device);
1513
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301514 if ((usecase->type == PCM_PLAYBACK) && (usecase->stream.out != NULL)) {
Varun Balaraje49253e2017-07-06 19:48:56 +05301515 /* Interactive streams are supported with only direct app type id.
1516 * Get Direct profile app type and use it for interactive streams
1517 */
1518 list_for_each(node, &adev->streams_output_cfg_list) {
1519 s_info = node_to_item(node, struct streams_io_cfg, list);
Aalique Grahame5cd12ff2017-10-19 10:57:00 -07001520 if (s_info->flags.out_flags == (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_BD |
Nikhil Laturkarac4a7492017-08-31 18:27:19 +05301521 AUDIO_OUTPUT_FLAG_DIRECT_PCM |
1522 AUDIO_OUTPUT_FLAG_DIRECT))
1523 bd_app_type = s_info->app_type_cfg.app_type;
Varun Balaraje49253e2017-07-06 19:48:56 +05301524 }
Aalique Grahame5cd12ff2017-10-19 10:57:00 -07001525 if (usecase->stream.out->flags == (audio_output_flags_t)AUDIO_OUTPUT_FLAG_INTERACTIVE)
Nikhil Laturkarac4a7492017-08-31 18:27:19 +05301526 app_type = bd_app_type;
Varun Balaraje49253e2017-07-06 19:48:56 +05301527 else
1528 app_type = usecase->stream.out->app_type_cfg.app_type;
Siena Richard7c2db772016-12-21 11:32:34 -08001529 app_type_cfg[len++] = app_type;
Mingming Yin21854652016-04-13 11:54:02 -07001530 app_type_cfg[len++] = acdb_dev_id;
Naresh Tanniru3a406772017-05-10 13:09:05 -07001531 app_type_cfg[len++] = sample_rate;
1532
Siena Richard7c2db772016-12-21 11:32:34 -08001533 if (snd_device_be_idx > 0)
1534 app_type_cfg[len++] = snd_device_be_idx;
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301535
Siena Richard7c2db772016-12-21 11:32:34 -08001536 ALOGI("%s PLAYBACK app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
1537 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301538
1539 } else if ((usecase->type == PCM_CAPTURE) && (usecase->stream.in != NULL)) {
Siena Richard7c2db772016-12-21 11:32:34 -08001540 app_type = usecase->stream.in->app_type_cfg.app_type;
1541 app_type_cfg[len++] = app_type;
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301542 app_type_cfg[len++] = acdb_dev_id;
Siena Richard7c2db772016-12-21 11:32:34 -08001543 app_type_cfg[len++] = sample_rate;
1544 if (snd_device_be_idx > 0)
1545 app_type_cfg[len++] = snd_device_be_idx;
1546 ALOGI("%s CAPTURE app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
1547 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301548 } else {
Siena Richard7c2db772016-12-21 11:32:34 -08001549 app_type = platform_get_default_app_type_v2(adev->platform, usecase->type);
Surendar Karka93cd25a2018-08-28 14:21:37 +05301550 if(usecase->type == TRANSCODE_LOOPBACK_RX) {
Siddartha Shaik343abc62017-08-08 11:15:25 +05301551 app_type = usecase->stream.inout->out_app_type_cfg.app_type;
1552 }
Siena Richard7c2db772016-12-21 11:32:34 -08001553 app_type_cfg[len++] = app_type;
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301554 app_type_cfg[len++] = acdb_dev_id;
1555 app_type_cfg[len++] = sample_rate;
Siena Richard7c2db772016-12-21 11:32:34 -08001556 if (snd_device_be_idx > 0)
1557 app_type_cfg[len++] = snd_device_be_idx;
1558 ALOGI("%s default app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
1559 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Ashish Jainc02430d2016-01-04 10:42:43 +05301560 }
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301561
Siddartha Shaik343abc62017-08-08 11:15:25 +05301562 if(ctl)
1563 mixer_ctl_set_array(ctl, app_type_cfg, len);
Zhou Songd9bd9302020-08-04 16:34:45 +08001564
1565 /* send app type cfg for haptics */
1566 if (usecase->id == USECASE_AUDIO_PLAYBACK_WITH_HAPTICS) {
1567 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
1568 "Audio Stream %d App Type Cfg", adev->haptic_pcm_device_id );
1569 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
1570 if (!ctl) {
1571 ALOGE("%s: Could not get ctl for mixer cmd - %s", __func__,
1572 mixer_ctl_name);
1573 rc = -EINVAL;
1574 goto exit_send_app_type_cfg;
1575 }
1576 acdb_dev_id = platform_get_snd_device_acdb_id(SND_DEVICE_OUT_HAPTICS);
1577 snd_device_be_idx = platform_get_snd_device_backend_index(SND_DEVICE_OUT_HAPTICS);
1578 /* haptics acdb id */
1579 app_type_cfg[1] = acdb_dev_id;
1580 /* haptics be index */
1581 app_type_cfg[3] = snd_device_be_idx;
1582 mixer_ctl_set_array(ctl, app_type_cfg, len);
1583 }
1584
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001585 rc = 0;
1586exit_send_app_type_cfg:
1587 return rc;
1588}
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001589
Deeraj Soman14230922019-01-30 16:39:30 +05301590static int audio_extn_utils_check_input_parameters(uint32_t sample_rate,
1591 audio_format_t format,
1592 int channel_count)
1593{
1594 int ret = 0;
1595
1596 if (((format != AUDIO_FORMAT_PCM_16_BIT) && (format != AUDIO_FORMAT_PCM_8_24_BIT) &&
1597 (format != AUDIO_FORMAT_PCM_24_BIT_PACKED) && (format != AUDIO_FORMAT_PCM_32_BIT) &&
1598 (format != AUDIO_FORMAT_PCM_FLOAT)) &&
1599 !voice_extn_compress_voip_is_format_supported(format) &&
1600 !audio_extn_compr_cap_format_supported(format) &&
1601 !audio_extn_cin_format_supported(format))
1602 ret = -EINVAL;
1603
1604 switch (channel_count) {
1605 case 1:
1606 case 2:
1607 case 3:
1608 case 4:
1609 case 6:
1610 case 8:
1611 break;
1612 default:
1613 ret = -EINVAL;
1614 }
1615
1616 switch (sample_rate) {
1617 case 8000:
1618 case 11025:
1619 case 12000:
1620 case 16000:
1621 case 22050:
1622 case 24000:
1623 case 32000:
1624 case 44100:
1625 case 48000:
1626 case 88200:
1627 case 96000:
1628 case 176400:
1629 case 192000:
1630 break;
1631 default:
1632 ret = -EINVAL;
1633 }
1634
1635 return ret;
1636}
1637
1638static inline uint32_t audio_extn_utils_nearest_multiple(uint32_t num, uint32_t multiplier)
1639{
1640 uint32_t remainder = 0;
1641
1642 if (!multiplier)
1643 return num;
1644
1645 remainder = num % multiplier;
1646 if (remainder)
1647 num += (multiplier - remainder);
1648
1649 return num;
1650}
1651
1652static inline uint32_t audio_extn_utils_lcm(uint32_t num1, uint32_t num2)
1653{
1654 uint32_t high = num1, low = num2, temp = 0;
1655
1656 if (!num1 || !num2)
1657 return 0;
1658
1659 if (num1 < num2) {
1660 high = num2;
1661 low = num1;
1662 }
1663
1664 while (low != 0) {
1665 temp = low;
1666 low = high % low;
1667 high = temp;
1668 }
1669 return (num1 * num2)/high;
1670}
1671
Siena Richard7c2db772016-12-21 11:32:34 -08001672int audio_extn_utils_send_app_type_cfg(struct audio_device *adev,
1673 struct audio_usecase *usecase)
1674{
1675 int i, num_devices = 0;
1676 snd_device_t new_snd_devices[SND_DEVICE_OUT_END] = {0};
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301677 snd_device_t in_snd_device = usecase->in_snd_device;
Siena Richard7c2db772016-12-21 11:32:34 -08001678 int rc = 0;
1679
Aalique Grahame22e49102018-12-18 14:23:57 -08001680 if (usecase->type == PCM_HFP_CALL) {
1681 return audio_extn_utils_send_app_type_cfg_hfp(adev, usecase);
Derek Chena30a5f42019-12-03 11:17:09 -05001682 } else if (usecase->type == ICC_CALL) {
1683 return audio_extn_utils_send_app_type_cfg_icc(adev, usecase);
Fei Tongaffdf732020-02-20 20:39:05 +08001684 } else if (usecase->type == SYNTH_LOOPBACK) {
1685 return audio_extn_utils_send_app_type_cfg_synth(adev, usecase);
Aalique Grahame22e49102018-12-18 14:23:57 -08001686 }
1687
Siena Richard7c2db772016-12-21 11:32:34 -08001688 switch (usecase->type) {
1689 case PCM_PLAYBACK:
Surendar Karka93cd25a2018-08-28 14:21:37 +05301690 case TRANSCODE_LOOPBACK_RX:
Siena Richard7c2db772016-12-21 11:32:34 -08001691 ALOGD("%s: usecase->out_snd_device %s",
1692 __func__, platform_get_snd_device_name(usecase->out_snd_device));
1693 /* check for out combo device */
1694 if (platform_split_snd_device(adev->platform,
1695 usecase->out_snd_device,
1696 &num_devices, new_snd_devices)) {
1697 new_snd_devices[0] = usecase->out_snd_device;
1698 num_devices = 1;
1699 }
1700 break;
1701 case PCM_CAPTURE:
1702 ALOGD("%s: usecase->in_snd_device %s",
1703 __func__, platform_get_snd_device_name(usecase->in_snd_device));
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301704 if (voice_is_in_call_rec_stream(usecase->stream.in)) {
1705 in_snd_device = voice_get_incall_rec_backend_device(usecase->stream.in);
1706 }
Siena Richard7c2db772016-12-21 11:32:34 -08001707 /* check for in combo device */
1708 if (platform_split_snd_device(adev->platform,
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301709 in_snd_device,
Siena Richard7c2db772016-12-21 11:32:34 -08001710 &num_devices, new_snd_devices)) {
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301711 new_snd_devices[0] = in_snd_device;
Siena Richard7c2db772016-12-21 11:32:34 -08001712 num_devices = 1;
1713 }
1714 break;
Sriram Kumar9c836482020-10-27 20:34:35 +05301715 case PCM_HFP_CALL:
1716 rc = audio_extn_utils_send_app_type_cfg_hfp(adev,usecase);
1717 return rc;
Siena Richard7c2db772016-12-21 11:32:34 -08001718 default:
1719 ALOGI("%s: not a playback/capture path, no need to cfg app type", __func__);
1720 rc = 0;
1721 break;
1722 }
1723
1724 for (i = 0; i < num_devices; i++) {
1725 rc = send_app_type_cfg_for_device(adev, usecase, new_snd_devices[i]);
1726 if (rc)
1727 break;
1728 }
1729
1730 return rc;
1731}
1732
Preetam Singh Ranawat9519e9c2015-11-18 16:05:55 +05301733int read_line_from_file(const char *path, char *buf, size_t count)
1734{
1735 char * fgets_ret;
1736 FILE * fd;
1737 int rv;
1738
1739 fd = fopen(path, "r");
1740 if (fd == NULL)
1741 return -1;
1742
1743 fgets_ret = fgets(buf, (int)count, fd);
1744 if (NULL != fgets_ret) {
1745 rv = (int)strlen(buf);
1746 } else {
1747 rv = ferror(fd);
1748 }
1749 fclose(fd);
1750
1751 return rv;
1752}
1753
Ashish Jainf1eaa582016-05-23 20:54:24 +05301754/*Translates ALSA formats to AOSP PCM formats*/
1755audio_format_t alsa_format_to_hal(uint32_t alsa_format)
1756{
1757 audio_format_t format;
1758
1759 switch(alsa_format) {
1760 case SNDRV_PCM_FORMAT_S16_LE:
1761 format = AUDIO_FORMAT_PCM_16_BIT;
1762 break;
1763 case SNDRV_PCM_FORMAT_S24_3LE:
1764 format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
1765 break;
1766 case SNDRV_PCM_FORMAT_S24_LE:
1767 format = AUDIO_FORMAT_PCM_8_24_BIT;
1768 break;
1769 case SNDRV_PCM_FORMAT_S32_LE:
1770 format = AUDIO_FORMAT_PCM_32_BIT;
1771 break;
1772 default:
1773 ALOGW("Incorrect ALSA format");
1774 format = AUDIO_FORMAT_INVALID;
1775 }
1776 return format;
1777}
1778
1779/*Translates hal format (AOSP) to alsa formats*/
1780uint32_t hal_format_to_alsa(audio_format_t hal_format)
1781{
1782 uint32_t alsa_format;
1783
1784 switch (hal_format) {
1785 case AUDIO_FORMAT_PCM_32_BIT: {
1786 if (platform_supports_true_32bit())
1787 alsa_format = SNDRV_PCM_FORMAT_S32_LE;
1788 else
1789 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1790 }
1791 break;
1792 case AUDIO_FORMAT_PCM_8_BIT:
1793 alsa_format = SNDRV_PCM_FORMAT_S8;
1794 break;
1795 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
1796 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1797 break;
1798 case AUDIO_FORMAT_PCM_8_24_BIT: {
1799 if (platform_supports_true_32bit())
1800 alsa_format = SNDRV_PCM_FORMAT_S32_LE;
1801 else
1802 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1803 }
1804 break;
1805 case AUDIO_FORMAT_PCM_FLOAT:
1806 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1807 break;
1808 default:
1809 case AUDIO_FORMAT_PCM_16_BIT:
1810 alsa_format = SNDRV_PCM_FORMAT_S16_LE;
1811 break;
1812 }
1813 return alsa_format;
1814}
1815
Ashish Jain83a6cc22016-06-28 14:34:17 +05301816/*Translates PCM formats to AOSP formats*/
1817audio_format_t pcm_format_to_hal(uint32_t pcm_format)
1818{
1819 audio_format_t format = AUDIO_FORMAT_INVALID;
1820
1821 switch(pcm_format) {
1822 case PCM_FORMAT_S16_LE:
1823 format = AUDIO_FORMAT_PCM_16_BIT;
1824 break;
1825 case PCM_FORMAT_S24_3LE:
1826 format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
1827 break;
1828 case PCM_FORMAT_S24_LE:
1829 format = AUDIO_FORMAT_PCM_8_24_BIT;
1830 break;
1831 case PCM_FORMAT_S32_LE:
1832 format = AUDIO_FORMAT_PCM_32_BIT;
1833 break;
1834 default:
1835 ALOGW("Incorrect PCM format");
1836 format = AUDIO_FORMAT_INVALID;
1837 }
1838 return format;
1839}
1840
1841/*Translates hal format (AOSP) to alsa formats*/
1842uint32_t hal_format_to_pcm(audio_format_t hal_format)
1843{
1844 uint32_t pcm_format;
1845
1846 switch (hal_format) {
1847 case AUDIO_FORMAT_PCM_32_BIT:
1848 case AUDIO_FORMAT_PCM_8_24_BIT:
1849 case AUDIO_FORMAT_PCM_FLOAT: {
1850 if (platform_supports_true_32bit())
1851 pcm_format = PCM_FORMAT_S32_LE;
1852 else
1853 pcm_format = PCM_FORMAT_S24_3LE;
1854 }
1855 break;
1856 case AUDIO_FORMAT_PCM_8_BIT:
1857 pcm_format = PCM_FORMAT_S8;
1858 break;
1859 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
1860 pcm_format = PCM_FORMAT_S24_3LE;
1861 break;
1862 default:
1863 case AUDIO_FORMAT_PCM_16_BIT:
1864 pcm_format = PCM_FORMAT_S16_LE;
1865 break;
1866 }
1867 return pcm_format;
1868}
1869
Ashish Jainf1eaa582016-05-23 20:54:24 +05301870uint32_t get_alsa_fragment_size(uint32_t bytes_per_sample,
1871 uint32_t sample_rate,
Deeraj Soman65358ab2019-02-07 15:40:49 +05301872 uint32_t noOfChannels,
1873 int64_t duration_ms)
Ashish Jainf1eaa582016-05-23 20:54:24 +05301874{
1875 uint32_t fragment_size = 0;
1876 uint32_t pcm_offload_time = PCM_OFFLOAD_BUFFER_DURATION;
1877
Deeraj Soman65358ab2019-02-07 15:40:49 +05301878 if (duration_ms >= MIN_OFFLOAD_BUFFER_DURATION_MS && duration_ms <= MAX_OFFLOAD_BUFFER_DURATION_MS)
1879 pcm_offload_time = duration_ms;
1880
Ashish Jainf1eaa582016-05-23 20:54:24 +05301881 fragment_size = (pcm_offload_time
1882 * sample_rate
1883 * bytes_per_sample
1884 * noOfChannels)/1000;
1885 if (fragment_size < MIN_PCM_OFFLOAD_FRAGMENT_SIZE)
1886 fragment_size = MIN_PCM_OFFLOAD_FRAGMENT_SIZE;
1887 else if (fragment_size > MAX_PCM_OFFLOAD_FRAGMENT_SIZE)
1888 fragment_size = MAX_PCM_OFFLOAD_FRAGMENT_SIZE;
1889 /*To have same PCM samples for all channels, the buffer size requires to
1890 *be multiple of (number of channels * bytes per sample)
1891 *For writes to succeed, the buffer must be written at address which is multiple of 32
1892 */
Aalique Grahameb85f2d92017-10-16 17:53:23 -07001893 fragment_size = ALIGN(fragment_size, (bytes_per_sample * noOfChannels * 32));
Ashish Jainf1eaa582016-05-23 20:54:24 +05301894
1895 ALOGI("PCM offload Fragment size to %d bytes", fragment_size);
1896 return fragment_size;
1897}
1898
1899/* Calculates the fragment size required to configure compress session.
1900 * Based on the alsa format selected, decide if conversion is needed in
1901
1902 * HAL ( e.g. convert AUDIO_FORMAT_PCM_FLOAT input format to
1903 * AUDIO_FORMAT_PCM_24_BIT_PACKED before writing to the compress driver.
1904 */
1905void audio_extn_utils_update_direct_pcm_fragment_size(struct stream_out *out)
1906{
Ashish Jain83a6cc22016-06-28 14:34:17 +05301907 audio_format_t dst_format = out->hal_op_format;
1908 audio_format_t src_format = out->hal_ip_format;
Ashish Jainf1eaa582016-05-23 20:54:24 +05301909 uint32_t hal_op_bytes_per_sample = audio_bytes_per_sample(dst_format);
1910 uint32_t hal_ip_bytes_per_sample = audio_bytes_per_sample(src_format);
1911
1912 out->compr_config.fragment_size =
1913 get_alsa_fragment_size(hal_op_bytes_per_sample,
1914 out->sample_rate,
Deeraj Soman65358ab2019-02-07 15:40:49 +05301915 popcount(out->channel_mask),
1916 out->info.duration_us / 1000);
Ashish Jainf1eaa582016-05-23 20:54:24 +05301917
1918 if ((src_format != dst_format) &&
1919 hal_op_bytes_per_sample != hal_ip_bytes_per_sample) {
1920
Ashish Jain83a6cc22016-06-28 14:34:17 +05301921 out->hal_fragment_size =
Ashish Jainf1eaa582016-05-23 20:54:24 +05301922 ((out->compr_config.fragment_size * hal_ip_bytes_per_sample) /
1923 hal_op_bytes_per_sample);
1924 ALOGI("enable conversion hal_input_fragment_size is %d src_format %x dst_format %x",
Ashish Jain83a6cc22016-06-28 14:34:17 +05301925 out->hal_fragment_size, src_format, dst_format);
Ashish Jainf1eaa582016-05-23 20:54:24 +05301926 } else {
Ashish Jain83a6cc22016-06-28 14:34:17 +05301927 out->hal_fragment_size = out->compr_config.fragment_size;
Ashish Jainf1eaa582016-05-23 20:54:24 +05301928 }
1929}
1930
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301931/* converts pcm format 24_8 to 8_24 inplace */
1932size_t audio_extn_utils_convert_format_24_8_to_8_24(void *buf, size_t bytes)
1933{
1934 size_t i = 0;
1935 int *int_buf_stream = buf;
1936
1937 if ((bytes % 4) != 0) {
1938 ALOGE("%s: wrong inout buffer! ... is not 32 bit aligned ", __func__);
1939 return -EINVAL;
1940 }
1941
1942 for (; i < (bytes / 4); i++)
1943 int_buf_stream[i] >>= 8;
1944
1945 return bytes;
1946}
1947
Xiaojun Sang782e5b12020-06-29 21:13:06 +08001948#ifdef AUDIO_GKI_ENABLED
1949int get_snd_codec_id(audio_format_t format)
1950{
1951 int id = 0;
1952
1953 switch (format & AUDIO_FORMAT_MAIN_MASK) {
1954 case AUDIO_FORMAT_MP3:
1955 id = SND_AUDIOCODEC_MP3;
1956 break;
1957 case AUDIO_FORMAT_AAC:
1958 id = SND_AUDIOCODEC_AAC;
1959 break;
1960 case AUDIO_FORMAT_AAC_ADTS:
1961 id = SND_AUDIOCODEC_AAC;
1962 break;
1963 case AUDIO_FORMAT_AAC_LATM:
1964 id = SND_AUDIOCODEC_AAC;
1965 break;
1966 case AUDIO_FORMAT_PCM:
1967 id = SND_AUDIOCODEC_PCM;
1968 break;
1969 case AUDIO_FORMAT_FLAC:
1970 case AUDIO_FORMAT_ALAC:
1971 case AUDIO_FORMAT_APE:
1972 case AUDIO_FORMAT_VORBIS:
1973 case AUDIO_FORMAT_WMA:
1974 case AUDIO_FORMAT_WMA_PRO:
1975 case AUDIO_FORMAT_DSD:
1976 case AUDIO_FORMAT_APTX:
1977 id = SND_AUDIOCODEC_BESPOKE;
1978 break;
1979 case AUDIO_FORMAT_MP2:
1980 id = SND_AUDIOCODEC_MP2;
1981 break;
1982 case AUDIO_FORMAT_AC3:
1983 id = SND_AUDIOCODEC_AC3;
1984 break;
1985 case AUDIO_FORMAT_E_AC3:
1986 case AUDIO_FORMAT_E_AC3_JOC:
1987 id = SND_AUDIOCODEC_EAC3;
1988 break;
1989 case AUDIO_FORMAT_DTS:
1990 case AUDIO_FORMAT_DTS_HD:
1991 id = SND_AUDIOCODEC_DTS;
1992 break;
1993 case AUDIO_FORMAT_DOLBY_TRUEHD:
1994 id = SND_AUDIOCODEC_TRUEHD;
1995 break;
1996 case AUDIO_FORMAT_IEC61937:
1997 id = SND_AUDIOCODEC_IEC61937;
1998 break;
1999 default:
2000 ALOGE("%s: Unsupported audio format :%x", __func__, format);
2001 }
2002
2003 return id;
2004}
2005#else
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05302006int get_snd_codec_id(audio_format_t format)
2007{
2008 int id = 0;
2009
2010 switch (format & AUDIO_FORMAT_MAIN_MASK) {
2011 case AUDIO_FORMAT_MP3:
2012 id = SND_AUDIOCODEC_MP3;
2013 break;
2014 case AUDIO_FORMAT_AAC:
2015 id = SND_AUDIOCODEC_AAC;
2016 break;
2017 case AUDIO_FORMAT_AAC_ADTS:
2018 id = SND_AUDIOCODEC_AAC;
2019 break;
Arun Kumar Dasari3b174182016-12-27 13:01:14 +05302020 case AUDIO_FORMAT_AAC_LATM:
2021 id = SND_AUDIOCODEC_AAC;
2022 break;
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05302023 case AUDIO_FORMAT_PCM:
2024 id = SND_AUDIOCODEC_PCM;
2025 break;
2026 case AUDIO_FORMAT_FLAC:
2027 id = SND_AUDIOCODEC_FLAC;
2028 break;
2029 case AUDIO_FORMAT_ALAC:
2030 id = SND_AUDIOCODEC_ALAC;
2031 break;
2032 case AUDIO_FORMAT_APE:
2033 id = SND_AUDIOCODEC_APE;
2034 break;
2035 case AUDIO_FORMAT_VORBIS:
2036 id = SND_AUDIOCODEC_VORBIS;
2037 break;
2038 case AUDIO_FORMAT_WMA:
2039 id = SND_AUDIOCODEC_WMA;
2040 break;
Dechen Chai4a575252021-07-30 09:21:28 +05302041#ifndef AUDIO_DISABLE_COMPRESS_FORMAT
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05302042 case AUDIO_FORMAT_WMA_PRO:
2043 id = SND_AUDIOCODEC_WMA_PRO;
2044 break;
Dechen Chai4a575252021-07-30 09:21:28 +05302045#endif
Satish Babu Patakokila0c313922016-12-08 12:07:08 +05302046 case AUDIO_FORMAT_MP2:
2047 id = SND_AUDIOCODEC_MP2;
2048 break;
Satish Babu Patakokila915ecba2017-01-10 17:43:56 +05302049 case AUDIO_FORMAT_AC3:
2050 id = SND_AUDIOCODEC_AC3;
2051 break;
2052 case AUDIO_FORMAT_E_AC3:
2053 case AUDIO_FORMAT_E_AC3_JOC:
2054 id = SND_AUDIOCODEC_EAC3;
2055 break;
2056 case AUDIO_FORMAT_DTS:
2057 case AUDIO_FORMAT_DTS_HD:
2058 id = SND_AUDIOCODEC_DTS;
2059 break;
Ben Romberger1aaaf862017-04-06 17:49:46 -07002060 case AUDIO_FORMAT_DOLBY_TRUEHD:
2061 id = SND_AUDIOCODEC_TRUEHD;
2062 break;
Naresh Tanniru928f0862017-04-07 16:44:23 -07002063 case AUDIO_FORMAT_IEC61937:
2064 id = SND_AUDIOCODEC_IEC61937;
2065 break;
Dechen Chai4a575252021-07-30 09:21:28 +05302066#ifndef AUDIO_DISABLE_COMPRESS_FORMAT
Preetam Singh Ranawat4277a5a2017-01-18 19:02:24 +05302067 case AUDIO_FORMAT_DSD:
2068 id = SND_AUDIOCODEC_DSD;
2069 break;
Dhanalakshmi Siddani18737932016-11-29 17:33:17 +05302070 case AUDIO_FORMAT_APTX:
2071 id = SND_AUDIOCODEC_APTX;
2072 break;
Dechen Chai4a575252021-07-30 09:21:28 +05302073#endif
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05302074 default:
2075 ALOGE("%s: Unsupported audio format :%x", __func__, format);
2076 }
2077
2078 return id;
2079}
Xiaojun Sang782e5b12020-06-29 21:13:06 +08002080#endif
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05302081
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07002082void audio_extn_utils_send_audio_calibration(struct audio_device *adev,
2083 struct audio_usecase *usecase)
2084{
2085 int type = usecase->type;
2086
Dhananjay Kumar14245982017-01-16 20:21:00 +05302087 if (type == PCM_PLAYBACK && usecase->stream.out != NULL) {
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05302088 platform_send_audio_calibration(adev->platform, usecase,
Zhou Song06761dd2019-04-28 18:08:17 +08002089 usecase->stream.out->app_type_cfg.app_type);
Dhananjay Kumar14245982017-01-16 20:21:00 +05302090 } else if (type == PCM_CAPTURE && usecase->stream.in != NULL) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +05302091 platform_send_audio_calibration(adev->platform, usecase,
Zhou Song06761dd2019-04-28 18:08:17 +08002092 usecase->stream.in->app_type_cfg.app_type);
Sriram Kumar9c836482020-10-27 20:34:35 +05302093 } else if (type == PCM_HFP_CALL) {
2094 /* when app type is default. the sample rate is not used to send cal */
2095#ifdef ENABLE_HFP_CALIBRATION
2096 platform_send_audio_calibration_hfp(adev->platform, usecase->in_snd_device);
2097#else
2098 platform_send_audio_calibration(adev->platform, usecase,
2099 platform_get_default_app_type_v2(adev->platform, usecase->type));
2100#endif
2101 } else if ((type == PCM_CAPTURE) ||
Derek Chena30a5f42019-12-03 11:17:09 -05002102 (type == TRANSCODE_LOOPBACK_RX && usecase->stream.inout != NULL) ||
Fei Tongaffdf732020-02-20 20:39:05 +08002103 (type == ICC_CALL) || (type == SYNTH_LOOPBACK)) {
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05302104 platform_send_audio_calibration(adev->platform, usecase,
Zhou Song06761dd2019-04-28 18:08:17 +08002105 platform_get_default_app_type_v2(adev->platform, usecase->type));
Vidyakumar Athotae57f6002017-03-01 13:13:16 -08002106 } else {
2107 /* No need to send audio calibration for voice and voip call usecases */
2108 if ((type != VOICE_CALL) && (type != VOIP_CALL))
2109 ALOGW("%s: No audio calibration for usecase type = %d", __func__, type);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07002110 }
2111}
2112
Ben Rombergera04fabc2014-11-14 12:16:03 -08002113// Base64 Encode and Decode
2114// Not all features supported. This must be used only with following conditions.
2115// Decode Modes: Support with and without padding
2116// CRLF not handling. So no CRLF in string to decode.
2117// Encode Modes: Supports only padding
2118int b64decode(char *inp, int ilen, uint8_t* outp)
2119{
2120 int i, j, k, ii, num;
2121 int rem, pcnt;
2122 uint32_t res=0;
2123 uint8_t getIndex[MAX_BASEINDEX_LEN];
2124 uint8_t tmp, cflag;
2125
2126 if(inp == NULL || outp == NULL || ilen <= 0) {
2127 ALOGE("[%s] received NULL pointer or zero length",__func__);
2128 return -1;
2129 }
2130
2131 memset(getIndex, MAX_BASEINDEX_LEN-1, sizeof(getIndex));
2132 for(i=0;i<BASE_TABLE_SIZE;i++) {
2133 getIndex[(uint8_t)bTable[i]] = (uint8_t)i;
2134 }
2135 getIndex[(uint8_t)'=']=0;
2136
2137 j=0;k=0;
2138 num = ilen/4;
2139 rem = ilen%4;
2140 if(rem==0)
2141 num = num-1;
2142 cflag=0;
2143 for(i=0; i<num; i++) {
2144 res=0;
2145 for(ii=0;ii<4;ii++) {
2146 res = res << 6;
2147 tmp = getIndex[(uint8_t)inp[j++]];
2148 res = res | tmp;
2149 cflag = cflag | tmp;
2150 }
2151 outp[k++] = (res >> 16)&0xFF;
2152 outp[k++] = (res >> 8)&0xFF;
2153 outp[k++] = res & 0xFF;
2154 }
2155
2156 // Handle last bytes special
2157 pcnt=0;
2158 if(rem == 0) {
2159 //With padding or full data
2160 res = 0;
2161 for(ii=0;ii<4;ii++) {
2162 if(inp[j] == '=')
2163 pcnt++;
2164 res = res << 6;
2165 tmp = getIndex[(uint8_t)inp[j++]];
2166 res = res | tmp;
2167 cflag = cflag | tmp;
2168 }
2169 outp[k++] = res >> 16;
2170 if(pcnt == 2)
2171 goto done;
2172 outp[k++] = (res>>8)&0xFF;
2173 if(pcnt == 1)
2174 goto done;
2175 outp[k++] = res&0xFF;
2176 } else {
2177 //without padding
2178 res = 0;
2179 for(i=0;i<rem;i++) {
2180 res = res << 6;
2181 tmp = getIndex[(uint8_t)inp[j++]];
2182 res = res | tmp;
2183 cflag = cflag | tmp;
2184 }
2185 for(i=rem;i<4;i++) {
2186 res = res << 6;
2187 pcnt++;
2188 }
2189 outp[k++] = res >> 16;
2190 if(pcnt == 2)
2191 goto done;
2192 outp[k++] = (res>>8)&0xFF;
2193 if(pcnt == 1)
2194 goto done;
2195 outp[k++] = res&0xFF;
2196 }
2197done:
2198 if(cflag == 0xFF) {
2199 ALOGE("[%s] base64 decode failed. Invalid character found %s",
2200 __func__, inp);
2201 return 0;
2202 }
2203 return k;
2204}
2205
2206int b64encode(uint8_t *inp, int ilen, char* outp)
2207{
2208 int i,j,k, num;
2209 int rem=0;
2210 uint32_t res=0;
2211
2212 if(inp == NULL || outp == NULL || ilen<=0) {
2213 ALOGE("[%s] received NULL pointer or zero input length",__func__);
2214 return -1;
2215 }
2216
2217 num = ilen/3;
2218 rem = ilen%3;
2219 j=0;k=0;
2220 for(i=0; i<num; i++) {
2221 //prepare index
2222 res = inp[j++]<<16;
2223 res = res | inp[j++]<<8;
2224 res = res | inp[j++];
2225 //get output map from index
2226 outp[k++] = (char) bTable[(res>>18)&0x3F];
2227 outp[k++] = (char) bTable[(res>>12)&0x3F];
2228 outp[k++] = (char) bTable[(res>>6)&0x3F];
2229 outp[k++] = (char) bTable[res&0x3F];
2230 }
2231
2232 switch(rem) {
2233 case 1:
2234 res = inp[j++]<<16;
2235 outp[k++] = (char) bTable[res>>18];
2236 outp[k++] = (char) bTable[(res>>12)&0x3F];
2237 //outp[k++] = '=';
2238 //outp[k++] = '=';
2239 break;
2240 case 2:
2241 res = inp[j++]<<16;
2242 res = res | inp[j++]<<8;
2243 outp[k++] = (char) bTable[res>>18];
2244 outp[k++] = (char) bTable[(res>>12)&0x3F];
2245 outp[k++] = (char) bTable[(res>>6)&0x3F];
2246 //outp[k++] = '=';
2247 break;
2248 default:
2249 break;
2250 }
Ben Rombergera04fabc2014-11-14 12:16:03 -08002251 outp[k] = '\0';
2252 return k;
2253}
Ashish Jain81eb2a82015-05-13 10:52:34 +05302254
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05302255
2256int audio_extn_utils_get_codec_version(const char *snd_card_name,
2257 int card_num,
2258 char *codec_version)
2259{
2260 char procfs_path[50];
2261 FILE *fp;
2262
2263 if (strstr(snd_card_name, "tasha")) {
2264 snprintf(procfs_path, sizeof(procfs_path),
2265 "/proc/asound/card%d/codecs/tasha/version", card_num);
2266 if ((fp = fopen(procfs_path, "r")) != NULL) {
2267 fgets(codec_version, CODEC_VERSION_MAX_LENGTH, fp);
2268 fclose(fp);
2269 } else {
2270 ALOGE("%s: ERROR. cannot open %s", __func__, procfs_path);
2271 return -ENOENT;
2272 }
2273 ALOGD("%s: codec version %s", __func__, codec_version);
2274 }
2275
2276 return 0;
2277}
2278
Preetam Singh Ranawat9d0d8e42019-07-15 12:27:25 +05302279int audio_extn_utils_get_codec_variant(int card_num,
2280 char *codec_variant)
2281{
2282 char procfs_path[50];
2283 FILE *fp;
2284 snprintf(procfs_path, sizeof(procfs_path),
2285 "/proc/asound/card%d/codecs/wcd938x/variant", card_num);
2286 if ((fp = fopen(procfs_path, "r")) == NULL) {
2287 snprintf(procfs_path, sizeof(procfs_path),
2288 "/proc/asound/card%d/codecs/wcd937x/variant", card_num);
2289 if ((fp = fopen(procfs_path, "r")) == NULL) {
2290 ALOGE("%s: ERROR. cannot open %s", __func__, procfs_path);
2291 return -ENOENT;
2292 }
2293 }
2294 fgets(codec_variant, CODEC_VARIANT_MAX_LENGTH, fp);
2295 fclose(fp);
2296 ALOGD("%s: codec variant is %s", __func__, codec_variant);
2297 return 0;
2298}
2299
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05302300
Ashish Jain81eb2a82015-05-13 10:52:34 +05302301#ifdef AUDIO_EXTERNAL_HDMI_ENABLED
2302
2303void get_default_compressed_channel_status(
2304 unsigned char *channel_status)
2305{
Ashish Jain81eb2a82015-05-13 10:52:34 +05302306 memset(channel_status,0,24);
2307
2308 /* block start bit in preamble bit 3 */
2309 channel_status[0] |= PROFESSIONAL;
2310 //compre out
2311 channel_status[0] |= NON_LPCM;
2312 // sample rate; fixed 48K for default/transcode
2313 channel_status[3] |= SR_48000;
2314}
2315
Ashish Jain81eb2a82015-05-13 10:52:34 +05302316int32_t get_compressed_channel_status(void *audio_stream_data,
2317 uint32_t audio_frame_size,
2318 unsigned char *channel_status,
2319 enum audio_parser_code_type codec_type)
2320 // codec_type - AUDIO_PARSER_CODEC_AC3
2321 // - AUDIO_PARSER_CODEC_DTS
2322{
2323 unsigned char *stream;
2324 int ret = 0;
2325 stream = (unsigned char *)audio_stream_data;
2326
2327 if (audio_stream_data == NULL || audio_frame_size == 0) {
2328 ALOGW("no buffer to get channel status, return default for compress");
2329 get_default_compressed_channel_status(channel_status);
2330 return ret;
2331 }
2332
2333 memset(channel_status,0,24);
2334 if(init_audio_parser(stream, audio_frame_size, codec_type) == -1)
2335 {
2336 ALOGE("init audio parser failed");
2337 return -1;
2338 }
2339 ret = get_channel_status(channel_status, codec_type);
2340 return ret;
2341
2342}
2343
Ashish Jain81eb2a82015-05-13 10:52:34 +05302344void get_lpcm_channel_status(uint32_t sampleRate,
2345 unsigned char *channel_status)
2346{
2347 int32_t status = 0;
Ashish Jain81eb2a82015-05-13 10:52:34 +05302348 memset(channel_status,0,24);
2349 /* block start bit in preamble bit 3 */
2350 channel_status[0] |= PROFESSIONAL;
2351 //LPCM OUT
2352 channel_status[0] &= ~NON_LPCM;
2353
2354 switch (sampleRate) {
2355 case 8000:
2356 case 11025:
2357 case 12000:
2358 case 16000:
2359 case 22050:
2360 channel_status[3] |= SR_NOTID;
Preetam Singh Ranawat61716b12015-12-14 11:55:24 +05302361 break;
Ashish Jain81eb2a82015-05-13 10:52:34 +05302362 case 24000:
2363 channel_status[3] |= SR_24000;
2364 break;
2365 case 32000:
2366 channel_status[3] |= SR_32000;
2367 break;
2368 case 44100:
2369 channel_status[3] |= SR_44100;
2370 break;
2371 case 48000:
2372 channel_status[3] |= SR_48000;
2373 break;
2374 case 88200:
2375 channel_status[3] |= SR_88200;
2376 break;
2377 case 96000:
2378 channel_status[3] |= SR_96000;
2379 break;
2380 case 176400:
2381 channel_status[3] |= SR_176400;
2382 break;
2383 case 192000:
2384 channel_status[3] |= SR_192000;
2385 break;
2386 default:
2387 ALOGV("Invalid sample_rate %u\n", sampleRate);
2388 status = -1;
2389 break;
2390 }
2391}
2392
2393void audio_utils_set_hdmi_channel_status(struct stream_out *out, char * buffer, size_t bytes)
2394{
2395 unsigned char channel_status[24]={0};
2396 struct snd_aes_iec958 iec958;
2397 const char *mixer_ctl_name = "IEC958 Playback PCM Stream";
2398 struct mixer_ctl *ctl;
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +05302399 ALOGV("%s: buffer %s bytes %zd", __func__, buffer, bytes);
Arun Mirpurie008ed22019-03-21 11:21:04 -07002400
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05302401 if (audio_extn_utils_is_dolby_format(out->format) &&
Ashish Jain81eb2a82015-05-13 10:52:34 +05302402 /*TODO:Extend code to support DTS passthrough*/
2403 /*set compressed channel status bits*/
Arun Mirpurie008ed22019-03-21 11:21:04 -07002404 audio_extn_passthru_is_passthrough_stream(out) &&
2405 audio_extn_is_hdmi_passthru_enabled()) {
Ashish Jain81eb2a82015-05-13 10:52:34 +05302406 get_compressed_channel_status(buffer, bytes, channel_status, AUDIO_PARSER_CODEC_AC3);
Arun Mirpurie008ed22019-03-21 11:21:04 -07002407 } else {
Ashish Jain81eb2a82015-05-13 10:52:34 +05302408 /*set channel status bit for LPCM*/
2409 get_lpcm_channel_status(out->sample_rate, channel_status);
2410 }
2411
2412 memcpy(iec958.status, channel_status,sizeof(iec958.status));
2413 ctl = mixer_get_ctl_by_name(out->dev->mixer, mixer_ctl_name);
2414 if (!ctl) {
2415 ALOGE("%s: Could not get ctl for mixer cmd - %s",
2416 __func__, mixer_ctl_name);
2417 return;
2418 }
2419 if (mixer_ctl_set_array(ctl, &iec958, sizeof(iec958)) < 0) {
2420 ALOGE("%s: Could not set channel status for ext HDMI ",
2421 __func__);
2422 return;
2423 }
2424
2425}
2426#endif
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302427
2428int audio_extn_utils_get_avt_device_drift(
2429 struct audio_usecase *usecase,
2430 struct audio_avt_device_drift_param *drift_param)
2431{
2432 int ret = 0, count = 0;
2433 char avt_device_drift_mixer_ctl_name[MIXER_PATH_MAX_LENGTH] = {0};
Naresh Tanniru6160c712017-04-17 15:43:48 +05302434 const char *backend = NULL;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302435 struct mixer_ctl *ctl = NULL;
2436 struct audio_avt_device_drift_stats drift_stats;
2437 struct audio_device *adev = NULL;
2438
2439 if (usecase != NULL && usecase->type == PCM_PLAYBACK) {
Naresh Tanniru6160c712017-04-17 15:43:48 +05302440 backend = platform_get_snd_device_backend_interface(usecase->out_snd_device);
2441 if (!backend) {
2442 ALOGE("%s: Unsupported device %d", __func__,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08002443 get_device_types(&usecase->stream.out->device_list));
Naresh Tanniru6160c712017-04-17 15:43:48 +05302444 ret = -EINVAL;
2445 goto done;
2446 }
2447 strlcpy(avt_device_drift_mixer_ctl_name,
2448 backend,
2449 MIXER_PATH_MAX_LENGTH);
2450
2451 count = strlen(backend);
2452 if (MIXER_PATH_MAX_LENGTH - count > 0) {
2453 strlcat(&avt_device_drift_mixer_ctl_name[count],
2454 " DRIFT",
2455 MIXER_PATH_MAX_LENGTH - count);
2456 } else {
2457 ret = -EINVAL;
2458 goto done;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302459 }
2460 } else {
Naresh Tanniru7586e292017-04-13 10:38:13 +05302461 ALOGE("%s: Invalid usecase",__func__);
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302462 ret = -EINVAL;
Naresh Tanniru6160c712017-04-17 15:43:48 +05302463 goto done;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302464 }
2465
Naresh Tanniru6160c712017-04-17 15:43:48 +05302466 adev = usecase->stream.out->dev;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302467 ctl = mixer_get_ctl_by_name(adev->mixer, avt_device_drift_mixer_ctl_name);
2468 if (!ctl) {
2469 ALOGE("%s: Could not get ctl for mixer cmd - %s",
2470 __func__, avt_device_drift_mixer_ctl_name);
2471
2472 ret = -EINVAL;
2473 goto done;
2474 }
2475
2476 ALOGV("%s: Getting AV Timer vs Device Drift mixer ctrl name %s", __func__,
2477 avt_device_drift_mixer_ctl_name);
2478
2479 mixer_ctl_update(ctl);
2480 count = mixer_ctl_get_num_values(ctl);
2481 if (count != sizeof(struct audio_avt_device_drift_stats)) {
2482 ALOGE("%s: mixer_ctl_get_num_values() invalid drift_stats data size",
2483 __func__);
2484
2485 ret = -EINVAL;
2486 goto done;
2487 }
2488
2489 ret = mixer_ctl_get_array(ctl, (void *)&drift_stats, count);
2490 if (ret != 0) {
2491 ALOGE("%s: mixer_ctl_get_array() failed to get drift_stats Params",
2492 __func__);
2493
2494 ret = -EINVAL;
2495 goto done;
2496 }
2497 memcpy(drift_param, &drift_stats.drift_param,
2498 sizeof(struct audio_avt_device_drift_param));
2499done:
2500 return ret;
2501}
Manish Dewangan07de2142017-02-27 19:27:20 +05302502
2503#ifdef SNDRV_COMPRESS_PATH_DELAY
2504int audio_extn_utils_compress_get_dsp_latency(struct stream_out *out)
2505{
2506 int ret = -EINVAL;
2507 struct snd_compr_metadata metadata;
2508 int delay_ms = COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
2509
Weiyin Jiangc49a3b52018-08-17 16:16:08 +08002510 /* override the latency for pcm offload use case */
2511 if ((out->flags & AUDIO_OUTPUT_FLAG_DIRECT) &&
2512 !(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)) {
2513 delay_ms = PCM_OFFLOAD_PLAYBACK_LATENCY;
2514 }
2515
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -07002516 if (property_get_bool("vendor.audio.playback.dsp.pathdelay", false)) {
Manish Dewangan07de2142017-02-27 19:27:20 +05302517 ALOGD("%s:: Quering DSP delay %d",__func__, __LINE__);
2518 if (!(is_offload_usecase(out->usecase))) {
2519 ALOGE("%s:: not supported for non offload session", __func__);
2520 goto exit;
2521 }
2522
2523 if (!out->compr) {
2524 ALOGD("%s:: Invalid compress handle,returning default dsp latency",
2525 __func__);
2526 goto exit;
2527 }
2528
2529 metadata.key = SNDRV_COMPRESS_PATH_DELAY;
2530 ret = compress_get_metadata(out->compr, &metadata);
2531 if(ret) {
2532 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2533 goto exit;
2534 }
2535 delay_ms = metadata.value[0] / 1000; /*convert to ms*/
2536 } else {
2537 ALOGD("%s:: Using Fix DSP delay",__func__);
2538 }
2539
2540exit:
2541 ALOGD("%s:: delay in ms is %d",__func__, delay_ms);
2542 return delay_ms;
2543}
2544#else
2545int audio_extn_utils_compress_get_dsp_latency(struct stream_out *out __unused)
2546{
2547 return COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
2548}
2549#endif
Manish Dewangan69426c82017-01-30 17:35:36 +05302550
2551#ifdef SNDRV_COMPRESS_RENDER_MODE
2552int audio_extn_utils_compress_set_render_mode(struct stream_out *out)
2553{
2554 struct snd_compr_metadata metadata;
2555 int ret = -EINVAL;
2556
2557 if (!(is_offload_usecase(out->usecase))) {
2558 ALOGE("%s:: not supported for non offload session", __func__);
2559 goto exit;
2560 }
2561
2562 if (!out->compr) {
2563 ALOGD("%s:: Invalid compress handle",
2564 __func__);
2565 goto exit;
2566 }
2567
2568 ALOGD("%s:: render mode %d", __func__, out->render_mode);
2569
2570 metadata.key = SNDRV_COMPRESS_RENDER_MODE;
2571 if (out->render_mode == RENDER_MODE_AUDIO_MASTER) {
2572 metadata.value[0] = SNDRV_COMPRESS_RENDER_MODE_AUDIO_MASTER;
2573 } else if (out->render_mode == RENDER_MODE_AUDIO_STC_MASTER) {
2574 metadata.value[0] = SNDRV_COMPRESS_RENDER_MODE_STC_MASTER;
2575 } else {
2576 ret = 0;
2577 goto exit;
2578 }
2579 ret = compress_set_metadata(out->compr, &metadata);
2580 if(ret) {
2581 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2582 }
2583exit:
2584 return ret;
2585}
2586#else
2587int audio_extn_utils_compress_set_render_mode(struct stream_out *out __unused)
2588{
2589 ALOGD("%s:: configuring render mode not supported", __func__);
2590 return 0;
2591}
2592#endif
Manish Dewangan58229382017-02-02 15:48:41 +05302593
2594#ifdef SNDRV_COMPRESS_CLK_REC_MODE
2595int audio_extn_utils_compress_set_clk_rec_mode(
2596 struct audio_usecase *usecase)
2597{
2598 struct snd_compr_metadata metadata;
2599 struct stream_out *out = NULL;
2600 int ret = -EINVAL;
2601
Naresh Tanniru7586e292017-04-13 10:38:13 +05302602 if (usecase == NULL || usecase->type != PCM_PLAYBACK) {
Manish Dewangan58229382017-02-02 15:48:41 +05302603 ALOGE("%s:: Invalid use case", __func__);
2604 goto exit;
2605 }
2606
2607 out = usecase->stream.out;
2608 if (!out) {
2609 ALOGE("%s:: invalid stream", __func__);
2610 goto exit;
2611 }
2612
2613 if (!is_offload_usecase(out->usecase)) {
2614 ALOGE("%s:: not supported for non offload session", __func__);
2615 goto exit;
2616 }
2617
2618 if (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER) {
2619 ALOGD("%s:: clk recovery is only supported in STC render mode",
2620 __func__);
2621 ret = 0;
2622 goto exit;
2623 }
2624
2625 if (!out->compr) {
2626 ALOGD("%s:: Invalid compress handle",
2627 __func__);
2628 goto exit;
2629 }
2630 metadata.key = SNDRV_COMPRESS_CLK_REC_MODE;
2631 switch(usecase->out_snd_device) {
2632 case SND_DEVICE_OUT_HDMI:
2633 case SND_DEVICE_OUT_SPEAKER_AND_HDMI:
2634 case SND_DEVICE_OUT_DISPLAY_PORT:
2635 case SND_DEVICE_OUT_SPEAKER_AND_DISPLAY_PORT:
2636 metadata.value[0] = SNDRV_COMPRESS_CLK_REC_MODE_NONE;
2637 break;
2638 default:
2639 metadata.value[0] = SNDRV_COMPRESS_CLK_REC_MODE_AUTO;
2640 break;
2641 }
2642
2643 ALOGD("%s:: clk recovery mode %d",__func__, metadata.value[0]);
2644
2645 ret = compress_set_metadata(out->compr, &metadata);
2646 if(ret) {
2647 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2648 }
2649
2650exit:
2651 return ret;
2652}
2653#else
2654int audio_extn_utils_compress_set_clk_rec_mode(
2655 struct audio_usecase *usecase __unused)
2656{
2657 ALOGD("%s:: configuring render mode not supported", __func__);
2658 return 0;
2659}
2660#endif
Manish Dewangan27346042017-03-01 12:56:12 +05302661
2662#ifdef SNDRV_COMPRESS_RENDER_WINDOW
2663int audio_extn_utils_compress_set_render_window(
2664 struct stream_out *out,
2665 struct audio_out_render_window_param *render_window)
2666{
2667 struct snd_compr_metadata metadata;
2668 int ret = -EINVAL;
2669
Manish Dewangan27346042017-03-01 12:56:12 +05302670 if(render_window == NULL) {
2671 ALOGE("%s:: Invalid render_window", __func__);
2672 goto exit;
2673 }
2674
Aniket Kumar Lata1e1d3662017-06-01 18:45:48 -07002675 ALOGD("%s:: render window start 0x%"PRIx64" end 0x%"PRIx64"",
2676 __func__,render_window->render_ws, render_window->render_we);
2677
Manish Dewangan27346042017-03-01 12:56:12 +05302678 if (!is_offload_usecase(out->usecase)) {
2679 ALOGE("%s:: not supported for non offload session", __func__);
2680 goto exit;
2681 }
2682
Naresh Tanniru6160c712017-04-17 15:43:48 +05302683 if ((out->render_mode != RENDER_MODE_AUDIO_MASTER) &&
2684 (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER)) {
Manish Dewangan27346042017-03-01 12:56:12 +05302685 ALOGD("%s:: only supported in timestamp mode, current "
2686 "render mode mode %d", __func__, out->render_mode);
2687 goto exit;
2688 }
2689
2690 if (!out->compr) {
2691 ALOGW("%s:: offload session not yet opened,"
2692 "render window will be configure later", __func__);
2693 /* store render window to reconfigure in start_output_stream() */
2694 goto exit;
2695 }
2696
2697 metadata.key = SNDRV_COMPRESS_RENDER_WINDOW;
2698 /*render window start value */
2699 metadata.value[0] = 0xFFFFFFFF & render_window->render_ws; /* lsb */
2700 metadata.value[1] = \
2701 (0xFFFFFFFF00000000 & render_window->render_ws) >> 32; /* msb*/
2702 /*render window end value */
2703 metadata.value[2] = 0xFFFFFFFF & render_window->render_we; /* lsb */
2704 metadata.value[3] = \
2705 (0xFFFFFFFF00000000 & render_window->render_we) >> 32; /* msb*/
2706
2707 ret = compress_set_metadata(out->compr, &metadata);
2708 if(ret) {
2709 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2710 }
2711
2712exit:
2713 return ret;
2714}
2715#else
2716int audio_extn_utils_compress_set_render_window(
2717 struct stream_out *out __unused,
2718 struct audio_out_render_window_param *render_window __unused)
2719{
2720 ALOGD("%s:: configuring render window not supported", __func__);
2721 return 0;
2722}
2723#endif
Manish Dewangan14956cc2017-02-14 18:54:42 +05302724
2725#ifdef SNDRV_COMPRESS_START_DELAY
2726int audio_extn_utils_compress_set_start_delay(
2727 struct stream_out *out,
2728 struct audio_out_start_delay_param *delay_param)
2729{
2730 struct snd_compr_metadata metadata;
2731 int ret = -EINVAL;
2732
2733 if(delay_param == NULL) {
2734 ALOGE("%s:: Invalid delay_param", __func__);
2735 goto exit;
2736 }
2737
2738 ALOGD("%s:: render start delay 0x%"PRIx64" ", __func__,
2739 delay_param->start_delay);
2740
2741 if (!is_offload_usecase(out->usecase)) {
2742 ALOGE("%s:: not supported for non offload session", __func__);
2743 goto exit;
2744 }
2745
Naresh Tanniru6160c712017-04-17 15:43:48 +05302746 if ((out->render_mode != RENDER_MODE_AUDIO_MASTER) &&
2747 (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER)) {
Manish Dewangan14956cc2017-02-14 18:54:42 +05302748 ALOGD("%s:: only supported in timestamp mode, current "
2749 "render mode mode %d", __func__, out->render_mode);
2750 goto exit;
2751 }
2752
2753 if (!out->compr) {
2754 ALOGW("%s:: offload session not yet opened,"
2755 "start delay will be configure later", __func__);
2756 goto exit;
2757 }
2758
2759 metadata.key = SNDRV_COMPRESS_START_DELAY;
2760 metadata.value[0] = 0xFFFFFFFF & delay_param->start_delay; /* lsb */
2761 metadata.value[1] = \
2762 (0xFFFFFFFF00000000 & delay_param->start_delay) >> 32; /* msb*/
2763
2764 ret = compress_set_metadata(out->compr, &metadata);
2765 if(ret) {
2766 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2767 }
2768
2769exit:
2770 return ret;
2771}
2772#else
2773int audio_extn_utils_compress_set_start_delay(
2774 struct stream_out *out __unused,
2775 struct audio_out_start_delay_param *delay_param __unused)
2776{
2777 ALOGD("%s:: configuring render window not supported", __func__);
2778 return 0;
2779}
2780#endif
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002781
Surendar Karka287348c2019-04-10 18:31:46 +05302782#ifdef SNDRV_COMPRESS_DSP_POSITION
2783int audio_extn_utils_compress_get_dsp_presentation_pos(struct stream_out *out,
2784 uint64_t *frames, struct timespec *timestamp, int32_t clock_id)
2785{
2786 int ret = -EINVAL;
2787 uint64_t *val = NULL;
2788 uint64_t time = 0;
2789 struct snd_compr_metadata metadata;
2790
2791 ALOGV("%s:: Quering DSP position with clock id %d",__func__, clock_id);
2792 metadata.key = SNDRV_COMPRESS_DSP_POSITION;
2793 metadata.value[0] = clock_id;
2794 ret = compress_get_metadata(out->compr, &metadata);
2795 if (ret) {
2796 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2797 ret = -errno;
2798 goto exit;
2799 }
2800 val = (uint64_t *)&metadata.value[1];
2801 *frames = *val;
2802 time = *(val + 1);
2803 timestamp->tv_sec = time / 1000000;
2804 timestamp->tv_nsec = (time % 1000000)*1000;
2805
2806exit:
2807 return ret;
2808}
2809#else
2810int audio_extn_utils_compress_get_dsp_presentation_pos(struct stream_out *out __unused,
2811 uint64_t *frames __unused, struct timespec *timestamp __unused,
2812 int32_t clock_id __unused)
2813{
2814 ALOGD("%s:: dsp presentation position not supported", __func__);
2815 return 0;
2816
2817}
2818#endif
2819
2820#ifdef SNDRV_PCM_IOCTL_DSP_POSITION
2821int audio_extn_utils_pcm_get_dsp_presentation_pos(struct stream_out *out,
2822 uint64_t *frames, struct timespec *timestamp, int32_t clock_id)
2823{
2824 int ret = -EINVAL;
2825 uint64_t time = 0;
2826 struct snd_pcm_prsnt_position prsnt_position;
Krishna Kishor Jhac1542b22022-08-22 13:12:09 +05302827 memset(&prsnt_position, 0, sizeof(struct snd_pcm_prsnt_position));
Surendar Karka287348c2019-04-10 18:31:46 +05302828
2829 ALOGV("%s:: Quering DSP position with clock id %d",__func__, clock_id);
2830 prsnt_position.clock_id = clock_id;
2831 ret = pcm_ioctl(out->pcm, SNDRV_PCM_IOCTL_DSP_POSITION, &prsnt_position);
2832 if (ret) {
2833 ALOGE("%s::error %d", __func__, ret);
2834 ret = -EIO;
2835 goto exit;
2836 }
2837
2838 *frames = prsnt_position.frames;
2839 time = prsnt_position.timestamp;
2840 timestamp->tv_sec = time / 1000000;
2841 timestamp->tv_nsec = (time % 1000000)*1000;
2842
2843exit:
2844 return ret;
2845}
2846#else
2847int audio_extn_utils_pcm_get_dsp_presentation_pos(struct stream_out *out __unused,
2848 uint64_t *frames __unused, struct timespec *timestamp __unused,
2849 int32_t clock_id __unused)
2850{
2851 ALOGD("%s:: dsp presentation position not supported", __func__);
2852 return 0;
2853
2854}
2855#endif
2856
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002857#define MAX_SND_CARD 8
Jaideep Sharmadf948882020-10-22 14:13:52 +05302858#define RETRY_US 400000
2859#define RETRY_NUMBER 100
Aalique Grahame22e49102018-12-18 14:23:57 -08002860#define PLATFORM_INFO_XML_PATH "audio_platform_info.xml"
2861#define PLATFORM_INFO_XML_BASE_STRING "audio_platform_info"
2862
Aalique Grahame22e49102018-12-18 14:23:57 -08002863bool audio_extn_utils_resolve_config_file(char file_name[MIXER_PATH_MAX_LENGTH])
2864{
2865 char full_config_path[MIXER_PATH_MAX_LENGTH];
Saurav Kumarf5d2c342020-07-29 19:31:49 +05302866 char vendor_config_path[VENDOR_CONFIG_PATH_MAX_LENGTH];
2867
2868 /* Get path for audio configuration files in vendor */
2869 audio_get_vendor_config_path(vendor_config_path,
2870 sizeof(vendor_config_path));
2871 snprintf(full_config_path, sizeof(full_config_path),
2872 "%s/%s", vendor_config_path, file_name);
2873 if (F_OK == access(full_config_path, 0)) {
2874 strlcpy(file_name, full_config_path, MIXER_PATH_MAX_LENGTH);
2875 return true;
Aalique Grahame22e49102018-12-18 14:23:57 -08002876 }
2877 return false;
2878}
2879
2880/* platform_info_file should be size 'MIXER_PATH_MAX_LENGTH' */
2881int audio_extn_utils_get_platform_info(const char* snd_card_name, char* platform_info_file)
2882{
2883 if (NULL == snd_card_name) {
2884 return -1;
2885 }
2886
2887 struct snd_card_split *snd_split_handle = NULL;
2888 int ret = 0;
2889 audio_extn_set_snd_card_split(snd_card_name);
2890 snd_split_handle = audio_extn_get_snd_card_split();
2891
2892 snprintf(platform_info_file, MIXER_PATH_MAX_LENGTH, "%s_%s_%s.xml",
2893 PLATFORM_INFO_XML_BASE_STRING, snd_split_handle->snd_card,
2894 snd_split_handle->form_factor);
2895
2896 if (!audio_extn_utils_resolve_config_file(platform_info_file)) {
2897 memset(platform_info_file, 0, MIXER_PATH_MAX_LENGTH);
2898 snprintf(platform_info_file, MIXER_PATH_MAX_LENGTH, "%s_%s.xml",
2899 PLATFORM_INFO_XML_BASE_STRING, snd_split_handle->snd_card);
2900
2901 if (!audio_extn_utils_resolve_config_file(platform_info_file)) {
2902 memset(platform_info_file, 0, MIXER_PATH_MAX_LENGTH);
2903 strlcpy(platform_info_file, PLATFORM_INFO_XML_PATH, MIXER_PATH_MAX_LENGTH);
2904 ret = audio_extn_utils_resolve_config_file(platform_info_file) ? 0 : -1;
2905 }
2906 }
2907
2908 return ret;
2909}
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002910
2911int audio_extn_utils_get_snd_card_num()
2912{
Soumya Managoli9fee7c62018-04-06 16:21:50 +05302913 int snd_card_num = 0;
2914 struct mixer *mixer = NULL;
2915
2916 snd_card_num = audio_extn_utils_open_snd_mixer(&mixer);
2917 if (mixer)
2918 mixer_close(mixer);
2919 return snd_card_num;
2920}
2921
2922int audio_extn_utils_open_snd_mixer(struct mixer **mixer_handle)
2923{
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002924
2925 void *hw_info = NULL;
2926 struct mixer *mixer = NULL;
2927 int retry_num = 0;
mpangfaa7bae2019-01-15 16:38:13 +08002928 int snd_card_num = 0;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002929 char* snd_card_name = NULL;
mpangfaa7bae2019-01-15 16:38:13 +08002930 int snd_card_detection_info[MAX_SND_CARD] = {0};
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002931
Soumya Managoli9fee7c62018-04-06 16:21:50 +05302932 if (!mixer_handle) {
2933 ALOGE("invalid mixer handle");
2934 return -1;
2935 }
2936 *mixer_handle = NULL;
Ashish Jain30ae8942017-11-05 17:21:23 +05302937 /*
mpangfaa7bae2019-01-15 16:38:13 +08002938 * Try with all the sound cards ( 0 to 7 ) and if none of them were detected
Ashish Jain30ae8942017-11-05 17:21:23 +05302939 * sleep for 1 sec and try detections with sound card 0 again.
2940 * If sound card gets detected, check if it is relevant, if not check with the
2941 * other sound cards. To ensure that the irrelevant sound card is not check again,
2942 * we maintain it in min_snd_card_num.
2943 */
2944 while (retry_num < RETRY_NUMBER) {
mpangfaa7bae2019-01-15 16:38:13 +08002945 snd_card_num = 0;
2946 while (snd_card_num < MAX_SND_CARD) {
2947 if (snd_card_detection_info[snd_card_num] == 0) {
2948 mixer = mixer_open(snd_card_num);
2949 if (!mixer)
2950 snd_card_num++;
2951 else
2952 break;
2953 } else
2954 snd_card_num++;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002955 }
2956
2957 if (!mixer) {
Ashish Jain30ae8942017-11-05 17:21:23 +05302958 usleep(RETRY_US);
Ashish Jain30ae8942017-11-05 17:21:23 +05302959 retry_num++;
mpangfaa7bae2019-01-15 16:38:13 +08002960 ALOGD("%s: retry, retry_num %d", __func__, retry_num);
Ashish Jain30ae8942017-11-05 17:21:23 +05302961 continue;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002962 }
2963
2964 snd_card_name = strdup(mixer_get_name(mixer));
2965 if (!snd_card_name) {
2966 ALOGE("failed to allocate memory for snd_card_name\n");
2967 mixer_close(mixer);
2968 return -1;
2969 }
2970 ALOGD("%s: snd_card_name: %s", __func__, snd_card_name);
mpangfaa7bae2019-01-15 16:38:13 +08002971 snd_card_detection_info[snd_card_num] = 1;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002972 hw_info = hw_info_init(snd_card_name);
2973 if (hw_info) {
2974 ALOGD("%s: Opened sound card:%d", __func__, snd_card_num);
2975 break;
2976 }
mpangfaa7bae2019-01-15 16:38:13 +08002977 ALOGE("%s: Failed to init hardware info, snd_card_num:%d", __func__, snd_card_num);
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002978
Dhananjay Kumara7e27832017-07-11 15:40:39 +05302979 free(snd_card_name);
2980 snd_card_name = NULL;
2981
2982 mixer_close(mixer);
2983 mixer = NULL;
2984 }
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002985 if (snd_card_name)
2986 free(snd_card_name);
Ashish Jain30ae8942017-11-05 17:21:23 +05302987
Dhananjay Kumara7e27832017-07-11 15:40:39 +05302988 if (hw_info)
2989 hw_info_deinit(hw_info);
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002990
mpangfaa7bae2019-01-15 16:38:13 +08002991 if (retry_num >= RETRY_NUMBER) {
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002992 ALOGE("%s: Unable to find correct sound card, aborting.", __func__);
2993 return -1;
2994 }
2995
Soumya Managoli9fee7c62018-04-06 16:21:50 +05302996 if (mixer)
2997 *mixer_handle = mixer;
2998
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002999 return snd_card_num;
3000}
Naresh Tanniru6160c712017-04-17 15:43:48 +05303001
Soumya Managoli9fee7c62018-04-06 16:21:50 +05303002void audio_extn_utils_close_snd_mixer(struct mixer *mixer)
3003{
3004 if (mixer)
3005 mixer_close(mixer);
3006}
3007
Naresh Tanniru6160c712017-04-17 15:43:48 +05303008#ifdef SNDRV_COMPRESS_ENABLE_ADJUST_SESSION_CLOCK
3009int audio_extn_utils_compress_enable_drift_correction(
3010 struct stream_out *out,
3011 struct audio_out_enable_drift_correction *drift)
3012{
3013 struct snd_compr_metadata metadata;
3014 int ret = -EINVAL;
3015
3016 if(drift == NULL) {
3017 ALOGE("%s:: Invalid param", __func__);
3018 goto exit;
3019 }
3020
3021 ALOGD("%s:: drift enable %d", __func__,drift->enable);
3022
3023 if (!is_offload_usecase(out->usecase)) {
3024 ALOGE("%s:: not supported for non offload session", __func__);
3025 goto exit;
3026 }
3027
3028 if (!out->compr) {
3029 ALOGW("%s:: offload session not yet opened,"
3030 "start delay will be configure later", __func__);
3031 goto exit;
3032 }
3033
3034 metadata.key = SNDRV_COMPRESS_ENABLE_ADJUST_SESSION_CLOCK;
3035 metadata.value[0] = drift->enable;
3036 out->drift_correction_enabled = drift->enable;
3037
3038 ret = compress_set_metadata(out->compr, &metadata);
3039 if(ret) {
3040 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
3041 out->drift_correction_enabled = false;
3042 }
3043
3044exit:
3045 return ret;
3046}
3047#else
3048int audio_extn_utils_compress_enable_drift_correction(
3049 struct stream_out *out __unused,
3050 struct audio_out_enable_drift_correction *drift __unused)
3051{
3052 ALOGD("%s:: configuring drift enablement not supported", __func__);
3053 return 0;
3054}
3055#endif
3056
3057#ifdef SNDRV_COMPRESS_ADJUST_SESSION_CLOCK
3058int audio_extn_utils_compress_correct_drift(
3059 struct stream_out *out,
3060 struct audio_out_correct_drift *drift_param)
3061{
3062 struct snd_compr_metadata metadata;
3063 int ret = -EINVAL;
3064
3065 if (drift_param == NULL) {
3066 ALOGE("%s:: Invalid drift_param", __func__);
3067 goto exit;
3068 }
3069
3070 ALOGD("%s:: adjust time 0x%"PRIx64" ", __func__,
3071 drift_param->adjust_time);
3072
3073 if (!is_offload_usecase(out->usecase)) {
3074 ALOGE("%s:: not supported for non offload session", __func__);
3075 goto exit;
3076 }
3077
3078 if (!out->compr) {
3079 ALOGW("%s:: offload session not yet opened", __func__);
3080 goto exit;
3081 }
3082
3083 if (!out->drift_correction_enabled) {
3084 ALOGE("%s:: drift correction not enabled", __func__);
3085 goto exit;
3086 }
3087
3088 metadata.key = SNDRV_COMPRESS_ADJUST_SESSION_CLOCK;
3089 metadata.value[0] = 0xFFFFFFFF & drift_param->adjust_time; /* lsb */
3090 metadata.value[1] = \
3091 (0xFFFFFFFF00000000 & drift_param->adjust_time) >> 32; /* msb*/
3092
3093 ret = compress_set_metadata(out->compr, &metadata);
3094 if(ret)
3095 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
3096exit:
3097 return ret;
3098}
3099#else
3100int audio_extn_utils_compress_correct_drift(
3101 struct stream_out *out __unused,
3102 struct audio_out_correct_drift *drift_param __unused)
3103{
3104 ALOGD("%s:: setting adjust clock not supported", __func__);
3105 return 0;
3106}
3107#endif
Naresh Tanniru29bce4e2017-04-27 17:54:30 +05303108
3109int audio_extn_utils_set_channel_map(
3110 struct stream_out *out,
3111 struct audio_out_channel_map_param *channel_map_param)
3112{
3113 int ret = -EINVAL, i = 0;
3114 int channels = audio_channel_count_from_out_mask(out->channel_mask);
3115
3116 if (channel_map_param == NULL) {
3117 ALOGE("%s:: Invalid channel_map", __func__);
3118 goto exit;
3119 }
3120
3121 if (channel_map_param->channels != channels) {
3122 ALOGE("%s:: Channels(%d) does not match stream channels(%d)",
3123 __func__, channel_map_param->channels, channels);
3124 goto exit;
3125 }
3126
3127 for ( i = 0; i < channels; i++) {
3128 ALOGV("%s:: channel_map[%d]- %d", __func__, i, channel_map_param->channel_map[i]);
3129 out->channel_map_param.channel_map[i] = channel_map_param->channel_map[i];
3130 }
3131 ret = 0;
3132exit:
3133 return ret;
3134}
Varun Balaraje49253e2017-07-06 19:48:56 +05303135
3136int audio_extn_utils_set_pan_scale_params(
3137 struct stream_out *out,
3138 struct mix_matrix_params *mm_params)
3139{
3140 int ret = -EINVAL, i = 0, j = 0;
3141
Varun Balaraj003ee752017-08-07 17:54:55 +05303142 if (mm_params == NULL || out == NULL) {
3143 ALOGE("%s:: Invalid mix matrix or out param", __func__);
Varun Balaraje49253e2017-07-06 19:48:56 +05303144 goto exit;
3145 }
3146
3147 if (mm_params->num_output_channels > MAX_CHANNELS_SUPPORTED ||
3148 mm_params->num_output_channels <= 0 ||
3149 mm_params->num_input_channels > MAX_CHANNELS_SUPPORTED ||
3150 mm_params->num_input_channels <= 0)
3151 goto exit;
3152
3153 out->pan_scale_params.num_output_channels = mm_params->num_output_channels;
3154 out->pan_scale_params.num_input_channels = mm_params->num_input_channels;
3155 out->pan_scale_params.has_output_channel_map =
3156 mm_params->has_output_channel_map;
3157 for (i = 0; i < mm_params->num_output_channels; i++)
3158 out->pan_scale_params.output_channel_map[i] =
3159 mm_params->output_channel_map[i];
3160
3161 out->pan_scale_params.has_input_channel_map =
3162 mm_params->has_input_channel_map;
3163 for (i = 0; i < mm_params->num_input_channels; i++)
3164 out->pan_scale_params.input_channel_map[i] =
3165 mm_params->input_channel_map[i];
3166
3167 out->pan_scale_params.has_mixer_coeffs = mm_params->has_mixer_coeffs;
3168 for (i = 0; i < mm_params->num_output_channels; i++)
3169 for (j = 0; j < mm_params->num_input_channels; j++) {
3170 //Convert the channel coefficient gains in Q14 format
3171 out->pan_scale_params.mixer_coeffs[i][j] =
3172 mm_params->mixer_coeffs[i][j] * (2 << 13);
3173 }
3174
3175 ret = platform_set_stream_pan_scale_params(out->dev->platform,
3176 out->pcm_device_id,
3177 out->pan_scale_params);
3178
3179exit:
3180 return ret;
3181}
3182
3183int audio_extn_utils_set_downmix_params(
3184 struct stream_out *out,
3185 struct mix_matrix_params *mm_params)
3186{
3187 int ret = -EINVAL, i = 0, j = 0;
3188 struct audio_usecase *usecase = NULL;
3189
Aniket Kumar Lataf9f246e2017-09-15 15:20:16 -07003190 if (mm_params == NULL || out == NULL) {
Varun Balaraj003ee752017-08-07 17:54:55 +05303191 ALOGE("%s:: Invalid mix matrix or out param", __func__);
Varun Balaraje49253e2017-07-06 19:48:56 +05303192 goto exit;
3193 }
3194
3195 if (mm_params->num_output_channels > MAX_CHANNELS_SUPPORTED ||
3196 mm_params->num_output_channels <= 0 ||
3197 mm_params->num_input_channels > MAX_CHANNELS_SUPPORTED ||
3198 mm_params->num_input_channels <= 0)
3199 goto exit;
3200
3201 usecase = get_usecase_from_list(out->dev, out->usecase);
Varun Balaraj003ee752017-08-07 17:54:55 +05303202 if (!usecase) {
3203 ALOGE("%s: Get usecase list failed!", __func__);
Aniket Kumar Lataf9f246e2017-09-15 15:20:16 -07003204 goto exit;
3205 }
Varun Balaraje49253e2017-07-06 19:48:56 +05303206 out->downmix_params.num_output_channels = mm_params->num_output_channels;
3207 out->downmix_params.num_input_channels = mm_params->num_input_channels;
3208
3209 out->downmix_params.has_output_channel_map =
3210 mm_params->has_output_channel_map;
3211 for (i = 0; i < mm_params->num_output_channels; i++) {
3212 out->downmix_params.output_channel_map[i] =
3213 mm_params->output_channel_map[i];
3214 }
3215
3216 out->downmix_params.has_input_channel_map =
3217 mm_params->has_input_channel_map;
3218 for (i = 0; i < mm_params->num_input_channels; i++)
3219 out->downmix_params.input_channel_map[i] =
3220 mm_params->input_channel_map[i];
3221
3222 out->downmix_params.has_mixer_coeffs = mm_params->has_mixer_coeffs;
3223 for (i = 0; i < mm_params->num_output_channels; i++)
Nikhil Latukar2e6f6242017-08-15 13:37:07 +05303224 for (j = 0; j < mm_params->num_input_channels; j++) {
3225 //Convert the channel coefficient gains in Q14 format
Varun Balaraje49253e2017-07-06 19:48:56 +05303226 out->downmix_params.mixer_coeffs[i][j] =
Nikhil Latukar2e6f6242017-08-15 13:37:07 +05303227 mm_params->mixer_coeffs[i][j] * (2 << 13);
3228 }
Varun Balaraje49253e2017-07-06 19:48:56 +05303229
3230 ret = platform_set_stream_downmix_params(out->dev->platform,
3231 out->pcm_device_id,
3232 usecase->out_snd_device,
3233 out->downmix_params);
3234
3235exit:
3236 return ret;
3237}
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05303238
3239bool audio_extn_utils_is_dolby_format(audio_format_t format)
3240{
3241 if (format == AUDIO_FORMAT_AC3 ||
3242 format == AUDIO_FORMAT_E_AC3 ||
3243 format == AUDIO_FORMAT_E_AC3_JOC)
3244 return true;
3245 else
3246 return false;
3247}
3248
`Deeraj Soman676c2702017-09-18 19:25:53 +05303249int audio_extn_utils_get_bit_width_from_string(const char *id_string)
3250{
3251 int i;
3252 const mixer_config_lookup mixer_bitwidth_config[] = {{"S24_3LE", 24},
3253 {"S32_LE", 32},
3254 {"S24_LE", 24},
3255 {"S16_LE", 16}};
3256 int num_configs = sizeof(mixer_bitwidth_config) / sizeof(mixer_bitwidth_config[0]);
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05303257
`Deeraj Soman676c2702017-09-18 19:25:53 +05303258 for (i = 0; i < num_configs; i++) {
3259 if (!strcmp(id_string, mixer_bitwidth_config[i].id_string))
3260 return mixer_bitwidth_config[i].value;
3261 }
3262
3263 return -EINVAL;
3264}
3265
3266int audio_extn_utils_get_sample_rate_from_string(const char *id_string)
3267{
3268 int i;
Weiyin Jiang30f9ab52020-11-11 11:37:21 +08003269 const mixer_config_lookup mixer_samplerate_config[] = {{"KHZ_8", 8000},
3270 {"KHZ_11P025", 11025},
3271 {"KHZ_16", 16000},
3272 {"KHZ_22P05", 22050},
3273 {"KHZ_32", 32000},
`Deeraj Soman676c2702017-09-18 19:25:53 +05303274 {"KHZ_48", 48000},
3275 {"KHZ_96", 96000},
3276 {"KHZ_144", 144000},
3277 {"KHZ_192", 192000},
3278 {"KHZ_384", 384000},
3279 {"KHZ_44P1", 44100},
3280 {"KHZ_88P2", 88200},
3281 {"KHZ_176P4", 176400},
3282 {"KHZ_352P8", 352800}};
3283 int num_configs = sizeof(mixer_samplerate_config) / sizeof(mixer_samplerate_config[0]);
3284
3285 for (i = 0; i < num_configs; i++) {
3286 if (!strcmp(id_string, mixer_samplerate_config[i].id_string))
3287 return mixer_samplerate_config[i].value;
3288 }
3289
3290 return -EINVAL;
3291}
3292
3293int audio_extn_utils_get_channels_from_string(const char *id_string)
3294{
3295 int i;
3296 const mixer_config_lookup mixer_channels_config[] = {{"One", 1},
3297 {"Two", 2},
3298 {"Three",3},
3299 {"Four", 4},
3300 {"Five", 5},
3301 {"Six", 6},
3302 {"Seven", 7},
3303 {"Eight", 8}};
3304 int num_configs = sizeof(mixer_channels_config) / sizeof(mixer_channels_config[0]);
3305
3306 for (i = 0; i < num_configs; i++) {
3307 if (!strcmp(id_string, mixer_channels_config[i].id_string))
3308 return mixer_channels_config[i].value;
3309 }
3310
3311 return -EINVAL;
3312}
Surendar karka30569792018-05-08 12:02:21 +05303313
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08003314void audio_extn_utils_release_snd_device(snd_device_t snd_device)
3315{
3316 audio_extn_dev_arbi_release(snd_device);
3317 audio_extn_sound_trigger_update_device_status(snd_device,
3318 ST_EVENT_SND_DEVICE_FREE);
3319 audio_extn_listen_update_device_status(snd_device,
3320 LISTEN_EVENT_SND_DEVICE_FREE);
3321}
3322
Weiyin Jiangd5718bd2019-09-04 16:52:08 +08003323int audio_extn_utils_get_license_params(
3324 const struct audio_device *adev,
3325 struct audio_license_params *license_params)
Surendar karka30569792018-05-08 12:02:21 +05303326{
3327 if(!license_params)
3328 return -EINVAL;
Weiyin Jiangd5718bd2019-09-04 16:52:08 +08003329
3330 return platform_get_license_by_product(adev->platform,
3331 (const char*)license_params->product, &license_params->key, license_params->license);
Surendar karka30569792018-05-08 12:02:21 +05303332}
3333
Aalique Grahame22e49102018-12-18 14:23:57 -08003334int audio_extn_utils_send_app_type_gain(struct audio_device *adev,
3335 int app_type,
3336 int *gain)
3337{
3338 int gain_cfg[4];
3339 const char *mixer_ctl_name = "App Type Gain";
3340 struct mixer_ctl *ctl;
3341 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
3342 if (!ctl) {
3343 ALOGE("%s: Could not get volume ctl mixer %s", __func__,
3344 mixer_ctl_name);
3345 return -EINVAL;
3346 }
3347 gain_cfg[0] = 0;
3348 gain_cfg[1] = app_type;
3349 gain_cfg[2] = gain[0];
3350 gain_cfg[3] = gain[1];
3351 ALOGV("%s app_type %d l(%d) r(%d)", __func__, app_type, gain[0], gain[1]);
3352 return mixer_ctl_set_array(ctl, gain_cfg,
3353 sizeof(gain_cfg)/sizeof(gain_cfg[0]));
3354}
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -07003355
Weiyin Jiangd5718bd2019-09-04 16:52:08 +08003356static void vndk_fwk_init()
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -07003357{
Weiyin Jiangd5718bd2019-09-04 16:52:08 +08003358 if (mVndkFwk.lib_handle != NULL)
3359 return;
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -07003360
Weiyin Jiangd5718bd2019-09-04 16:52:08 +08003361 mVndkFwk.lib_handle = dlopen(VNDK_FWK_LIB_PATH, RTLD_NOW);
3362 if (mVndkFwk.lib_handle == NULL) {
3363 ALOGW("%s: failed to dlopen VNDK_FWK_LIB %s", __func__, strerror(errno));
3364 return;
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -07003365 }
3366
Weiyin Jiangd5718bd2019-09-04 16:52:08 +08003367 *(void **)(&mVndkFwk.isVendorEnhancedFwk) =
3368 dlsym(mVndkFwk.lib_handle, "isRunningWithVendorEnhancedFramework");
3369 if (mVndkFwk.isVendorEnhancedFwk == NULL) {
3370 ALOGW("%s: dlsym failed %s", __func__, strerror(errno));
3371 if (mVndkFwk.lib_handle) {
3372 dlclose(mVndkFwk.lib_handle);
3373 mVndkFwk.lib_handle = NULL;
3374 }
3375 return;
3376 }
3377
3378
3379 *(void **)(&mVndkFwk.getVendorEnhancedInfo) =
3380 dlsym(mVndkFwk.lib_handle, "getVendorEnhancedInfo");
3381 if (mVndkFwk.getVendorEnhancedInfo == NULL) {
3382 ALOGW("%s: dlsym failed %s", __func__, strerror(errno));
3383 if (mVndkFwk.lib_handle) {
3384 dlclose(mVndkFwk.lib_handle);
3385 mVndkFwk.lib_handle = NULL;
3386 }
3387 }
3388
3389 return;
3390}
3391
3392bool audio_extn_utils_is_vendor_enhanced_fwk()
3393{
3394 static int is_vendor_enhanced_fwk = -EINVAL;
3395 if (is_vendor_enhanced_fwk != -EINVAL)
3396 return (bool)is_vendor_enhanced_fwk;
3397
3398 vndk_fwk_init();
3399
3400 if (mVndkFwk.isVendorEnhancedFwk != NULL) {
3401 is_vendor_enhanced_fwk = mVndkFwk.isVendorEnhancedFwk();
3402 ALOGW("%s: is_vendor_enhanced_fwk %d", __func__, is_vendor_enhanced_fwk);
3403 } else {
3404 is_vendor_enhanced_fwk = 0;
3405 ALOGW("%s: default to non enhanced_fwk config", __func__);
3406 }
3407
3408 return (bool)is_vendor_enhanced_fwk;
3409}
3410
3411int audio_extn_utils_get_vendor_enhanced_info()
3412{
3413 static int vendor_enhanced_info = -EINVAL;
3414 if (vendor_enhanced_info != -EINVAL)
3415 return vendor_enhanced_info;
3416
3417 vndk_fwk_init();
3418
3419 if (mVndkFwk.getVendorEnhancedInfo != NULL) {
3420 vendor_enhanced_info = mVndkFwk.getVendorEnhancedInfo();
3421 ALOGW("%s: vendor_enhanced_info 0x%x", __func__, vendor_enhanced_info);
3422 } else {
3423 vendor_enhanced_info = 0x0;
3424 ALOGW("%s: default to vendor_enhanced_info 0x0", __func__);
3425 }
3426
3427 return vendor_enhanced_info;
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -07003428}
Deeraj Soman14230922019-01-30 16:39:30 +05303429
Deeraj Somanfa377bf2019-02-06 12:57:59 +05303430int audio_extn_utils_get_perf_mode_flag(void)
3431{
3432#ifdef COMPRESSED_PERF_MODE_FLAG
3433 return COMPRESSED_PERF_MODE_FLAG;
3434#else
3435 return 0;
3436#endif
3437}
3438
Deeraj Soman14230922019-01-30 16:39:30 +05303439size_t audio_extn_utils_get_input_buffer_size(uint32_t sample_rate,
3440 audio_format_t format,
3441 int channel_count,
3442 int64_t duration_ms,
3443 bool is_low_latency)
3444{
3445 size_t size = 0;
3446 size_t capture_duration = AUDIO_CAPTURE_PERIOD_DURATION_MSEC;
3447 uint32_t bytes_per_period_sample = 0;
3448
3449
3450 if (audio_extn_utils_check_input_parameters(sample_rate, format, channel_count) != 0)
3451 return 0;
3452
3453 if (duration_ms >= MIN_OFFLOAD_BUFFER_DURATION_MS && duration_ms <= MAX_OFFLOAD_BUFFER_DURATION_MS)
3454 capture_duration = duration_ms;
3455
3456 size = (sample_rate * capture_duration) / 1000;
3457 if (is_low_latency)
3458 size = LOW_LATENCY_CAPTURE_PERIOD_SIZE;
3459
3460
3461 bytes_per_period_sample = audio_bytes_per_sample(format) * channel_count;
3462 size *= bytes_per_period_sample;
3463
3464 /* make sure the size is multiple of 32 bytes and additionally multiple of
3465 * the frame_size (required for 24bit samples and non-power-of-2 channel counts)
3466 * At 48 kHz mono 16-bit PCM:
3467 * 5.000 ms = 240 frames = 15*16*1*2 = 480, a whole multiple of 32 (15)
3468 * 3.333 ms = 160 frames = 10*16*1*2 = 320, a whole multiple of 32 (10)
3469 *
3470 * The loop reaches result within 32 iterations, as initial size is
3471 * already a multiple of frame_size
3472 */
3473 size = audio_extn_utils_nearest_multiple(size, audio_extn_utils_lcm(32, bytes_per_period_sample));
3474
3475 return size;
3476}
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07003477
3478int audio_extn_utils_hash_fn(void *key)
3479{
3480 return (int)key;
3481}
3482
3483bool audio_extn_utils_hash_eq(void *key1, void *key2)
3484{
3485 return (key1 == key2);
3486}