blob: 906a37b73cd9893b7fee28b697f9958e2b8c0b7e [file] [log] [blame]
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001/*
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07002 * Copyright (c) 2014-2020, The Linux Foundation. All rights reserved.
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07003 * Not a Contribution.
4 *
5 * Copyright (C) 2014 The Android Open Source Project
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19
20#define LOG_TAG "audio_hw_utils"
21/* #define LOG_NDEBUG 0 */
22
Manish Dewangan27346042017-03-01 12:56:12 +053023#include <inttypes.h>
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070024#include <errno.h>
25#include <cutils/properties.h>
26#include <cutils/config_utils.h>
27#include <stdlib.h>
28#include <dlfcn.h>
29#include <cutils/str_parms.h>
Aalique Grahame22e49102018-12-18 14:23:57 -080030#include <log/log.h>
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070031#include <cutils/misc.h>
Vinay Vermaaddfa4a2018-04-29 14:03:38 +053032#include <unistd.h>
Surendar Karka287348c2019-04-10 18:31:46 +053033#include <sys/ioctl.h>
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070034
Manish Dewangan07de2142017-02-27 19:27:20 +053035
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070036#include "audio_hw.h"
37#include "platform.h"
38#include "platform_api.h"
39#include "audio_extn.h"
Deeraj Soman14230922019-01-30 16:39:30 +053040#include "voice_extn.h"
Narsinga Rao Chella212e2542014-11-17 19:57:04 -080041#include "voice.h"
Manish Dewangan07de2142017-02-27 19:27:20 +053042#include <sound/compress_params.h>
43#include <sound/compress_offload.h>
Surendar Karka287348c2019-04-10 18:31:46 +053044#include <sound/devdep_params.h>
Manish Dewangan07de2142017-02-27 19:27:20 +053045#include <tinycompress/tinycompress.h>
Revathi Uddaraju1eac8b02017-05-18 17:13:33 +053046
47#ifdef DYNAMIC_LOG_ENABLED
48#include <log_xml_parser.h>
49#define LOG_MASK HAL_MOD_FILE_UTILS
50#include <log_utils.h>
51#endif
52
Ashish Jain81eb2a82015-05-13 10:52:34 +053053#ifdef AUDIO_EXTERNAL_HDMI_ENABLED
Ashish Jain81eb2a82015-05-13 10:52:34 +053054#include "audio_parsers.h"
55#endif
Ashish Jain81eb2a82015-05-13 10:52:34 +053056
Roopesh Nataraja0f68be72020-04-28 12:32:02 -070057#define AUDIO_IO_POLICY_VENDOR_CONFIG_FILE_NAME "audio_io_policy.conf"
58#define AUDIO_OUTPUT_POLICY_VENDOR_CONFIG_FILE_NAME "audio_output_policy.conf"
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070059
60#define OUTPUTS_TAG "outputs"
Dhananjay Kumard6d32152016-10-13 16:11:03 +053061#define INPUTS_TAG "inputs"
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070062
63#define DYNAMIC_VALUE_TAG "dynamic"
64#define FLAGS_TAG "flags"
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +053065#define PROFILES_TAG "profile"
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070066#define FORMATS_TAG "formats"
67#define SAMPLING_RATES_TAG "sampling_rates"
68#define BIT_WIDTH_TAG "bit_width"
69#define APP_TYPE_TAG "app_type"
70
71#define STRING_TO_ENUM(string) { #string, string }
72#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
73
Ben Rombergera04fabc2014-11-14 12:16:03 -080074#define BASE_TABLE_SIZE 64
75#define MAX_BASEINDEX_LEN 256
76
Ben Romberger1aaaf862017-04-06 17:49:46 -070077#ifndef SND_AUDIOCODEC_TRUEHD
78#define SND_AUDIOCODEC_TRUEHD 0x00000023
79#endif
80
Vikram Panduranga93f080e2017-06-07 18:16:14 -070081#define APP_TYPE_VOIP_AUDIO 0x1113A
82
Ashish Jain81eb2a82015-05-13 10:52:34 +053083#ifdef AUDIO_EXTERNAL_HDMI_ENABLED
84#define PROFESSIONAL (1<<0) /* 0 = consumer, 1 = professional */
85#define NON_LPCM (1<<1) /* 0 = audio, 1 = non-audio */
86#define SR_44100 (0<<0) /* 44.1kHz */
87#define SR_NOTID (1<<0) /* non indicated */
88#define SR_48000 (2<<0) /* 48kHz */
89#define SR_32000 (3<<0) /* 32kHz */
90#define SR_22050 (4<<0) /* 22.05kHz */
91#define SR_24000 (6<<0) /* 24kHz */
92#define SR_88200 (8<<0) /* 88.2kHz */
93#define SR_96000 (10<<0) /* 96kHz */
94#define SR_176400 (12<<0) /* 176.4kHz */
95#define SR_192000 (14<<0) /* 192kHz */
96
97#endif
Manish Dewangan07de2142017-02-27 19:27:20 +053098
99/* ToDo: Check and update a proper value in msec */
Weiyin Jiangc49a3b52018-08-17 16:16:08 +0800100#define COMPRESS_OFFLOAD_PLAYBACK_LATENCY 50
101#define PCM_OFFLOAD_PLAYBACK_DSP_PATHDELAY 62
Samyak Jain2546f8a2019-08-29 19:24:21 +0530102#define PCM_OFFLOAD_PLAYBACK_LATENCY PCM_OFFLOAD_PLAYBACK_DSP_PATHDELAY
Manish Dewangan07de2142017-02-27 19:27:20 +0530103
Varun Balaraje49253e2017-07-06 19:48:56 +0530104#ifndef MAX_CHANNELS_SUPPORTED
105#define MAX_CHANNELS_SUPPORTED 8
106#endif
107
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -0700108#ifdef __LP64__
109#define VNDK_FWK_LIB_PATH "/vendor/lib64/libqti_vndfwk_detect.so"
110#else
111#define VNDK_FWK_LIB_PATH "/vendor/lib/libqti_vndfwk_detect.so"
112#endif
113
Weiyin Jiangd5718bd2019-09-04 16:52:08 +0800114typedef struct vndkfwk_s {
115 void *lib_handle;
116 int (*isVendorEnhancedFwk)(void);
117 int (*getVendorEnhancedInfo)(void);
118 const char *lib_name;
119} vndkfwk_t;
120
121static vndkfwk_t mVndkFwk = {
122 NULL, NULL, NULL, VNDK_FWK_LIB_PATH};
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -0700123
`Deeraj Soman676c2702017-09-18 19:25:53 +0530124typedef struct {
125 const char *id_string;
126 const int value;
127} mixer_config_lookup;
128
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700129struct string_to_enum {
130 const char *name;
131 uint32_t value;
132};
133
134const struct string_to_enum s_flag_name_to_enum_table[] = {
135 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_DIRECT),
136 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_PRIMARY),
137 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_FAST),
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800138 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_RAW),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700139 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_DEEP_BUFFER),
140 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD),
141 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_NON_BLOCKING),
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700142 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_HW_AV_SYNC),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700143 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_INCALL_MUSIC),
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700144 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH),
Naresh Tanniruee3499a2017-01-05 14:05:35 +0530145 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_TIMESTAMP),
Vikram Panduranga93f080e2017-06-07 18:16:14 -0700146 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_VOIP_RX),
Ben Romberger6c4d3812017-06-13 17:46:45 -0700147 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_BD),
Varun Balaraje49253e2017-07-06 19:48:56 +0530148 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_INTERACTIVE),
Derek Chen090dfc52018-03-22 22:15:29 -0400149 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_MEDIA),
150 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_SYS_NOTIFICATION),
151 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_NAV_GUIDANCE),
152 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_PHONE),
Derek Chen1bcdc6b2020-02-06 22:44:53 -0800153 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_FRONT_PASSENGER),
Derek Chendf05eea2019-08-01 13:57:49 -0700154 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_REAR_SEAT),
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530155 STRING_TO_ENUM(AUDIO_INPUT_FLAG_NONE),
156 STRING_TO_ENUM(AUDIO_INPUT_FLAG_FAST),
157 STRING_TO_ENUM(AUDIO_INPUT_FLAG_HW_HOTWORD),
158 STRING_TO_ENUM(AUDIO_INPUT_FLAG_RAW),
159 STRING_TO_ENUM(AUDIO_INPUT_FLAG_SYNC),
Dhananjay Kumaree4d2002016-10-25 18:02:58 +0530160 STRING_TO_ENUM(AUDIO_INPUT_FLAG_TIMESTAMP),
Dhananjay Kumar704ce6f2017-09-28 22:08:00 +0530161 STRING_TO_ENUM(AUDIO_INPUT_FLAG_COMPRESS),
Ralf Herzaec80262018-07-03 07:08:49 +0200162 STRING_TO_ENUM(AUDIO_INPUT_FLAG_PASSTHROUGH),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700163};
164
165const struct string_to_enum s_format_name_to_enum_table[] = {
Ashish Jain83a6cc22016-06-28 14:34:17 +0530166 STRING_TO_ENUM(AUDIO_FORMAT_PCM_8_BIT),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700167 STRING_TO_ENUM(AUDIO_FORMAT_PCM_16_BIT),
Ashish Jain5106d362016-05-11 19:23:33 +0530168 STRING_TO_ENUM(AUDIO_FORMAT_PCM_24_BIT_PACKED),
169 STRING_TO_ENUM(AUDIO_FORMAT_PCM_8_24_BIT),
Ashish Jainf1eaa582016-05-23 20:54:24 +0530170 STRING_TO_ENUM(AUDIO_FORMAT_PCM_32_BIT),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700171 STRING_TO_ENUM(AUDIO_FORMAT_MP3),
172 STRING_TO_ENUM(AUDIO_FORMAT_AAC),
173 STRING_TO_ENUM(AUDIO_FORMAT_VORBIS),
Mingming Yinae3530f2014-07-03 16:50:18 -0700174 STRING_TO_ENUM(AUDIO_FORMAT_AMR_NB),
175 STRING_TO_ENUM(AUDIO_FORMAT_AMR_WB),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700176 STRING_TO_ENUM(AUDIO_FORMAT_AC3),
Mingming Yinae3530f2014-07-03 16:50:18 -0700177 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700178 STRING_TO_ENUM(AUDIO_FORMAT_DTS),
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530179 STRING_TO_ENUM(AUDIO_FORMAT_DTS_HD),
Ben Romberger1aaaf862017-04-06 17:49:46 -0700180 STRING_TO_ENUM(AUDIO_FORMAT_DOLBY_TRUEHD),
Naresh Tanniru928f0862017-04-07 16:44:23 -0700181 STRING_TO_ENUM(AUDIO_FORMAT_IEC61937),
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530182 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3_JOC),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700183 STRING_TO_ENUM(AUDIO_FORMAT_WMA),
184 STRING_TO_ENUM(AUDIO_FORMAT_WMA_PRO),
185 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADIF),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700186 STRING_TO_ENUM(AUDIO_FORMAT_AMR_WB_PLUS),
187 STRING_TO_ENUM(AUDIO_FORMAT_EVRC),
188 STRING_TO_ENUM(AUDIO_FORMAT_EVRCB),
189 STRING_TO_ENUM(AUDIO_FORMAT_EVRCWB),
190 STRING_TO_ENUM(AUDIO_FORMAT_QCELP),
191 STRING_TO_ENUM(AUDIO_FORMAT_MP2),
192 STRING_TO_ENUM(AUDIO_FORMAT_EVRCNW),
Amit Shekhar6f461b12014-08-01 14:52:58 -0700193 STRING_TO_ENUM(AUDIO_FORMAT_FLAC),
Satya Krishna Pindiproli70471602015-04-24 19:12:43 +0530194 STRING_TO_ENUM(AUDIO_FORMAT_ALAC),
195 STRING_TO_ENUM(AUDIO_FORMAT_APE),
Alexy Josephcd8eaed2014-12-11 12:46:53 -0800196 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LC),
197 STRING_TO_ENUM(AUDIO_FORMAT_AAC_HE_V1),
198 STRING_TO_ENUM(AUDIO_FORMAT_AAC_HE_V2),
Manish Dewangana6fc5442015-08-24 20:30:31 +0530199 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS),
Ashish Jaine513a872015-11-19 17:00:56 +0530200 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_LC),
201 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_HE_V1),
202 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_HE_V2),
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +0530203 STRING_TO_ENUM(AUDIO_FORMAT_DSD),
Arun Kumar Dasari3b174182016-12-27 13:01:14 +0530204 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM),
205 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM_LC),
206 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM_HE_V1),
207 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM_HE_V2),
Dhanalakshmi Siddani18737932016-11-29 17:33:17 +0530208 STRING_TO_ENUM(AUDIO_FORMAT_APTX),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700209};
210
Manish Dewangan3ccdea52017-02-13 19:31:54 +0530211/* payload structure avt_device drift query */
212struct audio_avt_device_drift_stats {
213 uint32_t minor_version;
Manish Dewangan338c50a2017-09-12 15:22:03 +0530214
Manish Dewangan3ccdea52017-02-13 19:31:54 +0530215 /* Indicates the device interface direction as either
216 * source (Tx) or sink (Rx).
217 */
218 uint16_t device_direction;
Manish Dewangan338c50a2017-09-12 15:22:03 +0530219
220 /* Reference timer for drift accumulation and time stamp information.
221 * currently it only support AFE_REF_TIMER_TYPE_AVTIMER
222 */
223 uint16_t reference_timer;
Manish Dewangan3ccdea52017-02-13 19:31:54 +0530224 struct audio_avt_device_drift_param drift_param;
Manish Dewangan338c50a2017-09-12 15:22:03 +0530225} __attribute__((packed));
Manish Dewangan3ccdea52017-02-13 19:31:54 +0530226
Ben Rombergera04fabc2014-11-14 12:16:03 -0800227static char bTable[BASE_TABLE_SIZE] = {
228 'A','B','C','D','E','F','G','H','I','J','K','L',
229 'M','N','O','P','Q','R','S','T','U','V','W','X',
230 'Y','Z','a','b','c','d','e','f','g','h','i','j',
231 'k','l','m','n','o','p','q','r','s','t','u','v',
232 'w','x','y','z','0','1','2','3','4','5','6','7',
233 '8','9','+','/'
234};
235
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700236static uint32_t string_to_enum(const struct string_to_enum *table, size_t size,
237 const char *name)
238{
239 size_t i;
240 for (i = 0; i < size; i++) {
241 if (strcmp(table[i].name, name) == 0) {
242 ALOGV("%s found %s", __func__, table[i].name);
243 return table[i].value;
244 }
245 }
246 return 0;
247}
248
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530249static audio_io_flags_t parse_flag_names(char *name)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700250{
251 uint32_t flag = 0;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530252 audio_io_flags_t io_flags;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800253 char *last_r;
254 char *flag_name = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700255 while (flag_name != NULL) {
256 if (strlen(flag_name) != 0) {
257 flag |= string_to_enum(s_flag_name_to_enum_table,
258 ARRAY_SIZE(s_flag_name_to_enum_table),
259 flag_name);
260 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800261 flag_name = strtok_r(NULL, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700262 }
263
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800264 ALOGV("parse_flag_names: flag - %x", flag);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530265 io_flags.in_flags = (audio_input_flags_t)flag;
266 io_flags.out_flags = (audio_output_flags_t)flag;
267 return io_flags;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700268}
269
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530270static void parse_format_names(char *name, struct streams_io_cfg *s_info)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700271{
272 struct stream_format *sf_info = NULL;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800273 char *last_r;
274 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700275
276 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG) == 0)
277 return;
278
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530279 list_init(&s_info->format_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700280 while (str != NULL) {
281 audio_format_t format = (audio_format_t)string_to_enum(s_format_name_to_enum_table,
282 ARRAY_SIZE(s_format_name_to_enum_table), str);
283 ALOGV("%s: format - %d", __func__, format);
284 if (format != 0) {
285 sf_info = (struct stream_format *)calloc(1, sizeof(struct stream_format));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700286 if (sf_info == NULL)
287 break; /* return whatever was parsed */
288
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700289 sf_info->format = format;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530290 list_add_tail(&s_info->format_list, &sf_info->list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700291 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800292 str = strtok_r(NULL, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700293 }
294}
295
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530296static void parse_sample_rate_names(char *name, struct streams_io_cfg *s_info)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700297{
Amit Shekhar6f461b12014-08-01 14:52:58 -0700298 struct stream_sample_rate *ss_info = NULL;
299 uint32_t sample_rate = 48000;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800300 char *last_r;
301 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700302
Amit Shekhar6f461b12014-08-01 14:52:58 -0700303 if (str != NULL && 0 == strcmp(str, DYNAMIC_VALUE_TAG))
304 return;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700305
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530306 list_init(&s_info->sample_rate_list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700307 while (str != NULL) {
308 sample_rate = (uint32_t)strtol(str, (char **)NULL, 10);
309 ALOGV("%s: sample_rate - %d", __func__, sample_rate);
310 if (0 != sample_rate) {
311 ss_info = (struct stream_sample_rate *)calloc(1, sizeof(struct stream_sample_rate));
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800312 if (!ss_info) {
313 ALOGE("%s: memory allocation failure", __func__);
314 return;
315 }
Amit Shekhar6f461b12014-08-01 14:52:58 -0700316 ss_info->sample_rate = sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530317 list_add_tail(&s_info->sample_rate_list, &ss_info->list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700318 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800319 str = strtok_r(NULL, "|", &last_r);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700320 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700321}
322
323static int parse_bit_width_names(char *name)
324{
325 int bit_width = 16;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800326 char *last_r;
327 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700328
329 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG))
330 bit_width = (int)strtol(str, (char **)NULL, 10);
331
332 ALOGV("%s: bit_width - %d", __func__, bit_width);
333 return bit_width;
334}
335
336static int parse_app_type_names(void *platform, char *name)
337{
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700338 int app_type = platform_get_default_app_type(platform);
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800339 char *last_r;
340 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700341
342 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG))
343 app_type = (int)strtol(str, (char **)NULL, 10);
344
345 ALOGV("%s: app_type - %d", __func__, app_type);
346 return app_type;
347}
348
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530349static void update_streams_cfg_list(cnode *root, void *platform,
350 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700351{
352 cnode *node = root->first_child;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530353 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700354
355 ALOGV("%s", __func__);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530356 s_info = (struct streams_io_cfg *)calloc(1, sizeof(struct streams_io_cfg));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700357
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530358 if (!s_info) {
359 ALOGE("failed to allocate mem for s_info list element");
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700360 return;
361 }
362
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700363 while (node) {
364 if (strcmp(node->name, FLAGS_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530365 s_info->flags = parse_flag_names((char *)node->value);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530366 } else if (strcmp(node->name, PROFILES_TAG) == 0) {
367 strlcpy(s_info->profile, (char *)node->value, sizeof(s_info->profile));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700368 } else if (strcmp(node->name, FORMATS_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530369 parse_format_names((char *)node->value, s_info);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700370 } else if (strcmp(node->name, SAMPLING_RATES_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530371 s_info->app_type_cfg.sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
372 parse_sample_rate_names((char *)node->value, s_info);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700373 } else if (strcmp(node->name, BIT_WIDTH_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530374 s_info->app_type_cfg.bit_width = parse_bit_width_names((char *)node->value);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700375 } else if (strcmp(node->name, APP_TYPE_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530376 s_info->app_type_cfg.app_type = parse_app_type_names(platform, (char *)node->value);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700377 }
378 node = node->next;
379 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530380 list_add_tail(streams_cfg_list, &s_info->list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700381}
382
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530383static void load_cfg_list(cnode *root, void *platform,
384 struct listnode *streams_output_cfg_list,
385 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700386{
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530387 cnode *node = NULL;
388
389 node = config_find(root, OUTPUTS_TAG);
390 if (node != NULL) {
391 node = node->first_child;
392 while (node) {
393 ALOGV("%s: loading output %s", __func__, node->name);
394 update_streams_cfg_list(node, platform, streams_output_cfg_list);
395 node = node->next;
396 }
397 } else {
398 ALOGI("%s: could not load output, node is NULL", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700399 }
400
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530401 node = config_find(root, INPUTS_TAG);
402 if (node != NULL) {
403 node = node->first_child;
404 while (node) {
405 ALOGV("%s: loading input %s", __func__, node->name);
406 update_streams_cfg_list(node, platform, streams_input_cfg_list);
407 node = node->next;
408 }
409 } else {
410 ALOGI("%s: could not load input, node is NULL", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700411 }
412}
413
414static void send_app_type_cfg(void *platform, struct mixer *mixer,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530415 struct listnode *streams_output_cfg_list,
416 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700417{
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530418 size_t app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT] = {0};
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700419 int length = 0, i, num_app_types = 0;
420 struct listnode *node;
421 bool update;
422 struct mixer_ctl *ctl = NULL;
423 const char *mixer_ctl_name = "App Type Config";
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530424 struct streams_io_cfg *s_info = NULL;
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800425 uint32_t target_bit_width = 0;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700426
427 if (!mixer) {
428 ALOGE("%s: mixer is null",__func__);
429 return;
430 }
431 ctl = mixer_get_ctl_by_name(mixer, mixer_ctl_name);
432 if (!ctl) {
433 ALOGE("%s: Could not get ctl for mixer cmd - %s",__func__, mixer_ctl_name);
434 return;
435 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530436 app_type_cfg[length++] = num_app_types;
437
438 if (list_empty(streams_output_cfg_list)) {
439 app_type_cfg[length++] = platform_get_default_app_type_v2(platform, PCM_PLAYBACK);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700440 app_type_cfg[length++] = 48000;
441 app_type_cfg[length++] = 16;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530442 num_app_types += 1;
443 }
444 if (list_empty(streams_input_cfg_list)) {
445 app_type_cfg[length++] = platform_get_default_app_type_v2(platform, PCM_CAPTURE);
446 app_type_cfg[length++] = 48000;
447 app_type_cfg[length++] = 16;
448 num_app_types += 1;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700449 }
450
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800451 /* get target bit width for ADM enforce mode */
452 target_bit_width = adev_get_dsp_bit_width_enforce_mode();
453
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700454 list_for_each(node, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530455 s_info = node_to_item(node, struct streams_io_cfg, list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700456 update = true;
457 for (i=0; i<length; i=i+3) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530458 if (app_type_cfg[i+1] == 0)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700459 break;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530460 else if (app_type_cfg[i+1] == (size_t)s_info->app_type_cfg.app_type) {
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530461 if (app_type_cfg[i+2] < (size_t)s_info->app_type_cfg.sample_rate)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530462 app_type_cfg[i+2] = s_info->app_type_cfg.sample_rate;
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530463 if (app_type_cfg[i+3] < (size_t)s_info->app_type_cfg.bit_width)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530464 app_type_cfg[i+3] = s_info->app_type_cfg.bit_width;
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800465 /* ADM bit width = max(enforce_bit_width, bit_width from s_info */
466 if (audio_extn_is_dsp_bit_width_enforce_mode_supported(s_info->flags.out_flags) &&
467 (target_bit_width > app_type_cfg[i+3]))
468 app_type_cfg[i+3] = target_bit_width;
469
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700470 update = false;
471 break;
472 }
473 }
474 if (update && ((length + 3) <= MAX_LENGTH_MIXER_CONTROL_IN_INT)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530475 num_app_types += 1;
476 app_type_cfg[length++] = s_info->app_type_cfg.app_type;
477 app_type_cfg[length++] = s_info->app_type_cfg.sample_rate;
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800478 app_type_cfg[length] = s_info->app_type_cfg.bit_width;
479 if (audio_extn_is_dsp_bit_width_enforce_mode_supported(s_info->flags.out_flags) &&
480 (target_bit_width > app_type_cfg[length]))
481 app_type_cfg[length] = target_bit_width;
482
483 length++;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530484 }
485 }
486 list_for_each(node, streams_input_cfg_list) {
487 s_info = node_to_item(node, struct streams_io_cfg, list);
488 update = true;
489 for (i=0; i<length; i=i+3) {
490 if (app_type_cfg[i+1] == 0)
491 break;
492 else if (app_type_cfg[i+1] == (size_t)s_info->app_type_cfg.app_type) {
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530493 if (app_type_cfg[i+2] < (size_t)s_info->app_type_cfg.sample_rate)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530494 app_type_cfg[i+2] = s_info->app_type_cfg.sample_rate;
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530495 if (app_type_cfg[i+3] < (size_t)s_info->app_type_cfg.bit_width)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530496 app_type_cfg[i+3] = s_info->app_type_cfg.bit_width;
497 update = false;
498 break;
499 }
500 }
501 if (update && ((length + 3) <= MAX_LENGTH_MIXER_CONTROL_IN_INT)) {
502 num_app_types += 1;
503 app_type_cfg[length++] = s_info->app_type_cfg.app_type;
504 app_type_cfg[length++] = s_info->app_type_cfg.sample_rate;
505 app_type_cfg[length++] = s_info->app_type_cfg.bit_width;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700506 }
507 }
508 ALOGV("%s: num_app_types: %d", __func__, num_app_types);
509 if (num_app_types) {
510 app_type_cfg[0] = num_app_types;
511 mixer_ctl_set_array(ctl, app_type_cfg, length);
512 }
513}
514
Roopesh Nataraja0f68be72020-04-28 12:32:02 -0700515/* Function to retrieve audio vendor configs path */
516void audio_get_vendor_config_path (char* config_file_path, int path_size)
517{
518 char vendor_sku[PROPERTY_VALUE_MAX] = {'\0'};
519 if (property_get("ro.boot.product.vendor.sku", vendor_sku, "") <= 0) {
520#ifdef LINUX_ENABLED
521 /* Audio configs are stored in /etc */
522 snprintf(config_file_path, path_size, "%s", "/etc");
523#else
524 /* Audio configs are stored in /vendor/etc */
525 snprintf(config_file_path, path_size, "%s", "/vendor/etc");
526#endif
527 } else {
528 /* Audio configs are stored in /vendor/etc/audio/sku_${vendor_sku} */
529 snprintf(config_file_path, path_size,
530 "%s%s", "/vendor/etc/audio/sku_", vendor_sku);
531 }
532}
533
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530534void audio_extn_utils_update_streams_cfg_lists(void *platform,
535 struct mixer *mixer,
536 struct listnode *streams_output_cfg_list,
537 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700538{
539 cnode *root;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530540 char *data = NULL;
Roopesh Nataraja0f68be72020-04-28 12:32:02 -0700541 char vendor_config_path[VENDOR_CONFIG_PATH_MAX_LENGTH];
542 char audio_io_policy_file[VENDOR_CONFIG_FILE_MAX_LENGTH];
543 char audio_output_policy_file[VENDOR_CONFIG_FILE_MAX_LENGTH];
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700544
545 ALOGV("%s", __func__);
546 list_init(streams_output_cfg_list);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530547 list_init(streams_input_cfg_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700548
549 root = config_node("", "");
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700550 if (root == NULL) {
551 ALOGE("cfg_list, NULL config root");
552 return;
553 }
554
Roopesh Nataraja0f68be72020-04-28 12:32:02 -0700555 /* Get path for audio configuration files in vendor */
556 audio_get_vendor_config_path(vendor_config_path,
557 sizeof(vendor_config_path));
558
559 /* Get path for audio_io_policy_file in vendor */
560 snprintf(audio_io_policy_file, sizeof(audio_io_policy_file),
561 "%s/%s", vendor_config_path, AUDIO_IO_POLICY_VENDOR_CONFIG_FILE_NAME);
562
563 /* Load audio_io_policy_file from vendor */
564 data = (char *)load_file(audio_io_policy_file, NULL);
565
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530566 if (data == NULL) {
567 ALOGD("%s: failed to open io config file(%s), trying older config file",
Roopesh Nataraja0f68be72020-04-28 12:32:02 -0700568 __func__, audio_io_policy_file);
569
570 /* Get path for audio_output_policy_file in vendor */
571 snprintf(audio_output_policy_file, sizeof(audio_output_policy_file),
572 "%s/%s", vendor_config_path,
573 AUDIO_OUTPUT_POLICY_VENDOR_CONFIG_FILE_NAME);
574
575 /* Load audio_output_policy_file from vendor */
576 data = (char *)load_file(audio_output_policy_file, NULL);
577
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530578 if (data == NULL) {
579 send_app_type_cfg(platform, mixer,
580 streams_output_cfg_list,
581 streams_input_cfg_list);
582 ALOGE("%s: could not load io policy config!", __func__);
yidongh6eb4f752017-04-19 18:20:57 +0800583 free(root);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530584 return;
585 }
586 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700587
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530588 config_load(root, data);
589 load_cfg_list(root, platform, streams_output_cfg_list,
590 streams_input_cfg_list);
591
592 send_app_type_cfg(platform, mixer, streams_output_cfg_list,
593 streams_input_cfg_list);
Alexy Josephaee4fdd2016-01-29 13:02:07 -0800594
595 config_free(root);
yidongh6eb4f752017-04-19 18:20:57 +0800596 free(root);
Alexy Josephaee4fdd2016-01-29 13:02:07 -0800597 free(data);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700598}
599
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530600static void audio_extn_utils_dump_streams_cfg_list(
601 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700602{
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700603 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530604 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700605 struct stream_format *sf_info;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700606 struct stream_sample_rate *ss_info;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530607
608 list_for_each(node_i, streams_cfg_list) {
609 s_info = node_to_item(node_i, struct streams_io_cfg, list);
610 ALOGV("%s: flags-%d, sample_rate-%d, bit_width-%d, app_type-%d",
611 __func__, s_info->flags.out_flags, s_info->app_type_cfg.sample_rate,
612 s_info->app_type_cfg.bit_width, s_info->app_type_cfg.app_type);
613 list_for_each(node_j, &s_info->format_list) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700614 sf_info = node_to_item(node_j, struct stream_format, list);
615 ALOGV("format-%x", sf_info->format);
616 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530617 list_for_each(node_j, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700618 ss_info = node_to_item(node_j, struct stream_sample_rate, list);
619 ALOGV("sample rate-%d", ss_info->sample_rate);
620 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700621 }
622}
623
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530624void audio_extn_utils_dump_streams_cfg_lists(
625 struct listnode *streams_output_cfg_list,
626 struct listnode *streams_input_cfg_list)
627{
628 ALOGV("%s", __func__);
629 audio_extn_utils_dump_streams_cfg_list(streams_output_cfg_list);
630 audio_extn_utils_dump_streams_cfg_list(streams_input_cfg_list);
631}
632
633static void audio_extn_utils_release_streams_cfg_list(
634 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700635{
636 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530637 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700638
639 ALOGV("%s", __func__);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530640
641 while (!list_empty(streams_cfg_list)) {
642 node_i = list_head(streams_cfg_list);
643 s_info = node_to_item(node_i, struct streams_io_cfg, list);
644 while (!list_empty(&s_info->format_list)) {
645 node_j = list_head(&s_info->format_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700646 list_remove(node_j);
647 free(node_to_item(node_j, struct stream_format, list));
648 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530649 while (!list_empty(&s_info->sample_rate_list)) {
650 node_j = list_head(&s_info->sample_rate_list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700651 list_remove(node_j);
652 free(node_to_item(node_j, struct stream_sample_rate, list));
653 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700654 list_remove(node_i);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530655 free(node_to_item(node_i, struct streams_io_cfg, list));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700656 }
657}
658
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530659void audio_extn_utils_release_streams_cfg_lists(
660 struct listnode *streams_output_cfg_list,
661 struct listnode *streams_input_cfg_list)
662{
663 ALOGV("%s", __func__);
664 audio_extn_utils_release_streams_cfg_list(streams_output_cfg_list);
665 audio_extn_utils_release_streams_cfg_list(streams_input_cfg_list);
666}
667
668static bool set_app_type_cfg(struct streams_io_cfg *s_info,
669 struct stream_app_type_cfg *app_type_cfg,
670 uint32_t sample_rate, uint32_t bit_width)
Amit Shekhar6f461b12014-08-01 14:52:58 -0700671 {
672 struct listnode *node_i;
673 struct stream_sample_rate *ss_info;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530674 list_for_each(node_i, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700675 ss_info = node_to_item(node_i, struct stream_sample_rate, list);
676 if ((sample_rate <= ss_info->sample_rate) &&
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530677 (bit_width == s_info->app_type_cfg.bit_width)) {
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700678
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530679 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700680 app_type_cfg->sample_rate = ss_info->sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530681 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700682 ALOGV("%s app_type_cfg->app_type %d, app_type_cfg->sample_rate %d, app_type_cfg->bit_width %d",
683 __func__, app_type_cfg->app_type, app_type_cfg->sample_rate, app_type_cfg->bit_width);
684 return true;
685 }
686 }
687 /*
688 * Reiterate through the list assuming dafault sample rate.
689 * Handles scenario where input sample rate is higher
690 * than all sample rates in list for the input bit width.
691 */
692 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
Apoorv Raghuvanshif59bb222015-02-18 12:23:23 -0800693
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530694 list_for_each(node_i, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700695 ss_info = node_to_item(node_i, struct stream_sample_rate, list);
696 if ((sample_rate <= ss_info->sample_rate) &&
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530697 (bit_width == s_info->app_type_cfg.bit_width)) {
698 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700699 app_type_cfg->sample_rate = sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530700 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Apoorv Raghuvanshif59bb222015-02-18 12:23:23 -0800701 ALOGV("%s Assuming sample rate. app_type_cfg->app_type %d, app_type_cfg->sample_rate %d, app_type_cfg->bit_width %d",
Amit Shekhar6f461b12014-08-01 14:52:58 -0700702 __func__, app_type_cfg->app_type, app_type_cfg->sample_rate, app_type_cfg->bit_width);
703 return true;
704 }
705 }
706 return false;
707}
708
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530709void audio_extn_utils_update_stream_input_app_type_cfg(void *platform,
710 struct listnode *streams_input_cfg_list,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -0800711 struct listnode *devices __unused,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530712 audio_input_flags_t flags,
713 audio_format_t format,
714 uint32_t sample_rate,
715 uint32_t bit_width,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530716 char* profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530717 struct stream_app_type_cfg *app_type_cfg)
718{
719 struct listnode *node_i, *node_j;
720 struct streams_io_cfg *s_info;
721 struct stream_format *sf_info;
722
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530723 ALOGV("%s: flags: 0x%x, format: 0x%x sample_rate %d, profile %s",
724 __func__, flags, format, sample_rate, profile);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530725
726 list_for_each(node_i, streams_input_cfg_list) {
727 s_info = node_to_item(node_i, struct streams_io_cfg, list);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530728 /* Along with flags do profile matching if set at either end.*/
729 if (s_info->flags.in_flags == flags &&
730 ((profile[0] == '\0' && s_info->profile[0] == '\0') ||
731 strncmp(s_info->profile, profile, sizeof(s_info->profile)) == 0)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530732 list_for_each(node_j, &s_info->format_list) {
733 sf_info = node_to_item(node_j, struct stream_format, list);
734 if (sf_info->format == format) {
735 if (set_app_type_cfg(s_info, app_type_cfg, sample_rate, bit_width))
736 return;
737 }
738 }
739 }
740 }
741 ALOGW("%s: App type could not be selected. Falling back to default", __func__);
742 app_type_cfg->app_type = platform_get_default_app_type_v2(platform, PCM_CAPTURE);
743 app_type_cfg->sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
744 app_type_cfg->bit_width = 16;
745}
746
747void audio_extn_utils_update_stream_output_app_type_cfg(void *platform,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700748 struct listnode *streams_output_cfg_list,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -0800749 struct listnode *devices,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700750 audio_output_flags_t flags,
751 audio_format_t format,
Amit Shekhar6f461b12014-08-01 14:52:58 -0700752 uint32_t sample_rate,
753 uint32_t bit_width,
Manish Dewangan837dc462015-05-27 10:17:41 +0530754 audio_channel_mask_t channel_mask,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530755 char *profile,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700756 struct stream_app_type_cfg *app_type_cfg)
757{
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +0530758 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530759 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700760 struct stream_format *sf_info;
Manish Dewangan837dc462015-05-27 10:17:41 +0530761 char value[PROPERTY_VALUE_MAX] = {0};
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700762
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -0800763 if (compare_device_type(devices, AUDIO_DEVICE_OUT_SPEAKER)) {
Ramu Gottipati074fa4d2018-09-11 20:05:51 +0530764 int bw = platform_get_snd_device_bit_width(SND_DEVICE_OUT_SPEAKER);
765 if ((-ENOSYS != bw) && (bit_width > (uint32_t)bw))
Amit Shekhar5a39c912014-10-14 15:39:30 -0700766 bit_width = (uint32_t)bw;
Ramu Gottipati074fa4d2018-09-11 20:05:51 +0530767 else if (-ENOSYS == bw)
768 bit_width = CODEC_BACKEND_DEFAULT_BIT_WIDTH;
Amit Shekhar1d896042014-10-03 13:16:09 -0700769 sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Aniket Kumar Latad13758f2020-08-06 15:11:36 -0700770 ALOGV("%s Allowing 24 and above bits playback on speaker \
771 ONLY at default sampling rate", __func__);
Amit Shekhar1d896042014-10-03 13:16:09 -0700772 }
773
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -0700774 property_get("vendor.audio.playback.mch.downsample",value,"");
Manish Dewangan837dc462015-05-27 10:17:41 +0530775 if (!strncmp("true", value, sizeof("true"))) {
776 if ((popcount(channel_mask) > 2) &&
777 (sample_rate > CODEC_BACKEND_DEFAULT_SAMPLE_RATE) &&
Aalique Grahame5cd12ff2017-10-19 10:57:00 -0700778 !(flags & (audio_output_flags_t)AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH)) {
Manish Dewangan837dc462015-05-27 10:17:41 +0530779 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
780 ALOGD("%s: MCH session defaulting sample rate to %d",
781 __func__, sample_rate);
782 }
783 }
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +0530784
785 /* Set sampling rate to 176.4 for DSD64
786 * and 352.8Khz for DSD128.
787 * Set Bit Width to 16. output will be 16 bit
788 * post DoP in ASM.
789 */
Aalique Grahame5cd12ff2017-10-19 10:57:00 -0700790 if ((flags & (audio_output_flags_t)AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH) &&
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +0530791 (format == AUDIO_FORMAT_DSD)) {
792 bit_width = 16;
793 if (sample_rate == INPUT_SAMPLING_RATE_DSD64)
794 sample_rate = OUTPUT_SAMPLING_RATE_DSD64;
795 else if (sample_rate == INPUT_SAMPLING_RATE_DSD128)
796 sample_rate = OUTPUT_SAMPLING_RATE_DSD128;
797 }
798
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +0530799
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800800 ALOGV("%s: flags: %x, format: %x sample_rate %d, profile %s, app_type %d",
801 __func__, flags, format, sample_rate, profile, app_type_cfg->app_type);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700802 list_for_each(node_i, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530803 s_info = node_to_item(node_i, struct streams_io_cfg, list);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530804 /* Along with flags do profile matching if set at either end.*/
805 if (s_info->flags.out_flags == flags &&
806 ((profile[0] == '\0' && s_info->profile[0] == '\0') ||
807 strncmp(s_info->profile, profile, sizeof(s_info->profile)) == 0)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530808 list_for_each(node_j, &s_info->format_list) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700809 sf_info = node_to_item(node_j, struct stream_format, list);
810 if (sf_info->format == format) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530811 if (set_app_type_cfg(s_info, app_type_cfg, sample_rate, bit_width))
Amit Shekhar6f461b12014-08-01 14:52:58 -0700812 return;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700813 }
814 }
815 }
816 }
817 list_for_each(node_i, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530818 s_info = node_to_item(node_i, struct streams_io_cfg, list);
819 if (s_info->flags.out_flags == AUDIO_OUTPUT_FLAG_PRIMARY) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700820 ALOGV("Compatible output profile not found.");
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530821 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
822 app_type_cfg->sample_rate = s_info->app_type_cfg.sample_rate;
823 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700824 ALOGV("%s Default to primary output: App type: %d sample_rate %d",
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530825 __func__, s_info->app_type_cfg.app_type, app_type_cfg->sample_rate);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700826 return;
827 }
828 }
829 ALOGW("%s: App type could not be selected. Falling back to default", __func__);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700830 app_type_cfg->app_type = platform_get_default_app_type(platform);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700831 app_type_cfg->sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700832 app_type_cfg->bit_width = 16;
833}
834
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +0530835static bool audio_is_this_native_usecase(struct audio_usecase *uc)
836{
837 bool native_usecase = false;
838 struct stream_out *out = (struct stream_out*) uc->stream.out;
839
840 if (PCM_PLAYBACK == uc->type && out != NULL &&
841 NATIVE_AUDIO_MODE_INVALID != platform_get_native_support() &&
842 is_offload_usecase(uc->id) &&
843 (out->sample_rate == OUTPUT_SAMPLING_RATE_44100))
844 native_usecase = true;
845
846 return native_usecase;
847}
848
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800849bool audio_extn_is_dsp_bit_width_enforce_mode_supported(audio_output_flags_t flags)
850{
851 /* DSP bitwidth enforce mode for ADM and AFE:
852 * includes:
853 * deep buffer, low latency, direct pcm and offload.
854 * excludes:
855 * ull(raw+fast), VOIP.
856 */
857 if ((flags & AUDIO_OUTPUT_FLAG_VOIP_RX) ||
858 ((flags & AUDIO_OUTPUT_FLAG_RAW) &&
859 (flags & AUDIO_OUTPUT_FLAG_FAST)))
860 return false;
861
862
863 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) ||
864 (flags & AUDIO_OUTPUT_FLAG_DIRECT) ||
865 (flags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) ||
866 (flags & AUDIO_OUTPUT_FLAG_PRIMARY))
867 return true;
868 else
869 return false;
870}
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800871
872static inline bool audio_is_vr_mode_on(struct audio_device *(__attribute__((unused)) adev))
873{
874 return adev->vr_audio_mode_enabled;
875}
876
Derek Chenf7092792017-05-23 12:23:53 -0400877static void audio_extn_btsco_get_sample_rate(int snd_device, int *sample_rate)
878{
879 switch (snd_device) {
880 case SND_DEVICE_OUT_BT_SCO:
881 case SND_DEVICE_IN_BT_SCO_MIC:
882 case SND_DEVICE_IN_BT_SCO_MIC_NREC:
883 *sample_rate = 8000;
884 break;
885 case SND_DEVICE_OUT_BT_SCO_WB:
886 case SND_DEVICE_IN_BT_SCO_MIC_WB:
887 case SND_DEVICE_IN_BT_SCO_MIC_WB_NREC:
888 *sample_rate = 16000;
889 break;
890 default:
Aniket Kumar Latad13758f2020-08-06 15:11:36 -0700891 ALOGV("%s:Not a BT SCO device, need not update sampling rate\n", __func__);
Derek Chenf7092792017-05-23 12:23:53 -0400892 break;
893 }
894}
895
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530896void audio_extn_utils_update_stream_app_type_cfg_for_usecase(
897 struct audio_device *adev,
898 struct audio_usecase *usecase)
899{
900 ALOGV("%s", __func__);
901
902 switch(usecase->type) {
903 case PCM_PLAYBACK:
904 audio_extn_utils_update_stream_output_app_type_cfg(adev->platform,
905 &adev->streams_output_cfg_list,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -0800906 &usecase->stream.out->device_list,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530907 usecase->stream.out->flags,
Aalique Grahame65780b52017-09-27 14:59:56 -0700908 usecase->stream.out->hal_op_format,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530909 usecase->stream.out->sample_rate,
910 usecase->stream.out->bit_width,
911 usecase->stream.out->channel_mask,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530912 usecase->stream.out->profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530913 &usecase->stream.out->app_type_cfg);
914 ALOGV("%s Selected apptype: %d", __func__, usecase->stream.out->app_type_cfg.app_type);
915 break;
916 case PCM_CAPTURE:
Revathi Uddarajud9f23d92020-07-27 10:55:06 +0530917 if (usecase->id == USECASE_AUDIO_RECORD_VOIP
918 || usecase->id == USECASE_AUDIO_RECORD_VOIP_LOW_LATENCY)
Vikram Panduranga93f080e2017-06-07 18:16:14 -0700919 usecase->stream.in->app_type_cfg.app_type = APP_TYPE_VOIP_AUDIO;
920 else
921 audio_extn_utils_update_stream_input_app_type_cfg(adev->platform,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530922 &adev->streams_input_cfg_list,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -0800923 &usecase->stream.in->device_list,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530924 usecase->stream.in->flags,
925 usecase->stream.in->format,
926 usecase->stream.in->sample_rate,
927 usecase->stream.in->bit_width,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530928 usecase->stream.in->profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530929 &usecase->stream.in->app_type_cfg);
930 ALOGV("%s Selected apptype: %d", __func__, usecase->stream.in->app_type_cfg.app_type);
931 break;
Surendar Karka93cd25a2018-08-28 14:21:37 +0530932 case TRANSCODE_LOOPBACK_RX :
Siddartha Shaik343abc62017-08-08 11:15:25 +0530933 audio_extn_utils_update_stream_output_app_type_cfg(adev->platform,
934 &adev->streams_output_cfg_list,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -0800935 &usecase->stream.inout->out_config.device_list,
Siddartha Shaik343abc62017-08-08 11:15:25 +0530936 0,
937 usecase->stream.inout->out_config.format,
938 usecase->stream.inout->out_config.sample_rate,
939 usecase->stream.inout->out_config.bit_width,
940 usecase->stream.inout->out_config.channel_mask,
941 usecase->stream.inout->profile,
942 &usecase->stream.inout->out_app_type_cfg);
943 ALOGV("%s Selected apptype: %d", __func__, usecase->stream.inout->out_app_type_cfg.app_type);
944 break;
Derek Chenf7092792017-05-23 12:23:53 -0400945 case PCM_HFP_CALL:
946 switch (usecase->id) {
947 case USECASE_AUDIO_HFP_SCO:
948 case USECASE_AUDIO_HFP_SCO_WB:
949 audio_extn_btsco_get_sample_rate(usecase->out_snd_device,
950 &usecase->out_app_type_cfg.sample_rate);
Derek Chend7307012019-11-12 12:10:41 -0500951 usecase->in_app_type_cfg.sample_rate = usecase->out_app_type_cfg.sample_rate;
Derek Chenf7092792017-05-23 12:23:53 -0400952 break;
953 case USECASE_AUDIO_HFP_SCO_DOWNLINK:
954 case USECASE_AUDIO_HFP_SCO_WB_DOWNLINK:
955 audio_extn_btsco_get_sample_rate(usecase->in_snd_device,
956 &usecase->in_app_type_cfg.sample_rate);
Derek Chend7307012019-11-12 12:10:41 -0500957 usecase->out_app_type_cfg.sample_rate = usecase->in_app_type_cfg.sample_rate;
Derek Chenf7092792017-05-23 12:23:53 -0400958 break;
959 default:
960 ALOGE("%s: usecase id (%d) not supported, use default sample rate",
961 __func__, usecase->id);
962 usecase->in_app_type_cfg.sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
963 usecase->out_app_type_cfg.sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
964 break;
965 }
966 /* update out_app_type_cfg */
967 usecase->out_app_type_cfg.bit_width =
968 platform_get_snd_device_bit_width(usecase->out_snd_device);
969 usecase->out_app_type_cfg.app_type =
970 platform_get_default_app_type_v2(adev->platform, PCM_PLAYBACK);
971 /* update in_app_type_cfg */
972 usecase->in_app_type_cfg.bit_width =
973 platform_get_snd_device_bit_width(usecase->in_snd_device);
974 usecase->in_app_type_cfg.app_type =
975 platform_get_default_app_type_v2(adev->platform, PCM_CAPTURE);
976 ALOGV("%s Selected apptype: playback %d capture %d",
977 __func__, usecase->out_app_type_cfg.app_type, usecase->in_app_type_cfg.app_type);
978 break;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530979 default:
980 ALOGE("%s: app type cfg not supported for usecase type (%d)",
981 __func__, usecase->type);
982 }
983}
984
Aalique Grahame22e49102018-12-18 14:23:57 -0800985static int set_stream_app_type_mixer_ctrl(struct audio_device *adev,
986 int pcm_device_id, int app_type,
987 int acdb_dev_id, int sample_rate,
988 int stream_type,
989 snd_device_t snd_device)
990{
991
992 char mixer_ctl_name[MAX_LENGTH_MIXER_CONTROL_IN_INT];
993 struct mixer_ctl *ctl;
994 int app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT], len = 0, rc = 0;
995 int snd_device_be_idx = -1;
996
997 if (stream_type == PCM_PLAYBACK) {
998 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
999 "Audio Stream %d App Type Cfg", pcm_device_id);
1000 } else if (stream_type == PCM_CAPTURE) {
1001 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
1002 "Audio Stream Capture %d App Type Cfg", pcm_device_id);
1003 }
1004
1005 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
1006 if (!ctl) {
1007 ALOGE("%s: Could not get ctl for mixer cmd - %s",
1008 __func__, mixer_ctl_name);
1009 rc = -EINVAL;
1010 goto exit;
1011 }
1012 app_type_cfg[len++] = app_type;
1013 app_type_cfg[len++] = acdb_dev_id;
1014 app_type_cfg[len++] = sample_rate;
1015
1016 snd_device_be_idx = platform_get_snd_device_backend_index(snd_device);
1017 if (snd_device_be_idx > 0)
1018 app_type_cfg[len++] = snd_device_be_idx;
1019 ALOGV("%s: stream type %d app_type %d, acdb_dev_id %d "
1020 "sample rate %d, snd_device_be_idx %d",
1021 __func__, stream_type, app_type, acdb_dev_id, sample_rate,
1022 snd_device_be_idx);
1023 mixer_ctl_set_array(ctl, app_type_cfg, len);
1024
1025exit:
1026 return rc;
1027}
1028
1029static int audio_extn_utils_send_app_type_cfg_hfp(struct audio_device *adev,
1030 struct audio_usecase *usecase)
1031{
1032 int pcm_device_id, acdb_dev_id = 0, snd_device = usecase->out_snd_device;
1033 int32_t sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
1034 int app_type = 0, rc = 0;
Derek Chenf7092792017-05-23 12:23:53 -04001035 bool is_bus_dev_usecase = false;
Aalique Grahame22e49102018-12-18 14:23:57 -08001036
1037 ALOGV("%s", __func__);
1038
1039 if (usecase->type != PCM_HFP_CALL) {
Derek Chenf7092792017-05-23 12:23:53 -04001040 ALOGV("%s: not a HFP path, no need to cfg app type", __func__);
Aalique Grahame22e49102018-12-18 14:23:57 -08001041 rc = 0;
1042 goto exit_send_app_type_cfg;
1043 }
1044 if ((usecase->id != USECASE_AUDIO_HFP_SCO) &&
Derek Chenf7092792017-05-23 12:23:53 -04001045 (usecase->id != USECASE_AUDIO_HFP_SCO_WB) &&
1046 (usecase->id != USECASE_AUDIO_HFP_SCO_DOWNLINK) &&
1047 (usecase->id != USECASE_AUDIO_HFP_SCO_WB_DOWNLINK)) {
1048 ALOGV("%s: a usecase where app type cfg is not required", __func__);
Aalique Grahame22e49102018-12-18 14:23:57 -08001049 rc = 0;
1050 goto exit_send_app_type_cfg;
1051 }
1052
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08001053 if (compare_device_type(&usecase->device_list, AUDIO_DEVICE_OUT_BUS))
Derek Chenf7092792017-05-23 12:23:53 -04001054 is_bus_dev_usecase = true;
1055
Aalique Grahame22e49102018-12-18 14:23:57 -08001056 snd_device = usecase->out_snd_device;
1057 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_PLAYBACK);
1058
1059 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
1060 if (acdb_dev_id < 0) {
1061 ALOGE("%s: Couldn't get the acdb dev id", __func__);
1062 rc = -EINVAL;
1063 goto exit_send_app_type_cfg;
1064 }
1065
1066 if (usecase->type == PCM_HFP_CALL) {
1067
1068 /* config HFP session:1 playback path */
Derek Chenf7092792017-05-23 12:23:53 -04001069 if (is_bus_dev_usecase) {
1070 app_type = usecase->out_app_type_cfg.app_type;
1071 sample_rate= usecase->out_app_type_cfg.sample_rate;
1072 } else {
1073 snd_device = SND_DEVICE_NONE; // use legacy behavior
1074 app_type = platform_get_default_app_type_v2(adev->platform, PCM_PLAYBACK);
1075 sample_rate= CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
1076 }
Aalique Grahame22e49102018-12-18 14:23:57 -08001077 rc = set_stream_app_type_mixer_ctrl(adev, pcm_device_id, app_type,
1078 acdb_dev_id, sample_rate,
1079 PCM_PLAYBACK,
Derek Chenf7092792017-05-23 12:23:53 -04001080 snd_device);
Aalique Grahame22e49102018-12-18 14:23:57 -08001081 if (rc < 0)
1082 goto exit_send_app_type_cfg;
1083
1084 /* config HFP session:1 capture path */
Derek Chenf7092792017-05-23 12:23:53 -04001085 if (is_bus_dev_usecase) {
1086 snd_device = usecase->in_snd_device;
1087 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_CAPTURE);
1088 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
1089 if (acdb_dev_id < 0) {
1090 ALOGE("%s: Couldn't get the acdb dev id", __func__);
1091 rc = -EINVAL;
1092 goto exit_send_app_type_cfg;
1093 }
1094 app_type = usecase->in_app_type_cfg.app_type;
1095 sample_rate= usecase->in_app_type_cfg.sample_rate;
1096 } else {
1097 snd_device = SND_DEVICE_NONE; // use legacy behavior
1098 app_type = platform_get_default_app_type_v2(adev->platform, PCM_CAPTURE);
1099 }
Aalique Grahame22e49102018-12-18 14:23:57 -08001100 rc = set_stream_app_type_mixer_ctrl(adev, pcm_device_id, app_type,
1101 acdb_dev_id, sample_rate,
1102 PCM_CAPTURE,
Derek Chenf7092792017-05-23 12:23:53 -04001103 snd_device);
Aalique Grahame22e49102018-12-18 14:23:57 -08001104 if (rc < 0)
1105 goto exit_send_app_type_cfg;
1106
Derek Chenf7092792017-05-23 12:23:53 -04001107 if (is_bus_dev_usecase)
1108 goto exit_send_app_type_cfg;
1109
Aalique Grahame22e49102018-12-18 14:23:57 -08001110 /* config HFP session:2 capture path */
1111 pcm_device_id = HFP_ASM_RX_TX;
1112 snd_device = usecase->in_snd_device;
1113 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
1114 if (acdb_dev_id <= 0) {
1115 ALOGE("%s: Couldn't get the acdb dev id", __func__);
1116 rc = -EINVAL;
1117 goto exit_send_app_type_cfg;
1118 }
1119 app_type = platform_get_default_app_type_v2(adev->platform, PCM_CAPTURE);
1120 rc = set_stream_app_type_mixer_ctrl(adev, pcm_device_id, app_type,
1121 acdb_dev_id, sample_rate, PCM_CAPTURE,
1122 SND_DEVICE_NONE);
1123 if (rc < 0)
1124 goto exit_send_app_type_cfg;
1125
1126 /* config HFP session:2 playback path */
1127 app_type = platform_get_default_app_type_v2(adev->platform, PCM_PLAYBACK);
1128 rc = set_stream_app_type_mixer_ctrl(adev, pcm_device_id, app_type,
1129 acdb_dev_id, sample_rate,
1130 PCM_PLAYBACK, SND_DEVICE_NONE);
1131 if (rc < 0)
1132 goto exit_send_app_type_cfg;
1133 }
1134
1135 rc = 0;
1136exit_send_app_type_cfg:
1137 return rc;
1138}
1139
Zhou Song06761dd2019-04-28 18:08:17 +08001140int audio_extn_utils_get_app_sample_rate_for_device(
1141 struct audio_device *adev,
1142 struct audio_usecase *usecase, int snd_device)
1143{
1144 char value[PROPERTY_VALUE_MAX] = {0};
1145 int sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
1146
1147 if ((usecase->type == PCM_PLAYBACK) && (usecase->stream.out != NULL)) {
1148 property_get("vendor.audio.playback.mch.downsample",value,"");
1149 if (!strncmp("true", value, sizeof("true"))) {
1150 if ((popcount(usecase->stream.out->channel_mask) > 2) &&
1151 (usecase->stream.out->app_type_cfg.sample_rate > CODEC_BACKEND_DEFAULT_SAMPLE_RATE) &&
1152 !(usecase->stream.out->flags &
1153 (audio_output_flags_t)AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH))
1154 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
1155 }
1156
1157 if (usecase->id == USECASE_AUDIO_PLAYBACK_VOIP) {
1158 usecase->stream.out->app_type_cfg.sample_rate = usecase->stream.out->sample_rate;
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08001159 } else if (compare_device_type(&usecase->stream.out->device_list,
1160 AUDIO_DEVICE_OUT_SPEAKER)) {
Zhou Song06761dd2019-04-28 18:08:17 +08001161 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
1162 } else if ((snd_device == SND_DEVICE_OUT_HDMI ||
1163 snd_device == SND_DEVICE_OUT_USB_HEADSET ||
1164 snd_device == SND_DEVICE_OUT_DISPLAY_PORT) &&
1165 (usecase->stream.out->sample_rate >= OUTPUT_SAMPLING_RATE_44100)) {
1166 /*
1167 * To best utlize DSP, check if the stream sample rate is supported/multiple of
1168 * configured device sample rate, if not update the COPP rate to be equal to the
1169 * device sample rate, else open COPP at stream sample rate
1170 */
1171 platform_check_and_update_copp_sample_rate(adev->platform, snd_device,
1172 usecase->stream.out->sample_rate,
1173 &usecase->stream.out->app_type_cfg.sample_rate);
1174 } else if (((snd_device != SND_DEVICE_OUT_HEADPHONES_44_1 &&
1175 !audio_is_this_native_usecase(usecase)) &&
1176 usecase->stream.out->sample_rate == OUTPUT_SAMPLING_RATE_44100) ||
1177 (usecase->stream.out->sample_rate < OUTPUT_SAMPLING_RATE_44100)) {
1178 /* Reset to default if no native stream is active*/
1179 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Zhou Song9e9b3812019-12-30 17:52:10 +08001180 } else if (usecase->out_snd_device == SND_DEVICE_OUT_BT_A2DP ||
1181 usecase->out_snd_device == SND_DEVICE_OUT_SPEAKER_AND_BT_A2DP ||
1182 usecase->out_snd_device == SND_DEVICE_OUT_SPEAKER_SAFE_AND_BT_A2DP) {
Zhou Song06761dd2019-04-28 18:08:17 +08001183 /*
1184 * For a2dp playback get encoder sampling rate and set copp sampling rate,
1185 * for bit width use the stream param only.
1186 */
1187 audio_extn_a2dp_get_enc_sample_rate(&usecase->stream.out->app_type_cfg.sample_rate);
1188 ALOGI("%s using %d sample rate rate for A2DP CoPP",
1189 __func__, usecase->stream.out->app_type_cfg.sample_rate);
1190 }
1191 audio_extn_btsco_get_sample_rate(snd_device, &usecase->stream.out->app_type_cfg.sample_rate);
1192 sample_rate = usecase->stream.out->app_type_cfg.sample_rate;
1193
1194 if (((usecase->stream.out->format == AUDIO_FORMAT_E_AC3) ||
1195 (usecase->stream.out->format == AUDIO_FORMAT_E_AC3_JOC) ||
1196 (usecase->stream.out->format == AUDIO_FORMAT_DOLBY_TRUEHD))
1197 && audio_extn_passthru_is_passthrough_stream(usecase->stream.out)
1198 && !audio_extn_passthru_is_convert_supported(adev, usecase->stream.out)) {
1199 sample_rate = sample_rate * 4;
1200 if (sample_rate > HDMI_PASSTHROUGH_MAX_SAMPLE_RATE)
1201 sample_rate = HDMI_PASSTHROUGH_MAX_SAMPLE_RATE;
1202 }
1203 } else if (usecase->type == PCM_CAPTURE) {
1204 if (usecase->stream.in != NULL) {
Revathi Uddarajud9f23d92020-07-27 10:55:06 +05301205 if (usecase->id == USECASE_AUDIO_RECORD_VOIP
1206 || usecase->id == USECASE_AUDIO_RECORD_VOIP_LOW_LATENCY)
Zhou Song06761dd2019-04-28 18:08:17 +08001207 usecase->stream.in->app_type_cfg.sample_rate = usecase->stream.in->sample_rate;
1208 if (voice_is_in_call_rec_stream(usecase->stream.in)) {
1209 audio_extn_btsco_get_sample_rate(usecase->in_snd_device,
1210 &usecase->stream.in->app_type_cfg.sample_rate);
1211 } else {
1212 audio_extn_btsco_get_sample_rate(snd_device,
1213 &usecase->stream.in->app_type_cfg.sample_rate);
1214 }
1215 sample_rate = usecase->stream.in->app_type_cfg.sample_rate;
1216 } else if (usecase->id == USECASE_AUDIO_SPKR_CALIB_TX) {
Sujin Panickerb6d76262019-09-09 10:55:38 +05301217 if ((property_get("vendor.audio.spkr_prot.tx.sampling_rate", value, NULL) > 0))
1218 sample_rate = atoi(value);
1219 else
1220 sample_rate = SAMPLE_RATE_8000;
Zhou Song06761dd2019-04-28 18:08:17 +08001221 }
1222 } else if (usecase->type == TRANSCODE_LOOPBACK_RX) {
1223 sample_rate = usecase->stream.inout->out_config.sample_rate;
1224 }
1225 return sample_rate;
1226}
1227
Siena Richard7c2db772016-12-21 11:32:34 -08001228static int send_app_type_cfg_for_device(struct audio_device *adev,
1229 struct audio_usecase *usecase,
1230 int split_snd_device)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001231{
1232 char mixer_ctl_name[MAX_LENGTH_MIXER_CONTROL_IN_INT];
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301233 size_t app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT] = {0};
1234 int len = 0, rc;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001235 struct mixer_ctl *ctl;
Siena Richard7c2db772016-12-21 11:32:34 -08001236 int pcm_device_id = 0, acdb_dev_id, app_type;
1237 int snd_device = split_snd_device, snd_device_be_idx = -1;
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +05301238 int32_t sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Varun Balaraje49253e2017-07-06 19:48:56 +05301239 struct streams_io_cfg *s_info = NULL;
1240 struct listnode *node = NULL;
Nikhil Laturkarac4a7492017-08-31 18:27:19 +05301241 int bd_app_type = 0;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001242
Siena Richard7c2db772016-12-21 11:32:34 -08001243 ALOGV("%s: usecase->out_snd_device %s, usecase->in_snd_device %s, split_snd_device %s",
1244 __func__, platform_get_snd_device_name(usecase->out_snd_device),
1245 platform_get_snd_device_name(usecase->in_snd_device),
1246 platform_get_snd_device_name(split_snd_device));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001247
Siddartha Shaik343abc62017-08-08 11:15:25 +05301248 if (usecase->type != PCM_PLAYBACK && usecase->type != PCM_CAPTURE &&
Surendar Karka93cd25a2018-08-28 14:21:37 +05301249 usecase->type != TRANSCODE_LOOPBACK_RX) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301250 ALOGE("%s: not a playback/capture path, no need to cfg app type", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001251 rc = 0;
1252 goto exit_send_app_type_cfg;
1253 }
1254 if ((usecase->id != USECASE_AUDIO_PLAYBACK_DEEP_BUFFER) &&
Vignesh Kulothungan0a2eff02019-07-11 16:30:13 -07001255 (usecase->id != USECASE_AUDIO_PLAYBACK_WITH_HAPTICS) &&
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001256 (usecase->id != USECASE_AUDIO_PLAYBACK_LOW_LATENCY) &&
1257 (usecase->id != USECASE_AUDIO_PLAYBACK_MULTI_CH) &&
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -08001258 (usecase->id != USECASE_AUDIO_PLAYBACK_ULL) &&
Vikram Panduranga93f080e2017-06-07 18:16:14 -07001259 (usecase->id != USECASE_AUDIO_PLAYBACK_VOIP) &&
Surendar Karka93cd25a2018-08-28 14:21:37 +05301260 (usecase->id != USECASE_AUDIO_TRANSCODE_LOOPBACK_RX) &&
Varun Balaraje49253e2017-07-06 19:48:56 +05301261 (!is_interactive_usecase(usecase->id)) &&
Srikanth Uyyala9d551402015-08-25 16:03:42 +05301262 (!is_offload_usecase(usecase->id)) &&
Derek Chenf6318be2017-06-12 17:16:24 -04001263 (usecase->type != PCM_CAPTURE) &&
1264 (!audio_extn_auto_hal_is_bus_device_usecase(usecase->id))) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301265 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 -07001266 rc = 0;
1267 goto exit_send_app_type_cfg;
1268 }
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -08001269
1270 //if VR is active then only send the mixer control
1271 if (usecase->id == USECASE_AUDIO_PLAYBACK_ULL && !audio_is_vr_mode_on(adev)) {
1272 ALOGI("ULL doesnt need sending app type cfg, returning");
1273 rc = 0;
1274 goto exit_send_app_type_cfg;
1275 }
1276
Surendar Karka93cd25a2018-08-28 14:21:37 +05301277 if (usecase->type == PCM_PLAYBACK || usecase->type == TRANSCODE_LOOPBACK_RX) {
Ben Romberger1fafdde2015-09-09 19:43:15 -07001278 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_PLAYBACK);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301279 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
1280 "Audio Stream %d App Type Cfg", pcm_device_id);
Ben Romberger1fafdde2015-09-09 19:43:15 -07001281 } else if (usecase->type == PCM_CAPTURE) {
Ben Romberger1fafdde2015-09-09 19:43:15 -07001282 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_CAPTURE);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301283 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
1284 "Audio Stream Capture %d App Type Cfg", pcm_device_id);
Srikanth Uyyala9d551402015-08-25 16:03:42 +05301285 }
Siena Richard7c2db772016-12-21 11:32:34 -08001286
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001287 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
1288 if (!ctl) {
1289 ALOGE("%s: Could not get ctl for mixer cmd - %s", __func__,
1290 mixer_ctl_name);
1291 rc = -EINVAL;
1292 goto exit_send_app_type_cfg;
1293 }
Aditya Bavanari701a6992017-03-30 19:17:16 +05301294 snd_device = platform_get_spkr_prot_snd_device(snd_device);
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301295 if (voice_is_in_call_rec_stream(usecase->stream.in) && usecase->type == PCM_CAPTURE) {
1296 snd_device_t voice_device = voice_get_incall_rec_snd_device(usecase->in_snd_device);
1297 acdb_dev_id = platform_get_snd_device_acdb_id(voice_device);
1298 ALOGV("acdb id for voice call use case %d", acdb_dev_id);
1299 } else {
1300 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
1301 }
Rohit Kumar1181b292017-01-31 18:07:17 +05301302 if (acdb_dev_id <= 0) {
1303 ALOGE("%s: Couldn't get the acdb dev id", __func__);
1304 rc = -EINVAL;
1305 goto exit_send_app_type_cfg;
1306 }
1307
Siena Richard7c2db772016-12-21 11:32:34 -08001308 snd_device_be_idx = platform_get_snd_device_backend_index(snd_device);
1309 if (snd_device_be_idx < 0) {
1310 ALOGE("%s: Couldn't get the backend index for snd device %s ret=%d",
1311 __func__, platform_get_snd_device_name(snd_device),
1312 snd_device_be_idx);
1313 }
1314
Zhou Song06761dd2019-04-28 18:08:17 +08001315 sample_rate = audio_extn_utils_get_app_sample_rate_for_device(adev, usecase, snd_device);
1316
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301317 if ((usecase->type == PCM_PLAYBACK) && (usecase->stream.out != NULL)) {
Varun Balaraje49253e2017-07-06 19:48:56 +05301318 /* Interactive streams are supported with only direct app type id.
1319 * Get Direct profile app type and use it for interactive streams
1320 */
1321 list_for_each(node, &adev->streams_output_cfg_list) {
1322 s_info = node_to_item(node, struct streams_io_cfg, list);
Aalique Grahame5cd12ff2017-10-19 10:57:00 -07001323 if (s_info->flags.out_flags == (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_BD |
Nikhil Laturkarac4a7492017-08-31 18:27:19 +05301324 AUDIO_OUTPUT_FLAG_DIRECT_PCM |
1325 AUDIO_OUTPUT_FLAG_DIRECT))
1326 bd_app_type = s_info->app_type_cfg.app_type;
Varun Balaraje49253e2017-07-06 19:48:56 +05301327 }
Aalique Grahame5cd12ff2017-10-19 10:57:00 -07001328 if (usecase->stream.out->flags == (audio_output_flags_t)AUDIO_OUTPUT_FLAG_INTERACTIVE)
Nikhil Laturkarac4a7492017-08-31 18:27:19 +05301329 app_type = bd_app_type;
Varun Balaraje49253e2017-07-06 19:48:56 +05301330 else
1331 app_type = usecase->stream.out->app_type_cfg.app_type;
Siena Richard7c2db772016-12-21 11:32:34 -08001332 app_type_cfg[len++] = app_type;
Mingming Yin21854652016-04-13 11:54:02 -07001333 app_type_cfg[len++] = acdb_dev_id;
Naresh Tanniru3a406772017-05-10 13:09:05 -07001334 app_type_cfg[len++] = sample_rate;
1335
Siena Richard7c2db772016-12-21 11:32:34 -08001336 if (snd_device_be_idx > 0)
1337 app_type_cfg[len++] = snd_device_be_idx;
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301338
Siena Richard7c2db772016-12-21 11:32:34 -08001339 ALOGI("%s PLAYBACK app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
1340 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301341
1342 } else if ((usecase->type == PCM_CAPTURE) && (usecase->stream.in != NULL)) {
Siena Richard7c2db772016-12-21 11:32:34 -08001343 app_type = usecase->stream.in->app_type_cfg.app_type;
1344 app_type_cfg[len++] = app_type;
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301345 app_type_cfg[len++] = acdb_dev_id;
Siena Richard7c2db772016-12-21 11:32:34 -08001346 app_type_cfg[len++] = sample_rate;
1347 if (snd_device_be_idx > 0)
1348 app_type_cfg[len++] = snd_device_be_idx;
1349 ALOGI("%s CAPTURE app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
1350 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301351 } else {
Siena Richard7c2db772016-12-21 11:32:34 -08001352 app_type = platform_get_default_app_type_v2(adev->platform, usecase->type);
Surendar Karka93cd25a2018-08-28 14:21:37 +05301353 if(usecase->type == TRANSCODE_LOOPBACK_RX) {
Siddartha Shaik343abc62017-08-08 11:15:25 +05301354 app_type = usecase->stream.inout->out_app_type_cfg.app_type;
1355 }
Siena Richard7c2db772016-12-21 11:32:34 -08001356 app_type_cfg[len++] = app_type;
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301357 app_type_cfg[len++] = acdb_dev_id;
1358 app_type_cfg[len++] = sample_rate;
Siena Richard7c2db772016-12-21 11:32:34 -08001359 if (snd_device_be_idx > 0)
1360 app_type_cfg[len++] = snd_device_be_idx;
1361 ALOGI("%s default app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
1362 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Ashish Jainc02430d2016-01-04 10:42:43 +05301363 }
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301364
Siddartha Shaik343abc62017-08-08 11:15:25 +05301365 if(ctl)
1366 mixer_ctl_set_array(ctl, app_type_cfg, len);
Zhou Songd9bd9302020-08-04 16:34:45 +08001367
1368 /* send app type cfg for haptics */
1369 if (usecase->id == USECASE_AUDIO_PLAYBACK_WITH_HAPTICS) {
1370 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
1371 "Audio Stream %d App Type Cfg", adev->haptic_pcm_device_id );
1372 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
1373 if (!ctl) {
1374 ALOGE("%s: Could not get ctl for mixer cmd - %s", __func__,
1375 mixer_ctl_name);
1376 rc = -EINVAL;
1377 goto exit_send_app_type_cfg;
1378 }
1379 acdb_dev_id = platform_get_snd_device_acdb_id(SND_DEVICE_OUT_HAPTICS);
1380 snd_device_be_idx = platform_get_snd_device_backend_index(SND_DEVICE_OUT_HAPTICS);
1381 /* haptics acdb id */
1382 app_type_cfg[1] = acdb_dev_id;
1383 /* haptics be index */
1384 app_type_cfg[3] = snd_device_be_idx;
1385 mixer_ctl_set_array(ctl, app_type_cfg, len);
1386 }
1387
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001388 rc = 0;
1389exit_send_app_type_cfg:
1390 return rc;
1391}
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001392
Deeraj Soman14230922019-01-30 16:39:30 +05301393static int audio_extn_utils_check_input_parameters(uint32_t sample_rate,
1394 audio_format_t format,
1395 int channel_count)
1396{
1397 int ret = 0;
1398
1399 if (((format != AUDIO_FORMAT_PCM_16_BIT) && (format != AUDIO_FORMAT_PCM_8_24_BIT) &&
1400 (format != AUDIO_FORMAT_PCM_24_BIT_PACKED) && (format != AUDIO_FORMAT_PCM_32_BIT) &&
1401 (format != AUDIO_FORMAT_PCM_FLOAT)) &&
1402 !voice_extn_compress_voip_is_format_supported(format) &&
1403 !audio_extn_compr_cap_format_supported(format) &&
1404 !audio_extn_cin_format_supported(format))
1405 ret = -EINVAL;
1406
1407 switch (channel_count) {
1408 case 1:
1409 case 2:
1410 case 3:
1411 case 4:
1412 case 6:
1413 case 8:
1414 break;
1415 default:
1416 ret = -EINVAL;
1417 }
1418
1419 switch (sample_rate) {
1420 case 8000:
1421 case 11025:
1422 case 12000:
1423 case 16000:
1424 case 22050:
1425 case 24000:
1426 case 32000:
1427 case 44100:
1428 case 48000:
1429 case 88200:
1430 case 96000:
1431 case 176400:
1432 case 192000:
1433 break;
1434 default:
1435 ret = -EINVAL;
1436 }
1437
1438 return ret;
1439}
1440
1441static inline uint32_t audio_extn_utils_nearest_multiple(uint32_t num, uint32_t multiplier)
1442{
1443 uint32_t remainder = 0;
1444
1445 if (!multiplier)
1446 return num;
1447
1448 remainder = num % multiplier;
1449 if (remainder)
1450 num += (multiplier - remainder);
1451
1452 return num;
1453}
1454
1455static inline uint32_t audio_extn_utils_lcm(uint32_t num1, uint32_t num2)
1456{
1457 uint32_t high = num1, low = num2, temp = 0;
1458
1459 if (!num1 || !num2)
1460 return 0;
1461
1462 if (num1 < num2) {
1463 high = num2;
1464 low = num1;
1465 }
1466
1467 while (low != 0) {
1468 temp = low;
1469 low = high % low;
1470 high = temp;
1471 }
1472 return (num1 * num2)/high;
1473}
1474
Siena Richard7c2db772016-12-21 11:32:34 -08001475int audio_extn_utils_send_app_type_cfg(struct audio_device *adev,
1476 struct audio_usecase *usecase)
1477{
1478 int i, num_devices = 0;
1479 snd_device_t new_snd_devices[SND_DEVICE_OUT_END] = {0};
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301480 snd_device_t in_snd_device = usecase->in_snd_device;
Siena Richard7c2db772016-12-21 11:32:34 -08001481 int rc = 0;
1482
Aalique Grahame22e49102018-12-18 14:23:57 -08001483 if (usecase->type == PCM_HFP_CALL) {
1484 return audio_extn_utils_send_app_type_cfg_hfp(adev, usecase);
1485 }
1486
Siena Richard7c2db772016-12-21 11:32:34 -08001487 switch (usecase->type) {
1488 case PCM_PLAYBACK:
Surendar Karka93cd25a2018-08-28 14:21:37 +05301489 case TRANSCODE_LOOPBACK_RX:
Siena Richard7c2db772016-12-21 11:32:34 -08001490 ALOGD("%s: usecase->out_snd_device %s",
1491 __func__, platform_get_snd_device_name(usecase->out_snd_device));
1492 /* check for out combo device */
1493 if (platform_split_snd_device(adev->platform,
1494 usecase->out_snd_device,
1495 &num_devices, new_snd_devices)) {
1496 new_snd_devices[0] = usecase->out_snd_device;
1497 num_devices = 1;
1498 }
1499 break;
1500 case PCM_CAPTURE:
1501 ALOGD("%s: usecase->in_snd_device %s",
1502 __func__, platform_get_snd_device_name(usecase->in_snd_device));
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301503 if (voice_is_in_call_rec_stream(usecase->stream.in)) {
1504 in_snd_device = voice_get_incall_rec_backend_device(usecase->stream.in);
1505 }
Siena Richard7c2db772016-12-21 11:32:34 -08001506 /* check for in combo device */
1507 if (platform_split_snd_device(adev->platform,
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301508 in_snd_device,
Siena Richard7c2db772016-12-21 11:32:34 -08001509 &num_devices, new_snd_devices)) {
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301510 new_snd_devices[0] = in_snd_device;
Siena Richard7c2db772016-12-21 11:32:34 -08001511 num_devices = 1;
1512 }
1513 break;
1514 default:
1515 ALOGI("%s: not a playback/capture path, no need to cfg app type", __func__);
1516 rc = 0;
1517 break;
1518 }
1519
1520 for (i = 0; i < num_devices; i++) {
1521 rc = send_app_type_cfg_for_device(adev, usecase, new_snd_devices[i]);
1522 if (rc)
1523 break;
1524 }
1525
1526 return rc;
1527}
1528
Preetam Singh Ranawat9519e9c2015-11-18 16:05:55 +05301529int read_line_from_file(const char *path, char *buf, size_t count)
1530{
1531 char * fgets_ret;
1532 FILE * fd;
1533 int rv;
1534
1535 fd = fopen(path, "r");
1536 if (fd == NULL)
1537 return -1;
1538
1539 fgets_ret = fgets(buf, (int)count, fd);
1540 if (NULL != fgets_ret) {
1541 rv = (int)strlen(buf);
1542 } else {
1543 rv = ferror(fd);
1544 }
1545 fclose(fd);
1546
1547 return rv;
1548}
1549
Ashish Jainf1eaa582016-05-23 20:54:24 +05301550/*Translates ALSA formats to AOSP PCM formats*/
1551audio_format_t alsa_format_to_hal(uint32_t alsa_format)
1552{
1553 audio_format_t format;
1554
1555 switch(alsa_format) {
1556 case SNDRV_PCM_FORMAT_S16_LE:
1557 format = AUDIO_FORMAT_PCM_16_BIT;
1558 break;
1559 case SNDRV_PCM_FORMAT_S24_3LE:
1560 format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
1561 break;
1562 case SNDRV_PCM_FORMAT_S24_LE:
1563 format = AUDIO_FORMAT_PCM_8_24_BIT;
1564 break;
1565 case SNDRV_PCM_FORMAT_S32_LE:
1566 format = AUDIO_FORMAT_PCM_32_BIT;
1567 break;
1568 default:
1569 ALOGW("Incorrect ALSA format");
1570 format = AUDIO_FORMAT_INVALID;
1571 }
1572 return format;
1573}
1574
1575/*Translates hal format (AOSP) to alsa formats*/
1576uint32_t hal_format_to_alsa(audio_format_t hal_format)
1577{
1578 uint32_t alsa_format;
1579
1580 switch (hal_format) {
1581 case AUDIO_FORMAT_PCM_32_BIT: {
1582 if (platform_supports_true_32bit())
1583 alsa_format = SNDRV_PCM_FORMAT_S32_LE;
1584 else
1585 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1586 }
1587 break;
1588 case AUDIO_FORMAT_PCM_8_BIT:
1589 alsa_format = SNDRV_PCM_FORMAT_S8;
1590 break;
1591 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
1592 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1593 break;
1594 case AUDIO_FORMAT_PCM_8_24_BIT: {
1595 if (platform_supports_true_32bit())
1596 alsa_format = SNDRV_PCM_FORMAT_S32_LE;
1597 else
1598 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1599 }
1600 break;
1601 case AUDIO_FORMAT_PCM_FLOAT:
1602 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1603 break;
1604 default:
1605 case AUDIO_FORMAT_PCM_16_BIT:
1606 alsa_format = SNDRV_PCM_FORMAT_S16_LE;
1607 break;
1608 }
1609 return alsa_format;
1610}
1611
Ashish Jain83a6cc22016-06-28 14:34:17 +05301612/*Translates PCM formats to AOSP formats*/
1613audio_format_t pcm_format_to_hal(uint32_t pcm_format)
1614{
1615 audio_format_t format = AUDIO_FORMAT_INVALID;
1616
1617 switch(pcm_format) {
1618 case PCM_FORMAT_S16_LE:
1619 format = AUDIO_FORMAT_PCM_16_BIT;
1620 break;
1621 case PCM_FORMAT_S24_3LE:
1622 format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
1623 break;
1624 case PCM_FORMAT_S24_LE:
1625 format = AUDIO_FORMAT_PCM_8_24_BIT;
1626 break;
1627 case PCM_FORMAT_S32_LE:
1628 format = AUDIO_FORMAT_PCM_32_BIT;
1629 break;
1630 default:
1631 ALOGW("Incorrect PCM format");
1632 format = AUDIO_FORMAT_INVALID;
1633 }
1634 return format;
1635}
1636
1637/*Translates hal format (AOSP) to alsa formats*/
1638uint32_t hal_format_to_pcm(audio_format_t hal_format)
1639{
1640 uint32_t pcm_format;
1641
1642 switch (hal_format) {
1643 case AUDIO_FORMAT_PCM_32_BIT:
1644 case AUDIO_FORMAT_PCM_8_24_BIT:
1645 case AUDIO_FORMAT_PCM_FLOAT: {
1646 if (platform_supports_true_32bit())
1647 pcm_format = PCM_FORMAT_S32_LE;
1648 else
1649 pcm_format = PCM_FORMAT_S24_3LE;
1650 }
1651 break;
1652 case AUDIO_FORMAT_PCM_8_BIT:
1653 pcm_format = PCM_FORMAT_S8;
1654 break;
1655 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
1656 pcm_format = PCM_FORMAT_S24_3LE;
1657 break;
1658 default:
1659 case AUDIO_FORMAT_PCM_16_BIT:
1660 pcm_format = PCM_FORMAT_S16_LE;
1661 break;
1662 }
1663 return pcm_format;
1664}
1665
Ashish Jainf1eaa582016-05-23 20:54:24 +05301666uint32_t get_alsa_fragment_size(uint32_t bytes_per_sample,
1667 uint32_t sample_rate,
Deeraj Soman65358ab2019-02-07 15:40:49 +05301668 uint32_t noOfChannels,
1669 int64_t duration_ms)
Ashish Jainf1eaa582016-05-23 20:54:24 +05301670{
1671 uint32_t fragment_size = 0;
1672 uint32_t pcm_offload_time = PCM_OFFLOAD_BUFFER_DURATION;
1673
Deeraj Soman65358ab2019-02-07 15:40:49 +05301674 if (duration_ms >= MIN_OFFLOAD_BUFFER_DURATION_MS && duration_ms <= MAX_OFFLOAD_BUFFER_DURATION_MS)
1675 pcm_offload_time = duration_ms;
1676
Ashish Jainf1eaa582016-05-23 20:54:24 +05301677 fragment_size = (pcm_offload_time
1678 * sample_rate
1679 * bytes_per_sample
1680 * noOfChannels)/1000;
1681 if (fragment_size < MIN_PCM_OFFLOAD_FRAGMENT_SIZE)
1682 fragment_size = MIN_PCM_OFFLOAD_FRAGMENT_SIZE;
1683 else if (fragment_size > MAX_PCM_OFFLOAD_FRAGMENT_SIZE)
1684 fragment_size = MAX_PCM_OFFLOAD_FRAGMENT_SIZE;
1685 /*To have same PCM samples for all channels, the buffer size requires to
1686 *be multiple of (number of channels * bytes per sample)
1687 *For writes to succeed, the buffer must be written at address which is multiple of 32
1688 */
Aalique Grahameb85f2d92017-10-16 17:53:23 -07001689 fragment_size = ALIGN(fragment_size, (bytes_per_sample * noOfChannels * 32));
Ashish Jainf1eaa582016-05-23 20:54:24 +05301690
1691 ALOGI("PCM offload Fragment size to %d bytes", fragment_size);
1692 return fragment_size;
1693}
1694
1695/* Calculates the fragment size required to configure compress session.
1696 * Based on the alsa format selected, decide if conversion is needed in
1697
1698 * HAL ( e.g. convert AUDIO_FORMAT_PCM_FLOAT input format to
1699 * AUDIO_FORMAT_PCM_24_BIT_PACKED before writing to the compress driver.
1700 */
1701void audio_extn_utils_update_direct_pcm_fragment_size(struct stream_out *out)
1702{
Ashish Jain83a6cc22016-06-28 14:34:17 +05301703 audio_format_t dst_format = out->hal_op_format;
1704 audio_format_t src_format = out->hal_ip_format;
Ashish Jainf1eaa582016-05-23 20:54:24 +05301705 uint32_t hal_op_bytes_per_sample = audio_bytes_per_sample(dst_format);
1706 uint32_t hal_ip_bytes_per_sample = audio_bytes_per_sample(src_format);
1707
1708 out->compr_config.fragment_size =
1709 get_alsa_fragment_size(hal_op_bytes_per_sample,
1710 out->sample_rate,
Deeraj Soman65358ab2019-02-07 15:40:49 +05301711 popcount(out->channel_mask),
1712 out->info.duration_us / 1000);
Ashish Jainf1eaa582016-05-23 20:54:24 +05301713
1714 if ((src_format != dst_format) &&
1715 hal_op_bytes_per_sample != hal_ip_bytes_per_sample) {
1716
Ashish Jain83a6cc22016-06-28 14:34:17 +05301717 out->hal_fragment_size =
Ashish Jainf1eaa582016-05-23 20:54:24 +05301718 ((out->compr_config.fragment_size * hal_ip_bytes_per_sample) /
1719 hal_op_bytes_per_sample);
1720 ALOGI("enable conversion hal_input_fragment_size is %d src_format %x dst_format %x",
Ashish Jain83a6cc22016-06-28 14:34:17 +05301721 out->hal_fragment_size, src_format, dst_format);
Ashish Jainf1eaa582016-05-23 20:54:24 +05301722 } else {
Ashish Jain83a6cc22016-06-28 14:34:17 +05301723 out->hal_fragment_size = out->compr_config.fragment_size;
Ashish Jainf1eaa582016-05-23 20:54:24 +05301724 }
1725}
1726
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301727/* converts pcm format 24_8 to 8_24 inplace */
1728size_t audio_extn_utils_convert_format_24_8_to_8_24(void *buf, size_t bytes)
1729{
1730 size_t i = 0;
1731 int *int_buf_stream = buf;
1732
1733 if ((bytes % 4) != 0) {
1734 ALOGE("%s: wrong inout buffer! ... is not 32 bit aligned ", __func__);
1735 return -EINVAL;
1736 }
1737
1738 for (; i < (bytes / 4); i++)
1739 int_buf_stream[i] >>= 8;
1740
1741 return bytes;
1742}
1743
Xiaojun Sang782e5b12020-06-29 21:13:06 +08001744#ifdef AUDIO_GKI_ENABLED
1745int get_snd_codec_id(audio_format_t format)
1746{
1747 int id = 0;
1748
1749 switch (format & AUDIO_FORMAT_MAIN_MASK) {
1750 case AUDIO_FORMAT_MP3:
1751 id = SND_AUDIOCODEC_MP3;
1752 break;
1753 case AUDIO_FORMAT_AAC:
1754 id = SND_AUDIOCODEC_AAC;
1755 break;
1756 case AUDIO_FORMAT_AAC_ADTS:
1757 id = SND_AUDIOCODEC_AAC;
1758 break;
1759 case AUDIO_FORMAT_AAC_LATM:
1760 id = SND_AUDIOCODEC_AAC;
1761 break;
1762 case AUDIO_FORMAT_PCM:
1763 id = SND_AUDIOCODEC_PCM;
1764 break;
1765 case AUDIO_FORMAT_FLAC:
1766 case AUDIO_FORMAT_ALAC:
1767 case AUDIO_FORMAT_APE:
1768 case AUDIO_FORMAT_VORBIS:
1769 case AUDIO_FORMAT_WMA:
1770 case AUDIO_FORMAT_WMA_PRO:
1771 case AUDIO_FORMAT_DSD:
1772 case AUDIO_FORMAT_APTX:
1773 id = SND_AUDIOCODEC_BESPOKE;
1774 break;
1775 case AUDIO_FORMAT_MP2:
1776 id = SND_AUDIOCODEC_MP2;
1777 break;
1778 case AUDIO_FORMAT_AC3:
1779 id = SND_AUDIOCODEC_AC3;
1780 break;
1781 case AUDIO_FORMAT_E_AC3:
1782 case AUDIO_FORMAT_E_AC3_JOC:
1783 id = SND_AUDIOCODEC_EAC3;
1784 break;
1785 case AUDIO_FORMAT_DTS:
1786 case AUDIO_FORMAT_DTS_HD:
1787 id = SND_AUDIOCODEC_DTS;
1788 break;
1789 case AUDIO_FORMAT_DOLBY_TRUEHD:
1790 id = SND_AUDIOCODEC_TRUEHD;
1791 break;
1792 case AUDIO_FORMAT_IEC61937:
1793 id = SND_AUDIOCODEC_IEC61937;
1794 break;
1795 default:
1796 ALOGE("%s: Unsupported audio format :%x", __func__, format);
1797 }
1798
1799 return id;
1800}
1801#else
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301802int get_snd_codec_id(audio_format_t format)
1803{
1804 int id = 0;
1805
1806 switch (format & AUDIO_FORMAT_MAIN_MASK) {
1807 case AUDIO_FORMAT_MP3:
1808 id = SND_AUDIOCODEC_MP3;
1809 break;
1810 case AUDIO_FORMAT_AAC:
1811 id = SND_AUDIOCODEC_AAC;
1812 break;
1813 case AUDIO_FORMAT_AAC_ADTS:
1814 id = SND_AUDIOCODEC_AAC;
1815 break;
Arun Kumar Dasari3b174182016-12-27 13:01:14 +05301816 case AUDIO_FORMAT_AAC_LATM:
1817 id = SND_AUDIOCODEC_AAC;
1818 break;
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301819 case AUDIO_FORMAT_PCM:
1820 id = SND_AUDIOCODEC_PCM;
1821 break;
1822 case AUDIO_FORMAT_FLAC:
1823 id = SND_AUDIOCODEC_FLAC;
1824 break;
1825 case AUDIO_FORMAT_ALAC:
1826 id = SND_AUDIOCODEC_ALAC;
1827 break;
1828 case AUDIO_FORMAT_APE:
1829 id = SND_AUDIOCODEC_APE;
1830 break;
1831 case AUDIO_FORMAT_VORBIS:
1832 id = SND_AUDIOCODEC_VORBIS;
1833 break;
1834 case AUDIO_FORMAT_WMA:
1835 id = SND_AUDIOCODEC_WMA;
1836 break;
1837 case AUDIO_FORMAT_WMA_PRO:
1838 id = SND_AUDIOCODEC_WMA_PRO;
1839 break;
Satish Babu Patakokila0c313922016-12-08 12:07:08 +05301840 case AUDIO_FORMAT_MP2:
1841 id = SND_AUDIOCODEC_MP2;
1842 break;
Satish Babu Patakokila915ecba2017-01-10 17:43:56 +05301843 case AUDIO_FORMAT_AC3:
1844 id = SND_AUDIOCODEC_AC3;
1845 break;
1846 case AUDIO_FORMAT_E_AC3:
1847 case AUDIO_FORMAT_E_AC3_JOC:
1848 id = SND_AUDIOCODEC_EAC3;
1849 break;
1850 case AUDIO_FORMAT_DTS:
1851 case AUDIO_FORMAT_DTS_HD:
1852 id = SND_AUDIOCODEC_DTS;
1853 break;
Ben Romberger1aaaf862017-04-06 17:49:46 -07001854 case AUDIO_FORMAT_DOLBY_TRUEHD:
1855 id = SND_AUDIOCODEC_TRUEHD;
1856 break;
Naresh Tanniru928f0862017-04-07 16:44:23 -07001857 case AUDIO_FORMAT_IEC61937:
1858 id = SND_AUDIOCODEC_IEC61937;
1859 break;
Preetam Singh Ranawat4277a5a2017-01-18 19:02:24 +05301860 case AUDIO_FORMAT_DSD:
1861 id = SND_AUDIOCODEC_DSD;
1862 break;
Dhanalakshmi Siddani18737932016-11-29 17:33:17 +05301863 case AUDIO_FORMAT_APTX:
1864 id = SND_AUDIOCODEC_APTX;
1865 break;
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301866 default:
1867 ALOGE("%s: Unsupported audio format :%x", __func__, format);
1868 }
1869
1870 return id;
1871}
Xiaojun Sang782e5b12020-06-29 21:13:06 +08001872#endif
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301873
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001874void audio_extn_utils_send_audio_calibration(struct audio_device *adev,
1875 struct audio_usecase *usecase)
1876{
1877 int type = usecase->type;
1878
Dhananjay Kumar14245982017-01-16 20:21:00 +05301879 if (type == PCM_PLAYBACK && usecase->stream.out != NULL) {
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05301880 platform_send_audio_calibration(adev->platform, usecase,
Zhou Song06761dd2019-04-28 18:08:17 +08001881 usecase->stream.out->app_type_cfg.app_type);
Dhananjay Kumar14245982017-01-16 20:21:00 +05301882 } else if (type == PCM_CAPTURE && usecase->stream.in != NULL) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301883 platform_send_audio_calibration(adev->platform, usecase,
Zhou Song06761dd2019-04-28 18:08:17 +08001884 usecase->stream.in->app_type_cfg.app_type);
1885 } else if ((type == PCM_HFP_CALL) || (type == PCM_CAPTURE) ||
1886 (type == TRANSCODE_LOOPBACK_RX && usecase->stream.inout != NULL)) {
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05301887 platform_send_audio_calibration(adev->platform, usecase,
Zhou Song06761dd2019-04-28 18:08:17 +08001888 platform_get_default_app_type_v2(adev->platform, usecase->type));
Vidyakumar Athotae57f6002017-03-01 13:13:16 -08001889 } else {
1890 /* No need to send audio calibration for voice and voip call usecases */
1891 if ((type != VOICE_CALL) && (type != VOIP_CALL))
1892 ALOGW("%s: No audio calibration for usecase type = %d", __func__, type);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001893 }
1894}
1895
Ben Rombergera04fabc2014-11-14 12:16:03 -08001896// Base64 Encode and Decode
1897// Not all features supported. This must be used only with following conditions.
1898// Decode Modes: Support with and without padding
1899// CRLF not handling. So no CRLF in string to decode.
1900// Encode Modes: Supports only padding
1901int b64decode(char *inp, int ilen, uint8_t* outp)
1902{
1903 int i, j, k, ii, num;
1904 int rem, pcnt;
1905 uint32_t res=0;
1906 uint8_t getIndex[MAX_BASEINDEX_LEN];
1907 uint8_t tmp, cflag;
1908
1909 if(inp == NULL || outp == NULL || ilen <= 0) {
1910 ALOGE("[%s] received NULL pointer or zero length",__func__);
1911 return -1;
1912 }
1913
1914 memset(getIndex, MAX_BASEINDEX_LEN-1, sizeof(getIndex));
1915 for(i=0;i<BASE_TABLE_SIZE;i++) {
1916 getIndex[(uint8_t)bTable[i]] = (uint8_t)i;
1917 }
1918 getIndex[(uint8_t)'=']=0;
1919
1920 j=0;k=0;
1921 num = ilen/4;
1922 rem = ilen%4;
1923 if(rem==0)
1924 num = num-1;
1925 cflag=0;
1926 for(i=0; i<num; i++) {
1927 res=0;
1928 for(ii=0;ii<4;ii++) {
1929 res = res << 6;
1930 tmp = getIndex[(uint8_t)inp[j++]];
1931 res = res | tmp;
1932 cflag = cflag | tmp;
1933 }
1934 outp[k++] = (res >> 16)&0xFF;
1935 outp[k++] = (res >> 8)&0xFF;
1936 outp[k++] = res & 0xFF;
1937 }
1938
1939 // Handle last bytes special
1940 pcnt=0;
1941 if(rem == 0) {
1942 //With padding or full data
1943 res = 0;
1944 for(ii=0;ii<4;ii++) {
1945 if(inp[j] == '=')
1946 pcnt++;
1947 res = res << 6;
1948 tmp = getIndex[(uint8_t)inp[j++]];
1949 res = res | tmp;
1950 cflag = cflag | tmp;
1951 }
1952 outp[k++] = res >> 16;
1953 if(pcnt == 2)
1954 goto done;
1955 outp[k++] = (res>>8)&0xFF;
1956 if(pcnt == 1)
1957 goto done;
1958 outp[k++] = res&0xFF;
1959 } else {
1960 //without padding
1961 res = 0;
1962 for(i=0;i<rem;i++) {
1963 res = res << 6;
1964 tmp = getIndex[(uint8_t)inp[j++]];
1965 res = res | tmp;
1966 cflag = cflag | tmp;
1967 }
1968 for(i=rem;i<4;i++) {
1969 res = res << 6;
1970 pcnt++;
1971 }
1972 outp[k++] = res >> 16;
1973 if(pcnt == 2)
1974 goto done;
1975 outp[k++] = (res>>8)&0xFF;
1976 if(pcnt == 1)
1977 goto done;
1978 outp[k++] = res&0xFF;
1979 }
1980done:
1981 if(cflag == 0xFF) {
1982 ALOGE("[%s] base64 decode failed. Invalid character found %s",
1983 __func__, inp);
1984 return 0;
1985 }
1986 return k;
1987}
1988
1989int b64encode(uint8_t *inp, int ilen, char* outp)
1990{
1991 int i,j,k, num;
1992 int rem=0;
1993 uint32_t res=0;
1994
1995 if(inp == NULL || outp == NULL || ilen<=0) {
1996 ALOGE("[%s] received NULL pointer or zero input length",__func__);
1997 return -1;
1998 }
1999
2000 num = ilen/3;
2001 rem = ilen%3;
2002 j=0;k=0;
2003 for(i=0; i<num; i++) {
2004 //prepare index
2005 res = inp[j++]<<16;
2006 res = res | inp[j++]<<8;
2007 res = res | inp[j++];
2008 //get output map from index
2009 outp[k++] = (char) bTable[(res>>18)&0x3F];
2010 outp[k++] = (char) bTable[(res>>12)&0x3F];
2011 outp[k++] = (char) bTable[(res>>6)&0x3F];
2012 outp[k++] = (char) bTable[res&0x3F];
2013 }
2014
2015 switch(rem) {
2016 case 1:
2017 res = inp[j++]<<16;
2018 outp[k++] = (char) bTable[res>>18];
2019 outp[k++] = (char) bTable[(res>>12)&0x3F];
2020 //outp[k++] = '=';
2021 //outp[k++] = '=';
2022 break;
2023 case 2:
2024 res = inp[j++]<<16;
2025 res = res | inp[j++]<<8;
2026 outp[k++] = (char) bTable[res>>18];
2027 outp[k++] = (char) bTable[(res>>12)&0x3F];
2028 outp[k++] = (char) bTable[(res>>6)&0x3F];
2029 //outp[k++] = '=';
2030 break;
2031 default:
2032 break;
2033 }
Ben Rombergera04fabc2014-11-14 12:16:03 -08002034 outp[k] = '\0';
2035 return k;
2036}
Ashish Jain81eb2a82015-05-13 10:52:34 +05302037
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05302038
2039int audio_extn_utils_get_codec_version(const char *snd_card_name,
2040 int card_num,
2041 char *codec_version)
2042{
2043 char procfs_path[50];
2044 FILE *fp;
2045
2046 if (strstr(snd_card_name, "tasha")) {
2047 snprintf(procfs_path, sizeof(procfs_path),
2048 "/proc/asound/card%d/codecs/tasha/version", card_num);
2049 if ((fp = fopen(procfs_path, "r")) != NULL) {
2050 fgets(codec_version, CODEC_VERSION_MAX_LENGTH, fp);
2051 fclose(fp);
2052 } else {
2053 ALOGE("%s: ERROR. cannot open %s", __func__, procfs_path);
2054 return -ENOENT;
2055 }
2056 ALOGD("%s: codec version %s", __func__, codec_version);
2057 }
2058
2059 return 0;
2060}
2061
Preetam Singh Ranawat9d0d8e42019-07-15 12:27:25 +05302062int audio_extn_utils_get_codec_variant(int card_num,
2063 char *codec_variant)
2064{
2065 char procfs_path[50];
2066 FILE *fp;
2067 snprintf(procfs_path, sizeof(procfs_path),
2068 "/proc/asound/card%d/codecs/wcd938x/variant", card_num);
2069 if ((fp = fopen(procfs_path, "r")) == NULL) {
2070 snprintf(procfs_path, sizeof(procfs_path),
2071 "/proc/asound/card%d/codecs/wcd937x/variant", card_num);
2072 if ((fp = fopen(procfs_path, "r")) == NULL) {
2073 ALOGE("%s: ERROR. cannot open %s", __func__, procfs_path);
2074 return -ENOENT;
2075 }
2076 }
2077 fgets(codec_variant, CODEC_VARIANT_MAX_LENGTH, fp);
2078 fclose(fp);
2079 ALOGD("%s: codec variant is %s", __func__, codec_variant);
2080 return 0;
2081}
2082
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05302083
Ashish Jain81eb2a82015-05-13 10:52:34 +05302084#ifdef AUDIO_EXTERNAL_HDMI_ENABLED
2085
2086void get_default_compressed_channel_status(
2087 unsigned char *channel_status)
2088{
Ashish Jain81eb2a82015-05-13 10:52:34 +05302089 memset(channel_status,0,24);
2090
2091 /* block start bit in preamble bit 3 */
2092 channel_status[0] |= PROFESSIONAL;
2093 //compre out
2094 channel_status[0] |= NON_LPCM;
2095 // sample rate; fixed 48K for default/transcode
2096 channel_status[3] |= SR_48000;
2097}
2098
Ashish Jain81eb2a82015-05-13 10:52:34 +05302099int32_t get_compressed_channel_status(void *audio_stream_data,
2100 uint32_t audio_frame_size,
2101 unsigned char *channel_status,
2102 enum audio_parser_code_type codec_type)
2103 // codec_type - AUDIO_PARSER_CODEC_AC3
2104 // - AUDIO_PARSER_CODEC_DTS
2105{
2106 unsigned char *stream;
2107 int ret = 0;
2108 stream = (unsigned char *)audio_stream_data;
2109
2110 if (audio_stream_data == NULL || audio_frame_size == 0) {
2111 ALOGW("no buffer to get channel status, return default for compress");
2112 get_default_compressed_channel_status(channel_status);
2113 return ret;
2114 }
2115
2116 memset(channel_status,0,24);
2117 if(init_audio_parser(stream, audio_frame_size, codec_type) == -1)
2118 {
2119 ALOGE("init audio parser failed");
2120 return -1;
2121 }
2122 ret = get_channel_status(channel_status, codec_type);
2123 return ret;
2124
2125}
2126
Ashish Jain81eb2a82015-05-13 10:52:34 +05302127void get_lpcm_channel_status(uint32_t sampleRate,
2128 unsigned char *channel_status)
2129{
2130 int32_t status = 0;
Ashish Jain81eb2a82015-05-13 10:52:34 +05302131 memset(channel_status,0,24);
2132 /* block start bit in preamble bit 3 */
2133 channel_status[0] |= PROFESSIONAL;
2134 //LPCM OUT
2135 channel_status[0] &= ~NON_LPCM;
2136
2137 switch (sampleRate) {
2138 case 8000:
2139 case 11025:
2140 case 12000:
2141 case 16000:
2142 case 22050:
2143 channel_status[3] |= SR_NOTID;
Preetam Singh Ranawat61716b12015-12-14 11:55:24 +05302144 break;
Ashish Jain81eb2a82015-05-13 10:52:34 +05302145 case 24000:
2146 channel_status[3] |= SR_24000;
2147 break;
2148 case 32000:
2149 channel_status[3] |= SR_32000;
2150 break;
2151 case 44100:
2152 channel_status[3] |= SR_44100;
2153 break;
2154 case 48000:
2155 channel_status[3] |= SR_48000;
2156 break;
2157 case 88200:
2158 channel_status[3] |= SR_88200;
2159 break;
2160 case 96000:
2161 channel_status[3] |= SR_96000;
2162 break;
2163 case 176400:
2164 channel_status[3] |= SR_176400;
2165 break;
2166 case 192000:
2167 channel_status[3] |= SR_192000;
2168 break;
2169 default:
2170 ALOGV("Invalid sample_rate %u\n", sampleRate);
2171 status = -1;
2172 break;
2173 }
2174}
2175
2176void audio_utils_set_hdmi_channel_status(struct stream_out *out, char * buffer, size_t bytes)
2177{
2178 unsigned char channel_status[24]={0};
2179 struct snd_aes_iec958 iec958;
2180 const char *mixer_ctl_name = "IEC958 Playback PCM Stream";
2181 struct mixer_ctl *ctl;
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +05302182 ALOGV("%s: buffer %s bytes %zd", __func__, buffer, bytes);
Arun Mirpurie008ed22019-03-21 11:21:04 -07002183
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05302184 if (audio_extn_utils_is_dolby_format(out->format) &&
Ashish Jain81eb2a82015-05-13 10:52:34 +05302185 /*TODO:Extend code to support DTS passthrough*/
2186 /*set compressed channel status bits*/
Arun Mirpurie008ed22019-03-21 11:21:04 -07002187 audio_extn_passthru_is_passthrough_stream(out) &&
2188 audio_extn_is_hdmi_passthru_enabled()) {
Ashish Jain81eb2a82015-05-13 10:52:34 +05302189 get_compressed_channel_status(buffer, bytes, channel_status, AUDIO_PARSER_CODEC_AC3);
Arun Mirpurie008ed22019-03-21 11:21:04 -07002190 } else {
Ashish Jain81eb2a82015-05-13 10:52:34 +05302191 /*set channel status bit for LPCM*/
2192 get_lpcm_channel_status(out->sample_rate, channel_status);
2193 }
2194
2195 memcpy(iec958.status, channel_status,sizeof(iec958.status));
2196 ctl = mixer_get_ctl_by_name(out->dev->mixer, mixer_ctl_name);
2197 if (!ctl) {
2198 ALOGE("%s: Could not get ctl for mixer cmd - %s",
2199 __func__, mixer_ctl_name);
2200 return;
2201 }
2202 if (mixer_ctl_set_array(ctl, &iec958, sizeof(iec958)) < 0) {
2203 ALOGE("%s: Could not set channel status for ext HDMI ",
2204 __func__);
2205 return;
2206 }
2207
2208}
2209#endif
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302210
2211int audio_extn_utils_get_avt_device_drift(
2212 struct audio_usecase *usecase,
2213 struct audio_avt_device_drift_param *drift_param)
2214{
2215 int ret = 0, count = 0;
2216 char avt_device_drift_mixer_ctl_name[MIXER_PATH_MAX_LENGTH] = {0};
Naresh Tanniru6160c712017-04-17 15:43:48 +05302217 const char *backend = NULL;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302218 struct mixer_ctl *ctl = NULL;
2219 struct audio_avt_device_drift_stats drift_stats;
2220 struct audio_device *adev = NULL;
2221
2222 if (usecase != NULL && usecase->type == PCM_PLAYBACK) {
Naresh Tanniru6160c712017-04-17 15:43:48 +05302223 backend = platform_get_snd_device_backend_interface(usecase->out_snd_device);
2224 if (!backend) {
2225 ALOGE("%s: Unsupported device %d", __func__,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08002226 get_device_types(&usecase->stream.out->device_list));
Naresh Tanniru6160c712017-04-17 15:43:48 +05302227 ret = -EINVAL;
2228 goto done;
2229 }
2230 strlcpy(avt_device_drift_mixer_ctl_name,
2231 backend,
2232 MIXER_PATH_MAX_LENGTH);
2233
2234 count = strlen(backend);
2235 if (MIXER_PATH_MAX_LENGTH - count > 0) {
2236 strlcat(&avt_device_drift_mixer_ctl_name[count],
2237 " DRIFT",
2238 MIXER_PATH_MAX_LENGTH - count);
2239 } else {
2240 ret = -EINVAL;
2241 goto done;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302242 }
2243 } else {
Naresh Tanniru7586e292017-04-13 10:38:13 +05302244 ALOGE("%s: Invalid usecase",__func__);
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302245 ret = -EINVAL;
Naresh Tanniru6160c712017-04-17 15:43:48 +05302246 goto done;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302247 }
2248
Naresh Tanniru6160c712017-04-17 15:43:48 +05302249 adev = usecase->stream.out->dev;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302250 ctl = mixer_get_ctl_by_name(adev->mixer, avt_device_drift_mixer_ctl_name);
2251 if (!ctl) {
2252 ALOGE("%s: Could not get ctl for mixer cmd - %s",
2253 __func__, avt_device_drift_mixer_ctl_name);
2254
2255 ret = -EINVAL;
2256 goto done;
2257 }
2258
2259 ALOGV("%s: Getting AV Timer vs Device Drift mixer ctrl name %s", __func__,
2260 avt_device_drift_mixer_ctl_name);
2261
2262 mixer_ctl_update(ctl);
2263 count = mixer_ctl_get_num_values(ctl);
2264 if (count != sizeof(struct audio_avt_device_drift_stats)) {
2265 ALOGE("%s: mixer_ctl_get_num_values() invalid drift_stats data size",
2266 __func__);
2267
2268 ret = -EINVAL;
2269 goto done;
2270 }
2271
2272 ret = mixer_ctl_get_array(ctl, (void *)&drift_stats, count);
2273 if (ret != 0) {
2274 ALOGE("%s: mixer_ctl_get_array() failed to get drift_stats Params",
2275 __func__);
2276
2277 ret = -EINVAL;
2278 goto done;
2279 }
2280 memcpy(drift_param, &drift_stats.drift_param,
2281 sizeof(struct audio_avt_device_drift_param));
2282done:
2283 return ret;
2284}
Manish Dewangan07de2142017-02-27 19:27:20 +05302285
2286#ifdef SNDRV_COMPRESS_PATH_DELAY
2287int audio_extn_utils_compress_get_dsp_latency(struct stream_out *out)
2288{
2289 int ret = -EINVAL;
2290 struct snd_compr_metadata metadata;
2291 int delay_ms = COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
2292
Weiyin Jiangc49a3b52018-08-17 16:16:08 +08002293 /* override the latency for pcm offload use case */
2294 if ((out->flags & AUDIO_OUTPUT_FLAG_DIRECT) &&
2295 !(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)) {
2296 delay_ms = PCM_OFFLOAD_PLAYBACK_LATENCY;
2297 }
2298
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -07002299 if (property_get_bool("vendor.audio.playback.dsp.pathdelay", false)) {
Manish Dewangan07de2142017-02-27 19:27:20 +05302300 ALOGD("%s:: Quering DSP delay %d",__func__, __LINE__);
2301 if (!(is_offload_usecase(out->usecase))) {
2302 ALOGE("%s:: not supported for non offload session", __func__);
2303 goto exit;
2304 }
2305
2306 if (!out->compr) {
2307 ALOGD("%s:: Invalid compress handle,returning default dsp latency",
2308 __func__);
2309 goto exit;
2310 }
2311
2312 metadata.key = SNDRV_COMPRESS_PATH_DELAY;
2313 ret = compress_get_metadata(out->compr, &metadata);
2314 if(ret) {
2315 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2316 goto exit;
2317 }
2318 delay_ms = metadata.value[0] / 1000; /*convert to ms*/
2319 } else {
2320 ALOGD("%s:: Using Fix DSP delay",__func__);
2321 }
2322
2323exit:
2324 ALOGD("%s:: delay in ms is %d",__func__, delay_ms);
2325 return delay_ms;
2326}
2327#else
2328int audio_extn_utils_compress_get_dsp_latency(struct stream_out *out __unused)
2329{
2330 return COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
2331}
2332#endif
Manish Dewangan69426c82017-01-30 17:35:36 +05302333
2334#ifdef SNDRV_COMPRESS_RENDER_MODE
2335int audio_extn_utils_compress_set_render_mode(struct stream_out *out)
2336{
2337 struct snd_compr_metadata metadata;
2338 int ret = -EINVAL;
2339
2340 if (!(is_offload_usecase(out->usecase))) {
2341 ALOGE("%s:: not supported for non offload session", __func__);
2342 goto exit;
2343 }
2344
2345 if (!out->compr) {
2346 ALOGD("%s:: Invalid compress handle",
2347 __func__);
2348 goto exit;
2349 }
2350
2351 ALOGD("%s:: render mode %d", __func__, out->render_mode);
2352
2353 metadata.key = SNDRV_COMPRESS_RENDER_MODE;
2354 if (out->render_mode == RENDER_MODE_AUDIO_MASTER) {
2355 metadata.value[0] = SNDRV_COMPRESS_RENDER_MODE_AUDIO_MASTER;
2356 } else if (out->render_mode == RENDER_MODE_AUDIO_STC_MASTER) {
2357 metadata.value[0] = SNDRV_COMPRESS_RENDER_MODE_STC_MASTER;
2358 } else {
2359 ret = 0;
2360 goto exit;
2361 }
2362 ret = compress_set_metadata(out->compr, &metadata);
2363 if(ret) {
2364 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2365 }
2366exit:
2367 return ret;
2368}
2369#else
2370int audio_extn_utils_compress_set_render_mode(struct stream_out *out __unused)
2371{
2372 ALOGD("%s:: configuring render mode not supported", __func__);
2373 return 0;
2374}
2375#endif
Manish Dewangan58229382017-02-02 15:48:41 +05302376
2377#ifdef SNDRV_COMPRESS_CLK_REC_MODE
2378int audio_extn_utils_compress_set_clk_rec_mode(
2379 struct audio_usecase *usecase)
2380{
2381 struct snd_compr_metadata metadata;
2382 struct stream_out *out = NULL;
2383 int ret = -EINVAL;
2384
Naresh Tanniru7586e292017-04-13 10:38:13 +05302385 if (usecase == NULL || usecase->type != PCM_PLAYBACK) {
Manish Dewangan58229382017-02-02 15:48:41 +05302386 ALOGE("%s:: Invalid use case", __func__);
2387 goto exit;
2388 }
2389
2390 out = usecase->stream.out;
2391 if (!out) {
2392 ALOGE("%s:: invalid stream", __func__);
2393 goto exit;
2394 }
2395
2396 if (!is_offload_usecase(out->usecase)) {
2397 ALOGE("%s:: not supported for non offload session", __func__);
2398 goto exit;
2399 }
2400
2401 if (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER) {
2402 ALOGD("%s:: clk recovery is only supported in STC render mode",
2403 __func__);
2404 ret = 0;
2405 goto exit;
2406 }
2407
2408 if (!out->compr) {
2409 ALOGD("%s:: Invalid compress handle",
2410 __func__);
2411 goto exit;
2412 }
2413 metadata.key = SNDRV_COMPRESS_CLK_REC_MODE;
2414 switch(usecase->out_snd_device) {
2415 case SND_DEVICE_OUT_HDMI:
2416 case SND_DEVICE_OUT_SPEAKER_AND_HDMI:
2417 case SND_DEVICE_OUT_DISPLAY_PORT:
2418 case SND_DEVICE_OUT_SPEAKER_AND_DISPLAY_PORT:
2419 metadata.value[0] = SNDRV_COMPRESS_CLK_REC_MODE_NONE;
2420 break;
2421 default:
2422 metadata.value[0] = SNDRV_COMPRESS_CLK_REC_MODE_AUTO;
2423 break;
2424 }
2425
2426 ALOGD("%s:: clk recovery mode %d",__func__, metadata.value[0]);
2427
2428 ret = compress_set_metadata(out->compr, &metadata);
2429 if(ret) {
2430 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2431 }
2432
2433exit:
2434 return ret;
2435}
2436#else
2437int audio_extn_utils_compress_set_clk_rec_mode(
2438 struct audio_usecase *usecase __unused)
2439{
2440 ALOGD("%s:: configuring render mode not supported", __func__);
2441 return 0;
2442}
2443#endif
Manish Dewangan27346042017-03-01 12:56:12 +05302444
2445#ifdef SNDRV_COMPRESS_RENDER_WINDOW
2446int audio_extn_utils_compress_set_render_window(
2447 struct stream_out *out,
2448 struct audio_out_render_window_param *render_window)
2449{
2450 struct snd_compr_metadata metadata;
2451 int ret = -EINVAL;
2452
Manish Dewangan27346042017-03-01 12:56:12 +05302453 if(render_window == NULL) {
2454 ALOGE("%s:: Invalid render_window", __func__);
2455 goto exit;
2456 }
2457
Aniket Kumar Lata1e1d3662017-06-01 18:45:48 -07002458 ALOGD("%s:: render window start 0x%"PRIx64" end 0x%"PRIx64"",
2459 __func__,render_window->render_ws, render_window->render_we);
2460
Manish Dewangan27346042017-03-01 12:56:12 +05302461 if (!is_offload_usecase(out->usecase)) {
2462 ALOGE("%s:: not supported for non offload session", __func__);
2463 goto exit;
2464 }
2465
Naresh Tanniru6160c712017-04-17 15:43:48 +05302466 if ((out->render_mode != RENDER_MODE_AUDIO_MASTER) &&
2467 (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER)) {
Manish Dewangan27346042017-03-01 12:56:12 +05302468 ALOGD("%s:: only supported in timestamp mode, current "
2469 "render mode mode %d", __func__, out->render_mode);
2470 goto exit;
2471 }
2472
2473 if (!out->compr) {
2474 ALOGW("%s:: offload session not yet opened,"
2475 "render window will be configure later", __func__);
2476 /* store render window to reconfigure in start_output_stream() */
2477 goto exit;
2478 }
2479
2480 metadata.key = SNDRV_COMPRESS_RENDER_WINDOW;
2481 /*render window start value */
2482 metadata.value[0] = 0xFFFFFFFF & render_window->render_ws; /* lsb */
2483 metadata.value[1] = \
2484 (0xFFFFFFFF00000000 & render_window->render_ws) >> 32; /* msb*/
2485 /*render window end value */
2486 metadata.value[2] = 0xFFFFFFFF & render_window->render_we; /* lsb */
2487 metadata.value[3] = \
2488 (0xFFFFFFFF00000000 & render_window->render_we) >> 32; /* msb*/
2489
2490 ret = compress_set_metadata(out->compr, &metadata);
2491 if(ret) {
2492 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2493 }
2494
2495exit:
2496 return ret;
2497}
2498#else
2499int audio_extn_utils_compress_set_render_window(
2500 struct stream_out *out __unused,
2501 struct audio_out_render_window_param *render_window __unused)
2502{
2503 ALOGD("%s:: configuring render window not supported", __func__);
2504 return 0;
2505}
2506#endif
Manish Dewangan14956cc2017-02-14 18:54:42 +05302507
2508#ifdef SNDRV_COMPRESS_START_DELAY
2509int audio_extn_utils_compress_set_start_delay(
2510 struct stream_out *out,
2511 struct audio_out_start_delay_param *delay_param)
2512{
2513 struct snd_compr_metadata metadata;
2514 int ret = -EINVAL;
2515
2516 if(delay_param == NULL) {
2517 ALOGE("%s:: Invalid delay_param", __func__);
2518 goto exit;
2519 }
2520
2521 ALOGD("%s:: render start delay 0x%"PRIx64" ", __func__,
2522 delay_param->start_delay);
2523
2524 if (!is_offload_usecase(out->usecase)) {
2525 ALOGE("%s:: not supported for non offload session", __func__);
2526 goto exit;
2527 }
2528
Naresh Tanniru6160c712017-04-17 15:43:48 +05302529 if ((out->render_mode != RENDER_MODE_AUDIO_MASTER) &&
2530 (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER)) {
Manish Dewangan14956cc2017-02-14 18:54:42 +05302531 ALOGD("%s:: only supported in timestamp mode, current "
2532 "render mode mode %d", __func__, out->render_mode);
2533 goto exit;
2534 }
2535
2536 if (!out->compr) {
2537 ALOGW("%s:: offload session not yet opened,"
2538 "start delay will be configure later", __func__);
2539 goto exit;
2540 }
2541
2542 metadata.key = SNDRV_COMPRESS_START_DELAY;
2543 metadata.value[0] = 0xFFFFFFFF & delay_param->start_delay; /* lsb */
2544 metadata.value[1] = \
2545 (0xFFFFFFFF00000000 & delay_param->start_delay) >> 32; /* msb*/
2546
2547 ret = compress_set_metadata(out->compr, &metadata);
2548 if(ret) {
2549 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2550 }
2551
2552exit:
2553 return ret;
2554}
2555#else
2556int audio_extn_utils_compress_set_start_delay(
2557 struct stream_out *out __unused,
2558 struct audio_out_start_delay_param *delay_param __unused)
2559{
2560 ALOGD("%s:: configuring render window not supported", __func__);
2561 return 0;
2562}
2563#endif
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002564
Surendar Karka287348c2019-04-10 18:31:46 +05302565#ifdef SNDRV_COMPRESS_DSP_POSITION
2566int audio_extn_utils_compress_get_dsp_presentation_pos(struct stream_out *out,
2567 uint64_t *frames, struct timespec *timestamp, int32_t clock_id)
2568{
2569 int ret = -EINVAL;
2570 uint64_t *val = NULL;
2571 uint64_t time = 0;
2572 struct snd_compr_metadata metadata;
2573
2574 ALOGV("%s:: Quering DSP position with clock id %d",__func__, clock_id);
2575 metadata.key = SNDRV_COMPRESS_DSP_POSITION;
2576 metadata.value[0] = clock_id;
2577 ret = compress_get_metadata(out->compr, &metadata);
2578 if (ret) {
2579 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2580 ret = -errno;
2581 goto exit;
2582 }
2583 val = (uint64_t *)&metadata.value[1];
2584 *frames = *val;
2585 time = *(val + 1);
2586 timestamp->tv_sec = time / 1000000;
2587 timestamp->tv_nsec = (time % 1000000)*1000;
2588
2589exit:
2590 return ret;
2591}
2592#else
2593int audio_extn_utils_compress_get_dsp_presentation_pos(struct stream_out *out __unused,
2594 uint64_t *frames __unused, struct timespec *timestamp __unused,
2595 int32_t clock_id __unused)
2596{
2597 ALOGD("%s:: dsp presentation position not supported", __func__);
2598 return 0;
2599
2600}
2601#endif
2602
2603#ifdef SNDRV_PCM_IOCTL_DSP_POSITION
2604int audio_extn_utils_pcm_get_dsp_presentation_pos(struct stream_out *out,
2605 uint64_t *frames, struct timespec *timestamp, int32_t clock_id)
2606{
2607 int ret = -EINVAL;
2608 uint64_t time = 0;
2609 struct snd_pcm_prsnt_position prsnt_position;
2610
2611 ALOGV("%s:: Quering DSP position with clock id %d",__func__, clock_id);
2612 prsnt_position.clock_id = clock_id;
2613 ret = pcm_ioctl(out->pcm, SNDRV_PCM_IOCTL_DSP_POSITION, &prsnt_position);
2614 if (ret) {
2615 ALOGE("%s::error %d", __func__, ret);
2616 ret = -EIO;
2617 goto exit;
2618 }
2619
2620 *frames = prsnt_position.frames;
2621 time = prsnt_position.timestamp;
2622 timestamp->tv_sec = time / 1000000;
2623 timestamp->tv_nsec = (time % 1000000)*1000;
2624
2625exit:
2626 return ret;
2627}
2628#else
2629int audio_extn_utils_pcm_get_dsp_presentation_pos(struct stream_out *out __unused,
2630 uint64_t *frames __unused, struct timespec *timestamp __unused,
2631 int32_t clock_id __unused)
2632{
2633 ALOGD("%s:: dsp presentation position not supported", __func__);
2634 return 0;
2635
2636}
2637#endif
2638
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002639#define MAX_SND_CARD 8
Ashish Jain30ae8942017-11-05 17:21:23 +05302640#define RETRY_US 1000000
2641#define RETRY_NUMBER 40
Aalique Grahame22e49102018-12-18 14:23:57 -08002642#define PLATFORM_INFO_XML_PATH "audio_platform_info.xml"
2643#define PLATFORM_INFO_XML_BASE_STRING "audio_platform_info"
2644
Aalique Grahame22e49102018-12-18 14:23:57 -08002645bool audio_extn_utils_resolve_config_file(char file_name[MIXER_PATH_MAX_LENGTH])
2646{
2647 char full_config_path[MIXER_PATH_MAX_LENGTH];
Saurav Kumarf5d2c342020-07-29 19:31:49 +05302648 char vendor_config_path[VENDOR_CONFIG_PATH_MAX_LENGTH];
2649
2650 /* Get path for audio configuration files in vendor */
2651 audio_get_vendor_config_path(vendor_config_path,
2652 sizeof(vendor_config_path));
2653 snprintf(full_config_path, sizeof(full_config_path),
2654 "%s/%s", vendor_config_path, file_name);
2655 if (F_OK == access(full_config_path, 0)) {
2656 strlcpy(file_name, full_config_path, MIXER_PATH_MAX_LENGTH);
2657 return true;
Aalique Grahame22e49102018-12-18 14:23:57 -08002658 }
2659 return false;
2660}
2661
2662/* platform_info_file should be size 'MIXER_PATH_MAX_LENGTH' */
2663int audio_extn_utils_get_platform_info(const char* snd_card_name, char* platform_info_file)
2664{
2665 if (NULL == snd_card_name) {
2666 return -1;
2667 }
2668
2669 struct snd_card_split *snd_split_handle = NULL;
2670 int ret = 0;
2671 audio_extn_set_snd_card_split(snd_card_name);
2672 snd_split_handle = audio_extn_get_snd_card_split();
2673
2674 snprintf(platform_info_file, MIXER_PATH_MAX_LENGTH, "%s_%s_%s.xml",
2675 PLATFORM_INFO_XML_BASE_STRING, snd_split_handle->snd_card,
2676 snd_split_handle->form_factor);
2677
2678 if (!audio_extn_utils_resolve_config_file(platform_info_file)) {
2679 memset(platform_info_file, 0, MIXER_PATH_MAX_LENGTH);
2680 snprintf(platform_info_file, MIXER_PATH_MAX_LENGTH, "%s_%s.xml",
2681 PLATFORM_INFO_XML_BASE_STRING, snd_split_handle->snd_card);
2682
2683 if (!audio_extn_utils_resolve_config_file(platform_info_file)) {
2684 memset(platform_info_file, 0, MIXER_PATH_MAX_LENGTH);
2685 strlcpy(platform_info_file, PLATFORM_INFO_XML_PATH, MIXER_PATH_MAX_LENGTH);
2686 ret = audio_extn_utils_resolve_config_file(platform_info_file) ? 0 : -1;
2687 }
2688 }
2689
2690 return ret;
2691}
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002692
2693int audio_extn_utils_get_snd_card_num()
2694{
Soumya Managoli9fee7c62018-04-06 16:21:50 +05302695 int snd_card_num = 0;
2696 struct mixer *mixer = NULL;
2697
2698 snd_card_num = audio_extn_utils_open_snd_mixer(&mixer);
2699 if (mixer)
2700 mixer_close(mixer);
2701 return snd_card_num;
2702}
2703
2704int audio_extn_utils_open_snd_mixer(struct mixer **mixer_handle)
2705{
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002706
2707 void *hw_info = NULL;
2708 struct mixer *mixer = NULL;
2709 int retry_num = 0;
mpangfaa7bae2019-01-15 16:38:13 +08002710 int snd_card_num = 0;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002711 char* snd_card_name = NULL;
mpangfaa7bae2019-01-15 16:38:13 +08002712 int snd_card_detection_info[MAX_SND_CARD] = {0};
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002713
Soumya Managoli9fee7c62018-04-06 16:21:50 +05302714 if (!mixer_handle) {
2715 ALOGE("invalid mixer handle");
2716 return -1;
2717 }
2718 *mixer_handle = NULL;
Ashish Jain30ae8942017-11-05 17:21:23 +05302719 /*
mpangfaa7bae2019-01-15 16:38:13 +08002720 * Try with all the sound cards ( 0 to 7 ) and if none of them were detected
Ashish Jain30ae8942017-11-05 17:21:23 +05302721 * sleep for 1 sec and try detections with sound card 0 again.
2722 * If sound card gets detected, check if it is relevant, if not check with the
2723 * other sound cards. To ensure that the irrelevant sound card is not check again,
2724 * we maintain it in min_snd_card_num.
2725 */
2726 while (retry_num < RETRY_NUMBER) {
mpangfaa7bae2019-01-15 16:38:13 +08002727 snd_card_num = 0;
2728 while (snd_card_num < MAX_SND_CARD) {
2729 if (snd_card_detection_info[snd_card_num] == 0) {
2730 mixer = mixer_open(snd_card_num);
2731 if (!mixer)
2732 snd_card_num++;
2733 else
2734 break;
2735 } else
2736 snd_card_num++;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002737 }
2738
2739 if (!mixer) {
Ashish Jain30ae8942017-11-05 17:21:23 +05302740 usleep(RETRY_US);
Ashish Jain30ae8942017-11-05 17:21:23 +05302741 retry_num++;
mpangfaa7bae2019-01-15 16:38:13 +08002742 ALOGD("%s: retry, retry_num %d", __func__, retry_num);
Ashish Jain30ae8942017-11-05 17:21:23 +05302743 continue;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002744 }
2745
2746 snd_card_name = strdup(mixer_get_name(mixer));
2747 if (!snd_card_name) {
2748 ALOGE("failed to allocate memory for snd_card_name\n");
2749 mixer_close(mixer);
2750 return -1;
2751 }
2752 ALOGD("%s: snd_card_name: %s", __func__, snd_card_name);
mpangfaa7bae2019-01-15 16:38:13 +08002753 snd_card_detection_info[snd_card_num] = 1;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002754 hw_info = hw_info_init(snd_card_name);
2755 if (hw_info) {
2756 ALOGD("%s: Opened sound card:%d", __func__, snd_card_num);
2757 break;
2758 }
mpangfaa7bae2019-01-15 16:38:13 +08002759 ALOGE("%s: Failed to init hardware info, snd_card_num:%d", __func__, snd_card_num);
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002760
Dhananjay Kumara7e27832017-07-11 15:40:39 +05302761 free(snd_card_name);
2762 snd_card_name = NULL;
2763
2764 mixer_close(mixer);
2765 mixer = NULL;
2766 }
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002767 if (snd_card_name)
2768 free(snd_card_name);
Ashish Jain30ae8942017-11-05 17:21:23 +05302769
Dhananjay Kumara7e27832017-07-11 15:40:39 +05302770 if (hw_info)
2771 hw_info_deinit(hw_info);
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002772
mpangfaa7bae2019-01-15 16:38:13 +08002773 if (retry_num >= RETRY_NUMBER) {
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002774 ALOGE("%s: Unable to find correct sound card, aborting.", __func__);
2775 return -1;
2776 }
2777
Soumya Managoli9fee7c62018-04-06 16:21:50 +05302778 if (mixer)
2779 *mixer_handle = mixer;
2780
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002781 return snd_card_num;
2782}
Naresh Tanniru6160c712017-04-17 15:43:48 +05302783
Soumya Managoli9fee7c62018-04-06 16:21:50 +05302784void audio_extn_utils_close_snd_mixer(struct mixer *mixer)
2785{
2786 if (mixer)
2787 mixer_close(mixer);
2788}
2789
Naresh Tanniru6160c712017-04-17 15:43:48 +05302790#ifdef SNDRV_COMPRESS_ENABLE_ADJUST_SESSION_CLOCK
2791int audio_extn_utils_compress_enable_drift_correction(
2792 struct stream_out *out,
2793 struct audio_out_enable_drift_correction *drift)
2794{
2795 struct snd_compr_metadata metadata;
2796 int ret = -EINVAL;
2797
2798 if(drift == NULL) {
2799 ALOGE("%s:: Invalid param", __func__);
2800 goto exit;
2801 }
2802
2803 ALOGD("%s:: drift enable %d", __func__,drift->enable);
2804
2805 if (!is_offload_usecase(out->usecase)) {
2806 ALOGE("%s:: not supported for non offload session", __func__);
2807 goto exit;
2808 }
2809
2810 if (!out->compr) {
2811 ALOGW("%s:: offload session not yet opened,"
2812 "start delay will be configure later", __func__);
2813 goto exit;
2814 }
2815
2816 metadata.key = SNDRV_COMPRESS_ENABLE_ADJUST_SESSION_CLOCK;
2817 metadata.value[0] = drift->enable;
2818 out->drift_correction_enabled = drift->enable;
2819
2820 ret = compress_set_metadata(out->compr, &metadata);
2821 if(ret) {
2822 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2823 out->drift_correction_enabled = false;
2824 }
2825
2826exit:
2827 return ret;
2828}
2829#else
2830int audio_extn_utils_compress_enable_drift_correction(
2831 struct stream_out *out __unused,
2832 struct audio_out_enable_drift_correction *drift __unused)
2833{
2834 ALOGD("%s:: configuring drift enablement not supported", __func__);
2835 return 0;
2836}
2837#endif
2838
2839#ifdef SNDRV_COMPRESS_ADJUST_SESSION_CLOCK
2840int audio_extn_utils_compress_correct_drift(
2841 struct stream_out *out,
2842 struct audio_out_correct_drift *drift_param)
2843{
2844 struct snd_compr_metadata metadata;
2845 int ret = -EINVAL;
2846
2847 if (drift_param == NULL) {
2848 ALOGE("%s:: Invalid drift_param", __func__);
2849 goto exit;
2850 }
2851
2852 ALOGD("%s:: adjust time 0x%"PRIx64" ", __func__,
2853 drift_param->adjust_time);
2854
2855 if (!is_offload_usecase(out->usecase)) {
2856 ALOGE("%s:: not supported for non offload session", __func__);
2857 goto exit;
2858 }
2859
2860 if (!out->compr) {
2861 ALOGW("%s:: offload session not yet opened", __func__);
2862 goto exit;
2863 }
2864
2865 if (!out->drift_correction_enabled) {
2866 ALOGE("%s:: drift correction not enabled", __func__);
2867 goto exit;
2868 }
2869
2870 metadata.key = SNDRV_COMPRESS_ADJUST_SESSION_CLOCK;
2871 metadata.value[0] = 0xFFFFFFFF & drift_param->adjust_time; /* lsb */
2872 metadata.value[1] = \
2873 (0xFFFFFFFF00000000 & drift_param->adjust_time) >> 32; /* msb*/
2874
2875 ret = compress_set_metadata(out->compr, &metadata);
2876 if(ret)
2877 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2878exit:
2879 return ret;
2880}
2881#else
2882int audio_extn_utils_compress_correct_drift(
2883 struct stream_out *out __unused,
2884 struct audio_out_correct_drift *drift_param __unused)
2885{
2886 ALOGD("%s:: setting adjust clock not supported", __func__);
2887 return 0;
2888}
2889#endif
Naresh Tanniru29bce4e2017-04-27 17:54:30 +05302890
2891int audio_extn_utils_set_channel_map(
2892 struct stream_out *out,
2893 struct audio_out_channel_map_param *channel_map_param)
2894{
2895 int ret = -EINVAL, i = 0;
2896 int channels = audio_channel_count_from_out_mask(out->channel_mask);
2897
2898 if (channel_map_param == NULL) {
2899 ALOGE("%s:: Invalid channel_map", __func__);
2900 goto exit;
2901 }
2902
2903 if (channel_map_param->channels != channels) {
2904 ALOGE("%s:: Channels(%d) does not match stream channels(%d)",
2905 __func__, channel_map_param->channels, channels);
2906 goto exit;
2907 }
2908
2909 for ( i = 0; i < channels; i++) {
2910 ALOGV("%s:: channel_map[%d]- %d", __func__, i, channel_map_param->channel_map[i]);
2911 out->channel_map_param.channel_map[i] = channel_map_param->channel_map[i];
2912 }
2913 ret = 0;
2914exit:
2915 return ret;
2916}
Varun Balaraje49253e2017-07-06 19:48:56 +05302917
2918int audio_extn_utils_set_pan_scale_params(
2919 struct stream_out *out,
2920 struct mix_matrix_params *mm_params)
2921{
2922 int ret = -EINVAL, i = 0, j = 0;
2923
Varun Balaraj003ee752017-08-07 17:54:55 +05302924 if (mm_params == NULL || out == NULL) {
2925 ALOGE("%s:: Invalid mix matrix or out param", __func__);
Varun Balaraje49253e2017-07-06 19:48:56 +05302926 goto exit;
2927 }
2928
2929 if (mm_params->num_output_channels > MAX_CHANNELS_SUPPORTED ||
2930 mm_params->num_output_channels <= 0 ||
2931 mm_params->num_input_channels > MAX_CHANNELS_SUPPORTED ||
2932 mm_params->num_input_channels <= 0)
2933 goto exit;
2934
2935 out->pan_scale_params.num_output_channels = mm_params->num_output_channels;
2936 out->pan_scale_params.num_input_channels = mm_params->num_input_channels;
2937 out->pan_scale_params.has_output_channel_map =
2938 mm_params->has_output_channel_map;
2939 for (i = 0; i < mm_params->num_output_channels; i++)
2940 out->pan_scale_params.output_channel_map[i] =
2941 mm_params->output_channel_map[i];
2942
2943 out->pan_scale_params.has_input_channel_map =
2944 mm_params->has_input_channel_map;
2945 for (i = 0; i < mm_params->num_input_channels; i++)
2946 out->pan_scale_params.input_channel_map[i] =
2947 mm_params->input_channel_map[i];
2948
2949 out->pan_scale_params.has_mixer_coeffs = mm_params->has_mixer_coeffs;
2950 for (i = 0; i < mm_params->num_output_channels; i++)
2951 for (j = 0; j < mm_params->num_input_channels; j++) {
2952 //Convert the channel coefficient gains in Q14 format
2953 out->pan_scale_params.mixer_coeffs[i][j] =
2954 mm_params->mixer_coeffs[i][j] * (2 << 13);
2955 }
2956
2957 ret = platform_set_stream_pan_scale_params(out->dev->platform,
2958 out->pcm_device_id,
2959 out->pan_scale_params);
2960
2961exit:
2962 return ret;
2963}
2964
2965int audio_extn_utils_set_downmix_params(
2966 struct stream_out *out,
2967 struct mix_matrix_params *mm_params)
2968{
2969 int ret = -EINVAL, i = 0, j = 0;
2970 struct audio_usecase *usecase = NULL;
2971
Aniket Kumar Lataf9f246e2017-09-15 15:20:16 -07002972 if (mm_params == NULL || out == NULL) {
Varun Balaraj003ee752017-08-07 17:54:55 +05302973 ALOGE("%s:: Invalid mix matrix or out param", __func__);
Varun Balaraje49253e2017-07-06 19:48:56 +05302974 goto exit;
2975 }
2976
2977 if (mm_params->num_output_channels > MAX_CHANNELS_SUPPORTED ||
2978 mm_params->num_output_channels <= 0 ||
2979 mm_params->num_input_channels > MAX_CHANNELS_SUPPORTED ||
2980 mm_params->num_input_channels <= 0)
2981 goto exit;
2982
2983 usecase = get_usecase_from_list(out->dev, out->usecase);
Varun Balaraj003ee752017-08-07 17:54:55 +05302984 if (!usecase) {
2985 ALOGE("%s: Get usecase list failed!", __func__);
Aniket Kumar Lataf9f246e2017-09-15 15:20:16 -07002986 goto exit;
2987 }
Varun Balaraje49253e2017-07-06 19:48:56 +05302988 out->downmix_params.num_output_channels = mm_params->num_output_channels;
2989 out->downmix_params.num_input_channels = mm_params->num_input_channels;
2990
2991 out->downmix_params.has_output_channel_map =
2992 mm_params->has_output_channel_map;
2993 for (i = 0; i < mm_params->num_output_channels; i++) {
2994 out->downmix_params.output_channel_map[i] =
2995 mm_params->output_channel_map[i];
2996 }
2997
2998 out->downmix_params.has_input_channel_map =
2999 mm_params->has_input_channel_map;
3000 for (i = 0; i < mm_params->num_input_channels; i++)
3001 out->downmix_params.input_channel_map[i] =
3002 mm_params->input_channel_map[i];
3003
3004 out->downmix_params.has_mixer_coeffs = mm_params->has_mixer_coeffs;
3005 for (i = 0; i < mm_params->num_output_channels; i++)
Nikhil Latukar2e6f6242017-08-15 13:37:07 +05303006 for (j = 0; j < mm_params->num_input_channels; j++) {
3007 //Convert the channel coefficient gains in Q14 format
Varun Balaraje49253e2017-07-06 19:48:56 +05303008 out->downmix_params.mixer_coeffs[i][j] =
Nikhil Latukar2e6f6242017-08-15 13:37:07 +05303009 mm_params->mixer_coeffs[i][j] * (2 << 13);
3010 }
Varun Balaraje49253e2017-07-06 19:48:56 +05303011
3012 ret = platform_set_stream_downmix_params(out->dev->platform,
3013 out->pcm_device_id,
3014 usecase->out_snd_device,
3015 out->downmix_params);
3016
3017exit:
3018 return ret;
3019}
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05303020
3021bool audio_extn_utils_is_dolby_format(audio_format_t format)
3022{
3023 if (format == AUDIO_FORMAT_AC3 ||
3024 format == AUDIO_FORMAT_E_AC3 ||
3025 format == AUDIO_FORMAT_E_AC3_JOC)
3026 return true;
3027 else
3028 return false;
3029}
3030
`Deeraj Soman676c2702017-09-18 19:25:53 +05303031int audio_extn_utils_get_bit_width_from_string(const char *id_string)
3032{
3033 int i;
3034 const mixer_config_lookup mixer_bitwidth_config[] = {{"S24_3LE", 24},
3035 {"S32_LE", 32},
3036 {"S24_LE", 24},
3037 {"S16_LE", 16}};
3038 int num_configs = sizeof(mixer_bitwidth_config) / sizeof(mixer_bitwidth_config[0]);
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05303039
`Deeraj Soman676c2702017-09-18 19:25:53 +05303040 for (i = 0; i < num_configs; i++) {
3041 if (!strcmp(id_string, mixer_bitwidth_config[i].id_string))
3042 return mixer_bitwidth_config[i].value;
3043 }
3044
3045 return -EINVAL;
3046}
3047
3048int audio_extn_utils_get_sample_rate_from_string(const char *id_string)
3049{
3050 int i;
3051 const mixer_config_lookup mixer_samplerate_config[] = {{"KHZ_32", 32000},
3052 {"KHZ_48", 48000},
3053 {"KHZ_96", 96000},
3054 {"KHZ_144", 144000},
3055 {"KHZ_192", 192000},
3056 {"KHZ_384", 384000},
3057 {"KHZ_44P1", 44100},
3058 {"KHZ_88P2", 88200},
3059 {"KHZ_176P4", 176400},
3060 {"KHZ_352P8", 352800}};
3061 int num_configs = sizeof(mixer_samplerate_config) / sizeof(mixer_samplerate_config[0]);
3062
3063 for (i = 0; i < num_configs; i++) {
3064 if (!strcmp(id_string, mixer_samplerate_config[i].id_string))
3065 return mixer_samplerate_config[i].value;
3066 }
3067
3068 return -EINVAL;
3069}
3070
3071int audio_extn_utils_get_channels_from_string(const char *id_string)
3072{
3073 int i;
3074 const mixer_config_lookup mixer_channels_config[] = {{"One", 1},
3075 {"Two", 2},
3076 {"Three",3},
3077 {"Four", 4},
3078 {"Five", 5},
3079 {"Six", 6},
3080 {"Seven", 7},
3081 {"Eight", 8}};
3082 int num_configs = sizeof(mixer_channels_config) / sizeof(mixer_channels_config[0]);
3083
3084 for (i = 0; i < num_configs; i++) {
3085 if (!strcmp(id_string, mixer_channels_config[i].id_string))
3086 return mixer_channels_config[i].value;
3087 }
3088
3089 return -EINVAL;
3090}
Surendar karka30569792018-05-08 12:02:21 +05303091
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08003092void audio_extn_utils_release_snd_device(snd_device_t snd_device)
3093{
3094 audio_extn_dev_arbi_release(snd_device);
3095 audio_extn_sound_trigger_update_device_status(snd_device,
3096 ST_EVENT_SND_DEVICE_FREE);
3097 audio_extn_listen_update_device_status(snd_device,
3098 LISTEN_EVENT_SND_DEVICE_FREE);
3099}
3100
Weiyin Jiangd5718bd2019-09-04 16:52:08 +08003101int audio_extn_utils_get_license_params(
3102 const struct audio_device *adev,
3103 struct audio_license_params *license_params)
Surendar karka30569792018-05-08 12:02:21 +05303104{
3105 if(!license_params)
3106 return -EINVAL;
Weiyin Jiangd5718bd2019-09-04 16:52:08 +08003107
3108 return platform_get_license_by_product(adev->platform,
3109 (const char*)license_params->product, &license_params->key, license_params->license);
Surendar karka30569792018-05-08 12:02:21 +05303110}
3111
Aalique Grahame22e49102018-12-18 14:23:57 -08003112int audio_extn_utils_send_app_type_gain(struct audio_device *adev,
3113 int app_type,
3114 int *gain)
3115{
3116 int gain_cfg[4];
3117 const char *mixer_ctl_name = "App Type Gain";
3118 struct mixer_ctl *ctl;
3119 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
3120 if (!ctl) {
3121 ALOGE("%s: Could not get volume ctl mixer %s", __func__,
3122 mixer_ctl_name);
3123 return -EINVAL;
3124 }
3125 gain_cfg[0] = 0;
3126 gain_cfg[1] = app_type;
3127 gain_cfg[2] = gain[0];
3128 gain_cfg[3] = gain[1];
3129 ALOGV("%s app_type %d l(%d) r(%d)", __func__, app_type, gain[0], gain[1]);
3130 return mixer_ctl_set_array(ctl, gain_cfg,
3131 sizeof(gain_cfg)/sizeof(gain_cfg[0]));
3132}
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -07003133
Weiyin Jiangd5718bd2019-09-04 16:52:08 +08003134static void vndk_fwk_init()
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -07003135{
Weiyin Jiangd5718bd2019-09-04 16:52:08 +08003136 if (mVndkFwk.lib_handle != NULL)
3137 return;
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -07003138
Weiyin Jiangd5718bd2019-09-04 16:52:08 +08003139 mVndkFwk.lib_handle = dlopen(VNDK_FWK_LIB_PATH, RTLD_NOW);
3140 if (mVndkFwk.lib_handle == NULL) {
3141 ALOGW("%s: failed to dlopen VNDK_FWK_LIB %s", __func__, strerror(errno));
3142 return;
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -07003143 }
3144
Weiyin Jiangd5718bd2019-09-04 16:52:08 +08003145 *(void **)(&mVndkFwk.isVendorEnhancedFwk) =
3146 dlsym(mVndkFwk.lib_handle, "isRunningWithVendorEnhancedFramework");
3147 if (mVndkFwk.isVendorEnhancedFwk == NULL) {
3148 ALOGW("%s: dlsym failed %s", __func__, strerror(errno));
3149 if (mVndkFwk.lib_handle) {
3150 dlclose(mVndkFwk.lib_handle);
3151 mVndkFwk.lib_handle = NULL;
3152 }
3153 return;
3154 }
3155
3156
3157 *(void **)(&mVndkFwk.getVendorEnhancedInfo) =
3158 dlsym(mVndkFwk.lib_handle, "getVendorEnhancedInfo");
3159 if (mVndkFwk.getVendorEnhancedInfo == NULL) {
3160 ALOGW("%s: dlsym failed %s", __func__, strerror(errno));
3161 if (mVndkFwk.lib_handle) {
3162 dlclose(mVndkFwk.lib_handle);
3163 mVndkFwk.lib_handle = NULL;
3164 }
3165 }
3166
3167 return;
3168}
3169
3170bool audio_extn_utils_is_vendor_enhanced_fwk()
3171{
3172 static int is_vendor_enhanced_fwk = -EINVAL;
3173 if (is_vendor_enhanced_fwk != -EINVAL)
3174 return (bool)is_vendor_enhanced_fwk;
3175
3176 vndk_fwk_init();
3177
3178 if (mVndkFwk.isVendorEnhancedFwk != NULL) {
3179 is_vendor_enhanced_fwk = mVndkFwk.isVendorEnhancedFwk();
3180 ALOGW("%s: is_vendor_enhanced_fwk %d", __func__, is_vendor_enhanced_fwk);
3181 } else {
3182 is_vendor_enhanced_fwk = 0;
3183 ALOGW("%s: default to non enhanced_fwk config", __func__);
3184 }
3185
3186 return (bool)is_vendor_enhanced_fwk;
3187}
3188
3189int audio_extn_utils_get_vendor_enhanced_info()
3190{
3191 static int vendor_enhanced_info = -EINVAL;
3192 if (vendor_enhanced_info != -EINVAL)
3193 return vendor_enhanced_info;
3194
3195 vndk_fwk_init();
3196
3197 if (mVndkFwk.getVendorEnhancedInfo != NULL) {
3198 vendor_enhanced_info = mVndkFwk.getVendorEnhancedInfo();
3199 ALOGW("%s: vendor_enhanced_info 0x%x", __func__, vendor_enhanced_info);
3200 } else {
3201 vendor_enhanced_info = 0x0;
3202 ALOGW("%s: default to vendor_enhanced_info 0x0", __func__);
3203 }
3204
3205 return vendor_enhanced_info;
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -07003206}
Deeraj Soman14230922019-01-30 16:39:30 +05303207
Deeraj Somanfa377bf2019-02-06 12:57:59 +05303208int audio_extn_utils_get_perf_mode_flag(void)
3209{
3210#ifdef COMPRESSED_PERF_MODE_FLAG
3211 return COMPRESSED_PERF_MODE_FLAG;
3212#else
3213 return 0;
3214#endif
3215}
3216
Deeraj Soman14230922019-01-30 16:39:30 +05303217size_t audio_extn_utils_get_input_buffer_size(uint32_t sample_rate,
3218 audio_format_t format,
3219 int channel_count,
3220 int64_t duration_ms,
3221 bool is_low_latency)
3222{
3223 size_t size = 0;
3224 size_t capture_duration = AUDIO_CAPTURE_PERIOD_DURATION_MSEC;
3225 uint32_t bytes_per_period_sample = 0;
3226
3227
3228 if (audio_extn_utils_check_input_parameters(sample_rate, format, channel_count) != 0)
3229 return 0;
3230
3231 if (duration_ms >= MIN_OFFLOAD_BUFFER_DURATION_MS && duration_ms <= MAX_OFFLOAD_BUFFER_DURATION_MS)
3232 capture_duration = duration_ms;
3233
3234 size = (sample_rate * capture_duration) / 1000;
3235 if (is_low_latency)
3236 size = LOW_LATENCY_CAPTURE_PERIOD_SIZE;
3237
3238
3239 bytes_per_period_sample = audio_bytes_per_sample(format) * channel_count;
3240 size *= bytes_per_period_sample;
3241
3242 /* make sure the size is multiple of 32 bytes and additionally multiple of
3243 * the frame_size (required for 24bit samples and non-power-of-2 channel counts)
3244 * At 48 kHz mono 16-bit PCM:
3245 * 5.000 ms = 240 frames = 15*16*1*2 = 480, a whole multiple of 32 (15)
3246 * 3.333 ms = 160 frames = 10*16*1*2 = 320, a whole multiple of 32 (10)
3247 *
3248 * The loop reaches result within 32 iterations, as initial size is
3249 * already a multiple of frame_size
3250 */
3251 size = audio_extn_utils_nearest_multiple(size, audio_extn_utils_lcm(32, bytes_per_period_sample));
3252
3253 return size;
3254}
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07003255
3256int audio_extn_utils_hash_fn(void *key)
3257{
3258 return (int)key;
3259}
3260
3261bool audio_extn_utils_hash_eq(void *key1, void *key2)
3262{
3263 return (key1 == key2);
3264}