blob: 5795e4c0209b9f650d255e9bd36f0ef2fab9c19b [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
Samyak Jain2546f8a2019-08-29 19:24:21 +0530107#define PCM_OFFLOAD_PLAYBACK_LATENCY PCM_OFFLOAD_PLAYBACK_DSP_PATHDELAY
Manish Dewangan07de2142017-02-27 19:27:20 +0530108
Varun Balaraje49253e2017-07-06 19:48:56 +0530109#ifndef MAX_CHANNELS_SUPPORTED
110#define MAX_CHANNELS_SUPPORTED 8
111#endif
112
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -0700113#ifdef __LP64__
114#define VNDK_FWK_LIB_PATH "/vendor/lib64/libqti_vndfwk_detect.so"
115#else
116#define VNDK_FWK_LIB_PATH "/vendor/lib/libqti_vndfwk_detect.so"
117#endif
118
119static void *vndk_fwk_lib_handle = NULL;
120typedef int (*vndk_fwk_isVendorEnhancedFwk_t)();
121static vndk_fwk_isVendorEnhancedFwk_t vndk_fwk_isVendorEnhancedFwk;
122
`Deeraj Soman676c2702017-09-18 19:25:53 +0530123typedef struct {
124 const char *id_string;
125 const int value;
126} mixer_config_lookup;
127
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700128struct string_to_enum {
129 const char *name;
130 uint32_t value;
131};
132
133const struct string_to_enum s_flag_name_to_enum_table[] = {
134 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_DIRECT),
135 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_PRIMARY),
136 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_FAST),
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800137 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_RAW),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700138 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_DEEP_BUFFER),
139 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD),
140 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_NON_BLOCKING),
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700141 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_HW_AV_SYNC),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700142 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_INCALL_MUSIC),
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700143 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH),
Naresh Tanniruee3499a2017-01-05 14:05:35 +0530144 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_TIMESTAMP),
Vikram Panduranga93f080e2017-06-07 18:16:14 -0700145 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_VOIP_RX),
Ben Romberger6c4d3812017-06-13 17:46:45 -0700146 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_BD),
Varun Balaraje49253e2017-07-06 19:48:56 +0530147 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_INTERACTIVE),
Derek Chen090dfc52018-03-22 22:15:29 -0400148 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_MEDIA),
149 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_SYS_NOTIFICATION),
150 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_NAV_GUIDANCE),
151 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_PHONE),
Derek Chendf05eea2019-08-01 13:57:49 -0700152 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_REAR_SEAT),
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530153 STRING_TO_ENUM(AUDIO_INPUT_FLAG_NONE),
154 STRING_TO_ENUM(AUDIO_INPUT_FLAG_FAST),
155 STRING_TO_ENUM(AUDIO_INPUT_FLAG_HW_HOTWORD),
156 STRING_TO_ENUM(AUDIO_INPUT_FLAG_RAW),
157 STRING_TO_ENUM(AUDIO_INPUT_FLAG_SYNC),
Dhananjay Kumaree4d2002016-10-25 18:02:58 +0530158 STRING_TO_ENUM(AUDIO_INPUT_FLAG_TIMESTAMP),
Dhananjay Kumar704ce6f2017-09-28 22:08:00 +0530159 STRING_TO_ENUM(AUDIO_INPUT_FLAG_COMPRESS),
Ralf Herzaec80262018-07-03 07:08:49 +0200160 STRING_TO_ENUM(AUDIO_INPUT_FLAG_PASSTHROUGH),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700161};
162
163const struct string_to_enum s_format_name_to_enum_table[] = {
Ashish Jain83a6cc22016-06-28 14:34:17 +0530164 STRING_TO_ENUM(AUDIO_FORMAT_PCM_8_BIT),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700165 STRING_TO_ENUM(AUDIO_FORMAT_PCM_16_BIT),
Ashish Jain5106d362016-05-11 19:23:33 +0530166 STRING_TO_ENUM(AUDIO_FORMAT_PCM_24_BIT_PACKED),
167 STRING_TO_ENUM(AUDIO_FORMAT_PCM_8_24_BIT),
Ashish Jainf1eaa582016-05-23 20:54:24 +0530168 STRING_TO_ENUM(AUDIO_FORMAT_PCM_32_BIT),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700169 STRING_TO_ENUM(AUDIO_FORMAT_MP3),
170 STRING_TO_ENUM(AUDIO_FORMAT_AAC),
171 STRING_TO_ENUM(AUDIO_FORMAT_VORBIS),
Mingming Yinae3530f2014-07-03 16:50:18 -0700172 STRING_TO_ENUM(AUDIO_FORMAT_AMR_NB),
173 STRING_TO_ENUM(AUDIO_FORMAT_AMR_WB),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700174 STRING_TO_ENUM(AUDIO_FORMAT_AC3),
Mingming Yinae3530f2014-07-03 16:50:18 -0700175 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700176 STRING_TO_ENUM(AUDIO_FORMAT_DTS),
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530177 STRING_TO_ENUM(AUDIO_FORMAT_DTS_HD),
Ben Romberger1aaaf862017-04-06 17:49:46 -0700178 STRING_TO_ENUM(AUDIO_FORMAT_DOLBY_TRUEHD),
Naresh Tanniru928f0862017-04-07 16:44:23 -0700179 STRING_TO_ENUM(AUDIO_FORMAT_IEC61937),
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530180 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3_JOC),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700181 STRING_TO_ENUM(AUDIO_FORMAT_WMA),
182 STRING_TO_ENUM(AUDIO_FORMAT_WMA_PRO),
183 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADIF),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700184 STRING_TO_ENUM(AUDIO_FORMAT_AMR_WB_PLUS),
185 STRING_TO_ENUM(AUDIO_FORMAT_EVRC),
186 STRING_TO_ENUM(AUDIO_FORMAT_EVRCB),
187 STRING_TO_ENUM(AUDIO_FORMAT_EVRCWB),
188 STRING_TO_ENUM(AUDIO_FORMAT_QCELP),
189 STRING_TO_ENUM(AUDIO_FORMAT_MP2),
190 STRING_TO_ENUM(AUDIO_FORMAT_EVRCNW),
Amit Shekhar6f461b12014-08-01 14:52:58 -0700191 STRING_TO_ENUM(AUDIO_FORMAT_FLAC),
Satya Krishna Pindiproli70471602015-04-24 19:12:43 +0530192 STRING_TO_ENUM(AUDIO_FORMAT_ALAC),
193 STRING_TO_ENUM(AUDIO_FORMAT_APE),
Alexy Josephcd8eaed2014-12-11 12:46:53 -0800194 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LC),
195 STRING_TO_ENUM(AUDIO_FORMAT_AAC_HE_V1),
196 STRING_TO_ENUM(AUDIO_FORMAT_AAC_HE_V2),
Manish Dewangana6fc5442015-08-24 20:30:31 +0530197 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS),
Ashish Jaine513a872015-11-19 17:00:56 +0530198 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_LC),
199 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_HE_V1),
200 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_HE_V2),
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +0530201 STRING_TO_ENUM(AUDIO_FORMAT_DSD),
Arun Kumar Dasari3b174182016-12-27 13:01:14 +0530202 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM),
203 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM_LC),
204 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM_HE_V1),
205 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM_HE_V2),
Dhanalakshmi Siddani18737932016-11-29 17:33:17 +0530206 STRING_TO_ENUM(AUDIO_FORMAT_APTX),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700207};
208
Manish Dewangan3ccdea52017-02-13 19:31:54 +0530209/* payload structure avt_device drift query */
210struct audio_avt_device_drift_stats {
211 uint32_t minor_version;
Manish Dewangan338c50a2017-09-12 15:22:03 +0530212
Manish Dewangan3ccdea52017-02-13 19:31:54 +0530213 /* Indicates the device interface direction as either
214 * source (Tx) or sink (Rx).
215 */
216 uint16_t device_direction;
Manish Dewangan338c50a2017-09-12 15:22:03 +0530217
218 /* Reference timer for drift accumulation and time stamp information.
219 * currently it only support AFE_REF_TIMER_TYPE_AVTIMER
220 */
221 uint16_t reference_timer;
Manish Dewangan3ccdea52017-02-13 19:31:54 +0530222 struct audio_avt_device_drift_param drift_param;
Manish Dewangan338c50a2017-09-12 15:22:03 +0530223} __attribute__((packed));
Manish Dewangan3ccdea52017-02-13 19:31:54 +0530224
Ben Rombergera04fabc2014-11-14 12:16:03 -0800225static char bTable[BASE_TABLE_SIZE] = {
226 'A','B','C','D','E','F','G','H','I','J','K','L',
227 'M','N','O','P','Q','R','S','T','U','V','W','X',
228 'Y','Z','a','b','c','d','e','f','g','h','i','j',
229 'k','l','m','n','o','p','q','r','s','t','u','v',
230 'w','x','y','z','0','1','2','3','4','5','6','7',
231 '8','9','+','/'
232};
233
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700234static uint32_t string_to_enum(const struct string_to_enum *table, size_t size,
235 const char *name)
236{
237 size_t i;
238 for (i = 0; i < size; i++) {
239 if (strcmp(table[i].name, name) == 0) {
240 ALOGV("%s found %s", __func__, table[i].name);
241 return table[i].value;
242 }
243 }
244 return 0;
245}
246
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530247static audio_io_flags_t parse_flag_names(char *name)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700248{
249 uint32_t flag = 0;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530250 audio_io_flags_t io_flags;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800251 char *last_r;
252 char *flag_name = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700253 while (flag_name != NULL) {
254 if (strlen(flag_name) != 0) {
255 flag |= string_to_enum(s_flag_name_to_enum_table,
256 ARRAY_SIZE(s_flag_name_to_enum_table),
257 flag_name);
258 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800259 flag_name = strtok_r(NULL, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700260 }
261
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800262 ALOGV("parse_flag_names: flag - %x", flag);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530263 io_flags.in_flags = (audio_input_flags_t)flag;
264 io_flags.out_flags = (audio_output_flags_t)flag;
265 return io_flags;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700266}
267
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530268static void parse_format_names(char *name, struct streams_io_cfg *s_info)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700269{
270 struct stream_format *sf_info = NULL;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800271 char *last_r;
272 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700273
274 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG) == 0)
275 return;
276
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530277 list_init(&s_info->format_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700278 while (str != NULL) {
279 audio_format_t format = (audio_format_t)string_to_enum(s_format_name_to_enum_table,
280 ARRAY_SIZE(s_format_name_to_enum_table), str);
281 ALOGV("%s: format - %d", __func__, format);
282 if (format != 0) {
283 sf_info = (struct stream_format *)calloc(1, sizeof(struct stream_format));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700284 if (sf_info == NULL)
285 break; /* return whatever was parsed */
286
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700287 sf_info->format = format;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530288 list_add_tail(&s_info->format_list, &sf_info->list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700289 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800290 str = strtok_r(NULL, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700291 }
292}
293
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530294static void parse_sample_rate_names(char *name, struct streams_io_cfg *s_info)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700295{
Amit Shekhar6f461b12014-08-01 14:52:58 -0700296 struct stream_sample_rate *ss_info = NULL;
297 uint32_t sample_rate = 48000;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800298 char *last_r;
299 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700300
Amit Shekhar6f461b12014-08-01 14:52:58 -0700301 if (str != NULL && 0 == strcmp(str, DYNAMIC_VALUE_TAG))
302 return;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700303
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530304 list_init(&s_info->sample_rate_list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700305 while (str != NULL) {
306 sample_rate = (uint32_t)strtol(str, (char **)NULL, 10);
307 ALOGV("%s: sample_rate - %d", __func__, sample_rate);
308 if (0 != sample_rate) {
309 ss_info = (struct stream_sample_rate *)calloc(1, sizeof(struct stream_sample_rate));
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800310 if (!ss_info) {
311 ALOGE("%s: memory allocation failure", __func__);
312 return;
313 }
Amit Shekhar6f461b12014-08-01 14:52:58 -0700314 ss_info->sample_rate = sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530315 list_add_tail(&s_info->sample_rate_list, &ss_info->list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700316 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800317 str = strtok_r(NULL, "|", &last_r);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700318 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700319}
320
321static int parse_bit_width_names(char *name)
322{
323 int bit_width = 16;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800324 char *last_r;
325 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700326
327 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG))
328 bit_width = (int)strtol(str, (char **)NULL, 10);
329
330 ALOGV("%s: bit_width - %d", __func__, bit_width);
331 return bit_width;
332}
333
334static int parse_app_type_names(void *platform, char *name)
335{
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700336 int app_type = platform_get_default_app_type(platform);
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800337 char *last_r;
338 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700339
340 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG))
341 app_type = (int)strtol(str, (char **)NULL, 10);
342
343 ALOGV("%s: app_type - %d", __func__, app_type);
344 return app_type;
345}
346
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530347static void update_streams_cfg_list(cnode *root, void *platform,
348 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700349{
350 cnode *node = root->first_child;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530351 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700352
353 ALOGV("%s", __func__);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530354 s_info = (struct streams_io_cfg *)calloc(1, sizeof(struct streams_io_cfg));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700355
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530356 if (!s_info) {
357 ALOGE("failed to allocate mem for s_info list element");
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700358 return;
359 }
360
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700361 while (node) {
362 if (strcmp(node->name, FLAGS_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530363 s_info->flags = parse_flag_names((char *)node->value);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530364 } else if (strcmp(node->name, PROFILES_TAG) == 0) {
365 strlcpy(s_info->profile, (char *)node->value, sizeof(s_info->profile));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700366 } else if (strcmp(node->name, FORMATS_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530367 parse_format_names((char *)node->value, s_info);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700368 } else if (strcmp(node->name, SAMPLING_RATES_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530369 s_info->app_type_cfg.sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
370 parse_sample_rate_names((char *)node->value, s_info);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700371 } else if (strcmp(node->name, BIT_WIDTH_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530372 s_info->app_type_cfg.bit_width = parse_bit_width_names((char *)node->value);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700373 } else if (strcmp(node->name, APP_TYPE_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530374 s_info->app_type_cfg.app_type = parse_app_type_names(platform, (char *)node->value);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700375 }
376 node = node->next;
377 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530378 list_add_tail(streams_cfg_list, &s_info->list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700379}
380
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530381static void load_cfg_list(cnode *root, void *platform,
382 struct listnode *streams_output_cfg_list,
383 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700384{
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530385 cnode *node = NULL;
386
387 node = config_find(root, OUTPUTS_TAG);
388 if (node != NULL) {
389 node = node->first_child;
390 while (node) {
391 ALOGV("%s: loading output %s", __func__, node->name);
392 update_streams_cfg_list(node, platform, streams_output_cfg_list);
393 node = node->next;
394 }
395 } else {
396 ALOGI("%s: could not load output, node is NULL", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700397 }
398
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530399 node = config_find(root, INPUTS_TAG);
400 if (node != NULL) {
401 node = node->first_child;
402 while (node) {
403 ALOGV("%s: loading input %s", __func__, node->name);
404 update_streams_cfg_list(node, platform, streams_input_cfg_list);
405 node = node->next;
406 }
407 } else {
408 ALOGI("%s: could not load input, node is NULL", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700409 }
410}
411
412static void send_app_type_cfg(void *platform, struct mixer *mixer,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530413 struct listnode *streams_output_cfg_list,
414 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700415{
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530416 size_t app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT] = {0};
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700417 int length = 0, i, num_app_types = 0;
418 struct listnode *node;
419 bool update;
420 struct mixer_ctl *ctl = NULL;
421 const char *mixer_ctl_name = "App Type Config";
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530422 struct streams_io_cfg *s_info = NULL;
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800423 uint32_t target_bit_width = 0;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700424
425 if (!mixer) {
426 ALOGE("%s: mixer is null",__func__);
427 return;
428 }
429 ctl = mixer_get_ctl_by_name(mixer, mixer_ctl_name);
430 if (!ctl) {
431 ALOGE("%s: Could not get ctl for mixer cmd - %s",__func__, mixer_ctl_name);
432 return;
433 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530434 app_type_cfg[length++] = num_app_types;
435
436 if (list_empty(streams_output_cfg_list)) {
437 app_type_cfg[length++] = platform_get_default_app_type_v2(platform, PCM_PLAYBACK);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700438 app_type_cfg[length++] = 48000;
439 app_type_cfg[length++] = 16;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530440 num_app_types += 1;
441 }
442 if (list_empty(streams_input_cfg_list)) {
443 app_type_cfg[length++] = platform_get_default_app_type_v2(platform, PCM_CAPTURE);
444 app_type_cfg[length++] = 48000;
445 app_type_cfg[length++] = 16;
446 num_app_types += 1;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700447 }
448
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800449 /* get target bit width for ADM enforce mode */
450 target_bit_width = adev_get_dsp_bit_width_enforce_mode();
451
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700452 list_for_each(node, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530453 s_info = node_to_item(node, struct streams_io_cfg, list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700454 update = true;
455 for (i=0; i<length; i=i+3) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530456 if (app_type_cfg[i+1] == 0)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700457 break;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530458 else if (app_type_cfg[i+1] == (size_t)s_info->app_type_cfg.app_type) {
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530459 if (app_type_cfg[i+2] < (size_t)s_info->app_type_cfg.sample_rate)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530460 app_type_cfg[i+2] = s_info->app_type_cfg.sample_rate;
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530461 if (app_type_cfg[i+3] < (size_t)s_info->app_type_cfg.bit_width)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530462 app_type_cfg[i+3] = s_info->app_type_cfg.bit_width;
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800463 /* ADM bit width = max(enforce_bit_width, bit_width from s_info */
464 if (audio_extn_is_dsp_bit_width_enforce_mode_supported(s_info->flags.out_flags) &&
465 (target_bit_width > app_type_cfg[i+3]))
466 app_type_cfg[i+3] = target_bit_width;
467
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700468 update = false;
469 break;
470 }
471 }
472 if (update && ((length + 3) <= MAX_LENGTH_MIXER_CONTROL_IN_INT)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530473 num_app_types += 1;
474 app_type_cfg[length++] = s_info->app_type_cfg.app_type;
475 app_type_cfg[length++] = s_info->app_type_cfg.sample_rate;
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800476 app_type_cfg[length] = s_info->app_type_cfg.bit_width;
477 if (audio_extn_is_dsp_bit_width_enforce_mode_supported(s_info->flags.out_flags) &&
478 (target_bit_width > app_type_cfg[length]))
479 app_type_cfg[length] = target_bit_width;
480
481 length++;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530482 }
483 }
484 list_for_each(node, streams_input_cfg_list) {
485 s_info = node_to_item(node, struct streams_io_cfg, list);
486 update = true;
487 for (i=0; i<length; i=i+3) {
488 if (app_type_cfg[i+1] == 0)
489 break;
490 else if (app_type_cfg[i+1] == (size_t)s_info->app_type_cfg.app_type) {
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530491 if (app_type_cfg[i+2] < (size_t)s_info->app_type_cfg.sample_rate)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530492 app_type_cfg[i+2] = s_info->app_type_cfg.sample_rate;
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530493 if (app_type_cfg[i+3] < (size_t)s_info->app_type_cfg.bit_width)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530494 app_type_cfg[i+3] = s_info->app_type_cfg.bit_width;
495 update = false;
496 break;
497 }
498 }
499 if (update && ((length + 3) <= MAX_LENGTH_MIXER_CONTROL_IN_INT)) {
500 num_app_types += 1;
501 app_type_cfg[length++] = s_info->app_type_cfg.app_type;
502 app_type_cfg[length++] = s_info->app_type_cfg.sample_rate;
503 app_type_cfg[length++] = s_info->app_type_cfg.bit_width;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700504 }
505 }
506 ALOGV("%s: num_app_types: %d", __func__, num_app_types);
507 if (num_app_types) {
508 app_type_cfg[0] = num_app_types;
509 mixer_ctl_set_array(ctl, app_type_cfg, length);
510 }
511}
512
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530513void audio_extn_utils_update_streams_cfg_lists(void *platform,
514 struct mixer *mixer,
515 struct listnode *streams_output_cfg_list,
516 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700517{
518 cnode *root;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530519 char *data = NULL;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700520
521 ALOGV("%s", __func__);
522 list_init(streams_output_cfg_list);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530523 list_init(streams_input_cfg_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700524
525 root = config_node("", "");
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700526 if (root == NULL) {
527 ALOGE("cfg_list, NULL config root");
528 return;
529 }
530
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530531 data = (char *)load_file(AUDIO_IO_POLICY_VENDOR_CONFIG_FILE, NULL);
532 if (data == NULL) {
533 ALOGD("%s: failed to open io config file(%s), trying older config file",
534 __func__, AUDIO_IO_POLICY_VENDOR_CONFIG_FILE);
535 data = (char *)load_file(AUDIO_OUTPUT_POLICY_VENDOR_CONFIG_FILE, NULL);
536 if (data == NULL) {
537 send_app_type_cfg(platform, mixer,
538 streams_output_cfg_list,
539 streams_input_cfg_list);
540 ALOGE("%s: could not load io policy config!", __func__);
yidongh6eb4f752017-04-19 18:20:57 +0800541 free(root);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530542 return;
543 }
544 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700545
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530546 config_load(root, data);
547 load_cfg_list(root, platform, streams_output_cfg_list,
548 streams_input_cfg_list);
549
550 send_app_type_cfg(platform, mixer, streams_output_cfg_list,
551 streams_input_cfg_list);
Alexy Josephaee4fdd2016-01-29 13:02:07 -0800552
553 config_free(root);
yidongh6eb4f752017-04-19 18:20:57 +0800554 free(root);
Alexy Josephaee4fdd2016-01-29 13:02:07 -0800555 free(data);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700556}
557
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530558static void audio_extn_utils_dump_streams_cfg_list(
559 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700560{
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700561 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530562 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700563 struct stream_format *sf_info;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700564 struct stream_sample_rate *ss_info;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530565
566 list_for_each(node_i, streams_cfg_list) {
567 s_info = node_to_item(node_i, struct streams_io_cfg, list);
568 ALOGV("%s: flags-%d, sample_rate-%d, bit_width-%d, app_type-%d",
569 __func__, s_info->flags.out_flags, s_info->app_type_cfg.sample_rate,
570 s_info->app_type_cfg.bit_width, s_info->app_type_cfg.app_type);
571 list_for_each(node_j, &s_info->format_list) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700572 sf_info = node_to_item(node_j, struct stream_format, list);
573 ALOGV("format-%x", sf_info->format);
574 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530575 list_for_each(node_j, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700576 ss_info = node_to_item(node_j, struct stream_sample_rate, list);
577 ALOGV("sample rate-%d", ss_info->sample_rate);
578 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700579 }
580}
581
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530582void audio_extn_utils_dump_streams_cfg_lists(
583 struct listnode *streams_output_cfg_list,
584 struct listnode *streams_input_cfg_list)
585{
586 ALOGV("%s", __func__);
587 audio_extn_utils_dump_streams_cfg_list(streams_output_cfg_list);
588 audio_extn_utils_dump_streams_cfg_list(streams_input_cfg_list);
589}
590
591static void audio_extn_utils_release_streams_cfg_list(
592 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700593{
594 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530595 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700596
597 ALOGV("%s", __func__);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530598
599 while (!list_empty(streams_cfg_list)) {
600 node_i = list_head(streams_cfg_list);
601 s_info = node_to_item(node_i, struct streams_io_cfg, list);
602 while (!list_empty(&s_info->format_list)) {
603 node_j = list_head(&s_info->format_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700604 list_remove(node_j);
605 free(node_to_item(node_j, struct stream_format, list));
606 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530607 while (!list_empty(&s_info->sample_rate_list)) {
608 node_j = list_head(&s_info->sample_rate_list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700609 list_remove(node_j);
610 free(node_to_item(node_j, struct stream_sample_rate, list));
611 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700612 list_remove(node_i);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530613 free(node_to_item(node_i, struct streams_io_cfg, list));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700614 }
615}
616
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530617void audio_extn_utils_release_streams_cfg_lists(
618 struct listnode *streams_output_cfg_list,
619 struct listnode *streams_input_cfg_list)
620{
621 ALOGV("%s", __func__);
622 audio_extn_utils_release_streams_cfg_list(streams_output_cfg_list);
623 audio_extn_utils_release_streams_cfg_list(streams_input_cfg_list);
624}
625
626static bool set_app_type_cfg(struct streams_io_cfg *s_info,
627 struct stream_app_type_cfg *app_type_cfg,
628 uint32_t sample_rate, uint32_t bit_width)
Amit Shekhar6f461b12014-08-01 14:52:58 -0700629 {
630 struct listnode *node_i;
631 struct stream_sample_rate *ss_info;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530632 list_for_each(node_i, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700633 ss_info = node_to_item(node_i, struct stream_sample_rate, list);
634 if ((sample_rate <= ss_info->sample_rate) &&
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530635 (bit_width == s_info->app_type_cfg.bit_width)) {
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700636
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530637 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700638 app_type_cfg->sample_rate = ss_info->sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530639 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700640 ALOGV("%s app_type_cfg->app_type %d, app_type_cfg->sample_rate %d, app_type_cfg->bit_width %d",
641 __func__, app_type_cfg->app_type, app_type_cfg->sample_rate, app_type_cfg->bit_width);
642 return true;
643 }
644 }
645 /*
646 * Reiterate through the list assuming dafault sample rate.
647 * Handles scenario where input sample rate is higher
648 * than all sample rates in list for the input bit width.
649 */
650 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
Apoorv Raghuvanshif59bb222015-02-18 12:23:23 -0800651
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530652 list_for_each(node_i, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700653 ss_info = node_to_item(node_i, struct stream_sample_rate, list);
654 if ((sample_rate <= ss_info->sample_rate) &&
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530655 (bit_width == s_info->app_type_cfg.bit_width)) {
656 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700657 app_type_cfg->sample_rate = sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530658 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Apoorv Raghuvanshif59bb222015-02-18 12:23:23 -0800659 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 -0700660 __func__, app_type_cfg->app_type, app_type_cfg->sample_rate, app_type_cfg->bit_width);
661 return true;
662 }
663 }
664 return false;
665}
666
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530667void audio_extn_utils_update_stream_input_app_type_cfg(void *platform,
668 struct listnode *streams_input_cfg_list,
669 audio_devices_t devices __unused,
670 audio_input_flags_t flags,
671 audio_format_t format,
672 uint32_t sample_rate,
673 uint32_t bit_width,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530674 char* profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530675 struct stream_app_type_cfg *app_type_cfg)
676{
677 struct listnode *node_i, *node_j;
678 struct streams_io_cfg *s_info;
679 struct stream_format *sf_info;
680
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530681 ALOGV("%s: flags: 0x%x, format: 0x%x sample_rate %d, profile %s",
682 __func__, flags, format, sample_rate, profile);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530683
684 list_for_each(node_i, streams_input_cfg_list) {
685 s_info = node_to_item(node_i, struct streams_io_cfg, list);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530686 /* Along with flags do profile matching if set at either end.*/
687 if (s_info->flags.in_flags == flags &&
688 ((profile[0] == '\0' && s_info->profile[0] == '\0') ||
689 strncmp(s_info->profile, profile, sizeof(s_info->profile)) == 0)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530690 list_for_each(node_j, &s_info->format_list) {
691 sf_info = node_to_item(node_j, struct stream_format, list);
692 if (sf_info->format == format) {
693 if (set_app_type_cfg(s_info, app_type_cfg, sample_rate, bit_width))
694 return;
695 }
696 }
697 }
698 }
699 ALOGW("%s: App type could not be selected. Falling back to default", __func__);
700 app_type_cfg->app_type = platform_get_default_app_type_v2(platform, PCM_CAPTURE);
701 app_type_cfg->sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
702 app_type_cfg->bit_width = 16;
703}
704
705void audio_extn_utils_update_stream_output_app_type_cfg(void *platform,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700706 struct listnode *streams_output_cfg_list,
Amit Shekhar1d896042014-10-03 13:16:09 -0700707 audio_devices_t devices,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700708 audio_output_flags_t flags,
709 audio_format_t format,
Amit Shekhar6f461b12014-08-01 14:52:58 -0700710 uint32_t sample_rate,
711 uint32_t bit_width,
Manish Dewangan837dc462015-05-27 10:17:41 +0530712 audio_channel_mask_t channel_mask,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530713 char *profile,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700714 struct stream_app_type_cfg *app_type_cfg)
715{
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +0530716 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530717 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700718 struct stream_format *sf_info;
Manish Dewangan837dc462015-05-27 10:17:41 +0530719 char value[PROPERTY_VALUE_MAX] = {0};
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700720
Ramu Gottipati074fa4d2018-09-11 20:05:51 +0530721 if (devices & AUDIO_DEVICE_OUT_SPEAKER) {
722 int bw = platform_get_snd_device_bit_width(SND_DEVICE_OUT_SPEAKER);
723 if ((-ENOSYS != bw) && (bit_width > (uint32_t)bw))
Amit Shekhar5a39c912014-10-14 15:39:30 -0700724 bit_width = (uint32_t)bw;
Ramu Gottipati074fa4d2018-09-11 20:05:51 +0530725 else if (-ENOSYS == bw)
726 bit_width = CODEC_BACKEND_DEFAULT_BIT_WIDTH;
Amit Shekhar1d896042014-10-03 13:16:09 -0700727 sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Ramu Gottipati074fa4d2018-09-11 20:05:51 +0530728 ALOGI("%s Allowing 24 and above bits playback on speaker ONLY at default sampling rate", __func__);
Amit Shekhar1d896042014-10-03 13:16:09 -0700729 }
730
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -0700731 property_get("vendor.audio.playback.mch.downsample",value,"");
Manish Dewangan837dc462015-05-27 10:17:41 +0530732 if (!strncmp("true", value, sizeof("true"))) {
733 if ((popcount(channel_mask) > 2) &&
734 (sample_rate > CODEC_BACKEND_DEFAULT_SAMPLE_RATE) &&
Aalique Grahame5cd12ff2017-10-19 10:57:00 -0700735 !(flags & (audio_output_flags_t)AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH)) {
Manish Dewangan837dc462015-05-27 10:17:41 +0530736 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
737 ALOGD("%s: MCH session defaulting sample rate to %d",
738 __func__, sample_rate);
739 }
740 }
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +0530741
742 /* Set sampling rate to 176.4 for DSD64
743 * and 352.8Khz for DSD128.
744 * Set Bit Width to 16. output will be 16 bit
745 * post DoP in ASM.
746 */
Aalique Grahame5cd12ff2017-10-19 10:57:00 -0700747 if ((flags & (audio_output_flags_t)AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH) &&
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +0530748 (format == AUDIO_FORMAT_DSD)) {
749 bit_width = 16;
750 if (sample_rate == INPUT_SAMPLING_RATE_DSD64)
751 sample_rate = OUTPUT_SAMPLING_RATE_DSD64;
752 else if (sample_rate == INPUT_SAMPLING_RATE_DSD128)
753 sample_rate = OUTPUT_SAMPLING_RATE_DSD128;
754 }
755
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +0530756
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800757 ALOGV("%s: flags: %x, format: %x sample_rate %d, profile %s, app_type %d",
758 __func__, flags, format, sample_rate, profile, app_type_cfg->app_type);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700759 list_for_each(node_i, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530760 s_info = node_to_item(node_i, struct streams_io_cfg, list);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530761 /* Along with flags do profile matching if set at either end.*/
762 if (s_info->flags.out_flags == flags &&
763 ((profile[0] == '\0' && s_info->profile[0] == '\0') ||
764 strncmp(s_info->profile, profile, sizeof(s_info->profile)) == 0)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530765 list_for_each(node_j, &s_info->format_list) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700766 sf_info = node_to_item(node_j, struct stream_format, list);
767 if (sf_info->format == format) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530768 if (set_app_type_cfg(s_info, app_type_cfg, sample_rate, bit_width))
Amit Shekhar6f461b12014-08-01 14:52:58 -0700769 return;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700770 }
771 }
772 }
773 }
774 list_for_each(node_i, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530775 s_info = node_to_item(node_i, struct streams_io_cfg, list);
776 if (s_info->flags.out_flags == AUDIO_OUTPUT_FLAG_PRIMARY) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700777 ALOGV("Compatible output profile not found.");
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530778 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
779 app_type_cfg->sample_rate = s_info->app_type_cfg.sample_rate;
780 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700781 ALOGV("%s Default to primary output: App type: %d sample_rate %d",
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530782 __func__, s_info->app_type_cfg.app_type, app_type_cfg->sample_rate);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700783 return;
784 }
785 }
786 ALOGW("%s: App type could not be selected. Falling back to default", __func__);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700787 app_type_cfg->app_type = platform_get_default_app_type(platform);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700788 app_type_cfg->sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700789 app_type_cfg->bit_width = 16;
790}
791
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +0530792static bool audio_is_this_native_usecase(struct audio_usecase *uc)
793{
794 bool native_usecase = false;
795 struct stream_out *out = (struct stream_out*) uc->stream.out;
796
797 if (PCM_PLAYBACK == uc->type && out != NULL &&
798 NATIVE_AUDIO_MODE_INVALID != platform_get_native_support() &&
799 is_offload_usecase(uc->id) &&
800 (out->sample_rate == OUTPUT_SAMPLING_RATE_44100))
801 native_usecase = true;
802
803 return native_usecase;
804}
805
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800806bool audio_extn_is_dsp_bit_width_enforce_mode_supported(audio_output_flags_t flags)
807{
808 /* DSP bitwidth enforce mode for ADM and AFE:
809 * includes:
810 * deep buffer, low latency, direct pcm and offload.
811 * excludes:
812 * ull(raw+fast), VOIP.
813 */
814 if ((flags & AUDIO_OUTPUT_FLAG_VOIP_RX) ||
815 ((flags & AUDIO_OUTPUT_FLAG_RAW) &&
816 (flags & AUDIO_OUTPUT_FLAG_FAST)))
817 return false;
818
819
820 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) ||
821 (flags & AUDIO_OUTPUT_FLAG_DIRECT) ||
822 (flags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) ||
823 (flags & AUDIO_OUTPUT_FLAG_PRIMARY))
824 return true;
825 else
826 return false;
827}
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800828
829static inline bool audio_is_vr_mode_on(struct audio_device *(__attribute__((unused)) adev))
830{
831 return adev->vr_audio_mode_enabled;
832}
833
Derek Chenf7092792017-05-23 12:23:53 -0400834static void audio_extn_btsco_get_sample_rate(int snd_device, int *sample_rate)
835{
836 switch (snd_device) {
837 case SND_DEVICE_OUT_BT_SCO:
838 case SND_DEVICE_IN_BT_SCO_MIC:
839 case SND_DEVICE_IN_BT_SCO_MIC_NREC:
840 *sample_rate = 8000;
841 break;
842 case SND_DEVICE_OUT_BT_SCO_WB:
843 case SND_DEVICE_IN_BT_SCO_MIC_WB:
844 case SND_DEVICE_IN_BT_SCO_MIC_WB_NREC:
845 *sample_rate = 16000;
846 break;
847 default:
848 ALOGD("%s:Not a BT SCO device, need not update sampling rate\n", __func__);
849 break;
850 }
851}
852
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530853void audio_extn_utils_update_stream_app_type_cfg_for_usecase(
854 struct audio_device *adev,
855 struct audio_usecase *usecase)
856{
857 ALOGV("%s", __func__);
858
859 switch(usecase->type) {
860 case PCM_PLAYBACK:
861 audio_extn_utils_update_stream_output_app_type_cfg(adev->platform,
862 &adev->streams_output_cfg_list,
863 usecase->stream.out->devices,
864 usecase->stream.out->flags,
Aalique Grahame65780b52017-09-27 14:59:56 -0700865 usecase->stream.out->hal_op_format,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530866 usecase->stream.out->sample_rate,
867 usecase->stream.out->bit_width,
868 usecase->stream.out->channel_mask,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530869 usecase->stream.out->profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530870 &usecase->stream.out->app_type_cfg);
871 ALOGV("%s Selected apptype: %d", __func__, usecase->stream.out->app_type_cfg.app_type);
872 break;
873 case PCM_CAPTURE:
Vikram Panduranga93f080e2017-06-07 18:16:14 -0700874 if (usecase->id == USECASE_AUDIO_RECORD_VOIP)
875 usecase->stream.in->app_type_cfg.app_type = APP_TYPE_VOIP_AUDIO;
876 else
877 audio_extn_utils_update_stream_input_app_type_cfg(adev->platform,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530878 &adev->streams_input_cfg_list,
879 usecase->stream.in->device,
880 usecase->stream.in->flags,
881 usecase->stream.in->format,
882 usecase->stream.in->sample_rate,
883 usecase->stream.in->bit_width,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530884 usecase->stream.in->profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530885 &usecase->stream.in->app_type_cfg);
886 ALOGV("%s Selected apptype: %d", __func__, usecase->stream.in->app_type_cfg.app_type);
887 break;
Surendar Karka93cd25a2018-08-28 14:21:37 +0530888 case TRANSCODE_LOOPBACK_RX :
Siddartha Shaik343abc62017-08-08 11:15:25 +0530889 audio_extn_utils_update_stream_output_app_type_cfg(adev->platform,
890 &adev->streams_output_cfg_list,
891 usecase->stream.inout->out_config.devices,
892 0,
893 usecase->stream.inout->out_config.format,
894 usecase->stream.inout->out_config.sample_rate,
895 usecase->stream.inout->out_config.bit_width,
896 usecase->stream.inout->out_config.channel_mask,
897 usecase->stream.inout->profile,
898 &usecase->stream.inout->out_app_type_cfg);
899 ALOGV("%s Selected apptype: %d", __func__, usecase->stream.inout->out_app_type_cfg.app_type);
900 break;
Derek Chenf7092792017-05-23 12:23:53 -0400901 case PCM_HFP_CALL:
902 switch (usecase->id) {
903 case USECASE_AUDIO_HFP_SCO:
904 case USECASE_AUDIO_HFP_SCO_WB:
905 audio_extn_btsco_get_sample_rate(usecase->out_snd_device,
906 &usecase->out_app_type_cfg.sample_rate);
907 usecase->in_app_type_cfg.sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
908 break;
909 case USECASE_AUDIO_HFP_SCO_DOWNLINK:
910 case USECASE_AUDIO_HFP_SCO_WB_DOWNLINK:
911 audio_extn_btsco_get_sample_rate(usecase->in_snd_device,
912 &usecase->in_app_type_cfg.sample_rate);
913 usecase->out_app_type_cfg.sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
914 break;
915 default:
916 ALOGE("%s: usecase id (%d) not supported, use default sample rate",
917 __func__, usecase->id);
918 usecase->in_app_type_cfg.sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
919 usecase->out_app_type_cfg.sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
920 break;
921 }
922 /* update out_app_type_cfg */
923 usecase->out_app_type_cfg.bit_width =
924 platform_get_snd_device_bit_width(usecase->out_snd_device);
925 usecase->out_app_type_cfg.app_type =
926 platform_get_default_app_type_v2(adev->platform, PCM_PLAYBACK);
927 /* update in_app_type_cfg */
928 usecase->in_app_type_cfg.bit_width =
929 platform_get_snd_device_bit_width(usecase->in_snd_device);
930 usecase->in_app_type_cfg.app_type =
931 platform_get_default_app_type_v2(adev->platform, PCM_CAPTURE);
932 ALOGV("%s Selected apptype: playback %d capture %d",
933 __func__, usecase->out_app_type_cfg.app_type, usecase->in_app_type_cfg.app_type);
934 break;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530935 default:
936 ALOGE("%s: app type cfg not supported for usecase type (%d)",
937 __func__, usecase->type);
938 }
939}
940
Aalique Grahame22e49102018-12-18 14:23:57 -0800941static int set_stream_app_type_mixer_ctrl(struct audio_device *adev,
942 int pcm_device_id, int app_type,
943 int acdb_dev_id, int sample_rate,
944 int stream_type,
945 snd_device_t snd_device)
946{
947
948 char mixer_ctl_name[MAX_LENGTH_MIXER_CONTROL_IN_INT];
949 struct mixer_ctl *ctl;
950 int app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT], len = 0, rc = 0;
951 int snd_device_be_idx = -1;
952
953 if (stream_type == PCM_PLAYBACK) {
954 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
955 "Audio Stream %d App Type Cfg", pcm_device_id);
956 } else if (stream_type == PCM_CAPTURE) {
957 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
958 "Audio Stream Capture %d App Type Cfg", pcm_device_id);
959 }
960
961 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
962 if (!ctl) {
963 ALOGE("%s: Could not get ctl for mixer cmd - %s",
964 __func__, mixer_ctl_name);
965 rc = -EINVAL;
966 goto exit;
967 }
968 app_type_cfg[len++] = app_type;
969 app_type_cfg[len++] = acdb_dev_id;
970 app_type_cfg[len++] = sample_rate;
971
972 snd_device_be_idx = platform_get_snd_device_backend_index(snd_device);
973 if (snd_device_be_idx > 0)
974 app_type_cfg[len++] = snd_device_be_idx;
975 ALOGV("%s: stream type %d app_type %d, acdb_dev_id %d "
976 "sample rate %d, snd_device_be_idx %d",
977 __func__, stream_type, app_type, acdb_dev_id, sample_rate,
978 snd_device_be_idx);
979 mixer_ctl_set_array(ctl, app_type_cfg, len);
980
981exit:
982 return rc;
983}
984
985static int audio_extn_utils_send_app_type_cfg_hfp(struct audio_device *adev,
986 struct audio_usecase *usecase)
987{
988 int pcm_device_id, acdb_dev_id = 0, snd_device = usecase->out_snd_device;
989 int32_t sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
990 int app_type = 0, rc = 0;
Derek Chenf7092792017-05-23 12:23:53 -0400991 bool is_bus_dev_usecase = false;
Aalique Grahame22e49102018-12-18 14:23:57 -0800992
993 ALOGV("%s", __func__);
994
995 if (usecase->type != PCM_HFP_CALL) {
Derek Chenf7092792017-05-23 12:23:53 -0400996 ALOGV("%s: not a HFP path, no need to cfg app type", __func__);
Aalique Grahame22e49102018-12-18 14:23:57 -0800997 rc = 0;
998 goto exit_send_app_type_cfg;
999 }
1000 if ((usecase->id != USECASE_AUDIO_HFP_SCO) &&
Derek Chenf7092792017-05-23 12:23:53 -04001001 (usecase->id != USECASE_AUDIO_HFP_SCO_WB) &&
1002 (usecase->id != USECASE_AUDIO_HFP_SCO_DOWNLINK) &&
1003 (usecase->id != USECASE_AUDIO_HFP_SCO_WB_DOWNLINK)) {
1004 ALOGV("%s: a usecase where app type cfg is not required", __func__);
Aalique Grahame22e49102018-12-18 14:23:57 -08001005 rc = 0;
1006 goto exit_send_app_type_cfg;
1007 }
1008
Derek Chenf7092792017-05-23 12:23:53 -04001009 if (usecase->devices & AUDIO_DEVICE_OUT_BUS)
1010 is_bus_dev_usecase = true;
1011
Aalique Grahame22e49102018-12-18 14:23:57 -08001012 snd_device = usecase->out_snd_device;
1013 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_PLAYBACK);
1014
1015 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
1016 if (acdb_dev_id < 0) {
1017 ALOGE("%s: Couldn't get the acdb dev id", __func__);
1018 rc = -EINVAL;
1019 goto exit_send_app_type_cfg;
1020 }
1021
1022 if (usecase->type == PCM_HFP_CALL) {
1023
1024 /* config HFP session:1 playback path */
Derek Chenf7092792017-05-23 12:23:53 -04001025 if (is_bus_dev_usecase) {
1026 app_type = usecase->out_app_type_cfg.app_type;
1027 sample_rate= usecase->out_app_type_cfg.sample_rate;
1028 } else {
1029 snd_device = SND_DEVICE_NONE; // use legacy behavior
1030 app_type = platform_get_default_app_type_v2(adev->platform, PCM_PLAYBACK);
1031 sample_rate= CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
1032 }
Aalique Grahame22e49102018-12-18 14:23:57 -08001033 rc = set_stream_app_type_mixer_ctrl(adev, pcm_device_id, app_type,
1034 acdb_dev_id, sample_rate,
1035 PCM_PLAYBACK,
Derek Chenf7092792017-05-23 12:23:53 -04001036 snd_device);
Aalique Grahame22e49102018-12-18 14:23:57 -08001037 if (rc < 0)
1038 goto exit_send_app_type_cfg;
1039
1040 /* config HFP session:1 capture path */
Derek Chenf7092792017-05-23 12:23:53 -04001041 if (is_bus_dev_usecase) {
1042 snd_device = usecase->in_snd_device;
1043 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_CAPTURE);
1044 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
1045 if (acdb_dev_id < 0) {
1046 ALOGE("%s: Couldn't get the acdb dev id", __func__);
1047 rc = -EINVAL;
1048 goto exit_send_app_type_cfg;
1049 }
1050 app_type = usecase->in_app_type_cfg.app_type;
1051 sample_rate= usecase->in_app_type_cfg.sample_rate;
1052 } else {
1053 snd_device = SND_DEVICE_NONE; // use legacy behavior
1054 app_type = platform_get_default_app_type_v2(adev->platform, PCM_CAPTURE);
1055 }
Aalique Grahame22e49102018-12-18 14:23:57 -08001056 rc = set_stream_app_type_mixer_ctrl(adev, pcm_device_id, app_type,
1057 acdb_dev_id, sample_rate,
1058 PCM_CAPTURE,
Derek Chenf7092792017-05-23 12:23:53 -04001059 snd_device);
Aalique Grahame22e49102018-12-18 14:23:57 -08001060 if (rc < 0)
1061 goto exit_send_app_type_cfg;
1062
Derek Chenf7092792017-05-23 12:23:53 -04001063 if (is_bus_dev_usecase)
1064 goto exit_send_app_type_cfg;
1065
Aalique Grahame22e49102018-12-18 14:23:57 -08001066 /* config HFP session:2 capture path */
1067 pcm_device_id = HFP_ASM_RX_TX;
1068 snd_device = usecase->in_snd_device;
1069 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
1070 if (acdb_dev_id <= 0) {
1071 ALOGE("%s: Couldn't get the acdb dev id", __func__);
1072 rc = -EINVAL;
1073 goto exit_send_app_type_cfg;
1074 }
1075 app_type = platform_get_default_app_type_v2(adev->platform, PCM_CAPTURE);
1076 rc = set_stream_app_type_mixer_ctrl(adev, pcm_device_id, app_type,
1077 acdb_dev_id, sample_rate, PCM_CAPTURE,
1078 SND_DEVICE_NONE);
1079 if (rc < 0)
1080 goto exit_send_app_type_cfg;
1081
1082 /* config HFP session:2 playback path */
1083 app_type = platform_get_default_app_type_v2(adev->platform, PCM_PLAYBACK);
1084 rc = set_stream_app_type_mixer_ctrl(adev, pcm_device_id, app_type,
1085 acdb_dev_id, sample_rate,
1086 PCM_PLAYBACK, SND_DEVICE_NONE);
1087 if (rc < 0)
1088 goto exit_send_app_type_cfg;
1089 }
1090
1091 rc = 0;
1092exit_send_app_type_cfg:
1093 return rc;
1094}
1095
Zhou Song06761dd2019-04-28 18:08:17 +08001096int audio_extn_utils_get_app_sample_rate_for_device(
1097 struct audio_device *adev,
1098 struct audio_usecase *usecase, int snd_device)
1099{
1100 char value[PROPERTY_VALUE_MAX] = {0};
1101 int sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
1102
1103 if ((usecase->type == PCM_PLAYBACK) && (usecase->stream.out != NULL)) {
1104 property_get("vendor.audio.playback.mch.downsample",value,"");
1105 if (!strncmp("true", value, sizeof("true"))) {
1106 if ((popcount(usecase->stream.out->channel_mask) > 2) &&
1107 (usecase->stream.out->app_type_cfg.sample_rate > CODEC_BACKEND_DEFAULT_SAMPLE_RATE) &&
1108 !(usecase->stream.out->flags &
1109 (audio_output_flags_t)AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH))
1110 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
1111 }
1112
1113 if (usecase->id == USECASE_AUDIO_PLAYBACK_VOIP) {
1114 usecase->stream.out->app_type_cfg.sample_rate = usecase->stream.out->sample_rate;
1115 } else if (usecase->stream.out->devices & AUDIO_DEVICE_OUT_SPEAKER) {
1116 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
1117 } else if ((snd_device == SND_DEVICE_OUT_HDMI ||
1118 snd_device == SND_DEVICE_OUT_USB_HEADSET ||
1119 snd_device == SND_DEVICE_OUT_DISPLAY_PORT) &&
1120 (usecase->stream.out->sample_rate >= OUTPUT_SAMPLING_RATE_44100)) {
1121 /*
1122 * To best utlize DSP, check if the stream sample rate is supported/multiple of
1123 * configured device sample rate, if not update the COPP rate to be equal to the
1124 * device sample rate, else open COPP at stream sample rate
1125 */
1126 platform_check_and_update_copp_sample_rate(adev->platform, snd_device,
1127 usecase->stream.out->sample_rate,
1128 &usecase->stream.out->app_type_cfg.sample_rate);
1129 } else if (((snd_device != SND_DEVICE_OUT_HEADPHONES_44_1 &&
1130 !audio_is_this_native_usecase(usecase)) &&
1131 usecase->stream.out->sample_rate == OUTPUT_SAMPLING_RATE_44100) ||
1132 (usecase->stream.out->sample_rate < OUTPUT_SAMPLING_RATE_44100)) {
1133 /* Reset to default if no native stream is active*/
1134 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
1135 } else if (usecase->stream.out->devices & AUDIO_DEVICE_OUT_ALL_A2DP) {
1136 /*
1137 * For a2dp playback get encoder sampling rate and set copp sampling rate,
1138 * for bit width use the stream param only.
1139 */
1140 audio_extn_a2dp_get_enc_sample_rate(&usecase->stream.out->app_type_cfg.sample_rate);
1141 ALOGI("%s using %d sample rate rate for A2DP CoPP",
1142 __func__, usecase->stream.out->app_type_cfg.sample_rate);
1143 }
1144 audio_extn_btsco_get_sample_rate(snd_device, &usecase->stream.out->app_type_cfg.sample_rate);
1145 sample_rate = usecase->stream.out->app_type_cfg.sample_rate;
1146
1147 if (((usecase->stream.out->format == AUDIO_FORMAT_E_AC3) ||
1148 (usecase->stream.out->format == AUDIO_FORMAT_E_AC3_JOC) ||
1149 (usecase->stream.out->format == AUDIO_FORMAT_DOLBY_TRUEHD))
1150 && audio_extn_passthru_is_passthrough_stream(usecase->stream.out)
1151 && !audio_extn_passthru_is_convert_supported(adev, usecase->stream.out)) {
1152 sample_rate = sample_rate * 4;
1153 if (sample_rate > HDMI_PASSTHROUGH_MAX_SAMPLE_RATE)
1154 sample_rate = HDMI_PASSTHROUGH_MAX_SAMPLE_RATE;
1155 }
1156 } else if (usecase->type == PCM_CAPTURE) {
1157 if (usecase->stream.in != NULL) {
1158 if (usecase->id == USECASE_AUDIO_RECORD_VOIP)
1159 usecase->stream.in->app_type_cfg.sample_rate = usecase->stream.in->sample_rate;
1160 if (voice_is_in_call_rec_stream(usecase->stream.in)) {
1161 audio_extn_btsco_get_sample_rate(usecase->in_snd_device,
1162 &usecase->stream.in->app_type_cfg.sample_rate);
1163 } else {
1164 audio_extn_btsco_get_sample_rate(snd_device,
1165 &usecase->stream.in->app_type_cfg.sample_rate);
1166 }
1167 sample_rate = usecase->stream.in->app_type_cfg.sample_rate;
1168 } else if (usecase->id == USECASE_AUDIO_SPKR_CALIB_TX) {
1169 sample_rate = SAMPLE_RATE_8000;
1170 }
1171 } else if (usecase->type == TRANSCODE_LOOPBACK_RX) {
1172 sample_rate = usecase->stream.inout->out_config.sample_rate;
1173 }
1174 return sample_rate;
1175}
1176
Siena Richard7c2db772016-12-21 11:32:34 -08001177static int send_app_type_cfg_for_device(struct audio_device *adev,
1178 struct audio_usecase *usecase,
1179 int split_snd_device)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001180{
1181 char mixer_ctl_name[MAX_LENGTH_MIXER_CONTROL_IN_INT];
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301182 size_t app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT] = {0};
1183 int len = 0, rc;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001184 struct mixer_ctl *ctl;
Siena Richard7c2db772016-12-21 11:32:34 -08001185 int pcm_device_id = 0, acdb_dev_id, app_type;
1186 int snd_device = split_snd_device, snd_device_be_idx = -1;
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +05301187 int32_t sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Varun Balaraje49253e2017-07-06 19:48:56 +05301188 struct streams_io_cfg *s_info = NULL;
1189 struct listnode *node = NULL;
Nikhil Laturkarac4a7492017-08-31 18:27:19 +05301190 int bd_app_type = 0;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001191
Siena Richard7c2db772016-12-21 11:32:34 -08001192 ALOGV("%s: usecase->out_snd_device %s, usecase->in_snd_device %s, split_snd_device %s",
1193 __func__, platform_get_snd_device_name(usecase->out_snd_device),
1194 platform_get_snd_device_name(usecase->in_snd_device),
1195 platform_get_snd_device_name(split_snd_device));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001196
Siddartha Shaik343abc62017-08-08 11:15:25 +05301197 if (usecase->type != PCM_PLAYBACK && usecase->type != PCM_CAPTURE &&
Surendar Karka93cd25a2018-08-28 14:21:37 +05301198 usecase->type != TRANSCODE_LOOPBACK_RX) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301199 ALOGE("%s: not a playback/capture path, no need to cfg app type", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001200 rc = 0;
1201 goto exit_send_app_type_cfg;
1202 }
1203 if ((usecase->id != USECASE_AUDIO_PLAYBACK_DEEP_BUFFER) &&
Vignesh Kulothungan0a2eff02019-07-11 16:30:13 -07001204 (usecase->id != USECASE_AUDIO_PLAYBACK_WITH_HAPTICS) &&
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001205 (usecase->id != USECASE_AUDIO_PLAYBACK_LOW_LATENCY) &&
1206 (usecase->id != USECASE_AUDIO_PLAYBACK_MULTI_CH) &&
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -08001207 (usecase->id != USECASE_AUDIO_PLAYBACK_ULL) &&
Vikram Panduranga93f080e2017-06-07 18:16:14 -07001208 (usecase->id != USECASE_AUDIO_PLAYBACK_VOIP) &&
Surendar Karka93cd25a2018-08-28 14:21:37 +05301209 (usecase->id != USECASE_AUDIO_TRANSCODE_LOOPBACK_RX) &&
Varun Balaraje49253e2017-07-06 19:48:56 +05301210 (!is_interactive_usecase(usecase->id)) &&
Srikanth Uyyala9d551402015-08-25 16:03:42 +05301211 (!is_offload_usecase(usecase->id)) &&
Derek Chenf6318be2017-06-12 17:16:24 -04001212 (usecase->type != PCM_CAPTURE) &&
1213 (!audio_extn_auto_hal_is_bus_device_usecase(usecase->id))) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301214 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 -07001215 rc = 0;
1216 goto exit_send_app_type_cfg;
1217 }
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -08001218
1219 //if VR is active then only send the mixer control
1220 if (usecase->id == USECASE_AUDIO_PLAYBACK_ULL && !audio_is_vr_mode_on(adev)) {
1221 ALOGI("ULL doesnt need sending app type cfg, returning");
1222 rc = 0;
1223 goto exit_send_app_type_cfg;
1224 }
1225
Surendar Karka93cd25a2018-08-28 14:21:37 +05301226 if (usecase->type == PCM_PLAYBACK || usecase->type == TRANSCODE_LOOPBACK_RX) {
Ben Romberger1fafdde2015-09-09 19:43:15 -07001227 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_PLAYBACK);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301228 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
1229 "Audio Stream %d App Type Cfg", pcm_device_id);
Ben Romberger1fafdde2015-09-09 19:43:15 -07001230 } else if (usecase->type == PCM_CAPTURE) {
Ben Romberger1fafdde2015-09-09 19:43:15 -07001231 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_CAPTURE);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301232 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
1233 "Audio Stream Capture %d App Type Cfg", pcm_device_id);
Srikanth Uyyala9d551402015-08-25 16:03:42 +05301234 }
Siena Richard7c2db772016-12-21 11:32:34 -08001235
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001236 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
1237 if (!ctl) {
1238 ALOGE("%s: Could not get ctl for mixer cmd - %s", __func__,
1239 mixer_ctl_name);
1240 rc = -EINVAL;
1241 goto exit_send_app_type_cfg;
1242 }
Aditya Bavanari701a6992017-03-30 19:17:16 +05301243 snd_device = platform_get_spkr_prot_snd_device(snd_device);
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301244 if (voice_is_in_call_rec_stream(usecase->stream.in) && usecase->type == PCM_CAPTURE) {
1245 snd_device_t voice_device = voice_get_incall_rec_snd_device(usecase->in_snd_device);
1246 acdb_dev_id = platform_get_snd_device_acdb_id(voice_device);
1247 ALOGV("acdb id for voice call use case %d", acdb_dev_id);
1248 } else {
1249 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
1250 }
Rohit Kumar1181b292017-01-31 18:07:17 +05301251 if (acdb_dev_id <= 0) {
1252 ALOGE("%s: Couldn't get the acdb dev id", __func__);
1253 rc = -EINVAL;
1254 goto exit_send_app_type_cfg;
1255 }
1256
Siena Richard7c2db772016-12-21 11:32:34 -08001257 snd_device_be_idx = platform_get_snd_device_backend_index(snd_device);
1258 if (snd_device_be_idx < 0) {
1259 ALOGE("%s: Couldn't get the backend index for snd device %s ret=%d",
1260 __func__, platform_get_snd_device_name(snd_device),
1261 snd_device_be_idx);
1262 }
1263
Zhou Song06761dd2019-04-28 18:08:17 +08001264 sample_rate = audio_extn_utils_get_app_sample_rate_for_device(adev, usecase, snd_device);
1265
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301266 if ((usecase->type == PCM_PLAYBACK) && (usecase->stream.out != NULL)) {
Varun Balaraje49253e2017-07-06 19:48:56 +05301267 /* Interactive streams are supported with only direct app type id.
1268 * Get Direct profile app type and use it for interactive streams
1269 */
1270 list_for_each(node, &adev->streams_output_cfg_list) {
1271 s_info = node_to_item(node, struct streams_io_cfg, list);
Aalique Grahame5cd12ff2017-10-19 10:57:00 -07001272 if (s_info->flags.out_flags == (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_BD |
Nikhil Laturkarac4a7492017-08-31 18:27:19 +05301273 AUDIO_OUTPUT_FLAG_DIRECT_PCM |
1274 AUDIO_OUTPUT_FLAG_DIRECT))
1275 bd_app_type = s_info->app_type_cfg.app_type;
Varun Balaraje49253e2017-07-06 19:48:56 +05301276 }
Aalique Grahame5cd12ff2017-10-19 10:57:00 -07001277 if (usecase->stream.out->flags == (audio_output_flags_t)AUDIO_OUTPUT_FLAG_INTERACTIVE)
Nikhil Laturkarac4a7492017-08-31 18:27:19 +05301278 app_type = bd_app_type;
Varun Balaraje49253e2017-07-06 19:48:56 +05301279 else
1280 app_type = usecase->stream.out->app_type_cfg.app_type;
Siena Richard7c2db772016-12-21 11:32:34 -08001281 app_type_cfg[len++] = app_type;
Mingming Yin21854652016-04-13 11:54:02 -07001282 app_type_cfg[len++] = acdb_dev_id;
Naresh Tanniru3a406772017-05-10 13:09:05 -07001283 app_type_cfg[len++] = sample_rate;
1284
Siena Richard7c2db772016-12-21 11:32:34 -08001285 if (snd_device_be_idx > 0)
1286 app_type_cfg[len++] = snd_device_be_idx;
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301287
Siena Richard7c2db772016-12-21 11:32:34 -08001288 ALOGI("%s PLAYBACK app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
1289 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301290
1291 } else if ((usecase->type == PCM_CAPTURE) && (usecase->stream.in != NULL)) {
Siena Richard7c2db772016-12-21 11:32:34 -08001292 app_type = usecase->stream.in->app_type_cfg.app_type;
1293 app_type_cfg[len++] = app_type;
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301294 app_type_cfg[len++] = acdb_dev_id;
Siena Richard7c2db772016-12-21 11:32:34 -08001295 app_type_cfg[len++] = sample_rate;
1296 if (snd_device_be_idx > 0)
1297 app_type_cfg[len++] = snd_device_be_idx;
1298 ALOGI("%s CAPTURE app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
1299 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301300 } else {
Siena Richard7c2db772016-12-21 11:32:34 -08001301 app_type = platform_get_default_app_type_v2(adev->platform, usecase->type);
Surendar Karka93cd25a2018-08-28 14:21:37 +05301302 if(usecase->type == TRANSCODE_LOOPBACK_RX) {
Siddartha Shaik343abc62017-08-08 11:15:25 +05301303 app_type = usecase->stream.inout->out_app_type_cfg.app_type;
1304 }
Siena Richard7c2db772016-12-21 11:32:34 -08001305 app_type_cfg[len++] = app_type;
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301306 app_type_cfg[len++] = acdb_dev_id;
1307 app_type_cfg[len++] = sample_rate;
Siena Richard7c2db772016-12-21 11:32:34 -08001308 if (snd_device_be_idx > 0)
1309 app_type_cfg[len++] = snd_device_be_idx;
1310 ALOGI("%s default app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
1311 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Ashish Jainc02430d2016-01-04 10:42:43 +05301312 }
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301313
Siddartha Shaik343abc62017-08-08 11:15:25 +05301314 if(ctl)
1315 mixer_ctl_set_array(ctl, app_type_cfg, len);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001316 rc = 0;
1317exit_send_app_type_cfg:
1318 return rc;
1319}
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001320
Deeraj Soman14230922019-01-30 16:39:30 +05301321static int audio_extn_utils_check_input_parameters(uint32_t sample_rate,
1322 audio_format_t format,
1323 int channel_count)
1324{
1325 int ret = 0;
1326
1327 if (((format != AUDIO_FORMAT_PCM_16_BIT) && (format != AUDIO_FORMAT_PCM_8_24_BIT) &&
1328 (format != AUDIO_FORMAT_PCM_24_BIT_PACKED) && (format != AUDIO_FORMAT_PCM_32_BIT) &&
1329 (format != AUDIO_FORMAT_PCM_FLOAT)) &&
1330 !voice_extn_compress_voip_is_format_supported(format) &&
1331 !audio_extn_compr_cap_format_supported(format) &&
1332 !audio_extn_cin_format_supported(format))
1333 ret = -EINVAL;
1334
1335 switch (channel_count) {
1336 case 1:
1337 case 2:
1338 case 3:
1339 case 4:
1340 case 6:
1341 case 8:
1342 break;
1343 default:
1344 ret = -EINVAL;
1345 }
1346
1347 switch (sample_rate) {
1348 case 8000:
1349 case 11025:
1350 case 12000:
1351 case 16000:
1352 case 22050:
1353 case 24000:
1354 case 32000:
1355 case 44100:
1356 case 48000:
1357 case 88200:
1358 case 96000:
1359 case 176400:
1360 case 192000:
1361 break;
1362 default:
1363 ret = -EINVAL;
1364 }
1365
1366 return ret;
1367}
1368
1369static inline uint32_t audio_extn_utils_nearest_multiple(uint32_t num, uint32_t multiplier)
1370{
1371 uint32_t remainder = 0;
1372
1373 if (!multiplier)
1374 return num;
1375
1376 remainder = num % multiplier;
1377 if (remainder)
1378 num += (multiplier - remainder);
1379
1380 return num;
1381}
1382
1383static inline uint32_t audio_extn_utils_lcm(uint32_t num1, uint32_t num2)
1384{
1385 uint32_t high = num1, low = num2, temp = 0;
1386
1387 if (!num1 || !num2)
1388 return 0;
1389
1390 if (num1 < num2) {
1391 high = num2;
1392 low = num1;
1393 }
1394
1395 while (low != 0) {
1396 temp = low;
1397 low = high % low;
1398 high = temp;
1399 }
1400 return (num1 * num2)/high;
1401}
1402
Siena Richard7c2db772016-12-21 11:32:34 -08001403int audio_extn_utils_send_app_type_cfg(struct audio_device *adev,
1404 struct audio_usecase *usecase)
1405{
1406 int i, num_devices = 0;
1407 snd_device_t new_snd_devices[SND_DEVICE_OUT_END] = {0};
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301408 snd_device_t in_snd_device = usecase->in_snd_device;
Siena Richard7c2db772016-12-21 11:32:34 -08001409 int rc = 0;
1410
Aalique Grahame22e49102018-12-18 14:23:57 -08001411 if (usecase->type == PCM_HFP_CALL) {
1412 return audio_extn_utils_send_app_type_cfg_hfp(adev, usecase);
1413 }
1414
Siena Richard7c2db772016-12-21 11:32:34 -08001415 switch (usecase->type) {
1416 case PCM_PLAYBACK:
Surendar Karka93cd25a2018-08-28 14:21:37 +05301417 case TRANSCODE_LOOPBACK_RX:
Siena Richard7c2db772016-12-21 11:32:34 -08001418 ALOGD("%s: usecase->out_snd_device %s",
1419 __func__, platform_get_snd_device_name(usecase->out_snd_device));
1420 /* check for out combo device */
1421 if (platform_split_snd_device(adev->platform,
1422 usecase->out_snd_device,
1423 &num_devices, new_snd_devices)) {
1424 new_snd_devices[0] = usecase->out_snd_device;
1425 num_devices = 1;
1426 }
1427 break;
1428 case PCM_CAPTURE:
1429 ALOGD("%s: usecase->in_snd_device %s",
1430 __func__, platform_get_snd_device_name(usecase->in_snd_device));
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301431 if (voice_is_in_call_rec_stream(usecase->stream.in)) {
1432 in_snd_device = voice_get_incall_rec_backend_device(usecase->stream.in);
1433 }
Siena Richard7c2db772016-12-21 11:32:34 -08001434 /* check for in combo device */
1435 if (platform_split_snd_device(adev->platform,
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301436 in_snd_device,
Siena Richard7c2db772016-12-21 11:32:34 -08001437 &num_devices, new_snd_devices)) {
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301438 new_snd_devices[0] = in_snd_device;
Siena Richard7c2db772016-12-21 11:32:34 -08001439 num_devices = 1;
1440 }
1441 break;
1442 default:
1443 ALOGI("%s: not a playback/capture path, no need to cfg app type", __func__);
1444 rc = 0;
1445 break;
1446 }
1447
1448 for (i = 0; i < num_devices; i++) {
1449 rc = send_app_type_cfg_for_device(adev, usecase, new_snd_devices[i]);
1450 if (rc)
1451 break;
1452 }
1453
1454 return rc;
1455}
1456
Preetam Singh Ranawat9519e9c2015-11-18 16:05:55 +05301457int read_line_from_file(const char *path, char *buf, size_t count)
1458{
1459 char * fgets_ret;
1460 FILE * fd;
1461 int rv;
1462
1463 fd = fopen(path, "r");
1464 if (fd == NULL)
1465 return -1;
1466
1467 fgets_ret = fgets(buf, (int)count, fd);
1468 if (NULL != fgets_ret) {
1469 rv = (int)strlen(buf);
1470 } else {
1471 rv = ferror(fd);
1472 }
1473 fclose(fd);
1474
1475 return rv;
1476}
1477
Ashish Jainf1eaa582016-05-23 20:54:24 +05301478/*Translates ALSA formats to AOSP PCM formats*/
1479audio_format_t alsa_format_to_hal(uint32_t alsa_format)
1480{
1481 audio_format_t format;
1482
1483 switch(alsa_format) {
1484 case SNDRV_PCM_FORMAT_S16_LE:
1485 format = AUDIO_FORMAT_PCM_16_BIT;
1486 break;
1487 case SNDRV_PCM_FORMAT_S24_3LE:
1488 format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
1489 break;
1490 case SNDRV_PCM_FORMAT_S24_LE:
1491 format = AUDIO_FORMAT_PCM_8_24_BIT;
1492 break;
1493 case SNDRV_PCM_FORMAT_S32_LE:
1494 format = AUDIO_FORMAT_PCM_32_BIT;
1495 break;
1496 default:
1497 ALOGW("Incorrect ALSA format");
1498 format = AUDIO_FORMAT_INVALID;
1499 }
1500 return format;
1501}
1502
1503/*Translates hal format (AOSP) to alsa formats*/
1504uint32_t hal_format_to_alsa(audio_format_t hal_format)
1505{
1506 uint32_t alsa_format;
1507
1508 switch (hal_format) {
1509 case AUDIO_FORMAT_PCM_32_BIT: {
1510 if (platform_supports_true_32bit())
1511 alsa_format = SNDRV_PCM_FORMAT_S32_LE;
1512 else
1513 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1514 }
1515 break;
1516 case AUDIO_FORMAT_PCM_8_BIT:
1517 alsa_format = SNDRV_PCM_FORMAT_S8;
1518 break;
1519 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
1520 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1521 break;
1522 case AUDIO_FORMAT_PCM_8_24_BIT: {
1523 if (platform_supports_true_32bit())
1524 alsa_format = SNDRV_PCM_FORMAT_S32_LE;
1525 else
1526 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1527 }
1528 break;
1529 case AUDIO_FORMAT_PCM_FLOAT:
1530 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1531 break;
1532 default:
1533 case AUDIO_FORMAT_PCM_16_BIT:
1534 alsa_format = SNDRV_PCM_FORMAT_S16_LE;
1535 break;
1536 }
1537 return alsa_format;
1538}
1539
Ashish Jain83a6cc22016-06-28 14:34:17 +05301540/*Translates PCM formats to AOSP formats*/
1541audio_format_t pcm_format_to_hal(uint32_t pcm_format)
1542{
1543 audio_format_t format = AUDIO_FORMAT_INVALID;
1544
1545 switch(pcm_format) {
1546 case PCM_FORMAT_S16_LE:
1547 format = AUDIO_FORMAT_PCM_16_BIT;
1548 break;
1549 case PCM_FORMAT_S24_3LE:
1550 format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
1551 break;
1552 case PCM_FORMAT_S24_LE:
1553 format = AUDIO_FORMAT_PCM_8_24_BIT;
1554 break;
1555 case PCM_FORMAT_S32_LE:
1556 format = AUDIO_FORMAT_PCM_32_BIT;
1557 break;
1558 default:
1559 ALOGW("Incorrect PCM format");
1560 format = AUDIO_FORMAT_INVALID;
1561 }
1562 return format;
1563}
1564
1565/*Translates hal format (AOSP) to alsa formats*/
1566uint32_t hal_format_to_pcm(audio_format_t hal_format)
1567{
1568 uint32_t pcm_format;
1569
1570 switch (hal_format) {
1571 case AUDIO_FORMAT_PCM_32_BIT:
1572 case AUDIO_FORMAT_PCM_8_24_BIT:
1573 case AUDIO_FORMAT_PCM_FLOAT: {
1574 if (platform_supports_true_32bit())
1575 pcm_format = PCM_FORMAT_S32_LE;
1576 else
1577 pcm_format = PCM_FORMAT_S24_3LE;
1578 }
1579 break;
1580 case AUDIO_FORMAT_PCM_8_BIT:
1581 pcm_format = PCM_FORMAT_S8;
1582 break;
1583 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
1584 pcm_format = PCM_FORMAT_S24_3LE;
1585 break;
1586 default:
1587 case AUDIO_FORMAT_PCM_16_BIT:
1588 pcm_format = PCM_FORMAT_S16_LE;
1589 break;
1590 }
1591 return pcm_format;
1592}
1593
Ashish Jainf1eaa582016-05-23 20:54:24 +05301594uint32_t get_alsa_fragment_size(uint32_t bytes_per_sample,
1595 uint32_t sample_rate,
Deeraj Soman65358ab2019-02-07 15:40:49 +05301596 uint32_t noOfChannels,
1597 int64_t duration_ms)
Ashish Jainf1eaa582016-05-23 20:54:24 +05301598{
1599 uint32_t fragment_size = 0;
1600 uint32_t pcm_offload_time = PCM_OFFLOAD_BUFFER_DURATION;
1601
Deeraj Soman65358ab2019-02-07 15:40:49 +05301602 if (duration_ms >= MIN_OFFLOAD_BUFFER_DURATION_MS && duration_ms <= MAX_OFFLOAD_BUFFER_DURATION_MS)
1603 pcm_offload_time = duration_ms;
1604
Ashish Jainf1eaa582016-05-23 20:54:24 +05301605 fragment_size = (pcm_offload_time
1606 * sample_rate
1607 * bytes_per_sample
1608 * noOfChannels)/1000;
1609 if (fragment_size < MIN_PCM_OFFLOAD_FRAGMENT_SIZE)
1610 fragment_size = MIN_PCM_OFFLOAD_FRAGMENT_SIZE;
1611 else if (fragment_size > MAX_PCM_OFFLOAD_FRAGMENT_SIZE)
1612 fragment_size = MAX_PCM_OFFLOAD_FRAGMENT_SIZE;
1613 /*To have same PCM samples for all channels, the buffer size requires to
1614 *be multiple of (number of channels * bytes per sample)
1615 *For writes to succeed, the buffer must be written at address which is multiple of 32
1616 */
Aalique Grahameb85f2d92017-10-16 17:53:23 -07001617 fragment_size = ALIGN(fragment_size, (bytes_per_sample * noOfChannels * 32));
Ashish Jainf1eaa582016-05-23 20:54:24 +05301618
1619 ALOGI("PCM offload Fragment size to %d bytes", fragment_size);
1620 return fragment_size;
1621}
1622
1623/* Calculates the fragment size required to configure compress session.
1624 * Based on the alsa format selected, decide if conversion is needed in
1625
1626 * HAL ( e.g. convert AUDIO_FORMAT_PCM_FLOAT input format to
1627 * AUDIO_FORMAT_PCM_24_BIT_PACKED before writing to the compress driver.
1628 */
1629void audio_extn_utils_update_direct_pcm_fragment_size(struct stream_out *out)
1630{
Ashish Jain83a6cc22016-06-28 14:34:17 +05301631 audio_format_t dst_format = out->hal_op_format;
1632 audio_format_t src_format = out->hal_ip_format;
Ashish Jainf1eaa582016-05-23 20:54:24 +05301633 uint32_t hal_op_bytes_per_sample = audio_bytes_per_sample(dst_format);
1634 uint32_t hal_ip_bytes_per_sample = audio_bytes_per_sample(src_format);
1635
1636 out->compr_config.fragment_size =
1637 get_alsa_fragment_size(hal_op_bytes_per_sample,
1638 out->sample_rate,
Deeraj Soman65358ab2019-02-07 15:40:49 +05301639 popcount(out->channel_mask),
1640 out->info.duration_us / 1000);
Ashish Jainf1eaa582016-05-23 20:54:24 +05301641
1642 if ((src_format != dst_format) &&
1643 hal_op_bytes_per_sample != hal_ip_bytes_per_sample) {
1644
Ashish Jain83a6cc22016-06-28 14:34:17 +05301645 out->hal_fragment_size =
Ashish Jainf1eaa582016-05-23 20:54:24 +05301646 ((out->compr_config.fragment_size * hal_ip_bytes_per_sample) /
1647 hal_op_bytes_per_sample);
1648 ALOGI("enable conversion hal_input_fragment_size is %d src_format %x dst_format %x",
Ashish Jain83a6cc22016-06-28 14:34:17 +05301649 out->hal_fragment_size, src_format, dst_format);
Ashish Jainf1eaa582016-05-23 20:54:24 +05301650 } else {
Ashish Jain83a6cc22016-06-28 14:34:17 +05301651 out->hal_fragment_size = out->compr_config.fragment_size;
Ashish Jainf1eaa582016-05-23 20:54:24 +05301652 }
1653}
1654
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301655/* converts pcm format 24_8 to 8_24 inplace */
1656size_t audio_extn_utils_convert_format_24_8_to_8_24(void *buf, size_t bytes)
1657{
1658 size_t i = 0;
1659 int *int_buf_stream = buf;
1660
1661 if ((bytes % 4) != 0) {
1662 ALOGE("%s: wrong inout buffer! ... is not 32 bit aligned ", __func__);
1663 return -EINVAL;
1664 }
1665
1666 for (; i < (bytes / 4); i++)
1667 int_buf_stream[i] >>= 8;
1668
1669 return bytes;
1670}
1671
1672int get_snd_codec_id(audio_format_t format)
1673{
1674 int id = 0;
1675
1676 switch (format & AUDIO_FORMAT_MAIN_MASK) {
1677 case AUDIO_FORMAT_MP3:
1678 id = SND_AUDIOCODEC_MP3;
1679 break;
1680 case AUDIO_FORMAT_AAC:
1681 id = SND_AUDIOCODEC_AAC;
1682 break;
1683 case AUDIO_FORMAT_AAC_ADTS:
1684 id = SND_AUDIOCODEC_AAC;
1685 break;
Arun Kumar Dasari3b174182016-12-27 13:01:14 +05301686 case AUDIO_FORMAT_AAC_LATM:
1687 id = SND_AUDIOCODEC_AAC;
1688 break;
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301689 case AUDIO_FORMAT_PCM:
1690 id = SND_AUDIOCODEC_PCM;
1691 break;
1692 case AUDIO_FORMAT_FLAC:
1693 id = SND_AUDIOCODEC_FLAC;
1694 break;
1695 case AUDIO_FORMAT_ALAC:
1696 id = SND_AUDIOCODEC_ALAC;
1697 break;
1698 case AUDIO_FORMAT_APE:
1699 id = SND_AUDIOCODEC_APE;
1700 break;
1701 case AUDIO_FORMAT_VORBIS:
1702 id = SND_AUDIOCODEC_VORBIS;
1703 break;
1704 case AUDIO_FORMAT_WMA:
1705 id = SND_AUDIOCODEC_WMA;
1706 break;
1707 case AUDIO_FORMAT_WMA_PRO:
1708 id = SND_AUDIOCODEC_WMA_PRO;
1709 break;
Satish Babu Patakokila0c313922016-12-08 12:07:08 +05301710 case AUDIO_FORMAT_MP2:
1711 id = SND_AUDIOCODEC_MP2;
1712 break;
Satish Babu Patakokila915ecba2017-01-10 17:43:56 +05301713 case AUDIO_FORMAT_AC3:
1714 id = SND_AUDIOCODEC_AC3;
1715 break;
1716 case AUDIO_FORMAT_E_AC3:
1717 case AUDIO_FORMAT_E_AC3_JOC:
1718 id = SND_AUDIOCODEC_EAC3;
1719 break;
1720 case AUDIO_FORMAT_DTS:
1721 case AUDIO_FORMAT_DTS_HD:
1722 id = SND_AUDIOCODEC_DTS;
1723 break;
Ben Romberger1aaaf862017-04-06 17:49:46 -07001724 case AUDIO_FORMAT_DOLBY_TRUEHD:
1725 id = SND_AUDIOCODEC_TRUEHD;
1726 break;
Naresh Tanniru928f0862017-04-07 16:44:23 -07001727 case AUDIO_FORMAT_IEC61937:
1728 id = SND_AUDIOCODEC_IEC61937;
1729 break;
Preetam Singh Ranawat4277a5a2017-01-18 19:02:24 +05301730 case AUDIO_FORMAT_DSD:
1731 id = SND_AUDIOCODEC_DSD;
1732 break;
Dhanalakshmi Siddani18737932016-11-29 17:33:17 +05301733 case AUDIO_FORMAT_APTX:
1734 id = SND_AUDIOCODEC_APTX;
1735 break;
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301736 default:
1737 ALOGE("%s: Unsupported audio format :%x", __func__, format);
1738 }
1739
1740 return id;
1741}
1742
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001743void audio_extn_utils_send_audio_calibration(struct audio_device *adev,
1744 struct audio_usecase *usecase)
1745{
1746 int type = usecase->type;
1747
Dhananjay Kumar14245982017-01-16 20:21:00 +05301748 if (type == PCM_PLAYBACK && usecase->stream.out != NULL) {
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05301749 platform_send_audio_calibration(adev->platform, usecase,
Zhou Song06761dd2019-04-28 18:08:17 +08001750 usecase->stream.out->app_type_cfg.app_type);
Dhananjay Kumar14245982017-01-16 20:21:00 +05301751 } else if (type == PCM_CAPTURE && usecase->stream.in != NULL) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301752 platform_send_audio_calibration(adev->platform, usecase,
Zhou Song06761dd2019-04-28 18:08:17 +08001753 usecase->stream.in->app_type_cfg.app_type);
1754 } else if ((type == PCM_HFP_CALL) || (type == PCM_CAPTURE) ||
1755 (type == TRANSCODE_LOOPBACK_RX && usecase->stream.inout != NULL)) {
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05301756 platform_send_audio_calibration(adev->platform, usecase,
Zhou Song06761dd2019-04-28 18:08:17 +08001757 platform_get_default_app_type_v2(adev->platform, usecase->type));
Vidyakumar Athotae57f6002017-03-01 13:13:16 -08001758 } else {
1759 /* No need to send audio calibration for voice and voip call usecases */
1760 if ((type != VOICE_CALL) && (type != VOIP_CALL))
1761 ALOGW("%s: No audio calibration for usecase type = %d", __func__, type);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001762 }
1763}
1764
Ben Rombergera04fabc2014-11-14 12:16:03 -08001765// Base64 Encode and Decode
1766// Not all features supported. This must be used only with following conditions.
1767// Decode Modes: Support with and without padding
1768// CRLF not handling. So no CRLF in string to decode.
1769// Encode Modes: Supports only padding
1770int b64decode(char *inp, int ilen, uint8_t* outp)
1771{
1772 int i, j, k, ii, num;
1773 int rem, pcnt;
1774 uint32_t res=0;
1775 uint8_t getIndex[MAX_BASEINDEX_LEN];
1776 uint8_t tmp, cflag;
1777
1778 if(inp == NULL || outp == NULL || ilen <= 0) {
1779 ALOGE("[%s] received NULL pointer or zero length",__func__);
1780 return -1;
1781 }
1782
1783 memset(getIndex, MAX_BASEINDEX_LEN-1, sizeof(getIndex));
1784 for(i=0;i<BASE_TABLE_SIZE;i++) {
1785 getIndex[(uint8_t)bTable[i]] = (uint8_t)i;
1786 }
1787 getIndex[(uint8_t)'=']=0;
1788
1789 j=0;k=0;
1790 num = ilen/4;
1791 rem = ilen%4;
1792 if(rem==0)
1793 num = num-1;
1794 cflag=0;
1795 for(i=0; i<num; i++) {
1796 res=0;
1797 for(ii=0;ii<4;ii++) {
1798 res = res << 6;
1799 tmp = getIndex[(uint8_t)inp[j++]];
1800 res = res | tmp;
1801 cflag = cflag | tmp;
1802 }
1803 outp[k++] = (res >> 16)&0xFF;
1804 outp[k++] = (res >> 8)&0xFF;
1805 outp[k++] = res & 0xFF;
1806 }
1807
1808 // Handle last bytes special
1809 pcnt=0;
1810 if(rem == 0) {
1811 //With padding or full data
1812 res = 0;
1813 for(ii=0;ii<4;ii++) {
1814 if(inp[j] == '=')
1815 pcnt++;
1816 res = res << 6;
1817 tmp = getIndex[(uint8_t)inp[j++]];
1818 res = res | tmp;
1819 cflag = cflag | tmp;
1820 }
1821 outp[k++] = res >> 16;
1822 if(pcnt == 2)
1823 goto done;
1824 outp[k++] = (res>>8)&0xFF;
1825 if(pcnt == 1)
1826 goto done;
1827 outp[k++] = res&0xFF;
1828 } else {
1829 //without padding
1830 res = 0;
1831 for(i=0;i<rem;i++) {
1832 res = res << 6;
1833 tmp = getIndex[(uint8_t)inp[j++]];
1834 res = res | tmp;
1835 cflag = cflag | tmp;
1836 }
1837 for(i=rem;i<4;i++) {
1838 res = res << 6;
1839 pcnt++;
1840 }
1841 outp[k++] = res >> 16;
1842 if(pcnt == 2)
1843 goto done;
1844 outp[k++] = (res>>8)&0xFF;
1845 if(pcnt == 1)
1846 goto done;
1847 outp[k++] = res&0xFF;
1848 }
1849done:
1850 if(cflag == 0xFF) {
1851 ALOGE("[%s] base64 decode failed. Invalid character found %s",
1852 __func__, inp);
1853 return 0;
1854 }
1855 return k;
1856}
1857
1858int b64encode(uint8_t *inp, int ilen, char* outp)
1859{
1860 int i,j,k, num;
1861 int rem=0;
1862 uint32_t res=0;
1863
1864 if(inp == NULL || outp == NULL || ilen<=0) {
1865 ALOGE("[%s] received NULL pointer or zero input length",__func__);
1866 return -1;
1867 }
1868
1869 num = ilen/3;
1870 rem = ilen%3;
1871 j=0;k=0;
1872 for(i=0; i<num; i++) {
1873 //prepare index
1874 res = inp[j++]<<16;
1875 res = res | inp[j++]<<8;
1876 res = res | inp[j++];
1877 //get output map from index
1878 outp[k++] = (char) bTable[(res>>18)&0x3F];
1879 outp[k++] = (char) bTable[(res>>12)&0x3F];
1880 outp[k++] = (char) bTable[(res>>6)&0x3F];
1881 outp[k++] = (char) bTable[res&0x3F];
1882 }
1883
1884 switch(rem) {
1885 case 1:
1886 res = inp[j++]<<16;
1887 outp[k++] = (char) bTable[res>>18];
1888 outp[k++] = (char) bTable[(res>>12)&0x3F];
1889 //outp[k++] = '=';
1890 //outp[k++] = '=';
1891 break;
1892 case 2:
1893 res = inp[j++]<<16;
1894 res = res | inp[j++]<<8;
1895 outp[k++] = (char) bTable[res>>18];
1896 outp[k++] = (char) bTable[(res>>12)&0x3F];
1897 outp[k++] = (char) bTable[(res>>6)&0x3F];
1898 //outp[k++] = '=';
1899 break;
1900 default:
1901 break;
1902 }
Ben Rombergera04fabc2014-11-14 12:16:03 -08001903 outp[k] = '\0';
1904 return k;
1905}
Ashish Jain81eb2a82015-05-13 10:52:34 +05301906
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301907
1908int audio_extn_utils_get_codec_version(const char *snd_card_name,
1909 int card_num,
1910 char *codec_version)
1911{
1912 char procfs_path[50];
1913 FILE *fp;
1914
1915 if (strstr(snd_card_name, "tasha")) {
1916 snprintf(procfs_path, sizeof(procfs_path),
1917 "/proc/asound/card%d/codecs/tasha/version", card_num);
1918 if ((fp = fopen(procfs_path, "r")) != NULL) {
1919 fgets(codec_version, CODEC_VERSION_MAX_LENGTH, fp);
1920 fclose(fp);
1921 } else {
1922 ALOGE("%s: ERROR. cannot open %s", __func__, procfs_path);
1923 return -ENOENT;
1924 }
1925 ALOGD("%s: codec version %s", __func__, codec_version);
1926 }
1927
1928 return 0;
1929}
1930
Preetam Singh Ranawat9d0d8e42019-07-15 12:27:25 +05301931int audio_extn_utils_get_codec_variant(int card_num,
1932 char *codec_variant)
1933{
1934 char procfs_path[50];
1935 FILE *fp;
1936 snprintf(procfs_path, sizeof(procfs_path),
1937 "/proc/asound/card%d/codecs/wcd938x/variant", card_num);
1938 if ((fp = fopen(procfs_path, "r")) == NULL) {
1939 snprintf(procfs_path, sizeof(procfs_path),
1940 "/proc/asound/card%d/codecs/wcd937x/variant", card_num);
1941 if ((fp = fopen(procfs_path, "r")) == NULL) {
1942 ALOGE("%s: ERROR. cannot open %s", __func__, procfs_path);
1943 return -ENOENT;
1944 }
1945 }
1946 fgets(codec_variant, CODEC_VARIANT_MAX_LENGTH, fp);
1947 fclose(fp);
1948 ALOGD("%s: codec variant is %s", __func__, codec_variant);
1949 return 0;
1950}
1951
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301952
Ashish Jain81eb2a82015-05-13 10:52:34 +05301953#ifdef AUDIO_EXTERNAL_HDMI_ENABLED
1954
1955void get_default_compressed_channel_status(
1956 unsigned char *channel_status)
1957{
Ashish Jain81eb2a82015-05-13 10:52:34 +05301958 memset(channel_status,0,24);
1959
1960 /* block start bit in preamble bit 3 */
1961 channel_status[0] |= PROFESSIONAL;
1962 //compre out
1963 channel_status[0] |= NON_LPCM;
1964 // sample rate; fixed 48K for default/transcode
1965 channel_status[3] |= SR_48000;
1966}
1967
Ashish Jain81eb2a82015-05-13 10:52:34 +05301968int32_t get_compressed_channel_status(void *audio_stream_data,
1969 uint32_t audio_frame_size,
1970 unsigned char *channel_status,
1971 enum audio_parser_code_type codec_type)
1972 // codec_type - AUDIO_PARSER_CODEC_AC3
1973 // - AUDIO_PARSER_CODEC_DTS
1974{
1975 unsigned char *stream;
1976 int ret = 0;
1977 stream = (unsigned char *)audio_stream_data;
1978
1979 if (audio_stream_data == NULL || audio_frame_size == 0) {
1980 ALOGW("no buffer to get channel status, return default for compress");
1981 get_default_compressed_channel_status(channel_status);
1982 return ret;
1983 }
1984
1985 memset(channel_status,0,24);
1986 if(init_audio_parser(stream, audio_frame_size, codec_type) == -1)
1987 {
1988 ALOGE("init audio parser failed");
1989 return -1;
1990 }
1991 ret = get_channel_status(channel_status, codec_type);
1992 return ret;
1993
1994}
1995
Ashish Jain81eb2a82015-05-13 10:52:34 +05301996void get_lpcm_channel_status(uint32_t sampleRate,
1997 unsigned char *channel_status)
1998{
1999 int32_t status = 0;
Ashish Jain81eb2a82015-05-13 10:52:34 +05302000 memset(channel_status,0,24);
2001 /* block start bit in preamble bit 3 */
2002 channel_status[0] |= PROFESSIONAL;
2003 //LPCM OUT
2004 channel_status[0] &= ~NON_LPCM;
2005
2006 switch (sampleRate) {
2007 case 8000:
2008 case 11025:
2009 case 12000:
2010 case 16000:
2011 case 22050:
2012 channel_status[3] |= SR_NOTID;
Preetam Singh Ranawat61716b12015-12-14 11:55:24 +05302013 break;
Ashish Jain81eb2a82015-05-13 10:52:34 +05302014 case 24000:
2015 channel_status[3] |= SR_24000;
2016 break;
2017 case 32000:
2018 channel_status[3] |= SR_32000;
2019 break;
2020 case 44100:
2021 channel_status[3] |= SR_44100;
2022 break;
2023 case 48000:
2024 channel_status[3] |= SR_48000;
2025 break;
2026 case 88200:
2027 channel_status[3] |= SR_88200;
2028 break;
2029 case 96000:
2030 channel_status[3] |= SR_96000;
2031 break;
2032 case 176400:
2033 channel_status[3] |= SR_176400;
2034 break;
2035 case 192000:
2036 channel_status[3] |= SR_192000;
2037 break;
2038 default:
2039 ALOGV("Invalid sample_rate %u\n", sampleRate);
2040 status = -1;
2041 break;
2042 }
2043}
2044
2045void audio_utils_set_hdmi_channel_status(struct stream_out *out, char * buffer, size_t bytes)
2046{
2047 unsigned char channel_status[24]={0};
2048 struct snd_aes_iec958 iec958;
2049 const char *mixer_ctl_name = "IEC958 Playback PCM Stream";
2050 struct mixer_ctl *ctl;
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +05302051 ALOGV("%s: buffer %s bytes %zd", __func__, buffer, bytes);
Arun Mirpurie008ed22019-03-21 11:21:04 -07002052
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05302053 if (audio_extn_utils_is_dolby_format(out->format) &&
Ashish Jain81eb2a82015-05-13 10:52:34 +05302054 /*TODO:Extend code to support DTS passthrough*/
2055 /*set compressed channel status bits*/
Arun Mirpurie008ed22019-03-21 11:21:04 -07002056 audio_extn_passthru_is_passthrough_stream(out) &&
2057 audio_extn_is_hdmi_passthru_enabled()) {
Ashish Jain81eb2a82015-05-13 10:52:34 +05302058 get_compressed_channel_status(buffer, bytes, channel_status, AUDIO_PARSER_CODEC_AC3);
Arun Mirpurie008ed22019-03-21 11:21:04 -07002059 } else {
Ashish Jain81eb2a82015-05-13 10:52:34 +05302060 /*set channel status bit for LPCM*/
2061 get_lpcm_channel_status(out->sample_rate, channel_status);
2062 }
2063
2064 memcpy(iec958.status, channel_status,sizeof(iec958.status));
2065 ctl = mixer_get_ctl_by_name(out->dev->mixer, mixer_ctl_name);
2066 if (!ctl) {
2067 ALOGE("%s: Could not get ctl for mixer cmd - %s",
2068 __func__, mixer_ctl_name);
2069 return;
2070 }
2071 if (mixer_ctl_set_array(ctl, &iec958, sizeof(iec958)) < 0) {
2072 ALOGE("%s: Could not set channel status for ext HDMI ",
2073 __func__);
2074 return;
2075 }
2076
2077}
2078#endif
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302079
2080int audio_extn_utils_get_avt_device_drift(
2081 struct audio_usecase *usecase,
2082 struct audio_avt_device_drift_param *drift_param)
2083{
2084 int ret = 0, count = 0;
2085 char avt_device_drift_mixer_ctl_name[MIXER_PATH_MAX_LENGTH] = {0};
Naresh Tanniru6160c712017-04-17 15:43:48 +05302086 const char *backend = NULL;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302087 struct mixer_ctl *ctl = NULL;
2088 struct audio_avt_device_drift_stats drift_stats;
2089 struct audio_device *adev = NULL;
2090
2091 if (usecase != NULL && usecase->type == PCM_PLAYBACK) {
Naresh Tanniru6160c712017-04-17 15:43:48 +05302092 backend = platform_get_snd_device_backend_interface(usecase->out_snd_device);
2093 if (!backend) {
2094 ALOGE("%s: Unsupported device %d", __func__,
2095 usecase->stream.out->devices);
2096 ret = -EINVAL;
2097 goto done;
2098 }
2099 strlcpy(avt_device_drift_mixer_ctl_name,
2100 backend,
2101 MIXER_PATH_MAX_LENGTH);
2102
2103 count = strlen(backend);
2104 if (MIXER_PATH_MAX_LENGTH - count > 0) {
2105 strlcat(&avt_device_drift_mixer_ctl_name[count],
2106 " DRIFT",
2107 MIXER_PATH_MAX_LENGTH - count);
2108 } else {
2109 ret = -EINVAL;
2110 goto done;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302111 }
2112 } else {
Naresh Tanniru7586e292017-04-13 10:38:13 +05302113 ALOGE("%s: Invalid usecase",__func__);
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302114 ret = -EINVAL;
Naresh Tanniru6160c712017-04-17 15:43:48 +05302115 goto done;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302116 }
2117
Naresh Tanniru6160c712017-04-17 15:43:48 +05302118 adev = usecase->stream.out->dev;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302119 ctl = mixer_get_ctl_by_name(adev->mixer, avt_device_drift_mixer_ctl_name);
2120 if (!ctl) {
2121 ALOGE("%s: Could not get ctl for mixer cmd - %s",
2122 __func__, avt_device_drift_mixer_ctl_name);
2123
2124 ret = -EINVAL;
2125 goto done;
2126 }
2127
2128 ALOGV("%s: Getting AV Timer vs Device Drift mixer ctrl name %s", __func__,
2129 avt_device_drift_mixer_ctl_name);
2130
2131 mixer_ctl_update(ctl);
2132 count = mixer_ctl_get_num_values(ctl);
2133 if (count != sizeof(struct audio_avt_device_drift_stats)) {
2134 ALOGE("%s: mixer_ctl_get_num_values() invalid drift_stats data size",
2135 __func__);
2136
2137 ret = -EINVAL;
2138 goto done;
2139 }
2140
2141 ret = mixer_ctl_get_array(ctl, (void *)&drift_stats, count);
2142 if (ret != 0) {
2143 ALOGE("%s: mixer_ctl_get_array() failed to get drift_stats Params",
2144 __func__);
2145
2146 ret = -EINVAL;
2147 goto done;
2148 }
2149 memcpy(drift_param, &drift_stats.drift_param,
2150 sizeof(struct audio_avt_device_drift_param));
2151done:
2152 return ret;
2153}
Manish Dewangan07de2142017-02-27 19:27:20 +05302154
2155#ifdef SNDRV_COMPRESS_PATH_DELAY
2156int audio_extn_utils_compress_get_dsp_latency(struct stream_out *out)
2157{
2158 int ret = -EINVAL;
2159 struct snd_compr_metadata metadata;
2160 int delay_ms = COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
2161
Weiyin Jiangc49a3b52018-08-17 16:16:08 +08002162 /* override the latency for pcm offload use case */
2163 if ((out->flags & AUDIO_OUTPUT_FLAG_DIRECT) &&
2164 !(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)) {
2165 delay_ms = PCM_OFFLOAD_PLAYBACK_LATENCY;
2166 }
2167
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -07002168 if (property_get_bool("vendor.audio.playback.dsp.pathdelay", false)) {
Manish Dewangan07de2142017-02-27 19:27:20 +05302169 ALOGD("%s:: Quering DSP delay %d",__func__, __LINE__);
2170 if (!(is_offload_usecase(out->usecase))) {
2171 ALOGE("%s:: not supported for non offload session", __func__);
2172 goto exit;
2173 }
2174
2175 if (!out->compr) {
2176 ALOGD("%s:: Invalid compress handle,returning default dsp latency",
2177 __func__);
2178 goto exit;
2179 }
2180
2181 metadata.key = SNDRV_COMPRESS_PATH_DELAY;
2182 ret = compress_get_metadata(out->compr, &metadata);
2183 if(ret) {
2184 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2185 goto exit;
2186 }
2187 delay_ms = metadata.value[0] / 1000; /*convert to ms*/
2188 } else {
2189 ALOGD("%s:: Using Fix DSP delay",__func__);
2190 }
2191
2192exit:
2193 ALOGD("%s:: delay in ms is %d",__func__, delay_ms);
2194 return delay_ms;
2195}
2196#else
2197int audio_extn_utils_compress_get_dsp_latency(struct stream_out *out __unused)
2198{
2199 return COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
2200}
2201#endif
Manish Dewangan69426c82017-01-30 17:35:36 +05302202
2203#ifdef SNDRV_COMPRESS_RENDER_MODE
2204int audio_extn_utils_compress_set_render_mode(struct stream_out *out)
2205{
2206 struct snd_compr_metadata metadata;
2207 int ret = -EINVAL;
2208
2209 if (!(is_offload_usecase(out->usecase))) {
2210 ALOGE("%s:: not supported for non offload session", __func__);
2211 goto exit;
2212 }
2213
2214 if (!out->compr) {
2215 ALOGD("%s:: Invalid compress handle",
2216 __func__);
2217 goto exit;
2218 }
2219
2220 ALOGD("%s:: render mode %d", __func__, out->render_mode);
2221
2222 metadata.key = SNDRV_COMPRESS_RENDER_MODE;
2223 if (out->render_mode == RENDER_MODE_AUDIO_MASTER) {
2224 metadata.value[0] = SNDRV_COMPRESS_RENDER_MODE_AUDIO_MASTER;
2225 } else if (out->render_mode == RENDER_MODE_AUDIO_STC_MASTER) {
2226 metadata.value[0] = SNDRV_COMPRESS_RENDER_MODE_STC_MASTER;
2227 } else {
2228 ret = 0;
2229 goto exit;
2230 }
2231 ret = compress_set_metadata(out->compr, &metadata);
2232 if(ret) {
2233 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2234 }
2235exit:
2236 return ret;
2237}
2238#else
2239int audio_extn_utils_compress_set_render_mode(struct stream_out *out __unused)
2240{
2241 ALOGD("%s:: configuring render mode not supported", __func__);
2242 return 0;
2243}
2244#endif
Manish Dewangan58229382017-02-02 15:48:41 +05302245
2246#ifdef SNDRV_COMPRESS_CLK_REC_MODE
2247int audio_extn_utils_compress_set_clk_rec_mode(
2248 struct audio_usecase *usecase)
2249{
2250 struct snd_compr_metadata metadata;
2251 struct stream_out *out = NULL;
2252 int ret = -EINVAL;
2253
Naresh Tanniru7586e292017-04-13 10:38:13 +05302254 if (usecase == NULL || usecase->type != PCM_PLAYBACK) {
Manish Dewangan58229382017-02-02 15:48:41 +05302255 ALOGE("%s:: Invalid use case", __func__);
2256 goto exit;
2257 }
2258
2259 out = usecase->stream.out;
2260 if (!out) {
2261 ALOGE("%s:: invalid stream", __func__);
2262 goto exit;
2263 }
2264
2265 if (!is_offload_usecase(out->usecase)) {
2266 ALOGE("%s:: not supported for non offload session", __func__);
2267 goto exit;
2268 }
2269
2270 if (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER) {
2271 ALOGD("%s:: clk recovery is only supported in STC render mode",
2272 __func__);
2273 ret = 0;
2274 goto exit;
2275 }
2276
2277 if (!out->compr) {
2278 ALOGD("%s:: Invalid compress handle",
2279 __func__);
2280 goto exit;
2281 }
2282 metadata.key = SNDRV_COMPRESS_CLK_REC_MODE;
2283 switch(usecase->out_snd_device) {
2284 case SND_DEVICE_OUT_HDMI:
2285 case SND_DEVICE_OUT_SPEAKER_AND_HDMI:
2286 case SND_DEVICE_OUT_DISPLAY_PORT:
2287 case SND_DEVICE_OUT_SPEAKER_AND_DISPLAY_PORT:
2288 metadata.value[0] = SNDRV_COMPRESS_CLK_REC_MODE_NONE;
2289 break;
2290 default:
2291 metadata.value[0] = SNDRV_COMPRESS_CLK_REC_MODE_AUTO;
2292 break;
2293 }
2294
2295 ALOGD("%s:: clk recovery mode %d",__func__, metadata.value[0]);
2296
2297 ret = compress_set_metadata(out->compr, &metadata);
2298 if(ret) {
2299 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2300 }
2301
2302exit:
2303 return ret;
2304}
2305#else
2306int audio_extn_utils_compress_set_clk_rec_mode(
2307 struct audio_usecase *usecase __unused)
2308{
2309 ALOGD("%s:: configuring render mode not supported", __func__);
2310 return 0;
2311}
2312#endif
Manish Dewangan27346042017-03-01 12:56:12 +05302313
2314#ifdef SNDRV_COMPRESS_RENDER_WINDOW
2315int audio_extn_utils_compress_set_render_window(
2316 struct stream_out *out,
2317 struct audio_out_render_window_param *render_window)
2318{
2319 struct snd_compr_metadata metadata;
2320 int ret = -EINVAL;
2321
Manish Dewangan27346042017-03-01 12:56:12 +05302322 if(render_window == NULL) {
2323 ALOGE("%s:: Invalid render_window", __func__);
2324 goto exit;
2325 }
2326
Aniket Kumar Lata1e1d3662017-06-01 18:45:48 -07002327 ALOGD("%s:: render window start 0x%"PRIx64" end 0x%"PRIx64"",
2328 __func__,render_window->render_ws, render_window->render_we);
2329
Manish Dewangan27346042017-03-01 12:56:12 +05302330 if (!is_offload_usecase(out->usecase)) {
2331 ALOGE("%s:: not supported for non offload session", __func__);
2332 goto exit;
2333 }
2334
Naresh Tanniru6160c712017-04-17 15:43:48 +05302335 if ((out->render_mode != RENDER_MODE_AUDIO_MASTER) &&
2336 (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER)) {
Manish Dewangan27346042017-03-01 12:56:12 +05302337 ALOGD("%s:: only supported in timestamp mode, current "
2338 "render mode mode %d", __func__, out->render_mode);
2339 goto exit;
2340 }
2341
2342 if (!out->compr) {
2343 ALOGW("%s:: offload session not yet opened,"
2344 "render window will be configure later", __func__);
2345 /* store render window to reconfigure in start_output_stream() */
2346 goto exit;
2347 }
2348
2349 metadata.key = SNDRV_COMPRESS_RENDER_WINDOW;
2350 /*render window start value */
2351 metadata.value[0] = 0xFFFFFFFF & render_window->render_ws; /* lsb */
2352 metadata.value[1] = \
2353 (0xFFFFFFFF00000000 & render_window->render_ws) >> 32; /* msb*/
2354 /*render window end value */
2355 metadata.value[2] = 0xFFFFFFFF & render_window->render_we; /* lsb */
2356 metadata.value[3] = \
2357 (0xFFFFFFFF00000000 & render_window->render_we) >> 32; /* msb*/
2358
2359 ret = compress_set_metadata(out->compr, &metadata);
2360 if(ret) {
2361 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2362 }
2363
2364exit:
2365 return ret;
2366}
2367#else
2368int audio_extn_utils_compress_set_render_window(
2369 struct stream_out *out __unused,
2370 struct audio_out_render_window_param *render_window __unused)
2371{
2372 ALOGD("%s:: configuring render window not supported", __func__);
2373 return 0;
2374}
2375#endif
Manish Dewangan14956cc2017-02-14 18:54:42 +05302376
2377#ifdef SNDRV_COMPRESS_START_DELAY
2378int audio_extn_utils_compress_set_start_delay(
2379 struct stream_out *out,
2380 struct audio_out_start_delay_param *delay_param)
2381{
2382 struct snd_compr_metadata metadata;
2383 int ret = -EINVAL;
2384
2385 if(delay_param == NULL) {
2386 ALOGE("%s:: Invalid delay_param", __func__);
2387 goto exit;
2388 }
2389
2390 ALOGD("%s:: render start delay 0x%"PRIx64" ", __func__,
2391 delay_param->start_delay);
2392
2393 if (!is_offload_usecase(out->usecase)) {
2394 ALOGE("%s:: not supported for non offload session", __func__);
2395 goto exit;
2396 }
2397
Naresh Tanniru6160c712017-04-17 15:43:48 +05302398 if ((out->render_mode != RENDER_MODE_AUDIO_MASTER) &&
2399 (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER)) {
Manish Dewangan14956cc2017-02-14 18:54:42 +05302400 ALOGD("%s:: only supported in timestamp mode, current "
2401 "render mode mode %d", __func__, out->render_mode);
2402 goto exit;
2403 }
2404
2405 if (!out->compr) {
2406 ALOGW("%s:: offload session not yet opened,"
2407 "start delay will be configure later", __func__);
2408 goto exit;
2409 }
2410
2411 metadata.key = SNDRV_COMPRESS_START_DELAY;
2412 metadata.value[0] = 0xFFFFFFFF & delay_param->start_delay; /* lsb */
2413 metadata.value[1] = \
2414 (0xFFFFFFFF00000000 & delay_param->start_delay) >> 32; /* msb*/
2415
2416 ret = compress_set_metadata(out->compr, &metadata);
2417 if(ret) {
2418 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2419 }
2420
2421exit:
2422 return ret;
2423}
2424#else
2425int audio_extn_utils_compress_set_start_delay(
2426 struct stream_out *out __unused,
2427 struct audio_out_start_delay_param *delay_param __unused)
2428{
2429 ALOGD("%s:: configuring render window not supported", __func__);
2430 return 0;
2431}
2432#endif
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002433
Surendar Karka287348c2019-04-10 18:31:46 +05302434#ifdef SNDRV_COMPRESS_DSP_POSITION
2435int audio_extn_utils_compress_get_dsp_presentation_pos(struct stream_out *out,
2436 uint64_t *frames, struct timespec *timestamp, int32_t clock_id)
2437{
2438 int ret = -EINVAL;
2439 uint64_t *val = NULL;
2440 uint64_t time = 0;
2441 struct snd_compr_metadata metadata;
2442
2443 ALOGV("%s:: Quering DSP position with clock id %d",__func__, clock_id);
2444 metadata.key = SNDRV_COMPRESS_DSP_POSITION;
2445 metadata.value[0] = clock_id;
2446 ret = compress_get_metadata(out->compr, &metadata);
2447 if (ret) {
2448 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2449 ret = -errno;
2450 goto exit;
2451 }
2452 val = (uint64_t *)&metadata.value[1];
2453 *frames = *val;
2454 time = *(val + 1);
2455 timestamp->tv_sec = time / 1000000;
2456 timestamp->tv_nsec = (time % 1000000)*1000;
2457
2458exit:
2459 return ret;
2460}
2461#else
2462int audio_extn_utils_compress_get_dsp_presentation_pos(struct stream_out *out __unused,
2463 uint64_t *frames __unused, struct timespec *timestamp __unused,
2464 int32_t clock_id __unused)
2465{
2466 ALOGD("%s:: dsp presentation position not supported", __func__);
2467 return 0;
2468
2469}
2470#endif
2471
2472#ifdef SNDRV_PCM_IOCTL_DSP_POSITION
2473int audio_extn_utils_pcm_get_dsp_presentation_pos(struct stream_out *out,
2474 uint64_t *frames, struct timespec *timestamp, int32_t clock_id)
2475{
2476 int ret = -EINVAL;
2477 uint64_t time = 0;
2478 struct snd_pcm_prsnt_position prsnt_position;
2479
2480 ALOGV("%s:: Quering DSP position with clock id %d",__func__, clock_id);
2481 prsnt_position.clock_id = clock_id;
2482 ret = pcm_ioctl(out->pcm, SNDRV_PCM_IOCTL_DSP_POSITION, &prsnt_position);
2483 if (ret) {
2484 ALOGE("%s::error %d", __func__, ret);
2485 ret = -EIO;
2486 goto exit;
2487 }
2488
2489 *frames = prsnt_position.frames;
2490 time = prsnt_position.timestamp;
2491 timestamp->tv_sec = time / 1000000;
2492 timestamp->tv_nsec = (time % 1000000)*1000;
2493
2494exit:
2495 return ret;
2496}
2497#else
2498int audio_extn_utils_pcm_get_dsp_presentation_pos(struct stream_out *out __unused,
2499 uint64_t *frames __unused, struct timespec *timestamp __unused,
2500 int32_t clock_id __unused)
2501{
2502 ALOGD("%s:: dsp presentation position not supported", __func__);
2503 return 0;
2504
2505}
2506#endif
2507
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002508#define MAX_SND_CARD 8
Ashish Jain30ae8942017-11-05 17:21:23 +05302509#define RETRY_US 1000000
2510#define RETRY_NUMBER 40
Aalique Grahame22e49102018-12-18 14:23:57 -08002511#define PLATFORM_INFO_XML_PATH "audio_platform_info.xml"
2512#define PLATFORM_INFO_XML_BASE_STRING "audio_platform_info"
2513
2514#ifdef LINUX_ENABLED
2515static const char *kConfigLocationList[] =
2516 {"/etc"};
2517#else
2518static const char *kConfigLocationList[] =
2519 {"/vendor/etc"};
2520#endif
2521static const int kConfigLocationListSize =
2522 (sizeof(kConfigLocationList) / sizeof(kConfigLocationList[0]));
2523
2524bool audio_extn_utils_resolve_config_file(char file_name[MIXER_PATH_MAX_LENGTH])
2525{
2526 char full_config_path[MIXER_PATH_MAX_LENGTH];
2527 for (int i = 0; i < kConfigLocationListSize; i++) {
2528 snprintf(full_config_path,
2529 MIXER_PATH_MAX_LENGTH,
2530 "%s/%s",
2531 kConfigLocationList[i],
2532 file_name);
2533 if (F_OK == access(full_config_path, 0)) {
Weiyin Jiang2995f662019-04-17 14:25:12 +08002534 strlcpy(file_name, full_config_path, MIXER_PATH_MAX_LENGTH);
Aalique Grahame22e49102018-12-18 14:23:57 -08002535 return true;
2536 }
2537 }
2538 return false;
2539}
2540
2541/* platform_info_file should be size 'MIXER_PATH_MAX_LENGTH' */
2542int audio_extn_utils_get_platform_info(const char* snd_card_name, char* platform_info_file)
2543{
2544 if (NULL == snd_card_name) {
2545 return -1;
2546 }
2547
2548 struct snd_card_split *snd_split_handle = NULL;
2549 int ret = 0;
2550 audio_extn_set_snd_card_split(snd_card_name);
2551 snd_split_handle = audio_extn_get_snd_card_split();
2552
2553 snprintf(platform_info_file, MIXER_PATH_MAX_LENGTH, "%s_%s_%s.xml",
2554 PLATFORM_INFO_XML_BASE_STRING, snd_split_handle->snd_card,
2555 snd_split_handle->form_factor);
2556
2557 if (!audio_extn_utils_resolve_config_file(platform_info_file)) {
2558 memset(platform_info_file, 0, MIXER_PATH_MAX_LENGTH);
2559 snprintf(platform_info_file, MIXER_PATH_MAX_LENGTH, "%s_%s.xml",
2560 PLATFORM_INFO_XML_BASE_STRING, snd_split_handle->snd_card);
2561
2562 if (!audio_extn_utils_resolve_config_file(platform_info_file)) {
2563 memset(platform_info_file, 0, MIXER_PATH_MAX_LENGTH);
2564 strlcpy(platform_info_file, PLATFORM_INFO_XML_PATH, MIXER_PATH_MAX_LENGTH);
2565 ret = audio_extn_utils_resolve_config_file(platform_info_file) ? 0 : -1;
2566 }
2567 }
2568
2569 return ret;
2570}
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002571
2572int audio_extn_utils_get_snd_card_num()
2573{
Soumya Managoli9fee7c62018-04-06 16:21:50 +05302574 int snd_card_num = 0;
2575 struct mixer *mixer = NULL;
2576
2577 snd_card_num = audio_extn_utils_open_snd_mixer(&mixer);
2578 if (mixer)
2579 mixer_close(mixer);
2580 return snd_card_num;
2581}
2582
2583int audio_extn_utils_open_snd_mixer(struct mixer **mixer_handle)
2584{
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002585
2586 void *hw_info = NULL;
2587 struct mixer *mixer = NULL;
2588 int retry_num = 0;
mpangfaa7bae2019-01-15 16:38:13 +08002589 int snd_card_num = 0;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002590 char* snd_card_name = NULL;
mpangfaa7bae2019-01-15 16:38:13 +08002591 int snd_card_detection_info[MAX_SND_CARD] = {0};
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002592
Soumya Managoli9fee7c62018-04-06 16:21:50 +05302593 if (!mixer_handle) {
2594 ALOGE("invalid mixer handle");
2595 return -1;
2596 }
2597 *mixer_handle = NULL;
Ashish Jain30ae8942017-11-05 17:21:23 +05302598 /*
mpangfaa7bae2019-01-15 16:38:13 +08002599 * Try with all the sound cards ( 0 to 7 ) and if none of them were detected
Ashish Jain30ae8942017-11-05 17:21:23 +05302600 * sleep for 1 sec and try detections with sound card 0 again.
2601 * If sound card gets detected, check if it is relevant, if not check with the
2602 * other sound cards. To ensure that the irrelevant sound card is not check again,
2603 * we maintain it in min_snd_card_num.
2604 */
2605 while (retry_num < RETRY_NUMBER) {
mpangfaa7bae2019-01-15 16:38:13 +08002606 snd_card_num = 0;
2607 while (snd_card_num < MAX_SND_CARD) {
2608 if (snd_card_detection_info[snd_card_num] == 0) {
2609 mixer = mixer_open(snd_card_num);
2610 if (!mixer)
2611 snd_card_num++;
2612 else
2613 break;
2614 } else
2615 snd_card_num++;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002616 }
2617
2618 if (!mixer) {
Ashish Jain30ae8942017-11-05 17:21:23 +05302619 usleep(RETRY_US);
Ashish Jain30ae8942017-11-05 17:21:23 +05302620 retry_num++;
mpangfaa7bae2019-01-15 16:38:13 +08002621 ALOGD("%s: retry, retry_num %d", __func__, retry_num);
Ashish Jain30ae8942017-11-05 17:21:23 +05302622 continue;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002623 }
2624
2625 snd_card_name = strdup(mixer_get_name(mixer));
2626 if (!snd_card_name) {
2627 ALOGE("failed to allocate memory for snd_card_name\n");
2628 mixer_close(mixer);
2629 return -1;
2630 }
2631 ALOGD("%s: snd_card_name: %s", __func__, snd_card_name);
mpangfaa7bae2019-01-15 16:38:13 +08002632 snd_card_detection_info[snd_card_num] = 1;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002633 hw_info = hw_info_init(snd_card_name);
2634 if (hw_info) {
2635 ALOGD("%s: Opened sound card:%d", __func__, snd_card_num);
2636 break;
2637 }
mpangfaa7bae2019-01-15 16:38:13 +08002638 ALOGE("%s: Failed to init hardware info, snd_card_num:%d", __func__, snd_card_num);
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002639
Dhananjay Kumara7e27832017-07-11 15:40:39 +05302640 free(snd_card_name);
2641 snd_card_name = NULL;
2642
2643 mixer_close(mixer);
2644 mixer = NULL;
2645 }
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002646 if (snd_card_name)
2647 free(snd_card_name);
Ashish Jain30ae8942017-11-05 17:21:23 +05302648
Dhananjay Kumara7e27832017-07-11 15:40:39 +05302649 if (hw_info)
2650 hw_info_deinit(hw_info);
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002651
mpangfaa7bae2019-01-15 16:38:13 +08002652 if (retry_num >= RETRY_NUMBER) {
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002653 ALOGE("%s: Unable to find correct sound card, aborting.", __func__);
2654 return -1;
2655 }
2656
Soumya Managoli9fee7c62018-04-06 16:21:50 +05302657 if (mixer)
2658 *mixer_handle = mixer;
2659
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002660 return snd_card_num;
2661}
Naresh Tanniru6160c712017-04-17 15:43:48 +05302662
Soumya Managoli9fee7c62018-04-06 16:21:50 +05302663void audio_extn_utils_close_snd_mixer(struct mixer *mixer)
2664{
2665 if (mixer)
2666 mixer_close(mixer);
2667}
2668
Naresh Tanniru6160c712017-04-17 15:43:48 +05302669#ifdef SNDRV_COMPRESS_ENABLE_ADJUST_SESSION_CLOCK
2670int audio_extn_utils_compress_enable_drift_correction(
2671 struct stream_out *out,
2672 struct audio_out_enable_drift_correction *drift)
2673{
2674 struct snd_compr_metadata metadata;
2675 int ret = -EINVAL;
2676
2677 if(drift == NULL) {
2678 ALOGE("%s:: Invalid param", __func__);
2679 goto exit;
2680 }
2681
2682 ALOGD("%s:: drift enable %d", __func__,drift->enable);
2683
2684 if (!is_offload_usecase(out->usecase)) {
2685 ALOGE("%s:: not supported for non offload session", __func__);
2686 goto exit;
2687 }
2688
2689 if (!out->compr) {
2690 ALOGW("%s:: offload session not yet opened,"
2691 "start delay will be configure later", __func__);
2692 goto exit;
2693 }
2694
2695 metadata.key = SNDRV_COMPRESS_ENABLE_ADJUST_SESSION_CLOCK;
2696 metadata.value[0] = drift->enable;
2697 out->drift_correction_enabled = drift->enable;
2698
2699 ret = compress_set_metadata(out->compr, &metadata);
2700 if(ret) {
2701 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2702 out->drift_correction_enabled = false;
2703 }
2704
2705exit:
2706 return ret;
2707}
2708#else
2709int audio_extn_utils_compress_enable_drift_correction(
2710 struct stream_out *out __unused,
2711 struct audio_out_enable_drift_correction *drift __unused)
2712{
2713 ALOGD("%s:: configuring drift enablement not supported", __func__);
2714 return 0;
2715}
2716#endif
2717
2718#ifdef SNDRV_COMPRESS_ADJUST_SESSION_CLOCK
2719int audio_extn_utils_compress_correct_drift(
2720 struct stream_out *out,
2721 struct audio_out_correct_drift *drift_param)
2722{
2723 struct snd_compr_metadata metadata;
2724 int ret = -EINVAL;
2725
2726 if (drift_param == NULL) {
2727 ALOGE("%s:: Invalid drift_param", __func__);
2728 goto exit;
2729 }
2730
2731 ALOGD("%s:: adjust time 0x%"PRIx64" ", __func__,
2732 drift_param->adjust_time);
2733
2734 if (!is_offload_usecase(out->usecase)) {
2735 ALOGE("%s:: not supported for non offload session", __func__);
2736 goto exit;
2737 }
2738
2739 if (!out->compr) {
2740 ALOGW("%s:: offload session not yet opened", __func__);
2741 goto exit;
2742 }
2743
2744 if (!out->drift_correction_enabled) {
2745 ALOGE("%s:: drift correction not enabled", __func__);
2746 goto exit;
2747 }
2748
2749 metadata.key = SNDRV_COMPRESS_ADJUST_SESSION_CLOCK;
2750 metadata.value[0] = 0xFFFFFFFF & drift_param->adjust_time; /* lsb */
2751 metadata.value[1] = \
2752 (0xFFFFFFFF00000000 & drift_param->adjust_time) >> 32; /* msb*/
2753
2754 ret = compress_set_metadata(out->compr, &metadata);
2755 if(ret)
2756 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2757exit:
2758 return ret;
2759}
2760#else
2761int audio_extn_utils_compress_correct_drift(
2762 struct stream_out *out __unused,
2763 struct audio_out_correct_drift *drift_param __unused)
2764{
2765 ALOGD("%s:: setting adjust clock not supported", __func__);
2766 return 0;
2767}
2768#endif
Naresh Tanniru29bce4e2017-04-27 17:54:30 +05302769
2770int audio_extn_utils_set_channel_map(
2771 struct stream_out *out,
2772 struct audio_out_channel_map_param *channel_map_param)
2773{
2774 int ret = -EINVAL, i = 0;
2775 int channels = audio_channel_count_from_out_mask(out->channel_mask);
2776
2777 if (channel_map_param == NULL) {
2778 ALOGE("%s:: Invalid channel_map", __func__);
2779 goto exit;
2780 }
2781
2782 if (channel_map_param->channels != channels) {
2783 ALOGE("%s:: Channels(%d) does not match stream channels(%d)",
2784 __func__, channel_map_param->channels, channels);
2785 goto exit;
2786 }
2787
2788 for ( i = 0; i < channels; i++) {
2789 ALOGV("%s:: channel_map[%d]- %d", __func__, i, channel_map_param->channel_map[i]);
2790 out->channel_map_param.channel_map[i] = channel_map_param->channel_map[i];
2791 }
2792 ret = 0;
2793exit:
2794 return ret;
2795}
Varun Balaraje49253e2017-07-06 19:48:56 +05302796
2797int audio_extn_utils_set_pan_scale_params(
2798 struct stream_out *out,
2799 struct mix_matrix_params *mm_params)
2800{
2801 int ret = -EINVAL, i = 0, j = 0;
2802
Varun Balaraj003ee752017-08-07 17:54:55 +05302803 if (mm_params == NULL || out == NULL) {
2804 ALOGE("%s:: Invalid mix matrix or out param", __func__);
Varun Balaraje49253e2017-07-06 19:48:56 +05302805 goto exit;
2806 }
2807
2808 if (mm_params->num_output_channels > MAX_CHANNELS_SUPPORTED ||
2809 mm_params->num_output_channels <= 0 ||
2810 mm_params->num_input_channels > MAX_CHANNELS_SUPPORTED ||
2811 mm_params->num_input_channels <= 0)
2812 goto exit;
2813
2814 out->pan_scale_params.num_output_channels = mm_params->num_output_channels;
2815 out->pan_scale_params.num_input_channels = mm_params->num_input_channels;
2816 out->pan_scale_params.has_output_channel_map =
2817 mm_params->has_output_channel_map;
2818 for (i = 0; i < mm_params->num_output_channels; i++)
2819 out->pan_scale_params.output_channel_map[i] =
2820 mm_params->output_channel_map[i];
2821
2822 out->pan_scale_params.has_input_channel_map =
2823 mm_params->has_input_channel_map;
2824 for (i = 0; i < mm_params->num_input_channels; i++)
2825 out->pan_scale_params.input_channel_map[i] =
2826 mm_params->input_channel_map[i];
2827
2828 out->pan_scale_params.has_mixer_coeffs = mm_params->has_mixer_coeffs;
2829 for (i = 0; i < mm_params->num_output_channels; i++)
2830 for (j = 0; j < mm_params->num_input_channels; j++) {
2831 //Convert the channel coefficient gains in Q14 format
2832 out->pan_scale_params.mixer_coeffs[i][j] =
2833 mm_params->mixer_coeffs[i][j] * (2 << 13);
2834 }
2835
2836 ret = platform_set_stream_pan_scale_params(out->dev->platform,
2837 out->pcm_device_id,
2838 out->pan_scale_params);
2839
2840exit:
2841 return ret;
2842}
2843
2844int audio_extn_utils_set_downmix_params(
2845 struct stream_out *out,
2846 struct mix_matrix_params *mm_params)
2847{
2848 int ret = -EINVAL, i = 0, j = 0;
2849 struct audio_usecase *usecase = NULL;
2850
Aniket Kumar Lataf9f246e2017-09-15 15:20:16 -07002851 if (mm_params == NULL || out == NULL) {
Varun Balaraj003ee752017-08-07 17:54:55 +05302852 ALOGE("%s:: Invalid mix matrix or out param", __func__);
Varun Balaraje49253e2017-07-06 19:48:56 +05302853 goto exit;
2854 }
2855
2856 if (mm_params->num_output_channels > MAX_CHANNELS_SUPPORTED ||
2857 mm_params->num_output_channels <= 0 ||
2858 mm_params->num_input_channels > MAX_CHANNELS_SUPPORTED ||
2859 mm_params->num_input_channels <= 0)
2860 goto exit;
2861
2862 usecase = get_usecase_from_list(out->dev, out->usecase);
Varun Balaraj003ee752017-08-07 17:54:55 +05302863 if (!usecase) {
2864 ALOGE("%s: Get usecase list failed!", __func__);
Aniket Kumar Lataf9f246e2017-09-15 15:20:16 -07002865 goto exit;
2866 }
Varun Balaraje49253e2017-07-06 19:48:56 +05302867 out->downmix_params.num_output_channels = mm_params->num_output_channels;
2868 out->downmix_params.num_input_channels = mm_params->num_input_channels;
2869
2870 out->downmix_params.has_output_channel_map =
2871 mm_params->has_output_channel_map;
2872 for (i = 0; i < mm_params->num_output_channels; i++) {
2873 out->downmix_params.output_channel_map[i] =
2874 mm_params->output_channel_map[i];
2875 }
2876
2877 out->downmix_params.has_input_channel_map =
2878 mm_params->has_input_channel_map;
2879 for (i = 0; i < mm_params->num_input_channels; i++)
2880 out->downmix_params.input_channel_map[i] =
2881 mm_params->input_channel_map[i];
2882
2883 out->downmix_params.has_mixer_coeffs = mm_params->has_mixer_coeffs;
2884 for (i = 0; i < mm_params->num_output_channels; i++)
Nikhil Latukar2e6f6242017-08-15 13:37:07 +05302885 for (j = 0; j < mm_params->num_input_channels; j++) {
2886 //Convert the channel coefficient gains in Q14 format
Varun Balaraje49253e2017-07-06 19:48:56 +05302887 out->downmix_params.mixer_coeffs[i][j] =
Nikhil Latukar2e6f6242017-08-15 13:37:07 +05302888 mm_params->mixer_coeffs[i][j] * (2 << 13);
2889 }
Varun Balaraje49253e2017-07-06 19:48:56 +05302890
2891 ret = platform_set_stream_downmix_params(out->dev->platform,
2892 out->pcm_device_id,
2893 usecase->out_snd_device,
2894 out->downmix_params);
2895
2896exit:
2897 return ret;
2898}
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05302899
2900bool audio_extn_utils_is_dolby_format(audio_format_t format)
2901{
2902 if (format == AUDIO_FORMAT_AC3 ||
2903 format == AUDIO_FORMAT_E_AC3 ||
2904 format == AUDIO_FORMAT_E_AC3_JOC)
2905 return true;
2906 else
2907 return false;
2908}
2909
`Deeraj Soman676c2702017-09-18 19:25:53 +05302910int audio_extn_utils_get_bit_width_from_string(const char *id_string)
2911{
2912 int i;
2913 const mixer_config_lookup mixer_bitwidth_config[] = {{"S24_3LE", 24},
2914 {"S32_LE", 32},
2915 {"S24_LE", 24},
2916 {"S16_LE", 16}};
2917 int num_configs = sizeof(mixer_bitwidth_config) / sizeof(mixer_bitwidth_config[0]);
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05302918
`Deeraj Soman676c2702017-09-18 19:25:53 +05302919 for (i = 0; i < num_configs; i++) {
2920 if (!strcmp(id_string, mixer_bitwidth_config[i].id_string))
2921 return mixer_bitwidth_config[i].value;
2922 }
2923
2924 return -EINVAL;
2925}
2926
2927int audio_extn_utils_get_sample_rate_from_string(const char *id_string)
2928{
2929 int i;
2930 const mixer_config_lookup mixer_samplerate_config[] = {{"KHZ_32", 32000},
2931 {"KHZ_48", 48000},
2932 {"KHZ_96", 96000},
2933 {"KHZ_144", 144000},
2934 {"KHZ_192", 192000},
2935 {"KHZ_384", 384000},
2936 {"KHZ_44P1", 44100},
2937 {"KHZ_88P2", 88200},
2938 {"KHZ_176P4", 176400},
2939 {"KHZ_352P8", 352800}};
2940 int num_configs = sizeof(mixer_samplerate_config) / sizeof(mixer_samplerate_config[0]);
2941
2942 for (i = 0; i < num_configs; i++) {
2943 if (!strcmp(id_string, mixer_samplerate_config[i].id_string))
2944 return mixer_samplerate_config[i].value;
2945 }
2946
2947 return -EINVAL;
2948}
2949
2950int audio_extn_utils_get_channels_from_string(const char *id_string)
2951{
2952 int i;
2953 const mixer_config_lookup mixer_channels_config[] = {{"One", 1},
2954 {"Two", 2},
2955 {"Three",3},
2956 {"Four", 4},
2957 {"Five", 5},
2958 {"Six", 6},
2959 {"Seven", 7},
2960 {"Eight", 8}};
2961 int num_configs = sizeof(mixer_channels_config) / sizeof(mixer_channels_config[0]);
2962
2963 for (i = 0; i < num_configs; i++) {
2964 if (!strcmp(id_string, mixer_channels_config[i].id_string))
2965 return mixer_channels_config[i].value;
2966 }
2967
2968 return -EINVAL;
2969}
Surendar karka30569792018-05-08 12:02:21 +05302970
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08002971void audio_extn_utils_release_snd_device(snd_device_t snd_device)
2972{
2973 audio_extn_dev_arbi_release(snd_device);
2974 audio_extn_sound_trigger_update_device_status(snd_device,
2975 ST_EVENT_SND_DEVICE_FREE);
2976 audio_extn_listen_update_device_status(snd_device,
2977 LISTEN_EVENT_SND_DEVICE_FREE);
2978}
2979
Surendar karka30569792018-05-08 12:02:21 +05302980int audio_extn_utils_get_license_params
2981(
2982const struct audio_device *adev,
2983struct audio_license_params *license_params
2984)
2985{
2986 if(!license_params)
2987 return -EINVAL;
2988 return platform_get_license_by_product(adev->platform, (const char*)license_params->product, &license_params->key, license_params->license);
2989}
2990
Aalique Grahame22e49102018-12-18 14:23:57 -08002991int audio_extn_utils_send_app_type_gain(struct audio_device *adev,
2992 int app_type,
2993 int *gain)
2994{
2995 int gain_cfg[4];
2996 const char *mixer_ctl_name = "App Type Gain";
2997 struct mixer_ctl *ctl;
2998 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
2999 if (!ctl) {
3000 ALOGE("%s: Could not get volume ctl mixer %s", __func__,
3001 mixer_ctl_name);
3002 return -EINVAL;
3003 }
3004 gain_cfg[0] = 0;
3005 gain_cfg[1] = app_type;
3006 gain_cfg[2] = gain[0];
3007 gain_cfg[3] = gain[1];
3008 ALOGV("%s app_type %d l(%d) r(%d)", __func__, app_type, gain[0], gain[1]);
3009 return mixer_ctl_set_array(ctl, gain_cfg,
3010 sizeof(gain_cfg)/sizeof(gain_cfg[0]));
3011}
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -07003012
3013int audio_extn_utils_is_vendor_enhanced_fwk()
3014{
3015 static int is_running_with_enhanced_fwk = -EINVAL;
3016
3017 if (is_running_with_enhanced_fwk == -EINVAL) {
3018 vndk_fwk_lib_handle = dlopen(VNDK_FWK_LIB_PATH, RTLD_NOW);
3019 if (vndk_fwk_lib_handle != NULL) {
3020 vndk_fwk_isVendorEnhancedFwk = (vndk_fwk_isVendorEnhancedFwk_t)
3021 dlsym(vndk_fwk_lib_handle, "isRunningWithVendorEnhancedFramework");
3022 if (vndk_fwk_isVendorEnhancedFwk == NULL) {
3023 ALOGW("%s: dlsym failed, defaulting to enhanced_fwk configuration",
3024 __func__);
3025 is_running_with_enhanced_fwk = 1;
3026 } else {
3027 is_running_with_enhanced_fwk = vndk_fwk_isVendorEnhancedFwk();
3028 }
3029 dlclose(vndk_fwk_lib_handle);
3030 vndk_fwk_lib_handle = NULL;
3031 } else {
3032 ALOGW("%s: VNDK_FWK_LIB not found, setting stock configuration", __func__);
3033 is_running_with_enhanced_fwk = 0;
3034 }
3035 ALOGV("%s: vndk_fwk_isVendorEnhancedFwk=%d", __func__, is_running_with_enhanced_fwk);
3036 }
3037
3038 return is_running_with_enhanced_fwk;
3039}
Deeraj Soman14230922019-01-30 16:39:30 +05303040
Deeraj Somanfa377bf2019-02-06 12:57:59 +05303041int audio_extn_utils_get_perf_mode_flag(void)
3042{
3043#ifdef COMPRESSED_PERF_MODE_FLAG
3044 return COMPRESSED_PERF_MODE_FLAG;
3045#else
3046 return 0;
3047#endif
3048}
3049
Deeraj Soman14230922019-01-30 16:39:30 +05303050size_t audio_extn_utils_get_input_buffer_size(uint32_t sample_rate,
3051 audio_format_t format,
3052 int channel_count,
3053 int64_t duration_ms,
3054 bool is_low_latency)
3055{
3056 size_t size = 0;
3057 size_t capture_duration = AUDIO_CAPTURE_PERIOD_DURATION_MSEC;
3058 uint32_t bytes_per_period_sample = 0;
3059
3060
3061 if (audio_extn_utils_check_input_parameters(sample_rate, format, channel_count) != 0)
3062 return 0;
3063
3064 if (duration_ms >= MIN_OFFLOAD_BUFFER_DURATION_MS && duration_ms <= MAX_OFFLOAD_BUFFER_DURATION_MS)
3065 capture_duration = duration_ms;
3066
3067 size = (sample_rate * capture_duration) / 1000;
3068 if (is_low_latency)
3069 size = LOW_LATENCY_CAPTURE_PERIOD_SIZE;
3070
3071
3072 bytes_per_period_sample = audio_bytes_per_sample(format) * channel_count;
3073 size *= bytes_per_period_sample;
3074
3075 /* make sure the size is multiple of 32 bytes and additionally multiple of
3076 * the frame_size (required for 24bit samples and non-power-of-2 channel counts)
3077 * At 48 kHz mono 16-bit PCM:
3078 * 5.000 ms = 240 frames = 15*16*1*2 = 480, a whole multiple of 32 (15)
3079 * 3.333 ms = 160 frames = 10*16*1*2 = 320, a whole multiple of 32 (10)
3080 *
3081 * The loop reaches result within 32 iterations, as initial size is
3082 * already a multiple of frame_size
3083 */
3084 size = audio_extn_utils_nearest_multiple(size, audio_extn_utils_lcm(32, bytes_per_period_sample));
3085
3086 return size;
3087}