blob: b15f7e744f4f2eaff2c492a079b09bb52724440c [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) &&
Vignesh Kulothungan0a2eff02019-07-11 16:30:13 -07001143 (usecase->id != USECASE_AUDIO_PLAYBACK_WITH_HAPTICS) &&
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001144 (usecase->id != USECASE_AUDIO_PLAYBACK_LOW_LATENCY) &&
1145 (usecase->id != USECASE_AUDIO_PLAYBACK_MULTI_CH) &&
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -08001146 (usecase->id != USECASE_AUDIO_PLAYBACK_ULL) &&
Vikram Panduranga93f080e2017-06-07 18:16:14 -07001147 (usecase->id != USECASE_AUDIO_PLAYBACK_VOIP) &&
Surendar Karka93cd25a2018-08-28 14:21:37 +05301148 (usecase->id != USECASE_AUDIO_TRANSCODE_LOOPBACK_RX) &&
Varun Balaraje49253e2017-07-06 19:48:56 +05301149 (!is_interactive_usecase(usecase->id)) &&
Srikanth Uyyala9d551402015-08-25 16:03:42 +05301150 (!is_offload_usecase(usecase->id)) &&
Derek Chenf6318be2017-06-12 17:16:24 -04001151 (usecase->type != PCM_CAPTURE) &&
1152 (!audio_extn_auto_hal_is_bus_device_usecase(usecase->id))) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301153 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 -07001154 rc = 0;
1155 goto exit_send_app_type_cfg;
1156 }
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -08001157
1158 //if VR is active then only send the mixer control
1159 if (usecase->id == USECASE_AUDIO_PLAYBACK_ULL && !audio_is_vr_mode_on(adev)) {
1160 ALOGI("ULL doesnt need sending app type cfg, returning");
1161 rc = 0;
1162 goto exit_send_app_type_cfg;
1163 }
1164
Surendar Karka93cd25a2018-08-28 14:21:37 +05301165 if (usecase->type == PCM_PLAYBACK || usecase->type == TRANSCODE_LOOPBACK_RX) {
Ben Romberger1fafdde2015-09-09 19:43:15 -07001166 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_PLAYBACK);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301167 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
1168 "Audio Stream %d App Type Cfg", pcm_device_id);
Ben Romberger1fafdde2015-09-09 19:43:15 -07001169 } else if (usecase->type == PCM_CAPTURE) {
Ben Romberger1fafdde2015-09-09 19:43:15 -07001170 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_CAPTURE);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301171 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
1172 "Audio Stream Capture %d App Type Cfg", pcm_device_id);
Srikanth Uyyala9d551402015-08-25 16:03:42 +05301173 }
Siena Richard7c2db772016-12-21 11:32:34 -08001174
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001175 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
1176 if (!ctl) {
1177 ALOGE("%s: Could not get ctl for mixer cmd - %s", __func__,
1178 mixer_ctl_name);
1179 rc = -EINVAL;
1180 goto exit_send_app_type_cfg;
1181 }
Aditya Bavanari701a6992017-03-30 19:17:16 +05301182 snd_device = platform_get_spkr_prot_snd_device(snd_device);
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301183 if (voice_is_in_call_rec_stream(usecase->stream.in) && usecase->type == PCM_CAPTURE) {
1184 snd_device_t voice_device = voice_get_incall_rec_snd_device(usecase->in_snd_device);
1185 acdb_dev_id = platform_get_snd_device_acdb_id(voice_device);
1186 ALOGV("acdb id for voice call use case %d", acdb_dev_id);
1187 } else {
1188 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
1189 }
Rohit Kumar1181b292017-01-31 18:07:17 +05301190 if (acdb_dev_id <= 0) {
1191 ALOGE("%s: Couldn't get the acdb dev id", __func__);
1192 rc = -EINVAL;
1193 goto exit_send_app_type_cfg;
1194 }
1195
Siena Richard7c2db772016-12-21 11:32:34 -08001196 snd_device_be_idx = platform_get_snd_device_backend_index(snd_device);
1197 if (snd_device_be_idx < 0) {
1198 ALOGE("%s: Couldn't get the backend index for snd device %s ret=%d",
1199 __func__, platform_get_snd_device_name(snd_device),
1200 snd_device_be_idx);
1201 }
1202
Zhou Song06761dd2019-04-28 18:08:17 +08001203 sample_rate = audio_extn_utils_get_app_sample_rate_for_device(adev, usecase, snd_device);
1204
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301205 if ((usecase->type == PCM_PLAYBACK) && (usecase->stream.out != NULL)) {
Varun Balaraje49253e2017-07-06 19:48:56 +05301206 /* Interactive streams are supported with only direct app type id.
1207 * Get Direct profile app type and use it for interactive streams
1208 */
1209 list_for_each(node, &adev->streams_output_cfg_list) {
1210 s_info = node_to_item(node, struct streams_io_cfg, list);
Aalique Grahame5cd12ff2017-10-19 10:57:00 -07001211 if (s_info->flags.out_flags == (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_BD |
Nikhil Laturkarac4a7492017-08-31 18:27:19 +05301212 AUDIO_OUTPUT_FLAG_DIRECT_PCM |
1213 AUDIO_OUTPUT_FLAG_DIRECT))
1214 bd_app_type = s_info->app_type_cfg.app_type;
Varun Balaraje49253e2017-07-06 19:48:56 +05301215 }
Aalique Grahame5cd12ff2017-10-19 10:57:00 -07001216 if (usecase->stream.out->flags == (audio_output_flags_t)AUDIO_OUTPUT_FLAG_INTERACTIVE)
Nikhil Laturkarac4a7492017-08-31 18:27:19 +05301217 app_type = bd_app_type;
Varun Balaraje49253e2017-07-06 19:48:56 +05301218 else
1219 app_type = usecase->stream.out->app_type_cfg.app_type;
Siena Richard7c2db772016-12-21 11:32:34 -08001220 app_type_cfg[len++] = app_type;
Mingming Yin21854652016-04-13 11:54:02 -07001221 app_type_cfg[len++] = acdb_dev_id;
Naresh Tanniru3a406772017-05-10 13:09:05 -07001222 app_type_cfg[len++] = sample_rate;
1223
Siena Richard7c2db772016-12-21 11:32:34 -08001224 if (snd_device_be_idx > 0)
1225 app_type_cfg[len++] = snd_device_be_idx;
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301226
Siena Richard7c2db772016-12-21 11:32:34 -08001227 ALOGI("%s PLAYBACK app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
1228 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301229
1230 } else if ((usecase->type == PCM_CAPTURE) && (usecase->stream.in != NULL)) {
Siena Richard7c2db772016-12-21 11:32:34 -08001231 app_type = usecase->stream.in->app_type_cfg.app_type;
1232 app_type_cfg[len++] = app_type;
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301233 app_type_cfg[len++] = acdb_dev_id;
Siena Richard7c2db772016-12-21 11:32:34 -08001234 app_type_cfg[len++] = sample_rate;
1235 if (snd_device_be_idx > 0)
1236 app_type_cfg[len++] = snd_device_be_idx;
1237 ALOGI("%s CAPTURE app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
1238 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301239 } else {
Siena Richard7c2db772016-12-21 11:32:34 -08001240 app_type = platform_get_default_app_type_v2(adev->platform, usecase->type);
Surendar Karka93cd25a2018-08-28 14:21:37 +05301241 if(usecase->type == TRANSCODE_LOOPBACK_RX) {
Siddartha Shaik343abc62017-08-08 11:15:25 +05301242 app_type = usecase->stream.inout->out_app_type_cfg.app_type;
1243 }
Siena Richard7c2db772016-12-21 11:32:34 -08001244 app_type_cfg[len++] = app_type;
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301245 app_type_cfg[len++] = acdb_dev_id;
1246 app_type_cfg[len++] = sample_rate;
Siena Richard7c2db772016-12-21 11:32:34 -08001247 if (snd_device_be_idx > 0)
1248 app_type_cfg[len++] = snd_device_be_idx;
1249 ALOGI("%s default app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
1250 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Ashish Jainc02430d2016-01-04 10:42:43 +05301251 }
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301252
Siddartha Shaik343abc62017-08-08 11:15:25 +05301253 if(ctl)
1254 mixer_ctl_set_array(ctl, app_type_cfg, len);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001255 rc = 0;
1256exit_send_app_type_cfg:
1257 return rc;
1258}
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001259
Deeraj Soman14230922019-01-30 16:39:30 +05301260static int audio_extn_utils_check_input_parameters(uint32_t sample_rate,
1261 audio_format_t format,
1262 int channel_count)
1263{
1264 int ret = 0;
1265
1266 if (((format != AUDIO_FORMAT_PCM_16_BIT) && (format != AUDIO_FORMAT_PCM_8_24_BIT) &&
1267 (format != AUDIO_FORMAT_PCM_24_BIT_PACKED) && (format != AUDIO_FORMAT_PCM_32_BIT) &&
1268 (format != AUDIO_FORMAT_PCM_FLOAT)) &&
1269 !voice_extn_compress_voip_is_format_supported(format) &&
1270 !audio_extn_compr_cap_format_supported(format) &&
1271 !audio_extn_cin_format_supported(format))
1272 ret = -EINVAL;
1273
1274 switch (channel_count) {
1275 case 1:
1276 case 2:
1277 case 3:
1278 case 4:
1279 case 6:
1280 case 8:
1281 break;
1282 default:
1283 ret = -EINVAL;
1284 }
1285
1286 switch (sample_rate) {
1287 case 8000:
1288 case 11025:
1289 case 12000:
1290 case 16000:
1291 case 22050:
1292 case 24000:
1293 case 32000:
1294 case 44100:
1295 case 48000:
1296 case 88200:
1297 case 96000:
1298 case 176400:
1299 case 192000:
1300 break;
1301 default:
1302 ret = -EINVAL;
1303 }
1304
1305 return ret;
1306}
1307
1308static inline uint32_t audio_extn_utils_nearest_multiple(uint32_t num, uint32_t multiplier)
1309{
1310 uint32_t remainder = 0;
1311
1312 if (!multiplier)
1313 return num;
1314
1315 remainder = num % multiplier;
1316 if (remainder)
1317 num += (multiplier - remainder);
1318
1319 return num;
1320}
1321
1322static inline uint32_t audio_extn_utils_lcm(uint32_t num1, uint32_t num2)
1323{
1324 uint32_t high = num1, low = num2, temp = 0;
1325
1326 if (!num1 || !num2)
1327 return 0;
1328
1329 if (num1 < num2) {
1330 high = num2;
1331 low = num1;
1332 }
1333
1334 while (low != 0) {
1335 temp = low;
1336 low = high % low;
1337 high = temp;
1338 }
1339 return (num1 * num2)/high;
1340}
1341
Siena Richard7c2db772016-12-21 11:32:34 -08001342int audio_extn_utils_send_app_type_cfg(struct audio_device *adev,
1343 struct audio_usecase *usecase)
1344{
1345 int i, num_devices = 0;
1346 snd_device_t new_snd_devices[SND_DEVICE_OUT_END] = {0};
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301347 snd_device_t in_snd_device = usecase->in_snd_device;
Siena Richard7c2db772016-12-21 11:32:34 -08001348 int rc = 0;
1349
Aalique Grahame22e49102018-12-18 14:23:57 -08001350 if (usecase->type == PCM_HFP_CALL) {
1351 return audio_extn_utils_send_app_type_cfg_hfp(adev, usecase);
1352 }
1353
Siena Richard7c2db772016-12-21 11:32:34 -08001354 switch (usecase->type) {
1355 case PCM_PLAYBACK:
Surendar Karka93cd25a2018-08-28 14:21:37 +05301356 case TRANSCODE_LOOPBACK_RX:
Siena Richard7c2db772016-12-21 11:32:34 -08001357 ALOGD("%s: usecase->out_snd_device %s",
1358 __func__, platform_get_snd_device_name(usecase->out_snd_device));
1359 /* check for out combo device */
1360 if (platform_split_snd_device(adev->platform,
1361 usecase->out_snd_device,
1362 &num_devices, new_snd_devices)) {
1363 new_snd_devices[0] = usecase->out_snd_device;
1364 num_devices = 1;
1365 }
1366 break;
1367 case PCM_CAPTURE:
1368 ALOGD("%s: usecase->in_snd_device %s",
1369 __func__, platform_get_snd_device_name(usecase->in_snd_device));
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301370 if (voice_is_in_call_rec_stream(usecase->stream.in)) {
1371 in_snd_device = voice_get_incall_rec_backend_device(usecase->stream.in);
1372 }
Siena Richard7c2db772016-12-21 11:32:34 -08001373 /* check for in combo device */
1374 if (platform_split_snd_device(adev->platform,
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301375 in_snd_device,
Siena Richard7c2db772016-12-21 11:32:34 -08001376 &num_devices, new_snd_devices)) {
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301377 new_snd_devices[0] = in_snd_device;
Siena Richard7c2db772016-12-21 11:32:34 -08001378 num_devices = 1;
1379 }
1380 break;
1381 default:
1382 ALOGI("%s: not a playback/capture path, no need to cfg app type", __func__);
1383 rc = 0;
1384 break;
1385 }
1386
1387 for (i = 0; i < num_devices; i++) {
1388 rc = send_app_type_cfg_for_device(adev, usecase, new_snd_devices[i]);
1389 if (rc)
1390 break;
1391 }
1392
1393 return rc;
1394}
1395
Preetam Singh Ranawat9519e9c2015-11-18 16:05:55 +05301396int read_line_from_file(const char *path, char *buf, size_t count)
1397{
1398 char * fgets_ret;
1399 FILE * fd;
1400 int rv;
1401
1402 fd = fopen(path, "r");
1403 if (fd == NULL)
1404 return -1;
1405
1406 fgets_ret = fgets(buf, (int)count, fd);
1407 if (NULL != fgets_ret) {
1408 rv = (int)strlen(buf);
1409 } else {
1410 rv = ferror(fd);
1411 }
1412 fclose(fd);
1413
1414 return rv;
1415}
1416
Ashish Jainf1eaa582016-05-23 20:54:24 +05301417/*Translates ALSA formats to AOSP PCM formats*/
1418audio_format_t alsa_format_to_hal(uint32_t alsa_format)
1419{
1420 audio_format_t format;
1421
1422 switch(alsa_format) {
1423 case SNDRV_PCM_FORMAT_S16_LE:
1424 format = AUDIO_FORMAT_PCM_16_BIT;
1425 break;
1426 case SNDRV_PCM_FORMAT_S24_3LE:
1427 format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
1428 break;
1429 case SNDRV_PCM_FORMAT_S24_LE:
1430 format = AUDIO_FORMAT_PCM_8_24_BIT;
1431 break;
1432 case SNDRV_PCM_FORMAT_S32_LE:
1433 format = AUDIO_FORMAT_PCM_32_BIT;
1434 break;
1435 default:
1436 ALOGW("Incorrect ALSA format");
1437 format = AUDIO_FORMAT_INVALID;
1438 }
1439 return format;
1440}
1441
1442/*Translates hal format (AOSP) to alsa formats*/
1443uint32_t hal_format_to_alsa(audio_format_t hal_format)
1444{
1445 uint32_t alsa_format;
1446
1447 switch (hal_format) {
1448 case AUDIO_FORMAT_PCM_32_BIT: {
1449 if (platform_supports_true_32bit())
1450 alsa_format = SNDRV_PCM_FORMAT_S32_LE;
1451 else
1452 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1453 }
1454 break;
1455 case AUDIO_FORMAT_PCM_8_BIT:
1456 alsa_format = SNDRV_PCM_FORMAT_S8;
1457 break;
1458 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
1459 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1460 break;
1461 case AUDIO_FORMAT_PCM_8_24_BIT: {
1462 if (platform_supports_true_32bit())
1463 alsa_format = SNDRV_PCM_FORMAT_S32_LE;
1464 else
1465 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1466 }
1467 break;
1468 case AUDIO_FORMAT_PCM_FLOAT:
1469 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1470 break;
1471 default:
1472 case AUDIO_FORMAT_PCM_16_BIT:
1473 alsa_format = SNDRV_PCM_FORMAT_S16_LE;
1474 break;
1475 }
1476 return alsa_format;
1477}
1478
Ashish Jain83a6cc22016-06-28 14:34:17 +05301479/*Translates PCM formats to AOSP formats*/
1480audio_format_t pcm_format_to_hal(uint32_t pcm_format)
1481{
1482 audio_format_t format = AUDIO_FORMAT_INVALID;
1483
1484 switch(pcm_format) {
1485 case PCM_FORMAT_S16_LE:
1486 format = AUDIO_FORMAT_PCM_16_BIT;
1487 break;
1488 case PCM_FORMAT_S24_3LE:
1489 format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
1490 break;
1491 case PCM_FORMAT_S24_LE:
1492 format = AUDIO_FORMAT_PCM_8_24_BIT;
1493 break;
1494 case PCM_FORMAT_S32_LE:
1495 format = AUDIO_FORMAT_PCM_32_BIT;
1496 break;
1497 default:
1498 ALOGW("Incorrect PCM format");
1499 format = AUDIO_FORMAT_INVALID;
1500 }
1501 return format;
1502}
1503
1504/*Translates hal format (AOSP) to alsa formats*/
1505uint32_t hal_format_to_pcm(audio_format_t hal_format)
1506{
1507 uint32_t pcm_format;
1508
1509 switch (hal_format) {
1510 case AUDIO_FORMAT_PCM_32_BIT:
1511 case AUDIO_FORMAT_PCM_8_24_BIT:
1512 case AUDIO_FORMAT_PCM_FLOAT: {
1513 if (platform_supports_true_32bit())
1514 pcm_format = PCM_FORMAT_S32_LE;
1515 else
1516 pcm_format = PCM_FORMAT_S24_3LE;
1517 }
1518 break;
1519 case AUDIO_FORMAT_PCM_8_BIT:
1520 pcm_format = PCM_FORMAT_S8;
1521 break;
1522 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
1523 pcm_format = PCM_FORMAT_S24_3LE;
1524 break;
1525 default:
1526 case AUDIO_FORMAT_PCM_16_BIT:
1527 pcm_format = PCM_FORMAT_S16_LE;
1528 break;
1529 }
1530 return pcm_format;
1531}
1532
Ashish Jainf1eaa582016-05-23 20:54:24 +05301533uint32_t get_alsa_fragment_size(uint32_t bytes_per_sample,
1534 uint32_t sample_rate,
Deeraj Soman65358ab2019-02-07 15:40:49 +05301535 uint32_t noOfChannels,
1536 int64_t duration_ms)
Ashish Jainf1eaa582016-05-23 20:54:24 +05301537{
1538 uint32_t fragment_size = 0;
1539 uint32_t pcm_offload_time = PCM_OFFLOAD_BUFFER_DURATION;
1540
Deeraj Soman65358ab2019-02-07 15:40:49 +05301541 if (duration_ms >= MIN_OFFLOAD_BUFFER_DURATION_MS && duration_ms <= MAX_OFFLOAD_BUFFER_DURATION_MS)
1542 pcm_offload_time = duration_ms;
1543
Ashish Jainf1eaa582016-05-23 20:54:24 +05301544 fragment_size = (pcm_offload_time
1545 * sample_rate
1546 * bytes_per_sample
1547 * noOfChannels)/1000;
1548 if (fragment_size < MIN_PCM_OFFLOAD_FRAGMENT_SIZE)
1549 fragment_size = MIN_PCM_OFFLOAD_FRAGMENT_SIZE;
1550 else if (fragment_size > MAX_PCM_OFFLOAD_FRAGMENT_SIZE)
1551 fragment_size = MAX_PCM_OFFLOAD_FRAGMENT_SIZE;
1552 /*To have same PCM samples for all channels, the buffer size requires to
1553 *be multiple of (number of channels * bytes per sample)
1554 *For writes to succeed, the buffer must be written at address which is multiple of 32
1555 */
Aalique Grahameb85f2d92017-10-16 17:53:23 -07001556 fragment_size = ALIGN(fragment_size, (bytes_per_sample * noOfChannels * 32));
Ashish Jainf1eaa582016-05-23 20:54:24 +05301557
1558 ALOGI("PCM offload Fragment size to %d bytes", fragment_size);
1559 return fragment_size;
1560}
1561
1562/* Calculates the fragment size required to configure compress session.
1563 * Based on the alsa format selected, decide if conversion is needed in
1564
1565 * HAL ( e.g. convert AUDIO_FORMAT_PCM_FLOAT input format to
1566 * AUDIO_FORMAT_PCM_24_BIT_PACKED before writing to the compress driver.
1567 */
1568void audio_extn_utils_update_direct_pcm_fragment_size(struct stream_out *out)
1569{
Ashish Jain83a6cc22016-06-28 14:34:17 +05301570 audio_format_t dst_format = out->hal_op_format;
1571 audio_format_t src_format = out->hal_ip_format;
Ashish Jainf1eaa582016-05-23 20:54:24 +05301572 uint32_t hal_op_bytes_per_sample = audio_bytes_per_sample(dst_format);
1573 uint32_t hal_ip_bytes_per_sample = audio_bytes_per_sample(src_format);
1574
1575 out->compr_config.fragment_size =
1576 get_alsa_fragment_size(hal_op_bytes_per_sample,
1577 out->sample_rate,
Deeraj Soman65358ab2019-02-07 15:40:49 +05301578 popcount(out->channel_mask),
1579 out->info.duration_us / 1000);
Ashish Jainf1eaa582016-05-23 20:54:24 +05301580
1581 if ((src_format != dst_format) &&
1582 hal_op_bytes_per_sample != hal_ip_bytes_per_sample) {
1583
Ashish Jain83a6cc22016-06-28 14:34:17 +05301584 out->hal_fragment_size =
Ashish Jainf1eaa582016-05-23 20:54:24 +05301585 ((out->compr_config.fragment_size * hal_ip_bytes_per_sample) /
1586 hal_op_bytes_per_sample);
1587 ALOGI("enable conversion hal_input_fragment_size is %d src_format %x dst_format %x",
Ashish Jain83a6cc22016-06-28 14:34:17 +05301588 out->hal_fragment_size, src_format, dst_format);
Ashish Jainf1eaa582016-05-23 20:54:24 +05301589 } else {
Ashish Jain83a6cc22016-06-28 14:34:17 +05301590 out->hal_fragment_size = out->compr_config.fragment_size;
Ashish Jainf1eaa582016-05-23 20:54:24 +05301591 }
1592}
1593
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301594/* converts pcm format 24_8 to 8_24 inplace */
1595size_t audio_extn_utils_convert_format_24_8_to_8_24(void *buf, size_t bytes)
1596{
1597 size_t i = 0;
1598 int *int_buf_stream = buf;
1599
1600 if ((bytes % 4) != 0) {
1601 ALOGE("%s: wrong inout buffer! ... is not 32 bit aligned ", __func__);
1602 return -EINVAL;
1603 }
1604
1605 for (; i < (bytes / 4); i++)
1606 int_buf_stream[i] >>= 8;
1607
1608 return bytes;
1609}
1610
1611int get_snd_codec_id(audio_format_t format)
1612{
1613 int id = 0;
1614
1615 switch (format & AUDIO_FORMAT_MAIN_MASK) {
1616 case AUDIO_FORMAT_MP3:
1617 id = SND_AUDIOCODEC_MP3;
1618 break;
1619 case AUDIO_FORMAT_AAC:
1620 id = SND_AUDIOCODEC_AAC;
1621 break;
1622 case AUDIO_FORMAT_AAC_ADTS:
1623 id = SND_AUDIOCODEC_AAC;
1624 break;
Arun Kumar Dasari3b174182016-12-27 13:01:14 +05301625 case AUDIO_FORMAT_AAC_LATM:
1626 id = SND_AUDIOCODEC_AAC;
1627 break;
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301628 case AUDIO_FORMAT_PCM:
1629 id = SND_AUDIOCODEC_PCM;
1630 break;
1631 case AUDIO_FORMAT_FLAC:
1632 id = SND_AUDIOCODEC_FLAC;
1633 break;
1634 case AUDIO_FORMAT_ALAC:
1635 id = SND_AUDIOCODEC_ALAC;
1636 break;
1637 case AUDIO_FORMAT_APE:
1638 id = SND_AUDIOCODEC_APE;
1639 break;
1640 case AUDIO_FORMAT_VORBIS:
1641 id = SND_AUDIOCODEC_VORBIS;
1642 break;
1643 case AUDIO_FORMAT_WMA:
1644 id = SND_AUDIOCODEC_WMA;
1645 break;
1646 case AUDIO_FORMAT_WMA_PRO:
1647 id = SND_AUDIOCODEC_WMA_PRO;
1648 break;
Satish Babu Patakokila0c313922016-12-08 12:07:08 +05301649 case AUDIO_FORMAT_MP2:
1650 id = SND_AUDIOCODEC_MP2;
1651 break;
Satish Babu Patakokila915ecba2017-01-10 17:43:56 +05301652 case AUDIO_FORMAT_AC3:
1653 id = SND_AUDIOCODEC_AC3;
1654 break;
1655 case AUDIO_FORMAT_E_AC3:
1656 case AUDIO_FORMAT_E_AC3_JOC:
1657 id = SND_AUDIOCODEC_EAC3;
1658 break;
1659 case AUDIO_FORMAT_DTS:
1660 case AUDIO_FORMAT_DTS_HD:
1661 id = SND_AUDIOCODEC_DTS;
1662 break;
Ben Romberger1aaaf862017-04-06 17:49:46 -07001663 case AUDIO_FORMAT_DOLBY_TRUEHD:
1664 id = SND_AUDIOCODEC_TRUEHD;
1665 break;
Naresh Tanniru928f0862017-04-07 16:44:23 -07001666 case AUDIO_FORMAT_IEC61937:
1667 id = SND_AUDIOCODEC_IEC61937;
1668 break;
Preetam Singh Ranawat4277a5a2017-01-18 19:02:24 +05301669 case AUDIO_FORMAT_DSD:
1670 id = SND_AUDIOCODEC_DSD;
1671 break;
Dhanalakshmi Siddani18737932016-11-29 17:33:17 +05301672 case AUDIO_FORMAT_APTX:
1673 id = SND_AUDIOCODEC_APTX;
1674 break;
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301675 default:
1676 ALOGE("%s: Unsupported audio format :%x", __func__, format);
1677 }
1678
1679 return id;
1680}
1681
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001682void audio_extn_utils_send_audio_calibration(struct audio_device *adev,
1683 struct audio_usecase *usecase)
1684{
1685 int type = usecase->type;
1686
Dhananjay Kumar14245982017-01-16 20:21:00 +05301687 if (type == PCM_PLAYBACK && usecase->stream.out != NULL) {
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05301688 platform_send_audio_calibration(adev->platform, usecase,
Zhou Song06761dd2019-04-28 18:08:17 +08001689 usecase->stream.out->app_type_cfg.app_type);
Dhananjay Kumar14245982017-01-16 20:21:00 +05301690 } else if (type == PCM_CAPTURE && usecase->stream.in != NULL) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301691 platform_send_audio_calibration(adev->platform, usecase,
Zhou Song06761dd2019-04-28 18:08:17 +08001692 usecase->stream.in->app_type_cfg.app_type);
1693 } else if ((type == PCM_HFP_CALL) || (type == PCM_CAPTURE) ||
1694 (type == TRANSCODE_LOOPBACK_RX && usecase->stream.inout != NULL)) {
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05301695 platform_send_audio_calibration(adev->platform, usecase,
Zhou Song06761dd2019-04-28 18:08:17 +08001696 platform_get_default_app_type_v2(adev->platform, usecase->type));
Vidyakumar Athotae57f6002017-03-01 13:13:16 -08001697 } else {
1698 /* No need to send audio calibration for voice and voip call usecases */
1699 if ((type != VOICE_CALL) && (type != VOIP_CALL))
1700 ALOGW("%s: No audio calibration for usecase type = %d", __func__, type);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001701 }
1702}
1703
Ben Rombergera04fabc2014-11-14 12:16:03 -08001704// Base64 Encode and Decode
1705// Not all features supported. This must be used only with following conditions.
1706// Decode Modes: Support with and without padding
1707// CRLF not handling. So no CRLF in string to decode.
1708// Encode Modes: Supports only padding
1709int b64decode(char *inp, int ilen, uint8_t* outp)
1710{
1711 int i, j, k, ii, num;
1712 int rem, pcnt;
1713 uint32_t res=0;
1714 uint8_t getIndex[MAX_BASEINDEX_LEN];
1715 uint8_t tmp, cflag;
1716
1717 if(inp == NULL || outp == NULL || ilen <= 0) {
1718 ALOGE("[%s] received NULL pointer or zero length",__func__);
1719 return -1;
1720 }
1721
1722 memset(getIndex, MAX_BASEINDEX_LEN-1, sizeof(getIndex));
1723 for(i=0;i<BASE_TABLE_SIZE;i++) {
1724 getIndex[(uint8_t)bTable[i]] = (uint8_t)i;
1725 }
1726 getIndex[(uint8_t)'=']=0;
1727
1728 j=0;k=0;
1729 num = ilen/4;
1730 rem = ilen%4;
1731 if(rem==0)
1732 num = num-1;
1733 cflag=0;
1734 for(i=0; i<num; i++) {
1735 res=0;
1736 for(ii=0;ii<4;ii++) {
1737 res = res << 6;
1738 tmp = getIndex[(uint8_t)inp[j++]];
1739 res = res | tmp;
1740 cflag = cflag | tmp;
1741 }
1742 outp[k++] = (res >> 16)&0xFF;
1743 outp[k++] = (res >> 8)&0xFF;
1744 outp[k++] = res & 0xFF;
1745 }
1746
1747 // Handle last bytes special
1748 pcnt=0;
1749 if(rem == 0) {
1750 //With padding or full data
1751 res = 0;
1752 for(ii=0;ii<4;ii++) {
1753 if(inp[j] == '=')
1754 pcnt++;
1755 res = res << 6;
1756 tmp = getIndex[(uint8_t)inp[j++]];
1757 res = res | tmp;
1758 cflag = cflag | tmp;
1759 }
1760 outp[k++] = res >> 16;
1761 if(pcnt == 2)
1762 goto done;
1763 outp[k++] = (res>>8)&0xFF;
1764 if(pcnt == 1)
1765 goto done;
1766 outp[k++] = res&0xFF;
1767 } else {
1768 //without padding
1769 res = 0;
1770 for(i=0;i<rem;i++) {
1771 res = res << 6;
1772 tmp = getIndex[(uint8_t)inp[j++]];
1773 res = res | tmp;
1774 cflag = cflag | tmp;
1775 }
1776 for(i=rem;i<4;i++) {
1777 res = res << 6;
1778 pcnt++;
1779 }
1780 outp[k++] = res >> 16;
1781 if(pcnt == 2)
1782 goto done;
1783 outp[k++] = (res>>8)&0xFF;
1784 if(pcnt == 1)
1785 goto done;
1786 outp[k++] = res&0xFF;
1787 }
1788done:
1789 if(cflag == 0xFF) {
1790 ALOGE("[%s] base64 decode failed. Invalid character found %s",
1791 __func__, inp);
1792 return 0;
1793 }
1794 return k;
1795}
1796
1797int b64encode(uint8_t *inp, int ilen, char* outp)
1798{
1799 int i,j,k, num;
1800 int rem=0;
1801 uint32_t res=0;
1802
1803 if(inp == NULL || outp == NULL || ilen<=0) {
1804 ALOGE("[%s] received NULL pointer or zero input length",__func__);
1805 return -1;
1806 }
1807
1808 num = ilen/3;
1809 rem = ilen%3;
1810 j=0;k=0;
1811 for(i=0; i<num; i++) {
1812 //prepare index
1813 res = inp[j++]<<16;
1814 res = res | inp[j++]<<8;
1815 res = res | inp[j++];
1816 //get output map from index
1817 outp[k++] = (char) bTable[(res>>18)&0x3F];
1818 outp[k++] = (char) bTable[(res>>12)&0x3F];
1819 outp[k++] = (char) bTable[(res>>6)&0x3F];
1820 outp[k++] = (char) bTable[res&0x3F];
1821 }
1822
1823 switch(rem) {
1824 case 1:
1825 res = inp[j++]<<16;
1826 outp[k++] = (char) bTable[res>>18];
1827 outp[k++] = (char) bTable[(res>>12)&0x3F];
1828 //outp[k++] = '=';
1829 //outp[k++] = '=';
1830 break;
1831 case 2:
1832 res = inp[j++]<<16;
1833 res = res | inp[j++]<<8;
1834 outp[k++] = (char) bTable[res>>18];
1835 outp[k++] = (char) bTable[(res>>12)&0x3F];
1836 outp[k++] = (char) bTable[(res>>6)&0x3F];
1837 //outp[k++] = '=';
1838 break;
1839 default:
1840 break;
1841 }
Ben Rombergera04fabc2014-11-14 12:16:03 -08001842 outp[k] = '\0';
1843 return k;
1844}
Ashish Jain81eb2a82015-05-13 10:52:34 +05301845
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301846
1847int audio_extn_utils_get_codec_version(const char *snd_card_name,
1848 int card_num,
1849 char *codec_version)
1850{
1851 char procfs_path[50];
1852 FILE *fp;
1853
1854 if (strstr(snd_card_name, "tasha")) {
1855 snprintf(procfs_path, sizeof(procfs_path),
1856 "/proc/asound/card%d/codecs/tasha/version", card_num);
1857 if ((fp = fopen(procfs_path, "r")) != NULL) {
1858 fgets(codec_version, CODEC_VERSION_MAX_LENGTH, fp);
1859 fclose(fp);
1860 } else {
1861 ALOGE("%s: ERROR. cannot open %s", __func__, procfs_path);
1862 return -ENOENT;
1863 }
1864 ALOGD("%s: codec version %s", __func__, codec_version);
1865 }
1866
1867 return 0;
1868}
1869
Preetam Singh Ranawat9d0d8e42019-07-15 12:27:25 +05301870int audio_extn_utils_get_codec_variant(int card_num,
1871 char *codec_variant)
1872{
1873 char procfs_path[50];
1874 FILE *fp;
1875 snprintf(procfs_path, sizeof(procfs_path),
1876 "/proc/asound/card%d/codecs/wcd938x/variant", card_num);
1877 if ((fp = fopen(procfs_path, "r")) == NULL) {
1878 snprintf(procfs_path, sizeof(procfs_path),
1879 "/proc/asound/card%d/codecs/wcd937x/variant", card_num);
1880 if ((fp = fopen(procfs_path, "r")) == NULL) {
1881 ALOGE("%s: ERROR. cannot open %s", __func__, procfs_path);
1882 return -ENOENT;
1883 }
1884 }
1885 fgets(codec_variant, CODEC_VARIANT_MAX_LENGTH, fp);
1886 fclose(fp);
1887 ALOGD("%s: codec variant is %s", __func__, codec_variant);
1888 return 0;
1889}
1890
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301891
Ashish Jain81eb2a82015-05-13 10:52:34 +05301892#ifdef AUDIO_EXTERNAL_HDMI_ENABLED
1893
1894void get_default_compressed_channel_status(
1895 unsigned char *channel_status)
1896{
Ashish Jain81eb2a82015-05-13 10:52:34 +05301897 memset(channel_status,0,24);
1898
1899 /* block start bit in preamble bit 3 */
1900 channel_status[0] |= PROFESSIONAL;
1901 //compre out
1902 channel_status[0] |= NON_LPCM;
1903 // sample rate; fixed 48K for default/transcode
1904 channel_status[3] |= SR_48000;
1905}
1906
Ashish Jain81eb2a82015-05-13 10:52:34 +05301907int32_t get_compressed_channel_status(void *audio_stream_data,
1908 uint32_t audio_frame_size,
1909 unsigned char *channel_status,
1910 enum audio_parser_code_type codec_type)
1911 // codec_type - AUDIO_PARSER_CODEC_AC3
1912 // - AUDIO_PARSER_CODEC_DTS
1913{
1914 unsigned char *stream;
1915 int ret = 0;
1916 stream = (unsigned char *)audio_stream_data;
1917
1918 if (audio_stream_data == NULL || audio_frame_size == 0) {
1919 ALOGW("no buffer to get channel status, return default for compress");
1920 get_default_compressed_channel_status(channel_status);
1921 return ret;
1922 }
1923
1924 memset(channel_status,0,24);
1925 if(init_audio_parser(stream, audio_frame_size, codec_type) == -1)
1926 {
1927 ALOGE("init audio parser failed");
1928 return -1;
1929 }
1930 ret = get_channel_status(channel_status, codec_type);
1931 return ret;
1932
1933}
1934
Ashish Jain81eb2a82015-05-13 10:52:34 +05301935void get_lpcm_channel_status(uint32_t sampleRate,
1936 unsigned char *channel_status)
1937{
1938 int32_t status = 0;
Ashish Jain81eb2a82015-05-13 10:52:34 +05301939 memset(channel_status,0,24);
1940 /* block start bit in preamble bit 3 */
1941 channel_status[0] |= PROFESSIONAL;
1942 //LPCM OUT
1943 channel_status[0] &= ~NON_LPCM;
1944
1945 switch (sampleRate) {
1946 case 8000:
1947 case 11025:
1948 case 12000:
1949 case 16000:
1950 case 22050:
1951 channel_status[3] |= SR_NOTID;
Preetam Singh Ranawat61716b12015-12-14 11:55:24 +05301952 break;
Ashish Jain81eb2a82015-05-13 10:52:34 +05301953 case 24000:
1954 channel_status[3] |= SR_24000;
1955 break;
1956 case 32000:
1957 channel_status[3] |= SR_32000;
1958 break;
1959 case 44100:
1960 channel_status[3] |= SR_44100;
1961 break;
1962 case 48000:
1963 channel_status[3] |= SR_48000;
1964 break;
1965 case 88200:
1966 channel_status[3] |= SR_88200;
1967 break;
1968 case 96000:
1969 channel_status[3] |= SR_96000;
1970 break;
1971 case 176400:
1972 channel_status[3] |= SR_176400;
1973 break;
1974 case 192000:
1975 channel_status[3] |= SR_192000;
1976 break;
1977 default:
1978 ALOGV("Invalid sample_rate %u\n", sampleRate);
1979 status = -1;
1980 break;
1981 }
1982}
1983
1984void audio_utils_set_hdmi_channel_status(struct stream_out *out, char * buffer, size_t bytes)
1985{
1986 unsigned char channel_status[24]={0};
1987 struct snd_aes_iec958 iec958;
1988 const char *mixer_ctl_name = "IEC958 Playback PCM Stream";
1989 struct mixer_ctl *ctl;
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +05301990 ALOGV("%s: buffer %s bytes %zd", __func__, buffer, bytes);
Arun Mirpurie008ed22019-03-21 11:21:04 -07001991
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05301992 if (audio_extn_utils_is_dolby_format(out->format) &&
Ashish Jain81eb2a82015-05-13 10:52:34 +05301993 /*TODO:Extend code to support DTS passthrough*/
1994 /*set compressed channel status bits*/
Arun Mirpurie008ed22019-03-21 11:21:04 -07001995 audio_extn_passthru_is_passthrough_stream(out) &&
1996 audio_extn_is_hdmi_passthru_enabled()) {
Ashish Jain81eb2a82015-05-13 10:52:34 +05301997 get_compressed_channel_status(buffer, bytes, channel_status, AUDIO_PARSER_CODEC_AC3);
Arun Mirpurie008ed22019-03-21 11:21:04 -07001998 } else {
Ashish Jain81eb2a82015-05-13 10:52:34 +05301999 /*set channel status bit for LPCM*/
2000 get_lpcm_channel_status(out->sample_rate, channel_status);
2001 }
2002
2003 memcpy(iec958.status, channel_status,sizeof(iec958.status));
2004 ctl = mixer_get_ctl_by_name(out->dev->mixer, mixer_ctl_name);
2005 if (!ctl) {
2006 ALOGE("%s: Could not get ctl for mixer cmd - %s",
2007 __func__, mixer_ctl_name);
2008 return;
2009 }
2010 if (mixer_ctl_set_array(ctl, &iec958, sizeof(iec958)) < 0) {
2011 ALOGE("%s: Could not set channel status for ext HDMI ",
2012 __func__);
2013 return;
2014 }
2015
2016}
2017#endif
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302018
2019int audio_extn_utils_get_avt_device_drift(
2020 struct audio_usecase *usecase,
2021 struct audio_avt_device_drift_param *drift_param)
2022{
2023 int ret = 0, count = 0;
2024 char avt_device_drift_mixer_ctl_name[MIXER_PATH_MAX_LENGTH] = {0};
Naresh Tanniru6160c712017-04-17 15:43:48 +05302025 const char *backend = NULL;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302026 struct mixer_ctl *ctl = NULL;
2027 struct audio_avt_device_drift_stats drift_stats;
2028 struct audio_device *adev = NULL;
2029
2030 if (usecase != NULL && usecase->type == PCM_PLAYBACK) {
Naresh Tanniru6160c712017-04-17 15:43:48 +05302031 backend = platform_get_snd_device_backend_interface(usecase->out_snd_device);
2032 if (!backend) {
2033 ALOGE("%s: Unsupported device %d", __func__,
2034 usecase->stream.out->devices);
2035 ret = -EINVAL;
2036 goto done;
2037 }
2038 strlcpy(avt_device_drift_mixer_ctl_name,
2039 backend,
2040 MIXER_PATH_MAX_LENGTH);
2041
2042 count = strlen(backend);
2043 if (MIXER_PATH_MAX_LENGTH - count > 0) {
2044 strlcat(&avt_device_drift_mixer_ctl_name[count],
2045 " DRIFT",
2046 MIXER_PATH_MAX_LENGTH - count);
2047 } else {
2048 ret = -EINVAL;
2049 goto done;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302050 }
2051 } else {
Naresh Tanniru7586e292017-04-13 10:38:13 +05302052 ALOGE("%s: Invalid usecase",__func__);
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302053 ret = -EINVAL;
Naresh Tanniru6160c712017-04-17 15:43:48 +05302054 goto done;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302055 }
2056
Naresh Tanniru6160c712017-04-17 15:43:48 +05302057 adev = usecase->stream.out->dev;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302058 ctl = mixer_get_ctl_by_name(adev->mixer, avt_device_drift_mixer_ctl_name);
2059 if (!ctl) {
2060 ALOGE("%s: Could not get ctl for mixer cmd - %s",
2061 __func__, avt_device_drift_mixer_ctl_name);
2062
2063 ret = -EINVAL;
2064 goto done;
2065 }
2066
2067 ALOGV("%s: Getting AV Timer vs Device Drift mixer ctrl name %s", __func__,
2068 avt_device_drift_mixer_ctl_name);
2069
2070 mixer_ctl_update(ctl);
2071 count = mixer_ctl_get_num_values(ctl);
2072 if (count != sizeof(struct audio_avt_device_drift_stats)) {
2073 ALOGE("%s: mixer_ctl_get_num_values() invalid drift_stats data size",
2074 __func__);
2075
2076 ret = -EINVAL;
2077 goto done;
2078 }
2079
2080 ret = mixer_ctl_get_array(ctl, (void *)&drift_stats, count);
2081 if (ret != 0) {
2082 ALOGE("%s: mixer_ctl_get_array() failed to get drift_stats Params",
2083 __func__);
2084
2085 ret = -EINVAL;
2086 goto done;
2087 }
2088 memcpy(drift_param, &drift_stats.drift_param,
2089 sizeof(struct audio_avt_device_drift_param));
2090done:
2091 return ret;
2092}
Manish Dewangan07de2142017-02-27 19:27:20 +05302093
2094#ifdef SNDRV_COMPRESS_PATH_DELAY
2095int audio_extn_utils_compress_get_dsp_latency(struct stream_out *out)
2096{
2097 int ret = -EINVAL;
2098 struct snd_compr_metadata metadata;
2099 int delay_ms = COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
2100
Weiyin Jiangc49a3b52018-08-17 16:16:08 +08002101 /* override the latency for pcm offload use case */
2102 if ((out->flags & AUDIO_OUTPUT_FLAG_DIRECT) &&
2103 !(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)) {
2104 delay_ms = PCM_OFFLOAD_PLAYBACK_LATENCY;
2105 }
2106
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -07002107 if (property_get_bool("vendor.audio.playback.dsp.pathdelay", false)) {
Manish Dewangan07de2142017-02-27 19:27:20 +05302108 ALOGD("%s:: Quering DSP delay %d",__func__, __LINE__);
2109 if (!(is_offload_usecase(out->usecase))) {
2110 ALOGE("%s:: not supported for non offload session", __func__);
2111 goto exit;
2112 }
2113
2114 if (!out->compr) {
2115 ALOGD("%s:: Invalid compress handle,returning default dsp latency",
2116 __func__);
2117 goto exit;
2118 }
2119
2120 metadata.key = SNDRV_COMPRESS_PATH_DELAY;
2121 ret = compress_get_metadata(out->compr, &metadata);
2122 if(ret) {
2123 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2124 goto exit;
2125 }
2126 delay_ms = metadata.value[0] / 1000; /*convert to ms*/
2127 } else {
2128 ALOGD("%s:: Using Fix DSP delay",__func__);
2129 }
2130
2131exit:
2132 ALOGD("%s:: delay in ms is %d",__func__, delay_ms);
2133 return delay_ms;
2134}
2135#else
2136int audio_extn_utils_compress_get_dsp_latency(struct stream_out *out __unused)
2137{
2138 return COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
2139}
2140#endif
Manish Dewangan69426c82017-01-30 17:35:36 +05302141
2142#ifdef SNDRV_COMPRESS_RENDER_MODE
2143int audio_extn_utils_compress_set_render_mode(struct stream_out *out)
2144{
2145 struct snd_compr_metadata metadata;
2146 int ret = -EINVAL;
2147
2148 if (!(is_offload_usecase(out->usecase))) {
2149 ALOGE("%s:: not supported for non offload session", __func__);
2150 goto exit;
2151 }
2152
2153 if (!out->compr) {
2154 ALOGD("%s:: Invalid compress handle",
2155 __func__);
2156 goto exit;
2157 }
2158
2159 ALOGD("%s:: render mode %d", __func__, out->render_mode);
2160
2161 metadata.key = SNDRV_COMPRESS_RENDER_MODE;
2162 if (out->render_mode == RENDER_MODE_AUDIO_MASTER) {
2163 metadata.value[0] = SNDRV_COMPRESS_RENDER_MODE_AUDIO_MASTER;
2164 } else if (out->render_mode == RENDER_MODE_AUDIO_STC_MASTER) {
2165 metadata.value[0] = SNDRV_COMPRESS_RENDER_MODE_STC_MASTER;
2166 } else {
2167 ret = 0;
2168 goto exit;
2169 }
2170 ret = compress_set_metadata(out->compr, &metadata);
2171 if(ret) {
2172 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2173 }
2174exit:
2175 return ret;
2176}
2177#else
2178int audio_extn_utils_compress_set_render_mode(struct stream_out *out __unused)
2179{
2180 ALOGD("%s:: configuring render mode not supported", __func__);
2181 return 0;
2182}
2183#endif
Manish Dewangan58229382017-02-02 15:48:41 +05302184
2185#ifdef SNDRV_COMPRESS_CLK_REC_MODE
2186int audio_extn_utils_compress_set_clk_rec_mode(
2187 struct audio_usecase *usecase)
2188{
2189 struct snd_compr_metadata metadata;
2190 struct stream_out *out = NULL;
2191 int ret = -EINVAL;
2192
Naresh Tanniru7586e292017-04-13 10:38:13 +05302193 if (usecase == NULL || usecase->type != PCM_PLAYBACK) {
Manish Dewangan58229382017-02-02 15:48:41 +05302194 ALOGE("%s:: Invalid use case", __func__);
2195 goto exit;
2196 }
2197
2198 out = usecase->stream.out;
2199 if (!out) {
2200 ALOGE("%s:: invalid stream", __func__);
2201 goto exit;
2202 }
2203
2204 if (!is_offload_usecase(out->usecase)) {
2205 ALOGE("%s:: not supported for non offload session", __func__);
2206 goto exit;
2207 }
2208
2209 if (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER) {
2210 ALOGD("%s:: clk recovery is only supported in STC render mode",
2211 __func__);
2212 ret = 0;
2213 goto exit;
2214 }
2215
2216 if (!out->compr) {
2217 ALOGD("%s:: Invalid compress handle",
2218 __func__);
2219 goto exit;
2220 }
2221 metadata.key = SNDRV_COMPRESS_CLK_REC_MODE;
2222 switch(usecase->out_snd_device) {
2223 case SND_DEVICE_OUT_HDMI:
2224 case SND_DEVICE_OUT_SPEAKER_AND_HDMI:
2225 case SND_DEVICE_OUT_DISPLAY_PORT:
2226 case SND_DEVICE_OUT_SPEAKER_AND_DISPLAY_PORT:
2227 metadata.value[0] = SNDRV_COMPRESS_CLK_REC_MODE_NONE;
2228 break;
2229 default:
2230 metadata.value[0] = SNDRV_COMPRESS_CLK_REC_MODE_AUTO;
2231 break;
2232 }
2233
2234 ALOGD("%s:: clk recovery mode %d",__func__, metadata.value[0]);
2235
2236 ret = compress_set_metadata(out->compr, &metadata);
2237 if(ret) {
2238 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2239 }
2240
2241exit:
2242 return ret;
2243}
2244#else
2245int audio_extn_utils_compress_set_clk_rec_mode(
2246 struct audio_usecase *usecase __unused)
2247{
2248 ALOGD("%s:: configuring render mode not supported", __func__);
2249 return 0;
2250}
2251#endif
Manish Dewangan27346042017-03-01 12:56:12 +05302252
2253#ifdef SNDRV_COMPRESS_RENDER_WINDOW
2254int audio_extn_utils_compress_set_render_window(
2255 struct stream_out *out,
2256 struct audio_out_render_window_param *render_window)
2257{
2258 struct snd_compr_metadata metadata;
2259 int ret = -EINVAL;
2260
Manish Dewangan27346042017-03-01 12:56:12 +05302261 if(render_window == NULL) {
2262 ALOGE("%s:: Invalid render_window", __func__);
2263 goto exit;
2264 }
2265
Aniket Kumar Lata1e1d3662017-06-01 18:45:48 -07002266 ALOGD("%s:: render window start 0x%"PRIx64" end 0x%"PRIx64"",
2267 __func__,render_window->render_ws, render_window->render_we);
2268
Manish Dewangan27346042017-03-01 12:56:12 +05302269 if (!is_offload_usecase(out->usecase)) {
2270 ALOGE("%s:: not supported for non offload session", __func__);
2271 goto exit;
2272 }
2273
Naresh Tanniru6160c712017-04-17 15:43:48 +05302274 if ((out->render_mode != RENDER_MODE_AUDIO_MASTER) &&
2275 (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER)) {
Manish Dewangan27346042017-03-01 12:56:12 +05302276 ALOGD("%s:: only supported in timestamp mode, current "
2277 "render mode mode %d", __func__, out->render_mode);
2278 goto exit;
2279 }
2280
2281 if (!out->compr) {
2282 ALOGW("%s:: offload session not yet opened,"
2283 "render window will be configure later", __func__);
2284 /* store render window to reconfigure in start_output_stream() */
2285 goto exit;
2286 }
2287
2288 metadata.key = SNDRV_COMPRESS_RENDER_WINDOW;
2289 /*render window start value */
2290 metadata.value[0] = 0xFFFFFFFF & render_window->render_ws; /* lsb */
2291 metadata.value[1] = \
2292 (0xFFFFFFFF00000000 & render_window->render_ws) >> 32; /* msb*/
2293 /*render window end value */
2294 metadata.value[2] = 0xFFFFFFFF & render_window->render_we; /* lsb */
2295 metadata.value[3] = \
2296 (0xFFFFFFFF00000000 & render_window->render_we) >> 32; /* msb*/
2297
2298 ret = compress_set_metadata(out->compr, &metadata);
2299 if(ret) {
2300 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2301 }
2302
2303exit:
2304 return ret;
2305}
2306#else
2307int audio_extn_utils_compress_set_render_window(
2308 struct stream_out *out __unused,
2309 struct audio_out_render_window_param *render_window __unused)
2310{
2311 ALOGD("%s:: configuring render window not supported", __func__);
2312 return 0;
2313}
2314#endif
Manish Dewangan14956cc2017-02-14 18:54:42 +05302315
2316#ifdef SNDRV_COMPRESS_START_DELAY
2317int audio_extn_utils_compress_set_start_delay(
2318 struct stream_out *out,
2319 struct audio_out_start_delay_param *delay_param)
2320{
2321 struct snd_compr_metadata metadata;
2322 int ret = -EINVAL;
2323
2324 if(delay_param == NULL) {
2325 ALOGE("%s:: Invalid delay_param", __func__);
2326 goto exit;
2327 }
2328
2329 ALOGD("%s:: render start delay 0x%"PRIx64" ", __func__,
2330 delay_param->start_delay);
2331
2332 if (!is_offload_usecase(out->usecase)) {
2333 ALOGE("%s:: not supported for non offload session", __func__);
2334 goto exit;
2335 }
2336
Naresh Tanniru6160c712017-04-17 15:43:48 +05302337 if ((out->render_mode != RENDER_MODE_AUDIO_MASTER) &&
2338 (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER)) {
Manish Dewangan14956cc2017-02-14 18:54:42 +05302339 ALOGD("%s:: only supported in timestamp mode, current "
2340 "render mode mode %d", __func__, out->render_mode);
2341 goto exit;
2342 }
2343
2344 if (!out->compr) {
2345 ALOGW("%s:: offload session not yet opened,"
2346 "start delay will be configure later", __func__);
2347 goto exit;
2348 }
2349
2350 metadata.key = SNDRV_COMPRESS_START_DELAY;
2351 metadata.value[0] = 0xFFFFFFFF & delay_param->start_delay; /* lsb */
2352 metadata.value[1] = \
2353 (0xFFFFFFFF00000000 & delay_param->start_delay) >> 32; /* msb*/
2354
2355 ret = compress_set_metadata(out->compr, &metadata);
2356 if(ret) {
2357 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2358 }
2359
2360exit:
2361 return ret;
2362}
2363#else
2364int audio_extn_utils_compress_set_start_delay(
2365 struct stream_out *out __unused,
2366 struct audio_out_start_delay_param *delay_param __unused)
2367{
2368 ALOGD("%s:: configuring render window not supported", __func__);
2369 return 0;
2370}
2371#endif
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002372
Surendar Karka287348c2019-04-10 18:31:46 +05302373#ifdef SNDRV_COMPRESS_DSP_POSITION
2374int audio_extn_utils_compress_get_dsp_presentation_pos(struct stream_out *out,
2375 uint64_t *frames, struct timespec *timestamp, int32_t clock_id)
2376{
2377 int ret = -EINVAL;
2378 uint64_t *val = NULL;
2379 uint64_t time = 0;
2380 struct snd_compr_metadata metadata;
2381
2382 ALOGV("%s:: Quering DSP position with clock id %d",__func__, clock_id);
2383 metadata.key = SNDRV_COMPRESS_DSP_POSITION;
2384 metadata.value[0] = clock_id;
2385 ret = compress_get_metadata(out->compr, &metadata);
2386 if (ret) {
2387 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2388 ret = -errno;
2389 goto exit;
2390 }
2391 val = (uint64_t *)&metadata.value[1];
2392 *frames = *val;
2393 time = *(val + 1);
2394 timestamp->tv_sec = time / 1000000;
2395 timestamp->tv_nsec = (time % 1000000)*1000;
2396
2397exit:
2398 return ret;
2399}
2400#else
2401int audio_extn_utils_compress_get_dsp_presentation_pos(struct stream_out *out __unused,
2402 uint64_t *frames __unused, struct timespec *timestamp __unused,
2403 int32_t clock_id __unused)
2404{
2405 ALOGD("%s:: dsp presentation position not supported", __func__);
2406 return 0;
2407
2408}
2409#endif
2410
2411#ifdef SNDRV_PCM_IOCTL_DSP_POSITION
2412int audio_extn_utils_pcm_get_dsp_presentation_pos(struct stream_out *out,
2413 uint64_t *frames, struct timespec *timestamp, int32_t clock_id)
2414{
2415 int ret = -EINVAL;
2416 uint64_t time = 0;
2417 struct snd_pcm_prsnt_position prsnt_position;
2418
2419 ALOGV("%s:: Quering DSP position with clock id %d",__func__, clock_id);
2420 prsnt_position.clock_id = clock_id;
2421 ret = pcm_ioctl(out->pcm, SNDRV_PCM_IOCTL_DSP_POSITION, &prsnt_position);
2422 if (ret) {
2423 ALOGE("%s::error %d", __func__, ret);
2424 ret = -EIO;
2425 goto exit;
2426 }
2427
2428 *frames = prsnt_position.frames;
2429 time = prsnt_position.timestamp;
2430 timestamp->tv_sec = time / 1000000;
2431 timestamp->tv_nsec = (time % 1000000)*1000;
2432
2433exit:
2434 return ret;
2435}
2436#else
2437int audio_extn_utils_pcm_get_dsp_presentation_pos(struct stream_out *out __unused,
2438 uint64_t *frames __unused, struct timespec *timestamp __unused,
2439 int32_t clock_id __unused)
2440{
2441 ALOGD("%s:: dsp presentation position not supported", __func__);
2442 return 0;
2443
2444}
2445#endif
2446
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002447#define MAX_SND_CARD 8
Ashish Jain30ae8942017-11-05 17:21:23 +05302448#define RETRY_US 1000000
2449#define RETRY_NUMBER 40
Aalique Grahame22e49102018-12-18 14:23:57 -08002450#define PLATFORM_INFO_XML_PATH "audio_platform_info.xml"
2451#define PLATFORM_INFO_XML_BASE_STRING "audio_platform_info"
2452
2453#ifdef LINUX_ENABLED
2454static const char *kConfigLocationList[] =
2455 {"/etc"};
2456#else
2457static const char *kConfigLocationList[] =
2458 {"/vendor/etc"};
2459#endif
2460static const int kConfigLocationListSize =
2461 (sizeof(kConfigLocationList) / sizeof(kConfigLocationList[0]));
2462
2463bool audio_extn_utils_resolve_config_file(char file_name[MIXER_PATH_MAX_LENGTH])
2464{
2465 char full_config_path[MIXER_PATH_MAX_LENGTH];
2466 for (int i = 0; i < kConfigLocationListSize; i++) {
2467 snprintf(full_config_path,
2468 MIXER_PATH_MAX_LENGTH,
2469 "%s/%s",
2470 kConfigLocationList[i],
2471 file_name);
2472 if (F_OK == access(full_config_path, 0)) {
Weiyin Jiang2995f662019-04-17 14:25:12 +08002473 strlcpy(file_name, full_config_path, MIXER_PATH_MAX_LENGTH);
Aalique Grahame22e49102018-12-18 14:23:57 -08002474 return true;
2475 }
2476 }
2477 return false;
2478}
2479
2480/* platform_info_file should be size 'MIXER_PATH_MAX_LENGTH' */
2481int audio_extn_utils_get_platform_info(const char* snd_card_name, char* platform_info_file)
2482{
2483 if (NULL == snd_card_name) {
2484 return -1;
2485 }
2486
2487 struct snd_card_split *snd_split_handle = NULL;
2488 int ret = 0;
2489 audio_extn_set_snd_card_split(snd_card_name);
2490 snd_split_handle = audio_extn_get_snd_card_split();
2491
2492 snprintf(platform_info_file, MIXER_PATH_MAX_LENGTH, "%s_%s_%s.xml",
2493 PLATFORM_INFO_XML_BASE_STRING, snd_split_handle->snd_card,
2494 snd_split_handle->form_factor);
2495
2496 if (!audio_extn_utils_resolve_config_file(platform_info_file)) {
2497 memset(platform_info_file, 0, MIXER_PATH_MAX_LENGTH);
2498 snprintf(platform_info_file, MIXER_PATH_MAX_LENGTH, "%s_%s.xml",
2499 PLATFORM_INFO_XML_BASE_STRING, snd_split_handle->snd_card);
2500
2501 if (!audio_extn_utils_resolve_config_file(platform_info_file)) {
2502 memset(platform_info_file, 0, MIXER_PATH_MAX_LENGTH);
2503 strlcpy(platform_info_file, PLATFORM_INFO_XML_PATH, MIXER_PATH_MAX_LENGTH);
2504 ret = audio_extn_utils_resolve_config_file(platform_info_file) ? 0 : -1;
2505 }
2506 }
2507
2508 return ret;
2509}
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002510
2511int audio_extn_utils_get_snd_card_num()
2512{
Soumya Managoli9fee7c62018-04-06 16:21:50 +05302513 int snd_card_num = 0;
2514 struct mixer *mixer = NULL;
2515
2516 snd_card_num = audio_extn_utils_open_snd_mixer(&mixer);
2517 if (mixer)
2518 mixer_close(mixer);
2519 return snd_card_num;
2520}
2521
2522int audio_extn_utils_open_snd_mixer(struct mixer **mixer_handle)
2523{
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002524
2525 void *hw_info = NULL;
2526 struct mixer *mixer = NULL;
2527 int retry_num = 0;
mpangfaa7bae2019-01-15 16:38:13 +08002528 int snd_card_num = 0;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002529 char* snd_card_name = NULL;
mpangfaa7bae2019-01-15 16:38:13 +08002530 int snd_card_detection_info[MAX_SND_CARD] = {0};
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002531
Soumya Managoli9fee7c62018-04-06 16:21:50 +05302532 if (!mixer_handle) {
2533 ALOGE("invalid mixer handle");
2534 return -1;
2535 }
2536 *mixer_handle = NULL;
Ashish Jain30ae8942017-11-05 17:21:23 +05302537 /*
mpangfaa7bae2019-01-15 16:38:13 +08002538 * Try with all the sound cards ( 0 to 7 ) and if none of them were detected
Ashish Jain30ae8942017-11-05 17:21:23 +05302539 * sleep for 1 sec and try detections with sound card 0 again.
2540 * If sound card gets detected, check if it is relevant, if not check with the
2541 * other sound cards. To ensure that the irrelevant sound card is not check again,
2542 * we maintain it in min_snd_card_num.
2543 */
2544 while (retry_num < RETRY_NUMBER) {
mpangfaa7bae2019-01-15 16:38:13 +08002545 snd_card_num = 0;
2546 while (snd_card_num < MAX_SND_CARD) {
2547 if (snd_card_detection_info[snd_card_num] == 0) {
2548 mixer = mixer_open(snd_card_num);
2549 if (!mixer)
2550 snd_card_num++;
2551 else
2552 break;
2553 } else
2554 snd_card_num++;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002555 }
2556
2557 if (!mixer) {
Ashish Jain30ae8942017-11-05 17:21:23 +05302558 usleep(RETRY_US);
Ashish Jain30ae8942017-11-05 17:21:23 +05302559 retry_num++;
mpangfaa7bae2019-01-15 16:38:13 +08002560 ALOGD("%s: retry, retry_num %d", __func__, retry_num);
Ashish Jain30ae8942017-11-05 17:21:23 +05302561 continue;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002562 }
2563
2564 snd_card_name = strdup(mixer_get_name(mixer));
2565 if (!snd_card_name) {
2566 ALOGE("failed to allocate memory for snd_card_name\n");
2567 mixer_close(mixer);
2568 return -1;
2569 }
2570 ALOGD("%s: snd_card_name: %s", __func__, snd_card_name);
mpangfaa7bae2019-01-15 16:38:13 +08002571 snd_card_detection_info[snd_card_num] = 1;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002572 hw_info = hw_info_init(snd_card_name);
2573 if (hw_info) {
2574 ALOGD("%s: Opened sound card:%d", __func__, snd_card_num);
2575 break;
2576 }
mpangfaa7bae2019-01-15 16:38:13 +08002577 ALOGE("%s: Failed to init hardware info, snd_card_num:%d", __func__, snd_card_num);
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002578
Dhananjay Kumara7e27832017-07-11 15:40:39 +05302579 free(snd_card_name);
2580 snd_card_name = NULL;
2581
2582 mixer_close(mixer);
2583 mixer = NULL;
2584 }
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002585 if (snd_card_name)
2586 free(snd_card_name);
Ashish Jain30ae8942017-11-05 17:21:23 +05302587
Dhananjay Kumara7e27832017-07-11 15:40:39 +05302588 if (hw_info)
2589 hw_info_deinit(hw_info);
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002590
mpangfaa7bae2019-01-15 16:38:13 +08002591 if (retry_num >= RETRY_NUMBER) {
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002592 ALOGE("%s: Unable to find correct sound card, aborting.", __func__);
2593 return -1;
2594 }
2595
Soumya Managoli9fee7c62018-04-06 16:21:50 +05302596 if (mixer)
2597 *mixer_handle = mixer;
2598
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002599 return snd_card_num;
2600}
Naresh Tanniru6160c712017-04-17 15:43:48 +05302601
Soumya Managoli9fee7c62018-04-06 16:21:50 +05302602void audio_extn_utils_close_snd_mixer(struct mixer *mixer)
2603{
2604 if (mixer)
2605 mixer_close(mixer);
2606}
2607
Naresh Tanniru6160c712017-04-17 15:43:48 +05302608#ifdef SNDRV_COMPRESS_ENABLE_ADJUST_SESSION_CLOCK
2609int audio_extn_utils_compress_enable_drift_correction(
2610 struct stream_out *out,
2611 struct audio_out_enable_drift_correction *drift)
2612{
2613 struct snd_compr_metadata metadata;
2614 int ret = -EINVAL;
2615
2616 if(drift == NULL) {
2617 ALOGE("%s:: Invalid param", __func__);
2618 goto exit;
2619 }
2620
2621 ALOGD("%s:: drift enable %d", __func__,drift->enable);
2622
2623 if (!is_offload_usecase(out->usecase)) {
2624 ALOGE("%s:: not supported for non offload session", __func__);
2625 goto exit;
2626 }
2627
2628 if (!out->compr) {
2629 ALOGW("%s:: offload session not yet opened,"
2630 "start delay will be configure later", __func__);
2631 goto exit;
2632 }
2633
2634 metadata.key = SNDRV_COMPRESS_ENABLE_ADJUST_SESSION_CLOCK;
2635 metadata.value[0] = drift->enable;
2636 out->drift_correction_enabled = drift->enable;
2637
2638 ret = compress_set_metadata(out->compr, &metadata);
2639 if(ret) {
2640 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2641 out->drift_correction_enabled = false;
2642 }
2643
2644exit:
2645 return ret;
2646}
2647#else
2648int audio_extn_utils_compress_enable_drift_correction(
2649 struct stream_out *out __unused,
2650 struct audio_out_enable_drift_correction *drift __unused)
2651{
2652 ALOGD("%s:: configuring drift enablement not supported", __func__);
2653 return 0;
2654}
2655#endif
2656
2657#ifdef SNDRV_COMPRESS_ADJUST_SESSION_CLOCK
2658int audio_extn_utils_compress_correct_drift(
2659 struct stream_out *out,
2660 struct audio_out_correct_drift *drift_param)
2661{
2662 struct snd_compr_metadata metadata;
2663 int ret = -EINVAL;
2664
2665 if (drift_param == NULL) {
2666 ALOGE("%s:: Invalid drift_param", __func__);
2667 goto exit;
2668 }
2669
2670 ALOGD("%s:: adjust time 0x%"PRIx64" ", __func__,
2671 drift_param->adjust_time);
2672
2673 if (!is_offload_usecase(out->usecase)) {
2674 ALOGE("%s:: not supported for non offload session", __func__);
2675 goto exit;
2676 }
2677
2678 if (!out->compr) {
2679 ALOGW("%s:: offload session not yet opened", __func__);
2680 goto exit;
2681 }
2682
2683 if (!out->drift_correction_enabled) {
2684 ALOGE("%s:: drift correction not enabled", __func__);
2685 goto exit;
2686 }
2687
2688 metadata.key = SNDRV_COMPRESS_ADJUST_SESSION_CLOCK;
2689 metadata.value[0] = 0xFFFFFFFF & drift_param->adjust_time; /* lsb */
2690 metadata.value[1] = \
2691 (0xFFFFFFFF00000000 & drift_param->adjust_time) >> 32; /* msb*/
2692
2693 ret = compress_set_metadata(out->compr, &metadata);
2694 if(ret)
2695 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2696exit:
2697 return ret;
2698}
2699#else
2700int audio_extn_utils_compress_correct_drift(
2701 struct stream_out *out __unused,
2702 struct audio_out_correct_drift *drift_param __unused)
2703{
2704 ALOGD("%s:: setting adjust clock not supported", __func__);
2705 return 0;
2706}
2707#endif
Naresh Tanniru29bce4e2017-04-27 17:54:30 +05302708
2709int audio_extn_utils_set_channel_map(
2710 struct stream_out *out,
2711 struct audio_out_channel_map_param *channel_map_param)
2712{
2713 int ret = -EINVAL, i = 0;
2714 int channels = audio_channel_count_from_out_mask(out->channel_mask);
2715
2716 if (channel_map_param == NULL) {
2717 ALOGE("%s:: Invalid channel_map", __func__);
2718 goto exit;
2719 }
2720
2721 if (channel_map_param->channels != channels) {
2722 ALOGE("%s:: Channels(%d) does not match stream channels(%d)",
2723 __func__, channel_map_param->channels, channels);
2724 goto exit;
2725 }
2726
2727 for ( i = 0; i < channels; i++) {
2728 ALOGV("%s:: channel_map[%d]- %d", __func__, i, channel_map_param->channel_map[i]);
2729 out->channel_map_param.channel_map[i] = channel_map_param->channel_map[i];
2730 }
2731 ret = 0;
2732exit:
2733 return ret;
2734}
Varun Balaraje49253e2017-07-06 19:48:56 +05302735
2736int audio_extn_utils_set_pan_scale_params(
2737 struct stream_out *out,
2738 struct mix_matrix_params *mm_params)
2739{
2740 int ret = -EINVAL, i = 0, j = 0;
2741
Varun Balaraj003ee752017-08-07 17:54:55 +05302742 if (mm_params == NULL || out == NULL) {
2743 ALOGE("%s:: Invalid mix matrix or out param", __func__);
Varun Balaraje49253e2017-07-06 19:48:56 +05302744 goto exit;
2745 }
2746
2747 if (mm_params->num_output_channels > MAX_CHANNELS_SUPPORTED ||
2748 mm_params->num_output_channels <= 0 ||
2749 mm_params->num_input_channels > MAX_CHANNELS_SUPPORTED ||
2750 mm_params->num_input_channels <= 0)
2751 goto exit;
2752
2753 out->pan_scale_params.num_output_channels = mm_params->num_output_channels;
2754 out->pan_scale_params.num_input_channels = mm_params->num_input_channels;
2755 out->pan_scale_params.has_output_channel_map =
2756 mm_params->has_output_channel_map;
2757 for (i = 0; i < mm_params->num_output_channels; i++)
2758 out->pan_scale_params.output_channel_map[i] =
2759 mm_params->output_channel_map[i];
2760
2761 out->pan_scale_params.has_input_channel_map =
2762 mm_params->has_input_channel_map;
2763 for (i = 0; i < mm_params->num_input_channels; i++)
2764 out->pan_scale_params.input_channel_map[i] =
2765 mm_params->input_channel_map[i];
2766
2767 out->pan_scale_params.has_mixer_coeffs = mm_params->has_mixer_coeffs;
2768 for (i = 0; i < mm_params->num_output_channels; i++)
2769 for (j = 0; j < mm_params->num_input_channels; j++) {
2770 //Convert the channel coefficient gains in Q14 format
2771 out->pan_scale_params.mixer_coeffs[i][j] =
2772 mm_params->mixer_coeffs[i][j] * (2 << 13);
2773 }
2774
2775 ret = platform_set_stream_pan_scale_params(out->dev->platform,
2776 out->pcm_device_id,
2777 out->pan_scale_params);
2778
2779exit:
2780 return ret;
2781}
2782
2783int audio_extn_utils_set_downmix_params(
2784 struct stream_out *out,
2785 struct mix_matrix_params *mm_params)
2786{
2787 int ret = -EINVAL, i = 0, j = 0;
2788 struct audio_usecase *usecase = NULL;
2789
Aniket Kumar Lataf9f246e2017-09-15 15:20:16 -07002790 if (mm_params == NULL || out == NULL) {
Varun Balaraj003ee752017-08-07 17:54:55 +05302791 ALOGE("%s:: Invalid mix matrix or out param", __func__);
Varun Balaraje49253e2017-07-06 19:48:56 +05302792 goto exit;
2793 }
2794
2795 if (mm_params->num_output_channels > MAX_CHANNELS_SUPPORTED ||
2796 mm_params->num_output_channels <= 0 ||
2797 mm_params->num_input_channels > MAX_CHANNELS_SUPPORTED ||
2798 mm_params->num_input_channels <= 0)
2799 goto exit;
2800
2801 usecase = get_usecase_from_list(out->dev, out->usecase);
Varun Balaraj003ee752017-08-07 17:54:55 +05302802 if (!usecase) {
2803 ALOGE("%s: Get usecase list failed!", __func__);
Aniket Kumar Lataf9f246e2017-09-15 15:20:16 -07002804 goto exit;
2805 }
Varun Balaraje49253e2017-07-06 19:48:56 +05302806 out->downmix_params.num_output_channels = mm_params->num_output_channels;
2807 out->downmix_params.num_input_channels = mm_params->num_input_channels;
2808
2809 out->downmix_params.has_output_channel_map =
2810 mm_params->has_output_channel_map;
2811 for (i = 0; i < mm_params->num_output_channels; i++) {
2812 out->downmix_params.output_channel_map[i] =
2813 mm_params->output_channel_map[i];
2814 }
2815
2816 out->downmix_params.has_input_channel_map =
2817 mm_params->has_input_channel_map;
2818 for (i = 0; i < mm_params->num_input_channels; i++)
2819 out->downmix_params.input_channel_map[i] =
2820 mm_params->input_channel_map[i];
2821
2822 out->downmix_params.has_mixer_coeffs = mm_params->has_mixer_coeffs;
2823 for (i = 0; i < mm_params->num_output_channels; i++)
Nikhil Latukar2e6f6242017-08-15 13:37:07 +05302824 for (j = 0; j < mm_params->num_input_channels; j++) {
2825 //Convert the channel coefficient gains in Q14 format
Varun Balaraje49253e2017-07-06 19:48:56 +05302826 out->downmix_params.mixer_coeffs[i][j] =
Nikhil Latukar2e6f6242017-08-15 13:37:07 +05302827 mm_params->mixer_coeffs[i][j] * (2 << 13);
2828 }
Varun Balaraje49253e2017-07-06 19:48:56 +05302829
2830 ret = platform_set_stream_downmix_params(out->dev->platform,
2831 out->pcm_device_id,
2832 usecase->out_snd_device,
2833 out->downmix_params);
2834
2835exit:
2836 return ret;
2837}
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05302838
2839bool audio_extn_utils_is_dolby_format(audio_format_t format)
2840{
2841 if (format == AUDIO_FORMAT_AC3 ||
2842 format == AUDIO_FORMAT_E_AC3 ||
2843 format == AUDIO_FORMAT_E_AC3_JOC)
2844 return true;
2845 else
2846 return false;
2847}
2848
`Deeraj Soman676c2702017-09-18 19:25:53 +05302849int audio_extn_utils_get_bit_width_from_string(const char *id_string)
2850{
2851 int i;
2852 const mixer_config_lookup mixer_bitwidth_config[] = {{"S24_3LE", 24},
2853 {"S32_LE", 32},
2854 {"S24_LE", 24},
2855 {"S16_LE", 16}};
2856 int num_configs = sizeof(mixer_bitwidth_config) / sizeof(mixer_bitwidth_config[0]);
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05302857
`Deeraj Soman676c2702017-09-18 19:25:53 +05302858 for (i = 0; i < num_configs; i++) {
2859 if (!strcmp(id_string, mixer_bitwidth_config[i].id_string))
2860 return mixer_bitwidth_config[i].value;
2861 }
2862
2863 return -EINVAL;
2864}
2865
2866int audio_extn_utils_get_sample_rate_from_string(const char *id_string)
2867{
2868 int i;
2869 const mixer_config_lookup mixer_samplerate_config[] = {{"KHZ_32", 32000},
2870 {"KHZ_48", 48000},
2871 {"KHZ_96", 96000},
2872 {"KHZ_144", 144000},
2873 {"KHZ_192", 192000},
2874 {"KHZ_384", 384000},
2875 {"KHZ_44P1", 44100},
2876 {"KHZ_88P2", 88200},
2877 {"KHZ_176P4", 176400},
2878 {"KHZ_352P8", 352800}};
2879 int num_configs = sizeof(mixer_samplerate_config) / sizeof(mixer_samplerate_config[0]);
2880
2881 for (i = 0; i < num_configs; i++) {
2882 if (!strcmp(id_string, mixer_samplerate_config[i].id_string))
2883 return mixer_samplerate_config[i].value;
2884 }
2885
2886 return -EINVAL;
2887}
2888
2889int audio_extn_utils_get_channels_from_string(const char *id_string)
2890{
2891 int i;
2892 const mixer_config_lookup mixer_channels_config[] = {{"One", 1},
2893 {"Two", 2},
2894 {"Three",3},
2895 {"Four", 4},
2896 {"Five", 5},
2897 {"Six", 6},
2898 {"Seven", 7},
2899 {"Eight", 8}};
2900 int num_configs = sizeof(mixer_channels_config) / sizeof(mixer_channels_config[0]);
2901
2902 for (i = 0; i < num_configs; i++) {
2903 if (!strcmp(id_string, mixer_channels_config[i].id_string))
2904 return mixer_channels_config[i].value;
2905 }
2906
2907 return -EINVAL;
2908}
Surendar karka30569792018-05-08 12:02:21 +05302909
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08002910void audio_extn_utils_release_snd_device(snd_device_t snd_device)
2911{
2912 audio_extn_dev_arbi_release(snd_device);
2913 audio_extn_sound_trigger_update_device_status(snd_device,
2914 ST_EVENT_SND_DEVICE_FREE);
2915 audio_extn_listen_update_device_status(snd_device,
2916 LISTEN_EVENT_SND_DEVICE_FREE);
2917}
2918
Surendar karka30569792018-05-08 12:02:21 +05302919int audio_extn_utils_get_license_params
2920(
2921const struct audio_device *adev,
2922struct audio_license_params *license_params
2923)
2924{
2925 if(!license_params)
2926 return -EINVAL;
2927 return platform_get_license_by_product(adev->platform, (const char*)license_params->product, &license_params->key, license_params->license);
2928}
2929
Aalique Grahame22e49102018-12-18 14:23:57 -08002930int audio_extn_utils_send_app_type_gain(struct audio_device *adev,
2931 int app_type,
2932 int *gain)
2933{
2934 int gain_cfg[4];
2935 const char *mixer_ctl_name = "App Type Gain";
2936 struct mixer_ctl *ctl;
2937 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
2938 if (!ctl) {
2939 ALOGE("%s: Could not get volume ctl mixer %s", __func__,
2940 mixer_ctl_name);
2941 return -EINVAL;
2942 }
2943 gain_cfg[0] = 0;
2944 gain_cfg[1] = app_type;
2945 gain_cfg[2] = gain[0];
2946 gain_cfg[3] = gain[1];
2947 ALOGV("%s app_type %d l(%d) r(%d)", __func__, app_type, gain[0], gain[1]);
2948 return mixer_ctl_set_array(ctl, gain_cfg,
2949 sizeof(gain_cfg)/sizeof(gain_cfg[0]));
2950}
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -07002951
2952int audio_extn_utils_is_vendor_enhanced_fwk()
2953{
2954 static int is_running_with_enhanced_fwk = -EINVAL;
2955
2956 if (is_running_with_enhanced_fwk == -EINVAL) {
2957 vndk_fwk_lib_handle = dlopen(VNDK_FWK_LIB_PATH, RTLD_NOW);
2958 if (vndk_fwk_lib_handle != NULL) {
2959 vndk_fwk_isVendorEnhancedFwk = (vndk_fwk_isVendorEnhancedFwk_t)
2960 dlsym(vndk_fwk_lib_handle, "isRunningWithVendorEnhancedFramework");
2961 if (vndk_fwk_isVendorEnhancedFwk == NULL) {
2962 ALOGW("%s: dlsym failed, defaulting to enhanced_fwk configuration",
2963 __func__);
2964 is_running_with_enhanced_fwk = 1;
2965 } else {
2966 is_running_with_enhanced_fwk = vndk_fwk_isVendorEnhancedFwk();
2967 }
2968 dlclose(vndk_fwk_lib_handle);
2969 vndk_fwk_lib_handle = NULL;
2970 } else {
2971 ALOGW("%s: VNDK_FWK_LIB not found, setting stock configuration", __func__);
2972 is_running_with_enhanced_fwk = 0;
2973 }
2974 ALOGV("%s: vndk_fwk_isVendorEnhancedFwk=%d", __func__, is_running_with_enhanced_fwk);
2975 }
2976
2977 return is_running_with_enhanced_fwk;
2978}
Deeraj Soman14230922019-01-30 16:39:30 +05302979
Deeraj Somanfa377bf2019-02-06 12:57:59 +05302980int audio_extn_utils_get_perf_mode_flag(void)
2981{
2982#ifdef COMPRESSED_PERF_MODE_FLAG
2983 return COMPRESSED_PERF_MODE_FLAG;
2984#else
2985 return 0;
2986#endif
2987}
2988
Deeraj Soman14230922019-01-30 16:39:30 +05302989size_t audio_extn_utils_get_input_buffer_size(uint32_t sample_rate,
2990 audio_format_t format,
2991 int channel_count,
2992 int64_t duration_ms,
2993 bool is_low_latency)
2994{
2995 size_t size = 0;
2996 size_t capture_duration = AUDIO_CAPTURE_PERIOD_DURATION_MSEC;
2997 uint32_t bytes_per_period_sample = 0;
2998
2999
3000 if (audio_extn_utils_check_input_parameters(sample_rate, format, channel_count) != 0)
3001 return 0;
3002
3003 if (duration_ms >= MIN_OFFLOAD_BUFFER_DURATION_MS && duration_ms <= MAX_OFFLOAD_BUFFER_DURATION_MS)
3004 capture_duration = duration_ms;
3005
3006 size = (sample_rate * capture_duration) / 1000;
3007 if (is_low_latency)
3008 size = LOW_LATENCY_CAPTURE_PERIOD_SIZE;
3009
3010
3011 bytes_per_period_sample = audio_bytes_per_sample(format) * channel_count;
3012 size *= bytes_per_period_sample;
3013
3014 /* make sure the size is multiple of 32 bytes and additionally multiple of
3015 * the frame_size (required for 24bit samples and non-power-of-2 channel counts)
3016 * At 48 kHz mono 16-bit PCM:
3017 * 5.000 ms = 240 frames = 15*16*1*2 = 480, a whole multiple of 32 (15)
3018 * 3.333 ms = 160 frames = 10*16*1*2 = 320, a whole multiple of 32 (10)
3019 *
3020 * The loop reaches result within 32 iterations, as initial size is
3021 * already a multiple of frame_size
3022 */
3023 size = audio_extn_utils_nearest_multiple(size, audio_extn_utils_lcm(32, bytes_per_period_sample));
3024
3025 return size;
3026}