blob: 1f61d6dc94deb75cbaf46830339367d7c8489cf2 [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),
Derek Chen1bcdc6b2020-02-06 22:44:53 -0800153 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_FRONT_PASSENGER),
Derek Chendf05eea2019-08-01 13:57:49 -0700154 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_REAR_SEAT),
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530155 STRING_TO_ENUM(AUDIO_INPUT_FLAG_NONE),
156 STRING_TO_ENUM(AUDIO_INPUT_FLAG_FAST),
157 STRING_TO_ENUM(AUDIO_INPUT_FLAG_HW_HOTWORD),
158 STRING_TO_ENUM(AUDIO_INPUT_FLAG_RAW),
159 STRING_TO_ENUM(AUDIO_INPUT_FLAG_SYNC),
Dhananjay Kumaree4d2002016-10-25 18:02:58 +0530160 STRING_TO_ENUM(AUDIO_INPUT_FLAG_TIMESTAMP),
Dhananjay Kumar704ce6f2017-09-28 22:08:00 +0530161 STRING_TO_ENUM(AUDIO_INPUT_FLAG_COMPRESS),
Ralf Herzaec80262018-07-03 07:08:49 +0200162 STRING_TO_ENUM(AUDIO_INPUT_FLAG_PASSTHROUGH),
George Gao3d477082020-09-17 22:29:07 -0700163 STRING_TO_ENUM(AUDIO_INPUT_FLAG_MMAP_NOIRQ),
164 STRING_TO_ENUM(AUDIO_INPUT_FLAG_VOIP_TX),
165 STRING_TO_ENUM(AUDIO_INPUT_FLAG_HW_AV_SYNC),
166 STRING_TO_ENUM(AUDIO_INPUT_FLAG_DIRECT),
Huicheng Liu1404ba12020-09-11 01:03:25 -0400167 STRING_TO_ENUM(AUDIO_INPUT_FLAG_PRIMARY),
168 STRING_TO_ENUM(AUDIO_INPUT_FLAG_FRONT_PASSENGER),
169 STRING_TO_ENUM(AUDIO_INPUT_FLAG_REAR_SEAT),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700170};
171
172const struct string_to_enum s_format_name_to_enum_table[] = {
Ashish Jain83a6cc22016-06-28 14:34:17 +0530173 STRING_TO_ENUM(AUDIO_FORMAT_PCM_8_BIT),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700174 STRING_TO_ENUM(AUDIO_FORMAT_PCM_16_BIT),
Ashish Jain5106d362016-05-11 19:23:33 +0530175 STRING_TO_ENUM(AUDIO_FORMAT_PCM_24_BIT_PACKED),
176 STRING_TO_ENUM(AUDIO_FORMAT_PCM_8_24_BIT),
Ashish Jainf1eaa582016-05-23 20:54:24 +0530177 STRING_TO_ENUM(AUDIO_FORMAT_PCM_32_BIT),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700178 STRING_TO_ENUM(AUDIO_FORMAT_MP3),
179 STRING_TO_ENUM(AUDIO_FORMAT_AAC),
180 STRING_TO_ENUM(AUDIO_FORMAT_VORBIS),
Mingming Yinae3530f2014-07-03 16:50:18 -0700181 STRING_TO_ENUM(AUDIO_FORMAT_AMR_NB),
182 STRING_TO_ENUM(AUDIO_FORMAT_AMR_WB),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700183 STRING_TO_ENUM(AUDIO_FORMAT_AC3),
Mingming Yinae3530f2014-07-03 16:50:18 -0700184 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700185 STRING_TO_ENUM(AUDIO_FORMAT_DTS),
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530186 STRING_TO_ENUM(AUDIO_FORMAT_DTS_HD),
Ben Romberger1aaaf862017-04-06 17:49:46 -0700187 STRING_TO_ENUM(AUDIO_FORMAT_DOLBY_TRUEHD),
Naresh Tanniru928f0862017-04-07 16:44:23 -0700188 STRING_TO_ENUM(AUDIO_FORMAT_IEC61937),
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530189 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3_JOC),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700190 STRING_TO_ENUM(AUDIO_FORMAT_WMA),
191 STRING_TO_ENUM(AUDIO_FORMAT_WMA_PRO),
192 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADIF),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700193 STRING_TO_ENUM(AUDIO_FORMAT_AMR_WB_PLUS),
194 STRING_TO_ENUM(AUDIO_FORMAT_EVRC),
195 STRING_TO_ENUM(AUDIO_FORMAT_EVRCB),
196 STRING_TO_ENUM(AUDIO_FORMAT_EVRCWB),
197 STRING_TO_ENUM(AUDIO_FORMAT_QCELP),
198 STRING_TO_ENUM(AUDIO_FORMAT_MP2),
199 STRING_TO_ENUM(AUDIO_FORMAT_EVRCNW),
Amit Shekhar6f461b12014-08-01 14:52:58 -0700200 STRING_TO_ENUM(AUDIO_FORMAT_FLAC),
Satya Krishna Pindiproli70471602015-04-24 19:12:43 +0530201 STRING_TO_ENUM(AUDIO_FORMAT_ALAC),
202 STRING_TO_ENUM(AUDIO_FORMAT_APE),
Alexy Josephcd8eaed2014-12-11 12:46:53 -0800203 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LC),
204 STRING_TO_ENUM(AUDIO_FORMAT_AAC_HE_V1),
205 STRING_TO_ENUM(AUDIO_FORMAT_AAC_HE_V2),
Manish Dewangana6fc5442015-08-24 20:30:31 +0530206 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS),
Ashish Jaine513a872015-11-19 17:00:56 +0530207 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_LC),
208 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_HE_V1),
209 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_HE_V2),
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +0530210 STRING_TO_ENUM(AUDIO_FORMAT_DSD),
Arun Kumar Dasari3b174182016-12-27 13:01:14 +0530211 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM),
212 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM_LC),
213 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM_HE_V1),
214 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM_HE_V2),
Dhanalakshmi Siddani18737932016-11-29 17:33:17 +0530215 STRING_TO_ENUM(AUDIO_FORMAT_APTX),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700216};
217
Manish Dewangan3ccdea52017-02-13 19:31:54 +0530218/* payload structure avt_device drift query */
219struct audio_avt_device_drift_stats {
220 uint32_t minor_version;
Manish Dewangan338c50a2017-09-12 15:22:03 +0530221
Manish Dewangan3ccdea52017-02-13 19:31:54 +0530222 /* Indicates the device interface direction as either
223 * source (Tx) or sink (Rx).
224 */
225 uint16_t device_direction;
Manish Dewangan338c50a2017-09-12 15:22:03 +0530226
227 /* Reference timer for drift accumulation and time stamp information.
228 * currently it only support AFE_REF_TIMER_TYPE_AVTIMER
229 */
230 uint16_t reference_timer;
Manish Dewangan3ccdea52017-02-13 19:31:54 +0530231 struct audio_avt_device_drift_param drift_param;
Manish Dewangan338c50a2017-09-12 15:22:03 +0530232} __attribute__((packed));
Manish Dewangan3ccdea52017-02-13 19:31:54 +0530233
Ben Rombergera04fabc2014-11-14 12:16:03 -0800234static char bTable[BASE_TABLE_SIZE] = {
235 'A','B','C','D','E','F','G','H','I','J','K','L',
236 'M','N','O','P','Q','R','S','T','U','V','W','X',
237 'Y','Z','a','b','c','d','e','f','g','h','i','j',
238 'k','l','m','n','o','p','q','r','s','t','u','v',
239 'w','x','y','z','0','1','2','3','4','5','6','7',
240 '8','9','+','/'
241};
242
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700243static uint32_t string_to_enum(const struct string_to_enum *table, size_t size,
244 const char *name)
245{
246 size_t i;
247 for (i = 0; i < size; i++) {
248 if (strcmp(table[i].name, name) == 0) {
249 ALOGV("%s found %s", __func__, table[i].name);
250 return table[i].value;
251 }
252 }
George Gao3d477082020-09-17 22:29:07 -0700253 ALOGE("%s cound not find %s", __func__, name);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700254 return 0;
255}
256
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530257static audio_io_flags_t parse_flag_names(char *name)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700258{
259 uint32_t flag = 0;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530260 audio_io_flags_t io_flags;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800261 char *last_r;
262 char *flag_name = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700263 while (flag_name != NULL) {
264 if (strlen(flag_name) != 0) {
265 flag |= string_to_enum(s_flag_name_to_enum_table,
266 ARRAY_SIZE(s_flag_name_to_enum_table),
267 flag_name);
268 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800269 flag_name = strtok_r(NULL, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700270 }
271
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800272 ALOGV("parse_flag_names: flag - %x", flag);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530273 io_flags.in_flags = (audio_input_flags_t)flag;
274 io_flags.out_flags = (audio_output_flags_t)flag;
275 return io_flags;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700276}
277
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530278static void parse_format_names(char *name, struct streams_io_cfg *s_info)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700279{
280 struct stream_format *sf_info = NULL;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800281 char *last_r;
282 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700283
284 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG) == 0)
285 return;
286
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530287 list_init(&s_info->format_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700288 while (str != NULL) {
289 audio_format_t format = (audio_format_t)string_to_enum(s_format_name_to_enum_table,
290 ARRAY_SIZE(s_format_name_to_enum_table), str);
291 ALOGV("%s: format - %d", __func__, format);
292 if (format != 0) {
293 sf_info = (struct stream_format *)calloc(1, sizeof(struct stream_format));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700294 if (sf_info == NULL)
295 break; /* return whatever was parsed */
296
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700297 sf_info->format = format;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530298 list_add_tail(&s_info->format_list, &sf_info->list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700299 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800300 str = strtok_r(NULL, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700301 }
302}
303
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530304static void parse_sample_rate_names(char *name, struct streams_io_cfg *s_info)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700305{
Amit Shekhar6f461b12014-08-01 14:52:58 -0700306 struct stream_sample_rate *ss_info = NULL;
307 uint32_t sample_rate = 48000;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800308 char *last_r;
309 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700310
Amit Shekhar6f461b12014-08-01 14:52:58 -0700311 if (str != NULL && 0 == strcmp(str, DYNAMIC_VALUE_TAG))
312 return;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700313
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530314 list_init(&s_info->sample_rate_list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700315 while (str != NULL) {
316 sample_rate = (uint32_t)strtol(str, (char **)NULL, 10);
317 ALOGV("%s: sample_rate - %d", __func__, sample_rate);
318 if (0 != sample_rate) {
319 ss_info = (struct stream_sample_rate *)calloc(1, sizeof(struct stream_sample_rate));
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800320 if (!ss_info) {
321 ALOGE("%s: memory allocation failure", __func__);
322 return;
323 }
Amit Shekhar6f461b12014-08-01 14:52:58 -0700324 ss_info->sample_rate = sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530325 list_add_tail(&s_info->sample_rate_list, &ss_info->list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700326 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800327 str = strtok_r(NULL, "|", &last_r);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700328 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700329}
330
331static int parse_bit_width_names(char *name)
332{
333 int bit_width = 16;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800334 char *last_r;
335 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700336
337 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG))
338 bit_width = (int)strtol(str, (char **)NULL, 10);
339
340 ALOGV("%s: bit_width - %d", __func__, bit_width);
341 return bit_width;
342}
343
344static int parse_app_type_names(void *platform, char *name)
345{
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700346 int app_type = platform_get_default_app_type(platform);
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800347 char *last_r;
348 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700349
350 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG))
351 app_type = (int)strtol(str, (char **)NULL, 10);
352
353 ALOGV("%s: app_type - %d", __func__, app_type);
354 return app_type;
355}
356
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530357static void update_streams_cfg_list(cnode *root, void *platform,
358 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700359{
360 cnode *node = root->first_child;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530361 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700362
363 ALOGV("%s", __func__);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530364 s_info = (struct streams_io_cfg *)calloc(1, sizeof(struct streams_io_cfg));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700365
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530366 if (!s_info) {
367 ALOGE("failed to allocate mem for s_info list element");
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700368 return;
369 }
370
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700371 while (node) {
372 if (strcmp(node->name, FLAGS_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530373 s_info->flags = parse_flag_names((char *)node->value);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530374 } else if (strcmp(node->name, PROFILES_TAG) == 0) {
375 strlcpy(s_info->profile, (char *)node->value, sizeof(s_info->profile));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700376 } else if (strcmp(node->name, FORMATS_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530377 parse_format_names((char *)node->value, s_info);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700378 } else if (strcmp(node->name, SAMPLING_RATES_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530379 s_info->app_type_cfg.sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
380 parse_sample_rate_names((char *)node->value, s_info);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700381 } else if (strcmp(node->name, BIT_WIDTH_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530382 s_info->app_type_cfg.bit_width = parse_bit_width_names((char *)node->value);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700383 } else if (strcmp(node->name, APP_TYPE_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530384 s_info->app_type_cfg.app_type = parse_app_type_names(platform, (char *)node->value);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700385 }
386 node = node->next;
387 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530388 list_add_tail(streams_cfg_list, &s_info->list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700389}
390
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530391static void load_cfg_list(cnode *root, void *platform,
392 struct listnode *streams_output_cfg_list,
393 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700394{
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530395 cnode *node = NULL;
396
397 node = config_find(root, OUTPUTS_TAG);
398 if (node != NULL) {
399 node = node->first_child;
400 while (node) {
401 ALOGV("%s: loading output %s", __func__, node->name);
402 update_streams_cfg_list(node, platform, streams_output_cfg_list);
403 node = node->next;
404 }
405 } else {
406 ALOGI("%s: could not load output, node is NULL", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700407 }
408
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530409 node = config_find(root, INPUTS_TAG);
410 if (node != NULL) {
411 node = node->first_child;
412 while (node) {
413 ALOGV("%s: loading input %s", __func__, node->name);
414 update_streams_cfg_list(node, platform, streams_input_cfg_list);
415 node = node->next;
416 }
417 } else {
418 ALOGI("%s: could not load input, node is NULL", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700419 }
420}
421
422static void send_app_type_cfg(void *platform, struct mixer *mixer,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530423 struct listnode *streams_output_cfg_list,
424 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700425{
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530426 size_t app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT] = {0};
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700427 int length = 0, i, num_app_types = 0;
428 struct listnode *node;
429 bool update;
430 struct mixer_ctl *ctl = NULL;
431 const char *mixer_ctl_name = "App Type Config";
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530432 struct streams_io_cfg *s_info = NULL;
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800433 uint32_t target_bit_width = 0;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700434
435 if (!mixer) {
436 ALOGE("%s: mixer is null",__func__);
437 return;
438 }
439 ctl = mixer_get_ctl_by_name(mixer, mixer_ctl_name);
440 if (!ctl) {
441 ALOGE("%s: Could not get ctl for mixer cmd - %s",__func__, mixer_ctl_name);
442 return;
443 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530444 app_type_cfg[length++] = num_app_types;
445
446 if (list_empty(streams_output_cfg_list)) {
447 app_type_cfg[length++] = platform_get_default_app_type_v2(platform, PCM_PLAYBACK);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700448 app_type_cfg[length++] = 48000;
449 app_type_cfg[length++] = 16;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530450 num_app_types += 1;
451 }
452 if (list_empty(streams_input_cfg_list)) {
453 app_type_cfg[length++] = platform_get_default_app_type_v2(platform, PCM_CAPTURE);
454 app_type_cfg[length++] = 48000;
455 app_type_cfg[length++] = 16;
456 num_app_types += 1;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700457 }
458
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800459 /* get target bit width for ADM enforce mode */
460 target_bit_width = adev_get_dsp_bit_width_enforce_mode();
461
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700462 list_for_each(node, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530463 s_info = node_to_item(node, struct streams_io_cfg, list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700464 update = true;
465 for (i=0; i<length; i=i+3) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530466 if (app_type_cfg[i+1] == 0)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700467 break;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530468 else if (app_type_cfg[i+1] == (size_t)s_info->app_type_cfg.app_type) {
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530469 if (app_type_cfg[i+2] < (size_t)s_info->app_type_cfg.sample_rate)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530470 app_type_cfg[i+2] = s_info->app_type_cfg.sample_rate;
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530471 if (app_type_cfg[i+3] < (size_t)s_info->app_type_cfg.bit_width)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530472 app_type_cfg[i+3] = s_info->app_type_cfg.bit_width;
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800473 /* ADM bit width = max(enforce_bit_width, bit_width from s_info */
474 if (audio_extn_is_dsp_bit_width_enforce_mode_supported(s_info->flags.out_flags) &&
475 (target_bit_width > app_type_cfg[i+3]))
476 app_type_cfg[i+3] = target_bit_width;
477
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700478 update = false;
479 break;
480 }
481 }
482 if (update && ((length + 3) <= MAX_LENGTH_MIXER_CONTROL_IN_INT)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530483 num_app_types += 1;
484 app_type_cfg[length++] = s_info->app_type_cfg.app_type;
485 app_type_cfg[length++] = s_info->app_type_cfg.sample_rate;
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800486 app_type_cfg[length] = s_info->app_type_cfg.bit_width;
487 if (audio_extn_is_dsp_bit_width_enforce_mode_supported(s_info->flags.out_flags) &&
488 (target_bit_width > app_type_cfg[length]))
489 app_type_cfg[length] = target_bit_width;
490
491 length++;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530492 }
493 }
494 list_for_each(node, streams_input_cfg_list) {
495 s_info = node_to_item(node, struct streams_io_cfg, list);
496 update = true;
497 for (i=0; i<length; i=i+3) {
498 if (app_type_cfg[i+1] == 0)
499 break;
500 else if (app_type_cfg[i+1] == (size_t)s_info->app_type_cfg.app_type) {
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530501 if (app_type_cfg[i+2] < (size_t)s_info->app_type_cfg.sample_rate)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530502 app_type_cfg[i+2] = s_info->app_type_cfg.sample_rate;
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530503 if (app_type_cfg[i+3] < (size_t)s_info->app_type_cfg.bit_width)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530504 app_type_cfg[i+3] = s_info->app_type_cfg.bit_width;
505 update = false;
506 break;
507 }
508 }
509 if (update && ((length + 3) <= MAX_LENGTH_MIXER_CONTROL_IN_INT)) {
510 num_app_types += 1;
511 app_type_cfg[length++] = s_info->app_type_cfg.app_type;
512 app_type_cfg[length++] = s_info->app_type_cfg.sample_rate;
513 app_type_cfg[length++] = s_info->app_type_cfg.bit_width;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700514 }
515 }
516 ALOGV("%s: num_app_types: %d", __func__, num_app_types);
517 if (num_app_types) {
518 app_type_cfg[0] = num_app_types;
519 mixer_ctl_set_array(ctl, app_type_cfg, length);
520 }
521}
522
Roopesh Nataraja0f68be72020-04-28 12:32:02 -0700523/* Function to retrieve audio vendor configs path */
524void audio_get_vendor_config_path (char* config_file_path, int path_size)
525{
526 char vendor_sku[PROPERTY_VALUE_MAX] = {'\0'};
527 if (property_get("ro.boot.product.vendor.sku", vendor_sku, "") <= 0) {
528#ifdef LINUX_ENABLED
529 /* Audio configs are stored in /etc */
530 snprintf(config_file_path, path_size, "%s", "/etc");
531#else
532 /* Audio configs are stored in /vendor/etc */
533 snprintf(config_file_path, path_size, "%s", "/vendor/etc");
534#endif
535 } else {
536 /* Audio configs are stored in /vendor/etc/audio/sku_${vendor_sku} */
537 snprintf(config_file_path, path_size,
538 "%s%s", "/vendor/etc/audio/sku_", vendor_sku);
539 }
540}
541
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530542void audio_extn_utils_update_streams_cfg_lists(void *platform,
543 struct mixer *mixer,
544 struct listnode *streams_output_cfg_list,
545 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700546{
547 cnode *root;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530548 char *data = NULL;
Roopesh Nataraja0f68be72020-04-28 12:32:02 -0700549 char vendor_config_path[VENDOR_CONFIG_PATH_MAX_LENGTH];
550 char audio_io_policy_file[VENDOR_CONFIG_FILE_MAX_LENGTH];
551 char audio_output_policy_file[VENDOR_CONFIG_FILE_MAX_LENGTH];
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700552
553 ALOGV("%s", __func__);
554 list_init(streams_output_cfg_list);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530555 list_init(streams_input_cfg_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700556
557 root = config_node("", "");
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700558 if (root == NULL) {
559 ALOGE("cfg_list, NULL config root");
560 return;
561 }
562
Roopesh Nataraja0f68be72020-04-28 12:32:02 -0700563 /* Get path for audio configuration files in vendor */
564 audio_get_vendor_config_path(vendor_config_path,
565 sizeof(vendor_config_path));
566
567 /* Get path for audio_io_policy_file in vendor */
568 snprintf(audio_io_policy_file, sizeof(audio_io_policy_file),
569 "%s/%s", vendor_config_path, AUDIO_IO_POLICY_VENDOR_CONFIG_FILE_NAME);
570
571 /* Load audio_io_policy_file from vendor */
572 data = (char *)load_file(audio_io_policy_file, NULL);
573
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530574 if (data == NULL) {
575 ALOGD("%s: failed to open io config file(%s), trying older config file",
Roopesh Nataraja0f68be72020-04-28 12:32:02 -0700576 __func__, audio_io_policy_file);
577
578 /* Get path for audio_output_policy_file in vendor */
579 snprintf(audio_output_policy_file, sizeof(audio_output_policy_file),
580 "%s/%s", vendor_config_path,
581 AUDIO_OUTPUT_POLICY_VENDOR_CONFIG_FILE_NAME);
582
583 /* Load audio_output_policy_file from vendor */
584 data = (char *)load_file(audio_output_policy_file, NULL);
585
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530586 if (data == NULL) {
587 send_app_type_cfg(platform, mixer,
588 streams_output_cfg_list,
589 streams_input_cfg_list);
590 ALOGE("%s: could not load io policy config!", __func__);
yidongh6eb4f752017-04-19 18:20:57 +0800591 free(root);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530592 return;
593 }
594 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700595
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530596 config_load(root, data);
597 load_cfg_list(root, platform, streams_output_cfg_list,
598 streams_input_cfg_list);
599
600 send_app_type_cfg(platform, mixer, streams_output_cfg_list,
601 streams_input_cfg_list);
Alexy Josephaee4fdd2016-01-29 13:02:07 -0800602
603 config_free(root);
yidongh6eb4f752017-04-19 18:20:57 +0800604 free(root);
Alexy Josephaee4fdd2016-01-29 13:02:07 -0800605 free(data);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700606}
607
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530608static void audio_extn_utils_dump_streams_cfg_list(
609 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700610{
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700611 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530612 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700613 struct stream_format *sf_info;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700614 struct stream_sample_rate *ss_info;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530615
616 list_for_each(node_i, streams_cfg_list) {
617 s_info = node_to_item(node_i, struct streams_io_cfg, list);
618 ALOGV("%s: flags-%d, sample_rate-%d, bit_width-%d, app_type-%d",
619 __func__, s_info->flags.out_flags, s_info->app_type_cfg.sample_rate,
620 s_info->app_type_cfg.bit_width, s_info->app_type_cfg.app_type);
621 list_for_each(node_j, &s_info->format_list) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700622 sf_info = node_to_item(node_j, struct stream_format, list);
623 ALOGV("format-%x", sf_info->format);
624 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530625 list_for_each(node_j, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700626 ss_info = node_to_item(node_j, struct stream_sample_rate, list);
627 ALOGV("sample rate-%d", ss_info->sample_rate);
628 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700629 }
630}
631
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530632void audio_extn_utils_dump_streams_cfg_lists(
633 struct listnode *streams_output_cfg_list,
634 struct listnode *streams_input_cfg_list)
635{
636 ALOGV("%s", __func__);
637 audio_extn_utils_dump_streams_cfg_list(streams_output_cfg_list);
638 audio_extn_utils_dump_streams_cfg_list(streams_input_cfg_list);
639}
640
641static void audio_extn_utils_release_streams_cfg_list(
642 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700643{
644 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530645 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700646
647 ALOGV("%s", __func__);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530648
649 while (!list_empty(streams_cfg_list)) {
650 node_i = list_head(streams_cfg_list);
651 s_info = node_to_item(node_i, struct streams_io_cfg, list);
652 while (!list_empty(&s_info->format_list)) {
653 node_j = list_head(&s_info->format_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700654 list_remove(node_j);
655 free(node_to_item(node_j, struct stream_format, list));
656 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530657 while (!list_empty(&s_info->sample_rate_list)) {
658 node_j = list_head(&s_info->sample_rate_list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700659 list_remove(node_j);
660 free(node_to_item(node_j, struct stream_sample_rate, list));
661 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700662 list_remove(node_i);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530663 free(node_to_item(node_i, struct streams_io_cfg, list));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700664 }
665}
666
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530667void audio_extn_utils_release_streams_cfg_lists(
668 struct listnode *streams_output_cfg_list,
669 struct listnode *streams_input_cfg_list)
670{
671 ALOGV("%s", __func__);
672 audio_extn_utils_release_streams_cfg_list(streams_output_cfg_list);
673 audio_extn_utils_release_streams_cfg_list(streams_input_cfg_list);
674}
675
676static bool set_app_type_cfg(struct streams_io_cfg *s_info,
677 struct stream_app_type_cfg *app_type_cfg,
678 uint32_t sample_rate, uint32_t bit_width)
Amit Shekhar6f461b12014-08-01 14:52:58 -0700679 {
680 struct listnode *node_i;
681 struct stream_sample_rate *ss_info;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530682 list_for_each(node_i, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700683 ss_info = node_to_item(node_i, struct stream_sample_rate, list);
684 if ((sample_rate <= ss_info->sample_rate) &&
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530685 (bit_width == s_info->app_type_cfg.bit_width)) {
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700686
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530687 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700688 app_type_cfg->sample_rate = ss_info->sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530689 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700690 ALOGV("%s app_type_cfg->app_type %d, app_type_cfg->sample_rate %d, app_type_cfg->bit_width %d",
691 __func__, app_type_cfg->app_type, app_type_cfg->sample_rate, app_type_cfg->bit_width);
692 return true;
693 }
694 }
695 /*
696 * Reiterate through the list assuming dafault sample rate.
697 * Handles scenario where input sample rate is higher
698 * than all sample rates in list for the input bit width.
699 */
700 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
Apoorv Raghuvanshif59bb222015-02-18 12:23:23 -0800701
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530702 list_for_each(node_i, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700703 ss_info = node_to_item(node_i, struct stream_sample_rate, list);
704 if ((sample_rate <= ss_info->sample_rate) &&
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530705 (bit_width == s_info->app_type_cfg.bit_width)) {
706 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700707 app_type_cfg->sample_rate = sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530708 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Apoorv Raghuvanshif59bb222015-02-18 12:23:23 -0800709 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 -0700710 __func__, app_type_cfg->app_type, app_type_cfg->sample_rate, app_type_cfg->bit_width);
711 return true;
712 }
713 }
714 return false;
715}
716
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530717void audio_extn_utils_update_stream_input_app_type_cfg(void *platform,
718 struct listnode *streams_input_cfg_list,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -0800719 struct listnode *devices __unused,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530720 audio_input_flags_t flags,
721 audio_format_t format,
722 uint32_t sample_rate,
723 uint32_t bit_width,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530724 char* profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530725 struct stream_app_type_cfg *app_type_cfg)
726{
727 struct listnode *node_i, *node_j;
728 struct streams_io_cfg *s_info;
729 struct stream_format *sf_info;
730
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530731 ALOGV("%s: flags: 0x%x, format: 0x%x sample_rate %d, profile %s",
732 __func__, flags, format, sample_rate, profile);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530733
734 list_for_each(node_i, streams_input_cfg_list) {
735 s_info = node_to_item(node_i, struct streams_io_cfg, list);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530736 /* Along with flags do profile matching if set at either end.*/
737 if (s_info->flags.in_flags == flags &&
738 ((profile[0] == '\0' && s_info->profile[0] == '\0') ||
739 strncmp(s_info->profile, profile, sizeof(s_info->profile)) == 0)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530740 list_for_each(node_j, &s_info->format_list) {
741 sf_info = node_to_item(node_j, struct stream_format, list);
742 if (sf_info->format == format) {
743 if (set_app_type_cfg(s_info, app_type_cfg, sample_rate, bit_width))
744 return;
745 }
746 }
747 }
748 }
749 ALOGW("%s: App type could not be selected. Falling back to default", __func__);
750 app_type_cfg->app_type = platform_get_default_app_type_v2(platform, PCM_CAPTURE);
751 app_type_cfg->sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
752 app_type_cfg->bit_width = 16;
753}
754
755void audio_extn_utils_update_stream_output_app_type_cfg(void *platform,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700756 struct listnode *streams_output_cfg_list,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -0800757 struct listnode *devices,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700758 audio_output_flags_t flags,
759 audio_format_t format,
Amit Shekhar6f461b12014-08-01 14:52:58 -0700760 uint32_t sample_rate,
761 uint32_t bit_width,
Manish Dewangan837dc462015-05-27 10:17:41 +0530762 audio_channel_mask_t channel_mask,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530763 char *profile,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700764 struct stream_app_type_cfg *app_type_cfg)
765{
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +0530766 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530767 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700768 struct stream_format *sf_info;
Manish Dewangan837dc462015-05-27 10:17:41 +0530769 char value[PROPERTY_VALUE_MAX] = {0};
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700770
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -0800771 if (compare_device_type(devices, AUDIO_DEVICE_OUT_SPEAKER)) {
Ramu Gottipati074fa4d2018-09-11 20:05:51 +0530772 int bw = platform_get_snd_device_bit_width(SND_DEVICE_OUT_SPEAKER);
773 if ((-ENOSYS != bw) && (bit_width > (uint32_t)bw))
Amit Shekhar5a39c912014-10-14 15:39:30 -0700774 bit_width = (uint32_t)bw;
Ramu Gottipati074fa4d2018-09-11 20:05:51 +0530775 else if (-ENOSYS == bw)
776 bit_width = CODEC_BACKEND_DEFAULT_BIT_WIDTH;
Amit Shekhar1d896042014-10-03 13:16:09 -0700777 sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Aniket Kumar Latad13758f2020-08-06 15:11:36 -0700778 ALOGV("%s Allowing 24 and above bits playback on speaker \
779 ONLY at default sampling rate", __func__);
Amit Shekhar1d896042014-10-03 13:16:09 -0700780 }
781
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -0700782 property_get("vendor.audio.playback.mch.downsample",value,"");
Manish Dewangan837dc462015-05-27 10:17:41 +0530783 if (!strncmp("true", value, sizeof("true"))) {
784 if ((popcount(channel_mask) > 2) &&
785 (sample_rate > CODEC_BACKEND_DEFAULT_SAMPLE_RATE) &&
Aalique Grahame5cd12ff2017-10-19 10:57:00 -0700786 !(flags & (audio_output_flags_t)AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH)) {
Manish Dewangan837dc462015-05-27 10:17:41 +0530787 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
788 ALOGD("%s: MCH session defaulting sample rate to %d",
789 __func__, sample_rate);
790 }
791 }
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +0530792
793 /* Set sampling rate to 176.4 for DSD64
794 * and 352.8Khz for DSD128.
795 * Set Bit Width to 16. output will be 16 bit
796 * post DoP in ASM.
797 */
Aalique Grahame5cd12ff2017-10-19 10:57:00 -0700798 if ((flags & (audio_output_flags_t)AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH) &&
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +0530799 (format == AUDIO_FORMAT_DSD)) {
800 bit_width = 16;
801 if (sample_rate == INPUT_SAMPLING_RATE_DSD64)
802 sample_rate = OUTPUT_SAMPLING_RATE_DSD64;
803 else if (sample_rate == INPUT_SAMPLING_RATE_DSD128)
804 sample_rate = OUTPUT_SAMPLING_RATE_DSD128;
805 }
806
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +0530807
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800808 ALOGV("%s: flags: %x, format: %x sample_rate %d, profile %s, app_type %d",
809 __func__, flags, format, sample_rate, profile, app_type_cfg->app_type);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700810 list_for_each(node_i, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530811 s_info = node_to_item(node_i, struct streams_io_cfg, list);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530812 /* Along with flags do profile matching if set at either end.*/
813 if (s_info->flags.out_flags == flags &&
814 ((profile[0] == '\0' && s_info->profile[0] == '\0') ||
815 strncmp(s_info->profile, profile, sizeof(s_info->profile)) == 0)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530816 list_for_each(node_j, &s_info->format_list) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700817 sf_info = node_to_item(node_j, struct stream_format, list);
818 if (sf_info->format == format) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530819 if (set_app_type_cfg(s_info, app_type_cfg, sample_rate, bit_width))
Amit Shekhar6f461b12014-08-01 14:52:58 -0700820 return;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700821 }
822 }
823 }
824 }
825 list_for_each(node_i, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530826 s_info = node_to_item(node_i, struct streams_io_cfg, list);
827 if (s_info->flags.out_flags == AUDIO_OUTPUT_FLAG_PRIMARY) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700828 ALOGV("Compatible output profile not found.");
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530829 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
830 app_type_cfg->sample_rate = s_info->app_type_cfg.sample_rate;
831 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700832 ALOGV("%s Default to primary output: App type: %d sample_rate %d",
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530833 __func__, s_info->app_type_cfg.app_type, app_type_cfg->sample_rate);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700834 return;
835 }
836 }
837 ALOGW("%s: App type could not be selected. Falling back to default", __func__);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700838 app_type_cfg->app_type = platform_get_default_app_type(platform);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700839 app_type_cfg->sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700840 app_type_cfg->bit_width = 16;
841}
842
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +0530843static bool audio_is_this_native_usecase(struct audio_usecase *uc)
844{
845 bool native_usecase = false;
846 struct stream_out *out = (struct stream_out*) uc->stream.out;
847
848 if (PCM_PLAYBACK == uc->type && out != NULL &&
849 NATIVE_AUDIO_MODE_INVALID != platform_get_native_support() &&
850 is_offload_usecase(uc->id) &&
851 (out->sample_rate == OUTPUT_SAMPLING_RATE_44100))
852 native_usecase = true;
853
854 return native_usecase;
855}
856
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800857bool audio_extn_is_dsp_bit_width_enforce_mode_supported(audio_output_flags_t flags)
858{
859 /* DSP bitwidth enforce mode for ADM and AFE:
860 * includes:
861 * deep buffer, low latency, direct pcm and offload.
862 * excludes:
863 * ull(raw+fast), VOIP.
864 */
865 if ((flags & AUDIO_OUTPUT_FLAG_VOIP_RX) ||
866 ((flags & AUDIO_OUTPUT_FLAG_RAW) &&
867 (flags & AUDIO_OUTPUT_FLAG_FAST)))
868 return false;
869
870
871 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) ||
872 (flags & AUDIO_OUTPUT_FLAG_DIRECT) ||
873 (flags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) ||
874 (flags & AUDIO_OUTPUT_FLAG_PRIMARY))
875 return true;
876 else
877 return false;
878}
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800879
880static inline bool audio_is_vr_mode_on(struct audio_device *(__attribute__((unused)) adev))
881{
882 return adev->vr_audio_mode_enabled;
883}
884
Derek Chenf7092792017-05-23 12:23:53 -0400885static void audio_extn_btsco_get_sample_rate(int snd_device, int *sample_rate)
886{
887 switch (snd_device) {
888 case SND_DEVICE_OUT_BT_SCO:
889 case SND_DEVICE_IN_BT_SCO_MIC:
890 case SND_DEVICE_IN_BT_SCO_MIC_NREC:
891 *sample_rate = 8000;
892 break;
893 case SND_DEVICE_OUT_BT_SCO_WB:
894 case SND_DEVICE_IN_BT_SCO_MIC_WB:
895 case SND_DEVICE_IN_BT_SCO_MIC_WB_NREC:
896 *sample_rate = 16000;
897 break;
898 default:
Aniket Kumar Latad13758f2020-08-06 15:11:36 -0700899 ALOGV("%s:Not a BT SCO device, need not update sampling rate\n", __func__);
Derek Chenf7092792017-05-23 12:23:53 -0400900 break;
901 }
902}
903
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530904void audio_extn_utils_update_stream_app_type_cfg_for_usecase(
905 struct audio_device *adev,
906 struct audio_usecase *usecase)
907{
908 ALOGV("%s", __func__);
909
910 switch(usecase->type) {
911 case PCM_PLAYBACK:
912 audio_extn_utils_update_stream_output_app_type_cfg(adev->platform,
913 &adev->streams_output_cfg_list,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -0800914 &usecase->stream.out->device_list,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530915 usecase->stream.out->flags,
Aalique Grahame65780b52017-09-27 14:59:56 -0700916 usecase->stream.out->hal_op_format,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530917 usecase->stream.out->sample_rate,
918 usecase->stream.out->bit_width,
919 usecase->stream.out->channel_mask,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530920 usecase->stream.out->profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530921 &usecase->stream.out->app_type_cfg);
922 ALOGV("%s Selected apptype: %d", __func__, usecase->stream.out->app_type_cfg.app_type);
923 break;
924 case PCM_CAPTURE:
Revathi Uddarajud9f23d92020-07-27 10:55:06 +0530925 if (usecase->id == USECASE_AUDIO_RECORD_VOIP
926 || usecase->id == USECASE_AUDIO_RECORD_VOIP_LOW_LATENCY)
Vikram Panduranga93f080e2017-06-07 18:16:14 -0700927 usecase->stream.in->app_type_cfg.app_type = APP_TYPE_VOIP_AUDIO;
928 else
929 audio_extn_utils_update_stream_input_app_type_cfg(adev->platform,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530930 &adev->streams_input_cfg_list,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -0800931 &usecase->stream.in->device_list,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530932 usecase->stream.in->flags,
933 usecase->stream.in->format,
934 usecase->stream.in->sample_rate,
935 usecase->stream.in->bit_width,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530936 usecase->stream.in->profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530937 &usecase->stream.in->app_type_cfg);
938 ALOGV("%s Selected apptype: %d", __func__, usecase->stream.in->app_type_cfg.app_type);
939 break;
Surendar Karka93cd25a2018-08-28 14:21:37 +0530940 case TRANSCODE_LOOPBACK_RX :
Siddartha Shaik343abc62017-08-08 11:15:25 +0530941 audio_extn_utils_update_stream_output_app_type_cfg(adev->platform,
942 &adev->streams_output_cfg_list,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -0800943 &usecase->stream.inout->out_config.device_list,
Siddartha Shaik343abc62017-08-08 11:15:25 +0530944 0,
945 usecase->stream.inout->out_config.format,
946 usecase->stream.inout->out_config.sample_rate,
947 usecase->stream.inout->out_config.bit_width,
948 usecase->stream.inout->out_config.channel_mask,
949 usecase->stream.inout->profile,
950 &usecase->stream.inout->out_app_type_cfg);
951 ALOGV("%s Selected apptype: %d", __func__, usecase->stream.inout->out_app_type_cfg.app_type);
952 break;
Derek Chenf7092792017-05-23 12:23:53 -0400953 case PCM_HFP_CALL:
954 switch (usecase->id) {
955 case USECASE_AUDIO_HFP_SCO:
956 case USECASE_AUDIO_HFP_SCO_WB:
957 audio_extn_btsco_get_sample_rate(usecase->out_snd_device,
958 &usecase->out_app_type_cfg.sample_rate);
Derek Chend7307012019-11-12 12:10:41 -0500959 usecase->in_app_type_cfg.sample_rate = usecase->out_app_type_cfg.sample_rate;
Derek Chenf7092792017-05-23 12:23:53 -0400960 break;
961 case USECASE_AUDIO_HFP_SCO_DOWNLINK:
962 case USECASE_AUDIO_HFP_SCO_WB_DOWNLINK:
963 audio_extn_btsco_get_sample_rate(usecase->in_snd_device,
964 &usecase->in_app_type_cfg.sample_rate);
Derek Chend7307012019-11-12 12:10:41 -0500965 usecase->out_app_type_cfg.sample_rate = usecase->in_app_type_cfg.sample_rate;
Derek Chenf7092792017-05-23 12:23:53 -0400966 break;
967 default:
968 ALOGE("%s: usecase id (%d) not supported, use default sample rate",
969 __func__, usecase->id);
970 usecase->in_app_type_cfg.sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
971 usecase->out_app_type_cfg.sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
972 break;
973 }
974 /* update out_app_type_cfg */
975 usecase->out_app_type_cfg.bit_width =
976 platform_get_snd_device_bit_width(usecase->out_snd_device);
977 usecase->out_app_type_cfg.app_type =
978 platform_get_default_app_type_v2(adev->platform, PCM_PLAYBACK);
979 /* update in_app_type_cfg */
980 usecase->in_app_type_cfg.bit_width =
981 platform_get_snd_device_bit_width(usecase->in_snd_device);
982 usecase->in_app_type_cfg.app_type =
983 platform_get_default_app_type_v2(adev->platform, PCM_CAPTURE);
984 ALOGV("%s Selected apptype: playback %d capture %d",
985 __func__, usecase->out_app_type_cfg.app_type, usecase->in_app_type_cfg.app_type);
986 break;
Derek Chena30a5f42019-12-03 11:17:09 -0500987 case ICC_CALL:
988 /* ICC usecase: Loopback from TERT_TDM_TX to TERT_TDM_RX */
989 /* update out_app_type_cfg */
990 usecase->out_app_type_cfg.sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
991 usecase->out_app_type_cfg.bit_width = platform_get_snd_device_bit_width(usecase->out_snd_device);
992 usecase->out_app_type_cfg.app_type = platform_get_default_app_type_v2(adev->platform, PCM_PLAYBACK);
993 /* update in_app_type_cfg */
994 usecase->in_app_type_cfg.sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
995 usecase->in_app_type_cfg.bit_width = platform_get_snd_device_bit_width(usecase->in_snd_device);
996 usecase->in_app_type_cfg.app_type = platform_get_default_app_type_v2(adev->platform, PCM_CAPTURE);
997
998 ALOGV("%s Selected apptype: playback %d capture %d",
999 __func__, usecase->out_app_type_cfg.app_type, usecase->in_app_type_cfg.app_type);
1000 break;
Fei Tongaffdf732020-02-20 20:39:05 +08001001 case SYNTH_LOOPBACK:
1002 /* update out_app_type_cfg */
1003 usecase->out_app_type_cfg.sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
1004 usecase->out_app_type_cfg.bit_width =
1005 platform_get_snd_device_bit_width(usecase->out_snd_device);
1006 usecase->out_app_type_cfg.app_type =
1007 platform_get_default_app_type_v2(adev->platform, PCM_PLAYBACK);
1008 /* update in_app_type_cfg */
1009 usecase->in_app_type_cfg.sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
1010 usecase->in_app_type_cfg.bit_width =
1011 platform_get_snd_device_bit_width(usecase->in_snd_device);
1012 usecase->in_app_type_cfg.app_type =
1013 platform_get_default_app_type_v2(adev->platform, PCM_CAPTURE);
1014 ALOGV("%s Selected apptype: playback %d capture %d",
1015 __func__, usecase->out_app_type_cfg.app_type, usecase->in_app_type_cfg.app_type);
1016 break;
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301017 default:
1018 ALOGE("%s: app type cfg not supported for usecase type (%d)",
1019 __func__, usecase->type);
1020 }
1021}
1022
Aalique Grahame22e49102018-12-18 14:23:57 -08001023static int set_stream_app_type_mixer_ctrl(struct audio_device *adev,
1024 int pcm_device_id, int app_type,
1025 int acdb_dev_id, int sample_rate,
1026 int stream_type,
1027 snd_device_t snd_device)
1028{
1029
1030 char mixer_ctl_name[MAX_LENGTH_MIXER_CONTROL_IN_INT];
1031 struct mixer_ctl *ctl;
1032 int app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT], len = 0, rc = 0;
1033 int snd_device_be_idx = -1;
1034
1035 if (stream_type == PCM_PLAYBACK) {
1036 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
1037 "Audio Stream %d App Type Cfg", pcm_device_id);
1038 } else if (stream_type == PCM_CAPTURE) {
1039 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
1040 "Audio Stream Capture %d App Type Cfg", pcm_device_id);
1041 }
1042
1043 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
1044 if (!ctl) {
1045 ALOGE("%s: Could not get ctl for mixer cmd - %s",
1046 __func__, mixer_ctl_name);
1047 rc = -EINVAL;
1048 goto exit;
1049 }
1050 app_type_cfg[len++] = app_type;
1051 app_type_cfg[len++] = acdb_dev_id;
1052 app_type_cfg[len++] = sample_rate;
1053
1054 snd_device_be_idx = platform_get_snd_device_backend_index(snd_device);
1055 if (snd_device_be_idx > 0)
1056 app_type_cfg[len++] = snd_device_be_idx;
1057 ALOGV("%s: stream type %d app_type %d, acdb_dev_id %d "
1058 "sample rate %d, snd_device_be_idx %d",
1059 __func__, stream_type, app_type, acdb_dev_id, sample_rate,
1060 snd_device_be_idx);
1061 mixer_ctl_set_array(ctl, app_type_cfg, len);
1062
1063exit:
1064 return rc;
1065}
1066
1067static int audio_extn_utils_send_app_type_cfg_hfp(struct audio_device *adev,
1068 struct audio_usecase *usecase)
1069{
1070 int pcm_device_id, acdb_dev_id = 0, snd_device = usecase->out_snd_device;
1071 int32_t sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
1072 int app_type = 0, rc = 0;
Derek Chenf7092792017-05-23 12:23:53 -04001073 bool is_bus_dev_usecase = false;
Aalique Grahame22e49102018-12-18 14:23:57 -08001074
1075 ALOGV("%s", __func__);
1076
1077 if (usecase->type != PCM_HFP_CALL) {
Derek Chenf7092792017-05-23 12:23:53 -04001078 ALOGV("%s: not a HFP path, no need to cfg app type", __func__);
Aalique Grahame22e49102018-12-18 14:23:57 -08001079 rc = 0;
1080 goto exit_send_app_type_cfg;
1081 }
1082 if ((usecase->id != USECASE_AUDIO_HFP_SCO) &&
Derek Chenf7092792017-05-23 12:23:53 -04001083 (usecase->id != USECASE_AUDIO_HFP_SCO_WB) &&
1084 (usecase->id != USECASE_AUDIO_HFP_SCO_DOWNLINK) &&
1085 (usecase->id != USECASE_AUDIO_HFP_SCO_WB_DOWNLINK)) {
1086 ALOGV("%s: a usecase where app type cfg is not required", __func__);
Aalique Grahame22e49102018-12-18 14:23:57 -08001087 rc = 0;
1088 goto exit_send_app_type_cfg;
1089 }
1090
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08001091 if (compare_device_type(&usecase->device_list, AUDIO_DEVICE_OUT_BUS))
Derek Chenf7092792017-05-23 12:23:53 -04001092 is_bus_dev_usecase = true;
1093
Aalique Grahame22e49102018-12-18 14:23:57 -08001094 snd_device = usecase->out_snd_device;
1095 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_PLAYBACK);
1096
1097 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
1098 if (acdb_dev_id < 0) {
1099 ALOGE("%s: Couldn't get the acdb dev id", __func__);
1100 rc = -EINVAL;
1101 goto exit_send_app_type_cfg;
1102 }
1103
sriram kumar53c4c972021-02-18 16:49:33 +05301104 /*
1105 * Value of afe_loopback gets read based on the property defined in
1106 * audio_platform_info.xml. If afe loopback is set then do not execute
1107 * session 1 path as app type mixer control will not be created for
1108 * afe loopback
1109 */
1110
Aalique Grahame22e49102018-12-18 14:23:57 -08001111 if (usecase->type == PCM_HFP_CALL) {
sriram kumar53c4c972021-02-18 16:49:33 +05301112 if (!(platform_get_is_afe_loopback_enabled(adev->platform))) {
1113 /* config HFP session:1 playback path */
1114 if (is_bus_dev_usecase) {
1115 app_type = usecase->out_app_type_cfg.app_type;
1116 sample_rate= usecase->out_app_type_cfg.sample_rate;
1117 } else {
1118 snd_device = SND_DEVICE_NONE; // use legacy behavior
1119 app_type = platform_get_default_app_type_v2(adev->platform, PCM_PLAYBACK);
1120 sample_rate= CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
Derek Chenf7092792017-05-23 12:23:53 -04001121 }
sriram kumar53c4c972021-02-18 16:49:33 +05301122 rc = set_stream_app_type_mixer_ctrl(adev, pcm_device_id, app_type,
1123 acdb_dev_id, sample_rate,
1124 PCM_PLAYBACK,
1125 snd_device);
1126 if (rc < 0)
1127 goto exit_send_app_type_cfg;
1128
1129 /* config HFP session:1 capture path */
1130 if (is_bus_dev_usecase) {
1131 snd_device = usecase->in_snd_device;
1132 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_CAPTURE);
1133 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
1134 if (acdb_dev_id < 0) {
1135 ALOGE("%s: Couldn't get the acdb dev id", __func__);
1136 rc = -EINVAL;
1137 goto exit_send_app_type_cfg;
1138 }
1139 app_type = usecase->in_app_type_cfg.app_type;
1140 sample_rate= usecase->in_app_type_cfg.sample_rate;
1141 } else {
1142 snd_device = SND_DEVICE_NONE; // use legacy behavior
1143 app_type = platform_get_default_app_type_v2(adev->platform, PCM_CAPTURE);
1144 }
1145 rc = set_stream_app_type_mixer_ctrl(adev, pcm_device_id, app_type,
1146 acdb_dev_id, sample_rate,
1147 PCM_CAPTURE,
1148 snd_device);
1149 if (rc < 0)
1150 goto exit_send_app_type_cfg;
1151
1152 if (is_bus_dev_usecase)
1153 goto exit_send_app_type_cfg;
Derek Chenf7092792017-05-23 12:23:53 -04001154 }
Aalique Grahame22e49102018-12-18 14:23:57 -08001155 /* config HFP session:2 capture path */
1156 pcm_device_id = HFP_ASM_RX_TX;
1157 snd_device = usecase->in_snd_device;
1158 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
1159 if (acdb_dev_id <= 0) {
1160 ALOGE("%s: Couldn't get the acdb dev id", __func__);
1161 rc = -EINVAL;
1162 goto exit_send_app_type_cfg;
1163 }
1164 app_type = platform_get_default_app_type_v2(adev->platform, PCM_CAPTURE);
1165 rc = set_stream_app_type_mixer_ctrl(adev, pcm_device_id, app_type,
1166 acdb_dev_id, sample_rate, PCM_CAPTURE,
sriram kumar53c4c972021-02-18 16:49:33 +05301167 usecase->in_snd_device);
Aalique Grahame22e49102018-12-18 14:23:57 -08001168 if (rc < 0)
1169 goto exit_send_app_type_cfg;
1170
1171 /* config HFP session:2 playback path */
1172 app_type = platform_get_default_app_type_v2(adev->platform, PCM_PLAYBACK);
1173 rc = set_stream_app_type_mixer_ctrl(adev, pcm_device_id, app_type,
1174 acdb_dev_id, sample_rate,
sriram kumar53c4c972021-02-18 16:49:33 +05301175 PCM_PLAYBACK, usecase->out_snd_device);
Aalique Grahame22e49102018-12-18 14:23:57 -08001176 if (rc < 0)
1177 goto exit_send_app_type_cfg;
1178 }
1179
1180 rc = 0;
1181exit_send_app_type_cfg:
1182 return rc;
1183}
1184
Derek Chena30a5f42019-12-03 11:17:09 -05001185int audio_extn_utils_send_app_type_cfg_icc(struct audio_device *adev,
1186 struct audio_usecase *usecase)
1187{
1188 int pcm_device_id, acdb_dev_id = 0, snd_device = usecase->out_snd_device;
1189 int32_t sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
1190 int app_type = 0, rc = 0;
1191
1192 ALOGV("%s", __func__);
1193
1194 if (usecase->type != ICC_CALL) {
1195 ALOGV("%s: not an ICC path, no need to cfg app type", __func__);
1196 rc = 0;
1197 goto exit_send_app_type_cfg;
1198 }
1199 if (usecase->id != USECASE_ICC_CALL) {
1200 ALOGV("%s: a usecase where app type cfg is not required", __func__);
1201 rc = 0;
1202 goto exit_send_app_type_cfg;
1203 }
1204
1205 snd_device = usecase->out_snd_device;
1206 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_PLAYBACK);
1207 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
1208 if (acdb_dev_id < 0) {
1209 ALOGE("%s: Couldn't get the acdb dev id", __func__);
1210 rc = -EINVAL;
1211 goto exit_send_app_type_cfg;
1212 }
1213 /* config ICC session: playback path */
1214 app_type = usecase->out_app_type_cfg.app_type;
1215 sample_rate= usecase->out_app_type_cfg.sample_rate;
1216
1217 rc = set_stream_app_type_mixer_ctrl(adev, pcm_device_id, app_type,
1218 acdb_dev_id, sample_rate,
1219 PCM_PLAYBACK,
1220 snd_device);
1221 if (rc < 0)
1222 goto exit_send_app_type_cfg;
1223
1224 /* config ICC session: capture path */
1225 snd_device = usecase->in_snd_device;
1226 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_CAPTURE);
1227 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
1228 if (acdb_dev_id < 0) {
1229 ALOGE("%s: Couldn't get the acdb dev id", __func__);
1230 rc = -EINVAL;
1231 goto exit_send_app_type_cfg;
1232 }
1233 app_type = usecase->in_app_type_cfg.app_type;
1234 sample_rate= usecase->in_app_type_cfg.sample_rate;
1235 rc = set_stream_app_type_mixer_ctrl(adev, pcm_device_id, app_type,
1236 acdb_dev_id, sample_rate,
1237 PCM_CAPTURE,
1238 snd_device);
1239exit_send_app_type_cfg:
1240 return rc;
1241}
1242
Fei Tongaffdf732020-02-20 20:39:05 +08001243static int audio_extn_utils_send_app_type_cfg_synth(struct audio_device *adev,
1244 struct audio_usecase *usecase)
1245{
1246 int pcm_device_id, acdb_dev_id = 0, snd_device = usecase->out_snd_device;
1247 int32_t sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
1248 int app_type = 0, rc = 0;
1249 bool is_bus_dev_usecase = false;
1250
1251 ALOGV("%s", __func__);
1252
1253 if (usecase->type != SYNTH_LOOPBACK) {
1254 ALOGV("%s: not a SYNTH path, no need to cfg app type", __func__);
1255 rc = 0;
1256 goto exit_send_app_type_cfg;
1257 }
1258 if (usecase->id != USECASE_AUDIO_PLAYBACK_SYNTHESIZER) {
1259 ALOGV("%s: a usecase where app type cfg is not required", __func__);
1260 rc = 0;
1261 goto exit_send_app_type_cfg;
1262 }
1263
1264 if (compare_device_type(&usecase->device_list, AUDIO_DEVICE_OUT_BUS)) {
1265 is_bus_dev_usecase = true;
1266 }
1267
1268 snd_device = usecase->out_snd_device;
1269 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_PLAYBACK);
1270
1271 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
1272 if (acdb_dev_id < 0) {
1273 ALOGE("%s: Couldn't get the acdb dev id", __func__);
1274 rc = -EINVAL;
1275 goto exit_send_app_type_cfg;
1276 }
1277
1278 if (usecase->type == SYNTH_LOOPBACK) {
1279 /* config SYNTH session: playback path */
1280 if (is_bus_dev_usecase) {
1281 app_type = usecase->out_app_type_cfg.app_type;
1282 sample_rate= usecase->out_app_type_cfg.sample_rate;
1283 } else {
1284 snd_device = SND_DEVICE_NONE; // use legacy behavior
1285 app_type = platform_get_default_app_type_v2(adev->platform, PCM_PLAYBACK);
1286 sample_rate= CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
1287 }
1288 rc = set_stream_app_type_mixer_ctrl(adev, pcm_device_id, app_type,
1289 acdb_dev_id, sample_rate,
1290 PCM_PLAYBACK,
1291 snd_device);
1292 if (rc < 0)
1293 goto exit_send_app_type_cfg;
1294 }
1295
1296 rc = 0;
1297exit_send_app_type_cfg:
1298 return rc;
1299}
1300
Zhou Song06761dd2019-04-28 18:08:17 +08001301int audio_extn_utils_get_app_sample_rate_for_device(
1302 struct audio_device *adev,
1303 struct audio_usecase *usecase, int snd_device)
1304{
1305 char value[PROPERTY_VALUE_MAX] = {0};
1306 int sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
1307
1308 if ((usecase->type == PCM_PLAYBACK) && (usecase->stream.out != NULL)) {
1309 property_get("vendor.audio.playback.mch.downsample",value,"");
1310 if (!strncmp("true", value, sizeof("true"))) {
1311 if ((popcount(usecase->stream.out->channel_mask) > 2) &&
1312 (usecase->stream.out->app_type_cfg.sample_rate > CODEC_BACKEND_DEFAULT_SAMPLE_RATE) &&
1313 !(usecase->stream.out->flags &
1314 (audio_output_flags_t)AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH))
1315 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
1316 }
1317
1318 if (usecase->id == USECASE_AUDIO_PLAYBACK_VOIP) {
1319 usecase->stream.out->app_type_cfg.sample_rate = usecase->stream.out->sample_rate;
Zhou Song06761dd2019-04-28 18:08:17 +08001320 } else if ((snd_device == SND_DEVICE_OUT_HDMI ||
1321 snd_device == SND_DEVICE_OUT_USB_HEADSET ||
1322 snd_device == SND_DEVICE_OUT_DISPLAY_PORT) &&
1323 (usecase->stream.out->sample_rate >= OUTPUT_SAMPLING_RATE_44100)) {
1324 /*
1325 * To best utlize DSP, check if the stream sample rate is supported/multiple of
1326 * configured device sample rate, if not update the COPP rate to be equal to the
1327 * device sample rate, else open COPP at stream sample rate
1328 */
1329 platform_check_and_update_copp_sample_rate(adev->platform, snd_device,
1330 usecase->stream.out->sample_rate,
1331 &usecase->stream.out->app_type_cfg.sample_rate);
1332 } else if (((snd_device != SND_DEVICE_OUT_HEADPHONES_44_1 &&
1333 !audio_is_this_native_usecase(usecase)) &&
1334 usecase->stream.out->sample_rate == OUTPUT_SAMPLING_RATE_44100) ||
1335 (usecase->stream.out->sample_rate < OUTPUT_SAMPLING_RATE_44100)) {
1336 /* Reset to default if no native stream is active*/
1337 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Lakshman Chaluvaraju5d0894e2021-01-27 23:22:58 +05301338 } else if (snd_device == SND_DEVICE_OUT_BT_A2DP) {
Zhou Song06761dd2019-04-28 18:08:17 +08001339 /*
1340 * For a2dp playback get encoder sampling rate and set copp sampling rate,
1341 * for bit width use the stream param only.
1342 */
1343 audio_extn_a2dp_get_enc_sample_rate(&usecase->stream.out->app_type_cfg.sample_rate);
1344 ALOGI("%s using %d sample rate rate for A2DP CoPP",
1345 __func__, usecase->stream.out->app_type_cfg.sample_rate);
Lakshman Chaluvaraju5d0894e2021-01-27 23:22:58 +05301346 } else if (compare_device_type(&usecase->stream.out->device_list,
1347 AUDIO_DEVICE_OUT_SPEAKER)) {
1348 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Zhou Song06761dd2019-04-28 18:08:17 +08001349 }
1350 audio_extn_btsco_get_sample_rate(snd_device, &usecase->stream.out->app_type_cfg.sample_rate);
1351 sample_rate = usecase->stream.out->app_type_cfg.sample_rate;
1352
1353 if (((usecase->stream.out->format == AUDIO_FORMAT_E_AC3) ||
1354 (usecase->stream.out->format == AUDIO_FORMAT_E_AC3_JOC) ||
1355 (usecase->stream.out->format == AUDIO_FORMAT_DOLBY_TRUEHD))
1356 && audio_extn_passthru_is_passthrough_stream(usecase->stream.out)
1357 && !audio_extn_passthru_is_convert_supported(adev, usecase->stream.out)) {
1358 sample_rate = sample_rate * 4;
1359 if (sample_rate > HDMI_PASSTHROUGH_MAX_SAMPLE_RATE)
1360 sample_rate = HDMI_PASSTHROUGH_MAX_SAMPLE_RATE;
1361 }
1362 } else if (usecase->type == PCM_CAPTURE) {
1363 if (usecase->stream.in != NULL) {
Revathi Uddarajud9f23d92020-07-27 10:55:06 +05301364 if (usecase->id == USECASE_AUDIO_RECORD_VOIP
1365 || usecase->id == USECASE_AUDIO_RECORD_VOIP_LOW_LATENCY)
Zhou Song06761dd2019-04-28 18:08:17 +08001366 usecase->stream.in->app_type_cfg.sample_rate = usecase->stream.in->sample_rate;
1367 if (voice_is_in_call_rec_stream(usecase->stream.in)) {
1368 audio_extn_btsco_get_sample_rate(usecase->in_snd_device,
1369 &usecase->stream.in->app_type_cfg.sample_rate);
Zhou Songab94f062021-05-12 13:11:17 +08001370 } if (SND_DEVICE_IN_BT_A2DP == snd_device) {
1371 audio_extn_a2dp_get_dec_sample_rate(&usecase->stream.in->app_type_cfg.sample_rate);
Zhou Song06761dd2019-04-28 18:08:17 +08001372 } else {
1373 audio_extn_btsco_get_sample_rate(snd_device,
1374 &usecase->stream.in->app_type_cfg.sample_rate);
1375 }
1376 sample_rate = usecase->stream.in->app_type_cfg.sample_rate;
1377 } else if (usecase->id == USECASE_AUDIO_SPKR_CALIB_TX) {
Sujin Panickerb6d76262019-09-09 10:55:38 +05301378 if ((property_get("vendor.audio.spkr_prot.tx.sampling_rate", value, NULL) > 0))
1379 sample_rate = atoi(value);
1380 else
1381 sample_rate = SAMPLE_RATE_8000;
Zhou Song06761dd2019-04-28 18:08:17 +08001382 }
1383 } else if (usecase->type == TRANSCODE_LOOPBACK_RX) {
1384 sample_rate = usecase->stream.inout->out_config.sample_rate;
1385 }
1386 return sample_rate;
1387}
1388
Siena Richard7c2db772016-12-21 11:32:34 -08001389static int send_app_type_cfg_for_device(struct audio_device *adev,
1390 struct audio_usecase *usecase,
1391 int split_snd_device)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001392{
1393 char mixer_ctl_name[MAX_LENGTH_MIXER_CONTROL_IN_INT];
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301394 size_t app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT] = {0};
1395 int len = 0, rc;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001396 struct mixer_ctl *ctl;
Siena Richard7c2db772016-12-21 11:32:34 -08001397 int pcm_device_id = 0, acdb_dev_id, app_type;
1398 int snd_device = split_snd_device, snd_device_be_idx = -1;
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +05301399 int32_t sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Varun Balaraje49253e2017-07-06 19:48:56 +05301400 struct streams_io_cfg *s_info = NULL;
1401 struct listnode *node = NULL;
Nikhil Laturkarac4a7492017-08-31 18:27:19 +05301402 int bd_app_type = 0;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001403
Siena Richard7c2db772016-12-21 11:32:34 -08001404 ALOGV("%s: usecase->out_snd_device %s, usecase->in_snd_device %s, split_snd_device %s",
1405 __func__, platform_get_snd_device_name(usecase->out_snd_device),
1406 platform_get_snd_device_name(usecase->in_snd_device),
1407 platform_get_snd_device_name(split_snd_device));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001408
Siddartha Shaik343abc62017-08-08 11:15:25 +05301409 if (usecase->type != PCM_PLAYBACK && usecase->type != PCM_CAPTURE &&
Surendar Karka93cd25a2018-08-28 14:21:37 +05301410 usecase->type != TRANSCODE_LOOPBACK_RX) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301411 ALOGE("%s: not a playback/capture path, no need to cfg app type", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001412 rc = 0;
1413 goto exit_send_app_type_cfg;
1414 }
1415 if ((usecase->id != USECASE_AUDIO_PLAYBACK_DEEP_BUFFER) &&
Vignesh Kulothungan0a2eff02019-07-11 16:30:13 -07001416 (usecase->id != USECASE_AUDIO_PLAYBACK_WITH_HAPTICS) &&
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001417 (usecase->id != USECASE_AUDIO_PLAYBACK_LOW_LATENCY) &&
1418 (usecase->id != USECASE_AUDIO_PLAYBACK_MULTI_CH) &&
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -08001419 (usecase->id != USECASE_AUDIO_PLAYBACK_ULL) &&
Vikram Panduranga93f080e2017-06-07 18:16:14 -07001420 (usecase->id != USECASE_AUDIO_PLAYBACK_VOIP) &&
Surendar Karka93cd25a2018-08-28 14:21:37 +05301421 (usecase->id != USECASE_AUDIO_TRANSCODE_LOOPBACK_RX) &&
Varun Balaraje49253e2017-07-06 19:48:56 +05301422 (!is_interactive_usecase(usecase->id)) &&
Srikanth Uyyala9d551402015-08-25 16:03:42 +05301423 (!is_offload_usecase(usecase->id)) &&
Derek Chenf6318be2017-06-12 17:16:24 -04001424 (usecase->type != PCM_CAPTURE) &&
1425 (!audio_extn_auto_hal_is_bus_device_usecase(usecase->id))) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301426 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 -07001427 rc = 0;
1428 goto exit_send_app_type_cfg;
1429 }
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -08001430
1431 //if VR is active then only send the mixer control
1432 if (usecase->id == USECASE_AUDIO_PLAYBACK_ULL && !audio_is_vr_mode_on(adev)) {
1433 ALOGI("ULL doesnt need sending app type cfg, returning");
1434 rc = 0;
1435 goto exit_send_app_type_cfg;
1436 }
1437
Surendar Karka93cd25a2018-08-28 14:21:37 +05301438 if (usecase->type == PCM_PLAYBACK || usecase->type == TRANSCODE_LOOPBACK_RX) {
Ben Romberger1fafdde2015-09-09 19:43:15 -07001439 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_PLAYBACK);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301440 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
1441 "Audio Stream %d App Type Cfg", pcm_device_id);
Ben Romberger1fafdde2015-09-09 19:43:15 -07001442 } else if (usecase->type == PCM_CAPTURE) {
Ben Romberger1fafdde2015-09-09 19:43:15 -07001443 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_CAPTURE);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301444 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
1445 "Audio Stream Capture %d App Type Cfg", pcm_device_id);
Srikanth Uyyala9d551402015-08-25 16:03:42 +05301446 }
Siena Richard7c2db772016-12-21 11:32:34 -08001447
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001448 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
1449 if (!ctl) {
1450 ALOGE("%s: Could not get ctl for mixer cmd - %s", __func__,
1451 mixer_ctl_name);
1452 rc = -EINVAL;
1453 goto exit_send_app_type_cfg;
1454 }
Aditya Bavanari701a6992017-03-30 19:17:16 +05301455 snd_device = platform_get_spkr_prot_snd_device(snd_device);
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301456 if (voice_is_in_call_rec_stream(usecase->stream.in) && usecase->type == PCM_CAPTURE) {
1457 snd_device_t voice_device = voice_get_incall_rec_snd_device(usecase->in_snd_device);
1458 acdb_dev_id = platform_get_snd_device_acdb_id(voice_device);
1459 ALOGV("acdb id for voice call use case %d", acdb_dev_id);
1460 } else {
1461 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
1462 }
Rohit Kumar1181b292017-01-31 18:07:17 +05301463 if (acdb_dev_id <= 0) {
1464 ALOGE("%s: Couldn't get the acdb dev id", __func__);
1465 rc = -EINVAL;
1466 goto exit_send_app_type_cfg;
1467 }
1468
Siena Richard7c2db772016-12-21 11:32:34 -08001469 snd_device_be_idx = platform_get_snd_device_backend_index(snd_device);
1470 if (snd_device_be_idx < 0) {
1471 ALOGE("%s: Couldn't get the backend index for snd device %s ret=%d",
1472 __func__, platform_get_snd_device_name(snd_device),
1473 snd_device_be_idx);
1474 }
1475
Zhou Song06761dd2019-04-28 18:08:17 +08001476 sample_rate = audio_extn_utils_get_app_sample_rate_for_device(adev, usecase, snd_device);
1477
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301478 if ((usecase->type == PCM_PLAYBACK) && (usecase->stream.out != NULL)) {
Varun Balaraje49253e2017-07-06 19:48:56 +05301479 /* Interactive streams are supported with only direct app type id.
1480 * Get Direct profile app type and use it for interactive streams
1481 */
1482 list_for_each(node, &adev->streams_output_cfg_list) {
1483 s_info = node_to_item(node, struct streams_io_cfg, list);
Aalique Grahame5cd12ff2017-10-19 10:57:00 -07001484 if (s_info->flags.out_flags == (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_BD |
Nikhil Laturkarac4a7492017-08-31 18:27:19 +05301485 AUDIO_OUTPUT_FLAG_DIRECT_PCM |
1486 AUDIO_OUTPUT_FLAG_DIRECT))
1487 bd_app_type = s_info->app_type_cfg.app_type;
Varun Balaraje49253e2017-07-06 19:48:56 +05301488 }
Aalique Grahame5cd12ff2017-10-19 10:57:00 -07001489 if (usecase->stream.out->flags == (audio_output_flags_t)AUDIO_OUTPUT_FLAG_INTERACTIVE)
Nikhil Laturkarac4a7492017-08-31 18:27:19 +05301490 app_type = bd_app_type;
Varun Balaraje49253e2017-07-06 19:48:56 +05301491 else
1492 app_type = usecase->stream.out->app_type_cfg.app_type;
Siena Richard7c2db772016-12-21 11:32:34 -08001493 app_type_cfg[len++] = app_type;
Mingming Yin21854652016-04-13 11:54:02 -07001494 app_type_cfg[len++] = acdb_dev_id;
Naresh Tanniru3a406772017-05-10 13:09:05 -07001495 app_type_cfg[len++] = sample_rate;
1496
Siena Richard7c2db772016-12-21 11:32:34 -08001497 if (snd_device_be_idx > 0)
1498 app_type_cfg[len++] = snd_device_be_idx;
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301499
Siena Richard7c2db772016-12-21 11:32:34 -08001500 ALOGI("%s PLAYBACK app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
1501 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301502
1503 } else if ((usecase->type == PCM_CAPTURE) && (usecase->stream.in != NULL)) {
Siena Richard7c2db772016-12-21 11:32:34 -08001504 app_type = usecase->stream.in->app_type_cfg.app_type;
1505 app_type_cfg[len++] = app_type;
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301506 app_type_cfg[len++] = acdb_dev_id;
Siena Richard7c2db772016-12-21 11:32:34 -08001507 app_type_cfg[len++] = sample_rate;
1508 if (snd_device_be_idx > 0)
1509 app_type_cfg[len++] = snd_device_be_idx;
1510 ALOGI("%s CAPTURE app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
1511 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301512 } else {
Siena Richard7c2db772016-12-21 11:32:34 -08001513 app_type = platform_get_default_app_type_v2(adev->platform, usecase->type);
Surendar Karka93cd25a2018-08-28 14:21:37 +05301514 if(usecase->type == TRANSCODE_LOOPBACK_RX) {
Siddartha Shaik343abc62017-08-08 11:15:25 +05301515 app_type = usecase->stream.inout->out_app_type_cfg.app_type;
1516 }
Siena Richard7c2db772016-12-21 11:32:34 -08001517 app_type_cfg[len++] = app_type;
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301518 app_type_cfg[len++] = acdb_dev_id;
1519 app_type_cfg[len++] = sample_rate;
Siena Richard7c2db772016-12-21 11:32:34 -08001520 if (snd_device_be_idx > 0)
1521 app_type_cfg[len++] = snd_device_be_idx;
1522 ALOGI("%s default app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
1523 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Ashish Jainc02430d2016-01-04 10:42:43 +05301524 }
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301525
Siddartha Shaik343abc62017-08-08 11:15:25 +05301526 if(ctl)
1527 mixer_ctl_set_array(ctl, app_type_cfg, len);
Zhou Songd9bd9302020-08-04 16:34:45 +08001528
1529 /* send app type cfg for haptics */
1530 if (usecase->id == USECASE_AUDIO_PLAYBACK_WITH_HAPTICS) {
1531 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
1532 "Audio Stream %d App Type Cfg", adev->haptic_pcm_device_id );
1533 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
1534 if (!ctl) {
1535 ALOGE("%s: Could not get ctl for mixer cmd - %s", __func__,
1536 mixer_ctl_name);
1537 rc = -EINVAL;
1538 goto exit_send_app_type_cfg;
1539 }
1540 acdb_dev_id = platform_get_snd_device_acdb_id(SND_DEVICE_OUT_HAPTICS);
1541 snd_device_be_idx = platform_get_snd_device_backend_index(SND_DEVICE_OUT_HAPTICS);
1542 /* haptics acdb id */
1543 app_type_cfg[1] = acdb_dev_id;
1544 /* haptics be index */
1545 app_type_cfg[3] = snd_device_be_idx;
1546 mixer_ctl_set_array(ctl, app_type_cfg, len);
1547 }
1548
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001549 rc = 0;
1550exit_send_app_type_cfg:
1551 return rc;
1552}
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001553
Deeraj Soman14230922019-01-30 16:39:30 +05301554static int audio_extn_utils_check_input_parameters(uint32_t sample_rate,
1555 audio_format_t format,
1556 int channel_count)
1557{
1558 int ret = 0;
1559
1560 if (((format != AUDIO_FORMAT_PCM_16_BIT) && (format != AUDIO_FORMAT_PCM_8_24_BIT) &&
1561 (format != AUDIO_FORMAT_PCM_24_BIT_PACKED) && (format != AUDIO_FORMAT_PCM_32_BIT) &&
1562 (format != AUDIO_FORMAT_PCM_FLOAT)) &&
1563 !voice_extn_compress_voip_is_format_supported(format) &&
1564 !audio_extn_compr_cap_format_supported(format) &&
1565 !audio_extn_cin_format_supported(format))
1566 ret = -EINVAL;
1567
1568 switch (channel_count) {
1569 case 1:
1570 case 2:
1571 case 3:
1572 case 4:
1573 case 6:
1574 case 8:
1575 break;
1576 default:
1577 ret = -EINVAL;
1578 }
1579
1580 switch (sample_rate) {
1581 case 8000:
1582 case 11025:
1583 case 12000:
1584 case 16000:
1585 case 22050:
1586 case 24000:
1587 case 32000:
1588 case 44100:
1589 case 48000:
1590 case 88200:
1591 case 96000:
1592 case 176400:
1593 case 192000:
1594 break;
1595 default:
1596 ret = -EINVAL;
1597 }
1598
1599 return ret;
1600}
1601
1602static inline uint32_t audio_extn_utils_nearest_multiple(uint32_t num, uint32_t multiplier)
1603{
1604 uint32_t remainder = 0;
1605
1606 if (!multiplier)
1607 return num;
1608
1609 remainder = num % multiplier;
1610 if (remainder)
1611 num += (multiplier - remainder);
1612
1613 return num;
1614}
1615
1616static inline uint32_t audio_extn_utils_lcm(uint32_t num1, uint32_t num2)
1617{
1618 uint32_t high = num1, low = num2, temp = 0;
1619
1620 if (!num1 || !num2)
1621 return 0;
1622
1623 if (num1 < num2) {
1624 high = num2;
1625 low = num1;
1626 }
1627
1628 while (low != 0) {
1629 temp = low;
1630 low = high % low;
1631 high = temp;
1632 }
1633 return (num1 * num2)/high;
1634}
1635
Siena Richard7c2db772016-12-21 11:32:34 -08001636int audio_extn_utils_send_app_type_cfg(struct audio_device *adev,
1637 struct audio_usecase *usecase)
1638{
1639 int i, num_devices = 0;
1640 snd_device_t new_snd_devices[SND_DEVICE_OUT_END] = {0};
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301641 snd_device_t in_snd_device = usecase->in_snd_device;
Siena Richard7c2db772016-12-21 11:32:34 -08001642 int rc = 0;
1643
Aalique Grahame22e49102018-12-18 14:23:57 -08001644 if (usecase->type == PCM_HFP_CALL) {
1645 return audio_extn_utils_send_app_type_cfg_hfp(adev, usecase);
Derek Chena30a5f42019-12-03 11:17:09 -05001646 } else if (usecase->type == ICC_CALL) {
1647 return audio_extn_utils_send_app_type_cfg_icc(adev, usecase);
Fei Tongaffdf732020-02-20 20:39:05 +08001648 } else if (usecase->type == SYNTH_LOOPBACK) {
1649 return audio_extn_utils_send_app_type_cfg_synth(adev, usecase);
Aalique Grahame22e49102018-12-18 14:23:57 -08001650 }
1651
Siena Richard7c2db772016-12-21 11:32:34 -08001652 switch (usecase->type) {
1653 case PCM_PLAYBACK:
Surendar Karka93cd25a2018-08-28 14:21:37 +05301654 case TRANSCODE_LOOPBACK_RX:
Siena Richard7c2db772016-12-21 11:32:34 -08001655 ALOGD("%s: usecase->out_snd_device %s",
1656 __func__, platform_get_snd_device_name(usecase->out_snd_device));
1657 /* check for out combo device */
1658 if (platform_split_snd_device(adev->platform,
1659 usecase->out_snd_device,
1660 &num_devices, new_snd_devices)) {
1661 new_snd_devices[0] = usecase->out_snd_device;
1662 num_devices = 1;
1663 }
1664 break;
1665 case PCM_CAPTURE:
1666 ALOGD("%s: usecase->in_snd_device %s",
1667 __func__, platform_get_snd_device_name(usecase->in_snd_device));
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301668 if (voice_is_in_call_rec_stream(usecase->stream.in)) {
1669 in_snd_device = voice_get_incall_rec_backend_device(usecase->stream.in);
1670 }
Siena Richard7c2db772016-12-21 11:32:34 -08001671 /* check for in combo device */
1672 if (platform_split_snd_device(adev->platform,
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301673 in_snd_device,
Siena Richard7c2db772016-12-21 11:32:34 -08001674 &num_devices, new_snd_devices)) {
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301675 new_snd_devices[0] = in_snd_device;
Siena Richard7c2db772016-12-21 11:32:34 -08001676 num_devices = 1;
1677 }
1678 break;
Sriram Kumar9c836482020-10-27 20:34:35 +05301679 case PCM_HFP_CALL:
1680 rc = audio_extn_utils_send_app_type_cfg_hfp(adev,usecase);
1681 return rc;
Siena Richard7c2db772016-12-21 11:32:34 -08001682 default:
1683 ALOGI("%s: not a playback/capture path, no need to cfg app type", __func__);
1684 rc = 0;
1685 break;
1686 }
1687
1688 for (i = 0; i < num_devices; i++) {
1689 rc = send_app_type_cfg_for_device(adev, usecase, new_snd_devices[i]);
1690 if (rc)
1691 break;
1692 }
1693
1694 return rc;
1695}
1696
Preetam Singh Ranawat9519e9c2015-11-18 16:05:55 +05301697int read_line_from_file(const char *path, char *buf, size_t count)
1698{
1699 char * fgets_ret;
1700 FILE * fd;
1701 int rv;
1702
1703 fd = fopen(path, "r");
1704 if (fd == NULL)
1705 return -1;
1706
1707 fgets_ret = fgets(buf, (int)count, fd);
1708 if (NULL != fgets_ret) {
1709 rv = (int)strlen(buf);
1710 } else {
1711 rv = ferror(fd);
1712 }
1713 fclose(fd);
1714
1715 return rv;
1716}
1717
Ashish Jainf1eaa582016-05-23 20:54:24 +05301718/*Translates ALSA formats to AOSP PCM formats*/
1719audio_format_t alsa_format_to_hal(uint32_t alsa_format)
1720{
1721 audio_format_t format;
1722
1723 switch(alsa_format) {
1724 case SNDRV_PCM_FORMAT_S16_LE:
1725 format = AUDIO_FORMAT_PCM_16_BIT;
1726 break;
1727 case SNDRV_PCM_FORMAT_S24_3LE:
1728 format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
1729 break;
1730 case SNDRV_PCM_FORMAT_S24_LE:
1731 format = AUDIO_FORMAT_PCM_8_24_BIT;
1732 break;
1733 case SNDRV_PCM_FORMAT_S32_LE:
1734 format = AUDIO_FORMAT_PCM_32_BIT;
1735 break;
1736 default:
1737 ALOGW("Incorrect ALSA format");
1738 format = AUDIO_FORMAT_INVALID;
1739 }
1740 return format;
1741}
1742
1743/*Translates hal format (AOSP) to alsa formats*/
1744uint32_t hal_format_to_alsa(audio_format_t hal_format)
1745{
1746 uint32_t alsa_format;
1747
1748 switch (hal_format) {
1749 case AUDIO_FORMAT_PCM_32_BIT: {
1750 if (platform_supports_true_32bit())
1751 alsa_format = SNDRV_PCM_FORMAT_S32_LE;
1752 else
1753 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1754 }
1755 break;
1756 case AUDIO_FORMAT_PCM_8_BIT:
1757 alsa_format = SNDRV_PCM_FORMAT_S8;
1758 break;
1759 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
1760 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1761 break;
1762 case AUDIO_FORMAT_PCM_8_24_BIT: {
1763 if (platform_supports_true_32bit())
1764 alsa_format = SNDRV_PCM_FORMAT_S32_LE;
1765 else
1766 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1767 }
1768 break;
1769 case AUDIO_FORMAT_PCM_FLOAT:
1770 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1771 break;
1772 default:
1773 case AUDIO_FORMAT_PCM_16_BIT:
1774 alsa_format = SNDRV_PCM_FORMAT_S16_LE;
1775 break;
1776 }
1777 return alsa_format;
1778}
1779
Ashish Jain83a6cc22016-06-28 14:34:17 +05301780/*Translates PCM formats to AOSP formats*/
1781audio_format_t pcm_format_to_hal(uint32_t pcm_format)
1782{
1783 audio_format_t format = AUDIO_FORMAT_INVALID;
1784
1785 switch(pcm_format) {
1786 case PCM_FORMAT_S16_LE:
1787 format = AUDIO_FORMAT_PCM_16_BIT;
1788 break;
1789 case PCM_FORMAT_S24_3LE:
1790 format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
1791 break;
1792 case PCM_FORMAT_S24_LE:
1793 format = AUDIO_FORMAT_PCM_8_24_BIT;
1794 break;
1795 case PCM_FORMAT_S32_LE:
1796 format = AUDIO_FORMAT_PCM_32_BIT;
1797 break;
1798 default:
1799 ALOGW("Incorrect PCM format");
1800 format = AUDIO_FORMAT_INVALID;
1801 }
1802 return format;
1803}
1804
1805/*Translates hal format (AOSP) to alsa formats*/
1806uint32_t hal_format_to_pcm(audio_format_t hal_format)
1807{
1808 uint32_t pcm_format;
1809
1810 switch (hal_format) {
1811 case AUDIO_FORMAT_PCM_32_BIT:
1812 case AUDIO_FORMAT_PCM_8_24_BIT:
1813 case AUDIO_FORMAT_PCM_FLOAT: {
1814 if (platform_supports_true_32bit())
1815 pcm_format = PCM_FORMAT_S32_LE;
1816 else
1817 pcm_format = PCM_FORMAT_S24_3LE;
1818 }
1819 break;
1820 case AUDIO_FORMAT_PCM_8_BIT:
1821 pcm_format = PCM_FORMAT_S8;
1822 break;
1823 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
1824 pcm_format = PCM_FORMAT_S24_3LE;
1825 break;
1826 default:
1827 case AUDIO_FORMAT_PCM_16_BIT:
1828 pcm_format = PCM_FORMAT_S16_LE;
1829 break;
1830 }
1831 return pcm_format;
1832}
1833
Ashish Jainf1eaa582016-05-23 20:54:24 +05301834uint32_t get_alsa_fragment_size(uint32_t bytes_per_sample,
1835 uint32_t sample_rate,
Deeraj Soman65358ab2019-02-07 15:40:49 +05301836 uint32_t noOfChannels,
1837 int64_t duration_ms)
Ashish Jainf1eaa582016-05-23 20:54:24 +05301838{
1839 uint32_t fragment_size = 0;
1840 uint32_t pcm_offload_time = PCM_OFFLOAD_BUFFER_DURATION;
1841
Deeraj Soman65358ab2019-02-07 15:40:49 +05301842 if (duration_ms >= MIN_OFFLOAD_BUFFER_DURATION_MS && duration_ms <= MAX_OFFLOAD_BUFFER_DURATION_MS)
1843 pcm_offload_time = duration_ms;
1844
Ashish Jainf1eaa582016-05-23 20:54:24 +05301845 fragment_size = (pcm_offload_time
1846 * sample_rate
1847 * bytes_per_sample
1848 * noOfChannels)/1000;
1849 if (fragment_size < MIN_PCM_OFFLOAD_FRAGMENT_SIZE)
1850 fragment_size = MIN_PCM_OFFLOAD_FRAGMENT_SIZE;
1851 else if (fragment_size > MAX_PCM_OFFLOAD_FRAGMENT_SIZE)
1852 fragment_size = MAX_PCM_OFFLOAD_FRAGMENT_SIZE;
1853 /*To have same PCM samples for all channels, the buffer size requires to
1854 *be multiple of (number of channels * bytes per sample)
1855 *For writes to succeed, the buffer must be written at address which is multiple of 32
1856 */
Aalique Grahameb85f2d92017-10-16 17:53:23 -07001857 fragment_size = ALIGN(fragment_size, (bytes_per_sample * noOfChannels * 32));
Ashish Jainf1eaa582016-05-23 20:54:24 +05301858
1859 ALOGI("PCM offload Fragment size to %d bytes", fragment_size);
1860 return fragment_size;
1861}
1862
1863/* Calculates the fragment size required to configure compress session.
1864 * Based on the alsa format selected, decide if conversion is needed in
1865
1866 * HAL ( e.g. convert AUDIO_FORMAT_PCM_FLOAT input format to
1867 * AUDIO_FORMAT_PCM_24_BIT_PACKED before writing to the compress driver.
1868 */
1869void audio_extn_utils_update_direct_pcm_fragment_size(struct stream_out *out)
1870{
Ashish Jain83a6cc22016-06-28 14:34:17 +05301871 audio_format_t dst_format = out->hal_op_format;
1872 audio_format_t src_format = out->hal_ip_format;
Ashish Jainf1eaa582016-05-23 20:54:24 +05301873 uint32_t hal_op_bytes_per_sample = audio_bytes_per_sample(dst_format);
1874 uint32_t hal_ip_bytes_per_sample = audio_bytes_per_sample(src_format);
1875
1876 out->compr_config.fragment_size =
1877 get_alsa_fragment_size(hal_op_bytes_per_sample,
1878 out->sample_rate,
Deeraj Soman65358ab2019-02-07 15:40:49 +05301879 popcount(out->channel_mask),
1880 out->info.duration_us / 1000);
Ashish Jainf1eaa582016-05-23 20:54:24 +05301881
1882 if ((src_format != dst_format) &&
1883 hal_op_bytes_per_sample != hal_ip_bytes_per_sample) {
1884
Ashish Jain83a6cc22016-06-28 14:34:17 +05301885 out->hal_fragment_size =
Ashish Jainf1eaa582016-05-23 20:54:24 +05301886 ((out->compr_config.fragment_size * hal_ip_bytes_per_sample) /
1887 hal_op_bytes_per_sample);
1888 ALOGI("enable conversion hal_input_fragment_size is %d src_format %x dst_format %x",
Ashish Jain83a6cc22016-06-28 14:34:17 +05301889 out->hal_fragment_size, src_format, dst_format);
Ashish Jainf1eaa582016-05-23 20:54:24 +05301890 } else {
Ashish Jain83a6cc22016-06-28 14:34:17 +05301891 out->hal_fragment_size = out->compr_config.fragment_size;
Ashish Jainf1eaa582016-05-23 20:54:24 +05301892 }
1893}
1894
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301895/* converts pcm format 24_8 to 8_24 inplace */
1896size_t audio_extn_utils_convert_format_24_8_to_8_24(void *buf, size_t bytes)
1897{
1898 size_t i = 0;
1899 int *int_buf_stream = buf;
1900
1901 if ((bytes % 4) != 0) {
1902 ALOGE("%s: wrong inout buffer! ... is not 32 bit aligned ", __func__);
1903 return -EINVAL;
1904 }
1905
1906 for (; i < (bytes / 4); i++)
1907 int_buf_stream[i] >>= 8;
1908
1909 return bytes;
1910}
1911
Xiaojun Sang782e5b12020-06-29 21:13:06 +08001912#ifdef AUDIO_GKI_ENABLED
1913int get_snd_codec_id(audio_format_t format)
1914{
1915 int id = 0;
1916
1917 switch (format & AUDIO_FORMAT_MAIN_MASK) {
1918 case AUDIO_FORMAT_MP3:
1919 id = SND_AUDIOCODEC_MP3;
1920 break;
1921 case AUDIO_FORMAT_AAC:
1922 id = SND_AUDIOCODEC_AAC;
1923 break;
1924 case AUDIO_FORMAT_AAC_ADTS:
1925 id = SND_AUDIOCODEC_AAC;
1926 break;
1927 case AUDIO_FORMAT_AAC_LATM:
1928 id = SND_AUDIOCODEC_AAC;
1929 break;
1930 case AUDIO_FORMAT_PCM:
1931 id = SND_AUDIOCODEC_PCM;
1932 break;
1933 case AUDIO_FORMAT_FLAC:
1934 case AUDIO_FORMAT_ALAC:
1935 case AUDIO_FORMAT_APE:
1936 case AUDIO_FORMAT_VORBIS:
1937 case AUDIO_FORMAT_WMA:
1938 case AUDIO_FORMAT_WMA_PRO:
1939 case AUDIO_FORMAT_DSD:
1940 case AUDIO_FORMAT_APTX:
1941 id = SND_AUDIOCODEC_BESPOKE;
1942 break;
1943 case AUDIO_FORMAT_MP2:
1944 id = SND_AUDIOCODEC_MP2;
1945 break;
1946 case AUDIO_FORMAT_AC3:
1947 id = SND_AUDIOCODEC_AC3;
1948 break;
1949 case AUDIO_FORMAT_E_AC3:
1950 case AUDIO_FORMAT_E_AC3_JOC:
1951 id = SND_AUDIOCODEC_EAC3;
1952 break;
1953 case AUDIO_FORMAT_DTS:
1954 case AUDIO_FORMAT_DTS_HD:
1955 id = SND_AUDIOCODEC_DTS;
1956 break;
1957 case AUDIO_FORMAT_DOLBY_TRUEHD:
1958 id = SND_AUDIOCODEC_TRUEHD;
1959 break;
1960 case AUDIO_FORMAT_IEC61937:
1961 id = SND_AUDIOCODEC_IEC61937;
1962 break;
1963 default:
1964 ALOGE("%s: Unsupported audio format :%x", __func__, format);
1965 }
1966
1967 return id;
1968}
1969#else
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301970int get_snd_codec_id(audio_format_t format)
1971{
1972 int id = 0;
1973
1974 switch (format & AUDIO_FORMAT_MAIN_MASK) {
1975 case AUDIO_FORMAT_MP3:
1976 id = SND_AUDIOCODEC_MP3;
1977 break;
1978 case AUDIO_FORMAT_AAC:
1979 id = SND_AUDIOCODEC_AAC;
1980 break;
1981 case AUDIO_FORMAT_AAC_ADTS:
1982 id = SND_AUDIOCODEC_AAC;
1983 break;
Arun Kumar Dasari3b174182016-12-27 13:01:14 +05301984 case AUDIO_FORMAT_AAC_LATM:
1985 id = SND_AUDIOCODEC_AAC;
1986 break;
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301987 case AUDIO_FORMAT_PCM:
1988 id = SND_AUDIOCODEC_PCM;
1989 break;
1990 case AUDIO_FORMAT_FLAC:
1991 id = SND_AUDIOCODEC_FLAC;
1992 break;
1993 case AUDIO_FORMAT_ALAC:
1994 id = SND_AUDIOCODEC_ALAC;
1995 break;
1996 case AUDIO_FORMAT_APE:
1997 id = SND_AUDIOCODEC_APE;
1998 break;
1999 case AUDIO_FORMAT_VORBIS:
2000 id = SND_AUDIOCODEC_VORBIS;
2001 break;
2002 case AUDIO_FORMAT_WMA:
2003 id = SND_AUDIOCODEC_WMA;
2004 break;
2005 case AUDIO_FORMAT_WMA_PRO:
2006 id = SND_AUDIOCODEC_WMA_PRO;
2007 break;
Satish Babu Patakokila0c313922016-12-08 12:07:08 +05302008 case AUDIO_FORMAT_MP2:
2009 id = SND_AUDIOCODEC_MP2;
2010 break;
Satish Babu Patakokila915ecba2017-01-10 17:43:56 +05302011 case AUDIO_FORMAT_AC3:
2012 id = SND_AUDIOCODEC_AC3;
2013 break;
2014 case AUDIO_FORMAT_E_AC3:
2015 case AUDIO_FORMAT_E_AC3_JOC:
2016 id = SND_AUDIOCODEC_EAC3;
2017 break;
2018 case AUDIO_FORMAT_DTS:
2019 case AUDIO_FORMAT_DTS_HD:
2020 id = SND_AUDIOCODEC_DTS;
2021 break;
Ben Romberger1aaaf862017-04-06 17:49:46 -07002022 case AUDIO_FORMAT_DOLBY_TRUEHD:
2023 id = SND_AUDIOCODEC_TRUEHD;
2024 break;
Naresh Tanniru928f0862017-04-07 16:44:23 -07002025 case AUDIO_FORMAT_IEC61937:
2026 id = SND_AUDIOCODEC_IEC61937;
2027 break;
Preetam Singh Ranawat4277a5a2017-01-18 19:02:24 +05302028 case AUDIO_FORMAT_DSD:
2029 id = SND_AUDIOCODEC_DSD;
2030 break;
Dhanalakshmi Siddani18737932016-11-29 17:33:17 +05302031 case AUDIO_FORMAT_APTX:
2032 id = SND_AUDIOCODEC_APTX;
2033 break;
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05302034 default:
2035 ALOGE("%s: Unsupported audio format :%x", __func__, format);
2036 }
2037
2038 return id;
2039}
Xiaojun Sang782e5b12020-06-29 21:13:06 +08002040#endif
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05302041
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07002042void audio_extn_utils_send_audio_calibration(struct audio_device *adev,
2043 struct audio_usecase *usecase)
2044{
2045 int type = usecase->type;
2046
Dhananjay Kumar14245982017-01-16 20:21:00 +05302047 if (type == PCM_PLAYBACK && usecase->stream.out != NULL) {
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05302048 platform_send_audio_calibration(adev->platform, usecase,
Zhou Song06761dd2019-04-28 18:08:17 +08002049 usecase->stream.out->app_type_cfg.app_type);
Dhananjay Kumar14245982017-01-16 20:21:00 +05302050 } else if (type == PCM_CAPTURE && usecase->stream.in != NULL) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +05302051 platform_send_audio_calibration(adev->platform, usecase,
Zhou Song06761dd2019-04-28 18:08:17 +08002052 usecase->stream.in->app_type_cfg.app_type);
Sriram Kumar9c836482020-10-27 20:34:35 +05302053 } else if (type == PCM_HFP_CALL) {
2054 /* when app type is default. the sample rate is not used to send cal */
2055#ifdef ENABLE_HFP_CALIBRATION
2056 platform_send_audio_calibration_hfp(adev->platform, usecase->in_snd_device);
2057#else
2058 platform_send_audio_calibration(adev->platform, usecase,
2059 platform_get_default_app_type_v2(adev->platform, usecase->type));
2060#endif
2061 } else if ((type == PCM_CAPTURE) ||
Derek Chena30a5f42019-12-03 11:17:09 -05002062 (type == TRANSCODE_LOOPBACK_RX && usecase->stream.inout != NULL) ||
Fei Tongaffdf732020-02-20 20:39:05 +08002063 (type == ICC_CALL) || (type == SYNTH_LOOPBACK)) {
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05302064 platform_send_audio_calibration(adev->platform, usecase,
Zhou Song06761dd2019-04-28 18:08:17 +08002065 platform_get_default_app_type_v2(adev->platform, usecase->type));
Vidyakumar Athotae57f6002017-03-01 13:13:16 -08002066 } else {
2067 /* No need to send audio calibration for voice and voip call usecases */
2068 if ((type != VOICE_CALL) && (type != VOIP_CALL))
2069 ALOGW("%s: No audio calibration for usecase type = %d", __func__, type);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07002070 }
2071}
2072
Ben Rombergera04fabc2014-11-14 12:16:03 -08002073// Base64 Encode and Decode
2074// Not all features supported. This must be used only with following conditions.
2075// Decode Modes: Support with and without padding
2076// CRLF not handling. So no CRLF in string to decode.
2077// Encode Modes: Supports only padding
2078int b64decode(char *inp, int ilen, uint8_t* outp)
2079{
2080 int i, j, k, ii, num;
2081 int rem, pcnt;
2082 uint32_t res=0;
2083 uint8_t getIndex[MAX_BASEINDEX_LEN];
2084 uint8_t tmp, cflag;
2085
2086 if(inp == NULL || outp == NULL || ilen <= 0) {
2087 ALOGE("[%s] received NULL pointer or zero length",__func__);
2088 return -1;
2089 }
2090
2091 memset(getIndex, MAX_BASEINDEX_LEN-1, sizeof(getIndex));
2092 for(i=0;i<BASE_TABLE_SIZE;i++) {
2093 getIndex[(uint8_t)bTable[i]] = (uint8_t)i;
2094 }
2095 getIndex[(uint8_t)'=']=0;
2096
2097 j=0;k=0;
2098 num = ilen/4;
2099 rem = ilen%4;
2100 if(rem==0)
2101 num = num-1;
2102 cflag=0;
2103 for(i=0; i<num; i++) {
2104 res=0;
2105 for(ii=0;ii<4;ii++) {
2106 res = res << 6;
2107 tmp = getIndex[(uint8_t)inp[j++]];
2108 res = res | tmp;
2109 cflag = cflag | tmp;
2110 }
2111 outp[k++] = (res >> 16)&0xFF;
2112 outp[k++] = (res >> 8)&0xFF;
2113 outp[k++] = res & 0xFF;
2114 }
2115
2116 // Handle last bytes special
2117 pcnt=0;
2118 if(rem == 0) {
2119 //With padding or full data
2120 res = 0;
2121 for(ii=0;ii<4;ii++) {
2122 if(inp[j] == '=')
2123 pcnt++;
2124 res = res << 6;
2125 tmp = getIndex[(uint8_t)inp[j++]];
2126 res = res | tmp;
2127 cflag = cflag | tmp;
2128 }
2129 outp[k++] = res >> 16;
2130 if(pcnt == 2)
2131 goto done;
2132 outp[k++] = (res>>8)&0xFF;
2133 if(pcnt == 1)
2134 goto done;
2135 outp[k++] = res&0xFF;
2136 } else {
2137 //without padding
2138 res = 0;
2139 for(i=0;i<rem;i++) {
2140 res = res << 6;
2141 tmp = getIndex[(uint8_t)inp[j++]];
2142 res = res | tmp;
2143 cflag = cflag | tmp;
2144 }
2145 for(i=rem;i<4;i++) {
2146 res = res << 6;
2147 pcnt++;
2148 }
2149 outp[k++] = res >> 16;
2150 if(pcnt == 2)
2151 goto done;
2152 outp[k++] = (res>>8)&0xFF;
2153 if(pcnt == 1)
2154 goto done;
2155 outp[k++] = res&0xFF;
2156 }
2157done:
2158 if(cflag == 0xFF) {
2159 ALOGE("[%s] base64 decode failed. Invalid character found %s",
2160 __func__, inp);
2161 return 0;
2162 }
2163 return k;
2164}
2165
2166int b64encode(uint8_t *inp, int ilen, char* outp)
2167{
2168 int i,j,k, num;
2169 int rem=0;
2170 uint32_t res=0;
2171
2172 if(inp == NULL || outp == NULL || ilen<=0) {
2173 ALOGE("[%s] received NULL pointer or zero input length",__func__);
2174 return -1;
2175 }
2176
2177 num = ilen/3;
2178 rem = ilen%3;
2179 j=0;k=0;
2180 for(i=0; i<num; i++) {
2181 //prepare index
2182 res = inp[j++]<<16;
2183 res = res | inp[j++]<<8;
2184 res = res | inp[j++];
2185 //get output map from index
2186 outp[k++] = (char) bTable[(res>>18)&0x3F];
2187 outp[k++] = (char) bTable[(res>>12)&0x3F];
2188 outp[k++] = (char) bTable[(res>>6)&0x3F];
2189 outp[k++] = (char) bTable[res&0x3F];
2190 }
2191
2192 switch(rem) {
2193 case 1:
2194 res = inp[j++]<<16;
2195 outp[k++] = (char) bTable[res>>18];
2196 outp[k++] = (char) bTable[(res>>12)&0x3F];
2197 //outp[k++] = '=';
2198 //outp[k++] = '=';
2199 break;
2200 case 2:
2201 res = inp[j++]<<16;
2202 res = res | inp[j++]<<8;
2203 outp[k++] = (char) bTable[res>>18];
2204 outp[k++] = (char) bTable[(res>>12)&0x3F];
2205 outp[k++] = (char) bTable[(res>>6)&0x3F];
2206 //outp[k++] = '=';
2207 break;
2208 default:
2209 break;
2210 }
Ben Rombergera04fabc2014-11-14 12:16:03 -08002211 outp[k] = '\0';
2212 return k;
2213}
Ashish Jain81eb2a82015-05-13 10:52:34 +05302214
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05302215
2216int audio_extn_utils_get_codec_version(const char *snd_card_name,
2217 int card_num,
2218 char *codec_version)
2219{
2220 char procfs_path[50];
2221 FILE *fp;
2222
2223 if (strstr(snd_card_name, "tasha")) {
2224 snprintf(procfs_path, sizeof(procfs_path),
2225 "/proc/asound/card%d/codecs/tasha/version", card_num);
2226 if ((fp = fopen(procfs_path, "r")) != NULL) {
2227 fgets(codec_version, CODEC_VERSION_MAX_LENGTH, fp);
2228 fclose(fp);
2229 } else {
2230 ALOGE("%s: ERROR. cannot open %s", __func__, procfs_path);
2231 return -ENOENT;
2232 }
2233 ALOGD("%s: codec version %s", __func__, codec_version);
2234 }
2235
2236 return 0;
2237}
2238
Preetam Singh Ranawat9d0d8e42019-07-15 12:27:25 +05302239int audio_extn_utils_get_codec_variant(int card_num,
2240 char *codec_variant)
2241{
2242 char procfs_path[50];
2243 FILE *fp;
2244 snprintf(procfs_path, sizeof(procfs_path),
2245 "/proc/asound/card%d/codecs/wcd938x/variant", card_num);
2246 if ((fp = fopen(procfs_path, "r")) == NULL) {
2247 snprintf(procfs_path, sizeof(procfs_path),
2248 "/proc/asound/card%d/codecs/wcd937x/variant", card_num);
2249 if ((fp = fopen(procfs_path, "r")) == NULL) {
2250 ALOGE("%s: ERROR. cannot open %s", __func__, procfs_path);
2251 return -ENOENT;
2252 }
2253 }
2254 fgets(codec_variant, CODEC_VARIANT_MAX_LENGTH, fp);
2255 fclose(fp);
2256 ALOGD("%s: codec variant is %s", __func__, codec_variant);
2257 return 0;
2258}
2259
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05302260
Ashish Jain81eb2a82015-05-13 10:52:34 +05302261#ifdef AUDIO_EXTERNAL_HDMI_ENABLED
2262
2263void get_default_compressed_channel_status(
2264 unsigned char *channel_status)
2265{
Ashish Jain81eb2a82015-05-13 10:52:34 +05302266 memset(channel_status,0,24);
2267
2268 /* block start bit in preamble bit 3 */
2269 channel_status[0] |= PROFESSIONAL;
2270 //compre out
2271 channel_status[0] |= NON_LPCM;
2272 // sample rate; fixed 48K for default/transcode
2273 channel_status[3] |= SR_48000;
2274}
2275
Ashish Jain81eb2a82015-05-13 10:52:34 +05302276int32_t get_compressed_channel_status(void *audio_stream_data,
2277 uint32_t audio_frame_size,
2278 unsigned char *channel_status,
2279 enum audio_parser_code_type codec_type)
2280 // codec_type - AUDIO_PARSER_CODEC_AC3
2281 // - AUDIO_PARSER_CODEC_DTS
2282{
2283 unsigned char *stream;
2284 int ret = 0;
2285 stream = (unsigned char *)audio_stream_data;
2286
2287 if (audio_stream_data == NULL || audio_frame_size == 0) {
2288 ALOGW("no buffer to get channel status, return default for compress");
2289 get_default_compressed_channel_status(channel_status);
2290 return ret;
2291 }
2292
2293 memset(channel_status,0,24);
2294 if(init_audio_parser(stream, audio_frame_size, codec_type) == -1)
2295 {
2296 ALOGE("init audio parser failed");
2297 return -1;
2298 }
2299 ret = get_channel_status(channel_status, codec_type);
2300 return ret;
2301
2302}
2303
Ashish Jain81eb2a82015-05-13 10:52:34 +05302304void get_lpcm_channel_status(uint32_t sampleRate,
2305 unsigned char *channel_status)
2306{
2307 int32_t status = 0;
Ashish Jain81eb2a82015-05-13 10:52:34 +05302308 memset(channel_status,0,24);
2309 /* block start bit in preamble bit 3 */
2310 channel_status[0] |= PROFESSIONAL;
2311 //LPCM OUT
2312 channel_status[0] &= ~NON_LPCM;
2313
2314 switch (sampleRate) {
2315 case 8000:
2316 case 11025:
2317 case 12000:
2318 case 16000:
2319 case 22050:
2320 channel_status[3] |= SR_NOTID;
Preetam Singh Ranawat61716b12015-12-14 11:55:24 +05302321 break;
Ashish Jain81eb2a82015-05-13 10:52:34 +05302322 case 24000:
2323 channel_status[3] |= SR_24000;
2324 break;
2325 case 32000:
2326 channel_status[3] |= SR_32000;
2327 break;
2328 case 44100:
2329 channel_status[3] |= SR_44100;
2330 break;
2331 case 48000:
2332 channel_status[3] |= SR_48000;
2333 break;
2334 case 88200:
2335 channel_status[3] |= SR_88200;
2336 break;
2337 case 96000:
2338 channel_status[3] |= SR_96000;
2339 break;
2340 case 176400:
2341 channel_status[3] |= SR_176400;
2342 break;
2343 case 192000:
2344 channel_status[3] |= SR_192000;
2345 break;
2346 default:
2347 ALOGV("Invalid sample_rate %u\n", sampleRate);
2348 status = -1;
2349 break;
2350 }
2351}
2352
2353void audio_utils_set_hdmi_channel_status(struct stream_out *out, char * buffer, size_t bytes)
2354{
2355 unsigned char channel_status[24]={0};
2356 struct snd_aes_iec958 iec958;
2357 const char *mixer_ctl_name = "IEC958 Playback PCM Stream";
2358 struct mixer_ctl *ctl;
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +05302359 ALOGV("%s: buffer %s bytes %zd", __func__, buffer, bytes);
Arun Mirpurie008ed22019-03-21 11:21:04 -07002360
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05302361 if (audio_extn_utils_is_dolby_format(out->format) &&
Ashish Jain81eb2a82015-05-13 10:52:34 +05302362 /*TODO:Extend code to support DTS passthrough*/
2363 /*set compressed channel status bits*/
Arun Mirpurie008ed22019-03-21 11:21:04 -07002364 audio_extn_passthru_is_passthrough_stream(out) &&
2365 audio_extn_is_hdmi_passthru_enabled()) {
Ashish Jain81eb2a82015-05-13 10:52:34 +05302366 get_compressed_channel_status(buffer, bytes, channel_status, AUDIO_PARSER_CODEC_AC3);
Arun Mirpurie008ed22019-03-21 11:21:04 -07002367 } else {
Ashish Jain81eb2a82015-05-13 10:52:34 +05302368 /*set channel status bit for LPCM*/
2369 get_lpcm_channel_status(out->sample_rate, channel_status);
2370 }
2371
2372 memcpy(iec958.status, channel_status,sizeof(iec958.status));
2373 ctl = mixer_get_ctl_by_name(out->dev->mixer, mixer_ctl_name);
2374 if (!ctl) {
2375 ALOGE("%s: Could not get ctl for mixer cmd - %s",
2376 __func__, mixer_ctl_name);
2377 return;
2378 }
2379 if (mixer_ctl_set_array(ctl, &iec958, sizeof(iec958)) < 0) {
2380 ALOGE("%s: Could not set channel status for ext HDMI ",
2381 __func__);
2382 return;
2383 }
2384
2385}
2386#endif
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302387
2388int audio_extn_utils_get_avt_device_drift(
2389 struct audio_usecase *usecase,
2390 struct audio_avt_device_drift_param *drift_param)
2391{
2392 int ret = 0, count = 0;
2393 char avt_device_drift_mixer_ctl_name[MIXER_PATH_MAX_LENGTH] = {0};
Naresh Tanniru6160c712017-04-17 15:43:48 +05302394 const char *backend = NULL;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302395 struct mixer_ctl *ctl = NULL;
2396 struct audio_avt_device_drift_stats drift_stats;
2397 struct audio_device *adev = NULL;
2398
2399 if (usecase != NULL && usecase->type == PCM_PLAYBACK) {
Naresh Tanniru6160c712017-04-17 15:43:48 +05302400 backend = platform_get_snd_device_backend_interface(usecase->out_snd_device);
2401 if (!backend) {
2402 ALOGE("%s: Unsupported device %d", __func__,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08002403 get_device_types(&usecase->stream.out->device_list));
Naresh Tanniru6160c712017-04-17 15:43:48 +05302404 ret = -EINVAL;
2405 goto done;
2406 }
2407 strlcpy(avt_device_drift_mixer_ctl_name,
2408 backend,
2409 MIXER_PATH_MAX_LENGTH);
2410
2411 count = strlen(backend);
2412 if (MIXER_PATH_MAX_LENGTH - count > 0) {
2413 strlcat(&avt_device_drift_mixer_ctl_name[count],
2414 " DRIFT",
2415 MIXER_PATH_MAX_LENGTH - count);
2416 } else {
2417 ret = -EINVAL;
2418 goto done;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302419 }
2420 } else {
Naresh Tanniru7586e292017-04-13 10:38:13 +05302421 ALOGE("%s: Invalid usecase",__func__);
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302422 ret = -EINVAL;
Naresh Tanniru6160c712017-04-17 15:43:48 +05302423 goto done;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302424 }
2425
Naresh Tanniru6160c712017-04-17 15:43:48 +05302426 adev = usecase->stream.out->dev;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302427 ctl = mixer_get_ctl_by_name(adev->mixer, avt_device_drift_mixer_ctl_name);
2428 if (!ctl) {
2429 ALOGE("%s: Could not get ctl for mixer cmd - %s",
2430 __func__, avt_device_drift_mixer_ctl_name);
2431
2432 ret = -EINVAL;
2433 goto done;
2434 }
2435
2436 ALOGV("%s: Getting AV Timer vs Device Drift mixer ctrl name %s", __func__,
2437 avt_device_drift_mixer_ctl_name);
2438
2439 mixer_ctl_update(ctl);
2440 count = mixer_ctl_get_num_values(ctl);
2441 if (count != sizeof(struct audio_avt_device_drift_stats)) {
2442 ALOGE("%s: mixer_ctl_get_num_values() invalid drift_stats data size",
2443 __func__);
2444
2445 ret = -EINVAL;
2446 goto done;
2447 }
2448
2449 ret = mixer_ctl_get_array(ctl, (void *)&drift_stats, count);
2450 if (ret != 0) {
2451 ALOGE("%s: mixer_ctl_get_array() failed to get drift_stats Params",
2452 __func__);
2453
2454 ret = -EINVAL;
2455 goto done;
2456 }
2457 memcpy(drift_param, &drift_stats.drift_param,
2458 sizeof(struct audio_avt_device_drift_param));
2459done:
2460 return ret;
2461}
Manish Dewangan07de2142017-02-27 19:27:20 +05302462
2463#ifdef SNDRV_COMPRESS_PATH_DELAY
2464int audio_extn_utils_compress_get_dsp_latency(struct stream_out *out)
2465{
2466 int ret = -EINVAL;
2467 struct snd_compr_metadata metadata;
2468 int delay_ms = COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
2469
Weiyin Jiangc49a3b52018-08-17 16:16:08 +08002470 /* override the latency for pcm offload use case */
2471 if ((out->flags & AUDIO_OUTPUT_FLAG_DIRECT) &&
2472 !(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)) {
2473 delay_ms = PCM_OFFLOAD_PLAYBACK_LATENCY;
2474 }
2475
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -07002476 if (property_get_bool("vendor.audio.playback.dsp.pathdelay", false)) {
Manish Dewangan07de2142017-02-27 19:27:20 +05302477 ALOGD("%s:: Quering DSP delay %d",__func__, __LINE__);
2478 if (!(is_offload_usecase(out->usecase))) {
2479 ALOGE("%s:: not supported for non offload session", __func__);
2480 goto exit;
2481 }
2482
2483 if (!out->compr) {
2484 ALOGD("%s:: Invalid compress handle,returning default dsp latency",
2485 __func__);
2486 goto exit;
2487 }
2488
2489 metadata.key = SNDRV_COMPRESS_PATH_DELAY;
2490 ret = compress_get_metadata(out->compr, &metadata);
2491 if(ret) {
2492 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2493 goto exit;
2494 }
2495 delay_ms = metadata.value[0] / 1000; /*convert to ms*/
2496 } else {
2497 ALOGD("%s:: Using Fix DSP delay",__func__);
2498 }
2499
2500exit:
2501 ALOGD("%s:: delay in ms is %d",__func__, delay_ms);
2502 return delay_ms;
2503}
2504#else
2505int audio_extn_utils_compress_get_dsp_latency(struct stream_out *out __unused)
2506{
2507 return COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
2508}
2509#endif
Manish Dewangan69426c82017-01-30 17:35:36 +05302510
2511#ifdef SNDRV_COMPRESS_RENDER_MODE
2512int audio_extn_utils_compress_set_render_mode(struct stream_out *out)
2513{
2514 struct snd_compr_metadata metadata;
2515 int ret = -EINVAL;
2516
2517 if (!(is_offload_usecase(out->usecase))) {
2518 ALOGE("%s:: not supported for non offload session", __func__);
2519 goto exit;
2520 }
2521
2522 if (!out->compr) {
2523 ALOGD("%s:: Invalid compress handle",
2524 __func__);
2525 goto exit;
2526 }
2527
2528 ALOGD("%s:: render mode %d", __func__, out->render_mode);
2529
2530 metadata.key = SNDRV_COMPRESS_RENDER_MODE;
2531 if (out->render_mode == RENDER_MODE_AUDIO_MASTER) {
2532 metadata.value[0] = SNDRV_COMPRESS_RENDER_MODE_AUDIO_MASTER;
2533 } else if (out->render_mode == RENDER_MODE_AUDIO_STC_MASTER) {
2534 metadata.value[0] = SNDRV_COMPRESS_RENDER_MODE_STC_MASTER;
2535 } else {
2536 ret = 0;
2537 goto exit;
2538 }
2539 ret = compress_set_metadata(out->compr, &metadata);
2540 if(ret) {
2541 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2542 }
2543exit:
2544 return ret;
2545}
2546#else
2547int audio_extn_utils_compress_set_render_mode(struct stream_out *out __unused)
2548{
2549 ALOGD("%s:: configuring render mode not supported", __func__);
2550 return 0;
2551}
2552#endif
Manish Dewangan58229382017-02-02 15:48:41 +05302553
2554#ifdef SNDRV_COMPRESS_CLK_REC_MODE
2555int audio_extn_utils_compress_set_clk_rec_mode(
2556 struct audio_usecase *usecase)
2557{
2558 struct snd_compr_metadata metadata;
2559 struct stream_out *out = NULL;
2560 int ret = -EINVAL;
2561
Naresh Tanniru7586e292017-04-13 10:38:13 +05302562 if (usecase == NULL || usecase->type != PCM_PLAYBACK) {
Manish Dewangan58229382017-02-02 15:48:41 +05302563 ALOGE("%s:: Invalid use case", __func__);
2564 goto exit;
2565 }
2566
2567 out = usecase->stream.out;
2568 if (!out) {
2569 ALOGE("%s:: invalid stream", __func__);
2570 goto exit;
2571 }
2572
2573 if (!is_offload_usecase(out->usecase)) {
2574 ALOGE("%s:: not supported for non offload session", __func__);
2575 goto exit;
2576 }
2577
2578 if (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER) {
2579 ALOGD("%s:: clk recovery is only supported in STC render mode",
2580 __func__);
2581 ret = 0;
2582 goto exit;
2583 }
2584
2585 if (!out->compr) {
2586 ALOGD("%s:: Invalid compress handle",
2587 __func__);
2588 goto exit;
2589 }
2590 metadata.key = SNDRV_COMPRESS_CLK_REC_MODE;
2591 switch(usecase->out_snd_device) {
2592 case SND_DEVICE_OUT_HDMI:
2593 case SND_DEVICE_OUT_SPEAKER_AND_HDMI:
2594 case SND_DEVICE_OUT_DISPLAY_PORT:
2595 case SND_DEVICE_OUT_SPEAKER_AND_DISPLAY_PORT:
2596 metadata.value[0] = SNDRV_COMPRESS_CLK_REC_MODE_NONE;
2597 break;
2598 default:
2599 metadata.value[0] = SNDRV_COMPRESS_CLK_REC_MODE_AUTO;
2600 break;
2601 }
2602
2603 ALOGD("%s:: clk recovery mode %d",__func__, metadata.value[0]);
2604
2605 ret = compress_set_metadata(out->compr, &metadata);
2606 if(ret) {
2607 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2608 }
2609
2610exit:
2611 return ret;
2612}
2613#else
2614int audio_extn_utils_compress_set_clk_rec_mode(
2615 struct audio_usecase *usecase __unused)
2616{
2617 ALOGD("%s:: configuring render mode not supported", __func__);
2618 return 0;
2619}
2620#endif
Manish Dewangan27346042017-03-01 12:56:12 +05302621
2622#ifdef SNDRV_COMPRESS_RENDER_WINDOW
2623int audio_extn_utils_compress_set_render_window(
2624 struct stream_out *out,
2625 struct audio_out_render_window_param *render_window)
2626{
2627 struct snd_compr_metadata metadata;
2628 int ret = -EINVAL;
2629
Manish Dewangan27346042017-03-01 12:56:12 +05302630 if(render_window == NULL) {
2631 ALOGE("%s:: Invalid render_window", __func__);
2632 goto exit;
2633 }
2634
Aniket Kumar Lata1e1d3662017-06-01 18:45:48 -07002635 ALOGD("%s:: render window start 0x%"PRIx64" end 0x%"PRIx64"",
2636 __func__,render_window->render_ws, render_window->render_we);
2637
Manish Dewangan27346042017-03-01 12:56:12 +05302638 if (!is_offload_usecase(out->usecase)) {
2639 ALOGE("%s:: not supported for non offload session", __func__);
2640 goto exit;
2641 }
2642
Naresh Tanniru6160c712017-04-17 15:43:48 +05302643 if ((out->render_mode != RENDER_MODE_AUDIO_MASTER) &&
2644 (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER)) {
Manish Dewangan27346042017-03-01 12:56:12 +05302645 ALOGD("%s:: only supported in timestamp mode, current "
2646 "render mode mode %d", __func__, out->render_mode);
2647 goto exit;
2648 }
2649
2650 if (!out->compr) {
2651 ALOGW("%s:: offload session not yet opened,"
2652 "render window will be configure later", __func__);
2653 /* store render window to reconfigure in start_output_stream() */
2654 goto exit;
2655 }
2656
2657 metadata.key = SNDRV_COMPRESS_RENDER_WINDOW;
2658 /*render window start value */
2659 metadata.value[0] = 0xFFFFFFFF & render_window->render_ws; /* lsb */
2660 metadata.value[1] = \
2661 (0xFFFFFFFF00000000 & render_window->render_ws) >> 32; /* msb*/
2662 /*render window end value */
2663 metadata.value[2] = 0xFFFFFFFF & render_window->render_we; /* lsb */
2664 metadata.value[3] = \
2665 (0xFFFFFFFF00000000 & render_window->render_we) >> 32; /* msb*/
2666
2667 ret = compress_set_metadata(out->compr, &metadata);
2668 if(ret) {
2669 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2670 }
2671
2672exit:
2673 return ret;
2674}
2675#else
2676int audio_extn_utils_compress_set_render_window(
2677 struct stream_out *out __unused,
2678 struct audio_out_render_window_param *render_window __unused)
2679{
2680 ALOGD("%s:: configuring render window not supported", __func__);
2681 return 0;
2682}
2683#endif
Manish Dewangan14956cc2017-02-14 18:54:42 +05302684
2685#ifdef SNDRV_COMPRESS_START_DELAY
2686int audio_extn_utils_compress_set_start_delay(
2687 struct stream_out *out,
2688 struct audio_out_start_delay_param *delay_param)
2689{
2690 struct snd_compr_metadata metadata;
2691 int ret = -EINVAL;
2692
2693 if(delay_param == NULL) {
2694 ALOGE("%s:: Invalid delay_param", __func__);
2695 goto exit;
2696 }
2697
2698 ALOGD("%s:: render start delay 0x%"PRIx64" ", __func__,
2699 delay_param->start_delay);
2700
2701 if (!is_offload_usecase(out->usecase)) {
2702 ALOGE("%s:: not supported for non offload session", __func__);
2703 goto exit;
2704 }
2705
Naresh Tanniru6160c712017-04-17 15:43:48 +05302706 if ((out->render_mode != RENDER_MODE_AUDIO_MASTER) &&
2707 (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER)) {
Manish Dewangan14956cc2017-02-14 18:54:42 +05302708 ALOGD("%s:: only supported in timestamp mode, current "
2709 "render mode mode %d", __func__, out->render_mode);
2710 goto exit;
2711 }
2712
2713 if (!out->compr) {
2714 ALOGW("%s:: offload session not yet opened,"
2715 "start delay will be configure later", __func__);
2716 goto exit;
2717 }
2718
2719 metadata.key = SNDRV_COMPRESS_START_DELAY;
2720 metadata.value[0] = 0xFFFFFFFF & delay_param->start_delay; /* lsb */
2721 metadata.value[1] = \
2722 (0xFFFFFFFF00000000 & delay_param->start_delay) >> 32; /* msb*/
2723
2724 ret = compress_set_metadata(out->compr, &metadata);
2725 if(ret) {
2726 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2727 }
2728
2729exit:
2730 return ret;
2731}
2732#else
2733int audio_extn_utils_compress_set_start_delay(
2734 struct stream_out *out __unused,
2735 struct audio_out_start_delay_param *delay_param __unused)
2736{
2737 ALOGD("%s:: configuring render window not supported", __func__);
2738 return 0;
2739}
2740#endif
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002741
Surendar Karka287348c2019-04-10 18:31:46 +05302742#ifdef SNDRV_COMPRESS_DSP_POSITION
2743int audio_extn_utils_compress_get_dsp_presentation_pos(struct stream_out *out,
2744 uint64_t *frames, struct timespec *timestamp, int32_t clock_id)
2745{
2746 int ret = -EINVAL;
2747 uint64_t *val = NULL;
2748 uint64_t time = 0;
2749 struct snd_compr_metadata metadata;
2750
2751 ALOGV("%s:: Quering DSP position with clock id %d",__func__, clock_id);
2752 metadata.key = SNDRV_COMPRESS_DSP_POSITION;
2753 metadata.value[0] = clock_id;
2754 ret = compress_get_metadata(out->compr, &metadata);
2755 if (ret) {
2756 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2757 ret = -errno;
2758 goto exit;
2759 }
2760 val = (uint64_t *)&metadata.value[1];
2761 *frames = *val;
2762 time = *(val + 1);
2763 timestamp->tv_sec = time / 1000000;
2764 timestamp->tv_nsec = (time % 1000000)*1000;
2765
2766exit:
2767 return ret;
2768}
2769#else
2770int audio_extn_utils_compress_get_dsp_presentation_pos(struct stream_out *out __unused,
2771 uint64_t *frames __unused, struct timespec *timestamp __unused,
2772 int32_t clock_id __unused)
2773{
2774 ALOGD("%s:: dsp presentation position not supported", __func__);
2775 return 0;
2776
2777}
2778#endif
2779
2780#ifdef SNDRV_PCM_IOCTL_DSP_POSITION
2781int audio_extn_utils_pcm_get_dsp_presentation_pos(struct stream_out *out,
2782 uint64_t *frames, struct timespec *timestamp, int32_t clock_id)
2783{
2784 int ret = -EINVAL;
2785 uint64_t time = 0;
2786 struct snd_pcm_prsnt_position prsnt_position;
2787
2788 ALOGV("%s:: Quering DSP position with clock id %d",__func__, clock_id);
2789 prsnt_position.clock_id = clock_id;
2790 ret = pcm_ioctl(out->pcm, SNDRV_PCM_IOCTL_DSP_POSITION, &prsnt_position);
2791 if (ret) {
2792 ALOGE("%s::error %d", __func__, ret);
2793 ret = -EIO;
2794 goto exit;
2795 }
2796
2797 *frames = prsnt_position.frames;
2798 time = prsnt_position.timestamp;
2799 timestamp->tv_sec = time / 1000000;
2800 timestamp->tv_nsec = (time % 1000000)*1000;
2801
2802exit:
2803 return ret;
2804}
2805#else
2806int audio_extn_utils_pcm_get_dsp_presentation_pos(struct stream_out *out __unused,
2807 uint64_t *frames __unused, struct timespec *timestamp __unused,
2808 int32_t clock_id __unused)
2809{
2810 ALOGD("%s:: dsp presentation position not supported", __func__);
2811 return 0;
2812
2813}
2814#endif
2815
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002816#define MAX_SND_CARD 8
Jaideep Sharmadf948882020-10-22 14:13:52 +05302817#define RETRY_US 400000
2818#define RETRY_NUMBER 100
Aalique Grahame22e49102018-12-18 14:23:57 -08002819#define PLATFORM_INFO_XML_PATH "audio_platform_info.xml"
2820#define PLATFORM_INFO_XML_BASE_STRING "audio_platform_info"
2821
Aalique Grahame22e49102018-12-18 14:23:57 -08002822bool audio_extn_utils_resolve_config_file(char file_name[MIXER_PATH_MAX_LENGTH])
2823{
2824 char full_config_path[MIXER_PATH_MAX_LENGTH];
Saurav Kumarf5d2c342020-07-29 19:31:49 +05302825 char vendor_config_path[VENDOR_CONFIG_PATH_MAX_LENGTH];
2826
2827 /* Get path for audio configuration files in vendor */
2828 audio_get_vendor_config_path(vendor_config_path,
2829 sizeof(vendor_config_path));
2830 snprintf(full_config_path, sizeof(full_config_path),
2831 "%s/%s", vendor_config_path, file_name);
2832 if (F_OK == access(full_config_path, 0)) {
2833 strlcpy(file_name, full_config_path, MIXER_PATH_MAX_LENGTH);
2834 return true;
Aalique Grahame22e49102018-12-18 14:23:57 -08002835 }
2836 return false;
2837}
2838
2839/* platform_info_file should be size 'MIXER_PATH_MAX_LENGTH' */
2840int audio_extn_utils_get_platform_info(const char* snd_card_name, char* platform_info_file)
2841{
2842 if (NULL == snd_card_name) {
2843 return -1;
2844 }
2845
2846 struct snd_card_split *snd_split_handle = NULL;
2847 int ret = 0;
2848 audio_extn_set_snd_card_split(snd_card_name);
2849 snd_split_handle = audio_extn_get_snd_card_split();
2850
2851 snprintf(platform_info_file, MIXER_PATH_MAX_LENGTH, "%s_%s_%s.xml",
2852 PLATFORM_INFO_XML_BASE_STRING, snd_split_handle->snd_card,
2853 snd_split_handle->form_factor);
2854
2855 if (!audio_extn_utils_resolve_config_file(platform_info_file)) {
2856 memset(platform_info_file, 0, MIXER_PATH_MAX_LENGTH);
2857 snprintf(platform_info_file, MIXER_PATH_MAX_LENGTH, "%s_%s.xml",
2858 PLATFORM_INFO_XML_BASE_STRING, snd_split_handle->snd_card);
2859
2860 if (!audio_extn_utils_resolve_config_file(platform_info_file)) {
2861 memset(platform_info_file, 0, MIXER_PATH_MAX_LENGTH);
2862 strlcpy(platform_info_file, PLATFORM_INFO_XML_PATH, MIXER_PATH_MAX_LENGTH);
2863 ret = audio_extn_utils_resolve_config_file(platform_info_file) ? 0 : -1;
2864 }
2865 }
2866
2867 return ret;
2868}
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002869
2870int audio_extn_utils_get_snd_card_num()
2871{
Soumya Managoli9fee7c62018-04-06 16:21:50 +05302872 int snd_card_num = 0;
2873 struct mixer *mixer = NULL;
2874
2875 snd_card_num = audio_extn_utils_open_snd_mixer(&mixer);
2876 if (mixer)
2877 mixer_close(mixer);
2878 return snd_card_num;
2879}
2880
2881int audio_extn_utils_open_snd_mixer(struct mixer **mixer_handle)
2882{
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002883
2884 void *hw_info = NULL;
2885 struct mixer *mixer = NULL;
2886 int retry_num = 0;
mpangfaa7bae2019-01-15 16:38:13 +08002887 int snd_card_num = 0;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002888 char* snd_card_name = NULL;
mpangfaa7bae2019-01-15 16:38:13 +08002889 int snd_card_detection_info[MAX_SND_CARD] = {0};
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002890
Soumya Managoli9fee7c62018-04-06 16:21:50 +05302891 if (!mixer_handle) {
2892 ALOGE("invalid mixer handle");
2893 return -1;
2894 }
2895 *mixer_handle = NULL;
Ashish Jain30ae8942017-11-05 17:21:23 +05302896 /*
mpangfaa7bae2019-01-15 16:38:13 +08002897 * Try with all the sound cards ( 0 to 7 ) and if none of them were detected
Ashish Jain30ae8942017-11-05 17:21:23 +05302898 * sleep for 1 sec and try detections with sound card 0 again.
2899 * If sound card gets detected, check if it is relevant, if not check with the
2900 * other sound cards. To ensure that the irrelevant sound card is not check again,
2901 * we maintain it in min_snd_card_num.
2902 */
2903 while (retry_num < RETRY_NUMBER) {
mpangfaa7bae2019-01-15 16:38:13 +08002904 snd_card_num = 0;
2905 while (snd_card_num < MAX_SND_CARD) {
2906 if (snd_card_detection_info[snd_card_num] == 0) {
2907 mixer = mixer_open(snd_card_num);
2908 if (!mixer)
2909 snd_card_num++;
2910 else
2911 break;
2912 } else
2913 snd_card_num++;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002914 }
2915
2916 if (!mixer) {
Ashish Jain30ae8942017-11-05 17:21:23 +05302917 usleep(RETRY_US);
Ashish Jain30ae8942017-11-05 17:21:23 +05302918 retry_num++;
mpangfaa7bae2019-01-15 16:38:13 +08002919 ALOGD("%s: retry, retry_num %d", __func__, retry_num);
Ashish Jain30ae8942017-11-05 17:21:23 +05302920 continue;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002921 }
2922
2923 snd_card_name = strdup(mixer_get_name(mixer));
2924 if (!snd_card_name) {
2925 ALOGE("failed to allocate memory for snd_card_name\n");
2926 mixer_close(mixer);
2927 return -1;
2928 }
2929 ALOGD("%s: snd_card_name: %s", __func__, snd_card_name);
mpangfaa7bae2019-01-15 16:38:13 +08002930 snd_card_detection_info[snd_card_num] = 1;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002931 hw_info = hw_info_init(snd_card_name);
2932 if (hw_info) {
2933 ALOGD("%s: Opened sound card:%d", __func__, snd_card_num);
2934 break;
2935 }
mpangfaa7bae2019-01-15 16:38:13 +08002936 ALOGE("%s: Failed to init hardware info, snd_card_num:%d", __func__, snd_card_num);
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002937
Dhananjay Kumara7e27832017-07-11 15:40:39 +05302938 free(snd_card_name);
2939 snd_card_name = NULL;
2940
2941 mixer_close(mixer);
2942 mixer = NULL;
2943 }
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002944 if (snd_card_name)
2945 free(snd_card_name);
Ashish Jain30ae8942017-11-05 17:21:23 +05302946
Dhananjay Kumara7e27832017-07-11 15:40:39 +05302947 if (hw_info)
2948 hw_info_deinit(hw_info);
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002949
mpangfaa7bae2019-01-15 16:38:13 +08002950 if (retry_num >= RETRY_NUMBER) {
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002951 ALOGE("%s: Unable to find correct sound card, aborting.", __func__);
2952 return -1;
2953 }
2954
Soumya Managoli9fee7c62018-04-06 16:21:50 +05302955 if (mixer)
2956 *mixer_handle = mixer;
2957
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002958 return snd_card_num;
2959}
Naresh Tanniru6160c712017-04-17 15:43:48 +05302960
Soumya Managoli9fee7c62018-04-06 16:21:50 +05302961void audio_extn_utils_close_snd_mixer(struct mixer *mixer)
2962{
2963 if (mixer)
2964 mixer_close(mixer);
2965}
2966
Naresh Tanniru6160c712017-04-17 15:43:48 +05302967#ifdef SNDRV_COMPRESS_ENABLE_ADJUST_SESSION_CLOCK
2968int audio_extn_utils_compress_enable_drift_correction(
2969 struct stream_out *out,
2970 struct audio_out_enable_drift_correction *drift)
2971{
2972 struct snd_compr_metadata metadata;
2973 int ret = -EINVAL;
2974
2975 if(drift == NULL) {
2976 ALOGE("%s:: Invalid param", __func__);
2977 goto exit;
2978 }
2979
2980 ALOGD("%s:: drift enable %d", __func__,drift->enable);
2981
2982 if (!is_offload_usecase(out->usecase)) {
2983 ALOGE("%s:: not supported for non offload session", __func__);
2984 goto exit;
2985 }
2986
2987 if (!out->compr) {
2988 ALOGW("%s:: offload session not yet opened,"
2989 "start delay will be configure later", __func__);
2990 goto exit;
2991 }
2992
2993 metadata.key = SNDRV_COMPRESS_ENABLE_ADJUST_SESSION_CLOCK;
2994 metadata.value[0] = drift->enable;
2995 out->drift_correction_enabled = drift->enable;
2996
2997 ret = compress_set_metadata(out->compr, &metadata);
2998 if(ret) {
2999 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
3000 out->drift_correction_enabled = false;
3001 }
3002
3003exit:
3004 return ret;
3005}
3006#else
3007int audio_extn_utils_compress_enable_drift_correction(
3008 struct stream_out *out __unused,
3009 struct audio_out_enable_drift_correction *drift __unused)
3010{
3011 ALOGD("%s:: configuring drift enablement not supported", __func__);
3012 return 0;
3013}
3014#endif
3015
3016#ifdef SNDRV_COMPRESS_ADJUST_SESSION_CLOCK
3017int audio_extn_utils_compress_correct_drift(
3018 struct stream_out *out,
3019 struct audio_out_correct_drift *drift_param)
3020{
3021 struct snd_compr_metadata metadata;
3022 int ret = -EINVAL;
3023
3024 if (drift_param == NULL) {
3025 ALOGE("%s:: Invalid drift_param", __func__);
3026 goto exit;
3027 }
3028
3029 ALOGD("%s:: adjust time 0x%"PRIx64" ", __func__,
3030 drift_param->adjust_time);
3031
3032 if (!is_offload_usecase(out->usecase)) {
3033 ALOGE("%s:: not supported for non offload session", __func__);
3034 goto exit;
3035 }
3036
3037 if (!out->compr) {
3038 ALOGW("%s:: offload session not yet opened", __func__);
3039 goto exit;
3040 }
3041
3042 if (!out->drift_correction_enabled) {
3043 ALOGE("%s:: drift correction not enabled", __func__);
3044 goto exit;
3045 }
3046
3047 metadata.key = SNDRV_COMPRESS_ADJUST_SESSION_CLOCK;
3048 metadata.value[0] = 0xFFFFFFFF & drift_param->adjust_time; /* lsb */
3049 metadata.value[1] = \
3050 (0xFFFFFFFF00000000 & drift_param->adjust_time) >> 32; /* msb*/
3051
3052 ret = compress_set_metadata(out->compr, &metadata);
3053 if(ret)
3054 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
3055exit:
3056 return ret;
3057}
3058#else
3059int audio_extn_utils_compress_correct_drift(
3060 struct stream_out *out __unused,
3061 struct audio_out_correct_drift *drift_param __unused)
3062{
3063 ALOGD("%s:: setting adjust clock not supported", __func__);
3064 return 0;
3065}
3066#endif
Naresh Tanniru29bce4e2017-04-27 17:54:30 +05303067
3068int audio_extn_utils_set_channel_map(
3069 struct stream_out *out,
3070 struct audio_out_channel_map_param *channel_map_param)
3071{
3072 int ret = -EINVAL, i = 0;
3073 int channels = audio_channel_count_from_out_mask(out->channel_mask);
3074
3075 if (channel_map_param == NULL) {
3076 ALOGE("%s:: Invalid channel_map", __func__);
3077 goto exit;
3078 }
3079
3080 if (channel_map_param->channels != channels) {
3081 ALOGE("%s:: Channels(%d) does not match stream channels(%d)",
3082 __func__, channel_map_param->channels, channels);
3083 goto exit;
3084 }
3085
3086 for ( i = 0; i < channels; i++) {
3087 ALOGV("%s:: channel_map[%d]- %d", __func__, i, channel_map_param->channel_map[i]);
3088 out->channel_map_param.channel_map[i] = channel_map_param->channel_map[i];
3089 }
3090 ret = 0;
3091exit:
3092 return ret;
3093}
Varun Balaraje49253e2017-07-06 19:48:56 +05303094
3095int audio_extn_utils_set_pan_scale_params(
3096 struct stream_out *out,
3097 struct mix_matrix_params *mm_params)
3098{
3099 int ret = -EINVAL, i = 0, j = 0;
3100
Varun Balaraj003ee752017-08-07 17:54:55 +05303101 if (mm_params == NULL || out == NULL) {
3102 ALOGE("%s:: Invalid mix matrix or out param", __func__);
Varun Balaraje49253e2017-07-06 19:48:56 +05303103 goto exit;
3104 }
3105
3106 if (mm_params->num_output_channels > MAX_CHANNELS_SUPPORTED ||
3107 mm_params->num_output_channels <= 0 ||
3108 mm_params->num_input_channels > MAX_CHANNELS_SUPPORTED ||
3109 mm_params->num_input_channels <= 0)
3110 goto exit;
3111
3112 out->pan_scale_params.num_output_channels = mm_params->num_output_channels;
3113 out->pan_scale_params.num_input_channels = mm_params->num_input_channels;
3114 out->pan_scale_params.has_output_channel_map =
3115 mm_params->has_output_channel_map;
3116 for (i = 0; i < mm_params->num_output_channels; i++)
3117 out->pan_scale_params.output_channel_map[i] =
3118 mm_params->output_channel_map[i];
3119
3120 out->pan_scale_params.has_input_channel_map =
3121 mm_params->has_input_channel_map;
3122 for (i = 0; i < mm_params->num_input_channels; i++)
3123 out->pan_scale_params.input_channel_map[i] =
3124 mm_params->input_channel_map[i];
3125
3126 out->pan_scale_params.has_mixer_coeffs = mm_params->has_mixer_coeffs;
3127 for (i = 0; i < mm_params->num_output_channels; i++)
3128 for (j = 0; j < mm_params->num_input_channels; j++) {
3129 //Convert the channel coefficient gains in Q14 format
3130 out->pan_scale_params.mixer_coeffs[i][j] =
3131 mm_params->mixer_coeffs[i][j] * (2 << 13);
3132 }
3133
3134 ret = platform_set_stream_pan_scale_params(out->dev->platform,
3135 out->pcm_device_id,
3136 out->pan_scale_params);
3137
3138exit:
3139 return ret;
3140}
3141
3142int audio_extn_utils_set_downmix_params(
3143 struct stream_out *out,
3144 struct mix_matrix_params *mm_params)
3145{
3146 int ret = -EINVAL, i = 0, j = 0;
3147 struct audio_usecase *usecase = NULL;
3148
Aniket Kumar Lataf9f246e2017-09-15 15:20:16 -07003149 if (mm_params == NULL || out == NULL) {
Varun Balaraj003ee752017-08-07 17:54:55 +05303150 ALOGE("%s:: Invalid mix matrix or out param", __func__);
Varun Balaraje49253e2017-07-06 19:48:56 +05303151 goto exit;
3152 }
3153
3154 if (mm_params->num_output_channels > MAX_CHANNELS_SUPPORTED ||
3155 mm_params->num_output_channels <= 0 ||
3156 mm_params->num_input_channels > MAX_CHANNELS_SUPPORTED ||
3157 mm_params->num_input_channels <= 0)
3158 goto exit;
3159
3160 usecase = get_usecase_from_list(out->dev, out->usecase);
Varun Balaraj003ee752017-08-07 17:54:55 +05303161 if (!usecase) {
3162 ALOGE("%s: Get usecase list failed!", __func__);
Aniket Kumar Lataf9f246e2017-09-15 15:20:16 -07003163 goto exit;
3164 }
Varun Balaraje49253e2017-07-06 19:48:56 +05303165 out->downmix_params.num_output_channels = mm_params->num_output_channels;
3166 out->downmix_params.num_input_channels = mm_params->num_input_channels;
3167
3168 out->downmix_params.has_output_channel_map =
3169 mm_params->has_output_channel_map;
3170 for (i = 0; i < mm_params->num_output_channels; i++) {
3171 out->downmix_params.output_channel_map[i] =
3172 mm_params->output_channel_map[i];
3173 }
3174
3175 out->downmix_params.has_input_channel_map =
3176 mm_params->has_input_channel_map;
3177 for (i = 0; i < mm_params->num_input_channels; i++)
3178 out->downmix_params.input_channel_map[i] =
3179 mm_params->input_channel_map[i];
3180
3181 out->downmix_params.has_mixer_coeffs = mm_params->has_mixer_coeffs;
3182 for (i = 0; i < mm_params->num_output_channels; i++)
Nikhil Latukar2e6f6242017-08-15 13:37:07 +05303183 for (j = 0; j < mm_params->num_input_channels; j++) {
3184 //Convert the channel coefficient gains in Q14 format
Varun Balaraje49253e2017-07-06 19:48:56 +05303185 out->downmix_params.mixer_coeffs[i][j] =
Nikhil Latukar2e6f6242017-08-15 13:37:07 +05303186 mm_params->mixer_coeffs[i][j] * (2 << 13);
3187 }
Varun Balaraje49253e2017-07-06 19:48:56 +05303188
3189 ret = platform_set_stream_downmix_params(out->dev->platform,
3190 out->pcm_device_id,
3191 usecase->out_snd_device,
3192 out->downmix_params);
3193
3194exit:
3195 return ret;
3196}
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05303197
3198bool audio_extn_utils_is_dolby_format(audio_format_t format)
3199{
3200 if (format == AUDIO_FORMAT_AC3 ||
3201 format == AUDIO_FORMAT_E_AC3 ||
3202 format == AUDIO_FORMAT_E_AC3_JOC)
3203 return true;
3204 else
3205 return false;
3206}
3207
`Deeraj Soman676c2702017-09-18 19:25:53 +05303208int audio_extn_utils_get_bit_width_from_string(const char *id_string)
3209{
3210 int i;
3211 const mixer_config_lookup mixer_bitwidth_config[] = {{"S24_3LE", 24},
3212 {"S32_LE", 32},
3213 {"S24_LE", 24},
3214 {"S16_LE", 16}};
3215 int num_configs = sizeof(mixer_bitwidth_config) / sizeof(mixer_bitwidth_config[0]);
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05303216
`Deeraj Soman676c2702017-09-18 19:25:53 +05303217 for (i = 0; i < num_configs; i++) {
3218 if (!strcmp(id_string, mixer_bitwidth_config[i].id_string))
3219 return mixer_bitwidth_config[i].value;
3220 }
3221
3222 return -EINVAL;
3223}
3224
3225int audio_extn_utils_get_sample_rate_from_string(const char *id_string)
3226{
3227 int i;
Weiyin Jiang30f9ab52020-11-11 11:37:21 +08003228 const mixer_config_lookup mixer_samplerate_config[] = {{"KHZ_8", 8000},
3229 {"KHZ_11P025", 11025},
3230 {"KHZ_16", 16000},
3231 {"KHZ_22P05", 22050},
3232 {"KHZ_32", 32000},
`Deeraj Soman676c2702017-09-18 19:25:53 +05303233 {"KHZ_48", 48000},
3234 {"KHZ_96", 96000},
3235 {"KHZ_144", 144000},
3236 {"KHZ_192", 192000},
3237 {"KHZ_384", 384000},
3238 {"KHZ_44P1", 44100},
3239 {"KHZ_88P2", 88200},
3240 {"KHZ_176P4", 176400},
3241 {"KHZ_352P8", 352800}};
3242 int num_configs = sizeof(mixer_samplerate_config) / sizeof(mixer_samplerate_config[0]);
3243
3244 for (i = 0; i < num_configs; i++) {
3245 if (!strcmp(id_string, mixer_samplerate_config[i].id_string))
3246 return mixer_samplerate_config[i].value;
3247 }
3248
3249 return -EINVAL;
3250}
3251
3252int audio_extn_utils_get_channels_from_string(const char *id_string)
3253{
3254 int i;
3255 const mixer_config_lookup mixer_channels_config[] = {{"One", 1},
3256 {"Two", 2},
3257 {"Three",3},
3258 {"Four", 4},
3259 {"Five", 5},
3260 {"Six", 6},
3261 {"Seven", 7},
3262 {"Eight", 8}};
3263 int num_configs = sizeof(mixer_channels_config) / sizeof(mixer_channels_config[0]);
3264
3265 for (i = 0; i < num_configs; i++) {
3266 if (!strcmp(id_string, mixer_channels_config[i].id_string))
3267 return mixer_channels_config[i].value;
3268 }
3269
3270 return -EINVAL;
3271}
Surendar karka30569792018-05-08 12:02:21 +05303272
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08003273void audio_extn_utils_release_snd_device(snd_device_t snd_device)
3274{
3275 audio_extn_dev_arbi_release(snd_device);
3276 audio_extn_sound_trigger_update_device_status(snd_device,
3277 ST_EVENT_SND_DEVICE_FREE);
3278 audio_extn_listen_update_device_status(snd_device,
3279 LISTEN_EVENT_SND_DEVICE_FREE);
3280}
3281
Weiyin Jiangd5718bd2019-09-04 16:52:08 +08003282int audio_extn_utils_get_license_params(
3283 const struct audio_device *adev,
3284 struct audio_license_params *license_params)
Surendar karka30569792018-05-08 12:02:21 +05303285{
3286 if(!license_params)
3287 return -EINVAL;
Weiyin Jiangd5718bd2019-09-04 16:52:08 +08003288
3289 return platform_get_license_by_product(adev->platform,
3290 (const char*)license_params->product, &license_params->key, license_params->license);
Surendar karka30569792018-05-08 12:02:21 +05303291}
3292
Aalique Grahame22e49102018-12-18 14:23:57 -08003293int audio_extn_utils_send_app_type_gain(struct audio_device *adev,
3294 int app_type,
3295 int *gain)
3296{
3297 int gain_cfg[4];
3298 const char *mixer_ctl_name = "App Type Gain";
3299 struct mixer_ctl *ctl;
3300 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
3301 if (!ctl) {
3302 ALOGE("%s: Could not get volume ctl mixer %s", __func__,
3303 mixer_ctl_name);
3304 return -EINVAL;
3305 }
3306 gain_cfg[0] = 0;
3307 gain_cfg[1] = app_type;
3308 gain_cfg[2] = gain[0];
3309 gain_cfg[3] = gain[1];
3310 ALOGV("%s app_type %d l(%d) r(%d)", __func__, app_type, gain[0], gain[1]);
3311 return mixer_ctl_set_array(ctl, gain_cfg,
3312 sizeof(gain_cfg)/sizeof(gain_cfg[0]));
3313}
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -07003314
Weiyin Jiangd5718bd2019-09-04 16:52:08 +08003315static void vndk_fwk_init()
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -07003316{
Weiyin Jiangd5718bd2019-09-04 16:52:08 +08003317 if (mVndkFwk.lib_handle != NULL)
3318 return;
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -07003319
Weiyin Jiangd5718bd2019-09-04 16:52:08 +08003320 mVndkFwk.lib_handle = dlopen(VNDK_FWK_LIB_PATH, RTLD_NOW);
3321 if (mVndkFwk.lib_handle == NULL) {
3322 ALOGW("%s: failed to dlopen VNDK_FWK_LIB %s", __func__, strerror(errno));
3323 return;
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -07003324 }
3325
Weiyin Jiangd5718bd2019-09-04 16:52:08 +08003326 *(void **)(&mVndkFwk.isVendorEnhancedFwk) =
3327 dlsym(mVndkFwk.lib_handle, "isRunningWithVendorEnhancedFramework");
3328 if (mVndkFwk.isVendorEnhancedFwk == NULL) {
3329 ALOGW("%s: dlsym failed %s", __func__, strerror(errno));
3330 if (mVndkFwk.lib_handle) {
3331 dlclose(mVndkFwk.lib_handle);
3332 mVndkFwk.lib_handle = NULL;
3333 }
3334 return;
3335 }
3336
3337
3338 *(void **)(&mVndkFwk.getVendorEnhancedInfo) =
3339 dlsym(mVndkFwk.lib_handle, "getVendorEnhancedInfo");
3340 if (mVndkFwk.getVendorEnhancedInfo == NULL) {
3341 ALOGW("%s: dlsym failed %s", __func__, strerror(errno));
3342 if (mVndkFwk.lib_handle) {
3343 dlclose(mVndkFwk.lib_handle);
3344 mVndkFwk.lib_handle = NULL;
3345 }
3346 }
3347
3348 return;
3349}
3350
3351bool audio_extn_utils_is_vendor_enhanced_fwk()
3352{
3353 static int is_vendor_enhanced_fwk = -EINVAL;
3354 if (is_vendor_enhanced_fwk != -EINVAL)
3355 return (bool)is_vendor_enhanced_fwk;
3356
3357 vndk_fwk_init();
3358
3359 if (mVndkFwk.isVendorEnhancedFwk != NULL) {
3360 is_vendor_enhanced_fwk = mVndkFwk.isVendorEnhancedFwk();
3361 ALOGW("%s: is_vendor_enhanced_fwk %d", __func__, is_vendor_enhanced_fwk);
3362 } else {
3363 is_vendor_enhanced_fwk = 0;
3364 ALOGW("%s: default to non enhanced_fwk config", __func__);
3365 }
3366
3367 return (bool)is_vendor_enhanced_fwk;
3368}
3369
3370int audio_extn_utils_get_vendor_enhanced_info()
3371{
3372 static int vendor_enhanced_info = -EINVAL;
3373 if (vendor_enhanced_info != -EINVAL)
3374 return vendor_enhanced_info;
3375
3376 vndk_fwk_init();
3377
3378 if (mVndkFwk.getVendorEnhancedInfo != NULL) {
3379 vendor_enhanced_info = mVndkFwk.getVendorEnhancedInfo();
3380 ALOGW("%s: vendor_enhanced_info 0x%x", __func__, vendor_enhanced_info);
3381 } else {
3382 vendor_enhanced_info = 0x0;
3383 ALOGW("%s: default to vendor_enhanced_info 0x0", __func__);
3384 }
3385
3386 return vendor_enhanced_info;
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -07003387}
Deeraj Soman14230922019-01-30 16:39:30 +05303388
Deeraj Somanfa377bf2019-02-06 12:57:59 +05303389int audio_extn_utils_get_perf_mode_flag(void)
3390{
3391#ifdef COMPRESSED_PERF_MODE_FLAG
3392 return COMPRESSED_PERF_MODE_FLAG;
3393#else
3394 return 0;
3395#endif
3396}
3397
Deeraj Soman14230922019-01-30 16:39:30 +05303398size_t audio_extn_utils_get_input_buffer_size(uint32_t sample_rate,
3399 audio_format_t format,
3400 int channel_count,
3401 int64_t duration_ms,
3402 bool is_low_latency)
3403{
3404 size_t size = 0;
3405 size_t capture_duration = AUDIO_CAPTURE_PERIOD_DURATION_MSEC;
3406 uint32_t bytes_per_period_sample = 0;
3407
3408
3409 if (audio_extn_utils_check_input_parameters(sample_rate, format, channel_count) != 0)
3410 return 0;
3411
3412 if (duration_ms >= MIN_OFFLOAD_BUFFER_DURATION_MS && duration_ms <= MAX_OFFLOAD_BUFFER_DURATION_MS)
3413 capture_duration = duration_ms;
3414
3415 size = (sample_rate * capture_duration) / 1000;
3416 if (is_low_latency)
3417 size = LOW_LATENCY_CAPTURE_PERIOD_SIZE;
3418
3419
3420 bytes_per_period_sample = audio_bytes_per_sample(format) * channel_count;
3421 size *= bytes_per_period_sample;
3422
3423 /* make sure the size is multiple of 32 bytes and additionally multiple of
3424 * the frame_size (required for 24bit samples and non-power-of-2 channel counts)
3425 * At 48 kHz mono 16-bit PCM:
3426 * 5.000 ms = 240 frames = 15*16*1*2 = 480, a whole multiple of 32 (15)
3427 * 3.333 ms = 160 frames = 10*16*1*2 = 320, a whole multiple of 32 (10)
3428 *
3429 * The loop reaches result within 32 iterations, as initial size is
3430 * already a multiple of frame_size
3431 */
3432 size = audio_extn_utils_nearest_multiple(size, audio_extn_utils_lcm(32, bytes_per_period_sample));
3433
3434 return size;
3435}
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07003436
3437int audio_extn_utils_hash_fn(void *key)
3438{
3439 return (int)key;
3440}
3441
3442bool audio_extn_utils_hash_eq(void *key1, void *key2)
3443{
3444 return (key1 == key2);
3445}