blob: f8bb52949f5408b1f64fae0cf1fde03c6087810c [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) &&
1137 (usecase->id != USECASE_AUDIO_PLAYBACK_LOW_LATENCY) &&
1138 (usecase->id != USECASE_AUDIO_PLAYBACK_MULTI_CH) &&
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -08001139 (usecase->id != USECASE_AUDIO_PLAYBACK_ULL) &&
Vikram Panduranga93f080e2017-06-07 18:16:14 -07001140 (usecase->id != USECASE_AUDIO_PLAYBACK_VOIP) &&
Surendar Karka93cd25a2018-08-28 14:21:37 +05301141 (usecase->id != USECASE_AUDIO_TRANSCODE_LOOPBACK_RX) &&
Varun Balaraje49253e2017-07-06 19:48:56 +05301142 (!is_interactive_usecase(usecase->id)) &&
Srikanth Uyyala9d551402015-08-25 16:03:42 +05301143 (!is_offload_usecase(usecase->id)) &&
Derek Chenf6318be2017-06-12 17:16:24 -04001144 (usecase->type != PCM_CAPTURE) &&
1145 (!audio_extn_auto_hal_is_bus_device_usecase(usecase->id))) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301146 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 -07001147 rc = 0;
1148 goto exit_send_app_type_cfg;
1149 }
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -08001150
1151 //if VR is active then only send the mixer control
1152 if (usecase->id == USECASE_AUDIO_PLAYBACK_ULL && !audio_is_vr_mode_on(adev)) {
1153 ALOGI("ULL doesnt need sending app type cfg, returning");
1154 rc = 0;
1155 goto exit_send_app_type_cfg;
1156 }
1157
Surendar Karka93cd25a2018-08-28 14:21:37 +05301158 if (usecase->type == PCM_PLAYBACK || usecase->type == TRANSCODE_LOOPBACK_RX) {
Ben Romberger1fafdde2015-09-09 19:43:15 -07001159 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_PLAYBACK);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301160 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
1161 "Audio Stream %d App Type Cfg", pcm_device_id);
Ben Romberger1fafdde2015-09-09 19:43:15 -07001162 } else if (usecase->type == PCM_CAPTURE) {
Ben Romberger1fafdde2015-09-09 19:43:15 -07001163 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_CAPTURE);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301164 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
1165 "Audio Stream Capture %d App Type Cfg", pcm_device_id);
Srikanth Uyyala9d551402015-08-25 16:03:42 +05301166 }
Siena Richard7c2db772016-12-21 11:32:34 -08001167
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001168 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
1169 if (!ctl) {
1170 ALOGE("%s: Could not get ctl for mixer cmd - %s", __func__,
1171 mixer_ctl_name);
1172 rc = -EINVAL;
1173 goto exit_send_app_type_cfg;
1174 }
Aditya Bavanari701a6992017-03-30 19:17:16 +05301175 snd_device = platform_get_spkr_prot_snd_device(snd_device);
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301176 if (voice_is_in_call_rec_stream(usecase->stream.in) && usecase->type == PCM_CAPTURE) {
1177 snd_device_t voice_device = voice_get_incall_rec_snd_device(usecase->in_snd_device);
1178 acdb_dev_id = platform_get_snd_device_acdb_id(voice_device);
1179 ALOGV("acdb id for voice call use case %d", acdb_dev_id);
1180 } else {
1181 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
1182 }
Rohit Kumar1181b292017-01-31 18:07:17 +05301183 if (acdb_dev_id <= 0) {
1184 ALOGE("%s: Couldn't get the acdb dev id", __func__);
1185 rc = -EINVAL;
1186 goto exit_send_app_type_cfg;
1187 }
1188
Siena Richard7c2db772016-12-21 11:32:34 -08001189 snd_device_be_idx = platform_get_snd_device_backend_index(snd_device);
1190 if (snd_device_be_idx < 0) {
1191 ALOGE("%s: Couldn't get the backend index for snd device %s ret=%d",
1192 __func__, platform_get_snd_device_name(snd_device),
1193 snd_device_be_idx);
1194 }
1195
Zhou Song06761dd2019-04-28 18:08:17 +08001196 sample_rate = audio_extn_utils_get_app_sample_rate_for_device(adev, usecase, snd_device);
1197
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301198 if ((usecase->type == PCM_PLAYBACK) && (usecase->stream.out != NULL)) {
Varun Balaraje49253e2017-07-06 19:48:56 +05301199 /* Interactive streams are supported with only direct app type id.
1200 * Get Direct profile app type and use it for interactive streams
1201 */
1202 list_for_each(node, &adev->streams_output_cfg_list) {
1203 s_info = node_to_item(node, struct streams_io_cfg, list);
Aalique Grahame5cd12ff2017-10-19 10:57:00 -07001204 if (s_info->flags.out_flags == (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_BD |
Nikhil Laturkarac4a7492017-08-31 18:27:19 +05301205 AUDIO_OUTPUT_FLAG_DIRECT_PCM |
1206 AUDIO_OUTPUT_FLAG_DIRECT))
1207 bd_app_type = s_info->app_type_cfg.app_type;
Varun Balaraje49253e2017-07-06 19:48:56 +05301208 }
Aalique Grahame5cd12ff2017-10-19 10:57:00 -07001209 if (usecase->stream.out->flags == (audio_output_flags_t)AUDIO_OUTPUT_FLAG_INTERACTIVE)
Nikhil Laturkarac4a7492017-08-31 18:27:19 +05301210 app_type = bd_app_type;
Varun Balaraje49253e2017-07-06 19:48:56 +05301211 else
1212 app_type = usecase->stream.out->app_type_cfg.app_type;
Siena Richard7c2db772016-12-21 11:32:34 -08001213 app_type_cfg[len++] = app_type;
Mingming Yin21854652016-04-13 11:54:02 -07001214 app_type_cfg[len++] = acdb_dev_id;
Naresh Tanniru3a406772017-05-10 13:09:05 -07001215 app_type_cfg[len++] = sample_rate;
1216
Siena Richard7c2db772016-12-21 11:32:34 -08001217 if (snd_device_be_idx > 0)
1218 app_type_cfg[len++] = snd_device_be_idx;
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301219
Siena Richard7c2db772016-12-21 11:32:34 -08001220 ALOGI("%s PLAYBACK app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
1221 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301222
1223 } else if ((usecase->type == PCM_CAPTURE) && (usecase->stream.in != NULL)) {
Siena Richard7c2db772016-12-21 11:32:34 -08001224 app_type = usecase->stream.in->app_type_cfg.app_type;
1225 app_type_cfg[len++] = app_type;
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301226 app_type_cfg[len++] = acdb_dev_id;
Siena Richard7c2db772016-12-21 11:32:34 -08001227 app_type_cfg[len++] = sample_rate;
1228 if (snd_device_be_idx > 0)
1229 app_type_cfg[len++] = snd_device_be_idx;
1230 ALOGI("%s CAPTURE app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
1231 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301232 } else {
Siena Richard7c2db772016-12-21 11:32:34 -08001233 app_type = platform_get_default_app_type_v2(adev->platform, usecase->type);
Surendar Karka93cd25a2018-08-28 14:21:37 +05301234 if(usecase->type == TRANSCODE_LOOPBACK_RX) {
Siddartha Shaik343abc62017-08-08 11:15:25 +05301235 app_type = usecase->stream.inout->out_app_type_cfg.app_type;
1236 }
Siena Richard7c2db772016-12-21 11:32:34 -08001237 app_type_cfg[len++] = app_type;
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301238 app_type_cfg[len++] = acdb_dev_id;
1239 app_type_cfg[len++] = sample_rate;
Siena Richard7c2db772016-12-21 11:32:34 -08001240 if (snd_device_be_idx > 0)
1241 app_type_cfg[len++] = snd_device_be_idx;
1242 ALOGI("%s default app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
1243 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Ashish Jainc02430d2016-01-04 10:42:43 +05301244 }
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301245
Siddartha Shaik343abc62017-08-08 11:15:25 +05301246 if(ctl)
1247 mixer_ctl_set_array(ctl, app_type_cfg, len);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001248 rc = 0;
1249exit_send_app_type_cfg:
1250 return rc;
1251}
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001252
Deeraj Soman14230922019-01-30 16:39:30 +05301253static int audio_extn_utils_check_input_parameters(uint32_t sample_rate,
1254 audio_format_t format,
1255 int channel_count)
1256{
1257 int ret = 0;
1258
1259 if (((format != AUDIO_FORMAT_PCM_16_BIT) && (format != AUDIO_FORMAT_PCM_8_24_BIT) &&
1260 (format != AUDIO_FORMAT_PCM_24_BIT_PACKED) && (format != AUDIO_FORMAT_PCM_32_BIT) &&
1261 (format != AUDIO_FORMAT_PCM_FLOAT)) &&
1262 !voice_extn_compress_voip_is_format_supported(format) &&
1263 !audio_extn_compr_cap_format_supported(format) &&
1264 !audio_extn_cin_format_supported(format))
1265 ret = -EINVAL;
1266
1267 switch (channel_count) {
1268 case 1:
1269 case 2:
1270 case 3:
1271 case 4:
1272 case 6:
1273 case 8:
1274 break;
1275 default:
1276 ret = -EINVAL;
1277 }
1278
1279 switch (sample_rate) {
1280 case 8000:
1281 case 11025:
1282 case 12000:
1283 case 16000:
1284 case 22050:
1285 case 24000:
1286 case 32000:
1287 case 44100:
1288 case 48000:
1289 case 88200:
1290 case 96000:
1291 case 176400:
1292 case 192000:
1293 break;
1294 default:
1295 ret = -EINVAL;
1296 }
1297
1298 return ret;
1299}
1300
1301static inline uint32_t audio_extn_utils_nearest_multiple(uint32_t num, uint32_t multiplier)
1302{
1303 uint32_t remainder = 0;
1304
1305 if (!multiplier)
1306 return num;
1307
1308 remainder = num % multiplier;
1309 if (remainder)
1310 num += (multiplier - remainder);
1311
1312 return num;
1313}
1314
1315static inline uint32_t audio_extn_utils_lcm(uint32_t num1, uint32_t num2)
1316{
1317 uint32_t high = num1, low = num2, temp = 0;
1318
1319 if (!num1 || !num2)
1320 return 0;
1321
1322 if (num1 < num2) {
1323 high = num2;
1324 low = num1;
1325 }
1326
1327 while (low != 0) {
1328 temp = low;
1329 low = high % low;
1330 high = temp;
1331 }
1332 return (num1 * num2)/high;
1333}
1334
Siena Richard7c2db772016-12-21 11:32:34 -08001335int audio_extn_utils_send_app_type_cfg(struct audio_device *adev,
1336 struct audio_usecase *usecase)
1337{
1338 int i, num_devices = 0;
1339 snd_device_t new_snd_devices[SND_DEVICE_OUT_END] = {0};
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301340 snd_device_t in_snd_device = usecase->in_snd_device;
Siena Richard7c2db772016-12-21 11:32:34 -08001341 int rc = 0;
1342
Aalique Grahame22e49102018-12-18 14:23:57 -08001343 if (usecase->type == PCM_HFP_CALL) {
1344 return audio_extn_utils_send_app_type_cfg_hfp(adev, usecase);
1345 }
1346
Siena Richard7c2db772016-12-21 11:32:34 -08001347 switch (usecase->type) {
1348 case PCM_PLAYBACK:
Surendar Karka93cd25a2018-08-28 14:21:37 +05301349 case TRANSCODE_LOOPBACK_RX:
Siena Richard7c2db772016-12-21 11:32:34 -08001350 ALOGD("%s: usecase->out_snd_device %s",
1351 __func__, platform_get_snd_device_name(usecase->out_snd_device));
1352 /* check for out combo device */
1353 if (platform_split_snd_device(adev->platform,
1354 usecase->out_snd_device,
1355 &num_devices, new_snd_devices)) {
1356 new_snd_devices[0] = usecase->out_snd_device;
1357 num_devices = 1;
1358 }
1359 break;
1360 case PCM_CAPTURE:
1361 ALOGD("%s: usecase->in_snd_device %s",
1362 __func__, platform_get_snd_device_name(usecase->in_snd_device));
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301363 if (voice_is_in_call_rec_stream(usecase->stream.in)) {
1364 in_snd_device = voice_get_incall_rec_backend_device(usecase->stream.in);
1365 }
Siena Richard7c2db772016-12-21 11:32:34 -08001366 /* check for in combo device */
1367 if (platform_split_snd_device(adev->platform,
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301368 in_snd_device,
Siena Richard7c2db772016-12-21 11:32:34 -08001369 &num_devices, new_snd_devices)) {
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301370 new_snd_devices[0] = in_snd_device;
Siena Richard7c2db772016-12-21 11:32:34 -08001371 num_devices = 1;
1372 }
1373 break;
1374 default:
1375 ALOGI("%s: not a playback/capture path, no need to cfg app type", __func__);
1376 rc = 0;
1377 break;
1378 }
1379
1380 for (i = 0; i < num_devices; i++) {
1381 rc = send_app_type_cfg_for_device(adev, usecase, new_snd_devices[i]);
1382 if (rc)
1383 break;
1384 }
1385
1386 return rc;
1387}
1388
Preetam Singh Ranawat9519e9c2015-11-18 16:05:55 +05301389int read_line_from_file(const char *path, char *buf, size_t count)
1390{
1391 char * fgets_ret;
1392 FILE * fd;
1393 int rv;
1394
1395 fd = fopen(path, "r");
1396 if (fd == NULL)
1397 return -1;
1398
1399 fgets_ret = fgets(buf, (int)count, fd);
1400 if (NULL != fgets_ret) {
1401 rv = (int)strlen(buf);
1402 } else {
1403 rv = ferror(fd);
1404 }
1405 fclose(fd);
1406
1407 return rv;
1408}
1409
Ashish Jainf1eaa582016-05-23 20:54:24 +05301410/*Translates ALSA formats to AOSP PCM formats*/
1411audio_format_t alsa_format_to_hal(uint32_t alsa_format)
1412{
1413 audio_format_t format;
1414
1415 switch(alsa_format) {
1416 case SNDRV_PCM_FORMAT_S16_LE:
1417 format = AUDIO_FORMAT_PCM_16_BIT;
1418 break;
1419 case SNDRV_PCM_FORMAT_S24_3LE:
1420 format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
1421 break;
1422 case SNDRV_PCM_FORMAT_S24_LE:
1423 format = AUDIO_FORMAT_PCM_8_24_BIT;
1424 break;
1425 case SNDRV_PCM_FORMAT_S32_LE:
1426 format = AUDIO_FORMAT_PCM_32_BIT;
1427 break;
1428 default:
1429 ALOGW("Incorrect ALSA format");
1430 format = AUDIO_FORMAT_INVALID;
1431 }
1432 return format;
1433}
1434
1435/*Translates hal format (AOSP) to alsa formats*/
1436uint32_t hal_format_to_alsa(audio_format_t hal_format)
1437{
1438 uint32_t alsa_format;
1439
1440 switch (hal_format) {
1441 case AUDIO_FORMAT_PCM_32_BIT: {
1442 if (platform_supports_true_32bit())
1443 alsa_format = SNDRV_PCM_FORMAT_S32_LE;
1444 else
1445 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1446 }
1447 break;
1448 case AUDIO_FORMAT_PCM_8_BIT:
1449 alsa_format = SNDRV_PCM_FORMAT_S8;
1450 break;
1451 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
1452 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1453 break;
1454 case AUDIO_FORMAT_PCM_8_24_BIT: {
1455 if (platform_supports_true_32bit())
1456 alsa_format = SNDRV_PCM_FORMAT_S32_LE;
1457 else
1458 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1459 }
1460 break;
1461 case AUDIO_FORMAT_PCM_FLOAT:
1462 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1463 break;
1464 default:
1465 case AUDIO_FORMAT_PCM_16_BIT:
1466 alsa_format = SNDRV_PCM_FORMAT_S16_LE;
1467 break;
1468 }
1469 return alsa_format;
1470}
1471
Ashish Jain83a6cc22016-06-28 14:34:17 +05301472/*Translates PCM formats to AOSP formats*/
1473audio_format_t pcm_format_to_hal(uint32_t pcm_format)
1474{
1475 audio_format_t format = AUDIO_FORMAT_INVALID;
1476
1477 switch(pcm_format) {
1478 case PCM_FORMAT_S16_LE:
1479 format = AUDIO_FORMAT_PCM_16_BIT;
1480 break;
1481 case PCM_FORMAT_S24_3LE:
1482 format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
1483 break;
1484 case PCM_FORMAT_S24_LE:
1485 format = AUDIO_FORMAT_PCM_8_24_BIT;
1486 break;
1487 case PCM_FORMAT_S32_LE:
1488 format = AUDIO_FORMAT_PCM_32_BIT;
1489 break;
1490 default:
1491 ALOGW("Incorrect PCM format");
1492 format = AUDIO_FORMAT_INVALID;
1493 }
1494 return format;
1495}
1496
1497/*Translates hal format (AOSP) to alsa formats*/
1498uint32_t hal_format_to_pcm(audio_format_t hal_format)
1499{
1500 uint32_t pcm_format;
1501
1502 switch (hal_format) {
1503 case AUDIO_FORMAT_PCM_32_BIT:
1504 case AUDIO_FORMAT_PCM_8_24_BIT:
1505 case AUDIO_FORMAT_PCM_FLOAT: {
1506 if (platform_supports_true_32bit())
1507 pcm_format = PCM_FORMAT_S32_LE;
1508 else
1509 pcm_format = PCM_FORMAT_S24_3LE;
1510 }
1511 break;
1512 case AUDIO_FORMAT_PCM_8_BIT:
1513 pcm_format = PCM_FORMAT_S8;
1514 break;
1515 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
1516 pcm_format = PCM_FORMAT_S24_3LE;
1517 break;
1518 default:
1519 case AUDIO_FORMAT_PCM_16_BIT:
1520 pcm_format = PCM_FORMAT_S16_LE;
1521 break;
1522 }
1523 return pcm_format;
1524}
1525
Ashish Jainf1eaa582016-05-23 20:54:24 +05301526uint32_t get_alsa_fragment_size(uint32_t bytes_per_sample,
1527 uint32_t sample_rate,
Deeraj Soman65358ab2019-02-07 15:40:49 +05301528 uint32_t noOfChannels,
1529 int64_t duration_ms)
Ashish Jainf1eaa582016-05-23 20:54:24 +05301530{
1531 uint32_t fragment_size = 0;
1532 uint32_t pcm_offload_time = PCM_OFFLOAD_BUFFER_DURATION;
1533
Deeraj Soman65358ab2019-02-07 15:40:49 +05301534 if (duration_ms >= MIN_OFFLOAD_BUFFER_DURATION_MS && duration_ms <= MAX_OFFLOAD_BUFFER_DURATION_MS)
1535 pcm_offload_time = duration_ms;
1536
Ashish Jainf1eaa582016-05-23 20:54:24 +05301537 fragment_size = (pcm_offload_time
1538 * sample_rate
1539 * bytes_per_sample
1540 * noOfChannels)/1000;
1541 if (fragment_size < MIN_PCM_OFFLOAD_FRAGMENT_SIZE)
1542 fragment_size = MIN_PCM_OFFLOAD_FRAGMENT_SIZE;
1543 else if (fragment_size > MAX_PCM_OFFLOAD_FRAGMENT_SIZE)
1544 fragment_size = MAX_PCM_OFFLOAD_FRAGMENT_SIZE;
1545 /*To have same PCM samples for all channels, the buffer size requires to
1546 *be multiple of (number of channels * bytes per sample)
1547 *For writes to succeed, the buffer must be written at address which is multiple of 32
1548 */
Aalique Grahameb85f2d92017-10-16 17:53:23 -07001549 fragment_size = ALIGN(fragment_size, (bytes_per_sample * noOfChannels * 32));
Ashish Jainf1eaa582016-05-23 20:54:24 +05301550
1551 ALOGI("PCM offload Fragment size to %d bytes", fragment_size);
1552 return fragment_size;
1553}
1554
1555/* Calculates the fragment size required to configure compress session.
1556 * Based on the alsa format selected, decide if conversion is needed in
1557
1558 * HAL ( e.g. convert AUDIO_FORMAT_PCM_FLOAT input format to
1559 * AUDIO_FORMAT_PCM_24_BIT_PACKED before writing to the compress driver.
1560 */
1561void audio_extn_utils_update_direct_pcm_fragment_size(struct stream_out *out)
1562{
Ashish Jain83a6cc22016-06-28 14:34:17 +05301563 audio_format_t dst_format = out->hal_op_format;
1564 audio_format_t src_format = out->hal_ip_format;
Ashish Jainf1eaa582016-05-23 20:54:24 +05301565 uint32_t hal_op_bytes_per_sample = audio_bytes_per_sample(dst_format);
1566 uint32_t hal_ip_bytes_per_sample = audio_bytes_per_sample(src_format);
1567
1568 out->compr_config.fragment_size =
1569 get_alsa_fragment_size(hal_op_bytes_per_sample,
1570 out->sample_rate,
Deeraj Soman65358ab2019-02-07 15:40:49 +05301571 popcount(out->channel_mask),
1572 out->info.duration_us / 1000);
Ashish Jainf1eaa582016-05-23 20:54:24 +05301573
1574 if ((src_format != dst_format) &&
1575 hal_op_bytes_per_sample != hal_ip_bytes_per_sample) {
1576
Ashish Jain83a6cc22016-06-28 14:34:17 +05301577 out->hal_fragment_size =
Ashish Jainf1eaa582016-05-23 20:54:24 +05301578 ((out->compr_config.fragment_size * hal_ip_bytes_per_sample) /
1579 hal_op_bytes_per_sample);
1580 ALOGI("enable conversion hal_input_fragment_size is %d src_format %x dst_format %x",
Ashish Jain83a6cc22016-06-28 14:34:17 +05301581 out->hal_fragment_size, src_format, dst_format);
Ashish Jainf1eaa582016-05-23 20:54:24 +05301582 } else {
Ashish Jain83a6cc22016-06-28 14:34:17 +05301583 out->hal_fragment_size = out->compr_config.fragment_size;
Ashish Jainf1eaa582016-05-23 20:54:24 +05301584 }
1585}
1586
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301587/* converts pcm format 24_8 to 8_24 inplace */
1588size_t audio_extn_utils_convert_format_24_8_to_8_24(void *buf, size_t bytes)
1589{
1590 size_t i = 0;
1591 int *int_buf_stream = buf;
1592
1593 if ((bytes % 4) != 0) {
1594 ALOGE("%s: wrong inout buffer! ... is not 32 bit aligned ", __func__);
1595 return -EINVAL;
1596 }
1597
1598 for (; i < (bytes / 4); i++)
1599 int_buf_stream[i] >>= 8;
1600
1601 return bytes;
1602}
1603
1604int get_snd_codec_id(audio_format_t format)
1605{
1606 int id = 0;
1607
1608 switch (format & AUDIO_FORMAT_MAIN_MASK) {
1609 case AUDIO_FORMAT_MP3:
1610 id = SND_AUDIOCODEC_MP3;
1611 break;
1612 case AUDIO_FORMAT_AAC:
1613 id = SND_AUDIOCODEC_AAC;
1614 break;
1615 case AUDIO_FORMAT_AAC_ADTS:
1616 id = SND_AUDIOCODEC_AAC;
1617 break;
Arun Kumar Dasari3b174182016-12-27 13:01:14 +05301618 case AUDIO_FORMAT_AAC_LATM:
1619 id = SND_AUDIOCODEC_AAC;
1620 break;
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301621 case AUDIO_FORMAT_PCM:
1622 id = SND_AUDIOCODEC_PCM;
1623 break;
1624 case AUDIO_FORMAT_FLAC:
1625 id = SND_AUDIOCODEC_FLAC;
1626 break;
1627 case AUDIO_FORMAT_ALAC:
1628 id = SND_AUDIOCODEC_ALAC;
1629 break;
1630 case AUDIO_FORMAT_APE:
1631 id = SND_AUDIOCODEC_APE;
1632 break;
1633 case AUDIO_FORMAT_VORBIS:
1634 id = SND_AUDIOCODEC_VORBIS;
1635 break;
1636 case AUDIO_FORMAT_WMA:
1637 id = SND_AUDIOCODEC_WMA;
1638 break;
1639 case AUDIO_FORMAT_WMA_PRO:
1640 id = SND_AUDIOCODEC_WMA_PRO;
1641 break;
Satish Babu Patakokila0c313922016-12-08 12:07:08 +05301642 case AUDIO_FORMAT_MP2:
1643 id = SND_AUDIOCODEC_MP2;
1644 break;
Satish Babu Patakokila915ecba2017-01-10 17:43:56 +05301645 case AUDIO_FORMAT_AC3:
1646 id = SND_AUDIOCODEC_AC3;
1647 break;
1648 case AUDIO_FORMAT_E_AC3:
1649 case AUDIO_FORMAT_E_AC3_JOC:
1650 id = SND_AUDIOCODEC_EAC3;
1651 break;
1652 case AUDIO_FORMAT_DTS:
1653 case AUDIO_FORMAT_DTS_HD:
1654 id = SND_AUDIOCODEC_DTS;
1655 break;
Ben Romberger1aaaf862017-04-06 17:49:46 -07001656 case AUDIO_FORMAT_DOLBY_TRUEHD:
1657 id = SND_AUDIOCODEC_TRUEHD;
1658 break;
Naresh Tanniru928f0862017-04-07 16:44:23 -07001659 case AUDIO_FORMAT_IEC61937:
1660 id = SND_AUDIOCODEC_IEC61937;
1661 break;
Preetam Singh Ranawat4277a5a2017-01-18 19:02:24 +05301662 case AUDIO_FORMAT_DSD:
1663 id = SND_AUDIOCODEC_DSD;
1664 break;
Dhanalakshmi Siddani18737932016-11-29 17:33:17 +05301665 case AUDIO_FORMAT_APTX:
1666 id = SND_AUDIOCODEC_APTX;
1667 break;
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301668 default:
1669 ALOGE("%s: Unsupported audio format :%x", __func__, format);
1670 }
1671
1672 return id;
1673}
1674
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001675void audio_extn_utils_send_audio_calibration(struct audio_device *adev,
1676 struct audio_usecase *usecase)
1677{
1678 int type = usecase->type;
1679
Dhananjay Kumar14245982017-01-16 20:21:00 +05301680 if (type == PCM_PLAYBACK && usecase->stream.out != NULL) {
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05301681 platform_send_audio_calibration(adev->platform, usecase,
Zhou Song06761dd2019-04-28 18:08:17 +08001682 usecase->stream.out->app_type_cfg.app_type);
Dhananjay Kumar14245982017-01-16 20:21:00 +05301683 } else if (type == PCM_CAPTURE && usecase->stream.in != NULL) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301684 platform_send_audio_calibration(adev->platform, usecase,
Zhou Song06761dd2019-04-28 18:08:17 +08001685 usecase->stream.in->app_type_cfg.app_type);
1686 } else if ((type == PCM_HFP_CALL) || (type == PCM_CAPTURE) ||
1687 (type == TRANSCODE_LOOPBACK_RX && usecase->stream.inout != NULL)) {
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05301688 platform_send_audio_calibration(adev->platform, usecase,
Zhou Song06761dd2019-04-28 18:08:17 +08001689 platform_get_default_app_type_v2(adev->platform, usecase->type));
Vidyakumar Athotae57f6002017-03-01 13:13:16 -08001690 } else {
1691 /* No need to send audio calibration for voice and voip call usecases */
1692 if ((type != VOICE_CALL) && (type != VOIP_CALL))
1693 ALOGW("%s: No audio calibration for usecase type = %d", __func__, type);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001694 }
1695}
1696
Ben Rombergera04fabc2014-11-14 12:16:03 -08001697// Base64 Encode and Decode
1698// Not all features supported. This must be used only with following conditions.
1699// Decode Modes: Support with and without padding
1700// CRLF not handling. So no CRLF in string to decode.
1701// Encode Modes: Supports only padding
1702int b64decode(char *inp, int ilen, uint8_t* outp)
1703{
1704 int i, j, k, ii, num;
1705 int rem, pcnt;
1706 uint32_t res=0;
1707 uint8_t getIndex[MAX_BASEINDEX_LEN];
1708 uint8_t tmp, cflag;
1709
1710 if(inp == NULL || outp == NULL || ilen <= 0) {
1711 ALOGE("[%s] received NULL pointer or zero length",__func__);
1712 return -1;
1713 }
1714
1715 memset(getIndex, MAX_BASEINDEX_LEN-1, sizeof(getIndex));
1716 for(i=0;i<BASE_TABLE_SIZE;i++) {
1717 getIndex[(uint8_t)bTable[i]] = (uint8_t)i;
1718 }
1719 getIndex[(uint8_t)'=']=0;
1720
1721 j=0;k=0;
1722 num = ilen/4;
1723 rem = ilen%4;
1724 if(rem==0)
1725 num = num-1;
1726 cflag=0;
1727 for(i=0; i<num; i++) {
1728 res=0;
1729 for(ii=0;ii<4;ii++) {
1730 res = res << 6;
1731 tmp = getIndex[(uint8_t)inp[j++]];
1732 res = res | tmp;
1733 cflag = cflag | tmp;
1734 }
1735 outp[k++] = (res >> 16)&0xFF;
1736 outp[k++] = (res >> 8)&0xFF;
1737 outp[k++] = res & 0xFF;
1738 }
1739
1740 // Handle last bytes special
1741 pcnt=0;
1742 if(rem == 0) {
1743 //With padding or full data
1744 res = 0;
1745 for(ii=0;ii<4;ii++) {
1746 if(inp[j] == '=')
1747 pcnt++;
1748 res = res << 6;
1749 tmp = getIndex[(uint8_t)inp[j++]];
1750 res = res | tmp;
1751 cflag = cflag | tmp;
1752 }
1753 outp[k++] = res >> 16;
1754 if(pcnt == 2)
1755 goto done;
1756 outp[k++] = (res>>8)&0xFF;
1757 if(pcnt == 1)
1758 goto done;
1759 outp[k++] = res&0xFF;
1760 } else {
1761 //without padding
1762 res = 0;
1763 for(i=0;i<rem;i++) {
1764 res = res << 6;
1765 tmp = getIndex[(uint8_t)inp[j++]];
1766 res = res | tmp;
1767 cflag = cflag | tmp;
1768 }
1769 for(i=rem;i<4;i++) {
1770 res = res << 6;
1771 pcnt++;
1772 }
1773 outp[k++] = res >> 16;
1774 if(pcnt == 2)
1775 goto done;
1776 outp[k++] = (res>>8)&0xFF;
1777 if(pcnt == 1)
1778 goto done;
1779 outp[k++] = res&0xFF;
1780 }
1781done:
1782 if(cflag == 0xFF) {
1783 ALOGE("[%s] base64 decode failed. Invalid character found %s",
1784 __func__, inp);
1785 return 0;
1786 }
1787 return k;
1788}
1789
1790int b64encode(uint8_t *inp, int ilen, char* outp)
1791{
1792 int i,j,k, num;
1793 int rem=0;
1794 uint32_t res=0;
1795
1796 if(inp == NULL || outp == NULL || ilen<=0) {
1797 ALOGE("[%s] received NULL pointer or zero input length",__func__);
1798 return -1;
1799 }
1800
1801 num = ilen/3;
1802 rem = ilen%3;
1803 j=0;k=0;
1804 for(i=0; i<num; i++) {
1805 //prepare index
1806 res = inp[j++]<<16;
1807 res = res | inp[j++]<<8;
1808 res = res | inp[j++];
1809 //get output map from index
1810 outp[k++] = (char) bTable[(res>>18)&0x3F];
1811 outp[k++] = (char) bTable[(res>>12)&0x3F];
1812 outp[k++] = (char) bTable[(res>>6)&0x3F];
1813 outp[k++] = (char) bTable[res&0x3F];
1814 }
1815
1816 switch(rem) {
1817 case 1:
1818 res = inp[j++]<<16;
1819 outp[k++] = (char) bTable[res>>18];
1820 outp[k++] = (char) bTable[(res>>12)&0x3F];
1821 //outp[k++] = '=';
1822 //outp[k++] = '=';
1823 break;
1824 case 2:
1825 res = inp[j++]<<16;
1826 res = res | inp[j++]<<8;
1827 outp[k++] = (char) bTable[res>>18];
1828 outp[k++] = (char) bTable[(res>>12)&0x3F];
1829 outp[k++] = (char) bTable[(res>>6)&0x3F];
1830 //outp[k++] = '=';
1831 break;
1832 default:
1833 break;
1834 }
Ben Rombergera04fabc2014-11-14 12:16:03 -08001835 outp[k] = '\0';
1836 return k;
1837}
Ashish Jain81eb2a82015-05-13 10:52:34 +05301838
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301839
1840int audio_extn_utils_get_codec_version(const char *snd_card_name,
1841 int card_num,
1842 char *codec_version)
1843{
1844 char procfs_path[50];
1845 FILE *fp;
1846
1847 if (strstr(snd_card_name, "tasha")) {
1848 snprintf(procfs_path, sizeof(procfs_path),
1849 "/proc/asound/card%d/codecs/tasha/version", card_num);
1850 if ((fp = fopen(procfs_path, "r")) != NULL) {
1851 fgets(codec_version, CODEC_VERSION_MAX_LENGTH, fp);
1852 fclose(fp);
1853 } else {
1854 ALOGE("%s: ERROR. cannot open %s", __func__, procfs_path);
1855 return -ENOENT;
1856 }
1857 ALOGD("%s: codec version %s", __func__, codec_version);
1858 }
1859
1860 return 0;
1861}
1862
Preetam Singh Ranawat9d0d8e42019-07-15 12:27:25 +05301863int audio_extn_utils_get_codec_variant(int card_num,
1864 char *codec_variant)
1865{
1866 char procfs_path[50];
1867 FILE *fp;
1868 snprintf(procfs_path, sizeof(procfs_path),
1869 "/proc/asound/card%d/codecs/wcd938x/variant", card_num);
1870 if ((fp = fopen(procfs_path, "r")) == NULL) {
1871 snprintf(procfs_path, sizeof(procfs_path),
1872 "/proc/asound/card%d/codecs/wcd937x/variant", card_num);
1873 if ((fp = fopen(procfs_path, "r")) == NULL) {
1874 ALOGE("%s: ERROR. cannot open %s", __func__, procfs_path);
1875 return -ENOENT;
1876 }
1877 }
1878 fgets(codec_variant, CODEC_VARIANT_MAX_LENGTH, fp);
1879 fclose(fp);
1880 ALOGD("%s: codec variant is %s", __func__, codec_variant);
1881 return 0;
1882}
1883
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301884
Ashish Jain81eb2a82015-05-13 10:52:34 +05301885#ifdef AUDIO_EXTERNAL_HDMI_ENABLED
1886
1887void get_default_compressed_channel_status(
1888 unsigned char *channel_status)
1889{
Ashish Jain81eb2a82015-05-13 10:52:34 +05301890 memset(channel_status,0,24);
1891
1892 /* block start bit in preamble bit 3 */
1893 channel_status[0] |= PROFESSIONAL;
1894 //compre out
1895 channel_status[0] |= NON_LPCM;
1896 // sample rate; fixed 48K for default/transcode
1897 channel_status[3] |= SR_48000;
1898}
1899
Ashish Jain81eb2a82015-05-13 10:52:34 +05301900int32_t get_compressed_channel_status(void *audio_stream_data,
1901 uint32_t audio_frame_size,
1902 unsigned char *channel_status,
1903 enum audio_parser_code_type codec_type)
1904 // codec_type - AUDIO_PARSER_CODEC_AC3
1905 // - AUDIO_PARSER_CODEC_DTS
1906{
1907 unsigned char *stream;
1908 int ret = 0;
1909 stream = (unsigned char *)audio_stream_data;
1910
1911 if (audio_stream_data == NULL || audio_frame_size == 0) {
1912 ALOGW("no buffer to get channel status, return default for compress");
1913 get_default_compressed_channel_status(channel_status);
1914 return ret;
1915 }
1916
1917 memset(channel_status,0,24);
1918 if(init_audio_parser(stream, audio_frame_size, codec_type) == -1)
1919 {
1920 ALOGE("init audio parser failed");
1921 return -1;
1922 }
1923 ret = get_channel_status(channel_status, codec_type);
1924 return ret;
1925
1926}
1927
Ashish Jain81eb2a82015-05-13 10:52:34 +05301928void get_lpcm_channel_status(uint32_t sampleRate,
1929 unsigned char *channel_status)
1930{
1931 int32_t status = 0;
Ashish Jain81eb2a82015-05-13 10:52:34 +05301932 memset(channel_status,0,24);
1933 /* block start bit in preamble bit 3 */
1934 channel_status[0] |= PROFESSIONAL;
1935 //LPCM OUT
1936 channel_status[0] &= ~NON_LPCM;
1937
1938 switch (sampleRate) {
1939 case 8000:
1940 case 11025:
1941 case 12000:
1942 case 16000:
1943 case 22050:
1944 channel_status[3] |= SR_NOTID;
Preetam Singh Ranawat61716b12015-12-14 11:55:24 +05301945 break;
Ashish Jain81eb2a82015-05-13 10:52:34 +05301946 case 24000:
1947 channel_status[3] |= SR_24000;
1948 break;
1949 case 32000:
1950 channel_status[3] |= SR_32000;
1951 break;
1952 case 44100:
1953 channel_status[3] |= SR_44100;
1954 break;
1955 case 48000:
1956 channel_status[3] |= SR_48000;
1957 break;
1958 case 88200:
1959 channel_status[3] |= SR_88200;
1960 break;
1961 case 96000:
1962 channel_status[3] |= SR_96000;
1963 break;
1964 case 176400:
1965 channel_status[3] |= SR_176400;
1966 break;
1967 case 192000:
1968 channel_status[3] |= SR_192000;
1969 break;
1970 default:
1971 ALOGV("Invalid sample_rate %u\n", sampleRate);
1972 status = -1;
1973 break;
1974 }
1975}
1976
1977void audio_utils_set_hdmi_channel_status(struct stream_out *out, char * buffer, size_t bytes)
1978{
1979 unsigned char channel_status[24]={0};
1980 struct snd_aes_iec958 iec958;
1981 const char *mixer_ctl_name = "IEC958 Playback PCM Stream";
1982 struct mixer_ctl *ctl;
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +05301983 ALOGV("%s: buffer %s bytes %zd", __func__, buffer, bytes);
Arun Mirpurie008ed22019-03-21 11:21:04 -07001984
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05301985 if (audio_extn_utils_is_dolby_format(out->format) &&
Ashish Jain81eb2a82015-05-13 10:52:34 +05301986 /*TODO:Extend code to support DTS passthrough*/
1987 /*set compressed channel status bits*/
Arun Mirpurie008ed22019-03-21 11:21:04 -07001988 audio_extn_passthru_is_passthrough_stream(out) &&
1989 audio_extn_is_hdmi_passthru_enabled()) {
Ashish Jain81eb2a82015-05-13 10:52:34 +05301990 get_compressed_channel_status(buffer, bytes, channel_status, AUDIO_PARSER_CODEC_AC3);
Arun Mirpurie008ed22019-03-21 11:21:04 -07001991 } else {
Ashish Jain81eb2a82015-05-13 10:52:34 +05301992 /*set channel status bit for LPCM*/
1993 get_lpcm_channel_status(out->sample_rate, channel_status);
1994 }
1995
1996 memcpy(iec958.status, channel_status,sizeof(iec958.status));
1997 ctl = mixer_get_ctl_by_name(out->dev->mixer, mixer_ctl_name);
1998 if (!ctl) {
1999 ALOGE("%s: Could not get ctl for mixer cmd - %s",
2000 __func__, mixer_ctl_name);
2001 return;
2002 }
2003 if (mixer_ctl_set_array(ctl, &iec958, sizeof(iec958)) < 0) {
2004 ALOGE("%s: Could not set channel status for ext HDMI ",
2005 __func__);
2006 return;
2007 }
2008
2009}
2010#endif
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302011
2012int audio_extn_utils_get_avt_device_drift(
2013 struct audio_usecase *usecase,
2014 struct audio_avt_device_drift_param *drift_param)
2015{
2016 int ret = 0, count = 0;
2017 char avt_device_drift_mixer_ctl_name[MIXER_PATH_MAX_LENGTH] = {0};
Naresh Tanniru6160c712017-04-17 15:43:48 +05302018 const char *backend = NULL;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302019 struct mixer_ctl *ctl = NULL;
2020 struct audio_avt_device_drift_stats drift_stats;
2021 struct audio_device *adev = NULL;
2022
2023 if (usecase != NULL && usecase->type == PCM_PLAYBACK) {
Naresh Tanniru6160c712017-04-17 15:43:48 +05302024 backend = platform_get_snd_device_backend_interface(usecase->out_snd_device);
2025 if (!backend) {
2026 ALOGE("%s: Unsupported device %d", __func__,
2027 usecase->stream.out->devices);
2028 ret = -EINVAL;
2029 goto done;
2030 }
2031 strlcpy(avt_device_drift_mixer_ctl_name,
2032 backend,
2033 MIXER_PATH_MAX_LENGTH);
2034
2035 count = strlen(backend);
2036 if (MIXER_PATH_MAX_LENGTH - count > 0) {
2037 strlcat(&avt_device_drift_mixer_ctl_name[count],
2038 " DRIFT",
2039 MIXER_PATH_MAX_LENGTH - count);
2040 } else {
2041 ret = -EINVAL;
2042 goto done;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302043 }
2044 } else {
Naresh Tanniru7586e292017-04-13 10:38:13 +05302045 ALOGE("%s: Invalid usecase",__func__);
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302046 ret = -EINVAL;
Naresh Tanniru6160c712017-04-17 15:43:48 +05302047 goto done;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302048 }
2049
Naresh Tanniru6160c712017-04-17 15:43:48 +05302050 adev = usecase->stream.out->dev;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302051 ctl = mixer_get_ctl_by_name(adev->mixer, avt_device_drift_mixer_ctl_name);
2052 if (!ctl) {
2053 ALOGE("%s: Could not get ctl for mixer cmd - %s",
2054 __func__, avt_device_drift_mixer_ctl_name);
2055
2056 ret = -EINVAL;
2057 goto done;
2058 }
2059
2060 ALOGV("%s: Getting AV Timer vs Device Drift mixer ctrl name %s", __func__,
2061 avt_device_drift_mixer_ctl_name);
2062
2063 mixer_ctl_update(ctl);
2064 count = mixer_ctl_get_num_values(ctl);
2065 if (count != sizeof(struct audio_avt_device_drift_stats)) {
2066 ALOGE("%s: mixer_ctl_get_num_values() invalid drift_stats data size",
2067 __func__);
2068
2069 ret = -EINVAL;
2070 goto done;
2071 }
2072
2073 ret = mixer_ctl_get_array(ctl, (void *)&drift_stats, count);
2074 if (ret != 0) {
2075 ALOGE("%s: mixer_ctl_get_array() failed to get drift_stats Params",
2076 __func__);
2077
2078 ret = -EINVAL;
2079 goto done;
2080 }
2081 memcpy(drift_param, &drift_stats.drift_param,
2082 sizeof(struct audio_avt_device_drift_param));
2083done:
2084 return ret;
2085}
Manish Dewangan07de2142017-02-27 19:27:20 +05302086
2087#ifdef SNDRV_COMPRESS_PATH_DELAY
2088int audio_extn_utils_compress_get_dsp_latency(struct stream_out *out)
2089{
2090 int ret = -EINVAL;
2091 struct snd_compr_metadata metadata;
2092 int delay_ms = COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
2093
Weiyin Jiangc49a3b52018-08-17 16:16:08 +08002094 /* override the latency for pcm offload use case */
2095 if ((out->flags & AUDIO_OUTPUT_FLAG_DIRECT) &&
2096 !(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)) {
2097 delay_ms = PCM_OFFLOAD_PLAYBACK_LATENCY;
2098 }
2099
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -07002100 if (property_get_bool("vendor.audio.playback.dsp.pathdelay", false)) {
Manish Dewangan07de2142017-02-27 19:27:20 +05302101 ALOGD("%s:: Quering DSP delay %d",__func__, __LINE__);
2102 if (!(is_offload_usecase(out->usecase))) {
2103 ALOGE("%s:: not supported for non offload session", __func__);
2104 goto exit;
2105 }
2106
2107 if (!out->compr) {
2108 ALOGD("%s:: Invalid compress handle,returning default dsp latency",
2109 __func__);
2110 goto exit;
2111 }
2112
2113 metadata.key = SNDRV_COMPRESS_PATH_DELAY;
2114 ret = compress_get_metadata(out->compr, &metadata);
2115 if(ret) {
2116 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2117 goto exit;
2118 }
2119 delay_ms = metadata.value[0] / 1000; /*convert to ms*/
2120 } else {
2121 ALOGD("%s:: Using Fix DSP delay",__func__);
2122 }
2123
2124exit:
2125 ALOGD("%s:: delay in ms is %d",__func__, delay_ms);
2126 return delay_ms;
2127}
2128#else
2129int audio_extn_utils_compress_get_dsp_latency(struct stream_out *out __unused)
2130{
2131 return COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
2132}
2133#endif
Manish Dewangan69426c82017-01-30 17:35:36 +05302134
2135#ifdef SNDRV_COMPRESS_RENDER_MODE
2136int audio_extn_utils_compress_set_render_mode(struct stream_out *out)
2137{
2138 struct snd_compr_metadata metadata;
2139 int ret = -EINVAL;
2140
2141 if (!(is_offload_usecase(out->usecase))) {
2142 ALOGE("%s:: not supported for non offload session", __func__);
2143 goto exit;
2144 }
2145
2146 if (!out->compr) {
2147 ALOGD("%s:: Invalid compress handle",
2148 __func__);
2149 goto exit;
2150 }
2151
2152 ALOGD("%s:: render mode %d", __func__, out->render_mode);
2153
2154 metadata.key = SNDRV_COMPRESS_RENDER_MODE;
2155 if (out->render_mode == RENDER_MODE_AUDIO_MASTER) {
2156 metadata.value[0] = SNDRV_COMPRESS_RENDER_MODE_AUDIO_MASTER;
2157 } else if (out->render_mode == RENDER_MODE_AUDIO_STC_MASTER) {
2158 metadata.value[0] = SNDRV_COMPRESS_RENDER_MODE_STC_MASTER;
2159 } else {
2160 ret = 0;
2161 goto exit;
2162 }
2163 ret = compress_set_metadata(out->compr, &metadata);
2164 if(ret) {
2165 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2166 }
2167exit:
2168 return ret;
2169}
2170#else
2171int audio_extn_utils_compress_set_render_mode(struct stream_out *out __unused)
2172{
2173 ALOGD("%s:: configuring render mode not supported", __func__);
2174 return 0;
2175}
2176#endif
Manish Dewangan58229382017-02-02 15:48:41 +05302177
2178#ifdef SNDRV_COMPRESS_CLK_REC_MODE
2179int audio_extn_utils_compress_set_clk_rec_mode(
2180 struct audio_usecase *usecase)
2181{
2182 struct snd_compr_metadata metadata;
2183 struct stream_out *out = NULL;
2184 int ret = -EINVAL;
2185
Naresh Tanniru7586e292017-04-13 10:38:13 +05302186 if (usecase == NULL || usecase->type != PCM_PLAYBACK) {
Manish Dewangan58229382017-02-02 15:48:41 +05302187 ALOGE("%s:: Invalid use case", __func__);
2188 goto exit;
2189 }
2190
2191 out = usecase->stream.out;
2192 if (!out) {
2193 ALOGE("%s:: invalid stream", __func__);
2194 goto exit;
2195 }
2196
2197 if (!is_offload_usecase(out->usecase)) {
2198 ALOGE("%s:: not supported for non offload session", __func__);
2199 goto exit;
2200 }
2201
2202 if (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER) {
2203 ALOGD("%s:: clk recovery is only supported in STC render mode",
2204 __func__);
2205 ret = 0;
2206 goto exit;
2207 }
2208
2209 if (!out->compr) {
2210 ALOGD("%s:: Invalid compress handle",
2211 __func__);
2212 goto exit;
2213 }
2214 metadata.key = SNDRV_COMPRESS_CLK_REC_MODE;
2215 switch(usecase->out_snd_device) {
2216 case SND_DEVICE_OUT_HDMI:
2217 case SND_DEVICE_OUT_SPEAKER_AND_HDMI:
2218 case SND_DEVICE_OUT_DISPLAY_PORT:
2219 case SND_DEVICE_OUT_SPEAKER_AND_DISPLAY_PORT:
2220 metadata.value[0] = SNDRV_COMPRESS_CLK_REC_MODE_NONE;
2221 break;
2222 default:
2223 metadata.value[0] = SNDRV_COMPRESS_CLK_REC_MODE_AUTO;
2224 break;
2225 }
2226
2227 ALOGD("%s:: clk recovery mode %d",__func__, metadata.value[0]);
2228
2229 ret = compress_set_metadata(out->compr, &metadata);
2230 if(ret) {
2231 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2232 }
2233
2234exit:
2235 return ret;
2236}
2237#else
2238int audio_extn_utils_compress_set_clk_rec_mode(
2239 struct audio_usecase *usecase __unused)
2240{
2241 ALOGD("%s:: configuring render mode not supported", __func__);
2242 return 0;
2243}
2244#endif
Manish Dewangan27346042017-03-01 12:56:12 +05302245
2246#ifdef SNDRV_COMPRESS_RENDER_WINDOW
2247int audio_extn_utils_compress_set_render_window(
2248 struct stream_out *out,
2249 struct audio_out_render_window_param *render_window)
2250{
2251 struct snd_compr_metadata metadata;
2252 int ret = -EINVAL;
2253
Manish Dewangan27346042017-03-01 12:56:12 +05302254 if(render_window == NULL) {
2255 ALOGE("%s:: Invalid render_window", __func__);
2256 goto exit;
2257 }
2258
Aniket Kumar Lata1e1d3662017-06-01 18:45:48 -07002259 ALOGD("%s:: render window start 0x%"PRIx64" end 0x%"PRIx64"",
2260 __func__,render_window->render_ws, render_window->render_we);
2261
Manish Dewangan27346042017-03-01 12:56:12 +05302262 if (!is_offload_usecase(out->usecase)) {
2263 ALOGE("%s:: not supported for non offload session", __func__);
2264 goto exit;
2265 }
2266
Naresh Tanniru6160c712017-04-17 15:43:48 +05302267 if ((out->render_mode != RENDER_MODE_AUDIO_MASTER) &&
2268 (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER)) {
Manish Dewangan27346042017-03-01 12:56:12 +05302269 ALOGD("%s:: only supported in timestamp mode, current "
2270 "render mode mode %d", __func__, out->render_mode);
2271 goto exit;
2272 }
2273
2274 if (!out->compr) {
2275 ALOGW("%s:: offload session not yet opened,"
2276 "render window will be configure later", __func__);
2277 /* store render window to reconfigure in start_output_stream() */
2278 goto exit;
2279 }
2280
2281 metadata.key = SNDRV_COMPRESS_RENDER_WINDOW;
2282 /*render window start value */
2283 metadata.value[0] = 0xFFFFFFFF & render_window->render_ws; /* lsb */
2284 metadata.value[1] = \
2285 (0xFFFFFFFF00000000 & render_window->render_ws) >> 32; /* msb*/
2286 /*render window end value */
2287 metadata.value[2] = 0xFFFFFFFF & render_window->render_we; /* lsb */
2288 metadata.value[3] = \
2289 (0xFFFFFFFF00000000 & render_window->render_we) >> 32; /* msb*/
2290
2291 ret = compress_set_metadata(out->compr, &metadata);
2292 if(ret) {
2293 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2294 }
2295
2296exit:
2297 return ret;
2298}
2299#else
2300int audio_extn_utils_compress_set_render_window(
2301 struct stream_out *out __unused,
2302 struct audio_out_render_window_param *render_window __unused)
2303{
2304 ALOGD("%s:: configuring render window not supported", __func__);
2305 return 0;
2306}
2307#endif
Manish Dewangan14956cc2017-02-14 18:54:42 +05302308
2309#ifdef SNDRV_COMPRESS_START_DELAY
2310int audio_extn_utils_compress_set_start_delay(
2311 struct stream_out *out,
2312 struct audio_out_start_delay_param *delay_param)
2313{
2314 struct snd_compr_metadata metadata;
2315 int ret = -EINVAL;
2316
2317 if(delay_param == NULL) {
2318 ALOGE("%s:: Invalid delay_param", __func__);
2319 goto exit;
2320 }
2321
2322 ALOGD("%s:: render start delay 0x%"PRIx64" ", __func__,
2323 delay_param->start_delay);
2324
2325 if (!is_offload_usecase(out->usecase)) {
2326 ALOGE("%s:: not supported for non offload session", __func__);
2327 goto exit;
2328 }
2329
Naresh Tanniru6160c712017-04-17 15:43:48 +05302330 if ((out->render_mode != RENDER_MODE_AUDIO_MASTER) &&
2331 (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER)) {
Manish Dewangan14956cc2017-02-14 18:54:42 +05302332 ALOGD("%s:: only supported in timestamp mode, current "
2333 "render mode mode %d", __func__, out->render_mode);
2334 goto exit;
2335 }
2336
2337 if (!out->compr) {
2338 ALOGW("%s:: offload session not yet opened,"
2339 "start delay will be configure later", __func__);
2340 goto exit;
2341 }
2342
2343 metadata.key = SNDRV_COMPRESS_START_DELAY;
2344 metadata.value[0] = 0xFFFFFFFF & delay_param->start_delay; /* lsb */
2345 metadata.value[1] = \
2346 (0xFFFFFFFF00000000 & delay_param->start_delay) >> 32; /* msb*/
2347
2348 ret = compress_set_metadata(out->compr, &metadata);
2349 if(ret) {
2350 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2351 }
2352
2353exit:
2354 return ret;
2355}
2356#else
2357int audio_extn_utils_compress_set_start_delay(
2358 struct stream_out *out __unused,
2359 struct audio_out_start_delay_param *delay_param __unused)
2360{
2361 ALOGD("%s:: configuring render window not supported", __func__);
2362 return 0;
2363}
2364#endif
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002365
Surendar Karka287348c2019-04-10 18:31:46 +05302366#ifdef SNDRV_COMPRESS_DSP_POSITION
2367int audio_extn_utils_compress_get_dsp_presentation_pos(struct stream_out *out,
2368 uint64_t *frames, struct timespec *timestamp, int32_t clock_id)
2369{
2370 int ret = -EINVAL;
2371 uint64_t *val = NULL;
2372 uint64_t time = 0;
2373 struct snd_compr_metadata metadata;
2374
2375 ALOGV("%s:: Quering DSP position with clock id %d",__func__, clock_id);
2376 metadata.key = SNDRV_COMPRESS_DSP_POSITION;
2377 metadata.value[0] = clock_id;
2378 ret = compress_get_metadata(out->compr, &metadata);
2379 if (ret) {
2380 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2381 ret = -errno;
2382 goto exit;
2383 }
2384 val = (uint64_t *)&metadata.value[1];
2385 *frames = *val;
2386 time = *(val + 1);
2387 timestamp->tv_sec = time / 1000000;
2388 timestamp->tv_nsec = (time % 1000000)*1000;
2389
2390exit:
2391 return ret;
2392}
2393#else
2394int audio_extn_utils_compress_get_dsp_presentation_pos(struct stream_out *out __unused,
2395 uint64_t *frames __unused, struct timespec *timestamp __unused,
2396 int32_t clock_id __unused)
2397{
2398 ALOGD("%s:: dsp presentation position not supported", __func__);
2399 return 0;
2400
2401}
2402#endif
2403
2404#ifdef SNDRV_PCM_IOCTL_DSP_POSITION
2405int audio_extn_utils_pcm_get_dsp_presentation_pos(struct stream_out *out,
2406 uint64_t *frames, struct timespec *timestamp, int32_t clock_id)
2407{
2408 int ret = -EINVAL;
2409 uint64_t time = 0;
2410 struct snd_pcm_prsnt_position prsnt_position;
2411
2412 ALOGV("%s:: Quering DSP position with clock id %d",__func__, clock_id);
2413 prsnt_position.clock_id = clock_id;
2414 ret = pcm_ioctl(out->pcm, SNDRV_PCM_IOCTL_DSP_POSITION, &prsnt_position);
2415 if (ret) {
2416 ALOGE("%s::error %d", __func__, ret);
2417 ret = -EIO;
2418 goto exit;
2419 }
2420
2421 *frames = prsnt_position.frames;
2422 time = prsnt_position.timestamp;
2423 timestamp->tv_sec = time / 1000000;
2424 timestamp->tv_nsec = (time % 1000000)*1000;
2425
2426exit:
2427 return ret;
2428}
2429#else
2430int audio_extn_utils_pcm_get_dsp_presentation_pos(struct stream_out *out __unused,
2431 uint64_t *frames __unused, struct timespec *timestamp __unused,
2432 int32_t clock_id __unused)
2433{
2434 ALOGD("%s:: dsp presentation position not supported", __func__);
2435 return 0;
2436
2437}
2438#endif
2439
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002440#define MAX_SND_CARD 8
Ashish Jain30ae8942017-11-05 17:21:23 +05302441#define RETRY_US 1000000
2442#define RETRY_NUMBER 40
Aalique Grahame22e49102018-12-18 14:23:57 -08002443#define PLATFORM_INFO_XML_PATH "audio_platform_info.xml"
2444#define PLATFORM_INFO_XML_BASE_STRING "audio_platform_info"
2445
2446#ifdef LINUX_ENABLED
2447static const char *kConfigLocationList[] =
2448 {"/etc"};
2449#else
2450static const char *kConfigLocationList[] =
2451 {"/vendor/etc"};
2452#endif
2453static const int kConfigLocationListSize =
2454 (sizeof(kConfigLocationList) / sizeof(kConfigLocationList[0]));
2455
2456bool audio_extn_utils_resolve_config_file(char file_name[MIXER_PATH_MAX_LENGTH])
2457{
2458 char full_config_path[MIXER_PATH_MAX_LENGTH];
2459 for (int i = 0; i < kConfigLocationListSize; i++) {
2460 snprintf(full_config_path,
2461 MIXER_PATH_MAX_LENGTH,
2462 "%s/%s",
2463 kConfigLocationList[i],
2464 file_name);
2465 if (F_OK == access(full_config_path, 0)) {
Weiyin Jiang2995f662019-04-17 14:25:12 +08002466 strlcpy(file_name, full_config_path, MIXER_PATH_MAX_LENGTH);
Aalique Grahame22e49102018-12-18 14:23:57 -08002467 return true;
2468 }
2469 }
2470 return false;
2471}
2472
2473/* platform_info_file should be size 'MIXER_PATH_MAX_LENGTH' */
2474int audio_extn_utils_get_platform_info(const char* snd_card_name, char* platform_info_file)
2475{
2476 if (NULL == snd_card_name) {
2477 return -1;
2478 }
2479
2480 struct snd_card_split *snd_split_handle = NULL;
2481 int ret = 0;
2482 audio_extn_set_snd_card_split(snd_card_name);
2483 snd_split_handle = audio_extn_get_snd_card_split();
2484
2485 snprintf(platform_info_file, MIXER_PATH_MAX_LENGTH, "%s_%s_%s.xml",
2486 PLATFORM_INFO_XML_BASE_STRING, snd_split_handle->snd_card,
2487 snd_split_handle->form_factor);
2488
2489 if (!audio_extn_utils_resolve_config_file(platform_info_file)) {
2490 memset(platform_info_file, 0, MIXER_PATH_MAX_LENGTH);
2491 snprintf(platform_info_file, MIXER_PATH_MAX_LENGTH, "%s_%s.xml",
2492 PLATFORM_INFO_XML_BASE_STRING, snd_split_handle->snd_card);
2493
2494 if (!audio_extn_utils_resolve_config_file(platform_info_file)) {
2495 memset(platform_info_file, 0, MIXER_PATH_MAX_LENGTH);
2496 strlcpy(platform_info_file, PLATFORM_INFO_XML_PATH, MIXER_PATH_MAX_LENGTH);
2497 ret = audio_extn_utils_resolve_config_file(platform_info_file) ? 0 : -1;
2498 }
2499 }
2500
2501 return ret;
2502}
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002503
2504int audio_extn_utils_get_snd_card_num()
2505{
Soumya Managoli9fee7c62018-04-06 16:21:50 +05302506 int snd_card_num = 0;
2507 struct mixer *mixer = NULL;
2508
2509 snd_card_num = audio_extn_utils_open_snd_mixer(&mixer);
2510 if (mixer)
2511 mixer_close(mixer);
2512 return snd_card_num;
2513}
2514
2515int audio_extn_utils_open_snd_mixer(struct mixer **mixer_handle)
2516{
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002517
2518 void *hw_info = NULL;
2519 struct mixer *mixer = NULL;
2520 int retry_num = 0;
mpangfaa7bae2019-01-15 16:38:13 +08002521 int snd_card_num = 0;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002522 char* snd_card_name = NULL;
mpangfaa7bae2019-01-15 16:38:13 +08002523 int snd_card_detection_info[MAX_SND_CARD] = {0};
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002524
Soumya Managoli9fee7c62018-04-06 16:21:50 +05302525 if (!mixer_handle) {
2526 ALOGE("invalid mixer handle");
2527 return -1;
2528 }
2529 *mixer_handle = NULL;
Ashish Jain30ae8942017-11-05 17:21:23 +05302530 /*
mpangfaa7bae2019-01-15 16:38:13 +08002531 * Try with all the sound cards ( 0 to 7 ) and if none of them were detected
Ashish Jain30ae8942017-11-05 17:21:23 +05302532 * sleep for 1 sec and try detections with sound card 0 again.
2533 * If sound card gets detected, check if it is relevant, if not check with the
2534 * other sound cards. To ensure that the irrelevant sound card is not check again,
2535 * we maintain it in min_snd_card_num.
2536 */
2537 while (retry_num < RETRY_NUMBER) {
mpangfaa7bae2019-01-15 16:38:13 +08002538 snd_card_num = 0;
2539 while (snd_card_num < MAX_SND_CARD) {
2540 if (snd_card_detection_info[snd_card_num] == 0) {
2541 mixer = mixer_open(snd_card_num);
2542 if (!mixer)
2543 snd_card_num++;
2544 else
2545 break;
2546 } else
2547 snd_card_num++;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002548 }
2549
2550 if (!mixer) {
Ashish Jain30ae8942017-11-05 17:21:23 +05302551 usleep(RETRY_US);
Ashish Jain30ae8942017-11-05 17:21:23 +05302552 retry_num++;
mpangfaa7bae2019-01-15 16:38:13 +08002553 ALOGD("%s: retry, retry_num %d", __func__, retry_num);
Ashish Jain30ae8942017-11-05 17:21:23 +05302554 continue;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002555 }
2556
2557 snd_card_name = strdup(mixer_get_name(mixer));
2558 if (!snd_card_name) {
2559 ALOGE("failed to allocate memory for snd_card_name\n");
2560 mixer_close(mixer);
2561 return -1;
2562 }
2563 ALOGD("%s: snd_card_name: %s", __func__, snd_card_name);
mpangfaa7bae2019-01-15 16:38:13 +08002564 snd_card_detection_info[snd_card_num] = 1;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002565 hw_info = hw_info_init(snd_card_name);
2566 if (hw_info) {
2567 ALOGD("%s: Opened sound card:%d", __func__, snd_card_num);
2568 break;
2569 }
mpangfaa7bae2019-01-15 16:38:13 +08002570 ALOGE("%s: Failed to init hardware info, snd_card_num:%d", __func__, snd_card_num);
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002571
Dhananjay Kumara7e27832017-07-11 15:40:39 +05302572 free(snd_card_name);
2573 snd_card_name = NULL;
2574
2575 mixer_close(mixer);
2576 mixer = NULL;
2577 }
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002578 if (snd_card_name)
2579 free(snd_card_name);
Ashish Jain30ae8942017-11-05 17:21:23 +05302580
Dhananjay Kumara7e27832017-07-11 15:40:39 +05302581 if (hw_info)
2582 hw_info_deinit(hw_info);
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002583
mpangfaa7bae2019-01-15 16:38:13 +08002584 if (retry_num >= RETRY_NUMBER) {
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002585 ALOGE("%s: Unable to find correct sound card, aborting.", __func__);
2586 return -1;
2587 }
2588
Soumya Managoli9fee7c62018-04-06 16:21:50 +05302589 if (mixer)
2590 *mixer_handle = mixer;
2591
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002592 return snd_card_num;
2593}
Naresh Tanniru6160c712017-04-17 15:43:48 +05302594
Soumya Managoli9fee7c62018-04-06 16:21:50 +05302595void audio_extn_utils_close_snd_mixer(struct mixer *mixer)
2596{
2597 if (mixer)
2598 mixer_close(mixer);
2599}
2600
Naresh Tanniru6160c712017-04-17 15:43:48 +05302601#ifdef SNDRV_COMPRESS_ENABLE_ADJUST_SESSION_CLOCK
2602int audio_extn_utils_compress_enable_drift_correction(
2603 struct stream_out *out,
2604 struct audio_out_enable_drift_correction *drift)
2605{
2606 struct snd_compr_metadata metadata;
2607 int ret = -EINVAL;
2608
2609 if(drift == NULL) {
2610 ALOGE("%s:: Invalid param", __func__);
2611 goto exit;
2612 }
2613
2614 ALOGD("%s:: drift enable %d", __func__,drift->enable);
2615
2616 if (!is_offload_usecase(out->usecase)) {
2617 ALOGE("%s:: not supported for non offload session", __func__);
2618 goto exit;
2619 }
2620
2621 if (!out->compr) {
2622 ALOGW("%s:: offload session not yet opened,"
2623 "start delay will be configure later", __func__);
2624 goto exit;
2625 }
2626
2627 metadata.key = SNDRV_COMPRESS_ENABLE_ADJUST_SESSION_CLOCK;
2628 metadata.value[0] = drift->enable;
2629 out->drift_correction_enabled = drift->enable;
2630
2631 ret = compress_set_metadata(out->compr, &metadata);
2632 if(ret) {
2633 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2634 out->drift_correction_enabled = false;
2635 }
2636
2637exit:
2638 return ret;
2639}
2640#else
2641int audio_extn_utils_compress_enable_drift_correction(
2642 struct stream_out *out __unused,
2643 struct audio_out_enable_drift_correction *drift __unused)
2644{
2645 ALOGD("%s:: configuring drift enablement not supported", __func__);
2646 return 0;
2647}
2648#endif
2649
2650#ifdef SNDRV_COMPRESS_ADJUST_SESSION_CLOCK
2651int audio_extn_utils_compress_correct_drift(
2652 struct stream_out *out,
2653 struct audio_out_correct_drift *drift_param)
2654{
2655 struct snd_compr_metadata metadata;
2656 int ret = -EINVAL;
2657
2658 if (drift_param == NULL) {
2659 ALOGE("%s:: Invalid drift_param", __func__);
2660 goto exit;
2661 }
2662
2663 ALOGD("%s:: adjust time 0x%"PRIx64" ", __func__,
2664 drift_param->adjust_time);
2665
2666 if (!is_offload_usecase(out->usecase)) {
2667 ALOGE("%s:: not supported for non offload session", __func__);
2668 goto exit;
2669 }
2670
2671 if (!out->compr) {
2672 ALOGW("%s:: offload session not yet opened", __func__);
2673 goto exit;
2674 }
2675
2676 if (!out->drift_correction_enabled) {
2677 ALOGE("%s:: drift correction not enabled", __func__);
2678 goto exit;
2679 }
2680
2681 metadata.key = SNDRV_COMPRESS_ADJUST_SESSION_CLOCK;
2682 metadata.value[0] = 0xFFFFFFFF & drift_param->adjust_time; /* lsb */
2683 metadata.value[1] = \
2684 (0xFFFFFFFF00000000 & drift_param->adjust_time) >> 32; /* msb*/
2685
2686 ret = compress_set_metadata(out->compr, &metadata);
2687 if(ret)
2688 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2689exit:
2690 return ret;
2691}
2692#else
2693int audio_extn_utils_compress_correct_drift(
2694 struct stream_out *out __unused,
2695 struct audio_out_correct_drift *drift_param __unused)
2696{
2697 ALOGD("%s:: setting adjust clock not supported", __func__);
2698 return 0;
2699}
2700#endif
Naresh Tanniru29bce4e2017-04-27 17:54:30 +05302701
2702int audio_extn_utils_set_channel_map(
2703 struct stream_out *out,
2704 struct audio_out_channel_map_param *channel_map_param)
2705{
2706 int ret = -EINVAL, i = 0;
2707 int channels = audio_channel_count_from_out_mask(out->channel_mask);
2708
2709 if (channel_map_param == NULL) {
2710 ALOGE("%s:: Invalid channel_map", __func__);
2711 goto exit;
2712 }
2713
2714 if (channel_map_param->channels != channels) {
2715 ALOGE("%s:: Channels(%d) does not match stream channels(%d)",
2716 __func__, channel_map_param->channels, channels);
2717 goto exit;
2718 }
2719
2720 for ( i = 0; i < channels; i++) {
2721 ALOGV("%s:: channel_map[%d]- %d", __func__, i, channel_map_param->channel_map[i]);
2722 out->channel_map_param.channel_map[i] = channel_map_param->channel_map[i];
2723 }
2724 ret = 0;
2725exit:
2726 return ret;
2727}
Varun Balaraje49253e2017-07-06 19:48:56 +05302728
2729int audio_extn_utils_set_pan_scale_params(
2730 struct stream_out *out,
2731 struct mix_matrix_params *mm_params)
2732{
2733 int ret = -EINVAL, i = 0, j = 0;
2734
Varun Balaraj003ee752017-08-07 17:54:55 +05302735 if (mm_params == NULL || out == NULL) {
2736 ALOGE("%s:: Invalid mix matrix or out param", __func__);
Varun Balaraje49253e2017-07-06 19:48:56 +05302737 goto exit;
2738 }
2739
2740 if (mm_params->num_output_channels > MAX_CHANNELS_SUPPORTED ||
2741 mm_params->num_output_channels <= 0 ||
2742 mm_params->num_input_channels > MAX_CHANNELS_SUPPORTED ||
2743 mm_params->num_input_channels <= 0)
2744 goto exit;
2745
2746 out->pan_scale_params.num_output_channels = mm_params->num_output_channels;
2747 out->pan_scale_params.num_input_channels = mm_params->num_input_channels;
2748 out->pan_scale_params.has_output_channel_map =
2749 mm_params->has_output_channel_map;
2750 for (i = 0; i < mm_params->num_output_channels; i++)
2751 out->pan_scale_params.output_channel_map[i] =
2752 mm_params->output_channel_map[i];
2753
2754 out->pan_scale_params.has_input_channel_map =
2755 mm_params->has_input_channel_map;
2756 for (i = 0; i < mm_params->num_input_channels; i++)
2757 out->pan_scale_params.input_channel_map[i] =
2758 mm_params->input_channel_map[i];
2759
2760 out->pan_scale_params.has_mixer_coeffs = mm_params->has_mixer_coeffs;
2761 for (i = 0; i < mm_params->num_output_channels; i++)
2762 for (j = 0; j < mm_params->num_input_channels; j++) {
2763 //Convert the channel coefficient gains in Q14 format
2764 out->pan_scale_params.mixer_coeffs[i][j] =
2765 mm_params->mixer_coeffs[i][j] * (2 << 13);
2766 }
2767
2768 ret = platform_set_stream_pan_scale_params(out->dev->platform,
2769 out->pcm_device_id,
2770 out->pan_scale_params);
2771
2772exit:
2773 return ret;
2774}
2775
2776int audio_extn_utils_set_downmix_params(
2777 struct stream_out *out,
2778 struct mix_matrix_params *mm_params)
2779{
2780 int ret = -EINVAL, i = 0, j = 0;
2781 struct audio_usecase *usecase = NULL;
2782
Aniket Kumar Lataf9f246e2017-09-15 15:20:16 -07002783 if (mm_params == NULL || out == NULL) {
Varun Balaraj003ee752017-08-07 17:54:55 +05302784 ALOGE("%s:: Invalid mix matrix or out param", __func__);
Varun Balaraje49253e2017-07-06 19:48:56 +05302785 goto exit;
2786 }
2787
2788 if (mm_params->num_output_channels > MAX_CHANNELS_SUPPORTED ||
2789 mm_params->num_output_channels <= 0 ||
2790 mm_params->num_input_channels > MAX_CHANNELS_SUPPORTED ||
2791 mm_params->num_input_channels <= 0)
2792 goto exit;
2793
2794 usecase = get_usecase_from_list(out->dev, out->usecase);
Varun Balaraj003ee752017-08-07 17:54:55 +05302795 if (!usecase) {
2796 ALOGE("%s: Get usecase list failed!", __func__);
Aniket Kumar Lataf9f246e2017-09-15 15:20:16 -07002797 goto exit;
2798 }
Varun Balaraje49253e2017-07-06 19:48:56 +05302799 out->downmix_params.num_output_channels = mm_params->num_output_channels;
2800 out->downmix_params.num_input_channels = mm_params->num_input_channels;
2801
2802 out->downmix_params.has_output_channel_map =
2803 mm_params->has_output_channel_map;
2804 for (i = 0; i < mm_params->num_output_channels; i++) {
2805 out->downmix_params.output_channel_map[i] =
2806 mm_params->output_channel_map[i];
2807 }
2808
2809 out->downmix_params.has_input_channel_map =
2810 mm_params->has_input_channel_map;
2811 for (i = 0; i < mm_params->num_input_channels; i++)
2812 out->downmix_params.input_channel_map[i] =
2813 mm_params->input_channel_map[i];
2814
2815 out->downmix_params.has_mixer_coeffs = mm_params->has_mixer_coeffs;
2816 for (i = 0; i < mm_params->num_output_channels; i++)
Nikhil Latukar2e6f6242017-08-15 13:37:07 +05302817 for (j = 0; j < mm_params->num_input_channels; j++) {
2818 //Convert the channel coefficient gains in Q14 format
Varun Balaraje49253e2017-07-06 19:48:56 +05302819 out->downmix_params.mixer_coeffs[i][j] =
Nikhil Latukar2e6f6242017-08-15 13:37:07 +05302820 mm_params->mixer_coeffs[i][j] * (2 << 13);
2821 }
Varun Balaraje49253e2017-07-06 19:48:56 +05302822
2823 ret = platform_set_stream_downmix_params(out->dev->platform,
2824 out->pcm_device_id,
2825 usecase->out_snd_device,
2826 out->downmix_params);
2827
2828exit:
2829 return ret;
2830}
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05302831
2832bool audio_extn_utils_is_dolby_format(audio_format_t format)
2833{
2834 if (format == AUDIO_FORMAT_AC3 ||
2835 format == AUDIO_FORMAT_E_AC3 ||
2836 format == AUDIO_FORMAT_E_AC3_JOC)
2837 return true;
2838 else
2839 return false;
2840}
2841
`Deeraj Soman676c2702017-09-18 19:25:53 +05302842int audio_extn_utils_get_bit_width_from_string(const char *id_string)
2843{
2844 int i;
2845 const mixer_config_lookup mixer_bitwidth_config[] = {{"S24_3LE", 24},
2846 {"S32_LE", 32},
2847 {"S24_LE", 24},
2848 {"S16_LE", 16}};
2849 int num_configs = sizeof(mixer_bitwidth_config) / sizeof(mixer_bitwidth_config[0]);
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05302850
`Deeraj Soman676c2702017-09-18 19:25:53 +05302851 for (i = 0; i < num_configs; i++) {
2852 if (!strcmp(id_string, mixer_bitwidth_config[i].id_string))
2853 return mixer_bitwidth_config[i].value;
2854 }
2855
2856 return -EINVAL;
2857}
2858
2859int audio_extn_utils_get_sample_rate_from_string(const char *id_string)
2860{
2861 int i;
2862 const mixer_config_lookup mixer_samplerate_config[] = {{"KHZ_32", 32000},
2863 {"KHZ_48", 48000},
2864 {"KHZ_96", 96000},
2865 {"KHZ_144", 144000},
2866 {"KHZ_192", 192000},
2867 {"KHZ_384", 384000},
2868 {"KHZ_44P1", 44100},
2869 {"KHZ_88P2", 88200},
2870 {"KHZ_176P4", 176400},
2871 {"KHZ_352P8", 352800}};
2872 int num_configs = sizeof(mixer_samplerate_config) / sizeof(mixer_samplerate_config[0]);
2873
2874 for (i = 0; i < num_configs; i++) {
2875 if (!strcmp(id_string, mixer_samplerate_config[i].id_string))
2876 return mixer_samplerate_config[i].value;
2877 }
2878
2879 return -EINVAL;
2880}
2881
2882int audio_extn_utils_get_channels_from_string(const char *id_string)
2883{
2884 int i;
2885 const mixer_config_lookup mixer_channels_config[] = {{"One", 1},
2886 {"Two", 2},
2887 {"Three",3},
2888 {"Four", 4},
2889 {"Five", 5},
2890 {"Six", 6},
2891 {"Seven", 7},
2892 {"Eight", 8}};
2893 int num_configs = sizeof(mixer_channels_config) / sizeof(mixer_channels_config[0]);
2894
2895 for (i = 0; i < num_configs; i++) {
2896 if (!strcmp(id_string, mixer_channels_config[i].id_string))
2897 return mixer_channels_config[i].value;
2898 }
2899
2900 return -EINVAL;
2901}
Surendar karka30569792018-05-08 12:02:21 +05302902
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08002903void audio_extn_utils_release_snd_device(snd_device_t snd_device)
2904{
2905 audio_extn_dev_arbi_release(snd_device);
2906 audio_extn_sound_trigger_update_device_status(snd_device,
2907 ST_EVENT_SND_DEVICE_FREE);
2908 audio_extn_listen_update_device_status(snd_device,
2909 LISTEN_EVENT_SND_DEVICE_FREE);
2910}
2911
Surendar karka30569792018-05-08 12:02:21 +05302912int audio_extn_utils_get_license_params
2913(
2914const struct audio_device *adev,
2915struct audio_license_params *license_params
2916)
2917{
2918 if(!license_params)
2919 return -EINVAL;
2920 return platform_get_license_by_product(adev->platform, (const char*)license_params->product, &license_params->key, license_params->license);
2921}
2922
Aalique Grahame22e49102018-12-18 14:23:57 -08002923int audio_extn_utils_send_app_type_gain(struct audio_device *adev,
2924 int app_type,
2925 int *gain)
2926{
2927 int gain_cfg[4];
2928 const char *mixer_ctl_name = "App Type Gain";
2929 struct mixer_ctl *ctl;
2930 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
2931 if (!ctl) {
2932 ALOGE("%s: Could not get volume ctl mixer %s", __func__,
2933 mixer_ctl_name);
2934 return -EINVAL;
2935 }
2936 gain_cfg[0] = 0;
2937 gain_cfg[1] = app_type;
2938 gain_cfg[2] = gain[0];
2939 gain_cfg[3] = gain[1];
2940 ALOGV("%s app_type %d l(%d) r(%d)", __func__, app_type, gain[0], gain[1]);
2941 return mixer_ctl_set_array(ctl, gain_cfg,
2942 sizeof(gain_cfg)/sizeof(gain_cfg[0]));
2943}
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -07002944
2945int audio_extn_utils_is_vendor_enhanced_fwk()
2946{
2947 static int is_running_with_enhanced_fwk = -EINVAL;
2948
2949 if (is_running_with_enhanced_fwk == -EINVAL) {
2950 vndk_fwk_lib_handle = dlopen(VNDK_FWK_LIB_PATH, RTLD_NOW);
2951 if (vndk_fwk_lib_handle != NULL) {
2952 vndk_fwk_isVendorEnhancedFwk = (vndk_fwk_isVendorEnhancedFwk_t)
2953 dlsym(vndk_fwk_lib_handle, "isRunningWithVendorEnhancedFramework");
2954 if (vndk_fwk_isVendorEnhancedFwk == NULL) {
2955 ALOGW("%s: dlsym failed, defaulting to enhanced_fwk configuration",
2956 __func__);
2957 is_running_with_enhanced_fwk = 1;
2958 } else {
2959 is_running_with_enhanced_fwk = vndk_fwk_isVendorEnhancedFwk();
2960 }
2961 dlclose(vndk_fwk_lib_handle);
2962 vndk_fwk_lib_handle = NULL;
2963 } else {
2964 ALOGW("%s: VNDK_FWK_LIB not found, setting stock configuration", __func__);
2965 is_running_with_enhanced_fwk = 0;
2966 }
2967 ALOGV("%s: vndk_fwk_isVendorEnhancedFwk=%d", __func__, is_running_with_enhanced_fwk);
2968 }
2969
2970 return is_running_with_enhanced_fwk;
2971}
Deeraj Soman14230922019-01-30 16:39:30 +05302972
Deeraj Somanfa377bf2019-02-06 12:57:59 +05302973int audio_extn_utils_get_perf_mode_flag(void)
2974{
2975#ifdef COMPRESSED_PERF_MODE_FLAG
2976 return COMPRESSED_PERF_MODE_FLAG;
2977#else
2978 return 0;
2979#endif
2980}
2981
Deeraj Soman14230922019-01-30 16:39:30 +05302982size_t audio_extn_utils_get_input_buffer_size(uint32_t sample_rate,
2983 audio_format_t format,
2984 int channel_count,
2985 int64_t duration_ms,
2986 bool is_low_latency)
2987{
2988 size_t size = 0;
2989 size_t capture_duration = AUDIO_CAPTURE_PERIOD_DURATION_MSEC;
2990 uint32_t bytes_per_period_sample = 0;
2991
2992
2993 if (audio_extn_utils_check_input_parameters(sample_rate, format, channel_count) != 0)
2994 return 0;
2995
2996 if (duration_ms >= MIN_OFFLOAD_BUFFER_DURATION_MS && duration_ms <= MAX_OFFLOAD_BUFFER_DURATION_MS)
2997 capture_duration = duration_ms;
2998
2999 size = (sample_rate * capture_duration) / 1000;
3000 if (is_low_latency)
3001 size = LOW_LATENCY_CAPTURE_PERIOD_SIZE;
3002
3003
3004 bytes_per_period_sample = audio_bytes_per_sample(format) * channel_count;
3005 size *= bytes_per_period_sample;
3006
3007 /* make sure the size is multiple of 32 bytes and additionally multiple of
3008 * the frame_size (required for 24bit samples and non-power-of-2 channel counts)
3009 * At 48 kHz mono 16-bit PCM:
3010 * 5.000 ms = 240 frames = 15*16*1*2 = 480, a whole multiple of 32 (15)
3011 * 3.333 ms = 160 frames = 10*16*1*2 = 320, a whole multiple of 32 (10)
3012 *
3013 * The loop reaches result within 32 iterations, as initial size is
3014 * already a multiple of frame_size
3015 */
3016 size = audio_extn_utils_nearest_multiple(size, audio_extn_utils_lcm(32, bytes_per_period_sample));
3017
3018 return size;
3019}