blob: d396ae337644403af2950c1b1ed23546e9755043 [file] [log] [blame]
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001/*
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07002 * Copyright (c) 2014-2020, 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
Weiyin Jiangd5718bd2019-09-04 16:52:08 +0800114typedef struct vndkfwk_s {
115 void *lib_handle;
116 int (*isVendorEnhancedFwk)(void);
117 int (*getVendorEnhancedInfo)(void);
118 const char *lib_name;
119} vndkfwk_t;
120
121static vndkfwk_t mVndkFwk = {
122 NULL, NULL, NULL, VNDK_FWK_LIB_PATH};
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -0700123
`Deeraj Soman676c2702017-09-18 19:25:53 +0530124typedef struct {
125 const char *id_string;
126 const int value;
127} mixer_config_lookup;
128
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700129struct string_to_enum {
130 const char *name;
131 uint32_t value;
132};
133
134const struct string_to_enum s_flag_name_to_enum_table[] = {
135 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_DIRECT),
136 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_PRIMARY),
137 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_FAST),
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800138 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_RAW),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700139 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_DEEP_BUFFER),
140 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD),
141 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_NON_BLOCKING),
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700142 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_HW_AV_SYNC),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700143 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_INCALL_MUSIC),
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700144 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH),
Naresh Tanniruee3499a2017-01-05 14:05:35 +0530145 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_TIMESTAMP),
Vikram Panduranga93f080e2017-06-07 18:16:14 -0700146 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_VOIP_RX),
Ben Romberger6c4d3812017-06-13 17:46:45 -0700147 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_BD),
Varun Balaraje49253e2017-07-06 19:48:56 +0530148 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_INTERACTIVE),
Derek Chen090dfc52018-03-22 22:15:29 -0400149 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_MEDIA),
150 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_SYS_NOTIFICATION),
151 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_NAV_GUIDANCE),
152 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_PHONE),
Derek Chen1bcdc6b2020-02-06 22:44:53 -0800153 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_FRONT_PASSENGER),
Derek Chendf05eea2019-08-01 13:57:49 -0700154 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_REAR_SEAT),
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530155 STRING_TO_ENUM(AUDIO_INPUT_FLAG_NONE),
156 STRING_TO_ENUM(AUDIO_INPUT_FLAG_FAST),
157 STRING_TO_ENUM(AUDIO_INPUT_FLAG_HW_HOTWORD),
158 STRING_TO_ENUM(AUDIO_INPUT_FLAG_RAW),
159 STRING_TO_ENUM(AUDIO_INPUT_FLAG_SYNC),
Dhananjay Kumaree4d2002016-10-25 18:02:58 +0530160 STRING_TO_ENUM(AUDIO_INPUT_FLAG_TIMESTAMP),
Dhananjay Kumar704ce6f2017-09-28 22:08:00 +0530161 STRING_TO_ENUM(AUDIO_INPUT_FLAG_COMPRESS),
Ralf Herzaec80262018-07-03 07:08:49 +0200162 STRING_TO_ENUM(AUDIO_INPUT_FLAG_PASSTHROUGH),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700163};
164
165const struct string_to_enum s_format_name_to_enum_table[] = {
Ashish Jain83a6cc22016-06-28 14:34:17 +0530166 STRING_TO_ENUM(AUDIO_FORMAT_PCM_8_BIT),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700167 STRING_TO_ENUM(AUDIO_FORMAT_PCM_16_BIT),
Ashish Jain5106d362016-05-11 19:23:33 +0530168 STRING_TO_ENUM(AUDIO_FORMAT_PCM_24_BIT_PACKED),
169 STRING_TO_ENUM(AUDIO_FORMAT_PCM_8_24_BIT),
Ashish Jainf1eaa582016-05-23 20:54:24 +0530170 STRING_TO_ENUM(AUDIO_FORMAT_PCM_32_BIT),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700171 STRING_TO_ENUM(AUDIO_FORMAT_MP3),
172 STRING_TO_ENUM(AUDIO_FORMAT_AAC),
173 STRING_TO_ENUM(AUDIO_FORMAT_VORBIS),
Mingming Yinae3530f2014-07-03 16:50:18 -0700174 STRING_TO_ENUM(AUDIO_FORMAT_AMR_NB),
175 STRING_TO_ENUM(AUDIO_FORMAT_AMR_WB),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700176 STRING_TO_ENUM(AUDIO_FORMAT_AC3),
Mingming Yinae3530f2014-07-03 16:50:18 -0700177 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700178 STRING_TO_ENUM(AUDIO_FORMAT_DTS),
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530179 STRING_TO_ENUM(AUDIO_FORMAT_DTS_HD),
Ben Romberger1aaaf862017-04-06 17:49:46 -0700180 STRING_TO_ENUM(AUDIO_FORMAT_DOLBY_TRUEHD),
Naresh Tanniru928f0862017-04-07 16:44:23 -0700181 STRING_TO_ENUM(AUDIO_FORMAT_IEC61937),
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530182 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3_JOC),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700183 STRING_TO_ENUM(AUDIO_FORMAT_WMA),
184 STRING_TO_ENUM(AUDIO_FORMAT_WMA_PRO),
185 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADIF),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700186 STRING_TO_ENUM(AUDIO_FORMAT_AMR_WB_PLUS),
187 STRING_TO_ENUM(AUDIO_FORMAT_EVRC),
188 STRING_TO_ENUM(AUDIO_FORMAT_EVRCB),
189 STRING_TO_ENUM(AUDIO_FORMAT_EVRCWB),
190 STRING_TO_ENUM(AUDIO_FORMAT_QCELP),
191 STRING_TO_ENUM(AUDIO_FORMAT_MP2),
192 STRING_TO_ENUM(AUDIO_FORMAT_EVRCNW),
Amit Shekhar6f461b12014-08-01 14:52:58 -0700193 STRING_TO_ENUM(AUDIO_FORMAT_FLAC),
Satya Krishna Pindiproli70471602015-04-24 19:12:43 +0530194 STRING_TO_ENUM(AUDIO_FORMAT_ALAC),
195 STRING_TO_ENUM(AUDIO_FORMAT_APE),
Alexy Josephcd8eaed2014-12-11 12:46:53 -0800196 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LC),
197 STRING_TO_ENUM(AUDIO_FORMAT_AAC_HE_V1),
198 STRING_TO_ENUM(AUDIO_FORMAT_AAC_HE_V2),
Manish Dewangana6fc5442015-08-24 20:30:31 +0530199 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS),
Ashish Jaine513a872015-11-19 17:00:56 +0530200 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_LC),
201 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_HE_V1),
202 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_HE_V2),
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +0530203 STRING_TO_ENUM(AUDIO_FORMAT_DSD),
Arun Kumar Dasari3b174182016-12-27 13:01:14 +0530204 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM),
205 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM_LC),
206 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM_HE_V1),
207 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM_HE_V2),
Dhanalakshmi Siddani18737932016-11-29 17:33:17 +0530208 STRING_TO_ENUM(AUDIO_FORMAT_APTX),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700209};
210
Manish Dewangan3ccdea52017-02-13 19:31:54 +0530211/* payload structure avt_device drift query */
212struct audio_avt_device_drift_stats {
213 uint32_t minor_version;
Manish Dewangan338c50a2017-09-12 15:22:03 +0530214
Manish Dewangan3ccdea52017-02-13 19:31:54 +0530215 /* Indicates the device interface direction as either
216 * source (Tx) or sink (Rx).
217 */
218 uint16_t device_direction;
Manish Dewangan338c50a2017-09-12 15:22:03 +0530219
220 /* Reference timer for drift accumulation and time stamp information.
221 * currently it only support AFE_REF_TIMER_TYPE_AVTIMER
222 */
223 uint16_t reference_timer;
Manish Dewangan3ccdea52017-02-13 19:31:54 +0530224 struct audio_avt_device_drift_param drift_param;
Manish Dewangan338c50a2017-09-12 15:22:03 +0530225} __attribute__((packed));
Manish Dewangan3ccdea52017-02-13 19:31:54 +0530226
Ben Rombergera04fabc2014-11-14 12:16:03 -0800227static char bTable[BASE_TABLE_SIZE] = {
228 'A','B','C','D','E','F','G','H','I','J','K','L',
229 'M','N','O','P','Q','R','S','T','U','V','W','X',
230 'Y','Z','a','b','c','d','e','f','g','h','i','j',
231 'k','l','m','n','o','p','q','r','s','t','u','v',
232 'w','x','y','z','0','1','2','3','4','5','6','7',
233 '8','9','+','/'
234};
235
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700236static uint32_t string_to_enum(const struct string_to_enum *table, size_t size,
237 const char *name)
238{
239 size_t i;
240 for (i = 0; i < size; i++) {
241 if (strcmp(table[i].name, name) == 0) {
242 ALOGV("%s found %s", __func__, table[i].name);
243 return table[i].value;
244 }
245 }
246 return 0;
247}
248
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530249static audio_io_flags_t parse_flag_names(char *name)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700250{
251 uint32_t flag = 0;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530252 audio_io_flags_t io_flags;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800253 char *last_r;
254 char *flag_name = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700255 while (flag_name != NULL) {
256 if (strlen(flag_name) != 0) {
257 flag |= string_to_enum(s_flag_name_to_enum_table,
258 ARRAY_SIZE(s_flag_name_to_enum_table),
259 flag_name);
260 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800261 flag_name = strtok_r(NULL, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700262 }
263
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800264 ALOGV("parse_flag_names: flag - %x", flag);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530265 io_flags.in_flags = (audio_input_flags_t)flag;
266 io_flags.out_flags = (audio_output_flags_t)flag;
267 return io_flags;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700268}
269
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530270static void parse_format_names(char *name, struct streams_io_cfg *s_info)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700271{
272 struct stream_format *sf_info = NULL;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800273 char *last_r;
274 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700275
276 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG) == 0)
277 return;
278
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530279 list_init(&s_info->format_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700280 while (str != NULL) {
281 audio_format_t format = (audio_format_t)string_to_enum(s_format_name_to_enum_table,
282 ARRAY_SIZE(s_format_name_to_enum_table), str);
283 ALOGV("%s: format - %d", __func__, format);
284 if (format != 0) {
285 sf_info = (struct stream_format *)calloc(1, sizeof(struct stream_format));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700286 if (sf_info == NULL)
287 break; /* return whatever was parsed */
288
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700289 sf_info->format = format;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530290 list_add_tail(&s_info->format_list, &sf_info->list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700291 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800292 str = strtok_r(NULL, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700293 }
294}
295
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530296static void parse_sample_rate_names(char *name, struct streams_io_cfg *s_info)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700297{
Amit Shekhar6f461b12014-08-01 14:52:58 -0700298 struct stream_sample_rate *ss_info = NULL;
299 uint32_t sample_rate = 48000;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800300 char *last_r;
301 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700302
Amit Shekhar6f461b12014-08-01 14:52:58 -0700303 if (str != NULL && 0 == strcmp(str, DYNAMIC_VALUE_TAG))
304 return;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700305
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530306 list_init(&s_info->sample_rate_list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700307 while (str != NULL) {
308 sample_rate = (uint32_t)strtol(str, (char **)NULL, 10);
309 ALOGV("%s: sample_rate - %d", __func__, sample_rate);
310 if (0 != sample_rate) {
311 ss_info = (struct stream_sample_rate *)calloc(1, sizeof(struct stream_sample_rate));
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800312 if (!ss_info) {
313 ALOGE("%s: memory allocation failure", __func__);
314 return;
315 }
Amit Shekhar6f461b12014-08-01 14:52:58 -0700316 ss_info->sample_rate = sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530317 list_add_tail(&s_info->sample_rate_list, &ss_info->list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700318 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800319 str = strtok_r(NULL, "|", &last_r);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700320 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700321}
322
323static int parse_bit_width_names(char *name)
324{
325 int bit_width = 16;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800326 char *last_r;
327 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700328
329 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG))
330 bit_width = (int)strtol(str, (char **)NULL, 10);
331
332 ALOGV("%s: bit_width - %d", __func__, bit_width);
333 return bit_width;
334}
335
336static int parse_app_type_names(void *platform, char *name)
337{
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700338 int app_type = platform_get_default_app_type(platform);
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 app_type = (int)strtol(str, (char **)NULL, 10);
344
345 ALOGV("%s: app_type - %d", __func__, app_type);
346 return app_type;
347}
348
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530349static void update_streams_cfg_list(cnode *root, void *platform,
350 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700351{
352 cnode *node = root->first_child;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530353 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700354
355 ALOGV("%s", __func__);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530356 s_info = (struct streams_io_cfg *)calloc(1, sizeof(struct streams_io_cfg));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700357
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530358 if (!s_info) {
359 ALOGE("failed to allocate mem for s_info list element");
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700360 return;
361 }
362
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700363 while (node) {
364 if (strcmp(node->name, FLAGS_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530365 s_info->flags = parse_flag_names((char *)node->value);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530366 } else if (strcmp(node->name, PROFILES_TAG) == 0) {
367 strlcpy(s_info->profile, (char *)node->value, sizeof(s_info->profile));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700368 } else if (strcmp(node->name, FORMATS_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530369 parse_format_names((char *)node->value, s_info);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700370 } else if (strcmp(node->name, SAMPLING_RATES_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530371 s_info->app_type_cfg.sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
372 parse_sample_rate_names((char *)node->value, s_info);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700373 } else if (strcmp(node->name, BIT_WIDTH_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530374 s_info->app_type_cfg.bit_width = parse_bit_width_names((char *)node->value);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700375 } else if (strcmp(node->name, APP_TYPE_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530376 s_info->app_type_cfg.app_type = parse_app_type_names(platform, (char *)node->value);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700377 }
378 node = node->next;
379 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530380 list_add_tail(streams_cfg_list, &s_info->list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700381}
382
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530383static void load_cfg_list(cnode *root, void *platform,
384 struct listnode *streams_output_cfg_list,
385 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700386{
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530387 cnode *node = NULL;
388
389 node = config_find(root, OUTPUTS_TAG);
390 if (node != NULL) {
391 node = node->first_child;
392 while (node) {
393 ALOGV("%s: loading output %s", __func__, node->name);
394 update_streams_cfg_list(node, platform, streams_output_cfg_list);
395 node = node->next;
396 }
397 } else {
398 ALOGI("%s: could not load output, node is NULL", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700399 }
400
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530401 node = config_find(root, INPUTS_TAG);
402 if (node != NULL) {
403 node = node->first_child;
404 while (node) {
405 ALOGV("%s: loading input %s", __func__, node->name);
406 update_streams_cfg_list(node, platform, streams_input_cfg_list);
407 node = node->next;
408 }
409 } else {
410 ALOGI("%s: could not load input, node is NULL", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700411 }
412}
413
414static void send_app_type_cfg(void *platform, struct mixer *mixer,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530415 struct listnode *streams_output_cfg_list,
416 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700417{
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530418 size_t app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT] = {0};
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700419 int length = 0, i, num_app_types = 0;
420 struct listnode *node;
421 bool update;
422 struct mixer_ctl *ctl = NULL;
423 const char *mixer_ctl_name = "App Type Config";
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530424 struct streams_io_cfg *s_info = NULL;
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800425 uint32_t target_bit_width = 0;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700426
427 if (!mixer) {
428 ALOGE("%s: mixer is null",__func__);
429 return;
430 }
431 ctl = mixer_get_ctl_by_name(mixer, mixer_ctl_name);
432 if (!ctl) {
433 ALOGE("%s: Could not get ctl for mixer cmd - %s",__func__, mixer_ctl_name);
434 return;
435 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530436 app_type_cfg[length++] = num_app_types;
437
438 if (list_empty(streams_output_cfg_list)) {
439 app_type_cfg[length++] = platform_get_default_app_type_v2(platform, PCM_PLAYBACK);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700440 app_type_cfg[length++] = 48000;
441 app_type_cfg[length++] = 16;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530442 num_app_types += 1;
443 }
444 if (list_empty(streams_input_cfg_list)) {
445 app_type_cfg[length++] = platform_get_default_app_type_v2(platform, PCM_CAPTURE);
446 app_type_cfg[length++] = 48000;
447 app_type_cfg[length++] = 16;
448 num_app_types += 1;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700449 }
450
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800451 /* get target bit width for ADM enforce mode */
452 target_bit_width = adev_get_dsp_bit_width_enforce_mode();
453
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700454 list_for_each(node, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530455 s_info = node_to_item(node, struct streams_io_cfg, list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700456 update = true;
457 for (i=0; i<length; i=i+3) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530458 if (app_type_cfg[i+1] == 0)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700459 break;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530460 else if (app_type_cfg[i+1] == (size_t)s_info->app_type_cfg.app_type) {
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530461 if (app_type_cfg[i+2] < (size_t)s_info->app_type_cfg.sample_rate)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530462 app_type_cfg[i+2] = s_info->app_type_cfg.sample_rate;
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530463 if (app_type_cfg[i+3] < (size_t)s_info->app_type_cfg.bit_width)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530464 app_type_cfg[i+3] = s_info->app_type_cfg.bit_width;
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800465 /* ADM bit width = max(enforce_bit_width, bit_width from s_info */
466 if (audio_extn_is_dsp_bit_width_enforce_mode_supported(s_info->flags.out_flags) &&
467 (target_bit_width > app_type_cfg[i+3]))
468 app_type_cfg[i+3] = target_bit_width;
469
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700470 update = false;
471 break;
472 }
473 }
474 if (update && ((length + 3) <= MAX_LENGTH_MIXER_CONTROL_IN_INT)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530475 num_app_types += 1;
476 app_type_cfg[length++] = s_info->app_type_cfg.app_type;
477 app_type_cfg[length++] = s_info->app_type_cfg.sample_rate;
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800478 app_type_cfg[length] = s_info->app_type_cfg.bit_width;
479 if (audio_extn_is_dsp_bit_width_enforce_mode_supported(s_info->flags.out_flags) &&
480 (target_bit_width > app_type_cfg[length]))
481 app_type_cfg[length] = target_bit_width;
482
483 length++;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530484 }
485 }
486 list_for_each(node, streams_input_cfg_list) {
487 s_info = node_to_item(node, struct streams_io_cfg, list);
488 update = true;
489 for (i=0; i<length; i=i+3) {
490 if (app_type_cfg[i+1] == 0)
491 break;
492 else if (app_type_cfg[i+1] == (size_t)s_info->app_type_cfg.app_type) {
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530493 if (app_type_cfg[i+2] < (size_t)s_info->app_type_cfg.sample_rate)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530494 app_type_cfg[i+2] = s_info->app_type_cfg.sample_rate;
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530495 if (app_type_cfg[i+3] < (size_t)s_info->app_type_cfg.bit_width)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530496 app_type_cfg[i+3] = s_info->app_type_cfg.bit_width;
497 update = false;
498 break;
499 }
500 }
501 if (update && ((length + 3) <= MAX_LENGTH_MIXER_CONTROL_IN_INT)) {
502 num_app_types += 1;
503 app_type_cfg[length++] = s_info->app_type_cfg.app_type;
504 app_type_cfg[length++] = s_info->app_type_cfg.sample_rate;
505 app_type_cfg[length++] = s_info->app_type_cfg.bit_width;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700506 }
507 }
508 ALOGV("%s: num_app_types: %d", __func__, num_app_types);
509 if (num_app_types) {
510 app_type_cfg[0] = num_app_types;
511 mixer_ctl_set_array(ctl, app_type_cfg, length);
512 }
513}
514
Roopesh Nataraja0f68be72020-04-28 12:32:02 -0700515/* Function to retrieve audio vendor configs path */
516void audio_get_vendor_config_path (char* config_file_path, int path_size)
517{
518 char vendor_sku[PROPERTY_VALUE_MAX] = {'\0'};
519 if (property_get("ro.boot.product.vendor.sku", vendor_sku, "") <= 0) {
520#ifdef LINUX_ENABLED
521 /* Audio configs are stored in /etc */
522 snprintf(config_file_path, path_size, "%s", "/etc");
523#else
524 /* Audio configs are stored in /vendor/etc */
525 snprintf(config_file_path, path_size, "%s", "/vendor/etc");
526#endif
527 } else {
528 /* Audio configs are stored in /vendor/etc/audio/sku_${vendor_sku} */
529 snprintf(config_file_path, path_size,
530 "%s%s", "/vendor/etc/audio/sku_", vendor_sku);
531 }
532}
533
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530534void audio_extn_utils_update_streams_cfg_lists(void *platform,
535 struct mixer *mixer,
536 struct listnode *streams_output_cfg_list,
537 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700538{
539 cnode *root;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530540 char *data = NULL;
Roopesh Nataraja0f68be72020-04-28 12:32:02 -0700541 char vendor_config_path[VENDOR_CONFIG_PATH_MAX_LENGTH];
542 char audio_io_policy_file[VENDOR_CONFIG_FILE_MAX_LENGTH];
543 char audio_output_policy_file[VENDOR_CONFIG_FILE_MAX_LENGTH];
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700544
545 ALOGV("%s", __func__);
546 list_init(streams_output_cfg_list);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530547 list_init(streams_input_cfg_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700548
549 root = config_node("", "");
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700550 if (root == NULL) {
551 ALOGE("cfg_list, NULL config root");
552 return;
553 }
554
Roopesh Nataraja0f68be72020-04-28 12:32:02 -0700555 /* Get path for audio configuration files in vendor */
556 audio_get_vendor_config_path(vendor_config_path,
557 sizeof(vendor_config_path));
558
559 /* Get path for audio_io_policy_file in vendor */
560 snprintf(audio_io_policy_file, sizeof(audio_io_policy_file),
561 "%s/%s", vendor_config_path, AUDIO_IO_POLICY_VENDOR_CONFIG_FILE_NAME);
562
563 /* Load audio_io_policy_file from vendor */
564 data = (char *)load_file(audio_io_policy_file, NULL);
565
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530566 if (data == NULL) {
567 ALOGD("%s: failed to open io config file(%s), trying older config file",
Roopesh Nataraja0f68be72020-04-28 12:32:02 -0700568 __func__, audio_io_policy_file);
569
570 /* Get path for audio_output_policy_file in vendor */
571 snprintf(audio_output_policy_file, sizeof(audio_output_policy_file),
572 "%s/%s", vendor_config_path,
573 AUDIO_OUTPUT_POLICY_VENDOR_CONFIG_FILE_NAME);
574
575 /* Load audio_output_policy_file from vendor */
576 data = (char *)load_file(audio_output_policy_file, NULL);
577
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530578 if (data == NULL) {
579 send_app_type_cfg(platform, mixer,
580 streams_output_cfg_list,
581 streams_input_cfg_list);
582 ALOGE("%s: could not load io policy config!", __func__);
yidongh6eb4f752017-04-19 18:20:57 +0800583 free(root);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530584 return;
585 }
586 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700587
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530588 config_load(root, data);
589 load_cfg_list(root, platform, streams_output_cfg_list,
590 streams_input_cfg_list);
591
592 send_app_type_cfg(platform, mixer, streams_output_cfg_list,
593 streams_input_cfg_list);
Alexy Josephaee4fdd2016-01-29 13:02:07 -0800594
595 config_free(root);
yidongh6eb4f752017-04-19 18:20:57 +0800596 free(root);
Alexy Josephaee4fdd2016-01-29 13:02:07 -0800597 free(data);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700598}
599
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530600static void audio_extn_utils_dump_streams_cfg_list(
601 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700602{
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700603 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530604 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700605 struct stream_format *sf_info;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700606 struct stream_sample_rate *ss_info;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530607
608 list_for_each(node_i, streams_cfg_list) {
609 s_info = node_to_item(node_i, struct streams_io_cfg, list);
610 ALOGV("%s: flags-%d, sample_rate-%d, bit_width-%d, app_type-%d",
611 __func__, s_info->flags.out_flags, s_info->app_type_cfg.sample_rate,
612 s_info->app_type_cfg.bit_width, s_info->app_type_cfg.app_type);
613 list_for_each(node_j, &s_info->format_list) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700614 sf_info = node_to_item(node_j, struct stream_format, list);
615 ALOGV("format-%x", sf_info->format);
616 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530617 list_for_each(node_j, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700618 ss_info = node_to_item(node_j, struct stream_sample_rate, list);
619 ALOGV("sample rate-%d", ss_info->sample_rate);
620 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700621 }
622}
623
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530624void audio_extn_utils_dump_streams_cfg_lists(
625 struct listnode *streams_output_cfg_list,
626 struct listnode *streams_input_cfg_list)
627{
628 ALOGV("%s", __func__);
629 audio_extn_utils_dump_streams_cfg_list(streams_output_cfg_list);
630 audio_extn_utils_dump_streams_cfg_list(streams_input_cfg_list);
631}
632
633static void audio_extn_utils_release_streams_cfg_list(
634 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700635{
636 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530637 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700638
639 ALOGV("%s", __func__);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530640
641 while (!list_empty(streams_cfg_list)) {
642 node_i = list_head(streams_cfg_list);
643 s_info = node_to_item(node_i, struct streams_io_cfg, list);
644 while (!list_empty(&s_info->format_list)) {
645 node_j = list_head(&s_info->format_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700646 list_remove(node_j);
647 free(node_to_item(node_j, struct stream_format, list));
648 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530649 while (!list_empty(&s_info->sample_rate_list)) {
650 node_j = list_head(&s_info->sample_rate_list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700651 list_remove(node_j);
652 free(node_to_item(node_j, struct stream_sample_rate, list));
653 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700654 list_remove(node_i);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530655 free(node_to_item(node_i, struct streams_io_cfg, list));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700656 }
657}
658
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530659void audio_extn_utils_release_streams_cfg_lists(
660 struct listnode *streams_output_cfg_list,
661 struct listnode *streams_input_cfg_list)
662{
663 ALOGV("%s", __func__);
664 audio_extn_utils_release_streams_cfg_list(streams_output_cfg_list);
665 audio_extn_utils_release_streams_cfg_list(streams_input_cfg_list);
666}
667
668static bool set_app_type_cfg(struct streams_io_cfg *s_info,
669 struct stream_app_type_cfg *app_type_cfg,
670 uint32_t sample_rate, uint32_t bit_width)
Amit Shekhar6f461b12014-08-01 14:52:58 -0700671 {
672 struct listnode *node_i;
673 struct stream_sample_rate *ss_info;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530674 list_for_each(node_i, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700675 ss_info = node_to_item(node_i, struct stream_sample_rate, list);
676 if ((sample_rate <= ss_info->sample_rate) &&
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530677 (bit_width == s_info->app_type_cfg.bit_width)) {
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700678
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530679 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700680 app_type_cfg->sample_rate = ss_info->sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530681 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700682 ALOGV("%s app_type_cfg->app_type %d, app_type_cfg->sample_rate %d, app_type_cfg->bit_width %d",
683 __func__, app_type_cfg->app_type, app_type_cfg->sample_rate, app_type_cfg->bit_width);
684 return true;
685 }
686 }
687 /*
688 * Reiterate through the list assuming dafault sample rate.
689 * Handles scenario where input sample rate is higher
690 * than all sample rates in list for the input bit width.
691 */
692 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
Apoorv Raghuvanshif59bb222015-02-18 12:23:23 -0800693
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530694 list_for_each(node_i, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700695 ss_info = node_to_item(node_i, struct stream_sample_rate, list);
696 if ((sample_rate <= ss_info->sample_rate) &&
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530697 (bit_width == s_info->app_type_cfg.bit_width)) {
698 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700699 app_type_cfg->sample_rate = sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530700 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Apoorv Raghuvanshif59bb222015-02-18 12:23:23 -0800701 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 -0700702 __func__, app_type_cfg->app_type, app_type_cfg->sample_rate, app_type_cfg->bit_width);
703 return true;
704 }
705 }
706 return false;
707}
708
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530709void audio_extn_utils_update_stream_input_app_type_cfg(void *platform,
710 struct listnode *streams_input_cfg_list,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -0800711 struct listnode *devices __unused,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530712 audio_input_flags_t flags,
713 audio_format_t format,
714 uint32_t sample_rate,
715 uint32_t bit_width,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530716 char* profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530717 struct stream_app_type_cfg *app_type_cfg)
718{
719 struct listnode *node_i, *node_j;
720 struct streams_io_cfg *s_info;
721 struct stream_format *sf_info;
722
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530723 ALOGV("%s: flags: 0x%x, format: 0x%x sample_rate %d, profile %s",
724 __func__, flags, format, sample_rate, profile);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530725
726 list_for_each(node_i, streams_input_cfg_list) {
727 s_info = node_to_item(node_i, struct streams_io_cfg, list);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530728 /* Along with flags do profile matching if set at either end.*/
729 if (s_info->flags.in_flags == flags &&
730 ((profile[0] == '\0' && s_info->profile[0] == '\0') ||
731 strncmp(s_info->profile, profile, sizeof(s_info->profile)) == 0)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530732 list_for_each(node_j, &s_info->format_list) {
733 sf_info = node_to_item(node_j, struct stream_format, list);
734 if (sf_info->format == format) {
735 if (set_app_type_cfg(s_info, app_type_cfg, sample_rate, bit_width))
736 return;
737 }
738 }
739 }
740 }
741 ALOGW("%s: App type could not be selected. Falling back to default", __func__);
742 app_type_cfg->app_type = platform_get_default_app_type_v2(platform, PCM_CAPTURE);
743 app_type_cfg->sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
744 app_type_cfg->bit_width = 16;
745}
746
747void audio_extn_utils_update_stream_output_app_type_cfg(void *platform,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700748 struct listnode *streams_output_cfg_list,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -0800749 struct listnode *devices,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700750 audio_output_flags_t flags,
751 audio_format_t format,
Amit Shekhar6f461b12014-08-01 14:52:58 -0700752 uint32_t sample_rate,
753 uint32_t bit_width,
Manish Dewangan837dc462015-05-27 10:17:41 +0530754 audio_channel_mask_t channel_mask,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530755 char *profile,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700756 struct stream_app_type_cfg *app_type_cfg)
757{
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +0530758 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530759 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700760 struct stream_format *sf_info;
Manish Dewangan837dc462015-05-27 10:17:41 +0530761 char value[PROPERTY_VALUE_MAX] = {0};
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700762
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -0800763 if (compare_device_type(devices, AUDIO_DEVICE_OUT_SPEAKER)) {
Ramu Gottipati074fa4d2018-09-11 20:05:51 +0530764 int bw = platform_get_snd_device_bit_width(SND_DEVICE_OUT_SPEAKER);
765 if ((-ENOSYS != bw) && (bit_width > (uint32_t)bw))
Amit Shekhar5a39c912014-10-14 15:39:30 -0700766 bit_width = (uint32_t)bw;
Ramu Gottipati074fa4d2018-09-11 20:05:51 +0530767 else if (-ENOSYS == bw)
768 bit_width = CODEC_BACKEND_DEFAULT_BIT_WIDTH;
Amit Shekhar1d896042014-10-03 13:16:09 -0700769 sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Aniket Kumar Latad13758f2020-08-06 15:11:36 -0700770 ALOGV("%s Allowing 24 and above bits playback on speaker \
771 ONLY at default sampling rate", __func__);
Amit Shekhar1d896042014-10-03 13:16:09 -0700772 }
773
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -0700774 property_get("vendor.audio.playback.mch.downsample",value,"");
Manish Dewangan837dc462015-05-27 10:17:41 +0530775 if (!strncmp("true", value, sizeof("true"))) {
776 if ((popcount(channel_mask) > 2) &&
777 (sample_rate > CODEC_BACKEND_DEFAULT_SAMPLE_RATE) &&
Aalique Grahame5cd12ff2017-10-19 10:57:00 -0700778 !(flags & (audio_output_flags_t)AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH)) {
Manish Dewangan837dc462015-05-27 10:17:41 +0530779 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
780 ALOGD("%s: MCH session defaulting sample rate to %d",
781 __func__, sample_rate);
782 }
783 }
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +0530784
785 /* Set sampling rate to 176.4 for DSD64
786 * and 352.8Khz for DSD128.
787 * Set Bit Width to 16. output will be 16 bit
788 * post DoP in ASM.
789 */
Aalique Grahame5cd12ff2017-10-19 10:57:00 -0700790 if ((flags & (audio_output_flags_t)AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH) &&
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +0530791 (format == AUDIO_FORMAT_DSD)) {
792 bit_width = 16;
793 if (sample_rate == INPUT_SAMPLING_RATE_DSD64)
794 sample_rate = OUTPUT_SAMPLING_RATE_DSD64;
795 else if (sample_rate == INPUT_SAMPLING_RATE_DSD128)
796 sample_rate = OUTPUT_SAMPLING_RATE_DSD128;
797 }
798
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +0530799
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800800 ALOGV("%s: flags: %x, format: %x sample_rate %d, profile %s, app_type %d",
801 __func__, flags, format, sample_rate, profile, app_type_cfg->app_type);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700802 list_for_each(node_i, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530803 s_info = node_to_item(node_i, struct streams_io_cfg, list);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530804 /* Along with flags do profile matching if set at either end.*/
805 if (s_info->flags.out_flags == flags &&
806 ((profile[0] == '\0' && s_info->profile[0] == '\0') ||
807 strncmp(s_info->profile, profile, sizeof(s_info->profile)) == 0)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530808 list_for_each(node_j, &s_info->format_list) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700809 sf_info = node_to_item(node_j, struct stream_format, list);
810 if (sf_info->format == format) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530811 if (set_app_type_cfg(s_info, app_type_cfg, sample_rate, bit_width))
Amit Shekhar6f461b12014-08-01 14:52:58 -0700812 return;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700813 }
814 }
815 }
816 }
817 list_for_each(node_i, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530818 s_info = node_to_item(node_i, struct streams_io_cfg, list);
819 if (s_info->flags.out_flags == AUDIO_OUTPUT_FLAG_PRIMARY) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700820 ALOGV("Compatible output profile not found.");
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530821 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
822 app_type_cfg->sample_rate = s_info->app_type_cfg.sample_rate;
823 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700824 ALOGV("%s Default to primary output: App type: %d sample_rate %d",
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530825 __func__, s_info->app_type_cfg.app_type, app_type_cfg->sample_rate);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700826 return;
827 }
828 }
829 ALOGW("%s: App type could not be selected. Falling back to default", __func__);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700830 app_type_cfg->app_type = platform_get_default_app_type(platform);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700831 app_type_cfg->sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700832 app_type_cfg->bit_width = 16;
833}
834
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +0530835static bool audio_is_this_native_usecase(struct audio_usecase *uc)
836{
837 bool native_usecase = false;
838 struct stream_out *out = (struct stream_out*) uc->stream.out;
839
840 if (PCM_PLAYBACK == uc->type && out != NULL &&
841 NATIVE_AUDIO_MODE_INVALID != platform_get_native_support() &&
842 is_offload_usecase(uc->id) &&
843 (out->sample_rate == OUTPUT_SAMPLING_RATE_44100))
844 native_usecase = true;
845
846 return native_usecase;
847}
848
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800849bool audio_extn_is_dsp_bit_width_enforce_mode_supported(audio_output_flags_t flags)
850{
851 /* DSP bitwidth enforce mode for ADM and AFE:
852 * includes:
853 * deep buffer, low latency, direct pcm and offload.
854 * excludes:
855 * ull(raw+fast), VOIP.
856 */
857 if ((flags & AUDIO_OUTPUT_FLAG_VOIP_RX) ||
858 ((flags & AUDIO_OUTPUT_FLAG_RAW) &&
859 (flags & AUDIO_OUTPUT_FLAG_FAST)))
860 return false;
861
862
863 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) ||
864 (flags & AUDIO_OUTPUT_FLAG_DIRECT) ||
865 (flags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) ||
866 (flags & AUDIO_OUTPUT_FLAG_PRIMARY))
867 return true;
868 else
869 return false;
870}
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800871
872static inline bool audio_is_vr_mode_on(struct audio_device *(__attribute__((unused)) adev))
873{
874 return adev->vr_audio_mode_enabled;
875}
876
Derek Chenf7092792017-05-23 12:23:53 -0400877static void audio_extn_btsco_get_sample_rate(int snd_device, int *sample_rate)
878{
879 switch (snd_device) {
880 case SND_DEVICE_OUT_BT_SCO:
881 case SND_DEVICE_IN_BT_SCO_MIC:
882 case SND_DEVICE_IN_BT_SCO_MIC_NREC:
883 *sample_rate = 8000;
884 break;
885 case SND_DEVICE_OUT_BT_SCO_WB:
886 case SND_DEVICE_IN_BT_SCO_MIC_WB:
887 case SND_DEVICE_IN_BT_SCO_MIC_WB_NREC:
888 *sample_rate = 16000;
889 break;
890 default:
Aniket Kumar Latad13758f2020-08-06 15:11:36 -0700891 ALOGV("%s:Not a BT SCO device, need not update sampling rate\n", __func__);
Derek Chenf7092792017-05-23 12:23:53 -0400892 break;
893 }
894}
895
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530896void audio_extn_utils_update_stream_app_type_cfg_for_usecase(
897 struct audio_device *adev,
898 struct audio_usecase *usecase)
899{
900 ALOGV("%s", __func__);
901
902 switch(usecase->type) {
903 case PCM_PLAYBACK:
904 audio_extn_utils_update_stream_output_app_type_cfg(adev->platform,
905 &adev->streams_output_cfg_list,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -0800906 &usecase->stream.out->device_list,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530907 usecase->stream.out->flags,
Aalique Grahame65780b52017-09-27 14:59:56 -0700908 usecase->stream.out->hal_op_format,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530909 usecase->stream.out->sample_rate,
910 usecase->stream.out->bit_width,
911 usecase->stream.out->channel_mask,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530912 usecase->stream.out->profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530913 &usecase->stream.out->app_type_cfg);
914 ALOGV("%s Selected apptype: %d", __func__, usecase->stream.out->app_type_cfg.app_type);
915 break;
916 case PCM_CAPTURE:
Vikram Panduranga93f080e2017-06-07 18:16:14 -0700917 if (usecase->id == USECASE_AUDIO_RECORD_VOIP)
918 usecase->stream.in->app_type_cfg.app_type = APP_TYPE_VOIP_AUDIO;
919 else
920 audio_extn_utils_update_stream_input_app_type_cfg(adev->platform,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530921 &adev->streams_input_cfg_list,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -0800922 &usecase->stream.in->device_list,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530923 usecase->stream.in->flags,
924 usecase->stream.in->format,
925 usecase->stream.in->sample_rate,
926 usecase->stream.in->bit_width,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530927 usecase->stream.in->profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530928 &usecase->stream.in->app_type_cfg);
929 ALOGV("%s Selected apptype: %d", __func__, usecase->stream.in->app_type_cfg.app_type);
930 break;
Surendar Karka93cd25a2018-08-28 14:21:37 +0530931 case TRANSCODE_LOOPBACK_RX :
Siddartha Shaik343abc62017-08-08 11:15:25 +0530932 audio_extn_utils_update_stream_output_app_type_cfg(adev->platform,
933 &adev->streams_output_cfg_list,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -0800934 &usecase->stream.inout->out_config.device_list,
Siddartha Shaik343abc62017-08-08 11:15:25 +0530935 0,
936 usecase->stream.inout->out_config.format,
937 usecase->stream.inout->out_config.sample_rate,
938 usecase->stream.inout->out_config.bit_width,
939 usecase->stream.inout->out_config.channel_mask,
940 usecase->stream.inout->profile,
941 &usecase->stream.inout->out_app_type_cfg);
942 ALOGV("%s Selected apptype: %d", __func__, usecase->stream.inout->out_app_type_cfg.app_type);
943 break;
Derek Chenf7092792017-05-23 12:23:53 -0400944 case PCM_HFP_CALL:
945 switch (usecase->id) {
946 case USECASE_AUDIO_HFP_SCO:
947 case USECASE_AUDIO_HFP_SCO_WB:
948 audio_extn_btsco_get_sample_rate(usecase->out_snd_device,
949 &usecase->out_app_type_cfg.sample_rate);
Derek Chend7307012019-11-12 12:10:41 -0500950 usecase->in_app_type_cfg.sample_rate = usecase->out_app_type_cfg.sample_rate;
Derek Chenf7092792017-05-23 12:23:53 -0400951 break;
952 case USECASE_AUDIO_HFP_SCO_DOWNLINK:
953 case USECASE_AUDIO_HFP_SCO_WB_DOWNLINK:
954 audio_extn_btsco_get_sample_rate(usecase->in_snd_device,
955 &usecase->in_app_type_cfg.sample_rate);
Derek Chend7307012019-11-12 12:10:41 -0500956 usecase->out_app_type_cfg.sample_rate = usecase->in_app_type_cfg.sample_rate;
Derek Chenf7092792017-05-23 12:23:53 -0400957 break;
958 default:
959 ALOGE("%s: usecase id (%d) not supported, use default sample rate",
960 __func__, usecase->id);
961 usecase->in_app_type_cfg.sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
962 usecase->out_app_type_cfg.sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
963 break;
964 }
965 /* update out_app_type_cfg */
966 usecase->out_app_type_cfg.bit_width =
967 platform_get_snd_device_bit_width(usecase->out_snd_device);
968 usecase->out_app_type_cfg.app_type =
969 platform_get_default_app_type_v2(adev->platform, PCM_PLAYBACK);
970 /* update in_app_type_cfg */
971 usecase->in_app_type_cfg.bit_width =
972 platform_get_snd_device_bit_width(usecase->in_snd_device);
973 usecase->in_app_type_cfg.app_type =
974 platform_get_default_app_type_v2(adev->platform, PCM_CAPTURE);
975 ALOGV("%s Selected apptype: playback %d capture %d",
976 __func__, usecase->out_app_type_cfg.app_type, usecase->in_app_type_cfg.app_type);
977 break;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530978 default:
979 ALOGE("%s: app type cfg not supported for usecase type (%d)",
980 __func__, usecase->type);
981 }
982}
983
Aalique Grahame22e49102018-12-18 14:23:57 -0800984static int set_stream_app_type_mixer_ctrl(struct audio_device *adev,
985 int pcm_device_id, int app_type,
986 int acdb_dev_id, int sample_rate,
987 int stream_type,
988 snd_device_t snd_device)
989{
990
991 char mixer_ctl_name[MAX_LENGTH_MIXER_CONTROL_IN_INT];
992 struct mixer_ctl *ctl;
993 int app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT], len = 0, rc = 0;
994 int snd_device_be_idx = -1;
995
996 if (stream_type == PCM_PLAYBACK) {
997 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
998 "Audio Stream %d App Type Cfg", pcm_device_id);
999 } else if (stream_type == PCM_CAPTURE) {
1000 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
1001 "Audio Stream Capture %d App Type Cfg", pcm_device_id);
1002 }
1003
1004 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
1005 if (!ctl) {
1006 ALOGE("%s: Could not get ctl for mixer cmd - %s",
1007 __func__, mixer_ctl_name);
1008 rc = -EINVAL;
1009 goto exit;
1010 }
1011 app_type_cfg[len++] = app_type;
1012 app_type_cfg[len++] = acdb_dev_id;
1013 app_type_cfg[len++] = sample_rate;
1014
1015 snd_device_be_idx = platform_get_snd_device_backend_index(snd_device);
1016 if (snd_device_be_idx > 0)
1017 app_type_cfg[len++] = snd_device_be_idx;
1018 ALOGV("%s: stream type %d app_type %d, acdb_dev_id %d "
1019 "sample rate %d, snd_device_be_idx %d",
1020 __func__, stream_type, app_type, acdb_dev_id, sample_rate,
1021 snd_device_be_idx);
1022 mixer_ctl_set_array(ctl, app_type_cfg, len);
1023
1024exit:
1025 return rc;
1026}
1027
1028static int audio_extn_utils_send_app_type_cfg_hfp(struct audio_device *adev,
1029 struct audio_usecase *usecase)
1030{
1031 int pcm_device_id, acdb_dev_id = 0, snd_device = usecase->out_snd_device;
1032 int32_t sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
1033 int app_type = 0, rc = 0;
Derek Chenf7092792017-05-23 12:23:53 -04001034 bool is_bus_dev_usecase = false;
Aalique Grahame22e49102018-12-18 14:23:57 -08001035
1036 ALOGV("%s", __func__);
1037
1038 if (usecase->type != PCM_HFP_CALL) {
Derek Chenf7092792017-05-23 12:23:53 -04001039 ALOGV("%s: not a HFP path, no need to cfg app type", __func__);
Aalique Grahame22e49102018-12-18 14:23:57 -08001040 rc = 0;
1041 goto exit_send_app_type_cfg;
1042 }
1043 if ((usecase->id != USECASE_AUDIO_HFP_SCO) &&
Derek Chenf7092792017-05-23 12:23:53 -04001044 (usecase->id != USECASE_AUDIO_HFP_SCO_WB) &&
1045 (usecase->id != USECASE_AUDIO_HFP_SCO_DOWNLINK) &&
1046 (usecase->id != USECASE_AUDIO_HFP_SCO_WB_DOWNLINK)) {
1047 ALOGV("%s: a usecase where app type cfg is not required", __func__);
Aalique Grahame22e49102018-12-18 14:23:57 -08001048 rc = 0;
1049 goto exit_send_app_type_cfg;
1050 }
1051
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08001052 if (compare_device_type(&usecase->device_list, AUDIO_DEVICE_OUT_BUS))
Derek Chenf7092792017-05-23 12:23:53 -04001053 is_bus_dev_usecase = true;
1054
Aalique Grahame22e49102018-12-18 14:23:57 -08001055 snd_device = usecase->out_snd_device;
1056 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_PLAYBACK);
1057
1058 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
1059 if (acdb_dev_id < 0) {
1060 ALOGE("%s: Couldn't get the acdb dev id", __func__);
1061 rc = -EINVAL;
1062 goto exit_send_app_type_cfg;
1063 }
1064
1065 if (usecase->type == PCM_HFP_CALL) {
1066
1067 /* config HFP session:1 playback path */
Derek Chenf7092792017-05-23 12:23:53 -04001068 if (is_bus_dev_usecase) {
1069 app_type = usecase->out_app_type_cfg.app_type;
1070 sample_rate= usecase->out_app_type_cfg.sample_rate;
1071 } else {
1072 snd_device = SND_DEVICE_NONE; // use legacy behavior
1073 app_type = platform_get_default_app_type_v2(adev->platform, PCM_PLAYBACK);
1074 sample_rate= CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
1075 }
Aalique Grahame22e49102018-12-18 14:23:57 -08001076 rc = set_stream_app_type_mixer_ctrl(adev, pcm_device_id, app_type,
1077 acdb_dev_id, sample_rate,
1078 PCM_PLAYBACK,
Derek Chenf7092792017-05-23 12:23:53 -04001079 snd_device);
Aalique Grahame22e49102018-12-18 14:23:57 -08001080 if (rc < 0)
1081 goto exit_send_app_type_cfg;
1082
1083 /* config HFP session:1 capture path */
Derek Chenf7092792017-05-23 12:23:53 -04001084 if (is_bus_dev_usecase) {
1085 snd_device = usecase->in_snd_device;
1086 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_CAPTURE);
1087 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
1088 if (acdb_dev_id < 0) {
1089 ALOGE("%s: Couldn't get the acdb dev id", __func__);
1090 rc = -EINVAL;
1091 goto exit_send_app_type_cfg;
1092 }
1093 app_type = usecase->in_app_type_cfg.app_type;
1094 sample_rate= usecase->in_app_type_cfg.sample_rate;
1095 } else {
1096 snd_device = SND_DEVICE_NONE; // use legacy behavior
1097 app_type = platform_get_default_app_type_v2(adev->platform, PCM_CAPTURE);
1098 }
Aalique Grahame22e49102018-12-18 14:23:57 -08001099 rc = set_stream_app_type_mixer_ctrl(adev, pcm_device_id, app_type,
1100 acdb_dev_id, sample_rate,
1101 PCM_CAPTURE,
Derek Chenf7092792017-05-23 12:23:53 -04001102 snd_device);
Aalique Grahame22e49102018-12-18 14:23:57 -08001103 if (rc < 0)
1104 goto exit_send_app_type_cfg;
1105
Derek Chenf7092792017-05-23 12:23:53 -04001106 if (is_bus_dev_usecase)
1107 goto exit_send_app_type_cfg;
1108
Aalique Grahame22e49102018-12-18 14:23:57 -08001109 /* config HFP session:2 capture path */
1110 pcm_device_id = HFP_ASM_RX_TX;
1111 snd_device = usecase->in_snd_device;
1112 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
1113 if (acdb_dev_id <= 0) {
1114 ALOGE("%s: Couldn't get the acdb dev id", __func__);
1115 rc = -EINVAL;
1116 goto exit_send_app_type_cfg;
1117 }
1118 app_type = platform_get_default_app_type_v2(adev->platform, PCM_CAPTURE);
1119 rc = set_stream_app_type_mixer_ctrl(adev, pcm_device_id, app_type,
1120 acdb_dev_id, sample_rate, PCM_CAPTURE,
1121 SND_DEVICE_NONE);
1122 if (rc < 0)
1123 goto exit_send_app_type_cfg;
1124
1125 /* config HFP session:2 playback path */
1126 app_type = platform_get_default_app_type_v2(adev->platform, PCM_PLAYBACK);
1127 rc = set_stream_app_type_mixer_ctrl(adev, pcm_device_id, app_type,
1128 acdb_dev_id, sample_rate,
1129 PCM_PLAYBACK, SND_DEVICE_NONE);
1130 if (rc < 0)
1131 goto exit_send_app_type_cfg;
1132 }
1133
1134 rc = 0;
1135exit_send_app_type_cfg:
1136 return rc;
1137}
1138
Zhou Song06761dd2019-04-28 18:08:17 +08001139int audio_extn_utils_get_app_sample_rate_for_device(
1140 struct audio_device *adev,
1141 struct audio_usecase *usecase, int snd_device)
1142{
1143 char value[PROPERTY_VALUE_MAX] = {0};
1144 int sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
1145
1146 if ((usecase->type == PCM_PLAYBACK) && (usecase->stream.out != NULL)) {
1147 property_get("vendor.audio.playback.mch.downsample",value,"");
1148 if (!strncmp("true", value, sizeof("true"))) {
1149 if ((popcount(usecase->stream.out->channel_mask) > 2) &&
1150 (usecase->stream.out->app_type_cfg.sample_rate > CODEC_BACKEND_DEFAULT_SAMPLE_RATE) &&
1151 !(usecase->stream.out->flags &
1152 (audio_output_flags_t)AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH))
1153 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
1154 }
1155
1156 if (usecase->id == USECASE_AUDIO_PLAYBACK_VOIP) {
1157 usecase->stream.out->app_type_cfg.sample_rate = usecase->stream.out->sample_rate;
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08001158 } else if (compare_device_type(&usecase->stream.out->device_list,
1159 AUDIO_DEVICE_OUT_SPEAKER)) {
Zhou Song06761dd2019-04-28 18:08:17 +08001160 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
1161 } else if ((snd_device == SND_DEVICE_OUT_HDMI ||
1162 snd_device == SND_DEVICE_OUT_USB_HEADSET ||
1163 snd_device == SND_DEVICE_OUT_DISPLAY_PORT) &&
1164 (usecase->stream.out->sample_rate >= OUTPUT_SAMPLING_RATE_44100)) {
1165 /*
1166 * To best utlize DSP, check if the stream sample rate is supported/multiple of
1167 * configured device sample rate, if not update the COPP rate to be equal to the
1168 * device sample rate, else open COPP at stream sample rate
1169 */
1170 platform_check_and_update_copp_sample_rate(adev->platform, snd_device,
1171 usecase->stream.out->sample_rate,
1172 &usecase->stream.out->app_type_cfg.sample_rate);
1173 } else if (((snd_device != SND_DEVICE_OUT_HEADPHONES_44_1 &&
1174 !audio_is_this_native_usecase(usecase)) &&
1175 usecase->stream.out->sample_rate == OUTPUT_SAMPLING_RATE_44100) ||
1176 (usecase->stream.out->sample_rate < OUTPUT_SAMPLING_RATE_44100)) {
1177 /* Reset to default if no native stream is active*/
1178 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Zhou Song9e9b3812019-12-30 17:52:10 +08001179 } else if (usecase->out_snd_device == SND_DEVICE_OUT_BT_A2DP ||
1180 usecase->out_snd_device == SND_DEVICE_OUT_SPEAKER_AND_BT_A2DP ||
1181 usecase->out_snd_device == SND_DEVICE_OUT_SPEAKER_SAFE_AND_BT_A2DP) {
Zhou Song06761dd2019-04-28 18:08:17 +08001182 /*
1183 * For a2dp playback get encoder sampling rate and set copp sampling rate,
1184 * for bit width use the stream param only.
1185 */
1186 audio_extn_a2dp_get_enc_sample_rate(&usecase->stream.out->app_type_cfg.sample_rate);
1187 ALOGI("%s using %d sample rate rate for A2DP CoPP",
1188 __func__, usecase->stream.out->app_type_cfg.sample_rate);
1189 }
1190 audio_extn_btsco_get_sample_rate(snd_device, &usecase->stream.out->app_type_cfg.sample_rate);
1191 sample_rate = usecase->stream.out->app_type_cfg.sample_rate;
1192
1193 if (((usecase->stream.out->format == AUDIO_FORMAT_E_AC3) ||
1194 (usecase->stream.out->format == AUDIO_FORMAT_E_AC3_JOC) ||
1195 (usecase->stream.out->format == AUDIO_FORMAT_DOLBY_TRUEHD))
1196 && audio_extn_passthru_is_passthrough_stream(usecase->stream.out)
1197 && !audio_extn_passthru_is_convert_supported(adev, usecase->stream.out)) {
1198 sample_rate = sample_rate * 4;
1199 if (sample_rate > HDMI_PASSTHROUGH_MAX_SAMPLE_RATE)
1200 sample_rate = HDMI_PASSTHROUGH_MAX_SAMPLE_RATE;
1201 }
1202 } else if (usecase->type == PCM_CAPTURE) {
1203 if (usecase->stream.in != NULL) {
1204 if (usecase->id == USECASE_AUDIO_RECORD_VOIP)
1205 usecase->stream.in->app_type_cfg.sample_rate = usecase->stream.in->sample_rate;
1206 if (voice_is_in_call_rec_stream(usecase->stream.in)) {
1207 audio_extn_btsco_get_sample_rate(usecase->in_snd_device,
1208 &usecase->stream.in->app_type_cfg.sample_rate);
1209 } else {
1210 audio_extn_btsco_get_sample_rate(snd_device,
1211 &usecase->stream.in->app_type_cfg.sample_rate);
1212 }
1213 sample_rate = usecase->stream.in->app_type_cfg.sample_rate;
1214 } else if (usecase->id == USECASE_AUDIO_SPKR_CALIB_TX) {
Sujin Panickerb6d76262019-09-09 10:55:38 +05301215 if ((property_get("vendor.audio.spkr_prot.tx.sampling_rate", value, NULL) > 0))
1216 sample_rate = atoi(value);
1217 else
1218 sample_rate = SAMPLE_RATE_8000;
Zhou Song06761dd2019-04-28 18:08:17 +08001219 }
1220 } else if (usecase->type == TRANSCODE_LOOPBACK_RX) {
1221 sample_rate = usecase->stream.inout->out_config.sample_rate;
1222 }
1223 return sample_rate;
1224}
1225
Siena Richard7c2db772016-12-21 11:32:34 -08001226static int send_app_type_cfg_for_device(struct audio_device *adev,
1227 struct audio_usecase *usecase,
1228 int split_snd_device)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001229{
1230 char mixer_ctl_name[MAX_LENGTH_MIXER_CONTROL_IN_INT];
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301231 size_t app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT] = {0};
1232 int len = 0, rc;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001233 struct mixer_ctl *ctl;
Siena Richard7c2db772016-12-21 11:32:34 -08001234 int pcm_device_id = 0, acdb_dev_id, app_type;
1235 int snd_device = split_snd_device, snd_device_be_idx = -1;
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +05301236 int32_t sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Varun Balaraje49253e2017-07-06 19:48:56 +05301237 struct streams_io_cfg *s_info = NULL;
1238 struct listnode *node = NULL;
Nikhil Laturkarac4a7492017-08-31 18:27:19 +05301239 int bd_app_type = 0;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001240
Siena Richard7c2db772016-12-21 11:32:34 -08001241 ALOGV("%s: usecase->out_snd_device %s, usecase->in_snd_device %s, split_snd_device %s",
1242 __func__, platform_get_snd_device_name(usecase->out_snd_device),
1243 platform_get_snd_device_name(usecase->in_snd_device),
1244 platform_get_snd_device_name(split_snd_device));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001245
Siddartha Shaik343abc62017-08-08 11:15:25 +05301246 if (usecase->type != PCM_PLAYBACK && usecase->type != PCM_CAPTURE &&
Surendar Karka93cd25a2018-08-28 14:21:37 +05301247 usecase->type != TRANSCODE_LOOPBACK_RX) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301248 ALOGE("%s: not a playback/capture path, no need to cfg app type", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001249 rc = 0;
1250 goto exit_send_app_type_cfg;
1251 }
1252 if ((usecase->id != USECASE_AUDIO_PLAYBACK_DEEP_BUFFER) &&
Vignesh Kulothungan0a2eff02019-07-11 16:30:13 -07001253 (usecase->id != USECASE_AUDIO_PLAYBACK_WITH_HAPTICS) &&
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001254 (usecase->id != USECASE_AUDIO_PLAYBACK_LOW_LATENCY) &&
1255 (usecase->id != USECASE_AUDIO_PLAYBACK_MULTI_CH) &&
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -08001256 (usecase->id != USECASE_AUDIO_PLAYBACK_ULL) &&
Vikram Panduranga93f080e2017-06-07 18:16:14 -07001257 (usecase->id != USECASE_AUDIO_PLAYBACK_VOIP) &&
Surendar Karka93cd25a2018-08-28 14:21:37 +05301258 (usecase->id != USECASE_AUDIO_TRANSCODE_LOOPBACK_RX) &&
Varun Balaraje49253e2017-07-06 19:48:56 +05301259 (!is_interactive_usecase(usecase->id)) &&
Srikanth Uyyala9d551402015-08-25 16:03:42 +05301260 (!is_offload_usecase(usecase->id)) &&
Derek Chenf6318be2017-06-12 17:16:24 -04001261 (usecase->type != PCM_CAPTURE) &&
1262 (!audio_extn_auto_hal_is_bus_device_usecase(usecase->id))) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301263 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 -07001264 rc = 0;
1265 goto exit_send_app_type_cfg;
1266 }
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -08001267
1268 //if VR is active then only send the mixer control
1269 if (usecase->id == USECASE_AUDIO_PLAYBACK_ULL && !audio_is_vr_mode_on(adev)) {
1270 ALOGI("ULL doesnt need sending app type cfg, returning");
1271 rc = 0;
1272 goto exit_send_app_type_cfg;
1273 }
1274
Surendar Karka93cd25a2018-08-28 14:21:37 +05301275 if (usecase->type == PCM_PLAYBACK || usecase->type == TRANSCODE_LOOPBACK_RX) {
Ben Romberger1fafdde2015-09-09 19:43:15 -07001276 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_PLAYBACK);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301277 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
1278 "Audio Stream %d App Type Cfg", pcm_device_id);
Ben Romberger1fafdde2015-09-09 19:43:15 -07001279 } else if (usecase->type == PCM_CAPTURE) {
Ben Romberger1fafdde2015-09-09 19:43:15 -07001280 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_CAPTURE);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301281 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
1282 "Audio Stream Capture %d App Type Cfg", pcm_device_id);
Srikanth Uyyala9d551402015-08-25 16:03:42 +05301283 }
Siena Richard7c2db772016-12-21 11:32:34 -08001284
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001285 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
1286 if (!ctl) {
1287 ALOGE("%s: Could not get ctl for mixer cmd - %s", __func__,
1288 mixer_ctl_name);
1289 rc = -EINVAL;
1290 goto exit_send_app_type_cfg;
1291 }
Aditya Bavanari701a6992017-03-30 19:17:16 +05301292 snd_device = platform_get_spkr_prot_snd_device(snd_device);
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301293 if (voice_is_in_call_rec_stream(usecase->stream.in) && usecase->type == PCM_CAPTURE) {
1294 snd_device_t voice_device = voice_get_incall_rec_snd_device(usecase->in_snd_device);
1295 acdb_dev_id = platform_get_snd_device_acdb_id(voice_device);
1296 ALOGV("acdb id for voice call use case %d", acdb_dev_id);
1297 } else {
1298 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
1299 }
Rohit Kumar1181b292017-01-31 18:07:17 +05301300 if (acdb_dev_id <= 0) {
1301 ALOGE("%s: Couldn't get the acdb dev id", __func__);
1302 rc = -EINVAL;
1303 goto exit_send_app_type_cfg;
1304 }
1305
Siena Richard7c2db772016-12-21 11:32:34 -08001306 snd_device_be_idx = platform_get_snd_device_backend_index(snd_device);
1307 if (snd_device_be_idx < 0) {
1308 ALOGE("%s: Couldn't get the backend index for snd device %s ret=%d",
1309 __func__, platform_get_snd_device_name(snd_device),
1310 snd_device_be_idx);
1311 }
1312
Zhou Song06761dd2019-04-28 18:08:17 +08001313 sample_rate = audio_extn_utils_get_app_sample_rate_for_device(adev, usecase, snd_device);
1314
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301315 if ((usecase->type == PCM_PLAYBACK) && (usecase->stream.out != NULL)) {
Varun Balaraje49253e2017-07-06 19:48:56 +05301316 /* Interactive streams are supported with only direct app type id.
1317 * Get Direct profile app type and use it for interactive streams
1318 */
1319 list_for_each(node, &adev->streams_output_cfg_list) {
1320 s_info = node_to_item(node, struct streams_io_cfg, list);
Aalique Grahame5cd12ff2017-10-19 10:57:00 -07001321 if (s_info->flags.out_flags == (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_BD |
Nikhil Laturkarac4a7492017-08-31 18:27:19 +05301322 AUDIO_OUTPUT_FLAG_DIRECT_PCM |
1323 AUDIO_OUTPUT_FLAG_DIRECT))
1324 bd_app_type = s_info->app_type_cfg.app_type;
Varun Balaraje49253e2017-07-06 19:48:56 +05301325 }
Aalique Grahame5cd12ff2017-10-19 10:57:00 -07001326 if (usecase->stream.out->flags == (audio_output_flags_t)AUDIO_OUTPUT_FLAG_INTERACTIVE)
Nikhil Laturkarac4a7492017-08-31 18:27:19 +05301327 app_type = bd_app_type;
Varun Balaraje49253e2017-07-06 19:48:56 +05301328 else
1329 app_type = usecase->stream.out->app_type_cfg.app_type;
Siena Richard7c2db772016-12-21 11:32:34 -08001330 app_type_cfg[len++] = app_type;
Mingming Yin21854652016-04-13 11:54:02 -07001331 app_type_cfg[len++] = acdb_dev_id;
Naresh Tanniru3a406772017-05-10 13:09:05 -07001332 app_type_cfg[len++] = sample_rate;
1333
Siena Richard7c2db772016-12-21 11:32:34 -08001334 if (snd_device_be_idx > 0)
1335 app_type_cfg[len++] = snd_device_be_idx;
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301336
Siena Richard7c2db772016-12-21 11:32:34 -08001337 ALOGI("%s PLAYBACK app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
1338 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301339
1340 } else if ((usecase->type == PCM_CAPTURE) && (usecase->stream.in != NULL)) {
Siena Richard7c2db772016-12-21 11:32:34 -08001341 app_type = usecase->stream.in->app_type_cfg.app_type;
1342 app_type_cfg[len++] = app_type;
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301343 app_type_cfg[len++] = acdb_dev_id;
Siena Richard7c2db772016-12-21 11:32:34 -08001344 app_type_cfg[len++] = sample_rate;
1345 if (snd_device_be_idx > 0)
1346 app_type_cfg[len++] = snd_device_be_idx;
1347 ALOGI("%s CAPTURE app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
1348 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301349 } else {
Siena Richard7c2db772016-12-21 11:32:34 -08001350 app_type = platform_get_default_app_type_v2(adev->platform, usecase->type);
Surendar Karka93cd25a2018-08-28 14:21:37 +05301351 if(usecase->type == TRANSCODE_LOOPBACK_RX) {
Siddartha Shaik343abc62017-08-08 11:15:25 +05301352 app_type = usecase->stream.inout->out_app_type_cfg.app_type;
1353 }
Siena Richard7c2db772016-12-21 11:32:34 -08001354 app_type_cfg[len++] = app_type;
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301355 app_type_cfg[len++] = acdb_dev_id;
1356 app_type_cfg[len++] = sample_rate;
Siena Richard7c2db772016-12-21 11:32:34 -08001357 if (snd_device_be_idx > 0)
1358 app_type_cfg[len++] = snd_device_be_idx;
1359 ALOGI("%s default app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
1360 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Ashish Jainc02430d2016-01-04 10:42:43 +05301361 }
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301362
Siddartha Shaik343abc62017-08-08 11:15:25 +05301363 if(ctl)
1364 mixer_ctl_set_array(ctl, app_type_cfg, len);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001365 rc = 0;
1366exit_send_app_type_cfg:
1367 return rc;
1368}
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001369
Deeraj Soman14230922019-01-30 16:39:30 +05301370static int audio_extn_utils_check_input_parameters(uint32_t sample_rate,
1371 audio_format_t format,
1372 int channel_count)
1373{
1374 int ret = 0;
1375
1376 if (((format != AUDIO_FORMAT_PCM_16_BIT) && (format != AUDIO_FORMAT_PCM_8_24_BIT) &&
1377 (format != AUDIO_FORMAT_PCM_24_BIT_PACKED) && (format != AUDIO_FORMAT_PCM_32_BIT) &&
1378 (format != AUDIO_FORMAT_PCM_FLOAT)) &&
1379 !voice_extn_compress_voip_is_format_supported(format) &&
1380 !audio_extn_compr_cap_format_supported(format) &&
1381 !audio_extn_cin_format_supported(format))
1382 ret = -EINVAL;
1383
1384 switch (channel_count) {
1385 case 1:
1386 case 2:
1387 case 3:
1388 case 4:
1389 case 6:
1390 case 8:
1391 break;
1392 default:
1393 ret = -EINVAL;
1394 }
1395
1396 switch (sample_rate) {
1397 case 8000:
1398 case 11025:
1399 case 12000:
1400 case 16000:
1401 case 22050:
1402 case 24000:
1403 case 32000:
1404 case 44100:
1405 case 48000:
1406 case 88200:
1407 case 96000:
1408 case 176400:
1409 case 192000:
1410 break;
1411 default:
1412 ret = -EINVAL;
1413 }
1414
1415 return ret;
1416}
1417
1418static inline uint32_t audio_extn_utils_nearest_multiple(uint32_t num, uint32_t multiplier)
1419{
1420 uint32_t remainder = 0;
1421
1422 if (!multiplier)
1423 return num;
1424
1425 remainder = num % multiplier;
1426 if (remainder)
1427 num += (multiplier - remainder);
1428
1429 return num;
1430}
1431
1432static inline uint32_t audio_extn_utils_lcm(uint32_t num1, uint32_t num2)
1433{
1434 uint32_t high = num1, low = num2, temp = 0;
1435
1436 if (!num1 || !num2)
1437 return 0;
1438
1439 if (num1 < num2) {
1440 high = num2;
1441 low = num1;
1442 }
1443
1444 while (low != 0) {
1445 temp = low;
1446 low = high % low;
1447 high = temp;
1448 }
1449 return (num1 * num2)/high;
1450}
1451
Siena Richard7c2db772016-12-21 11:32:34 -08001452int audio_extn_utils_send_app_type_cfg(struct audio_device *adev,
1453 struct audio_usecase *usecase)
1454{
1455 int i, num_devices = 0;
1456 snd_device_t new_snd_devices[SND_DEVICE_OUT_END] = {0};
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301457 snd_device_t in_snd_device = usecase->in_snd_device;
Siena Richard7c2db772016-12-21 11:32:34 -08001458 int rc = 0;
1459
Aalique Grahame22e49102018-12-18 14:23:57 -08001460 if (usecase->type == PCM_HFP_CALL) {
1461 return audio_extn_utils_send_app_type_cfg_hfp(adev, usecase);
1462 }
1463
Siena Richard7c2db772016-12-21 11:32:34 -08001464 switch (usecase->type) {
1465 case PCM_PLAYBACK:
Surendar Karka93cd25a2018-08-28 14:21:37 +05301466 case TRANSCODE_LOOPBACK_RX:
Siena Richard7c2db772016-12-21 11:32:34 -08001467 ALOGD("%s: usecase->out_snd_device %s",
1468 __func__, platform_get_snd_device_name(usecase->out_snd_device));
1469 /* check for out combo device */
1470 if (platform_split_snd_device(adev->platform,
1471 usecase->out_snd_device,
1472 &num_devices, new_snd_devices)) {
1473 new_snd_devices[0] = usecase->out_snd_device;
1474 num_devices = 1;
1475 }
1476 break;
1477 case PCM_CAPTURE:
1478 ALOGD("%s: usecase->in_snd_device %s",
1479 __func__, platform_get_snd_device_name(usecase->in_snd_device));
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301480 if (voice_is_in_call_rec_stream(usecase->stream.in)) {
1481 in_snd_device = voice_get_incall_rec_backend_device(usecase->stream.in);
1482 }
Siena Richard7c2db772016-12-21 11:32:34 -08001483 /* check for in combo device */
1484 if (platform_split_snd_device(adev->platform,
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301485 in_snd_device,
Siena Richard7c2db772016-12-21 11:32:34 -08001486 &num_devices, new_snd_devices)) {
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301487 new_snd_devices[0] = in_snd_device;
Siena Richard7c2db772016-12-21 11:32:34 -08001488 num_devices = 1;
1489 }
1490 break;
1491 default:
1492 ALOGI("%s: not a playback/capture path, no need to cfg app type", __func__);
1493 rc = 0;
1494 break;
1495 }
1496
1497 for (i = 0; i < num_devices; i++) {
1498 rc = send_app_type_cfg_for_device(adev, usecase, new_snd_devices[i]);
1499 if (rc)
1500 break;
1501 }
1502
1503 return rc;
1504}
1505
Preetam Singh Ranawat9519e9c2015-11-18 16:05:55 +05301506int read_line_from_file(const char *path, char *buf, size_t count)
1507{
1508 char * fgets_ret;
1509 FILE * fd;
1510 int rv;
1511
1512 fd = fopen(path, "r");
1513 if (fd == NULL)
1514 return -1;
1515
1516 fgets_ret = fgets(buf, (int)count, fd);
1517 if (NULL != fgets_ret) {
1518 rv = (int)strlen(buf);
1519 } else {
1520 rv = ferror(fd);
1521 }
1522 fclose(fd);
1523
1524 return rv;
1525}
1526
Ashish Jainf1eaa582016-05-23 20:54:24 +05301527/*Translates ALSA formats to AOSP PCM formats*/
1528audio_format_t alsa_format_to_hal(uint32_t alsa_format)
1529{
1530 audio_format_t format;
1531
1532 switch(alsa_format) {
1533 case SNDRV_PCM_FORMAT_S16_LE:
1534 format = AUDIO_FORMAT_PCM_16_BIT;
1535 break;
1536 case SNDRV_PCM_FORMAT_S24_3LE:
1537 format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
1538 break;
1539 case SNDRV_PCM_FORMAT_S24_LE:
1540 format = AUDIO_FORMAT_PCM_8_24_BIT;
1541 break;
1542 case SNDRV_PCM_FORMAT_S32_LE:
1543 format = AUDIO_FORMAT_PCM_32_BIT;
1544 break;
1545 default:
1546 ALOGW("Incorrect ALSA format");
1547 format = AUDIO_FORMAT_INVALID;
1548 }
1549 return format;
1550}
1551
1552/*Translates hal format (AOSP) to alsa formats*/
1553uint32_t hal_format_to_alsa(audio_format_t hal_format)
1554{
1555 uint32_t alsa_format;
1556
1557 switch (hal_format) {
1558 case AUDIO_FORMAT_PCM_32_BIT: {
1559 if (platform_supports_true_32bit())
1560 alsa_format = SNDRV_PCM_FORMAT_S32_LE;
1561 else
1562 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1563 }
1564 break;
1565 case AUDIO_FORMAT_PCM_8_BIT:
1566 alsa_format = SNDRV_PCM_FORMAT_S8;
1567 break;
1568 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
1569 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1570 break;
1571 case AUDIO_FORMAT_PCM_8_24_BIT: {
1572 if (platform_supports_true_32bit())
1573 alsa_format = SNDRV_PCM_FORMAT_S32_LE;
1574 else
1575 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1576 }
1577 break;
1578 case AUDIO_FORMAT_PCM_FLOAT:
1579 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1580 break;
1581 default:
1582 case AUDIO_FORMAT_PCM_16_BIT:
1583 alsa_format = SNDRV_PCM_FORMAT_S16_LE;
1584 break;
1585 }
1586 return alsa_format;
1587}
1588
Ashish Jain83a6cc22016-06-28 14:34:17 +05301589/*Translates PCM formats to AOSP formats*/
1590audio_format_t pcm_format_to_hal(uint32_t pcm_format)
1591{
1592 audio_format_t format = AUDIO_FORMAT_INVALID;
1593
1594 switch(pcm_format) {
1595 case PCM_FORMAT_S16_LE:
1596 format = AUDIO_FORMAT_PCM_16_BIT;
1597 break;
1598 case PCM_FORMAT_S24_3LE:
1599 format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
1600 break;
1601 case PCM_FORMAT_S24_LE:
1602 format = AUDIO_FORMAT_PCM_8_24_BIT;
1603 break;
1604 case PCM_FORMAT_S32_LE:
1605 format = AUDIO_FORMAT_PCM_32_BIT;
1606 break;
1607 default:
1608 ALOGW("Incorrect PCM format");
1609 format = AUDIO_FORMAT_INVALID;
1610 }
1611 return format;
1612}
1613
1614/*Translates hal format (AOSP) to alsa formats*/
1615uint32_t hal_format_to_pcm(audio_format_t hal_format)
1616{
1617 uint32_t pcm_format;
1618
1619 switch (hal_format) {
1620 case AUDIO_FORMAT_PCM_32_BIT:
1621 case AUDIO_FORMAT_PCM_8_24_BIT:
1622 case AUDIO_FORMAT_PCM_FLOAT: {
1623 if (platform_supports_true_32bit())
1624 pcm_format = PCM_FORMAT_S32_LE;
1625 else
1626 pcm_format = PCM_FORMAT_S24_3LE;
1627 }
1628 break;
1629 case AUDIO_FORMAT_PCM_8_BIT:
1630 pcm_format = PCM_FORMAT_S8;
1631 break;
1632 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
1633 pcm_format = PCM_FORMAT_S24_3LE;
1634 break;
1635 default:
1636 case AUDIO_FORMAT_PCM_16_BIT:
1637 pcm_format = PCM_FORMAT_S16_LE;
1638 break;
1639 }
1640 return pcm_format;
1641}
1642
Ashish Jainf1eaa582016-05-23 20:54:24 +05301643uint32_t get_alsa_fragment_size(uint32_t bytes_per_sample,
1644 uint32_t sample_rate,
Deeraj Soman65358ab2019-02-07 15:40:49 +05301645 uint32_t noOfChannels,
1646 int64_t duration_ms)
Ashish Jainf1eaa582016-05-23 20:54:24 +05301647{
1648 uint32_t fragment_size = 0;
1649 uint32_t pcm_offload_time = PCM_OFFLOAD_BUFFER_DURATION;
1650
Deeraj Soman65358ab2019-02-07 15:40:49 +05301651 if (duration_ms >= MIN_OFFLOAD_BUFFER_DURATION_MS && duration_ms <= MAX_OFFLOAD_BUFFER_DURATION_MS)
1652 pcm_offload_time = duration_ms;
1653
Ashish Jainf1eaa582016-05-23 20:54:24 +05301654 fragment_size = (pcm_offload_time
1655 * sample_rate
1656 * bytes_per_sample
1657 * noOfChannels)/1000;
1658 if (fragment_size < MIN_PCM_OFFLOAD_FRAGMENT_SIZE)
1659 fragment_size = MIN_PCM_OFFLOAD_FRAGMENT_SIZE;
1660 else if (fragment_size > MAX_PCM_OFFLOAD_FRAGMENT_SIZE)
1661 fragment_size = MAX_PCM_OFFLOAD_FRAGMENT_SIZE;
1662 /*To have same PCM samples for all channels, the buffer size requires to
1663 *be multiple of (number of channels * bytes per sample)
1664 *For writes to succeed, the buffer must be written at address which is multiple of 32
1665 */
Aalique Grahameb85f2d92017-10-16 17:53:23 -07001666 fragment_size = ALIGN(fragment_size, (bytes_per_sample * noOfChannels * 32));
Ashish Jainf1eaa582016-05-23 20:54:24 +05301667
1668 ALOGI("PCM offload Fragment size to %d bytes", fragment_size);
1669 return fragment_size;
1670}
1671
1672/* Calculates the fragment size required to configure compress session.
1673 * Based on the alsa format selected, decide if conversion is needed in
1674
1675 * HAL ( e.g. convert AUDIO_FORMAT_PCM_FLOAT input format to
1676 * AUDIO_FORMAT_PCM_24_BIT_PACKED before writing to the compress driver.
1677 */
1678void audio_extn_utils_update_direct_pcm_fragment_size(struct stream_out *out)
1679{
Ashish Jain83a6cc22016-06-28 14:34:17 +05301680 audio_format_t dst_format = out->hal_op_format;
1681 audio_format_t src_format = out->hal_ip_format;
Ashish Jainf1eaa582016-05-23 20:54:24 +05301682 uint32_t hal_op_bytes_per_sample = audio_bytes_per_sample(dst_format);
1683 uint32_t hal_ip_bytes_per_sample = audio_bytes_per_sample(src_format);
1684
1685 out->compr_config.fragment_size =
1686 get_alsa_fragment_size(hal_op_bytes_per_sample,
1687 out->sample_rate,
Deeraj Soman65358ab2019-02-07 15:40:49 +05301688 popcount(out->channel_mask),
1689 out->info.duration_us / 1000);
Ashish Jainf1eaa582016-05-23 20:54:24 +05301690
1691 if ((src_format != dst_format) &&
1692 hal_op_bytes_per_sample != hal_ip_bytes_per_sample) {
1693
Ashish Jain83a6cc22016-06-28 14:34:17 +05301694 out->hal_fragment_size =
Ashish Jainf1eaa582016-05-23 20:54:24 +05301695 ((out->compr_config.fragment_size * hal_ip_bytes_per_sample) /
1696 hal_op_bytes_per_sample);
1697 ALOGI("enable conversion hal_input_fragment_size is %d src_format %x dst_format %x",
Ashish Jain83a6cc22016-06-28 14:34:17 +05301698 out->hal_fragment_size, src_format, dst_format);
Ashish Jainf1eaa582016-05-23 20:54:24 +05301699 } else {
Ashish Jain83a6cc22016-06-28 14:34:17 +05301700 out->hal_fragment_size = out->compr_config.fragment_size;
Ashish Jainf1eaa582016-05-23 20:54:24 +05301701 }
1702}
1703
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301704/* converts pcm format 24_8 to 8_24 inplace */
1705size_t audio_extn_utils_convert_format_24_8_to_8_24(void *buf, size_t bytes)
1706{
1707 size_t i = 0;
1708 int *int_buf_stream = buf;
1709
1710 if ((bytes % 4) != 0) {
1711 ALOGE("%s: wrong inout buffer! ... is not 32 bit aligned ", __func__);
1712 return -EINVAL;
1713 }
1714
1715 for (; i < (bytes / 4); i++)
1716 int_buf_stream[i] >>= 8;
1717
1718 return bytes;
1719}
1720
Xiaojun Sang782e5b12020-06-29 21:13:06 +08001721#ifdef AUDIO_GKI_ENABLED
1722int get_snd_codec_id(audio_format_t format)
1723{
1724 int id = 0;
1725
1726 switch (format & AUDIO_FORMAT_MAIN_MASK) {
1727 case AUDIO_FORMAT_MP3:
1728 id = SND_AUDIOCODEC_MP3;
1729 break;
1730 case AUDIO_FORMAT_AAC:
1731 id = SND_AUDIOCODEC_AAC;
1732 break;
1733 case AUDIO_FORMAT_AAC_ADTS:
1734 id = SND_AUDIOCODEC_AAC;
1735 break;
1736 case AUDIO_FORMAT_AAC_LATM:
1737 id = SND_AUDIOCODEC_AAC;
1738 break;
1739 case AUDIO_FORMAT_PCM:
1740 id = SND_AUDIOCODEC_PCM;
1741 break;
1742 case AUDIO_FORMAT_FLAC:
1743 case AUDIO_FORMAT_ALAC:
1744 case AUDIO_FORMAT_APE:
1745 case AUDIO_FORMAT_VORBIS:
1746 case AUDIO_FORMAT_WMA:
1747 case AUDIO_FORMAT_WMA_PRO:
1748 case AUDIO_FORMAT_DSD:
1749 case AUDIO_FORMAT_APTX:
1750 id = SND_AUDIOCODEC_BESPOKE;
1751 break;
1752 case AUDIO_FORMAT_MP2:
1753 id = SND_AUDIOCODEC_MP2;
1754 break;
1755 case AUDIO_FORMAT_AC3:
1756 id = SND_AUDIOCODEC_AC3;
1757 break;
1758 case AUDIO_FORMAT_E_AC3:
1759 case AUDIO_FORMAT_E_AC3_JOC:
1760 id = SND_AUDIOCODEC_EAC3;
1761 break;
1762 case AUDIO_FORMAT_DTS:
1763 case AUDIO_FORMAT_DTS_HD:
1764 id = SND_AUDIOCODEC_DTS;
1765 break;
1766 case AUDIO_FORMAT_DOLBY_TRUEHD:
1767 id = SND_AUDIOCODEC_TRUEHD;
1768 break;
1769 case AUDIO_FORMAT_IEC61937:
1770 id = SND_AUDIOCODEC_IEC61937;
1771 break;
1772 default:
1773 ALOGE("%s: Unsupported audio format :%x", __func__, format);
1774 }
1775
1776 return id;
1777}
1778#else
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301779int get_snd_codec_id(audio_format_t format)
1780{
1781 int id = 0;
1782
1783 switch (format & AUDIO_FORMAT_MAIN_MASK) {
1784 case AUDIO_FORMAT_MP3:
1785 id = SND_AUDIOCODEC_MP3;
1786 break;
1787 case AUDIO_FORMAT_AAC:
1788 id = SND_AUDIOCODEC_AAC;
1789 break;
1790 case AUDIO_FORMAT_AAC_ADTS:
1791 id = SND_AUDIOCODEC_AAC;
1792 break;
Arun Kumar Dasari3b174182016-12-27 13:01:14 +05301793 case AUDIO_FORMAT_AAC_LATM:
1794 id = SND_AUDIOCODEC_AAC;
1795 break;
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301796 case AUDIO_FORMAT_PCM:
1797 id = SND_AUDIOCODEC_PCM;
1798 break;
1799 case AUDIO_FORMAT_FLAC:
1800 id = SND_AUDIOCODEC_FLAC;
1801 break;
1802 case AUDIO_FORMAT_ALAC:
1803 id = SND_AUDIOCODEC_ALAC;
1804 break;
1805 case AUDIO_FORMAT_APE:
1806 id = SND_AUDIOCODEC_APE;
1807 break;
1808 case AUDIO_FORMAT_VORBIS:
1809 id = SND_AUDIOCODEC_VORBIS;
1810 break;
1811 case AUDIO_FORMAT_WMA:
1812 id = SND_AUDIOCODEC_WMA;
1813 break;
1814 case AUDIO_FORMAT_WMA_PRO:
1815 id = SND_AUDIOCODEC_WMA_PRO;
1816 break;
Satish Babu Patakokila0c313922016-12-08 12:07:08 +05301817 case AUDIO_FORMAT_MP2:
1818 id = SND_AUDIOCODEC_MP2;
1819 break;
Satish Babu Patakokila915ecba2017-01-10 17:43:56 +05301820 case AUDIO_FORMAT_AC3:
1821 id = SND_AUDIOCODEC_AC3;
1822 break;
1823 case AUDIO_FORMAT_E_AC3:
1824 case AUDIO_FORMAT_E_AC3_JOC:
1825 id = SND_AUDIOCODEC_EAC3;
1826 break;
1827 case AUDIO_FORMAT_DTS:
1828 case AUDIO_FORMAT_DTS_HD:
1829 id = SND_AUDIOCODEC_DTS;
1830 break;
Ben Romberger1aaaf862017-04-06 17:49:46 -07001831 case AUDIO_FORMAT_DOLBY_TRUEHD:
1832 id = SND_AUDIOCODEC_TRUEHD;
1833 break;
Naresh Tanniru928f0862017-04-07 16:44:23 -07001834 case AUDIO_FORMAT_IEC61937:
1835 id = SND_AUDIOCODEC_IEC61937;
1836 break;
Preetam Singh Ranawat4277a5a2017-01-18 19:02:24 +05301837 case AUDIO_FORMAT_DSD:
1838 id = SND_AUDIOCODEC_DSD;
1839 break;
Dhanalakshmi Siddani18737932016-11-29 17:33:17 +05301840 case AUDIO_FORMAT_APTX:
1841 id = SND_AUDIOCODEC_APTX;
1842 break;
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301843 default:
1844 ALOGE("%s: Unsupported audio format :%x", __func__, format);
1845 }
1846
1847 return id;
1848}
Xiaojun Sang782e5b12020-06-29 21:13:06 +08001849#endif
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301850
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001851void audio_extn_utils_send_audio_calibration(struct audio_device *adev,
1852 struct audio_usecase *usecase)
1853{
1854 int type = usecase->type;
1855
Dhananjay Kumar14245982017-01-16 20:21:00 +05301856 if (type == PCM_PLAYBACK && usecase->stream.out != NULL) {
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05301857 platform_send_audio_calibration(adev->platform, usecase,
Zhou Song06761dd2019-04-28 18:08:17 +08001858 usecase->stream.out->app_type_cfg.app_type);
Dhananjay Kumar14245982017-01-16 20:21:00 +05301859 } else if (type == PCM_CAPTURE && usecase->stream.in != NULL) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301860 platform_send_audio_calibration(adev->platform, usecase,
Zhou Song06761dd2019-04-28 18:08:17 +08001861 usecase->stream.in->app_type_cfg.app_type);
1862 } else if ((type == PCM_HFP_CALL) || (type == PCM_CAPTURE) ||
1863 (type == TRANSCODE_LOOPBACK_RX && usecase->stream.inout != NULL)) {
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05301864 platform_send_audio_calibration(adev->platform, usecase,
Zhou Song06761dd2019-04-28 18:08:17 +08001865 platform_get_default_app_type_v2(adev->platform, usecase->type));
Vidyakumar Athotae57f6002017-03-01 13:13:16 -08001866 } else {
1867 /* No need to send audio calibration for voice and voip call usecases */
1868 if ((type != VOICE_CALL) && (type != VOIP_CALL))
1869 ALOGW("%s: No audio calibration for usecase type = %d", __func__, type);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001870 }
1871}
1872
Ben Rombergera04fabc2014-11-14 12:16:03 -08001873// Base64 Encode and Decode
1874// Not all features supported. This must be used only with following conditions.
1875// Decode Modes: Support with and without padding
1876// CRLF not handling. So no CRLF in string to decode.
1877// Encode Modes: Supports only padding
1878int b64decode(char *inp, int ilen, uint8_t* outp)
1879{
1880 int i, j, k, ii, num;
1881 int rem, pcnt;
1882 uint32_t res=0;
1883 uint8_t getIndex[MAX_BASEINDEX_LEN];
1884 uint8_t tmp, cflag;
1885
1886 if(inp == NULL || outp == NULL || ilen <= 0) {
1887 ALOGE("[%s] received NULL pointer or zero length",__func__);
1888 return -1;
1889 }
1890
1891 memset(getIndex, MAX_BASEINDEX_LEN-1, sizeof(getIndex));
1892 for(i=0;i<BASE_TABLE_SIZE;i++) {
1893 getIndex[(uint8_t)bTable[i]] = (uint8_t)i;
1894 }
1895 getIndex[(uint8_t)'=']=0;
1896
1897 j=0;k=0;
1898 num = ilen/4;
1899 rem = ilen%4;
1900 if(rem==0)
1901 num = num-1;
1902 cflag=0;
1903 for(i=0; i<num; i++) {
1904 res=0;
1905 for(ii=0;ii<4;ii++) {
1906 res = res << 6;
1907 tmp = getIndex[(uint8_t)inp[j++]];
1908 res = res | tmp;
1909 cflag = cflag | tmp;
1910 }
1911 outp[k++] = (res >> 16)&0xFF;
1912 outp[k++] = (res >> 8)&0xFF;
1913 outp[k++] = res & 0xFF;
1914 }
1915
1916 // Handle last bytes special
1917 pcnt=0;
1918 if(rem == 0) {
1919 //With padding or full data
1920 res = 0;
1921 for(ii=0;ii<4;ii++) {
1922 if(inp[j] == '=')
1923 pcnt++;
1924 res = res << 6;
1925 tmp = getIndex[(uint8_t)inp[j++]];
1926 res = res | tmp;
1927 cflag = cflag | tmp;
1928 }
1929 outp[k++] = res >> 16;
1930 if(pcnt == 2)
1931 goto done;
1932 outp[k++] = (res>>8)&0xFF;
1933 if(pcnt == 1)
1934 goto done;
1935 outp[k++] = res&0xFF;
1936 } else {
1937 //without padding
1938 res = 0;
1939 for(i=0;i<rem;i++) {
1940 res = res << 6;
1941 tmp = getIndex[(uint8_t)inp[j++]];
1942 res = res | tmp;
1943 cflag = cflag | tmp;
1944 }
1945 for(i=rem;i<4;i++) {
1946 res = res << 6;
1947 pcnt++;
1948 }
1949 outp[k++] = res >> 16;
1950 if(pcnt == 2)
1951 goto done;
1952 outp[k++] = (res>>8)&0xFF;
1953 if(pcnt == 1)
1954 goto done;
1955 outp[k++] = res&0xFF;
1956 }
1957done:
1958 if(cflag == 0xFF) {
1959 ALOGE("[%s] base64 decode failed. Invalid character found %s",
1960 __func__, inp);
1961 return 0;
1962 }
1963 return k;
1964}
1965
1966int b64encode(uint8_t *inp, int ilen, char* outp)
1967{
1968 int i,j,k, num;
1969 int rem=0;
1970 uint32_t res=0;
1971
1972 if(inp == NULL || outp == NULL || ilen<=0) {
1973 ALOGE("[%s] received NULL pointer or zero input length",__func__);
1974 return -1;
1975 }
1976
1977 num = ilen/3;
1978 rem = ilen%3;
1979 j=0;k=0;
1980 for(i=0; i<num; i++) {
1981 //prepare index
1982 res = inp[j++]<<16;
1983 res = res | inp[j++]<<8;
1984 res = res | inp[j++];
1985 //get output map from index
1986 outp[k++] = (char) bTable[(res>>18)&0x3F];
1987 outp[k++] = (char) bTable[(res>>12)&0x3F];
1988 outp[k++] = (char) bTable[(res>>6)&0x3F];
1989 outp[k++] = (char) bTable[res&0x3F];
1990 }
1991
1992 switch(rem) {
1993 case 1:
1994 res = inp[j++]<<16;
1995 outp[k++] = (char) bTable[res>>18];
1996 outp[k++] = (char) bTable[(res>>12)&0x3F];
1997 //outp[k++] = '=';
1998 //outp[k++] = '=';
1999 break;
2000 case 2:
2001 res = inp[j++]<<16;
2002 res = res | inp[j++]<<8;
2003 outp[k++] = (char) bTable[res>>18];
2004 outp[k++] = (char) bTable[(res>>12)&0x3F];
2005 outp[k++] = (char) bTable[(res>>6)&0x3F];
2006 //outp[k++] = '=';
2007 break;
2008 default:
2009 break;
2010 }
Ben Rombergera04fabc2014-11-14 12:16:03 -08002011 outp[k] = '\0';
2012 return k;
2013}
Ashish Jain81eb2a82015-05-13 10:52:34 +05302014
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05302015
2016int audio_extn_utils_get_codec_version(const char *snd_card_name,
2017 int card_num,
2018 char *codec_version)
2019{
2020 char procfs_path[50];
2021 FILE *fp;
2022
2023 if (strstr(snd_card_name, "tasha")) {
2024 snprintf(procfs_path, sizeof(procfs_path),
2025 "/proc/asound/card%d/codecs/tasha/version", card_num);
2026 if ((fp = fopen(procfs_path, "r")) != NULL) {
2027 fgets(codec_version, CODEC_VERSION_MAX_LENGTH, fp);
2028 fclose(fp);
2029 } else {
2030 ALOGE("%s: ERROR. cannot open %s", __func__, procfs_path);
2031 return -ENOENT;
2032 }
2033 ALOGD("%s: codec version %s", __func__, codec_version);
2034 }
2035
2036 return 0;
2037}
2038
Preetam Singh Ranawat9d0d8e42019-07-15 12:27:25 +05302039int audio_extn_utils_get_codec_variant(int card_num,
2040 char *codec_variant)
2041{
2042 char procfs_path[50];
2043 FILE *fp;
2044 snprintf(procfs_path, sizeof(procfs_path),
2045 "/proc/asound/card%d/codecs/wcd938x/variant", card_num);
2046 if ((fp = fopen(procfs_path, "r")) == NULL) {
2047 snprintf(procfs_path, sizeof(procfs_path),
2048 "/proc/asound/card%d/codecs/wcd937x/variant", card_num);
2049 if ((fp = fopen(procfs_path, "r")) == NULL) {
2050 ALOGE("%s: ERROR. cannot open %s", __func__, procfs_path);
2051 return -ENOENT;
2052 }
2053 }
2054 fgets(codec_variant, CODEC_VARIANT_MAX_LENGTH, fp);
2055 fclose(fp);
2056 ALOGD("%s: codec variant is %s", __func__, codec_variant);
2057 return 0;
2058}
2059
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05302060
Ashish Jain81eb2a82015-05-13 10:52:34 +05302061#ifdef AUDIO_EXTERNAL_HDMI_ENABLED
2062
2063void get_default_compressed_channel_status(
2064 unsigned char *channel_status)
2065{
Ashish Jain81eb2a82015-05-13 10:52:34 +05302066 memset(channel_status,0,24);
2067
2068 /* block start bit in preamble bit 3 */
2069 channel_status[0] |= PROFESSIONAL;
2070 //compre out
2071 channel_status[0] |= NON_LPCM;
2072 // sample rate; fixed 48K for default/transcode
2073 channel_status[3] |= SR_48000;
2074}
2075
Ashish Jain81eb2a82015-05-13 10:52:34 +05302076int32_t get_compressed_channel_status(void *audio_stream_data,
2077 uint32_t audio_frame_size,
2078 unsigned char *channel_status,
2079 enum audio_parser_code_type codec_type)
2080 // codec_type - AUDIO_PARSER_CODEC_AC3
2081 // - AUDIO_PARSER_CODEC_DTS
2082{
2083 unsigned char *stream;
2084 int ret = 0;
2085 stream = (unsigned char *)audio_stream_data;
2086
2087 if (audio_stream_data == NULL || audio_frame_size == 0) {
2088 ALOGW("no buffer to get channel status, return default for compress");
2089 get_default_compressed_channel_status(channel_status);
2090 return ret;
2091 }
2092
2093 memset(channel_status,0,24);
2094 if(init_audio_parser(stream, audio_frame_size, codec_type) == -1)
2095 {
2096 ALOGE("init audio parser failed");
2097 return -1;
2098 }
2099 ret = get_channel_status(channel_status, codec_type);
2100 return ret;
2101
2102}
2103
Ashish Jain81eb2a82015-05-13 10:52:34 +05302104void get_lpcm_channel_status(uint32_t sampleRate,
2105 unsigned char *channel_status)
2106{
2107 int32_t status = 0;
Ashish Jain81eb2a82015-05-13 10:52:34 +05302108 memset(channel_status,0,24);
2109 /* block start bit in preamble bit 3 */
2110 channel_status[0] |= PROFESSIONAL;
2111 //LPCM OUT
2112 channel_status[0] &= ~NON_LPCM;
2113
2114 switch (sampleRate) {
2115 case 8000:
2116 case 11025:
2117 case 12000:
2118 case 16000:
2119 case 22050:
2120 channel_status[3] |= SR_NOTID;
Preetam Singh Ranawat61716b12015-12-14 11:55:24 +05302121 break;
Ashish Jain81eb2a82015-05-13 10:52:34 +05302122 case 24000:
2123 channel_status[3] |= SR_24000;
2124 break;
2125 case 32000:
2126 channel_status[3] |= SR_32000;
2127 break;
2128 case 44100:
2129 channel_status[3] |= SR_44100;
2130 break;
2131 case 48000:
2132 channel_status[3] |= SR_48000;
2133 break;
2134 case 88200:
2135 channel_status[3] |= SR_88200;
2136 break;
2137 case 96000:
2138 channel_status[3] |= SR_96000;
2139 break;
2140 case 176400:
2141 channel_status[3] |= SR_176400;
2142 break;
2143 case 192000:
2144 channel_status[3] |= SR_192000;
2145 break;
2146 default:
2147 ALOGV("Invalid sample_rate %u\n", sampleRate);
2148 status = -1;
2149 break;
2150 }
2151}
2152
2153void audio_utils_set_hdmi_channel_status(struct stream_out *out, char * buffer, size_t bytes)
2154{
2155 unsigned char channel_status[24]={0};
2156 struct snd_aes_iec958 iec958;
2157 const char *mixer_ctl_name = "IEC958 Playback PCM Stream";
2158 struct mixer_ctl *ctl;
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +05302159 ALOGV("%s: buffer %s bytes %zd", __func__, buffer, bytes);
Arun Mirpurie008ed22019-03-21 11:21:04 -07002160
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05302161 if (audio_extn_utils_is_dolby_format(out->format) &&
Ashish Jain81eb2a82015-05-13 10:52:34 +05302162 /*TODO:Extend code to support DTS passthrough*/
2163 /*set compressed channel status bits*/
Arun Mirpurie008ed22019-03-21 11:21:04 -07002164 audio_extn_passthru_is_passthrough_stream(out) &&
2165 audio_extn_is_hdmi_passthru_enabled()) {
Ashish Jain81eb2a82015-05-13 10:52:34 +05302166 get_compressed_channel_status(buffer, bytes, channel_status, AUDIO_PARSER_CODEC_AC3);
Arun Mirpurie008ed22019-03-21 11:21:04 -07002167 } else {
Ashish Jain81eb2a82015-05-13 10:52:34 +05302168 /*set channel status bit for LPCM*/
2169 get_lpcm_channel_status(out->sample_rate, channel_status);
2170 }
2171
2172 memcpy(iec958.status, channel_status,sizeof(iec958.status));
2173 ctl = mixer_get_ctl_by_name(out->dev->mixer, mixer_ctl_name);
2174 if (!ctl) {
2175 ALOGE("%s: Could not get ctl for mixer cmd - %s",
2176 __func__, mixer_ctl_name);
2177 return;
2178 }
2179 if (mixer_ctl_set_array(ctl, &iec958, sizeof(iec958)) < 0) {
2180 ALOGE("%s: Could not set channel status for ext HDMI ",
2181 __func__);
2182 return;
2183 }
2184
2185}
2186#endif
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302187
2188int audio_extn_utils_get_avt_device_drift(
2189 struct audio_usecase *usecase,
2190 struct audio_avt_device_drift_param *drift_param)
2191{
2192 int ret = 0, count = 0;
2193 char avt_device_drift_mixer_ctl_name[MIXER_PATH_MAX_LENGTH] = {0};
Naresh Tanniru6160c712017-04-17 15:43:48 +05302194 const char *backend = NULL;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302195 struct mixer_ctl *ctl = NULL;
2196 struct audio_avt_device_drift_stats drift_stats;
2197 struct audio_device *adev = NULL;
2198
2199 if (usecase != NULL && usecase->type == PCM_PLAYBACK) {
Naresh Tanniru6160c712017-04-17 15:43:48 +05302200 backend = platform_get_snd_device_backend_interface(usecase->out_snd_device);
2201 if (!backend) {
2202 ALOGE("%s: Unsupported device %d", __func__,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08002203 get_device_types(&usecase->stream.out->device_list));
Naresh Tanniru6160c712017-04-17 15:43:48 +05302204 ret = -EINVAL;
2205 goto done;
2206 }
2207 strlcpy(avt_device_drift_mixer_ctl_name,
2208 backend,
2209 MIXER_PATH_MAX_LENGTH);
2210
2211 count = strlen(backend);
2212 if (MIXER_PATH_MAX_LENGTH - count > 0) {
2213 strlcat(&avt_device_drift_mixer_ctl_name[count],
2214 " DRIFT",
2215 MIXER_PATH_MAX_LENGTH - count);
2216 } else {
2217 ret = -EINVAL;
2218 goto done;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302219 }
2220 } else {
Naresh Tanniru7586e292017-04-13 10:38:13 +05302221 ALOGE("%s: Invalid usecase",__func__);
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302222 ret = -EINVAL;
Naresh Tanniru6160c712017-04-17 15:43:48 +05302223 goto done;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302224 }
2225
Naresh Tanniru6160c712017-04-17 15:43:48 +05302226 adev = usecase->stream.out->dev;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302227 ctl = mixer_get_ctl_by_name(adev->mixer, avt_device_drift_mixer_ctl_name);
2228 if (!ctl) {
2229 ALOGE("%s: Could not get ctl for mixer cmd - %s",
2230 __func__, avt_device_drift_mixer_ctl_name);
2231
2232 ret = -EINVAL;
2233 goto done;
2234 }
2235
2236 ALOGV("%s: Getting AV Timer vs Device Drift mixer ctrl name %s", __func__,
2237 avt_device_drift_mixer_ctl_name);
2238
2239 mixer_ctl_update(ctl);
2240 count = mixer_ctl_get_num_values(ctl);
2241 if (count != sizeof(struct audio_avt_device_drift_stats)) {
2242 ALOGE("%s: mixer_ctl_get_num_values() invalid drift_stats data size",
2243 __func__);
2244
2245 ret = -EINVAL;
2246 goto done;
2247 }
2248
2249 ret = mixer_ctl_get_array(ctl, (void *)&drift_stats, count);
2250 if (ret != 0) {
2251 ALOGE("%s: mixer_ctl_get_array() failed to get drift_stats Params",
2252 __func__);
2253
2254 ret = -EINVAL;
2255 goto done;
2256 }
2257 memcpy(drift_param, &drift_stats.drift_param,
2258 sizeof(struct audio_avt_device_drift_param));
2259done:
2260 return ret;
2261}
Manish Dewangan07de2142017-02-27 19:27:20 +05302262
2263#ifdef SNDRV_COMPRESS_PATH_DELAY
2264int audio_extn_utils_compress_get_dsp_latency(struct stream_out *out)
2265{
2266 int ret = -EINVAL;
2267 struct snd_compr_metadata metadata;
2268 int delay_ms = COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
2269
Weiyin Jiangc49a3b52018-08-17 16:16:08 +08002270 /* override the latency for pcm offload use case */
2271 if ((out->flags & AUDIO_OUTPUT_FLAG_DIRECT) &&
2272 !(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)) {
2273 delay_ms = PCM_OFFLOAD_PLAYBACK_LATENCY;
2274 }
2275
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -07002276 if (property_get_bool("vendor.audio.playback.dsp.pathdelay", false)) {
Manish Dewangan07de2142017-02-27 19:27:20 +05302277 ALOGD("%s:: Quering DSP delay %d",__func__, __LINE__);
2278 if (!(is_offload_usecase(out->usecase))) {
2279 ALOGE("%s:: not supported for non offload session", __func__);
2280 goto exit;
2281 }
2282
2283 if (!out->compr) {
2284 ALOGD("%s:: Invalid compress handle,returning default dsp latency",
2285 __func__);
2286 goto exit;
2287 }
2288
2289 metadata.key = SNDRV_COMPRESS_PATH_DELAY;
2290 ret = compress_get_metadata(out->compr, &metadata);
2291 if(ret) {
2292 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2293 goto exit;
2294 }
2295 delay_ms = metadata.value[0] / 1000; /*convert to ms*/
2296 } else {
2297 ALOGD("%s:: Using Fix DSP delay",__func__);
2298 }
2299
2300exit:
2301 ALOGD("%s:: delay in ms is %d",__func__, delay_ms);
2302 return delay_ms;
2303}
2304#else
2305int audio_extn_utils_compress_get_dsp_latency(struct stream_out *out __unused)
2306{
2307 return COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
2308}
2309#endif
Manish Dewangan69426c82017-01-30 17:35:36 +05302310
2311#ifdef SNDRV_COMPRESS_RENDER_MODE
2312int audio_extn_utils_compress_set_render_mode(struct stream_out *out)
2313{
2314 struct snd_compr_metadata metadata;
2315 int ret = -EINVAL;
2316
2317 if (!(is_offload_usecase(out->usecase))) {
2318 ALOGE("%s:: not supported for non offload session", __func__);
2319 goto exit;
2320 }
2321
2322 if (!out->compr) {
2323 ALOGD("%s:: Invalid compress handle",
2324 __func__);
2325 goto exit;
2326 }
2327
2328 ALOGD("%s:: render mode %d", __func__, out->render_mode);
2329
2330 metadata.key = SNDRV_COMPRESS_RENDER_MODE;
2331 if (out->render_mode == RENDER_MODE_AUDIO_MASTER) {
2332 metadata.value[0] = SNDRV_COMPRESS_RENDER_MODE_AUDIO_MASTER;
2333 } else if (out->render_mode == RENDER_MODE_AUDIO_STC_MASTER) {
2334 metadata.value[0] = SNDRV_COMPRESS_RENDER_MODE_STC_MASTER;
2335 } else {
2336 ret = 0;
2337 goto exit;
2338 }
2339 ret = compress_set_metadata(out->compr, &metadata);
2340 if(ret) {
2341 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2342 }
2343exit:
2344 return ret;
2345}
2346#else
2347int audio_extn_utils_compress_set_render_mode(struct stream_out *out __unused)
2348{
2349 ALOGD("%s:: configuring render mode not supported", __func__);
2350 return 0;
2351}
2352#endif
Manish Dewangan58229382017-02-02 15:48:41 +05302353
2354#ifdef SNDRV_COMPRESS_CLK_REC_MODE
2355int audio_extn_utils_compress_set_clk_rec_mode(
2356 struct audio_usecase *usecase)
2357{
2358 struct snd_compr_metadata metadata;
2359 struct stream_out *out = NULL;
2360 int ret = -EINVAL;
2361
Naresh Tanniru7586e292017-04-13 10:38:13 +05302362 if (usecase == NULL || usecase->type != PCM_PLAYBACK) {
Manish Dewangan58229382017-02-02 15:48:41 +05302363 ALOGE("%s:: Invalid use case", __func__);
2364 goto exit;
2365 }
2366
2367 out = usecase->stream.out;
2368 if (!out) {
2369 ALOGE("%s:: invalid stream", __func__);
2370 goto exit;
2371 }
2372
2373 if (!is_offload_usecase(out->usecase)) {
2374 ALOGE("%s:: not supported for non offload session", __func__);
2375 goto exit;
2376 }
2377
2378 if (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER) {
2379 ALOGD("%s:: clk recovery is only supported in STC render mode",
2380 __func__);
2381 ret = 0;
2382 goto exit;
2383 }
2384
2385 if (!out->compr) {
2386 ALOGD("%s:: Invalid compress handle",
2387 __func__);
2388 goto exit;
2389 }
2390 metadata.key = SNDRV_COMPRESS_CLK_REC_MODE;
2391 switch(usecase->out_snd_device) {
2392 case SND_DEVICE_OUT_HDMI:
2393 case SND_DEVICE_OUT_SPEAKER_AND_HDMI:
2394 case SND_DEVICE_OUT_DISPLAY_PORT:
2395 case SND_DEVICE_OUT_SPEAKER_AND_DISPLAY_PORT:
2396 metadata.value[0] = SNDRV_COMPRESS_CLK_REC_MODE_NONE;
2397 break;
2398 default:
2399 metadata.value[0] = SNDRV_COMPRESS_CLK_REC_MODE_AUTO;
2400 break;
2401 }
2402
2403 ALOGD("%s:: clk recovery mode %d",__func__, metadata.value[0]);
2404
2405 ret = compress_set_metadata(out->compr, &metadata);
2406 if(ret) {
2407 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2408 }
2409
2410exit:
2411 return ret;
2412}
2413#else
2414int audio_extn_utils_compress_set_clk_rec_mode(
2415 struct audio_usecase *usecase __unused)
2416{
2417 ALOGD("%s:: configuring render mode not supported", __func__);
2418 return 0;
2419}
2420#endif
Manish Dewangan27346042017-03-01 12:56:12 +05302421
2422#ifdef SNDRV_COMPRESS_RENDER_WINDOW
2423int audio_extn_utils_compress_set_render_window(
2424 struct stream_out *out,
2425 struct audio_out_render_window_param *render_window)
2426{
2427 struct snd_compr_metadata metadata;
2428 int ret = -EINVAL;
2429
Manish Dewangan27346042017-03-01 12:56:12 +05302430 if(render_window == NULL) {
2431 ALOGE("%s:: Invalid render_window", __func__);
2432 goto exit;
2433 }
2434
Aniket Kumar Lata1e1d3662017-06-01 18:45:48 -07002435 ALOGD("%s:: render window start 0x%"PRIx64" end 0x%"PRIx64"",
2436 __func__,render_window->render_ws, render_window->render_we);
2437
Manish Dewangan27346042017-03-01 12:56:12 +05302438 if (!is_offload_usecase(out->usecase)) {
2439 ALOGE("%s:: not supported for non offload session", __func__);
2440 goto exit;
2441 }
2442
Naresh Tanniru6160c712017-04-17 15:43:48 +05302443 if ((out->render_mode != RENDER_MODE_AUDIO_MASTER) &&
2444 (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER)) {
Manish Dewangan27346042017-03-01 12:56:12 +05302445 ALOGD("%s:: only supported in timestamp mode, current "
2446 "render mode mode %d", __func__, out->render_mode);
2447 goto exit;
2448 }
2449
2450 if (!out->compr) {
2451 ALOGW("%s:: offload session not yet opened,"
2452 "render window will be configure later", __func__);
2453 /* store render window to reconfigure in start_output_stream() */
2454 goto exit;
2455 }
2456
2457 metadata.key = SNDRV_COMPRESS_RENDER_WINDOW;
2458 /*render window start value */
2459 metadata.value[0] = 0xFFFFFFFF & render_window->render_ws; /* lsb */
2460 metadata.value[1] = \
2461 (0xFFFFFFFF00000000 & render_window->render_ws) >> 32; /* msb*/
2462 /*render window end value */
2463 metadata.value[2] = 0xFFFFFFFF & render_window->render_we; /* lsb */
2464 metadata.value[3] = \
2465 (0xFFFFFFFF00000000 & render_window->render_we) >> 32; /* msb*/
2466
2467 ret = compress_set_metadata(out->compr, &metadata);
2468 if(ret) {
2469 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2470 }
2471
2472exit:
2473 return ret;
2474}
2475#else
2476int audio_extn_utils_compress_set_render_window(
2477 struct stream_out *out __unused,
2478 struct audio_out_render_window_param *render_window __unused)
2479{
2480 ALOGD("%s:: configuring render window not supported", __func__);
2481 return 0;
2482}
2483#endif
Manish Dewangan14956cc2017-02-14 18:54:42 +05302484
2485#ifdef SNDRV_COMPRESS_START_DELAY
2486int audio_extn_utils_compress_set_start_delay(
2487 struct stream_out *out,
2488 struct audio_out_start_delay_param *delay_param)
2489{
2490 struct snd_compr_metadata metadata;
2491 int ret = -EINVAL;
2492
2493 if(delay_param == NULL) {
2494 ALOGE("%s:: Invalid delay_param", __func__);
2495 goto exit;
2496 }
2497
2498 ALOGD("%s:: render start delay 0x%"PRIx64" ", __func__,
2499 delay_param->start_delay);
2500
2501 if (!is_offload_usecase(out->usecase)) {
2502 ALOGE("%s:: not supported for non offload session", __func__);
2503 goto exit;
2504 }
2505
Naresh Tanniru6160c712017-04-17 15:43:48 +05302506 if ((out->render_mode != RENDER_MODE_AUDIO_MASTER) &&
2507 (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER)) {
Manish Dewangan14956cc2017-02-14 18:54:42 +05302508 ALOGD("%s:: only supported in timestamp mode, current "
2509 "render mode mode %d", __func__, out->render_mode);
2510 goto exit;
2511 }
2512
2513 if (!out->compr) {
2514 ALOGW("%s:: offload session not yet opened,"
2515 "start delay will be configure later", __func__);
2516 goto exit;
2517 }
2518
2519 metadata.key = SNDRV_COMPRESS_START_DELAY;
2520 metadata.value[0] = 0xFFFFFFFF & delay_param->start_delay; /* lsb */
2521 metadata.value[1] = \
2522 (0xFFFFFFFF00000000 & delay_param->start_delay) >> 32; /* msb*/
2523
2524 ret = compress_set_metadata(out->compr, &metadata);
2525 if(ret) {
2526 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2527 }
2528
2529exit:
2530 return ret;
2531}
2532#else
2533int audio_extn_utils_compress_set_start_delay(
2534 struct stream_out *out __unused,
2535 struct audio_out_start_delay_param *delay_param __unused)
2536{
2537 ALOGD("%s:: configuring render window not supported", __func__);
2538 return 0;
2539}
2540#endif
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002541
Surendar Karka287348c2019-04-10 18:31:46 +05302542#ifdef SNDRV_COMPRESS_DSP_POSITION
2543int audio_extn_utils_compress_get_dsp_presentation_pos(struct stream_out *out,
2544 uint64_t *frames, struct timespec *timestamp, int32_t clock_id)
2545{
2546 int ret = -EINVAL;
2547 uint64_t *val = NULL;
2548 uint64_t time = 0;
2549 struct snd_compr_metadata metadata;
2550
2551 ALOGV("%s:: Quering DSP position with clock id %d",__func__, clock_id);
2552 metadata.key = SNDRV_COMPRESS_DSP_POSITION;
2553 metadata.value[0] = clock_id;
2554 ret = compress_get_metadata(out->compr, &metadata);
2555 if (ret) {
2556 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2557 ret = -errno;
2558 goto exit;
2559 }
2560 val = (uint64_t *)&metadata.value[1];
2561 *frames = *val;
2562 time = *(val + 1);
2563 timestamp->tv_sec = time / 1000000;
2564 timestamp->tv_nsec = (time % 1000000)*1000;
2565
2566exit:
2567 return ret;
2568}
2569#else
2570int audio_extn_utils_compress_get_dsp_presentation_pos(struct stream_out *out __unused,
2571 uint64_t *frames __unused, struct timespec *timestamp __unused,
2572 int32_t clock_id __unused)
2573{
2574 ALOGD("%s:: dsp presentation position not supported", __func__);
2575 return 0;
2576
2577}
2578#endif
2579
2580#ifdef SNDRV_PCM_IOCTL_DSP_POSITION
2581int audio_extn_utils_pcm_get_dsp_presentation_pos(struct stream_out *out,
2582 uint64_t *frames, struct timespec *timestamp, int32_t clock_id)
2583{
2584 int ret = -EINVAL;
2585 uint64_t time = 0;
2586 struct snd_pcm_prsnt_position prsnt_position;
2587
2588 ALOGV("%s:: Quering DSP position with clock id %d",__func__, clock_id);
2589 prsnt_position.clock_id = clock_id;
2590 ret = pcm_ioctl(out->pcm, SNDRV_PCM_IOCTL_DSP_POSITION, &prsnt_position);
2591 if (ret) {
2592 ALOGE("%s::error %d", __func__, ret);
2593 ret = -EIO;
2594 goto exit;
2595 }
2596
2597 *frames = prsnt_position.frames;
2598 time = prsnt_position.timestamp;
2599 timestamp->tv_sec = time / 1000000;
2600 timestamp->tv_nsec = (time % 1000000)*1000;
2601
2602exit:
2603 return ret;
2604}
2605#else
2606int audio_extn_utils_pcm_get_dsp_presentation_pos(struct stream_out *out __unused,
2607 uint64_t *frames __unused, struct timespec *timestamp __unused,
2608 int32_t clock_id __unused)
2609{
2610 ALOGD("%s:: dsp presentation position not supported", __func__);
2611 return 0;
2612
2613}
2614#endif
2615
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002616#define MAX_SND_CARD 8
Ashish Jain30ae8942017-11-05 17:21:23 +05302617#define RETRY_US 1000000
2618#define RETRY_NUMBER 40
Aalique Grahame22e49102018-12-18 14:23:57 -08002619#define PLATFORM_INFO_XML_PATH "audio_platform_info.xml"
2620#define PLATFORM_INFO_XML_BASE_STRING "audio_platform_info"
2621
2622#ifdef LINUX_ENABLED
2623static const char *kConfigLocationList[] =
2624 {"/etc"};
2625#else
2626static const char *kConfigLocationList[] =
2627 {"/vendor/etc"};
2628#endif
2629static const int kConfigLocationListSize =
2630 (sizeof(kConfigLocationList) / sizeof(kConfigLocationList[0]));
2631
2632bool audio_extn_utils_resolve_config_file(char file_name[MIXER_PATH_MAX_LENGTH])
2633{
2634 char full_config_path[MIXER_PATH_MAX_LENGTH];
2635 for (int i = 0; i < kConfigLocationListSize; i++) {
2636 snprintf(full_config_path,
2637 MIXER_PATH_MAX_LENGTH,
2638 "%s/%s",
2639 kConfigLocationList[i],
2640 file_name);
2641 if (F_OK == access(full_config_path, 0)) {
Weiyin Jiang2995f662019-04-17 14:25:12 +08002642 strlcpy(file_name, full_config_path, MIXER_PATH_MAX_LENGTH);
Aalique Grahame22e49102018-12-18 14:23:57 -08002643 return true;
2644 }
2645 }
2646 return false;
2647}
2648
2649/* platform_info_file should be size 'MIXER_PATH_MAX_LENGTH' */
2650int audio_extn_utils_get_platform_info(const char* snd_card_name, char* platform_info_file)
2651{
2652 if (NULL == snd_card_name) {
2653 return -1;
2654 }
2655
2656 struct snd_card_split *snd_split_handle = NULL;
2657 int ret = 0;
2658 audio_extn_set_snd_card_split(snd_card_name);
2659 snd_split_handle = audio_extn_get_snd_card_split();
2660
2661 snprintf(platform_info_file, MIXER_PATH_MAX_LENGTH, "%s_%s_%s.xml",
2662 PLATFORM_INFO_XML_BASE_STRING, snd_split_handle->snd_card,
2663 snd_split_handle->form_factor);
2664
2665 if (!audio_extn_utils_resolve_config_file(platform_info_file)) {
2666 memset(platform_info_file, 0, MIXER_PATH_MAX_LENGTH);
2667 snprintf(platform_info_file, MIXER_PATH_MAX_LENGTH, "%s_%s.xml",
2668 PLATFORM_INFO_XML_BASE_STRING, snd_split_handle->snd_card);
2669
2670 if (!audio_extn_utils_resolve_config_file(platform_info_file)) {
2671 memset(platform_info_file, 0, MIXER_PATH_MAX_LENGTH);
2672 strlcpy(platform_info_file, PLATFORM_INFO_XML_PATH, MIXER_PATH_MAX_LENGTH);
2673 ret = audio_extn_utils_resolve_config_file(platform_info_file) ? 0 : -1;
2674 }
2675 }
2676
2677 return ret;
2678}
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002679
2680int audio_extn_utils_get_snd_card_num()
2681{
Soumya Managoli9fee7c62018-04-06 16:21:50 +05302682 int snd_card_num = 0;
2683 struct mixer *mixer = NULL;
2684
2685 snd_card_num = audio_extn_utils_open_snd_mixer(&mixer);
2686 if (mixer)
2687 mixer_close(mixer);
2688 return snd_card_num;
2689}
2690
2691int audio_extn_utils_open_snd_mixer(struct mixer **mixer_handle)
2692{
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002693
2694 void *hw_info = NULL;
2695 struct mixer *mixer = NULL;
2696 int retry_num = 0;
mpangfaa7bae2019-01-15 16:38:13 +08002697 int snd_card_num = 0;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002698 char* snd_card_name = NULL;
mpangfaa7bae2019-01-15 16:38:13 +08002699 int snd_card_detection_info[MAX_SND_CARD] = {0};
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002700
Soumya Managoli9fee7c62018-04-06 16:21:50 +05302701 if (!mixer_handle) {
2702 ALOGE("invalid mixer handle");
2703 return -1;
2704 }
2705 *mixer_handle = NULL;
Ashish Jain30ae8942017-11-05 17:21:23 +05302706 /*
mpangfaa7bae2019-01-15 16:38:13 +08002707 * Try with all the sound cards ( 0 to 7 ) and if none of them were detected
Ashish Jain30ae8942017-11-05 17:21:23 +05302708 * sleep for 1 sec and try detections with sound card 0 again.
2709 * If sound card gets detected, check if it is relevant, if not check with the
2710 * other sound cards. To ensure that the irrelevant sound card is not check again,
2711 * we maintain it in min_snd_card_num.
2712 */
2713 while (retry_num < RETRY_NUMBER) {
mpangfaa7bae2019-01-15 16:38:13 +08002714 snd_card_num = 0;
2715 while (snd_card_num < MAX_SND_CARD) {
2716 if (snd_card_detection_info[snd_card_num] == 0) {
2717 mixer = mixer_open(snd_card_num);
2718 if (!mixer)
2719 snd_card_num++;
2720 else
2721 break;
2722 } else
2723 snd_card_num++;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002724 }
2725
2726 if (!mixer) {
Ashish Jain30ae8942017-11-05 17:21:23 +05302727 usleep(RETRY_US);
Ashish Jain30ae8942017-11-05 17:21:23 +05302728 retry_num++;
mpangfaa7bae2019-01-15 16:38:13 +08002729 ALOGD("%s: retry, retry_num %d", __func__, retry_num);
Ashish Jain30ae8942017-11-05 17:21:23 +05302730 continue;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002731 }
2732
2733 snd_card_name = strdup(mixer_get_name(mixer));
2734 if (!snd_card_name) {
2735 ALOGE("failed to allocate memory for snd_card_name\n");
2736 mixer_close(mixer);
2737 return -1;
2738 }
2739 ALOGD("%s: snd_card_name: %s", __func__, snd_card_name);
mpangfaa7bae2019-01-15 16:38:13 +08002740 snd_card_detection_info[snd_card_num] = 1;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002741 hw_info = hw_info_init(snd_card_name);
2742 if (hw_info) {
2743 ALOGD("%s: Opened sound card:%d", __func__, snd_card_num);
2744 break;
2745 }
mpangfaa7bae2019-01-15 16:38:13 +08002746 ALOGE("%s: Failed to init hardware info, snd_card_num:%d", __func__, snd_card_num);
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002747
Dhananjay Kumara7e27832017-07-11 15:40:39 +05302748 free(snd_card_name);
2749 snd_card_name = NULL;
2750
2751 mixer_close(mixer);
2752 mixer = NULL;
2753 }
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002754 if (snd_card_name)
2755 free(snd_card_name);
Ashish Jain30ae8942017-11-05 17:21:23 +05302756
Dhananjay Kumara7e27832017-07-11 15:40:39 +05302757 if (hw_info)
2758 hw_info_deinit(hw_info);
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002759
mpangfaa7bae2019-01-15 16:38:13 +08002760 if (retry_num >= RETRY_NUMBER) {
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002761 ALOGE("%s: Unable to find correct sound card, aborting.", __func__);
2762 return -1;
2763 }
2764
Soumya Managoli9fee7c62018-04-06 16:21:50 +05302765 if (mixer)
2766 *mixer_handle = mixer;
2767
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002768 return snd_card_num;
2769}
Naresh Tanniru6160c712017-04-17 15:43:48 +05302770
Soumya Managoli9fee7c62018-04-06 16:21:50 +05302771void audio_extn_utils_close_snd_mixer(struct mixer *mixer)
2772{
2773 if (mixer)
2774 mixer_close(mixer);
2775}
2776
Naresh Tanniru6160c712017-04-17 15:43:48 +05302777#ifdef SNDRV_COMPRESS_ENABLE_ADJUST_SESSION_CLOCK
2778int audio_extn_utils_compress_enable_drift_correction(
2779 struct stream_out *out,
2780 struct audio_out_enable_drift_correction *drift)
2781{
2782 struct snd_compr_metadata metadata;
2783 int ret = -EINVAL;
2784
2785 if(drift == NULL) {
2786 ALOGE("%s:: Invalid param", __func__);
2787 goto exit;
2788 }
2789
2790 ALOGD("%s:: drift enable %d", __func__,drift->enable);
2791
2792 if (!is_offload_usecase(out->usecase)) {
2793 ALOGE("%s:: not supported for non offload session", __func__);
2794 goto exit;
2795 }
2796
2797 if (!out->compr) {
2798 ALOGW("%s:: offload session not yet opened,"
2799 "start delay will be configure later", __func__);
2800 goto exit;
2801 }
2802
2803 metadata.key = SNDRV_COMPRESS_ENABLE_ADJUST_SESSION_CLOCK;
2804 metadata.value[0] = drift->enable;
2805 out->drift_correction_enabled = drift->enable;
2806
2807 ret = compress_set_metadata(out->compr, &metadata);
2808 if(ret) {
2809 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2810 out->drift_correction_enabled = false;
2811 }
2812
2813exit:
2814 return ret;
2815}
2816#else
2817int audio_extn_utils_compress_enable_drift_correction(
2818 struct stream_out *out __unused,
2819 struct audio_out_enable_drift_correction *drift __unused)
2820{
2821 ALOGD("%s:: configuring drift enablement not supported", __func__);
2822 return 0;
2823}
2824#endif
2825
2826#ifdef SNDRV_COMPRESS_ADJUST_SESSION_CLOCK
2827int audio_extn_utils_compress_correct_drift(
2828 struct stream_out *out,
2829 struct audio_out_correct_drift *drift_param)
2830{
2831 struct snd_compr_metadata metadata;
2832 int ret = -EINVAL;
2833
2834 if (drift_param == NULL) {
2835 ALOGE("%s:: Invalid drift_param", __func__);
2836 goto exit;
2837 }
2838
2839 ALOGD("%s:: adjust time 0x%"PRIx64" ", __func__,
2840 drift_param->adjust_time);
2841
2842 if (!is_offload_usecase(out->usecase)) {
2843 ALOGE("%s:: not supported for non offload session", __func__);
2844 goto exit;
2845 }
2846
2847 if (!out->compr) {
2848 ALOGW("%s:: offload session not yet opened", __func__);
2849 goto exit;
2850 }
2851
2852 if (!out->drift_correction_enabled) {
2853 ALOGE("%s:: drift correction not enabled", __func__);
2854 goto exit;
2855 }
2856
2857 metadata.key = SNDRV_COMPRESS_ADJUST_SESSION_CLOCK;
2858 metadata.value[0] = 0xFFFFFFFF & drift_param->adjust_time; /* lsb */
2859 metadata.value[1] = \
2860 (0xFFFFFFFF00000000 & drift_param->adjust_time) >> 32; /* msb*/
2861
2862 ret = compress_set_metadata(out->compr, &metadata);
2863 if(ret)
2864 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2865exit:
2866 return ret;
2867}
2868#else
2869int audio_extn_utils_compress_correct_drift(
2870 struct stream_out *out __unused,
2871 struct audio_out_correct_drift *drift_param __unused)
2872{
2873 ALOGD("%s:: setting adjust clock not supported", __func__);
2874 return 0;
2875}
2876#endif
Naresh Tanniru29bce4e2017-04-27 17:54:30 +05302877
2878int audio_extn_utils_set_channel_map(
2879 struct stream_out *out,
2880 struct audio_out_channel_map_param *channel_map_param)
2881{
2882 int ret = -EINVAL, i = 0;
2883 int channels = audio_channel_count_from_out_mask(out->channel_mask);
2884
2885 if (channel_map_param == NULL) {
2886 ALOGE("%s:: Invalid channel_map", __func__);
2887 goto exit;
2888 }
2889
2890 if (channel_map_param->channels != channels) {
2891 ALOGE("%s:: Channels(%d) does not match stream channels(%d)",
2892 __func__, channel_map_param->channels, channels);
2893 goto exit;
2894 }
2895
2896 for ( i = 0; i < channels; i++) {
2897 ALOGV("%s:: channel_map[%d]- %d", __func__, i, channel_map_param->channel_map[i]);
2898 out->channel_map_param.channel_map[i] = channel_map_param->channel_map[i];
2899 }
2900 ret = 0;
2901exit:
2902 return ret;
2903}
Varun Balaraje49253e2017-07-06 19:48:56 +05302904
2905int audio_extn_utils_set_pan_scale_params(
2906 struct stream_out *out,
2907 struct mix_matrix_params *mm_params)
2908{
2909 int ret = -EINVAL, i = 0, j = 0;
2910
Varun Balaraj003ee752017-08-07 17:54:55 +05302911 if (mm_params == NULL || out == NULL) {
2912 ALOGE("%s:: Invalid mix matrix or out param", __func__);
Varun Balaraje49253e2017-07-06 19:48:56 +05302913 goto exit;
2914 }
2915
2916 if (mm_params->num_output_channels > MAX_CHANNELS_SUPPORTED ||
2917 mm_params->num_output_channels <= 0 ||
2918 mm_params->num_input_channels > MAX_CHANNELS_SUPPORTED ||
2919 mm_params->num_input_channels <= 0)
2920 goto exit;
2921
2922 out->pan_scale_params.num_output_channels = mm_params->num_output_channels;
2923 out->pan_scale_params.num_input_channels = mm_params->num_input_channels;
2924 out->pan_scale_params.has_output_channel_map =
2925 mm_params->has_output_channel_map;
2926 for (i = 0; i < mm_params->num_output_channels; i++)
2927 out->pan_scale_params.output_channel_map[i] =
2928 mm_params->output_channel_map[i];
2929
2930 out->pan_scale_params.has_input_channel_map =
2931 mm_params->has_input_channel_map;
2932 for (i = 0; i < mm_params->num_input_channels; i++)
2933 out->pan_scale_params.input_channel_map[i] =
2934 mm_params->input_channel_map[i];
2935
2936 out->pan_scale_params.has_mixer_coeffs = mm_params->has_mixer_coeffs;
2937 for (i = 0; i < mm_params->num_output_channels; i++)
2938 for (j = 0; j < mm_params->num_input_channels; j++) {
2939 //Convert the channel coefficient gains in Q14 format
2940 out->pan_scale_params.mixer_coeffs[i][j] =
2941 mm_params->mixer_coeffs[i][j] * (2 << 13);
2942 }
2943
2944 ret = platform_set_stream_pan_scale_params(out->dev->platform,
2945 out->pcm_device_id,
2946 out->pan_scale_params);
2947
2948exit:
2949 return ret;
2950}
2951
2952int audio_extn_utils_set_downmix_params(
2953 struct stream_out *out,
2954 struct mix_matrix_params *mm_params)
2955{
2956 int ret = -EINVAL, i = 0, j = 0;
2957 struct audio_usecase *usecase = NULL;
2958
Aniket Kumar Lataf9f246e2017-09-15 15:20:16 -07002959 if (mm_params == NULL || out == NULL) {
Varun Balaraj003ee752017-08-07 17:54:55 +05302960 ALOGE("%s:: Invalid mix matrix or out param", __func__);
Varun Balaraje49253e2017-07-06 19:48:56 +05302961 goto exit;
2962 }
2963
2964 if (mm_params->num_output_channels > MAX_CHANNELS_SUPPORTED ||
2965 mm_params->num_output_channels <= 0 ||
2966 mm_params->num_input_channels > MAX_CHANNELS_SUPPORTED ||
2967 mm_params->num_input_channels <= 0)
2968 goto exit;
2969
2970 usecase = get_usecase_from_list(out->dev, out->usecase);
Varun Balaraj003ee752017-08-07 17:54:55 +05302971 if (!usecase) {
2972 ALOGE("%s: Get usecase list failed!", __func__);
Aniket Kumar Lataf9f246e2017-09-15 15:20:16 -07002973 goto exit;
2974 }
Varun Balaraje49253e2017-07-06 19:48:56 +05302975 out->downmix_params.num_output_channels = mm_params->num_output_channels;
2976 out->downmix_params.num_input_channels = mm_params->num_input_channels;
2977
2978 out->downmix_params.has_output_channel_map =
2979 mm_params->has_output_channel_map;
2980 for (i = 0; i < mm_params->num_output_channels; i++) {
2981 out->downmix_params.output_channel_map[i] =
2982 mm_params->output_channel_map[i];
2983 }
2984
2985 out->downmix_params.has_input_channel_map =
2986 mm_params->has_input_channel_map;
2987 for (i = 0; i < mm_params->num_input_channels; i++)
2988 out->downmix_params.input_channel_map[i] =
2989 mm_params->input_channel_map[i];
2990
2991 out->downmix_params.has_mixer_coeffs = mm_params->has_mixer_coeffs;
2992 for (i = 0; i < mm_params->num_output_channels; i++)
Nikhil Latukar2e6f6242017-08-15 13:37:07 +05302993 for (j = 0; j < mm_params->num_input_channels; j++) {
2994 //Convert the channel coefficient gains in Q14 format
Varun Balaraje49253e2017-07-06 19:48:56 +05302995 out->downmix_params.mixer_coeffs[i][j] =
Nikhil Latukar2e6f6242017-08-15 13:37:07 +05302996 mm_params->mixer_coeffs[i][j] * (2 << 13);
2997 }
Varun Balaraje49253e2017-07-06 19:48:56 +05302998
2999 ret = platform_set_stream_downmix_params(out->dev->platform,
3000 out->pcm_device_id,
3001 usecase->out_snd_device,
3002 out->downmix_params);
3003
3004exit:
3005 return ret;
3006}
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05303007
3008bool audio_extn_utils_is_dolby_format(audio_format_t format)
3009{
3010 if (format == AUDIO_FORMAT_AC3 ||
3011 format == AUDIO_FORMAT_E_AC3 ||
3012 format == AUDIO_FORMAT_E_AC3_JOC)
3013 return true;
3014 else
3015 return false;
3016}
3017
`Deeraj Soman676c2702017-09-18 19:25:53 +05303018int audio_extn_utils_get_bit_width_from_string(const char *id_string)
3019{
3020 int i;
3021 const mixer_config_lookup mixer_bitwidth_config[] = {{"S24_3LE", 24},
3022 {"S32_LE", 32},
3023 {"S24_LE", 24},
3024 {"S16_LE", 16}};
3025 int num_configs = sizeof(mixer_bitwidth_config) / sizeof(mixer_bitwidth_config[0]);
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05303026
`Deeraj Soman676c2702017-09-18 19:25:53 +05303027 for (i = 0; i < num_configs; i++) {
3028 if (!strcmp(id_string, mixer_bitwidth_config[i].id_string))
3029 return mixer_bitwidth_config[i].value;
3030 }
3031
3032 return -EINVAL;
3033}
3034
3035int audio_extn_utils_get_sample_rate_from_string(const char *id_string)
3036{
3037 int i;
3038 const mixer_config_lookup mixer_samplerate_config[] = {{"KHZ_32", 32000},
3039 {"KHZ_48", 48000},
3040 {"KHZ_96", 96000},
3041 {"KHZ_144", 144000},
3042 {"KHZ_192", 192000},
3043 {"KHZ_384", 384000},
3044 {"KHZ_44P1", 44100},
3045 {"KHZ_88P2", 88200},
3046 {"KHZ_176P4", 176400},
3047 {"KHZ_352P8", 352800}};
3048 int num_configs = sizeof(mixer_samplerate_config) / sizeof(mixer_samplerate_config[0]);
3049
3050 for (i = 0; i < num_configs; i++) {
3051 if (!strcmp(id_string, mixer_samplerate_config[i].id_string))
3052 return mixer_samplerate_config[i].value;
3053 }
3054
3055 return -EINVAL;
3056}
3057
3058int audio_extn_utils_get_channels_from_string(const char *id_string)
3059{
3060 int i;
3061 const mixer_config_lookup mixer_channels_config[] = {{"One", 1},
3062 {"Two", 2},
3063 {"Three",3},
3064 {"Four", 4},
3065 {"Five", 5},
3066 {"Six", 6},
3067 {"Seven", 7},
3068 {"Eight", 8}};
3069 int num_configs = sizeof(mixer_channels_config) / sizeof(mixer_channels_config[0]);
3070
3071 for (i = 0; i < num_configs; i++) {
3072 if (!strcmp(id_string, mixer_channels_config[i].id_string))
3073 return mixer_channels_config[i].value;
3074 }
3075
3076 return -EINVAL;
3077}
Surendar karka30569792018-05-08 12:02:21 +05303078
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08003079void audio_extn_utils_release_snd_device(snd_device_t snd_device)
3080{
3081 audio_extn_dev_arbi_release(snd_device);
3082 audio_extn_sound_trigger_update_device_status(snd_device,
3083 ST_EVENT_SND_DEVICE_FREE);
3084 audio_extn_listen_update_device_status(snd_device,
3085 LISTEN_EVENT_SND_DEVICE_FREE);
3086}
3087
Weiyin Jiangd5718bd2019-09-04 16:52:08 +08003088int audio_extn_utils_get_license_params(
3089 const struct audio_device *adev,
3090 struct audio_license_params *license_params)
Surendar karka30569792018-05-08 12:02:21 +05303091{
3092 if(!license_params)
3093 return -EINVAL;
Weiyin Jiangd5718bd2019-09-04 16:52:08 +08003094
3095 return platform_get_license_by_product(adev->platform,
3096 (const char*)license_params->product, &license_params->key, license_params->license);
Surendar karka30569792018-05-08 12:02:21 +05303097}
3098
Aalique Grahame22e49102018-12-18 14:23:57 -08003099int audio_extn_utils_send_app_type_gain(struct audio_device *adev,
3100 int app_type,
3101 int *gain)
3102{
3103 int gain_cfg[4];
3104 const char *mixer_ctl_name = "App Type Gain";
3105 struct mixer_ctl *ctl;
3106 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
3107 if (!ctl) {
3108 ALOGE("%s: Could not get volume ctl mixer %s", __func__,
3109 mixer_ctl_name);
3110 return -EINVAL;
3111 }
3112 gain_cfg[0] = 0;
3113 gain_cfg[1] = app_type;
3114 gain_cfg[2] = gain[0];
3115 gain_cfg[3] = gain[1];
3116 ALOGV("%s app_type %d l(%d) r(%d)", __func__, app_type, gain[0], gain[1]);
3117 return mixer_ctl_set_array(ctl, gain_cfg,
3118 sizeof(gain_cfg)/sizeof(gain_cfg[0]));
3119}
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -07003120
Weiyin Jiangd5718bd2019-09-04 16:52:08 +08003121static void vndk_fwk_init()
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -07003122{
Weiyin Jiangd5718bd2019-09-04 16:52:08 +08003123 if (mVndkFwk.lib_handle != NULL)
3124 return;
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -07003125
Weiyin Jiangd5718bd2019-09-04 16:52:08 +08003126 mVndkFwk.lib_handle = dlopen(VNDK_FWK_LIB_PATH, RTLD_NOW);
3127 if (mVndkFwk.lib_handle == NULL) {
3128 ALOGW("%s: failed to dlopen VNDK_FWK_LIB %s", __func__, strerror(errno));
3129 return;
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -07003130 }
3131
Weiyin Jiangd5718bd2019-09-04 16:52:08 +08003132 *(void **)(&mVndkFwk.isVendorEnhancedFwk) =
3133 dlsym(mVndkFwk.lib_handle, "isRunningWithVendorEnhancedFramework");
3134 if (mVndkFwk.isVendorEnhancedFwk == NULL) {
3135 ALOGW("%s: dlsym failed %s", __func__, strerror(errno));
3136 if (mVndkFwk.lib_handle) {
3137 dlclose(mVndkFwk.lib_handle);
3138 mVndkFwk.lib_handle = NULL;
3139 }
3140 return;
3141 }
3142
3143
3144 *(void **)(&mVndkFwk.getVendorEnhancedInfo) =
3145 dlsym(mVndkFwk.lib_handle, "getVendorEnhancedInfo");
3146 if (mVndkFwk.getVendorEnhancedInfo == NULL) {
3147 ALOGW("%s: dlsym failed %s", __func__, strerror(errno));
3148 if (mVndkFwk.lib_handle) {
3149 dlclose(mVndkFwk.lib_handle);
3150 mVndkFwk.lib_handle = NULL;
3151 }
3152 }
3153
3154 return;
3155}
3156
3157bool audio_extn_utils_is_vendor_enhanced_fwk()
3158{
3159 static int is_vendor_enhanced_fwk = -EINVAL;
3160 if (is_vendor_enhanced_fwk != -EINVAL)
3161 return (bool)is_vendor_enhanced_fwk;
3162
3163 vndk_fwk_init();
3164
3165 if (mVndkFwk.isVendorEnhancedFwk != NULL) {
3166 is_vendor_enhanced_fwk = mVndkFwk.isVendorEnhancedFwk();
3167 ALOGW("%s: is_vendor_enhanced_fwk %d", __func__, is_vendor_enhanced_fwk);
3168 } else {
3169 is_vendor_enhanced_fwk = 0;
3170 ALOGW("%s: default to non enhanced_fwk config", __func__);
3171 }
3172
3173 return (bool)is_vendor_enhanced_fwk;
3174}
3175
3176int audio_extn_utils_get_vendor_enhanced_info()
3177{
3178 static int vendor_enhanced_info = -EINVAL;
3179 if (vendor_enhanced_info != -EINVAL)
3180 return vendor_enhanced_info;
3181
3182 vndk_fwk_init();
3183
3184 if (mVndkFwk.getVendorEnhancedInfo != NULL) {
3185 vendor_enhanced_info = mVndkFwk.getVendorEnhancedInfo();
3186 ALOGW("%s: vendor_enhanced_info 0x%x", __func__, vendor_enhanced_info);
3187 } else {
3188 vendor_enhanced_info = 0x0;
3189 ALOGW("%s: default to vendor_enhanced_info 0x0", __func__);
3190 }
3191
3192 return vendor_enhanced_info;
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -07003193}
Deeraj Soman14230922019-01-30 16:39:30 +05303194
Deeraj Somanfa377bf2019-02-06 12:57:59 +05303195int audio_extn_utils_get_perf_mode_flag(void)
3196{
3197#ifdef COMPRESSED_PERF_MODE_FLAG
3198 return COMPRESSED_PERF_MODE_FLAG;
3199#else
3200 return 0;
3201#endif
3202}
3203
Deeraj Soman14230922019-01-30 16:39:30 +05303204size_t audio_extn_utils_get_input_buffer_size(uint32_t sample_rate,
3205 audio_format_t format,
3206 int channel_count,
3207 int64_t duration_ms,
3208 bool is_low_latency)
3209{
3210 size_t size = 0;
3211 size_t capture_duration = AUDIO_CAPTURE_PERIOD_DURATION_MSEC;
3212 uint32_t bytes_per_period_sample = 0;
3213
3214
3215 if (audio_extn_utils_check_input_parameters(sample_rate, format, channel_count) != 0)
3216 return 0;
3217
3218 if (duration_ms >= MIN_OFFLOAD_BUFFER_DURATION_MS && duration_ms <= MAX_OFFLOAD_BUFFER_DURATION_MS)
3219 capture_duration = duration_ms;
3220
3221 size = (sample_rate * capture_duration) / 1000;
3222 if (is_low_latency)
3223 size = LOW_LATENCY_CAPTURE_PERIOD_SIZE;
3224
3225
3226 bytes_per_period_sample = audio_bytes_per_sample(format) * channel_count;
3227 size *= bytes_per_period_sample;
3228
3229 /* make sure the size is multiple of 32 bytes and additionally multiple of
3230 * the frame_size (required for 24bit samples and non-power-of-2 channel counts)
3231 * At 48 kHz mono 16-bit PCM:
3232 * 5.000 ms = 240 frames = 15*16*1*2 = 480, a whole multiple of 32 (15)
3233 * 3.333 ms = 160 frames = 10*16*1*2 = 320, a whole multiple of 32 (10)
3234 *
3235 * The loop reaches result within 32 iterations, as initial size is
3236 * already a multiple of frame_size
3237 */
3238 size = audio_extn_utils_nearest_multiple(size, audio_extn_utils_lcm(32, bytes_per_period_sample));
3239
3240 return size;
3241}
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07003242
3243int audio_extn_utils_hash_fn(void *key)
3244{
3245 return (int)key;
3246}
3247
3248bool audio_extn_utils_hash_eq(void *key1, void *key2)
3249{
3250 return (key1 == key2);
3251}