blob: 4675fc39c2933b9c342e8afbe49813eb2cf55ad0 [file] [log] [blame]
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001/*
Aalique Grahame22e49102018-12-18 14:23:57 -08002 * Copyright (c) 2014-2019, 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
Yamit Mehtaa0d653a2016-11-25 20:33:25 +053057#ifdef LINUX_ENABLED
58#define AUDIO_OUTPUT_POLICY_VENDOR_CONFIG_FILE "/etc/audio_output_policy.conf"
Dhananjay Kumard6d32152016-10-13 16:11:03 +053059#define AUDIO_IO_POLICY_VENDOR_CONFIG_FILE "/etc/audio_io_policy.conf"
Yamit Mehtaa0d653a2016-11-25 20:33:25 +053060#else
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070061#define AUDIO_OUTPUT_POLICY_VENDOR_CONFIG_FILE "/vendor/etc/audio_output_policy.conf"
Dhananjay Kumard6d32152016-10-13 16:11:03 +053062#define AUDIO_IO_POLICY_VENDOR_CONFIG_FILE "/vendor/etc/audio_io_policy.conf"
Yamit Mehtaa0d653a2016-11-25 20:33:25 +053063#endif
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070064
65#define OUTPUTS_TAG "outputs"
Dhananjay Kumard6d32152016-10-13 16:11:03 +053066#define INPUTS_TAG "inputs"
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070067
68#define DYNAMIC_VALUE_TAG "dynamic"
69#define FLAGS_TAG "flags"
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +053070#define PROFILES_TAG "profile"
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070071#define FORMATS_TAG "formats"
72#define SAMPLING_RATES_TAG "sampling_rates"
73#define BIT_WIDTH_TAG "bit_width"
74#define APP_TYPE_TAG "app_type"
75
76#define STRING_TO_ENUM(string) { #string, string }
77#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
78
Ben Rombergera04fabc2014-11-14 12:16:03 -080079#define BASE_TABLE_SIZE 64
80#define MAX_BASEINDEX_LEN 256
81
Ben Romberger1aaaf862017-04-06 17:49:46 -070082#ifndef SND_AUDIOCODEC_TRUEHD
83#define SND_AUDIOCODEC_TRUEHD 0x00000023
84#endif
85
Vikram Panduranga93f080e2017-06-07 18:16:14 -070086#define APP_TYPE_VOIP_AUDIO 0x1113A
87
Ashish Jain81eb2a82015-05-13 10:52:34 +053088#ifdef AUDIO_EXTERNAL_HDMI_ENABLED
89#define PROFESSIONAL (1<<0) /* 0 = consumer, 1 = professional */
90#define NON_LPCM (1<<1) /* 0 = audio, 1 = non-audio */
91#define SR_44100 (0<<0) /* 44.1kHz */
92#define SR_NOTID (1<<0) /* non indicated */
93#define SR_48000 (2<<0) /* 48kHz */
94#define SR_32000 (3<<0) /* 32kHz */
95#define SR_22050 (4<<0) /* 22.05kHz */
96#define SR_24000 (6<<0) /* 24kHz */
97#define SR_88200 (8<<0) /* 88.2kHz */
98#define SR_96000 (10<<0) /* 96kHz */
99#define SR_176400 (12<<0) /* 176.4kHz */
100#define SR_192000 (14<<0) /* 192kHz */
101
102#endif
Manish Dewangan07de2142017-02-27 19:27:20 +0530103
104/* ToDo: Check and update a proper value in msec */
Weiyin Jiangc49a3b52018-08-17 16:16:08 +0800105#define COMPRESS_OFFLOAD_PLAYBACK_LATENCY 50
106#define PCM_OFFLOAD_PLAYBACK_DSP_PATHDELAY 62
107#define PCM_OFFLOAD_PLAYBACK_LATENCY \
108 (PCM_OFFLOAD_BUFFER_DURATION + PCM_OFFLOAD_PLAYBACK_DSP_PATHDELAY)
Manish Dewangan07de2142017-02-27 19:27:20 +0530109
Varun Balaraje49253e2017-07-06 19:48:56 +0530110#ifndef MAX_CHANNELS_SUPPORTED
111#define MAX_CHANNELS_SUPPORTED 8
112#endif
113
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -0700114#ifdef __LP64__
115#define VNDK_FWK_LIB_PATH "/vendor/lib64/libqti_vndfwk_detect.so"
116#else
117#define VNDK_FWK_LIB_PATH "/vendor/lib/libqti_vndfwk_detect.so"
118#endif
119
Weiyin Jiang37472b92019-09-04 16:52:08 +0800120typedef struct vndkfwk_s {
121 void *lib_handle;
122 int (*isVendorEnhancedFwk)(void);
123 int (*getVendorEnhancedInfo)(void);
124 const char *lib_name;
125} vndkfwk_t;
126
127static vndkfwk_t mVndkFwk = {
128 NULL, NULL, NULL, VNDK_FWK_LIB_PATH};
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -0700129
`Deeraj Soman676c2702017-09-18 19:25:53 +0530130typedef struct {
131 const char *id_string;
132 const int value;
133} mixer_config_lookup;
134
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700135struct string_to_enum {
136 const char *name;
137 uint32_t value;
138};
139
140const struct string_to_enum s_flag_name_to_enum_table[] = {
141 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_DIRECT),
142 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_PRIMARY),
143 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_FAST),
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800144 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_RAW),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700145 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_DEEP_BUFFER),
146 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD),
147 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_NON_BLOCKING),
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700148 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_HW_AV_SYNC),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700149 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_INCALL_MUSIC),
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700150 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH),
Naresh Tanniruee3499a2017-01-05 14:05:35 +0530151 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_TIMESTAMP),
Vikram Panduranga93f080e2017-06-07 18:16:14 -0700152 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_VOIP_RX),
Ben Romberger6c4d3812017-06-13 17:46:45 -0700153 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_BD),
Varun Balaraje49253e2017-07-06 19:48:56 +0530154 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_INTERACTIVE),
Derek Chen090dfc52018-03-22 22:15:29 -0400155#ifdef AUDIO_EXTN_AUTO_HAL_ENABLED
156 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_MEDIA),
157 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_SYS_NOTIFICATION),
158 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_NAV_GUIDANCE),
159 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_PHONE),
160#endif
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530161 STRING_TO_ENUM(AUDIO_INPUT_FLAG_NONE),
162 STRING_TO_ENUM(AUDIO_INPUT_FLAG_FAST),
163 STRING_TO_ENUM(AUDIO_INPUT_FLAG_HW_HOTWORD),
164 STRING_TO_ENUM(AUDIO_INPUT_FLAG_RAW),
165 STRING_TO_ENUM(AUDIO_INPUT_FLAG_SYNC),
Dhananjay Kumaree4d2002016-10-25 18:02:58 +0530166 STRING_TO_ENUM(AUDIO_INPUT_FLAG_TIMESTAMP),
Dhananjay Kumar704ce6f2017-09-28 22:08:00 +0530167 STRING_TO_ENUM(AUDIO_INPUT_FLAG_COMPRESS),
Ralf Herzaec80262018-07-03 07:08:49 +0200168 STRING_TO_ENUM(AUDIO_INPUT_FLAG_PASSTHROUGH),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700169};
170
171const struct string_to_enum s_format_name_to_enum_table[] = {
Ashish Jain83a6cc22016-06-28 14:34:17 +0530172 STRING_TO_ENUM(AUDIO_FORMAT_PCM_8_BIT),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700173 STRING_TO_ENUM(AUDIO_FORMAT_PCM_16_BIT),
Ashish Jain5106d362016-05-11 19:23:33 +0530174 STRING_TO_ENUM(AUDIO_FORMAT_PCM_24_BIT_PACKED),
175 STRING_TO_ENUM(AUDIO_FORMAT_PCM_8_24_BIT),
Ashish Jainf1eaa582016-05-23 20:54:24 +0530176 STRING_TO_ENUM(AUDIO_FORMAT_PCM_32_BIT),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700177 STRING_TO_ENUM(AUDIO_FORMAT_MP3),
178 STRING_TO_ENUM(AUDIO_FORMAT_AAC),
179 STRING_TO_ENUM(AUDIO_FORMAT_VORBIS),
Mingming Yinae3530f2014-07-03 16:50:18 -0700180 STRING_TO_ENUM(AUDIO_FORMAT_AMR_NB),
181 STRING_TO_ENUM(AUDIO_FORMAT_AMR_WB),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700182 STRING_TO_ENUM(AUDIO_FORMAT_AC3),
Mingming Yinae3530f2014-07-03 16:50:18 -0700183 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700184 STRING_TO_ENUM(AUDIO_FORMAT_DTS),
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530185 STRING_TO_ENUM(AUDIO_FORMAT_DTS_HD),
Ben Romberger1aaaf862017-04-06 17:49:46 -0700186 STRING_TO_ENUM(AUDIO_FORMAT_DOLBY_TRUEHD),
Naresh Tanniru928f0862017-04-07 16:44:23 -0700187 STRING_TO_ENUM(AUDIO_FORMAT_IEC61937),
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530188 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3_JOC),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700189 STRING_TO_ENUM(AUDIO_FORMAT_WMA),
190 STRING_TO_ENUM(AUDIO_FORMAT_WMA_PRO),
191 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADIF),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700192 STRING_TO_ENUM(AUDIO_FORMAT_AMR_WB_PLUS),
193 STRING_TO_ENUM(AUDIO_FORMAT_EVRC),
194 STRING_TO_ENUM(AUDIO_FORMAT_EVRCB),
195 STRING_TO_ENUM(AUDIO_FORMAT_EVRCWB),
196 STRING_TO_ENUM(AUDIO_FORMAT_QCELP),
197 STRING_TO_ENUM(AUDIO_FORMAT_MP2),
198 STRING_TO_ENUM(AUDIO_FORMAT_EVRCNW),
Amit Shekhar6f461b12014-08-01 14:52:58 -0700199 STRING_TO_ENUM(AUDIO_FORMAT_FLAC),
Satya Krishna Pindiproli70471602015-04-24 19:12:43 +0530200 STRING_TO_ENUM(AUDIO_FORMAT_ALAC),
201 STRING_TO_ENUM(AUDIO_FORMAT_APE),
Alexy Josephcd8eaed2014-12-11 12:46:53 -0800202 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LC),
203 STRING_TO_ENUM(AUDIO_FORMAT_AAC_HE_V1),
204 STRING_TO_ENUM(AUDIO_FORMAT_AAC_HE_V2),
Manish Dewangana6fc5442015-08-24 20:30:31 +0530205 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS),
Ashish Jaine513a872015-11-19 17:00:56 +0530206 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_LC),
207 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_HE_V1),
208 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_HE_V2),
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +0530209 STRING_TO_ENUM(AUDIO_FORMAT_DSD),
Arun Kumar Dasari3b174182016-12-27 13:01:14 +0530210 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM),
211 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM_LC),
212 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM_HE_V1),
213 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM_HE_V2),
Dhanalakshmi Siddani18737932016-11-29 17:33:17 +0530214 STRING_TO_ENUM(AUDIO_FORMAT_APTX),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700215};
216
Manish Dewangan3ccdea52017-02-13 19:31:54 +0530217/* payload structure avt_device drift query */
218struct audio_avt_device_drift_stats {
219 uint32_t minor_version;
Manish Dewangan338c50a2017-09-12 15:22:03 +0530220
Manish Dewangan3ccdea52017-02-13 19:31:54 +0530221 /* Indicates the device interface direction as either
222 * source (Tx) or sink (Rx).
223 */
224 uint16_t device_direction;
Manish Dewangan338c50a2017-09-12 15:22:03 +0530225
226 /* Reference timer for drift accumulation and time stamp information.
227 * currently it only support AFE_REF_TIMER_TYPE_AVTIMER
228 */
229 uint16_t reference_timer;
Manish Dewangan3ccdea52017-02-13 19:31:54 +0530230 struct audio_avt_device_drift_param drift_param;
Manish Dewangan338c50a2017-09-12 15:22:03 +0530231} __attribute__((packed));
Manish Dewangan3ccdea52017-02-13 19:31:54 +0530232
Ben Rombergera04fabc2014-11-14 12:16:03 -0800233static char bTable[BASE_TABLE_SIZE] = {
234 'A','B','C','D','E','F','G','H','I','J','K','L',
235 'M','N','O','P','Q','R','S','T','U','V','W','X',
236 'Y','Z','a','b','c','d','e','f','g','h','i','j',
237 'k','l','m','n','o','p','q','r','s','t','u','v',
238 'w','x','y','z','0','1','2','3','4','5','6','7',
239 '8','9','+','/'
240};
241
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700242static uint32_t string_to_enum(const struct string_to_enum *table, size_t size,
243 const char *name)
244{
245 size_t i;
246 for (i = 0; i < size; i++) {
247 if (strcmp(table[i].name, name) == 0) {
248 ALOGV("%s found %s", __func__, table[i].name);
249 return table[i].value;
250 }
251 }
252 return 0;
253}
254
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530255static audio_io_flags_t parse_flag_names(char *name)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700256{
257 uint32_t flag = 0;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530258 audio_io_flags_t io_flags;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800259 char *last_r;
260 char *flag_name = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700261 while (flag_name != NULL) {
262 if (strlen(flag_name) != 0) {
263 flag |= string_to_enum(s_flag_name_to_enum_table,
264 ARRAY_SIZE(s_flag_name_to_enum_table),
265 flag_name);
266 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800267 flag_name = strtok_r(NULL, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700268 }
269
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800270 ALOGV("parse_flag_names: flag - %x", flag);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530271 io_flags.in_flags = (audio_input_flags_t)flag;
272 io_flags.out_flags = (audio_output_flags_t)flag;
273 return io_flags;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700274}
275
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530276static void parse_format_names(char *name, struct streams_io_cfg *s_info)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700277{
278 struct stream_format *sf_info = NULL;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800279 char *last_r;
280 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700281
282 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG) == 0)
283 return;
284
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530285 list_init(&s_info->format_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700286 while (str != NULL) {
287 audio_format_t format = (audio_format_t)string_to_enum(s_format_name_to_enum_table,
288 ARRAY_SIZE(s_format_name_to_enum_table), str);
289 ALOGV("%s: format - %d", __func__, format);
290 if (format != 0) {
291 sf_info = (struct stream_format *)calloc(1, sizeof(struct stream_format));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700292 if (sf_info == NULL)
293 break; /* return whatever was parsed */
294
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700295 sf_info->format = format;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530296 list_add_tail(&s_info->format_list, &sf_info->list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700297 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800298 str = strtok_r(NULL, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700299 }
300}
301
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530302static void parse_sample_rate_names(char *name, struct streams_io_cfg *s_info)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700303{
Amit Shekhar6f461b12014-08-01 14:52:58 -0700304 struct stream_sample_rate *ss_info = NULL;
305 uint32_t sample_rate = 48000;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800306 char *last_r;
307 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700308
Amit Shekhar6f461b12014-08-01 14:52:58 -0700309 if (str != NULL && 0 == strcmp(str, DYNAMIC_VALUE_TAG))
310 return;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700311
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530312 list_init(&s_info->sample_rate_list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700313 while (str != NULL) {
314 sample_rate = (uint32_t)strtol(str, (char **)NULL, 10);
315 ALOGV("%s: sample_rate - %d", __func__, sample_rate);
316 if (0 != sample_rate) {
317 ss_info = (struct stream_sample_rate *)calloc(1, sizeof(struct stream_sample_rate));
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800318 if (!ss_info) {
319 ALOGE("%s: memory allocation failure", __func__);
320 return;
321 }
Amit Shekhar6f461b12014-08-01 14:52:58 -0700322 ss_info->sample_rate = sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530323 list_add_tail(&s_info->sample_rate_list, &ss_info->list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700324 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800325 str = strtok_r(NULL, "|", &last_r);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700326 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700327}
328
329static int parse_bit_width_names(char *name)
330{
331 int bit_width = 16;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800332 char *last_r;
333 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700334
335 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG))
336 bit_width = (int)strtol(str, (char **)NULL, 10);
337
338 ALOGV("%s: bit_width - %d", __func__, bit_width);
339 return bit_width;
340}
341
342static int parse_app_type_names(void *platform, char *name)
343{
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700344 int app_type = platform_get_default_app_type(platform);
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800345 char *last_r;
346 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700347
348 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG))
349 app_type = (int)strtol(str, (char **)NULL, 10);
350
351 ALOGV("%s: app_type - %d", __func__, app_type);
352 return app_type;
353}
354
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530355static void update_streams_cfg_list(cnode *root, void *platform,
356 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700357{
358 cnode *node = root->first_child;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530359 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700360
361 ALOGV("%s", __func__);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530362 s_info = (struct streams_io_cfg *)calloc(1, sizeof(struct streams_io_cfg));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700363
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530364 if (!s_info) {
365 ALOGE("failed to allocate mem for s_info list element");
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700366 return;
367 }
368
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700369 while (node) {
370 if (strcmp(node->name, FLAGS_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530371 s_info->flags = parse_flag_names((char *)node->value);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530372 } else if (strcmp(node->name, PROFILES_TAG) == 0) {
373 strlcpy(s_info->profile, (char *)node->value, sizeof(s_info->profile));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700374 } else if (strcmp(node->name, FORMATS_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530375 parse_format_names((char *)node->value, s_info);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700376 } else if (strcmp(node->name, SAMPLING_RATES_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530377 s_info->app_type_cfg.sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
378 parse_sample_rate_names((char *)node->value, s_info);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700379 } else if (strcmp(node->name, BIT_WIDTH_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530380 s_info->app_type_cfg.bit_width = parse_bit_width_names((char *)node->value);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700381 } else if (strcmp(node->name, APP_TYPE_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530382 s_info->app_type_cfg.app_type = parse_app_type_names(platform, (char *)node->value);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700383 }
384 node = node->next;
385 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530386 list_add_tail(streams_cfg_list, &s_info->list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700387}
388
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530389static void load_cfg_list(cnode *root, void *platform,
390 struct listnode *streams_output_cfg_list,
391 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700392{
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530393 cnode *node = NULL;
394
395 node = config_find(root, OUTPUTS_TAG);
396 if (node != NULL) {
397 node = node->first_child;
398 while (node) {
399 ALOGV("%s: loading output %s", __func__, node->name);
400 update_streams_cfg_list(node, platform, streams_output_cfg_list);
401 node = node->next;
402 }
403 } else {
404 ALOGI("%s: could not load output, node is NULL", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700405 }
406
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530407 node = config_find(root, INPUTS_TAG);
408 if (node != NULL) {
409 node = node->first_child;
410 while (node) {
411 ALOGV("%s: loading input %s", __func__, node->name);
412 update_streams_cfg_list(node, platform, streams_input_cfg_list);
413 node = node->next;
414 }
415 } else {
416 ALOGI("%s: could not load input, node is NULL", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700417 }
418}
419
420static void send_app_type_cfg(void *platform, struct mixer *mixer,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530421 struct listnode *streams_output_cfg_list,
422 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700423{
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530424 size_t app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT] = {0};
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700425 int length = 0, i, num_app_types = 0;
426 struct listnode *node;
427 bool update;
428 struct mixer_ctl *ctl = NULL;
429 const char *mixer_ctl_name = "App Type Config";
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530430 struct streams_io_cfg *s_info = NULL;
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800431 uint32_t target_bit_width = 0;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700432
433 if (!mixer) {
434 ALOGE("%s: mixer is null",__func__);
435 return;
436 }
437 ctl = mixer_get_ctl_by_name(mixer, mixer_ctl_name);
438 if (!ctl) {
439 ALOGE("%s: Could not get ctl for mixer cmd - %s",__func__, mixer_ctl_name);
440 return;
441 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530442 app_type_cfg[length++] = num_app_types;
443
444 if (list_empty(streams_output_cfg_list)) {
445 app_type_cfg[length++] = platform_get_default_app_type_v2(platform, PCM_PLAYBACK);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700446 app_type_cfg[length++] = 48000;
447 app_type_cfg[length++] = 16;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530448 num_app_types += 1;
449 }
450 if (list_empty(streams_input_cfg_list)) {
451 app_type_cfg[length++] = platform_get_default_app_type_v2(platform, PCM_CAPTURE);
452 app_type_cfg[length++] = 48000;
453 app_type_cfg[length++] = 16;
454 num_app_types += 1;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700455 }
456
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800457 /* get target bit width for ADM enforce mode */
458 target_bit_width = adev_get_dsp_bit_width_enforce_mode();
459
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700460 list_for_each(node, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530461 s_info = node_to_item(node, struct streams_io_cfg, list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700462 update = true;
463 for (i=0; i<length; i=i+3) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530464 if (app_type_cfg[i+1] == 0)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700465 break;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530466 else if (app_type_cfg[i+1] == (size_t)s_info->app_type_cfg.app_type) {
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530467 if (app_type_cfg[i+2] < (size_t)s_info->app_type_cfg.sample_rate)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530468 app_type_cfg[i+2] = s_info->app_type_cfg.sample_rate;
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530469 if (app_type_cfg[i+3] < (size_t)s_info->app_type_cfg.bit_width)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530470 app_type_cfg[i+3] = s_info->app_type_cfg.bit_width;
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800471 /* ADM bit width = max(enforce_bit_width, bit_width from s_info */
472 if (audio_extn_is_dsp_bit_width_enforce_mode_supported(s_info->flags.out_flags) &&
473 (target_bit_width > app_type_cfg[i+3]))
474 app_type_cfg[i+3] = target_bit_width;
475
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700476 update = false;
477 break;
478 }
479 }
480 if (update && ((length + 3) <= MAX_LENGTH_MIXER_CONTROL_IN_INT)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530481 num_app_types += 1;
482 app_type_cfg[length++] = s_info->app_type_cfg.app_type;
483 app_type_cfg[length++] = s_info->app_type_cfg.sample_rate;
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800484 app_type_cfg[length] = s_info->app_type_cfg.bit_width;
485 if (audio_extn_is_dsp_bit_width_enforce_mode_supported(s_info->flags.out_flags) &&
486 (target_bit_width > app_type_cfg[length]))
487 app_type_cfg[length] = target_bit_width;
488
489 length++;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530490 }
491 }
492 list_for_each(node, streams_input_cfg_list) {
493 s_info = node_to_item(node, struct streams_io_cfg, list);
494 update = true;
495 for (i=0; i<length; i=i+3) {
496 if (app_type_cfg[i+1] == 0)
497 break;
498 else if (app_type_cfg[i+1] == (size_t)s_info->app_type_cfg.app_type) {
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530499 if (app_type_cfg[i+2] < (size_t)s_info->app_type_cfg.sample_rate)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530500 app_type_cfg[i+2] = s_info->app_type_cfg.sample_rate;
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530501 if (app_type_cfg[i+3] < (size_t)s_info->app_type_cfg.bit_width)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530502 app_type_cfg[i+3] = s_info->app_type_cfg.bit_width;
503 update = false;
504 break;
505 }
506 }
507 if (update && ((length + 3) <= MAX_LENGTH_MIXER_CONTROL_IN_INT)) {
508 num_app_types += 1;
509 app_type_cfg[length++] = s_info->app_type_cfg.app_type;
510 app_type_cfg[length++] = s_info->app_type_cfg.sample_rate;
511 app_type_cfg[length++] = s_info->app_type_cfg.bit_width;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700512 }
513 }
514 ALOGV("%s: num_app_types: %d", __func__, num_app_types);
515 if (num_app_types) {
516 app_type_cfg[0] = num_app_types;
517 mixer_ctl_set_array(ctl, app_type_cfg, length);
518 }
519}
520
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530521void audio_extn_utils_update_streams_cfg_lists(void *platform,
522 struct mixer *mixer,
523 struct listnode *streams_output_cfg_list,
524 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700525{
526 cnode *root;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530527 char *data = NULL;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700528
529 ALOGV("%s", __func__);
530 list_init(streams_output_cfg_list);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530531 list_init(streams_input_cfg_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700532
533 root = config_node("", "");
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700534 if (root == NULL) {
535 ALOGE("cfg_list, NULL config root");
536 return;
537 }
538
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530539 data = (char *)load_file(AUDIO_IO_POLICY_VENDOR_CONFIG_FILE, NULL);
540 if (data == NULL) {
541 ALOGD("%s: failed to open io config file(%s), trying older config file",
542 __func__, AUDIO_IO_POLICY_VENDOR_CONFIG_FILE);
543 data = (char *)load_file(AUDIO_OUTPUT_POLICY_VENDOR_CONFIG_FILE, NULL);
544 if (data == NULL) {
545 send_app_type_cfg(platform, mixer,
546 streams_output_cfg_list,
547 streams_input_cfg_list);
548 ALOGE("%s: could not load io policy config!", __func__);
yidongh6eb4f752017-04-19 18:20:57 +0800549 free(root);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530550 return;
551 }
552 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700553
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530554 config_load(root, data);
555 load_cfg_list(root, platform, streams_output_cfg_list,
556 streams_input_cfg_list);
557
558 send_app_type_cfg(platform, mixer, streams_output_cfg_list,
559 streams_input_cfg_list);
Alexy Josephaee4fdd2016-01-29 13:02:07 -0800560
561 config_free(root);
yidongh6eb4f752017-04-19 18:20:57 +0800562 free(root);
Alexy Josephaee4fdd2016-01-29 13:02:07 -0800563 free(data);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700564}
565
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530566static void audio_extn_utils_dump_streams_cfg_list(
567 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700568{
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700569 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530570 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700571 struct stream_format *sf_info;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700572 struct stream_sample_rate *ss_info;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530573
574 list_for_each(node_i, streams_cfg_list) {
575 s_info = node_to_item(node_i, struct streams_io_cfg, list);
576 ALOGV("%s: flags-%d, sample_rate-%d, bit_width-%d, app_type-%d",
577 __func__, s_info->flags.out_flags, s_info->app_type_cfg.sample_rate,
578 s_info->app_type_cfg.bit_width, s_info->app_type_cfg.app_type);
579 list_for_each(node_j, &s_info->format_list) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700580 sf_info = node_to_item(node_j, struct stream_format, list);
581 ALOGV("format-%x", sf_info->format);
582 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530583 list_for_each(node_j, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700584 ss_info = node_to_item(node_j, struct stream_sample_rate, list);
585 ALOGV("sample rate-%d", ss_info->sample_rate);
586 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700587 }
588}
589
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530590void audio_extn_utils_dump_streams_cfg_lists(
591 struct listnode *streams_output_cfg_list,
592 struct listnode *streams_input_cfg_list)
593{
594 ALOGV("%s", __func__);
595 audio_extn_utils_dump_streams_cfg_list(streams_output_cfg_list);
596 audio_extn_utils_dump_streams_cfg_list(streams_input_cfg_list);
597}
598
599static void audio_extn_utils_release_streams_cfg_list(
600 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700601{
602 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530603 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700604
605 ALOGV("%s", __func__);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530606
607 while (!list_empty(streams_cfg_list)) {
608 node_i = list_head(streams_cfg_list);
609 s_info = node_to_item(node_i, struct streams_io_cfg, list);
610 while (!list_empty(&s_info->format_list)) {
611 node_j = list_head(&s_info->format_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700612 list_remove(node_j);
613 free(node_to_item(node_j, struct stream_format, list));
614 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530615 while (!list_empty(&s_info->sample_rate_list)) {
616 node_j = list_head(&s_info->sample_rate_list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700617 list_remove(node_j);
618 free(node_to_item(node_j, struct stream_sample_rate, list));
619 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700620 list_remove(node_i);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530621 free(node_to_item(node_i, struct streams_io_cfg, list));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700622 }
623}
624
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530625void audio_extn_utils_release_streams_cfg_lists(
626 struct listnode *streams_output_cfg_list,
627 struct listnode *streams_input_cfg_list)
628{
629 ALOGV("%s", __func__);
630 audio_extn_utils_release_streams_cfg_list(streams_output_cfg_list);
631 audio_extn_utils_release_streams_cfg_list(streams_input_cfg_list);
632}
633
634static bool set_app_type_cfg(struct streams_io_cfg *s_info,
635 struct stream_app_type_cfg *app_type_cfg,
636 uint32_t sample_rate, uint32_t bit_width)
Amit Shekhar6f461b12014-08-01 14:52:58 -0700637 {
638 struct listnode *node_i;
639 struct stream_sample_rate *ss_info;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530640 list_for_each(node_i, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700641 ss_info = node_to_item(node_i, struct stream_sample_rate, list);
642 if ((sample_rate <= ss_info->sample_rate) &&
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530643 (bit_width == s_info->app_type_cfg.bit_width)) {
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700644
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530645 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700646 app_type_cfg->sample_rate = ss_info->sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530647 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700648 ALOGV("%s app_type_cfg->app_type %d, app_type_cfg->sample_rate %d, app_type_cfg->bit_width %d",
649 __func__, app_type_cfg->app_type, app_type_cfg->sample_rate, app_type_cfg->bit_width);
650 return true;
651 }
652 }
653 /*
654 * Reiterate through the list assuming dafault sample rate.
655 * Handles scenario where input sample rate is higher
656 * than all sample rates in list for the input bit width.
657 */
658 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
Apoorv Raghuvanshif59bb222015-02-18 12:23:23 -0800659
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530660 list_for_each(node_i, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700661 ss_info = node_to_item(node_i, struct stream_sample_rate, list);
662 if ((sample_rate <= ss_info->sample_rate) &&
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530663 (bit_width == s_info->app_type_cfg.bit_width)) {
664 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700665 app_type_cfg->sample_rate = sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530666 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Apoorv Raghuvanshif59bb222015-02-18 12:23:23 -0800667 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 -0700668 __func__, app_type_cfg->app_type, app_type_cfg->sample_rate, app_type_cfg->bit_width);
669 return true;
670 }
671 }
672 return false;
673}
674
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530675void audio_extn_utils_update_stream_input_app_type_cfg(void *platform,
676 struct listnode *streams_input_cfg_list,
677 audio_devices_t devices __unused,
678 audio_input_flags_t flags,
679 audio_format_t format,
680 uint32_t sample_rate,
681 uint32_t bit_width,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530682 char* profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530683 struct stream_app_type_cfg *app_type_cfg)
684{
685 struct listnode *node_i, *node_j;
686 struct streams_io_cfg *s_info;
687 struct stream_format *sf_info;
688
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530689 ALOGV("%s: flags: 0x%x, format: 0x%x sample_rate %d, profile %s",
690 __func__, flags, format, sample_rate, profile);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530691
692 list_for_each(node_i, streams_input_cfg_list) {
693 s_info = node_to_item(node_i, struct streams_io_cfg, list);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530694 /* Along with flags do profile matching if set at either end.*/
695 if (s_info->flags.in_flags == flags &&
696 ((profile[0] == '\0' && s_info->profile[0] == '\0') ||
697 strncmp(s_info->profile, profile, sizeof(s_info->profile)) == 0)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530698 list_for_each(node_j, &s_info->format_list) {
699 sf_info = node_to_item(node_j, struct stream_format, list);
700 if (sf_info->format == format) {
701 if (set_app_type_cfg(s_info, app_type_cfg, sample_rate, bit_width))
702 return;
703 }
704 }
705 }
706 }
707 ALOGW("%s: App type could not be selected. Falling back to default", __func__);
708 app_type_cfg->app_type = platform_get_default_app_type_v2(platform, PCM_CAPTURE);
709 app_type_cfg->sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
710 app_type_cfg->bit_width = 16;
711}
712
713void audio_extn_utils_update_stream_output_app_type_cfg(void *platform,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700714 struct listnode *streams_output_cfg_list,
Amit Shekhar1d896042014-10-03 13:16:09 -0700715 audio_devices_t devices,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700716 audio_output_flags_t flags,
717 audio_format_t format,
Amit Shekhar6f461b12014-08-01 14:52:58 -0700718 uint32_t sample_rate,
719 uint32_t bit_width,
Manish Dewangan837dc462015-05-27 10:17:41 +0530720 audio_channel_mask_t channel_mask,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530721 char *profile,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700722 struct stream_app_type_cfg *app_type_cfg)
723{
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +0530724 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530725 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700726 struct stream_format *sf_info;
Manish Dewangan837dc462015-05-27 10:17:41 +0530727 char value[PROPERTY_VALUE_MAX] = {0};
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700728
Ramu Gottipati074fa4d2018-09-11 20:05:51 +0530729 if (devices & AUDIO_DEVICE_OUT_SPEAKER) {
730 int bw = platform_get_snd_device_bit_width(SND_DEVICE_OUT_SPEAKER);
731 if ((-ENOSYS != bw) && (bit_width > (uint32_t)bw))
Amit Shekhar5a39c912014-10-14 15:39:30 -0700732 bit_width = (uint32_t)bw;
Ramu Gottipati074fa4d2018-09-11 20:05:51 +0530733 else if (-ENOSYS == bw)
734 bit_width = CODEC_BACKEND_DEFAULT_BIT_WIDTH;
Amit Shekhar1d896042014-10-03 13:16:09 -0700735 sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Ramu Gottipati074fa4d2018-09-11 20:05:51 +0530736 ALOGI("%s Allowing 24 and above bits playback on speaker ONLY at default sampling rate", __func__);
Amit Shekhar1d896042014-10-03 13:16:09 -0700737 }
738
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -0700739 property_get("vendor.audio.playback.mch.downsample",value,"");
Manish Dewangan837dc462015-05-27 10:17:41 +0530740 if (!strncmp("true", value, sizeof("true"))) {
741 if ((popcount(channel_mask) > 2) &&
742 (sample_rate > CODEC_BACKEND_DEFAULT_SAMPLE_RATE) &&
Aalique Grahame5cd12ff2017-10-19 10:57:00 -0700743 !(flags & (audio_output_flags_t)AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH)) {
Manish Dewangan837dc462015-05-27 10:17:41 +0530744 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
745 ALOGD("%s: MCH session defaulting sample rate to %d",
746 __func__, sample_rate);
747 }
748 }
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +0530749
750 /* Set sampling rate to 176.4 for DSD64
751 * and 352.8Khz for DSD128.
752 * Set Bit Width to 16. output will be 16 bit
753 * post DoP in ASM.
754 */
Aalique Grahame5cd12ff2017-10-19 10:57:00 -0700755 if ((flags & (audio_output_flags_t)AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH) &&
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +0530756 (format == AUDIO_FORMAT_DSD)) {
757 bit_width = 16;
758 if (sample_rate == INPUT_SAMPLING_RATE_DSD64)
759 sample_rate = OUTPUT_SAMPLING_RATE_DSD64;
760 else if (sample_rate == INPUT_SAMPLING_RATE_DSD128)
761 sample_rate = OUTPUT_SAMPLING_RATE_DSD128;
762 }
763
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +0530764
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800765 ALOGV("%s: flags: %x, format: %x sample_rate %d, profile %s, app_type %d",
766 __func__, flags, format, sample_rate, profile, app_type_cfg->app_type);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700767 list_for_each(node_i, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530768 s_info = node_to_item(node_i, struct streams_io_cfg, list);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530769 /* Along with flags do profile matching if set at either end.*/
770 if (s_info->flags.out_flags == flags &&
771 ((profile[0] == '\0' && s_info->profile[0] == '\0') ||
772 strncmp(s_info->profile, profile, sizeof(s_info->profile)) == 0)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530773 list_for_each(node_j, &s_info->format_list) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700774 sf_info = node_to_item(node_j, struct stream_format, list);
775 if (sf_info->format == format) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530776 if (set_app_type_cfg(s_info, app_type_cfg, sample_rate, bit_width))
Amit Shekhar6f461b12014-08-01 14:52:58 -0700777 return;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700778 }
779 }
780 }
781 }
782 list_for_each(node_i, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530783 s_info = node_to_item(node_i, struct streams_io_cfg, list);
784 if (s_info->flags.out_flags == AUDIO_OUTPUT_FLAG_PRIMARY) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700785 ALOGV("Compatible output profile not found.");
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530786 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
787 app_type_cfg->sample_rate = s_info->app_type_cfg.sample_rate;
788 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700789 ALOGV("%s Default to primary output: App type: %d sample_rate %d",
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530790 __func__, s_info->app_type_cfg.app_type, app_type_cfg->sample_rate);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700791 return;
792 }
793 }
794 ALOGW("%s: App type could not be selected. Falling back to default", __func__);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700795 app_type_cfg->app_type = platform_get_default_app_type(platform);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700796 app_type_cfg->sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700797 app_type_cfg->bit_width = 16;
798}
799
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +0530800static bool audio_is_this_native_usecase(struct audio_usecase *uc)
801{
802 bool native_usecase = false;
803 struct stream_out *out = (struct stream_out*) uc->stream.out;
804
805 if (PCM_PLAYBACK == uc->type && out != NULL &&
806 NATIVE_AUDIO_MODE_INVALID != platform_get_native_support() &&
807 is_offload_usecase(uc->id) &&
808 (out->sample_rate == OUTPUT_SAMPLING_RATE_44100))
809 native_usecase = true;
810
811 return native_usecase;
812}
813
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800814bool audio_extn_is_dsp_bit_width_enforce_mode_supported(audio_output_flags_t flags)
815{
816 /* DSP bitwidth enforce mode for ADM and AFE:
817 * includes:
818 * deep buffer, low latency, direct pcm and offload.
819 * excludes:
820 * ull(raw+fast), VOIP.
821 */
822 if ((flags & AUDIO_OUTPUT_FLAG_VOIP_RX) ||
823 ((flags & AUDIO_OUTPUT_FLAG_RAW) &&
824 (flags & AUDIO_OUTPUT_FLAG_FAST)))
825 return false;
826
827
828 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) ||
829 (flags & AUDIO_OUTPUT_FLAG_DIRECT) ||
830 (flags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) ||
831 (flags & AUDIO_OUTPUT_FLAG_PRIMARY))
832 return true;
833 else
834 return false;
835}
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800836
837static inline bool audio_is_vr_mode_on(struct audio_device *(__attribute__((unused)) adev))
838{
839 return adev->vr_audio_mode_enabled;
840}
841
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530842void audio_extn_utils_update_stream_app_type_cfg_for_usecase(
843 struct audio_device *adev,
844 struct audio_usecase *usecase)
845{
846 ALOGV("%s", __func__);
847
848 switch(usecase->type) {
849 case PCM_PLAYBACK:
850 audio_extn_utils_update_stream_output_app_type_cfg(adev->platform,
851 &adev->streams_output_cfg_list,
852 usecase->stream.out->devices,
853 usecase->stream.out->flags,
Aalique Grahame65780b52017-09-27 14:59:56 -0700854 usecase->stream.out->hal_op_format,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530855 usecase->stream.out->sample_rate,
856 usecase->stream.out->bit_width,
857 usecase->stream.out->channel_mask,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530858 usecase->stream.out->profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530859 &usecase->stream.out->app_type_cfg);
860 ALOGV("%s Selected apptype: %d", __func__, usecase->stream.out->app_type_cfg.app_type);
861 break;
862 case PCM_CAPTURE:
Vikram Panduranga93f080e2017-06-07 18:16:14 -0700863 if (usecase->id == USECASE_AUDIO_RECORD_VOIP)
864 usecase->stream.in->app_type_cfg.app_type = APP_TYPE_VOIP_AUDIO;
865 else
866 audio_extn_utils_update_stream_input_app_type_cfg(adev->platform,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530867 &adev->streams_input_cfg_list,
868 usecase->stream.in->device,
869 usecase->stream.in->flags,
870 usecase->stream.in->format,
871 usecase->stream.in->sample_rate,
872 usecase->stream.in->bit_width,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530873 usecase->stream.in->profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530874 &usecase->stream.in->app_type_cfg);
875 ALOGV("%s Selected apptype: %d", __func__, usecase->stream.in->app_type_cfg.app_type);
876 break;
Surendar Karka93cd25a2018-08-28 14:21:37 +0530877 case TRANSCODE_LOOPBACK_RX :
Siddartha Shaik343abc62017-08-08 11:15:25 +0530878 audio_extn_utils_update_stream_output_app_type_cfg(adev->platform,
879 &adev->streams_output_cfg_list,
880 usecase->stream.inout->out_config.devices,
881 0,
882 usecase->stream.inout->out_config.format,
883 usecase->stream.inout->out_config.sample_rate,
884 usecase->stream.inout->out_config.bit_width,
885 usecase->stream.inout->out_config.channel_mask,
886 usecase->stream.inout->profile,
887 &usecase->stream.inout->out_app_type_cfg);
888 ALOGV("%s Selected apptype: %d", __func__, usecase->stream.inout->out_app_type_cfg.app_type);
889 break;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530890 default:
891 ALOGE("%s: app type cfg not supported for usecase type (%d)",
892 __func__, usecase->type);
893 }
894}
895
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +0530896void audio_extn_btsco_get_sample_rate(int snd_device, int *sample_rate)
897{
898 switch (snd_device) {
899 case SND_DEVICE_OUT_BT_SCO:
900 case SND_DEVICE_IN_BT_SCO_MIC:
901 case SND_DEVICE_IN_BT_SCO_MIC_NREC:
902 *sample_rate = 8000;
903 break;
904 case SND_DEVICE_OUT_BT_SCO_WB:
905 case SND_DEVICE_IN_BT_SCO_MIC_WB:
906 case SND_DEVICE_IN_BT_SCO_MIC_WB_NREC:
907 *sample_rate = 16000;
908 break;
909 default:
910 ALOGD("%s:Not a BT SCO device, need not update sampling rate\n", __func__);
911 break;
912 }
913}
914
Aalique Grahame22e49102018-12-18 14:23:57 -0800915static int set_stream_app_type_mixer_ctrl(struct audio_device *adev,
916 int pcm_device_id, int app_type,
917 int acdb_dev_id, int sample_rate,
918 int stream_type,
919 snd_device_t snd_device)
920{
921
922 char mixer_ctl_name[MAX_LENGTH_MIXER_CONTROL_IN_INT];
923 struct mixer_ctl *ctl;
924 int app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT], len = 0, rc = 0;
925 int snd_device_be_idx = -1;
926
927 if (stream_type == PCM_PLAYBACK) {
928 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
929 "Audio Stream %d App Type Cfg", pcm_device_id);
930 } else if (stream_type == PCM_CAPTURE) {
931 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
932 "Audio Stream Capture %d App Type Cfg", pcm_device_id);
933 }
934
935 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
936 if (!ctl) {
937 ALOGE("%s: Could not get ctl for mixer cmd - %s",
938 __func__, mixer_ctl_name);
939 rc = -EINVAL;
940 goto exit;
941 }
942 app_type_cfg[len++] = app_type;
943 app_type_cfg[len++] = acdb_dev_id;
944 app_type_cfg[len++] = sample_rate;
945
946 snd_device_be_idx = platform_get_snd_device_backend_index(snd_device);
947 if (snd_device_be_idx > 0)
948 app_type_cfg[len++] = snd_device_be_idx;
949 ALOGV("%s: stream type %d app_type %d, acdb_dev_id %d "
950 "sample rate %d, snd_device_be_idx %d",
951 __func__, stream_type, app_type, acdb_dev_id, sample_rate,
952 snd_device_be_idx);
953 mixer_ctl_set_array(ctl, app_type_cfg, len);
954
955exit:
956 return rc;
957}
958
959static int audio_extn_utils_send_app_type_cfg_hfp(struct audio_device *adev,
960 struct audio_usecase *usecase)
961{
962 int pcm_device_id, acdb_dev_id = 0, snd_device = usecase->out_snd_device;
963 int32_t sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
964 int app_type = 0, rc = 0;
965
966 ALOGV("%s", __func__);
967
968 if (usecase->type != PCM_HFP_CALL) {
969 ALOGV("%s: not a playback or HFP path, no need to cfg app type", __func__);
970 rc = 0;
971 goto exit_send_app_type_cfg;
972 }
973 if ((usecase->id != USECASE_AUDIO_HFP_SCO) &&
974 (usecase->id != USECASE_AUDIO_HFP_SCO_WB)) {
975 ALOGV("%s: a playback path where app type cfg is not required", __func__);
976 rc = 0;
977 goto exit_send_app_type_cfg;
978 }
979
980 snd_device = usecase->out_snd_device;
981 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_PLAYBACK);
982
983 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
984 if (acdb_dev_id < 0) {
985 ALOGE("%s: Couldn't get the acdb dev id", __func__);
986 rc = -EINVAL;
987 goto exit_send_app_type_cfg;
988 }
989
990 if (usecase->type == PCM_HFP_CALL) {
991
992 /* config HFP session:1 playback path */
993 app_type = platform_get_default_app_type_v2(adev->platform, PCM_PLAYBACK);
994 sample_rate= CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
995 rc = set_stream_app_type_mixer_ctrl(adev, pcm_device_id, app_type,
996 acdb_dev_id, sample_rate,
997 PCM_PLAYBACK,
998 SND_DEVICE_NONE); // use legacy behavior
999 if (rc < 0)
1000 goto exit_send_app_type_cfg;
1001
1002 /* config HFP session:1 capture path */
1003 app_type = platform_get_default_app_type_v2(adev->platform, PCM_CAPTURE);
1004 rc = set_stream_app_type_mixer_ctrl(adev, pcm_device_id, app_type,
1005 acdb_dev_id, sample_rate,
1006 PCM_CAPTURE,
1007 SND_DEVICE_NONE);
1008 if (rc < 0)
1009 goto exit_send_app_type_cfg;
1010
1011 /* config HFP session:2 capture path */
1012 pcm_device_id = HFP_ASM_RX_TX;
1013 snd_device = usecase->in_snd_device;
1014 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
1015 if (acdb_dev_id <= 0) {
1016 ALOGE("%s: Couldn't get the acdb dev id", __func__);
1017 rc = -EINVAL;
1018 goto exit_send_app_type_cfg;
1019 }
1020 app_type = platform_get_default_app_type_v2(adev->platform, PCM_CAPTURE);
1021 rc = set_stream_app_type_mixer_ctrl(adev, pcm_device_id, app_type,
1022 acdb_dev_id, sample_rate, PCM_CAPTURE,
1023 SND_DEVICE_NONE);
1024 if (rc < 0)
1025 goto exit_send_app_type_cfg;
1026
1027 /* config HFP session:2 playback path */
1028 app_type = platform_get_default_app_type_v2(adev->platform, PCM_PLAYBACK);
1029 rc = set_stream_app_type_mixer_ctrl(adev, pcm_device_id, app_type,
1030 acdb_dev_id, sample_rate,
1031 PCM_PLAYBACK, SND_DEVICE_NONE);
1032 if (rc < 0)
1033 goto exit_send_app_type_cfg;
1034 }
1035
1036 rc = 0;
1037exit_send_app_type_cfg:
1038 return rc;
1039}
1040
Zhou Song06761dd2019-04-28 18:08:17 +08001041int audio_extn_utils_get_app_sample_rate_for_device(
1042 struct audio_device *adev,
1043 struct audio_usecase *usecase, int snd_device)
1044{
1045 char value[PROPERTY_VALUE_MAX] = {0};
1046 int sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
1047
1048 if ((usecase->type == PCM_PLAYBACK) && (usecase->stream.out != NULL)) {
1049 property_get("vendor.audio.playback.mch.downsample",value,"");
1050 if (!strncmp("true", value, sizeof("true"))) {
1051 if ((popcount(usecase->stream.out->channel_mask) > 2) &&
1052 (usecase->stream.out->app_type_cfg.sample_rate > CODEC_BACKEND_DEFAULT_SAMPLE_RATE) &&
1053 !(usecase->stream.out->flags &
1054 (audio_output_flags_t)AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH))
1055 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
1056 }
1057
1058 if (usecase->id == USECASE_AUDIO_PLAYBACK_VOIP) {
1059 usecase->stream.out->app_type_cfg.sample_rate = usecase->stream.out->sample_rate;
1060 } else if (usecase->stream.out->devices & AUDIO_DEVICE_OUT_SPEAKER) {
1061 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
1062 } else if ((snd_device == SND_DEVICE_OUT_HDMI ||
1063 snd_device == SND_DEVICE_OUT_USB_HEADSET ||
1064 snd_device == SND_DEVICE_OUT_DISPLAY_PORT) &&
1065 (usecase->stream.out->sample_rate >= OUTPUT_SAMPLING_RATE_44100)) {
1066 /*
1067 * To best utlize DSP, check if the stream sample rate is supported/multiple of
1068 * configured device sample rate, if not update the COPP rate to be equal to the
1069 * device sample rate, else open COPP at stream sample rate
1070 */
1071 platform_check_and_update_copp_sample_rate(adev->platform, snd_device,
1072 usecase->stream.out->sample_rate,
1073 &usecase->stream.out->app_type_cfg.sample_rate);
1074 } else if (((snd_device != SND_DEVICE_OUT_HEADPHONES_44_1 &&
1075 !audio_is_this_native_usecase(usecase)) &&
1076 usecase->stream.out->sample_rate == OUTPUT_SAMPLING_RATE_44100) ||
1077 (usecase->stream.out->sample_rate < OUTPUT_SAMPLING_RATE_44100)) {
1078 /* Reset to default if no native stream is active*/
1079 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Zhou Song82ec30d2019-12-30 17:52:10 +08001080 } else if (usecase->out_snd_device == SND_DEVICE_OUT_BT_A2DP ||
1081 usecase->out_snd_device == SND_DEVICE_OUT_SPEAKER_AND_BT_A2DP ||
1082 usecase->out_snd_device == SND_DEVICE_OUT_SPEAKER_SAFE_AND_BT_A2DP) {
Zhou Song06761dd2019-04-28 18:08:17 +08001083 /*
1084 * For a2dp playback get encoder sampling rate and set copp sampling rate,
1085 * for bit width use the stream param only.
1086 */
1087 audio_extn_a2dp_get_enc_sample_rate(&usecase->stream.out->app_type_cfg.sample_rate);
1088 ALOGI("%s using %d sample rate rate for A2DP CoPP",
1089 __func__, usecase->stream.out->app_type_cfg.sample_rate);
1090 }
1091 audio_extn_btsco_get_sample_rate(snd_device, &usecase->stream.out->app_type_cfg.sample_rate);
1092 sample_rate = usecase->stream.out->app_type_cfg.sample_rate;
1093
1094 if (((usecase->stream.out->format == AUDIO_FORMAT_E_AC3) ||
1095 (usecase->stream.out->format == AUDIO_FORMAT_E_AC3_JOC) ||
1096 (usecase->stream.out->format == AUDIO_FORMAT_DOLBY_TRUEHD))
1097 && audio_extn_passthru_is_passthrough_stream(usecase->stream.out)
1098 && !audio_extn_passthru_is_convert_supported(adev, usecase->stream.out)) {
1099 sample_rate = sample_rate * 4;
1100 if (sample_rate > HDMI_PASSTHROUGH_MAX_SAMPLE_RATE)
1101 sample_rate = HDMI_PASSTHROUGH_MAX_SAMPLE_RATE;
1102 }
1103 } else if (usecase->type == PCM_CAPTURE) {
1104 if (usecase->stream.in != NULL) {
1105 if (usecase->id == USECASE_AUDIO_RECORD_VOIP)
1106 usecase->stream.in->app_type_cfg.sample_rate = usecase->stream.in->sample_rate;
1107 if (voice_is_in_call_rec_stream(usecase->stream.in)) {
1108 audio_extn_btsco_get_sample_rate(usecase->in_snd_device,
1109 &usecase->stream.in->app_type_cfg.sample_rate);
1110 } else {
1111 audio_extn_btsco_get_sample_rate(snd_device,
1112 &usecase->stream.in->app_type_cfg.sample_rate);
1113 }
1114 sample_rate = usecase->stream.in->app_type_cfg.sample_rate;
1115 } else if (usecase->id == USECASE_AUDIO_SPKR_CALIB_TX) {
Sujin Panicker0961d712019-09-09 10:55:38 +05301116 if ((property_get("vendor.audio.spkr_prot.tx.sampling_rate", value, NULL) > 0))
1117 sample_rate = atoi(value);
1118 else
1119 sample_rate = SAMPLE_RATE_8000;
Zhou Song06761dd2019-04-28 18:08:17 +08001120 }
1121 } else if (usecase->type == TRANSCODE_LOOPBACK_RX) {
1122 sample_rate = usecase->stream.inout->out_config.sample_rate;
1123 }
1124 return sample_rate;
1125}
1126
Siena Richard7c2db772016-12-21 11:32:34 -08001127static int send_app_type_cfg_for_device(struct audio_device *adev,
1128 struct audio_usecase *usecase,
1129 int split_snd_device)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001130{
1131 char mixer_ctl_name[MAX_LENGTH_MIXER_CONTROL_IN_INT];
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301132 size_t app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT] = {0};
1133 int len = 0, rc;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001134 struct mixer_ctl *ctl;
Siena Richard7c2db772016-12-21 11:32:34 -08001135 int pcm_device_id = 0, acdb_dev_id, app_type;
1136 int snd_device = split_snd_device, snd_device_be_idx = -1;
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +05301137 int32_t sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Varun Balaraje49253e2017-07-06 19:48:56 +05301138 struct streams_io_cfg *s_info = NULL;
1139 struct listnode *node = NULL;
Nikhil Laturkarac4a7492017-08-31 18:27:19 +05301140 int bd_app_type = 0;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001141
Siena Richard7c2db772016-12-21 11:32:34 -08001142 ALOGV("%s: usecase->out_snd_device %s, usecase->in_snd_device %s, split_snd_device %s",
1143 __func__, platform_get_snd_device_name(usecase->out_snd_device),
1144 platform_get_snd_device_name(usecase->in_snd_device),
1145 platform_get_snd_device_name(split_snd_device));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001146
Siddartha Shaik343abc62017-08-08 11:15:25 +05301147 if (usecase->type != PCM_PLAYBACK && usecase->type != PCM_CAPTURE &&
Surendar Karka93cd25a2018-08-28 14:21:37 +05301148 usecase->type != TRANSCODE_LOOPBACK_RX) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301149 ALOGE("%s: not a playback/capture path, no need to cfg app type", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001150 rc = 0;
1151 goto exit_send_app_type_cfg;
1152 }
1153 if ((usecase->id != USECASE_AUDIO_PLAYBACK_DEEP_BUFFER) &&
1154 (usecase->id != USECASE_AUDIO_PLAYBACK_LOW_LATENCY) &&
1155 (usecase->id != USECASE_AUDIO_PLAYBACK_MULTI_CH) &&
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -08001156 (usecase->id != USECASE_AUDIO_PLAYBACK_ULL) &&
Vikram Panduranga93f080e2017-06-07 18:16:14 -07001157 (usecase->id != USECASE_AUDIO_PLAYBACK_VOIP) &&
Surendar Karka93cd25a2018-08-28 14:21:37 +05301158 (usecase->id != USECASE_AUDIO_TRANSCODE_LOOPBACK_RX) &&
Varun Balaraje49253e2017-07-06 19:48:56 +05301159 (!is_interactive_usecase(usecase->id)) &&
Srikanth Uyyala9d551402015-08-25 16:03:42 +05301160 (!is_offload_usecase(usecase->id)) &&
Derek Chenf6318be2017-06-12 17:16:24 -04001161 (usecase->type != PCM_CAPTURE) &&
1162 (!audio_extn_auto_hal_is_bus_device_usecase(usecase->id))) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301163 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 -07001164 rc = 0;
1165 goto exit_send_app_type_cfg;
1166 }
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -08001167
1168 //if VR is active then only send the mixer control
1169 if (usecase->id == USECASE_AUDIO_PLAYBACK_ULL && !audio_is_vr_mode_on(adev)) {
1170 ALOGI("ULL doesnt need sending app type cfg, returning");
1171 rc = 0;
1172 goto exit_send_app_type_cfg;
1173 }
1174
Surendar Karka93cd25a2018-08-28 14:21:37 +05301175 if (usecase->type == PCM_PLAYBACK || usecase->type == TRANSCODE_LOOPBACK_RX) {
Ben Romberger1fafdde2015-09-09 19:43:15 -07001176 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_PLAYBACK);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301177 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
1178 "Audio Stream %d App Type Cfg", pcm_device_id);
Ben Romberger1fafdde2015-09-09 19:43:15 -07001179 } else if (usecase->type == PCM_CAPTURE) {
Ben Romberger1fafdde2015-09-09 19:43:15 -07001180 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_CAPTURE);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301181 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
1182 "Audio Stream Capture %d App Type Cfg", pcm_device_id);
Srikanth Uyyala9d551402015-08-25 16:03:42 +05301183 }
Siena Richard7c2db772016-12-21 11:32:34 -08001184
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001185 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
1186 if (!ctl) {
1187 ALOGE("%s: Could not get ctl for mixer cmd - %s", __func__,
1188 mixer_ctl_name);
1189 rc = -EINVAL;
1190 goto exit_send_app_type_cfg;
1191 }
Aditya Bavanari701a6992017-03-30 19:17:16 +05301192 snd_device = platform_get_spkr_prot_snd_device(snd_device);
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301193 if (voice_is_in_call_rec_stream(usecase->stream.in) && usecase->type == PCM_CAPTURE) {
1194 snd_device_t voice_device = voice_get_incall_rec_snd_device(usecase->in_snd_device);
1195 acdb_dev_id = platform_get_snd_device_acdb_id(voice_device);
1196 ALOGV("acdb id for voice call use case %d", acdb_dev_id);
1197 } else {
1198 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
1199 }
Rohit Kumar1181b292017-01-31 18:07:17 +05301200 if (acdb_dev_id <= 0) {
1201 ALOGE("%s: Couldn't get the acdb dev id", __func__);
1202 rc = -EINVAL;
1203 goto exit_send_app_type_cfg;
1204 }
1205
Siena Richard7c2db772016-12-21 11:32:34 -08001206 snd_device_be_idx = platform_get_snd_device_backend_index(snd_device);
1207 if (snd_device_be_idx < 0) {
1208 ALOGE("%s: Couldn't get the backend index for snd device %s ret=%d",
1209 __func__, platform_get_snd_device_name(snd_device),
1210 snd_device_be_idx);
1211 }
1212
Zhou Song06761dd2019-04-28 18:08:17 +08001213 sample_rate = audio_extn_utils_get_app_sample_rate_for_device(adev, usecase, snd_device);
1214
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301215 if ((usecase->type == PCM_PLAYBACK) && (usecase->stream.out != NULL)) {
Varun Balaraje49253e2017-07-06 19:48:56 +05301216 /* Interactive streams are supported with only direct app type id.
1217 * Get Direct profile app type and use it for interactive streams
1218 */
1219 list_for_each(node, &adev->streams_output_cfg_list) {
1220 s_info = node_to_item(node, struct streams_io_cfg, list);
Aalique Grahame5cd12ff2017-10-19 10:57:00 -07001221 if (s_info->flags.out_flags == (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_BD |
Nikhil Laturkarac4a7492017-08-31 18:27:19 +05301222 AUDIO_OUTPUT_FLAG_DIRECT_PCM |
1223 AUDIO_OUTPUT_FLAG_DIRECT))
1224 bd_app_type = s_info->app_type_cfg.app_type;
Varun Balaraje49253e2017-07-06 19:48:56 +05301225 }
Aalique Grahame5cd12ff2017-10-19 10:57:00 -07001226 if (usecase->stream.out->flags == (audio_output_flags_t)AUDIO_OUTPUT_FLAG_INTERACTIVE)
Nikhil Laturkarac4a7492017-08-31 18:27:19 +05301227 app_type = bd_app_type;
Varun Balaraje49253e2017-07-06 19:48:56 +05301228 else
1229 app_type = usecase->stream.out->app_type_cfg.app_type;
Siena Richard7c2db772016-12-21 11:32:34 -08001230 app_type_cfg[len++] = app_type;
Mingming Yin21854652016-04-13 11:54:02 -07001231 app_type_cfg[len++] = acdb_dev_id;
Naresh Tanniru3a406772017-05-10 13:09:05 -07001232 app_type_cfg[len++] = sample_rate;
1233
Siena Richard7c2db772016-12-21 11:32:34 -08001234 if (snd_device_be_idx > 0)
1235 app_type_cfg[len++] = snd_device_be_idx;
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301236
Siena Richard7c2db772016-12-21 11:32:34 -08001237 ALOGI("%s PLAYBACK app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
1238 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301239
1240 } else if ((usecase->type == PCM_CAPTURE) && (usecase->stream.in != NULL)) {
Siena Richard7c2db772016-12-21 11:32:34 -08001241 app_type = usecase->stream.in->app_type_cfg.app_type;
1242 app_type_cfg[len++] = app_type;
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301243 app_type_cfg[len++] = acdb_dev_id;
Siena Richard7c2db772016-12-21 11:32:34 -08001244 app_type_cfg[len++] = sample_rate;
1245 if (snd_device_be_idx > 0)
1246 app_type_cfg[len++] = snd_device_be_idx;
1247 ALOGI("%s CAPTURE app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
1248 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301249 } else {
Siena Richard7c2db772016-12-21 11:32:34 -08001250 app_type = platform_get_default_app_type_v2(adev->platform, usecase->type);
Surendar Karka93cd25a2018-08-28 14:21:37 +05301251 if(usecase->type == TRANSCODE_LOOPBACK_RX) {
Siddartha Shaik343abc62017-08-08 11:15:25 +05301252 app_type = usecase->stream.inout->out_app_type_cfg.app_type;
1253 }
Siena Richard7c2db772016-12-21 11:32:34 -08001254 app_type_cfg[len++] = app_type;
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301255 app_type_cfg[len++] = acdb_dev_id;
1256 app_type_cfg[len++] = sample_rate;
Siena Richard7c2db772016-12-21 11:32:34 -08001257 if (snd_device_be_idx > 0)
1258 app_type_cfg[len++] = snd_device_be_idx;
1259 ALOGI("%s default app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
1260 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Ashish Jainc02430d2016-01-04 10:42:43 +05301261 }
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301262
Siddartha Shaik343abc62017-08-08 11:15:25 +05301263 if(ctl)
1264 mixer_ctl_set_array(ctl, app_type_cfg, len);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001265 rc = 0;
1266exit_send_app_type_cfg:
1267 return rc;
1268}
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001269
Deeraj Soman14230922019-01-30 16:39:30 +05301270static int audio_extn_utils_check_input_parameters(uint32_t sample_rate,
1271 audio_format_t format,
1272 int channel_count)
1273{
1274 int ret = 0;
1275
1276 if (((format != AUDIO_FORMAT_PCM_16_BIT) && (format != AUDIO_FORMAT_PCM_8_24_BIT) &&
1277 (format != AUDIO_FORMAT_PCM_24_BIT_PACKED) && (format != AUDIO_FORMAT_PCM_32_BIT) &&
1278 (format != AUDIO_FORMAT_PCM_FLOAT)) &&
1279 !voice_extn_compress_voip_is_format_supported(format) &&
1280 !audio_extn_compr_cap_format_supported(format) &&
1281 !audio_extn_cin_format_supported(format))
1282 ret = -EINVAL;
1283
1284 switch (channel_count) {
1285 case 1:
1286 case 2:
1287 case 3:
1288 case 4:
1289 case 6:
1290 case 8:
1291 break;
1292 default:
1293 ret = -EINVAL;
1294 }
1295
1296 switch (sample_rate) {
1297 case 8000:
1298 case 11025:
1299 case 12000:
1300 case 16000:
1301 case 22050:
1302 case 24000:
1303 case 32000:
1304 case 44100:
1305 case 48000:
1306 case 88200:
1307 case 96000:
1308 case 176400:
1309 case 192000:
1310 break;
1311 default:
1312 ret = -EINVAL;
1313 }
1314
1315 return ret;
1316}
1317
1318static inline uint32_t audio_extn_utils_nearest_multiple(uint32_t num, uint32_t multiplier)
1319{
1320 uint32_t remainder = 0;
1321
1322 if (!multiplier)
1323 return num;
1324
1325 remainder = num % multiplier;
1326 if (remainder)
1327 num += (multiplier - remainder);
1328
1329 return num;
1330}
1331
1332static inline uint32_t audio_extn_utils_lcm(uint32_t num1, uint32_t num2)
1333{
1334 uint32_t high = num1, low = num2, temp = 0;
1335
1336 if (!num1 || !num2)
1337 return 0;
1338
1339 if (num1 < num2) {
1340 high = num2;
1341 low = num1;
1342 }
1343
1344 while (low != 0) {
1345 temp = low;
1346 low = high % low;
1347 high = temp;
1348 }
1349 return (num1 * num2)/high;
1350}
1351
Siena Richard7c2db772016-12-21 11:32:34 -08001352int audio_extn_utils_send_app_type_cfg(struct audio_device *adev,
1353 struct audio_usecase *usecase)
1354{
1355 int i, num_devices = 0;
1356 snd_device_t new_snd_devices[SND_DEVICE_OUT_END] = {0};
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301357 snd_device_t in_snd_device = usecase->in_snd_device;
Siena Richard7c2db772016-12-21 11:32:34 -08001358 int rc = 0;
1359
Aalique Grahame22e49102018-12-18 14:23:57 -08001360 if (usecase->type == PCM_HFP_CALL) {
1361 return audio_extn_utils_send_app_type_cfg_hfp(adev, usecase);
1362 }
1363
Siena Richard7c2db772016-12-21 11:32:34 -08001364 switch (usecase->type) {
1365 case PCM_PLAYBACK:
Surendar Karka93cd25a2018-08-28 14:21:37 +05301366 case TRANSCODE_LOOPBACK_RX:
Siena Richard7c2db772016-12-21 11:32:34 -08001367 ALOGD("%s: usecase->out_snd_device %s",
1368 __func__, platform_get_snd_device_name(usecase->out_snd_device));
1369 /* check for out combo device */
1370 if (platform_split_snd_device(adev->platform,
1371 usecase->out_snd_device,
1372 &num_devices, new_snd_devices)) {
1373 new_snd_devices[0] = usecase->out_snd_device;
1374 num_devices = 1;
1375 }
1376 break;
1377 case PCM_CAPTURE:
1378 ALOGD("%s: usecase->in_snd_device %s",
1379 __func__, platform_get_snd_device_name(usecase->in_snd_device));
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301380 if (voice_is_in_call_rec_stream(usecase->stream.in)) {
1381 in_snd_device = voice_get_incall_rec_backend_device(usecase->stream.in);
1382 }
Siena Richard7c2db772016-12-21 11:32:34 -08001383 /* check for in combo device */
1384 if (platform_split_snd_device(adev->platform,
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301385 in_snd_device,
Siena Richard7c2db772016-12-21 11:32:34 -08001386 &num_devices, new_snd_devices)) {
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301387 new_snd_devices[0] = in_snd_device;
Siena Richard7c2db772016-12-21 11:32:34 -08001388 num_devices = 1;
1389 }
1390 break;
1391 default:
1392 ALOGI("%s: not a playback/capture path, no need to cfg app type", __func__);
1393 rc = 0;
1394 break;
1395 }
1396
1397 for (i = 0; i < num_devices; i++) {
1398 rc = send_app_type_cfg_for_device(adev, usecase, new_snd_devices[i]);
1399 if (rc)
1400 break;
1401 }
1402
1403 return rc;
1404}
1405
Preetam Singh Ranawat9519e9c2015-11-18 16:05:55 +05301406int read_line_from_file(const char *path, char *buf, size_t count)
1407{
1408 char * fgets_ret;
1409 FILE * fd;
1410 int rv;
1411
1412 fd = fopen(path, "r");
1413 if (fd == NULL)
1414 return -1;
1415
1416 fgets_ret = fgets(buf, (int)count, fd);
1417 if (NULL != fgets_ret) {
1418 rv = (int)strlen(buf);
1419 } else {
1420 rv = ferror(fd);
1421 }
1422 fclose(fd);
1423
1424 return rv;
1425}
1426
Ashish Jainf1eaa582016-05-23 20:54:24 +05301427/*Translates ALSA formats to AOSP PCM formats*/
1428audio_format_t alsa_format_to_hal(uint32_t alsa_format)
1429{
1430 audio_format_t format;
1431
1432 switch(alsa_format) {
1433 case SNDRV_PCM_FORMAT_S16_LE:
1434 format = AUDIO_FORMAT_PCM_16_BIT;
1435 break;
1436 case SNDRV_PCM_FORMAT_S24_3LE:
1437 format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
1438 break;
1439 case SNDRV_PCM_FORMAT_S24_LE:
1440 format = AUDIO_FORMAT_PCM_8_24_BIT;
1441 break;
1442 case SNDRV_PCM_FORMAT_S32_LE:
1443 format = AUDIO_FORMAT_PCM_32_BIT;
1444 break;
1445 default:
1446 ALOGW("Incorrect ALSA format");
1447 format = AUDIO_FORMAT_INVALID;
1448 }
1449 return format;
1450}
1451
1452/*Translates hal format (AOSP) to alsa formats*/
1453uint32_t hal_format_to_alsa(audio_format_t hal_format)
1454{
1455 uint32_t alsa_format;
1456
1457 switch (hal_format) {
1458 case AUDIO_FORMAT_PCM_32_BIT: {
1459 if (platform_supports_true_32bit())
1460 alsa_format = SNDRV_PCM_FORMAT_S32_LE;
1461 else
1462 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1463 }
1464 break;
1465 case AUDIO_FORMAT_PCM_8_BIT:
1466 alsa_format = SNDRV_PCM_FORMAT_S8;
1467 break;
1468 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
1469 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1470 break;
1471 case AUDIO_FORMAT_PCM_8_24_BIT: {
1472 if (platform_supports_true_32bit())
1473 alsa_format = SNDRV_PCM_FORMAT_S32_LE;
1474 else
1475 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1476 }
1477 break;
1478 case AUDIO_FORMAT_PCM_FLOAT:
1479 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1480 break;
1481 default:
1482 case AUDIO_FORMAT_PCM_16_BIT:
1483 alsa_format = SNDRV_PCM_FORMAT_S16_LE;
1484 break;
1485 }
1486 return alsa_format;
1487}
1488
Ashish Jain83a6cc22016-06-28 14:34:17 +05301489/*Translates PCM formats to AOSP formats*/
1490audio_format_t pcm_format_to_hal(uint32_t pcm_format)
1491{
1492 audio_format_t format = AUDIO_FORMAT_INVALID;
1493
1494 switch(pcm_format) {
1495 case PCM_FORMAT_S16_LE:
1496 format = AUDIO_FORMAT_PCM_16_BIT;
1497 break;
1498 case PCM_FORMAT_S24_3LE:
1499 format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
1500 break;
1501 case PCM_FORMAT_S24_LE:
1502 format = AUDIO_FORMAT_PCM_8_24_BIT;
1503 break;
1504 case PCM_FORMAT_S32_LE:
1505 format = AUDIO_FORMAT_PCM_32_BIT;
1506 break;
1507 default:
1508 ALOGW("Incorrect PCM format");
1509 format = AUDIO_FORMAT_INVALID;
1510 }
1511 return format;
1512}
1513
1514/*Translates hal format (AOSP) to alsa formats*/
1515uint32_t hal_format_to_pcm(audio_format_t hal_format)
1516{
1517 uint32_t pcm_format;
1518
1519 switch (hal_format) {
1520 case AUDIO_FORMAT_PCM_32_BIT:
1521 case AUDIO_FORMAT_PCM_8_24_BIT:
1522 case AUDIO_FORMAT_PCM_FLOAT: {
1523 if (platform_supports_true_32bit())
1524 pcm_format = PCM_FORMAT_S32_LE;
1525 else
1526 pcm_format = PCM_FORMAT_S24_3LE;
1527 }
1528 break;
1529 case AUDIO_FORMAT_PCM_8_BIT:
1530 pcm_format = PCM_FORMAT_S8;
1531 break;
1532 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
1533 pcm_format = PCM_FORMAT_S24_3LE;
1534 break;
1535 default:
1536 case AUDIO_FORMAT_PCM_16_BIT:
1537 pcm_format = PCM_FORMAT_S16_LE;
1538 break;
1539 }
1540 return pcm_format;
1541}
1542
Ashish Jainf1eaa582016-05-23 20:54:24 +05301543uint32_t get_alsa_fragment_size(uint32_t bytes_per_sample,
1544 uint32_t sample_rate,
Deeraj Soman65358ab2019-02-07 15:40:49 +05301545 uint32_t noOfChannels,
1546 int64_t duration_ms)
Ashish Jainf1eaa582016-05-23 20:54:24 +05301547{
1548 uint32_t fragment_size = 0;
1549 uint32_t pcm_offload_time = PCM_OFFLOAD_BUFFER_DURATION;
1550
Deeraj Soman65358ab2019-02-07 15:40:49 +05301551 if (duration_ms >= MIN_OFFLOAD_BUFFER_DURATION_MS && duration_ms <= MAX_OFFLOAD_BUFFER_DURATION_MS)
1552 pcm_offload_time = duration_ms;
1553
Ashish Jainf1eaa582016-05-23 20:54:24 +05301554 fragment_size = (pcm_offload_time
1555 * sample_rate
1556 * bytes_per_sample
1557 * noOfChannels)/1000;
1558 if (fragment_size < MIN_PCM_OFFLOAD_FRAGMENT_SIZE)
1559 fragment_size = MIN_PCM_OFFLOAD_FRAGMENT_SIZE;
1560 else if (fragment_size > MAX_PCM_OFFLOAD_FRAGMENT_SIZE)
1561 fragment_size = MAX_PCM_OFFLOAD_FRAGMENT_SIZE;
1562 /*To have same PCM samples for all channels, the buffer size requires to
1563 *be multiple of (number of channels * bytes per sample)
1564 *For writes to succeed, the buffer must be written at address which is multiple of 32
1565 */
Aalique Grahameb85f2d92017-10-16 17:53:23 -07001566 fragment_size = ALIGN(fragment_size, (bytes_per_sample * noOfChannels * 32));
Ashish Jainf1eaa582016-05-23 20:54:24 +05301567
1568 ALOGI("PCM offload Fragment size to %d bytes", fragment_size);
1569 return fragment_size;
1570}
1571
1572/* Calculates the fragment size required to configure compress session.
1573 * Based on the alsa format selected, decide if conversion is needed in
1574
1575 * HAL ( e.g. convert AUDIO_FORMAT_PCM_FLOAT input format to
1576 * AUDIO_FORMAT_PCM_24_BIT_PACKED before writing to the compress driver.
1577 */
1578void audio_extn_utils_update_direct_pcm_fragment_size(struct stream_out *out)
1579{
Ashish Jain83a6cc22016-06-28 14:34:17 +05301580 audio_format_t dst_format = out->hal_op_format;
1581 audio_format_t src_format = out->hal_ip_format;
Ashish Jainf1eaa582016-05-23 20:54:24 +05301582 uint32_t hal_op_bytes_per_sample = audio_bytes_per_sample(dst_format);
1583 uint32_t hal_ip_bytes_per_sample = audio_bytes_per_sample(src_format);
1584
1585 out->compr_config.fragment_size =
1586 get_alsa_fragment_size(hal_op_bytes_per_sample,
1587 out->sample_rate,
Deeraj Soman65358ab2019-02-07 15:40:49 +05301588 popcount(out->channel_mask),
1589 out->info.duration_us / 1000);
Ashish Jainf1eaa582016-05-23 20:54:24 +05301590
1591 if ((src_format != dst_format) &&
1592 hal_op_bytes_per_sample != hal_ip_bytes_per_sample) {
1593
Ashish Jain83a6cc22016-06-28 14:34:17 +05301594 out->hal_fragment_size =
Ashish Jainf1eaa582016-05-23 20:54:24 +05301595 ((out->compr_config.fragment_size * hal_ip_bytes_per_sample) /
1596 hal_op_bytes_per_sample);
1597 ALOGI("enable conversion hal_input_fragment_size is %d src_format %x dst_format %x",
Ashish Jain83a6cc22016-06-28 14:34:17 +05301598 out->hal_fragment_size, src_format, dst_format);
Ashish Jainf1eaa582016-05-23 20:54:24 +05301599 } else {
Ashish Jain83a6cc22016-06-28 14:34:17 +05301600 out->hal_fragment_size = out->compr_config.fragment_size;
Ashish Jainf1eaa582016-05-23 20:54:24 +05301601 }
1602}
1603
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301604/* converts pcm format 24_8 to 8_24 inplace */
1605size_t audio_extn_utils_convert_format_24_8_to_8_24(void *buf, size_t bytes)
1606{
1607 size_t i = 0;
1608 int *int_buf_stream = buf;
1609
1610 if ((bytes % 4) != 0) {
1611 ALOGE("%s: wrong inout buffer! ... is not 32 bit aligned ", __func__);
1612 return -EINVAL;
1613 }
1614
1615 for (; i < (bytes / 4); i++)
1616 int_buf_stream[i] >>= 8;
1617
1618 return bytes;
1619}
1620
1621int get_snd_codec_id(audio_format_t format)
1622{
1623 int id = 0;
1624
1625 switch (format & AUDIO_FORMAT_MAIN_MASK) {
1626 case AUDIO_FORMAT_MP3:
1627 id = SND_AUDIOCODEC_MP3;
1628 break;
1629 case AUDIO_FORMAT_AAC:
1630 id = SND_AUDIOCODEC_AAC;
1631 break;
1632 case AUDIO_FORMAT_AAC_ADTS:
1633 id = SND_AUDIOCODEC_AAC;
1634 break;
Arun Kumar Dasari3b174182016-12-27 13:01:14 +05301635 case AUDIO_FORMAT_AAC_LATM:
1636 id = SND_AUDIOCODEC_AAC;
1637 break;
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301638 case AUDIO_FORMAT_PCM:
1639 id = SND_AUDIOCODEC_PCM;
1640 break;
1641 case AUDIO_FORMAT_FLAC:
1642 id = SND_AUDIOCODEC_FLAC;
1643 break;
1644 case AUDIO_FORMAT_ALAC:
1645 id = SND_AUDIOCODEC_ALAC;
1646 break;
1647 case AUDIO_FORMAT_APE:
1648 id = SND_AUDIOCODEC_APE;
1649 break;
1650 case AUDIO_FORMAT_VORBIS:
1651 id = SND_AUDIOCODEC_VORBIS;
1652 break;
1653 case AUDIO_FORMAT_WMA:
1654 id = SND_AUDIOCODEC_WMA;
1655 break;
1656 case AUDIO_FORMAT_WMA_PRO:
1657 id = SND_AUDIOCODEC_WMA_PRO;
1658 break;
Satish Babu Patakokila0c313922016-12-08 12:07:08 +05301659 case AUDIO_FORMAT_MP2:
1660 id = SND_AUDIOCODEC_MP2;
1661 break;
Satish Babu Patakokila915ecba2017-01-10 17:43:56 +05301662 case AUDIO_FORMAT_AC3:
1663 id = SND_AUDIOCODEC_AC3;
1664 break;
1665 case AUDIO_FORMAT_E_AC3:
1666 case AUDIO_FORMAT_E_AC3_JOC:
1667 id = SND_AUDIOCODEC_EAC3;
1668 break;
1669 case AUDIO_FORMAT_DTS:
1670 case AUDIO_FORMAT_DTS_HD:
1671 id = SND_AUDIOCODEC_DTS;
1672 break;
Ben Romberger1aaaf862017-04-06 17:49:46 -07001673 case AUDIO_FORMAT_DOLBY_TRUEHD:
1674 id = SND_AUDIOCODEC_TRUEHD;
1675 break;
Naresh Tanniru928f0862017-04-07 16:44:23 -07001676 case AUDIO_FORMAT_IEC61937:
1677 id = SND_AUDIOCODEC_IEC61937;
1678 break;
Preetam Singh Ranawat4277a5a2017-01-18 19:02:24 +05301679 case AUDIO_FORMAT_DSD:
1680 id = SND_AUDIOCODEC_DSD;
1681 break;
Dhanalakshmi Siddani18737932016-11-29 17:33:17 +05301682 case AUDIO_FORMAT_APTX:
1683 id = SND_AUDIOCODEC_APTX;
1684 break;
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301685 default:
1686 ALOGE("%s: Unsupported audio format :%x", __func__, format);
1687 }
1688
1689 return id;
1690}
1691
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001692void audio_extn_utils_send_audio_calibration(struct audio_device *adev,
1693 struct audio_usecase *usecase)
1694{
1695 int type = usecase->type;
1696
Dhananjay Kumar14245982017-01-16 20:21:00 +05301697 if (type == PCM_PLAYBACK && usecase->stream.out != NULL) {
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05301698 platform_send_audio_calibration(adev->platform, usecase,
Zhou Song06761dd2019-04-28 18:08:17 +08001699 usecase->stream.out->app_type_cfg.app_type);
Dhananjay Kumar14245982017-01-16 20:21:00 +05301700 } else if (type == PCM_CAPTURE && usecase->stream.in != NULL) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301701 platform_send_audio_calibration(adev->platform, usecase,
Zhou Song06761dd2019-04-28 18:08:17 +08001702 usecase->stream.in->app_type_cfg.app_type);
1703 } else if ((type == PCM_HFP_CALL) || (type == PCM_CAPTURE) ||
1704 (type == TRANSCODE_LOOPBACK_RX && usecase->stream.inout != NULL)) {
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05301705 platform_send_audio_calibration(adev->platform, usecase,
Zhou Song06761dd2019-04-28 18:08:17 +08001706 platform_get_default_app_type_v2(adev->platform, usecase->type));
Vidyakumar Athotae57f6002017-03-01 13:13:16 -08001707 } else {
1708 /* No need to send audio calibration for voice and voip call usecases */
1709 if ((type != VOICE_CALL) && (type != VOIP_CALL))
1710 ALOGW("%s: No audio calibration for usecase type = %d", __func__, type);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001711 }
1712}
1713
Ben Rombergera04fabc2014-11-14 12:16:03 -08001714// Base64 Encode and Decode
1715// Not all features supported. This must be used only with following conditions.
1716// Decode Modes: Support with and without padding
1717// CRLF not handling. So no CRLF in string to decode.
1718// Encode Modes: Supports only padding
1719int b64decode(char *inp, int ilen, uint8_t* outp)
1720{
1721 int i, j, k, ii, num;
1722 int rem, pcnt;
1723 uint32_t res=0;
1724 uint8_t getIndex[MAX_BASEINDEX_LEN];
1725 uint8_t tmp, cflag;
1726
1727 if(inp == NULL || outp == NULL || ilen <= 0) {
1728 ALOGE("[%s] received NULL pointer or zero length",__func__);
1729 return -1;
1730 }
1731
1732 memset(getIndex, MAX_BASEINDEX_LEN-1, sizeof(getIndex));
1733 for(i=0;i<BASE_TABLE_SIZE;i++) {
1734 getIndex[(uint8_t)bTable[i]] = (uint8_t)i;
1735 }
1736 getIndex[(uint8_t)'=']=0;
1737
1738 j=0;k=0;
1739 num = ilen/4;
1740 rem = ilen%4;
1741 if(rem==0)
1742 num = num-1;
1743 cflag=0;
1744 for(i=0; i<num; i++) {
1745 res=0;
1746 for(ii=0;ii<4;ii++) {
1747 res = res << 6;
1748 tmp = getIndex[(uint8_t)inp[j++]];
1749 res = res | tmp;
1750 cflag = cflag | tmp;
1751 }
1752 outp[k++] = (res >> 16)&0xFF;
1753 outp[k++] = (res >> 8)&0xFF;
1754 outp[k++] = res & 0xFF;
1755 }
1756
1757 // Handle last bytes special
1758 pcnt=0;
1759 if(rem == 0) {
1760 //With padding or full data
1761 res = 0;
1762 for(ii=0;ii<4;ii++) {
1763 if(inp[j] == '=')
1764 pcnt++;
1765 res = res << 6;
1766 tmp = getIndex[(uint8_t)inp[j++]];
1767 res = res | tmp;
1768 cflag = cflag | tmp;
1769 }
1770 outp[k++] = res >> 16;
1771 if(pcnt == 2)
1772 goto done;
1773 outp[k++] = (res>>8)&0xFF;
1774 if(pcnt == 1)
1775 goto done;
1776 outp[k++] = res&0xFF;
1777 } else {
1778 //without padding
1779 res = 0;
1780 for(i=0;i<rem;i++) {
1781 res = res << 6;
1782 tmp = getIndex[(uint8_t)inp[j++]];
1783 res = res | tmp;
1784 cflag = cflag | tmp;
1785 }
1786 for(i=rem;i<4;i++) {
1787 res = res << 6;
1788 pcnt++;
1789 }
1790 outp[k++] = res >> 16;
1791 if(pcnt == 2)
1792 goto done;
1793 outp[k++] = (res>>8)&0xFF;
1794 if(pcnt == 1)
1795 goto done;
1796 outp[k++] = res&0xFF;
1797 }
1798done:
1799 if(cflag == 0xFF) {
1800 ALOGE("[%s] base64 decode failed. Invalid character found %s",
1801 __func__, inp);
1802 return 0;
1803 }
1804 return k;
1805}
1806
1807int b64encode(uint8_t *inp, int ilen, char* outp)
1808{
1809 int i,j,k, num;
1810 int rem=0;
1811 uint32_t res=0;
1812
1813 if(inp == NULL || outp == NULL || ilen<=0) {
1814 ALOGE("[%s] received NULL pointer or zero input length",__func__);
1815 return -1;
1816 }
1817
1818 num = ilen/3;
1819 rem = ilen%3;
1820 j=0;k=0;
1821 for(i=0; i<num; i++) {
1822 //prepare index
1823 res = inp[j++]<<16;
1824 res = res | inp[j++]<<8;
1825 res = res | inp[j++];
1826 //get output map from index
1827 outp[k++] = (char) bTable[(res>>18)&0x3F];
1828 outp[k++] = (char) bTable[(res>>12)&0x3F];
1829 outp[k++] = (char) bTable[(res>>6)&0x3F];
1830 outp[k++] = (char) bTable[res&0x3F];
1831 }
1832
1833 switch(rem) {
1834 case 1:
1835 res = inp[j++]<<16;
1836 outp[k++] = (char) bTable[res>>18];
1837 outp[k++] = (char) bTable[(res>>12)&0x3F];
1838 //outp[k++] = '=';
1839 //outp[k++] = '=';
1840 break;
1841 case 2:
1842 res = inp[j++]<<16;
1843 res = res | inp[j++]<<8;
1844 outp[k++] = (char) bTable[res>>18];
1845 outp[k++] = (char) bTable[(res>>12)&0x3F];
1846 outp[k++] = (char) bTable[(res>>6)&0x3F];
1847 //outp[k++] = '=';
1848 break;
1849 default:
1850 break;
1851 }
Ben Rombergera04fabc2014-11-14 12:16:03 -08001852 outp[k] = '\0';
1853 return k;
1854}
Ashish Jain81eb2a82015-05-13 10:52:34 +05301855
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301856
1857int audio_extn_utils_get_codec_version(const char *snd_card_name,
1858 int card_num,
1859 char *codec_version)
1860{
1861 char procfs_path[50];
1862 FILE *fp;
1863
1864 if (strstr(snd_card_name, "tasha")) {
1865 snprintf(procfs_path, sizeof(procfs_path),
1866 "/proc/asound/card%d/codecs/tasha/version", card_num);
1867 if ((fp = fopen(procfs_path, "r")) != NULL) {
1868 fgets(codec_version, CODEC_VERSION_MAX_LENGTH, fp);
1869 fclose(fp);
1870 } else {
1871 ALOGE("%s: ERROR. cannot open %s", __func__, procfs_path);
1872 return -ENOENT;
1873 }
1874 ALOGD("%s: codec version %s", __func__, codec_version);
1875 }
1876
1877 return 0;
1878}
1879
Preetam Singh Ranawat9d0d8e42019-07-15 12:27:25 +05301880int audio_extn_utils_get_codec_variant(int card_num,
1881 char *codec_variant)
1882{
1883 char procfs_path[50];
1884 FILE *fp;
1885 snprintf(procfs_path, sizeof(procfs_path),
1886 "/proc/asound/card%d/codecs/wcd938x/variant", card_num);
1887 if ((fp = fopen(procfs_path, "r")) == NULL) {
1888 snprintf(procfs_path, sizeof(procfs_path),
1889 "/proc/asound/card%d/codecs/wcd937x/variant", card_num);
1890 if ((fp = fopen(procfs_path, "r")) == NULL) {
1891 ALOGE("%s: ERROR. cannot open %s", __func__, procfs_path);
1892 return -ENOENT;
1893 }
1894 }
1895 fgets(codec_variant, CODEC_VARIANT_MAX_LENGTH, fp);
1896 fclose(fp);
1897 ALOGD("%s: codec variant is %s", __func__, codec_variant);
1898 return 0;
1899}
1900
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301901
Ashish Jain81eb2a82015-05-13 10:52:34 +05301902#ifdef AUDIO_EXTERNAL_HDMI_ENABLED
1903
1904void get_default_compressed_channel_status(
1905 unsigned char *channel_status)
1906{
Ashish Jain81eb2a82015-05-13 10:52:34 +05301907 memset(channel_status,0,24);
1908
1909 /* block start bit in preamble bit 3 */
1910 channel_status[0] |= PROFESSIONAL;
1911 //compre out
1912 channel_status[0] |= NON_LPCM;
1913 // sample rate; fixed 48K for default/transcode
1914 channel_status[3] |= SR_48000;
1915}
1916
Ashish Jain81eb2a82015-05-13 10:52:34 +05301917int32_t get_compressed_channel_status(void *audio_stream_data,
1918 uint32_t audio_frame_size,
1919 unsigned char *channel_status,
1920 enum audio_parser_code_type codec_type)
1921 // codec_type - AUDIO_PARSER_CODEC_AC3
1922 // - AUDIO_PARSER_CODEC_DTS
1923{
1924 unsigned char *stream;
1925 int ret = 0;
1926 stream = (unsigned char *)audio_stream_data;
1927
1928 if (audio_stream_data == NULL || audio_frame_size == 0) {
1929 ALOGW("no buffer to get channel status, return default for compress");
1930 get_default_compressed_channel_status(channel_status);
1931 return ret;
1932 }
1933
1934 memset(channel_status,0,24);
1935 if(init_audio_parser(stream, audio_frame_size, codec_type) == -1)
1936 {
1937 ALOGE("init audio parser failed");
1938 return -1;
1939 }
1940 ret = get_channel_status(channel_status, codec_type);
1941 return ret;
1942
1943}
1944
Ashish Jain81eb2a82015-05-13 10:52:34 +05301945void get_lpcm_channel_status(uint32_t sampleRate,
1946 unsigned char *channel_status)
1947{
1948 int32_t status = 0;
Ashish Jain81eb2a82015-05-13 10:52:34 +05301949 memset(channel_status,0,24);
1950 /* block start bit in preamble bit 3 */
1951 channel_status[0] |= PROFESSIONAL;
1952 //LPCM OUT
1953 channel_status[0] &= ~NON_LPCM;
1954
1955 switch (sampleRate) {
1956 case 8000:
1957 case 11025:
1958 case 12000:
1959 case 16000:
1960 case 22050:
1961 channel_status[3] |= SR_NOTID;
Preetam Singh Ranawat61716b12015-12-14 11:55:24 +05301962 break;
Ashish Jain81eb2a82015-05-13 10:52:34 +05301963 case 24000:
1964 channel_status[3] |= SR_24000;
1965 break;
1966 case 32000:
1967 channel_status[3] |= SR_32000;
1968 break;
1969 case 44100:
1970 channel_status[3] |= SR_44100;
1971 break;
1972 case 48000:
1973 channel_status[3] |= SR_48000;
1974 break;
1975 case 88200:
1976 channel_status[3] |= SR_88200;
1977 break;
1978 case 96000:
1979 channel_status[3] |= SR_96000;
1980 break;
1981 case 176400:
1982 channel_status[3] |= SR_176400;
1983 break;
1984 case 192000:
1985 channel_status[3] |= SR_192000;
1986 break;
1987 default:
1988 ALOGV("Invalid sample_rate %u\n", sampleRate);
1989 status = -1;
1990 break;
1991 }
1992}
1993
1994void audio_utils_set_hdmi_channel_status(struct stream_out *out, char * buffer, size_t bytes)
1995{
1996 unsigned char channel_status[24]={0};
1997 struct snd_aes_iec958 iec958;
1998 const char *mixer_ctl_name = "IEC958 Playback PCM Stream";
1999 struct mixer_ctl *ctl;
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +05302000 ALOGV("%s: buffer %s bytes %zd", __func__, buffer, bytes);
Arun Mirpurie008ed22019-03-21 11:21:04 -07002001
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05302002 if (audio_extn_utils_is_dolby_format(out->format) &&
Ashish Jain81eb2a82015-05-13 10:52:34 +05302003 /*TODO:Extend code to support DTS passthrough*/
2004 /*set compressed channel status bits*/
Arun Mirpurie008ed22019-03-21 11:21:04 -07002005 audio_extn_passthru_is_passthrough_stream(out) &&
2006 audio_extn_is_hdmi_passthru_enabled()) {
Ashish Jain81eb2a82015-05-13 10:52:34 +05302007 get_compressed_channel_status(buffer, bytes, channel_status, AUDIO_PARSER_CODEC_AC3);
Arun Mirpurie008ed22019-03-21 11:21:04 -07002008 } else {
Ashish Jain81eb2a82015-05-13 10:52:34 +05302009 /*set channel status bit for LPCM*/
2010 get_lpcm_channel_status(out->sample_rate, channel_status);
2011 }
2012
2013 memcpy(iec958.status, channel_status,sizeof(iec958.status));
2014 ctl = mixer_get_ctl_by_name(out->dev->mixer, mixer_ctl_name);
2015 if (!ctl) {
2016 ALOGE("%s: Could not get ctl for mixer cmd - %s",
2017 __func__, mixer_ctl_name);
2018 return;
2019 }
2020 if (mixer_ctl_set_array(ctl, &iec958, sizeof(iec958)) < 0) {
2021 ALOGE("%s: Could not set channel status for ext HDMI ",
2022 __func__);
2023 return;
2024 }
2025
2026}
2027#endif
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302028
2029int audio_extn_utils_get_avt_device_drift(
2030 struct audio_usecase *usecase,
2031 struct audio_avt_device_drift_param *drift_param)
2032{
2033 int ret = 0, count = 0;
2034 char avt_device_drift_mixer_ctl_name[MIXER_PATH_MAX_LENGTH] = {0};
Naresh Tanniru6160c712017-04-17 15:43:48 +05302035 const char *backend = NULL;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302036 struct mixer_ctl *ctl = NULL;
2037 struct audio_avt_device_drift_stats drift_stats;
2038 struct audio_device *adev = NULL;
2039
2040 if (usecase != NULL && usecase->type == PCM_PLAYBACK) {
Naresh Tanniru6160c712017-04-17 15:43:48 +05302041 backend = platform_get_snd_device_backend_interface(usecase->out_snd_device);
2042 if (!backend) {
2043 ALOGE("%s: Unsupported device %d", __func__,
2044 usecase->stream.out->devices);
2045 ret = -EINVAL;
2046 goto done;
2047 }
2048 strlcpy(avt_device_drift_mixer_ctl_name,
2049 backend,
2050 MIXER_PATH_MAX_LENGTH);
2051
2052 count = strlen(backend);
2053 if (MIXER_PATH_MAX_LENGTH - count > 0) {
2054 strlcat(&avt_device_drift_mixer_ctl_name[count],
2055 " DRIFT",
2056 MIXER_PATH_MAX_LENGTH - count);
2057 } else {
2058 ret = -EINVAL;
2059 goto done;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302060 }
2061 } else {
Naresh Tanniru7586e292017-04-13 10:38:13 +05302062 ALOGE("%s: Invalid usecase",__func__);
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302063 ret = -EINVAL;
Naresh Tanniru6160c712017-04-17 15:43:48 +05302064 goto done;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302065 }
2066
Naresh Tanniru6160c712017-04-17 15:43:48 +05302067 adev = usecase->stream.out->dev;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302068 ctl = mixer_get_ctl_by_name(adev->mixer, avt_device_drift_mixer_ctl_name);
2069 if (!ctl) {
2070 ALOGE("%s: Could not get ctl for mixer cmd - %s",
2071 __func__, avt_device_drift_mixer_ctl_name);
2072
2073 ret = -EINVAL;
2074 goto done;
2075 }
2076
2077 ALOGV("%s: Getting AV Timer vs Device Drift mixer ctrl name %s", __func__,
2078 avt_device_drift_mixer_ctl_name);
2079
2080 mixer_ctl_update(ctl);
2081 count = mixer_ctl_get_num_values(ctl);
2082 if (count != sizeof(struct audio_avt_device_drift_stats)) {
2083 ALOGE("%s: mixer_ctl_get_num_values() invalid drift_stats data size",
2084 __func__);
2085
2086 ret = -EINVAL;
2087 goto done;
2088 }
2089
2090 ret = mixer_ctl_get_array(ctl, (void *)&drift_stats, count);
2091 if (ret != 0) {
2092 ALOGE("%s: mixer_ctl_get_array() failed to get drift_stats Params",
2093 __func__);
2094
2095 ret = -EINVAL;
2096 goto done;
2097 }
2098 memcpy(drift_param, &drift_stats.drift_param,
2099 sizeof(struct audio_avt_device_drift_param));
2100done:
2101 return ret;
2102}
Manish Dewangan07de2142017-02-27 19:27:20 +05302103
2104#ifdef SNDRV_COMPRESS_PATH_DELAY
2105int audio_extn_utils_compress_get_dsp_latency(struct stream_out *out)
2106{
2107 int ret = -EINVAL;
2108 struct snd_compr_metadata metadata;
2109 int delay_ms = COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
2110
Weiyin Jiangc49a3b52018-08-17 16:16:08 +08002111 /* override the latency for pcm offload use case */
2112 if ((out->flags & AUDIO_OUTPUT_FLAG_DIRECT) &&
2113 !(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)) {
2114 delay_ms = PCM_OFFLOAD_PLAYBACK_LATENCY;
2115 }
2116
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -07002117 if (property_get_bool("vendor.audio.playback.dsp.pathdelay", false)) {
Manish Dewangan07de2142017-02-27 19:27:20 +05302118 ALOGD("%s:: Quering DSP delay %d",__func__, __LINE__);
2119 if (!(is_offload_usecase(out->usecase))) {
2120 ALOGE("%s:: not supported for non offload session", __func__);
2121 goto exit;
2122 }
2123
2124 if (!out->compr) {
2125 ALOGD("%s:: Invalid compress handle,returning default dsp latency",
2126 __func__);
2127 goto exit;
2128 }
2129
2130 metadata.key = SNDRV_COMPRESS_PATH_DELAY;
2131 ret = compress_get_metadata(out->compr, &metadata);
2132 if(ret) {
2133 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2134 goto exit;
2135 }
2136 delay_ms = metadata.value[0] / 1000; /*convert to ms*/
2137 } else {
2138 ALOGD("%s:: Using Fix DSP delay",__func__);
2139 }
2140
2141exit:
2142 ALOGD("%s:: delay in ms is %d",__func__, delay_ms);
2143 return delay_ms;
2144}
2145#else
2146int audio_extn_utils_compress_get_dsp_latency(struct stream_out *out __unused)
2147{
2148 return COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
2149}
2150#endif
Manish Dewangan69426c82017-01-30 17:35:36 +05302151
2152#ifdef SNDRV_COMPRESS_RENDER_MODE
2153int audio_extn_utils_compress_set_render_mode(struct stream_out *out)
2154{
2155 struct snd_compr_metadata metadata;
2156 int ret = -EINVAL;
2157
2158 if (!(is_offload_usecase(out->usecase))) {
2159 ALOGE("%s:: not supported for non offload session", __func__);
2160 goto exit;
2161 }
2162
2163 if (!out->compr) {
2164 ALOGD("%s:: Invalid compress handle",
2165 __func__);
2166 goto exit;
2167 }
2168
2169 ALOGD("%s:: render mode %d", __func__, out->render_mode);
2170
2171 metadata.key = SNDRV_COMPRESS_RENDER_MODE;
2172 if (out->render_mode == RENDER_MODE_AUDIO_MASTER) {
2173 metadata.value[0] = SNDRV_COMPRESS_RENDER_MODE_AUDIO_MASTER;
2174 } else if (out->render_mode == RENDER_MODE_AUDIO_STC_MASTER) {
2175 metadata.value[0] = SNDRV_COMPRESS_RENDER_MODE_STC_MASTER;
2176 } else {
2177 ret = 0;
2178 goto exit;
2179 }
2180 ret = compress_set_metadata(out->compr, &metadata);
2181 if(ret) {
2182 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2183 }
2184exit:
2185 return ret;
2186}
2187#else
2188int audio_extn_utils_compress_set_render_mode(struct stream_out *out __unused)
2189{
2190 ALOGD("%s:: configuring render mode not supported", __func__);
2191 return 0;
2192}
2193#endif
Manish Dewangan58229382017-02-02 15:48:41 +05302194
2195#ifdef SNDRV_COMPRESS_CLK_REC_MODE
2196int audio_extn_utils_compress_set_clk_rec_mode(
2197 struct audio_usecase *usecase)
2198{
2199 struct snd_compr_metadata metadata;
2200 struct stream_out *out = NULL;
2201 int ret = -EINVAL;
2202
Naresh Tanniru7586e292017-04-13 10:38:13 +05302203 if (usecase == NULL || usecase->type != PCM_PLAYBACK) {
Manish Dewangan58229382017-02-02 15:48:41 +05302204 ALOGE("%s:: Invalid use case", __func__);
2205 goto exit;
2206 }
2207
2208 out = usecase->stream.out;
2209 if (!out) {
2210 ALOGE("%s:: invalid stream", __func__);
2211 goto exit;
2212 }
2213
2214 if (!is_offload_usecase(out->usecase)) {
2215 ALOGE("%s:: not supported for non offload session", __func__);
2216 goto exit;
2217 }
2218
2219 if (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER) {
2220 ALOGD("%s:: clk recovery is only supported in STC render mode",
2221 __func__);
2222 ret = 0;
2223 goto exit;
2224 }
2225
2226 if (!out->compr) {
2227 ALOGD("%s:: Invalid compress handle",
2228 __func__);
2229 goto exit;
2230 }
2231 metadata.key = SNDRV_COMPRESS_CLK_REC_MODE;
2232 switch(usecase->out_snd_device) {
2233 case SND_DEVICE_OUT_HDMI:
2234 case SND_DEVICE_OUT_SPEAKER_AND_HDMI:
2235 case SND_DEVICE_OUT_DISPLAY_PORT:
2236 case SND_DEVICE_OUT_SPEAKER_AND_DISPLAY_PORT:
2237 metadata.value[0] = SNDRV_COMPRESS_CLK_REC_MODE_NONE;
2238 break;
2239 default:
2240 metadata.value[0] = SNDRV_COMPRESS_CLK_REC_MODE_AUTO;
2241 break;
2242 }
2243
2244 ALOGD("%s:: clk recovery mode %d",__func__, metadata.value[0]);
2245
2246 ret = compress_set_metadata(out->compr, &metadata);
2247 if(ret) {
2248 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2249 }
2250
2251exit:
2252 return ret;
2253}
2254#else
2255int audio_extn_utils_compress_set_clk_rec_mode(
2256 struct audio_usecase *usecase __unused)
2257{
2258 ALOGD("%s:: configuring render mode not supported", __func__);
2259 return 0;
2260}
2261#endif
Manish Dewangan27346042017-03-01 12:56:12 +05302262
2263#ifdef SNDRV_COMPRESS_RENDER_WINDOW
2264int audio_extn_utils_compress_set_render_window(
2265 struct stream_out *out,
2266 struct audio_out_render_window_param *render_window)
2267{
2268 struct snd_compr_metadata metadata;
2269 int ret = -EINVAL;
2270
Manish Dewangan27346042017-03-01 12:56:12 +05302271 if(render_window == NULL) {
2272 ALOGE("%s:: Invalid render_window", __func__);
2273 goto exit;
2274 }
2275
Aniket Kumar Lata1e1d3662017-06-01 18:45:48 -07002276 ALOGD("%s:: render window start 0x%"PRIx64" end 0x%"PRIx64"",
2277 __func__,render_window->render_ws, render_window->render_we);
2278
Manish Dewangan27346042017-03-01 12:56:12 +05302279 if (!is_offload_usecase(out->usecase)) {
2280 ALOGE("%s:: not supported for non offload session", __func__);
2281 goto exit;
2282 }
2283
Naresh Tanniru6160c712017-04-17 15:43:48 +05302284 if ((out->render_mode != RENDER_MODE_AUDIO_MASTER) &&
2285 (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER)) {
Manish Dewangan27346042017-03-01 12:56:12 +05302286 ALOGD("%s:: only supported in timestamp mode, current "
2287 "render mode mode %d", __func__, out->render_mode);
2288 goto exit;
2289 }
2290
2291 if (!out->compr) {
2292 ALOGW("%s:: offload session not yet opened,"
2293 "render window will be configure later", __func__);
2294 /* store render window to reconfigure in start_output_stream() */
2295 goto exit;
2296 }
2297
2298 metadata.key = SNDRV_COMPRESS_RENDER_WINDOW;
2299 /*render window start value */
2300 metadata.value[0] = 0xFFFFFFFF & render_window->render_ws; /* lsb */
2301 metadata.value[1] = \
2302 (0xFFFFFFFF00000000 & render_window->render_ws) >> 32; /* msb*/
2303 /*render window end value */
2304 metadata.value[2] = 0xFFFFFFFF & render_window->render_we; /* lsb */
2305 metadata.value[3] = \
2306 (0xFFFFFFFF00000000 & render_window->render_we) >> 32; /* msb*/
2307
2308 ret = compress_set_metadata(out->compr, &metadata);
2309 if(ret) {
2310 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2311 }
2312
2313exit:
2314 return ret;
2315}
2316#else
2317int audio_extn_utils_compress_set_render_window(
2318 struct stream_out *out __unused,
2319 struct audio_out_render_window_param *render_window __unused)
2320{
2321 ALOGD("%s:: configuring render window not supported", __func__);
2322 return 0;
2323}
2324#endif
Manish Dewangan14956cc2017-02-14 18:54:42 +05302325
2326#ifdef SNDRV_COMPRESS_START_DELAY
2327int audio_extn_utils_compress_set_start_delay(
2328 struct stream_out *out,
2329 struct audio_out_start_delay_param *delay_param)
2330{
2331 struct snd_compr_metadata metadata;
2332 int ret = -EINVAL;
2333
2334 if(delay_param == NULL) {
2335 ALOGE("%s:: Invalid delay_param", __func__);
2336 goto exit;
2337 }
2338
2339 ALOGD("%s:: render start delay 0x%"PRIx64" ", __func__,
2340 delay_param->start_delay);
2341
2342 if (!is_offload_usecase(out->usecase)) {
2343 ALOGE("%s:: not supported for non offload session", __func__);
2344 goto exit;
2345 }
2346
Naresh Tanniru6160c712017-04-17 15:43:48 +05302347 if ((out->render_mode != RENDER_MODE_AUDIO_MASTER) &&
2348 (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER)) {
Manish Dewangan14956cc2017-02-14 18:54:42 +05302349 ALOGD("%s:: only supported in timestamp mode, current "
2350 "render mode mode %d", __func__, out->render_mode);
2351 goto exit;
2352 }
2353
2354 if (!out->compr) {
2355 ALOGW("%s:: offload session not yet opened,"
2356 "start delay will be configure later", __func__);
2357 goto exit;
2358 }
2359
2360 metadata.key = SNDRV_COMPRESS_START_DELAY;
2361 metadata.value[0] = 0xFFFFFFFF & delay_param->start_delay; /* lsb */
2362 metadata.value[1] = \
2363 (0xFFFFFFFF00000000 & delay_param->start_delay) >> 32; /* msb*/
2364
2365 ret = compress_set_metadata(out->compr, &metadata);
2366 if(ret) {
2367 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2368 }
2369
2370exit:
2371 return ret;
2372}
2373#else
2374int audio_extn_utils_compress_set_start_delay(
2375 struct stream_out *out __unused,
2376 struct audio_out_start_delay_param *delay_param __unused)
2377{
2378 ALOGD("%s:: configuring render window not supported", __func__);
2379 return 0;
2380}
2381#endif
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002382
Surendar Karka287348c2019-04-10 18:31:46 +05302383#ifdef SNDRV_COMPRESS_DSP_POSITION
2384int audio_extn_utils_compress_get_dsp_presentation_pos(struct stream_out *out,
2385 uint64_t *frames, struct timespec *timestamp, int32_t clock_id)
2386{
2387 int ret = -EINVAL;
2388 uint64_t *val = NULL;
2389 uint64_t time = 0;
2390 struct snd_compr_metadata metadata;
2391
2392 ALOGV("%s:: Quering DSP position with clock id %d",__func__, clock_id);
2393 metadata.key = SNDRV_COMPRESS_DSP_POSITION;
2394 metadata.value[0] = clock_id;
2395 ret = compress_get_metadata(out->compr, &metadata);
2396 if (ret) {
2397 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2398 ret = -errno;
2399 goto exit;
2400 }
2401 val = (uint64_t *)&metadata.value[1];
2402 *frames = *val;
2403 time = *(val + 1);
2404 timestamp->tv_sec = time / 1000000;
2405 timestamp->tv_nsec = (time % 1000000)*1000;
2406
2407exit:
2408 return ret;
2409}
2410#else
2411int audio_extn_utils_compress_get_dsp_presentation_pos(struct stream_out *out __unused,
2412 uint64_t *frames __unused, struct timespec *timestamp __unused,
2413 int32_t clock_id __unused)
2414{
2415 ALOGD("%s:: dsp presentation position not supported", __func__);
2416 return 0;
2417
2418}
2419#endif
2420
2421#ifdef SNDRV_PCM_IOCTL_DSP_POSITION
2422int audio_extn_utils_pcm_get_dsp_presentation_pos(struct stream_out *out,
2423 uint64_t *frames, struct timespec *timestamp, int32_t clock_id)
2424{
2425 int ret = -EINVAL;
2426 uint64_t time = 0;
2427 struct snd_pcm_prsnt_position prsnt_position;
2428
2429 ALOGV("%s:: Quering DSP position with clock id %d",__func__, clock_id);
2430 prsnt_position.clock_id = clock_id;
2431 ret = pcm_ioctl(out->pcm, SNDRV_PCM_IOCTL_DSP_POSITION, &prsnt_position);
2432 if (ret) {
2433 ALOGE("%s::error %d", __func__, ret);
2434 ret = -EIO;
2435 goto exit;
2436 }
2437
2438 *frames = prsnt_position.frames;
2439 time = prsnt_position.timestamp;
2440 timestamp->tv_sec = time / 1000000;
2441 timestamp->tv_nsec = (time % 1000000)*1000;
2442
2443exit:
2444 return ret;
2445}
2446#else
2447int audio_extn_utils_pcm_get_dsp_presentation_pos(struct stream_out *out __unused,
2448 uint64_t *frames __unused, struct timespec *timestamp __unused,
2449 int32_t clock_id __unused)
2450{
2451 ALOGD("%s:: dsp presentation position not supported", __func__);
2452 return 0;
2453
2454}
2455#endif
2456
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002457#define MAX_SND_CARD 8
Ashish Jain30ae8942017-11-05 17:21:23 +05302458#define RETRY_US 1000000
2459#define RETRY_NUMBER 40
Aalique Grahame22e49102018-12-18 14:23:57 -08002460#define PLATFORM_INFO_XML_PATH "audio_platform_info.xml"
2461#define PLATFORM_INFO_XML_BASE_STRING "audio_platform_info"
2462
2463#ifdef LINUX_ENABLED
2464static const char *kConfigLocationList[] =
2465 {"/etc"};
2466#else
2467static const char *kConfigLocationList[] =
2468 {"/vendor/etc"};
2469#endif
2470static const int kConfigLocationListSize =
2471 (sizeof(kConfigLocationList) / sizeof(kConfigLocationList[0]));
2472
2473bool audio_extn_utils_resolve_config_file(char file_name[MIXER_PATH_MAX_LENGTH])
2474{
2475 char full_config_path[MIXER_PATH_MAX_LENGTH];
2476 for (int i = 0; i < kConfigLocationListSize; i++) {
2477 snprintf(full_config_path,
2478 MIXER_PATH_MAX_LENGTH,
2479 "%s/%s",
2480 kConfigLocationList[i],
2481 file_name);
2482 if (F_OK == access(full_config_path, 0)) {
Weiyin Jiang2995f662019-04-17 14:25:12 +08002483 strlcpy(file_name, full_config_path, MIXER_PATH_MAX_LENGTH);
Aalique Grahame22e49102018-12-18 14:23:57 -08002484 return true;
2485 }
2486 }
2487 return false;
2488}
2489
2490/* platform_info_file should be size 'MIXER_PATH_MAX_LENGTH' */
2491int audio_extn_utils_get_platform_info(const char* snd_card_name, char* platform_info_file)
2492{
2493 if (NULL == snd_card_name) {
2494 return -1;
2495 }
2496
2497 struct snd_card_split *snd_split_handle = NULL;
2498 int ret = 0;
2499 audio_extn_set_snd_card_split(snd_card_name);
2500 snd_split_handle = audio_extn_get_snd_card_split();
2501
2502 snprintf(platform_info_file, MIXER_PATH_MAX_LENGTH, "%s_%s_%s.xml",
2503 PLATFORM_INFO_XML_BASE_STRING, snd_split_handle->snd_card,
2504 snd_split_handle->form_factor);
2505
2506 if (!audio_extn_utils_resolve_config_file(platform_info_file)) {
2507 memset(platform_info_file, 0, MIXER_PATH_MAX_LENGTH);
2508 snprintf(platform_info_file, MIXER_PATH_MAX_LENGTH, "%s_%s.xml",
2509 PLATFORM_INFO_XML_BASE_STRING, snd_split_handle->snd_card);
2510
2511 if (!audio_extn_utils_resolve_config_file(platform_info_file)) {
2512 memset(platform_info_file, 0, MIXER_PATH_MAX_LENGTH);
2513 strlcpy(platform_info_file, PLATFORM_INFO_XML_PATH, MIXER_PATH_MAX_LENGTH);
2514 ret = audio_extn_utils_resolve_config_file(platform_info_file) ? 0 : -1;
2515 }
2516 }
2517
2518 return ret;
2519}
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002520
2521int audio_extn_utils_get_snd_card_num()
2522{
Soumya Managoli9fee7c62018-04-06 16:21:50 +05302523 int snd_card_num = 0;
2524 struct mixer *mixer = NULL;
2525
2526 snd_card_num = audio_extn_utils_open_snd_mixer(&mixer);
2527 if (mixer)
2528 mixer_close(mixer);
2529 return snd_card_num;
2530}
2531
2532int audio_extn_utils_open_snd_mixer(struct mixer **mixer_handle)
2533{
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002534
2535 void *hw_info = NULL;
2536 struct mixer *mixer = NULL;
2537 int retry_num = 0;
mpangfaa7bae2019-01-15 16:38:13 +08002538 int snd_card_num = 0;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002539 char* snd_card_name = NULL;
mpangfaa7bae2019-01-15 16:38:13 +08002540 int snd_card_detection_info[MAX_SND_CARD] = {0};
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002541
Soumya Managoli9fee7c62018-04-06 16:21:50 +05302542 if (!mixer_handle) {
2543 ALOGE("invalid mixer handle");
2544 return -1;
2545 }
2546 *mixer_handle = NULL;
Ashish Jain30ae8942017-11-05 17:21:23 +05302547 /*
mpangfaa7bae2019-01-15 16:38:13 +08002548 * Try with all the sound cards ( 0 to 7 ) and if none of them were detected
Ashish Jain30ae8942017-11-05 17:21:23 +05302549 * sleep for 1 sec and try detections with sound card 0 again.
2550 * If sound card gets detected, check if it is relevant, if not check with the
2551 * other sound cards. To ensure that the irrelevant sound card is not check again,
2552 * we maintain it in min_snd_card_num.
2553 */
2554 while (retry_num < RETRY_NUMBER) {
mpangfaa7bae2019-01-15 16:38:13 +08002555 snd_card_num = 0;
2556 while (snd_card_num < MAX_SND_CARD) {
2557 if (snd_card_detection_info[snd_card_num] == 0) {
2558 mixer = mixer_open(snd_card_num);
2559 if (!mixer)
2560 snd_card_num++;
2561 else
2562 break;
2563 } else
2564 snd_card_num++;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002565 }
2566
2567 if (!mixer) {
Ashish Jain30ae8942017-11-05 17:21:23 +05302568 usleep(RETRY_US);
Ashish Jain30ae8942017-11-05 17:21:23 +05302569 retry_num++;
mpangfaa7bae2019-01-15 16:38:13 +08002570 ALOGD("%s: retry, retry_num %d", __func__, retry_num);
Ashish Jain30ae8942017-11-05 17:21:23 +05302571 continue;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002572 }
2573
2574 snd_card_name = strdup(mixer_get_name(mixer));
2575 if (!snd_card_name) {
2576 ALOGE("failed to allocate memory for snd_card_name\n");
2577 mixer_close(mixer);
2578 return -1;
2579 }
2580 ALOGD("%s: snd_card_name: %s", __func__, snd_card_name);
mpangfaa7bae2019-01-15 16:38:13 +08002581 snd_card_detection_info[snd_card_num] = 1;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002582 hw_info = hw_info_init(snd_card_name);
2583 if (hw_info) {
2584 ALOGD("%s: Opened sound card:%d", __func__, snd_card_num);
2585 break;
2586 }
mpangfaa7bae2019-01-15 16:38:13 +08002587 ALOGE("%s: Failed to init hardware info, snd_card_num:%d", __func__, snd_card_num);
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002588
Dhananjay Kumara7e27832017-07-11 15:40:39 +05302589 free(snd_card_name);
2590 snd_card_name = NULL;
2591
2592 mixer_close(mixer);
2593 mixer = NULL;
2594 }
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002595 if (snd_card_name)
2596 free(snd_card_name);
Ashish Jain30ae8942017-11-05 17:21:23 +05302597
Dhananjay Kumara7e27832017-07-11 15:40:39 +05302598 if (hw_info)
2599 hw_info_deinit(hw_info);
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002600
mpangfaa7bae2019-01-15 16:38:13 +08002601 if (retry_num >= RETRY_NUMBER) {
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002602 ALOGE("%s: Unable to find correct sound card, aborting.", __func__);
2603 return -1;
2604 }
2605
Soumya Managoli9fee7c62018-04-06 16:21:50 +05302606 if (mixer)
2607 *mixer_handle = mixer;
2608
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002609 return snd_card_num;
2610}
Naresh Tanniru6160c712017-04-17 15:43:48 +05302611
Soumya Managoli9fee7c62018-04-06 16:21:50 +05302612void audio_extn_utils_close_snd_mixer(struct mixer *mixer)
2613{
2614 if (mixer)
2615 mixer_close(mixer);
2616}
2617
Naresh Tanniru6160c712017-04-17 15:43:48 +05302618#ifdef SNDRV_COMPRESS_ENABLE_ADJUST_SESSION_CLOCK
2619int audio_extn_utils_compress_enable_drift_correction(
2620 struct stream_out *out,
2621 struct audio_out_enable_drift_correction *drift)
2622{
2623 struct snd_compr_metadata metadata;
2624 int ret = -EINVAL;
2625
2626 if(drift == NULL) {
2627 ALOGE("%s:: Invalid param", __func__);
2628 goto exit;
2629 }
2630
2631 ALOGD("%s:: drift enable %d", __func__,drift->enable);
2632
2633 if (!is_offload_usecase(out->usecase)) {
2634 ALOGE("%s:: not supported for non offload session", __func__);
2635 goto exit;
2636 }
2637
2638 if (!out->compr) {
2639 ALOGW("%s:: offload session not yet opened,"
2640 "start delay will be configure later", __func__);
2641 goto exit;
2642 }
2643
2644 metadata.key = SNDRV_COMPRESS_ENABLE_ADJUST_SESSION_CLOCK;
2645 metadata.value[0] = drift->enable;
2646 out->drift_correction_enabled = drift->enable;
2647
2648 ret = compress_set_metadata(out->compr, &metadata);
2649 if(ret) {
2650 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2651 out->drift_correction_enabled = false;
2652 }
2653
2654exit:
2655 return ret;
2656}
2657#else
2658int audio_extn_utils_compress_enable_drift_correction(
2659 struct stream_out *out __unused,
2660 struct audio_out_enable_drift_correction *drift __unused)
2661{
2662 ALOGD("%s:: configuring drift enablement not supported", __func__);
2663 return 0;
2664}
2665#endif
2666
2667#ifdef SNDRV_COMPRESS_ADJUST_SESSION_CLOCK
2668int audio_extn_utils_compress_correct_drift(
2669 struct stream_out *out,
2670 struct audio_out_correct_drift *drift_param)
2671{
2672 struct snd_compr_metadata metadata;
2673 int ret = -EINVAL;
2674
2675 if (drift_param == NULL) {
2676 ALOGE("%s:: Invalid drift_param", __func__);
2677 goto exit;
2678 }
2679
2680 ALOGD("%s:: adjust time 0x%"PRIx64" ", __func__,
2681 drift_param->adjust_time);
2682
2683 if (!is_offload_usecase(out->usecase)) {
2684 ALOGE("%s:: not supported for non offload session", __func__);
2685 goto exit;
2686 }
2687
2688 if (!out->compr) {
2689 ALOGW("%s:: offload session not yet opened", __func__);
2690 goto exit;
2691 }
2692
2693 if (!out->drift_correction_enabled) {
2694 ALOGE("%s:: drift correction not enabled", __func__);
2695 goto exit;
2696 }
2697
2698 metadata.key = SNDRV_COMPRESS_ADJUST_SESSION_CLOCK;
2699 metadata.value[0] = 0xFFFFFFFF & drift_param->adjust_time; /* lsb */
2700 metadata.value[1] = \
2701 (0xFFFFFFFF00000000 & drift_param->adjust_time) >> 32; /* msb*/
2702
2703 ret = compress_set_metadata(out->compr, &metadata);
2704 if(ret)
2705 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2706exit:
2707 return ret;
2708}
2709#else
2710int audio_extn_utils_compress_correct_drift(
2711 struct stream_out *out __unused,
2712 struct audio_out_correct_drift *drift_param __unused)
2713{
2714 ALOGD("%s:: setting adjust clock not supported", __func__);
2715 return 0;
2716}
2717#endif
Naresh Tanniru29bce4e2017-04-27 17:54:30 +05302718
2719int audio_extn_utils_set_channel_map(
2720 struct stream_out *out,
2721 struct audio_out_channel_map_param *channel_map_param)
2722{
2723 int ret = -EINVAL, i = 0;
2724 int channels = audio_channel_count_from_out_mask(out->channel_mask);
2725
2726 if (channel_map_param == NULL) {
2727 ALOGE("%s:: Invalid channel_map", __func__);
2728 goto exit;
2729 }
2730
2731 if (channel_map_param->channels != channels) {
2732 ALOGE("%s:: Channels(%d) does not match stream channels(%d)",
2733 __func__, channel_map_param->channels, channels);
2734 goto exit;
2735 }
2736
2737 for ( i = 0; i < channels; i++) {
2738 ALOGV("%s:: channel_map[%d]- %d", __func__, i, channel_map_param->channel_map[i]);
2739 out->channel_map_param.channel_map[i] = channel_map_param->channel_map[i];
2740 }
2741 ret = 0;
2742exit:
2743 return ret;
2744}
Varun Balaraje49253e2017-07-06 19:48:56 +05302745
2746int audio_extn_utils_set_pan_scale_params(
2747 struct stream_out *out,
2748 struct mix_matrix_params *mm_params)
2749{
2750 int ret = -EINVAL, i = 0, j = 0;
2751
Varun Balaraj003ee752017-08-07 17:54:55 +05302752 if (mm_params == NULL || out == NULL) {
2753 ALOGE("%s:: Invalid mix matrix or out param", __func__);
Varun Balaraje49253e2017-07-06 19:48:56 +05302754 goto exit;
2755 }
2756
2757 if (mm_params->num_output_channels > MAX_CHANNELS_SUPPORTED ||
2758 mm_params->num_output_channels <= 0 ||
2759 mm_params->num_input_channels > MAX_CHANNELS_SUPPORTED ||
2760 mm_params->num_input_channels <= 0)
2761 goto exit;
2762
2763 out->pan_scale_params.num_output_channels = mm_params->num_output_channels;
2764 out->pan_scale_params.num_input_channels = mm_params->num_input_channels;
2765 out->pan_scale_params.has_output_channel_map =
2766 mm_params->has_output_channel_map;
2767 for (i = 0; i < mm_params->num_output_channels; i++)
2768 out->pan_scale_params.output_channel_map[i] =
2769 mm_params->output_channel_map[i];
2770
2771 out->pan_scale_params.has_input_channel_map =
2772 mm_params->has_input_channel_map;
2773 for (i = 0; i < mm_params->num_input_channels; i++)
2774 out->pan_scale_params.input_channel_map[i] =
2775 mm_params->input_channel_map[i];
2776
2777 out->pan_scale_params.has_mixer_coeffs = mm_params->has_mixer_coeffs;
2778 for (i = 0; i < mm_params->num_output_channels; i++)
2779 for (j = 0; j < mm_params->num_input_channels; j++) {
2780 //Convert the channel coefficient gains in Q14 format
2781 out->pan_scale_params.mixer_coeffs[i][j] =
2782 mm_params->mixer_coeffs[i][j] * (2 << 13);
2783 }
2784
2785 ret = platform_set_stream_pan_scale_params(out->dev->platform,
2786 out->pcm_device_id,
2787 out->pan_scale_params);
2788
2789exit:
2790 return ret;
2791}
2792
2793int audio_extn_utils_set_downmix_params(
2794 struct stream_out *out,
2795 struct mix_matrix_params *mm_params)
2796{
2797 int ret = -EINVAL, i = 0, j = 0;
2798 struct audio_usecase *usecase = NULL;
2799
Aniket Kumar Lataf9f246e2017-09-15 15:20:16 -07002800 if (mm_params == NULL || out == NULL) {
Varun Balaraj003ee752017-08-07 17:54:55 +05302801 ALOGE("%s:: Invalid mix matrix or out param", __func__);
Varun Balaraje49253e2017-07-06 19:48:56 +05302802 goto exit;
2803 }
2804
2805 if (mm_params->num_output_channels > MAX_CHANNELS_SUPPORTED ||
2806 mm_params->num_output_channels <= 0 ||
2807 mm_params->num_input_channels > MAX_CHANNELS_SUPPORTED ||
2808 mm_params->num_input_channels <= 0)
2809 goto exit;
2810
2811 usecase = get_usecase_from_list(out->dev, out->usecase);
Varun Balaraj003ee752017-08-07 17:54:55 +05302812 if (!usecase) {
2813 ALOGE("%s: Get usecase list failed!", __func__);
Aniket Kumar Lataf9f246e2017-09-15 15:20:16 -07002814 goto exit;
2815 }
Varun Balaraje49253e2017-07-06 19:48:56 +05302816 out->downmix_params.num_output_channels = mm_params->num_output_channels;
2817 out->downmix_params.num_input_channels = mm_params->num_input_channels;
2818
2819 out->downmix_params.has_output_channel_map =
2820 mm_params->has_output_channel_map;
2821 for (i = 0; i < mm_params->num_output_channels; i++) {
2822 out->downmix_params.output_channel_map[i] =
2823 mm_params->output_channel_map[i];
2824 }
2825
2826 out->downmix_params.has_input_channel_map =
2827 mm_params->has_input_channel_map;
2828 for (i = 0; i < mm_params->num_input_channels; i++)
2829 out->downmix_params.input_channel_map[i] =
2830 mm_params->input_channel_map[i];
2831
2832 out->downmix_params.has_mixer_coeffs = mm_params->has_mixer_coeffs;
2833 for (i = 0; i < mm_params->num_output_channels; i++)
Nikhil Latukar2e6f6242017-08-15 13:37:07 +05302834 for (j = 0; j < mm_params->num_input_channels; j++) {
2835 //Convert the channel coefficient gains in Q14 format
Varun Balaraje49253e2017-07-06 19:48:56 +05302836 out->downmix_params.mixer_coeffs[i][j] =
Nikhil Latukar2e6f6242017-08-15 13:37:07 +05302837 mm_params->mixer_coeffs[i][j] * (2 << 13);
2838 }
Varun Balaraje49253e2017-07-06 19:48:56 +05302839
2840 ret = platform_set_stream_downmix_params(out->dev->platform,
2841 out->pcm_device_id,
2842 usecase->out_snd_device,
2843 out->downmix_params);
2844
2845exit:
2846 return ret;
2847}
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05302848
2849bool audio_extn_utils_is_dolby_format(audio_format_t format)
2850{
2851 if (format == AUDIO_FORMAT_AC3 ||
2852 format == AUDIO_FORMAT_E_AC3 ||
2853 format == AUDIO_FORMAT_E_AC3_JOC)
2854 return true;
2855 else
2856 return false;
2857}
2858
`Deeraj Soman676c2702017-09-18 19:25:53 +05302859int audio_extn_utils_get_bit_width_from_string(const char *id_string)
2860{
2861 int i;
2862 const mixer_config_lookup mixer_bitwidth_config[] = {{"S24_3LE", 24},
2863 {"S32_LE", 32},
2864 {"S24_LE", 24},
2865 {"S16_LE", 16}};
2866 int num_configs = sizeof(mixer_bitwidth_config) / sizeof(mixer_bitwidth_config[0]);
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05302867
`Deeraj Soman676c2702017-09-18 19:25:53 +05302868 for (i = 0; i < num_configs; i++) {
2869 if (!strcmp(id_string, mixer_bitwidth_config[i].id_string))
2870 return mixer_bitwidth_config[i].value;
2871 }
2872
2873 return -EINVAL;
2874}
2875
2876int audio_extn_utils_get_sample_rate_from_string(const char *id_string)
2877{
2878 int i;
2879 const mixer_config_lookup mixer_samplerate_config[] = {{"KHZ_32", 32000},
2880 {"KHZ_48", 48000},
2881 {"KHZ_96", 96000},
2882 {"KHZ_144", 144000},
2883 {"KHZ_192", 192000},
2884 {"KHZ_384", 384000},
2885 {"KHZ_44P1", 44100},
2886 {"KHZ_88P2", 88200},
2887 {"KHZ_176P4", 176400},
2888 {"KHZ_352P8", 352800}};
2889 int num_configs = sizeof(mixer_samplerate_config) / sizeof(mixer_samplerate_config[0]);
2890
2891 for (i = 0; i < num_configs; i++) {
2892 if (!strcmp(id_string, mixer_samplerate_config[i].id_string))
2893 return mixer_samplerate_config[i].value;
2894 }
2895
2896 return -EINVAL;
2897}
2898
2899int audio_extn_utils_get_channels_from_string(const char *id_string)
2900{
2901 int i;
2902 const mixer_config_lookup mixer_channels_config[] = {{"One", 1},
2903 {"Two", 2},
2904 {"Three",3},
2905 {"Four", 4},
2906 {"Five", 5},
2907 {"Six", 6},
2908 {"Seven", 7},
2909 {"Eight", 8}};
2910 int num_configs = sizeof(mixer_channels_config) / sizeof(mixer_channels_config[0]);
2911
2912 for (i = 0; i < num_configs; i++) {
2913 if (!strcmp(id_string, mixer_channels_config[i].id_string))
2914 return mixer_channels_config[i].value;
2915 }
2916
2917 return -EINVAL;
2918}
Surendar karka30569792018-05-08 12:02:21 +05302919
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08002920void audio_extn_utils_release_snd_device(snd_device_t snd_device)
2921{
2922 audio_extn_dev_arbi_release(snd_device);
2923 audio_extn_sound_trigger_update_device_status(snd_device,
2924 ST_EVENT_SND_DEVICE_FREE);
2925 audio_extn_listen_update_device_status(snd_device,
2926 LISTEN_EVENT_SND_DEVICE_FREE);
2927}
2928
Weiyin Jiang37472b92019-09-04 16:52:08 +08002929int audio_extn_utils_get_license_params(
2930 const struct audio_device *adev,
2931 struct audio_license_params *license_params)
Surendar karka30569792018-05-08 12:02:21 +05302932{
2933 if(!license_params)
2934 return -EINVAL;
Weiyin Jiang37472b92019-09-04 16:52:08 +08002935
2936 return platform_get_license_by_product(adev->platform,
2937 (const char*)license_params->product, &license_params->key, license_params->license);
Surendar karka30569792018-05-08 12:02:21 +05302938}
2939
Aalique Grahame22e49102018-12-18 14:23:57 -08002940int audio_extn_utils_send_app_type_gain(struct audio_device *adev,
2941 int app_type,
2942 int *gain)
2943{
2944 int gain_cfg[4];
2945 const char *mixer_ctl_name = "App Type Gain";
2946 struct mixer_ctl *ctl;
2947 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
2948 if (!ctl) {
2949 ALOGE("%s: Could not get volume ctl mixer %s", __func__,
2950 mixer_ctl_name);
2951 return -EINVAL;
2952 }
2953 gain_cfg[0] = 0;
2954 gain_cfg[1] = app_type;
2955 gain_cfg[2] = gain[0];
2956 gain_cfg[3] = gain[1];
2957 ALOGV("%s app_type %d l(%d) r(%d)", __func__, app_type, gain[0], gain[1]);
2958 return mixer_ctl_set_array(ctl, gain_cfg,
2959 sizeof(gain_cfg)/sizeof(gain_cfg[0]));
2960}
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -07002961
Weiyin Jiang37472b92019-09-04 16:52:08 +08002962static void vndk_fwk_init()
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -07002963{
Weiyin Jiang37472b92019-09-04 16:52:08 +08002964 if (mVndkFwk.lib_handle != NULL)
2965 return;
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -07002966
Weiyin Jiang37472b92019-09-04 16:52:08 +08002967 mVndkFwk.lib_handle = dlopen(VNDK_FWK_LIB_PATH, RTLD_NOW);
2968 if (mVndkFwk.lib_handle == NULL) {
2969 ALOGW("%s: failed to dlopen VNDK_FWK_LIB %s", __func__, strerror(errno));
2970 return;
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -07002971 }
2972
Weiyin Jiang37472b92019-09-04 16:52:08 +08002973 *(void **)(&mVndkFwk.isVendorEnhancedFwk) =
2974 dlsym(mVndkFwk.lib_handle, "isRunningWithVendorEnhancedFramework");
2975 if (mVndkFwk.isVendorEnhancedFwk == NULL) {
2976 ALOGW("%s: dlsym failed %s", __func__, strerror(errno));
2977 if (mVndkFwk.lib_handle) {
2978 dlclose(mVndkFwk.lib_handle);
2979 mVndkFwk.lib_handle = NULL;
2980 }
2981 return;
2982 }
2983
2984
2985 *(void **)(&mVndkFwk.getVendorEnhancedInfo) =
2986 dlsym(mVndkFwk.lib_handle, "getVendorEnhancedInfo");
2987 if (mVndkFwk.getVendorEnhancedInfo == NULL) {
2988 ALOGW("%s: dlsym failed %s", __func__, strerror(errno));
2989 if (mVndkFwk.lib_handle) {
2990 dlclose(mVndkFwk.lib_handle);
2991 mVndkFwk.lib_handle = NULL;
2992 }
2993 }
2994
2995 return;
2996}
2997
2998bool audio_extn_utils_is_vendor_enhanced_fwk()
2999{
3000 static int is_vendor_enhanced_fwk = -EINVAL;
3001 if (is_vendor_enhanced_fwk != -EINVAL)
3002 return (bool)is_vendor_enhanced_fwk;
3003
3004 vndk_fwk_init();
3005
3006 if (mVndkFwk.isVendorEnhancedFwk != NULL) {
3007 is_vendor_enhanced_fwk = mVndkFwk.isVendorEnhancedFwk();
3008 ALOGW("%s: is_vendor_enhanced_fwk %d", __func__, is_vendor_enhanced_fwk);
3009 } else {
3010 is_vendor_enhanced_fwk = 0;
3011 ALOGW("%s: default to non enhanced_fwk config", __func__);
3012 }
3013
3014 return (bool)is_vendor_enhanced_fwk;
3015}
3016
3017int audio_extn_utils_get_vendor_enhanced_info()
3018{
3019 static int vendor_enhanced_info = -EINVAL;
3020 if (vendor_enhanced_info != -EINVAL)
3021 return vendor_enhanced_info;
3022
3023 vndk_fwk_init();
3024
3025 if (mVndkFwk.getVendorEnhancedInfo != NULL) {
3026 vendor_enhanced_info = mVndkFwk.getVendorEnhancedInfo();
3027 ALOGW("%s: vendor_enhanced_info 0x%x", __func__, vendor_enhanced_info);
3028 } else {
3029 vendor_enhanced_info = 0x0;
3030 ALOGW("%s: default to vendor_enhanced_info 0x0", __func__);
3031 }
3032
3033 return vendor_enhanced_info;
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -07003034}
Deeraj Soman14230922019-01-30 16:39:30 +05303035
Deeraj Somanfa377bf2019-02-06 12:57:59 +05303036int audio_extn_utils_get_perf_mode_flag(void)
3037{
3038#ifdef COMPRESSED_PERF_MODE_FLAG
3039 return COMPRESSED_PERF_MODE_FLAG;
3040#else
3041 return 0;
3042#endif
3043}
3044
Deeraj Soman14230922019-01-30 16:39:30 +05303045size_t audio_extn_utils_get_input_buffer_size(uint32_t sample_rate,
3046 audio_format_t format,
3047 int channel_count,
3048 int64_t duration_ms,
3049 bool is_low_latency)
3050{
3051 size_t size = 0;
3052 size_t capture_duration = AUDIO_CAPTURE_PERIOD_DURATION_MSEC;
3053 uint32_t bytes_per_period_sample = 0;
3054
3055
3056 if (audio_extn_utils_check_input_parameters(sample_rate, format, channel_count) != 0)
3057 return 0;
3058
3059 if (duration_ms >= MIN_OFFLOAD_BUFFER_DURATION_MS && duration_ms <= MAX_OFFLOAD_BUFFER_DURATION_MS)
3060 capture_duration = duration_ms;
3061
3062 size = (sample_rate * capture_duration) / 1000;
3063 if (is_low_latency)
3064 size = LOW_LATENCY_CAPTURE_PERIOD_SIZE;
3065
3066
3067 bytes_per_period_sample = audio_bytes_per_sample(format) * channel_count;
3068 size *= bytes_per_period_sample;
3069
3070 /* make sure the size is multiple of 32 bytes and additionally multiple of
3071 * the frame_size (required for 24bit samples and non-power-of-2 channel counts)
3072 * At 48 kHz mono 16-bit PCM:
3073 * 5.000 ms = 240 frames = 15*16*1*2 = 480, a whole multiple of 32 (15)
3074 * 3.333 ms = 160 frames = 10*16*1*2 = 320, a whole multiple of 32 (10)
3075 *
3076 * The loop reaches result within 32 iterations, as initial size is
3077 * already a multiple of frame_size
3078 */
3079 size = audio_extn_utils_nearest_multiple(size, audio_extn_utils_lcm(32, bytes_per_period_sample));
3080
3081 return size;
3082}