blob: 4f37ac3066d25cb123b2262ce9683726186ad5e9 [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#ifdef AUDIO_EXTN_AUTO_HAL_ENABLED
150 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_MEDIA),
151 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_SYS_NOTIFICATION),
152 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_NAV_GUIDANCE),
153 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_PHONE),
154#endif
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530155 STRING_TO_ENUM(AUDIO_INPUT_FLAG_NONE),
156 STRING_TO_ENUM(AUDIO_INPUT_FLAG_FAST),
157 STRING_TO_ENUM(AUDIO_INPUT_FLAG_HW_HOTWORD),
158 STRING_TO_ENUM(AUDIO_INPUT_FLAG_RAW),
159 STRING_TO_ENUM(AUDIO_INPUT_FLAG_SYNC),
Dhananjay Kumaree4d2002016-10-25 18:02:58 +0530160 STRING_TO_ENUM(AUDIO_INPUT_FLAG_TIMESTAMP),
Dhananjay Kumar704ce6f2017-09-28 22:08:00 +0530161 STRING_TO_ENUM(AUDIO_INPUT_FLAG_COMPRESS),
Ralf Herzaec80262018-07-03 07:08:49 +0200162 STRING_TO_ENUM(AUDIO_INPUT_FLAG_PASSTHROUGH),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700163};
164
165const struct string_to_enum s_format_name_to_enum_table[] = {
Ashish Jain83a6cc22016-06-28 14:34:17 +0530166 STRING_TO_ENUM(AUDIO_FORMAT_PCM_8_BIT),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700167 STRING_TO_ENUM(AUDIO_FORMAT_PCM_16_BIT),
Ashish Jain5106d362016-05-11 19:23:33 +0530168 STRING_TO_ENUM(AUDIO_FORMAT_PCM_24_BIT_PACKED),
169 STRING_TO_ENUM(AUDIO_FORMAT_PCM_8_24_BIT),
Ashish Jainf1eaa582016-05-23 20:54:24 +0530170 STRING_TO_ENUM(AUDIO_FORMAT_PCM_32_BIT),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700171 STRING_TO_ENUM(AUDIO_FORMAT_MP3),
172 STRING_TO_ENUM(AUDIO_FORMAT_AAC),
173 STRING_TO_ENUM(AUDIO_FORMAT_VORBIS),
Mingming Yinae3530f2014-07-03 16:50:18 -0700174 STRING_TO_ENUM(AUDIO_FORMAT_AMR_NB),
175 STRING_TO_ENUM(AUDIO_FORMAT_AMR_WB),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700176 STRING_TO_ENUM(AUDIO_FORMAT_AC3),
Mingming Yinae3530f2014-07-03 16:50:18 -0700177 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700178 STRING_TO_ENUM(AUDIO_FORMAT_DTS),
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530179 STRING_TO_ENUM(AUDIO_FORMAT_DTS_HD),
Ben Romberger1aaaf862017-04-06 17:49:46 -0700180 STRING_TO_ENUM(AUDIO_FORMAT_DOLBY_TRUEHD),
Naresh Tanniru928f0862017-04-07 16:44:23 -0700181 STRING_TO_ENUM(AUDIO_FORMAT_IEC61937),
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530182 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3_JOC),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700183 STRING_TO_ENUM(AUDIO_FORMAT_WMA),
184 STRING_TO_ENUM(AUDIO_FORMAT_WMA_PRO),
185 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADIF),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700186 STRING_TO_ENUM(AUDIO_FORMAT_AMR_WB_PLUS),
187 STRING_TO_ENUM(AUDIO_FORMAT_EVRC),
188 STRING_TO_ENUM(AUDIO_FORMAT_EVRCB),
189 STRING_TO_ENUM(AUDIO_FORMAT_EVRCWB),
190 STRING_TO_ENUM(AUDIO_FORMAT_QCELP),
191 STRING_TO_ENUM(AUDIO_FORMAT_MP2),
192 STRING_TO_ENUM(AUDIO_FORMAT_EVRCNW),
Amit Shekhar6f461b12014-08-01 14:52:58 -0700193 STRING_TO_ENUM(AUDIO_FORMAT_FLAC),
Satya Krishna Pindiproli70471602015-04-24 19:12:43 +0530194 STRING_TO_ENUM(AUDIO_FORMAT_ALAC),
195 STRING_TO_ENUM(AUDIO_FORMAT_APE),
Alexy Josephcd8eaed2014-12-11 12:46:53 -0800196 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LC),
197 STRING_TO_ENUM(AUDIO_FORMAT_AAC_HE_V1),
198 STRING_TO_ENUM(AUDIO_FORMAT_AAC_HE_V2),
Manish Dewangana6fc5442015-08-24 20:30:31 +0530199 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS),
Ashish Jaine513a872015-11-19 17:00:56 +0530200 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_LC),
201 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_HE_V1),
202 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_HE_V2),
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +0530203 STRING_TO_ENUM(AUDIO_FORMAT_DSD),
Arun Kumar Dasari3b174182016-12-27 13:01:14 +0530204 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM),
205 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM_LC),
206 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM_HE_V1),
207 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM_HE_V2),
Dhanalakshmi Siddani18737932016-11-29 17:33:17 +0530208 STRING_TO_ENUM(AUDIO_FORMAT_APTX),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700209};
210
Manish Dewangan3ccdea52017-02-13 19:31:54 +0530211/* payload structure avt_device drift query */
212struct audio_avt_device_drift_stats {
213 uint32_t minor_version;
Manish Dewangan338c50a2017-09-12 15:22:03 +0530214
Manish Dewangan3ccdea52017-02-13 19:31:54 +0530215 /* Indicates the device interface direction as either
216 * source (Tx) or sink (Rx).
217 */
218 uint16_t device_direction;
Manish Dewangan338c50a2017-09-12 15:22:03 +0530219
220 /* Reference timer for drift accumulation and time stamp information.
221 * currently it only support AFE_REF_TIMER_TYPE_AVTIMER
222 */
223 uint16_t reference_timer;
Manish Dewangan3ccdea52017-02-13 19:31:54 +0530224 struct audio_avt_device_drift_param drift_param;
Manish Dewangan338c50a2017-09-12 15:22:03 +0530225} __attribute__((packed));
Manish Dewangan3ccdea52017-02-13 19:31:54 +0530226
Ben Rombergera04fabc2014-11-14 12:16:03 -0800227static char bTable[BASE_TABLE_SIZE] = {
228 'A','B','C','D','E','F','G','H','I','J','K','L',
229 'M','N','O','P','Q','R','S','T','U','V','W','X',
230 'Y','Z','a','b','c','d','e','f','g','h','i','j',
231 'k','l','m','n','o','p','q','r','s','t','u','v',
232 'w','x','y','z','0','1','2','3','4','5','6','7',
233 '8','9','+','/'
234};
235
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700236static uint32_t string_to_enum(const struct string_to_enum *table, size_t size,
237 const char *name)
238{
239 size_t i;
240 for (i = 0; i < size; i++) {
241 if (strcmp(table[i].name, name) == 0) {
242 ALOGV("%s found %s", __func__, table[i].name);
243 return table[i].value;
244 }
245 }
246 return 0;
247}
248
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530249static audio_io_flags_t parse_flag_names(char *name)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700250{
251 uint32_t flag = 0;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530252 audio_io_flags_t io_flags;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800253 char *last_r;
254 char *flag_name = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700255 while (flag_name != NULL) {
256 if (strlen(flag_name) != 0) {
257 flag |= string_to_enum(s_flag_name_to_enum_table,
258 ARRAY_SIZE(s_flag_name_to_enum_table),
259 flag_name);
260 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800261 flag_name = strtok_r(NULL, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700262 }
263
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800264 ALOGV("parse_flag_names: flag - %x", flag);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530265 io_flags.in_flags = (audio_input_flags_t)flag;
266 io_flags.out_flags = (audio_output_flags_t)flag;
267 return io_flags;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700268}
269
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530270static void parse_format_names(char *name, struct streams_io_cfg *s_info)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700271{
272 struct stream_format *sf_info = NULL;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800273 char *last_r;
274 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700275
276 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG) == 0)
277 return;
278
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530279 list_init(&s_info->format_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700280 while (str != NULL) {
281 audio_format_t format = (audio_format_t)string_to_enum(s_format_name_to_enum_table,
282 ARRAY_SIZE(s_format_name_to_enum_table), str);
283 ALOGV("%s: format - %d", __func__, format);
284 if (format != 0) {
285 sf_info = (struct stream_format *)calloc(1, sizeof(struct stream_format));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700286 if (sf_info == NULL)
287 break; /* return whatever was parsed */
288
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700289 sf_info->format = format;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530290 list_add_tail(&s_info->format_list, &sf_info->list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700291 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800292 str = strtok_r(NULL, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700293 }
294}
295
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530296static void parse_sample_rate_names(char *name, struct streams_io_cfg *s_info)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700297{
Amit Shekhar6f461b12014-08-01 14:52:58 -0700298 struct stream_sample_rate *ss_info = NULL;
299 uint32_t sample_rate = 48000;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800300 char *last_r;
301 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700302
Amit Shekhar6f461b12014-08-01 14:52:58 -0700303 if (str != NULL && 0 == strcmp(str, DYNAMIC_VALUE_TAG))
304 return;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700305
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530306 list_init(&s_info->sample_rate_list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700307 while (str != NULL) {
308 sample_rate = (uint32_t)strtol(str, (char **)NULL, 10);
309 ALOGV("%s: sample_rate - %d", __func__, sample_rate);
310 if (0 != sample_rate) {
311 ss_info = (struct stream_sample_rate *)calloc(1, sizeof(struct stream_sample_rate));
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800312 if (!ss_info) {
313 ALOGE("%s: memory allocation failure", __func__);
314 return;
315 }
Amit Shekhar6f461b12014-08-01 14:52:58 -0700316 ss_info->sample_rate = sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530317 list_add_tail(&s_info->sample_rate_list, &ss_info->list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700318 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800319 str = strtok_r(NULL, "|", &last_r);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700320 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700321}
322
323static int parse_bit_width_names(char *name)
324{
325 int bit_width = 16;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800326 char *last_r;
327 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700328
329 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG))
330 bit_width = (int)strtol(str, (char **)NULL, 10);
331
332 ALOGV("%s: bit_width - %d", __func__, bit_width);
333 return bit_width;
334}
335
336static int parse_app_type_names(void *platform, char *name)
337{
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700338 int app_type = platform_get_default_app_type(platform);
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800339 char *last_r;
340 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700341
342 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG))
343 app_type = (int)strtol(str, (char **)NULL, 10);
344
345 ALOGV("%s: app_type - %d", __func__, app_type);
346 return app_type;
347}
348
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530349static void update_streams_cfg_list(cnode *root, void *platform,
350 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700351{
352 cnode *node = root->first_child;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530353 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700354
355 ALOGV("%s", __func__);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530356 s_info = (struct streams_io_cfg *)calloc(1, sizeof(struct streams_io_cfg));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700357
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530358 if (!s_info) {
359 ALOGE("failed to allocate mem for s_info list element");
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700360 return;
361 }
362
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700363 while (node) {
364 if (strcmp(node->name, FLAGS_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530365 s_info->flags = parse_flag_names((char *)node->value);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530366 } else if (strcmp(node->name, PROFILES_TAG) == 0) {
367 strlcpy(s_info->profile, (char *)node->value, sizeof(s_info->profile));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700368 } else if (strcmp(node->name, FORMATS_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530369 parse_format_names((char *)node->value, s_info);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700370 } else if (strcmp(node->name, SAMPLING_RATES_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530371 s_info->app_type_cfg.sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
372 parse_sample_rate_names((char *)node->value, s_info);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700373 } else if (strcmp(node->name, BIT_WIDTH_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530374 s_info->app_type_cfg.bit_width = parse_bit_width_names((char *)node->value);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700375 } else if (strcmp(node->name, APP_TYPE_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530376 s_info->app_type_cfg.app_type = parse_app_type_names(platform, (char *)node->value);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700377 }
378 node = node->next;
379 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530380 list_add_tail(streams_cfg_list, &s_info->list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700381}
382
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530383static void load_cfg_list(cnode *root, void *platform,
384 struct listnode *streams_output_cfg_list,
385 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700386{
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530387 cnode *node = NULL;
388
389 node = config_find(root, OUTPUTS_TAG);
390 if (node != NULL) {
391 node = node->first_child;
392 while (node) {
393 ALOGV("%s: loading output %s", __func__, node->name);
394 update_streams_cfg_list(node, platform, streams_output_cfg_list);
395 node = node->next;
396 }
397 } else {
398 ALOGI("%s: could not load output, node is NULL", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700399 }
400
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530401 node = config_find(root, INPUTS_TAG);
402 if (node != NULL) {
403 node = node->first_child;
404 while (node) {
405 ALOGV("%s: loading input %s", __func__, node->name);
406 update_streams_cfg_list(node, platform, streams_input_cfg_list);
407 node = node->next;
408 }
409 } else {
410 ALOGI("%s: could not load input, node is NULL", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700411 }
412}
413
414static void send_app_type_cfg(void *platform, struct mixer *mixer,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530415 struct listnode *streams_output_cfg_list,
416 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700417{
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530418 size_t app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT] = {0};
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700419 int length = 0, i, num_app_types = 0;
420 struct listnode *node;
421 bool update;
422 struct mixer_ctl *ctl = NULL;
423 const char *mixer_ctl_name = "App Type Config";
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530424 struct streams_io_cfg *s_info = NULL;
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800425 uint32_t target_bit_width = 0;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700426
427 if (!mixer) {
428 ALOGE("%s: mixer is null",__func__);
429 return;
430 }
431 ctl = mixer_get_ctl_by_name(mixer, mixer_ctl_name);
432 if (!ctl) {
433 ALOGE("%s: Could not get ctl for mixer cmd - %s",__func__, mixer_ctl_name);
434 return;
435 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530436 app_type_cfg[length++] = num_app_types;
437
438 if (list_empty(streams_output_cfg_list)) {
439 app_type_cfg[length++] = platform_get_default_app_type_v2(platform, PCM_PLAYBACK);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700440 app_type_cfg[length++] = 48000;
441 app_type_cfg[length++] = 16;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530442 num_app_types += 1;
443 }
444 if (list_empty(streams_input_cfg_list)) {
445 app_type_cfg[length++] = platform_get_default_app_type_v2(platform, PCM_CAPTURE);
446 app_type_cfg[length++] = 48000;
447 app_type_cfg[length++] = 16;
448 num_app_types += 1;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700449 }
450
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800451 /* get target bit width for ADM enforce mode */
452 target_bit_width = adev_get_dsp_bit_width_enforce_mode();
453
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700454 list_for_each(node, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530455 s_info = node_to_item(node, struct streams_io_cfg, list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700456 update = true;
457 for (i=0; i<length; i=i+3) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530458 if (app_type_cfg[i+1] == 0)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700459 break;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530460 else if (app_type_cfg[i+1] == (size_t)s_info->app_type_cfg.app_type) {
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530461 if (app_type_cfg[i+2] < (size_t)s_info->app_type_cfg.sample_rate)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530462 app_type_cfg[i+2] = s_info->app_type_cfg.sample_rate;
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530463 if (app_type_cfg[i+3] < (size_t)s_info->app_type_cfg.bit_width)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530464 app_type_cfg[i+3] = s_info->app_type_cfg.bit_width;
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800465 /* ADM bit width = max(enforce_bit_width, bit_width from s_info */
466 if (audio_extn_is_dsp_bit_width_enforce_mode_supported(s_info->flags.out_flags) &&
467 (target_bit_width > app_type_cfg[i+3]))
468 app_type_cfg[i+3] = target_bit_width;
469
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700470 update = false;
471 break;
472 }
473 }
474 if (update && ((length + 3) <= MAX_LENGTH_MIXER_CONTROL_IN_INT)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530475 num_app_types += 1;
476 app_type_cfg[length++] = s_info->app_type_cfg.app_type;
477 app_type_cfg[length++] = s_info->app_type_cfg.sample_rate;
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800478 app_type_cfg[length] = s_info->app_type_cfg.bit_width;
479 if (audio_extn_is_dsp_bit_width_enforce_mode_supported(s_info->flags.out_flags) &&
480 (target_bit_width > app_type_cfg[length]))
481 app_type_cfg[length] = target_bit_width;
482
483 length++;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530484 }
485 }
486 list_for_each(node, streams_input_cfg_list) {
487 s_info = node_to_item(node, struct streams_io_cfg, list);
488 update = true;
489 for (i=0; i<length; i=i+3) {
490 if (app_type_cfg[i+1] == 0)
491 break;
492 else if (app_type_cfg[i+1] == (size_t)s_info->app_type_cfg.app_type) {
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530493 if (app_type_cfg[i+2] < (size_t)s_info->app_type_cfg.sample_rate)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530494 app_type_cfg[i+2] = s_info->app_type_cfg.sample_rate;
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530495 if (app_type_cfg[i+3] < (size_t)s_info->app_type_cfg.bit_width)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530496 app_type_cfg[i+3] = s_info->app_type_cfg.bit_width;
497 update = false;
498 break;
499 }
500 }
501 if (update && ((length + 3) <= MAX_LENGTH_MIXER_CONTROL_IN_INT)) {
502 num_app_types += 1;
503 app_type_cfg[length++] = s_info->app_type_cfg.app_type;
504 app_type_cfg[length++] = s_info->app_type_cfg.sample_rate;
505 app_type_cfg[length++] = s_info->app_type_cfg.bit_width;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700506 }
507 }
508 ALOGV("%s: num_app_types: %d", __func__, num_app_types);
509 if (num_app_types) {
510 app_type_cfg[0] = num_app_types;
511 mixer_ctl_set_array(ctl, app_type_cfg, length);
512 }
513}
514
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530515void audio_extn_utils_update_streams_cfg_lists(void *platform,
516 struct mixer *mixer,
517 struct listnode *streams_output_cfg_list,
518 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700519{
520 cnode *root;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530521 char *data = NULL;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700522
523 ALOGV("%s", __func__);
524 list_init(streams_output_cfg_list);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530525 list_init(streams_input_cfg_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700526
527 root = config_node("", "");
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700528 if (root == NULL) {
529 ALOGE("cfg_list, NULL config root");
530 return;
531 }
532
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530533 data = (char *)load_file(AUDIO_IO_POLICY_VENDOR_CONFIG_FILE, NULL);
534 if (data == NULL) {
535 ALOGD("%s: failed to open io config file(%s), trying older config file",
536 __func__, AUDIO_IO_POLICY_VENDOR_CONFIG_FILE);
537 data = (char *)load_file(AUDIO_OUTPUT_POLICY_VENDOR_CONFIG_FILE, NULL);
538 if (data == NULL) {
539 send_app_type_cfg(platform, mixer,
540 streams_output_cfg_list,
541 streams_input_cfg_list);
542 ALOGE("%s: could not load io policy config!", __func__);
yidongh6eb4f752017-04-19 18:20:57 +0800543 free(root);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530544 return;
545 }
546 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700547
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530548 config_load(root, data);
549 load_cfg_list(root, platform, streams_output_cfg_list,
550 streams_input_cfg_list);
551
552 send_app_type_cfg(platform, mixer, streams_output_cfg_list,
553 streams_input_cfg_list);
Alexy Josephaee4fdd2016-01-29 13:02:07 -0800554
555 config_free(root);
yidongh6eb4f752017-04-19 18:20:57 +0800556 free(root);
Alexy Josephaee4fdd2016-01-29 13:02:07 -0800557 free(data);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700558}
559
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530560static void audio_extn_utils_dump_streams_cfg_list(
561 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700562{
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700563 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530564 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700565 struct stream_format *sf_info;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700566 struct stream_sample_rate *ss_info;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530567
568 list_for_each(node_i, streams_cfg_list) {
569 s_info = node_to_item(node_i, struct streams_io_cfg, list);
570 ALOGV("%s: flags-%d, sample_rate-%d, bit_width-%d, app_type-%d",
571 __func__, s_info->flags.out_flags, s_info->app_type_cfg.sample_rate,
572 s_info->app_type_cfg.bit_width, s_info->app_type_cfg.app_type);
573 list_for_each(node_j, &s_info->format_list) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700574 sf_info = node_to_item(node_j, struct stream_format, list);
575 ALOGV("format-%x", sf_info->format);
576 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530577 list_for_each(node_j, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700578 ss_info = node_to_item(node_j, struct stream_sample_rate, list);
579 ALOGV("sample rate-%d", ss_info->sample_rate);
580 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700581 }
582}
583
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530584void audio_extn_utils_dump_streams_cfg_lists(
585 struct listnode *streams_output_cfg_list,
586 struct listnode *streams_input_cfg_list)
587{
588 ALOGV("%s", __func__);
589 audio_extn_utils_dump_streams_cfg_list(streams_output_cfg_list);
590 audio_extn_utils_dump_streams_cfg_list(streams_input_cfg_list);
591}
592
593static void audio_extn_utils_release_streams_cfg_list(
594 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700595{
596 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530597 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700598
599 ALOGV("%s", __func__);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530600
601 while (!list_empty(streams_cfg_list)) {
602 node_i = list_head(streams_cfg_list);
603 s_info = node_to_item(node_i, struct streams_io_cfg, list);
604 while (!list_empty(&s_info->format_list)) {
605 node_j = list_head(&s_info->format_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700606 list_remove(node_j);
607 free(node_to_item(node_j, struct stream_format, list));
608 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530609 while (!list_empty(&s_info->sample_rate_list)) {
610 node_j = list_head(&s_info->sample_rate_list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700611 list_remove(node_j);
612 free(node_to_item(node_j, struct stream_sample_rate, list));
613 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700614 list_remove(node_i);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530615 free(node_to_item(node_i, struct streams_io_cfg, list));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700616 }
617}
618
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530619void audio_extn_utils_release_streams_cfg_lists(
620 struct listnode *streams_output_cfg_list,
621 struct listnode *streams_input_cfg_list)
622{
623 ALOGV("%s", __func__);
624 audio_extn_utils_release_streams_cfg_list(streams_output_cfg_list);
625 audio_extn_utils_release_streams_cfg_list(streams_input_cfg_list);
626}
627
628static bool set_app_type_cfg(struct streams_io_cfg *s_info,
629 struct stream_app_type_cfg *app_type_cfg,
630 uint32_t sample_rate, uint32_t bit_width)
Amit Shekhar6f461b12014-08-01 14:52:58 -0700631 {
632 struct listnode *node_i;
633 struct stream_sample_rate *ss_info;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530634 list_for_each(node_i, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700635 ss_info = node_to_item(node_i, struct stream_sample_rate, list);
636 if ((sample_rate <= ss_info->sample_rate) &&
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530637 (bit_width == s_info->app_type_cfg.bit_width)) {
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700638
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530639 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700640 app_type_cfg->sample_rate = ss_info->sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530641 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700642 ALOGV("%s app_type_cfg->app_type %d, app_type_cfg->sample_rate %d, app_type_cfg->bit_width %d",
643 __func__, app_type_cfg->app_type, app_type_cfg->sample_rate, app_type_cfg->bit_width);
644 return true;
645 }
646 }
647 /*
648 * Reiterate through the list assuming dafault sample rate.
649 * Handles scenario where input sample rate is higher
650 * than all sample rates in list for the input bit width.
651 */
652 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
Apoorv Raghuvanshif59bb222015-02-18 12:23:23 -0800653
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530654 list_for_each(node_i, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700655 ss_info = node_to_item(node_i, struct stream_sample_rate, list);
656 if ((sample_rate <= ss_info->sample_rate) &&
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530657 (bit_width == s_info->app_type_cfg.bit_width)) {
658 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700659 app_type_cfg->sample_rate = sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530660 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Apoorv Raghuvanshif59bb222015-02-18 12:23:23 -0800661 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 -0700662 __func__, app_type_cfg->app_type, app_type_cfg->sample_rate, app_type_cfg->bit_width);
663 return true;
664 }
665 }
666 return false;
667}
668
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530669void audio_extn_utils_update_stream_input_app_type_cfg(void *platform,
670 struct listnode *streams_input_cfg_list,
671 audio_devices_t devices __unused,
672 audio_input_flags_t flags,
673 audio_format_t format,
674 uint32_t sample_rate,
675 uint32_t bit_width,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530676 char* profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530677 struct stream_app_type_cfg *app_type_cfg)
678{
679 struct listnode *node_i, *node_j;
680 struct streams_io_cfg *s_info;
681 struct stream_format *sf_info;
682
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530683 ALOGV("%s: flags: 0x%x, format: 0x%x sample_rate %d, profile %s",
684 __func__, flags, format, sample_rate, profile);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530685
686 list_for_each(node_i, streams_input_cfg_list) {
687 s_info = node_to_item(node_i, struct streams_io_cfg, list);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530688 /* Along with flags do profile matching if set at either end.*/
689 if (s_info->flags.in_flags == flags &&
690 ((profile[0] == '\0' && s_info->profile[0] == '\0') ||
691 strncmp(s_info->profile, profile, sizeof(s_info->profile)) == 0)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530692 list_for_each(node_j, &s_info->format_list) {
693 sf_info = node_to_item(node_j, struct stream_format, list);
694 if (sf_info->format == format) {
695 if (set_app_type_cfg(s_info, app_type_cfg, sample_rate, bit_width))
696 return;
697 }
698 }
699 }
700 }
701 ALOGW("%s: App type could not be selected. Falling back to default", __func__);
702 app_type_cfg->app_type = platform_get_default_app_type_v2(platform, PCM_CAPTURE);
703 app_type_cfg->sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
704 app_type_cfg->bit_width = 16;
705}
706
707void audio_extn_utils_update_stream_output_app_type_cfg(void *platform,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700708 struct listnode *streams_output_cfg_list,
Amit Shekhar1d896042014-10-03 13:16:09 -0700709 audio_devices_t devices,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700710 audio_output_flags_t flags,
711 audio_format_t format,
Amit Shekhar6f461b12014-08-01 14:52:58 -0700712 uint32_t sample_rate,
713 uint32_t bit_width,
Manish Dewangan837dc462015-05-27 10:17:41 +0530714 audio_channel_mask_t channel_mask,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530715 char *profile,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700716 struct stream_app_type_cfg *app_type_cfg)
717{
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +0530718 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530719 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700720 struct stream_format *sf_info;
Manish Dewangan837dc462015-05-27 10:17:41 +0530721 char value[PROPERTY_VALUE_MAX] = {0};
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700722
Ramu Gottipati074fa4d2018-09-11 20:05:51 +0530723 if (devices & AUDIO_DEVICE_OUT_SPEAKER) {
724 int bw = platform_get_snd_device_bit_width(SND_DEVICE_OUT_SPEAKER);
725 if ((-ENOSYS != bw) && (bit_width > (uint32_t)bw))
Amit Shekhar5a39c912014-10-14 15:39:30 -0700726 bit_width = (uint32_t)bw;
Ramu Gottipati074fa4d2018-09-11 20:05:51 +0530727 else if (-ENOSYS == bw)
728 bit_width = CODEC_BACKEND_DEFAULT_BIT_WIDTH;
Amit Shekhar1d896042014-10-03 13:16:09 -0700729 sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Ramu Gottipati074fa4d2018-09-11 20:05:51 +0530730 ALOGI("%s Allowing 24 and above bits playback on speaker ONLY at default sampling rate", __func__);
Amit Shekhar1d896042014-10-03 13:16:09 -0700731 }
732
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -0700733 property_get("vendor.audio.playback.mch.downsample",value,"");
Manish Dewangan837dc462015-05-27 10:17:41 +0530734 if (!strncmp("true", value, sizeof("true"))) {
735 if ((popcount(channel_mask) > 2) &&
736 (sample_rate > CODEC_BACKEND_DEFAULT_SAMPLE_RATE) &&
Aalique Grahame5cd12ff2017-10-19 10:57:00 -0700737 !(flags & (audio_output_flags_t)AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH)) {
Manish Dewangan837dc462015-05-27 10:17:41 +0530738 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
739 ALOGD("%s: MCH session defaulting sample rate to %d",
740 __func__, sample_rate);
741 }
742 }
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +0530743
744 /* Set sampling rate to 176.4 for DSD64
745 * and 352.8Khz for DSD128.
746 * Set Bit Width to 16. output will be 16 bit
747 * post DoP in ASM.
748 */
Aalique Grahame5cd12ff2017-10-19 10:57:00 -0700749 if ((flags & (audio_output_flags_t)AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH) &&
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +0530750 (format == AUDIO_FORMAT_DSD)) {
751 bit_width = 16;
752 if (sample_rate == INPUT_SAMPLING_RATE_DSD64)
753 sample_rate = OUTPUT_SAMPLING_RATE_DSD64;
754 else if (sample_rate == INPUT_SAMPLING_RATE_DSD128)
755 sample_rate = OUTPUT_SAMPLING_RATE_DSD128;
756 }
757
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +0530758
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800759 ALOGV("%s: flags: %x, format: %x sample_rate %d, profile %s, app_type %d",
760 __func__, flags, format, sample_rate, profile, app_type_cfg->app_type);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700761 list_for_each(node_i, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530762 s_info = node_to_item(node_i, struct streams_io_cfg, list);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530763 /* Along with flags do profile matching if set at either end.*/
764 if (s_info->flags.out_flags == flags &&
765 ((profile[0] == '\0' && s_info->profile[0] == '\0') ||
766 strncmp(s_info->profile, profile, sizeof(s_info->profile)) == 0)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530767 list_for_each(node_j, &s_info->format_list) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700768 sf_info = node_to_item(node_j, struct stream_format, list);
769 if (sf_info->format == format) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530770 if (set_app_type_cfg(s_info, app_type_cfg, sample_rate, bit_width))
Amit Shekhar6f461b12014-08-01 14:52:58 -0700771 return;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700772 }
773 }
774 }
775 }
776 list_for_each(node_i, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530777 s_info = node_to_item(node_i, struct streams_io_cfg, list);
778 if (s_info->flags.out_flags == AUDIO_OUTPUT_FLAG_PRIMARY) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700779 ALOGV("Compatible output profile not found.");
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530780 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
781 app_type_cfg->sample_rate = s_info->app_type_cfg.sample_rate;
782 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700783 ALOGV("%s Default to primary output: App type: %d sample_rate %d",
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530784 __func__, s_info->app_type_cfg.app_type, app_type_cfg->sample_rate);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700785 return;
786 }
787 }
788 ALOGW("%s: App type could not be selected. Falling back to default", __func__);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700789 app_type_cfg->app_type = platform_get_default_app_type(platform);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700790 app_type_cfg->sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700791 app_type_cfg->bit_width = 16;
792}
793
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +0530794static bool audio_is_this_native_usecase(struct audio_usecase *uc)
795{
796 bool native_usecase = false;
797 struct stream_out *out = (struct stream_out*) uc->stream.out;
798
799 if (PCM_PLAYBACK == uc->type && out != NULL &&
800 NATIVE_AUDIO_MODE_INVALID != platform_get_native_support() &&
801 is_offload_usecase(uc->id) &&
802 (out->sample_rate == OUTPUT_SAMPLING_RATE_44100))
803 native_usecase = true;
804
805 return native_usecase;
806}
807
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800808bool audio_extn_is_dsp_bit_width_enforce_mode_supported(audio_output_flags_t flags)
809{
810 /* DSP bitwidth enforce mode for ADM and AFE:
811 * includes:
812 * deep buffer, low latency, direct pcm and offload.
813 * excludes:
814 * ull(raw+fast), VOIP.
815 */
816 if ((flags & AUDIO_OUTPUT_FLAG_VOIP_RX) ||
817 ((flags & AUDIO_OUTPUT_FLAG_RAW) &&
818 (flags & AUDIO_OUTPUT_FLAG_FAST)))
819 return false;
820
821
822 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) ||
823 (flags & AUDIO_OUTPUT_FLAG_DIRECT) ||
824 (flags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) ||
825 (flags & AUDIO_OUTPUT_FLAG_PRIMARY))
826 return true;
827 else
828 return false;
829}
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800830
831static inline bool audio_is_vr_mode_on(struct audio_device *(__attribute__((unused)) adev))
832{
833 return adev->vr_audio_mode_enabled;
834}
835
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530836void audio_extn_utils_update_stream_app_type_cfg_for_usecase(
837 struct audio_device *adev,
838 struct audio_usecase *usecase)
839{
840 ALOGV("%s", __func__);
841
842 switch(usecase->type) {
843 case PCM_PLAYBACK:
844 audio_extn_utils_update_stream_output_app_type_cfg(adev->platform,
845 &adev->streams_output_cfg_list,
846 usecase->stream.out->devices,
847 usecase->stream.out->flags,
Aalique Grahame65780b52017-09-27 14:59:56 -0700848 usecase->stream.out->hal_op_format,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530849 usecase->stream.out->sample_rate,
850 usecase->stream.out->bit_width,
851 usecase->stream.out->channel_mask,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530852 usecase->stream.out->profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530853 &usecase->stream.out->app_type_cfg);
854 ALOGV("%s Selected apptype: %d", __func__, usecase->stream.out->app_type_cfg.app_type);
855 break;
856 case PCM_CAPTURE:
Vikram Panduranga93f080e2017-06-07 18:16:14 -0700857 if (usecase->id == USECASE_AUDIO_RECORD_VOIP)
858 usecase->stream.in->app_type_cfg.app_type = APP_TYPE_VOIP_AUDIO;
859 else
860 audio_extn_utils_update_stream_input_app_type_cfg(adev->platform,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530861 &adev->streams_input_cfg_list,
862 usecase->stream.in->device,
863 usecase->stream.in->flags,
864 usecase->stream.in->format,
865 usecase->stream.in->sample_rate,
866 usecase->stream.in->bit_width,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530867 usecase->stream.in->profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530868 &usecase->stream.in->app_type_cfg);
869 ALOGV("%s Selected apptype: %d", __func__, usecase->stream.in->app_type_cfg.app_type);
870 break;
Surendar Karka93cd25a2018-08-28 14:21:37 +0530871 case TRANSCODE_LOOPBACK_RX :
Siddartha Shaik343abc62017-08-08 11:15:25 +0530872 audio_extn_utils_update_stream_output_app_type_cfg(adev->platform,
873 &adev->streams_output_cfg_list,
874 usecase->stream.inout->out_config.devices,
875 0,
876 usecase->stream.inout->out_config.format,
877 usecase->stream.inout->out_config.sample_rate,
878 usecase->stream.inout->out_config.bit_width,
879 usecase->stream.inout->out_config.channel_mask,
880 usecase->stream.inout->profile,
881 &usecase->stream.inout->out_app_type_cfg);
882 ALOGV("%s Selected apptype: %d", __func__, usecase->stream.inout->out_app_type_cfg.app_type);
883 break;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530884 default:
885 ALOGE("%s: app type cfg not supported for usecase type (%d)",
886 __func__, usecase->type);
887 }
888}
889
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +0530890void audio_extn_btsco_get_sample_rate(int snd_device, int *sample_rate)
891{
892 switch (snd_device) {
893 case SND_DEVICE_OUT_BT_SCO:
894 case SND_DEVICE_IN_BT_SCO_MIC:
895 case SND_DEVICE_IN_BT_SCO_MIC_NREC:
896 *sample_rate = 8000;
897 break;
898 case SND_DEVICE_OUT_BT_SCO_WB:
899 case SND_DEVICE_IN_BT_SCO_MIC_WB:
900 case SND_DEVICE_IN_BT_SCO_MIC_WB_NREC:
901 *sample_rate = 16000;
902 break;
903 default:
904 ALOGD("%s:Not a BT SCO device, need not update sampling rate\n", __func__);
905 break;
906 }
907}
908
Aalique Grahame22e49102018-12-18 14:23:57 -0800909static int set_stream_app_type_mixer_ctrl(struct audio_device *adev,
910 int pcm_device_id, int app_type,
911 int acdb_dev_id, int sample_rate,
912 int stream_type,
913 snd_device_t snd_device)
914{
915
916 char mixer_ctl_name[MAX_LENGTH_MIXER_CONTROL_IN_INT];
917 struct mixer_ctl *ctl;
918 int app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT], len = 0, rc = 0;
919 int snd_device_be_idx = -1;
920
921 if (stream_type == PCM_PLAYBACK) {
922 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
923 "Audio Stream %d App Type Cfg", pcm_device_id);
924 } else if (stream_type == PCM_CAPTURE) {
925 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
926 "Audio Stream Capture %d App Type Cfg", pcm_device_id);
927 }
928
929 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
930 if (!ctl) {
931 ALOGE("%s: Could not get ctl for mixer cmd - %s",
932 __func__, mixer_ctl_name);
933 rc = -EINVAL;
934 goto exit;
935 }
936 app_type_cfg[len++] = app_type;
937 app_type_cfg[len++] = acdb_dev_id;
938 app_type_cfg[len++] = sample_rate;
939
940 snd_device_be_idx = platform_get_snd_device_backend_index(snd_device);
941 if (snd_device_be_idx > 0)
942 app_type_cfg[len++] = snd_device_be_idx;
943 ALOGV("%s: stream type %d app_type %d, acdb_dev_id %d "
944 "sample rate %d, snd_device_be_idx %d",
945 __func__, stream_type, app_type, acdb_dev_id, sample_rate,
946 snd_device_be_idx);
947 mixer_ctl_set_array(ctl, app_type_cfg, len);
948
949exit:
950 return rc;
951}
952
953static int audio_extn_utils_send_app_type_cfg_hfp(struct audio_device *adev,
954 struct audio_usecase *usecase)
955{
956 int pcm_device_id, acdb_dev_id = 0, snd_device = usecase->out_snd_device;
957 int32_t sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
958 int app_type = 0, rc = 0;
959
960 ALOGV("%s", __func__);
961
962 if (usecase->type != PCM_HFP_CALL) {
963 ALOGV("%s: not a playback or HFP path, no need to cfg app type", __func__);
964 rc = 0;
965 goto exit_send_app_type_cfg;
966 }
967 if ((usecase->id != USECASE_AUDIO_HFP_SCO) &&
968 (usecase->id != USECASE_AUDIO_HFP_SCO_WB)) {
969 ALOGV("%s: a playback path where app type cfg is not required", __func__);
970 rc = 0;
971 goto exit_send_app_type_cfg;
972 }
973
974 snd_device = usecase->out_snd_device;
975 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_PLAYBACK);
976
977 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
978 if (acdb_dev_id < 0) {
979 ALOGE("%s: Couldn't get the acdb dev id", __func__);
980 rc = -EINVAL;
981 goto exit_send_app_type_cfg;
982 }
983
984 if (usecase->type == PCM_HFP_CALL) {
985
986 /* config HFP session:1 playback path */
987 app_type = platform_get_default_app_type_v2(adev->platform, PCM_PLAYBACK);
988 sample_rate= CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
989 rc = set_stream_app_type_mixer_ctrl(adev, pcm_device_id, app_type,
990 acdb_dev_id, sample_rate,
991 PCM_PLAYBACK,
992 SND_DEVICE_NONE); // use legacy behavior
993 if (rc < 0)
994 goto exit_send_app_type_cfg;
995
996 /* config HFP session:1 capture path */
997 app_type = platform_get_default_app_type_v2(adev->platform, PCM_CAPTURE);
998 rc = set_stream_app_type_mixer_ctrl(adev, pcm_device_id, app_type,
999 acdb_dev_id, sample_rate,
1000 PCM_CAPTURE,
1001 SND_DEVICE_NONE);
1002 if (rc < 0)
1003 goto exit_send_app_type_cfg;
1004
1005 /* config HFP session:2 capture path */
1006 pcm_device_id = HFP_ASM_RX_TX;
1007 snd_device = usecase->in_snd_device;
1008 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
1009 if (acdb_dev_id <= 0) {
1010 ALOGE("%s: Couldn't get the acdb dev id", __func__);
1011 rc = -EINVAL;
1012 goto exit_send_app_type_cfg;
1013 }
1014 app_type = platform_get_default_app_type_v2(adev->platform, PCM_CAPTURE);
1015 rc = set_stream_app_type_mixer_ctrl(adev, pcm_device_id, app_type,
1016 acdb_dev_id, sample_rate, PCM_CAPTURE,
1017 SND_DEVICE_NONE);
1018 if (rc < 0)
1019 goto exit_send_app_type_cfg;
1020
1021 /* config HFP session:2 playback path */
1022 app_type = platform_get_default_app_type_v2(adev->platform, PCM_PLAYBACK);
1023 rc = set_stream_app_type_mixer_ctrl(adev, pcm_device_id, app_type,
1024 acdb_dev_id, sample_rate,
1025 PCM_PLAYBACK, SND_DEVICE_NONE);
1026 if (rc < 0)
1027 goto exit_send_app_type_cfg;
1028 }
1029
1030 rc = 0;
1031exit_send_app_type_cfg:
1032 return rc;
1033}
1034
Zhou Song06761dd2019-04-28 18:08:17 +08001035int audio_extn_utils_get_app_sample_rate_for_device(
1036 struct audio_device *adev,
1037 struct audio_usecase *usecase, int snd_device)
1038{
1039 char value[PROPERTY_VALUE_MAX] = {0};
1040 int sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
1041
1042 if ((usecase->type == PCM_PLAYBACK) && (usecase->stream.out != NULL)) {
1043 property_get("vendor.audio.playback.mch.downsample",value,"");
1044 if (!strncmp("true", value, sizeof("true"))) {
1045 if ((popcount(usecase->stream.out->channel_mask) > 2) &&
1046 (usecase->stream.out->app_type_cfg.sample_rate > CODEC_BACKEND_DEFAULT_SAMPLE_RATE) &&
1047 !(usecase->stream.out->flags &
1048 (audio_output_flags_t)AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH))
1049 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
1050 }
1051
1052 if (usecase->id == USECASE_AUDIO_PLAYBACK_VOIP) {
1053 usecase->stream.out->app_type_cfg.sample_rate = usecase->stream.out->sample_rate;
1054 } else if (usecase->stream.out->devices & AUDIO_DEVICE_OUT_SPEAKER) {
1055 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
1056 } else if ((snd_device == SND_DEVICE_OUT_HDMI ||
1057 snd_device == SND_DEVICE_OUT_USB_HEADSET ||
1058 snd_device == SND_DEVICE_OUT_DISPLAY_PORT) &&
1059 (usecase->stream.out->sample_rate >= OUTPUT_SAMPLING_RATE_44100)) {
1060 /*
1061 * To best utlize DSP, check if the stream sample rate is supported/multiple of
1062 * configured device sample rate, if not update the COPP rate to be equal to the
1063 * device sample rate, else open COPP at stream sample rate
1064 */
1065 platform_check_and_update_copp_sample_rate(adev->platform, snd_device,
1066 usecase->stream.out->sample_rate,
1067 &usecase->stream.out->app_type_cfg.sample_rate);
1068 } else if (((snd_device != SND_DEVICE_OUT_HEADPHONES_44_1 &&
1069 !audio_is_this_native_usecase(usecase)) &&
1070 usecase->stream.out->sample_rate == OUTPUT_SAMPLING_RATE_44100) ||
1071 (usecase->stream.out->sample_rate < OUTPUT_SAMPLING_RATE_44100)) {
1072 /* Reset to default if no native stream is active*/
1073 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
1074 } else if (usecase->stream.out->devices & AUDIO_DEVICE_OUT_ALL_A2DP) {
1075 /*
1076 * For a2dp playback get encoder sampling rate and set copp sampling rate,
1077 * for bit width use the stream param only.
1078 */
1079 audio_extn_a2dp_get_enc_sample_rate(&usecase->stream.out->app_type_cfg.sample_rate);
1080 ALOGI("%s using %d sample rate rate for A2DP CoPP",
1081 __func__, usecase->stream.out->app_type_cfg.sample_rate);
1082 }
1083 audio_extn_btsco_get_sample_rate(snd_device, &usecase->stream.out->app_type_cfg.sample_rate);
1084 sample_rate = usecase->stream.out->app_type_cfg.sample_rate;
1085
1086 if (((usecase->stream.out->format == AUDIO_FORMAT_E_AC3) ||
1087 (usecase->stream.out->format == AUDIO_FORMAT_E_AC3_JOC) ||
1088 (usecase->stream.out->format == AUDIO_FORMAT_DOLBY_TRUEHD))
1089 && audio_extn_passthru_is_passthrough_stream(usecase->stream.out)
1090 && !audio_extn_passthru_is_convert_supported(adev, usecase->stream.out)) {
1091 sample_rate = sample_rate * 4;
1092 if (sample_rate > HDMI_PASSTHROUGH_MAX_SAMPLE_RATE)
1093 sample_rate = HDMI_PASSTHROUGH_MAX_SAMPLE_RATE;
1094 }
1095 } else if (usecase->type == PCM_CAPTURE) {
1096 if (usecase->stream.in != NULL) {
1097 if (usecase->id == USECASE_AUDIO_RECORD_VOIP)
1098 usecase->stream.in->app_type_cfg.sample_rate = usecase->stream.in->sample_rate;
1099 if (voice_is_in_call_rec_stream(usecase->stream.in)) {
1100 audio_extn_btsco_get_sample_rate(usecase->in_snd_device,
1101 &usecase->stream.in->app_type_cfg.sample_rate);
1102 } else {
1103 audio_extn_btsco_get_sample_rate(snd_device,
1104 &usecase->stream.in->app_type_cfg.sample_rate);
1105 }
1106 sample_rate = usecase->stream.in->app_type_cfg.sample_rate;
1107 } else if (usecase->id == USECASE_AUDIO_SPKR_CALIB_TX) {
1108 sample_rate = SAMPLE_RATE_8000;
1109 }
1110 } else if (usecase->type == TRANSCODE_LOOPBACK_RX) {
1111 sample_rate = usecase->stream.inout->out_config.sample_rate;
1112 }
1113 return sample_rate;
1114}
1115
Siena Richard7c2db772016-12-21 11:32:34 -08001116static int send_app_type_cfg_for_device(struct audio_device *adev,
1117 struct audio_usecase *usecase,
1118 int split_snd_device)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001119{
1120 char mixer_ctl_name[MAX_LENGTH_MIXER_CONTROL_IN_INT];
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301121 size_t app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT] = {0};
1122 int len = 0, rc;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001123 struct mixer_ctl *ctl;
Siena Richard7c2db772016-12-21 11:32:34 -08001124 int pcm_device_id = 0, acdb_dev_id, app_type;
1125 int snd_device = split_snd_device, snd_device_be_idx = -1;
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +05301126 int32_t sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Varun Balaraje49253e2017-07-06 19:48:56 +05301127 struct streams_io_cfg *s_info = NULL;
1128 struct listnode *node = NULL;
Nikhil Laturkarac4a7492017-08-31 18:27:19 +05301129 int bd_app_type = 0;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001130
Siena Richard7c2db772016-12-21 11:32:34 -08001131 ALOGV("%s: usecase->out_snd_device %s, usecase->in_snd_device %s, split_snd_device %s",
1132 __func__, platform_get_snd_device_name(usecase->out_snd_device),
1133 platform_get_snd_device_name(usecase->in_snd_device),
1134 platform_get_snd_device_name(split_snd_device));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001135
Siddartha Shaik343abc62017-08-08 11:15:25 +05301136 if (usecase->type != PCM_PLAYBACK && usecase->type != PCM_CAPTURE &&
Surendar Karka93cd25a2018-08-28 14:21:37 +05301137 usecase->type != TRANSCODE_LOOPBACK_RX) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301138 ALOGE("%s: not a playback/capture path, no need to cfg app type", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001139 rc = 0;
1140 goto exit_send_app_type_cfg;
1141 }
1142 if ((usecase->id != USECASE_AUDIO_PLAYBACK_DEEP_BUFFER) &&
1143 (usecase->id != USECASE_AUDIO_PLAYBACK_LOW_LATENCY) &&
1144 (usecase->id != USECASE_AUDIO_PLAYBACK_MULTI_CH) &&
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -08001145 (usecase->id != USECASE_AUDIO_PLAYBACK_ULL) &&
Vikram Panduranga93f080e2017-06-07 18:16:14 -07001146 (usecase->id != USECASE_AUDIO_PLAYBACK_VOIP) &&
Surendar Karka93cd25a2018-08-28 14:21:37 +05301147 (usecase->id != USECASE_AUDIO_TRANSCODE_LOOPBACK_RX) &&
Varun Balaraje49253e2017-07-06 19:48:56 +05301148 (!is_interactive_usecase(usecase->id)) &&
Srikanth Uyyala9d551402015-08-25 16:03:42 +05301149 (!is_offload_usecase(usecase->id)) &&
Derek Chenf6318be2017-06-12 17:16:24 -04001150 (usecase->type != PCM_CAPTURE) &&
1151 (!audio_extn_auto_hal_is_bus_device_usecase(usecase->id))) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301152 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 -07001153 rc = 0;
1154 goto exit_send_app_type_cfg;
1155 }
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -08001156
1157 //if VR is active then only send the mixer control
1158 if (usecase->id == USECASE_AUDIO_PLAYBACK_ULL && !audio_is_vr_mode_on(adev)) {
1159 ALOGI("ULL doesnt need sending app type cfg, returning");
1160 rc = 0;
1161 goto exit_send_app_type_cfg;
1162 }
1163
Surendar Karka93cd25a2018-08-28 14:21:37 +05301164 if (usecase->type == PCM_PLAYBACK || usecase->type == TRANSCODE_LOOPBACK_RX) {
Ben Romberger1fafdde2015-09-09 19:43:15 -07001165 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_PLAYBACK);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301166 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
1167 "Audio Stream %d App Type Cfg", pcm_device_id);
Ben Romberger1fafdde2015-09-09 19:43:15 -07001168 } else if (usecase->type == PCM_CAPTURE) {
Ben Romberger1fafdde2015-09-09 19:43:15 -07001169 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_CAPTURE);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301170 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
1171 "Audio Stream Capture %d App Type Cfg", pcm_device_id);
Srikanth Uyyala9d551402015-08-25 16:03:42 +05301172 }
Siena Richard7c2db772016-12-21 11:32:34 -08001173
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001174 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
1175 if (!ctl) {
1176 ALOGE("%s: Could not get ctl for mixer cmd - %s", __func__,
1177 mixer_ctl_name);
1178 rc = -EINVAL;
1179 goto exit_send_app_type_cfg;
1180 }
Aditya Bavanari701a6992017-03-30 19:17:16 +05301181 snd_device = platform_get_spkr_prot_snd_device(snd_device);
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301182 if (voice_is_in_call_rec_stream(usecase->stream.in) && usecase->type == PCM_CAPTURE) {
1183 snd_device_t voice_device = voice_get_incall_rec_snd_device(usecase->in_snd_device);
1184 acdb_dev_id = platform_get_snd_device_acdb_id(voice_device);
1185 ALOGV("acdb id for voice call use case %d", acdb_dev_id);
1186 } else {
1187 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
1188 }
Rohit Kumar1181b292017-01-31 18:07:17 +05301189 if (acdb_dev_id <= 0) {
1190 ALOGE("%s: Couldn't get the acdb dev id", __func__);
1191 rc = -EINVAL;
1192 goto exit_send_app_type_cfg;
1193 }
1194
Siena Richard7c2db772016-12-21 11:32:34 -08001195 snd_device_be_idx = platform_get_snd_device_backend_index(snd_device);
1196 if (snd_device_be_idx < 0) {
1197 ALOGE("%s: Couldn't get the backend index for snd device %s ret=%d",
1198 __func__, platform_get_snd_device_name(snd_device),
1199 snd_device_be_idx);
1200 }
1201
Zhou Song06761dd2019-04-28 18:08:17 +08001202 sample_rate = audio_extn_utils_get_app_sample_rate_for_device(adev, usecase, snd_device);
1203
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301204 if ((usecase->type == PCM_PLAYBACK) && (usecase->stream.out != NULL)) {
Varun Balaraje49253e2017-07-06 19:48:56 +05301205 /* Interactive streams are supported with only direct app type id.
1206 * Get Direct profile app type and use it for interactive streams
1207 */
1208 list_for_each(node, &adev->streams_output_cfg_list) {
1209 s_info = node_to_item(node, struct streams_io_cfg, list);
Aalique Grahame5cd12ff2017-10-19 10:57:00 -07001210 if (s_info->flags.out_flags == (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_BD |
Nikhil Laturkarac4a7492017-08-31 18:27:19 +05301211 AUDIO_OUTPUT_FLAG_DIRECT_PCM |
1212 AUDIO_OUTPUT_FLAG_DIRECT))
1213 bd_app_type = s_info->app_type_cfg.app_type;
Varun Balaraje49253e2017-07-06 19:48:56 +05301214 }
Aalique Grahame5cd12ff2017-10-19 10:57:00 -07001215 if (usecase->stream.out->flags == (audio_output_flags_t)AUDIO_OUTPUT_FLAG_INTERACTIVE)
Nikhil Laturkarac4a7492017-08-31 18:27:19 +05301216 app_type = bd_app_type;
Varun Balaraje49253e2017-07-06 19:48:56 +05301217 else
1218 app_type = usecase->stream.out->app_type_cfg.app_type;
Siena Richard7c2db772016-12-21 11:32:34 -08001219 app_type_cfg[len++] = app_type;
Mingming Yin21854652016-04-13 11:54:02 -07001220 app_type_cfg[len++] = acdb_dev_id;
Naresh Tanniru3a406772017-05-10 13:09:05 -07001221 app_type_cfg[len++] = sample_rate;
1222
Siena Richard7c2db772016-12-21 11:32:34 -08001223 if (snd_device_be_idx > 0)
1224 app_type_cfg[len++] = snd_device_be_idx;
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301225
Siena Richard7c2db772016-12-21 11:32:34 -08001226 ALOGI("%s PLAYBACK app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
1227 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301228
1229 } else if ((usecase->type == PCM_CAPTURE) && (usecase->stream.in != NULL)) {
Siena Richard7c2db772016-12-21 11:32:34 -08001230 app_type = usecase->stream.in->app_type_cfg.app_type;
1231 app_type_cfg[len++] = app_type;
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301232 app_type_cfg[len++] = acdb_dev_id;
Siena Richard7c2db772016-12-21 11:32:34 -08001233 app_type_cfg[len++] = sample_rate;
1234 if (snd_device_be_idx > 0)
1235 app_type_cfg[len++] = snd_device_be_idx;
1236 ALOGI("%s CAPTURE app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
1237 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301238 } else {
Siena Richard7c2db772016-12-21 11:32:34 -08001239 app_type = platform_get_default_app_type_v2(adev->platform, usecase->type);
Surendar Karka93cd25a2018-08-28 14:21:37 +05301240 if(usecase->type == TRANSCODE_LOOPBACK_RX) {
Siddartha Shaik343abc62017-08-08 11:15:25 +05301241 app_type = usecase->stream.inout->out_app_type_cfg.app_type;
1242 }
Siena Richard7c2db772016-12-21 11:32:34 -08001243 app_type_cfg[len++] = app_type;
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301244 app_type_cfg[len++] = acdb_dev_id;
1245 app_type_cfg[len++] = sample_rate;
Siena Richard7c2db772016-12-21 11:32:34 -08001246 if (snd_device_be_idx > 0)
1247 app_type_cfg[len++] = snd_device_be_idx;
1248 ALOGI("%s default app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
1249 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Ashish Jainc02430d2016-01-04 10:42:43 +05301250 }
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301251
Siddartha Shaik343abc62017-08-08 11:15:25 +05301252 if(ctl)
1253 mixer_ctl_set_array(ctl, app_type_cfg, len);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001254 rc = 0;
1255exit_send_app_type_cfg:
1256 return rc;
1257}
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001258
Deeraj Soman14230922019-01-30 16:39:30 +05301259static int audio_extn_utils_check_input_parameters(uint32_t sample_rate,
1260 audio_format_t format,
1261 int channel_count)
1262{
1263 int ret = 0;
1264
1265 if (((format != AUDIO_FORMAT_PCM_16_BIT) && (format != AUDIO_FORMAT_PCM_8_24_BIT) &&
1266 (format != AUDIO_FORMAT_PCM_24_BIT_PACKED) && (format != AUDIO_FORMAT_PCM_32_BIT) &&
1267 (format != AUDIO_FORMAT_PCM_FLOAT)) &&
1268 !voice_extn_compress_voip_is_format_supported(format) &&
1269 !audio_extn_compr_cap_format_supported(format) &&
1270 !audio_extn_cin_format_supported(format))
1271 ret = -EINVAL;
1272
1273 switch (channel_count) {
1274 case 1:
1275 case 2:
1276 case 3:
1277 case 4:
1278 case 6:
1279 case 8:
1280 break;
1281 default:
1282 ret = -EINVAL;
1283 }
1284
1285 switch (sample_rate) {
1286 case 8000:
1287 case 11025:
1288 case 12000:
1289 case 16000:
1290 case 22050:
1291 case 24000:
1292 case 32000:
1293 case 44100:
1294 case 48000:
1295 case 88200:
1296 case 96000:
1297 case 176400:
1298 case 192000:
1299 break;
1300 default:
1301 ret = -EINVAL;
1302 }
1303
1304 return ret;
1305}
1306
1307static inline uint32_t audio_extn_utils_nearest_multiple(uint32_t num, uint32_t multiplier)
1308{
1309 uint32_t remainder = 0;
1310
1311 if (!multiplier)
1312 return num;
1313
1314 remainder = num % multiplier;
1315 if (remainder)
1316 num += (multiplier - remainder);
1317
1318 return num;
1319}
1320
1321static inline uint32_t audio_extn_utils_lcm(uint32_t num1, uint32_t num2)
1322{
1323 uint32_t high = num1, low = num2, temp = 0;
1324
1325 if (!num1 || !num2)
1326 return 0;
1327
1328 if (num1 < num2) {
1329 high = num2;
1330 low = num1;
1331 }
1332
1333 while (low != 0) {
1334 temp = low;
1335 low = high % low;
1336 high = temp;
1337 }
1338 return (num1 * num2)/high;
1339}
1340
Siena Richard7c2db772016-12-21 11:32:34 -08001341int audio_extn_utils_send_app_type_cfg(struct audio_device *adev,
1342 struct audio_usecase *usecase)
1343{
1344 int i, num_devices = 0;
1345 snd_device_t new_snd_devices[SND_DEVICE_OUT_END] = {0};
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301346 snd_device_t in_snd_device = usecase->in_snd_device;
Siena Richard7c2db772016-12-21 11:32:34 -08001347 int rc = 0;
1348
Aalique Grahame22e49102018-12-18 14:23:57 -08001349 if (usecase->type == PCM_HFP_CALL) {
1350 return audio_extn_utils_send_app_type_cfg_hfp(adev, usecase);
1351 }
1352
Siena Richard7c2db772016-12-21 11:32:34 -08001353 switch (usecase->type) {
1354 case PCM_PLAYBACK:
Surendar Karka93cd25a2018-08-28 14:21:37 +05301355 case TRANSCODE_LOOPBACK_RX:
Siena Richard7c2db772016-12-21 11:32:34 -08001356 ALOGD("%s: usecase->out_snd_device %s",
1357 __func__, platform_get_snd_device_name(usecase->out_snd_device));
1358 /* check for out combo device */
1359 if (platform_split_snd_device(adev->platform,
1360 usecase->out_snd_device,
1361 &num_devices, new_snd_devices)) {
1362 new_snd_devices[0] = usecase->out_snd_device;
1363 num_devices = 1;
1364 }
1365 break;
1366 case PCM_CAPTURE:
1367 ALOGD("%s: usecase->in_snd_device %s",
1368 __func__, platform_get_snd_device_name(usecase->in_snd_device));
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301369 if (voice_is_in_call_rec_stream(usecase->stream.in)) {
1370 in_snd_device = voice_get_incall_rec_backend_device(usecase->stream.in);
1371 }
Siena Richard7c2db772016-12-21 11:32:34 -08001372 /* check for in combo device */
1373 if (platform_split_snd_device(adev->platform,
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301374 in_snd_device,
Siena Richard7c2db772016-12-21 11:32:34 -08001375 &num_devices, new_snd_devices)) {
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301376 new_snd_devices[0] = in_snd_device;
Siena Richard7c2db772016-12-21 11:32:34 -08001377 num_devices = 1;
1378 }
1379 break;
1380 default:
1381 ALOGI("%s: not a playback/capture path, no need to cfg app type", __func__);
1382 rc = 0;
1383 break;
1384 }
1385
1386 for (i = 0; i < num_devices; i++) {
1387 rc = send_app_type_cfg_for_device(adev, usecase, new_snd_devices[i]);
1388 if (rc)
1389 break;
1390 }
1391
1392 return rc;
1393}
1394
Preetam Singh Ranawat9519e9c2015-11-18 16:05:55 +05301395int read_line_from_file(const char *path, char *buf, size_t count)
1396{
1397 char * fgets_ret;
1398 FILE * fd;
1399 int rv;
1400
1401 fd = fopen(path, "r");
1402 if (fd == NULL)
1403 return -1;
1404
1405 fgets_ret = fgets(buf, (int)count, fd);
1406 if (NULL != fgets_ret) {
1407 rv = (int)strlen(buf);
1408 } else {
1409 rv = ferror(fd);
1410 }
1411 fclose(fd);
1412
1413 return rv;
1414}
1415
Ashish Jainf1eaa582016-05-23 20:54:24 +05301416/*Translates ALSA formats to AOSP PCM formats*/
1417audio_format_t alsa_format_to_hal(uint32_t alsa_format)
1418{
1419 audio_format_t format;
1420
1421 switch(alsa_format) {
1422 case SNDRV_PCM_FORMAT_S16_LE:
1423 format = AUDIO_FORMAT_PCM_16_BIT;
1424 break;
1425 case SNDRV_PCM_FORMAT_S24_3LE:
1426 format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
1427 break;
1428 case SNDRV_PCM_FORMAT_S24_LE:
1429 format = AUDIO_FORMAT_PCM_8_24_BIT;
1430 break;
1431 case SNDRV_PCM_FORMAT_S32_LE:
1432 format = AUDIO_FORMAT_PCM_32_BIT;
1433 break;
1434 default:
1435 ALOGW("Incorrect ALSA format");
1436 format = AUDIO_FORMAT_INVALID;
1437 }
1438 return format;
1439}
1440
1441/*Translates hal format (AOSP) to alsa formats*/
1442uint32_t hal_format_to_alsa(audio_format_t hal_format)
1443{
1444 uint32_t alsa_format;
1445
1446 switch (hal_format) {
1447 case AUDIO_FORMAT_PCM_32_BIT: {
1448 if (platform_supports_true_32bit())
1449 alsa_format = SNDRV_PCM_FORMAT_S32_LE;
1450 else
1451 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1452 }
1453 break;
1454 case AUDIO_FORMAT_PCM_8_BIT:
1455 alsa_format = SNDRV_PCM_FORMAT_S8;
1456 break;
1457 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
1458 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1459 break;
1460 case AUDIO_FORMAT_PCM_8_24_BIT: {
1461 if (platform_supports_true_32bit())
1462 alsa_format = SNDRV_PCM_FORMAT_S32_LE;
1463 else
1464 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1465 }
1466 break;
1467 case AUDIO_FORMAT_PCM_FLOAT:
1468 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1469 break;
1470 default:
1471 case AUDIO_FORMAT_PCM_16_BIT:
1472 alsa_format = SNDRV_PCM_FORMAT_S16_LE;
1473 break;
1474 }
1475 return alsa_format;
1476}
1477
Ashish Jain83a6cc22016-06-28 14:34:17 +05301478/*Translates PCM formats to AOSP formats*/
1479audio_format_t pcm_format_to_hal(uint32_t pcm_format)
1480{
1481 audio_format_t format = AUDIO_FORMAT_INVALID;
1482
1483 switch(pcm_format) {
1484 case PCM_FORMAT_S16_LE:
1485 format = AUDIO_FORMAT_PCM_16_BIT;
1486 break;
1487 case PCM_FORMAT_S24_3LE:
1488 format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
1489 break;
1490 case PCM_FORMAT_S24_LE:
1491 format = AUDIO_FORMAT_PCM_8_24_BIT;
1492 break;
1493 case PCM_FORMAT_S32_LE:
1494 format = AUDIO_FORMAT_PCM_32_BIT;
1495 break;
1496 default:
1497 ALOGW("Incorrect PCM format");
1498 format = AUDIO_FORMAT_INVALID;
1499 }
1500 return format;
1501}
1502
1503/*Translates hal format (AOSP) to alsa formats*/
1504uint32_t hal_format_to_pcm(audio_format_t hal_format)
1505{
1506 uint32_t pcm_format;
1507
1508 switch (hal_format) {
1509 case AUDIO_FORMAT_PCM_32_BIT:
1510 case AUDIO_FORMAT_PCM_8_24_BIT:
1511 case AUDIO_FORMAT_PCM_FLOAT: {
1512 if (platform_supports_true_32bit())
1513 pcm_format = PCM_FORMAT_S32_LE;
1514 else
1515 pcm_format = PCM_FORMAT_S24_3LE;
1516 }
1517 break;
1518 case AUDIO_FORMAT_PCM_8_BIT:
1519 pcm_format = PCM_FORMAT_S8;
1520 break;
1521 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
1522 pcm_format = PCM_FORMAT_S24_3LE;
1523 break;
1524 default:
1525 case AUDIO_FORMAT_PCM_16_BIT:
1526 pcm_format = PCM_FORMAT_S16_LE;
1527 break;
1528 }
1529 return pcm_format;
1530}
1531
Ashish Jainf1eaa582016-05-23 20:54:24 +05301532uint32_t get_alsa_fragment_size(uint32_t bytes_per_sample,
1533 uint32_t sample_rate,
Deeraj Soman65358ab2019-02-07 15:40:49 +05301534 uint32_t noOfChannels,
1535 int64_t duration_ms)
Ashish Jainf1eaa582016-05-23 20:54:24 +05301536{
1537 uint32_t fragment_size = 0;
1538 uint32_t pcm_offload_time = PCM_OFFLOAD_BUFFER_DURATION;
1539
Deeraj Soman65358ab2019-02-07 15:40:49 +05301540 if (duration_ms >= MIN_OFFLOAD_BUFFER_DURATION_MS && duration_ms <= MAX_OFFLOAD_BUFFER_DURATION_MS)
1541 pcm_offload_time = duration_ms;
1542
Ashish Jainf1eaa582016-05-23 20:54:24 +05301543 fragment_size = (pcm_offload_time
1544 * sample_rate
1545 * bytes_per_sample
1546 * noOfChannels)/1000;
1547 if (fragment_size < MIN_PCM_OFFLOAD_FRAGMENT_SIZE)
1548 fragment_size = MIN_PCM_OFFLOAD_FRAGMENT_SIZE;
1549 else if (fragment_size > MAX_PCM_OFFLOAD_FRAGMENT_SIZE)
1550 fragment_size = MAX_PCM_OFFLOAD_FRAGMENT_SIZE;
1551 /*To have same PCM samples for all channels, the buffer size requires to
1552 *be multiple of (number of channels * bytes per sample)
1553 *For writes to succeed, the buffer must be written at address which is multiple of 32
1554 */
Aalique Grahameb85f2d92017-10-16 17:53:23 -07001555 fragment_size = ALIGN(fragment_size, (bytes_per_sample * noOfChannels * 32));
Ashish Jainf1eaa582016-05-23 20:54:24 +05301556
1557 ALOGI("PCM offload Fragment size to %d bytes", fragment_size);
1558 return fragment_size;
1559}
1560
1561/* Calculates the fragment size required to configure compress session.
1562 * Based on the alsa format selected, decide if conversion is needed in
1563
1564 * HAL ( e.g. convert AUDIO_FORMAT_PCM_FLOAT input format to
1565 * AUDIO_FORMAT_PCM_24_BIT_PACKED before writing to the compress driver.
1566 */
1567void audio_extn_utils_update_direct_pcm_fragment_size(struct stream_out *out)
1568{
Ashish Jain83a6cc22016-06-28 14:34:17 +05301569 audio_format_t dst_format = out->hal_op_format;
1570 audio_format_t src_format = out->hal_ip_format;
Ashish Jainf1eaa582016-05-23 20:54:24 +05301571 uint32_t hal_op_bytes_per_sample = audio_bytes_per_sample(dst_format);
1572 uint32_t hal_ip_bytes_per_sample = audio_bytes_per_sample(src_format);
1573
1574 out->compr_config.fragment_size =
1575 get_alsa_fragment_size(hal_op_bytes_per_sample,
1576 out->sample_rate,
Deeraj Soman65358ab2019-02-07 15:40:49 +05301577 popcount(out->channel_mask),
1578 out->info.duration_us / 1000);
Ashish Jainf1eaa582016-05-23 20:54:24 +05301579
1580 if ((src_format != dst_format) &&
1581 hal_op_bytes_per_sample != hal_ip_bytes_per_sample) {
1582
Ashish Jain83a6cc22016-06-28 14:34:17 +05301583 out->hal_fragment_size =
Ashish Jainf1eaa582016-05-23 20:54:24 +05301584 ((out->compr_config.fragment_size * hal_ip_bytes_per_sample) /
1585 hal_op_bytes_per_sample);
1586 ALOGI("enable conversion hal_input_fragment_size is %d src_format %x dst_format %x",
Ashish Jain83a6cc22016-06-28 14:34:17 +05301587 out->hal_fragment_size, src_format, dst_format);
Ashish Jainf1eaa582016-05-23 20:54:24 +05301588 } else {
Ashish Jain83a6cc22016-06-28 14:34:17 +05301589 out->hal_fragment_size = out->compr_config.fragment_size;
Ashish Jainf1eaa582016-05-23 20:54:24 +05301590 }
1591}
1592
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301593/* converts pcm format 24_8 to 8_24 inplace */
1594size_t audio_extn_utils_convert_format_24_8_to_8_24(void *buf, size_t bytes)
1595{
1596 size_t i = 0;
1597 int *int_buf_stream = buf;
1598
1599 if ((bytes % 4) != 0) {
1600 ALOGE("%s: wrong inout buffer! ... is not 32 bit aligned ", __func__);
1601 return -EINVAL;
1602 }
1603
1604 for (; i < (bytes / 4); i++)
1605 int_buf_stream[i] >>= 8;
1606
1607 return bytes;
1608}
1609
1610int get_snd_codec_id(audio_format_t format)
1611{
1612 int id = 0;
1613
1614 switch (format & AUDIO_FORMAT_MAIN_MASK) {
1615 case AUDIO_FORMAT_MP3:
1616 id = SND_AUDIOCODEC_MP3;
1617 break;
1618 case AUDIO_FORMAT_AAC:
1619 id = SND_AUDIOCODEC_AAC;
1620 break;
1621 case AUDIO_FORMAT_AAC_ADTS:
1622 id = SND_AUDIOCODEC_AAC;
1623 break;
Arun Kumar Dasari3b174182016-12-27 13:01:14 +05301624 case AUDIO_FORMAT_AAC_LATM:
1625 id = SND_AUDIOCODEC_AAC;
1626 break;
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301627 case AUDIO_FORMAT_PCM:
1628 id = SND_AUDIOCODEC_PCM;
1629 break;
1630 case AUDIO_FORMAT_FLAC:
1631 id = SND_AUDIOCODEC_FLAC;
1632 break;
1633 case AUDIO_FORMAT_ALAC:
1634 id = SND_AUDIOCODEC_ALAC;
1635 break;
1636 case AUDIO_FORMAT_APE:
1637 id = SND_AUDIOCODEC_APE;
1638 break;
1639 case AUDIO_FORMAT_VORBIS:
1640 id = SND_AUDIOCODEC_VORBIS;
1641 break;
1642 case AUDIO_FORMAT_WMA:
1643 id = SND_AUDIOCODEC_WMA;
1644 break;
1645 case AUDIO_FORMAT_WMA_PRO:
1646 id = SND_AUDIOCODEC_WMA_PRO;
1647 break;
Satish Babu Patakokila0c313922016-12-08 12:07:08 +05301648 case AUDIO_FORMAT_MP2:
1649 id = SND_AUDIOCODEC_MP2;
1650 break;
Satish Babu Patakokila915ecba2017-01-10 17:43:56 +05301651 case AUDIO_FORMAT_AC3:
1652 id = SND_AUDIOCODEC_AC3;
1653 break;
1654 case AUDIO_FORMAT_E_AC3:
1655 case AUDIO_FORMAT_E_AC3_JOC:
1656 id = SND_AUDIOCODEC_EAC3;
1657 break;
1658 case AUDIO_FORMAT_DTS:
1659 case AUDIO_FORMAT_DTS_HD:
1660 id = SND_AUDIOCODEC_DTS;
1661 break;
Ben Romberger1aaaf862017-04-06 17:49:46 -07001662 case AUDIO_FORMAT_DOLBY_TRUEHD:
1663 id = SND_AUDIOCODEC_TRUEHD;
1664 break;
Naresh Tanniru928f0862017-04-07 16:44:23 -07001665 case AUDIO_FORMAT_IEC61937:
1666 id = SND_AUDIOCODEC_IEC61937;
1667 break;
Preetam Singh Ranawat4277a5a2017-01-18 19:02:24 +05301668 case AUDIO_FORMAT_DSD:
1669 id = SND_AUDIOCODEC_DSD;
1670 break;
Dhanalakshmi Siddani18737932016-11-29 17:33:17 +05301671 case AUDIO_FORMAT_APTX:
1672 id = SND_AUDIOCODEC_APTX;
1673 break;
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301674 default:
1675 ALOGE("%s: Unsupported audio format :%x", __func__, format);
1676 }
1677
1678 return id;
1679}
1680
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001681void audio_extn_utils_send_audio_calibration(struct audio_device *adev,
1682 struct audio_usecase *usecase)
1683{
1684 int type = usecase->type;
1685
Dhananjay Kumar14245982017-01-16 20:21:00 +05301686 if (type == PCM_PLAYBACK && usecase->stream.out != NULL) {
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05301687 platform_send_audio_calibration(adev->platform, usecase,
Zhou Song06761dd2019-04-28 18:08:17 +08001688 usecase->stream.out->app_type_cfg.app_type);
Dhananjay Kumar14245982017-01-16 20:21:00 +05301689 } else if (type == PCM_CAPTURE && usecase->stream.in != NULL) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301690 platform_send_audio_calibration(adev->platform, usecase,
Zhou Song06761dd2019-04-28 18:08:17 +08001691 usecase->stream.in->app_type_cfg.app_type);
1692 } else if ((type == PCM_HFP_CALL) || (type == PCM_CAPTURE) ||
1693 (type == TRANSCODE_LOOPBACK_RX && usecase->stream.inout != NULL)) {
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05301694 platform_send_audio_calibration(adev->platform, usecase,
Zhou Song06761dd2019-04-28 18:08:17 +08001695 platform_get_default_app_type_v2(adev->platform, usecase->type));
Vidyakumar Athotae57f6002017-03-01 13:13:16 -08001696 } else {
1697 /* No need to send audio calibration for voice and voip call usecases */
1698 if ((type != VOICE_CALL) && (type != VOIP_CALL))
1699 ALOGW("%s: No audio calibration for usecase type = %d", __func__, type);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001700 }
1701}
1702
Ben Rombergera04fabc2014-11-14 12:16:03 -08001703// Base64 Encode and Decode
1704// Not all features supported. This must be used only with following conditions.
1705// Decode Modes: Support with and without padding
1706// CRLF not handling. So no CRLF in string to decode.
1707// Encode Modes: Supports only padding
1708int b64decode(char *inp, int ilen, uint8_t* outp)
1709{
1710 int i, j, k, ii, num;
1711 int rem, pcnt;
1712 uint32_t res=0;
1713 uint8_t getIndex[MAX_BASEINDEX_LEN];
1714 uint8_t tmp, cflag;
1715
1716 if(inp == NULL || outp == NULL || ilen <= 0) {
1717 ALOGE("[%s] received NULL pointer or zero length",__func__);
1718 return -1;
1719 }
1720
1721 memset(getIndex, MAX_BASEINDEX_LEN-1, sizeof(getIndex));
1722 for(i=0;i<BASE_TABLE_SIZE;i++) {
1723 getIndex[(uint8_t)bTable[i]] = (uint8_t)i;
1724 }
1725 getIndex[(uint8_t)'=']=0;
1726
1727 j=0;k=0;
1728 num = ilen/4;
1729 rem = ilen%4;
1730 if(rem==0)
1731 num = num-1;
1732 cflag=0;
1733 for(i=0; i<num; i++) {
1734 res=0;
1735 for(ii=0;ii<4;ii++) {
1736 res = res << 6;
1737 tmp = getIndex[(uint8_t)inp[j++]];
1738 res = res | tmp;
1739 cflag = cflag | tmp;
1740 }
1741 outp[k++] = (res >> 16)&0xFF;
1742 outp[k++] = (res >> 8)&0xFF;
1743 outp[k++] = res & 0xFF;
1744 }
1745
1746 // Handle last bytes special
1747 pcnt=0;
1748 if(rem == 0) {
1749 //With padding or full data
1750 res = 0;
1751 for(ii=0;ii<4;ii++) {
1752 if(inp[j] == '=')
1753 pcnt++;
1754 res = res << 6;
1755 tmp = getIndex[(uint8_t)inp[j++]];
1756 res = res | tmp;
1757 cflag = cflag | tmp;
1758 }
1759 outp[k++] = res >> 16;
1760 if(pcnt == 2)
1761 goto done;
1762 outp[k++] = (res>>8)&0xFF;
1763 if(pcnt == 1)
1764 goto done;
1765 outp[k++] = res&0xFF;
1766 } else {
1767 //without padding
1768 res = 0;
1769 for(i=0;i<rem;i++) {
1770 res = res << 6;
1771 tmp = getIndex[(uint8_t)inp[j++]];
1772 res = res | tmp;
1773 cflag = cflag | tmp;
1774 }
1775 for(i=rem;i<4;i++) {
1776 res = res << 6;
1777 pcnt++;
1778 }
1779 outp[k++] = res >> 16;
1780 if(pcnt == 2)
1781 goto done;
1782 outp[k++] = (res>>8)&0xFF;
1783 if(pcnt == 1)
1784 goto done;
1785 outp[k++] = res&0xFF;
1786 }
1787done:
1788 if(cflag == 0xFF) {
1789 ALOGE("[%s] base64 decode failed. Invalid character found %s",
1790 __func__, inp);
1791 return 0;
1792 }
1793 return k;
1794}
1795
1796int b64encode(uint8_t *inp, int ilen, char* outp)
1797{
1798 int i,j,k, num;
1799 int rem=0;
1800 uint32_t res=0;
1801
1802 if(inp == NULL || outp == NULL || ilen<=0) {
1803 ALOGE("[%s] received NULL pointer or zero input length",__func__);
1804 return -1;
1805 }
1806
1807 num = ilen/3;
1808 rem = ilen%3;
1809 j=0;k=0;
1810 for(i=0; i<num; i++) {
1811 //prepare index
1812 res = inp[j++]<<16;
1813 res = res | inp[j++]<<8;
1814 res = res | inp[j++];
1815 //get output map from index
1816 outp[k++] = (char) bTable[(res>>18)&0x3F];
1817 outp[k++] = (char) bTable[(res>>12)&0x3F];
1818 outp[k++] = (char) bTable[(res>>6)&0x3F];
1819 outp[k++] = (char) bTable[res&0x3F];
1820 }
1821
1822 switch(rem) {
1823 case 1:
1824 res = inp[j++]<<16;
1825 outp[k++] = (char) bTable[res>>18];
1826 outp[k++] = (char) bTable[(res>>12)&0x3F];
1827 //outp[k++] = '=';
1828 //outp[k++] = '=';
1829 break;
1830 case 2:
1831 res = inp[j++]<<16;
1832 res = res | inp[j++]<<8;
1833 outp[k++] = (char) bTable[res>>18];
1834 outp[k++] = (char) bTable[(res>>12)&0x3F];
1835 outp[k++] = (char) bTable[(res>>6)&0x3F];
1836 //outp[k++] = '=';
1837 break;
1838 default:
1839 break;
1840 }
Ben Rombergera04fabc2014-11-14 12:16:03 -08001841 outp[k] = '\0';
1842 return k;
1843}
Ashish Jain81eb2a82015-05-13 10:52:34 +05301844
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301845
1846int audio_extn_utils_get_codec_version(const char *snd_card_name,
1847 int card_num,
1848 char *codec_version)
1849{
1850 char procfs_path[50];
1851 FILE *fp;
1852
1853 if (strstr(snd_card_name, "tasha")) {
1854 snprintf(procfs_path, sizeof(procfs_path),
1855 "/proc/asound/card%d/codecs/tasha/version", card_num);
1856 if ((fp = fopen(procfs_path, "r")) != NULL) {
1857 fgets(codec_version, CODEC_VERSION_MAX_LENGTH, fp);
1858 fclose(fp);
1859 } else {
1860 ALOGE("%s: ERROR. cannot open %s", __func__, procfs_path);
1861 return -ENOENT;
1862 }
1863 ALOGD("%s: codec version %s", __func__, codec_version);
1864 }
1865
1866 return 0;
1867}
1868
Preetam Singh Ranawat9d0d8e42019-07-15 12:27:25 +05301869int audio_extn_utils_get_codec_variant(int card_num,
1870 char *codec_variant)
1871{
1872 char procfs_path[50];
1873 FILE *fp;
1874 snprintf(procfs_path, sizeof(procfs_path),
1875 "/proc/asound/card%d/codecs/wcd938x/variant", card_num);
1876 if ((fp = fopen(procfs_path, "r")) == NULL) {
1877 snprintf(procfs_path, sizeof(procfs_path),
1878 "/proc/asound/card%d/codecs/wcd937x/variant", card_num);
1879 if ((fp = fopen(procfs_path, "r")) == NULL) {
1880 ALOGE("%s: ERROR. cannot open %s", __func__, procfs_path);
1881 return -ENOENT;
1882 }
1883 }
1884 fgets(codec_variant, CODEC_VARIANT_MAX_LENGTH, fp);
1885 fclose(fp);
1886 ALOGD("%s: codec variant is %s", __func__, codec_variant);
1887 return 0;
1888}
1889
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301890
Ashish Jain81eb2a82015-05-13 10:52:34 +05301891#ifdef AUDIO_EXTERNAL_HDMI_ENABLED
1892
1893void get_default_compressed_channel_status(
1894 unsigned char *channel_status)
1895{
Ashish Jain81eb2a82015-05-13 10:52:34 +05301896 memset(channel_status,0,24);
1897
1898 /* block start bit in preamble bit 3 */
1899 channel_status[0] |= PROFESSIONAL;
1900 //compre out
1901 channel_status[0] |= NON_LPCM;
1902 // sample rate; fixed 48K for default/transcode
1903 channel_status[3] |= SR_48000;
1904}
1905
Ashish Jain81eb2a82015-05-13 10:52:34 +05301906int32_t get_compressed_channel_status(void *audio_stream_data,
1907 uint32_t audio_frame_size,
1908 unsigned char *channel_status,
1909 enum audio_parser_code_type codec_type)
1910 // codec_type - AUDIO_PARSER_CODEC_AC3
1911 // - AUDIO_PARSER_CODEC_DTS
1912{
1913 unsigned char *stream;
1914 int ret = 0;
1915 stream = (unsigned char *)audio_stream_data;
1916
1917 if (audio_stream_data == NULL || audio_frame_size == 0) {
1918 ALOGW("no buffer to get channel status, return default for compress");
1919 get_default_compressed_channel_status(channel_status);
1920 return ret;
1921 }
1922
1923 memset(channel_status,0,24);
1924 if(init_audio_parser(stream, audio_frame_size, codec_type) == -1)
1925 {
1926 ALOGE("init audio parser failed");
1927 return -1;
1928 }
1929 ret = get_channel_status(channel_status, codec_type);
1930 return ret;
1931
1932}
1933
Ashish Jain81eb2a82015-05-13 10:52:34 +05301934void get_lpcm_channel_status(uint32_t sampleRate,
1935 unsigned char *channel_status)
1936{
1937 int32_t status = 0;
Ashish Jain81eb2a82015-05-13 10:52:34 +05301938 memset(channel_status,0,24);
1939 /* block start bit in preamble bit 3 */
1940 channel_status[0] |= PROFESSIONAL;
1941 //LPCM OUT
1942 channel_status[0] &= ~NON_LPCM;
1943
1944 switch (sampleRate) {
1945 case 8000:
1946 case 11025:
1947 case 12000:
1948 case 16000:
1949 case 22050:
1950 channel_status[3] |= SR_NOTID;
Preetam Singh Ranawat61716b12015-12-14 11:55:24 +05301951 break;
Ashish Jain81eb2a82015-05-13 10:52:34 +05301952 case 24000:
1953 channel_status[3] |= SR_24000;
1954 break;
1955 case 32000:
1956 channel_status[3] |= SR_32000;
1957 break;
1958 case 44100:
1959 channel_status[3] |= SR_44100;
1960 break;
1961 case 48000:
1962 channel_status[3] |= SR_48000;
1963 break;
1964 case 88200:
1965 channel_status[3] |= SR_88200;
1966 break;
1967 case 96000:
1968 channel_status[3] |= SR_96000;
1969 break;
1970 case 176400:
1971 channel_status[3] |= SR_176400;
1972 break;
1973 case 192000:
1974 channel_status[3] |= SR_192000;
1975 break;
1976 default:
1977 ALOGV("Invalid sample_rate %u\n", sampleRate);
1978 status = -1;
1979 break;
1980 }
1981}
1982
1983void audio_utils_set_hdmi_channel_status(struct stream_out *out, char * buffer, size_t bytes)
1984{
1985 unsigned char channel_status[24]={0};
1986 struct snd_aes_iec958 iec958;
1987 const char *mixer_ctl_name = "IEC958 Playback PCM Stream";
1988 struct mixer_ctl *ctl;
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +05301989 ALOGV("%s: buffer %s bytes %zd", __func__, buffer, bytes);
Arun Mirpurie008ed22019-03-21 11:21:04 -07001990
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05301991 if (audio_extn_utils_is_dolby_format(out->format) &&
Ashish Jain81eb2a82015-05-13 10:52:34 +05301992 /*TODO:Extend code to support DTS passthrough*/
1993 /*set compressed channel status bits*/
Arun Mirpurie008ed22019-03-21 11:21:04 -07001994 audio_extn_passthru_is_passthrough_stream(out) &&
1995 audio_extn_is_hdmi_passthru_enabled()) {
Ashish Jain81eb2a82015-05-13 10:52:34 +05301996 get_compressed_channel_status(buffer, bytes, channel_status, AUDIO_PARSER_CODEC_AC3);
Arun Mirpurie008ed22019-03-21 11:21:04 -07001997 } else {
Ashish Jain81eb2a82015-05-13 10:52:34 +05301998 /*set channel status bit for LPCM*/
1999 get_lpcm_channel_status(out->sample_rate, channel_status);
2000 }
2001
2002 memcpy(iec958.status, channel_status,sizeof(iec958.status));
2003 ctl = mixer_get_ctl_by_name(out->dev->mixer, mixer_ctl_name);
2004 if (!ctl) {
2005 ALOGE("%s: Could not get ctl for mixer cmd - %s",
2006 __func__, mixer_ctl_name);
2007 return;
2008 }
2009 if (mixer_ctl_set_array(ctl, &iec958, sizeof(iec958)) < 0) {
2010 ALOGE("%s: Could not set channel status for ext HDMI ",
2011 __func__);
2012 return;
2013 }
2014
2015}
2016#endif
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302017
2018int audio_extn_utils_get_avt_device_drift(
2019 struct audio_usecase *usecase,
2020 struct audio_avt_device_drift_param *drift_param)
2021{
2022 int ret = 0, count = 0;
2023 char avt_device_drift_mixer_ctl_name[MIXER_PATH_MAX_LENGTH] = {0};
Naresh Tanniru6160c712017-04-17 15:43:48 +05302024 const char *backend = NULL;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302025 struct mixer_ctl *ctl = NULL;
2026 struct audio_avt_device_drift_stats drift_stats;
2027 struct audio_device *adev = NULL;
2028
2029 if (usecase != NULL && usecase->type == PCM_PLAYBACK) {
Naresh Tanniru6160c712017-04-17 15:43:48 +05302030 backend = platform_get_snd_device_backend_interface(usecase->out_snd_device);
2031 if (!backend) {
2032 ALOGE("%s: Unsupported device %d", __func__,
2033 usecase->stream.out->devices);
2034 ret = -EINVAL;
2035 goto done;
2036 }
2037 strlcpy(avt_device_drift_mixer_ctl_name,
2038 backend,
2039 MIXER_PATH_MAX_LENGTH);
2040
2041 count = strlen(backend);
2042 if (MIXER_PATH_MAX_LENGTH - count > 0) {
2043 strlcat(&avt_device_drift_mixer_ctl_name[count],
2044 " DRIFT",
2045 MIXER_PATH_MAX_LENGTH - count);
2046 } else {
2047 ret = -EINVAL;
2048 goto done;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302049 }
2050 } else {
Naresh Tanniru7586e292017-04-13 10:38:13 +05302051 ALOGE("%s: Invalid usecase",__func__);
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302052 ret = -EINVAL;
Naresh Tanniru6160c712017-04-17 15:43:48 +05302053 goto done;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302054 }
2055
Naresh Tanniru6160c712017-04-17 15:43:48 +05302056 adev = usecase->stream.out->dev;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302057 ctl = mixer_get_ctl_by_name(adev->mixer, avt_device_drift_mixer_ctl_name);
2058 if (!ctl) {
2059 ALOGE("%s: Could not get ctl for mixer cmd - %s",
2060 __func__, avt_device_drift_mixer_ctl_name);
2061
2062 ret = -EINVAL;
2063 goto done;
2064 }
2065
2066 ALOGV("%s: Getting AV Timer vs Device Drift mixer ctrl name %s", __func__,
2067 avt_device_drift_mixer_ctl_name);
2068
2069 mixer_ctl_update(ctl);
2070 count = mixer_ctl_get_num_values(ctl);
2071 if (count != sizeof(struct audio_avt_device_drift_stats)) {
2072 ALOGE("%s: mixer_ctl_get_num_values() invalid drift_stats data size",
2073 __func__);
2074
2075 ret = -EINVAL;
2076 goto done;
2077 }
2078
2079 ret = mixer_ctl_get_array(ctl, (void *)&drift_stats, count);
2080 if (ret != 0) {
2081 ALOGE("%s: mixer_ctl_get_array() failed to get drift_stats Params",
2082 __func__);
2083
2084 ret = -EINVAL;
2085 goto done;
2086 }
2087 memcpy(drift_param, &drift_stats.drift_param,
2088 sizeof(struct audio_avt_device_drift_param));
2089done:
2090 return ret;
2091}
Manish Dewangan07de2142017-02-27 19:27:20 +05302092
2093#ifdef SNDRV_COMPRESS_PATH_DELAY
2094int audio_extn_utils_compress_get_dsp_latency(struct stream_out *out)
2095{
2096 int ret = -EINVAL;
2097 struct snd_compr_metadata metadata;
2098 int delay_ms = COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
2099
Weiyin Jiangc49a3b52018-08-17 16:16:08 +08002100 /* override the latency for pcm offload use case */
2101 if ((out->flags & AUDIO_OUTPUT_FLAG_DIRECT) &&
2102 !(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)) {
2103 delay_ms = PCM_OFFLOAD_PLAYBACK_LATENCY;
2104 }
2105
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -07002106 if (property_get_bool("vendor.audio.playback.dsp.pathdelay", false)) {
Manish Dewangan07de2142017-02-27 19:27:20 +05302107 ALOGD("%s:: Quering DSP delay %d",__func__, __LINE__);
2108 if (!(is_offload_usecase(out->usecase))) {
2109 ALOGE("%s:: not supported for non offload session", __func__);
2110 goto exit;
2111 }
2112
2113 if (!out->compr) {
2114 ALOGD("%s:: Invalid compress handle,returning default dsp latency",
2115 __func__);
2116 goto exit;
2117 }
2118
2119 metadata.key = SNDRV_COMPRESS_PATH_DELAY;
2120 ret = compress_get_metadata(out->compr, &metadata);
2121 if(ret) {
2122 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2123 goto exit;
2124 }
2125 delay_ms = metadata.value[0] / 1000; /*convert to ms*/
2126 } else {
2127 ALOGD("%s:: Using Fix DSP delay",__func__);
2128 }
2129
2130exit:
2131 ALOGD("%s:: delay in ms is %d",__func__, delay_ms);
2132 return delay_ms;
2133}
2134#else
2135int audio_extn_utils_compress_get_dsp_latency(struct stream_out *out __unused)
2136{
2137 return COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
2138}
2139#endif
Manish Dewangan69426c82017-01-30 17:35:36 +05302140
2141#ifdef SNDRV_COMPRESS_RENDER_MODE
2142int audio_extn_utils_compress_set_render_mode(struct stream_out *out)
2143{
2144 struct snd_compr_metadata metadata;
2145 int ret = -EINVAL;
2146
2147 if (!(is_offload_usecase(out->usecase))) {
2148 ALOGE("%s:: not supported for non offload session", __func__);
2149 goto exit;
2150 }
2151
2152 if (!out->compr) {
2153 ALOGD("%s:: Invalid compress handle",
2154 __func__);
2155 goto exit;
2156 }
2157
2158 ALOGD("%s:: render mode %d", __func__, out->render_mode);
2159
2160 metadata.key = SNDRV_COMPRESS_RENDER_MODE;
2161 if (out->render_mode == RENDER_MODE_AUDIO_MASTER) {
2162 metadata.value[0] = SNDRV_COMPRESS_RENDER_MODE_AUDIO_MASTER;
2163 } else if (out->render_mode == RENDER_MODE_AUDIO_STC_MASTER) {
2164 metadata.value[0] = SNDRV_COMPRESS_RENDER_MODE_STC_MASTER;
2165 } else {
2166 ret = 0;
2167 goto exit;
2168 }
2169 ret = compress_set_metadata(out->compr, &metadata);
2170 if(ret) {
2171 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2172 }
2173exit:
2174 return ret;
2175}
2176#else
2177int audio_extn_utils_compress_set_render_mode(struct stream_out *out __unused)
2178{
2179 ALOGD("%s:: configuring render mode not supported", __func__);
2180 return 0;
2181}
2182#endif
Manish Dewangan58229382017-02-02 15:48:41 +05302183
2184#ifdef SNDRV_COMPRESS_CLK_REC_MODE
2185int audio_extn_utils_compress_set_clk_rec_mode(
2186 struct audio_usecase *usecase)
2187{
2188 struct snd_compr_metadata metadata;
2189 struct stream_out *out = NULL;
2190 int ret = -EINVAL;
2191
Naresh Tanniru7586e292017-04-13 10:38:13 +05302192 if (usecase == NULL || usecase->type != PCM_PLAYBACK) {
Manish Dewangan58229382017-02-02 15:48:41 +05302193 ALOGE("%s:: Invalid use case", __func__);
2194 goto exit;
2195 }
2196
2197 out = usecase->stream.out;
2198 if (!out) {
2199 ALOGE("%s:: invalid stream", __func__);
2200 goto exit;
2201 }
2202
2203 if (!is_offload_usecase(out->usecase)) {
2204 ALOGE("%s:: not supported for non offload session", __func__);
2205 goto exit;
2206 }
2207
2208 if (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER) {
2209 ALOGD("%s:: clk recovery is only supported in STC render mode",
2210 __func__);
2211 ret = 0;
2212 goto exit;
2213 }
2214
2215 if (!out->compr) {
2216 ALOGD("%s:: Invalid compress handle",
2217 __func__);
2218 goto exit;
2219 }
2220 metadata.key = SNDRV_COMPRESS_CLK_REC_MODE;
2221 switch(usecase->out_snd_device) {
2222 case SND_DEVICE_OUT_HDMI:
2223 case SND_DEVICE_OUT_SPEAKER_AND_HDMI:
2224 case SND_DEVICE_OUT_DISPLAY_PORT:
2225 case SND_DEVICE_OUT_SPEAKER_AND_DISPLAY_PORT:
2226 metadata.value[0] = SNDRV_COMPRESS_CLK_REC_MODE_NONE;
2227 break;
2228 default:
2229 metadata.value[0] = SNDRV_COMPRESS_CLK_REC_MODE_AUTO;
2230 break;
2231 }
2232
2233 ALOGD("%s:: clk recovery mode %d",__func__, metadata.value[0]);
2234
2235 ret = compress_set_metadata(out->compr, &metadata);
2236 if(ret) {
2237 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2238 }
2239
2240exit:
2241 return ret;
2242}
2243#else
2244int audio_extn_utils_compress_set_clk_rec_mode(
2245 struct audio_usecase *usecase __unused)
2246{
2247 ALOGD("%s:: configuring render mode not supported", __func__);
2248 return 0;
2249}
2250#endif
Manish Dewangan27346042017-03-01 12:56:12 +05302251
2252#ifdef SNDRV_COMPRESS_RENDER_WINDOW
2253int audio_extn_utils_compress_set_render_window(
2254 struct stream_out *out,
2255 struct audio_out_render_window_param *render_window)
2256{
2257 struct snd_compr_metadata metadata;
2258 int ret = -EINVAL;
2259
Manish Dewangan27346042017-03-01 12:56:12 +05302260 if(render_window == NULL) {
2261 ALOGE("%s:: Invalid render_window", __func__);
2262 goto exit;
2263 }
2264
Aniket Kumar Lata1e1d3662017-06-01 18:45:48 -07002265 ALOGD("%s:: render window start 0x%"PRIx64" end 0x%"PRIx64"",
2266 __func__,render_window->render_ws, render_window->render_we);
2267
Manish Dewangan27346042017-03-01 12:56:12 +05302268 if (!is_offload_usecase(out->usecase)) {
2269 ALOGE("%s:: not supported for non offload session", __func__);
2270 goto exit;
2271 }
2272
Naresh Tanniru6160c712017-04-17 15:43:48 +05302273 if ((out->render_mode != RENDER_MODE_AUDIO_MASTER) &&
2274 (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER)) {
Manish Dewangan27346042017-03-01 12:56:12 +05302275 ALOGD("%s:: only supported in timestamp mode, current "
2276 "render mode mode %d", __func__, out->render_mode);
2277 goto exit;
2278 }
2279
2280 if (!out->compr) {
2281 ALOGW("%s:: offload session not yet opened,"
2282 "render window will be configure later", __func__);
2283 /* store render window to reconfigure in start_output_stream() */
2284 goto exit;
2285 }
2286
2287 metadata.key = SNDRV_COMPRESS_RENDER_WINDOW;
2288 /*render window start value */
2289 metadata.value[0] = 0xFFFFFFFF & render_window->render_ws; /* lsb */
2290 metadata.value[1] = \
2291 (0xFFFFFFFF00000000 & render_window->render_ws) >> 32; /* msb*/
2292 /*render window end value */
2293 metadata.value[2] = 0xFFFFFFFF & render_window->render_we; /* lsb */
2294 metadata.value[3] = \
2295 (0xFFFFFFFF00000000 & render_window->render_we) >> 32; /* msb*/
2296
2297 ret = compress_set_metadata(out->compr, &metadata);
2298 if(ret) {
2299 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2300 }
2301
2302exit:
2303 return ret;
2304}
2305#else
2306int audio_extn_utils_compress_set_render_window(
2307 struct stream_out *out __unused,
2308 struct audio_out_render_window_param *render_window __unused)
2309{
2310 ALOGD("%s:: configuring render window not supported", __func__);
2311 return 0;
2312}
2313#endif
Manish Dewangan14956cc2017-02-14 18:54:42 +05302314
2315#ifdef SNDRV_COMPRESS_START_DELAY
2316int audio_extn_utils_compress_set_start_delay(
2317 struct stream_out *out,
2318 struct audio_out_start_delay_param *delay_param)
2319{
2320 struct snd_compr_metadata metadata;
2321 int ret = -EINVAL;
2322
2323 if(delay_param == NULL) {
2324 ALOGE("%s:: Invalid delay_param", __func__);
2325 goto exit;
2326 }
2327
2328 ALOGD("%s:: render start delay 0x%"PRIx64" ", __func__,
2329 delay_param->start_delay);
2330
2331 if (!is_offload_usecase(out->usecase)) {
2332 ALOGE("%s:: not supported for non offload session", __func__);
2333 goto exit;
2334 }
2335
Naresh Tanniru6160c712017-04-17 15:43:48 +05302336 if ((out->render_mode != RENDER_MODE_AUDIO_MASTER) &&
2337 (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER)) {
Manish Dewangan14956cc2017-02-14 18:54:42 +05302338 ALOGD("%s:: only supported in timestamp mode, current "
2339 "render mode mode %d", __func__, out->render_mode);
2340 goto exit;
2341 }
2342
2343 if (!out->compr) {
2344 ALOGW("%s:: offload session not yet opened,"
2345 "start delay will be configure later", __func__);
2346 goto exit;
2347 }
2348
2349 metadata.key = SNDRV_COMPRESS_START_DELAY;
2350 metadata.value[0] = 0xFFFFFFFF & delay_param->start_delay; /* lsb */
2351 metadata.value[1] = \
2352 (0xFFFFFFFF00000000 & delay_param->start_delay) >> 32; /* msb*/
2353
2354 ret = compress_set_metadata(out->compr, &metadata);
2355 if(ret) {
2356 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2357 }
2358
2359exit:
2360 return ret;
2361}
2362#else
2363int audio_extn_utils_compress_set_start_delay(
2364 struct stream_out *out __unused,
2365 struct audio_out_start_delay_param *delay_param __unused)
2366{
2367 ALOGD("%s:: configuring render window not supported", __func__);
2368 return 0;
2369}
2370#endif
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002371
Surendar Karka287348c2019-04-10 18:31:46 +05302372#ifdef SNDRV_COMPRESS_DSP_POSITION
2373int audio_extn_utils_compress_get_dsp_presentation_pos(struct stream_out *out,
2374 uint64_t *frames, struct timespec *timestamp, int32_t clock_id)
2375{
2376 int ret = -EINVAL;
2377 uint64_t *val = NULL;
2378 uint64_t time = 0;
2379 struct snd_compr_metadata metadata;
2380
2381 ALOGV("%s:: Quering DSP position with clock id %d",__func__, clock_id);
2382 metadata.key = SNDRV_COMPRESS_DSP_POSITION;
2383 metadata.value[0] = clock_id;
2384 ret = compress_get_metadata(out->compr, &metadata);
2385 if (ret) {
2386 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2387 ret = -errno;
2388 goto exit;
2389 }
2390 val = (uint64_t *)&metadata.value[1];
2391 *frames = *val;
2392 time = *(val + 1);
2393 timestamp->tv_sec = time / 1000000;
2394 timestamp->tv_nsec = (time % 1000000)*1000;
2395
2396exit:
2397 return ret;
2398}
2399#else
2400int audio_extn_utils_compress_get_dsp_presentation_pos(struct stream_out *out __unused,
2401 uint64_t *frames __unused, struct timespec *timestamp __unused,
2402 int32_t clock_id __unused)
2403{
2404 ALOGD("%s:: dsp presentation position not supported", __func__);
2405 return 0;
2406
2407}
2408#endif
2409
2410#ifdef SNDRV_PCM_IOCTL_DSP_POSITION
2411int audio_extn_utils_pcm_get_dsp_presentation_pos(struct stream_out *out,
2412 uint64_t *frames, struct timespec *timestamp, int32_t clock_id)
2413{
2414 int ret = -EINVAL;
2415 uint64_t time = 0;
2416 struct snd_pcm_prsnt_position prsnt_position;
2417
2418 ALOGV("%s:: Quering DSP position with clock id %d",__func__, clock_id);
2419 prsnt_position.clock_id = clock_id;
2420 ret = pcm_ioctl(out->pcm, SNDRV_PCM_IOCTL_DSP_POSITION, &prsnt_position);
2421 if (ret) {
2422 ALOGE("%s::error %d", __func__, ret);
2423 ret = -EIO;
2424 goto exit;
2425 }
2426
2427 *frames = prsnt_position.frames;
2428 time = prsnt_position.timestamp;
2429 timestamp->tv_sec = time / 1000000;
2430 timestamp->tv_nsec = (time % 1000000)*1000;
2431
2432exit:
2433 return ret;
2434}
2435#else
2436int audio_extn_utils_pcm_get_dsp_presentation_pos(struct stream_out *out __unused,
2437 uint64_t *frames __unused, struct timespec *timestamp __unused,
2438 int32_t clock_id __unused)
2439{
2440 ALOGD("%s:: dsp presentation position not supported", __func__);
2441 return 0;
2442
2443}
2444#endif
2445
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002446#define MAX_SND_CARD 8
Ashish Jain30ae8942017-11-05 17:21:23 +05302447#define RETRY_US 1000000
2448#define RETRY_NUMBER 40
Aalique Grahame22e49102018-12-18 14:23:57 -08002449#define PLATFORM_INFO_XML_PATH "audio_platform_info.xml"
2450#define PLATFORM_INFO_XML_BASE_STRING "audio_platform_info"
2451
2452#ifdef LINUX_ENABLED
2453static const char *kConfigLocationList[] =
2454 {"/etc"};
2455#else
2456static const char *kConfigLocationList[] =
2457 {"/vendor/etc"};
2458#endif
2459static const int kConfigLocationListSize =
2460 (sizeof(kConfigLocationList) / sizeof(kConfigLocationList[0]));
2461
2462bool audio_extn_utils_resolve_config_file(char file_name[MIXER_PATH_MAX_LENGTH])
2463{
2464 char full_config_path[MIXER_PATH_MAX_LENGTH];
2465 for (int i = 0; i < kConfigLocationListSize; i++) {
2466 snprintf(full_config_path,
2467 MIXER_PATH_MAX_LENGTH,
2468 "%s/%s",
2469 kConfigLocationList[i],
2470 file_name);
2471 if (F_OK == access(full_config_path, 0)) {
Weiyin Jiang2995f662019-04-17 14:25:12 +08002472 strlcpy(file_name, full_config_path, MIXER_PATH_MAX_LENGTH);
Aalique Grahame22e49102018-12-18 14:23:57 -08002473 return true;
2474 }
2475 }
2476 return false;
2477}
2478
2479/* platform_info_file should be size 'MIXER_PATH_MAX_LENGTH' */
2480int audio_extn_utils_get_platform_info(const char* snd_card_name, char* platform_info_file)
2481{
2482 if (NULL == snd_card_name) {
2483 return -1;
2484 }
2485
2486 struct snd_card_split *snd_split_handle = NULL;
2487 int ret = 0;
2488 audio_extn_set_snd_card_split(snd_card_name);
2489 snd_split_handle = audio_extn_get_snd_card_split();
2490
2491 snprintf(platform_info_file, MIXER_PATH_MAX_LENGTH, "%s_%s_%s.xml",
2492 PLATFORM_INFO_XML_BASE_STRING, snd_split_handle->snd_card,
2493 snd_split_handle->form_factor);
2494
2495 if (!audio_extn_utils_resolve_config_file(platform_info_file)) {
2496 memset(platform_info_file, 0, MIXER_PATH_MAX_LENGTH);
2497 snprintf(platform_info_file, MIXER_PATH_MAX_LENGTH, "%s_%s.xml",
2498 PLATFORM_INFO_XML_BASE_STRING, snd_split_handle->snd_card);
2499
2500 if (!audio_extn_utils_resolve_config_file(platform_info_file)) {
2501 memset(platform_info_file, 0, MIXER_PATH_MAX_LENGTH);
2502 strlcpy(platform_info_file, PLATFORM_INFO_XML_PATH, MIXER_PATH_MAX_LENGTH);
2503 ret = audio_extn_utils_resolve_config_file(platform_info_file) ? 0 : -1;
2504 }
2505 }
2506
2507 return ret;
2508}
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002509
2510int audio_extn_utils_get_snd_card_num()
2511{
Soumya Managoli9fee7c62018-04-06 16:21:50 +05302512 int snd_card_num = 0;
2513 struct mixer *mixer = NULL;
2514
2515 snd_card_num = audio_extn_utils_open_snd_mixer(&mixer);
2516 if (mixer)
2517 mixer_close(mixer);
2518 return snd_card_num;
2519}
2520
2521int audio_extn_utils_open_snd_mixer(struct mixer **mixer_handle)
2522{
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002523
2524 void *hw_info = NULL;
2525 struct mixer *mixer = NULL;
2526 int retry_num = 0;
mpangfaa7bae2019-01-15 16:38:13 +08002527 int snd_card_num = 0;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002528 char* snd_card_name = NULL;
mpangfaa7bae2019-01-15 16:38:13 +08002529 int snd_card_detection_info[MAX_SND_CARD] = {0};
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002530
Soumya Managoli9fee7c62018-04-06 16:21:50 +05302531 if (!mixer_handle) {
2532 ALOGE("invalid mixer handle");
2533 return -1;
2534 }
2535 *mixer_handle = NULL;
Ashish Jain30ae8942017-11-05 17:21:23 +05302536 /*
mpangfaa7bae2019-01-15 16:38:13 +08002537 * Try with all the sound cards ( 0 to 7 ) and if none of them were detected
Ashish Jain30ae8942017-11-05 17:21:23 +05302538 * sleep for 1 sec and try detections with sound card 0 again.
2539 * If sound card gets detected, check if it is relevant, if not check with the
2540 * other sound cards. To ensure that the irrelevant sound card is not check again,
2541 * we maintain it in min_snd_card_num.
2542 */
2543 while (retry_num < RETRY_NUMBER) {
mpangfaa7bae2019-01-15 16:38:13 +08002544 snd_card_num = 0;
2545 while (snd_card_num < MAX_SND_CARD) {
2546 if (snd_card_detection_info[snd_card_num] == 0) {
2547 mixer = mixer_open(snd_card_num);
2548 if (!mixer)
2549 snd_card_num++;
2550 else
2551 break;
2552 } else
2553 snd_card_num++;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002554 }
2555
2556 if (!mixer) {
Ashish Jain30ae8942017-11-05 17:21:23 +05302557 usleep(RETRY_US);
Ashish Jain30ae8942017-11-05 17:21:23 +05302558 retry_num++;
mpangfaa7bae2019-01-15 16:38:13 +08002559 ALOGD("%s: retry, retry_num %d", __func__, retry_num);
Ashish Jain30ae8942017-11-05 17:21:23 +05302560 continue;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002561 }
2562
2563 snd_card_name = strdup(mixer_get_name(mixer));
2564 if (!snd_card_name) {
2565 ALOGE("failed to allocate memory for snd_card_name\n");
2566 mixer_close(mixer);
2567 return -1;
2568 }
2569 ALOGD("%s: snd_card_name: %s", __func__, snd_card_name);
mpangfaa7bae2019-01-15 16:38:13 +08002570 snd_card_detection_info[snd_card_num] = 1;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002571 hw_info = hw_info_init(snd_card_name);
2572 if (hw_info) {
2573 ALOGD("%s: Opened sound card:%d", __func__, snd_card_num);
2574 break;
2575 }
mpangfaa7bae2019-01-15 16:38:13 +08002576 ALOGE("%s: Failed to init hardware info, snd_card_num:%d", __func__, snd_card_num);
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002577
Dhananjay Kumara7e27832017-07-11 15:40:39 +05302578 free(snd_card_name);
2579 snd_card_name = NULL;
2580
2581 mixer_close(mixer);
2582 mixer = NULL;
2583 }
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002584 if (snd_card_name)
2585 free(snd_card_name);
Ashish Jain30ae8942017-11-05 17:21:23 +05302586
Dhananjay Kumara7e27832017-07-11 15:40:39 +05302587 if (hw_info)
2588 hw_info_deinit(hw_info);
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002589
mpangfaa7bae2019-01-15 16:38:13 +08002590 if (retry_num >= RETRY_NUMBER) {
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002591 ALOGE("%s: Unable to find correct sound card, aborting.", __func__);
2592 return -1;
2593 }
2594
Soumya Managoli9fee7c62018-04-06 16:21:50 +05302595 if (mixer)
2596 *mixer_handle = mixer;
2597
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002598 return snd_card_num;
2599}
Naresh Tanniru6160c712017-04-17 15:43:48 +05302600
Soumya Managoli9fee7c62018-04-06 16:21:50 +05302601void audio_extn_utils_close_snd_mixer(struct mixer *mixer)
2602{
2603 if (mixer)
2604 mixer_close(mixer);
2605}
2606
Naresh Tanniru6160c712017-04-17 15:43:48 +05302607#ifdef SNDRV_COMPRESS_ENABLE_ADJUST_SESSION_CLOCK
2608int audio_extn_utils_compress_enable_drift_correction(
2609 struct stream_out *out,
2610 struct audio_out_enable_drift_correction *drift)
2611{
2612 struct snd_compr_metadata metadata;
2613 int ret = -EINVAL;
2614
2615 if(drift == NULL) {
2616 ALOGE("%s:: Invalid param", __func__);
2617 goto exit;
2618 }
2619
2620 ALOGD("%s:: drift enable %d", __func__,drift->enable);
2621
2622 if (!is_offload_usecase(out->usecase)) {
2623 ALOGE("%s:: not supported for non offload session", __func__);
2624 goto exit;
2625 }
2626
2627 if (!out->compr) {
2628 ALOGW("%s:: offload session not yet opened,"
2629 "start delay will be configure later", __func__);
2630 goto exit;
2631 }
2632
2633 metadata.key = SNDRV_COMPRESS_ENABLE_ADJUST_SESSION_CLOCK;
2634 metadata.value[0] = drift->enable;
2635 out->drift_correction_enabled = drift->enable;
2636
2637 ret = compress_set_metadata(out->compr, &metadata);
2638 if(ret) {
2639 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2640 out->drift_correction_enabled = false;
2641 }
2642
2643exit:
2644 return ret;
2645}
2646#else
2647int audio_extn_utils_compress_enable_drift_correction(
2648 struct stream_out *out __unused,
2649 struct audio_out_enable_drift_correction *drift __unused)
2650{
2651 ALOGD("%s:: configuring drift enablement not supported", __func__);
2652 return 0;
2653}
2654#endif
2655
2656#ifdef SNDRV_COMPRESS_ADJUST_SESSION_CLOCK
2657int audio_extn_utils_compress_correct_drift(
2658 struct stream_out *out,
2659 struct audio_out_correct_drift *drift_param)
2660{
2661 struct snd_compr_metadata metadata;
2662 int ret = -EINVAL;
2663
2664 if (drift_param == NULL) {
2665 ALOGE("%s:: Invalid drift_param", __func__);
2666 goto exit;
2667 }
2668
2669 ALOGD("%s:: adjust time 0x%"PRIx64" ", __func__,
2670 drift_param->adjust_time);
2671
2672 if (!is_offload_usecase(out->usecase)) {
2673 ALOGE("%s:: not supported for non offload session", __func__);
2674 goto exit;
2675 }
2676
2677 if (!out->compr) {
2678 ALOGW("%s:: offload session not yet opened", __func__);
2679 goto exit;
2680 }
2681
2682 if (!out->drift_correction_enabled) {
2683 ALOGE("%s:: drift correction not enabled", __func__);
2684 goto exit;
2685 }
2686
2687 metadata.key = SNDRV_COMPRESS_ADJUST_SESSION_CLOCK;
2688 metadata.value[0] = 0xFFFFFFFF & drift_param->adjust_time; /* lsb */
2689 metadata.value[1] = \
2690 (0xFFFFFFFF00000000 & drift_param->adjust_time) >> 32; /* msb*/
2691
2692 ret = compress_set_metadata(out->compr, &metadata);
2693 if(ret)
2694 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2695exit:
2696 return ret;
2697}
2698#else
2699int audio_extn_utils_compress_correct_drift(
2700 struct stream_out *out __unused,
2701 struct audio_out_correct_drift *drift_param __unused)
2702{
2703 ALOGD("%s:: setting adjust clock not supported", __func__);
2704 return 0;
2705}
2706#endif
Naresh Tanniru29bce4e2017-04-27 17:54:30 +05302707
2708int audio_extn_utils_set_channel_map(
2709 struct stream_out *out,
2710 struct audio_out_channel_map_param *channel_map_param)
2711{
2712 int ret = -EINVAL, i = 0;
2713 int channels = audio_channel_count_from_out_mask(out->channel_mask);
2714
2715 if (channel_map_param == NULL) {
2716 ALOGE("%s:: Invalid channel_map", __func__);
2717 goto exit;
2718 }
2719
2720 if (channel_map_param->channels != channels) {
2721 ALOGE("%s:: Channels(%d) does not match stream channels(%d)",
2722 __func__, channel_map_param->channels, channels);
2723 goto exit;
2724 }
2725
2726 for ( i = 0; i < channels; i++) {
2727 ALOGV("%s:: channel_map[%d]- %d", __func__, i, channel_map_param->channel_map[i]);
2728 out->channel_map_param.channel_map[i] = channel_map_param->channel_map[i];
2729 }
2730 ret = 0;
2731exit:
2732 return ret;
2733}
Varun Balaraje49253e2017-07-06 19:48:56 +05302734
2735int audio_extn_utils_set_pan_scale_params(
2736 struct stream_out *out,
2737 struct mix_matrix_params *mm_params)
2738{
2739 int ret = -EINVAL, i = 0, j = 0;
2740
Varun Balaraj003ee752017-08-07 17:54:55 +05302741 if (mm_params == NULL || out == NULL) {
2742 ALOGE("%s:: Invalid mix matrix or out param", __func__);
Varun Balaraje49253e2017-07-06 19:48:56 +05302743 goto exit;
2744 }
2745
2746 if (mm_params->num_output_channels > MAX_CHANNELS_SUPPORTED ||
2747 mm_params->num_output_channels <= 0 ||
2748 mm_params->num_input_channels > MAX_CHANNELS_SUPPORTED ||
2749 mm_params->num_input_channels <= 0)
2750 goto exit;
2751
2752 out->pan_scale_params.num_output_channels = mm_params->num_output_channels;
2753 out->pan_scale_params.num_input_channels = mm_params->num_input_channels;
2754 out->pan_scale_params.has_output_channel_map =
2755 mm_params->has_output_channel_map;
2756 for (i = 0; i < mm_params->num_output_channels; i++)
2757 out->pan_scale_params.output_channel_map[i] =
2758 mm_params->output_channel_map[i];
2759
2760 out->pan_scale_params.has_input_channel_map =
2761 mm_params->has_input_channel_map;
2762 for (i = 0; i < mm_params->num_input_channels; i++)
2763 out->pan_scale_params.input_channel_map[i] =
2764 mm_params->input_channel_map[i];
2765
2766 out->pan_scale_params.has_mixer_coeffs = mm_params->has_mixer_coeffs;
2767 for (i = 0; i < mm_params->num_output_channels; i++)
2768 for (j = 0; j < mm_params->num_input_channels; j++) {
2769 //Convert the channel coefficient gains in Q14 format
2770 out->pan_scale_params.mixer_coeffs[i][j] =
2771 mm_params->mixer_coeffs[i][j] * (2 << 13);
2772 }
2773
2774 ret = platform_set_stream_pan_scale_params(out->dev->platform,
2775 out->pcm_device_id,
2776 out->pan_scale_params);
2777
2778exit:
2779 return ret;
2780}
2781
2782int audio_extn_utils_set_downmix_params(
2783 struct stream_out *out,
2784 struct mix_matrix_params *mm_params)
2785{
2786 int ret = -EINVAL, i = 0, j = 0;
2787 struct audio_usecase *usecase = NULL;
2788
Aniket Kumar Lataf9f246e2017-09-15 15:20:16 -07002789 if (mm_params == NULL || out == NULL) {
Varun Balaraj003ee752017-08-07 17:54:55 +05302790 ALOGE("%s:: Invalid mix matrix or out param", __func__);
Varun Balaraje49253e2017-07-06 19:48:56 +05302791 goto exit;
2792 }
2793
2794 if (mm_params->num_output_channels > MAX_CHANNELS_SUPPORTED ||
2795 mm_params->num_output_channels <= 0 ||
2796 mm_params->num_input_channels > MAX_CHANNELS_SUPPORTED ||
2797 mm_params->num_input_channels <= 0)
2798 goto exit;
2799
2800 usecase = get_usecase_from_list(out->dev, out->usecase);
Varun Balaraj003ee752017-08-07 17:54:55 +05302801 if (!usecase) {
2802 ALOGE("%s: Get usecase list failed!", __func__);
Aniket Kumar Lataf9f246e2017-09-15 15:20:16 -07002803 goto exit;
2804 }
Varun Balaraje49253e2017-07-06 19:48:56 +05302805 out->downmix_params.num_output_channels = mm_params->num_output_channels;
2806 out->downmix_params.num_input_channels = mm_params->num_input_channels;
2807
2808 out->downmix_params.has_output_channel_map =
2809 mm_params->has_output_channel_map;
2810 for (i = 0; i < mm_params->num_output_channels; i++) {
2811 out->downmix_params.output_channel_map[i] =
2812 mm_params->output_channel_map[i];
2813 }
2814
2815 out->downmix_params.has_input_channel_map =
2816 mm_params->has_input_channel_map;
2817 for (i = 0; i < mm_params->num_input_channels; i++)
2818 out->downmix_params.input_channel_map[i] =
2819 mm_params->input_channel_map[i];
2820
2821 out->downmix_params.has_mixer_coeffs = mm_params->has_mixer_coeffs;
2822 for (i = 0; i < mm_params->num_output_channels; i++)
Nikhil Latukar2e6f6242017-08-15 13:37:07 +05302823 for (j = 0; j < mm_params->num_input_channels; j++) {
2824 //Convert the channel coefficient gains in Q14 format
Varun Balaraje49253e2017-07-06 19:48:56 +05302825 out->downmix_params.mixer_coeffs[i][j] =
Nikhil Latukar2e6f6242017-08-15 13:37:07 +05302826 mm_params->mixer_coeffs[i][j] * (2 << 13);
2827 }
Varun Balaraje49253e2017-07-06 19:48:56 +05302828
2829 ret = platform_set_stream_downmix_params(out->dev->platform,
2830 out->pcm_device_id,
2831 usecase->out_snd_device,
2832 out->downmix_params);
2833
2834exit:
2835 return ret;
2836}
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05302837
2838bool audio_extn_utils_is_dolby_format(audio_format_t format)
2839{
2840 if (format == AUDIO_FORMAT_AC3 ||
2841 format == AUDIO_FORMAT_E_AC3 ||
2842 format == AUDIO_FORMAT_E_AC3_JOC)
2843 return true;
2844 else
2845 return false;
2846}
2847
`Deeraj Soman676c2702017-09-18 19:25:53 +05302848int audio_extn_utils_get_bit_width_from_string(const char *id_string)
2849{
2850 int i;
2851 const mixer_config_lookup mixer_bitwidth_config[] = {{"S24_3LE", 24},
2852 {"S32_LE", 32},
2853 {"S24_LE", 24},
2854 {"S16_LE", 16}};
2855 int num_configs = sizeof(mixer_bitwidth_config) / sizeof(mixer_bitwidth_config[0]);
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05302856
`Deeraj Soman676c2702017-09-18 19:25:53 +05302857 for (i = 0; i < num_configs; i++) {
2858 if (!strcmp(id_string, mixer_bitwidth_config[i].id_string))
2859 return mixer_bitwidth_config[i].value;
2860 }
2861
2862 return -EINVAL;
2863}
2864
2865int audio_extn_utils_get_sample_rate_from_string(const char *id_string)
2866{
2867 int i;
2868 const mixer_config_lookup mixer_samplerate_config[] = {{"KHZ_32", 32000},
2869 {"KHZ_48", 48000},
2870 {"KHZ_96", 96000},
2871 {"KHZ_144", 144000},
2872 {"KHZ_192", 192000},
2873 {"KHZ_384", 384000},
2874 {"KHZ_44P1", 44100},
2875 {"KHZ_88P2", 88200},
2876 {"KHZ_176P4", 176400},
2877 {"KHZ_352P8", 352800}};
2878 int num_configs = sizeof(mixer_samplerate_config) / sizeof(mixer_samplerate_config[0]);
2879
2880 for (i = 0; i < num_configs; i++) {
2881 if (!strcmp(id_string, mixer_samplerate_config[i].id_string))
2882 return mixer_samplerate_config[i].value;
2883 }
2884
2885 return -EINVAL;
2886}
2887
2888int audio_extn_utils_get_channels_from_string(const char *id_string)
2889{
2890 int i;
2891 const mixer_config_lookup mixer_channels_config[] = {{"One", 1},
2892 {"Two", 2},
2893 {"Three",3},
2894 {"Four", 4},
2895 {"Five", 5},
2896 {"Six", 6},
2897 {"Seven", 7},
2898 {"Eight", 8}};
2899 int num_configs = sizeof(mixer_channels_config) / sizeof(mixer_channels_config[0]);
2900
2901 for (i = 0; i < num_configs; i++) {
2902 if (!strcmp(id_string, mixer_channels_config[i].id_string))
2903 return mixer_channels_config[i].value;
2904 }
2905
2906 return -EINVAL;
2907}
Surendar karka30569792018-05-08 12:02:21 +05302908
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08002909void audio_extn_utils_release_snd_device(snd_device_t snd_device)
2910{
2911 audio_extn_dev_arbi_release(snd_device);
2912 audio_extn_sound_trigger_update_device_status(snd_device,
2913 ST_EVENT_SND_DEVICE_FREE);
2914 audio_extn_listen_update_device_status(snd_device,
2915 LISTEN_EVENT_SND_DEVICE_FREE);
2916}
2917
Surendar karka30569792018-05-08 12:02:21 +05302918int audio_extn_utils_get_license_params
2919(
2920const struct audio_device *adev,
2921struct audio_license_params *license_params
2922)
2923{
2924 if(!license_params)
2925 return -EINVAL;
2926 return platform_get_license_by_product(adev->platform, (const char*)license_params->product, &license_params->key, license_params->license);
2927}
2928
Aalique Grahame22e49102018-12-18 14:23:57 -08002929int audio_extn_utils_send_app_type_gain(struct audio_device *adev,
2930 int app_type,
2931 int *gain)
2932{
2933 int gain_cfg[4];
2934 const char *mixer_ctl_name = "App Type Gain";
2935 struct mixer_ctl *ctl;
2936 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
2937 if (!ctl) {
2938 ALOGE("%s: Could not get volume ctl mixer %s", __func__,
2939 mixer_ctl_name);
2940 return -EINVAL;
2941 }
2942 gain_cfg[0] = 0;
2943 gain_cfg[1] = app_type;
2944 gain_cfg[2] = gain[0];
2945 gain_cfg[3] = gain[1];
2946 ALOGV("%s app_type %d l(%d) r(%d)", __func__, app_type, gain[0], gain[1]);
2947 return mixer_ctl_set_array(ctl, gain_cfg,
2948 sizeof(gain_cfg)/sizeof(gain_cfg[0]));
2949}
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -07002950
2951int audio_extn_utils_is_vendor_enhanced_fwk()
2952{
2953 static int is_running_with_enhanced_fwk = -EINVAL;
2954
2955 if (is_running_with_enhanced_fwk == -EINVAL) {
2956 vndk_fwk_lib_handle = dlopen(VNDK_FWK_LIB_PATH, RTLD_NOW);
2957 if (vndk_fwk_lib_handle != NULL) {
2958 vndk_fwk_isVendorEnhancedFwk = (vndk_fwk_isVendorEnhancedFwk_t)
2959 dlsym(vndk_fwk_lib_handle, "isRunningWithVendorEnhancedFramework");
2960 if (vndk_fwk_isVendorEnhancedFwk == NULL) {
2961 ALOGW("%s: dlsym failed, defaulting to enhanced_fwk configuration",
2962 __func__);
2963 is_running_with_enhanced_fwk = 1;
2964 } else {
2965 is_running_with_enhanced_fwk = vndk_fwk_isVendorEnhancedFwk();
2966 }
2967 dlclose(vndk_fwk_lib_handle);
2968 vndk_fwk_lib_handle = NULL;
2969 } else {
2970 ALOGW("%s: VNDK_FWK_LIB not found, setting stock configuration", __func__);
2971 is_running_with_enhanced_fwk = 0;
2972 }
2973 ALOGV("%s: vndk_fwk_isVendorEnhancedFwk=%d", __func__, is_running_with_enhanced_fwk);
2974 }
2975
2976 return is_running_with_enhanced_fwk;
2977}
Deeraj Soman14230922019-01-30 16:39:30 +05302978
Deeraj Somanfa377bf2019-02-06 12:57:59 +05302979int audio_extn_utils_get_perf_mode_flag(void)
2980{
2981#ifdef COMPRESSED_PERF_MODE_FLAG
2982 return COMPRESSED_PERF_MODE_FLAG;
2983#else
2984 return 0;
2985#endif
2986}
2987
Deeraj Soman14230922019-01-30 16:39:30 +05302988size_t audio_extn_utils_get_input_buffer_size(uint32_t sample_rate,
2989 audio_format_t format,
2990 int channel_count,
2991 int64_t duration_ms,
2992 bool is_low_latency)
2993{
2994 size_t size = 0;
2995 size_t capture_duration = AUDIO_CAPTURE_PERIOD_DURATION_MSEC;
2996 uint32_t bytes_per_period_sample = 0;
2997
2998
2999 if (audio_extn_utils_check_input_parameters(sample_rate, format, channel_count) != 0)
3000 return 0;
3001
3002 if (duration_ms >= MIN_OFFLOAD_BUFFER_DURATION_MS && duration_ms <= MAX_OFFLOAD_BUFFER_DURATION_MS)
3003 capture_duration = duration_ms;
3004
3005 size = (sample_rate * capture_duration) / 1000;
3006 if (is_low_latency)
3007 size = LOW_LATENCY_CAPTURE_PERIOD_SIZE;
3008
3009
3010 bytes_per_period_sample = audio_bytes_per_sample(format) * channel_count;
3011 size *= bytes_per_period_sample;
3012
3013 /* make sure the size is multiple of 32 bytes and additionally multiple of
3014 * the frame_size (required for 24bit samples and non-power-of-2 channel counts)
3015 * At 48 kHz mono 16-bit PCM:
3016 * 5.000 ms = 240 frames = 15*16*1*2 = 480, a whole multiple of 32 (15)
3017 * 3.333 ms = 160 frames = 10*16*1*2 = 320, a whole multiple of 32 (10)
3018 *
3019 * The loop reaches result within 32 iterations, as initial size is
3020 * already a multiple of frame_size
3021 */
3022 size = audio_extn_utils_nearest_multiple(size, audio_extn_utils_lcm(32, bytes_per_period_sample));
3023
3024 return size;
3025}