blob: b25fe3cb18b2ec4f78e08192624d20062a76bee0 [file] [log] [blame]
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001/*
Aalique Grahame22e49102018-12-18 14:23:57 -08002 * Copyright (c) 2014-2019, The Linux Foundation. All rights reserved.
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07003 * Not a Contribution.
4 *
5 * Copyright (C) 2014 The Android Open Source Project
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19
20#define LOG_TAG "audio_hw_utils"
21/* #define LOG_NDEBUG 0 */
22
Manish Dewangan27346042017-03-01 12:56:12 +053023#include <inttypes.h>
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070024#include <errno.h>
25#include <cutils/properties.h>
26#include <cutils/config_utils.h>
27#include <stdlib.h>
28#include <dlfcn.h>
29#include <cutils/str_parms.h>
Aalique Grahame22e49102018-12-18 14:23:57 -080030#include <log/log.h>
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070031#include <cutils/misc.h>
Vinay Vermaaddfa4a2018-04-29 14:03:38 +053032#include <unistd.h>
Surendar Karka287348c2019-04-10 18:31:46 +053033#include <sys/ioctl.h>
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070034
Manish Dewangan07de2142017-02-27 19:27:20 +053035
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070036#include "audio_hw.h"
37#include "platform.h"
38#include "platform_api.h"
39#include "audio_extn.h"
Deeraj Soman14230922019-01-30 16:39:30 +053040#include "voice_extn.h"
Narsinga Rao Chella212e2542014-11-17 19:57:04 -080041#include "voice.h"
Manish Dewangan07de2142017-02-27 19:27:20 +053042#include <sound/compress_params.h>
43#include <sound/compress_offload.h>
Surendar Karka287348c2019-04-10 18:31:46 +053044#include <sound/devdep_params.h>
Manish Dewangan07de2142017-02-27 19:27:20 +053045#include <tinycompress/tinycompress.h>
Revathi Uddaraju1eac8b02017-05-18 17:13:33 +053046
47#ifdef DYNAMIC_LOG_ENABLED
48#include <log_xml_parser.h>
49#define LOG_MASK HAL_MOD_FILE_UTILS
50#include <log_utils.h>
51#endif
52
Ashish Jain81eb2a82015-05-13 10:52:34 +053053#ifdef AUDIO_EXTERNAL_HDMI_ENABLED
Ashish Jain81eb2a82015-05-13 10:52:34 +053054#include "audio_parsers.h"
55#endif
Ashish Jain81eb2a82015-05-13 10:52:34 +053056
Yamit Mehtaa0d653a2016-11-25 20:33:25 +053057#ifdef LINUX_ENABLED
58#define AUDIO_OUTPUT_POLICY_VENDOR_CONFIG_FILE "/etc/audio_output_policy.conf"
Dhananjay Kumard6d32152016-10-13 16:11:03 +053059#define AUDIO_IO_POLICY_VENDOR_CONFIG_FILE "/etc/audio_io_policy.conf"
Yamit Mehtaa0d653a2016-11-25 20:33:25 +053060#else
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070061#define AUDIO_OUTPUT_POLICY_VENDOR_CONFIG_FILE "/vendor/etc/audio_output_policy.conf"
Dhananjay Kumard6d32152016-10-13 16:11:03 +053062#define AUDIO_IO_POLICY_VENDOR_CONFIG_FILE "/vendor/etc/audio_io_policy.conf"
Yamit Mehtaa0d653a2016-11-25 20:33:25 +053063#endif
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070064
65#define OUTPUTS_TAG "outputs"
Dhananjay Kumard6d32152016-10-13 16:11:03 +053066#define INPUTS_TAG "inputs"
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070067
68#define DYNAMIC_VALUE_TAG "dynamic"
69#define FLAGS_TAG "flags"
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +053070#define PROFILES_TAG "profile"
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070071#define FORMATS_TAG "formats"
72#define SAMPLING_RATES_TAG "sampling_rates"
73#define BIT_WIDTH_TAG "bit_width"
74#define APP_TYPE_TAG "app_type"
75
76#define STRING_TO_ENUM(string) { #string, string }
77#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
78
Ben Rombergera04fabc2014-11-14 12:16:03 -080079#define BASE_TABLE_SIZE 64
80#define MAX_BASEINDEX_LEN 256
81
Ben Romberger1aaaf862017-04-06 17:49:46 -070082#ifndef SND_AUDIOCODEC_TRUEHD
83#define SND_AUDIOCODEC_TRUEHD 0x00000023
84#endif
85
Vikram Panduranga93f080e2017-06-07 18:16:14 -070086#define APP_TYPE_VOIP_AUDIO 0x1113A
87
Ashish Jain81eb2a82015-05-13 10:52:34 +053088#ifdef AUDIO_EXTERNAL_HDMI_ENABLED
89#define PROFESSIONAL (1<<0) /* 0 = consumer, 1 = professional */
90#define NON_LPCM (1<<1) /* 0 = audio, 1 = non-audio */
91#define SR_44100 (0<<0) /* 44.1kHz */
92#define SR_NOTID (1<<0) /* non indicated */
93#define SR_48000 (2<<0) /* 48kHz */
94#define SR_32000 (3<<0) /* 32kHz */
95#define SR_22050 (4<<0) /* 22.05kHz */
96#define SR_24000 (6<<0) /* 24kHz */
97#define SR_88200 (8<<0) /* 88.2kHz */
98#define SR_96000 (10<<0) /* 96kHz */
99#define SR_176400 (12<<0) /* 176.4kHz */
100#define SR_192000 (14<<0) /* 192kHz */
101
102#endif
Manish Dewangan07de2142017-02-27 19:27:20 +0530103
104/* ToDo: Check and update a proper value in msec */
Weiyin Jiangc49a3b52018-08-17 16:16:08 +0800105#define COMPRESS_OFFLOAD_PLAYBACK_LATENCY 50
106#define PCM_OFFLOAD_PLAYBACK_DSP_PATHDELAY 62
107#define PCM_OFFLOAD_PLAYBACK_LATENCY \
108 (PCM_OFFLOAD_BUFFER_DURATION + PCM_OFFLOAD_PLAYBACK_DSP_PATHDELAY)
Manish Dewangan07de2142017-02-27 19:27:20 +0530109
Varun Balaraje49253e2017-07-06 19:48:56 +0530110#ifndef MAX_CHANNELS_SUPPORTED
111#define MAX_CHANNELS_SUPPORTED 8
112#endif
113
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -0700114#ifdef __LP64__
115#define VNDK_FWK_LIB_PATH "/vendor/lib64/libqti_vndfwk_detect.so"
116#else
117#define VNDK_FWK_LIB_PATH "/vendor/lib/libqti_vndfwk_detect.so"
118#endif
119
120static void *vndk_fwk_lib_handle = NULL;
121typedef int (*vndk_fwk_isVendorEnhancedFwk_t)();
122static vndk_fwk_isVendorEnhancedFwk_t vndk_fwk_isVendorEnhancedFwk;
123
`Deeraj Soman676c2702017-09-18 19:25:53 +0530124typedef struct {
125 const char *id_string;
126 const int value;
127} mixer_config_lookup;
128
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700129struct string_to_enum {
130 const char *name;
131 uint32_t value;
132};
133
134const struct string_to_enum s_flag_name_to_enum_table[] = {
135 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_DIRECT),
136 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_PRIMARY),
137 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_FAST),
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800138 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_RAW),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700139 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_DEEP_BUFFER),
140 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD),
141 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_NON_BLOCKING),
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700142 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_HW_AV_SYNC),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700143 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_INCALL_MUSIC),
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700144 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH),
Naresh Tanniruee3499a2017-01-05 14:05:35 +0530145 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_TIMESTAMP),
Vikram Panduranga93f080e2017-06-07 18:16:14 -0700146 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_VOIP_RX),
Ben Romberger6c4d3812017-06-13 17:46:45 -0700147 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_BD),
Varun Balaraje49253e2017-07-06 19:48:56 +0530148 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_INTERACTIVE),
Derek Chen090dfc52018-03-22 22:15:29 -0400149 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_MEDIA),
150 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_SYS_NOTIFICATION),
151 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_NAV_GUIDANCE),
152 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_PHONE),
Derek Chendf05eea2019-08-01 13:57:49 -0700153 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_REAR_SEAT),
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530154 STRING_TO_ENUM(AUDIO_INPUT_FLAG_NONE),
155 STRING_TO_ENUM(AUDIO_INPUT_FLAG_FAST),
156 STRING_TO_ENUM(AUDIO_INPUT_FLAG_HW_HOTWORD),
157 STRING_TO_ENUM(AUDIO_INPUT_FLAG_RAW),
158 STRING_TO_ENUM(AUDIO_INPUT_FLAG_SYNC),
Dhananjay Kumaree4d2002016-10-25 18:02:58 +0530159 STRING_TO_ENUM(AUDIO_INPUT_FLAG_TIMESTAMP),
Dhananjay Kumar704ce6f2017-09-28 22:08:00 +0530160 STRING_TO_ENUM(AUDIO_INPUT_FLAG_COMPRESS),
Ralf Herzaec80262018-07-03 07:08:49 +0200161 STRING_TO_ENUM(AUDIO_INPUT_FLAG_PASSTHROUGH),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700162};
163
164const struct string_to_enum s_format_name_to_enum_table[] = {
Ashish Jain83a6cc22016-06-28 14:34:17 +0530165 STRING_TO_ENUM(AUDIO_FORMAT_PCM_8_BIT),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700166 STRING_TO_ENUM(AUDIO_FORMAT_PCM_16_BIT),
Ashish Jain5106d362016-05-11 19:23:33 +0530167 STRING_TO_ENUM(AUDIO_FORMAT_PCM_24_BIT_PACKED),
168 STRING_TO_ENUM(AUDIO_FORMAT_PCM_8_24_BIT),
Ashish Jainf1eaa582016-05-23 20:54:24 +0530169 STRING_TO_ENUM(AUDIO_FORMAT_PCM_32_BIT),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700170 STRING_TO_ENUM(AUDIO_FORMAT_MP3),
171 STRING_TO_ENUM(AUDIO_FORMAT_AAC),
172 STRING_TO_ENUM(AUDIO_FORMAT_VORBIS),
Mingming Yinae3530f2014-07-03 16:50:18 -0700173 STRING_TO_ENUM(AUDIO_FORMAT_AMR_NB),
174 STRING_TO_ENUM(AUDIO_FORMAT_AMR_WB),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700175 STRING_TO_ENUM(AUDIO_FORMAT_AC3),
Mingming Yinae3530f2014-07-03 16:50:18 -0700176 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700177 STRING_TO_ENUM(AUDIO_FORMAT_DTS),
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530178 STRING_TO_ENUM(AUDIO_FORMAT_DTS_HD),
Ben Romberger1aaaf862017-04-06 17:49:46 -0700179 STRING_TO_ENUM(AUDIO_FORMAT_DOLBY_TRUEHD),
Naresh Tanniru928f0862017-04-07 16:44:23 -0700180 STRING_TO_ENUM(AUDIO_FORMAT_IEC61937),
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530181 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3_JOC),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700182 STRING_TO_ENUM(AUDIO_FORMAT_WMA),
183 STRING_TO_ENUM(AUDIO_FORMAT_WMA_PRO),
184 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADIF),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700185 STRING_TO_ENUM(AUDIO_FORMAT_AMR_WB_PLUS),
186 STRING_TO_ENUM(AUDIO_FORMAT_EVRC),
187 STRING_TO_ENUM(AUDIO_FORMAT_EVRCB),
188 STRING_TO_ENUM(AUDIO_FORMAT_EVRCWB),
189 STRING_TO_ENUM(AUDIO_FORMAT_QCELP),
190 STRING_TO_ENUM(AUDIO_FORMAT_MP2),
191 STRING_TO_ENUM(AUDIO_FORMAT_EVRCNW),
Amit Shekhar6f461b12014-08-01 14:52:58 -0700192 STRING_TO_ENUM(AUDIO_FORMAT_FLAC),
Satya Krishna Pindiproli70471602015-04-24 19:12:43 +0530193 STRING_TO_ENUM(AUDIO_FORMAT_ALAC),
194 STRING_TO_ENUM(AUDIO_FORMAT_APE),
Alexy Josephcd8eaed2014-12-11 12:46:53 -0800195 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LC),
196 STRING_TO_ENUM(AUDIO_FORMAT_AAC_HE_V1),
197 STRING_TO_ENUM(AUDIO_FORMAT_AAC_HE_V2),
Manish Dewangana6fc5442015-08-24 20:30:31 +0530198 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS),
Ashish Jaine513a872015-11-19 17:00:56 +0530199 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_LC),
200 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_HE_V1),
201 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_HE_V2),
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +0530202 STRING_TO_ENUM(AUDIO_FORMAT_DSD),
Arun Kumar Dasari3b174182016-12-27 13:01:14 +0530203 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM),
204 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM_LC),
205 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM_HE_V1),
206 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM_HE_V2),
Dhanalakshmi Siddani18737932016-11-29 17:33:17 +0530207 STRING_TO_ENUM(AUDIO_FORMAT_APTX),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700208};
209
Manish Dewangan3ccdea52017-02-13 19:31:54 +0530210/* payload structure avt_device drift query */
211struct audio_avt_device_drift_stats {
212 uint32_t minor_version;
Manish Dewangan338c50a2017-09-12 15:22:03 +0530213
Manish Dewangan3ccdea52017-02-13 19:31:54 +0530214 /* Indicates the device interface direction as either
215 * source (Tx) or sink (Rx).
216 */
217 uint16_t device_direction;
Manish Dewangan338c50a2017-09-12 15:22:03 +0530218
219 /* Reference timer for drift accumulation and time stamp information.
220 * currently it only support AFE_REF_TIMER_TYPE_AVTIMER
221 */
222 uint16_t reference_timer;
Manish Dewangan3ccdea52017-02-13 19:31:54 +0530223 struct audio_avt_device_drift_param drift_param;
Manish Dewangan338c50a2017-09-12 15:22:03 +0530224} __attribute__((packed));
Manish Dewangan3ccdea52017-02-13 19:31:54 +0530225
Ben Rombergera04fabc2014-11-14 12:16:03 -0800226static char bTable[BASE_TABLE_SIZE] = {
227 'A','B','C','D','E','F','G','H','I','J','K','L',
228 'M','N','O','P','Q','R','S','T','U','V','W','X',
229 'Y','Z','a','b','c','d','e','f','g','h','i','j',
230 'k','l','m','n','o','p','q','r','s','t','u','v',
231 'w','x','y','z','0','1','2','3','4','5','6','7',
232 '8','9','+','/'
233};
234
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700235static uint32_t string_to_enum(const struct string_to_enum *table, size_t size,
236 const char *name)
237{
238 size_t i;
239 for (i = 0; i < size; i++) {
240 if (strcmp(table[i].name, name) == 0) {
241 ALOGV("%s found %s", __func__, table[i].name);
242 return table[i].value;
243 }
244 }
245 return 0;
246}
247
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530248static audio_io_flags_t parse_flag_names(char *name)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700249{
250 uint32_t flag = 0;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530251 audio_io_flags_t io_flags;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800252 char *last_r;
253 char *flag_name = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700254 while (flag_name != NULL) {
255 if (strlen(flag_name) != 0) {
256 flag |= string_to_enum(s_flag_name_to_enum_table,
257 ARRAY_SIZE(s_flag_name_to_enum_table),
258 flag_name);
259 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800260 flag_name = strtok_r(NULL, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700261 }
262
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800263 ALOGV("parse_flag_names: flag - %x", flag);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530264 io_flags.in_flags = (audio_input_flags_t)flag;
265 io_flags.out_flags = (audio_output_flags_t)flag;
266 return io_flags;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700267}
268
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530269static void parse_format_names(char *name, struct streams_io_cfg *s_info)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700270{
271 struct stream_format *sf_info = NULL;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800272 char *last_r;
273 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700274
275 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG) == 0)
276 return;
277
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530278 list_init(&s_info->format_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700279 while (str != NULL) {
280 audio_format_t format = (audio_format_t)string_to_enum(s_format_name_to_enum_table,
281 ARRAY_SIZE(s_format_name_to_enum_table), str);
282 ALOGV("%s: format - %d", __func__, format);
283 if (format != 0) {
284 sf_info = (struct stream_format *)calloc(1, sizeof(struct stream_format));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700285 if (sf_info == NULL)
286 break; /* return whatever was parsed */
287
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700288 sf_info->format = format;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530289 list_add_tail(&s_info->format_list, &sf_info->list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700290 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800291 str = strtok_r(NULL, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700292 }
293}
294
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530295static void parse_sample_rate_names(char *name, struct streams_io_cfg *s_info)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700296{
Amit Shekhar6f461b12014-08-01 14:52:58 -0700297 struct stream_sample_rate *ss_info = NULL;
298 uint32_t sample_rate = 48000;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800299 char *last_r;
300 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700301
Amit Shekhar6f461b12014-08-01 14:52:58 -0700302 if (str != NULL && 0 == strcmp(str, DYNAMIC_VALUE_TAG))
303 return;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700304
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530305 list_init(&s_info->sample_rate_list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700306 while (str != NULL) {
307 sample_rate = (uint32_t)strtol(str, (char **)NULL, 10);
308 ALOGV("%s: sample_rate - %d", __func__, sample_rate);
309 if (0 != sample_rate) {
310 ss_info = (struct stream_sample_rate *)calloc(1, sizeof(struct stream_sample_rate));
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800311 if (!ss_info) {
312 ALOGE("%s: memory allocation failure", __func__);
313 return;
314 }
Amit Shekhar6f461b12014-08-01 14:52:58 -0700315 ss_info->sample_rate = sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530316 list_add_tail(&s_info->sample_rate_list, &ss_info->list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700317 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800318 str = strtok_r(NULL, "|", &last_r);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700319 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700320}
321
322static int parse_bit_width_names(char *name)
323{
324 int bit_width = 16;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800325 char *last_r;
326 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700327
328 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG))
329 bit_width = (int)strtol(str, (char **)NULL, 10);
330
331 ALOGV("%s: bit_width - %d", __func__, bit_width);
332 return bit_width;
333}
334
335static int parse_app_type_names(void *platform, char *name)
336{
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700337 int app_type = platform_get_default_app_type(platform);
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800338 char *last_r;
339 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700340
341 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG))
342 app_type = (int)strtol(str, (char **)NULL, 10);
343
344 ALOGV("%s: app_type - %d", __func__, app_type);
345 return app_type;
346}
347
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530348static void update_streams_cfg_list(cnode *root, void *platform,
349 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700350{
351 cnode *node = root->first_child;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530352 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700353
354 ALOGV("%s", __func__);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530355 s_info = (struct streams_io_cfg *)calloc(1, sizeof(struct streams_io_cfg));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700356
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530357 if (!s_info) {
358 ALOGE("failed to allocate mem for s_info list element");
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700359 return;
360 }
361
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700362 while (node) {
363 if (strcmp(node->name, FLAGS_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530364 s_info->flags = parse_flag_names((char *)node->value);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530365 } else if (strcmp(node->name, PROFILES_TAG) == 0) {
366 strlcpy(s_info->profile, (char *)node->value, sizeof(s_info->profile));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700367 } else if (strcmp(node->name, FORMATS_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530368 parse_format_names((char *)node->value, s_info);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700369 } else if (strcmp(node->name, SAMPLING_RATES_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530370 s_info->app_type_cfg.sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
371 parse_sample_rate_names((char *)node->value, s_info);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700372 } else if (strcmp(node->name, BIT_WIDTH_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530373 s_info->app_type_cfg.bit_width = parse_bit_width_names((char *)node->value);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700374 } else if (strcmp(node->name, APP_TYPE_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530375 s_info->app_type_cfg.app_type = parse_app_type_names(platform, (char *)node->value);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700376 }
377 node = node->next;
378 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530379 list_add_tail(streams_cfg_list, &s_info->list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700380}
381
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530382static void load_cfg_list(cnode *root, void *platform,
383 struct listnode *streams_output_cfg_list,
384 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700385{
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530386 cnode *node = NULL;
387
388 node = config_find(root, OUTPUTS_TAG);
389 if (node != NULL) {
390 node = node->first_child;
391 while (node) {
392 ALOGV("%s: loading output %s", __func__, node->name);
393 update_streams_cfg_list(node, platform, streams_output_cfg_list);
394 node = node->next;
395 }
396 } else {
397 ALOGI("%s: could not load output, node is NULL", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700398 }
399
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530400 node = config_find(root, INPUTS_TAG);
401 if (node != NULL) {
402 node = node->first_child;
403 while (node) {
404 ALOGV("%s: loading input %s", __func__, node->name);
405 update_streams_cfg_list(node, platform, streams_input_cfg_list);
406 node = node->next;
407 }
408 } else {
409 ALOGI("%s: could not load input, node is NULL", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700410 }
411}
412
413static void send_app_type_cfg(void *platform, struct mixer *mixer,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530414 struct listnode *streams_output_cfg_list,
415 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700416{
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530417 size_t app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT] = {0};
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700418 int length = 0, i, num_app_types = 0;
419 struct listnode *node;
420 bool update;
421 struct mixer_ctl *ctl = NULL;
422 const char *mixer_ctl_name = "App Type Config";
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530423 struct streams_io_cfg *s_info = NULL;
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800424 uint32_t target_bit_width = 0;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700425
426 if (!mixer) {
427 ALOGE("%s: mixer is null",__func__);
428 return;
429 }
430 ctl = mixer_get_ctl_by_name(mixer, mixer_ctl_name);
431 if (!ctl) {
432 ALOGE("%s: Could not get ctl for mixer cmd - %s",__func__, mixer_ctl_name);
433 return;
434 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530435 app_type_cfg[length++] = num_app_types;
436
437 if (list_empty(streams_output_cfg_list)) {
438 app_type_cfg[length++] = platform_get_default_app_type_v2(platform, PCM_PLAYBACK);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700439 app_type_cfg[length++] = 48000;
440 app_type_cfg[length++] = 16;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530441 num_app_types += 1;
442 }
443 if (list_empty(streams_input_cfg_list)) {
444 app_type_cfg[length++] = platform_get_default_app_type_v2(platform, PCM_CAPTURE);
445 app_type_cfg[length++] = 48000;
446 app_type_cfg[length++] = 16;
447 num_app_types += 1;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700448 }
449
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800450 /* get target bit width for ADM enforce mode */
451 target_bit_width = adev_get_dsp_bit_width_enforce_mode();
452
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700453 list_for_each(node, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530454 s_info = node_to_item(node, struct streams_io_cfg, list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700455 update = true;
456 for (i=0; i<length; i=i+3) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530457 if (app_type_cfg[i+1] == 0)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700458 break;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530459 else if (app_type_cfg[i+1] == (size_t)s_info->app_type_cfg.app_type) {
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530460 if (app_type_cfg[i+2] < (size_t)s_info->app_type_cfg.sample_rate)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530461 app_type_cfg[i+2] = s_info->app_type_cfg.sample_rate;
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530462 if (app_type_cfg[i+3] < (size_t)s_info->app_type_cfg.bit_width)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530463 app_type_cfg[i+3] = s_info->app_type_cfg.bit_width;
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800464 /* ADM bit width = max(enforce_bit_width, bit_width from s_info */
465 if (audio_extn_is_dsp_bit_width_enforce_mode_supported(s_info->flags.out_flags) &&
466 (target_bit_width > app_type_cfg[i+3]))
467 app_type_cfg[i+3] = target_bit_width;
468
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700469 update = false;
470 break;
471 }
472 }
473 if (update && ((length + 3) <= MAX_LENGTH_MIXER_CONTROL_IN_INT)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530474 num_app_types += 1;
475 app_type_cfg[length++] = s_info->app_type_cfg.app_type;
476 app_type_cfg[length++] = s_info->app_type_cfg.sample_rate;
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800477 app_type_cfg[length] = s_info->app_type_cfg.bit_width;
478 if (audio_extn_is_dsp_bit_width_enforce_mode_supported(s_info->flags.out_flags) &&
479 (target_bit_width > app_type_cfg[length]))
480 app_type_cfg[length] = target_bit_width;
481
482 length++;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530483 }
484 }
485 list_for_each(node, streams_input_cfg_list) {
486 s_info = node_to_item(node, struct streams_io_cfg, list);
487 update = true;
488 for (i=0; i<length; i=i+3) {
489 if (app_type_cfg[i+1] == 0)
490 break;
491 else if (app_type_cfg[i+1] == (size_t)s_info->app_type_cfg.app_type) {
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530492 if (app_type_cfg[i+2] < (size_t)s_info->app_type_cfg.sample_rate)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530493 app_type_cfg[i+2] = s_info->app_type_cfg.sample_rate;
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530494 if (app_type_cfg[i+3] < (size_t)s_info->app_type_cfg.bit_width)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530495 app_type_cfg[i+3] = s_info->app_type_cfg.bit_width;
496 update = false;
497 break;
498 }
499 }
500 if (update && ((length + 3) <= MAX_LENGTH_MIXER_CONTROL_IN_INT)) {
501 num_app_types += 1;
502 app_type_cfg[length++] = s_info->app_type_cfg.app_type;
503 app_type_cfg[length++] = s_info->app_type_cfg.sample_rate;
504 app_type_cfg[length++] = s_info->app_type_cfg.bit_width;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700505 }
506 }
507 ALOGV("%s: num_app_types: %d", __func__, num_app_types);
508 if (num_app_types) {
509 app_type_cfg[0] = num_app_types;
510 mixer_ctl_set_array(ctl, app_type_cfg, length);
511 }
512}
513
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530514void audio_extn_utils_update_streams_cfg_lists(void *platform,
515 struct mixer *mixer,
516 struct listnode *streams_output_cfg_list,
517 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700518{
519 cnode *root;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530520 char *data = NULL;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700521
522 ALOGV("%s", __func__);
523 list_init(streams_output_cfg_list);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530524 list_init(streams_input_cfg_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700525
526 root = config_node("", "");
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700527 if (root == NULL) {
528 ALOGE("cfg_list, NULL config root");
529 return;
530 }
531
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530532 data = (char *)load_file(AUDIO_IO_POLICY_VENDOR_CONFIG_FILE, NULL);
533 if (data == NULL) {
534 ALOGD("%s: failed to open io config file(%s), trying older config file",
535 __func__, AUDIO_IO_POLICY_VENDOR_CONFIG_FILE);
536 data = (char *)load_file(AUDIO_OUTPUT_POLICY_VENDOR_CONFIG_FILE, NULL);
537 if (data == NULL) {
538 send_app_type_cfg(platform, mixer,
539 streams_output_cfg_list,
540 streams_input_cfg_list);
541 ALOGE("%s: could not load io policy config!", __func__);
yidongh6eb4f752017-04-19 18:20:57 +0800542 free(root);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530543 return;
544 }
545 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700546
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530547 config_load(root, data);
548 load_cfg_list(root, platform, streams_output_cfg_list,
549 streams_input_cfg_list);
550
551 send_app_type_cfg(platform, mixer, streams_output_cfg_list,
552 streams_input_cfg_list);
Alexy Josephaee4fdd2016-01-29 13:02:07 -0800553
554 config_free(root);
yidongh6eb4f752017-04-19 18:20:57 +0800555 free(root);
Alexy Josephaee4fdd2016-01-29 13:02:07 -0800556 free(data);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700557}
558
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530559static void audio_extn_utils_dump_streams_cfg_list(
560 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700561{
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700562 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530563 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700564 struct stream_format *sf_info;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700565 struct stream_sample_rate *ss_info;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530566
567 list_for_each(node_i, streams_cfg_list) {
568 s_info = node_to_item(node_i, struct streams_io_cfg, list);
569 ALOGV("%s: flags-%d, sample_rate-%d, bit_width-%d, app_type-%d",
570 __func__, s_info->flags.out_flags, s_info->app_type_cfg.sample_rate,
571 s_info->app_type_cfg.bit_width, s_info->app_type_cfg.app_type);
572 list_for_each(node_j, &s_info->format_list) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700573 sf_info = node_to_item(node_j, struct stream_format, list);
574 ALOGV("format-%x", sf_info->format);
575 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530576 list_for_each(node_j, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700577 ss_info = node_to_item(node_j, struct stream_sample_rate, list);
578 ALOGV("sample rate-%d", ss_info->sample_rate);
579 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700580 }
581}
582
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530583void audio_extn_utils_dump_streams_cfg_lists(
584 struct listnode *streams_output_cfg_list,
585 struct listnode *streams_input_cfg_list)
586{
587 ALOGV("%s", __func__);
588 audio_extn_utils_dump_streams_cfg_list(streams_output_cfg_list);
589 audio_extn_utils_dump_streams_cfg_list(streams_input_cfg_list);
590}
591
592static void audio_extn_utils_release_streams_cfg_list(
593 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700594{
595 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530596 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700597
598 ALOGV("%s", __func__);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530599
600 while (!list_empty(streams_cfg_list)) {
601 node_i = list_head(streams_cfg_list);
602 s_info = node_to_item(node_i, struct streams_io_cfg, list);
603 while (!list_empty(&s_info->format_list)) {
604 node_j = list_head(&s_info->format_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700605 list_remove(node_j);
606 free(node_to_item(node_j, struct stream_format, list));
607 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530608 while (!list_empty(&s_info->sample_rate_list)) {
609 node_j = list_head(&s_info->sample_rate_list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700610 list_remove(node_j);
611 free(node_to_item(node_j, struct stream_sample_rate, list));
612 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700613 list_remove(node_i);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530614 free(node_to_item(node_i, struct streams_io_cfg, list));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700615 }
616}
617
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530618void audio_extn_utils_release_streams_cfg_lists(
619 struct listnode *streams_output_cfg_list,
620 struct listnode *streams_input_cfg_list)
621{
622 ALOGV("%s", __func__);
623 audio_extn_utils_release_streams_cfg_list(streams_output_cfg_list);
624 audio_extn_utils_release_streams_cfg_list(streams_input_cfg_list);
625}
626
627static bool set_app_type_cfg(struct streams_io_cfg *s_info,
628 struct stream_app_type_cfg *app_type_cfg,
629 uint32_t sample_rate, uint32_t bit_width)
Amit Shekhar6f461b12014-08-01 14:52:58 -0700630 {
631 struct listnode *node_i;
632 struct stream_sample_rate *ss_info;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530633 list_for_each(node_i, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700634 ss_info = node_to_item(node_i, struct stream_sample_rate, list);
635 if ((sample_rate <= ss_info->sample_rate) &&
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530636 (bit_width == s_info->app_type_cfg.bit_width)) {
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700637
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530638 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700639 app_type_cfg->sample_rate = ss_info->sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530640 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700641 ALOGV("%s app_type_cfg->app_type %d, app_type_cfg->sample_rate %d, app_type_cfg->bit_width %d",
642 __func__, app_type_cfg->app_type, app_type_cfg->sample_rate, app_type_cfg->bit_width);
643 return true;
644 }
645 }
646 /*
647 * Reiterate through the list assuming dafault sample rate.
648 * Handles scenario where input sample rate is higher
649 * than all sample rates in list for the input bit width.
650 */
651 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
Apoorv Raghuvanshif59bb222015-02-18 12:23:23 -0800652
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530653 list_for_each(node_i, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700654 ss_info = node_to_item(node_i, struct stream_sample_rate, list);
655 if ((sample_rate <= ss_info->sample_rate) &&
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530656 (bit_width == s_info->app_type_cfg.bit_width)) {
657 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700658 app_type_cfg->sample_rate = sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530659 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Apoorv Raghuvanshif59bb222015-02-18 12:23:23 -0800660 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 -0700661 __func__, app_type_cfg->app_type, app_type_cfg->sample_rate, app_type_cfg->bit_width);
662 return true;
663 }
664 }
665 return false;
666}
667
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530668void audio_extn_utils_update_stream_input_app_type_cfg(void *platform,
669 struct listnode *streams_input_cfg_list,
670 audio_devices_t devices __unused,
671 audio_input_flags_t flags,
672 audio_format_t format,
673 uint32_t sample_rate,
674 uint32_t bit_width,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530675 char* profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530676 struct stream_app_type_cfg *app_type_cfg)
677{
678 struct listnode *node_i, *node_j;
679 struct streams_io_cfg *s_info;
680 struct stream_format *sf_info;
681
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530682 ALOGV("%s: flags: 0x%x, format: 0x%x sample_rate %d, profile %s",
683 __func__, flags, format, sample_rate, profile);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530684
685 list_for_each(node_i, streams_input_cfg_list) {
686 s_info = node_to_item(node_i, struct streams_io_cfg, list);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530687 /* Along with flags do profile matching if set at either end.*/
688 if (s_info->flags.in_flags == flags &&
689 ((profile[0] == '\0' && s_info->profile[0] == '\0') ||
690 strncmp(s_info->profile, profile, sizeof(s_info->profile)) == 0)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530691 list_for_each(node_j, &s_info->format_list) {
692 sf_info = node_to_item(node_j, struct stream_format, list);
693 if (sf_info->format == format) {
694 if (set_app_type_cfg(s_info, app_type_cfg, sample_rate, bit_width))
695 return;
696 }
697 }
698 }
699 }
700 ALOGW("%s: App type could not be selected. Falling back to default", __func__);
701 app_type_cfg->app_type = platform_get_default_app_type_v2(platform, PCM_CAPTURE);
702 app_type_cfg->sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
703 app_type_cfg->bit_width = 16;
704}
705
706void audio_extn_utils_update_stream_output_app_type_cfg(void *platform,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700707 struct listnode *streams_output_cfg_list,
Amit Shekhar1d896042014-10-03 13:16:09 -0700708 audio_devices_t devices,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700709 audio_output_flags_t flags,
710 audio_format_t format,
Amit Shekhar6f461b12014-08-01 14:52:58 -0700711 uint32_t sample_rate,
712 uint32_t bit_width,
Manish Dewangan837dc462015-05-27 10:17:41 +0530713 audio_channel_mask_t channel_mask,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530714 char *profile,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700715 struct stream_app_type_cfg *app_type_cfg)
716{
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +0530717 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530718 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700719 struct stream_format *sf_info;
Manish Dewangan837dc462015-05-27 10:17:41 +0530720 char value[PROPERTY_VALUE_MAX] = {0};
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700721
Ramu Gottipati074fa4d2018-09-11 20:05:51 +0530722 if (devices & AUDIO_DEVICE_OUT_SPEAKER) {
723 int bw = platform_get_snd_device_bit_width(SND_DEVICE_OUT_SPEAKER);
724 if ((-ENOSYS != bw) && (bit_width > (uint32_t)bw))
Amit Shekhar5a39c912014-10-14 15:39:30 -0700725 bit_width = (uint32_t)bw;
Ramu Gottipati074fa4d2018-09-11 20:05:51 +0530726 else if (-ENOSYS == bw)
727 bit_width = CODEC_BACKEND_DEFAULT_BIT_WIDTH;
Amit Shekhar1d896042014-10-03 13:16:09 -0700728 sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Ramu Gottipati074fa4d2018-09-11 20:05:51 +0530729 ALOGI("%s Allowing 24 and above bits playback on speaker ONLY at default sampling rate", __func__);
Amit Shekhar1d896042014-10-03 13:16:09 -0700730 }
731
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -0700732 property_get("vendor.audio.playback.mch.downsample",value,"");
Manish Dewangan837dc462015-05-27 10:17:41 +0530733 if (!strncmp("true", value, sizeof("true"))) {
734 if ((popcount(channel_mask) > 2) &&
735 (sample_rate > CODEC_BACKEND_DEFAULT_SAMPLE_RATE) &&
Aalique Grahame5cd12ff2017-10-19 10:57:00 -0700736 !(flags & (audio_output_flags_t)AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH)) {
Manish Dewangan837dc462015-05-27 10:17:41 +0530737 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
738 ALOGD("%s: MCH session defaulting sample rate to %d",
739 __func__, sample_rate);
740 }
741 }
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +0530742
743 /* Set sampling rate to 176.4 for DSD64
744 * and 352.8Khz for DSD128.
745 * Set Bit Width to 16. output will be 16 bit
746 * post DoP in ASM.
747 */
Aalique Grahame5cd12ff2017-10-19 10:57:00 -0700748 if ((flags & (audio_output_flags_t)AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH) &&
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +0530749 (format == AUDIO_FORMAT_DSD)) {
750 bit_width = 16;
751 if (sample_rate == INPUT_SAMPLING_RATE_DSD64)
752 sample_rate = OUTPUT_SAMPLING_RATE_DSD64;
753 else if (sample_rate == INPUT_SAMPLING_RATE_DSD128)
754 sample_rate = OUTPUT_SAMPLING_RATE_DSD128;
755 }
756
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +0530757
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800758 ALOGV("%s: flags: %x, format: %x sample_rate %d, profile %s, app_type %d",
759 __func__, flags, format, sample_rate, profile, app_type_cfg->app_type);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700760 list_for_each(node_i, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530761 s_info = node_to_item(node_i, struct streams_io_cfg, list);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530762 /* Along with flags do profile matching if set at either end.*/
763 if (s_info->flags.out_flags == flags &&
764 ((profile[0] == '\0' && s_info->profile[0] == '\0') ||
765 strncmp(s_info->profile, profile, sizeof(s_info->profile)) == 0)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530766 list_for_each(node_j, &s_info->format_list) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700767 sf_info = node_to_item(node_j, struct stream_format, list);
768 if (sf_info->format == format) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530769 if (set_app_type_cfg(s_info, app_type_cfg, sample_rate, bit_width))
Amit Shekhar6f461b12014-08-01 14:52:58 -0700770 return;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700771 }
772 }
773 }
774 }
775 list_for_each(node_i, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530776 s_info = node_to_item(node_i, struct streams_io_cfg, list);
777 if (s_info->flags.out_flags == AUDIO_OUTPUT_FLAG_PRIMARY) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700778 ALOGV("Compatible output profile not found.");
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530779 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
780 app_type_cfg->sample_rate = s_info->app_type_cfg.sample_rate;
781 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700782 ALOGV("%s Default to primary output: App type: %d sample_rate %d",
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530783 __func__, s_info->app_type_cfg.app_type, app_type_cfg->sample_rate);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700784 return;
785 }
786 }
787 ALOGW("%s: App type could not be selected. Falling back to default", __func__);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700788 app_type_cfg->app_type = platform_get_default_app_type(platform);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700789 app_type_cfg->sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700790 app_type_cfg->bit_width = 16;
791}
792
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +0530793static bool audio_is_this_native_usecase(struct audio_usecase *uc)
794{
795 bool native_usecase = false;
796 struct stream_out *out = (struct stream_out*) uc->stream.out;
797
798 if (PCM_PLAYBACK == uc->type && out != NULL &&
799 NATIVE_AUDIO_MODE_INVALID != platform_get_native_support() &&
800 is_offload_usecase(uc->id) &&
801 (out->sample_rate == OUTPUT_SAMPLING_RATE_44100))
802 native_usecase = true;
803
804 return native_usecase;
805}
806
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800807bool audio_extn_is_dsp_bit_width_enforce_mode_supported(audio_output_flags_t flags)
808{
809 /* DSP bitwidth enforce mode for ADM and AFE:
810 * includes:
811 * deep buffer, low latency, direct pcm and offload.
812 * excludes:
813 * ull(raw+fast), VOIP.
814 */
815 if ((flags & AUDIO_OUTPUT_FLAG_VOIP_RX) ||
816 ((flags & AUDIO_OUTPUT_FLAG_RAW) &&
817 (flags & AUDIO_OUTPUT_FLAG_FAST)))
818 return false;
819
820
821 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) ||
822 (flags & AUDIO_OUTPUT_FLAG_DIRECT) ||
823 (flags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) ||
824 (flags & AUDIO_OUTPUT_FLAG_PRIMARY))
825 return true;
826 else
827 return false;
828}
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800829
830static inline bool audio_is_vr_mode_on(struct audio_device *(__attribute__((unused)) adev))
831{
832 return adev->vr_audio_mode_enabled;
833}
834
Derek Chenf7092792017-05-23 12:23:53 -0400835static void audio_extn_btsco_get_sample_rate(int snd_device, int *sample_rate)
836{
837 switch (snd_device) {
838 case SND_DEVICE_OUT_BT_SCO:
839 case SND_DEVICE_IN_BT_SCO_MIC:
840 case SND_DEVICE_IN_BT_SCO_MIC_NREC:
841 *sample_rate = 8000;
842 break;
843 case SND_DEVICE_OUT_BT_SCO_WB:
844 case SND_DEVICE_IN_BT_SCO_MIC_WB:
845 case SND_DEVICE_IN_BT_SCO_MIC_WB_NREC:
846 *sample_rate = 16000;
847 break;
848 default:
849 ALOGD("%s:Not a BT SCO device, need not update sampling rate\n", __func__);
850 break;
851 }
852}
853
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530854void audio_extn_utils_update_stream_app_type_cfg_for_usecase(
855 struct audio_device *adev,
856 struct audio_usecase *usecase)
857{
858 ALOGV("%s", __func__);
859
860 switch(usecase->type) {
861 case PCM_PLAYBACK:
862 audio_extn_utils_update_stream_output_app_type_cfg(adev->platform,
863 &adev->streams_output_cfg_list,
864 usecase->stream.out->devices,
865 usecase->stream.out->flags,
Aalique Grahame65780b52017-09-27 14:59:56 -0700866 usecase->stream.out->hal_op_format,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530867 usecase->stream.out->sample_rate,
868 usecase->stream.out->bit_width,
869 usecase->stream.out->channel_mask,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530870 usecase->stream.out->profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530871 &usecase->stream.out->app_type_cfg);
872 ALOGV("%s Selected apptype: %d", __func__, usecase->stream.out->app_type_cfg.app_type);
873 break;
874 case PCM_CAPTURE:
Vikram Panduranga93f080e2017-06-07 18:16:14 -0700875 if (usecase->id == USECASE_AUDIO_RECORD_VOIP)
876 usecase->stream.in->app_type_cfg.app_type = APP_TYPE_VOIP_AUDIO;
877 else
878 audio_extn_utils_update_stream_input_app_type_cfg(adev->platform,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530879 &adev->streams_input_cfg_list,
880 usecase->stream.in->device,
881 usecase->stream.in->flags,
882 usecase->stream.in->format,
883 usecase->stream.in->sample_rate,
884 usecase->stream.in->bit_width,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530885 usecase->stream.in->profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530886 &usecase->stream.in->app_type_cfg);
887 ALOGV("%s Selected apptype: %d", __func__, usecase->stream.in->app_type_cfg.app_type);
888 break;
Surendar Karka93cd25a2018-08-28 14:21:37 +0530889 case TRANSCODE_LOOPBACK_RX :
Siddartha Shaik343abc62017-08-08 11:15:25 +0530890 audio_extn_utils_update_stream_output_app_type_cfg(adev->platform,
891 &adev->streams_output_cfg_list,
892 usecase->stream.inout->out_config.devices,
893 0,
894 usecase->stream.inout->out_config.format,
895 usecase->stream.inout->out_config.sample_rate,
896 usecase->stream.inout->out_config.bit_width,
897 usecase->stream.inout->out_config.channel_mask,
898 usecase->stream.inout->profile,
899 &usecase->stream.inout->out_app_type_cfg);
900 ALOGV("%s Selected apptype: %d", __func__, usecase->stream.inout->out_app_type_cfg.app_type);
901 break;
Derek Chenf7092792017-05-23 12:23:53 -0400902 case PCM_HFP_CALL:
903 switch (usecase->id) {
904 case USECASE_AUDIO_HFP_SCO:
905 case USECASE_AUDIO_HFP_SCO_WB:
906 audio_extn_btsco_get_sample_rate(usecase->out_snd_device,
907 &usecase->out_app_type_cfg.sample_rate);
908 usecase->in_app_type_cfg.sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
909 break;
910 case USECASE_AUDIO_HFP_SCO_DOWNLINK:
911 case USECASE_AUDIO_HFP_SCO_WB_DOWNLINK:
912 audio_extn_btsco_get_sample_rate(usecase->in_snd_device,
913 &usecase->in_app_type_cfg.sample_rate);
914 usecase->out_app_type_cfg.sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
915 break;
916 default:
917 ALOGE("%s: usecase id (%d) not supported, use default sample rate",
918 __func__, usecase->id);
919 usecase->in_app_type_cfg.sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
920 usecase->out_app_type_cfg.sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
921 break;
922 }
923 /* update out_app_type_cfg */
924 usecase->out_app_type_cfg.bit_width =
925 platform_get_snd_device_bit_width(usecase->out_snd_device);
926 usecase->out_app_type_cfg.app_type =
927 platform_get_default_app_type_v2(adev->platform, PCM_PLAYBACK);
928 /* update in_app_type_cfg */
929 usecase->in_app_type_cfg.bit_width =
930 platform_get_snd_device_bit_width(usecase->in_snd_device);
931 usecase->in_app_type_cfg.app_type =
932 platform_get_default_app_type_v2(adev->platform, PCM_CAPTURE);
933 ALOGV("%s Selected apptype: playback %d capture %d",
934 __func__, usecase->out_app_type_cfg.app_type, usecase->in_app_type_cfg.app_type);
935 break;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530936 default:
937 ALOGE("%s: app type cfg not supported for usecase type (%d)",
938 __func__, usecase->type);
939 }
940}
941
Aalique Grahame22e49102018-12-18 14:23:57 -0800942static int set_stream_app_type_mixer_ctrl(struct audio_device *adev,
943 int pcm_device_id, int app_type,
944 int acdb_dev_id, int sample_rate,
945 int stream_type,
946 snd_device_t snd_device)
947{
948
949 char mixer_ctl_name[MAX_LENGTH_MIXER_CONTROL_IN_INT];
950 struct mixer_ctl *ctl;
951 int app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT], len = 0, rc = 0;
952 int snd_device_be_idx = -1;
953
954 if (stream_type == PCM_PLAYBACK) {
955 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
956 "Audio Stream %d App Type Cfg", pcm_device_id);
957 } else if (stream_type == PCM_CAPTURE) {
958 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
959 "Audio Stream Capture %d App Type Cfg", pcm_device_id);
960 }
961
962 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
963 if (!ctl) {
964 ALOGE("%s: Could not get ctl for mixer cmd - %s",
965 __func__, mixer_ctl_name);
966 rc = -EINVAL;
967 goto exit;
968 }
969 app_type_cfg[len++] = app_type;
970 app_type_cfg[len++] = acdb_dev_id;
971 app_type_cfg[len++] = sample_rate;
972
973 snd_device_be_idx = platform_get_snd_device_backend_index(snd_device);
974 if (snd_device_be_idx > 0)
975 app_type_cfg[len++] = snd_device_be_idx;
976 ALOGV("%s: stream type %d app_type %d, acdb_dev_id %d "
977 "sample rate %d, snd_device_be_idx %d",
978 __func__, stream_type, app_type, acdb_dev_id, sample_rate,
979 snd_device_be_idx);
980 mixer_ctl_set_array(ctl, app_type_cfg, len);
981
982exit:
983 return rc;
984}
985
986static int audio_extn_utils_send_app_type_cfg_hfp(struct audio_device *adev,
987 struct audio_usecase *usecase)
988{
989 int pcm_device_id, acdb_dev_id = 0, snd_device = usecase->out_snd_device;
990 int32_t sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
991 int app_type = 0, rc = 0;
Derek Chenf7092792017-05-23 12:23:53 -0400992 bool is_bus_dev_usecase = false;
Aalique Grahame22e49102018-12-18 14:23:57 -0800993
994 ALOGV("%s", __func__);
995
996 if (usecase->type != PCM_HFP_CALL) {
Derek Chenf7092792017-05-23 12:23:53 -0400997 ALOGV("%s: not a HFP path, no need to cfg app type", __func__);
Aalique Grahame22e49102018-12-18 14:23:57 -0800998 rc = 0;
999 goto exit_send_app_type_cfg;
1000 }
1001 if ((usecase->id != USECASE_AUDIO_HFP_SCO) &&
Derek Chenf7092792017-05-23 12:23:53 -04001002 (usecase->id != USECASE_AUDIO_HFP_SCO_WB) &&
1003 (usecase->id != USECASE_AUDIO_HFP_SCO_DOWNLINK) &&
1004 (usecase->id != USECASE_AUDIO_HFP_SCO_WB_DOWNLINK)) {
1005 ALOGV("%s: a usecase where app type cfg is not required", __func__);
Aalique Grahame22e49102018-12-18 14:23:57 -08001006 rc = 0;
1007 goto exit_send_app_type_cfg;
1008 }
1009
Derek Chenf7092792017-05-23 12:23:53 -04001010 if (usecase->devices & AUDIO_DEVICE_OUT_BUS)
1011 is_bus_dev_usecase = true;
1012
Aalique Grahame22e49102018-12-18 14:23:57 -08001013 snd_device = usecase->out_snd_device;
1014 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_PLAYBACK);
1015
1016 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
1017 if (acdb_dev_id < 0) {
1018 ALOGE("%s: Couldn't get the acdb dev id", __func__);
1019 rc = -EINVAL;
1020 goto exit_send_app_type_cfg;
1021 }
1022
1023 if (usecase->type == PCM_HFP_CALL) {
1024
1025 /* config HFP session:1 playback path */
Derek Chenf7092792017-05-23 12:23:53 -04001026 if (is_bus_dev_usecase) {
1027 app_type = usecase->out_app_type_cfg.app_type;
1028 sample_rate= usecase->out_app_type_cfg.sample_rate;
1029 } else {
1030 snd_device = SND_DEVICE_NONE; // use legacy behavior
1031 app_type = platform_get_default_app_type_v2(adev->platform, PCM_PLAYBACK);
1032 sample_rate= CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
1033 }
Aalique Grahame22e49102018-12-18 14:23:57 -08001034 rc = set_stream_app_type_mixer_ctrl(adev, pcm_device_id, app_type,
1035 acdb_dev_id, sample_rate,
1036 PCM_PLAYBACK,
Derek Chenf7092792017-05-23 12:23:53 -04001037 snd_device);
Aalique Grahame22e49102018-12-18 14:23:57 -08001038 if (rc < 0)
1039 goto exit_send_app_type_cfg;
1040
1041 /* config HFP session:1 capture path */
Derek Chenf7092792017-05-23 12:23:53 -04001042 if (is_bus_dev_usecase) {
1043 snd_device = usecase->in_snd_device;
1044 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_CAPTURE);
1045 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
1046 if (acdb_dev_id < 0) {
1047 ALOGE("%s: Couldn't get the acdb dev id", __func__);
1048 rc = -EINVAL;
1049 goto exit_send_app_type_cfg;
1050 }
1051 app_type = usecase->in_app_type_cfg.app_type;
1052 sample_rate= usecase->in_app_type_cfg.sample_rate;
1053 } else {
1054 snd_device = SND_DEVICE_NONE; // use legacy behavior
1055 app_type = platform_get_default_app_type_v2(adev->platform, PCM_CAPTURE);
1056 }
Aalique Grahame22e49102018-12-18 14:23:57 -08001057 rc = set_stream_app_type_mixer_ctrl(adev, pcm_device_id, app_type,
1058 acdb_dev_id, sample_rate,
1059 PCM_CAPTURE,
Derek Chenf7092792017-05-23 12:23:53 -04001060 snd_device);
Aalique Grahame22e49102018-12-18 14:23:57 -08001061 if (rc < 0)
1062 goto exit_send_app_type_cfg;
1063
Derek Chenf7092792017-05-23 12:23:53 -04001064 if (is_bus_dev_usecase)
1065 goto exit_send_app_type_cfg;
1066
Aalique Grahame22e49102018-12-18 14:23:57 -08001067 /* config HFP session:2 capture path */
1068 pcm_device_id = HFP_ASM_RX_TX;
1069 snd_device = usecase->in_snd_device;
1070 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
1071 if (acdb_dev_id <= 0) {
1072 ALOGE("%s: Couldn't get the acdb dev id", __func__);
1073 rc = -EINVAL;
1074 goto exit_send_app_type_cfg;
1075 }
1076 app_type = platform_get_default_app_type_v2(adev->platform, PCM_CAPTURE);
1077 rc = set_stream_app_type_mixer_ctrl(adev, pcm_device_id, app_type,
1078 acdb_dev_id, sample_rate, PCM_CAPTURE,
1079 SND_DEVICE_NONE);
1080 if (rc < 0)
1081 goto exit_send_app_type_cfg;
1082
1083 /* config HFP session:2 playback path */
1084 app_type = platform_get_default_app_type_v2(adev->platform, PCM_PLAYBACK);
1085 rc = set_stream_app_type_mixer_ctrl(adev, pcm_device_id, app_type,
1086 acdb_dev_id, sample_rate,
1087 PCM_PLAYBACK, SND_DEVICE_NONE);
1088 if (rc < 0)
1089 goto exit_send_app_type_cfg;
1090 }
1091
1092 rc = 0;
1093exit_send_app_type_cfg:
1094 return rc;
1095}
1096
Zhou Song06761dd2019-04-28 18:08:17 +08001097int audio_extn_utils_get_app_sample_rate_for_device(
1098 struct audio_device *adev,
1099 struct audio_usecase *usecase, int snd_device)
1100{
1101 char value[PROPERTY_VALUE_MAX] = {0};
1102 int sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
1103
1104 if ((usecase->type == PCM_PLAYBACK) && (usecase->stream.out != NULL)) {
1105 property_get("vendor.audio.playback.mch.downsample",value,"");
1106 if (!strncmp("true", value, sizeof("true"))) {
1107 if ((popcount(usecase->stream.out->channel_mask) > 2) &&
1108 (usecase->stream.out->app_type_cfg.sample_rate > CODEC_BACKEND_DEFAULT_SAMPLE_RATE) &&
1109 !(usecase->stream.out->flags &
1110 (audio_output_flags_t)AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH))
1111 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
1112 }
1113
1114 if (usecase->id == USECASE_AUDIO_PLAYBACK_VOIP) {
1115 usecase->stream.out->app_type_cfg.sample_rate = usecase->stream.out->sample_rate;
1116 } else if (usecase->stream.out->devices & AUDIO_DEVICE_OUT_SPEAKER) {
1117 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
1118 } else if ((snd_device == SND_DEVICE_OUT_HDMI ||
1119 snd_device == SND_DEVICE_OUT_USB_HEADSET ||
1120 snd_device == SND_DEVICE_OUT_DISPLAY_PORT) &&
1121 (usecase->stream.out->sample_rate >= OUTPUT_SAMPLING_RATE_44100)) {
1122 /*
1123 * To best utlize DSP, check if the stream sample rate is supported/multiple of
1124 * configured device sample rate, if not update the COPP rate to be equal to the
1125 * device sample rate, else open COPP at stream sample rate
1126 */
1127 platform_check_and_update_copp_sample_rate(adev->platform, snd_device,
1128 usecase->stream.out->sample_rate,
1129 &usecase->stream.out->app_type_cfg.sample_rate);
1130 } else if (((snd_device != SND_DEVICE_OUT_HEADPHONES_44_1 &&
1131 !audio_is_this_native_usecase(usecase)) &&
1132 usecase->stream.out->sample_rate == OUTPUT_SAMPLING_RATE_44100) ||
1133 (usecase->stream.out->sample_rate < OUTPUT_SAMPLING_RATE_44100)) {
1134 /* Reset to default if no native stream is active*/
1135 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
1136 } else if (usecase->stream.out->devices & AUDIO_DEVICE_OUT_ALL_A2DP) {
1137 /*
1138 * For a2dp playback get encoder sampling rate and set copp sampling rate,
1139 * for bit width use the stream param only.
1140 */
1141 audio_extn_a2dp_get_enc_sample_rate(&usecase->stream.out->app_type_cfg.sample_rate);
1142 ALOGI("%s using %d sample rate rate for A2DP CoPP",
1143 __func__, usecase->stream.out->app_type_cfg.sample_rate);
1144 }
1145 audio_extn_btsco_get_sample_rate(snd_device, &usecase->stream.out->app_type_cfg.sample_rate);
1146 sample_rate = usecase->stream.out->app_type_cfg.sample_rate;
1147
1148 if (((usecase->stream.out->format == AUDIO_FORMAT_E_AC3) ||
1149 (usecase->stream.out->format == AUDIO_FORMAT_E_AC3_JOC) ||
1150 (usecase->stream.out->format == AUDIO_FORMAT_DOLBY_TRUEHD))
1151 && audio_extn_passthru_is_passthrough_stream(usecase->stream.out)
1152 && !audio_extn_passthru_is_convert_supported(adev, usecase->stream.out)) {
1153 sample_rate = sample_rate * 4;
1154 if (sample_rate > HDMI_PASSTHROUGH_MAX_SAMPLE_RATE)
1155 sample_rate = HDMI_PASSTHROUGH_MAX_SAMPLE_RATE;
1156 }
1157 } else if (usecase->type == PCM_CAPTURE) {
1158 if (usecase->stream.in != NULL) {
1159 if (usecase->id == USECASE_AUDIO_RECORD_VOIP)
1160 usecase->stream.in->app_type_cfg.sample_rate = usecase->stream.in->sample_rate;
1161 if (voice_is_in_call_rec_stream(usecase->stream.in)) {
1162 audio_extn_btsco_get_sample_rate(usecase->in_snd_device,
1163 &usecase->stream.in->app_type_cfg.sample_rate);
1164 } else {
1165 audio_extn_btsco_get_sample_rate(snd_device,
1166 &usecase->stream.in->app_type_cfg.sample_rate);
1167 }
1168 sample_rate = usecase->stream.in->app_type_cfg.sample_rate;
1169 } else if (usecase->id == USECASE_AUDIO_SPKR_CALIB_TX) {
1170 sample_rate = SAMPLE_RATE_8000;
1171 }
1172 } else if (usecase->type == TRANSCODE_LOOPBACK_RX) {
1173 sample_rate = usecase->stream.inout->out_config.sample_rate;
1174 }
1175 return sample_rate;
1176}
1177
Siena Richard7c2db772016-12-21 11:32:34 -08001178static int send_app_type_cfg_for_device(struct audio_device *adev,
1179 struct audio_usecase *usecase,
1180 int split_snd_device)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001181{
1182 char mixer_ctl_name[MAX_LENGTH_MIXER_CONTROL_IN_INT];
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301183 size_t app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT] = {0};
1184 int len = 0, rc;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001185 struct mixer_ctl *ctl;
Siena Richard7c2db772016-12-21 11:32:34 -08001186 int pcm_device_id = 0, acdb_dev_id, app_type;
1187 int snd_device = split_snd_device, snd_device_be_idx = -1;
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +05301188 int32_t sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Varun Balaraje49253e2017-07-06 19:48:56 +05301189 struct streams_io_cfg *s_info = NULL;
1190 struct listnode *node = NULL;
Nikhil Laturkarac4a7492017-08-31 18:27:19 +05301191 int bd_app_type = 0;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001192
Siena Richard7c2db772016-12-21 11:32:34 -08001193 ALOGV("%s: usecase->out_snd_device %s, usecase->in_snd_device %s, split_snd_device %s",
1194 __func__, platform_get_snd_device_name(usecase->out_snd_device),
1195 platform_get_snd_device_name(usecase->in_snd_device),
1196 platform_get_snd_device_name(split_snd_device));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001197
Siddartha Shaik343abc62017-08-08 11:15:25 +05301198 if (usecase->type != PCM_PLAYBACK && usecase->type != PCM_CAPTURE &&
Surendar Karka93cd25a2018-08-28 14:21:37 +05301199 usecase->type != TRANSCODE_LOOPBACK_RX) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301200 ALOGE("%s: not a playback/capture path, no need to cfg app type", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001201 rc = 0;
1202 goto exit_send_app_type_cfg;
1203 }
1204 if ((usecase->id != USECASE_AUDIO_PLAYBACK_DEEP_BUFFER) &&
Vignesh Kulothungan0a2eff02019-07-11 16:30:13 -07001205 (usecase->id != USECASE_AUDIO_PLAYBACK_WITH_HAPTICS) &&
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001206 (usecase->id != USECASE_AUDIO_PLAYBACK_LOW_LATENCY) &&
1207 (usecase->id != USECASE_AUDIO_PLAYBACK_MULTI_CH) &&
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -08001208 (usecase->id != USECASE_AUDIO_PLAYBACK_ULL) &&
Vikram Panduranga93f080e2017-06-07 18:16:14 -07001209 (usecase->id != USECASE_AUDIO_PLAYBACK_VOIP) &&
Surendar Karka93cd25a2018-08-28 14:21:37 +05301210 (usecase->id != USECASE_AUDIO_TRANSCODE_LOOPBACK_RX) &&
Varun Balaraje49253e2017-07-06 19:48:56 +05301211 (!is_interactive_usecase(usecase->id)) &&
Srikanth Uyyala9d551402015-08-25 16:03:42 +05301212 (!is_offload_usecase(usecase->id)) &&
Derek Chenf6318be2017-06-12 17:16:24 -04001213 (usecase->type != PCM_CAPTURE) &&
1214 (!audio_extn_auto_hal_is_bus_device_usecase(usecase->id))) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301215 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 -07001216 rc = 0;
1217 goto exit_send_app_type_cfg;
1218 }
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -08001219
1220 //if VR is active then only send the mixer control
1221 if (usecase->id == USECASE_AUDIO_PLAYBACK_ULL && !audio_is_vr_mode_on(adev)) {
1222 ALOGI("ULL doesnt need sending app type cfg, returning");
1223 rc = 0;
1224 goto exit_send_app_type_cfg;
1225 }
1226
Surendar Karka93cd25a2018-08-28 14:21:37 +05301227 if (usecase->type == PCM_PLAYBACK || usecase->type == TRANSCODE_LOOPBACK_RX) {
Ben Romberger1fafdde2015-09-09 19:43:15 -07001228 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_PLAYBACK);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301229 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
1230 "Audio Stream %d App Type Cfg", pcm_device_id);
Ben Romberger1fafdde2015-09-09 19:43:15 -07001231 } else if (usecase->type == PCM_CAPTURE) {
Ben Romberger1fafdde2015-09-09 19:43:15 -07001232 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_CAPTURE);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301233 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
1234 "Audio Stream Capture %d App Type Cfg", pcm_device_id);
Srikanth Uyyala9d551402015-08-25 16:03:42 +05301235 }
Siena Richard7c2db772016-12-21 11:32:34 -08001236
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001237 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
1238 if (!ctl) {
1239 ALOGE("%s: Could not get ctl for mixer cmd - %s", __func__,
1240 mixer_ctl_name);
1241 rc = -EINVAL;
1242 goto exit_send_app_type_cfg;
1243 }
Aditya Bavanari701a6992017-03-30 19:17:16 +05301244 snd_device = platform_get_spkr_prot_snd_device(snd_device);
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301245 if (voice_is_in_call_rec_stream(usecase->stream.in) && usecase->type == PCM_CAPTURE) {
1246 snd_device_t voice_device = voice_get_incall_rec_snd_device(usecase->in_snd_device);
1247 acdb_dev_id = platform_get_snd_device_acdb_id(voice_device);
1248 ALOGV("acdb id for voice call use case %d", acdb_dev_id);
1249 } else {
1250 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
1251 }
Rohit Kumar1181b292017-01-31 18:07:17 +05301252 if (acdb_dev_id <= 0) {
1253 ALOGE("%s: Couldn't get the acdb dev id", __func__);
1254 rc = -EINVAL;
1255 goto exit_send_app_type_cfg;
1256 }
1257
Siena Richard7c2db772016-12-21 11:32:34 -08001258 snd_device_be_idx = platform_get_snd_device_backend_index(snd_device);
1259 if (snd_device_be_idx < 0) {
1260 ALOGE("%s: Couldn't get the backend index for snd device %s ret=%d",
1261 __func__, platform_get_snd_device_name(snd_device),
1262 snd_device_be_idx);
1263 }
1264
Zhou Song06761dd2019-04-28 18:08:17 +08001265 sample_rate = audio_extn_utils_get_app_sample_rate_for_device(adev, usecase, snd_device);
1266
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301267 if ((usecase->type == PCM_PLAYBACK) && (usecase->stream.out != NULL)) {
Varun Balaraje49253e2017-07-06 19:48:56 +05301268 /* Interactive streams are supported with only direct app type id.
1269 * Get Direct profile app type and use it for interactive streams
1270 */
1271 list_for_each(node, &adev->streams_output_cfg_list) {
1272 s_info = node_to_item(node, struct streams_io_cfg, list);
Aalique Grahame5cd12ff2017-10-19 10:57:00 -07001273 if (s_info->flags.out_flags == (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_BD |
Nikhil Laturkarac4a7492017-08-31 18:27:19 +05301274 AUDIO_OUTPUT_FLAG_DIRECT_PCM |
1275 AUDIO_OUTPUT_FLAG_DIRECT))
1276 bd_app_type = s_info->app_type_cfg.app_type;
Varun Balaraje49253e2017-07-06 19:48:56 +05301277 }
Aalique Grahame5cd12ff2017-10-19 10:57:00 -07001278 if (usecase->stream.out->flags == (audio_output_flags_t)AUDIO_OUTPUT_FLAG_INTERACTIVE)
Nikhil Laturkarac4a7492017-08-31 18:27:19 +05301279 app_type = bd_app_type;
Varun Balaraje49253e2017-07-06 19:48:56 +05301280 else
1281 app_type = usecase->stream.out->app_type_cfg.app_type;
Siena Richard7c2db772016-12-21 11:32:34 -08001282 app_type_cfg[len++] = app_type;
Mingming Yin21854652016-04-13 11:54:02 -07001283 app_type_cfg[len++] = acdb_dev_id;
Naresh Tanniru3a406772017-05-10 13:09:05 -07001284 app_type_cfg[len++] = sample_rate;
1285
Siena Richard7c2db772016-12-21 11:32:34 -08001286 if (snd_device_be_idx > 0)
1287 app_type_cfg[len++] = snd_device_be_idx;
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301288
Siena Richard7c2db772016-12-21 11:32:34 -08001289 ALOGI("%s PLAYBACK app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
1290 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301291
1292 } else if ((usecase->type == PCM_CAPTURE) && (usecase->stream.in != NULL)) {
Siena Richard7c2db772016-12-21 11:32:34 -08001293 app_type = usecase->stream.in->app_type_cfg.app_type;
1294 app_type_cfg[len++] = app_type;
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301295 app_type_cfg[len++] = acdb_dev_id;
Siena Richard7c2db772016-12-21 11:32:34 -08001296 app_type_cfg[len++] = sample_rate;
1297 if (snd_device_be_idx > 0)
1298 app_type_cfg[len++] = snd_device_be_idx;
1299 ALOGI("%s CAPTURE app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
1300 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301301 } else {
Siena Richard7c2db772016-12-21 11:32:34 -08001302 app_type = platform_get_default_app_type_v2(adev->platform, usecase->type);
Surendar Karka93cd25a2018-08-28 14:21:37 +05301303 if(usecase->type == TRANSCODE_LOOPBACK_RX) {
Siddartha Shaik343abc62017-08-08 11:15:25 +05301304 app_type = usecase->stream.inout->out_app_type_cfg.app_type;
1305 }
Siena Richard7c2db772016-12-21 11:32:34 -08001306 app_type_cfg[len++] = app_type;
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301307 app_type_cfg[len++] = acdb_dev_id;
1308 app_type_cfg[len++] = sample_rate;
Siena Richard7c2db772016-12-21 11:32:34 -08001309 if (snd_device_be_idx > 0)
1310 app_type_cfg[len++] = snd_device_be_idx;
1311 ALOGI("%s default app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
1312 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Ashish Jainc02430d2016-01-04 10:42:43 +05301313 }
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301314
Siddartha Shaik343abc62017-08-08 11:15:25 +05301315 if(ctl)
1316 mixer_ctl_set_array(ctl, app_type_cfg, len);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001317 rc = 0;
1318exit_send_app_type_cfg:
1319 return rc;
1320}
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001321
Deeraj Soman14230922019-01-30 16:39:30 +05301322static int audio_extn_utils_check_input_parameters(uint32_t sample_rate,
1323 audio_format_t format,
1324 int channel_count)
1325{
1326 int ret = 0;
1327
1328 if (((format != AUDIO_FORMAT_PCM_16_BIT) && (format != AUDIO_FORMAT_PCM_8_24_BIT) &&
1329 (format != AUDIO_FORMAT_PCM_24_BIT_PACKED) && (format != AUDIO_FORMAT_PCM_32_BIT) &&
1330 (format != AUDIO_FORMAT_PCM_FLOAT)) &&
1331 !voice_extn_compress_voip_is_format_supported(format) &&
1332 !audio_extn_compr_cap_format_supported(format) &&
1333 !audio_extn_cin_format_supported(format))
1334 ret = -EINVAL;
1335
1336 switch (channel_count) {
1337 case 1:
1338 case 2:
1339 case 3:
1340 case 4:
1341 case 6:
1342 case 8:
1343 break;
1344 default:
1345 ret = -EINVAL;
1346 }
1347
1348 switch (sample_rate) {
1349 case 8000:
1350 case 11025:
1351 case 12000:
1352 case 16000:
1353 case 22050:
1354 case 24000:
1355 case 32000:
1356 case 44100:
1357 case 48000:
1358 case 88200:
1359 case 96000:
1360 case 176400:
1361 case 192000:
1362 break;
1363 default:
1364 ret = -EINVAL;
1365 }
1366
1367 return ret;
1368}
1369
1370static inline uint32_t audio_extn_utils_nearest_multiple(uint32_t num, uint32_t multiplier)
1371{
1372 uint32_t remainder = 0;
1373
1374 if (!multiplier)
1375 return num;
1376
1377 remainder = num % multiplier;
1378 if (remainder)
1379 num += (multiplier - remainder);
1380
1381 return num;
1382}
1383
1384static inline uint32_t audio_extn_utils_lcm(uint32_t num1, uint32_t num2)
1385{
1386 uint32_t high = num1, low = num2, temp = 0;
1387
1388 if (!num1 || !num2)
1389 return 0;
1390
1391 if (num1 < num2) {
1392 high = num2;
1393 low = num1;
1394 }
1395
1396 while (low != 0) {
1397 temp = low;
1398 low = high % low;
1399 high = temp;
1400 }
1401 return (num1 * num2)/high;
1402}
1403
Siena Richard7c2db772016-12-21 11:32:34 -08001404int audio_extn_utils_send_app_type_cfg(struct audio_device *adev,
1405 struct audio_usecase *usecase)
1406{
1407 int i, num_devices = 0;
1408 snd_device_t new_snd_devices[SND_DEVICE_OUT_END] = {0};
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301409 snd_device_t in_snd_device = usecase->in_snd_device;
Siena Richard7c2db772016-12-21 11:32:34 -08001410 int rc = 0;
1411
Aalique Grahame22e49102018-12-18 14:23:57 -08001412 if (usecase->type == PCM_HFP_CALL) {
1413 return audio_extn_utils_send_app_type_cfg_hfp(adev, usecase);
1414 }
1415
Siena Richard7c2db772016-12-21 11:32:34 -08001416 switch (usecase->type) {
1417 case PCM_PLAYBACK:
Surendar Karka93cd25a2018-08-28 14:21:37 +05301418 case TRANSCODE_LOOPBACK_RX:
Siena Richard7c2db772016-12-21 11:32:34 -08001419 ALOGD("%s: usecase->out_snd_device %s",
1420 __func__, platform_get_snd_device_name(usecase->out_snd_device));
1421 /* check for out combo device */
1422 if (platform_split_snd_device(adev->platform,
1423 usecase->out_snd_device,
1424 &num_devices, new_snd_devices)) {
1425 new_snd_devices[0] = usecase->out_snd_device;
1426 num_devices = 1;
1427 }
1428 break;
1429 case PCM_CAPTURE:
1430 ALOGD("%s: usecase->in_snd_device %s",
1431 __func__, platform_get_snd_device_name(usecase->in_snd_device));
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301432 if (voice_is_in_call_rec_stream(usecase->stream.in)) {
1433 in_snd_device = voice_get_incall_rec_backend_device(usecase->stream.in);
1434 }
Siena Richard7c2db772016-12-21 11:32:34 -08001435 /* check for in combo device */
1436 if (platform_split_snd_device(adev->platform,
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301437 in_snd_device,
Siena Richard7c2db772016-12-21 11:32:34 -08001438 &num_devices, new_snd_devices)) {
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301439 new_snd_devices[0] = in_snd_device;
Siena Richard7c2db772016-12-21 11:32:34 -08001440 num_devices = 1;
1441 }
1442 break;
1443 default:
1444 ALOGI("%s: not a playback/capture path, no need to cfg app type", __func__);
1445 rc = 0;
1446 break;
1447 }
1448
1449 for (i = 0; i < num_devices; i++) {
1450 rc = send_app_type_cfg_for_device(adev, usecase, new_snd_devices[i]);
1451 if (rc)
1452 break;
1453 }
1454
1455 return rc;
1456}
1457
Preetam Singh Ranawat9519e9c2015-11-18 16:05:55 +05301458int read_line_from_file(const char *path, char *buf, size_t count)
1459{
1460 char * fgets_ret;
1461 FILE * fd;
1462 int rv;
1463
1464 fd = fopen(path, "r");
1465 if (fd == NULL)
1466 return -1;
1467
1468 fgets_ret = fgets(buf, (int)count, fd);
1469 if (NULL != fgets_ret) {
1470 rv = (int)strlen(buf);
1471 } else {
1472 rv = ferror(fd);
1473 }
1474 fclose(fd);
1475
1476 return rv;
1477}
1478
Ashish Jainf1eaa582016-05-23 20:54:24 +05301479/*Translates ALSA formats to AOSP PCM formats*/
1480audio_format_t alsa_format_to_hal(uint32_t alsa_format)
1481{
1482 audio_format_t format;
1483
1484 switch(alsa_format) {
1485 case SNDRV_PCM_FORMAT_S16_LE:
1486 format = AUDIO_FORMAT_PCM_16_BIT;
1487 break;
1488 case SNDRV_PCM_FORMAT_S24_3LE:
1489 format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
1490 break;
1491 case SNDRV_PCM_FORMAT_S24_LE:
1492 format = AUDIO_FORMAT_PCM_8_24_BIT;
1493 break;
1494 case SNDRV_PCM_FORMAT_S32_LE:
1495 format = AUDIO_FORMAT_PCM_32_BIT;
1496 break;
1497 default:
1498 ALOGW("Incorrect ALSA format");
1499 format = AUDIO_FORMAT_INVALID;
1500 }
1501 return format;
1502}
1503
1504/*Translates hal format (AOSP) to alsa formats*/
1505uint32_t hal_format_to_alsa(audio_format_t hal_format)
1506{
1507 uint32_t alsa_format;
1508
1509 switch (hal_format) {
1510 case AUDIO_FORMAT_PCM_32_BIT: {
1511 if (platform_supports_true_32bit())
1512 alsa_format = SNDRV_PCM_FORMAT_S32_LE;
1513 else
1514 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1515 }
1516 break;
1517 case AUDIO_FORMAT_PCM_8_BIT:
1518 alsa_format = SNDRV_PCM_FORMAT_S8;
1519 break;
1520 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
1521 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1522 break;
1523 case AUDIO_FORMAT_PCM_8_24_BIT: {
1524 if (platform_supports_true_32bit())
1525 alsa_format = SNDRV_PCM_FORMAT_S32_LE;
1526 else
1527 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1528 }
1529 break;
1530 case AUDIO_FORMAT_PCM_FLOAT:
1531 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1532 break;
1533 default:
1534 case AUDIO_FORMAT_PCM_16_BIT:
1535 alsa_format = SNDRV_PCM_FORMAT_S16_LE;
1536 break;
1537 }
1538 return alsa_format;
1539}
1540
Ashish Jain83a6cc22016-06-28 14:34:17 +05301541/*Translates PCM formats to AOSP formats*/
1542audio_format_t pcm_format_to_hal(uint32_t pcm_format)
1543{
1544 audio_format_t format = AUDIO_FORMAT_INVALID;
1545
1546 switch(pcm_format) {
1547 case PCM_FORMAT_S16_LE:
1548 format = AUDIO_FORMAT_PCM_16_BIT;
1549 break;
1550 case PCM_FORMAT_S24_3LE:
1551 format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
1552 break;
1553 case PCM_FORMAT_S24_LE:
1554 format = AUDIO_FORMAT_PCM_8_24_BIT;
1555 break;
1556 case PCM_FORMAT_S32_LE:
1557 format = AUDIO_FORMAT_PCM_32_BIT;
1558 break;
1559 default:
1560 ALOGW("Incorrect PCM format");
1561 format = AUDIO_FORMAT_INVALID;
1562 }
1563 return format;
1564}
1565
1566/*Translates hal format (AOSP) to alsa formats*/
1567uint32_t hal_format_to_pcm(audio_format_t hal_format)
1568{
1569 uint32_t pcm_format;
1570
1571 switch (hal_format) {
1572 case AUDIO_FORMAT_PCM_32_BIT:
1573 case AUDIO_FORMAT_PCM_8_24_BIT:
1574 case AUDIO_FORMAT_PCM_FLOAT: {
1575 if (platform_supports_true_32bit())
1576 pcm_format = PCM_FORMAT_S32_LE;
1577 else
1578 pcm_format = PCM_FORMAT_S24_3LE;
1579 }
1580 break;
1581 case AUDIO_FORMAT_PCM_8_BIT:
1582 pcm_format = PCM_FORMAT_S8;
1583 break;
1584 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
1585 pcm_format = PCM_FORMAT_S24_3LE;
1586 break;
1587 default:
1588 case AUDIO_FORMAT_PCM_16_BIT:
1589 pcm_format = PCM_FORMAT_S16_LE;
1590 break;
1591 }
1592 return pcm_format;
1593}
1594
Ashish Jainf1eaa582016-05-23 20:54:24 +05301595uint32_t get_alsa_fragment_size(uint32_t bytes_per_sample,
1596 uint32_t sample_rate,
Deeraj Soman65358ab2019-02-07 15:40:49 +05301597 uint32_t noOfChannels,
1598 int64_t duration_ms)
Ashish Jainf1eaa582016-05-23 20:54:24 +05301599{
1600 uint32_t fragment_size = 0;
1601 uint32_t pcm_offload_time = PCM_OFFLOAD_BUFFER_DURATION;
1602
Deeraj Soman65358ab2019-02-07 15:40:49 +05301603 if (duration_ms >= MIN_OFFLOAD_BUFFER_DURATION_MS && duration_ms <= MAX_OFFLOAD_BUFFER_DURATION_MS)
1604 pcm_offload_time = duration_ms;
1605
Ashish Jainf1eaa582016-05-23 20:54:24 +05301606 fragment_size = (pcm_offload_time
1607 * sample_rate
1608 * bytes_per_sample
1609 * noOfChannels)/1000;
1610 if (fragment_size < MIN_PCM_OFFLOAD_FRAGMENT_SIZE)
1611 fragment_size = MIN_PCM_OFFLOAD_FRAGMENT_SIZE;
1612 else if (fragment_size > MAX_PCM_OFFLOAD_FRAGMENT_SIZE)
1613 fragment_size = MAX_PCM_OFFLOAD_FRAGMENT_SIZE;
1614 /*To have same PCM samples for all channels, the buffer size requires to
1615 *be multiple of (number of channels * bytes per sample)
1616 *For writes to succeed, the buffer must be written at address which is multiple of 32
1617 */
Aalique Grahameb85f2d92017-10-16 17:53:23 -07001618 fragment_size = ALIGN(fragment_size, (bytes_per_sample * noOfChannels * 32));
Ashish Jainf1eaa582016-05-23 20:54:24 +05301619
1620 ALOGI("PCM offload Fragment size to %d bytes", fragment_size);
1621 return fragment_size;
1622}
1623
1624/* Calculates the fragment size required to configure compress session.
1625 * Based on the alsa format selected, decide if conversion is needed in
1626
1627 * HAL ( e.g. convert AUDIO_FORMAT_PCM_FLOAT input format to
1628 * AUDIO_FORMAT_PCM_24_BIT_PACKED before writing to the compress driver.
1629 */
1630void audio_extn_utils_update_direct_pcm_fragment_size(struct stream_out *out)
1631{
Ashish Jain83a6cc22016-06-28 14:34:17 +05301632 audio_format_t dst_format = out->hal_op_format;
1633 audio_format_t src_format = out->hal_ip_format;
Ashish Jainf1eaa582016-05-23 20:54:24 +05301634 uint32_t hal_op_bytes_per_sample = audio_bytes_per_sample(dst_format);
1635 uint32_t hal_ip_bytes_per_sample = audio_bytes_per_sample(src_format);
1636
1637 out->compr_config.fragment_size =
1638 get_alsa_fragment_size(hal_op_bytes_per_sample,
1639 out->sample_rate,
Deeraj Soman65358ab2019-02-07 15:40:49 +05301640 popcount(out->channel_mask),
1641 out->info.duration_us / 1000);
Ashish Jainf1eaa582016-05-23 20:54:24 +05301642
1643 if ((src_format != dst_format) &&
1644 hal_op_bytes_per_sample != hal_ip_bytes_per_sample) {
1645
Ashish Jain83a6cc22016-06-28 14:34:17 +05301646 out->hal_fragment_size =
Ashish Jainf1eaa582016-05-23 20:54:24 +05301647 ((out->compr_config.fragment_size * hal_ip_bytes_per_sample) /
1648 hal_op_bytes_per_sample);
1649 ALOGI("enable conversion hal_input_fragment_size is %d src_format %x dst_format %x",
Ashish Jain83a6cc22016-06-28 14:34:17 +05301650 out->hal_fragment_size, src_format, dst_format);
Ashish Jainf1eaa582016-05-23 20:54:24 +05301651 } else {
Ashish Jain83a6cc22016-06-28 14:34:17 +05301652 out->hal_fragment_size = out->compr_config.fragment_size;
Ashish Jainf1eaa582016-05-23 20:54:24 +05301653 }
1654}
1655
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301656/* converts pcm format 24_8 to 8_24 inplace */
1657size_t audio_extn_utils_convert_format_24_8_to_8_24(void *buf, size_t bytes)
1658{
1659 size_t i = 0;
1660 int *int_buf_stream = buf;
1661
1662 if ((bytes % 4) != 0) {
1663 ALOGE("%s: wrong inout buffer! ... is not 32 bit aligned ", __func__);
1664 return -EINVAL;
1665 }
1666
1667 for (; i < (bytes / 4); i++)
1668 int_buf_stream[i] >>= 8;
1669
1670 return bytes;
1671}
1672
1673int get_snd_codec_id(audio_format_t format)
1674{
1675 int id = 0;
1676
1677 switch (format & AUDIO_FORMAT_MAIN_MASK) {
1678 case AUDIO_FORMAT_MP3:
1679 id = SND_AUDIOCODEC_MP3;
1680 break;
1681 case AUDIO_FORMAT_AAC:
1682 id = SND_AUDIOCODEC_AAC;
1683 break;
1684 case AUDIO_FORMAT_AAC_ADTS:
1685 id = SND_AUDIOCODEC_AAC;
1686 break;
Arun Kumar Dasari3b174182016-12-27 13:01:14 +05301687 case AUDIO_FORMAT_AAC_LATM:
1688 id = SND_AUDIOCODEC_AAC;
1689 break;
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301690 case AUDIO_FORMAT_PCM:
1691 id = SND_AUDIOCODEC_PCM;
1692 break;
1693 case AUDIO_FORMAT_FLAC:
1694 id = SND_AUDIOCODEC_FLAC;
1695 break;
1696 case AUDIO_FORMAT_ALAC:
1697 id = SND_AUDIOCODEC_ALAC;
1698 break;
1699 case AUDIO_FORMAT_APE:
1700 id = SND_AUDIOCODEC_APE;
1701 break;
1702 case AUDIO_FORMAT_VORBIS:
1703 id = SND_AUDIOCODEC_VORBIS;
1704 break;
1705 case AUDIO_FORMAT_WMA:
1706 id = SND_AUDIOCODEC_WMA;
1707 break;
1708 case AUDIO_FORMAT_WMA_PRO:
1709 id = SND_AUDIOCODEC_WMA_PRO;
1710 break;
Satish Babu Patakokila0c313922016-12-08 12:07:08 +05301711 case AUDIO_FORMAT_MP2:
1712 id = SND_AUDIOCODEC_MP2;
1713 break;
Satish Babu Patakokila915ecba2017-01-10 17:43:56 +05301714 case AUDIO_FORMAT_AC3:
1715 id = SND_AUDIOCODEC_AC3;
1716 break;
1717 case AUDIO_FORMAT_E_AC3:
1718 case AUDIO_FORMAT_E_AC3_JOC:
1719 id = SND_AUDIOCODEC_EAC3;
1720 break;
1721 case AUDIO_FORMAT_DTS:
1722 case AUDIO_FORMAT_DTS_HD:
1723 id = SND_AUDIOCODEC_DTS;
1724 break;
Ben Romberger1aaaf862017-04-06 17:49:46 -07001725 case AUDIO_FORMAT_DOLBY_TRUEHD:
1726 id = SND_AUDIOCODEC_TRUEHD;
1727 break;
Naresh Tanniru928f0862017-04-07 16:44:23 -07001728 case AUDIO_FORMAT_IEC61937:
1729 id = SND_AUDIOCODEC_IEC61937;
1730 break;
Preetam Singh Ranawat4277a5a2017-01-18 19:02:24 +05301731 case AUDIO_FORMAT_DSD:
1732 id = SND_AUDIOCODEC_DSD;
1733 break;
Dhanalakshmi Siddani18737932016-11-29 17:33:17 +05301734 case AUDIO_FORMAT_APTX:
1735 id = SND_AUDIOCODEC_APTX;
1736 break;
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301737 default:
1738 ALOGE("%s: Unsupported audio format :%x", __func__, format);
1739 }
1740
1741 return id;
1742}
1743
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001744void audio_extn_utils_send_audio_calibration(struct audio_device *adev,
1745 struct audio_usecase *usecase)
1746{
1747 int type = usecase->type;
1748
Dhananjay Kumar14245982017-01-16 20:21:00 +05301749 if (type == PCM_PLAYBACK && usecase->stream.out != NULL) {
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05301750 platform_send_audio_calibration(adev->platform, usecase,
Zhou Song06761dd2019-04-28 18:08:17 +08001751 usecase->stream.out->app_type_cfg.app_type);
Dhananjay Kumar14245982017-01-16 20:21:00 +05301752 } else if (type == PCM_CAPTURE && usecase->stream.in != NULL) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301753 platform_send_audio_calibration(adev->platform, usecase,
Zhou Song06761dd2019-04-28 18:08:17 +08001754 usecase->stream.in->app_type_cfg.app_type);
1755 } else if ((type == PCM_HFP_CALL) || (type == PCM_CAPTURE) ||
1756 (type == TRANSCODE_LOOPBACK_RX && usecase->stream.inout != NULL)) {
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05301757 platform_send_audio_calibration(adev->platform, usecase,
Zhou Song06761dd2019-04-28 18:08:17 +08001758 platform_get_default_app_type_v2(adev->platform, usecase->type));
Vidyakumar Athotae57f6002017-03-01 13:13:16 -08001759 } else {
1760 /* No need to send audio calibration for voice and voip call usecases */
1761 if ((type != VOICE_CALL) && (type != VOIP_CALL))
1762 ALOGW("%s: No audio calibration for usecase type = %d", __func__, type);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001763 }
1764}
1765
Ben Rombergera04fabc2014-11-14 12:16:03 -08001766// Base64 Encode and Decode
1767// Not all features supported. This must be used only with following conditions.
1768// Decode Modes: Support with and without padding
1769// CRLF not handling. So no CRLF in string to decode.
1770// Encode Modes: Supports only padding
1771int b64decode(char *inp, int ilen, uint8_t* outp)
1772{
1773 int i, j, k, ii, num;
1774 int rem, pcnt;
1775 uint32_t res=0;
1776 uint8_t getIndex[MAX_BASEINDEX_LEN];
1777 uint8_t tmp, cflag;
1778
1779 if(inp == NULL || outp == NULL || ilen <= 0) {
1780 ALOGE("[%s] received NULL pointer or zero length",__func__);
1781 return -1;
1782 }
1783
1784 memset(getIndex, MAX_BASEINDEX_LEN-1, sizeof(getIndex));
1785 for(i=0;i<BASE_TABLE_SIZE;i++) {
1786 getIndex[(uint8_t)bTable[i]] = (uint8_t)i;
1787 }
1788 getIndex[(uint8_t)'=']=0;
1789
1790 j=0;k=0;
1791 num = ilen/4;
1792 rem = ilen%4;
1793 if(rem==0)
1794 num = num-1;
1795 cflag=0;
1796 for(i=0; i<num; i++) {
1797 res=0;
1798 for(ii=0;ii<4;ii++) {
1799 res = res << 6;
1800 tmp = getIndex[(uint8_t)inp[j++]];
1801 res = res | tmp;
1802 cflag = cflag | tmp;
1803 }
1804 outp[k++] = (res >> 16)&0xFF;
1805 outp[k++] = (res >> 8)&0xFF;
1806 outp[k++] = res & 0xFF;
1807 }
1808
1809 // Handle last bytes special
1810 pcnt=0;
1811 if(rem == 0) {
1812 //With padding or full data
1813 res = 0;
1814 for(ii=0;ii<4;ii++) {
1815 if(inp[j] == '=')
1816 pcnt++;
1817 res = res << 6;
1818 tmp = getIndex[(uint8_t)inp[j++]];
1819 res = res | tmp;
1820 cflag = cflag | tmp;
1821 }
1822 outp[k++] = res >> 16;
1823 if(pcnt == 2)
1824 goto done;
1825 outp[k++] = (res>>8)&0xFF;
1826 if(pcnt == 1)
1827 goto done;
1828 outp[k++] = res&0xFF;
1829 } else {
1830 //without padding
1831 res = 0;
1832 for(i=0;i<rem;i++) {
1833 res = res << 6;
1834 tmp = getIndex[(uint8_t)inp[j++]];
1835 res = res | tmp;
1836 cflag = cflag | tmp;
1837 }
1838 for(i=rem;i<4;i++) {
1839 res = res << 6;
1840 pcnt++;
1841 }
1842 outp[k++] = res >> 16;
1843 if(pcnt == 2)
1844 goto done;
1845 outp[k++] = (res>>8)&0xFF;
1846 if(pcnt == 1)
1847 goto done;
1848 outp[k++] = res&0xFF;
1849 }
1850done:
1851 if(cflag == 0xFF) {
1852 ALOGE("[%s] base64 decode failed. Invalid character found %s",
1853 __func__, inp);
1854 return 0;
1855 }
1856 return k;
1857}
1858
1859int b64encode(uint8_t *inp, int ilen, char* outp)
1860{
1861 int i,j,k, num;
1862 int rem=0;
1863 uint32_t res=0;
1864
1865 if(inp == NULL || outp == NULL || ilen<=0) {
1866 ALOGE("[%s] received NULL pointer or zero input length",__func__);
1867 return -1;
1868 }
1869
1870 num = ilen/3;
1871 rem = ilen%3;
1872 j=0;k=0;
1873 for(i=0; i<num; i++) {
1874 //prepare index
1875 res = inp[j++]<<16;
1876 res = res | inp[j++]<<8;
1877 res = res | inp[j++];
1878 //get output map from index
1879 outp[k++] = (char) bTable[(res>>18)&0x3F];
1880 outp[k++] = (char) bTable[(res>>12)&0x3F];
1881 outp[k++] = (char) bTable[(res>>6)&0x3F];
1882 outp[k++] = (char) bTable[res&0x3F];
1883 }
1884
1885 switch(rem) {
1886 case 1:
1887 res = inp[j++]<<16;
1888 outp[k++] = (char) bTable[res>>18];
1889 outp[k++] = (char) bTable[(res>>12)&0x3F];
1890 //outp[k++] = '=';
1891 //outp[k++] = '=';
1892 break;
1893 case 2:
1894 res = inp[j++]<<16;
1895 res = res | inp[j++]<<8;
1896 outp[k++] = (char) bTable[res>>18];
1897 outp[k++] = (char) bTable[(res>>12)&0x3F];
1898 outp[k++] = (char) bTable[(res>>6)&0x3F];
1899 //outp[k++] = '=';
1900 break;
1901 default:
1902 break;
1903 }
Ben Rombergera04fabc2014-11-14 12:16:03 -08001904 outp[k] = '\0';
1905 return k;
1906}
Ashish Jain81eb2a82015-05-13 10:52:34 +05301907
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301908
1909int audio_extn_utils_get_codec_version(const char *snd_card_name,
1910 int card_num,
1911 char *codec_version)
1912{
1913 char procfs_path[50];
1914 FILE *fp;
1915
1916 if (strstr(snd_card_name, "tasha")) {
1917 snprintf(procfs_path, sizeof(procfs_path),
1918 "/proc/asound/card%d/codecs/tasha/version", card_num);
1919 if ((fp = fopen(procfs_path, "r")) != NULL) {
1920 fgets(codec_version, CODEC_VERSION_MAX_LENGTH, fp);
1921 fclose(fp);
1922 } else {
1923 ALOGE("%s: ERROR. cannot open %s", __func__, procfs_path);
1924 return -ENOENT;
1925 }
1926 ALOGD("%s: codec version %s", __func__, codec_version);
1927 }
1928
1929 return 0;
1930}
1931
Preetam Singh Ranawat9d0d8e42019-07-15 12:27:25 +05301932int audio_extn_utils_get_codec_variant(int card_num,
1933 char *codec_variant)
1934{
1935 char procfs_path[50];
1936 FILE *fp;
1937 snprintf(procfs_path, sizeof(procfs_path),
1938 "/proc/asound/card%d/codecs/wcd938x/variant", card_num);
1939 if ((fp = fopen(procfs_path, "r")) == NULL) {
1940 snprintf(procfs_path, sizeof(procfs_path),
1941 "/proc/asound/card%d/codecs/wcd937x/variant", card_num);
1942 if ((fp = fopen(procfs_path, "r")) == NULL) {
1943 ALOGE("%s: ERROR. cannot open %s", __func__, procfs_path);
1944 return -ENOENT;
1945 }
1946 }
1947 fgets(codec_variant, CODEC_VARIANT_MAX_LENGTH, fp);
1948 fclose(fp);
1949 ALOGD("%s: codec variant is %s", __func__, codec_variant);
1950 return 0;
1951}
1952
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301953
Ashish Jain81eb2a82015-05-13 10:52:34 +05301954#ifdef AUDIO_EXTERNAL_HDMI_ENABLED
1955
1956void get_default_compressed_channel_status(
1957 unsigned char *channel_status)
1958{
Ashish Jain81eb2a82015-05-13 10:52:34 +05301959 memset(channel_status,0,24);
1960
1961 /* block start bit in preamble bit 3 */
1962 channel_status[0] |= PROFESSIONAL;
1963 //compre out
1964 channel_status[0] |= NON_LPCM;
1965 // sample rate; fixed 48K for default/transcode
1966 channel_status[3] |= SR_48000;
1967}
1968
Ashish Jain81eb2a82015-05-13 10:52:34 +05301969int32_t get_compressed_channel_status(void *audio_stream_data,
1970 uint32_t audio_frame_size,
1971 unsigned char *channel_status,
1972 enum audio_parser_code_type codec_type)
1973 // codec_type - AUDIO_PARSER_CODEC_AC3
1974 // - AUDIO_PARSER_CODEC_DTS
1975{
1976 unsigned char *stream;
1977 int ret = 0;
1978 stream = (unsigned char *)audio_stream_data;
1979
1980 if (audio_stream_data == NULL || audio_frame_size == 0) {
1981 ALOGW("no buffer to get channel status, return default for compress");
1982 get_default_compressed_channel_status(channel_status);
1983 return ret;
1984 }
1985
1986 memset(channel_status,0,24);
1987 if(init_audio_parser(stream, audio_frame_size, codec_type) == -1)
1988 {
1989 ALOGE("init audio parser failed");
1990 return -1;
1991 }
1992 ret = get_channel_status(channel_status, codec_type);
1993 return ret;
1994
1995}
1996
Ashish Jain81eb2a82015-05-13 10:52:34 +05301997void get_lpcm_channel_status(uint32_t sampleRate,
1998 unsigned char *channel_status)
1999{
2000 int32_t status = 0;
Ashish Jain81eb2a82015-05-13 10:52:34 +05302001 memset(channel_status,0,24);
2002 /* block start bit in preamble bit 3 */
2003 channel_status[0] |= PROFESSIONAL;
2004 //LPCM OUT
2005 channel_status[0] &= ~NON_LPCM;
2006
2007 switch (sampleRate) {
2008 case 8000:
2009 case 11025:
2010 case 12000:
2011 case 16000:
2012 case 22050:
2013 channel_status[3] |= SR_NOTID;
Preetam Singh Ranawat61716b12015-12-14 11:55:24 +05302014 break;
Ashish Jain81eb2a82015-05-13 10:52:34 +05302015 case 24000:
2016 channel_status[3] |= SR_24000;
2017 break;
2018 case 32000:
2019 channel_status[3] |= SR_32000;
2020 break;
2021 case 44100:
2022 channel_status[3] |= SR_44100;
2023 break;
2024 case 48000:
2025 channel_status[3] |= SR_48000;
2026 break;
2027 case 88200:
2028 channel_status[3] |= SR_88200;
2029 break;
2030 case 96000:
2031 channel_status[3] |= SR_96000;
2032 break;
2033 case 176400:
2034 channel_status[3] |= SR_176400;
2035 break;
2036 case 192000:
2037 channel_status[3] |= SR_192000;
2038 break;
2039 default:
2040 ALOGV("Invalid sample_rate %u\n", sampleRate);
2041 status = -1;
2042 break;
2043 }
2044}
2045
2046void audio_utils_set_hdmi_channel_status(struct stream_out *out, char * buffer, size_t bytes)
2047{
2048 unsigned char channel_status[24]={0};
2049 struct snd_aes_iec958 iec958;
2050 const char *mixer_ctl_name = "IEC958 Playback PCM Stream";
2051 struct mixer_ctl *ctl;
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +05302052 ALOGV("%s: buffer %s bytes %zd", __func__, buffer, bytes);
Arun Mirpurie008ed22019-03-21 11:21:04 -07002053
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05302054 if (audio_extn_utils_is_dolby_format(out->format) &&
Ashish Jain81eb2a82015-05-13 10:52:34 +05302055 /*TODO:Extend code to support DTS passthrough*/
2056 /*set compressed channel status bits*/
Arun Mirpurie008ed22019-03-21 11:21:04 -07002057 audio_extn_passthru_is_passthrough_stream(out) &&
2058 audio_extn_is_hdmi_passthru_enabled()) {
Ashish Jain81eb2a82015-05-13 10:52:34 +05302059 get_compressed_channel_status(buffer, bytes, channel_status, AUDIO_PARSER_CODEC_AC3);
Arun Mirpurie008ed22019-03-21 11:21:04 -07002060 } else {
Ashish Jain81eb2a82015-05-13 10:52:34 +05302061 /*set channel status bit for LPCM*/
2062 get_lpcm_channel_status(out->sample_rate, channel_status);
2063 }
2064
2065 memcpy(iec958.status, channel_status,sizeof(iec958.status));
2066 ctl = mixer_get_ctl_by_name(out->dev->mixer, mixer_ctl_name);
2067 if (!ctl) {
2068 ALOGE("%s: Could not get ctl for mixer cmd - %s",
2069 __func__, mixer_ctl_name);
2070 return;
2071 }
2072 if (mixer_ctl_set_array(ctl, &iec958, sizeof(iec958)) < 0) {
2073 ALOGE("%s: Could not set channel status for ext HDMI ",
2074 __func__);
2075 return;
2076 }
2077
2078}
2079#endif
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302080
2081int audio_extn_utils_get_avt_device_drift(
2082 struct audio_usecase *usecase,
2083 struct audio_avt_device_drift_param *drift_param)
2084{
2085 int ret = 0, count = 0;
2086 char avt_device_drift_mixer_ctl_name[MIXER_PATH_MAX_LENGTH] = {0};
Naresh Tanniru6160c712017-04-17 15:43:48 +05302087 const char *backend = NULL;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302088 struct mixer_ctl *ctl = NULL;
2089 struct audio_avt_device_drift_stats drift_stats;
2090 struct audio_device *adev = NULL;
2091
2092 if (usecase != NULL && usecase->type == PCM_PLAYBACK) {
Naresh Tanniru6160c712017-04-17 15:43:48 +05302093 backend = platform_get_snd_device_backend_interface(usecase->out_snd_device);
2094 if (!backend) {
2095 ALOGE("%s: Unsupported device %d", __func__,
2096 usecase->stream.out->devices);
2097 ret = -EINVAL;
2098 goto done;
2099 }
2100 strlcpy(avt_device_drift_mixer_ctl_name,
2101 backend,
2102 MIXER_PATH_MAX_LENGTH);
2103
2104 count = strlen(backend);
2105 if (MIXER_PATH_MAX_LENGTH - count > 0) {
2106 strlcat(&avt_device_drift_mixer_ctl_name[count],
2107 " DRIFT",
2108 MIXER_PATH_MAX_LENGTH - count);
2109 } else {
2110 ret = -EINVAL;
2111 goto done;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302112 }
2113 } else {
Naresh Tanniru7586e292017-04-13 10:38:13 +05302114 ALOGE("%s: Invalid usecase",__func__);
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302115 ret = -EINVAL;
Naresh Tanniru6160c712017-04-17 15:43:48 +05302116 goto done;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302117 }
2118
Naresh Tanniru6160c712017-04-17 15:43:48 +05302119 adev = usecase->stream.out->dev;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302120 ctl = mixer_get_ctl_by_name(adev->mixer, avt_device_drift_mixer_ctl_name);
2121 if (!ctl) {
2122 ALOGE("%s: Could not get ctl for mixer cmd - %s",
2123 __func__, avt_device_drift_mixer_ctl_name);
2124
2125 ret = -EINVAL;
2126 goto done;
2127 }
2128
2129 ALOGV("%s: Getting AV Timer vs Device Drift mixer ctrl name %s", __func__,
2130 avt_device_drift_mixer_ctl_name);
2131
2132 mixer_ctl_update(ctl);
2133 count = mixer_ctl_get_num_values(ctl);
2134 if (count != sizeof(struct audio_avt_device_drift_stats)) {
2135 ALOGE("%s: mixer_ctl_get_num_values() invalid drift_stats data size",
2136 __func__);
2137
2138 ret = -EINVAL;
2139 goto done;
2140 }
2141
2142 ret = mixer_ctl_get_array(ctl, (void *)&drift_stats, count);
2143 if (ret != 0) {
2144 ALOGE("%s: mixer_ctl_get_array() failed to get drift_stats Params",
2145 __func__);
2146
2147 ret = -EINVAL;
2148 goto done;
2149 }
2150 memcpy(drift_param, &drift_stats.drift_param,
2151 sizeof(struct audio_avt_device_drift_param));
2152done:
2153 return ret;
2154}
Manish Dewangan07de2142017-02-27 19:27:20 +05302155
2156#ifdef SNDRV_COMPRESS_PATH_DELAY
2157int audio_extn_utils_compress_get_dsp_latency(struct stream_out *out)
2158{
2159 int ret = -EINVAL;
2160 struct snd_compr_metadata metadata;
2161 int delay_ms = COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
2162
Weiyin Jiangc49a3b52018-08-17 16:16:08 +08002163 /* override the latency for pcm offload use case */
2164 if ((out->flags & AUDIO_OUTPUT_FLAG_DIRECT) &&
2165 !(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)) {
2166 delay_ms = PCM_OFFLOAD_PLAYBACK_LATENCY;
2167 }
2168
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -07002169 if (property_get_bool("vendor.audio.playback.dsp.pathdelay", false)) {
Manish Dewangan07de2142017-02-27 19:27:20 +05302170 ALOGD("%s:: Quering DSP delay %d",__func__, __LINE__);
2171 if (!(is_offload_usecase(out->usecase))) {
2172 ALOGE("%s:: not supported for non offload session", __func__);
2173 goto exit;
2174 }
2175
2176 if (!out->compr) {
2177 ALOGD("%s:: Invalid compress handle,returning default dsp latency",
2178 __func__);
2179 goto exit;
2180 }
2181
2182 metadata.key = SNDRV_COMPRESS_PATH_DELAY;
2183 ret = compress_get_metadata(out->compr, &metadata);
2184 if(ret) {
2185 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2186 goto exit;
2187 }
2188 delay_ms = metadata.value[0] / 1000; /*convert to ms*/
2189 } else {
2190 ALOGD("%s:: Using Fix DSP delay",__func__);
2191 }
2192
2193exit:
2194 ALOGD("%s:: delay in ms is %d",__func__, delay_ms);
2195 return delay_ms;
2196}
2197#else
2198int audio_extn_utils_compress_get_dsp_latency(struct stream_out *out __unused)
2199{
2200 return COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
2201}
2202#endif
Manish Dewangan69426c82017-01-30 17:35:36 +05302203
2204#ifdef SNDRV_COMPRESS_RENDER_MODE
2205int audio_extn_utils_compress_set_render_mode(struct stream_out *out)
2206{
2207 struct snd_compr_metadata metadata;
2208 int ret = -EINVAL;
2209
2210 if (!(is_offload_usecase(out->usecase))) {
2211 ALOGE("%s:: not supported for non offload session", __func__);
2212 goto exit;
2213 }
2214
2215 if (!out->compr) {
2216 ALOGD("%s:: Invalid compress handle",
2217 __func__);
2218 goto exit;
2219 }
2220
2221 ALOGD("%s:: render mode %d", __func__, out->render_mode);
2222
2223 metadata.key = SNDRV_COMPRESS_RENDER_MODE;
2224 if (out->render_mode == RENDER_MODE_AUDIO_MASTER) {
2225 metadata.value[0] = SNDRV_COMPRESS_RENDER_MODE_AUDIO_MASTER;
2226 } else if (out->render_mode == RENDER_MODE_AUDIO_STC_MASTER) {
2227 metadata.value[0] = SNDRV_COMPRESS_RENDER_MODE_STC_MASTER;
2228 } else {
2229 ret = 0;
2230 goto exit;
2231 }
2232 ret = compress_set_metadata(out->compr, &metadata);
2233 if(ret) {
2234 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2235 }
2236exit:
2237 return ret;
2238}
2239#else
2240int audio_extn_utils_compress_set_render_mode(struct stream_out *out __unused)
2241{
2242 ALOGD("%s:: configuring render mode not supported", __func__);
2243 return 0;
2244}
2245#endif
Manish Dewangan58229382017-02-02 15:48:41 +05302246
2247#ifdef SNDRV_COMPRESS_CLK_REC_MODE
2248int audio_extn_utils_compress_set_clk_rec_mode(
2249 struct audio_usecase *usecase)
2250{
2251 struct snd_compr_metadata metadata;
2252 struct stream_out *out = NULL;
2253 int ret = -EINVAL;
2254
Naresh Tanniru7586e292017-04-13 10:38:13 +05302255 if (usecase == NULL || usecase->type != PCM_PLAYBACK) {
Manish Dewangan58229382017-02-02 15:48:41 +05302256 ALOGE("%s:: Invalid use case", __func__);
2257 goto exit;
2258 }
2259
2260 out = usecase->stream.out;
2261 if (!out) {
2262 ALOGE("%s:: invalid stream", __func__);
2263 goto exit;
2264 }
2265
2266 if (!is_offload_usecase(out->usecase)) {
2267 ALOGE("%s:: not supported for non offload session", __func__);
2268 goto exit;
2269 }
2270
2271 if (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER) {
2272 ALOGD("%s:: clk recovery is only supported in STC render mode",
2273 __func__);
2274 ret = 0;
2275 goto exit;
2276 }
2277
2278 if (!out->compr) {
2279 ALOGD("%s:: Invalid compress handle",
2280 __func__);
2281 goto exit;
2282 }
2283 metadata.key = SNDRV_COMPRESS_CLK_REC_MODE;
2284 switch(usecase->out_snd_device) {
2285 case SND_DEVICE_OUT_HDMI:
2286 case SND_DEVICE_OUT_SPEAKER_AND_HDMI:
2287 case SND_DEVICE_OUT_DISPLAY_PORT:
2288 case SND_DEVICE_OUT_SPEAKER_AND_DISPLAY_PORT:
2289 metadata.value[0] = SNDRV_COMPRESS_CLK_REC_MODE_NONE;
2290 break;
2291 default:
2292 metadata.value[0] = SNDRV_COMPRESS_CLK_REC_MODE_AUTO;
2293 break;
2294 }
2295
2296 ALOGD("%s:: clk recovery mode %d",__func__, metadata.value[0]);
2297
2298 ret = compress_set_metadata(out->compr, &metadata);
2299 if(ret) {
2300 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2301 }
2302
2303exit:
2304 return ret;
2305}
2306#else
2307int audio_extn_utils_compress_set_clk_rec_mode(
2308 struct audio_usecase *usecase __unused)
2309{
2310 ALOGD("%s:: configuring render mode not supported", __func__);
2311 return 0;
2312}
2313#endif
Manish Dewangan27346042017-03-01 12:56:12 +05302314
2315#ifdef SNDRV_COMPRESS_RENDER_WINDOW
2316int audio_extn_utils_compress_set_render_window(
2317 struct stream_out *out,
2318 struct audio_out_render_window_param *render_window)
2319{
2320 struct snd_compr_metadata metadata;
2321 int ret = -EINVAL;
2322
Manish Dewangan27346042017-03-01 12:56:12 +05302323 if(render_window == NULL) {
2324 ALOGE("%s:: Invalid render_window", __func__);
2325 goto exit;
2326 }
2327
Aniket Kumar Lata1e1d3662017-06-01 18:45:48 -07002328 ALOGD("%s:: render window start 0x%"PRIx64" end 0x%"PRIx64"",
2329 __func__,render_window->render_ws, render_window->render_we);
2330
Manish Dewangan27346042017-03-01 12:56:12 +05302331 if (!is_offload_usecase(out->usecase)) {
2332 ALOGE("%s:: not supported for non offload session", __func__);
2333 goto exit;
2334 }
2335
Naresh Tanniru6160c712017-04-17 15:43:48 +05302336 if ((out->render_mode != RENDER_MODE_AUDIO_MASTER) &&
2337 (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER)) {
Manish Dewangan27346042017-03-01 12:56:12 +05302338 ALOGD("%s:: only supported in timestamp mode, current "
2339 "render mode mode %d", __func__, out->render_mode);
2340 goto exit;
2341 }
2342
2343 if (!out->compr) {
2344 ALOGW("%s:: offload session not yet opened,"
2345 "render window will be configure later", __func__);
2346 /* store render window to reconfigure in start_output_stream() */
2347 goto exit;
2348 }
2349
2350 metadata.key = SNDRV_COMPRESS_RENDER_WINDOW;
2351 /*render window start value */
2352 metadata.value[0] = 0xFFFFFFFF & render_window->render_ws; /* lsb */
2353 metadata.value[1] = \
2354 (0xFFFFFFFF00000000 & render_window->render_ws) >> 32; /* msb*/
2355 /*render window end value */
2356 metadata.value[2] = 0xFFFFFFFF & render_window->render_we; /* lsb */
2357 metadata.value[3] = \
2358 (0xFFFFFFFF00000000 & render_window->render_we) >> 32; /* msb*/
2359
2360 ret = compress_set_metadata(out->compr, &metadata);
2361 if(ret) {
2362 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2363 }
2364
2365exit:
2366 return ret;
2367}
2368#else
2369int audio_extn_utils_compress_set_render_window(
2370 struct stream_out *out __unused,
2371 struct audio_out_render_window_param *render_window __unused)
2372{
2373 ALOGD("%s:: configuring render window not supported", __func__);
2374 return 0;
2375}
2376#endif
Manish Dewangan14956cc2017-02-14 18:54:42 +05302377
2378#ifdef SNDRV_COMPRESS_START_DELAY
2379int audio_extn_utils_compress_set_start_delay(
2380 struct stream_out *out,
2381 struct audio_out_start_delay_param *delay_param)
2382{
2383 struct snd_compr_metadata metadata;
2384 int ret = -EINVAL;
2385
2386 if(delay_param == NULL) {
2387 ALOGE("%s:: Invalid delay_param", __func__);
2388 goto exit;
2389 }
2390
2391 ALOGD("%s:: render start delay 0x%"PRIx64" ", __func__,
2392 delay_param->start_delay);
2393
2394 if (!is_offload_usecase(out->usecase)) {
2395 ALOGE("%s:: not supported for non offload session", __func__);
2396 goto exit;
2397 }
2398
Naresh Tanniru6160c712017-04-17 15:43:48 +05302399 if ((out->render_mode != RENDER_MODE_AUDIO_MASTER) &&
2400 (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER)) {
Manish Dewangan14956cc2017-02-14 18:54:42 +05302401 ALOGD("%s:: only supported in timestamp mode, current "
2402 "render mode mode %d", __func__, out->render_mode);
2403 goto exit;
2404 }
2405
2406 if (!out->compr) {
2407 ALOGW("%s:: offload session not yet opened,"
2408 "start delay will be configure later", __func__);
2409 goto exit;
2410 }
2411
2412 metadata.key = SNDRV_COMPRESS_START_DELAY;
2413 metadata.value[0] = 0xFFFFFFFF & delay_param->start_delay; /* lsb */
2414 metadata.value[1] = \
2415 (0xFFFFFFFF00000000 & delay_param->start_delay) >> 32; /* msb*/
2416
2417 ret = compress_set_metadata(out->compr, &metadata);
2418 if(ret) {
2419 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2420 }
2421
2422exit:
2423 return ret;
2424}
2425#else
2426int audio_extn_utils_compress_set_start_delay(
2427 struct stream_out *out __unused,
2428 struct audio_out_start_delay_param *delay_param __unused)
2429{
2430 ALOGD("%s:: configuring render window not supported", __func__);
2431 return 0;
2432}
2433#endif
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002434
Surendar Karka287348c2019-04-10 18:31:46 +05302435#ifdef SNDRV_COMPRESS_DSP_POSITION
2436int audio_extn_utils_compress_get_dsp_presentation_pos(struct stream_out *out,
2437 uint64_t *frames, struct timespec *timestamp, int32_t clock_id)
2438{
2439 int ret = -EINVAL;
2440 uint64_t *val = NULL;
2441 uint64_t time = 0;
2442 struct snd_compr_metadata metadata;
2443
2444 ALOGV("%s:: Quering DSP position with clock id %d",__func__, clock_id);
2445 metadata.key = SNDRV_COMPRESS_DSP_POSITION;
2446 metadata.value[0] = clock_id;
2447 ret = compress_get_metadata(out->compr, &metadata);
2448 if (ret) {
2449 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2450 ret = -errno;
2451 goto exit;
2452 }
2453 val = (uint64_t *)&metadata.value[1];
2454 *frames = *val;
2455 time = *(val + 1);
2456 timestamp->tv_sec = time / 1000000;
2457 timestamp->tv_nsec = (time % 1000000)*1000;
2458
2459exit:
2460 return ret;
2461}
2462#else
2463int audio_extn_utils_compress_get_dsp_presentation_pos(struct stream_out *out __unused,
2464 uint64_t *frames __unused, struct timespec *timestamp __unused,
2465 int32_t clock_id __unused)
2466{
2467 ALOGD("%s:: dsp presentation position not supported", __func__);
2468 return 0;
2469
2470}
2471#endif
2472
2473#ifdef SNDRV_PCM_IOCTL_DSP_POSITION
2474int audio_extn_utils_pcm_get_dsp_presentation_pos(struct stream_out *out,
2475 uint64_t *frames, struct timespec *timestamp, int32_t clock_id)
2476{
2477 int ret = -EINVAL;
2478 uint64_t time = 0;
2479 struct snd_pcm_prsnt_position prsnt_position;
2480
2481 ALOGV("%s:: Quering DSP position with clock id %d",__func__, clock_id);
2482 prsnt_position.clock_id = clock_id;
2483 ret = pcm_ioctl(out->pcm, SNDRV_PCM_IOCTL_DSP_POSITION, &prsnt_position);
2484 if (ret) {
2485 ALOGE("%s::error %d", __func__, ret);
2486 ret = -EIO;
2487 goto exit;
2488 }
2489
2490 *frames = prsnt_position.frames;
2491 time = prsnt_position.timestamp;
2492 timestamp->tv_sec = time / 1000000;
2493 timestamp->tv_nsec = (time % 1000000)*1000;
2494
2495exit:
2496 return ret;
2497}
2498#else
2499int audio_extn_utils_pcm_get_dsp_presentation_pos(struct stream_out *out __unused,
2500 uint64_t *frames __unused, struct timespec *timestamp __unused,
2501 int32_t clock_id __unused)
2502{
2503 ALOGD("%s:: dsp presentation position not supported", __func__);
2504 return 0;
2505
2506}
2507#endif
2508
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002509#define MAX_SND_CARD 8
Ashish Jain30ae8942017-11-05 17:21:23 +05302510#define RETRY_US 1000000
2511#define RETRY_NUMBER 40
Aalique Grahame22e49102018-12-18 14:23:57 -08002512#define PLATFORM_INFO_XML_PATH "audio_platform_info.xml"
2513#define PLATFORM_INFO_XML_BASE_STRING "audio_platform_info"
2514
2515#ifdef LINUX_ENABLED
2516static const char *kConfigLocationList[] =
2517 {"/etc"};
2518#else
2519static const char *kConfigLocationList[] =
2520 {"/vendor/etc"};
2521#endif
2522static const int kConfigLocationListSize =
2523 (sizeof(kConfigLocationList) / sizeof(kConfigLocationList[0]));
2524
2525bool audio_extn_utils_resolve_config_file(char file_name[MIXER_PATH_MAX_LENGTH])
2526{
2527 char full_config_path[MIXER_PATH_MAX_LENGTH];
2528 for (int i = 0; i < kConfigLocationListSize; i++) {
2529 snprintf(full_config_path,
2530 MIXER_PATH_MAX_LENGTH,
2531 "%s/%s",
2532 kConfigLocationList[i],
2533 file_name);
2534 if (F_OK == access(full_config_path, 0)) {
Weiyin Jiang2995f662019-04-17 14:25:12 +08002535 strlcpy(file_name, full_config_path, MIXER_PATH_MAX_LENGTH);
Aalique Grahame22e49102018-12-18 14:23:57 -08002536 return true;
2537 }
2538 }
2539 return false;
2540}
2541
2542/* platform_info_file should be size 'MIXER_PATH_MAX_LENGTH' */
2543int audio_extn_utils_get_platform_info(const char* snd_card_name, char* platform_info_file)
2544{
2545 if (NULL == snd_card_name) {
2546 return -1;
2547 }
2548
2549 struct snd_card_split *snd_split_handle = NULL;
2550 int ret = 0;
2551 audio_extn_set_snd_card_split(snd_card_name);
2552 snd_split_handle = audio_extn_get_snd_card_split();
2553
2554 snprintf(platform_info_file, MIXER_PATH_MAX_LENGTH, "%s_%s_%s.xml",
2555 PLATFORM_INFO_XML_BASE_STRING, snd_split_handle->snd_card,
2556 snd_split_handle->form_factor);
2557
2558 if (!audio_extn_utils_resolve_config_file(platform_info_file)) {
2559 memset(platform_info_file, 0, MIXER_PATH_MAX_LENGTH);
2560 snprintf(platform_info_file, MIXER_PATH_MAX_LENGTH, "%s_%s.xml",
2561 PLATFORM_INFO_XML_BASE_STRING, snd_split_handle->snd_card);
2562
2563 if (!audio_extn_utils_resolve_config_file(platform_info_file)) {
2564 memset(platform_info_file, 0, MIXER_PATH_MAX_LENGTH);
2565 strlcpy(platform_info_file, PLATFORM_INFO_XML_PATH, MIXER_PATH_MAX_LENGTH);
2566 ret = audio_extn_utils_resolve_config_file(platform_info_file) ? 0 : -1;
2567 }
2568 }
2569
2570 return ret;
2571}
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002572
2573int audio_extn_utils_get_snd_card_num()
2574{
Soumya Managoli9fee7c62018-04-06 16:21:50 +05302575 int snd_card_num = 0;
2576 struct mixer *mixer = NULL;
2577
2578 snd_card_num = audio_extn_utils_open_snd_mixer(&mixer);
2579 if (mixer)
2580 mixer_close(mixer);
2581 return snd_card_num;
2582}
2583
2584int audio_extn_utils_open_snd_mixer(struct mixer **mixer_handle)
2585{
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002586
2587 void *hw_info = NULL;
2588 struct mixer *mixer = NULL;
2589 int retry_num = 0;
mpangfaa7bae2019-01-15 16:38:13 +08002590 int snd_card_num = 0;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002591 char* snd_card_name = NULL;
mpangfaa7bae2019-01-15 16:38:13 +08002592 int snd_card_detection_info[MAX_SND_CARD] = {0};
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002593
Soumya Managoli9fee7c62018-04-06 16:21:50 +05302594 if (!mixer_handle) {
2595 ALOGE("invalid mixer handle");
2596 return -1;
2597 }
2598 *mixer_handle = NULL;
Ashish Jain30ae8942017-11-05 17:21:23 +05302599 /*
mpangfaa7bae2019-01-15 16:38:13 +08002600 * Try with all the sound cards ( 0 to 7 ) and if none of them were detected
Ashish Jain30ae8942017-11-05 17:21:23 +05302601 * sleep for 1 sec and try detections with sound card 0 again.
2602 * If sound card gets detected, check if it is relevant, if not check with the
2603 * other sound cards. To ensure that the irrelevant sound card is not check again,
2604 * we maintain it in min_snd_card_num.
2605 */
2606 while (retry_num < RETRY_NUMBER) {
mpangfaa7bae2019-01-15 16:38:13 +08002607 snd_card_num = 0;
2608 while (snd_card_num < MAX_SND_CARD) {
2609 if (snd_card_detection_info[snd_card_num] == 0) {
2610 mixer = mixer_open(snd_card_num);
2611 if (!mixer)
2612 snd_card_num++;
2613 else
2614 break;
2615 } else
2616 snd_card_num++;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002617 }
2618
2619 if (!mixer) {
Ashish Jain30ae8942017-11-05 17:21:23 +05302620 usleep(RETRY_US);
Ashish Jain30ae8942017-11-05 17:21:23 +05302621 retry_num++;
mpangfaa7bae2019-01-15 16:38:13 +08002622 ALOGD("%s: retry, retry_num %d", __func__, retry_num);
Ashish Jain30ae8942017-11-05 17:21:23 +05302623 continue;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002624 }
2625
2626 snd_card_name = strdup(mixer_get_name(mixer));
2627 if (!snd_card_name) {
2628 ALOGE("failed to allocate memory for snd_card_name\n");
2629 mixer_close(mixer);
2630 return -1;
2631 }
2632 ALOGD("%s: snd_card_name: %s", __func__, snd_card_name);
mpangfaa7bae2019-01-15 16:38:13 +08002633 snd_card_detection_info[snd_card_num] = 1;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002634 hw_info = hw_info_init(snd_card_name);
2635 if (hw_info) {
2636 ALOGD("%s: Opened sound card:%d", __func__, snd_card_num);
2637 break;
2638 }
mpangfaa7bae2019-01-15 16:38:13 +08002639 ALOGE("%s: Failed to init hardware info, snd_card_num:%d", __func__, snd_card_num);
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002640
Dhananjay Kumara7e27832017-07-11 15:40:39 +05302641 free(snd_card_name);
2642 snd_card_name = NULL;
2643
2644 mixer_close(mixer);
2645 mixer = NULL;
2646 }
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002647 if (snd_card_name)
2648 free(snd_card_name);
Ashish Jain30ae8942017-11-05 17:21:23 +05302649
Dhananjay Kumara7e27832017-07-11 15:40:39 +05302650 if (hw_info)
2651 hw_info_deinit(hw_info);
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002652
mpangfaa7bae2019-01-15 16:38:13 +08002653 if (retry_num >= RETRY_NUMBER) {
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002654 ALOGE("%s: Unable to find correct sound card, aborting.", __func__);
2655 return -1;
2656 }
2657
Soumya Managoli9fee7c62018-04-06 16:21:50 +05302658 if (mixer)
2659 *mixer_handle = mixer;
2660
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002661 return snd_card_num;
2662}
Naresh Tanniru6160c712017-04-17 15:43:48 +05302663
Soumya Managoli9fee7c62018-04-06 16:21:50 +05302664void audio_extn_utils_close_snd_mixer(struct mixer *mixer)
2665{
2666 if (mixer)
2667 mixer_close(mixer);
2668}
2669
Naresh Tanniru6160c712017-04-17 15:43:48 +05302670#ifdef SNDRV_COMPRESS_ENABLE_ADJUST_SESSION_CLOCK
2671int audio_extn_utils_compress_enable_drift_correction(
2672 struct stream_out *out,
2673 struct audio_out_enable_drift_correction *drift)
2674{
2675 struct snd_compr_metadata metadata;
2676 int ret = -EINVAL;
2677
2678 if(drift == NULL) {
2679 ALOGE("%s:: Invalid param", __func__);
2680 goto exit;
2681 }
2682
2683 ALOGD("%s:: drift enable %d", __func__,drift->enable);
2684
2685 if (!is_offload_usecase(out->usecase)) {
2686 ALOGE("%s:: not supported for non offload session", __func__);
2687 goto exit;
2688 }
2689
2690 if (!out->compr) {
2691 ALOGW("%s:: offload session not yet opened,"
2692 "start delay will be configure later", __func__);
2693 goto exit;
2694 }
2695
2696 metadata.key = SNDRV_COMPRESS_ENABLE_ADJUST_SESSION_CLOCK;
2697 metadata.value[0] = drift->enable;
2698 out->drift_correction_enabled = drift->enable;
2699
2700 ret = compress_set_metadata(out->compr, &metadata);
2701 if(ret) {
2702 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2703 out->drift_correction_enabled = false;
2704 }
2705
2706exit:
2707 return ret;
2708}
2709#else
2710int audio_extn_utils_compress_enable_drift_correction(
2711 struct stream_out *out __unused,
2712 struct audio_out_enable_drift_correction *drift __unused)
2713{
2714 ALOGD("%s:: configuring drift enablement not supported", __func__);
2715 return 0;
2716}
2717#endif
2718
2719#ifdef SNDRV_COMPRESS_ADJUST_SESSION_CLOCK
2720int audio_extn_utils_compress_correct_drift(
2721 struct stream_out *out,
2722 struct audio_out_correct_drift *drift_param)
2723{
2724 struct snd_compr_metadata metadata;
2725 int ret = -EINVAL;
2726
2727 if (drift_param == NULL) {
2728 ALOGE("%s:: Invalid drift_param", __func__);
2729 goto exit;
2730 }
2731
2732 ALOGD("%s:: adjust time 0x%"PRIx64" ", __func__,
2733 drift_param->adjust_time);
2734
2735 if (!is_offload_usecase(out->usecase)) {
2736 ALOGE("%s:: not supported for non offload session", __func__);
2737 goto exit;
2738 }
2739
2740 if (!out->compr) {
2741 ALOGW("%s:: offload session not yet opened", __func__);
2742 goto exit;
2743 }
2744
2745 if (!out->drift_correction_enabled) {
2746 ALOGE("%s:: drift correction not enabled", __func__);
2747 goto exit;
2748 }
2749
2750 metadata.key = SNDRV_COMPRESS_ADJUST_SESSION_CLOCK;
2751 metadata.value[0] = 0xFFFFFFFF & drift_param->adjust_time; /* lsb */
2752 metadata.value[1] = \
2753 (0xFFFFFFFF00000000 & drift_param->adjust_time) >> 32; /* msb*/
2754
2755 ret = compress_set_metadata(out->compr, &metadata);
2756 if(ret)
2757 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2758exit:
2759 return ret;
2760}
2761#else
2762int audio_extn_utils_compress_correct_drift(
2763 struct stream_out *out __unused,
2764 struct audio_out_correct_drift *drift_param __unused)
2765{
2766 ALOGD("%s:: setting adjust clock not supported", __func__);
2767 return 0;
2768}
2769#endif
Naresh Tanniru29bce4e2017-04-27 17:54:30 +05302770
2771int audio_extn_utils_set_channel_map(
2772 struct stream_out *out,
2773 struct audio_out_channel_map_param *channel_map_param)
2774{
2775 int ret = -EINVAL, i = 0;
2776 int channels = audio_channel_count_from_out_mask(out->channel_mask);
2777
2778 if (channel_map_param == NULL) {
2779 ALOGE("%s:: Invalid channel_map", __func__);
2780 goto exit;
2781 }
2782
2783 if (channel_map_param->channels != channels) {
2784 ALOGE("%s:: Channels(%d) does not match stream channels(%d)",
2785 __func__, channel_map_param->channels, channels);
2786 goto exit;
2787 }
2788
2789 for ( i = 0; i < channels; i++) {
2790 ALOGV("%s:: channel_map[%d]- %d", __func__, i, channel_map_param->channel_map[i]);
2791 out->channel_map_param.channel_map[i] = channel_map_param->channel_map[i];
2792 }
2793 ret = 0;
2794exit:
2795 return ret;
2796}
Varun Balaraje49253e2017-07-06 19:48:56 +05302797
2798int audio_extn_utils_set_pan_scale_params(
2799 struct stream_out *out,
2800 struct mix_matrix_params *mm_params)
2801{
2802 int ret = -EINVAL, i = 0, j = 0;
2803
Varun Balaraj003ee752017-08-07 17:54:55 +05302804 if (mm_params == NULL || out == NULL) {
2805 ALOGE("%s:: Invalid mix matrix or out param", __func__);
Varun Balaraje49253e2017-07-06 19:48:56 +05302806 goto exit;
2807 }
2808
2809 if (mm_params->num_output_channels > MAX_CHANNELS_SUPPORTED ||
2810 mm_params->num_output_channels <= 0 ||
2811 mm_params->num_input_channels > MAX_CHANNELS_SUPPORTED ||
2812 mm_params->num_input_channels <= 0)
2813 goto exit;
2814
2815 out->pan_scale_params.num_output_channels = mm_params->num_output_channels;
2816 out->pan_scale_params.num_input_channels = mm_params->num_input_channels;
2817 out->pan_scale_params.has_output_channel_map =
2818 mm_params->has_output_channel_map;
2819 for (i = 0; i < mm_params->num_output_channels; i++)
2820 out->pan_scale_params.output_channel_map[i] =
2821 mm_params->output_channel_map[i];
2822
2823 out->pan_scale_params.has_input_channel_map =
2824 mm_params->has_input_channel_map;
2825 for (i = 0; i < mm_params->num_input_channels; i++)
2826 out->pan_scale_params.input_channel_map[i] =
2827 mm_params->input_channel_map[i];
2828
2829 out->pan_scale_params.has_mixer_coeffs = mm_params->has_mixer_coeffs;
2830 for (i = 0; i < mm_params->num_output_channels; i++)
2831 for (j = 0; j < mm_params->num_input_channels; j++) {
2832 //Convert the channel coefficient gains in Q14 format
2833 out->pan_scale_params.mixer_coeffs[i][j] =
2834 mm_params->mixer_coeffs[i][j] * (2 << 13);
2835 }
2836
2837 ret = platform_set_stream_pan_scale_params(out->dev->platform,
2838 out->pcm_device_id,
2839 out->pan_scale_params);
2840
2841exit:
2842 return ret;
2843}
2844
2845int audio_extn_utils_set_downmix_params(
2846 struct stream_out *out,
2847 struct mix_matrix_params *mm_params)
2848{
2849 int ret = -EINVAL, i = 0, j = 0;
2850 struct audio_usecase *usecase = NULL;
2851
Aniket Kumar Lataf9f246e2017-09-15 15:20:16 -07002852 if (mm_params == NULL || out == NULL) {
Varun Balaraj003ee752017-08-07 17:54:55 +05302853 ALOGE("%s:: Invalid mix matrix or out param", __func__);
Varun Balaraje49253e2017-07-06 19:48:56 +05302854 goto exit;
2855 }
2856
2857 if (mm_params->num_output_channels > MAX_CHANNELS_SUPPORTED ||
2858 mm_params->num_output_channels <= 0 ||
2859 mm_params->num_input_channels > MAX_CHANNELS_SUPPORTED ||
2860 mm_params->num_input_channels <= 0)
2861 goto exit;
2862
2863 usecase = get_usecase_from_list(out->dev, out->usecase);
Varun Balaraj003ee752017-08-07 17:54:55 +05302864 if (!usecase) {
2865 ALOGE("%s: Get usecase list failed!", __func__);
Aniket Kumar Lataf9f246e2017-09-15 15:20:16 -07002866 goto exit;
2867 }
Varun Balaraje49253e2017-07-06 19:48:56 +05302868 out->downmix_params.num_output_channels = mm_params->num_output_channels;
2869 out->downmix_params.num_input_channels = mm_params->num_input_channels;
2870
2871 out->downmix_params.has_output_channel_map =
2872 mm_params->has_output_channel_map;
2873 for (i = 0; i < mm_params->num_output_channels; i++) {
2874 out->downmix_params.output_channel_map[i] =
2875 mm_params->output_channel_map[i];
2876 }
2877
2878 out->downmix_params.has_input_channel_map =
2879 mm_params->has_input_channel_map;
2880 for (i = 0; i < mm_params->num_input_channels; i++)
2881 out->downmix_params.input_channel_map[i] =
2882 mm_params->input_channel_map[i];
2883
2884 out->downmix_params.has_mixer_coeffs = mm_params->has_mixer_coeffs;
2885 for (i = 0; i < mm_params->num_output_channels; i++)
Nikhil Latukar2e6f6242017-08-15 13:37:07 +05302886 for (j = 0; j < mm_params->num_input_channels; j++) {
2887 //Convert the channel coefficient gains in Q14 format
Varun Balaraje49253e2017-07-06 19:48:56 +05302888 out->downmix_params.mixer_coeffs[i][j] =
Nikhil Latukar2e6f6242017-08-15 13:37:07 +05302889 mm_params->mixer_coeffs[i][j] * (2 << 13);
2890 }
Varun Balaraje49253e2017-07-06 19:48:56 +05302891
2892 ret = platform_set_stream_downmix_params(out->dev->platform,
2893 out->pcm_device_id,
2894 usecase->out_snd_device,
2895 out->downmix_params);
2896
2897exit:
2898 return ret;
2899}
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05302900
2901bool audio_extn_utils_is_dolby_format(audio_format_t format)
2902{
2903 if (format == AUDIO_FORMAT_AC3 ||
2904 format == AUDIO_FORMAT_E_AC3 ||
2905 format == AUDIO_FORMAT_E_AC3_JOC)
2906 return true;
2907 else
2908 return false;
2909}
2910
`Deeraj Soman676c2702017-09-18 19:25:53 +05302911int audio_extn_utils_get_bit_width_from_string(const char *id_string)
2912{
2913 int i;
2914 const mixer_config_lookup mixer_bitwidth_config[] = {{"S24_3LE", 24},
2915 {"S32_LE", 32},
2916 {"S24_LE", 24},
2917 {"S16_LE", 16}};
2918 int num_configs = sizeof(mixer_bitwidth_config) / sizeof(mixer_bitwidth_config[0]);
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05302919
`Deeraj Soman676c2702017-09-18 19:25:53 +05302920 for (i = 0; i < num_configs; i++) {
2921 if (!strcmp(id_string, mixer_bitwidth_config[i].id_string))
2922 return mixer_bitwidth_config[i].value;
2923 }
2924
2925 return -EINVAL;
2926}
2927
2928int audio_extn_utils_get_sample_rate_from_string(const char *id_string)
2929{
2930 int i;
2931 const mixer_config_lookup mixer_samplerate_config[] = {{"KHZ_32", 32000},
2932 {"KHZ_48", 48000},
2933 {"KHZ_96", 96000},
2934 {"KHZ_144", 144000},
2935 {"KHZ_192", 192000},
2936 {"KHZ_384", 384000},
2937 {"KHZ_44P1", 44100},
2938 {"KHZ_88P2", 88200},
2939 {"KHZ_176P4", 176400},
2940 {"KHZ_352P8", 352800}};
2941 int num_configs = sizeof(mixer_samplerate_config) / sizeof(mixer_samplerate_config[0]);
2942
2943 for (i = 0; i < num_configs; i++) {
2944 if (!strcmp(id_string, mixer_samplerate_config[i].id_string))
2945 return mixer_samplerate_config[i].value;
2946 }
2947
2948 return -EINVAL;
2949}
2950
2951int audio_extn_utils_get_channels_from_string(const char *id_string)
2952{
2953 int i;
2954 const mixer_config_lookup mixer_channels_config[] = {{"One", 1},
2955 {"Two", 2},
2956 {"Three",3},
2957 {"Four", 4},
2958 {"Five", 5},
2959 {"Six", 6},
2960 {"Seven", 7},
2961 {"Eight", 8}};
2962 int num_configs = sizeof(mixer_channels_config) / sizeof(mixer_channels_config[0]);
2963
2964 for (i = 0; i < num_configs; i++) {
2965 if (!strcmp(id_string, mixer_channels_config[i].id_string))
2966 return mixer_channels_config[i].value;
2967 }
2968
2969 return -EINVAL;
2970}
Surendar karka30569792018-05-08 12:02:21 +05302971
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08002972void audio_extn_utils_release_snd_device(snd_device_t snd_device)
2973{
2974 audio_extn_dev_arbi_release(snd_device);
2975 audio_extn_sound_trigger_update_device_status(snd_device,
2976 ST_EVENT_SND_DEVICE_FREE);
2977 audio_extn_listen_update_device_status(snd_device,
2978 LISTEN_EVENT_SND_DEVICE_FREE);
2979}
2980
Surendar karka30569792018-05-08 12:02:21 +05302981int audio_extn_utils_get_license_params
2982(
2983const struct audio_device *adev,
2984struct audio_license_params *license_params
2985)
2986{
2987 if(!license_params)
2988 return -EINVAL;
2989 return platform_get_license_by_product(adev->platform, (const char*)license_params->product, &license_params->key, license_params->license);
2990}
2991
Aalique Grahame22e49102018-12-18 14:23:57 -08002992int audio_extn_utils_send_app_type_gain(struct audio_device *adev,
2993 int app_type,
2994 int *gain)
2995{
2996 int gain_cfg[4];
2997 const char *mixer_ctl_name = "App Type Gain";
2998 struct mixer_ctl *ctl;
2999 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
3000 if (!ctl) {
3001 ALOGE("%s: Could not get volume ctl mixer %s", __func__,
3002 mixer_ctl_name);
3003 return -EINVAL;
3004 }
3005 gain_cfg[0] = 0;
3006 gain_cfg[1] = app_type;
3007 gain_cfg[2] = gain[0];
3008 gain_cfg[3] = gain[1];
3009 ALOGV("%s app_type %d l(%d) r(%d)", __func__, app_type, gain[0], gain[1]);
3010 return mixer_ctl_set_array(ctl, gain_cfg,
3011 sizeof(gain_cfg)/sizeof(gain_cfg[0]));
3012}
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -07003013
3014int audio_extn_utils_is_vendor_enhanced_fwk()
3015{
3016 static int is_running_with_enhanced_fwk = -EINVAL;
3017
3018 if (is_running_with_enhanced_fwk == -EINVAL) {
3019 vndk_fwk_lib_handle = dlopen(VNDK_FWK_LIB_PATH, RTLD_NOW);
3020 if (vndk_fwk_lib_handle != NULL) {
3021 vndk_fwk_isVendorEnhancedFwk = (vndk_fwk_isVendorEnhancedFwk_t)
3022 dlsym(vndk_fwk_lib_handle, "isRunningWithVendorEnhancedFramework");
3023 if (vndk_fwk_isVendorEnhancedFwk == NULL) {
3024 ALOGW("%s: dlsym failed, defaulting to enhanced_fwk configuration",
3025 __func__);
3026 is_running_with_enhanced_fwk = 1;
3027 } else {
3028 is_running_with_enhanced_fwk = vndk_fwk_isVendorEnhancedFwk();
3029 }
3030 dlclose(vndk_fwk_lib_handle);
3031 vndk_fwk_lib_handle = NULL;
3032 } else {
3033 ALOGW("%s: VNDK_FWK_LIB not found, setting stock configuration", __func__);
3034 is_running_with_enhanced_fwk = 0;
3035 }
3036 ALOGV("%s: vndk_fwk_isVendorEnhancedFwk=%d", __func__, is_running_with_enhanced_fwk);
3037 }
3038
3039 return is_running_with_enhanced_fwk;
3040}
Deeraj Soman14230922019-01-30 16:39:30 +05303041
Deeraj Somanfa377bf2019-02-06 12:57:59 +05303042int audio_extn_utils_get_perf_mode_flag(void)
3043{
3044#ifdef COMPRESSED_PERF_MODE_FLAG
3045 return COMPRESSED_PERF_MODE_FLAG;
3046#else
3047 return 0;
3048#endif
3049}
3050
Deeraj Soman14230922019-01-30 16:39:30 +05303051size_t audio_extn_utils_get_input_buffer_size(uint32_t sample_rate,
3052 audio_format_t format,
3053 int channel_count,
3054 int64_t duration_ms,
3055 bool is_low_latency)
3056{
3057 size_t size = 0;
3058 size_t capture_duration = AUDIO_CAPTURE_PERIOD_DURATION_MSEC;
3059 uint32_t bytes_per_period_sample = 0;
3060
3061
3062 if (audio_extn_utils_check_input_parameters(sample_rate, format, channel_count) != 0)
3063 return 0;
3064
3065 if (duration_ms >= MIN_OFFLOAD_BUFFER_DURATION_MS && duration_ms <= MAX_OFFLOAD_BUFFER_DURATION_MS)
3066 capture_duration = duration_ms;
3067
3068 size = (sample_rate * capture_duration) / 1000;
3069 if (is_low_latency)
3070 size = LOW_LATENCY_CAPTURE_PERIOD_SIZE;
3071
3072
3073 bytes_per_period_sample = audio_bytes_per_sample(format) * channel_count;
3074 size *= bytes_per_period_sample;
3075
3076 /* make sure the size is multiple of 32 bytes and additionally multiple of
3077 * the frame_size (required for 24bit samples and non-power-of-2 channel counts)
3078 * At 48 kHz mono 16-bit PCM:
3079 * 5.000 ms = 240 frames = 15*16*1*2 = 480, a whole multiple of 32 (15)
3080 * 3.333 ms = 160 frames = 10*16*1*2 = 320, a whole multiple of 32 (10)
3081 *
3082 * The loop reaches result within 32 iterations, as initial size is
3083 * already a multiple of frame_size
3084 */
3085 size = audio_extn_utils_nearest_multiple(size, audio_extn_utils_lcm(32, bytes_per_period_sample));
3086
3087 return size;
3088}