blob: fa4e2d902f00d9df2c177cfca6dd8a476892cbeb [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;
Ramu Gottipati074fa4d2018-09-11 20:05:51 +0530770 ALOGI("%s Allowing 24 and above bits playback on speaker ONLY at default sampling rate", __func__);
Amit Shekhar1d896042014-10-03 13:16:09 -0700771 }
772
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -0700773 property_get("vendor.audio.playback.mch.downsample",value,"");
Manish Dewangan837dc462015-05-27 10:17:41 +0530774 if (!strncmp("true", value, sizeof("true"))) {
775 if ((popcount(channel_mask) > 2) &&
776 (sample_rate > CODEC_BACKEND_DEFAULT_SAMPLE_RATE) &&
Aalique Grahame5cd12ff2017-10-19 10:57:00 -0700777 !(flags & (audio_output_flags_t)AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH)) {
Manish Dewangan837dc462015-05-27 10:17:41 +0530778 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
779 ALOGD("%s: MCH session defaulting sample rate to %d",
780 __func__, sample_rate);
781 }
782 }
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +0530783
784 /* Set sampling rate to 176.4 for DSD64
785 * and 352.8Khz for DSD128.
786 * Set Bit Width to 16. output will be 16 bit
787 * post DoP in ASM.
788 */
Aalique Grahame5cd12ff2017-10-19 10:57:00 -0700789 if ((flags & (audio_output_flags_t)AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH) &&
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +0530790 (format == AUDIO_FORMAT_DSD)) {
791 bit_width = 16;
792 if (sample_rate == INPUT_SAMPLING_RATE_DSD64)
793 sample_rate = OUTPUT_SAMPLING_RATE_DSD64;
794 else if (sample_rate == INPUT_SAMPLING_RATE_DSD128)
795 sample_rate = OUTPUT_SAMPLING_RATE_DSD128;
796 }
797
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +0530798
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800799 ALOGV("%s: flags: %x, format: %x sample_rate %d, profile %s, app_type %d",
800 __func__, flags, format, sample_rate, profile, app_type_cfg->app_type);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700801 list_for_each(node_i, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530802 s_info = node_to_item(node_i, struct streams_io_cfg, list);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530803 /* Along with flags do profile matching if set at either end.*/
804 if (s_info->flags.out_flags == flags &&
805 ((profile[0] == '\0' && s_info->profile[0] == '\0') ||
806 strncmp(s_info->profile, profile, sizeof(s_info->profile)) == 0)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530807 list_for_each(node_j, &s_info->format_list) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700808 sf_info = node_to_item(node_j, struct stream_format, list);
809 if (sf_info->format == format) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530810 if (set_app_type_cfg(s_info, app_type_cfg, sample_rate, bit_width))
Amit Shekhar6f461b12014-08-01 14:52:58 -0700811 return;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700812 }
813 }
814 }
815 }
816 list_for_each(node_i, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530817 s_info = node_to_item(node_i, struct streams_io_cfg, list);
818 if (s_info->flags.out_flags == AUDIO_OUTPUT_FLAG_PRIMARY) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700819 ALOGV("Compatible output profile not found.");
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530820 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
821 app_type_cfg->sample_rate = s_info->app_type_cfg.sample_rate;
822 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700823 ALOGV("%s Default to primary output: App type: %d sample_rate %d",
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530824 __func__, s_info->app_type_cfg.app_type, app_type_cfg->sample_rate);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700825 return;
826 }
827 }
828 ALOGW("%s: App type could not be selected. Falling back to default", __func__);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700829 app_type_cfg->app_type = platform_get_default_app_type(platform);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700830 app_type_cfg->sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700831 app_type_cfg->bit_width = 16;
832}
833
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +0530834static bool audio_is_this_native_usecase(struct audio_usecase *uc)
835{
836 bool native_usecase = false;
837 struct stream_out *out = (struct stream_out*) uc->stream.out;
838
839 if (PCM_PLAYBACK == uc->type && out != NULL &&
840 NATIVE_AUDIO_MODE_INVALID != platform_get_native_support() &&
841 is_offload_usecase(uc->id) &&
842 (out->sample_rate == OUTPUT_SAMPLING_RATE_44100))
843 native_usecase = true;
844
845 return native_usecase;
846}
847
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800848bool audio_extn_is_dsp_bit_width_enforce_mode_supported(audio_output_flags_t flags)
849{
850 /* DSP bitwidth enforce mode for ADM and AFE:
851 * includes:
852 * deep buffer, low latency, direct pcm and offload.
853 * excludes:
854 * ull(raw+fast), VOIP.
855 */
856 if ((flags & AUDIO_OUTPUT_FLAG_VOIP_RX) ||
857 ((flags & AUDIO_OUTPUT_FLAG_RAW) &&
858 (flags & AUDIO_OUTPUT_FLAG_FAST)))
859 return false;
860
861
862 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) ||
863 (flags & AUDIO_OUTPUT_FLAG_DIRECT) ||
864 (flags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) ||
865 (flags & AUDIO_OUTPUT_FLAG_PRIMARY))
866 return true;
867 else
868 return false;
869}
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800870
871static inline bool audio_is_vr_mode_on(struct audio_device *(__attribute__((unused)) adev))
872{
873 return adev->vr_audio_mode_enabled;
874}
875
Derek Chenf7092792017-05-23 12:23:53 -0400876static void audio_extn_btsco_get_sample_rate(int snd_device, int *sample_rate)
877{
878 switch (snd_device) {
879 case SND_DEVICE_OUT_BT_SCO:
880 case SND_DEVICE_IN_BT_SCO_MIC:
881 case SND_DEVICE_IN_BT_SCO_MIC_NREC:
882 *sample_rate = 8000;
883 break;
884 case SND_DEVICE_OUT_BT_SCO_WB:
885 case SND_DEVICE_IN_BT_SCO_MIC_WB:
886 case SND_DEVICE_IN_BT_SCO_MIC_WB_NREC:
887 *sample_rate = 16000;
888 break;
889 default:
890 ALOGD("%s:Not a BT SCO device, need not update sampling rate\n", __func__);
891 break;
892 }
893}
894
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530895void audio_extn_utils_update_stream_app_type_cfg_for_usecase(
896 struct audio_device *adev,
897 struct audio_usecase *usecase)
898{
899 ALOGV("%s", __func__);
900
901 switch(usecase->type) {
902 case PCM_PLAYBACK:
903 audio_extn_utils_update_stream_output_app_type_cfg(adev->platform,
904 &adev->streams_output_cfg_list,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -0800905 &usecase->stream.out->device_list,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530906 usecase->stream.out->flags,
Aalique Grahame65780b52017-09-27 14:59:56 -0700907 usecase->stream.out->hal_op_format,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530908 usecase->stream.out->sample_rate,
909 usecase->stream.out->bit_width,
910 usecase->stream.out->channel_mask,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530911 usecase->stream.out->profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530912 &usecase->stream.out->app_type_cfg);
913 ALOGV("%s Selected apptype: %d", __func__, usecase->stream.out->app_type_cfg.app_type);
914 break;
915 case PCM_CAPTURE:
Revathi Uddarajud9f23d92020-07-27 10:55:06 +0530916 if (usecase->id == USECASE_AUDIO_RECORD_VOIP
917 || usecase->id == USECASE_AUDIO_RECORD_VOIP_LOW_LATENCY)
Vikram Panduranga93f080e2017-06-07 18:16:14 -0700918 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) {
Revathi Uddarajud9f23d92020-07-27 10:55:06 +05301204 if (usecase->id == USECASE_AUDIO_RECORD_VOIP
1205 || usecase->id == USECASE_AUDIO_RECORD_VOIP_LOW_LATENCY)
Zhou Song06761dd2019-04-28 18:08:17 +08001206 usecase->stream.in->app_type_cfg.sample_rate = usecase->stream.in->sample_rate;
1207 if (voice_is_in_call_rec_stream(usecase->stream.in)) {
1208 audio_extn_btsco_get_sample_rate(usecase->in_snd_device,
1209 &usecase->stream.in->app_type_cfg.sample_rate);
1210 } else {
1211 audio_extn_btsco_get_sample_rate(snd_device,
1212 &usecase->stream.in->app_type_cfg.sample_rate);
1213 }
1214 sample_rate = usecase->stream.in->app_type_cfg.sample_rate;
1215 } else if (usecase->id == USECASE_AUDIO_SPKR_CALIB_TX) {
Sujin Panickerb6d76262019-09-09 10:55:38 +05301216 if ((property_get("vendor.audio.spkr_prot.tx.sampling_rate", value, NULL) > 0))
1217 sample_rate = atoi(value);
1218 else
1219 sample_rate = SAMPLE_RATE_8000;
Zhou Song06761dd2019-04-28 18:08:17 +08001220 }
1221 } else if (usecase->type == TRANSCODE_LOOPBACK_RX) {
1222 sample_rate = usecase->stream.inout->out_config.sample_rate;
1223 }
1224 return sample_rate;
1225}
1226
Siena Richard7c2db772016-12-21 11:32:34 -08001227static int send_app_type_cfg_for_device(struct audio_device *adev,
1228 struct audio_usecase *usecase,
1229 int split_snd_device)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001230{
1231 char mixer_ctl_name[MAX_LENGTH_MIXER_CONTROL_IN_INT];
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301232 size_t app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT] = {0};
1233 int len = 0, rc;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001234 struct mixer_ctl *ctl;
Siena Richard7c2db772016-12-21 11:32:34 -08001235 int pcm_device_id = 0, acdb_dev_id, app_type;
1236 int snd_device = split_snd_device, snd_device_be_idx = -1;
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +05301237 int32_t sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Varun Balaraje49253e2017-07-06 19:48:56 +05301238 struct streams_io_cfg *s_info = NULL;
1239 struct listnode *node = NULL;
Nikhil Laturkarac4a7492017-08-31 18:27:19 +05301240 int bd_app_type = 0;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001241
Siena Richard7c2db772016-12-21 11:32:34 -08001242 ALOGV("%s: usecase->out_snd_device %s, usecase->in_snd_device %s, split_snd_device %s",
1243 __func__, platform_get_snd_device_name(usecase->out_snd_device),
1244 platform_get_snd_device_name(usecase->in_snd_device),
1245 platform_get_snd_device_name(split_snd_device));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001246
Siddartha Shaik343abc62017-08-08 11:15:25 +05301247 if (usecase->type != PCM_PLAYBACK && usecase->type != PCM_CAPTURE &&
Surendar Karka93cd25a2018-08-28 14:21:37 +05301248 usecase->type != TRANSCODE_LOOPBACK_RX) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301249 ALOGE("%s: not a playback/capture path, no need to cfg app type", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001250 rc = 0;
1251 goto exit_send_app_type_cfg;
1252 }
1253 if ((usecase->id != USECASE_AUDIO_PLAYBACK_DEEP_BUFFER) &&
Vignesh Kulothungan0a2eff02019-07-11 16:30:13 -07001254 (usecase->id != USECASE_AUDIO_PLAYBACK_WITH_HAPTICS) &&
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001255 (usecase->id != USECASE_AUDIO_PLAYBACK_LOW_LATENCY) &&
1256 (usecase->id != USECASE_AUDIO_PLAYBACK_MULTI_CH) &&
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -08001257 (usecase->id != USECASE_AUDIO_PLAYBACK_ULL) &&
Vikram Panduranga93f080e2017-06-07 18:16:14 -07001258 (usecase->id != USECASE_AUDIO_PLAYBACK_VOIP) &&
Surendar Karka93cd25a2018-08-28 14:21:37 +05301259 (usecase->id != USECASE_AUDIO_TRANSCODE_LOOPBACK_RX) &&
Varun Balaraje49253e2017-07-06 19:48:56 +05301260 (!is_interactive_usecase(usecase->id)) &&
Srikanth Uyyala9d551402015-08-25 16:03:42 +05301261 (!is_offload_usecase(usecase->id)) &&
Derek Chenf6318be2017-06-12 17:16:24 -04001262 (usecase->type != PCM_CAPTURE) &&
1263 (!audio_extn_auto_hal_is_bus_device_usecase(usecase->id))) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301264 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 -07001265 rc = 0;
1266 goto exit_send_app_type_cfg;
1267 }
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -08001268
1269 //if VR is active then only send the mixer control
1270 if (usecase->id == USECASE_AUDIO_PLAYBACK_ULL && !audio_is_vr_mode_on(adev)) {
1271 ALOGI("ULL doesnt need sending app type cfg, returning");
1272 rc = 0;
1273 goto exit_send_app_type_cfg;
1274 }
1275
Surendar Karka93cd25a2018-08-28 14:21:37 +05301276 if (usecase->type == PCM_PLAYBACK || usecase->type == TRANSCODE_LOOPBACK_RX) {
Ben Romberger1fafdde2015-09-09 19:43:15 -07001277 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_PLAYBACK);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301278 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
1279 "Audio Stream %d App Type Cfg", pcm_device_id);
Ben Romberger1fafdde2015-09-09 19:43:15 -07001280 } else if (usecase->type == PCM_CAPTURE) {
Ben Romberger1fafdde2015-09-09 19:43:15 -07001281 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_CAPTURE);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301282 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
1283 "Audio Stream Capture %d App Type Cfg", pcm_device_id);
Srikanth Uyyala9d551402015-08-25 16:03:42 +05301284 }
Siena Richard7c2db772016-12-21 11:32:34 -08001285
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001286 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
1287 if (!ctl) {
1288 ALOGE("%s: Could not get ctl for mixer cmd - %s", __func__,
1289 mixer_ctl_name);
1290 rc = -EINVAL;
1291 goto exit_send_app_type_cfg;
1292 }
Aditya Bavanari701a6992017-03-30 19:17:16 +05301293 snd_device = platform_get_spkr_prot_snd_device(snd_device);
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301294 if (voice_is_in_call_rec_stream(usecase->stream.in) && usecase->type == PCM_CAPTURE) {
1295 snd_device_t voice_device = voice_get_incall_rec_snd_device(usecase->in_snd_device);
1296 acdb_dev_id = platform_get_snd_device_acdb_id(voice_device);
1297 ALOGV("acdb id for voice call use case %d", acdb_dev_id);
1298 } else {
1299 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
1300 }
Rohit Kumar1181b292017-01-31 18:07:17 +05301301 if (acdb_dev_id <= 0) {
1302 ALOGE("%s: Couldn't get the acdb dev id", __func__);
1303 rc = -EINVAL;
1304 goto exit_send_app_type_cfg;
1305 }
1306
Siena Richard7c2db772016-12-21 11:32:34 -08001307 snd_device_be_idx = platform_get_snd_device_backend_index(snd_device);
1308 if (snd_device_be_idx < 0) {
1309 ALOGE("%s: Couldn't get the backend index for snd device %s ret=%d",
1310 __func__, platform_get_snd_device_name(snd_device),
1311 snd_device_be_idx);
1312 }
1313
Zhou Song06761dd2019-04-28 18:08:17 +08001314 sample_rate = audio_extn_utils_get_app_sample_rate_for_device(adev, usecase, snd_device);
1315
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301316 if ((usecase->type == PCM_PLAYBACK) && (usecase->stream.out != NULL)) {
Varun Balaraje49253e2017-07-06 19:48:56 +05301317 /* Interactive streams are supported with only direct app type id.
1318 * Get Direct profile app type and use it for interactive streams
1319 */
1320 list_for_each(node, &adev->streams_output_cfg_list) {
1321 s_info = node_to_item(node, struct streams_io_cfg, list);
Aalique Grahame5cd12ff2017-10-19 10:57:00 -07001322 if (s_info->flags.out_flags == (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_BD |
Nikhil Laturkarac4a7492017-08-31 18:27:19 +05301323 AUDIO_OUTPUT_FLAG_DIRECT_PCM |
1324 AUDIO_OUTPUT_FLAG_DIRECT))
1325 bd_app_type = s_info->app_type_cfg.app_type;
Varun Balaraje49253e2017-07-06 19:48:56 +05301326 }
Aalique Grahame5cd12ff2017-10-19 10:57:00 -07001327 if (usecase->stream.out->flags == (audio_output_flags_t)AUDIO_OUTPUT_FLAG_INTERACTIVE)
Nikhil Laturkarac4a7492017-08-31 18:27:19 +05301328 app_type = bd_app_type;
Varun Balaraje49253e2017-07-06 19:48:56 +05301329 else
1330 app_type = usecase->stream.out->app_type_cfg.app_type;
Siena Richard7c2db772016-12-21 11:32:34 -08001331 app_type_cfg[len++] = app_type;
Mingming Yin21854652016-04-13 11:54:02 -07001332 app_type_cfg[len++] = acdb_dev_id;
Naresh Tanniru3a406772017-05-10 13:09:05 -07001333 app_type_cfg[len++] = sample_rate;
1334
Siena Richard7c2db772016-12-21 11:32:34 -08001335 if (snd_device_be_idx > 0)
1336 app_type_cfg[len++] = snd_device_be_idx;
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301337
Siena Richard7c2db772016-12-21 11:32:34 -08001338 ALOGI("%s PLAYBACK app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
1339 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301340
1341 } else if ((usecase->type == PCM_CAPTURE) && (usecase->stream.in != NULL)) {
Siena Richard7c2db772016-12-21 11:32:34 -08001342 app_type = usecase->stream.in->app_type_cfg.app_type;
1343 app_type_cfg[len++] = app_type;
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301344 app_type_cfg[len++] = acdb_dev_id;
Siena Richard7c2db772016-12-21 11:32:34 -08001345 app_type_cfg[len++] = sample_rate;
1346 if (snd_device_be_idx > 0)
1347 app_type_cfg[len++] = snd_device_be_idx;
1348 ALOGI("%s CAPTURE app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
1349 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301350 } else {
Siena Richard7c2db772016-12-21 11:32:34 -08001351 app_type = platform_get_default_app_type_v2(adev->platform, usecase->type);
Surendar Karka93cd25a2018-08-28 14:21:37 +05301352 if(usecase->type == TRANSCODE_LOOPBACK_RX) {
Siddartha Shaik343abc62017-08-08 11:15:25 +05301353 app_type = usecase->stream.inout->out_app_type_cfg.app_type;
1354 }
Siena Richard7c2db772016-12-21 11:32:34 -08001355 app_type_cfg[len++] = app_type;
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301356 app_type_cfg[len++] = acdb_dev_id;
1357 app_type_cfg[len++] = sample_rate;
Siena Richard7c2db772016-12-21 11:32:34 -08001358 if (snd_device_be_idx > 0)
1359 app_type_cfg[len++] = snd_device_be_idx;
1360 ALOGI("%s default app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
1361 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Ashish Jainc02430d2016-01-04 10:42:43 +05301362 }
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301363
Siddartha Shaik343abc62017-08-08 11:15:25 +05301364 if(ctl)
1365 mixer_ctl_set_array(ctl, app_type_cfg, len);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001366 rc = 0;
1367exit_send_app_type_cfg:
1368 return rc;
1369}
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001370
Deeraj Soman14230922019-01-30 16:39:30 +05301371static int audio_extn_utils_check_input_parameters(uint32_t sample_rate,
1372 audio_format_t format,
1373 int channel_count)
1374{
1375 int ret = 0;
1376
1377 if (((format != AUDIO_FORMAT_PCM_16_BIT) && (format != AUDIO_FORMAT_PCM_8_24_BIT) &&
1378 (format != AUDIO_FORMAT_PCM_24_BIT_PACKED) && (format != AUDIO_FORMAT_PCM_32_BIT) &&
1379 (format != AUDIO_FORMAT_PCM_FLOAT)) &&
1380 !voice_extn_compress_voip_is_format_supported(format) &&
1381 !audio_extn_compr_cap_format_supported(format) &&
1382 !audio_extn_cin_format_supported(format))
1383 ret = -EINVAL;
1384
1385 switch (channel_count) {
1386 case 1:
1387 case 2:
1388 case 3:
1389 case 4:
1390 case 6:
1391 case 8:
1392 break;
1393 default:
1394 ret = -EINVAL;
1395 }
1396
1397 switch (sample_rate) {
1398 case 8000:
1399 case 11025:
1400 case 12000:
1401 case 16000:
1402 case 22050:
1403 case 24000:
1404 case 32000:
1405 case 44100:
1406 case 48000:
1407 case 88200:
1408 case 96000:
1409 case 176400:
1410 case 192000:
1411 break;
1412 default:
1413 ret = -EINVAL;
1414 }
1415
1416 return ret;
1417}
1418
1419static inline uint32_t audio_extn_utils_nearest_multiple(uint32_t num, uint32_t multiplier)
1420{
1421 uint32_t remainder = 0;
1422
1423 if (!multiplier)
1424 return num;
1425
1426 remainder = num % multiplier;
1427 if (remainder)
1428 num += (multiplier - remainder);
1429
1430 return num;
1431}
1432
1433static inline uint32_t audio_extn_utils_lcm(uint32_t num1, uint32_t num2)
1434{
1435 uint32_t high = num1, low = num2, temp = 0;
1436
1437 if (!num1 || !num2)
1438 return 0;
1439
1440 if (num1 < num2) {
1441 high = num2;
1442 low = num1;
1443 }
1444
1445 while (low != 0) {
1446 temp = low;
1447 low = high % low;
1448 high = temp;
1449 }
1450 return (num1 * num2)/high;
1451}
1452
Siena Richard7c2db772016-12-21 11:32:34 -08001453int audio_extn_utils_send_app_type_cfg(struct audio_device *adev,
1454 struct audio_usecase *usecase)
1455{
1456 int i, num_devices = 0;
1457 snd_device_t new_snd_devices[SND_DEVICE_OUT_END] = {0};
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301458 snd_device_t in_snd_device = usecase->in_snd_device;
Siena Richard7c2db772016-12-21 11:32:34 -08001459 int rc = 0;
1460
Aalique Grahame22e49102018-12-18 14:23:57 -08001461 if (usecase->type == PCM_HFP_CALL) {
1462 return audio_extn_utils_send_app_type_cfg_hfp(adev, usecase);
1463 }
1464
Siena Richard7c2db772016-12-21 11:32:34 -08001465 switch (usecase->type) {
1466 case PCM_PLAYBACK:
Surendar Karka93cd25a2018-08-28 14:21:37 +05301467 case TRANSCODE_LOOPBACK_RX:
Siena Richard7c2db772016-12-21 11:32:34 -08001468 ALOGD("%s: usecase->out_snd_device %s",
1469 __func__, platform_get_snd_device_name(usecase->out_snd_device));
1470 /* check for out combo device */
1471 if (platform_split_snd_device(adev->platform,
1472 usecase->out_snd_device,
1473 &num_devices, new_snd_devices)) {
1474 new_snd_devices[0] = usecase->out_snd_device;
1475 num_devices = 1;
1476 }
1477 break;
1478 case PCM_CAPTURE:
1479 ALOGD("%s: usecase->in_snd_device %s",
1480 __func__, platform_get_snd_device_name(usecase->in_snd_device));
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301481 if (voice_is_in_call_rec_stream(usecase->stream.in)) {
1482 in_snd_device = voice_get_incall_rec_backend_device(usecase->stream.in);
1483 }
Siena Richard7c2db772016-12-21 11:32:34 -08001484 /* check for in combo device */
1485 if (platform_split_snd_device(adev->platform,
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301486 in_snd_device,
Siena Richard7c2db772016-12-21 11:32:34 -08001487 &num_devices, new_snd_devices)) {
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301488 new_snd_devices[0] = in_snd_device;
Siena Richard7c2db772016-12-21 11:32:34 -08001489 num_devices = 1;
1490 }
1491 break;
1492 default:
1493 ALOGI("%s: not a playback/capture path, no need to cfg app type", __func__);
1494 rc = 0;
1495 break;
1496 }
1497
1498 for (i = 0; i < num_devices; i++) {
1499 rc = send_app_type_cfg_for_device(adev, usecase, new_snd_devices[i]);
1500 if (rc)
1501 break;
1502 }
1503
1504 return rc;
1505}
1506
Preetam Singh Ranawat9519e9c2015-11-18 16:05:55 +05301507int read_line_from_file(const char *path, char *buf, size_t count)
1508{
1509 char * fgets_ret;
1510 FILE * fd;
1511 int rv;
1512
1513 fd = fopen(path, "r");
1514 if (fd == NULL)
1515 return -1;
1516
1517 fgets_ret = fgets(buf, (int)count, fd);
1518 if (NULL != fgets_ret) {
1519 rv = (int)strlen(buf);
1520 } else {
1521 rv = ferror(fd);
1522 }
1523 fclose(fd);
1524
1525 return rv;
1526}
1527
Ashish Jainf1eaa582016-05-23 20:54:24 +05301528/*Translates ALSA formats to AOSP PCM formats*/
1529audio_format_t alsa_format_to_hal(uint32_t alsa_format)
1530{
1531 audio_format_t format;
1532
1533 switch(alsa_format) {
1534 case SNDRV_PCM_FORMAT_S16_LE:
1535 format = AUDIO_FORMAT_PCM_16_BIT;
1536 break;
1537 case SNDRV_PCM_FORMAT_S24_3LE:
1538 format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
1539 break;
1540 case SNDRV_PCM_FORMAT_S24_LE:
1541 format = AUDIO_FORMAT_PCM_8_24_BIT;
1542 break;
1543 case SNDRV_PCM_FORMAT_S32_LE:
1544 format = AUDIO_FORMAT_PCM_32_BIT;
1545 break;
1546 default:
1547 ALOGW("Incorrect ALSA format");
1548 format = AUDIO_FORMAT_INVALID;
1549 }
1550 return format;
1551}
1552
1553/*Translates hal format (AOSP) to alsa formats*/
1554uint32_t hal_format_to_alsa(audio_format_t hal_format)
1555{
1556 uint32_t alsa_format;
1557
1558 switch (hal_format) {
1559 case AUDIO_FORMAT_PCM_32_BIT: {
1560 if (platform_supports_true_32bit())
1561 alsa_format = SNDRV_PCM_FORMAT_S32_LE;
1562 else
1563 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1564 }
1565 break;
1566 case AUDIO_FORMAT_PCM_8_BIT:
1567 alsa_format = SNDRV_PCM_FORMAT_S8;
1568 break;
1569 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
1570 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1571 break;
1572 case AUDIO_FORMAT_PCM_8_24_BIT: {
1573 if (platform_supports_true_32bit())
1574 alsa_format = SNDRV_PCM_FORMAT_S32_LE;
1575 else
1576 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1577 }
1578 break;
1579 case AUDIO_FORMAT_PCM_FLOAT:
1580 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1581 break;
1582 default:
1583 case AUDIO_FORMAT_PCM_16_BIT:
1584 alsa_format = SNDRV_PCM_FORMAT_S16_LE;
1585 break;
1586 }
1587 return alsa_format;
1588}
1589
Ashish Jain83a6cc22016-06-28 14:34:17 +05301590/*Translates PCM formats to AOSP formats*/
1591audio_format_t pcm_format_to_hal(uint32_t pcm_format)
1592{
1593 audio_format_t format = AUDIO_FORMAT_INVALID;
1594
1595 switch(pcm_format) {
1596 case PCM_FORMAT_S16_LE:
1597 format = AUDIO_FORMAT_PCM_16_BIT;
1598 break;
1599 case PCM_FORMAT_S24_3LE:
1600 format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
1601 break;
1602 case PCM_FORMAT_S24_LE:
1603 format = AUDIO_FORMAT_PCM_8_24_BIT;
1604 break;
1605 case PCM_FORMAT_S32_LE:
1606 format = AUDIO_FORMAT_PCM_32_BIT;
1607 break;
1608 default:
1609 ALOGW("Incorrect PCM format");
1610 format = AUDIO_FORMAT_INVALID;
1611 }
1612 return format;
1613}
1614
1615/*Translates hal format (AOSP) to alsa formats*/
1616uint32_t hal_format_to_pcm(audio_format_t hal_format)
1617{
1618 uint32_t pcm_format;
1619
1620 switch (hal_format) {
1621 case AUDIO_FORMAT_PCM_32_BIT:
1622 case AUDIO_FORMAT_PCM_8_24_BIT:
1623 case AUDIO_FORMAT_PCM_FLOAT: {
1624 if (platform_supports_true_32bit())
1625 pcm_format = PCM_FORMAT_S32_LE;
1626 else
1627 pcm_format = PCM_FORMAT_S24_3LE;
1628 }
1629 break;
1630 case AUDIO_FORMAT_PCM_8_BIT:
1631 pcm_format = PCM_FORMAT_S8;
1632 break;
1633 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
1634 pcm_format = PCM_FORMAT_S24_3LE;
1635 break;
1636 default:
1637 case AUDIO_FORMAT_PCM_16_BIT:
1638 pcm_format = PCM_FORMAT_S16_LE;
1639 break;
1640 }
1641 return pcm_format;
1642}
1643
Ashish Jainf1eaa582016-05-23 20:54:24 +05301644uint32_t get_alsa_fragment_size(uint32_t bytes_per_sample,
1645 uint32_t sample_rate,
Deeraj Soman65358ab2019-02-07 15:40:49 +05301646 uint32_t noOfChannels,
1647 int64_t duration_ms)
Ashish Jainf1eaa582016-05-23 20:54:24 +05301648{
1649 uint32_t fragment_size = 0;
1650 uint32_t pcm_offload_time = PCM_OFFLOAD_BUFFER_DURATION;
1651
Deeraj Soman65358ab2019-02-07 15:40:49 +05301652 if (duration_ms >= MIN_OFFLOAD_BUFFER_DURATION_MS && duration_ms <= MAX_OFFLOAD_BUFFER_DURATION_MS)
1653 pcm_offload_time = duration_ms;
1654
Ashish Jainf1eaa582016-05-23 20:54:24 +05301655 fragment_size = (pcm_offload_time
1656 * sample_rate
1657 * bytes_per_sample
1658 * noOfChannels)/1000;
1659 if (fragment_size < MIN_PCM_OFFLOAD_FRAGMENT_SIZE)
1660 fragment_size = MIN_PCM_OFFLOAD_FRAGMENT_SIZE;
1661 else if (fragment_size > MAX_PCM_OFFLOAD_FRAGMENT_SIZE)
1662 fragment_size = MAX_PCM_OFFLOAD_FRAGMENT_SIZE;
1663 /*To have same PCM samples for all channels, the buffer size requires to
1664 *be multiple of (number of channels * bytes per sample)
1665 *For writes to succeed, the buffer must be written at address which is multiple of 32
1666 */
Aalique Grahameb85f2d92017-10-16 17:53:23 -07001667 fragment_size = ALIGN(fragment_size, (bytes_per_sample * noOfChannels * 32));
Ashish Jainf1eaa582016-05-23 20:54:24 +05301668
1669 ALOGI("PCM offload Fragment size to %d bytes", fragment_size);
1670 return fragment_size;
1671}
1672
1673/* Calculates the fragment size required to configure compress session.
1674 * Based on the alsa format selected, decide if conversion is needed in
1675
1676 * HAL ( e.g. convert AUDIO_FORMAT_PCM_FLOAT input format to
1677 * AUDIO_FORMAT_PCM_24_BIT_PACKED before writing to the compress driver.
1678 */
1679void audio_extn_utils_update_direct_pcm_fragment_size(struct stream_out *out)
1680{
Ashish Jain83a6cc22016-06-28 14:34:17 +05301681 audio_format_t dst_format = out->hal_op_format;
1682 audio_format_t src_format = out->hal_ip_format;
Ashish Jainf1eaa582016-05-23 20:54:24 +05301683 uint32_t hal_op_bytes_per_sample = audio_bytes_per_sample(dst_format);
1684 uint32_t hal_ip_bytes_per_sample = audio_bytes_per_sample(src_format);
1685
1686 out->compr_config.fragment_size =
1687 get_alsa_fragment_size(hal_op_bytes_per_sample,
1688 out->sample_rate,
Deeraj Soman65358ab2019-02-07 15:40:49 +05301689 popcount(out->channel_mask),
1690 out->info.duration_us / 1000);
Ashish Jainf1eaa582016-05-23 20:54:24 +05301691
1692 if ((src_format != dst_format) &&
1693 hal_op_bytes_per_sample != hal_ip_bytes_per_sample) {
1694
Ashish Jain83a6cc22016-06-28 14:34:17 +05301695 out->hal_fragment_size =
Ashish Jainf1eaa582016-05-23 20:54:24 +05301696 ((out->compr_config.fragment_size * hal_ip_bytes_per_sample) /
1697 hal_op_bytes_per_sample);
1698 ALOGI("enable conversion hal_input_fragment_size is %d src_format %x dst_format %x",
Ashish Jain83a6cc22016-06-28 14:34:17 +05301699 out->hal_fragment_size, src_format, dst_format);
Ashish Jainf1eaa582016-05-23 20:54:24 +05301700 } else {
Ashish Jain83a6cc22016-06-28 14:34:17 +05301701 out->hal_fragment_size = out->compr_config.fragment_size;
Ashish Jainf1eaa582016-05-23 20:54:24 +05301702 }
1703}
1704
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301705/* converts pcm format 24_8 to 8_24 inplace */
1706size_t audio_extn_utils_convert_format_24_8_to_8_24(void *buf, size_t bytes)
1707{
1708 size_t i = 0;
1709 int *int_buf_stream = buf;
1710
1711 if ((bytes % 4) != 0) {
1712 ALOGE("%s: wrong inout buffer! ... is not 32 bit aligned ", __func__);
1713 return -EINVAL;
1714 }
1715
1716 for (; i < (bytes / 4); i++)
1717 int_buf_stream[i] >>= 8;
1718
1719 return bytes;
1720}
1721
Xiaojun Sang782e5b12020-06-29 21:13:06 +08001722#ifdef AUDIO_GKI_ENABLED
1723int get_snd_codec_id(audio_format_t format)
1724{
1725 int id = 0;
1726
1727 switch (format & AUDIO_FORMAT_MAIN_MASK) {
1728 case AUDIO_FORMAT_MP3:
1729 id = SND_AUDIOCODEC_MP3;
1730 break;
1731 case AUDIO_FORMAT_AAC:
1732 id = SND_AUDIOCODEC_AAC;
1733 break;
1734 case AUDIO_FORMAT_AAC_ADTS:
1735 id = SND_AUDIOCODEC_AAC;
1736 break;
1737 case AUDIO_FORMAT_AAC_LATM:
1738 id = SND_AUDIOCODEC_AAC;
1739 break;
1740 case AUDIO_FORMAT_PCM:
1741 id = SND_AUDIOCODEC_PCM;
1742 break;
1743 case AUDIO_FORMAT_FLAC:
1744 case AUDIO_FORMAT_ALAC:
1745 case AUDIO_FORMAT_APE:
1746 case AUDIO_FORMAT_VORBIS:
1747 case AUDIO_FORMAT_WMA:
1748 case AUDIO_FORMAT_WMA_PRO:
1749 case AUDIO_FORMAT_DSD:
1750 case AUDIO_FORMAT_APTX:
1751 id = SND_AUDIOCODEC_BESPOKE;
1752 break;
1753 case AUDIO_FORMAT_MP2:
1754 id = SND_AUDIOCODEC_MP2;
1755 break;
1756 case AUDIO_FORMAT_AC3:
1757 id = SND_AUDIOCODEC_AC3;
1758 break;
1759 case AUDIO_FORMAT_E_AC3:
1760 case AUDIO_FORMAT_E_AC3_JOC:
1761 id = SND_AUDIOCODEC_EAC3;
1762 break;
1763 case AUDIO_FORMAT_DTS:
1764 case AUDIO_FORMAT_DTS_HD:
1765 id = SND_AUDIOCODEC_DTS;
1766 break;
1767 case AUDIO_FORMAT_DOLBY_TRUEHD:
1768 id = SND_AUDIOCODEC_TRUEHD;
1769 break;
1770 case AUDIO_FORMAT_IEC61937:
1771 id = SND_AUDIOCODEC_IEC61937;
1772 break;
1773 default:
1774 ALOGE("%s: Unsupported audio format :%x", __func__, format);
1775 }
1776
1777 return id;
1778}
1779#else
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301780int get_snd_codec_id(audio_format_t format)
1781{
1782 int id = 0;
1783
1784 switch (format & AUDIO_FORMAT_MAIN_MASK) {
1785 case AUDIO_FORMAT_MP3:
1786 id = SND_AUDIOCODEC_MP3;
1787 break;
1788 case AUDIO_FORMAT_AAC:
1789 id = SND_AUDIOCODEC_AAC;
1790 break;
1791 case AUDIO_FORMAT_AAC_ADTS:
1792 id = SND_AUDIOCODEC_AAC;
1793 break;
Arun Kumar Dasari3b174182016-12-27 13:01:14 +05301794 case AUDIO_FORMAT_AAC_LATM:
1795 id = SND_AUDIOCODEC_AAC;
1796 break;
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301797 case AUDIO_FORMAT_PCM:
1798 id = SND_AUDIOCODEC_PCM;
1799 break;
1800 case AUDIO_FORMAT_FLAC:
1801 id = SND_AUDIOCODEC_FLAC;
1802 break;
1803 case AUDIO_FORMAT_ALAC:
1804 id = SND_AUDIOCODEC_ALAC;
1805 break;
1806 case AUDIO_FORMAT_APE:
1807 id = SND_AUDIOCODEC_APE;
1808 break;
1809 case AUDIO_FORMAT_VORBIS:
1810 id = SND_AUDIOCODEC_VORBIS;
1811 break;
1812 case AUDIO_FORMAT_WMA:
1813 id = SND_AUDIOCODEC_WMA;
1814 break;
1815 case AUDIO_FORMAT_WMA_PRO:
1816 id = SND_AUDIOCODEC_WMA_PRO;
1817 break;
Satish Babu Patakokila0c313922016-12-08 12:07:08 +05301818 case AUDIO_FORMAT_MP2:
1819 id = SND_AUDIOCODEC_MP2;
1820 break;
Satish Babu Patakokila915ecba2017-01-10 17:43:56 +05301821 case AUDIO_FORMAT_AC3:
1822 id = SND_AUDIOCODEC_AC3;
1823 break;
1824 case AUDIO_FORMAT_E_AC3:
1825 case AUDIO_FORMAT_E_AC3_JOC:
1826 id = SND_AUDIOCODEC_EAC3;
1827 break;
1828 case AUDIO_FORMAT_DTS:
1829 case AUDIO_FORMAT_DTS_HD:
1830 id = SND_AUDIOCODEC_DTS;
1831 break;
Ben Romberger1aaaf862017-04-06 17:49:46 -07001832 case AUDIO_FORMAT_DOLBY_TRUEHD:
1833 id = SND_AUDIOCODEC_TRUEHD;
1834 break;
Naresh Tanniru928f0862017-04-07 16:44:23 -07001835 case AUDIO_FORMAT_IEC61937:
1836 id = SND_AUDIOCODEC_IEC61937;
1837 break;
Preetam Singh Ranawat4277a5a2017-01-18 19:02:24 +05301838 case AUDIO_FORMAT_DSD:
1839 id = SND_AUDIOCODEC_DSD;
1840 break;
Dhanalakshmi Siddani18737932016-11-29 17:33:17 +05301841 case AUDIO_FORMAT_APTX:
1842 id = SND_AUDIOCODEC_APTX;
1843 break;
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301844 default:
1845 ALOGE("%s: Unsupported audio format :%x", __func__, format);
1846 }
1847
1848 return id;
1849}
Xiaojun Sang782e5b12020-06-29 21:13:06 +08001850#endif
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301851
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001852void audio_extn_utils_send_audio_calibration(struct audio_device *adev,
1853 struct audio_usecase *usecase)
1854{
1855 int type = usecase->type;
1856
Dhananjay Kumar14245982017-01-16 20:21:00 +05301857 if (type == PCM_PLAYBACK && usecase->stream.out != NULL) {
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05301858 platform_send_audio_calibration(adev->platform, usecase,
Zhou Song06761dd2019-04-28 18:08:17 +08001859 usecase->stream.out->app_type_cfg.app_type);
Dhananjay Kumar14245982017-01-16 20:21:00 +05301860 } else if (type == PCM_CAPTURE && usecase->stream.in != NULL) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301861 platform_send_audio_calibration(adev->platform, usecase,
Zhou Song06761dd2019-04-28 18:08:17 +08001862 usecase->stream.in->app_type_cfg.app_type);
1863 } else if ((type == PCM_HFP_CALL) || (type == PCM_CAPTURE) ||
1864 (type == TRANSCODE_LOOPBACK_RX && usecase->stream.inout != NULL)) {
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05301865 platform_send_audio_calibration(adev->platform, usecase,
Zhou Song06761dd2019-04-28 18:08:17 +08001866 platform_get_default_app_type_v2(adev->platform, usecase->type));
Vidyakumar Athotae57f6002017-03-01 13:13:16 -08001867 } else {
1868 /* No need to send audio calibration for voice and voip call usecases */
1869 if ((type != VOICE_CALL) && (type != VOIP_CALL))
1870 ALOGW("%s: No audio calibration for usecase type = %d", __func__, type);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001871 }
1872}
1873
Ben Rombergera04fabc2014-11-14 12:16:03 -08001874// Base64 Encode and Decode
1875// Not all features supported. This must be used only with following conditions.
1876// Decode Modes: Support with and without padding
1877// CRLF not handling. So no CRLF in string to decode.
1878// Encode Modes: Supports only padding
1879int b64decode(char *inp, int ilen, uint8_t* outp)
1880{
1881 int i, j, k, ii, num;
1882 int rem, pcnt;
1883 uint32_t res=0;
1884 uint8_t getIndex[MAX_BASEINDEX_LEN];
1885 uint8_t tmp, cflag;
1886
1887 if(inp == NULL || outp == NULL || ilen <= 0) {
1888 ALOGE("[%s] received NULL pointer or zero length",__func__);
1889 return -1;
1890 }
1891
1892 memset(getIndex, MAX_BASEINDEX_LEN-1, sizeof(getIndex));
1893 for(i=0;i<BASE_TABLE_SIZE;i++) {
1894 getIndex[(uint8_t)bTable[i]] = (uint8_t)i;
1895 }
1896 getIndex[(uint8_t)'=']=0;
1897
1898 j=0;k=0;
1899 num = ilen/4;
1900 rem = ilen%4;
1901 if(rem==0)
1902 num = num-1;
1903 cflag=0;
1904 for(i=0; i<num; i++) {
1905 res=0;
1906 for(ii=0;ii<4;ii++) {
1907 res = res << 6;
1908 tmp = getIndex[(uint8_t)inp[j++]];
1909 res = res | tmp;
1910 cflag = cflag | tmp;
1911 }
1912 outp[k++] = (res >> 16)&0xFF;
1913 outp[k++] = (res >> 8)&0xFF;
1914 outp[k++] = res & 0xFF;
1915 }
1916
1917 // Handle last bytes special
1918 pcnt=0;
1919 if(rem == 0) {
1920 //With padding or full data
1921 res = 0;
1922 for(ii=0;ii<4;ii++) {
1923 if(inp[j] == '=')
1924 pcnt++;
1925 res = res << 6;
1926 tmp = getIndex[(uint8_t)inp[j++]];
1927 res = res | tmp;
1928 cflag = cflag | tmp;
1929 }
1930 outp[k++] = res >> 16;
1931 if(pcnt == 2)
1932 goto done;
1933 outp[k++] = (res>>8)&0xFF;
1934 if(pcnt == 1)
1935 goto done;
1936 outp[k++] = res&0xFF;
1937 } else {
1938 //without padding
1939 res = 0;
1940 for(i=0;i<rem;i++) {
1941 res = res << 6;
1942 tmp = getIndex[(uint8_t)inp[j++]];
1943 res = res | tmp;
1944 cflag = cflag | tmp;
1945 }
1946 for(i=rem;i<4;i++) {
1947 res = res << 6;
1948 pcnt++;
1949 }
1950 outp[k++] = res >> 16;
1951 if(pcnt == 2)
1952 goto done;
1953 outp[k++] = (res>>8)&0xFF;
1954 if(pcnt == 1)
1955 goto done;
1956 outp[k++] = res&0xFF;
1957 }
1958done:
1959 if(cflag == 0xFF) {
1960 ALOGE("[%s] base64 decode failed. Invalid character found %s",
1961 __func__, inp);
1962 return 0;
1963 }
1964 return k;
1965}
1966
1967int b64encode(uint8_t *inp, int ilen, char* outp)
1968{
1969 int i,j,k, num;
1970 int rem=0;
1971 uint32_t res=0;
1972
1973 if(inp == NULL || outp == NULL || ilen<=0) {
1974 ALOGE("[%s] received NULL pointer or zero input length",__func__);
1975 return -1;
1976 }
1977
1978 num = ilen/3;
1979 rem = ilen%3;
1980 j=0;k=0;
1981 for(i=0; i<num; i++) {
1982 //prepare index
1983 res = inp[j++]<<16;
1984 res = res | inp[j++]<<8;
1985 res = res | inp[j++];
1986 //get output map from index
1987 outp[k++] = (char) bTable[(res>>18)&0x3F];
1988 outp[k++] = (char) bTable[(res>>12)&0x3F];
1989 outp[k++] = (char) bTable[(res>>6)&0x3F];
1990 outp[k++] = (char) bTable[res&0x3F];
1991 }
1992
1993 switch(rem) {
1994 case 1:
1995 res = inp[j++]<<16;
1996 outp[k++] = (char) bTable[res>>18];
1997 outp[k++] = (char) bTable[(res>>12)&0x3F];
1998 //outp[k++] = '=';
1999 //outp[k++] = '=';
2000 break;
2001 case 2:
2002 res = inp[j++]<<16;
2003 res = res | inp[j++]<<8;
2004 outp[k++] = (char) bTable[res>>18];
2005 outp[k++] = (char) bTable[(res>>12)&0x3F];
2006 outp[k++] = (char) bTable[(res>>6)&0x3F];
2007 //outp[k++] = '=';
2008 break;
2009 default:
2010 break;
2011 }
Ben Rombergera04fabc2014-11-14 12:16:03 -08002012 outp[k] = '\0';
2013 return k;
2014}
Ashish Jain81eb2a82015-05-13 10:52:34 +05302015
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05302016
2017int audio_extn_utils_get_codec_version(const char *snd_card_name,
2018 int card_num,
2019 char *codec_version)
2020{
2021 char procfs_path[50];
2022 FILE *fp;
2023
2024 if (strstr(snd_card_name, "tasha")) {
2025 snprintf(procfs_path, sizeof(procfs_path),
2026 "/proc/asound/card%d/codecs/tasha/version", card_num);
2027 if ((fp = fopen(procfs_path, "r")) != NULL) {
2028 fgets(codec_version, CODEC_VERSION_MAX_LENGTH, fp);
2029 fclose(fp);
2030 } else {
2031 ALOGE("%s: ERROR. cannot open %s", __func__, procfs_path);
2032 return -ENOENT;
2033 }
2034 ALOGD("%s: codec version %s", __func__, codec_version);
2035 }
2036
2037 return 0;
2038}
2039
Preetam Singh Ranawat9d0d8e42019-07-15 12:27:25 +05302040int audio_extn_utils_get_codec_variant(int card_num,
2041 char *codec_variant)
2042{
2043 char procfs_path[50];
2044 FILE *fp;
2045 snprintf(procfs_path, sizeof(procfs_path),
2046 "/proc/asound/card%d/codecs/wcd938x/variant", card_num);
2047 if ((fp = fopen(procfs_path, "r")) == NULL) {
2048 snprintf(procfs_path, sizeof(procfs_path),
2049 "/proc/asound/card%d/codecs/wcd937x/variant", card_num);
2050 if ((fp = fopen(procfs_path, "r")) == NULL) {
2051 ALOGE("%s: ERROR. cannot open %s", __func__, procfs_path);
2052 return -ENOENT;
2053 }
2054 }
2055 fgets(codec_variant, CODEC_VARIANT_MAX_LENGTH, fp);
2056 fclose(fp);
2057 ALOGD("%s: codec variant is %s", __func__, codec_variant);
2058 return 0;
2059}
2060
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05302061
Ashish Jain81eb2a82015-05-13 10:52:34 +05302062#ifdef AUDIO_EXTERNAL_HDMI_ENABLED
2063
2064void get_default_compressed_channel_status(
2065 unsigned char *channel_status)
2066{
Ashish Jain81eb2a82015-05-13 10:52:34 +05302067 memset(channel_status,0,24);
2068
2069 /* block start bit in preamble bit 3 */
2070 channel_status[0] |= PROFESSIONAL;
2071 //compre out
2072 channel_status[0] |= NON_LPCM;
2073 // sample rate; fixed 48K for default/transcode
2074 channel_status[3] |= SR_48000;
2075}
2076
Ashish Jain81eb2a82015-05-13 10:52:34 +05302077int32_t get_compressed_channel_status(void *audio_stream_data,
2078 uint32_t audio_frame_size,
2079 unsigned char *channel_status,
2080 enum audio_parser_code_type codec_type)
2081 // codec_type - AUDIO_PARSER_CODEC_AC3
2082 // - AUDIO_PARSER_CODEC_DTS
2083{
2084 unsigned char *stream;
2085 int ret = 0;
2086 stream = (unsigned char *)audio_stream_data;
2087
2088 if (audio_stream_data == NULL || audio_frame_size == 0) {
2089 ALOGW("no buffer to get channel status, return default for compress");
2090 get_default_compressed_channel_status(channel_status);
2091 return ret;
2092 }
2093
2094 memset(channel_status,0,24);
2095 if(init_audio_parser(stream, audio_frame_size, codec_type) == -1)
2096 {
2097 ALOGE("init audio parser failed");
2098 return -1;
2099 }
2100 ret = get_channel_status(channel_status, codec_type);
2101 return ret;
2102
2103}
2104
Ashish Jain81eb2a82015-05-13 10:52:34 +05302105void get_lpcm_channel_status(uint32_t sampleRate,
2106 unsigned char *channel_status)
2107{
2108 int32_t status = 0;
Ashish Jain81eb2a82015-05-13 10:52:34 +05302109 memset(channel_status,0,24);
2110 /* block start bit in preamble bit 3 */
2111 channel_status[0] |= PROFESSIONAL;
2112 //LPCM OUT
2113 channel_status[0] &= ~NON_LPCM;
2114
2115 switch (sampleRate) {
2116 case 8000:
2117 case 11025:
2118 case 12000:
2119 case 16000:
2120 case 22050:
2121 channel_status[3] |= SR_NOTID;
Preetam Singh Ranawat61716b12015-12-14 11:55:24 +05302122 break;
Ashish Jain81eb2a82015-05-13 10:52:34 +05302123 case 24000:
2124 channel_status[3] |= SR_24000;
2125 break;
2126 case 32000:
2127 channel_status[3] |= SR_32000;
2128 break;
2129 case 44100:
2130 channel_status[3] |= SR_44100;
2131 break;
2132 case 48000:
2133 channel_status[3] |= SR_48000;
2134 break;
2135 case 88200:
2136 channel_status[3] |= SR_88200;
2137 break;
2138 case 96000:
2139 channel_status[3] |= SR_96000;
2140 break;
2141 case 176400:
2142 channel_status[3] |= SR_176400;
2143 break;
2144 case 192000:
2145 channel_status[3] |= SR_192000;
2146 break;
2147 default:
2148 ALOGV("Invalid sample_rate %u\n", sampleRate);
2149 status = -1;
2150 break;
2151 }
2152}
2153
2154void audio_utils_set_hdmi_channel_status(struct stream_out *out, char * buffer, size_t bytes)
2155{
2156 unsigned char channel_status[24]={0};
2157 struct snd_aes_iec958 iec958;
2158 const char *mixer_ctl_name = "IEC958 Playback PCM Stream";
2159 struct mixer_ctl *ctl;
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +05302160 ALOGV("%s: buffer %s bytes %zd", __func__, buffer, bytes);
Arun Mirpurie008ed22019-03-21 11:21:04 -07002161
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05302162 if (audio_extn_utils_is_dolby_format(out->format) &&
Ashish Jain81eb2a82015-05-13 10:52:34 +05302163 /*TODO:Extend code to support DTS passthrough*/
2164 /*set compressed channel status bits*/
Arun Mirpurie008ed22019-03-21 11:21:04 -07002165 audio_extn_passthru_is_passthrough_stream(out) &&
2166 audio_extn_is_hdmi_passthru_enabled()) {
Ashish Jain81eb2a82015-05-13 10:52:34 +05302167 get_compressed_channel_status(buffer, bytes, channel_status, AUDIO_PARSER_CODEC_AC3);
Arun Mirpurie008ed22019-03-21 11:21:04 -07002168 } else {
Ashish Jain81eb2a82015-05-13 10:52:34 +05302169 /*set channel status bit for LPCM*/
2170 get_lpcm_channel_status(out->sample_rate, channel_status);
2171 }
2172
2173 memcpy(iec958.status, channel_status,sizeof(iec958.status));
2174 ctl = mixer_get_ctl_by_name(out->dev->mixer, mixer_ctl_name);
2175 if (!ctl) {
2176 ALOGE("%s: Could not get ctl for mixer cmd - %s",
2177 __func__, mixer_ctl_name);
2178 return;
2179 }
2180 if (mixer_ctl_set_array(ctl, &iec958, sizeof(iec958)) < 0) {
2181 ALOGE("%s: Could not set channel status for ext HDMI ",
2182 __func__);
2183 return;
2184 }
2185
2186}
2187#endif
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302188
2189int audio_extn_utils_get_avt_device_drift(
2190 struct audio_usecase *usecase,
2191 struct audio_avt_device_drift_param *drift_param)
2192{
2193 int ret = 0, count = 0;
2194 char avt_device_drift_mixer_ctl_name[MIXER_PATH_MAX_LENGTH] = {0};
Naresh Tanniru6160c712017-04-17 15:43:48 +05302195 const char *backend = NULL;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302196 struct mixer_ctl *ctl = NULL;
2197 struct audio_avt_device_drift_stats drift_stats;
2198 struct audio_device *adev = NULL;
2199
2200 if (usecase != NULL && usecase->type == PCM_PLAYBACK) {
Naresh Tanniru6160c712017-04-17 15:43:48 +05302201 backend = platform_get_snd_device_backend_interface(usecase->out_snd_device);
2202 if (!backend) {
2203 ALOGE("%s: Unsupported device %d", __func__,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08002204 get_device_types(&usecase->stream.out->device_list));
Naresh Tanniru6160c712017-04-17 15:43:48 +05302205 ret = -EINVAL;
2206 goto done;
2207 }
2208 strlcpy(avt_device_drift_mixer_ctl_name,
2209 backend,
2210 MIXER_PATH_MAX_LENGTH);
2211
2212 count = strlen(backend);
2213 if (MIXER_PATH_MAX_LENGTH - count > 0) {
2214 strlcat(&avt_device_drift_mixer_ctl_name[count],
2215 " DRIFT",
2216 MIXER_PATH_MAX_LENGTH - count);
2217 } else {
2218 ret = -EINVAL;
2219 goto done;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302220 }
2221 } else {
Naresh Tanniru7586e292017-04-13 10:38:13 +05302222 ALOGE("%s: Invalid usecase",__func__);
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302223 ret = -EINVAL;
Naresh Tanniru6160c712017-04-17 15:43:48 +05302224 goto done;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302225 }
2226
Naresh Tanniru6160c712017-04-17 15:43:48 +05302227 adev = usecase->stream.out->dev;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302228 ctl = mixer_get_ctl_by_name(adev->mixer, avt_device_drift_mixer_ctl_name);
2229 if (!ctl) {
2230 ALOGE("%s: Could not get ctl for mixer cmd - %s",
2231 __func__, avt_device_drift_mixer_ctl_name);
2232
2233 ret = -EINVAL;
2234 goto done;
2235 }
2236
2237 ALOGV("%s: Getting AV Timer vs Device Drift mixer ctrl name %s", __func__,
2238 avt_device_drift_mixer_ctl_name);
2239
2240 mixer_ctl_update(ctl);
2241 count = mixer_ctl_get_num_values(ctl);
2242 if (count != sizeof(struct audio_avt_device_drift_stats)) {
2243 ALOGE("%s: mixer_ctl_get_num_values() invalid drift_stats data size",
2244 __func__);
2245
2246 ret = -EINVAL;
2247 goto done;
2248 }
2249
2250 ret = mixer_ctl_get_array(ctl, (void *)&drift_stats, count);
2251 if (ret != 0) {
2252 ALOGE("%s: mixer_ctl_get_array() failed to get drift_stats Params",
2253 __func__);
2254
2255 ret = -EINVAL;
2256 goto done;
2257 }
2258 memcpy(drift_param, &drift_stats.drift_param,
2259 sizeof(struct audio_avt_device_drift_param));
2260done:
2261 return ret;
2262}
Manish Dewangan07de2142017-02-27 19:27:20 +05302263
2264#ifdef SNDRV_COMPRESS_PATH_DELAY
2265int audio_extn_utils_compress_get_dsp_latency(struct stream_out *out)
2266{
2267 int ret = -EINVAL;
2268 struct snd_compr_metadata metadata;
2269 int delay_ms = COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
2270
Weiyin Jiangc49a3b52018-08-17 16:16:08 +08002271 /* override the latency for pcm offload use case */
2272 if ((out->flags & AUDIO_OUTPUT_FLAG_DIRECT) &&
2273 !(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)) {
2274 delay_ms = PCM_OFFLOAD_PLAYBACK_LATENCY;
2275 }
2276
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -07002277 if (property_get_bool("vendor.audio.playback.dsp.pathdelay", false)) {
Manish Dewangan07de2142017-02-27 19:27:20 +05302278 ALOGD("%s:: Quering DSP delay %d",__func__, __LINE__);
2279 if (!(is_offload_usecase(out->usecase))) {
2280 ALOGE("%s:: not supported for non offload session", __func__);
2281 goto exit;
2282 }
2283
2284 if (!out->compr) {
2285 ALOGD("%s:: Invalid compress handle,returning default dsp latency",
2286 __func__);
2287 goto exit;
2288 }
2289
2290 metadata.key = SNDRV_COMPRESS_PATH_DELAY;
2291 ret = compress_get_metadata(out->compr, &metadata);
2292 if(ret) {
2293 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2294 goto exit;
2295 }
2296 delay_ms = metadata.value[0] / 1000; /*convert to ms*/
2297 } else {
2298 ALOGD("%s:: Using Fix DSP delay",__func__);
2299 }
2300
2301exit:
2302 ALOGD("%s:: delay in ms is %d",__func__, delay_ms);
2303 return delay_ms;
2304}
2305#else
2306int audio_extn_utils_compress_get_dsp_latency(struct stream_out *out __unused)
2307{
2308 return COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
2309}
2310#endif
Manish Dewangan69426c82017-01-30 17:35:36 +05302311
2312#ifdef SNDRV_COMPRESS_RENDER_MODE
2313int audio_extn_utils_compress_set_render_mode(struct stream_out *out)
2314{
2315 struct snd_compr_metadata metadata;
2316 int ret = -EINVAL;
2317
2318 if (!(is_offload_usecase(out->usecase))) {
2319 ALOGE("%s:: not supported for non offload session", __func__);
2320 goto exit;
2321 }
2322
2323 if (!out->compr) {
2324 ALOGD("%s:: Invalid compress handle",
2325 __func__);
2326 goto exit;
2327 }
2328
2329 ALOGD("%s:: render mode %d", __func__, out->render_mode);
2330
2331 metadata.key = SNDRV_COMPRESS_RENDER_MODE;
2332 if (out->render_mode == RENDER_MODE_AUDIO_MASTER) {
2333 metadata.value[0] = SNDRV_COMPRESS_RENDER_MODE_AUDIO_MASTER;
2334 } else if (out->render_mode == RENDER_MODE_AUDIO_STC_MASTER) {
2335 metadata.value[0] = SNDRV_COMPRESS_RENDER_MODE_STC_MASTER;
2336 } else {
2337 ret = 0;
2338 goto exit;
2339 }
2340 ret = compress_set_metadata(out->compr, &metadata);
2341 if(ret) {
2342 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2343 }
2344exit:
2345 return ret;
2346}
2347#else
2348int audio_extn_utils_compress_set_render_mode(struct stream_out *out __unused)
2349{
2350 ALOGD("%s:: configuring render mode not supported", __func__);
2351 return 0;
2352}
2353#endif
Manish Dewangan58229382017-02-02 15:48:41 +05302354
2355#ifdef SNDRV_COMPRESS_CLK_REC_MODE
2356int audio_extn_utils_compress_set_clk_rec_mode(
2357 struct audio_usecase *usecase)
2358{
2359 struct snd_compr_metadata metadata;
2360 struct stream_out *out = NULL;
2361 int ret = -EINVAL;
2362
Naresh Tanniru7586e292017-04-13 10:38:13 +05302363 if (usecase == NULL || usecase->type != PCM_PLAYBACK) {
Manish Dewangan58229382017-02-02 15:48:41 +05302364 ALOGE("%s:: Invalid use case", __func__);
2365 goto exit;
2366 }
2367
2368 out = usecase->stream.out;
2369 if (!out) {
2370 ALOGE("%s:: invalid stream", __func__);
2371 goto exit;
2372 }
2373
2374 if (!is_offload_usecase(out->usecase)) {
2375 ALOGE("%s:: not supported for non offload session", __func__);
2376 goto exit;
2377 }
2378
2379 if (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER) {
2380 ALOGD("%s:: clk recovery is only supported in STC render mode",
2381 __func__);
2382 ret = 0;
2383 goto exit;
2384 }
2385
2386 if (!out->compr) {
2387 ALOGD("%s:: Invalid compress handle",
2388 __func__);
2389 goto exit;
2390 }
2391 metadata.key = SNDRV_COMPRESS_CLK_REC_MODE;
2392 switch(usecase->out_snd_device) {
2393 case SND_DEVICE_OUT_HDMI:
2394 case SND_DEVICE_OUT_SPEAKER_AND_HDMI:
2395 case SND_DEVICE_OUT_DISPLAY_PORT:
2396 case SND_DEVICE_OUT_SPEAKER_AND_DISPLAY_PORT:
2397 metadata.value[0] = SNDRV_COMPRESS_CLK_REC_MODE_NONE;
2398 break;
2399 default:
2400 metadata.value[0] = SNDRV_COMPRESS_CLK_REC_MODE_AUTO;
2401 break;
2402 }
2403
2404 ALOGD("%s:: clk recovery mode %d",__func__, metadata.value[0]);
2405
2406 ret = compress_set_metadata(out->compr, &metadata);
2407 if(ret) {
2408 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2409 }
2410
2411exit:
2412 return ret;
2413}
2414#else
2415int audio_extn_utils_compress_set_clk_rec_mode(
2416 struct audio_usecase *usecase __unused)
2417{
2418 ALOGD("%s:: configuring render mode not supported", __func__);
2419 return 0;
2420}
2421#endif
Manish Dewangan27346042017-03-01 12:56:12 +05302422
2423#ifdef SNDRV_COMPRESS_RENDER_WINDOW
2424int audio_extn_utils_compress_set_render_window(
2425 struct stream_out *out,
2426 struct audio_out_render_window_param *render_window)
2427{
2428 struct snd_compr_metadata metadata;
2429 int ret = -EINVAL;
2430
Manish Dewangan27346042017-03-01 12:56:12 +05302431 if(render_window == NULL) {
2432 ALOGE("%s:: Invalid render_window", __func__);
2433 goto exit;
2434 }
2435
Aniket Kumar Lata1e1d3662017-06-01 18:45:48 -07002436 ALOGD("%s:: render window start 0x%"PRIx64" end 0x%"PRIx64"",
2437 __func__,render_window->render_ws, render_window->render_we);
2438
Manish Dewangan27346042017-03-01 12:56:12 +05302439 if (!is_offload_usecase(out->usecase)) {
2440 ALOGE("%s:: not supported for non offload session", __func__);
2441 goto exit;
2442 }
2443
Naresh Tanniru6160c712017-04-17 15:43:48 +05302444 if ((out->render_mode != RENDER_MODE_AUDIO_MASTER) &&
2445 (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER)) {
Manish Dewangan27346042017-03-01 12:56:12 +05302446 ALOGD("%s:: only supported in timestamp mode, current "
2447 "render mode mode %d", __func__, out->render_mode);
2448 goto exit;
2449 }
2450
2451 if (!out->compr) {
2452 ALOGW("%s:: offload session not yet opened,"
2453 "render window will be configure later", __func__);
2454 /* store render window to reconfigure in start_output_stream() */
2455 goto exit;
2456 }
2457
2458 metadata.key = SNDRV_COMPRESS_RENDER_WINDOW;
2459 /*render window start value */
2460 metadata.value[0] = 0xFFFFFFFF & render_window->render_ws; /* lsb */
2461 metadata.value[1] = \
2462 (0xFFFFFFFF00000000 & render_window->render_ws) >> 32; /* msb*/
2463 /*render window end value */
2464 metadata.value[2] = 0xFFFFFFFF & render_window->render_we; /* lsb */
2465 metadata.value[3] = \
2466 (0xFFFFFFFF00000000 & render_window->render_we) >> 32; /* msb*/
2467
2468 ret = compress_set_metadata(out->compr, &metadata);
2469 if(ret) {
2470 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2471 }
2472
2473exit:
2474 return ret;
2475}
2476#else
2477int audio_extn_utils_compress_set_render_window(
2478 struct stream_out *out __unused,
2479 struct audio_out_render_window_param *render_window __unused)
2480{
2481 ALOGD("%s:: configuring render window not supported", __func__);
2482 return 0;
2483}
2484#endif
Manish Dewangan14956cc2017-02-14 18:54:42 +05302485
2486#ifdef SNDRV_COMPRESS_START_DELAY
2487int audio_extn_utils_compress_set_start_delay(
2488 struct stream_out *out,
2489 struct audio_out_start_delay_param *delay_param)
2490{
2491 struct snd_compr_metadata metadata;
2492 int ret = -EINVAL;
2493
2494 if(delay_param == NULL) {
2495 ALOGE("%s:: Invalid delay_param", __func__);
2496 goto exit;
2497 }
2498
2499 ALOGD("%s:: render start delay 0x%"PRIx64" ", __func__,
2500 delay_param->start_delay);
2501
2502 if (!is_offload_usecase(out->usecase)) {
2503 ALOGE("%s:: not supported for non offload session", __func__);
2504 goto exit;
2505 }
2506
Naresh Tanniru6160c712017-04-17 15:43:48 +05302507 if ((out->render_mode != RENDER_MODE_AUDIO_MASTER) &&
2508 (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER)) {
Manish Dewangan14956cc2017-02-14 18:54:42 +05302509 ALOGD("%s:: only supported in timestamp mode, current "
2510 "render mode mode %d", __func__, out->render_mode);
2511 goto exit;
2512 }
2513
2514 if (!out->compr) {
2515 ALOGW("%s:: offload session not yet opened,"
2516 "start delay will be configure later", __func__);
2517 goto exit;
2518 }
2519
2520 metadata.key = SNDRV_COMPRESS_START_DELAY;
2521 metadata.value[0] = 0xFFFFFFFF & delay_param->start_delay; /* lsb */
2522 metadata.value[1] = \
2523 (0xFFFFFFFF00000000 & delay_param->start_delay) >> 32; /* msb*/
2524
2525 ret = compress_set_metadata(out->compr, &metadata);
2526 if(ret) {
2527 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2528 }
2529
2530exit:
2531 return ret;
2532}
2533#else
2534int audio_extn_utils_compress_set_start_delay(
2535 struct stream_out *out __unused,
2536 struct audio_out_start_delay_param *delay_param __unused)
2537{
2538 ALOGD("%s:: configuring render window not supported", __func__);
2539 return 0;
2540}
2541#endif
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002542
Surendar Karka287348c2019-04-10 18:31:46 +05302543#ifdef SNDRV_COMPRESS_DSP_POSITION
2544int audio_extn_utils_compress_get_dsp_presentation_pos(struct stream_out *out,
2545 uint64_t *frames, struct timespec *timestamp, int32_t clock_id)
2546{
2547 int ret = -EINVAL;
2548 uint64_t *val = NULL;
2549 uint64_t time = 0;
2550 struct snd_compr_metadata metadata;
2551
2552 ALOGV("%s:: Quering DSP position with clock id %d",__func__, clock_id);
2553 metadata.key = SNDRV_COMPRESS_DSP_POSITION;
2554 metadata.value[0] = clock_id;
2555 ret = compress_get_metadata(out->compr, &metadata);
2556 if (ret) {
2557 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2558 ret = -errno;
2559 goto exit;
2560 }
2561 val = (uint64_t *)&metadata.value[1];
2562 *frames = *val;
2563 time = *(val + 1);
2564 timestamp->tv_sec = time / 1000000;
2565 timestamp->tv_nsec = (time % 1000000)*1000;
2566
2567exit:
2568 return ret;
2569}
2570#else
2571int audio_extn_utils_compress_get_dsp_presentation_pos(struct stream_out *out __unused,
2572 uint64_t *frames __unused, struct timespec *timestamp __unused,
2573 int32_t clock_id __unused)
2574{
2575 ALOGD("%s:: dsp presentation position not supported", __func__);
2576 return 0;
2577
2578}
2579#endif
2580
2581#ifdef SNDRV_PCM_IOCTL_DSP_POSITION
2582int audio_extn_utils_pcm_get_dsp_presentation_pos(struct stream_out *out,
2583 uint64_t *frames, struct timespec *timestamp, int32_t clock_id)
2584{
2585 int ret = -EINVAL;
2586 uint64_t time = 0;
2587 struct snd_pcm_prsnt_position prsnt_position;
2588
2589 ALOGV("%s:: Quering DSP position with clock id %d",__func__, clock_id);
2590 prsnt_position.clock_id = clock_id;
2591 ret = pcm_ioctl(out->pcm, SNDRV_PCM_IOCTL_DSP_POSITION, &prsnt_position);
2592 if (ret) {
2593 ALOGE("%s::error %d", __func__, ret);
2594 ret = -EIO;
2595 goto exit;
2596 }
2597
2598 *frames = prsnt_position.frames;
2599 time = prsnt_position.timestamp;
2600 timestamp->tv_sec = time / 1000000;
2601 timestamp->tv_nsec = (time % 1000000)*1000;
2602
2603exit:
2604 return ret;
2605}
2606#else
2607int audio_extn_utils_pcm_get_dsp_presentation_pos(struct stream_out *out __unused,
2608 uint64_t *frames __unused, struct timespec *timestamp __unused,
2609 int32_t clock_id __unused)
2610{
2611 ALOGD("%s:: dsp presentation position not supported", __func__);
2612 return 0;
2613
2614}
2615#endif
2616
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002617#define MAX_SND_CARD 8
Ashish Jain30ae8942017-11-05 17:21:23 +05302618#define RETRY_US 1000000
2619#define RETRY_NUMBER 40
Aalique Grahame22e49102018-12-18 14:23:57 -08002620#define PLATFORM_INFO_XML_PATH "audio_platform_info.xml"
2621#define PLATFORM_INFO_XML_BASE_STRING "audio_platform_info"
2622
Aalique Grahame22e49102018-12-18 14:23:57 -08002623bool audio_extn_utils_resolve_config_file(char file_name[MIXER_PATH_MAX_LENGTH])
2624{
2625 char full_config_path[MIXER_PATH_MAX_LENGTH];
Saurav Kumarf5d2c342020-07-29 19:31:49 +05302626 char vendor_config_path[VENDOR_CONFIG_PATH_MAX_LENGTH];
2627
2628 /* Get path for audio configuration files in vendor */
2629 audio_get_vendor_config_path(vendor_config_path,
2630 sizeof(vendor_config_path));
2631 snprintf(full_config_path, sizeof(full_config_path),
2632 "%s/%s", vendor_config_path, file_name);
2633 if (F_OK == access(full_config_path, 0)) {
2634 strlcpy(file_name, full_config_path, MIXER_PATH_MAX_LENGTH);
2635 return true;
Aalique Grahame22e49102018-12-18 14:23:57 -08002636 }
2637 return false;
2638}
2639
2640/* platform_info_file should be size 'MIXER_PATH_MAX_LENGTH' */
2641int audio_extn_utils_get_platform_info(const char* snd_card_name, char* platform_info_file)
2642{
2643 if (NULL == snd_card_name) {
2644 return -1;
2645 }
2646
2647 struct snd_card_split *snd_split_handle = NULL;
2648 int ret = 0;
2649 audio_extn_set_snd_card_split(snd_card_name);
2650 snd_split_handle = audio_extn_get_snd_card_split();
2651
2652 snprintf(platform_info_file, MIXER_PATH_MAX_LENGTH, "%s_%s_%s.xml",
2653 PLATFORM_INFO_XML_BASE_STRING, snd_split_handle->snd_card,
2654 snd_split_handle->form_factor);
2655
2656 if (!audio_extn_utils_resolve_config_file(platform_info_file)) {
2657 memset(platform_info_file, 0, MIXER_PATH_MAX_LENGTH);
2658 snprintf(platform_info_file, MIXER_PATH_MAX_LENGTH, "%s_%s.xml",
2659 PLATFORM_INFO_XML_BASE_STRING, snd_split_handle->snd_card);
2660
2661 if (!audio_extn_utils_resolve_config_file(platform_info_file)) {
2662 memset(platform_info_file, 0, MIXER_PATH_MAX_LENGTH);
2663 strlcpy(platform_info_file, PLATFORM_INFO_XML_PATH, MIXER_PATH_MAX_LENGTH);
2664 ret = audio_extn_utils_resolve_config_file(platform_info_file) ? 0 : -1;
2665 }
2666 }
2667
2668 return ret;
2669}
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002670
2671int audio_extn_utils_get_snd_card_num()
2672{
Soumya Managoli9fee7c62018-04-06 16:21:50 +05302673 int snd_card_num = 0;
2674 struct mixer *mixer = NULL;
2675
2676 snd_card_num = audio_extn_utils_open_snd_mixer(&mixer);
2677 if (mixer)
2678 mixer_close(mixer);
2679 return snd_card_num;
2680}
2681
2682int audio_extn_utils_open_snd_mixer(struct mixer **mixer_handle)
2683{
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002684
2685 void *hw_info = NULL;
2686 struct mixer *mixer = NULL;
2687 int retry_num = 0;
mpangfaa7bae2019-01-15 16:38:13 +08002688 int snd_card_num = 0;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002689 char* snd_card_name = NULL;
mpangfaa7bae2019-01-15 16:38:13 +08002690 int snd_card_detection_info[MAX_SND_CARD] = {0};
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002691
Soumya Managoli9fee7c62018-04-06 16:21:50 +05302692 if (!mixer_handle) {
2693 ALOGE("invalid mixer handle");
2694 return -1;
2695 }
2696 *mixer_handle = NULL;
Ashish Jain30ae8942017-11-05 17:21:23 +05302697 /*
mpangfaa7bae2019-01-15 16:38:13 +08002698 * Try with all the sound cards ( 0 to 7 ) and if none of them were detected
Ashish Jain30ae8942017-11-05 17:21:23 +05302699 * sleep for 1 sec and try detections with sound card 0 again.
2700 * If sound card gets detected, check if it is relevant, if not check with the
2701 * other sound cards. To ensure that the irrelevant sound card is not check again,
2702 * we maintain it in min_snd_card_num.
2703 */
2704 while (retry_num < RETRY_NUMBER) {
mpangfaa7bae2019-01-15 16:38:13 +08002705 snd_card_num = 0;
2706 while (snd_card_num < MAX_SND_CARD) {
2707 if (snd_card_detection_info[snd_card_num] == 0) {
2708 mixer = mixer_open(snd_card_num);
2709 if (!mixer)
2710 snd_card_num++;
2711 else
2712 break;
2713 } else
2714 snd_card_num++;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002715 }
2716
2717 if (!mixer) {
Ashish Jain30ae8942017-11-05 17:21:23 +05302718 usleep(RETRY_US);
Ashish Jain30ae8942017-11-05 17:21:23 +05302719 retry_num++;
mpangfaa7bae2019-01-15 16:38:13 +08002720 ALOGD("%s: retry, retry_num %d", __func__, retry_num);
Ashish Jain30ae8942017-11-05 17:21:23 +05302721 continue;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002722 }
2723
2724 snd_card_name = strdup(mixer_get_name(mixer));
2725 if (!snd_card_name) {
2726 ALOGE("failed to allocate memory for snd_card_name\n");
2727 mixer_close(mixer);
2728 return -1;
2729 }
2730 ALOGD("%s: snd_card_name: %s", __func__, snd_card_name);
mpangfaa7bae2019-01-15 16:38:13 +08002731 snd_card_detection_info[snd_card_num] = 1;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002732 hw_info = hw_info_init(snd_card_name);
2733 if (hw_info) {
2734 ALOGD("%s: Opened sound card:%d", __func__, snd_card_num);
2735 break;
2736 }
mpangfaa7bae2019-01-15 16:38:13 +08002737 ALOGE("%s: Failed to init hardware info, snd_card_num:%d", __func__, snd_card_num);
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002738
Dhananjay Kumara7e27832017-07-11 15:40:39 +05302739 free(snd_card_name);
2740 snd_card_name = NULL;
2741
2742 mixer_close(mixer);
2743 mixer = NULL;
2744 }
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002745 if (snd_card_name)
2746 free(snd_card_name);
Ashish Jain30ae8942017-11-05 17:21:23 +05302747
Dhananjay Kumara7e27832017-07-11 15:40:39 +05302748 if (hw_info)
2749 hw_info_deinit(hw_info);
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002750
mpangfaa7bae2019-01-15 16:38:13 +08002751 if (retry_num >= RETRY_NUMBER) {
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002752 ALOGE("%s: Unable to find correct sound card, aborting.", __func__);
2753 return -1;
2754 }
2755
Soumya Managoli9fee7c62018-04-06 16:21:50 +05302756 if (mixer)
2757 *mixer_handle = mixer;
2758
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002759 return snd_card_num;
2760}
Naresh Tanniru6160c712017-04-17 15:43:48 +05302761
Soumya Managoli9fee7c62018-04-06 16:21:50 +05302762void audio_extn_utils_close_snd_mixer(struct mixer *mixer)
2763{
2764 if (mixer)
2765 mixer_close(mixer);
2766}
2767
Naresh Tanniru6160c712017-04-17 15:43:48 +05302768#ifdef SNDRV_COMPRESS_ENABLE_ADJUST_SESSION_CLOCK
2769int audio_extn_utils_compress_enable_drift_correction(
2770 struct stream_out *out,
2771 struct audio_out_enable_drift_correction *drift)
2772{
2773 struct snd_compr_metadata metadata;
2774 int ret = -EINVAL;
2775
2776 if(drift == NULL) {
2777 ALOGE("%s:: Invalid param", __func__);
2778 goto exit;
2779 }
2780
2781 ALOGD("%s:: drift enable %d", __func__,drift->enable);
2782
2783 if (!is_offload_usecase(out->usecase)) {
2784 ALOGE("%s:: not supported for non offload session", __func__);
2785 goto exit;
2786 }
2787
2788 if (!out->compr) {
2789 ALOGW("%s:: offload session not yet opened,"
2790 "start delay will be configure later", __func__);
2791 goto exit;
2792 }
2793
2794 metadata.key = SNDRV_COMPRESS_ENABLE_ADJUST_SESSION_CLOCK;
2795 metadata.value[0] = drift->enable;
2796 out->drift_correction_enabled = drift->enable;
2797
2798 ret = compress_set_metadata(out->compr, &metadata);
2799 if(ret) {
2800 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2801 out->drift_correction_enabled = false;
2802 }
2803
2804exit:
2805 return ret;
2806}
2807#else
2808int audio_extn_utils_compress_enable_drift_correction(
2809 struct stream_out *out __unused,
2810 struct audio_out_enable_drift_correction *drift __unused)
2811{
2812 ALOGD("%s:: configuring drift enablement not supported", __func__);
2813 return 0;
2814}
2815#endif
2816
2817#ifdef SNDRV_COMPRESS_ADJUST_SESSION_CLOCK
2818int audio_extn_utils_compress_correct_drift(
2819 struct stream_out *out,
2820 struct audio_out_correct_drift *drift_param)
2821{
2822 struct snd_compr_metadata metadata;
2823 int ret = -EINVAL;
2824
2825 if (drift_param == NULL) {
2826 ALOGE("%s:: Invalid drift_param", __func__);
2827 goto exit;
2828 }
2829
2830 ALOGD("%s:: adjust time 0x%"PRIx64" ", __func__,
2831 drift_param->adjust_time);
2832
2833 if (!is_offload_usecase(out->usecase)) {
2834 ALOGE("%s:: not supported for non offload session", __func__);
2835 goto exit;
2836 }
2837
2838 if (!out->compr) {
2839 ALOGW("%s:: offload session not yet opened", __func__);
2840 goto exit;
2841 }
2842
2843 if (!out->drift_correction_enabled) {
2844 ALOGE("%s:: drift correction not enabled", __func__);
2845 goto exit;
2846 }
2847
2848 metadata.key = SNDRV_COMPRESS_ADJUST_SESSION_CLOCK;
2849 metadata.value[0] = 0xFFFFFFFF & drift_param->adjust_time; /* lsb */
2850 metadata.value[1] = \
2851 (0xFFFFFFFF00000000 & drift_param->adjust_time) >> 32; /* msb*/
2852
2853 ret = compress_set_metadata(out->compr, &metadata);
2854 if(ret)
2855 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2856exit:
2857 return ret;
2858}
2859#else
2860int audio_extn_utils_compress_correct_drift(
2861 struct stream_out *out __unused,
2862 struct audio_out_correct_drift *drift_param __unused)
2863{
2864 ALOGD("%s:: setting adjust clock not supported", __func__);
2865 return 0;
2866}
2867#endif
Naresh Tanniru29bce4e2017-04-27 17:54:30 +05302868
2869int audio_extn_utils_set_channel_map(
2870 struct stream_out *out,
2871 struct audio_out_channel_map_param *channel_map_param)
2872{
2873 int ret = -EINVAL, i = 0;
2874 int channels = audio_channel_count_from_out_mask(out->channel_mask);
2875
2876 if (channel_map_param == NULL) {
2877 ALOGE("%s:: Invalid channel_map", __func__);
2878 goto exit;
2879 }
2880
2881 if (channel_map_param->channels != channels) {
2882 ALOGE("%s:: Channels(%d) does not match stream channels(%d)",
2883 __func__, channel_map_param->channels, channels);
2884 goto exit;
2885 }
2886
2887 for ( i = 0; i < channels; i++) {
2888 ALOGV("%s:: channel_map[%d]- %d", __func__, i, channel_map_param->channel_map[i]);
2889 out->channel_map_param.channel_map[i] = channel_map_param->channel_map[i];
2890 }
2891 ret = 0;
2892exit:
2893 return ret;
2894}
Varun Balaraje49253e2017-07-06 19:48:56 +05302895
2896int audio_extn_utils_set_pan_scale_params(
2897 struct stream_out *out,
2898 struct mix_matrix_params *mm_params)
2899{
2900 int ret = -EINVAL, i = 0, j = 0;
2901
Varun Balaraj003ee752017-08-07 17:54:55 +05302902 if (mm_params == NULL || out == NULL) {
2903 ALOGE("%s:: Invalid mix matrix or out param", __func__);
Varun Balaraje49253e2017-07-06 19:48:56 +05302904 goto exit;
2905 }
2906
2907 if (mm_params->num_output_channels > MAX_CHANNELS_SUPPORTED ||
2908 mm_params->num_output_channels <= 0 ||
2909 mm_params->num_input_channels > MAX_CHANNELS_SUPPORTED ||
2910 mm_params->num_input_channels <= 0)
2911 goto exit;
2912
2913 out->pan_scale_params.num_output_channels = mm_params->num_output_channels;
2914 out->pan_scale_params.num_input_channels = mm_params->num_input_channels;
2915 out->pan_scale_params.has_output_channel_map =
2916 mm_params->has_output_channel_map;
2917 for (i = 0; i < mm_params->num_output_channels; i++)
2918 out->pan_scale_params.output_channel_map[i] =
2919 mm_params->output_channel_map[i];
2920
2921 out->pan_scale_params.has_input_channel_map =
2922 mm_params->has_input_channel_map;
2923 for (i = 0; i < mm_params->num_input_channels; i++)
2924 out->pan_scale_params.input_channel_map[i] =
2925 mm_params->input_channel_map[i];
2926
2927 out->pan_scale_params.has_mixer_coeffs = mm_params->has_mixer_coeffs;
2928 for (i = 0; i < mm_params->num_output_channels; i++)
2929 for (j = 0; j < mm_params->num_input_channels; j++) {
2930 //Convert the channel coefficient gains in Q14 format
2931 out->pan_scale_params.mixer_coeffs[i][j] =
2932 mm_params->mixer_coeffs[i][j] * (2 << 13);
2933 }
2934
2935 ret = platform_set_stream_pan_scale_params(out->dev->platform,
2936 out->pcm_device_id,
2937 out->pan_scale_params);
2938
2939exit:
2940 return ret;
2941}
2942
2943int audio_extn_utils_set_downmix_params(
2944 struct stream_out *out,
2945 struct mix_matrix_params *mm_params)
2946{
2947 int ret = -EINVAL, i = 0, j = 0;
2948 struct audio_usecase *usecase = NULL;
2949
Aniket Kumar Lataf9f246e2017-09-15 15:20:16 -07002950 if (mm_params == NULL || out == NULL) {
Varun Balaraj003ee752017-08-07 17:54:55 +05302951 ALOGE("%s:: Invalid mix matrix or out param", __func__);
Varun Balaraje49253e2017-07-06 19:48:56 +05302952 goto exit;
2953 }
2954
2955 if (mm_params->num_output_channels > MAX_CHANNELS_SUPPORTED ||
2956 mm_params->num_output_channels <= 0 ||
2957 mm_params->num_input_channels > MAX_CHANNELS_SUPPORTED ||
2958 mm_params->num_input_channels <= 0)
2959 goto exit;
2960
2961 usecase = get_usecase_from_list(out->dev, out->usecase);
Varun Balaraj003ee752017-08-07 17:54:55 +05302962 if (!usecase) {
2963 ALOGE("%s: Get usecase list failed!", __func__);
Aniket Kumar Lataf9f246e2017-09-15 15:20:16 -07002964 goto exit;
2965 }
Varun Balaraje49253e2017-07-06 19:48:56 +05302966 out->downmix_params.num_output_channels = mm_params->num_output_channels;
2967 out->downmix_params.num_input_channels = mm_params->num_input_channels;
2968
2969 out->downmix_params.has_output_channel_map =
2970 mm_params->has_output_channel_map;
2971 for (i = 0; i < mm_params->num_output_channels; i++) {
2972 out->downmix_params.output_channel_map[i] =
2973 mm_params->output_channel_map[i];
2974 }
2975
2976 out->downmix_params.has_input_channel_map =
2977 mm_params->has_input_channel_map;
2978 for (i = 0; i < mm_params->num_input_channels; i++)
2979 out->downmix_params.input_channel_map[i] =
2980 mm_params->input_channel_map[i];
2981
2982 out->downmix_params.has_mixer_coeffs = mm_params->has_mixer_coeffs;
2983 for (i = 0; i < mm_params->num_output_channels; i++)
Nikhil Latukar2e6f6242017-08-15 13:37:07 +05302984 for (j = 0; j < mm_params->num_input_channels; j++) {
2985 //Convert the channel coefficient gains in Q14 format
Varun Balaraje49253e2017-07-06 19:48:56 +05302986 out->downmix_params.mixer_coeffs[i][j] =
Nikhil Latukar2e6f6242017-08-15 13:37:07 +05302987 mm_params->mixer_coeffs[i][j] * (2 << 13);
2988 }
Varun Balaraje49253e2017-07-06 19:48:56 +05302989
2990 ret = platform_set_stream_downmix_params(out->dev->platform,
2991 out->pcm_device_id,
2992 usecase->out_snd_device,
2993 out->downmix_params);
2994
2995exit:
2996 return ret;
2997}
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05302998
2999bool audio_extn_utils_is_dolby_format(audio_format_t format)
3000{
3001 if (format == AUDIO_FORMAT_AC3 ||
3002 format == AUDIO_FORMAT_E_AC3 ||
3003 format == AUDIO_FORMAT_E_AC3_JOC)
3004 return true;
3005 else
3006 return false;
3007}
3008
`Deeraj Soman676c2702017-09-18 19:25:53 +05303009int audio_extn_utils_get_bit_width_from_string(const char *id_string)
3010{
3011 int i;
3012 const mixer_config_lookup mixer_bitwidth_config[] = {{"S24_3LE", 24},
3013 {"S32_LE", 32},
3014 {"S24_LE", 24},
3015 {"S16_LE", 16}};
3016 int num_configs = sizeof(mixer_bitwidth_config) / sizeof(mixer_bitwidth_config[0]);
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05303017
`Deeraj Soman676c2702017-09-18 19:25:53 +05303018 for (i = 0; i < num_configs; i++) {
3019 if (!strcmp(id_string, mixer_bitwidth_config[i].id_string))
3020 return mixer_bitwidth_config[i].value;
3021 }
3022
3023 return -EINVAL;
3024}
3025
3026int audio_extn_utils_get_sample_rate_from_string(const char *id_string)
3027{
3028 int i;
3029 const mixer_config_lookup mixer_samplerate_config[] = {{"KHZ_32", 32000},
3030 {"KHZ_48", 48000},
3031 {"KHZ_96", 96000},
3032 {"KHZ_144", 144000},
3033 {"KHZ_192", 192000},
3034 {"KHZ_384", 384000},
3035 {"KHZ_44P1", 44100},
3036 {"KHZ_88P2", 88200},
3037 {"KHZ_176P4", 176400},
3038 {"KHZ_352P8", 352800}};
3039 int num_configs = sizeof(mixer_samplerate_config) / sizeof(mixer_samplerate_config[0]);
3040
3041 for (i = 0; i < num_configs; i++) {
3042 if (!strcmp(id_string, mixer_samplerate_config[i].id_string))
3043 return mixer_samplerate_config[i].value;
3044 }
3045
3046 return -EINVAL;
3047}
3048
3049int audio_extn_utils_get_channels_from_string(const char *id_string)
3050{
3051 int i;
3052 const mixer_config_lookup mixer_channels_config[] = {{"One", 1},
3053 {"Two", 2},
3054 {"Three",3},
3055 {"Four", 4},
3056 {"Five", 5},
3057 {"Six", 6},
3058 {"Seven", 7},
3059 {"Eight", 8}};
3060 int num_configs = sizeof(mixer_channels_config) / sizeof(mixer_channels_config[0]);
3061
3062 for (i = 0; i < num_configs; i++) {
3063 if (!strcmp(id_string, mixer_channels_config[i].id_string))
3064 return mixer_channels_config[i].value;
3065 }
3066
3067 return -EINVAL;
3068}
Surendar karka30569792018-05-08 12:02:21 +05303069
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08003070void audio_extn_utils_release_snd_device(snd_device_t snd_device)
3071{
3072 audio_extn_dev_arbi_release(snd_device);
3073 audio_extn_sound_trigger_update_device_status(snd_device,
3074 ST_EVENT_SND_DEVICE_FREE);
3075 audio_extn_listen_update_device_status(snd_device,
3076 LISTEN_EVENT_SND_DEVICE_FREE);
3077}
3078
Weiyin Jiangd5718bd2019-09-04 16:52:08 +08003079int audio_extn_utils_get_license_params(
3080 const struct audio_device *adev,
3081 struct audio_license_params *license_params)
Surendar karka30569792018-05-08 12:02:21 +05303082{
3083 if(!license_params)
3084 return -EINVAL;
Weiyin Jiangd5718bd2019-09-04 16:52:08 +08003085
3086 return platform_get_license_by_product(adev->platform,
3087 (const char*)license_params->product, &license_params->key, license_params->license);
Surendar karka30569792018-05-08 12:02:21 +05303088}
3089
Aalique Grahame22e49102018-12-18 14:23:57 -08003090int audio_extn_utils_send_app_type_gain(struct audio_device *adev,
3091 int app_type,
3092 int *gain)
3093{
3094 int gain_cfg[4];
3095 const char *mixer_ctl_name = "App Type Gain";
3096 struct mixer_ctl *ctl;
3097 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
3098 if (!ctl) {
3099 ALOGE("%s: Could not get volume ctl mixer %s", __func__,
3100 mixer_ctl_name);
3101 return -EINVAL;
3102 }
3103 gain_cfg[0] = 0;
3104 gain_cfg[1] = app_type;
3105 gain_cfg[2] = gain[0];
3106 gain_cfg[3] = gain[1];
3107 ALOGV("%s app_type %d l(%d) r(%d)", __func__, app_type, gain[0], gain[1]);
3108 return mixer_ctl_set_array(ctl, gain_cfg,
3109 sizeof(gain_cfg)/sizeof(gain_cfg[0]));
3110}
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -07003111
Weiyin Jiangd5718bd2019-09-04 16:52:08 +08003112static void vndk_fwk_init()
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -07003113{
Weiyin Jiangd5718bd2019-09-04 16:52:08 +08003114 if (mVndkFwk.lib_handle != NULL)
3115 return;
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -07003116
Weiyin Jiangd5718bd2019-09-04 16:52:08 +08003117 mVndkFwk.lib_handle = dlopen(VNDK_FWK_LIB_PATH, RTLD_NOW);
3118 if (mVndkFwk.lib_handle == NULL) {
3119 ALOGW("%s: failed to dlopen VNDK_FWK_LIB %s", __func__, strerror(errno));
3120 return;
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -07003121 }
3122
Weiyin Jiangd5718bd2019-09-04 16:52:08 +08003123 *(void **)(&mVndkFwk.isVendorEnhancedFwk) =
3124 dlsym(mVndkFwk.lib_handle, "isRunningWithVendorEnhancedFramework");
3125 if (mVndkFwk.isVendorEnhancedFwk == NULL) {
3126 ALOGW("%s: dlsym failed %s", __func__, strerror(errno));
3127 if (mVndkFwk.lib_handle) {
3128 dlclose(mVndkFwk.lib_handle);
3129 mVndkFwk.lib_handle = NULL;
3130 }
3131 return;
3132 }
3133
3134
3135 *(void **)(&mVndkFwk.getVendorEnhancedInfo) =
3136 dlsym(mVndkFwk.lib_handle, "getVendorEnhancedInfo");
3137 if (mVndkFwk.getVendorEnhancedInfo == NULL) {
3138 ALOGW("%s: dlsym failed %s", __func__, strerror(errno));
3139 if (mVndkFwk.lib_handle) {
3140 dlclose(mVndkFwk.lib_handle);
3141 mVndkFwk.lib_handle = NULL;
3142 }
3143 }
3144
3145 return;
3146}
3147
3148bool audio_extn_utils_is_vendor_enhanced_fwk()
3149{
3150 static int is_vendor_enhanced_fwk = -EINVAL;
3151 if (is_vendor_enhanced_fwk != -EINVAL)
3152 return (bool)is_vendor_enhanced_fwk;
3153
3154 vndk_fwk_init();
3155
3156 if (mVndkFwk.isVendorEnhancedFwk != NULL) {
3157 is_vendor_enhanced_fwk = mVndkFwk.isVendorEnhancedFwk();
3158 ALOGW("%s: is_vendor_enhanced_fwk %d", __func__, is_vendor_enhanced_fwk);
3159 } else {
3160 is_vendor_enhanced_fwk = 0;
3161 ALOGW("%s: default to non enhanced_fwk config", __func__);
3162 }
3163
3164 return (bool)is_vendor_enhanced_fwk;
3165}
3166
3167int audio_extn_utils_get_vendor_enhanced_info()
3168{
3169 static int vendor_enhanced_info = -EINVAL;
3170 if (vendor_enhanced_info != -EINVAL)
3171 return vendor_enhanced_info;
3172
3173 vndk_fwk_init();
3174
3175 if (mVndkFwk.getVendorEnhancedInfo != NULL) {
3176 vendor_enhanced_info = mVndkFwk.getVendorEnhancedInfo();
3177 ALOGW("%s: vendor_enhanced_info 0x%x", __func__, vendor_enhanced_info);
3178 } else {
3179 vendor_enhanced_info = 0x0;
3180 ALOGW("%s: default to vendor_enhanced_info 0x0", __func__);
3181 }
3182
3183 return vendor_enhanced_info;
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -07003184}
Deeraj Soman14230922019-01-30 16:39:30 +05303185
Deeraj Somanfa377bf2019-02-06 12:57:59 +05303186int audio_extn_utils_get_perf_mode_flag(void)
3187{
3188#ifdef COMPRESSED_PERF_MODE_FLAG
3189 return COMPRESSED_PERF_MODE_FLAG;
3190#else
3191 return 0;
3192#endif
3193}
3194
Deeraj Soman14230922019-01-30 16:39:30 +05303195size_t audio_extn_utils_get_input_buffer_size(uint32_t sample_rate,
3196 audio_format_t format,
3197 int channel_count,
3198 int64_t duration_ms,
3199 bool is_low_latency)
3200{
3201 size_t size = 0;
3202 size_t capture_duration = AUDIO_CAPTURE_PERIOD_DURATION_MSEC;
3203 uint32_t bytes_per_period_sample = 0;
3204
3205
3206 if (audio_extn_utils_check_input_parameters(sample_rate, format, channel_count) != 0)
3207 return 0;
3208
3209 if (duration_ms >= MIN_OFFLOAD_BUFFER_DURATION_MS && duration_ms <= MAX_OFFLOAD_BUFFER_DURATION_MS)
3210 capture_duration = duration_ms;
3211
3212 size = (sample_rate * capture_duration) / 1000;
3213 if (is_low_latency)
3214 size = LOW_LATENCY_CAPTURE_PERIOD_SIZE;
3215
3216
3217 bytes_per_period_sample = audio_bytes_per_sample(format) * channel_count;
3218 size *= bytes_per_period_sample;
3219
3220 /* make sure the size is multiple of 32 bytes and additionally multiple of
3221 * the frame_size (required for 24bit samples and non-power-of-2 channel counts)
3222 * At 48 kHz mono 16-bit PCM:
3223 * 5.000 ms = 240 frames = 15*16*1*2 = 480, a whole multiple of 32 (15)
3224 * 3.333 ms = 160 frames = 10*16*1*2 = 320, a whole multiple of 32 (10)
3225 *
3226 * The loop reaches result within 32 iterations, as initial size is
3227 * already a multiple of frame_size
3228 */
3229 size = audio_extn_utils_nearest_multiple(size, audio_extn_utils_lcm(32, bytes_per_period_sample));
3230
3231 return size;
3232}
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07003233
3234int audio_extn_utils_hash_fn(void *key)
3235{
3236 return (int)key;
3237}
3238
3239bool audio_extn_utils_hash_eq(void *key1, void *key2)
3240{
3241 return (key1 == key2);
3242}