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