blob: 6c97447c9a9e899e81e9a9258de25a9a1179b022 [file] [log] [blame]
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001/*
Sriram Kumar9c836482020-10-27 20:34:35 +05302 * Copyright (c) 2014-2021, The Linux Foundation. All rights reserved.
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07003 * Not a Contribution.
4 *
5 * Copyright (C) 2014 The Android Open Source Project
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19
20#define LOG_TAG "audio_hw_utils"
21/* #define LOG_NDEBUG 0 */
22
Manish Dewangan27346042017-03-01 12:56:12 +053023#include <inttypes.h>
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070024#include <errno.h>
25#include <cutils/properties.h>
26#include <cutils/config_utils.h>
27#include <stdlib.h>
28#include <dlfcn.h>
29#include <cutils/str_parms.h>
Aalique Grahame22e49102018-12-18 14:23:57 -080030#include <log/log.h>
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070031#include <cutils/misc.h>
Vinay Vermaaddfa4a2018-04-29 14:03:38 +053032#include <unistd.h>
Surendar Karka287348c2019-04-10 18:31:46 +053033#include <sys/ioctl.h>
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070034
Manish Dewangan07de2142017-02-27 19:27:20 +053035
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070036#include "audio_hw.h"
37#include "platform.h"
38#include "platform_api.h"
39#include "audio_extn.h"
Deeraj Soman14230922019-01-30 16:39:30 +053040#include "voice_extn.h"
Narsinga Rao Chella212e2542014-11-17 19:57:04 -080041#include "voice.h"
Manish Dewangan07de2142017-02-27 19:27:20 +053042#include <sound/compress_params.h>
43#include <sound/compress_offload.h>
Surendar Karka287348c2019-04-10 18:31:46 +053044#include <sound/devdep_params.h>
Manish Dewangan07de2142017-02-27 19:27:20 +053045#include <tinycompress/tinycompress.h>
Revathi Uddaraju1eac8b02017-05-18 17:13:33 +053046
47#ifdef DYNAMIC_LOG_ENABLED
48#include <log_xml_parser.h>
49#define LOG_MASK HAL_MOD_FILE_UTILS
50#include <log_utils.h>
51#endif
52
Ashish Jain81eb2a82015-05-13 10:52:34 +053053#ifdef AUDIO_EXTERNAL_HDMI_ENABLED
Ashish Jain81eb2a82015-05-13 10:52:34 +053054#include "audio_parsers.h"
55#endif
Ashish Jain81eb2a82015-05-13 10:52:34 +053056
Roopesh Nataraja0f68be72020-04-28 12:32:02 -070057#define AUDIO_IO_POLICY_VENDOR_CONFIG_FILE_NAME "audio_io_policy.conf"
58#define AUDIO_OUTPUT_POLICY_VENDOR_CONFIG_FILE_NAME "audio_output_policy.conf"
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070059
60#define OUTPUTS_TAG "outputs"
Dhananjay Kumard6d32152016-10-13 16:11:03 +053061#define INPUTS_TAG "inputs"
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070062
63#define DYNAMIC_VALUE_TAG "dynamic"
64#define FLAGS_TAG "flags"
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +053065#define PROFILES_TAG "profile"
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070066#define FORMATS_TAG "formats"
67#define SAMPLING_RATES_TAG "sampling_rates"
68#define BIT_WIDTH_TAG "bit_width"
69#define APP_TYPE_TAG "app_type"
70
71#define STRING_TO_ENUM(string) { #string, string }
72#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
73
Ben Rombergera04fabc2014-11-14 12:16:03 -080074#define BASE_TABLE_SIZE 64
75#define MAX_BASEINDEX_LEN 256
76
Ben Romberger1aaaf862017-04-06 17:49:46 -070077#ifndef SND_AUDIOCODEC_TRUEHD
78#define SND_AUDIOCODEC_TRUEHD 0x00000023
79#endif
80
Vikram Panduranga93f080e2017-06-07 18:16:14 -070081#define APP_TYPE_VOIP_AUDIO 0x1113A
82
Ashish Jain81eb2a82015-05-13 10:52:34 +053083#ifdef AUDIO_EXTERNAL_HDMI_ENABLED
84#define PROFESSIONAL (1<<0) /* 0 = consumer, 1 = professional */
85#define NON_LPCM (1<<1) /* 0 = audio, 1 = non-audio */
86#define SR_44100 (0<<0) /* 44.1kHz */
87#define SR_NOTID (1<<0) /* non indicated */
88#define SR_48000 (2<<0) /* 48kHz */
89#define SR_32000 (3<<0) /* 32kHz */
90#define SR_22050 (4<<0) /* 22.05kHz */
91#define SR_24000 (6<<0) /* 24kHz */
92#define SR_88200 (8<<0) /* 88.2kHz */
93#define SR_96000 (10<<0) /* 96kHz */
94#define SR_176400 (12<<0) /* 176.4kHz */
95#define SR_192000 (14<<0) /* 192kHz */
96
97#endif
Manish Dewangan07de2142017-02-27 19:27:20 +053098
99/* ToDo: Check and update a proper value in msec */
Weiyin Jiangc49a3b52018-08-17 16:16:08 +0800100#define COMPRESS_OFFLOAD_PLAYBACK_LATENCY 50
101#define PCM_OFFLOAD_PLAYBACK_DSP_PATHDELAY 62
Samyak Jain2546f8a2019-08-29 19:24:21 +0530102#define PCM_OFFLOAD_PLAYBACK_LATENCY PCM_OFFLOAD_PLAYBACK_DSP_PATHDELAY
Manish Dewangan07de2142017-02-27 19:27:20 +0530103
Varun Balaraje49253e2017-07-06 19:48:56 +0530104#ifndef MAX_CHANNELS_SUPPORTED
105#define MAX_CHANNELS_SUPPORTED 8
106#endif
107
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -0700108#ifdef __LP64__
109#define VNDK_FWK_LIB_PATH "/vendor/lib64/libqti_vndfwk_detect.so"
110#else
111#define VNDK_FWK_LIB_PATH "/vendor/lib/libqti_vndfwk_detect.so"
112#endif
113
Kogara Naveen Kumar51bb57d2022-02-17 16:40:06 +0530114/* 24 KHz ECNR support */
115#define ECNS_USE_CASE_ACDB_DEV_ID 95
116#define ECNS_UNSUPPORTED_CAPTURE_SAMPLE_RATE_FOR_ADM 24000
117#define ECNS_SUPPORTED_CAPTURE_SAMPLE_RATE_FOR_ADM 48000
118
Weiyin Jiangd5718bd2019-09-04 16:52:08 +0800119typedef struct vndkfwk_s {
120 void *lib_handle;
121 int (*isVendorEnhancedFwk)(void);
122 int (*getVendorEnhancedInfo)(void);
123 const char *lib_name;
124} vndkfwk_t;
125
126static vndkfwk_t mVndkFwk = {
127 NULL, NULL, NULL, VNDK_FWK_LIB_PATH};
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -0700128
`Deeraj Soman676c2702017-09-18 19:25:53 +0530129typedef struct {
130 const char *id_string;
131 const int value;
132} mixer_config_lookup;
133
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700134struct string_to_enum {
135 const char *name;
136 uint32_t value;
137};
138
139const struct string_to_enum s_flag_name_to_enum_table[] = {
140 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_DIRECT),
141 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_PRIMARY),
142 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_FAST),
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800143 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_RAW),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700144 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_DEEP_BUFFER),
145 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD),
146 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_NON_BLOCKING),
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700147 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_HW_AV_SYNC),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700148 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_INCALL_MUSIC),
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700149 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH),
Naresh Tanniruee3499a2017-01-05 14:05:35 +0530150 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_TIMESTAMP),
Vikram Panduranga93f080e2017-06-07 18:16:14 -0700151 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_VOIP_RX),
Ben Romberger6c4d3812017-06-13 17:46:45 -0700152 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_BD),
Varun Balaraje49253e2017-07-06 19:48:56 +0530153 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_INTERACTIVE),
Derek Chen090dfc52018-03-22 22:15:29 -0400154 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_MEDIA),
155 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_SYS_NOTIFICATION),
156 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_NAV_GUIDANCE),
157 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_PHONE),
Susan Wang117cf6f2022-04-06 20:11:46 -0400158 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_ALERTS),
Derek Chen1bcdc6b2020-02-06 22:44:53 -0800159 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_FRONT_PASSENGER),
Derek Chendf05eea2019-08-01 13:57:49 -0700160 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_REAR_SEAT),
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530161 STRING_TO_ENUM(AUDIO_INPUT_FLAG_NONE),
162 STRING_TO_ENUM(AUDIO_INPUT_FLAG_FAST),
163 STRING_TO_ENUM(AUDIO_INPUT_FLAG_HW_HOTWORD),
164 STRING_TO_ENUM(AUDIO_INPUT_FLAG_RAW),
165 STRING_TO_ENUM(AUDIO_INPUT_FLAG_SYNC),
Dhananjay Kumaree4d2002016-10-25 18:02:58 +0530166 STRING_TO_ENUM(AUDIO_INPUT_FLAG_TIMESTAMP),
Dhananjay Kumar704ce6f2017-09-28 22:08:00 +0530167 STRING_TO_ENUM(AUDIO_INPUT_FLAG_COMPRESS),
Ralf Herzaec80262018-07-03 07:08:49 +0200168 STRING_TO_ENUM(AUDIO_INPUT_FLAG_PASSTHROUGH),
George Gao3d477082020-09-17 22:29:07 -0700169 STRING_TO_ENUM(AUDIO_INPUT_FLAG_MMAP_NOIRQ),
170 STRING_TO_ENUM(AUDIO_INPUT_FLAG_VOIP_TX),
171 STRING_TO_ENUM(AUDIO_INPUT_FLAG_HW_AV_SYNC),
172 STRING_TO_ENUM(AUDIO_INPUT_FLAG_DIRECT),
Huicheng Liu1404ba12020-09-11 01:03:25 -0400173 STRING_TO_ENUM(AUDIO_INPUT_FLAG_PRIMARY),
174 STRING_TO_ENUM(AUDIO_INPUT_FLAG_FRONT_PASSENGER),
175 STRING_TO_ENUM(AUDIO_INPUT_FLAG_REAR_SEAT),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700176};
177
178const struct string_to_enum s_format_name_to_enum_table[] = {
Ashish Jain83a6cc22016-06-28 14:34:17 +0530179 STRING_TO_ENUM(AUDIO_FORMAT_PCM_8_BIT),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700180 STRING_TO_ENUM(AUDIO_FORMAT_PCM_16_BIT),
Ashish Jain5106d362016-05-11 19:23:33 +0530181 STRING_TO_ENUM(AUDIO_FORMAT_PCM_24_BIT_PACKED),
182 STRING_TO_ENUM(AUDIO_FORMAT_PCM_8_24_BIT),
Ashish Jainf1eaa582016-05-23 20:54:24 +0530183 STRING_TO_ENUM(AUDIO_FORMAT_PCM_32_BIT),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700184 STRING_TO_ENUM(AUDIO_FORMAT_MP3),
185 STRING_TO_ENUM(AUDIO_FORMAT_AAC),
186 STRING_TO_ENUM(AUDIO_FORMAT_VORBIS),
Mingming Yinae3530f2014-07-03 16:50:18 -0700187 STRING_TO_ENUM(AUDIO_FORMAT_AMR_NB),
188 STRING_TO_ENUM(AUDIO_FORMAT_AMR_WB),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700189 STRING_TO_ENUM(AUDIO_FORMAT_AC3),
Mingming Yinae3530f2014-07-03 16:50:18 -0700190 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700191 STRING_TO_ENUM(AUDIO_FORMAT_DTS),
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530192 STRING_TO_ENUM(AUDIO_FORMAT_DTS_HD),
Ben Romberger1aaaf862017-04-06 17:49:46 -0700193 STRING_TO_ENUM(AUDIO_FORMAT_DOLBY_TRUEHD),
Naresh Tanniru928f0862017-04-07 16:44:23 -0700194 STRING_TO_ENUM(AUDIO_FORMAT_IEC61937),
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530195 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3_JOC),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700196 STRING_TO_ENUM(AUDIO_FORMAT_WMA),
197 STRING_TO_ENUM(AUDIO_FORMAT_WMA_PRO),
198 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADIF),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700199 STRING_TO_ENUM(AUDIO_FORMAT_AMR_WB_PLUS),
200 STRING_TO_ENUM(AUDIO_FORMAT_EVRC),
201 STRING_TO_ENUM(AUDIO_FORMAT_EVRCB),
202 STRING_TO_ENUM(AUDIO_FORMAT_EVRCWB),
203 STRING_TO_ENUM(AUDIO_FORMAT_QCELP),
204 STRING_TO_ENUM(AUDIO_FORMAT_MP2),
205 STRING_TO_ENUM(AUDIO_FORMAT_EVRCNW),
Amit Shekhar6f461b12014-08-01 14:52:58 -0700206 STRING_TO_ENUM(AUDIO_FORMAT_FLAC),
Satya Krishna Pindiproli70471602015-04-24 19:12:43 +0530207 STRING_TO_ENUM(AUDIO_FORMAT_ALAC),
208 STRING_TO_ENUM(AUDIO_FORMAT_APE),
Alexy Josephcd8eaed2014-12-11 12:46:53 -0800209 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LC),
210 STRING_TO_ENUM(AUDIO_FORMAT_AAC_HE_V1),
211 STRING_TO_ENUM(AUDIO_FORMAT_AAC_HE_V2),
Manish Dewangana6fc5442015-08-24 20:30:31 +0530212 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS),
Ashish Jaine513a872015-11-19 17:00:56 +0530213 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_LC),
214 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_HE_V1),
215 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_HE_V2),
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +0530216 STRING_TO_ENUM(AUDIO_FORMAT_DSD),
Arun Kumar Dasari3b174182016-12-27 13:01:14 +0530217 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM),
218 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM_LC),
219 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM_HE_V1),
220 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM_HE_V2),
Dhanalakshmi Siddani18737932016-11-29 17:33:17 +0530221 STRING_TO_ENUM(AUDIO_FORMAT_APTX),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700222};
223
Manish Dewangan3ccdea52017-02-13 19:31:54 +0530224/* payload structure avt_device drift query */
225struct audio_avt_device_drift_stats {
226 uint32_t minor_version;
Manish Dewangan338c50a2017-09-12 15:22:03 +0530227
Manish Dewangan3ccdea52017-02-13 19:31:54 +0530228 /* Indicates the device interface direction as either
229 * source (Tx) or sink (Rx).
230 */
231 uint16_t device_direction;
Manish Dewangan338c50a2017-09-12 15:22:03 +0530232
233 /* Reference timer for drift accumulation and time stamp information.
234 * currently it only support AFE_REF_TIMER_TYPE_AVTIMER
235 */
236 uint16_t reference_timer;
Manish Dewangan3ccdea52017-02-13 19:31:54 +0530237 struct audio_avt_device_drift_param drift_param;
Manish Dewangan338c50a2017-09-12 15:22:03 +0530238} __attribute__((packed));
Manish Dewangan3ccdea52017-02-13 19:31:54 +0530239
Ben Rombergera04fabc2014-11-14 12:16:03 -0800240static char bTable[BASE_TABLE_SIZE] = {
241 'A','B','C','D','E','F','G','H','I','J','K','L',
242 'M','N','O','P','Q','R','S','T','U','V','W','X',
243 'Y','Z','a','b','c','d','e','f','g','h','i','j',
244 'k','l','m','n','o','p','q','r','s','t','u','v',
245 'w','x','y','z','0','1','2','3','4','5','6','7',
246 '8','9','+','/'
247};
248
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700249static uint32_t string_to_enum(const struct string_to_enum *table, size_t size,
250 const char *name)
251{
252 size_t i;
253 for (i = 0; i < size; i++) {
254 if (strcmp(table[i].name, name) == 0) {
255 ALOGV("%s found %s", __func__, table[i].name);
256 return table[i].value;
257 }
258 }
George Gao3d477082020-09-17 22:29:07 -0700259 ALOGE("%s cound not find %s", __func__, name);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700260 return 0;
261}
262
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530263static audio_io_flags_t parse_flag_names(char *name)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700264{
265 uint32_t flag = 0;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530266 audio_io_flags_t io_flags;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800267 char *last_r;
268 char *flag_name = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700269 while (flag_name != NULL) {
270 if (strlen(flag_name) != 0) {
271 flag |= string_to_enum(s_flag_name_to_enum_table,
272 ARRAY_SIZE(s_flag_name_to_enum_table),
273 flag_name);
274 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800275 flag_name = strtok_r(NULL, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700276 }
277
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800278 ALOGV("parse_flag_names: flag - %x", flag);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530279 io_flags.in_flags = (audio_input_flags_t)flag;
280 io_flags.out_flags = (audio_output_flags_t)flag;
281 return io_flags;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700282}
283
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530284static void parse_format_names(char *name, struct streams_io_cfg *s_info)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700285{
286 struct stream_format *sf_info = NULL;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800287 char *last_r;
288 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700289
290 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG) == 0)
291 return;
292
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530293 list_init(&s_info->format_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700294 while (str != NULL) {
295 audio_format_t format = (audio_format_t)string_to_enum(s_format_name_to_enum_table,
296 ARRAY_SIZE(s_format_name_to_enum_table), str);
297 ALOGV("%s: format - %d", __func__, format);
298 if (format != 0) {
299 sf_info = (struct stream_format *)calloc(1, sizeof(struct stream_format));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700300 if (sf_info == NULL)
301 break; /* return whatever was parsed */
302
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700303 sf_info->format = format;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530304 list_add_tail(&s_info->format_list, &sf_info->list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700305 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800306 str = strtok_r(NULL, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700307 }
308}
309
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530310static void parse_sample_rate_names(char *name, struct streams_io_cfg *s_info)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700311{
Amit Shekhar6f461b12014-08-01 14:52:58 -0700312 struct stream_sample_rate *ss_info = NULL;
313 uint32_t sample_rate = 48000;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800314 char *last_r;
315 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700316
Amit Shekhar6f461b12014-08-01 14:52:58 -0700317 if (str != NULL && 0 == strcmp(str, DYNAMIC_VALUE_TAG))
318 return;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700319
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530320 list_init(&s_info->sample_rate_list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700321 while (str != NULL) {
322 sample_rate = (uint32_t)strtol(str, (char **)NULL, 10);
323 ALOGV("%s: sample_rate - %d", __func__, sample_rate);
324 if (0 != sample_rate) {
325 ss_info = (struct stream_sample_rate *)calloc(1, sizeof(struct stream_sample_rate));
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800326 if (!ss_info) {
327 ALOGE("%s: memory allocation failure", __func__);
328 return;
329 }
Amit Shekhar6f461b12014-08-01 14:52:58 -0700330 ss_info->sample_rate = sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530331 list_add_tail(&s_info->sample_rate_list, &ss_info->list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700332 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800333 str = strtok_r(NULL, "|", &last_r);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700334 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700335}
336
337static int parse_bit_width_names(char *name)
338{
339 int bit_width = 16;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800340 char *last_r;
341 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700342
343 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG))
344 bit_width = (int)strtol(str, (char **)NULL, 10);
345
346 ALOGV("%s: bit_width - %d", __func__, bit_width);
347 return bit_width;
348}
349
350static int parse_app_type_names(void *platform, char *name)
351{
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700352 int app_type = platform_get_default_app_type(platform);
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800353 char *last_r;
354 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700355
356 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG))
357 app_type = (int)strtol(str, (char **)NULL, 10);
358
359 ALOGV("%s: app_type - %d", __func__, app_type);
360 return app_type;
361}
362
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530363static void update_streams_cfg_list(cnode *root, void *platform,
364 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700365{
366 cnode *node = root->first_child;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530367 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700368
369 ALOGV("%s", __func__);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530370 s_info = (struct streams_io_cfg *)calloc(1, sizeof(struct streams_io_cfg));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700371
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530372 if (!s_info) {
373 ALOGE("failed to allocate mem for s_info list element");
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700374 return;
375 }
376
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700377 while (node) {
378 if (strcmp(node->name, FLAGS_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530379 s_info->flags = parse_flag_names((char *)node->value);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530380 } else if (strcmp(node->name, PROFILES_TAG) == 0) {
381 strlcpy(s_info->profile, (char *)node->value, sizeof(s_info->profile));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700382 } else if (strcmp(node->name, FORMATS_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530383 parse_format_names((char *)node->value, s_info);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700384 } else if (strcmp(node->name, SAMPLING_RATES_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530385 s_info->app_type_cfg.sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
386 parse_sample_rate_names((char *)node->value, s_info);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700387 } else if (strcmp(node->name, BIT_WIDTH_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530388 s_info->app_type_cfg.bit_width = parse_bit_width_names((char *)node->value);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700389 } else if (strcmp(node->name, APP_TYPE_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530390 s_info->app_type_cfg.app_type = parse_app_type_names(platform, (char *)node->value);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700391 }
392 node = node->next;
393 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530394 list_add_tail(streams_cfg_list, &s_info->list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700395}
396
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530397static void load_cfg_list(cnode *root, void *platform,
398 struct listnode *streams_output_cfg_list,
399 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700400{
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530401 cnode *node = NULL;
402
403 node = config_find(root, OUTPUTS_TAG);
404 if (node != NULL) {
405 node = node->first_child;
406 while (node) {
407 ALOGV("%s: loading output %s", __func__, node->name);
408 update_streams_cfg_list(node, platform, streams_output_cfg_list);
409 node = node->next;
410 }
411 } else {
412 ALOGI("%s: could not load output, node is NULL", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700413 }
414
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530415 node = config_find(root, INPUTS_TAG);
416 if (node != NULL) {
417 node = node->first_child;
418 while (node) {
419 ALOGV("%s: loading input %s", __func__, node->name);
420 update_streams_cfg_list(node, platform, streams_input_cfg_list);
421 node = node->next;
422 }
423 } else {
424 ALOGI("%s: could not load input, node is NULL", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700425 }
426}
427
428static void send_app_type_cfg(void *platform, struct mixer *mixer,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530429 struct listnode *streams_output_cfg_list,
430 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700431{
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530432 size_t app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT] = {0};
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700433 int length = 0, i, num_app_types = 0;
434 struct listnode *node;
435 bool update;
436 struct mixer_ctl *ctl = NULL;
437 const char *mixer_ctl_name = "App Type Config";
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530438 struct streams_io_cfg *s_info = NULL;
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800439 uint32_t target_bit_width = 0;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700440
441 if (!mixer) {
442 ALOGE("%s: mixer is null",__func__);
443 return;
444 }
445 ctl = mixer_get_ctl_by_name(mixer, mixer_ctl_name);
446 if (!ctl) {
447 ALOGE("%s: Could not get ctl for mixer cmd - %s",__func__, mixer_ctl_name);
448 return;
449 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530450 app_type_cfg[length++] = num_app_types;
451
452 if (list_empty(streams_output_cfg_list)) {
453 app_type_cfg[length++] = platform_get_default_app_type_v2(platform, PCM_PLAYBACK);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700454 app_type_cfg[length++] = 48000;
455 app_type_cfg[length++] = 16;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530456 num_app_types += 1;
457 }
458 if (list_empty(streams_input_cfg_list)) {
459 app_type_cfg[length++] = platform_get_default_app_type_v2(platform, PCM_CAPTURE);
460 app_type_cfg[length++] = 48000;
461 app_type_cfg[length++] = 16;
462 num_app_types += 1;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700463 }
464
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800465 /* get target bit width for ADM enforce mode */
466 target_bit_width = adev_get_dsp_bit_width_enforce_mode();
467
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700468 list_for_each(node, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530469 s_info = node_to_item(node, struct streams_io_cfg, list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700470 update = true;
471 for (i=0; i<length; i=i+3) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530472 if (app_type_cfg[i+1] == 0)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700473 break;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530474 else if (app_type_cfg[i+1] == (size_t)s_info->app_type_cfg.app_type) {
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530475 if (app_type_cfg[i+2] < (size_t)s_info->app_type_cfg.sample_rate)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530476 app_type_cfg[i+2] = s_info->app_type_cfg.sample_rate;
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530477 if (app_type_cfg[i+3] < (size_t)s_info->app_type_cfg.bit_width)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530478 app_type_cfg[i+3] = s_info->app_type_cfg.bit_width;
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800479 /* ADM bit width = max(enforce_bit_width, bit_width from s_info */
480 if (audio_extn_is_dsp_bit_width_enforce_mode_supported(s_info->flags.out_flags) &&
481 (target_bit_width > app_type_cfg[i+3]))
482 app_type_cfg[i+3] = target_bit_width;
483
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700484 update = false;
485 break;
486 }
487 }
488 if (update && ((length + 3) <= MAX_LENGTH_MIXER_CONTROL_IN_INT)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530489 num_app_types += 1;
490 app_type_cfg[length++] = s_info->app_type_cfg.app_type;
491 app_type_cfg[length++] = s_info->app_type_cfg.sample_rate;
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800492 app_type_cfg[length] = s_info->app_type_cfg.bit_width;
493 if (audio_extn_is_dsp_bit_width_enforce_mode_supported(s_info->flags.out_flags) &&
494 (target_bit_width > app_type_cfg[length]))
495 app_type_cfg[length] = target_bit_width;
496
497 length++;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530498 }
499 }
500 list_for_each(node, streams_input_cfg_list) {
501 s_info = node_to_item(node, struct streams_io_cfg, list);
502 update = true;
503 for (i=0; i<length; i=i+3) {
504 if (app_type_cfg[i+1] == 0)
505 break;
506 else if (app_type_cfg[i+1] == (size_t)s_info->app_type_cfg.app_type) {
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530507 if (app_type_cfg[i+2] < (size_t)s_info->app_type_cfg.sample_rate)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530508 app_type_cfg[i+2] = s_info->app_type_cfg.sample_rate;
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530509 if (app_type_cfg[i+3] < (size_t)s_info->app_type_cfg.bit_width)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530510 app_type_cfg[i+3] = s_info->app_type_cfg.bit_width;
511 update = false;
512 break;
513 }
514 }
515 if (update && ((length + 3) <= MAX_LENGTH_MIXER_CONTROL_IN_INT)) {
516 num_app_types += 1;
517 app_type_cfg[length++] = s_info->app_type_cfg.app_type;
518 app_type_cfg[length++] = s_info->app_type_cfg.sample_rate;
519 app_type_cfg[length++] = s_info->app_type_cfg.bit_width;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700520 }
521 }
522 ALOGV("%s: num_app_types: %d", __func__, num_app_types);
523 if (num_app_types) {
524 app_type_cfg[0] = num_app_types;
525 mixer_ctl_set_array(ctl, app_type_cfg, length);
526 }
527}
528
Roopesh Nataraja0f68be72020-04-28 12:32:02 -0700529/* Function to retrieve audio vendor configs path */
530void audio_get_vendor_config_path (char* config_file_path, int path_size)
531{
532 char vendor_sku[PROPERTY_VALUE_MAX] = {'\0'};
533 if (property_get("ro.boot.product.vendor.sku", vendor_sku, "") <= 0) {
534#ifdef LINUX_ENABLED
535 /* Audio configs are stored in /etc */
536 snprintf(config_file_path, path_size, "%s", "/etc");
537#else
538 /* Audio configs are stored in /vendor/etc */
539 snprintf(config_file_path, path_size, "%s", "/vendor/etc");
540#endif
541 } else {
542 /* Audio configs are stored in /vendor/etc/audio/sku_${vendor_sku} */
543 snprintf(config_file_path, path_size,
544 "%s%s", "/vendor/etc/audio/sku_", vendor_sku);
545 }
546}
547
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530548void audio_extn_utils_update_streams_cfg_lists(void *platform,
549 struct mixer *mixer,
550 struct listnode *streams_output_cfg_list,
551 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700552{
553 cnode *root;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530554 char *data = NULL;
Roopesh Nataraja0f68be72020-04-28 12:32:02 -0700555 char vendor_config_path[VENDOR_CONFIG_PATH_MAX_LENGTH];
556 char audio_io_policy_file[VENDOR_CONFIG_FILE_MAX_LENGTH];
557 char audio_output_policy_file[VENDOR_CONFIG_FILE_MAX_LENGTH];
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700558
559 ALOGV("%s", __func__);
560 list_init(streams_output_cfg_list);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530561 list_init(streams_input_cfg_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700562
563 root = config_node("", "");
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700564 if (root == NULL) {
565 ALOGE("cfg_list, NULL config root");
566 return;
567 }
568
Roopesh Nataraja0f68be72020-04-28 12:32:02 -0700569 /* Get path for audio configuration files in vendor */
570 audio_get_vendor_config_path(vendor_config_path,
571 sizeof(vendor_config_path));
572
573 /* Get path for audio_io_policy_file in vendor */
574 snprintf(audio_io_policy_file, sizeof(audio_io_policy_file),
575 "%s/%s", vendor_config_path, AUDIO_IO_POLICY_VENDOR_CONFIG_FILE_NAME);
576
577 /* Load audio_io_policy_file from vendor */
578 data = (char *)load_file(audio_io_policy_file, NULL);
579
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530580 if (data == NULL) {
581 ALOGD("%s: failed to open io config file(%s), trying older config file",
Roopesh Nataraja0f68be72020-04-28 12:32:02 -0700582 __func__, audio_io_policy_file);
583
584 /* Get path for audio_output_policy_file in vendor */
585 snprintf(audio_output_policy_file, sizeof(audio_output_policy_file),
586 "%s/%s", vendor_config_path,
587 AUDIO_OUTPUT_POLICY_VENDOR_CONFIG_FILE_NAME);
588
589 /* Load audio_output_policy_file from vendor */
590 data = (char *)load_file(audio_output_policy_file, NULL);
591
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530592 if (data == NULL) {
593 send_app_type_cfg(platform, mixer,
594 streams_output_cfg_list,
595 streams_input_cfg_list);
596 ALOGE("%s: could not load io policy config!", __func__);
yidongh6eb4f752017-04-19 18:20:57 +0800597 free(root);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530598 return;
599 }
600 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700601
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530602 config_load(root, data);
603 load_cfg_list(root, platform, streams_output_cfg_list,
604 streams_input_cfg_list);
605
606 send_app_type_cfg(platform, mixer, streams_output_cfg_list,
607 streams_input_cfg_list);
Alexy Josephaee4fdd2016-01-29 13:02:07 -0800608
609 config_free(root);
yidongh6eb4f752017-04-19 18:20:57 +0800610 free(root);
Alexy Josephaee4fdd2016-01-29 13:02:07 -0800611 free(data);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700612}
613
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530614static void audio_extn_utils_dump_streams_cfg_list(
615 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700616{
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700617 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530618 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700619 struct stream_format *sf_info;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700620 struct stream_sample_rate *ss_info;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530621
622 list_for_each(node_i, streams_cfg_list) {
623 s_info = node_to_item(node_i, struct streams_io_cfg, list);
624 ALOGV("%s: flags-%d, sample_rate-%d, bit_width-%d, app_type-%d",
625 __func__, s_info->flags.out_flags, s_info->app_type_cfg.sample_rate,
626 s_info->app_type_cfg.bit_width, s_info->app_type_cfg.app_type);
627 list_for_each(node_j, &s_info->format_list) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700628 sf_info = node_to_item(node_j, struct stream_format, list);
629 ALOGV("format-%x", sf_info->format);
630 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530631 list_for_each(node_j, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700632 ss_info = node_to_item(node_j, struct stream_sample_rate, list);
633 ALOGV("sample rate-%d", ss_info->sample_rate);
634 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700635 }
636}
637
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530638void audio_extn_utils_dump_streams_cfg_lists(
639 struct listnode *streams_output_cfg_list,
640 struct listnode *streams_input_cfg_list)
641{
642 ALOGV("%s", __func__);
643 audio_extn_utils_dump_streams_cfg_list(streams_output_cfg_list);
644 audio_extn_utils_dump_streams_cfg_list(streams_input_cfg_list);
645}
646
647static void audio_extn_utils_release_streams_cfg_list(
648 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700649{
650 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530651 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700652
653 ALOGV("%s", __func__);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530654
655 while (!list_empty(streams_cfg_list)) {
656 node_i = list_head(streams_cfg_list);
657 s_info = node_to_item(node_i, struct streams_io_cfg, list);
658 while (!list_empty(&s_info->format_list)) {
659 node_j = list_head(&s_info->format_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700660 list_remove(node_j);
661 free(node_to_item(node_j, struct stream_format, list));
662 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530663 while (!list_empty(&s_info->sample_rate_list)) {
664 node_j = list_head(&s_info->sample_rate_list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700665 list_remove(node_j);
666 free(node_to_item(node_j, struct stream_sample_rate, list));
667 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700668 list_remove(node_i);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530669 free(node_to_item(node_i, struct streams_io_cfg, list));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700670 }
671}
672
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530673void audio_extn_utils_release_streams_cfg_lists(
674 struct listnode *streams_output_cfg_list,
675 struct listnode *streams_input_cfg_list)
676{
677 ALOGV("%s", __func__);
678 audio_extn_utils_release_streams_cfg_list(streams_output_cfg_list);
679 audio_extn_utils_release_streams_cfg_list(streams_input_cfg_list);
680}
681
682static bool set_app_type_cfg(struct streams_io_cfg *s_info,
683 struct stream_app_type_cfg *app_type_cfg,
684 uint32_t sample_rate, uint32_t bit_width)
Amit Shekhar6f461b12014-08-01 14:52:58 -0700685 {
686 struct listnode *node_i;
687 struct stream_sample_rate *ss_info;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530688 list_for_each(node_i, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700689 ss_info = node_to_item(node_i, struct stream_sample_rate, list);
690 if ((sample_rate <= ss_info->sample_rate) &&
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530691 (bit_width == s_info->app_type_cfg.bit_width)) {
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700692
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530693 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700694 app_type_cfg->sample_rate = ss_info->sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530695 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700696 ALOGV("%s app_type_cfg->app_type %d, app_type_cfg->sample_rate %d, app_type_cfg->bit_width %d",
697 __func__, app_type_cfg->app_type, app_type_cfg->sample_rate, app_type_cfg->bit_width);
698 return true;
699 }
700 }
701 /*
702 * Reiterate through the list assuming dafault sample rate.
703 * Handles scenario where input sample rate is higher
704 * than all sample rates in list for the input bit width.
705 */
706 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
Apoorv Raghuvanshif59bb222015-02-18 12:23:23 -0800707
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530708 list_for_each(node_i, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700709 ss_info = node_to_item(node_i, struct stream_sample_rate, list);
710 if ((sample_rate <= ss_info->sample_rate) &&
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530711 (bit_width == s_info->app_type_cfg.bit_width)) {
712 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700713 app_type_cfg->sample_rate = sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530714 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Apoorv Raghuvanshif59bb222015-02-18 12:23:23 -0800715 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 -0700716 __func__, app_type_cfg->app_type, app_type_cfg->sample_rate, app_type_cfg->bit_width);
717 return true;
718 }
719 }
720 return false;
721}
722
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530723void audio_extn_utils_update_stream_input_app_type_cfg(void *platform,
724 struct listnode *streams_input_cfg_list,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -0800725 struct listnode *devices __unused,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530726 audio_input_flags_t flags,
727 audio_format_t format,
728 uint32_t sample_rate,
729 uint32_t bit_width,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530730 char* profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530731 struct stream_app_type_cfg *app_type_cfg)
732{
733 struct listnode *node_i, *node_j;
734 struct streams_io_cfg *s_info;
735 struct stream_format *sf_info;
736
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530737 ALOGV("%s: flags: 0x%x, format: 0x%x sample_rate %d, profile %s",
738 __func__, flags, format, sample_rate, profile);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530739
740 list_for_each(node_i, streams_input_cfg_list) {
741 s_info = node_to_item(node_i, struct streams_io_cfg, list);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530742 /* Along with flags do profile matching if set at either end.*/
743 if (s_info->flags.in_flags == flags &&
744 ((profile[0] == '\0' && s_info->profile[0] == '\0') ||
745 strncmp(s_info->profile, profile, sizeof(s_info->profile)) == 0)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530746 list_for_each(node_j, &s_info->format_list) {
747 sf_info = node_to_item(node_j, struct stream_format, list);
748 if (sf_info->format == format) {
749 if (set_app_type_cfg(s_info, app_type_cfg, sample_rate, bit_width))
750 return;
751 }
752 }
753 }
754 }
755 ALOGW("%s: App type could not be selected. Falling back to default", __func__);
756 app_type_cfg->app_type = platform_get_default_app_type_v2(platform, PCM_CAPTURE);
757 app_type_cfg->sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
758 app_type_cfg->bit_width = 16;
Kogara Naveen Kumar51bb57d2022-02-17 16:40:06 +0530759 if ((flags & AUDIO_INPUT_FLAG_TIMESTAMP) == 0 &&
760 (flags & AUDIO_INPUT_FLAG_COMPRESS) == 0 &&
761 (flags & AUDIO_INPUT_FLAG_FAST) != 0) {
762 // Support low latency record for different sample rates
763 app_type_cfg->sample_rate = sample_rate;
764 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530765}
766
767void audio_extn_utils_update_stream_output_app_type_cfg(void *platform,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700768 struct listnode *streams_output_cfg_list,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -0800769 struct listnode *devices,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700770 audio_output_flags_t flags,
771 audio_format_t format,
Amit Shekhar6f461b12014-08-01 14:52:58 -0700772 uint32_t sample_rate,
773 uint32_t bit_width,
Manish Dewangan837dc462015-05-27 10:17:41 +0530774 audio_channel_mask_t channel_mask,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530775 char *profile,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700776 struct stream_app_type_cfg *app_type_cfg)
777{
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +0530778 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530779 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700780 struct stream_format *sf_info;
Manish Dewangan837dc462015-05-27 10:17:41 +0530781 char value[PROPERTY_VALUE_MAX] = {0};
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700782
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -0800783 if (compare_device_type(devices, AUDIO_DEVICE_OUT_SPEAKER)) {
Ramu Gottipati074fa4d2018-09-11 20:05:51 +0530784 int bw = platform_get_snd_device_bit_width(SND_DEVICE_OUT_SPEAKER);
785 if ((-ENOSYS != bw) && (bit_width > (uint32_t)bw))
Amit Shekhar5a39c912014-10-14 15:39:30 -0700786 bit_width = (uint32_t)bw;
Ramu Gottipati074fa4d2018-09-11 20:05:51 +0530787 else if (-ENOSYS == bw)
788 bit_width = CODEC_BACKEND_DEFAULT_BIT_WIDTH;
Amit Shekhar1d896042014-10-03 13:16:09 -0700789 sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Aniket Kumar Latad13758f2020-08-06 15:11:36 -0700790 ALOGV("%s Allowing 24 and above bits playback on speaker \
791 ONLY at default sampling rate", __func__);
Amit Shekhar1d896042014-10-03 13:16:09 -0700792 }
793
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -0700794 property_get("vendor.audio.playback.mch.downsample",value,"");
Manish Dewangan837dc462015-05-27 10:17:41 +0530795 if (!strncmp("true", value, sizeof("true"))) {
796 if ((popcount(channel_mask) > 2) &&
797 (sample_rate > CODEC_BACKEND_DEFAULT_SAMPLE_RATE) &&
Aalique Grahame5cd12ff2017-10-19 10:57:00 -0700798 !(flags & (audio_output_flags_t)AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH)) {
Manish Dewangan837dc462015-05-27 10:17:41 +0530799 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
800 ALOGD("%s: MCH session defaulting sample rate to %d",
801 __func__, sample_rate);
802 }
803 }
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +0530804
805 /* Set sampling rate to 176.4 for DSD64
806 * and 352.8Khz for DSD128.
807 * Set Bit Width to 16. output will be 16 bit
808 * post DoP in ASM.
809 */
Aalique Grahame5cd12ff2017-10-19 10:57:00 -0700810 if ((flags & (audio_output_flags_t)AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH) &&
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +0530811 (format == AUDIO_FORMAT_DSD)) {
812 bit_width = 16;
813 if (sample_rate == INPUT_SAMPLING_RATE_DSD64)
814 sample_rate = OUTPUT_SAMPLING_RATE_DSD64;
815 else if (sample_rate == INPUT_SAMPLING_RATE_DSD128)
816 sample_rate = OUTPUT_SAMPLING_RATE_DSD128;
817 }
818
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +0530819
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800820 ALOGV("%s: flags: %x, format: %x sample_rate %d, profile %s, app_type %d",
821 __func__, flags, format, sample_rate, profile, app_type_cfg->app_type);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700822 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);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530824 /* Along with flags do profile matching if set at either end.*/
825 if (s_info->flags.out_flags == flags &&
826 ((profile[0] == '\0' && s_info->profile[0] == '\0') ||
827 strncmp(s_info->profile, profile, sizeof(s_info->profile)) == 0)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530828 list_for_each(node_j, &s_info->format_list) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700829 sf_info = node_to_item(node_j, struct stream_format, list);
830 if (sf_info->format == format) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530831 if (set_app_type_cfg(s_info, app_type_cfg, sample_rate, bit_width))
Amit Shekhar6f461b12014-08-01 14:52:58 -0700832 return;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700833 }
834 }
835 }
836 }
837 list_for_each(node_i, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530838 s_info = node_to_item(node_i, struct streams_io_cfg, list);
839 if (s_info->flags.out_flags == AUDIO_OUTPUT_FLAG_PRIMARY) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700840 ALOGV("Compatible output profile not found.");
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530841 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
842 app_type_cfg->sample_rate = s_info->app_type_cfg.sample_rate;
843 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700844 ALOGV("%s Default to primary output: App type: %d sample_rate %d",
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530845 __func__, s_info->app_type_cfg.app_type, app_type_cfg->sample_rate);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700846 return;
847 }
848 }
849 ALOGW("%s: App type could not be selected. Falling back to default", __func__);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700850 app_type_cfg->app_type = platform_get_default_app_type(platform);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700851 app_type_cfg->sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700852 app_type_cfg->bit_width = 16;
Kogara Naveen Kumar51bb57d2022-02-17 16:40:06 +0530853 if (compare_device_type(devices, AUDIO_DEVICE_OUT_BUS) && (flags &
854 (audio_output_flags_t)AUDIO_OUTPUT_FLAG_FAST)) {
855 // Support low latency playback for different sample rates
856 app_type_cfg->sample_rate = sample_rate;
857 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700858}
859
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +0530860static bool audio_is_this_native_usecase(struct audio_usecase *uc)
861{
862 bool native_usecase = false;
863 struct stream_out *out = (struct stream_out*) uc->stream.out;
864
865 if (PCM_PLAYBACK == uc->type && out != NULL &&
866 NATIVE_AUDIO_MODE_INVALID != platform_get_native_support() &&
867 is_offload_usecase(uc->id) &&
868 (out->sample_rate == OUTPUT_SAMPLING_RATE_44100))
869 native_usecase = true;
870
871 return native_usecase;
872}
873
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800874bool audio_extn_is_dsp_bit_width_enforce_mode_supported(audio_output_flags_t flags)
875{
876 /* DSP bitwidth enforce mode for ADM and AFE:
877 * includes:
878 * deep buffer, low latency, direct pcm and offload.
879 * excludes:
880 * ull(raw+fast), VOIP.
881 */
882 if ((flags & AUDIO_OUTPUT_FLAG_VOIP_RX) ||
883 ((flags & AUDIO_OUTPUT_FLAG_RAW) &&
884 (flags & AUDIO_OUTPUT_FLAG_FAST)))
885 return false;
886
887
888 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) ||
889 (flags & AUDIO_OUTPUT_FLAG_DIRECT) ||
890 (flags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) ||
891 (flags & AUDIO_OUTPUT_FLAG_PRIMARY))
892 return true;
893 else
894 return false;
895}
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800896
897static inline bool audio_is_vr_mode_on(struct audio_device *(__attribute__((unused)) adev))
898{
899 return adev->vr_audio_mode_enabled;
900}
901
Derek Chenf7092792017-05-23 12:23:53 -0400902static void audio_extn_btsco_get_sample_rate(int snd_device, int *sample_rate)
903{
904 switch (snd_device) {
905 case SND_DEVICE_OUT_BT_SCO:
906 case SND_DEVICE_IN_BT_SCO_MIC:
907 case SND_DEVICE_IN_BT_SCO_MIC_NREC:
908 *sample_rate = 8000;
909 break;
910 case SND_DEVICE_OUT_BT_SCO_WB:
911 case SND_DEVICE_IN_BT_SCO_MIC_WB:
912 case SND_DEVICE_IN_BT_SCO_MIC_WB_NREC:
913 *sample_rate = 16000;
914 break;
915 default:
Aniket Kumar Latad13758f2020-08-06 15:11:36 -0700916 ALOGV("%s:Not a BT SCO device, need not update sampling rate\n", __func__);
Derek Chenf7092792017-05-23 12:23:53 -0400917 break;
918 }
919}
920
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530921void audio_extn_utils_update_stream_app_type_cfg_for_usecase(
922 struct audio_device *adev,
923 struct audio_usecase *usecase)
924{
925 ALOGV("%s", __func__);
926
927 switch(usecase->type) {
928 case PCM_PLAYBACK:
929 audio_extn_utils_update_stream_output_app_type_cfg(adev->platform,
930 &adev->streams_output_cfg_list,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -0800931 &usecase->stream.out->device_list,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530932 usecase->stream.out->flags,
Aalique Grahame65780b52017-09-27 14:59:56 -0700933 usecase->stream.out->hal_op_format,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530934 usecase->stream.out->sample_rate,
935 usecase->stream.out->bit_width,
936 usecase->stream.out->channel_mask,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530937 usecase->stream.out->profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530938 &usecase->stream.out->app_type_cfg);
939 ALOGV("%s Selected apptype: %d", __func__, usecase->stream.out->app_type_cfg.app_type);
940 break;
941 case PCM_CAPTURE:
Revathi Uddarajud9f23d92020-07-27 10:55:06 +0530942 if (usecase->id == USECASE_AUDIO_RECORD_VOIP
943 || usecase->id == USECASE_AUDIO_RECORD_VOIP_LOW_LATENCY)
Vikram Panduranga93f080e2017-06-07 18:16:14 -0700944 usecase->stream.in->app_type_cfg.app_type = APP_TYPE_VOIP_AUDIO;
945 else
946 audio_extn_utils_update_stream_input_app_type_cfg(adev->platform,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530947 &adev->streams_input_cfg_list,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -0800948 &usecase->stream.in->device_list,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530949 usecase->stream.in->flags,
950 usecase->stream.in->format,
951 usecase->stream.in->sample_rate,
952 usecase->stream.in->bit_width,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530953 usecase->stream.in->profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530954 &usecase->stream.in->app_type_cfg);
955 ALOGV("%s Selected apptype: %d", __func__, usecase->stream.in->app_type_cfg.app_type);
956 break;
Surendar Karka93cd25a2018-08-28 14:21:37 +0530957 case TRANSCODE_LOOPBACK_RX :
Siddartha Shaik343abc62017-08-08 11:15:25 +0530958 audio_extn_utils_update_stream_output_app_type_cfg(adev->platform,
959 &adev->streams_output_cfg_list,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -0800960 &usecase->stream.inout->out_config.device_list,
Siddartha Shaik343abc62017-08-08 11:15:25 +0530961 0,
962 usecase->stream.inout->out_config.format,
963 usecase->stream.inout->out_config.sample_rate,
964 usecase->stream.inout->out_config.bit_width,
965 usecase->stream.inout->out_config.channel_mask,
966 usecase->stream.inout->profile,
967 &usecase->stream.inout->out_app_type_cfg);
968 ALOGV("%s Selected apptype: %d", __func__, usecase->stream.inout->out_app_type_cfg.app_type);
969 break;
Derek Chenf7092792017-05-23 12:23:53 -0400970 case PCM_HFP_CALL:
971 switch (usecase->id) {
972 case USECASE_AUDIO_HFP_SCO:
973 case USECASE_AUDIO_HFP_SCO_WB:
974 audio_extn_btsco_get_sample_rate(usecase->out_snd_device,
975 &usecase->out_app_type_cfg.sample_rate);
Derek Chend7307012019-11-12 12:10:41 -0500976 usecase->in_app_type_cfg.sample_rate = usecase->out_app_type_cfg.sample_rate;
Derek Chenf7092792017-05-23 12:23:53 -0400977 break;
978 case USECASE_AUDIO_HFP_SCO_DOWNLINK:
979 case USECASE_AUDIO_HFP_SCO_WB_DOWNLINK:
980 audio_extn_btsco_get_sample_rate(usecase->in_snd_device,
981 &usecase->in_app_type_cfg.sample_rate);
Derek Chend7307012019-11-12 12:10:41 -0500982 usecase->out_app_type_cfg.sample_rate = usecase->in_app_type_cfg.sample_rate;
Derek Chenf7092792017-05-23 12:23:53 -0400983 break;
984 default:
985 ALOGE("%s: usecase id (%d) not supported, use default sample rate",
986 __func__, usecase->id);
987 usecase->in_app_type_cfg.sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
988 usecase->out_app_type_cfg.sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
989 break;
990 }
991 /* update out_app_type_cfg */
992 usecase->out_app_type_cfg.bit_width =
993 platform_get_snd_device_bit_width(usecase->out_snd_device);
994 usecase->out_app_type_cfg.app_type =
995 platform_get_default_app_type_v2(adev->platform, PCM_PLAYBACK);
996 /* update in_app_type_cfg */
997 usecase->in_app_type_cfg.bit_width =
998 platform_get_snd_device_bit_width(usecase->in_snd_device);
999 usecase->in_app_type_cfg.app_type =
1000 platform_get_default_app_type_v2(adev->platform, PCM_CAPTURE);
1001 ALOGV("%s Selected apptype: playback %d capture %d",
1002 __func__, usecase->out_app_type_cfg.app_type, usecase->in_app_type_cfg.app_type);
1003 break;
Derek Chena30a5f42019-12-03 11:17:09 -05001004 case ICC_CALL:
1005 /* ICC usecase: Loopback from TERT_TDM_TX to TERT_TDM_RX */
1006 /* update out_app_type_cfg */
1007 usecase->out_app_type_cfg.sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
1008 usecase->out_app_type_cfg.bit_width = platform_get_snd_device_bit_width(usecase->out_snd_device);
1009 usecase->out_app_type_cfg.app_type = platform_get_default_app_type_v2(adev->platform, PCM_PLAYBACK);
1010 /* update in_app_type_cfg */
1011 usecase->in_app_type_cfg.sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
1012 usecase->in_app_type_cfg.bit_width = platform_get_snd_device_bit_width(usecase->in_snd_device);
1013 usecase->in_app_type_cfg.app_type = platform_get_default_app_type_v2(adev->platform, PCM_CAPTURE);
1014
1015 ALOGV("%s Selected apptype: playback %d capture %d",
1016 __func__, usecase->out_app_type_cfg.app_type, usecase->in_app_type_cfg.app_type);
1017 break;
Fei Tongaffdf732020-02-20 20:39:05 +08001018 case SYNTH_LOOPBACK:
1019 /* update out_app_type_cfg */
1020 usecase->out_app_type_cfg.sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
1021 usecase->out_app_type_cfg.bit_width =
1022 platform_get_snd_device_bit_width(usecase->out_snd_device);
1023 usecase->out_app_type_cfg.app_type =
1024 platform_get_default_app_type_v2(adev->platform, PCM_PLAYBACK);
1025 /* update in_app_type_cfg */
1026 usecase->in_app_type_cfg.sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
1027 usecase->in_app_type_cfg.bit_width =
1028 platform_get_snd_device_bit_width(usecase->in_snd_device);
1029 usecase->in_app_type_cfg.app_type =
1030 platform_get_default_app_type_v2(adev->platform, PCM_CAPTURE);
1031 ALOGV("%s Selected apptype: playback %d capture %d",
1032 __func__, usecase->out_app_type_cfg.app_type, usecase->in_app_type_cfg.app_type);
1033 break;
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301034 default:
1035 ALOGE("%s: app type cfg not supported for usecase type (%d)",
1036 __func__, usecase->type);
1037 }
1038}
1039
Aalique Grahame22e49102018-12-18 14:23:57 -08001040static int set_stream_app_type_mixer_ctrl(struct audio_device *adev,
1041 int pcm_device_id, int app_type,
1042 int acdb_dev_id, int sample_rate,
1043 int stream_type,
1044 snd_device_t snd_device)
1045{
1046
1047 char mixer_ctl_name[MAX_LENGTH_MIXER_CONTROL_IN_INT];
1048 struct mixer_ctl *ctl;
1049 int app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT], len = 0, rc = 0;
1050 int snd_device_be_idx = -1;
1051
1052 if (stream_type == PCM_PLAYBACK) {
1053 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
1054 "Audio Stream %d App Type Cfg", pcm_device_id);
1055 } else if (stream_type == PCM_CAPTURE) {
1056 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
1057 "Audio Stream Capture %d App Type Cfg", pcm_device_id);
1058 }
1059
1060 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
1061 if (!ctl) {
1062 ALOGE("%s: Could not get ctl for mixer cmd - %s",
1063 __func__, mixer_ctl_name);
1064 rc = -EINVAL;
1065 goto exit;
1066 }
1067 app_type_cfg[len++] = app_type;
1068 app_type_cfg[len++] = acdb_dev_id;
1069 app_type_cfg[len++] = sample_rate;
1070
1071 snd_device_be_idx = platform_get_snd_device_backend_index(snd_device);
1072 if (snd_device_be_idx > 0)
1073 app_type_cfg[len++] = snd_device_be_idx;
1074 ALOGV("%s: stream type %d app_type %d, acdb_dev_id %d "
1075 "sample rate %d, snd_device_be_idx %d",
1076 __func__, stream_type, app_type, acdb_dev_id, sample_rate,
1077 snd_device_be_idx);
1078 mixer_ctl_set_array(ctl, app_type_cfg, len);
1079
1080exit:
1081 return rc;
1082}
1083
1084static int audio_extn_utils_send_app_type_cfg_hfp(struct audio_device *adev,
1085 struct audio_usecase *usecase)
1086{
1087 int pcm_device_id, acdb_dev_id = 0, snd_device = usecase->out_snd_device;
1088 int32_t sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
1089 int app_type = 0, rc = 0;
Derek Chenf7092792017-05-23 12:23:53 -04001090 bool is_bus_dev_usecase = false;
Aalique Grahame22e49102018-12-18 14:23:57 -08001091
1092 ALOGV("%s", __func__);
1093
1094 if (usecase->type != PCM_HFP_CALL) {
Derek Chenf7092792017-05-23 12:23:53 -04001095 ALOGV("%s: not a HFP path, no need to cfg app type", __func__);
Aalique Grahame22e49102018-12-18 14:23:57 -08001096 rc = 0;
1097 goto exit_send_app_type_cfg;
1098 }
1099 if ((usecase->id != USECASE_AUDIO_HFP_SCO) &&
Derek Chenf7092792017-05-23 12:23:53 -04001100 (usecase->id != USECASE_AUDIO_HFP_SCO_WB) &&
1101 (usecase->id != USECASE_AUDIO_HFP_SCO_DOWNLINK) &&
1102 (usecase->id != USECASE_AUDIO_HFP_SCO_WB_DOWNLINK)) {
1103 ALOGV("%s: a usecase where app type cfg is not required", __func__);
Aalique Grahame22e49102018-12-18 14:23:57 -08001104 rc = 0;
1105 goto exit_send_app_type_cfg;
1106 }
1107
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08001108 if (compare_device_type(&usecase->device_list, AUDIO_DEVICE_OUT_BUS))
Derek Chenf7092792017-05-23 12:23:53 -04001109 is_bus_dev_usecase = true;
1110
Aalique Grahame22e49102018-12-18 14:23:57 -08001111 snd_device = usecase->out_snd_device;
1112 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_PLAYBACK);
1113
1114 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
1115 if (acdb_dev_id < 0) {
1116 ALOGE("%s: Couldn't get the acdb dev id", __func__);
1117 rc = -EINVAL;
1118 goto exit_send_app_type_cfg;
1119 }
1120
sriram kumar53c4c972021-02-18 16:49:33 +05301121 /*
1122 * Value of afe_loopback gets read based on the property defined in
1123 * audio_platform_info.xml. If afe loopback is set then do not execute
1124 * session 1 path as app type mixer control will not be created for
1125 * afe loopback
1126 */
1127
Aalique Grahame22e49102018-12-18 14:23:57 -08001128 if (usecase->type == PCM_HFP_CALL) {
sriram kumar53c4c972021-02-18 16:49:33 +05301129 if (!(platform_get_is_afe_loopback_enabled(adev->platform))) {
1130 /* config HFP session:1 playback path */
1131 if (is_bus_dev_usecase) {
1132 app_type = usecase->out_app_type_cfg.app_type;
1133 sample_rate= usecase->out_app_type_cfg.sample_rate;
1134 } else {
1135 snd_device = SND_DEVICE_NONE; // use legacy behavior
1136 app_type = platform_get_default_app_type_v2(adev->platform, PCM_PLAYBACK);
1137 sample_rate= CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
Derek Chenf7092792017-05-23 12:23:53 -04001138 }
sriram kumar53c4c972021-02-18 16:49:33 +05301139 rc = set_stream_app_type_mixer_ctrl(adev, pcm_device_id, app_type,
1140 acdb_dev_id, sample_rate,
1141 PCM_PLAYBACK,
1142 snd_device);
1143 if (rc < 0)
1144 goto exit_send_app_type_cfg;
1145
1146 /* config HFP session:1 capture path */
1147 if (is_bus_dev_usecase) {
1148 snd_device = usecase->in_snd_device;
1149 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_CAPTURE);
1150 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
1151 if (acdb_dev_id < 0) {
1152 ALOGE("%s: Couldn't get the acdb dev id", __func__);
1153 rc = -EINVAL;
1154 goto exit_send_app_type_cfg;
1155 }
1156 app_type = usecase->in_app_type_cfg.app_type;
1157 sample_rate= usecase->in_app_type_cfg.sample_rate;
1158 } else {
1159 snd_device = SND_DEVICE_NONE; // use legacy behavior
1160 app_type = platform_get_default_app_type_v2(adev->platform, PCM_CAPTURE);
1161 }
1162 rc = set_stream_app_type_mixer_ctrl(adev, pcm_device_id, app_type,
1163 acdb_dev_id, sample_rate,
1164 PCM_CAPTURE,
1165 snd_device);
1166 if (rc < 0)
1167 goto exit_send_app_type_cfg;
1168
1169 if (is_bus_dev_usecase)
1170 goto exit_send_app_type_cfg;
Derek Chenf7092792017-05-23 12:23:53 -04001171 }
Aalique Grahame22e49102018-12-18 14:23:57 -08001172 /* config HFP session:2 capture path */
1173 pcm_device_id = HFP_ASM_RX_TX;
1174 snd_device = usecase->in_snd_device;
1175 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
1176 if (acdb_dev_id <= 0) {
1177 ALOGE("%s: Couldn't get the acdb dev id", __func__);
1178 rc = -EINVAL;
1179 goto exit_send_app_type_cfg;
1180 }
1181 app_type = platform_get_default_app_type_v2(adev->platform, PCM_CAPTURE);
1182 rc = set_stream_app_type_mixer_ctrl(adev, pcm_device_id, app_type,
1183 acdb_dev_id, sample_rate, PCM_CAPTURE,
sriram kumar53c4c972021-02-18 16:49:33 +05301184 usecase->in_snd_device);
Aalique Grahame22e49102018-12-18 14:23:57 -08001185 if (rc < 0)
1186 goto exit_send_app_type_cfg;
1187
1188 /* config HFP session:2 playback path */
1189 app_type = platform_get_default_app_type_v2(adev->platform, PCM_PLAYBACK);
1190 rc = set_stream_app_type_mixer_ctrl(adev, pcm_device_id, app_type,
1191 acdb_dev_id, sample_rate,
sriram kumar53c4c972021-02-18 16:49:33 +05301192 PCM_PLAYBACK, usecase->out_snd_device);
Aalique Grahame22e49102018-12-18 14:23:57 -08001193 if (rc < 0)
1194 goto exit_send_app_type_cfg;
1195 }
1196
1197 rc = 0;
1198exit_send_app_type_cfg:
1199 return rc;
1200}
1201
Derek Chena30a5f42019-12-03 11:17:09 -05001202int audio_extn_utils_send_app_type_cfg_icc(struct audio_device *adev,
1203 struct audio_usecase *usecase)
1204{
1205 int pcm_device_id, acdb_dev_id = 0, snd_device = usecase->out_snd_device;
1206 int32_t sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
1207 int app_type = 0, rc = 0;
1208
1209 ALOGV("%s", __func__);
1210
1211 if (usecase->type != ICC_CALL) {
1212 ALOGV("%s: not an ICC path, no need to cfg app type", __func__);
1213 rc = 0;
1214 goto exit_send_app_type_cfg;
1215 }
1216 if (usecase->id != USECASE_ICC_CALL) {
1217 ALOGV("%s: a usecase where app type cfg is not required", __func__);
1218 rc = 0;
1219 goto exit_send_app_type_cfg;
1220 }
1221
1222 snd_device = usecase->out_snd_device;
1223 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_PLAYBACK);
1224 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
1225 if (acdb_dev_id < 0) {
1226 ALOGE("%s: Couldn't get the acdb dev id", __func__);
1227 rc = -EINVAL;
1228 goto exit_send_app_type_cfg;
1229 }
1230 /* config ICC session: playback path */
1231 app_type = usecase->out_app_type_cfg.app_type;
1232 sample_rate= usecase->out_app_type_cfg.sample_rate;
1233
1234 rc = set_stream_app_type_mixer_ctrl(adev, pcm_device_id, app_type,
1235 acdb_dev_id, sample_rate,
1236 PCM_PLAYBACK,
1237 snd_device);
1238 if (rc < 0)
1239 goto exit_send_app_type_cfg;
1240
1241 /* config ICC session: capture path */
1242 snd_device = usecase->in_snd_device;
1243 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_CAPTURE);
1244 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
1245 if (acdb_dev_id < 0) {
1246 ALOGE("%s: Couldn't get the acdb dev id", __func__);
1247 rc = -EINVAL;
1248 goto exit_send_app_type_cfg;
1249 }
1250 app_type = usecase->in_app_type_cfg.app_type;
1251 sample_rate= usecase->in_app_type_cfg.sample_rate;
1252 rc = set_stream_app_type_mixer_ctrl(adev, pcm_device_id, app_type,
1253 acdb_dev_id, sample_rate,
1254 PCM_CAPTURE,
1255 snd_device);
1256exit_send_app_type_cfg:
1257 return rc;
1258}
1259
Fei Tongaffdf732020-02-20 20:39:05 +08001260static int audio_extn_utils_send_app_type_cfg_synth(struct audio_device *adev,
1261 struct audio_usecase *usecase)
1262{
1263 int pcm_device_id, acdb_dev_id = 0, snd_device = usecase->out_snd_device;
1264 int32_t sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
1265 int app_type = 0, rc = 0;
1266 bool is_bus_dev_usecase = false;
1267
1268 ALOGV("%s", __func__);
1269
1270 if (usecase->type != SYNTH_LOOPBACK) {
1271 ALOGV("%s: not a SYNTH path, no need to cfg app type", __func__);
1272 rc = 0;
1273 goto exit_send_app_type_cfg;
1274 }
1275 if (usecase->id != USECASE_AUDIO_PLAYBACK_SYNTHESIZER) {
1276 ALOGV("%s: a usecase where app type cfg is not required", __func__);
1277 rc = 0;
1278 goto exit_send_app_type_cfg;
1279 }
1280
1281 if (compare_device_type(&usecase->device_list, AUDIO_DEVICE_OUT_BUS)) {
1282 is_bus_dev_usecase = true;
1283 }
1284
1285 snd_device = usecase->out_snd_device;
1286 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_PLAYBACK);
1287
1288 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
1289 if (acdb_dev_id < 0) {
1290 ALOGE("%s: Couldn't get the acdb dev id", __func__);
1291 rc = -EINVAL;
1292 goto exit_send_app_type_cfg;
1293 }
1294
1295 if (usecase->type == SYNTH_LOOPBACK) {
1296 /* config SYNTH session: playback path */
1297 if (is_bus_dev_usecase) {
1298 app_type = usecase->out_app_type_cfg.app_type;
1299 sample_rate= usecase->out_app_type_cfg.sample_rate;
1300 } else {
1301 snd_device = SND_DEVICE_NONE; // use legacy behavior
1302 app_type = platform_get_default_app_type_v2(adev->platform, PCM_PLAYBACK);
1303 sample_rate= CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
1304 }
1305 rc = set_stream_app_type_mixer_ctrl(adev, pcm_device_id, app_type,
1306 acdb_dev_id, sample_rate,
1307 PCM_PLAYBACK,
1308 snd_device);
1309 if (rc < 0)
1310 goto exit_send_app_type_cfg;
1311 }
1312
1313 rc = 0;
1314exit_send_app_type_cfg:
1315 return rc;
1316}
1317
Zhou Song06761dd2019-04-28 18:08:17 +08001318int audio_extn_utils_get_app_sample_rate_for_device(
1319 struct audio_device *adev,
1320 struct audio_usecase *usecase, int snd_device)
1321{
1322 char value[PROPERTY_VALUE_MAX] = {0};
1323 int sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Kogara Naveen Kumar51bb57d2022-02-17 16:40:06 +05301324 int acdb_dev_id;
Zhou Song06761dd2019-04-28 18:08:17 +08001325
1326 if ((usecase->type == PCM_PLAYBACK) && (usecase->stream.out != NULL)) {
1327 property_get("vendor.audio.playback.mch.downsample",value,"");
1328 if (!strncmp("true", value, sizeof("true"))) {
1329 if ((popcount(usecase->stream.out->channel_mask) > 2) &&
1330 (usecase->stream.out->app_type_cfg.sample_rate > CODEC_BACKEND_DEFAULT_SAMPLE_RATE) &&
1331 !(usecase->stream.out->flags &
1332 (audio_output_flags_t)AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH))
1333 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
1334 }
1335
1336 if (usecase->id == USECASE_AUDIO_PLAYBACK_VOIP) {
1337 usecase->stream.out->app_type_cfg.sample_rate = usecase->stream.out->sample_rate;
Zhou Song06761dd2019-04-28 18:08:17 +08001338 } else if ((snd_device == SND_DEVICE_OUT_HDMI ||
1339 snd_device == SND_DEVICE_OUT_USB_HEADSET ||
1340 snd_device == SND_DEVICE_OUT_DISPLAY_PORT) &&
1341 (usecase->stream.out->sample_rate >= OUTPUT_SAMPLING_RATE_44100)) {
1342 /*
1343 * To best utlize DSP, check if the stream sample rate is supported/multiple of
1344 * configured device sample rate, if not update the COPP rate to be equal to the
1345 * device sample rate, else open COPP at stream sample rate
1346 */
1347 platform_check_and_update_copp_sample_rate(adev->platform, snd_device,
1348 usecase->stream.out->sample_rate,
1349 &usecase->stream.out->app_type_cfg.sample_rate);
Lakshman Chaluvaraju5d0894e2021-01-27 23:22:58 +05301350 } else if (snd_device == SND_DEVICE_OUT_BT_A2DP) {
Zhou Song06761dd2019-04-28 18:08:17 +08001351 /*
1352 * For a2dp playback get encoder sampling rate and set copp sampling rate,
1353 * for bit width use the stream param only.
1354 */
1355 audio_extn_a2dp_get_enc_sample_rate(&usecase->stream.out->app_type_cfg.sample_rate);
1356 ALOGI("%s using %d sample rate rate for A2DP CoPP",
1357 __func__, usecase->stream.out->app_type_cfg.sample_rate);
sriram kumar68e21132021-06-25 19:27:20 +05301358 } else if ((((snd_device != SND_DEVICE_OUT_HEADPHONES_44_1 &&
1359 !audio_is_this_native_usecase(usecase)) &&
1360 usecase->stream.out->sample_rate == OUTPUT_SAMPLING_RATE_44100) ||
1361 (usecase->stream.out->sample_rate < OUTPUT_SAMPLING_RATE_44100)) ||
1362 (compare_device_type(&usecase->stream.out->device_list,AUDIO_DEVICE_OUT_SPEAKER))) {
Kogara Naveen Kumar51bb57d2022-02-17 16:40:06 +05301363 if (!((compare_device_type(&usecase->device_list, AUDIO_DEVICE_OUT_BUS)) && ((usecase->stream.out->flags &
1364 (audio_output_flags_t)AUDIO_OUTPUT_FLAG_SYS_NOTIFICATION) || (usecase->stream.out->flags &
1365 (audio_output_flags_t)AUDIO_OUTPUT_FLAG_PHONE)))) {
1366 /* Reset to default if no native stream is active or default device is speaker*/
1367 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
1368 }
Zhou Song06761dd2019-04-28 18:08:17 +08001369 }
1370 audio_extn_btsco_get_sample_rate(snd_device, &usecase->stream.out->app_type_cfg.sample_rate);
1371 sample_rate = usecase->stream.out->app_type_cfg.sample_rate;
1372
1373 if (((usecase->stream.out->format == AUDIO_FORMAT_E_AC3) ||
1374 (usecase->stream.out->format == AUDIO_FORMAT_E_AC3_JOC) ||
1375 (usecase->stream.out->format == AUDIO_FORMAT_DOLBY_TRUEHD))
1376 && audio_extn_passthru_is_passthrough_stream(usecase->stream.out)
1377 && !audio_extn_passthru_is_convert_supported(adev, usecase->stream.out)) {
1378 sample_rate = sample_rate * 4;
1379 if (sample_rate > HDMI_PASSTHROUGH_MAX_SAMPLE_RATE)
1380 sample_rate = HDMI_PASSTHROUGH_MAX_SAMPLE_RATE;
1381 }
1382 } else if (usecase->type == PCM_CAPTURE) {
1383 if (usecase->stream.in != NULL) {
Revathi Uddarajud9f23d92020-07-27 10:55:06 +05301384 if (usecase->id == USECASE_AUDIO_RECORD_VOIP
1385 || usecase->id == USECASE_AUDIO_RECORD_VOIP_LOW_LATENCY)
Zhou Song06761dd2019-04-28 18:08:17 +08001386 usecase->stream.in->app_type_cfg.sample_rate = usecase->stream.in->sample_rate;
1387 if (voice_is_in_call_rec_stream(usecase->stream.in)) {
1388 audio_extn_btsco_get_sample_rate(usecase->in_snd_device,
1389 &usecase->stream.in->app_type_cfg.sample_rate);
Zhou Songab94f062021-05-12 13:11:17 +08001390 } if (SND_DEVICE_IN_BT_A2DP == snd_device) {
1391 audio_extn_a2dp_get_dec_sample_rate(&usecase->stream.in->app_type_cfg.sample_rate);
Zhou Song06761dd2019-04-28 18:08:17 +08001392 } else {
1393 audio_extn_btsco_get_sample_rate(snd_device,
1394 &usecase->stream.in->app_type_cfg.sample_rate);
1395 }
1396 sample_rate = usecase->stream.in->app_type_cfg.sample_rate;
1397 } else if (usecase->id == USECASE_AUDIO_SPKR_CALIB_TX) {
Sujin Panickerb6d76262019-09-09 10:55:38 +05301398 if ((property_get("vendor.audio.spkr_prot.tx.sampling_rate", value, NULL) > 0))
1399 sample_rate = atoi(value);
1400 else
1401 sample_rate = SAMPLE_RATE_8000;
Zhou Song06761dd2019-04-28 18:08:17 +08001402 }
Kogara Naveen Kumar51bb57d2022-02-17 16:40:06 +05301403
1404 /* ECNR module in DSP does not support 24 KHz sample rate. As a workaround,
1405 run ADM at 48 KHz when ECNR is enabled in ACDB topology (e.g. device id = 95)
1406 */
1407 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
1408 if (sample_rate == ECNS_UNSUPPORTED_CAPTURE_SAMPLE_RATE_FOR_ADM && acdb_dev_id == ECNS_USE_CASE_ACDB_DEV_ID) {
1409 sample_rate = ECNS_SUPPORTED_CAPTURE_SAMPLE_RATE_FOR_ADM;
1410 ALOGD("%s: update sample rate from 24K to 48K to support ECNR in PCM_CAPTURE, sample_rate=%d",__func__,sample_rate);
1411 }
Zhou Song06761dd2019-04-28 18:08:17 +08001412 } else if (usecase->type == TRANSCODE_LOOPBACK_RX) {
1413 sample_rate = usecase->stream.inout->out_config.sample_rate;
1414 }
1415 return sample_rate;
1416}
1417
Siena Richard7c2db772016-12-21 11:32:34 -08001418static int send_app_type_cfg_for_device(struct audio_device *adev,
1419 struct audio_usecase *usecase,
1420 int split_snd_device)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001421{
1422 char mixer_ctl_name[MAX_LENGTH_MIXER_CONTROL_IN_INT];
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301423 size_t app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT] = {0};
1424 int len = 0, rc;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001425 struct mixer_ctl *ctl;
Siena Richard7c2db772016-12-21 11:32:34 -08001426 int pcm_device_id = 0, acdb_dev_id, app_type;
1427 int snd_device = split_snd_device, snd_device_be_idx = -1;
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +05301428 int32_t sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Varun Balaraje49253e2017-07-06 19:48:56 +05301429 struct streams_io_cfg *s_info = NULL;
1430 struct listnode *node = NULL;
Nikhil Laturkarac4a7492017-08-31 18:27:19 +05301431 int bd_app_type = 0;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001432
Siena Richard7c2db772016-12-21 11:32:34 -08001433 ALOGV("%s: usecase->out_snd_device %s, usecase->in_snd_device %s, split_snd_device %s",
1434 __func__, platform_get_snd_device_name(usecase->out_snd_device),
1435 platform_get_snd_device_name(usecase->in_snd_device),
1436 platform_get_snd_device_name(split_snd_device));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001437
Siddartha Shaik343abc62017-08-08 11:15:25 +05301438 if (usecase->type != PCM_PLAYBACK && usecase->type != PCM_CAPTURE &&
Surendar Karka93cd25a2018-08-28 14:21:37 +05301439 usecase->type != TRANSCODE_LOOPBACK_RX) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301440 ALOGE("%s: not a playback/capture path, no need to cfg app type", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001441 rc = 0;
1442 goto exit_send_app_type_cfg;
1443 }
1444 if ((usecase->id != USECASE_AUDIO_PLAYBACK_DEEP_BUFFER) &&
Vignesh Kulothungan0a2eff02019-07-11 16:30:13 -07001445 (usecase->id != USECASE_AUDIO_PLAYBACK_WITH_HAPTICS) &&
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001446 (usecase->id != USECASE_AUDIO_PLAYBACK_LOW_LATENCY) &&
1447 (usecase->id != USECASE_AUDIO_PLAYBACK_MULTI_CH) &&
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -08001448 (usecase->id != USECASE_AUDIO_PLAYBACK_ULL) &&
Vikram Panduranga93f080e2017-06-07 18:16:14 -07001449 (usecase->id != USECASE_AUDIO_PLAYBACK_VOIP) &&
Surendar Karka93cd25a2018-08-28 14:21:37 +05301450 (usecase->id != USECASE_AUDIO_TRANSCODE_LOOPBACK_RX) &&
Varun Balaraje49253e2017-07-06 19:48:56 +05301451 (!is_interactive_usecase(usecase->id)) &&
Srikanth Uyyala9d551402015-08-25 16:03:42 +05301452 (!is_offload_usecase(usecase->id)) &&
Derek Chenf6318be2017-06-12 17:16:24 -04001453 (usecase->type != PCM_CAPTURE) &&
1454 (!audio_extn_auto_hal_is_bus_device_usecase(usecase->id))) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301455 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 -07001456 rc = 0;
1457 goto exit_send_app_type_cfg;
1458 }
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -08001459
1460 //if VR is active then only send the mixer control
1461 if (usecase->id == USECASE_AUDIO_PLAYBACK_ULL && !audio_is_vr_mode_on(adev)) {
1462 ALOGI("ULL doesnt need sending app type cfg, returning");
1463 rc = 0;
1464 goto exit_send_app_type_cfg;
1465 }
1466
Surendar Karka93cd25a2018-08-28 14:21:37 +05301467 if (usecase->type == PCM_PLAYBACK || usecase->type == TRANSCODE_LOOPBACK_RX) {
Ben Romberger1fafdde2015-09-09 19:43:15 -07001468 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_PLAYBACK);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301469 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
1470 "Audio Stream %d App Type Cfg", pcm_device_id);
Ben Romberger1fafdde2015-09-09 19:43:15 -07001471 } else if (usecase->type == PCM_CAPTURE) {
Ben Romberger1fafdde2015-09-09 19:43:15 -07001472 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_CAPTURE);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301473 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
1474 "Audio Stream Capture %d App Type Cfg", pcm_device_id);
Srikanth Uyyala9d551402015-08-25 16:03:42 +05301475 }
Siena Richard7c2db772016-12-21 11:32:34 -08001476
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001477 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
1478 if (!ctl) {
1479 ALOGE("%s: Could not get ctl for mixer cmd - %s", __func__,
1480 mixer_ctl_name);
1481 rc = -EINVAL;
1482 goto exit_send_app_type_cfg;
1483 }
Aditya Bavanari701a6992017-03-30 19:17:16 +05301484 snd_device = platform_get_spkr_prot_snd_device(snd_device);
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301485 if (voice_is_in_call_rec_stream(usecase->stream.in) && usecase->type == PCM_CAPTURE) {
1486 snd_device_t voice_device = voice_get_incall_rec_snd_device(usecase->in_snd_device);
1487 acdb_dev_id = platform_get_snd_device_acdb_id(voice_device);
1488 ALOGV("acdb id for voice call use case %d", acdb_dev_id);
1489 } else {
1490 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
1491 }
Rohit Kumar1181b292017-01-31 18:07:17 +05301492 if (acdb_dev_id <= 0) {
1493 ALOGE("%s: Couldn't get the acdb dev id", __func__);
1494 rc = -EINVAL;
1495 goto exit_send_app_type_cfg;
1496 }
1497
Siena Richard7c2db772016-12-21 11:32:34 -08001498 snd_device_be_idx = platform_get_snd_device_backend_index(snd_device);
1499 if (snd_device_be_idx < 0) {
1500 ALOGE("%s: Couldn't get the backend index for snd device %s ret=%d",
1501 __func__, platform_get_snd_device_name(snd_device),
1502 snd_device_be_idx);
1503 }
1504
Zhou Song06761dd2019-04-28 18:08:17 +08001505 sample_rate = audio_extn_utils_get_app_sample_rate_for_device(adev, usecase, snd_device);
1506
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301507 if ((usecase->type == PCM_PLAYBACK) && (usecase->stream.out != NULL)) {
Varun Balaraje49253e2017-07-06 19:48:56 +05301508 /* Interactive streams are supported with only direct app type id.
1509 * Get Direct profile app type and use it for interactive streams
1510 */
1511 list_for_each(node, &adev->streams_output_cfg_list) {
1512 s_info = node_to_item(node, struct streams_io_cfg, list);
Aalique Grahame5cd12ff2017-10-19 10:57:00 -07001513 if (s_info->flags.out_flags == (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_BD |
Nikhil Laturkarac4a7492017-08-31 18:27:19 +05301514 AUDIO_OUTPUT_FLAG_DIRECT_PCM |
1515 AUDIO_OUTPUT_FLAG_DIRECT))
1516 bd_app_type = s_info->app_type_cfg.app_type;
Varun Balaraje49253e2017-07-06 19:48:56 +05301517 }
Aalique Grahame5cd12ff2017-10-19 10:57:00 -07001518 if (usecase->stream.out->flags == (audio_output_flags_t)AUDIO_OUTPUT_FLAG_INTERACTIVE)
Nikhil Laturkarac4a7492017-08-31 18:27:19 +05301519 app_type = bd_app_type;
Varun Balaraje49253e2017-07-06 19:48:56 +05301520 else
1521 app_type = usecase->stream.out->app_type_cfg.app_type;
Siena Richard7c2db772016-12-21 11:32:34 -08001522 app_type_cfg[len++] = app_type;
Mingming Yin21854652016-04-13 11:54:02 -07001523 app_type_cfg[len++] = acdb_dev_id;
Naresh Tanniru3a406772017-05-10 13:09:05 -07001524 app_type_cfg[len++] = sample_rate;
1525
Siena Richard7c2db772016-12-21 11:32:34 -08001526 if (snd_device_be_idx > 0)
1527 app_type_cfg[len++] = snd_device_be_idx;
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301528
Siena Richard7c2db772016-12-21 11:32:34 -08001529 ALOGI("%s PLAYBACK app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
1530 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301531
1532 } else if ((usecase->type == PCM_CAPTURE) && (usecase->stream.in != NULL)) {
Siena Richard7c2db772016-12-21 11:32:34 -08001533 app_type = usecase->stream.in->app_type_cfg.app_type;
1534 app_type_cfg[len++] = app_type;
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301535 app_type_cfg[len++] = acdb_dev_id;
Siena Richard7c2db772016-12-21 11:32:34 -08001536 app_type_cfg[len++] = sample_rate;
1537 if (snd_device_be_idx > 0)
1538 app_type_cfg[len++] = snd_device_be_idx;
1539 ALOGI("%s CAPTURE app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
1540 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301541 } else {
Siena Richard7c2db772016-12-21 11:32:34 -08001542 app_type = platform_get_default_app_type_v2(adev->platform, usecase->type);
Surendar Karka93cd25a2018-08-28 14:21:37 +05301543 if(usecase->type == TRANSCODE_LOOPBACK_RX) {
Siddartha Shaik343abc62017-08-08 11:15:25 +05301544 app_type = usecase->stream.inout->out_app_type_cfg.app_type;
1545 }
Siena Richard7c2db772016-12-21 11:32:34 -08001546 app_type_cfg[len++] = app_type;
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301547 app_type_cfg[len++] = acdb_dev_id;
1548 app_type_cfg[len++] = sample_rate;
Siena Richard7c2db772016-12-21 11:32:34 -08001549 if (snd_device_be_idx > 0)
1550 app_type_cfg[len++] = snd_device_be_idx;
1551 ALOGI("%s default app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
1552 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Ashish Jainc02430d2016-01-04 10:42:43 +05301553 }
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301554
Siddartha Shaik343abc62017-08-08 11:15:25 +05301555 if(ctl)
1556 mixer_ctl_set_array(ctl, app_type_cfg, len);
Zhou Songd9bd9302020-08-04 16:34:45 +08001557
1558 /* send app type cfg for haptics */
1559 if (usecase->id == USECASE_AUDIO_PLAYBACK_WITH_HAPTICS) {
1560 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
1561 "Audio Stream %d App Type Cfg", adev->haptic_pcm_device_id );
1562 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
1563 if (!ctl) {
1564 ALOGE("%s: Could not get ctl for mixer cmd - %s", __func__,
1565 mixer_ctl_name);
1566 rc = -EINVAL;
1567 goto exit_send_app_type_cfg;
1568 }
1569 acdb_dev_id = platform_get_snd_device_acdb_id(SND_DEVICE_OUT_HAPTICS);
1570 snd_device_be_idx = platform_get_snd_device_backend_index(SND_DEVICE_OUT_HAPTICS);
1571 /* haptics acdb id */
1572 app_type_cfg[1] = acdb_dev_id;
1573 /* haptics be index */
1574 app_type_cfg[3] = snd_device_be_idx;
1575 mixer_ctl_set_array(ctl, app_type_cfg, len);
1576 }
1577
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001578 rc = 0;
1579exit_send_app_type_cfg:
1580 return rc;
1581}
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001582
Deeraj Soman14230922019-01-30 16:39:30 +05301583static int audio_extn_utils_check_input_parameters(uint32_t sample_rate,
1584 audio_format_t format,
1585 int channel_count)
1586{
1587 int ret = 0;
1588
1589 if (((format != AUDIO_FORMAT_PCM_16_BIT) && (format != AUDIO_FORMAT_PCM_8_24_BIT) &&
1590 (format != AUDIO_FORMAT_PCM_24_BIT_PACKED) && (format != AUDIO_FORMAT_PCM_32_BIT) &&
1591 (format != AUDIO_FORMAT_PCM_FLOAT)) &&
1592 !voice_extn_compress_voip_is_format_supported(format) &&
1593 !audio_extn_compr_cap_format_supported(format) &&
1594 !audio_extn_cin_format_supported(format))
1595 ret = -EINVAL;
1596
1597 switch (channel_count) {
1598 case 1:
1599 case 2:
1600 case 3:
1601 case 4:
1602 case 6:
1603 case 8:
1604 break;
1605 default:
1606 ret = -EINVAL;
1607 }
1608
1609 switch (sample_rate) {
1610 case 8000:
1611 case 11025:
1612 case 12000:
1613 case 16000:
1614 case 22050:
1615 case 24000:
1616 case 32000:
1617 case 44100:
1618 case 48000:
1619 case 88200:
1620 case 96000:
1621 case 176400:
1622 case 192000:
1623 break;
1624 default:
1625 ret = -EINVAL;
1626 }
1627
1628 return ret;
1629}
1630
1631static inline uint32_t audio_extn_utils_nearest_multiple(uint32_t num, uint32_t multiplier)
1632{
1633 uint32_t remainder = 0;
1634
1635 if (!multiplier)
1636 return num;
1637
1638 remainder = num % multiplier;
1639 if (remainder)
1640 num += (multiplier - remainder);
1641
1642 return num;
1643}
1644
1645static inline uint32_t audio_extn_utils_lcm(uint32_t num1, uint32_t num2)
1646{
1647 uint32_t high = num1, low = num2, temp = 0;
1648
1649 if (!num1 || !num2)
1650 return 0;
1651
1652 if (num1 < num2) {
1653 high = num2;
1654 low = num1;
1655 }
1656
1657 while (low != 0) {
1658 temp = low;
1659 low = high % low;
1660 high = temp;
1661 }
1662 return (num1 * num2)/high;
1663}
1664
Siena Richard7c2db772016-12-21 11:32:34 -08001665int audio_extn_utils_send_app_type_cfg(struct audio_device *adev,
1666 struct audio_usecase *usecase)
1667{
1668 int i, num_devices = 0;
1669 snd_device_t new_snd_devices[SND_DEVICE_OUT_END] = {0};
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301670 snd_device_t in_snd_device = usecase->in_snd_device;
Siena Richard7c2db772016-12-21 11:32:34 -08001671 int rc = 0;
1672
Aalique Grahame22e49102018-12-18 14:23:57 -08001673 if (usecase->type == PCM_HFP_CALL) {
1674 return audio_extn_utils_send_app_type_cfg_hfp(adev, usecase);
Derek Chena30a5f42019-12-03 11:17:09 -05001675 } else if (usecase->type == ICC_CALL) {
1676 return audio_extn_utils_send_app_type_cfg_icc(adev, usecase);
Fei Tongaffdf732020-02-20 20:39:05 +08001677 } else if (usecase->type == SYNTH_LOOPBACK) {
1678 return audio_extn_utils_send_app_type_cfg_synth(adev, usecase);
Aalique Grahame22e49102018-12-18 14:23:57 -08001679 }
1680
Siena Richard7c2db772016-12-21 11:32:34 -08001681 switch (usecase->type) {
1682 case PCM_PLAYBACK:
Surendar Karka93cd25a2018-08-28 14:21:37 +05301683 case TRANSCODE_LOOPBACK_RX:
Siena Richard7c2db772016-12-21 11:32:34 -08001684 ALOGD("%s: usecase->out_snd_device %s",
1685 __func__, platform_get_snd_device_name(usecase->out_snd_device));
1686 /* check for out combo device */
1687 if (platform_split_snd_device(adev->platform,
1688 usecase->out_snd_device,
1689 &num_devices, new_snd_devices)) {
1690 new_snd_devices[0] = usecase->out_snd_device;
1691 num_devices = 1;
1692 }
1693 break;
1694 case PCM_CAPTURE:
1695 ALOGD("%s: usecase->in_snd_device %s",
1696 __func__, platform_get_snd_device_name(usecase->in_snd_device));
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301697 if (voice_is_in_call_rec_stream(usecase->stream.in)) {
1698 in_snd_device = voice_get_incall_rec_backend_device(usecase->stream.in);
1699 }
Siena Richard7c2db772016-12-21 11:32:34 -08001700 /* check for in combo device */
1701 if (platform_split_snd_device(adev->platform,
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301702 in_snd_device,
Siena Richard7c2db772016-12-21 11:32:34 -08001703 &num_devices, new_snd_devices)) {
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301704 new_snd_devices[0] = in_snd_device;
Siena Richard7c2db772016-12-21 11:32:34 -08001705 num_devices = 1;
1706 }
1707 break;
Sriram Kumar9c836482020-10-27 20:34:35 +05301708 case PCM_HFP_CALL:
1709 rc = audio_extn_utils_send_app_type_cfg_hfp(adev,usecase);
1710 return rc;
Siena Richard7c2db772016-12-21 11:32:34 -08001711 default:
1712 ALOGI("%s: not a playback/capture path, no need to cfg app type", __func__);
1713 rc = 0;
1714 break;
1715 }
1716
1717 for (i = 0; i < num_devices; i++) {
1718 rc = send_app_type_cfg_for_device(adev, usecase, new_snd_devices[i]);
1719 if (rc)
1720 break;
1721 }
1722
1723 return rc;
1724}
1725
Preetam Singh Ranawat9519e9c2015-11-18 16:05:55 +05301726int read_line_from_file(const char *path, char *buf, size_t count)
1727{
1728 char * fgets_ret;
1729 FILE * fd;
1730 int rv;
1731
1732 fd = fopen(path, "r");
1733 if (fd == NULL)
1734 return -1;
1735
1736 fgets_ret = fgets(buf, (int)count, fd);
1737 if (NULL != fgets_ret) {
1738 rv = (int)strlen(buf);
1739 } else {
1740 rv = ferror(fd);
1741 }
1742 fclose(fd);
1743
1744 return rv;
1745}
1746
Ashish Jainf1eaa582016-05-23 20:54:24 +05301747/*Translates ALSA formats to AOSP PCM formats*/
1748audio_format_t alsa_format_to_hal(uint32_t alsa_format)
1749{
1750 audio_format_t format;
1751
1752 switch(alsa_format) {
1753 case SNDRV_PCM_FORMAT_S16_LE:
1754 format = AUDIO_FORMAT_PCM_16_BIT;
1755 break;
1756 case SNDRV_PCM_FORMAT_S24_3LE:
1757 format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
1758 break;
1759 case SNDRV_PCM_FORMAT_S24_LE:
1760 format = AUDIO_FORMAT_PCM_8_24_BIT;
1761 break;
1762 case SNDRV_PCM_FORMAT_S32_LE:
1763 format = AUDIO_FORMAT_PCM_32_BIT;
1764 break;
1765 default:
1766 ALOGW("Incorrect ALSA format");
1767 format = AUDIO_FORMAT_INVALID;
1768 }
1769 return format;
1770}
1771
1772/*Translates hal format (AOSP) to alsa formats*/
1773uint32_t hal_format_to_alsa(audio_format_t hal_format)
1774{
1775 uint32_t alsa_format;
1776
1777 switch (hal_format) {
1778 case AUDIO_FORMAT_PCM_32_BIT: {
1779 if (platform_supports_true_32bit())
1780 alsa_format = SNDRV_PCM_FORMAT_S32_LE;
1781 else
1782 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1783 }
1784 break;
1785 case AUDIO_FORMAT_PCM_8_BIT:
1786 alsa_format = SNDRV_PCM_FORMAT_S8;
1787 break;
1788 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
1789 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1790 break;
1791 case AUDIO_FORMAT_PCM_8_24_BIT: {
1792 if (platform_supports_true_32bit())
1793 alsa_format = SNDRV_PCM_FORMAT_S32_LE;
1794 else
1795 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1796 }
1797 break;
1798 case AUDIO_FORMAT_PCM_FLOAT:
1799 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1800 break;
1801 default:
1802 case AUDIO_FORMAT_PCM_16_BIT:
1803 alsa_format = SNDRV_PCM_FORMAT_S16_LE;
1804 break;
1805 }
1806 return alsa_format;
1807}
1808
Ashish Jain83a6cc22016-06-28 14:34:17 +05301809/*Translates PCM formats to AOSP formats*/
1810audio_format_t pcm_format_to_hal(uint32_t pcm_format)
1811{
1812 audio_format_t format = AUDIO_FORMAT_INVALID;
1813
1814 switch(pcm_format) {
1815 case PCM_FORMAT_S16_LE:
1816 format = AUDIO_FORMAT_PCM_16_BIT;
1817 break;
1818 case PCM_FORMAT_S24_3LE:
1819 format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
1820 break;
1821 case PCM_FORMAT_S24_LE:
1822 format = AUDIO_FORMAT_PCM_8_24_BIT;
1823 break;
1824 case PCM_FORMAT_S32_LE:
1825 format = AUDIO_FORMAT_PCM_32_BIT;
1826 break;
1827 default:
1828 ALOGW("Incorrect PCM format");
1829 format = AUDIO_FORMAT_INVALID;
1830 }
1831 return format;
1832}
1833
1834/*Translates hal format (AOSP) to alsa formats*/
1835uint32_t hal_format_to_pcm(audio_format_t hal_format)
1836{
1837 uint32_t pcm_format;
1838
1839 switch (hal_format) {
1840 case AUDIO_FORMAT_PCM_32_BIT:
1841 case AUDIO_FORMAT_PCM_8_24_BIT:
1842 case AUDIO_FORMAT_PCM_FLOAT: {
1843 if (platform_supports_true_32bit())
1844 pcm_format = PCM_FORMAT_S32_LE;
1845 else
1846 pcm_format = PCM_FORMAT_S24_3LE;
1847 }
1848 break;
1849 case AUDIO_FORMAT_PCM_8_BIT:
1850 pcm_format = PCM_FORMAT_S8;
1851 break;
1852 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
1853 pcm_format = PCM_FORMAT_S24_3LE;
1854 break;
1855 default:
1856 case AUDIO_FORMAT_PCM_16_BIT:
1857 pcm_format = PCM_FORMAT_S16_LE;
1858 break;
1859 }
1860 return pcm_format;
1861}
1862
Ashish Jainf1eaa582016-05-23 20:54:24 +05301863uint32_t get_alsa_fragment_size(uint32_t bytes_per_sample,
1864 uint32_t sample_rate,
Deeraj Soman65358ab2019-02-07 15:40:49 +05301865 uint32_t noOfChannels,
1866 int64_t duration_ms)
Ashish Jainf1eaa582016-05-23 20:54:24 +05301867{
1868 uint32_t fragment_size = 0;
1869 uint32_t pcm_offload_time = PCM_OFFLOAD_BUFFER_DURATION;
1870
Deeraj Soman65358ab2019-02-07 15:40:49 +05301871 if (duration_ms >= MIN_OFFLOAD_BUFFER_DURATION_MS && duration_ms <= MAX_OFFLOAD_BUFFER_DURATION_MS)
1872 pcm_offload_time = duration_ms;
1873
Ashish Jainf1eaa582016-05-23 20:54:24 +05301874 fragment_size = (pcm_offload_time
1875 * sample_rate
1876 * bytes_per_sample
1877 * noOfChannels)/1000;
1878 if (fragment_size < MIN_PCM_OFFLOAD_FRAGMENT_SIZE)
1879 fragment_size = MIN_PCM_OFFLOAD_FRAGMENT_SIZE;
1880 else if (fragment_size > MAX_PCM_OFFLOAD_FRAGMENT_SIZE)
1881 fragment_size = MAX_PCM_OFFLOAD_FRAGMENT_SIZE;
1882 /*To have same PCM samples for all channels, the buffer size requires to
1883 *be multiple of (number of channels * bytes per sample)
1884 *For writes to succeed, the buffer must be written at address which is multiple of 32
1885 */
Aalique Grahameb85f2d92017-10-16 17:53:23 -07001886 fragment_size = ALIGN(fragment_size, (bytes_per_sample * noOfChannels * 32));
Ashish Jainf1eaa582016-05-23 20:54:24 +05301887
1888 ALOGI("PCM offload Fragment size to %d bytes", fragment_size);
1889 return fragment_size;
1890}
1891
1892/* Calculates the fragment size required to configure compress session.
1893 * Based on the alsa format selected, decide if conversion is needed in
1894
1895 * HAL ( e.g. convert AUDIO_FORMAT_PCM_FLOAT input format to
1896 * AUDIO_FORMAT_PCM_24_BIT_PACKED before writing to the compress driver.
1897 */
1898void audio_extn_utils_update_direct_pcm_fragment_size(struct stream_out *out)
1899{
Ashish Jain83a6cc22016-06-28 14:34:17 +05301900 audio_format_t dst_format = out->hal_op_format;
1901 audio_format_t src_format = out->hal_ip_format;
Ashish Jainf1eaa582016-05-23 20:54:24 +05301902 uint32_t hal_op_bytes_per_sample = audio_bytes_per_sample(dst_format);
1903 uint32_t hal_ip_bytes_per_sample = audio_bytes_per_sample(src_format);
1904
1905 out->compr_config.fragment_size =
1906 get_alsa_fragment_size(hal_op_bytes_per_sample,
1907 out->sample_rate,
Deeraj Soman65358ab2019-02-07 15:40:49 +05301908 popcount(out->channel_mask),
1909 out->info.duration_us / 1000);
Ashish Jainf1eaa582016-05-23 20:54:24 +05301910
1911 if ((src_format != dst_format) &&
1912 hal_op_bytes_per_sample != hal_ip_bytes_per_sample) {
1913
Ashish Jain83a6cc22016-06-28 14:34:17 +05301914 out->hal_fragment_size =
Ashish Jainf1eaa582016-05-23 20:54:24 +05301915 ((out->compr_config.fragment_size * hal_ip_bytes_per_sample) /
1916 hal_op_bytes_per_sample);
1917 ALOGI("enable conversion hal_input_fragment_size is %d src_format %x dst_format %x",
Ashish Jain83a6cc22016-06-28 14:34:17 +05301918 out->hal_fragment_size, src_format, dst_format);
Ashish Jainf1eaa582016-05-23 20:54:24 +05301919 } else {
Ashish Jain83a6cc22016-06-28 14:34:17 +05301920 out->hal_fragment_size = out->compr_config.fragment_size;
Ashish Jainf1eaa582016-05-23 20:54:24 +05301921 }
1922}
1923
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301924/* converts pcm format 24_8 to 8_24 inplace */
1925size_t audio_extn_utils_convert_format_24_8_to_8_24(void *buf, size_t bytes)
1926{
1927 size_t i = 0;
1928 int *int_buf_stream = buf;
1929
1930 if ((bytes % 4) != 0) {
1931 ALOGE("%s: wrong inout buffer! ... is not 32 bit aligned ", __func__);
1932 return -EINVAL;
1933 }
1934
1935 for (; i < (bytes / 4); i++)
1936 int_buf_stream[i] >>= 8;
1937
1938 return bytes;
1939}
1940
Xiaojun Sang782e5b12020-06-29 21:13:06 +08001941#ifdef AUDIO_GKI_ENABLED
1942int get_snd_codec_id(audio_format_t format)
1943{
1944 int id = 0;
1945
1946 switch (format & AUDIO_FORMAT_MAIN_MASK) {
1947 case AUDIO_FORMAT_MP3:
1948 id = SND_AUDIOCODEC_MP3;
1949 break;
1950 case AUDIO_FORMAT_AAC:
1951 id = SND_AUDIOCODEC_AAC;
1952 break;
1953 case AUDIO_FORMAT_AAC_ADTS:
1954 id = SND_AUDIOCODEC_AAC;
1955 break;
1956 case AUDIO_FORMAT_AAC_LATM:
1957 id = SND_AUDIOCODEC_AAC;
1958 break;
1959 case AUDIO_FORMAT_PCM:
1960 id = SND_AUDIOCODEC_PCM;
1961 break;
1962 case AUDIO_FORMAT_FLAC:
1963 case AUDIO_FORMAT_ALAC:
1964 case AUDIO_FORMAT_APE:
1965 case AUDIO_FORMAT_VORBIS:
1966 case AUDIO_FORMAT_WMA:
1967 case AUDIO_FORMAT_WMA_PRO:
1968 case AUDIO_FORMAT_DSD:
1969 case AUDIO_FORMAT_APTX:
1970 id = SND_AUDIOCODEC_BESPOKE;
1971 break;
1972 case AUDIO_FORMAT_MP2:
1973 id = SND_AUDIOCODEC_MP2;
1974 break;
1975 case AUDIO_FORMAT_AC3:
1976 id = SND_AUDIOCODEC_AC3;
1977 break;
1978 case AUDIO_FORMAT_E_AC3:
1979 case AUDIO_FORMAT_E_AC3_JOC:
1980 id = SND_AUDIOCODEC_EAC3;
1981 break;
1982 case AUDIO_FORMAT_DTS:
1983 case AUDIO_FORMAT_DTS_HD:
1984 id = SND_AUDIOCODEC_DTS;
1985 break;
1986 case AUDIO_FORMAT_DOLBY_TRUEHD:
1987 id = SND_AUDIOCODEC_TRUEHD;
1988 break;
1989 case AUDIO_FORMAT_IEC61937:
1990 id = SND_AUDIOCODEC_IEC61937;
1991 break;
1992 default:
1993 ALOGE("%s: Unsupported audio format :%x", __func__, format);
1994 }
1995
1996 return id;
1997}
1998#else
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301999int get_snd_codec_id(audio_format_t format)
2000{
2001 int id = 0;
2002
2003 switch (format & AUDIO_FORMAT_MAIN_MASK) {
2004 case AUDIO_FORMAT_MP3:
2005 id = SND_AUDIOCODEC_MP3;
2006 break;
2007 case AUDIO_FORMAT_AAC:
2008 id = SND_AUDIOCODEC_AAC;
2009 break;
2010 case AUDIO_FORMAT_AAC_ADTS:
2011 id = SND_AUDIOCODEC_AAC;
2012 break;
Arun Kumar Dasari3b174182016-12-27 13:01:14 +05302013 case AUDIO_FORMAT_AAC_LATM:
2014 id = SND_AUDIOCODEC_AAC;
2015 break;
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05302016 case AUDIO_FORMAT_PCM:
2017 id = SND_AUDIOCODEC_PCM;
2018 break;
2019 case AUDIO_FORMAT_FLAC:
2020 id = SND_AUDIOCODEC_FLAC;
2021 break;
2022 case AUDIO_FORMAT_ALAC:
2023 id = SND_AUDIOCODEC_ALAC;
2024 break;
2025 case AUDIO_FORMAT_APE:
2026 id = SND_AUDIOCODEC_APE;
2027 break;
2028 case AUDIO_FORMAT_VORBIS:
2029 id = SND_AUDIOCODEC_VORBIS;
2030 break;
2031 case AUDIO_FORMAT_WMA:
2032 id = SND_AUDIOCODEC_WMA;
2033 break;
Dechen Chai4a575252021-07-30 09:21:28 +05302034#ifndef AUDIO_DISABLE_COMPRESS_FORMAT
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05302035 case AUDIO_FORMAT_WMA_PRO:
2036 id = SND_AUDIOCODEC_WMA_PRO;
2037 break;
Dechen Chai4a575252021-07-30 09:21:28 +05302038#endif
Satish Babu Patakokila0c313922016-12-08 12:07:08 +05302039 case AUDIO_FORMAT_MP2:
2040 id = SND_AUDIOCODEC_MP2;
2041 break;
Satish Babu Patakokila915ecba2017-01-10 17:43:56 +05302042 case AUDIO_FORMAT_AC3:
2043 id = SND_AUDIOCODEC_AC3;
2044 break;
2045 case AUDIO_FORMAT_E_AC3:
2046 case AUDIO_FORMAT_E_AC3_JOC:
2047 id = SND_AUDIOCODEC_EAC3;
2048 break;
2049 case AUDIO_FORMAT_DTS:
2050 case AUDIO_FORMAT_DTS_HD:
2051 id = SND_AUDIOCODEC_DTS;
2052 break;
Ben Romberger1aaaf862017-04-06 17:49:46 -07002053 case AUDIO_FORMAT_DOLBY_TRUEHD:
2054 id = SND_AUDIOCODEC_TRUEHD;
2055 break;
Naresh Tanniru928f0862017-04-07 16:44:23 -07002056 case AUDIO_FORMAT_IEC61937:
2057 id = SND_AUDIOCODEC_IEC61937;
2058 break;
Dechen Chai4a575252021-07-30 09:21:28 +05302059#ifndef AUDIO_DISABLE_COMPRESS_FORMAT
Preetam Singh Ranawat4277a5a2017-01-18 19:02:24 +05302060 case AUDIO_FORMAT_DSD:
2061 id = SND_AUDIOCODEC_DSD;
2062 break;
Dhanalakshmi Siddani18737932016-11-29 17:33:17 +05302063 case AUDIO_FORMAT_APTX:
2064 id = SND_AUDIOCODEC_APTX;
2065 break;
Dechen Chai4a575252021-07-30 09:21:28 +05302066#endif
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05302067 default:
2068 ALOGE("%s: Unsupported audio format :%x", __func__, format);
2069 }
2070
2071 return id;
2072}
Xiaojun Sang782e5b12020-06-29 21:13:06 +08002073#endif
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05302074
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07002075void audio_extn_utils_send_audio_calibration(struct audio_device *adev,
2076 struct audio_usecase *usecase)
2077{
2078 int type = usecase->type;
2079
Dhananjay Kumar14245982017-01-16 20:21:00 +05302080 if (type == PCM_PLAYBACK && usecase->stream.out != NULL) {
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05302081 platform_send_audio_calibration(adev->platform, usecase,
Zhou Song06761dd2019-04-28 18:08:17 +08002082 usecase->stream.out->app_type_cfg.app_type);
Dhananjay Kumar14245982017-01-16 20:21:00 +05302083 } else if (type == PCM_CAPTURE && usecase->stream.in != NULL) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +05302084 platform_send_audio_calibration(adev->platform, usecase,
Zhou Song06761dd2019-04-28 18:08:17 +08002085 usecase->stream.in->app_type_cfg.app_type);
Sriram Kumar9c836482020-10-27 20:34:35 +05302086 } else if (type == PCM_HFP_CALL) {
2087 /* when app type is default. the sample rate is not used to send cal */
2088#ifdef ENABLE_HFP_CALIBRATION
2089 platform_send_audio_calibration_hfp(adev->platform, usecase->in_snd_device);
2090#else
2091 platform_send_audio_calibration(adev->platform, usecase,
2092 platform_get_default_app_type_v2(adev->platform, usecase->type));
2093#endif
2094 } else if ((type == PCM_CAPTURE) ||
Derek Chena30a5f42019-12-03 11:17:09 -05002095 (type == TRANSCODE_LOOPBACK_RX && usecase->stream.inout != NULL) ||
Fei Tongaffdf732020-02-20 20:39:05 +08002096 (type == ICC_CALL) || (type == SYNTH_LOOPBACK)) {
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05302097 platform_send_audio_calibration(adev->platform, usecase,
Zhou Song06761dd2019-04-28 18:08:17 +08002098 platform_get_default_app_type_v2(adev->platform, usecase->type));
Vidyakumar Athotae57f6002017-03-01 13:13:16 -08002099 } else {
2100 /* No need to send audio calibration for voice and voip call usecases */
2101 if ((type != VOICE_CALL) && (type != VOIP_CALL))
2102 ALOGW("%s: No audio calibration for usecase type = %d", __func__, type);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07002103 }
2104}
2105
Ben Rombergera04fabc2014-11-14 12:16:03 -08002106// Base64 Encode and Decode
2107// Not all features supported. This must be used only with following conditions.
2108// Decode Modes: Support with and without padding
2109// CRLF not handling. So no CRLF in string to decode.
2110// Encode Modes: Supports only padding
2111int b64decode(char *inp, int ilen, uint8_t* outp)
2112{
2113 int i, j, k, ii, num;
2114 int rem, pcnt;
2115 uint32_t res=0;
2116 uint8_t getIndex[MAX_BASEINDEX_LEN];
2117 uint8_t tmp, cflag;
2118
2119 if(inp == NULL || outp == NULL || ilen <= 0) {
2120 ALOGE("[%s] received NULL pointer or zero length",__func__);
2121 return -1;
2122 }
2123
2124 memset(getIndex, MAX_BASEINDEX_LEN-1, sizeof(getIndex));
2125 for(i=0;i<BASE_TABLE_SIZE;i++) {
2126 getIndex[(uint8_t)bTable[i]] = (uint8_t)i;
2127 }
2128 getIndex[(uint8_t)'=']=0;
2129
2130 j=0;k=0;
2131 num = ilen/4;
2132 rem = ilen%4;
2133 if(rem==0)
2134 num = num-1;
2135 cflag=0;
2136 for(i=0; i<num; i++) {
2137 res=0;
2138 for(ii=0;ii<4;ii++) {
2139 res = res << 6;
2140 tmp = getIndex[(uint8_t)inp[j++]];
2141 res = res | tmp;
2142 cflag = cflag | tmp;
2143 }
2144 outp[k++] = (res >> 16)&0xFF;
2145 outp[k++] = (res >> 8)&0xFF;
2146 outp[k++] = res & 0xFF;
2147 }
2148
2149 // Handle last bytes special
2150 pcnt=0;
2151 if(rem == 0) {
2152 //With padding or full data
2153 res = 0;
2154 for(ii=0;ii<4;ii++) {
2155 if(inp[j] == '=')
2156 pcnt++;
2157 res = res << 6;
2158 tmp = getIndex[(uint8_t)inp[j++]];
2159 res = res | tmp;
2160 cflag = cflag | tmp;
2161 }
2162 outp[k++] = res >> 16;
2163 if(pcnt == 2)
2164 goto done;
2165 outp[k++] = (res>>8)&0xFF;
2166 if(pcnt == 1)
2167 goto done;
2168 outp[k++] = res&0xFF;
2169 } else {
2170 //without padding
2171 res = 0;
2172 for(i=0;i<rem;i++) {
2173 res = res << 6;
2174 tmp = getIndex[(uint8_t)inp[j++]];
2175 res = res | tmp;
2176 cflag = cflag | tmp;
2177 }
2178 for(i=rem;i<4;i++) {
2179 res = res << 6;
2180 pcnt++;
2181 }
2182 outp[k++] = res >> 16;
2183 if(pcnt == 2)
2184 goto done;
2185 outp[k++] = (res>>8)&0xFF;
2186 if(pcnt == 1)
2187 goto done;
2188 outp[k++] = res&0xFF;
2189 }
2190done:
2191 if(cflag == 0xFF) {
2192 ALOGE("[%s] base64 decode failed. Invalid character found %s",
2193 __func__, inp);
2194 return 0;
2195 }
2196 return k;
2197}
2198
2199int b64encode(uint8_t *inp, int ilen, char* outp)
2200{
2201 int i,j,k, num;
2202 int rem=0;
2203 uint32_t res=0;
2204
2205 if(inp == NULL || outp == NULL || ilen<=0) {
2206 ALOGE("[%s] received NULL pointer or zero input length",__func__);
2207 return -1;
2208 }
2209
2210 num = ilen/3;
2211 rem = ilen%3;
2212 j=0;k=0;
2213 for(i=0; i<num; i++) {
2214 //prepare index
2215 res = inp[j++]<<16;
2216 res = res | inp[j++]<<8;
2217 res = res | inp[j++];
2218 //get output map from index
2219 outp[k++] = (char) bTable[(res>>18)&0x3F];
2220 outp[k++] = (char) bTable[(res>>12)&0x3F];
2221 outp[k++] = (char) bTable[(res>>6)&0x3F];
2222 outp[k++] = (char) bTable[res&0x3F];
2223 }
2224
2225 switch(rem) {
2226 case 1:
2227 res = inp[j++]<<16;
2228 outp[k++] = (char) bTable[res>>18];
2229 outp[k++] = (char) bTable[(res>>12)&0x3F];
2230 //outp[k++] = '=';
2231 //outp[k++] = '=';
2232 break;
2233 case 2:
2234 res = inp[j++]<<16;
2235 res = res | inp[j++]<<8;
2236 outp[k++] = (char) bTable[res>>18];
2237 outp[k++] = (char) bTable[(res>>12)&0x3F];
2238 outp[k++] = (char) bTable[(res>>6)&0x3F];
2239 //outp[k++] = '=';
2240 break;
2241 default:
2242 break;
2243 }
Ben Rombergera04fabc2014-11-14 12:16:03 -08002244 outp[k] = '\0';
2245 return k;
2246}
Ashish Jain81eb2a82015-05-13 10:52:34 +05302247
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05302248
2249int audio_extn_utils_get_codec_version(const char *snd_card_name,
2250 int card_num,
2251 char *codec_version)
2252{
2253 char procfs_path[50];
2254 FILE *fp;
2255
2256 if (strstr(snd_card_name, "tasha")) {
2257 snprintf(procfs_path, sizeof(procfs_path),
2258 "/proc/asound/card%d/codecs/tasha/version", card_num);
2259 if ((fp = fopen(procfs_path, "r")) != NULL) {
2260 fgets(codec_version, CODEC_VERSION_MAX_LENGTH, fp);
2261 fclose(fp);
2262 } else {
2263 ALOGE("%s: ERROR. cannot open %s", __func__, procfs_path);
2264 return -ENOENT;
2265 }
2266 ALOGD("%s: codec version %s", __func__, codec_version);
2267 }
2268
2269 return 0;
2270}
2271
Preetam Singh Ranawat9d0d8e42019-07-15 12:27:25 +05302272int audio_extn_utils_get_codec_variant(int card_num,
2273 char *codec_variant)
2274{
2275 char procfs_path[50];
2276 FILE *fp;
2277 snprintf(procfs_path, sizeof(procfs_path),
2278 "/proc/asound/card%d/codecs/wcd938x/variant", card_num);
2279 if ((fp = fopen(procfs_path, "r")) == NULL) {
2280 snprintf(procfs_path, sizeof(procfs_path),
2281 "/proc/asound/card%d/codecs/wcd937x/variant", card_num);
2282 if ((fp = fopen(procfs_path, "r")) == NULL) {
2283 ALOGE("%s: ERROR. cannot open %s", __func__, procfs_path);
2284 return -ENOENT;
2285 }
2286 }
2287 fgets(codec_variant, CODEC_VARIANT_MAX_LENGTH, fp);
2288 fclose(fp);
2289 ALOGD("%s: codec variant is %s", __func__, codec_variant);
2290 return 0;
2291}
2292
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05302293
Ashish Jain81eb2a82015-05-13 10:52:34 +05302294#ifdef AUDIO_EXTERNAL_HDMI_ENABLED
2295
2296void get_default_compressed_channel_status(
2297 unsigned char *channel_status)
2298{
Ashish Jain81eb2a82015-05-13 10:52:34 +05302299 memset(channel_status,0,24);
2300
2301 /* block start bit in preamble bit 3 */
2302 channel_status[0] |= PROFESSIONAL;
2303 //compre out
2304 channel_status[0] |= NON_LPCM;
2305 // sample rate; fixed 48K for default/transcode
2306 channel_status[3] |= SR_48000;
2307}
2308
Ashish Jain81eb2a82015-05-13 10:52:34 +05302309int32_t get_compressed_channel_status(void *audio_stream_data,
2310 uint32_t audio_frame_size,
2311 unsigned char *channel_status,
2312 enum audio_parser_code_type codec_type)
2313 // codec_type - AUDIO_PARSER_CODEC_AC3
2314 // - AUDIO_PARSER_CODEC_DTS
2315{
2316 unsigned char *stream;
2317 int ret = 0;
2318 stream = (unsigned char *)audio_stream_data;
2319
2320 if (audio_stream_data == NULL || audio_frame_size == 0) {
2321 ALOGW("no buffer to get channel status, return default for compress");
2322 get_default_compressed_channel_status(channel_status);
2323 return ret;
2324 }
2325
2326 memset(channel_status,0,24);
2327 if(init_audio_parser(stream, audio_frame_size, codec_type) == -1)
2328 {
2329 ALOGE("init audio parser failed");
2330 return -1;
2331 }
2332 ret = get_channel_status(channel_status, codec_type);
2333 return ret;
2334
2335}
2336
Ashish Jain81eb2a82015-05-13 10:52:34 +05302337void get_lpcm_channel_status(uint32_t sampleRate,
2338 unsigned char *channel_status)
2339{
2340 int32_t status = 0;
Ashish Jain81eb2a82015-05-13 10:52:34 +05302341 memset(channel_status,0,24);
2342 /* block start bit in preamble bit 3 */
2343 channel_status[0] |= PROFESSIONAL;
2344 //LPCM OUT
2345 channel_status[0] &= ~NON_LPCM;
2346
2347 switch (sampleRate) {
2348 case 8000:
2349 case 11025:
2350 case 12000:
2351 case 16000:
2352 case 22050:
2353 channel_status[3] |= SR_NOTID;
Preetam Singh Ranawat61716b12015-12-14 11:55:24 +05302354 break;
Ashish Jain81eb2a82015-05-13 10:52:34 +05302355 case 24000:
2356 channel_status[3] |= SR_24000;
2357 break;
2358 case 32000:
2359 channel_status[3] |= SR_32000;
2360 break;
2361 case 44100:
2362 channel_status[3] |= SR_44100;
2363 break;
2364 case 48000:
2365 channel_status[3] |= SR_48000;
2366 break;
2367 case 88200:
2368 channel_status[3] |= SR_88200;
2369 break;
2370 case 96000:
2371 channel_status[3] |= SR_96000;
2372 break;
2373 case 176400:
2374 channel_status[3] |= SR_176400;
2375 break;
2376 case 192000:
2377 channel_status[3] |= SR_192000;
2378 break;
2379 default:
2380 ALOGV("Invalid sample_rate %u\n", sampleRate);
2381 status = -1;
2382 break;
2383 }
2384}
2385
2386void audio_utils_set_hdmi_channel_status(struct stream_out *out, char * buffer, size_t bytes)
2387{
2388 unsigned char channel_status[24]={0};
2389 struct snd_aes_iec958 iec958;
2390 const char *mixer_ctl_name = "IEC958 Playback PCM Stream";
2391 struct mixer_ctl *ctl;
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +05302392 ALOGV("%s: buffer %s bytes %zd", __func__, buffer, bytes);
Arun Mirpurie008ed22019-03-21 11:21:04 -07002393
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05302394 if (audio_extn_utils_is_dolby_format(out->format) &&
Ashish Jain81eb2a82015-05-13 10:52:34 +05302395 /*TODO:Extend code to support DTS passthrough*/
2396 /*set compressed channel status bits*/
Arun Mirpurie008ed22019-03-21 11:21:04 -07002397 audio_extn_passthru_is_passthrough_stream(out) &&
2398 audio_extn_is_hdmi_passthru_enabled()) {
Ashish Jain81eb2a82015-05-13 10:52:34 +05302399 get_compressed_channel_status(buffer, bytes, channel_status, AUDIO_PARSER_CODEC_AC3);
Arun Mirpurie008ed22019-03-21 11:21:04 -07002400 } else {
Ashish Jain81eb2a82015-05-13 10:52:34 +05302401 /*set channel status bit for LPCM*/
2402 get_lpcm_channel_status(out->sample_rate, channel_status);
2403 }
2404
2405 memcpy(iec958.status, channel_status,sizeof(iec958.status));
2406 ctl = mixer_get_ctl_by_name(out->dev->mixer, mixer_ctl_name);
2407 if (!ctl) {
2408 ALOGE("%s: Could not get ctl for mixer cmd - %s",
2409 __func__, mixer_ctl_name);
2410 return;
2411 }
2412 if (mixer_ctl_set_array(ctl, &iec958, sizeof(iec958)) < 0) {
2413 ALOGE("%s: Could not set channel status for ext HDMI ",
2414 __func__);
2415 return;
2416 }
2417
2418}
2419#endif
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302420
2421int audio_extn_utils_get_avt_device_drift(
2422 struct audio_usecase *usecase,
2423 struct audio_avt_device_drift_param *drift_param)
2424{
2425 int ret = 0, count = 0;
2426 char avt_device_drift_mixer_ctl_name[MIXER_PATH_MAX_LENGTH] = {0};
Naresh Tanniru6160c712017-04-17 15:43:48 +05302427 const char *backend = NULL;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302428 struct mixer_ctl *ctl = NULL;
2429 struct audio_avt_device_drift_stats drift_stats;
2430 struct audio_device *adev = NULL;
2431
2432 if (usecase != NULL && usecase->type == PCM_PLAYBACK) {
Naresh Tanniru6160c712017-04-17 15:43:48 +05302433 backend = platform_get_snd_device_backend_interface(usecase->out_snd_device);
2434 if (!backend) {
2435 ALOGE("%s: Unsupported device %d", __func__,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08002436 get_device_types(&usecase->stream.out->device_list));
Naresh Tanniru6160c712017-04-17 15:43:48 +05302437 ret = -EINVAL;
2438 goto done;
2439 }
2440 strlcpy(avt_device_drift_mixer_ctl_name,
2441 backend,
2442 MIXER_PATH_MAX_LENGTH);
2443
2444 count = strlen(backend);
2445 if (MIXER_PATH_MAX_LENGTH - count > 0) {
2446 strlcat(&avt_device_drift_mixer_ctl_name[count],
2447 " DRIFT",
2448 MIXER_PATH_MAX_LENGTH - count);
2449 } else {
2450 ret = -EINVAL;
2451 goto done;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302452 }
2453 } else {
Naresh Tanniru7586e292017-04-13 10:38:13 +05302454 ALOGE("%s: Invalid usecase",__func__);
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302455 ret = -EINVAL;
Naresh Tanniru6160c712017-04-17 15:43:48 +05302456 goto done;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302457 }
2458
Naresh Tanniru6160c712017-04-17 15:43:48 +05302459 adev = usecase->stream.out->dev;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302460 ctl = mixer_get_ctl_by_name(adev->mixer, avt_device_drift_mixer_ctl_name);
2461 if (!ctl) {
2462 ALOGE("%s: Could not get ctl for mixer cmd - %s",
2463 __func__, avt_device_drift_mixer_ctl_name);
2464
2465 ret = -EINVAL;
2466 goto done;
2467 }
2468
2469 ALOGV("%s: Getting AV Timer vs Device Drift mixer ctrl name %s", __func__,
2470 avt_device_drift_mixer_ctl_name);
2471
2472 mixer_ctl_update(ctl);
2473 count = mixer_ctl_get_num_values(ctl);
2474 if (count != sizeof(struct audio_avt_device_drift_stats)) {
2475 ALOGE("%s: mixer_ctl_get_num_values() invalid drift_stats data size",
2476 __func__);
2477
2478 ret = -EINVAL;
2479 goto done;
2480 }
2481
2482 ret = mixer_ctl_get_array(ctl, (void *)&drift_stats, count);
2483 if (ret != 0) {
2484 ALOGE("%s: mixer_ctl_get_array() failed to get drift_stats Params",
2485 __func__);
2486
2487 ret = -EINVAL;
2488 goto done;
2489 }
2490 memcpy(drift_param, &drift_stats.drift_param,
2491 sizeof(struct audio_avt_device_drift_param));
2492done:
2493 return ret;
2494}
Manish Dewangan07de2142017-02-27 19:27:20 +05302495
2496#ifdef SNDRV_COMPRESS_PATH_DELAY
2497int audio_extn_utils_compress_get_dsp_latency(struct stream_out *out)
2498{
2499 int ret = -EINVAL;
2500 struct snd_compr_metadata metadata;
2501 int delay_ms = COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
2502
Weiyin Jiangc49a3b52018-08-17 16:16:08 +08002503 /* override the latency for pcm offload use case */
2504 if ((out->flags & AUDIO_OUTPUT_FLAG_DIRECT) &&
2505 !(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)) {
2506 delay_ms = PCM_OFFLOAD_PLAYBACK_LATENCY;
2507 }
2508
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -07002509 if (property_get_bool("vendor.audio.playback.dsp.pathdelay", false)) {
Manish Dewangan07de2142017-02-27 19:27:20 +05302510 ALOGD("%s:: Quering DSP delay %d",__func__, __LINE__);
2511 if (!(is_offload_usecase(out->usecase))) {
2512 ALOGE("%s:: not supported for non offload session", __func__);
2513 goto exit;
2514 }
2515
2516 if (!out->compr) {
2517 ALOGD("%s:: Invalid compress handle,returning default dsp latency",
2518 __func__);
2519 goto exit;
2520 }
2521
2522 metadata.key = SNDRV_COMPRESS_PATH_DELAY;
2523 ret = compress_get_metadata(out->compr, &metadata);
2524 if(ret) {
2525 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2526 goto exit;
2527 }
2528 delay_ms = metadata.value[0] / 1000; /*convert to ms*/
2529 } else {
2530 ALOGD("%s:: Using Fix DSP delay",__func__);
2531 }
2532
2533exit:
2534 ALOGD("%s:: delay in ms is %d",__func__, delay_ms);
2535 return delay_ms;
2536}
2537#else
2538int audio_extn_utils_compress_get_dsp_latency(struct stream_out *out __unused)
2539{
2540 return COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
2541}
2542#endif
Manish Dewangan69426c82017-01-30 17:35:36 +05302543
2544#ifdef SNDRV_COMPRESS_RENDER_MODE
2545int audio_extn_utils_compress_set_render_mode(struct stream_out *out)
2546{
2547 struct snd_compr_metadata metadata;
2548 int ret = -EINVAL;
2549
2550 if (!(is_offload_usecase(out->usecase))) {
2551 ALOGE("%s:: not supported for non offload session", __func__);
2552 goto exit;
2553 }
2554
2555 if (!out->compr) {
2556 ALOGD("%s:: Invalid compress handle",
2557 __func__);
2558 goto exit;
2559 }
2560
2561 ALOGD("%s:: render mode %d", __func__, out->render_mode);
2562
2563 metadata.key = SNDRV_COMPRESS_RENDER_MODE;
2564 if (out->render_mode == RENDER_MODE_AUDIO_MASTER) {
2565 metadata.value[0] = SNDRV_COMPRESS_RENDER_MODE_AUDIO_MASTER;
2566 } else if (out->render_mode == RENDER_MODE_AUDIO_STC_MASTER) {
2567 metadata.value[0] = SNDRV_COMPRESS_RENDER_MODE_STC_MASTER;
2568 } else {
2569 ret = 0;
2570 goto exit;
2571 }
2572 ret = compress_set_metadata(out->compr, &metadata);
2573 if(ret) {
2574 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2575 }
2576exit:
2577 return ret;
2578}
2579#else
2580int audio_extn_utils_compress_set_render_mode(struct stream_out *out __unused)
2581{
2582 ALOGD("%s:: configuring render mode not supported", __func__);
2583 return 0;
2584}
2585#endif
Manish Dewangan58229382017-02-02 15:48:41 +05302586
2587#ifdef SNDRV_COMPRESS_CLK_REC_MODE
2588int audio_extn_utils_compress_set_clk_rec_mode(
2589 struct audio_usecase *usecase)
2590{
2591 struct snd_compr_metadata metadata;
2592 struct stream_out *out = NULL;
2593 int ret = -EINVAL;
2594
Naresh Tanniru7586e292017-04-13 10:38:13 +05302595 if (usecase == NULL || usecase->type != PCM_PLAYBACK) {
Manish Dewangan58229382017-02-02 15:48:41 +05302596 ALOGE("%s:: Invalid use case", __func__);
2597 goto exit;
2598 }
2599
2600 out = usecase->stream.out;
2601 if (!out) {
2602 ALOGE("%s:: invalid stream", __func__);
2603 goto exit;
2604 }
2605
2606 if (!is_offload_usecase(out->usecase)) {
2607 ALOGE("%s:: not supported for non offload session", __func__);
2608 goto exit;
2609 }
2610
2611 if (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER) {
2612 ALOGD("%s:: clk recovery is only supported in STC render mode",
2613 __func__);
2614 ret = 0;
2615 goto exit;
2616 }
2617
2618 if (!out->compr) {
2619 ALOGD("%s:: Invalid compress handle",
2620 __func__);
2621 goto exit;
2622 }
2623 metadata.key = SNDRV_COMPRESS_CLK_REC_MODE;
2624 switch(usecase->out_snd_device) {
2625 case SND_DEVICE_OUT_HDMI:
2626 case SND_DEVICE_OUT_SPEAKER_AND_HDMI:
2627 case SND_DEVICE_OUT_DISPLAY_PORT:
2628 case SND_DEVICE_OUT_SPEAKER_AND_DISPLAY_PORT:
2629 metadata.value[0] = SNDRV_COMPRESS_CLK_REC_MODE_NONE;
2630 break;
2631 default:
2632 metadata.value[0] = SNDRV_COMPRESS_CLK_REC_MODE_AUTO;
2633 break;
2634 }
2635
2636 ALOGD("%s:: clk recovery mode %d",__func__, metadata.value[0]);
2637
2638 ret = compress_set_metadata(out->compr, &metadata);
2639 if(ret) {
2640 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2641 }
2642
2643exit:
2644 return ret;
2645}
2646#else
2647int audio_extn_utils_compress_set_clk_rec_mode(
2648 struct audio_usecase *usecase __unused)
2649{
2650 ALOGD("%s:: configuring render mode not supported", __func__);
2651 return 0;
2652}
2653#endif
Manish Dewangan27346042017-03-01 12:56:12 +05302654
2655#ifdef SNDRV_COMPRESS_RENDER_WINDOW
2656int audio_extn_utils_compress_set_render_window(
2657 struct stream_out *out,
2658 struct audio_out_render_window_param *render_window)
2659{
2660 struct snd_compr_metadata metadata;
2661 int ret = -EINVAL;
2662
Manish Dewangan27346042017-03-01 12:56:12 +05302663 if(render_window == NULL) {
2664 ALOGE("%s:: Invalid render_window", __func__);
2665 goto exit;
2666 }
2667
Aniket Kumar Lata1e1d3662017-06-01 18:45:48 -07002668 ALOGD("%s:: render window start 0x%"PRIx64" end 0x%"PRIx64"",
2669 __func__,render_window->render_ws, render_window->render_we);
2670
Manish Dewangan27346042017-03-01 12:56:12 +05302671 if (!is_offload_usecase(out->usecase)) {
2672 ALOGE("%s:: not supported for non offload session", __func__);
2673 goto exit;
2674 }
2675
Naresh Tanniru6160c712017-04-17 15:43:48 +05302676 if ((out->render_mode != RENDER_MODE_AUDIO_MASTER) &&
2677 (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER)) {
Manish Dewangan27346042017-03-01 12:56:12 +05302678 ALOGD("%s:: only supported in timestamp mode, current "
2679 "render mode mode %d", __func__, out->render_mode);
2680 goto exit;
2681 }
2682
2683 if (!out->compr) {
2684 ALOGW("%s:: offload session not yet opened,"
2685 "render window will be configure later", __func__);
2686 /* store render window to reconfigure in start_output_stream() */
2687 goto exit;
2688 }
2689
2690 metadata.key = SNDRV_COMPRESS_RENDER_WINDOW;
2691 /*render window start value */
2692 metadata.value[0] = 0xFFFFFFFF & render_window->render_ws; /* lsb */
2693 metadata.value[1] = \
2694 (0xFFFFFFFF00000000 & render_window->render_ws) >> 32; /* msb*/
2695 /*render window end value */
2696 metadata.value[2] = 0xFFFFFFFF & render_window->render_we; /* lsb */
2697 metadata.value[3] = \
2698 (0xFFFFFFFF00000000 & render_window->render_we) >> 32; /* msb*/
2699
2700 ret = compress_set_metadata(out->compr, &metadata);
2701 if(ret) {
2702 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2703 }
2704
2705exit:
2706 return ret;
2707}
2708#else
2709int audio_extn_utils_compress_set_render_window(
2710 struct stream_out *out __unused,
2711 struct audio_out_render_window_param *render_window __unused)
2712{
2713 ALOGD("%s:: configuring render window not supported", __func__);
2714 return 0;
2715}
2716#endif
Manish Dewangan14956cc2017-02-14 18:54:42 +05302717
2718#ifdef SNDRV_COMPRESS_START_DELAY
2719int audio_extn_utils_compress_set_start_delay(
2720 struct stream_out *out,
2721 struct audio_out_start_delay_param *delay_param)
2722{
2723 struct snd_compr_metadata metadata;
2724 int ret = -EINVAL;
2725
2726 if(delay_param == NULL) {
2727 ALOGE("%s:: Invalid delay_param", __func__);
2728 goto exit;
2729 }
2730
2731 ALOGD("%s:: render start delay 0x%"PRIx64" ", __func__,
2732 delay_param->start_delay);
2733
2734 if (!is_offload_usecase(out->usecase)) {
2735 ALOGE("%s:: not supported for non offload session", __func__);
2736 goto exit;
2737 }
2738
Naresh Tanniru6160c712017-04-17 15:43:48 +05302739 if ((out->render_mode != RENDER_MODE_AUDIO_MASTER) &&
2740 (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER)) {
Manish Dewangan14956cc2017-02-14 18:54:42 +05302741 ALOGD("%s:: only supported in timestamp mode, current "
2742 "render mode mode %d", __func__, out->render_mode);
2743 goto exit;
2744 }
2745
2746 if (!out->compr) {
2747 ALOGW("%s:: offload session not yet opened,"
2748 "start delay will be configure later", __func__);
2749 goto exit;
2750 }
2751
2752 metadata.key = SNDRV_COMPRESS_START_DELAY;
2753 metadata.value[0] = 0xFFFFFFFF & delay_param->start_delay; /* lsb */
2754 metadata.value[1] = \
2755 (0xFFFFFFFF00000000 & delay_param->start_delay) >> 32; /* msb*/
2756
2757 ret = compress_set_metadata(out->compr, &metadata);
2758 if(ret) {
2759 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2760 }
2761
2762exit:
2763 return ret;
2764}
2765#else
2766int audio_extn_utils_compress_set_start_delay(
2767 struct stream_out *out __unused,
2768 struct audio_out_start_delay_param *delay_param __unused)
2769{
2770 ALOGD("%s:: configuring render window not supported", __func__);
2771 return 0;
2772}
2773#endif
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002774
Surendar Karka287348c2019-04-10 18:31:46 +05302775#ifdef SNDRV_COMPRESS_DSP_POSITION
2776int audio_extn_utils_compress_get_dsp_presentation_pos(struct stream_out *out,
2777 uint64_t *frames, struct timespec *timestamp, int32_t clock_id)
2778{
2779 int ret = -EINVAL;
2780 uint64_t *val = NULL;
2781 uint64_t time = 0;
2782 struct snd_compr_metadata metadata;
2783
2784 ALOGV("%s:: Quering DSP position with clock id %d",__func__, clock_id);
2785 metadata.key = SNDRV_COMPRESS_DSP_POSITION;
2786 metadata.value[0] = clock_id;
2787 ret = compress_get_metadata(out->compr, &metadata);
2788 if (ret) {
2789 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2790 ret = -errno;
2791 goto exit;
2792 }
2793 val = (uint64_t *)&metadata.value[1];
2794 *frames = *val;
2795 time = *(val + 1);
2796 timestamp->tv_sec = time / 1000000;
2797 timestamp->tv_nsec = (time % 1000000)*1000;
2798
2799exit:
2800 return ret;
2801}
2802#else
2803int audio_extn_utils_compress_get_dsp_presentation_pos(struct stream_out *out __unused,
2804 uint64_t *frames __unused, struct timespec *timestamp __unused,
2805 int32_t clock_id __unused)
2806{
2807 ALOGD("%s:: dsp presentation position not supported", __func__);
2808 return 0;
2809
2810}
2811#endif
2812
2813#ifdef SNDRV_PCM_IOCTL_DSP_POSITION
2814int audio_extn_utils_pcm_get_dsp_presentation_pos(struct stream_out *out,
2815 uint64_t *frames, struct timespec *timestamp, int32_t clock_id)
2816{
2817 int ret = -EINVAL;
2818 uint64_t time = 0;
2819 struct snd_pcm_prsnt_position prsnt_position;
2820
2821 ALOGV("%s:: Quering DSP position with clock id %d",__func__, clock_id);
2822 prsnt_position.clock_id = clock_id;
2823 ret = pcm_ioctl(out->pcm, SNDRV_PCM_IOCTL_DSP_POSITION, &prsnt_position);
2824 if (ret) {
2825 ALOGE("%s::error %d", __func__, ret);
2826 ret = -EIO;
2827 goto exit;
2828 }
2829
2830 *frames = prsnt_position.frames;
2831 time = prsnt_position.timestamp;
2832 timestamp->tv_sec = time / 1000000;
2833 timestamp->tv_nsec = (time % 1000000)*1000;
2834
2835exit:
2836 return ret;
2837}
2838#else
2839int audio_extn_utils_pcm_get_dsp_presentation_pos(struct stream_out *out __unused,
2840 uint64_t *frames __unused, struct timespec *timestamp __unused,
2841 int32_t clock_id __unused)
2842{
2843 ALOGD("%s:: dsp presentation position not supported", __func__);
2844 return 0;
2845
2846}
2847#endif
2848
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002849#define MAX_SND_CARD 8
Jaideep Sharmadf948882020-10-22 14:13:52 +05302850#define RETRY_US 400000
2851#define RETRY_NUMBER 100
Aalique Grahame22e49102018-12-18 14:23:57 -08002852#define PLATFORM_INFO_XML_PATH "audio_platform_info.xml"
2853#define PLATFORM_INFO_XML_BASE_STRING "audio_platform_info"
2854
Aalique Grahame22e49102018-12-18 14:23:57 -08002855bool audio_extn_utils_resolve_config_file(char file_name[MIXER_PATH_MAX_LENGTH])
2856{
2857 char full_config_path[MIXER_PATH_MAX_LENGTH];
Saurav Kumarf5d2c342020-07-29 19:31:49 +05302858 char vendor_config_path[VENDOR_CONFIG_PATH_MAX_LENGTH];
2859
2860 /* Get path for audio configuration files in vendor */
2861 audio_get_vendor_config_path(vendor_config_path,
2862 sizeof(vendor_config_path));
2863 snprintf(full_config_path, sizeof(full_config_path),
2864 "%s/%s", vendor_config_path, file_name);
2865 if (F_OK == access(full_config_path, 0)) {
2866 strlcpy(file_name, full_config_path, MIXER_PATH_MAX_LENGTH);
2867 return true;
Aalique Grahame22e49102018-12-18 14:23:57 -08002868 }
2869 return false;
2870}
2871
2872/* platform_info_file should be size 'MIXER_PATH_MAX_LENGTH' */
2873int audio_extn_utils_get_platform_info(const char* snd_card_name, char* platform_info_file)
2874{
2875 if (NULL == snd_card_name) {
2876 return -1;
2877 }
2878
2879 struct snd_card_split *snd_split_handle = NULL;
2880 int ret = 0;
2881 audio_extn_set_snd_card_split(snd_card_name);
2882 snd_split_handle = audio_extn_get_snd_card_split();
2883
2884 snprintf(platform_info_file, MIXER_PATH_MAX_LENGTH, "%s_%s_%s.xml",
2885 PLATFORM_INFO_XML_BASE_STRING, snd_split_handle->snd_card,
2886 snd_split_handle->form_factor);
2887
2888 if (!audio_extn_utils_resolve_config_file(platform_info_file)) {
2889 memset(platform_info_file, 0, MIXER_PATH_MAX_LENGTH);
2890 snprintf(platform_info_file, MIXER_PATH_MAX_LENGTH, "%s_%s.xml",
2891 PLATFORM_INFO_XML_BASE_STRING, snd_split_handle->snd_card);
2892
2893 if (!audio_extn_utils_resolve_config_file(platform_info_file)) {
2894 memset(platform_info_file, 0, MIXER_PATH_MAX_LENGTH);
2895 strlcpy(platform_info_file, PLATFORM_INFO_XML_PATH, MIXER_PATH_MAX_LENGTH);
2896 ret = audio_extn_utils_resolve_config_file(platform_info_file) ? 0 : -1;
2897 }
2898 }
2899
2900 return ret;
2901}
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002902
2903int audio_extn_utils_get_snd_card_num()
2904{
Soumya Managoli9fee7c62018-04-06 16:21:50 +05302905 int snd_card_num = 0;
2906 struct mixer *mixer = NULL;
2907
2908 snd_card_num = audio_extn_utils_open_snd_mixer(&mixer);
2909 if (mixer)
2910 mixer_close(mixer);
2911 return snd_card_num;
2912}
2913
2914int audio_extn_utils_open_snd_mixer(struct mixer **mixer_handle)
2915{
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002916
2917 void *hw_info = NULL;
2918 struct mixer *mixer = NULL;
2919 int retry_num = 0;
mpangfaa7bae2019-01-15 16:38:13 +08002920 int snd_card_num = 0;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002921 char* snd_card_name = NULL;
mpangfaa7bae2019-01-15 16:38:13 +08002922 int snd_card_detection_info[MAX_SND_CARD] = {0};
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002923
Soumya Managoli9fee7c62018-04-06 16:21:50 +05302924 if (!mixer_handle) {
2925 ALOGE("invalid mixer handle");
2926 return -1;
2927 }
2928 *mixer_handle = NULL;
Ashish Jain30ae8942017-11-05 17:21:23 +05302929 /*
mpangfaa7bae2019-01-15 16:38:13 +08002930 * Try with all the sound cards ( 0 to 7 ) and if none of them were detected
Ashish Jain30ae8942017-11-05 17:21:23 +05302931 * sleep for 1 sec and try detections with sound card 0 again.
2932 * If sound card gets detected, check if it is relevant, if not check with the
2933 * other sound cards. To ensure that the irrelevant sound card is not check again,
2934 * we maintain it in min_snd_card_num.
2935 */
2936 while (retry_num < RETRY_NUMBER) {
mpangfaa7bae2019-01-15 16:38:13 +08002937 snd_card_num = 0;
2938 while (snd_card_num < MAX_SND_CARD) {
2939 if (snd_card_detection_info[snd_card_num] == 0) {
2940 mixer = mixer_open(snd_card_num);
2941 if (!mixer)
2942 snd_card_num++;
2943 else
2944 break;
2945 } else
2946 snd_card_num++;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002947 }
2948
2949 if (!mixer) {
Ashish Jain30ae8942017-11-05 17:21:23 +05302950 usleep(RETRY_US);
Ashish Jain30ae8942017-11-05 17:21:23 +05302951 retry_num++;
mpangfaa7bae2019-01-15 16:38:13 +08002952 ALOGD("%s: retry, retry_num %d", __func__, retry_num);
Ashish Jain30ae8942017-11-05 17:21:23 +05302953 continue;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002954 }
2955
2956 snd_card_name = strdup(mixer_get_name(mixer));
2957 if (!snd_card_name) {
2958 ALOGE("failed to allocate memory for snd_card_name\n");
2959 mixer_close(mixer);
2960 return -1;
2961 }
2962 ALOGD("%s: snd_card_name: %s", __func__, snd_card_name);
mpangfaa7bae2019-01-15 16:38:13 +08002963 snd_card_detection_info[snd_card_num] = 1;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002964 hw_info = hw_info_init(snd_card_name);
2965 if (hw_info) {
2966 ALOGD("%s: Opened sound card:%d", __func__, snd_card_num);
2967 break;
2968 }
mpangfaa7bae2019-01-15 16:38:13 +08002969 ALOGE("%s: Failed to init hardware info, snd_card_num:%d", __func__, snd_card_num);
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002970
Dhananjay Kumara7e27832017-07-11 15:40:39 +05302971 free(snd_card_name);
2972 snd_card_name = NULL;
2973
2974 mixer_close(mixer);
2975 mixer = NULL;
2976 }
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002977 if (snd_card_name)
2978 free(snd_card_name);
Ashish Jain30ae8942017-11-05 17:21:23 +05302979
Dhananjay Kumara7e27832017-07-11 15:40:39 +05302980 if (hw_info)
2981 hw_info_deinit(hw_info);
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002982
mpangfaa7bae2019-01-15 16:38:13 +08002983 if (retry_num >= RETRY_NUMBER) {
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002984 ALOGE("%s: Unable to find correct sound card, aborting.", __func__);
2985 return -1;
2986 }
2987
Soumya Managoli9fee7c62018-04-06 16:21:50 +05302988 if (mixer)
2989 *mixer_handle = mixer;
2990
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002991 return snd_card_num;
2992}
Naresh Tanniru6160c712017-04-17 15:43:48 +05302993
Soumya Managoli9fee7c62018-04-06 16:21:50 +05302994void audio_extn_utils_close_snd_mixer(struct mixer *mixer)
2995{
2996 if (mixer)
2997 mixer_close(mixer);
2998}
2999
Naresh Tanniru6160c712017-04-17 15:43:48 +05303000#ifdef SNDRV_COMPRESS_ENABLE_ADJUST_SESSION_CLOCK
3001int audio_extn_utils_compress_enable_drift_correction(
3002 struct stream_out *out,
3003 struct audio_out_enable_drift_correction *drift)
3004{
3005 struct snd_compr_metadata metadata;
3006 int ret = -EINVAL;
3007
3008 if(drift == NULL) {
3009 ALOGE("%s:: Invalid param", __func__);
3010 goto exit;
3011 }
3012
3013 ALOGD("%s:: drift enable %d", __func__,drift->enable);
3014
3015 if (!is_offload_usecase(out->usecase)) {
3016 ALOGE("%s:: not supported for non offload session", __func__);
3017 goto exit;
3018 }
3019
3020 if (!out->compr) {
3021 ALOGW("%s:: offload session not yet opened,"
3022 "start delay will be configure later", __func__);
3023 goto exit;
3024 }
3025
3026 metadata.key = SNDRV_COMPRESS_ENABLE_ADJUST_SESSION_CLOCK;
3027 metadata.value[0] = drift->enable;
3028 out->drift_correction_enabled = drift->enable;
3029
3030 ret = compress_set_metadata(out->compr, &metadata);
3031 if(ret) {
3032 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
3033 out->drift_correction_enabled = false;
3034 }
3035
3036exit:
3037 return ret;
3038}
3039#else
3040int audio_extn_utils_compress_enable_drift_correction(
3041 struct stream_out *out __unused,
3042 struct audio_out_enable_drift_correction *drift __unused)
3043{
3044 ALOGD("%s:: configuring drift enablement not supported", __func__);
3045 return 0;
3046}
3047#endif
3048
3049#ifdef SNDRV_COMPRESS_ADJUST_SESSION_CLOCK
3050int audio_extn_utils_compress_correct_drift(
3051 struct stream_out *out,
3052 struct audio_out_correct_drift *drift_param)
3053{
3054 struct snd_compr_metadata metadata;
3055 int ret = -EINVAL;
3056
3057 if (drift_param == NULL) {
3058 ALOGE("%s:: Invalid drift_param", __func__);
3059 goto exit;
3060 }
3061
3062 ALOGD("%s:: adjust time 0x%"PRIx64" ", __func__,
3063 drift_param->adjust_time);
3064
3065 if (!is_offload_usecase(out->usecase)) {
3066 ALOGE("%s:: not supported for non offload session", __func__);
3067 goto exit;
3068 }
3069
3070 if (!out->compr) {
3071 ALOGW("%s:: offload session not yet opened", __func__);
3072 goto exit;
3073 }
3074
3075 if (!out->drift_correction_enabled) {
3076 ALOGE("%s:: drift correction not enabled", __func__);
3077 goto exit;
3078 }
3079
3080 metadata.key = SNDRV_COMPRESS_ADJUST_SESSION_CLOCK;
3081 metadata.value[0] = 0xFFFFFFFF & drift_param->adjust_time; /* lsb */
3082 metadata.value[1] = \
3083 (0xFFFFFFFF00000000 & drift_param->adjust_time) >> 32; /* msb*/
3084
3085 ret = compress_set_metadata(out->compr, &metadata);
3086 if(ret)
3087 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
3088exit:
3089 return ret;
3090}
3091#else
3092int audio_extn_utils_compress_correct_drift(
3093 struct stream_out *out __unused,
3094 struct audio_out_correct_drift *drift_param __unused)
3095{
3096 ALOGD("%s:: setting adjust clock not supported", __func__);
3097 return 0;
3098}
3099#endif
Naresh Tanniru29bce4e2017-04-27 17:54:30 +05303100
3101int audio_extn_utils_set_channel_map(
3102 struct stream_out *out,
3103 struct audio_out_channel_map_param *channel_map_param)
3104{
3105 int ret = -EINVAL, i = 0;
3106 int channels = audio_channel_count_from_out_mask(out->channel_mask);
3107
3108 if (channel_map_param == NULL) {
3109 ALOGE("%s:: Invalid channel_map", __func__);
3110 goto exit;
3111 }
3112
3113 if (channel_map_param->channels != channels) {
3114 ALOGE("%s:: Channels(%d) does not match stream channels(%d)",
3115 __func__, channel_map_param->channels, channels);
3116 goto exit;
3117 }
3118
3119 for ( i = 0; i < channels; i++) {
3120 ALOGV("%s:: channel_map[%d]- %d", __func__, i, channel_map_param->channel_map[i]);
3121 out->channel_map_param.channel_map[i] = channel_map_param->channel_map[i];
3122 }
3123 ret = 0;
3124exit:
3125 return ret;
3126}
Varun Balaraje49253e2017-07-06 19:48:56 +05303127
3128int audio_extn_utils_set_pan_scale_params(
3129 struct stream_out *out,
3130 struct mix_matrix_params *mm_params)
3131{
3132 int ret = -EINVAL, i = 0, j = 0;
3133
Varun Balaraj003ee752017-08-07 17:54:55 +05303134 if (mm_params == NULL || out == NULL) {
3135 ALOGE("%s:: Invalid mix matrix or out param", __func__);
Varun Balaraje49253e2017-07-06 19:48:56 +05303136 goto exit;
3137 }
3138
3139 if (mm_params->num_output_channels > MAX_CHANNELS_SUPPORTED ||
3140 mm_params->num_output_channels <= 0 ||
3141 mm_params->num_input_channels > MAX_CHANNELS_SUPPORTED ||
3142 mm_params->num_input_channels <= 0)
3143 goto exit;
3144
3145 out->pan_scale_params.num_output_channels = mm_params->num_output_channels;
3146 out->pan_scale_params.num_input_channels = mm_params->num_input_channels;
3147 out->pan_scale_params.has_output_channel_map =
3148 mm_params->has_output_channel_map;
3149 for (i = 0; i < mm_params->num_output_channels; i++)
3150 out->pan_scale_params.output_channel_map[i] =
3151 mm_params->output_channel_map[i];
3152
3153 out->pan_scale_params.has_input_channel_map =
3154 mm_params->has_input_channel_map;
3155 for (i = 0; i < mm_params->num_input_channels; i++)
3156 out->pan_scale_params.input_channel_map[i] =
3157 mm_params->input_channel_map[i];
3158
3159 out->pan_scale_params.has_mixer_coeffs = mm_params->has_mixer_coeffs;
3160 for (i = 0; i < mm_params->num_output_channels; i++)
3161 for (j = 0; j < mm_params->num_input_channels; j++) {
3162 //Convert the channel coefficient gains in Q14 format
3163 out->pan_scale_params.mixer_coeffs[i][j] =
3164 mm_params->mixer_coeffs[i][j] * (2 << 13);
3165 }
3166
3167 ret = platform_set_stream_pan_scale_params(out->dev->platform,
3168 out->pcm_device_id,
3169 out->pan_scale_params);
3170
3171exit:
3172 return ret;
3173}
3174
3175int audio_extn_utils_set_downmix_params(
3176 struct stream_out *out,
3177 struct mix_matrix_params *mm_params)
3178{
3179 int ret = -EINVAL, i = 0, j = 0;
3180 struct audio_usecase *usecase = NULL;
3181
Aniket Kumar Lataf9f246e2017-09-15 15:20:16 -07003182 if (mm_params == NULL || out == NULL) {
Varun Balaraj003ee752017-08-07 17:54:55 +05303183 ALOGE("%s:: Invalid mix matrix or out param", __func__);
Varun Balaraje49253e2017-07-06 19:48:56 +05303184 goto exit;
3185 }
3186
3187 if (mm_params->num_output_channels > MAX_CHANNELS_SUPPORTED ||
3188 mm_params->num_output_channels <= 0 ||
3189 mm_params->num_input_channels > MAX_CHANNELS_SUPPORTED ||
3190 mm_params->num_input_channels <= 0)
3191 goto exit;
3192
3193 usecase = get_usecase_from_list(out->dev, out->usecase);
Varun Balaraj003ee752017-08-07 17:54:55 +05303194 if (!usecase) {
3195 ALOGE("%s: Get usecase list failed!", __func__);
Aniket Kumar Lataf9f246e2017-09-15 15:20:16 -07003196 goto exit;
3197 }
Varun Balaraje49253e2017-07-06 19:48:56 +05303198 out->downmix_params.num_output_channels = mm_params->num_output_channels;
3199 out->downmix_params.num_input_channels = mm_params->num_input_channels;
3200
3201 out->downmix_params.has_output_channel_map =
3202 mm_params->has_output_channel_map;
3203 for (i = 0; i < mm_params->num_output_channels; i++) {
3204 out->downmix_params.output_channel_map[i] =
3205 mm_params->output_channel_map[i];
3206 }
3207
3208 out->downmix_params.has_input_channel_map =
3209 mm_params->has_input_channel_map;
3210 for (i = 0; i < mm_params->num_input_channels; i++)
3211 out->downmix_params.input_channel_map[i] =
3212 mm_params->input_channel_map[i];
3213
3214 out->downmix_params.has_mixer_coeffs = mm_params->has_mixer_coeffs;
3215 for (i = 0; i < mm_params->num_output_channels; i++)
Nikhil Latukar2e6f6242017-08-15 13:37:07 +05303216 for (j = 0; j < mm_params->num_input_channels; j++) {
3217 //Convert the channel coefficient gains in Q14 format
Varun Balaraje49253e2017-07-06 19:48:56 +05303218 out->downmix_params.mixer_coeffs[i][j] =
Nikhil Latukar2e6f6242017-08-15 13:37:07 +05303219 mm_params->mixer_coeffs[i][j] * (2 << 13);
3220 }
Varun Balaraje49253e2017-07-06 19:48:56 +05303221
3222 ret = platform_set_stream_downmix_params(out->dev->platform,
3223 out->pcm_device_id,
3224 usecase->out_snd_device,
3225 out->downmix_params);
3226
3227exit:
3228 return ret;
3229}
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05303230
3231bool audio_extn_utils_is_dolby_format(audio_format_t format)
3232{
3233 if (format == AUDIO_FORMAT_AC3 ||
3234 format == AUDIO_FORMAT_E_AC3 ||
3235 format == AUDIO_FORMAT_E_AC3_JOC)
3236 return true;
3237 else
3238 return false;
3239}
3240
`Deeraj Soman676c2702017-09-18 19:25:53 +05303241int audio_extn_utils_get_bit_width_from_string(const char *id_string)
3242{
3243 int i;
3244 const mixer_config_lookup mixer_bitwidth_config[] = {{"S24_3LE", 24},
3245 {"S32_LE", 32},
3246 {"S24_LE", 24},
3247 {"S16_LE", 16}};
3248 int num_configs = sizeof(mixer_bitwidth_config) / sizeof(mixer_bitwidth_config[0]);
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05303249
`Deeraj Soman676c2702017-09-18 19:25:53 +05303250 for (i = 0; i < num_configs; i++) {
3251 if (!strcmp(id_string, mixer_bitwidth_config[i].id_string))
3252 return mixer_bitwidth_config[i].value;
3253 }
3254
3255 return -EINVAL;
3256}
3257
3258int audio_extn_utils_get_sample_rate_from_string(const char *id_string)
3259{
3260 int i;
Weiyin Jiang30f9ab52020-11-11 11:37:21 +08003261 const mixer_config_lookup mixer_samplerate_config[] = {{"KHZ_8", 8000},
3262 {"KHZ_11P025", 11025},
3263 {"KHZ_16", 16000},
3264 {"KHZ_22P05", 22050},
3265 {"KHZ_32", 32000},
`Deeraj Soman676c2702017-09-18 19:25:53 +05303266 {"KHZ_48", 48000},
3267 {"KHZ_96", 96000},
3268 {"KHZ_144", 144000},
3269 {"KHZ_192", 192000},
3270 {"KHZ_384", 384000},
3271 {"KHZ_44P1", 44100},
3272 {"KHZ_88P2", 88200},
3273 {"KHZ_176P4", 176400},
3274 {"KHZ_352P8", 352800}};
3275 int num_configs = sizeof(mixer_samplerate_config) / sizeof(mixer_samplerate_config[0]);
3276
3277 for (i = 0; i < num_configs; i++) {
3278 if (!strcmp(id_string, mixer_samplerate_config[i].id_string))
3279 return mixer_samplerate_config[i].value;
3280 }
3281
3282 return -EINVAL;
3283}
3284
3285int audio_extn_utils_get_channels_from_string(const char *id_string)
3286{
3287 int i;
3288 const mixer_config_lookup mixer_channels_config[] = {{"One", 1},
3289 {"Two", 2},
3290 {"Three",3},
3291 {"Four", 4},
3292 {"Five", 5},
3293 {"Six", 6},
3294 {"Seven", 7},
3295 {"Eight", 8}};
3296 int num_configs = sizeof(mixer_channels_config) / sizeof(mixer_channels_config[0]);
3297
3298 for (i = 0; i < num_configs; i++) {
3299 if (!strcmp(id_string, mixer_channels_config[i].id_string))
3300 return mixer_channels_config[i].value;
3301 }
3302
3303 return -EINVAL;
3304}
Surendar karka30569792018-05-08 12:02:21 +05303305
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08003306void audio_extn_utils_release_snd_device(snd_device_t snd_device)
3307{
3308 audio_extn_dev_arbi_release(snd_device);
3309 audio_extn_sound_trigger_update_device_status(snd_device,
3310 ST_EVENT_SND_DEVICE_FREE);
3311 audio_extn_listen_update_device_status(snd_device,
3312 LISTEN_EVENT_SND_DEVICE_FREE);
3313}
3314
Weiyin Jiangd5718bd2019-09-04 16:52:08 +08003315int audio_extn_utils_get_license_params(
3316 const struct audio_device *adev,
3317 struct audio_license_params *license_params)
Surendar karka30569792018-05-08 12:02:21 +05303318{
3319 if(!license_params)
3320 return -EINVAL;
Weiyin Jiangd5718bd2019-09-04 16:52:08 +08003321
3322 return platform_get_license_by_product(adev->platform,
3323 (const char*)license_params->product, &license_params->key, license_params->license);
Surendar karka30569792018-05-08 12:02:21 +05303324}
3325
Aalique Grahame22e49102018-12-18 14:23:57 -08003326int audio_extn_utils_send_app_type_gain(struct audio_device *adev,
3327 int app_type,
3328 int *gain)
3329{
3330 int gain_cfg[4];
3331 const char *mixer_ctl_name = "App Type Gain";
3332 struct mixer_ctl *ctl;
3333 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
3334 if (!ctl) {
3335 ALOGE("%s: Could not get volume ctl mixer %s", __func__,
3336 mixer_ctl_name);
3337 return -EINVAL;
3338 }
3339 gain_cfg[0] = 0;
3340 gain_cfg[1] = app_type;
3341 gain_cfg[2] = gain[0];
3342 gain_cfg[3] = gain[1];
3343 ALOGV("%s app_type %d l(%d) r(%d)", __func__, app_type, gain[0], gain[1]);
3344 return mixer_ctl_set_array(ctl, gain_cfg,
3345 sizeof(gain_cfg)/sizeof(gain_cfg[0]));
3346}
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -07003347
Weiyin Jiangd5718bd2019-09-04 16:52:08 +08003348static void vndk_fwk_init()
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -07003349{
Weiyin Jiangd5718bd2019-09-04 16:52:08 +08003350 if (mVndkFwk.lib_handle != NULL)
3351 return;
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -07003352
Weiyin Jiangd5718bd2019-09-04 16:52:08 +08003353 mVndkFwk.lib_handle = dlopen(VNDK_FWK_LIB_PATH, RTLD_NOW);
3354 if (mVndkFwk.lib_handle == NULL) {
3355 ALOGW("%s: failed to dlopen VNDK_FWK_LIB %s", __func__, strerror(errno));
3356 return;
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -07003357 }
3358
Weiyin Jiangd5718bd2019-09-04 16:52:08 +08003359 *(void **)(&mVndkFwk.isVendorEnhancedFwk) =
3360 dlsym(mVndkFwk.lib_handle, "isRunningWithVendorEnhancedFramework");
3361 if (mVndkFwk.isVendorEnhancedFwk == NULL) {
3362 ALOGW("%s: dlsym failed %s", __func__, strerror(errno));
3363 if (mVndkFwk.lib_handle) {
3364 dlclose(mVndkFwk.lib_handle);
3365 mVndkFwk.lib_handle = NULL;
3366 }
3367 return;
3368 }
3369
3370
3371 *(void **)(&mVndkFwk.getVendorEnhancedInfo) =
3372 dlsym(mVndkFwk.lib_handle, "getVendorEnhancedInfo");
3373 if (mVndkFwk.getVendorEnhancedInfo == NULL) {
3374 ALOGW("%s: dlsym failed %s", __func__, strerror(errno));
3375 if (mVndkFwk.lib_handle) {
3376 dlclose(mVndkFwk.lib_handle);
3377 mVndkFwk.lib_handle = NULL;
3378 }
3379 }
3380
3381 return;
3382}
3383
3384bool audio_extn_utils_is_vendor_enhanced_fwk()
3385{
3386 static int is_vendor_enhanced_fwk = -EINVAL;
3387 if (is_vendor_enhanced_fwk != -EINVAL)
3388 return (bool)is_vendor_enhanced_fwk;
3389
3390 vndk_fwk_init();
3391
3392 if (mVndkFwk.isVendorEnhancedFwk != NULL) {
3393 is_vendor_enhanced_fwk = mVndkFwk.isVendorEnhancedFwk();
3394 ALOGW("%s: is_vendor_enhanced_fwk %d", __func__, is_vendor_enhanced_fwk);
3395 } else {
3396 is_vendor_enhanced_fwk = 0;
3397 ALOGW("%s: default to non enhanced_fwk config", __func__);
3398 }
3399
3400 return (bool)is_vendor_enhanced_fwk;
3401}
3402
3403int audio_extn_utils_get_vendor_enhanced_info()
3404{
3405 static int vendor_enhanced_info = -EINVAL;
3406 if (vendor_enhanced_info != -EINVAL)
3407 return vendor_enhanced_info;
3408
3409 vndk_fwk_init();
3410
3411 if (mVndkFwk.getVendorEnhancedInfo != NULL) {
3412 vendor_enhanced_info = mVndkFwk.getVendorEnhancedInfo();
3413 ALOGW("%s: vendor_enhanced_info 0x%x", __func__, vendor_enhanced_info);
3414 } else {
3415 vendor_enhanced_info = 0x0;
3416 ALOGW("%s: default to vendor_enhanced_info 0x0", __func__);
3417 }
3418
3419 return vendor_enhanced_info;
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -07003420}
Deeraj Soman14230922019-01-30 16:39:30 +05303421
Deeraj Somanfa377bf2019-02-06 12:57:59 +05303422int audio_extn_utils_get_perf_mode_flag(void)
3423{
3424#ifdef COMPRESSED_PERF_MODE_FLAG
3425 return COMPRESSED_PERF_MODE_FLAG;
3426#else
3427 return 0;
3428#endif
3429}
3430
Deeraj Soman14230922019-01-30 16:39:30 +05303431size_t audio_extn_utils_get_input_buffer_size(uint32_t sample_rate,
3432 audio_format_t format,
3433 int channel_count,
3434 int64_t duration_ms,
3435 bool is_low_latency)
3436{
3437 size_t size = 0;
3438 size_t capture_duration = AUDIO_CAPTURE_PERIOD_DURATION_MSEC;
3439 uint32_t bytes_per_period_sample = 0;
3440
3441
3442 if (audio_extn_utils_check_input_parameters(sample_rate, format, channel_count) != 0)
3443 return 0;
3444
3445 if (duration_ms >= MIN_OFFLOAD_BUFFER_DURATION_MS && duration_ms <= MAX_OFFLOAD_BUFFER_DURATION_MS)
3446 capture_duration = duration_ms;
3447
3448 size = (sample_rate * capture_duration) / 1000;
3449 if (is_low_latency)
3450 size = LOW_LATENCY_CAPTURE_PERIOD_SIZE;
3451
3452
3453 bytes_per_period_sample = audio_bytes_per_sample(format) * channel_count;
3454 size *= bytes_per_period_sample;
3455
3456 /* make sure the size is multiple of 32 bytes and additionally multiple of
3457 * the frame_size (required for 24bit samples and non-power-of-2 channel counts)
3458 * At 48 kHz mono 16-bit PCM:
3459 * 5.000 ms = 240 frames = 15*16*1*2 = 480, a whole multiple of 32 (15)
3460 * 3.333 ms = 160 frames = 10*16*1*2 = 320, a whole multiple of 32 (10)
3461 *
3462 * The loop reaches result within 32 iterations, as initial size is
3463 * already a multiple of frame_size
3464 */
3465 size = audio_extn_utils_nearest_multiple(size, audio_extn_utils_lcm(32, bytes_per_period_sample));
3466
3467 return size;
3468}
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07003469
3470int audio_extn_utils_hash_fn(void *key)
3471{
3472 return (int)key;
3473}
3474
3475bool audio_extn_utils_hash_eq(void *key1, void *key2)
3476{
3477 return (key1 == key2);
3478}