blob: 5f10ca740b98a9b50ec6f5b88597cc9fbb93e017 [file] [log] [blame]
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001/*
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07002 * Copyright (c) 2014-2020, The Linux Foundation. All rights reserved.
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07003 * Not a Contribution.
4 *
5 * Copyright (C) 2014 The Android Open Source Project
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19
20#define LOG_TAG "audio_hw_utils"
21/* #define LOG_NDEBUG 0 */
22
Manish Dewangan27346042017-03-01 12:56:12 +053023#include <inttypes.h>
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070024#include <errno.h>
25#include <cutils/properties.h>
26#include <cutils/config_utils.h>
27#include <stdlib.h>
28#include <dlfcn.h>
29#include <cutils/str_parms.h>
Aalique Grahame22e49102018-12-18 14:23:57 -080030#include <log/log.h>
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070031#include <cutils/misc.h>
Vinay Vermaaddfa4a2018-04-29 14:03:38 +053032#include <unistd.h>
Surendar Karka287348c2019-04-10 18:31:46 +053033#include <sys/ioctl.h>
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070034
Manish Dewangan07de2142017-02-27 19:27:20 +053035
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070036#include "audio_hw.h"
37#include "platform.h"
38#include "platform_api.h"
39#include "audio_extn.h"
Deeraj Soman14230922019-01-30 16:39:30 +053040#include "voice_extn.h"
Narsinga Rao Chella212e2542014-11-17 19:57:04 -080041#include "voice.h"
Manish Dewangan07de2142017-02-27 19:27:20 +053042#include <sound/compress_params.h>
43#include <sound/compress_offload.h>
Surendar Karka287348c2019-04-10 18:31:46 +053044#include <sound/devdep_params.h>
Manish Dewangan07de2142017-02-27 19:27:20 +053045#include <tinycompress/tinycompress.h>
Revathi Uddaraju1eac8b02017-05-18 17:13:33 +053046
47#ifdef DYNAMIC_LOG_ENABLED
48#include <log_xml_parser.h>
49#define LOG_MASK HAL_MOD_FILE_UTILS
50#include <log_utils.h>
51#endif
52
Ashish Jain81eb2a82015-05-13 10:52:34 +053053#ifdef AUDIO_EXTERNAL_HDMI_ENABLED
Ashish Jain81eb2a82015-05-13 10:52:34 +053054#include "audio_parsers.h"
55#endif
Ashish Jain81eb2a82015-05-13 10:52:34 +053056
Roopesh Nataraja0f68be72020-04-28 12:32:02 -070057#define AUDIO_IO_POLICY_VENDOR_CONFIG_FILE_NAME "audio_io_policy.conf"
58#define AUDIO_OUTPUT_POLICY_VENDOR_CONFIG_FILE_NAME "audio_output_policy.conf"
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070059
60#define OUTPUTS_TAG "outputs"
Dhananjay Kumard6d32152016-10-13 16:11:03 +053061#define INPUTS_TAG "inputs"
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070062
63#define DYNAMIC_VALUE_TAG "dynamic"
64#define FLAGS_TAG "flags"
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +053065#define PROFILES_TAG "profile"
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070066#define FORMATS_TAG "formats"
67#define SAMPLING_RATES_TAG "sampling_rates"
68#define BIT_WIDTH_TAG "bit_width"
69#define APP_TYPE_TAG "app_type"
70
71#define STRING_TO_ENUM(string) { #string, string }
72#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
73
Ben Rombergera04fabc2014-11-14 12:16:03 -080074#define BASE_TABLE_SIZE 64
75#define MAX_BASEINDEX_LEN 256
76
Ben Romberger1aaaf862017-04-06 17:49:46 -070077#ifndef SND_AUDIOCODEC_TRUEHD
78#define SND_AUDIOCODEC_TRUEHD 0x00000023
79#endif
80
Vikram Panduranga93f080e2017-06-07 18:16:14 -070081#define APP_TYPE_VOIP_AUDIO 0x1113A
82
Ashish Jain81eb2a82015-05-13 10:52:34 +053083#ifdef AUDIO_EXTERNAL_HDMI_ENABLED
84#define PROFESSIONAL (1<<0) /* 0 = consumer, 1 = professional */
85#define NON_LPCM (1<<1) /* 0 = audio, 1 = non-audio */
86#define SR_44100 (0<<0) /* 44.1kHz */
87#define SR_NOTID (1<<0) /* non indicated */
88#define SR_48000 (2<<0) /* 48kHz */
89#define SR_32000 (3<<0) /* 32kHz */
90#define SR_22050 (4<<0) /* 22.05kHz */
91#define SR_24000 (6<<0) /* 24kHz */
92#define SR_88200 (8<<0) /* 88.2kHz */
93#define SR_96000 (10<<0) /* 96kHz */
94#define SR_176400 (12<<0) /* 176.4kHz */
95#define SR_192000 (14<<0) /* 192kHz */
96
97#endif
Manish Dewangan07de2142017-02-27 19:27:20 +053098
99/* ToDo: Check and update a proper value in msec */
Weiyin Jiangc49a3b52018-08-17 16:16:08 +0800100#define COMPRESS_OFFLOAD_PLAYBACK_LATENCY 50
101#define PCM_OFFLOAD_PLAYBACK_DSP_PATHDELAY 62
Samyak Jain2546f8a2019-08-29 19:24:21 +0530102#define PCM_OFFLOAD_PLAYBACK_LATENCY PCM_OFFLOAD_PLAYBACK_DSP_PATHDELAY
Manish Dewangan07de2142017-02-27 19:27:20 +0530103
Varun Balaraje49253e2017-07-06 19:48:56 +0530104#ifndef MAX_CHANNELS_SUPPORTED
105#define MAX_CHANNELS_SUPPORTED 8
106#endif
107
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -0700108#ifdef __LP64__
109#define VNDK_FWK_LIB_PATH "/vendor/lib64/libqti_vndfwk_detect.so"
110#else
111#define VNDK_FWK_LIB_PATH "/vendor/lib/libqti_vndfwk_detect.so"
112#endif
113
Weiyin Jiangd5718bd2019-09-04 16:52:08 +0800114typedef struct vndkfwk_s {
115 void *lib_handle;
116 int (*isVendorEnhancedFwk)(void);
117 int (*getVendorEnhancedInfo)(void);
118 const char *lib_name;
119} vndkfwk_t;
120
121static vndkfwk_t mVndkFwk = {
122 NULL, NULL, NULL, VNDK_FWK_LIB_PATH};
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -0700123
`Deeraj Soman676c2702017-09-18 19:25:53 +0530124typedef struct {
125 const char *id_string;
126 const int value;
127} mixer_config_lookup;
128
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700129struct string_to_enum {
130 const char *name;
131 uint32_t value;
132};
133
134const struct string_to_enum s_flag_name_to_enum_table[] = {
135 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_DIRECT),
136 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_PRIMARY),
137 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_FAST),
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800138 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_RAW),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700139 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_DEEP_BUFFER),
140 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD),
141 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_NON_BLOCKING),
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700142 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_HW_AV_SYNC),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700143 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_INCALL_MUSIC),
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700144 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH),
Naresh Tanniruee3499a2017-01-05 14:05:35 +0530145 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_TIMESTAMP),
Vikram Panduranga93f080e2017-06-07 18:16:14 -0700146 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_VOIP_RX),
Ben Romberger6c4d3812017-06-13 17:46:45 -0700147 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_BD),
Varun Balaraje49253e2017-07-06 19:48:56 +0530148 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_INTERACTIVE),
Derek Chen090dfc52018-03-22 22:15:29 -0400149 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_MEDIA),
150 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_SYS_NOTIFICATION),
151 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_NAV_GUIDANCE),
152 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_PHONE),
Derek Chen1bcdc6b2020-02-06 22:44:53 -0800153 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_FRONT_PASSENGER),
Derek Chendf05eea2019-08-01 13:57:49 -0700154 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_REAR_SEAT),
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530155 STRING_TO_ENUM(AUDIO_INPUT_FLAG_NONE),
156 STRING_TO_ENUM(AUDIO_INPUT_FLAG_FAST),
157 STRING_TO_ENUM(AUDIO_INPUT_FLAG_HW_HOTWORD),
158 STRING_TO_ENUM(AUDIO_INPUT_FLAG_RAW),
159 STRING_TO_ENUM(AUDIO_INPUT_FLAG_SYNC),
Dhananjay Kumaree4d2002016-10-25 18:02:58 +0530160 STRING_TO_ENUM(AUDIO_INPUT_FLAG_TIMESTAMP),
Dhananjay Kumar704ce6f2017-09-28 22:08:00 +0530161 STRING_TO_ENUM(AUDIO_INPUT_FLAG_COMPRESS),
Ralf Herzaec80262018-07-03 07:08:49 +0200162 STRING_TO_ENUM(AUDIO_INPUT_FLAG_PASSTHROUGH),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700163};
164
165const struct string_to_enum s_format_name_to_enum_table[] = {
Ashish Jain83a6cc22016-06-28 14:34:17 +0530166 STRING_TO_ENUM(AUDIO_FORMAT_PCM_8_BIT),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700167 STRING_TO_ENUM(AUDIO_FORMAT_PCM_16_BIT),
Ashish Jain5106d362016-05-11 19:23:33 +0530168 STRING_TO_ENUM(AUDIO_FORMAT_PCM_24_BIT_PACKED),
169 STRING_TO_ENUM(AUDIO_FORMAT_PCM_8_24_BIT),
Ashish Jainf1eaa582016-05-23 20:54:24 +0530170 STRING_TO_ENUM(AUDIO_FORMAT_PCM_32_BIT),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700171 STRING_TO_ENUM(AUDIO_FORMAT_MP3),
172 STRING_TO_ENUM(AUDIO_FORMAT_AAC),
173 STRING_TO_ENUM(AUDIO_FORMAT_VORBIS),
Mingming Yinae3530f2014-07-03 16:50:18 -0700174 STRING_TO_ENUM(AUDIO_FORMAT_AMR_NB),
175 STRING_TO_ENUM(AUDIO_FORMAT_AMR_WB),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700176 STRING_TO_ENUM(AUDIO_FORMAT_AC3),
Mingming Yinae3530f2014-07-03 16:50:18 -0700177 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700178 STRING_TO_ENUM(AUDIO_FORMAT_DTS),
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530179 STRING_TO_ENUM(AUDIO_FORMAT_DTS_HD),
Ben Romberger1aaaf862017-04-06 17:49:46 -0700180 STRING_TO_ENUM(AUDIO_FORMAT_DOLBY_TRUEHD),
Naresh Tanniru928f0862017-04-07 16:44:23 -0700181 STRING_TO_ENUM(AUDIO_FORMAT_IEC61937),
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530182 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3_JOC),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700183 STRING_TO_ENUM(AUDIO_FORMAT_WMA),
184 STRING_TO_ENUM(AUDIO_FORMAT_WMA_PRO),
185 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADIF),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700186 STRING_TO_ENUM(AUDIO_FORMAT_AMR_WB_PLUS),
187 STRING_TO_ENUM(AUDIO_FORMAT_EVRC),
188 STRING_TO_ENUM(AUDIO_FORMAT_EVRCB),
189 STRING_TO_ENUM(AUDIO_FORMAT_EVRCWB),
190 STRING_TO_ENUM(AUDIO_FORMAT_QCELP),
191 STRING_TO_ENUM(AUDIO_FORMAT_MP2),
192 STRING_TO_ENUM(AUDIO_FORMAT_EVRCNW),
Amit Shekhar6f461b12014-08-01 14:52:58 -0700193 STRING_TO_ENUM(AUDIO_FORMAT_FLAC),
Satya Krishna Pindiproli70471602015-04-24 19:12:43 +0530194 STRING_TO_ENUM(AUDIO_FORMAT_ALAC),
195 STRING_TO_ENUM(AUDIO_FORMAT_APE),
Alexy Josephcd8eaed2014-12-11 12:46:53 -0800196 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LC),
197 STRING_TO_ENUM(AUDIO_FORMAT_AAC_HE_V1),
198 STRING_TO_ENUM(AUDIO_FORMAT_AAC_HE_V2),
Manish Dewangana6fc5442015-08-24 20:30:31 +0530199 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS),
Ashish Jaine513a872015-11-19 17:00:56 +0530200 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_LC),
201 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_HE_V1),
202 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_HE_V2),
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +0530203 STRING_TO_ENUM(AUDIO_FORMAT_DSD),
Arun Kumar Dasari3b174182016-12-27 13:01:14 +0530204 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM),
205 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM_LC),
206 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM_HE_V1),
207 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM_HE_V2),
Dhanalakshmi Siddani18737932016-11-29 17:33:17 +0530208 STRING_TO_ENUM(AUDIO_FORMAT_APTX),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700209};
210
Manish Dewangan3ccdea52017-02-13 19:31:54 +0530211/* payload structure avt_device drift query */
212struct audio_avt_device_drift_stats {
213 uint32_t minor_version;
Manish Dewangan338c50a2017-09-12 15:22:03 +0530214
Manish Dewangan3ccdea52017-02-13 19:31:54 +0530215 /* Indicates the device interface direction as either
216 * source (Tx) or sink (Rx).
217 */
218 uint16_t device_direction;
Manish Dewangan338c50a2017-09-12 15:22:03 +0530219
220 /* Reference timer for drift accumulation and time stamp information.
221 * currently it only support AFE_REF_TIMER_TYPE_AVTIMER
222 */
223 uint16_t reference_timer;
Manish Dewangan3ccdea52017-02-13 19:31:54 +0530224 struct audio_avt_device_drift_param drift_param;
Manish Dewangan338c50a2017-09-12 15:22:03 +0530225} __attribute__((packed));
Manish Dewangan3ccdea52017-02-13 19:31:54 +0530226
Ben Rombergera04fabc2014-11-14 12:16:03 -0800227static char bTable[BASE_TABLE_SIZE] = {
228 'A','B','C','D','E','F','G','H','I','J','K','L',
229 'M','N','O','P','Q','R','S','T','U','V','W','X',
230 'Y','Z','a','b','c','d','e','f','g','h','i','j',
231 'k','l','m','n','o','p','q','r','s','t','u','v',
232 'w','x','y','z','0','1','2','3','4','5','6','7',
233 '8','9','+','/'
234};
235
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700236static uint32_t string_to_enum(const struct string_to_enum *table, size_t size,
237 const char *name)
238{
239 size_t i;
240 for (i = 0; i < size; i++) {
241 if (strcmp(table[i].name, name) == 0) {
242 ALOGV("%s found %s", __func__, table[i].name);
243 return table[i].value;
244 }
245 }
246 return 0;
247}
248
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530249static audio_io_flags_t parse_flag_names(char *name)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700250{
251 uint32_t flag = 0;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530252 audio_io_flags_t io_flags;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800253 char *last_r;
254 char *flag_name = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700255 while (flag_name != NULL) {
256 if (strlen(flag_name) != 0) {
257 flag |= string_to_enum(s_flag_name_to_enum_table,
258 ARRAY_SIZE(s_flag_name_to_enum_table),
259 flag_name);
260 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800261 flag_name = strtok_r(NULL, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700262 }
263
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800264 ALOGV("parse_flag_names: flag - %x", flag);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530265 io_flags.in_flags = (audio_input_flags_t)flag;
266 io_flags.out_flags = (audio_output_flags_t)flag;
267 return io_flags;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700268}
269
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530270static void parse_format_names(char *name, struct streams_io_cfg *s_info)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700271{
272 struct stream_format *sf_info = NULL;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800273 char *last_r;
274 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700275
276 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG) == 0)
277 return;
278
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530279 list_init(&s_info->format_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700280 while (str != NULL) {
281 audio_format_t format = (audio_format_t)string_to_enum(s_format_name_to_enum_table,
282 ARRAY_SIZE(s_format_name_to_enum_table), str);
283 ALOGV("%s: format - %d", __func__, format);
284 if (format != 0) {
285 sf_info = (struct stream_format *)calloc(1, sizeof(struct stream_format));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700286 if (sf_info == NULL)
287 break; /* return whatever was parsed */
288
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700289 sf_info->format = format;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530290 list_add_tail(&s_info->format_list, &sf_info->list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700291 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800292 str = strtok_r(NULL, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700293 }
294}
295
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530296static void parse_sample_rate_names(char *name, struct streams_io_cfg *s_info)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700297{
Amit Shekhar6f461b12014-08-01 14:52:58 -0700298 struct stream_sample_rate *ss_info = NULL;
299 uint32_t sample_rate = 48000;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800300 char *last_r;
301 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700302
Amit Shekhar6f461b12014-08-01 14:52:58 -0700303 if (str != NULL && 0 == strcmp(str, DYNAMIC_VALUE_TAG))
304 return;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700305
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530306 list_init(&s_info->sample_rate_list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700307 while (str != NULL) {
308 sample_rate = (uint32_t)strtol(str, (char **)NULL, 10);
309 ALOGV("%s: sample_rate - %d", __func__, sample_rate);
310 if (0 != sample_rate) {
311 ss_info = (struct stream_sample_rate *)calloc(1, sizeof(struct stream_sample_rate));
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800312 if (!ss_info) {
313 ALOGE("%s: memory allocation failure", __func__);
314 return;
315 }
Amit Shekhar6f461b12014-08-01 14:52:58 -0700316 ss_info->sample_rate = sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530317 list_add_tail(&s_info->sample_rate_list, &ss_info->list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700318 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800319 str = strtok_r(NULL, "|", &last_r);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700320 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700321}
322
323static int parse_bit_width_names(char *name)
324{
325 int bit_width = 16;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800326 char *last_r;
327 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700328
329 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG))
330 bit_width = (int)strtol(str, (char **)NULL, 10);
331
332 ALOGV("%s: bit_width - %d", __func__, bit_width);
333 return bit_width;
334}
335
336static int parse_app_type_names(void *platform, char *name)
337{
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700338 int app_type = platform_get_default_app_type(platform);
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800339 char *last_r;
340 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700341
342 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG))
343 app_type = (int)strtol(str, (char **)NULL, 10);
344
345 ALOGV("%s: app_type - %d", __func__, app_type);
346 return app_type;
347}
348
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530349static void update_streams_cfg_list(cnode *root, void *platform,
350 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700351{
352 cnode *node = root->first_child;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530353 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700354
355 ALOGV("%s", __func__);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530356 s_info = (struct streams_io_cfg *)calloc(1, sizeof(struct streams_io_cfg));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700357
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530358 if (!s_info) {
359 ALOGE("failed to allocate mem for s_info list element");
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700360 return;
361 }
362
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700363 while (node) {
364 if (strcmp(node->name, FLAGS_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530365 s_info->flags = parse_flag_names((char *)node->value);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530366 } else if (strcmp(node->name, PROFILES_TAG) == 0) {
367 strlcpy(s_info->profile, (char *)node->value, sizeof(s_info->profile));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700368 } else if (strcmp(node->name, FORMATS_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530369 parse_format_names((char *)node->value, s_info);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700370 } else if (strcmp(node->name, SAMPLING_RATES_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530371 s_info->app_type_cfg.sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
372 parse_sample_rate_names((char *)node->value, s_info);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700373 } else if (strcmp(node->name, BIT_WIDTH_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530374 s_info->app_type_cfg.bit_width = parse_bit_width_names((char *)node->value);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700375 } else if (strcmp(node->name, APP_TYPE_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530376 s_info->app_type_cfg.app_type = parse_app_type_names(platform, (char *)node->value);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700377 }
378 node = node->next;
379 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530380 list_add_tail(streams_cfg_list, &s_info->list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700381}
382
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530383static void load_cfg_list(cnode *root, void *platform,
384 struct listnode *streams_output_cfg_list,
385 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700386{
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530387 cnode *node = NULL;
388
389 node = config_find(root, OUTPUTS_TAG);
390 if (node != NULL) {
391 node = node->first_child;
392 while (node) {
393 ALOGV("%s: loading output %s", __func__, node->name);
394 update_streams_cfg_list(node, platform, streams_output_cfg_list);
395 node = node->next;
396 }
397 } else {
398 ALOGI("%s: could not load output, node is NULL", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700399 }
400
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530401 node = config_find(root, INPUTS_TAG);
402 if (node != NULL) {
403 node = node->first_child;
404 while (node) {
405 ALOGV("%s: loading input %s", __func__, node->name);
406 update_streams_cfg_list(node, platform, streams_input_cfg_list);
407 node = node->next;
408 }
409 } else {
410 ALOGI("%s: could not load input, node is NULL", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700411 }
412}
413
414static void send_app_type_cfg(void *platform, struct mixer *mixer,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530415 struct listnode *streams_output_cfg_list,
416 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700417{
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530418 size_t app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT] = {0};
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700419 int length = 0, i, num_app_types = 0;
420 struct listnode *node;
421 bool update;
422 struct mixer_ctl *ctl = NULL;
423 const char *mixer_ctl_name = "App Type Config";
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530424 struct streams_io_cfg *s_info = NULL;
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800425 uint32_t target_bit_width = 0;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700426
427 if (!mixer) {
428 ALOGE("%s: mixer is null",__func__);
429 return;
430 }
431 ctl = mixer_get_ctl_by_name(mixer, mixer_ctl_name);
432 if (!ctl) {
433 ALOGE("%s: Could not get ctl for mixer cmd - %s",__func__, mixer_ctl_name);
434 return;
435 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530436 app_type_cfg[length++] = num_app_types;
437
438 if (list_empty(streams_output_cfg_list)) {
439 app_type_cfg[length++] = platform_get_default_app_type_v2(platform, PCM_PLAYBACK);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700440 app_type_cfg[length++] = 48000;
441 app_type_cfg[length++] = 16;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530442 num_app_types += 1;
443 }
444 if (list_empty(streams_input_cfg_list)) {
445 app_type_cfg[length++] = platform_get_default_app_type_v2(platform, PCM_CAPTURE);
446 app_type_cfg[length++] = 48000;
447 app_type_cfg[length++] = 16;
448 num_app_types += 1;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700449 }
450
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800451 /* get target bit width for ADM enforce mode */
452 target_bit_width = adev_get_dsp_bit_width_enforce_mode();
453
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700454 list_for_each(node, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530455 s_info = node_to_item(node, struct streams_io_cfg, list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700456 update = true;
457 for (i=0; i<length; i=i+3) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530458 if (app_type_cfg[i+1] == 0)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700459 break;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530460 else if (app_type_cfg[i+1] == (size_t)s_info->app_type_cfg.app_type) {
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530461 if (app_type_cfg[i+2] < (size_t)s_info->app_type_cfg.sample_rate)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530462 app_type_cfg[i+2] = s_info->app_type_cfg.sample_rate;
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530463 if (app_type_cfg[i+3] < (size_t)s_info->app_type_cfg.bit_width)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530464 app_type_cfg[i+3] = s_info->app_type_cfg.bit_width;
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800465 /* ADM bit width = max(enforce_bit_width, bit_width from s_info */
466 if (audio_extn_is_dsp_bit_width_enforce_mode_supported(s_info->flags.out_flags) &&
467 (target_bit_width > app_type_cfg[i+3]))
468 app_type_cfg[i+3] = target_bit_width;
469
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700470 update = false;
471 break;
472 }
473 }
474 if (update && ((length + 3) <= MAX_LENGTH_MIXER_CONTROL_IN_INT)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530475 num_app_types += 1;
476 app_type_cfg[length++] = s_info->app_type_cfg.app_type;
477 app_type_cfg[length++] = s_info->app_type_cfg.sample_rate;
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800478 app_type_cfg[length] = s_info->app_type_cfg.bit_width;
479 if (audio_extn_is_dsp_bit_width_enforce_mode_supported(s_info->flags.out_flags) &&
480 (target_bit_width > app_type_cfg[length]))
481 app_type_cfg[length] = target_bit_width;
482
483 length++;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530484 }
485 }
486 list_for_each(node, streams_input_cfg_list) {
487 s_info = node_to_item(node, struct streams_io_cfg, list);
488 update = true;
489 for (i=0; i<length; i=i+3) {
490 if (app_type_cfg[i+1] == 0)
491 break;
492 else if (app_type_cfg[i+1] == (size_t)s_info->app_type_cfg.app_type) {
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530493 if (app_type_cfg[i+2] < (size_t)s_info->app_type_cfg.sample_rate)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530494 app_type_cfg[i+2] = s_info->app_type_cfg.sample_rate;
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530495 if (app_type_cfg[i+3] < (size_t)s_info->app_type_cfg.bit_width)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530496 app_type_cfg[i+3] = s_info->app_type_cfg.bit_width;
497 update = false;
498 break;
499 }
500 }
501 if (update && ((length + 3) <= MAX_LENGTH_MIXER_CONTROL_IN_INT)) {
502 num_app_types += 1;
503 app_type_cfg[length++] = s_info->app_type_cfg.app_type;
504 app_type_cfg[length++] = s_info->app_type_cfg.sample_rate;
505 app_type_cfg[length++] = s_info->app_type_cfg.bit_width;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700506 }
507 }
508 ALOGV("%s: num_app_types: %d", __func__, num_app_types);
509 if (num_app_types) {
510 app_type_cfg[0] = num_app_types;
511 mixer_ctl_set_array(ctl, app_type_cfg, length);
512 }
513}
514
Roopesh Nataraja0f68be72020-04-28 12:32:02 -0700515/* Function to retrieve audio vendor configs path */
516void audio_get_vendor_config_path (char* config_file_path, int path_size)
517{
518 char vendor_sku[PROPERTY_VALUE_MAX] = {'\0'};
519 if (property_get("ro.boot.product.vendor.sku", vendor_sku, "") <= 0) {
520#ifdef LINUX_ENABLED
521 /* Audio configs are stored in /etc */
522 snprintf(config_file_path, path_size, "%s", "/etc");
523#else
524 /* Audio configs are stored in /vendor/etc */
525 snprintf(config_file_path, path_size, "%s", "/vendor/etc");
526#endif
527 } else {
528 /* Audio configs are stored in /vendor/etc/audio/sku_${vendor_sku} */
529 snprintf(config_file_path, path_size,
530 "%s%s", "/vendor/etc/audio/sku_", vendor_sku);
531 }
532}
533
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530534void audio_extn_utils_update_streams_cfg_lists(void *platform,
535 struct mixer *mixer,
536 struct listnode *streams_output_cfg_list,
537 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700538{
539 cnode *root;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530540 char *data = NULL;
Roopesh Nataraja0f68be72020-04-28 12:32:02 -0700541 char vendor_config_path[VENDOR_CONFIG_PATH_MAX_LENGTH];
542 char audio_io_policy_file[VENDOR_CONFIG_FILE_MAX_LENGTH];
543 char audio_output_policy_file[VENDOR_CONFIG_FILE_MAX_LENGTH];
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700544
545 ALOGV("%s", __func__);
546 list_init(streams_output_cfg_list);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530547 list_init(streams_input_cfg_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700548
549 root = config_node("", "");
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700550 if (root == NULL) {
551 ALOGE("cfg_list, NULL config root");
552 return;
553 }
554
Roopesh Nataraja0f68be72020-04-28 12:32:02 -0700555 /* Get path for audio configuration files in vendor */
556 audio_get_vendor_config_path(vendor_config_path,
557 sizeof(vendor_config_path));
558
559 /* Get path for audio_io_policy_file in vendor */
560 snprintf(audio_io_policy_file, sizeof(audio_io_policy_file),
561 "%s/%s", vendor_config_path, AUDIO_IO_POLICY_VENDOR_CONFIG_FILE_NAME);
562
563 /* Load audio_io_policy_file from vendor */
564 data = (char *)load_file(audio_io_policy_file, NULL);
565
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530566 if (data == NULL) {
567 ALOGD("%s: failed to open io config file(%s), trying older config file",
Roopesh Nataraja0f68be72020-04-28 12:32:02 -0700568 __func__, audio_io_policy_file);
569
570 /* Get path for audio_output_policy_file in vendor */
571 snprintf(audio_output_policy_file, sizeof(audio_output_policy_file),
572 "%s/%s", vendor_config_path,
573 AUDIO_OUTPUT_POLICY_VENDOR_CONFIG_FILE_NAME);
574
575 /* Load audio_output_policy_file from vendor */
576 data = (char *)load_file(audio_output_policy_file, NULL);
577
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530578 if (data == NULL) {
579 send_app_type_cfg(platform, mixer,
580 streams_output_cfg_list,
581 streams_input_cfg_list);
582 ALOGE("%s: could not load io policy config!", __func__);
yidongh6eb4f752017-04-19 18:20:57 +0800583 free(root);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530584 return;
585 }
586 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700587
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530588 config_load(root, data);
589 load_cfg_list(root, platform, streams_output_cfg_list,
590 streams_input_cfg_list);
591
592 send_app_type_cfg(platform, mixer, streams_output_cfg_list,
593 streams_input_cfg_list);
Alexy Josephaee4fdd2016-01-29 13:02:07 -0800594
595 config_free(root);
yidongh6eb4f752017-04-19 18:20:57 +0800596 free(root);
Alexy Josephaee4fdd2016-01-29 13:02:07 -0800597 free(data);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700598}
599
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530600static void audio_extn_utils_dump_streams_cfg_list(
601 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700602{
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700603 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530604 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700605 struct stream_format *sf_info;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700606 struct stream_sample_rate *ss_info;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530607
608 list_for_each(node_i, streams_cfg_list) {
609 s_info = node_to_item(node_i, struct streams_io_cfg, list);
610 ALOGV("%s: flags-%d, sample_rate-%d, bit_width-%d, app_type-%d",
611 __func__, s_info->flags.out_flags, s_info->app_type_cfg.sample_rate,
612 s_info->app_type_cfg.bit_width, s_info->app_type_cfg.app_type);
613 list_for_each(node_j, &s_info->format_list) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700614 sf_info = node_to_item(node_j, struct stream_format, list);
615 ALOGV("format-%x", sf_info->format);
616 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530617 list_for_each(node_j, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700618 ss_info = node_to_item(node_j, struct stream_sample_rate, list);
619 ALOGV("sample rate-%d", ss_info->sample_rate);
620 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700621 }
622}
623
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530624void audio_extn_utils_dump_streams_cfg_lists(
625 struct listnode *streams_output_cfg_list,
626 struct listnode *streams_input_cfg_list)
627{
628 ALOGV("%s", __func__);
629 audio_extn_utils_dump_streams_cfg_list(streams_output_cfg_list);
630 audio_extn_utils_dump_streams_cfg_list(streams_input_cfg_list);
631}
632
633static void audio_extn_utils_release_streams_cfg_list(
634 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700635{
636 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530637 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700638
639 ALOGV("%s", __func__);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530640
641 while (!list_empty(streams_cfg_list)) {
642 node_i = list_head(streams_cfg_list);
643 s_info = node_to_item(node_i, struct streams_io_cfg, list);
644 while (!list_empty(&s_info->format_list)) {
645 node_j = list_head(&s_info->format_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700646 list_remove(node_j);
647 free(node_to_item(node_j, struct stream_format, list));
648 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530649 while (!list_empty(&s_info->sample_rate_list)) {
650 node_j = list_head(&s_info->sample_rate_list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700651 list_remove(node_j);
652 free(node_to_item(node_j, struct stream_sample_rate, list));
653 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700654 list_remove(node_i);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530655 free(node_to_item(node_i, struct streams_io_cfg, list));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700656 }
657}
658
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530659void audio_extn_utils_release_streams_cfg_lists(
660 struct listnode *streams_output_cfg_list,
661 struct listnode *streams_input_cfg_list)
662{
663 ALOGV("%s", __func__);
664 audio_extn_utils_release_streams_cfg_list(streams_output_cfg_list);
665 audio_extn_utils_release_streams_cfg_list(streams_input_cfg_list);
666}
667
668static bool set_app_type_cfg(struct streams_io_cfg *s_info,
669 struct stream_app_type_cfg *app_type_cfg,
670 uint32_t sample_rate, uint32_t bit_width)
Amit Shekhar6f461b12014-08-01 14:52:58 -0700671 {
672 struct listnode *node_i;
673 struct stream_sample_rate *ss_info;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530674 list_for_each(node_i, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700675 ss_info = node_to_item(node_i, struct stream_sample_rate, list);
676 if ((sample_rate <= ss_info->sample_rate) &&
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530677 (bit_width == s_info->app_type_cfg.bit_width)) {
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700678
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530679 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700680 app_type_cfg->sample_rate = ss_info->sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530681 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700682 ALOGV("%s app_type_cfg->app_type %d, app_type_cfg->sample_rate %d, app_type_cfg->bit_width %d",
683 __func__, app_type_cfg->app_type, app_type_cfg->sample_rate, app_type_cfg->bit_width);
684 return true;
685 }
686 }
687 /*
688 * Reiterate through the list assuming dafault sample rate.
689 * Handles scenario where input sample rate is higher
690 * than all sample rates in list for the input bit width.
691 */
692 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
Apoorv Raghuvanshif59bb222015-02-18 12:23:23 -0800693
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530694 list_for_each(node_i, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700695 ss_info = node_to_item(node_i, struct stream_sample_rate, list);
696 if ((sample_rate <= ss_info->sample_rate) &&
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530697 (bit_width == s_info->app_type_cfg.bit_width)) {
698 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700699 app_type_cfg->sample_rate = sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530700 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Apoorv Raghuvanshif59bb222015-02-18 12:23:23 -0800701 ALOGV("%s Assuming sample rate. app_type_cfg->app_type %d, app_type_cfg->sample_rate %d, app_type_cfg->bit_width %d",
Amit Shekhar6f461b12014-08-01 14:52:58 -0700702 __func__, app_type_cfg->app_type, app_type_cfg->sample_rate, app_type_cfg->bit_width);
703 return true;
704 }
705 }
706 return false;
707}
708
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530709void audio_extn_utils_update_stream_input_app_type_cfg(void *platform,
710 struct listnode *streams_input_cfg_list,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -0800711 struct listnode *devices __unused,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530712 audio_input_flags_t flags,
713 audio_format_t format,
714 uint32_t sample_rate,
715 uint32_t bit_width,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530716 char* profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530717 struct stream_app_type_cfg *app_type_cfg)
718{
719 struct listnode *node_i, *node_j;
720 struct streams_io_cfg *s_info;
721 struct stream_format *sf_info;
722
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530723 ALOGV("%s: flags: 0x%x, format: 0x%x sample_rate %d, profile %s",
724 __func__, flags, format, sample_rate, profile);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530725
726 list_for_each(node_i, streams_input_cfg_list) {
727 s_info = node_to_item(node_i, struct streams_io_cfg, list);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530728 /* Along with flags do profile matching if set at either end.*/
729 if (s_info->flags.in_flags == flags &&
730 ((profile[0] == '\0' && s_info->profile[0] == '\0') ||
731 strncmp(s_info->profile, profile, sizeof(s_info->profile)) == 0)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530732 list_for_each(node_j, &s_info->format_list) {
733 sf_info = node_to_item(node_j, struct stream_format, list);
734 if (sf_info->format == format) {
735 if (set_app_type_cfg(s_info, app_type_cfg, sample_rate, bit_width))
736 return;
737 }
738 }
739 }
740 }
741 ALOGW("%s: App type could not be selected. Falling back to default", __func__);
742 app_type_cfg->app_type = platform_get_default_app_type_v2(platform, PCM_CAPTURE);
743 app_type_cfg->sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
744 app_type_cfg->bit_width = 16;
745}
746
747void audio_extn_utils_update_stream_output_app_type_cfg(void *platform,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700748 struct listnode *streams_output_cfg_list,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -0800749 struct listnode *devices,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700750 audio_output_flags_t flags,
751 audio_format_t format,
Amit Shekhar6f461b12014-08-01 14:52:58 -0700752 uint32_t sample_rate,
753 uint32_t bit_width,
Manish Dewangan837dc462015-05-27 10:17:41 +0530754 audio_channel_mask_t channel_mask,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530755 char *profile,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700756 struct stream_app_type_cfg *app_type_cfg)
757{
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +0530758 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530759 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700760 struct stream_format *sf_info;
Manish Dewangan837dc462015-05-27 10:17:41 +0530761 char value[PROPERTY_VALUE_MAX] = {0};
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700762
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -0800763 if (compare_device_type(devices, AUDIO_DEVICE_OUT_SPEAKER)) {
Ramu Gottipati074fa4d2018-09-11 20:05:51 +0530764 int bw = platform_get_snd_device_bit_width(SND_DEVICE_OUT_SPEAKER);
765 if ((-ENOSYS != bw) && (bit_width > (uint32_t)bw))
Amit Shekhar5a39c912014-10-14 15:39:30 -0700766 bit_width = (uint32_t)bw;
Ramu Gottipati074fa4d2018-09-11 20:05:51 +0530767 else if (-ENOSYS == bw)
768 bit_width = CODEC_BACKEND_DEFAULT_BIT_WIDTH;
Amit Shekhar1d896042014-10-03 13:16:09 -0700769 sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Ramu Gottipati074fa4d2018-09-11 20:05:51 +0530770 ALOGI("%s Allowing 24 and above bits playback on speaker ONLY at default sampling rate", __func__);
Amit Shekhar1d896042014-10-03 13:16:09 -0700771 }
772
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -0700773 property_get("vendor.audio.playback.mch.downsample",value,"");
Manish Dewangan837dc462015-05-27 10:17:41 +0530774 if (!strncmp("true", value, sizeof("true"))) {
775 if ((popcount(channel_mask) > 2) &&
776 (sample_rate > CODEC_BACKEND_DEFAULT_SAMPLE_RATE) &&
Aalique Grahame5cd12ff2017-10-19 10:57:00 -0700777 !(flags & (audio_output_flags_t)AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH)) {
Manish Dewangan837dc462015-05-27 10:17:41 +0530778 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
779 ALOGD("%s: MCH session defaulting sample rate to %d",
780 __func__, sample_rate);
781 }
782 }
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +0530783
784 /* Set sampling rate to 176.4 for DSD64
785 * and 352.8Khz for DSD128.
786 * Set Bit Width to 16. output will be 16 bit
787 * post DoP in ASM.
788 */
Aalique Grahame5cd12ff2017-10-19 10:57:00 -0700789 if ((flags & (audio_output_flags_t)AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH) &&
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +0530790 (format == AUDIO_FORMAT_DSD)) {
791 bit_width = 16;
792 if (sample_rate == INPUT_SAMPLING_RATE_DSD64)
793 sample_rate = OUTPUT_SAMPLING_RATE_DSD64;
794 else if (sample_rate == INPUT_SAMPLING_RATE_DSD128)
795 sample_rate = OUTPUT_SAMPLING_RATE_DSD128;
796 }
797
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +0530798
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800799 ALOGV("%s: flags: %x, format: %x sample_rate %d, profile %s, app_type %d",
800 __func__, flags, format, sample_rate, profile, app_type_cfg->app_type);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700801 list_for_each(node_i, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530802 s_info = node_to_item(node_i, struct streams_io_cfg, list);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530803 /* Along with flags do profile matching if set at either end.*/
804 if (s_info->flags.out_flags == flags &&
805 ((profile[0] == '\0' && s_info->profile[0] == '\0') ||
806 strncmp(s_info->profile, profile, sizeof(s_info->profile)) == 0)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530807 list_for_each(node_j, &s_info->format_list) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700808 sf_info = node_to_item(node_j, struct stream_format, list);
809 if (sf_info->format == format) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530810 if (set_app_type_cfg(s_info, app_type_cfg, sample_rate, bit_width))
Amit Shekhar6f461b12014-08-01 14:52:58 -0700811 return;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700812 }
813 }
814 }
815 }
816 list_for_each(node_i, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530817 s_info = node_to_item(node_i, struct streams_io_cfg, list);
818 if (s_info->flags.out_flags == AUDIO_OUTPUT_FLAG_PRIMARY) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700819 ALOGV("Compatible output profile not found.");
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530820 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
821 app_type_cfg->sample_rate = s_info->app_type_cfg.sample_rate;
822 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700823 ALOGV("%s Default to primary output: App type: %d sample_rate %d",
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530824 __func__, s_info->app_type_cfg.app_type, app_type_cfg->sample_rate);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700825 return;
826 }
827 }
828 ALOGW("%s: App type could not be selected. Falling back to default", __func__);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700829 app_type_cfg->app_type = platform_get_default_app_type(platform);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700830 app_type_cfg->sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700831 app_type_cfg->bit_width = 16;
832}
833
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +0530834static bool audio_is_this_native_usecase(struct audio_usecase *uc)
835{
836 bool native_usecase = false;
837 struct stream_out *out = (struct stream_out*) uc->stream.out;
838
839 if (PCM_PLAYBACK == uc->type && out != NULL &&
840 NATIVE_AUDIO_MODE_INVALID != platform_get_native_support() &&
841 is_offload_usecase(uc->id) &&
842 (out->sample_rate == OUTPUT_SAMPLING_RATE_44100))
843 native_usecase = true;
844
845 return native_usecase;
846}
847
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800848bool audio_extn_is_dsp_bit_width_enforce_mode_supported(audio_output_flags_t flags)
849{
850 /* DSP bitwidth enforce mode for ADM and AFE:
851 * includes:
852 * deep buffer, low latency, direct pcm and offload.
853 * excludes:
854 * ull(raw+fast), VOIP.
855 */
856 if ((flags & AUDIO_OUTPUT_FLAG_VOIP_RX) ||
857 ((flags & AUDIO_OUTPUT_FLAG_RAW) &&
858 (flags & AUDIO_OUTPUT_FLAG_FAST)))
859 return false;
860
861
862 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) ||
863 (flags & AUDIO_OUTPUT_FLAG_DIRECT) ||
864 (flags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) ||
865 (flags & AUDIO_OUTPUT_FLAG_PRIMARY))
866 return true;
867 else
868 return false;
869}
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800870
871static inline bool audio_is_vr_mode_on(struct audio_device *(__attribute__((unused)) adev))
872{
873 return adev->vr_audio_mode_enabled;
874}
875
Derek Chenf7092792017-05-23 12:23:53 -0400876static void audio_extn_btsco_get_sample_rate(int snd_device, int *sample_rate)
877{
878 switch (snd_device) {
879 case SND_DEVICE_OUT_BT_SCO:
880 case SND_DEVICE_IN_BT_SCO_MIC:
881 case SND_DEVICE_IN_BT_SCO_MIC_NREC:
882 *sample_rate = 8000;
883 break;
884 case SND_DEVICE_OUT_BT_SCO_WB:
885 case SND_DEVICE_IN_BT_SCO_MIC_WB:
886 case SND_DEVICE_IN_BT_SCO_MIC_WB_NREC:
887 *sample_rate = 16000;
888 break;
889 default:
890 ALOGD("%s:Not a BT SCO device, need not update sampling rate\n", __func__);
891 break;
892 }
893}
894
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530895void audio_extn_utils_update_stream_app_type_cfg_for_usecase(
896 struct audio_device *adev,
897 struct audio_usecase *usecase)
898{
899 ALOGV("%s", __func__);
900
901 switch(usecase->type) {
902 case PCM_PLAYBACK:
903 audio_extn_utils_update_stream_output_app_type_cfg(adev->platform,
904 &adev->streams_output_cfg_list,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -0800905 &usecase->stream.out->device_list,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530906 usecase->stream.out->flags,
Aalique Grahame65780b52017-09-27 14:59:56 -0700907 usecase->stream.out->hal_op_format,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530908 usecase->stream.out->sample_rate,
909 usecase->stream.out->bit_width,
910 usecase->stream.out->channel_mask,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530911 usecase->stream.out->profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530912 &usecase->stream.out->app_type_cfg);
913 ALOGV("%s Selected apptype: %d", __func__, usecase->stream.out->app_type_cfg.app_type);
914 break;
915 case PCM_CAPTURE:
Vikram Panduranga93f080e2017-06-07 18:16:14 -0700916 if (usecase->id == USECASE_AUDIO_RECORD_VOIP)
917 usecase->stream.in->app_type_cfg.app_type = APP_TYPE_VOIP_AUDIO;
918 else
919 audio_extn_utils_update_stream_input_app_type_cfg(adev->platform,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530920 &adev->streams_input_cfg_list,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -0800921 &usecase->stream.in->device_list,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530922 usecase->stream.in->flags,
923 usecase->stream.in->format,
924 usecase->stream.in->sample_rate,
925 usecase->stream.in->bit_width,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530926 usecase->stream.in->profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530927 &usecase->stream.in->app_type_cfg);
928 ALOGV("%s Selected apptype: %d", __func__, usecase->stream.in->app_type_cfg.app_type);
929 break;
Surendar Karka93cd25a2018-08-28 14:21:37 +0530930 case TRANSCODE_LOOPBACK_RX :
Siddartha Shaik343abc62017-08-08 11:15:25 +0530931 audio_extn_utils_update_stream_output_app_type_cfg(adev->platform,
932 &adev->streams_output_cfg_list,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -0800933 &usecase->stream.inout->out_config.device_list,
Siddartha Shaik343abc62017-08-08 11:15:25 +0530934 0,
935 usecase->stream.inout->out_config.format,
936 usecase->stream.inout->out_config.sample_rate,
937 usecase->stream.inout->out_config.bit_width,
938 usecase->stream.inout->out_config.channel_mask,
939 usecase->stream.inout->profile,
940 &usecase->stream.inout->out_app_type_cfg);
941 ALOGV("%s Selected apptype: %d", __func__, usecase->stream.inout->out_app_type_cfg.app_type);
942 break;
Derek Chenf7092792017-05-23 12:23:53 -0400943 case PCM_HFP_CALL:
944 switch (usecase->id) {
945 case USECASE_AUDIO_HFP_SCO:
946 case USECASE_AUDIO_HFP_SCO_WB:
947 audio_extn_btsco_get_sample_rate(usecase->out_snd_device,
948 &usecase->out_app_type_cfg.sample_rate);
Derek Chend7307012019-11-12 12:10:41 -0500949 usecase->in_app_type_cfg.sample_rate = usecase->out_app_type_cfg.sample_rate;
Derek Chenf7092792017-05-23 12:23:53 -0400950 break;
951 case USECASE_AUDIO_HFP_SCO_DOWNLINK:
952 case USECASE_AUDIO_HFP_SCO_WB_DOWNLINK:
953 audio_extn_btsco_get_sample_rate(usecase->in_snd_device,
954 &usecase->in_app_type_cfg.sample_rate);
Derek Chend7307012019-11-12 12:10:41 -0500955 usecase->out_app_type_cfg.sample_rate = usecase->in_app_type_cfg.sample_rate;
Derek Chenf7092792017-05-23 12:23:53 -0400956 break;
957 default:
958 ALOGE("%s: usecase id (%d) not supported, use default sample rate",
959 __func__, usecase->id);
960 usecase->in_app_type_cfg.sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
961 usecase->out_app_type_cfg.sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
962 break;
963 }
964 /* update out_app_type_cfg */
965 usecase->out_app_type_cfg.bit_width =
966 platform_get_snd_device_bit_width(usecase->out_snd_device);
967 usecase->out_app_type_cfg.app_type =
968 platform_get_default_app_type_v2(adev->platform, PCM_PLAYBACK);
969 /* update in_app_type_cfg */
970 usecase->in_app_type_cfg.bit_width =
971 platform_get_snd_device_bit_width(usecase->in_snd_device);
972 usecase->in_app_type_cfg.app_type =
973 platform_get_default_app_type_v2(adev->platform, PCM_CAPTURE);
974 ALOGV("%s Selected apptype: playback %d capture %d",
975 __func__, usecase->out_app_type_cfg.app_type, usecase->in_app_type_cfg.app_type);
976 break;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530977 default:
978 ALOGE("%s: app type cfg not supported for usecase type (%d)",
979 __func__, usecase->type);
980 }
981}
982
Aalique Grahame22e49102018-12-18 14:23:57 -0800983static int set_stream_app_type_mixer_ctrl(struct audio_device *adev,
984 int pcm_device_id, int app_type,
985 int acdb_dev_id, int sample_rate,
986 int stream_type,
987 snd_device_t snd_device)
988{
989
990 char mixer_ctl_name[MAX_LENGTH_MIXER_CONTROL_IN_INT];
991 struct mixer_ctl *ctl;
992 int app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT], len = 0, rc = 0;
993 int snd_device_be_idx = -1;
994
995 if (stream_type == PCM_PLAYBACK) {
996 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
997 "Audio Stream %d App Type Cfg", pcm_device_id);
998 } else if (stream_type == PCM_CAPTURE) {
999 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
1000 "Audio Stream Capture %d App Type Cfg", pcm_device_id);
1001 }
1002
1003 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
1004 if (!ctl) {
1005 ALOGE("%s: Could not get ctl for mixer cmd - %s",
1006 __func__, mixer_ctl_name);
1007 rc = -EINVAL;
1008 goto exit;
1009 }
1010 app_type_cfg[len++] = app_type;
1011 app_type_cfg[len++] = acdb_dev_id;
1012 app_type_cfg[len++] = sample_rate;
1013
1014 snd_device_be_idx = platform_get_snd_device_backend_index(snd_device);
1015 if (snd_device_be_idx > 0)
1016 app_type_cfg[len++] = snd_device_be_idx;
1017 ALOGV("%s: stream type %d app_type %d, acdb_dev_id %d "
1018 "sample rate %d, snd_device_be_idx %d",
1019 __func__, stream_type, app_type, acdb_dev_id, sample_rate,
1020 snd_device_be_idx);
1021 mixer_ctl_set_array(ctl, app_type_cfg, len);
1022
1023exit:
1024 return rc;
1025}
1026
1027static int audio_extn_utils_send_app_type_cfg_hfp(struct audio_device *adev,
1028 struct audio_usecase *usecase)
1029{
1030 int pcm_device_id, acdb_dev_id = 0, snd_device = usecase->out_snd_device;
1031 int32_t sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
1032 int app_type = 0, rc = 0;
Derek Chenf7092792017-05-23 12:23:53 -04001033 bool is_bus_dev_usecase = false;
Aalique Grahame22e49102018-12-18 14:23:57 -08001034
1035 ALOGV("%s", __func__);
1036
1037 if (usecase->type != PCM_HFP_CALL) {
Derek Chenf7092792017-05-23 12:23:53 -04001038 ALOGV("%s: not a HFP path, no need to cfg app type", __func__);
Aalique Grahame22e49102018-12-18 14:23:57 -08001039 rc = 0;
1040 goto exit_send_app_type_cfg;
1041 }
1042 if ((usecase->id != USECASE_AUDIO_HFP_SCO) &&
Derek Chenf7092792017-05-23 12:23:53 -04001043 (usecase->id != USECASE_AUDIO_HFP_SCO_WB) &&
1044 (usecase->id != USECASE_AUDIO_HFP_SCO_DOWNLINK) &&
1045 (usecase->id != USECASE_AUDIO_HFP_SCO_WB_DOWNLINK)) {
1046 ALOGV("%s: a usecase where app type cfg is not required", __func__);
Aalique Grahame22e49102018-12-18 14:23:57 -08001047 rc = 0;
1048 goto exit_send_app_type_cfg;
1049 }
1050
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08001051 if (compare_device_type(&usecase->device_list, AUDIO_DEVICE_OUT_BUS))
Derek Chenf7092792017-05-23 12:23:53 -04001052 is_bus_dev_usecase = true;
1053
Aalique Grahame22e49102018-12-18 14:23:57 -08001054 snd_device = usecase->out_snd_device;
1055 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_PLAYBACK);
1056
1057 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
1058 if (acdb_dev_id < 0) {
1059 ALOGE("%s: Couldn't get the acdb dev id", __func__);
1060 rc = -EINVAL;
1061 goto exit_send_app_type_cfg;
1062 }
1063
1064 if (usecase->type == PCM_HFP_CALL) {
1065
1066 /* config HFP session:1 playback path */
Derek Chenf7092792017-05-23 12:23:53 -04001067 if (is_bus_dev_usecase) {
1068 app_type = usecase->out_app_type_cfg.app_type;
1069 sample_rate= usecase->out_app_type_cfg.sample_rate;
1070 } else {
1071 snd_device = SND_DEVICE_NONE; // use legacy behavior
1072 app_type = platform_get_default_app_type_v2(adev->platform, PCM_PLAYBACK);
1073 sample_rate= CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
1074 }
Aalique Grahame22e49102018-12-18 14:23:57 -08001075 rc = set_stream_app_type_mixer_ctrl(adev, pcm_device_id, app_type,
1076 acdb_dev_id, sample_rate,
1077 PCM_PLAYBACK,
Derek Chenf7092792017-05-23 12:23:53 -04001078 snd_device);
Aalique Grahame22e49102018-12-18 14:23:57 -08001079 if (rc < 0)
1080 goto exit_send_app_type_cfg;
1081
1082 /* config HFP session:1 capture path */
Derek Chenf7092792017-05-23 12:23:53 -04001083 if (is_bus_dev_usecase) {
1084 snd_device = usecase->in_snd_device;
1085 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_CAPTURE);
1086 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
1087 if (acdb_dev_id < 0) {
1088 ALOGE("%s: Couldn't get the acdb dev id", __func__);
1089 rc = -EINVAL;
1090 goto exit_send_app_type_cfg;
1091 }
1092 app_type = usecase->in_app_type_cfg.app_type;
1093 sample_rate= usecase->in_app_type_cfg.sample_rate;
1094 } else {
1095 snd_device = SND_DEVICE_NONE; // use legacy behavior
1096 app_type = platform_get_default_app_type_v2(adev->platform, PCM_CAPTURE);
1097 }
Aalique Grahame22e49102018-12-18 14:23:57 -08001098 rc = set_stream_app_type_mixer_ctrl(adev, pcm_device_id, app_type,
1099 acdb_dev_id, sample_rate,
1100 PCM_CAPTURE,
Derek Chenf7092792017-05-23 12:23:53 -04001101 snd_device);
Aalique Grahame22e49102018-12-18 14:23:57 -08001102 if (rc < 0)
1103 goto exit_send_app_type_cfg;
1104
Derek Chenf7092792017-05-23 12:23:53 -04001105 if (is_bus_dev_usecase)
1106 goto exit_send_app_type_cfg;
1107
Aalique Grahame22e49102018-12-18 14:23:57 -08001108 /* config HFP session:2 capture path */
1109 pcm_device_id = HFP_ASM_RX_TX;
1110 snd_device = usecase->in_snd_device;
1111 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
1112 if (acdb_dev_id <= 0) {
1113 ALOGE("%s: Couldn't get the acdb dev id", __func__);
1114 rc = -EINVAL;
1115 goto exit_send_app_type_cfg;
1116 }
1117 app_type = platform_get_default_app_type_v2(adev->platform, PCM_CAPTURE);
1118 rc = set_stream_app_type_mixer_ctrl(adev, pcm_device_id, app_type,
1119 acdb_dev_id, sample_rate, PCM_CAPTURE,
1120 SND_DEVICE_NONE);
1121 if (rc < 0)
1122 goto exit_send_app_type_cfg;
1123
1124 /* config HFP session:2 playback path */
1125 app_type = platform_get_default_app_type_v2(adev->platform, PCM_PLAYBACK);
1126 rc = set_stream_app_type_mixer_ctrl(adev, pcm_device_id, app_type,
1127 acdb_dev_id, sample_rate,
1128 PCM_PLAYBACK, SND_DEVICE_NONE);
1129 if (rc < 0)
1130 goto exit_send_app_type_cfg;
1131 }
1132
1133 rc = 0;
1134exit_send_app_type_cfg:
1135 return rc;
1136}
1137
Zhou Song06761dd2019-04-28 18:08:17 +08001138int audio_extn_utils_get_app_sample_rate_for_device(
1139 struct audio_device *adev,
1140 struct audio_usecase *usecase, int snd_device)
1141{
1142 char value[PROPERTY_VALUE_MAX] = {0};
1143 int sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
1144
1145 if ((usecase->type == PCM_PLAYBACK) && (usecase->stream.out != NULL)) {
1146 property_get("vendor.audio.playback.mch.downsample",value,"");
1147 if (!strncmp("true", value, sizeof("true"))) {
1148 if ((popcount(usecase->stream.out->channel_mask) > 2) &&
1149 (usecase->stream.out->app_type_cfg.sample_rate > CODEC_BACKEND_DEFAULT_SAMPLE_RATE) &&
1150 !(usecase->stream.out->flags &
1151 (audio_output_flags_t)AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH))
1152 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
1153 }
1154
1155 if (usecase->id == USECASE_AUDIO_PLAYBACK_VOIP) {
1156 usecase->stream.out->app_type_cfg.sample_rate = usecase->stream.out->sample_rate;
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08001157 } else if (compare_device_type(&usecase->stream.out->device_list,
1158 AUDIO_DEVICE_OUT_SPEAKER)) {
Zhou Song06761dd2019-04-28 18:08:17 +08001159 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
1160 } else if ((snd_device == SND_DEVICE_OUT_HDMI ||
1161 snd_device == SND_DEVICE_OUT_USB_HEADSET ||
1162 snd_device == SND_DEVICE_OUT_DISPLAY_PORT) &&
1163 (usecase->stream.out->sample_rate >= OUTPUT_SAMPLING_RATE_44100)) {
1164 /*
1165 * To best utlize DSP, check if the stream sample rate is supported/multiple of
1166 * configured device sample rate, if not update the COPP rate to be equal to the
1167 * device sample rate, else open COPP at stream sample rate
1168 */
1169 platform_check_and_update_copp_sample_rate(adev->platform, snd_device,
1170 usecase->stream.out->sample_rate,
1171 &usecase->stream.out->app_type_cfg.sample_rate);
1172 } else if (((snd_device != SND_DEVICE_OUT_HEADPHONES_44_1 &&
1173 !audio_is_this_native_usecase(usecase)) &&
1174 usecase->stream.out->sample_rate == OUTPUT_SAMPLING_RATE_44100) ||
1175 (usecase->stream.out->sample_rate < OUTPUT_SAMPLING_RATE_44100)) {
1176 /* Reset to default if no native stream is active*/
1177 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Zhou Song9e9b3812019-12-30 17:52:10 +08001178 } else if (usecase->out_snd_device == SND_DEVICE_OUT_BT_A2DP ||
1179 usecase->out_snd_device == SND_DEVICE_OUT_SPEAKER_AND_BT_A2DP ||
1180 usecase->out_snd_device == SND_DEVICE_OUT_SPEAKER_SAFE_AND_BT_A2DP) {
Zhou Song06761dd2019-04-28 18:08:17 +08001181 /*
1182 * For a2dp playback get encoder sampling rate and set copp sampling rate,
1183 * for bit width use the stream param only.
1184 */
1185 audio_extn_a2dp_get_enc_sample_rate(&usecase->stream.out->app_type_cfg.sample_rate);
1186 ALOGI("%s using %d sample rate rate for A2DP CoPP",
1187 __func__, usecase->stream.out->app_type_cfg.sample_rate);
1188 }
1189 audio_extn_btsco_get_sample_rate(snd_device, &usecase->stream.out->app_type_cfg.sample_rate);
1190 sample_rate = usecase->stream.out->app_type_cfg.sample_rate;
1191
1192 if (((usecase->stream.out->format == AUDIO_FORMAT_E_AC3) ||
1193 (usecase->stream.out->format == AUDIO_FORMAT_E_AC3_JOC) ||
1194 (usecase->stream.out->format == AUDIO_FORMAT_DOLBY_TRUEHD))
1195 && audio_extn_passthru_is_passthrough_stream(usecase->stream.out)
1196 && !audio_extn_passthru_is_convert_supported(adev, usecase->stream.out)) {
1197 sample_rate = sample_rate * 4;
1198 if (sample_rate > HDMI_PASSTHROUGH_MAX_SAMPLE_RATE)
1199 sample_rate = HDMI_PASSTHROUGH_MAX_SAMPLE_RATE;
1200 }
1201 } else if (usecase->type == PCM_CAPTURE) {
1202 if (usecase->stream.in != NULL) {
1203 if (usecase->id == USECASE_AUDIO_RECORD_VOIP)
1204 usecase->stream.in->app_type_cfg.sample_rate = usecase->stream.in->sample_rate;
1205 if (voice_is_in_call_rec_stream(usecase->stream.in)) {
1206 audio_extn_btsco_get_sample_rate(usecase->in_snd_device,
1207 &usecase->stream.in->app_type_cfg.sample_rate);
1208 } else {
1209 audio_extn_btsco_get_sample_rate(snd_device,
1210 &usecase->stream.in->app_type_cfg.sample_rate);
1211 }
1212 sample_rate = usecase->stream.in->app_type_cfg.sample_rate;
1213 } else if (usecase->id == USECASE_AUDIO_SPKR_CALIB_TX) {
Sujin Panickerb6d76262019-09-09 10:55:38 +05301214 if ((property_get("vendor.audio.spkr_prot.tx.sampling_rate", value, NULL) > 0))
1215 sample_rate = atoi(value);
1216 else
1217 sample_rate = SAMPLE_RATE_8000;
Zhou Song06761dd2019-04-28 18:08:17 +08001218 }
1219 } else if (usecase->type == TRANSCODE_LOOPBACK_RX) {
1220 sample_rate = usecase->stream.inout->out_config.sample_rate;
1221 }
1222 return sample_rate;
1223}
1224
Siena Richard7c2db772016-12-21 11:32:34 -08001225static int send_app_type_cfg_for_device(struct audio_device *adev,
1226 struct audio_usecase *usecase,
1227 int split_snd_device)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001228{
1229 char mixer_ctl_name[MAX_LENGTH_MIXER_CONTROL_IN_INT];
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301230 size_t app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT] = {0};
1231 int len = 0, rc;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001232 struct mixer_ctl *ctl;
Siena Richard7c2db772016-12-21 11:32:34 -08001233 int pcm_device_id = 0, acdb_dev_id, app_type;
1234 int snd_device = split_snd_device, snd_device_be_idx = -1;
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +05301235 int32_t sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Varun Balaraje49253e2017-07-06 19:48:56 +05301236 struct streams_io_cfg *s_info = NULL;
1237 struct listnode *node = NULL;
Nikhil Laturkarac4a7492017-08-31 18:27:19 +05301238 int bd_app_type = 0;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001239
Siena Richard7c2db772016-12-21 11:32:34 -08001240 ALOGV("%s: usecase->out_snd_device %s, usecase->in_snd_device %s, split_snd_device %s",
1241 __func__, platform_get_snd_device_name(usecase->out_snd_device),
1242 platform_get_snd_device_name(usecase->in_snd_device),
1243 platform_get_snd_device_name(split_snd_device));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001244
Siddartha Shaik343abc62017-08-08 11:15:25 +05301245 if (usecase->type != PCM_PLAYBACK && usecase->type != PCM_CAPTURE &&
Surendar Karka93cd25a2018-08-28 14:21:37 +05301246 usecase->type != TRANSCODE_LOOPBACK_RX) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301247 ALOGE("%s: not a playback/capture path, no need to cfg app type", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001248 rc = 0;
1249 goto exit_send_app_type_cfg;
1250 }
1251 if ((usecase->id != USECASE_AUDIO_PLAYBACK_DEEP_BUFFER) &&
Vignesh Kulothungan0a2eff02019-07-11 16:30:13 -07001252 (usecase->id != USECASE_AUDIO_PLAYBACK_WITH_HAPTICS) &&
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001253 (usecase->id != USECASE_AUDIO_PLAYBACK_LOW_LATENCY) &&
1254 (usecase->id != USECASE_AUDIO_PLAYBACK_MULTI_CH) &&
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -08001255 (usecase->id != USECASE_AUDIO_PLAYBACK_ULL) &&
Vikram Panduranga93f080e2017-06-07 18:16:14 -07001256 (usecase->id != USECASE_AUDIO_PLAYBACK_VOIP) &&
Surendar Karka93cd25a2018-08-28 14:21:37 +05301257 (usecase->id != USECASE_AUDIO_TRANSCODE_LOOPBACK_RX) &&
Varun Balaraje49253e2017-07-06 19:48:56 +05301258 (!is_interactive_usecase(usecase->id)) &&
Srikanth Uyyala9d551402015-08-25 16:03:42 +05301259 (!is_offload_usecase(usecase->id)) &&
Derek Chenf6318be2017-06-12 17:16:24 -04001260 (usecase->type != PCM_CAPTURE) &&
1261 (!audio_extn_auto_hal_is_bus_device_usecase(usecase->id))) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301262 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 -07001263 rc = 0;
1264 goto exit_send_app_type_cfg;
1265 }
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -08001266
1267 //if VR is active then only send the mixer control
1268 if (usecase->id == USECASE_AUDIO_PLAYBACK_ULL && !audio_is_vr_mode_on(adev)) {
1269 ALOGI("ULL doesnt need sending app type cfg, returning");
1270 rc = 0;
1271 goto exit_send_app_type_cfg;
1272 }
1273
Surendar Karka93cd25a2018-08-28 14:21:37 +05301274 if (usecase->type == PCM_PLAYBACK || usecase->type == TRANSCODE_LOOPBACK_RX) {
Ben Romberger1fafdde2015-09-09 19:43:15 -07001275 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_PLAYBACK);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301276 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
1277 "Audio Stream %d App Type Cfg", pcm_device_id);
Ben Romberger1fafdde2015-09-09 19:43:15 -07001278 } else if (usecase->type == PCM_CAPTURE) {
Ben Romberger1fafdde2015-09-09 19:43:15 -07001279 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_CAPTURE);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301280 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
1281 "Audio Stream Capture %d App Type Cfg", pcm_device_id);
Srikanth Uyyala9d551402015-08-25 16:03:42 +05301282 }
Siena Richard7c2db772016-12-21 11:32:34 -08001283
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001284 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
1285 if (!ctl) {
1286 ALOGE("%s: Could not get ctl for mixer cmd - %s", __func__,
1287 mixer_ctl_name);
1288 rc = -EINVAL;
1289 goto exit_send_app_type_cfg;
1290 }
Aditya Bavanari701a6992017-03-30 19:17:16 +05301291 snd_device = platform_get_spkr_prot_snd_device(snd_device);
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301292 if (voice_is_in_call_rec_stream(usecase->stream.in) && usecase->type == PCM_CAPTURE) {
1293 snd_device_t voice_device = voice_get_incall_rec_snd_device(usecase->in_snd_device);
1294 acdb_dev_id = platform_get_snd_device_acdb_id(voice_device);
1295 ALOGV("acdb id for voice call use case %d", acdb_dev_id);
1296 } else {
1297 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
1298 }
Rohit Kumar1181b292017-01-31 18:07:17 +05301299 if (acdb_dev_id <= 0) {
1300 ALOGE("%s: Couldn't get the acdb dev id", __func__);
1301 rc = -EINVAL;
1302 goto exit_send_app_type_cfg;
1303 }
1304
Siena Richard7c2db772016-12-21 11:32:34 -08001305 snd_device_be_idx = platform_get_snd_device_backend_index(snd_device);
1306 if (snd_device_be_idx < 0) {
1307 ALOGE("%s: Couldn't get the backend index for snd device %s ret=%d",
1308 __func__, platform_get_snd_device_name(snd_device),
1309 snd_device_be_idx);
1310 }
1311
Zhou Song06761dd2019-04-28 18:08:17 +08001312 sample_rate = audio_extn_utils_get_app_sample_rate_for_device(adev, usecase, snd_device);
1313
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301314 if ((usecase->type == PCM_PLAYBACK) && (usecase->stream.out != NULL)) {
Varun Balaraje49253e2017-07-06 19:48:56 +05301315 /* Interactive streams are supported with only direct app type id.
1316 * Get Direct profile app type and use it for interactive streams
1317 */
1318 list_for_each(node, &adev->streams_output_cfg_list) {
1319 s_info = node_to_item(node, struct streams_io_cfg, list);
Aalique Grahame5cd12ff2017-10-19 10:57:00 -07001320 if (s_info->flags.out_flags == (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_BD |
Nikhil Laturkarac4a7492017-08-31 18:27:19 +05301321 AUDIO_OUTPUT_FLAG_DIRECT_PCM |
1322 AUDIO_OUTPUT_FLAG_DIRECT))
1323 bd_app_type = s_info->app_type_cfg.app_type;
Varun Balaraje49253e2017-07-06 19:48:56 +05301324 }
Aalique Grahame5cd12ff2017-10-19 10:57:00 -07001325 if (usecase->stream.out->flags == (audio_output_flags_t)AUDIO_OUTPUT_FLAG_INTERACTIVE)
Nikhil Laturkarac4a7492017-08-31 18:27:19 +05301326 app_type = bd_app_type;
Varun Balaraje49253e2017-07-06 19:48:56 +05301327 else
1328 app_type = usecase->stream.out->app_type_cfg.app_type;
Siena Richard7c2db772016-12-21 11:32:34 -08001329 app_type_cfg[len++] = app_type;
Mingming Yin21854652016-04-13 11:54:02 -07001330 app_type_cfg[len++] = acdb_dev_id;
Naresh Tanniru3a406772017-05-10 13:09:05 -07001331 app_type_cfg[len++] = sample_rate;
1332
Siena Richard7c2db772016-12-21 11:32:34 -08001333 if (snd_device_be_idx > 0)
1334 app_type_cfg[len++] = snd_device_be_idx;
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301335
Siena Richard7c2db772016-12-21 11:32:34 -08001336 ALOGI("%s PLAYBACK app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
1337 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301338
1339 } else if ((usecase->type == PCM_CAPTURE) && (usecase->stream.in != NULL)) {
Siena Richard7c2db772016-12-21 11:32:34 -08001340 app_type = usecase->stream.in->app_type_cfg.app_type;
1341 app_type_cfg[len++] = app_type;
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301342 app_type_cfg[len++] = acdb_dev_id;
Siena Richard7c2db772016-12-21 11:32:34 -08001343 app_type_cfg[len++] = sample_rate;
1344 if (snd_device_be_idx > 0)
1345 app_type_cfg[len++] = snd_device_be_idx;
1346 ALOGI("%s CAPTURE app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
1347 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301348 } else {
Siena Richard7c2db772016-12-21 11:32:34 -08001349 app_type = platform_get_default_app_type_v2(adev->platform, usecase->type);
Surendar Karka93cd25a2018-08-28 14:21:37 +05301350 if(usecase->type == TRANSCODE_LOOPBACK_RX) {
Siddartha Shaik343abc62017-08-08 11:15:25 +05301351 app_type = usecase->stream.inout->out_app_type_cfg.app_type;
1352 }
Siena Richard7c2db772016-12-21 11:32:34 -08001353 app_type_cfg[len++] = app_type;
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301354 app_type_cfg[len++] = acdb_dev_id;
1355 app_type_cfg[len++] = sample_rate;
Siena Richard7c2db772016-12-21 11:32:34 -08001356 if (snd_device_be_idx > 0)
1357 app_type_cfg[len++] = snd_device_be_idx;
1358 ALOGI("%s default app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
1359 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Ashish Jainc02430d2016-01-04 10:42:43 +05301360 }
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301361
Siddartha Shaik343abc62017-08-08 11:15:25 +05301362 if(ctl)
1363 mixer_ctl_set_array(ctl, app_type_cfg, len);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001364 rc = 0;
1365exit_send_app_type_cfg:
1366 return rc;
1367}
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001368
Deeraj Soman14230922019-01-30 16:39:30 +05301369static int audio_extn_utils_check_input_parameters(uint32_t sample_rate,
1370 audio_format_t format,
1371 int channel_count)
1372{
1373 int ret = 0;
1374
1375 if (((format != AUDIO_FORMAT_PCM_16_BIT) && (format != AUDIO_FORMAT_PCM_8_24_BIT) &&
1376 (format != AUDIO_FORMAT_PCM_24_BIT_PACKED) && (format != AUDIO_FORMAT_PCM_32_BIT) &&
1377 (format != AUDIO_FORMAT_PCM_FLOAT)) &&
1378 !voice_extn_compress_voip_is_format_supported(format) &&
1379 !audio_extn_compr_cap_format_supported(format) &&
1380 !audio_extn_cin_format_supported(format))
1381 ret = -EINVAL;
1382
1383 switch (channel_count) {
1384 case 1:
1385 case 2:
1386 case 3:
1387 case 4:
1388 case 6:
1389 case 8:
1390 break;
1391 default:
1392 ret = -EINVAL;
1393 }
1394
1395 switch (sample_rate) {
1396 case 8000:
1397 case 11025:
1398 case 12000:
1399 case 16000:
1400 case 22050:
1401 case 24000:
1402 case 32000:
1403 case 44100:
1404 case 48000:
1405 case 88200:
1406 case 96000:
1407 case 176400:
1408 case 192000:
1409 break;
1410 default:
1411 ret = -EINVAL;
1412 }
1413
1414 return ret;
1415}
1416
1417static inline uint32_t audio_extn_utils_nearest_multiple(uint32_t num, uint32_t multiplier)
1418{
1419 uint32_t remainder = 0;
1420
1421 if (!multiplier)
1422 return num;
1423
1424 remainder = num % multiplier;
1425 if (remainder)
1426 num += (multiplier - remainder);
1427
1428 return num;
1429}
1430
1431static inline uint32_t audio_extn_utils_lcm(uint32_t num1, uint32_t num2)
1432{
1433 uint32_t high = num1, low = num2, temp = 0;
1434
1435 if (!num1 || !num2)
1436 return 0;
1437
1438 if (num1 < num2) {
1439 high = num2;
1440 low = num1;
1441 }
1442
1443 while (low != 0) {
1444 temp = low;
1445 low = high % low;
1446 high = temp;
1447 }
1448 return (num1 * num2)/high;
1449}
1450
Siena Richard7c2db772016-12-21 11:32:34 -08001451int audio_extn_utils_send_app_type_cfg(struct audio_device *adev,
1452 struct audio_usecase *usecase)
1453{
1454 int i, num_devices = 0;
1455 snd_device_t new_snd_devices[SND_DEVICE_OUT_END] = {0};
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301456 snd_device_t in_snd_device = usecase->in_snd_device;
Siena Richard7c2db772016-12-21 11:32:34 -08001457 int rc = 0;
1458
Aalique Grahame22e49102018-12-18 14:23:57 -08001459 if (usecase->type == PCM_HFP_CALL) {
1460 return audio_extn_utils_send_app_type_cfg_hfp(adev, usecase);
1461 }
1462
Siena Richard7c2db772016-12-21 11:32:34 -08001463 switch (usecase->type) {
1464 case PCM_PLAYBACK:
Surendar Karka93cd25a2018-08-28 14:21:37 +05301465 case TRANSCODE_LOOPBACK_RX:
Siena Richard7c2db772016-12-21 11:32:34 -08001466 ALOGD("%s: usecase->out_snd_device %s",
1467 __func__, platform_get_snd_device_name(usecase->out_snd_device));
1468 /* check for out combo device */
1469 if (platform_split_snd_device(adev->platform,
1470 usecase->out_snd_device,
1471 &num_devices, new_snd_devices)) {
1472 new_snd_devices[0] = usecase->out_snd_device;
1473 num_devices = 1;
1474 }
1475 break;
1476 case PCM_CAPTURE:
1477 ALOGD("%s: usecase->in_snd_device %s",
1478 __func__, platform_get_snd_device_name(usecase->in_snd_device));
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301479 if (voice_is_in_call_rec_stream(usecase->stream.in)) {
1480 in_snd_device = voice_get_incall_rec_backend_device(usecase->stream.in);
1481 }
Siena Richard7c2db772016-12-21 11:32:34 -08001482 /* check for in combo device */
1483 if (platform_split_snd_device(adev->platform,
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301484 in_snd_device,
Siena Richard7c2db772016-12-21 11:32:34 -08001485 &num_devices, new_snd_devices)) {
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301486 new_snd_devices[0] = in_snd_device;
Siena Richard7c2db772016-12-21 11:32:34 -08001487 num_devices = 1;
1488 }
1489 break;
1490 default:
1491 ALOGI("%s: not a playback/capture path, no need to cfg app type", __func__);
1492 rc = 0;
1493 break;
1494 }
1495
1496 for (i = 0; i < num_devices; i++) {
1497 rc = send_app_type_cfg_for_device(adev, usecase, new_snd_devices[i]);
1498 if (rc)
1499 break;
1500 }
1501
1502 return rc;
1503}
1504
Preetam Singh Ranawat9519e9c2015-11-18 16:05:55 +05301505int read_line_from_file(const char *path, char *buf, size_t count)
1506{
1507 char * fgets_ret;
1508 FILE * fd;
1509 int rv;
1510
1511 fd = fopen(path, "r");
1512 if (fd == NULL)
1513 return -1;
1514
1515 fgets_ret = fgets(buf, (int)count, fd);
1516 if (NULL != fgets_ret) {
1517 rv = (int)strlen(buf);
1518 } else {
1519 rv = ferror(fd);
1520 }
1521 fclose(fd);
1522
1523 return rv;
1524}
1525
Ashish Jainf1eaa582016-05-23 20:54:24 +05301526/*Translates ALSA formats to AOSP PCM formats*/
1527audio_format_t alsa_format_to_hal(uint32_t alsa_format)
1528{
1529 audio_format_t format;
1530
1531 switch(alsa_format) {
1532 case SNDRV_PCM_FORMAT_S16_LE:
1533 format = AUDIO_FORMAT_PCM_16_BIT;
1534 break;
1535 case SNDRV_PCM_FORMAT_S24_3LE:
1536 format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
1537 break;
1538 case SNDRV_PCM_FORMAT_S24_LE:
1539 format = AUDIO_FORMAT_PCM_8_24_BIT;
1540 break;
1541 case SNDRV_PCM_FORMAT_S32_LE:
1542 format = AUDIO_FORMAT_PCM_32_BIT;
1543 break;
1544 default:
1545 ALOGW("Incorrect ALSA format");
1546 format = AUDIO_FORMAT_INVALID;
1547 }
1548 return format;
1549}
1550
1551/*Translates hal format (AOSP) to alsa formats*/
1552uint32_t hal_format_to_alsa(audio_format_t hal_format)
1553{
1554 uint32_t alsa_format;
1555
1556 switch (hal_format) {
1557 case AUDIO_FORMAT_PCM_32_BIT: {
1558 if (platform_supports_true_32bit())
1559 alsa_format = SNDRV_PCM_FORMAT_S32_LE;
1560 else
1561 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1562 }
1563 break;
1564 case AUDIO_FORMAT_PCM_8_BIT:
1565 alsa_format = SNDRV_PCM_FORMAT_S8;
1566 break;
1567 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
1568 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1569 break;
1570 case AUDIO_FORMAT_PCM_8_24_BIT: {
1571 if (platform_supports_true_32bit())
1572 alsa_format = SNDRV_PCM_FORMAT_S32_LE;
1573 else
1574 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1575 }
1576 break;
1577 case AUDIO_FORMAT_PCM_FLOAT:
1578 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1579 break;
1580 default:
1581 case AUDIO_FORMAT_PCM_16_BIT:
1582 alsa_format = SNDRV_PCM_FORMAT_S16_LE;
1583 break;
1584 }
1585 return alsa_format;
1586}
1587
Ashish Jain83a6cc22016-06-28 14:34:17 +05301588/*Translates PCM formats to AOSP formats*/
1589audio_format_t pcm_format_to_hal(uint32_t pcm_format)
1590{
1591 audio_format_t format = AUDIO_FORMAT_INVALID;
1592
1593 switch(pcm_format) {
1594 case PCM_FORMAT_S16_LE:
1595 format = AUDIO_FORMAT_PCM_16_BIT;
1596 break;
1597 case PCM_FORMAT_S24_3LE:
1598 format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
1599 break;
1600 case PCM_FORMAT_S24_LE:
1601 format = AUDIO_FORMAT_PCM_8_24_BIT;
1602 break;
1603 case PCM_FORMAT_S32_LE:
1604 format = AUDIO_FORMAT_PCM_32_BIT;
1605 break;
1606 default:
1607 ALOGW("Incorrect PCM format");
1608 format = AUDIO_FORMAT_INVALID;
1609 }
1610 return format;
1611}
1612
1613/*Translates hal format (AOSP) to alsa formats*/
1614uint32_t hal_format_to_pcm(audio_format_t hal_format)
1615{
1616 uint32_t pcm_format;
1617
1618 switch (hal_format) {
1619 case AUDIO_FORMAT_PCM_32_BIT:
1620 case AUDIO_FORMAT_PCM_8_24_BIT:
1621 case AUDIO_FORMAT_PCM_FLOAT: {
1622 if (platform_supports_true_32bit())
1623 pcm_format = PCM_FORMAT_S32_LE;
1624 else
1625 pcm_format = PCM_FORMAT_S24_3LE;
1626 }
1627 break;
1628 case AUDIO_FORMAT_PCM_8_BIT:
1629 pcm_format = PCM_FORMAT_S8;
1630 break;
1631 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
1632 pcm_format = PCM_FORMAT_S24_3LE;
1633 break;
1634 default:
1635 case AUDIO_FORMAT_PCM_16_BIT:
1636 pcm_format = PCM_FORMAT_S16_LE;
1637 break;
1638 }
1639 return pcm_format;
1640}
1641
Ashish Jainf1eaa582016-05-23 20:54:24 +05301642uint32_t get_alsa_fragment_size(uint32_t bytes_per_sample,
1643 uint32_t sample_rate,
Deeraj Soman65358ab2019-02-07 15:40:49 +05301644 uint32_t noOfChannels,
1645 int64_t duration_ms)
Ashish Jainf1eaa582016-05-23 20:54:24 +05301646{
1647 uint32_t fragment_size = 0;
1648 uint32_t pcm_offload_time = PCM_OFFLOAD_BUFFER_DURATION;
1649
Deeraj Soman65358ab2019-02-07 15:40:49 +05301650 if (duration_ms >= MIN_OFFLOAD_BUFFER_DURATION_MS && duration_ms <= MAX_OFFLOAD_BUFFER_DURATION_MS)
1651 pcm_offload_time = duration_ms;
1652
Ashish Jainf1eaa582016-05-23 20:54:24 +05301653 fragment_size = (pcm_offload_time
1654 * sample_rate
1655 * bytes_per_sample
1656 * noOfChannels)/1000;
1657 if (fragment_size < MIN_PCM_OFFLOAD_FRAGMENT_SIZE)
1658 fragment_size = MIN_PCM_OFFLOAD_FRAGMENT_SIZE;
1659 else if (fragment_size > MAX_PCM_OFFLOAD_FRAGMENT_SIZE)
1660 fragment_size = MAX_PCM_OFFLOAD_FRAGMENT_SIZE;
1661 /*To have same PCM samples for all channels, the buffer size requires to
1662 *be multiple of (number of channels * bytes per sample)
1663 *For writes to succeed, the buffer must be written at address which is multiple of 32
1664 */
Aalique Grahameb85f2d92017-10-16 17:53:23 -07001665 fragment_size = ALIGN(fragment_size, (bytes_per_sample * noOfChannels * 32));
Ashish Jainf1eaa582016-05-23 20:54:24 +05301666
1667 ALOGI("PCM offload Fragment size to %d bytes", fragment_size);
1668 return fragment_size;
1669}
1670
1671/* Calculates the fragment size required to configure compress session.
1672 * Based on the alsa format selected, decide if conversion is needed in
1673
1674 * HAL ( e.g. convert AUDIO_FORMAT_PCM_FLOAT input format to
1675 * AUDIO_FORMAT_PCM_24_BIT_PACKED before writing to the compress driver.
1676 */
1677void audio_extn_utils_update_direct_pcm_fragment_size(struct stream_out *out)
1678{
Ashish Jain83a6cc22016-06-28 14:34:17 +05301679 audio_format_t dst_format = out->hal_op_format;
1680 audio_format_t src_format = out->hal_ip_format;
Ashish Jainf1eaa582016-05-23 20:54:24 +05301681 uint32_t hal_op_bytes_per_sample = audio_bytes_per_sample(dst_format);
1682 uint32_t hal_ip_bytes_per_sample = audio_bytes_per_sample(src_format);
1683
1684 out->compr_config.fragment_size =
1685 get_alsa_fragment_size(hal_op_bytes_per_sample,
1686 out->sample_rate,
Deeraj Soman65358ab2019-02-07 15:40:49 +05301687 popcount(out->channel_mask),
1688 out->info.duration_us / 1000);
Ashish Jainf1eaa582016-05-23 20:54:24 +05301689
1690 if ((src_format != dst_format) &&
1691 hal_op_bytes_per_sample != hal_ip_bytes_per_sample) {
1692
Ashish Jain83a6cc22016-06-28 14:34:17 +05301693 out->hal_fragment_size =
Ashish Jainf1eaa582016-05-23 20:54:24 +05301694 ((out->compr_config.fragment_size * hal_ip_bytes_per_sample) /
1695 hal_op_bytes_per_sample);
1696 ALOGI("enable conversion hal_input_fragment_size is %d src_format %x dst_format %x",
Ashish Jain83a6cc22016-06-28 14:34:17 +05301697 out->hal_fragment_size, src_format, dst_format);
Ashish Jainf1eaa582016-05-23 20:54:24 +05301698 } else {
Ashish Jain83a6cc22016-06-28 14:34:17 +05301699 out->hal_fragment_size = out->compr_config.fragment_size;
Ashish Jainf1eaa582016-05-23 20:54:24 +05301700 }
1701}
1702
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301703/* converts pcm format 24_8 to 8_24 inplace */
1704size_t audio_extn_utils_convert_format_24_8_to_8_24(void *buf, size_t bytes)
1705{
1706 size_t i = 0;
1707 int *int_buf_stream = buf;
1708
1709 if ((bytes % 4) != 0) {
1710 ALOGE("%s: wrong inout buffer! ... is not 32 bit aligned ", __func__);
1711 return -EINVAL;
1712 }
1713
1714 for (; i < (bytes / 4); i++)
1715 int_buf_stream[i] >>= 8;
1716
1717 return bytes;
1718}
1719
Xiaojun Sang782e5b12020-06-29 21:13:06 +08001720#ifdef AUDIO_GKI_ENABLED
1721int get_snd_codec_id(audio_format_t format)
1722{
1723 int id = 0;
1724
1725 switch (format & AUDIO_FORMAT_MAIN_MASK) {
1726 case AUDIO_FORMAT_MP3:
1727 id = SND_AUDIOCODEC_MP3;
1728 break;
1729 case AUDIO_FORMAT_AAC:
1730 id = SND_AUDIOCODEC_AAC;
1731 break;
1732 case AUDIO_FORMAT_AAC_ADTS:
1733 id = SND_AUDIOCODEC_AAC;
1734 break;
1735 case AUDIO_FORMAT_AAC_LATM:
1736 id = SND_AUDIOCODEC_AAC;
1737 break;
1738 case AUDIO_FORMAT_PCM:
1739 id = SND_AUDIOCODEC_PCM;
1740 break;
1741 case AUDIO_FORMAT_FLAC:
1742 case AUDIO_FORMAT_ALAC:
1743 case AUDIO_FORMAT_APE:
1744 case AUDIO_FORMAT_VORBIS:
1745 case AUDIO_FORMAT_WMA:
1746 case AUDIO_FORMAT_WMA_PRO:
1747 case AUDIO_FORMAT_DSD:
1748 case AUDIO_FORMAT_APTX:
1749 id = SND_AUDIOCODEC_BESPOKE;
1750 break;
1751 case AUDIO_FORMAT_MP2:
1752 id = SND_AUDIOCODEC_MP2;
1753 break;
1754 case AUDIO_FORMAT_AC3:
1755 id = SND_AUDIOCODEC_AC3;
1756 break;
1757 case AUDIO_FORMAT_E_AC3:
1758 case AUDIO_FORMAT_E_AC3_JOC:
1759 id = SND_AUDIOCODEC_EAC3;
1760 break;
1761 case AUDIO_FORMAT_DTS:
1762 case AUDIO_FORMAT_DTS_HD:
1763 id = SND_AUDIOCODEC_DTS;
1764 break;
1765 case AUDIO_FORMAT_DOLBY_TRUEHD:
1766 id = SND_AUDIOCODEC_TRUEHD;
1767 break;
1768 case AUDIO_FORMAT_IEC61937:
1769 id = SND_AUDIOCODEC_IEC61937;
1770 break;
1771 default:
1772 ALOGE("%s: Unsupported audio format :%x", __func__, format);
1773 }
1774
1775 return id;
1776}
1777#else
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301778int get_snd_codec_id(audio_format_t format)
1779{
1780 int id = 0;
1781
1782 switch (format & AUDIO_FORMAT_MAIN_MASK) {
1783 case AUDIO_FORMAT_MP3:
1784 id = SND_AUDIOCODEC_MP3;
1785 break;
1786 case AUDIO_FORMAT_AAC:
1787 id = SND_AUDIOCODEC_AAC;
1788 break;
1789 case AUDIO_FORMAT_AAC_ADTS:
1790 id = SND_AUDIOCODEC_AAC;
1791 break;
Arun Kumar Dasari3b174182016-12-27 13:01:14 +05301792 case AUDIO_FORMAT_AAC_LATM:
1793 id = SND_AUDIOCODEC_AAC;
1794 break;
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301795 case AUDIO_FORMAT_PCM:
1796 id = SND_AUDIOCODEC_PCM;
1797 break;
1798 case AUDIO_FORMAT_FLAC:
1799 id = SND_AUDIOCODEC_FLAC;
1800 break;
1801 case AUDIO_FORMAT_ALAC:
1802 id = SND_AUDIOCODEC_ALAC;
1803 break;
1804 case AUDIO_FORMAT_APE:
1805 id = SND_AUDIOCODEC_APE;
1806 break;
1807 case AUDIO_FORMAT_VORBIS:
1808 id = SND_AUDIOCODEC_VORBIS;
1809 break;
1810 case AUDIO_FORMAT_WMA:
1811 id = SND_AUDIOCODEC_WMA;
1812 break;
1813 case AUDIO_FORMAT_WMA_PRO:
1814 id = SND_AUDIOCODEC_WMA_PRO;
1815 break;
Satish Babu Patakokila0c313922016-12-08 12:07:08 +05301816 case AUDIO_FORMAT_MP2:
1817 id = SND_AUDIOCODEC_MP2;
1818 break;
Satish Babu Patakokila915ecba2017-01-10 17:43:56 +05301819 case AUDIO_FORMAT_AC3:
1820 id = SND_AUDIOCODEC_AC3;
1821 break;
1822 case AUDIO_FORMAT_E_AC3:
1823 case AUDIO_FORMAT_E_AC3_JOC:
1824 id = SND_AUDIOCODEC_EAC3;
1825 break;
1826 case AUDIO_FORMAT_DTS:
1827 case AUDIO_FORMAT_DTS_HD:
1828 id = SND_AUDIOCODEC_DTS;
1829 break;
Ben Romberger1aaaf862017-04-06 17:49:46 -07001830 case AUDIO_FORMAT_DOLBY_TRUEHD:
1831 id = SND_AUDIOCODEC_TRUEHD;
1832 break;
Naresh Tanniru928f0862017-04-07 16:44:23 -07001833 case AUDIO_FORMAT_IEC61937:
1834 id = SND_AUDIOCODEC_IEC61937;
1835 break;
Preetam Singh Ranawat4277a5a2017-01-18 19:02:24 +05301836 case AUDIO_FORMAT_DSD:
1837 id = SND_AUDIOCODEC_DSD;
1838 break;
Dhanalakshmi Siddani18737932016-11-29 17:33:17 +05301839 case AUDIO_FORMAT_APTX:
1840 id = SND_AUDIOCODEC_APTX;
1841 break;
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301842 default:
1843 ALOGE("%s: Unsupported audio format :%x", __func__, format);
1844 }
1845
1846 return id;
1847}
Xiaojun Sang782e5b12020-06-29 21:13:06 +08001848#endif
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301849
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001850void audio_extn_utils_send_audio_calibration(struct audio_device *adev,
1851 struct audio_usecase *usecase)
1852{
1853 int type = usecase->type;
1854
Dhananjay Kumar14245982017-01-16 20:21:00 +05301855 if (type == PCM_PLAYBACK && usecase->stream.out != NULL) {
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05301856 platform_send_audio_calibration(adev->platform, usecase,
Zhou Song06761dd2019-04-28 18:08:17 +08001857 usecase->stream.out->app_type_cfg.app_type);
Dhananjay Kumar14245982017-01-16 20:21:00 +05301858 } else if (type == PCM_CAPTURE && usecase->stream.in != NULL) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301859 platform_send_audio_calibration(adev->platform, usecase,
Zhou Song06761dd2019-04-28 18:08:17 +08001860 usecase->stream.in->app_type_cfg.app_type);
1861 } else if ((type == PCM_HFP_CALL) || (type == PCM_CAPTURE) ||
1862 (type == TRANSCODE_LOOPBACK_RX && usecase->stream.inout != NULL)) {
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05301863 platform_send_audio_calibration(adev->platform, usecase,
Zhou Song06761dd2019-04-28 18:08:17 +08001864 platform_get_default_app_type_v2(adev->platform, usecase->type));
Vidyakumar Athotae57f6002017-03-01 13:13:16 -08001865 } else {
1866 /* No need to send audio calibration for voice and voip call usecases */
1867 if ((type != VOICE_CALL) && (type != VOIP_CALL))
1868 ALOGW("%s: No audio calibration for usecase type = %d", __func__, type);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001869 }
1870}
1871
Ben Rombergera04fabc2014-11-14 12:16:03 -08001872// Base64 Encode and Decode
1873// Not all features supported. This must be used only with following conditions.
1874// Decode Modes: Support with and without padding
1875// CRLF not handling. So no CRLF in string to decode.
1876// Encode Modes: Supports only padding
1877int b64decode(char *inp, int ilen, uint8_t* outp)
1878{
1879 int i, j, k, ii, num;
1880 int rem, pcnt;
1881 uint32_t res=0;
1882 uint8_t getIndex[MAX_BASEINDEX_LEN];
1883 uint8_t tmp, cflag;
1884
1885 if(inp == NULL || outp == NULL || ilen <= 0) {
1886 ALOGE("[%s] received NULL pointer or zero length",__func__);
1887 return -1;
1888 }
1889
1890 memset(getIndex, MAX_BASEINDEX_LEN-1, sizeof(getIndex));
1891 for(i=0;i<BASE_TABLE_SIZE;i++) {
1892 getIndex[(uint8_t)bTable[i]] = (uint8_t)i;
1893 }
1894 getIndex[(uint8_t)'=']=0;
1895
1896 j=0;k=0;
1897 num = ilen/4;
1898 rem = ilen%4;
1899 if(rem==0)
1900 num = num-1;
1901 cflag=0;
1902 for(i=0; i<num; i++) {
1903 res=0;
1904 for(ii=0;ii<4;ii++) {
1905 res = res << 6;
1906 tmp = getIndex[(uint8_t)inp[j++]];
1907 res = res | tmp;
1908 cflag = cflag | tmp;
1909 }
1910 outp[k++] = (res >> 16)&0xFF;
1911 outp[k++] = (res >> 8)&0xFF;
1912 outp[k++] = res & 0xFF;
1913 }
1914
1915 // Handle last bytes special
1916 pcnt=0;
1917 if(rem == 0) {
1918 //With padding or full data
1919 res = 0;
1920 for(ii=0;ii<4;ii++) {
1921 if(inp[j] == '=')
1922 pcnt++;
1923 res = res << 6;
1924 tmp = getIndex[(uint8_t)inp[j++]];
1925 res = res | tmp;
1926 cflag = cflag | tmp;
1927 }
1928 outp[k++] = res >> 16;
1929 if(pcnt == 2)
1930 goto done;
1931 outp[k++] = (res>>8)&0xFF;
1932 if(pcnt == 1)
1933 goto done;
1934 outp[k++] = res&0xFF;
1935 } else {
1936 //without padding
1937 res = 0;
1938 for(i=0;i<rem;i++) {
1939 res = res << 6;
1940 tmp = getIndex[(uint8_t)inp[j++]];
1941 res = res | tmp;
1942 cflag = cflag | tmp;
1943 }
1944 for(i=rem;i<4;i++) {
1945 res = res << 6;
1946 pcnt++;
1947 }
1948 outp[k++] = res >> 16;
1949 if(pcnt == 2)
1950 goto done;
1951 outp[k++] = (res>>8)&0xFF;
1952 if(pcnt == 1)
1953 goto done;
1954 outp[k++] = res&0xFF;
1955 }
1956done:
1957 if(cflag == 0xFF) {
1958 ALOGE("[%s] base64 decode failed. Invalid character found %s",
1959 __func__, inp);
1960 return 0;
1961 }
1962 return k;
1963}
1964
1965int b64encode(uint8_t *inp, int ilen, char* outp)
1966{
1967 int i,j,k, num;
1968 int rem=0;
1969 uint32_t res=0;
1970
1971 if(inp == NULL || outp == NULL || ilen<=0) {
1972 ALOGE("[%s] received NULL pointer or zero input length",__func__);
1973 return -1;
1974 }
1975
1976 num = ilen/3;
1977 rem = ilen%3;
1978 j=0;k=0;
1979 for(i=0; i<num; i++) {
1980 //prepare index
1981 res = inp[j++]<<16;
1982 res = res | inp[j++]<<8;
1983 res = res | inp[j++];
1984 //get output map from index
1985 outp[k++] = (char) bTable[(res>>18)&0x3F];
1986 outp[k++] = (char) bTable[(res>>12)&0x3F];
1987 outp[k++] = (char) bTable[(res>>6)&0x3F];
1988 outp[k++] = (char) bTable[res&0x3F];
1989 }
1990
1991 switch(rem) {
1992 case 1:
1993 res = inp[j++]<<16;
1994 outp[k++] = (char) bTable[res>>18];
1995 outp[k++] = (char) bTable[(res>>12)&0x3F];
1996 //outp[k++] = '=';
1997 //outp[k++] = '=';
1998 break;
1999 case 2:
2000 res = inp[j++]<<16;
2001 res = res | inp[j++]<<8;
2002 outp[k++] = (char) bTable[res>>18];
2003 outp[k++] = (char) bTable[(res>>12)&0x3F];
2004 outp[k++] = (char) bTable[(res>>6)&0x3F];
2005 //outp[k++] = '=';
2006 break;
2007 default:
2008 break;
2009 }
Ben Rombergera04fabc2014-11-14 12:16:03 -08002010 outp[k] = '\0';
2011 return k;
2012}
Ashish Jain81eb2a82015-05-13 10:52:34 +05302013
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05302014
2015int audio_extn_utils_get_codec_version(const char *snd_card_name,
2016 int card_num,
2017 char *codec_version)
2018{
2019 char procfs_path[50];
2020 FILE *fp;
2021
2022 if (strstr(snd_card_name, "tasha")) {
2023 snprintf(procfs_path, sizeof(procfs_path),
2024 "/proc/asound/card%d/codecs/tasha/version", card_num);
2025 if ((fp = fopen(procfs_path, "r")) != NULL) {
2026 fgets(codec_version, CODEC_VERSION_MAX_LENGTH, fp);
2027 fclose(fp);
2028 } else {
2029 ALOGE("%s: ERROR. cannot open %s", __func__, procfs_path);
2030 return -ENOENT;
2031 }
2032 ALOGD("%s: codec version %s", __func__, codec_version);
2033 }
2034
2035 return 0;
2036}
2037
Preetam Singh Ranawat9d0d8e42019-07-15 12:27:25 +05302038int audio_extn_utils_get_codec_variant(int card_num,
2039 char *codec_variant)
2040{
2041 char procfs_path[50];
2042 FILE *fp;
2043 snprintf(procfs_path, sizeof(procfs_path),
2044 "/proc/asound/card%d/codecs/wcd938x/variant", card_num);
2045 if ((fp = fopen(procfs_path, "r")) == NULL) {
2046 snprintf(procfs_path, sizeof(procfs_path),
2047 "/proc/asound/card%d/codecs/wcd937x/variant", card_num);
2048 if ((fp = fopen(procfs_path, "r")) == NULL) {
2049 ALOGE("%s: ERROR. cannot open %s", __func__, procfs_path);
2050 return -ENOENT;
2051 }
2052 }
2053 fgets(codec_variant, CODEC_VARIANT_MAX_LENGTH, fp);
2054 fclose(fp);
2055 ALOGD("%s: codec variant is %s", __func__, codec_variant);
2056 return 0;
2057}
2058
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05302059
Ashish Jain81eb2a82015-05-13 10:52:34 +05302060#ifdef AUDIO_EXTERNAL_HDMI_ENABLED
2061
2062void get_default_compressed_channel_status(
2063 unsigned char *channel_status)
2064{
Ashish Jain81eb2a82015-05-13 10:52:34 +05302065 memset(channel_status,0,24);
2066
2067 /* block start bit in preamble bit 3 */
2068 channel_status[0] |= PROFESSIONAL;
2069 //compre out
2070 channel_status[0] |= NON_LPCM;
2071 // sample rate; fixed 48K for default/transcode
2072 channel_status[3] |= SR_48000;
2073}
2074
Ashish Jain81eb2a82015-05-13 10:52:34 +05302075int32_t get_compressed_channel_status(void *audio_stream_data,
2076 uint32_t audio_frame_size,
2077 unsigned char *channel_status,
2078 enum audio_parser_code_type codec_type)
2079 // codec_type - AUDIO_PARSER_CODEC_AC3
2080 // - AUDIO_PARSER_CODEC_DTS
2081{
2082 unsigned char *stream;
2083 int ret = 0;
2084 stream = (unsigned char *)audio_stream_data;
2085
2086 if (audio_stream_data == NULL || audio_frame_size == 0) {
2087 ALOGW("no buffer to get channel status, return default for compress");
2088 get_default_compressed_channel_status(channel_status);
2089 return ret;
2090 }
2091
2092 memset(channel_status,0,24);
2093 if(init_audio_parser(stream, audio_frame_size, codec_type) == -1)
2094 {
2095 ALOGE("init audio parser failed");
2096 return -1;
2097 }
2098 ret = get_channel_status(channel_status, codec_type);
2099 return ret;
2100
2101}
2102
Ashish Jain81eb2a82015-05-13 10:52:34 +05302103void get_lpcm_channel_status(uint32_t sampleRate,
2104 unsigned char *channel_status)
2105{
2106 int32_t status = 0;
Ashish Jain81eb2a82015-05-13 10:52:34 +05302107 memset(channel_status,0,24);
2108 /* block start bit in preamble bit 3 */
2109 channel_status[0] |= PROFESSIONAL;
2110 //LPCM OUT
2111 channel_status[0] &= ~NON_LPCM;
2112
2113 switch (sampleRate) {
2114 case 8000:
2115 case 11025:
2116 case 12000:
2117 case 16000:
2118 case 22050:
2119 channel_status[3] |= SR_NOTID;
Preetam Singh Ranawat61716b12015-12-14 11:55:24 +05302120 break;
Ashish Jain81eb2a82015-05-13 10:52:34 +05302121 case 24000:
2122 channel_status[3] |= SR_24000;
2123 break;
2124 case 32000:
2125 channel_status[3] |= SR_32000;
2126 break;
2127 case 44100:
2128 channel_status[3] |= SR_44100;
2129 break;
2130 case 48000:
2131 channel_status[3] |= SR_48000;
2132 break;
2133 case 88200:
2134 channel_status[3] |= SR_88200;
2135 break;
2136 case 96000:
2137 channel_status[3] |= SR_96000;
2138 break;
2139 case 176400:
2140 channel_status[3] |= SR_176400;
2141 break;
2142 case 192000:
2143 channel_status[3] |= SR_192000;
2144 break;
2145 default:
2146 ALOGV("Invalid sample_rate %u\n", sampleRate);
2147 status = -1;
2148 break;
2149 }
2150}
2151
2152void audio_utils_set_hdmi_channel_status(struct stream_out *out, char * buffer, size_t bytes)
2153{
2154 unsigned char channel_status[24]={0};
2155 struct snd_aes_iec958 iec958;
2156 const char *mixer_ctl_name = "IEC958 Playback PCM Stream";
2157 struct mixer_ctl *ctl;
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +05302158 ALOGV("%s: buffer %s bytes %zd", __func__, buffer, bytes);
Arun Mirpurie008ed22019-03-21 11:21:04 -07002159
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05302160 if (audio_extn_utils_is_dolby_format(out->format) &&
Ashish Jain81eb2a82015-05-13 10:52:34 +05302161 /*TODO:Extend code to support DTS passthrough*/
2162 /*set compressed channel status bits*/
Arun Mirpurie008ed22019-03-21 11:21:04 -07002163 audio_extn_passthru_is_passthrough_stream(out) &&
2164 audio_extn_is_hdmi_passthru_enabled()) {
Ashish Jain81eb2a82015-05-13 10:52:34 +05302165 get_compressed_channel_status(buffer, bytes, channel_status, AUDIO_PARSER_CODEC_AC3);
Arun Mirpurie008ed22019-03-21 11:21:04 -07002166 } else {
Ashish Jain81eb2a82015-05-13 10:52:34 +05302167 /*set channel status bit for LPCM*/
2168 get_lpcm_channel_status(out->sample_rate, channel_status);
2169 }
2170
2171 memcpy(iec958.status, channel_status,sizeof(iec958.status));
2172 ctl = mixer_get_ctl_by_name(out->dev->mixer, mixer_ctl_name);
2173 if (!ctl) {
2174 ALOGE("%s: Could not get ctl for mixer cmd - %s",
2175 __func__, mixer_ctl_name);
2176 return;
2177 }
2178 if (mixer_ctl_set_array(ctl, &iec958, sizeof(iec958)) < 0) {
2179 ALOGE("%s: Could not set channel status for ext HDMI ",
2180 __func__);
2181 return;
2182 }
2183
2184}
2185#endif
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302186
2187int audio_extn_utils_get_avt_device_drift(
2188 struct audio_usecase *usecase,
2189 struct audio_avt_device_drift_param *drift_param)
2190{
2191 int ret = 0, count = 0;
2192 char avt_device_drift_mixer_ctl_name[MIXER_PATH_MAX_LENGTH] = {0};
Naresh Tanniru6160c712017-04-17 15:43:48 +05302193 const char *backend = NULL;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302194 struct mixer_ctl *ctl = NULL;
2195 struct audio_avt_device_drift_stats drift_stats;
2196 struct audio_device *adev = NULL;
2197
2198 if (usecase != NULL && usecase->type == PCM_PLAYBACK) {
Naresh Tanniru6160c712017-04-17 15:43:48 +05302199 backend = platform_get_snd_device_backend_interface(usecase->out_snd_device);
2200 if (!backend) {
2201 ALOGE("%s: Unsupported device %d", __func__,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08002202 get_device_types(&usecase->stream.out->device_list));
Naresh Tanniru6160c712017-04-17 15:43:48 +05302203 ret = -EINVAL;
2204 goto done;
2205 }
2206 strlcpy(avt_device_drift_mixer_ctl_name,
2207 backend,
2208 MIXER_PATH_MAX_LENGTH);
2209
2210 count = strlen(backend);
2211 if (MIXER_PATH_MAX_LENGTH - count > 0) {
2212 strlcat(&avt_device_drift_mixer_ctl_name[count],
2213 " DRIFT",
2214 MIXER_PATH_MAX_LENGTH - count);
2215 } else {
2216 ret = -EINVAL;
2217 goto done;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302218 }
2219 } else {
Naresh Tanniru7586e292017-04-13 10:38:13 +05302220 ALOGE("%s: Invalid usecase",__func__);
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302221 ret = -EINVAL;
Naresh Tanniru6160c712017-04-17 15:43:48 +05302222 goto done;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302223 }
2224
Naresh Tanniru6160c712017-04-17 15:43:48 +05302225 adev = usecase->stream.out->dev;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302226 ctl = mixer_get_ctl_by_name(adev->mixer, avt_device_drift_mixer_ctl_name);
2227 if (!ctl) {
2228 ALOGE("%s: Could not get ctl for mixer cmd - %s",
2229 __func__, avt_device_drift_mixer_ctl_name);
2230
2231 ret = -EINVAL;
2232 goto done;
2233 }
2234
2235 ALOGV("%s: Getting AV Timer vs Device Drift mixer ctrl name %s", __func__,
2236 avt_device_drift_mixer_ctl_name);
2237
2238 mixer_ctl_update(ctl);
2239 count = mixer_ctl_get_num_values(ctl);
2240 if (count != sizeof(struct audio_avt_device_drift_stats)) {
2241 ALOGE("%s: mixer_ctl_get_num_values() invalid drift_stats data size",
2242 __func__);
2243
2244 ret = -EINVAL;
2245 goto done;
2246 }
2247
2248 ret = mixer_ctl_get_array(ctl, (void *)&drift_stats, count);
2249 if (ret != 0) {
2250 ALOGE("%s: mixer_ctl_get_array() failed to get drift_stats Params",
2251 __func__);
2252
2253 ret = -EINVAL;
2254 goto done;
2255 }
2256 memcpy(drift_param, &drift_stats.drift_param,
2257 sizeof(struct audio_avt_device_drift_param));
2258done:
2259 return ret;
2260}
Manish Dewangan07de2142017-02-27 19:27:20 +05302261
2262#ifdef SNDRV_COMPRESS_PATH_DELAY
2263int audio_extn_utils_compress_get_dsp_latency(struct stream_out *out)
2264{
2265 int ret = -EINVAL;
2266 struct snd_compr_metadata metadata;
2267 int delay_ms = COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
2268
Weiyin Jiangc49a3b52018-08-17 16:16:08 +08002269 /* override the latency for pcm offload use case */
2270 if ((out->flags & AUDIO_OUTPUT_FLAG_DIRECT) &&
2271 !(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)) {
2272 delay_ms = PCM_OFFLOAD_PLAYBACK_LATENCY;
2273 }
2274
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -07002275 if (property_get_bool("vendor.audio.playback.dsp.pathdelay", false)) {
Manish Dewangan07de2142017-02-27 19:27:20 +05302276 ALOGD("%s:: Quering DSP delay %d",__func__, __LINE__);
2277 if (!(is_offload_usecase(out->usecase))) {
2278 ALOGE("%s:: not supported for non offload session", __func__);
2279 goto exit;
2280 }
2281
2282 if (!out->compr) {
2283 ALOGD("%s:: Invalid compress handle,returning default dsp latency",
2284 __func__);
2285 goto exit;
2286 }
2287
2288 metadata.key = SNDRV_COMPRESS_PATH_DELAY;
2289 ret = compress_get_metadata(out->compr, &metadata);
2290 if(ret) {
2291 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2292 goto exit;
2293 }
2294 delay_ms = metadata.value[0] / 1000; /*convert to ms*/
2295 } else {
2296 ALOGD("%s:: Using Fix DSP delay",__func__);
2297 }
2298
2299exit:
2300 ALOGD("%s:: delay in ms is %d",__func__, delay_ms);
2301 return delay_ms;
2302}
2303#else
2304int audio_extn_utils_compress_get_dsp_latency(struct stream_out *out __unused)
2305{
2306 return COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
2307}
2308#endif
Manish Dewangan69426c82017-01-30 17:35:36 +05302309
2310#ifdef SNDRV_COMPRESS_RENDER_MODE
2311int audio_extn_utils_compress_set_render_mode(struct stream_out *out)
2312{
2313 struct snd_compr_metadata metadata;
2314 int ret = -EINVAL;
2315
2316 if (!(is_offload_usecase(out->usecase))) {
2317 ALOGE("%s:: not supported for non offload session", __func__);
2318 goto exit;
2319 }
2320
2321 if (!out->compr) {
2322 ALOGD("%s:: Invalid compress handle",
2323 __func__);
2324 goto exit;
2325 }
2326
2327 ALOGD("%s:: render mode %d", __func__, out->render_mode);
2328
2329 metadata.key = SNDRV_COMPRESS_RENDER_MODE;
2330 if (out->render_mode == RENDER_MODE_AUDIO_MASTER) {
2331 metadata.value[0] = SNDRV_COMPRESS_RENDER_MODE_AUDIO_MASTER;
2332 } else if (out->render_mode == RENDER_MODE_AUDIO_STC_MASTER) {
2333 metadata.value[0] = SNDRV_COMPRESS_RENDER_MODE_STC_MASTER;
2334 } else {
2335 ret = 0;
2336 goto exit;
2337 }
2338 ret = compress_set_metadata(out->compr, &metadata);
2339 if(ret) {
2340 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2341 }
2342exit:
2343 return ret;
2344}
2345#else
2346int audio_extn_utils_compress_set_render_mode(struct stream_out *out __unused)
2347{
2348 ALOGD("%s:: configuring render mode not supported", __func__);
2349 return 0;
2350}
2351#endif
Manish Dewangan58229382017-02-02 15:48:41 +05302352
2353#ifdef SNDRV_COMPRESS_CLK_REC_MODE
2354int audio_extn_utils_compress_set_clk_rec_mode(
2355 struct audio_usecase *usecase)
2356{
2357 struct snd_compr_metadata metadata;
2358 struct stream_out *out = NULL;
2359 int ret = -EINVAL;
2360
Naresh Tanniru7586e292017-04-13 10:38:13 +05302361 if (usecase == NULL || usecase->type != PCM_PLAYBACK) {
Manish Dewangan58229382017-02-02 15:48:41 +05302362 ALOGE("%s:: Invalid use case", __func__);
2363 goto exit;
2364 }
2365
2366 out = usecase->stream.out;
2367 if (!out) {
2368 ALOGE("%s:: invalid stream", __func__);
2369 goto exit;
2370 }
2371
2372 if (!is_offload_usecase(out->usecase)) {
2373 ALOGE("%s:: not supported for non offload session", __func__);
2374 goto exit;
2375 }
2376
2377 if (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER) {
2378 ALOGD("%s:: clk recovery is only supported in STC render mode",
2379 __func__);
2380 ret = 0;
2381 goto exit;
2382 }
2383
2384 if (!out->compr) {
2385 ALOGD("%s:: Invalid compress handle",
2386 __func__);
2387 goto exit;
2388 }
2389 metadata.key = SNDRV_COMPRESS_CLK_REC_MODE;
2390 switch(usecase->out_snd_device) {
2391 case SND_DEVICE_OUT_HDMI:
2392 case SND_DEVICE_OUT_SPEAKER_AND_HDMI:
2393 case SND_DEVICE_OUT_DISPLAY_PORT:
2394 case SND_DEVICE_OUT_SPEAKER_AND_DISPLAY_PORT:
2395 metadata.value[0] = SNDRV_COMPRESS_CLK_REC_MODE_NONE;
2396 break;
2397 default:
2398 metadata.value[0] = SNDRV_COMPRESS_CLK_REC_MODE_AUTO;
2399 break;
2400 }
2401
2402 ALOGD("%s:: clk recovery mode %d",__func__, metadata.value[0]);
2403
2404 ret = compress_set_metadata(out->compr, &metadata);
2405 if(ret) {
2406 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2407 }
2408
2409exit:
2410 return ret;
2411}
2412#else
2413int audio_extn_utils_compress_set_clk_rec_mode(
2414 struct audio_usecase *usecase __unused)
2415{
2416 ALOGD("%s:: configuring render mode not supported", __func__);
2417 return 0;
2418}
2419#endif
Manish Dewangan27346042017-03-01 12:56:12 +05302420
2421#ifdef SNDRV_COMPRESS_RENDER_WINDOW
2422int audio_extn_utils_compress_set_render_window(
2423 struct stream_out *out,
2424 struct audio_out_render_window_param *render_window)
2425{
2426 struct snd_compr_metadata metadata;
2427 int ret = -EINVAL;
2428
Manish Dewangan27346042017-03-01 12:56:12 +05302429 if(render_window == NULL) {
2430 ALOGE("%s:: Invalid render_window", __func__);
2431 goto exit;
2432 }
2433
Aniket Kumar Lata1e1d3662017-06-01 18:45:48 -07002434 ALOGD("%s:: render window start 0x%"PRIx64" end 0x%"PRIx64"",
2435 __func__,render_window->render_ws, render_window->render_we);
2436
Manish Dewangan27346042017-03-01 12:56:12 +05302437 if (!is_offload_usecase(out->usecase)) {
2438 ALOGE("%s:: not supported for non offload session", __func__);
2439 goto exit;
2440 }
2441
Naresh Tanniru6160c712017-04-17 15:43:48 +05302442 if ((out->render_mode != RENDER_MODE_AUDIO_MASTER) &&
2443 (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER)) {
Manish Dewangan27346042017-03-01 12:56:12 +05302444 ALOGD("%s:: only supported in timestamp mode, current "
2445 "render mode mode %d", __func__, out->render_mode);
2446 goto exit;
2447 }
2448
2449 if (!out->compr) {
2450 ALOGW("%s:: offload session not yet opened,"
2451 "render window will be configure later", __func__);
2452 /* store render window to reconfigure in start_output_stream() */
2453 goto exit;
2454 }
2455
2456 metadata.key = SNDRV_COMPRESS_RENDER_WINDOW;
2457 /*render window start value */
2458 metadata.value[0] = 0xFFFFFFFF & render_window->render_ws; /* lsb */
2459 metadata.value[1] = \
2460 (0xFFFFFFFF00000000 & render_window->render_ws) >> 32; /* msb*/
2461 /*render window end value */
2462 metadata.value[2] = 0xFFFFFFFF & render_window->render_we; /* lsb */
2463 metadata.value[3] = \
2464 (0xFFFFFFFF00000000 & render_window->render_we) >> 32; /* msb*/
2465
2466 ret = compress_set_metadata(out->compr, &metadata);
2467 if(ret) {
2468 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2469 }
2470
2471exit:
2472 return ret;
2473}
2474#else
2475int audio_extn_utils_compress_set_render_window(
2476 struct stream_out *out __unused,
2477 struct audio_out_render_window_param *render_window __unused)
2478{
2479 ALOGD("%s:: configuring render window not supported", __func__);
2480 return 0;
2481}
2482#endif
Manish Dewangan14956cc2017-02-14 18:54:42 +05302483
2484#ifdef SNDRV_COMPRESS_START_DELAY
2485int audio_extn_utils_compress_set_start_delay(
2486 struct stream_out *out,
2487 struct audio_out_start_delay_param *delay_param)
2488{
2489 struct snd_compr_metadata metadata;
2490 int ret = -EINVAL;
2491
2492 if(delay_param == NULL) {
2493 ALOGE("%s:: Invalid delay_param", __func__);
2494 goto exit;
2495 }
2496
2497 ALOGD("%s:: render start delay 0x%"PRIx64" ", __func__,
2498 delay_param->start_delay);
2499
2500 if (!is_offload_usecase(out->usecase)) {
2501 ALOGE("%s:: not supported for non offload session", __func__);
2502 goto exit;
2503 }
2504
Naresh Tanniru6160c712017-04-17 15:43:48 +05302505 if ((out->render_mode != RENDER_MODE_AUDIO_MASTER) &&
2506 (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER)) {
Manish Dewangan14956cc2017-02-14 18:54:42 +05302507 ALOGD("%s:: only supported in timestamp mode, current "
2508 "render mode mode %d", __func__, out->render_mode);
2509 goto exit;
2510 }
2511
2512 if (!out->compr) {
2513 ALOGW("%s:: offload session not yet opened,"
2514 "start delay will be configure later", __func__);
2515 goto exit;
2516 }
2517
2518 metadata.key = SNDRV_COMPRESS_START_DELAY;
2519 metadata.value[0] = 0xFFFFFFFF & delay_param->start_delay; /* lsb */
2520 metadata.value[1] = \
2521 (0xFFFFFFFF00000000 & delay_param->start_delay) >> 32; /* msb*/
2522
2523 ret = compress_set_metadata(out->compr, &metadata);
2524 if(ret) {
2525 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2526 }
2527
2528exit:
2529 return ret;
2530}
2531#else
2532int audio_extn_utils_compress_set_start_delay(
2533 struct stream_out *out __unused,
2534 struct audio_out_start_delay_param *delay_param __unused)
2535{
2536 ALOGD("%s:: configuring render window not supported", __func__);
2537 return 0;
2538}
2539#endif
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002540
Surendar Karka287348c2019-04-10 18:31:46 +05302541#ifdef SNDRV_COMPRESS_DSP_POSITION
2542int audio_extn_utils_compress_get_dsp_presentation_pos(struct stream_out *out,
2543 uint64_t *frames, struct timespec *timestamp, int32_t clock_id)
2544{
2545 int ret = -EINVAL;
2546 uint64_t *val = NULL;
2547 uint64_t time = 0;
2548 struct snd_compr_metadata metadata;
2549
2550 ALOGV("%s:: Quering DSP position with clock id %d",__func__, clock_id);
2551 metadata.key = SNDRV_COMPRESS_DSP_POSITION;
2552 metadata.value[0] = clock_id;
2553 ret = compress_get_metadata(out->compr, &metadata);
2554 if (ret) {
2555 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2556 ret = -errno;
2557 goto exit;
2558 }
2559 val = (uint64_t *)&metadata.value[1];
2560 *frames = *val;
2561 time = *(val + 1);
2562 timestamp->tv_sec = time / 1000000;
2563 timestamp->tv_nsec = (time % 1000000)*1000;
2564
2565exit:
2566 return ret;
2567}
2568#else
2569int audio_extn_utils_compress_get_dsp_presentation_pos(struct stream_out *out __unused,
2570 uint64_t *frames __unused, struct timespec *timestamp __unused,
2571 int32_t clock_id __unused)
2572{
2573 ALOGD("%s:: dsp presentation position not supported", __func__);
2574 return 0;
2575
2576}
2577#endif
2578
2579#ifdef SNDRV_PCM_IOCTL_DSP_POSITION
2580int audio_extn_utils_pcm_get_dsp_presentation_pos(struct stream_out *out,
2581 uint64_t *frames, struct timespec *timestamp, int32_t clock_id)
2582{
2583 int ret = -EINVAL;
2584 uint64_t time = 0;
2585 struct snd_pcm_prsnt_position prsnt_position;
2586
2587 ALOGV("%s:: Quering DSP position with clock id %d",__func__, clock_id);
2588 prsnt_position.clock_id = clock_id;
2589 ret = pcm_ioctl(out->pcm, SNDRV_PCM_IOCTL_DSP_POSITION, &prsnt_position);
2590 if (ret) {
2591 ALOGE("%s::error %d", __func__, ret);
2592 ret = -EIO;
2593 goto exit;
2594 }
2595
2596 *frames = prsnt_position.frames;
2597 time = prsnt_position.timestamp;
2598 timestamp->tv_sec = time / 1000000;
2599 timestamp->tv_nsec = (time % 1000000)*1000;
2600
2601exit:
2602 return ret;
2603}
2604#else
2605int audio_extn_utils_pcm_get_dsp_presentation_pos(struct stream_out *out __unused,
2606 uint64_t *frames __unused, struct timespec *timestamp __unused,
2607 int32_t clock_id __unused)
2608{
2609 ALOGD("%s:: dsp presentation position not supported", __func__);
2610 return 0;
2611
2612}
2613#endif
2614
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002615#define MAX_SND_CARD 8
Ashish Jain30ae8942017-11-05 17:21:23 +05302616#define RETRY_US 1000000
2617#define RETRY_NUMBER 40
Aalique Grahame22e49102018-12-18 14:23:57 -08002618#define PLATFORM_INFO_XML_PATH "audio_platform_info.xml"
2619#define PLATFORM_INFO_XML_BASE_STRING "audio_platform_info"
2620
2621#ifdef LINUX_ENABLED
2622static const char *kConfigLocationList[] =
2623 {"/etc"};
2624#else
2625static const char *kConfigLocationList[] =
2626 {"/vendor/etc"};
2627#endif
2628static const int kConfigLocationListSize =
2629 (sizeof(kConfigLocationList) / sizeof(kConfigLocationList[0]));
2630
2631bool audio_extn_utils_resolve_config_file(char file_name[MIXER_PATH_MAX_LENGTH])
2632{
2633 char full_config_path[MIXER_PATH_MAX_LENGTH];
2634 for (int i = 0; i < kConfigLocationListSize; i++) {
2635 snprintf(full_config_path,
2636 MIXER_PATH_MAX_LENGTH,
2637 "%s/%s",
2638 kConfigLocationList[i],
2639 file_name);
2640 if (F_OK == access(full_config_path, 0)) {
Weiyin Jiang2995f662019-04-17 14:25:12 +08002641 strlcpy(file_name, full_config_path, MIXER_PATH_MAX_LENGTH);
Aalique Grahame22e49102018-12-18 14:23:57 -08002642 return true;
2643 }
2644 }
2645 return false;
2646}
2647
2648/* platform_info_file should be size 'MIXER_PATH_MAX_LENGTH' */
2649int audio_extn_utils_get_platform_info(const char* snd_card_name, char* platform_info_file)
2650{
2651 if (NULL == snd_card_name) {
2652 return -1;
2653 }
2654
2655 struct snd_card_split *snd_split_handle = NULL;
2656 int ret = 0;
2657 audio_extn_set_snd_card_split(snd_card_name);
2658 snd_split_handle = audio_extn_get_snd_card_split();
2659
2660 snprintf(platform_info_file, MIXER_PATH_MAX_LENGTH, "%s_%s_%s.xml",
2661 PLATFORM_INFO_XML_BASE_STRING, snd_split_handle->snd_card,
2662 snd_split_handle->form_factor);
2663
2664 if (!audio_extn_utils_resolve_config_file(platform_info_file)) {
2665 memset(platform_info_file, 0, MIXER_PATH_MAX_LENGTH);
2666 snprintf(platform_info_file, MIXER_PATH_MAX_LENGTH, "%s_%s.xml",
2667 PLATFORM_INFO_XML_BASE_STRING, snd_split_handle->snd_card);
2668
2669 if (!audio_extn_utils_resolve_config_file(platform_info_file)) {
2670 memset(platform_info_file, 0, MIXER_PATH_MAX_LENGTH);
2671 strlcpy(platform_info_file, PLATFORM_INFO_XML_PATH, MIXER_PATH_MAX_LENGTH);
2672 ret = audio_extn_utils_resolve_config_file(platform_info_file) ? 0 : -1;
2673 }
2674 }
2675
2676 return ret;
2677}
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002678
2679int audio_extn_utils_get_snd_card_num()
2680{
Soumya Managoli9fee7c62018-04-06 16:21:50 +05302681 int snd_card_num = 0;
2682 struct mixer *mixer = NULL;
2683
2684 snd_card_num = audio_extn_utils_open_snd_mixer(&mixer);
2685 if (mixer)
2686 mixer_close(mixer);
2687 return snd_card_num;
2688}
2689
2690int audio_extn_utils_open_snd_mixer(struct mixer **mixer_handle)
2691{
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002692
2693 void *hw_info = NULL;
2694 struct mixer *mixer = NULL;
2695 int retry_num = 0;
mpangfaa7bae2019-01-15 16:38:13 +08002696 int snd_card_num = 0;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002697 char* snd_card_name = NULL;
mpangfaa7bae2019-01-15 16:38:13 +08002698 int snd_card_detection_info[MAX_SND_CARD] = {0};
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002699
Soumya Managoli9fee7c62018-04-06 16:21:50 +05302700 if (!mixer_handle) {
2701 ALOGE("invalid mixer handle");
2702 return -1;
2703 }
2704 *mixer_handle = NULL;
Ashish Jain30ae8942017-11-05 17:21:23 +05302705 /*
mpangfaa7bae2019-01-15 16:38:13 +08002706 * Try with all the sound cards ( 0 to 7 ) and if none of them were detected
Ashish Jain30ae8942017-11-05 17:21:23 +05302707 * sleep for 1 sec and try detections with sound card 0 again.
2708 * If sound card gets detected, check if it is relevant, if not check with the
2709 * other sound cards. To ensure that the irrelevant sound card is not check again,
2710 * we maintain it in min_snd_card_num.
2711 */
2712 while (retry_num < RETRY_NUMBER) {
mpangfaa7bae2019-01-15 16:38:13 +08002713 snd_card_num = 0;
2714 while (snd_card_num < MAX_SND_CARD) {
2715 if (snd_card_detection_info[snd_card_num] == 0) {
2716 mixer = mixer_open(snd_card_num);
2717 if (!mixer)
2718 snd_card_num++;
2719 else
2720 break;
2721 } else
2722 snd_card_num++;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002723 }
2724
2725 if (!mixer) {
Ashish Jain30ae8942017-11-05 17:21:23 +05302726 usleep(RETRY_US);
Ashish Jain30ae8942017-11-05 17:21:23 +05302727 retry_num++;
mpangfaa7bae2019-01-15 16:38:13 +08002728 ALOGD("%s: retry, retry_num %d", __func__, retry_num);
Ashish Jain30ae8942017-11-05 17:21:23 +05302729 continue;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002730 }
2731
2732 snd_card_name = strdup(mixer_get_name(mixer));
2733 if (!snd_card_name) {
2734 ALOGE("failed to allocate memory for snd_card_name\n");
2735 mixer_close(mixer);
2736 return -1;
2737 }
2738 ALOGD("%s: snd_card_name: %s", __func__, snd_card_name);
mpangfaa7bae2019-01-15 16:38:13 +08002739 snd_card_detection_info[snd_card_num] = 1;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002740 hw_info = hw_info_init(snd_card_name);
2741 if (hw_info) {
2742 ALOGD("%s: Opened sound card:%d", __func__, snd_card_num);
2743 break;
2744 }
mpangfaa7bae2019-01-15 16:38:13 +08002745 ALOGE("%s: Failed to init hardware info, snd_card_num:%d", __func__, snd_card_num);
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002746
Dhananjay Kumara7e27832017-07-11 15:40:39 +05302747 free(snd_card_name);
2748 snd_card_name = NULL;
2749
2750 mixer_close(mixer);
2751 mixer = NULL;
2752 }
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002753 if (snd_card_name)
2754 free(snd_card_name);
Ashish Jain30ae8942017-11-05 17:21:23 +05302755
Dhananjay Kumara7e27832017-07-11 15:40:39 +05302756 if (hw_info)
2757 hw_info_deinit(hw_info);
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002758
mpangfaa7bae2019-01-15 16:38:13 +08002759 if (retry_num >= RETRY_NUMBER) {
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002760 ALOGE("%s: Unable to find correct sound card, aborting.", __func__);
2761 return -1;
2762 }
2763
Soumya Managoli9fee7c62018-04-06 16:21:50 +05302764 if (mixer)
2765 *mixer_handle = mixer;
2766
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002767 return snd_card_num;
2768}
Naresh Tanniru6160c712017-04-17 15:43:48 +05302769
Soumya Managoli9fee7c62018-04-06 16:21:50 +05302770void audio_extn_utils_close_snd_mixer(struct mixer *mixer)
2771{
2772 if (mixer)
2773 mixer_close(mixer);
2774}
2775
Naresh Tanniru6160c712017-04-17 15:43:48 +05302776#ifdef SNDRV_COMPRESS_ENABLE_ADJUST_SESSION_CLOCK
2777int audio_extn_utils_compress_enable_drift_correction(
2778 struct stream_out *out,
2779 struct audio_out_enable_drift_correction *drift)
2780{
2781 struct snd_compr_metadata metadata;
2782 int ret = -EINVAL;
2783
2784 if(drift == NULL) {
2785 ALOGE("%s:: Invalid param", __func__);
2786 goto exit;
2787 }
2788
2789 ALOGD("%s:: drift enable %d", __func__,drift->enable);
2790
2791 if (!is_offload_usecase(out->usecase)) {
2792 ALOGE("%s:: not supported for non offload session", __func__);
2793 goto exit;
2794 }
2795
2796 if (!out->compr) {
2797 ALOGW("%s:: offload session not yet opened,"
2798 "start delay will be configure later", __func__);
2799 goto exit;
2800 }
2801
2802 metadata.key = SNDRV_COMPRESS_ENABLE_ADJUST_SESSION_CLOCK;
2803 metadata.value[0] = drift->enable;
2804 out->drift_correction_enabled = drift->enable;
2805
2806 ret = compress_set_metadata(out->compr, &metadata);
2807 if(ret) {
2808 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2809 out->drift_correction_enabled = false;
2810 }
2811
2812exit:
2813 return ret;
2814}
2815#else
2816int audio_extn_utils_compress_enable_drift_correction(
2817 struct stream_out *out __unused,
2818 struct audio_out_enable_drift_correction *drift __unused)
2819{
2820 ALOGD("%s:: configuring drift enablement not supported", __func__);
2821 return 0;
2822}
2823#endif
2824
2825#ifdef SNDRV_COMPRESS_ADJUST_SESSION_CLOCK
2826int audio_extn_utils_compress_correct_drift(
2827 struct stream_out *out,
2828 struct audio_out_correct_drift *drift_param)
2829{
2830 struct snd_compr_metadata metadata;
2831 int ret = -EINVAL;
2832
2833 if (drift_param == NULL) {
2834 ALOGE("%s:: Invalid drift_param", __func__);
2835 goto exit;
2836 }
2837
2838 ALOGD("%s:: adjust time 0x%"PRIx64" ", __func__,
2839 drift_param->adjust_time);
2840
2841 if (!is_offload_usecase(out->usecase)) {
2842 ALOGE("%s:: not supported for non offload session", __func__);
2843 goto exit;
2844 }
2845
2846 if (!out->compr) {
2847 ALOGW("%s:: offload session not yet opened", __func__);
2848 goto exit;
2849 }
2850
2851 if (!out->drift_correction_enabled) {
2852 ALOGE("%s:: drift correction not enabled", __func__);
2853 goto exit;
2854 }
2855
2856 metadata.key = SNDRV_COMPRESS_ADJUST_SESSION_CLOCK;
2857 metadata.value[0] = 0xFFFFFFFF & drift_param->adjust_time; /* lsb */
2858 metadata.value[1] = \
2859 (0xFFFFFFFF00000000 & drift_param->adjust_time) >> 32; /* msb*/
2860
2861 ret = compress_set_metadata(out->compr, &metadata);
2862 if(ret)
2863 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2864exit:
2865 return ret;
2866}
2867#else
2868int audio_extn_utils_compress_correct_drift(
2869 struct stream_out *out __unused,
2870 struct audio_out_correct_drift *drift_param __unused)
2871{
2872 ALOGD("%s:: setting adjust clock not supported", __func__);
2873 return 0;
2874}
2875#endif
Naresh Tanniru29bce4e2017-04-27 17:54:30 +05302876
2877int audio_extn_utils_set_channel_map(
2878 struct stream_out *out,
2879 struct audio_out_channel_map_param *channel_map_param)
2880{
2881 int ret = -EINVAL, i = 0;
2882 int channels = audio_channel_count_from_out_mask(out->channel_mask);
2883
2884 if (channel_map_param == NULL) {
2885 ALOGE("%s:: Invalid channel_map", __func__);
2886 goto exit;
2887 }
2888
2889 if (channel_map_param->channels != channels) {
2890 ALOGE("%s:: Channels(%d) does not match stream channels(%d)",
2891 __func__, channel_map_param->channels, channels);
2892 goto exit;
2893 }
2894
2895 for ( i = 0; i < channels; i++) {
2896 ALOGV("%s:: channel_map[%d]- %d", __func__, i, channel_map_param->channel_map[i]);
2897 out->channel_map_param.channel_map[i] = channel_map_param->channel_map[i];
2898 }
2899 ret = 0;
2900exit:
2901 return ret;
2902}
Varun Balaraje49253e2017-07-06 19:48:56 +05302903
2904int audio_extn_utils_set_pan_scale_params(
2905 struct stream_out *out,
2906 struct mix_matrix_params *mm_params)
2907{
2908 int ret = -EINVAL, i = 0, j = 0;
2909
Varun Balaraj003ee752017-08-07 17:54:55 +05302910 if (mm_params == NULL || out == NULL) {
2911 ALOGE("%s:: Invalid mix matrix or out param", __func__);
Varun Balaraje49253e2017-07-06 19:48:56 +05302912 goto exit;
2913 }
2914
2915 if (mm_params->num_output_channels > MAX_CHANNELS_SUPPORTED ||
2916 mm_params->num_output_channels <= 0 ||
2917 mm_params->num_input_channels > MAX_CHANNELS_SUPPORTED ||
2918 mm_params->num_input_channels <= 0)
2919 goto exit;
2920
2921 out->pan_scale_params.num_output_channels = mm_params->num_output_channels;
2922 out->pan_scale_params.num_input_channels = mm_params->num_input_channels;
2923 out->pan_scale_params.has_output_channel_map =
2924 mm_params->has_output_channel_map;
2925 for (i = 0; i < mm_params->num_output_channels; i++)
2926 out->pan_scale_params.output_channel_map[i] =
2927 mm_params->output_channel_map[i];
2928
2929 out->pan_scale_params.has_input_channel_map =
2930 mm_params->has_input_channel_map;
2931 for (i = 0; i < mm_params->num_input_channels; i++)
2932 out->pan_scale_params.input_channel_map[i] =
2933 mm_params->input_channel_map[i];
2934
2935 out->pan_scale_params.has_mixer_coeffs = mm_params->has_mixer_coeffs;
2936 for (i = 0; i < mm_params->num_output_channels; i++)
2937 for (j = 0; j < mm_params->num_input_channels; j++) {
2938 //Convert the channel coefficient gains in Q14 format
2939 out->pan_scale_params.mixer_coeffs[i][j] =
2940 mm_params->mixer_coeffs[i][j] * (2 << 13);
2941 }
2942
2943 ret = platform_set_stream_pan_scale_params(out->dev->platform,
2944 out->pcm_device_id,
2945 out->pan_scale_params);
2946
2947exit:
2948 return ret;
2949}
2950
2951int audio_extn_utils_set_downmix_params(
2952 struct stream_out *out,
2953 struct mix_matrix_params *mm_params)
2954{
2955 int ret = -EINVAL, i = 0, j = 0;
2956 struct audio_usecase *usecase = NULL;
2957
Aniket Kumar Lataf9f246e2017-09-15 15:20:16 -07002958 if (mm_params == NULL || out == NULL) {
Varun Balaraj003ee752017-08-07 17:54:55 +05302959 ALOGE("%s:: Invalid mix matrix or out param", __func__);
Varun Balaraje49253e2017-07-06 19:48:56 +05302960 goto exit;
2961 }
2962
2963 if (mm_params->num_output_channels > MAX_CHANNELS_SUPPORTED ||
2964 mm_params->num_output_channels <= 0 ||
2965 mm_params->num_input_channels > MAX_CHANNELS_SUPPORTED ||
2966 mm_params->num_input_channels <= 0)
2967 goto exit;
2968
2969 usecase = get_usecase_from_list(out->dev, out->usecase);
Varun Balaraj003ee752017-08-07 17:54:55 +05302970 if (!usecase) {
2971 ALOGE("%s: Get usecase list failed!", __func__);
Aniket Kumar Lataf9f246e2017-09-15 15:20:16 -07002972 goto exit;
2973 }
Varun Balaraje49253e2017-07-06 19:48:56 +05302974 out->downmix_params.num_output_channels = mm_params->num_output_channels;
2975 out->downmix_params.num_input_channels = mm_params->num_input_channels;
2976
2977 out->downmix_params.has_output_channel_map =
2978 mm_params->has_output_channel_map;
2979 for (i = 0; i < mm_params->num_output_channels; i++) {
2980 out->downmix_params.output_channel_map[i] =
2981 mm_params->output_channel_map[i];
2982 }
2983
2984 out->downmix_params.has_input_channel_map =
2985 mm_params->has_input_channel_map;
2986 for (i = 0; i < mm_params->num_input_channels; i++)
2987 out->downmix_params.input_channel_map[i] =
2988 mm_params->input_channel_map[i];
2989
2990 out->downmix_params.has_mixer_coeffs = mm_params->has_mixer_coeffs;
2991 for (i = 0; i < mm_params->num_output_channels; i++)
Nikhil Latukar2e6f6242017-08-15 13:37:07 +05302992 for (j = 0; j < mm_params->num_input_channels; j++) {
2993 //Convert the channel coefficient gains in Q14 format
Varun Balaraje49253e2017-07-06 19:48:56 +05302994 out->downmix_params.mixer_coeffs[i][j] =
Nikhil Latukar2e6f6242017-08-15 13:37:07 +05302995 mm_params->mixer_coeffs[i][j] * (2 << 13);
2996 }
Varun Balaraje49253e2017-07-06 19:48:56 +05302997
2998 ret = platform_set_stream_downmix_params(out->dev->platform,
2999 out->pcm_device_id,
3000 usecase->out_snd_device,
3001 out->downmix_params);
3002
3003exit:
3004 return ret;
3005}
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05303006
3007bool audio_extn_utils_is_dolby_format(audio_format_t format)
3008{
3009 if (format == AUDIO_FORMAT_AC3 ||
3010 format == AUDIO_FORMAT_E_AC3 ||
3011 format == AUDIO_FORMAT_E_AC3_JOC)
3012 return true;
3013 else
3014 return false;
3015}
3016
`Deeraj Soman676c2702017-09-18 19:25:53 +05303017int audio_extn_utils_get_bit_width_from_string(const char *id_string)
3018{
3019 int i;
3020 const mixer_config_lookup mixer_bitwidth_config[] = {{"S24_3LE", 24},
3021 {"S32_LE", 32},
3022 {"S24_LE", 24},
3023 {"S16_LE", 16}};
3024 int num_configs = sizeof(mixer_bitwidth_config) / sizeof(mixer_bitwidth_config[0]);
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05303025
`Deeraj Soman676c2702017-09-18 19:25:53 +05303026 for (i = 0; i < num_configs; i++) {
3027 if (!strcmp(id_string, mixer_bitwidth_config[i].id_string))
3028 return mixer_bitwidth_config[i].value;
3029 }
3030
3031 return -EINVAL;
3032}
3033
3034int audio_extn_utils_get_sample_rate_from_string(const char *id_string)
3035{
3036 int i;
3037 const mixer_config_lookup mixer_samplerate_config[] = {{"KHZ_32", 32000},
3038 {"KHZ_48", 48000},
3039 {"KHZ_96", 96000},
3040 {"KHZ_144", 144000},
3041 {"KHZ_192", 192000},
3042 {"KHZ_384", 384000},
3043 {"KHZ_44P1", 44100},
3044 {"KHZ_88P2", 88200},
3045 {"KHZ_176P4", 176400},
3046 {"KHZ_352P8", 352800}};
3047 int num_configs = sizeof(mixer_samplerate_config) / sizeof(mixer_samplerate_config[0]);
3048
3049 for (i = 0; i < num_configs; i++) {
3050 if (!strcmp(id_string, mixer_samplerate_config[i].id_string))
3051 return mixer_samplerate_config[i].value;
3052 }
3053
3054 return -EINVAL;
3055}
3056
3057int audio_extn_utils_get_channels_from_string(const char *id_string)
3058{
3059 int i;
3060 const mixer_config_lookup mixer_channels_config[] = {{"One", 1},
3061 {"Two", 2},
3062 {"Three",3},
3063 {"Four", 4},
3064 {"Five", 5},
3065 {"Six", 6},
3066 {"Seven", 7},
3067 {"Eight", 8}};
3068 int num_configs = sizeof(mixer_channels_config) / sizeof(mixer_channels_config[0]);
3069
3070 for (i = 0; i < num_configs; i++) {
3071 if (!strcmp(id_string, mixer_channels_config[i].id_string))
3072 return mixer_channels_config[i].value;
3073 }
3074
3075 return -EINVAL;
3076}
Surendar karka30569792018-05-08 12:02:21 +05303077
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08003078void audio_extn_utils_release_snd_device(snd_device_t snd_device)
3079{
3080 audio_extn_dev_arbi_release(snd_device);
3081 audio_extn_sound_trigger_update_device_status(snd_device,
3082 ST_EVENT_SND_DEVICE_FREE);
3083 audio_extn_listen_update_device_status(snd_device,
3084 LISTEN_EVENT_SND_DEVICE_FREE);
3085}
3086
Weiyin Jiangd5718bd2019-09-04 16:52:08 +08003087int audio_extn_utils_get_license_params(
3088 const struct audio_device *adev,
3089 struct audio_license_params *license_params)
Surendar karka30569792018-05-08 12:02:21 +05303090{
3091 if(!license_params)
3092 return -EINVAL;
Weiyin Jiangd5718bd2019-09-04 16:52:08 +08003093
3094 return platform_get_license_by_product(adev->platform,
3095 (const char*)license_params->product, &license_params->key, license_params->license);
Surendar karka30569792018-05-08 12:02:21 +05303096}
3097
Aalique Grahame22e49102018-12-18 14:23:57 -08003098int audio_extn_utils_send_app_type_gain(struct audio_device *adev,
3099 int app_type,
3100 int *gain)
3101{
3102 int gain_cfg[4];
3103 const char *mixer_ctl_name = "App Type Gain";
3104 struct mixer_ctl *ctl;
3105 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
3106 if (!ctl) {
3107 ALOGE("%s: Could not get volume ctl mixer %s", __func__,
3108 mixer_ctl_name);
3109 return -EINVAL;
3110 }
3111 gain_cfg[0] = 0;
3112 gain_cfg[1] = app_type;
3113 gain_cfg[2] = gain[0];
3114 gain_cfg[3] = gain[1];
3115 ALOGV("%s app_type %d l(%d) r(%d)", __func__, app_type, gain[0], gain[1]);
3116 return mixer_ctl_set_array(ctl, gain_cfg,
3117 sizeof(gain_cfg)/sizeof(gain_cfg[0]));
3118}
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -07003119
Weiyin Jiangd5718bd2019-09-04 16:52:08 +08003120static void vndk_fwk_init()
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -07003121{
Weiyin Jiangd5718bd2019-09-04 16:52:08 +08003122 if (mVndkFwk.lib_handle != NULL)
3123 return;
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -07003124
Weiyin Jiangd5718bd2019-09-04 16:52:08 +08003125 mVndkFwk.lib_handle = dlopen(VNDK_FWK_LIB_PATH, RTLD_NOW);
3126 if (mVndkFwk.lib_handle == NULL) {
3127 ALOGW("%s: failed to dlopen VNDK_FWK_LIB %s", __func__, strerror(errno));
3128 return;
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -07003129 }
3130
Weiyin Jiangd5718bd2019-09-04 16:52:08 +08003131 *(void **)(&mVndkFwk.isVendorEnhancedFwk) =
3132 dlsym(mVndkFwk.lib_handle, "isRunningWithVendorEnhancedFramework");
3133 if (mVndkFwk.isVendorEnhancedFwk == NULL) {
3134 ALOGW("%s: dlsym failed %s", __func__, strerror(errno));
3135 if (mVndkFwk.lib_handle) {
3136 dlclose(mVndkFwk.lib_handle);
3137 mVndkFwk.lib_handle = NULL;
3138 }
3139 return;
3140 }
3141
3142
3143 *(void **)(&mVndkFwk.getVendorEnhancedInfo) =
3144 dlsym(mVndkFwk.lib_handle, "getVendorEnhancedInfo");
3145 if (mVndkFwk.getVendorEnhancedInfo == NULL) {
3146 ALOGW("%s: dlsym failed %s", __func__, strerror(errno));
3147 if (mVndkFwk.lib_handle) {
3148 dlclose(mVndkFwk.lib_handle);
3149 mVndkFwk.lib_handle = NULL;
3150 }
3151 }
3152
3153 return;
3154}
3155
3156bool audio_extn_utils_is_vendor_enhanced_fwk()
3157{
3158 static int is_vendor_enhanced_fwk = -EINVAL;
3159 if (is_vendor_enhanced_fwk != -EINVAL)
3160 return (bool)is_vendor_enhanced_fwk;
3161
3162 vndk_fwk_init();
3163
3164 if (mVndkFwk.isVendorEnhancedFwk != NULL) {
3165 is_vendor_enhanced_fwk = mVndkFwk.isVendorEnhancedFwk();
3166 ALOGW("%s: is_vendor_enhanced_fwk %d", __func__, is_vendor_enhanced_fwk);
3167 } else {
3168 is_vendor_enhanced_fwk = 0;
3169 ALOGW("%s: default to non enhanced_fwk config", __func__);
3170 }
3171
3172 return (bool)is_vendor_enhanced_fwk;
3173}
3174
3175int audio_extn_utils_get_vendor_enhanced_info()
3176{
3177 static int vendor_enhanced_info = -EINVAL;
3178 if (vendor_enhanced_info != -EINVAL)
3179 return vendor_enhanced_info;
3180
3181 vndk_fwk_init();
3182
3183 if (mVndkFwk.getVendorEnhancedInfo != NULL) {
3184 vendor_enhanced_info = mVndkFwk.getVendorEnhancedInfo();
3185 ALOGW("%s: vendor_enhanced_info 0x%x", __func__, vendor_enhanced_info);
3186 } else {
3187 vendor_enhanced_info = 0x0;
3188 ALOGW("%s: default to vendor_enhanced_info 0x0", __func__);
3189 }
3190
3191 return vendor_enhanced_info;
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -07003192}
Deeraj Soman14230922019-01-30 16:39:30 +05303193
Deeraj Somanfa377bf2019-02-06 12:57:59 +05303194int audio_extn_utils_get_perf_mode_flag(void)
3195{
3196#ifdef COMPRESSED_PERF_MODE_FLAG
3197 return COMPRESSED_PERF_MODE_FLAG;
3198#else
3199 return 0;
3200#endif
3201}
3202
Deeraj Soman14230922019-01-30 16:39:30 +05303203size_t audio_extn_utils_get_input_buffer_size(uint32_t sample_rate,
3204 audio_format_t format,
3205 int channel_count,
3206 int64_t duration_ms,
3207 bool is_low_latency)
3208{
3209 size_t size = 0;
3210 size_t capture_duration = AUDIO_CAPTURE_PERIOD_DURATION_MSEC;
3211 uint32_t bytes_per_period_sample = 0;
3212
3213
3214 if (audio_extn_utils_check_input_parameters(sample_rate, format, channel_count) != 0)
3215 return 0;
3216
3217 if (duration_ms >= MIN_OFFLOAD_BUFFER_DURATION_MS && duration_ms <= MAX_OFFLOAD_BUFFER_DURATION_MS)
3218 capture_duration = duration_ms;
3219
3220 size = (sample_rate * capture_duration) / 1000;
3221 if (is_low_latency)
3222 size = LOW_LATENCY_CAPTURE_PERIOD_SIZE;
3223
3224
3225 bytes_per_period_sample = audio_bytes_per_sample(format) * channel_count;
3226 size *= bytes_per_period_sample;
3227
3228 /* make sure the size is multiple of 32 bytes and additionally multiple of
3229 * the frame_size (required for 24bit samples and non-power-of-2 channel counts)
3230 * At 48 kHz mono 16-bit PCM:
3231 * 5.000 ms = 240 frames = 15*16*1*2 = 480, a whole multiple of 32 (15)
3232 * 3.333 ms = 160 frames = 10*16*1*2 = 320, a whole multiple of 32 (10)
3233 *
3234 * The loop reaches result within 32 iterations, as initial size is
3235 * already a multiple of frame_size
3236 */
3237 size = audio_extn_utils_nearest_multiple(size, audio_extn_utils_lcm(32, bytes_per_period_sample));
3238
3239 return size;
3240}
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07003241
3242int audio_extn_utils_hash_fn(void *key)
3243{
3244 return (int)key;
3245}
3246
3247bool audio_extn_utils_hash_eq(void *key1, void *key2)
3248{
3249 return (key1 == key2);
3250}