blob: 87f3dd0f2680c7a77af191a1c10bb44d195a01b5 [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) {
Sujin Panickerb6d76262019-09-09 10:55:38 +05301170 if ((property_get("vendor.audio.spkr_prot.tx.sampling_rate", value, NULL) > 0))
1171 sample_rate = atoi(value);
1172 else
1173 sample_rate = SAMPLE_RATE_8000;
Zhou Song06761dd2019-04-28 18:08:17 +08001174 }
1175 } else if (usecase->type == TRANSCODE_LOOPBACK_RX) {
1176 sample_rate = usecase->stream.inout->out_config.sample_rate;
1177 }
1178 return sample_rate;
1179}
1180
Siena Richard7c2db772016-12-21 11:32:34 -08001181static int send_app_type_cfg_for_device(struct audio_device *adev,
1182 struct audio_usecase *usecase,
1183 int split_snd_device)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001184{
1185 char mixer_ctl_name[MAX_LENGTH_MIXER_CONTROL_IN_INT];
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301186 size_t app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT] = {0};
1187 int len = 0, rc;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001188 struct mixer_ctl *ctl;
Siena Richard7c2db772016-12-21 11:32:34 -08001189 int pcm_device_id = 0, acdb_dev_id, app_type;
1190 int snd_device = split_snd_device, snd_device_be_idx = -1;
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +05301191 int32_t sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Varun Balaraje49253e2017-07-06 19:48:56 +05301192 struct streams_io_cfg *s_info = NULL;
1193 struct listnode *node = NULL;
Nikhil Laturkarac4a7492017-08-31 18:27:19 +05301194 int bd_app_type = 0;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001195
Siena Richard7c2db772016-12-21 11:32:34 -08001196 ALOGV("%s: usecase->out_snd_device %s, usecase->in_snd_device %s, split_snd_device %s",
1197 __func__, platform_get_snd_device_name(usecase->out_snd_device),
1198 platform_get_snd_device_name(usecase->in_snd_device),
1199 platform_get_snd_device_name(split_snd_device));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001200
Siddartha Shaik343abc62017-08-08 11:15:25 +05301201 if (usecase->type != PCM_PLAYBACK && usecase->type != PCM_CAPTURE &&
Surendar Karka93cd25a2018-08-28 14:21:37 +05301202 usecase->type != TRANSCODE_LOOPBACK_RX) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301203 ALOGE("%s: not a playback/capture path, no need to cfg app type", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001204 rc = 0;
1205 goto exit_send_app_type_cfg;
1206 }
1207 if ((usecase->id != USECASE_AUDIO_PLAYBACK_DEEP_BUFFER) &&
Vignesh Kulothungan0a2eff02019-07-11 16:30:13 -07001208 (usecase->id != USECASE_AUDIO_PLAYBACK_WITH_HAPTICS) &&
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001209 (usecase->id != USECASE_AUDIO_PLAYBACK_LOW_LATENCY) &&
1210 (usecase->id != USECASE_AUDIO_PLAYBACK_MULTI_CH) &&
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -08001211 (usecase->id != USECASE_AUDIO_PLAYBACK_ULL) &&
Vikram Panduranga93f080e2017-06-07 18:16:14 -07001212 (usecase->id != USECASE_AUDIO_PLAYBACK_VOIP) &&
Surendar Karka93cd25a2018-08-28 14:21:37 +05301213 (usecase->id != USECASE_AUDIO_TRANSCODE_LOOPBACK_RX) &&
Varun Balaraje49253e2017-07-06 19:48:56 +05301214 (!is_interactive_usecase(usecase->id)) &&
Srikanth Uyyala9d551402015-08-25 16:03:42 +05301215 (!is_offload_usecase(usecase->id)) &&
Derek Chenf6318be2017-06-12 17:16:24 -04001216 (usecase->type != PCM_CAPTURE) &&
1217 (!audio_extn_auto_hal_is_bus_device_usecase(usecase->id))) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301218 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 -07001219 rc = 0;
1220 goto exit_send_app_type_cfg;
1221 }
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -08001222
1223 //if VR is active then only send the mixer control
1224 if (usecase->id == USECASE_AUDIO_PLAYBACK_ULL && !audio_is_vr_mode_on(adev)) {
1225 ALOGI("ULL doesnt need sending app type cfg, returning");
1226 rc = 0;
1227 goto exit_send_app_type_cfg;
1228 }
1229
Surendar Karka93cd25a2018-08-28 14:21:37 +05301230 if (usecase->type == PCM_PLAYBACK || usecase->type == TRANSCODE_LOOPBACK_RX) {
Ben Romberger1fafdde2015-09-09 19:43:15 -07001231 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_PLAYBACK);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301232 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
1233 "Audio Stream %d App Type Cfg", pcm_device_id);
Ben Romberger1fafdde2015-09-09 19:43:15 -07001234 } else if (usecase->type == PCM_CAPTURE) {
Ben Romberger1fafdde2015-09-09 19:43:15 -07001235 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_CAPTURE);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301236 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
1237 "Audio Stream Capture %d App Type Cfg", pcm_device_id);
Srikanth Uyyala9d551402015-08-25 16:03:42 +05301238 }
Siena Richard7c2db772016-12-21 11:32:34 -08001239
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001240 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
1241 if (!ctl) {
1242 ALOGE("%s: Could not get ctl for mixer cmd - %s", __func__,
1243 mixer_ctl_name);
1244 rc = -EINVAL;
1245 goto exit_send_app_type_cfg;
1246 }
Aditya Bavanari701a6992017-03-30 19:17:16 +05301247 snd_device = platform_get_spkr_prot_snd_device(snd_device);
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301248 if (voice_is_in_call_rec_stream(usecase->stream.in) && usecase->type == PCM_CAPTURE) {
1249 snd_device_t voice_device = voice_get_incall_rec_snd_device(usecase->in_snd_device);
1250 acdb_dev_id = platform_get_snd_device_acdb_id(voice_device);
1251 ALOGV("acdb id for voice call use case %d", acdb_dev_id);
1252 } else {
1253 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
1254 }
Rohit Kumar1181b292017-01-31 18:07:17 +05301255 if (acdb_dev_id <= 0) {
1256 ALOGE("%s: Couldn't get the acdb dev id", __func__);
1257 rc = -EINVAL;
1258 goto exit_send_app_type_cfg;
1259 }
1260
Siena Richard7c2db772016-12-21 11:32:34 -08001261 snd_device_be_idx = platform_get_snd_device_backend_index(snd_device);
1262 if (snd_device_be_idx < 0) {
1263 ALOGE("%s: Couldn't get the backend index for snd device %s ret=%d",
1264 __func__, platform_get_snd_device_name(snd_device),
1265 snd_device_be_idx);
1266 }
1267
Zhou Song06761dd2019-04-28 18:08:17 +08001268 sample_rate = audio_extn_utils_get_app_sample_rate_for_device(adev, usecase, snd_device);
1269
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301270 if ((usecase->type == PCM_PLAYBACK) && (usecase->stream.out != NULL)) {
Varun Balaraje49253e2017-07-06 19:48:56 +05301271 /* Interactive streams are supported with only direct app type id.
1272 * Get Direct profile app type and use it for interactive streams
1273 */
1274 list_for_each(node, &adev->streams_output_cfg_list) {
1275 s_info = node_to_item(node, struct streams_io_cfg, list);
Aalique Grahame5cd12ff2017-10-19 10:57:00 -07001276 if (s_info->flags.out_flags == (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_BD |
Nikhil Laturkarac4a7492017-08-31 18:27:19 +05301277 AUDIO_OUTPUT_FLAG_DIRECT_PCM |
1278 AUDIO_OUTPUT_FLAG_DIRECT))
1279 bd_app_type = s_info->app_type_cfg.app_type;
Varun Balaraje49253e2017-07-06 19:48:56 +05301280 }
Aalique Grahame5cd12ff2017-10-19 10:57:00 -07001281 if (usecase->stream.out->flags == (audio_output_flags_t)AUDIO_OUTPUT_FLAG_INTERACTIVE)
Nikhil Laturkarac4a7492017-08-31 18:27:19 +05301282 app_type = bd_app_type;
Varun Balaraje49253e2017-07-06 19:48:56 +05301283 else
1284 app_type = usecase->stream.out->app_type_cfg.app_type;
Siena Richard7c2db772016-12-21 11:32:34 -08001285 app_type_cfg[len++] = app_type;
Mingming Yin21854652016-04-13 11:54:02 -07001286 app_type_cfg[len++] = acdb_dev_id;
Naresh Tanniru3a406772017-05-10 13:09:05 -07001287 app_type_cfg[len++] = sample_rate;
1288
Siena Richard7c2db772016-12-21 11:32:34 -08001289 if (snd_device_be_idx > 0)
1290 app_type_cfg[len++] = snd_device_be_idx;
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301291
Siena Richard7c2db772016-12-21 11:32:34 -08001292 ALOGI("%s PLAYBACK app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
1293 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301294
1295 } else if ((usecase->type == PCM_CAPTURE) && (usecase->stream.in != NULL)) {
Siena Richard7c2db772016-12-21 11:32:34 -08001296 app_type = usecase->stream.in->app_type_cfg.app_type;
1297 app_type_cfg[len++] = app_type;
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301298 app_type_cfg[len++] = acdb_dev_id;
Siena Richard7c2db772016-12-21 11:32:34 -08001299 app_type_cfg[len++] = sample_rate;
1300 if (snd_device_be_idx > 0)
1301 app_type_cfg[len++] = snd_device_be_idx;
1302 ALOGI("%s CAPTURE app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
1303 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301304 } else {
Siena Richard7c2db772016-12-21 11:32:34 -08001305 app_type = platform_get_default_app_type_v2(adev->platform, usecase->type);
Surendar Karka93cd25a2018-08-28 14:21:37 +05301306 if(usecase->type == TRANSCODE_LOOPBACK_RX) {
Siddartha Shaik343abc62017-08-08 11:15:25 +05301307 app_type = usecase->stream.inout->out_app_type_cfg.app_type;
1308 }
Siena Richard7c2db772016-12-21 11:32:34 -08001309 app_type_cfg[len++] = app_type;
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301310 app_type_cfg[len++] = acdb_dev_id;
1311 app_type_cfg[len++] = sample_rate;
Siena Richard7c2db772016-12-21 11:32:34 -08001312 if (snd_device_be_idx > 0)
1313 app_type_cfg[len++] = snd_device_be_idx;
1314 ALOGI("%s default app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
1315 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Ashish Jainc02430d2016-01-04 10:42:43 +05301316 }
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301317
Siddartha Shaik343abc62017-08-08 11:15:25 +05301318 if(ctl)
1319 mixer_ctl_set_array(ctl, app_type_cfg, len);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001320 rc = 0;
1321exit_send_app_type_cfg:
1322 return rc;
1323}
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001324
Deeraj Soman14230922019-01-30 16:39:30 +05301325static int audio_extn_utils_check_input_parameters(uint32_t sample_rate,
1326 audio_format_t format,
1327 int channel_count)
1328{
1329 int ret = 0;
1330
1331 if (((format != AUDIO_FORMAT_PCM_16_BIT) && (format != AUDIO_FORMAT_PCM_8_24_BIT) &&
1332 (format != AUDIO_FORMAT_PCM_24_BIT_PACKED) && (format != AUDIO_FORMAT_PCM_32_BIT) &&
1333 (format != AUDIO_FORMAT_PCM_FLOAT)) &&
1334 !voice_extn_compress_voip_is_format_supported(format) &&
1335 !audio_extn_compr_cap_format_supported(format) &&
1336 !audio_extn_cin_format_supported(format))
1337 ret = -EINVAL;
1338
1339 switch (channel_count) {
1340 case 1:
1341 case 2:
1342 case 3:
1343 case 4:
1344 case 6:
1345 case 8:
1346 break;
1347 default:
1348 ret = -EINVAL;
1349 }
1350
1351 switch (sample_rate) {
1352 case 8000:
1353 case 11025:
1354 case 12000:
1355 case 16000:
1356 case 22050:
1357 case 24000:
1358 case 32000:
1359 case 44100:
1360 case 48000:
1361 case 88200:
1362 case 96000:
1363 case 176400:
1364 case 192000:
1365 break;
1366 default:
1367 ret = -EINVAL;
1368 }
1369
1370 return ret;
1371}
1372
1373static inline uint32_t audio_extn_utils_nearest_multiple(uint32_t num, uint32_t multiplier)
1374{
1375 uint32_t remainder = 0;
1376
1377 if (!multiplier)
1378 return num;
1379
1380 remainder = num % multiplier;
1381 if (remainder)
1382 num += (multiplier - remainder);
1383
1384 return num;
1385}
1386
1387static inline uint32_t audio_extn_utils_lcm(uint32_t num1, uint32_t num2)
1388{
1389 uint32_t high = num1, low = num2, temp = 0;
1390
1391 if (!num1 || !num2)
1392 return 0;
1393
1394 if (num1 < num2) {
1395 high = num2;
1396 low = num1;
1397 }
1398
1399 while (low != 0) {
1400 temp = low;
1401 low = high % low;
1402 high = temp;
1403 }
1404 return (num1 * num2)/high;
1405}
1406
Siena Richard7c2db772016-12-21 11:32:34 -08001407int audio_extn_utils_send_app_type_cfg(struct audio_device *adev,
1408 struct audio_usecase *usecase)
1409{
1410 int i, num_devices = 0;
1411 snd_device_t new_snd_devices[SND_DEVICE_OUT_END] = {0};
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301412 snd_device_t in_snd_device = usecase->in_snd_device;
Siena Richard7c2db772016-12-21 11:32:34 -08001413 int rc = 0;
1414
Aalique Grahame22e49102018-12-18 14:23:57 -08001415 if (usecase->type == PCM_HFP_CALL) {
1416 return audio_extn_utils_send_app_type_cfg_hfp(adev, usecase);
1417 }
1418
Siena Richard7c2db772016-12-21 11:32:34 -08001419 switch (usecase->type) {
1420 case PCM_PLAYBACK:
Surendar Karka93cd25a2018-08-28 14:21:37 +05301421 case TRANSCODE_LOOPBACK_RX:
Siena Richard7c2db772016-12-21 11:32:34 -08001422 ALOGD("%s: usecase->out_snd_device %s",
1423 __func__, platform_get_snd_device_name(usecase->out_snd_device));
1424 /* check for out combo device */
1425 if (platform_split_snd_device(adev->platform,
1426 usecase->out_snd_device,
1427 &num_devices, new_snd_devices)) {
1428 new_snd_devices[0] = usecase->out_snd_device;
1429 num_devices = 1;
1430 }
1431 break;
1432 case PCM_CAPTURE:
1433 ALOGD("%s: usecase->in_snd_device %s",
1434 __func__, platform_get_snd_device_name(usecase->in_snd_device));
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301435 if (voice_is_in_call_rec_stream(usecase->stream.in)) {
1436 in_snd_device = voice_get_incall_rec_backend_device(usecase->stream.in);
1437 }
Siena Richard7c2db772016-12-21 11:32:34 -08001438 /* check for in combo device */
1439 if (platform_split_snd_device(adev->platform,
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301440 in_snd_device,
Siena Richard7c2db772016-12-21 11:32:34 -08001441 &num_devices, new_snd_devices)) {
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301442 new_snd_devices[0] = in_snd_device;
Siena Richard7c2db772016-12-21 11:32:34 -08001443 num_devices = 1;
1444 }
1445 break;
1446 default:
1447 ALOGI("%s: not a playback/capture path, no need to cfg app type", __func__);
1448 rc = 0;
1449 break;
1450 }
1451
1452 for (i = 0; i < num_devices; i++) {
1453 rc = send_app_type_cfg_for_device(adev, usecase, new_snd_devices[i]);
1454 if (rc)
1455 break;
1456 }
1457
1458 return rc;
1459}
1460
Preetam Singh Ranawat9519e9c2015-11-18 16:05:55 +05301461int read_line_from_file(const char *path, char *buf, size_t count)
1462{
1463 char * fgets_ret;
1464 FILE * fd;
1465 int rv;
1466
1467 fd = fopen(path, "r");
1468 if (fd == NULL)
1469 return -1;
1470
1471 fgets_ret = fgets(buf, (int)count, fd);
1472 if (NULL != fgets_ret) {
1473 rv = (int)strlen(buf);
1474 } else {
1475 rv = ferror(fd);
1476 }
1477 fclose(fd);
1478
1479 return rv;
1480}
1481
Ashish Jainf1eaa582016-05-23 20:54:24 +05301482/*Translates ALSA formats to AOSP PCM formats*/
1483audio_format_t alsa_format_to_hal(uint32_t alsa_format)
1484{
1485 audio_format_t format;
1486
1487 switch(alsa_format) {
1488 case SNDRV_PCM_FORMAT_S16_LE:
1489 format = AUDIO_FORMAT_PCM_16_BIT;
1490 break;
1491 case SNDRV_PCM_FORMAT_S24_3LE:
1492 format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
1493 break;
1494 case SNDRV_PCM_FORMAT_S24_LE:
1495 format = AUDIO_FORMAT_PCM_8_24_BIT;
1496 break;
1497 case SNDRV_PCM_FORMAT_S32_LE:
1498 format = AUDIO_FORMAT_PCM_32_BIT;
1499 break;
1500 default:
1501 ALOGW("Incorrect ALSA format");
1502 format = AUDIO_FORMAT_INVALID;
1503 }
1504 return format;
1505}
1506
1507/*Translates hal format (AOSP) to alsa formats*/
1508uint32_t hal_format_to_alsa(audio_format_t hal_format)
1509{
1510 uint32_t alsa_format;
1511
1512 switch (hal_format) {
1513 case AUDIO_FORMAT_PCM_32_BIT: {
1514 if (platform_supports_true_32bit())
1515 alsa_format = SNDRV_PCM_FORMAT_S32_LE;
1516 else
1517 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1518 }
1519 break;
1520 case AUDIO_FORMAT_PCM_8_BIT:
1521 alsa_format = SNDRV_PCM_FORMAT_S8;
1522 break;
1523 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
1524 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1525 break;
1526 case AUDIO_FORMAT_PCM_8_24_BIT: {
1527 if (platform_supports_true_32bit())
1528 alsa_format = SNDRV_PCM_FORMAT_S32_LE;
1529 else
1530 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1531 }
1532 break;
1533 case AUDIO_FORMAT_PCM_FLOAT:
1534 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1535 break;
1536 default:
1537 case AUDIO_FORMAT_PCM_16_BIT:
1538 alsa_format = SNDRV_PCM_FORMAT_S16_LE;
1539 break;
1540 }
1541 return alsa_format;
1542}
1543
Ashish Jain83a6cc22016-06-28 14:34:17 +05301544/*Translates PCM formats to AOSP formats*/
1545audio_format_t pcm_format_to_hal(uint32_t pcm_format)
1546{
1547 audio_format_t format = AUDIO_FORMAT_INVALID;
1548
1549 switch(pcm_format) {
1550 case PCM_FORMAT_S16_LE:
1551 format = AUDIO_FORMAT_PCM_16_BIT;
1552 break;
1553 case PCM_FORMAT_S24_3LE:
1554 format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
1555 break;
1556 case PCM_FORMAT_S24_LE:
1557 format = AUDIO_FORMAT_PCM_8_24_BIT;
1558 break;
1559 case PCM_FORMAT_S32_LE:
1560 format = AUDIO_FORMAT_PCM_32_BIT;
1561 break;
1562 default:
1563 ALOGW("Incorrect PCM format");
1564 format = AUDIO_FORMAT_INVALID;
1565 }
1566 return format;
1567}
1568
1569/*Translates hal format (AOSP) to alsa formats*/
1570uint32_t hal_format_to_pcm(audio_format_t hal_format)
1571{
1572 uint32_t pcm_format;
1573
1574 switch (hal_format) {
1575 case AUDIO_FORMAT_PCM_32_BIT:
1576 case AUDIO_FORMAT_PCM_8_24_BIT:
1577 case AUDIO_FORMAT_PCM_FLOAT: {
1578 if (platform_supports_true_32bit())
1579 pcm_format = PCM_FORMAT_S32_LE;
1580 else
1581 pcm_format = PCM_FORMAT_S24_3LE;
1582 }
1583 break;
1584 case AUDIO_FORMAT_PCM_8_BIT:
1585 pcm_format = PCM_FORMAT_S8;
1586 break;
1587 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
1588 pcm_format = PCM_FORMAT_S24_3LE;
1589 break;
1590 default:
1591 case AUDIO_FORMAT_PCM_16_BIT:
1592 pcm_format = PCM_FORMAT_S16_LE;
1593 break;
1594 }
1595 return pcm_format;
1596}
1597
Ashish Jainf1eaa582016-05-23 20:54:24 +05301598uint32_t get_alsa_fragment_size(uint32_t bytes_per_sample,
1599 uint32_t sample_rate,
Deeraj Soman65358ab2019-02-07 15:40:49 +05301600 uint32_t noOfChannels,
1601 int64_t duration_ms)
Ashish Jainf1eaa582016-05-23 20:54:24 +05301602{
1603 uint32_t fragment_size = 0;
1604 uint32_t pcm_offload_time = PCM_OFFLOAD_BUFFER_DURATION;
1605
Deeraj Soman65358ab2019-02-07 15:40:49 +05301606 if (duration_ms >= MIN_OFFLOAD_BUFFER_DURATION_MS && duration_ms <= MAX_OFFLOAD_BUFFER_DURATION_MS)
1607 pcm_offload_time = duration_ms;
1608
Ashish Jainf1eaa582016-05-23 20:54:24 +05301609 fragment_size = (pcm_offload_time
1610 * sample_rate
1611 * bytes_per_sample
1612 * noOfChannels)/1000;
1613 if (fragment_size < MIN_PCM_OFFLOAD_FRAGMENT_SIZE)
1614 fragment_size = MIN_PCM_OFFLOAD_FRAGMENT_SIZE;
1615 else if (fragment_size > MAX_PCM_OFFLOAD_FRAGMENT_SIZE)
1616 fragment_size = MAX_PCM_OFFLOAD_FRAGMENT_SIZE;
1617 /*To have same PCM samples for all channels, the buffer size requires to
1618 *be multiple of (number of channels * bytes per sample)
1619 *For writes to succeed, the buffer must be written at address which is multiple of 32
1620 */
Aalique Grahameb85f2d92017-10-16 17:53:23 -07001621 fragment_size = ALIGN(fragment_size, (bytes_per_sample * noOfChannels * 32));
Ashish Jainf1eaa582016-05-23 20:54:24 +05301622
1623 ALOGI("PCM offload Fragment size to %d bytes", fragment_size);
1624 return fragment_size;
1625}
1626
1627/* Calculates the fragment size required to configure compress session.
1628 * Based on the alsa format selected, decide if conversion is needed in
1629
1630 * HAL ( e.g. convert AUDIO_FORMAT_PCM_FLOAT input format to
1631 * AUDIO_FORMAT_PCM_24_BIT_PACKED before writing to the compress driver.
1632 */
1633void audio_extn_utils_update_direct_pcm_fragment_size(struct stream_out *out)
1634{
Ashish Jain83a6cc22016-06-28 14:34:17 +05301635 audio_format_t dst_format = out->hal_op_format;
1636 audio_format_t src_format = out->hal_ip_format;
Ashish Jainf1eaa582016-05-23 20:54:24 +05301637 uint32_t hal_op_bytes_per_sample = audio_bytes_per_sample(dst_format);
1638 uint32_t hal_ip_bytes_per_sample = audio_bytes_per_sample(src_format);
1639
1640 out->compr_config.fragment_size =
1641 get_alsa_fragment_size(hal_op_bytes_per_sample,
1642 out->sample_rate,
Deeraj Soman65358ab2019-02-07 15:40:49 +05301643 popcount(out->channel_mask),
1644 out->info.duration_us / 1000);
Ashish Jainf1eaa582016-05-23 20:54:24 +05301645
1646 if ((src_format != dst_format) &&
1647 hal_op_bytes_per_sample != hal_ip_bytes_per_sample) {
1648
Ashish Jain83a6cc22016-06-28 14:34:17 +05301649 out->hal_fragment_size =
Ashish Jainf1eaa582016-05-23 20:54:24 +05301650 ((out->compr_config.fragment_size * hal_ip_bytes_per_sample) /
1651 hal_op_bytes_per_sample);
1652 ALOGI("enable conversion hal_input_fragment_size is %d src_format %x dst_format %x",
Ashish Jain83a6cc22016-06-28 14:34:17 +05301653 out->hal_fragment_size, src_format, dst_format);
Ashish Jainf1eaa582016-05-23 20:54:24 +05301654 } else {
Ashish Jain83a6cc22016-06-28 14:34:17 +05301655 out->hal_fragment_size = out->compr_config.fragment_size;
Ashish Jainf1eaa582016-05-23 20:54:24 +05301656 }
1657}
1658
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301659/* converts pcm format 24_8 to 8_24 inplace */
1660size_t audio_extn_utils_convert_format_24_8_to_8_24(void *buf, size_t bytes)
1661{
1662 size_t i = 0;
1663 int *int_buf_stream = buf;
1664
1665 if ((bytes % 4) != 0) {
1666 ALOGE("%s: wrong inout buffer! ... is not 32 bit aligned ", __func__);
1667 return -EINVAL;
1668 }
1669
1670 for (; i < (bytes / 4); i++)
1671 int_buf_stream[i] >>= 8;
1672
1673 return bytes;
1674}
1675
1676int get_snd_codec_id(audio_format_t format)
1677{
1678 int id = 0;
1679
1680 switch (format & AUDIO_FORMAT_MAIN_MASK) {
1681 case AUDIO_FORMAT_MP3:
1682 id = SND_AUDIOCODEC_MP3;
1683 break;
1684 case AUDIO_FORMAT_AAC:
1685 id = SND_AUDIOCODEC_AAC;
1686 break;
1687 case AUDIO_FORMAT_AAC_ADTS:
1688 id = SND_AUDIOCODEC_AAC;
1689 break;
Arun Kumar Dasari3b174182016-12-27 13:01:14 +05301690 case AUDIO_FORMAT_AAC_LATM:
1691 id = SND_AUDIOCODEC_AAC;
1692 break;
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301693 case AUDIO_FORMAT_PCM:
1694 id = SND_AUDIOCODEC_PCM;
1695 break;
1696 case AUDIO_FORMAT_FLAC:
1697 id = SND_AUDIOCODEC_FLAC;
1698 break;
1699 case AUDIO_FORMAT_ALAC:
1700 id = SND_AUDIOCODEC_ALAC;
1701 break;
1702 case AUDIO_FORMAT_APE:
1703 id = SND_AUDIOCODEC_APE;
1704 break;
1705 case AUDIO_FORMAT_VORBIS:
1706 id = SND_AUDIOCODEC_VORBIS;
1707 break;
1708 case AUDIO_FORMAT_WMA:
1709 id = SND_AUDIOCODEC_WMA;
1710 break;
1711 case AUDIO_FORMAT_WMA_PRO:
1712 id = SND_AUDIOCODEC_WMA_PRO;
1713 break;
Satish Babu Patakokila0c313922016-12-08 12:07:08 +05301714 case AUDIO_FORMAT_MP2:
1715 id = SND_AUDIOCODEC_MP2;
1716 break;
Satish Babu Patakokila915ecba2017-01-10 17:43:56 +05301717 case AUDIO_FORMAT_AC3:
1718 id = SND_AUDIOCODEC_AC3;
1719 break;
1720 case AUDIO_FORMAT_E_AC3:
1721 case AUDIO_FORMAT_E_AC3_JOC:
1722 id = SND_AUDIOCODEC_EAC3;
1723 break;
1724 case AUDIO_FORMAT_DTS:
1725 case AUDIO_FORMAT_DTS_HD:
1726 id = SND_AUDIOCODEC_DTS;
1727 break;
Ben Romberger1aaaf862017-04-06 17:49:46 -07001728 case AUDIO_FORMAT_DOLBY_TRUEHD:
1729 id = SND_AUDIOCODEC_TRUEHD;
1730 break;
Naresh Tanniru928f0862017-04-07 16:44:23 -07001731 case AUDIO_FORMAT_IEC61937:
1732 id = SND_AUDIOCODEC_IEC61937;
1733 break;
Preetam Singh Ranawat4277a5a2017-01-18 19:02:24 +05301734 case AUDIO_FORMAT_DSD:
1735 id = SND_AUDIOCODEC_DSD;
1736 break;
Dhanalakshmi Siddani18737932016-11-29 17:33:17 +05301737 case AUDIO_FORMAT_APTX:
1738 id = SND_AUDIOCODEC_APTX;
1739 break;
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301740 default:
1741 ALOGE("%s: Unsupported audio format :%x", __func__, format);
1742 }
1743
1744 return id;
1745}
1746
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001747void audio_extn_utils_send_audio_calibration(struct audio_device *adev,
1748 struct audio_usecase *usecase)
1749{
1750 int type = usecase->type;
1751
Dhananjay Kumar14245982017-01-16 20:21:00 +05301752 if (type == PCM_PLAYBACK && usecase->stream.out != NULL) {
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05301753 platform_send_audio_calibration(adev->platform, usecase,
Zhou Song06761dd2019-04-28 18:08:17 +08001754 usecase->stream.out->app_type_cfg.app_type);
Dhananjay Kumar14245982017-01-16 20:21:00 +05301755 } else if (type == PCM_CAPTURE && usecase->stream.in != NULL) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301756 platform_send_audio_calibration(adev->platform, usecase,
Zhou Song06761dd2019-04-28 18:08:17 +08001757 usecase->stream.in->app_type_cfg.app_type);
1758 } else if ((type == PCM_HFP_CALL) || (type == PCM_CAPTURE) ||
1759 (type == TRANSCODE_LOOPBACK_RX && usecase->stream.inout != NULL)) {
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05301760 platform_send_audio_calibration(adev->platform, usecase,
Zhou Song06761dd2019-04-28 18:08:17 +08001761 platform_get_default_app_type_v2(adev->platform, usecase->type));
Vidyakumar Athotae57f6002017-03-01 13:13:16 -08001762 } else {
1763 /* No need to send audio calibration for voice and voip call usecases */
1764 if ((type != VOICE_CALL) && (type != VOIP_CALL))
1765 ALOGW("%s: No audio calibration for usecase type = %d", __func__, type);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001766 }
1767}
1768
Ben Rombergera04fabc2014-11-14 12:16:03 -08001769// Base64 Encode and Decode
1770// Not all features supported. This must be used only with following conditions.
1771// Decode Modes: Support with and without padding
1772// CRLF not handling. So no CRLF in string to decode.
1773// Encode Modes: Supports only padding
1774int b64decode(char *inp, int ilen, uint8_t* outp)
1775{
1776 int i, j, k, ii, num;
1777 int rem, pcnt;
1778 uint32_t res=0;
1779 uint8_t getIndex[MAX_BASEINDEX_LEN];
1780 uint8_t tmp, cflag;
1781
1782 if(inp == NULL || outp == NULL || ilen <= 0) {
1783 ALOGE("[%s] received NULL pointer or zero length",__func__);
1784 return -1;
1785 }
1786
1787 memset(getIndex, MAX_BASEINDEX_LEN-1, sizeof(getIndex));
1788 for(i=0;i<BASE_TABLE_SIZE;i++) {
1789 getIndex[(uint8_t)bTable[i]] = (uint8_t)i;
1790 }
1791 getIndex[(uint8_t)'=']=0;
1792
1793 j=0;k=0;
1794 num = ilen/4;
1795 rem = ilen%4;
1796 if(rem==0)
1797 num = num-1;
1798 cflag=0;
1799 for(i=0; i<num; i++) {
1800 res=0;
1801 for(ii=0;ii<4;ii++) {
1802 res = res << 6;
1803 tmp = getIndex[(uint8_t)inp[j++]];
1804 res = res | tmp;
1805 cflag = cflag | tmp;
1806 }
1807 outp[k++] = (res >> 16)&0xFF;
1808 outp[k++] = (res >> 8)&0xFF;
1809 outp[k++] = res & 0xFF;
1810 }
1811
1812 // Handle last bytes special
1813 pcnt=0;
1814 if(rem == 0) {
1815 //With padding or full data
1816 res = 0;
1817 for(ii=0;ii<4;ii++) {
1818 if(inp[j] == '=')
1819 pcnt++;
1820 res = res << 6;
1821 tmp = getIndex[(uint8_t)inp[j++]];
1822 res = res | tmp;
1823 cflag = cflag | tmp;
1824 }
1825 outp[k++] = res >> 16;
1826 if(pcnt == 2)
1827 goto done;
1828 outp[k++] = (res>>8)&0xFF;
1829 if(pcnt == 1)
1830 goto done;
1831 outp[k++] = res&0xFF;
1832 } else {
1833 //without padding
1834 res = 0;
1835 for(i=0;i<rem;i++) {
1836 res = res << 6;
1837 tmp = getIndex[(uint8_t)inp[j++]];
1838 res = res | tmp;
1839 cflag = cflag | tmp;
1840 }
1841 for(i=rem;i<4;i++) {
1842 res = res << 6;
1843 pcnt++;
1844 }
1845 outp[k++] = res >> 16;
1846 if(pcnt == 2)
1847 goto done;
1848 outp[k++] = (res>>8)&0xFF;
1849 if(pcnt == 1)
1850 goto done;
1851 outp[k++] = res&0xFF;
1852 }
1853done:
1854 if(cflag == 0xFF) {
1855 ALOGE("[%s] base64 decode failed. Invalid character found %s",
1856 __func__, inp);
1857 return 0;
1858 }
1859 return k;
1860}
1861
1862int b64encode(uint8_t *inp, int ilen, char* outp)
1863{
1864 int i,j,k, num;
1865 int rem=0;
1866 uint32_t res=0;
1867
1868 if(inp == NULL || outp == NULL || ilen<=0) {
1869 ALOGE("[%s] received NULL pointer or zero input length",__func__);
1870 return -1;
1871 }
1872
1873 num = ilen/3;
1874 rem = ilen%3;
1875 j=0;k=0;
1876 for(i=0; i<num; i++) {
1877 //prepare index
1878 res = inp[j++]<<16;
1879 res = res | inp[j++]<<8;
1880 res = res | inp[j++];
1881 //get output map from index
1882 outp[k++] = (char) bTable[(res>>18)&0x3F];
1883 outp[k++] = (char) bTable[(res>>12)&0x3F];
1884 outp[k++] = (char) bTable[(res>>6)&0x3F];
1885 outp[k++] = (char) bTable[res&0x3F];
1886 }
1887
1888 switch(rem) {
1889 case 1:
1890 res = inp[j++]<<16;
1891 outp[k++] = (char) bTable[res>>18];
1892 outp[k++] = (char) bTable[(res>>12)&0x3F];
1893 //outp[k++] = '=';
1894 //outp[k++] = '=';
1895 break;
1896 case 2:
1897 res = inp[j++]<<16;
1898 res = res | inp[j++]<<8;
1899 outp[k++] = (char) bTable[res>>18];
1900 outp[k++] = (char) bTable[(res>>12)&0x3F];
1901 outp[k++] = (char) bTable[(res>>6)&0x3F];
1902 //outp[k++] = '=';
1903 break;
1904 default:
1905 break;
1906 }
Ben Rombergera04fabc2014-11-14 12:16:03 -08001907 outp[k] = '\0';
1908 return k;
1909}
Ashish Jain81eb2a82015-05-13 10:52:34 +05301910
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301911
1912int audio_extn_utils_get_codec_version(const char *snd_card_name,
1913 int card_num,
1914 char *codec_version)
1915{
1916 char procfs_path[50];
1917 FILE *fp;
1918
1919 if (strstr(snd_card_name, "tasha")) {
1920 snprintf(procfs_path, sizeof(procfs_path),
1921 "/proc/asound/card%d/codecs/tasha/version", card_num);
1922 if ((fp = fopen(procfs_path, "r")) != NULL) {
1923 fgets(codec_version, CODEC_VERSION_MAX_LENGTH, fp);
1924 fclose(fp);
1925 } else {
1926 ALOGE("%s: ERROR. cannot open %s", __func__, procfs_path);
1927 return -ENOENT;
1928 }
1929 ALOGD("%s: codec version %s", __func__, codec_version);
1930 }
1931
1932 return 0;
1933}
1934
Preetam Singh Ranawat9d0d8e42019-07-15 12:27:25 +05301935int audio_extn_utils_get_codec_variant(int card_num,
1936 char *codec_variant)
1937{
1938 char procfs_path[50];
1939 FILE *fp;
1940 snprintf(procfs_path, sizeof(procfs_path),
1941 "/proc/asound/card%d/codecs/wcd938x/variant", card_num);
1942 if ((fp = fopen(procfs_path, "r")) == NULL) {
1943 snprintf(procfs_path, sizeof(procfs_path),
1944 "/proc/asound/card%d/codecs/wcd937x/variant", card_num);
1945 if ((fp = fopen(procfs_path, "r")) == NULL) {
1946 ALOGE("%s: ERROR. cannot open %s", __func__, procfs_path);
1947 return -ENOENT;
1948 }
1949 }
1950 fgets(codec_variant, CODEC_VARIANT_MAX_LENGTH, fp);
1951 fclose(fp);
1952 ALOGD("%s: codec variant is %s", __func__, codec_variant);
1953 return 0;
1954}
1955
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301956
Ashish Jain81eb2a82015-05-13 10:52:34 +05301957#ifdef AUDIO_EXTERNAL_HDMI_ENABLED
1958
1959void get_default_compressed_channel_status(
1960 unsigned char *channel_status)
1961{
Ashish Jain81eb2a82015-05-13 10:52:34 +05301962 memset(channel_status,0,24);
1963
1964 /* block start bit in preamble bit 3 */
1965 channel_status[0] |= PROFESSIONAL;
1966 //compre out
1967 channel_status[0] |= NON_LPCM;
1968 // sample rate; fixed 48K for default/transcode
1969 channel_status[3] |= SR_48000;
1970}
1971
Ashish Jain81eb2a82015-05-13 10:52:34 +05301972int32_t get_compressed_channel_status(void *audio_stream_data,
1973 uint32_t audio_frame_size,
1974 unsigned char *channel_status,
1975 enum audio_parser_code_type codec_type)
1976 // codec_type - AUDIO_PARSER_CODEC_AC3
1977 // - AUDIO_PARSER_CODEC_DTS
1978{
1979 unsigned char *stream;
1980 int ret = 0;
1981 stream = (unsigned char *)audio_stream_data;
1982
1983 if (audio_stream_data == NULL || audio_frame_size == 0) {
1984 ALOGW("no buffer to get channel status, return default for compress");
1985 get_default_compressed_channel_status(channel_status);
1986 return ret;
1987 }
1988
1989 memset(channel_status,0,24);
1990 if(init_audio_parser(stream, audio_frame_size, codec_type) == -1)
1991 {
1992 ALOGE("init audio parser failed");
1993 return -1;
1994 }
1995 ret = get_channel_status(channel_status, codec_type);
1996 return ret;
1997
1998}
1999
Ashish Jain81eb2a82015-05-13 10:52:34 +05302000void get_lpcm_channel_status(uint32_t sampleRate,
2001 unsigned char *channel_status)
2002{
2003 int32_t status = 0;
Ashish Jain81eb2a82015-05-13 10:52:34 +05302004 memset(channel_status,0,24);
2005 /* block start bit in preamble bit 3 */
2006 channel_status[0] |= PROFESSIONAL;
2007 //LPCM OUT
2008 channel_status[0] &= ~NON_LPCM;
2009
2010 switch (sampleRate) {
2011 case 8000:
2012 case 11025:
2013 case 12000:
2014 case 16000:
2015 case 22050:
2016 channel_status[3] |= SR_NOTID;
Preetam Singh Ranawat61716b12015-12-14 11:55:24 +05302017 break;
Ashish Jain81eb2a82015-05-13 10:52:34 +05302018 case 24000:
2019 channel_status[3] |= SR_24000;
2020 break;
2021 case 32000:
2022 channel_status[3] |= SR_32000;
2023 break;
2024 case 44100:
2025 channel_status[3] |= SR_44100;
2026 break;
2027 case 48000:
2028 channel_status[3] |= SR_48000;
2029 break;
2030 case 88200:
2031 channel_status[3] |= SR_88200;
2032 break;
2033 case 96000:
2034 channel_status[3] |= SR_96000;
2035 break;
2036 case 176400:
2037 channel_status[3] |= SR_176400;
2038 break;
2039 case 192000:
2040 channel_status[3] |= SR_192000;
2041 break;
2042 default:
2043 ALOGV("Invalid sample_rate %u\n", sampleRate);
2044 status = -1;
2045 break;
2046 }
2047}
2048
2049void audio_utils_set_hdmi_channel_status(struct stream_out *out, char * buffer, size_t bytes)
2050{
2051 unsigned char channel_status[24]={0};
2052 struct snd_aes_iec958 iec958;
2053 const char *mixer_ctl_name = "IEC958 Playback PCM Stream";
2054 struct mixer_ctl *ctl;
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +05302055 ALOGV("%s: buffer %s bytes %zd", __func__, buffer, bytes);
Arun Mirpurie008ed22019-03-21 11:21:04 -07002056
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05302057 if (audio_extn_utils_is_dolby_format(out->format) &&
Ashish Jain81eb2a82015-05-13 10:52:34 +05302058 /*TODO:Extend code to support DTS passthrough*/
2059 /*set compressed channel status bits*/
Arun Mirpurie008ed22019-03-21 11:21:04 -07002060 audio_extn_passthru_is_passthrough_stream(out) &&
2061 audio_extn_is_hdmi_passthru_enabled()) {
Ashish Jain81eb2a82015-05-13 10:52:34 +05302062 get_compressed_channel_status(buffer, bytes, channel_status, AUDIO_PARSER_CODEC_AC3);
Arun Mirpurie008ed22019-03-21 11:21:04 -07002063 } else {
Ashish Jain81eb2a82015-05-13 10:52:34 +05302064 /*set channel status bit for LPCM*/
2065 get_lpcm_channel_status(out->sample_rate, channel_status);
2066 }
2067
2068 memcpy(iec958.status, channel_status,sizeof(iec958.status));
2069 ctl = mixer_get_ctl_by_name(out->dev->mixer, mixer_ctl_name);
2070 if (!ctl) {
2071 ALOGE("%s: Could not get ctl for mixer cmd - %s",
2072 __func__, mixer_ctl_name);
2073 return;
2074 }
2075 if (mixer_ctl_set_array(ctl, &iec958, sizeof(iec958)) < 0) {
2076 ALOGE("%s: Could not set channel status for ext HDMI ",
2077 __func__);
2078 return;
2079 }
2080
2081}
2082#endif
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302083
2084int audio_extn_utils_get_avt_device_drift(
2085 struct audio_usecase *usecase,
2086 struct audio_avt_device_drift_param *drift_param)
2087{
2088 int ret = 0, count = 0;
2089 char avt_device_drift_mixer_ctl_name[MIXER_PATH_MAX_LENGTH] = {0};
Naresh Tanniru6160c712017-04-17 15:43:48 +05302090 const char *backend = NULL;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302091 struct mixer_ctl *ctl = NULL;
2092 struct audio_avt_device_drift_stats drift_stats;
2093 struct audio_device *adev = NULL;
2094
2095 if (usecase != NULL && usecase->type == PCM_PLAYBACK) {
Naresh Tanniru6160c712017-04-17 15:43:48 +05302096 backend = platform_get_snd_device_backend_interface(usecase->out_snd_device);
2097 if (!backend) {
2098 ALOGE("%s: Unsupported device %d", __func__,
2099 usecase->stream.out->devices);
2100 ret = -EINVAL;
2101 goto done;
2102 }
2103 strlcpy(avt_device_drift_mixer_ctl_name,
2104 backend,
2105 MIXER_PATH_MAX_LENGTH);
2106
2107 count = strlen(backend);
2108 if (MIXER_PATH_MAX_LENGTH - count > 0) {
2109 strlcat(&avt_device_drift_mixer_ctl_name[count],
2110 " DRIFT",
2111 MIXER_PATH_MAX_LENGTH - count);
2112 } else {
2113 ret = -EINVAL;
2114 goto done;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302115 }
2116 } else {
Naresh Tanniru7586e292017-04-13 10:38:13 +05302117 ALOGE("%s: Invalid usecase",__func__);
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302118 ret = -EINVAL;
Naresh Tanniru6160c712017-04-17 15:43:48 +05302119 goto done;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302120 }
2121
Naresh Tanniru6160c712017-04-17 15:43:48 +05302122 adev = usecase->stream.out->dev;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302123 ctl = mixer_get_ctl_by_name(adev->mixer, avt_device_drift_mixer_ctl_name);
2124 if (!ctl) {
2125 ALOGE("%s: Could not get ctl for mixer cmd - %s",
2126 __func__, avt_device_drift_mixer_ctl_name);
2127
2128 ret = -EINVAL;
2129 goto done;
2130 }
2131
2132 ALOGV("%s: Getting AV Timer vs Device Drift mixer ctrl name %s", __func__,
2133 avt_device_drift_mixer_ctl_name);
2134
2135 mixer_ctl_update(ctl);
2136 count = mixer_ctl_get_num_values(ctl);
2137 if (count != sizeof(struct audio_avt_device_drift_stats)) {
2138 ALOGE("%s: mixer_ctl_get_num_values() invalid drift_stats data size",
2139 __func__);
2140
2141 ret = -EINVAL;
2142 goto done;
2143 }
2144
2145 ret = mixer_ctl_get_array(ctl, (void *)&drift_stats, count);
2146 if (ret != 0) {
2147 ALOGE("%s: mixer_ctl_get_array() failed to get drift_stats Params",
2148 __func__);
2149
2150 ret = -EINVAL;
2151 goto done;
2152 }
2153 memcpy(drift_param, &drift_stats.drift_param,
2154 sizeof(struct audio_avt_device_drift_param));
2155done:
2156 return ret;
2157}
Manish Dewangan07de2142017-02-27 19:27:20 +05302158
2159#ifdef SNDRV_COMPRESS_PATH_DELAY
2160int audio_extn_utils_compress_get_dsp_latency(struct stream_out *out)
2161{
2162 int ret = -EINVAL;
2163 struct snd_compr_metadata metadata;
2164 int delay_ms = COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
2165
Weiyin Jiangc49a3b52018-08-17 16:16:08 +08002166 /* override the latency for pcm offload use case */
2167 if ((out->flags & AUDIO_OUTPUT_FLAG_DIRECT) &&
2168 !(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)) {
2169 delay_ms = PCM_OFFLOAD_PLAYBACK_LATENCY;
2170 }
2171
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -07002172 if (property_get_bool("vendor.audio.playback.dsp.pathdelay", false)) {
Manish Dewangan07de2142017-02-27 19:27:20 +05302173 ALOGD("%s:: Quering DSP delay %d",__func__, __LINE__);
2174 if (!(is_offload_usecase(out->usecase))) {
2175 ALOGE("%s:: not supported for non offload session", __func__);
2176 goto exit;
2177 }
2178
2179 if (!out->compr) {
2180 ALOGD("%s:: Invalid compress handle,returning default dsp latency",
2181 __func__);
2182 goto exit;
2183 }
2184
2185 metadata.key = SNDRV_COMPRESS_PATH_DELAY;
2186 ret = compress_get_metadata(out->compr, &metadata);
2187 if(ret) {
2188 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2189 goto exit;
2190 }
2191 delay_ms = metadata.value[0] / 1000; /*convert to ms*/
2192 } else {
2193 ALOGD("%s:: Using Fix DSP delay",__func__);
2194 }
2195
2196exit:
2197 ALOGD("%s:: delay in ms is %d",__func__, delay_ms);
2198 return delay_ms;
2199}
2200#else
2201int audio_extn_utils_compress_get_dsp_latency(struct stream_out *out __unused)
2202{
2203 return COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
2204}
2205#endif
Manish Dewangan69426c82017-01-30 17:35:36 +05302206
2207#ifdef SNDRV_COMPRESS_RENDER_MODE
2208int audio_extn_utils_compress_set_render_mode(struct stream_out *out)
2209{
2210 struct snd_compr_metadata metadata;
2211 int ret = -EINVAL;
2212
2213 if (!(is_offload_usecase(out->usecase))) {
2214 ALOGE("%s:: not supported for non offload session", __func__);
2215 goto exit;
2216 }
2217
2218 if (!out->compr) {
2219 ALOGD("%s:: Invalid compress handle",
2220 __func__);
2221 goto exit;
2222 }
2223
2224 ALOGD("%s:: render mode %d", __func__, out->render_mode);
2225
2226 metadata.key = SNDRV_COMPRESS_RENDER_MODE;
2227 if (out->render_mode == RENDER_MODE_AUDIO_MASTER) {
2228 metadata.value[0] = SNDRV_COMPRESS_RENDER_MODE_AUDIO_MASTER;
2229 } else if (out->render_mode == RENDER_MODE_AUDIO_STC_MASTER) {
2230 metadata.value[0] = SNDRV_COMPRESS_RENDER_MODE_STC_MASTER;
2231 } else {
2232 ret = 0;
2233 goto exit;
2234 }
2235 ret = compress_set_metadata(out->compr, &metadata);
2236 if(ret) {
2237 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2238 }
2239exit:
2240 return ret;
2241}
2242#else
2243int audio_extn_utils_compress_set_render_mode(struct stream_out *out __unused)
2244{
2245 ALOGD("%s:: configuring render mode not supported", __func__);
2246 return 0;
2247}
2248#endif
Manish Dewangan58229382017-02-02 15:48:41 +05302249
2250#ifdef SNDRV_COMPRESS_CLK_REC_MODE
2251int audio_extn_utils_compress_set_clk_rec_mode(
2252 struct audio_usecase *usecase)
2253{
2254 struct snd_compr_metadata metadata;
2255 struct stream_out *out = NULL;
2256 int ret = -EINVAL;
2257
Naresh Tanniru7586e292017-04-13 10:38:13 +05302258 if (usecase == NULL || usecase->type != PCM_PLAYBACK) {
Manish Dewangan58229382017-02-02 15:48:41 +05302259 ALOGE("%s:: Invalid use case", __func__);
2260 goto exit;
2261 }
2262
2263 out = usecase->stream.out;
2264 if (!out) {
2265 ALOGE("%s:: invalid stream", __func__);
2266 goto exit;
2267 }
2268
2269 if (!is_offload_usecase(out->usecase)) {
2270 ALOGE("%s:: not supported for non offload session", __func__);
2271 goto exit;
2272 }
2273
2274 if (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER) {
2275 ALOGD("%s:: clk recovery is only supported in STC render mode",
2276 __func__);
2277 ret = 0;
2278 goto exit;
2279 }
2280
2281 if (!out->compr) {
2282 ALOGD("%s:: Invalid compress handle",
2283 __func__);
2284 goto exit;
2285 }
2286 metadata.key = SNDRV_COMPRESS_CLK_REC_MODE;
2287 switch(usecase->out_snd_device) {
2288 case SND_DEVICE_OUT_HDMI:
2289 case SND_DEVICE_OUT_SPEAKER_AND_HDMI:
2290 case SND_DEVICE_OUT_DISPLAY_PORT:
2291 case SND_DEVICE_OUT_SPEAKER_AND_DISPLAY_PORT:
2292 metadata.value[0] = SNDRV_COMPRESS_CLK_REC_MODE_NONE;
2293 break;
2294 default:
2295 metadata.value[0] = SNDRV_COMPRESS_CLK_REC_MODE_AUTO;
2296 break;
2297 }
2298
2299 ALOGD("%s:: clk recovery mode %d",__func__, metadata.value[0]);
2300
2301 ret = compress_set_metadata(out->compr, &metadata);
2302 if(ret) {
2303 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2304 }
2305
2306exit:
2307 return ret;
2308}
2309#else
2310int audio_extn_utils_compress_set_clk_rec_mode(
2311 struct audio_usecase *usecase __unused)
2312{
2313 ALOGD("%s:: configuring render mode not supported", __func__);
2314 return 0;
2315}
2316#endif
Manish Dewangan27346042017-03-01 12:56:12 +05302317
2318#ifdef SNDRV_COMPRESS_RENDER_WINDOW
2319int audio_extn_utils_compress_set_render_window(
2320 struct stream_out *out,
2321 struct audio_out_render_window_param *render_window)
2322{
2323 struct snd_compr_metadata metadata;
2324 int ret = -EINVAL;
2325
Manish Dewangan27346042017-03-01 12:56:12 +05302326 if(render_window == NULL) {
2327 ALOGE("%s:: Invalid render_window", __func__);
2328 goto exit;
2329 }
2330
Aniket Kumar Lata1e1d3662017-06-01 18:45:48 -07002331 ALOGD("%s:: render window start 0x%"PRIx64" end 0x%"PRIx64"",
2332 __func__,render_window->render_ws, render_window->render_we);
2333
Manish Dewangan27346042017-03-01 12:56:12 +05302334 if (!is_offload_usecase(out->usecase)) {
2335 ALOGE("%s:: not supported for non offload session", __func__);
2336 goto exit;
2337 }
2338
Naresh Tanniru6160c712017-04-17 15:43:48 +05302339 if ((out->render_mode != RENDER_MODE_AUDIO_MASTER) &&
2340 (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER)) {
Manish Dewangan27346042017-03-01 12:56:12 +05302341 ALOGD("%s:: only supported in timestamp mode, current "
2342 "render mode mode %d", __func__, out->render_mode);
2343 goto exit;
2344 }
2345
2346 if (!out->compr) {
2347 ALOGW("%s:: offload session not yet opened,"
2348 "render window will be configure later", __func__);
2349 /* store render window to reconfigure in start_output_stream() */
2350 goto exit;
2351 }
2352
2353 metadata.key = SNDRV_COMPRESS_RENDER_WINDOW;
2354 /*render window start value */
2355 metadata.value[0] = 0xFFFFFFFF & render_window->render_ws; /* lsb */
2356 metadata.value[1] = \
2357 (0xFFFFFFFF00000000 & render_window->render_ws) >> 32; /* msb*/
2358 /*render window end value */
2359 metadata.value[2] = 0xFFFFFFFF & render_window->render_we; /* lsb */
2360 metadata.value[3] = \
2361 (0xFFFFFFFF00000000 & render_window->render_we) >> 32; /* msb*/
2362
2363 ret = compress_set_metadata(out->compr, &metadata);
2364 if(ret) {
2365 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2366 }
2367
2368exit:
2369 return ret;
2370}
2371#else
2372int audio_extn_utils_compress_set_render_window(
2373 struct stream_out *out __unused,
2374 struct audio_out_render_window_param *render_window __unused)
2375{
2376 ALOGD("%s:: configuring render window not supported", __func__);
2377 return 0;
2378}
2379#endif
Manish Dewangan14956cc2017-02-14 18:54:42 +05302380
2381#ifdef SNDRV_COMPRESS_START_DELAY
2382int audio_extn_utils_compress_set_start_delay(
2383 struct stream_out *out,
2384 struct audio_out_start_delay_param *delay_param)
2385{
2386 struct snd_compr_metadata metadata;
2387 int ret = -EINVAL;
2388
2389 if(delay_param == NULL) {
2390 ALOGE("%s:: Invalid delay_param", __func__);
2391 goto exit;
2392 }
2393
2394 ALOGD("%s:: render start delay 0x%"PRIx64" ", __func__,
2395 delay_param->start_delay);
2396
2397 if (!is_offload_usecase(out->usecase)) {
2398 ALOGE("%s:: not supported for non offload session", __func__);
2399 goto exit;
2400 }
2401
Naresh Tanniru6160c712017-04-17 15:43:48 +05302402 if ((out->render_mode != RENDER_MODE_AUDIO_MASTER) &&
2403 (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER)) {
Manish Dewangan14956cc2017-02-14 18:54:42 +05302404 ALOGD("%s:: only supported in timestamp mode, current "
2405 "render mode mode %d", __func__, out->render_mode);
2406 goto exit;
2407 }
2408
2409 if (!out->compr) {
2410 ALOGW("%s:: offload session not yet opened,"
2411 "start delay will be configure later", __func__);
2412 goto exit;
2413 }
2414
2415 metadata.key = SNDRV_COMPRESS_START_DELAY;
2416 metadata.value[0] = 0xFFFFFFFF & delay_param->start_delay; /* lsb */
2417 metadata.value[1] = \
2418 (0xFFFFFFFF00000000 & delay_param->start_delay) >> 32; /* msb*/
2419
2420 ret = compress_set_metadata(out->compr, &metadata);
2421 if(ret) {
2422 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2423 }
2424
2425exit:
2426 return ret;
2427}
2428#else
2429int audio_extn_utils_compress_set_start_delay(
2430 struct stream_out *out __unused,
2431 struct audio_out_start_delay_param *delay_param __unused)
2432{
2433 ALOGD("%s:: configuring render window not supported", __func__);
2434 return 0;
2435}
2436#endif
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002437
Surendar Karka287348c2019-04-10 18:31:46 +05302438#ifdef SNDRV_COMPRESS_DSP_POSITION
2439int audio_extn_utils_compress_get_dsp_presentation_pos(struct stream_out *out,
2440 uint64_t *frames, struct timespec *timestamp, int32_t clock_id)
2441{
2442 int ret = -EINVAL;
2443 uint64_t *val = NULL;
2444 uint64_t time = 0;
2445 struct snd_compr_metadata metadata;
2446
2447 ALOGV("%s:: Quering DSP position with clock id %d",__func__, clock_id);
2448 metadata.key = SNDRV_COMPRESS_DSP_POSITION;
2449 metadata.value[0] = clock_id;
2450 ret = compress_get_metadata(out->compr, &metadata);
2451 if (ret) {
2452 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2453 ret = -errno;
2454 goto exit;
2455 }
2456 val = (uint64_t *)&metadata.value[1];
2457 *frames = *val;
2458 time = *(val + 1);
2459 timestamp->tv_sec = time / 1000000;
2460 timestamp->tv_nsec = (time % 1000000)*1000;
2461
2462exit:
2463 return ret;
2464}
2465#else
2466int audio_extn_utils_compress_get_dsp_presentation_pos(struct stream_out *out __unused,
2467 uint64_t *frames __unused, struct timespec *timestamp __unused,
2468 int32_t clock_id __unused)
2469{
2470 ALOGD("%s:: dsp presentation position not supported", __func__);
2471 return 0;
2472
2473}
2474#endif
2475
2476#ifdef SNDRV_PCM_IOCTL_DSP_POSITION
2477int audio_extn_utils_pcm_get_dsp_presentation_pos(struct stream_out *out,
2478 uint64_t *frames, struct timespec *timestamp, int32_t clock_id)
2479{
2480 int ret = -EINVAL;
2481 uint64_t time = 0;
2482 struct snd_pcm_prsnt_position prsnt_position;
2483
2484 ALOGV("%s:: Quering DSP position with clock id %d",__func__, clock_id);
2485 prsnt_position.clock_id = clock_id;
2486 ret = pcm_ioctl(out->pcm, SNDRV_PCM_IOCTL_DSP_POSITION, &prsnt_position);
2487 if (ret) {
2488 ALOGE("%s::error %d", __func__, ret);
2489 ret = -EIO;
2490 goto exit;
2491 }
2492
2493 *frames = prsnt_position.frames;
2494 time = prsnt_position.timestamp;
2495 timestamp->tv_sec = time / 1000000;
2496 timestamp->tv_nsec = (time % 1000000)*1000;
2497
2498exit:
2499 return ret;
2500}
2501#else
2502int audio_extn_utils_pcm_get_dsp_presentation_pos(struct stream_out *out __unused,
2503 uint64_t *frames __unused, struct timespec *timestamp __unused,
2504 int32_t clock_id __unused)
2505{
2506 ALOGD("%s:: dsp presentation position not supported", __func__);
2507 return 0;
2508
2509}
2510#endif
2511
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002512#define MAX_SND_CARD 8
Ashish Jain30ae8942017-11-05 17:21:23 +05302513#define RETRY_US 1000000
2514#define RETRY_NUMBER 40
Aalique Grahame22e49102018-12-18 14:23:57 -08002515#define PLATFORM_INFO_XML_PATH "audio_platform_info.xml"
2516#define PLATFORM_INFO_XML_BASE_STRING "audio_platform_info"
2517
2518#ifdef LINUX_ENABLED
2519static const char *kConfigLocationList[] =
2520 {"/etc"};
2521#else
2522static const char *kConfigLocationList[] =
2523 {"/vendor/etc"};
2524#endif
2525static const int kConfigLocationListSize =
2526 (sizeof(kConfigLocationList) / sizeof(kConfigLocationList[0]));
2527
2528bool audio_extn_utils_resolve_config_file(char file_name[MIXER_PATH_MAX_LENGTH])
2529{
2530 char full_config_path[MIXER_PATH_MAX_LENGTH];
2531 for (int i = 0; i < kConfigLocationListSize; i++) {
2532 snprintf(full_config_path,
2533 MIXER_PATH_MAX_LENGTH,
2534 "%s/%s",
2535 kConfigLocationList[i],
2536 file_name);
2537 if (F_OK == access(full_config_path, 0)) {
Weiyin Jiang2995f662019-04-17 14:25:12 +08002538 strlcpy(file_name, full_config_path, MIXER_PATH_MAX_LENGTH);
Aalique Grahame22e49102018-12-18 14:23:57 -08002539 return true;
2540 }
2541 }
2542 return false;
2543}
2544
2545/* platform_info_file should be size 'MIXER_PATH_MAX_LENGTH' */
2546int audio_extn_utils_get_platform_info(const char* snd_card_name, char* platform_info_file)
2547{
2548 if (NULL == snd_card_name) {
2549 return -1;
2550 }
2551
2552 struct snd_card_split *snd_split_handle = NULL;
2553 int ret = 0;
2554 audio_extn_set_snd_card_split(snd_card_name);
2555 snd_split_handle = audio_extn_get_snd_card_split();
2556
2557 snprintf(platform_info_file, MIXER_PATH_MAX_LENGTH, "%s_%s_%s.xml",
2558 PLATFORM_INFO_XML_BASE_STRING, snd_split_handle->snd_card,
2559 snd_split_handle->form_factor);
2560
2561 if (!audio_extn_utils_resolve_config_file(platform_info_file)) {
2562 memset(platform_info_file, 0, MIXER_PATH_MAX_LENGTH);
2563 snprintf(platform_info_file, MIXER_PATH_MAX_LENGTH, "%s_%s.xml",
2564 PLATFORM_INFO_XML_BASE_STRING, snd_split_handle->snd_card);
2565
2566 if (!audio_extn_utils_resolve_config_file(platform_info_file)) {
2567 memset(platform_info_file, 0, MIXER_PATH_MAX_LENGTH);
2568 strlcpy(platform_info_file, PLATFORM_INFO_XML_PATH, MIXER_PATH_MAX_LENGTH);
2569 ret = audio_extn_utils_resolve_config_file(platform_info_file) ? 0 : -1;
2570 }
2571 }
2572
2573 return ret;
2574}
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002575
2576int audio_extn_utils_get_snd_card_num()
2577{
Soumya Managoli9fee7c62018-04-06 16:21:50 +05302578 int snd_card_num = 0;
2579 struct mixer *mixer = NULL;
2580
2581 snd_card_num = audio_extn_utils_open_snd_mixer(&mixer);
2582 if (mixer)
2583 mixer_close(mixer);
2584 return snd_card_num;
2585}
2586
2587int audio_extn_utils_open_snd_mixer(struct mixer **mixer_handle)
2588{
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002589
2590 void *hw_info = NULL;
2591 struct mixer *mixer = NULL;
2592 int retry_num = 0;
mpangfaa7bae2019-01-15 16:38:13 +08002593 int snd_card_num = 0;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002594 char* snd_card_name = NULL;
mpangfaa7bae2019-01-15 16:38:13 +08002595 int snd_card_detection_info[MAX_SND_CARD] = {0};
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002596
Soumya Managoli9fee7c62018-04-06 16:21:50 +05302597 if (!mixer_handle) {
2598 ALOGE("invalid mixer handle");
2599 return -1;
2600 }
2601 *mixer_handle = NULL;
Ashish Jain30ae8942017-11-05 17:21:23 +05302602 /*
mpangfaa7bae2019-01-15 16:38:13 +08002603 * Try with all the sound cards ( 0 to 7 ) and if none of them were detected
Ashish Jain30ae8942017-11-05 17:21:23 +05302604 * sleep for 1 sec and try detections with sound card 0 again.
2605 * If sound card gets detected, check if it is relevant, if not check with the
2606 * other sound cards. To ensure that the irrelevant sound card is not check again,
2607 * we maintain it in min_snd_card_num.
2608 */
2609 while (retry_num < RETRY_NUMBER) {
mpangfaa7bae2019-01-15 16:38:13 +08002610 snd_card_num = 0;
2611 while (snd_card_num < MAX_SND_CARD) {
2612 if (snd_card_detection_info[snd_card_num] == 0) {
2613 mixer = mixer_open(snd_card_num);
2614 if (!mixer)
2615 snd_card_num++;
2616 else
2617 break;
2618 } else
2619 snd_card_num++;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002620 }
2621
2622 if (!mixer) {
Ashish Jain30ae8942017-11-05 17:21:23 +05302623 usleep(RETRY_US);
Ashish Jain30ae8942017-11-05 17:21:23 +05302624 retry_num++;
mpangfaa7bae2019-01-15 16:38:13 +08002625 ALOGD("%s: retry, retry_num %d", __func__, retry_num);
Ashish Jain30ae8942017-11-05 17:21:23 +05302626 continue;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002627 }
2628
2629 snd_card_name = strdup(mixer_get_name(mixer));
2630 if (!snd_card_name) {
2631 ALOGE("failed to allocate memory for snd_card_name\n");
2632 mixer_close(mixer);
2633 return -1;
2634 }
2635 ALOGD("%s: snd_card_name: %s", __func__, snd_card_name);
mpangfaa7bae2019-01-15 16:38:13 +08002636 snd_card_detection_info[snd_card_num] = 1;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002637 hw_info = hw_info_init(snd_card_name);
2638 if (hw_info) {
2639 ALOGD("%s: Opened sound card:%d", __func__, snd_card_num);
2640 break;
2641 }
mpangfaa7bae2019-01-15 16:38:13 +08002642 ALOGE("%s: Failed to init hardware info, snd_card_num:%d", __func__, snd_card_num);
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002643
Dhananjay Kumara7e27832017-07-11 15:40:39 +05302644 free(snd_card_name);
2645 snd_card_name = NULL;
2646
2647 mixer_close(mixer);
2648 mixer = NULL;
2649 }
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002650 if (snd_card_name)
2651 free(snd_card_name);
Ashish Jain30ae8942017-11-05 17:21:23 +05302652
Dhananjay Kumara7e27832017-07-11 15:40:39 +05302653 if (hw_info)
2654 hw_info_deinit(hw_info);
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002655
mpangfaa7bae2019-01-15 16:38:13 +08002656 if (retry_num >= RETRY_NUMBER) {
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002657 ALOGE("%s: Unable to find correct sound card, aborting.", __func__);
2658 return -1;
2659 }
2660
Soumya Managoli9fee7c62018-04-06 16:21:50 +05302661 if (mixer)
2662 *mixer_handle = mixer;
2663
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002664 return snd_card_num;
2665}
Naresh Tanniru6160c712017-04-17 15:43:48 +05302666
Soumya Managoli9fee7c62018-04-06 16:21:50 +05302667void audio_extn_utils_close_snd_mixer(struct mixer *mixer)
2668{
2669 if (mixer)
2670 mixer_close(mixer);
2671}
2672
Naresh Tanniru6160c712017-04-17 15:43:48 +05302673#ifdef SNDRV_COMPRESS_ENABLE_ADJUST_SESSION_CLOCK
2674int audio_extn_utils_compress_enable_drift_correction(
2675 struct stream_out *out,
2676 struct audio_out_enable_drift_correction *drift)
2677{
2678 struct snd_compr_metadata metadata;
2679 int ret = -EINVAL;
2680
2681 if(drift == NULL) {
2682 ALOGE("%s:: Invalid param", __func__);
2683 goto exit;
2684 }
2685
2686 ALOGD("%s:: drift enable %d", __func__,drift->enable);
2687
2688 if (!is_offload_usecase(out->usecase)) {
2689 ALOGE("%s:: not supported for non offload session", __func__);
2690 goto exit;
2691 }
2692
2693 if (!out->compr) {
2694 ALOGW("%s:: offload session not yet opened,"
2695 "start delay will be configure later", __func__);
2696 goto exit;
2697 }
2698
2699 metadata.key = SNDRV_COMPRESS_ENABLE_ADJUST_SESSION_CLOCK;
2700 metadata.value[0] = drift->enable;
2701 out->drift_correction_enabled = drift->enable;
2702
2703 ret = compress_set_metadata(out->compr, &metadata);
2704 if(ret) {
2705 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2706 out->drift_correction_enabled = false;
2707 }
2708
2709exit:
2710 return ret;
2711}
2712#else
2713int audio_extn_utils_compress_enable_drift_correction(
2714 struct stream_out *out __unused,
2715 struct audio_out_enable_drift_correction *drift __unused)
2716{
2717 ALOGD("%s:: configuring drift enablement not supported", __func__);
2718 return 0;
2719}
2720#endif
2721
2722#ifdef SNDRV_COMPRESS_ADJUST_SESSION_CLOCK
2723int audio_extn_utils_compress_correct_drift(
2724 struct stream_out *out,
2725 struct audio_out_correct_drift *drift_param)
2726{
2727 struct snd_compr_metadata metadata;
2728 int ret = -EINVAL;
2729
2730 if (drift_param == NULL) {
2731 ALOGE("%s:: Invalid drift_param", __func__);
2732 goto exit;
2733 }
2734
2735 ALOGD("%s:: adjust time 0x%"PRIx64" ", __func__,
2736 drift_param->adjust_time);
2737
2738 if (!is_offload_usecase(out->usecase)) {
2739 ALOGE("%s:: not supported for non offload session", __func__);
2740 goto exit;
2741 }
2742
2743 if (!out->compr) {
2744 ALOGW("%s:: offload session not yet opened", __func__);
2745 goto exit;
2746 }
2747
2748 if (!out->drift_correction_enabled) {
2749 ALOGE("%s:: drift correction not enabled", __func__);
2750 goto exit;
2751 }
2752
2753 metadata.key = SNDRV_COMPRESS_ADJUST_SESSION_CLOCK;
2754 metadata.value[0] = 0xFFFFFFFF & drift_param->adjust_time; /* lsb */
2755 metadata.value[1] = \
2756 (0xFFFFFFFF00000000 & drift_param->adjust_time) >> 32; /* msb*/
2757
2758 ret = compress_set_metadata(out->compr, &metadata);
2759 if(ret)
2760 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2761exit:
2762 return ret;
2763}
2764#else
2765int audio_extn_utils_compress_correct_drift(
2766 struct stream_out *out __unused,
2767 struct audio_out_correct_drift *drift_param __unused)
2768{
2769 ALOGD("%s:: setting adjust clock not supported", __func__);
2770 return 0;
2771}
2772#endif
Naresh Tanniru29bce4e2017-04-27 17:54:30 +05302773
2774int audio_extn_utils_set_channel_map(
2775 struct stream_out *out,
2776 struct audio_out_channel_map_param *channel_map_param)
2777{
2778 int ret = -EINVAL, i = 0;
2779 int channels = audio_channel_count_from_out_mask(out->channel_mask);
2780
2781 if (channel_map_param == NULL) {
2782 ALOGE("%s:: Invalid channel_map", __func__);
2783 goto exit;
2784 }
2785
2786 if (channel_map_param->channels != channels) {
2787 ALOGE("%s:: Channels(%d) does not match stream channels(%d)",
2788 __func__, channel_map_param->channels, channels);
2789 goto exit;
2790 }
2791
2792 for ( i = 0; i < channels; i++) {
2793 ALOGV("%s:: channel_map[%d]- %d", __func__, i, channel_map_param->channel_map[i]);
2794 out->channel_map_param.channel_map[i] = channel_map_param->channel_map[i];
2795 }
2796 ret = 0;
2797exit:
2798 return ret;
2799}
Varun Balaraje49253e2017-07-06 19:48:56 +05302800
2801int audio_extn_utils_set_pan_scale_params(
2802 struct stream_out *out,
2803 struct mix_matrix_params *mm_params)
2804{
2805 int ret = -EINVAL, i = 0, j = 0;
2806
Varun Balaraj003ee752017-08-07 17:54:55 +05302807 if (mm_params == NULL || out == NULL) {
2808 ALOGE("%s:: Invalid mix matrix or out param", __func__);
Varun Balaraje49253e2017-07-06 19:48:56 +05302809 goto exit;
2810 }
2811
2812 if (mm_params->num_output_channels > MAX_CHANNELS_SUPPORTED ||
2813 mm_params->num_output_channels <= 0 ||
2814 mm_params->num_input_channels > MAX_CHANNELS_SUPPORTED ||
2815 mm_params->num_input_channels <= 0)
2816 goto exit;
2817
2818 out->pan_scale_params.num_output_channels = mm_params->num_output_channels;
2819 out->pan_scale_params.num_input_channels = mm_params->num_input_channels;
2820 out->pan_scale_params.has_output_channel_map =
2821 mm_params->has_output_channel_map;
2822 for (i = 0; i < mm_params->num_output_channels; i++)
2823 out->pan_scale_params.output_channel_map[i] =
2824 mm_params->output_channel_map[i];
2825
2826 out->pan_scale_params.has_input_channel_map =
2827 mm_params->has_input_channel_map;
2828 for (i = 0; i < mm_params->num_input_channels; i++)
2829 out->pan_scale_params.input_channel_map[i] =
2830 mm_params->input_channel_map[i];
2831
2832 out->pan_scale_params.has_mixer_coeffs = mm_params->has_mixer_coeffs;
2833 for (i = 0; i < mm_params->num_output_channels; i++)
2834 for (j = 0; j < mm_params->num_input_channels; j++) {
2835 //Convert the channel coefficient gains in Q14 format
2836 out->pan_scale_params.mixer_coeffs[i][j] =
2837 mm_params->mixer_coeffs[i][j] * (2 << 13);
2838 }
2839
2840 ret = platform_set_stream_pan_scale_params(out->dev->platform,
2841 out->pcm_device_id,
2842 out->pan_scale_params);
2843
2844exit:
2845 return ret;
2846}
2847
2848int audio_extn_utils_set_downmix_params(
2849 struct stream_out *out,
2850 struct mix_matrix_params *mm_params)
2851{
2852 int ret = -EINVAL, i = 0, j = 0;
2853 struct audio_usecase *usecase = NULL;
2854
Aniket Kumar Lataf9f246e2017-09-15 15:20:16 -07002855 if (mm_params == NULL || out == NULL) {
Varun Balaraj003ee752017-08-07 17:54:55 +05302856 ALOGE("%s:: Invalid mix matrix or out param", __func__);
Varun Balaraje49253e2017-07-06 19:48:56 +05302857 goto exit;
2858 }
2859
2860 if (mm_params->num_output_channels > MAX_CHANNELS_SUPPORTED ||
2861 mm_params->num_output_channels <= 0 ||
2862 mm_params->num_input_channels > MAX_CHANNELS_SUPPORTED ||
2863 mm_params->num_input_channels <= 0)
2864 goto exit;
2865
2866 usecase = get_usecase_from_list(out->dev, out->usecase);
Varun Balaraj003ee752017-08-07 17:54:55 +05302867 if (!usecase) {
2868 ALOGE("%s: Get usecase list failed!", __func__);
Aniket Kumar Lataf9f246e2017-09-15 15:20:16 -07002869 goto exit;
2870 }
Varun Balaraje49253e2017-07-06 19:48:56 +05302871 out->downmix_params.num_output_channels = mm_params->num_output_channels;
2872 out->downmix_params.num_input_channels = mm_params->num_input_channels;
2873
2874 out->downmix_params.has_output_channel_map =
2875 mm_params->has_output_channel_map;
2876 for (i = 0; i < mm_params->num_output_channels; i++) {
2877 out->downmix_params.output_channel_map[i] =
2878 mm_params->output_channel_map[i];
2879 }
2880
2881 out->downmix_params.has_input_channel_map =
2882 mm_params->has_input_channel_map;
2883 for (i = 0; i < mm_params->num_input_channels; i++)
2884 out->downmix_params.input_channel_map[i] =
2885 mm_params->input_channel_map[i];
2886
2887 out->downmix_params.has_mixer_coeffs = mm_params->has_mixer_coeffs;
2888 for (i = 0; i < mm_params->num_output_channels; i++)
Nikhil Latukar2e6f6242017-08-15 13:37:07 +05302889 for (j = 0; j < mm_params->num_input_channels; j++) {
2890 //Convert the channel coefficient gains in Q14 format
Varun Balaraje49253e2017-07-06 19:48:56 +05302891 out->downmix_params.mixer_coeffs[i][j] =
Nikhil Latukar2e6f6242017-08-15 13:37:07 +05302892 mm_params->mixer_coeffs[i][j] * (2 << 13);
2893 }
Varun Balaraje49253e2017-07-06 19:48:56 +05302894
2895 ret = platform_set_stream_downmix_params(out->dev->platform,
2896 out->pcm_device_id,
2897 usecase->out_snd_device,
2898 out->downmix_params);
2899
2900exit:
2901 return ret;
2902}
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05302903
2904bool audio_extn_utils_is_dolby_format(audio_format_t format)
2905{
2906 if (format == AUDIO_FORMAT_AC3 ||
2907 format == AUDIO_FORMAT_E_AC3 ||
2908 format == AUDIO_FORMAT_E_AC3_JOC)
2909 return true;
2910 else
2911 return false;
2912}
2913
`Deeraj Soman676c2702017-09-18 19:25:53 +05302914int audio_extn_utils_get_bit_width_from_string(const char *id_string)
2915{
2916 int i;
2917 const mixer_config_lookup mixer_bitwidth_config[] = {{"S24_3LE", 24},
2918 {"S32_LE", 32},
2919 {"S24_LE", 24},
2920 {"S16_LE", 16}};
2921 int num_configs = sizeof(mixer_bitwidth_config) / sizeof(mixer_bitwidth_config[0]);
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05302922
`Deeraj Soman676c2702017-09-18 19:25:53 +05302923 for (i = 0; i < num_configs; i++) {
2924 if (!strcmp(id_string, mixer_bitwidth_config[i].id_string))
2925 return mixer_bitwidth_config[i].value;
2926 }
2927
2928 return -EINVAL;
2929}
2930
2931int audio_extn_utils_get_sample_rate_from_string(const char *id_string)
2932{
2933 int i;
2934 const mixer_config_lookup mixer_samplerate_config[] = {{"KHZ_32", 32000},
2935 {"KHZ_48", 48000},
2936 {"KHZ_96", 96000},
2937 {"KHZ_144", 144000},
2938 {"KHZ_192", 192000},
2939 {"KHZ_384", 384000},
2940 {"KHZ_44P1", 44100},
2941 {"KHZ_88P2", 88200},
2942 {"KHZ_176P4", 176400},
2943 {"KHZ_352P8", 352800}};
2944 int num_configs = sizeof(mixer_samplerate_config) / sizeof(mixer_samplerate_config[0]);
2945
2946 for (i = 0; i < num_configs; i++) {
2947 if (!strcmp(id_string, mixer_samplerate_config[i].id_string))
2948 return mixer_samplerate_config[i].value;
2949 }
2950
2951 return -EINVAL;
2952}
2953
2954int audio_extn_utils_get_channels_from_string(const char *id_string)
2955{
2956 int i;
2957 const mixer_config_lookup mixer_channels_config[] = {{"One", 1},
2958 {"Two", 2},
2959 {"Three",3},
2960 {"Four", 4},
2961 {"Five", 5},
2962 {"Six", 6},
2963 {"Seven", 7},
2964 {"Eight", 8}};
2965 int num_configs = sizeof(mixer_channels_config) / sizeof(mixer_channels_config[0]);
2966
2967 for (i = 0; i < num_configs; i++) {
2968 if (!strcmp(id_string, mixer_channels_config[i].id_string))
2969 return mixer_channels_config[i].value;
2970 }
2971
2972 return -EINVAL;
2973}
Surendar karka30569792018-05-08 12:02:21 +05302974
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08002975void audio_extn_utils_release_snd_device(snd_device_t snd_device)
2976{
2977 audio_extn_dev_arbi_release(snd_device);
2978 audio_extn_sound_trigger_update_device_status(snd_device,
2979 ST_EVENT_SND_DEVICE_FREE);
2980 audio_extn_listen_update_device_status(snd_device,
2981 LISTEN_EVENT_SND_DEVICE_FREE);
2982}
2983
Surendar karka30569792018-05-08 12:02:21 +05302984int audio_extn_utils_get_license_params
2985(
2986const struct audio_device *adev,
2987struct audio_license_params *license_params
2988)
2989{
2990 if(!license_params)
2991 return -EINVAL;
2992 return platform_get_license_by_product(adev->platform, (const char*)license_params->product, &license_params->key, license_params->license);
2993}
2994
Aalique Grahame22e49102018-12-18 14:23:57 -08002995int audio_extn_utils_send_app_type_gain(struct audio_device *adev,
2996 int app_type,
2997 int *gain)
2998{
2999 int gain_cfg[4];
3000 const char *mixer_ctl_name = "App Type Gain";
3001 struct mixer_ctl *ctl;
3002 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
3003 if (!ctl) {
3004 ALOGE("%s: Could not get volume ctl mixer %s", __func__,
3005 mixer_ctl_name);
3006 return -EINVAL;
3007 }
3008 gain_cfg[0] = 0;
3009 gain_cfg[1] = app_type;
3010 gain_cfg[2] = gain[0];
3011 gain_cfg[3] = gain[1];
3012 ALOGV("%s app_type %d l(%d) r(%d)", __func__, app_type, gain[0], gain[1]);
3013 return mixer_ctl_set_array(ctl, gain_cfg,
3014 sizeof(gain_cfg)/sizeof(gain_cfg[0]));
3015}
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -07003016
3017int audio_extn_utils_is_vendor_enhanced_fwk()
3018{
3019 static int is_running_with_enhanced_fwk = -EINVAL;
3020
3021 if (is_running_with_enhanced_fwk == -EINVAL) {
3022 vndk_fwk_lib_handle = dlopen(VNDK_FWK_LIB_PATH, RTLD_NOW);
3023 if (vndk_fwk_lib_handle != NULL) {
3024 vndk_fwk_isVendorEnhancedFwk = (vndk_fwk_isVendorEnhancedFwk_t)
3025 dlsym(vndk_fwk_lib_handle, "isRunningWithVendorEnhancedFramework");
3026 if (vndk_fwk_isVendorEnhancedFwk == NULL) {
3027 ALOGW("%s: dlsym failed, defaulting to enhanced_fwk configuration",
3028 __func__);
3029 is_running_with_enhanced_fwk = 1;
3030 } else {
3031 is_running_with_enhanced_fwk = vndk_fwk_isVendorEnhancedFwk();
3032 }
3033 dlclose(vndk_fwk_lib_handle);
3034 vndk_fwk_lib_handle = NULL;
3035 } else {
3036 ALOGW("%s: VNDK_FWK_LIB not found, setting stock configuration", __func__);
3037 is_running_with_enhanced_fwk = 0;
3038 }
3039 ALOGV("%s: vndk_fwk_isVendorEnhancedFwk=%d", __func__, is_running_with_enhanced_fwk);
3040 }
3041
3042 return is_running_with_enhanced_fwk;
3043}
Deeraj Soman14230922019-01-30 16:39:30 +05303044
Deeraj Somanfa377bf2019-02-06 12:57:59 +05303045int audio_extn_utils_get_perf_mode_flag(void)
3046{
3047#ifdef COMPRESSED_PERF_MODE_FLAG
3048 return COMPRESSED_PERF_MODE_FLAG;
3049#else
3050 return 0;
3051#endif
3052}
3053
Deeraj Soman14230922019-01-30 16:39:30 +05303054size_t audio_extn_utils_get_input_buffer_size(uint32_t sample_rate,
3055 audio_format_t format,
3056 int channel_count,
3057 int64_t duration_ms,
3058 bool is_low_latency)
3059{
3060 size_t size = 0;
3061 size_t capture_duration = AUDIO_CAPTURE_PERIOD_DURATION_MSEC;
3062 uint32_t bytes_per_period_sample = 0;
3063
3064
3065 if (audio_extn_utils_check_input_parameters(sample_rate, format, channel_count) != 0)
3066 return 0;
3067
3068 if (duration_ms >= MIN_OFFLOAD_BUFFER_DURATION_MS && duration_ms <= MAX_OFFLOAD_BUFFER_DURATION_MS)
3069 capture_duration = duration_ms;
3070
3071 size = (sample_rate * capture_duration) / 1000;
3072 if (is_low_latency)
3073 size = LOW_LATENCY_CAPTURE_PERIOD_SIZE;
3074
3075
3076 bytes_per_period_sample = audio_bytes_per_sample(format) * channel_count;
3077 size *= bytes_per_period_sample;
3078
3079 /* make sure the size is multiple of 32 bytes and additionally multiple of
3080 * the frame_size (required for 24bit samples and non-power-of-2 channel counts)
3081 * At 48 kHz mono 16-bit PCM:
3082 * 5.000 ms = 240 frames = 15*16*1*2 = 480, a whole multiple of 32 (15)
3083 * 3.333 ms = 160 frames = 10*16*1*2 = 320, a whole multiple of 32 (10)
3084 *
3085 * The loop reaches result within 32 iterations, as initial size is
3086 * already a multiple of frame_size
3087 */
3088 size = audio_extn_utils_nearest_multiple(size, audio_extn_utils_lcm(32, bytes_per_period_sample));
3089
3090 return size;
3091}