blob: b34171b794e4146a7b72442c4426a7eea6653f45 [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
Weiyin Jiangd5718bd2019-09-04 16:52:08 +0800114typedef struct vndkfwk_s {
115 void *lib_handle;
116 int (*isVendorEnhancedFwk)(void);
117 int (*getVendorEnhancedInfo)(void);
118 const char *lib_name;
119} vndkfwk_t;
120
121static vndkfwk_t mVndkFwk = {
122 NULL, NULL, NULL, VNDK_FWK_LIB_PATH};
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -0700123
`Deeraj Soman676c2702017-09-18 19:25:53 +0530124typedef struct {
125 const char *id_string;
126 const int value;
127} mixer_config_lookup;
128
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700129struct string_to_enum {
130 const char *name;
131 uint32_t value;
132};
133
134const struct string_to_enum s_flag_name_to_enum_table[] = {
135 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_DIRECT),
136 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_PRIMARY),
137 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_FAST),
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800138 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_RAW),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700139 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_DEEP_BUFFER),
140 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD),
141 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_NON_BLOCKING),
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700142 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_HW_AV_SYNC),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700143 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_INCALL_MUSIC),
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700144 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH),
Naresh Tanniruee3499a2017-01-05 14:05:35 +0530145 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_TIMESTAMP),
Vikram Panduranga93f080e2017-06-07 18:16:14 -0700146 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_VOIP_RX),
Ben Romberger6c4d3812017-06-13 17:46:45 -0700147 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_BD),
Varun Balaraje49253e2017-07-06 19:48:56 +0530148 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_INTERACTIVE),
Derek Chen090dfc52018-03-22 22:15:29 -0400149 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_MEDIA),
150 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_SYS_NOTIFICATION),
151 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_NAV_GUIDANCE),
152 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_PHONE),
Susan Wang117cf6f2022-04-06 20:11:46 -0400153 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_ALERTS),
Derek Chen1bcdc6b2020-02-06 22:44:53 -0800154 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_FRONT_PASSENGER),
Derek Chendf05eea2019-08-01 13:57:49 -0700155 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_REAR_SEAT),
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530156 STRING_TO_ENUM(AUDIO_INPUT_FLAG_NONE),
157 STRING_TO_ENUM(AUDIO_INPUT_FLAG_FAST),
158 STRING_TO_ENUM(AUDIO_INPUT_FLAG_HW_HOTWORD),
159 STRING_TO_ENUM(AUDIO_INPUT_FLAG_RAW),
160 STRING_TO_ENUM(AUDIO_INPUT_FLAG_SYNC),
Dhananjay Kumaree4d2002016-10-25 18:02:58 +0530161 STRING_TO_ENUM(AUDIO_INPUT_FLAG_TIMESTAMP),
Dhananjay Kumar704ce6f2017-09-28 22:08:00 +0530162 STRING_TO_ENUM(AUDIO_INPUT_FLAG_COMPRESS),
Ralf Herzaec80262018-07-03 07:08:49 +0200163 STRING_TO_ENUM(AUDIO_INPUT_FLAG_PASSTHROUGH),
George Gao3d477082020-09-17 22:29:07 -0700164 STRING_TO_ENUM(AUDIO_INPUT_FLAG_MMAP_NOIRQ),
165 STRING_TO_ENUM(AUDIO_INPUT_FLAG_VOIP_TX),
166 STRING_TO_ENUM(AUDIO_INPUT_FLAG_HW_AV_SYNC),
167 STRING_TO_ENUM(AUDIO_INPUT_FLAG_DIRECT),
Huicheng Liu1404ba12020-09-11 01:03:25 -0400168 STRING_TO_ENUM(AUDIO_INPUT_FLAG_PRIMARY),
169 STRING_TO_ENUM(AUDIO_INPUT_FLAG_FRONT_PASSENGER),
170 STRING_TO_ENUM(AUDIO_INPUT_FLAG_REAR_SEAT),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700171};
172
173const struct string_to_enum s_format_name_to_enum_table[] = {
Ashish Jain83a6cc22016-06-28 14:34:17 +0530174 STRING_TO_ENUM(AUDIO_FORMAT_PCM_8_BIT),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700175 STRING_TO_ENUM(AUDIO_FORMAT_PCM_16_BIT),
Ashish Jain5106d362016-05-11 19:23:33 +0530176 STRING_TO_ENUM(AUDIO_FORMAT_PCM_24_BIT_PACKED),
177 STRING_TO_ENUM(AUDIO_FORMAT_PCM_8_24_BIT),
Ashish Jainf1eaa582016-05-23 20:54:24 +0530178 STRING_TO_ENUM(AUDIO_FORMAT_PCM_32_BIT),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700179 STRING_TO_ENUM(AUDIO_FORMAT_MP3),
180 STRING_TO_ENUM(AUDIO_FORMAT_AAC),
181 STRING_TO_ENUM(AUDIO_FORMAT_VORBIS),
Mingming Yinae3530f2014-07-03 16:50:18 -0700182 STRING_TO_ENUM(AUDIO_FORMAT_AMR_NB),
183 STRING_TO_ENUM(AUDIO_FORMAT_AMR_WB),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700184 STRING_TO_ENUM(AUDIO_FORMAT_AC3),
Mingming Yinae3530f2014-07-03 16:50:18 -0700185 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700186 STRING_TO_ENUM(AUDIO_FORMAT_DTS),
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530187 STRING_TO_ENUM(AUDIO_FORMAT_DTS_HD),
Ben Romberger1aaaf862017-04-06 17:49:46 -0700188 STRING_TO_ENUM(AUDIO_FORMAT_DOLBY_TRUEHD),
Naresh Tanniru928f0862017-04-07 16:44:23 -0700189 STRING_TO_ENUM(AUDIO_FORMAT_IEC61937),
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530190 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3_JOC),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700191 STRING_TO_ENUM(AUDIO_FORMAT_WMA),
192 STRING_TO_ENUM(AUDIO_FORMAT_WMA_PRO),
193 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADIF),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700194 STRING_TO_ENUM(AUDIO_FORMAT_AMR_WB_PLUS),
195 STRING_TO_ENUM(AUDIO_FORMAT_EVRC),
196 STRING_TO_ENUM(AUDIO_FORMAT_EVRCB),
197 STRING_TO_ENUM(AUDIO_FORMAT_EVRCWB),
198 STRING_TO_ENUM(AUDIO_FORMAT_QCELP),
199 STRING_TO_ENUM(AUDIO_FORMAT_MP2),
200 STRING_TO_ENUM(AUDIO_FORMAT_EVRCNW),
Amit Shekhar6f461b12014-08-01 14:52:58 -0700201 STRING_TO_ENUM(AUDIO_FORMAT_FLAC),
Satya Krishna Pindiproli70471602015-04-24 19:12:43 +0530202 STRING_TO_ENUM(AUDIO_FORMAT_ALAC),
203 STRING_TO_ENUM(AUDIO_FORMAT_APE),
Alexy Josephcd8eaed2014-12-11 12:46:53 -0800204 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LC),
205 STRING_TO_ENUM(AUDIO_FORMAT_AAC_HE_V1),
206 STRING_TO_ENUM(AUDIO_FORMAT_AAC_HE_V2),
Manish Dewangana6fc5442015-08-24 20:30:31 +0530207 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS),
Ashish Jaine513a872015-11-19 17:00:56 +0530208 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_LC),
209 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_HE_V1),
210 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_HE_V2),
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +0530211 STRING_TO_ENUM(AUDIO_FORMAT_DSD),
Arun Kumar Dasari3b174182016-12-27 13:01:14 +0530212 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM),
213 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM_LC),
214 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM_HE_V1),
215 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM_HE_V2),
Dhanalakshmi Siddani18737932016-11-29 17:33:17 +0530216 STRING_TO_ENUM(AUDIO_FORMAT_APTX),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700217};
218
Manish Dewangan3ccdea52017-02-13 19:31:54 +0530219/* payload structure avt_device drift query */
220struct audio_avt_device_drift_stats {
221 uint32_t minor_version;
Manish Dewangan338c50a2017-09-12 15:22:03 +0530222
Manish Dewangan3ccdea52017-02-13 19:31:54 +0530223 /* Indicates the device interface direction as either
224 * source (Tx) or sink (Rx).
225 */
226 uint16_t device_direction;
Manish Dewangan338c50a2017-09-12 15:22:03 +0530227
228 /* Reference timer for drift accumulation and time stamp information.
229 * currently it only support AFE_REF_TIMER_TYPE_AVTIMER
230 */
231 uint16_t reference_timer;
Manish Dewangan3ccdea52017-02-13 19:31:54 +0530232 struct audio_avt_device_drift_param drift_param;
Manish Dewangan338c50a2017-09-12 15:22:03 +0530233} __attribute__((packed));
Manish Dewangan3ccdea52017-02-13 19:31:54 +0530234
Ben Rombergera04fabc2014-11-14 12:16:03 -0800235static char bTable[BASE_TABLE_SIZE] = {
236 'A','B','C','D','E','F','G','H','I','J','K','L',
237 'M','N','O','P','Q','R','S','T','U','V','W','X',
238 'Y','Z','a','b','c','d','e','f','g','h','i','j',
239 'k','l','m','n','o','p','q','r','s','t','u','v',
240 'w','x','y','z','0','1','2','3','4','5','6','7',
241 '8','9','+','/'
242};
243
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700244static uint32_t string_to_enum(const struct string_to_enum *table, size_t size,
245 const char *name)
246{
247 size_t i;
248 for (i = 0; i < size; i++) {
249 if (strcmp(table[i].name, name) == 0) {
250 ALOGV("%s found %s", __func__, table[i].name);
251 return table[i].value;
252 }
253 }
George Gao3d477082020-09-17 22:29:07 -0700254 ALOGE("%s cound not find %s", __func__, name);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700255 return 0;
256}
257
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530258static audio_io_flags_t parse_flag_names(char *name)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700259{
260 uint32_t flag = 0;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530261 audio_io_flags_t io_flags;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800262 char *last_r;
263 char *flag_name = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700264 while (flag_name != NULL) {
265 if (strlen(flag_name) != 0) {
266 flag |= string_to_enum(s_flag_name_to_enum_table,
267 ARRAY_SIZE(s_flag_name_to_enum_table),
268 flag_name);
269 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800270 flag_name = strtok_r(NULL, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700271 }
272
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800273 ALOGV("parse_flag_names: flag - %x", flag);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530274 io_flags.in_flags = (audio_input_flags_t)flag;
275 io_flags.out_flags = (audio_output_flags_t)flag;
276 return io_flags;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700277}
278
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530279static void parse_format_names(char *name, struct streams_io_cfg *s_info)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700280{
281 struct stream_format *sf_info = NULL;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800282 char *last_r;
283 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700284
285 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG) == 0)
286 return;
287
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530288 list_init(&s_info->format_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700289 while (str != NULL) {
290 audio_format_t format = (audio_format_t)string_to_enum(s_format_name_to_enum_table,
291 ARRAY_SIZE(s_format_name_to_enum_table), str);
292 ALOGV("%s: format - %d", __func__, format);
293 if (format != 0) {
294 sf_info = (struct stream_format *)calloc(1, sizeof(struct stream_format));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700295 if (sf_info == NULL)
296 break; /* return whatever was parsed */
297
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700298 sf_info->format = format;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530299 list_add_tail(&s_info->format_list, &sf_info->list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700300 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800301 str = strtok_r(NULL, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700302 }
303}
304
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530305static void parse_sample_rate_names(char *name, struct streams_io_cfg *s_info)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700306{
Amit Shekhar6f461b12014-08-01 14:52:58 -0700307 struct stream_sample_rate *ss_info = NULL;
308 uint32_t sample_rate = 48000;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800309 char *last_r;
310 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700311
Amit Shekhar6f461b12014-08-01 14:52:58 -0700312 if (str != NULL && 0 == strcmp(str, DYNAMIC_VALUE_TAG))
313 return;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700314
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530315 list_init(&s_info->sample_rate_list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700316 while (str != NULL) {
317 sample_rate = (uint32_t)strtol(str, (char **)NULL, 10);
318 ALOGV("%s: sample_rate - %d", __func__, sample_rate);
319 if (0 != sample_rate) {
320 ss_info = (struct stream_sample_rate *)calloc(1, sizeof(struct stream_sample_rate));
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800321 if (!ss_info) {
322 ALOGE("%s: memory allocation failure", __func__);
323 return;
324 }
Amit Shekhar6f461b12014-08-01 14:52:58 -0700325 ss_info->sample_rate = sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530326 list_add_tail(&s_info->sample_rate_list, &ss_info->list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700327 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800328 str = strtok_r(NULL, "|", &last_r);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700329 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700330}
331
332static int parse_bit_width_names(char *name)
333{
334 int bit_width = 16;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800335 char *last_r;
336 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700337
338 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG))
339 bit_width = (int)strtol(str, (char **)NULL, 10);
340
341 ALOGV("%s: bit_width - %d", __func__, bit_width);
342 return bit_width;
343}
344
345static int parse_app_type_names(void *platform, char *name)
346{
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700347 int app_type = platform_get_default_app_type(platform);
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800348 char *last_r;
349 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700350
351 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG))
352 app_type = (int)strtol(str, (char **)NULL, 10);
353
354 ALOGV("%s: app_type - %d", __func__, app_type);
355 return app_type;
356}
357
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530358static void update_streams_cfg_list(cnode *root, void *platform,
359 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700360{
361 cnode *node = root->first_child;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530362 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700363
364 ALOGV("%s", __func__);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530365 s_info = (struct streams_io_cfg *)calloc(1, sizeof(struct streams_io_cfg));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700366
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530367 if (!s_info) {
368 ALOGE("failed to allocate mem for s_info list element");
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700369 return;
370 }
371
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700372 while (node) {
373 if (strcmp(node->name, FLAGS_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530374 s_info->flags = parse_flag_names((char *)node->value);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530375 } else if (strcmp(node->name, PROFILES_TAG) == 0) {
376 strlcpy(s_info->profile, (char *)node->value, sizeof(s_info->profile));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700377 } else if (strcmp(node->name, FORMATS_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530378 parse_format_names((char *)node->value, s_info);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700379 } else if (strcmp(node->name, SAMPLING_RATES_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530380 s_info->app_type_cfg.sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
381 parse_sample_rate_names((char *)node->value, s_info);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700382 } else if (strcmp(node->name, BIT_WIDTH_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530383 s_info->app_type_cfg.bit_width = parse_bit_width_names((char *)node->value);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700384 } else if (strcmp(node->name, APP_TYPE_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530385 s_info->app_type_cfg.app_type = parse_app_type_names(platform, (char *)node->value);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700386 }
387 node = node->next;
388 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530389 list_add_tail(streams_cfg_list, &s_info->list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700390}
391
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530392static void load_cfg_list(cnode *root, void *platform,
393 struct listnode *streams_output_cfg_list,
394 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700395{
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530396 cnode *node = NULL;
397
398 node = config_find(root, OUTPUTS_TAG);
399 if (node != NULL) {
400 node = node->first_child;
401 while (node) {
402 ALOGV("%s: loading output %s", __func__, node->name);
403 update_streams_cfg_list(node, platform, streams_output_cfg_list);
404 node = node->next;
405 }
406 } else {
407 ALOGI("%s: could not load output, node is NULL", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700408 }
409
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530410 node = config_find(root, INPUTS_TAG);
411 if (node != NULL) {
412 node = node->first_child;
413 while (node) {
414 ALOGV("%s: loading input %s", __func__, node->name);
415 update_streams_cfg_list(node, platform, streams_input_cfg_list);
416 node = node->next;
417 }
418 } else {
419 ALOGI("%s: could not load input, node is NULL", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700420 }
421}
422
423static void send_app_type_cfg(void *platform, struct mixer *mixer,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530424 struct listnode *streams_output_cfg_list,
425 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700426{
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530427 size_t app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT] = {0};
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700428 int length = 0, i, num_app_types = 0;
429 struct listnode *node;
430 bool update;
431 struct mixer_ctl *ctl = NULL;
432 const char *mixer_ctl_name = "App Type Config";
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530433 struct streams_io_cfg *s_info = NULL;
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800434 uint32_t target_bit_width = 0;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700435
436 if (!mixer) {
437 ALOGE("%s: mixer is null",__func__);
438 return;
439 }
440 ctl = mixer_get_ctl_by_name(mixer, mixer_ctl_name);
441 if (!ctl) {
442 ALOGE("%s: Could not get ctl for mixer cmd - %s",__func__, mixer_ctl_name);
443 return;
444 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530445 app_type_cfg[length++] = num_app_types;
446
447 if (list_empty(streams_output_cfg_list)) {
448 app_type_cfg[length++] = platform_get_default_app_type_v2(platform, PCM_PLAYBACK);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700449 app_type_cfg[length++] = 48000;
450 app_type_cfg[length++] = 16;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530451 num_app_types += 1;
452 }
453 if (list_empty(streams_input_cfg_list)) {
454 app_type_cfg[length++] = platform_get_default_app_type_v2(platform, PCM_CAPTURE);
455 app_type_cfg[length++] = 48000;
456 app_type_cfg[length++] = 16;
457 num_app_types += 1;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700458 }
459
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800460 /* get target bit width for ADM enforce mode */
461 target_bit_width = adev_get_dsp_bit_width_enforce_mode();
462
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700463 list_for_each(node, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530464 s_info = node_to_item(node, struct streams_io_cfg, list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700465 update = true;
466 for (i=0; i<length; i=i+3) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530467 if (app_type_cfg[i+1] == 0)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700468 break;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530469 else if (app_type_cfg[i+1] == (size_t)s_info->app_type_cfg.app_type) {
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530470 if (app_type_cfg[i+2] < (size_t)s_info->app_type_cfg.sample_rate)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530471 app_type_cfg[i+2] = s_info->app_type_cfg.sample_rate;
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530472 if (app_type_cfg[i+3] < (size_t)s_info->app_type_cfg.bit_width)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530473 app_type_cfg[i+3] = s_info->app_type_cfg.bit_width;
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800474 /* ADM bit width = max(enforce_bit_width, bit_width from s_info */
475 if (audio_extn_is_dsp_bit_width_enforce_mode_supported(s_info->flags.out_flags) &&
476 (target_bit_width > app_type_cfg[i+3]))
477 app_type_cfg[i+3] = target_bit_width;
478
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700479 update = false;
480 break;
481 }
482 }
483 if (update && ((length + 3) <= MAX_LENGTH_MIXER_CONTROL_IN_INT)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530484 num_app_types += 1;
485 app_type_cfg[length++] = s_info->app_type_cfg.app_type;
486 app_type_cfg[length++] = s_info->app_type_cfg.sample_rate;
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800487 app_type_cfg[length] = s_info->app_type_cfg.bit_width;
488 if (audio_extn_is_dsp_bit_width_enforce_mode_supported(s_info->flags.out_flags) &&
489 (target_bit_width > app_type_cfg[length]))
490 app_type_cfg[length] = target_bit_width;
491
492 length++;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530493 }
494 }
495 list_for_each(node, streams_input_cfg_list) {
496 s_info = node_to_item(node, struct streams_io_cfg, list);
497 update = true;
498 for (i=0; i<length; i=i+3) {
499 if (app_type_cfg[i+1] == 0)
500 break;
501 else if (app_type_cfg[i+1] == (size_t)s_info->app_type_cfg.app_type) {
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530502 if (app_type_cfg[i+2] < (size_t)s_info->app_type_cfg.sample_rate)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530503 app_type_cfg[i+2] = s_info->app_type_cfg.sample_rate;
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530504 if (app_type_cfg[i+3] < (size_t)s_info->app_type_cfg.bit_width)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530505 app_type_cfg[i+3] = s_info->app_type_cfg.bit_width;
506 update = false;
507 break;
508 }
509 }
510 if (update && ((length + 3) <= MAX_LENGTH_MIXER_CONTROL_IN_INT)) {
511 num_app_types += 1;
512 app_type_cfg[length++] = s_info->app_type_cfg.app_type;
513 app_type_cfg[length++] = s_info->app_type_cfg.sample_rate;
514 app_type_cfg[length++] = s_info->app_type_cfg.bit_width;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700515 }
516 }
517 ALOGV("%s: num_app_types: %d", __func__, num_app_types);
518 if (num_app_types) {
519 app_type_cfg[0] = num_app_types;
520 mixer_ctl_set_array(ctl, app_type_cfg, length);
521 }
522}
523
Roopesh Nataraja0f68be72020-04-28 12:32:02 -0700524/* Function to retrieve audio vendor configs path */
525void audio_get_vendor_config_path (char* config_file_path, int path_size)
526{
527 char vendor_sku[PROPERTY_VALUE_MAX] = {'\0'};
528 if (property_get("ro.boot.product.vendor.sku", vendor_sku, "") <= 0) {
529#ifdef LINUX_ENABLED
530 /* Audio configs are stored in /etc */
531 snprintf(config_file_path, path_size, "%s", "/etc");
532#else
533 /* Audio configs are stored in /vendor/etc */
534 snprintf(config_file_path, path_size, "%s", "/vendor/etc");
535#endif
536 } else {
537 /* Audio configs are stored in /vendor/etc/audio/sku_${vendor_sku} */
538 snprintf(config_file_path, path_size,
539 "%s%s", "/vendor/etc/audio/sku_", vendor_sku);
540 }
541}
542
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530543void audio_extn_utils_update_streams_cfg_lists(void *platform,
544 struct mixer *mixer,
545 struct listnode *streams_output_cfg_list,
546 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700547{
548 cnode *root;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530549 char *data = NULL;
Roopesh Nataraja0f68be72020-04-28 12:32:02 -0700550 char vendor_config_path[VENDOR_CONFIG_PATH_MAX_LENGTH];
551 char audio_io_policy_file[VENDOR_CONFIG_FILE_MAX_LENGTH];
552 char audio_output_policy_file[VENDOR_CONFIG_FILE_MAX_LENGTH];
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700553
554 ALOGV("%s", __func__);
555 list_init(streams_output_cfg_list);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530556 list_init(streams_input_cfg_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700557
558 root = config_node("", "");
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700559 if (root == NULL) {
560 ALOGE("cfg_list, NULL config root");
561 return;
562 }
563
Roopesh Nataraja0f68be72020-04-28 12:32:02 -0700564 /* Get path for audio configuration files in vendor */
565 audio_get_vendor_config_path(vendor_config_path,
566 sizeof(vendor_config_path));
567
568 /* Get path for audio_io_policy_file in vendor */
569 snprintf(audio_io_policy_file, sizeof(audio_io_policy_file),
570 "%s/%s", vendor_config_path, AUDIO_IO_POLICY_VENDOR_CONFIG_FILE_NAME);
571
572 /* Load audio_io_policy_file from vendor */
573 data = (char *)load_file(audio_io_policy_file, NULL);
574
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530575 if (data == NULL) {
576 ALOGD("%s: failed to open io config file(%s), trying older config file",
Roopesh Nataraja0f68be72020-04-28 12:32:02 -0700577 __func__, audio_io_policy_file);
578
579 /* Get path for audio_output_policy_file in vendor */
580 snprintf(audio_output_policy_file, sizeof(audio_output_policy_file),
581 "%s/%s", vendor_config_path,
582 AUDIO_OUTPUT_POLICY_VENDOR_CONFIG_FILE_NAME);
583
584 /* Load audio_output_policy_file from vendor */
585 data = (char *)load_file(audio_output_policy_file, NULL);
586
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530587 if (data == NULL) {
588 send_app_type_cfg(platform, mixer,
589 streams_output_cfg_list,
590 streams_input_cfg_list);
591 ALOGE("%s: could not load io policy config!", __func__);
yidongh6eb4f752017-04-19 18:20:57 +0800592 free(root);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530593 return;
594 }
595 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700596
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530597 config_load(root, data);
598 load_cfg_list(root, platform, streams_output_cfg_list,
599 streams_input_cfg_list);
600
601 send_app_type_cfg(platform, mixer, streams_output_cfg_list,
602 streams_input_cfg_list);
Alexy Josephaee4fdd2016-01-29 13:02:07 -0800603
604 config_free(root);
yidongh6eb4f752017-04-19 18:20:57 +0800605 free(root);
Alexy Josephaee4fdd2016-01-29 13:02:07 -0800606 free(data);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700607}
608
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530609static void audio_extn_utils_dump_streams_cfg_list(
610 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700611{
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700612 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530613 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700614 struct stream_format *sf_info;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700615 struct stream_sample_rate *ss_info;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530616
617 list_for_each(node_i, streams_cfg_list) {
618 s_info = node_to_item(node_i, struct streams_io_cfg, list);
619 ALOGV("%s: flags-%d, sample_rate-%d, bit_width-%d, app_type-%d",
620 __func__, s_info->flags.out_flags, s_info->app_type_cfg.sample_rate,
621 s_info->app_type_cfg.bit_width, s_info->app_type_cfg.app_type);
622 list_for_each(node_j, &s_info->format_list) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700623 sf_info = node_to_item(node_j, struct stream_format, list);
624 ALOGV("format-%x", sf_info->format);
625 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530626 list_for_each(node_j, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700627 ss_info = node_to_item(node_j, struct stream_sample_rate, list);
628 ALOGV("sample rate-%d", ss_info->sample_rate);
629 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700630 }
631}
632
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530633void audio_extn_utils_dump_streams_cfg_lists(
634 struct listnode *streams_output_cfg_list,
635 struct listnode *streams_input_cfg_list)
636{
637 ALOGV("%s", __func__);
638 audio_extn_utils_dump_streams_cfg_list(streams_output_cfg_list);
639 audio_extn_utils_dump_streams_cfg_list(streams_input_cfg_list);
640}
641
642static void audio_extn_utils_release_streams_cfg_list(
643 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700644{
645 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530646 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700647
648 ALOGV("%s", __func__);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530649
650 while (!list_empty(streams_cfg_list)) {
651 node_i = list_head(streams_cfg_list);
652 s_info = node_to_item(node_i, struct streams_io_cfg, list);
653 while (!list_empty(&s_info->format_list)) {
654 node_j = list_head(&s_info->format_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700655 list_remove(node_j);
656 free(node_to_item(node_j, struct stream_format, list));
657 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530658 while (!list_empty(&s_info->sample_rate_list)) {
659 node_j = list_head(&s_info->sample_rate_list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700660 list_remove(node_j);
661 free(node_to_item(node_j, struct stream_sample_rate, list));
662 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700663 list_remove(node_i);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530664 free(node_to_item(node_i, struct streams_io_cfg, list));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700665 }
666}
667
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530668void audio_extn_utils_release_streams_cfg_lists(
669 struct listnode *streams_output_cfg_list,
670 struct listnode *streams_input_cfg_list)
671{
672 ALOGV("%s", __func__);
673 audio_extn_utils_release_streams_cfg_list(streams_output_cfg_list);
674 audio_extn_utils_release_streams_cfg_list(streams_input_cfg_list);
675}
676
677static bool set_app_type_cfg(struct streams_io_cfg *s_info,
678 struct stream_app_type_cfg *app_type_cfg,
679 uint32_t sample_rate, uint32_t bit_width)
Amit Shekhar6f461b12014-08-01 14:52:58 -0700680 {
681 struct listnode *node_i;
682 struct stream_sample_rate *ss_info;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530683 list_for_each(node_i, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700684 ss_info = node_to_item(node_i, struct stream_sample_rate, list);
685 if ((sample_rate <= ss_info->sample_rate) &&
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530686 (bit_width == s_info->app_type_cfg.bit_width)) {
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700687
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530688 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700689 app_type_cfg->sample_rate = ss_info->sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530690 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700691 ALOGV("%s app_type_cfg->app_type %d, app_type_cfg->sample_rate %d, app_type_cfg->bit_width %d",
692 __func__, app_type_cfg->app_type, app_type_cfg->sample_rate, app_type_cfg->bit_width);
693 return true;
694 }
695 }
696 /*
697 * Reiterate through the list assuming dafault sample rate.
698 * Handles scenario where input sample rate is higher
699 * than all sample rates in list for the input bit width.
700 */
701 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
Apoorv Raghuvanshif59bb222015-02-18 12:23:23 -0800702
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530703 list_for_each(node_i, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700704 ss_info = node_to_item(node_i, struct stream_sample_rate, list);
705 if ((sample_rate <= ss_info->sample_rate) &&
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530706 (bit_width == s_info->app_type_cfg.bit_width)) {
707 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700708 app_type_cfg->sample_rate = sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530709 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Apoorv Raghuvanshif59bb222015-02-18 12:23:23 -0800710 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 -0700711 __func__, app_type_cfg->app_type, app_type_cfg->sample_rate, app_type_cfg->bit_width);
712 return true;
713 }
714 }
715 return false;
716}
717
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530718void audio_extn_utils_update_stream_input_app_type_cfg(void *platform,
719 struct listnode *streams_input_cfg_list,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -0800720 struct listnode *devices __unused,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530721 audio_input_flags_t flags,
722 audio_format_t format,
723 uint32_t sample_rate,
724 uint32_t bit_width,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530725 char* profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530726 struct stream_app_type_cfg *app_type_cfg)
727{
728 struct listnode *node_i, *node_j;
729 struct streams_io_cfg *s_info;
730 struct stream_format *sf_info;
731
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530732 ALOGV("%s: flags: 0x%x, format: 0x%x sample_rate %d, profile %s",
733 __func__, flags, format, sample_rate, profile);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530734
735 list_for_each(node_i, streams_input_cfg_list) {
736 s_info = node_to_item(node_i, struct streams_io_cfg, list);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530737 /* Along with flags do profile matching if set at either end.*/
738 if (s_info->flags.in_flags == flags &&
739 ((profile[0] == '\0' && s_info->profile[0] == '\0') ||
740 strncmp(s_info->profile, profile, sizeof(s_info->profile)) == 0)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530741 list_for_each(node_j, &s_info->format_list) {
742 sf_info = node_to_item(node_j, struct stream_format, list);
743 if (sf_info->format == format) {
744 if (set_app_type_cfg(s_info, app_type_cfg, sample_rate, bit_width))
745 return;
746 }
747 }
748 }
749 }
750 ALOGW("%s: App type could not be selected. Falling back to default", __func__);
751 app_type_cfg->app_type = platform_get_default_app_type_v2(platform, PCM_CAPTURE);
752 app_type_cfg->sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
753 app_type_cfg->bit_width = 16;
754}
755
756void audio_extn_utils_update_stream_output_app_type_cfg(void *platform,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700757 struct listnode *streams_output_cfg_list,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -0800758 struct listnode *devices,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700759 audio_output_flags_t flags,
760 audio_format_t format,
Amit Shekhar6f461b12014-08-01 14:52:58 -0700761 uint32_t sample_rate,
762 uint32_t bit_width,
Manish Dewangan837dc462015-05-27 10:17:41 +0530763 audio_channel_mask_t channel_mask,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530764 char *profile,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700765 struct stream_app_type_cfg *app_type_cfg)
766{
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +0530767 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530768 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700769 struct stream_format *sf_info;
Manish Dewangan837dc462015-05-27 10:17:41 +0530770 char value[PROPERTY_VALUE_MAX] = {0};
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700771
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -0800772 if (compare_device_type(devices, AUDIO_DEVICE_OUT_SPEAKER)) {
Ramu Gottipati074fa4d2018-09-11 20:05:51 +0530773 int bw = platform_get_snd_device_bit_width(SND_DEVICE_OUT_SPEAKER);
774 if ((-ENOSYS != bw) && (bit_width > (uint32_t)bw))
Amit Shekhar5a39c912014-10-14 15:39:30 -0700775 bit_width = (uint32_t)bw;
Ramu Gottipati074fa4d2018-09-11 20:05:51 +0530776 else if (-ENOSYS == bw)
777 bit_width = CODEC_BACKEND_DEFAULT_BIT_WIDTH;
Amit Shekhar1d896042014-10-03 13:16:09 -0700778 sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Aniket Kumar Latad13758f2020-08-06 15:11:36 -0700779 ALOGV("%s Allowing 24 and above bits playback on speaker \
780 ONLY at default sampling rate", __func__);
Amit Shekhar1d896042014-10-03 13:16:09 -0700781 }
782
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -0700783 property_get("vendor.audio.playback.mch.downsample",value,"");
Manish Dewangan837dc462015-05-27 10:17:41 +0530784 if (!strncmp("true", value, sizeof("true"))) {
785 if ((popcount(channel_mask) > 2) &&
786 (sample_rate > CODEC_BACKEND_DEFAULT_SAMPLE_RATE) &&
Aalique Grahame5cd12ff2017-10-19 10:57:00 -0700787 !(flags & (audio_output_flags_t)AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH)) {
Manish Dewangan837dc462015-05-27 10:17:41 +0530788 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
789 ALOGD("%s: MCH session defaulting sample rate to %d",
790 __func__, sample_rate);
791 }
792 }
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +0530793
794 /* Set sampling rate to 176.4 for DSD64
795 * and 352.8Khz for DSD128.
796 * Set Bit Width to 16. output will be 16 bit
797 * post DoP in ASM.
798 */
Aalique Grahame5cd12ff2017-10-19 10:57:00 -0700799 if ((flags & (audio_output_flags_t)AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH) &&
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +0530800 (format == AUDIO_FORMAT_DSD)) {
801 bit_width = 16;
802 if (sample_rate == INPUT_SAMPLING_RATE_DSD64)
803 sample_rate = OUTPUT_SAMPLING_RATE_DSD64;
804 else if (sample_rate == INPUT_SAMPLING_RATE_DSD128)
805 sample_rate = OUTPUT_SAMPLING_RATE_DSD128;
806 }
807
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +0530808
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800809 ALOGV("%s: flags: %x, format: %x sample_rate %d, profile %s, app_type %d",
810 __func__, flags, format, sample_rate, profile, app_type_cfg->app_type);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700811 list_for_each(node_i, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530812 s_info = node_to_item(node_i, struct streams_io_cfg, list);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530813 /* Along with flags do profile matching if set at either end.*/
814 if (s_info->flags.out_flags == flags &&
815 ((profile[0] == '\0' && s_info->profile[0] == '\0') ||
816 strncmp(s_info->profile, profile, sizeof(s_info->profile)) == 0)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530817 list_for_each(node_j, &s_info->format_list) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700818 sf_info = node_to_item(node_j, struct stream_format, list);
819 if (sf_info->format == format) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530820 if (set_app_type_cfg(s_info, app_type_cfg, sample_rate, bit_width))
Amit Shekhar6f461b12014-08-01 14:52:58 -0700821 return;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700822 }
823 }
824 }
825 }
826 list_for_each(node_i, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530827 s_info = node_to_item(node_i, struct streams_io_cfg, list);
828 if (s_info->flags.out_flags == AUDIO_OUTPUT_FLAG_PRIMARY) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700829 ALOGV("Compatible output profile not found.");
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530830 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
831 app_type_cfg->sample_rate = s_info->app_type_cfg.sample_rate;
832 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700833 ALOGV("%s Default to primary output: App type: %d sample_rate %d",
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530834 __func__, s_info->app_type_cfg.app_type, app_type_cfg->sample_rate);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700835 return;
836 }
837 }
838 ALOGW("%s: App type could not be selected. Falling back to default", __func__);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700839 app_type_cfg->app_type = platform_get_default_app_type(platform);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700840 app_type_cfg->sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700841 app_type_cfg->bit_width = 16;
842}
843
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +0530844static bool audio_is_this_native_usecase(struct audio_usecase *uc)
845{
846 bool native_usecase = false;
847 struct stream_out *out = (struct stream_out*) uc->stream.out;
848
849 if (PCM_PLAYBACK == uc->type && out != NULL &&
850 NATIVE_AUDIO_MODE_INVALID != platform_get_native_support() &&
851 is_offload_usecase(uc->id) &&
852 (out->sample_rate == OUTPUT_SAMPLING_RATE_44100))
853 native_usecase = true;
854
855 return native_usecase;
856}
857
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800858bool audio_extn_is_dsp_bit_width_enforce_mode_supported(audio_output_flags_t flags)
859{
860 /* DSP bitwidth enforce mode for ADM and AFE:
861 * includes:
862 * deep buffer, low latency, direct pcm and offload.
863 * excludes:
864 * ull(raw+fast), VOIP.
865 */
866 if ((flags & AUDIO_OUTPUT_FLAG_VOIP_RX) ||
867 ((flags & AUDIO_OUTPUT_FLAG_RAW) &&
868 (flags & AUDIO_OUTPUT_FLAG_FAST)))
869 return false;
870
871
872 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) ||
873 (flags & AUDIO_OUTPUT_FLAG_DIRECT) ||
874 (flags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) ||
875 (flags & AUDIO_OUTPUT_FLAG_PRIMARY))
876 return true;
877 else
878 return false;
879}
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800880
881static inline bool audio_is_vr_mode_on(struct audio_device *(__attribute__((unused)) adev))
882{
883 return adev->vr_audio_mode_enabled;
884}
885
Derek Chenf7092792017-05-23 12:23:53 -0400886static void audio_extn_btsco_get_sample_rate(int snd_device, int *sample_rate)
887{
888 switch (snd_device) {
889 case SND_DEVICE_OUT_BT_SCO:
890 case SND_DEVICE_IN_BT_SCO_MIC:
891 case SND_DEVICE_IN_BT_SCO_MIC_NREC:
892 *sample_rate = 8000;
893 break;
894 case SND_DEVICE_OUT_BT_SCO_WB:
895 case SND_DEVICE_IN_BT_SCO_MIC_WB:
896 case SND_DEVICE_IN_BT_SCO_MIC_WB_NREC:
897 *sample_rate = 16000;
898 break;
899 default:
Aniket Kumar Latad13758f2020-08-06 15:11:36 -0700900 ALOGV("%s:Not a BT SCO device, need not update sampling rate\n", __func__);
Derek Chenf7092792017-05-23 12:23:53 -0400901 break;
902 }
903}
904
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530905void audio_extn_utils_update_stream_app_type_cfg_for_usecase(
906 struct audio_device *adev,
907 struct audio_usecase *usecase)
908{
909 ALOGV("%s", __func__);
910
911 switch(usecase->type) {
912 case PCM_PLAYBACK:
913 audio_extn_utils_update_stream_output_app_type_cfg(adev->platform,
914 &adev->streams_output_cfg_list,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -0800915 &usecase->stream.out->device_list,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530916 usecase->stream.out->flags,
Aalique Grahame65780b52017-09-27 14:59:56 -0700917 usecase->stream.out->hal_op_format,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530918 usecase->stream.out->sample_rate,
919 usecase->stream.out->bit_width,
920 usecase->stream.out->channel_mask,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530921 usecase->stream.out->profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530922 &usecase->stream.out->app_type_cfg);
923 ALOGV("%s Selected apptype: %d", __func__, usecase->stream.out->app_type_cfg.app_type);
924 break;
925 case PCM_CAPTURE:
Revathi Uddarajud9f23d92020-07-27 10:55:06 +0530926 if (usecase->id == USECASE_AUDIO_RECORD_VOIP
927 || usecase->id == USECASE_AUDIO_RECORD_VOIP_LOW_LATENCY)
Vikram Panduranga93f080e2017-06-07 18:16:14 -0700928 usecase->stream.in->app_type_cfg.app_type = APP_TYPE_VOIP_AUDIO;
929 else
930 audio_extn_utils_update_stream_input_app_type_cfg(adev->platform,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530931 &adev->streams_input_cfg_list,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -0800932 &usecase->stream.in->device_list,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530933 usecase->stream.in->flags,
934 usecase->stream.in->format,
935 usecase->stream.in->sample_rate,
936 usecase->stream.in->bit_width,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530937 usecase->stream.in->profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530938 &usecase->stream.in->app_type_cfg);
939 ALOGV("%s Selected apptype: %d", __func__, usecase->stream.in->app_type_cfg.app_type);
940 break;
Surendar Karka93cd25a2018-08-28 14:21:37 +0530941 case TRANSCODE_LOOPBACK_RX :
Siddartha Shaik343abc62017-08-08 11:15:25 +0530942 audio_extn_utils_update_stream_output_app_type_cfg(adev->platform,
943 &adev->streams_output_cfg_list,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -0800944 &usecase->stream.inout->out_config.device_list,
Siddartha Shaik343abc62017-08-08 11:15:25 +0530945 0,
946 usecase->stream.inout->out_config.format,
947 usecase->stream.inout->out_config.sample_rate,
948 usecase->stream.inout->out_config.bit_width,
949 usecase->stream.inout->out_config.channel_mask,
950 usecase->stream.inout->profile,
951 &usecase->stream.inout->out_app_type_cfg);
952 ALOGV("%s Selected apptype: %d", __func__, usecase->stream.inout->out_app_type_cfg.app_type);
953 break;
Derek Chenf7092792017-05-23 12:23:53 -0400954 case PCM_HFP_CALL:
955 switch (usecase->id) {
956 case USECASE_AUDIO_HFP_SCO:
957 case USECASE_AUDIO_HFP_SCO_WB:
958 audio_extn_btsco_get_sample_rate(usecase->out_snd_device,
959 &usecase->out_app_type_cfg.sample_rate);
Derek Chend7307012019-11-12 12:10:41 -0500960 usecase->in_app_type_cfg.sample_rate = usecase->out_app_type_cfg.sample_rate;
Derek Chenf7092792017-05-23 12:23:53 -0400961 break;
962 case USECASE_AUDIO_HFP_SCO_DOWNLINK:
963 case USECASE_AUDIO_HFP_SCO_WB_DOWNLINK:
964 audio_extn_btsco_get_sample_rate(usecase->in_snd_device,
965 &usecase->in_app_type_cfg.sample_rate);
Derek Chend7307012019-11-12 12:10:41 -0500966 usecase->out_app_type_cfg.sample_rate = usecase->in_app_type_cfg.sample_rate;
Derek Chenf7092792017-05-23 12:23:53 -0400967 break;
968 default:
969 ALOGE("%s: usecase id (%d) not supported, use default sample rate",
970 __func__, usecase->id);
971 usecase->in_app_type_cfg.sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
972 usecase->out_app_type_cfg.sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
973 break;
974 }
975 /* update out_app_type_cfg */
976 usecase->out_app_type_cfg.bit_width =
977 platform_get_snd_device_bit_width(usecase->out_snd_device);
978 usecase->out_app_type_cfg.app_type =
979 platform_get_default_app_type_v2(adev->platform, PCM_PLAYBACK);
980 /* update in_app_type_cfg */
981 usecase->in_app_type_cfg.bit_width =
982 platform_get_snd_device_bit_width(usecase->in_snd_device);
983 usecase->in_app_type_cfg.app_type =
984 platform_get_default_app_type_v2(adev->platform, PCM_CAPTURE);
985 ALOGV("%s Selected apptype: playback %d capture %d",
986 __func__, usecase->out_app_type_cfg.app_type, usecase->in_app_type_cfg.app_type);
987 break;
Derek Chena30a5f42019-12-03 11:17:09 -0500988 case ICC_CALL:
989 /* ICC usecase: Loopback from TERT_TDM_TX to TERT_TDM_RX */
990 /* update out_app_type_cfg */
991 usecase->out_app_type_cfg.sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
992 usecase->out_app_type_cfg.bit_width = platform_get_snd_device_bit_width(usecase->out_snd_device);
993 usecase->out_app_type_cfg.app_type = platform_get_default_app_type_v2(adev->platform, PCM_PLAYBACK);
994 /* update in_app_type_cfg */
995 usecase->in_app_type_cfg.sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
996 usecase->in_app_type_cfg.bit_width = platform_get_snd_device_bit_width(usecase->in_snd_device);
997 usecase->in_app_type_cfg.app_type = platform_get_default_app_type_v2(adev->platform, PCM_CAPTURE);
998
999 ALOGV("%s Selected apptype: playback %d capture %d",
1000 __func__, usecase->out_app_type_cfg.app_type, usecase->in_app_type_cfg.app_type);
1001 break;
Fei Tongaffdf732020-02-20 20:39:05 +08001002 case SYNTH_LOOPBACK:
1003 /* update out_app_type_cfg */
1004 usecase->out_app_type_cfg.sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
1005 usecase->out_app_type_cfg.bit_width =
1006 platform_get_snd_device_bit_width(usecase->out_snd_device);
1007 usecase->out_app_type_cfg.app_type =
1008 platform_get_default_app_type_v2(adev->platform, PCM_PLAYBACK);
1009 /* update in_app_type_cfg */
1010 usecase->in_app_type_cfg.sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
1011 usecase->in_app_type_cfg.bit_width =
1012 platform_get_snd_device_bit_width(usecase->in_snd_device);
1013 usecase->in_app_type_cfg.app_type =
1014 platform_get_default_app_type_v2(adev->platform, PCM_CAPTURE);
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;
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301018 default:
1019 ALOGE("%s: app type cfg not supported for usecase type (%d)",
1020 __func__, usecase->type);
1021 }
1022}
1023
Aalique Grahame22e49102018-12-18 14:23:57 -08001024static int set_stream_app_type_mixer_ctrl(struct audio_device *adev,
1025 int pcm_device_id, int app_type,
1026 int acdb_dev_id, int sample_rate,
1027 int stream_type,
1028 snd_device_t snd_device)
1029{
1030
1031 char mixer_ctl_name[MAX_LENGTH_MIXER_CONTROL_IN_INT];
1032 struct mixer_ctl *ctl;
1033 int app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT], len = 0, rc = 0;
1034 int snd_device_be_idx = -1;
1035
1036 if (stream_type == PCM_PLAYBACK) {
1037 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
1038 "Audio Stream %d App Type Cfg", pcm_device_id);
1039 } else if (stream_type == PCM_CAPTURE) {
1040 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
1041 "Audio Stream Capture %d App Type Cfg", pcm_device_id);
1042 }
1043
1044 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
1045 if (!ctl) {
1046 ALOGE("%s: Could not get ctl for mixer cmd - %s",
1047 __func__, mixer_ctl_name);
1048 rc = -EINVAL;
1049 goto exit;
1050 }
1051 app_type_cfg[len++] = app_type;
1052 app_type_cfg[len++] = acdb_dev_id;
1053 app_type_cfg[len++] = sample_rate;
1054
1055 snd_device_be_idx = platform_get_snd_device_backend_index(snd_device);
1056 if (snd_device_be_idx > 0)
1057 app_type_cfg[len++] = snd_device_be_idx;
1058 ALOGV("%s: stream type %d app_type %d, acdb_dev_id %d "
1059 "sample rate %d, snd_device_be_idx %d",
1060 __func__, stream_type, app_type, acdb_dev_id, sample_rate,
1061 snd_device_be_idx);
1062 mixer_ctl_set_array(ctl, app_type_cfg, len);
1063
1064exit:
1065 return rc;
1066}
1067
1068static int audio_extn_utils_send_app_type_cfg_hfp(struct audio_device *adev,
1069 struct audio_usecase *usecase)
1070{
1071 int pcm_device_id, acdb_dev_id = 0, snd_device = usecase->out_snd_device;
1072 int32_t sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
1073 int app_type = 0, rc = 0;
Derek Chenf7092792017-05-23 12:23:53 -04001074 bool is_bus_dev_usecase = false;
Aalique Grahame22e49102018-12-18 14:23:57 -08001075
1076 ALOGV("%s", __func__);
1077
1078 if (usecase->type != PCM_HFP_CALL) {
Derek Chenf7092792017-05-23 12:23:53 -04001079 ALOGV("%s: not a HFP path, no need to cfg app type", __func__);
Aalique Grahame22e49102018-12-18 14:23:57 -08001080 rc = 0;
1081 goto exit_send_app_type_cfg;
1082 }
1083 if ((usecase->id != USECASE_AUDIO_HFP_SCO) &&
Derek Chenf7092792017-05-23 12:23:53 -04001084 (usecase->id != USECASE_AUDIO_HFP_SCO_WB) &&
1085 (usecase->id != USECASE_AUDIO_HFP_SCO_DOWNLINK) &&
1086 (usecase->id != USECASE_AUDIO_HFP_SCO_WB_DOWNLINK)) {
1087 ALOGV("%s: a usecase where app type cfg is not required", __func__);
Aalique Grahame22e49102018-12-18 14:23:57 -08001088 rc = 0;
1089 goto exit_send_app_type_cfg;
1090 }
1091
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08001092 if (compare_device_type(&usecase->device_list, AUDIO_DEVICE_OUT_BUS))
Derek Chenf7092792017-05-23 12:23:53 -04001093 is_bus_dev_usecase = true;
1094
Aalique Grahame22e49102018-12-18 14:23:57 -08001095 snd_device = usecase->out_snd_device;
1096 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_PLAYBACK);
1097
1098 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
1099 if (acdb_dev_id < 0) {
1100 ALOGE("%s: Couldn't get the acdb dev id", __func__);
1101 rc = -EINVAL;
1102 goto exit_send_app_type_cfg;
1103 }
1104
sriram kumar53c4c972021-02-18 16:49:33 +05301105 /*
1106 * Value of afe_loopback gets read based on the property defined in
1107 * audio_platform_info.xml. If afe loopback is set then do not execute
1108 * session 1 path as app type mixer control will not be created for
1109 * afe loopback
1110 */
1111
Aalique Grahame22e49102018-12-18 14:23:57 -08001112 if (usecase->type == PCM_HFP_CALL) {
sriram kumar53c4c972021-02-18 16:49:33 +05301113 if (!(platform_get_is_afe_loopback_enabled(adev->platform))) {
1114 /* config HFP session:1 playback path */
1115 if (is_bus_dev_usecase) {
1116 app_type = usecase->out_app_type_cfg.app_type;
1117 sample_rate= usecase->out_app_type_cfg.sample_rate;
1118 } else {
1119 snd_device = SND_DEVICE_NONE; // use legacy behavior
1120 app_type = platform_get_default_app_type_v2(adev->platform, PCM_PLAYBACK);
1121 sample_rate= CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
Derek Chenf7092792017-05-23 12:23:53 -04001122 }
sriram kumar53c4c972021-02-18 16:49:33 +05301123 rc = set_stream_app_type_mixer_ctrl(adev, pcm_device_id, app_type,
1124 acdb_dev_id, sample_rate,
1125 PCM_PLAYBACK,
1126 snd_device);
1127 if (rc < 0)
1128 goto exit_send_app_type_cfg;
1129
1130 /* config HFP session:1 capture path */
1131 if (is_bus_dev_usecase) {
1132 snd_device = usecase->in_snd_device;
1133 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_CAPTURE);
1134 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
1135 if (acdb_dev_id < 0) {
1136 ALOGE("%s: Couldn't get the acdb dev id", __func__);
1137 rc = -EINVAL;
1138 goto exit_send_app_type_cfg;
1139 }
1140 app_type = usecase->in_app_type_cfg.app_type;
1141 sample_rate= usecase->in_app_type_cfg.sample_rate;
1142 } else {
1143 snd_device = SND_DEVICE_NONE; // use legacy behavior
1144 app_type = platform_get_default_app_type_v2(adev->platform, PCM_CAPTURE);
1145 }
1146 rc = set_stream_app_type_mixer_ctrl(adev, pcm_device_id, app_type,
1147 acdb_dev_id, sample_rate,
1148 PCM_CAPTURE,
1149 snd_device);
1150 if (rc < 0)
1151 goto exit_send_app_type_cfg;
1152
1153 if (is_bus_dev_usecase)
1154 goto exit_send_app_type_cfg;
Derek Chenf7092792017-05-23 12:23:53 -04001155 }
Aalique Grahame22e49102018-12-18 14:23:57 -08001156 /* config HFP session:2 capture path */
1157 pcm_device_id = HFP_ASM_RX_TX;
1158 snd_device = usecase->in_snd_device;
1159 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
1160 if (acdb_dev_id <= 0) {
1161 ALOGE("%s: Couldn't get the acdb dev id", __func__);
1162 rc = -EINVAL;
1163 goto exit_send_app_type_cfg;
1164 }
1165 app_type = platform_get_default_app_type_v2(adev->platform, PCM_CAPTURE);
1166 rc = set_stream_app_type_mixer_ctrl(adev, pcm_device_id, app_type,
1167 acdb_dev_id, sample_rate, PCM_CAPTURE,
sriram kumar53c4c972021-02-18 16:49:33 +05301168 usecase->in_snd_device);
Aalique Grahame22e49102018-12-18 14:23:57 -08001169 if (rc < 0)
1170 goto exit_send_app_type_cfg;
1171
1172 /* config HFP session:2 playback path */
1173 app_type = platform_get_default_app_type_v2(adev->platform, PCM_PLAYBACK);
1174 rc = set_stream_app_type_mixer_ctrl(adev, pcm_device_id, app_type,
1175 acdb_dev_id, sample_rate,
sriram kumar53c4c972021-02-18 16:49:33 +05301176 PCM_PLAYBACK, usecase->out_snd_device);
Aalique Grahame22e49102018-12-18 14:23:57 -08001177 if (rc < 0)
1178 goto exit_send_app_type_cfg;
1179 }
1180
1181 rc = 0;
1182exit_send_app_type_cfg:
1183 return rc;
1184}
1185
Derek Chena30a5f42019-12-03 11:17:09 -05001186int audio_extn_utils_send_app_type_cfg_icc(struct audio_device *adev,
1187 struct audio_usecase *usecase)
1188{
1189 int pcm_device_id, acdb_dev_id = 0, snd_device = usecase->out_snd_device;
1190 int32_t sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
1191 int app_type = 0, rc = 0;
1192
1193 ALOGV("%s", __func__);
1194
1195 if (usecase->type != ICC_CALL) {
1196 ALOGV("%s: not an ICC path, no need to cfg app type", __func__);
1197 rc = 0;
1198 goto exit_send_app_type_cfg;
1199 }
1200 if (usecase->id != USECASE_ICC_CALL) {
1201 ALOGV("%s: a usecase where app type cfg is not required", __func__);
1202 rc = 0;
1203 goto exit_send_app_type_cfg;
1204 }
1205
1206 snd_device = usecase->out_snd_device;
1207 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_PLAYBACK);
1208 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
1209 if (acdb_dev_id < 0) {
1210 ALOGE("%s: Couldn't get the acdb dev id", __func__);
1211 rc = -EINVAL;
1212 goto exit_send_app_type_cfg;
1213 }
1214 /* config ICC session: playback path */
1215 app_type = usecase->out_app_type_cfg.app_type;
1216 sample_rate= usecase->out_app_type_cfg.sample_rate;
1217
1218 rc = set_stream_app_type_mixer_ctrl(adev, pcm_device_id, app_type,
1219 acdb_dev_id, sample_rate,
1220 PCM_PLAYBACK,
1221 snd_device);
1222 if (rc < 0)
1223 goto exit_send_app_type_cfg;
1224
1225 /* config ICC session: capture path */
1226 snd_device = usecase->in_snd_device;
1227 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_CAPTURE);
1228 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
1229 if (acdb_dev_id < 0) {
1230 ALOGE("%s: Couldn't get the acdb dev id", __func__);
1231 rc = -EINVAL;
1232 goto exit_send_app_type_cfg;
1233 }
1234 app_type = usecase->in_app_type_cfg.app_type;
1235 sample_rate= usecase->in_app_type_cfg.sample_rate;
1236 rc = set_stream_app_type_mixer_ctrl(adev, pcm_device_id, app_type,
1237 acdb_dev_id, sample_rate,
1238 PCM_CAPTURE,
1239 snd_device);
1240exit_send_app_type_cfg:
1241 return rc;
1242}
1243
Fei Tongaffdf732020-02-20 20:39:05 +08001244static int audio_extn_utils_send_app_type_cfg_synth(struct audio_device *adev,
1245 struct audio_usecase *usecase)
1246{
1247 int pcm_device_id, acdb_dev_id = 0, snd_device = usecase->out_snd_device;
1248 int32_t sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
1249 int app_type = 0, rc = 0;
1250 bool is_bus_dev_usecase = false;
1251
1252 ALOGV("%s", __func__);
1253
1254 if (usecase->type != SYNTH_LOOPBACK) {
1255 ALOGV("%s: not a SYNTH path, no need to cfg app type", __func__);
1256 rc = 0;
1257 goto exit_send_app_type_cfg;
1258 }
1259 if (usecase->id != USECASE_AUDIO_PLAYBACK_SYNTHESIZER) {
1260 ALOGV("%s: a usecase where app type cfg is not required", __func__);
1261 rc = 0;
1262 goto exit_send_app_type_cfg;
1263 }
1264
1265 if (compare_device_type(&usecase->device_list, AUDIO_DEVICE_OUT_BUS)) {
1266 is_bus_dev_usecase = true;
1267 }
1268
1269 snd_device = usecase->out_snd_device;
1270 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_PLAYBACK);
1271
1272 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
1273 if (acdb_dev_id < 0) {
1274 ALOGE("%s: Couldn't get the acdb dev id", __func__);
1275 rc = -EINVAL;
1276 goto exit_send_app_type_cfg;
1277 }
1278
1279 if (usecase->type == SYNTH_LOOPBACK) {
1280 /* config SYNTH session: playback path */
1281 if (is_bus_dev_usecase) {
1282 app_type = usecase->out_app_type_cfg.app_type;
1283 sample_rate= usecase->out_app_type_cfg.sample_rate;
1284 } else {
1285 snd_device = SND_DEVICE_NONE; // use legacy behavior
1286 app_type = platform_get_default_app_type_v2(adev->platform, PCM_PLAYBACK);
1287 sample_rate= CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
1288 }
1289 rc = set_stream_app_type_mixer_ctrl(adev, pcm_device_id, app_type,
1290 acdb_dev_id, sample_rate,
1291 PCM_PLAYBACK,
1292 snd_device);
1293 if (rc < 0)
1294 goto exit_send_app_type_cfg;
1295 }
1296
1297 rc = 0;
1298exit_send_app_type_cfg:
1299 return rc;
1300}
1301
Zhou Song06761dd2019-04-28 18:08:17 +08001302int audio_extn_utils_get_app_sample_rate_for_device(
1303 struct audio_device *adev,
1304 struct audio_usecase *usecase, int snd_device)
1305{
1306 char value[PROPERTY_VALUE_MAX] = {0};
1307 int sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
1308
1309 if ((usecase->type == PCM_PLAYBACK) && (usecase->stream.out != NULL)) {
1310 property_get("vendor.audio.playback.mch.downsample",value,"");
1311 if (!strncmp("true", value, sizeof("true"))) {
1312 if ((popcount(usecase->stream.out->channel_mask) > 2) &&
1313 (usecase->stream.out->app_type_cfg.sample_rate > CODEC_BACKEND_DEFAULT_SAMPLE_RATE) &&
1314 !(usecase->stream.out->flags &
1315 (audio_output_flags_t)AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH))
1316 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
1317 }
1318
1319 if (usecase->id == USECASE_AUDIO_PLAYBACK_VOIP) {
1320 usecase->stream.out->app_type_cfg.sample_rate = usecase->stream.out->sample_rate;
Zhou Song06761dd2019-04-28 18:08:17 +08001321 } else if ((snd_device == SND_DEVICE_OUT_HDMI ||
1322 snd_device == SND_DEVICE_OUT_USB_HEADSET ||
1323 snd_device == SND_DEVICE_OUT_DISPLAY_PORT) &&
1324 (usecase->stream.out->sample_rate >= OUTPUT_SAMPLING_RATE_44100)) {
1325 /*
1326 * To best utlize DSP, check if the stream sample rate is supported/multiple of
1327 * configured device sample rate, if not update the COPP rate to be equal to the
1328 * device sample rate, else open COPP at stream sample rate
1329 */
1330 platform_check_and_update_copp_sample_rate(adev->platform, snd_device,
1331 usecase->stream.out->sample_rate,
1332 &usecase->stream.out->app_type_cfg.sample_rate);
Lakshman Chaluvaraju5d0894e2021-01-27 23:22:58 +05301333 } else if (snd_device == SND_DEVICE_OUT_BT_A2DP) {
Zhou Song06761dd2019-04-28 18:08:17 +08001334 /*
1335 * For a2dp playback get encoder sampling rate and set copp sampling rate,
1336 * for bit width use the stream param only.
1337 */
1338 audio_extn_a2dp_get_enc_sample_rate(&usecase->stream.out->app_type_cfg.sample_rate);
1339 ALOGI("%s using %d sample rate rate for A2DP CoPP",
1340 __func__, usecase->stream.out->app_type_cfg.sample_rate);
sriram kumar68e21132021-06-25 19:27:20 +05301341 } else if ((((snd_device != SND_DEVICE_OUT_HEADPHONES_44_1 &&
1342 !audio_is_this_native_usecase(usecase)) &&
1343 usecase->stream.out->sample_rate == OUTPUT_SAMPLING_RATE_44100) ||
1344 (usecase->stream.out->sample_rate < OUTPUT_SAMPLING_RATE_44100)) ||
1345 (compare_device_type(&usecase->stream.out->device_list,AUDIO_DEVICE_OUT_SPEAKER))) {
1346 /* Reset to default if no native stream is active or default device is speaker*/
Lakshman Chaluvaraju5d0894e2021-01-27 23:22:58 +05301347 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Zhou Song06761dd2019-04-28 18:08:17 +08001348 }
1349 audio_extn_btsco_get_sample_rate(snd_device, &usecase->stream.out->app_type_cfg.sample_rate);
1350 sample_rate = usecase->stream.out->app_type_cfg.sample_rate;
1351
1352 if (((usecase->stream.out->format == AUDIO_FORMAT_E_AC3) ||
1353 (usecase->stream.out->format == AUDIO_FORMAT_E_AC3_JOC) ||
1354 (usecase->stream.out->format == AUDIO_FORMAT_DOLBY_TRUEHD))
1355 && audio_extn_passthru_is_passthrough_stream(usecase->stream.out)
1356 && !audio_extn_passthru_is_convert_supported(adev, usecase->stream.out)) {
1357 sample_rate = sample_rate * 4;
1358 if (sample_rate > HDMI_PASSTHROUGH_MAX_SAMPLE_RATE)
1359 sample_rate = HDMI_PASSTHROUGH_MAX_SAMPLE_RATE;
1360 }
1361 } else if (usecase->type == PCM_CAPTURE) {
1362 if (usecase->stream.in != NULL) {
Revathi Uddarajud9f23d92020-07-27 10:55:06 +05301363 if (usecase->id == USECASE_AUDIO_RECORD_VOIP
1364 || usecase->id == USECASE_AUDIO_RECORD_VOIP_LOW_LATENCY)
Zhou Song06761dd2019-04-28 18:08:17 +08001365 usecase->stream.in->app_type_cfg.sample_rate = usecase->stream.in->sample_rate;
1366 if (voice_is_in_call_rec_stream(usecase->stream.in)) {
1367 audio_extn_btsco_get_sample_rate(usecase->in_snd_device,
1368 &usecase->stream.in->app_type_cfg.sample_rate);
Zhou Songab94f062021-05-12 13:11:17 +08001369 } if (SND_DEVICE_IN_BT_A2DP == snd_device) {
1370 audio_extn_a2dp_get_dec_sample_rate(&usecase->stream.in->app_type_cfg.sample_rate);
Zhou Song06761dd2019-04-28 18:08:17 +08001371 } else {
1372 audio_extn_btsco_get_sample_rate(snd_device,
1373 &usecase->stream.in->app_type_cfg.sample_rate);
1374 }
1375 sample_rate = usecase->stream.in->app_type_cfg.sample_rate;
1376 } else if (usecase->id == USECASE_AUDIO_SPKR_CALIB_TX) {
Sujin Panickerb6d76262019-09-09 10:55:38 +05301377 if ((property_get("vendor.audio.spkr_prot.tx.sampling_rate", value, NULL) > 0))
1378 sample_rate = atoi(value);
1379 else
1380 sample_rate = SAMPLE_RATE_8000;
Zhou Song06761dd2019-04-28 18:08:17 +08001381 }
1382 } else if (usecase->type == TRANSCODE_LOOPBACK_RX) {
1383 sample_rate = usecase->stream.inout->out_config.sample_rate;
1384 }
1385 return sample_rate;
1386}
1387
Siena Richard7c2db772016-12-21 11:32:34 -08001388static int send_app_type_cfg_for_device(struct audio_device *adev,
1389 struct audio_usecase *usecase,
1390 int split_snd_device)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001391{
1392 char mixer_ctl_name[MAX_LENGTH_MIXER_CONTROL_IN_INT];
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301393 size_t app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT] = {0};
1394 int len = 0, rc;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001395 struct mixer_ctl *ctl;
Siena Richard7c2db772016-12-21 11:32:34 -08001396 int pcm_device_id = 0, acdb_dev_id, app_type;
1397 int snd_device = split_snd_device, snd_device_be_idx = -1;
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +05301398 int32_t sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Varun Balaraje49253e2017-07-06 19:48:56 +05301399 struct streams_io_cfg *s_info = NULL;
1400 struct listnode *node = NULL;
Nikhil Laturkarac4a7492017-08-31 18:27:19 +05301401 int bd_app_type = 0;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001402
Siena Richard7c2db772016-12-21 11:32:34 -08001403 ALOGV("%s: usecase->out_snd_device %s, usecase->in_snd_device %s, split_snd_device %s",
1404 __func__, platform_get_snd_device_name(usecase->out_snd_device),
1405 platform_get_snd_device_name(usecase->in_snd_device),
1406 platform_get_snd_device_name(split_snd_device));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001407
Siddartha Shaik343abc62017-08-08 11:15:25 +05301408 if (usecase->type != PCM_PLAYBACK && usecase->type != PCM_CAPTURE &&
Surendar Karka93cd25a2018-08-28 14:21:37 +05301409 usecase->type != TRANSCODE_LOOPBACK_RX) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301410 ALOGE("%s: not a playback/capture path, no need to cfg app type", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001411 rc = 0;
1412 goto exit_send_app_type_cfg;
1413 }
1414 if ((usecase->id != USECASE_AUDIO_PLAYBACK_DEEP_BUFFER) &&
Vignesh Kulothungan0a2eff02019-07-11 16:30:13 -07001415 (usecase->id != USECASE_AUDIO_PLAYBACK_WITH_HAPTICS) &&
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001416 (usecase->id != USECASE_AUDIO_PLAYBACK_LOW_LATENCY) &&
1417 (usecase->id != USECASE_AUDIO_PLAYBACK_MULTI_CH) &&
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -08001418 (usecase->id != USECASE_AUDIO_PLAYBACK_ULL) &&
Vikram Panduranga93f080e2017-06-07 18:16:14 -07001419 (usecase->id != USECASE_AUDIO_PLAYBACK_VOIP) &&
Surendar Karka93cd25a2018-08-28 14:21:37 +05301420 (usecase->id != USECASE_AUDIO_TRANSCODE_LOOPBACK_RX) &&
Varun Balaraje49253e2017-07-06 19:48:56 +05301421 (!is_interactive_usecase(usecase->id)) &&
Srikanth Uyyala9d551402015-08-25 16:03:42 +05301422 (!is_offload_usecase(usecase->id)) &&
Derek Chenf6318be2017-06-12 17:16:24 -04001423 (usecase->type != PCM_CAPTURE) &&
1424 (!audio_extn_auto_hal_is_bus_device_usecase(usecase->id))) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301425 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 -07001426 rc = 0;
1427 goto exit_send_app_type_cfg;
1428 }
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -08001429
1430 //if VR is active then only send the mixer control
1431 if (usecase->id == USECASE_AUDIO_PLAYBACK_ULL && !audio_is_vr_mode_on(adev)) {
1432 ALOGI("ULL doesnt need sending app type cfg, returning");
1433 rc = 0;
1434 goto exit_send_app_type_cfg;
1435 }
1436
Surendar Karka93cd25a2018-08-28 14:21:37 +05301437 if (usecase->type == PCM_PLAYBACK || usecase->type == TRANSCODE_LOOPBACK_RX) {
Ben Romberger1fafdde2015-09-09 19:43:15 -07001438 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_PLAYBACK);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301439 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
1440 "Audio Stream %d App Type Cfg", pcm_device_id);
Ben Romberger1fafdde2015-09-09 19:43:15 -07001441 } else if (usecase->type == PCM_CAPTURE) {
Ben Romberger1fafdde2015-09-09 19:43:15 -07001442 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_CAPTURE);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301443 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
1444 "Audio Stream Capture %d App Type Cfg", pcm_device_id);
Srikanth Uyyala9d551402015-08-25 16:03:42 +05301445 }
Siena Richard7c2db772016-12-21 11:32:34 -08001446
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001447 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
1448 if (!ctl) {
1449 ALOGE("%s: Could not get ctl for mixer cmd - %s", __func__,
1450 mixer_ctl_name);
1451 rc = -EINVAL;
1452 goto exit_send_app_type_cfg;
1453 }
Aditya Bavanari701a6992017-03-30 19:17:16 +05301454 snd_device = platform_get_spkr_prot_snd_device(snd_device);
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301455 if (voice_is_in_call_rec_stream(usecase->stream.in) && usecase->type == PCM_CAPTURE) {
1456 snd_device_t voice_device = voice_get_incall_rec_snd_device(usecase->in_snd_device);
1457 acdb_dev_id = platform_get_snd_device_acdb_id(voice_device);
1458 ALOGV("acdb id for voice call use case %d", acdb_dev_id);
1459 } else {
1460 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
1461 }
Rohit Kumar1181b292017-01-31 18:07:17 +05301462 if (acdb_dev_id <= 0) {
1463 ALOGE("%s: Couldn't get the acdb dev id", __func__);
1464 rc = -EINVAL;
1465 goto exit_send_app_type_cfg;
1466 }
1467
Siena Richard7c2db772016-12-21 11:32:34 -08001468 snd_device_be_idx = platform_get_snd_device_backend_index(snd_device);
1469 if (snd_device_be_idx < 0) {
1470 ALOGE("%s: Couldn't get the backend index for snd device %s ret=%d",
1471 __func__, platform_get_snd_device_name(snd_device),
1472 snd_device_be_idx);
1473 }
1474
Zhou Song06761dd2019-04-28 18:08:17 +08001475 sample_rate = audio_extn_utils_get_app_sample_rate_for_device(adev, usecase, snd_device);
1476
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301477 if ((usecase->type == PCM_PLAYBACK) && (usecase->stream.out != NULL)) {
Varun Balaraje49253e2017-07-06 19:48:56 +05301478 /* Interactive streams are supported with only direct app type id.
1479 * Get Direct profile app type and use it for interactive streams
1480 */
1481 list_for_each(node, &adev->streams_output_cfg_list) {
1482 s_info = node_to_item(node, struct streams_io_cfg, list);
Aalique Grahame5cd12ff2017-10-19 10:57:00 -07001483 if (s_info->flags.out_flags == (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_BD |
Nikhil Laturkarac4a7492017-08-31 18:27:19 +05301484 AUDIO_OUTPUT_FLAG_DIRECT_PCM |
1485 AUDIO_OUTPUT_FLAG_DIRECT))
1486 bd_app_type = s_info->app_type_cfg.app_type;
Varun Balaraje49253e2017-07-06 19:48:56 +05301487 }
Aalique Grahame5cd12ff2017-10-19 10:57:00 -07001488 if (usecase->stream.out->flags == (audio_output_flags_t)AUDIO_OUTPUT_FLAG_INTERACTIVE)
Nikhil Laturkarac4a7492017-08-31 18:27:19 +05301489 app_type = bd_app_type;
Varun Balaraje49253e2017-07-06 19:48:56 +05301490 else
1491 app_type = usecase->stream.out->app_type_cfg.app_type;
Siena Richard7c2db772016-12-21 11:32:34 -08001492 app_type_cfg[len++] = app_type;
Mingming Yin21854652016-04-13 11:54:02 -07001493 app_type_cfg[len++] = acdb_dev_id;
Naresh Tanniru3a406772017-05-10 13:09:05 -07001494 app_type_cfg[len++] = sample_rate;
1495
Siena Richard7c2db772016-12-21 11:32:34 -08001496 if (snd_device_be_idx > 0)
1497 app_type_cfg[len++] = snd_device_be_idx;
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301498
Siena Richard7c2db772016-12-21 11:32:34 -08001499 ALOGI("%s PLAYBACK app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
1500 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301501
1502 } else if ((usecase->type == PCM_CAPTURE) && (usecase->stream.in != NULL)) {
Siena Richard7c2db772016-12-21 11:32:34 -08001503 app_type = usecase->stream.in->app_type_cfg.app_type;
1504 app_type_cfg[len++] = app_type;
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301505 app_type_cfg[len++] = acdb_dev_id;
Siena Richard7c2db772016-12-21 11:32:34 -08001506 app_type_cfg[len++] = sample_rate;
1507 if (snd_device_be_idx > 0)
1508 app_type_cfg[len++] = snd_device_be_idx;
1509 ALOGI("%s CAPTURE app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
1510 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301511 } else {
Siena Richard7c2db772016-12-21 11:32:34 -08001512 app_type = platform_get_default_app_type_v2(adev->platform, usecase->type);
Surendar Karka93cd25a2018-08-28 14:21:37 +05301513 if(usecase->type == TRANSCODE_LOOPBACK_RX) {
Siddartha Shaik343abc62017-08-08 11:15:25 +05301514 app_type = usecase->stream.inout->out_app_type_cfg.app_type;
1515 }
Siena Richard7c2db772016-12-21 11:32:34 -08001516 app_type_cfg[len++] = app_type;
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301517 app_type_cfg[len++] = acdb_dev_id;
1518 app_type_cfg[len++] = sample_rate;
Siena Richard7c2db772016-12-21 11:32:34 -08001519 if (snd_device_be_idx > 0)
1520 app_type_cfg[len++] = snd_device_be_idx;
1521 ALOGI("%s default app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
1522 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Ashish Jainc02430d2016-01-04 10:42:43 +05301523 }
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301524
Siddartha Shaik343abc62017-08-08 11:15:25 +05301525 if(ctl)
1526 mixer_ctl_set_array(ctl, app_type_cfg, len);
Zhou Songd9bd9302020-08-04 16:34:45 +08001527
1528 /* send app type cfg for haptics */
1529 if (usecase->id == USECASE_AUDIO_PLAYBACK_WITH_HAPTICS) {
1530 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
1531 "Audio Stream %d App Type Cfg", adev->haptic_pcm_device_id );
1532 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
1533 if (!ctl) {
1534 ALOGE("%s: Could not get ctl for mixer cmd - %s", __func__,
1535 mixer_ctl_name);
1536 rc = -EINVAL;
1537 goto exit_send_app_type_cfg;
1538 }
1539 acdb_dev_id = platform_get_snd_device_acdb_id(SND_DEVICE_OUT_HAPTICS);
1540 snd_device_be_idx = platform_get_snd_device_backend_index(SND_DEVICE_OUT_HAPTICS);
1541 /* haptics acdb id */
1542 app_type_cfg[1] = acdb_dev_id;
1543 /* haptics be index */
1544 app_type_cfg[3] = snd_device_be_idx;
1545 mixer_ctl_set_array(ctl, app_type_cfg, len);
1546 }
1547
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001548 rc = 0;
1549exit_send_app_type_cfg:
1550 return rc;
1551}
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001552
Deeraj Soman14230922019-01-30 16:39:30 +05301553static int audio_extn_utils_check_input_parameters(uint32_t sample_rate,
1554 audio_format_t format,
1555 int channel_count)
1556{
1557 int ret = 0;
1558
1559 if (((format != AUDIO_FORMAT_PCM_16_BIT) && (format != AUDIO_FORMAT_PCM_8_24_BIT) &&
1560 (format != AUDIO_FORMAT_PCM_24_BIT_PACKED) && (format != AUDIO_FORMAT_PCM_32_BIT) &&
1561 (format != AUDIO_FORMAT_PCM_FLOAT)) &&
1562 !voice_extn_compress_voip_is_format_supported(format) &&
1563 !audio_extn_compr_cap_format_supported(format) &&
1564 !audio_extn_cin_format_supported(format))
1565 ret = -EINVAL;
1566
1567 switch (channel_count) {
1568 case 1:
1569 case 2:
1570 case 3:
1571 case 4:
1572 case 6:
1573 case 8:
1574 break;
1575 default:
1576 ret = -EINVAL;
1577 }
1578
1579 switch (sample_rate) {
1580 case 8000:
1581 case 11025:
1582 case 12000:
1583 case 16000:
1584 case 22050:
1585 case 24000:
1586 case 32000:
1587 case 44100:
1588 case 48000:
1589 case 88200:
1590 case 96000:
1591 case 176400:
1592 case 192000:
1593 break;
1594 default:
1595 ret = -EINVAL;
1596 }
1597
1598 return ret;
1599}
1600
1601static inline uint32_t audio_extn_utils_nearest_multiple(uint32_t num, uint32_t multiplier)
1602{
1603 uint32_t remainder = 0;
1604
1605 if (!multiplier)
1606 return num;
1607
1608 remainder = num % multiplier;
1609 if (remainder)
1610 num += (multiplier - remainder);
1611
1612 return num;
1613}
1614
1615static inline uint32_t audio_extn_utils_lcm(uint32_t num1, uint32_t num2)
1616{
1617 uint32_t high = num1, low = num2, temp = 0;
1618
1619 if (!num1 || !num2)
1620 return 0;
1621
1622 if (num1 < num2) {
1623 high = num2;
1624 low = num1;
1625 }
1626
1627 while (low != 0) {
1628 temp = low;
1629 low = high % low;
1630 high = temp;
1631 }
1632 return (num1 * num2)/high;
1633}
1634
Siena Richard7c2db772016-12-21 11:32:34 -08001635int audio_extn_utils_send_app_type_cfg(struct audio_device *adev,
1636 struct audio_usecase *usecase)
1637{
1638 int i, num_devices = 0;
1639 snd_device_t new_snd_devices[SND_DEVICE_OUT_END] = {0};
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301640 snd_device_t in_snd_device = usecase->in_snd_device;
Siena Richard7c2db772016-12-21 11:32:34 -08001641 int rc = 0;
1642
Aalique Grahame22e49102018-12-18 14:23:57 -08001643 if (usecase->type == PCM_HFP_CALL) {
1644 return audio_extn_utils_send_app_type_cfg_hfp(adev, usecase);
Derek Chena30a5f42019-12-03 11:17:09 -05001645 } else if (usecase->type == ICC_CALL) {
1646 return audio_extn_utils_send_app_type_cfg_icc(adev, usecase);
Fei Tongaffdf732020-02-20 20:39:05 +08001647 } else if (usecase->type == SYNTH_LOOPBACK) {
1648 return audio_extn_utils_send_app_type_cfg_synth(adev, usecase);
Aalique Grahame22e49102018-12-18 14:23:57 -08001649 }
1650
Siena Richard7c2db772016-12-21 11:32:34 -08001651 switch (usecase->type) {
1652 case PCM_PLAYBACK:
Surendar Karka93cd25a2018-08-28 14:21:37 +05301653 case TRANSCODE_LOOPBACK_RX:
Siena Richard7c2db772016-12-21 11:32:34 -08001654 ALOGD("%s: usecase->out_snd_device %s",
1655 __func__, platform_get_snd_device_name(usecase->out_snd_device));
1656 /* check for out combo device */
1657 if (platform_split_snd_device(adev->platform,
1658 usecase->out_snd_device,
1659 &num_devices, new_snd_devices)) {
1660 new_snd_devices[0] = usecase->out_snd_device;
1661 num_devices = 1;
1662 }
1663 break;
1664 case PCM_CAPTURE:
1665 ALOGD("%s: usecase->in_snd_device %s",
1666 __func__, platform_get_snd_device_name(usecase->in_snd_device));
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301667 if (voice_is_in_call_rec_stream(usecase->stream.in)) {
1668 in_snd_device = voice_get_incall_rec_backend_device(usecase->stream.in);
1669 }
Siena Richard7c2db772016-12-21 11:32:34 -08001670 /* check for in combo device */
1671 if (platform_split_snd_device(adev->platform,
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301672 in_snd_device,
Siena Richard7c2db772016-12-21 11:32:34 -08001673 &num_devices, new_snd_devices)) {
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301674 new_snd_devices[0] = in_snd_device;
Siena Richard7c2db772016-12-21 11:32:34 -08001675 num_devices = 1;
1676 }
1677 break;
Sriram Kumar9c836482020-10-27 20:34:35 +05301678 case PCM_HFP_CALL:
1679 rc = audio_extn_utils_send_app_type_cfg_hfp(adev,usecase);
1680 return rc;
Siena Richard7c2db772016-12-21 11:32:34 -08001681 default:
1682 ALOGI("%s: not a playback/capture path, no need to cfg app type", __func__);
1683 rc = 0;
1684 break;
1685 }
1686
1687 for (i = 0; i < num_devices; i++) {
1688 rc = send_app_type_cfg_for_device(adev, usecase, new_snd_devices[i]);
1689 if (rc)
1690 break;
1691 }
1692
1693 return rc;
1694}
1695
Preetam Singh Ranawat9519e9c2015-11-18 16:05:55 +05301696int read_line_from_file(const char *path, char *buf, size_t count)
1697{
1698 char * fgets_ret;
1699 FILE * fd;
1700 int rv;
1701
1702 fd = fopen(path, "r");
1703 if (fd == NULL)
1704 return -1;
1705
1706 fgets_ret = fgets(buf, (int)count, fd);
1707 if (NULL != fgets_ret) {
1708 rv = (int)strlen(buf);
1709 } else {
1710 rv = ferror(fd);
1711 }
1712 fclose(fd);
1713
1714 return rv;
1715}
1716
Ashish Jainf1eaa582016-05-23 20:54:24 +05301717/*Translates ALSA formats to AOSP PCM formats*/
1718audio_format_t alsa_format_to_hal(uint32_t alsa_format)
1719{
1720 audio_format_t format;
1721
1722 switch(alsa_format) {
1723 case SNDRV_PCM_FORMAT_S16_LE:
1724 format = AUDIO_FORMAT_PCM_16_BIT;
1725 break;
1726 case SNDRV_PCM_FORMAT_S24_3LE:
1727 format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
1728 break;
1729 case SNDRV_PCM_FORMAT_S24_LE:
1730 format = AUDIO_FORMAT_PCM_8_24_BIT;
1731 break;
1732 case SNDRV_PCM_FORMAT_S32_LE:
1733 format = AUDIO_FORMAT_PCM_32_BIT;
1734 break;
1735 default:
1736 ALOGW("Incorrect ALSA format");
1737 format = AUDIO_FORMAT_INVALID;
1738 }
1739 return format;
1740}
1741
1742/*Translates hal format (AOSP) to alsa formats*/
1743uint32_t hal_format_to_alsa(audio_format_t hal_format)
1744{
1745 uint32_t alsa_format;
1746
1747 switch (hal_format) {
1748 case AUDIO_FORMAT_PCM_32_BIT: {
1749 if (platform_supports_true_32bit())
1750 alsa_format = SNDRV_PCM_FORMAT_S32_LE;
1751 else
1752 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1753 }
1754 break;
1755 case AUDIO_FORMAT_PCM_8_BIT:
1756 alsa_format = SNDRV_PCM_FORMAT_S8;
1757 break;
1758 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
1759 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1760 break;
1761 case AUDIO_FORMAT_PCM_8_24_BIT: {
1762 if (platform_supports_true_32bit())
1763 alsa_format = SNDRV_PCM_FORMAT_S32_LE;
1764 else
1765 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1766 }
1767 break;
1768 case AUDIO_FORMAT_PCM_FLOAT:
1769 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1770 break;
1771 default:
1772 case AUDIO_FORMAT_PCM_16_BIT:
1773 alsa_format = SNDRV_PCM_FORMAT_S16_LE;
1774 break;
1775 }
1776 return alsa_format;
1777}
1778
Ashish Jain83a6cc22016-06-28 14:34:17 +05301779/*Translates PCM formats to AOSP formats*/
1780audio_format_t pcm_format_to_hal(uint32_t pcm_format)
1781{
1782 audio_format_t format = AUDIO_FORMAT_INVALID;
1783
1784 switch(pcm_format) {
1785 case PCM_FORMAT_S16_LE:
1786 format = AUDIO_FORMAT_PCM_16_BIT;
1787 break;
1788 case PCM_FORMAT_S24_3LE:
1789 format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
1790 break;
1791 case PCM_FORMAT_S24_LE:
1792 format = AUDIO_FORMAT_PCM_8_24_BIT;
1793 break;
1794 case PCM_FORMAT_S32_LE:
1795 format = AUDIO_FORMAT_PCM_32_BIT;
1796 break;
1797 default:
1798 ALOGW("Incorrect PCM format");
1799 format = AUDIO_FORMAT_INVALID;
1800 }
1801 return format;
1802}
1803
1804/*Translates hal format (AOSP) to alsa formats*/
1805uint32_t hal_format_to_pcm(audio_format_t hal_format)
1806{
1807 uint32_t pcm_format;
1808
1809 switch (hal_format) {
1810 case AUDIO_FORMAT_PCM_32_BIT:
1811 case AUDIO_FORMAT_PCM_8_24_BIT:
1812 case AUDIO_FORMAT_PCM_FLOAT: {
1813 if (platform_supports_true_32bit())
1814 pcm_format = PCM_FORMAT_S32_LE;
1815 else
1816 pcm_format = PCM_FORMAT_S24_3LE;
1817 }
1818 break;
1819 case AUDIO_FORMAT_PCM_8_BIT:
1820 pcm_format = PCM_FORMAT_S8;
1821 break;
1822 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
1823 pcm_format = PCM_FORMAT_S24_3LE;
1824 break;
1825 default:
1826 case AUDIO_FORMAT_PCM_16_BIT:
1827 pcm_format = PCM_FORMAT_S16_LE;
1828 break;
1829 }
1830 return pcm_format;
1831}
1832
Ashish Jainf1eaa582016-05-23 20:54:24 +05301833uint32_t get_alsa_fragment_size(uint32_t bytes_per_sample,
1834 uint32_t sample_rate,
Deeraj Soman65358ab2019-02-07 15:40:49 +05301835 uint32_t noOfChannels,
1836 int64_t duration_ms)
Ashish Jainf1eaa582016-05-23 20:54:24 +05301837{
1838 uint32_t fragment_size = 0;
1839 uint32_t pcm_offload_time = PCM_OFFLOAD_BUFFER_DURATION;
1840
Deeraj Soman65358ab2019-02-07 15:40:49 +05301841 if (duration_ms >= MIN_OFFLOAD_BUFFER_DURATION_MS && duration_ms <= MAX_OFFLOAD_BUFFER_DURATION_MS)
1842 pcm_offload_time = duration_ms;
1843
Ashish Jainf1eaa582016-05-23 20:54:24 +05301844 fragment_size = (pcm_offload_time
1845 * sample_rate
1846 * bytes_per_sample
1847 * noOfChannels)/1000;
1848 if (fragment_size < MIN_PCM_OFFLOAD_FRAGMENT_SIZE)
1849 fragment_size = MIN_PCM_OFFLOAD_FRAGMENT_SIZE;
1850 else if (fragment_size > MAX_PCM_OFFLOAD_FRAGMENT_SIZE)
1851 fragment_size = MAX_PCM_OFFLOAD_FRAGMENT_SIZE;
1852 /*To have same PCM samples for all channels, the buffer size requires to
1853 *be multiple of (number of channels * bytes per sample)
1854 *For writes to succeed, the buffer must be written at address which is multiple of 32
1855 */
Aalique Grahameb85f2d92017-10-16 17:53:23 -07001856 fragment_size = ALIGN(fragment_size, (bytes_per_sample * noOfChannels * 32));
Ashish Jainf1eaa582016-05-23 20:54:24 +05301857
1858 ALOGI("PCM offload Fragment size to %d bytes", fragment_size);
1859 return fragment_size;
1860}
1861
1862/* Calculates the fragment size required to configure compress session.
1863 * Based on the alsa format selected, decide if conversion is needed in
1864
1865 * HAL ( e.g. convert AUDIO_FORMAT_PCM_FLOAT input format to
1866 * AUDIO_FORMAT_PCM_24_BIT_PACKED before writing to the compress driver.
1867 */
1868void audio_extn_utils_update_direct_pcm_fragment_size(struct stream_out *out)
1869{
Ashish Jain83a6cc22016-06-28 14:34:17 +05301870 audio_format_t dst_format = out->hal_op_format;
1871 audio_format_t src_format = out->hal_ip_format;
Ashish Jainf1eaa582016-05-23 20:54:24 +05301872 uint32_t hal_op_bytes_per_sample = audio_bytes_per_sample(dst_format);
1873 uint32_t hal_ip_bytes_per_sample = audio_bytes_per_sample(src_format);
1874
1875 out->compr_config.fragment_size =
1876 get_alsa_fragment_size(hal_op_bytes_per_sample,
1877 out->sample_rate,
Deeraj Soman65358ab2019-02-07 15:40:49 +05301878 popcount(out->channel_mask),
1879 out->info.duration_us / 1000);
Ashish Jainf1eaa582016-05-23 20:54:24 +05301880
1881 if ((src_format != dst_format) &&
1882 hal_op_bytes_per_sample != hal_ip_bytes_per_sample) {
1883
Ashish Jain83a6cc22016-06-28 14:34:17 +05301884 out->hal_fragment_size =
Ashish Jainf1eaa582016-05-23 20:54:24 +05301885 ((out->compr_config.fragment_size * hal_ip_bytes_per_sample) /
1886 hal_op_bytes_per_sample);
1887 ALOGI("enable conversion hal_input_fragment_size is %d src_format %x dst_format %x",
Ashish Jain83a6cc22016-06-28 14:34:17 +05301888 out->hal_fragment_size, src_format, dst_format);
Ashish Jainf1eaa582016-05-23 20:54:24 +05301889 } else {
Ashish Jain83a6cc22016-06-28 14:34:17 +05301890 out->hal_fragment_size = out->compr_config.fragment_size;
Ashish Jainf1eaa582016-05-23 20:54:24 +05301891 }
1892}
1893
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301894/* converts pcm format 24_8 to 8_24 inplace */
1895size_t audio_extn_utils_convert_format_24_8_to_8_24(void *buf, size_t bytes)
1896{
1897 size_t i = 0;
1898 int *int_buf_stream = buf;
1899
1900 if ((bytes % 4) != 0) {
1901 ALOGE("%s: wrong inout buffer! ... is not 32 bit aligned ", __func__);
1902 return -EINVAL;
1903 }
1904
1905 for (; i < (bytes / 4); i++)
1906 int_buf_stream[i] >>= 8;
1907
1908 return bytes;
1909}
1910
Xiaojun Sang782e5b12020-06-29 21:13:06 +08001911#ifdef AUDIO_GKI_ENABLED
1912int get_snd_codec_id(audio_format_t format)
1913{
1914 int id = 0;
1915
1916 switch (format & AUDIO_FORMAT_MAIN_MASK) {
1917 case AUDIO_FORMAT_MP3:
1918 id = SND_AUDIOCODEC_MP3;
1919 break;
1920 case AUDIO_FORMAT_AAC:
1921 id = SND_AUDIOCODEC_AAC;
1922 break;
1923 case AUDIO_FORMAT_AAC_ADTS:
1924 id = SND_AUDIOCODEC_AAC;
1925 break;
1926 case AUDIO_FORMAT_AAC_LATM:
1927 id = SND_AUDIOCODEC_AAC;
1928 break;
1929 case AUDIO_FORMAT_PCM:
1930 id = SND_AUDIOCODEC_PCM;
1931 break;
1932 case AUDIO_FORMAT_FLAC:
1933 case AUDIO_FORMAT_ALAC:
1934 case AUDIO_FORMAT_APE:
1935 case AUDIO_FORMAT_VORBIS:
1936 case AUDIO_FORMAT_WMA:
1937 case AUDIO_FORMAT_WMA_PRO:
1938 case AUDIO_FORMAT_DSD:
1939 case AUDIO_FORMAT_APTX:
1940 id = SND_AUDIOCODEC_BESPOKE;
1941 break;
1942 case AUDIO_FORMAT_MP2:
1943 id = SND_AUDIOCODEC_MP2;
1944 break;
1945 case AUDIO_FORMAT_AC3:
1946 id = SND_AUDIOCODEC_AC3;
1947 break;
1948 case AUDIO_FORMAT_E_AC3:
1949 case AUDIO_FORMAT_E_AC3_JOC:
1950 id = SND_AUDIOCODEC_EAC3;
1951 break;
1952 case AUDIO_FORMAT_DTS:
1953 case AUDIO_FORMAT_DTS_HD:
1954 id = SND_AUDIOCODEC_DTS;
1955 break;
1956 case AUDIO_FORMAT_DOLBY_TRUEHD:
1957 id = SND_AUDIOCODEC_TRUEHD;
1958 break;
1959 case AUDIO_FORMAT_IEC61937:
1960 id = SND_AUDIOCODEC_IEC61937;
1961 break;
1962 default:
1963 ALOGE("%s: Unsupported audio format :%x", __func__, format);
1964 }
1965
1966 return id;
1967}
1968#else
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301969int get_snd_codec_id(audio_format_t format)
1970{
1971 int id = 0;
1972
1973 switch (format & AUDIO_FORMAT_MAIN_MASK) {
1974 case AUDIO_FORMAT_MP3:
1975 id = SND_AUDIOCODEC_MP3;
1976 break;
1977 case AUDIO_FORMAT_AAC:
1978 id = SND_AUDIOCODEC_AAC;
1979 break;
1980 case AUDIO_FORMAT_AAC_ADTS:
1981 id = SND_AUDIOCODEC_AAC;
1982 break;
Arun Kumar Dasari3b174182016-12-27 13:01:14 +05301983 case AUDIO_FORMAT_AAC_LATM:
1984 id = SND_AUDIOCODEC_AAC;
1985 break;
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301986 case AUDIO_FORMAT_PCM:
1987 id = SND_AUDIOCODEC_PCM;
1988 break;
1989 case AUDIO_FORMAT_FLAC:
1990 id = SND_AUDIOCODEC_FLAC;
1991 break;
1992 case AUDIO_FORMAT_ALAC:
1993 id = SND_AUDIOCODEC_ALAC;
1994 break;
1995 case AUDIO_FORMAT_APE:
1996 id = SND_AUDIOCODEC_APE;
1997 break;
1998 case AUDIO_FORMAT_VORBIS:
1999 id = SND_AUDIOCODEC_VORBIS;
2000 break;
2001 case AUDIO_FORMAT_WMA:
2002 id = SND_AUDIOCODEC_WMA;
2003 break;
Dechen Chai4a575252021-07-30 09:21:28 +05302004#ifndef AUDIO_DISABLE_COMPRESS_FORMAT
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05302005 case AUDIO_FORMAT_WMA_PRO:
2006 id = SND_AUDIOCODEC_WMA_PRO;
2007 break;
Dechen Chai4a575252021-07-30 09:21:28 +05302008#endif
Satish Babu Patakokila0c313922016-12-08 12:07:08 +05302009 case AUDIO_FORMAT_MP2:
2010 id = SND_AUDIOCODEC_MP2;
2011 break;
Satish Babu Patakokila915ecba2017-01-10 17:43:56 +05302012 case AUDIO_FORMAT_AC3:
2013 id = SND_AUDIOCODEC_AC3;
2014 break;
2015 case AUDIO_FORMAT_E_AC3:
2016 case AUDIO_FORMAT_E_AC3_JOC:
2017 id = SND_AUDIOCODEC_EAC3;
2018 break;
2019 case AUDIO_FORMAT_DTS:
2020 case AUDIO_FORMAT_DTS_HD:
2021 id = SND_AUDIOCODEC_DTS;
2022 break;
Ben Romberger1aaaf862017-04-06 17:49:46 -07002023 case AUDIO_FORMAT_DOLBY_TRUEHD:
2024 id = SND_AUDIOCODEC_TRUEHD;
2025 break;
Naresh Tanniru928f0862017-04-07 16:44:23 -07002026 case AUDIO_FORMAT_IEC61937:
2027 id = SND_AUDIOCODEC_IEC61937;
2028 break;
Dechen Chai4a575252021-07-30 09:21:28 +05302029#ifndef AUDIO_DISABLE_COMPRESS_FORMAT
Preetam Singh Ranawat4277a5a2017-01-18 19:02:24 +05302030 case AUDIO_FORMAT_DSD:
2031 id = SND_AUDIOCODEC_DSD;
2032 break;
Dhanalakshmi Siddani18737932016-11-29 17:33:17 +05302033 case AUDIO_FORMAT_APTX:
2034 id = SND_AUDIOCODEC_APTX;
2035 break;
Dechen Chai4a575252021-07-30 09:21:28 +05302036#endif
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05302037 default:
2038 ALOGE("%s: Unsupported audio format :%x", __func__, format);
2039 }
2040
2041 return id;
2042}
Xiaojun Sang782e5b12020-06-29 21:13:06 +08002043#endif
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05302044
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07002045void audio_extn_utils_send_audio_calibration(struct audio_device *adev,
2046 struct audio_usecase *usecase)
2047{
2048 int type = usecase->type;
2049
Dhananjay Kumar14245982017-01-16 20:21:00 +05302050 if (type == PCM_PLAYBACK && usecase->stream.out != NULL) {
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05302051 platform_send_audio_calibration(adev->platform, usecase,
Zhou Song06761dd2019-04-28 18:08:17 +08002052 usecase->stream.out->app_type_cfg.app_type);
Dhananjay Kumar14245982017-01-16 20:21:00 +05302053 } else if (type == PCM_CAPTURE && usecase->stream.in != NULL) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +05302054 platform_send_audio_calibration(adev->platform, usecase,
Zhou Song06761dd2019-04-28 18:08:17 +08002055 usecase->stream.in->app_type_cfg.app_type);
Sriram Kumar9c836482020-10-27 20:34:35 +05302056 } else if (type == PCM_HFP_CALL) {
2057 /* when app type is default. the sample rate is not used to send cal */
2058#ifdef ENABLE_HFP_CALIBRATION
2059 platform_send_audio_calibration_hfp(adev->platform, usecase->in_snd_device);
2060#else
2061 platform_send_audio_calibration(adev->platform, usecase,
2062 platform_get_default_app_type_v2(adev->platform, usecase->type));
2063#endif
2064 } else if ((type == PCM_CAPTURE) ||
Derek Chena30a5f42019-12-03 11:17:09 -05002065 (type == TRANSCODE_LOOPBACK_RX && usecase->stream.inout != NULL) ||
Fei Tongaffdf732020-02-20 20:39:05 +08002066 (type == ICC_CALL) || (type == SYNTH_LOOPBACK)) {
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05302067 platform_send_audio_calibration(adev->platform, usecase,
Zhou Song06761dd2019-04-28 18:08:17 +08002068 platform_get_default_app_type_v2(adev->platform, usecase->type));
Vidyakumar Athotae57f6002017-03-01 13:13:16 -08002069 } else {
2070 /* No need to send audio calibration for voice and voip call usecases */
2071 if ((type != VOICE_CALL) && (type != VOIP_CALL))
2072 ALOGW("%s: No audio calibration for usecase type = %d", __func__, type);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07002073 }
2074}
2075
Ben Rombergera04fabc2014-11-14 12:16:03 -08002076// Base64 Encode and Decode
2077// Not all features supported. This must be used only with following conditions.
2078// Decode Modes: Support with and without padding
2079// CRLF not handling. So no CRLF in string to decode.
2080// Encode Modes: Supports only padding
2081int b64decode(char *inp, int ilen, uint8_t* outp)
2082{
2083 int i, j, k, ii, num;
2084 int rem, pcnt;
2085 uint32_t res=0;
2086 uint8_t getIndex[MAX_BASEINDEX_LEN];
2087 uint8_t tmp, cflag;
2088
2089 if(inp == NULL || outp == NULL || ilen <= 0) {
2090 ALOGE("[%s] received NULL pointer or zero length",__func__);
2091 return -1;
2092 }
2093
2094 memset(getIndex, MAX_BASEINDEX_LEN-1, sizeof(getIndex));
2095 for(i=0;i<BASE_TABLE_SIZE;i++) {
2096 getIndex[(uint8_t)bTable[i]] = (uint8_t)i;
2097 }
2098 getIndex[(uint8_t)'=']=0;
2099
2100 j=0;k=0;
2101 num = ilen/4;
2102 rem = ilen%4;
2103 if(rem==0)
2104 num = num-1;
2105 cflag=0;
2106 for(i=0; i<num; i++) {
2107 res=0;
2108 for(ii=0;ii<4;ii++) {
2109 res = res << 6;
2110 tmp = getIndex[(uint8_t)inp[j++]];
2111 res = res | tmp;
2112 cflag = cflag | tmp;
2113 }
2114 outp[k++] = (res >> 16)&0xFF;
2115 outp[k++] = (res >> 8)&0xFF;
2116 outp[k++] = res & 0xFF;
2117 }
2118
2119 // Handle last bytes special
2120 pcnt=0;
2121 if(rem == 0) {
2122 //With padding or full data
2123 res = 0;
2124 for(ii=0;ii<4;ii++) {
2125 if(inp[j] == '=')
2126 pcnt++;
2127 res = res << 6;
2128 tmp = getIndex[(uint8_t)inp[j++]];
2129 res = res | tmp;
2130 cflag = cflag | tmp;
2131 }
2132 outp[k++] = res >> 16;
2133 if(pcnt == 2)
2134 goto done;
2135 outp[k++] = (res>>8)&0xFF;
2136 if(pcnt == 1)
2137 goto done;
2138 outp[k++] = res&0xFF;
2139 } else {
2140 //without padding
2141 res = 0;
2142 for(i=0;i<rem;i++) {
2143 res = res << 6;
2144 tmp = getIndex[(uint8_t)inp[j++]];
2145 res = res | tmp;
2146 cflag = cflag | tmp;
2147 }
2148 for(i=rem;i<4;i++) {
2149 res = res << 6;
2150 pcnt++;
2151 }
2152 outp[k++] = res >> 16;
2153 if(pcnt == 2)
2154 goto done;
2155 outp[k++] = (res>>8)&0xFF;
2156 if(pcnt == 1)
2157 goto done;
2158 outp[k++] = res&0xFF;
2159 }
2160done:
2161 if(cflag == 0xFF) {
2162 ALOGE("[%s] base64 decode failed. Invalid character found %s",
2163 __func__, inp);
2164 return 0;
2165 }
2166 return k;
2167}
2168
2169int b64encode(uint8_t *inp, int ilen, char* outp)
2170{
2171 int i,j,k, num;
2172 int rem=0;
2173 uint32_t res=0;
2174
2175 if(inp == NULL || outp == NULL || ilen<=0) {
2176 ALOGE("[%s] received NULL pointer or zero input length",__func__);
2177 return -1;
2178 }
2179
2180 num = ilen/3;
2181 rem = ilen%3;
2182 j=0;k=0;
2183 for(i=0; i<num; i++) {
2184 //prepare index
2185 res = inp[j++]<<16;
2186 res = res | inp[j++]<<8;
2187 res = res | inp[j++];
2188 //get output map from index
2189 outp[k++] = (char) bTable[(res>>18)&0x3F];
2190 outp[k++] = (char) bTable[(res>>12)&0x3F];
2191 outp[k++] = (char) bTable[(res>>6)&0x3F];
2192 outp[k++] = (char) bTable[res&0x3F];
2193 }
2194
2195 switch(rem) {
2196 case 1:
2197 res = inp[j++]<<16;
2198 outp[k++] = (char) bTable[res>>18];
2199 outp[k++] = (char) bTable[(res>>12)&0x3F];
2200 //outp[k++] = '=';
2201 //outp[k++] = '=';
2202 break;
2203 case 2:
2204 res = inp[j++]<<16;
2205 res = res | inp[j++]<<8;
2206 outp[k++] = (char) bTable[res>>18];
2207 outp[k++] = (char) bTable[(res>>12)&0x3F];
2208 outp[k++] = (char) bTable[(res>>6)&0x3F];
2209 //outp[k++] = '=';
2210 break;
2211 default:
2212 break;
2213 }
Ben Rombergera04fabc2014-11-14 12:16:03 -08002214 outp[k] = '\0';
2215 return k;
2216}
Ashish Jain81eb2a82015-05-13 10:52:34 +05302217
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05302218
2219int audio_extn_utils_get_codec_version(const char *snd_card_name,
2220 int card_num,
2221 char *codec_version)
2222{
2223 char procfs_path[50];
2224 FILE *fp;
2225
2226 if (strstr(snd_card_name, "tasha")) {
2227 snprintf(procfs_path, sizeof(procfs_path),
2228 "/proc/asound/card%d/codecs/tasha/version", card_num);
2229 if ((fp = fopen(procfs_path, "r")) != NULL) {
2230 fgets(codec_version, CODEC_VERSION_MAX_LENGTH, fp);
2231 fclose(fp);
2232 } else {
2233 ALOGE("%s: ERROR. cannot open %s", __func__, procfs_path);
2234 return -ENOENT;
2235 }
2236 ALOGD("%s: codec version %s", __func__, codec_version);
2237 }
2238
2239 return 0;
2240}
2241
Preetam Singh Ranawat9d0d8e42019-07-15 12:27:25 +05302242int audio_extn_utils_get_codec_variant(int card_num,
2243 char *codec_variant)
2244{
2245 char procfs_path[50];
2246 FILE *fp;
2247 snprintf(procfs_path, sizeof(procfs_path),
2248 "/proc/asound/card%d/codecs/wcd938x/variant", card_num);
2249 if ((fp = fopen(procfs_path, "r")) == NULL) {
2250 snprintf(procfs_path, sizeof(procfs_path),
2251 "/proc/asound/card%d/codecs/wcd937x/variant", card_num);
2252 if ((fp = fopen(procfs_path, "r")) == NULL) {
2253 ALOGE("%s: ERROR. cannot open %s", __func__, procfs_path);
2254 return -ENOENT;
2255 }
2256 }
2257 fgets(codec_variant, CODEC_VARIANT_MAX_LENGTH, fp);
2258 fclose(fp);
2259 ALOGD("%s: codec variant is %s", __func__, codec_variant);
2260 return 0;
2261}
2262
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05302263
Ashish Jain81eb2a82015-05-13 10:52:34 +05302264#ifdef AUDIO_EXTERNAL_HDMI_ENABLED
2265
2266void get_default_compressed_channel_status(
2267 unsigned char *channel_status)
2268{
Ashish Jain81eb2a82015-05-13 10:52:34 +05302269 memset(channel_status,0,24);
2270
2271 /* block start bit in preamble bit 3 */
2272 channel_status[0] |= PROFESSIONAL;
2273 //compre out
2274 channel_status[0] |= NON_LPCM;
2275 // sample rate; fixed 48K for default/transcode
2276 channel_status[3] |= SR_48000;
2277}
2278
Ashish Jain81eb2a82015-05-13 10:52:34 +05302279int32_t get_compressed_channel_status(void *audio_stream_data,
2280 uint32_t audio_frame_size,
2281 unsigned char *channel_status,
2282 enum audio_parser_code_type codec_type)
2283 // codec_type - AUDIO_PARSER_CODEC_AC3
2284 // - AUDIO_PARSER_CODEC_DTS
2285{
2286 unsigned char *stream;
2287 int ret = 0;
2288 stream = (unsigned char *)audio_stream_data;
2289
2290 if (audio_stream_data == NULL || audio_frame_size == 0) {
2291 ALOGW("no buffer to get channel status, return default for compress");
2292 get_default_compressed_channel_status(channel_status);
2293 return ret;
2294 }
2295
2296 memset(channel_status,0,24);
2297 if(init_audio_parser(stream, audio_frame_size, codec_type) == -1)
2298 {
2299 ALOGE("init audio parser failed");
2300 return -1;
2301 }
2302 ret = get_channel_status(channel_status, codec_type);
2303 return ret;
2304
2305}
2306
Ashish Jain81eb2a82015-05-13 10:52:34 +05302307void get_lpcm_channel_status(uint32_t sampleRate,
2308 unsigned char *channel_status)
2309{
2310 int32_t status = 0;
Ashish Jain81eb2a82015-05-13 10:52:34 +05302311 memset(channel_status,0,24);
2312 /* block start bit in preamble bit 3 */
2313 channel_status[0] |= PROFESSIONAL;
2314 //LPCM OUT
2315 channel_status[0] &= ~NON_LPCM;
2316
2317 switch (sampleRate) {
2318 case 8000:
2319 case 11025:
2320 case 12000:
2321 case 16000:
2322 case 22050:
2323 channel_status[3] |= SR_NOTID;
Preetam Singh Ranawat61716b12015-12-14 11:55:24 +05302324 break;
Ashish Jain81eb2a82015-05-13 10:52:34 +05302325 case 24000:
2326 channel_status[3] |= SR_24000;
2327 break;
2328 case 32000:
2329 channel_status[3] |= SR_32000;
2330 break;
2331 case 44100:
2332 channel_status[3] |= SR_44100;
2333 break;
2334 case 48000:
2335 channel_status[3] |= SR_48000;
2336 break;
2337 case 88200:
2338 channel_status[3] |= SR_88200;
2339 break;
2340 case 96000:
2341 channel_status[3] |= SR_96000;
2342 break;
2343 case 176400:
2344 channel_status[3] |= SR_176400;
2345 break;
2346 case 192000:
2347 channel_status[3] |= SR_192000;
2348 break;
2349 default:
2350 ALOGV("Invalid sample_rate %u\n", sampleRate);
2351 status = -1;
2352 break;
2353 }
2354}
2355
2356void audio_utils_set_hdmi_channel_status(struct stream_out *out, char * buffer, size_t bytes)
2357{
2358 unsigned char channel_status[24]={0};
2359 struct snd_aes_iec958 iec958;
2360 const char *mixer_ctl_name = "IEC958 Playback PCM Stream";
2361 struct mixer_ctl *ctl;
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +05302362 ALOGV("%s: buffer %s bytes %zd", __func__, buffer, bytes);
Arun Mirpurie008ed22019-03-21 11:21:04 -07002363
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05302364 if (audio_extn_utils_is_dolby_format(out->format) &&
Ashish Jain81eb2a82015-05-13 10:52:34 +05302365 /*TODO:Extend code to support DTS passthrough*/
2366 /*set compressed channel status bits*/
Arun Mirpurie008ed22019-03-21 11:21:04 -07002367 audio_extn_passthru_is_passthrough_stream(out) &&
2368 audio_extn_is_hdmi_passthru_enabled()) {
Ashish Jain81eb2a82015-05-13 10:52:34 +05302369 get_compressed_channel_status(buffer, bytes, channel_status, AUDIO_PARSER_CODEC_AC3);
Arun Mirpurie008ed22019-03-21 11:21:04 -07002370 } else {
Ashish Jain81eb2a82015-05-13 10:52:34 +05302371 /*set channel status bit for LPCM*/
2372 get_lpcm_channel_status(out->sample_rate, channel_status);
2373 }
2374
2375 memcpy(iec958.status, channel_status,sizeof(iec958.status));
2376 ctl = mixer_get_ctl_by_name(out->dev->mixer, mixer_ctl_name);
2377 if (!ctl) {
2378 ALOGE("%s: Could not get ctl for mixer cmd - %s",
2379 __func__, mixer_ctl_name);
2380 return;
2381 }
2382 if (mixer_ctl_set_array(ctl, &iec958, sizeof(iec958)) < 0) {
2383 ALOGE("%s: Could not set channel status for ext HDMI ",
2384 __func__);
2385 return;
2386 }
2387
2388}
2389#endif
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302390
2391int audio_extn_utils_get_avt_device_drift(
2392 struct audio_usecase *usecase,
2393 struct audio_avt_device_drift_param *drift_param)
2394{
2395 int ret = 0, count = 0;
2396 char avt_device_drift_mixer_ctl_name[MIXER_PATH_MAX_LENGTH] = {0};
Naresh Tanniru6160c712017-04-17 15:43:48 +05302397 const char *backend = NULL;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302398 struct mixer_ctl *ctl = NULL;
2399 struct audio_avt_device_drift_stats drift_stats;
2400 struct audio_device *adev = NULL;
2401
2402 if (usecase != NULL && usecase->type == PCM_PLAYBACK) {
Naresh Tanniru6160c712017-04-17 15:43:48 +05302403 backend = platform_get_snd_device_backend_interface(usecase->out_snd_device);
2404 if (!backend) {
2405 ALOGE("%s: Unsupported device %d", __func__,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08002406 get_device_types(&usecase->stream.out->device_list));
Naresh Tanniru6160c712017-04-17 15:43:48 +05302407 ret = -EINVAL;
2408 goto done;
2409 }
2410 strlcpy(avt_device_drift_mixer_ctl_name,
2411 backend,
2412 MIXER_PATH_MAX_LENGTH);
2413
2414 count = strlen(backend);
2415 if (MIXER_PATH_MAX_LENGTH - count > 0) {
2416 strlcat(&avt_device_drift_mixer_ctl_name[count],
2417 " DRIFT",
2418 MIXER_PATH_MAX_LENGTH - count);
2419 } else {
2420 ret = -EINVAL;
2421 goto done;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302422 }
2423 } else {
Naresh Tanniru7586e292017-04-13 10:38:13 +05302424 ALOGE("%s: Invalid usecase",__func__);
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302425 ret = -EINVAL;
Naresh Tanniru6160c712017-04-17 15:43:48 +05302426 goto done;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302427 }
2428
Naresh Tanniru6160c712017-04-17 15:43:48 +05302429 adev = usecase->stream.out->dev;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302430 ctl = mixer_get_ctl_by_name(adev->mixer, avt_device_drift_mixer_ctl_name);
2431 if (!ctl) {
2432 ALOGE("%s: Could not get ctl for mixer cmd - %s",
2433 __func__, avt_device_drift_mixer_ctl_name);
2434
2435 ret = -EINVAL;
2436 goto done;
2437 }
2438
2439 ALOGV("%s: Getting AV Timer vs Device Drift mixer ctrl name %s", __func__,
2440 avt_device_drift_mixer_ctl_name);
2441
2442 mixer_ctl_update(ctl);
2443 count = mixer_ctl_get_num_values(ctl);
2444 if (count != sizeof(struct audio_avt_device_drift_stats)) {
2445 ALOGE("%s: mixer_ctl_get_num_values() invalid drift_stats data size",
2446 __func__);
2447
2448 ret = -EINVAL;
2449 goto done;
2450 }
2451
2452 ret = mixer_ctl_get_array(ctl, (void *)&drift_stats, count);
2453 if (ret != 0) {
2454 ALOGE("%s: mixer_ctl_get_array() failed to get drift_stats Params",
2455 __func__);
2456
2457 ret = -EINVAL;
2458 goto done;
2459 }
2460 memcpy(drift_param, &drift_stats.drift_param,
2461 sizeof(struct audio_avt_device_drift_param));
2462done:
2463 return ret;
2464}
Manish Dewangan07de2142017-02-27 19:27:20 +05302465
2466#ifdef SNDRV_COMPRESS_PATH_DELAY
2467int audio_extn_utils_compress_get_dsp_latency(struct stream_out *out)
2468{
2469 int ret = -EINVAL;
2470 struct snd_compr_metadata metadata;
2471 int delay_ms = COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
2472
Weiyin Jiangc49a3b52018-08-17 16:16:08 +08002473 /* override the latency for pcm offload use case */
2474 if ((out->flags & AUDIO_OUTPUT_FLAG_DIRECT) &&
2475 !(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)) {
2476 delay_ms = PCM_OFFLOAD_PLAYBACK_LATENCY;
2477 }
2478
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -07002479 if (property_get_bool("vendor.audio.playback.dsp.pathdelay", false)) {
Manish Dewangan07de2142017-02-27 19:27:20 +05302480 ALOGD("%s:: Quering DSP delay %d",__func__, __LINE__);
2481 if (!(is_offload_usecase(out->usecase))) {
2482 ALOGE("%s:: not supported for non offload session", __func__);
2483 goto exit;
2484 }
2485
2486 if (!out->compr) {
2487 ALOGD("%s:: Invalid compress handle,returning default dsp latency",
2488 __func__);
2489 goto exit;
2490 }
2491
2492 metadata.key = SNDRV_COMPRESS_PATH_DELAY;
2493 ret = compress_get_metadata(out->compr, &metadata);
2494 if(ret) {
2495 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2496 goto exit;
2497 }
2498 delay_ms = metadata.value[0] / 1000; /*convert to ms*/
2499 } else {
2500 ALOGD("%s:: Using Fix DSP delay",__func__);
2501 }
2502
2503exit:
2504 ALOGD("%s:: delay in ms is %d",__func__, delay_ms);
2505 return delay_ms;
2506}
2507#else
2508int audio_extn_utils_compress_get_dsp_latency(struct stream_out *out __unused)
2509{
2510 return COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
2511}
2512#endif
Manish Dewangan69426c82017-01-30 17:35:36 +05302513
2514#ifdef SNDRV_COMPRESS_RENDER_MODE
2515int audio_extn_utils_compress_set_render_mode(struct stream_out *out)
2516{
2517 struct snd_compr_metadata metadata;
2518 int ret = -EINVAL;
2519
2520 if (!(is_offload_usecase(out->usecase))) {
2521 ALOGE("%s:: not supported for non offload session", __func__);
2522 goto exit;
2523 }
2524
2525 if (!out->compr) {
2526 ALOGD("%s:: Invalid compress handle",
2527 __func__);
2528 goto exit;
2529 }
2530
2531 ALOGD("%s:: render mode %d", __func__, out->render_mode);
2532
2533 metadata.key = SNDRV_COMPRESS_RENDER_MODE;
2534 if (out->render_mode == RENDER_MODE_AUDIO_MASTER) {
2535 metadata.value[0] = SNDRV_COMPRESS_RENDER_MODE_AUDIO_MASTER;
2536 } else if (out->render_mode == RENDER_MODE_AUDIO_STC_MASTER) {
2537 metadata.value[0] = SNDRV_COMPRESS_RENDER_MODE_STC_MASTER;
2538 } else {
2539 ret = 0;
2540 goto exit;
2541 }
2542 ret = compress_set_metadata(out->compr, &metadata);
2543 if(ret) {
2544 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2545 }
2546exit:
2547 return ret;
2548}
2549#else
2550int audio_extn_utils_compress_set_render_mode(struct stream_out *out __unused)
2551{
2552 ALOGD("%s:: configuring render mode not supported", __func__);
2553 return 0;
2554}
2555#endif
Manish Dewangan58229382017-02-02 15:48:41 +05302556
2557#ifdef SNDRV_COMPRESS_CLK_REC_MODE
2558int audio_extn_utils_compress_set_clk_rec_mode(
2559 struct audio_usecase *usecase)
2560{
2561 struct snd_compr_metadata metadata;
2562 struct stream_out *out = NULL;
2563 int ret = -EINVAL;
2564
Naresh Tanniru7586e292017-04-13 10:38:13 +05302565 if (usecase == NULL || usecase->type != PCM_PLAYBACK) {
Manish Dewangan58229382017-02-02 15:48:41 +05302566 ALOGE("%s:: Invalid use case", __func__);
2567 goto exit;
2568 }
2569
2570 out = usecase->stream.out;
2571 if (!out) {
2572 ALOGE("%s:: invalid stream", __func__);
2573 goto exit;
2574 }
2575
2576 if (!is_offload_usecase(out->usecase)) {
2577 ALOGE("%s:: not supported for non offload session", __func__);
2578 goto exit;
2579 }
2580
2581 if (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER) {
2582 ALOGD("%s:: clk recovery is only supported in STC render mode",
2583 __func__);
2584 ret = 0;
2585 goto exit;
2586 }
2587
2588 if (!out->compr) {
2589 ALOGD("%s:: Invalid compress handle",
2590 __func__);
2591 goto exit;
2592 }
2593 metadata.key = SNDRV_COMPRESS_CLK_REC_MODE;
2594 switch(usecase->out_snd_device) {
2595 case SND_DEVICE_OUT_HDMI:
2596 case SND_DEVICE_OUT_SPEAKER_AND_HDMI:
2597 case SND_DEVICE_OUT_DISPLAY_PORT:
2598 case SND_DEVICE_OUT_SPEAKER_AND_DISPLAY_PORT:
2599 metadata.value[0] = SNDRV_COMPRESS_CLK_REC_MODE_NONE;
2600 break;
2601 default:
2602 metadata.value[0] = SNDRV_COMPRESS_CLK_REC_MODE_AUTO;
2603 break;
2604 }
2605
2606 ALOGD("%s:: clk recovery mode %d",__func__, metadata.value[0]);
2607
2608 ret = compress_set_metadata(out->compr, &metadata);
2609 if(ret) {
2610 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2611 }
2612
2613exit:
2614 return ret;
2615}
2616#else
2617int audio_extn_utils_compress_set_clk_rec_mode(
2618 struct audio_usecase *usecase __unused)
2619{
2620 ALOGD("%s:: configuring render mode not supported", __func__);
2621 return 0;
2622}
2623#endif
Manish Dewangan27346042017-03-01 12:56:12 +05302624
2625#ifdef SNDRV_COMPRESS_RENDER_WINDOW
2626int audio_extn_utils_compress_set_render_window(
2627 struct stream_out *out,
2628 struct audio_out_render_window_param *render_window)
2629{
2630 struct snd_compr_metadata metadata;
2631 int ret = -EINVAL;
2632
Manish Dewangan27346042017-03-01 12:56:12 +05302633 if(render_window == NULL) {
2634 ALOGE("%s:: Invalid render_window", __func__);
2635 goto exit;
2636 }
2637
Aniket Kumar Lata1e1d3662017-06-01 18:45:48 -07002638 ALOGD("%s:: render window start 0x%"PRIx64" end 0x%"PRIx64"",
2639 __func__,render_window->render_ws, render_window->render_we);
2640
Manish Dewangan27346042017-03-01 12:56:12 +05302641 if (!is_offload_usecase(out->usecase)) {
2642 ALOGE("%s:: not supported for non offload session", __func__);
2643 goto exit;
2644 }
2645
Naresh Tanniru6160c712017-04-17 15:43:48 +05302646 if ((out->render_mode != RENDER_MODE_AUDIO_MASTER) &&
2647 (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER)) {
Manish Dewangan27346042017-03-01 12:56:12 +05302648 ALOGD("%s:: only supported in timestamp mode, current "
2649 "render mode mode %d", __func__, out->render_mode);
2650 goto exit;
2651 }
2652
2653 if (!out->compr) {
2654 ALOGW("%s:: offload session not yet opened,"
2655 "render window will be configure later", __func__);
2656 /* store render window to reconfigure in start_output_stream() */
2657 goto exit;
2658 }
2659
2660 metadata.key = SNDRV_COMPRESS_RENDER_WINDOW;
2661 /*render window start value */
2662 metadata.value[0] = 0xFFFFFFFF & render_window->render_ws; /* lsb */
2663 metadata.value[1] = \
2664 (0xFFFFFFFF00000000 & render_window->render_ws) >> 32; /* msb*/
2665 /*render window end value */
2666 metadata.value[2] = 0xFFFFFFFF & render_window->render_we; /* lsb */
2667 metadata.value[3] = \
2668 (0xFFFFFFFF00000000 & render_window->render_we) >> 32; /* msb*/
2669
2670 ret = compress_set_metadata(out->compr, &metadata);
2671 if(ret) {
2672 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2673 }
2674
2675exit:
2676 return ret;
2677}
2678#else
2679int audio_extn_utils_compress_set_render_window(
2680 struct stream_out *out __unused,
2681 struct audio_out_render_window_param *render_window __unused)
2682{
2683 ALOGD("%s:: configuring render window not supported", __func__);
2684 return 0;
2685}
2686#endif
Manish Dewangan14956cc2017-02-14 18:54:42 +05302687
2688#ifdef SNDRV_COMPRESS_START_DELAY
2689int audio_extn_utils_compress_set_start_delay(
2690 struct stream_out *out,
2691 struct audio_out_start_delay_param *delay_param)
2692{
2693 struct snd_compr_metadata metadata;
2694 int ret = -EINVAL;
2695
2696 if(delay_param == NULL) {
2697 ALOGE("%s:: Invalid delay_param", __func__);
2698 goto exit;
2699 }
2700
2701 ALOGD("%s:: render start delay 0x%"PRIx64" ", __func__,
2702 delay_param->start_delay);
2703
2704 if (!is_offload_usecase(out->usecase)) {
2705 ALOGE("%s:: not supported for non offload session", __func__);
2706 goto exit;
2707 }
2708
Naresh Tanniru6160c712017-04-17 15:43:48 +05302709 if ((out->render_mode != RENDER_MODE_AUDIO_MASTER) &&
2710 (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER)) {
Manish Dewangan14956cc2017-02-14 18:54:42 +05302711 ALOGD("%s:: only supported in timestamp mode, current "
2712 "render mode mode %d", __func__, out->render_mode);
2713 goto exit;
2714 }
2715
2716 if (!out->compr) {
2717 ALOGW("%s:: offload session not yet opened,"
2718 "start delay will be configure later", __func__);
2719 goto exit;
2720 }
2721
2722 metadata.key = SNDRV_COMPRESS_START_DELAY;
2723 metadata.value[0] = 0xFFFFFFFF & delay_param->start_delay; /* lsb */
2724 metadata.value[1] = \
2725 (0xFFFFFFFF00000000 & delay_param->start_delay) >> 32; /* msb*/
2726
2727 ret = compress_set_metadata(out->compr, &metadata);
2728 if(ret) {
2729 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2730 }
2731
2732exit:
2733 return ret;
2734}
2735#else
2736int audio_extn_utils_compress_set_start_delay(
2737 struct stream_out *out __unused,
2738 struct audio_out_start_delay_param *delay_param __unused)
2739{
2740 ALOGD("%s:: configuring render window not supported", __func__);
2741 return 0;
2742}
2743#endif
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002744
Surendar Karka287348c2019-04-10 18:31:46 +05302745#ifdef SNDRV_COMPRESS_DSP_POSITION
2746int audio_extn_utils_compress_get_dsp_presentation_pos(struct stream_out *out,
2747 uint64_t *frames, struct timespec *timestamp, int32_t clock_id)
2748{
2749 int ret = -EINVAL;
2750 uint64_t *val = NULL;
2751 uint64_t time = 0;
2752 struct snd_compr_metadata metadata;
2753
2754 ALOGV("%s:: Quering DSP position with clock id %d",__func__, clock_id);
2755 metadata.key = SNDRV_COMPRESS_DSP_POSITION;
2756 metadata.value[0] = clock_id;
2757 ret = compress_get_metadata(out->compr, &metadata);
2758 if (ret) {
2759 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2760 ret = -errno;
2761 goto exit;
2762 }
2763 val = (uint64_t *)&metadata.value[1];
2764 *frames = *val;
2765 time = *(val + 1);
2766 timestamp->tv_sec = time / 1000000;
2767 timestamp->tv_nsec = (time % 1000000)*1000;
2768
2769exit:
2770 return ret;
2771}
2772#else
2773int audio_extn_utils_compress_get_dsp_presentation_pos(struct stream_out *out __unused,
2774 uint64_t *frames __unused, struct timespec *timestamp __unused,
2775 int32_t clock_id __unused)
2776{
2777 ALOGD("%s:: dsp presentation position not supported", __func__);
2778 return 0;
2779
2780}
2781#endif
2782
2783#ifdef SNDRV_PCM_IOCTL_DSP_POSITION
2784int audio_extn_utils_pcm_get_dsp_presentation_pos(struct stream_out *out,
2785 uint64_t *frames, struct timespec *timestamp, int32_t clock_id)
2786{
2787 int ret = -EINVAL;
2788 uint64_t time = 0;
2789 struct snd_pcm_prsnt_position prsnt_position;
2790
2791 ALOGV("%s:: Quering DSP position with clock id %d",__func__, clock_id);
2792 prsnt_position.clock_id = clock_id;
2793 ret = pcm_ioctl(out->pcm, SNDRV_PCM_IOCTL_DSP_POSITION, &prsnt_position);
2794 if (ret) {
2795 ALOGE("%s::error %d", __func__, ret);
2796 ret = -EIO;
2797 goto exit;
2798 }
2799
2800 *frames = prsnt_position.frames;
2801 time = prsnt_position.timestamp;
2802 timestamp->tv_sec = time / 1000000;
2803 timestamp->tv_nsec = (time % 1000000)*1000;
2804
2805exit:
2806 return ret;
2807}
2808#else
2809int audio_extn_utils_pcm_get_dsp_presentation_pos(struct stream_out *out __unused,
2810 uint64_t *frames __unused, struct timespec *timestamp __unused,
2811 int32_t clock_id __unused)
2812{
2813 ALOGD("%s:: dsp presentation position not supported", __func__);
2814 return 0;
2815
2816}
2817#endif
2818
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002819#define MAX_SND_CARD 8
Jaideep Sharmadf948882020-10-22 14:13:52 +05302820#define RETRY_US 400000
2821#define RETRY_NUMBER 100
Aalique Grahame22e49102018-12-18 14:23:57 -08002822#define PLATFORM_INFO_XML_PATH "audio_platform_info.xml"
2823#define PLATFORM_INFO_XML_BASE_STRING "audio_platform_info"
2824
Aalique Grahame22e49102018-12-18 14:23:57 -08002825bool audio_extn_utils_resolve_config_file(char file_name[MIXER_PATH_MAX_LENGTH])
2826{
2827 char full_config_path[MIXER_PATH_MAX_LENGTH];
Saurav Kumarf5d2c342020-07-29 19:31:49 +05302828 char vendor_config_path[VENDOR_CONFIG_PATH_MAX_LENGTH];
2829
2830 /* Get path for audio configuration files in vendor */
2831 audio_get_vendor_config_path(vendor_config_path,
2832 sizeof(vendor_config_path));
2833 snprintf(full_config_path, sizeof(full_config_path),
2834 "%s/%s", vendor_config_path, file_name);
2835 if (F_OK == access(full_config_path, 0)) {
2836 strlcpy(file_name, full_config_path, MIXER_PATH_MAX_LENGTH);
2837 return true;
Aalique Grahame22e49102018-12-18 14:23:57 -08002838 }
2839 return false;
2840}
2841
2842/* platform_info_file should be size 'MIXER_PATH_MAX_LENGTH' */
2843int audio_extn_utils_get_platform_info(const char* snd_card_name, char* platform_info_file)
2844{
2845 if (NULL == snd_card_name) {
2846 return -1;
2847 }
2848
2849 struct snd_card_split *snd_split_handle = NULL;
2850 int ret = 0;
2851 audio_extn_set_snd_card_split(snd_card_name);
2852 snd_split_handle = audio_extn_get_snd_card_split();
2853
2854 snprintf(platform_info_file, MIXER_PATH_MAX_LENGTH, "%s_%s_%s.xml",
2855 PLATFORM_INFO_XML_BASE_STRING, snd_split_handle->snd_card,
2856 snd_split_handle->form_factor);
2857
2858 if (!audio_extn_utils_resolve_config_file(platform_info_file)) {
2859 memset(platform_info_file, 0, MIXER_PATH_MAX_LENGTH);
2860 snprintf(platform_info_file, MIXER_PATH_MAX_LENGTH, "%s_%s.xml",
2861 PLATFORM_INFO_XML_BASE_STRING, snd_split_handle->snd_card);
2862
2863 if (!audio_extn_utils_resolve_config_file(platform_info_file)) {
2864 memset(platform_info_file, 0, MIXER_PATH_MAX_LENGTH);
2865 strlcpy(platform_info_file, PLATFORM_INFO_XML_PATH, MIXER_PATH_MAX_LENGTH);
2866 ret = audio_extn_utils_resolve_config_file(platform_info_file) ? 0 : -1;
2867 }
2868 }
2869
2870 return ret;
2871}
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002872
2873int audio_extn_utils_get_snd_card_num()
2874{
Soumya Managoli9fee7c62018-04-06 16:21:50 +05302875 int snd_card_num = 0;
2876 struct mixer *mixer = NULL;
2877
2878 snd_card_num = audio_extn_utils_open_snd_mixer(&mixer);
2879 if (mixer)
2880 mixer_close(mixer);
2881 return snd_card_num;
2882}
2883
2884int audio_extn_utils_open_snd_mixer(struct mixer **mixer_handle)
2885{
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002886
2887 void *hw_info = NULL;
2888 struct mixer *mixer = NULL;
2889 int retry_num = 0;
mpangfaa7bae2019-01-15 16:38:13 +08002890 int snd_card_num = 0;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002891 char* snd_card_name = NULL;
mpangfaa7bae2019-01-15 16:38:13 +08002892 int snd_card_detection_info[MAX_SND_CARD] = {0};
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002893
Soumya Managoli9fee7c62018-04-06 16:21:50 +05302894 if (!mixer_handle) {
2895 ALOGE("invalid mixer handle");
2896 return -1;
2897 }
2898 *mixer_handle = NULL;
Ashish Jain30ae8942017-11-05 17:21:23 +05302899 /*
mpangfaa7bae2019-01-15 16:38:13 +08002900 * Try with all the sound cards ( 0 to 7 ) and if none of them were detected
Ashish Jain30ae8942017-11-05 17:21:23 +05302901 * sleep for 1 sec and try detections with sound card 0 again.
2902 * If sound card gets detected, check if it is relevant, if not check with the
2903 * other sound cards. To ensure that the irrelevant sound card is not check again,
2904 * we maintain it in min_snd_card_num.
2905 */
2906 while (retry_num < RETRY_NUMBER) {
mpangfaa7bae2019-01-15 16:38:13 +08002907 snd_card_num = 0;
2908 while (snd_card_num < MAX_SND_CARD) {
2909 if (snd_card_detection_info[snd_card_num] == 0) {
2910 mixer = mixer_open(snd_card_num);
2911 if (!mixer)
2912 snd_card_num++;
2913 else
2914 break;
2915 } else
2916 snd_card_num++;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002917 }
2918
2919 if (!mixer) {
Ashish Jain30ae8942017-11-05 17:21:23 +05302920 usleep(RETRY_US);
Ashish Jain30ae8942017-11-05 17:21:23 +05302921 retry_num++;
mpangfaa7bae2019-01-15 16:38:13 +08002922 ALOGD("%s: retry, retry_num %d", __func__, retry_num);
Ashish Jain30ae8942017-11-05 17:21:23 +05302923 continue;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002924 }
2925
2926 snd_card_name = strdup(mixer_get_name(mixer));
2927 if (!snd_card_name) {
2928 ALOGE("failed to allocate memory for snd_card_name\n");
2929 mixer_close(mixer);
2930 return -1;
2931 }
2932 ALOGD("%s: snd_card_name: %s", __func__, snd_card_name);
mpangfaa7bae2019-01-15 16:38:13 +08002933 snd_card_detection_info[snd_card_num] = 1;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002934 hw_info = hw_info_init(snd_card_name);
2935 if (hw_info) {
2936 ALOGD("%s: Opened sound card:%d", __func__, snd_card_num);
2937 break;
2938 }
mpangfaa7bae2019-01-15 16:38:13 +08002939 ALOGE("%s: Failed to init hardware info, snd_card_num:%d", __func__, snd_card_num);
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002940
Dhananjay Kumara7e27832017-07-11 15:40:39 +05302941 free(snd_card_name);
2942 snd_card_name = NULL;
2943
2944 mixer_close(mixer);
2945 mixer = NULL;
2946 }
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002947 if (snd_card_name)
2948 free(snd_card_name);
Ashish Jain30ae8942017-11-05 17:21:23 +05302949
Dhananjay Kumara7e27832017-07-11 15:40:39 +05302950 if (hw_info)
2951 hw_info_deinit(hw_info);
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002952
mpangfaa7bae2019-01-15 16:38:13 +08002953 if (retry_num >= RETRY_NUMBER) {
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002954 ALOGE("%s: Unable to find correct sound card, aborting.", __func__);
2955 return -1;
2956 }
2957
Soumya Managoli9fee7c62018-04-06 16:21:50 +05302958 if (mixer)
2959 *mixer_handle = mixer;
2960
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002961 return snd_card_num;
2962}
Naresh Tanniru6160c712017-04-17 15:43:48 +05302963
Soumya Managoli9fee7c62018-04-06 16:21:50 +05302964void audio_extn_utils_close_snd_mixer(struct mixer *mixer)
2965{
2966 if (mixer)
2967 mixer_close(mixer);
2968}
2969
Naresh Tanniru6160c712017-04-17 15:43:48 +05302970#ifdef SNDRV_COMPRESS_ENABLE_ADJUST_SESSION_CLOCK
2971int audio_extn_utils_compress_enable_drift_correction(
2972 struct stream_out *out,
2973 struct audio_out_enable_drift_correction *drift)
2974{
2975 struct snd_compr_metadata metadata;
2976 int ret = -EINVAL;
2977
2978 if(drift == NULL) {
2979 ALOGE("%s:: Invalid param", __func__);
2980 goto exit;
2981 }
2982
2983 ALOGD("%s:: drift enable %d", __func__,drift->enable);
2984
2985 if (!is_offload_usecase(out->usecase)) {
2986 ALOGE("%s:: not supported for non offload session", __func__);
2987 goto exit;
2988 }
2989
2990 if (!out->compr) {
2991 ALOGW("%s:: offload session not yet opened,"
2992 "start delay will be configure later", __func__);
2993 goto exit;
2994 }
2995
2996 metadata.key = SNDRV_COMPRESS_ENABLE_ADJUST_SESSION_CLOCK;
2997 metadata.value[0] = drift->enable;
2998 out->drift_correction_enabled = drift->enable;
2999
3000 ret = compress_set_metadata(out->compr, &metadata);
3001 if(ret) {
3002 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
3003 out->drift_correction_enabled = false;
3004 }
3005
3006exit:
3007 return ret;
3008}
3009#else
3010int audio_extn_utils_compress_enable_drift_correction(
3011 struct stream_out *out __unused,
3012 struct audio_out_enable_drift_correction *drift __unused)
3013{
3014 ALOGD("%s:: configuring drift enablement not supported", __func__);
3015 return 0;
3016}
3017#endif
3018
3019#ifdef SNDRV_COMPRESS_ADJUST_SESSION_CLOCK
3020int audio_extn_utils_compress_correct_drift(
3021 struct stream_out *out,
3022 struct audio_out_correct_drift *drift_param)
3023{
3024 struct snd_compr_metadata metadata;
3025 int ret = -EINVAL;
3026
3027 if (drift_param == NULL) {
3028 ALOGE("%s:: Invalid drift_param", __func__);
3029 goto exit;
3030 }
3031
3032 ALOGD("%s:: adjust time 0x%"PRIx64" ", __func__,
3033 drift_param->adjust_time);
3034
3035 if (!is_offload_usecase(out->usecase)) {
3036 ALOGE("%s:: not supported for non offload session", __func__);
3037 goto exit;
3038 }
3039
3040 if (!out->compr) {
3041 ALOGW("%s:: offload session not yet opened", __func__);
3042 goto exit;
3043 }
3044
3045 if (!out->drift_correction_enabled) {
3046 ALOGE("%s:: drift correction not enabled", __func__);
3047 goto exit;
3048 }
3049
3050 metadata.key = SNDRV_COMPRESS_ADJUST_SESSION_CLOCK;
3051 metadata.value[0] = 0xFFFFFFFF & drift_param->adjust_time; /* lsb */
3052 metadata.value[1] = \
3053 (0xFFFFFFFF00000000 & drift_param->adjust_time) >> 32; /* msb*/
3054
3055 ret = compress_set_metadata(out->compr, &metadata);
3056 if(ret)
3057 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
3058exit:
3059 return ret;
3060}
3061#else
3062int audio_extn_utils_compress_correct_drift(
3063 struct stream_out *out __unused,
3064 struct audio_out_correct_drift *drift_param __unused)
3065{
3066 ALOGD("%s:: setting adjust clock not supported", __func__);
3067 return 0;
3068}
3069#endif
Naresh Tanniru29bce4e2017-04-27 17:54:30 +05303070
3071int audio_extn_utils_set_channel_map(
3072 struct stream_out *out,
3073 struct audio_out_channel_map_param *channel_map_param)
3074{
3075 int ret = -EINVAL, i = 0;
3076 int channels = audio_channel_count_from_out_mask(out->channel_mask);
3077
3078 if (channel_map_param == NULL) {
3079 ALOGE("%s:: Invalid channel_map", __func__);
3080 goto exit;
3081 }
3082
3083 if (channel_map_param->channels != channels) {
3084 ALOGE("%s:: Channels(%d) does not match stream channels(%d)",
3085 __func__, channel_map_param->channels, channels);
3086 goto exit;
3087 }
3088
3089 for ( i = 0; i < channels; i++) {
3090 ALOGV("%s:: channel_map[%d]- %d", __func__, i, channel_map_param->channel_map[i]);
3091 out->channel_map_param.channel_map[i] = channel_map_param->channel_map[i];
3092 }
3093 ret = 0;
3094exit:
3095 return ret;
3096}
Varun Balaraje49253e2017-07-06 19:48:56 +05303097
3098int audio_extn_utils_set_pan_scale_params(
3099 struct stream_out *out,
3100 struct mix_matrix_params *mm_params)
3101{
3102 int ret = -EINVAL, i = 0, j = 0;
3103
Varun Balaraj003ee752017-08-07 17:54:55 +05303104 if (mm_params == NULL || out == NULL) {
3105 ALOGE("%s:: Invalid mix matrix or out param", __func__);
Varun Balaraje49253e2017-07-06 19:48:56 +05303106 goto exit;
3107 }
3108
3109 if (mm_params->num_output_channels > MAX_CHANNELS_SUPPORTED ||
3110 mm_params->num_output_channels <= 0 ||
3111 mm_params->num_input_channels > MAX_CHANNELS_SUPPORTED ||
3112 mm_params->num_input_channels <= 0)
3113 goto exit;
3114
3115 out->pan_scale_params.num_output_channels = mm_params->num_output_channels;
3116 out->pan_scale_params.num_input_channels = mm_params->num_input_channels;
3117 out->pan_scale_params.has_output_channel_map =
3118 mm_params->has_output_channel_map;
3119 for (i = 0; i < mm_params->num_output_channels; i++)
3120 out->pan_scale_params.output_channel_map[i] =
3121 mm_params->output_channel_map[i];
3122
3123 out->pan_scale_params.has_input_channel_map =
3124 mm_params->has_input_channel_map;
3125 for (i = 0; i < mm_params->num_input_channels; i++)
3126 out->pan_scale_params.input_channel_map[i] =
3127 mm_params->input_channel_map[i];
3128
3129 out->pan_scale_params.has_mixer_coeffs = mm_params->has_mixer_coeffs;
3130 for (i = 0; i < mm_params->num_output_channels; i++)
3131 for (j = 0; j < mm_params->num_input_channels; j++) {
3132 //Convert the channel coefficient gains in Q14 format
3133 out->pan_scale_params.mixer_coeffs[i][j] =
3134 mm_params->mixer_coeffs[i][j] * (2 << 13);
3135 }
3136
3137 ret = platform_set_stream_pan_scale_params(out->dev->platform,
3138 out->pcm_device_id,
3139 out->pan_scale_params);
3140
3141exit:
3142 return ret;
3143}
3144
3145int audio_extn_utils_set_downmix_params(
3146 struct stream_out *out,
3147 struct mix_matrix_params *mm_params)
3148{
3149 int ret = -EINVAL, i = 0, j = 0;
3150 struct audio_usecase *usecase = NULL;
3151
Aniket Kumar Lataf9f246e2017-09-15 15:20:16 -07003152 if (mm_params == NULL || out == NULL) {
Varun Balaraj003ee752017-08-07 17:54:55 +05303153 ALOGE("%s:: Invalid mix matrix or out param", __func__);
Varun Balaraje49253e2017-07-06 19:48:56 +05303154 goto exit;
3155 }
3156
3157 if (mm_params->num_output_channels > MAX_CHANNELS_SUPPORTED ||
3158 mm_params->num_output_channels <= 0 ||
3159 mm_params->num_input_channels > MAX_CHANNELS_SUPPORTED ||
3160 mm_params->num_input_channels <= 0)
3161 goto exit;
3162
3163 usecase = get_usecase_from_list(out->dev, out->usecase);
Varun Balaraj003ee752017-08-07 17:54:55 +05303164 if (!usecase) {
3165 ALOGE("%s: Get usecase list failed!", __func__);
Aniket Kumar Lataf9f246e2017-09-15 15:20:16 -07003166 goto exit;
3167 }
Varun Balaraje49253e2017-07-06 19:48:56 +05303168 out->downmix_params.num_output_channels = mm_params->num_output_channels;
3169 out->downmix_params.num_input_channels = mm_params->num_input_channels;
3170
3171 out->downmix_params.has_output_channel_map =
3172 mm_params->has_output_channel_map;
3173 for (i = 0; i < mm_params->num_output_channels; i++) {
3174 out->downmix_params.output_channel_map[i] =
3175 mm_params->output_channel_map[i];
3176 }
3177
3178 out->downmix_params.has_input_channel_map =
3179 mm_params->has_input_channel_map;
3180 for (i = 0; i < mm_params->num_input_channels; i++)
3181 out->downmix_params.input_channel_map[i] =
3182 mm_params->input_channel_map[i];
3183
3184 out->downmix_params.has_mixer_coeffs = mm_params->has_mixer_coeffs;
3185 for (i = 0; i < mm_params->num_output_channels; i++)
Nikhil Latukar2e6f6242017-08-15 13:37:07 +05303186 for (j = 0; j < mm_params->num_input_channels; j++) {
3187 //Convert the channel coefficient gains in Q14 format
Varun Balaraje49253e2017-07-06 19:48:56 +05303188 out->downmix_params.mixer_coeffs[i][j] =
Nikhil Latukar2e6f6242017-08-15 13:37:07 +05303189 mm_params->mixer_coeffs[i][j] * (2 << 13);
3190 }
Varun Balaraje49253e2017-07-06 19:48:56 +05303191
3192 ret = platform_set_stream_downmix_params(out->dev->platform,
3193 out->pcm_device_id,
3194 usecase->out_snd_device,
3195 out->downmix_params);
3196
3197exit:
3198 return ret;
3199}
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05303200
3201bool audio_extn_utils_is_dolby_format(audio_format_t format)
3202{
3203 if (format == AUDIO_FORMAT_AC3 ||
3204 format == AUDIO_FORMAT_E_AC3 ||
3205 format == AUDIO_FORMAT_E_AC3_JOC)
3206 return true;
3207 else
3208 return false;
3209}
3210
`Deeraj Soman676c2702017-09-18 19:25:53 +05303211int audio_extn_utils_get_bit_width_from_string(const char *id_string)
3212{
3213 int i;
3214 const mixer_config_lookup mixer_bitwidth_config[] = {{"S24_3LE", 24},
3215 {"S32_LE", 32},
3216 {"S24_LE", 24},
3217 {"S16_LE", 16}};
3218 int num_configs = sizeof(mixer_bitwidth_config) / sizeof(mixer_bitwidth_config[0]);
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05303219
`Deeraj Soman676c2702017-09-18 19:25:53 +05303220 for (i = 0; i < num_configs; i++) {
3221 if (!strcmp(id_string, mixer_bitwidth_config[i].id_string))
3222 return mixer_bitwidth_config[i].value;
3223 }
3224
3225 return -EINVAL;
3226}
3227
3228int audio_extn_utils_get_sample_rate_from_string(const char *id_string)
3229{
3230 int i;
Weiyin Jiang30f9ab52020-11-11 11:37:21 +08003231 const mixer_config_lookup mixer_samplerate_config[] = {{"KHZ_8", 8000},
3232 {"KHZ_11P025", 11025},
3233 {"KHZ_16", 16000},
3234 {"KHZ_22P05", 22050},
3235 {"KHZ_32", 32000},
`Deeraj Soman676c2702017-09-18 19:25:53 +05303236 {"KHZ_48", 48000},
3237 {"KHZ_96", 96000},
3238 {"KHZ_144", 144000},
3239 {"KHZ_192", 192000},
3240 {"KHZ_384", 384000},
3241 {"KHZ_44P1", 44100},
3242 {"KHZ_88P2", 88200},
3243 {"KHZ_176P4", 176400},
3244 {"KHZ_352P8", 352800}};
3245 int num_configs = sizeof(mixer_samplerate_config) / sizeof(mixer_samplerate_config[0]);
3246
3247 for (i = 0; i < num_configs; i++) {
3248 if (!strcmp(id_string, mixer_samplerate_config[i].id_string))
3249 return mixer_samplerate_config[i].value;
3250 }
3251
3252 return -EINVAL;
3253}
3254
3255int audio_extn_utils_get_channels_from_string(const char *id_string)
3256{
3257 int i;
3258 const mixer_config_lookup mixer_channels_config[] = {{"One", 1},
3259 {"Two", 2},
3260 {"Three",3},
3261 {"Four", 4},
3262 {"Five", 5},
3263 {"Six", 6},
3264 {"Seven", 7},
3265 {"Eight", 8}};
3266 int num_configs = sizeof(mixer_channels_config) / sizeof(mixer_channels_config[0]);
3267
3268 for (i = 0; i < num_configs; i++) {
3269 if (!strcmp(id_string, mixer_channels_config[i].id_string))
3270 return mixer_channels_config[i].value;
3271 }
3272
3273 return -EINVAL;
3274}
Surendar karka30569792018-05-08 12:02:21 +05303275
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08003276void audio_extn_utils_release_snd_device(snd_device_t snd_device)
3277{
3278 audio_extn_dev_arbi_release(snd_device);
3279 audio_extn_sound_trigger_update_device_status(snd_device,
3280 ST_EVENT_SND_DEVICE_FREE);
3281 audio_extn_listen_update_device_status(snd_device,
3282 LISTEN_EVENT_SND_DEVICE_FREE);
3283}
3284
Weiyin Jiangd5718bd2019-09-04 16:52:08 +08003285int audio_extn_utils_get_license_params(
3286 const struct audio_device *adev,
3287 struct audio_license_params *license_params)
Surendar karka30569792018-05-08 12:02:21 +05303288{
3289 if(!license_params)
3290 return -EINVAL;
Weiyin Jiangd5718bd2019-09-04 16:52:08 +08003291
3292 return platform_get_license_by_product(adev->platform,
3293 (const char*)license_params->product, &license_params->key, license_params->license);
Surendar karka30569792018-05-08 12:02:21 +05303294}
3295
Aalique Grahame22e49102018-12-18 14:23:57 -08003296int audio_extn_utils_send_app_type_gain(struct audio_device *adev,
3297 int app_type,
3298 int *gain)
3299{
3300 int gain_cfg[4];
3301 const char *mixer_ctl_name = "App Type Gain";
3302 struct mixer_ctl *ctl;
3303 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
3304 if (!ctl) {
3305 ALOGE("%s: Could not get volume ctl mixer %s", __func__,
3306 mixer_ctl_name);
3307 return -EINVAL;
3308 }
3309 gain_cfg[0] = 0;
3310 gain_cfg[1] = app_type;
3311 gain_cfg[2] = gain[0];
3312 gain_cfg[3] = gain[1];
3313 ALOGV("%s app_type %d l(%d) r(%d)", __func__, app_type, gain[0], gain[1]);
3314 return mixer_ctl_set_array(ctl, gain_cfg,
3315 sizeof(gain_cfg)/sizeof(gain_cfg[0]));
3316}
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -07003317
Weiyin Jiangd5718bd2019-09-04 16:52:08 +08003318static void vndk_fwk_init()
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -07003319{
Weiyin Jiangd5718bd2019-09-04 16:52:08 +08003320 if (mVndkFwk.lib_handle != NULL)
3321 return;
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -07003322
Weiyin Jiangd5718bd2019-09-04 16:52:08 +08003323 mVndkFwk.lib_handle = dlopen(VNDK_FWK_LIB_PATH, RTLD_NOW);
3324 if (mVndkFwk.lib_handle == NULL) {
3325 ALOGW("%s: failed to dlopen VNDK_FWK_LIB %s", __func__, strerror(errno));
3326 return;
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -07003327 }
3328
Weiyin Jiangd5718bd2019-09-04 16:52:08 +08003329 *(void **)(&mVndkFwk.isVendorEnhancedFwk) =
3330 dlsym(mVndkFwk.lib_handle, "isRunningWithVendorEnhancedFramework");
3331 if (mVndkFwk.isVendorEnhancedFwk == NULL) {
3332 ALOGW("%s: dlsym failed %s", __func__, strerror(errno));
3333 if (mVndkFwk.lib_handle) {
3334 dlclose(mVndkFwk.lib_handle);
3335 mVndkFwk.lib_handle = NULL;
3336 }
3337 return;
3338 }
3339
3340
3341 *(void **)(&mVndkFwk.getVendorEnhancedInfo) =
3342 dlsym(mVndkFwk.lib_handle, "getVendorEnhancedInfo");
3343 if (mVndkFwk.getVendorEnhancedInfo == NULL) {
3344 ALOGW("%s: dlsym failed %s", __func__, strerror(errno));
3345 if (mVndkFwk.lib_handle) {
3346 dlclose(mVndkFwk.lib_handle);
3347 mVndkFwk.lib_handle = NULL;
3348 }
3349 }
3350
3351 return;
3352}
3353
3354bool audio_extn_utils_is_vendor_enhanced_fwk()
3355{
3356 static int is_vendor_enhanced_fwk = -EINVAL;
3357 if (is_vendor_enhanced_fwk != -EINVAL)
3358 return (bool)is_vendor_enhanced_fwk;
3359
3360 vndk_fwk_init();
3361
3362 if (mVndkFwk.isVendorEnhancedFwk != NULL) {
3363 is_vendor_enhanced_fwk = mVndkFwk.isVendorEnhancedFwk();
3364 ALOGW("%s: is_vendor_enhanced_fwk %d", __func__, is_vendor_enhanced_fwk);
3365 } else {
3366 is_vendor_enhanced_fwk = 0;
3367 ALOGW("%s: default to non enhanced_fwk config", __func__);
3368 }
3369
3370 return (bool)is_vendor_enhanced_fwk;
3371}
3372
3373int audio_extn_utils_get_vendor_enhanced_info()
3374{
3375 static int vendor_enhanced_info = -EINVAL;
3376 if (vendor_enhanced_info != -EINVAL)
3377 return vendor_enhanced_info;
3378
3379 vndk_fwk_init();
3380
3381 if (mVndkFwk.getVendorEnhancedInfo != NULL) {
3382 vendor_enhanced_info = mVndkFwk.getVendorEnhancedInfo();
3383 ALOGW("%s: vendor_enhanced_info 0x%x", __func__, vendor_enhanced_info);
3384 } else {
3385 vendor_enhanced_info = 0x0;
3386 ALOGW("%s: default to vendor_enhanced_info 0x0", __func__);
3387 }
3388
3389 return vendor_enhanced_info;
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -07003390}
Deeraj Soman14230922019-01-30 16:39:30 +05303391
Deeraj Somanfa377bf2019-02-06 12:57:59 +05303392int audio_extn_utils_get_perf_mode_flag(void)
3393{
3394#ifdef COMPRESSED_PERF_MODE_FLAG
3395 return COMPRESSED_PERF_MODE_FLAG;
3396#else
3397 return 0;
3398#endif
3399}
3400
Deeraj Soman14230922019-01-30 16:39:30 +05303401size_t audio_extn_utils_get_input_buffer_size(uint32_t sample_rate,
3402 audio_format_t format,
3403 int channel_count,
3404 int64_t duration_ms,
3405 bool is_low_latency)
3406{
3407 size_t size = 0;
3408 size_t capture_duration = AUDIO_CAPTURE_PERIOD_DURATION_MSEC;
3409 uint32_t bytes_per_period_sample = 0;
3410
3411
3412 if (audio_extn_utils_check_input_parameters(sample_rate, format, channel_count) != 0)
3413 return 0;
3414
3415 if (duration_ms >= MIN_OFFLOAD_BUFFER_DURATION_MS && duration_ms <= MAX_OFFLOAD_BUFFER_DURATION_MS)
3416 capture_duration = duration_ms;
3417
3418 size = (sample_rate * capture_duration) / 1000;
3419 if (is_low_latency)
3420 size = LOW_LATENCY_CAPTURE_PERIOD_SIZE;
3421
3422
3423 bytes_per_period_sample = audio_bytes_per_sample(format) * channel_count;
3424 size *= bytes_per_period_sample;
3425
3426 /* make sure the size is multiple of 32 bytes and additionally multiple of
3427 * the frame_size (required for 24bit samples and non-power-of-2 channel counts)
3428 * At 48 kHz mono 16-bit PCM:
3429 * 5.000 ms = 240 frames = 15*16*1*2 = 480, a whole multiple of 32 (15)
3430 * 3.333 ms = 160 frames = 10*16*1*2 = 320, a whole multiple of 32 (10)
3431 *
3432 * The loop reaches result within 32 iterations, as initial size is
3433 * already a multiple of frame_size
3434 */
3435 size = audio_extn_utils_nearest_multiple(size, audio_extn_utils_lcm(32, bytes_per_period_sample));
3436
3437 return size;
3438}
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07003439
3440int audio_extn_utils_hash_fn(void *key)
3441{
3442 return (int)key;
3443}
3444
3445bool audio_extn_utils_hash_eq(void *key1, void *key2)
3446{
3447 return (key1 == key2);
3448}