blob: 9a4a848b68e4ff49fa18a6e36a913dd788a527ce [file] [log] [blame]
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001/*
Sriram Kumar9c836482020-10-27 20:34:35 +05302 * Copyright (c) 2014-2021, The Linux Foundation. All rights reserved.
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07003 * Not a Contribution.
4 *
5 * Copyright (C) 2014 The Android Open Source Project
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19
20#define LOG_TAG "audio_hw_utils"
21/* #define LOG_NDEBUG 0 */
22
Manish Dewangan27346042017-03-01 12:56:12 +053023#include <inttypes.h>
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070024#include <errno.h>
25#include <cutils/properties.h>
26#include <cutils/config_utils.h>
27#include <stdlib.h>
28#include <dlfcn.h>
29#include <cutils/str_parms.h>
Aalique Grahame22e49102018-12-18 14:23:57 -080030#include <log/log.h>
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070031#include <cutils/misc.h>
Vinay Vermaaddfa4a2018-04-29 14:03:38 +053032#include <unistd.h>
Surendar Karka287348c2019-04-10 18:31:46 +053033#include <sys/ioctl.h>
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070034
Manish Dewangan07de2142017-02-27 19:27:20 +053035
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070036#include "audio_hw.h"
37#include "platform.h"
38#include "platform_api.h"
39#include "audio_extn.h"
Deeraj Soman14230922019-01-30 16:39:30 +053040#include "voice_extn.h"
Narsinga Rao Chella212e2542014-11-17 19:57:04 -080041#include "voice.h"
Manish Dewangan07de2142017-02-27 19:27:20 +053042#include <sound/compress_params.h>
43#include <sound/compress_offload.h>
Surendar Karka287348c2019-04-10 18:31:46 +053044#include <sound/devdep_params.h>
Manish Dewangan07de2142017-02-27 19:27:20 +053045#include <tinycompress/tinycompress.h>
Revathi Uddaraju1eac8b02017-05-18 17:13:33 +053046
47#ifdef DYNAMIC_LOG_ENABLED
48#include <log_xml_parser.h>
49#define LOG_MASK HAL_MOD_FILE_UTILS
50#include <log_utils.h>
51#endif
52
Ashish Jain81eb2a82015-05-13 10:52:34 +053053#ifdef AUDIO_EXTERNAL_HDMI_ENABLED
Ashish Jain81eb2a82015-05-13 10:52:34 +053054#include "audio_parsers.h"
55#endif
Ashish Jain81eb2a82015-05-13 10:52:34 +053056
Roopesh Nataraja0f68be72020-04-28 12:32:02 -070057#define AUDIO_IO_POLICY_VENDOR_CONFIG_FILE_NAME "audio_io_policy.conf"
58#define AUDIO_OUTPUT_POLICY_VENDOR_CONFIG_FILE_NAME "audio_output_policy.conf"
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070059
60#define OUTPUTS_TAG "outputs"
Dhananjay Kumard6d32152016-10-13 16:11:03 +053061#define INPUTS_TAG "inputs"
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070062
63#define DYNAMIC_VALUE_TAG "dynamic"
64#define FLAGS_TAG "flags"
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +053065#define PROFILES_TAG "profile"
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070066#define FORMATS_TAG "formats"
67#define SAMPLING_RATES_TAG "sampling_rates"
68#define BIT_WIDTH_TAG "bit_width"
69#define APP_TYPE_TAG "app_type"
70
71#define STRING_TO_ENUM(string) { #string, string }
72#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
73
Ben Rombergera04fabc2014-11-14 12:16:03 -080074#define BASE_TABLE_SIZE 64
75#define MAX_BASEINDEX_LEN 256
76
Ben Romberger1aaaf862017-04-06 17:49:46 -070077#ifndef SND_AUDIOCODEC_TRUEHD
78#define SND_AUDIOCODEC_TRUEHD 0x00000023
79#endif
80
Vikram Panduranga93f080e2017-06-07 18:16:14 -070081#define APP_TYPE_VOIP_AUDIO 0x1113A
82
Ashish Jain81eb2a82015-05-13 10:52:34 +053083#ifdef AUDIO_EXTERNAL_HDMI_ENABLED
84#define PROFESSIONAL (1<<0) /* 0 = consumer, 1 = professional */
85#define NON_LPCM (1<<1) /* 0 = audio, 1 = non-audio */
86#define SR_44100 (0<<0) /* 44.1kHz */
87#define SR_NOTID (1<<0) /* non indicated */
88#define SR_48000 (2<<0) /* 48kHz */
89#define SR_32000 (3<<0) /* 32kHz */
90#define SR_22050 (4<<0) /* 22.05kHz */
91#define SR_24000 (6<<0) /* 24kHz */
92#define SR_88200 (8<<0) /* 88.2kHz */
93#define SR_96000 (10<<0) /* 96kHz */
94#define SR_176400 (12<<0) /* 176.4kHz */
95#define SR_192000 (14<<0) /* 192kHz */
96
97#endif
Manish Dewangan07de2142017-02-27 19:27:20 +053098
99/* ToDo: Check and update a proper value in msec */
Weiyin Jiangc49a3b52018-08-17 16:16:08 +0800100#define COMPRESS_OFFLOAD_PLAYBACK_LATENCY 50
101#define PCM_OFFLOAD_PLAYBACK_DSP_PATHDELAY 62
Samyak Jain2546f8a2019-08-29 19:24:21 +0530102#define PCM_OFFLOAD_PLAYBACK_LATENCY PCM_OFFLOAD_PLAYBACK_DSP_PATHDELAY
Manish Dewangan07de2142017-02-27 19:27:20 +0530103
Varun Balaraje49253e2017-07-06 19:48:56 +0530104#ifndef MAX_CHANNELS_SUPPORTED
105#define MAX_CHANNELS_SUPPORTED 8
106#endif
107
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -0700108#ifdef __LP64__
109#define VNDK_FWK_LIB_PATH "/vendor/lib64/libqti_vndfwk_detect.so"
110#else
111#define VNDK_FWK_LIB_PATH "/vendor/lib/libqti_vndfwk_detect.so"
112#endif
113
Kogara Naveen Kumar51bb57d2022-02-17 16:40:06 +0530114/* 24 KHz ECNR support */
115#define ECNS_USE_CASE_ACDB_DEV_ID 95
116#define ECNS_UNSUPPORTED_CAPTURE_SAMPLE_RATE_FOR_ADM 24000
117#define ECNS_SUPPORTED_CAPTURE_SAMPLE_RATE_FOR_ADM 48000
118
Weiyin Jiangd5718bd2019-09-04 16:52:08 +0800119typedef struct vndkfwk_s {
120 void *lib_handle;
121 int (*isVendorEnhancedFwk)(void);
122 int (*getVendorEnhancedInfo)(void);
123 const char *lib_name;
124} vndkfwk_t;
125
126static vndkfwk_t mVndkFwk = {
127 NULL, NULL, NULL, VNDK_FWK_LIB_PATH};
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -0700128
`Deeraj Soman676c2702017-09-18 19:25:53 +0530129typedef struct {
130 const char *id_string;
131 const int value;
132} mixer_config_lookup;
133
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700134struct string_to_enum {
135 const char *name;
136 uint32_t value;
137};
138
139const struct string_to_enum s_flag_name_to_enum_table[] = {
140 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_DIRECT),
141 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_PRIMARY),
142 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_FAST),
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800143 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_RAW),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700144 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_DEEP_BUFFER),
145 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD),
146 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_NON_BLOCKING),
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700147 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_HW_AV_SYNC),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700148 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_INCALL_MUSIC),
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700149 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH),
Naresh Tanniruee3499a2017-01-05 14:05:35 +0530150 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_TIMESTAMP),
Vikram Panduranga93f080e2017-06-07 18:16:14 -0700151 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_VOIP_RX),
Ben Romberger6c4d3812017-06-13 17:46:45 -0700152 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_BD),
Varun Balaraje49253e2017-07-06 19:48:56 +0530153 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_INTERACTIVE),
Derek Chen090dfc52018-03-22 22:15:29 -0400154 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_MEDIA),
155 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_SYS_NOTIFICATION),
156 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_NAV_GUIDANCE),
157 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_PHONE),
Derek Chen1bcdc6b2020-02-06 22:44:53 -0800158 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_FRONT_PASSENGER),
Derek Chendf05eea2019-08-01 13:57:49 -0700159 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_REAR_SEAT),
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530160 STRING_TO_ENUM(AUDIO_INPUT_FLAG_NONE),
161 STRING_TO_ENUM(AUDIO_INPUT_FLAG_FAST),
162 STRING_TO_ENUM(AUDIO_INPUT_FLAG_HW_HOTWORD),
163 STRING_TO_ENUM(AUDIO_INPUT_FLAG_RAW),
164 STRING_TO_ENUM(AUDIO_INPUT_FLAG_SYNC),
Dhananjay Kumaree4d2002016-10-25 18:02:58 +0530165 STRING_TO_ENUM(AUDIO_INPUT_FLAG_TIMESTAMP),
Dhananjay Kumar704ce6f2017-09-28 22:08:00 +0530166 STRING_TO_ENUM(AUDIO_INPUT_FLAG_COMPRESS),
Ralf Herzaec80262018-07-03 07:08:49 +0200167 STRING_TO_ENUM(AUDIO_INPUT_FLAG_PASSTHROUGH),
George Gao3d477082020-09-17 22:29:07 -0700168 STRING_TO_ENUM(AUDIO_INPUT_FLAG_MMAP_NOIRQ),
169 STRING_TO_ENUM(AUDIO_INPUT_FLAG_VOIP_TX),
170 STRING_TO_ENUM(AUDIO_INPUT_FLAG_HW_AV_SYNC),
171 STRING_TO_ENUM(AUDIO_INPUT_FLAG_DIRECT),
Huicheng Liu1404ba12020-09-11 01:03:25 -0400172 STRING_TO_ENUM(AUDIO_INPUT_FLAG_PRIMARY),
173 STRING_TO_ENUM(AUDIO_INPUT_FLAG_FRONT_PASSENGER),
174 STRING_TO_ENUM(AUDIO_INPUT_FLAG_REAR_SEAT),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700175};
176
177const struct string_to_enum s_format_name_to_enum_table[] = {
Ashish Jain83a6cc22016-06-28 14:34:17 +0530178 STRING_TO_ENUM(AUDIO_FORMAT_PCM_8_BIT),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700179 STRING_TO_ENUM(AUDIO_FORMAT_PCM_16_BIT),
Ashish Jain5106d362016-05-11 19:23:33 +0530180 STRING_TO_ENUM(AUDIO_FORMAT_PCM_24_BIT_PACKED),
181 STRING_TO_ENUM(AUDIO_FORMAT_PCM_8_24_BIT),
Ashish Jainf1eaa582016-05-23 20:54:24 +0530182 STRING_TO_ENUM(AUDIO_FORMAT_PCM_32_BIT),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700183 STRING_TO_ENUM(AUDIO_FORMAT_MP3),
184 STRING_TO_ENUM(AUDIO_FORMAT_AAC),
185 STRING_TO_ENUM(AUDIO_FORMAT_VORBIS),
Mingming Yinae3530f2014-07-03 16:50:18 -0700186 STRING_TO_ENUM(AUDIO_FORMAT_AMR_NB),
187 STRING_TO_ENUM(AUDIO_FORMAT_AMR_WB),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700188 STRING_TO_ENUM(AUDIO_FORMAT_AC3),
Mingming Yinae3530f2014-07-03 16:50:18 -0700189 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700190 STRING_TO_ENUM(AUDIO_FORMAT_DTS),
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530191 STRING_TO_ENUM(AUDIO_FORMAT_DTS_HD),
Ben Romberger1aaaf862017-04-06 17:49:46 -0700192 STRING_TO_ENUM(AUDIO_FORMAT_DOLBY_TRUEHD),
Naresh Tanniru928f0862017-04-07 16:44:23 -0700193 STRING_TO_ENUM(AUDIO_FORMAT_IEC61937),
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530194 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3_JOC),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700195 STRING_TO_ENUM(AUDIO_FORMAT_WMA),
196 STRING_TO_ENUM(AUDIO_FORMAT_WMA_PRO),
197 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADIF),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700198 STRING_TO_ENUM(AUDIO_FORMAT_AMR_WB_PLUS),
199 STRING_TO_ENUM(AUDIO_FORMAT_EVRC),
200 STRING_TO_ENUM(AUDIO_FORMAT_EVRCB),
201 STRING_TO_ENUM(AUDIO_FORMAT_EVRCWB),
202 STRING_TO_ENUM(AUDIO_FORMAT_QCELP),
203 STRING_TO_ENUM(AUDIO_FORMAT_MP2),
204 STRING_TO_ENUM(AUDIO_FORMAT_EVRCNW),
Amit Shekhar6f461b12014-08-01 14:52:58 -0700205 STRING_TO_ENUM(AUDIO_FORMAT_FLAC),
Satya Krishna Pindiproli70471602015-04-24 19:12:43 +0530206 STRING_TO_ENUM(AUDIO_FORMAT_ALAC),
207 STRING_TO_ENUM(AUDIO_FORMAT_APE),
Alexy Josephcd8eaed2014-12-11 12:46:53 -0800208 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LC),
209 STRING_TO_ENUM(AUDIO_FORMAT_AAC_HE_V1),
210 STRING_TO_ENUM(AUDIO_FORMAT_AAC_HE_V2),
Manish Dewangana6fc5442015-08-24 20:30:31 +0530211 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS),
Ashish Jaine513a872015-11-19 17:00:56 +0530212 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_LC),
213 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_HE_V1),
214 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_HE_V2),
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +0530215 STRING_TO_ENUM(AUDIO_FORMAT_DSD),
Arun Kumar Dasari3b174182016-12-27 13:01:14 +0530216 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM),
217 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM_LC),
218 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM_HE_V1),
219 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM_HE_V2),
Dhanalakshmi Siddani18737932016-11-29 17:33:17 +0530220 STRING_TO_ENUM(AUDIO_FORMAT_APTX),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700221};
222
Manish Dewangan3ccdea52017-02-13 19:31:54 +0530223/* payload structure avt_device drift query */
224struct audio_avt_device_drift_stats {
225 uint32_t minor_version;
Manish Dewangan338c50a2017-09-12 15:22:03 +0530226
Manish Dewangan3ccdea52017-02-13 19:31:54 +0530227 /* Indicates the device interface direction as either
228 * source (Tx) or sink (Rx).
229 */
230 uint16_t device_direction;
Manish Dewangan338c50a2017-09-12 15:22:03 +0530231
232 /* Reference timer for drift accumulation and time stamp information.
233 * currently it only support AFE_REF_TIMER_TYPE_AVTIMER
234 */
235 uint16_t reference_timer;
Manish Dewangan3ccdea52017-02-13 19:31:54 +0530236 struct audio_avt_device_drift_param drift_param;
Manish Dewangan338c50a2017-09-12 15:22:03 +0530237} __attribute__((packed));
Manish Dewangan3ccdea52017-02-13 19:31:54 +0530238
Ben Rombergera04fabc2014-11-14 12:16:03 -0800239static char bTable[BASE_TABLE_SIZE] = {
240 'A','B','C','D','E','F','G','H','I','J','K','L',
241 'M','N','O','P','Q','R','S','T','U','V','W','X',
242 'Y','Z','a','b','c','d','e','f','g','h','i','j',
243 'k','l','m','n','o','p','q','r','s','t','u','v',
244 'w','x','y','z','0','1','2','3','4','5','6','7',
245 '8','9','+','/'
246};
247
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700248static uint32_t string_to_enum(const struct string_to_enum *table, size_t size,
249 const char *name)
250{
251 size_t i;
252 for (i = 0; i < size; i++) {
253 if (strcmp(table[i].name, name) == 0) {
254 ALOGV("%s found %s", __func__, table[i].name);
255 return table[i].value;
256 }
257 }
George Gao3d477082020-09-17 22:29:07 -0700258 ALOGE("%s cound not find %s", __func__, name);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700259 return 0;
260}
261
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530262static audio_io_flags_t parse_flag_names(char *name)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700263{
264 uint32_t flag = 0;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530265 audio_io_flags_t io_flags;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800266 char *last_r;
267 char *flag_name = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700268 while (flag_name != NULL) {
269 if (strlen(flag_name) != 0) {
270 flag |= string_to_enum(s_flag_name_to_enum_table,
271 ARRAY_SIZE(s_flag_name_to_enum_table),
272 flag_name);
273 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800274 flag_name = strtok_r(NULL, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700275 }
276
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800277 ALOGV("parse_flag_names: flag - %x", flag);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530278 io_flags.in_flags = (audio_input_flags_t)flag;
279 io_flags.out_flags = (audio_output_flags_t)flag;
280 return io_flags;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700281}
282
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530283static void parse_format_names(char *name, struct streams_io_cfg *s_info)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700284{
285 struct stream_format *sf_info = NULL;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800286 char *last_r;
287 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700288
289 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG) == 0)
290 return;
291
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530292 list_init(&s_info->format_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700293 while (str != NULL) {
294 audio_format_t format = (audio_format_t)string_to_enum(s_format_name_to_enum_table,
295 ARRAY_SIZE(s_format_name_to_enum_table), str);
296 ALOGV("%s: format - %d", __func__, format);
297 if (format != 0) {
298 sf_info = (struct stream_format *)calloc(1, sizeof(struct stream_format));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700299 if (sf_info == NULL)
300 break; /* return whatever was parsed */
301
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700302 sf_info->format = format;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530303 list_add_tail(&s_info->format_list, &sf_info->list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700304 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800305 str = strtok_r(NULL, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700306 }
307}
308
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530309static void parse_sample_rate_names(char *name, struct streams_io_cfg *s_info)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700310{
Amit Shekhar6f461b12014-08-01 14:52:58 -0700311 struct stream_sample_rate *ss_info = NULL;
312 uint32_t sample_rate = 48000;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800313 char *last_r;
314 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700315
Amit Shekhar6f461b12014-08-01 14:52:58 -0700316 if (str != NULL && 0 == strcmp(str, DYNAMIC_VALUE_TAG))
317 return;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700318
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530319 list_init(&s_info->sample_rate_list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700320 while (str != NULL) {
321 sample_rate = (uint32_t)strtol(str, (char **)NULL, 10);
322 ALOGV("%s: sample_rate - %d", __func__, sample_rate);
323 if (0 != sample_rate) {
324 ss_info = (struct stream_sample_rate *)calloc(1, sizeof(struct stream_sample_rate));
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800325 if (!ss_info) {
326 ALOGE("%s: memory allocation failure", __func__);
327 return;
328 }
Amit Shekhar6f461b12014-08-01 14:52:58 -0700329 ss_info->sample_rate = sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530330 list_add_tail(&s_info->sample_rate_list, &ss_info->list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700331 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800332 str = strtok_r(NULL, "|", &last_r);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700333 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700334}
335
336static int parse_bit_width_names(char *name)
337{
338 int bit_width = 16;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800339 char *last_r;
340 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700341
342 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG))
343 bit_width = (int)strtol(str, (char **)NULL, 10);
344
345 ALOGV("%s: bit_width - %d", __func__, bit_width);
346 return bit_width;
347}
348
349static int parse_app_type_names(void *platform, char *name)
350{
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700351 int app_type = platform_get_default_app_type(platform);
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800352 char *last_r;
353 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700354
355 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG))
356 app_type = (int)strtol(str, (char **)NULL, 10);
357
358 ALOGV("%s: app_type - %d", __func__, app_type);
359 return app_type;
360}
361
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530362static void update_streams_cfg_list(cnode *root, void *platform,
363 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700364{
365 cnode *node = root->first_child;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530366 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700367
368 ALOGV("%s", __func__);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530369 s_info = (struct streams_io_cfg *)calloc(1, sizeof(struct streams_io_cfg));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700370
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530371 if (!s_info) {
372 ALOGE("failed to allocate mem for s_info list element");
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700373 return;
374 }
375
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700376 while (node) {
377 if (strcmp(node->name, FLAGS_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530378 s_info->flags = parse_flag_names((char *)node->value);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530379 } else if (strcmp(node->name, PROFILES_TAG) == 0) {
380 strlcpy(s_info->profile, (char *)node->value, sizeof(s_info->profile));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700381 } else if (strcmp(node->name, FORMATS_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530382 parse_format_names((char *)node->value, s_info);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700383 } else if (strcmp(node->name, SAMPLING_RATES_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530384 s_info->app_type_cfg.sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
385 parse_sample_rate_names((char *)node->value, s_info);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700386 } else if (strcmp(node->name, BIT_WIDTH_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530387 s_info->app_type_cfg.bit_width = parse_bit_width_names((char *)node->value);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700388 } else if (strcmp(node->name, APP_TYPE_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530389 s_info->app_type_cfg.app_type = parse_app_type_names(platform, (char *)node->value);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700390 }
391 node = node->next;
392 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530393 list_add_tail(streams_cfg_list, &s_info->list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700394}
395
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530396static void load_cfg_list(cnode *root, void *platform,
397 struct listnode *streams_output_cfg_list,
398 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700399{
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530400 cnode *node = NULL;
401
402 node = config_find(root, OUTPUTS_TAG);
403 if (node != NULL) {
404 node = node->first_child;
405 while (node) {
406 ALOGV("%s: loading output %s", __func__, node->name);
407 update_streams_cfg_list(node, platform, streams_output_cfg_list);
408 node = node->next;
409 }
410 } else {
411 ALOGI("%s: could not load output, node is NULL", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700412 }
413
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530414 node = config_find(root, INPUTS_TAG);
415 if (node != NULL) {
416 node = node->first_child;
417 while (node) {
418 ALOGV("%s: loading input %s", __func__, node->name);
419 update_streams_cfg_list(node, platform, streams_input_cfg_list);
420 node = node->next;
421 }
422 } else {
423 ALOGI("%s: could not load input, node is NULL", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700424 }
425}
426
427static void send_app_type_cfg(void *platform, struct mixer *mixer,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530428 struct listnode *streams_output_cfg_list,
429 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700430{
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530431 size_t app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT] = {0};
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700432 int length = 0, i, num_app_types = 0;
433 struct listnode *node;
434 bool update;
435 struct mixer_ctl *ctl = NULL;
436 const char *mixer_ctl_name = "App Type Config";
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530437 struct streams_io_cfg *s_info = NULL;
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800438 uint32_t target_bit_width = 0;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700439
440 if (!mixer) {
441 ALOGE("%s: mixer is null",__func__);
442 return;
443 }
444 ctl = mixer_get_ctl_by_name(mixer, mixer_ctl_name);
445 if (!ctl) {
446 ALOGE("%s: Could not get ctl for mixer cmd - %s",__func__, mixer_ctl_name);
447 return;
448 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530449 app_type_cfg[length++] = num_app_types;
450
451 if (list_empty(streams_output_cfg_list)) {
452 app_type_cfg[length++] = platform_get_default_app_type_v2(platform, PCM_PLAYBACK);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700453 app_type_cfg[length++] = 48000;
454 app_type_cfg[length++] = 16;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530455 num_app_types += 1;
456 }
457 if (list_empty(streams_input_cfg_list)) {
458 app_type_cfg[length++] = platform_get_default_app_type_v2(platform, PCM_CAPTURE);
459 app_type_cfg[length++] = 48000;
460 app_type_cfg[length++] = 16;
461 num_app_types += 1;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700462 }
463
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800464 /* get target bit width for ADM enforce mode */
465 target_bit_width = adev_get_dsp_bit_width_enforce_mode();
466
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700467 list_for_each(node, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530468 s_info = node_to_item(node, struct streams_io_cfg, list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700469 update = true;
470 for (i=0; i<length; i=i+3) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530471 if (app_type_cfg[i+1] == 0)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700472 break;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530473 else if (app_type_cfg[i+1] == (size_t)s_info->app_type_cfg.app_type) {
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530474 if (app_type_cfg[i+2] < (size_t)s_info->app_type_cfg.sample_rate)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530475 app_type_cfg[i+2] = s_info->app_type_cfg.sample_rate;
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530476 if (app_type_cfg[i+3] < (size_t)s_info->app_type_cfg.bit_width)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530477 app_type_cfg[i+3] = s_info->app_type_cfg.bit_width;
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800478 /* ADM bit width = max(enforce_bit_width, bit_width from s_info */
479 if (audio_extn_is_dsp_bit_width_enforce_mode_supported(s_info->flags.out_flags) &&
480 (target_bit_width > app_type_cfg[i+3]))
481 app_type_cfg[i+3] = target_bit_width;
482
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700483 update = false;
484 break;
485 }
486 }
487 if (update && ((length + 3) <= MAX_LENGTH_MIXER_CONTROL_IN_INT)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530488 num_app_types += 1;
489 app_type_cfg[length++] = s_info->app_type_cfg.app_type;
490 app_type_cfg[length++] = s_info->app_type_cfg.sample_rate;
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800491 app_type_cfg[length] = s_info->app_type_cfg.bit_width;
492 if (audio_extn_is_dsp_bit_width_enforce_mode_supported(s_info->flags.out_flags) &&
493 (target_bit_width > app_type_cfg[length]))
494 app_type_cfg[length] = target_bit_width;
495
496 length++;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530497 }
498 }
499 list_for_each(node, streams_input_cfg_list) {
500 s_info = node_to_item(node, struct streams_io_cfg, list);
501 update = true;
502 for (i=0; i<length; i=i+3) {
503 if (app_type_cfg[i+1] == 0)
504 break;
505 else if (app_type_cfg[i+1] == (size_t)s_info->app_type_cfg.app_type) {
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530506 if (app_type_cfg[i+2] < (size_t)s_info->app_type_cfg.sample_rate)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530507 app_type_cfg[i+2] = s_info->app_type_cfg.sample_rate;
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530508 if (app_type_cfg[i+3] < (size_t)s_info->app_type_cfg.bit_width)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530509 app_type_cfg[i+3] = s_info->app_type_cfg.bit_width;
510 update = false;
511 break;
512 }
513 }
514 if (update && ((length + 3) <= MAX_LENGTH_MIXER_CONTROL_IN_INT)) {
515 num_app_types += 1;
516 app_type_cfg[length++] = s_info->app_type_cfg.app_type;
517 app_type_cfg[length++] = s_info->app_type_cfg.sample_rate;
518 app_type_cfg[length++] = s_info->app_type_cfg.bit_width;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700519 }
520 }
521 ALOGV("%s: num_app_types: %d", __func__, num_app_types);
522 if (num_app_types) {
523 app_type_cfg[0] = num_app_types;
524 mixer_ctl_set_array(ctl, app_type_cfg, length);
525 }
526}
527
Roopesh Nataraja0f68be72020-04-28 12:32:02 -0700528/* Function to retrieve audio vendor configs path */
529void audio_get_vendor_config_path (char* config_file_path, int path_size)
530{
531 char vendor_sku[PROPERTY_VALUE_MAX] = {'\0'};
532 if (property_get("ro.boot.product.vendor.sku", vendor_sku, "") <= 0) {
533#ifdef LINUX_ENABLED
534 /* Audio configs are stored in /etc */
535 snprintf(config_file_path, path_size, "%s", "/etc");
536#else
537 /* Audio configs are stored in /vendor/etc */
538 snprintf(config_file_path, path_size, "%s", "/vendor/etc");
539#endif
540 } else {
541 /* Audio configs are stored in /vendor/etc/audio/sku_${vendor_sku} */
542 snprintf(config_file_path, path_size,
543 "%s%s", "/vendor/etc/audio/sku_", vendor_sku);
544 }
545}
546
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530547void audio_extn_utils_update_streams_cfg_lists(void *platform,
548 struct mixer *mixer,
549 struct listnode *streams_output_cfg_list,
550 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700551{
552 cnode *root;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530553 char *data = NULL;
Roopesh Nataraja0f68be72020-04-28 12:32:02 -0700554 char vendor_config_path[VENDOR_CONFIG_PATH_MAX_LENGTH];
555 char audio_io_policy_file[VENDOR_CONFIG_FILE_MAX_LENGTH];
556 char audio_output_policy_file[VENDOR_CONFIG_FILE_MAX_LENGTH];
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700557
558 ALOGV("%s", __func__);
559 list_init(streams_output_cfg_list);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530560 list_init(streams_input_cfg_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700561
562 root = config_node("", "");
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700563 if (root == NULL) {
564 ALOGE("cfg_list, NULL config root");
565 return;
566 }
567
Roopesh Nataraja0f68be72020-04-28 12:32:02 -0700568 /* Get path for audio configuration files in vendor */
569 audio_get_vendor_config_path(vendor_config_path,
570 sizeof(vendor_config_path));
571
572 /* Get path for audio_io_policy_file in vendor */
573 snprintf(audio_io_policy_file, sizeof(audio_io_policy_file),
574 "%s/%s", vendor_config_path, AUDIO_IO_POLICY_VENDOR_CONFIG_FILE_NAME);
575
576 /* Load audio_io_policy_file from vendor */
577 data = (char *)load_file(audio_io_policy_file, NULL);
578
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530579 if (data == NULL) {
580 ALOGD("%s: failed to open io config file(%s), trying older config file",
Roopesh Nataraja0f68be72020-04-28 12:32:02 -0700581 __func__, audio_io_policy_file);
582
583 /* Get path for audio_output_policy_file in vendor */
584 snprintf(audio_output_policy_file, sizeof(audio_output_policy_file),
585 "%s/%s", vendor_config_path,
586 AUDIO_OUTPUT_POLICY_VENDOR_CONFIG_FILE_NAME);
587
588 /* Load audio_output_policy_file from vendor */
589 data = (char *)load_file(audio_output_policy_file, NULL);
590
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530591 if (data == NULL) {
592 send_app_type_cfg(platform, mixer,
593 streams_output_cfg_list,
594 streams_input_cfg_list);
595 ALOGE("%s: could not load io policy config!", __func__);
yidongh6eb4f752017-04-19 18:20:57 +0800596 free(root);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530597 return;
598 }
599 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700600
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530601 config_load(root, data);
602 load_cfg_list(root, platform, streams_output_cfg_list,
603 streams_input_cfg_list);
604
605 send_app_type_cfg(platform, mixer, streams_output_cfg_list,
606 streams_input_cfg_list);
Alexy Josephaee4fdd2016-01-29 13:02:07 -0800607
608 config_free(root);
yidongh6eb4f752017-04-19 18:20:57 +0800609 free(root);
Alexy Josephaee4fdd2016-01-29 13:02:07 -0800610 free(data);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700611}
612
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530613static void audio_extn_utils_dump_streams_cfg_list(
614 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700615{
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700616 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530617 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700618 struct stream_format *sf_info;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700619 struct stream_sample_rate *ss_info;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530620
621 list_for_each(node_i, streams_cfg_list) {
622 s_info = node_to_item(node_i, struct streams_io_cfg, list);
623 ALOGV("%s: flags-%d, sample_rate-%d, bit_width-%d, app_type-%d",
624 __func__, s_info->flags.out_flags, s_info->app_type_cfg.sample_rate,
625 s_info->app_type_cfg.bit_width, s_info->app_type_cfg.app_type);
626 list_for_each(node_j, &s_info->format_list) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700627 sf_info = node_to_item(node_j, struct stream_format, list);
628 ALOGV("format-%x", sf_info->format);
629 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530630 list_for_each(node_j, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700631 ss_info = node_to_item(node_j, struct stream_sample_rate, list);
632 ALOGV("sample rate-%d", ss_info->sample_rate);
633 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700634 }
635}
636
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530637void audio_extn_utils_dump_streams_cfg_lists(
638 struct listnode *streams_output_cfg_list,
639 struct listnode *streams_input_cfg_list)
640{
641 ALOGV("%s", __func__);
642 audio_extn_utils_dump_streams_cfg_list(streams_output_cfg_list);
643 audio_extn_utils_dump_streams_cfg_list(streams_input_cfg_list);
644}
645
646static void audio_extn_utils_release_streams_cfg_list(
647 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700648{
649 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530650 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700651
652 ALOGV("%s", __func__);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530653
654 while (!list_empty(streams_cfg_list)) {
655 node_i = list_head(streams_cfg_list);
656 s_info = node_to_item(node_i, struct streams_io_cfg, list);
657 while (!list_empty(&s_info->format_list)) {
658 node_j = list_head(&s_info->format_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700659 list_remove(node_j);
660 free(node_to_item(node_j, struct stream_format, list));
661 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530662 while (!list_empty(&s_info->sample_rate_list)) {
663 node_j = list_head(&s_info->sample_rate_list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700664 list_remove(node_j);
665 free(node_to_item(node_j, struct stream_sample_rate, list));
666 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700667 list_remove(node_i);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530668 free(node_to_item(node_i, struct streams_io_cfg, list));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700669 }
670}
671
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530672void audio_extn_utils_release_streams_cfg_lists(
673 struct listnode *streams_output_cfg_list,
674 struct listnode *streams_input_cfg_list)
675{
676 ALOGV("%s", __func__);
677 audio_extn_utils_release_streams_cfg_list(streams_output_cfg_list);
678 audio_extn_utils_release_streams_cfg_list(streams_input_cfg_list);
679}
680
681static bool set_app_type_cfg(struct streams_io_cfg *s_info,
682 struct stream_app_type_cfg *app_type_cfg,
683 uint32_t sample_rate, uint32_t bit_width)
Amit Shekhar6f461b12014-08-01 14:52:58 -0700684 {
685 struct listnode *node_i;
686 struct stream_sample_rate *ss_info;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530687 list_for_each(node_i, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700688 ss_info = node_to_item(node_i, struct stream_sample_rate, list);
689 if ((sample_rate <= ss_info->sample_rate) &&
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530690 (bit_width == s_info->app_type_cfg.bit_width)) {
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700691
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530692 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700693 app_type_cfg->sample_rate = ss_info->sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530694 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700695 ALOGV("%s app_type_cfg->app_type %d, app_type_cfg->sample_rate %d, app_type_cfg->bit_width %d",
696 __func__, app_type_cfg->app_type, app_type_cfg->sample_rate, app_type_cfg->bit_width);
697 return true;
698 }
699 }
700 /*
701 * Reiterate through the list assuming dafault sample rate.
702 * Handles scenario where input sample rate is higher
703 * than all sample rates in list for the input bit width.
704 */
705 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
Apoorv Raghuvanshif59bb222015-02-18 12:23:23 -0800706
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530707 list_for_each(node_i, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700708 ss_info = node_to_item(node_i, struct stream_sample_rate, list);
709 if ((sample_rate <= ss_info->sample_rate) &&
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530710 (bit_width == s_info->app_type_cfg.bit_width)) {
711 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700712 app_type_cfg->sample_rate = sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530713 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Apoorv Raghuvanshif59bb222015-02-18 12:23:23 -0800714 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 -0700715 __func__, app_type_cfg->app_type, app_type_cfg->sample_rate, app_type_cfg->bit_width);
716 return true;
717 }
718 }
719 return false;
720}
721
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530722void audio_extn_utils_update_stream_input_app_type_cfg(void *platform,
723 struct listnode *streams_input_cfg_list,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -0800724 struct listnode *devices __unused,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530725 audio_input_flags_t flags,
726 audio_format_t format,
727 uint32_t sample_rate,
728 uint32_t bit_width,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530729 char* profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530730 struct stream_app_type_cfg *app_type_cfg)
731{
732 struct listnode *node_i, *node_j;
733 struct streams_io_cfg *s_info;
734 struct stream_format *sf_info;
735
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530736 ALOGV("%s: flags: 0x%x, format: 0x%x sample_rate %d, profile %s",
737 __func__, flags, format, sample_rate, profile);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530738
739 list_for_each(node_i, streams_input_cfg_list) {
740 s_info = node_to_item(node_i, struct streams_io_cfg, list);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530741 /* Along with flags do profile matching if set at either end.*/
742 if (s_info->flags.in_flags == flags &&
743 ((profile[0] == '\0' && s_info->profile[0] == '\0') ||
744 strncmp(s_info->profile, profile, sizeof(s_info->profile)) == 0)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530745 list_for_each(node_j, &s_info->format_list) {
746 sf_info = node_to_item(node_j, struct stream_format, list);
747 if (sf_info->format == format) {
748 if (set_app_type_cfg(s_info, app_type_cfg, sample_rate, bit_width))
749 return;
750 }
751 }
752 }
753 }
754 ALOGW("%s: App type could not be selected. Falling back to default", __func__);
755 app_type_cfg->app_type = platform_get_default_app_type_v2(platform, PCM_CAPTURE);
756 app_type_cfg->sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
757 app_type_cfg->bit_width = 16;
Kogara Naveen Kumar51bb57d2022-02-17 16:40:06 +0530758 if ((flags & AUDIO_INPUT_FLAG_TIMESTAMP) == 0 &&
759 (flags & AUDIO_INPUT_FLAG_COMPRESS) == 0 &&
760 (flags & AUDIO_INPUT_FLAG_FAST) != 0) {
761 // Support low latency record for different sample rates
762 app_type_cfg->sample_rate = sample_rate;
763 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530764}
765
766void audio_extn_utils_update_stream_output_app_type_cfg(void *platform,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700767 struct listnode *streams_output_cfg_list,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -0800768 struct listnode *devices,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700769 audio_output_flags_t flags,
770 audio_format_t format,
Amit Shekhar6f461b12014-08-01 14:52:58 -0700771 uint32_t sample_rate,
772 uint32_t bit_width,
Manish Dewangan837dc462015-05-27 10:17:41 +0530773 audio_channel_mask_t channel_mask,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530774 char *profile,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700775 struct stream_app_type_cfg *app_type_cfg)
776{
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +0530777 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530778 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700779 struct stream_format *sf_info;
Manish Dewangan837dc462015-05-27 10:17:41 +0530780 char value[PROPERTY_VALUE_MAX] = {0};
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700781
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -0800782 if (compare_device_type(devices, AUDIO_DEVICE_OUT_SPEAKER)) {
Ramu Gottipati074fa4d2018-09-11 20:05:51 +0530783 int bw = platform_get_snd_device_bit_width(SND_DEVICE_OUT_SPEAKER);
784 if ((-ENOSYS != bw) && (bit_width > (uint32_t)bw))
Amit Shekhar5a39c912014-10-14 15:39:30 -0700785 bit_width = (uint32_t)bw;
Ramu Gottipati074fa4d2018-09-11 20:05:51 +0530786 else if (-ENOSYS == bw)
787 bit_width = CODEC_BACKEND_DEFAULT_BIT_WIDTH;
Amit Shekhar1d896042014-10-03 13:16:09 -0700788 sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Aniket Kumar Latad13758f2020-08-06 15:11:36 -0700789 ALOGV("%s Allowing 24 and above bits playback on speaker \
790 ONLY at default sampling rate", __func__);
Amit Shekhar1d896042014-10-03 13:16:09 -0700791 }
792
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -0700793 property_get("vendor.audio.playback.mch.downsample",value,"");
Manish Dewangan837dc462015-05-27 10:17:41 +0530794 if (!strncmp("true", value, sizeof("true"))) {
795 if ((popcount(channel_mask) > 2) &&
796 (sample_rate > CODEC_BACKEND_DEFAULT_SAMPLE_RATE) &&
Aalique Grahame5cd12ff2017-10-19 10:57:00 -0700797 !(flags & (audio_output_flags_t)AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH)) {
Manish Dewangan837dc462015-05-27 10:17:41 +0530798 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
799 ALOGD("%s: MCH session defaulting sample rate to %d",
800 __func__, sample_rate);
801 }
802 }
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +0530803
804 /* Set sampling rate to 176.4 for DSD64
805 * and 352.8Khz for DSD128.
806 * Set Bit Width to 16. output will be 16 bit
807 * post DoP in ASM.
808 */
Aalique Grahame5cd12ff2017-10-19 10:57:00 -0700809 if ((flags & (audio_output_flags_t)AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH) &&
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +0530810 (format == AUDIO_FORMAT_DSD)) {
811 bit_width = 16;
812 if (sample_rate == INPUT_SAMPLING_RATE_DSD64)
813 sample_rate = OUTPUT_SAMPLING_RATE_DSD64;
814 else if (sample_rate == INPUT_SAMPLING_RATE_DSD128)
815 sample_rate = OUTPUT_SAMPLING_RATE_DSD128;
816 }
817
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +0530818
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800819 ALOGV("%s: flags: %x, format: %x sample_rate %d, profile %s, app_type %d",
820 __func__, flags, format, sample_rate, profile, app_type_cfg->app_type);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700821 list_for_each(node_i, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530822 s_info = node_to_item(node_i, struct streams_io_cfg, list);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530823 /* Along with flags do profile matching if set at either end.*/
824 if (s_info->flags.out_flags == flags &&
825 ((profile[0] == '\0' && s_info->profile[0] == '\0') ||
826 strncmp(s_info->profile, profile, sizeof(s_info->profile)) == 0)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530827 list_for_each(node_j, &s_info->format_list) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700828 sf_info = node_to_item(node_j, struct stream_format, list);
829 if (sf_info->format == format) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530830 if (set_app_type_cfg(s_info, app_type_cfg, sample_rate, bit_width))
Amit Shekhar6f461b12014-08-01 14:52:58 -0700831 return;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700832 }
833 }
834 }
835 }
836 list_for_each(node_i, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530837 s_info = node_to_item(node_i, struct streams_io_cfg, list);
838 if (s_info->flags.out_flags == AUDIO_OUTPUT_FLAG_PRIMARY) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700839 ALOGV("Compatible output profile not found.");
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530840 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
841 app_type_cfg->sample_rate = s_info->app_type_cfg.sample_rate;
842 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700843 ALOGV("%s Default to primary output: App type: %d sample_rate %d",
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530844 __func__, s_info->app_type_cfg.app_type, app_type_cfg->sample_rate);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700845 return;
846 }
847 }
848 ALOGW("%s: App type could not be selected. Falling back to default", __func__);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700849 app_type_cfg->app_type = platform_get_default_app_type(platform);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700850 app_type_cfg->sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700851 app_type_cfg->bit_width = 16;
Kogara Naveen Kumar51bb57d2022-02-17 16:40:06 +0530852 if (compare_device_type(devices, AUDIO_DEVICE_OUT_BUS) && (flags &
853 (audio_output_flags_t)AUDIO_OUTPUT_FLAG_FAST)) {
854 // Support low latency playback for different sample rates
855 app_type_cfg->sample_rate = sample_rate;
856 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700857}
858
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +0530859static bool audio_is_this_native_usecase(struct audio_usecase *uc)
860{
861 bool native_usecase = false;
862 struct stream_out *out = (struct stream_out*) uc->stream.out;
863
864 if (PCM_PLAYBACK == uc->type && out != NULL &&
865 NATIVE_AUDIO_MODE_INVALID != platform_get_native_support() &&
866 is_offload_usecase(uc->id) &&
867 (out->sample_rate == OUTPUT_SAMPLING_RATE_44100))
868 native_usecase = true;
869
870 return native_usecase;
871}
872
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800873bool audio_extn_is_dsp_bit_width_enforce_mode_supported(audio_output_flags_t flags)
874{
875 /* DSP bitwidth enforce mode for ADM and AFE:
876 * includes:
877 * deep buffer, low latency, direct pcm and offload.
878 * excludes:
879 * ull(raw+fast), VOIP.
880 */
881 if ((flags & AUDIO_OUTPUT_FLAG_VOIP_RX) ||
882 ((flags & AUDIO_OUTPUT_FLAG_RAW) &&
883 (flags & AUDIO_OUTPUT_FLAG_FAST)))
884 return false;
885
886
887 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) ||
888 (flags & AUDIO_OUTPUT_FLAG_DIRECT) ||
889 (flags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) ||
890 (flags & AUDIO_OUTPUT_FLAG_PRIMARY))
891 return true;
892 else
893 return false;
894}
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800895
896static inline bool audio_is_vr_mode_on(struct audio_device *(__attribute__((unused)) adev))
897{
898 return adev->vr_audio_mode_enabled;
899}
900
Derek Chenf7092792017-05-23 12:23:53 -0400901static void audio_extn_btsco_get_sample_rate(int snd_device, int *sample_rate)
902{
903 switch (snd_device) {
904 case SND_DEVICE_OUT_BT_SCO:
905 case SND_DEVICE_IN_BT_SCO_MIC:
906 case SND_DEVICE_IN_BT_SCO_MIC_NREC:
907 *sample_rate = 8000;
908 break;
909 case SND_DEVICE_OUT_BT_SCO_WB:
910 case SND_DEVICE_IN_BT_SCO_MIC_WB:
911 case SND_DEVICE_IN_BT_SCO_MIC_WB_NREC:
912 *sample_rate = 16000;
913 break;
914 default:
Aniket Kumar Latad13758f2020-08-06 15:11:36 -0700915 ALOGV("%s:Not a BT SCO device, need not update sampling rate\n", __func__);
Derek Chenf7092792017-05-23 12:23:53 -0400916 break;
917 }
918}
919
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530920void audio_extn_utils_update_stream_app_type_cfg_for_usecase(
921 struct audio_device *adev,
922 struct audio_usecase *usecase)
923{
924 ALOGV("%s", __func__);
925
926 switch(usecase->type) {
927 case PCM_PLAYBACK:
928 audio_extn_utils_update_stream_output_app_type_cfg(adev->platform,
929 &adev->streams_output_cfg_list,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -0800930 &usecase->stream.out->device_list,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530931 usecase->stream.out->flags,
Aalique Grahame65780b52017-09-27 14:59:56 -0700932 usecase->stream.out->hal_op_format,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530933 usecase->stream.out->sample_rate,
934 usecase->stream.out->bit_width,
935 usecase->stream.out->channel_mask,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530936 usecase->stream.out->profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530937 &usecase->stream.out->app_type_cfg);
938 ALOGV("%s Selected apptype: %d", __func__, usecase->stream.out->app_type_cfg.app_type);
939 break;
940 case PCM_CAPTURE:
Revathi Uddarajud9f23d92020-07-27 10:55:06 +0530941 if (usecase->id == USECASE_AUDIO_RECORD_VOIP
942 || usecase->id == USECASE_AUDIO_RECORD_VOIP_LOW_LATENCY)
Vikram Panduranga93f080e2017-06-07 18:16:14 -0700943 usecase->stream.in->app_type_cfg.app_type = APP_TYPE_VOIP_AUDIO;
944 else
945 audio_extn_utils_update_stream_input_app_type_cfg(adev->platform,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530946 &adev->streams_input_cfg_list,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -0800947 &usecase->stream.in->device_list,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530948 usecase->stream.in->flags,
949 usecase->stream.in->format,
950 usecase->stream.in->sample_rate,
951 usecase->stream.in->bit_width,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530952 usecase->stream.in->profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530953 &usecase->stream.in->app_type_cfg);
954 ALOGV("%s Selected apptype: %d", __func__, usecase->stream.in->app_type_cfg.app_type);
955 break;
Surendar Karka93cd25a2018-08-28 14:21:37 +0530956 case TRANSCODE_LOOPBACK_RX :
Siddartha Shaik343abc62017-08-08 11:15:25 +0530957 audio_extn_utils_update_stream_output_app_type_cfg(adev->platform,
958 &adev->streams_output_cfg_list,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -0800959 &usecase->stream.inout->out_config.device_list,
Siddartha Shaik343abc62017-08-08 11:15:25 +0530960 0,
961 usecase->stream.inout->out_config.format,
962 usecase->stream.inout->out_config.sample_rate,
963 usecase->stream.inout->out_config.bit_width,
964 usecase->stream.inout->out_config.channel_mask,
965 usecase->stream.inout->profile,
966 &usecase->stream.inout->out_app_type_cfg);
967 ALOGV("%s Selected apptype: %d", __func__, usecase->stream.inout->out_app_type_cfg.app_type);
968 break;
Derek Chenf7092792017-05-23 12:23:53 -0400969 case PCM_HFP_CALL:
970 switch (usecase->id) {
971 case USECASE_AUDIO_HFP_SCO:
972 case USECASE_AUDIO_HFP_SCO_WB:
973 audio_extn_btsco_get_sample_rate(usecase->out_snd_device,
974 &usecase->out_app_type_cfg.sample_rate);
Derek Chend7307012019-11-12 12:10:41 -0500975 usecase->in_app_type_cfg.sample_rate = usecase->out_app_type_cfg.sample_rate;
Derek Chenf7092792017-05-23 12:23:53 -0400976 break;
977 case USECASE_AUDIO_HFP_SCO_DOWNLINK:
978 case USECASE_AUDIO_HFP_SCO_WB_DOWNLINK:
979 audio_extn_btsco_get_sample_rate(usecase->in_snd_device,
980 &usecase->in_app_type_cfg.sample_rate);
Derek Chend7307012019-11-12 12:10:41 -0500981 usecase->out_app_type_cfg.sample_rate = usecase->in_app_type_cfg.sample_rate;
Derek Chenf7092792017-05-23 12:23:53 -0400982 break;
983 default:
984 ALOGE("%s: usecase id (%d) not supported, use default sample rate",
985 __func__, usecase->id);
986 usecase->in_app_type_cfg.sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
987 usecase->out_app_type_cfg.sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
988 break;
989 }
990 /* update out_app_type_cfg */
991 usecase->out_app_type_cfg.bit_width =
992 platform_get_snd_device_bit_width(usecase->out_snd_device);
993 usecase->out_app_type_cfg.app_type =
994 platform_get_default_app_type_v2(adev->platform, PCM_PLAYBACK);
995 /* update in_app_type_cfg */
996 usecase->in_app_type_cfg.bit_width =
997 platform_get_snd_device_bit_width(usecase->in_snd_device);
998 usecase->in_app_type_cfg.app_type =
999 platform_get_default_app_type_v2(adev->platform, PCM_CAPTURE);
1000 ALOGV("%s Selected apptype: playback %d capture %d",
1001 __func__, usecase->out_app_type_cfg.app_type, usecase->in_app_type_cfg.app_type);
1002 break;
Derek Chena30a5f42019-12-03 11:17:09 -05001003 case ICC_CALL:
1004 /* ICC usecase: Loopback from TERT_TDM_TX to TERT_TDM_RX */
1005 /* update out_app_type_cfg */
1006 usecase->out_app_type_cfg.sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
1007 usecase->out_app_type_cfg.bit_width = platform_get_snd_device_bit_width(usecase->out_snd_device);
1008 usecase->out_app_type_cfg.app_type = platform_get_default_app_type_v2(adev->platform, PCM_PLAYBACK);
1009 /* update in_app_type_cfg */
1010 usecase->in_app_type_cfg.sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
1011 usecase->in_app_type_cfg.bit_width = platform_get_snd_device_bit_width(usecase->in_snd_device);
1012 usecase->in_app_type_cfg.app_type = platform_get_default_app_type_v2(adev->platform, PCM_CAPTURE);
1013
1014 ALOGV("%s Selected apptype: playback %d capture %d",
1015 __func__, usecase->out_app_type_cfg.app_type, usecase->in_app_type_cfg.app_type);
1016 break;
Fei Tongaffdf732020-02-20 20:39:05 +08001017 case SYNTH_LOOPBACK:
1018 /* update out_app_type_cfg */
1019 usecase->out_app_type_cfg.sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
1020 usecase->out_app_type_cfg.bit_width =
1021 platform_get_snd_device_bit_width(usecase->out_snd_device);
1022 usecase->out_app_type_cfg.app_type =
1023 platform_get_default_app_type_v2(adev->platform, PCM_PLAYBACK);
1024 /* update in_app_type_cfg */
1025 usecase->in_app_type_cfg.sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
1026 usecase->in_app_type_cfg.bit_width =
1027 platform_get_snd_device_bit_width(usecase->in_snd_device);
1028 usecase->in_app_type_cfg.app_type =
1029 platform_get_default_app_type_v2(adev->platform, PCM_CAPTURE);
1030 ALOGV("%s Selected apptype: playback %d capture %d",
1031 __func__, usecase->out_app_type_cfg.app_type, usecase->in_app_type_cfg.app_type);
1032 break;
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301033 default:
1034 ALOGE("%s: app type cfg not supported for usecase type (%d)",
1035 __func__, usecase->type);
1036 }
1037}
1038
Aalique Grahame22e49102018-12-18 14:23:57 -08001039static int set_stream_app_type_mixer_ctrl(struct audio_device *adev,
1040 int pcm_device_id, int app_type,
1041 int acdb_dev_id, int sample_rate,
1042 int stream_type,
1043 snd_device_t snd_device)
1044{
1045
1046 char mixer_ctl_name[MAX_LENGTH_MIXER_CONTROL_IN_INT];
1047 struct mixer_ctl *ctl;
1048 int app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT], len = 0, rc = 0;
1049 int snd_device_be_idx = -1;
1050
1051 if (stream_type == PCM_PLAYBACK) {
1052 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
1053 "Audio Stream %d App Type Cfg", pcm_device_id);
1054 } else if (stream_type == PCM_CAPTURE) {
1055 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
1056 "Audio Stream Capture %d App Type Cfg", pcm_device_id);
1057 }
1058
1059 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
1060 if (!ctl) {
1061 ALOGE("%s: Could not get ctl for mixer cmd - %s",
1062 __func__, mixer_ctl_name);
1063 rc = -EINVAL;
1064 goto exit;
1065 }
1066 app_type_cfg[len++] = app_type;
1067 app_type_cfg[len++] = acdb_dev_id;
1068 app_type_cfg[len++] = sample_rate;
1069
1070 snd_device_be_idx = platform_get_snd_device_backend_index(snd_device);
1071 if (snd_device_be_idx > 0)
1072 app_type_cfg[len++] = snd_device_be_idx;
1073 ALOGV("%s: stream type %d app_type %d, acdb_dev_id %d "
1074 "sample rate %d, snd_device_be_idx %d",
1075 __func__, stream_type, app_type, acdb_dev_id, sample_rate,
1076 snd_device_be_idx);
1077 mixer_ctl_set_array(ctl, app_type_cfg, len);
1078
1079exit:
1080 return rc;
1081}
1082
1083static int audio_extn_utils_send_app_type_cfg_hfp(struct audio_device *adev,
1084 struct audio_usecase *usecase)
1085{
1086 int pcm_device_id, acdb_dev_id = 0, snd_device = usecase->out_snd_device;
1087 int32_t sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
1088 int app_type = 0, rc = 0;
Derek Chenf7092792017-05-23 12:23:53 -04001089 bool is_bus_dev_usecase = false;
Aalique Grahame22e49102018-12-18 14:23:57 -08001090
1091 ALOGV("%s", __func__);
1092
1093 if (usecase->type != PCM_HFP_CALL) {
Derek Chenf7092792017-05-23 12:23:53 -04001094 ALOGV("%s: not a HFP path, no need to cfg app type", __func__);
Aalique Grahame22e49102018-12-18 14:23:57 -08001095 rc = 0;
1096 goto exit_send_app_type_cfg;
1097 }
1098 if ((usecase->id != USECASE_AUDIO_HFP_SCO) &&
Derek Chenf7092792017-05-23 12:23:53 -04001099 (usecase->id != USECASE_AUDIO_HFP_SCO_WB) &&
1100 (usecase->id != USECASE_AUDIO_HFP_SCO_DOWNLINK) &&
1101 (usecase->id != USECASE_AUDIO_HFP_SCO_WB_DOWNLINK)) {
1102 ALOGV("%s: a usecase where app type cfg is not required", __func__);
Aalique Grahame22e49102018-12-18 14:23:57 -08001103 rc = 0;
1104 goto exit_send_app_type_cfg;
1105 }
1106
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08001107 if (compare_device_type(&usecase->device_list, AUDIO_DEVICE_OUT_BUS))
Derek Chenf7092792017-05-23 12:23:53 -04001108 is_bus_dev_usecase = true;
1109
Aalique Grahame22e49102018-12-18 14:23:57 -08001110 snd_device = usecase->out_snd_device;
1111 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_PLAYBACK);
1112
1113 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
1114 if (acdb_dev_id < 0) {
1115 ALOGE("%s: Couldn't get the acdb dev id", __func__);
1116 rc = -EINVAL;
1117 goto exit_send_app_type_cfg;
1118 }
1119
sriram kumar53c4c972021-02-18 16:49:33 +05301120 /*
1121 * Value of afe_loopback gets read based on the property defined in
1122 * audio_platform_info.xml. If afe loopback is set then do not execute
1123 * session 1 path as app type mixer control will not be created for
1124 * afe loopback
1125 */
1126
Aalique Grahame22e49102018-12-18 14:23:57 -08001127 if (usecase->type == PCM_HFP_CALL) {
sriram kumar53c4c972021-02-18 16:49:33 +05301128 if (!(platform_get_is_afe_loopback_enabled(adev->platform))) {
1129 /* config HFP session:1 playback path */
1130 if (is_bus_dev_usecase) {
1131 app_type = usecase->out_app_type_cfg.app_type;
1132 sample_rate= usecase->out_app_type_cfg.sample_rate;
1133 } else {
1134 snd_device = SND_DEVICE_NONE; // use legacy behavior
1135 app_type = platform_get_default_app_type_v2(adev->platform, PCM_PLAYBACK);
1136 sample_rate= CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
Derek Chenf7092792017-05-23 12:23:53 -04001137 }
sriram kumar53c4c972021-02-18 16:49:33 +05301138 rc = set_stream_app_type_mixer_ctrl(adev, pcm_device_id, app_type,
1139 acdb_dev_id, sample_rate,
1140 PCM_PLAYBACK,
1141 snd_device);
1142 if (rc < 0)
1143 goto exit_send_app_type_cfg;
1144
1145 /* config HFP session:1 capture path */
1146 if (is_bus_dev_usecase) {
1147 snd_device = usecase->in_snd_device;
1148 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_CAPTURE);
1149 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
1150 if (acdb_dev_id < 0) {
1151 ALOGE("%s: Couldn't get the acdb dev id", __func__);
1152 rc = -EINVAL;
1153 goto exit_send_app_type_cfg;
1154 }
1155 app_type = usecase->in_app_type_cfg.app_type;
1156 sample_rate= usecase->in_app_type_cfg.sample_rate;
1157 } else {
1158 snd_device = SND_DEVICE_NONE; // use legacy behavior
1159 app_type = platform_get_default_app_type_v2(adev->platform, PCM_CAPTURE);
1160 }
1161 rc = set_stream_app_type_mixer_ctrl(adev, pcm_device_id, app_type,
1162 acdb_dev_id, sample_rate,
1163 PCM_CAPTURE,
1164 snd_device);
1165 if (rc < 0)
1166 goto exit_send_app_type_cfg;
1167
1168 if (is_bus_dev_usecase)
1169 goto exit_send_app_type_cfg;
Derek Chenf7092792017-05-23 12:23:53 -04001170 }
Aalique Grahame22e49102018-12-18 14:23:57 -08001171 /* config HFP session:2 capture path */
1172 pcm_device_id = HFP_ASM_RX_TX;
1173 snd_device = usecase->in_snd_device;
1174 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
1175 if (acdb_dev_id <= 0) {
1176 ALOGE("%s: Couldn't get the acdb dev id", __func__);
1177 rc = -EINVAL;
1178 goto exit_send_app_type_cfg;
1179 }
1180 app_type = platform_get_default_app_type_v2(adev->platform, PCM_CAPTURE);
1181 rc = set_stream_app_type_mixer_ctrl(adev, pcm_device_id, app_type,
1182 acdb_dev_id, sample_rate, PCM_CAPTURE,
sriram kumar53c4c972021-02-18 16:49:33 +05301183 usecase->in_snd_device);
Aalique Grahame22e49102018-12-18 14:23:57 -08001184 if (rc < 0)
1185 goto exit_send_app_type_cfg;
1186
1187 /* config HFP session:2 playback path */
1188 app_type = platform_get_default_app_type_v2(adev->platform, PCM_PLAYBACK);
1189 rc = set_stream_app_type_mixer_ctrl(adev, pcm_device_id, app_type,
1190 acdb_dev_id, sample_rate,
sriram kumar53c4c972021-02-18 16:49:33 +05301191 PCM_PLAYBACK, usecase->out_snd_device);
Aalique Grahame22e49102018-12-18 14:23:57 -08001192 if (rc < 0)
1193 goto exit_send_app_type_cfg;
1194 }
1195
1196 rc = 0;
1197exit_send_app_type_cfg:
1198 return rc;
1199}
1200
Derek Chena30a5f42019-12-03 11:17:09 -05001201int audio_extn_utils_send_app_type_cfg_icc(struct audio_device *adev,
1202 struct audio_usecase *usecase)
1203{
1204 int pcm_device_id, acdb_dev_id = 0, snd_device = usecase->out_snd_device;
1205 int32_t sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
1206 int app_type = 0, rc = 0;
1207
1208 ALOGV("%s", __func__);
1209
1210 if (usecase->type != ICC_CALL) {
1211 ALOGV("%s: not an ICC path, no need to cfg app type", __func__);
1212 rc = 0;
1213 goto exit_send_app_type_cfg;
1214 }
1215 if (usecase->id != USECASE_ICC_CALL) {
1216 ALOGV("%s: a usecase where app type cfg is not required", __func__);
1217 rc = 0;
1218 goto exit_send_app_type_cfg;
1219 }
1220
1221 snd_device = usecase->out_snd_device;
1222 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_PLAYBACK);
1223 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
1224 if (acdb_dev_id < 0) {
1225 ALOGE("%s: Couldn't get the acdb dev id", __func__);
1226 rc = -EINVAL;
1227 goto exit_send_app_type_cfg;
1228 }
1229 /* config ICC session: playback path */
1230 app_type = usecase->out_app_type_cfg.app_type;
1231 sample_rate= usecase->out_app_type_cfg.sample_rate;
1232
1233 rc = set_stream_app_type_mixer_ctrl(adev, pcm_device_id, app_type,
1234 acdb_dev_id, sample_rate,
1235 PCM_PLAYBACK,
1236 snd_device);
1237 if (rc < 0)
1238 goto exit_send_app_type_cfg;
1239
1240 /* config ICC session: capture path */
1241 snd_device = usecase->in_snd_device;
1242 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_CAPTURE);
1243 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
1244 if (acdb_dev_id < 0) {
1245 ALOGE("%s: Couldn't get the acdb dev id", __func__);
1246 rc = -EINVAL;
1247 goto exit_send_app_type_cfg;
1248 }
1249 app_type = usecase->in_app_type_cfg.app_type;
1250 sample_rate= usecase->in_app_type_cfg.sample_rate;
1251 rc = set_stream_app_type_mixer_ctrl(adev, pcm_device_id, app_type,
1252 acdb_dev_id, sample_rate,
1253 PCM_CAPTURE,
1254 snd_device);
1255exit_send_app_type_cfg:
1256 return rc;
1257}
1258
Fei Tongaffdf732020-02-20 20:39:05 +08001259static int audio_extn_utils_send_app_type_cfg_synth(struct audio_device *adev,
1260 struct audio_usecase *usecase)
1261{
1262 int pcm_device_id, acdb_dev_id = 0, snd_device = usecase->out_snd_device;
1263 int32_t sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
1264 int app_type = 0, rc = 0;
1265 bool is_bus_dev_usecase = false;
1266
1267 ALOGV("%s", __func__);
1268
1269 if (usecase->type != SYNTH_LOOPBACK) {
1270 ALOGV("%s: not a SYNTH path, no need to cfg app type", __func__);
1271 rc = 0;
1272 goto exit_send_app_type_cfg;
1273 }
1274 if (usecase->id != USECASE_AUDIO_PLAYBACK_SYNTHESIZER) {
1275 ALOGV("%s: a usecase where app type cfg is not required", __func__);
1276 rc = 0;
1277 goto exit_send_app_type_cfg;
1278 }
1279
1280 if (compare_device_type(&usecase->device_list, AUDIO_DEVICE_OUT_BUS)) {
1281 is_bus_dev_usecase = true;
1282 }
1283
1284 snd_device = usecase->out_snd_device;
1285 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_PLAYBACK);
1286
1287 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
1288 if (acdb_dev_id < 0) {
1289 ALOGE("%s: Couldn't get the acdb dev id", __func__);
1290 rc = -EINVAL;
1291 goto exit_send_app_type_cfg;
1292 }
1293
1294 if (usecase->type == SYNTH_LOOPBACK) {
1295 /* config SYNTH session: playback path */
1296 if (is_bus_dev_usecase) {
1297 app_type = usecase->out_app_type_cfg.app_type;
1298 sample_rate= usecase->out_app_type_cfg.sample_rate;
1299 } else {
1300 snd_device = SND_DEVICE_NONE; // use legacy behavior
1301 app_type = platform_get_default_app_type_v2(adev->platform, PCM_PLAYBACK);
1302 sample_rate= CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
1303 }
1304 rc = set_stream_app_type_mixer_ctrl(adev, pcm_device_id, app_type,
1305 acdb_dev_id, sample_rate,
1306 PCM_PLAYBACK,
1307 snd_device);
1308 if (rc < 0)
1309 goto exit_send_app_type_cfg;
1310 }
1311
1312 rc = 0;
1313exit_send_app_type_cfg:
1314 return rc;
1315}
1316
Zhou Song06761dd2019-04-28 18:08:17 +08001317int audio_extn_utils_get_app_sample_rate_for_device(
1318 struct audio_device *adev,
1319 struct audio_usecase *usecase, int snd_device)
1320{
1321 char value[PROPERTY_VALUE_MAX] = {0};
1322 int sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Kogara Naveen Kumar51bb57d2022-02-17 16:40:06 +05301323 int acdb_dev_id;
Zhou Song06761dd2019-04-28 18:08:17 +08001324
1325 if ((usecase->type == PCM_PLAYBACK) && (usecase->stream.out != NULL)) {
1326 property_get("vendor.audio.playback.mch.downsample",value,"");
1327 if (!strncmp("true", value, sizeof("true"))) {
1328 if ((popcount(usecase->stream.out->channel_mask) > 2) &&
1329 (usecase->stream.out->app_type_cfg.sample_rate > CODEC_BACKEND_DEFAULT_SAMPLE_RATE) &&
1330 !(usecase->stream.out->flags &
1331 (audio_output_flags_t)AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH))
1332 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
1333 }
1334
1335 if (usecase->id == USECASE_AUDIO_PLAYBACK_VOIP) {
1336 usecase->stream.out->app_type_cfg.sample_rate = usecase->stream.out->sample_rate;
Zhou Song06761dd2019-04-28 18:08:17 +08001337 } else if ((snd_device == SND_DEVICE_OUT_HDMI ||
1338 snd_device == SND_DEVICE_OUT_USB_HEADSET ||
1339 snd_device == SND_DEVICE_OUT_DISPLAY_PORT) &&
1340 (usecase->stream.out->sample_rate >= OUTPUT_SAMPLING_RATE_44100)) {
1341 /*
1342 * To best utlize DSP, check if the stream sample rate is supported/multiple of
1343 * configured device sample rate, if not update the COPP rate to be equal to the
1344 * device sample rate, else open COPP at stream sample rate
1345 */
1346 platform_check_and_update_copp_sample_rate(adev->platform, snd_device,
1347 usecase->stream.out->sample_rate,
1348 &usecase->stream.out->app_type_cfg.sample_rate);
Lakshman Chaluvaraju5d0894e2021-01-27 23:22:58 +05301349 } else if (snd_device == SND_DEVICE_OUT_BT_A2DP) {
Zhou Song06761dd2019-04-28 18:08:17 +08001350 /*
1351 * For a2dp playback get encoder sampling rate and set copp sampling rate,
1352 * for bit width use the stream param only.
1353 */
1354 audio_extn_a2dp_get_enc_sample_rate(&usecase->stream.out->app_type_cfg.sample_rate);
1355 ALOGI("%s using %d sample rate rate for A2DP CoPP",
1356 __func__, usecase->stream.out->app_type_cfg.sample_rate);
sriram kumar68e21132021-06-25 19:27:20 +05301357 } else if ((((snd_device != SND_DEVICE_OUT_HEADPHONES_44_1 &&
1358 !audio_is_this_native_usecase(usecase)) &&
1359 usecase->stream.out->sample_rate == OUTPUT_SAMPLING_RATE_44100) ||
1360 (usecase->stream.out->sample_rate < OUTPUT_SAMPLING_RATE_44100)) ||
1361 (compare_device_type(&usecase->stream.out->device_list,AUDIO_DEVICE_OUT_SPEAKER))) {
Kogara Naveen Kumar51bb57d2022-02-17 16:40:06 +05301362 if (!((compare_device_type(&usecase->device_list, AUDIO_DEVICE_OUT_BUS)) && ((usecase->stream.out->flags &
1363 (audio_output_flags_t)AUDIO_OUTPUT_FLAG_SYS_NOTIFICATION) || (usecase->stream.out->flags &
1364 (audio_output_flags_t)AUDIO_OUTPUT_FLAG_PHONE)))) {
1365 /* Reset to default if no native stream is active or default device is speaker*/
1366 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
1367 }
Zhou Song06761dd2019-04-28 18:08:17 +08001368 }
1369 audio_extn_btsco_get_sample_rate(snd_device, &usecase->stream.out->app_type_cfg.sample_rate);
1370 sample_rate = usecase->stream.out->app_type_cfg.sample_rate;
1371
1372 if (((usecase->stream.out->format == AUDIO_FORMAT_E_AC3) ||
1373 (usecase->stream.out->format == AUDIO_FORMAT_E_AC3_JOC) ||
1374 (usecase->stream.out->format == AUDIO_FORMAT_DOLBY_TRUEHD))
1375 && audio_extn_passthru_is_passthrough_stream(usecase->stream.out)
1376 && !audio_extn_passthru_is_convert_supported(adev, usecase->stream.out)) {
1377 sample_rate = sample_rate * 4;
1378 if (sample_rate > HDMI_PASSTHROUGH_MAX_SAMPLE_RATE)
1379 sample_rate = HDMI_PASSTHROUGH_MAX_SAMPLE_RATE;
1380 }
1381 } else if (usecase->type == PCM_CAPTURE) {
1382 if (usecase->stream.in != NULL) {
Revathi Uddarajud9f23d92020-07-27 10:55:06 +05301383 if (usecase->id == USECASE_AUDIO_RECORD_VOIP
1384 || usecase->id == USECASE_AUDIO_RECORD_VOIP_LOW_LATENCY)
Zhou Song06761dd2019-04-28 18:08:17 +08001385 usecase->stream.in->app_type_cfg.sample_rate = usecase->stream.in->sample_rate;
1386 if (voice_is_in_call_rec_stream(usecase->stream.in)) {
1387 audio_extn_btsco_get_sample_rate(usecase->in_snd_device,
1388 &usecase->stream.in->app_type_cfg.sample_rate);
Zhou Songab94f062021-05-12 13:11:17 +08001389 } if (SND_DEVICE_IN_BT_A2DP == snd_device) {
1390 audio_extn_a2dp_get_dec_sample_rate(&usecase->stream.in->app_type_cfg.sample_rate);
Zhou Song06761dd2019-04-28 18:08:17 +08001391 } else {
1392 audio_extn_btsco_get_sample_rate(snd_device,
1393 &usecase->stream.in->app_type_cfg.sample_rate);
1394 }
1395 sample_rate = usecase->stream.in->app_type_cfg.sample_rate;
1396 } else if (usecase->id == USECASE_AUDIO_SPKR_CALIB_TX) {
Sujin Panickerb6d76262019-09-09 10:55:38 +05301397 if ((property_get("vendor.audio.spkr_prot.tx.sampling_rate", value, NULL) > 0))
1398 sample_rate = atoi(value);
1399 else
1400 sample_rate = SAMPLE_RATE_8000;
Zhou Song06761dd2019-04-28 18:08:17 +08001401 }
Kogara Naveen Kumar51bb57d2022-02-17 16:40:06 +05301402
1403 /* ECNR module in DSP does not support 24 KHz sample rate. As a workaround,
1404 run ADM at 48 KHz when ECNR is enabled in ACDB topology (e.g. device id = 95)
1405 */
1406 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
1407 if (sample_rate == ECNS_UNSUPPORTED_CAPTURE_SAMPLE_RATE_FOR_ADM && acdb_dev_id == ECNS_USE_CASE_ACDB_DEV_ID) {
1408 sample_rate = ECNS_SUPPORTED_CAPTURE_SAMPLE_RATE_FOR_ADM;
1409 ALOGD("%s: update sample rate from 24K to 48K to support ECNR in PCM_CAPTURE, sample_rate=%d",__func__,sample_rate);
1410 }
Zhou Song06761dd2019-04-28 18:08:17 +08001411 } else if (usecase->type == TRANSCODE_LOOPBACK_RX) {
1412 sample_rate = usecase->stream.inout->out_config.sample_rate;
1413 }
1414 return sample_rate;
1415}
1416
Siena Richard7c2db772016-12-21 11:32:34 -08001417static int send_app_type_cfg_for_device(struct audio_device *adev,
1418 struct audio_usecase *usecase,
1419 int split_snd_device)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001420{
1421 char mixer_ctl_name[MAX_LENGTH_MIXER_CONTROL_IN_INT];
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301422 size_t app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT] = {0};
1423 int len = 0, rc;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001424 struct mixer_ctl *ctl;
Siena Richard7c2db772016-12-21 11:32:34 -08001425 int pcm_device_id = 0, acdb_dev_id, app_type;
1426 int snd_device = split_snd_device, snd_device_be_idx = -1;
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +05301427 int32_t sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Varun Balaraje49253e2017-07-06 19:48:56 +05301428 struct streams_io_cfg *s_info = NULL;
1429 struct listnode *node = NULL;
Nikhil Laturkarac4a7492017-08-31 18:27:19 +05301430 int bd_app_type = 0;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001431
Siena Richard7c2db772016-12-21 11:32:34 -08001432 ALOGV("%s: usecase->out_snd_device %s, usecase->in_snd_device %s, split_snd_device %s",
1433 __func__, platform_get_snd_device_name(usecase->out_snd_device),
1434 platform_get_snd_device_name(usecase->in_snd_device),
1435 platform_get_snd_device_name(split_snd_device));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001436
Siddartha Shaik343abc62017-08-08 11:15:25 +05301437 if (usecase->type != PCM_PLAYBACK && usecase->type != PCM_CAPTURE &&
Surendar Karka93cd25a2018-08-28 14:21:37 +05301438 usecase->type != TRANSCODE_LOOPBACK_RX) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301439 ALOGE("%s: not a playback/capture path, no need to cfg app type", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001440 rc = 0;
1441 goto exit_send_app_type_cfg;
1442 }
1443 if ((usecase->id != USECASE_AUDIO_PLAYBACK_DEEP_BUFFER) &&
Vignesh Kulothungan0a2eff02019-07-11 16:30:13 -07001444 (usecase->id != USECASE_AUDIO_PLAYBACK_WITH_HAPTICS) &&
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001445 (usecase->id != USECASE_AUDIO_PLAYBACK_LOW_LATENCY) &&
1446 (usecase->id != USECASE_AUDIO_PLAYBACK_MULTI_CH) &&
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -08001447 (usecase->id != USECASE_AUDIO_PLAYBACK_ULL) &&
Vikram Panduranga93f080e2017-06-07 18:16:14 -07001448 (usecase->id != USECASE_AUDIO_PLAYBACK_VOIP) &&
Surendar Karka93cd25a2018-08-28 14:21:37 +05301449 (usecase->id != USECASE_AUDIO_TRANSCODE_LOOPBACK_RX) &&
Varun Balaraje49253e2017-07-06 19:48:56 +05301450 (!is_interactive_usecase(usecase->id)) &&
Srikanth Uyyala9d551402015-08-25 16:03:42 +05301451 (!is_offload_usecase(usecase->id)) &&
Derek Chenf6318be2017-06-12 17:16:24 -04001452 (usecase->type != PCM_CAPTURE) &&
1453 (!audio_extn_auto_hal_is_bus_device_usecase(usecase->id))) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301454 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 -07001455 rc = 0;
1456 goto exit_send_app_type_cfg;
1457 }
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -08001458
1459 //if VR is active then only send the mixer control
1460 if (usecase->id == USECASE_AUDIO_PLAYBACK_ULL && !audio_is_vr_mode_on(adev)) {
1461 ALOGI("ULL doesnt need sending app type cfg, returning");
1462 rc = 0;
1463 goto exit_send_app_type_cfg;
1464 }
1465
Surendar Karka93cd25a2018-08-28 14:21:37 +05301466 if (usecase->type == PCM_PLAYBACK || usecase->type == TRANSCODE_LOOPBACK_RX) {
Ben Romberger1fafdde2015-09-09 19:43:15 -07001467 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_PLAYBACK);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301468 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
1469 "Audio Stream %d App Type Cfg", pcm_device_id);
Ben Romberger1fafdde2015-09-09 19:43:15 -07001470 } else if (usecase->type == PCM_CAPTURE) {
Ben Romberger1fafdde2015-09-09 19:43:15 -07001471 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_CAPTURE);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301472 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
1473 "Audio Stream Capture %d App Type Cfg", pcm_device_id);
Srikanth Uyyala9d551402015-08-25 16:03:42 +05301474 }
Siena Richard7c2db772016-12-21 11:32:34 -08001475
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001476 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
1477 if (!ctl) {
1478 ALOGE("%s: Could not get ctl for mixer cmd - %s", __func__,
1479 mixer_ctl_name);
1480 rc = -EINVAL;
1481 goto exit_send_app_type_cfg;
1482 }
Aditya Bavanari701a6992017-03-30 19:17:16 +05301483 snd_device = platform_get_spkr_prot_snd_device(snd_device);
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301484 if (voice_is_in_call_rec_stream(usecase->stream.in) && usecase->type == PCM_CAPTURE) {
1485 snd_device_t voice_device = voice_get_incall_rec_snd_device(usecase->in_snd_device);
1486 acdb_dev_id = platform_get_snd_device_acdb_id(voice_device);
1487 ALOGV("acdb id for voice call use case %d", acdb_dev_id);
1488 } else {
1489 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
1490 }
Rohit Kumar1181b292017-01-31 18:07:17 +05301491 if (acdb_dev_id <= 0) {
1492 ALOGE("%s: Couldn't get the acdb dev id", __func__);
1493 rc = -EINVAL;
1494 goto exit_send_app_type_cfg;
1495 }
1496
Siena Richard7c2db772016-12-21 11:32:34 -08001497 snd_device_be_idx = platform_get_snd_device_backend_index(snd_device);
1498 if (snd_device_be_idx < 0) {
1499 ALOGE("%s: Couldn't get the backend index for snd device %s ret=%d",
1500 __func__, platform_get_snd_device_name(snd_device),
1501 snd_device_be_idx);
1502 }
1503
Zhou Song06761dd2019-04-28 18:08:17 +08001504 sample_rate = audio_extn_utils_get_app_sample_rate_for_device(adev, usecase, snd_device);
1505
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301506 if ((usecase->type == PCM_PLAYBACK) && (usecase->stream.out != NULL)) {
Varun Balaraje49253e2017-07-06 19:48:56 +05301507 /* Interactive streams are supported with only direct app type id.
1508 * Get Direct profile app type and use it for interactive streams
1509 */
1510 list_for_each(node, &adev->streams_output_cfg_list) {
1511 s_info = node_to_item(node, struct streams_io_cfg, list);
Aalique Grahame5cd12ff2017-10-19 10:57:00 -07001512 if (s_info->flags.out_flags == (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_BD |
Nikhil Laturkarac4a7492017-08-31 18:27:19 +05301513 AUDIO_OUTPUT_FLAG_DIRECT_PCM |
1514 AUDIO_OUTPUT_FLAG_DIRECT))
1515 bd_app_type = s_info->app_type_cfg.app_type;
Varun Balaraje49253e2017-07-06 19:48:56 +05301516 }
Aalique Grahame5cd12ff2017-10-19 10:57:00 -07001517 if (usecase->stream.out->flags == (audio_output_flags_t)AUDIO_OUTPUT_FLAG_INTERACTIVE)
Nikhil Laturkarac4a7492017-08-31 18:27:19 +05301518 app_type = bd_app_type;
Varun Balaraje49253e2017-07-06 19:48:56 +05301519 else
1520 app_type = usecase->stream.out->app_type_cfg.app_type;
Siena Richard7c2db772016-12-21 11:32:34 -08001521 app_type_cfg[len++] = app_type;
Mingming Yin21854652016-04-13 11:54:02 -07001522 app_type_cfg[len++] = acdb_dev_id;
Naresh Tanniru3a406772017-05-10 13:09:05 -07001523 app_type_cfg[len++] = sample_rate;
1524
Siena Richard7c2db772016-12-21 11:32:34 -08001525 if (snd_device_be_idx > 0)
1526 app_type_cfg[len++] = snd_device_be_idx;
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301527
Siena Richard7c2db772016-12-21 11:32:34 -08001528 ALOGI("%s PLAYBACK app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
1529 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301530
1531 } else if ((usecase->type == PCM_CAPTURE) && (usecase->stream.in != NULL)) {
Siena Richard7c2db772016-12-21 11:32:34 -08001532 app_type = usecase->stream.in->app_type_cfg.app_type;
1533 app_type_cfg[len++] = app_type;
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301534 app_type_cfg[len++] = acdb_dev_id;
Siena Richard7c2db772016-12-21 11:32:34 -08001535 app_type_cfg[len++] = sample_rate;
1536 if (snd_device_be_idx > 0)
1537 app_type_cfg[len++] = snd_device_be_idx;
1538 ALOGI("%s CAPTURE 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 } else {
Siena Richard7c2db772016-12-21 11:32:34 -08001541 app_type = platform_get_default_app_type_v2(adev->platform, usecase->type);
Surendar Karka93cd25a2018-08-28 14:21:37 +05301542 if(usecase->type == TRANSCODE_LOOPBACK_RX) {
Siddartha Shaik343abc62017-08-08 11:15:25 +05301543 app_type = usecase->stream.inout->out_app_type_cfg.app_type;
1544 }
Siena Richard7c2db772016-12-21 11:32:34 -08001545 app_type_cfg[len++] = app_type;
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301546 app_type_cfg[len++] = acdb_dev_id;
1547 app_type_cfg[len++] = sample_rate;
Siena Richard7c2db772016-12-21 11:32:34 -08001548 if (snd_device_be_idx > 0)
1549 app_type_cfg[len++] = snd_device_be_idx;
1550 ALOGI("%s default app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
1551 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Ashish Jainc02430d2016-01-04 10:42:43 +05301552 }
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301553
Siddartha Shaik343abc62017-08-08 11:15:25 +05301554 if(ctl)
1555 mixer_ctl_set_array(ctl, app_type_cfg, len);
Zhou Songd9bd9302020-08-04 16:34:45 +08001556
1557 /* send app type cfg for haptics */
1558 if (usecase->id == USECASE_AUDIO_PLAYBACK_WITH_HAPTICS) {
1559 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
1560 "Audio Stream %d App Type Cfg", adev->haptic_pcm_device_id );
1561 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
1562 if (!ctl) {
1563 ALOGE("%s: Could not get ctl for mixer cmd - %s", __func__,
1564 mixer_ctl_name);
1565 rc = -EINVAL;
1566 goto exit_send_app_type_cfg;
1567 }
1568 acdb_dev_id = platform_get_snd_device_acdb_id(SND_DEVICE_OUT_HAPTICS);
1569 snd_device_be_idx = platform_get_snd_device_backend_index(SND_DEVICE_OUT_HAPTICS);
1570 /* haptics acdb id */
1571 app_type_cfg[1] = acdb_dev_id;
1572 /* haptics be index */
1573 app_type_cfg[3] = snd_device_be_idx;
1574 mixer_ctl_set_array(ctl, app_type_cfg, len);
1575 }
1576
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001577 rc = 0;
1578exit_send_app_type_cfg:
1579 return rc;
1580}
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001581
Deeraj Soman14230922019-01-30 16:39:30 +05301582static int audio_extn_utils_check_input_parameters(uint32_t sample_rate,
1583 audio_format_t format,
1584 int channel_count)
1585{
1586 int ret = 0;
1587
1588 if (((format != AUDIO_FORMAT_PCM_16_BIT) && (format != AUDIO_FORMAT_PCM_8_24_BIT) &&
1589 (format != AUDIO_FORMAT_PCM_24_BIT_PACKED) && (format != AUDIO_FORMAT_PCM_32_BIT) &&
1590 (format != AUDIO_FORMAT_PCM_FLOAT)) &&
1591 !voice_extn_compress_voip_is_format_supported(format) &&
1592 !audio_extn_compr_cap_format_supported(format) &&
1593 !audio_extn_cin_format_supported(format))
1594 ret = -EINVAL;
1595
1596 switch (channel_count) {
1597 case 1:
1598 case 2:
1599 case 3:
1600 case 4:
1601 case 6:
1602 case 8:
1603 break;
1604 default:
1605 ret = -EINVAL;
1606 }
1607
1608 switch (sample_rate) {
1609 case 8000:
1610 case 11025:
1611 case 12000:
1612 case 16000:
1613 case 22050:
1614 case 24000:
1615 case 32000:
1616 case 44100:
1617 case 48000:
1618 case 88200:
1619 case 96000:
1620 case 176400:
1621 case 192000:
1622 break;
1623 default:
1624 ret = -EINVAL;
1625 }
1626
1627 return ret;
1628}
1629
1630static inline uint32_t audio_extn_utils_nearest_multiple(uint32_t num, uint32_t multiplier)
1631{
1632 uint32_t remainder = 0;
1633
1634 if (!multiplier)
1635 return num;
1636
1637 remainder = num % multiplier;
1638 if (remainder)
1639 num += (multiplier - remainder);
1640
1641 return num;
1642}
1643
1644static inline uint32_t audio_extn_utils_lcm(uint32_t num1, uint32_t num2)
1645{
1646 uint32_t high = num1, low = num2, temp = 0;
1647
1648 if (!num1 || !num2)
1649 return 0;
1650
1651 if (num1 < num2) {
1652 high = num2;
1653 low = num1;
1654 }
1655
1656 while (low != 0) {
1657 temp = low;
1658 low = high % low;
1659 high = temp;
1660 }
1661 return (num1 * num2)/high;
1662}
1663
Siena Richard7c2db772016-12-21 11:32:34 -08001664int audio_extn_utils_send_app_type_cfg(struct audio_device *adev,
1665 struct audio_usecase *usecase)
1666{
1667 int i, num_devices = 0;
1668 snd_device_t new_snd_devices[SND_DEVICE_OUT_END] = {0};
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301669 snd_device_t in_snd_device = usecase->in_snd_device;
Siena Richard7c2db772016-12-21 11:32:34 -08001670 int rc = 0;
1671
Aalique Grahame22e49102018-12-18 14:23:57 -08001672 if (usecase->type == PCM_HFP_CALL) {
1673 return audio_extn_utils_send_app_type_cfg_hfp(adev, usecase);
Derek Chena30a5f42019-12-03 11:17:09 -05001674 } else if (usecase->type == ICC_CALL) {
1675 return audio_extn_utils_send_app_type_cfg_icc(adev, usecase);
Fei Tongaffdf732020-02-20 20:39:05 +08001676 } else if (usecase->type == SYNTH_LOOPBACK) {
1677 return audio_extn_utils_send_app_type_cfg_synth(adev, usecase);
Aalique Grahame22e49102018-12-18 14:23:57 -08001678 }
1679
Siena Richard7c2db772016-12-21 11:32:34 -08001680 switch (usecase->type) {
1681 case PCM_PLAYBACK:
Surendar Karka93cd25a2018-08-28 14:21:37 +05301682 case TRANSCODE_LOOPBACK_RX:
Siena Richard7c2db772016-12-21 11:32:34 -08001683 ALOGD("%s: usecase->out_snd_device %s",
1684 __func__, platform_get_snd_device_name(usecase->out_snd_device));
1685 /* check for out combo device */
1686 if (platform_split_snd_device(adev->platform,
1687 usecase->out_snd_device,
1688 &num_devices, new_snd_devices)) {
1689 new_snd_devices[0] = usecase->out_snd_device;
1690 num_devices = 1;
1691 }
1692 break;
1693 case PCM_CAPTURE:
1694 ALOGD("%s: usecase->in_snd_device %s",
1695 __func__, platform_get_snd_device_name(usecase->in_snd_device));
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301696 if (voice_is_in_call_rec_stream(usecase->stream.in)) {
1697 in_snd_device = voice_get_incall_rec_backend_device(usecase->stream.in);
1698 }
Siena Richard7c2db772016-12-21 11:32:34 -08001699 /* check for in combo device */
1700 if (platform_split_snd_device(adev->platform,
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301701 in_snd_device,
Siena Richard7c2db772016-12-21 11:32:34 -08001702 &num_devices, new_snd_devices)) {
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301703 new_snd_devices[0] = in_snd_device;
Siena Richard7c2db772016-12-21 11:32:34 -08001704 num_devices = 1;
1705 }
1706 break;
Sriram Kumar9c836482020-10-27 20:34:35 +05301707 case PCM_HFP_CALL:
1708 rc = audio_extn_utils_send_app_type_cfg_hfp(adev,usecase);
1709 return rc;
Siena Richard7c2db772016-12-21 11:32:34 -08001710 default:
1711 ALOGI("%s: not a playback/capture path, no need to cfg app type", __func__);
1712 rc = 0;
1713 break;
1714 }
1715
1716 for (i = 0; i < num_devices; i++) {
1717 rc = send_app_type_cfg_for_device(adev, usecase, new_snd_devices[i]);
1718 if (rc)
1719 break;
1720 }
1721
1722 return rc;
1723}
1724
Preetam Singh Ranawat9519e9c2015-11-18 16:05:55 +05301725int read_line_from_file(const char *path, char *buf, size_t count)
1726{
1727 char * fgets_ret;
1728 FILE * fd;
1729 int rv;
1730
1731 fd = fopen(path, "r");
1732 if (fd == NULL)
1733 return -1;
1734
1735 fgets_ret = fgets(buf, (int)count, fd);
1736 if (NULL != fgets_ret) {
1737 rv = (int)strlen(buf);
1738 } else {
1739 rv = ferror(fd);
1740 }
1741 fclose(fd);
1742
1743 return rv;
1744}
1745
Ashish Jainf1eaa582016-05-23 20:54:24 +05301746/*Translates ALSA formats to AOSP PCM formats*/
1747audio_format_t alsa_format_to_hal(uint32_t alsa_format)
1748{
1749 audio_format_t format;
1750
1751 switch(alsa_format) {
1752 case SNDRV_PCM_FORMAT_S16_LE:
1753 format = AUDIO_FORMAT_PCM_16_BIT;
1754 break;
1755 case SNDRV_PCM_FORMAT_S24_3LE:
1756 format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
1757 break;
1758 case SNDRV_PCM_FORMAT_S24_LE:
1759 format = AUDIO_FORMAT_PCM_8_24_BIT;
1760 break;
1761 case SNDRV_PCM_FORMAT_S32_LE:
1762 format = AUDIO_FORMAT_PCM_32_BIT;
1763 break;
1764 default:
1765 ALOGW("Incorrect ALSA format");
1766 format = AUDIO_FORMAT_INVALID;
1767 }
1768 return format;
1769}
1770
1771/*Translates hal format (AOSP) to alsa formats*/
1772uint32_t hal_format_to_alsa(audio_format_t hal_format)
1773{
1774 uint32_t alsa_format;
1775
1776 switch (hal_format) {
1777 case AUDIO_FORMAT_PCM_32_BIT: {
1778 if (platform_supports_true_32bit())
1779 alsa_format = SNDRV_PCM_FORMAT_S32_LE;
1780 else
1781 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1782 }
1783 break;
1784 case AUDIO_FORMAT_PCM_8_BIT:
1785 alsa_format = SNDRV_PCM_FORMAT_S8;
1786 break;
1787 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
1788 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1789 break;
1790 case AUDIO_FORMAT_PCM_8_24_BIT: {
1791 if (platform_supports_true_32bit())
1792 alsa_format = SNDRV_PCM_FORMAT_S32_LE;
1793 else
1794 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1795 }
1796 break;
1797 case AUDIO_FORMAT_PCM_FLOAT:
1798 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1799 break;
1800 default:
1801 case AUDIO_FORMAT_PCM_16_BIT:
1802 alsa_format = SNDRV_PCM_FORMAT_S16_LE;
1803 break;
1804 }
1805 return alsa_format;
1806}
1807
Ashish Jain83a6cc22016-06-28 14:34:17 +05301808/*Translates PCM formats to AOSP formats*/
1809audio_format_t pcm_format_to_hal(uint32_t pcm_format)
1810{
1811 audio_format_t format = AUDIO_FORMAT_INVALID;
1812
1813 switch(pcm_format) {
1814 case PCM_FORMAT_S16_LE:
1815 format = AUDIO_FORMAT_PCM_16_BIT;
1816 break;
1817 case PCM_FORMAT_S24_3LE:
1818 format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
1819 break;
1820 case PCM_FORMAT_S24_LE:
1821 format = AUDIO_FORMAT_PCM_8_24_BIT;
1822 break;
1823 case PCM_FORMAT_S32_LE:
1824 format = AUDIO_FORMAT_PCM_32_BIT;
1825 break;
1826 default:
1827 ALOGW("Incorrect PCM format");
1828 format = AUDIO_FORMAT_INVALID;
1829 }
1830 return format;
1831}
1832
1833/*Translates hal format (AOSP) to alsa formats*/
1834uint32_t hal_format_to_pcm(audio_format_t hal_format)
1835{
1836 uint32_t pcm_format;
1837
1838 switch (hal_format) {
1839 case AUDIO_FORMAT_PCM_32_BIT:
1840 case AUDIO_FORMAT_PCM_8_24_BIT:
1841 case AUDIO_FORMAT_PCM_FLOAT: {
1842 if (platform_supports_true_32bit())
1843 pcm_format = PCM_FORMAT_S32_LE;
1844 else
1845 pcm_format = PCM_FORMAT_S24_3LE;
1846 }
1847 break;
1848 case AUDIO_FORMAT_PCM_8_BIT:
1849 pcm_format = PCM_FORMAT_S8;
1850 break;
1851 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
1852 pcm_format = PCM_FORMAT_S24_3LE;
1853 break;
1854 default:
1855 case AUDIO_FORMAT_PCM_16_BIT:
1856 pcm_format = PCM_FORMAT_S16_LE;
1857 break;
1858 }
1859 return pcm_format;
1860}
1861
Ashish Jainf1eaa582016-05-23 20:54:24 +05301862uint32_t get_alsa_fragment_size(uint32_t bytes_per_sample,
1863 uint32_t sample_rate,
Deeraj Soman65358ab2019-02-07 15:40:49 +05301864 uint32_t noOfChannels,
1865 int64_t duration_ms)
Ashish Jainf1eaa582016-05-23 20:54:24 +05301866{
1867 uint32_t fragment_size = 0;
1868 uint32_t pcm_offload_time = PCM_OFFLOAD_BUFFER_DURATION;
1869
Deeraj Soman65358ab2019-02-07 15:40:49 +05301870 if (duration_ms >= MIN_OFFLOAD_BUFFER_DURATION_MS && duration_ms <= MAX_OFFLOAD_BUFFER_DURATION_MS)
1871 pcm_offload_time = duration_ms;
1872
Ashish Jainf1eaa582016-05-23 20:54:24 +05301873 fragment_size = (pcm_offload_time
1874 * sample_rate
1875 * bytes_per_sample
1876 * noOfChannels)/1000;
1877 if (fragment_size < MIN_PCM_OFFLOAD_FRAGMENT_SIZE)
1878 fragment_size = MIN_PCM_OFFLOAD_FRAGMENT_SIZE;
1879 else if (fragment_size > MAX_PCM_OFFLOAD_FRAGMENT_SIZE)
1880 fragment_size = MAX_PCM_OFFLOAD_FRAGMENT_SIZE;
1881 /*To have same PCM samples for all channels, the buffer size requires to
1882 *be multiple of (number of channels * bytes per sample)
1883 *For writes to succeed, the buffer must be written at address which is multiple of 32
1884 */
Aalique Grahameb85f2d92017-10-16 17:53:23 -07001885 fragment_size = ALIGN(fragment_size, (bytes_per_sample * noOfChannels * 32));
Ashish Jainf1eaa582016-05-23 20:54:24 +05301886
1887 ALOGI("PCM offload Fragment size to %d bytes", fragment_size);
1888 return fragment_size;
1889}
1890
1891/* Calculates the fragment size required to configure compress session.
1892 * Based on the alsa format selected, decide if conversion is needed in
1893
1894 * HAL ( e.g. convert AUDIO_FORMAT_PCM_FLOAT input format to
1895 * AUDIO_FORMAT_PCM_24_BIT_PACKED before writing to the compress driver.
1896 */
1897void audio_extn_utils_update_direct_pcm_fragment_size(struct stream_out *out)
1898{
Ashish Jain83a6cc22016-06-28 14:34:17 +05301899 audio_format_t dst_format = out->hal_op_format;
1900 audio_format_t src_format = out->hal_ip_format;
Ashish Jainf1eaa582016-05-23 20:54:24 +05301901 uint32_t hal_op_bytes_per_sample = audio_bytes_per_sample(dst_format);
1902 uint32_t hal_ip_bytes_per_sample = audio_bytes_per_sample(src_format);
1903
1904 out->compr_config.fragment_size =
1905 get_alsa_fragment_size(hal_op_bytes_per_sample,
1906 out->sample_rate,
Deeraj Soman65358ab2019-02-07 15:40:49 +05301907 popcount(out->channel_mask),
1908 out->info.duration_us / 1000);
Ashish Jainf1eaa582016-05-23 20:54:24 +05301909
1910 if ((src_format != dst_format) &&
1911 hal_op_bytes_per_sample != hal_ip_bytes_per_sample) {
1912
Ashish Jain83a6cc22016-06-28 14:34:17 +05301913 out->hal_fragment_size =
Ashish Jainf1eaa582016-05-23 20:54:24 +05301914 ((out->compr_config.fragment_size * hal_ip_bytes_per_sample) /
1915 hal_op_bytes_per_sample);
1916 ALOGI("enable conversion hal_input_fragment_size is %d src_format %x dst_format %x",
Ashish Jain83a6cc22016-06-28 14:34:17 +05301917 out->hal_fragment_size, src_format, dst_format);
Ashish Jainf1eaa582016-05-23 20:54:24 +05301918 } else {
Ashish Jain83a6cc22016-06-28 14:34:17 +05301919 out->hal_fragment_size = out->compr_config.fragment_size;
Ashish Jainf1eaa582016-05-23 20:54:24 +05301920 }
1921}
1922
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301923/* converts pcm format 24_8 to 8_24 inplace */
1924size_t audio_extn_utils_convert_format_24_8_to_8_24(void *buf, size_t bytes)
1925{
1926 size_t i = 0;
1927 int *int_buf_stream = buf;
1928
1929 if ((bytes % 4) != 0) {
1930 ALOGE("%s: wrong inout buffer! ... is not 32 bit aligned ", __func__);
1931 return -EINVAL;
1932 }
1933
1934 for (; i < (bytes / 4); i++)
1935 int_buf_stream[i] >>= 8;
1936
1937 return bytes;
1938}
1939
Xiaojun Sang782e5b12020-06-29 21:13:06 +08001940#ifdef AUDIO_GKI_ENABLED
1941int get_snd_codec_id(audio_format_t format)
1942{
1943 int id = 0;
1944
1945 switch (format & AUDIO_FORMAT_MAIN_MASK) {
1946 case AUDIO_FORMAT_MP3:
1947 id = SND_AUDIOCODEC_MP3;
1948 break;
1949 case AUDIO_FORMAT_AAC:
1950 id = SND_AUDIOCODEC_AAC;
1951 break;
1952 case AUDIO_FORMAT_AAC_ADTS:
1953 id = SND_AUDIOCODEC_AAC;
1954 break;
1955 case AUDIO_FORMAT_AAC_LATM:
1956 id = SND_AUDIOCODEC_AAC;
1957 break;
1958 case AUDIO_FORMAT_PCM:
1959 id = SND_AUDIOCODEC_PCM;
1960 break;
1961 case AUDIO_FORMAT_FLAC:
1962 case AUDIO_FORMAT_ALAC:
1963 case AUDIO_FORMAT_APE:
1964 case AUDIO_FORMAT_VORBIS:
1965 case AUDIO_FORMAT_WMA:
1966 case AUDIO_FORMAT_WMA_PRO:
1967 case AUDIO_FORMAT_DSD:
1968 case AUDIO_FORMAT_APTX:
1969 id = SND_AUDIOCODEC_BESPOKE;
1970 break;
1971 case AUDIO_FORMAT_MP2:
1972 id = SND_AUDIOCODEC_MP2;
1973 break;
1974 case AUDIO_FORMAT_AC3:
1975 id = SND_AUDIOCODEC_AC3;
1976 break;
1977 case AUDIO_FORMAT_E_AC3:
1978 case AUDIO_FORMAT_E_AC3_JOC:
1979 id = SND_AUDIOCODEC_EAC3;
1980 break;
1981 case AUDIO_FORMAT_DTS:
1982 case AUDIO_FORMAT_DTS_HD:
1983 id = SND_AUDIOCODEC_DTS;
1984 break;
1985 case AUDIO_FORMAT_DOLBY_TRUEHD:
1986 id = SND_AUDIOCODEC_TRUEHD;
1987 break;
1988 case AUDIO_FORMAT_IEC61937:
1989 id = SND_AUDIOCODEC_IEC61937;
1990 break;
1991 default:
1992 ALOGE("%s: Unsupported audio format :%x", __func__, format);
1993 }
1994
1995 return id;
1996}
1997#else
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301998int get_snd_codec_id(audio_format_t format)
1999{
2000 int id = 0;
2001
2002 switch (format & AUDIO_FORMAT_MAIN_MASK) {
2003 case AUDIO_FORMAT_MP3:
2004 id = SND_AUDIOCODEC_MP3;
2005 break;
2006 case AUDIO_FORMAT_AAC:
2007 id = SND_AUDIOCODEC_AAC;
2008 break;
2009 case AUDIO_FORMAT_AAC_ADTS:
2010 id = SND_AUDIOCODEC_AAC;
2011 break;
Arun Kumar Dasari3b174182016-12-27 13:01:14 +05302012 case AUDIO_FORMAT_AAC_LATM:
2013 id = SND_AUDIOCODEC_AAC;
2014 break;
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05302015 case AUDIO_FORMAT_PCM:
2016 id = SND_AUDIOCODEC_PCM;
2017 break;
2018 case AUDIO_FORMAT_FLAC:
2019 id = SND_AUDIOCODEC_FLAC;
2020 break;
2021 case AUDIO_FORMAT_ALAC:
2022 id = SND_AUDIOCODEC_ALAC;
2023 break;
2024 case AUDIO_FORMAT_APE:
2025 id = SND_AUDIOCODEC_APE;
2026 break;
2027 case AUDIO_FORMAT_VORBIS:
2028 id = SND_AUDIOCODEC_VORBIS;
2029 break;
2030 case AUDIO_FORMAT_WMA:
2031 id = SND_AUDIOCODEC_WMA;
2032 break;
Dechen Chai4a575252021-07-30 09:21:28 +05302033#ifndef AUDIO_DISABLE_COMPRESS_FORMAT
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05302034 case AUDIO_FORMAT_WMA_PRO:
2035 id = SND_AUDIOCODEC_WMA_PRO;
2036 break;
Dechen Chai4a575252021-07-30 09:21:28 +05302037#endif
Satish Babu Patakokila0c313922016-12-08 12:07:08 +05302038 case AUDIO_FORMAT_MP2:
2039 id = SND_AUDIOCODEC_MP2;
2040 break;
Satish Babu Patakokila915ecba2017-01-10 17:43:56 +05302041 case AUDIO_FORMAT_AC3:
2042 id = SND_AUDIOCODEC_AC3;
2043 break;
2044 case AUDIO_FORMAT_E_AC3:
2045 case AUDIO_FORMAT_E_AC3_JOC:
2046 id = SND_AUDIOCODEC_EAC3;
2047 break;
2048 case AUDIO_FORMAT_DTS:
2049 case AUDIO_FORMAT_DTS_HD:
2050 id = SND_AUDIOCODEC_DTS;
2051 break;
Ben Romberger1aaaf862017-04-06 17:49:46 -07002052 case AUDIO_FORMAT_DOLBY_TRUEHD:
2053 id = SND_AUDIOCODEC_TRUEHD;
2054 break;
Naresh Tanniru928f0862017-04-07 16:44:23 -07002055 case AUDIO_FORMAT_IEC61937:
2056 id = SND_AUDIOCODEC_IEC61937;
2057 break;
Dechen Chai4a575252021-07-30 09:21:28 +05302058#ifndef AUDIO_DISABLE_COMPRESS_FORMAT
Preetam Singh Ranawat4277a5a2017-01-18 19:02:24 +05302059 case AUDIO_FORMAT_DSD:
2060 id = SND_AUDIOCODEC_DSD;
2061 break;
Dhanalakshmi Siddani18737932016-11-29 17:33:17 +05302062 case AUDIO_FORMAT_APTX:
2063 id = SND_AUDIOCODEC_APTX;
2064 break;
Dechen Chai4a575252021-07-30 09:21:28 +05302065#endif
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05302066 default:
2067 ALOGE("%s: Unsupported audio format :%x", __func__, format);
2068 }
2069
2070 return id;
2071}
Xiaojun Sang782e5b12020-06-29 21:13:06 +08002072#endif
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05302073
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07002074void audio_extn_utils_send_audio_calibration(struct audio_device *adev,
2075 struct audio_usecase *usecase)
2076{
2077 int type = usecase->type;
2078
Dhananjay Kumar14245982017-01-16 20:21:00 +05302079 if (type == PCM_PLAYBACK && usecase->stream.out != NULL) {
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05302080 platform_send_audio_calibration(adev->platform, usecase,
Zhou Song06761dd2019-04-28 18:08:17 +08002081 usecase->stream.out->app_type_cfg.app_type);
Dhananjay Kumar14245982017-01-16 20:21:00 +05302082 } else if (type == PCM_CAPTURE && usecase->stream.in != NULL) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +05302083 platform_send_audio_calibration(adev->platform, usecase,
Zhou Song06761dd2019-04-28 18:08:17 +08002084 usecase->stream.in->app_type_cfg.app_type);
Sriram Kumar9c836482020-10-27 20:34:35 +05302085 } else if (type == PCM_HFP_CALL) {
2086 /* when app type is default. the sample rate is not used to send cal */
2087#ifdef ENABLE_HFP_CALIBRATION
2088 platform_send_audio_calibration_hfp(adev->platform, usecase->in_snd_device);
2089#else
2090 platform_send_audio_calibration(adev->platform, usecase,
2091 platform_get_default_app_type_v2(adev->platform, usecase->type));
2092#endif
2093 } else if ((type == PCM_CAPTURE) ||
Derek Chena30a5f42019-12-03 11:17:09 -05002094 (type == TRANSCODE_LOOPBACK_RX && usecase->stream.inout != NULL) ||
Fei Tongaffdf732020-02-20 20:39:05 +08002095 (type == ICC_CALL) || (type == SYNTH_LOOPBACK)) {
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05302096 platform_send_audio_calibration(adev->platform, usecase,
Zhou Song06761dd2019-04-28 18:08:17 +08002097 platform_get_default_app_type_v2(adev->platform, usecase->type));
Vidyakumar Athotae57f6002017-03-01 13:13:16 -08002098 } else {
2099 /* No need to send audio calibration for voice and voip call usecases */
2100 if ((type != VOICE_CALL) && (type != VOIP_CALL))
2101 ALOGW("%s: No audio calibration for usecase type = %d", __func__, type);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07002102 }
2103}
2104
Ben Rombergera04fabc2014-11-14 12:16:03 -08002105// Base64 Encode and Decode
2106// Not all features supported. This must be used only with following conditions.
2107// Decode Modes: Support with and without padding
2108// CRLF not handling. So no CRLF in string to decode.
2109// Encode Modes: Supports only padding
2110int b64decode(char *inp, int ilen, uint8_t* outp)
2111{
2112 int i, j, k, ii, num;
2113 int rem, pcnt;
2114 uint32_t res=0;
2115 uint8_t getIndex[MAX_BASEINDEX_LEN];
2116 uint8_t tmp, cflag;
2117
2118 if(inp == NULL || outp == NULL || ilen <= 0) {
2119 ALOGE("[%s] received NULL pointer or zero length",__func__);
2120 return -1;
2121 }
2122
2123 memset(getIndex, MAX_BASEINDEX_LEN-1, sizeof(getIndex));
2124 for(i=0;i<BASE_TABLE_SIZE;i++) {
2125 getIndex[(uint8_t)bTable[i]] = (uint8_t)i;
2126 }
2127 getIndex[(uint8_t)'=']=0;
2128
2129 j=0;k=0;
2130 num = ilen/4;
2131 rem = ilen%4;
2132 if(rem==0)
2133 num = num-1;
2134 cflag=0;
2135 for(i=0; i<num; i++) {
2136 res=0;
2137 for(ii=0;ii<4;ii++) {
2138 res = res << 6;
2139 tmp = getIndex[(uint8_t)inp[j++]];
2140 res = res | tmp;
2141 cflag = cflag | tmp;
2142 }
2143 outp[k++] = (res >> 16)&0xFF;
2144 outp[k++] = (res >> 8)&0xFF;
2145 outp[k++] = res & 0xFF;
2146 }
2147
2148 // Handle last bytes special
2149 pcnt=0;
2150 if(rem == 0) {
2151 //With padding or full data
2152 res = 0;
2153 for(ii=0;ii<4;ii++) {
2154 if(inp[j] == '=')
2155 pcnt++;
2156 res = res << 6;
2157 tmp = getIndex[(uint8_t)inp[j++]];
2158 res = res | tmp;
2159 cflag = cflag | tmp;
2160 }
2161 outp[k++] = res >> 16;
2162 if(pcnt == 2)
2163 goto done;
2164 outp[k++] = (res>>8)&0xFF;
2165 if(pcnt == 1)
2166 goto done;
2167 outp[k++] = res&0xFF;
2168 } else {
2169 //without padding
2170 res = 0;
2171 for(i=0;i<rem;i++) {
2172 res = res << 6;
2173 tmp = getIndex[(uint8_t)inp[j++]];
2174 res = res | tmp;
2175 cflag = cflag | tmp;
2176 }
2177 for(i=rem;i<4;i++) {
2178 res = res << 6;
2179 pcnt++;
2180 }
2181 outp[k++] = res >> 16;
2182 if(pcnt == 2)
2183 goto done;
2184 outp[k++] = (res>>8)&0xFF;
2185 if(pcnt == 1)
2186 goto done;
2187 outp[k++] = res&0xFF;
2188 }
2189done:
2190 if(cflag == 0xFF) {
2191 ALOGE("[%s] base64 decode failed. Invalid character found %s",
2192 __func__, inp);
2193 return 0;
2194 }
2195 return k;
2196}
2197
2198int b64encode(uint8_t *inp, int ilen, char* outp)
2199{
2200 int i,j,k, num;
2201 int rem=0;
2202 uint32_t res=0;
2203
2204 if(inp == NULL || outp == NULL || ilen<=0) {
2205 ALOGE("[%s] received NULL pointer or zero input length",__func__);
2206 return -1;
2207 }
2208
2209 num = ilen/3;
2210 rem = ilen%3;
2211 j=0;k=0;
2212 for(i=0; i<num; i++) {
2213 //prepare index
2214 res = inp[j++]<<16;
2215 res = res | inp[j++]<<8;
2216 res = res | inp[j++];
2217 //get output map from index
2218 outp[k++] = (char) bTable[(res>>18)&0x3F];
2219 outp[k++] = (char) bTable[(res>>12)&0x3F];
2220 outp[k++] = (char) bTable[(res>>6)&0x3F];
2221 outp[k++] = (char) bTable[res&0x3F];
2222 }
2223
2224 switch(rem) {
2225 case 1:
2226 res = inp[j++]<<16;
2227 outp[k++] = (char) bTable[res>>18];
2228 outp[k++] = (char) bTable[(res>>12)&0x3F];
2229 //outp[k++] = '=';
2230 //outp[k++] = '=';
2231 break;
2232 case 2:
2233 res = inp[j++]<<16;
2234 res = res | inp[j++]<<8;
2235 outp[k++] = (char) bTable[res>>18];
2236 outp[k++] = (char) bTable[(res>>12)&0x3F];
2237 outp[k++] = (char) bTable[(res>>6)&0x3F];
2238 //outp[k++] = '=';
2239 break;
2240 default:
2241 break;
2242 }
Ben Rombergera04fabc2014-11-14 12:16:03 -08002243 outp[k] = '\0';
2244 return k;
2245}
Ashish Jain81eb2a82015-05-13 10:52:34 +05302246
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05302247
2248int audio_extn_utils_get_codec_version(const char *snd_card_name,
2249 int card_num,
2250 char *codec_version)
2251{
2252 char procfs_path[50];
2253 FILE *fp;
2254
2255 if (strstr(snd_card_name, "tasha")) {
2256 snprintf(procfs_path, sizeof(procfs_path),
2257 "/proc/asound/card%d/codecs/tasha/version", card_num);
2258 if ((fp = fopen(procfs_path, "r")) != NULL) {
2259 fgets(codec_version, CODEC_VERSION_MAX_LENGTH, fp);
2260 fclose(fp);
2261 } else {
2262 ALOGE("%s: ERROR. cannot open %s", __func__, procfs_path);
2263 return -ENOENT;
2264 }
2265 ALOGD("%s: codec version %s", __func__, codec_version);
2266 }
2267
2268 return 0;
2269}
2270
Preetam Singh Ranawat9d0d8e42019-07-15 12:27:25 +05302271int audio_extn_utils_get_codec_variant(int card_num,
2272 char *codec_variant)
2273{
2274 char procfs_path[50];
2275 FILE *fp;
2276 snprintf(procfs_path, sizeof(procfs_path),
2277 "/proc/asound/card%d/codecs/wcd938x/variant", card_num);
2278 if ((fp = fopen(procfs_path, "r")) == NULL) {
2279 snprintf(procfs_path, sizeof(procfs_path),
2280 "/proc/asound/card%d/codecs/wcd937x/variant", card_num);
2281 if ((fp = fopen(procfs_path, "r")) == NULL) {
2282 ALOGE("%s: ERROR. cannot open %s", __func__, procfs_path);
2283 return -ENOENT;
2284 }
2285 }
2286 fgets(codec_variant, CODEC_VARIANT_MAX_LENGTH, fp);
2287 fclose(fp);
2288 ALOGD("%s: codec variant is %s", __func__, codec_variant);
2289 return 0;
2290}
2291
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05302292
Ashish Jain81eb2a82015-05-13 10:52:34 +05302293#ifdef AUDIO_EXTERNAL_HDMI_ENABLED
2294
2295void get_default_compressed_channel_status(
2296 unsigned char *channel_status)
2297{
Ashish Jain81eb2a82015-05-13 10:52:34 +05302298 memset(channel_status,0,24);
2299
2300 /* block start bit in preamble bit 3 */
2301 channel_status[0] |= PROFESSIONAL;
2302 //compre out
2303 channel_status[0] |= NON_LPCM;
2304 // sample rate; fixed 48K for default/transcode
2305 channel_status[3] |= SR_48000;
2306}
2307
Ashish Jain81eb2a82015-05-13 10:52:34 +05302308int32_t get_compressed_channel_status(void *audio_stream_data,
2309 uint32_t audio_frame_size,
2310 unsigned char *channel_status,
2311 enum audio_parser_code_type codec_type)
2312 // codec_type - AUDIO_PARSER_CODEC_AC3
2313 // - AUDIO_PARSER_CODEC_DTS
2314{
2315 unsigned char *stream;
2316 int ret = 0;
2317 stream = (unsigned char *)audio_stream_data;
2318
2319 if (audio_stream_data == NULL || audio_frame_size == 0) {
2320 ALOGW("no buffer to get channel status, return default for compress");
2321 get_default_compressed_channel_status(channel_status);
2322 return ret;
2323 }
2324
2325 memset(channel_status,0,24);
2326 if(init_audio_parser(stream, audio_frame_size, codec_type) == -1)
2327 {
2328 ALOGE("init audio parser failed");
2329 return -1;
2330 }
2331 ret = get_channel_status(channel_status, codec_type);
2332 return ret;
2333
2334}
2335
Ashish Jain81eb2a82015-05-13 10:52:34 +05302336void get_lpcm_channel_status(uint32_t sampleRate,
2337 unsigned char *channel_status)
2338{
2339 int32_t status = 0;
Ashish Jain81eb2a82015-05-13 10:52:34 +05302340 memset(channel_status,0,24);
2341 /* block start bit in preamble bit 3 */
2342 channel_status[0] |= PROFESSIONAL;
2343 //LPCM OUT
2344 channel_status[0] &= ~NON_LPCM;
2345
2346 switch (sampleRate) {
2347 case 8000:
2348 case 11025:
2349 case 12000:
2350 case 16000:
2351 case 22050:
2352 channel_status[3] |= SR_NOTID;
Preetam Singh Ranawat61716b12015-12-14 11:55:24 +05302353 break;
Ashish Jain81eb2a82015-05-13 10:52:34 +05302354 case 24000:
2355 channel_status[3] |= SR_24000;
2356 break;
2357 case 32000:
2358 channel_status[3] |= SR_32000;
2359 break;
2360 case 44100:
2361 channel_status[3] |= SR_44100;
2362 break;
2363 case 48000:
2364 channel_status[3] |= SR_48000;
2365 break;
2366 case 88200:
2367 channel_status[3] |= SR_88200;
2368 break;
2369 case 96000:
2370 channel_status[3] |= SR_96000;
2371 break;
2372 case 176400:
2373 channel_status[3] |= SR_176400;
2374 break;
2375 case 192000:
2376 channel_status[3] |= SR_192000;
2377 break;
2378 default:
2379 ALOGV("Invalid sample_rate %u\n", sampleRate);
2380 status = -1;
2381 break;
2382 }
2383}
2384
2385void audio_utils_set_hdmi_channel_status(struct stream_out *out, char * buffer, size_t bytes)
2386{
2387 unsigned char channel_status[24]={0};
2388 struct snd_aes_iec958 iec958;
2389 const char *mixer_ctl_name = "IEC958 Playback PCM Stream";
2390 struct mixer_ctl *ctl;
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +05302391 ALOGV("%s: buffer %s bytes %zd", __func__, buffer, bytes);
Arun Mirpurie008ed22019-03-21 11:21:04 -07002392
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05302393 if (audio_extn_utils_is_dolby_format(out->format) &&
Ashish Jain81eb2a82015-05-13 10:52:34 +05302394 /*TODO:Extend code to support DTS passthrough*/
2395 /*set compressed channel status bits*/
Arun Mirpurie008ed22019-03-21 11:21:04 -07002396 audio_extn_passthru_is_passthrough_stream(out) &&
2397 audio_extn_is_hdmi_passthru_enabled()) {
Ashish Jain81eb2a82015-05-13 10:52:34 +05302398 get_compressed_channel_status(buffer, bytes, channel_status, AUDIO_PARSER_CODEC_AC3);
Arun Mirpurie008ed22019-03-21 11:21:04 -07002399 } else {
Ashish Jain81eb2a82015-05-13 10:52:34 +05302400 /*set channel status bit for LPCM*/
2401 get_lpcm_channel_status(out->sample_rate, channel_status);
2402 }
2403
2404 memcpy(iec958.status, channel_status,sizeof(iec958.status));
2405 ctl = mixer_get_ctl_by_name(out->dev->mixer, mixer_ctl_name);
2406 if (!ctl) {
2407 ALOGE("%s: Could not get ctl for mixer cmd - %s",
2408 __func__, mixer_ctl_name);
2409 return;
2410 }
2411 if (mixer_ctl_set_array(ctl, &iec958, sizeof(iec958)) < 0) {
2412 ALOGE("%s: Could not set channel status for ext HDMI ",
2413 __func__);
2414 return;
2415 }
2416
2417}
2418#endif
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302419
2420int audio_extn_utils_get_avt_device_drift(
2421 struct audio_usecase *usecase,
2422 struct audio_avt_device_drift_param *drift_param)
2423{
2424 int ret = 0, count = 0;
2425 char avt_device_drift_mixer_ctl_name[MIXER_PATH_MAX_LENGTH] = {0};
Naresh Tanniru6160c712017-04-17 15:43:48 +05302426 const char *backend = NULL;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302427 struct mixer_ctl *ctl = NULL;
2428 struct audio_avt_device_drift_stats drift_stats;
2429 struct audio_device *adev = NULL;
2430
2431 if (usecase != NULL && usecase->type == PCM_PLAYBACK) {
Naresh Tanniru6160c712017-04-17 15:43:48 +05302432 backend = platform_get_snd_device_backend_interface(usecase->out_snd_device);
2433 if (!backend) {
2434 ALOGE("%s: Unsupported device %d", __func__,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08002435 get_device_types(&usecase->stream.out->device_list));
Naresh Tanniru6160c712017-04-17 15:43:48 +05302436 ret = -EINVAL;
2437 goto done;
2438 }
2439 strlcpy(avt_device_drift_mixer_ctl_name,
2440 backend,
2441 MIXER_PATH_MAX_LENGTH);
2442
2443 count = strlen(backend);
2444 if (MIXER_PATH_MAX_LENGTH - count > 0) {
2445 strlcat(&avt_device_drift_mixer_ctl_name[count],
2446 " DRIFT",
2447 MIXER_PATH_MAX_LENGTH - count);
2448 } else {
2449 ret = -EINVAL;
2450 goto done;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302451 }
2452 } else {
Naresh Tanniru7586e292017-04-13 10:38:13 +05302453 ALOGE("%s: Invalid usecase",__func__);
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302454 ret = -EINVAL;
Naresh Tanniru6160c712017-04-17 15:43:48 +05302455 goto done;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302456 }
2457
Naresh Tanniru6160c712017-04-17 15:43:48 +05302458 adev = usecase->stream.out->dev;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302459 ctl = mixer_get_ctl_by_name(adev->mixer, avt_device_drift_mixer_ctl_name);
2460 if (!ctl) {
2461 ALOGE("%s: Could not get ctl for mixer cmd - %s",
2462 __func__, avt_device_drift_mixer_ctl_name);
2463
2464 ret = -EINVAL;
2465 goto done;
2466 }
2467
2468 ALOGV("%s: Getting AV Timer vs Device Drift mixer ctrl name %s", __func__,
2469 avt_device_drift_mixer_ctl_name);
2470
2471 mixer_ctl_update(ctl);
2472 count = mixer_ctl_get_num_values(ctl);
2473 if (count != sizeof(struct audio_avt_device_drift_stats)) {
2474 ALOGE("%s: mixer_ctl_get_num_values() invalid drift_stats data size",
2475 __func__);
2476
2477 ret = -EINVAL;
2478 goto done;
2479 }
2480
2481 ret = mixer_ctl_get_array(ctl, (void *)&drift_stats, count);
2482 if (ret != 0) {
2483 ALOGE("%s: mixer_ctl_get_array() failed to get drift_stats Params",
2484 __func__);
2485
2486 ret = -EINVAL;
2487 goto done;
2488 }
2489 memcpy(drift_param, &drift_stats.drift_param,
2490 sizeof(struct audio_avt_device_drift_param));
2491done:
2492 return ret;
2493}
Manish Dewangan07de2142017-02-27 19:27:20 +05302494
2495#ifdef SNDRV_COMPRESS_PATH_DELAY
2496int audio_extn_utils_compress_get_dsp_latency(struct stream_out *out)
2497{
2498 int ret = -EINVAL;
2499 struct snd_compr_metadata metadata;
2500 int delay_ms = COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
2501
Weiyin Jiangc49a3b52018-08-17 16:16:08 +08002502 /* override the latency for pcm offload use case */
2503 if ((out->flags & AUDIO_OUTPUT_FLAG_DIRECT) &&
2504 !(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)) {
2505 delay_ms = PCM_OFFLOAD_PLAYBACK_LATENCY;
2506 }
2507
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -07002508 if (property_get_bool("vendor.audio.playback.dsp.pathdelay", false)) {
Manish Dewangan07de2142017-02-27 19:27:20 +05302509 ALOGD("%s:: Quering DSP delay %d",__func__, __LINE__);
2510 if (!(is_offload_usecase(out->usecase))) {
2511 ALOGE("%s:: not supported for non offload session", __func__);
2512 goto exit;
2513 }
2514
2515 if (!out->compr) {
2516 ALOGD("%s:: Invalid compress handle,returning default dsp latency",
2517 __func__);
2518 goto exit;
2519 }
2520
2521 metadata.key = SNDRV_COMPRESS_PATH_DELAY;
2522 ret = compress_get_metadata(out->compr, &metadata);
2523 if(ret) {
2524 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2525 goto exit;
2526 }
2527 delay_ms = metadata.value[0] / 1000; /*convert to ms*/
2528 } else {
2529 ALOGD("%s:: Using Fix DSP delay",__func__);
2530 }
2531
2532exit:
2533 ALOGD("%s:: delay in ms is %d",__func__, delay_ms);
2534 return delay_ms;
2535}
2536#else
2537int audio_extn_utils_compress_get_dsp_latency(struct stream_out *out __unused)
2538{
2539 return COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
2540}
2541#endif
Manish Dewangan69426c82017-01-30 17:35:36 +05302542
2543#ifdef SNDRV_COMPRESS_RENDER_MODE
2544int audio_extn_utils_compress_set_render_mode(struct stream_out *out)
2545{
2546 struct snd_compr_metadata metadata;
2547 int ret = -EINVAL;
2548
2549 if (!(is_offload_usecase(out->usecase))) {
2550 ALOGE("%s:: not supported for non offload session", __func__);
2551 goto exit;
2552 }
2553
2554 if (!out->compr) {
2555 ALOGD("%s:: Invalid compress handle",
2556 __func__);
2557 goto exit;
2558 }
2559
2560 ALOGD("%s:: render mode %d", __func__, out->render_mode);
2561
2562 metadata.key = SNDRV_COMPRESS_RENDER_MODE;
2563 if (out->render_mode == RENDER_MODE_AUDIO_MASTER) {
2564 metadata.value[0] = SNDRV_COMPRESS_RENDER_MODE_AUDIO_MASTER;
2565 } else if (out->render_mode == RENDER_MODE_AUDIO_STC_MASTER) {
2566 metadata.value[0] = SNDRV_COMPRESS_RENDER_MODE_STC_MASTER;
2567 } else {
2568 ret = 0;
2569 goto exit;
2570 }
2571 ret = compress_set_metadata(out->compr, &metadata);
2572 if(ret) {
2573 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2574 }
2575exit:
2576 return ret;
2577}
2578#else
2579int audio_extn_utils_compress_set_render_mode(struct stream_out *out __unused)
2580{
2581 ALOGD("%s:: configuring render mode not supported", __func__);
2582 return 0;
2583}
2584#endif
Manish Dewangan58229382017-02-02 15:48:41 +05302585
2586#ifdef SNDRV_COMPRESS_CLK_REC_MODE
2587int audio_extn_utils_compress_set_clk_rec_mode(
2588 struct audio_usecase *usecase)
2589{
2590 struct snd_compr_metadata metadata;
2591 struct stream_out *out = NULL;
2592 int ret = -EINVAL;
2593
Naresh Tanniru7586e292017-04-13 10:38:13 +05302594 if (usecase == NULL || usecase->type != PCM_PLAYBACK) {
Manish Dewangan58229382017-02-02 15:48:41 +05302595 ALOGE("%s:: Invalid use case", __func__);
2596 goto exit;
2597 }
2598
2599 out = usecase->stream.out;
2600 if (!out) {
2601 ALOGE("%s:: invalid stream", __func__);
2602 goto exit;
2603 }
2604
2605 if (!is_offload_usecase(out->usecase)) {
2606 ALOGE("%s:: not supported for non offload session", __func__);
2607 goto exit;
2608 }
2609
2610 if (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER) {
2611 ALOGD("%s:: clk recovery is only supported in STC render mode",
2612 __func__);
2613 ret = 0;
2614 goto exit;
2615 }
2616
2617 if (!out->compr) {
2618 ALOGD("%s:: Invalid compress handle",
2619 __func__);
2620 goto exit;
2621 }
2622 metadata.key = SNDRV_COMPRESS_CLK_REC_MODE;
2623 switch(usecase->out_snd_device) {
2624 case SND_DEVICE_OUT_HDMI:
2625 case SND_DEVICE_OUT_SPEAKER_AND_HDMI:
2626 case SND_DEVICE_OUT_DISPLAY_PORT:
2627 case SND_DEVICE_OUT_SPEAKER_AND_DISPLAY_PORT:
2628 metadata.value[0] = SNDRV_COMPRESS_CLK_REC_MODE_NONE;
2629 break;
2630 default:
2631 metadata.value[0] = SNDRV_COMPRESS_CLK_REC_MODE_AUTO;
2632 break;
2633 }
2634
2635 ALOGD("%s:: clk recovery mode %d",__func__, metadata.value[0]);
2636
2637 ret = compress_set_metadata(out->compr, &metadata);
2638 if(ret) {
2639 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2640 }
2641
2642exit:
2643 return ret;
2644}
2645#else
2646int audio_extn_utils_compress_set_clk_rec_mode(
2647 struct audio_usecase *usecase __unused)
2648{
2649 ALOGD("%s:: configuring render mode not supported", __func__);
2650 return 0;
2651}
2652#endif
Manish Dewangan27346042017-03-01 12:56:12 +05302653
2654#ifdef SNDRV_COMPRESS_RENDER_WINDOW
2655int audio_extn_utils_compress_set_render_window(
2656 struct stream_out *out,
2657 struct audio_out_render_window_param *render_window)
2658{
2659 struct snd_compr_metadata metadata;
2660 int ret = -EINVAL;
2661
Manish Dewangan27346042017-03-01 12:56:12 +05302662 if(render_window == NULL) {
2663 ALOGE("%s:: Invalid render_window", __func__);
2664 goto exit;
2665 }
2666
Aniket Kumar Lata1e1d3662017-06-01 18:45:48 -07002667 ALOGD("%s:: render window start 0x%"PRIx64" end 0x%"PRIx64"",
2668 __func__,render_window->render_ws, render_window->render_we);
2669
Manish Dewangan27346042017-03-01 12:56:12 +05302670 if (!is_offload_usecase(out->usecase)) {
2671 ALOGE("%s:: not supported for non offload session", __func__);
2672 goto exit;
2673 }
2674
Naresh Tanniru6160c712017-04-17 15:43:48 +05302675 if ((out->render_mode != RENDER_MODE_AUDIO_MASTER) &&
2676 (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER)) {
Manish Dewangan27346042017-03-01 12:56:12 +05302677 ALOGD("%s:: only supported in timestamp mode, current "
2678 "render mode mode %d", __func__, out->render_mode);
2679 goto exit;
2680 }
2681
2682 if (!out->compr) {
2683 ALOGW("%s:: offload session not yet opened,"
2684 "render window will be configure later", __func__);
2685 /* store render window to reconfigure in start_output_stream() */
2686 goto exit;
2687 }
2688
2689 metadata.key = SNDRV_COMPRESS_RENDER_WINDOW;
2690 /*render window start value */
2691 metadata.value[0] = 0xFFFFFFFF & render_window->render_ws; /* lsb */
2692 metadata.value[1] = \
2693 (0xFFFFFFFF00000000 & render_window->render_ws) >> 32; /* msb*/
2694 /*render window end value */
2695 metadata.value[2] = 0xFFFFFFFF & render_window->render_we; /* lsb */
2696 metadata.value[3] = \
2697 (0xFFFFFFFF00000000 & render_window->render_we) >> 32; /* msb*/
2698
2699 ret = compress_set_metadata(out->compr, &metadata);
2700 if(ret) {
2701 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2702 }
2703
2704exit:
2705 return ret;
2706}
2707#else
2708int audio_extn_utils_compress_set_render_window(
2709 struct stream_out *out __unused,
2710 struct audio_out_render_window_param *render_window __unused)
2711{
2712 ALOGD("%s:: configuring render window not supported", __func__);
2713 return 0;
2714}
2715#endif
Manish Dewangan14956cc2017-02-14 18:54:42 +05302716
2717#ifdef SNDRV_COMPRESS_START_DELAY
2718int audio_extn_utils_compress_set_start_delay(
2719 struct stream_out *out,
2720 struct audio_out_start_delay_param *delay_param)
2721{
2722 struct snd_compr_metadata metadata;
2723 int ret = -EINVAL;
2724
2725 if(delay_param == NULL) {
2726 ALOGE("%s:: Invalid delay_param", __func__);
2727 goto exit;
2728 }
2729
2730 ALOGD("%s:: render start delay 0x%"PRIx64" ", __func__,
2731 delay_param->start_delay);
2732
2733 if (!is_offload_usecase(out->usecase)) {
2734 ALOGE("%s:: not supported for non offload session", __func__);
2735 goto exit;
2736 }
2737
Naresh Tanniru6160c712017-04-17 15:43:48 +05302738 if ((out->render_mode != RENDER_MODE_AUDIO_MASTER) &&
2739 (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER)) {
Manish Dewangan14956cc2017-02-14 18:54:42 +05302740 ALOGD("%s:: only supported in timestamp mode, current "
2741 "render mode mode %d", __func__, out->render_mode);
2742 goto exit;
2743 }
2744
2745 if (!out->compr) {
2746 ALOGW("%s:: offload session not yet opened,"
2747 "start delay will be configure later", __func__);
2748 goto exit;
2749 }
2750
2751 metadata.key = SNDRV_COMPRESS_START_DELAY;
2752 metadata.value[0] = 0xFFFFFFFF & delay_param->start_delay; /* lsb */
2753 metadata.value[1] = \
2754 (0xFFFFFFFF00000000 & delay_param->start_delay) >> 32; /* msb*/
2755
2756 ret = compress_set_metadata(out->compr, &metadata);
2757 if(ret) {
2758 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2759 }
2760
2761exit:
2762 return ret;
2763}
2764#else
2765int audio_extn_utils_compress_set_start_delay(
2766 struct stream_out *out __unused,
2767 struct audio_out_start_delay_param *delay_param __unused)
2768{
2769 ALOGD("%s:: configuring render window not supported", __func__);
2770 return 0;
2771}
2772#endif
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002773
Surendar Karka287348c2019-04-10 18:31:46 +05302774#ifdef SNDRV_COMPRESS_DSP_POSITION
2775int audio_extn_utils_compress_get_dsp_presentation_pos(struct stream_out *out,
2776 uint64_t *frames, struct timespec *timestamp, int32_t clock_id)
2777{
2778 int ret = -EINVAL;
2779 uint64_t *val = NULL;
2780 uint64_t time = 0;
2781 struct snd_compr_metadata metadata;
2782
2783 ALOGV("%s:: Quering DSP position with clock id %d",__func__, clock_id);
2784 metadata.key = SNDRV_COMPRESS_DSP_POSITION;
2785 metadata.value[0] = clock_id;
2786 ret = compress_get_metadata(out->compr, &metadata);
2787 if (ret) {
2788 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2789 ret = -errno;
2790 goto exit;
2791 }
2792 val = (uint64_t *)&metadata.value[1];
2793 *frames = *val;
2794 time = *(val + 1);
2795 timestamp->tv_sec = time / 1000000;
2796 timestamp->tv_nsec = (time % 1000000)*1000;
2797
2798exit:
2799 return ret;
2800}
2801#else
2802int audio_extn_utils_compress_get_dsp_presentation_pos(struct stream_out *out __unused,
2803 uint64_t *frames __unused, struct timespec *timestamp __unused,
2804 int32_t clock_id __unused)
2805{
2806 ALOGD("%s:: dsp presentation position not supported", __func__);
2807 return 0;
2808
2809}
2810#endif
2811
2812#ifdef SNDRV_PCM_IOCTL_DSP_POSITION
2813int audio_extn_utils_pcm_get_dsp_presentation_pos(struct stream_out *out,
2814 uint64_t *frames, struct timespec *timestamp, int32_t clock_id)
2815{
2816 int ret = -EINVAL;
2817 uint64_t time = 0;
2818 struct snd_pcm_prsnt_position prsnt_position;
2819
2820 ALOGV("%s:: Quering DSP position with clock id %d",__func__, clock_id);
2821 prsnt_position.clock_id = clock_id;
2822 ret = pcm_ioctl(out->pcm, SNDRV_PCM_IOCTL_DSP_POSITION, &prsnt_position);
2823 if (ret) {
2824 ALOGE("%s::error %d", __func__, ret);
2825 ret = -EIO;
2826 goto exit;
2827 }
2828
2829 *frames = prsnt_position.frames;
2830 time = prsnt_position.timestamp;
2831 timestamp->tv_sec = time / 1000000;
2832 timestamp->tv_nsec = (time % 1000000)*1000;
2833
2834exit:
2835 return ret;
2836}
2837#else
2838int audio_extn_utils_pcm_get_dsp_presentation_pos(struct stream_out *out __unused,
2839 uint64_t *frames __unused, struct timespec *timestamp __unused,
2840 int32_t clock_id __unused)
2841{
2842 ALOGD("%s:: dsp presentation position not supported", __func__);
2843 return 0;
2844
2845}
2846#endif
2847
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002848#define MAX_SND_CARD 8
Jaideep Sharmadf948882020-10-22 14:13:52 +05302849#define RETRY_US 400000
2850#define RETRY_NUMBER 100
Aalique Grahame22e49102018-12-18 14:23:57 -08002851#define PLATFORM_INFO_XML_PATH "audio_platform_info.xml"
2852#define PLATFORM_INFO_XML_BASE_STRING "audio_platform_info"
2853
Aalique Grahame22e49102018-12-18 14:23:57 -08002854bool audio_extn_utils_resolve_config_file(char file_name[MIXER_PATH_MAX_LENGTH])
2855{
2856 char full_config_path[MIXER_PATH_MAX_LENGTH];
Saurav Kumarf5d2c342020-07-29 19:31:49 +05302857 char vendor_config_path[VENDOR_CONFIG_PATH_MAX_LENGTH];
2858
2859 /* Get path for audio configuration files in vendor */
2860 audio_get_vendor_config_path(vendor_config_path,
2861 sizeof(vendor_config_path));
2862 snprintf(full_config_path, sizeof(full_config_path),
2863 "%s/%s", vendor_config_path, file_name);
2864 if (F_OK == access(full_config_path, 0)) {
2865 strlcpy(file_name, full_config_path, MIXER_PATH_MAX_LENGTH);
2866 return true;
Aalique Grahame22e49102018-12-18 14:23:57 -08002867 }
2868 return false;
2869}
2870
2871/* platform_info_file should be size 'MIXER_PATH_MAX_LENGTH' */
2872int audio_extn_utils_get_platform_info(const char* snd_card_name, char* platform_info_file)
2873{
2874 if (NULL == snd_card_name) {
2875 return -1;
2876 }
2877
2878 struct snd_card_split *snd_split_handle = NULL;
2879 int ret = 0;
2880 audio_extn_set_snd_card_split(snd_card_name);
2881 snd_split_handle = audio_extn_get_snd_card_split();
2882
2883 snprintf(platform_info_file, MIXER_PATH_MAX_LENGTH, "%s_%s_%s.xml",
2884 PLATFORM_INFO_XML_BASE_STRING, snd_split_handle->snd_card,
2885 snd_split_handle->form_factor);
2886
2887 if (!audio_extn_utils_resolve_config_file(platform_info_file)) {
2888 memset(platform_info_file, 0, MIXER_PATH_MAX_LENGTH);
2889 snprintf(platform_info_file, MIXER_PATH_MAX_LENGTH, "%s_%s.xml",
2890 PLATFORM_INFO_XML_BASE_STRING, snd_split_handle->snd_card);
2891
2892 if (!audio_extn_utils_resolve_config_file(platform_info_file)) {
2893 memset(platform_info_file, 0, MIXER_PATH_MAX_LENGTH);
2894 strlcpy(platform_info_file, PLATFORM_INFO_XML_PATH, MIXER_PATH_MAX_LENGTH);
2895 ret = audio_extn_utils_resolve_config_file(platform_info_file) ? 0 : -1;
2896 }
2897 }
2898
2899 return ret;
2900}
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002901
2902int audio_extn_utils_get_snd_card_num()
2903{
Soumya Managoli9fee7c62018-04-06 16:21:50 +05302904 int snd_card_num = 0;
2905 struct mixer *mixer = NULL;
2906
2907 snd_card_num = audio_extn_utils_open_snd_mixer(&mixer);
2908 if (mixer)
2909 mixer_close(mixer);
2910 return snd_card_num;
2911}
2912
2913int audio_extn_utils_open_snd_mixer(struct mixer **mixer_handle)
2914{
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002915
2916 void *hw_info = NULL;
2917 struct mixer *mixer = NULL;
2918 int retry_num = 0;
mpangfaa7bae2019-01-15 16:38:13 +08002919 int snd_card_num = 0;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002920 char* snd_card_name = NULL;
mpangfaa7bae2019-01-15 16:38:13 +08002921 int snd_card_detection_info[MAX_SND_CARD] = {0};
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002922
Soumya Managoli9fee7c62018-04-06 16:21:50 +05302923 if (!mixer_handle) {
2924 ALOGE("invalid mixer handle");
2925 return -1;
2926 }
2927 *mixer_handle = NULL;
Ashish Jain30ae8942017-11-05 17:21:23 +05302928 /*
mpangfaa7bae2019-01-15 16:38:13 +08002929 * Try with all the sound cards ( 0 to 7 ) and if none of them were detected
Ashish Jain30ae8942017-11-05 17:21:23 +05302930 * sleep for 1 sec and try detections with sound card 0 again.
2931 * If sound card gets detected, check if it is relevant, if not check with the
2932 * other sound cards. To ensure that the irrelevant sound card is not check again,
2933 * we maintain it in min_snd_card_num.
2934 */
2935 while (retry_num < RETRY_NUMBER) {
mpangfaa7bae2019-01-15 16:38:13 +08002936 snd_card_num = 0;
2937 while (snd_card_num < MAX_SND_CARD) {
2938 if (snd_card_detection_info[snd_card_num] == 0) {
2939 mixer = mixer_open(snd_card_num);
2940 if (!mixer)
2941 snd_card_num++;
2942 else
2943 break;
2944 } else
2945 snd_card_num++;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002946 }
2947
2948 if (!mixer) {
Ashish Jain30ae8942017-11-05 17:21:23 +05302949 usleep(RETRY_US);
Ashish Jain30ae8942017-11-05 17:21:23 +05302950 retry_num++;
mpangfaa7bae2019-01-15 16:38:13 +08002951 ALOGD("%s: retry, retry_num %d", __func__, retry_num);
Ashish Jain30ae8942017-11-05 17:21:23 +05302952 continue;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002953 }
2954
2955 snd_card_name = strdup(mixer_get_name(mixer));
2956 if (!snd_card_name) {
2957 ALOGE("failed to allocate memory for snd_card_name\n");
2958 mixer_close(mixer);
2959 return -1;
2960 }
2961 ALOGD("%s: snd_card_name: %s", __func__, snd_card_name);
mpangfaa7bae2019-01-15 16:38:13 +08002962 snd_card_detection_info[snd_card_num] = 1;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002963 hw_info = hw_info_init(snd_card_name);
2964 if (hw_info) {
2965 ALOGD("%s: Opened sound card:%d", __func__, snd_card_num);
2966 break;
2967 }
mpangfaa7bae2019-01-15 16:38:13 +08002968 ALOGE("%s: Failed to init hardware info, snd_card_num:%d", __func__, snd_card_num);
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002969
Dhananjay Kumara7e27832017-07-11 15:40:39 +05302970 free(snd_card_name);
2971 snd_card_name = NULL;
2972
2973 mixer_close(mixer);
2974 mixer = NULL;
2975 }
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002976 if (snd_card_name)
2977 free(snd_card_name);
Ashish Jain30ae8942017-11-05 17:21:23 +05302978
Dhananjay Kumara7e27832017-07-11 15:40:39 +05302979 if (hw_info)
2980 hw_info_deinit(hw_info);
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002981
mpangfaa7bae2019-01-15 16:38:13 +08002982 if (retry_num >= RETRY_NUMBER) {
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002983 ALOGE("%s: Unable to find correct sound card, aborting.", __func__);
2984 return -1;
2985 }
2986
Soumya Managoli9fee7c62018-04-06 16:21:50 +05302987 if (mixer)
2988 *mixer_handle = mixer;
2989
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002990 return snd_card_num;
2991}
Naresh Tanniru6160c712017-04-17 15:43:48 +05302992
Soumya Managoli9fee7c62018-04-06 16:21:50 +05302993void audio_extn_utils_close_snd_mixer(struct mixer *mixer)
2994{
2995 if (mixer)
2996 mixer_close(mixer);
2997}
2998
Naresh Tanniru6160c712017-04-17 15:43:48 +05302999#ifdef SNDRV_COMPRESS_ENABLE_ADJUST_SESSION_CLOCK
3000int audio_extn_utils_compress_enable_drift_correction(
3001 struct stream_out *out,
3002 struct audio_out_enable_drift_correction *drift)
3003{
3004 struct snd_compr_metadata metadata;
3005 int ret = -EINVAL;
3006
3007 if(drift == NULL) {
3008 ALOGE("%s:: Invalid param", __func__);
3009 goto exit;
3010 }
3011
3012 ALOGD("%s:: drift enable %d", __func__,drift->enable);
3013
3014 if (!is_offload_usecase(out->usecase)) {
3015 ALOGE("%s:: not supported for non offload session", __func__);
3016 goto exit;
3017 }
3018
3019 if (!out->compr) {
3020 ALOGW("%s:: offload session not yet opened,"
3021 "start delay will be configure later", __func__);
3022 goto exit;
3023 }
3024
3025 metadata.key = SNDRV_COMPRESS_ENABLE_ADJUST_SESSION_CLOCK;
3026 metadata.value[0] = drift->enable;
3027 out->drift_correction_enabled = drift->enable;
3028
3029 ret = compress_set_metadata(out->compr, &metadata);
3030 if(ret) {
3031 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
3032 out->drift_correction_enabled = false;
3033 }
3034
3035exit:
3036 return ret;
3037}
3038#else
3039int audio_extn_utils_compress_enable_drift_correction(
3040 struct stream_out *out __unused,
3041 struct audio_out_enable_drift_correction *drift __unused)
3042{
3043 ALOGD("%s:: configuring drift enablement not supported", __func__);
3044 return 0;
3045}
3046#endif
3047
3048#ifdef SNDRV_COMPRESS_ADJUST_SESSION_CLOCK
3049int audio_extn_utils_compress_correct_drift(
3050 struct stream_out *out,
3051 struct audio_out_correct_drift *drift_param)
3052{
3053 struct snd_compr_metadata metadata;
3054 int ret = -EINVAL;
3055
3056 if (drift_param == NULL) {
3057 ALOGE("%s:: Invalid drift_param", __func__);
3058 goto exit;
3059 }
3060
3061 ALOGD("%s:: adjust time 0x%"PRIx64" ", __func__,
3062 drift_param->adjust_time);
3063
3064 if (!is_offload_usecase(out->usecase)) {
3065 ALOGE("%s:: not supported for non offload session", __func__);
3066 goto exit;
3067 }
3068
3069 if (!out->compr) {
3070 ALOGW("%s:: offload session not yet opened", __func__);
3071 goto exit;
3072 }
3073
3074 if (!out->drift_correction_enabled) {
3075 ALOGE("%s:: drift correction not enabled", __func__);
3076 goto exit;
3077 }
3078
3079 metadata.key = SNDRV_COMPRESS_ADJUST_SESSION_CLOCK;
3080 metadata.value[0] = 0xFFFFFFFF & drift_param->adjust_time; /* lsb */
3081 metadata.value[1] = \
3082 (0xFFFFFFFF00000000 & drift_param->adjust_time) >> 32; /* msb*/
3083
3084 ret = compress_set_metadata(out->compr, &metadata);
3085 if(ret)
3086 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
3087exit:
3088 return ret;
3089}
3090#else
3091int audio_extn_utils_compress_correct_drift(
3092 struct stream_out *out __unused,
3093 struct audio_out_correct_drift *drift_param __unused)
3094{
3095 ALOGD("%s:: setting adjust clock not supported", __func__);
3096 return 0;
3097}
3098#endif
Naresh Tanniru29bce4e2017-04-27 17:54:30 +05303099
3100int audio_extn_utils_set_channel_map(
3101 struct stream_out *out,
3102 struct audio_out_channel_map_param *channel_map_param)
3103{
3104 int ret = -EINVAL, i = 0;
3105 int channels = audio_channel_count_from_out_mask(out->channel_mask);
3106
3107 if (channel_map_param == NULL) {
3108 ALOGE("%s:: Invalid channel_map", __func__);
3109 goto exit;
3110 }
3111
3112 if (channel_map_param->channels != channels) {
3113 ALOGE("%s:: Channels(%d) does not match stream channels(%d)",
3114 __func__, channel_map_param->channels, channels);
3115 goto exit;
3116 }
3117
3118 for ( i = 0; i < channels; i++) {
3119 ALOGV("%s:: channel_map[%d]- %d", __func__, i, channel_map_param->channel_map[i]);
3120 out->channel_map_param.channel_map[i] = channel_map_param->channel_map[i];
3121 }
3122 ret = 0;
3123exit:
3124 return ret;
3125}
Varun Balaraje49253e2017-07-06 19:48:56 +05303126
3127int audio_extn_utils_set_pan_scale_params(
3128 struct stream_out *out,
3129 struct mix_matrix_params *mm_params)
3130{
3131 int ret = -EINVAL, i = 0, j = 0;
3132
Varun Balaraj003ee752017-08-07 17:54:55 +05303133 if (mm_params == NULL || out == NULL) {
3134 ALOGE("%s:: Invalid mix matrix or out param", __func__);
Varun Balaraje49253e2017-07-06 19:48:56 +05303135 goto exit;
3136 }
3137
3138 if (mm_params->num_output_channels > MAX_CHANNELS_SUPPORTED ||
3139 mm_params->num_output_channels <= 0 ||
3140 mm_params->num_input_channels > MAX_CHANNELS_SUPPORTED ||
3141 mm_params->num_input_channels <= 0)
3142 goto exit;
3143
3144 out->pan_scale_params.num_output_channels = mm_params->num_output_channels;
3145 out->pan_scale_params.num_input_channels = mm_params->num_input_channels;
3146 out->pan_scale_params.has_output_channel_map =
3147 mm_params->has_output_channel_map;
3148 for (i = 0; i < mm_params->num_output_channels; i++)
3149 out->pan_scale_params.output_channel_map[i] =
3150 mm_params->output_channel_map[i];
3151
3152 out->pan_scale_params.has_input_channel_map =
3153 mm_params->has_input_channel_map;
3154 for (i = 0; i < mm_params->num_input_channels; i++)
3155 out->pan_scale_params.input_channel_map[i] =
3156 mm_params->input_channel_map[i];
3157
3158 out->pan_scale_params.has_mixer_coeffs = mm_params->has_mixer_coeffs;
3159 for (i = 0; i < mm_params->num_output_channels; i++)
3160 for (j = 0; j < mm_params->num_input_channels; j++) {
3161 //Convert the channel coefficient gains in Q14 format
3162 out->pan_scale_params.mixer_coeffs[i][j] =
3163 mm_params->mixer_coeffs[i][j] * (2 << 13);
3164 }
3165
3166 ret = platform_set_stream_pan_scale_params(out->dev->platform,
3167 out->pcm_device_id,
3168 out->pan_scale_params);
3169
3170exit:
3171 return ret;
3172}
3173
3174int audio_extn_utils_set_downmix_params(
3175 struct stream_out *out,
3176 struct mix_matrix_params *mm_params)
3177{
3178 int ret = -EINVAL, i = 0, j = 0;
3179 struct audio_usecase *usecase = NULL;
3180
Aniket Kumar Lataf9f246e2017-09-15 15:20:16 -07003181 if (mm_params == NULL || out == NULL) {
Varun Balaraj003ee752017-08-07 17:54:55 +05303182 ALOGE("%s:: Invalid mix matrix or out param", __func__);
Varun Balaraje49253e2017-07-06 19:48:56 +05303183 goto exit;
3184 }
3185
3186 if (mm_params->num_output_channels > MAX_CHANNELS_SUPPORTED ||
3187 mm_params->num_output_channels <= 0 ||
3188 mm_params->num_input_channels > MAX_CHANNELS_SUPPORTED ||
3189 mm_params->num_input_channels <= 0)
3190 goto exit;
3191
3192 usecase = get_usecase_from_list(out->dev, out->usecase);
Varun Balaraj003ee752017-08-07 17:54:55 +05303193 if (!usecase) {
3194 ALOGE("%s: Get usecase list failed!", __func__);
Aniket Kumar Lataf9f246e2017-09-15 15:20:16 -07003195 goto exit;
3196 }
Varun Balaraje49253e2017-07-06 19:48:56 +05303197 out->downmix_params.num_output_channels = mm_params->num_output_channels;
3198 out->downmix_params.num_input_channels = mm_params->num_input_channels;
3199
3200 out->downmix_params.has_output_channel_map =
3201 mm_params->has_output_channel_map;
3202 for (i = 0; i < mm_params->num_output_channels; i++) {
3203 out->downmix_params.output_channel_map[i] =
3204 mm_params->output_channel_map[i];
3205 }
3206
3207 out->downmix_params.has_input_channel_map =
3208 mm_params->has_input_channel_map;
3209 for (i = 0; i < mm_params->num_input_channels; i++)
3210 out->downmix_params.input_channel_map[i] =
3211 mm_params->input_channel_map[i];
3212
3213 out->downmix_params.has_mixer_coeffs = mm_params->has_mixer_coeffs;
3214 for (i = 0; i < mm_params->num_output_channels; i++)
Nikhil Latukar2e6f6242017-08-15 13:37:07 +05303215 for (j = 0; j < mm_params->num_input_channels; j++) {
3216 //Convert the channel coefficient gains in Q14 format
Varun Balaraje49253e2017-07-06 19:48:56 +05303217 out->downmix_params.mixer_coeffs[i][j] =
Nikhil Latukar2e6f6242017-08-15 13:37:07 +05303218 mm_params->mixer_coeffs[i][j] * (2 << 13);
3219 }
Varun Balaraje49253e2017-07-06 19:48:56 +05303220
3221 ret = platform_set_stream_downmix_params(out->dev->platform,
3222 out->pcm_device_id,
3223 usecase->out_snd_device,
3224 out->downmix_params);
3225
3226exit:
3227 return ret;
3228}
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05303229
3230bool audio_extn_utils_is_dolby_format(audio_format_t format)
3231{
3232 if (format == AUDIO_FORMAT_AC3 ||
3233 format == AUDIO_FORMAT_E_AC3 ||
3234 format == AUDIO_FORMAT_E_AC3_JOC)
3235 return true;
3236 else
3237 return false;
3238}
3239
`Deeraj Soman676c2702017-09-18 19:25:53 +05303240int audio_extn_utils_get_bit_width_from_string(const char *id_string)
3241{
3242 int i;
3243 const mixer_config_lookup mixer_bitwidth_config[] = {{"S24_3LE", 24},
3244 {"S32_LE", 32},
3245 {"S24_LE", 24},
3246 {"S16_LE", 16}};
3247 int num_configs = sizeof(mixer_bitwidth_config) / sizeof(mixer_bitwidth_config[0]);
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05303248
`Deeraj Soman676c2702017-09-18 19:25:53 +05303249 for (i = 0; i < num_configs; i++) {
3250 if (!strcmp(id_string, mixer_bitwidth_config[i].id_string))
3251 return mixer_bitwidth_config[i].value;
3252 }
3253
3254 return -EINVAL;
3255}
3256
3257int audio_extn_utils_get_sample_rate_from_string(const char *id_string)
3258{
3259 int i;
Weiyin Jiang30f9ab52020-11-11 11:37:21 +08003260 const mixer_config_lookup mixer_samplerate_config[] = {{"KHZ_8", 8000},
3261 {"KHZ_11P025", 11025},
3262 {"KHZ_16", 16000},
3263 {"KHZ_22P05", 22050},
3264 {"KHZ_32", 32000},
`Deeraj Soman676c2702017-09-18 19:25:53 +05303265 {"KHZ_48", 48000},
3266 {"KHZ_96", 96000},
3267 {"KHZ_144", 144000},
3268 {"KHZ_192", 192000},
3269 {"KHZ_384", 384000},
3270 {"KHZ_44P1", 44100},
3271 {"KHZ_88P2", 88200},
3272 {"KHZ_176P4", 176400},
3273 {"KHZ_352P8", 352800}};
3274 int num_configs = sizeof(mixer_samplerate_config) / sizeof(mixer_samplerate_config[0]);
3275
3276 for (i = 0; i < num_configs; i++) {
3277 if (!strcmp(id_string, mixer_samplerate_config[i].id_string))
3278 return mixer_samplerate_config[i].value;
3279 }
3280
3281 return -EINVAL;
3282}
3283
3284int audio_extn_utils_get_channels_from_string(const char *id_string)
3285{
3286 int i;
3287 const mixer_config_lookup mixer_channels_config[] = {{"One", 1},
3288 {"Two", 2},
3289 {"Three",3},
3290 {"Four", 4},
3291 {"Five", 5},
3292 {"Six", 6},
3293 {"Seven", 7},
3294 {"Eight", 8}};
3295 int num_configs = sizeof(mixer_channels_config) / sizeof(mixer_channels_config[0]);
3296
3297 for (i = 0; i < num_configs; i++) {
3298 if (!strcmp(id_string, mixer_channels_config[i].id_string))
3299 return mixer_channels_config[i].value;
3300 }
3301
3302 return -EINVAL;
3303}
Surendar karka30569792018-05-08 12:02:21 +05303304
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08003305void audio_extn_utils_release_snd_device(snd_device_t snd_device)
3306{
3307 audio_extn_dev_arbi_release(snd_device);
3308 audio_extn_sound_trigger_update_device_status(snd_device,
3309 ST_EVENT_SND_DEVICE_FREE);
3310 audio_extn_listen_update_device_status(snd_device,
3311 LISTEN_EVENT_SND_DEVICE_FREE);
3312}
3313
Weiyin Jiangd5718bd2019-09-04 16:52:08 +08003314int audio_extn_utils_get_license_params(
3315 const struct audio_device *adev,
3316 struct audio_license_params *license_params)
Surendar karka30569792018-05-08 12:02:21 +05303317{
3318 if(!license_params)
3319 return -EINVAL;
Weiyin Jiangd5718bd2019-09-04 16:52:08 +08003320
3321 return platform_get_license_by_product(adev->platform,
3322 (const char*)license_params->product, &license_params->key, license_params->license);
Surendar karka30569792018-05-08 12:02:21 +05303323}
3324
Aalique Grahame22e49102018-12-18 14:23:57 -08003325int audio_extn_utils_send_app_type_gain(struct audio_device *adev,
3326 int app_type,
3327 int *gain)
3328{
3329 int gain_cfg[4];
3330 const char *mixer_ctl_name = "App Type Gain";
3331 struct mixer_ctl *ctl;
3332 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
3333 if (!ctl) {
3334 ALOGE("%s: Could not get volume ctl mixer %s", __func__,
3335 mixer_ctl_name);
3336 return -EINVAL;
3337 }
3338 gain_cfg[0] = 0;
3339 gain_cfg[1] = app_type;
3340 gain_cfg[2] = gain[0];
3341 gain_cfg[3] = gain[1];
3342 ALOGV("%s app_type %d l(%d) r(%d)", __func__, app_type, gain[0], gain[1]);
3343 return mixer_ctl_set_array(ctl, gain_cfg,
3344 sizeof(gain_cfg)/sizeof(gain_cfg[0]));
3345}
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -07003346
Weiyin Jiangd5718bd2019-09-04 16:52:08 +08003347static void vndk_fwk_init()
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -07003348{
Weiyin Jiangd5718bd2019-09-04 16:52:08 +08003349 if (mVndkFwk.lib_handle != NULL)
3350 return;
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -07003351
Weiyin Jiangd5718bd2019-09-04 16:52:08 +08003352 mVndkFwk.lib_handle = dlopen(VNDK_FWK_LIB_PATH, RTLD_NOW);
3353 if (mVndkFwk.lib_handle == NULL) {
3354 ALOGW("%s: failed to dlopen VNDK_FWK_LIB %s", __func__, strerror(errno));
3355 return;
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -07003356 }
3357
Weiyin Jiangd5718bd2019-09-04 16:52:08 +08003358 *(void **)(&mVndkFwk.isVendorEnhancedFwk) =
3359 dlsym(mVndkFwk.lib_handle, "isRunningWithVendorEnhancedFramework");
3360 if (mVndkFwk.isVendorEnhancedFwk == NULL) {
3361 ALOGW("%s: dlsym failed %s", __func__, strerror(errno));
3362 if (mVndkFwk.lib_handle) {
3363 dlclose(mVndkFwk.lib_handle);
3364 mVndkFwk.lib_handle = NULL;
3365 }
3366 return;
3367 }
3368
3369
3370 *(void **)(&mVndkFwk.getVendorEnhancedInfo) =
3371 dlsym(mVndkFwk.lib_handle, "getVendorEnhancedInfo");
3372 if (mVndkFwk.getVendorEnhancedInfo == NULL) {
3373 ALOGW("%s: dlsym failed %s", __func__, strerror(errno));
3374 if (mVndkFwk.lib_handle) {
3375 dlclose(mVndkFwk.lib_handle);
3376 mVndkFwk.lib_handle = NULL;
3377 }
3378 }
3379
3380 return;
3381}
3382
3383bool audio_extn_utils_is_vendor_enhanced_fwk()
3384{
3385 static int is_vendor_enhanced_fwk = -EINVAL;
3386 if (is_vendor_enhanced_fwk != -EINVAL)
3387 return (bool)is_vendor_enhanced_fwk;
3388
3389 vndk_fwk_init();
3390
3391 if (mVndkFwk.isVendorEnhancedFwk != NULL) {
3392 is_vendor_enhanced_fwk = mVndkFwk.isVendorEnhancedFwk();
3393 ALOGW("%s: is_vendor_enhanced_fwk %d", __func__, is_vendor_enhanced_fwk);
3394 } else {
3395 is_vendor_enhanced_fwk = 0;
3396 ALOGW("%s: default to non enhanced_fwk config", __func__);
3397 }
3398
3399 return (bool)is_vendor_enhanced_fwk;
3400}
3401
3402int audio_extn_utils_get_vendor_enhanced_info()
3403{
3404 static int vendor_enhanced_info = -EINVAL;
3405 if (vendor_enhanced_info != -EINVAL)
3406 return vendor_enhanced_info;
3407
3408 vndk_fwk_init();
3409
3410 if (mVndkFwk.getVendorEnhancedInfo != NULL) {
3411 vendor_enhanced_info = mVndkFwk.getVendorEnhancedInfo();
3412 ALOGW("%s: vendor_enhanced_info 0x%x", __func__, vendor_enhanced_info);
3413 } else {
3414 vendor_enhanced_info = 0x0;
3415 ALOGW("%s: default to vendor_enhanced_info 0x0", __func__);
3416 }
3417
3418 return vendor_enhanced_info;
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -07003419}
Deeraj Soman14230922019-01-30 16:39:30 +05303420
Deeraj Somanfa377bf2019-02-06 12:57:59 +05303421int audio_extn_utils_get_perf_mode_flag(void)
3422{
3423#ifdef COMPRESSED_PERF_MODE_FLAG
3424 return COMPRESSED_PERF_MODE_FLAG;
3425#else
3426 return 0;
3427#endif
3428}
3429
Deeraj Soman14230922019-01-30 16:39:30 +05303430size_t audio_extn_utils_get_input_buffer_size(uint32_t sample_rate,
3431 audio_format_t format,
3432 int channel_count,
3433 int64_t duration_ms,
3434 bool is_low_latency)
3435{
3436 size_t size = 0;
3437 size_t capture_duration = AUDIO_CAPTURE_PERIOD_DURATION_MSEC;
3438 uint32_t bytes_per_period_sample = 0;
3439
3440
3441 if (audio_extn_utils_check_input_parameters(sample_rate, format, channel_count) != 0)
3442 return 0;
3443
3444 if (duration_ms >= MIN_OFFLOAD_BUFFER_DURATION_MS && duration_ms <= MAX_OFFLOAD_BUFFER_DURATION_MS)
3445 capture_duration = duration_ms;
3446
3447 size = (sample_rate * capture_duration) / 1000;
3448 if (is_low_latency)
3449 size = LOW_LATENCY_CAPTURE_PERIOD_SIZE;
3450
3451
3452 bytes_per_period_sample = audio_bytes_per_sample(format) * channel_count;
3453 size *= bytes_per_period_sample;
3454
3455 /* make sure the size is multiple of 32 bytes and additionally multiple of
3456 * the frame_size (required for 24bit samples and non-power-of-2 channel counts)
3457 * At 48 kHz mono 16-bit PCM:
3458 * 5.000 ms = 240 frames = 15*16*1*2 = 480, a whole multiple of 32 (15)
3459 * 3.333 ms = 160 frames = 10*16*1*2 = 320, a whole multiple of 32 (10)
3460 *
3461 * The loop reaches result within 32 iterations, as initial size is
3462 * already a multiple of frame_size
3463 */
3464 size = audio_extn_utils_nearest_multiple(size, audio_extn_utils_lcm(32, bytes_per_period_sample));
3465
3466 return size;
3467}
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07003468
3469int audio_extn_utils_hash_fn(void *key)
3470{
3471 return (int)key;
3472}
3473
3474bool audio_extn_utils_hash_eq(void *key1, void *key2)
3475{
3476 return (key1 == key2);
3477}