blob: fa113be4b3f1339c801280beeedd8b6ba40a7b58 [file] [log] [blame]
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001/*
Aalique Grahame22e49102018-12-18 14:23:57 -08002 * Copyright (c) 2014-2019, 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
Yamit Mehtaa0d653a2016-11-25 20:33:25 +053057#ifdef LINUX_ENABLED
58#define AUDIO_OUTPUT_POLICY_VENDOR_CONFIG_FILE "/etc/audio_output_policy.conf"
Dhananjay Kumard6d32152016-10-13 16:11:03 +053059#define AUDIO_IO_POLICY_VENDOR_CONFIG_FILE "/etc/audio_io_policy.conf"
Yamit Mehtaa0d653a2016-11-25 20:33:25 +053060#else
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070061#define AUDIO_OUTPUT_POLICY_VENDOR_CONFIG_FILE "/vendor/etc/audio_output_policy.conf"
Dhananjay Kumard6d32152016-10-13 16:11:03 +053062#define AUDIO_IO_POLICY_VENDOR_CONFIG_FILE "/vendor/etc/audio_io_policy.conf"
Yamit Mehtaa0d653a2016-11-25 20:33:25 +053063#endif
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070064
65#define OUTPUTS_TAG "outputs"
Dhananjay Kumard6d32152016-10-13 16:11:03 +053066#define INPUTS_TAG "inputs"
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070067
68#define DYNAMIC_VALUE_TAG "dynamic"
69#define FLAGS_TAG "flags"
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +053070#define PROFILES_TAG "profile"
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070071#define FORMATS_TAG "formats"
72#define SAMPLING_RATES_TAG "sampling_rates"
73#define BIT_WIDTH_TAG "bit_width"
74#define APP_TYPE_TAG "app_type"
75
76#define STRING_TO_ENUM(string) { #string, string }
77#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
78
Ben Rombergera04fabc2014-11-14 12:16:03 -080079#define BASE_TABLE_SIZE 64
80#define MAX_BASEINDEX_LEN 256
81
Ben Romberger1aaaf862017-04-06 17:49:46 -070082#ifndef SND_AUDIOCODEC_TRUEHD
83#define SND_AUDIOCODEC_TRUEHD 0x00000023
84#endif
85
Vikram Panduranga93f080e2017-06-07 18:16:14 -070086#define APP_TYPE_VOIP_AUDIO 0x1113A
87
Ashish Jain81eb2a82015-05-13 10:52:34 +053088#ifdef AUDIO_EXTERNAL_HDMI_ENABLED
89#define PROFESSIONAL (1<<0) /* 0 = consumer, 1 = professional */
90#define NON_LPCM (1<<1) /* 0 = audio, 1 = non-audio */
91#define SR_44100 (0<<0) /* 44.1kHz */
92#define SR_NOTID (1<<0) /* non indicated */
93#define SR_48000 (2<<0) /* 48kHz */
94#define SR_32000 (3<<0) /* 32kHz */
95#define SR_22050 (4<<0) /* 22.05kHz */
96#define SR_24000 (6<<0) /* 24kHz */
97#define SR_88200 (8<<0) /* 88.2kHz */
98#define SR_96000 (10<<0) /* 96kHz */
99#define SR_176400 (12<<0) /* 176.4kHz */
100#define SR_192000 (14<<0) /* 192kHz */
101
102#endif
Manish Dewangan07de2142017-02-27 19:27:20 +0530103
104/* ToDo: Check and update a proper value in msec */
Weiyin Jiangc49a3b52018-08-17 16:16:08 +0800105#define COMPRESS_OFFLOAD_PLAYBACK_LATENCY 50
106#define PCM_OFFLOAD_PLAYBACK_DSP_PATHDELAY 62
107#define PCM_OFFLOAD_PLAYBACK_LATENCY \
108 (PCM_OFFLOAD_BUFFER_DURATION + PCM_OFFLOAD_PLAYBACK_DSP_PATHDELAY)
Manish Dewangan07de2142017-02-27 19:27:20 +0530109
Varun Balaraje49253e2017-07-06 19:48:56 +0530110#ifndef MAX_CHANNELS_SUPPORTED
111#define MAX_CHANNELS_SUPPORTED 8
112#endif
113
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -0700114#ifdef __LP64__
115#define VNDK_FWK_LIB_PATH "/vendor/lib64/libqti_vndfwk_detect.so"
116#else
117#define VNDK_FWK_LIB_PATH "/vendor/lib/libqti_vndfwk_detect.so"
118#endif
119
120static void *vndk_fwk_lib_handle = NULL;
121typedef int (*vndk_fwk_isVendorEnhancedFwk_t)();
122static vndk_fwk_isVendorEnhancedFwk_t vndk_fwk_isVendorEnhancedFwk;
123
`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),
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530149 STRING_TO_ENUM(AUDIO_INPUT_FLAG_NONE),
150 STRING_TO_ENUM(AUDIO_INPUT_FLAG_FAST),
151 STRING_TO_ENUM(AUDIO_INPUT_FLAG_HW_HOTWORD),
152 STRING_TO_ENUM(AUDIO_INPUT_FLAG_RAW),
153 STRING_TO_ENUM(AUDIO_INPUT_FLAG_SYNC),
Dhananjay Kumaree4d2002016-10-25 18:02:58 +0530154 STRING_TO_ENUM(AUDIO_INPUT_FLAG_TIMESTAMP),
Dhananjay Kumar704ce6f2017-09-28 22:08:00 +0530155 STRING_TO_ENUM(AUDIO_INPUT_FLAG_COMPRESS),
Ralf Herzaec80262018-07-03 07:08:49 +0200156 STRING_TO_ENUM(AUDIO_INPUT_FLAG_PASSTHROUGH),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700157};
158
159const struct string_to_enum s_format_name_to_enum_table[] = {
Ashish Jain83a6cc22016-06-28 14:34:17 +0530160 STRING_TO_ENUM(AUDIO_FORMAT_PCM_8_BIT),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700161 STRING_TO_ENUM(AUDIO_FORMAT_PCM_16_BIT),
Ashish Jain5106d362016-05-11 19:23:33 +0530162 STRING_TO_ENUM(AUDIO_FORMAT_PCM_24_BIT_PACKED),
163 STRING_TO_ENUM(AUDIO_FORMAT_PCM_8_24_BIT),
Ashish Jainf1eaa582016-05-23 20:54:24 +0530164 STRING_TO_ENUM(AUDIO_FORMAT_PCM_32_BIT),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700165 STRING_TO_ENUM(AUDIO_FORMAT_MP3),
166 STRING_TO_ENUM(AUDIO_FORMAT_AAC),
167 STRING_TO_ENUM(AUDIO_FORMAT_VORBIS),
Mingming Yinae3530f2014-07-03 16:50:18 -0700168 STRING_TO_ENUM(AUDIO_FORMAT_AMR_NB),
169 STRING_TO_ENUM(AUDIO_FORMAT_AMR_WB),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700170 STRING_TO_ENUM(AUDIO_FORMAT_AC3),
Mingming Yinae3530f2014-07-03 16:50:18 -0700171 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700172 STRING_TO_ENUM(AUDIO_FORMAT_DTS),
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530173 STRING_TO_ENUM(AUDIO_FORMAT_DTS_HD),
Ben Romberger1aaaf862017-04-06 17:49:46 -0700174 STRING_TO_ENUM(AUDIO_FORMAT_DOLBY_TRUEHD),
Naresh Tanniru928f0862017-04-07 16:44:23 -0700175 STRING_TO_ENUM(AUDIO_FORMAT_IEC61937),
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530176 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3_JOC),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700177 STRING_TO_ENUM(AUDIO_FORMAT_WMA),
178 STRING_TO_ENUM(AUDIO_FORMAT_WMA_PRO),
179 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADIF),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700180 STRING_TO_ENUM(AUDIO_FORMAT_AMR_WB_PLUS),
181 STRING_TO_ENUM(AUDIO_FORMAT_EVRC),
182 STRING_TO_ENUM(AUDIO_FORMAT_EVRCB),
183 STRING_TO_ENUM(AUDIO_FORMAT_EVRCWB),
184 STRING_TO_ENUM(AUDIO_FORMAT_QCELP),
185 STRING_TO_ENUM(AUDIO_FORMAT_MP2),
186 STRING_TO_ENUM(AUDIO_FORMAT_EVRCNW),
Amit Shekhar6f461b12014-08-01 14:52:58 -0700187 STRING_TO_ENUM(AUDIO_FORMAT_FLAC),
Satya Krishna Pindiproli70471602015-04-24 19:12:43 +0530188 STRING_TO_ENUM(AUDIO_FORMAT_ALAC),
189 STRING_TO_ENUM(AUDIO_FORMAT_APE),
Alexy Josephcd8eaed2014-12-11 12:46:53 -0800190 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LC),
191 STRING_TO_ENUM(AUDIO_FORMAT_AAC_HE_V1),
192 STRING_TO_ENUM(AUDIO_FORMAT_AAC_HE_V2),
Manish Dewangana6fc5442015-08-24 20:30:31 +0530193 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS),
Ashish Jaine513a872015-11-19 17:00:56 +0530194 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_LC),
195 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_HE_V1),
196 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_HE_V2),
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +0530197 STRING_TO_ENUM(AUDIO_FORMAT_DSD),
Arun Kumar Dasari3b174182016-12-27 13:01:14 +0530198 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM),
199 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM_LC),
200 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM_HE_V1),
201 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM_HE_V2),
Dhanalakshmi Siddani18737932016-11-29 17:33:17 +0530202 STRING_TO_ENUM(AUDIO_FORMAT_APTX),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700203};
204
Manish Dewangan3ccdea52017-02-13 19:31:54 +0530205/* payload structure avt_device drift query */
206struct audio_avt_device_drift_stats {
207 uint32_t minor_version;
Manish Dewangan338c50a2017-09-12 15:22:03 +0530208
Manish Dewangan3ccdea52017-02-13 19:31:54 +0530209 /* Indicates the device interface direction as either
210 * source (Tx) or sink (Rx).
211 */
212 uint16_t device_direction;
Manish Dewangan338c50a2017-09-12 15:22:03 +0530213
214 /* Reference timer for drift accumulation and time stamp information.
215 * currently it only support AFE_REF_TIMER_TYPE_AVTIMER
216 */
217 uint16_t reference_timer;
Manish Dewangan3ccdea52017-02-13 19:31:54 +0530218 struct audio_avt_device_drift_param drift_param;
Manish Dewangan338c50a2017-09-12 15:22:03 +0530219} __attribute__((packed));
Manish Dewangan3ccdea52017-02-13 19:31:54 +0530220
Ben Rombergera04fabc2014-11-14 12:16:03 -0800221static char bTable[BASE_TABLE_SIZE] = {
222 'A','B','C','D','E','F','G','H','I','J','K','L',
223 'M','N','O','P','Q','R','S','T','U','V','W','X',
224 'Y','Z','a','b','c','d','e','f','g','h','i','j',
225 'k','l','m','n','o','p','q','r','s','t','u','v',
226 'w','x','y','z','0','1','2','3','4','5','6','7',
227 '8','9','+','/'
228};
229
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700230static uint32_t string_to_enum(const struct string_to_enum *table, size_t size,
231 const char *name)
232{
233 size_t i;
234 for (i = 0; i < size; i++) {
235 if (strcmp(table[i].name, name) == 0) {
236 ALOGV("%s found %s", __func__, table[i].name);
237 return table[i].value;
238 }
239 }
240 return 0;
241}
242
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530243static audio_io_flags_t parse_flag_names(char *name)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700244{
245 uint32_t flag = 0;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530246 audio_io_flags_t io_flags;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800247 char *last_r;
248 char *flag_name = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700249 while (flag_name != NULL) {
250 if (strlen(flag_name) != 0) {
251 flag |= string_to_enum(s_flag_name_to_enum_table,
252 ARRAY_SIZE(s_flag_name_to_enum_table),
253 flag_name);
254 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800255 flag_name = strtok_r(NULL, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700256 }
257
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800258 ALOGV("parse_flag_names: flag - %x", flag);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530259 io_flags.in_flags = (audio_input_flags_t)flag;
260 io_flags.out_flags = (audio_output_flags_t)flag;
261 return io_flags;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700262}
263
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530264static void parse_format_names(char *name, struct streams_io_cfg *s_info)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700265{
266 struct stream_format *sf_info = NULL;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800267 char *last_r;
268 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700269
270 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG) == 0)
271 return;
272
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530273 list_init(&s_info->format_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700274 while (str != NULL) {
275 audio_format_t format = (audio_format_t)string_to_enum(s_format_name_to_enum_table,
276 ARRAY_SIZE(s_format_name_to_enum_table), str);
277 ALOGV("%s: format - %d", __func__, format);
278 if (format != 0) {
279 sf_info = (struct stream_format *)calloc(1, sizeof(struct stream_format));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700280 if (sf_info == NULL)
281 break; /* return whatever was parsed */
282
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700283 sf_info->format = format;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530284 list_add_tail(&s_info->format_list, &sf_info->list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700285 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800286 str = strtok_r(NULL, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700287 }
288}
289
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530290static void parse_sample_rate_names(char *name, struct streams_io_cfg *s_info)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700291{
Amit Shekhar6f461b12014-08-01 14:52:58 -0700292 struct stream_sample_rate *ss_info = NULL;
293 uint32_t sample_rate = 48000;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800294 char *last_r;
295 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700296
Amit Shekhar6f461b12014-08-01 14:52:58 -0700297 if (str != NULL && 0 == strcmp(str, DYNAMIC_VALUE_TAG))
298 return;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700299
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530300 list_init(&s_info->sample_rate_list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700301 while (str != NULL) {
302 sample_rate = (uint32_t)strtol(str, (char **)NULL, 10);
303 ALOGV("%s: sample_rate - %d", __func__, sample_rate);
304 if (0 != sample_rate) {
305 ss_info = (struct stream_sample_rate *)calloc(1, sizeof(struct stream_sample_rate));
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800306 if (!ss_info) {
307 ALOGE("%s: memory allocation failure", __func__);
308 return;
309 }
Amit Shekhar6f461b12014-08-01 14:52:58 -0700310 ss_info->sample_rate = sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530311 list_add_tail(&s_info->sample_rate_list, &ss_info->list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700312 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800313 str = strtok_r(NULL, "|", &last_r);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700314 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700315}
316
317static int parse_bit_width_names(char *name)
318{
319 int bit_width = 16;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800320 char *last_r;
321 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700322
323 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG))
324 bit_width = (int)strtol(str, (char **)NULL, 10);
325
326 ALOGV("%s: bit_width - %d", __func__, bit_width);
327 return bit_width;
328}
329
330static int parse_app_type_names(void *platform, char *name)
331{
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700332 int app_type = platform_get_default_app_type(platform);
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800333 char *last_r;
334 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700335
336 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG))
337 app_type = (int)strtol(str, (char **)NULL, 10);
338
339 ALOGV("%s: app_type - %d", __func__, app_type);
340 return app_type;
341}
342
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530343static void update_streams_cfg_list(cnode *root, void *platform,
344 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700345{
346 cnode *node = root->first_child;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530347 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700348
349 ALOGV("%s", __func__);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530350 s_info = (struct streams_io_cfg *)calloc(1, sizeof(struct streams_io_cfg));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700351
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530352 if (!s_info) {
353 ALOGE("failed to allocate mem for s_info list element");
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700354 return;
355 }
356
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700357 while (node) {
358 if (strcmp(node->name, FLAGS_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530359 s_info->flags = parse_flag_names((char *)node->value);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530360 } else if (strcmp(node->name, PROFILES_TAG) == 0) {
361 strlcpy(s_info->profile, (char *)node->value, sizeof(s_info->profile));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700362 } else if (strcmp(node->name, FORMATS_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530363 parse_format_names((char *)node->value, s_info);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700364 } else if (strcmp(node->name, SAMPLING_RATES_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530365 s_info->app_type_cfg.sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
366 parse_sample_rate_names((char *)node->value, s_info);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700367 } else if (strcmp(node->name, BIT_WIDTH_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530368 s_info->app_type_cfg.bit_width = parse_bit_width_names((char *)node->value);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700369 } else if (strcmp(node->name, APP_TYPE_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530370 s_info->app_type_cfg.app_type = parse_app_type_names(platform, (char *)node->value);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700371 }
372 node = node->next;
373 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530374 list_add_tail(streams_cfg_list, &s_info->list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700375}
376
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530377static void load_cfg_list(cnode *root, void *platform,
378 struct listnode *streams_output_cfg_list,
379 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700380{
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530381 cnode *node = NULL;
382
383 node = config_find(root, OUTPUTS_TAG);
384 if (node != NULL) {
385 node = node->first_child;
386 while (node) {
387 ALOGV("%s: loading output %s", __func__, node->name);
388 update_streams_cfg_list(node, platform, streams_output_cfg_list);
389 node = node->next;
390 }
391 } else {
392 ALOGI("%s: could not load output, node is NULL", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700393 }
394
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530395 node = config_find(root, INPUTS_TAG);
396 if (node != NULL) {
397 node = node->first_child;
398 while (node) {
399 ALOGV("%s: loading input %s", __func__, node->name);
400 update_streams_cfg_list(node, platform, streams_input_cfg_list);
401 node = node->next;
402 }
403 } else {
404 ALOGI("%s: could not load input, node is NULL", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700405 }
406}
407
408static void send_app_type_cfg(void *platform, struct mixer *mixer,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530409 struct listnode *streams_output_cfg_list,
410 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700411{
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530412 size_t app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT] = {0};
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700413 int length = 0, i, num_app_types = 0;
414 struct listnode *node;
415 bool update;
416 struct mixer_ctl *ctl = NULL;
417 const char *mixer_ctl_name = "App Type Config";
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530418 struct streams_io_cfg *s_info = NULL;
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800419 uint32_t target_bit_width = 0;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700420
421 if (!mixer) {
422 ALOGE("%s: mixer is null",__func__);
423 return;
424 }
425 ctl = mixer_get_ctl_by_name(mixer, mixer_ctl_name);
426 if (!ctl) {
427 ALOGE("%s: Could not get ctl for mixer cmd - %s",__func__, mixer_ctl_name);
428 return;
429 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530430 app_type_cfg[length++] = num_app_types;
431
432 if (list_empty(streams_output_cfg_list)) {
433 app_type_cfg[length++] = platform_get_default_app_type_v2(platform, PCM_PLAYBACK);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700434 app_type_cfg[length++] = 48000;
435 app_type_cfg[length++] = 16;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530436 num_app_types += 1;
437 }
438 if (list_empty(streams_input_cfg_list)) {
439 app_type_cfg[length++] = platform_get_default_app_type_v2(platform, PCM_CAPTURE);
440 app_type_cfg[length++] = 48000;
441 app_type_cfg[length++] = 16;
442 num_app_types += 1;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700443 }
444
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800445 /* get target bit width for ADM enforce mode */
446 target_bit_width = adev_get_dsp_bit_width_enforce_mode();
447
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700448 list_for_each(node, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530449 s_info = node_to_item(node, struct streams_io_cfg, list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700450 update = true;
451 for (i=0; i<length; i=i+3) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530452 if (app_type_cfg[i+1] == 0)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700453 break;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530454 else if (app_type_cfg[i+1] == (size_t)s_info->app_type_cfg.app_type) {
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530455 if (app_type_cfg[i+2] < (size_t)s_info->app_type_cfg.sample_rate)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530456 app_type_cfg[i+2] = s_info->app_type_cfg.sample_rate;
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530457 if (app_type_cfg[i+3] < (size_t)s_info->app_type_cfg.bit_width)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530458 app_type_cfg[i+3] = s_info->app_type_cfg.bit_width;
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800459 /* ADM bit width = max(enforce_bit_width, bit_width from s_info */
460 if (audio_extn_is_dsp_bit_width_enforce_mode_supported(s_info->flags.out_flags) &&
461 (target_bit_width > app_type_cfg[i+3]))
462 app_type_cfg[i+3] = target_bit_width;
463
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700464 update = false;
465 break;
466 }
467 }
468 if (update && ((length + 3) <= MAX_LENGTH_MIXER_CONTROL_IN_INT)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530469 num_app_types += 1;
470 app_type_cfg[length++] = s_info->app_type_cfg.app_type;
471 app_type_cfg[length++] = s_info->app_type_cfg.sample_rate;
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800472 app_type_cfg[length] = s_info->app_type_cfg.bit_width;
473 if (audio_extn_is_dsp_bit_width_enforce_mode_supported(s_info->flags.out_flags) &&
474 (target_bit_width > app_type_cfg[length]))
475 app_type_cfg[length] = target_bit_width;
476
477 length++;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530478 }
479 }
480 list_for_each(node, streams_input_cfg_list) {
481 s_info = node_to_item(node, struct streams_io_cfg, list);
482 update = true;
483 for (i=0; i<length; i=i+3) {
484 if (app_type_cfg[i+1] == 0)
485 break;
486 else if (app_type_cfg[i+1] == (size_t)s_info->app_type_cfg.app_type) {
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530487 if (app_type_cfg[i+2] < (size_t)s_info->app_type_cfg.sample_rate)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530488 app_type_cfg[i+2] = s_info->app_type_cfg.sample_rate;
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530489 if (app_type_cfg[i+3] < (size_t)s_info->app_type_cfg.bit_width)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530490 app_type_cfg[i+3] = s_info->app_type_cfg.bit_width;
491 update = false;
492 break;
493 }
494 }
495 if (update && ((length + 3) <= MAX_LENGTH_MIXER_CONTROL_IN_INT)) {
496 num_app_types += 1;
497 app_type_cfg[length++] = s_info->app_type_cfg.app_type;
498 app_type_cfg[length++] = s_info->app_type_cfg.sample_rate;
499 app_type_cfg[length++] = s_info->app_type_cfg.bit_width;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700500 }
501 }
502 ALOGV("%s: num_app_types: %d", __func__, num_app_types);
503 if (num_app_types) {
504 app_type_cfg[0] = num_app_types;
505 mixer_ctl_set_array(ctl, app_type_cfg, length);
506 }
507}
508
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530509void audio_extn_utils_update_streams_cfg_lists(void *platform,
510 struct mixer *mixer,
511 struct listnode *streams_output_cfg_list,
512 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700513{
514 cnode *root;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530515 char *data = NULL;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700516
517 ALOGV("%s", __func__);
518 list_init(streams_output_cfg_list);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530519 list_init(streams_input_cfg_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700520
521 root = config_node("", "");
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700522 if (root == NULL) {
523 ALOGE("cfg_list, NULL config root");
524 return;
525 }
526
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530527 data = (char *)load_file(AUDIO_IO_POLICY_VENDOR_CONFIG_FILE, NULL);
528 if (data == NULL) {
529 ALOGD("%s: failed to open io config file(%s), trying older config file",
530 __func__, AUDIO_IO_POLICY_VENDOR_CONFIG_FILE);
531 data = (char *)load_file(AUDIO_OUTPUT_POLICY_VENDOR_CONFIG_FILE, NULL);
532 if (data == NULL) {
533 send_app_type_cfg(platform, mixer,
534 streams_output_cfg_list,
535 streams_input_cfg_list);
536 ALOGE("%s: could not load io policy config!", __func__);
yidongh6eb4f752017-04-19 18:20:57 +0800537 free(root);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530538 return;
539 }
540 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700541
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530542 config_load(root, data);
543 load_cfg_list(root, platform, streams_output_cfg_list,
544 streams_input_cfg_list);
545
546 send_app_type_cfg(platform, mixer, streams_output_cfg_list,
547 streams_input_cfg_list);
Alexy Josephaee4fdd2016-01-29 13:02:07 -0800548
549 config_free(root);
yidongh6eb4f752017-04-19 18:20:57 +0800550 free(root);
Alexy Josephaee4fdd2016-01-29 13:02:07 -0800551 free(data);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700552}
553
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530554static void audio_extn_utils_dump_streams_cfg_list(
555 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700556{
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700557 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530558 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700559 struct stream_format *sf_info;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700560 struct stream_sample_rate *ss_info;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530561
562 list_for_each(node_i, streams_cfg_list) {
563 s_info = node_to_item(node_i, struct streams_io_cfg, list);
564 ALOGV("%s: flags-%d, sample_rate-%d, bit_width-%d, app_type-%d",
565 __func__, s_info->flags.out_flags, s_info->app_type_cfg.sample_rate,
566 s_info->app_type_cfg.bit_width, s_info->app_type_cfg.app_type);
567 list_for_each(node_j, &s_info->format_list) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700568 sf_info = node_to_item(node_j, struct stream_format, list);
569 ALOGV("format-%x", sf_info->format);
570 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530571 list_for_each(node_j, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700572 ss_info = node_to_item(node_j, struct stream_sample_rate, list);
573 ALOGV("sample rate-%d", ss_info->sample_rate);
574 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700575 }
576}
577
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530578void audio_extn_utils_dump_streams_cfg_lists(
579 struct listnode *streams_output_cfg_list,
580 struct listnode *streams_input_cfg_list)
581{
582 ALOGV("%s", __func__);
583 audio_extn_utils_dump_streams_cfg_list(streams_output_cfg_list);
584 audio_extn_utils_dump_streams_cfg_list(streams_input_cfg_list);
585}
586
587static void audio_extn_utils_release_streams_cfg_list(
588 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700589{
590 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530591 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700592
593 ALOGV("%s", __func__);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530594
595 while (!list_empty(streams_cfg_list)) {
596 node_i = list_head(streams_cfg_list);
597 s_info = node_to_item(node_i, struct streams_io_cfg, list);
598 while (!list_empty(&s_info->format_list)) {
599 node_j = list_head(&s_info->format_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700600 list_remove(node_j);
601 free(node_to_item(node_j, struct stream_format, list));
602 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530603 while (!list_empty(&s_info->sample_rate_list)) {
604 node_j = list_head(&s_info->sample_rate_list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700605 list_remove(node_j);
606 free(node_to_item(node_j, struct stream_sample_rate, list));
607 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700608 list_remove(node_i);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530609 free(node_to_item(node_i, struct streams_io_cfg, list));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700610 }
611}
612
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530613void audio_extn_utils_release_streams_cfg_lists(
614 struct listnode *streams_output_cfg_list,
615 struct listnode *streams_input_cfg_list)
616{
617 ALOGV("%s", __func__);
618 audio_extn_utils_release_streams_cfg_list(streams_output_cfg_list);
619 audio_extn_utils_release_streams_cfg_list(streams_input_cfg_list);
620}
621
622static bool set_app_type_cfg(struct streams_io_cfg *s_info,
623 struct stream_app_type_cfg *app_type_cfg,
624 uint32_t sample_rate, uint32_t bit_width)
Amit Shekhar6f461b12014-08-01 14:52:58 -0700625 {
626 struct listnode *node_i;
627 struct stream_sample_rate *ss_info;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530628 list_for_each(node_i, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700629 ss_info = node_to_item(node_i, struct stream_sample_rate, list);
630 if ((sample_rate <= ss_info->sample_rate) &&
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530631 (bit_width == s_info->app_type_cfg.bit_width)) {
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700632
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530633 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700634 app_type_cfg->sample_rate = ss_info->sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530635 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700636 ALOGV("%s app_type_cfg->app_type %d, app_type_cfg->sample_rate %d, app_type_cfg->bit_width %d",
637 __func__, app_type_cfg->app_type, app_type_cfg->sample_rate, app_type_cfg->bit_width);
638 return true;
639 }
640 }
641 /*
642 * Reiterate through the list assuming dafault sample rate.
643 * Handles scenario where input sample rate is higher
644 * than all sample rates in list for the input bit width.
645 */
646 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
Apoorv Raghuvanshif59bb222015-02-18 12:23:23 -0800647
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530648 list_for_each(node_i, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700649 ss_info = node_to_item(node_i, struct stream_sample_rate, list);
650 if ((sample_rate <= ss_info->sample_rate) &&
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530651 (bit_width == s_info->app_type_cfg.bit_width)) {
652 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700653 app_type_cfg->sample_rate = sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530654 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Apoorv Raghuvanshif59bb222015-02-18 12:23:23 -0800655 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 -0700656 __func__, app_type_cfg->app_type, app_type_cfg->sample_rate, app_type_cfg->bit_width);
657 return true;
658 }
659 }
660 return false;
661}
662
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530663void audio_extn_utils_update_stream_input_app_type_cfg(void *platform,
664 struct listnode *streams_input_cfg_list,
665 audio_devices_t devices __unused,
666 audio_input_flags_t flags,
667 audio_format_t format,
668 uint32_t sample_rate,
669 uint32_t bit_width,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530670 char* profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530671 struct stream_app_type_cfg *app_type_cfg)
672{
673 struct listnode *node_i, *node_j;
674 struct streams_io_cfg *s_info;
675 struct stream_format *sf_info;
676
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530677 ALOGV("%s: flags: 0x%x, format: 0x%x sample_rate %d, profile %s",
678 __func__, flags, format, sample_rate, profile);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530679
680 list_for_each(node_i, streams_input_cfg_list) {
681 s_info = node_to_item(node_i, struct streams_io_cfg, list);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530682 /* Along with flags do profile matching if set at either end.*/
683 if (s_info->flags.in_flags == flags &&
684 ((profile[0] == '\0' && s_info->profile[0] == '\0') ||
685 strncmp(s_info->profile, profile, sizeof(s_info->profile)) == 0)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530686 list_for_each(node_j, &s_info->format_list) {
687 sf_info = node_to_item(node_j, struct stream_format, list);
688 if (sf_info->format == format) {
689 if (set_app_type_cfg(s_info, app_type_cfg, sample_rate, bit_width))
690 return;
691 }
692 }
693 }
694 }
695 ALOGW("%s: App type could not be selected. Falling back to default", __func__);
696 app_type_cfg->app_type = platform_get_default_app_type_v2(platform, PCM_CAPTURE);
697 app_type_cfg->sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
698 app_type_cfg->bit_width = 16;
699}
700
701void audio_extn_utils_update_stream_output_app_type_cfg(void *platform,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700702 struct listnode *streams_output_cfg_list,
Amit Shekhar1d896042014-10-03 13:16:09 -0700703 audio_devices_t devices,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700704 audio_output_flags_t flags,
705 audio_format_t format,
Amit Shekhar6f461b12014-08-01 14:52:58 -0700706 uint32_t sample_rate,
707 uint32_t bit_width,
Manish Dewangan837dc462015-05-27 10:17:41 +0530708 audio_channel_mask_t channel_mask,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530709 char *profile,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700710 struct stream_app_type_cfg *app_type_cfg)
711{
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +0530712 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530713 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700714 struct stream_format *sf_info;
Manish Dewangan837dc462015-05-27 10:17:41 +0530715 char value[PROPERTY_VALUE_MAX] = {0};
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700716
Ramu Gottipati074fa4d2018-09-11 20:05:51 +0530717 if (devices & AUDIO_DEVICE_OUT_SPEAKER) {
718 int bw = platform_get_snd_device_bit_width(SND_DEVICE_OUT_SPEAKER);
719 if ((-ENOSYS != bw) && (bit_width > (uint32_t)bw))
Amit Shekhar5a39c912014-10-14 15:39:30 -0700720 bit_width = (uint32_t)bw;
Ramu Gottipati074fa4d2018-09-11 20:05:51 +0530721 else if (-ENOSYS == bw)
722 bit_width = CODEC_BACKEND_DEFAULT_BIT_WIDTH;
Amit Shekhar1d896042014-10-03 13:16:09 -0700723 sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Ramu Gottipati074fa4d2018-09-11 20:05:51 +0530724 ALOGI("%s Allowing 24 and above bits playback on speaker ONLY at default sampling rate", __func__);
Amit Shekhar1d896042014-10-03 13:16:09 -0700725 }
726
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -0700727 property_get("vendor.audio.playback.mch.downsample",value,"");
Manish Dewangan837dc462015-05-27 10:17:41 +0530728 if (!strncmp("true", value, sizeof("true"))) {
729 if ((popcount(channel_mask) > 2) &&
730 (sample_rate > CODEC_BACKEND_DEFAULT_SAMPLE_RATE) &&
Aalique Grahame5cd12ff2017-10-19 10:57:00 -0700731 !(flags & (audio_output_flags_t)AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH)) {
Manish Dewangan837dc462015-05-27 10:17:41 +0530732 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
733 ALOGD("%s: MCH session defaulting sample rate to %d",
734 __func__, sample_rate);
735 }
736 }
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +0530737
738 /* Set sampling rate to 176.4 for DSD64
739 * and 352.8Khz for DSD128.
740 * Set Bit Width to 16. output will be 16 bit
741 * post DoP in ASM.
742 */
Aalique Grahame5cd12ff2017-10-19 10:57:00 -0700743 if ((flags & (audio_output_flags_t)AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH) &&
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +0530744 (format == AUDIO_FORMAT_DSD)) {
745 bit_width = 16;
746 if (sample_rate == INPUT_SAMPLING_RATE_DSD64)
747 sample_rate = OUTPUT_SAMPLING_RATE_DSD64;
748 else if (sample_rate == INPUT_SAMPLING_RATE_DSD128)
749 sample_rate = OUTPUT_SAMPLING_RATE_DSD128;
750 }
751
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +0530752
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800753 ALOGV("%s: flags: %x, format: %x sample_rate %d, profile %s, app_type %d",
754 __func__, flags, format, sample_rate, profile, app_type_cfg->app_type);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700755 list_for_each(node_i, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530756 s_info = node_to_item(node_i, struct streams_io_cfg, list);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530757 /* Along with flags do profile matching if set at either end.*/
758 if (s_info->flags.out_flags == flags &&
759 ((profile[0] == '\0' && s_info->profile[0] == '\0') ||
760 strncmp(s_info->profile, profile, sizeof(s_info->profile)) == 0)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530761 list_for_each(node_j, &s_info->format_list) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700762 sf_info = node_to_item(node_j, struct stream_format, list);
763 if (sf_info->format == format) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530764 if (set_app_type_cfg(s_info, app_type_cfg, sample_rate, bit_width))
Amit Shekhar6f461b12014-08-01 14:52:58 -0700765 return;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700766 }
767 }
768 }
769 }
770 list_for_each(node_i, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530771 s_info = node_to_item(node_i, struct streams_io_cfg, list);
772 if (s_info->flags.out_flags == AUDIO_OUTPUT_FLAG_PRIMARY) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700773 ALOGV("Compatible output profile not found.");
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530774 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
775 app_type_cfg->sample_rate = s_info->app_type_cfg.sample_rate;
776 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700777 ALOGV("%s Default to primary output: App type: %d sample_rate %d",
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530778 __func__, s_info->app_type_cfg.app_type, app_type_cfg->sample_rate);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700779 return;
780 }
781 }
782 ALOGW("%s: App type could not be selected. Falling back to default", __func__);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700783 app_type_cfg->app_type = platform_get_default_app_type(platform);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700784 app_type_cfg->sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700785 app_type_cfg->bit_width = 16;
786}
787
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +0530788static bool audio_is_this_native_usecase(struct audio_usecase *uc)
789{
790 bool native_usecase = false;
791 struct stream_out *out = (struct stream_out*) uc->stream.out;
792
793 if (PCM_PLAYBACK == uc->type && out != NULL &&
794 NATIVE_AUDIO_MODE_INVALID != platform_get_native_support() &&
795 is_offload_usecase(uc->id) &&
796 (out->sample_rate == OUTPUT_SAMPLING_RATE_44100))
797 native_usecase = true;
798
799 return native_usecase;
800}
801
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800802bool audio_extn_is_dsp_bit_width_enforce_mode_supported(audio_output_flags_t flags)
803{
804 /* DSP bitwidth enforce mode for ADM and AFE:
805 * includes:
806 * deep buffer, low latency, direct pcm and offload.
807 * excludes:
808 * ull(raw+fast), VOIP.
809 */
810 if ((flags & AUDIO_OUTPUT_FLAG_VOIP_RX) ||
811 ((flags & AUDIO_OUTPUT_FLAG_RAW) &&
812 (flags & AUDIO_OUTPUT_FLAG_FAST)))
813 return false;
814
815
816 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) ||
817 (flags & AUDIO_OUTPUT_FLAG_DIRECT) ||
818 (flags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) ||
819 (flags & AUDIO_OUTPUT_FLAG_PRIMARY))
820 return true;
821 else
822 return false;
823}
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800824
825static inline bool audio_is_vr_mode_on(struct audio_device *(__attribute__((unused)) adev))
826{
827 return adev->vr_audio_mode_enabled;
828}
829
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530830void audio_extn_utils_update_stream_app_type_cfg_for_usecase(
831 struct audio_device *adev,
832 struct audio_usecase *usecase)
833{
834 ALOGV("%s", __func__);
835
836 switch(usecase->type) {
837 case PCM_PLAYBACK:
838 audio_extn_utils_update_stream_output_app_type_cfg(adev->platform,
839 &adev->streams_output_cfg_list,
840 usecase->stream.out->devices,
841 usecase->stream.out->flags,
Aalique Grahame65780b52017-09-27 14:59:56 -0700842 usecase->stream.out->hal_op_format,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530843 usecase->stream.out->sample_rate,
844 usecase->stream.out->bit_width,
845 usecase->stream.out->channel_mask,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530846 usecase->stream.out->profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530847 &usecase->stream.out->app_type_cfg);
848 ALOGV("%s Selected apptype: %d", __func__, usecase->stream.out->app_type_cfg.app_type);
849 break;
850 case PCM_CAPTURE:
Vikram Panduranga93f080e2017-06-07 18:16:14 -0700851 if (usecase->id == USECASE_AUDIO_RECORD_VOIP)
852 usecase->stream.in->app_type_cfg.app_type = APP_TYPE_VOIP_AUDIO;
853 else
854 audio_extn_utils_update_stream_input_app_type_cfg(adev->platform,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530855 &adev->streams_input_cfg_list,
856 usecase->stream.in->device,
857 usecase->stream.in->flags,
858 usecase->stream.in->format,
859 usecase->stream.in->sample_rate,
860 usecase->stream.in->bit_width,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530861 usecase->stream.in->profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530862 &usecase->stream.in->app_type_cfg);
863 ALOGV("%s Selected apptype: %d", __func__, usecase->stream.in->app_type_cfg.app_type);
864 break;
Surendar Karka93cd25a2018-08-28 14:21:37 +0530865 case TRANSCODE_LOOPBACK_RX :
Siddartha Shaik343abc62017-08-08 11:15:25 +0530866 audio_extn_utils_update_stream_output_app_type_cfg(adev->platform,
867 &adev->streams_output_cfg_list,
868 usecase->stream.inout->out_config.devices,
869 0,
870 usecase->stream.inout->out_config.format,
871 usecase->stream.inout->out_config.sample_rate,
872 usecase->stream.inout->out_config.bit_width,
873 usecase->stream.inout->out_config.channel_mask,
874 usecase->stream.inout->profile,
875 &usecase->stream.inout->out_app_type_cfg);
876 ALOGV("%s Selected apptype: %d", __func__, usecase->stream.inout->out_app_type_cfg.app_type);
877 break;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530878 default:
879 ALOGE("%s: app type cfg not supported for usecase type (%d)",
880 __func__, usecase->type);
881 }
882}
883
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +0530884void audio_extn_btsco_get_sample_rate(int snd_device, int *sample_rate)
885{
886 switch (snd_device) {
887 case SND_DEVICE_OUT_BT_SCO:
888 case SND_DEVICE_IN_BT_SCO_MIC:
889 case SND_DEVICE_IN_BT_SCO_MIC_NREC:
890 *sample_rate = 8000;
891 break;
892 case SND_DEVICE_OUT_BT_SCO_WB:
893 case SND_DEVICE_IN_BT_SCO_MIC_WB:
894 case SND_DEVICE_IN_BT_SCO_MIC_WB_NREC:
895 *sample_rate = 16000;
896 break;
897 default:
898 ALOGD("%s:Not a BT SCO device, need not update sampling rate\n", __func__);
899 break;
900 }
901}
902
Aalique Grahame22e49102018-12-18 14:23:57 -0800903static int set_stream_app_type_mixer_ctrl(struct audio_device *adev,
904 int pcm_device_id, int app_type,
905 int acdb_dev_id, int sample_rate,
906 int stream_type,
907 snd_device_t snd_device)
908{
909
910 char mixer_ctl_name[MAX_LENGTH_MIXER_CONTROL_IN_INT];
911 struct mixer_ctl *ctl;
912 int app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT], len = 0, rc = 0;
913 int snd_device_be_idx = -1;
914
915 if (stream_type == PCM_PLAYBACK) {
916 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
917 "Audio Stream %d App Type Cfg", pcm_device_id);
918 } else if (stream_type == PCM_CAPTURE) {
919 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
920 "Audio Stream Capture %d App Type Cfg", pcm_device_id);
921 }
922
923 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
924 if (!ctl) {
925 ALOGE("%s: Could not get ctl for mixer cmd - %s",
926 __func__, mixer_ctl_name);
927 rc = -EINVAL;
928 goto exit;
929 }
930 app_type_cfg[len++] = app_type;
931 app_type_cfg[len++] = acdb_dev_id;
932 app_type_cfg[len++] = sample_rate;
933
934 snd_device_be_idx = platform_get_snd_device_backend_index(snd_device);
935 if (snd_device_be_idx > 0)
936 app_type_cfg[len++] = snd_device_be_idx;
937 ALOGV("%s: stream type %d app_type %d, acdb_dev_id %d "
938 "sample rate %d, snd_device_be_idx %d",
939 __func__, stream_type, app_type, acdb_dev_id, sample_rate,
940 snd_device_be_idx);
941 mixer_ctl_set_array(ctl, app_type_cfg, len);
942
943exit:
944 return rc;
945}
946
947static int audio_extn_utils_send_app_type_cfg_hfp(struct audio_device *adev,
948 struct audio_usecase *usecase)
949{
950 int pcm_device_id, acdb_dev_id = 0, snd_device = usecase->out_snd_device;
951 int32_t sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
952 int app_type = 0, rc = 0;
953
954 ALOGV("%s", __func__);
955
956 if (usecase->type != PCM_HFP_CALL) {
957 ALOGV("%s: not a playback or HFP path, no need to cfg app type", __func__);
958 rc = 0;
959 goto exit_send_app_type_cfg;
960 }
961 if ((usecase->id != USECASE_AUDIO_HFP_SCO) &&
962 (usecase->id != USECASE_AUDIO_HFP_SCO_WB)) {
963 ALOGV("%s: a playback path where app type cfg is not required", __func__);
964 rc = 0;
965 goto exit_send_app_type_cfg;
966 }
967
968 snd_device = usecase->out_snd_device;
969 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_PLAYBACK);
970
971 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
972 if (acdb_dev_id < 0) {
973 ALOGE("%s: Couldn't get the acdb dev id", __func__);
974 rc = -EINVAL;
975 goto exit_send_app_type_cfg;
976 }
977
978 if (usecase->type == PCM_HFP_CALL) {
979
980 /* config HFP session:1 playback path */
981 app_type = platform_get_default_app_type_v2(adev->platform, PCM_PLAYBACK);
982 sample_rate= CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
983 rc = set_stream_app_type_mixer_ctrl(adev, pcm_device_id, app_type,
984 acdb_dev_id, sample_rate,
985 PCM_PLAYBACK,
986 SND_DEVICE_NONE); // use legacy behavior
987 if (rc < 0)
988 goto exit_send_app_type_cfg;
989
990 /* config HFP session:1 capture path */
991 app_type = platform_get_default_app_type_v2(adev->platform, PCM_CAPTURE);
992 rc = set_stream_app_type_mixer_ctrl(adev, pcm_device_id, app_type,
993 acdb_dev_id, sample_rate,
994 PCM_CAPTURE,
995 SND_DEVICE_NONE);
996 if (rc < 0)
997 goto exit_send_app_type_cfg;
998
999 /* config HFP session:2 capture path */
1000 pcm_device_id = HFP_ASM_RX_TX;
1001 snd_device = usecase->in_snd_device;
1002 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
1003 if (acdb_dev_id <= 0) {
1004 ALOGE("%s: Couldn't get the acdb dev id", __func__);
1005 rc = -EINVAL;
1006 goto exit_send_app_type_cfg;
1007 }
1008 app_type = platform_get_default_app_type_v2(adev->platform, PCM_CAPTURE);
1009 rc = set_stream_app_type_mixer_ctrl(adev, pcm_device_id, app_type,
1010 acdb_dev_id, sample_rate, PCM_CAPTURE,
1011 SND_DEVICE_NONE);
1012 if (rc < 0)
1013 goto exit_send_app_type_cfg;
1014
1015 /* config HFP session:2 playback path */
1016 app_type = platform_get_default_app_type_v2(adev->platform, PCM_PLAYBACK);
1017 rc = set_stream_app_type_mixer_ctrl(adev, pcm_device_id, app_type,
1018 acdb_dev_id, sample_rate,
1019 PCM_PLAYBACK, SND_DEVICE_NONE);
1020 if (rc < 0)
1021 goto exit_send_app_type_cfg;
1022 }
1023
1024 rc = 0;
1025exit_send_app_type_cfg:
1026 return rc;
1027}
1028
Zhou Song06761dd2019-04-28 18:08:17 +08001029int audio_extn_utils_get_app_sample_rate_for_device(
1030 struct audio_device *adev,
1031 struct audio_usecase *usecase, int snd_device)
1032{
1033 char value[PROPERTY_VALUE_MAX] = {0};
1034 int sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
1035
1036 if ((usecase->type == PCM_PLAYBACK) && (usecase->stream.out != NULL)) {
1037 property_get("vendor.audio.playback.mch.downsample",value,"");
1038 if (!strncmp("true", value, sizeof("true"))) {
1039 if ((popcount(usecase->stream.out->channel_mask) > 2) &&
1040 (usecase->stream.out->app_type_cfg.sample_rate > CODEC_BACKEND_DEFAULT_SAMPLE_RATE) &&
1041 !(usecase->stream.out->flags &
1042 (audio_output_flags_t)AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH))
1043 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
1044 }
1045
1046 if (usecase->id == USECASE_AUDIO_PLAYBACK_VOIP) {
1047 usecase->stream.out->app_type_cfg.sample_rate = usecase->stream.out->sample_rate;
1048 } else if (usecase->stream.out->devices & AUDIO_DEVICE_OUT_SPEAKER) {
1049 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
1050 } else if ((snd_device == SND_DEVICE_OUT_HDMI ||
1051 snd_device == SND_DEVICE_OUT_USB_HEADSET ||
1052 snd_device == SND_DEVICE_OUT_DISPLAY_PORT) &&
1053 (usecase->stream.out->sample_rate >= OUTPUT_SAMPLING_RATE_44100)) {
1054 /*
1055 * To best utlize DSP, check if the stream sample rate is supported/multiple of
1056 * configured device sample rate, if not update the COPP rate to be equal to the
1057 * device sample rate, else open COPP at stream sample rate
1058 */
1059 platform_check_and_update_copp_sample_rate(adev->platform, snd_device,
1060 usecase->stream.out->sample_rate,
1061 &usecase->stream.out->app_type_cfg.sample_rate);
1062 } else if (((snd_device != SND_DEVICE_OUT_HEADPHONES_44_1 &&
1063 !audio_is_this_native_usecase(usecase)) &&
1064 usecase->stream.out->sample_rate == OUTPUT_SAMPLING_RATE_44100) ||
1065 (usecase->stream.out->sample_rate < OUTPUT_SAMPLING_RATE_44100)) {
1066 /* Reset to default if no native stream is active*/
1067 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
1068 } else if (usecase->stream.out->devices & AUDIO_DEVICE_OUT_ALL_A2DP) {
1069 /*
1070 * For a2dp playback get encoder sampling rate and set copp sampling rate,
1071 * for bit width use the stream param only.
1072 */
1073 audio_extn_a2dp_get_enc_sample_rate(&usecase->stream.out->app_type_cfg.sample_rate);
1074 ALOGI("%s using %d sample rate rate for A2DP CoPP",
1075 __func__, usecase->stream.out->app_type_cfg.sample_rate);
1076 }
1077 audio_extn_btsco_get_sample_rate(snd_device, &usecase->stream.out->app_type_cfg.sample_rate);
1078 sample_rate = usecase->stream.out->app_type_cfg.sample_rate;
1079
1080 if (((usecase->stream.out->format == AUDIO_FORMAT_E_AC3) ||
1081 (usecase->stream.out->format == AUDIO_FORMAT_E_AC3_JOC) ||
1082 (usecase->stream.out->format == AUDIO_FORMAT_DOLBY_TRUEHD))
1083 && audio_extn_passthru_is_passthrough_stream(usecase->stream.out)
1084 && !audio_extn_passthru_is_convert_supported(adev, usecase->stream.out)) {
1085 sample_rate = sample_rate * 4;
1086 if (sample_rate > HDMI_PASSTHROUGH_MAX_SAMPLE_RATE)
1087 sample_rate = HDMI_PASSTHROUGH_MAX_SAMPLE_RATE;
1088 }
1089 } else if (usecase->type == PCM_CAPTURE) {
1090 if (usecase->stream.in != NULL) {
1091 if (usecase->id == USECASE_AUDIO_RECORD_VOIP)
1092 usecase->stream.in->app_type_cfg.sample_rate = usecase->stream.in->sample_rate;
1093 if (voice_is_in_call_rec_stream(usecase->stream.in)) {
1094 audio_extn_btsco_get_sample_rate(usecase->in_snd_device,
1095 &usecase->stream.in->app_type_cfg.sample_rate);
1096 } else {
1097 audio_extn_btsco_get_sample_rate(snd_device,
1098 &usecase->stream.in->app_type_cfg.sample_rate);
1099 }
1100 sample_rate = usecase->stream.in->app_type_cfg.sample_rate;
1101 } else if (usecase->id == USECASE_AUDIO_SPKR_CALIB_TX) {
1102 sample_rate = SAMPLE_RATE_8000;
1103 }
1104 } else if (usecase->type == TRANSCODE_LOOPBACK_RX) {
1105 sample_rate = usecase->stream.inout->out_config.sample_rate;
1106 }
1107 return sample_rate;
1108}
1109
Siena Richard7c2db772016-12-21 11:32:34 -08001110static int send_app_type_cfg_for_device(struct audio_device *adev,
1111 struct audio_usecase *usecase,
1112 int split_snd_device)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001113{
1114 char mixer_ctl_name[MAX_LENGTH_MIXER_CONTROL_IN_INT];
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301115 size_t app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT] = {0};
1116 int len = 0, rc;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001117 struct mixer_ctl *ctl;
Siena Richard7c2db772016-12-21 11:32:34 -08001118 int pcm_device_id = 0, acdb_dev_id, app_type;
1119 int snd_device = split_snd_device, snd_device_be_idx = -1;
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +05301120 int32_t sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Varun Balaraje49253e2017-07-06 19:48:56 +05301121 struct streams_io_cfg *s_info = NULL;
1122 struct listnode *node = NULL;
Nikhil Laturkarac4a7492017-08-31 18:27:19 +05301123 int bd_app_type = 0;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001124
Siena Richard7c2db772016-12-21 11:32:34 -08001125 ALOGV("%s: usecase->out_snd_device %s, usecase->in_snd_device %s, split_snd_device %s",
1126 __func__, platform_get_snd_device_name(usecase->out_snd_device),
1127 platform_get_snd_device_name(usecase->in_snd_device),
1128 platform_get_snd_device_name(split_snd_device));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001129
Siddartha Shaik343abc62017-08-08 11:15:25 +05301130 if (usecase->type != PCM_PLAYBACK && usecase->type != PCM_CAPTURE &&
Surendar Karka93cd25a2018-08-28 14:21:37 +05301131 usecase->type != TRANSCODE_LOOPBACK_RX) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301132 ALOGE("%s: not a playback/capture path, no need to cfg app type", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001133 rc = 0;
1134 goto exit_send_app_type_cfg;
1135 }
1136 if ((usecase->id != USECASE_AUDIO_PLAYBACK_DEEP_BUFFER) &&
Vignesh Kulothungan0a2eff02019-07-11 16:30:13 -07001137 (usecase->id != USECASE_AUDIO_PLAYBACK_WITH_HAPTICS) &&
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001138 (usecase->id != USECASE_AUDIO_PLAYBACK_LOW_LATENCY) &&
1139 (usecase->id != USECASE_AUDIO_PLAYBACK_MULTI_CH) &&
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -08001140 (usecase->id != USECASE_AUDIO_PLAYBACK_ULL) &&
Vikram Panduranga93f080e2017-06-07 18:16:14 -07001141 (usecase->id != USECASE_AUDIO_PLAYBACK_VOIP) &&
Surendar Karka93cd25a2018-08-28 14:21:37 +05301142 (usecase->id != USECASE_AUDIO_TRANSCODE_LOOPBACK_RX) &&
Varun Balaraje49253e2017-07-06 19:48:56 +05301143 (!is_interactive_usecase(usecase->id)) &&
Srikanth Uyyala9d551402015-08-25 16:03:42 +05301144 (!is_offload_usecase(usecase->id)) &&
Derek Chenf6318be2017-06-12 17:16:24 -04001145 (usecase->type != PCM_CAPTURE) &&
1146 (!audio_extn_auto_hal_is_bus_device_usecase(usecase->id))) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301147 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 -07001148 rc = 0;
1149 goto exit_send_app_type_cfg;
1150 }
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -08001151
1152 //if VR is active then only send the mixer control
1153 if (usecase->id == USECASE_AUDIO_PLAYBACK_ULL && !audio_is_vr_mode_on(adev)) {
1154 ALOGI("ULL doesnt need sending app type cfg, returning");
1155 rc = 0;
1156 goto exit_send_app_type_cfg;
1157 }
1158
Surendar Karka93cd25a2018-08-28 14:21:37 +05301159 if (usecase->type == PCM_PLAYBACK || usecase->type == TRANSCODE_LOOPBACK_RX) {
Ben Romberger1fafdde2015-09-09 19:43:15 -07001160 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_PLAYBACK);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301161 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
1162 "Audio Stream %d App Type Cfg", pcm_device_id);
Ben Romberger1fafdde2015-09-09 19:43:15 -07001163 } else if (usecase->type == PCM_CAPTURE) {
Ben Romberger1fafdde2015-09-09 19:43:15 -07001164 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_CAPTURE);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301165 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
1166 "Audio Stream Capture %d App Type Cfg", pcm_device_id);
Srikanth Uyyala9d551402015-08-25 16:03:42 +05301167 }
Siena Richard7c2db772016-12-21 11:32:34 -08001168
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001169 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
1170 if (!ctl) {
1171 ALOGE("%s: Could not get ctl for mixer cmd - %s", __func__,
1172 mixer_ctl_name);
1173 rc = -EINVAL;
1174 goto exit_send_app_type_cfg;
1175 }
Aditya Bavanari701a6992017-03-30 19:17:16 +05301176 snd_device = platform_get_spkr_prot_snd_device(snd_device);
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301177 if (voice_is_in_call_rec_stream(usecase->stream.in) && usecase->type == PCM_CAPTURE) {
1178 snd_device_t voice_device = voice_get_incall_rec_snd_device(usecase->in_snd_device);
1179 acdb_dev_id = platform_get_snd_device_acdb_id(voice_device);
1180 ALOGV("acdb id for voice call use case %d", acdb_dev_id);
1181 } else {
1182 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
1183 }
Rohit Kumar1181b292017-01-31 18:07:17 +05301184 if (acdb_dev_id <= 0) {
1185 ALOGE("%s: Couldn't get the acdb dev id", __func__);
1186 rc = -EINVAL;
1187 goto exit_send_app_type_cfg;
1188 }
1189
Siena Richard7c2db772016-12-21 11:32:34 -08001190 snd_device_be_idx = platform_get_snd_device_backend_index(snd_device);
1191 if (snd_device_be_idx < 0) {
1192 ALOGE("%s: Couldn't get the backend index for snd device %s ret=%d",
1193 __func__, platform_get_snd_device_name(snd_device),
1194 snd_device_be_idx);
1195 }
1196
Zhou Song06761dd2019-04-28 18:08:17 +08001197 sample_rate = audio_extn_utils_get_app_sample_rate_for_device(adev, usecase, snd_device);
1198
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301199 if ((usecase->type == PCM_PLAYBACK) && (usecase->stream.out != NULL)) {
Varun Balaraje49253e2017-07-06 19:48:56 +05301200 /* Interactive streams are supported with only direct app type id.
1201 * Get Direct profile app type and use it for interactive streams
1202 */
1203 list_for_each(node, &adev->streams_output_cfg_list) {
1204 s_info = node_to_item(node, struct streams_io_cfg, list);
Aalique Grahame5cd12ff2017-10-19 10:57:00 -07001205 if (s_info->flags.out_flags == (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_BD |
Nikhil Laturkarac4a7492017-08-31 18:27:19 +05301206 AUDIO_OUTPUT_FLAG_DIRECT_PCM |
1207 AUDIO_OUTPUT_FLAG_DIRECT))
1208 bd_app_type = s_info->app_type_cfg.app_type;
Varun Balaraje49253e2017-07-06 19:48:56 +05301209 }
Aalique Grahame5cd12ff2017-10-19 10:57:00 -07001210 if (usecase->stream.out->flags == (audio_output_flags_t)AUDIO_OUTPUT_FLAG_INTERACTIVE)
Nikhil Laturkarac4a7492017-08-31 18:27:19 +05301211 app_type = bd_app_type;
Varun Balaraje49253e2017-07-06 19:48:56 +05301212 else
1213 app_type = usecase->stream.out->app_type_cfg.app_type;
Siena Richard7c2db772016-12-21 11:32:34 -08001214 app_type_cfg[len++] = app_type;
Mingming Yin21854652016-04-13 11:54:02 -07001215 app_type_cfg[len++] = acdb_dev_id;
Naresh Tanniru3a406772017-05-10 13:09:05 -07001216 app_type_cfg[len++] = sample_rate;
1217
Siena Richard7c2db772016-12-21 11:32:34 -08001218 if (snd_device_be_idx > 0)
1219 app_type_cfg[len++] = snd_device_be_idx;
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301220
Siena Richard7c2db772016-12-21 11:32:34 -08001221 ALOGI("%s PLAYBACK app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
1222 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301223
1224 } else if ((usecase->type == PCM_CAPTURE) && (usecase->stream.in != NULL)) {
Siena Richard7c2db772016-12-21 11:32:34 -08001225 app_type = usecase->stream.in->app_type_cfg.app_type;
1226 app_type_cfg[len++] = app_type;
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301227 app_type_cfg[len++] = acdb_dev_id;
Siena Richard7c2db772016-12-21 11:32:34 -08001228 app_type_cfg[len++] = sample_rate;
1229 if (snd_device_be_idx > 0)
1230 app_type_cfg[len++] = snd_device_be_idx;
1231 ALOGI("%s CAPTURE app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
1232 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301233 } else {
Siena Richard7c2db772016-12-21 11:32:34 -08001234 app_type = platform_get_default_app_type_v2(adev->platform, usecase->type);
Surendar Karka93cd25a2018-08-28 14:21:37 +05301235 if(usecase->type == TRANSCODE_LOOPBACK_RX) {
Siddartha Shaik343abc62017-08-08 11:15:25 +05301236 app_type = usecase->stream.inout->out_app_type_cfg.app_type;
1237 }
Siena Richard7c2db772016-12-21 11:32:34 -08001238 app_type_cfg[len++] = app_type;
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301239 app_type_cfg[len++] = acdb_dev_id;
1240 app_type_cfg[len++] = sample_rate;
Siena Richard7c2db772016-12-21 11:32:34 -08001241 if (snd_device_be_idx > 0)
1242 app_type_cfg[len++] = snd_device_be_idx;
1243 ALOGI("%s default app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
1244 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Ashish Jainc02430d2016-01-04 10:42:43 +05301245 }
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301246
Siddartha Shaik343abc62017-08-08 11:15:25 +05301247 if(ctl)
1248 mixer_ctl_set_array(ctl, app_type_cfg, len);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001249 rc = 0;
1250exit_send_app_type_cfg:
1251 return rc;
1252}
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001253
Deeraj Soman14230922019-01-30 16:39:30 +05301254static int audio_extn_utils_check_input_parameters(uint32_t sample_rate,
1255 audio_format_t format,
1256 int channel_count)
1257{
1258 int ret = 0;
1259
1260 if (((format != AUDIO_FORMAT_PCM_16_BIT) && (format != AUDIO_FORMAT_PCM_8_24_BIT) &&
1261 (format != AUDIO_FORMAT_PCM_24_BIT_PACKED) && (format != AUDIO_FORMAT_PCM_32_BIT) &&
1262 (format != AUDIO_FORMAT_PCM_FLOAT)) &&
1263 !voice_extn_compress_voip_is_format_supported(format) &&
1264 !audio_extn_compr_cap_format_supported(format) &&
1265 !audio_extn_cin_format_supported(format))
1266 ret = -EINVAL;
1267
1268 switch (channel_count) {
1269 case 1:
1270 case 2:
1271 case 3:
1272 case 4:
1273 case 6:
1274 case 8:
1275 break;
1276 default:
1277 ret = -EINVAL;
1278 }
1279
1280 switch (sample_rate) {
1281 case 8000:
1282 case 11025:
1283 case 12000:
1284 case 16000:
1285 case 22050:
1286 case 24000:
1287 case 32000:
1288 case 44100:
1289 case 48000:
1290 case 88200:
1291 case 96000:
1292 case 176400:
1293 case 192000:
1294 break;
1295 default:
1296 ret = -EINVAL;
1297 }
1298
1299 return ret;
1300}
1301
1302static inline uint32_t audio_extn_utils_nearest_multiple(uint32_t num, uint32_t multiplier)
1303{
1304 uint32_t remainder = 0;
1305
1306 if (!multiplier)
1307 return num;
1308
1309 remainder = num % multiplier;
1310 if (remainder)
1311 num += (multiplier - remainder);
1312
1313 return num;
1314}
1315
1316static inline uint32_t audio_extn_utils_lcm(uint32_t num1, uint32_t num2)
1317{
1318 uint32_t high = num1, low = num2, temp = 0;
1319
1320 if (!num1 || !num2)
1321 return 0;
1322
1323 if (num1 < num2) {
1324 high = num2;
1325 low = num1;
1326 }
1327
1328 while (low != 0) {
1329 temp = low;
1330 low = high % low;
1331 high = temp;
1332 }
1333 return (num1 * num2)/high;
1334}
1335
Siena Richard7c2db772016-12-21 11:32:34 -08001336int audio_extn_utils_send_app_type_cfg(struct audio_device *adev,
1337 struct audio_usecase *usecase)
1338{
1339 int i, num_devices = 0;
1340 snd_device_t new_snd_devices[SND_DEVICE_OUT_END] = {0};
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301341 snd_device_t in_snd_device = usecase->in_snd_device;
Siena Richard7c2db772016-12-21 11:32:34 -08001342 int rc = 0;
1343
Aalique Grahame22e49102018-12-18 14:23:57 -08001344 if (usecase->type == PCM_HFP_CALL) {
1345 return audio_extn_utils_send_app_type_cfg_hfp(adev, usecase);
1346 }
1347
Siena Richard7c2db772016-12-21 11:32:34 -08001348 switch (usecase->type) {
1349 case PCM_PLAYBACK:
Surendar Karka93cd25a2018-08-28 14:21:37 +05301350 case TRANSCODE_LOOPBACK_RX:
Siena Richard7c2db772016-12-21 11:32:34 -08001351 ALOGD("%s: usecase->out_snd_device %s",
1352 __func__, platform_get_snd_device_name(usecase->out_snd_device));
1353 /* check for out combo device */
1354 if (platform_split_snd_device(adev->platform,
1355 usecase->out_snd_device,
1356 &num_devices, new_snd_devices)) {
1357 new_snd_devices[0] = usecase->out_snd_device;
1358 num_devices = 1;
1359 }
1360 break;
1361 case PCM_CAPTURE:
1362 ALOGD("%s: usecase->in_snd_device %s",
1363 __func__, platform_get_snd_device_name(usecase->in_snd_device));
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301364 if (voice_is_in_call_rec_stream(usecase->stream.in)) {
1365 in_snd_device = voice_get_incall_rec_backend_device(usecase->stream.in);
1366 }
Siena Richard7c2db772016-12-21 11:32:34 -08001367 /* check for in combo device */
1368 if (platform_split_snd_device(adev->platform,
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301369 in_snd_device,
Siena Richard7c2db772016-12-21 11:32:34 -08001370 &num_devices, new_snd_devices)) {
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301371 new_snd_devices[0] = in_snd_device;
Siena Richard7c2db772016-12-21 11:32:34 -08001372 num_devices = 1;
1373 }
1374 break;
1375 default:
1376 ALOGI("%s: not a playback/capture path, no need to cfg app type", __func__);
1377 rc = 0;
1378 break;
1379 }
1380
1381 for (i = 0; i < num_devices; i++) {
1382 rc = send_app_type_cfg_for_device(adev, usecase, new_snd_devices[i]);
1383 if (rc)
1384 break;
1385 }
1386
1387 return rc;
1388}
1389
Preetam Singh Ranawat9519e9c2015-11-18 16:05:55 +05301390int read_line_from_file(const char *path, char *buf, size_t count)
1391{
1392 char * fgets_ret;
1393 FILE * fd;
1394 int rv;
1395
1396 fd = fopen(path, "r");
1397 if (fd == NULL)
1398 return -1;
1399
1400 fgets_ret = fgets(buf, (int)count, fd);
1401 if (NULL != fgets_ret) {
1402 rv = (int)strlen(buf);
1403 } else {
1404 rv = ferror(fd);
1405 }
1406 fclose(fd);
1407
1408 return rv;
1409}
1410
Ashish Jainf1eaa582016-05-23 20:54:24 +05301411/*Translates ALSA formats to AOSP PCM formats*/
1412audio_format_t alsa_format_to_hal(uint32_t alsa_format)
1413{
1414 audio_format_t format;
1415
1416 switch(alsa_format) {
1417 case SNDRV_PCM_FORMAT_S16_LE:
1418 format = AUDIO_FORMAT_PCM_16_BIT;
1419 break;
1420 case SNDRV_PCM_FORMAT_S24_3LE:
1421 format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
1422 break;
1423 case SNDRV_PCM_FORMAT_S24_LE:
1424 format = AUDIO_FORMAT_PCM_8_24_BIT;
1425 break;
1426 case SNDRV_PCM_FORMAT_S32_LE:
1427 format = AUDIO_FORMAT_PCM_32_BIT;
1428 break;
1429 default:
1430 ALOGW("Incorrect ALSA format");
1431 format = AUDIO_FORMAT_INVALID;
1432 }
1433 return format;
1434}
1435
1436/*Translates hal format (AOSP) to alsa formats*/
1437uint32_t hal_format_to_alsa(audio_format_t hal_format)
1438{
1439 uint32_t alsa_format;
1440
1441 switch (hal_format) {
1442 case AUDIO_FORMAT_PCM_32_BIT: {
1443 if (platform_supports_true_32bit())
1444 alsa_format = SNDRV_PCM_FORMAT_S32_LE;
1445 else
1446 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1447 }
1448 break;
1449 case AUDIO_FORMAT_PCM_8_BIT:
1450 alsa_format = SNDRV_PCM_FORMAT_S8;
1451 break;
1452 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
1453 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1454 break;
1455 case AUDIO_FORMAT_PCM_8_24_BIT: {
1456 if (platform_supports_true_32bit())
1457 alsa_format = SNDRV_PCM_FORMAT_S32_LE;
1458 else
1459 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1460 }
1461 break;
1462 case AUDIO_FORMAT_PCM_FLOAT:
1463 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1464 break;
1465 default:
1466 case AUDIO_FORMAT_PCM_16_BIT:
1467 alsa_format = SNDRV_PCM_FORMAT_S16_LE;
1468 break;
1469 }
1470 return alsa_format;
1471}
1472
Ashish Jain83a6cc22016-06-28 14:34:17 +05301473/*Translates PCM formats to AOSP formats*/
1474audio_format_t pcm_format_to_hal(uint32_t pcm_format)
1475{
1476 audio_format_t format = AUDIO_FORMAT_INVALID;
1477
1478 switch(pcm_format) {
1479 case PCM_FORMAT_S16_LE:
1480 format = AUDIO_FORMAT_PCM_16_BIT;
1481 break;
1482 case PCM_FORMAT_S24_3LE:
1483 format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
1484 break;
1485 case PCM_FORMAT_S24_LE:
1486 format = AUDIO_FORMAT_PCM_8_24_BIT;
1487 break;
1488 case PCM_FORMAT_S32_LE:
1489 format = AUDIO_FORMAT_PCM_32_BIT;
1490 break;
1491 default:
1492 ALOGW("Incorrect PCM format");
1493 format = AUDIO_FORMAT_INVALID;
1494 }
1495 return format;
1496}
1497
1498/*Translates hal format (AOSP) to alsa formats*/
1499uint32_t hal_format_to_pcm(audio_format_t hal_format)
1500{
1501 uint32_t pcm_format;
1502
1503 switch (hal_format) {
1504 case AUDIO_FORMAT_PCM_32_BIT:
1505 case AUDIO_FORMAT_PCM_8_24_BIT:
1506 case AUDIO_FORMAT_PCM_FLOAT: {
1507 if (platform_supports_true_32bit())
1508 pcm_format = PCM_FORMAT_S32_LE;
1509 else
1510 pcm_format = PCM_FORMAT_S24_3LE;
1511 }
1512 break;
1513 case AUDIO_FORMAT_PCM_8_BIT:
1514 pcm_format = PCM_FORMAT_S8;
1515 break;
1516 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
1517 pcm_format = PCM_FORMAT_S24_3LE;
1518 break;
1519 default:
1520 case AUDIO_FORMAT_PCM_16_BIT:
1521 pcm_format = PCM_FORMAT_S16_LE;
1522 break;
1523 }
1524 return pcm_format;
1525}
1526
Ashish Jainf1eaa582016-05-23 20:54:24 +05301527uint32_t get_alsa_fragment_size(uint32_t bytes_per_sample,
1528 uint32_t sample_rate,
Deeraj Soman65358ab2019-02-07 15:40:49 +05301529 uint32_t noOfChannels,
1530 int64_t duration_ms)
Ashish Jainf1eaa582016-05-23 20:54:24 +05301531{
1532 uint32_t fragment_size = 0;
1533 uint32_t pcm_offload_time = PCM_OFFLOAD_BUFFER_DURATION;
1534
Deeraj Soman65358ab2019-02-07 15:40:49 +05301535 if (duration_ms >= MIN_OFFLOAD_BUFFER_DURATION_MS && duration_ms <= MAX_OFFLOAD_BUFFER_DURATION_MS)
1536 pcm_offload_time = duration_ms;
1537
Ashish Jainf1eaa582016-05-23 20:54:24 +05301538 fragment_size = (pcm_offload_time
1539 * sample_rate
1540 * bytes_per_sample
1541 * noOfChannels)/1000;
1542 if (fragment_size < MIN_PCM_OFFLOAD_FRAGMENT_SIZE)
1543 fragment_size = MIN_PCM_OFFLOAD_FRAGMENT_SIZE;
1544 else if (fragment_size > MAX_PCM_OFFLOAD_FRAGMENT_SIZE)
1545 fragment_size = MAX_PCM_OFFLOAD_FRAGMENT_SIZE;
1546 /*To have same PCM samples for all channels, the buffer size requires to
1547 *be multiple of (number of channels * bytes per sample)
1548 *For writes to succeed, the buffer must be written at address which is multiple of 32
1549 */
Aalique Grahameb85f2d92017-10-16 17:53:23 -07001550 fragment_size = ALIGN(fragment_size, (bytes_per_sample * noOfChannels * 32));
Ashish Jainf1eaa582016-05-23 20:54:24 +05301551
1552 ALOGI("PCM offload Fragment size to %d bytes", fragment_size);
1553 return fragment_size;
1554}
1555
1556/* Calculates the fragment size required to configure compress session.
1557 * Based on the alsa format selected, decide if conversion is needed in
1558
1559 * HAL ( e.g. convert AUDIO_FORMAT_PCM_FLOAT input format to
1560 * AUDIO_FORMAT_PCM_24_BIT_PACKED before writing to the compress driver.
1561 */
1562void audio_extn_utils_update_direct_pcm_fragment_size(struct stream_out *out)
1563{
Ashish Jain83a6cc22016-06-28 14:34:17 +05301564 audio_format_t dst_format = out->hal_op_format;
1565 audio_format_t src_format = out->hal_ip_format;
Ashish Jainf1eaa582016-05-23 20:54:24 +05301566 uint32_t hal_op_bytes_per_sample = audio_bytes_per_sample(dst_format);
1567 uint32_t hal_ip_bytes_per_sample = audio_bytes_per_sample(src_format);
1568
1569 out->compr_config.fragment_size =
1570 get_alsa_fragment_size(hal_op_bytes_per_sample,
1571 out->sample_rate,
Deeraj Soman65358ab2019-02-07 15:40:49 +05301572 popcount(out->channel_mask),
1573 out->info.duration_us / 1000);
Ashish Jainf1eaa582016-05-23 20:54:24 +05301574
1575 if ((src_format != dst_format) &&
1576 hal_op_bytes_per_sample != hal_ip_bytes_per_sample) {
1577
Ashish Jain83a6cc22016-06-28 14:34:17 +05301578 out->hal_fragment_size =
Ashish Jainf1eaa582016-05-23 20:54:24 +05301579 ((out->compr_config.fragment_size * hal_ip_bytes_per_sample) /
1580 hal_op_bytes_per_sample);
1581 ALOGI("enable conversion hal_input_fragment_size is %d src_format %x dst_format %x",
Ashish Jain83a6cc22016-06-28 14:34:17 +05301582 out->hal_fragment_size, src_format, dst_format);
Ashish Jainf1eaa582016-05-23 20:54:24 +05301583 } else {
Ashish Jain83a6cc22016-06-28 14:34:17 +05301584 out->hal_fragment_size = out->compr_config.fragment_size;
Ashish Jainf1eaa582016-05-23 20:54:24 +05301585 }
1586}
1587
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301588/* converts pcm format 24_8 to 8_24 inplace */
1589size_t audio_extn_utils_convert_format_24_8_to_8_24(void *buf, size_t bytes)
1590{
1591 size_t i = 0;
1592 int *int_buf_stream = buf;
1593
1594 if ((bytes % 4) != 0) {
1595 ALOGE("%s: wrong inout buffer! ... is not 32 bit aligned ", __func__);
1596 return -EINVAL;
1597 }
1598
1599 for (; i < (bytes / 4); i++)
1600 int_buf_stream[i] >>= 8;
1601
1602 return bytes;
1603}
1604
1605int get_snd_codec_id(audio_format_t format)
1606{
1607 int id = 0;
1608
1609 switch (format & AUDIO_FORMAT_MAIN_MASK) {
1610 case AUDIO_FORMAT_MP3:
1611 id = SND_AUDIOCODEC_MP3;
1612 break;
1613 case AUDIO_FORMAT_AAC:
1614 id = SND_AUDIOCODEC_AAC;
1615 break;
1616 case AUDIO_FORMAT_AAC_ADTS:
1617 id = SND_AUDIOCODEC_AAC;
1618 break;
Arun Kumar Dasari3b174182016-12-27 13:01:14 +05301619 case AUDIO_FORMAT_AAC_LATM:
1620 id = SND_AUDIOCODEC_AAC;
1621 break;
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301622 case AUDIO_FORMAT_PCM:
1623 id = SND_AUDIOCODEC_PCM;
1624 break;
1625 case AUDIO_FORMAT_FLAC:
1626 id = SND_AUDIOCODEC_FLAC;
1627 break;
1628 case AUDIO_FORMAT_ALAC:
1629 id = SND_AUDIOCODEC_ALAC;
1630 break;
1631 case AUDIO_FORMAT_APE:
1632 id = SND_AUDIOCODEC_APE;
1633 break;
1634 case AUDIO_FORMAT_VORBIS:
1635 id = SND_AUDIOCODEC_VORBIS;
1636 break;
1637 case AUDIO_FORMAT_WMA:
1638 id = SND_AUDIOCODEC_WMA;
1639 break;
1640 case AUDIO_FORMAT_WMA_PRO:
1641 id = SND_AUDIOCODEC_WMA_PRO;
1642 break;
Satish Babu Patakokila0c313922016-12-08 12:07:08 +05301643 case AUDIO_FORMAT_MP2:
1644 id = SND_AUDIOCODEC_MP2;
1645 break;
Satish Babu Patakokila915ecba2017-01-10 17:43:56 +05301646 case AUDIO_FORMAT_AC3:
1647 id = SND_AUDIOCODEC_AC3;
1648 break;
1649 case AUDIO_FORMAT_E_AC3:
1650 case AUDIO_FORMAT_E_AC3_JOC:
1651 id = SND_AUDIOCODEC_EAC3;
1652 break;
1653 case AUDIO_FORMAT_DTS:
1654 case AUDIO_FORMAT_DTS_HD:
1655 id = SND_AUDIOCODEC_DTS;
1656 break;
Ben Romberger1aaaf862017-04-06 17:49:46 -07001657 case AUDIO_FORMAT_DOLBY_TRUEHD:
1658 id = SND_AUDIOCODEC_TRUEHD;
1659 break;
Naresh Tanniru928f0862017-04-07 16:44:23 -07001660 case AUDIO_FORMAT_IEC61937:
1661 id = SND_AUDIOCODEC_IEC61937;
1662 break;
Preetam Singh Ranawat4277a5a2017-01-18 19:02:24 +05301663 case AUDIO_FORMAT_DSD:
1664 id = SND_AUDIOCODEC_DSD;
1665 break;
Dhanalakshmi Siddani18737932016-11-29 17:33:17 +05301666 case AUDIO_FORMAT_APTX:
1667 id = SND_AUDIOCODEC_APTX;
1668 break;
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301669 default:
1670 ALOGE("%s: Unsupported audio format :%x", __func__, format);
1671 }
1672
1673 return id;
1674}
1675
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001676void audio_extn_utils_send_audio_calibration(struct audio_device *adev,
1677 struct audio_usecase *usecase)
1678{
1679 int type = usecase->type;
1680
Dhananjay Kumar14245982017-01-16 20:21:00 +05301681 if (type == PCM_PLAYBACK && usecase->stream.out != NULL) {
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05301682 platform_send_audio_calibration(adev->platform, usecase,
Zhou Song06761dd2019-04-28 18:08:17 +08001683 usecase->stream.out->app_type_cfg.app_type);
Dhananjay Kumar14245982017-01-16 20:21:00 +05301684 } else if (type == PCM_CAPTURE && usecase->stream.in != NULL) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301685 platform_send_audio_calibration(adev->platform, usecase,
Zhou Song06761dd2019-04-28 18:08:17 +08001686 usecase->stream.in->app_type_cfg.app_type);
1687 } else if ((type == PCM_HFP_CALL) || (type == PCM_CAPTURE) ||
1688 (type == TRANSCODE_LOOPBACK_RX && usecase->stream.inout != NULL)) {
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05301689 platform_send_audio_calibration(adev->platform, usecase,
Zhou Song06761dd2019-04-28 18:08:17 +08001690 platform_get_default_app_type_v2(adev->platform, usecase->type));
Vidyakumar Athotae57f6002017-03-01 13:13:16 -08001691 } else {
1692 /* No need to send audio calibration for voice and voip call usecases */
1693 if ((type != VOICE_CALL) && (type != VOIP_CALL))
1694 ALOGW("%s: No audio calibration for usecase type = %d", __func__, type);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001695 }
1696}
1697
Ben Rombergera04fabc2014-11-14 12:16:03 -08001698// Base64 Encode and Decode
1699// Not all features supported. This must be used only with following conditions.
1700// Decode Modes: Support with and without padding
1701// CRLF not handling. So no CRLF in string to decode.
1702// Encode Modes: Supports only padding
1703int b64decode(char *inp, int ilen, uint8_t* outp)
1704{
1705 int i, j, k, ii, num;
1706 int rem, pcnt;
1707 uint32_t res=0;
1708 uint8_t getIndex[MAX_BASEINDEX_LEN];
1709 uint8_t tmp, cflag;
1710
1711 if(inp == NULL || outp == NULL || ilen <= 0) {
1712 ALOGE("[%s] received NULL pointer or zero length",__func__);
1713 return -1;
1714 }
1715
1716 memset(getIndex, MAX_BASEINDEX_LEN-1, sizeof(getIndex));
1717 for(i=0;i<BASE_TABLE_SIZE;i++) {
1718 getIndex[(uint8_t)bTable[i]] = (uint8_t)i;
1719 }
1720 getIndex[(uint8_t)'=']=0;
1721
1722 j=0;k=0;
1723 num = ilen/4;
1724 rem = ilen%4;
1725 if(rem==0)
1726 num = num-1;
1727 cflag=0;
1728 for(i=0; i<num; i++) {
1729 res=0;
1730 for(ii=0;ii<4;ii++) {
1731 res = res << 6;
1732 tmp = getIndex[(uint8_t)inp[j++]];
1733 res = res | tmp;
1734 cflag = cflag | tmp;
1735 }
1736 outp[k++] = (res >> 16)&0xFF;
1737 outp[k++] = (res >> 8)&0xFF;
1738 outp[k++] = res & 0xFF;
1739 }
1740
1741 // Handle last bytes special
1742 pcnt=0;
1743 if(rem == 0) {
1744 //With padding or full data
1745 res = 0;
1746 for(ii=0;ii<4;ii++) {
1747 if(inp[j] == '=')
1748 pcnt++;
1749 res = res << 6;
1750 tmp = getIndex[(uint8_t)inp[j++]];
1751 res = res | tmp;
1752 cflag = cflag | tmp;
1753 }
1754 outp[k++] = res >> 16;
1755 if(pcnt == 2)
1756 goto done;
1757 outp[k++] = (res>>8)&0xFF;
1758 if(pcnt == 1)
1759 goto done;
1760 outp[k++] = res&0xFF;
1761 } else {
1762 //without padding
1763 res = 0;
1764 for(i=0;i<rem;i++) {
1765 res = res << 6;
1766 tmp = getIndex[(uint8_t)inp[j++]];
1767 res = res | tmp;
1768 cflag = cflag | tmp;
1769 }
1770 for(i=rem;i<4;i++) {
1771 res = res << 6;
1772 pcnt++;
1773 }
1774 outp[k++] = res >> 16;
1775 if(pcnt == 2)
1776 goto done;
1777 outp[k++] = (res>>8)&0xFF;
1778 if(pcnt == 1)
1779 goto done;
1780 outp[k++] = res&0xFF;
1781 }
1782done:
1783 if(cflag == 0xFF) {
1784 ALOGE("[%s] base64 decode failed. Invalid character found %s",
1785 __func__, inp);
1786 return 0;
1787 }
1788 return k;
1789}
1790
1791int b64encode(uint8_t *inp, int ilen, char* outp)
1792{
1793 int i,j,k, num;
1794 int rem=0;
1795 uint32_t res=0;
1796
1797 if(inp == NULL || outp == NULL || ilen<=0) {
1798 ALOGE("[%s] received NULL pointer or zero input length",__func__);
1799 return -1;
1800 }
1801
1802 num = ilen/3;
1803 rem = ilen%3;
1804 j=0;k=0;
1805 for(i=0; i<num; i++) {
1806 //prepare index
1807 res = inp[j++]<<16;
1808 res = res | inp[j++]<<8;
1809 res = res | inp[j++];
1810 //get output map from index
1811 outp[k++] = (char) bTable[(res>>18)&0x3F];
1812 outp[k++] = (char) bTable[(res>>12)&0x3F];
1813 outp[k++] = (char) bTable[(res>>6)&0x3F];
1814 outp[k++] = (char) bTable[res&0x3F];
1815 }
1816
1817 switch(rem) {
1818 case 1:
1819 res = inp[j++]<<16;
1820 outp[k++] = (char) bTable[res>>18];
1821 outp[k++] = (char) bTable[(res>>12)&0x3F];
1822 //outp[k++] = '=';
1823 //outp[k++] = '=';
1824 break;
1825 case 2:
1826 res = inp[j++]<<16;
1827 res = res | inp[j++]<<8;
1828 outp[k++] = (char) bTable[res>>18];
1829 outp[k++] = (char) bTable[(res>>12)&0x3F];
1830 outp[k++] = (char) bTable[(res>>6)&0x3F];
1831 //outp[k++] = '=';
1832 break;
1833 default:
1834 break;
1835 }
Ben Rombergera04fabc2014-11-14 12:16:03 -08001836 outp[k] = '\0';
1837 return k;
1838}
Ashish Jain81eb2a82015-05-13 10:52:34 +05301839
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301840
1841int audio_extn_utils_get_codec_version(const char *snd_card_name,
1842 int card_num,
1843 char *codec_version)
1844{
1845 char procfs_path[50];
1846 FILE *fp;
1847
1848 if (strstr(snd_card_name, "tasha")) {
1849 snprintf(procfs_path, sizeof(procfs_path),
1850 "/proc/asound/card%d/codecs/tasha/version", card_num);
1851 if ((fp = fopen(procfs_path, "r")) != NULL) {
1852 fgets(codec_version, CODEC_VERSION_MAX_LENGTH, fp);
1853 fclose(fp);
1854 } else {
1855 ALOGE("%s: ERROR. cannot open %s", __func__, procfs_path);
1856 return -ENOENT;
1857 }
1858 ALOGD("%s: codec version %s", __func__, codec_version);
1859 }
1860
1861 return 0;
1862}
1863
1864
Ashish Jain81eb2a82015-05-13 10:52:34 +05301865#ifdef AUDIO_EXTERNAL_HDMI_ENABLED
1866
1867void get_default_compressed_channel_status(
1868 unsigned char *channel_status)
1869{
Ashish Jain81eb2a82015-05-13 10:52:34 +05301870 memset(channel_status,0,24);
1871
1872 /* block start bit in preamble bit 3 */
1873 channel_status[0] |= PROFESSIONAL;
1874 //compre out
1875 channel_status[0] |= NON_LPCM;
1876 // sample rate; fixed 48K for default/transcode
1877 channel_status[3] |= SR_48000;
1878}
1879
Ashish Jain81eb2a82015-05-13 10:52:34 +05301880int32_t get_compressed_channel_status(void *audio_stream_data,
1881 uint32_t audio_frame_size,
1882 unsigned char *channel_status,
1883 enum audio_parser_code_type codec_type)
1884 // codec_type - AUDIO_PARSER_CODEC_AC3
1885 // - AUDIO_PARSER_CODEC_DTS
1886{
1887 unsigned char *stream;
1888 int ret = 0;
1889 stream = (unsigned char *)audio_stream_data;
1890
1891 if (audio_stream_data == NULL || audio_frame_size == 0) {
1892 ALOGW("no buffer to get channel status, return default for compress");
1893 get_default_compressed_channel_status(channel_status);
1894 return ret;
1895 }
1896
1897 memset(channel_status,0,24);
1898 if(init_audio_parser(stream, audio_frame_size, codec_type) == -1)
1899 {
1900 ALOGE("init audio parser failed");
1901 return -1;
1902 }
1903 ret = get_channel_status(channel_status, codec_type);
1904 return ret;
1905
1906}
1907
Ashish Jain81eb2a82015-05-13 10:52:34 +05301908void get_lpcm_channel_status(uint32_t sampleRate,
1909 unsigned char *channel_status)
1910{
1911 int32_t status = 0;
Ashish Jain81eb2a82015-05-13 10:52:34 +05301912 memset(channel_status,0,24);
1913 /* block start bit in preamble bit 3 */
1914 channel_status[0] |= PROFESSIONAL;
1915 //LPCM OUT
1916 channel_status[0] &= ~NON_LPCM;
1917
1918 switch (sampleRate) {
1919 case 8000:
1920 case 11025:
1921 case 12000:
1922 case 16000:
1923 case 22050:
1924 channel_status[3] |= SR_NOTID;
Preetam Singh Ranawat61716b12015-12-14 11:55:24 +05301925 break;
Ashish Jain81eb2a82015-05-13 10:52:34 +05301926 case 24000:
1927 channel_status[3] |= SR_24000;
1928 break;
1929 case 32000:
1930 channel_status[3] |= SR_32000;
1931 break;
1932 case 44100:
1933 channel_status[3] |= SR_44100;
1934 break;
1935 case 48000:
1936 channel_status[3] |= SR_48000;
1937 break;
1938 case 88200:
1939 channel_status[3] |= SR_88200;
1940 break;
1941 case 96000:
1942 channel_status[3] |= SR_96000;
1943 break;
1944 case 176400:
1945 channel_status[3] |= SR_176400;
1946 break;
1947 case 192000:
1948 channel_status[3] |= SR_192000;
1949 break;
1950 default:
1951 ALOGV("Invalid sample_rate %u\n", sampleRate);
1952 status = -1;
1953 break;
1954 }
1955}
1956
1957void audio_utils_set_hdmi_channel_status(struct stream_out *out, char * buffer, size_t bytes)
1958{
1959 unsigned char channel_status[24]={0};
1960 struct snd_aes_iec958 iec958;
1961 const char *mixer_ctl_name = "IEC958 Playback PCM Stream";
1962 struct mixer_ctl *ctl;
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +05301963 ALOGV("%s: buffer %s bytes %zd", __func__, buffer, bytes);
Arun Mirpurie008ed22019-03-21 11:21:04 -07001964
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05301965 if (audio_extn_utils_is_dolby_format(out->format) &&
Ashish Jain81eb2a82015-05-13 10:52:34 +05301966 /*TODO:Extend code to support DTS passthrough*/
1967 /*set compressed channel status bits*/
Arun Mirpurie008ed22019-03-21 11:21:04 -07001968 audio_extn_passthru_is_passthrough_stream(out) &&
1969 audio_extn_is_hdmi_passthru_enabled()) {
Ashish Jain81eb2a82015-05-13 10:52:34 +05301970 get_compressed_channel_status(buffer, bytes, channel_status, AUDIO_PARSER_CODEC_AC3);
Arun Mirpurie008ed22019-03-21 11:21:04 -07001971 } else {
Ashish Jain81eb2a82015-05-13 10:52:34 +05301972 /*set channel status bit for LPCM*/
1973 get_lpcm_channel_status(out->sample_rate, channel_status);
1974 }
1975
1976 memcpy(iec958.status, channel_status,sizeof(iec958.status));
1977 ctl = mixer_get_ctl_by_name(out->dev->mixer, mixer_ctl_name);
1978 if (!ctl) {
1979 ALOGE("%s: Could not get ctl for mixer cmd - %s",
1980 __func__, mixer_ctl_name);
1981 return;
1982 }
1983 if (mixer_ctl_set_array(ctl, &iec958, sizeof(iec958)) < 0) {
1984 ALOGE("%s: Could not set channel status for ext HDMI ",
1985 __func__);
1986 return;
1987 }
1988
1989}
1990#endif
Manish Dewangan3ccdea52017-02-13 19:31:54 +05301991
1992int audio_extn_utils_get_avt_device_drift(
1993 struct audio_usecase *usecase,
1994 struct audio_avt_device_drift_param *drift_param)
1995{
1996 int ret = 0, count = 0;
1997 char avt_device_drift_mixer_ctl_name[MIXER_PATH_MAX_LENGTH] = {0};
Naresh Tanniru6160c712017-04-17 15:43:48 +05301998 const char *backend = NULL;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05301999 struct mixer_ctl *ctl = NULL;
2000 struct audio_avt_device_drift_stats drift_stats;
2001 struct audio_device *adev = NULL;
2002
2003 if (usecase != NULL && usecase->type == PCM_PLAYBACK) {
Naresh Tanniru6160c712017-04-17 15:43:48 +05302004 backend = platform_get_snd_device_backend_interface(usecase->out_snd_device);
2005 if (!backend) {
2006 ALOGE("%s: Unsupported device %d", __func__,
2007 usecase->stream.out->devices);
2008 ret = -EINVAL;
2009 goto done;
2010 }
2011 strlcpy(avt_device_drift_mixer_ctl_name,
2012 backend,
2013 MIXER_PATH_MAX_LENGTH);
2014
2015 count = strlen(backend);
2016 if (MIXER_PATH_MAX_LENGTH - count > 0) {
2017 strlcat(&avt_device_drift_mixer_ctl_name[count],
2018 " DRIFT",
2019 MIXER_PATH_MAX_LENGTH - count);
2020 } else {
2021 ret = -EINVAL;
2022 goto done;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302023 }
2024 } else {
Naresh Tanniru7586e292017-04-13 10:38:13 +05302025 ALOGE("%s: Invalid usecase",__func__);
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302026 ret = -EINVAL;
Naresh Tanniru6160c712017-04-17 15:43:48 +05302027 goto done;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302028 }
2029
Naresh Tanniru6160c712017-04-17 15:43:48 +05302030 adev = usecase->stream.out->dev;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302031 ctl = mixer_get_ctl_by_name(adev->mixer, avt_device_drift_mixer_ctl_name);
2032 if (!ctl) {
2033 ALOGE("%s: Could not get ctl for mixer cmd - %s",
2034 __func__, avt_device_drift_mixer_ctl_name);
2035
2036 ret = -EINVAL;
2037 goto done;
2038 }
2039
2040 ALOGV("%s: Getting AV Timer vs Device Drift mixer ctrl name %s", __func__,
2041 avt_device_drift_mixer_ctl_name);
2042
2043 mixer_ctl_update(ctl);
2044 count = mixer_ctl_get_num_values(ctl);
2045 if (count != sizeof(struct audio_avt_device_drift_stats)) {
2046 ALOGE("%s: mixer_ctl_get_num_values() invalid drift_stats data size",
2047 __func__);
2048
2049 ret = -EINVAL;
2050 goto done;
2051 }
2052
2053 ret = mixer_ctl_get_array(ctl, (void *)&drift_stats, count);
2054 if (ret != 0) {
2055 ALOGE("%s: mixer_ctl_get_array() failed to get drift_stats Params",
2056 __func__);
2057
2058 ret = -EINVAL;
2059 goto done;
2060 }
2061 memcpy(drift_param, &drift_stats.drift_param,
2062 sizeof(struct audio_avt_device_drift_param));
2063done:
2064 return ret;
2065}
Manish Dewangan07de2142017-02-27 19:27:20 +05302066
2067#ifdef SNDRV_COMPRESS_PATH_DELAY
2068int audio_extn_utils_compress_get_dsp_latency(struct stream_out *out)
2069{
2070 int ret = -EINVAL;
2071 struct snd_compr_metadata metadata;
2072 int delay_ms = COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
2073
Weiyin Jiangc49a3b52018-08-17 16:16:08 +08002074 /* override the latency for pcm offload use case */
2075 if ((out->flags & AUDIO_OUTPUT_FLAG_DIRECT) &&
2076 !(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)) {
2077 delay_ms = PCM_OFFLOAD_PLAYBACK_LATENCY;
2078 }
2079
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -07002080 if (property_get_bool("vendor.audio.playback.dsp.pathdelay", false)) {
Manish Dewangan07de2142017-02-27 19:27:20 +05302081 ALOGD("%s:: Quering DSP delay %d",__func__, __LINE__);
2082 if (!(is_offload_usecase(out->usecase))) {
2083 ALOGE("%s:: not supported for non offload session", __func__);
2084 goto exit;
2085 }
2086
2087 if (!out->compr) {
2088 ALOGD("%s:: Invalid compress handle,returning default dsp latency",
2089 __func__);
2090 goto exit;
2091 }
2092
2093 metadata.key = SNDRV_COMPRESS_PATH_DELAY;
2094 ret = compress_get_metadata(out->compr, &metadata);
2095 if(ret) {
2096 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2097 goto exit;
2098 }
2099 delay_ms = metadata.value[0] / 1000; /*convert to ms*/
2100 } else {
2101 ALOGD("%s:: Using Fix DSP delay",__func__);
2102 }
2103
2104exit:
2105 ALOGD("%s:: delay in ms is %d",__func__, delay_ms);
2106 return delay_ms;
2107}
2108#else
2109int audio_extn_utils_compress_get_dsp_latency(struct stream_out *out __unused)
2110{
2111 return COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
2112}
2113#endif
Manish Dewangan69426c82017-01-30 17:35:36 +05302114
2115#ifdef SNDRV_COMPRESS_RENDER_MODE
2116int audio_extn_utils_compress_set_render_mode(struct stream_out *out)
2117{
2118 struct snd_compr_metadata metadata;
2119 int ret = -EINVAL;
2120
2121 if (!(is_offload_usecase(out->usecase))) {
2122 ALOGE("%s:: not supported for non offload session", __func__);
2123 goto exit;
2124 }
2125
2126 if (!out->compr) {
2127 ALOGD("%s:: Invalid compress handle",
2128 __func__);
2129 goto exit;
2130 }
2131
2132 ALOGD("%s:: render mode %d", __func__, out->render_mode);
2133
2134 metadata.key = SNDRV_COMPRESS_RENDER_MODE;
2135 if (out->render_mode == RENDER_MODE_AUDIO_MASTER) {
2136 metadata.value[0] = SNDRV_COMPRESS_RENDER_MODE_AUDIO_MASTER;
2137 } else if (out->render_mode == RENDER_MODE_AUDIO_STC_MASTER) {
2138 metadata.value[0] = SNDRV_COMPRESS_RENDER_MODE_STC_MASTER;
2139 } else {
2140 ret = 0;
2141 goto exit;
2142 }
2143 ret = compress_set_metadata(out->compr, &metadata);
2144 if(ret) {
2145 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2146 }
2147exit:
2148 return ret;
2149}
2150#else
2151int audio_extn_utils_compress_set_render_mode(struct stream_out *out __unused)
2152{
2153 ALOGD("%s:: configuring render mode not supported", __func__);
2154 return 0;
2155}
2156#endif
Manish Dewangan58229382017-02-02 15:48:41 +05302157
2158#ifdef SNDRV_COMPRESS_CLK_REC_MODE
2159int audio_extn_utils_compress_set_clk_rec_mode(
2160 struct audio_usecase *usecase)
2161{
2162 struct snd_compr_metadata metadata;
2163 struct stream_out *out = NULL;
2164 int ret = -EINVAL;
2165
Naresh Tanniru7586e292017-04-13 10:38:13 +05302166 if (usecase == NULL || usecase->type != PCM_PLAYBACK) {
Manish Dewangan58229382017-02-02 15:48:41 +05302167 ALOGE("%s:: Invalid use case", __func__);
2168 goto exit;
2169 }
2170
2171 out = usecase->stream.out;
2172 if (!out) {
2173 ALOGE("%s:: invalid stream", __func__);
2174 goto exit;
2175 }
2176
2177 if (!is_offload_usecase(out->usecase)) {
2178 ALOGE("%s:: not supported for non offload session", __func__);
2179 goto exit;
2180 }
2181
2182 if (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER) {
2183 ALOGD("%s:: clk recovery is only supported in STC render mode",
2184 __func__);
2185 ret = 0;
2186 goto exit;
2187 }
2188
2189 if (!out->compr) {
2190 ALOGD("%s:: Invalid compress handle",
2191 __func__);
2192 goto exit;
2193 }
2194 metadata.key = SNDRV_COMPRESS_CLK_REC_MODE;
2195 switch(usecase->out_snd_device) {
2196 case SND_DEVICE_OUT_HDMI:
2197 case SND_DEVICE_OUT_SPEAKER_AND_HDMI:
2198 case SND_DEVICE_OUT_DISPLAY_PORT:
2199 case SND_DEVICE_OUT_SPEAKER_AND_DISPLAY_PORT:
2200 metadata.value[0] = SNDRV_COMPRESS_CLK_REC_MODE_NONE;
2201 break;
2202 default:
2203 metadata.value[0] = SNDRV_COMPRESS_CLK_REC_MODE_AUTO;
2204 break;
2205 }
2206
2207 ALOGD("%s:: clk recovery mode %d",__func__, metadata.value[0]);
2208
2209 ret = compress_set_metadata(out->compr, &metadata);
2210 if(ret) {
2211 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2212 }
2213
2214exit:
2215 return ret;
2216}
2217#else
2218int audio_extn_utils_compress_set_clk_rec_mode(
2219 struct audio_usecase *usecase __unused)
2220{
2221 ALOGD("%s:: configuring render mode not supported", __func__);
2222 return 0;
2223}
2224#endif
Manish Dewangan27346042017-03-01 12:56:12 +05302225
2226#ifdef SNDRV_COMPRESS_RENDER_WINDOW
2227int audio_extn_utils_compress_set_render_window(
2228 struct stream_out *out,
2229 struct audio_out_render_window_param *render_window)
2230{
2231 struct snd_compr_metadata metadata;
2232 int ret = -EINVAL;
2233
Manish Dewangan27346042017-03-01 12:56:12 +05302234 if(render_window == NULL) {
2235 ALOGE("%s:: Invalid render_window", __func__);
2236 goto exit;
2237 }
2238
Aniket Kumar Lata1e1d3662017-06-01 18:45:48 -07002239 ALOGD("%s:: render window start 0x%"PRIx64" end 0x%"PRIx64"",
2240 __func__,render_window->render_ws, render_window->render_we);
2241
Manish Dewangan27346042017-03-01 12:56:12 +05302242 if (!is_offload_usecase(out->usecase)) {
2243 ALOGE("%s:: not supported for non offload session", __func__);
2244 goto exit;
2245 }
2246
Naresh Tanniru6160c712017-04-17 15:43:48 +05302247 if ((out->render_mode != RENDER_MODE_AUDIO_MASTER) &&
2248 (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER)) {
Manish Dewangan27346042017-03-01 12:56:12 +05302249 ALOGD("%s:: only supported in timestamp mode, current "
2250 "render mode mode %d", __func__, out->render_mode);
2251 goto exit;
2252 }
2253
2254 if (!out->compr) {
2255 ALOGW("%s:: offload session not yet opened,"
2256 "render window will be configure later", __func__);
2257 /* store render window to reconfigure in start_output_stream() */
2258 goto exit;
2259 }
2260
2261 metadata.key = SNDRV_COMPRESS_RENDER_WINDOW;
2262 /*render window start value */
2263 metadata.value[0] = 0xFFFFFFFF & render_window->render_ws; /* lsb */
2264 metadata.value[1] = \
2265 (0xFFFFFFFF00000000 & render_window->render_ws) >> 32; /* msb*/
2266 /*render window end value */
2267 metadata.value[2] = 0xFFFFFFFF & render_window->render_we; /* lsb */
2268 metadata.value[3] = \
2269 (0xFFFFFFFF00000000 & render_window->render_we) >> 32; /* msb*/
2270
2271 ret = compress_set_metadata(out->compr, &metadata);
2272 if(ret) {
2273 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2274 }
2275
2276exit:
2277 return ret;
2278}
2279#else
2280int audio_extn_utils_compress_set_render_window(
2281 struct stream_out *out __unused,
2282 struct audio_out_render_window_param *render_window __unused)
2283{
2284 ALOGD("%s:: configuring render window not supported", __func__);
2285 return 0;
2286}
2287#endif
Manish Dewangan14956cc2017-02-14 18:54:42 +05302288
2289#ifdef SNDRV_COMPRESS_START_DELAY
2290int audio_extn_utils_compress_set_start_delay(
2291 struct stream_out *out,
2292 struct audio_out_start_delay_param *delay_param)
2293{
2294 struct snd_compr_metadata metadata;
2295 int ret = -EINVAL;
2296
2297 if(delay_param == NULL) {
2298 ALOGE("%s:: Invalid delay_param", __func__);
2299 goto exit;
2300 }
2301
2302 ALOGD("%s:: render start delay 0x%"PRIx64" ", __func__,
2303 delay_param->start_delay);
2304
2305 if (!is_offload_usecase(out->usecase)) {
2306 ALOGE("%s:: not supported for non offload session", __func__);
2307 goto exit;
2308 }
2309
Naresh Tanniru6160c712017-04-17 15:43:48 +05302310 if ((out->render_mode != RENDER_MODE_AUDIO_MASTER) &&
2311 (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER)) {
Manish Dewangan14956cc2017-02-14 18:54:42 +05302312 ALOGD("%s:: only supported in timestamp mode, current "
2313 "render mode mode %d", __func__, out->render_mode);
2314 goto exit;
2315 }
2316
2317 if (!out->compr) {
2318 ALOGW("%s:: offload session not yet opened,"
2319 "start delay will be configure later", __func__);
2320 goto exit;
2321 }
2322
2323 metadata.key = SNDRV_COMPRESS_START_DELAY;
2324 metadata.value[0] = 0xFFFFFFFF & delay_param->start_delay; /* lsb */
2325 metadata.value[1] = \
2326 (0xFFFFFFFF00000000 & delay_param->start_delay) >> 32; /* msb*/
2327
2328 ret = compress_set_metadata(out->compr, &metadata);
2329 if(ret) {
2330 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2331 }
2332
2333exit:
2334 return ret;
2335}
2336#else
2337int audio_extn_utils_compress_set_start_delay(
2338 struct stream_out *out __unused,
2339 struct audio_out_start_delay_param *delay_param __unused)
2340{
2341 ALOGD("%s:: configuring render window not supported", __func__);
2342 return 0;
2343}
2344#endif
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002345
Surendar Karka287348c2019-04-10 18:31:46 +05302346#ifdef SNDRV_COMPRESS_DSP_POSITION
2347int audio_extn_utils_compress_get_dsp_presentation_pos(struct stream_out *out,
2348 uint64_t *frames, struct timespec *timestamp, int32_t clock_id)
2349{
2350 int ret = -EINVAL;
2351 uint64_t *val = NULL;
2352 uint64_t time = 0;
2353 struct snd_compr_metadata metadata;
2354
2355 ALOGV("%s:: Quering DSP position with clock id %d",__func__, clock_id);
2356 metadata.key = SNDRV_COMPRESS_DSP_POSITION;
2357 metadata.value[0] = clock_id;
2358 ret = compress_get_metadata(out->compr, &metadata);
2359 if (ret) {
2360 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2361 ret = -errno;
2362 goto exit;
2363 }
2364 val = (uint64_t *)&metadata.value[1];
2365 *frames = *val;
2366 time = *(val + 1);
2367 timestamp->tv_sec = time / 1000000;
2368 timestamp->tv_nsec = (time % 1000000)*1000;
2369
2370exit:
2371 return ret;
2372}
2373#else
2374int audio_extn_utils_compress_get_dsp_presentation_pos(struct stream_out *out __unused,
2375 uint64_t *frames __unused, struct timespec *timestamp __unused,
2376 int32_t clock_id __unused)
2377{
2378 ALOGD("%s:: dsp presentation position not supported", __func__);
2379 return 0;
2380
2381}
2382#endif
2383
2384#ifdef SNDRV_PCM_IOCTL_DSP_POSITION
2385int audio_extn_utils_pcm_get_dsp_presentation_pos(struct stream_out *out,
2386 uint64_t *frames, struct timespec *timestamp, int32_t clock_id)
2387{
2388 int ret = -EINVAL;
2389 uint64_t time = 0;
2390 struct snd_pcm_prsnt_position prsnt_position;
2391
2392 ALOGV("%s:: Quering DSP position with clock id %d",__func__, clock_id);
2393 prsnt_position.clock_id = clock_id;
2394 ret = pcm_ioctl(out->pcm, SNDRV_PCM_IOCTL_DSP_POSITION, &prsnt_position);
2395 if (ret) {
2396 ALOGE("%s::error %d", __func__, ret);
2397 ret = -EIO;
2398 goto exit;
2399 }
2400
2401 *frames = prsnt_position.frames;
2402 time = prsnt_position.timestamp;
2403 timestamp->tv_sec = time / 1000000;
2404 timestamp->tv_nsec = (time % 1000000)*1000;
2405
2406exit:
2407 return ret;
2408}
2409#else
2410int audio_extn_utils_pcm_get_dsp_presentation_pos(struct stream_out *out __unused,
2411 uint64_t *frames __unused, struct timespec *timestamp __unused,
2412 int32_t clock_id __unused)
2413{
2414 ALOGD("%s:: dsp presentation position not supported", __func__);
2415 return 0;
2416
2417}
2418#endif
2419
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002420#define MAX_SND_CARD 8
Ashish Jain30ae8942017-11-05 17:21:23 +05302421#define RETRY_US 1000000
2422#define RETRY_NUMBER 40
Aalique Grahame22e49102018-12-18 14:23:57 -08002423#define PLATFORM_INFO_XML_PATH "audio_platform_info.xml"
2424#define PLATFORM_INFO_XML_BASE_STRING "audio_platform_info"
2425
2426#ifdef LINUX_ENABLED
2427static const char *kConfigLocationList[] =
2428 {"/etc"};
2429#else
2430static const char *kConfigLocationList[] =
2431 {"/vendor/etc"};
2432#endif
2433static const int kConfigLocationListSize =
2434 (sizeof(kConfigLocationList) / sizeof(kConfigLocationList[0]));
2435
2436bool audio_extn_utils_resolve_config_file(char file_name[MIXER_PATH_MAX_LENGTH])
2437{
2438 char full_config_path[MIXER_PATH_MAX_LENGTH];
2439 for (int i = 0; i < kConfigLocationListSize; i++) {
2440 snprintf(full_config_path,
2441 MIXER_PATH_MAX_LENGTH,
2442 "%s/%s",
2443 kConfigLocationList[i],
2444 file_name);
2445 if (F_OK == access(full_config_path, 0)) {
Weiyin Jiang2995f662019-04-17 14:25:12 +08002446 strlcpy(file_name, full_config_path, MIXER_PATH_MAX_LENGTH);
Aalique Grahame22e49102018-12-18 14:23:57 -08002447 return true;
2448 }
2449 }
2450 return false;
2451}
2452
2453/* platform_info_file should be size 'MIXER_PATH_MAX_LENGTH' */
2454int audio_extn_utils_get_platform_info(const char* snd_card_name, char* platform_info_file)
2455{
2456 if (NULL == snd_card_name) {
2457 return -1;
2458 }
2459
2460 struct snd_card_split *snd_split_handle = NULL;
2461 int ret = 0;
2462 audio_extn_set_snd_card_split(snd_card_name);
2463 snd_split_handle = audio_extn_get_snd_card_split();
2464
2465 snprintf(platform_info_file, MIXER_PATH_MAX_LENGTH, "%s_%s_%s.xml",
2466 PLATFORM_INFO_XML_BASE_STRING, snd_split_handle->snd_card,
2467 snd_split_handle->form_factor);
2468
2469 if (!audio_extn_utils_resolve_config_file(platform_info_file)) {
2470 memset(platform_info_file, 0, MIXER_PATH_MAX_LENGTH);
2471 snprintf(platform_info_file, MIXER_PATH_MAX_LENGTH, "%s_%s.xml",
2472 PLATFORM_INFO_XML_BASE_STRING, snd_split_handle->snd_card);
2473
2474 if (!audio_extn_utils_resolve_config_file(platform_info_file)) {
2475 memset(platform_info_file, 0, MIXER_PATH_MAX_LENGTH);
2476 strlcpy(platform_info_file, PLATFORM_INFO_XML_PATH, MIXER_PATH_MAX_LENGTH);
2477 ret = audio_extn_utils_resolve_config_file(platform_info_file) ? 0 : -1;
2478 }
2479 }
2480
2481 return ret;
2482}
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002483
2484int audio_extn_utils_get_snd_card_num()
2485{
Soumya Managoli9fee7c62018-04-06 16:21:50 +05302486 int snd_card_num = 0;
2487 struct mixer *mixer = NULL;
2488
2489 snd_card_num = audio_extn_utils_open_snd_mixer(&mixer);
2490 if (mixer)
2491 mixer_close(mixer);
2492 return snd_card_num;
2493}
2494
2495int audio_extn_utils_open_snd_mixer(struct mixer **mixer_handle)
2496{
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002497
2498 void *hw_info = NULL;
2499 struct mixer *mixer = NULL;
2500 int retry_num = 0;
mpangfaa7bae2019-01-15 16:38:13 +08002501 int snd_card_num = 0;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002502 char* snd_card_name = NULL;
mpangfaa7bae2019-01-15 16:38:13 +08002503 int snd_card_detection_info[MAX_SND_CARD] = {0};
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002504
Soumya Managoli9fee7c62018-04-06 16:21:50 +05302505 if (!mixer_handle) {
2506 ALOGE("invalid mixer handle");
2507 return -1;
2508 }
2509 *mixer_handle = NULL;
Ashish Jain30ae8942017-11-05 17:21:23 +05302510 /*
mpangfaa7bae2019-01-15 16:38:13 +08002511 * Try with all the sound cards ( 0 to 7 ) and if none of them were detected
Ashish Jain30ae8942017-11-05 17:21:23 +05302512 * sleep for 1 sec and try detections with sound card 0 again.
2513 * If sound card gets detected, check if it is relevant, if not check with the
2514 * other sound cards. To ensure that the irrelevant sound card is not check again,
2515 * we maintain it in min_snd_card_num.
2516 */
2517 while (retry_num < RETRY_NUMBER) {
mpangfaa7bae2019-01-15 16:38:13 +08002518 snd_card_num = 0;
2519 while (snd_card_num < MAX_SND_CARD) {
2520 if (snd_card_detection_info[snd_card_num] == 0) {
2521 mixer = mixer_open(snd_card_num);
2522 if (!mixer)
2523 snd_card_num++;
2524 else
2525 break;
2526 } else
2527 snd_card_num++;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002528 }
2529
2530 if (!mixer) {
Ashish Jain30ae8942017-11-05 17:21:23 +05302531 usleep(RETRY_US);
Ashish Jain30ae8942017-11-05 17:21:23 +05302532 retry_num++;
mpangfaa7bae2019-01-15 16:38:13 +08002533 ALOGD("%s: retry, retry_num %d", __func__, retry_num);
Ashish Jain30ae8942017-11-05 17:21:23 +05302534 continue;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002535 }
2536
2537 snd_card_name = strdup(mixer_get_name(mixer));
2538 if (!snd_card_name) {
2539 ALOGE("failed to allocate memory for snd_card_name\n");
2540 mixer_close(mixer);
2541 return -1;
2542 }
2543 ALOGD("%s: snd_card_name: %s", __func__, snd_card_name);
mpangfaa7bae2019-01-15 16:38:13 +08002544 snd_card_detection_info[snd_card_num] = 1;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002545 hw_info = hw_info_init(snd_card_name);
2546 if (hw_info) {
2547 ALOGD("%s: Opened sound card:%d", __func__, snd_card_num);
2548 break;
2549 }
mpangfaa7bae2019-01-15 16:38:13 +08002550 ALOGE("%s: Failed to init hardware info, snd_card_num:%d", __func__, snd_card_num);
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002551
Dhananjay Kumara7e27832017-07-11 15:40:39 +05302552 free(snd_card_name);
2553 snd_card_name = NULL;
2554
2555 mixer_close(mixer);
2556 mixer = NULL;
2557 }
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002558 if (snd_card_name)
2559 free(snd_card_name);
Ashish Jain30ae8942017-11-05 17:21:23 +05302560
Dhananjay Kumara7e27832017-07-11 15:40:39 +05302561 if (hw_info)
2562 hw_info_deinit(hw_info);
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002563
mpangfaa7bae2019-01-15 16:38:13 +08002564 if (retry_num >= RETRY_NUMBER) {
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002565 ALOGE("%s: Unable to find correct sound card, aborting.", __func__);
2566 return -1;
2567 }
2568
Soumya Managoli9fee7c62018-04-06 16:21:50 +05302569 if (mixer)
2570 *mixer_handle = mixer;
2571
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002572 return snd_card_num;
2573}
Naresh Tanniru6160c712017-04-17 15:43:48 +05302574
Soumya Managoli9fee7c62018-04-06 16:21:50 +05302575void audio_extn_utils_close_snd_mixer(struct mixer *mixer)
2576{
2577 if (mixer)
2578 mixer_close(mixer);
2579}
2580
Naresh Tanniru6160c712017-04-17 15:43:48 +05302581#ifdef SNDRV_COMPRESS_ENABLE_ADJUST_SESSION_CLOCK
2582int audio_extn_utils_compress_enable_drift_correction(
2583 struct stream_out *out,
2584 struct audio_out_enable_drift_correction *drift)
2585{
2586 struct snd_compr_metadata metadata;
2587 int ret = -EINVAL;
2588
2589 if(drift == NULL) {
2590 ALOGE("%s:: Invalid param", __func__);
2591 goto exit;
2592 }
2593
2594 ALOGD("%s:: drift enable %d", __func__,drift->enable);
2595
2596 if (!is_offload_usecase(out->usecase)) {
2597 ALOGE("%s:: not supported for non offload session", __func__);
2598 goto exit;
2599 }
2600
2601 if (!out->compr) {
2602 ALOGW("%s:: offload session not yet opened,"
2603 "start delay will be configure later", __func__);
2604 goto exit;
2605 }
2606
2607 metadata.key = SNDRV_COMPRESS_ENABLE_ADJUST_SESSION_CLOCK;
2608 metadata.value[0] = drift->enable;
2609 out->drift_correction_enabled = drift->enable;
2610
2611 ret = compress_set_metadata(out->compr, &metadata);
2612 if(ret) {
2613 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2614 out->drift_correction_enabled = false;
2615 }
2616
2617exit:
2618 return ret;
2619}
2620#else
2621int audio_extn_utils_compress_enable_drift_correction(
2622 struct stream_out *out __unused,
2623 struct audio_out_enable_drift_correction *drift __unused)
2624{
2625 ALOGD("%s:: configuring drift enablement not supported", __func__);
2626 return 0;
2627}
2628#endif
2629
2630#ifdef SNDRV_COMPRESS_ADJUST_SESSION_CLOCK
2631int audio_extn_utils_compress_correct_drift(
2632 struct stream_out *out,
2633 struct audio_out_correct_drift *drift_param)
2634{
2635 struct snd_compr_metadata metadata;
2636 int ret = -EINVAL;
2637
2638 if (drift_param == NULL) {
2639 ALOGE("%s:: Invalid drift_param", __func__);
2640 goto exit;
2641 }
2642
2643 ALOGD("%s:: adjust time 0x%"PRIx64" ", __func__,
2644 drift_param->adjust_time);
2645
2646 if (!is_offload_usecase(out->usecase)) {
2647 ALOGE("%s:: not supported for non offload session", __func__);
2648 goto exit;
2649 }
2650
2651 if (!out->compr) {
2652 ALOGW("%s:: offload session not yet opened", __func__);
2653 goto exit;
2654 }
2655
2656 if (!out->drift_correction_enabled) {
2657 ALOGE("%s:: drift correction not enabled", __func__);
2658 goto exit;
2659 }
2660
2661 metadata.key = SNDRV_COMPRESS_ADJUST_SESSION_CLOCK;
2662 metadata.value[0] = 0xFFFFFFFF & drift_param->adjust_time; /* lsb */
2663 metadata.value[1] = \
2664 (0xFFFFFFFF00000000 & drift_param->adjust_time) >> 32; /* msb*/
2665
2666 ret = compress_set_metadata(out->compr, &metadata);
2667 if(ret)
2668 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2669exit:
2670 return ret;
2671}
2672#else
2673int audio_extn_utils_compress_correct_drift(
2674 struct stream_out *out __unused,
2675 struct audio_out_correct_drift *drift_param __unused)
2676{
2677 ALOGD("%s:: setting adjust clock not supported", __func__);
2678 return 0;
2679}
2680#endif
Naresh Tanniru29bce4e2017-04-27 17:54:30 +05302681
2682int audio_extn_utils_set_channel_map(
2683 struct stream_out *out,
2684 struct audio_out_channel_map_param *channel_map_param)
2685{
2686 int ret = -EINVAL, i = 0;
2687 int channels = audio_channel_count_from_out_mask(out->channel_mask);
2688
2689 if (channel_map_param == NULL) {
2690 ALOGE("%s:: Invalid channel_map", __func__);
2691 goto exit;
2692 }
2693
2694 if (channel_map_param->channels != channels) {
2695 ALOGE("%s:: Channels(%d) does not match stream channels(%d)",
2696 __func__, channel_map_param->channels, channels);
2697 goto exit;
2698 }
2699
2700 for ( i = 0; i < channels; i++) {
2701 ALOGV("%s:: channel_map[%d]- %d", __func__, i, channel_map_param->channel_map[i]);
2702 out->channel_map_param.channel_map[i] = channel_map_param->channel_map[i];
2703 }
2704 ret = 0;
2705exit:
2706 return ret;
2707}
Varun Balaraje49253e2017-07-06 19:48:56 +05302708
2709int audio_extn_utils_set_pan_scale_params(
2710 struct stream_out *out,
2711 struct mix_matrix_params *mm_params)
2712{
2713 int ret = -EINVAL, i = 0, j = 0;
2714
Varun Balaraj003ee752017-08-07 17:54:55 +05302715 if (mm_params == NULL || out == NULL) {
2716 ALOGE("%s:: Invalid mix matrix or out param", __func__);
Varun Balaraje49253e2017-07-06 19:48:56 +05302717 goto exit;
2718 }
2719
2720 if (mm_params->num_output_channels > MAX_CHANNELS_SUPPORTED ||
2721 mm_params->num_output_channels <= 0 ||
2722 mm_params->num_input_channels > MAX_CHANNELS_SUPPORTED ||
2723 mm_params->num_input_channels <= 0)
2724 goto exit;
2725
2726 out->pan_scale_params.num_output_channels = mm_params->num_output_channels;
2727 out->pan_scale_params.num_input_channels = mm_params->num_input_channels;
2728 out->pan_scale_params.has_output_channel_map =
2729 mm_params->has_output_channel_map;
2730 for (i = 0; i < mm_params->num_output_channels; i++)
2731 out->pan_scale_params.output_channel_map[i] =
2732 mm_params->output_channel_map[i];
2733
2734 out->pan_scale_params.has_input_channel_map =
2735 mm_params->has_input_channel_map;
2736 for (i = 0; i < mm_params->num_input_channels; i++)
2737 out->pan_scale_params.input_channel_map[i] =
2738 mm_params->input_channel_map[i];
2739
2740 out->pan_scale_params.has_mixer_coeffs = mm_params->has_mixer_coeffs;
2741 for (i = 0; i < mm_params->num_output_channels; i++)
2742 for (j = 0; j < mm_params->num_input_channels; j++) {
2743 //Convert the channel coefficient gains in Q14 format
2744 out->pan_scale_params.mixer_coeffs[i][j] =
2745 mm_params->mixer_coeffs[i][j] * (2 << 13);
2746 }
2747
2748 ret = platform_set_stream_pan_scale_params(out->dev->platform,
2749 out->pcm_device_id,
2750 out->pan_scale_params);
2751
2752exit:
2753 return ret;
2754}
2755
2756int audio_extn_utils_set_downmix_params(
2757 struct stream_out *out,
2758 struct mix_matrix_params *mm_params)
2759{
2760 int ret = -EINVAL, i = 0, j = 0;
2761 struct audio_usecase *usecase = NULL;
2762
Aniket Kumar Lataf9f246e2017-09-15 15:20:16 -07002763 if (mm_params == NULL || out == NULL) {
Varun Balaraj003ee752017-08-07 17:54:55 +05302764 ALOGE("%s:: Invalid mix matrix or out param", __func__);
Varun Balaraje49253e2017-07-06 19:48:56 +05302765 goto exit;
2766 }
2767
2768 if (mm_params->num_output_channels > MAX_CHANNELS_SUPPORTED ||
2769 mm_params->num_output_channels <= 0 ||
2770 mm_params->num_input_channels > MAX_CHANNELS_SUPPORTED ||
2771 mm_params->num_input_channels <= 0)
2772 goto exit;
2773
2774 usecase = get_usecase_from_list(out->dev, out->usecase);
Varun Balaraj003ee752017-08-07 17:54:55 +05302775 if (!usecase) {
2776 ALOGE("%s: Get usecase list failed!", __func__);
Aniket Kumar Lataf9f246e2017-09-15 15:20:16 -07002777 goto exit;
2778 }
Varun Balaraje49253e2017-07-06 19:48:56 +05302779 out->downmix_params.num_output_channels = mm_params->num_output_channels;
2780 out->downmix_params.num_input_channels = mm_params->num_input_channels;
2781
2782 out->downmix_params.has_output_channel_map =
2783 mm_params->has_output_channel_map;
2784 for (i = 0; i < mm_params->num_output_channels; i++) {
2785 out->downmix_params.output_channel_map[i] =
2786 mm_params->output_channel_map[i];
2787 }
2788
2789 out->downmix_params.has_input_channel_map =
2790 mm_params->has_input_channel_map;
2791 for (i = 0; i < mm_params->num_input_channels; i++)
2792 out->downmix_params.input_channel_map[i] =
2793 mm_params->input_channel_map[i];
2794
2795 out->downmix_params.has_mixer_coeffs = mm_params->has_mixer_coeffs;
2796 for (i = 0; i < mm_params->num_output_channels; i++)
Nikhil Latukar2e6f6242017-08-15 13:37:07 +05302797 for (j = 0; j < mm_params->num_input_channels; j++) {
2798 //Convert the channel coefficient gains in Q14 format
Varun Balaraje49253e2017-07-06 19:48:56 +05302799 out->downmix_params.mixer_coeffs[i][j] =
Nikhil Latukar2e6f6242017-08-15 13:37:07 +05302800 mm_params->mixer_coeffs[i][j] * (2 << 13);
2801 }
Varun Balaraje49253e2017-07-06 19:48:56 +05302802
2803 ret = platform_set_stream_downmix_params(out->dev->platform,
2804 out->pcm_device_id,
2805 usecase->out_snd_device,
2806 out->downmix_params);
2807
2808exit:
2809 return ret;
2810}
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05302811
2812bool audio_extn_utils_is_dolby_format(audio_format_t format)
2813{
2814 if (format == AUDIO_FORMAT_AC3 ||
2815 format == AUDIO_FORMAT_E_AC3 ||
2816 format == AUDIO_FORMAT_E_AC3_JOC)
2817 return true;
2818 else
2819 return false;
2820}
2821
`Deeraj Soman676c2702017-09-18 19:25:53 +05302822int audio_extn_utils_get_bit_width_from_string(const char *id_string)
2823{
2824 int i;
2825 const mixer_config_lookup mixer_bitwidth_config[] = {{"S24_3LE", 24},
2826 {"S32_LE", 32},
2827 {"S24_LE", 24},
2828 {"S16_LE", 16}};
2829 int num_configs = sizeof(mixer_bitwidth_config) / sizeof(mixer_bitwidth_config[0]);
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05302830
`Deeraj Soman676c2702017-09-18 19:25:53 +05302831 for (i = 0; i < num_configs; i++) {
2832 if (!strcmp(id_string, mixer_bitwidth_config[i].id_string))
2833 return mixer_bitwidth_config[i].value;
2834 }
2835
2836 return -EINVAL;
2837}
2838
2839int audio_extn_utils_get_sample_rate_from_string(const char *id_string)
2840{
2841 int i;
2842 const mixer_config_lookup mixer_samplerate_config[] = {{"KHZ_32", 32000},
2843 {"KHZ_48", 48000},
2844 {"KHZ_96", 96000},
2845 {"KHZ_144", 144000},
2846 {"KHZ_192", 192000},
2847 {"KHZ_384", 384000},
2848 {"KHZ_44P1", 44100},
2849 {"KHZ_88P2", 88200},
2850 {"KHZ_176P4", 176400},
2851 {"KHZ_352P8", 352800}};
2852 int num_configs = sizeof(mixer_samplerate_config) / sizeof(mixer_samplerate_config[0]);
2853
2854 for (i = 0; i < num_configs; i++) {
2855 if (!strcmp(id_string, mixer_samplerate_config[i].id_string))
2856 return mixer_samplerate_config[i].value;
2857 }
2858
2859 return -EINVAL;
2860}
2861
2862int audio_extn_utils_get_channels_from_string(const char *id_string)
2863{
2864 int i;
2865 const mixer_config_lookup mixer_channels_config[] = {{"One", 1},
2866 {"Two", 2},
2867 {"Three",3},
2868 {"Four", 4},
2869 {"Five", 5},
2870 {"Six", 6},
2871 {"Seven", 7},
2872 {"Eight", 8}};
2873 int num_configs = sizeof(mixer_channels_config) / sizeof(mixer_channels_config[0]);
2874
2875 for (i = 0; i < num_configs; i++) {
2876 if (!strcmp(id_string, mixer_channels_config[i].id_string))
2877 return mixer_channels_config[i].value;
2878 }
2879
2880 return -EINVAL;
2881}
Surendar karka30569792018-05-08 12:02:21 +05302882
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08002883void audio_extn_utils_release_snd_device(snd_device_t snd_device)
2884{
2885 audio_extn_dev_arbi_release(snd_device);
2886 audio_extn_sound_trigger_update_device_status(snd_device,
2887 ST_EVENT_SND_DEVICE_FREE);
2888 audio_extn_listen_update_device_status(snd_device,
2889 LISTEN_EVENT_SND_DEVICE_FREE);
2890}
2891
Surendar karka30569792018-05-08 12:02:21 +05302892int audio_extn_utils_get_license_params
2893(
2894const struct audio_device *adev,
2895struct audio_license_params *license_params
2896)
2897{
2898 if(!license_params)
2899 return -EINVAL;
2900 return platform_get_license_by_product(adev->platform, (const char*)license_params->product, &license_params->key, license_params->license);
2901}
2902
Aalique Grahame22e49102018-12-18 14:23:57 -08002903int audio_extn_utils_send_app_type_gain(struct audio_device *adev,
2904 int app_type,
2905 int *gain)
2906{
2907 int gain_cfg[4];
2908 const char *mixer_ctl_name = "App Type Gain";
2909 struct mixer_ctl *ctl;
2910 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
2911 if (!ctl) {
2912 ALOGE("%s: Could not get volume ctl mixer %s", __func__,
2913 mixer_ctl_name);
2914 return -EINVAL;
2915 }
2916 gain_cfg[0] = 0;
2917 gain_cfg[1] = app_type;
2918 gain_cfg[2] = gain[0];
2919 gain_cfg[3] = gain[1];
2920 ALOGV("%s app_type %d l(%d) r(%d)", __func__, app_type, gain[0], gain[1]);
2921 return mixer_ctl_set_array(ctl, gain_cfg,
2922 sizeof(gain_cfg)/sizeof(gain_cfg[0]));
2923}
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -07002924
2925int audio_extn_utils_is_vendor_enhanced_fwk()
2926{
2927 static int is_running_with_enhanced_fwk = -EINVAL;
2928
2929 if (is_running_with_enhanced_fwk == -EINVAL) {
2930 vndk_fwk_lib_handle = dlopen(VNDK_FWK_LIB_PATH, RTLD_NOW);
2931 if (vndk_fwk_lib_handle != NULL) {
2932 vndk_fwk_isVendorEnhancedFwk = (vndk_fwk_isVendorEnhancedFwk_t)
2933 dlsym(vndk_fwk_lib_handle, "isRunningWithVendorEnhancedFramework");
2934 if (vndk_fwk_isVendorEnhancedFwk == NULL) {
2935 ALOGW("%s: dlsym failed, defaulting to enhanced_fwk configuration",
2936 __func__);
2937 is_running_with_enhanced_fwk = 1;
2938 } else {
2939 is_running_with_enhanced_fwk = vndk_fwk_isVendorEnhancedFwk();
2940 }
2941 dlclose(vndk_fwk_lib_handle);
2942 vndk_fwk_lib_handle = NULL;
2943 } else {
2944 ALOGW("%s: VNDK_FWK_LIB not found, setting stock configuration", __func__);
2945 is_running_with_enhanced_fwk = 0;
2946 }
2947 ALOGV("%s: vndk_fwk_isVendorEnhancedFwk=%d", __func__, is_running_with_enhanced_fwk);
2948 }
2949
2950 return is_running_with_enhanced_fwk;
2951}
Deeraj Soman14230922019-01-30 16:39:30 +05302952
Deeraj Somanfa377bf2019-02-06 12:57:59 +05302953int audio_extn_utils_get_perf_mode_flag(void)
2954{
2955#ifdef COMPRESSED_PERF_MODE_FLAG
2956 return COMPRESSED_PERF_MODE_FLAG;
2957#else
2958 return 0;
2959#endif
2960}
2961
Deeraj Soman14230922019-01-30 16:39:30 +05302962size_t audio_extn_utils_get_input_buffer_size(uint32_t sample_rate,
2963 audio_format_t format,
2964 int channel_count,
2965 int64_t duration_ms,
2966 bool is_low_latency)
2967{
2968 size_t size = 0;
2969 size_t capture_duration = AUDIO_CAPTURE_PERIOD_DURATION_MSEC;
2970 uint32_t bytes_per_period_sample = 0;
2971
2972
2973 if (audio_extn_utils_check_input_parameters(sample_rate, format, channel_count) != 0)
2974 return 0;
2975
2976 if (duration_ms >= MIN_OFFLOAD_BUFFER_DURATION_MS && duration_ms <= MAX_OFFLOAD_BUFFER_DURATION_MS)
2977 capture_duration = duration_ms;
2978
2979 size = (sample_rate * capture_duration) / 1000;
2980 if (is_low_latency)
2981 size = LOW_LATENCY_CAPTURE_PERIOD_SIZE;
2982
2983
2984 bytes_per_period_sample = audio_bytes_per_sample(format) * channel_count;
2985 size *= bytes_per_period_sample;
2986
2987 /* make sure the size is multiple of 32 bytes and additionally multiple of
2988 * the frame_size (required for 24bit samples and non-power-of-2 channel counts)
2989 * At 48 kHz mono 16-bit PCM:
2990 * 5.000 ms = 240 frames = 15*16*1*2 = 480, a whole multiple of 32 (15)
2991 * 3.333 ms = 160 frames = 10*16*1*2 = 320, a whole multiple of 32 (10)
2992 *
2993 * The loop reaches result within 32 iterations, as initial size is
2994 * already a multiple of frame_size
2995 */
2996 size = audio_extn_utils_nearest_multiple(size, audio_extn_utils_lcm(32, bytes_per_period_sample));
2997
2998 return size;
2999}