blob: fa826f57f0c6423bc37d1c183985045b3c932cee [file] [log] [blame]
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001/*
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07002 * Copyright (c) 2014-2020, The Linux Foundation. All rights reserved.
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07003 * Not a Contribution.
4 *
5 * Copyright (C) 2014 The Android Open Source Project
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19
20#define LOG_TAG "audio_hw_utils"
21/* #define LOG_NDEBUG 0 */
22
Manish Dewangan27346042017-03-01 12:56:12 +053023#include <inttypes.h>
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070024#include <errno.h>
25#include <cutils/properties.h>
26#include <cutils/config_utils.h>
27#include <stdlib.h>
28#include <dlfcn.h>
29#include <cutils/str_parms.h>
Aalique Grahame22e49102018-12-18 14:23:57 -080030#include <log/log.h>
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070031#include <cutils/misc.h>
Vinay Vermaaddfa4a2018-04-29 14:03:38 +053032#include <unistd.h>
Surendar Karka287348c2019-04-10 18:31:46 +053033#include <sys/ioctl.h>
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070034
Manish Dewangan07de2142017-02-27 19:27:20 +053035
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070036#include "audio_hw.h"
37#include "platform.h"
38#include "platform_api.h"
39#include "audio_extn.h"
Deeraj Soman14230922019-01-30 16:39:30 +053040#include "voice_extn.h"
Narsinga Rao Chella212e2542014-11-17 19:57:04 -080041#include "voice.h"
Manish Dewangan07de2142017-02-27 19:27:20 +053042#include <sound/compress_params.h>
43#include <sound/compress_offload.h>
Surendar Karka287348c2019-04-10 18:31:46 +053044#include <sound/devdep_params.h>
Manish Dewangan07de2142017-02-27 19:27:20 +053045#include <tinycompress/tinycompress.h>
Revathi Uddaraju1eac8b02017-05-18 17:13:33 +053046
47#ifdef DYNAMIC_LOG_ENABLED
48#include <log_xml_parser.h>
49#define LOG_MASK HAL_MOD_FILE_UTILS
50#include <log_utils.h>
51#endif
52
Ashish Jain81eb2a82015-05-13 10:52:34 +053053#ifdef AUDIO_EXTERNAL_HDMI_ENABLED
Ashish Jain81eb2a82015-05-13 10:52:34 +053054#include "audio_parsers.h"
55#endif
Ashish Jain81eb2a82015-05-13 10:52:34 +053056
Roopesh Nataraja0f68be72020-04-28 12:32:02 -070057#define AUDIO_IO_POLICY_VENDOR_CONFIG_FILE_NAME "audio_io_policy.conf"
58#define AUDIO_OUTPUT_POLICY_VENDOR_CONFIG_FILE_NAME "audio_output_policy.conf"
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070059
60#define OUTPUTS_TAG "outputs"
Dhananjay Kumard6d32152016-10-13 16:11:03 +053061#define INPUTS_TAG "inputs"
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070062
63#define DYNAMIC_VALUE_TAG "dynamic"
64#define FLAGS_TAG "flags"
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +053065#define PROFILES_TAG "profile"
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070066#define FORMATS_TAG "formats"
67#define SAMPLING_RATES_TAG "sampling_rates"
68#define BIT_WIDTH_TAG "bit_width"
69#define APP_TYPE_TAG "app_type"
70
71#define STRING_TO_ENUM(string) { #string, string }
72#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
73
Ben Rombergera04fabc2014-11-14 12:16:03 -080074#define BASE_TABLE_SIZE 64
75#define MAX_BASEINDEX_LEN 256
76
Ben Romberger1aaaf862017-04-06 17:49:46 -070077#ifndef SND_AUDIOCODEC_TRUEHD
78#define SND_AUDIOCODEC_TRUEHD 0x00000023
79#endif
80
Vikram Panduranga93f080e2017-06-07 18:16:14 -070081#define APP_TYPE_VOIP_AUDIO 0x1113A
82
Ashish Jain81eb2a82015-05-13 10:52:34 +053083#ifdef AUDIO_EXTERNAL_HDMI_ENABLED
84#define PROFESSIONAL (1<<0) /* 0 = consumer, 1 = professional */
85#define NON_LPCM (1<<1) /* 0 = audio, 1 = non-audio */
86#define SR_44100 (0<<0) /* 44.1kHz */
87#define SR_NOTID (1<<0) /* non indicated */
88#define SR_48000 (2<<0) /* 48kHz */
89#define SR_32000 (3<<0) /* 32kHz */
90#define SR_22050 (4<<0) /* 22.05kHz */
91#define SR_24000 (6<<0) /* 24kHz */
92#define SR_88200 (8<<0) /* 88.2kHz */
93#define SR_96000 (10<<0) /* 96kHz */
94#define SR_176400 (12<<0) /* 176.4kHz */
95#define SR_192000 (14<<0) /* 192kHz */
96
97#endif
Manish Dewangan07de2142017-02-27 19:27:20 +053098
99/* ToDo: Check and update a proper value in msec */
Weiyin Jiangc49a3b52018-08-17 16:16:08 +0800100#define COMPRESS_OFFLOAD_PLAYBACK_LATENCY 50
101#define PCM_OFFLOAD_PLAYBACK_DSP_PATHDELAY 62
Samyak Jain2546f8a2019-08-29 19:24:21 +0530102#define PCM_OFFLOAD_PLAYBACK_LATENCY PCM_OFFLOAD_PLAYBACK_DSP_PATHDELAY
Manish Dewangan07de2142017-02-27 19:27:20 +0530103
Varun Balaraje49253e2017-07-06 19:48:56 +0530104#ifndef MAX_CHANNELS_SUPPORTED
105#define MAX_CHANNELS_SUPPORTED 8
106#endif
107
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -0700108#ifdef __LP64__
109#define VNDK_FWK_LIB_PATH "/vendor/lib64/libqti_vndfwk_detect.so"
110#else
111#define VNDK_FWK_LIB_PATH "/vendor/lib/libqti_vndfwk_detect.so"
112#endif
113
Weiyin Jiangd5718bd2019-09-04 16:52:08 +0800114typedef struct vndkfwk_s {
115 void *lib_handle;
116 int (*isVendorEnhancedFwk)(void);
117 int (*getVendorEnhancedInfo)(void);
118 const char *lib_name;
119} vndkfwk_t;
120
121static vndkfwk_t mVndkFwk = {
122 NULL, NULL, NULL, VNDK_FWK_LIB_PATH};
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -0700123
`Deeraj Soman676c2702017-09-18 19:25:53 +0530124typedef struct {
125 const char *id_string;
126 const int value;
127} mixer_config_lookup;
128
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700129struct string_to_enum {
130 const char *name;
131 uint32_t value;
132};
133
134const struct string_to_enum s_flag_name_to_enum_table[] = {
135 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_DIRECT),
136 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_PRIMARY),
137 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_FAST),
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800138 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_RAW),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700139 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_DEEP_BUFFER),
140 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD),
141 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_NON_BLOCKING),
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700142 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_HW_AV_SYNC),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700143 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_INCALL_MUSIC),
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700144 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH),
Naresh Tanniruee3499a2017-01-05 14:05:35 +0530145 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_TIMESTAMP),
Vikram Panduranga93f080e2017-06-07 18:16:14 -0700146 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_VOIP_RX),
Ben Romberger6c4d3812017-06-13 17:46:45 -0700147 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_BD),
Varun Balaraje49253e2017-07-06 19:48:56 +0530148 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_INTERACTIVE),
Derek Chen090dfc52018-03-22 22:15:29 -0400149 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_MEDIA),
150 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_SYS_NOTIFICATION),
151 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_NAV_GUIDANCE),
152 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_PHONE),
Derek Chen1bcdc6b2020-02-06 22:44:53 -0800153 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_FRONT_PASSENGER),
Derek Chendf05eea2019-08-01 13:57:49 -0700154 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_REAR_SEAT),
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530155 STRING_TO_ENUM(AUDIO_INPUT_FLAG_NONE),
156 STRING_TO_ENUM(AUDIO_INPUT_FLAG_FAST),
157 STRING_TO_ENUM(AUDIO_INPUT_FLAG_HW_HOTWORD),
158 STRING_TO_ENUM(AUDIO_INPUT_FLAG_RAW),
159 STRING_TO_ENUM(AUDIO_INPUT_FLAG_SYNC),
Dhananjay Kumaree4d2002016-10-25 18:02:58 +0530160 STRING_TO_ENUM(AUDIO_INPUT_FLAG_TIMESTAMP),
Dhananjay Kumar704ce6f2017-09-28 22:08:00 +0530161 STRING_TO_ENUM(AUDIO_INPUT_FLAG_COMPRESS),
Ralf Herzaec80262018-07-03 07:08:49 +0200162 STRING_TO_ENUM(AUDIO_INPUT_FLAG_PASSTHROUGH),
George Gao4759d792020-09-17 22:29:07 -0700163 STRING_TO_ENUM(AUDIO_INPUT_FLAG_MMAP_NOIRQ),
164 STRING_TO_ENUM(AUDIO_INPUT_FLAG_VOIP_TX),
165 STRING_TO_ENUM(AUDIO_INPUT_FLAG_HW_AV_SYNC),
166 STRING_TO_ENUM(AUDIO_INPUT_FLAG_DIRECT),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700167};
168
169const struct string_to_enum s_format_name_to_enum_table[] = {
Ashish Jain83a6cc22016-06-28 14:34:17 +0530170 STRING_TO_ENUM(AUDIO_FORMAT_PCM_8_BIT),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700171 STRING_TO_ENUM(AUDIO_FORMAT_PCM_16_BIT),
Ashish Jain5106d362016-05-11 19:23:33 +0530172 STRING_TO_ENUM(AUDIO_FORMAT_PCM_24_BIT_PACKED),
173 STRING_TO_ENUM(AUDIO_FORMAT_PCM_8_24_BIT),
Ashish Jainf1eaa582016-05-23 20:54:24 +0530174 STRING_TO_ENUM(AUDIO_FORMAT_PCM_32_BIT),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700175 STRING_TO_ENUM(AUDIO_FORMAT_MP3),
176 STRING_TO_ENUM(AUDIO_FORMAT_AAC),
177 STRING_TO_ENUM(AUDIO_FORMAT_VORBIS),
Mingming Yinae3530f2014-07-03 16:50:18 -0700178 STRING_TO_ENUM(AUDIO_FORMAT_AMR_NB),
179 STRING_TO_ENUM(AUDIO_FORMAT_AMR_WB),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700180 STRING_TO_ENUM(AUDIO_FORMAT_AC3),
Mingming Yinae3530f2014-07-03 16:50:18 -0700181 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700182 STRING_TO_ENUM(AUDIO_FORMAT_DTS),
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530183 STRING_TO_ENUM(AUDIO_FORMAT_DTS_HD),
Ben Romberger1aaaf862017-04-06 17:49:46 -0700184 STRING_TO_ENUM(AUDIO_FORMAT_DOLBY_TRUEHD),
Naresh Tanniru928f0862017-04-07 16:44:23 -0700185 STRING_TO_ENUM(AUDIO_FORMAT_IEC61937),
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530186 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3_JOC),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700187 STRING_TO_ENUM(AUDIO_FORMAT_WMA),
188 STRING_TO_ENUM(AUDIO_FORMAT_WMA_PRO),
189 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADIF),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700190 STRING_TO_ENUM(AUDIO_FORMAT_AMR_WB_PLUS),
191 STRING_TO_ENUM(AUDIO_FORMAT_EVRC),
192 STRING_TO_ENUM(AUDIO_FORMAT_EVRCB),
193 STRING_TO_ENUM(AUDIO_FORMAT_EVRCWB),
194 STRING_TO_ENUM(AUDIO_FORMAT_QCELP),
195 STRING_TO_ENUM(AUDIO_FORMAT_MP2),
196 STRING_TO_ENUM(AUDIO_FORMAT_EVRCNW),
Amit Shekhar6f461b12014-08-01 14:52:58 -0700197 STRING_TO_ENUM(AUDIO_FORMAT_FLAC),
Satya Krishna Pindiproli70471602015-04-24 19:12:43 +0530198 STRING_TO_ENUM(AUDIO_FORMAT_ALAC),
199 STRING_TO_ENUM(AUDIO_FORMAT_APE),
Alexy Josephcd8eaed2014-12-11 12:46:53 -0800200 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LC),
201 STRING_TO_ENUM(AUDIO_FORMAT_AAC_HE_V1),
202 STRING_TO_ENUM(AUDIO_FORMAT_AAC_HE_V2),
Manish Dewangana6fc5442015-08-24 20:30:31 +0530203 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS),
Ashish Jaine513a872015-11-19 17:00:56 +0530204 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_LC),
205 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_HE_V1),
206 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_HE_V2),
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +0530207 STRING_TO_ENUM(AUDIO_FORMAT_DSD),
Arun Kumar Dasari3b174182016-12-27 13:01:14 +0530208 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM),
209 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM_LC),
210 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM_HE_V1),
211 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM_HE_V2),
Dhanalakshmi Siddani18737932016-11-29 17:33:17 +0530212 STRING_TO_ENUM(AUDIO_FORMAT_APTX),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700213};
214
Manish Dewangan3ccdea52017-02-13 19:31:54 +0530215/* payload structure avt_device drift query */
216struct audio_avt_device_drift_stats {
217 uint32_t minor_version;
Manish Dewangan338c50a2017-09-12 15:22:03 +0530218
Manish Dewangan3ccdea52017-02-13 19:31:54 +0530219 /* Indicates the device interface direction as either
220 * source (Tx) or sink (Rx).
221 */
222 uint16_t device_direction;
Manish Dewangan338c50a2017-09-12 15:22:03 +0530223
224 /* Reference timer for drift accumulation and time stamp information.
225 * currently it only support AFE_REF_TIMER_TYPE_AVTIMER
226 */
227 uint16_t reference_timer;
Manish Dewangan3ccdea52017-02-13 19:31:54 +0530228 struct audio_avt_device_drift_param drift_param;
Manish Dewangan338c50a2017-09-12 15:22:03 +0530229} __attribute__((packed));
Manish Dewangan3ccdea52017-02-13 19:31:54 +0530230
Ben Rombergera04fabc2014-11-14 12:16:03 -0800231static char bTable[BASE_TABLE_SIZE] = {
232 'A','B','C','D','E','F','G','H','I','J','K','L',
233 'M','N','O','P','Q','R','S','T','U','V','W','X',
234 'Y','Z','a','b','c','d','e','f','g','h','i','j',
235 'k','l','m','n','o','p','q','r','s','t','u','v',
236 'w','x','y','z','0','1','2','3','4','5','6','7',
237 '8','9','+','/'
238};
239
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700240static uint32_t string_to_enum(const struct string_to_enum *table, size_t size,
241 const char *name)
242{
243 size_t i;
244 for (i = 0; i < size; i++) {
245 if (strcmp(table[i].name, name) == 0) {
246 ALOGV("%s found %s", __func__, table[i].name);
247 return table[i].value;
248 }
249 }
George Gao4759d792020-09-17 22:29:07 -0700250 ALOGE("%s cound not find %s", __func__, name);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700251 return 0;
252}
253
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530254static audio_io_flags_t parse_flag_names(char *name)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700255{
256 uint32_t flag = 0;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530257 audio_io_flags_t io_flags;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800258 char *last_r;
259 char *flag_name = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700260 while (flag_name != NULL) {
261 if (strlen(flag_name) != 0) {
262 flag |= string_to_enum(s_flag_name_to_enum_table,
263 ARRAY_SIZE(s_flag_name_to_enum_table),
264 flag_name);
265 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800266 flag_name = strtok_r(NULL, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700267 }
268
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800269 ALOGV("parse_flag_names: flag - %x", flag);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530270 io_flags.in_flags = (audio_input_flags_t)flag;
271 io_flags.out_flags = (audio_output_flags_t)flag;
272 return io_flags;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700273}
274
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530275static void parse_format_names(char *name, struct streams_io_cfg *s_info)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700276{
277 struct stream_format *sf_info = NULL;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800278 char *last_r;
279 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700280
281 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG) == 0)
282 return;
283
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530284 list_init(&s_info->format_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700285 while (str != NULL) {
286 audio_format_t format = (audio_format_t)string_to_enum(s_format_name_to_enum_table,
287 ARRAY_SIZE(s_format_name_to_enum_table), str);
288 ALOGV("%s: format - %d", __func__, format);
289 if (format != 0) {
290 sf_info = (struct stream_format *)calloc(1, sizeof(struct stream_format));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700291 if (sf_info == NULL)
292 break; /* return whatever was parsed */
293
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700294 sf_info->format = format;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530295 list_add_tail(&s_info->format_list, &sf_info->list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700296 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800297 str = strtok_r(NULL, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700298 }
299}
300
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530301static void parse_sample_rate_names(char *name, struct streams_io_cfg *s_info)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700302{
Amit Shekhar6f461b12014-08-01 14:52:58 -0700303 struct stream_sample_rate *ss_info = NULL;
304 uint32_t sample_rate = 48000;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800305 char *last_r;
306 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700307
Amit Shekhar6f461b12014-08-01 14:52:58 -0700308 if (str != NULL && 0 == strcmp(str, DYNAMIC_VALUE_TAG))
309 return;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700310
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530311 list_init(&s_info->sample_rate_list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700312 while (str != NULL) {
313 sample_rate = (uint32_t)strtol(str, (char **)NULL, 10);
314 ALOGV("%s: sample_rate - %d", __func__, sample_rate);
315 if (0 != sample_rate) {
316 ss_info = (struct stream_sample_rate *)calloc(1, sizeof(struct stream_sample_rate));
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800317 if (!ss_info) {
318 ALOGE("%s: memory allocation failure", __func__);
319 return;
320 }
Amit Shekhar6f461b12014-08-01 14:52:58 -0700321 ss_info->sample_rate = sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530322 list_add_tail(&s_info->sample_rate_list, &ss_info->list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700323 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800324 str = strtok_r(NULL, "|", &last_r);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700325 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700326}
327
328static int parse_bit_width_names(char *name)
329{
330 int bit_width = 16;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800331 char *last_r;
332 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700333
334 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG))
335 bit_width = (int)strtol(str, (char **)NULL, 10);
336
337 ALOGV("%s: bit_width - %d", __func__, bit_width);
338 return bit_width;
339}
340
341static int parse_app_type_names(void *platform, char *name)
342{
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700343 int app_type = platform_get_default_app_type(platform);
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800344 char *last_r;
345 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700346
347 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG))
348 app_type = (int)strtol(str, (char **)NULL, 10);
349
350 ALOGV("%s: app_type - %d", __func__, app_type);
351 return app_type;
352}
353
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530354static void update_streams_cfg_list(cnode *root, void *platform,
355 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700356{
357 cnode *node = root->first_child;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530358 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700359
360 ALOGV("%s", __func__);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530361 s_info = (struct streams_io_cfg *)calloc(1, sizeof(struct streams_io_cfg));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700362
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530363 if (!s_info) {
364 ALOGE("failed to allocate mem for s_info list element");
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700365 return;
366 }
367
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700368 while (node) {
369 if (strcmp(node->name, FLAGS_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530370 s_info->flags = parse_flag_names((char *)node->value);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530371 } else if (strcmp(node->name, PROFILES_TAG) == 0) {
372 strlcpy(s_info->profile, (char *)node->value, sizeof(s_info->profile));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700373 } else if (strcmp(node->name, FORMATS_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530374 parse_format_names((char *)node->value, s_info);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700375 } else if (strcmp(node->name, SAMPLING_RATES_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530376 s_info->app_type_cfg.sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
377 parse_sample_rate_names((char *)node->value, s_info);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700378 } else if (strcmp(node->name, BIT_WIDTH_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530379 s_info->app_type_cfg.bit_width = parse_bit_width_names((char *)node->value);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700380 } else if (strcmp(node->name, APP_TYPE_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530381 s_info->app_type_cfg.app_type = parse_app_type_names(platform, (char *)node->value);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700382 }
383 node = node->next;
384 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530385 list_add_tail(streams_cfg_list, &s_info->list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700386}
387
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530388static void load_cfg_list(cnode *root, void *platform,
389 struct listnode *streams_output_cfg_list,
390 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700391{
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530392 cnode *node = NULL;
393
394 node = config_find(root, OUTPUTS_TAG);
395 if (node != NULL) {
396 node = node->first_child;
397 while (node) {
398 ALOGV("%s: loading output %s", __func__, node->name);
399 update_streams_cfg_list(node, platform, streams_output_cfg_list);
400 node = node->next;
401 }
402 } else {
403 ALOGI("%s: could not load output, node is NULL", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700404 }
405
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530406 node = config_find(root, INPUTS_TAG);
407 if (node != NULL) {
408 node = node->first_child;
409 while (node) {
410 ALOGV("%s: loading input %s", __func__, node->name);
411 update_streams_cfg_list(node, platform, streams_input_cfg_list);
412 node = node->next;
413 }
414 } else {
415 ALOGI("%s: could not load input, node is NULL", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700416 }
417}
418
419static void send_app_type_cfg(void *platform, struct mixer *mixer,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530420 struct listnode *streams_output_cfg_list,
421 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700422{
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530423 size_t app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT] = {0};
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700424 int length = 0, i, num_app_types = 0;
425 struct listnode *node;
426 bool update;
427 struct mixer_ctl *ctl = NULL;
428 const char *mixer_ctl_name = "App Type Config";
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530429 struct streams_io_cfg *s_info = NULL;
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800430 uint32_t target_bit_width = 0;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700431
432 if (!mixer) {
433 ALOGE("%s: mixer is null",__func__);
434 return;
435 }
436 ctl = mixer_get_ctl_by_name(mixer, mixer_ctl_name);
437 if (!ctl) {
438 ALOGE("%s: Could not get ctl for mixer cmd - %s",__func__, mixer_ctl_name);
439 return;
440 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530441 app_type_cfg[length++] = num_app_types;
442
443 if (list_empty(streams_output_cfg_list)) {
444 app_type_cfg[length++] = platform_get_default_app_type_v2(platform, PCM_PLAYBACK);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700445 app_type_cfg[length++] = 48000;
446 app_type_cfg[length++] = 16;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530447 num_app_types += 1;
448 }
449 if (list_empty(streams_input_cfg_list)) {
450 app_type_cfg[length++] = platform_get_default_app_type_v2(platform, PCM_CAPTURE);
451 app_type_cfg[length++] = 48000;
452 app_type_cfg[length++] = 16;
453 num_app_types += 1;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700454 }
455
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800456 /* get target bit width for ADM enforce mode */
457 target_bit_width = adev_get_dsp_bit_width_enforce_mode();
458
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700459 list_for_each(node, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530460 s_info = node_to_item(node, struct streams_io_cfg, list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700461 update = true;
462 for (i=0; i<length; i=i+3) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530463 if (app_type_cfg[i+1] == 0)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700464 break;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530465 else if (app_type_cfg[i+1] == (size_t)s_info->app_type_cfg.app_type) {
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530466 if (app_type_cfg[i+2] < (size_t)s_info->app_type_cfg.sample_rate)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530467 app_type_cfg[i+2] = s_info->app_type_cfg.sample_rate;
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530468 if (app_type_cfg[i+3] < (size_t)s_info->app_type_cfg.bit_width)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530469 app_type_cfg[i+3] = s_info->app_type_cfg.bit_width;
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800470 /* ADM bit width = max(enforce_bit_width, bit_width from s_info */
471 if (audio_extn_is_dsp_bit_width_enforce_mode_supported(s_info->flags.out_flags) &&
472 (target_bit_width > app_type_cfg[i+3]))
473 app_type_cfg[i+3] = target_bit_width;
474
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700475 update = false;
476 break;
477 }
478 }
479 if (update && ((length + 3) <= MAX_LENGTH_MIXER_CONTROL_IN_INT)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530480 num_app_types += 1;
481 app_type_cfg[length++] = s_info->app_type_cfg.app_type;
482 app_type_cfg[length++] = s_info->app_type_cfg.sample_rate;
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800483 app_type_cfg[length] = s_info->app_type_cfg.bit_width;
484 if (audio_extn_is_dsp_bit_width_enforce_mode_supported(s_info->flags.out_flags) &&
485 (target_bit_width > app_type_cfg[length]))
486 app_type_cfg[length] = target_bit_width;
487
488 length++;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530489 }
490 }
491 list_for_each(node, streams_input_cfg_list) {
492 s_info = node_to_item(node, struct streams_io_cfg, list);
493 update = true;
494 for (i=0; i<length; i=i+3) {
495 if (app_type_cfg[i+1] == 0)
496 break;
497 else if (app_type_cfg[i+1] == (size_t)s_info->app_type_cfg.app_type) {
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530498 if (app_type_cfg[i+2] < (size_t)s_info->app_type_cfg.sample_rate)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530499 app_type_cfg[i+2] = s_info->app_type_cfg.sample_rate;
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530500 if (app_type_cfg[i+3] < (size_t)s_info->app_type_cfg.bit_width)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530501 app_type_cfg[i+3] = s_info->app_type_cfg.bit_width;
502 update = false;
503 break;
504 }
505 }
506 if (update && ((length + 3) <= MAX_LENGTH_MIXER_CONTROL_IN_INT)) {
507 num_app_types += 1;
508 app_type_cfg[length++] = s_info->app_type_cfg.app_type;
509 app_type_cfg[length++] = s_info->app_type_cfg.sample_rate;
510 app_type_cfg[length++] = s_info->app_type_cfg.bit_width;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700511 }
512 }
513 ALOGV("%s: num_app_types: %d", __func__, num_app_types);
514 if (num_app_types) {
515 app_type_cfg[0] = num_app_types;
516 mixer_ctl_set_array(ctl, app_type_cfg, length);
517 }
518}
519
Roopesh Nataraja0f68be72020-04-28 12:32:02 -0700520/* Function to retrieve audio vendor configs path */
521void audio_get_vendor_config_path (char* config_file_path, int path_size)
522{
523 char vendor_sku[PROPERTY_VALUE_MAX] = {'\0'};
524 if (property_get("ro.boot.product.vendor.sku", vendor_sku, "") <= 0) {
525#ifdef LINUX_ENABLED
526 /* Audio configs are stored in /etc */
527 snprintf(config_file_path, path_size, "%s", "/etc");
528#else
529 /* Audio configs are stored in /vendor/etc */
530 snprintf(config_file_path, path_size, "%s", "/vendor/etc");
531#endif
532 } else {
533 /* Audio configs are stored in /vendor/etc/audio/sku_${vendor_sku} */
534 snprintf(config_file_path, path_size,
535 "%s%s", "/vendor/etc/audio/sku_", vendor_sku);
536 }
537}
538
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530539void audio_extn_utils_update_streams_cfg_lists(void *platform,
540 struct mixer *mixer,
541 struct listnode *streams_output_cfg_list,
542 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700543{
544 cnode *root;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530545 char *data = NULL;
Roopesh Nataraja0f68be72020-04-28 12:32:02 -0700546 char vendor_config_path[VENDOR_CONFIG_PATH_MAX_LENGTH];
547 char audio_io_policy_file[VENDOR_CONFIG_FILE_MAX_LENGTH];
548 char audio_output_policy_file[VENDOR_CONFIG_FILE_MAX_LENGTH];
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700549
550 ALOGV("%s", __func__);
551 list_init(streams_output_cfg_list);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530552 list_init(streams_input_cfg_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700553
554 root = config_node("", "");
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700555 if (root == NULL) {
556 ALOGE("cfg_list, NULL config root");
557 return;
558 }
559
Roopesh Nataraja0f68be72020-04-28 12:32:02 -0700560 /* Get path for audio configuration files in vendor */
561 audio_get_vendor_config_path(vendor_config_path,
562 sizeof(vendor_config_path));
563
564 /* Get path for audio_io_policy_file in vendor */
565 snprintf(audio_io_policy_file, sizeof(audio_io_policy_file),
566 "%s/%s", vendor_config_path, AUDIO_IO_POLICY_VENDOR_CONFIG_FILE_NAME);
567
568 /* Load audio_io_policy_file from vendor */
569 data = (char *)load_file(audio_io_policy_file, NULL);
570
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530571 if (data == NULL) {
572 ALOGD("%s: failed to open io config file(%s), trying older config file",
Roopesh Nataraja0f68be72020-04-28 12:32:02 -0700573 __func__, audio_io_policy_file);
574
575 /* Get path for audio_output_policy_file in vendor */
576 snprintf(audio_output_policy_file, sizeof(audio_output_policy_file),
577 "%s/%s", vendor_config_path,
578 AUDIO_OUTPUT_POLICY_VENDOR_CONFIG_FILE_NAME);
579
580 /* Load audio_output_policy_file from vendor */
581 data = (char *)load_file(audio_output_policy_file, NULL);
582
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530583 if (data == NULL) {
584 send_app_type_cfg(platform, mixer,
585 streams_output_cfg_list,
586 streams_input_cfg_list);
587 ALOGE("%s: could not load io policy config!", __func__);
yidongh6eb4f752017-04-19 18:20:57 +0800588 free(root);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530589 return;
590 }
591 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700592
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530593 config_load(root, data);
594 load_cfg_list(root, platform, streams_output_cfg_list,
595 streams_input_cfg_list);
596
597 send_app_type_cfg(platform, mixer, streams_output_cfg_list,
598 streams_input_cfg_list);
Alexy Josephaee4fdd2016-01-29 13:02:07 -0800599
600 config_free(root);
yidongh6eb4f752017-04-19 18:20:57 +0800601 free(root);
Alexy Josephaee4fdd2016-01-29 13:02:07 -0800602 free(data);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700603}
604
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530605static void audio_extn_utils_dump_streams_cfg_list(
606 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700607{
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700608 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530609 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700610 struct stream_format *sf_info;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700611 struct stream_sample_rate *ss_info;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530612
613 list_for_each(node_i, streams_cfg_list) {
614 s_info = node_to_item(node_i, struct streams_io_cfg, list);
615 ALOGV("%s: flags-%d, sample_rate-%d, bit_width-%d, app_type-%d",
616 __func__, s_info->flags.out_flags, s_info->app_type_cfg.sample_rate,
617 s_info->app_type_cfg.bit_width, s_info->app_type_cfg.app_type);
618 list_for_each(node_j, &s_info->format_list) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700619 sf_info = node_to_item(node_j, struct stream_format, list);
620 ALOGV("format-%x", sf_info->format);
621 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530622 list_for_each(node_j, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700623 ss_info = node_to_item(node_j, struct stream_sample_rate, list);
624 ALOGV("sample rate-%d", ss_info->sample_rate);
625 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700626 }
627}
628
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530629void audio_extn_utils_dump_streams_cfg_lists(
630 struct listnode *streams_output_cfg_list,
631 struct listnode *streams_input_cfg_list)
632{
633 ALOGV("%s", __func__);
634 audio_extn_utils_dump_streams_cfg_list(streams_output_cfg_list);
635 audio_extn_utils_dump_streams_cfg_list(streams_input_cfg_list);
636}
637
638static void audio_extn_utils_release_streams_cfg_list(
639 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700640{
641 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530642 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700643
644 ALOGV("%s", __func__);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530645
646 while (!list_empty(streams_cfg_list)) {
647 node_i = list_head(streams_cfg_list);
648 s_info = node_to_item(node_i, struct streams_io_cfg, list);
649 while (!list_empty(&s_info->format_list)) {
650 node_j = list_head(&s_info->format_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700651 list_remove(node_j);
652 free(node_to_item(node_j, struct stream_format, list));
653 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530654 while (!list_empty(&s_info->sample_rate_list)) {
655 node_j = list_head(&s_info->sample_rate_list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700656 list_remove(node_j);
657 free(node_to_item(node_j, struct stream_sample_rate, list));
658 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700659 list_remove(node_i);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530660 free(node_to_item(node_i, struct streams_io_cfg, list));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700661 }
662}
663
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530664void audio_extn_utils_release_streams_cfg_lists(
665 struct listnode *streams_output_cfg_list,
666 struct listnode *streams_input_cfg_list)
667{
668 ALOGV("%s", __func__);
669 audio_extn_utils_release_streams_cfg_list(streams_output_cfg_list);
670 audio_extn_utils_release_streams_cfg_list(streams_input_cfg_list);
671}
672
673static bool set_app_type_cfg(struct streams_io_cfg *s_info,
674 struct stream_app_type_cfg *app_type_cfg,
675 uint32_t sample_rate, uint32_t bit_width)
Amit Shekhar6f461b12014-08-01 14:52:58 -0700676 {
677 struct listnode *node_i;
678 struct stream_sample_rate *ss_info;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530679 list_for_each(node_i, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700680 ss_info = node_to_item(node_i, struct stream_sample_rate, list);
681 if ((sample_rate <= ss_info->sample_rate) &&
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530682 (bit_width == s_info->app_type_cfg.bit_width)) {
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700683
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530684 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700685 app_type_cfg->sample_rate = ss_info->sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530686 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700687 ALOGV("%s app_type_cfg->app_type %d, app_type_cfg->sample_rate %d, app_type_cfg->bit_width %d",
688 __func__, app_type_cfg->app_type, app_type_cfg->sample_rate, app_type_cfg->bit_width);
689 return true;
690 }
691 }
692 /*
693 * Reiterate through the list assuming dafault sample rate.
694 * Handles scenario where input sample rate is higher
695 * than all sample rates in list for the input bit width.
696 */
697 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
Apoorv Raghuvanshif59bb222015-02-18 12:23:23 -0800698
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530699 list_for_each(node_i, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700700 ss_info = node_to_item(node_i, struct stream_sample_rate, list);
701 if ((sample_rate <= ss_info->sample_rate) &&
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530702 (bit_width == s_info->app_type_cfg.bit_width)) {
703 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700704 app_type_cfg->sample_rate = sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530705 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Apoorv Raghuvanshif59bb222015-02-18 12:23:23 -0800706 ALOGV("%s Assuming sample rate. app_type_cfg->app_type %d, app_type_cfg->sample_rate %d, app_type_cfg->bit_width %d",
Amit Shekhar6f461b12014-08-01 14:52:58 -0700707 __func__, app_type_cfg->app_type, app_type_cfg->sample_rate, app_type_cfg->bit_width);
708 return true;
709 }
710 }
711 return false;
712}
713
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530714void audio_extn_utils_update_stream_input_app_type_cfg(void *platform,
715 struct listnode *streams_input_cfg_list,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -0800716 struct listnode *devices __unused,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530717 audio_input_flags_t flags,
718 audio_format_t format,
719 uint32_t sample_rate,
720 uint32_t bit_width,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530721 char* profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530722 struct stream_app_type_cfg *app_type_cfg)
723{
724 struct listnode *node_i, *node_j;
725 struct streams_io_cfg *s_info;
726 struct stream_format *sf_info;
727
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530728 ALOGV("%s: flags: 0x%x, format: 0x%x sample_rate %d, profile %s",
729 __func__, flags, format, sample_rate, profile);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530730
731 list_for_each(node_i, streams_input_cfg_list) {
732 s_info = node_to_item(node_i, struct streams_io_cfg, list);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530733 /* Along with flags do profile matching if set at either end.*/
734 if (s_info->flags.in_flags == flags &&
735 ((profile[0] == '\0' && s_info->profile[0] == '\0') ||
736 strncmp(s_info->profile, profile, sizeof(s_info->profile)) == 0)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530737 list_for_each(node_j, &s_info->format_list) {
738 sf_info = node_to_item(node_j, struct stream_format, list);
739 if (sf_info->format == format) {
740 if (set_app_type_cfg(s_info, app_type_cfg, sample_rate, bit_width))
741 return;
742 }
743 }
744 }
745 }
746 ALOGW("%s: App type could not be selected. Falling back to default", __func__);
747 app_type_cfg->app_type = platform_get_default_app_type_v2(platform, PCM_CAPTURE);
748 app_type_cfg->sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
749 app_type_cfg->bit_width = 16;
750}
751
752void audio_extn_utils_update_stream_output_app_type_cfg(void *platform,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700753 struct listnode *streams_output_cfg_list,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -0800754 struct listnode *devices,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700755 audio_output_flags_t flags,
756 audio_format_t format,
Amit Shekhar6f461b12014-08-01 14:52:58 -0700757 uint32_t sample_rate,
758 uint32_t bit_width,
Manish Dewangan837dc462015-05-27 10:17:41 +0530759 audio_channel_mask_t channel_mask,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530760 char *profile,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700761 struct stream_app_type_cfg *app_type_cfg)
762{
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +0530763 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530764 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700765 struct stream_format *sf_info;
Manish Dewangan837dc462015-05-27 10:17:41 +0530766 char value[PROPERTY_VALUE_MAX] = {0};
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700767
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -0800768 if (compare_device_type(devices, AUDIO_DEVICE_OUT_SPEAKER)) {
Ramu Gottipati074fa4d2018-09-11 20:05:51 +0530769 int bw = platform_get_snd_device_bit_width(SND_DEVICE_OUT_SPEAKER);
770 if ((-ENOSYS != bw) && (bit_width > (uint32_t)bw))
Amit Shekhar5a39c912014-10-14 15:39:30 -0700771 bit_width = (uint32_t)bw;
Ramu Gottipati074fa4d2018-09-11 20:05:51 +0530772 else if (-ENOSYS == bw)
773 bit_width = CODEC_BACKEND_DEFAULT_BIT_WIDTH;
Amit Shekhar1d896042014-10-03 13:16:09 -0700774 sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Ramu Gottipati074fa4d2018-09-11 20:05:51 +0530775 ALOGI("%s Allowing 24 and above bits playback on speaker ONLY at default sampling rate", __func__);
Amit Shekhar1d896042014-10-03 13:16:09 -0700776 }
777
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -0700778 property_get("vendor.audio.playback.mch.downsample",value,"");
Manish Dewangan837dc462015-05-27 10:17:41 +0530779 if (!strncmp("true", value, sizeof("true"))) {
780 if ((popcount(channel_mask) > 2) &&
781 (sample_rate > CODEC_BACKEND_DEFAULT_SAMPLE_RATE) &&
Aalique Grahame5cd12ff2017-10-19 10:57:00 -0700782 !(flags & (audio_output_flags_t)AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH)) {
Manish Dewangan837dc462015-05-27 10:17:41 +0530783 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
784 ALOGD("%s: MCH session defaulting sample rate to %d",
785 __func__, sample_rate);
786 }
787 }
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +0530788
789 /* Set sampling rate to 176.4 for DSD64
790 * and 352.8Khz for DSD128.
791 * Set Bit Width to 16. output will be 16 bit
792 * post DoP in ASM.
793 */
Aalique Grahame5cd12ff2017-10-19 10:57:00 -0700794 if ((flags & (audio_output_flags_t)AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH) &&
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +0530795 (format == AUDIO_FORMAT_DSD)) {
796 bit_width = 16;
797 if (sample_rate == INPUT_SAMPLING_RATE_DSD64)
798 sample_rate = OUTPUT_SAMPLING_RATE_DSD64;
799 else if (sample_rate == INPUT_SAMPLING_RATE_DSD128)
800 sample_rate = OUTPUT_SAMPLING_RATE_DSD128;
801 }
802
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +0530803
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800804 ALOGV("%s: flags: %x, format: %x sample_rate %d, profile %s, app_type %d",
805 __func__, flags, format, sample_rate, profile, app_type_cfg->app_type);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700806 list_for_each(node_i, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530807 s_info = node_to_item(node_i, struct streams_io_cfg, list);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530808 /* Along with flags do profile matching if set at either end.*/
809 if (s_info->flags.out_flags == flags &&
810 ((profile[0] == '\0' && s_info->profile[0] == '\0') ||
811 strncmp(s_info->profile, profile, sizeof(s_info->profile)) == 0)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530812 list_for_each(node_j, &s_info->format_list) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700813 sf_info = node_to_item(node_j, struct stream_format, list);
814 if (sf_info->format == format) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530815 if (set_app_type_cfg(s_info, app_type_cfg, sample_rate, bit_width))
Amit Shekhar6f461b12014-08-01 14:52:58 -0700816 return;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700817 }
818 }
819 }
820 }
821 list_for_each(node_i, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530822 s_info = node_to_item(node_i, struct streams_io_cfg, list);
823 if (s_info->flags.out_flags == AUDIO_OUTPUT_FLAG_PRIMARY) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700824 ALOGV("Compatible output profile not found.");
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530825 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
826 app_type_cfg->sample_rate = s_info->app_type_cfg.sample_rate;
827 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700828 ALOGV("%s Default to primary output: App type: %d sample_rate %d",
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530829 __func__, s_info->app_type_cfg.app_type, app_type_cfg->sample_rate);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700830 return;
831 }
832 }
833 ALOGW("%s: App type could not be selected. Falling back to default", __func__);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700834 app_type_cfg->app_type = platform_get_default_app_type(platform);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700835 app_type_cfg->sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700836 app_type_cfg->bit_width = 16;
837}
838
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +0530839static bool audio_is_this_native_usecase(struct audio_usecase *uc)
840{
841 bool native_usecase = false;
842 struct stream_out *out = (struct stream_out*) uc->stream.out;
843
844 if (PCM_PLAYBACK == uc->type && out != NULL &&
845 NATIVE_AUDIO_MODE_INVALID != platform_get_native_support() &&
846 is_offload_usecase(uc->id) &&
847 (out->sample_rate == OUTPUT_SAMPLING_RATE_44100))
848 native_usecase = true;
849
850 return native_usecase;
851}
852
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800853bool audio_extn_is_dsp_bit_width_enforce_mode_supported(audio_output_flags_t flags)
854{
855 /* DSP bitwidth enforce mode for ADM and AFE:
856 * includes:
857 * deep buffer, low latency, direct pcm and offload.
858 * excludes:
859 * ull(raw+fast), VOIP.
860 */
861 if ((flags & AUDIO_OUTPUT_FLAG_VOIP_RX) ||
862 ((flags & AUDIO_OUTPUT_FLAG_RAW) &&
863 (flags & AUDIO_OUTPUT_FLAG_FAST)))
864 return false;
865
866
867 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) ||
868 (flags & AUDIO_OUTPUT_FLAG_DIRECT) ||
869 (flags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) ||
870 (flags & AUDIO_OUTPUT_FLAG_PRIMARY))
871 return true;
872 else
873 return false;
874}
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800875
876static inline bool audio_is_vr_mode_on(struct audio_device *(__attribute__((unused)) adev))
877{
878 return adev->vr_audio_mode_enabled;
879}
880
Derek Chenf7092792017-05-23 12:23:53 -0400881static void audio_extn_btsco_get_sample_rate(int snd_device, int *sample_rate)
882{
883 switch (snd_device) {
884 case SND_DEVICE_OUT_BT_SCO:
885 case SND_DEVICE_IN_BT_SCO_MIC:
886 case SND_DEVICE_IN_BT_SCO_MIC_NREC:
887 *sample_rate = 8000;
888 break;
889 case SND_DEVICE_OUT_BT_SCO_WB:
890 case SND_DEVICE_IN_BT_SCO_MIC_WB:
891 case SND_DEVICE_IN_BT_SCO_MIC_WB_NREC:
892 *sample_rate = 16000;
893 break;
894 default:
895 ALOGD("%s:Not a BT SCO device, need not update sampling rate\n", __func__);
896 break;
897 }
898}
899
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530900void audio_extn_utils_update_stream_app_type_cfg_for_usecase(
901 struct audio_device *adev,
902 struct audio_usecase *usecase)
903{
904 ALOGV("%s", __func__);
905
906 switch(usecase->type) {
907 case PCM_PLAYBACK:
908 audio_extn_utils_update_stream_output_app_type_cfg(adev->platform,
909 &adev->streams_output_cfg_list,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -0800910 &usecase->stream.out->device_list,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530911 usecase->stream.out->flags,
Aalique Grahame65780b52017-09-27 14:59:56 -0700912 usecase->stream.out->hal_op_format,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530913 usecase->stream.out->sample_rate,
914 usecase->stream.out->bit_width,
915 usecase->stream.out->channel_mask,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530916 usecase->stream.out->profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530917 &usecase->stream.out->app_type_cfg);
918 ALOGV("%s Selected apptype: %d", __func__, usecase->stream.out->app_type_cfg.app_type);
919 break;
920 case PCM_CAPTURE:
Vikram Panduranga93f080e2017-06-07 18:16:14 -0700921 if (usecase->id == USECASE_AUDIO_RECORD_VOIP)
922 usecase->stream.in->app_type_cfg.app_type = APP_TYPE_VOIP_AUDIO;
923 else
924 audio_extn_utils_update_stream_input_app_type_cfg(adev->platform,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530925 &adev->streams_input_cfg_list,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -0800926 &usecase->stream.in->device_list,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530927 usecase->stream.in->flags,
928 usecase->stream.in->format,
929 usecase->stream.in->sample_rate,
930 usecase->stream.in->bit_width,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530931 usecase->stream.in->profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530932 &usecase->stream.in->app_type_cfg);
933 ALOGV("%s Selected apptype: %d", __func__, usecase->stream.in->app_type_cfg.app_type);
934 break;
Surendar Karka93cd25a2018-08-28 14:21:37 +0530935 case TRANSCODE_LOOPBACK_RX :
Siddartha Shaik343abc62017-08-08 11:15:25 +0530936 audio_extn_utils_update_stream_output_app_type_cfg(adev->platform,
937 &adev->streams_output_cfg_list,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -0800938 &usecase->stream.inout->out_config.device_list,
Siddartha Shaik343abc62017-08-08 11:15:25 +0530939 0,
940 usecase->stream.inout->out_config.format,
941 usecase->stream.inout->out_config.sample_rate,
942 usecase->stream.inout->out_config.bit_width,
943 usecase->stream.inout->out_config.channel_mask,
944 usecase->stream.inout->profile,
945 &usecase->stream.inout->out_app_type_cfg);
946 ALOGV("%s Selected apptype: %d", __func__, usecase->stream.inout->out_app_type_cfg.app_type);
947 break;
Derek Chenf7092792017-05-23 12:23:53 -0400948 case PCM_HFP_CALL:
949 switch (usecase->id) {
950 case USECASE_AUDIO_HFP_SCO:
951 case USECASE_AUDIO_HFP_SCO_WB:
952 audio_extn_btsco_get_sample_rate(usecase->out_snd_device,
953 &usecase->out_app_type_cfg.sample_rate);
Derek Chend7307012019-11-12 12:10:41 -0500954 usecase->in_app_type_cfg.sample_rate = usecase->out_app_type_cfg.sample_rate;
Derek Chenf7092792017-05-23 12:23:53 -0400955 break;
956 case USECASE_AUDIO_HFP_SCO_DOWNLINK:
957 case USECASE_AUDIO_HFP_SCO_WB_DOWNLINK:
958 audio_extn_btsco_get_sample_rate(usecase->in_snd_device,
959 &usecase->in_app_type_cfg.sample_rate);
Derek Chend7307012019-11-12 12:10:41 -0500960 usecase->out_app_type_cfg.sample_rate = usecase->in_app_type_cfg.sample_rate;
Derek Chenf7092792017-05-23 12:23:53 -0400961 break;
962 default:
963 ALOGE("%s: usecase id (%d) not supported, use default sample rate",
964 __func__, usecase->id);
965 usecase->in_app_type_cfg.sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
966 usecase->out_app_type_cfg.sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
967 break;
968 }
969 /* update out_app_type_cfg */
970 usecase->out_app_type_cfg.bit_width =
971 platform_get_snd_device_bit_width(usecase->out_snd_device);
972 usecase->out_app_type_cfg.app_type =
973 platform_get_default_app_type_v2(adev->platform, PCM_PLAYBACK);
974 /* update in_app_type_cfg */
975 usecase->in_app_type_cfg.bit_width =
976 platform_get_snd_device_bit_width(usecase->in_snd_device);
977 usecase->in_app_type_cfg.app_type =
978 platform_get_default_app_type_v2(adev->platform, PCM_CAPTURE);
979 ALOGV("%s Selected apptype: playback %d capture %d",
980 __func__, usecase->out_app_type_cfg.app_type, usecase->in_app_type_cfg.app_type);
981 break;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530982 default:
983 ALOGE("%s: app type cfg not supported for usecase type (%d)",
984 __func__, usecase->type);
985 }
986}
987
Aalique Grahame22e49102018-12-18 14:23:57 -0800988static int set_stream_app_type_mixer_ctrl(struct audio_device *adev,
989 int pcm_device_id, int app_type,
990 int acdb_dev_id, int sample_rate,
991 int stream_type,
992 snd_device_t snd_device)
993{
994
995 char mixer_ctl_name[MAX_LENGTH_MIXER_CONTROL_IN_INT];
996 struct mixer_ctl *ctl;
997 int app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT], len = 0, rc = 0;
998 int snd_device_be_idx = -1;
999
1000 if (stream_type == PCM_PLAYBACK) {
1001 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
1002 "Audio Stream %d App Type Cfg", pcm_device_id);
1003 } else if (stream_type == PCM_CAPTURE) {
1004 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
1005 "Audio Stream Capture %d App Type Cfg", pcm_device_id);
1006 }
1007
1008 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
1009 if (!ctl) {
1010 ALOGE("%s: Could not get ctl for mixer cmd - %s",
1011 __func__, mixer_ctl_name);
1012 rc = -EINVAL;
1013 goto exit;
1014 }
1015 app_type_cfg[len++] = app_type;
1016 app_type_cfg[len++] = acdb_dev_id;
1017 app_type_cfg[len++] = sample_rate;
1018
1019 snd_device_be_idx = platform_get_snd_device_backend_index(snd_device);
1020 if (snd_device_be_idx > 0)
1021 app_type_cfg[len++] = snd_device_be_idx;
1022 ALOGV("%s: stream type %d app_type %d, acdb_dev_id %d "
1023 "sample rate %d, snd_device_be_idx %d",
1024 __func__, stream_type, app_type, acdb_dev_id, sample_rate,
1025 snd_device_be_idx);
1026 mixer_ctl_set_array(ctl, app_type_cfg, len);
1027
1028exit:
1029 return rc;
1030}
1031
1032static int audio_extn_utils_send_app_type_cfg_hfp(struct audio_device *adev,
1033 struct audio_usecase *usecase)
1034{
1035 int pcm_device_id, acdb_dev_id = 0, snd_device = usecase->out_snd_device;
1036 int32_t sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
1037 int app_type = 0, rc = 0;
Derek Chenf7092792017-05-23 12:23:53 -04001038 bool is_bus_dev_usecase = false;
Aalique Grahame22e49102018-12-18 14:23:57 -08001039
1040 ALOGV("%s", __func__);
1041
1042 if (usecase->type != PCM_HFP_CALL) {
Derek Chenf7092792017-05-23 12:23:53 -04001043 ALOGV("%s: not a HFP path, no need to cfg app type", __func__);
Aalique Grahame22e49102018-12-18 14:23:57 -08001044 rc = 0;
1045 goto exit_send_app_type_cfg;
1046 }
1047 if ((usecase->id != USECASE_AUDIO_HFP_SCO) &&
Derek Chenf7092792017-05-23 12:23:53 -04001048 (usecase->id != USECASE_AUDIO_HFP_SCO_WB) &&
1049 (usecase->id != USECASE_AUDIO_HFP_SCO_DOWNLINK) &&
1050 (usecase->id != USECASE_AUDIO_HFP_SCO_WB_DOWNLINK)) {
1051 ALOGV("%s: a usecase where app type cfg is not required", __func__);
Aalique Grahame22e49102018-12-18 14:23:57 -08001052 rc = 0;
1053 goto exit_send_app_type_cfg;
1054 }
1055
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08001056 if (compare_device_type(&usecase->device_list, AUDIO_DEVICE_OUT_BUS))
Derek Chenf7092792017-05-23 12:23:53 -04001057 is_bus_dev_usecase = true;
1058
Aalique Grahame22e49102018-12-18 14:23:57 -08001059 snd_device = usecase->out_snd_device;
1060 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_PLAYBACK);
1061
1062 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
1063 if (acdb_dev_id < 0) {
1064 ALOGE("%s: Couldn't get the acdb dev id", __func__);
1065 rc = -EINVAL;
1066 goto exit_send_app_type_cfg;
1067 }
1068
1069 if (usecase->type == PCM_HFP_CALL) {
1070
1071 /* config HFP session:1 playback path */
Derek Chenf7092792017-05-23 12:23:53 -04001072 if (is_bus_dev_usecase) {
1073 app_type = usecase->out_app_type_cfg.app_type;
1074 sample_rate= usecase->out_app_type_cfg.sample_rate;
1075 } else {
1076 snd_device = SND_DEVICE_NONE; // use legacy behavior
1077 app_type = platform_get_default_app_type_v2(adev->platform, PCM_PLAYBACK);
1078 sample_rate= CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
1079 }
Aalique Grahame22e49102018-12-18 14:23:57 -08001080 rc = set_stream_app_type_mixer_ctrl(adev, pcm_device_id, app_type,
1081 acdb_dev_id, sample_rate,
1082 PCM_PLAYBACK,
Derek Chenf7092792017-05-23 12:23:53 -04001083 snd_device);
Aalique Grahame22e49102018-12-18 14:23:57 -08001084 if (rc < 0)
1085 goto exit_send_app_type_cfg;
1086
1087 /* config HFP session:1 capture path */
Derek Chenf7092792017-05-23 12:23:53 -04001088 if (is_bus_dev_usecase) {
1089 snd_device = usecase->in_snd_device;
1090 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_CAPTURE);
1091 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
1092 if (acdb_dev_id < 0) {
1093 ALOGE("%s: Couldn't get the acdb dev id", __func__);
1094 rc = -EINVAL;
1095 goto exit_send_app_type_cfg;
1096 }
1097 app_type = usecase->in_app_type_cfg.app_type;
1098 sample_rate= usecase->in_app_type_cfg.sample_rate;
1099 } else {
1100 snd_device = SND_DEVICE_NONE; // use legacy behavior
1101 app_type = platform_get_default_app_type_v2(adev->platform, PCM_CAPTURE);
1102 }
Aalique Grahame22e49102018-12-18 14:23:57 -08001103 rc = set_stream_app_type_mixer_ctrl(adev, pcm_device_id, app_type,
1104 acdb_dev_id, sample_rate,
1105 PCM_CAPTURE,
Derek Chenf7092792017-05-23 12:23:53 -04001106 snd_device);
Aalique Grahame22e49102018-12-18 14:23:57 -08001107 if (rc < 0)
1108 goto exit_send_app_type_cfg;
1109
Derek Chenf7092792017-05-23 12:23:53 -04001110 if (is_bus_dev_usecase)
1111 goto exit_send_app_type_cfg;
1112
Aalique Grahame22e49102018-12-18 14:23:57 -08001113 /* config HFP session:2 capture path */
1114 pcm_device_id = HFP_ASM_RX_TX;
1115 snd_device = usecase->in_snd_device;
1116 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
1117 if (acdb_dev_id <= 0) {
1118 ALOGE("%s: Couldn't get the acdb dev id", __func__);
1119 rc = -EINVAL;
1120 goto exit_send_app_type_cfg;
1121 }
1122 app_type = platform_get_default_app_type_v2(adev->platform, PCM_CAPTURE);
1123 rc = set_stream_app_type_mixer_ctrl(adev, pcm_device_id, app_type,
1124 acdb_dev_id, sample_rate, PCM_CAPTURE,
1125 SND_DEVICE_NONE);
1126 if (rc < 0)
1127 goto exit_send_app_type_cfg;
1128
1129 /* config HFP session:2 playback path */
1130 app_type = platform_get_default_app_type_v2(adev->platform, PCM_PLAYBACK);
1131 rc = set_stream_app_type_mixer_ctrl(adev, pcm_device_id, app_type,
1132 acdb_dev_id, sample_rate,
1133 PCM_PLAYBACK, SND_DEVICE_NONE);
1134 if (rc < 0)
1135 goto exit_send_app_type_cfg;
1136 }
1137
1138 rc = 0;
1139exit_send_app_type_cfg:
1140 return rc;
1141}
1142
Zhou Song06761dd2019-04-28 18:08:17 +08001143int audio_extn_utils_get_app_sample_rate_for_device(
1144 struct audio_device *adev,
1145 struct audio_usecase *usecase, int snd_device)
1146{
1147 char value[PROPERTY_VALUE_MAX] = {0};
1148 int sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
1149
1150 if ((usecase->type == PCM_PLAYBACK) && (usecase->stream.out != NULL)) {
1151 property_get("vendor.audio.playback.mch.downsample",value,"");
1152 if (!strncmp("true", value, sizeof("true"))) {
1153 if ((popcount(usecase->stream.out->channel_mask) > 2) &&
1154 (usecase->stream.out->app_type_cfg.sample_rate > CODEC_BACKEND_DEFAULT_SAMPLE_RATE) &&
1155 !(usecase->stream.out->flags &
1156 (audio_output_flags_t)AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH))
1157 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
1158 }
1159
1160 if (usecase->id == USECASE_AUDIO_PLAYBACK_VOIP) {
1161 usecase->stream.out->app_type_cfg.sample_rate = usecase->stream.out->sample_rate;
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08001162 } else if (compare_device_type(&usecase->stream.out->device_list,
1163 AUDIO_DEVICE_OUT_SPEAKER)) {
Zhou Song06761dd2019-04-28 18:08:17 +08001164 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
1165 } else if ((snd_device == SND_DEVICE_OUT_HDMI ||
1166 snd_device == SND_DEVICE_OUT_USB_HEADSET ||
1167 snd_device == SND_DEVICE_OUT_DISPLAY_PORT) &&
1168 (usecase->stream.out->sample_rate >= OUTPUT_SAMPLING_RATE_44100)) {
1169 /*
1170 * To best utlize DSP, check if the stream sample rate is supported/multiple of
1171 * configured device sample rate, if not update the COPP rate to be equal to the
1172 * device sample rate, else open COPP at stream sample rate
1173 */
1174 platform_check_and_update_copp_sample_rate(adev->platform, snd_device,
1175 usecase->stream.out->sample_rate,
1176 &usecase->stream.out->app_type_cfg.sample_rate);
1177 } else if (((snd_device != SND_DEVICE_OUT_HEADPHONES_44_1 &&
1178 !audio_is_this_native_usecase(usecase)) &&
1179 usecase->stream.out->sample_rate == OUTPUT_SAMPLING_RATE_44100) ||
1180 (usecase->stream.out->sample_rate < OUTPUT_SAMPLING_RATE_44100)) {
1181 /* Reset to default if no native stream is active*/
1182 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Zhou Song9e9b3812019-12-30 17:52:10 +08001183 } else if (usecase->out_snd_device == SND_DEVICE_OUT_BT_A2DP ||
1184 usecase->out_snd_device == SND_DEVICE_OUT_SPEAKER_AND_BT_A2DP ||
1185 usecase->out_snd_device == SND_DEVICE_OUT_SPEAKER_SAFE_AND_BT_A2DP) {
Zhou Song06761dd2019-04-28 18:08:17 +08001186 /*
1187 * For a2dp playback get encoder sampling rate and set copp sampling rate,
1188 * for bit width use the stream param only.
1189 */
1190 audio_extn_a2dp_get_enc_sample_rate(&usecase->stream.out->app_type_cfg.sample_rate);
1191 ALOGI("%s using %d sample rate rate for A2DP CoPP",
1192 __func__, usecase->stream.out->app_type_cfg.sample_rate);
1193 }
1194 audio_extn_btsco_get_sample_rate(snd_device, &usecase->stream.out->app_type_cfg.sample_rate);
1195 sample_rate = usecase->stream.out->app_type_cfg.sample_rate;
1196
1197 if (((usecase->stream.out->format == AUDIO_FORMAT_E_AC3) ||
1198 (usecase->stream.out->format == AUDIO_FORMAT_E_AC3_JOC) ||
1199 (usecase->stream.out->format == AUDIO_FORMAT_DOLBY_TRUEHD))
1200 && audio_extn_passthru_is_passthrough_stream(usecase->stream.out)
1201 && !audio_extn_passthru_is_convert_supported(adev, usecase->stream.out)) {
1202 sample_rate = sample_rate * 4;
1203 if (sample_rate > HDMI_PASSTHROUGH_MAX_SAMPLE_RATE)
1204 sample_rate = HDMI_PASSTHROUGH_MAX_SAMPLE_RATE;
1205 }
1206 } else if (usecase->type == PCM_CAPTURE) {
1207 if (usecase->stream.in != NULL) {
1208 if (usecase->id == USECASE_AUDIO_RECORD_VOIP)
1209 usecase->stream.in->app_type_cfg.sample_rate = usecase->stream.in->sample_rate;
1210 if (voice_is_in_call_rec_stream(usecase->stream.in)) {
1211 audio_extn_btsco_get_sample_rate(usecase->in_snd_device,
1212 &usecase->stream.in->app_type_cfg.sample_rate);
1213 } else {
1214 audio_extn_btsco_get_sample_rate(snd_device,
1215 &usecase->stream.in->app_type_cfg.sample_rate);
1216 }
1217 sample_rate = usecase->stream.in->app_type_cfg.sample_rate;
1218 } else if (usecase->id == USECASE_AUDIO_SPKR_CALIB_TX) {
Sujin Panickerb6d76262019-09-09 10:55:38 +05301219 if ((property_get("vendor.audio.spkr_prot.tx.sampling_rate", value, NULL) > 0))
1220 sample_rate = atoi(value);
1221 else
1222 sample_rate = SAMPLE_RATE_8000;
Zhou Song06761dd2019-04-28 18:08:17 +08001223 }
1224 } else if (usecase->type == TRANSCODE_LOOPBACK_RX) {
1225 sample_rate = usecase->stream.inout->out_config.sample_rate;
1226 }
1227 return sample_rate;
1228}
1229
Siena Richard7c2db772016-12-21 11:32:34 -08001230static int send_app_type_cfg_for_device(struct audio_device *adev,
1231 struct audio_usecase *usecase,
1232 int split_snd_device)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001233{
1234 char mixer_ctl_name[MAX_LENGTH_MIXER_CONTROL_IN_INT];
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301235 size_t app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT] = {0};
1236 int len = 0, rc;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001237 struct mixer_ctl *ctl;
Siena Richard7c2db772016-12-21 11:32:34 -08001238 int pcm_device_id = 0, acdb_dev_id, app_type;
1239 int snd_device = split_snd_device, snd_device_be_idx = -1;
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +05301240 int32_t sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Varun Balaraje49253e2017-07-06 19:48:56 +05301241 struct streams_io_cfg *s_info = NULL;
1242 struct listnode *node = NULL;
Nikhil Laturkarac4a7492017-08-31 18:27:19 +05301243 int bd_app_type = 0;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001244
Siena Richard7c2db772016-12-21 11:32:34 -08001245 ALOGV("%s: usecase->out_snd_device %s, usecase->in_snd_device %s, split_snd_device %s",
1246 __func__, platform_get_snd_device_name(usecase->out_snd_device),
1247 platform_get_snd_device_name(usecase->in_snd_device),
1248 platform_get_snd_device_name(split_snd_device));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001249
Siddartha Shaik343abc62017-08-08 11:15:25 +05301250 if (usecase->type != PCM_PLAYBACK && usecase->type != PCM_CAPTURE &&
Surendar Karka93cd25a2018-08-28 14:21:37 +05301251 usecase->type != TRANSCODE_LOOPBACK_RX) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301252 ALOGE("%s: not a playback/capture path, no need to cfg app type", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001253 rc = 0;
1254 goto exit_send_app_type_cfg;
1255 }
1256 if ((usecase->id != USECASE_AUDIO_PLAYBACK_DEEP_BUFFER) &&
Vignesh Kulothungan0a2eff02019-07-11 16:30:13 -07001257 (usecase->id != USECASE_AUDIO_PLAYBACK_WITH_HAPTICS) &&
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001258 (usecase->id != USECASE_AUDIO_PLAYBACK_LOW_LATENCY) &&
1259 (usecase->id != USECASE_AUDIO_PLAYBACK_MULTI_CH) &&
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -08001260 (usecase->id != USECASE_AUDIO_PLAYBACK_ULL) &&
Vikram Panduranga93f080e2017-06-07 18:16:14 -07001261 (usecase->id != USECASE_AUDIO_PLAYBACK_VOIP) &&
Surendar Karka93cd25a2018-08-28 14:21:37 +05301262 (usecase->id != USECASE_AUDIO_TRANSCODE_LOOPBACK_RX) &&
Varun Balaraje49253e2017-07-06 19:48:56 +05301263 (!is_interactive_usecase(usecase->id)) &&
Srikanth Uyyala9d551402015-08-25 16:03:42 +05301264 (!is_offload_usecase(usecase->id)) &&
Derek Chenf6318be2017-06-12 17:16:24 -04001265 (usecase->type != PCM_CAPTURE) &&
1266 (!audio_extn_auto_hal_is_bus_device_usecase(usecase->id))) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301267 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 -07001268 rc = 0;
1269 goto exit_send_app_type_cfg;
1270 }
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -08001271
1272 //if VR is active then only send the mixer control
1273 if (usecase->id == USECASE_AUDIO_PLAYBACK_ULL && !audio_is_vr_mode_on(adev)) {
1274 ALOGI("ULL doesnt need sending app type cfg, returning");
1275 rc = 0;
1276 goto exit_send_app_type_cfg;
1277 }
1278
Surendar Karka93cd25a2018-08-28 14:21:37 +05301279 if (usecase->type == PCM_PLAYBACK || usecase->type == TRANSCODE_LOOPBACK_RX) {
Ben Romberger1fafdde2015-09-09 19:43:15 -07001280 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_PLAYBACK);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301281 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
1282 "Audio Stream %d App Type Cfg", pcm_device_id);
Ben Romberger1fafdde2015-09-09 19:43:15 -07001283 } else if (usecase->type == PCM_CAPTURE) {
Ben Romberger1fafdde2015-09-09 19:43:15 -07001284 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_CAPTURE);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301285 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
1286 "Audio Stream Capture %d App Type Cfg", pcm_device_id);
Srikanth Uyyala9d551402015-08-25 16:03:42 +05301287 }
Siena Richard7c2db772016-12-21 11:32:34 -08001288
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001289 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
1290 if (!ctl) {
1291 ALOGE("%s: Could not get ctl for mixer cmd - %s", __func__,
1292 mixer_ctl_name);
1293 rc = -EINVAL;
1294 goto exit_send_app_type_cfg;
1295 }
Aditya Bavanari701a6992017-03-30 19:17:16 +05301296 snd_device = platform_get_spkr_prot_snd_device(snd_device);
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301297 if (voice_is_in_call_rec_stream(usecase->stream.in) && usecase->type == PCM_CAPTURE) {
1298 snd_device_t voice_device = voice_get_incall_rec_snd_device(usecase->in_snd_device);
1299 acdb_dev_id = platform_get_snd_device_acdb_id(voice_device);
1300 ALOGV("acdb id for voice call use case %d", acdb_dev_id);
1301 } else {
1302 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
1303 }
Rohit Kumar1181b292017-01-31 18:07:17 +05301304 if (acdb_dev_id <= 0) {
1305 ALOGE("%s: Couldn't get the acdb dev id", __func__);
1306 rc = -EINVAL;
1307 goto exit_send_app_type_cfg;
1308 }
1309
Siena Richard7c2db772016-12-21 11:32:34 -08001310 snd_device_be_idx = platform_get_snd_device_backend_index(snd_device);
1311 if (snd_device_be_idx < 0) {
1312 ALOGE("%s: Couldn't get the backend index for snd device %s ret=%d",
1313 __func__, platform_get_snd_device_name(snd_device),
1314 snd_device_be_idx);
1315 }
1316
Zhou Song06761dd2019-04-28 18:08:17 +08001317 sample_rate = audio_extn_utils_get_app_sample_rate_for_device(adev, usecase, snd_device);
1318
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301319 if ((usecase->type == PCM_PLAYBACK) && (usecase->stream.out != NULL)) {
Varun Balaraje49253e2017-07-06 19:48:56 +05301320 /* Interactive streams are supported with only direct app type id.
1321 * Get Direct profile app type and use it for interactive streams
1322 */
1323 list_for_each(node, &adev->streams_output_cfg_list) {
1324 s_info = node_to_item(node, struct streams_io_cfg, list);
Aalique Grahame5cd12ff2017-10-19 10:57:00 -07001325 if (s_info->flags.out_flags == (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_BD |
Nikhil Laturkarac4a7492017-08-31 18:27:19 +05301326 AUDIO_OUTPUT_FLAG_DIRECT_PCM |
1327 AUDIO_OUTPUT_FLAG_DIRECT))
1328 bd_app_type = s_info->app_type_cfg.app_type;
Varun Balaraje49253e2017-07-06 19:48:56 +05301329 }
Aalique Grahame5cd12ff2017-10-19 10:57:00 -07001330 if (usecase->stream.out->flags == (audio_output_flags_t)AUDIO_OUTPUT_FLAG_INTERACTIVE)
Nikhil Laturkarac4a7492017-08-31 18:27:19 +05301331 app_type = bd_app_type;
Varun Balaraje49253e2017-07-06 19:48:56 +05301332 else
1333 app_type = usecase->stream.out->app_type_cfg.app_type;
Siena Richard7c2db772016-12-21 11:32:34 -08001334 app_type_cfg[len++] = app_type;
Mingming Yin21854652016-04-13 11:54:02 -07001335 app_type_cfg[len++] = acdb_dev_id;
Naresh Tanniru3a406772017-05-10 13:09:05 -07001336 app_type_cfg[len++] = sample_rate;
1337
Siena Richard7c2db772016-12-21 11:32:34 -08001338 if (snd_device_be_idx > 0)
1339 app_type_cfg[len++] = snd_device_be_idx;
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301340
Siena Richard7c2db772016-12-21 11:32:34 -08001341 ALOGI("%s PLAYBACK app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
1342 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301343
1344 } else if ((usecase->type == PCM_CAPTURE) && (usecase->stream.in != NULL)) {
Siena Richard7c2db772016-12-21 11:32:34 -08001345 app_type = usecase->stream.in->app_type_cfg.app_type;
1346 app_type_cfg[len++] = app_type;
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301347 app_type_cfg[len++] = acdb_dev_id;
Siena Richard7c2db772016-12-21 11:32:34 -08001348 app_type_cfg[len++] = sample_rate;
1349 if (snd_device_be_idx > 0)
1350 app_type_cfg[len++] = snd_device_be_idx;
1351 ALOGI("%s CAPTURE app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
1352 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301353 } else {
Siena Richard7c2db772016-12-21 11:32:34 -08001354 app_type = platform_get_default_app_type_v2(adev->platform, usecase->type);
Surendar Karka93cd25a2018-08-28 14:21:37 +05301355 if(usecase->type == TRANSCODE_LOOPBACK_RX) {
Siddartha Shaik343abc62017-08-08 11:15:25 +05301356 app_type = usecase->stream.inout->out_app_type_cfg.app_type;
1357 }
Siena Richard7c2db772016-12-21 11:32:34 -08001358 app_type_cfg[len++] = app_type;
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301359 app_type_cfg[len++] = acdb_dev_id;
1360 app_type_cfg[len++] = sample_rate;
Siena Richard7c2db772016-12-21 11:32:34 -08001361 if (snd_device_be_idx > 0)
1362 app_type_cfg[len++] = snd_device_be_idx;
1363 ALOGI("%s default app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
1364 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Ashish Jainc02430d2016-01-04 10:42:43 +05301365 }
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301366
Siddartha Shaik343abc62017-08-08 11:15:25 +05301367 if(ctl)
1368 mixer_ctl_set_array(ctl, app_type_cfg, len);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001369 rc = 0;
1370exit_send_app_type_cfg:
1371 return rc;
1372}
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001373
Deeraj Soman14230922019-01-30 16:39:30 +05301374static int audio_extn_utils_check_input_parameters(uint32_t sample_rate,
1375 audio_format_t format,
1376 int channel_count)
1377{
1378 int ret = 0;
1379
1380 if (((format != AUDIO_FORMAT_PCM_16_BIT) && (format != AUDIO_FORMAT_PCM_8_24_BIT) &&
1381 (format != AUDIO_FORMAT_PCM_24_BIT_PACKED) && (format != AUDIO_FORMAT_PCM_32_BIT) &&
1382 (format != AUDIO_FORMAT_PCM_FLOAT)) &&
1383 !voice_extn_compress_voip_is_format_supported(format) &&
1384 !audio_extn_compr_cap_format_supported(format) &&
1385 !audio_extn_cin_format_supported(format))
1386 ret = -EINVAL;
1387
1388 switch (channel_count) {
1389 case 1:
1390 case 2:
1391 case 3:
1392 case 4:
1393 case 6:
1394 case 8:
1395 break;
1396 default:
1397 ret = -EINVAL;
1398 }
1399
1400 switch (sample_rate) {
1401 case 8000:
1402 case 11025:
1403 case 12000:
1404 case 16000:
1405 case 22050:
1406 case 24000:
1407 case 32000:
1408 case 44100:
1409 case 48000:
1410 case 88200:
1411 case 96000:
1412 case 176400:
1413 case 192000:
1414 break;
1415 default:
1416 ret = -EINVAL;
1417 }
1418
1419 return ret;
1420}
1421
1422static inline uint32_t audio_extn_utils_nearest_multiple(uint32_t num, uint32_t multiplier)
1423{
1424 uint32_t remainder = 0;
1425
1426 if (!multiplier)
1427 return num;
1428
1429 remainder = num % multiplier;
1430 if (remainder)
1431 num += (multiplier - remainder);
1432
1433 return num;
1434}
1435
1436static inline uint32_t audio_extn_utils_lcm(uint32_t num1, uint32_t num2)
1437{
1438 uint32_t high = num1, low = num2, temp = 0;
1439
1440 if (!num1 || !num2)
1441 return 0;
1442
1443 if (num1 < num2) {
1444 high = num2;
1445 low = num1;
1446 }
1447
1448 while (low != 0) {
1449 temp = low;
1450 low = high % low;
1451 high = temp;
1452 }
1453 return (num1 * num2)/high;
1454}
1455
Siena Richard7c2db772016-12-21 11:32:34 -08001456int audio_extn_utils_send_app_type_cfg(struct audio_device *adev,
1457 struct audio_usecase *usecase)
1458{
1459 int i, num_devices = 0;
1460 snd_device_t new_snd_devices[SND_DEVICE_OUT_END] = {0};
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301461 snd_device_t in_snd_device = usecase->in_snd_device;
Siena Richard7c2db772016-12-21 11:32:34 -08001462 int rc = 0;
1463
Aalique Grahame22e49102018-12-18 14:23:57 -08001464 if (usecase->type == PCM_HFP_CALL) {
1465 return audio_extn_utils_send_app_type_cfg_hfp(adev, usecase);
1466 }
1467
Siena Richard7c2db772016-12-21 11:32:34 -08001468 switch (usecase->type) {
1469 case PCM_PLAYBACK:
Surendar Karka93cd25a2018-08-28 14:21:37 +05301470 case TRANSCODE_LOOPBACK_RX:
Siena Richard7c2db772016-12-21 11:32:34 -08001471 ALOGD("%s: usecase->out_snd_device %s",
1472 __func__, platform_get_snd_device_name(usecase->out_snd_device));
1473 /* check for out combo device */
1474 if (platform_split_snd_device(adev->platform,
1475 usecase->out_snd_device,
1476 &num_devices, new_snd_devices)) {
1477 new_snd_devices[0] = usecase->out_snd_device;
1478 num_devices = 1;
1479 }
1480 break;
1481 case PCM_CAPTURE:
1482 ALOGD("%s: usecase->in_snd_device %s",
1483 __func__, platform_get_snd_device_name(usecase->in_snd_device));
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301484 if (voice_is_in_call_rec_stream(usecase->stream.in)) {
1485 in_snd_device = voice_get_incall_rec_backend_device(usecase->stream.in);
1486 }
Siena Richard7c2db772016-12-21 11:32:34 -08001487 /* check for in combo device */
1488 if (platform_split_snd_device(adev->platform,
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301489 in_snd_device,
Siena Richard7c2db772016-12-21 11:32:34 -08001490 &num_devices, new_snd_devices)) {
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301491 new_snd_devices[0] = in_snd_device;
Siena Richard7c2db772016-12-21 11:32:34 -08001492 num_devices = 1;
1493 }
1494 break;
1495 default:
1496 ALOGI("%s: not a playback/capture path, no need to cfg app type", __func__);
1497 rc = 0;
1498 break;
1499 }
1500
1501 for (i = 0; i < num_devices; i++) {
1502 rc = send_app_type_cfg_for_device(adev, usecase, new_snd_devices[i]);
1503 if (rc)
1504 break;
1505 }
1506
1507 return rc;
1508}
1509
Preetam Singh Ranawat9519e9c2015-11-18 16:05:55 +05301510int read_line_from_file(const char *path, char *buf, size_t count)
1511{
1512 char * fgets_ret;
1513 FILE * fd;
1514 int rv;
1515
1516 fd = fopen(path, "r");
1517 if (fd == NULL)
1518 return -1;
1519
1520 fgets_ret = fgets(buf, (int)count, fd);
1521 if (NULL != fgets_ret) {
1522 rv = (int)strlen(buf);
1523 } else {
1524 rv = ferror(fd);
1525 }
1526 fclose(fd);
1527
1528 return rv;
1529}
1530
Ashish Jainf1eaa582016-05-23 20:54:24 +05301531/*Translates ALSA formats to AOSP PCM formats*/
1532audio_format_t alsa_format_to_hal(uint32_t alsa_format)
1533{
1534 audio_format_t format;
1535
1536 switch(alsa_format) {
1537 case SNDRV_PCM_FORMAT_S16_LE:
1538 format = AUDIO_FORMAT_PCM_16_BIT;
1539 break;
1540 case SNDRV_PCM_FORMAT_S24_3LE:
1541 format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
1542 break;
1543 case SNDRV_PCM_FORMAT_S24_LE:
1544 format = AUDIO_FORMAT_PCM_8_24_BIT;
1545 break;
1546 case SNDRV_PCM_FORMAT_S32_LE:
1547 format = AUDIO_FORMAT_PCM_32_BIT;
1548 break;
1549 default:
1550 ALOGW("Incorrect ALSA format");
1551 format = AUDIO_FORMAT_INVALID;
1552 }
1553 return format;
1554}
1555
1556/*Translates hal format (AOSP) to alsa formats*/
1557uint32_t hal_format_to_alsa(audio_format_t hal_format)
1558{
1559 uint32_t alsa_format;
1560
1561 switch (hal_format) {
1562 case AUDIO_FORMAT_PCM_32_BIT: {
1563 if (platform_supports_true_32bit())
1564 alsa_format = SNDRV_PCM_FORMAT_S32_LE;
1565 else
1566 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1567 }
1568 break;
1569 case AUDIO_FORMAT_PCM_8_BIT:
1570 alsa_format = SNDRV_PCM_FORMAT_S8;
1571 break;
1572 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
1573 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1574 break;
1575 case AUDIO_FORMAT_PCM_8_24_BIT: {
1576 if (platform_supports_true_32bit())
1577 alsa_format = SNDRV_PCM_FORMAT_S32_LE;
1578 else
1579 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1580 }
1581 break;
1582 case AUDIO_FORMAT_PCM_FLOAT:
1583 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1584 break;
1585 default:
1586 case AUDIO_FORMAT_PCM_16_BIT:
1587 alsa_format = SNDRV_PCM_FORMAT_S16_LE;
1588 break;
1589 }
1590 return alsa_format;
1591}
1592
Ashish Jain83a6cc22016-06-28 14:34:17 +05301593/*Translates PCM formats to AOSP formats*/
1594audio_format_t pcm_format_to_hal(uint32_t pcm_format)
1595{
1596 audio_format_t format = AUDIO_FORMAT_INVALID;
1597
1598 switch(pcm_format) {
1599 case PCM_FORMAT_S16_LE:
1600 format = AUDIO_FORMAT_PCM_16_BIT;
1601 break;
1602 case PCM_FORMAT_S24_3LE:
1603 format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
1604 break;
1605 case PCM_FORMAT_S24_LE:
1606 format = AUDIO_FORMAT_PCM_8_24_BIT;
1607 break;
1608 case PCM_FORMAT_S32_LE:
1609 format = AUDIO_FORMAT_PCM_32_BIT;
1610 break;
1611 default:
1612 ALOGW("Incorrect PCM format");
1613 format = AUDIO_FORMAT_INVALID;
1614 }
1615 return format;
1616}
1617
1618/*Translates hal format (AOSP) to alsa formats*/
1619uint32_t hal_format_to_pcm(audio_format_t hal_format)
1620{
1621 uint32_t pcm_format;
1622
1623 switch (hal_format) {
1624 case AUDIO_FORMAT_PCM_32_BIT:
1625 case AUDIO_FORMAT_PCM_8_24_BIT:
1626 case AUDIO_FORMAT_PCM_FLOAT: {
1627 if (platform_supports_true_32bit())
1628 pcm_format = PCM_FORMAT_S32_LE;
1629 else
1630 pcm_format = PCM_FORMAT_S24_3LE;
1631 }
1632 break;
1633 case AUDIO_FORMAT_PCM_8_BIT:
1634 pcm_format = PCM_FORMAT_S8;
1635 break;
1636 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
1637 pcm_format = PCM_FORMAT_S24_3LE;
1638 break;
1639 default:
1640 case AUDIO_FORMAT_PCM_16_BIT:
1641 pcm_format = PCM_FORMAT_S16_LE;
1642 break;
1643 }
1644 return pcm_format;
1645}
1646
Ashish Jainf1eaa582016-05-23 20:54:24 +05301647uint32_t get_alsa_fragment_size(uint32_t bytes_per_sample,
1648 uint32_t sample_rate,
Deeraj Soman65358ab2019-02-07 15:40:49 +05301649 uint32_t noOfChannels,
1650 int64_t duration_ms)
Ashish Jainf1eaa582016-05-23 20:54:24 +05301651{
1652 uint32_t fragment_size = 0;
1653 uint32_t pcm_offload_time = PCM_OFFLOAD_BUFFER_DURATION;
1654
Deeraj Soman65358ab2019-02-07 15:40:49 +05301655 if (duration_ms >= MIN_OFFLOAD_BUFFER_DURATION_MS && duration_ms <= MAX_OFFLOAD_BUFFER_DURATION_MS)
1656 pcm_offload_time = duration_ms;
1657
Ashish Jainf1eaa582016-05-23 20:54:24 +05301658 fragment_size = (pcm_offload_time
1659 * sample_rate
1660 * bytes_per_sample
1661 * noOfChannels)/1000;
1662 if (fragment_size < MIN_PCM_OFFLOAD_FRAGMENT_SIZE)
1663 fragment_size = MIN_PCM_OFFLOAD_FRAGMENT_SIZE;
1664 else if (fragment_size > MAX_PCM_OFFLOAD_FRAGMENT_SIZE)
1665 fragment_size = MAX_PCM_OFFLOAD_FRAGMENT_SIZE;
1666 /*To have same PCM samples for all channels, the buffer size requires to
1667 *be multiple of (number of channels * bytes per sample)
1668 *For writes to succeed, the buffer must be written at address which is multiple of 32
1669 */
Aalique Grahameb85f2d92017-10-16 17:53:23 -07001670 fragment_size = ALIGN(fragment_size, (bytes_per_sample * noOfChannels * 32));
Ashish Jainf1eaa582016-05-23 20:54:24 +05301671
1672 ALOGI("PCM offload Fragment size to %d bytes", fragment_size);
1673 return fragment_size;
1674}
1675
1676/* Calculates the fragment size required to configure compress session.
1677 * Based on the alsa format selected, decide if conversion is needed in
1678
1679 * HAL ( e.g. convert AUDIO_FORMAT_PCM_FLOAT input format to
1680 * AUDIO_FORMAT_PCM_24_BIT_PACKED before writing to the compress driver.
1681 */
1682void audio_extn_utils_update_direct_pcm_fragment_size(struct stream_out *out)
1683{
Ashish Jain83a6cc22016-06-28 14:34:17 +05301684 audio_format_t dst_format = out->hal_op_format;
1685 audio_format_t src_format = out->hal_ip_format;
Ashish Jainf1eaa582016-05-23 20:54:24 +05301686 uint32_t hal_op_bytes_per_sample = audio_bytes_per_sample(dst_format);
1687 uint32_t hal_ip_bytes_per_sample = audio_bytes_per_sample(src_format);
1688
1689 out->compr_config.fragment_size =
1690 get_alsa_fragment_size(hal_op_bytes_per_sample,
1691 out->sample_rate,
Deeraj Soman65358ab2019-02-07 15:40:49 +05301692 popcount(out->channel_mask),
1693 out->info.duration_us / 1000);
Ashish Jainf1eaa582016-05-23 20:54:24 +05301694
1695 if ((src_format != dst_format) &&
1696 hal_op_bytes_per_sample != hal_ip_bytes_per_sample) {
1697
Ashish Jain83a6cc22016-06-28 14:34:17 +05301698 out->hal_fragment_size =
Ashish Jainf1eaa582016-05-23 20:54:24 +05301699 ((out->compr_config.fragment_size * hal_ip_bytes_per_sample) /
1700 hal_op_bytes_per_sample);
1701 ALOGI("enable conversion hal_input_fragment_size is %d src_format %x dst_format %x",
Ashish Jain83a6cc22016-06-28 14:34:17 +05301702 out->hal_fragment_size, src_format, dst_format);
Ashish Jainf1eaa582016-05-23 20:54:24 +05301703 } else {
Ashish Jain83a6cc22016-06-28 14:34:17 +05301704 out->hal_fragment_size = out->compr_config.fragment_size;
Ashish Jainf1eaa582016-05-23 20:54:24 +05301705 }
1706}
1707
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301708/* converts pcm format 24_8 to 8_24 inplace */
1709size_t audio_extn_utils_convert_format_24_8_to_8_24(void *buf, size_t bytes)
1710{
1711 size_t i = 0;
1712 int *int_buf_stream = buf;
1713
1714 if ((bytes % 4) != 0) {
1715 ALOGE("%s: wrong inout buffer! ... is not 32 bit aligned ", __func__);
1716 return -EINVAL;
1717 }
1718
1719 for (; i < (bytes / 4); i++)
1720 int_buf_stream[i] >>= 8;
1721
1722 return bytes;
1723}
1724
Xiaojun Sang782e5b12020-06-29 21:13:06 +08001725#ifdef AUDIO_GKI_ENABLED
1726int get_snd_codec_id(audio_format_t format)
1727{
1728 int id = 0;
1729
1730 switch (format & AUDIO_FORMAT_MAIN_MASK) {
1731 case AUDIO_FORMAT_MP3:
1732 id = SND_AUDIOCODEC_MP3;
1733 break;
1734 case AUDIO_FORMAT_AAC:
1735 id = SND_AUDIOCODEC_AAC;
1736 break;
1737 case AUDIO_FORMAT_AAC_ADTS:
1738 id = SND_AUDIOCODEC_AAC;
1739 break;
1740 case AUDIO_FORMAT_AAC_LATM:
1741 id = SND_AUDIOCODEC_AAC;
1742 break;
1743 case AUDIO_FORMAT_PCM:
1744 id = SND_AUDIOCODEC_PCM;
1745 break;
1746 case AUDIO_FORMAT_FLAC:
1747 case AUDIO_FORMAT_ALAC:
1748 case AUDIO_FORMAT_APE:
1749 case AUDIO_FORMAT_VORBIS:
1750 case AUDIO_FORMAT_WMA:
1751 case AUDIO_FORMAT_WMA_PRO:
1752 case AUDIO_FORMAT_DSD:
1753 case AUDIO_FORMAT_APTX:
1754 id = SND_AUDIOCODEC_BESPOKE;
1755 break;
1756 case AUDIO_FORMAT_MP2:
1757 id = SND_AUDIOCODEC_MP2;
1758 break;
1759 case AUDIO_FORMAT_AC3:
1760 id = SND_AUDIOCODEC_AC3;
1761 break;
1762 case AUDIO_FORMAT_E_AC3:
1763 case AUDIO_FORMAT_E_AC3_JOC:
1764 id = SND_AUDIOCODEC_EAC3;
1765 break;
1766 case AUDIO_FORMAT_DTS:
1767 case AUDIO_FORMAT_DTS_HD:
1768 id = SND_AUDIOCODEC_DTS;
1769 break;
1770 case AUDIO_FORMAT_DOLBY_TRUEHD:
1771 id = SND_AUDIOCODEC_TRUEHD;
1772 break;
1773 case AUDIO_FORMAT_IEC61937:
1774 id = SND_AUDIOCODEC_IEC61937;
1775 break;
1776 default:
1777 ALOGE("%s: Unsupported audio format :%x", __func__, format);
1778 }
1779
1780 return id;
1781}
1782#else
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301783int get_snd_codec_id(audio_format_t format)
1784{
1785 int id = 0;
1786
1787 switch (format & AUDIO_FORMAT_MAIN_MASK) {
1788 case AUDIO_FORMAT_MP3:
1789 id = SND_AUDIOCODEC_MP3;
1790 break;
1791 case AUDIO_FORMAT_AAC:
1792 id = SND_AUDIOCODEC_AAC;
1793 break;
1794 case AUDIO_FORMAT_AAC_ADTS:
1795 id = SND_AUDIOCODEC_AAC;
1796 break;
Arun Kumar Dasari3b174182016-12-27 13:01:14 +05301797 case AUDIO_FORMAT_AAC_LATM:
1798 id = SND_AUDIOCODEC_AAC;
1799 break;
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301800 case AUDIO_FORMAT_PCM:
1801 id = SND_AUDIOCODEC_PCM;
1802 break;
1803 case AUDIO_FORMAT_FLAC:
1804 id = SND_AUDIOCODEC_FLAC;
1805 break;
1806 case AUDIO_FORMAT_ALAC:
1807 id = SND_AUDIOCODEC_ALAC;
1808 break;
1809 case AUDIO_FORMAT_APE:
1810 id = SND_AUDIOCODEC_APE;
1811 break;
1812 case AUDIO_FORMAT_VORBIS:
1813 id = SND_AUDIOCODEC_VORBIS;
1814 break;
1815 case AUDIO_FORMAT_WMA:
1816 id = SND_AUDIOCODEC_WMA;
1817 break;
1818 case AUDIO_FORMAT_WMA_PRO:
1819 id = SND_AUDIOCODEC_WMA_PRO;
1820 break;
Satish Babu Patakokila0c313922016-12-08 12:07:08 +05301821 case AUDIO_FORMAT_MP2:
1822 id = SND_AUDIOCODEC_MP2;
1823 break;
Satish Babu Patakokila915ecba2017-01-10 17:43:56 +05301824 case AUDIO_FORMAT_AC3:
1825 id = SND_AUDIOCODEC_AC3;
1826 break;
1827 case AUDIO_FORMAT_E_AC3:
1828 case AUDIO_FORMAT_E_AC3_JOC:
1829 id = SND_AUDIOCODEC_EAC3;
1830 break;
1831 case AUDIO_FORMAT_DTS:
1832 case AUDIO_FORMAT_DTS_HD:
1833 id = SND_AUDIOCODEC_DTS;
1834 break;
Ben Romberger1aaaf862017-04-06 17:49:46 -07001835 case AUDIO_FORMAT_DOLBY_TRUEHD:
1836 id = SND_AUDIOCODEC_TRUEHD;
1837 break;
Naresh Tanniru928f0862017-04-07 16:44:23 -07001838 case AUDIO_FORMAT_IEC61937:
1839 id = SND_AUDIOCODEC_IEC61937;
1840 break;
Preetam Singh Ranawat4277a5a2017-01-18 19:02:24 +05301841 case AUDIO_FORMAT_DSD:
1842 id = SND_AUDIOCODEC_DSD;
1843 break;
Dhanalakshmi Siddani18737932016-11-29 17:33:17 +05301844 case AUDIO_FORMAT_APTX:
1845 id = SND_AUDIOCODEC_APTX;
1846 break;
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301847 default:
1848 ALOGE("%s: Unsupported audio format :%x", __func__, format);
1849 }
1850
1851 return id;
1852}
Xiaojun Sang782e5b12020-06-29 21:13:06 +08001853#endif
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301854
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001855void audio_extn_utils_send_audio_calibration(struct audio_device *adev,
1856 struct audio_usecase *usecase)
1857{
1858 int type = usecase->type;
1859
Dhananjay Kumar14245982017-01-16 20:21:00 +05301860 if (type == PCM_PLAYBACK && usecase->stream.out != NULL) {
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05301861 platform_send_audio_calibration(adev->platform, usecase,
Zhou Song06761dd2019-04-28 18:08:17 +08001862 usecase->stream.out->app_type_cfg.app_type);
Dhananjay Kumar14245982017-01-16 20:21:00 +05301863 } else if (type == PCM_CAPTURE && usecase->stream.in != NULL) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301864 platform_send_audio_calibration(adev->platform, usecase,
Zhou Song06761dd2019-04-28 18:08:17 +08001865 usecase->stream.in->app_type_cfg.app_type);
1866 } else if ((type == PCM_HFP_CALL) || (type == PCM_CAPTURE) ||
1867 (type == TRANSCODE_LOOPBACK_RX && usecase->stream.inout != NULL)) {
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05301868 platform_send_audio_calibration(adev->platform, usecase,
Zhou Song06761dd2019-04-28 18:08:17 +08001869 platform_get_default_app_type_v2(adev->platform, usecase->type));
Vidyakumar Athotae57f6002017-03-01 13:13:16 -08001870 } else {
1871 /* No need to send audio calibration for voice and voip call usecases */
1872 if ((type != VOICE_CALL) && (type != VOIP_CALL))
1873 ALOGW("%s: No audio calibration for usecase type = %d", __func__, type);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001874 }
1875}
1876
Ben Rombergera04fabc2014-11-14 12:16:03 -08001877// Base64 Encode and Decode
1878// Not all features supported. This must be used only with following conditions.
1879// Decode Modes: Support with and without padding
1880// CRLF not handling. So no CRLF in string to decode.
1881// Encode Modes: Supports only padding
1882int b64decode(char *inp, int ilen, uint8_t* outp)
1883{
1884 int i, j, k, ii, num;
1885 int rem, pcnt;
1886 uint32_t res=0;
1887 uint8_t getIndex[MAX_BASEINDEX_LEN];
1888 uint8_t tmp, cflag;
1889
1890 if(inp == NULL || outp == NULL || ilen <= 0) {
1891 ALOGE("[%s] received NULL pointer or zero length",__func__);
1892 return -1;
1893 }
1894
1895 memset(getIndex, MAX_BASEINDEX_LEN-1, sizeof(getIndex));
1896 for(i=0;i<BASE_TABLE_SIZE;i++) {
1897 getIndex[(uint8_t)bTable[i]] = (uint8_t)i;
1898 }
1899 getIndex[(uint8_t)'=']=0;
1900
1901 j=0;k=0;
1902 num = ilen/4;
1903 rem = ilen%4;
1904 if(rem==0)
1905 num = num-1;
1906 cflag=0;
1907 for(i=0; i<num; i++) {
1908 res=0;
1909 for(ii=0;ii<4;ii++) {
1910 res = res << 6;
1911 tmp = getIndex[(uint8_t)inp[j++]];
1912 res = res | tmp;
1913 cflag = cflag | tmp;
1914 }
1915 outp[k++] = (res >> 16)&0xFF;
1916 outp[k++] = (res >> 8)&0xFF;
1917 outp[k++] = res & 0xFF;
1918 }
1919
1920 // Handle last bytes special
1921 pcnt=0;
1922 if(rem == 0) {
1923 //With padding or full data
1924 res = 0;
1925 for(ii=0;ii<4;ii++) {
1926 if(inp[j] == '=')
1927 pcnt++;
1928 res = res << 6;
1929 tmp = getIndex[(uint8_t)inp[j++]];
1930 res = res | tmp;
1931 cflag = cflag | tmp;
1932 }
1933 outp[k++] = res >> 16;
1934 if(pcnt == 2)
1935 goto done;
1936 outp[k++] = (res>>8)&0xFF;
1937 if(pcnt == 1)
1938 goto done;
1939 outp[k++] = res&0xFF;
1940 } else {
1941 //without padding
1942 res = 0;
1943 for(i=0;i<rem;i++) {
1944 res = res << 6;
1945 tmp = getIndex[(uint8_t)inp[j++]];
1946 res = res | tmp;
1947 cflag = cflag | tmp;
1948 }
1949 for(i=rem;i<4;i++) {
1950 res = res << 6;
1951 pcnt++;
1952 }
1953 outp[k++] = res >> 16;
1954 if(pcnt == 2)
1955 goto done;
1956 outp[k++] = (res>>8)&0xFF;
1957 if(pcnt == 1)
1958 goto done;
1959 outp[k++] = res&0xFF;
1960 }
1961done:
1962 if(cflag == 0xFF) {
1963 ALOGE("[%s] base64 decode failed. Invalid character found %s",
1964 __func__, inp);
1965 return 0;
1966 }
1967 return k;
1968}
1969
1970int b64encode(uint8_t *inp, int ilen, char* outp)
1971{
1972 int i,j,k, num;
1973 int rem=0;
1974 uint32_t res=0;
1975
1976 if(inp == NULL || outp == NULL || ilen<=0) {
1977 ALOGE("[%s] received NULL pointer or zero input length",__func__);
1978 return -1;
1979 }
1980
1981 num = ilen/3;
1982 rem = ilen%3;
1983 j=0;k=0;
1984 for(i=0; i<num; i++) {
1985 //prepare index
1986 res = inp[j++]<<16;
1987 res = res | inp[j++]<<8;
1988 res = res | inp[j++];
1989 //get output map from index
1990 outp[k++] = (char) bTable[(res>>18)&0x3F];
1991 outp[k++] = (char) bTable[(res>>12)&0x3F];
1992 outp[k++] = (char) bTable[(res>>6)&0x3F];
1993 outp[k++] = (char) bTable[res&0x3F];
1994 }
1995
1996 switch(rem) {
1997 case 1:
1998 res = inp[j++]<<16;
1999 outp[k++] = (char) bTable[res>>18];
2000 outp[k++] = (char) bTable[(res>>12)&0x3F];
2001 //outp[k++] = '=';
2002 //outp[k++] = '=';
2003 break;
2004 case 2:
2005 res = inp[j++]<<16;
2006 res = res | inp[j++]<<8;
2007 outp[k++] = (char) bTable[res>>18];
2008 outp[k++] = (char) bTable[(res>>12)&0x3F];
2009 outp[k++] = (char) bTable[(res>>6)&0x3F];
2010 //outp[k++] = '=';
2011 break;
2012 default:
2013 break;
2014 }
Ben Rombergera04fabc2014-11-14 12:16:03 -08002015 outp[k] = '\0';
2016 return k;
2017}
Ashish Jain81eb2a82015-05-13 10:52:34 +05302018
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05302019
2020int audio_extn_utils_get_codec_version(const char *snd_card_name,
2021 int card_num,
2022 char *codec_version)
2023{
2024 char procfs_path[50];
2025 FILE *fp;
2026
2027 if (strstr(snd_card_name, "tasha")) {
2028 snprintf(procfs_path, sizeof(procfs_path),
2029 "/proc/asound/card%d/codecs/tasha/version", card_num);
2030 if ((fp = fopen(procfs_path, "r")) != NULL) {
2031 fgets(codec_version, CODEC_VERSION_MAX_LENGTH, fp);
2032 fclose(fp);
2033 } else {
2034 ALOGE("%s: ERROR. cannot open %s", __func__, procfs_path);
2035 return -ENOENT;
2036 }
2037 ALOGD("%s: codec version %s", __func__, codec_version);
2038 }
2039
2040 return 0;
2041}
2042
Preetam Singh Ranawat9d0d8e42019-07-15 12:27:25 +05302043int audio_extn_utils_get_codec_variant(int card_num,
2044 char *codec_variant)
2045{
2046 char procfs_path[50];
2047 FILE *fp;
2048 snprintf(procfs_path, sizeof(procfs_path),
2049 "/proc/asound/card%d/codecs/wcd938x/variant", card_num);
2050 if ((fp = fopen(procfs_path, "r")) == NULL) {
2051 snprintf(procfs_path, sizeof(procfs_path),
2052 "/proc/asound/card%d/codecs/wcd937x/variant", card_num);
2053 if ((fp = fopen(procfs_path, "r")) == NULL) {
2054 ALOGE("%s: ERROR. cannot open %s", __func__, procfs_path);
2055 return -ENOENT;
2056 }
2057 }
2058 fgets(codec_variant, CODEC_VARIANT_MAX_LENGTH, fp);
2059 fclose(fp);
2060 ALOGD("%s: codec variant is %s", __func__, codec_variant);
2061 return 0;
2062}
2063
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05302064
Ashish Jain81eb2a82015-05-13 10:52:34 +05302065#ifdef AUDIO_EXTERNAL_HDMI_ENABLED
2066
2067void get_default_compressed_channel_status(
2068 unsigned char *channel_status)
2069{
Ashish Jain81eb2a82015-05-13 10:52:34 +05302070 memset(channel_status,0,24);
2071
2072 /* block start bit in preamble bit 3 */
2073 channel_status[0] |= PROFESSIONAL;
2074 //compre out
2075 channel_status[0] |= NON_LPCM;
2076 // sample rate; fixed 48K for default/transcode
2077 channel_status[3] |= SR_48000;
2078}
2079
Ashish Jain81eb2a82015-05-13 10:52:34 +05302080int32_t get_compressed_channel_status(void *audio_stream_data,
2081 uint32_t audio_frame_size,
2082 unsigned char *channel_status,
2083 enum audio_parser_code_type codec_type)
2084 // codec_type - AUDIO_PARSER_CODEC_AC3
2085 // - AUDIO_PARSER_CODEC_DTS
2086{
2087 unsigned char *stream;
2088 int ret = 0;
2089 stream = (unsigned char *)audio_stream_data;
2090
2091 if (audio_stream_data == NULL || audio_frame_size == 0) {
2092 ALOGW("no buffer to get channel status, return default for compress");
2093 get_default_compressed_channel_status(channel_status);
2094 return ret;
2095 }
2096
2097 memset(channel_status,0,24);
2098 if(init_audio_parser(stream, audio_frame_size, codec_type) == -1)
2099 {
2100 ALOGE("init audio parser failed");
2101 return -1;
2102 }
2103 ret = get_channel_status(channel_status, codec_type);
2104 return ret;
2105
2106}
2107
Ashish Jain81eb2a82015-05-13 10:52:34 +05302108void get_lpcm_channel_status(uint32_t sampleRate,
2109 unsigned char *channel_status)
2110{
2111 int32_t status = 0;
Ashish Jain81eb2a82015-05-13 10:52:34 +05302112 memset(channel_status,0,24);
2113 /* block start bit in preamble bit 3 */
2114 channel_status[0] |= PROFESSIONAL;
2115 //LPCM OUT
2116 channel_status[0] &= ~NON_LPCM;
2117
2118 switch (sampleRate) {
2119 case 8000:
2120 case 11025:
2121 case 12000:
2122 case 16000:
2123 case 22050:
2124 channel_status[3] |= SR_NOTID;
Preetam Singh Ranawat61716b12015-12-14 11:55:24 +05302125 break;
Ashish Jain81eb2a82015-05-13 10:52:34 +05302126 case 24000:
2127 channel_status[3] |= SR_24000;
2128 break;
2129 case 32000:
2130 channel_status[3] |= SR_32000;
2131 break;
2132 case 44100:
2133 channel_status[3] |= SR_44100;
2134 break;
2135 case 48000:
2136 channel_status[3] |= SR_48000;
2137 break;
2138 case 88200:
2139 channel_status[3] |= SR_88200;
2140 break;
2141 case 96000:
2142 channel_status[3] |= SR_96000;
2143 break;
2144 case 176400:
2145 channel_status[3] |= SR_176400;
2146 break;
2147 case 192000:
2148 channel_status[3] |= SR_192000;
2149 break;
2150 default:
2151 ALOGV("Invalid sample_rate %u\n", sampleRate);
2152 status = -1;
2153 break;
2154 }
2155}
2156
2157void audio_utils_set_hdmi_channel_status(struct stream_out *out, char * buffer, size_t bytes)
2158{
2159 unsigned char channel_status[24]={0};
2160 struct snd_aes_iec958 iec958;
2161 const char *mixer_ctl_name = "IEC958 Playback PCM Stream";
2162 struct mixer_ctl *ctl;
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +05302163 ALOGV("%s: buffer %s bytes %zd", __func__, buffer, bytes);
Arun Mirpurie008ed22019-03-21 11:21:04 -07002164
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05302165 if (audio_extn_utils_is_dolby_format(out->format) &&
Ashish Jain81eb2a82015-05-13 10:52:34 +05302166 /*TODO:Extend code to support DTS passthrough*/
2167 /*set compressed channel status bits*/
Arun Mirpurie008ed22019-03-21 11:21:04 -07002168 audio_extn_passthru_is_passthrough_stream(out) &&
2169 audio_extn_is_hdmi_passthru_enabled()) {
Ashish Jain81eb2a82015-05-13 10:52:34 +05302170 get_compressed_channel_status(buffer, bytes, channel_status, AUDIO_PARSER_CODEC_AC3);
Arun Mirpurie008ed22019-03-21 11:21:04 -07002171 } else {
Ashish Jain81eb2a82015-05-13 10:52:34 +05302172 /*set channel status bit for LPCM*/
2173 get_lpcm_channel_status(out->sample_rate, channel_status);
2174 }
2175
2176 memcpy(iec958.status, channel_status,sizeof(iec958.status));
2177 ctl = mixer_get_ctl_by_name(out->dev->mixer, mixer_ctl_name);
2178 if (!ctl) {
2179 ALOGE("%s: Could not get ctl for mixer cmd - %s",
2180 __func__, mixer_ctl_name);
2181 return;
2182 }
2183 if (mixer_ctl_set_array(ctl, &iec958, sizeof(iec958)) < 0) {
2184 ALOGE("%s: Could not set channel status for ext HDMI ",
2185 __func__);
2186 return;
2187 }
2188
2189}
2190#endif
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302191
2192int audio_extn_utils_get_avt_device_drift(
2193 struct audio_usecase *usecase,
2194 struct audio_avt_device_drift_param *drift_param)
2195{
2196 int ret = 0, count = 0;
2197 char avt_device_drift_mixer_ctl_name[MIXER_PATH_MAX_LENGTH] = {0};
Naresh Tanniru6160c712017-04-17 15:43:48 +05302198 const char *backend = NULL;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302199 struct mixer_ctl *ctl = NULL;
2200 struct audio_avt_device_drift_stats drift_stats;
2201 struct audio_device *adev = NULL;
2202
2203 if (usecase != NULL && usecase->type == PCM_PLAYBACK) {
Naresh Tanniru6160c712017-04-17 15:43:48 +05302204 backend = platform_get_snd_device_backend_interface(usecase->out_snd_device);
2205 if (!backend) {
2206 ALOGE("%s: Unsupported device %d", __func__,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08002207 get_device_types(&usecase->stream.out->device_list));
Naresh Tanniru6160c712017-04-17 15:43:48 +05302208 ret = -EINVAL;
2209 goto done;
2210 }
2211 strlcpy(avt_device_drift_mixer_ctl_name,
2212 backend,
2213 MIXER_PATH_MAX_LENGTH);
2214
2215 count = strlen(backend);
2216 if (MIXER_PATH_MAX_LENGTH - count > 0) {
2217 strlcat(&avt_device_drift_mixer_ctl_name[count],
2218 " DRIFT",
2219 MIXER_PATH_MAX_LENGTH - count);
2220 } else {
2221 ret = -EINVAL;
2222 goto done;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302223 }
2224 } else {
Naresh Tanniru7586e292017-04-13 10:38:13 +05302225 ALOGE("%s: Invalid usecase",__func__);
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302226 ret = -EINVAL;
Naresh Tanniru6160c712017-04-17 15:43:48 +05302227 goto done;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302228 }
2229
Naresh Tanniru6160c712017-04-17 15:43:48 +05302230 adev = usecase->stream.out->dev;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302231 ctl = mixer_get_ctl_by_name(adev->mixer, avt_device_drift_mixer_ctl_name);
2232 if (!ctl) {
2233 ALOGE("%s: Could not get ctl for mixer cmd - %s",
2234 __func__, avt_device_drift_mixer_ctl_name);
2235
2236 ret = -EINVAL;
2237 goto done;
2238 }
2239
2240 ALOGV("%s: Getting AV Timer vs Device Drift mixer ctrl name %s", __func__,
2241 avt_device_drift_mixer_ctl_name);
2242
2243 mixer_ctl_update(ctl);
2244 count = mixer_ctl_get_num_values(ctl);
2245 if (count != sizeof(struct audio_avt_device_drift_stats)) {
2246 ALOGE("%s: mixer_ctl_get_num_values() invalid drift_stats data size",
2247 __func__);
2248
2249 ret = -EINVAL;
2250 goto done;
2251 }
2252
2253 ret = mixer_ctl_get_array(ctl, (void *)&drift_stats, count);
2254 if (ret != 0) {
2255 ALOGE("%s: mixer_ctl_get_array() failed to get drift_stats Params",
2256 __func__);
2257
2258 ret = -EINVAL;
2259 goto done;
2260 }
2261 memcpy(drift_param, &drift_stats.drift_param,
2262 sizeof(struct audio_avt_device_drift_param));
2263done:
2264 return ret;
2265}
Manish Dewangan07de2142017-02-27 19:27:20 +05302266
2267#ifdef SNDRV_COMPRESS_PATH_DELAY
2268int audio_extn_utils_compress_get_dsp_latency(struct stream_out *out)
2269{
2270 int ret = -EINVAL;
2271 struct snd_compr_metadata metadata;
2272 int delay_ms = COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
2273
Weiyin Jiangc49a3b52018-08-17 16:16:08 +08002274 /* override the latency for pcm offload use case */
2275 if ((out->flags & AUDIO_OUTPUT_FLAG_DIRECT) &&
2276 !(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)) {
2277 delay_ms = PCM_OFFLOAD_PLAYBACK_LATENCY;
2278 }
2279
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -07002280 if (property_get_bool("vendor.audio.playback.dsp.pathdelay", false)) {
Manish Dewangan07de2142017-02-27 19:27:20 +05302281 ALOGD("%s:: Quering DSP delay %d",__func__, __LINE__);
2282 if (!(is_offload_usecase(out->usecase))) {
2283 ALOGE("%s:: not supported for non offload session", __func__);
2284 goto exit;
2285 }
2286
2287 if (!out->compr) {
2288 ALOGD("%s:: Invalid compress handle,returning default dsp latency",
2289 __func__);
2290 goto exit;
2291 }
2292
2293 metadata.key = SNDRV_COMPRESS_PATH_DELAY;
2294 ret = compress_get_metadata(out->compr, &metadata);
2295 if(ret) {
2296 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2297 goto exit;
2298 }
2299 delay_ms = metadata.value[0] / 1000; /*convert to ms*/
2300 } else {
2301 ALOGD("%s:: Using Fix DSP delay",__func__);
2302 }
2303
2304exit:
2305 ALOGD("%s:: delay in ms is %d",__func__, delay_ms);
2306 return delay_ms;
2307}
2308#else
2309int audio_extn_utils_compress_get_dsp_latency(struct stream_out *out __unused)
2310{
2311 return COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
2312}
2313#endif
Manish Dewangan69426c82017-01-30 17:35:36 +05302314
2315#ifdef SNDRV_COMPRESS_RENDER_MODE
2316int audio_extn_utils_compress_set_render_mode(struct stream_out *out)
2317{
2318 struct snd_compr_metadata metadata;
2319 int ret = -EINVAL;
2320
2321 if (!(is_offload_usecase(out->usecase))) {
2322 ALOGE("%s:: not supported for non offload session", __func__);
2323 goto exit;
2324 }
2325
2326 if (!out->compr) {
2327 ALOGD("%s:: Invalid compress handle",
2328 __func__);
2329 goto exit;
2330 }
2331
2332 ALOGD("%s:: render mode %d", __func__, out->render_mode);
2333
2334 metadata.key = SNDRV_COMPRESS_RENDER_MODE;
2335 if (out->render_mode == RENDER_MODE_AUDIO_MASTER) {
2336 metadata.value[0] = SNDRV_COMPRESS_RENDER_MODE_AUDIO_MASTER;
2337 } else if (out->render_mode == RENDER_MODE_AUDIO_STC_MASTER) {
2338 metadata.value[0] = SNDRV_COMPRESS_RENDER_MODE_STC_MASTER;
2339 } else {
2340 ret = 0;
2341 goto exit;
2342 }
2343 ret = compress_set_metadata(out->compr, &metadata);
2344 if(ret) {
2345 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2346 }
2347exit:
2348 return ret;
2349}
2350#else
2351int audio_extn_utils_compress_set_render_mode(struct stream_out *out __unused)
2352{
2353 ALOGD("%s:: configuring render mode not supported", __func__);
2354 return 0;
2355}
2356#endif
Manish Dewangan58229382017-02-02 15:48:41 +05302357
2358#ifdef SNDRV_COMPRESS_CLK_REC_MODE
2359int audio_extn_utils_compress_set_clk_rec_mode(
2360 struct audio_usecase *usecase)
2361{
2362 struct snd_compr_metadata metadata;
2363 struct stream_out *out = NULL;
2364 int ret = -EINVAL;
2365
Naresh Tanniru7586e292017-04-13 10:38:13 +05302366 if (usecase == NULL || usecase->type != PCM_PLAYBACK) {
Manish Dewangan58229382017-02-02 15:48:41 +05302367 ALOGE("%s:: Invalid use case", __func__);
2368 goto exit;
2369 }
2370
2371 out = usecase->stream.out;
2372 if (!out) {
2373 ALOGE("%s:: invalid stream", __func__);
2374 goto exit;
2375 }
2376
2377 if (!is_offload_usecase(out->usecase)) {
2378 ALOGE("%s:: not supported for non offload session", __func__);
2379 goto exit;
2380 }
2381
2382 if (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER) {
2383 ALOGD("%s:: clk recovery is only supported in STC render mode",
2384 __func__);
2385 ret = 0;
2386 goto exit;
2387 }
2388
2389 if (!out->compr) {
2390 ALOGD("%s:: Invalid compress handle",
2391 __func__);
2392 goto exit;
2393 }
2394 metadata.key = SNDRV_COMPRESS_CLK_REC_MODE;
2395 switch(usecase->out_snd_device) {
2396 case SND_DEVICE_OUT_HDMI:
2397 case SND_DEVICE_OUT_SPEAKER_AND_HDMI:
2398 case SND_DEVICE_OUT_DISPLAY_PORT:
2399 case SND_DEVICE_OUT_SPEAKER_AND_DISPLAY_PORT:
2400 metadata.value[0] = SNDRV_COMPRESS_CLK_REC_MODE_NONE;
2401 break;
2402 default:
2403 metadata.value[0] = SNDRV_COMPRESS_CLK_REC_MODE_AUTO;
2404 break;
2405 }
2406
2407 ALOGD("%s:: clk recovery mode %d",__func__, metadata.value[0]);
2408
2409 ret = compress_set_metadata(out->compr, &metadata);
2410 if(ret) {
2411 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2412 }
2413
2414exit:
2415 return ret;
2416}
2417#else
2418int audio_extn_utils_compress_set_clk_rec_mode(
2419 struct audio_usecase *usecase __unused)
2420{
2421 ALOGD("%s:: configuring render mode not supported", __func__);
2422 return 0;
2423}
2424#endif
Manish Dewangan27346042017-03-01 12:56:12 +05302425
2426#ifdef SNDRV_COMPRESS_RENDER_WINDOW
2427int audio_extn_utils_compress_set_render_window(
2428 struct stream_out *out,
2429 struct audio_out_render_window_param *render_window)
2430{
2431 struct snd_compr_metadata metadata;
2432 int ret = -EINVAL;
2433
Manish Dewangan27346042017-03-01 12:56:12 +05302434 if(render_window == NULL) {
2435 ALOGE("%s:: Invalid render_window", __func__);
2436 goto exit;
2437 }
2438
Aniket Kumar Lata1e1d3662017-06-01 18:45:48 -07002439 ALOGD("%s:: render window start 0x%"PRIx64" end 0x%"PRIx64"",
2440 __func__,render_window->render_ws, render_window->render_we);
2441
Manish Dewangan27346042017-03-01 12:56:12 +05302442 if (!is_offload_usecase(out->usecase)) {
2443 ALOGE("%s:: not supported for non offload session", __func__);
2444 goto exit;
2445 }
2446
Naresh Tanniru6160c712017-04-17 15:43:48 +05302447 if ((out->render_mode != RENDER_MODE_AUDIO_MASTER) &&
2448 (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER)) {
Manish Dewangan27346042017-03-01 12:56:12 +05302449 ALOGD("%s:: only supported in timestamp mode, current "
2450 "render mode mode %d", __func__, out->render_mode);
2451 goto exit;
2452 }
2453
2454 if (!out->compr) {
2455 ALOGW("%s:: offload session not yet opened,"
2456 "render window will be configure later", __func__);
2457 /* store render window to reconfigure in start_output_stream() */
2458 goto exit;
2459 }
2460
2461 metadata.key = SNDRV_COMPRESS_RENDER_WINDOW;
2462 /*render window start value */
2463 metadata.value[0] = 0xFFFFFFFF & render_window->render_ws; /* lsb */
2464 metadata.value[1] = \
2465 (0xFFFFFFFF00000000 & render_window->render_ws) >> 32; /* msb*/
2466 /*render window end value */
2467 metadata.value[2] = 0xFFFFFFFF & render_window->render_we; /* lsb */
2468 metadata.value[3] = \
2469 (0xFFFFFFFF00000000 & render_window->render_we) >> 32; /* msb*/
2470
2471 ret = compress_set_metadata(out->compr, &metadata);
2472 if(ret) {
2473 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2474 }
2475
2476exit:
2477 return ret;
2478}
2479#else
2480int audio_extn_utils_compress_set_render_window(
2481 struct stream_out *out __unused,
2482 struct audio_out_render_window_param *render_window __unused)
2483{
2484 ALOGD("%s:: configuring render window not supported", __func__);
2485 return 0;
2486}
2487#endif
Manish Dewangan14956cc2017-02-14 18:54:42 +05302488
2489#ifdef SNDRV_COMPRESS_START_DELAY
2490int audio_extn_utils_compress_set_start_delay(
2491 struct stream_out *out,
2492 struct audio_out_start_delay_param *delay_param)
2493{
2494 struct snd_compr_metadata metadata;
2495 int ret = -EINVAL;
2496
2497 if(delay_param == NULL) {
2498 ALOGE("%s:: Invalid delay_param", __func__);
2499 goto exit;
2500 }
2501
2502 ALOGD("%s:: render start delay 0x%"PRIx64" ", __func__,
2503 delay_param->start_delay);
2504
2505 if (!is_offload_usecase(out->usecase)) {
2506 ALOGE("%s:: not supported for non offload session", __func__);
2507 goto exit;
2508 }
2509
Naresh Tanniru6160c712017-04-17 15:43:48 +05302510 if ((out->render_mode != RENDER_MODE_AUDIO_MASTER) &&
2511 (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER)) {
Manish Dewangan14956cc2017-02-14 18:54:42 +05302512 ALOGD("%s:: only supported in timestamp mode, current "
2513 "render mode mode %d", __func__, out->render_mode);
2514 goto exit;
2515 }
2516
2517 if (!out->compr) {
2518 ALOGW("%s:: offload session not yet opened,"
2519 "start delay will be configure later", __func__);
2520 goto exit;
2521 }
2522
2523 metadata.key = SNDRV_COMPRESS_START_DELAY;
2524 metadata.value[0] = 0xFFFFFFFF & delay_param->start_delay; /* lsb */
2525 metadata.value[1] = \
2526 (0xFFFFFFFF00000000 & delay_param->start_delay) >> 32; /* msb*/
2527
2528 ret = compress_set_metadata(out->compr, &metadata);
2529 if(ret) {
2530 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2531 }
2532
2533exit:
2534 return ret;
2535}
2536#else
2537int audio_extn_utils_compress_set_start_delay(
2538 struct stream_out *out __unused,
2539 struct audio_out_start_delay_param *delay_param __unused)
2540{
2541 ALOGD("%s:: configuring render window not supported", __func__);
2542 return 0;
2543}
2544#endif
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002545
Surendar Karka287348c2019-04-10 18:31:46 +05302546#ifdef SNDRV_COMPRESS_DSP_POSITION
2547int audio_extn_utils_compress_get_dsp_presentation_pos(struct stream_out *out,
2548 uint64_t *frames, struct timespec *timestamp, int32_t clock_id)
2549{
2550 int ret = -EINVAL;
2551 uint64_t *val = NULL;
2552 uint64_t time = 0;
2553 struct snd_compr_metadata metadata;
2554
2555 ALOGV("%s:: Quering DSP position with clock id %d",__func__, clock_id);
2556 metadata.key = SNDRV_COMPRESS_DSP_POSITION;
2557 metadata.value[0] = clock_id;
2558 ret = compress_get_metadata(out->compr, &metadata);
2559 if (ret) {
2560 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2561 ret = -errno;
2562 goto exit;
2563 }
2564 val = (uint64_t *)&metadata.value[1];
2565 *frames = *val;
2566 time = *(val + 1);
2567 timestamp->tv_sec = time / 1000000;
2568 timestamp->tv_nsec = (time % 1000000)*1000;
2569
2570exit:
2571 return ret;
2572}
2573#else
2574int audio_extn_utils_compress_get_dsp_presentation_pos(struct stream_out *out __unused,
2575 uint64_t *frames __unused, struct timespec *timestamp __unused,
2576 int32_t clock_id __unused)
2577{
2578 ALOGD("%s:: dsp presentation position not supported", __func__);
2579 return 0;
2580
2581}
2582#endif
2583
2584#ifdef SNDRV_PCM_IOCTL_DSP_POSITION
2585int audio_extn_utils_pcm_get_dsp_presentation_pos(struct stream_out *out,
2586 uint64_t *frames, struct timespec *timestamp, int32_t clock_id)
2587{
2588 int ret = -EINVAL;
2589 uint64_t time = 0;
2590 struct snd_pcm_prsnt_position prsnt_position;
2591
2592 ALOGV("%s:: Quering DSP position with clock id %d",__func__, clock_id);
2593 prsnt_position.clock_id = clock_id;
2594 ret = pcm_ioctl(out->pcm, SNDRV_PCM_IOCTL_DSP_POSITION, &prsnt_position);
2595 if (ret) {
2596 ALOGE("%s::error %d", __func__, ret);
2597 ret = -EIO;
2598 goto exit;
2599 }
2600
2601 *frames = prsnt_position.frames;
2602 time = prsnt_position.timestamp;
2603 timestamp->tv_sec = time / 1000000;
2604 timestamp->tv_nsec = (time % 1000000)*1000;
2605
2606exit:
2607 return ret;
2608}
2609#else
2610int audio_extn_utils_pcm_get_dsp_presentation_pos(struct stream_out *out __unused,
2611 uint64_t *frames __unused, struct timespec *timestamp __unused,
2612 int32_t clock_id __unused)
2613{
2614 ALOGD("%s:: dsp presentation position not supported", __func__);
2615 return 0;
2616
2617}
2618#endif
2619
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002620#define MAX_SND_CARD 8
Ashish Jain30ae8942017-11-05 17:21:23 +05302621#define RETRY_US 1000000
2622#define RETRY_NUMBER 40
Aalique Grahame22e49102018-12-18 14:23:57 -08002623#define PLATFORM_INFO_XML_PATH "audio_platform_info.xml"
2624#define PLATFORM_INFO_XML_BASE_STRING "audio_platform_info"
2625
Aalique Grahame22e49102018-12-18 14:23:57 -08002626bool audio_extn_utils_resolve_config_file(char file_name[MIXER_PATH_MAX_LENGTH])
2627{
2628 char full_config_path[MIXER_PATH_MAX_LENGTH];
Saurav Kumarf5d2c342020-07-29 19:31:49 +05302629 char vendor_config_path[VENDOR_CONFIG_PATH_MAX_LENGTH];
2630
2631 /* Get path for audio configuration files in vendor */
2632 audio_get_vendor_config_path(vendor_config_path,
2633 sizeof(vendor_config_path));
2634 snprintf(full_config_path, sizeof(full_config_path),
2635 "%s/%s", vendor_config_path, file_name);
2636 if (F_OK == access(full_config_path, 0)) {
2637 strlcpy(file_name, full_config_path, MIXER_PATH_MAX_LENGTH);
2638 return true;
Aalique Grahame22e49102018-12-18 14:23:57 -08002639 }
2640 return false;
2641}
2642
2643/* platform_info_file should be size 'MIXER_PATH_MAX_LENGTH' */
2644int audio_extn_utils_get_platform_info(const char* snd_card_name, char* platform_info_file)
2645{
2646 if (NULL == snd_card_name) {
2647 return -1;
2648 }
2649
2650 struct snd_card_split *snd_split_handle = NULL;
2651 int ret = 0;
2652 audio_extn_set_snd_card_split(snd_card_name);
2653 snd_split_handle = audio_extn_get_snd_card_split();
2654
2655 snprintf(platform_info_file, MIXER_PATH_MAX_LENGTH, "%s_%s_%s.xml",
2656 PLATFORM_INFO_XML_BASE_STRING, snd_split_handle->snd_card,
2657 snd_split_handle->form_factor);
2658
2659 if (!audio_extn_utils_resolve_config_file(platform_info_file)) {
2660 memset(platform_info_file, 0, MIXER_PATH_MAX_LENGTH);
2661 snprintf(platform_info_file, MIXER_PATH_MAX_LENGTH, "%s_%s.xml",
2662 PLATFORM_INFO_XML_BASE_STRING, snd_split_handle->snd_card);
2663
2664 if (!audio_extn_utils_resolve_config_file(platform_info_file)) {
2665 memset(platform_info_file, 0, MIXER_PATH_MAX_LENGTH);
2666 strlcpy(platform_info_file, PLATFORM_INFO_XML_PATH, MIXER_PATH_MAX_LENGTH);
2667 ret = audio_extn_utils_resolve_config_file(platform_info_file) ? 0 : -1;
2668 }
2669 }
2670
2671 return ret;
2672}
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002673
2674int audio_extn_utils_get_snd_card_num()
2675{
Soumya Managoli9fee7c62018-04-06 16:21:50 +05302676 int snd_card_num = 0;
2677 struct mixer *mixer = NULL;
2678
2679 snd_card_num = audio_extn_utils_open_snd_mixer(&mixer);
2680 if (mixer)
2681 mixer_close(mixer);
2682 return snd_card_num;
2683}
2684
2685int audio_extn_utils_open_snd_mixer(struct mixer **mixer_handle)
2686{
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002687
2688 void *hw_info = NULL;
2689 struct mixer *mixer = NULL;
2690 int retry_num = 0;
mpangfaa7bae2019-01-15 16:38:13 +08002691 int snd_card_num = 0;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002692 char* snd_card_name = NULL;
mpangfaa7bae2019-01-15 16:38:13 +08002693 int snd_card_detection_info[MAX_SND_CARD] = {0};
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002694
Soumya Managoli9fee7c62018-04-06 16:21:50 +05302695 if (!mixer_handle) {
2696 ALOGE("invalid mixer handle");
2697 return -1;
2698 }
2699 *mixer_handle = NULL;
Ashish Jain30ae8942017-11-05 17:21:23 +05302700 /*
mpangfaa7bae2019-01-15 16:38:13 +08002701 * Try with all the sound cards ( 0 to 7 ) and if none of them were detected
Ashish Jain30ae8942017-11-05 17:21:23 +05302702 * sleep for 1 sec and try detections with sound card 0 again.
2703 * If sound card gets detected, check if it is relevant, if not check with the
2704 * other sound cards. To ensure that the irrelevant sound card is not check again,
2705 * we maintain it in min_snd_card_num.
2706 */
2707 while (retry_num < RETRY_NUMBER) {
mpangfaa7bae2019-01-15 16:38:13 +08002708 snd_card_num = 0;
2709 while (snd_card_num < MAX_SND_CARD) {
2710 if (snd_card_detection_info[snd_card_num] == 0) {
2711 mixer = mixer_open(snd_card_num);
2712 if (!mixer)
2713 snd_card_num++;
2714 else
2715 break;
2716 } else
2717 snd_card_num++;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002718 }
2719
2720 if (!mixer) {
Ashish Jain30ae8942017-11-05 17:21:23 +05302721 usleep(RETRY_US);
Ashish Jain30ae8942017-11-05 17:21:23 +05302722 retry_num++;
mpangfaa7bae2019-01-15 16:38:13 +08002723 ALOGD("%s: retry, retry_num %d", __func__, retry_num);
Ashish Jain30ae8942017-11-05 17:21:23 +05302724 continue;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002725 }
2726
2727 snd_card_name = strdup(mixer_get_name(mixer));
2728 if (!snd_card_name) {
2729 ALOGE("failed to allocate memory for snd_card_name\n");
2730 mixer_close(mixer);
2731 return -1;
2732 }
2733 ALOGD("%s: snd_card_name: %s", __func__, snd_card_name);
mpangfaa7bae2019-01-15 16:38:13 +08002734 snd_card_detection_info[snd_card_num] = 1;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002735 hw_info = hw_info_init(snd_card_name);
2736 if (hw_info) {
2737 ALOGD("%s: Opened sound card:%d", __func__, snd_card_num);
2738 break;
2739 }
mpangfaa7bae2019-01-15 16:38:13 +08002740 ALOGE("%s: Failed to init hardware info, snd_card_num:%d", __func__, snd_card_num);
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002741
Dhananjay Kumara7e27832017-07-11 15:40:39 +05302742 free(snd_card_name);
2743 snd_card_name = NULL;
2744
2745 mixer_close(mixer);
2746 mixer = NULL;
2747 }
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002748 if (snd_card_name)
2749 free(snd_card_name);
Ashish Jain30ae8942017-11-05 17:21:23 +05302750
Dhananjay Kumara7e27832017-07-11 15:40:39 +05302751 if (hw_info)
2752 hw_info_deinit(hw_info);
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002753
mpangfaa7bae2019-01-15 16:38:13 +08002754 if (retry_num >= RETRY_NUMBER) {
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002755 ALOGE("%s: Unable to find correct sound card, aborting.", __func__);
2756 return -1;
2757 }
2758
Soumya Managoli9fee7c62018-04-06 16:21:50 +05302759 if (mixer)
2760 *mixer_handle = mixer;
2761
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002762 return snd_card_num;
2763}
Naresh Tanniru6160c712017-04-17 15:43:48 +05302764
Soumya Managoli9fee7c62018-04-06 16:21:50 +05302765void audio_extn_utils_close_snd_mixer(struct mixer *mixer)
2766{
2767 if (mixer)
2768 mixer_close(mixer);
2769}
2770
Naresh Tanniru6160c712017-04-17 15:43:48 +05302771#ifdef SNDRV_COMPRESS_ENABLE_ADJUST_SESSION_CLOCK
2772int audio_extn_utils_compress_enable_drift_correction(
2773 struct stream_out *out,
2774 struct audio_out_enable_drift_correction *drift)
2775{
2776 struct snd_compr_metadata metadata;
2777 int ret = -EINVAL;
2778
2779 if(drift == NULL) {
2780 ALOGE("%s:: Invalid param", __func__);
2781 goto exit;
2782 }
2783
2784 ALOGD("%s:: drift enable %d", __func__,drift->enable);
2785
2786 if (!is_offload_usecase(out->usecase)) {
2787 ALOGE("%s:: not supported for non offload session", __func__);
2788 goto exit;
2789 }
2790
2791 if (!out->compr) {
2792 ALOGW("%s:: offload session not yet opened,"
2793 "start delay will be configure later", __func__);
2794 goto exit;
2795 }
2796
2797 metadata.key = SNDRV_COMPRESS_ENABLE_ADJUST_SESSION_CLOCK;
2798 metadata.value[0] = drift->enable;
2799 out->drift_correction_enabled = drift->enable;
2800
2801 ret = compress_set_metadata(out->compr, &metadata);
2802 if(ret) {
2803 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2804 out->drift_correction_enabled = false;
2805 }
2806
2807exit:
2808 return ret;
2809}
2810#else
2811int audio_extn_utils_compress_enable_drift_correction(
2812 struct stream_out *out __unused,
2813 struct audio_out_enable_drift_correction *drift __unused)
2814{
2815 ALOGD("%s:: configuring drift enablement not supported", __func__);
2816 return 0;
2817}
2818#endif
2819
2820#ifdef SNDRV_COMPRESS_ADJUST_SESSION_CLOCK
2821int audio_extn_utils_compress_correct_drift(
2822 struct stream_out *out,
2823 struct audio_out_correct_drift *drift_param)
2824{
2825 struct snd_compr_metadata metadata;
2826 int ret = -EINVAL;
2827
2828 if (drift_param == NULL) {
2829 ALOGE("%s:: Invalid drift_param", __func__);
2830 goto exit;
2831 }
2832
2833 ALOGD("%s:: adjust time 0x%"PRIx64" ", __func__,
2834 drift_param->adjust_time);
2835
2836 if (!is_offload_usecase(out->usecase)) {
2837 ALOGE("%s:: not supported for non offload session", __func__);
2838 goto exit;
2839 }
2840
2841 if (!out->compr) {
2842 ALOGW("%s:: offload session not yet opened", __func__);
2843 goto exit;
2844 }
2845
2846 if (!out->drift_correction_enabled) {
2847 ALOGE("%s:: drift correction not enabled", __func__);
2848 goto exit;
2849 }
2850
2851 metadata.key = SNDRV_COMPRESS_ADJUST_SESSION_CLOCK;
2852 metadata.value[0] = 0xFFFFFFFF & drift_param->adjust_time; /* lsb */
2853 metadata.value[1] = \
2854 (0xFFFFFFFF00000000 & drift_param->adjust_time) >> 32; /* msb*/
2855
2856 ret = compress_set_metadata(out->compr, &metadata);
2857 if(ret)
2858 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2859exit:
2860 return ret;
2861}
2862#else
2863int audio_extn_utils_compress_correct_drift(
2864 struct stream_out *out __unused,
2865 struct audio_out_correct_drift *drift_param __unused)
2866{
2867 ALOGD("%s:: setting adjust clock not supported", __func__);
2868 return 0;
2869}
2870#endif
Naresh Tanniru29bce4e2017-04-27 17:54:30 +05302871
2872int audio_extn_utils_set_channel_map(
2873 struct stream_out *out,
2874 struct audio_out_channel_map_param *channel_map_param)
2875{
2876 int ret = -EINVAL, i = 0;
2877 int channels = audio_channel_count_from_out_mask(out->channel_mask);
2878
2879 if (channel_map_param == NULL) {
2880 ALOGE("%s:: Invalid channel_map", __func__);
2881 goto exit;
2882 }
2883
2884 if (channel_map_param->channels != channels) {
2885 ALOGE("%s:: Channels(%d) does not match stream channels(%d)",
2886 __func__, channel_map_param->channels, channels);
2887 goto exit;
2888 }
2889
2890 for ( i = 0; i < channels; i++) {
2891 ALOGV("%s:: channel_map[%d]- %d", __func__, i, channel_map_param->channel_map[i]);
2892 out->channel_map_param.channel_map[i] = channel_map_param->channel_map[i];
2893 }
2894 ret = 0;
2895exit:
2896 return ret;
2897}
Varun Balaraje49253e2017-07-06 19:48:56 +05302898
2899int audio_extn_utils_set_pan_scale_params(
2900 struct stream_out *out,
2901 struct mix_matrix_params *mm_params)
2902{
2903 int ret = -EINVAL, i = 0, j = 0;
2904
Varun Balaraj003ee752017-08-07 17:54:55 +05302905 if (mm_params == NULL || out == NULL) {
2906 ALOGE("%s:: Invalid mix matrix or out param", __func__);
Varun Balaraje49253e2017-07-06 19:48:56 +05302907 goto exit;
2908 }
2909
2910 if (mm_params->num_output_channels > MAX_CHANNELS_SUPPORTED ||
2911 mm_params->num_output_channels <= 0 ||
2912 mm_params->num_input_channels > MAX_CHANNELS_SUPPORTED ||
2913 mm_params->num_input_channels <= 0)
2914 goto exit;
2915
2916 out->pan_scale_params.num_output_channels = mm_params->num_output_channels;
2917 out->pan_scale_params.num_input_channels = mm_params->num_input_channels;
2918 out->pan_scale_params.has_output_channel_map =
2919 mm_params->has_output_channel_map;
2920 for (i = 0; i < mm_params->num_output_channels; i++)
2921 out->pan_scale_params.output_channel_map[i] =
2922 mm_params->output_channel_map[i];
2923
2924 out->pan_scale_params.has_input_channel_map =
2925 mm_params->has_input_channel_map;
2926 for (i = 0; i < mm_params->num_input_channels; i++)
2927 out->pan_scale_params.input_channel_map[i] =
2928 mm_params->input_channel_map[i];
2929
2930 out->pan_scale_params.has_mixer_coeffs = mm_params->has_mixer_coeffs;
2931 for (i = 0; i < mm_params->num_output_channels; i++)
2932 for (j = 0; j < mm_params->num_input_channels; j++) {
2933 //Convert the channel coefficient gains in Q14 format
2934 out->pan_scale_params.mixer_coeffs[i][j] =
2935 mm_params->mixer_coeffs[i][j] * (2 << 13);
2936 }
2937
2938 ret = platform_set_stream_pan_scale_params(out->dev->platform,
2939 out->pcm_device_id,
2940 out->pan_scale_params);
2941
2942exit:
2943 return ret;
2944}
2945
2946int audio_extn_utils_set_downmix_params(
2947 struct stream_out *out,
2948 struct mix_matrix_params *mm_params)
2949{
2950 int ret = -EINVAL, i = 0, j = 0;
2951 struct audio_usecase *usecase = NULL;
2952
Aniket Kumar Lataf9f246e2017-09-15 15:20:16 -07002953 if (mm_params == NULL || out == NULL) {
Varun Balaraj003ee752017-08-07 17:54:55 +05302954 ALOGE("%s:: Invalid mix matrix or out param", __func__);
Varun Balaraje49253e2017-07-06 19:48:56 +05302955 goto exit;
2956 }
2957
2958 if (mm_params->num_output_channels > MAX_CHANNELS_SUPPORTED ||
2959 mm_params->num_output_channels <= 0 ||
2960 mm_params->num_input_channels > MAX_CHANNELS_SUPPORTED ||
2961 mm_params->num_input_channels <= 0)
2962 goto exit;
2963
2964 usecase = get_usecase_from_list(out->dev, out->usecase);
Varun Balaraj003ee752017-08-07 17:54:55 +05302965 if (!usecase) {
2966 ALOGE("%s: Get usecase list failed!", __func__);
Aniket Kumar Lataf9f246e2017-09-15 15:20:16 -07002967 goto exit;
2968 }
Varun Balaraje49253e2017-07-06 19:48:56 +05302969 out->downmix_params.num_output_channels = mm_params->num_output_channels;
2970 out->downmix_params.num_input_channels = mm_params->num_input_channels;
2971
2972 out->downmix_params.has_output_channel_map =
2973 mm_params->has_output_channel_map;
2974 for (i = 0; i < mm_params->num_output_channels; i++) {
2975 out->downmix_params.output_channel_map[i] =
2976 mm_params->output_channel_map[i];
2977 }
2978
2979 out->downmix_params.has_input_channel_map =
2980 mm_params->has_input_channel_map;
2981 for (i = 0; i < mm_params->num_input_channels; i++)
2982 out->downmix_params.input_channel_map[i] =
2983 mm_params->input_channel_map[i];
2984
2985 out->downmix_params.has_mixer_coeffs = mm_params->has_mixer_coeffs;
2986 for (i = 0; i < mm_params->num_output_channels; i++)
Nikhil Latukar2e6f6242017-08-15 13:37:07 +05302987 for (j = 0; j < mm_params->num_input_channels; j++) {
2988 //Convert the channel coefficient gains in Q14 format
Varun Balaraje49253e2017-07-06 19:48:56 +05302989 out->downmix_params.mixer_coeffs[i][j] =
Nikhil Latukar2e6f6242017-08-15 13:37:07 +05302990 mm_params->mixer_coeffs[i][j] * (2 << 13);
2991 }
Varun Balaraje49253e2017-07-06 19:48:56 +05302992
2993 ret = platform_set_stream_downmix_params(out->dev->platform,
2994 out->pcm_device_id,
2995 usecase->out_snd_device,
2996 out->downmix_params);
2997
2998exit:
2999 return ret;
3000}
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05303001
3002bool audio_extn_utils_is_dolby_format(audio_format_t format)
3003{
3004 if (format == AUDIO_FORMAT_AC3 ||
3005 format == AUDIO_FORMAT_E_AC3 ||
3006 format == AUDIO_FORMAT_E_AC3_JOC)
3007 return true;
3008 else
3009 return false;
3010}
3011
`Deeraj Soman676c2702017-09-18 19:25:53 +05303012int audio_extn_utils_get_bit_width_from_string(const char *id_string)
3013{
3014 int i;
3015 const mixer_config_lookup mixer_bitwidth_config[] = {{"S24_3LE", 24},
3016 {"S32_LE", 32},
3017 {"S24_LE", 24},
3018 {"S16_LE", 16}};
3019 int num_configs = sizeof(mixer_bitwidth_config) / sizeof(mixer_bitwidth_config[0]);
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05303020
`Deeraj Soman676c2702017-09-18 19:25:53 +05303021 for (i = 0; i < num_configs; i++) {
3022 if (!strcmp(id_string, mixer_bitwidth_config[i].id_string))
3023 return mixer_bitwidth_config[i].value;
3024 }
3025
3026 return -EINVAL;
3027}
3028
3029int audio_extn_utils_get_sample_rate_from_string(const char *id_string)
3030{
3031 int i;
3032 const mixer_config_lookup mixer_samplerate_config[] = {{"KHZ_32", 32000},
3033 {"KHZ_48", 48000},
3034 {"KHZ_96", 96000},
3035 {"KHZ_144", 144000},
3036 {"KHZ_192", 192000},
3037 {"KHZ_384", 384000},
3038 {"KHZ_44P1", 44100},
3039 {"KHZ_88P2", 88200},
3040 {"KHZ_176P4", 176400},
3041 {"KHZ_352P8", 352800}};
3042 int num_configs = sizeof(mixer_samplerate_config) / sizeof(mixer_samplerate_config[0]);
3043
3044 for (i = 0; i < num_configs; i++) {
3045 if (!strcmp(id_string, mixer_samplerate_config[i].id_string))
3046 return mixer_samplerate_config[i].value;
3047 }
3048
3049 return -EINVAL;
3050}
3051
3052int audio_extn_utils_get_channels_from_string(const char *id_string)
3053{
3054 int i;
3055 const mixer_config_lookup mixer_channels_config[] = {{"One", 1},
3056 {"Two", 2},
3057 {"Three",3},
3058 {"Four", 4},
3059 {"Five", 5},
3060 {"Six", 6},
3061 {"Seven", 7},
3062 {"Eight", 8}};
3063 int num_configs = sizeof(mixer_channels_config) / sizeof(mixer_channels_config[0]);
3064
3065 for (i = 0; i < num_configs; i++) {
3066 if (!strcmp(id_string, mixer_channels_config[i].id_string))
3067 return mixer_channels_config[i].value;
3068 }
3069
3070 return -EINVAL;
3071}
Surendar karka30569792018-05-08 12:02:21 +05303072
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08003073void audio_extn_utils_release_snd_device(snd_device_t snd_device)
3074{
3075 audio_extn_dev_arbi_release(snd_device);
3076 audio_extn_sound_trigger_update_device_status(snd_device,
3077 ST_EVENT_SND_DEVICE_FREE);
3078 audio_extn_listen_update_device_status(snd_device,
3079 LISTEN_EVENT_SND_DEVICE_FREE);
3080}
3081
Weiyin Jiangd5718bd2019-09-04 16:52:08 +08003082int audio_extn_utils_get_license_params(
3083 const struct audio_device *adev,
3084 struct audio_license_params *license_params)
Surendar karka30569792018-05-08 12:02:21 +05303085{
3086 if(!license_params)
3087 return -EINVAL;
Weiyin Jiangd5718bd2019-09-04 16:52:08 +08003088
3089 return platform_get_license_by_product(adev->platform,
3090 (const char*)license_params->product, &license_params->key, license_params->license);
Surendar karka30569792018-05-08 12:02:21 +05303091}
3092
Aalique Grahame22e49102018-12-18 14:23:57 -08003093int audio_extn_utils_send_app_type_gain(struct audio_device *adev,
3094 int app_type,
3095 int *gain)
3096{
3097 int gain_cfg[4];
3098 const char *mixer_ctl_name = "App Type Gain";
3099 struct mixer_ctl *ctl;
3100 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
3101 if (!ctl) {
3102 ALOGE("%s: Could not get volume ctl mixer %s", __func__,
3103 mixer_ctl_name);
3104 return -EINVAL;
3105 }
3106 gain_cfg[0] = 0;
3107 gain_cfg[1] = app_type;
3108 gain_cfg[2] = gain[0];
3109 gain_cfg[3] = gain[1];
3110 ALOGV("%s app_type %d l(%d) r(%d)", __func__, app_type, gain[0], gain[1]);
3111 return mixer_ctl_set_array(ctl, gain_cfg,
3112 sizeof(gain_cfg)/sizeof(gain_cfg[0]));
3113}
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -07003114
Weiyin Jiangd5718bd2019-09-04 16:52:08 +08003115static void vndk_fwk_init()
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -07003116{
Weiyin Jiangd5718bd2019-09-04 16:52:08 +08003117 if (mVndkFwk.lib_handle != NULL)
3118 return;
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -07003119
Weiyin Jiangd5718bd2019-09-04 16:52:08 +08003120 mVndkFwk.lib_handle = dlopen(VNDK_FWK_LIB_PATH, RTLD_NOW);
3121 if (mVndkFwk.lib_handle == NULL) {
3122 ALOGW("%s: failed to dlopen VNDK_FWK_LIB %s", __func__, strerror(errno));
3123 return;
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -07003124 }
3125
Weiyin Jiangd5718bd2019-09-04 16:52:08 +08003126 *(void **)(&mVndkFwk.isVendorEnhancedFwk) =
3127 dlsym(mVndkFwk.lib_handle, "isRunningWithVendorEnhancedFramework");
3128 if (mVndkFwk.isVendorEnhancedFwk == NULL) {
3129 ALOGW("%s: dlsym failed %s", __func__, strerror(errno));
3130 if (mVndkFwk.lib_handle) {
3131 dlclose(mVndkFwk.lib_handle);
3132 mVndkFwk.lib_handle = NULL;
3133 }
3134 return;
3135 }
3136
3137
3138 *(void **)(&mVndkFwk.getVendorEnhancedInfo) =
3139 dlsym(mVndkFwk.lib_handle, "getVendorEnhancedInfo");
3140 if (mVndkFwk.getVendorEnhancedInfo == NULL) {
3141 ALOGW("%s: dlsym failed %s", __func__, strerror(errno));
3142 if (mVndkFwk.lib_handle) {
3143 dlclose(mVndkFwk.lib_handle);
3144 mVndkFwk.lib_handle = NULL;
3145 }
3146 }
3147
3148 return;
3149}
3150
3151bool audio_extn_utils_is_vendor_enhanced_fwk()
3152{
3153 static int is_vendor_enhanced_fwk = -EINVAL;
3154 if (is_vendor_enhanced_fwk != -EINVAL)
3155 return (bool)is_vendor_enhanced_fwk;
3156
3157 vndk_fwk_init();
3158
3159 if (mVndkFwk.isVendorEnhancedFwk != NULL) {
3160 is_vendor_enhanced_fwk = mVndkFwk.isVendorEnhancedFwk();
3161 ALOGW("%s: is_vendor_enhanced_fwk %d", __func__, is_vendor_enhanced_fwk);
3162 } else {
3163 is_vendor_enhanced_fwk = 0;
3164 ALOGW("%s: default to non enhanced_fwk config", __func__);
3165 }
3166
3167 return (bool)is_vendor_enhanced_fwk;
3168}
3169
3170int audio_extn_utils_get_vendor_enhanced_info()
3171{
3172 static int vendor_enhanced_info = -EINVAL;
3173 if (vendor_enhanced_info != -EINVAL)
3174 return vendor_enhanced_info;
3175
3176 vndk_fwk_init();
3177
3178 if (mVndkFwk.getVendorEnhancedInfo != NULL) {
3179 vendor_enhanced_info = mVndkFwk.getVendorEnhancedInfo();
3180 ALOGW("%s: vendor_enhanced_info 0x%x", __func__, vendor_enhanced_info);
3181 } else {
3182 vendor_enhanced_info = 0x0;
3183 ALOGW("%s: default to vendor_enhanced_info 0x0", __func__);
3184 }
3185
3186 return vendor_enhanced_info;
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -07003187}
Deeraj Soman14230922019-01-30 16:39:30 +05303188
Deeraj Somanfa377bf2019-02-06 12:57:59 +05303189int audio_extn_utils_get_perf_mode_flag(void)
3190{
3191#ifdef COMPRESSED_PERF_MODE_FLAG
3192 return COMPRESSED_PERF_MODE_FLAG;
3193#else
3194 return 0;
3195#endif
3196}
3197
Deeraj Soman14230922019-01-30 16:39:30 +05303198size_t audio_extn_utils_get_input_buffer_size(uint32_t sample_rate,
3199 audio_format_t format,
3200 int channel_count,
3201 int64_t duration_ms,
3202 bool is_low_latency)
3203{
3204 size_t size = 0;
3205 size_t capture_duration = AUDIO_CAPTURE_PERIOD_DURATION_MSEC;
3206 uint32_t bytes_per_period_sample = 0;
3207
3208
3209 if (audio_extn_utils_check_input_parameters(sample_rate, format, channel_count) != 0)
3210 return 0;
3211
3212 if (duration_ms >= MIN_OFFLOAD_BUFFER_DURATION_MS && duration_ms <= MAX_OFFLOAD_BUFFER_DURATION_MS)
3213 capture_duration = duration_ms;
3214
3215 size = (sample_rate * capture_duration) / 1000;
3216 if (is_low_latency)
3217 size = LOW_LATENCY_CAPTURE_PERIOD_SIZE;
3218
3219
3220 bytes_per_period_sample = audio_bytes_per_sample(format) * channel_count;
3221 size *= bytes_per_period_sample;
3222
3223 /* make sure the size is multiple of 32 bytes and additionally multiple of
3224 * the frame_size (required for 24bit samples and non-power-of-2 channel counts)
3225 * At 48 kHz mono 16-bit PCM:
3226 * 5.000 ms = 240 frames = 15*16*1*2 = 480, a whole multiple of 32 (15)
3227 * 3.333 ms = 160 frames = 10*16*1*2 = 320, a whole multiple of 32 (10)
3228 *
3229 * The loop reaches result within 32 iterations, as initial size is
3230 * already a multiple of frame_size
3231 */
3232 size = audio_extn_utils_nearest_multiple(size, audio_extn_utils_lcm(32, bytes_per_period_sample));
3233
3234 return size;
3235}
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07003236
3237int audio_extn_utils_hash_fn(void *key)
3238{
3239 return (int)key;
3240}
3241
3242bool audio_extn_utils_hash_eq(void *key1, void *key2)
3243{
3244 return (key1 == key2);
3245}