blob: e7b69ead1b7f1cd77f0ee3287dd3409b93360472 [file] [log] [blame]
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001/*
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07002 * Copyright (c) 2014-2020, 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
Samyak Jain2546f8a2019-08-29 19:24:21 +0530107#define PCM_OFFLOAD_PLAYBACK_LATENCY PCM_OFFLOAD_PLAYBACK_DSP_PATHDELAY
Manish Dewangan07de2142017-02-27 19:27:20 +0530108
Varun Balaraje49253e2017-07-06 19:48:56 +0530109#ifndef MAX_CHANNELS_SUPPORTED
110#define MAX_CHANNELS_SUPPORTED 8
111#endif
112
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -0700113#ifdef __LP64__
114#define VNDK_FWK_LIB_PATH "/vendor/lib64/libqti_vndfwk_detect.so"
115#else
116#define VNDK_FWK_LIB_PATH "/vendor/lib/libqti_vndfwk_detect.so"
117#endif
118
Weiyin Jiangd5718bd2019-09-04 16:52:08 +0800119typedef struct vndkfwk_s {
120 void *lib_handle;
121 int (*isVendorEnhancedFwk)(void);
122 int (*getVendorEnhancedInfo)(void);
123 const char *lib_name;
124} vndkfwk_t;
125
126static vndkfwk_t mVndkFwk = {
127 NULL, NULL, NULL, VNDK_FWK_LIB_PATH};
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -0700128
`Deeraj Soman676c2702017-09-18 19:25:53 +0530129typedef struct {
130 const char *id_string;
131 const int value;
132} mixer_config_lookup;
133
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700134struct string_to_enum {
135 const char *name;
136 uint32_t value;
137};
138
139const struct string_to_enum s_flag_name_to_enum_table[] = {
140 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_DIRECT),
141 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_PRIMARY),
142 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_FAST),
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800143 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_RAW),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700144 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_DEEP_BUFFER),
145 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD),
146 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_NON_BLOCKING),
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700147 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_HW_AV_SYNC),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700148 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_INCALL_MUSIC),
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700149 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH),
Naresh Tanniruee3499a2017-01-05 14:05:35 +0530150 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_TIMESTAMP),
Vikram Panduranga93f080e2017-06-07 18:16:14 -0700151 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_VOIP_RX),
Ben Romberger6c4d3812017-06-13 17:46:45 -0700152 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_BD),
Varun Balaraje49253e2017-07-06 19:48:56 +0530153 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_INTERACTIVE),
Derek Chen090dfc52018-03-22 22:15:29 -0400154 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_MEDIA),
155 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_SYS_NOTIFICATION),
156 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_NAV_GUIDANCE),
157 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_PHONE),
Derek Chen1bcdc6b2020-02-06 22:44:53 -0800158 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_FRONT_PASSENGER),
Derek Chendf05eea2019-08-01 13:57:49 -0700159 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_REAR_SEAT),
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530160 STRING_TO_ENUM(AUDIO_INPUT_FLAG_NONE),
161 STRING_TO_ENUM(AUDIO_INPUT_FLAG_FAST),
162 STRING_TO_ENUM(AUDIO_INPUT_FLAG_HW_HOTWORD),
163 STRING_TO_ENUM(AUDIO_INPUT_FLAG_RAW),
164 STRING_TO_ENUM(AUDIO_INPUT_FLAG_SYNC),
Dhananjay Kumaree4d2002016-10-25 18:02:58 +0530165 STRING_TO_ENUM(AUDIO_INPUT_FLAG_TIMESTAMP),
Dhananjay Kumar704ce6f2017-09-28 22:08:00 +0530166 STRING_TO_ENUM(AUDIO_INPUT_FLAG_COMPRESS),
Ralf Herzaec80262018-07-03 07:08:49 +0200167 STRING_TO_ENUM(AUDIO_INPUT_FLAG_PASSTHROUGH),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700168};
169
170const struct string_to_enum s_format_name_to_enum_table[] = {
Ashish Jain83a6cc22016-06-28 14:34:17 +0530171 STRING_TO_ENUM(AUDIO_FORMAT_PCM_8_BIT),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700172 STRING_TO_ENUM(AUDIO_FORMAT_PCM_16_BIT),
Ashish Jain5106d362016-05-11 19:23:33 +0530173 STRING_TO_ENUM(AUDIO_FORMAT_PCM_24_BIT_PACKED),
174 STRING_TO_ENUM(AUDIO_FORMAT_PCM_8_24_BIT),
Ashish Jainf1eaa582016-05-23 20:54:24 +0530175 STRING_TO_ENUM(AUDIO_FORMAT_PCM_32_BIT),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700176 STRING_TO_ENUM(AUDIO_FORMAT_MP3),
177 STRING_TO_ENUM(AUDIO_FORMAT_AAC),
178 STRING_TO_ENUM(AUDIO_FORMAT_VORBIS),
Mingming Yinae3530f2014-07-03 16:50:18 -0700179 STRING_TO_ENUM(AUDIO_FORMAT_AMR_NB),
180 STRING_TO_ENUM(AUDIO_FORMAT_AMR_WB),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700181 STRING_TO_ENUM(AUDIO_FORMAT_AC3),
Mingming Yinae3530f2014-07-03 16:50:18 -0700182 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700183 STRING_TO_ENUM(AUDIO_FORMAT_DTS),
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530184 STRING_TO_ENUM(AUDIO_FORMAT_DTS_HD),
Ben Romberger1aaaf862017-04-06 17:49:46 -0700185 STRING_TO_ENUM(AUDIO_FORMAT_DOLBY_TRUEHD),
Naresh Tanniru928f0862017-04-07 16:44:23 -0700186 STRING_TO_ENUM(AUDIO_FORMAT_IEC61937),
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530187 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3_JOC),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700188 STRING_TO_ENUM(AUDIO_FORMAT_WMA),
189 STRING_TO_ENUM(AUDIO_FORMAT_WMA_PRO),
190 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADIF),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700191 STRING_TO_ENUM(AUDIO_FORMAT_AMR_WB_PLUS),
192 STRING_TO_ENUM(AUDIO_FORMAT_EVRC),
193 STRING_TO_ENUM(AUDIO_FORMAT_EVRCB),
194 STRING_TO_ENUM(AUDIO_FORMAT_EVRCWB),
195 STRING_TO_ENUM(AUDIO_FORMAT_QCELP),
196 STRING_TO_ENUM(AUDIO_FORMAT_MP2),
197 STRING_TO_ENUM(AUDIO_FORMAT_EVRCNW),
Amit Shekhar6f461b12014-08-01 14:52:58 -0700198 STRING_TO_ENUM(AUDIO_FORMAT_FLAC),
Satya Krishna Pindiproli70471602015-04-24 19:12:43 +0530199 STRING_TO_ENUM(AUDIO_FORMAT_ALAC),
200 STRING_TO_ENUM(AUDIO_FORMAT_APE),
Alexy Josephcd8eaed2014-12-11 12:46:53 -0800201 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LC),
202 STRING_TO_ENUM(AUDIO_FORMAT_AAC_HE_V1),
203 STRING_TO_ENUM(AUDIO_FORMAT_AAC_HE_V2),
Manish Dewangana6fc5442015-08-24 20:30:31 +0530204 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS),
Ashish Jaine513a872015-11-19 17:00:56 +0530205 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_LC),
206 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_HE_V1),
207 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_HE_V2),
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +0530208 STRING_TO_ENUM(AUDIO_FORMAT_DSD),
Arun Kumar Dasari3b174182016-12-27 13:01:14 +0530209 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM),
210 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM_LC),
211 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM_HE_V1),
212 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM_HE_V2),
Dhanalakshmi Siddani18737932016-11-29 17:33:17 +0530213 STRING_TO_ENUM(AUDIO_FORMAT_APTX),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700214};
215
Manish Dewangan3ccdea52017-02-13 19:31:54 +0530216/* payload structure avt_device drift query */
217struct audio_avt_device_drift_stats {
218 uint32_t minor_version;
Manish Dewangan338c50a2017-09-12 15:22:03 +0530219
Manish Dewangan3ccdea52017-02-13 19:31:54 +0530220 /* Indicates the device interface direction as either
221 * source (Tx) or sink (Rx).
222 */
223 uint16_t device_direction;
Manish Dewangan338c50a2017-09-12 15:22:03 +0530224
225 /* Reference timer for drift accumulation and time stamp information.
226 * currently it only support AFE_REF_TIMER_TYPE_AVTIMER
227 */
228 uint16_t reference_timer;
Manish Dewangan3ccdea52017-02-13 19:31:54 +0530229 struct audio_avt_device_drift_param drift_param;
Manish Dewangan338c50a2017-09-12 15:22:03 +0530230} __attribute__((packed));
Manish Dewangan3ccdea52017-02-13 19:31:54 +0530231
Ben Rombergera04fabc2014-11-14 12:16:03 -0800232static char bTable[BASE_TABLE_SIZE] = {
233 'A','B','C','D','E','F','G','H','I','J','K','L',
234 'M','N','O','P','Q','R','S','T','U','V','W','X',
235 'Y','Z','a','b','c','d','e','f','g','h','i','j',
236 'k','l','m','n','o','p','q','r','s','t','u','v',
237 'w','x','y','z','0','1','2','3','4','5','6','7',
238 '8','9','+','/'
239};
240
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700241static uint32_t string_to_enum(const struct string_to_enum *table, size_t size,
242 const char *name)
243{
244 size_t i;
245 for (i = 0; i < size; i++) {
246 if (strcmp(table[i].name, name) == 0) {
247 ALOGV("%s found %s", __func__, table[i].name);
248 return table[i].value;
249 }
250 }
251 return 0;
252}
253
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530254static audio_io_flags_t parse_flag_names(char *name)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700255{
256 uint32_t flag = 0;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530257 audio_io_flags_t io_flags;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800258 char *last_r;
259 char *flag_name = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700260 while (flag_name != NULL) {
261 if (strlen(flag_name) != 0) {
262 flag |= string_to_enum(s_flag_name_to_enum_table,
263 ARRAY_SIZE(s_flag_name_to_enum_table),
264 flag_name);
265 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800266 flag_name = strtok_r(NULL, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700267 }
268
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800269 ALOGV("parse_flag_names: flag - %x", flag);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530270 io_flags.in_flags = (audio_input_flags_t)flag;
271 io_flags.out_flags = (audio_output_flags_t)flag;
272 return io_flags;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700273}
274
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530275static void parse_format_names(char *name, struct streams_io_cfg *s_info)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700276{
277 struct stream_format *sf_info = NULL;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800278 char *last_r;
279 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700280
281 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG) == 0)
282 return;
283
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530284 list_init(&s_info->format_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700285 while (str != NULL) {
286 audio_format_t format = (audio_format_t)string_to_enum(s_format_name_to_enum_table,
287 ARRAY_SIZE(s_format_name_to_enum_table), str);
288 ALOGV("%s: format - %d", __func__, format);
289 if (format != 0) {
290 sf_info = (struct stream_format *)calloc(1, sizeof(struct stream_format));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700291 if (sf_info == NULL)
292 break; /* return whatever was parsed */
293
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700294 sf_info->format = format;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530295 list_add_tail(&s_info->format_list, &sf_info->list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700296 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800297 str = strtok_r(NULL, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700298 }
299}
300
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530301static void parse_sample_rate_names(char *name, struct streams_io_cfg *s_info)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700302{
Amit Shekhar6f461b12014-08-01 14:52:58 -0700303 struct stream_sample_rate *ss_info = NULL;
304 uint32_t sample_rate = 48000;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800305 char *last_r;
306 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700307
Amit Shekhar6f461b12014-08-01 14:52:58 -0700308 if (str != NULL && 0 == strcmp(str, DYNAMIC_VALUE_TAG))
309 return;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700310
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530311 list_init(&s_info->sample_rate_list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700312 while (str != NULL) {
313 sample_rate = (uint32_t)strtol(str, (char **)NULL, 10);
314 ALOGV("%s: sample_rate - %d", __func__, sample_rate);
315 if (0 != sample_rate) {
316 ss_info = (struct stream_sample_rate *)calloc(1, sizeof(struct stream_sample_rate));
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800317 if (!ss_info) {
318 ALOGE("%s: memory allocation failure", __func__);
319 return;
320 }
Amit Shekhar6f461b12014-08-01 14:52:58 -0700321 ss_info->sample_rate = sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530322 list_add_tail(&s_info->sample_rate_list, &ss_info->list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700323 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800324 str = strtok_r(NULL, "|", &last_r);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700325 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700326}
327
328static int parse_bit_width_names(char *name)
329{
330 int bit_width = 16;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800331 char *last_r;
332 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700333
334 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG))
335 bit_width = (int)strtol(str, (char **)NULL, 10);
336
337 ALOGV("%s: bit_width - %d", __func__, bit_width);
338 return bit_width;
339}
340
341static int parse_app_type_names(void *platform, char *name)
342{
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700343 int app_type = platform_get_default_app_type(platform);
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800344 char *last_r;
345 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700346
347 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG))
348 app_type = (int)strtol(str, (char **)NULL, 10);
349
350 ALOGV("%s: app_type - %d", __func__, app_type);
351 return app_type;
352}
353
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530354static void update_streams_cfg_list(cnode *root, void *platform,
355 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700356{
357 cnode *node = root->first_child;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530358 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700359
360 ALOGV("%s", __func__);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530361 s_info = (struct streams_io_cfg *)calloc(1, sizeof(struct streams_io_cfg));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700362
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530363 if (!s_info) {
364 ALOGE("failed to allocate mem for s_info list element");
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700365 return;
366 }
367
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700368 while (node) {
369 if (strcmp(node->name, FLAGS_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530370 s_info->flags = parse_flag_names((char *)node->value);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530371 } else if (strcmp(node->name, PROFILES_TAG) == 0) {
372 strlcpy(s_info->profile, (char *)node->value, sizeof(s_info->profile));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700373 } else if (strcmp(node->name, FORMATS_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530374 parse_format_names((char *)node->value, s_info);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700375 } else if (strcmp(node->name, SAMPLING_RATES_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530376 s_info->app_type_cfg.sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
377 parse_sample_rate_names((char *)node->value, s_info);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700378 } else if (strcmp(node->name, BIT_WIDTH_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530379 s_info->app_type_cfg.bit_width = parse_bit_width_names((char *)node->value);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700380 } else if (strcmp(node->name, APP_TYPE_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530381 s_info->app_type_cfg.app_type = parse_app_type_names(platform, (char *)node->value);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700382 }
383 node = node->next;
384 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530385 list_add_tail(streams_cfg_list, &s_info->list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700386}
387
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530388static void load_cfg_list(cnode *root, void *platform,
389 struct listnode *streams_output_cfg_list,
390 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700391{
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530392 cnode *node = NULL;
393
394 node = config_find(root, OUTPUTS_TAG);
395 if (node != NULL) {
396 node = node->first_child;
397 while (node) {
398 ALOGV("%s: loading output %s", __func__, node->name);
399 update_streams_cfg_list(node, platform, streams_output_cfg_list);
400 node = node->next;
401 }
402 } else {
403 ALOGI("%s: could not load output, node is NULL", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700404 }
405
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530406 node = config_find(root, INPUTS_TAG);
407 if (node != NULL) {
408 node = node->first_child;
409 while (node) {
410 ALOGV("%s: loading input %s", __func__, node->name);
411 update_streams_cfg_list(node, platform, streams_input_cfg_list);
412 node = node->next;
413 }
414 } else {
415 ALOGI("%s: could not load input, node is NULL", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700416 }
417}
418
419static void send_app_type_cfg(void *platform, struct mixer *mixer,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530420 struct listnode *streams_output_cfg_list,
421 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700422{
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530423 size_t app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT] = {0};
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700424 int length = 0, i, num_app_types = 0;
425 struct listnode *node;
426 bool update;
427 struct mixer_ctl *ctl = NULL;
428 const char *mixer_ctl_name = "App Type Config";
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530429 struct streams_io_cfg *s_info = NULL;
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800430 uint32_t target_bit_width = 0;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700431
432 if (!mixer) {
433 ALOGE("%s: mixer is null",__func__);
434 return;
435 }
436 ctl = mixer_get_ctl_by_name(mixer, mixer_ctl_name);
437 if (!ctl) {
438 ALOGE("%s: Could not get ctl for mixer cmd - %s",__func__, mixer_ctl_name);
439 return;
440 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530441 app_type_cfg[length++] = num_app_types;
442
443 if (list_empty(streams_output_cfg_list)) {
444 app_type_cfg[length++] = platform_get_default_app_type_v2(platform, PCM_PLAYBACK);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700445 app_type_cfg[length++] = 48000;
446 app_type_cfg[length++] = 16;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530447 num_app_types += 1;
448 }
449 if (list_empty(streams_input_cfg_list)) {
450 app_type_cfg[length++] = platform_get_default_app_type_v2(platform, PCM_CAPTURE);
451 app_type_cfg[length++] = 48000;
452 app_type_cfg[length++] = 16;
453 num_app_types += 1;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700454 }
455
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800456 /* get target bit width for ADM enforce mode */
457 target_bit_width = adev_get_dsp_bit_width_enforce_mode();
458
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700459 list_for_each(node, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530460 s_info = node_to_item(node, struct streams_io_cfg, list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700461 update = true;
462 for (i=0; i<length; i=i+3) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530463 if (app_type_cfg[i+1] == 0)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700464 break;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530465 else if (app_type_cfg[i+1] == (size_t)s_info->app_type_cfg.app_type) {
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530466 if (app_type_cfg[i+2] < (size_t)s_info->app_type_cfg.sample_rate)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530467 app_type_cfg[i+2] = s_info->app_type_cfg.sample_rate;
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530468 if (app_type_cfg[i+3] < (size_t)s_info->app_type_cfg.bit_width)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530469 app_type_cfg[i+3] = s_info->app_type_cfg.bit_width;
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800470 /* ADM bit width = max(enforce_bit_width, bit_width from s_info */
471 if (audio_extn_is_dsp_bit_width_enforce_mode_supported(s_info->flags.out_flags) &&
472 (target_bit_width > app_type_cfg[i+3]))
473 app_type_cfg[i+3] = target_bit_width;
474
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700475 update = false;
476 break;
477 }
478 }
479 if (update && ((length + 3) <= MAX_LENGTH_MIXER_CONTROL_IN_INT)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530480 num_app_types += 1;
481 app_type_cfg[length++] = s_info->app_type_cfg.app_type;
482 app_type_cfg[length++] = s_info->app_type_cfg.sample_rate;
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800483 app_type_cfg[length] = s_info->app_type_cfg.bit_width;
484 if (audio_extn_is_dsp_bit_width_enforce_mode_supported(s_info->flags.out_flags) &&
485 (target_bit_width > app_type_cfg[length]))
486 app_type_cfg[length] = target_bit_width;
487
488 length++;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530489 }
490 }
491 list_for_each(node, streams_input_cfg_list) {
492 s_info = node_to_item(node, struct streams_io_cfg, list);
493 update = true;
494 for (i=0; i<length; i=i+3) {
495 if (app_type_cfg[i+1] == 0)
496 break;
497 else if (app_type_cfg[i+1] == (size_t)s_info->app_type_cfg.app_type) {
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530498 if (app_type_cfg[i+2] < (size_t)s_info->app_type_cfg.sample_rate)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530499 app_type_cfg[i+2] = s_info->app_type_cfg.sample_rate;
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530500 if (app_type_cfg[i+3] < (size_t)s_info->app_type_cfg.bit_width)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530501 app_type_cfg[i+3] = s_info->app_type_cfg.bit_width;
502 update = false;
503 break;
504 }
505 }
506 if (update && ((length + 3) <= MAX_LENGTH_MIXER_CONTROL_IN_INT)) {
507 num_app_types += 1;
508 app_type_cfg[length++] = s_info->app_type_cfg.app_type;
509 app_type_cfg[length++] = s_info->app_type_cfg.sample_rate;
510 app_type_cfg[length++] = s_info->app_type_cfg.bit_width;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700511 }
512 }
513 ALOGV("%s: num_app_types: %d", __func__, num_app_types);
514 if (num_app_types) {
515 app_type_cfg[0] = num_app_types;
516 mixer_ctl_set_array(ctl, app_type_cfg, length);
517 }
518}
519
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530520void audio_extn_utils_update_streams_cfg_lists(void *platform,
521 struct mixer *mixer,
522 struct listnode *streams_output_cfg_list,
523 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700524{
525 cnode *root;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530526 char *data = NULL;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700527
528 ALOGV("%s", __func__);
529 list_init(streams_output_cfg_list);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530530 list_init(streams_input_cfg_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700531
532 root = config_node("", "");
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700533 if (root == NULL) {
534 ALOGE("cfg_list, NULL config root");
535 return;
536 }
537
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530538 data = (char *)load_file(AUDIO_IO_POLICY_VENDOR_CONFIG_FILE, NULL);
539 if (data == NULL) {
540 ALOGD("%s: failed to open io config file(%s), trying older config file",
541 __func__, AUDIO_IO_POLICY_VENDOR_CONFIG_FILE);
542 data = (char *)load_file(AUDIO_OUTPUT_POLICY_VENDOR_CONFIG_FILE, NULL);
543 if (data == NULL) {
544 send_app_type_cfg(platform, mixer,
545 streams_output_cfg_list,
546 streams_input_cfg_list);
547 ALOGE("%s: could not load io policy config!", __func__);
yidongh6eb4f752017-04-19 18:20:57 +0800548 free(root);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530549 return;
550 }
551 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700552
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530553 config_load(root, data);
554 load_cfg_list(root, platform, streams_output_cfg_list,
555 streams_input_cfg_list);
556
557 send_app_type_cfg(platform, mixer, streams_output_cfg_list,
558 streams_input_cfg_list);
Alexy Josephaee4fdd2016-01-29 13:02:07 -0800559
560 config_free(root);
yidongh6eb4f752017-04-19 18:20:57 +0800561 free(root);
Alexy Josephaee4fdd2016-01-29 13:02:07 -0800562 free(data);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700563}
564
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530565static void audio_extn_utils_dump_streams_cfg_list(
566 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700567{
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700568 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530569 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700570 struct stream_format *sf_info;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700571 struct stream_sample_rate *ss_info;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530572
573 list_for_each(node_i, streams_cfg_list) {
574 s_info = node_to_item(node_i, struct streams_io_cfg, list);
575 ALOGV("%s: flags-%d, sample_rate-%d, bit_width-%d, app_type-%d",
576 __func__, s_info->flags.out_flags, s_info->app_type_cfg.sample_rate,
577 s_info->app_type_cfg.bit_width, s_info->app_type_cfg.app_type);
578 list_for_each(node_j, &s_info->format_list) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700579 sf_info = node_to_item(node_j, struct stream_format, list);
580 ALOGV("format-%x", sf_info->format);
581 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530582 list_for_each(node_j, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700583 ss_info = node_to_item(node_j, struct stream_sample_rate, list);
584 ALOGV("sample rate-%d", ss_info->sample_rate);
585 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700586 }
587}
588
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530589void audio_extn_utils_dump_streams_cfg_lists(
590 struct listnode *streams_output_cfg_list,
591 struct listnode *streams_input_cfg_list)
592{
593 ALOGV("%s", __func__);
594 audio_extn_utils_dump_streams_cfg_list(streams_output_cfg_list);
595 audio_extn_utils_dump_streams_cfg_list(streams_input_cfg_list);
596}
597
598static void audio_extn_utils_release_streams_cfg_list(
599 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700600{
601 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530602 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700603
604 ALOGV("%s", __func__);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530605
606 while (!list_empty(streams_cfg_list)) {
607 node_i = list_head(streams_cfg_list);
608 s_info = node_to_item(node_i, struct streams_io_cfg, list);
609 while (!list_empty(&s_info->format_list)) {
610 node_j = list_head(&s_info->format_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700611 list_remove(node_j);
612 free(node_to_item(node_j, struct stream_format, list));
613 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530614 while (!list_empty(&s_info->sample_rate_list)) {
615 node_j = list_head(&s_info->sample_rate_list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700616 list_remove(node_j);
617 free(node_to_item(node_j, struct stream_sample_rate, list));
618 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700619 list_remove(node_i);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530620 free(node_to_item(node_i, struct streams_io_cfg, list));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700621 }
622}
623
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530624void audio_extn_utils_release_streams_cfg_lists(
625 struct listnode *streams_output_cfg_list,
626 struct listnode *streams_input_cfg_list)
627{
628 ALOGV("%s", __func__);
629 audio_extn_utils_release_streams_cfg_list(streams_output_cfg_list);
630 audio_extn_utils_release_streams_cfg_list(streams_input_cfg_list);
631}
632
633static bool set_app_type_cfg(struct streams_io_cfg *s_info,
634 struct stream_app_type_cfg *app_type_cfg,
635 uint32_t sample_rate, uint32_t bit_width)
Amit Shekhar6f461b12014-08-01 14:52:58 -0700636 {
637 struct listnode *node_i;
638 struct stream_sample_rate *ss_info;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530639 list_for_each(node_i, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700640 ss_info = node_to_item(node_i, struct stream_sample_rate, list);
641 if ((sample_rate <= ss_info->sample_rate) &&
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530642 (bit_width == s_info->app_type_cfg.bit_width)) {
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700643
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530644 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700645 app_type_cfg->sample_rate = ss_info->sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530646 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700647 ALOGV("%s app_type_cfg->app_type %d, app_type_cfg->sample_rate %d, app_type_cfg->bit_width %d",
648 __func__, app_type_cfg->app_type, app_type_cfg->sample_rate, app_type_cfg->bit_width);
649 return true;
650 }
651 }
652 /*
653 * Reiterate through the list assuming dafault sample rate.
654 * Handles scenario where input sample rate is higher
655 * than all sample rates in list for the input bit width.
656 */
657 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
Apoorv Raghuvanshif59bb222015-02-18 12:23:23 -0800658
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530659 list_for_each(node_i, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700660 ss_info = node_to_item(node_i, struct stream_sample_rate, list);
661 if ((sample_rate <= ss_info->sample_rate) &&
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530662 (bit_width == s_info->app_type_cfg.bit_width)) {
663 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700664 app_type_cfg->sample_rate = sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530665 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Apoorv Raghuvanshif59bb222015-02-18 12:23:23 -0800666 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 -0700667 __func__, app_type_cfg->app_type, app_type_cfg->sample_rate, app_type_cfg->bit_width);
668 return true;
669 }
670 }
671 return false;
672}
673
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530674void audio_extn_utils_update_stream_input_app_type_cfg(void *platform,
675 struct listnode *streams_input_cfg_list,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -0800676 struct listnode *devices __unused,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530677 audio_input_flags_t flags,
678 audio_format_t format,
679 uint32_t sample_rate,
680 uint32_t bit_width,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530681 char* profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530682 struct stream_app_type_cfg *app_type_cfg)
683{
684 struct listnode *node_i, *node_j;
685 struct streams_io_cfg *s_info;
686 struct stream_format *sf_info;
687
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530688 ALOGV("%s: flags: 0x%x, format: 0x%x sample_rate %d, profile %s",
689 __func__, flags, format, sample_rate, profile);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530690
691 list_for_each(node_i, streams_input_cfg_list) {
692 s_info = node_to_item(node_i, struct streams_io_cfg, list);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530693 /* Along with flags do profile matching if set at either end.*/
694 if (s_info->flags.in_flags == flags &&
695 ((profile[0] == '\0' && s_info->profile[0] == '\0') ||
696 strncmp(s_info->profile, profile, sizeof(s_info->profile)) == 0)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530697 list_for_each(node_j, &s_info->format_list) {
698 sf_info = node_to_item(node_j, struct stream_format, list);
699 if (sf_info->format == format) {
700 if (set_app_type_cfg(s_info, app_type_cfg, sample_rate, bit_width))
701 return;
702 }
703 }
704 }
705 }
706 ALOGW("%s: App type could not be selected. Falling back to default", __func__);
707 app_type_cfg->app_type = platform_get_default_app_type_v2(platform, PCM_CAPTURE);
708 app_type_cfg->sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
709 app_type_cfg->bit_width = 16;
710}
711
712void audio_extn_utils_update_stream_output_app_type_cfg(void *platform,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700713 struct listnode *streams_output_cfg_list,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -0800714 struct listnode *devices,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700715 audio_output_flags_t flags,
716 audio_format_t format,
Amit Shekhar6f461b12014-08-01 14:52:58 -0700717 uint32_t sample_rate,
718 uint32_t bit_width,
Manish Dewangan837dc462015-05-27 10:17:41 +0530719 audio_channel_mask_t channel_mask,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530720 char *profile,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700721 struct stream_app_type_cfg *app_type_cfg)
722{
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +0530723 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530724 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700725 struct stream_format *sf_info;
Manish Dewangan837dc462015-05-27 10:17:41 +0530726 char value[PROPERTY_VALUE_MAX] = {0};
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700727
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -0800728 if (compare_device_type(devices, AUDIO_DEVICE_OUT_SPEAKER)) {
Ramu Gottipati074fa4d2018-09-11 20:05:51 +0530729 int bw = platform_get_snd_device_bit_width(SND_DEVICE_OUT_SPEAKER);
730 if ((-ENOSYS != bw) && (bit_width > (uint32_t)bw))
Amit Shekhar5a39c912014-10-14 15:39:30 -0700731 bit_width = (uint32_t)bw;
Ramu Gottipati074fa4d2018-09-11 20:05:51 +0530732 else if (-ENOSYS == bw)
733 bit_width = CODEC_BACKEND_DEFAULT_BIT_WIDTH;
Amit Shekhar1d896042014-10-03 13:16:09 -0700734 sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Ramu Gottipati074fa4d2018-09-11 20:05:51 +0530735 ALOGI("%s Allowing 24 and above bits playback on speaker ONLY at default sampling rate", __func__);
Amit Shekhar1d896042014-10-03 13:16:09 -0700736 }
737
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -0700738 property_get("vendor.audio.playback.mch.downsample",value,"");
Manish Dewangan837dc462015-05-27 10:17:41 +0530739 if (!strncmp("true", value, sizeof("true"))) {
740 if ((popcount(channel_mask) > 2) &&
741 (sample_rate > CODEC_BACKEND_DEFAULT_SAMPLE_RATE) &&
Aalique Grahame5cd12ff2017-10-19 10:57:00 -0700742 !(flags & (audio_output_flags_t)AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH)) {
Manish Dewangan837dc462015-05-27 10:17:41 +0530743 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
744 ALOGD("%s: MCH session defaulting sample rate to %d",
745 __func__, sample_rate);
746 }
747 }
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +0530748
749 /* Set sampling rate to 176.4 for DSD64
750 * and 352.8Khz for DSD128.
751 * Set Bit Width to 16. output will be 16 bit
752 * post DoP in ASM.
753 */
Aalique Grahame5cd12ff2017-10-19 10:57:00 -0700754 if ((flags & (audio_output_flags_t)AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH) &&
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +0530755 (format == AUDIO_FORMAT_DSD)) {
756 bit_width = 16;
757 if (sample_rate == INPUT_SAMPLING_RATE_DSD64)
758 sample_rate = OUTPUT_SAMPLING_RATE_DSD64;
759 else if (sample_rate == INPUT_SAMPLING_RATE_DSD128)
760 sample_rate = OUTPUT_SAMPLING_RATE_DSD128;
761 }
762
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +0530763
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800764 ALOGV("%s: flags: %x, format: %x sample_rate %d, profile %s, app_type %d",
765 __func__, flags, format, sample_rate, profile, app_type_cfg->app_type);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700766 list_for_each(node_i, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530767 s_info = node_to_item(node_i, struct streams_io_cfg, list);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530768 /* Along with flags do profile matching if set at either end.*/
769 if (s_info->flags.out_flags == flags &&
770 ((profile[0] == '\0' && s_info->profile[0] == '\0') ||
771 strncmp(s_info->profile, profile, sizeof(s_info->profile)) == 0)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530772 list_for_each(node_j, &s_info->format_list) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700773 sf_info = node_to_item(node_j, struct stream_format, list);
774 if (sf_info->format == format) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530775 if (set_app_type_cfg(s_info, app_type_cfg, sample_rate, bit_width))
Amit Shekhar6f461b12014-08-01 14:52:58 -0700776 return;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700777 }
778 }
779 }
780 }
781 list_for_each(node_i, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530782 s_info = node_to_item(node_i, struct streams_io_cfg, list);
783 if (s_info->flags.out_flags == AUDIO_OUTPUT_FLAG_PRIMARY) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700784 ALOGV("Compatible output profile not found.");
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530785 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
786 app_type_cfg->sample_rate = s_info->app_type_cfg.sample_rate;
787 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700788 ALOGV("%s Default to primary output: App type: %d sample_rate %d",
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530789 __func__, s_info->app_type_cfg.app_type, app_type_cfg->sample_rate);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700790 return;
791 }
792 }
793 ALOGW("%s: App type could not be selected. Falling back to default", __func__);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700794 app_type_cfg->app_type = platform_get_default_app_type(platform);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700795 app_type_cfg->sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700796 app_type_cfg->bit_width = 16;
797}
798
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +0530799static bool audio_is_this_native_usecase(struct audio_usecase *uc)
800{
801 bool native_usecase = false;
802 struct stream_out *out = (struct stream_out*) uc->stream.out;
803
804 if (PCM_PLAYBACK == uc->type && out != NULL &&
805 NATIVE_AUDIO_MODE_INVALID != platform_get_native_support() &&
806 is_offload_usecase(uc->id) &&
807 (out->sample_rate == OUTPUT_SAMPLING_RATE_44100))
808 native_usecase = true;
809
810 return native_usecase;
811}
812
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800813bool audio_extn_is_dsp_bit_width_enforce_mode_supported(audio_output_flags_t flags)
814{
815 /* DSP bitwidth enforce mode for ADM and AFE:
816 * includes:
817 * deep buffer, low latency, direct pcm and offload.
818 * excludes:
819 * ull(raw+fast), VOIP.
820 */
821 if ((flags & AUDIO_OUTPUT_FLAG_VOIP_RX) ||
822 ((flags & AUDIO_OUTPUT_FLAG_RAW) &&
823 (flags & AUDIO_OUTPUT_FLAG_FAST)))
824 return false;
825
826
827 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) ||
828 (flags & AUDIO_OUTPUT_FLAG_DIRECT) ||
829 (flags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) ||
830 (flags & AUDIO_OUTPUT_FLAG_PRIMARY))
831 return true;
832 else
833 return false;
834}
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800835
836static inline bool audio_is_vr_mode_on(struct audio_device *(__attribute__((unused)) adev))
837{
838 return adev->vr_audio_mode_enabled;
839}
840
Derek Chenf7092792017-05-23 12:23:53 -0400841static void audio_extn_btsco_get_sample_rate(int snd_device, int *sample_rate)
842{
843 switch (snd_device) {
844 case SND_DEVICE_OUT_BT_SCO:
845 case SND_DEVICE_IN_BT_SCO_MIC:
846 case SND_DEVICE_IN_BT_SCO_MIC_NREC:
847 *sample_rate = 8000;
848 break;
849 case SND_DEVICE_OUT_BT_SCO_WB:
850 case SND_DEVICE_IN_BT_SCO_MIC_WB:
851 case SND_DEVICE_IN_BT_SCO_MIC_WB_NREC:
852 *sample_rate = 16000;
853 break;
854 default:
855 ALOGD("%s:Not a BT SCO device, need not update sampling rate\n", __func__);
856 break;
857 }
858}
859
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530860void audio_extn_utils_update_stream_app_type_cfg_for_usecase(
861 struct audio_device *adev,
862 struct audio_usecase *usecase)
863{
864 ALOGV("%s", __func__);
865
866 switch(usecase->type) {
867 case PCM_PLAYBACK:
868 audio_extn_utils_update_stream_output_app_type_cfg(adev->platform,
869 &adev->streams_output_cfg_list,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -0800870 &usecase->stream.out->device_list,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530871 usecase->stream.out->flags,
Aalique Grahame65780b52017-09-27 14:59:56 -0700872 usecase->stream.out->hal_op_format,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530873 usecase->stream.out->sample_rate,
874 usecase->stream.out->bit_width,
875 usecase->stream.out->channel_mask,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530876 usecase->stream.out->profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530877 &usecase->stream.out->app_type_cfg);
878 ALOGV("%s Selected apptype: %d", __func__, usecase->stream.out->app_type_cfg.app_type);
879 break;
880 case PCM_CAPTURE:
Vikram Panduranga93f080e2017-06-07 18:16:14 -0700881 if (usecase->id == USECASE_AUDIO_RECORD_VOIP)
882 usecase->stream.in->app_type_cfg.app_type = APP_TYPE_VOIP_AUDIO;
883 else
884 audio_extn_utils_update_stream_input_app_type_cfg(adev->platform,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530885 &adev->streams_input_cfg_list,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -0800886 &usecase->stream.in->device_list,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530887 usecase->stream.in->flags,
888 usecase->stream.in->format,
889 usecase->stream.in->sample_rate,
890 usecase->stream.in->bit_width,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530891 usecase->stream.in->profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530892 &usecase->stream.in->app_type_cfg);
893 ALOGV("%s Selected apptype: %d", __func__, usecase->stream.in->app_type_cfg.app_type);
894 break;
Surendar Karka93cd25a2018-08-28 14:21:37 +0530895 case TRANSCODE_LOOPBACK_RX :
Siddartha Shaik343abc62017-08-08 11:15:25 +0530896 audio_extn_utils_update_stream_output_app_type_cfg(adev->platform,
897 &adev->streams_output_cfg_list,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -0800898 &usecase->stream.inout->out_config.device_list,
Siddartha Shaik343abc62017-08-08 11:15:25 +0530899 0,
900 usecase->stream.inout->out_config.format,
901 usecase->stream.inout->out_config.sample_rate,
902 usecase->stream.inout->out_config.bit_width,
903 usecase->stream.inout->out_config.channel_mask,
904 usecase->stream.inout->profile,
905 &usecase->stream.inout->out_app_type_cfg);
906 ALOGV("%s Selected apptype: %d", __func__, usecase->stream.inout->out_app_type_cfg.app_type);
907 break;
Derek Chenf7092792017-05-23 12:23:53 -0400908 case PCM_HFP_CALL:
909 switch (usecase->id) {
910 case USECASE_AUDIO_HFP_SCO:
911 case USECASE_AUDIO_HFP_SCO_WB:
912 audio_extn_btsco_get_sample_rate(usecase->out_snd_device,
913 &usecase->out_app_type_cfg.sample_rate);
Derek Chend7307012019-11-12 12:10:41 -0500914 usecase->in_app_type_cfg.sample_rate = usecase->out_app_type_cfg.sample_rate;
Derek Chenf7092792017-05-23 12:23:53 -0400915 break;
916 case USECASE_AUDIO_HFP_SCO_DOWNLINK:
917 case USECASE_AUDIO_HFP_SCO_WB_DOWNLINK:
918 audio_extn_btsco_get_sample_rate(usecase->in_snd_device,
919 &usecase->in_app_type_cfg.sample_rate);
Derek Chend7307012019-11-12 12:10:41 -0500920 usecase->out_app_type_cfg.sample_rate = usecase->in_app_type_cfg.sample_rate;
Derek Chenf7092792017-05-23 12:23:53 -0400921 break;
922 default:
923 ALOGE("%s: usecase id (%d) not supported, use default sample rate",
924 __func__, usecase->id);
925 usecase->in_app_type_cfg.sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
926 usecase->out_app_type_cfg.sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
927 break;
928 }
929 /* update out_app_type_cfg */
930 usecase->out_app_type_cfg.bit_width =
931 platform_get_snd_device_bit_width(usecase->out_snd_device);
932 usecase->out_app_type_cfg.app_type =
933 platform_get_default_app_type_v2(adev->platform, PCM_PLAYBACK);
934 /* update in_app_type_cfg */
935 usecase->in_app_type_cfg.bit_width =
936 platform_get_snd_device_bit_width(usecase->in_snd_device);
937 usecase->in_app_type_cfg.app_type =
938 platform_get_default_app_type_v2(adev->platform, PCM_CAPTURE);
939 ALOGV("%s Selected apptype: playback %d capture %d",
940 __func__, usecase->out_app_type_cfg.app_type, usecase->in_app_type_cfg.app_type);
941 break;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530942 default:
943 ALOGE("%s: app type cfg not supported for usecase type (%d)",
944 __func__, usecase->type);
945 }
946}
947
Aalique Grahame22e49102018-12-18 14:23:57 -0800948static int set_stream_app_type_mixer_ctrl(struct audio_device *adev,
949 int pcm_device_id, int app_type,
950 int acdb_dev_id, int sample_rate,
951 int stream_type,
952 snd_device_t snd_device)
953{
954
955 char mixer_ctl_name[MAX_LENGTH_MIXER_CONTROL_IN_INT];
956 struct mixer_ctl *ctl;
957 int app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT], len = 0, rc = 0;
958 int snd_device_be_idx = -1;
959
960 if (stream_type == PCM_PLAYBACK) {
961 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
962 "Audio Stream %d App Type Cfg", pcm_device_id);
963 } else if (stream_type == PCM_CAPTURE) {
964 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
965 "Audio Stream Capture %d App Type Cfg", pcm_device_id);
966 }
967
968 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
969 if (!ctl) {
970 ALOGE("%s: Could not get ctl for mixer cmd - %s",
971 __func__, mixer_ctl_name);
972 rc = -EINVAL;
973 goto exit;
974 }
975 app_type_cfg[len++] = app_type;
976 app_type_cfg[len++] = acdb_dev_id;
977 app_type_cfg[len++] = sample_rate;
978
979 snd_device_be_idx = platform_get_snd_device_backend_index(snd_device);
980 if (snd_device_be_idx > 0)
981 app_type_cfg[len++] = snd_device_be_idx;
982 ALOGV("%s: stream type %d app_type %d, acdb_dev_id %d "
983 "sample rate %d, snd_device_be_idx %d",
984 __func__, stream_type, app_type, acdb_dev_id, sample_rate,
985 snd_device_be_idx);
986 mixer_ctl_set_array(ctl, app_type_cfg, len);
987
988exit:
989 return rc;
990}
991
992static int audio_extn_utils_send_app_type_cfg_hfp(struct audio_device *adev,
993 struct audio_usecase *usecase)
994{
995 int pcm_device_id, acdb_dev_id = 0, snd_device = usecase->out_snd_device;
996 int32_t sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
997 int app_type = 0, rc = 0;
Derek Chenf7092792017-05-23 12:23:53 -0400998 bool is_bus_dev_usecase = false;
Aalique Grahame22e49102018-12-18 14:23:57 -0800999
1000 ALOGV("%s", __func__);
1001
1002 if (usecase->type != PCM_HFP_CALL) {
Derek Chenf7092792017-05-23 12:23:53 -04001003 ALOGV("%s: not a HFP path, no need to cfg app type", __func__);
Aalique Grahame22e49102018-12-18 14:23:57 -08001004 rc = 0;
1005 goto exit_send_app_type_cfg;
1006 }
1007 if ((usecase->id != USECASE_AUDIO_HFP_SCO) &&
Derek Chenf7092792017-05-23 12:23:53 -04001008 (usecase->id != USECASE_AUDIO_HFP_SCO_WB) &&
1009 (usecase->id != USECASE_AUDIO_HFP_SCO_DOWNLINK) &&
1010 (usecase->id != USECASE_AUDIO_HFP_SCO_WB_DOWNLINK)) {
1011 ALOGV("%s: a usecase where app type cfg is not required", __func__);
Aalique Grahame22e49102018-12-18 14:23:57 -08001012 rc = 0;
1013 goto exit_send_app_type_cfg;
1014 }
1015
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08001016 if (compare_device_type(&usecase->device_list, AUDIO_DEVICE_OUT_BUS))
Derek Chenf7092792017-05-23 12:23:53 -04001017 is_bus_dev_usecase = true;
1018
Aalique Grahame22e49102018-12-18 14:23:57 -08001019 snd_device = usecase->out_snd_device;
1020 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_PLAYBACK);
1021
1022 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
1023 if (acdb_dev_id < 0) {
1024 ALOGE("%s: Couldn't get the acdb dev id", __func__);
1025 rc = -EINVAL;
1026 goto exit_send_app_type_cfg;
1027 }
1028
1029 if (usecase->type == PCM_HFP_CALL) {
1030
1031 /* config HFP session:1 playback path */
Derek Chenf7092792017-05-23 12:23:53 -04001032 if (is_bus_dev_usecase) {
1033 app_type = usecase->out_app_type_cfg.app_type;
1034 sample_rate= usecase->out_app_type_cfg.sample_rate;
1035 } else {
1036 snd_device = SND_DEVICE_NONE; // use legacy behavior
1037 app_type = platform_get_default_app_type_v2(adev->platform, PCM_PLAYBACK);
1038 sample_rate= CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
1039 }
Aalique Grahame22e49102018-12-18 14:23:57 -08001040 rc = set_stream_app_type_mixer_ctrl(adev, pcm_device_id, app_type,
1041 acdb_dev_id, sample_rate,
1042 PCM_PLAYBACK,
Derek Chenf7092792017-05-23 12:23:53 -04001043 snd_device);
Aalique Grahame22e49102018-12-18 14:23:57 -08001044 if (rc < 0)
1045 goto exit_send_app_type_cfg;
1046
1047 /* config HFP session:1 capture path */
Derek Chenf7092792017-05-23 12:23:53 -04001048 if (is_bus_dev_usecase) {
1049 snd_device = usecase->in_snd_device;
1050 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_CAPTURE);
1051 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
1052 if (acdb_dev_id < 0) {
1053 ALOGE("%s: Couldn't get the acdb dev id", __func__);
1054 rc = -EINVAL;
1055 goto exit_send_app_type_cfg;
1056 }
1057 app_type = usecase->in_app_type_cfg.app_type;
1058 sample_rate= usecase->in_app_type_cfg.sample_rate;
1059 } else {
1060 snd_device = SND_DEVICE_NONE; // use legacy behavior
1061 app_type = platform_get_default_app_type_v2(adev->platform, PCM_CAPTURE);
1062 }
Aalique Grahame22e49102018-12-18 14:23:57 -08001063 rc = set_stream_app_type_mixer_ctrl(adev, pcm_device_id, app_type,
1064 acdb_dev_id, sample_rate,
1065 PCM_CAPTURE,
Derek Chenf7092792017-05-23 12:23:53 -04001066 snd_device);
Aalique Grahame22e49102018-12-18 14:23:57 -08001067 if (rc < 0)
1068 goto exit_send_app_type_cfg;
1069
Derek Chenf7092792017-05-23 12:23:53 -04001070 if (is_bus_dev_usecase)
1071 goto exit_send_app_type_cfg;
1072
Aalique Grahame22e49102018-12-18 14:23:57 -08001073 /* config HFP session:2 capture path */
1074 pcm_device_id = HFP_ASM_RX_TX;
1075 snd_device = usecase->in_snd_device;
1076 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
1077 if (acdb_dev_id <= 0) {
1078 ALOGE("%s: Couldn't get the acdb dev id", __func__);
1079 rc = -EINVAL;
1080 goto exit_send_app_type_cfg;
1081 }
1082 app_type = platform_get_default_app_type_v2(adev->platform, PCM_CAPTURE);
1083 rc = set_stream_app_type_mixer_ctrl(adev, pcm_device_id, app_type,
1084 acdb_dev_id, sample_rate, PCM_CAPTURE,
1085 SND_DEVICE_NONE);
1086 if (rc < 0)
1087 goto exit_send_app_type_cfg;
1088
1089 /* config HFP session:2 playback path */
1090 app_type = platform_get_default_app_type_v2(adev->platform, PCM_PLAYBACK);
1091 rc = set_stream_app_type_mixer_ctrl(adev, pcm_device_id, app_type,
1092 acdb_dev_id, sample_rate,
1093 PCM_PLAYBACK, SND_DEVICE_NONE);
1094 if (rc < 0)
1095 goto exit_send_app_type_cfg;
1096 }
1097
1098 rc = 0;
1099exit_send_app_type_cfg:
1100 return rc;
1101}
1102
Zhou Song06761dd2019-04-28 18:08:17 +08001103int audio_extn_utils_get_app_sample_rate_for_device(
1104 struct audio_device *adev,
1105 struct audio_usecase *usecase, int snd_device)
1106{
1107 char value[PROPERTY_VALUE_MAX] = {0};
1108 int sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
1109
1110 if ((usecase->type == PCM_PLAYBACK) && (usecase->stream.out != NULL)) {
1111 property_get("vendor.audio.playback.mch.downsample",value,"");
1112 if (!strncmp("true", value, sizeof("true"))) {
1113 if ((popcount(usecase->stream.out->channel_mask) > 2) &&
1114 (usecase->stream.out->app_type_cfg.sample_rate > CODEC_BACKEND_DEFAULT_SAMPLE_RATE) &&
1115 !(usecase->stream.out->flags &
1116 (audio_output_flags_t)AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH))
1117 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
1118 }
1119
1120 if (usecase->id == USECASE_AUDIO_PLAYBACK_VOIP) {
1121 usecase->stream.out->app_type_cfg.sample_rate = usecase->stream.out->sample_rate;
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08001122 } else if (compare_device_type(&usecase->stream.out->device_list,
1123 AUDIO_DEVICE_OUT_SPEAKER)) {
Zhou Song06761dd2019-04-28 18:08:17 +08001124 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
1125 } else if ((snd_device == SND_DEVICE_OUT_HDMI ||
1126 snd_device == SND_DEVICE_OUT_USB_HEADSET ||
1127 snd_device == SND_DEVICE_OUT_DISPLAY_PORT) &&
1128 (usecase->stream.out->sample_rate >= OUTPUT_SAMPLING_RATE_44100)) {
1129 /*
1130 * To best utlize DSP, check if the stream sample rate is supported/multiple of
1131 * configured device sample rate, if not update the COPP rate to be equal to the
1132 * device sample rate, else open COPP at stream sample rate
1133 */
1134 platform_check_and_update_copp_sample_rate(adev->platform, snd_device,
1135 usecase->stream.out->sample_rate,
1136 &usecase->stream.out->app_type_cfg.sample_rate);
1137 } else if (((snd_device != SND_DEVICE_OUT_HEADPHONES_44_1 &&
1138 !audio_is_this_native_usecase(usecase)) &&
1139 usecase->stream.out->sample_rate == OUTPUT_SAMPLING_RATE_44100) ||
1140 (usecase->stream.out->sample_rate < OUTPUT_SAMPLING_RATE_44100)) {
1141 /* Reset to default if no native stream is active*/
1142 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Zhou Song9e9b3812019-12-30 17:52:10 +08001143 } else if (usecase->out_snd_device == SND_DEVICE_OUT_BT_A2DP ||
1144 usecase->out_snd_device == SND_DEVICE_OUT_SPEAKER_AND_BT_A2DP ||
1145 usecase->out_snd_device == SND_DEVICE_OUT_SPEAKER_SAFE_AND_BT_A2DP) {
Zhou Song06761dd2019-04-28 18:08:17 +08001146 /*
1147 * For a2dp playback get encoder sampling rate and set copp sampling rate,
1148 * for bit width use the stream param only.
1149 */
1150 audio_extn_a2dp_get_enc_sample_rate(&usecase->stream.out->app_type_cfg.sample_rate);
1151 ALOGI("%s using %d sample rate rate for A2DP CoPP",
1152 __func__, usecase->stream.out->app_type_cfg.sample_rate);
1153 }
1154 audio_extn_btsco_get_sample_rate(snd_device, &usecase->stream.out->app_type_cfg.sample_rate);
1155 sample_rate = usecase->stream.out->app_type_cfg.sample_rate;
1156
1157 if (((usecase->stream.out->format == AUDIO_FORMAT_E_AC3) ||
1158 (usecase->stream.out->format == AUDIO_FORMAT_E_AC3_JOC) ||
1159 (usecase->stream.out->format == AUDIO_FORMAT_DOLBY_TRUEHD))
1160 && audio_extn_passthru_is_passthrough_stream(usecase->stream.out)
1161 && !audio_extn_passthru_is_convert_supported(adev, usecase->stream.out)) {
1162 sample_rate = sample_rate * 4;
1163 if (sample_rate > HDMI_PASSTHROUGH_MAX_SAMPLE_RATE)
1164 sample_rate = HDMI_PASSTHROUGH_MAX_SAMPLE_RATE;
1165 }
1166 } else if (usecase->type == PCM_CAPTURE) {
1167 if (usecase->stream.in != NULL) {
1168 if (usecase->id == USECASE_AUDIO_RECORD_VOIP)
1169 usecase->stream.in->app_type_cfg.sample_rate = usecase->stream.in->sample_rate;
1170 if (voice_is_in_call_rec_stream(usecase->stream.in)) {
1171 audio_extn_btsco_get_sample_rate(usecase->in_snd_device,
1172 &usecase->stream.in->app_type_cfg.sample_rate);
1173 } else {
1174 audio_extn_btsco_get_sample_rate(snd_device,
1175 &usecase->stream.in->app_type_cfg.sample_rate);
1176 }
1177 sample_rate = usecase->stream.in->app_type_cfg.sample_rate;
1178 } else if (usecase->id == USECASE_AUDIO_SPKR_CALIB_TX) {
Sujin Panickerb6d76262019-09-09 10:55:38 +05301179 if ((property_get("vendor.audio.spkr_prot.tx.sampling_rate", value, NULL) > 0))
1180 sample_rate = atoi(value);
1181 else
1182 sample_rate = SAMPLE_RATE_8000;
Zhou Song06761dd2019-04-28 18:08:17 +08001183 }
1184 } else if (usecase->type == TRANSCODE_LOOPBACK_RX) {
1185 sample_rate = usecase->stream.inout->out_config.sample_rate;
1186 }
1187 return sample_rate;
1188}
1189
Siena Richard7c2db772016-12-21 11:32:34 -08001190static int send_app_type_cfg_for_device(struct audio_device *adev,
1191 struct audio_usecase *usecase,
1192 int split_snd_device)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001193{
1194 char mixer_ctl_name[MAX_LENGTH_MIXER_CONTROL_IN_INT];
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301195 size_t app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT] = {0};
1196 int len = 0, rc;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001197 struct mixer_ctl *ctl;
Siena Richard7c2db772016-12-21 11:32:34 -08001198 int pcm_device_id = 0, acdb_dev_id, app_type;
1199 int snd_device = split_snd_device, snd_device_be_idx = -1;
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +05301200 int32_t sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Varun Balaraje49253e2017-07-06 19:48:56 +05301201 struct streams_io_cfg *s_info = NULL;
1202 struct listnode *node = NULL;
Nikhil Laturkarac4a7492017-08-31 18:27:19 +05301203 int bd_app_type = 0;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001204
Siena Richard7c2db772016-12-21 11:32:34 -08001205 ALOGV("%s: usecase->out_snd_device %s, usecase->in_snd_device %s, split_snd_device %s",
1206 __func__, platform_get_snd_device_name(usecase->out_snd_device),
1207 platform_get_snd_device_name(usecase->in_snd_device),
1208 platform_get_snd_device_name(split_snd_device));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001209
Siddartha Shaik343abc62017-08-08 11:15:25 +05301210 if (usecase->type != PCM_PLAYBACK && usecase->type != PCM_CAPTURE &&
Surendar Karka93cd25a2018-08-28 14:21:37 +05301211 usecase->type != TRANSCODE_LOOPBACK_RX) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301212 ALOGE("%s: not a playback/capture path, no need to cfg app type", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001213 rc = 0;
1214 goto exit_send_app_type_cfg;
1215 }
1216 if ((usecase->id != USECASE_AUDIO_PLAYBACK_DEEP_BUFFER) &&
Vignesh Kulothungan0a2eff02019-07-11 16:30:13 -07001217 (usecase->id != USECASE_AUDIO_PLAYBACK_WITH_HAPTICS) &&
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001218 (usecase->id != USECASE_AUDIO_PLAYBACK_LOW_LATENCY) &&
1219 (usecase->id != USECASE_AUDIO_PLAYBACK_MULTI_CH) &&
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -08001220 (usecase->id != USECASE_AUDIO_PLAYBACK_ULL) &&
Vikram Panduranga93f080e2017-06-07 18:16:14 -07001221 (usecase->id != USECASE_AUDIO_PLAYBACK_VOIP) &&
Surendar Karka93cd25a2018-08-28 14:21:37 +05301222 (usecase->id != USECASE_AUDIO_TRANSCODE_LOOPBACK_RX) &&
Varun Balaraje49253e2017-07-06 19:48:56 +05301223 (!is_interactive_usecase(usecase->id)) &&
Srikanth Uyyala9d551402015-08-25 16:03:42 +05301224 (!is_offload_usecase(usecase->id)) &&
Derek Chenf6318be2017-06-12 17:16:24 -04001225 (usecase->type != PCM_CAPTURE) &&
1226 (!audio_extn_auto_hal_is_bus_device_usecase(usecase->id))) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301227 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 -07001228 rc = 0;
1229 goto exit_send_app_type_cfg;
1230 }
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -08001231
1232 //if VR is active then only send the mixer control
1233 if (usecase->id == USECASE_AUDIO_PLAYBACK_ULL && !audio_is_vr_mode_on(adev)) {
1234 ALOGI("ULL doesnt need sending app type cfg, returning");
1235 rc = 0;
1236 goto exit_send_app_type_cfg;
1237 }
1238
Surendar Karka93cd25a2018-08-28 14:21:37 +05301239 if (usecase->type == PCM_PLAYBACK || usecase->type == TRANSCODE_LOOPBACK_RX) {
Ben Romberger1fafdde2015-09-09 19:43:15 -07001240 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_PLAYBACK);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301241 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
1242 "Audio Stream %d App Type Cfg", pcm_device_id);
Ben Romberger1fafdde2015-09-09 19:43:15 -07001243 } else if (usecase->type == PCM_CAPTURE) {
Ben Romberger1fafdde2015-09-09 19:43:15 -07001244 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_CAPTURE);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301245 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
1246 "Audio Stream Capture %d App Type Cfg", pcm_device_id);
Srikanth Uyyala9d551402015-08-25 16:03:42 +05301247 }
Siena Richard7c2db772016-12-21 11:32:34 -08001248
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001249 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
1250 if (!ctl) {
1251 ALOGE("%s: Could not get ctl for mixer cmd - %s", __func__,
1252 mixer_ctl_name);
1253 rc = -EINVAL;
1254 goto exit_send_app_type_cfg;
1255 }
Aditya Bavanari701a6992017-03-30 19:17:16 +05301256 snd_device = platform_get_spkr_prot_snd_device(snd_device);
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301257 if (voice_is_in_call_rec_stream(usecase->stream.in) && usecase->type == PCM_CAPTURE) {
1258 snd_device_t voice_device = voice_get_incall_rec_snd_device(usecase->in_snd_device);
1259 acdb_dev_id = platform_get_snd_device_acdb_id(voice_device);
1260 ALOGV("acdb id for voice call use case %d", acdb_dev_id);
1261 } else {
1262 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
1263 }
Rohit Kumar1181b292017-01-31 18:07:17 +05301264 if (acdb_dev_id <= 0) {
1265 ALOGE("%s: Couldn't get the acdb dev id", __func__);
1266 rc = -EINVAL;
1267 goto exit_send_app_type_cfg;
1268 }
1269
Siena Richard7c2db772016-12-21 11:32:34 -08001270 snd_device_be_idx = platform_get_snd_device_backend_index(snd_device);
1271 if (snd_device_be_idx < 0) {
1272 ALOGE("%s: Couldn't get the backend index for snd device %s ret=%d",
1273 __func__, platform_get_snd_device_name(snd_device),
1274 snd_device_be_idx);
1275 }
1276
Zhou Song06761dd2019-04-28 18:08:17 +08001277 sample_rate = audio_extn_utils_get_app_sample_rate_for_device(adev, usecase, snd_device);
1278
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301279 if ((usecase->type == PCM_PLAYBACK) && (usecase->stream.out != NULL)) {
Varun Balaraje49253e2017-07-06 19:48:56 +05301280 /* Interactive streams are supported with only direct app type id.
1281 * Get Direct profile app type and use it for interactive streams
1282 */
1283 list_for_each(node, &adev->streams_output_cfg_list) {
1284 s_info = node_to_item(node, struct streams_io_cfg, list);
Aalique Grahame5cd12ff2017-10-19 10:57:00 -07001285 if (s_info->flags.out_flags == (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_BD |
Nikhil Laturkarac4a7492017-08-31 18:27:19 +05301286 AUDIO_OUTPUT_FLAG_DIRECT_PCM |
1287 AUDIO_OUTPUT_FLAG_DIRECT))
1288 bd_app_type = s_info->app_type_cfg.app_type;
Varun Balaraje49253e2017-07-06 19:48:56 +05301289 }
Aalique Grahame5cd12ff2017-10-19 10:57:00 -07001290 if (usecase->stream.out->flags == (audio_output_flags_t)AUDIO_OUTPUT_FLAG_INTERACTIVE)
Nikhil Laturkarac4a7492017-08-31 18:27:19 +05301291 app_type = bd_app_type;
Varun Balaraje49253e2017-07-06 19:48:56 +05301292 else
1293 app_type = usecase->stream.out->app_type_cfg.app_type;
Siena Richard7c2db772016-12-21 11:32:34 -08001294 app_type_cfg[len++] = app_type;
Mingming Yin21854652016-04-13 11:54:02 -07001295 app_type_cfg[len++] = acdb_dev_id;
Naresh Tanniru3a406772017-05-10 13:09:05 -07001296 app_type_cfg[len++] = sample_rate;
1297
Siena Richard7c2db772016-12-21 11:32:34 -08001298 if (snd_device_be_idx > 0)
1299 app_type_cfg[len++] = snd_device_be_idx;
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301300
Siena Richard7c2db772016-12-21 11:32:34 -08001301 ALOGI("%s PLAYBACK app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
1302 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301303
1304 } else if ((usecase->type == PCM_CAPTURE) && (usecase->stream.in != NULL)) {
Siena Richard7c2db772016-12-21 11:32:34 -08001305 app_type = usecase->stream.in->app_type_cfg.app_type;
1306 app_type_cfg[len++] = app_type;
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301307 app_type_cfg[len++] = acdb_dev_id;
Siena Richard7c2db772016-12-21 11:32:34 -08001308 app_type_cfg[len++] = sample_rate;
1309 if (snd_device_be_idx > 0)
1310 app_type_cfg[len++] = snd_device_be_idx;
1311 ALOGI("%s CAPTURE app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
1312 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301313 } else {
Siena Richard7c2db772016-12-21 11:32:34 -08001314 app_type = platform_get_default_app_type_v2(adev->platform, usecase->type);
Surendar Karka93cd25a2018-08-28 14:21:37 +05301315 if(usecase->type == TRANSCODE_LOOPBACK_RX) {
Siddartha Shaik343abc62017-08-08 11:15:25 +05301316 app_type = usecase->stream.inout->out_app_type_cfg.app_type;
1317 }
Siena Richard7c2db772016-12-21 11:32:34 -08001318 app_type_cfg[len++] = app_type;
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301319 app_type_cfg[len++] = acdb_dev_id;
1320 app_type_cfg[len++] = sample_rate;
Siena Richard7c2db772016-12-21 11:32:34 -08001321 if (snd_device_be_idx > 0)
1322 app_type_cfg[len++] = snd_device_be_idx;
1323 ALOGI("%s default app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
1324 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Ashish Jainc02430d2016-01-04 10:42:43 +05301325 }
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301326
Siddartha Shaik343abc62017-08-08 11:15:25 +05301327 if(ctl)
1328 mixer_ctl_set_array(ctl, app_type_cfg, len);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001329 rc = 0;
1330exit_send_app_type_cfg:
1331 return rc;
1332}
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001333
Deeraj Soman14230922019-01-30 16:39:30 +05301334static int audio_extn_utils_check_input_parameters(uint32_t sample_rate,
1335 audio_format_t format,
1336 int channel_count)
1337{
1338 int ret = 0;
1339
1340 if (((format != AUDIO_FORMAT_PCM_16_BIT) && (format != AUDIO_FORMAT_PCM_8_24_BIT) &&
1341 (format != AUDIO_FORMAT_PCM_24_BIT_PACKED) && (format != AUDIO_FORMAT_PCM_32_BIT) &&
1342 (format != AUDIO_FORMAT_PCM_FLOAT)) &&
1343 !voice_extn_compress_voip_is_format_supported(format) &&
1344 !audio_extn_compr_cap_format_supported(format) &&
1345 !audio_extn_cin_format_supported(format))
1346 ret = -EINVAL;
1347
1348 switch (channel_count) {
1349 case 1:
1350 case 2:
1351 case 3:
1352 case 4:
1353 case 6:
1354 case 8:
1355 break;
1356 default:
1357 ret = -EINVAL;
1358 }
1359
1360 switch (sample_rate) {
1361 case 8000:
1362 case 11025:
1363 case 12000:
1364 case 16000:
1365 case 22050:
1366 case 24000:
1367 case 32000:
1368 case 44100:
1369 case 48000:
1370 case 88200:
1371 case 96000:
1372 case 176400:
1373 case 192000:
1374 break;
1375 default:
1376 ret = -EINVAL;
1377 }
1378
1379 return ret;
1380}
1381
1382static inline uint32_t audio_extn_utils_nearest_multiple(uint32_t num, uint32_t multiplier)
1383{
1384 uint32_t remainder = 0;
1385
1386 if (!multiplier)
1387 return num;
1388
1389 remainder = num % multiplier;
1390 if (remainder)
1391 num += (multiplier - remainder);
1392
1393 return num;
1394}
1395
1396static inline uint32_t audio_extn_utils_lcm(uint32_t num1, uint32_t num2)
1397{
1398 uint32_t high = num1, low = num2, temp = 0;
1399
1400 if (!num1 || !num2)
1401 return 0;
1402
1403 if (num1 < num2) {
1404 high = num2;
1405 low = num1;
1406 }
1407
1408 while (low != 0) {
1409 temp = low;
1410 low = high % low;
1411 high = temp;
1412 }
1413 return (num1 * num2)/high;
1414}
1415
Siena Richard7c2db772016-12-21 11:32:34 -08001416int audio_extn_utils_send_app_type_cfg(struct audio_device *adev,
1417 struct audio_usecase *usecase)
1418{
1419 int i, num_devices = 0;
1420 snd_device_t new_snd_devices[SND_DEVICE_OUT_END] = {0};
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301421 snd_device_t in_snd_device = usecase->in_snd_device;
Siena Richard7c2db772016-12-21 11:32:34 -08001422 int rc = 0;
1423
Aalique Grahame22e49102018-12-18 14:23:57 -08001424 if (usecase->type == PCM_HFP_CALL) {
1425 return audio_extn_utils_send_app_type_cfg_hfp(adev, usecase);
1426 }
1427
Siena Richard7c2db772016-12-21 11:32:34 -08001428 switch (usecase->type) {
1429 case PCM_PLAYBACK:
Surendar Karka93cd25a2018-08-28 14:21:37 +05301430 case TRANSCODE_LOOPBACK_RX:
Siena Richard7c2db772016-12-21 11:32:34 -08001431 ALOGD("%s: usecase->out_snd_device %s",
1432 __func__, platform_get_snd_device_name(usecase->out_snd_device));
1433 /* check for out combo device */
1434 if (platform_split_snd_device(adev->platform,
1435 usecase->out_snd_device,
1436 &num_devices, new_snd_devices)) {
1437 new_snd_devices[0] = usecase->out_snd_device;
1438 num_devices = 1;
1439 }
1440 break;
1441 case PCM_CAPTURE:
1442 ALOGD("%s: usecase->in_snd_device %s",
1443 __func__, platform_get_snd_device_name(usecase->in_snd_device));
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301444 if (voice_is_in_call_rec_stream(usecase->stream.in)) {
1445 in_snd_device = voice_get_incall_rec_backend_device(usecase->stream.in);
1446 }
Siena Richard7c2db772016-12-21 11:32:34 -08001447 /* check for in combo device */
1448 if (platform_split_snd_device(adev->platform,
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301449 in_snd_device,
Siena Richard7c2db772016-12-21 11:32:34 -08001450 &num_devices, new_snd_devices)) {
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301451 new_snd_devices[0] = in_snd_device;
Siena Richard7c2db772016-12-21 11:32:34 -08001452 num_devices = 1;
1453 }
1454 break;
1455 default:
1456 ALOGI("%s: not a playback/capture path, no need to cfg app type", __func__);
1457 rc = 0;
1458 break;
1459 }
1460
1461 for (i = 0; i < num_devices; i++) {
1462 rc = send_app_type_cfg_for_device(adev, usecase, new_snd_devices[i]);
1463 if (rc)
1464 break;
1465 }
1466
1467 return rc;
1468}
1469
Preetam Singh Ranawat9519e9c2015-11-18 16:05:55 +05301470int read_line_from_file(const char *path, char *buf, size_t count)
1471{
1472 char * fgets_ret;
1473 FILE * fd;
1474 int rv;
1475
1476 fd = fopen(path, "r");
1477 if (fd == NULL)
1478 return -1;
1479
1480 fgets_ret = fgets(buf, (int)count, fd);
1481 if (NULL != fgets_ret) {
1482 rv = (int)strlen(buf);
1483 } else {
1484 rv = ferror(fd);
1485 }
1486 fclose(fd);
1487
1488 return rv;
1489}
1490
Ashish Jainf1eaa582016-05-23 20:54:24 +05301491/*Translates ALSA formats to AOSP PCM formats*/
1492audio_format_t alsa_format_to_hal(uint32_t alsa_format)
1493{
1494 audio_format_t format;
1495
1496 switch(alsa_format) {
1497 case SNDRV_PCM_FORMAT_S16_LE:
1498 format = AUDIO_FORMAT_PCM_16_BIT;
1499 break;
1500 case SNDRV_PCM_FORMAT_S24_3LE:
1501 format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
1502 break;
1503 case SNDRV_PCM_FORMAT_S24_LE:
1504 format = AUDIO_FORMAT_PCM_8_24_BIT;
1505 break;
1506 case SNDRV_PCM_FORMAT_S32_LE:
1507 format = AUDIO_FORMAT_PCM_32_BIT;
1508 break;
1509 default:
1510 ALOGW("Incorrect ALSA format");
1511 format = AUDIO_FORMAT_INVALID;
1512 }
1513 return format;
1514}
1515
1516/*Translates hal format (AOSP) to alsa formats*/
1517uint32_t hal_format_to_alsa(audio_format_t hal_format)
1518{
1519 uint32_t alsa_format;
1520
1521 switch (hal_format) {
1522 case AUDIO_FORMAT_PCM_32_BIT: {
1523 if (platform_supports_true_32bit())
1524 alsa_format = SNDRV_PCM_FORMAT_S32_LE;
1525 else
1526 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1527 }
1528 break;
1529 case AUDIO_FORMAT_PCM_8_BIT:
1530 alsa_format = SNDRV_PCM_FORMAT_S8;
1531 break;
1532 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
1533 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1534 break;
1535 case AUDIO_FORMAT_PCM_8_24_BIT: {
1536 if (platform_supports_true_32bit())
1537 alsa_format = SNDRV_PCM_FORMAT_S32_LE;
1538 else
1539 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1540 }
1541 break;
1542 case AUDIO_FORMAT_PCM_FLOAT:
1543 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1544 break;
1545 default:
1546 case AUDIO_FORMAT_PCM_16_BIT:
1547 alsa_format = SNDRV_PCM_FORMAT_S16_LE;
1548 break;
1549 }
1550 return alsa_format;
1551}
1552
Ashish Jain83a6cc22016-06-28 14:34:17 +05301553/*Translates PCM formats to AOSP formats*/
1554audio_format_t pcm_format_to_hal(uint32_t pcm_format)
1555{
1556 audio_format_t format = AUDIO_FORMAT_INVALID;
1557
1558 switch(pcm_format) {
1559 case PCM_FORMAT_S16_LE:
1560 format = AUDIO_FORMAT_PCM_16_BIT;
1561 break;
1562 case PCM_FORMAT_S24_3LE:
1563 format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
1564 break;
1565 case PCM_FORMAT_S24_LE:
1566 format = AUDIO_FORMAT_PCM_8_24_BIT;
1567 break;
1568 case PCM_FORMAT_S32_LE:
1569 format = AUDIO_FORMAT_PCM_32_BIT;
1570 break;
1571 default:
1572 ALOGW("Incorrect PCM format");
1573 format = AUDIO_FORMAT_INVALID;
1574 }
1575 return format;
1576}
1577
1578/*Translates hal format (AOSP) to alsa formats*/
1579uint32_t hal_format_to_pcm(audio_format_t hal_format)
1580{
1581 uint32_t pcm_format;
1582
1583 switch (hal_format) {
1584 case AUDIO_FORMAT_PCM_32_BIT:
1585 case AUDIO_FORMAT_PCM_8_24_BIT:
1586 case AUDIO_FORMAT_PCM_FLOAT: {
1587 if (platform_supports_true_32bit())
1588 pcm_format = PCM_FORMAT_S32_LE;
1589 else
1590 pcm_format = PCM_FORMAT_S24_3LE;
1591 }
1592 break;
1593 case AUDIO_FORMAT_PCM_8_BIT:
1594 pcm_format = PCM_FORMAT_S8;
1595 break;
1596 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
1597 pcm_format = PCM_FORMAT_S24_3LE;
1598 break;
1599 default:
1600 case AUDIO_FORMAT_PCM_16_BIT:
1601 pcm_format = PCM_FORMAT_S16_LE;
1602 break;
1603 }
1604 return pcm_format;
1605}
1606
Ashish Jainf1eaa582016-05-23 20:54:24 +05301607uint32_t get_alsa_fragment_size(uint32_t bytes_per_sample,
1608 uint32_t sample_rate,
Deeraj Soman65358ab2019-02-07 15:40:49 +05301609 uint32_t noOfChannels,
1610 int64_t duration_ms)
Ashish Jainf1eaa582016-05-23 20:54:24 +05301611{
1612 uint32_t fragment_size = 0;
1613 uint32_t pcm_offload_time = PCM_OFFLOAD_BUFFER_DURATION;
1614
Deeraj Soman65358ab2019-02-07 15:40:49 +05301615 if (duration_ms >= MIN_OFFLOAD_BUFFER_DURATION_MS && duration_ms <= MAX_OFFLOAD_BUFFER_DURATION_MS)
1616 pcm_offload_time = duration_ms;
1617
Ashish Jainf1eaa582016-05-23 20:54:24 +05301618 fragment_size = (pcm_offload_time
1619 * sample_rate
1620 * bytes_per_sample
1621 * noOfChannels)/1000;
1622 if (fragment_size < MIN_PCM_OFFLOAD_FRAGMENT_SIZE)
1623 fragment_size = MIN_PCM_OFFLOAD_FRAGMENT_SIZE;
1624 else if (fragment_size > MAX_PCM_OFFLOAD_FRAGMENT_SIZE)
1625 fragment_size = MAX_PCM_OFFLOAD_FRAGMENT_SIZE;
1626 /*To have same PCM samples for all channels, the buffer size requires to
1627 *be multiple of (number of channels * bytes per sample)
1628 *For writes to succeed, the buffer must be written at address which is multiple of 32
1629 */
Aalique Grahameb85f2d92017-10-16 17:53:23 -07001630 fragment_size = ALIGN(fragment_size, (bytes_per_sample * noOfChannels * 32));
Ashish Jainf1eaa582016-05-23 20:54:24 +05301631
1632 ALOGI("PCM offload Fragment size to %d bytes", fragment_size);
1633 return fragment_size;
1634}
1635
1636/* Calculates the fragment size required to configure compress session.
1637 * Based on the alsa format selected, decide if conversion is needed in
1638
1639 * HAL ( e.g. convert AUDIO_FORMAT_PCM_FLOAT input format to
1640 * AUDIO_FORMAT_PCM_24_BIT_PACKED before writing to the compress driver.
1641 */
1642void audio_extn_utils_update_direct_pcm_fragment_size(struct stream_out *out)
1643{
Ashish Jain83a6cc22016-06-28 14:34:17 +05301644 audio_format_t dst_format = out->hal_op_format;
1645 audio_format_t src_format = out->hal_ip_format;
Ashish Jainf1eaa582016-05-23 20:54:24 +05301646 uint32_t hal_op_bytes_per_sample = audio_bytes_per_sample(dst_format);
1647 uint32_t hal_ip_bytes_per_sample = audio_bytes_per_sample(src_format);
1648
1649 out->compr_config.fragment_size =
1650 get_alsa_fragment_size(hal_op_bytes_per_sample,
1651 out->sample_rate,
Deeraj Soman65358ab2019-02-07 15:40:49 +05301652 popcount(out->channel_mask),
1653 out->info.duration_us / 1000);
Ashish Jainf1eaa582016-05-23 20:54:24 +05301654
1655 if ((src_format != dst_format) &&
1656 hal_op_bytes_per_sample != hal_ip_bytes_per_sample) {
1657
Ashish Jain83a6cc22016-06-28 14:34:17 +05301658 out->hal_fragment_size =
Ashish Jainf1eaa582016-05-23 20:54:24 +05301659 ((out->compr_config.fragment_size * hal_ip_bytes_per_sample) /
1660 hal_op_bytes_per_sample);
1661 ALOGI("enable conversion hal_input_fragment_size is %d src_format %x dst_format %x",
Ashish Jain83a6cc22016-06-28 14:34:17 +05301662 out->hal_fragment_size, src_format, dst_format);
Ashish Jainf1eaa582016-05-23 20:54:24 +05301663 } else {
Ashish Jain83a6cc22016-06-28 14:34:17 +05301664 out->hal_fragment_size = out->compr_config.fragment_size;
Ashish Jainf1eaa582016-05-23 20:54:24 +05301665 }
1666}
1667
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301668/* converts pcm format 24_8 to 8_24 inplace */
1669size_t audio_extn_utils_convert_format_24_8_to_8_24(void *buf, size_t bytes)
1670{
1671 size_t i = 0;
1672 int *int_buf_stream = buf;
1673
1674 if ((bytes % 4) != 0) {
1675 ALOGE("%s: wrong inout buffer! ... is not 32 bit aligned ", __func__);
1676 return -EINVAL;
1677 }
1678
1679 for (; i < (bytes / 4); i++)
1680 int_buf_stream[i] >>= 8;
1681
1682 return bytes;
1683}
1684
Xiaojun Sang782e5b12020-06-29 21:13:06 +08001685#ifdef AUDIO_GKI_ENABLED
1686int get_snd_codec_id(audio_format_t format)
1687{
1688 int id = 0;
1689
1690 switch (format & AUDIO_FORMAT_MAIN_MASK) {
1691 case AUDIO_FORMAT_MP3:
1692 id = SND_AUDIOCODEC_MP3;
1693 break;
1694 case AUDIO_FORMAT_AAC:
1695 id = SND_AUDIOCODEC_AAC;
1696 break;
1697 case AUDIO_FORMAT_AAC_ADTS:
1698 id = SND_AUDIOCODEC_AAC;
1699 break;
1700 case AUDIO_FORMAT_AAC_LATM:
1701 id = SND_AUDIOCODEC_AAC;
1702 break;
1703 case AUDIO_FORMAT_PCM:
1704 id = SND_AUDIOCODEC_PCM;
1705 break;
1706 case AUDIO_FORMAT_FLAC:
1707 case AUDIO_FORMAT_ALAC:
1708 case AUDIO_FORMAT_APE:
1709 case AUDIO_FORMAT_VORBIS:
1710 case AUDIO_FORMAT_WMA:
1711 case AUDIO_FORMAT_WMA_PRO:
1712 case AUDIO_FORMAT_DSD:
1713 case AUDIO_FORMAT_APTX:
1714 id = SND_AUDIOCODEC_BESPOKE;
1715 break;
1716 case AUDIO_FORMAT_MP2:
1717 id = SND_AUDIOCODEC_MP2;
1718 break;
1719 case AUDIO_FORMAT_AC3:
1720 id = SND_AUDIOCODEC_AC3;
1721 break;
1722 case AUDIO_FORMAT_E_AC3:
1723 case AUDIO_FORMAT_E_AC3_JOC:
1724 id = SND_AUDIOCODEC_EAC3;
1725 break;
1726 case AUDIO_FORMAT_DTS:
1727 case AUDIO_FORMAT_DTS_HD:
1728 id = SND_AUDIOCODEC_DTS;
1729 break;
1730 case AUDIO_FORMAT_DOLBY_TRUEHD:
1731 id = SND_AUDIOCODEC_TRUEHD;
1732 break;
1733 case AUDIO_FORMAT_IEC61937:
1734 id = SND_AUDIOCODEC_IEC61937;
1735 break;
1736 default:
1737 ALOGE("%s: Unsupported audio format :%x", __func__, format);
1738 }
1739
1740 return id;
1741}
1742#else
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301743int get_snd_codec_id(audio_format_t format)
1744{
1745 int id = 0;
1746
1747 switch (format & AUDIO_FORMAT_MAIN_MASK) {
1748 case AUDIO_FORMAT_MP3:
1749 id = SND_AUDIOCODEC_MP3;
1750 break;
1751 case AUDIO_FORMAT_AAC:
1752 id = SND_AUDIOCODEC_AAC;
1753 break;
1754 case AUDIO_FORMAT_AAC_ADTS:
1755 id = SND_AUDIOCODEC_AAC;
1756 break;
Arun Kumar Dasari3b174182016-12-27 13:01:14 +05301757 case AUDIO_FORMAT_AAC_LATM:
1758 id = SND_AUDIOCODEC_AAC;
1759 break;
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301760 case AUDIO_FORMAT_PCM:
1761 id = SND_AUDIOCODEC_PCM;
1762 break;
1763 case AUDIO_FORMAT_FLAC:
1764 id = SND_AUDIOCODEC_FLAC;
1765 break;
1766 case AUDIO_FORMAT_ALAC:
1767 id = SND_AUDIOCODEC_ALAC;
1768 break;
1769 case AUDIO_FORMAT_APE:
1770 id = SND_AUDIOCODEC_APE;
1771 break;
1772 case AUDIO_FORMAT_VORBIS:
1773 id = SND_AUDIOCODEC_VORBIS;
1774 break;
1775 case AUDIO_FORMAT_WMA:
1776 id = SND_AUDIOCODEC_WMA;
1777 break;
1778 case AUDIO_FORMAT_WMA_PRO:
1779 id = SND_AUDIOCODEC_WMA_PRO;
1780 break;
Satish Babu Patakokila0c313922016-12-08 12:07:08 +05301781 case AUDIO_FORMAT_MP2:
1782 id = SND_AUDIOCODEC_MP2;
1783 break;
Satish Babu Patakokila915ecba2017-01-10 17:43:56 +05301784 case AUDIO_FORMAT_AC3:
1785 id = SND_AUDIOCODEC_AC3;
1786 break;
1787 case AUDIO_FORMAT_E_AC3:
1788 case AUDIO_FORMAT_E_AC3_JOC:
1789 id = SND_AUDIOCODEC_EAC3;
1790 break;
1791 case AUDIO_FORMAT_DTS:
1792 case AUDIO_FORMAT_DTS_HD:
1793 id = SND_AUDIOCODEC_DTS;
1794 break;
Ben Romberger1aaaf862017-04-06 17:49:46 -07001795 case AUDIO_FORMAT_DOLBY_TRUEHD:
1796 id = SND_AUDIOCODEC_TRUEHD;
1797 break;
Naresh Tanniru928f0862017-04-07 16:44:23 -07001798 case AUDIO_FORMAT_IEC61937:
1799 id = SND_AUDIOCODEC_IEC61937;
1800 break;
Preetam Singh Ranawat4277a5a2017-01-18 19:02:24 +05301801 case AUDIO_FORMAT_DSD:
1802 id = SND_AUDIOCODEC_DSD;
1803 break;
Dhanalakshmi Siddani18737932016-11-29 17:33:17 +05301804 case AUDIO_FORMAT_APTX:
1805 id = SND_AUDIOCODEC_APTX;
1806 break;
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301807 default:
1808 ALOGE("%s: Unsupported audio format :%x", __func__, format);
1809 }
1810
1811 return id;
1812}
Xiaojun Sang782e5b12020-06-29 21:13:06 +08001813#endif
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301814
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001815void audio_extn_utils_send_audio_calibration(struct audio_device *adev,
1816 struct audio_usecase *usecase)
1817{
1818 int type = usecase->type;
1819
Dhananjay Kumar14245982017-01-16 20:21:00 +05301820 if (type == PCM_PLAYBACK && usecase->stream.out != NULL) {
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05301821 platform_send_audio_calibration(adev->platform, usecase,
Zhou Song06761dd2019-04-28 18:08:17 +08001822 usecase->stream.out->app_type_cfg.app_type);
Dhananjay Kumar14245982017-01-16 20:21:00 +05301823 } else if (type == PCM_CAPTURE && usecase->stream.in != NULL) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301824 platform_send_audio_calibration(adev->platform, usecase,
Zhou Song06761dd2019-04-28 18:08:17 +08001825 usecase->stream.in->app_type_cfg.app_type);
1826 } else if ((type == PCM_HFP_CALL) || (type == PCM_CAPTURE) ||
1827 (type == TRANSCODE_LOOPBACK_RX && usecase->stream.inout != NULL)) {
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05301828 platform_send_audio_calibration(adev->platform, usecase,
Zhou Song06761dd2019-04-28 18:08:17 +08001829 platform_get_default_app_type_v2(adev->platform, usecase->type));
Vidyakumar Athotae57f6002017-03-01 13:13:16 -08001830 } else {
1831 /* No need to send audio calibration for voice and voip call usecases */
1832 if ((type != VOICE_CALL) && (type != VOIP_CALL))
1833 ALOGW("%s: No audio calibration for usecase type = %d", __func__, type);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001834 }
1835}
1836
Ben Rombergera04fabc2014-11-14 12:16:03 -08001837// Base64 Encode and Decode
1838// Not all features supported. This must be used only with following conditions.
1839// Decode Modes: Support with and without padding
1840// CRLF not handling. So no CRLF in string to decode.
1841// Encode Modes: Supports only padding
1842int b64decode(char *inp, int ilen, uint8_t* outp)
1843{
1844 int i, j, k, ii, num;
1845 int rem, pcnt;
1846 uint32_t res=0;
1847 uint8_t getIndex[MAX_BASEINDEX_LEN];
1848 uint8_t tmp, cflag;
1849
1850 if(inp == NULL || outp == NULL || ilen <= 0) {
1851 ALOGE("[%s] received NULL pointer or zero length",__func__);
1852 return -1;
1853 }
1854
1855 memset(getIndex, MAX_BASEINDEX_LEN-1, sizeof(getIndex));
1856 for(i=0;i<BASE_TABLE_SIZE;i++) {
1857 getIndex[(uint8_t)bTable[i]] = (uint8_t)i;
1858 }
1859 getIndex[(uint8_t)'=']=0;
1860
1861 j=0;k=0;
1862 num = ilen/4;
1863 rem = ilen%4;
1864 if(rem==0)
1865 num = num-1;
1866 cflag=0;
1867 for(i=0; i<num; i++) {
1868 res=0;
1869 for(ii=0;ii<4;ii++) {
1870 res = res << 6;
1871 tmp = getIndex[(uint8_t)inp[j++]];
1872 res = res | tmp;
1873 cflag = cflag | tmp;
1874 }
1875 outp[k++] = (res >> 16)&0xFF;
1876 outp[k++] = (res >> 8)&0xFF;
1877 outp[k++] = res & 0xFF;
1878 }
1879
1880 // Handle last bytes special
1881 pcnt=0;
1882 if(rem == 0) {
1883 //With padding or full data
1884 res = 0;
1885 for(ii=0;ii<4;ii++) {
1886 if(inp[j] == '=')
1887 pcnt++;
1888 res = res << 6;
1889 tmp = getIndex[(uint8_t)inp[j++]];
1890 res = res | tmp;
1891 cflag = cflag | tmp;
1892 }
1893 outp[k++] = res >> 16;
1894 if(pcnt == 2)
1895 goto done;
1896 outp[k++] = (res>>8)&0xFF;
1897 if(pcnt == 1)
1898 goto done;
1899 outp[k++] = res&0xFF;
1900 } else {
1901 //without padding
1902 res = 0;
1903 for(i=0;i<rem;i++) {
1904 res = res << 6;
1905 tmp = getIndex[(uint8_t)inp[j++]];
1906 res = res | tmp;
1907 cflag = cflag | tmp;
1908 }
1909 for(i=rem;i<4;i++) {
1910 res = res << 6;
1911 pcnt++;
1912 }
1913 outp[k++] = res >> 16;
1914 if(pcnt == 2)
1915 goto done;
1916 outp[k++] = (res>>8)&0xFF;
1917 if(pcnt == 1)
1918 goto done;
1919 outp[k++] = res&0xFF;
1920 }
1921done:
1922 if(cflag == 0xFF) {
1923 ALOGE("[%s] base64 decode failed. Invalid character found %s",
1924 __func__, inp);
1925 return 0;
1926 }
1927 return k;
1928}
1929
1930int b64encode(uint8_t *inp, int ilen, char* outp)
1931{
1932 int i,j,k, num;
1933 int rem=0;
1934 uint32_t res=0;
1935
1936 if(inp == NULL || outp == NULL || ilen<=0) {
1937 ALOGE("[%s] received NULL pointer or zero input length",__func__);
1938 return -1;
1939 }
1940
1941 num = ilen/3;
1942 rem = ilen%3;
1943 j=0;k=0;
1944 for(i=0; i<num; i++) {
1945 //prepare index
1946 res = inp[j++]<<16;
1947 res = res | inp[j++]<<8;
1948 res = res | inp[j++];
1949 //get output map from index
1950 outp[k++] = (char) bTable[(res>>18)&0x3F];
1951 outp[k++] = (char) bTable[(res>>12)&0x3F];
1952 outp[k++] = (char) bTable[(res>>6)&0x3F];
1953 outp[k++] = (char) bTable[res&0x3F];
1954 }
1955
1956 switch(rem) {
1957 case 1:
1958 res = inp[j++]<<16;
1959 outp[k++] = (char) bTable[res>>18];
1960 outp[k++] = (char) bTable[(res>>12)&0x3F];
1961 //outp[k++] = '=';
1962 //outp[k++] = '=';
1963 break;
1964 case 2:
1965 res = inp[j++]<<16;
1966 res = res | inp[j++]<<8;
1967 outp[k++] = (char) bTable[res>>18];
1968 outp[k++] = (char) bTable[(res>>12)&0x3F];
1969 outp[k++] = (char) bTable[(res>>6)&0x3F];
1970 //outp[k++] = '=';
1971 break;
1972 default:
1973 break;
1974 }
Ben Rombergera04fabc2014-11-14 12:16:03 -08001975 outp[k] = '\0';
1976 return k;
1977}
Ashish Jain81eb2a82015-05-13 10:52:34 +05301978
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301979
1980int audio_extn_utils_get_codec_version(const char *snd_card_name,
1981 int card_num,
1982 char *codec_version)
1983{
1984 char procfs_path[50];
1985 FILE *fp;
1986
1987 if (strstr(snd_card_name, "tasha")) {
1988 snprintf(procfs_path, sizeof(procfs_path),
1989 "/proc/asound/card%d/codecs/tasha/version", card_num);
1990 if ((fp = fopen(procfs_path, "r")) != NULL) {
1991 fgets(codec_version, CODEC_VERSION_MAX_LENGTH, fp);
1992 fclose(fp);
1993 } else {
1994 ALOGE("%s: ERROR. cannot open %s", __func__, procfs_path);
1995 return -ENOENT;
1996 }
1997 ALOGD("%s: codec version %s", __func__, codec_version);
1998 }
1999
2000 return 0;
2001}
2002
Preetam Singh Ranawat9d0d8e42019-07-15 12:27:25 +05302003int audio_extn_utils_get_codec_variant(int card_num,
2004 char *codec_variant)
2005{
2006 char procfs_path[50];
2007 FILE *fp;
2008 snprintf(procfs_path, sizeof(procfs_path),
2009 "/proc/asound/card%d/codecs/wcd938x/variant", card_num);
2010 if ((fp = fopen(procfs_path, "r")) == NULL) {
2011 snprintf(procfs_path, sizeof(procfs_path),
2012 "/proc/asound/card%d/codecs/wcd937x/variant", card_num);
2013 if ((fp = fopen(procfs_path, "r")) == NULL) {
2014 ALOGE("%s: ERROR. cannot open %s", __func__, procfs_path);
2015 return -ENOENT;
2016 }
2017 }
2018 fgets(codec_variant, CODEC_VARIANT_MAX_LENGTH, fp);
2019 fclose(fp);
2020 ALOGD("%s: codec variant is %s", __func__, codec_variant);
2021 return 0;
2022}
2023
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05302024
Ashish Jain81eb2a82015-05-13 10:52:34 +05302025#ifdef AUDIO_EXTERNAL_HDMI_ENABLED
2026
2027void get_default_compressed_channel_status(
2028 unsigned char *channel_status)
2029{
Ashish Jain81eb2a82015-05-13 10:52:34 +05302030 memset(channel_status,0,24);
2031
2032 /* block start bit in preamble bit 3 */
2033 channel_status[0] |= PROFESSIONAL;
2034 //compre out
2035 channel_status[0] |= NON_LPCM;
2036 // sample rate; fixed 48K for default/transcode
2037 channel_status[3] |= SR_48000;
2038}
2039
Ashish Jain81eb2a82015-05-13 10:52:34 +05302040int32_t get_compressed_channel_status(void *audio_stream_data,
2041 uint32_t audio_frame_size,
2042 unsigned char *channel_status,
2043 enum audio_parser_code_type codec_type)
2044 // codec_type - AUDIO_PARSER_CODEC_AC3
2045 // - AUDIO_PARSER_CODEC_DTS
2046{
2047 unsigned char *stream;
2048 int ret = 0;
2049 stream = (unsigned char *)audio_stream_data;
2050
2051 if (audio_stream_data == NULL || audio_frame_size == 0) {
2052 ALOGW("no buffer to get channel status, return default for compress");
2053 get_default_compressed_channel_status(channel_status);
2054 return ret;
2055 }
2056
2057 memset(channel_status,0,24);
2058 if(init_audio_parser(stream, audio_frame_size, codec_type) == -1)
2059 {
2060 ALOGE("init audio parser failed");
2061 return -1;
2062 }
2063 ret = get_channel_status(channel_status, codec_type);
2064 return ret;
2065
2066}
2067
Ashish Jain81eb2a82015-05-13 10:52:34 +05302068void get_lpcm_channel_status(uint32_t sampleRate,
2069 unsigned char *channel_status)
2070{
2071 int32_t status = 0;
Ashish Jain81eb2a82015-05-13 10:52:34 +05302072 memset(channel_status,0,24);
2073 /* block start bit in preamble bit 3 */
2074 channel_status[0] |= PROFESSIONAL;
2075 //LPCM OUT
2076 channel_status[0] &= ~NON_LPCM;
2077
2078 switch (sampleRate) {
2079 case 8000:
2080 case 11025:
2081 case 12000:
2082 case 16000:
2083 case 22050:
2084 channel_status[3] |= SR_NOTID;
Preetam Singh Ranawat61716b12015-12-14 11:55:24 +05302085 break;
Ashish Jain81eb2a82015-05-13 10:52:34 +05302086 case 24000:
2087 channel_status[3] |= SR_24000;
2088 break;
2089 case 32000:
2090 channel_status[3] |= SR_32000;
2091 break;
2092 case 44100:
2093 channel_status[3] |= SR_44100;
2094 break;
2095 case 48000:
2096 channel_status[3] |= SR_48000;
2097 break;
2098 case 88200:
2099 channel_status[3] |= SR_88200;
2100 break;
2101 case 96000:
2102 channel_status[3] |= SR_96000;
2103 break;
2104 case 176400:
2105 channel_status[3] |= SR_176400;
2106 break;
2107 case 192000:
2108 channel_status[3] |= SR_192000;
2109 break;
2110 default:
2111 ALOGV("Invalid sample_rate %u\n", sampleRate);
2112 status = -1;
2113 break;
2114 }
2115}
2116
2117void audio_utils_set_hdmi_channel_status(struct stream_out *out, char * buffer, size_t bytes)
2118{
2119 unsigned char channel_status[24]={0};
2120 struct snd_aes_iec958 iec958;
2121 const char *mixer_ctl_name = "IEC958 Playback PCM Stream";
2122 struct mixer_ctl *ctl;
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +05302123 ALOGV("%s: buffer %s bytes %zd", __func__, buffer, bytes);
Arun Mirpurie008ed22019-03-21 11:21:04 -07002124
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05302125 if (audio_extn_utils_is_dolby_format(out->format) &&
Ashish Jain81eb2a82015-05-13 10:52:34 +05302126 /*TODO:Extend code to support DTS passthrough*/
2127 /*set compressed channel status bits*/
Arun Mirpurie008ed22019-03-21 11:21:04 -07002128 audio_extn_passthru_is_passthrough_stream(out) &&
2129 audio_extn_is_hdmi_passthru_enabled()) {
Ashish Jain81eb2a82015-05-13 10:52:34 +05302130 get_compressed_channel_status(buffer, bytes, channel_status, AUDIO_PARSER_CODEC_AC3);
Arun Mirpurie008ed22019-03-21 11:21:04 -07002131 } else {
Ashish Jain81eb2a82015-05-13 10:52:34 +05302132 /*set channel status bit for LPCM*/
2133 get_lpcm_channel_status(out->sample_rate, channel_status);
2134 }
2135
2136 memcpy(iec958.status, channel_status,sizeof(iec958.status));
2137 ctl = mixer_get_ctl_by_name(out->dev->mixer, mixer_ctl_name);
2138 if (!ctl) {
2139 ALOGE("%s: Could not get ctl for mixer cmd - %s",
2140 __func__, mixer_ctl_name);
2141 return;
2142 }
2143 if (mixer_ctl_set_array(ctl, &iec958, sizeof(iec958)) < 0) {
2144 ALOGE("%s: Could not set channel status for ext HDMI ",
2145 __func__);
2146 return;
2147 }
2148
2149}
2150#endif
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302151
2152int audio_extn_utils_get_avt_device_drift(
2153 struct audio_usecase *usecase,
2154 struct audio_avt_device_drift_param *drift_param)
2155{
2156 int ret = 0, count = 0;
2157 char avt_device_drift_mixer_ctl_name[MIXER_PATH_MAX_LENGTH] = {0};
Naresh Tanniru6160c712017-04-17 15:43:48 +05302158 const char *backend = NULL;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302159 struct mixer_ctl *ctl = NULL;
2160 struct audio_avt_device_drift_stats drift_stats;
2161 struct audio_device *adev = NULL;
2162
2163 if (usecase != NULL && usecase->type == PCM_PLAYBACK) {
Naresh Tanniru6160c712017-04-17 15:43:48 +05302164 backend = platform_get_snd_device_backend_interface(usecase->out_snd_device);
2165 if (!backend) {
2166 ALOGE("%s: Unsupported device %d", __func__,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08002167 get_device_types(&usecase->stream.out->device_list));
Naresh Tanniru6160c712017-04-17 15:43:48 +05302168 ret = -EINVAL;
2169 goto done;
2170 }
2171 strlcpy(avt_device_drift_mixer_ctl_name,
2172 backend,
2173 MIXER_PATH_MAX_LENGTH);
2174
2175 count = strlen(backend);
2176 if (MIXER_PATH_MAX_LENGTH - count > 0) {
2177 strlcat(&avt_device_drift_mixer_ctl_name[count],
2178 " DRIFT",
2179 MIXER_PATH_MAX_LENGTH - count);
2180 } else {
2181 ret = -EINVAL;
2182 goto done;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302183 }
2184 } else {
Naresh Tanniru7586e292017-04-13 10:38:13 +05302185 ALOGE("%s: Invalid usecase",__func__);
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302186 ret = -EINVAL;
Naresh Tanniru6160c712017-04-17 15:43:48 +05302187 goto done;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302188 }
2189
Naresh Tanniru6160c712017-04-17 15:43:48 +05302190 adev = usecase->stream.out->dev;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302191 ctl = mixer_get_ctl_by_name(adev->mixer, avt_device_drift_mixer_ctl_name);
2192 if (!ctl) {
2193 ALOGE("%s: Could not get ctl for mixer cmd - %s",
2194 __func__, avt_device_drift_mixer_ctl_name);
2195
2196 ret = -EINVAL;
2197 goto done;
2198 }
2199
2200 ALOGV("%s: Getting AV Timer vs Device Drift mixer ctrl name %s", __func__,
2201 avt_device_drift_mixer_ctl_name);
2202
2203 mixer_ctl_update(ctl);
2204 count = mixer_ctl_get_num_values(ctl);
2205 if (count != sizeof(struct audio_avt_device_drift_stats)) {
2206 ALOGE("%s: mixer_ctl_get_num_values() invalid drift_stats data size",
2207 __func__);
2208
2209 ret = -EINVAL;
2210 goto done;
2211 }
2212
2213 ret = mixer_ctl_get_array(ctl, (void *)&drift_stats, count);
2214 if (ret != 0) {
2215 ALOGE("%s: mixer_ctl_get_array() failed to get drift_stats Params",
2216 __func__);
2217
2218 ret = -EINVAL;
2219 goto done;
2220 }
2221 memcpy(drift_param, &drift_stats.drift_param,
2222 sizeof(struct audio_avt_device_drift_param));
2223done:
2224 return ret;
2225}
Manish Dewangan07de2142017-02-27 19:27:20 +05302226
2227#ifdef SNDRV_COMPRESS_PATH_DELAY
2228int audio_extn_utils_compress_get_dsp_latency(struct stream_out *out)
2229{
2230 int ret = -EINVAL;
2231 struct snd_compr_metadata metadata;
2232 int delay_ms = COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
2233
Weiyin Jiangc49a3b52018-08-17 16:16:08 +08002234 /* override the latency for pcm offload use case */
2235 if ((out->flags & AUDIO_OUTPUT_FLAG_DIRECT) &&
2236 !(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)) {
2237 delay_ms = PCM_OFFLOAD_PLAYBACK_LATENCY;
2238 }
2239
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -07002240 if (property_get_bool("vendor.audio.playback.dsp.pathdelay", false)) {
Manish Dewangan07de2142017-02-27 19:27:20 +05302241 ALOGD("%s:: Quering DSP delay %d",__func__, __LINE__);
2242 if (!(is_offload_usecase(out->usecase))) {
2243 ALOGE("%s:: not supported for non offload session", __func__);
2244 goto exit;
2245 }
2246
2247 if (!out->compr) {
2248 ALOGD("%s:: Invalid compress handle,returning default dsp latency",
2249 __func__);
2250 goto exit;
2251 }
2252
2253 metadata.key = SNDRV_COMPRESS_PATH_DELAY;
2254 ret = compress_get_metadata(out->compr, &metadata);
2255 if(ret) {
2256 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2257 goto exit;
2258 }
2259 delay_ms = metadata.value[0] / 1000; /*convert to ms*/
2260 } else {
2261 ALOGD("%s:: Using Fix DSP delay",__func__);
2262 }
2263
2264exit:
2265 ALOGD("%s:: delay in ms is %d",__func__, delay_ms);
2266 return delay_ms;
2267}
2268#else
2269int audio_extn_utils_compress_get_dsp_latency(struct stream_out *out __unused)
2270{
2271 return COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
2272}
2273#endif
Manish Dewangan69426c82017-01-30 17:35:36 +05302274
2275#ifdef SNDRV_COMPRESS_RENDER_MODE
2276int audio_extn_utils_compress_set_render_mode(struct stream_out *out)
2277{
2278 struct snd_compr_metadata metadata;
2279 int ret = -EINVAL;
2280
2281 if (!(is_offload_usecase(out->usecase))) {
2282 ALOGE("%s:: not supported for non offload session", __func__);
2283 goto exit;
2284 }
2285
2286 if (!out->compr) {
2287 ALOGD("%s:: Invalid compress handle",
2288 __func__);
2289 goto exit;
2290 }
2291
2292 ALOGD("%s:: render mode %d", __func__, out->render_mode);
2293
2294 metadata.key = SNDRV_COMPRESS_RENDER_MODE;
2295 if (out->render_mode == RENDER_MODE_AUDIO_MASTER) {
2296 metadata.value[0] = SNDRV_COMPRESS_RENDER_MODE_AUDIO_MASTER;
2297 } else if (out->render_mode == RENDER_MODE_AUDIO_STC_MASTER) {
2298 metadata.value[0] = SNDRV_COMPRESS_RENDER_MODE_STC_MASTER;
2299 } else {
2300 ret = 0;
2301 goto exit;
2302 }
2303 ret = compress_set_metadata(out->compr, &metadata);
2304 if(ret) {
2305 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2306 }
2307exit:
2308 return ret;
2309}
2310#else
2311int audio_extn_utils_compress_set_render_mode(struct stream_out *out __unused)
2312{
2313 ALOGD("%s:: configuring render mode not supported", __func__);
2314 return 0;
2315}
2316#endif
Manish Dewangan58229382017-02-02 15:48:41 +05302317
2318#ifdef SNDRV_COMPRESS_CLK_REC_MODE
2319int audio_extn_utils_compress_set_clk_rec_mode(
2320 struct audio_usecase *usecase)
2321{
2322 struct snd_compr_metadata metadata;
2323 struct stream_out *out = NULL;
2324 int ret = -EINVAL;
2325
Naresh Tanniru7586e292017-04-13 10:38:13 +05302326 if (usecase == NULL || usecase->type != PCM_PLAYBACK) {
Manish Dewangan58229382017-02-02 15:48:41 +05302327 ALOGE("%s:: Invalid use case", __func__);
2328 goto exit;
2329 }
2330
2331 out = usecase->stream.out;
2332 if (!out) {
2333 ALOGE("%s:: invalid stream", __func__);
2334 goto exit;
2335 }
2336
2337 if (!is_offload_usecase(out->usecase)) {
2338 ALOGE("%s:: not supported for non offload session", __func__);
2339 goto exit;
2340 }
2341
2342 if (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER) {
2343 ALOGD("%s:: clk recovery is only supported in STC render mode",
2344 __func__);
2345 ret = 0;
2346 goto exit;
2347 }
2348
2349 if (!out->compr) {
2350 ALOGD("%s:: Invalid compress handle",
2351 __func__);
2352 goto exit;
2353 }
2354 metadata.key = SNDRV_COMPRESS_CLK_REC_MODE;
2355 switch(usecase->out_snd_device) {
2356 case SND_DEVICE_OUT_HDMI:
2357 case SND_DEVICE_OUT_SPEAKER_AND_HDMI:
2358 case SND_DEVICE_OUT_DISPLAY_PORT:
2359 case SND_DEVICE_OUT_SPEAKER_AND_DISPLAY_PORT:
2360 metadata.value[0] = SNDRV_COMPRESS_CLK_REC_MODE_NONE;
2361 break;
2362 default:
2363 metadata.value[0] = SNDRV_COMPRESS_CLK_REC_MODE_AUTO;
2364 break;
2365 }
2366
2367 ALOGD("%s:: clk recovery mode %d",__func__, metadata.value[0]);
2368
2369 ret = compress_set_metadata(out->compr, &metadata);
2370 if(ret) {
2371 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2372 }
2373
2374exit:
2375 return ret;
2376}
2377#else
2378int audio_extn_utils_compress_set_clk_rec_mode(
2379 struct audio_usecase *usecase __unused)
2380{
2381 ALOGD("%s:: configuring render mode not supported", __func__);
2382 return 0;
2383}
2384#endif
Manish Dewangan27346042017-03-01 12:56:12 +05302385
2386#ifdef SNDRV_COMPRESS_RENDER_WINDOW
2387int audio_extn_utils_compress_set_render_window(
2388 struct stream_out *out,
2389 struct audio_out_render_window_param *render_window)
2390{
2391 struct snd_compr_metadata metadata;
2392 int ret = -EINVAL;
2393
Manish Dewangan27346042017-03-01 12:56:12 +05302394 if(render_window == NULL) {
2395 ALOGE("%s:: Invalid render_window", __func__);
2396 goto exit;
2397 }
2398
Aniket Kumar Lata1e1d3662017-06-01 18:45:48 -07002399 ALOGD("%s:: render window start 0x%"PRIx64" end 0x%"PRIx64"",
2400 __func__,render_window->render_ws, render_window->render_we);
2401
Manish Dewangan27346042017-03-01 12:56:12 +05302402 if (!is_offload_usecase(out->usecase)) {
2403 ALOGE("%s:: not supported for non offload session", __func__);
2404 goto exit;
2405 }
2406
Naresh Tanniru6160c712017-04-17 15:43:48 +05302407 if ((out->render_mode != RENDER_MODE_AUDIO_MASTER) &&
2408 (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER)) {
Manish Dewangan27346042017-03-01 12:56:12 +05302409 ALOGD("%s:: only supported in timestamp mode, current "
2410 "render mode mode %d", __func__, out->render_mode);
2411 goto exit;
2412 }
2413
2414 if (!out->compr) {
2415 ALOGW("%s:: offload session not yet opened,"
2416 "render window will be configure later", __func__);
2417 /* store render window to reconfigure in start_output_stream() */
2418 goto exit;
2419 }
2420
2421 metadata.key = SNDRV_COMPRESS_RENDER_WINDOW;
2422 /*render window start value */
2423 metadata.value[0] = 0xFFFFFFFF & render_window->render_ws; /* lsb */
2424 metadata.value[1] = \
2425 (0xFFFFFFFF00000000 & render_window->render_ws) >> 32; /* msb*/
2426 /*render window end value */
2427 metadata.value[2] = 0xFFFFFFFF & render_window->render_we; /* lsb */
2428 metadata.value[3] = \
2429 (0xFFFFFFFF00000000 & render_window->render_we) >> 32; /* msb*/
2430
2431 ret = compress_set_metadata(out->compr, &metadata);
2432 if(ret) {
2433 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2434 }
2435
2436exit:
2437 return ret;
2438}
2439#else
2440int audio_extn_utils_compress_set_render_window(
2441 struct stream_out *out __unused,
2442 struct audio_out_render_window_param *render_window __unused)
2443{
2444 ALOGD("%s:: configuring render window not supported", __func__);
2445 return 0;
2446}
2447#endif
Manish Dewangan14956cc2017-02-14 18:54:42 +05302448
2449#ifdef SNDRV_COMPRESS_START_DELAY
2450int audio_extn_utils_compress_set_start_delay(
2451 struct stream_out *out,
2452 struct audio_out_start_delay_param *delay_param)
2453{
2454 struct snd_compr_metadata metadata;
2455 int ret = -EINVAL;
2456
2457 if(delay_param == NULL) {
2458 ALOGE("%s:: Invalid delay_param", __func__);
2459 goto exit;
2460 }
2461
2462 ALOGD("%s:: render start delay 0x%"PRIx64" ", __func__,
2463 delay_param->start_delay);
2464
2465 if (!is_offload_usecase(out->usecase)) {
2466 ALOGE("%s:: not supported for non offload session", __func__);
2467 goto exit;
2468 }
2469
Naresh Tanniru6160c712017-04-17 15:43:48 +05302470 if ((out->render_mode != RENDER_MODE_AUDIO_MASTER) &&
2471 (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER)) {
Manish Dewangan14956cc2017-02-14 18:54:42 +05302472 ALOGD("%s:: only supported in timestamp mode, current "
2473 "render mode mode %d", __func__, out->render_mode);
2474 goto exit;
2475 }
2476
2477 if (!out->compr) {
2478 ALOGW("%s:: offload session not yet opened,"
2479 "start delay will be configure later", __func__);
2480 goto exit;
2481 }
2482
2483 metadata.key = SNDRV_COMPRESS_START_DELAY;
2484 metadata.value[0] = 0xFFFFFFFF & delay_param->start_delay; /* lsb */
2485 metadata.value[1] = \
2486 (0xFFFFFFFF00000000 & delay_param->start_delay) >> 32; /* msb*/
2487
2488 ret = compress_set_metadata(out->compr, &metadata);
2489 if(ret) {
2490 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2491 }
2492
2493exit:
2494 return ret;
2495}
2496#else
2497int audio_extn_utils_compress_set_start_delay(
2498 struct stream_out *out __unused,
2499 struct audio_out_start_delay_param *delay_param __unused)
2500{
2501 ALOGD("%s:: configuring render window not supported", __func__);
2502 return 0;
2503}
2504#endif
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002505
Surendar Karka287348c2019-04-10 18:31:46 +05302506#ifdef SNDRV_COMPRESS_DSP_POSITION
2507int audio_extn_utils_compress_get_dsp_presentation_pos(struct stream_out *out,
2508 uint64_t *frames, struct timespec *timestamp, int32_t clock_id)
2509{
2510 int ret = -EINVAL;
2511 uint64_t *val = NULL;
2512 uint64_t time = 0;
2513 struct snd_compr_metadata metadata;
2514
2515 ALOGV("%s:: Quering DSP position with clock id %d",__func__, clock_id);
2516 metadata.key = SNDRV_COMPRESS_DSP_POSITION;
2517 metadata.value[0] = clock_id;
2518 ret = compress_get_metadata(out->compr, &metadata);
2519 if (ret) {
2520 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2521 ret = -errno;
2522 goto exit;
2523 }
2524 val = (uint64_t *)&metadata.value[1];
2525 *frames = *val;
2526 time = *(val + 1);
2527 timestamp->tv_sec = time / 1000000;
2528 timestamp->tv_nsec = (time % 1000000)*1000;
2529
2530exit:
2531 return ret;
2532}
2533#else
2534int audio_extn_utils_compress_get_dsp_presentation_pos(struct stream_out *out __unused,
2535 uint64_t *frames __unused, struct timespec *timestamp __unused,
2536 int32_t clock_id __unused)
2537{
2538 ALOGD("%s:: dsp presentation position not supported", __func__);
2539 return 0;
2540
2541}
2542#endif
2543
2544#ifdef SNDRV_PCM_IOCTL_DSP_POSITION
2545int audio_extn_utils_pcm_get_dsp_presentation_pos(struct stream_out *out,
2546 uint64_t *frames, struct timespec *timestamp, int32_t clock_id)
2547{
2548 int ret = -EINVAL;
2549 uint64_t time = 0;
2550 struct snd_pcm_prsnt_position prsnt_position;
2551
2552 ALOGV("%s:: Quering DSP position with clock id %d",__func__, clock_id);
2553 prsnt_position.clock_id = clock_id;
2554 ret = pcm_ioctl(out->pcm, SNDRV_PCM_IOCTL_DSP_POSITION, &prsnt_position);
2555 if (ret) {
2556 ALOGE("%s::error %d", __func__, ret);
2557 ret = -EIO;
2558 goto exit;
2559 }
2560
2561 *frames = prsnt_position.frames;
2562 time = prsnt_position.timestamp;
2563 timestamp->tv_sec = time / 1000000;
2564 timestamp->tv_nsec = (time % 1000000)*1000;
2565
2566exit:
2567 return ret;
2568}
2569#else
2570int audio_extn_utils_pcm_get_dsp_presentation_pos(struct stream_out *out __unused,
2571 uint64_t *frames __unused, struct timespec *timestamp __unused,
2572 int32_t clock_id __unused)
2573{
2574 ALOGD("%s:: dsp presentation position not supported", __func__);
2575 return 0;
2576
2577}
2578#endif
2579
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002580#define MAX_SND_CARD 8
Ashish Jain30ae8942017-11-05 17:21:23 +05302581#define RETRY_US 1000000
2582#define RETRY_NUMBER 40
Aalique Grahame22e49102018-12-18 14:23:57 -08002583#define PLATFORM_INFO_XML_PATH "audio_platform_info.xml"
2584#define PLATFORM_INFO_XML_BASE_STRING "audio_platform_info"
2585
2586#ifdef LINUX_ENABLED
2587static const char *kConfigLocationList[] =
2588 {"/etc"};
2589#else
2590static const char *kConfigLocationList[] =
2591 {"/vendor/etc"};
2592#endif
2593static const int kConfigLocationListSize =
2594 (sizeof(kConfigLocationList) / sizeof(kConfigLocationList[0]));
2595
2596bool audio_extn_utils_resolve_config_file(char file_name[MIXER_PATH_MAX_LENGTH])
2597{
2598 char full_config_path[MIXER_PATH_MAX_LENGTH];
2599 for (int i = 0; i < kConfigLocationListSize; i++) {
2600 snprintf(full_config_path,
2601 MIXER_PATH_MAX_LENGTH,
2602 "%s/%s",
2603 kConfigLocationList[i],
2604 file_name);
2605 if (F_OK == access(full_config_path, 0)) {
Weiyin Jiang2995f662019-04-17 14:25:12 +08002606 strlcpy(file_name, full_config_path, MIXER_PATH_MAX_LENGTH);
Aalique Grahame22e49102018-12-18 14:23:57 -08002607 return true;
2608 }
2609 }
2610 return false;
2611}
2612
2613/* platform_info_file should be size 'MIXER_PATH_MAX_LENGTH' */
2614int audio_extn_utils_get_platform_info(const char* snd_card_name, char* platform_info_file)
2615{
2616 if (NULL == snd_card_name) {
2617 return -1;
2618 }
2619
2620 struct snd_card_split *snd_split_handle = NULL;
2621 int ret = 0;
2622 audio_extn_set_snd_card_split(snd_card_name);
2623 snd_split_handle = audio_extn_get_snd_card_split();
2624
2625 snprintf(platform_info_file, MIXER_PATH_MAX_LENGTH, "%s_%s_%s.xml",
2626 PLATFORM_INFO_XML_BASE_STRING, snd_split_handle->snd_card,
2627 snd_split_handle->form_factor);
2628
2629 if (!audio_extn_utils_resolve_config_file(platform_info_file)) {
2630 memset(platform_info_file, 0, MIXER_PATH_MAX_LENGTH);
2631 snprintf(platform_info_file, MIXER_PATH_MAX_LENGTH, "%s_%s.xml",
2632 PLATFORM_INFO_XML_BASE_STRING, snd_split_handle->snd_card);
2633
2634 if (!audio_extn_utils_resolve_config_file(platform_info_file)) {
2635 memset(platform_info_file, 0, MIXER_PATH_MAX_LENGTH);
2636 strlcpy(platform_info_file, PLATFORM_INFO_XML_PATH, MIXER_PATH_MAX_LENGTH);
2637 ret = audio_extn_utils_resolve_config_file(platform_info_file) ? 0 : -1;
2638 }
2639 }
2640
2641 return ret;
2642}
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002643
2644int audio_extn_utils_get_snd_card_num()
2645{
Soumya Managoli9fee7c62018-04-06 16:21:50 +05302646 int snd_card_num = 0;
2647 struct mixer *mixer = NULL;
2648
2649 snd_card_num = audio_extn_utils_open_snd_mixer(&mixer);
2650 if (mixer)
2651 mixer_close(mixer);
2652 return snd_card_num;
2653}
2654
2655int audio_extn_utils_open_snd_mixer(struct mixer **mixer_handle)
2656{
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002657
2658 void *hw_info = NULL;
2659 struct mixer *mixer = NULL;
2660 int retry_num = 0;
mpangfaa7bae2019-01-15 16:38:13 +08002661 int snd_card_num = 0;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002662 char* snd_card_name = NULL;
mpangfaa7bae2019-01-15 16:38:13 +08002663 int snd_card_detection_info[MAX_SND_CARD] = {0};
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002664
Soumya Managoli9fee7c62018-04-06 16:21:50 +05302665 if (!mixer_handle) {
2666 ALOGE("invalid mixer handle");
2667 return -1;
2668 }
2669 *mixer_handle = NULL;
Ashish Jain30ae8942017-11-05 17:21:23 +05302670 /*
mpangfaa7bae2019-01-15 16:38:13 +08002671 * Try with all the sound cards ( 0 to 7 ) and if none of them were detected
Ashish Jain30ae8942017-11-05 17:21:23 +05302672 * sleep for 1 sec and try detections with sound card 0 again.
2673 * If sound card gets detected, check if it is relevant, if not check with the
2674 * other sound cards. To ensure that the irrelevant sound card is not check again,
2675 * we maintain it in min_snd_card_num.
2676 */
2677 while (retry_num < RETRY_NUMBER) {
mpangfaa7bae2019-01-15 16:38:13 +08002678 snd_card_num = 0;
2679 while (snd_card_num < MAX_SND_CARD) {
2680 if (snd_card_detection_info[snd_card_num] == 0) {
2681 mixer = mixer_open(snd_card_num);
2682 if (!mixer)
2683 snd_card_num++;
2684 else
2685 break;
2686 } else
2687 snd_card_num++;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002688 }
2689
2690 if (!mixer) {
Ashish Jain30ae8942017-11-05 17:21:23 +05302691 usleep(RETRY_US);
Ashish Jain30ae8942017-11-05 17:21:23 +05302692 retry_num++;
mpangfaa7bae2019-01-15 16:38:13 +08002693 ALOGD("%s: retry, retry_num %d", __func__, retry_num);
Ashish Jain30ae8942017-11-05 17:21:23 +05302694 continue;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002695 }
2696
2697 snd_card_name = strdup(mixer_get_name(mixer));
2698 if (!snd_card_name) {
2699 ALOGE("failed to allocate memory for snd_card_name\n");
2700 mixer_close(mixer);
2701 return -1;
2702 }
2703 ALOGD("%s: snd_card_name: %s", __func__, snd_card_name);
mpangfaa7bae2019-01-15 16:38:13 +08002704 snd_card_detection_info[snd_card_num] = 1;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002705 hw_info = hw_info_init(snd_card_name);
2706 if (hw_info) {
2707 ALOGD("%s: Opened sound card:%d", __func__, snd_card_num);
2708 break;
2709 }
mpangfaa7bae2019-01-15 16:38:13 +08002710 ALOGE("%s: Failed to init hardware info, snd_card_num:%d", __func__, snd_card_num);
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002711
Dhananjay Kumara7e27832017-07-11 15:40:39 +05302712 free(snd_card_name);
2713 snd_card_name = NULL;
2714
2715 mixer_close(mixer);
2716 mixer = NULL;
2717 }
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002718 if (snd_card_name)
2719 free(snd_card_name);
Ashish Jain30ae8942017-11-05 17:21:23 +05302720
Dhananjay Kumara7e27832017-07-11 15:40:39 +05302721 if (hw_info)
2722 hw_info_deinit(hw_info);
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002723
mpangfaa7bae2019-01-15 16:38:13 +08002724 if (retry_num >= RETRY_NUMBER) {
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002725 ALOGE("%s: Unable to find correct sound card, aborting.", __func__);
2726 return -1;
2727 }
2728
Soumya Managoli9fee7c62018-04-06 16:21:50 +05302729 if (mixer)
2730 *mixer_handle = mixer;
2731
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002732 return snd_card_num;
2733}
Naresh Tanniru6160c712017-04-17 15:43:48 +05302734
Soumya Managoli9fee7c62018-04-06 16:21:50 +05302735void audio_extn_utils_close_snd_mixer(struct mixer *mixer)
2736{
2737 if (mixer)
2738 mixer_close(mixer);
2739}
2740
Naresh Tanniru6160c712017-04-17 15:43:48 +05302741#ifdef SNDRV_COMPRESS_ENABLE_ADJUST_SESSION_CLOCK
2742int audio_extn_utils_compress_enable_drift_correction(
2743 struct stream_out *out,
2744 struct audio_out_enable_drift_correction *drift)
2745{
2746 struct snd_compr_metadata metadata;
2747 int ret = -EINVAL;
2748
2749 if(drift == NULL) {
2750 ALOGE("%s:: Invalid param", __func__);
2751 goto exit;
2752 }
2753
2754 ALOGD("%s:: drift enable %d", __func__,drift->enable);
2755
2756 if (!is_offload_usecase(out->usecase)) {
2757 ALOGE("%s:: not supported for non offload session", __func__);
2758 goto exit;
2759 }
2760
2761 if (!out->compr) {
2762 ALOGW("%s:: offload session not yet opened,"
2763 "start delay will be configure later", __func__);
2764 goto exit;
2765 }
2766
2767 metadata.key = SNDRV_COMPRESS_ENABLE_ADJUST_SESSION_CLOCK;
2768 metadata.value[0] = drift->enable;
2769 out->drift_correction_enabled = drift->enable;
2770
2771 ret = compress_set_metadata(out->compr, &metadata);
2772 if(ret) {
2773 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2774 out->drift_correction_enabled = false;
2775 }
2776
2777exit:
2778 return ret;
2779}
2780#else
2781int audio_extn_utils_compress_enable_drift_correction(
2782 struct stream_out *out __unused,
2783 struct audio_out_enable_drift_correction *drift __unused)
2784{
2785 ALOGD("%s:: configuring drift enablement not supported", __func__);
2786 return 0;
2787}
2788#endif
2789
2790#ifdef SNDRV_COMPRESS_ADJUST_SESSION_CLOCK
2791int audio_extn_utils_compress_correct_drift(
2792 struct stream_out *out,
2793 struct audio_out_correct_drift *drift_param)
2794{
2795 struct snd_compr_metadata metadata;
2796 int ret = -EINVAL;
2797
2798 if (drift_param == NULL) {
2799 ALOGE("%s:: Invalid drift_param", __func__);
2800 goto exit;
2801 }
2802
2803 ALOGD("%s:: adjust time 0x%"PRIx64" ", __func__,
2804 drift_param->adjust_time);
2805
2806 if (!is_offload_usecase(out->usecase)) {
2807 ALOGE("%s:: not supported for non offload session", __func__);
2808 goto exit;
2809 }
2810
2811 if (!out->compr) {
2812 ALOGW("%s:: offload session not yet opened", __func__);
2813 goto exit;
2814 }
2815
2816 if (!out->drift_correction_enabled) {
2817 ALOGE("%s:: drift correction not enabled", __func__);
2818 goto exit;
2819 }
2820
2821 metadata.key = SNDRV_COMPRESS_ADJUST_SESSION_CLOCK;
2822 metadata.value[0] = 0xFFFFFFFF & drift_param->adjust_time; /* lsb */
2823 metadata.value[1] = \
2824 (0xFFFFFFFF00000000 & drift_param->adjust_time) >> 32; /* msb*/
2825
2826 ret = compress_set_metadata(out->compr, &metadata);
2827 if(ret)
2828 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2829exit:
2830 return ret;
2831}
2832#else
2833int audio_extn_utils_compress_correct_drift(
2834 struct stream_out *out __unused,
2835 struct audio_out_correct_drift *drift_param __unused)
2836{
2837 ALOGD("%s:: setting adjust clock not supported", __func__);
2838 return 0;
2839}
2840#endif
Naresh Tanniru29bce4e2017-04-27 17:54:30 +05302841
2842int audio_extn_utils_set_channel_map(
2843 struct stream_out *out,
2844 struct audio_out_channel_map_param *channel_map_param)
2845{
2846 int ret = -EINVAL, i = 0;
2847 int channels = audio_channel_count_from_out_mask(out->channel_mask);
2848
2849 if (channel_map_param == NULL) {
2850 ALOGE("%s:: Invalid channel_map", __func__);
2851 goto exit;
2852 }
2853
2854 if (channel_map_param->channels != channels) {
2855 ALOGE("%s:: Channels(%d) does not match stream channels(%d)",
2856 __func__, channel_map_param->channels, channels);
2857 goto exit;
2858 }
2859
2860 for ( i = 0; i < channels; i++) {
2861 ALOGV("%s:: channel_map[%d]- %d", __func__, i, channel_map_param->channel_map[i]);
2862 out->channel_map_param.channel_map[i] = channel_map_param->channel_map[i];
2863 }
2864 ret = 0;
2865exit:
2866 return ret;
2867}
Varun Balaraje49253e2017-07-06 19:48:56 +05302868
2869int audio_extn_utils_set_pan_scale_params(
2870 struct stream_out *out,
2871 struct mix_matrix_params *mm_params)
2872{
2873 int ret = -EINVAL, i = 0, j = 0;
2874
Varun Balaraj003ee752017-08-07 17:54:55 +05302875 if (mm_params == NULL || out == NULL) {
2876 ALOGE("%s:: Invalid mix matrix or out param", __func__);
Varun Balaraje49253e2017-07-06 19:48:56 +05302877 goto exit;
2878 }
2879
2880 if (mm_params->num_output_channels > MAX_CHANNELS_SUPPORTED ||
2881 mm_params->num_output_channels <= 0 ||
2882 mm_params->num_input_channels > MAX_CHANNELS_SUPPORTED ||
2883 mm_params->num_input_channels <= 0)
2884 goto exit;
2885
2886 out->pan_scale_params.num_output_channels = mm_params->num_output_channels;
2887 out->pan_scale_params.num_input_channels = mm_params->num_input_channels;
2888 out->pan_scale_params.has_output_channel_map =
2889 mm_params->has_output_channel_map;
2890 for (i = 0; i < mm_params->num_output_channels; i++)
2891 out->pan_scale_params.output_channel_map[i] =
2892 mm_params->output_channel_map[i];
2893
2894 out->pan_scale_params.has_input_channel_map =
2895 mm_params->has_input_channel_map;
2896 for (i = 0; i < mm_params->num_input_channels; i++)
2897 out->pan_scale_params.input_channel_map[i] =
2898 mm_params->input_channel_map[i];
2899
2900 out->pan_scale_params.has_mixer_coeffs = mm_params->has_mixer_coeffs;
2901 for (i = 0; i < mm_params->num_output_channels; i++)
2902 for (j = 0; j < mm_params->num_input_channels; j++) {
2903 //Convert the channel coefficient gains in Q14 format
2904 out->pan_scale_params.mixer_coeffs[i][j] =
2905 mm_params->mixer_coeffs[i][j] * (2 << 13);
2906 }
2907
2908 ret = platform_set_stream_pan_scale_params(out->dev->platform,
2909 out->pcm_device_id,
2910 out->pan_scale_params);
2911
2912exit:
2913 return ret;
2914}
2915
2916int audio_extn_utils_set_downmix_params(
2917 struct stream_out *out,
2918 struct mix_matrix_params *mm_params)
2919{
2920 int ret = -EINVAL, i = 0, j = 0;
2921 struct audio_usecase *usecase = NULL;
2922
Aniket Kumar Lataf9f246e2017-09-15 15:20:16 -07002923 if (mm_params == NULL || out == NULL) {
Varun Balaraj003ee752017-08-07 17:54:55 +05302924 ALOGE("%s:: Invalid mix matrix or out param", __func__);
Varun Balaraje49253e2017-07-06 19:48:56 +05302925 goto exit;
2926 }
2927
2928 if (mm_params->num_output_channels > MAX_CHANNELS_SUPPORTED ||
2929 mm_params->num_output_channels <= 0 ||
2930 mm_params->num_input_channels > MAX_CHANNELS_SUPPORTED ||
2931 mm_params->num_input_channels <= 0)
2932 goto exit;
2933
2934 usecase = get_usecase_from_list(out->dev, out->usecase);
Varun Balaraj003ee752017-08-07 17:54:55 +05302935 if (!usecase) {
2936 ALOGE("%s: Get usecase list failed!", __func__);
Aniket Kumar Lataf9f246e2017-09-15 15:20:16 -07002937 goto exit;
2938 }
Varun Balaraje49253e2017-07-06 19:48:56 +05302939 out->downmix_params.num_output_channels = mm_params->num_output_channels;
2940 out->downmix_params.num_input_channels = mm_params->num_input_channels;
2941
2942 out->downmix_params.has_output_channel_map =
2943 mm_params->has_output_channel_map;
2944 for (i = 0; i < mm_params->num_output_channels; i++) {
2945 out->downmix_params.output_channel_map[i] =
2946 mm_params->output_channel_map[i];
2947 }
2948
2949 out->downmix_params.has_input_channel_map =
2950 mm_params->has_input_channel_map;
2951 for (i = 0; i < mm_params->num_input_channels; i++)
2952 out->downmix_params.input_channel_map[i] =
2953 mm_params->input_channel_map[i];
2954
2955 out->downmix_params.has_mixer_coeffs = mm_params->has_mixer_coeffs;
2956 for (i = 0; i < mm_params->num_output_channels; i++)
Nikhil Latukar2e6f6242017-08-15 13:37:07 +05302957 for (j = 0; j < mm_params->num_input_channels; j++) {
2958 //Convert the channel coefficient gains in Q14 format
Varun Balaraje49253e2017-07-06 19:48:56 +05302959 out->downmix_params.mixer_coeffs[i][j] =
Nikhil Latukar2e6f6242017-08-15 13:37:07 +05302960 mm_params->mixer_coeffs[i][j] * (2 << 13);
2961 }
Varun Balaraje49253e2017-07-06 19:48:56 +05302962
2963 ret = platform_set_stream_downmix_params(out->dev->platform,
2964 out->pcm_device_id,
2965 usecase->out_snd_device,
2966 out->downmix_params);
2967
2968exit:
2969 return ret;
2970}
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05302971
2972bool audio_extn_utils_is_dolby_format(audio_format_t format)
2973{
2974 if (format == AUDIO_FORMAT_AC3 ||
2975 format == AUDIO_FORMAT_E_AC3 ||
2976 format == AUDIO_FORMAT_E_AC3_JOC)
2977 return true;
2978 else
2979 return false;
2980}
2981
`Deeraj Soman676c2702017-09-18 19:25:53 +05302982int audio_extn_utils_get_bit_width_from_string(const char *id_string)
2983{
2984 int i;
2985 const mixer_config_lookup mixer_bitwidth_config[] = {{"S24_3LE", 24},
2986 {"S32_LE", 32},
2987 {"S24_LE", 24},
2988 {"S16_LE", 16}};
2989 int num_configs = sizeof(mixer_bitwidth_config) / sizeof(mixer_bitwidth_config[0]);
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05302990
`Deeraj Soman676c2702017-09-18 19:25:53 +05302991 for (i = 0; i < num_configs; i++) {
2992 if (!strcmp(id_string, mixer_bitwidth_config[i].id_string))
2993 return mixer_bitwidth_config[i].value;
2994 }
2995
2996 return -EINVAL;
2997}
2998
2999int audio_extn_utils_get_sample_rate_from_string(const char *id_string)
3000{
3001 int i;
3002 const mixer_config_lookup mixer_samplerate_config[] = {{"KHZ_32", 32000},
3003 {"KHZ_48", 48000},
3004 {"KHZ_96", 96000},
3005 {"KHZ_144", 144000},
3006 {"KHZ_192", 192000},
3007 {"KHZ_384", 384000},
3008 {"KHZ_44P1", 44100},
3009 {"KHZ_88P2", 88200},
3010 {"KHZ_176P4", 176400},
3011 {"KHZ_352P8", 352800}};
3012 int num_configs = sizeof(mixer_samplerate_config) / sizeof(mixer_samplerate_config[0]);
3013
3014 for (i = 0; i < num_configs; i++) {
3015 if (!strcmp(id_string, mixer_samplerate_config[i].id_string))
3016 return mixer_samplerate_config[i].value;
3017 }
3018
3019 return -EINVAL;
3020}
3021
3022int audio_extn_utils_get_channels_from_string(const char *id_string)
3023{
3024 int i;
3025 const mixer_config_lookup mixer_channels_config[] = {{"One", 1},
3026 {"Two", 2},
3027 {"Three",3},
3028 {"Four", 4},
3029 {"Five", 5},
3030 {"Six", 6},
3031 {"Seven", 7},
3032 {"Eight", 8}};
3033 int num_configs = sizeof(mixer_channels_config) / sizeof(mixer_channels_config[0]);
3034
3035 for (i = 0; i < num_configs; i++) {
3036 if (!strcmp(id_string, mixer_channels_config[i].id_string))
3037 return mixer_channels_config[i].value;
3038 }
3039
3040 return -EINVAL;
3041}
Surendar karka30569792018-05-08 12:02:21 +05303042
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08003043void audio_extn_utils_release_snd_device(snd_device_t snd_device)
3044{
3045 audio_extn_dev_arbi_release(snd_device);
3046 audio_extn_sound_trigger_update_device_status(snd_device,
3047 ST_EVENT_SND_DEVICE_FREE);
3048 audio_extn_listen_update_device_status(snd_device,
3049 LISTEN_EVENT_SND_DEVICE_FREE);
3050}
3051
Weiyin Jiangd5718bd2019-09-04 16:52:08 +08003052int audio_extn_utils_get_license_params(
3053 const struct audio_device *adev,
3054 struct audio_license_params *license_params)
Surendar karka30569792018-05-08 12:02:21 +05303055{
3056 if(!license_params)
3057 return -EINVAL;
Weiyin Jiangd5718bd2019-09-04 16:52:08 +08003058
3059 return platform_get_license_by_product(adev->platform,
3060 (const char*)license_params->product, &license_params->key, license_params->license);
Surendar karka30569792018-05-08 12:02:21 +05303061}
3062
Aalique Grahame22e49102018-12-18 14:23:57 -08003063int audio_extn_utils_send_app_type_gain(struct audio_device *adev,
3064 int app_type,
3065 int *gain)
3066{
3067 int gain_cfg[4];
3068 const char *mixer_ctl_name = "App Type Gain";
3069 struct mixer_ctl *ctl;
3070 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
3071 if (!ctl) {
3072 ALOGE("%s: Could not get volume ctl mixer %s", __func__,
3073 mixer_ctl_name);
3074 return -EINVAL;
3075 }
3076 gain_cfg[0] = 0;
3077 gain_cfg[1] = app_type;
3078 gain_cfg[2] = gain[0];
3079 gain_cfg[3] = gain[1];
3080 ALOGV("%s app_type %d l(%d) r(%d)", __func__, app_type, gain[0], gain[1]);
3081 return mixer_ctl_set_array(ctl, gain_cfg,
3082 sizeof(gain_cfg)/sizeof(gain_cfg[0]));
3083}
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -07003084
Weiyin Jiangd5718bd2019-09-04 16:52:08 +08003085static void vndk_fwk_init()
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -07003086{
Weiyin Jiangd5718bd2019-09-04 16:52:08 +08003087 if (mVndkFwk.lib_handle != NULL)
3088 return;
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -07003089
Weiyin Jiangd5718bd2019-09-04 16:52:08 +08003090 mVndkFwk.lib_handle = dlopen(VNDK_FWK_LIB_PATH, RTLD_NOW);
3091 if (mVndkFwk.lib_handle == NULL) {
3092 ALOGW("%s: failed to dlopen VNDK_FWK_LIB %s", __func__, strerror(errno));
3093 return;
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -07003094 }
3095
Weiyin Jiangd5718bd2019-09-04 16:52:08 +08003096 *(void **)(&mVndkFwk.isVendorEnhancedFwk) =
3097 dlsym(mVndkFwk.lib_handle, "isRunningWithVendorEnhancedFramework");
3098 if (mVndkFwk.isVendorEnhancedFwk == NULL) {
3099 ALOGW("%s: dlsym failed %s", __func__, strerror(errno));
3100 if (mVndkFwk.lib_handle) {
3101 dlclose(mVndkFwk.lib_handle);
3102 mVndkFwk.lib_handle = NULL;
3103 }
3104 return;
3105 }
3106
3107
3108 *(void **)(&mVndkFwk.getVendorEnhancedInfo) =
3109 dlsym(mVndkFwk.lib_handle, "getVendorEnhancedInfo");
3110 if (mVndkFwk.getVendorEnhancedInfo == NULL) {
3111 ALOGW("%s: dlsym failed %s", __func__, strerror(errno));
3112 if (mVndkFwk.lib_handle) {
3113 dlclose(mVndkFwk.lib_handle);
3114 mVndkFwk.lib_handle = NULL;
3115 }
3116 }
3117
3118 return;
3119}
3120
3121bool audio_extn_utils_is_vendor_enhanced_fwk()
3122{
3123 static int is_vendor_enhanced_fwk = -EINVAL;
3124 if (is_vendor_enhanced_fwk != -EINVAL)
3125 return (bool)is_vendor_enhanced_fwk;
3126
3127 vndk_fwk_init();
3128
3129 if (mVndkFwk.isVendorEnhancedFwk != NULL) {
3130 is_vendor_enhanced_fwk = mVndkFwk.isVendorEnhancedFwk();
3131 ALOGW("%s: is_vendor_enhanced_fwk %d", __func__, is_vendor_enhanced_fwk);
3132 } else {
3133 is_vendor_enhanced_fwk = 0;
3134 ALOGW("%s: default to non enhanced_fwk config", __func__);
3135 }
3136
3137 return (bool)is_vendor_enhanced_fwk;
3138}
3139
3140int audio_extn_utils_get_vendor_enhanced_info()
3141{
3142 static int vendor_enhanced_info = -EINVAL;
3143 if (vendor_enhanced_info != -EINVAL)
3144 return vendor_enhanced_info;
3145
3146 vndk_fwk_init();
3147
3148 if (mVndkFwk.getVendorEnhancedInfo != NULL) {
3149 vendor_enhanced_info = mVndkFwk.getVendorEnhancedInfo();
3150 ALOGW("%s: vendor_enhanced_info 0x%x", __func__, vendor_enhanced_info);
3151 } else {
3152 vendor_enhanced_info = 0x0;
3153 ALOGW("%s: default to vendor_enhanced_info 0x0", __func__);
3154 }
3155
3156 return vendor_enhanced_info;
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -07003157}
Deeraj Soman14230922019-01-30 16:39:30 +05303158
Deeraj Somanfa377bf2019-02-06 12:57:59 +05303159int audio_extn_utils_get_perf_mode_flag(void)
3160{
3161#ifdef COMPRESSED_PERF_MODE_FLAG
3162 return COMPRESSED_PERF_MODE_FLAG;
3163#else
3164 return 0;
3165#endif
3166}
3167
Deeraj Soman14230922019-01-30 16:39:30 +05303168size_t audio_extn_utils_get_input_buffer_size(uint32_t sample_rate,
3169 audio_format_t format,
3170 int channel_count,
3171 int64_t duration_ms,
3172 bool is_low_latency)
3173{
3174 size_t size = 0;
3175 size_t capture_duration = AUDIO_CAPTURE_PERIOD_DURATION_MSEC;
3176 uint32_t bytes_per_period_sample = 0;
3177
3178
3179 if (audio_extn_utils_check_input_parameters(sample_rate, format, channel_count) != 0)
3180 return 0;
3181
3182 if (duration_ms >= MIN_OFFLOAD_BUFFER_DURATION_MS && duration_ms <= MAX_OFFLOAD_BUFFER_DURATION_MS)
3183 capture_duration = duration_ms;
3184
3185 size = (sample_rate * capture_duration) / 1000;
3186 if (is_low_latency)
3187 size = LOW_LATENCY_CAPTURE_PERIOD_SIZE;
3188
3189
3190 bytes_per_period_sample = audio_bytes_per_sample(format) * channel_count;
3191 size *= bytes_per_period_sample;
3192
3193 /* make sure the size is multiple of 32 bytes and additionally multiple of
3194 * the frame_size (required for 24bit samples and non-power-of-2 channel counts)
3195 * At 48 kHz mono 16-bit PCM:
3196 * 5.000 ms = 240 frames = 15*16*1*2 = 480, a whole multiple of 32 (15)
3197 * 3.333 ms = 160 frames = 10*16*1*2 = 320, a whole multiple of 32 (10)
3198 *
3199 * The loop reaches result within 32 iterations, as initial size is
3200 * already a multiple of frame_size
3201 */
3202 size = audio_extn_utils_nearest_multiple(size, audio_extn_utils_lcm(32, bytes_per_period_sample));
3203
3204 return size;
3205}
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07003206
3207int audio_extn_utils_hash_fn(void *key)
3208{
3209 return (int)key;
3210}
3211
3212bool audio_extn_utils_hash_eq(void *key1, void *key2)
3213{
3214 return (key1 == key2);
3215}