blob: 0ba2f4f82946259f76231f20b7ccd2d7eb071e44 [file] [log] [blame]
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001/*
Lakshman Chaluvarajue12dfa12021-01-27 23:22:58 +05302 * Copyright (c) 2014-2021, 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
Roopesh Nataraja0f68be72020-04-28 12:32:02 -070057#define AUDIO_IO_POLICY_VENDOR_CONFIG_FILE_NAME "audio_io_policy.conf"
58#define AUDIO_OUTPUT_POLICY_VENDOR_CONFIG_FILE_NAME "audio_output_policy.conf"
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070059
60#define OUTPUTS_TAG "outputs"
Dhananjay Kumard6d32152016-10-13 16:11:03 +053061#define INPUTS_TAG "inputs"
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070062
63#define DYNAMIC_VALUE_TAG "dynamic"
64#define FLAGS_TAG "flags"
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +053065#define PROFILES_TAG "profile"
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070066#define FORMATS_TAG "formats"
67#define SAMPLING_RATES_TAG "sampling_rates"
68#define BIT_WIDTH_TAG "bit_width"
69#define APP_TYPE_TAG "app_type"
70
71#define STRING_TO_ENUM(string) { #string, string }
72#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
73
Ben Rombergera04fabc2014-11-14 12:16:03 -080074#define BASE_TABLE_SIZE 64
75#define MAX_BASEINDEX_LEN 256
76
Ben Romberger1aaaf862017-04-06 17:49:46 -070077#ifndef SND_AUDIOCODEC_TRUEHD
78#define SND_AUDIOCODEC_TRUEHD 0x00000023
79#endif
80
Vikram Panduranga93f080e2017-06-07 18:16:14 -070081#define APP_TYPE_VOIP_AUDIO 0x1113A
82
Ashish Jain81eb2a82015-05-13 10:52:34 +053083#ifdef AUDIO_EXTERNAL_HDMI_ENABLED
84#define PROFESSIONAL (1<<0) /* 0 = consumer, 1 = professional */
85#define NON_LPCM (1<<1) /* 0 = audio, 1 = non-audio */
86#define SR_44100 (0<<0) /* 44.1kHz */
87#define SR_NOTID (1<<0) /* non indicated */
88#define SR_48000 (2<<0) /* 48kHz */
89#define SR_32000 (3<<0) /* 32kHz */
90#define SR_22050 (4<<0) /* 22.05kHz */
91#define SR_24000 (6<<0) /* 24kHz */
92#define SR_88200 (8<<0) /* 88.2kHz */
93#define SR_96000 (10<<0) /* 96kHz */
94#define SR_176400 (12<<0) /* 176.4kHz */
95#define SR_192000 (14<<0) /* 192kHz */
96
97#endif
Manish Dewangan07de2142017-02-27 19:27:20 +053098
99/* ToDo: Check and update a proper value in msec */
Weiyin Jiangc49a3b52018-08-17 16:16:08 +0800100#define COMPRESS_OFFLOAD_PLAYBACK_LATENCY 50
101#define PCM_OFFLOAD_PLAYBACK_DSP_PATHDELAY 62
Samyak Jain2546f8a2019-08-29 19:24:21 +0530102#define PCM_OFFLOAD_PLAYBACK_LATENCY PCM_OFFLOAD_PLAYBACK_DSP_PATHDELAY
Manish Dewangan07de2142017-02-27 19:27:20 +0530103
Varun Balaraje49253e2017-07-06 19:48:56 +0530104#ifndef MAX_CHANNELS_SUPPORTED
105#define MAX_CHANNELS_SUPPORTED 8
106#endif
107
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -0700108#ifdef __LP64__
109#define VNDK_FWK_LIB_PATH "/vendor/lib64/libqti_vndfwk_detect.so"
110#else
111#define VNDK_FWK_LIB_PATH "/vendor/lib/libqti_vndfwk_detect.so"
112#endif
113
Weiyin Jiangd5718bd2019-09-04 16:52:08 +0800114typedef struct vndkfwk_s {
115 void *lib_handle;
116 int (*isVendorEnhancedFwk)(void);
117 int (*getVendorEnhancedInfo)(void);
118 const char *lib_name;
119} vndkfwk_t;
120
121static vndkfwk_t mVndkFwk = {
122 NULL, NULL, NULL, VNDK_FWK_LIB_PATH};
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -0700123
`Deeraj Soman676c2702017-09-18 19:25:53 +0530124typedef struct {
125 const char *id_string;
126 const int value;
127} mixer_config_lookup;
128
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700129struct string_to_enum {
130 const char *name;
131 uint32_t value;
132};
133
134const struct string_to_enum s_flag_name_to_enum_table[] = {
135 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_DIRECT),
136 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_PRIMARY),
137 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_FAST),
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800138 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_RAW),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700139 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_DEEP_BUFFER),
140 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD),
141 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_NON_BLOCKING),
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700142 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_HW_AV_SYNC),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700143 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_INCALL_MUSIC),
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700144 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH),
Naresh Tanniruee3499a2017-01-05 14:05:35 +0530145 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_TIMESTAMP),
Vikram Panduranga93f080e2017-06-07 18:16:14 -0700146 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_VOIP_RX),
Ben Romberger6c4d3812017-06-13 17:46:45 -0700147 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_BD),
Varun Balaraje49253e2017-07-06 19:48:56 +0530148 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_INTERACTIVE),
Derek Chen090dfc52018-03-22 22:15:29 -0400149 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_MEDIA),
150 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_SYS_NOTIFICATION),
151 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_NAV_GUIDANCE),
152 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_PHONE),
Derek Chen1bcdc6b2020-02-06 22:44:53 -0800153 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_FRONT_PASSENGER),
Derek Chendf05eea2019-08-01 13:57:49 -0700154 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_REAR_SEAT),
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530155 STRING_TO_ENUM(AUDIO_INPUT_FLAG_NONE),
156 STRING_TO_ENUM(AUDIO_INPUT_FLAG_FAST),
157 STRING_TO_ENUM(AUDIO_INPUT_FLAG_HW_HOTWORD),
158 STRING_TO_ENUM(AUDIO_INPUT_FLAG_RAW),
159 STRING_TO_ENUM(AUDIO_INPUT_FLAG_SYNC),
Dhananjay Kumaree4d2002016-10-25 18:02:58 +0530160 STRING_TO_ENUM(AUDIO_INPUT_FLAG_TIMESTAMP),
Dhananjay Kumar704ce6f2017-09-28 22:08:00 +0530161 STRING_TO_ENUM(AUDIO_INPUT_FLAG_COMPRESS),
Ralf Herzaec80262018-07-03 07:08:49 +0200162 STRING_TO_ENUM(AUDIO_INPUT_FLAG_PASSTHROUGH),
George Gao4759d792020-09-17 22:29:07 -0700163 STRING_TO_ENUM(AUDIO_INPUT_FLAG_MMAP_NOIRQ),
164 STRING_TO_ENUM(AUDIO_INPUT_FLAG_VOIP_TX),
165 STRING_TO_ENUM(AUDIO_INPUT_FLAG_HW_AV_SYNC),
166 STRING_TO_ENUM(AUDIO_INPUT_FLAG_DIRECT),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700167};
168
169const struct string_to_enum s_format_name_to_enum_table[] = {
Ashish Jain83a6cc22016-06-28 14:34:17 +0530170 STRING_TO_ENUM(AUDIO_FORMAT_PCM_8_BIT),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700171 STRING_TO_ENUM(AUDIO_FORMAT_PCM_16_BIT),
Ashish Jain5106d362016-05-11 19:23:33 +0530172 STRING_TO_ENUM(AUDIO_FORMAT_PCM_24_BIT_PACKED),
173 STRING_TO_ENUM(AUDIO_FORMAT_PCM_8_24_BIT),
Ashish Jainf1eaa582016-05-23 20:54:24 +0530174 STRING_TO_ENUM(AUDIO_FORMAT_PCM_32_BIT),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700175 STRING_TO_ENUM(AUDIO_FORMAT_MP3),
176 STRING_TO_ENUM(AUDIO_FORMAT_AAC),
177 STRING_TO_ENUM(AUDIO_FORMAT_VORBIS),
Mingming Yinae3530f2014-07-03 16:50:18 -0700178 STRING_TO_ENUM(AUDIO_FORMAT_AMR_NB),
179 STRING_TO_ENUM(AUDIO_FORMAT_AMR_WB),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700180 STRING_TO_ENUM(AUDIO_FORMAT_AC3),
Mingming Yinae3530f2014-07-03 16:50:18 -0700181 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700182 STRING_TO_ENUM(AUDIO_FORMAT_DTS),
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530183 STRING_TO_ENUM(AUDIO_FORMAT_DTS_HD),
Ben Romberger1aaaf862017-04-06 17:49:46 -0700184 STRING_TO_ENUM(AUDIO_FORMAT_DOLBY_TRUEHD),
Naresh Tanniru928f0862017-04-07 16:44:23 -0700185 STRING_TO_ENUM(AUDIO_FORMAT_IEC61937),
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530186 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3_JOC),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700187 STRING_TO_ENUM(AUDIO_FORMAT_WMA),
188 STRING_TO_ENUM(AUDIO_FORMAT_WMA_PRO),
189 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADIF),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700190 STRING_TO_ENUM(AUDIO_FORMAT_AMR_WB_PLUS),
191 STRING_TO_ENUM(AUDIO_FORMAT_EVRC),
192 STRING_TO_ENUM(AUDIO_FORMAT_EVRCB),
193 STRING_TO_ENUM(AUDIO_FORMAT_EVRCWB),
194 STRING_TO_ENUM(AUDIO_FORMAT_QCELP),
195 STRING_TO_ENUM(AUDIO_FORMAT_MP2),
196 STRING_TO_ENUM(AUDIO_FORMAT_EVRCNW),
Amit Shekhar6f461b12014-08-01 14:52:58 -0700197 STRING_TO_ENUM(AUDIO_FORMAT_FLAC),
Satya Krishna Pindiproli70471602015-04-24 19:12:43 +0530198 STRING_TO_ENUM(AUDIO_FORMAT_ALAC),
199 STRING_TO_ENUM(AUDIO_FORMAT_APE),
Alexy Josephcd8eaed2014-12-11 12:46:53 -0800200 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LC),
201 STRING_TO_ENUM(AUDIO_FORMAT_AAC_HE_V1),
202 STRING_TO_ENUM(AUDIO_FORMAT_AAC_HE_V2),
Manish Dewangana6fc5442015-08-24 20:30:31 +0530203 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS),
Ashish Jaine513a872015-11-19 17:00:56 +0530204 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_LC),
205 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_HE_V1),
206 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_HE_V2),
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +0530207 STRING_TO_ENUM(AUDIO_FORMAT_DSD),
Arun Kumar Dasari3b174182016-12-27 13:01:14 +0530208 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM),
209 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM_LC),
210 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM_HE_V1),
211 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM_HE_V2),
Dhanalakshmi Siddani18737932016-11-29 17:33:17 +0530212 STRING_TO_ENUM(AUDIO_FORMAT_APTX),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700213};
214
Manish Dewangan3ccdea52017-02-13 19:31:54 +0530215/* payload structure avt_device drift query */
216struct audio_avt_device_drift_stats {
217 uint32_t minor_version;
Manish Dewangan338c50a2017-09-12 15:22:03 +0530218
Manish Dewangan3ccdea52017-02-13 19:31:54 +0530219 /* Indicates the device interface direction as either
220 * source (Tx) or sink (Rx).
221 */
222 uint16_t device_direction;
Manish Dewangan338c50a2017-09-12 15:22:03 +0530223
224 /* Reference timer for drift accumulation and time stamp information.
225 * currently it only support AFE_REF_TIMER_TYPE_AVTIMER
226 */
227 uint16_t reference_timer;
Manish Dewangan3ccdea52017-02-13 19:31:54 +0530228 struct audio_avt_device_drift_param drift_param;
Manish Dewangan338c50a2017-09-12 15:22:03 +0530229} __attribute__((packed));
Manish Dewangan3ccdea52017-02-13 19:31:54 +0530230
Ben Rombergera04fabc2014-11-14 12:16:03 -0800231static char bTable[BASE_TABLE_SIZE] = {
232 'A','B','C','D','E','F','G','H','I','J','K','L',
233 'M','N','O','P','Q','R','S','T','U','V','W','X',
234 'Y','Z','a','b','c','d','e','f','g','h','i','j',
235 'k','l','m','n','o','p','q','r','s','t','u','v',
236 'w','x','y','z','0','1','2','3','4','5','6','7',
237 '8','9','+','/'
238};
239
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700240static uint32_t string_to_enum(const struct string_to_enum *table, size_t size,
241 const char *name)
242{
243 size_t i;
244 for (i = 0; i < size; i++) {
245 if (strcmp(table[i].name, name) == 0) {
246 ALOGV("%s found %s", __func__, table[i].name);
247 return table[i].value;
248 }
249 }
George Gao4759d792020-09-17 22:29:07 -0700250 ALOGE("%s cound not find %s", __func__, name);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700251 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
Roopesh Nataraja0f68be72020-04-28 12:32:02 -0700520/* Function to retrieve audio vendor configs path */
521void audio_get_vendor_config_path (char* config_file_path, int path_size)
522{
523 char vendor_sku[PROPERTY_VALUE_MAX] = {'\0'};
524 if (property_get("ro.boot.product.vendor.sku", vendor_sku, "") <= 0) {
525#ifdef LINUX_ENABLED
526 /* Audio configs are stored in /etc */
527 snprintf(config_file_path, path_size, "%s", "/etc");
528#else
529 /* Audio configs are stored in /vendor/etc */
530 snprintf(config_file_path, path_size, "%s", "/vendor/etc");
531#endif
532 } else {
533 /* Audio configs are stored in /vendor/etc/audio/sku_${vendor_sku} */
534 snprintf(config_file_path, path_size,
535 "%s%s", "/vendor/etc/audio/sku_", vendor_sku);
536 }
537}
538
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530539void audio_extn_utils_update_streams_cfg_lists(void *platform,
540 struct mixer *mixer,
541 struct listnode *streams_output_cfg_list,
542 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700543{
544 cnode *root;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530545 char *data = NULL;
Roopesh Nataraja0f68be72020-04-28 12:32:02 -0700546 char vendor_config_path[VENDOR_CONFIG_PATH_MAX_LENGTH];
547 char audio_io_policy_file[VENDOR_CONFIG_FILE_MAX_LENGTH];
548 char audio_output_policy_file[VENDOR_CONFIG_FILE_MAX_LENGTH];
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700549
550 ALOGV("%s", __func__);
551 list_init(streams_output_cfg_list);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530552 list_init(streams_input_cfg_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700553
554 root = config_node("", "");
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700555 if (root == NULL) {
556 ALOGE("cfg_list, NULL config root");
557 return;
558 }
559
Roopesh Nataraja0f68be72020-04-28 12:32:02 -0700560 /* Get path for audio configuration files in vendor */
561 audio_get_vendor_config_path(vendor_config_path,
562 sizeof(vendor_config_path));
563
564 /* Get path for audio_io_policy_file in vendor */
565 snprintf(audio_io_policy_file, sizeof(audio_io_policy_file),
566 "%s/%s", vendor_config_path, AUDIO_IO_POLICY_VENDOR_CONFIG_FILE_NAME);
567
568 /* Load audio_io_policy_file from vendor */
569 data = (char *)load_file(audio_io_policy_file, NULL);
570
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530571 if (data == NULL) {
572 ALOGD("%s: failed to open io config file(%s), trying older config file",
Roopesh Nataraja0f68be72020-04-28 12:32:02 -0700573 __func__, audio_io_policy_file);
574
575 /* Get path for audio_output_policy_file in vendor */
576 snprintf(audio_output_policy_file, sizeof(audio_output_policy_file),
577 "%s/%s", vendor_config_path,
578 AUDIO_OUTPUT_POLICY_VENDOR_CONFIG_FILE_NAME);
579
580 /* Load audio_output_policy_file from vendor */
581 data = (char *)load_file(audio_output_policy_file, NULL);
582
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530583 if (data == NULL) {
584 send_app_type_cfg(platform, mixer,
585 streams_output_cfg_list,
586 streams_input_cfg_list);
587 ALOGE("%s: could not load io policy config!", __func__);
yidongh6eb4f752017-04-19 18:20:57 +0800588 free(root);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530589 return;
590 }
591 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700592
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530593 config_load(root, data);
594 load_cfg_list(root, platform, streams_output_cfg_list,
595 streams_input_cfg_list);
596
597 send_app_type_cfg(platform, mixer, streams_output_cfg_list,
598 streams_input_cfg_list);
Alexy Josephaee4fdd2016-01-29 13:02:07 -0800599
600 config_free(root);
yidongh6eb4f752017-04-19 18:20:57 +0800601 free(root);
Alexy Josephaee4fdd2016-01-29 13:02:07 -0800602 free(data);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700603}
604
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530605static void audio_extn_utils_dump_streams_cfg_list(
606 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700607{
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700608 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530609 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700610 struct stream_format *sf_info;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700611 struct stream_sample_rate *ss_info;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530612
613 list_for_each(node_i, streams_cfg_list) {
614 s_info = node_to_item(node_i, struct streams_io_cfg, list);
615 ALOGV("%s: flags-%d, sample_rate-%d, bit_width-%d, app_type-%d",
616 __func__, s_info->flags.out_flags, s_info->app_type_cfg.sample_rate,
617 s_info->app_type_cfg.bit_width, s_info->app_type_cfg.app_type);
618 list_for_each(node_j, &s_info->format_list) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700619 sf_info = node_to_item(node_j, struct stream_format, list);
620 ALOGV("format-%x", sf_info->format);
621 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530622 list_for_each(node_j, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700623 ss_info = node_to_item(node_j, struct stream_sample_rate, list);
624 ALOGV("sample rate-%d", ss_info->sample_rate);
625 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700626 }
627}
628
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530629void audio_extn_utils_dump_streams_cfg_lists(
630 struct listnode *streams_output_cfg_list,
631 struct listnode *streams_input_cfg_list)
632{
633 ALOGV("%s", __func__);
634 audio_extn_utils_dump_streams_cfg_list(streams_output_cfg_list);
635 audio_extn_utils_dump_streams_cfg_list(streams_input_cfg_list);
636}
637
638static void audio_extn_utils_release_streams_cfg_list(
639 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700640{
641 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530642 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700643
644 ALOGV("%s", __func__);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530645
646 while (!list_empty(streams_cfg_list)) {
647 node_i = list_head(streams_cfg_list);
648 s_info = node_to_item(node_i, struct streams_io_cfg, list);
649 while (!list_empty(&s_info->format_list)) {
650 node_j = list_head(&s_info->format_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700651 list_remove(node_j);
652 free(node_to_item(node_j, struct stream_format, list));
653 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530654 while (!list_empty(&s_info->sample_rate_list)) {
655 node_j = list_head(&s_info->sample_rate_list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700656 list_remove(node_j);
657 free(node_to_item(node_j, struct stream_sample_rate, list));
658 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700659 list_remove(node_i);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530660 free(node_to_item(node_i, struct streams_io_cfg, list));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700661 }
662}
663
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530664void audio_extn_utils_release_streams_cfg_lists(
665 struct listnode *streams_output_cfg_list,
666 struct listnode *streams_input_cfg_list)
667{
668 ALOGV("%s", __func__);
669 audio_extn_utils_release_streams_cfg_list(streams_output_cfg_list);
670 audio_extn_utils_release_streams_cfg_list(streams_input_cfg_list);
671}
672
673static bool set_app_type_cfg(struct streams_io_cfg *s_info,
674 struct stream_app_type_cfg *app_type_cfg,
675 uint32_t sample_rate, uint32_t bit_width)
Amit Shekhar6f461b12014-08-01 14:52:58 -0700676 {
677 struct listnode *node_i;
678 struct stream_sample_rate *ss_info;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530679 list_for_each(node_i, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700680 ss_info = node_to_item(node_i, struct stream_sample_rate, list);
681 if ((sample_rate <= ss_info->sample_rate) &&
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530682 (bit_width == s_info->app_type_cfg.bit_width)) {
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700683
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530684 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700685 app_type_cfg->sample_rate = ss_info->sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530686 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700687 ALOGV("%s app_type_cfg->app_type %d, app_type_cfg->sample_rate %d, app_type_cfg->bit_width %d",
688 __func__, app_type_cfg->app_type, app_type_cfg->sample_rate, app_type_cfg->bit_width);
689 return true;
690 }
691 }
692 /*
693 * Reiterate through the list assuming dafault sample rate.
694 * Handles scenario where input sample rate is higher
695 * than all sample rates in list for the input bit width.
696 */
697 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
Apoorv Raghuvanshif59bb222015-02-18 12:23:23 -0800698
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530699 list_for_each(node_i, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700700 ss_info = node_to_item(node_i, struct stream_sample_rate, list);
701 if ((sample_rate <= ss_info->sample_rate) &&
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530702 (bit_width == s_info->app_type_cfg.bit_width)) {
703 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700704 app_type_cfg->sample_rate = sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530705 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Apoorv Raghuvanshif59bb222015-02-18 12:23:23 -0800706 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 -0700707 __func__, app_type_cfg->app_type, app_type_cfg->sample_rate, app_type_cfg->bit_width);
708 return true;
709 }
710 }
711 return false;
712}
713
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530714void audio_extn_utils_update_stream_input_app_type_cfg(void *platform,
715 struct listnode *streams_input_cfg_list,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -0800716 struct listnode *devices __unused,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530717 audio_input_flags_t flags,
718 audio_format_t format,
719 uint32_t sample_rate,
720 uint32_t bit_width,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530721 char* profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530722 struct stream_app_type_cfg *app_type_cfg)
723{
724 struct listnode *node_i, *node_j;
725 struct streams_io_cfg *s_info;
726 struct stream_format *sf_info;
727
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530728 ALOGV("%s: flags: 0x%x, format: 0x%x sample_rate %d, profile %s",
729 __func__, flags, format, sample_rate, profile);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530730
731 list_for_each(node_i, streams_input_cfg_list) {
732 s_info = node_to_item(node_i, struct streams_io_cfg, list);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530733 /* Along with flags do profile matching if set at either end.*/
734 if (s_info->flags.in_flags == flags &&
735 ((profile[0] == '\0' && s_info->profile[0] == '\0') ||
736 strncmp(s_info->profile, profile, sizeof(s_info->profile)) == 0)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530737 list_for_each(node_j, &s_info->format_list) {
738 sf_info = node_to_item(node_j, struct stream_format, list);
739 if (sf_info->format == format) {
740 if (set_app_type_cfg(s_info, app_type_cfg, sample_rate, bit_width))
741 return;
742 }
743 }
744 }
745 }
746 ALOGW("%s: App type could not be selected. Falling back to default", __func__);
747 app_type_cfg->app_type = platform_get_default_app_type_v2(platform, PCM_CAPTURE);
748 app_type_cfg->sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
749 app_type_cfg->bit_width = 16;
750}
751
752void audio_extn_utils_update_stream_output_app_type_cfg(void *platform,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700753 struct listnode *streams_output_cfg_list,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -0800754 struct listnode *devices,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700755 audio_output_flags_t flags,
756 audio_format_t format,
Amit Shekhar6f461b12014-08-01 14:52:58 -0700757 uint32_t sample_rate,
758 uint32_t bit_width,
Manish Dewangan837dc462015-05-27 10:17:41 +0530759 audio_channel_mask_t channel_mask,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530760 char *profile,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700761 struct stream_app_type_cfg *app_type_cfg)
762{
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +0530763 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530764 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700765 struct stream_format *sf_info;
Manish Dewangan837dc462015-05-27 10:17:41 +0530766 char value[PROPERTY_VALUE_MAX] = {0};
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700767
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -0800768 if (compare_device_type(devices, AUDIO_DEVICE_OUT_SPEAKER)) {
Ramu Gottipati074fa4d2018-09-11 20:05:51 +0530769 int bw = platform_get_snd_device_bit_width(SND_DEVICE_OUT_SPEAKER);
770 if ((-ENOSYS != bw) && (bit_width > (uint32_t)bw))
Amit Shekhar5a39c912014-10-14 15:39:30 -0700771 bit_width = (uint32_t)bw;
Ramu Gottipati074fa4d2018-09-11 20:05:51 +0530772 else if (-ENOSYS == bw)
773 bit_width = CODEC_BACKEND_DEFAULT_BIT_WIDTH;
Amit Shekhar1d896042014-10-03 13:16:09 -0700774 sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Ramu Gottipati074fa4d2018-09-11 20:05:51 +0530775 ALOGI("%s Allowing 24 and above bits playback on speaker ONLY at default sampling rate", __func__);
Amit Shekhar1d896042014-10-03 13:16:09 -0700776 }
777
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -0700778 property_get("vendor.audio.playback.mch.downsample",value,"");
Manish Dewangan837dc462015-05-27 10:17:41 +0530779 if (!strncmp("true", value, sizeof("true"))) {
780 if ((popcount(channel_mask) > 2) &&
781 (sample_rate > CODEC_BACKEND_DEFAULT_SAMPLE_RATE) &&
Aalique Grahame5cd12ff2017-10-19 10:57:00 -0700782 !(flags & (audio_output_flags_t)AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH)) {
Manish Dewangan837dc462015-05-27 10:17:41 +0530783 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
784 ALOGD("%s: MCH session defaulting sample rate to %d",
785 __func__, sample_rate);
786 }
787 }
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +0530788
789 /* Set sampling rate to 176.4 for DSD64
790 * and 352.8Khz for DSD128.
791 * Set Bit Width to 16. output will be 16 bit
792 * post DoP in ASM.
793 */
Aalique Grahame5cd12ff2017-10-19 10:57:00 -0700794 if ((flags & (audio_output_flags_t)AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH) &&
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +0530795 (format == AUDIO_FORMAT_DSD)) {
796 bit_width = 16;
797 if (sample_rate == INPUT_SAMPLING_RATE_DSD64)
798 sample_rate = OUTPUT_SAMPLING_RATE_DSD64;
799 else if (sample_rate == INPUT_SAMPLING_RATE_DSD128)
800 sample_rate = OUTPUT_SAMPLING_RATE_DSD128;
801 }
802
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +0530803
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800804 ALOGV("%s: flags: %x, format: %x sample_rate %d, profile %s, app_type %d",
805 __func__, flags, format, sample_rate, profile, app_type_cfg->app_type);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700806 list_for_each(node_i, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530807 s_info = node_to_item(node_i, struct streams_io_cfg, list);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530808 /* Along with flags do profile matching if set at either end.*/
809 if (s_info->flags.out_flags == flags &&
810 ((profile[0] == '\0' && s_info->profile[0] == '\0') ||
811 strncmp(s_info->profile, profile, sizeof(s_info->profile)) == 0)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530812 list_for_each(node_j, &s_info->format_list) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700813 sf_info = node_to_item(node_j, struct stream_format, list);
814 if (sf_info->format == format) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530815 if (set_app_type_cfg(s_info, app_type_cfg, sample_rate, bit_width))
Amit Shekhar6f461b12014-08-01 14:52:58 -0700816 return;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700817 }
818 }
819 }
820 }
821 list_for_each(node_i, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530822 s_info = node_to_item(node_i, struct streams_io_cfg, list);
823 if (s_info->flags.out_flags == AUDIO_OUTPUT_FLAG_PRIMARY) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700824 ALOGV("Compatible output profile not found.");
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530825 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
826 app_type_cfg->sample_rate = s_info->app_type_cfg.sample_rate;
827 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700828 ALOGV("%s Default to primary output: App type: %d sample_rate %d",
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530829 __func__, s_info->app_type_cfg.app_type, app_type_cfg->sample_rate);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700830 return;
831 }
832 }
833 ALOGW("%s: App type could not be selected. Falling back to default", __func__);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700834 app_type_cfg->app_type = platform_get_default_app_type(platform);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700835 app_type_cfg->sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700836 app_type_cfg->bit_width = 16;
837}
838
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +0530839static bool audio_is_this_native_usecase(struct audio_usecase *uc)
840{
841 bool native_usecase = false;
842 struct stream_out *out = (struct stream_out*) uc->stream.out;
843
844 if (PCM_PLAYBACK == uc->type && out != NULL &&
845 NATIVE_AUDIO_MODE_INVALID != platform_get_native_support() &&
846 is_offload_usecase(uc->id) &&
847 (out->sample_rate == OUTPUT_SAMPLING_RATE_44100))
848 native_usecase = true;
849
850 return native_usecase;
851}
852
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800853bool audio_extn_is_dsp_bit_width_enforce_mode_supported(audio_output_flags_t flags)
854{
855 /* DSP bitwidth enforce mode for ADM and AFE:
856 * includes:
857 * deep buffer, low latency, direct pcm and offload.
858 * excludes:
859 * ull(raw+fast), VOIP.
860 */
861 if ((flags & AUDIO_OUTPUT_FLAG_VOIP_RX) ||
862 ((flags & AUDIO_OUTPUT_FLAG_RAW) &&
863 (flags & AUDIO_OUTPUT_FLAG_FAST)))
864 return false;
865
866
867 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) ||
868 (flags & AUDIO_OUTPUT_FLAG_DIRECT) ||
869 (flags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) ||
870 (flags & AUDIO_OUTPUT_FLAG_PRIMARY))
871 return true;
872 else
873 return false;
874}
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800875
876static inline bool audio_is_vr_mode_on(struct audio_device *(__attribute__((unused)) adev))
877{
878 return adev->vr_audio_mode_enabled;
879}
880
Derek Chenf7092792017-05-23 12:23:53 -0400881static void audio_extn_btsco_get_sample_rate(int snd_device, int *sample_rate)
882{
883 switch (snd_device) {
884 case SND_DEVICE_OUT_BT_SCO:
885 case SND_DEVICE_IN_BT_SCO_MIC:
886 case SND_DEVICE_IN_BT_SCO_MIC_NREC:
887 *sample_rate = 8000;
888 break;
889 case SND_DEVICE_OUT_BT_SCO_WB:
890 case SND_DEVICE_IN_BT_SCO_MIC_WB:
891 case SND_DEVICE_IN_BT_SCO_MIC_WB_NREC:
892 *sample_rate = 16000;
893 break;
894 default:
895 ALOGD("%s:Not a BT SCO device, need not update sampling rate\n", __func__);
896 break;
897 }
898}
899
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530900void audio_extn_utils_update_stream_app_type_cfg_for_usecase(
901 struct audio_device *adev,
902 struct audio_usecase *usecase)
903{
904 ALOGV("%s", __func__);
905
906 switch(usecase->type) {
907 case PCM_PLAYBACK:
908 audio_extn_utils_update_stream_output_app_type_cfg(adev->platform,
909 &adev->streams_output_cfg_list,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -0800910 &usecase->stream.out->device_list,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530911 usecase->stream.out->flags,
Aalique Grahame65780b52017-09-27 14:59:56 -0700912 usecase->stream.out->hal_op_format,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530913 usecase->stream.out->sample_rate,
914 usecase->stream.out->bit_width,
915 usecase->stream.out->channel_mask,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530916 usecase->stream.out->profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530917 &usecase->stream.out->app_type_cfg);
918 ALOGV("%s Selected apptype: %d", __func__, usecase->stream.out->app_type_cfg.app_type);
919 break;
920 case PCM_CAPTURE:
Vikram Panduranga93f080e2017-06-07 18:16:14 -0700921 if (usecase->id == USECASE_AUDIO_RECORD_VOIP)
922 usecase->stream.in->app_type_cfg.app_type = APP_TYPE_VOIP_AUDIO;
923 else
924 audio_extn_utils_update_stream_input_app_type_cfg(adev->platform,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530925 &adev->streams_input_cfg_list,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -0800926 &usecase->stream.in->device_list,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530927 usecase->stream.in->flags,
928 usecase->stream.in->format,
929 usecase->stream.in->sample_rate,
930 usecase->stream.in->bit_width,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530931 usecase->stream.in->profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530932 &usecase->stream.in->app_type_cfg);
933 ALOGV("%s Selected apptype: %d", __func__, usecase->stream.in->app_type_cfg.app_type);
934 break;
Surendar Karka93cd25a2018-08-28 14:21:37 +0530935 case TRANSCODE_LOOPBACK_RX :
Siddartha Shaik343abc62017-08-08 11:15:25 +0530936 audio_extn_utils_update_stream_output_app_type_cfg(adev->platform,
937 &adev->streams_output_cfg_list,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -0800938 &usecase->stream.inout->out_config.device_list,
Siddartha Shaik343abc62017-08-08 11:15:25 +0530939 0,
940 usecase->stream.inout->out_config.format,
941 usecase->stream.inout->out_config.sample_rate,
942 usecase->stream.inout->out_config.bit_width,
943 usecase->stream.inout->out_config.channel_mask,
944 usecase->stream.inout->profile,
945 &usecase->stream.inout->out_app_type_cfg);
946 ALOGV("%s Selected apptype: %d", __func__, usecase->stream.inout->out_app_type_cfg.app_type);
947 break;
Derek Chenf7092792017-05-23 12:23:53 -0400948 case PCM_HFP_CALL:
949 switch (usecase->id) {
950 case USECASE_AUDIO_HFP_SCO:
951 case USECASE_AUDIO_HFP_SCO_WB:
952 audio_extn_btsco_get_sample_rate(usecase->out_snd_device,
953 &usecase->out_app_type_cfg.sample_rate);
Derek Chend7307012019-11-12 12:10:41 -0500954 usecase->in_app_type_cfg.sample_rate = usecase->out_app_type_cfg.sample_rate;
Derek Chenf7092792017-05-23 12:23:53 -0400955 break;
956 case USECASE_AUDIO_HFP_SCO_DOWNLINK:
957 case USECASE_AUDIO_HFP_SCO_WB_DOWNLINK:
958 audio_extn_btsco_get_sample_rate(usecase->in_snd_device,
959 &usecase->in_app_type_cfg.sample_rate);
Derek Chend7307012019-11-12 12:10:41 -0500960 usecase->out_app_type_cfg.sample_rate = usecase->in_app_type_cfg.sample_rate;
Derek Chenf7092792017-05-23 12:23:53 -0400961 break;
962 default:
963 ALOGE("%s: usecase id (%d) not supported, use default sample rate",
964 __func__, usecase->id);
965 usecase->in_app_type_cfg.sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
966 usecase->out_app_type_cfg.sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
967 break;
968 }
969 /* update out_app_type_cfg */
970 usecase->out_app_type_cfg.bit_width =
971 platform_get_snd_device_bit_width(usecase->out_snd_device);
972 usecase->out_app_type_cfg.app_type =
973 platform_get_default_app_type_v2(adev->platform, PCM_PLAYBACK);
974 /* update in_app_type_cfg */
975 usecase->in_app_type_cfg.bit_width =
976 platform_get_snd_device_bit_width(usecase->in_snd_device);
977 usecase->in_app_type_cfg.app_type =
978 platform_get_default_app_type_v2(adev->platform, PCM_CAPTURE);
979 ALOGV("%s Selected apptype: playback %d capture %d",
980 __func__, usecase->out_app_type_cfg.app_type, usecase->in_app_type_cfg.app_type);
981 break;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530982 default:
983 ALOGE("%s: app type cfg not supported for usecase type (%d)",
984 __func__, usecase->type);
985 }
986}
987
Aalique Grahame22e49102018-12-18 14:23:57 -0800988static int set_stream_app_type_mixer_ctrl(struct audio_device *adev,
989 int pcm_device_id, int app_type,
990 int acdb_dev_id, int sample_rate,
991 int stream_type,
992 snd_device_t snd_device)
993{
994
995 char mixer_ctl_name[MAX_LENGTH_MIXER_CONTROL_IN_INT];
996 struct mixer_ctl *ctl;
997 int app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT], len = 0, rc = 0;
998 int snd_device_be_idx = -1;
999
1000 if (stream_type == PCM_PLAYBACK) {
1001 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
1002 "Audio Stream %d App Type Cfg", pcm_device_id);
1003 } else if (stream_type == PCM_CAPTURE) {
1004 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
1005 "Audio Stream Capture %d App Type Cfg", pcm_device_id);
1006 }
1007
1008 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
1009 if (!ctl) {
1010 ALOGE("%s: Could not get ctl for mixer cmd - %s",
1011 __func__, mixer_ctl_name);
1012 rc = -EINVAL;
1013 goto exit;
1014 }
1015 app_type_cfg[len++] = app_type;
1016 app_type_cfg[len++] = acdb_dev_id;
1017 app_type_cfg[len++] = sample_rate;
1018
1019 snd_device_be_idx = platform_get_snd_device_backend_index(snd_device);
1020 if (snd_device_be_idx > 0)
1021 app_type_cfg[len++] = snd_device_be_idx;
1022 ALOGV("%s: stream type %d app_type %d, acdb_dev_id %d "
1023 "sample rate %d, snd_device_be_idx %d",
1024 __func__, stream_type, app_type, acdb_dev_id, sample_rate,
1025 snd_device_be_idx);
1026 mixer_ctl_set_array(ctl, app_type_cfg, len);
1027
1028exit:
1029 return rc;
1030}
1031
1032static int audio_extn_utils_send_app_type_cfg_hfp(struct audio_device *adev,
1033 struct audio_usecase *usecase)
1034{
1035 int pcm_device_id, acdb_dev_id = 0, snd_device = usecase->out_snd_device;
1036 int32_t sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
1037 int app_type = 0, rc = 0;
Derek Chenf7092792017-05-23 12:23:53 -04001038 bool is_bus_dev_usecase = false;
Aalique Grahame22e49102018-12-18 14:23:57 -08001039
1040 ALOGV("%s", __func__);
1041
1042 if (usecase->type != PCM_HFP_CALL) {
Derek Chenf7092792017-05-23 12:23:53 -04001043 ALOGV("%s: not a HFP path, no need to cfg app type", __func__);
Aalique Grahame22e49102018-12-18 14:23:57 -08001044 rc = 0;
1045 goto exit_send_app_type_cfg;
1046 }
1047 if ((usecase->id != USECASE_AUDIO_HFP_SCO) &&
Derek Chenf7092792017-05-23 12:23:53 -04001048 (usecase->id != USECASE_AUDIO_HFP_SCO_WB) &&
1049 (usecase->id != USECASE_AUDIO_HFP_SCO_DOWNLINK) &&
1050 (usecase->id != USECASE_AUDIO_HFP_SCO_WB_DOWNLINK)) {
1051 ALOGV("%s: a usecase where app type cfg is not required", __func__);
Aalique Grahame22e49102018-12-18 14:23:57 -08001052 rc = 0;
1053 goto exit_send_app_type_cfg;
1054 }
1055
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08001056 if (compare_device_type(&usecase->device_list, AUDIO_DEVICE_OUT_BUS))
Derek Chenf7092792017-05-23 12:23:53 -04001057 is_bus_dev_usecase = true;
1058
Aalique Grahame22e49102018-12-18 14:23:57 -08001059 snd_device = usecase->out_snd_device;
1060 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_PLAYBACK);
1061
1062 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
1063 if (acdb_dev_id < 0) {
1064 ALOGE("%s: Couldn't get the acdb dev id", __func__);
1065 rc = -EINVAL;
1066 goto exit_send_app_type_cfg;
1067 }
1068
1069 if (usecase->type == PCM_HFP_CALL) {
1070
1071 /* config HFP session:1 playback path */
Derek Chenf7092792017-05-23 12:23:53 -04001072 if (is_bus_dev_usecase) {
1073 app_type = usecase->out_app_type_cfg.app_type;
1074 sample_rate= usecase->out_app_type_cfg.sample_rate;
1075 } else {
1076 snd_device = SND_DEVICE_NONE; // use legacy behavior
1077 app_type = platform_get_default_app_type_v2(adev->platform, PCM_PLAYBACK);
1078 sample_rate= CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
1079 }
Aalique Grahame22e49102018-12-18 14:23:57 -08001080 rc = set_stream_app_type_mixer_ctrl(adev, pcm_device_id, app_type,
1081 acdb_dev_id, sample_rate,
1082 PCM_PLAYBACK,
Derek Chenf7092792017-05-23 12:23:53 -04001083 snd_device);
Aalique Grahame22e49102018-12-18 14:23:57 -08001084 if (rc < 0)
1085 goto exit_send_app_type_cfg;
1086
1087 /* config HFP session:1 capture path */
Derek Chenf7092792017-05-23 12:23:53 -04001088 if (is_bus_dev_usecase) {
1089 snd_device = usecase->in_snd_device;
1090 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_CAPTURE);
1091 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
1092 if (acdb_dev_id < 0) {
1093 ALOGE("%s: Couldn't get the acdb dev id", __func__);
1094 rc = -EINVAL;
1095 goto exit_send_app_type_cfg;
1096 }
1097 app_type = usecase->in_app_type_cfg.app_type;
1098 sample_rate= usecase->in_app_type_cfg.sample_rate;
1099 } else {
1100 snd_device = SND_DEVICE_NONE; // use legacy behavior
1101 app_type = platform_get_default_app_type_v2(adev->platform, PCM_CAPTURE);
1102 }
Aalique Grahame22e49102018-12-18 14:23:57 -08001103 rc = set_stream_app_type_mixer_ctrl(adev, pcm_device_id, app_type,
1104 acdb_dev_id, sample_rate,
1105 PCM_CAPTURE,
Derek Chenf7092792017-05-23 12:23:53 -04001106 snd_device);
Aalique Grahame22e49102018-12-18 14:23:57 -08001107 if (rc < 0)
1108 goto exit_send_app_type_cfg;
1109
Derek Chenf7092792017-05-23 12:23:53 -04001110 if (is_bus_dev_usecase)
1111 goto exit_send_app_type_cfg;
1112
Aalique Grahame22e49102018-12-18 14:23:57 -08001113 /* config HFP session:2 capture path */
1114 pcm_device_id = HFP_ASM_RX_TX;
1115 snd_device = usecase->in_snd_device;
1116 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
1117 if (acdb_dev_id <= 0) {
1118 ALOGE("%s: Couldn't get the acdb dev id", __func__);
1119 rc = -EINVAL;
1120 goto exit_send_app_type_cfg;
1121 }
1122 app_type = platform_get_default_app_type_v2(adev->platform, PCM_CAPTURE);
1123 rc = set_stream_app_type_mixer_ctrl(adev, pcm_device_id, app_type,
1124 acdb_dev_id, sample_rate, PCM_CAPTURE,
1125 SND_DEVICE_NONE);
1126 if (rc < 0)
1127 goto exit_send_app_type_cfg;
1128
1129 /* config HFP session:2 playback path */
1130 app_type = platform_get_default_app_type_v2(adev->platform, PCM_PLAYBACK);
1131 rc = set_stream_app_type_mixer_ctrl(adev, pcm_device_id, app_type,
1132 acdb_dev_id, sample_rate,
1133 PCM_PLAYBACK, SND_DEVICE_NONE);
1134 if (rc < 0)
1135 goto exit_send_app_type_cfg;
1136 }
1137
1138 rc = 0;
1139exit_send_app_type_cfg:
1140 return rc;
1141}
1142
Zhou Song06761dd2019-04-28 18:08:17 +08001143int audio_extn_utils_get_app_sample_rate_for_device(
1144 struct audio_device *adev,
1145 struct audio_usecase *usecase, int snd_device)
1146{
1147 char value[PROPERTY_VALUE_MAX] = {0};
1148 int sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
1149
1150 if ((usecase->type == PCM_PLAYBACK) && (usecase->stream.out != NULL)) {
1151 property_get("vendor.audio.playback.mch.downsample",value,"");
1152 if (!strncmp("true", value, sizeof("true"))) {
1153 if ((popcount(usecase->stream.out->channel_mask) > 2) &&
1154 (usecase->stream.out->app_type_cfg.sample_rate > CODEC_BACKEND_DEFAULT_SAMPLE_RATE) &&
1155 !(usecase->stream.out->flags &
1156 (audio_output_flags_t)AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH))
1157 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
1158 }
1159
1160 if (usecase->id == USECASE_AUDIO_PLAYBACK_VOIP) {
1161 usecase->stream.out->app_type_cfg.sample_rate = usecase->stream.out->sample_rate;
Zhou Song06761dd2019-04-28 18:08:17 +08001162 } else if ((snd_device == SND_DEVICE_OUT_HDMI ||
1163 snd_device == SND_DEVICE_OUT_USB_HEADSET ||
1164 snd_device == SND_DEVICE_OUT_DISPLAY_PORT) &&
1165 (usecase->stream.out->sample_rate >= OUTPUT_SAMPLING_RATE_44100)) {
1166 /*
1167 * To best utlize DSP, check if the stream sample rate is supported/multiple of
1168 * configured device sample rate, if not update the COPP rate to be equal to the
1169 * device sample rate, else open COPP at stream sample rate
1170 */
1171 platform_check_and_update_copp_sample_rate(adev->platform, snd_device,
1172 usecase->stream.out->sample_rate,
1173 &usecase->stream.out->app_type_cfg.sample_rate);
1174 } else if (((snd_device != SND_DEVICE_OUT_HEADPHONES_44_1 &&
1175 !audio_is_this_native_usecase(usecase)) &&
1176 usecase->stream.out->sample_rate == OUTPUT_SAMPLING_RATE_44100) ||
1177 (usecase->stream.out->sample_rate < OUTPUT_SAMPLING_RATE_44100)) {
1178 /* Reset to default if no native stream is active*/
1179 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Lakshman Chaluvarajue12dfa12021-01-27 23:22:58 +05301180 } else if (snd_device == SND_DEVICE_OUT_BT_A2DP) {
Zhou Song06761dd2019-04-28 18:08:17 +08001181 /*
1182 * For a2dp playback get encoder sampling rate and set copp sampling rate,
1183 * for bit width use the stream param only.
1184 */
1185 audio_extn_a2dp_get_enc_sample_rate(&usecase->stream.out->app_type_cfg.sample_rate);
1186 ALOGI("%s using %d sample rate rate for A2DP CoPP",
1187 __func__, usecase->stream.out->app_type_cfg.sample_rate);
Lakshman Chaluvarajue12dfa12021-01-27 23:22:58 +05301188 } else if (compare_device_type(&usecase->stream.out->device_list,
1189 AUDIO_DEVICE_OUT_SPEAKER)) {
1190 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Zhou Song06761dd2019-04-28 18:08:17 +08001191 }
1192 audio_extn_btsco_get_sample_rate(snd_device, &usecase->stream.out->app_type_cfg.sample_rate);
1193 sample_rate = usecase->stream.out->app_type_cfg.sample_rate;
1194
1195 if (((usecase->stream.out->format == AUDIO_FORMAT_E_AC3) ||
1196 (usecase->stream.out->format == AUDIO_FORMAT_E_AC3_JOC) ||
1197 (usecase->stream.out->format == AUDIO_FORMAT_DOLBY_TRUEHD))
1198 && audio_extn_passthru_is_passthrough_stream(usecase->stream.out)
1199 && !audio_extn_passthru_is_convert_supported(adev, usecase->stream.out)) {
1200 sample_rate = sample_rate * 4;
1201 if (sample_rate > HDMI_PASSTHROUGH_MAX_SAMPLE_RATE)
1202 sample_rate = HDMI_PASSTHROUGH_MAX_SAMPLE_RATE;
1203 }
1204 } else if (usecase->type == PCM_CAPTURE) {
1205 if (usecase->stream.in != NULL) {
1206 if (usecase->id == USECASE_AUDIO_RECORD_VOIP)
1207 usecase->stream.in->app_type_cfg.sample_rate = usecase->stream.in->sample_rate;
1208 if (voice_is_in_call_rec_stream(usecase->stream.in)) {
1209 audio_extn_btsco_get_sample_rate(usecase->in_snd_device,
1210 &usecase->stream.in->app_type_cfg.sample_rate);
1211 } else {
1212 audio_extn_btsco_get_sample_rate(snd_device,
1213 &usecase->stream.in->app_type_cfg.sample_rate);
1214 }
1215 sample_rate = usecase->stream.in->app_type_cfg.sample_rate;
1216 } else if (usecase->id == USECASE_AUDIO_SPKR_CALIB_TX) {
Sujin Panickerb6d76262019-09-09 10:55:38 +05301217 if ((property_get("vendor.audio.spkr_prot.tx.sampling_rate", value, NULL) > 0))
1218 sample_rate = atoi(value);
1219 else
1220 sample_rate = SAMPLE_RATE_8000;
Zhou Song06761dd2019-04-28 18:08:17 +08001221 }
1222 } else if (usecase->type == TRANSCODE_LOOPBACK_RX) {
1223 sample_rate = usecase->stream.inout->out_config.sample_rate;
1224 }
1225 return sample_rate;
1226}
1227
Siena Richard7c2db772016-12-21 11:32:34 -08001228static int send_app_type_cfg_for_device(struct audio_device *adev,
1229 struct audio_usecase *usecase,
1230 int split_snd_device)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001231{
1232 char mixer_ctl_name[MAX_LENGTH_MIXER_CONTROL_IN_INT];
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301233 size_t app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT] = {0};
1234 int len = 0, rc;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001235 struct mixer_ctl *ctl;
Siena Richard7c2db772016-12-21 11:32:34 -08001236 int pcm_device_id = 0, acdb_dev_id, app_type;
1237 int snd_device = split_snd_device, snd_device_be_idx = -1;
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +05301238 int32_t sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Varun Balaraje49253e2017-07-06 19:48:56 +05301239 struct streams_io_cfg *s_info = NULL;
1240 struct listnode *node = NULL;
Nikhil Laturkarac4a7492017-08-31 18:27:19 +05301241 int bd_app_type = 0;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001242
Siena Richard7c2db772016-12-21 11:32:34 -08001243 ALOGV("%s: usecase->out_snd_device %s, usecase->in_snd_device %s, split_snd_device %s",
1244 __func__, platform_get_snd_device_name(usecase->out_snd_device),
1245 platform_get_snd_device_name(usecase->in_snd_device),
1246 platform_get_snd_device_name(split_snd_device));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001247
Siddartha Shaik343abc62017-08-08 11:15:25 +05301248 if (usecase->type != PCM_PLAYBACK && usecase->type != PCM_CAPTURE &&
Surendar Karka93cd25a2018-08-28 14:21:37 +05301249 usecase->type != TRANSCODE_LOOPBACK_RX) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301250 ALOGE("%s: not a playback/capture path, no need to cfg app type", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001251 rc = 0;
1252 goto exit_send_app_type_cfg;
1253 }
1254 if ((usecase->id != USECASE_AUDIO_PLAYBACK_DEEP_BUFFER) &&
Vignesh Kulothungan0a2eff02019-07-11 16:30:13 -07001255 (usecase->id != USECASE_AUDIO_PLAYBACK_WITH_HAPTICS) &&
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001256 (usecase->id != USECASE_AUDIO_PLAYBACK_LOW_LATENCY) &&
1257 (usecase->id != USECASE_AUDIO_PLAYBACK_MULTI_CH) &&
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -08001258 (usecase->id != USECASE_AUDIO_PLAYBACK_ULL) &&
Vikram Panduranga93f080e2017-06-07 18:16:14 -07001259 (usecase->id != USECASE_AUDIO_PLAYBACK_VOIP) &&
Surendar Karka93cd25a2018-08-28 14:21:37 +05301260 (usecase->id != USECASE_AUDIO_TRANSCODE_LOOPBACK_RX) &&
Varun Balaraje49253e2017-07-06 19:48:56 +05301261 (!is_interactive_usecase(usecase->id)) &&
Srikanth Uyyala9d551402015-08-25 16:03:42 +05301262 (!is_offload_usecase(usecase->id)) &&
Derek Chenf6318be2017-06-12 17:16:24 -04001263 (usecase->type != PCM_CAPTURE) &&
1264 (!audio_extn_auto_hal_is_bus_device_usecase(usecase->id))) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301265 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 -07001266 rc = 0;
1267 goto exit_send_app_type_cfg;
1268 }
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -08001269
1270 //if VR is active then only send the mixer control
1271 if (usecase->id == USECASE_AUDIO_PLAYBACK_ULL && !audio_is_vr_mode_on(adev)) {
1272 ALOGI("ULL doesnt need sending app type cfg, returning");
1273 rc = 0;
1274 goto exit_send_app_type_cfg;
1275 }
1276
Surendar Karka93cd25a2018-08-28 14:21:37 +05301277 if (usecase->type == PCM_PLAYBACK || usecase->type == TRANSCODE_LOOPBACK_RX) {
Ben Romberger1fafdde2015-09-09 19:43:15 -07001278 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_PLAYBACK);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301279 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
1280 "Audio Stream %d App Type Cfg", pcm_device_id);
Ben Romberger1fafdde2015-09-09 19:43:15 -07001281 } else if (usecase->type == PCM_CAPTURE) {
Ben Romberger1fafdde2015-09-09 19:43:15 -07001282 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_CAPTURE);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301283 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
1284 "Audio Stream Capture %d App Type Cfg", pcm_device_id);
Srikanth Uyyala9d551402015-08-25 16:03:42 +05301285 }
Siena Richard7c2db772016-12-21 11:32:34 -08001286
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001287 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
1288 if (!ctl) {
1289 ALOGE("%s: Could not get ctl for mixer cmd - %s", __func__,
1290 mixer_ctl_name);
1291 rc = -EINVAL;
1292 goto exit_send_app_type_cfg;
1293 }
Aditya Bavanari701a6992017-03-30 19:17:16 +05301294 snd_device = platform_get_spkr_prot_snd_device(snd_device);
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301295 if (voice_is_in_call_rec_stream(usecase->stream.in) && usecase->type == PCM_CAPTURE) {
1296 snd_device_t voice_device = voice_get_incall_rec_snd_device(usecase->in_snd_device);
1297 acdb_dev_id = platform_get_snd_device_acdb_id(voice_device);
1298 ALOGV("acdb id for voice call use case %d", acdb_dev_id);
1299 } else {
1300 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
1301 }
Rohit Kumar1181b292017-01-31 18:07:17 +05301302 if (acdb_dev_id <= 0) {
1303 ALOGE("%s: Couldn't get the acdb dev id", __func__);
1304 rc = -EINVAL;
1305 goto exit_send_app_type_cfg;
1306 }
1307
Siena Richard7c2db772016-12-21 11:32:34 -08001308 snd_device_be_idx = platform_get_snd_device_backend_index(snd_device);
1309 if (snd_device_be_idx < 0) {
1310 ALOGE("%s: Couldn't get the backend index for snd device %s ret=%d",
1311 __func__, platform_get_snd_device_name(snd_device),
1312 snd_device_be_idx);
1313 }
1314
Zhou Song06761dd2019-04-28 18:08:17 +08001315 sample_rate = audio_extn_utils_get_app_sample_rate_for_device(adev, usecase, snd_device);
1316
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301317 if ((usecase->type == PCM_PLAYBACK) && (usecase->stream.out != NULL)) {
Varun Balaraje49253e2017-07-06 19:48:56 +05301318 /* Interactive streams are supported with only direct app type id.
1319 * Get Direct profile app type and use it for interactive streams
1320 */
1321 list_for_each(node, &adev->streams_output_cfg_list) {
1322 s_info = node_to_item(node, struct streams_io_cfg, list);
Aalique Grahame5cd12ff2017-10-19 10:57:00 -07001323 if (s_info->flags.out_flags == (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_BD |
Nikhil Laturkarac4a7492017-08-31 18:27:19 +05301324 AUDIO_OUTPUT_FLAG_DIRECT_PCM |
1325 AUDIO_OUTPUT_FLAG_DIRECT))
1326 bd_app_type = s_info->app_type_cfg.app_type;
Varun Balaraje49253e2017-07-06 19:48:56 +05301327 }
Aalique Grahame5cd12ff2017-10-19 10:57:00 -07001328 if (usecase->stream.out->flags == (audio_output_flags_t)AUDIO_OUTPUT_FLAG_INTERACTIVE)
Nikhil Laturkarac4a7492017-08-31 18:27:19 +05301329 app_type = bd_app_type;
Varun Balaraje49253e2017-07-06 19:48:56 +05301330 else
1331 app_type = usecase->stream.out->app_type_cfg.app_type;
Siena Richard7c2db772016-12-21 11:32:34 -08001332 app_type_cfg[len++] = app_type;
Mingming Yin21854652016-04-13 11:54:02 -07001333 app_type_cfg[len++] = acdb_dev_id;
Naresh Tanniru3a406772017-05-10 13:09:05 -07001334 app_type_cfg[len++] = sample_rate;
1335
Siena Richard7c2db772016-12-21 11:32:34 -08001336 if (snd_device_be_idx > 0)
1337 app_type_cfg[len++] = snd_device_be_idx;
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301338
Siena Richard7c2db772016-12-21 11:32:34 -08001339 ALOGI("%s PLAYBACK app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
1340 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301341
1342 } else if ((usecase->type == PCM_CAPTURE) && (usecase->stream.in != NULL)) {
Siena Richard7c2db772016-12-21 11:32:34 -08001343 app_type = usecase->stream.in->app_type_cfg.app_type;
1344 app_type_cfg[len++] = app_type;
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301345 app_type_cfg[len++] = acdb_dev_id;
Siena Richard7c2db772016-12-21 11:32:34 -08001346 app_type_cfg[len++] = sample_rate;
1347 if (snd_device_be_idx > 0)
1348 app_type_cfg[len++] = snd_device_be_idx;
1349 ALOGI("%s CAPTURE app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
1350 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301351 } else {
Siena Richard7c2db772016-12-21 11:32:34 -08001352 app_type = platform_get_default_app_type_v2(adev->platform, usecase->type);
Surendar Karka93cd25a2018-08-28 14:21:37 +05301353 if(usecase->type == TRANSCODE_LOOPBACK_RX) {
Siddartha Shaik343abc62017-08-08 11:15:25 +05301354 app_type = usecase->stream.inout->out_app_type_cfg.app_type;
1355 }
Siena Richard7c2db772016-12-21 11:32:34 -08001356 app_type_cfg[len++] = app_type;
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301357 app_type_cfg[len++] = acdb_dev_id;
1358 app_type_cfg[len++] = sample_rate;
Siena Richard7c2db772016-12-21 11:32:34 -08001359 if (snd_device_be_idx > 0)
1360 app_type_cfg[len++] = snd_device_be_idx;
1361 ALOGI("%s default app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
1362 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Ashish Jainc02430d2016-01-04 10:42:43 +05301363 }
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301364
Siddartha Shaik343abc62017-08-08 11:15:25 +05301365 if(ctl)
1366 mixer_ctl_set_array(ctl, app_type_cfg, len);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001367 rc = 0;
1368exit_send_app_type_cfg:
1369 return rc;
1370}
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001371
Deeraj Soman14230922019-01-30 16:39:30 +05301372static int audio_extn_utils_check_input_parameters(uint32_t sample_rate,
1373 audio_format_t format,
1374 int channel_count)
1375{
1376 int ret = 0;
1377
1378 if (((format != AUDIO_FORMAT_PCM_16_BIT) && (format != AUDIO_FORMAT_PCM_8_24_BIT) &&
1379 (format != AUDIO_FORMAT_PCM_24_BIT_PACKED) && (format != AUDIO_FORMAT_PCM_32_BIT) &&
1380 (format != AUDIO_FORMAT_PCM_FLOAT)) &&
1381 !voice_extn_compress_voip_is_format_supported(format) &&
1382 !audio_extn_compr_cap_format_supported(format) &&
1383 !audio_extn_cin_format_supported(format))
1384 ret = -EINVAL;
1385
1386 switch (channel_count) {
1387 case 1:
1388 case 2:
1389 case 3:
1390 case 4:
1391 case 6:
1392 case 8:
1393 break;
1394 default:
1395 ret = -EINVAL;
1396 }
1397
1398 switch (sample_rate) {
1399 case 8000:
1400 case 11025:
1401 case 12000:
1402 case 16000:
1403 case 22050:
1404 case 24000:
1405 case 32000:
1406 case 44100:
1407 case 48000:
1408 case 88200:
1409 case 96000:
1410 case 176400:
1411 case 192000:
1412 break;
1413 default:
1414 ret = -EINVAL;
1415 }
1416
1417 return ret;
1418}
1419
1420static inline uint32_t audio_extn_utils_nearest_multiple(uint32_t num, uint32_t multiplier)
1421{
1422 uint32_t remainder = 0;
1423
1424 if (!multiplier)
1425 return num;
1426
1427 remainder = num % multiplier;
1428 if (remainder)
1429 num += (multiplier - remainder);
1430
1431 return num;
1432}
1433
1434static inline uint32_t audio_extn_utils_lcm(uint32_t num1, uint32_t num2)
1435{
1436 uint32_t high = num1, low = num2, temp = 0;
1437
1438 if (!num1 || !num2)
1439 return 0;
1440
1441 if (num1 < num2) {
1442 high = num2;
1443 low = num1;
1444 }
1445
1446 while (low != 0) {
1447 temp = low;
1448 low = high % low;
1449 high = temp;
1450 }
1451 return (num1 * num2)/high;
1452}
1453
Siena Richard7c2db772016-12-21 11:32:34 -08001454int audio_extn_utils_send_app_type_cfg(struct audio_device *adev,
1455 struct audio_usecase *usecase)
1456{
1457 int i, num_devices = 0;
1458 snd_device_t new_snd_devices[SND_DEVICE_OUT_END] = {0};
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301459 snd_device_t in_snd_device = usecase->in_snd_device;
Siena Richard7c2db772016-12-21 11:32:34 -08001460 int rc = 0;
1461
Aalique Grahame22e49102018-12-18 14:23:57 -08001462 if (usecase->type == PCM_HFP_CALL) {
1463 return audio_extn_utils_send_app_type_cfg_hfp(adev, usecase);
1464 }
1465
Siena Richard7c2db772016-12-21 11:32:34 -08001466 switch (usecase->type) {
1467 case PCM_PLAYBACK:
Surendar Karka93cd25a2018-08-28 14:21:37 +05301468 case TRANSCODE_LOOPBACK_RX:
Siena Richard7c2db772016-12-21 11:32:34 -08001469 ALOGD("%s: usecase->out_snd_device %s",
1470 __func__, platform_get_snd_device_name(usecase->out_snd_device));
1471 /* check for out combo device */
1472 if (platform_split_snd_device(adev->platform,
1473 usecase->out_snd_device,
1474 &num_devices, new_snd_devices)) {
1475 new_snd_devices[0] = usecase->out_snd_device;
1476 num_devices = 1;
1477 }
1478 break;
1479 case PCM_CAPTURE:
1480 ALOGD("%s: usecase->in_snd_device %s",
1481 __func__, platform_get_snd_device_name(usecase->in_snd_device));
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301482 if (voice_is_in_call_rec_stream(usecase->stream.in)) {
1483 in_snd_device = voice_get_incall_rec_backend_device(usecase->stream.in);
1484 }
Siena Richard7c2db772016-12-21 11:32:34 -08001485 /* check for in combo device */
1486 if (platform_split_snd_device(adev->platform,
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301487 in_snd_device,
Siena Richard7c2db772016-12-21 11:32:34 -08001488 &num_devices, new_snd_devices)) {
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301489 new_snd_devices[0] = in_snd_device;
Siena Richard7c2db772016-12-21 11:32:34 -08001490 num_devices = 1;
1491 }
1492 break;
1493 default:
1494 ALOGI("%s: not a playback/capture path, no need to cfg app type", __func__);
1495 rc = 0;
1496 break;
1497 }
1498
1499 for (i = 0; i < num_devices; i++) {
1500 rc = send_app_type_cfg_for_device(adev, usecase, new_snd_devices[i]);
1501 if (rc)
1502 break;
1503 }
1504
1505 return rc;
1506}
1507
Preetam Singh Ranawat9519e9c2015-11-18 16:05:55 +05301508int read_line_from_file(const char *path, char *buf, size_t count)
1509{
1510 char * fgets_ret;
1511 FILE * fd;
1512 int rv;
1513
1514 fd = fopen(path, "r");
1515 if (fd == NULL)
1516 return -1;
1517
1518 fgets_ret = fgets(buf, (int)count, fd);
1519 if (NULL != fgets_ret) {
1520 rv = (int)strlen(buf);
1521 } else {
1522 rv = ferror(fd);
1523 }
1524 fclose(fd);
1525
1526 return rv;
1527}
1528
Ashish Jainf1eaa582016-05-23 20:54:24 +05301529/*Translates ALSA formats to AOSP PCM formats*/
1530audio_format_t alsa_format_to_hal(uint32_t alsa_format)
1531{
1532 audio_format_t format;
1533
1534 switch(alsa_format) {
1535 case SNDRV_PCM_FORMAT_S16_LE:
1536 format = AUDIO_FORMAT_PCM_16_BIT;
1537 break;
1538 case SNDRV_PCM_FORMAT_S24_3LE:
1539 format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
1540 break;
1541 case SNDRV_PCM_FORMAT_S24_LE:
1542 format = AUDIO_FORMAT_PCM_8_24_BIT;
1543 break;
1544 case SNDRV_PCM_FORMAT_S32_LE:
1545 format = AUDIO_FORMAT_PCM_32_BIT;
1546 break;
1547 default:
1548 ALOGW("Incorrect ALSA format");
1549 format = AUDIO_FORMAT_INVALID;
1550 }
1551 return format;
1552}
1553
1554/*Translates hal format (AOSP) to alsa formats*/
1555uint32_t hal_format_to_alsa(audio_format_t hal_format)
1556{
1557 uint32_t alsa_format;
1558
1559 switch (hal_format) {
1560 case AUDIO_FORMAT_PCM_32_BIT: {
1561 if (platform_supports_true_32bit())
1562 alsa_format = SNDRV_PCM_FORMAT_S32_LE;
1563 else
1564 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1565 }
1566 break;
1567 case AUDIO_FORMAT_PCM_8_BIT:
1568 alsa_format = SNDRV_PCM_FORMAT_S8;
1569 break;
1570 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
1571 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1572 break;
1573 case AUDIO_FORMAT_PCM_8_24_BIT: {
1574 if (platform_supports_true_32bit())
1575 alsa_format = SNDRV_PCM_FORMAT_S32_LE;
1576 else
1577 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1578 }
1579 break;
1580 case AUDIO_FORMAT_PCM_FLOAT:
1581 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1582 break;
1583 default:
1584 case AUDIO_FORMAT_PCM_16_BIT:
1585 alsa_format = SNDRV_PCM_FORMAT_S16_LE;
1586 break;
1587 }
1588 return alsa_format;
1589}
1590
Ashish Jain83a6cc22016-06-28 14:34:17 +05301591/*Translates PCM formats to AOSP formats*/
1592audio_format_t pcm_format_to_hal(uint32_t pcm_format)
1593{
1594 audio_format_t format = AUDIO_FORMAT_INVALID;
1595
1596 switch(pcm_format) {
1597 case PCM_FORMAT_S16_LE:
1598 format = AUDIO_FORMAT_PCM_16_BIT;
1599 break;
1600 case PCM_FORMAT_S24_3LE:
1601 format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
1602 break;
1603 case PCM_FORMAT_S24_LE:
1604 format = AUDIO_FORMAT_PCM_8_24_BIT;
1605 break;
1606 case PCM_FORMAT_S32_LE:
1607 format = AUDIO_FORMAT_PCM_32_BIT;
1608 break;
1609 default:
1610 ALOGW("Incorrect PCM format");
1611 format = AUDIO_FORMAT_INVALID;
1612 }
1613 return format;
1614}
1615
1616/*Translates hal format (AOSP) to alsa formats*/
1617uint32_t hal_format_to_pcm(audio_format_t hal_format)
1618{
1619 uint32_t pcm_format;
1620
1621 switch (hal_format) {
1622 case AUDIO_FORMAT_PCM_32_BIT:
1623 case AUDIO_FORMAT_PCM_8_24_BIT:
1624 case AUDIO_FORMAT_PCM_FLOAT: {
1625 if (platform_supports_true_32bit())
1626 pcm_format = PCM_FORMAT_S32_LE;
1627 else
1628 pcm_format = PCM_FORMAT_S24_3LE;
1629 }
1630 break;
1631 case AUDIO_FORMAT_PCM_8_BIT:
1632 pcm_format = PCM_FORMAT_S8;
1633 break;
1634 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
1635 pcm_format = PCM_FORMAT_S24_3LE;
1636 break;
1637 default:
1638 case AUDIO_FORMAT_PCM_16_BIT:
1639 pcm_format = PCM_FORMAT_S16_LE;
1640 break;
1641 }
1642 return pcm_format;
1643}
1644
Ashish Jainf1eaa582016-05-23 20:54:24 +05301645uint32_t get_alsa_fragment_size(uint32_t bytes_per_sample,
1646 uint32_t sample_rate,
Deeraj Soman65358ab2019-02-07 15:40:49 +05301647 uint32_t noOfChannels,
1648 int64_t duration_ms)
Ashish Jainf1eaa582016-05-23 20:54:24 +05301649{
1650 uint32_t fragment_size = 0;
1651 uint32_t pcm_offload_time = PCM_OFFLOAD_BUFFER_DURATION;
1652
Deeraj Soman65358ab2019-02-07 15:40:49 +05301653 if (duration_ms >= MIN_OFFLOAD_BUFFER_DURATION_MS && duration_ms <= MAX_OFFLOAD_BUFFER_DURATION_MS)
1654 pcm_offload_time = duration_ms;
1655
Ashish Jainf1eaa582016-05-23 20:54:24 +05301656 fragment_size = (pcm_offload_time
1657 * sample_rate
1658 * bytes_per_sample
1659 * noOfChannels)/1000;
1660 if (fragment_size < MIN_PCM_OFFLOAD_FRAGMENT_SIZE)
1661 fragment_size = MIN_PCM_OFFLOAD_FRAGMENT_SIZE;
1662 else if (fragment_size > MAX_PCM_OFFLOAD_FRAGMENT_SIZE)
1663 fragment_size = MAX_PCM_OFFLOAD_FRAGMENT_SIZE;
1664 /*To have same PCM samples for all channels, the buffer size requires to
1665 *be multiple of (number of channels * bytes per sample)
1666 *For writes to succeed, the buffer must be written at address which is multiple of 32
1667 */
Aalique Grahameb85f2d92017-10-16 17:53:23 -07001668 fragment_size = ALIGN(fragment_size, (bytes_per_sample * noOfChannels * 32));
Ashish Jainf1eaa582016-05-23 20:54:24 +05301669
1670 ALOGI("PCM offload Fragment size to %d bytes", fragment_size);
1671 return fragment_size;
1672}
1673
1674/* Calculates the fragment size required to configure compress session.
1675 * Based on the alsa format selected, decide if conversion is needed in
1676
1677 * HAL ( e.g. convert AUDIO_FORMAT_PCM_FLOAT input format to
1678 * AUDIO_FORMAT_PCM_24_BIT_PACKED before writing to the compress driver.
1679 */
1680void audio_extn_utils_update_direct_pcm_fragment_size(struct stream_out *out)
1681{
Ashish Jain83a6cc22016-06-28 14:34:17 +05301682 audio_format_t dst_format = out->hal_op_format;
1683 audio_format_t src_format = out->hal_ip_format;
Ashish Jainf1eaa582016-05-23 20:54:24 +05301684 uint32_t hal_op_bytes_per_sample = audio_bytes_per_sample(dst_format);
1685 uint32_t hal_ip_bytes_per_sample = audio_bytes_per_sample(src_format);
1686
1687 out->compr_config.fragment_size =
1688 get_alsa_fragment_size(hal_op_bytes_per_sample,
1689 out->sample_rate,
Deeraj Soman65358ab2019-02-07 15:40:49 +05301690 popcount(out->channel_mask),
1691 out->info.duration_us / 1000);
Ashish Jainf1eaa582016-05-23 20:54:24 +05301692
1693 if ((src_format != dst_format) &&
1694 hal_op_bytes_per_sample != hal_ip_bytes_per_sample) {
1695
Ashish Jain83a6cc22016-06-28 14:34:17 +05301696 out->hal_fragment_size =
Ashish Jainf1eaa582016-05-23 20:54:24 +05301697 ((out->compr_config.fragment_size * hal_ip_bytes_per_sample) /
1698 hal_op_bytes_per_sample);
1699 ALOGI("enable conversion hal_input_fragment_size is %d src_format %x dst_format %x",
Ashish Jain83a6cc22016-06-28 14:34:17 +05301700 out->hal_fragment_size, src_format, dst_format);
Ashish Jainf1eaa582016-05-23 20:54:24 +05301701 } else {
Ashish Jain83a6cc22016-06-28 14:34:17 +05301702 out->hal_fragment_size = out->compr_config.fragment_size;
Ashish Jainf1eaa582016-05-23 20:54:24 +05301703 }
1704}
1705
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301706/* converts pcm format 24_8 to 8_24 inplace */
1707size_t audio_extn_utils_convert_format_24_8_to_8_24(void *buf, size_t bytes)
1708{
1709 size_t i = 0;
1710 int *int_buf_stream = buf;
1711
1712 if ((bytes % 4) != 0) {
1713 ALOGE("%s: wrong inout buffer! ... is not 32 bit aligned ", __func__);
1714 return -EINVAL;
1715 }
1716
1717 for (; i < (bytes / 4); i++)
1718 int_buf_stream[i] >>= 8;
1719
1720 return bytes;
1721}
1722
Xiaojun Sang782e5b12020-06-29 21:13:06 +08001723#ifdef AUDIO_GKI_ENABLED
1724int get_snd_codec_id(audio_format_t format)
1725{
1726 int id = 0;
1727
1728 switch (format & AUDIO_FORMAT_MAIN_MASK) {
1729 case AUDIO_FORMAT_MP3:
1730 id = SND_AUDIOCODEC_MP3;
1731 break;
1732 case AUDIO_FORMAT_AAC:
1733 id = SND_AUDIOCODEC_AAC;
1734 break;
1735 case AUDIO_FORMAT_AAC_ADTS:
1736 id = SND_AUDIOCODEC_AAC;
1737 break;
1738 case AUDIO_FORMAT_AAC_LATM:
1739 id = SND_AUDIOCODEC_AAC;
1740 break;
1741 case AUDIO_FORMAT_PCM:
1742 id = SND_AUDIOCODEC_PCM;
1743 break;
1744 case AUDIO_FORMAT_FLAC:
1745 case AUDIO_FORMAT_ALAC:
1746 case AUDIO_FORMAT_APE:
1747 case AUDIO_FORMAT_VORBIS:
1748 case AUDIO_FORMAT_WMA:
1749 case AUDIO_FORMAT_WMA_PRO:
1750 case AUDIO_FORMAT_DSD:
1751 case AUDIO_FORMAT_APTX:
1752 id = SND_AUDIOCODEC_BESPOKE;
1753 break;
1754 case AUDIO_FORMAT_MP2:
1755 id = SND_AUDIOCODEC_MP2;
1756 break;
1757 case AUDIO_FORMAT_AC3:
1758 id = SND_AUDIOCODEC_AC3;
1759 break;
1760 case AUDIO_FORMAT_E_AC3:
1761 case AUDIO_FORMAT_E_AC3_JOC:
1762 id = SND_AUDIOCODEC_EAC3;
1763 break;
1764 case AUDIO_FORMAT_DTS:
1765 case AUDIO_FORMAT_DTS_HD:
1766 id = SND_AUDIOCODEC_DTS;
1767 break;
1768 case AUDIO_FORMAT_DOLBY_TRUEHD:
1769 id = SND_AUDIOCODEC_TRUEHD;
1770 break;
1771 case AUDIO_FORMAT_IEC61937:
1772 id = SND_AUDIOCODEC_IEC61937;
1773 break;
1774 default:
1775 ALOGE("%s: Unsupported audio format :%x", __func__, format);
1776 }
1777
1778 return id;
1779}
1780#else
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301781int get_snd_codec_id(audio_format_t format)
1782{
1783 int id = 0;
1784
1785 switch (format & AUDIO_FORMAT_MAIN_MASK) {
1786 case AUDIO_FORMAT_MP3:
1787 id = SND_AUDIOCODEC_MP3;
1788 break;
1789 case AUDIO_FORMAT_AAC:
1790 id = SND_AUDIOCODEC_AAC;
1791 break;
1792 case AUDIO_FORMAT_AAC_ADTS:
1793 id = SND_AUDIOCODEC_AAC;
1794 break;
Arun Kumar Dasari3b174182016-12-27 13:01:14 +05301795 case AUDIO_FORMAT_AAC_LATM:
1796 id = SND_AUDIOCODEC_AAC;
1797 break;
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301798 case AUDIO_FORMAT_PCM:
1799 id = SND_AUDIOCODEC_PCM;
1800 break;
1801 case AUDIO_FORMAT_FLAC:
1802 id = SND_AUDIOCODEC_FLAC;
1803 break;
1804 case AUDIO_FORMAT_ALAC:
1805 id = SND_AUDIOCODEC_ALAC;
1806 break;
1807 case AUDIO_FORMAT_APE:
1808 id = SND_AUDIOCODEC_APE;
1809 break;
1810 case AUDIO_FORMAT_VORBIS:
1811 id = SND_AUDIOCODEC_VORBIS;
1812 break;
1813 case AUDIO_FORMAT_WMA:
1814 id = SND_AUDIOCODEC_WMA;
1815 break;
1816 case AUDIO_FORMAT_WMA_PRO:
1817 id = SND_AUDIOCODEC_WMA_PRO;
1818 break;
Satish Babu Patakokila0c313922016-12-08 12:07:08 +05301819 case AUDIO_FORMAT_MP2:
1820 id = SND_AUDIOCODEC_MP2;
1821 break;
Satish Babu Patakokila915ecba2017-01-10 17:43:56 +05301822 case AUDIO_FORMAT_AC3:
1823 id = SND_AUDIOCODEC_AC3;
1824 break;
1825 case AUDIO_FORMAT_E_AC3:
1826 case AUDIO_FORMAT_E_AC3_JOC:
1827 id = SND_AUDIOCODEC_EAC3;
1828 break;
1829 case AUDIO_FORMAT_DTS:
1830 case AUDIO_FORMAT_DTS_HD:
1831 id = SND_AUDIOCODEC_DTS;
1832 break;
Ben Romberger1aaaf862017-04-06 17:49:46 -07001833 case AUDIO_FORMAT_DOLBY_TRUEHD:
1834 id = SND_AUDIOCODEC_TRUEHD;
1835 break;
Naresh Tanniru928f0862017-04-07 16:44:23 -07001836 case AUDIO_FORMAT_IEC61937:
1837 id = SND_AUDIOCODEC_IEC61937;
1838 break;
Preetam Singh Ranawat4277a5a2017-01-18 19:02:24 +05301839 case AUDIO_FORMAT_DSD:
1840 id = SND_AUDIOCODEC_DSD;
1841 break;
Dhanalakshmi Siddani18737932016-11-29 17:33:17 +05301842 case AUDIO_FORMAT_APTX:
1843 id = SND_AUDIOCODEC_APTX;
1844 break;
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301845 default:
1846 ALOGE("%s: Unsupported audio format :%x", __func__, format);
1847 }
1848
1849 return id;
1850}
Xiaojun Sang782e5b12020-06-29 21:13:06 +08001851#endif
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301852
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001853void audio_extn_utils_send_audio_calibration(struct audio_device *adev,
1854 struct audio_usecase *usecase)
1855{
1856 int type = usecase->type;
1857
Dhananjay Kumar14245982017-01-16 20:21:00 +05301858 if (type == PCM_PLAYBACK && usecase->stream.out != NULL) {
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05301859 platform_send_audio_calibration(adev->platform, usecase,
Zhou Song06761dd2019-04-28 18:08:17 +08001860 usecase->stream.out->app_type_cfg.app_type);
Dhananjay Kumar14245982017-01-16 20:21:00 +05301861 } else if (type == PCM_CAPTURE && usecase->stream.in != NULL) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301862 platform_send_audio_calibration(adev->platform, usecase,
Zhou Song06761dd2019-04-28 18:08:17 +08001863 usecase->stream.in->app_type_cfg.app_type);
1864 } else if ((type == PCM_HFP_CALL) || (type == PCM_CAPTURE) ||
1865 (type == TRANSCODE_LOOPBACK_RX && usecase->stream.inout != NULL)) {
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05301866 platform_send_audio_calibration(adev->platform, usecase,
Zhou Song06761dd2019-04-28 18:08:17 +08001867 platform_get_default_app_type_v2(adev->platform, usecase->type));
Vidyakumar Athotae57f6002017-03-01 13:13:16 -08001868 } else {
1869 /* No need to send audio calibration for voice and voip call usecases */
1870 if ((type != VOICE_CALL) && (type != VOIP_CALL))
1871 ALOGW("%s: No audio calibration for usecase type = %d", __func__, type);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001872 }
1873}
1874
Ben Rombergera04fabc2014-11-14 12:16:03 -08001875// Base64 Encode and Decode
1876// Not all features supported. This must be used only with following conditions.
1877// Decode Modes: Support with and without padding
1878// CRLF not handling. So no CRLF in string to decode.
1879// Encode Modes: Supports only padding
1880int b64decode(char *inp, int ilen, uint8_t* outp)
1881{
1882 int i, j, k, ii, num;
1883 int rem, pcnt;
1884 uint32_t res=0;
1885 uint8_t getIndex[MAX_BASEINDEX_LEN];
1886 uint8_t tmp, cflag;
1887
1888 if(inp == NULL || outp == NULL || ilen <= 0) {
1889 ALOGE("[%s] received NULL pointer or zero length",__func__);
1890 return -1;
1891 }
1892
1893 memset(getIndex, MAX_BASEINDEX_LEN-1, sizeof(getIndex));
1894 for(i=0;i<BASE_TABLE_SIZE;i++) {
1895 getIndex[(uint8_t)bTable[i]] = (uint8_t)i;
1896 }
1897 getIndex[(uint8_t)'=']=0;
1898
1899 j=0;k=0;
1900 num = ilen/4;
1901 rem = ilen%4;
1902 if(rem==0)
1903 num = num-1;
1904 cflag=0;
1905 for(i=0; i<num; i++) {
1906 res=0;
1907 for(ii=0;ii<4;ii++) {
1908 res = res << 6;
1909 tmp = getIndex[(uint8_t)inp[j++]];
1910 res = res | tmp;
1911 cflag = cflag | tmp;
1912 }
1913 outp[k++] = (res >> 16)&0xFF;
1914 outp[k++] = (res >> 8)&0xFF;
1915 outp[k++] = res & 0xFF;
1916 }
1917
1918 // Handle last bytes special
1919 pcnt=0;
1920 if(rem == 0) {
1921 //With padding or full data
1922 res = 0;
1923 for(ii=0;ii<4;ii++) {
1924 if(inp[j] == '=')
1925 pcnt++;
1926 res = res << 6;
1927 tmp = getIndex[(uint8_t)inp[j++]];
1928 res = res | tmp;
1929 cflag = cflag | tmp;
1930 }
1931 outp[k++] = res >> 16;
1932 if(pcnt == 2)
1933 goto done;
1934 outp[k++] = (res>>8)&0xFF;
1935 if(pcnt == 1)
1936 goto done;
1937 outp[k++] = res&0xFF;
1938 } else {
1939 //without padding
1940 res = 0;
1941 for(i=0;i<rem;i++) {
1942 res = res << 6;
1943 tmp = getIndex[(uint8_t)inp[j++]];
1944 res = res | tmp;
1945 cflag = cflag | tmp;
1946 }
1947 for(i=rem;i<4;i++) {
1948 res = res << 6;
1949 pcnt++;
1950 }
1951 outp[k++] = res >> 16;
1952 if(pcnt == 2)
1953 goto done;
1954 outp[k++] = (res>>8)&0xFF;
1955 if(pcnt == 1)
1956 goto done;
1957 outp[k++] = res&0xFF;
1958 }
1959done:
1960 if(cflag == 0xFF) {
1961 ALOGE("[%s] base64 decode failed. Invalid character found %s",
1962 __func__, inp);
1963 return 0;
1964 }
1965 return k;
1966}
1967
1968int b64encode(uint8_t *inp, int ilen, char* outp)
1969{
1970 int i,j,k, num;
1971 int rem=0;
1972 uint32_t res=0;
1973
1974 if(inp == NULL || outp == NULL || ilen<=0) {
1975 ALOGE("[%s] received NULL pointer or zero input length",__func__);
1976 return -1;
1977 }
1978
1979 num = ilen/3;
1980 rem = ilen%3;
1981 j=0;k=0;
1982 for(i=0; i<num; i++) {
1983 //prepare index
1984 res = inp[j++]<<16;
1985 res = res | inp[j++]<<8;
1986 res = res | inp[j++];
1987 //get output map from index
1988 outp[k++] = (char) bTable[(res>>18)&0x3F];
1989 outp[k++] = (char) bTable[(res>>12)&0x3F];
1990 outp[k++] = (char) bTable[(res>>6)&0x3F];
1991 outp[k++] = (char) bTable[res&0x3F];
1992 }
1993
1994 switch(rem) {
1995 case 1:
1996 res = inp[j++]<<16;
1997 outp[k++] = (char) bTable[res>>18];
1998 outp[k++] = (char) bTable[(res>>12)&0x3F];
1999 //outp[k++] = '=';
2000 //outp[k++] = '=';
2001 break;
2002 case 2:
2003 res = inp[j++]<<16;
2004 res = res | inp[j++]<<8;
2005 outp[k++] = (char) bTable[res>>18];
2006 outp[k++] = (char) bTable[(res>>12)&0x3F];
2007 outp[k++] = (char) bTable[(res>>6)&0x3F];
2008 //outp[k++] = '=';
2009 break;
2010 default:
2011 break;
2012 }
Ben Rombergera04fabc2014-11-14 12:16:03 -08002013 outp[k] = '\0';
2014 return k;
2015}
Ashish Jain81eb2a82015-05-13 10:52:34 +05302016
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05302017
2018int audio_extn_utils_get_codec_version(const char *snd_card_name,
2019 int card_num,
2020 char *codec_version)
2021{
2022 char procfs_path[50];
2023 FILE *fp;
2024
2025 if (strstr(snd_card_name, "tasha")) {
2026 snprintf(procfs_path, sizeof(procfs_path),
2027 "/proc/asound/card%d/codecs/tasha/version", card_num);
2028 if ((fp = fopen(procfs_path, "r")) != NULL) {
2029 fgets(codec_version, CODEC_VERSION_MAX_LENGTH, fp);
2030 fclose(fp);
2031 } else {
2032 ALOGE("%s: ERROR. cannot open %s", __func__, procfs_path);
2033 return -ENOENT;
2034 }
2035 ALOGD("%s: codec version %s", __func__, codec_version);
2036 }
2037
2038 return 0;
2039}
2040
Preetam Singh Ranawat9d0d8e42019-07-15 12:27:25 +05302041int audio_extn_utils_get_codec_variant(int card_num,
2042 char *codec_variant)
2043{
2044 char procfs_path[50];
2045 FILE *fp;
2046 snprintf(procfs_path, sizeof(procfs_path),
2047 "/proc/asound/card%d/codecs/wcd938x/variant", card_num);
2048 if ((fp = fopen(procfs_path, "r")) == NULL) {
2049 snprintf(procfs_path, sizeof(procfs_path),
2050 "/proc/asound/card%d/codecs/wcd937x/variant", card_num);
2051 if ((fp = fopen(procfs_path, "r")) == NULL) {
2052 ALOGE("%s: ERROR. cannot open %s", __func__, procfs_path);
2053 return -ENOENT;
2054 }
2055 }
2056 fgets(codec_variant, CODEC_VARIANT_MAX_LENGTH, fp);
2057 fclose(fp);
2058 ALOGD("%s: codec variant is %s", __func__, codec_variant);
2059 return 0;
2060}
2061
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05302062
Ashish Jain81eb2a82015-05-13 10:52:34 +05302063#ifdef AUDIO_EXTERNAL_HDMI_ENABLED
2064
2065void get_default_compressed_channel_status(
2066 unsigned char *channel_status)
2067{
Ashish Jain81eb2a82015-05-13 10:52:34 +05302068 memset(channel_status,0,24);
2069
2070 /* block start bit in preamble bit 3 */
2071 channel_status[0] |= PROFESSIONAL;
2072 //compre out
2073 channel_status[0] |= NON_LPCM;
2074 // sample rate; fixed 48K for default/transcode
2075 channel_status[3] |= SR_48000;
2076}
2077
Ashish Jain81eb2a82015-05-13 10:52:34 +05302078int32_t get_compressed_channel_status(void *audio_stream_data,
2079 uint32_t audio_frame_size,
2080 unsigned char *channel_status,
2081 enum audio_parser_code_type codec_type)
2082 // codec_type - AUDIO_PARSER_CODEC_AC3
2083 // - AUDIO_PARSER_CODEC_DTS
2084{
2085 unsigned char *stream;
2086 int ret = 0;
2087 stream = (unsigned char *)audio_stream_data;
2088
2089 if (audio_stream_data == NULL || audio_frame_size == 0) {
2090 ALOGW("no buffer to get channel status, return default for compress");
2091 get_default_compressed_channel_status(channel_status);
2092 return ret;
2093 }
2094
2095 memset(channel_status,0,24);
2096 if(init_audio_parser(stream, audio_frame_size, codec_type) == -1)
2097 {
2098 ALOGE("init audio parser failed");
2099 return -1;
2100 }
2101 ret = get_channel_status(channel_status, codec_type);
2102 return ret;
2103
2104}
2105
Ashish Jain81eb2a82015-05-13 10:52:34 +05302106void get_lpcm_channel_status(uint32_t sampleRate,
2107 unsigned char *channel_status)
2108{
2109 int32_t status = 0;
Ashish Jain81eb2a82015-05-13 10:52:34 +05302110 memset(channel_status,0,24);
2111 /* block start bit in preamble bit 3 */
2112 channel_status[0] |= PROFESSIONAL;
2113 //LPCM OUT
2114 channel_status[0] &= ~NON_LPCM;
2115
2116 switch (sampleRate) {
2117 case 8000:
2118 case 11025:
2119 case 12000:
2120 case 16000:
2121 case 22050:
2122 channel_status[3] |= SR_NOTID;
Preetam Singh Ranawat61716b12015-12-14 11:55:24 +05302123 break;
Ashish Jain81eb2a82015-05-13 10:52:34 +05302124 case 24000:
2125 channel_status[3] |= SR_24000;
2126 break;
2127 case 32000:
2128 channel_status[3] |= SR_32000;
2129 break;
2130 case 44100:
2131 channel_status[3] |= SR_44100;
2132 break;
2133 case 48000:
2134 channel_status[3] |= SR_48000;
2135 break;
2136 case 88200:
2137 channel_status[3] |= SR_88200;
2138 break;
2139 case 96000:
2140 channel_status[3] |= SR_96000;
2141 break;
2142 case 176400:
2143 channel_status[3] |= SR_176400;
2144 break;
2145 case 192000:
2146 channel_status[3] |= SR_192000;
2147 break;
2148 default:
2149 ALOGV("Invalid sample_rate %u\n", sampleRate);
2150 status = -1;
2151 break;
2152 }
2153}
2154
2155void audio_utils_set_hdmi_channel_status(struct stream_out *out, char * buffer, size_t bytes)
2156{
2157 unsigned char channel_status[24]={0};
2158 struct snd_aes_iec958 iec958;
2159 const char *mixer_ctl_name = "IEC958 Playback PCM Stream";
2160 struct mixer_ctl *ctl;
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +05302161 ALOGV("%s: buffer %s bytes %zd", __func__, buffer, bytes);
Arun Mirpurie008ed22019-03-21 11:21:04 -07002162
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05302163 if (audio_extn_utils_is_dolby_format(out->format) &&
Ashish Jain81eb2a82015-05-13 10:52:34 +05302164 /*TODO:Extend code to support DTS passthrough*/
2165 /*set compressed channel status bits*/
Arun Mirpurie008ed22019-03-21 11:21:04 -07002166 audio_extn_passthru_is_passthrough_stream(out) &&
2167 audio_extn_is_hdmi_passthru_enabled()) {
Ashish Jain81eb2a82015-05-13 10:52:34 +05302168 get_compressed_channel_status(buffer, bytes, channel_status, AUDIO_PARSER_CODEC_AC3);
Arun Mirpurie008ed22019-03-21 11:21:04 -07002169 } else {
Ashish Jain81eb2a82015-05-13 10:52:34 +05302170 /*set channel status bit for LPCM*/
2171 get_lpcm_channel_status(out->sample_rate, channel_status);
2172 }
2173
2174 memcpy(iec958.status, channel_status,sizeof(iec958.status));
2175 ctl = mixer_get_ctl_by_name(out->dev->mixer, mixer_ctl_name);
2176 if (!ctl) {
2177 ALOGE("%s: Could not get ctl for mixer cmd - %s",
2178 __func__, mixer_ctl_name);
2179 return;
2180 }
2181 if (mixer_ctl_set_array(ctl, &iec958, sizeof(iec958)) < 0) {
2182 ALOGE("%s: Could not set channel status for ext HDMI ",
2183 __func__);
2184 return;
2185 }
2186
2187}
2188#endif
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302189
2190int audio_extn_utils_get_avt_device_drift(
2191 struct audio_usecase *usecase,
2192 struct audio_avt_device_drift_param *drift_param)
2193{
2194 int ret = 0, count = 0;
2195 char avt_device_drift_mixer_ctl_name[MIXER_PATH_MAX_LENGTH] = {0};
Naresh Tanniru6160c712017-04-17 15:43:48 +05302196 const char *backend = NULL;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302197 struct mixer_ctl *ctl = NULL;
2198 struct audio_avt_device_drift_stats drift_stats;
2199 struct audio_device *adev = NULL;
2200
2201 if (usecase != NULL && usecase->type == PCM_PLAYBACK) {
Naresh Tanniru6160c712017-04-17 15:43:48 +05302202 backend = platform_get_snd_device_backend_interface(usecase->out_snd_device);
2203 if (!backend) {
2204 ALOGE("%s: Unsupported device %d", __func__,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08002205 get_device_types(&usecase->stream.out->device_list));
Naresh Tanniru6160c712017-04-17 15:43:48 +05302206 ret = -EINVAL;
2207 goto done;
2208 }
2209 strlcpy(avt_device_drift_mixer_ctl_name,
2210 backend,
2211 MIXER_PATH_MAX_LENGTH);
2212
2213 count = strlen(backend);
2214 if (MIXER_PATH_MAX_LENGTH - count > 0) {
2215 strlcat(&avt_device_drift_mixer_ctl_name[count],
2216 " DRIFT",
2217 MIXER_PATH_MAX_LENGTH - count);
2218 } else {
2219 ret = -EINVAL;
2220 goto done;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302221 }
2222 } else {
Naresh Tanniru7586e292017-04-13 10:38:13 +05302223 ALOGE("%s: Invalid usecase",__func__);
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302224 ret = -EINVAL;
Naresh Tanniru6160c712017-04-17 15:43:48 +05302225 goto done;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302226 }
2227
Naresh Tanniru6160c712017-04-17 15:43:48 +05302228 adev = usecase->stream.out->dev;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302229 ctl = mixer_get_ctl_by_name(adev->mixer, avt_device_drift_mixer_ctl_name);
2230 if (!ctl) {
2231 ALOGE("%s: Could not get ctl for mixer cmd - %s",
2232 __func__, avt_device_drift_mixer_ctl_name);
2233
2234 ret = -EINVAL;
2235 goto done;
2236 }
2237
2238 ALOGV("%s: Getting AV Timer vs Device Drift mixer ctrl name %s", __func__,
2239 avt_device_drift_mixer_ctl_name);
2240
2241 mixer_ctl_update(ctl);
2242 count = mixer_ctl_get_num_values(ctl);
2243 if (count != sizeof(struct audio_avt_device_drift_stats)) {
2244 ALOGE("%s: mixer_ctl_get_num_values() invalid drift_stats data size",
2245 __func__);
2246
2247 ret = -EINVAL;
2248 goto done;
2249 }
2250
2251 ret = mixer_ctl_get_array(ctl, (void *)&drift_stats, count);
2252 if (ret != 0) {
2253 ALOGE("%s: mixer_ctl_get_array() failed to get drift_stats Params",
2254 __func__);
2255
2256 ret = -EINVAL;
2257 goto done;
2258 }
2259 memcpy(drift_param, &drift_stats.drift_param,
2260 sizeof(struct audio_avt_device_drift_param));
2261done:
2262 return ret;
2263}
Manish Dewangan07de2142017-02-27 19:27:20 +05302264
2265#ifdef SNDRV_COMPRESS_PATH_DELAY
2266int audio_extn_utils_compress_get_dsp_latency(struct stream_out *out)
2267{
2268 int ret = -EINVAL;
2269 struct snd_compr_metadata metadata;
2270 int delay_ms = COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
2271
Weiyin Jiangc49a3b52018-08-17 16:16:08 +08002272 /* override the latency for pcm offload use case */
2273 if ((out->flags & AUDIO_OUTPUT_FLAG_DIRECT) &&
2274 !(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)) {
2275 delay_ms = PCM_OFFLOAD_PLAYBACK_LATENCY;
2276 }
2277
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -07002278 if (property_get_bool("vendor.audio.playback.dsp.pathdelay", false)) {
Manish Dewangan07de2142017-02-27 19:27:20 +05302279 ALOGD("%s:: Quering DSP delay %d",__func__, __LINE__);
2280 if (!(is_offload_usecase(out->usecase))) {
2281 ALOGE("%s:: not supported for non offload session", __func__);
2282 goto exit;
2283 }
2284
2285 if (!out->compr) {
2286 ALOGD("%s:: Invalid compress handle,returning default dsp latency",
2287 __func__);
2288 goto exit;
2289 }
2290
2291 metadata.key = SNDRV_COMPRESS_PATH_DELAY;
2292 ret = compress_get_metadata(out->compr, &metadata);
2293 if(ret) {
2294 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2295 goto exit;
2296 }
2297 delay_ms = metadata.value[0] / 1000; /*convert to ms*/
2298 } else {
2299 ALOGD("%s:: Using Fix DSP delay",__func__);
2300 }
2301
2302exit:
2303 ALOGD("%s:: delay in ms is %d",__func__, delay_ms);
2304 return delay_ms;
2305}
2306#else
2307int audio_extn_utils_compress_get_dsp_latency(struct stream_out *out __unused)
2308{
2309 return COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
2310}
2311#endif
Manish Dewangan69426c82017-01-30 17:35:36 +05302312
2313#ifdef SNDRV_COMPRESS_RENDER_MODE
2314int audio_extn_utils_compress_set_render_mode(struct stream_out *out)
2315{
2316 struct snd_compr_metadata metadata;
2317 int ret = -EINVAL;
2318
2319 if (!(is_offload_usecase(out->usecase))) {
2320 ALOGE("%s:: not supported for non offload session", __func__);
2321 goto exit;
2322 }
2323
2324 if (!out->compr) {
2325 ALOGD("%s:: Invalid compress handle",
2326 __func__);
2327 goto exit;
2328 }
2329
2330 ALOGD("%s:: render mode %d", __func__, out->render_mode);
2331
2332 metadata.key = SNDRV_COMPRESS_RENDER_MODE;
2333 if (out->render_mode == RENDER_MODE_AUDIO_MASTER) {
2334 metadata.value[0] = SNDRV_COMPRESS_RENDER_MODE_AUDIO_MASTER;
2335 } else if (out->render_mode == RENDER_MODE_AUDIO_STC_MASTER) {
2336 metadata.value[0] = SNDRV_COMPRESS_RENDER_MODE_STC_MASTER;
2337 } else {
2338 ret = 0;
2339 goto exit;
2340 }
2341 ret = compress_set_metadata(out->compr, &metadata);
2342 if(ret) {
2343 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2344 }
2345exit:
2346 return ret;
2347}
2348#else
2349int audio_extn_utils_compress_set_render_mode(struct stream_out *out __unused)
2350{
2351 ALOGD("%s:: configuring render mode not supported", __func__);
2352 return 0;
2353}
2354#endif
Manish Dewangan58229382017-02-02 15:48:41 +05302355
2356#ifdef SNDRV_COMPRESS_CLK_REC_MODE
2357int audio_extn_utils_compress_set_clk_rec_mode(
2358 struct audio_usecase *usecase)
2359{
2360 struct snd_compr_metadata metadata;
2361 struct stream_out *out = NULL;
2362 int ret = -EINVAL;
2363
Naresh Tanniru7586e292017-04-13 10:38:13 +05302364 if (usecase == NULL || usecase->type != PCM_PLAYBACK) {
Manish Dewangan58229382017-02-02 15:48:41 +05302365 ALOGE("%s:: Invalid use case", __func__);
2366 goto exit;
2367 }
2368
2369 out = usecase->stream.out;
2370 if (!out) {
2371 ALOGE("%s:: invalid stream", __func__);
2372 goto exit;
2373 }
2374
2375 if (!is_offload_usecase(out->usecase)) {
2376 ALOGE("%s:: not supported for non offload session", __func__);
2377 goto exit;
2378 }
2379
2380 if (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER) {
2381 ALOGD("%s:: clk recovery is only supported in STC render mode",
2382 __func__);
2383 ret = 0;
2384 goto exit;
2385 }
2386
2387 if (!out->compr) {
2388 ALOGD("%s:: Invalid compress handle",
2389 __func__);
2390 goto exit;
2391 }
2392 metadata.key = SNDRV_COMPRESS_CLK_REC_MODE;
2393 switch(usecase->out_snd_device) {
2394 case SND_DEVICE_OUT_HDMI:
2395 case SND_DEVICE_OUT_SPEAKER_AND_HDMI:
2396 case SND_DEVICE_OUT_DISPLAY_PORT:
2397 case SND_DEVICE_OUT_SPEAKER_AND_DISPLAY_PORT:
2398 metadata.value[0] = SNDRV_COMPRESS_CLK_REC_MODE_NONE;
2399 break;
2400 default:
2401 metadata.value[0] = SNDRV_COMPRESS_CLK_REC_MODE_AUTO;
2402 break;
2403 }
2404
2405 ALOGD("%s:: clk recovery mode %d",__func__, metadata.value[0]);
2406
2407 ret = compress_set_metadata(out->compr, &metadata);
2408 if(ret) {
2409 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2410 }
2411
2412exit:
2413 return ret;
2414}
2415#else
2416int audio_extn_utils_compress_set_clk_rec_mode(
2417 struct audio_usecase *usecase __unused)
2418{
2419 ALOGD("%s:: configuring render mode not supported", __func__);
2420 return 0;
2421}
2422#endif
Manish Dewangan27346042017-03-01 12:56:12 +05302423
2424#ifdef SNDRV_COMPRESS_RENDER_WINDOW
2425int audio_extn_utils_compress_set_render_window(
2426 struct stream_out *out,
2427 struct audio_out_render_window_param *render_window)
2428{
2429 struct snd_compr_metadata metadata;
2430 int ret = -EINVAL;
2431
Manish Dewangan27346042017-03-01 12:56:12 +05302432 if(render_window == NULL) {
2433 ALOGE("%s:: Invalid render_window", __func__);
2434 goto exit;
2435 }
2436
Aniket Kumar Lata1e1d3662017-06-01 18:45:48 -07002437 ALOGD("%s:: render window start 0x%"PRIx64" end 0x%"PRIx64"",
2438 __func__,render_window->render_ws, render_window->render_we);
2439
Manish Dewangan27346042017-03-01 12:56:12 +05302440 if (!is_offload_usecase(out->usecase)) {
2441 ALOGE("%s:: not supported for non offload session", __func__);
2442 goto exit;
2443 }
2444
Naresh Tanniru6160c712017-04-17 15:43:48 +05302445 if ((out->render_mode != RENDER_MODE_AUDIO_MASTER) &&
2446 (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER)) {
Manish Dewangan27346042017-03-01 12:56:12 +05302447 ALOGD("%s:: only supported in timestamp mode, current "
2448 "render mode mode %d", __func__, out->render_mode);
2449 goto exit;
2450 }
2451
2452 if (!out->compr) {
2453 ALOGW("%s:: offload session not yet opened,"
2454 "render window will be configure later", __func__);
2455 /* store render window to reconfigure in start_output_stream() */
2456 goto exit;
2457 }
2458
2459 metadata.key = SNDRV_COMPRESS_RENDER_WINDOW;
2460 /*render window start value */
2461 metadata.value[0] = 0xFFFFFFFF & render_window->render_ws; /* lsb */
2462 metadata.value[1] = \
2463 (0xFFFFFFFF00000000 & render_window->render_ws) >> 32; /* msb*/
2464 /*render window end value */
2465 metadata.value[2] = 0xFFFFFFFF & render_window->render_we; /* lsb */
2466 metadata.value[3] = \
2467 (0xFFFFFFFF00000000 & render_window->render_we) >> 32; /* msb*/
2468
2469 ret = compress_set_metadata(out->compr, &metadata);
2470 if(ret) {
2471 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2472 }
2473
2474exit:
2475 return ret;
2476}
2477#else
2478int audio_extn_utils_compress_set_render_window(
2479 struct stream_out *out __unused,
2480 struct audio_out_render_window_param *render_window __unused)
2481{
2482 ALOGD("%s:: configuring render window not supported", __func__);
2483 return 0;
2484}
2485#endif
Manish Dewangan14956cc2017-02-14 18:54:42 +05302486
2487#ifdef SNDRV_COMPRESS_START_DELAY
2488int audio_extn_utils_compress_set_start_delay(
2489 struct stream_out *out,
2490 struct audio_out_start_delay_param *delay_param)
2491{
2492 struct snd_compr_metadata metadata;
2493 int ret = -EINVAL;
2494
2495 if(delay_param == NULL) {
2496 ALOGE("%s:: Invalid delay_param", __func__);
2497 goto exit;
2498 }
2499
2500 ALOGD("%s:: render start delay 0x%"PRIx64" ", __func__,
2501 delay_param->start_delay);
2502
2503 if (!is_offload_usecase(out->usecase)) {
2504 ALOGE("%s:: not supported for non offload session", __func__);
2505 goto exit;
2506 }
2507
Naresh Tanniru6160c712017-04-17 15:43:48 +05302508 if ((out->render_mode != RENDER_MODE_AUDIO_MASTER) &&
2509 (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER)) {
Manish Dewangan14956cc2017-02-14 18:54:42 +05302510 ALOGD("%s:: only supported in timestamp mode, current "
2511 "render mode mode %d", __func__, out->render_mode);
2512 goto exit;
2513 }
2514
2515 if (!out->compr) {
2516 ALOGW("%s:: offload session not yet opened,"
2517 "start delay will be configure later", __func__);
2518 goto exit;
2519 }
2520
2521 metadata.key = SNDRV_COMPRESS_START_DELAY;
2522 metadata.value[0] = 0xFFFFFFFF & delay_param->start_delay; /* lsb */
2523 metadata.value[1] = \
2524 (0xFFFFFFFF00000000 & delay_param->start_delay) >> 32; /* msb*/
2525
2526 ret = compress_set_metadata(out->compr, &metadata);
2527 if(ret) {
2528 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2529 }
2530
2531exit:
2532 return ret;
2533}
2534#else
2535int audio_extn_utils_compress_set_start_delay(
2536 struct stream_out *out __unused,
2537 struct audio_out_start_delay_param *delay_param __unused)
2538{
2539 ALOGD("%s:: configuring render window not supported", __func__);
2540 return 0;
2541}
2542#endif
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002543
Surendar Karka287348c2019-04-10 18:31:46 +05302544#ifdef SNDRV_COMPRESS_DSP_POSITION
2545int audio_extn_utils_compress_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 *val = NULL;
2550 uint64_t time = 0;
2551 struct snd_compr_metadata metadata;
2552
2553 ALOGV("%s:: Quering DSP position with clock id %d",__func__, clock_id);
2554 metadata.key = SNDRV_COMPRESS_DSP_POSITION;
2555 metadata.value[0] = clock_id;
2556 ret = compress_get_metadata(out->compr, &metadata);
2557 if (ret) {
2558 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2559 ret = -errno;
2560 goto exit;
2561 }
2562 val = (uint64_t *)&metadata.value[1];
2563 *frames = *val;
2564 time = *(val + 1);
2565 timestamp->tv_sec = time / 1000000;
2566 timestamp->tv_nsec = (time % 1000000)*1000;
2567
2568exit:
2569 return ret;
2570}
2571#else
2572int audio_extn_utils_compress_get_dsp_presentation_pos(struct stream_out *out __unused,
2573 uint64_t *frames __unused, struct timespec *timestamp __unused,
2574 int32_t clock_id __unused)
2575{
2576 ALOGD("%s:: dsp presentation position not supported", __func__);
2577 return 0;
2578
2579}
2580#endif
2581
2582#ifdef SNDRV_PCM_IOCTL_DSP_POSITION
2583int audio_extn_utils_pcm_get_dsp_presentation_pos(struct stream_out *out,
2584 uint64_t *frames, struct timespec *timestamp, int32_t clock_id)
2585{
2586 int ret = -EINVAL;
2587 uint64_t time = 0;
2588 struct snd_pcm_prsnt_position prsnt_position;
2589
2590 ALOGV("%s:: Quering DSP position with clock id %d",__func__, clock_id);
2591 prsnt_position.clock_id = clock_id;
2592 ret = pcm_ioctl(out->pcm, SNDRV_PCM_IOCTL_DSP_POSITION, &prsnt_position);
2593 if (ret) {
2594 ALOGE("%s::error %d", __func__, ret);
2595 ret = -EIO;
2596 goto exit;
2597 }
2598
2599 *frames = prsnt_position.frames;
2600 time = prsnt_position.timestamp;
2601 timestamp->tv_sec = time / 1000000;
2602 timestamp->tv_nsec = (time % 1000000)*1000;
2603
2604exit:
2605 return ret;
2606}
2607#else
2608int audio_extn_utils_pcm_get_dsp_presentation_pos(struct stream_out *out __unused,
2609 uint64_t *frames __unused, struct timespec *timestamp __unused,
2610 int32_t clock_id __unused)
2611{
2612 ALOGD("%s:: dsp presentation position not supported", __func__);
2613 return 0;
2614
2615}
2616#endif
2617
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002618#define MAX_SND_CARD 8
Ashish Jain30ae8942017-11-05 17:21:23 +05302619#define RETRY_US 1000000
2620#define RETRY_NUMBER 40
Aalique Grahame22e49102018-12-18 14:23:57 -08002621#define PLATFORM_INFO_XML_PATH "audio_platform_info.xml"
2622#define PLATFORM_INFO_XML_BASE_STRING "audio_platform_info"
2623
Aalique Grahame22e49102018-12-18 14:23:57 -08002624bool audio_extn_utils_resolve_config_file(char file_name[MIXER_PATH_MAX_LENGTH])
2625{
2626 char full_config_path[MIXER_PATH_MAX_LENGTH];
Saurav Kumarf5d2c342020-07-29 19:31:49 +05302627 char vendor_config_path[VENDOR_CONFIG_PATH_MAX_LENGTH];
2628
2629 /* Get path for audio configuration files in vendor */
2630 audio_get_vendor_config_path(vendor_config_path,
2631 sizeof(vendor_config_path));
2632 snprintf(full_config_path, sizeof(full_config_path),
2633 "%s/%s", vendor_config_path, file_name);
2634 if (F_OK == access(full_config_path, 0)) {
2635 strlcpy(file_name, full_config_path, MIXER_PATH_MAX_LENGTH);
2636 return true;
Aalique Grahame22e49102018-12-18 14:23:57 -08002637 }
2638 return false;
2639}
2640
2641/* platform_info_file should be size 'MIXER_PATH_MAX_LENGTH' */
2642int audio_extn_utils_get_platform_info(const char* snd_card_name, char* platform_info_file)
2643{
2644 if (NULL == snd_card_name) {
2645 return -1;
2646 }
2647
2648 struct snd_card_split *snd_split_handle = NULL;
2649 int ret = 0;
2650 audio_extn_set_snd_card_split(snd_card_name);
2651 snd_split_handle = audio_extn_get_snd_card_split();
2652
2653 snprintf(platform_info_file, MIXER_PATH_MAX_LENGTH, "%s_%s_%s.xml",
2654 PLATFORM_INFO_XML_BASE_STRING, snd_split_handle->snd_card,
2655 snd_split_handle->form_factor);
2656
2657 if (!audio_extn_utils_resolve_config_file(platform_info_file)) {
2658 memset(platform_info_file, 0, MIXER_PATH_MAX_LENGTH);
2659 snprintf(platform_info_file, MIXER_PATH_MAX_LENGTH, "%s_%s.xml",
2660 PLATFORM_INFO_XML_BASE_STRING, snd_split_handle->snd_card);
2661
2662 if (!audio_extn_utils_resolve_config_file(platform_info_file)) {
2663 memset(platform_info_file, 0, MIXER_PATH_MAX_LENGTH);
2664 strlcpy(platform_info_file, PLATFORM_INFO_XML_PATH, MIXER_PATH_MAX_LENGTH);
2665 ret = audio_extn_utils_resolve_config_file(platform_info_file) ? 0 : -1;
2666 }
2667 }
2668
2669 return ret;
2670}
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002671
2672int audio_extn_utils_get_snd_card_num()
2673{
Soumya Managoli9fee7c62018-04-06 16:21:50 +05302674 int snd_card_num = 0;
2675 struct mixer *mixer = NULL;
2676
2677 snd_card_num = audio_extn_utils_open_snd_mixer(&mixer);
2678 if (mixer)
2679 mixer_close(mixer);
2680 return snd_card_num;
2681}
2682
2683int audio_extn_utils_open_snd_mixer(struct mixer **mixer_handle)
2684{
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002685
2686 void *hw_info = NULL;
2687 struct mixer *mixer = NULL;
2688 int retry_num = 0;
mpangfaa7bae2019-01-15 16:38:13 +08002689 int snd_card_num = 0;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002690 char* snd_card_name = NULL;
mpangfaa7bae2019-01-15 16:38:13 +08002691 int snd_card_detection_info[MAX_SND_CARD] = {0};
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002692
Soumya Managoli9fee7c62018-04-06 16:21:50 +05302693 if (!mixer_handle) {
2694 ALOGE("invalid mixer handle");
2695 return -1;
2696 }
2697 *mixer_handle = NULL;
Ashish Jain30ae8942017-11-05 17:21:23 +05302698 /*
mpangfaa7bae2019-01-15 16:38:13 +08002699 * Try with all the sound cards ( 0 to 7 ) and if none of them were detected
Ashish Jain30ae8942017-11-05 17:21:23 +05302700 * sleep for 1 sec and try detections with sound card 0 again.
2701 * If sound card gets detected, check if it is relevant, if not check with the
2702 * other sound cards. To ensure that the irrelevant sound card is not check again,
2703 * we maintain it in min_snd_card_num.
2704 */
2705 while (retry_num < RETRY_NUMBER) {
mpangfaa7bae2019-01-15 16:38:13 +08002706 snd_card_num = 0;
2707 while (snd_card_num < MAX_SND_CARD) {
2708 if (snd_card_detection_info[snd_card_num] == 0) {
2709 mixer = mixer_open(snd_card_num);
2710 if (!mixer)
2711 snd_card_num++;
2712 else
2713 break;
2714 } else
2715 snd_card_num++;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002716 }
2717
2718 if (!mixer) {
Ashish Jain30ae8942017-11-05 17:21:23 +05302719 usleep(RETRY_US);
Ashish Jain30ae8942017-11-05 17:21:23 +05302720 retry_num++;
mpangfaa7bae2019-01-15 16:38:13 +08002721 ALOGD("%s: retry, retry_num %d", __func__, retry_num);
Ashish Jain30ae8942017-11-05 17:21:23 +05302722 continue;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002723 }
2724
2725 snd_card_name = strdup(mixer_get_name(mixer));
2726 if (!snd_card_name) {
2727 ALOGE("failed to allocate memory for snd_card_name\n");
2728 mixer_close(mixer);
2729 return -1;
2730 }
2731 ALOGD("%s: snd_card_name: %s", __func__, snd_card_name);
mpangfaa7bae2019-01-15 16:38:13 +08002732 snd_card_detection_info[snd_card_num] = 1;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002733 hw_info = hw_info_init(snd_card_name);
2734 if (hw_info) {
2735 ALOGD("%s: Opened sound card:%d", __func__, snd_card_num);
2736 break;
2737 }
mpangfaa7bae2019-01-15 16:38:13 +08002738 ALOGE("%s: Failed to init hardware info, snd_card_num:%d", __func__, snd_card_num);
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002739
Dhananjay Kumara7e27832017-07-11 15:40:39 +05302740 free(snd_card_name);
2741 snd_card_name = NULL;
2742
2743 mixer_close(mixer);
2744 mixer = NULL;
2745 }
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002746 if (snd_card_name)
2747 free(snd_card_name);
Ashish Jain30ae8942017-11-05 17:21:23 +05302748
Dhananjay Kumara7e27832017-07-11 15:40:39 +05302749 if (hw_info)
2750 hw_info_deinit(hw_info);
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002751
mpangfaa7bae2019-01-15 16:38:13 +08002752 if (retry_num >= RETRY_NUMBER) {
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002753 ALOGE("%s: Unable to find correct sound card, aborting.", __func__);
2754 return -1;
2755 }
2756
Soumya Managoli9fee7c62018-04-06 16:21:50 +05302757 if (mixer)
2758 *mixer_handle = mixer;
2759
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002760 return snd_card_num;
2761}
Naresh Tanniru6160c712017-04-17 15:43:48 +05302762
Soumya Managoli9fee7c62018-04-06 16:21:50 +05302763void audio_extn_utils_close_snd_mixer(struct mixer *mixer)
2764{
2765 if (mixer)
2766 mixer_close(mixer);
2767}
2768
Naresh Tanniru6160c712017-04-17 15:43:48 +05302769#ifdef SNDRV_COMPRESS_ENABLE_ADJUST_SESSION_CLOCK
2770int audio_extn_utils_compress_enable_drift_correction(
2771 struct stream_out *out,
2772 struct audio_out_enable_drift_correction *drift)
2773{
2774 struct snd_compr_metadata metadata;
2775 int ret = -EINVAL;
2776
2777 if(drift == NULL) {
2778 ALOGE("%s:: Invalid param", __func__);
2779 goto exit;
2780 }
2781
2782 ALOGD("%s:: drift enable %d", __func__,drift->enable);
2783
2784 if (!is_offload_usecase(out->usecase)) {
2785 ALOGE("%s:: not supported for non offload session", __func__);
2786 goto exit;
2787 }
2788
2789 if (!out->compr) {
2790 ALOGW("%s:: offload session not yet opened,"
2791 "start delay will be configure later", __func__);
2792 goto exit;
2793 }
2794
2795 metadata.key = SNDRV_COMPRESS_ENABLE_ADJUST_SESSION_CLOCK;
2796 metadata.value[0] = drift->enable;
2797 out->drift_correction_enabled = drift->enable;
2798
2799 ret = compress_set_metadata(out->compr, &metadata);
2800 if(ret) {
2801 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2802 out->drift_correction_enabled = false;
2803 }
2804
2805exit:
2806 return ret;
2807}
2808#else
2809int audio_extn_utils_compress_enable_drift_correction(
2810 struct stream_out *out __unused,
2811 struct audio_out_enable_drift_correction *drift __unused)
2812{
2813 ALOGD("%s:: configuring drift enablement not supported", __func__);
2814 return 0;
2815}
2816#endif
2817
2818#ifdef SNDRV_COMPRESS_ADJUST_SESSION_CLOCK
2819int audio_extn_utils_compress_correct_drift(
2820 struct stream_out *out,
2821 struct audio_out_correct_drift *drift_param)
2822{
2823 struct snd_compr_metadata metadata;
2824 int ret = -EINVAL;
2825
2826 if (drift_param == NULL) {
2827 ALOGE("%s:: Invalid drift_param", __func__);
2828 goto exit;
2829 }
2830
2831 ALOGD("%s:: adjust time 0x%"PRIx64" ", __func__,
2832 drift_param->adjust_time);
2833
2834 if (!is_offload_usecase(out->usecase)) {
2835 ALOGE("%s:: not supported for non offload session", __func__);
2836 goto exit;
2837 }
2838
2839 if (!out->compr) {
2840 ALOGW("%s:: offload session not yet opened", __func__);
2841 goto exit;
2842 }
2843
2844 if (!out->drift_correction_enabled) {
2845 ALOGE("%s:: drift correction not enabled", __func__);
2846 goto exit;
2847 }
2848
2849 metadata.key = SNDRV_COMPRESS_ADJUST_SESSION_CLOCK;
2850 metadata.value[0] = 0xFFFFFFFF & drift_param->adjust_time; /* lsb */
2851 metadata.value[1] = \
2852 (0xFFFFFFFF00000000 & drift_param->adjust_time) >> 32; /* msb*/
2853
2854 ret = compress_set_metadata(out->compr, &metadata);
2855 if(ret)
2856 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2857exit:
2858 return ret;
2859}
2860#else
2861int audio_extn_utils_compress_correct_drift(
2862 struct stream_out *out __unused,
2863 struct audio_out_correct_drift *drift_param __unused)
2864{
2865 ALOGD("%s:: setting adjust clock not supported", __func__);
2866 return 0;
2867}
2868#endif
Naresh Tanniru29bce4e2017-04-27 17:54:30 +05302869
2870int audio_extn_utils_set_channel_map(
2871 struct stream_out *out,
2872 struct audio_out_channel_map_param *channel_map_param)
2873{
2874 int ret = -EINVAL, i = 0;
2875 int channels = audio_channel_count_from_out_mask(out->channel_mask);
2876
2877 if (channel_map_param == NULL) {
2878 ALOGE("%s:: Invalid channel_map", __func__);
2879 goto exit;
2880 }
2881
2882 if (channel_map_param->channels != channels) {
2883 ALOGE("%s:: Channels(%d) does not match stream channels(%d)",
2884 __func__, channel_map_param->channels, channels);
2885 goto exit;
2886 }
2887
2888 for ( i = 0; i < channels; i++) {
2889 ALOGV("%s:: channel_map[%d]- %d", __func__, i, channel_map_param->channel_map[i]);
2890 out->channel_map_param.channel_map[i] = channel_map_param->channel_map[i];
2891 }
2892 ret = 0;
2893exit:
2894 return ret;
2895}
Varun Balaraje49253e2017-07-06 19:48:56 +05302896
2897int audio_extn_utils_set_pan_scale_params(
2898 struct stream_out *out,
2899 struct mix_matrix_params *mm_params)
2900{
2901 int ret = -EINVAL, i = 0, j = 0;
2902
Varun Balaraj003ee752017-08-07 17:54:55 +05302903 if (mm_params == NULL || out == NULL) {
2904 ALOGE("%s:: Invalid mix matrix or out param", __func__);
Varun Balaraje49253e2017-07-06 19:48:56 +05302905 goto exit;
2906 }
2907
2908 if (mm_params->num_output_channels > MAX_CHANNELS_SUPPORTED ||
2909 mm_params->num_output_channels <= 0 ||
2910 mm_params->num_input_channels > MAX_CHANNELS_SUPPORTED ||
2911 mm_params->num_input_channels <= 0)
2912 goto exit;
2913
2914 out->pan_scale_params.num_output_channels = mm_params->num_output_channels;
2915 out->pan_scale_params.num_input_channels = mm_params->num_input_channels;
2916 out->pan_scale_params.has_output_channel_map =
2917 mm_params->has_output_channel_map;
2918 for (i = 0; i < mm_params->num_output_channels; i++)
2919 out->pan_scale_params.output_channel_map[i] =
2920 mm_params->output_channel_map[i];
2921
2922 out->pan_scale_params.has_input_channel_map =
2923 mm_params->has_input_channel_map;
2924 for (i = 0; i < mm_params->num_input_channels; i++)
2925 out->pan_scale_params.input_channel_map[i] =
2926 mm_params->input_channel_map[i];
2927
2928 out->pan_scale_params.has_mixer_coeffs = mm_params->has_mixer_coeffs;
2929 for (i = 0; i < mm_params->num_output_channels; i++)
2930 for (j = 0; j < mm_params->num_input_channels; j++) {
2931 //Convert the channel coefficient gains in Q14 format
2932 out->pan_scale_params.mixer_coeffs[i][j] =
2933 mm_params->mixer_coeffs[i][j] * (2 << 13);
2934 }
2935
2936 ret = platform_set_stream_pan_scale_params(out->dev->platform,
2937 out->pcm_device_id,
2938 out->pan_scale_params);
2939
2940exit:
2941 return ret;
2942}
2943
2944int audio_extn_utils_set_downmix_params(
2945 struct stream_out *out,
2946 struct mix_matrix_params *mm_params)
2947{
2948 int ret = -EINVAL, i = 0, j = 0;
2949 struct audio_usecase *usecase = NULL;
2950
Aniket Kumar Lataf9f246e2017-09-15 15:20:16 -07002951 if (mm_params == NULL || out == NULL) {
Varun Balaraj003ee752017-08-07 17:54:55 +05302952 ALOGE("%s:: Invalid mix matrix or out param", __func__);
Varun Balaraje49253e2017-07-06 19:48:56 +05302953 goto exit;
2954 }
2955
2956 if (mm_params->num_output_channels > MAX_CHANNELS_SUPPORTED ||
2957 mm_params->num_output_channels <= 0 ||
2958 mm_params->num_input_channels > MAX_CHANNELS_SUPPORTED ||
2959 mm_params->num_input_channels <= 0)
2960 goto exit;
2961
2962 usecase = get_usecase_from_list(out->dev, out->usecase);
Varun Balaraj003ee752017-08-07 17:54:55 +05302963 if (!usecase) {
2964 ALOGE("%s: Get usecase list failed!", __func__);
Aniket Kumar Lataf9f246e2017-09-15 15:20:16 -07002965 goto exit;
2966 }
Varun Balaraje49253e2017-07-06 19:48:56 +05302967 out->downmix_params.num_output_channels = mm_params->num_output_channels;
2968 out->downmix_params.num_input_channels = mm_params->num_input_channels;
2969
2970 out->downmix_params.has_output_channel_map =
2971 mm_params->has_output_channel_map;
2972 for (i = 0; i < mm_params->num_output_channels; i++) {
2973 out->downmix_params.output_channel_map[i] =
2974 mm_params->output_channel_map[i];
2975 }
2976
2977 out->downmix_params.has_input_channel_map =
2978 mm_params->has_input_channel_map;
2979 for (i = 0; i < mm_params->num_input_channels; i++)
2980 out->downmix_params.input_channel_map[i] =
2981 mm_params->input_channel_map[i];
2982
2983 out->downmix_params.has_mixer_coeffs = mm_params->has_mixer_coeffs;
2984 for (i = 0; i < mm_params->num_output_channels; i++)
Nikhil Latukar2e6f6242017-08-15 13:37:07 +05302985 for (j = 0; j < mm_params->num_input_channels; j++) {
2986 //Convert the channel coefficient gains in Q14 format
Varun Balaraje49253e2017-07-06 19:48:56 +05302987 out->downmix_params.mixer_coeffs[i][j] =
Nikhil Latukar2e6f6242017-08-15 13:37:07 +05302988 mm_params->mixer_coeffs[i][j] * (2 << 13);
2989 }
Varun Balaraje49253e2017-07-06 19:48:56 +05302990
2991 ret = platform_set_stream_downmix_params(out->dev->platform,
2992 out->pcm_device_id,
2993 usecase->out_snd_device,
2994 out->downmix_params);
2995
2996exit:
2997 return ret;
2998}
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05302999
3000bool audio_extn_utils_is_dolby_format(audio_format_t format)
3001{
3002 if (format == AUDIO_FORMAT_AC3 ||
3003 format == AUDIO_FORMAT_E_AC3 ||
3004 format == AUDIO_FORMAT_E_AC3_JOC)
3005 return true;
3006 else
3007 return false;
3008}
3009
`Deeraj Soman676c2702017-09-18 19:25:53 +05303010int audio_extn_utils_get_bit_width_from_string(const char *id_string)
3011{
3012 int i;
3013 const mixer_config_lookup mixer_bitwidth_config[] = {{"S24_3LE", 24},
3014 {"S32_LE", 32},
3015 {"S24_LE", 24},
3016 {"S16_LE", 16}};
3017 int num_configs = sizeof(mixer_bitwidth_config) / sizeof(mixer_bitwidth_config[0]);
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05303018
`Deeraj Soman676c2702017-09-18 19:25:53 +05303019 for (i = 0; i < num_configs; i++) {
3020 if (!strcmp(id_string, mixer_bitwidth_config[i].id_string))
3021 return mixer_bitwidth_config[i].value;
3022 }
3023
3024 return -EINVAL;
3025}
3026
3027int audio_extn_utils_get_sample_rate_from_string(const char *id_string)
3028{
3029 int i;
Weiyin Jiang679df542020-11-11 11:37:21 +08003030 const mixer_config_lookup mixer_samplerate_config[] = {{"KHZ_8", 8000},
3031 {"KHZ_11P025", 11025},
3032 {"KHZ_16", 16000},
3033 {"KHZ_22P05", 22050},
3034 {"KHZ_32", 32000},
`Deeraj Soman676c2702017-09-18 19:25:53 +05303035 {"KHZ_48", 48000},
3036 {"KHZ_96", 96000},
3037 {"KHZ_144", 144000},
3038 {"KHZ_192", 192000},
3039 {"KHZ_384", 384000},
3040 {"KHZ_44P1", 44100},
3041 {"KHZ_88P2", 88200},
3042 {"KHZ_176P4", 176400},
3043 {"KHZ_352P8", 352800}};
3044 int num_configs = sizeof(mixer_samplerate_config) / sizeof(mixer_samplerate_config[0]);
3045
3046 for (i = 0; i < num_configs; i++) {
3047 if (!strcmp(id_string, mixer_samplerate_config[i].id_string))
3048 return mixer_samplerate_config[i].value;
3049 }
3050
3051 return -EINVAL;
3052}
3053
3054int audio_extn_utils_get_channels_from_string(const char *id_string)
3055{
3056 int i;
3057 const mixer_config_lookup mixer_channels_config[] = {{"One", 1},
3058 {"Two", 2},
3059 {"Three",3},
3060 {"Four", 4},
3061 {"Five", 5},
3062 {"Six", 6},
3063 {"Seven", 7},
3064 {"Eight", 8}};
3065 int num_configs = sizeof(mixer_channels_config) / sizeof(mixer_channels_config[0]);
3066
3067 for (i = 0; i < num_configs; i++) {
3068 if (!strcmp(id_string, mixer_channels_config[i].id_string))
3069 return mixer_channels_config[i].value;
3070 }
3071
3072 return -EINVAL;
3073}
Surendar karka30569792018-05-08 12:02:21 +05303074
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08003075void audio_extn_utils_release_snd_device(snd_device_t snd_device)
3076{
3077 audio_extn_dev_arbi_release(snd_device);
3078 audio_extn_sound_trigger_update_device_status(snd_device,
3079 ST_EVENT_SND_DEVICE_FREE);
3080 audio_extn_listen_update_device_status(snd_device,
3081 LISTEN_EVENT_SND_DEVICE_FREE);
3082}
3083
Weiyin Jiangd5718bd2019-09-04 16:52:08 +08003084int audio_extn_utils_get_license_params(
3085 const struct audio_device *adev,
3086 struct audio_license_params *license_params)
Surendar karka30569792018-05-08 12:02:21 +05303087{
3088 if(!license_params)
3089 return -EINVAL;
Weiyin Jiangd5718bd2019-09-04 16:52:08 +08003090
3091 return platform_get_license_by_product(adev->platform,
3092 (const char*)license_params->product, &license_params->key, license_params->license);
Surendar karka30569792018-05-08 12:02:21 +05303093}
3094
Aalique Grahame22e49102018-12-18 14:23:57 -08003095int audio_extn_utils_send_app_type_gain(struct audio_device *adev,
3096 int app_type,
3097 int *gain)
3098{
3099 int gain_cfg[4];
3100 const char *mixer_ctl_name = "App Type Gain";
3101 struct mixer_ctl *ctl;
3102 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
3103 if (!ctl) {
3104 ALOGE("%s: Could not get volume ctl mixer %s", __func__,
3105 mixer_ctl_name);
3106 return -EINVAL;
3107 }
3108 gain_cfg[0] = 0;
3109 gain_cfg[1] = app_type;
3110 gain_cfg[2] = gain[0];
3111 gain_cfg[3] = gain[1];
3112 ALOGV("%s app_type %d l(%d) r(%d)", __func__, app_type, gain[0], gain[1]);
3113 return mixer_ctl_set_array(ctl, gain_cfg,
3114 sizeof(gain_cfg)/sizeof(gain_cfg[0]));
3115}
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -07003116
Weiyin Jiangd5718bd2019-09-04 16:52:08 +08003117static void vndk_fwk_init()
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -07003118{
Weiyin Jiangd5718bd2019-09-04 16:52:08 +08003119 if (mVndkFwk.lib_handle != NULL)
3120 return;
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -07003121
Weiyin Jiangd5718bd2019-09-04 16:52:08 +08003122 mVndkFwk.lib_handle = dlopen(VNDK_FWK_LIB_PATH, RTLD_NOW);
3123 if (mVndkFwk.lib_handle == NULL) {
3124 ALOGW("%s: failed to dlopen VNDK_FWK_LIB %s", __func__, strerror(errno));
3125 return;
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -07003126 }
3127
Weiyin Jiangd5718bd2019-09-04 16:52:08 +08003128 *(void **)(&mVndkFwk.isVendorEnhancedFwk) =
3129 dlsym(mVndkFwk.lib_handle, "isRunningWithVendorEnhancedFramework");
3130 if (mVndkFwk.isVendorEnhancedFwk == NULL) {
3131 ALOGW("%s: dlsym failed %s", __func__, strerror(errno));
3132 if (mVndkFwk.lib_handle) {
3133 dlclose(mVndkFwk.lib_handle);
3134 mVndkFwk.lib_handle = NULL;
3135 }
3136 return;
3137 }
3138
3139
3140 *(void **)(&mVndkFwk.getVendorEnhancedInfo) =
3141 dlsym(mVndkFwk.lib_handle, "getVendorEnhancedInfo");
3142 if (mVndkFwk.getVendorEnhancedInfo == NULL) {
3143 ALOGW("%s: dlsym failed %s", __func__, strerror(errno));
3144 if (mVndkFwk.lib_handle) {
3145 dlclose(mVndkFwk.lib_handle);
3146 mVndkFwk.lib_handle = NULL;
3147 }
3148 }
3149
3150 return;
3151}
3152
3153bool audio_extn_utils_is_vendor_enhanced_fwk()
3154{
3155 static int is_vendor_enhanced_fwk = -EINVAL;
3156 if (is_vendor_enhanced_fwk != -EINVAL)
3157 return (bool)is_vendor_enhanced_fwk;
3158
3159 vndk_fwk_init();
3160
3161 if (mVndkFwk.isVendorEnhancedFwk != NULL) {
3162 is_vendor_enhanced_fwk = mVndkFwk.isVendorEnhancedFwk();
3163 ALOGW("%s: is_vendor_enhanced_fwk %d", __func__, is_vendor_enhanced_fwk);
3164 } else {
3165 is_vendor_enhanced_fwk = 0;
3166 ALOGW("%s: default to non enhanced_fwk config", __func__);
3167 }
3168
3169 return (bool)is_vendor_enhanced_fwk;
3170}
3171
3172int audio_extn_utils_get_vendor_enhanced_info()
3173{
3174 static int vendor_enhanced_info = -EINVAL;
3175 if (vendor_enhanced_info != -EINVAL)
3176 return vendor_enhanced_info;
3177
3178 vndk_fwk_init();
3179
3180 if (mVndkFwk.getVendorEnhancedInfo != NULL) {
3181 vendor_enhanced_info = mVndkFwk.getVendorEnhancedInfo();
3182 ALOGW("%s: vendor_enhanced_info 0x%x", __func__, vendor_enhanced_info);
3183 } else {
3184 vendor_enhanced_info = 0x0;
3185 ALOGW("%s: default to vendor_enhanced_info 0x0", __func__);
3186 }
3187
3188 return vendor_enhanced_info;
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -07003189}
Deeraj Soman14230922019-01-30 16:39:30 +05303190
Deeraj Somanfa377bf2019-02-06 12:57:59 +05303191int audio_extn_utils_get_perf_mode_flag(void)
3192{
3193#ifdef COMPRESSED_PERF_MODE_FLAG
3194 return COMPRESSED_PERF_MODE_FLAG;
3195#else
3196 return 0;
3197#endif
3198}
3199
Deeraj Soman14230922019-01-30 16:39:30 +05303200size_t audio_extn_utils_get_input_buffer_size(uint32_t sample_rate,
3201 audio_format_t format,
3202 int channel_count,
3203 int64_t duration_ms,
3204 bool is_low_latency)
3205{
3206 size_t size = 0;
3207 size_t capture_duration = AUDIO_CAPTURE_PERIOD_DURATION_MSEC;
3208 uint32_t bytes_per_period_sample = 0;
3209
3210
3211 if (audio_extn_utils_check_input_parameters(sample_rate, format, channel_count) != 0)
3212 return 0;
3213
3214 if (duration_ms >= MIN_OFFLOAD_BUFFER_DURATION_MS && duration_ms <= MAX_OFFLOAD_BUFFER_DURATION_MS)
3215 capture_duration = duration_ms;
3216
3217 size = (sample_rate * capture_duration) / 1000;
3218 if (is_low_latency)
3219 size = LOW_LATENCY_CAPTURE_PERIOD_SIZE;
3220
3221
3222 bytes_per_period_sample = audio_bytes_per_sample(format) * channel_count;
3223 size *= bytes_per_period_sample;
3224
3225 /* make sure the size is multiple of 32 bytes and additionally multiple of
3226 * the frame_size (required for 24bit samples and non-power-of-2 channel counts)
3227 * At 48 kHz mono 16-bit PCM:
3228 * 5.000 ms = 240 frames = 15*16*1*2 = 480, a whole multiple of 32 (15)
3229 * 3.333 ms = 160 frames = 10*16*1*2 = 320, a whole multiple of 32 (10)
3230 *
3231 * The loop reaches result within 32 iterations, as initial size is
3232 * already a multiple of frame_size
3233 */
3234 size = audio_extn_utils_nearest_multiple(size, audio_extn_utils_lcm(32, bytes_per_period_sample));
3235
3236 return size;
3237}
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07003238
3239int audio_extn_utils_hash_fn(void *key)
3240{
3241 return (int)key;
3242}
3243
3244bool audio_extn_utils_hash_eq(void *key1, void *key2)
3245{
3246 return (key1 == key2);
3247}