blob: e1b18dbf492d1ea6fd993f01a33287c96af3dadf [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 &
Kogara Naveen Kumare88ee942022-12-22 17:01:12 +05301370 (audio_output_flags_t)AUDIO_OUTPUT_FLAG_PHONE) || (usecase->stream.out->flags &
1371 (audio_output_flags_t)AUDIO_OUTPUT_FLAG_NAV_GUIDANCE) || (usecase->stream.out->flags &
1372 (audio_output_flags_t)AUDIO_OUTPUT_FLAG_ALERTS)))) {
Kogara Naveen Kumar51bb57d2022-02-17 16:40:06 +05301373 /* Reset to default if no native stream is active or default device is speaker*/
1374 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
1375 }
Zhou Song06761dd2019-04-28 18:08:17 +08001376 }
1377 audio_extn_btsco_get_sample_rate(snd_device, &usecase->stream.out->app_type_cfg.sample_rate);
1378 sample_rate = usecase->stream.out->app_type_cfg.sample_rate;
1379
1380 if (((usecase->stream.out->format == AUDIO_FORMAT_E_AC3) ||
1381 (usecase->stream.out->format == AUDIO_FORMAT_E_AC3_JOC) ||
1382 (usecase->stream.out->format == AUDIO_FORMAT_DOLBY_TRUEHD))
1383 && audio_extn_passthru_is_passthrough_stream(usecase->stream.out)
1384 && !audio_extn_passthru_is_convert_supported(adev, usecase->stream.out)) {
1385 sample_rate = sample_rate * 4;
1386 if (sample_rate > HDMI_PASSTHROUGH_MAX_SAMPLE_RATE)
1387 sample_rate = HDMI_PASSTHROUGH_MAX_SAMPLE_RATE;
1388 }
1389 } else if (usecase->type == PCM_CAPTURE) {
1390 if (usecase->stream.in != NULL) {
Revathi Uddarajud9f23d92020-07-27 10:55:06 +05301391 if (usecase->id == USECASE_AUDIO_RECORD_VOIP
1392 || usecase->id == USECASE_AUDIO_RECORD_VOIP_LOW_LATENCY)
Zhou Song06761dd2019-04-28 18:08:17 +08001393 usecase->stream.in->app_type_cfg.sample_rate = usecase->stream.in->sample_rate;
1394 if (voice_is_in_call_rec_stream(usecase->stream.in)) {
1395 audio_extn_btsco_get_sample_rate(usecase->in_snd_device,
1396 &usecase->stream.in->app_type_cfg.sample_rate);
Zhou Songab94f062021-05-12 13:11:17 +08001397 } if (SND_DEVICE_IN_BT_A2DP == snd_device) {
1398 audio_extn_a2dp_get_dec_sample_rate(&usecase->stream.in->app_type_cfg.sample_rate);
Zhou Song06761dd2019-04-28 18:08:17 +08001399 } else {
1400 audio_extn_btsco_get_sample_rate(snd_device,
1401 &usecase->stream.in->app_type_cfg.sample_rate);
1402 }
1403 sample_rate = usecase->stream.in->app_type_cfg.sample_rate;
1404 } else if (usecase->id == USECASE_AUDIO_SPKR_CALIB_TX) {
Sujin Panickerb6d76262019-09-09 10:55:38 +05301405 if ((property_get("vendor.audio.spkr_prot.tx.sampling_rate", value, NULL) > 0))
1406 sample_rate = atoi(value);
1407 else
1408 sample_rate = SAMPLE_RATE_8000;
Zhou Song06761dd2019-04-28 18:08:17 +08001409 }
Kogara Naveen Kumar51bb57d2022-02-17 16:40:06 +05301410
1411 /* ECNR module in DSP does not support 24 KHz sample rate. As a workaround,
1412 run ADM at 48 KHz when ECNR is enabled in ACDB topology (e.g. device id = 95)
1413 */
Krishna Kishor Jhac1542b22022-08-22 13:12:09 +05301414#ifdef PLATFORM_AUTO
Kogara Naveen Kumar51bb57d2022-02-17 16:40:06 +05301415 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
1416 if (sample_rate == ECNS_UNSUPPORTED_CAPTURE_SAMPLE_RATE_FOR_ADM && acdb_dev_id == ECNS_USE_CASE_ACDB_DEV_ID) {
1417 sample_rate = ECNS_SUPPORTED_CAPTURE_SAMPLE_RATE_FOR_ADM;
1418 ALOGD("%s: update sample rate from 24K to 48K to support ECNR in PCM_CAPTURE, sample_rate=%d",__func__,sample_rate);
1419 }
Krishna Kishor Jhac1542b22022-08-22 13:12:09 +05301420#endif
Zhou Song06761dd2019-04-28 18:08:17 +08001421 } else if (usecase->type == TRANSCODE_LOOPBACK_RX) {
1422 sample_rate = usecase->stream.inout->out_config.sample_rate;
1423 }
1424 return sample_rate;
1425}
1426
Siena Richard7c2db772016-12-21 11:32:34 -08001427static int send_app_type_cfg_for_device(struct audio_device *adev,
1428 struct audio_usecase *usecase,
1429 int split_snd_device)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001430{
1431 char mixer_ctl_name[MAX_LENGTH_MIXER_CONTROL_IN_INT];
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301432 size_t app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT] = {0};
1433 int len = 0, rc;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001434 struct mixer_ctl *ctl;
Siena Richard7c2db772016-12-21 11:32:34 -08001435 int pcm_device_id = 0, acdb_dev_id, app_type;
1436 int snd_device = split_snd_device, snd_device_be_idx = -1;
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +05301437 int32_t sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Varun Balaraje49253e2017-07-06 19:48:56 +05301438 struct streams_io_cfg *s_info = NULL;
1439 struct listnode *node = NULL;
Nikhil Laturkarac4a7492017-08-31 18:27:19 +05301440 int bd_app_type = 0;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001441
Siena Richard7c2db772016-12-21 11:32:34 -08001442 ALOGV("%s: usecase->out_snd_device %s, usecase->in_snd_device %s, split_snd_device %s",
1443 __func__, platform_get_snd_device_name(usecase->out_snd_device),
1444 platform_get_snd_device_name(usecase->in_snd_device),
1445 platform_get_snd_device_name(split_snd_device));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001446
Siddartha Shaik343abc62017-08-08 11:15:25 +05301447 if (usecase->type != PCM_PLAYBACK && usecase->type != PCM_CAPTURE &&
Surendar Karka93cd25a2018-08-28 14:21:37 +05301448 usecase->type != TRANSCODE_LOOPBACK_RX) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301449 ALOGE("%s: not a playback/capture path, no need to cfg app type", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001450 rc = 0;
1451 goto exit_send_app_type_cfg;
1452 }
1453 if ((usecase->id != USECASE_AUDIO_PLAYBACK_DEEP_BUFFER) &&
Vignesh Kulothungan0a2eff02019-07-11 16:30:13 -07001454 (usecase->id != USECASE_AUDIO_PLAYBACK_WITH_HAPTICS) &&
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001455 (usecase->id != USECASE_AUDIO_PLAYBACK_LOW_LATENCY) &&
1456 (usecase->id != USECASE_AUDIO_PLAYBACK_MULTI_CH) &&
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -08001457 (usecase->id != USECASE_AUDIO_PLAYBACK_ULL) &&
Vikram Panduranga93f080e2017-06-07 18:16:14 -07001458 (usecase->id != USECASE_AUDIO_PLAYBACK_VOIP) &&
Surendar Karka93cd25a2018-08-28 14:21:37 +05301459 (usecase->id != USECASE_AUDIO_TRANSCODE_LOOPBACK_RX) &&
Varun Balaraje49253e2017-07-06 19:48:56 +05301460 (!is_interactive_usecase(usecase->id)) &&
Srikanth Uyyala9d551402015-08-25 16:03:42 +05301461 (!is_offload_usecase(usecase->id)) &&
Derek Chenf6318be2017-06-12 17:16:24 -04001462 (usecase->type != PCM_CAPTURE) &&
1463 (!audio_extn_auto_hal_is_bus_device_usecase(usecase->id))) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301464 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 -07001465 rc = 0;
1466 goto exit_send_app_type_cfg;
1467 }
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -08001468
1469 //if VR is active then only send the mixer control
1470 if (usecase->id == USECASE_AUDIO_PLAYBACK_ULL && !audio_is_vr_mode_on(adev)) {
1471 ALOGI("ULL doesnt need sending app type cfg, returning");
1472 rc = 0;
1473 goto exit_send_app_type_cfg;
1474 }
1475
Surendar Karka93cd25a2018-08-28 14:21:37 +05301476 if (usecase->type == PCM_PLAYBACK || usecase->type == TRANSCODE_LOOPBACK_RX) {
Ben Romberger1fafdde2015-09-09 19:43:15 -07001477 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_PLAYBACK);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301478 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
1479 "Audio Stream %d App Type Cfg", pcm_device_id);
Ben Romberger1fafdde2015-09-09 19:43:15 -07001480 } else if (usecase->type == PCM_CAPTURE) {
Ben Romberger1fafdde2015-09-09 19:43:15 -07001481 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_CAPTURE);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301482 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
1483 "Audio Stream Capture %d App Type Cfg", pcm_device_id);
Srikanth Uyyala9d551402015-08-25 16:03:42 +05301484 }
Siena Richard7c2db772016-12-21 11:32:34 -08001485
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001486 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
1487 if (!ctl) {
1488 ALOGE("%s: Could not get ctl for mixer cmd - %s", __func__,
1489 mixer_ctl_name);
1490 rc = -EINVAL;
1491 goto exit_send_app_type_cfg;
1492 }
Aditya Bavanari701a6992017-03-30 19:17:16 +05301493 snd_device = platform_get_spkr_prot_snd_device(snd_device);
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301494 if (voice_is_in_call_rec_stream(usecase->stream.in) && usecase->type == PCM_CAPTURE) {
1495 snd_device_t voice_device = voice_get_incall_rec_snd_device(usecase->in_snd_device);
1496 acdb_dev_id = platform_get_snd_device_acdb_id(voice_device);
1497 ALOGV("acdb id for voice call use case %d", acdb_dev_id);
1498 } else {
1499 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
1500 }
Rohit Kumar1181b292017-01-31 18:07:17 +05301501 if (acdb_dev_id <= 0) {
1502 ALOGE("%s: Couldn't get the acdb dev id", __func__);
1503 rc = -EINVAL;
1504 goto exit_send_app_type_cfg;
1505 }
1506
Siena Richard7c2db772016-12-21 11:32:34 -08001507 snd_device_be_idx = platform_get_snd_device_backend_index(snd_device);
1508 if (snd_device_be_idx < 0) {
1509 ALOGE("%s: Couldn't get the backend index for snd device %s ret=%d",
1510 __func__, platform_get_snd_device_name(snd_device),
1511 snd_device_be_idx);
1512 }
1513
Zhou Song06761dd2019-04-28 18:08:17 +08001514 sample_rate = audio_extn_utils_get_app_sample_rate_for_device(adev, usecase, snd_device);
1515
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301516 if ((usecase->type == PCM_PLAYBACK) && (usecase->stream.out != NULL)) {
Varun Balaraje49253e2017-07-06 19:48:56 +05301517 /* Interactive streams are supported with only direct app type id.
1518 * Get Direct profile app type and use it for interactive streams
1519 */
1520 list_for_each(node, &adev->streams_output_cfg_list) {
1521 s_info = node_to_item(node, struct streams_io_cfg, list);
Aalique Grahame5cd12ff2017-10-19 10:57:00 -07001522 if (s_info->flags.out_flags == (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_BD |
Nikhil Laturkarac4a7492017-08-31 18:27:19 +05301523 AUDIO_OUTPUT_FLAG_DIRECT_PCM |
1524 AUDIO_OUTPUT_FLAG_DIRECT))
1525 bd_app_type = s_info->app_type_cfg.app_type;
Varun Balaraje49253e2017-07-06 19:48:56 +05301526 }
Aalique Grahame5cd12ff2017-10-19 10:57:00 -07001527 if (usecase->stream.out->flags == (audio_output_flags_t)AUDIO_OUTPUT_FLAG_INTERACTIVE)
Nikhil Laturkarac4a7492017-08-31 18:27:19 +05301528 app_type = bd_app_type;
Varun Balaraje49253e2017-07-06 19:48:56 +05301529 else
1530 app_type = usecase->stream.out->app_type_cfg.app_type;
Siena Richard7c2db772016-12-21 11:32:34 -08001531 app_type_cfg[len++] = app_type;
Mingming Yin21854652016-04-13 11:54:02 -07001532 app_type_cfg[len++] = acdb_dev_id;
Naresh Tanniru3a406772017-05-10 13:09:05 -07001533 app_type_cfg[len++] = sample_rate;
1534
Siena Richard7c2db772016-12-21 11:32:34 -08001535 if (snd_device_be_idx > 0)
1536 app_type_cfg[len++] = snd_device_be_idx;
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301537
Siena Richard7c2db772016-12-21 11:32:34 -08001538 ALOGI("%s PLAYBACK app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
1539 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301540
1541 } else if ((usecase->type == PCM_CAPTURE) && (usecase->stream.in != NULL)) {
Siena Richard7c2db772016-12-21 11:32:34 -08001542 app_type = usecase->stream.in->app_type_cfg.app_type;
1543 app_type_cfg[len++] = app_type;
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301544 app_type_cfg[len++] = acdb_dev_id;
Siena Richard7c2db772016-12-21 11:32:34 -08001545 app_type_cfg[len++] = sample_rate;
1546 if (snd_device_be_idx > 0)
1547 app_type_cfg[len++] = snd_device_be_idx;
1548 ALOGI("%s CAPTURE app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
1549 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301550 } else {
Siena Richard7c2db772016-12-21 11:32:34 -08001551 app_type = platform_get_default_app_type_v2(adev->platform, usecase->type);
Surendar Karka93cd25a2018-08-28 14:21:37 +05301552 if(usecase->type == TRANSCODE_LOOPBACK_RX) {
Siddartha Shaik343abc62017-08-08 11:15:25 +05301553 app_type = usecase->stream.inout->out_app_type_cfg.app_type;
1554 }
Siena Richard7c2db772016-12-21 11:32:34 -08001555 app_type_cfg[len++] = app_type;
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301556 app_type_cfg[len++] = acdb_dev_id;
1557 app_type_cfg[len++] = sample_rate;
Siena Richard7c2db772016-12-21 11:32:34 -08001558 if (snd_device_be_idx > 0)
1559 app_type_cfg[len++] = snd_device_be_idx;
1560 ALOGI("%s default app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
1561 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Ashish Jainc02430d2016-01-04 10:42:43 +05301562 }
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301563
Siddartha Shaik343abc62017-08-08 11:15:25 +05301564 if(ctl)
1565 mixer_ctl_set_array(ctl, app_type_cfg, len);
Zhou Songd9bd9302020-08-04 16:34:45 +08001566
1567 /* send app type cfg for haptics */
1568 if (usecase->id == USECASE_AUDIO_PLAYBACK_WITH_HAPTICS) {
1569 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
1570 "Audio Stream %d App Type Cfg", adev->haptic_pcm_device_id );
1571 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
1572 if (!ctl) {
1573 ALOGE("%s: Could not get ctl for mixer cmd - %s", __func__,
1574 mixer_ctl_name);
1575 rc = -EINVAL;
1576 goto exit_send_app_type_cfg;
1577 }
1578 acdb_dev_id = platform_get_snd_device_acdb_id(SND_DEVICE_OUT_HAPTICS);
1579 snd_device_be_idx = platform_get_snd_device_backend_index(SND_DEVICE_OUT_HAPTICS);
1580 /* haptics acdb id */
1581 app_type_cfg[1] = acdb_dev_id;
1582 /* haptics be index */
1583 app_type_cfg[3] = snd_device_be_idx;
1584 mixer_ctl_set_array(ctl, app_type_cfg, len);
1585 }
1586
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001587 rc = 0;
1588exit_send_app_type_cfg:
1589 return rc;
1590}
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001591
Deeraj Soman14230922019-01-30 16:39:30 +05301592static int audio_extn_utils_check_input_parameters(uint32_t sample_rate,
1593 audio_format_t format,
1594 int channel_count)
1595{
1596 int ret = 0;
1597
1598 if (((format != AUDIO_FORMAT_PCM_16_BIT) && (format != AUDIO_FORMAT_PCM_8_24_BIT) &&
1599 (format != AUDIO_FORMAT_PCM_24_BIT_PACKED) && (format != AUDIO_FORMAT_PCM_32_BIT) &&
1600 (format != AUDIO_FORMAT_PCM_FLOAT)) &&
1601 !voice_extn_compress_voip_is_format_supported(format) &&
1602 !audio_extn_compr_cap_format_supported(format) &&
1603 !audio_extn_cin_format_supported(format))
1604 ret = -EINVAL;
1605
1606 switch (channel_count) {
1607 case 1:
1608 case 2:
1609 case 3:
1610 case 4:
1611 case 6:
1612 case 8:
1613 break;
1614 default:
1615 ret = -EINVAL;
1616 }
1617
1618 switch (sample_rate) {
1619 case 8000:
1620 case 11025:
1621 case 12000:
1622 case 16000:
1623 case 22050:
1624 case 24000:
1625 case 32000:
1626 case 44100:
1627 case 48000:
1628 case 88200:
1629 case 96000:
1630 case 176400:
1631 case 192000:
1632 break;
1633 default:
1634 ret = -EINVAL;
1635 }
1636
1637 return ret;
1638}
1639
1640static inline uint32_t audio_extn_utils_nearest_multiple(uint32_t num, uint32_t multiplier)
1641{
1642 uint32_t remainder = 0;
1643
1644 if (!multiplier)
1645 return num;
1646
1647 remainder = num % multiplier;
1648 if (remainder)
1649 num += (multiplier - remainder);
1650
1651 return num;
1652}
1653
1654static inline uint32_t audio_extn_utils_lcm(uint32_t num1, uint32_t num2)
1655{
1656 uint32_t high = num1, low = num2, temp = 0;
1657
1658 if (!num1 || !num2)
1659 return 0;
1660
1661 if (num1 < num2) {
1662 high = num2;
1663 low = num1;
1664 }
1665
1666 while (low != 0) {
1667 temp = low;
1668 low = high % low;
1669 high = temp;
1670 }
1671 return (num1 * num2)/high;
1672}
1673
Siena Richard7c2db772016-12-21 11:32:34 -08001674int audio_extn_utils_send_app_type_cfg(struct audio_device *adev,
1675 struct audio_usecase *usecase)
1676{
1677 int i, num_devices = 0;
1678 snd_device_t new_snd_devices[SND_DEVICE_OUT_END] = {0};
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301679 snd_device_t in_snd_device = usecase->in_snd_device;
Siena Richard7c2db772016-12-21 11:32:34 -08001680 int rc = 0;
1681
Aalique Grahame22e49102018-12-18 14:23:57 -08001682 if (usecase->type == PCM_HFP_CALL) {
1683 return audio_extn_utils_send_app_type_cfg_hfp(adev, usecase);
Derek Chena30a5f42019-12-03 11:17:09 -05001684 } else if (usecase->type == ICC_CALL) {
1685 return audio_extn_utils_send_app_type_cfg_icc(adev, usecase);
Fei Tongaffdf732020-02-20 20:39:05 +08001686 } else if (usecase->type == SYNTH_LOOPBACK) {
1687 return audio_extn_utils_send_app_type_cfg_synth(adev, usecase);
Aalique Grahame22e49102018-12-18 14:23:57 -08001688 }
1689
Siena Richard7c2db772016-12-21 11:32:34 -08001690 switch (usecase->type) {
1691 case PCM_PLAYBACK:
Surendar Karka93cd25a2018-08-28 14:21:37 +05301692 case TRANSCODE_LOOPBACK_RX:
Siena Richard7c2db772016-12-21 11:32:34 -08001693 ALOGD("%s: usecase->out_snd_device %s",
1694 __func__, platform_get_snd_device_name(usecase->out_snd_device));
1695 /* check for out combo device */
1696 if (platform_split_snd_device(adev->platform,
1697 usecase->out_snd_device,
1698 &num_devices, new_snd_devices)) {
1699 new_snd_devices[0] = usecase->out_snd_device;
1700 num_devices = 1;
1701 }
1702 break;
1703 case PCM_CAPTURE:
1704 ALOGD("%s: usecase->in_snd_device %s",
1705 __func__, platform_get_snd_device_name(usecase->in_snd_device));
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301706 if (voice_is_in_call_rec_stream(usecase->stream.in)) {
1707 in_snd_device = voice_get_incall_rec_backend_device(usecase->stream.in);
1708 }
Siena Richard7c2db772016-12-21 11:32:34 -08001709 /* check for in combo device */
1710 if (platform_split_snd_device(adev->platform,
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301711 in_snd_device,
Siena Richard7c2db772016-12-21 11:32:34 -08001712 &num_devices, new_snd_devices)) {
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301713 new_snd_devices[0] = in_snd_device;
Siena Richard7c2db772016-12-21 11:32:34 -08001714 num_devices = 1;
1715 }
1716 break;
Sriram Kumar9c836482020-10-27 20:34:35 +05301717 case PCM_HFP_CALL:
1718 rc = audio_extn_utils_send_app_type_cfg_hfp(adev,usecase);
1719 return rc;
Siena Richard7c2db772016-12-21 11:32:34 -08001720 default:
1721 ALOGI("%s: not a playback/capture path, no need to cfg app type", __func__);
1722 rc = 0;
1723 break;
1724 }
1725
1726 for (i = 0; i < num_devices; i++) {
1727 rc = send_app_type_cfg_for_device(adev, usecase, new_snd_devices[i]);
1728 if (rc)
1729 break;
1730 }
1731
1732 return rc;
1733}
1734
Preetam Singh Ranawat9519e9c2015-11-18 16:05:55 +05301735int read_line_from_file(const char *path, char *buf, size_t count)
1736{
1737 char * fgets_ret;
1738 FILE * fd;
1739 int rv;
1740
1741 fd = fopen(path, "r");
1742 if (fd == NULL)
1743 return -1;
1744
1745 fgets_ret = fgets(buf, (int)count, fd);
1746 if (NULL != fgets_ret) {
1747 rv = (int)strlen(buf);
1748 } else {
1749 rv = ferror(fd);
1750 }
1751 fclose(fd);
1752
1753 return rv;
1754}
1755
Ashish Jainf1eaa582016-05-23 20:54:24 +05301756/*Translates ALSA formats to AOSP PCM formats*/
1757audio_format_t alsa_format_to_hal(uint32_t alsa_format)
1758{
1759 audio_format_t format;
1760
1761 switch(alsa_format) {
1762 case SNDRV_PCM_FORMAT_S16_LE:
1763 format = AUDIO_FORMAT_PCM_16_BIT;
1764 break;
1765 case SNDRV_PCM_FORMAT_S24_3LE:
1766 format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
1767 break;
1768 case SNDRV_PCM_FORMAT_S24_LE:
1769 format = AUDIO_FORMAT_PCM_8_24_BIT;
1770 break;
1771 case SNDRV_PCM_FORMAT_S32_LE:
1772 format = AUDIO_FORMAT_PCM_32_BIT;
1773 break;
1774 default:
1775 ALOGW("Incorrect ALSA format");
1776 format = AUDIO_FORMAT_INVALID;
1777 }
1778 return format;
1779}
1780
1781/*Translates hal format (AOSP) to alsa formats*/
1782uint32_t hal_format_to_alsa(audio_format_t hal_format)
1783{
1784 uint32_t alsa_format;
1785
1786 switch (hal_format) {
1787 case AUDIO_FORMAT_PCM_32_BIT: {
1788 if (platform_supports_true_32bit())
1789 alsa_format = SNDRV_PCM_FORMAT_S32_LE;
1790 else
1791 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1792 }
1793 break;
1794 case AUDIO_FORMAT_PCM_8_BIT:
1795 alsa_format = SNDRV_PCM_FORMAT_S8;
1796 break;
1797 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
1798 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1799 break;
1800 case AUDIO_FORMAT_PCM_8_24_BIT: {
1801 if (platform_supports_true_32bit())
1802 alsa_format = SNDRV_PCM_FORMAT_S32_LE;
1803 else
1804 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1805 }
1806 break;
1807 case AUDIO_FORMAT_PCM_FLOAT:
1808 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1809 break;
1810 default:
1811 case AUDIO_FORMAT_PCM_16_BIT:
1812 alsa_format = SNDRV_PCM_FORMAT_S16_LE;
1813 break;
1814 }
1815 return alsa_format;
1816}
1817
Ashish Jain83a6cc22016-06-28 14:34:17 +05301818/*Translates PCM formats to AOSP formats*/
1819audio_format_t pcm_format_to_hal(uint32_t pcm_format)
1820{
1821 audio_format_t format = AUDIO_FORMAT_INVALID;
1822
1823 switch(pcm_format) {
1824 case PCM_FORMAT_S16_LE:
1825 format = AUDIO_FORMAT_PCM_16_BIT;
1826 break;
1827 case PCM_FORMAT_S24_3LE:
1828 format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
1829 break;
1830 case PCM_FORMAT_S24_LE:
1831 format = AUDIO_FORMAT_PCM_8_24_BIT;
1832 break;
1833 case PCM_FORMAT_S32_LE:
1834 format = AUDIO_FORMAT_PCM_32_BIT;
1835 break;
1836 default:
1837 ALOGW("Incorrect PCM format");
1838 format = AUDIO_FORMAT_INVALID;
1839 }
1840 return format;
1841}
1842
1843/*Translates hal format (AOSP) to alsa formats*/
1844uint32_t hal_format_to_pcm(audio_format_t hal_format)
1845{
1846 uint32_t pcm_format;
1847
1848 switch (hal_format) {
1849 case AUDIO_FORMAT_PCM_32_BIT:
1850 case AUDIO_FORMAT_PCM_8_24_BIT:
1851 case AUDIO_FORMAT_PCM_FLOAT: {
1852 if (platform_supports_true_32bit())
1853 pcm_format = PCM_FORMAT_S32_LE;
1854 else
1855 pcm_format = PCM_FORMAT_S24_3LE;
1856 }
1857 break;
1858 case AUDIO_FORMAT_PCM_8_BIT:
1859 pcm_format = PCM_FORMAT_S8;
1860 break;
1861 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
1862 pcm_format = PCM_FORMAT_S24_3LE;
1863 break;
1864 default:
1865 case AUDIO_FORMAT_PCM_16_BIT:
1866 pcm_format = PCM_FORMAT_S16_LE;
1867 break;
1868 }
1869 return pcm_format;
1870}
1871
Ashish Jainf1eaa582016-05-23 20:54:24 +05301872uint32_t get_alsa_fragment_size(uint32_t bytes_per_sample,
1873 uint32_t sample_rate,
Deeraj Soman65358ab2019-02-07 15:40:49 +05301874 uint32_t noOfChannels,
1875 int64_t duration_ms)
Ashish Jainf1eaa582016-05-23 20:54:24 +05301876{
1877 uint32_t fragment_size = 0;
1878 uint32_t pcm_offload_time = PCM_OFFLOAD_BUFFER_DURATION;
1879
Deeraj Soman65358ab2019-02-07 15:40:49 +05301880 if (duration_ms >= MIN_OFFLOAD_BUFFER_DURATION_MS && duration_ms <= MAX_OFFLOAD_BUFFER_DURATION_MS)
1881 pcm_offload_time = duration_ms;
1882
Ashish Jainf1eaa582016-05-23 20:54:24 +05301883 fragment_size = (pcm_offload_time
1884 * sample_rate
1885 * bytes_per_sample
1886 * noOfChannels)/1000;
1887 if (fragment_size < MIN_PCM_OFFLOAD_FRAGMENT_SIZE)
1888 fragment_size = MIN_PCM_OFFLOAD_FRAGMENT_SIZE;
1889 else if (fragment_size > MAX_PCM_OFFLOAD_FRAGMENT_SIZE)
1890 fragment_size = MAX_PCM_OFFLOAD_FRAGMENT_SIZE;
1891 /*To have same PCM samples for all channels, the buffer size requires to
1892 *be multiple of (number of channels * bytes per sample)
1893 *For writes to succeed, the buffer must be written at address which is multiple of 32
1894 */
Aalique Grahameb85f2d92017-10-16 17:53:23 -07001895 fragment_size = ALIGN(fragment_size, (bytes_per_sample * noOfChannels * 32));
Ashish Jainf1eaa582016-05-23 20:54:24 +05301896
1897 ALOGI("PCM offload Fragment size to %d bytes", fragment_size);
1898 return fragment_size;
1899}
1900
1901/* Calculates the fragment size required to configure compress session.
1902 * Based on the alsa format selected, decide if conversion is needed in
1903
1904 * HAL ( e.g. convert AUDIO_FORMAT_PCM_FLOAT input format to
1905 * AUDIO_FORMAT_PCM_24_BIT_PACKED before writing to the compress driver.
1906 */
1907void audio_extn_utils_update_direct_pcm_fragment_size(struct stream_out *out)
1908{
Ashish Jain83a6cc22016-06-28 14:34:17 +05301909 audio_format_t dst_format = out->hal_op_format;
1910 audio_format_t src_format = out->hal_ip_format;
Ashish Jainf1eaa582016-05-23 20:54:24 +05301911 uint32_t hal_op_bytes_per_sample = audio_bytes_per_sample(dst_format);
1912 uint32_t hal_ip_bytes_per_sample = audio_bytes_per_sample(src_format);
1913
1914 out->compr_config.fragment_size =
1915 get_alsa_fragment_size(hal_op_bytes_per_sample,
1916 out->sample_rate,
Deeraj Soman65358ab2019-02-07 15:40:49 +05301917 popcount(out->channel_mask),
1918 out->info.duration_us / 1000);
Ashish Jainf1eaa582016-05-23 20:54:24 +05301919
1920 if ((src_format != dst_format) &&
1921 hal_op_bytes_per_sample != hal_ip_bytes_per_sample) {
1922
Ashish Jain83a6cc22016-06-28 14:34:17 +05301923 out->hal_fragment_size =
Ashish Jainf1eaa582016-05-23 20:54:24 +05301924 ((out->compr_config.fragment_size * hal_ip_bytes_per_sample) /
1925 hal_op_bytes_per_sample);
1926 ALOGI("enable conversion hal_input_fragment_size is %d src_format %x dst_format %x",
Ashish Jain83a6cc22016-06-28 14:34:17 +05301927 out->hal_fragment_size, src_format, dst_format);
Ashish Jainf1eaa582016-05-23 20:54:24 +05301928 } else {
Ashish Jain83a6cc22016-06-28 14:34:17 +05301929 out->hal_fragment_size = out->compr_config.fragment_size;
Ashish Jainf1eaa582016-05-23 20:54:24 +05301930 }
1931}
1932
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301933/* converts pcm format 24_8 to 8_24 inplace */
1934size_t audio_extn_utils_convert_format_24_8_to_8_24(void *buf, size_t bytes)
1935{
1936 size_t i = 0;
1937 int *int_buf_stream = buf;
1938
1939 if ((bytes % 4) != 0) {
1940 ALOGE("%s: wrong inout buffer! ... is not 32 bit aligned ", __func__);
1941 return -EINVAL;
1942 }
1943
1944 for (; i < (bytes / 4); i++)
1945 int_buf_stream[i] >>= 8;
1946
1947 return bytes;
1948}
1949
Xiaojun Sang782e5b12020-06-29 21:13:06 +08001950#ifdef AUDIO_GKI_ENABLED
1951int get_snd_codec_id(audio_format_t format)
1952{
1953 int id = 0;
1954
1955 switch (format & AUDIO_FORMAT_MAIN_MASK) {
1956 case AUDIO_FORMAT_MP3:
1957 id = SND_AUDIOCODEC_MP3;
1958 break;
1959 case AUDIO_FORMAT_AAC:
1960 id = SND_AUDIOCODEC_AAC;
1961 break;
1962 case AUDIO_FORMAT_AAC_ADTS:
1963 id = SND_AUDIOCODEC_AAC;
1964 break;
1965 case AUDIO_FORMAT_AAC_LATM:
1966 id = SND_AUDIOCODEC_AAC;
1967 break;
1968 case AUDIO_FORMAT_PCM:
1969 id = SND_AUDIOCODEC_PCM;
1970 break;
1971 case AUDIO_FORMAT_FLAC:
1972 case AUDIO_FORMAT_ALAC:
1973 case AUDIO_FORMAT_APE:
1974 case AUDIO_FORMAT_VORBIS:
1975 case AUDIO_FORMAT_WMA:
1976 case AUDIO_FORMAT_WMA_PRO:
1977 case AUDIO_FORMAT_DSD:
1978 case AUDIO_FORMAT_APTX:
1979 id = SND_AUDIOCODEC_BESPOKE;
1980 break;
1981 case AUDIO_FORMAT_MP2:
1982 id = SND_AUDIOCODEC_MP2;
1983 break;
1984 case AUDIO_FORMAT_AC3:
1985 id = SND_AUDIOCODEC_AC3;
1986 break;
1987 case AUDIO_FORMAT_E_AC3:
1988 case AUDIO_FORMAT_E_AC3_JOC:
1989 id = SND_AUDIOCODEC_EAC3;
1990 break;
1991 case AUDIO_FORMAT_DTS:
1992 case AUDIO_FORMAT_DTS_HD:
1993 id = SND_AUDIOCODEC_DTS;
1994 break;
1995 case AUDIO_FORMAT_DOLBY_TRUEHD:
1996 id = SND_AUDIOCODEC_TRUEHD;
1997 break;
1998 case AUDIO_FORMAT_IEC61937:
1999 id = SND_AUDIOCODEC_IEC61937;
2000 break;
2001 default:
2002 ALOGE("%s: Unsupported audio format :%x", __func__, format);
2003 }
2004
2005 return id;
2006}
2007#else
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05302008int get_snd_codec_id(audio_format_t format)
2009{
2010 int id = 0;
2011
2012 switch (format & AUDIO_FORMAT_MAIN_MASK) {
2013 case AUDIO_FORMAT_MP3:
2014 id = SND_AUDIOCODEC_MP3;
2015 break;
2016 case AUDIO_FORMAT_AAC:
2017 id = SND_AUDIOCODEC_AAC;
2018 break;
2019 case AUDIO_FORMAT_AAC_ADTS:
2020 id = SND_AUDIOCODEC_AAC;
2021 break;
Arun Kumar Dasari3b174182016-12-27 13:01:14 +05302022 case AUDIO_FORMAT_AAC_LATM:
2023 id = SND_AUDIOCODEC_AAC;
2024 break;
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05302025 case AUDIO_FORMAT_PCM:
2026 id = SND_AUDIOCODEC_PCM;
2027 break;
2028 case AUDIO_FORMAT_FLAC:
2029 id = SND_AUDIOCODEC_FLAC;
2030 break;
2031 case AUDIO_FORMAT_ALAC:
2032 id = SND_AUDIOCODEC_ALAC;
2033 break;
2034 case AUDIO_FORMAT_APE:
2035 id = SND_AUDIOCODEC_APE;
2036 break;
2037 case AUDIO_FORMAT_VORBIS:
2038 id = SND_AUDIOCODEC_VORBIS;
2039 break;
2040 case AUDIO_FORMAT_WMA:
2041 id = SND_AUDIOCODEC_WMA;
2042 break;
Dechen Chai4a575252021-07-30 09:21:28 +05302043#ifndef AUDIO_DISABLE_COMPRESS_FORMAT
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05302044 case AUDIO_FORMAT_WMA_PRO:
2045 id = SND_AUDIOCODEC_WMA_PRO;
2046 break;
Dechen Chai4a575252021-07-30 09:21:28 +05302047#endif
Satish Babu Patakokila0c313922016-12-08 12:07:08 +05302048 case AUDIO_FORMAT_MP2:
2049 id = SND_AUDIOCODEC_MP2;
2050 break;
Satish Babu Patakokila915ecba2017-01-10 17:43:56 +05302051 case AUDIO_FORMAT_AC3:
2052 id = SND_AUDIOCODEC_AC3;
2053 break;
2054 case AUDIO_FORMAT_E_AC3:
2055 case AUDIO_FORMAT_E_AC3_JOC:
2056 id = SND_AUDIOCODEC_EAC3;
2057 break;
2058 case AUDIO_FORMAT_DTS:
2059 case AUDIO_FORMAT_DTS_HD:
2060 id = SND_AUDIOCODEC_DTS;
2061 break;
Ben Romberger1aaaf862017-04-06 17:49:46 -07002062 case AUDIO_FORMAT_DOLBY_TRUEHD:
2063 id = SND_AUDIOCODEC_TRUEHD;
2064 break;
Naresh Tanniru928f0862017-04-07 16:44:23 -07002065 case AUDIO_FORMAT_IEC61937:
2066 id = SND_AUDIOCODEC_IEC61937;
2067 break;
Dechen Chai4a575252021-07-30 09:21:28 +05302068#ifndef AUDIO_DISABLE_COMPRESS_FORMAT
Preetam Singh Ranawat4277a5a2017-01-18 19:02:24 +05302069 case AUDIO_FORMAT_DSD:
2070 id = SND_AUDIOCODEC_DSD;
2071 break;
Dhanalakshmi Siddani18737932016-11-29 17:33:17 +05302072 case AUDIO_FORMAT_APTX:
2073 id = SND_AUDIOCODEC_APTX;
2074 break;
Dechen Chai4a575252021-07-30 09:21:28 +05302075#endif
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05302076 default:
2077 ALOGE("%s: Unsupported audio format :%x", __func__, format);
2078 }
2079
2080 return id;
2081}
Xiaojun Sang782e5b12020-06-29 21:13:06 +08002082#endif
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05302083
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07002084void audio_extn_utils_send_audio_calibration(struct audio_device *adev,
2085 struct audio_usecase *usecase)
2086{
2087 int type = usecase->type;
2088
Dhananjay Kumar14245982017-01-16 20:21:00 +05302089 if (type == PCM_PLAYBACK && usecase->stream.out != NULL) {
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05302090 platform_send_audio_calibration(adev->platform, usecase,
Zhou Song06761dd2019-04-28 18:08:17 +08002091 usecase->stream.out->app_type_cfg.app_type);
Dhananjay Kumar14245982017-01-16 20:21:00 +05302092 } else if (type == PCM_CAPTURE && usecase->stream.in != NULL) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +05302093 platform_send_audio_calibration(adev->platform, usecase,
Zhou Song06761dd2019-04-28 18:08:17 +08002094 usecase->stream.in->app_type_cfg.app_type);
Sriram Kumar9c836482020-10-27 20:34:35 +05302095 } else if (type == PCM_HFP_CALL) {
2096 /* when app type is default. the sample rate is not used to send cal */
2097#ifdef ENABLE_HFP_CALIBRATION
2098 platform_send_audio_calibration_hfp(adev->platform, usecase->in_snd_device);
2099#else
2100 platform_send_audio_calibration(adev->platform, usecase,
2101 platform_get_default_app_type_v2(adev->platform, usecase->type));
2102#endif
2103 } else if ((type == PCM_CAPTURE) ||
Derek Chena30a5f42019-12-03 11:17:09 -05002104 (type == TRANSCODE_LOOPBACK_RX && usecase->stream.inout != NULL) ||
Fei Tongaffdf732020-02-20 20:39:05 +08002105 (type == ICC_CALL) || (type == SYNTH_LOOPBACK)) {
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05302106 platform_send_audio_calibration(adev->platform, usecase,
Zhou Song06761dd2019-04-28 18:08:17 +08002107 platform_get_default_app_type_v2(adev->platform, usecase->type));
Vidyakumar Athotae57f6002017-03-01 13:13:16 -08002108 } else {
2109 /* No need to send audio calibration for voice and voip call usecases */
2110 if ((type != VOICE_CALL) && (type != VOIP_CALL))
2111 ALOGW("%s: No audio calibration for usecase type = %d", __func__, type);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07002112 }
2113}
2114
Ben Rombergera04fabc2014-11-14 12:16:03 -08002115// Base64 Encode and Decode
2116// Not all features supported. This must be used only with following conditions.
2117// Decode Modes: Support with and without padding
2118// CRLF not handling. So no CRLF in string to decode.
2119// Encode Modes: Supports only padding
2120int b64decode(char *inp, int ilen, uint8_t* outp)
2121{
2122 int i, j, k, ii, num;
2123 int rem, pcnt;
2124 uint32_t res=0;
2125 uint8_t getIndex[MAX_BASEINDEX_LEN];
2126 uint8_t tmp, cflag;
2127
2128 if(inp == NULL || outp == NULL || ilen <= 0) {
2129 ALOGE("[%s] received NULL pointer or zero length",__func__);
2130 return -1;
2131 }
2132
2133 memset(getIndex, MAX_BASEINDEX_LEN-1, sizeof(getIndex));
2134 for(i=0;i<BASE_TABLE_SIZE;i++) {
2135 getIndex[(uint8_t)bTable[i]] = (uint8_t)i;
2136 }
2137 getIndex[(uint8_t)'=']=0;
2138
2139 j=0;k=0;
2140 num = ilen/4;
2141 rem = ilen%4;
2142 if(rem==0)
2143 num = num-1;
2144 cflag=0;
2145 for(i=0; i<num; i++) {
2146 res=0;
2147 for(ii=0;ii<4;ii++) {
2148 res = res << 6;
2149 tmp = getIndex[(uint8_t)inp[j++]];
2150 res = res | tmp;
2151 cflag = cflag | tmp;
2152 }
2153 outp[k++] = (res >> 16)&0xFF;
2154 outp[k++] = (res >> 8)&0xFF;
2155 outp[k++] = res & 0xFF;
2156 }
2157
2158 // Handle last bytes special
2159 pcnt=0;
2160 if(rem == 0) {
2161 //With padding or full data
2162 res = 0;
2163 for(ii=0;ii<4;ii++) {
2164 if(inp[j] == '=')
2165 pcnt++;
2166 res = res << 6;
2167 tmp = getIndex[(uint8_t)inp[j++]];
2168 res = res | tmp;
2169 cflag = cflag | tmp;
2170 }
2171 outp[k++] = res >> 16;
2172 if(pcnt == 2)
2173 goto done;
2174 outp[k++] = (res>>8)&0xFF;
2175 if(pcnt == 1)
2176 goto done;
2177 outp[k++] = res&0xFF;
2178 } else {
2179 //without padding
2180 res = 0;
2181 for(i=0;i<rem;i++) {
2182 res = res << 6;
2183 tmp = getIndex[(uint8_t)inp[j++]];
2184 res = res | tmp;
2185 cflag = cflag | tmp;
2186 }
2187 for(i=rem;i<4;i++) {
2188 res = res << 6;
2189 pcnt++;
2190 }
2191 outp[k++] = res >> 16;
2192 if(pcnt == 2)
2193 goto done;
2194 outp[k++] = (res>>8)&0xFF;
2195 if(pcnt == 1)
2196 goto done;
2197 outp[k++] = res&0xFF;
2198 }
2199done:
2200 if(cflag == 0xFF) {
2201 ALOGE("[%s] base64 decode failed. Invalid character found %s",
2202 __func__, inp);
2203 return 0;
2204 }
2205 return k;
2206}
2207
2208int b64encode(uint8_t *inp, int ilen, char* outp)
2209{
2210 int i,j,k, num;
2211 int rem=0;
2212 uint32_t res=0;
2213
2214 if(inp == NULL || outp == NULL || ilen<=0) {
2215 ALOGE("[%s] received NULL pointer or zero input length",__func__);
2216 return -1;
2217 }
2218
2219 num = ilen/3;
2220 rem = ilen%3;
2221 j=0;k=0;
2222 for(i=0; i<num; i++) {
2223 //prepare index
2224 res = inp[j++]<<16;
2225 res = res | inp[j++]<<8;
2226 res = res | inp[j++];
2227 //get output map from index
2228 outp[k++] = (char) bTable[(res>>18)&0x3F];
2229 outp[k++] = (char) bTable[(res>>12)&0x3F];
2230 outp[k++] = (char) bTable[(res>>6)&0x3F];
2231 outp[k++] = (char) bTable[res&0x3F];
2232 }
2233
2234 switch(rem) {
2235 case 1:
2236 res = inp[j++]<<16;
2237 outp[k++] = (char) bTable[res>>18];
2238 outp[k++] = (char) bTable[(res>>12)&0x3F];
2239 //outp[k++] = '=';
2240 //outp[k++] = '=';
2241 break;
2242 case 2:
2243 res = inp[j++]<<16;
2244 res = res | inp[j++]<<8;
2245 outp[k++] = (char) bTable[res>>18];
2246 outp[k++] = (char) bTable[(res>>12)&0x3F];
2247 outp[k++] = (char) bTable[(res>>6)&0x3F];
2248 //outp[k++] = '=';
2249 break;
2250 default:
2251 break;
2252 }
Ben Rombergera04fabc2014-11-14 12:16:03 -08002253 outp[k] = '\0';
2254 return k;
2255}
Ashish Jain81eb2a82015-05-13 10:52:34 +05302256
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05302257
2258int audio_extn_utils_get_codec_version(const char *snd_card_name,
2259 int card_num,
2260 char *codec_version)
2261{
2262 char procfs_path[50];
2263 FILE *fp;
2264
2265 if (strstr(snd_card_name, "tasha")) {
2266 snprintf(procfs_path, sizeof(procfs_path),
2267 "/proc/asound/card%d/codecs/tasha/version", card_num);
2268 if ((fp = fopen(procfs_path, "r")) != NULL) {
2269 fgets(codec_version, CODEC_VERSION_MAX_LENGTH, fp);
2270 fclose(fp);
2271 } else {
2272 ALOGE("%s: ERROR. cannot open %s", __func__, procfs_path);
2273 return -ENOENT;
2274 }
2275 ALOGD("%s: codec version %s", __func__, codec_version);
2276 }
2277
2278 return 0;
2279}
2280
Preetam Singh Ranawat9d0d8e42019-07-15 12:27:25 +05302281int audio_extn_utils_get_codec_variant(int card_num,
2282 char *codec_variant)
2283{
2284 char procfs_path[50];
2285 FILE *fp;
2286 snprintf(procfs_path, sizeof(procfs_path),
2287 "/proc/asound/card%d/codecs/wcd938x/variant", card_num);
2288 if ((fp = fopen(procfs_path, "r")) == NULL) {
2289 snprintf(procfs_path, sizeof(procfs_path),
2290 "/proc/asound/card%d/codecs/wcd937x/variant", card_num);
2291 if ((fp = fopen(procfs_path, "r")) == NULL) {
2292 ALOGE("%s: ERROR. cannot open %s", __func__, procfs_path);
2293 return -ENOENT;
2294 }
2295 }
2296 fgets(codec_variant, CODEC_VARIANT_MAX_LENGTH, fp);
2297 fclose(fp);
2298 ALOGD("%s: codec variant is %s", __func__, codec_variant);
2299 return 0;
2300}
2301
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05302302
Ashish Jain81eb2a82015-05-13 10:52:34 +05302303#ifdef AUDIO_EXTERNAL_HDMI_ENABLED
2304
2305void get_default_compressed_channel_status(
2306 unsigned char *channel_status)
2307{
Ashish Jain81eb2a82015-05-13 10:52:34 +05302308 memset(channel_status,0,24);
2309
2310 /* block start bit in preamble bit 3 */
2311 channel_status[0] |= PROFESSIONAL;
2312 //compre out
2313 channel_status[0] |= NON_LPCM;
2314 // sample rate; fixed 48K for default/transcode
2315 channel_status[3] |= SR_48000;
2316}
2317
Ashish Jain81eb2a82015-05-13 10:52:34 +05302318int32_t get_compressed_channel_status(void *audio_stream_data,
2319 uint32_t audio_frame_size,
2320 unsigned char *channel_status,
2321 enum audio_parser_code_type codec_type)
2322 // codec_type - AUDIO_PARSER_CODEC_AC3
2323 // - AUDIO_PARSER_CODEC_DTS
2324{
2325 unsigned char *stream;
2326 int ret = 0;
2327 stream = (unsigned char *)audio_stream_data;
2328
2329 if (audio_stream_data == NULL || audio_frame_size == 0) {
2330 ALOGW("no buffer to get channel status, return default for compress");
2331 get_default_compressed_channel_status(channel_status);
2332 return ret;
2333 }
2334
2335 memset(channel_status,0,24);
2336 if(init_audio_parser(stream, audio_frame_size, codec_type) == -1)
2337 {
2338 ALOGE("init audio parser failed");
2339 return -1;
2340 }
2341 ret = get_channel_status(channel_status, codec_type);
2342 return ret;
2343
2344}
2345
Ashish Jain81eb2a82015-05-13 10:52:34 +05302346void get_lpcm_channel_status(uint32_t sampleRate,
2347 unsigned char *channel_status)
2348{
2349 int32_t status = 0;
Ashish Jain81eb2a82015-05-13 10:52:34 +05302350 memset(channel_status,0,24);
2351 /* block start bit in preamble bit 3 */
2352 channel_status[0] |= PROFESSIONAL;
2353 //LPCM OUT
2354 channel_status[0] &= ~NON_LPCM;
2355
2356 switch (sampleRate) {
2357 case 8000:
2358 case 11025:
2359 case 12000:
2360 case 16000:
2361 case 22050:
2362 channel_status[3] |= SR_NOTID;
Preetam Singh Ranawat61716b12015-12-14 11:55:24 +05302363 break;
Ashish Jain81eb2a82015-05-13 10:52:34 +05302364 case 24000:
2365 channel_status[3] |= SR_24000;
2366 break;
2367 case 32000:
2368 channel_status[3] |= SR_32000;
2369 break;
2370 case 44100:
2371 channel_status[3] |= SR_44100;
2372 break;
2373 case 48000:
2374 channel_status[3] |= SR_48000;
2375 break;
2376 case 88200:
2377 channel_status[3] |= SR_88200;
2378 break;
2379 case 96000:
2380 channel_status[3] |= SR_96000;
2381 break;
2382 case 176400:
2383 channel_status[3] |= SR_176400;
2384 break;
2385 case 192000:
2386 channel_status[3] |= SR_192000;
2387 break;
2388 default:
2389 ALOGV("Invalid sample_rate %u\n", sampleRate);
2390 status = -1;
2391 break;
2392 }
2393}
2394
2395void audio_utils_set_hdmi_channel_status(struct stream_out *out, char * buffer, size_t bytes)
2396{
2397 unsigned char channel_status[24]={0};
2398 struct snd_aes_iec958 iec958;
2399 const char *mixer_ctl_name = "IEC958 Playback PCM Stream";
2400 struct mixer_ctl *ctl;
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +05302401 ALOGV("%s: buffer %s bytes %zd", __func__, buffer, bytes);
Arun Mirpurie008ed22019-03-21 11:21:04 -07002402
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05302403 if (audio_extn_utils_is_dolby_format(out->format) &&
Ashish Jain81eb2a82015-05-13 10:52:34 +05302404 /*TODO:Extend code to support DTS passthrough*/
2405 /*set compressed channel status bits*/
Arun Mirpurie008ed22019-03-21 11:21:04 -07002406 audio_extn_passthru_is_passthrough_stream(out) &&
2407 audio_extn_is_hdmi_passthru_enabled()) {
Ashish Jain81eb2a82015-05-13 10:52:34 +05302408 get_compressed_channel_status(buffer, bytes, channel_status, AUDIO_PARSER_CODEC_AC3);
Arun Mirpurie008ed22019-03-21 11:21:04 -07002409 } else {
Ashish Jain81eb2a82015-05-13 10:52:34 +05302410 /*set channel status bit for LPCM*/
2411 get_lpcm_channel_status(out->sample_rate, channel_status);
2412 }
2413
2414 memcpy(iec958.status, channel_status,sizeof(iec958.status));
2415 ctl = mixer_get_ctl_by_name(out->dev->mixer, mixer_ctl_name);
2416 if (!ctl) {
2417 ALOGE("%s: Could not get ctl for mixer cmd - %s",
2418 __func__, mixer_ctl_name);
2419 return;
2420 }
2421 if (mixer_ctl_set_array(ctl, &iec958, sizeof(iec958)) < 0) {
2422 ALOGE("%s: Could not set channel status for ext HDMI ",
2423 __func__);
2424 return;
2425 }
2426
2427}
2428#endif
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302429
2430int audio_extn_utils_get_avt_device_drift(
2431 struct audio_usecase *usecase,
2432 struct audio_avt_device_drift_param *drift_param)
2433{
2434 int ret = 0, count = 0;
2435 char avt_device_drift_mixer_ctl_name[MIXER_PATH_MAX_LENGTH] = {0};
Naresh Tanniru6160c712017-04-17 15:43:48 +05302436 const char *backend = NULL;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302437 struct mixer_ctl *ctl = NULL;
2438 struct audio_avt_device_drift_stats drift_stats;
2439 struct audio_device *adev = NULL;
2440
2441 if (usecase != NULL && usecase->type == PCM_PLAYBACK) {
Naresh Tanniru6160c712017-04-17 15:43:48 +05302442 backend = platform_get_snd_device_backend_interface(usecase->out_snd_device);
2443 if (!backend) {
2444 ALOGE("%s: Unsupported device %d", __func__,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08002445 get_device_types(&usecase->stream.out->device_list));
Naresh Tanniru6160c712017-04-17 15:43:48 +05302446 ret = -EINVAL;
2447 goto done;
2448 }
2449 strlcpy(avt_device_drift_mixer_ctl_name,
2450 backend,
2451 MIXER_PATH_MAX_LENGTH);
2452
2453 count = strlen(backend);
2454 if (MIXER_PATH_MAX_LENGTH - count > 0) {
2455 strlcat(&avt_device_drift_mixer_ctl_name[count],
2456 " DRIFT",
2457 MIXER_PATH_MAX_LENGTH - count);
2458 } else {
2459 ret = -EINVAL;
2460 goto done;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302461 }
2462 } else {
Naresh Tanniru7586e292017-04-13 10:38:13 +05302463 ALOGE("%s: Invalid usecase",__func__);
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302464 ret = -EINVAL;
Naresh Tanniru6160c712017-04-17 15:43:48 +05302465 goto done;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302466 }
2467
Naresh Tanniru6160c712017-04-17 15:43:48 +05302468 adev = usecase->stream.out->dev;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302469 ctl = mixer_get_ctl_by_name(adev->mixer, avt_device_drift_mixer_ctl_name);
2470 if (!ctl) {
2471 ALOGE("%s: Could not get ctl for mixer cmd - %s",
2472 __func__, avt_device_drift_mixer_ctl_name);
2473
2474 ret = -EINVAL;
2475 goto done;
2476 }
2477
2478 ALOGV("%s: Getting AV Timer vs Device Drift mixer ctrl name %s", __func__,
2479 avt_device_drift_mixer_ctl_name);
2480
2481 mixer_ctl_update(ctl);
2482 count = mixer_ctl_get_num_values(ctl);
2483 if (count != sizeof(struct audio_avt_device_drift_stats)) {
2484 ALOGE("%s: mixer_ctl_get_num_values() invalid drift_stats data size",
2485 __func__);
2486
2487 ret = -EINVAL;
2488 goto done;
2489 }
2490
2491 ret = mixer_ctl_get_array(ctl, (void *)&drift_stats, count);
2492 if (ret != 0) {
2493 ALOGE("%s: mixer_ctl_get_array() failed to get drift_stats Params",
2494 __func__);
2495
2496 ret = -EINVAL;
2497 goto done;
2498 }
2499 memcpy(drift_param, &drift_stats.drift_param,
2500 sizeof(struct audio_avt_device_drift_param));
2501done:
2502 return ret;
2503}
Manish Dewangan07de2142017-02-27 19:27:20 +05302504
2505#ifdef SNDRV_COMPRESS_PATH_DELAY
2506int audio_extn_utils_compress_get_dsp_latency(struct stream_out *out)
2507{
2508 int ret = -EINVAL;
2509 struct snd_compr_metadata metadata;
2510 int delay_ms = COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
2511
Weiyin Jiangc49a3b52018-08-17 16:16:08 +08002512 /* override the latency for pcm offload use case */
2513 if ((out->flags & AUDIO_OUTPUT_FLAG_DIRECT) &&
2514 !(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)) {
2515 delay_ms = PCM_OFFLOAD_PLAYBACK_LATENCY;
2516 }
2517
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -07002518 if (property_get_bool("vendor.audio.playback.dsp.pathdelay", false)) {
Manish Dewangan07de2142017-02-27 19:27:20 +05302519 ALOGD("%s:: Quering DSP delay %d",__func__, __LINE__);
2520 if (!(is_offload_usecase(out->usecase))) {
2521 ALOGE("%s:: not supported for non offload session", __func__);
2522 goto exit;
2523 }
2524
2525 if (!out->compr) {
2526 ALOGD("%s:: Invalid compress handle,returning default dsp latency",
2527 __func__);
2528 goto exit;
2529 }
2530
2531 metadata.key = SNDRV_COMPRESS_PATH_DELAY;
2532 ret = compress_get_metadata(out->compr, &metadata);
2533 if(ret) {
2534 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2535 goto exit;
2536 }
2537 delay_ms = metadata.value[0] / 1000; /*convert to ms*/
2538 } else {
2539 ALOGD("%s:: Using Fix DSP delay",__func__);
2540 }
2541
2542exit:
2543 ALOGD("%s:: delay in ms is %d",__func__, delay_ms);
2544 return delay_ms;
2545}
2546#else
2547int audio_extn_utils_compress_get_dsp_latency(struct stream_out *out __unused)
2548{
2549 return COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
2550}
2551#endif
Manish Dewangan69426c82017-01-30 17:35:36 +05302552
2553#ifdef SNDRV_COMPRESS_RENDER_MODE
2554int audio_extn_utils_compress_set_render_mode(struct stream_out *out)
2555{
2556 struct snd_compr_metadata metadata;
2557 int ret = -EINVAL;
2558
2559 if (!(is_offload_usecase(out->usecase))) {
2560 ALOGE("%s:: not supported for non offload session", __func__);
2561 goto exit;
2562 }
2563
2564 if (!out->compr) {
2565 ALOGD("%s:: Invalid compress handle",
2566 __func__);
2567 goto exit;
2568 }
2569
2570 ALOGD("%s:: render mode %d", __func__, out->render_mode);
2571
2572 metadata.key = SNDRV_COMPRESS_RENDER_MODE;
2573 if (out->render_mode == RENDER_MODE_AUDIO_MASTER) {
2574 metadata.value[0] = SNDRV_COMPRESS_RENDER_MODE_AUDIO_MASTER;
2575 } else if (out->render_mode == RENDER_MODE_AUDIO_STC_MASTER) {
2576 metadata.value[0] = SNDRV_COMPRESS_RENDER_MODE_STC_MASTER;
2577 } else {
2578 ret = 0;
2579 goto exit;
2580 }
2581 ret = compress_set_metadata(out->compr, &metadata);
2582 if(ret) {
2583 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2584 }
2585exit:
2586 return ret;
2587}
2588#else
2589int audio_extn_utils_compress_set_render_mode(struct stream_out *out __unused)
2590{
2591 ALOGD("%s:: configuring render mode not supported", __func__);
2592 return 0;
2593}
2594#endif
Manish Dewangan58229382017-02-02 15:48:41 +05302595
2596#ifdef SNDRV_COMPRESS_CLK_REC_MODE
2597int audio_extn_utils_compress_set_clk_rec_mode(
2598 struct audio_usecase *usecase)
2599{
2600 struct snd_compr_metadata metadata;
2601 struct stream_out *out = NULL;
2602 int ret = -EINVAL;
2603
Naresh Tanniru7586e292017-04-13 10:38:13 +05302604 if (usecase == NULL || usecase->type != PCM_PLAYBACK) {
Manish Dewangan58229382017-02-02 15:48:41 +05302605 ALOGE("%s:: Invalid use case", __func__);
2606 goto exit;
2607 }
2608
2609 out = usecase->stream.out;
2610 if (!out) {
2611 ALOGE("%s:: invalid stream", __func__);
2612 goto exit;
2613 }
2614
2615 if (!is_offload_usecase(out->usecase)) {
2616 ALOGE("%s:: not supported for non offload session", __func__);
2617 goto exit;
2618 }
2619
2620 if (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER) {
2621 ALOGD("%s:: clk recovery is only supported in STC render mode",
2622 __func__);
2623 ret = 0;
2624 goto exit;
2625 }
2626
2627 if (!out->compr) {
2628 ALOGD("%s:: Invalid compress handle",
2629 __func__);
2630 goto exit;
2631 }
2632 metadata.key = SNDRV_COMPRESS_CLK_REC_MODE;
2633 switch(usecase->out_snd_device) {
2634 case SND_DEVICE_OUT_HDMI:
2635 case SND_DEVICE_OUT_SPEAKER_AND_HDMI:
2636 case SND_DEVICE_OUT_DISPLAY_PORT:
2637 case SND_DEVICE_OUT_SPEAKER_AND_DISPLAY_PORT:
2638 metadata.value[0] = SNDRV_COMPRESS_CLK_REC_MODE_NONE;
2639 break;
2640 default:
2641 metadata.value[0] = SNDRV_COMPRESS_CLK_REC_MODE_AUTO;
2642 break;
2643 }
2644
2645 ALOGD("%s:: clk recovery mode %d",__func__, metadata.value[0]);
2646
2647 ret = compress_set_metadata(out->compr, &metadata);
2648 if(ret) {
2649 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2650 }
2651
2652exit:
2653 return ret;
2654}
2655#else
2656int audio_extn_utils_compress_set_clk_rec_mode(
2657 struct audio_usecase *usecase __unused)
2658{
2659 ALOGD("%s:: configuring render mode not supported", __func__);
2660 return 0;
2661}
2662#endif
Manish Dewangan27346042017-03-01 12:56:12 +05302663
2664#ifdef SNDRV_COMPRESS_RENDER_WINDOW
2665int audio_extn_utils_compress_set_render_window(
2666 struct stream_out *out,
2667 struct audio_out_render_window_param *render_window)
2668{
2669 struct snd_compr_metadata metadata;
2670 int ret = -EINVAL;
2671
Manish Dewangan27346042017-03-01 12:56:12 +05302672 if(render_window == NULL) {
2673 ALOGE("%s:: Invalid render_window", __func__);
2674 goto exit;
2675 }
2676
Aniket Kumar Lata1e1d3662017-06-01 18:45:48 -07002677 ALOGD("%s:: render window start 0x%"PRIx64" end 0x%"PRIx64"",
2678 __func__,render_window->render_ws, render_window->render_we);
2679
Manish Dewangan27346042017-03-01 12:56:12 +05302680 if (!is_offload_usecase(out->usecase)) {
2681 ALOGE("%s:: not supported for non offload session", __func__);
2682 goto exit;
2683 }
2684
Naresh Tanniru6160c712017-04-17 15:43:48 +05302685 if ((out->render_mode != RENDER_MODE_AUDIO_MASTER) &&
2686 (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER)) {
Manish Dewangan27346042017-03-01 12:56:12 +05302687 ALOGD("%s:: only supported in timestamp mode, current "
2688 "render mode mode %d", __func__, out->render_mode);
2689 goto exit;
2690 }
2691
2692 if (!out->compr) {
2693 ALOGW("%s:: offload session not yet opened,"
2694 "render window will be configure later", __func__);
2695 /* store render window to reconfigure in start_output_stream() */
2696 goto exit;
2697 }
2698
2699 metadata.key = SNDRV_COMPRESS_RENDER_WINDOW;
2700 /*render window start value */
2701 metadata.value[0] = 0xFFFFFFFF & render_window->render_ws; /* lsb */
2702 metadata.value[1] = \
2703 (0xFFFFFFFF00000000 & render_window->render_ws) >> 32; /* msb*/
2704 /*render window end value */
2705 metadata.value[2] = 0xFFFFFFFF & render_window->render_we; /* lsb */
2706 metadata.value[3] = \
2707 (0xFFFFFFFF00000000 & render_window->render_we) >> 32; /* msb*/
2708
2709 ret = compress_set_metadata(out->compr, &metadata);
2710 if(ret) {
2711 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2712 }
2713
2714exit:
2715 return ret;
2716}
2717#else
2718int audio_extn_utils_compress_set_render_window(
2719 struct stream_out *out __unused,
2720 struct audio_out_render_window_param *render_window __unused)
2721{
2722 ALOGD("%s:: configuring render window not supported", __func__);
2723 return 0;
2724}
2725#endif
Manish Dewangan14956cc2017-02-14 18:54:42 +05302726
2727#ifdef SNDRV_COMPRESS_START_DELAY
2728int audio_extn_utils_compress_set_start_delay(
2729 struct stream_out *out,
2730 struct audio_out_start_delay_param *delay_param)
2731{
2732 struct snd_compr_metadata metadata;
2733 int ret = -EINVAL;
2734
2735 if(delay_param == NULL) {
2736 ALOGE("%s:: Invalid delay_param", __func__);
2737 goto exit;
2738 }
2739
2740 ALOGD("%s:: render start delay 0x%"PRIx64" ", __func__,
2741 delay_param->start_delay);
2742
2743 if (!is_offload_usecase(out->usecase)) {
2744 ALOGE("%s:: not supported for non offload session", __func__);
2745 goto exit;
2746 }
2747
Naresh Tanniru6160c712017-04-17 15:43:48 +05302748 if ((out->render_mode != RENDER_MODE_AUDIO_MASTER) &&
2749 (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER)) {
Manish Dewangan14956cc2017-02-14 18:54:42 +05302750 ALOGD("%s:: only supported in timestamp mode, current "
2751 "render mode mode %d", __func__, out->render_mode);
2752 goto exit;
2753 }
2754
2755 if (!out->compr) {
2756 ALOGW("%s:: offload session not yet opened,"
2757 "start delay will be configure later", __func__);
2758 goto exit;
2759 }
2760
2761 metadata.key = SNDRV_COMPRESS_START_DELAY;
2762 metadata.value[0] = 0xFFFFFFFF & delay_param->start_delay; /* lsb */
2763 metadata.value[1] = \
2764 (0xFFFFFFFF00000000 & delay_param->start_delay) >> 32; /* msb*/
2765
2766 ret = compress_set_metadata(out->compr, &metadata);
2767 if(ret) {
2768 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2769 }
2770
2771exit:
2772 return ret;
2773}
2774#else
2775int audio_extn_utils_compress_set_start_delay(
2776 struct stream_out *out __unused,
2777 struct audio_out_start_delay_param *delay_param __unused)
2778{
2779 ALOGD("%s:: configuring render window not supported", __func__);
2780 return 0;
2781}
2782#endif
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002783
Surendar Karka287348c2019-04-10 18:31:46 +05302784#ifdef SNDRV_COMPRESS_DSP_POSITION
2785int audio_extn_utils_compress_get_dsp_presentation_pos(struct stream_out *out,
2786 uint64_t *frames, struct timespec *timestamp, int32_t clock_id)
2787{
2788 int ret = -EINVAL;
2789 uint64_t *val = NULL;
2790 uint64_t time = 0;
2791 struct snd_compr_metadata metadata;
2792
2793 ALOGV("%s:: Quering DSP position with clock id %d",__func__, clock_id);
2794 metadata.key = SNDRV_COMPRESS_DSP_POSITION;
2795 metadata.value[0] = clock_id;
2796 ret = compress_get_metadata(out->compr, &metadata);
2797 if (ret) {
2798 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2799 ret = -errno;
2800 goto exit;
2801 }
2802 val = (uint64_t *)&metadata.value[1];
2803 *frames = *val;
2804 time = *(val + 1);
2805 timestamp->tv_sec = time / 1000000;
2806 timestamp->tv_nsec = (time % 1000000)*1000;
2807
2808exit:
2809 return ret;
2810}
2811#else
2812int audio_extn_utils_compress_get_dsp_presentation_pos(struct stream_out *out __unused,
2813 uint64_t *frames __unused, struct timespec *timestamp __unused,
2814 int32_t clock_id __unused)
2815{
2816 ALOGD("%s:: dsp presentation position not supported", __func__);
2817 return 0;
2818
2819}
2820#endif
2821
2822#ifdef SNDRV_PCM_IOCTL_DSP_POSITION
2823int audio_extn_utils_pcm_get_dsp_presentation_pos(struct stream_out *out,
2824 uint64_t *frames, struct timespec *timestamp, int32_t clock_id)
2825{
2826 int ret = -EINVAL;
2827 uint64_t time = 0;
2828 struct snd_pcm_prsnt_position prsnt_position;
Krishna Kishor Jhac1542b22022-08-22 13:12:09 +05302829 memset(&prsnt_position, 0, sizeof(struct snd_pcm_prsnt_position));
Surendar Karka287348c2019-04-10 18:31:46 +05302830
2831 ALOGV("%s:: Quering DSP position with clock id %d",__func__, clock_id);
2832 prsnt_position.clock_id = clock_id;
2833 ret = pcm_ioctl(out->pcm, SNDRV_PCM_IOCTL_DSP_POSITION, &prsnt_position);
2834 if (ret) {
2835 ALOGE("%s::error %d", __func__, ret);
2836 ret = -EIO;
2837 goto exit;
2838 }
2839
2840 *frames = prsnt_position.frames;
2841 time = prsnt_position.timestamp;
2842 timestamp->tv_sec = time / 1000000;
2843 timestamp->tv_nsec = (time % 1000000)*1000;
2844
2845exit:
2846 return ret;
2847}
2848#else
2849int audio_extn_utils_pcm_get_dsp_presentation_pos(struct stream_out *out __unused,
2850 uint64_t *frames __unused, struct timespec *timestamp __unused,
2851 int32_t clock_id __unused)
2852{
2853 ALOGD("%s:: dsp presentation position not supported", __func__);
2854 return 0;
2855
2856}
2857#endif
2858
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002859#define MAX_SND_CARD 8
Jaideep Sharmadf948882020-10-22 14:13:52 +05302860#define RETRY_US 400000
2861#define RETRY_NUMBER 100
Aalique Grahame22e49102018-12-18 14:23:57 -08002862#define PLATFORM_INFO_XML_PATH "audio_platform_info.xml"
2863#define PLATFORM_INFO_XML_BASE_STRING "audio_platform_info"
2864
Aalique Grahame22e49102018-12-18 14:23:57 -08002865bool audio_extn_utils_resolve_config_file(char file_name[MIXER_PATH_MAX_LENGTH])
2866{
2867 char full_config_path[MIXER_PATH_MAX_LENGTH];
Saurav Kumarf5d2c342020-07-29 19:31:49 +05302868 char vendor_config_path[VENDOR_CONFIG_PATH_MAX_LENGTH];
2869
2870 /* Get path for audio configuration files in vendor */
2871 audio_get_vendor_config_path(vendor_config_path,
2872 sizeof(vendor_config_path));
2873 snprintf(full_config_path, sizeof(full_config_path),
2874 "%s/%s", vendor_config_path, file_name);
2875 if (F_OK == access(full_config_path, 0)) {
2876 strlcpy(file_name, full_config_path, MIXER_PATH_MAX_LENGTH);
2877 return true;
Aalique Grahame22e49102018-12-18 14:23:57 -08002878 }
2879 return false;
2880}
2881
2882/* platform_info_file should be size 'MIXER_PATH_MAX_LENGTH' */
2883int audio_extn_utils_get_platform_info(const char* snd_card_name, char* platform_info_file)
2884{
2885 if (NULL == snd_card_name) {
2886 return -1;
2887 }
2888
2889 struct snd_card_split *snd_split_handle = NULL;
2890 int ret = 0;
2891 audio_extn_set_snd_card_split(snd_card_name);
2892 snd_split_handle = audio_extn_get_snd_card_split();
2893
2894 snprintf(platform_info_file, MIXER_PATH_MAX_LENGTH, "%s_%s_%s.xml",
2895 PLATFORM_INFO_XML_BASE_STRING, snd_split_handle->snd_card,
2896 snd_split_handle->form_factor);
2897
2898 if (!audio_extn_utils_resolve_config_file(platform_info_file)) {
2899 memset(platform_info_file, 0, MIXER_PATH_MAX_LENGTH);
2900 snprintf(platform_info_file, MIXER_PATH_MAX_LENGTH, "%s_%s.xml",
2901 PLATFORM_INFO_XML_BASE_STRING, snd_split_handle->snd_card);
2902
2903 if (!audio_extn_utils_resolve_config_file(platform_info_file)) {
2904 memset(platform_info_file, 0, MIXER_PATH_MAX_LENGTH);
2905 strlcpy(platform_info_file, PLATFORM_INFO_XML_PATH, MIXER_PATH_MAX_LENGTH);
2906 ret = audio_extn_utils_resolve_config_file(platform_info_file) ? 0 : -1;
2907 }
2908 }
2909
2910 return ret;
2911}
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002912
2913int audio_extn_utils_get_snd_card_num()
2914{
Soumya Managoli9fee7c62018-04-06 16:21:50 +05302915 int snd_card_num = 0;
2916 struct mixer *mixer = NULL;
2917
2918 snd_card_num = audio_extn_utils_open_snd_mixer(&mixer);
2919 if (mixer)
2920 mixer_close(mixer);
2921 return snd_card_num;
2922}
2923
2924int audio_extn_utils_open_snd_mixer(struct mixer **mixer_handle)
2925{
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002926
2927 void *hw_info = NULL;
2928 struct mixer *mixer = NULL;
2929 int retry_num = 0;
mpangfaa7bae2019-01-15 16:38:13 +08002930 int snd_card_num = 0;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002931 char* snd_card_name = NULL;
mpangfaa7bae2019-01-15 16:38:13 +08002932 int snd_card_detection_info[MAX_SND_CARD] = {0};
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002933
Soumya Managoli9fee7c62018-04-06 16:21:50 +05302934 if (!mixer_handle) {
2935 ALOGE("invalid mixer handle");
2936 return -1;
2937 }
2938 *mixer_handle = NULL;
Ashish Jain30ae8942017-11-05 17:21:23 +05302939 /*
mpangfaa7bae2019-01-15 16:38:13 +08002940 * Try with all the sound cards ( 0 to 7 ) and if none of them were detected
Ashish Jain30ae8942017-11-05 17:21:23 +05302941 * sleep for 1 sec and try detections with sound card 0 again.
2942 * If sound card gets detected, check if it is relevant, if not check with the
2943 * other sound cards. To ensure that the irrelevant sound card is not check again,
2944 * we maintain it in min_snd_card_num.
2945 */
2946 while (retry_num < RETRY_NUMBER) {
mpangfaa7bae2019-01-15 16:38:13 +08002947 snd_card_num = 0;
2948 while (snd_card_num < MAX_SND_CARD) {
2949 if (snd_card_detection_info[snd_card_num] == 0) {
2950 mixer = mixer_open(snd_card_num);
2951 if (!mixer)
2952 snd_card_num++;
2953 else
2954 break;
2955 } else
2956 snd_card_num++;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002957 }
2958
2959 if (!mixer) {
Ashish Jain30ae8942017-11-05 17:21:23 +05302960 usleep(RETRY_US);
Ashish Jain30ae8942017-11-05 17:21:23 +05302961 retry_num++;
mpangfaa7bae2019-01-15 16:38:13 +08002962 ALOGD("%s: retry, retry_num %d", __func__, retry_num);
Ashish Jain30ae8942017-11-05 17:21:23 +05302963 continue;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002964 }
2965
2966 snd_card_name = strdup(mixer_get_name(mixer));
2967 if (!snd_card_name) {
2968 ALOGE("failed to allocate memory for snd_card_name\n");
2969 mixer_close(mixer);
2970 return -1;
2971 }
2972 ALOGD("%s: snd_card_name: %s", __func__, snd_card_name);
mpangfaa7bae2019-01-15 16:38:13 +08002973 snd_card_detection_info[snd_card_num] = 1;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002974 hw_info = hw_info_init(snd_card_name);
2975 if (hw_info) {
2976 ALOGD("%s: Opened sound card:%d", __func__, snd_card_num);
2977 break;
2978 }
mpangfaa7bae2019-01-15 16:38:13 +08002979 ALOGE("%s: Failed to init hardware info, snd_card_num:%d", __func__, snd_card_num);
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002980
Dhananjay Kumara7e27832017-07-11 15:40:39 +05302981 free(snd_card_name);
2982 snd_card_name = NULL;
2983
2984 mixer_close(mixer);
2985 mixer = NULL;
2986 }
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002987 if (snd_card_name)
2988 free(snd_card_name);
Ashish Jain30ae8942017-11-05 17:21:23 +05302989
Dhananjay Kumara7e27832017-07-11 15:40:39 +05302990 if (hw_info)
2991 hw_info_deinit(hw_info);
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002992
mpangfaa7bae2019-01-15 16:38:13 +08002993 if (retry_num >= RETRY_NUMBER) {
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002994 ALOGE("%s: Unable to find correct sound card, aborting.", __func__);
2995 return -1;
2996 }
2997
Soumya Managoli9fee7c62018-04-06 16:21:50 +05302998 if (mixer)
2999 *mixer_handle = mixer;
3000
Vignesh Kulothungan55396882017-04-20 14:37:02 -07003001 return snd_card_num;
3002}
Naresh Tanniru6160c712017-04-17 15:43:48 +05303003
Soumya Managoli9fee7c62018-04-06 16:21:50 +05303004void audio_extn_utils_close_snd_mixer(struct mixer *mixer)
3005{
3006 if (mixer)
3007 mixer_close(mixer);
3008}
3009
Naresh Tanniru6160c712017-04-17 15:43:48 +05303010#ifdef SNDRV_COMPRESS_ENABLE_ADJUST_SESSION_CLOCK
3011int audio_extn_utils_compress_enable_drift_correction(
3012 struct stream_out *out,
3013 struct audio_out_enable_drift_correction *drift)
3014{
3015 struct snd_compr_metadata metadata;
3016 int ret = -EINVAL;
3017
3018 if(drift == NULL) {
3019 ALOGE("%s:: Invalid param", __func__);
3020 goto exit;
3021 }
3022
3023 ALOGD("%s:: drift enable %d", __func__,drift->enable);
3024
3025 if (!is_offload_usecase(out->usecase)) {
3026 ALOGE("%s:: not supported for non offload session", __func__);
3027 goto exit;
3028 }
3029
3030 if (!out->compr) {
3031 ALOGW("%s:: offload session not yet opened,"
3032 "start delay will be configure later", __func__);
3033 goto exit;
3034 }
3035
3036 metadata.key = SNDRV_COMPRESS_ENABLE_ADJUST_SESSION_CLOCK;
3037 metadata.value[0] = drift->enable;
3038 out->drift_correction_enabled = drift->enable;
3039
3040 ret = compress_set_metadata(out->compr, &metadata);
3041 if(ret) {
3042 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
3043 out->drift_correction_enabled = false;
3044 }
3045
3046exit:
3047 return ret;
3048}
3049#else
3050int audio_extn_utils_compress_enable_drift_correction(
3051 struct stream_out *out __unused,
3052 struct audio_out_enable_drift_correction *drift __unused)
3053{
3054 ALOGD("%s:: configuring drift enablement not supported", __func__);
3055 return 0;
3056}
3057#endif
3058
3059#ifdef SNDRV_COMPRESS_ADJUST_SESSION_CLOCK
3060int audio_extn_utils_compress_correct_drift(
3061 struct stream_out *out,
3062 struct audio_out_correct_drift *drift_param)
3063{
3064 struct snd_compr_metadata metadata;
3065 int ret = -EINVAL;
3066
3067 if (drift_param == NULL) {
3068 ALOGE("%s:: Invalid drift_param", __func__);
3069 goto exit;
3070 }
3071
3072 ALOGD("%s:: adjust time 0x%"PRIx64" ", __func__,
3073 drift_param->adjust_time);
3074
3075 if (!is_offload_usecase(out->usecase)) {
3076 ALOGE("%s:: not supported for non offload session", __func__);
3077 goto exit;
3078 }
3079
3080 if (!out->compr) {
3081 ALOGW("%s:: offload session not yet opened", __func__);
3082 goto exit;
3083 }
3084
3085 if (!out->drift_correction_enabled) {
3086 ALOGE("%s:: drift correction not enabled", __func__);
3087 goto exit;
3088 }
3089
3090 metadata.key = SNDRV_COMPRESS_ADJUST_SESSION_CLOCK;
3091 metadata.value[0] = 0xFFFFFFFF & drift_param->adjust_time; /* lsb */
3092 metadata.value[1] = \
3093 (0xFFFFFFFF00000000 & drift_param->adjust_time) >> 32; /* msb*/
3094
3095 ret = compress_set_metadata(out->compr, &metadata);
3096 if(ret)
3097 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
3098exit:
3099 return ret;
3100}
3101#else
3102int audio_extn_utils_compress_correct_drift(
3103 struct stream_out *out __unused,
3104 struct audio_out_correct_drift *drift_param __unused)
3105{
3106 ALOGD("%s:: setting adjust clock not supported", __func__);
3107 return 0;
3108}
3109#endif
Naresh Tanniru29bce4e2017-04-27 17:54:30 +05303110
3111int audio_extn_utils_set_channel_map(
3112 struct stream_out *out,
3113 struct audio_out_channel_map_param *channel_map_param)
3114{
3115 int ret = -EINVAL, i = 0;
3116 int channels = audio_channel_count_from_out_mask(out->channel_mask);
3117
3118 if (channel_map_param == NULL) {
3119 ALOGE("%s:: Invalid channel_map", __func__);
3120 goto exit;
3121 }
3122
3123 if (channel_map_param->channels != channels) {
3124 ALOGE("%s:: Channels(%d) does not match stream channels(%d)",
3125 __func__, channel_map_param->channels, channels);
3126 goto exit;
3127 }
3128
3129 for ( i = 0; i < channels; i++) {
3130 ALOGV("%s:: channel_map[%d]- %d", __func__, i, channel_map_param->channel_map[i]);
3131 out->channel_map_param.channel_map[i] = channel_map_param->channel_map[i];
3132 }
3133 ret = 0;
3134exit:
3135 return ret;
3136}
Varun Balaraje49253e2017-07-06 19:48:56 +05303137
3138int audio_extn_utils_set_pan_scale_params(
3139 struct stream_out *out,
3140 struct mix_matrix_params *mm_params)
3141{
3142 int ret = -EINVAL, i = 0, j = 0;
3143
Varun Balaraj003ee752017-08-07 17:54:55 +05303144 if (mm_params == NULL || out == NULL) {
3145 ALOGE("%s:: Invalid mix matrix or out param", __func__);
Varun Balaraje49253e2017-07-06 19:48:56 +05303146 goto exit;
3147 }
3148
3149 if (mm_params->num_output_channels > MAX_CHANNELS_SUPPORTED ||
3150 mm_params->num_output_channels <= 0 ||
3151 mm_params->num_input_channels > MAX_CHANNELS_SUPPORTED ||
3152 mm_params->num_input_channels <= 0)
3153 goto exit;
3154
3155 out->pan_scale_params.num_output_channels = mm_params->num_output_channels;
3156 out->pan_scale_params.num_input_channels = mm_params->num_input_channels;
3157 out->pan_scale_params.has_output_channel_map =
3158 mm_params->has_output_channel_map;
3159 for (i = 0; i < mm_params->num_output_channels; i++)
3160 out->pan_scale_params.output_channel_map[i] =
3161 mm_params->output_channel_map[i];
3162
3163 out->pan_scale_params.has_input_channel_map =
3164 mm_params->has_input_channel_map;
3165 for (i = 0; i < mm_params->num_input_channels; i++)
3166 out->pan_scale_params.input_channel_map[i] =
3167 mm_params->input_channel_map[i];
3168
3169 out->pan_scale_params.has_mixer_coeffs = mm_params->has_mixer_coeffs;
3170 for (i = 0; i < mm_params->num_output_channels; i++)
3171 for (j = 0; j < mm_params->num_input_channels; j++) {
3172 //Convert the channel coefficient gains in Q14 format
3173 out->pan_scale_params.mixer_coeffs[i][j] =
3174 mm_params->mixer_coeffs[i][j] * (2 << 13);
3175 }
3176
3177 ret = platform_set_stream_pan_scale_params(out->dev->platform,
3178 out->pcm_device_id,
3179 out->pan_scale_params);
3180
3181exit:
3182 return ret;
3183}
3184
3185int audio_extn_utils_set_downmix_params(
3186 struct stream_out *out,
3187 struct mix_matrix_params *mm_params)
3188{
3189 int ret = -EINVAL, i = 0, j = 0;
3190 struct audio_usecase *usecase = NULL;
3191
Aniket Kumar Lataf9f246e2017-09-15 15:20:16 -07003192 if (mm_params == NULL || out == NULL) {
Varun Balaraj003ee752017-08-07 17:54:55 +05303193 ALOGE("%s:: Invalid mix matrix or out param", __func__);
Varun Balaraje49253e2017-07-06 19:48:56 +05303194 goto exit;
3195 }
3196
3197 if (mm_params->num_output_channels > MAX_CHANNELS_SUPPORTED ||
3198 mm_params->num_output_channels <= 0 ||
3199 mm_params->num_input_channels > MAX_CHANNELS_SUPPORTED ||
3200 mm_params->num_input_channels <= 0)
3201 goto exit;
3202
3203 usecase = get_usecase_from_list(out->dev, out->usecase);
Varun Balaraj003ee752017-08-07 17:54:55 +05303204 if (!usecase) {
3205 ALOGE("%s: Get usecase list failed!", __func__);
Aniket Kumar Lataf9f246e2017-09-15 15:20:16 -07003206 goto exit;
3207 }
Varun Balaraje49253e2017-07-06 19:48:56 +05303208 out->downmix_params.num_output_channels = mm_params->num_output_channels;
3209 out->downmix_params.num_input_channels = mm_params->num_input_channels;
3210
3211 out->downmix_params.has_output_channel_map =
3212 mm_params->has_output_channel_map;
3213 for (i = 0; i < mm_params->num_output_channels; i++) {
3214 out->downmix_params.output_channel_map[i] =
3215 mm_params->output_channel_map[i];
3216 }
3217
3218 out->downmix_params.has_input_channel_map =
3219 mm_params->has_input_channel_map;
3220 for (i = 0; i < mm_params->num_input_channels; i++)
3221 out->downmix_params.input_channel_map[i] =
3222 mm_params->input_channel_map[i];
3223
3224 out->downmix_params.has_mixer_coeffs = mm_params->has_mixer_coeffs;
3225 for (i = 0; i < mm_params->num_output_channels; i++)
Nikhil Latukar2e6f6242017-08-15 13:37:07 +05303226 for (j = 0; j < mm_params->num_input_channels; j++) {
3227 //Convert the channel coefficient gains in Q14 format
Varun Balaraje49253e2017-07-06 19:48:56 +05303228 out->downmix_params.mixer_coeffs[i][j] =
Nikhil Latukar2e6f6242017-08-15 13:37:07 +05303229 mm_params->mixer_coeffs[i][j] * (2 << 13);
3230 }
Varun Balaraje49253e2017-07-06 19:48:56 +05303231
3232 ret = platform_set_stream_downmix_params(out->dev->platform,
3233 out->pcm_device_id,
3234 usecase->out_snd_device,
3235 out->downmix_params);
3236
3237exit:
3238 return ret;
3239}
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05303240
3241bool audio_extn_utils_is_dolby_format(audio_format_t format)
3242{
3243 if (format == AUDIO_FORMAT_AC3 ||
3244 format == AUDIO_FORMAT_E_AC3 ||
3245 format == AUDIO_FORMAT_E_AC3_JOC)
3246 return true;
3247 else
3248 return false;
3249}
3250
`Deeraj Soman676c2702017-09-18 19:25:53 +05303251int audio_extn_utils_get_bit_width_from_string(const char *id_string)
3252{
3253 int i;
3254 const mixer_config_lookup mixer_bitwidth_config[] = {{"S24_3LE", 24},
3255 {"S32_LE", 32},
3256 {"S24_LE", 24},
3257 {"S16_LE", 16}};
3258 int num_configs = sizeof(mixer_bitwidth_config) / sizeof(mixer_bitwidth_config[0]);
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05303259
`Deeraj Soman676c2702017-09-18 19:25:53 +05303260 for (i = 0; i < num_configs; i++) {
3261 if (!strcmp(id_string, mixer_bitwidth_config[i].id_string))
3262 return mixer_bitwidth_config[i].value;
3263 }
3264
3265 return -EINVAL;
3266}
3267
3268int audio_extn_utils_get_sample_rate_from_string(const char *id_string)
3269{
3270 int i;
Weiyin Jiang30f9ab52020-11-11 11:37:21 +08003271 const mixer_config_lookup mixer_samplerate_config[] = {{"KHZ_8", 8000},
3272 {"KHZ_11P025", 11025},
3273 {"KHZ_16", 16000},
3274 {"KHZ_22P05", 22050},
3275 {"KHZ_32", 32000},
`Deeraj Soman676c2702017-09-18 19:25:53 +05303276 {"KHZ_48", 48000},
3277 {"KHZ_96", 96000},
3278 {"KHZ_144", 144000},
3279 {"KHZ_192", 192000},
3280 {"KHZ_384", 384000},
3281 {"KHZ_44P1", 44100},
3282 {"KHZ_88P2", 88200},
3283 {"KHZ_176P4", 176400},
3284 {"KHZ_352P8", 352800}};
3285 int num_configs = sizeof(mixer_samplerate_config) / sizeof(mixer_samplerate_config[0]);
3286
3287 for (i = 0; i < num_configs; i++) {
3288 if (!strcmp(id_string, mixer_samplerate_config[i].id_string))
3289 return mixer_samplerate_config[i].value;
3290 }
3291
3292 return -EINVAL;
3293}
3294
3295int audio_extn_utils_get_channels_from_string(const char *id_string)
3296{
3297 int i;
3298 const mixer_config_lookup mixer_channels_config[] = {{"One", 1},
3299 {"Two", 2},
3300 {"Three",3},
3301 {"Four", 4},
3302 {"Five", 5},
3303 {"Six", 6},
3304 {"Seven", 7},
3305 {"Eight", 8}};
3306 int num_configs = sizeof(mixer_channels_config) / sizeof(mixer_channels_config[0]);
3307
3308 for (i = 0; i < num_configs; i++) {
3309 if (!strcmp(id_string, mixer_channels_config[i].id_string))
3310 return mixer_channels_config[i].value;
3311 }
3312
3313 return -EINVAL;
3314}
Surendar karka30569792018-05-08 12:02:21 +05303315
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08003316void audio_extn_utils_release_snd_device(snd_device_t snd_device)
3317{
3318 audio_extn_dev_arbi_release(snd_device);
3319 audio_extn_sound_trigger_update_device_status(snd_device,
3320 ST_EVENT_SND_DEVICE_FREE);
3321 audio_extn_listen_update_device_status(snd_device,
3322 LISTEN_EVENT_SND_DEVICE_FREE);
3323}
3324
Weiyin Jiangd5718bd2019-09-04 16:52:08 +08003325int audio_extn_utils_get_license_params(
3326 const struct audio_device *adev,
3327 struct audio_license_params *license_params)
Surendar karka30569792018-05-08 12:02:21 +05303328{
3329 if(!license_params)
3330 return -EINVAL;
Weiyin Jiangd5718bd2019-09-04 16:52:08 +08003331
3332 return platform_get_license_by_product(adev->platform,
3333 (const char*)license_params->product, &license_params->key, license_params->license);
Surendar karka30569792018-05-08 12:02:21 +05303334}
3335
Aalique Grahame22e49102018-12-18 14:23:57 -08003336int audio_extn_utils_send_app_type_gain(struct audio_device *adev,
3337 int app_type,
3338 int *gain)
3339{
3340 int gain_cfg[4];
3341 const char *mixer_ctl_name = "App Type Gain";
3342 struct mixer_ctl *ctl;
3343 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
3344 if (!ctl) {
3345 ALOGE("%s: Could not get volume ctl mixer %s", __func__,
3346 mixer_ctl_name);
3347 return -EINVAL;
3348 }
3349 gain_cfg[0] = 0;
3350 gain_cfg[1] = app_type;
3351 gain_cfg[2] = gain[0];
3352 gain_cfg[3] = gain[1];
3353 ALOGV("%s app_type %d l(%d) r(%d)", __func__, app_type, gain[0], gain[1]);
3354 return mixer_ctl_set_array(ctl, gain_cfg,
3355 sizeof(gain_cfg)/sizeof(gain_cfg[0]));
3356}
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -07003357
Weiyin Jiangd5718bd2019-09-04 16:52:08 +08003358static void vndk_fwk_init()
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -07003359{
Weiyin Jiangd5718bd2019-09-04 16:52:08 +08003360 if (mVndkFwk.lib_handle != NULL)
3361 return;
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -07003362
Weiyin Jiangd5718bd2019-09-04 16:52:08 +08003363 mVndkFwk.lib_handle = dlopen(VNDK_FWK_LIB_PATH, RTLD_NOW);
3364 if (mVndkFwk.lib_handle == NULL) {
3365 ALOGW("%s: failed to dlopen VNDK_FWK_LIB %s", __func__, strerror(errno));
3366 return;
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -07003367 }
3368
Weiyin Jiangd5718bd2019-09-04 16:52:08 +08003369 *(void **)(&mVndkFwk.isVendorEnhancedFwk) =
3370 dlsym(mVndkFwk.lib_handle, "isRunningWithVendorEnhancedFramework");
3371 if (mVndkFwk.isVendorEnhancedFwk == NULL) {
3372 ALOGW("%s: dlsym failed %s", __func__, strerror(errno));
3373 if (mVndkFwk.lib_handle) {
3374 dlclose(mVndkFwk.lib_handle);
3375 mVndkFwk.lib_handle = NULL;
3376 }
3377 return;
3378 }
3379
3380
3381 *(void **)(&mVndkFwk.getVendorEnhancedInfo) =
3382 dlsym(mVndkFwk.lib_handle, "getVendorEnhancedInfo");
3383 if (mVndkFwk.getVendorEnhancedInfo == NULL) {
3384 ALOGW("%s: dlsym failed %s", __func__, strerror(errno));
3385 if (mVndkFwk.lib_handle) {
3386 dlclose(mVndkFwk.lib_handle);
3387 mVndkFwk.lib_handle = NULL;
3388 }
3389 }
3390
3391 return;
3392}
3393
3394bool audio_extn_utils_is_vendor_enhanced_fwk()
3395{
3396 static int is_vendor_enhanced_fwk = -EINVAL;
3397 if (is_vendor_enhanced_fwk != -EINVAL)
3398 return (bool)is_vendor_enhanced_fwk;
3399
3400 vndk_fwk_init();
3401
3402 if (mVndkFwk.isVendorEnhancedFwk != NULL) {
3403 is_vendor_enhanced_fwk = mVndkFwk.isVendorEnhancedFwk();
3404 ALOGW("%s: is_vendor_enhanced_fwk %d", __func__, is_vendor_enhanced_fwk);
3405 } else {
3406 is_vendor_enhanced_fwk = 0;
3407 ALOGW("%s: default to non enhanced_fwk config", __func__);
3408 }
3409
3410 return (bool)is_vendor_enhanced_fwk;
3411}
3412
3413int audio_extn_utils_get_vendor_enhanced_info()
3414{
3415 static int vendor_enhanced_info = -EINVAL;
3416 if (vendor_enhanced_info != -EINVAL)
3417 return vendor_enhanced_info;
3418
3419 vndk_fwk_init();
3420
3421 if (mVndkFwk.getVendorEnhancedInfo != NULL) {
3422 vendor_enhanced_info = mVndkFwk.getVendorEnhancedInfo();
3423 ALOGW("%s: vendor_enhanced_info 0x%x", __func__, vendor_enhanced_info);
3424 } else {
3425 vendor_enhanced_info = 0x0;
3426 ALOGW("%s: default to vendor_enhanced_info 0x0", __func__);
3427 }
3428
3429 return vendor_enhanced_info;
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -07003430}
Deeraj Soman14230922019-01-30 16:39:30 +05303431
Deeraj Somanfa377bf2019-02-06 12:57:59 +05303432int audio_extn_utils_get_perf_mode_flag(void)
3433{
3434#ifdef COMPRESSED_PERF_MODE_FLAG
3435 return COMPRESSED_PERF_MODE_FLAG;
3436#else
3437 return 0;
3438#endif
3439}
3440
Deeraj Soman14230922019-01-30 16:39:30 +05303441size_t audio_extn_utils_get_input_buffer_size(uint32_t sample_rate,
3442 audio_format_t format,
3443 int channel_count,
3444 int64_t duration_ms,
3445 bool is_low_latency)
3446{
3447 size_t size = 0;
3448 size_t capture_duration = AUDIO_CAPTURE_PERIOD_DURATION_MSEC;
3449 uint32_t bytes_per_period_sample = 0;
3450
3451
3452 if (audio_extn_utils_check_input_parameters(sample_rate, format, channel_count) != 0)
3453 return 0;
3454
3455 if (duration_ms >= MIN_OFFLOAD_BUFFER_DURATION_MS && duration_ms <= MAX_OFFLOAD_BUFFER_DURATION_MS)
3456 capture_duration = duration_ms;
3457
3458 size = (sample_rate * capture_duration) / 1000;
3459 if (is_low_latency)
3460 size = LOW_LATENCY_CAPTURE_PERIOD_SIZE;
3461
3462
3463 bytes_per_period_sample = audio_bytes_per_sample(format) * channel_count;
3464 size *= bytes_per_period_sample;
3465
3466 /* make sure the size is multiple of 32 bytes and additionally multiple of
3467 * the frame_size (required for 24bit samples and non-power-of-2 channel counts)
3468 * At 48 kHz mono 16-bit PCM:
3469 * 5.000 ms = 240 frames = 15*16*1*2 = 480, a whole multiple of 32 (15)
3470 * 3.333 ms = 160 frames = 10*16*1*2 = 320, a whole multiple of 32 (10)
3471 *
3472 * The loop reaches result within 32 iterations, as initial size is
3473 * already a multiple of frame_size
3474 */
3475 size = audio_extn_utils_nearest_multiple(size, audio_extn_utils_lcm(32, bytes_per_period_sample));
3476
3477 return size;
3478}
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07003479
3480int audio_extn_utils_hash_fn(void *key)
3481{
3482 return (int)key;
3483}
3484
3485bool audio_extn_utils_hash_eq(void *key1, void *key2)
3486{
3487 return (key1 == key2);
3488}