blob: 366fc85ecc1b7ab3768dbcd09a8fa577985a69ba [file] [log] [blame]
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001/*
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07002 * Copyright (c) 2014-2020, The Linux Foundation. All rights reserved.
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07003 * Not a Contribution.
4 *
5 * Copyright (C) 2014 The Android Open Source Project
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19
20#define LOG_TAG "audio_hw_utils"
21/* #define LOG_NDEBUG 0 */
22
Manish Dewangan27346042017-03-01 12:56:12 +053023#include <inttypes.h>
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070024#include <errno.h>
25#include <cutils/properties.h>
26#include <cutils/config_utils.h>
27#include <stdlib.h>
28#include <dlfcn.h>
29#include <cutils/str_parms.h>
Aalique Grahame22e49102018-12-18 14:23:57 -080030#include <log/log.h>
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070031#include <cutils/misc.h>
Vinay Vermaaddfa4a2018-04-29 14:03:38 +053032#include <unistd.h>
Surendar Karka287348c2019-04-10 18:31:46 +053033#include <sys/ioctl.h>
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070034
Manish Dewangan07de2142017-02-27 19:27:20 +053035
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070036#include "audio_hw.h"
37#include "platform.h"
38#include "platform_api.h"
39#include "audio_extn.h"
Deeraj Soman14230922019-01-30 16:39:30 +053040#include "voice_extn.h"
Narsinga Rao Chella212e2542014-11-17 19:57:04 -080041#include "voice.h"
Manish Dewangan07de2142017-02-27 19:27:20 +053042#include <sound/compress_params.h>
43#include <sound/compress_offload.h>
Surendar Karka287348c2019-04-10 18:31:46 +053044#include <sound/devdep_params.h>
Manish Dewangan07de2142017-02-27 19:27:20 +053045#include <tinycompress/tinycompress.h>
Revathi Uddaraju1eac8b02017-05-18 17:13:33 +053046
47#ifdef DYNAMIC_LOG_ENABLED
48#include <log_xml_parser.h>
49#define LOG_MASK HAL_MOD_FILE_UTILS
50#include <log_utils.h>
51#endif
52
Ashish Jain81eb2a82015-05-13 10:52:34 +053053#ifdef AUDIO_EXTERNAL_HDMI_ENABLED
Ashish Jain81eb2a82015-05-13 10:52:34 +053054#include "audio_parsers.h"
55#endif
Ashish Jain81eb2a82015-05-13 10:52:34 +053056
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 Gao3d477082020-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 Gao3d477082020-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;
Aniket Kumar Latad13758f2020-08-06 15:11:36 -0700775 ALOGV("%s Allowing 24 and above bits playback on speaker \
776 ONLY at default sampling rate", __func__);
Amit Shekhar1d896042014-10-03 13:16:09 -0700777 }
778
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -0700779 property_get("vendor.audio.playback.mch.downsample",value,"");
Manish Dewangan837dc462015-05-27 10:17:41 +0530780 if (!strncmp("true", value, sizeof("true"))) {
781 if ((popcount(channel_mask) > 2) &&
782 (sample_rate > CODEC_BACKEND_DEFAULT_SAMPLE_RATE) &&
Aalique Grahame5cd12ff2017-10-19 10:57:00 -0700783 !(flags & (audio_output_flags_t)AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH)) {
Manish Dewangan837dc462015-05-27 10:17:41 +0530784 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
785 ALOGD("%s: MCH session defaulting sample rate to %d",
786 __func__, sample_rate);
787 }
788 }
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +0530789
790 /* Set sampling rate to 176.4 for DSD64
791 * and 352.8Khz for DSD128.
792 * Set Bit Width to 16. output will be 16 bit
793 * post DoP in ASM.
794 */
Aalique Grahame5cd12ff2017-10-19 10:57:00 -0700795 if ((flags & (audio_output_flags_t)AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH) &&
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +0530796 (format == AUDIO_FORMAT_DSD)) {
797 bit_width = 16;
798 if (sample_rate == INPUT_SAMPLING_RATE_DSD64)
799 sample_rate = OUTPUT_SAMPLING_RATE_DSD64;
800 else if (sample_rate == INPUT_SAMPLING_RATE_DSD128)
801 sample_rate = OUTPUT_SAMPLING_RATE_DSD128;
802 }
803
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +0530804
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800805 ALOGV("%s: flags: %x, format: %x sample_rate %d, profile %s, app_type %d",
806 __func__, flags, format, sample_rate, profile, app_type_cfg->app_type);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700807 list_for_each(node_i, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530808 s_info = node_to_item(node_i, struct streams_io_cfg, list);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530809 /* Along with flags do profile matching if set at either end.*/
810 if (s_info->flags.out_flags == flags &&
811 ((profile[0] == '\0' && s_info->profile[0] == '\0') ||
812 strncmp(s_info->profile, profile, sizeof(s_info->profile)) == 0)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530813 list_for_each(node_j, &s_info->format_list) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700814 sf_info = node_to_item(node_j, struct stream_format, list);
815 if (sf_info->format == format) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530816 if (set_app_type_cfg(s_info, app_type_cfg, sample_rate, bit_width))
Amit Shekhar6f461b12014-08-01 14:52:58 -0700817 return;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700818 }
819 }
820 }
821 }
822 list_for_each(node_i, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530823 s_info = node_to_item(node_i, struct streams_io_cfg, list);
824 if (s_info->flags.out_flags == AUDIO_OUTPUT_FLAG_PRIMARY) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700825 ALOGV("Compatible output profile not found.");
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530826 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
827 app_type_cfg->sample_rate = s_info->app_type_cfg.sample_rate;
828 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700829 ALOGV("%s Default to primary output: App type: %d sample_rate %d",
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530830 __func__, s_info->app_type_cfg.app_type, app_type_cfg->sample_rate);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700831 return;
832 }
833 }
834 ALOGW("%s: App type could not be selected. Falling back to default", __func__);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700835 app_type_cfg->app_type = platform_get_default_app_type(platform);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700836 app_type_cfg->sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700837 app_type_cfg->bit_width = 16;
838}
839
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +0530840static bool audio_is_this_native_usecase(struct audio_usecase *uc)
841{
842 bool native_usecase = false;
843 struct stream_out *out = (struct stream_out*) uc->stream.out;
844
845 if (PCM_PLAYBACK == uc->type && out != NULL &&
846 NATIVE_AUDIO_MODE_INVALID != platform_get_native_support() &&
847 is_offload_usecase(uc->id) &&
848 (out->sample_rate == OUTPUT_SAMPLING_RATE_44100))
849 native_usecase = true;
850
851 return native_usecase;
852}
853
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800854bool audio_extn_is_dsp_bit_width_enforce_mode_supported(audio_output_flags_t flags)
855{
856 /* DSP bitwidth enforce mode for ADM and AFE:
857 * includes:
858 * deep buffer, low latency, direct pcm and offload.
859 * excludes:
860 * ull(raw+fast), VOIP.
861 */
862 if ((flags & AUDIO_OUTPUT_FLAG_VOIP_RX) ||
863 ((flags & AUDIO_OUTPUT_FLAG_RAW) &&
864 (flags & AUDIO_OUTPUT_FLAG_FAST)))
865 return false;
866
867
868 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) ||
869 (flags & AUDIO_OUTPUT_FLAG_DIRECT) ||
870 (flags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) ||
871 (flags & AUDIO_OUTPUT_FLAG_PRIMARY))
872 return true;
873 else
874 return false;
875}
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800876
877static inline bool audio_is_vr_mode_on(struct audio_device *(__attribute__((unused)) adev))
878{
879 return adev->vr_audio_mode_enabled;
880}
881
Derek Chenf7092792017-05-23 12:23:53 -0400882static void audio_extn_btsco_get_sample_rate(int snd_device, int *sample_rate)
883{
884 switch (snd_device) {
885 case SND_DEVICE_OUT_BT_SCO:
886 case SND_DEVICE_IN_BT_SCO_MIC:
887 case SND_DEVICE_IN_BT_SCO_MIC_NREC:
888 *sample_rate = 8000;
889 break;
890 case SND_DEVICE_OUT_BT_SCO_WB:
891 case SND_DEVICE_IN_BT_SCO_MIC_WB:
892 case SND_DEVICE_IN_BT_SCO_MIC_WB_NREC:
893 *sample_rate = 16000;
894 break;
895 default:
Aniket Kumar Latad13758f2020-08-06 15:11:36 -0700896 ALOGV("%s:Not a BT SCO device, need not update sampling rate\n", __func__);
Derek Chenf7092792017-05-23 12:23:53 -0400897 break;
898 }
899}
900
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530901void audio_extn_utils_update_stream_app_type_cfg_for_usecase(
902 struct audio_device *adev,
903 struct audio_usecase *usecase)
904{
905 ALOGV("%s", __func__);
906
907 switch(usecase->type) {
908 case PCM_PLAYBACK:
909 audio_extn_utils_update_stream_output_app_type_cfg(adev->platform,
910 &adev->streams_output_cfg_list,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -0800911 &usecase->stream.out->device_list,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530912 usecase->stream.out->flags,
Aalique Grahame65780b52017-09-27 14:59:56 -0700913 usecase->stream.out->hal_op_format,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530914 usecase->stream.out->sample_rate,
915 usecase->stream.out->bit_width,
916 usecase->stream.out->channel_mask,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530917 usecase->stream.out->profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530918 &usecase->stream.out->app_type_cfg);
919 ALOGV("%s Selected apptype: %d", __func__, usecase->stream.out->app_type_cfg.app_type);
920 break;
921 case PCM_CAPTURE:
Revathi Uddarajud9f23d92020-07-27 10:55:06 +0530922 if (usecase->id == USECASE_AUDIO_RECORD_VOIP
923 || usecase->id == USECASE_AUDIO_RECORD_VOIP_LOW_LATENCY)
Vikram Panduranga93f080e2017-06-07 18:16:14 -0700924 usecase->stream.in->app_type_cfg.app_type = APP_TYPE_VOIP_AUDIO;
925 else
926 audio_extn_utils_update_stream_input_app_type_cfg(adev->platform,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530927 &adev->streams_input_cfg_list,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -0800928 &usecase->stream.in->device_list,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530929 usecase->stream.in->flags,
930 usecase->stream.in->format,
931 usecase->stream.in->sample_rate,
932 usecase->stream.in->bit_width,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530933 usecase->stream.in->profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530934 &usecase->stream.in->app_type_cfg);
935 ALOGV("%s Selected apptype: %d", __func__, usecase->stream.in->app_type_cfg.app_type);
936 break;
Surendar Karka93cd25a2018-08-28 14:21:37 +0530937 case TRANSCODE_LOOPBACK_RX :
Siddartha Shaik343abc62017-08-08 11:15:25 +0530938 audio_extn_utils_update_stream_output_app_type_cfg(adev->platform,
939 &adev->streams_output_cfg_list,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -0800940 &usecase->stream.inout->out_config.device_list,
Siddartha Shaik343abc62017-08-08 11:15:25 +0530941 0,
942 usecase->stream.inout->out_config.format,
943 usecase->stream.inout->out_config.sample_rate,
944 usecase->stream.inout->out_config.bit_width,
945 usecase->stream.inout->out_config.channel_mask,
946 usecase->stream.inout->profile,
947 &usecase->stream.inout->out_app_type_cfg);
948 ALOGV("%s Selected apptype: %d", __func__, usecase->stream.inout->out_app_type_cfg.app_type);
949 break;
Derek Chenf7092792017-05-23 12:23:53 -0400950 case PCM_HFP_CALL:
951 switch (usecase->id) {
952 case USECASE_AUDIO_HFP_SCO:
953 case USECASE_AUDIO_HFP_SCO_WB:
954 audio_extn_btsco_get_sample_rate(usecase->out_snd_device,
955 &usecase->out_app_type_cfg.sample_rate);
Derek Chend7307012019-11-12 12:10:41 -0500956 usecase->in_app_type_cfg.sample_rate = usecase->out_app_type_cfg.sample_rate;
Derek Chenf7092792017-05-23 12:23:53 -0400957 break;
958 case USECASE_AUDIO_HFP_SCO_DOWNLINK:
959 case USECASE_AUDIO_HFP_SCO_WB_DOWNLINK:
960 audio_extn_btsco_get_sample_rate(usecase->in_snd_device,
961 &usecase->in_app_type_cfg.sample_rate);
Derek Chend7307012019-11-12 12:10:41 -0500962 usecase->out_app_type_cfg.sample_rate = usecase->in_app_type_cfg.sample_rate;
Derek Chenf7092792017-05-23 12:23:53 -0400963 break;
964 default:
965 ALOGE("%s: usecase id (%d) not supported, use default sample rate",
966 __func__, usecase->id);
967 usecase->in_app_type_cfg.sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
968 usecase->out_app_type_cfg.sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
969 break;
970 }
971 /* update out_app_type_cfg */
972 usecase->out_app_type_cfg.bit_width =
973 platform_get_snd_device_bit_width(usecase->out_snd_device);
974 usecase->out_app_type_cfg.app_type =
975 platform_get_default_app_type_v2(adev->platform, PCM_PLAYBACK);
976 /* update in_app_type_cfg */
977 usecase->in_app_type_cfg.bit_width =
978 platform_get_snd_device_bit_width(usecase->in_snd_device);
979 usecase->in_app_type_cfg.app_type =
980 platform_get_default_app_type_v2(adev->platform, PCM_CAPTURE);
981 ALOGV("%s Selected apptype: playback %d capture %d",
982 __func__, usecase->out_app_type_cfg.app_type, usecase->in_app_type_cfg.app_type);
983 break;
Derek Chena30a5f42019-12-03 11:17:09 -0500984 case ICC_CALL:
985 /* ICC usecase: Loopback from TERT_TDM_TX to TERT_TDM_RX */
986 /* update out_app_type_cfg */
987 usecase->out_app_type_cfg.sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
988 usecase->out_app_type_cfg.bit_width = platform_get_snd_device_bit_width(usecase->out_snd_device);
989 usecase->out_app_type_cfg.app_type = platform_get_default_app_type_v2(adev->platform, PCM_PLAYBACK);
990 /* update in_app_type_cfg */
991 usecase->in_app_type_cfg.sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
992 usecase->in_app_type_cfg.bit_width = platform_get_snd_device_bit_width(usecase->in_snd_device);
993 usecase->in_app_type_cfg.app_type = platform_get_default_app_type_v2(adev->platform, PCM_CAPTURE);
994
995 ALOGV("%s Selected apptype: playback %d capture %d",
996 __func__, usecase->out_app_type_cfg.app_type, usecase->in_app_type_cfg.app_type);
997 break;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530998 default:
999 ALOGE("%s: app type cfg not supported for usecase type (%d)",
1000 __func__, usecase->type);
1001 }
1002}
1003
Aalique Grahame22e49102018-12-18 14:23:57 -08001004static int set_stream_app_type_mixer_ctrl(struct audio_device *adev,
1005 int pcm_device_id, int app_type,
1006 int acdb_dev_id, int sample_rate,
1007 int stream_type,
1008 snd_device_t snd_device)
1009{
1010
1011 char mixer_ctl_name[MAX_LENGTH_MIXER_CONTROL_IN_INT];
1012 struct mixer_ctl *ctl;
1013 int app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT], len = 0, rc = 0;
1014 int snd_device_be_idx = -1;
1015
1016 if (stream_type == PCM_PLAYBACK) {
1017 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
1018 "Audio Stream %d App Type Cfg", pcm_device_id);
1019 } else if (stream_type == PCM_CAPTURE) {
1020 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
1021 "Audio Stream Capture %d App Type Cfg", pcm_device_id);
1022 }
1023
1024 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
1025 if (!ctl) {
1026 ALOGE("%s: Could not get ctl for mixer cmd - %s",
1027 __func__, mixer_ctl_name);
1028 rc = -EINVAL;
1029 goto exit;
1030 }
1031 app_type_cfg[len++] = app_type;
1032 app_type_cfg[len++] = acdb_dev_id;
1033 app_type_cfg[len++] = sample_rate;
1034
1035 snd_device_be_idx = platform_get_snd_device_backend_index(snd_device);
1036 if (snd_device_be_idx > 0)
1037 app_type_cfg[len++] = snd_device_be_idx;
1038 ALOGV("%s: stream type %d app_type %d, acdb_dev_id %d "
1039 "sample rate %d, snd_device_be_idx %d",
1040 __func__, stream_type, app_type, acdb_dev_id, sample_rate,
1041 snd_device_be_idx);
1042 mixer_ctl_set_array(ctl, app_type_cfg, len);
1043
1044exit:
1045 return rc;
1046}
1047
1048static int audio_extn_utils_send_app_type_cfg_hfp(struct audio_device *adev,
1049 struct audio_usecase *usecase)
1050{
1051 int pcm_device_id, acdb_dev_id = 0, snd_device = usecase->out_snd_device;
1052 int32_t sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
1053 int app_type = 0, rc = 0;
Derek Chenf7092792017-05-23 12:23:53 -04001054 bool is_bus_dev_usecase = false;
Aalique Grahame22e49102018-12-18 14:23:57 -08001055
1056 ALOGV("%s", __func__);
1057
1058 if (usecase->type != PCM_HFP_CALL) {
Derek Chenf7092792017-05-23 12:23:53 -04001059 ALOGV("%s: not a HFP path, no need to cfg app type", __func__);
Aalique Grahame22e49102018-12-18 14:23:57 -08001060 rc = 0;
1061 goto exit_send_app_type_cfg;
1062 }
1063 if ((usecase->id != USECASE_AUDIO_HFP_SCO) &&
Derek Chenf7092792017-05-23 12:23:53 -04001064 (usecase->id != USECASE_AUDIO_HFP_SCO_WB) &&
1065 (usecase->id != USECASE_AUDIO_HFP_SCO_DOWNLINK) &&
1066 (usecase->id != USECASE_AUDIO_HFP_SCO_WB_DOWNLINK)) {
1067 ALOGV("%s: a usecase where app type cfg is not required", __func__);
Aalique Grahame22e49102018-12-18 14:23:57 -08001068 rc = 0;
1069 goto exit_send_app_type_cfg;
1070 }
1071
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08001072 if (compare_device_type(&usecase->device_list, AUDIO_DEVICE_OUT_BUS))
Derek Chenf7092792017-05-23 12:23:53 -04001073 is_bus_dev_usecase = true;
1074
Aalique Grahame22e49102018-12-18 14:23:57 -08001075 snd_device = usecase->out_snd_device;
1076 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_PLAYBACK);
1077
1078 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
1079 if (acdb_dev_id < 0) {
1080 ALOGE("%s: Couldn't get the acdb dev id", __func__);
1081 rc = -EINVAL;
1082 goto exit_send_app_type_cfg;
1083 }
1084
1085 if (usecase->type == PCM_HFP_CALL) {
1086
1087 /* config HFP session:1 playback path */
Derek Chenf7092792017-05-23 12:23:53 -04001088 if (is_bus_dev_usecase) {
1089 app_type = usecase->out_app_type_cfg.app_type;
1090 sample_rate= usecase->out_app_type_cfg.sample_rate;
1091 } else {
1092 snd_device = SND_DEVICE_NONE; // use legacy behavior
1093 app_type = platform_get_default_app_type_v2(adev->platform, PCM_PLAYBACK);
1094 sample_rate= CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
1095 }
Aalique Grahame22e49102018-12-18 14:23:57 -08001096 rc = set_stream_app_type_mixer_ctrl(adev, pcm_device_id, app_type,
1097 acdb_dev_id, sample_rate,
1098 PCM_PLAYBACK,
Derek Chenf7092792017-05-23 12:23:53 -04001099 snd_device);
Aalique Grahame22e49102018-12-18 14:23:57 -08001100 if (rc < 0)
1101 goto exit_send_app_type_cfg;
1102
1103 /* config HFP session:1 capture path */
Derek Chenf7092792017-05-23 12:23:53 -04001104 if (is_bus_dev_usecase) {
1105 snd_device = usecase->in_snd_device;
1106 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_CAPTURE);
1107 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
1108 if (acdb_dev_id < 0) {
1109 ALOGE("%s: Couldn't get the acdb dev id", __func__);
1110 rc = -EINVAL;
1111 goto exit_send_app_type_cfg;
1112 }
1113 app_type = usecase->in_app_type_cfg.app_type;
1114 sample_rate= usecase->in_app_type_cfg.sample_rate;
1115 } else {
1116 snd_device = SND_DEVICE_NONE; // use legacy behavior
1117 app_type = platform_get_default_app_type_v2(adev->platform, PCM_CAPTURE);
1118 }
Aalique Grahame22e49102018-12-18 14:23:57 -08001119 rc = set_stream_app_type_mixer_ctrl(adev, pcm_device_id, app_type,
1120 acdb_dev_id, sample_rate,
1121 PCM_CAPTURE,
Derek Chenf7092792017-05-23 12:23:53 -04001122 snd_device);
Aalique Grahame22e49102018-12-18 14:23:57 -08001123 if (rc < 0)
1124 goto exit_send_app_type_cfg;
1125
Derek Chenf7092792017-05-23 12:23:53 -04001126 if (is_bus_dev_usecase)
1127 goto exit_send_app_type_cfg;
1128
Aalique Grahame22e49102018-12-18 14:23:57 -08001129 /* config HFP session:2 capture path */
1130 pcm_device_id = HFP_ASM_RX_TX;
1131 snd_device = usecase->in_snd_device;
1132 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
1133 if (acdb_dev_id <= 0) {
1134 ALOGE("%s: Couldn't get the acdb dev id", __func__);
1135 rc = -EINVAL;
1136 goto exit_send_app_type_cfg;
1137 }
1138 app_type = platform_get_default_app_type_v2(adev->platform, PCM_CAPTURE);
1139 rc = set_stream_app_type_mixer_ctrl(adev, pcm_device_id, app_type,
1140 acdb_dev_id, sample_rate, PCM_CAPTURE,
1141 SND_DEVICE_NONE);
1142 if (rc < 0)
1143 goto exit_send_app_type_cfg;
1144
1145 /* config HFP session:2 playback path */
1146 app_type = platform_get_default_app_type_v2(adev->platform, PCM_PLAYBACK);
1147 rc = set_stream_app_type_mixer_ctrl(adev, pcm_device_id, app_type,
1148 acdb_dev_id, sample_rate,
1149 PCM_PLAYBACK, SND_DEVICE_NONE);
1150 if (rc < 0)
1151 goto exit_send_app_type_cfg;
1152 }
1153
1154 rc = 0;
1155exit_send_app_type_cfg:
1156 return rc;
1157}
1158
Derek Chena30a5f42019-12-03 11:17:09 -05001159int audio_extn_utils_send_app_type_cfg_icc(struct audio_device *adev,
1160 struct audio_usecase *usecase)
1161{
1162 int pcm_device_id, acdb_dev_id = 0, snd_device = usecase->out_snd_device;
1163 int32_t sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
1164 int app_type = 0, rc = 0;
1165
1166 ALOGV("%s", __func__);
1167
1168 if (usecase->type != ICC_CALL) {
1169 ALOGV("%s: not an ICC path, no need to cfg app type", __func__);
1170 rc = 0;
1171 goto exit_send_app_type_cfg;
1172 }
1173 if (usecase->id != USECASE_ICC_CALL) {
1174 ALOGV("%s: a usecase where app type cfg is not required", __func__);
1175 rc = 0;
1176 goto exit_send_app_type_cfg;
1177 }
1178
1179 snd_device = usecase->out_snd_device;
1180 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_PLAYBACK);
1181 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
1182 if (acdb_dev_id < 0) {
1183 ALOGE("%s: Couldn't get the acdb dev id", __func__);
1184 rc = -EINVAL;
1185 goto exit_send_app_type_cfg;
1186 }
1187 /* config ICC session: playback path */
1188 app_type = usecase->out_app_type_cfg.app_type;
1189 sample_rate= usecase->out_app_type_cfg.sample_rate;
1190
1191 rc = set_stream_app_type_mixer_ctrl(adev, pcm_device_id, app_type,
1192 acdb_dev_id, sample_rate,
1193 PCM_PLAYBACK,
1194 snd_device);
1195 if (rc < 0)
1196 goto exit_send_app_type_cfg;
1197
1198 /* config ICC session: capture path */
1199 snd_device = usecase->in_snd_device;
1200 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_CAPTURE);
1201 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
1202 if (acdb_dev_id < 0) {
1203 ALOGE("%s: Couldn't get the acdb dev id", __func__);
1204 rc = -EINVAL;
1205 goto exit_send_app_type_cfg;
1206 }
1207 app_type = usecase->in_app_type_cfg.app_type;
1208 sample_rate= usecase->in_app_type_cfg.sample_rate;
1209 rc = set_stream_app_type_mixer_ctrl(adev, pcm_device_id, app_type,
1210 acdb_dev_id, sample_rate,
1211 PCM_CAPTURE,
1212 snd_device);
1213exit_send_app_type_cfg:
1214 return rc;
1215}
1216
Zhou Song06761dd2019-04-28 18:08:17 +08001217int audio_extn_utils_get_app_sample_rate_for_device(
1218 struct audio_device *adev,
1219 struct audio_usecase *usecase, int snd_device)
1220{
1221 char value[PROPERTY_VALUE_MAX] = {0};
1222 int sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
1223
1224 if ((usecase->type == PCM_PLAYBACK) && (usecase->stream.out != NULL)) {
1225 property_get("vendor.audio.playback.mch.downsample",value,"");
1226 if (!strncmp("true", value, sizeof("true"))) {
1227 if ((popcount(usecase->stream.out->channel_mask) > 2) &&
1228 (usecase->stream.out->app_type_cfg.sample_rate > CODEC_BACKEND_DEFAULT_SAMPLE_RATE) &&
1229 !(usecase->stream.out->flags &
1230 (audio_output_flags_t)AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH))
1231 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
1232 }
1233
1234 if (usecase->id == USECASE_AUDIO_PLAYBACK_VOIP) {
1235 usecase->stream.out->app_type_cfg.sample_rate = usecase->stream.out->sample_rate;
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08001236 } else if (compare_device_type(&usecase->stream.out->device_list,
1237 AUDIO_DEVICE_OUT_SPEAKER)) {
Zhou Song06761dd2019-04-28 18:08:17 +08001238 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
1239 } else if ((snd_device == SND_DEVICE_OUT_HDMI ||
1240 snd_device == SND_DEVICE_OUT_USB_HEADSET ||
1241 snd_device == SND_DEVICE_OUT_DISPLAY_PORT) &&
1242 (usecase->stream.out->sample_rate >= OUTPUT_SAMPLING_RATE_44100)) {
1243 /*
1244 * To best utlize DSP, check if the stream sample rate is supported/multiple of
1245 * configured device sample rate, if not update the COPP rate to be equal to the
1246 * device sample rate, else open COPP at stream sample rate
1247 */
1248 platform_check_and_update_copp_sample_rate(adev->platform, snd_device,
1249 usecase->stream.out->sample_rate,
1250 &usecase->stream.out->app_type_cfg.sample_rate);
1251 } else if (((snd_device != SND_DEVICE_OUT_HEADPHONES_44_1 &&
1252 !audio_is_this_native_usecase(usecase)) &&
1253 usecase->stream.out->sample_rate == OUTPUT_SAMPLING_RATE_44100) ||
1254 (usecase->stream.out->sample_rate < OUTPUT_SAMPLING_RATE_44100)) {
1255 /* Reset to default if no native stream is active*/
1256 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Zhou Song9e9b3812019-12-30 17:52:10 +08001257 } else if (usecase->out_snd_device == SND_DEVICE_OUT_BT_A2DP ||
1258 usecase->out_snd_device == SND_DEVICE_OUT_SPEAKER_AND_BT_A2DP ||
1259 usecase->out_snd_device == SND_DEVICE_OUT_SPEAKER_SAFE_AND_BT_A2DP) {
Zhou Song06761dd2019-04-28 18:08:17 +08001260 /*
1261 * For a2dp playback get encoder sampling rate and set copp sampling rate,
1262 * for bit width use the stream param only.
1263 */
1264 audio_extn_a2dp_get_enc_sample_rate(&usecase->stream.out->app_type_cfg.sample_rate);
1265 ALOGI("%s using %d sample rate rate for A2DP CoPP",
1266 __func__, usecase->stream.out->app_type_cfg.sample_rate);
1267 }
1268 audio_extn_btsco_get_sample_rate(snd_device, &usecase->stream.out->app_type_cfg.sample_rate);
1269 sample_rate = usecase->stream.out->app_type_cfg.sample_rate;
1270
1271 if (((usecase->stream.out->format == AUDIO_FORMAT_E_AC3) ||
1272 (usecase->stream.out->format == AUDIO_FORMAT_E_AC3_JOC) ||
1273 (usecase->stream.out->format == AUDIO_FORMAT_DOLBY_TRUEHD))
1274 && audio_extn_passthru_is_passthrough_stream(usecase->stream.out)
1275 && !audio_extn_passthru_is_convert_supported(adev, usecase->stream.out)) {
1276 sample_rate = sample_rate * 4;
1277 if (sample_rate > HDMI_PASSTHROUGH_MAX_SAMPLE_RATE)
1278 sample_rate = HDMI_PASSTHROUGH_MAX_SAMPLE_RATE;
1279 }
1280 } else if (usecase->type == PCM_CAPTURE) {
1281 if (usecase->stream.in != NULL) {
Revathi Uddarajud9f23d92020-07-27 10:55:06 +05301282 if (usecase->id == USECASE_AUDIO_RECORD_VOIP
1283 || usecase->id == USECASE_AUDIO_RECORD_VOIP_LOW_LATENCY)
Zhou Song06761dd2019-04-28 18:08:17 +08001284 usecase->stream.in->app_type_cfg.sample_rate = usecase->stream.in->sample_rate;
1285 if (voice_is_in_call_rec_stream(usecase->stream.in)) {
1286 audio_extn_btsco_get_sample_rate(usecase->in_snd_device,
1287 &usecase->stream.in->app_type_cfg.sample_rate);
1288 } else {
1289 audio_extn_btsco_get_sample_rate(snd_device,
1290 &usecase->stream.in->app_type_cfg.sample_rate);
1291 }
1292 sample_rate = usecase->stream.in->app_type_cfg.sample_rate;
1293 } else if (usecase->id == USECASE_AUDIO_SPKR_CALIB_TX) {
Sujin Panickerb6d76262019-09-09 10:55:38 +05301294 if ((property_get("vendor.audio.spkr_prot.tx.sampling_rate", value, NULL) > 0))
1295 sample_rate = atoi(value);
1296 else
1297 sample_rate = SAMPLE_RATE_8000;
Zhou Song06761dd2019-04-28 18:08:17 +08001298 }
1299 } else if (usecase->type == TRANSCODE_LOOPBACK_RX) {
1300 sample_rate = usecase->stream.inout->out_config.sample_rate;
1301 }
1302 return sample_rate;
1303}
1304
Siena Richard7c2db772016-12-21 11:32:34 -08001305static int send_app_type_cfg_for_device(struct audio_device *adev,
1306 struct audio_usecase *usecase,
1307 int split_snd_device)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001308{
1309 char mixer_ctl_name[MAX_LENGTH_MIXER_CONTROL_IN_INT];
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301310 size_t app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT] = {0};
1311 int len = 0, rc;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001312 struct mixer_ctl *ctl;
Siena Richard7c2db772016-12-21 11:32:34 -08001313 int pcm_device_id = 0, acdb_dev_id, app_type;
1314 int snd_device = split_snd_device, snd_device_be_idx = -1;
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +05301315 int32_t sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Varun Balaraje49253e2017-07-06 19:48:56 +05301316 struct streams_io_cfg *s_info = NULL;
1317 struct listnode *node = NULL;
Nikhil Laturkarac4a7492017-08-31 18:27:19 +05301318 int bd_app_type = 0;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001319
Siena Richard7c2db772016-12-21 11:32:34 -08001320 ALOGV("%s: usecase->out_snd_device %s, usecase->in_snd_device %s, split_snd_device %s",
1321 __func__, platform_get_snd_device_name(usecase->out_snd_device),
1322 platform_get_snd_device_name(usecase->in_snd_device),
1323 platform_get_snd_device_name(split_snd_device));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001324
Siddartha Shaik343abc62017-08-08 11:15:25 +05301325 if (usecase->type != PCM_PLAYBACK && usecase->type != PCM_CAPTURE &&
Surendar Karka93cd25a2018-08-28 14:21:37 +05301326 usecase->type != TRANSCODE_LOOPBACK_RX) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301327 ALOGE("%s: not a playback/capture path, no need to cfg app type", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001328 rc = 0;
1329 goto exit_send_app_type_cfg;
1330 }
1331 if ((usecase->id != USECASE_AUDIO_PLAYBACK_DEEP_BUFFER) &&
Vignesh Kulothungan0a2eff02019-07-11 16:30:13 -07001332 (usecase->id != USECASE_AUDIO_PLAYBACK_WITH_HAPTICS) &&
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001333 (usecase->id != USECASE_AUDIO_PLAYBACK_LOW_LATENCY) &&
1334 (usecase->id != USECASE_AUDIO_PLAYBACK_MULTI_CH) &&
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -08001335 (usecase->id != USECASE_AUDIO_PLAYBACK_ULL) &&
Vikram Panduranga93f080e2017-06-07 18:16:14 -07001336 (usecase->id != USECASE_AUDIO_PLAYBACK_VOIP) &&
Surendar Karka93cd25a2018-08-28 14:21:37 +05301337 (usecase->id != USECASE_AUDIO_TRANSCODE_LOOPBACK_RX) &&
Varun Balaraje49253e2017-07-06 19:48:56 +05301338 (!is_interactive_usecase(usecase->id)) &&
Srikanth Uyyala9d551402015-08-25 16:03:42 +05301339 (!is_offload_usecase(usecase->id)) &&
Derek Chenf6318be2017-06-12 17:16:24 -04001340 (usecase->type != PCM_CAPTURE) &&
1341 (!audio_extn_auto_hal_is_bus_device_usecase(usecase->id))) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301342 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 -07001343 rc = 0;
1344 goto exit_send_app_type_cfg;
1345 }
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -08001346
1347 //if VR is active then only send the mixer control
1348 if (usecase->id == USECASE_AUDIO_PLAYBACK_ULL && !audio_is_vr_mode_on(adev)) {
1349 ALOGI("ULL doesnt need sending app type cfg, returning");
1350 rc = 0;
1351 goto exit_send_app_type_cfg;
1352 }
1353
Surendar Karka93cd25a2018-08-28 14:21:37 +05301354 if (usecase->type == PCM_PLAYBACK || usecase->type == TRANSCODE_LOOPBACK_RX) {
Ben Romberger1fafdde2015-09-09 19:43:15 -07001355 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_PLAYBACK);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301356 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
1357 "Audio Stream %d App Type Cfg", pcm_device_id);
Ben Romberger1fafdde2015-09-09 19:43:15 -07001358 } else if (usecase->type == PCM_CAPTURE) {
Ben Romberger1fafdde2015-09-09 19:43:15 -07001359 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_CAPTURE);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301360 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
1361 "Audio Stream Capture %d App Type Cfg", pcm_device_id);
Srikanth Uyyala9d551402015-08-25 16:03:42 +05301362 }
Siena Richard7c2db772016-12-21 11:32:34 -08001363
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001364 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
1365 if (!ctl) {
1366 ALOGE("%s: Could not get ctl for mixer cmd - %s", __func__,
1367 mixer_ctl_name);
1368 rc = -EINVAL;
1369 goto exit_send_app_type_cfg;
1370 }
Aditya Bavanari701a6992017-03-30 19:17:16 +05301371 snd_device = platform_get_spkr_prot_snd_device(snd_device);
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301372 if (voice_is_in_call_rec_stream(usecase->stream.in) && usecase->type == PCM_CAPTURE) {
1373 snd_device_t voice_device = voice_get_incall_rec_snd_device(usecase->in_snd_device);
1374 acdb_dev_id = platform_get_snd_device_acdb_id(voice_device);
1375 ALOGV("acdb id for voice call use case %d", acdb_dev_id);
1376 } else {
1377 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
1378 }
Rohit Kumar1181b292017-01-31 18:07:17 +05301379 if (acdb_dev_id <= 0) {
1380 ALOGE("%s: Couldn't get the acdb dev id", __func__);
1381 rc = -EINVAL;
1382 goto exit_send_app_type_cfg;
1383 }
1384
Siena Richard7c2db772016-12-21 11:32:34 -08001385 snd_device_be_idx = platform_get_snd_device_backend_index(snd_device);
1386 if (snd_device_be_idx < 0) {
1387 ALOGE("%s: Couldn't get the backend index for snd device %s ret=%d",
1388 __func__, platform_get_snd_device_name(snd_device),
1389 snd_device_be_idx);
1390 }
1391
Zhou Song06761dd2019-04-28 18:08:17 +08001392 sample_rate = audio_extn_utils_get_app_sample_rate_for_device(adev, usecase, snd_device);
1393
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301394 if ((usecase->type == PCM_PLAYBACK) && (usecase->stream.out != NULL)) {
Varun Balaraje49253e2017-07-06 19:48:56 +05301395 /* Interactive streams are supported with only direct app type id.
1396 * Get Direct profile app type and use it for interactive streams
1397 */
1398 list_for_each(node, &adev->streams_output_cfg_list) {
1399 s_info = node_to_item(node, struct streams_io_cfg, list);
Aalique Grahame5cd12ff2017-10-19 10:57:00 -07001400 if (s_info->flags.out_flags == (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_BD |
Nikhil Laturkarac4a7492017-08-31 18:27:19 +05301401 AUDIO_OUTPUT_FLAG_DIRECT_PCM |
1402 AUDIO_OUTPUT_FLAG_DIRECT))
1403 bd_app_type = s_info->app_type_cfg.app_type;
Varun Balaraje49253e2017-07-06 19:48:56 +05301404 }
Aalique Grahame5cd12ff2017-10-19 10:57:00 -07001405 if (usecase->stream.out->flags == (audio_output_flags_t)AUDIO_OUTPUT_FLAG_INTERACTIVE)
Nikhil Laturkarac4a7492017-08-31 18:27:19 +05301406 app_type = bd_app_type;
Varun Balaraje49253e2017-07-06 19:48:56 +05301407 else
1408 app_type = usecase->stream.out->app_type_cfg.app_type;
Siena Richard7c2db772016-12-21 11:32:34 -08001409 app_type_cfg[len++] = app_type;
Mingming Yin21854652016-04-13 11:54:02 -07001410 app_type_cfg[len++] = acdb_dev_id;
Naresh Tanniru3a406772017-05-10 13:09:05 -07001411 app_type_cfg[len++] = sample_rate;
1412
Siena Richard7c2db772016-12-21 11:32:34 -08001413 if (snd_device_be_idx > 0)
1414 app_type_cfg[len++] = snd_device_be_idx;
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301415
Siena Richard7c2db772016-12-21 11:32:34 -08001416 ALOGI("%s PLAYBACK app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
1417 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301418
1419 } else if ((usecase->type == PCM_CAPTURE) && (usecase->stream.in != NULL)) {
Siena Richard7c2db772016-12-21 11:32:34 -08001420 app_type = usecase->stream.in->app_type_cfg.app_type;
1421 app_type_cfg[len++] = app_type;
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301422 app_type_cfg[len++] = acdb_dev_id;
Siena Richard7c2db772016-12-21 11:32:34 -08001423 app_type_cfg[len++] = sample_rate;
1424 if (snd_device_be_idx > 0)
1425 app_type_cfg[len++] = snd_device_be_idx;
1426 ALOGI("%s CAPTURE app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
1427 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301428 } else {
Siena Richard7c2db772016-12-21 11:32:34 -08001429 app_type = platform_get_default_app_type_v2(adev->platform, usecase->type);
Surendar Karka93cd25a2018-08-28 14:21:37 +05301430 if(usecase->type == TRANSCODE_LOOPBACK_RX) {
Siddartha Shaik343abc62017-08-08 11:15:25 +05301431 app_type = usecase->stream.inout->out_app_type_cfg.app_type;
1432 }
Siena Richard7c2db772016-12-21 11:32:34 -08001433 app_type_cfg[len++] = app_type;
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301434 app_type_cfg[len++] = acdb_dev_id;
1435 app_type_cfg[len++] = sample_rate;
Siena Richard7c2db772016-12-21 11:32:34 -08001436 if (snd_device_be_idx > 0)
1437 app_type_cfg[len++] = snd_device_be_idx;
1438 ALOGI("%s default app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
1439 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Ashish Jainc02430d2016-01-04 10:42:43 +05301440 }
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301441
Siddartha Shaik343abc62017-08-08 11:15:25 +05301442 if(ctl)
1443 mixer_ctl_set_array(ctl, app_type_cfg, len);
Zhou Songd9bd9302020-08-04 16:34:45 +08001444
1445 /* send app type cfg for haptics */
1446 if (usecase->id == USECASE_AUDIO_PLAYBACK_WITH_HAPTICS) {
1447 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
1448 "Audio Stream %d App Type Cfg", adev->haptic_pcm_device_id );
1449 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
1450 if (!ctl) {
1451 ALOGE("%s: Could not get ctl for mixer cmd - %s", __func__,
1452 mixer_ctl_name);
1453 rc = -EINVAL;
1454 goto exit_send_app_type_cfg;
1455 }
1456 acdb_dev_id = platform_get_snd_device_acdb_id(SND_DEVICE_OUT_HAPTICS);
1457 snd_device_be_idx = platform_get_snd_device_backend_index(SND_DEVICE_OUT_HAPTICS);
1458 /* haptics acdb id */
1459 app_type_cfg[1] = acdb_dev_id;
1460 /* haptics be index */
1461 app_type_cfg[3] = snd_device_be_idx;
1462 mixer_ctl_set_array(ctl, app_type_cfg, len);
1463 }
1464
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001465 rc = 0;
1466exit_send_app_type_cfg:
1467 return rc;
1468}
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001469
Deeraj Soman14230922019-01-30 16:39:30 +05301470static int audio_extn_utils_check_input_parameters(uint32_t sample_rate,
1471 audio_format_t format,
1472 int channel_count)
1473{
1474 int ret = 0;
1475
1476 if (((format != AUDIO_FORMAT_PCM_16_BIT) && (format != AUDIO_FORMAT_PCM_8_24_BIT) &&
1477 (format != AUDIO_FORMAT_PCM_24_BIT_PACKED) && (format != AUDIO_FORMAT_PCM_32_BIT) &&
1478 (format != AUDIO_FORMAT_PCM_FLOAT)) &&
1479 !voice_extn_compress_voip_is_format_supported(format) &&
1480 !audio_extn_compr_cap_format_supported(format) &&
1481 !audio_extn_cin_format_supported(format))
1482 ret = -EINVAL;
1483
1484 switch (channel_count) {
1485 case 1:
1486 case 2:
1487 case 3:
1488 case 4:
1489 case 6:
1490 case 8:
1491 break;
1492 default:
1493 ret = -EINVAL;
1494 }
1495
1496 switch (sample_rate) {
1497 case 8000:
1498 case 11025:
1499 case 12000:
1500 case 16000:
1501 case 22050:
1502 case 24000:
1503 case 32000:
1504 case 44100:
1505 case 48000:
1506 case 88200:
1507 case 96000:
1508 case 176400:
1509 case 192000:
1510 break;
1511 default:
1512 ret = -EINVAL;
1513 }
1514
1515 return ret;
1516}
1517
1518static inline uint32_t audio_extn_utils_nearest_multiple(uint32_t num, uint32_t multiplier)
1519{
1520 uint32_t remainder = 0;
1521
1522 if (!multiplier)
1523 return num;
1524
1525 remainder = num % multiplier;
1526 if (remainder)
1527 num += (multiplier - remainder);
1528
1529 return num;
1530}
1531
1532static inline uint32_t audio_extn_utils_lcm(uint32_t num1, uint32_t num2)
1533{
1534 uint32_t high = num1, low = num2, temp = 0;
1535
1536 if (!num1 || !num2)
1537 return 0;
1538
1539 if (num1 < num2) {
1540 high = num2;
1541 low = num1;
1542 }
1543
1544 while (low != 0) {
1545 temp = low;
1546 low = high % low;
1547 high = temp;
1548 }
1549 return (num1 * num2)/high;
1550}
1551
Siena Richard7c2db772016-12-21 11:32:34 -08001552int audio_extn_utils_send_app_type_cfg(struct audio_device *adev,
1553 struct audio_usecase *usecase)
1554{
1555 int i, num_devices = 0;
1556 snd_device_t new_snd_devices[SND_DEVICE_OUT_END] = {0};
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301557 snd_device_t in_snd_device = usecase->in_snd_device;
Siena Richard7c2db772016-12-21 11:32:34 -08001558 int rc = 0;
1559
Aalique Grahame22e49102018-12-18 14:23:57 -08001560 if (usecase->type == PCM_HFP_CALL) {
1561 return audio_extn_utils_send_app_type_cfg_hfp(adev, usecase);
Derek Chena30a5f42019-12-03 11:17:09 -05001562 } else if (usecase->type == ICC_CALL) {
1563 return audio_extn_utils_send_app_type_cfg_icc(adev, usecase);
Aalique Grahame22e49102018-12-18 14:23:57 -08001564 }
1565
Siena Richard7c2db772016-12-21 11:32:34 -08001566 switch (usecase->type) {
1567 case PCM_PLAYBACK:
Surendar Karka93cd25a2018-08-28 14:21:37 +05301568 case TRANSCODE_LOOPBACK_RX:
Siena Richard7c2db772016-12-21 11:32:34 -08001569 ALOGD("%s: usecase->out_snd_device %s",
1570 __func__, platform_get_snd_device_name(usecase->out_snd_device));
1571 /* check for out combo device */
1572 if (platform_split_snd_device(adev->platform,
1573 usecase->out_snd_device,
1574 &num_devices, new_snd_devices)) {
1575 new_snd_devices[0] = usecase->out_snd_device;
1576 num_devices = 1;
1577 }
1578 break;
1579 case PCM_CAPTURE:
1580 ALOGD("%s: usecase->in_snd_device %s",
1581 __func__, platform_get_snd_device_name(usecase->in_snd_device));
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301582 if (voice_is_in_call_rec_stream(usecase->stream.in)) {
1583 in_snd_device = voice_get_incall_rec_backend_device(usecase->stream.in);
1584 }
Siena Richard7c2db772016-12-21 11:32:34 -08001585 /* check for in combo device */
1586 if (platform_split_snd_device(adev->platform,
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301587 in_snd_device,
Siena Richard7c2db772016-12-21 11:32:34 -08001588 &num_devices, new_snd_devices)) {
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301589 new_snd_devices[0] = in_snd_device;
Siena Richard7c2db772016-12-21 11:32:34 -08001590 num_devices = 1;
1591 }
1592 break;
1593 default:
1594 ALOGI("%s: not a playback/capture path, no need to cfg app type", __func__);
1595 rc = 0;
1596 break;
1597 }
1598
1599 for (i = 0; i < num_devices; i++) {
1600 rc = send_app_type_cfg_for_device(adev, usecase, new_snd_devices[i]);
1601 if (rc)
1602 break;
1603 }
1604
1605 return rc;
1606}
1607
Preetam Singh Ranawat9519e9c2015-11-18 16:05:55 +05301608int read_line_from_file(const char *path, char *buf, size_t count)
1609{
1610 char * fgets_ret;
1611 FILE * fd;
1612 int rv;
1613
1614 fd = fopen(path, "r");
1615 if (fd == NULL)
1616 return -1;
1617
1618 fgets_ret = fgets(buf, (int)count, fd);
1619 if (NULL != fgets_ret) {
1620 rv = (int)strlen(buf);
1621 } else {
1622 rv = ferror(fd);
1623 }
1624 fclose(fd);
1625
1626 return rv;
1627}
1628
Ashish Jainf1eaa582016-05-23 20:54:24 +05301629/*Translates ALSA formats to AOSP PCM formats*/
1630audio_format_t alsa_format_to_hal(uint32_t alsa_format)
1631{
1632 audio_format_t format;
1633
1634 switch(alsa_format) {
1635 case SNDRV_PCM_FORMAT_S16_LE:
1636 format = AUDIO_FORMAT_PCM_16_BIT;
1637 break;
1638 case SNDRV_PCM_FORMAT_S24_3LE:
1639 format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
1640 break;
1641 case SNDRV_PCM_FORMAT_S24_LE:
1642 format = AUDIO_FORMAT_PCM_8_24_BIT;
1643 break;
1644 case SNDRV_PCM_FORMAT_S32_LE:
1645 format = AUDIO_FORMAT_PCM_32_BIT;
1646 break;
1647 default:
1648 ALOGW("Incorrect ALSA format");
1649 format = AUDIO_FORMAT_INVALID;
1650 }
1651 return format;
1652}
1653
1654/*Translates hal format (AOSP) to alsa formats*/
1655uint32_t hal_format_to_alsa(audio_format_t hal_format)
1656{
1657 uint32_t alsa_format;
1658
1659 switch (hal_format) {
1660 case AUDIO_FORMAT_PCM_32_BIT: {
1661 if (platform_supports_true_32bit())
1662 alsa_format = SNDRV_PCM_FORMAT_S32_LE;
1663 else
1664 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1665 }
1666 break;
1667 case AUDIO_FORMAT_PCM_8_BIT:
1668 alsa_format = SNDRV_PCM_FORMAT_S8;
1669 break;
1670 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
1671 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1672 break;
1673 case AUDIO_FORMAT_PCM_8_24_BIT: {
1674 if (platform_supports_true_32bit())
1675 alsa_format = SNDRV_PCM_FORMAT_S32_LE;
1676 else
1677 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1678 }
1679 break;
1680 case AUDIO_FORMAT_PCM_FLOAT:
1681 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1682 break;
1683 default:
1684 case AUDIO_FORMAT_PCM_16_BIT:
1685 alsa_format = SNDRV_PCM_FORMAT_S16_LE;
1686 break;
1687 }
1688 return alsa_format;
1689}
1690
Ashish Jain83a6cc22016-06-28 14:34:17 +05301691/*Translates PCM formats to AOSP formats*/
1692audio_format_t pcm_format_to_hal(uint32_t pcm_format)
1693{
1694 audio_format_t format = AUDIO_FORMAT_INVALID;
1695
1696 switch(pcm_format) {
1697 case PCM_FORMAT_S16_LE:
1698 format = AUDIO_FORMAT_PCM_16_BIT;
1699 break;
1700 case PCM_FORMAT_S24_3LE:
1701 format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
1702 break;
1703 case PCM_FORMAT_S24_LE:
1704 format = AUDIO_FORMAT_PCM_8_24_BIT;
1705 break;
1706 case PCM_FORMAT_S32_LE:
1707 format = AUDIO_FORMAT_PCM_32_BIT;
1708 break;
1709 default:
1710 ALOGW("Incorrect PCM format");
1711 format = AUDIO_FORMAT_INVALID;
1712 }
1713 return format;
1714}
1715
1716/*Translates hal format (AOSP) to alsa formats*/
1717uint32_t hal_format_to_pcm(audio_format_t hal_format)
1718{
1719 uint32_t pcm_format;
1720
1721 switch (hal_format) {
1722 case AUDIO_FORMAT_PCM_32_BIT:
1723 case AUDIO_FORMAT_PCM_8_24_BIT:
1724 case AUDIO_FORMAT_PCM_FLOAT: {
1725 if (platform_supports_true_32bit())
1726 pcm_format = PCM_FORMAT_S32_LE;
1727 else
1728 pcm_format = PCM_FORMAT_S24_3LE;
1729 }
1730 break;
1731 case AUDIO_FORMAT_PCM_8_BIT:
1732 pcm_format = PCM_FORMAT_S8;
1733 break;
1734 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
1735 pcm_format = PCM_FORMAT_S24_3LE;
1736 break;
1737 default:
1738 case AUDIO_FORMAT_PCM_16_BIT:
1739 pcm_format = PCM_FORMAT_S16_LE;
1740 break;
1741 }
1742 return pcm_format;
1743}
1744
Ashish Jainf1eaa582016-05-23 20:54:24 +05301745uint32_t get_alsa_fragment_size(uint32_t bytes_per_sample,
1746 uint32_t sample_rate,
Deeraj Soman65358ab2019-02-07 15:40:49 +05301747 uint32_t noOfChannels,
1748 int64_t duration_ms)
Ashish Jainf1eaa582016-05-23 20:54:24 +05301749{
1750 uint32_t fragment_size = 0;
1751 uint32_t pcm_offload_time = PCM_OFFLOAD_BUFFER_DURATION;
1752
Deeraj Soman65358ab2019-02-07 15:40:49 +05301753 if (duration_ms >= MIN_OFFLOAD_BUFFER_DURATION_MS && duration_ms <= MAX_OFFLOAD_BUFFER_DURATION_MS)
1754 pcm_offload_time = duration_ms;
1755
Ashish Jainf1eaa582016-05-23 20:54:24 +05301756 fragment_size = (pcm_offload_time
1757 * sample_rate
1758 * bytes_per_sample
1759 * noOfChannels)/1000;
1760 if (fragment_size < MIN_PCM_OFFLOAD_FRAGMENT_SIZE)
1761 fragment_size = MIN_PCM_OFFLOAD_FRAGMENT_SIZE;
1762 else if (fragment_size > MAX_PCM_OFFLOAD_FRAGMENT_SIZE)
1763 fragment_size = MAX_PCM_OFFLOAD_FRAGMENT_SIZE;
1764 /*To have same PCM samples for all channels, the buffer size requires to
1765 *be multiple of (number of channels * bytes per sample)
1766 *For writes to succeed, the buffer must be written at address which is multiple of 32
1767 */
Aalique Grahameb85f2d92017-10-16 17:53:23 -07001768 fragment_size = ALIGN(fragment_size, (bytes_per_sample * noOfChannels * 32));
Ashish Jainf1eaa582016-05-23 20:54:24 +05301769
1770 ALOGI("PCM offload Fragment size to %d bytes", fragment_size);
1771 return fragment_size;
1772}
1773
1774/* Calculates the fragment size required to configure compress session.
1775 * Based on the alsa format selected, decide if conversion is needed in
1776
1777 * HAL ( e.g. convert AUDIO_FORMAT_PCM_FLOAT input format to
1778 * AUDIO_FORMAT_PCM_24_BIT_PACKED before writing to the compress driver.
1779 */
1780void audio_extn_utils_update_direct_pcm_fragment_size(struct stream_out *out)
1781{
Ashish Jain83a6cc22016-06-28 14:34:17 +05301782 audio_format_t dst_format = out->hal_op_format;
1783 audio_format_t src_format = out->hal_ip_format;
Ashish Jainf1eaa582016-05-23 20:54:24 +05301784 uint32_t hal_op_bytes_per_sample = audio_bytes_per_sample(dst_format);
1785 uint32_t hal_ip_bytes_per_sample = audio_bytes_per_sample(src_format);
1786
1787 out->compr_config.fragment_size =
1788 get_alsa_fragment_size(hal_op_bytes_per_sample,
1789 out->sample_rate,
Deeraj Soman65358ab2019-02-07 15:40:49 +05301790 popcount(out->channel_mask),
1791 out->info.duration_us / 1000);
Ashish Jainf1eaa582016-05-23 20:54:24 +05301792
1793 if ((src_format != dst_format) &&
1794 hal_op_bytes_per_sample != hal_ip_bytes_per_sample) {
1795
Ashish Jain83a6cc22016-06-28 14:34:17 +05301796 out->hal_fragment_size =
Ashish Jainf1eaa582016-05-23 20:54:24 +05301797 ((out->compr_config.fragment_size * hal_ip_bytes_per_sample) /
1798 hal_op_bytes_per_sample);
1799 ALOGI("enable conversion hal_input_fragment_size is %d src_format %x dst_format %x",
Ashish Jain83a6cc22016-06-28 14:34:17 +05301800 out->hal_fragment_size, src_format, dst_format);
Ashish Jainf1eaa582016-05-23 20:54:24 +05301801 } else {
Ashish Jain83a6cc22016-06-28 14:34:17 +05301802 out->hal_fragment_size = out->compr_config.fragment_size;
Ashish Jainf1eaa582016-05-23 20:54:24 +05301803 }
1804}
1805
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301806/* converts pcm format 24_8 to 8_24 inplace */
1807size_t audio_extn_utils_convert_format_24_8_to_8_24(void *buf, size_t bytes)
1808{
1809 size_t i = 0;
1810 int *int_buf_stream = buf;
1811
1812 if ((bytes % 4) != 0) {
1813 ALOGE("%s: wrong inout buffer! ... is not 32 bit aligned ", __func__);
1814 return -EINVAL;
1815 }
1816
1817 for (; i < (bytes / 4); i++)
1818 int_buf_stream[i] >>= 8;
1819
1820 return bytes;
1821}
1822
Xiaojun Sang782e5b12020-06-29 21:13:06 +08001823#ifdef AUDIO_GKI_ENABLED
1824int get_snd_codec_id(audio_format_t format)
1825{
1826 int id = 0;
1827
1828 switch (format & AUDIO_FORMAT_MAIN_MASK) {
1829 case AUDIO_FORMAT_MP3:
1830 id = SND_AUDIOCODEC_MP3;
1831 break;
1832 case AUDIO_FORMAT_AAC:
1833 id = SND_AUDIOCODEC_AAC;
1834 break;
1835 case AUDIO_FORMAT_AAC_ADTS:
1836 id = SND_AUDIOCODEC_AAC;
1837 break;
1838 case AUDIO_FORMAT_AAC_LATM:
1839 id = SND_AUDIOCODEC_AAC;
1840 break;
1841 case AUDIO_FORMAT_PCM:
1842 id = SND_AUDIOCODEC_PCM;
1843 break;
1844 case AUDIO_FORMAT_FLAC:
1845 case AUDIO_FORMAT_ALAC:
1846 case AUDIO_FORMAT_APE:
1847 case AUDIO_FORMAT_VORBIS:
1848 case AUDIO_FORMAT_WMA:
1849 case AUDIO_FORMAT_WMA_PRO:
1850 case AUDIO_FORMAT_DSD:
1851 case AUDIO_FORMAT_APTX:
1852 id = SND_AUDIOCODEC_BESPOKE;
1853 break;
1854 case AUDIO_FORMAT_MP2:
1855 id = SND_AUDIOCODEC_MP2;
1856 break;
1857 case AUDIO_FORMAT_AC3:
1858 id = SND_AUDIOCODEC_AC3;
1859 break;
1860 case AUDIO_FORMAT_E_AC3:
1861 case AUDIO_FORMAT_E_AC3_JOC:
1862 id = SND_AUDIOCODEC_EAC3;
1863 break;
1864 case AUDIO_FORMAT_DTS:
1865 case AUDIO_FORMAT_DTS_HD:
1866 id = SND_AUDIOCODEC_DTS;
1867 break;
1868 case AUDIO_FORMAT_DOLBY_TRUEHD:
1869 id = SND_AUDIOCODEC_TRUEHD;
1870 break;
1871 case AUDIO_FORMAT_IEC61937:
1872 id = SND_AUDIOCODEC_IEC61937;
1873 break;
1874 default:
1875 ALOGE("%s: Unsupported audio format :%x", __func__, format);
1876 }
1877
1878 return id;
1879}
1880#else
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301881int get_snd_codec_id(audio_format_t format)
1882{
1883 int id = 0;
1884
1885 switch (format & AUDIO_FORMAT_MAIN_MASK) {
1886 case AUDIO_FORMAT_MP3:
1887 id = SND_AUDIOCODEC_MP3;
1888 break;
1889 case AUDIO_FORMAT_AAC:
1890 id = SND_AUDIOCODEC_AAC;
1891 break;
1892 case AUDIO_FORMAT_AAC_ADTS:
1893 id = SND_AUDIOCODEC_AAC;
1894 break;
Arun Kumar Dasari3b174182016-12-27 13:01:14 +05301895 case AUDIO_FORMAT_AAC_LATM:
1896 id = SND_AUDIOCODEC_AAC;
1897 break;
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301898 case AUDIO_FORMAT_PCM:
1899 id = SND_AUDIOCODEC_PCM;
1900 break;
1901 case AUDIO_FORMAT_FLAC:
1902 id = SND_AUDIOCODEC_FLAC;
1903 break;
1904 case AUDIO_FORMAT_ALAC:
1905 id = SND_AUDIOCODEC_ALAC;
1906 break;
1907 case AUDIO_FORMAT_APE:
1908 id = SND_AUDIOCODEC_APE;
1909 break;
1910 case AUDIO_FORMAT_VORBIS:
1911 id = SND_AUDIOCODEC_VORBIS;
1912 break;
1913 case AUDIO_FORMAT_WMA:
1914 id = SND_AUDIOCODEC_WMA;
1915 break;
1916 case AUDIO_FORMAT_WMA_PRO:
1917 id = SND_AUDIOCODEC_WMA_PRO;
1918 break;
Satish Babu Patakokila0c313922016-12-08 12:07:08 +05301919 case AUDIO_FORMAT_MP2:
1920 id = SND_AUDIOCODEC_MP2;
1921 break;
Satish Babu Patakokila915ecba2017-01-10 17:43:56 +05301922 case AUDIO_FORMAT_AC3:
1923 id = SND_AUDIOCODEC_AC3;
1924 break;
1925 case AUDIO_FORMAT_E_AC3:
1926 case AUDIO_FORMAT_E_AC3_JOC:
1927 id = SND_AUDIOCODEC_EAC3;
1928 break;
1929 case AUDIO_FORMAT_DTS:
1930 case AUDIO_FORMAT_DTS_HD:
1931 id = SND_AUDIOCODEC_DTS;
1932 break;
Ben Romberger1aaaf862017-04-06 17:49:46 -07001933 case AUDIO_FORMAT_DOLBY_TRUEHD:
1934 id = SND_AUDIOCODEC_TRUEHD;
1935 break;
Naresh Tanniru928f0862017-04-07 16:44:23 -07001936 case AUDIO_FORMAT_IEC61937:
1937 id = SND_AUDIOCODEC_IEC61937;
1938 break;
Preetam Singh Ranawat4277a5a2017-01-18 19:02:24 +05301939 case AUDIO_FORMAT_DSD:
1940 id = SND_AUDIOCODEC_DSD;
1941 break;
Dhanalakshmi Siddani18737932016-11-29 17:33:17 +05301942 case AUDIO_FORMAT_APTX:
1943 id = SND_AUDIOCODEC_APTX;
1944 break;
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301945 default:
1946 ALOGE("%s: Unsupported audio format :%x", __func__, format);
1947 }
1948
1949 return id;
1950}
Xiaojun Sang782e5b12020-06-29 21:13:06 +08001951#endif
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301952
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001953void audio_extn_utils_send_audio_calibration(struct audio_device *adev,
1954 struct audio_usecase *usecase)
1955{
1956 int type = usecase->type;
1957
Dhananjay Kumar14245982017-01-16 20:21:00 +05301958 if (type == PCM_PLAYBACK && usecase->stream.out != NULL) {
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05301959 platform_send_audio_calibration(adev->platform, usecase,
Zhou Song06761dd2019-04-28 18:08:17 +08001960 usecase->stream.out->app_type_cfg.app_type);
Dhananjay Kumar14245982017-01-16 20:21:00 +05301961 } else if (type == PCM_CAPTURE && usecase->stream.in != NULL) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301962 platform_send_audio_calibration(adev->platform, usecase,
Zhou Song06761dd2019-04-28 18:08:17 +08001963 usecase->stream.in->app_type_cfg.app_type);
1964 } else if ((type == PCM_HFP_CALL) || (type == PCM_CAPTURE) ||
Derek Chena30a5f42019-12-03 11:17:09 -05001965 (type == TRANSCODE_LOOPBACK_RX && usecase->stream.inout != NULL) ||
1966 (type == ICC_CALL)) {
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05301967 platform_send_audio_calibration(adev->platform, usecase,
Zhou Song06761dd2019-04-28 18:08:17 +08001968 platform_get_default_app_type_v2(adev->platform, usecase->type));
Vidyakumar Athotae57f6002017-03-01 13:13:16 -08001969 } else {
1970 /* No need to send audio calibration for voice and voip call usecases */
1971 if ((type != VOICE_CALL) && (type != VOIP_CALL))
1972 ALOGW("%s: No audio calibration for usecase type = %d", __func__, type);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001973 }
1974}
1975
Ben Rombergera04fabc2014-11-14 12:16:03 -08001976// Base64 Encode and Decode
1977// Not all features supported. This must be used only with following conditions.
1978// Decode Modes: Support with and without padding
1979// CRLF not handling. So no CRLF in string to decode.
1980// Encode Modes: Supports only padding
1981int b64decode(char *inp, int ilen, uint8_t* outp)
1982{
1983 int i, j, k, ii, num;
1984 int rem, pcnt;
1985 uint32_t res=0;
1986 uint8_t getIndex[MAX_BASEINDEX_LEN];
1987 uint8_t tmp, cflag;
1988
1989 if(inp == NULL || outp == NULL || ilen <= 0) {
1990 ALOGE("[%s] received NULL pointer or zero length",__func__);
1991 return -1;
1992 }
1993
1994 memset(getIndex, MAX_BASEINDEX_LEN-1, sizeof(getIndex));
1995 for(i=0;i<BASE_TABLE_SIZE;i++) {
1996 getIndex[(uint8_t)bTable[i]] = (uint8_t)i;
1997 }
1998 getIndex[(uint8_t)'=']=0;
1999
2000 j=0;k=0;
2001 num = ilen/4;
2002 rem = ilen%4;
2003 if(rem==0)
2004 num = num-1;
2005 cflag=0;
2006 for(i=0; i<num; i++) {
2007 res=0;
2008 for(ii=0;ii<4;ii++) {
2009 res = res << 6;
2010 tmp = getIndex[(uint8_t)inp[j++]];
2011 res = res | tmp;
2012 cflag = cflag | tmp;
2013 }
2014 outp[k++] = (res >> 16)&0xFF;
2015 outp[k++] = (res >> 8)&0xFF;
2016 outp[k++] = res & 0xFF;
2017 }
2018
2019 // Handle last bytes special
2020 pcnt=0;
2021 if(rem == 0) {
2022 //With padding or full data
2023 res = 0;
2024 for(ii=0;ii<4;ii++) {
2025 if(inp[j] == '=')
2026 pcnt++;
2027 res = res << 6;
2028 tmp = getIndex[(uint8_t)inp[j++]];
2029 res = res | tmp;
2030 cflag = cflag | tmp;
2031 }
2032 outp[k++] = res >> 16;
2033 if(pcnt == 2)
2034 goto done;
2035 outp[k++] = (res>>8)&0xFF;
2036 if(pcnt == 1)
2037 goto done;
2038 outp[k++] = res&0xFF;
2039 } else {
2040 //without padding
2041 res = 0;
2042 for(i=0;i<rem;i++) {
2043 res = res << 6;
2044 tmp = getIndex[(uint8_t)inp[j++]];
2045 res = res | tmp;
2046 cflag = cflag | tmp;
2047 }
2048 for(i=rem;i<4;i++) {
2049 res = res << 6;
2050 pcnt++;
2051 }
2052 outp[k++] = res >> 16;
2053 if(pcnt == 2)
2054 goto done;
2055 outp[k++] = (res>>8)&0xFF;
2056 if(pcnt == 1)
2057 goto done;
2058 outp[k++] = res&0xFF;
2059 }
2060done:
2061 if(cflag == 0xFF) {
2062 ALOGE("[%s] base64 decode failed. Invalid character found %s",
2063 __func__, inp);
2064 return 0;
2065 }
2066 return k;
2067}
2068
2069int b64encode(uint8_t *inp, int ilen, char* outp)
2070{
2071 int i,j,k, num;
2072 int rem=0;
2073 uint32_t res=0;
2074
2075 if(inp == NULL || outp == NULL || ilen<=0) {
2076 ALOGE("[%s] received NULL pointer or zero input length",__func__);
2077 return -1;
2078 }
2079
2080 num = ilen/3;
2081 rem = ilen%3;
2082 j=0;k=0;
2083 for(i=0; i<num; i++) {
2084 //prepare index
2085 res = inp[j++]<<16;
2086 res = res | inp[j++]<<8;
2087 res = res | inp[j++];
2088 //get output map from index
2089 outp[k++] = (char) bTable[(res>>18)&0x3F];
2090 outp[k++] = (char) bTable[(res>>12)&0x3F];
2091 outp[k++] = (char) bTable[(res>>6)&0x3F];
2092 outp[k++] = (char) bTable[res&0x3F];
2093 }
2094
2095 switch(rem) {
2096 case 1:
2097 res = inp[j++]<<16;
2098 outp[k++] = (char) bTable[res>>18];
2099 outp[k++] = (char) bTable[(res>>12)&0x3F];
2100 //outp[k++] = '=';
2101 //outp[k++] = '=';
2102 break;
2103 case 2:
2104 res = inp[j++]<<16;
2105 res = res | inp[j++]<<8;
2106 outp[k++] = (char) bTable[res>>18];
2107 outp[k++] = (char) bTable[(res>>12)&0x3F];
2108 outp[k++] = (char) bTable[(res>>6)&0x3F];
2109 //outp[k++] = '=';
2110 break;
2111 default:
2112 break;
2113 }
Ben Rombergera04fabc2014-11-14 12:16:03 -08002114 outp[k] = '\0';
2115 return k;
2116}
Ashish Jain81eb2a82015-05-13 10:52:34 +05302117
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05302118
2119int audio_extn_utils_get_codec_version(const char *snd_card_name,
2120 int card_num,
2121 char *codec_version)
2122{
2123 char procfs_path[50];
2124 FILE *fp;
2125
2126 if (strstr(snd_card_name, "tasha")) {
2127 snprintf(procfs_path, sizeof(procfs_path),
2128 "/proc/asound/card%d/codecs/tasha/version", card_num);
2129 if ((fp = fopen(procfs_path, "r")) != NULL) {
2130 fgets(codec_version, CODEC_VERSION_MAX_LENGTH, fp);
2131 fclose(fp);
2132 } else {
2133 ALOGE("%s: ERROR. cannot open %s", __func__, procfs_path);
2134 return -ENOENT;
2135 }
2136 ALOGD("%s: codec version %s", __func__, codec_version);
2137 }
2138
2139 return 0;
2140}
2141
Preetam Singh Ranawat9d0d8e42019-07-15 12:27:25 +05302142int audio_extn_utils_get_codec_variant(int card_num,
2143 char *codec_variant)
2144{
2145 char procfs_path[50];
2146 FILE *fp;
2147 snprintf(procfs_path, sizeof(procfs_path),
2148 "/proc/asound/card%d/codecs/wcd938x/variant", card_num);
2149 if ((fp = fopen(procfs_path, "r")) == NULL) {
2150 snprintf(procfs_path, sizeof(procfs_path),
2151 "/proc/asound/card%d/codecs/wcd937x/variant", card_num);
2152 if ((fp = fopen(procfs_path, "r")) == NULL) {
2153 ALOGE("%s: ERROR. cannot open %s", __func__, procfs_path);
2154 return -ENOENT;
2155 }
2156 }
2157 fgets(codec_variant, CODEC_VARIANT_MAX_LENGTH, fp);
2158 fclose(fp);
2159 ALOGD("%s: codec variant is %s", __func__, codec_variant);
2160 return 0;
2161}
2162
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05302163
Ashish Jain81eb2a82015-05-13 10:52:34 +05302164#ifdef AUDIO_EXTERNAL_HDMI_ENABLED
2165
2166void get_default_compressed_channel_status(
2167 unsigned char *channel_status)
2168{
Ashish Jain81eb2a82015-05-13 10:52:34 +05302169 memset(channel_status,0,24);
2170
2171 /* block start bit in preamble bit 3 */
2172 channel_status[0] |= PROFESSIONAL;
2173 //compre out
2174 channel_status[0] |= NON_LPCM;
2175 // sample rate; fixed 48K for default/transcode
2176 channel_status[3] |= SR_48000;
2177}
2178
Ashish Jain81eb2a82015-05-13 10:52:34 +05302179int32_t get_compressed_channel_status(void *audio_stream_data,
2180 uint32_t audio_frame_size,
2181 unsigned char *channel_status,
2182 enum audio_parser_code_type codec_type)
2183 // codec_type - AUDIO_PARSER_CODEC_AC3
2184 // - AUDIO_PARSER_CODEC_DTS
2185{
2186 unsigned char *stream;
2187 int ret = 0;
2188 stream = (unsigned char *)audio_stream_data;
2189
2190 if (audio_stream_data == NULL || audio_frame_size == 0) {
2191 ALOGW("no buffer to get channel status, return default for compress");
2192 get_default_compressed_channel_status(channel_status);
2193 return ret;
2194 }
2195
2196 memset(channel_status,0,24);
2197 if(init_audio_parser(stream, audio_frame_size, codec_type) == -1)
2198 {
2199 ALOGE("init audio parser failed");
2200 return -1;
2201 }
2202 ret = get_channel_status(channel_status, codec_type);
2203 return ret;
2204
2205}
2206
Ashish Jain81eb2a82015-05-13 10:52:34 +05302207void get_lpcm_channel_status(uint32_t sampleRate,
2208 unsigned char *channel_status)
2209{
2210 int32_t status = 0;
Ashish Jain81eb2a82015-05-13 10:52:34 +05302211 memset(channel_status,0,24);
2212 /* block start bit in preamble bit 3 */
2213 channel_status[0] |= PROFESSIONAL;
2214 //LPCM OUT
2215 channel_status[0] &= ~NON_LPCM;
2216
2217 switch (sampleRate) {
2218 case 8000:
2219 case 11025:
2220 case 12000:
2221 case 16000:
2222 case 22050:
2223 channel_status[3] |= SR_NOTID;
Preetam Singh Ranawat61716b12015-12-14 11:55:24 +05302224 break;
Ashish Jain81eb2a82015-05-13 10:52:34 +05302225 case 24000:
2226 channel_status[3] |= SR_24000;
2227 break;
2228 case 32000:
2229 channel_status[3] |= SR_32000;
2230 break;
2231 case 44100:
2232 channel_status[3] |= SR_44100;
2233 break;
2234 case 48000:
2235 channel_status[3] |= SR_48000;
2236 break;
2237 case 88200:
2238 channel_status[3] |= SR_88200;
2239 break;
2240 case 96000:
2241 channel_status[3] |= SR_96000;
2242 break;
2243 case 176400:
2244 channel_status[3] |= SR_176400;
2245 break;
2246 case 192000:
2247 channel_status[3] |= SR_192000;
2248 break;
2249 default:
2250 ALOGV("Invalid sample_rate %u\n", sampleRate);
2251 status = -1;
2252 break;
2253 }
2254}
2255
2256void audio_utils_set_hdmi_channel_status(struct stream_out *out, char * buffer, size_t bytes)
2257{
2258 unsigned char channel_status[24]={0};
2259 struct snd_aes_iec958 iec958;
2260 const char *mixer_ctl_name = "IEC958 Playback PCM Stream";
2261 struct mixer_ctl *ctl;
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +05302262 ALOGV("%s: buffer %s bytes %zd", __func__, buffer, bytes);
Arun Mirpurie008ed22019-03-21 11:21:04 -07002263
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05302264 if (audio_extn_utils_is_dolby_format(out->format) &&
Ashish Jain81eb2a82015-05-13 10:52:34 +05302265 /*TODO:Extend code to support DTS passthrough*/
2266 /*set compressed channel status bits*/
Arun Mirpurie008ed22019-03-21 11:21:04 -07002267 audio_extn_passthru_is_passthrough_stream(out) &&
2268 audio_extn_is_hdmi_passthru_enabled()) {
Ashish Jain81eb2a82015-05-13 10:52:34 +05302269 get_compressed_channel_status(buffer, bytes, channel_status, AUDIO_PARSER_CODEC_AC3);
Arun Mirpurie008ed22019-03-21 11:21:04 -07002270 } else {
Ashish Jain81eb2a82015-05-13 10:52:34 +05302271 /*set channel status bit for LPCM*/
2272 get_lpcm_channel_status(out->sample_rate, channel_status);
2273 }
2274
2275 memcpy(iec958.status, channel_status,sizeof(iec958.status));
2276 ctl = mixer_get_ctl_by_name(out->dev->mixer, mixer_ctl_name);
2277 if (!ctl) {
2278 ALOGE("%s: Could not get ctl for mixer cmd - %s",
2279 __func__, mixer_ctl_name);
2280 return;
2281 }
2282 if (mixer_ctl_set_array(ctl, &iec958, sizeof(iec958)) < 0) {
2283 ALOGE("%s: Could not set channel status for ext HDMI ",
2284 __func__);
2285 return;
2286 }
2287
2288}
2289#endif
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302290
2291int audio_extn_utils_get_avt_device_drift(
2292 struct audio_usecase *usecase,
2293 struct audio_avt_device_drift_param *drift_param)
2294{
2295 int ret = 0, count = 0;
2296 char avt_device_drift_mixer_ctl_name[MIXER_PATH_MAX_LENGTH] = {0};
Naresh Tanniru6160c712017-04-17 15:43:48 +05302297 const char *backend = NULL;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302298 struct mixer_ctl *ctl = NULL;
2299 struct audio_avt_device_drift_stats drift_stats;
2300 struct audio_device *adev = NULL;
2301
2302 if (usecase != NULL && usecase->type == PCM_PLAYBACK) {
Naresh Tanniru6160c712017-04-17 15:43:48 +05302303 backend = platform_get_snd_device_backend_interface(usecase->out_snd_device);
2304 if (!backend) {
2305 ALOGE("%s: Unsupported device %d", __func__,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08002306 get_device_types(&usecase->stream.out->device_list));
Naresh Tanniru6160c712017-04-17 15:43:48 +05302307 ret = -EINVAL;
2308 goto done;
2309 }
2310 strlcpy(avt_device_drift_mixer_ctl_name,
2311 backend,
2312 MIXER_PATH_MAX_LENGTH);
2313
2314 count = strlen(backend);
2315 if (MIXER_PATH_MAX_LENGTH - count > 0) {
2316 strlcat(&avt_device_drift_mixer_ctl_name[count],
2317 " DRIFT",
2318 MIXER_PATH_MAX_LENGTH - count);
2319 } else {
2320 ret = -EINVAL;
2321 goto done;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302322 }
2323 } else {
Naresh Tanniru7586e292017-04-13 10:38:13 +05302324 ALOGE("%s: Invalid usecase",__func__);
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302325 ret = -EINVAL;
Naresh Tanniru6160c712017-04-17 15:43:48 +05302326 goto done;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302327 }
2328
Naresh Tanniru6160c712017-04-17 15:43:48 +05302329 adev = usecase->stream.out->dev;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302330 ctl = mixer_get_ctl_by_name(adev->mixer, avt_device_drift_mixer_ctl_name);
2331 if (!ctl) {
2332 ALOGE("%s: Could not get ctl for mixer cmd - %s",
2333 __func__, avt_device_drift_mixer_ctl_name);
2334
2335 ret = -EINVAL;
2336 goto done;
2337 }
2338
2339 ALOGV("%s: Getting AV Timer vs Device Drift mixer ctrl name %s", __func__,
2340 avt_device_drift_mixer_ctl_name);
2341
2342 mixer_ctl_update(ctl);
2343 count = mixer_ctl_get_num_values(ctl);
2344 if (count != sizeof(struct audio_avt_device_drift_stats)) {
2345 ALOGE("%s: mixer_ctl_get_num_values() invalid drift_stats data size",
2346 __func__);
2347
2348 ret = -EINVAL;
2349 goto done;
2350 }
2351
2352 ret = mixer_ctl_get_array(ctl, (void *)&drift_stats, count);
2353 if (ret != 0) {
2354 ALOGE("%s: mixer_ctl_get_array() failed to get drift_stats Params",
2355 __func__);
2356
2357 ret = -EINVAL;
2358 goto done;
2359 }
2360 memcpy(drift_param, &drift_stats.drift_param,
2361 sizeof(struct audio_avt_device_drift_param));
2362done:
2363 return ret;
2364}
Manish Dewangan07de2142017-02-27 19:27:20 +05302365
2366#ifdef SNDRV_COMPRESS_PATH_DELAY
2367int audio_extn_utils_compress_get_dsp_latency(struct stream_out *out)
2368{
2369 int ret = -EINVAL;
2370 struct snd_compr_metadata metadata;
2371 int delay_ms = COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
2372
Weiyin Jiangc49a3b52018-08-17 16:16:08 +08002373 /* override the latency for pcm offload use case */
2374 if ((out->flags & AUDIO_OUTPUT_FLAG_DIRECT) &&
2375 !(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)) {
2376 delay_ms = PCM_OFFLOAD_PLAYBACK_LATENCY;
2377 }
2378
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -07002379 if (property_get_bool("vendor.audio.playback.dsp.pathdelay", false)) {
Manish Dewangan07de2142017-02-27 19:27:20 +05302380 ALOGD("%s:: Quering DSP delay %d",__func__, __LINE__);
2381 if (!(is_offload_usecase(out->usecase))) {
2382 ALOGE("%s:: not supported for non offload session", __func__);
2383 goto exit;
2384 }
2385
2386 if (!out->compr) {
2387 ALOGD("%s:: Invalid compress handle,returning default dsp latency",
2388 __func__);
2389 goto exit;
2390 }
2391
2392 metadata.key = SNDRV_COMPRESS_PATH_DELAY;
2393 ret = compress_get_metadata(out->compr, &metadata);
2394 if(ret) {
2395 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2396 goto exit;
2397 }
2398 delay_ms = metadata.value[0] / 1000; /*convert to ms*/
2399 } else {
2400 ALOGD("%s:: Using Fix DSP delay",__func__);
2401 }
2402
2403exit:
2404 ALOGD("%s:: delay in ms is %d",__func__, delay_ms);
2405 return delay_ms;
2406}
2407#else
2408int audio_extn_utils_compress_get_dsp_latency(struct stream_out *out __unused)
2409{
2410 return COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
2411}
2412#endif
Manish Dewangan69426c82017-01-30 17:35:36 +05302413
2414#ifdef SNDRV_COMPRESS_RENDER_MODE
2415int audio_extn_utils_compress_set_render_mode(struct stream_out *out)
2416{
2417 struct snd_compr_metadata metadata;
2418 int ret = -EINVAL;
2419
2420 if (!(is_offload_usecase(out->usecase))) {
2421 ALOGE("%s:: not supported for non offload session", __func__);
2422 goto exit;
2423 }
2424
2425 if (!out->compr) {
2426 ALOGD("%s:: Invalid compress handle",
2427 __func__);
2428 goto exit;
2429 }
2430
2431 ALOGD("%s:: render mode %d", __func__, out->render_mode);
2432
2433 metadata.key = SNDRV_COMPRESS_RENDER_MODE;
2434 if (out->render_mode == RENDER_MODE_AUDIO_MASTER) {
2435 metadata.value[0] = SNDRV_COMPRESS_RENDER_MODE_AUDIO_MASTER;
2436 } else if (out->render_mode == RENDER_MODE_AUDIO_STC_MASTER) {
2437 metadata.value[0] = SNDRV_COMPRESS_RENDER_MODE_STC_MASTER;
2438 } else {
2439 ret = 0;
2440 goto exit;
2441 }
2442 ret = compress_set_metadata(out->compr, &metadata);
2443 if(ret) {
2444 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2445 }
2446exit:
2447 return ret;
2448}
2449#else
2450int audio_extn_utils_compress_set_render_mode(struct stream_out *out __unused)
2451{
2452 ALOGD("%s:: configuring render mode not supported", __func__);
2453 return 0;
2454}
2455#endif
Manish Dewangan58229382017-02-02 15:48:41 +05302456
2457#ifdef SNDRV_COMPRESS_CLK_REC_MODE
2458int audio_extn_utils_compress_set_clk_rec_mode(
2459 struct audio_usecase *usecase)
2460{
2461 struct snd_compr_metadata metadata;
2462 struct stream_out *out = NULL;
2463 int ret = -EINVAL;
2464
Naresh Tanniru7586e292017-04-13 10:38:13 +05302465 if (usecase == NULL || usecase->type != PCM_PLAYBACK) {
Manish Dewangan58229382017-02-02 15:48:41 +05302466 ALOGE("%s:: Invalid use case", __func__);
2467 goto exit;
2468 }
2469
2470 out = usecase->stream.out;
2471 if (!out) {
2472 ALOGE("%s:: invalid stream", __func__);
2473 goto exit;
2474 }
2475
2476 if (!is_offload_usecase(out->usecase)) {
2477 ALOGE("%s:: not supported for non offload session", __func__);
2478 goto exit;
2479 }
2480
2481 if (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER) {
2482 ALOGD("%s:: clk recovery is only supported in STC render mode",
2483 __func__);
2484 ret = 0;
2485 goto exit;
2486 }
2487
2488 if (!out->compr) {
2489 ALOGD("%s:: Invalid compress handle",
2490 __func__);
2491 goto exit;
2492 }
2493 metadata.key = SNDRV_COMPRESS_CLK_REC_MODE;
2494 switch(usecase->out_snd_device) {
2495 case SND_DEVICE_OUT_HDMI:
2496 case SND_DEVICE_OUT_SPEAKER_AND_HDMI:
2497 case SND_DEVICE_OUT_DISPLAY_PORT:
2498 case SND_DEVICE_OUT_SPEAKER_AND_DISPLAY_PORT:
2499 metadata.value[0] = SNDRV_COMPRESS_CLK_REC_MODE_NONE;
2500 break;
2501 default:
2502 metadata.value[0] = SNDRV_COMPRESS_CLK_REC_MODE_AUTO;
2503 break;
2504 }
2505
2506 ALOGD("%s:: clk recovery mode %d",__func__, metadata.value[0]);
2507
2508 ret = compress_set_metadata(out->compr, &metadata);
2509 if(ret) {
2510 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2511 }
2512
2513exit:
2514 return ret;
2515}
2516#else
2517int audio_extn_utils_compress_set_clk_rec_mode(
2518 struct audio_usecase *usecase __unused)
2519{
2520 ALOGD("%s:: configuring render mode not supported", __func__);
2521 return 0;
2522}
2523#endif
Manish Dewangan27346042017-03-01 12:56:12 +05302524
2525#ifdef SNDRV_COMPRESS_RENDER_WINDOW
2526int audio_extn_utils_compress_set_render_window(
2527 struct stream_out *out,
2528 struct audio_out_render_window_param *render_window)
2529{
2530 struct snd_compr_metadata metadata;
2531 int ret = -EINVAL;
2532
Manish Dewangan27346042017-03-01 12:56:12 +05302533 if(render_window == NULL) {
2534 ALOGE("%s:: Invalid render_window", __func__);
2535 goto exit;
2536 }
2537
Aniket Kumar Lata1e1d3662017-06-01 18:45:48 -07002538 ALOGD("%s:: render window start 0x%"PRIx64" end 0x%"PRIx64"",
2539 __func__,render_window->render_ws, render_window->render_we);
2540
Manish Dewangan27346042017-03-01 12:56:12 +05302541 if (!is_offload_usecase(out->usecase)) {
2542 ALOGE("%s:: not supported for non offload session", __func__);
2543 goto exit;
2544 }
2545
Naresh Tanniru6160c712017-04-17 15:43:48 +05302546 if ((out->render_mode != RENDER_MODE_AUDIO_MASTER) &&
2547 (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER)) {
Manish Dewangan27346042017-03-01 12:56:12 +05302548 ALOGD("%s:: only supported in timestamp mode, current "
2549 "render mode mode %d", __func__, out->render_mode);
2550 goto exit;
2551 }
2552
2553 if (!out->compr) {
2554 ALOGW("%s:: offload session not yet opened,"
2555 "render window will be configure later", __func__);
2556 /* store render window to reconfigure in start_output_stream() */
2557 goto exit;
2558 }
2559
2560 metadata.key = SNDRV_COMPRESS_RENDER_WINDOW;
2561 /*render window start value */
2562 metadata.value[0] = 0xFFFFFFFF & render_window->render_ws; /* lsb */
2563 metadata.value[1] = \
2564 (0xFFFFFFFF00000000 & render_window->render_ws) >> 32; /* msb*/
2565 /*render window end value */
2566 metadata.value[2] = 0xFFFFFFFF & render_window->render_we; /* lsb */
2567 metadata.value[3] = \
2568 (0xFFFFFFFF00000000 & render_window->render_we) >> 32; /* msb*/
2569
2570 ret = compress_set_metadata(out->compr, &metadata);
2571 if(ret) {
2572 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2573 }
2574
2575exit:
2576 return ret;
2577}
2578#else
2579int audio_extn_utils_compress_set_render_window(
2580 struct stream_out *out __unused,
2581 struct audio_out_render_window_param *render_window __unused)
2582{
2583 ALOGD("%s:: configuring render window not supported", __func__);
2584 return 0;
2585}
2586#endif
Manish Dewangan14956cc2017-02-14 18:54:42 +05302587
2588#ifdef SNDRV_COMPRESS_START_DELAY
2589int audio_extn_utils_compress_set_start_delay(
2590 struct stream_out *out,
2591 struct audio_out_start_delay_param *delay_param)
2592{
2593 struct snd_compr_metadata metadata;
2594 int ret = -EINVAL;
2595
2596 if(delay_param == NULL) {
2597 ALOGE("%s:: Invalid delay_param", __func__);
2598 goto exit;
2599 }
2600
2601 ALOGD("%s:: render start delay 0x%"PRIx64" ", __func__,
2602 delay_param->start_delay);
2603
2604 if (!is_offload_usecase(out->usecase)) {
2605 ALOGE("%s:: not supported for non offload session", __func__);
2606 goto exit;
2607 }
2608
Naresh Tanniru6160c712017-04-17 15:43:48 +05302609 if ((out->render_mode != RENDER_MODE_AUDIO_MASTER) &&
2610 (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER)) {
Manish Dewangan14956cc2017-02-14 18:54:42 +05302611 ALOGD("%s:: only supported in timestamp mode, current "
2612 "render mode mode %d", __func__, out->render_mode);
2613 goto exit;
2614 }
2615
2616 if (!out->compr) {
2617 ALOGW("%s:: offload session not yet opened,"
2618 "start delay will be configure later", __func__);
2619 goto exit;
2620 }
2621
2622 metadata.key = SNDRV_COMPRESS_START_DELAY;
2623 metadata.value[0] = 0xFFFFFFFF & delay_param->start_delay; /* lsb */
2624 metadata.value[1] = \
2625 (0xFFFFFFFF00000000 & delay_param->start_delay) >> 32; /* msb*/
2626
2627 ret = compress_set_metadata(out->compr, &metadata);
2628 if(ret) {
2629 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2630 }
2631
2632exit:
2633 return ret;
2634}
2635#else
2636int audio_extn_utils_compress_set_start_delay(
2637 struct stream_out *out __unused,
2638 struct audio_out_start_delay_param *delay_param __unused)
2639{
2640 ALOGD("%s:: configuring render window not supported", __func__);
2641 return 0;
2642}
2643#endif
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002644
Surendar Karka287348c2019-04-10 18:31:46 +05302645#ifdef SNDRV_COMPRESS_DSP_POSITION
2646int audio_extn_utils_compress_get_dsp_presentation_pos(struct stream_out *out,
2647 uint64_t *frames, struct timespec *timestamp, int32_t clock_id)
2648{
2649 int ret = -EINVAL;
2650 uint64_t *val = NULL;
2651 uint64_t time = 0;
2652 struct snd_compr_metadata metadata;
2653
2654 ALOGV("%s:: Quering DSP position with clock id %d",__func__, clock_id);
2655 metadata.key = SNDRV_COMPRESS_DSP_POSITION;
2656 metadata.value[0] = clock_id;
2657 ret = compress_get_metadata(out->compr, &metadata);
2658 if (ret) {
2659 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2660 ret = -errno;
2661 goto exit;
2662 }
2663 val = (uint64_t *)&metadata.value[1];
2664 *frames = *val;
2665 time = *(val + 1);
2666 timestamp->tv_sec = time / 1000000;
2667 timestamp->tv_nsec = (time % 1000000)*1000;
2668
2669exit:
2670 return ret;
2671}
2672#else
2673int audio_extn_utils_compress_get_dsp_presentation_pos(struct stream_out *out __unused,
2674 uint64_t *frames __unused, struct timespec *timestamp __unused,
2675 int32_t clock_id __unused)
2676{
2677 ALOGD("%s:: dsp presentation position not supported", __func__);
2678 return 0;
2679
2680}
2681#endif
2682
2683#ifdef SNDRV_PCM_IOCTL_DSP_POSITION
2684int audio_extn_utils_pcm_get_dsp_presentation_pos(struct stream_out *out,
2685 uint64_t *frames, struct timespec *timestamp, int32_t clock_id)
2686{
2687 int ret = -EINVAL;
2688 uint64_t time = 0;
2689 struct snd_pcm_prsnt_position prsnt_position;
2690
2691 ALOGV("%s:: Quering DSP position with clock id %d",__func__, clock_id);
2692 prsnt_position.clock_id = clock_id;
2693 ret = pcm_ioctl(out->pcm, SNDRV_PCM_IOCTL_DSP_POSITION, &prsnt_position);
2694 if (ret) {
2695 ALOGE("%s::error %d", __func__, ret);
2696 ret = -EIO;
2697 goto exit;
2698 }
2699
2700 *frames = prsnt_position.frames;
2701 time = prsnt_position.timestamp;
2702 timestamp->tv_sec = time / 1000000;
2703 timestamp->tv_nsec = (time % 1000000)*1000;
2704
2705exit:
2706 return ret;
2707}
2708#else
2709int audio_extn_utils_pcm_get_dsp_presentation_pos(struct stream_out *out __unused,
2710 uint64_t *frames __unused, struct timespec *timestamp __unused,
2711 int32_t clock_id __unused)
2712{
2713 ALOGD("%s:: dsp presentation position not supported", __func__);
2714 return 0;
2715
2716}
2717#endif
2718
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002719#define MAX_SND_CARD 8
Ashish Jain30ae8942017-11-05 17:21:23 +05302720#define RETRY_US 1000000
2721#define RETRY_NUMBER 40
Aalique Grahame22e49102018-12-18 14:23:57 -08002722#define PLATFORM_INFO_XML_PATH "audio_platform_info.xml"
2723#define PLATFORM_INFO_XML_BASE_STRING "audio_platform_info"
2724
Aalique Grahame22e49102018-12-18 14:23:57 -08002725bool audio_extn_utils_resolve_config_file(char file_name[MIXER_PATH_MAX_LENGTH])
2726{
2727 char full_config_path[MIXER_PATH_MAX_LENGTH];
Saurav Kumarf5d2c342020-07-29 19:31:49 +05302728 char vendor_config_path[VENDOR_CONFIG_PATH_MAX_LENGTH];
2729
2730 /* Get path for audio configuration files in vendor */
2731 audio_get_vendor_config_path(vendor_config_path,
2732 sizeof(vendor_config_path));
2733 snprintf(full_config_path, sizeof(full_config_path),
2734 "%s/%s", vendor_config_path, file_name);
2735 if (F_OK == access(full_config_path, 0)) {
2736 strlcpy(file_name, full_config_path, MIXER_PATH_MAX_LENGTH);
2737 return true;
Aalique Grahame22e49102018-12-18 14:23:57 -08002738 }
2739 return false;
2740}
2741
2742/* platform_info_file should be size 'MIXER_PATH_MAX_LENGTH' */
2743int audio_extn_utils_get_platform_info(const char* snd_card_name, char* platform_info_file)
2744{
2745 if (NULL == snd_card_name) {
2746 return -1;
2747 }
2748
2749 struct snd_card_split *snd_split_handle = NULL;
2750 int ret = 0;
2751 audio_extn_set_snd_card_split(snd_card_name);
2752 snd_split_handle = audio_extn_get_snd_card_split();
2753
2754 snprintf(platform_info_file, MIXER_PATH_MAX_LENGTH, "%s_%s_%s.xml",
2755 PLATFORM_INFO_XML_BASE_STRING, snd_split_handle->snd_card,
2756 snd_split_handle->form_factor);
2757
2758 if (!audio_extn_utils_resolve_config_file(platform_info_file)) {
2759 memset(platform_info_file, 0, MIXER_PATH_MAX_LENGTH);
2760 snprintf(platform_info_file, MIXER_PATH_MAX_LENGTH, "%s_%s.xml",
2761 PLATFORM_INFO_XML_BASE_STRING, snd_split_handle->snd_card);
2762
2763 if (!audio_extn_utils_resolve_config_file(platform_info_file)) {
2764 memset(platform_info_file, 0, MIXER_PATH_MAX_LENGTH);
2765 strlcpy(platform_info_file, PLATFORM_INFO_XML_PATH, MIXER_PATH_MAX_LENGTH);
2766 ret = audio_extn_utils_resolve_config_file(platform_info_file) ? 0 : -1;
2767 }
2768 }
2769
2770 return ret;
2771}
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002772
2773int audio_extn_utils_get_snd_card_num()
2774{
Soumya Managoli9fee7c62018-04-06 16:21:50 +05302775 int snd_card_num = 0;
2776 struct mixer *mixer = NULL;
2777
2778 snd_card_num = audio_extn_utils_open_snd_mixer(&mixer);
2779 if (mixer)
2780 mixer_close(mixer);
2781 return snd_card_num;
2782}
2783
2784int audio_extn_utils_open_snd_mixer(struct mixer **mixer_handle)
2785{
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002786
2787 void *hw_info = NULL;
2788 struct mixer *mixer = NULL;
2789 int retry_num = 0;
mpangfaa7bae2019-01-15 16:38:13 +08002790 int snd_card_num = 0;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002791 char* snd_card_name = NULL;
mpangfaa7bae2019-01-15 16:38:13 +08002792 int snd_card_detection_info[MAX_SND_CARD] = {0};
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002793
Soumya Managoli9fee7c62018-04-06 16:21:50 +05302794 if (!mixer_handle) {
2795 ALOGE("invalid mixer handle");
2796 return -1;
2797 }
2798 *mixer_handle = NULL;
Ashish Jain30ae8942017-11-05 17:21:23 +05302799 /*
mpangfaa7bae2019-01-15 16:38:13 +08002800 * Try with all the sound cards ( 0 to 7 ) and if none of them were detected
Ashish Jain30ae8942017-11-05 17:21:23 +05302801 * sleep for 1 sec and try detections with sound card 0 again.
2802 * If sound card gets detected, check if it is relevant, if not check with the
2803 * other sound cards. To ensure that the irrelevant sound card is not check again,
2804 * we maintain it in min_snd_card_num.
2805 */
2806 while (retry_num < RETRY_NUMBER) {
mpangfaa7bae2019-01-15 16:38:13 +08002807 snd_card_num = 0;
2808 while (snd_card_num < MAX_SND_CARD) {
2809 if (snd_card_detection_info[snd_card_num] == 0) {
2810 mixer = mixer_open(snd_card_num);
2811 if (!mixer)
2812 snd_card_num++;
2813 else
2814 break;
2815 } else
2816 snd_card_num++;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002817 }
2818
2819 if (!mixer) {
Ashish Jain30ae8942017-11-05 17:21:23 +05302820 usleep(RETRY_US);
Ashish Jain30ae8942017-11-05 17:21:23 +05302821 retry_num++;
mpangfaa7bae2019-01-15 16:38:13 +08002822 ALOGD("%s: retry, retry_num %d", __func__, retry_num);
Ashish Jain30ae8942017-11-05 17:21:23 +05302823 continue;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002824 }
2825
2826 snd_card_name = strdup(mixer_get_name(mixer));
2827 if (!snd_card_name) {
2828 ALOGE("failed to allocate memory for snd_card_name\n");
2829 mixer_close(mixer);
2830 return -1;
2831 }
2832 ALOGD("%s: snd_card_name: %s", __func__, snd_card_name);
mpangfaa7bae2019-01-15 16:38:13 +08002833 snd_card_detection_info[snd_card_num] = 1;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002834 hw_info = hw_info_init(snd_card_name);
2835 if (hw_info) {
2836 ALOGD("%s: Opened sound card:%d", __func__, snd_card_num);
2837 break;
2838 }
mpangfaa7bae2019-01-15 16:38:13 +08002839 ALOGE("%s: Failed to init hardware info, snd_card_num:%d", __func__, snd_card_num);
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002840
Dhananjay Kumara7e27832017-07-11 15:40:39 +05302841 free(snd_card_name);
2842 snd_card_name = NULL;
2843
2844 mixer_close(mixer);
2845 mixer = NULL;
2846 }
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002847 if (snd_card_name)
2848 free(snd_card_name);
Ashish Jain30ae8942017-11-05 17:21:23 +05302849
Dhananjay Kumara7e27832017-07-11 15:40:39 +05302850 if (hw_info)
2851 hw_info_deinit(hw_info);
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002852
mpangfaa7bae2019-01-15 16:38:13 +08002853 if (retry_num >= RETRY_NUMBER) {
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002854 ALOGE("%s: Unable to find correct sound card, aborting.", __func__);
2855 return -1;
2856 }
2857
Soumya Managoli9fee7c62018-04-06 16:21:50 +05302858 if (mixer)
2859 *mixer_handle = mixer;
2860
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002861 return snd_card_num;
2862}
Naresh Tanniru6160c712017-04-17 15:43:48 +05302863
Soumya Managoli9fee7c62018-04-06 16:21:50 +05302864void audio_extn_utils_close_snd_mixer(struct mixer *mixer)
2865{
2866 if (mixer)
2867 mixer_close(mixer);
2868}
2869
Naresh Tanniru6160c712017-04-17 15:43:48 +05302870#ifdef SNDRV_COMPRESS_ENABLE_ADJUST_SESSION_CLOCK
2871int audio_extn_utils_compress_enable_drift_correction(
2872 struct stream_out *out,
2873 struct audio_out_enable_drift_correction *drift)
2874{
2875 struct snd_compr_metadata metadata;
2876 int ret = -EINVAL;
2877
2878 if(drift == NULL) {
2879 ALOGE("%s:: Invalid param", __func__);
2880 goto exit;
2881 }
2882
2883 ALOGD("%s:: drift enable %d", __func__,drift->enable);
2884
2885 if (!is_offload_usecase(out->usecase)) {
2886 ALOGE("%s:: not supported for non offload session", __func__);
2887 goto exit;
2888 }
2889
2890 if (!out->compr) {
2891 ALOGW("%s:: offload session not yet opened,"
2892 "start delay will be configure later", __func__);
2893 goto exit;
2894 }
2895
2896 metadata.key = SNDRV_COMPRESS_ENABLE_ADJUST_SESSION_CLOCK;
2897 metadata.value[0] = drift->enable;
2898 out->drift_correction_enabled = drift->enable;
2899
2900 ret = compress_set_metadata(out->compr, &metadata);
2901 if(ret) {
2902 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2903 out->drift_correction_enabled = false;
2904 }
2905
2906exit:
2907 return ret;
2908}
2909#else
2910int audio_extn_utils_compress_enable_drift_correction(
2911 struct stream_out *out __unused,
2912 struct audio_out_enable_drift_correction *drift __unused)
2913{
2914 ALOGD("%s:: configuring drift enablement not supported", __func__);
2915 return 0;
2916}
2917#endif
2918
2919#ifdef SNDRV_COMPRESS_ADJUST_SESSION_CLOCK
2920int audio_extn_utils_compress_correct_drift(
2921 struct stream_out *out,
2922 struct audio_out_correct_drift *drift_param)
2923{
2924 struct snd_compr_metadata metadata;
2925 int ret = -EINVAL;
2926
2927 if (drift_param == NULL) {
2928 ALOGE("%s:: Invalid drift_param", __func__);
2929 goto exit;
2930 }
2931
2932 ALOGD("%s:: adjust time 0x%"PRIx64" ", __func__,
2933 drift_param->adjust_time);
2934
2935 if (!is_offload_usecase(out->usecase)) {
2936 ALOGE("%s:: not supported for non offload session", __func__);
2937 goto exit;
2938 }
2939
2940 if (!out->compr) {
2941 ALOGW("%s:: offload session not yet opened", __func__);
2942 goto exit;
2943 }
2944
2945 if (!out->drift_correction_enabled) {
2946 ALOGE("%s:: drift correction not enabled", __func__);
2947 goto exit;
2948 }
2949
2950 metadata.key = SNDRV_COMPRESS_ADJUST_SESSION_CLOCK;
2951 metadata.value[0] = 0xFFFFFFFF & drift_param->adjust_time; /* lsb */
2952 metadata.value[1] = \
2953 (0xFFFFFFFF00000000 & drift_param->adjust_time) >> 32; /* msb*/
2954
2955 ret = compress_set_metadata(out->compr, &metadata);
2956 if(ret)
2957 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2958exit:
2959 return ret;
2960}
2961#else
2962int audio_extn_utils_compress_correct_drift(
2963 struct stream_out *out __unused,
2964 struct audio_out_correct_drift *drift_param __unused)
2965{
2966 ALOGD("%s:: setting adjust clock not supported", __func__);
2967 return 0;
2968}
2969#endif
Naresh Tanniru29bce4e2017-04-27 17:54:30 +05302970
2971int audio_extn_utils_set_channel_map(
2972 struct stream_out *out,
2973 struct audio_out_channel_map_param *channel_map_param)
2974{
2975 int ret = -EINVAL, i = 0;
2976 int channels = audio_channel_count_from_out_mask(out->channel_mask);
2977
2978 if (channel_map_param == NULL) {
2979 ALOGE("%s:: Invalid channel_map", __func__);
2980 goto exit;
2981 }
2982
2983 if (channel_map_param->channels != channels) {
2984 ALOGE("%s:: Channels(%d) does not match stream channels(%d)",
2985 __func__, channel_map_param->channels, channels);
2986 goto exit;
2987 }
2988
2989 for ( i = 0; i < channels; i++) {
2990 ALOGV("%s:: channel_map[%d]- %d", __func__, i, channel_map_param->channel_map[i]);
2991 out->channel_map_param.channel_map[i] = channel_map_param->channel_map[i];
2992 }
2993 ret = 0;
2994exit:
2995 return ret;
2996}
Varun Balaraje49253e2017-07-06 19:48:56 +05302997
2998int audio_extn_utils_set_pan_scale_params(
2999 struct stream_out *out,
3000 struct mix_matrix_params *mm_params)
3001{
3002 int ret = -EINVAL, i = 0, j = 0;
3003
Varun Balaraj003ee752017-08-07 17:54:55 +05303004 if (mm_params == NULL || out == NULL) {
3005 ALOGE("%s:: Invalid mix matrix or out param", __func__);
Varun Balaraje49253e2017-07-06 19:48:56 +05303006 goto exit;
3007 }
3008
3009 if (mm_params->num_output_channels > MAX_CHANNELS_SUPPORTED ||
3010 mm_params->num_output_channels <= 0 ||
3011 mm_params->num_input_channels > MAX_CHANNELS_SUPPORTED ||
3012 mm_params->num_input_channels <= 0)
3013 goto exit;
3014
3015 out->pan_scale_params.num_output_channels = mm_params->num_output_channels;
3016 out->pan_scale_params.num_input_channels = mm_params->num_input_channels;
3017 out->pan_scale_params.has_output_channel_map =
3018 mm_params->has_output_channel_map;
3019 for (i = 0; i < mm_params->num_output_channels; i++)
3020 out->pan_scale_params.output_channel_map[i] =
3021 mm_params->output_channel_map[i];
3022
3023 out->pan_scale_params.has_input_channel_map =
3024 mm_params->has_input_channel_map;
3025 for (i = 0; i < mm_params->num_input_channels; i++)
3026 out->pan_scale_params.input_channel_map[i] =
3027 mm_params->input_channel_map[i];
3028
3029 out->pan_scale_params.has_mixer_coeffs = mm_params->has_mixer_coeffs;
3030 for (i = 0; i < mm_params->num_output_channels; i++)
3031 for (j = 0; j < mm_params->num_input_channels; j++) {
3032 //Convert the channel coefficient gains in Q14 format
3033 out->pan_scale_params.mixer_coeffs[i][j] =
3034 mm_params->mixer_coeffs[i][j] * (2 << 13);
3035 }
3036
3037 ret = platform_set_stream_pan_scale_params(out->dev->platform,
3038 out->pcm_device_id,
3039 out->pan_scale_params);
3040
3041exit:
3042 return ret;
3043}
3044
3045int audio_extn_utils_set_downmix_params(
3046 struct stream_out *out,
3047 struct mix_matrix_params *mm_params)
3048{
3049 int ret = -EINVAL, i = 0, j = 0;
3050 struct audio_usecase *usecase = NULL;
3051
Aniket Kumar Lataf9f246e2017-09-15 15:20:16 -07003052 if (mm_params == NULL || out == NULL) {
Varun Balaraj003ee752017-08-07 17:54:55 +05303053 ALOGE("%s:: Invalid mix matrix or out param", __func__);
Varun Balaraje49253e2017-07-06 19:48:56 +05303054 goto exit;
3055 }
3056
3057 if (mm_params->num_output_channels > MAX_CHANNELS_SUPPORTED ||
3058 mm_params->num_output_channels <= 0 ||
3059 mm_params->num_input_channels > MAX_CHANNELS_SUPPORTED ||
3060 mm_params->num_input_channels <= 0)
3061 goto exit;
3062
3063 usecase = get_usecase_from_list(out->dev, out->usecase);
Varun Balaraj003ee752017-08-07 17:54:55 +05303064 if (!usecase) {
3065 ALOGE("%s: Get usecase list failed!", __func__);
Aniket Kumar Lataf9f246e2017-09-15 15:20:16 -07003066 goto exit;
3067 }
Varun Balaraje49253e2017-07-06 19:48:56 +05303068 out->downmix_params.num_output_channels = mm_params->num_output_channels;
3069 out->downmix_params.num_input_channels = mm_params->num_input_channels;
3070
3071 out->downmix_params.has_output_channel_map =
3072 mm_params->has_output_channel_map;
3073 for (i = 0; i < mm_params->num_output_channels; i++) {
3074 out->downmix_params.output_channel_map[i] =
3075 mm_params->output_channel_map[i];
3076 }
3077
3078 out->downmix_params.has_input_channel_map =
3079 mm_params->has_input_channel_map;
3080 for (i = 0; i < mm_params->num_input_channels; i++)
3081 out->downmix_params.input_channel_map[i] =
3082 mm_params->input_channel_map[i];
3083
3084 out->downmix_params.has_mixer_coeffs = mm_params->has_mixer_coeffs;
3085 for (i = 0; i < mm_params->num_output_channels; i++)
Nikhil Latukar2e6f6242017-08-15 13:37:07 +05303086 for (j = 0; j < mm_params->num_input_channels; j++) {
3087 //Convert the channel coefficient gains in Q14 format
Varun Balaraje49253e2017-07-06 19:48:56 +05303088 out->downmix_params.mixer_coeffs[i][j] =
Nikhil Latukar2e6f6242017-08-15 13:37:07 +05303089 mm_params->mixer_coeffs[i][j] * (2 << 13);
3090 }
Varun Balaraje49253e2017-07-06 19:48:56 +05303091
3092 ret = platform_set_stream_downmix_params(out->dev->platform,
3093 out->pcm_device_id,
3094 usecase->out_snd_device,
3095 out->downmix_params);
3096
3097exit:
3098 return ret;
3099}
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05303100
3101bool audio_extn_utils_is_dolby_format(audio_format_t format)
3102{
3103 if (format == AUDIO_FORMAT_AC3 ||
3104 format == AUDIO_FORMAT_E_AC3 ||
3105 format == AUDIO_FORMAT_E_AC3_JOC)
3106 return true;
3107 else
3108 return false;
3109}
3110
`Deeraj Soman676c2702017-09-18 19:25:53 +05303111int audio_extn_utils_get_bit_width_from_string(const char *id_string)
3112{
3113 int i;
3114 const mixer_config_lookup mixer_bitwidth_config[] = {{"S24_3LE", 24},
3115 {"S32_LE", 32},
3116 {"S24_LE", 24},
3117 {"S16_LE", 16}};
3118 int num_configs = sizeof(mixer_bitwidth_config) / sizeof(mixer_bitwidth_config[0]);
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05303119
`Deeraj Soman676c2702017-09-18 19:25:53 +05303120 for (i = 0; i < num_configs; i++) {
3121 if (!strcmp(id_string, mixer_bitwidth_config[i].id_string))
3122 return mixer_bitwidth_config[i].value;
3123 }
3124
3125 return -EINVAL;
3126}
3127
3128int audio_extn_utils_get_sample_rate_from_string(const char *id_string)
3129{
3130 int i;
3131 const mixer_config_lookup mixer_samplerate_config[] = {{"KHZ_32", 32000},
3132 {"KHZ_48", 48000},
3133 {"KHZ_96", 96000},
3134 {"KHZ_144", 144000},
3135 {"KHZ_192", 192000},
3136 {"KHZ_384", 384000},
3137 {"KHZ_44P1", 44100},
3138 {"KHZ_88P2", 88200},
3139 {"KHZ_176P4", 176400},
3140 {"KHZ_352P8", 352800}};
3141 int num_configs = sizeof(mixer_samplerate_config) / sizeof(mixer_samplerate_config[0]);
3142
3143 for (i = 0; i < num_configs; i++) {
3144 if (!strcmp(id_string, mixer_samplerate_config[i].id_string))
3145 return mixer_samplerate_config[i].value;
3146 }
3147
3148 return -EINVAL;
3149}
3150
3151int audio_extn_utils_get_channels_from_string(const char *id_string)
3152{
3153 int i;
3154 const mixer_config_lookup mixer_channels_config[] = {{"One", 1},
3155 {"Two", 2},
3156 {"Three",3},
3157 {"Four", 4},
3158 {"Five", 5},
3159 {"Six", 6},
3160 {"Seven", 7},
3161 {"Eight", 8}};
3162 int num_configs = sizeof(mixer_channels_config) / sizeof(mixer_channels_config[0]);
3163
3164 for (i = 0; i < num_configs; i++) {
3165 if (!strcmp(id_string, mixer_channels_config[i].id_string))
3166 return mixer_channels_config[i].value;
3167 }
3168
3169 return -EINVAL;
3170}
Surendar karka30569792018-05-08 12:02:21 +05303171
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08003172void audio_extn_utils_release_snd_device(snd_device_t snd_device)
3173{
3174 audio_extn_dev_arbi_release(snd_device);
3175 audio_extn_sound_trigger_update_device_status(snd_device,
3176 ST_EVENT_SND_DEVICE_FREE);
3177 audio_extn_listen_update_device_status(snd_device,
3178 LISTEN_EVENT_SND_DEVICE_FREE);
3179}
3180
Weiyin Jiangd5718bd2019-09-04 16:52:08 +08003181int audio_extn_utils_get_license_params(
3182 const struct audio_device *adev,
3183 struct audio_license_params *license_params)
Surendar karka30569792018-05-08 12:02:21 +05303184{
3185 if(!license_params)
3186 return -EINVAL;
Weiyin Jiangd5718bd2019-09-04 16:52:08 +08003187
3188 return platform_get_license_by_product(adev->platform,
3189 (const char*)license_params->product, &license_params->key, license_params->license);
Surendar karka30569792018-05-08 12:02:21 +05303190}
3191
Aalique Grahame22e49102018-12-18 14:23:57 -08003192int audio_extn_utils_send_app_type_gain(struct audio_device *adev,
3193 int app_type,
3194 int *gain)
3195{
3196 int gain_cfg[4];
3197 const char *mixer_ctl_name = "App Type Gain";
3198 struct mixer_ctl *ctl;
3199 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
3200 if (!ctl) {
3201 ALOGE("%s: Could not get volume ctl mixer %s", __func__,
3202 mixer_ctl_name);
3203 return -EINVAL;
3204 }
3205 gain_cfg[0] = 0;
3206 gain_cfg[1] = app_type;
3207 gain_cfg[2] = gain[0];
3208 gain_cfg[3] = gain[1];
3209 ALOGV("%s app_type %d l(%d) r(%d)", __func__, app_type, gain[0], gain[1]);
3210 return mixer_ctl_set_array(ctl, gain_cfg,
3211 sizeof(gain_cfg)/sizeof(gain_cfg[0]));
3212}
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -07003213
Weiyin Jiangd5718bd2019-09-04 16:52:08 +08003214static void vndk_fwk_init()
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -07003215{
Weiyin Jiangd5718bd2019-09-04 16:52:08 +08003216 if (mVndkFwk.lib_handle != NULL)
3217 return;
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -07003218
Weiyin Jiangd5718bd2019-09-04 16:52:08 +08003219 mVndkFwk.lib_handle = dlopen(VNDK_FWK_LIB_PATH, RTLD_NOW);
3220 if (mVndkFwk.lib_handle == NULL) {
3221 ALOGW("%s: failed to dlopen VNDK_FWK_LIB %s", __func__, strerror(errno));
3222 return;
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -07003223 }
3224
Weiyin Jiangd5718bd2019-09-04 16:52:08 +08003225 *(void **)(&mVndkFwk.isVendorEnhancedFwk) =
3226 dlsym(mVndkFwk.lib_handle, "isRunningWithVendorEnhancedFramework");
3227 if (mVndkFwk.isVendorEnhancedFwk == NULL) {
3228 ALOGW("%s: dlsym failed %s", __func__, strerror(errno));
3229 if (mVndkFwk.lib_handle) {
3230 dlclose(mVndkFwk.lib_handle);
3231 mVndkFwk.lib_handle = NULL;
3232 }
3233 return;
3234 }
3235
3236
3237 *(void **)(&mVndkFwk.getVendorEnhancedInfo) =
3238 dlsym(mVndkFwk.lib_handle, "getVendorEnhancedInfo");
3239 if (mVndkFwk.getVendorEnhancedInfo == NULL) {
3240 ALOGW("%s: dlsym failed %s", __func__, strerror(errno));
3241 if (mVndkFwk.lib_handle) {
3242 dlclose(mVndkFwk.lib_handle);
3243 mVndkFwk.lib_handle = NULL;
3244 }
3245 }
3246
3247 return;
3248}
3249
3250bool audio_extn_utils_is_vendor_enhanced_fwk()
3251{
3252 static int is_vendor_enhanced_fwk = -EINVAL;
3253 if (is_vendor_enhanced_fwk != -EINVAL)
3254 return (bool)is_vendor_enhanced_fwk;
3255
3256 vndk_fwk_init();
3257
3258 if (mVndkFwk.isVendorEnhancedFwk != NULL) {
3259 is_vendor_enhanced_fwk = mVndkFwk.isVendorEnhancedFwk();
3260 ALOGW("%s: is_vendor_enhanced_fwk %d", __func__, is_vendor_enhanced_fwk);
3261 } else {
3262 is_vendor_enhanced_fwk = 0;
3263 ALOGW("%s: default to non enhanced_fwk config", __func__);
3264 }
3265
3266 return (bool)is_vendor_enhanced_fwk;
3267}
3268
3269int audio_extn_utils_get_vendor_enhanced_info()
3270{
3271 static int vendor_enhanced_info = -EINVAL;
3272 if (vendor_enhanced_info != -EINVAL)
3273 return vendor_enhanced_info;
3274
3275 vndk_fwk_init();
3276
3277 if (mVndkFwk.getVendorEnhancedInfo != NULL) {
3278 vendor_enhanced_info = mVndkFwk.getVendorEnhancedInfo();
3279 ALOGW("%s: vendor_enhanced_info 0x%x", __func__, vendor_enhanced_info);
3280 } else {
3281 vendor_enhanced_info = 0x0;
3282 ALOGW("%s: default to vendor_enhanced_info 0x0", __func__);
3283 }
3284
3285 return vendor_enhanced_info;
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -07003286}
Deeraj Soman14230922019-01-30 16:39:30 +05303287
Deeraj Somanfa377bf2019-02-06 12:57:59 +05303288int audio_extn_utils_get_perf_mode_flag(void)
3289{
3290#ifdef COMPRESSED_PERF_MODE_FLAG
3291 return COMPRESSED_PERF_MODE_FLAG;
3292#else
3293 return 0;
3294#endif
3295}
3296
Deeraj Soman14230922019-01-30 16:39:30 +05303297size_t audio_extn_utils_get_input_buffer_size(uint32_t sample_rate,
3298 audio_format_t format,
3299 int channel_count,
3300 int64_t duration_ms,
3301 bool is_low_latency)
3302{
3303 size_t size = 0;
3304 size_t capture_duration = AUDIO_CAPTURE_PERIOD_DURATION_MSEC;
3305 uint32_t bytes_per_period_sample = 0;
3306
3307
3308 if (audio_extn_utils_check_input_parameters(sample_rate, format, channel_count) != 0)
3309 return 0;
3310
3311 if (duration_ms >= MIN_OFFLOAD_BUFFER_DURATION_MS && duration_ms <= MAX_OFFLOAD_BUFFER_DURATION_MS)
3312 capture_duration = duration_ms;
3313
3314 size = (sample_rate * capture_duration) / 1000;
3315 if (is_low_latency)
3316 size = LOW_LATENCY_CAPTURE_PERIOD_SIZE;
3317
3318
3319 bytes_per_period_sample = audio_bytes_per_sample(format) * channel_count;
3320 size *= bytes_per_period_sample;
3321
3322 /* make sure the size is multiple of 32 bytes and additionally multiple of
3323 * the frame_size (required for 24bit samples and non-power-of-2 channel counts)
3324 * At 48 kHz mono 16-bit PCM:
3325 * 5.000 ms = 240 frames = 15*16*1*2 = 480, a whole multiple of 32 (15)
3326 * 3.333 ms = 160 frames = 10*16*1*2 = 320, a whole multiple of 32 (10)
3327 *
3328 * The loop reaches result within 32 iterations, as initial size is
3329 * already a multiple of frame_size
3330 */
3331 size = audio_extn_utils_nearest_multiple(size, audio_extn_utils_lcm(32, bytes_per_period_sample));
3332
3333 return size;
3334}
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07003335
3336int audio_extn_utils_hash_fn(void *key)
3337{
3338 return (int)key;
3339}
3340
3341bool audio_extn_utils_hash_eq(void *key1, void *key2)
3342{
3343 return (key1 == key2);
3344}