blob: 9abddb90e529608f2ea5ab02bfcb8fc005ef5704 [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"
Narsinga Rao Chella212e2542014-11-17 19:57:04 -080040#include "voice.h"
Manish Dewangan07de2142017-02-27 19:27:20 +053041#include <sound/compress_params.h>
42#include <sound/compress_offload.h>
Surendar Karka287348c2019-04-10 18:31:46 +053043#include <sound/devdep_params.h>
Manish Dewangan07de2142017-02-27 19:27:20 +053044#include <tinycompress/tinycompress.h>
Revathi Uddaraju1eac8b02017-05-18 17:13:33 +053045
46#ifdef DYNAMIC_LOG_ENABLED
47#include <log_xml_parser.h>
48#define LOG_MASK HAL_MOD_FILE_UTILS
49#include <log_utils.h>
50#endif
51
Ashish Jain81eb2a82015-05-13 10:52:34 +053052#ifdef AUDIO_EXTERNAL_HDMI_ENABLED
Ashish Jain81eb2a82015-05-13 10:52:34 +053053#include "audio_parsers.h"
54#endif
Ashish Jain81eb2a82015-05-13 10:52:34 +053055
Yamit Mehtaa0d653a2016-11-25 20:33:25 +053056#ifdef LINUX_ENABLED
57#define AUDIO_OUTPUT_POLICY_VENDOR_CONFIG_FILE "/etc/audio_output_policy.conf"
Dhananjay Kumard6d32152016-10-13 16:11:03 +053058#define AUDIO_IO_POLICY_VENDOR_CONFIG_FILE "/etc/audio_io_policy.conf"
Yamit Mehtaa0d653a2016-11-25 20:33:25 +053059#else
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070060#define AUDIO_OUTPUT_POLICY_VENDOR_CONFIG_FILE "/vendor/etc/audio_output_policy.conf"
Dhananjay Kumard6d32152016-10-13 16:11:03 +053061#define AUDIO_IO_POLICY_VENDOR_CONFIG_FILE "/vendor/etc/audio_io_policy.conf"
Yamit Mehtaa0d653a2016-11-25 20:33:25 +053062#endif
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070063
64#define OUTPUTS_TAG "outputs"
Dhananjay Kumard6d32152016-10-13 16:11:03 +053065#define INPUTS_TAG "inputs"
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070066
67#define DYNAMIC_VALUE_TAG "dynamic"
68#define FLAGS_TAG "flags"
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +053069#define PROFILES_TAG "profile"
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070070#define FORMATS_TAG "formats"
71#define SAMPLING_RATES_TAG "sampling_rates"
72#define BIT_WIDTH_TAG "bit_width"
73#define APP_TYPE_TAG "app_type"
74
75#define STRING_TO_ENUM(string) { #string, string }
76#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
77
Ben Rombergera04fabc2014-11-14 12:16:03 -080078#define BASE_TABLE_SIZE 64
79#define MAX_BASEINDEX_LEN 256
80
Ben Romberger1aaaf862017-04-06 17:49:46 -070081#ifndef SND_AUDIOCODEC_TRUEHD
82#define SND_AUDIOCODEC_TRUEHD 0x00000023
83#endif
84
Vikram Panduranga93f080e2017-06-07 18:16:14 -070085#define APP_TYPE_VOIP_AUDIO 0x1113A
86
Ashish Jain81eb2a82015-05-13 10:52:34 +053087#ifdef AUDIO_EXTERNAL_HDMI_ENABLED
88#define PROFESSIONAL (1<<0) /* 0 = consumer, 1 = professional */
89#define NON_LPCM (1<<1) /* 0 = audio, 1 = non-audio */
90#define SR_44100 (0<<0) /* 44.1kHz */
91#define SR_NOTID (1<<0) /* non indicated */
92#define SR_48000 (2<<0) /* 48kHz */
93#define SR_32000 (3<<0) /* 32kHz */
94#define SR_22050 (4<<0) /* 22.05kHz */
95#define SR_24000 (6<<0) /* 24kHz */
96#define SR_88200 (8<<0) /* 88.2kHz */
97#define SR_96000 (10<<0) /* 96kHz */
98#define SR_176400 (12<<0) /* 176.4kHz */
99#define SR_192000 (14<<0) /* 192kHz */
100
101#endif
Manish Dewangan07de2142017-02-27 19:27:20 +0530102
103/* ToDo: Check and update a proper value in msec */
Weiyin Jiangc49a3b52018-08-17 16:16:08 +0800104#define COMPRESS_OFFLOAD_PLAYBACK_LATENCY 50
105#define PCM_OFFLOAD_PLAYBACK_DSP_PATHDELAY 62
106#define PCM_OFFLOAD_PLAYBACK_LATENCY \
107 (PCM_OFFLOAD_BUFFER_DURATION + PCM_OFFLOAD_PLAYBACK_DSP_PATHDELAY)
Manish Dewangan07de2142017-02-27 19:27:20 +0530108
Varun Balaraje49253e2017-07-06 19:48:56 +0530109#ifndef MAX_CHANNELS_SUPPORTED
110#define MAX_CHANNELS_SUPPORTED 8
111#endif
112
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -0700113#ifdef __LP64__
114#define VNDK_FWK_LIB_PATH "/vendor/lib64/libqti_vndfwk_detect.so"
115#else
116#define VNDK_FWK_LIB_PATH "/vendor/lib/libqti_vndfwk_detect.so"
117#endif
118
119static void *vndk_fwk_lib_handle = NULL;
120typedef int (*vndk_fwk_isVendorEnhancedFwk_t)();
121static vndk_fwk_isVendorEnhancedFwk_t vndk_fwk_isVendorEnhancedFwk;
122
`Deeraj Soman676c2702017-09-18 19:25:53 +0530123typedef struct {
124 const char *id_string;
125 const int value;
126} mixer_config_lookup;
127
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700128struct string_to_enum {
129 const char *name;
130 uint32_t value;
131};
132
133const struct string_to_enum s_flag_name_to_enum_table[] = {
134 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_DIRECT),
135 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_PRIMARY),
136 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_FAST),
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800137 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_RAW),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700138 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_DEEP_BUFFER),
139 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD),
140 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_NON_BLOCKING),
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700141 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_HW_AV_SYNC),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700142 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_INCALL_MUSIC),
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700143 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH),
Naresh Tanniruee3499a2017-01-05 14:05:35 +0530144 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_TIMESTAMP),
Vikram Panduranga93f080e2017-06-07 18:16:14 -0700145 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_VOIP_RX),
Ben Romberger6c4d3812017-06-13 17:46:45 -0700146 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_BD),
Varun Balaraje49253e2017-07-06 19:48:56 +0530147 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_INTERACTIVE),
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530148 STRING_TO_ENUM(AUDIO_INPUT_FLAG_NONE),
149 STRING_TO_ENUM(AUDIO_INPUT_FLAG_FAST),
150 STRING_TO_ENUM(AUDIO_INPUT_FLAG_HW_HOTWORD),
151 STRING_TO_ENUM(AUDIO_INPUT_FLAG_RAW),
152 STRING_TO_ENUM(AUDIO_INPUT_FLAG_SYNC),
Dhananjay Kumaree4d2002016-10-25 18:02:58 +0530153 STRING_TO_ENUM(AUDIO_INPUT_FLAG_TIMESTAMP),
Dhananjay Kumar704ce6f2017-09-28 22:08:00 +0530154 STRING_TO_ENUM(AUDIO_INPUT_FLAG_COMPRESS),
Ralf Herzaec80262018-07-03 07:08:49 +0200155 STRING_TO_ENUM(AUDIO_INPUT_FLAG_PASSTHROUGH),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700156};
157
158const struct string_to_enum s_format_name_to_enum_table[] = {
Ashish Jain83a6cc22016-06-28 14:34:17 +0530159 STRING_TO_ENUM(AUDIO_FORMAT_PCM_8_BIT),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700160 STRING_TO_ENUM(AUDIO_FORMAT_PCM_16_BIT),
Ashish Jain5106d362016-05-11 19:23:33 +0530161 STRING_TO_ENUM(AUDIO_FORMAT_PCM_24_BIT_PACKED),
162 STRING_TO_ENUM(AUDIO_FORMAT_PCM_8_24_BIT),
Ashish Jainf1eaa582016-05-23 20:54:24 +0530163 STRING_TO_ENUM(AUDIO_FORMAT_PCM_32_BIT),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700164 STRING_TO_ENUM(AUDIO_FORMAT_MP3),
165 STRING_TO_ENUM(AUDIO_FORMAT_AAC),
166 STRING_TO_ENUM(AUDIO_FORMAT_VORBIS),
Mingming Yinae3530f2014-07-03 16:50:18 -0700167 STRING_TO_ENUM(AUDIO_FORMAT_AMR_NB),
168 STRING_TO_ENUM(AUDIO_FORMAT_AMR_WB),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700169 STRING_TO_ENUM(AUDIO_FORMAT_AC3),
Mingming Yinae3530f2014-07-03 16:50:18 -0700170 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700171 STRING_TO_ENUM(AUDIO_FORMAT_DTS),
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530172 STRING_TO_ENUM(AUDIO_FORMAT_DTS_HD),
Ben Romberger1aaaf862017-04-06 17:49:46 -0700173 STRING_TO_ENUM(AUDIO_FORMAT_DOLBY_TRUEHD),
Naresh Tanniru928f0862017-04-07 16:44:23 -0700174 STRING_TO_ENUM(AUDIO_FORMAT_IEC61937),
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530175 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3_JOC),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700176 STRING_TO_ENUM(AUDIO_FORMAT_WMA),
177 STRING_TO_ENUM(AUDIO_FORMAT_WMA_PRO),
178 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADIF),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700179 STRING_TO_ENUM(AUDIO_FORMAT_AMR_WB_PLUS),
180 STRING_TO_ENUM(AUDIO_FORMAT_EVRC),
181 STRING_TO_ENUM(AUDIO_FORMAT_EVRCB),
182 STRING_TO_ENUM(AUDIO_FORMAT_EVRCWB),
183 STRING_TO_ENUM(AUDIO_FORMAT_QCELP),
184 STRING_TO_ENUM(AUDIO_FORMAT_MP2),
185 STRING_TO_ENUM(AUDIO_FORMAT_EVRCNW),
Amit Shekhar6f461b12014-08-01 14:52:58 -0700186 STRING_TO_ENUM(AUDIO_FORMAT_FLAC),
Satya Krishna Pindiproli70471602015-04-24 19:12:43 +0530187 STRING_TO_ENUM(AUDIO_FORMAT_ALAC),
188 STRING_TO_ENUM(AUDIO_FORMAT_APE),
Alexy Josephcd8eaed2014-12-11 12:46:53 -0800189 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LC),
190 STRING_TO_ENUM(AUDIO_FORMAT_AAC_HE_V1),
191 STRING_TO_ENUM(AUDIO_FORMAT_AAC_HE_V2),
Manish Dewangana6fc5442015-08-24 20:30:31 +0530192 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS),
Ashish Jaine513a872015-11-19 17:00:56 +0530193 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_LC),
194 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_HE_V1),
195 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_HE_V2),
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +0530196 STRING_TO_ENUM(AUDIO_FORMAT_DSD),
Arun Kumar Dasari3b174182016-12-27 13:01:14 +0530197 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM),
198 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM_LC),
199 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM_HE_V1),
200 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM_HE_V2),
Dhanalakshmi Siddani18737932016-11-29 17:33:17 +0530201 STRING_TO_ENUM(AUDIO_FORMAT_APTX),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700202};
203
Manish Dewangan3ccdea52017-02-13 19:31:54 +0530204/* payload structure avt_device drift query */
205struct audio_avt_device_drift_stats {
206 uint32_t minor_version;
Manish Dewangan338c50a2017-09-12 15:22:03 +0530207
Manish Dewangan3ccdea52017-02-13 19:31:54 +0530208 /* Indicates the device interface direction as either
209 * source (Tx) or sink (Rx).
210 */
211 uint16_t device_direction;
Manish Dewangan338c50a2017-09-12 15:22:03 +0530212
213 /* Reference timer for drift accumulation and time stamp information.
214 * currently it only support AFE_REF_TIMER_TYPE_AVTIMER
215 */
216 uint16_t reference_timer;
Manish Dewangan3ccdea52017-02-13 19:31:54 +0530217 struct audio_avt_device_drift_param drift_param;
Manish Dewangan338c50a2017-09-12 15:22:03 +0530218} __attribute__((packed));
Manish Dewangan3ccdea52017-02-13 19:31:54 +0530219
Ben Rombergera04fabc2014-11-14 12:16:03 -0800220static char bTable[BASE_TABLE_SIZE] = {
221 'A','B','C','D','E','F','G','H','I','J','K','L',
222 'M','N','O','P','Q','R','S','T','U','V','W','X',
223 'Y','Z','a','b','c','d','e','f','g','h','i','j',
224 'k','l','m','n','o','p','q','r','s','t','u','v',
225 'w','x','y','z','0','1','2','3','4','5','6','7',
226 '8','9','+','/'
227};
228
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700229static uint32_t string_to_enum(const struct string_to_enum *table, size_t size,
230 const char *name)
231{
232 size_t i;
233 for (i = 0; i < size; i++) {
234 if (strcmp(table[i].name, name) == 0) {
235 ALOGV("%s found %s", __func__, table[i].name);
236 return table[i].value;
237 }
238 }
239 return 0;
240}
241
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530242static audio_io_flags_t parse_flag_names(char *name)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700243{
244 uint32_t flag = 0;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530245 audio_io_flags_t io_flags;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800246 char *last_r;
247 char *flag_name = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700248 while (flag_name != NULL) {
249 if (strlen(flag_name) != 0) {
250 flag |= string_to_enum(s_flag_name_to_enum_table,
251 ARRAY_SIZE(s_flag_name_to_enum_table),
252 flag_name);
253 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800254 flag_name = strtok_r(NULL, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700255 }
256
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800257 ALOGV("parse_flag_names: flag - %x", flag);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530258 io_flags.in_flags = (audio_input_flags_t)flag;
259 io_flags.out_flags = (audio_output_flags_t)flag;
260 return io_flags;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700261}
262
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530263static void parse_format_names(char *name, struct streams_io_cfg *s_info)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700264{
265 struct stream_format *sf_info = NULL;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800266 char *last_r;
267 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700268
269 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG) == 0)
270 return;
271
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530272 list_init(&s_info->format_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700273 while (str != NULL) {
274 audio_format_t format = (audio_format_t)string_to_enum(s_format_name_to_enum_table,
275 ARRAY_SIZE(s_format_name_to_enum_table), str);
276 ALOGV("%s: format - %d", __func__, format);
277 if (format != 0) {
278 sf_info = (struct stream_format *)calloc(1, sizeof(struct stream_format));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700279 if (sf_info == NULL)
280 break; /* return whatever was parsed */
281
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700282 sf_info->format = format;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530283 list_add_tail(&s_info->format_list, &sf_info->list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700284 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800285 str = strtok_r(NULL, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700286 }
287}
288
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530289static void parse_sample_rate_names(char *name, struct streams_io_cfg *s_info)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700290{
Amit Shekhar6f461b12014-08-01 14:52:58 -0700291 struct stream_sample_rate *ss_info = NULL;
292 uint32_t sample_rate = 48000;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800293 char *last_r;
294 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700295
Amit Shekhar6f461b12014-08-01 14:52:58 -0700296 if (str != NULL && 0 == strcmp(str, DYNAMIC_VALUE_TAG))
297 return;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700298
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530299 list_init(&s_info->sample_rate_list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700300 while (str != NULL) {
301 sample_rate = (uint32_t)strtol(str, (char **)NULL, 10);
302 ALOGV("%s: sample_rate - %d", __func__, sample_rate);
303 if (0 != sample_rate) {
304 ss_info = (struct stream_sample_rate *)calloc(1, sizeof(struct stream_sample_rate));
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800305 if (!ss_info) {
306 ALOGE("%s: memory allocation failure", __func__);
307 return;
308 }
Amit Shekhar6f461b12014-08-01 14:52:58 -0700309 ss_info->sample_rate = sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530310 list_add_tail(&s_info->sample_rate_list, &ss_info->list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700311 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800312 str = strtok_r(NULL, "|", &last_r);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700313 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700314}
315
316static int parse_bit_width_names(char *name)
317{
318 int bit_width = 16;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800319 char *last_r;
320 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700321
322 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG))
323 bit_width = (int)strtol(str, (char **)NULL, 10);
324
325 ALOGV("%s: bit_width - %d", __func__, bit_width);
326 return bit_width;
327}
328
329static int parse_app_type_names(void *platform, char *name)
330{
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700331 int app_type = platform_get_default_app_type(platform);
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800332 char *last_r;
333 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700334
335 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG))
336 app_type = (int)strtol(str, (char **)NULL, 10);
337
338 ALOGV("%s: app_type - %d", __func__, app_type);
339 return app_type;
340}
341
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530342static void update_streams_cfg_list(cnode *root, void *platform,
343 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700344{
345 cnode *node = root->first_child;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530346 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700347
348 ALOGV("%s", __func__);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530349 s_info = (struct streams_io_cfg *)calloc(1, sizeof(struct streams_io_cfg));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700350
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530351 if (!s_info) {
352 ALOGE("failed to allocate mem for s_info list element");
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700353 return;
354 }
355
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700356 while (node) {
357 if (strcmp(node->name, FLAGS_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530358 s_info->flags = parse_flag_names((char *)node->value);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530359 } else if (strcmp(node->name, PROFILES_TAG) == 0) {
360 strlcpy(s_info->profile, (char *)node->value, sizeof(s_info->profile));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700361 } else if (strcmp(node->name, FORMATS_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530362 parse_format_names((char *)node->value, s_info);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700363 } else if (strcmp(node->name, SAMPLING_RATES_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530364 s_info->app_type_cfg.sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
365 parse_sample_rate_names((char *)node->value, s_info);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700366 } else if (strcmp(node->name, BIT_WIDTH_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530367 s_info->app_type_cfg.bit_width = parse_bit_width_names((char *)node->value);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700368 } else if (strcmp(node->name, APP_TYPE_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530369 s_info->app_type_cfg.app_type = parse_app_type_names(platform, (char *)node->value);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700370 }
371 node = node->next;
372 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530373 list_add_tail(streams_cfg_list, &s_info->list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700374}
375
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530376static void load_cfg_list(cnode *root, void *platform,
377 struct listnode *streams_output_cfg_list,
378 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700379{
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530380 cnode *node = NULL;
381
382 node = config_find(root, OUTPUTS_TAG);
383 if (node != NULL) {
384 node = node->first_child;
385 while (node) {
386 ALOGV("%s: loading output %s", __func__, node->name);
387 update_streams_cfg_list(node, platform, streams_output_cfg_list);
388 node = node->next;
389 }
390 } else {
391 ALOGI("%s: could not load output, node is NULL", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700392 }
393
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530394 node = config_find(root, INPUTS_TAG);
395 if (node != NULL) {
396 node = node->first_child;
397 while (node) {
398 ALOGV("%s: loading input %s", __func__, node->name);
399 update_streams_cfg_list(node, platform, streams_input_cfg_list);
400 node = node->next;
401 }
402 } else {
403 ALOGI("%s: could not load input, node is NULL", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700404 }
405}
406
407static void send_app_type_cfg(void *platform, struct mixer *mixer,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530408 struct listnode *streams_output_cfg_list,
409 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700410{
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530411 size_t app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT] = {0};
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700412 int length = 0, i, num_app_types = 0;
413 struct listnode *node;
414 bool update;
415 struct mixer_ctl *ctl = NULL;
416 const char *mixer_ctl_name = "App Type Config";
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530417 struct streams_io_cfg *s_info = NULL;
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800418 uint32_t target_bit_width = 0;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700419
420 if (!mixer) {
421 ALOGE("%s: mixer is null",__func__);
422 return;
423 }
424 ctl = mixer_get_ctl_by_name(mixer, mixer_ctl_name);
425 if (!ctl) {
426 ALOGE("%s: Could not get ctl for mixer cmd - %s",__func__, mixer_ctl_name);
427 return;
428 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530429 app_type_cfg[length++] = num_app_types;
430
431 if (list_empty(streams_output_cfg_list)) {
432 app_type_cfg[length++] = platform_get_default_app_type_v2(platform, PCM_PLAYBACK);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700433 app_type_cfg[length++] = 48000;
434 app_type_cfg[length++] = 16;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530435 num_app_types += 1;
436 }
437 if (list_empty(streams_input_cfg_list)) {
438 app_type_cfg[length++] = platform_get_default_app_type_v2(platform, PCM_CAPTURE);
439 app_type_cfg[length++] = 48000;
440 app_type_cfg[length++] = 16;
441 num_app_types += 1;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700442 }
443
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800444 /* get target bit width for ADM enforce mode */
445 target_bit_width = adev_get_dsp_bit_width_enforce_mode();
446
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700447 list_for_each(node, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530448 s_info = node_to_item(node, struct streams_io_cfg, list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700449 update = true;
450 for (i=0; i<length; i=i+3) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530451 if (app_type_cfg[i+1] == 0)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700452 break;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530453 else if (app_type_cfg[i+1] == (size_t)s_info->app_type_cfg.app_type) {
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530454 if (app_type_cfg[i+2] < (size_t)s_info->app_type_cfg.sample_rate)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530455 app_type_cfg[i+2] = s_info->app_type_cfg.sample_rate;
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530456 if (app_type_cfg[i+3] < (size_t)s_info->app_type_cfg.bit_width)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530457 app_type_cfg[i+3] = s_info->app_type_cfg.bit_width;
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800458 /* ADM bit width = max(enforce_bit_width, bit_width from s_info */
459 if (audio_extn_is_dsp_bit_width_enforce_mode_supported(s_info->flags.out_flags) &&
460 (target_bit_width > app_type_cfg[i+3]))
461 app_type_cfg[i+3] = target_bit_width;
462
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700463 update = false;
464 break;
465 }
466 }
467 if (update && ((length + 3) <= MAX_LENGTH_MIXER_CONTROL_IN_INT)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530468 num_app_types += 1;
469 app_type_cfg[length++] = s_info->app_type_cfg.app_type;
470 app_type_cfg[length++] = s_info->app_type_cfg.sample_rate;
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800471 app_type_cfg[length] = s_info->app_type_cfg.bit_width;
472 if (audio_extn_is_dsp_bit_width_enforce_mode_supported(s_info->flags.out_flags) &&
473 (target_bit_width > app_type_cfg[length]))
474 app_type_cfg[length] = target_bit_width;
475
476 length++;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530477 }
478 }
479 list_for_each(node, streams_input_cfg_list) {
480 s_info = node_to_item(node, struct streams_io_cfg, list);
481 update = true;
482 for (i=0; i<length; i=i+3) {
483 if (app_type_cfg[i+1] == 0)
484 break;
485 else if (app_type_cfg[i+1] == (size_t)s_info->app_type_cfg.app_type) {
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530486 if (app_type_cfg[i+2] < (size_t)s_info->app_type_cfg.sample_rate)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530487 app_type_cfg[i+2] = s_info->app_type_cfg.sample_rate;
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530488 if (app_type_cfg[i+3] < (size_t)s_info->app_type_cfg.bit_width)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530489 app_type_cfg[i+3] = s_info->app_type_cfg.bit_width;
490 update = false;
491 break;
492 }
493 }
494 if (update && ((length + 3) <= MAX_LENGTH_MIXER_CONTROL_IN_INT)) {
495 num_app_types += 1;
496 app_type_cfg[length++] = s_info->app_type_cfg.app_type;
497 app_type_cfg[length++] = s_info->app_type_cfg.sample_rate;
498 app_type_cfg[length++] = s_info->app_type_cfg.bit_width;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700499 }
500 }
501 ALOGV("%s: num_app_types: %d", __func__, num_app_types);
502 if (num_app_types) {
503 app_type_cfg[0] = num_app_types;
504 mixer_ctl_set_array(ctl, app_type_cfg, length);
505 }
506}
507
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530508void audio_extn_utils_update_streams_cfg_lists(void *platform,
509 struct mixer *mixer,
510 struct listnode *streams_output_cfg_list,
511 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700512{
513 cnode *root;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530514 char *data = NULL;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700515
516 ALOGV("%s", __func__);
517 list_init(streams_output_cfg_list);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530518 list_init(streams_input_cfg_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700519
520 root = config_node("", "");
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700521 if (root == NULL) {
522 ALOGE("cfg_list, NULL config root");
523 return;
524 }
525
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530526 data = (char *)load_file(AUDIO_IO_POLICY_VENDOR_CONFIG_FILE, NULL);
527 if (data == NULL) {
528 ALOGD("%s: failed to open io config file(%s), trying older config file",
529 __func__, AUDIO_IO_POLICY_VENDOR_CONFIG_FILE);
530 data = (char *)load_file(AUDIO_OUTPUT_POLICY_VENDOR_CONFIG_FILE, NULL);
531 if (data == NULL) {
532 send_app_type_cfg(platform, mixer,
533 streams_output_cfg_list,
534 streams_input_cfg_list);
535 ALOGE("%s: could not load io policy config!", __func__);
yidongh6eb4f752017-04-19 18:20:57 +0800536 free(root);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530537 return;
538 }
539 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700540
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530541 config_load(root, data);
542 load_cfg_list(root, platform, streams_output_cfg_list,
543 streams_input_cfg_list);
544
545 send_app_type_cfg(platform, mixer, streams_output_cfg_list,
546 streams_input_cfg_list);
Alexy Josephaee4fdd2016-01-29 13:02:07 -0800547
548 config_free(root);
yidongh6eb4f752017-04-19 18:20:57 +0800549 free(root);
Alexy Josephaee4fdd2016-01-29 13:02:07 -0800550 free(data);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700551}
552
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530553static void audio_extn_utils_dump_streams_cfg_list(
554 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700555{
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700556 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530557 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700558 struct stream_format *sf_info;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700559 struct stream_sample_rate *ss_info;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530560
561 list_for_each(node_i, streams_cfg_list) {
562 s_info = node_to_item(node_i, struct streams_io_cfg, list);
563 ALOGV("%s: flags-%d, sample_rate-%d, bit_width-%d, app_type-%d",
564 __func__, s_info->flags.out_flags, s_info->app_type_cfg.sample_rate,
565 s_info->app_type_cfg.bit_width, s_info->app_type_cfg.app_type);
566 list_for_each(node_j, &s_info->format_list) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700567 sf_info = node_to_item(node_j, struct stream_format, list);
568 ALOGV("format-%x", sf_info->format);
569 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530570 list_for_each(node_j, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700571 ss_info = node_to_item(node_j, struct stream_sample_rate, list);
572 ALOGV("sample rate-%d", ss_info->sample_rate);
573 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700574 }
575}
576
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530577void audio_extn_utils_dump_streams_cfg_lists(
578 struct listnode *streams_output_cfg_list,
579 struct listnode *streams_input_cfg_list)
580{
581 ALOGV("%s", __func__);
582 audio_extn_utils_dump_streams_cfg_list(streams_output_cfg_list);
583 audio_extn_utils_dump_streams_cfg_list(streams_input_cfg_list);
584}
585
586static void audio_extn_utils_release_streams_cfg_list(
587 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700588{
589 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530590 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700591
592 ALOGV("%s", __func__);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530593
594 while (!list_empty(streams_cfg_list)) {
595 node_i = list_head(streams_cfg_list);
596 s_info = node_to_item(node_i, struct streams_io_cfg, list);
597 while (!list_empty(&s_info->format_list)) {
598 node_j = list_head(&s_info->format_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700599 list_remove(node_j);
600 free(node_to_item(node_j, struct stream_format, list));
601 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530602 while (!list_empty(&s_info->sample_rate_list)) {
603 node_j = list_head(&s_info->sample_rate_list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700604 list_remove(node_j);
605 free(node_to_item(node_j, struct stream_sample_rate, list));
606 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700607 list_remove(node_i);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530608 free(node_to_item(node_i, struct streams_io_cfg, list));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700609 }
610}
611
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530612void audio_extn_utils_release_streams_cfg_lists(
613 struct listnode *streams_output_cfg_list,
614 struct listnode *streams_input_cfg_list)
615{
616 ALOGV("%s", __func__);
617 audio_extn_utils_release_streams_cfg_list(streams_output_cfg_list);
618 audio_extn_utils_release_streams_cfg_list(streams_input_cfg_list);
619}
620
621static bool set_app_type_cfg(struct streams_io_cfg *s_info,
622 struct stream_app_type_cfg *app_type_cfg,
623 uint32_t sample_rate, uint32_t bit_width)
Amit Shekhar6f461b12014-08-01 14:52:58 -0700624 {
625 struct listnode *node_i;
626 struct stream_sample_rate *ss_info;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530627 list_for_each(node_i, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700628 ss_info = node_to_item(node_i, struct stream_sample_rate, list);
629 if ((sample_rate <= ss_info->sample_rate) &&
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530630 (bit_width == s_info->app_type_cfg.bit_width)) {
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700631
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530632 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700633 app_type_cfg->sample_rate = ss_info->sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530634 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700635 ALOGV("%s app_type_cfg->app_type %d, app_type_cfg->sample_rate %d, app_type_cfg->bit_width %d",
636 __func__, app_type_cfg->app_type, app_type_cfg->sample_rate, app_type_cfg->bit_width);
637 return true;
638 }
639 }
640 /*
641 * Reiterate through the list assuming dafault sample rate.
642 * Handles scenario where input sample rate is higher
643 * than all sample rates in list for the input bit width.
644 */
645 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
Apoorv Raghuvanshif59bb222015-02-18 12:23:23 -0800646
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530647 list_for_each(node_i, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700648 ss_info = node_to_item(node_i, struct stream_sample_rate, list);
649 if ((sample_rate <= ss_info->sample_rate) &&
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530650 (bit_width == s_info->app_type_cfg.bit_width)) {
651 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700652 app_type_cfg->sample_rate = sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530653 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Apoorv Raghuvanshif59bb222015-02-18 12:23:23 -0800654 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 -0700655 __func__, app_type_cfg->app_type, app_type_cfg->sample_rate, app_type_cfg->bit_width);
656 return true;
657 }
658 }
659 return false;
660}
661
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530662void audio_extn_utils_update_stream_input_app_type_cfg(void *platform,
663 struct listnode *streams_input_cfg_list,
664 audio_devices_t devices __unused,
665 audio_input_flags_t flags,
666 audio_format_t format,
667 uint32_t sample_rate,
668 uint32_t bit_width,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530669 char* profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530670 struct stream_app_type_cfg *app_type_cfg)
671{
672 struct listnode *node_i, *node_j;
673 struct streams_io_cfg *s_info;
674 struct stream_format *sf_info;
675
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530676 ALOGV("%s: flags: 0x%x, format: 0x%x sample_rate %d, profile %s",
677 __func__, flags, format, sample_rate, profile);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530678
679 list_for_each(node_i, streams_input_cfg_list) {
680 s_info = node_to_item(node_i, struct streams_io_cfg, list);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530681 /* Along with flags do profile matching if set at either end.*/
682 if (s_info->flags.in_flags == flags &&
683 ((profile[0] == '\0' && s_info->profile[0] == '\0') ||
684 strncmp(s_info->profile, profile, sizeof(s_info->profile)) == 0)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530685 list_for_each(node_j, &s_info->format_list) {
686 sf_info = node_to_item(node_j, struct stream_format, list);
687 if (sf_info->format == format) {
688 if (set_app_type_cfg(s_info, app_type_cfg, sample_rate, bit_width))
689 return;
690 }
691 }
692 }
693 }
694 ALOGW("%s: App type could not be selected. Falling back to default", __func__);
695 app_type_cfg->app_type = platform_get_default_app_type_v2(platform, PCM_CAPTURE);
696 app_type_cfg->sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
697 app_type_cfg->bit_width = 16;
698}
699
700void audio_extn_utils_update_stream_output_app_type_cfg(void *platform,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700701 struct listnode *streams_output_cfg_list,
Amit Shekhar1d896042014-10-03 13:16:09 -0700702 audio_devices_t devices,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700703 audio_output_flags_t flags,
704 audio_format_t format,
Amit Shekhar6f461b12014-08-01 14:52:58 -0700705 uint32_t sample_rate,
706 uint32_t bit_width,
Manish Dewangan837dc462015-05-27 10:17:41 +0530707 audio_channel_mask_t channel_mask,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530708 char *profile,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700709 struct stream_app_type_cfg *app_type_cfg)
710{
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +0530711 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530712 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700713 struct stream_format *sf_info;
Manish Dewangan837dc462015-05-27 10:17:41 +0530714 char value[PROPERTY_VALUE_MAX] = {0};
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700715
Ramu Gottipati074fa4d2018-09-11 20:05:51 +0530716 if (devices & AUDIO_DEVICE_OUT_SPEAKER) {
717 int bw = platform_get_snd_device_bit_width(SND_DEVICE_OUT_SPEAKER);
718 if ((-ENOSYS != bw) && (bit_width > (uint32_t)bw))
Amit Shekhar5a39c912014-10-14 15:39:30 -0700719 bit_width = (uint32_t)bw;
Ramu Gottipati074fa4d2018-09-11 20:05:51 +0530720 else if (-ENOSYS == bw)
721 bit_width = CODEC_BACKEND_DEFAULT_BIT_WIDTH;
Amit Shekhar1d896042014-10-03 13:16:09 -0700722 sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Ramu Gottipati074fa4d2018-09-11 20:05:51 +0530723 ALOGI("%s Allowing 24 and above bits playback on speaker ONLY at default sampling rate", __func__);
Amit Shekhar1d896042014-10-03 13:16:09 -0700724 }
725
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -0700726 property_get("vendor.audio.playback.mch.downsample",value,"");
Manish Dewangan837dc462015-05-27 10:17:41 +0530727 if (!strncmp("true", value, sizeof("true"))) {
728 if ((popcount(channel_mask) > 2) &&
729 (sample_rate > CODEC_BACKEND_DEFAULT_SAMPLE_RATE) &&
Aalique Grahame5cd12ff2017-10-19 10:57:00 -0700730 !(flags & (audio_output_flags_t)AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH)) {
Manish Dewangan837dc462015-05-27 10:17:41 +0530731 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
732 ALOGD("%s: MCH session defaulting sample rate to %d",
733 __func__, sample_rate);
734 }
735 }
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +0530736
737 /* Set sampling rate to 176.4 for DSD64
738 * and 352.8Khz for DSD128.
739 * Set Bit Width to 16. output will be 16 bit
740 * post DoP in ASM.
741 */
Aalique Grahame5cd12ff2017-10-19 10:57:00 -0700742 if ((flags & (audio_output_flags_t)AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH) &&
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +0530743 (format == AUDIO_FORMAT_DSD)) {
744 bit_width = 16;
745 if (sample_rate == INPUT_SAMPLING_RATE_DSD64)
746 sample_rate = OUTPUT_SAMPLING_RATE_DSD64;
747 else if (sample_rate == INPUT_SAMPLING_RATE_DSD128)
748 sample_rate = OUTPUT_SAMPLING_RATE_DSD128;
749 }
750
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +0530751
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800752 ALOGV("%s: flags: %x, format: %x sample_rate %d, profile %s, app_type %d",
753 __func__, flags, format, sample_rate, profile, app_type_cfg->app_type);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700754 list_for_each(node_i, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530755 s_info = node_to_item(node_i, struct streams_io_cfg, list);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530756 /* Along with flags do profile matching if set at either end.*/
757 if (s_info->flags.out_flags == flags &&
758 ((profile[0] == '\0' && s_info->profile[0] == '\0') ||
759 strncmp(s_info->profile, profile, sizeof(s_info->profile)) == 0)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530760 list_for_each(node_j, &s_info->format_list) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700761 sf_info = node_to_item(node_j, struct stream_format, list);
762 if (sf_info->format == format) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530763 if (set_app_type_cfg(s_info, app_type_cfg, sample_rate, bit_width))
Amit Shekhar6f461b12014-08-01 14:52:58 -0700764 return;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700765 }
766 }
767 }
768 }
769 list_for_each(node_i, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530770 s_info = node_to_item(node_i, struct streams_io_cfg, list);
771 if (s_info->flags.out_flags == AUDIO_OUTPUT_FLAG_PRIMARY) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700772 ALOGV("Compatible output profile not found.");
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530773 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
774 app_type_cfg->sample_rate = s_info->app_type_cfg.sample_rate;
775 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700776 ALOGV("%s Default to primary output: App type: %d sample_rate %d",
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530777 __func__, s_info->app_type_cfg.app_type, app_type_cfg->sample_rate);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700778 return;
779 }
780 }
781 ALOGW("%s: App type could not be selected. Falling back to default", __func__);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700782 app_type_cfg->app_type = platform_get_default_app_type(platform);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700783 app_type_cfg->sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700784 app_type_cfg->bit_width = 16;
785}
786
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +0530787static bool audio_is_this_native_usecase(struct audio_usecase *uc)
788{
789 bool native_usecase = false;
790 struct stream_out *out = (struct stream_out*) uc->stream.out;
791
792 if (PCM_PLAYBACK == uc->type && out != NULL &&
793 NATIVE_AUDIO_MODE_INVALID != platform_get_native_support() &&
794 is_offload_usecase(uc->id) &&
795 (out->sample_rate == OUTPUT_SAMPLING_RATE_44100))
796 native_usecase = true;
797
798 return native_usecase;
799}
800
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800801bool audio_extn_is_dsp_bit_width_enforce_mode_supported(audio_output_flags_t flags)
802{
803 /* DSP bitwidth enforce mode for ADM and AFE:
804 * includes:
805 * deep buffer, low latency, direct pcm and offload.
806 * excludes:
807 * ull(raw+fast), VOIP.
808 */
809 if ((flags & AUDIO_OUTPUT_FLAG_VOIP_RX) ||
810 ((flags & AUDIO_OUTPUT_FLAG_RAW) &&
811 (flags & AUDIO_OUTPUT_FLAG_FAST)))
812 return false;
813
814
815 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) ||
816 (flags & AUDIO_OUTPUT_FLAG_DIRECT) ||
817 (flags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) ||
818 (flags & AUDIO_OUTPUT_FLAG_PRIMARY))
819 return true;
820 else
821 return false;
822}
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800823
824static inline bool audio_is_vr_mode_on(struct audio_device *(__attribute__((unused)) adev))
825{
826 return adev->vr_audio_mode_enabled;
827}
828
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530829void audio_extn_utils_update_stream_app_type_cfg_for_usecase(
830 struct audio_device *adev,
831 struct audio_usecase *usecase)
832{
833 ALOGV("%s", __func__);
834
835 switch(usecase->type) {
836 case PCM_PLAYBACK:
837 audio_extn_utils_update_stream_output_app_type_cfg(adev->platform,
838 &adev->streams_output_cfg_list,
839 usecase->stream.out->devices,
840 usecase->stream.out->flags,
Aalique Grahame65780b52017-09-27 14:59:56 -0700841 usecase->stream.out->hal_op_format,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530842 usecase->stream.out->sample_rate,
843 usecase->stream.out->bit_width,
844 usecase->stream.out->channel_mask,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530845 usecase->stream.out->profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530846 &usecase->stream.out->app_type_cfg);
847 ALOGV("%s Selected apptype: %d", __func__, usecase->stream.out->app_type_cfg.app_type);
848 break;
849 case PCM_CAPTURE:
Vikram Panduranga93f080e2017-06-07 18:16:14 -0700850 if (usecase->id == USECASE_AUDIO_RECORD_VOIP)
851 usecase->stream.in->app_type_cfg.app_type = APP_TYPE_VOIP_AUDIO;
852 else
853 audio_extn_utils_update_stream_input_app_type_cfg(adev->platform,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530854 &adev->streams_input_cfg_list,
855 usecase->stream.in->device,
856 usecase->stream.in->flags,
857 usecase->stream.in->format,
858 usecase->stream.in->sample_rate,
859 usecase->stream.in->bit_width,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530860 usecase->stream.in->profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530861 &usecase->stream.in->app_type_cfg);
862 ALOGV("%s Selected apptype: %d", __func__, usecase->stream.in->app_type_cfg.app_type);
863 break;
Surendar Karka93cd25a2018-08-28 14:21:37 +0530864 case TRANSCODE_LOOPBACK_RX :
Siddartha Shaik343abc62017-08-08 11:15:25 +0530865 audio_extn_utils_update_stream_output_app_type_cfg(adev->platform,
866 &adev->streams_output_cfg_list,
867 usecase->stream.inout->out_config.devices,
868 0,
869 usecase->stream.inout->out_config.format,
870 usecase->stream.inout->out_config.sample_rate,
871 usecase->stream.inout->out_config.bit_width,
872 usecase->stream.inout->out_config.channel_mask,
873 usecase->stream.inout->profile,
874 &usecase->stream.inout->out_app_type_cfg);
875 ALOGV("%s Selected apptype: %d", __func__, usecase->stream.inout->out_app_type_cfg.app_type);
876 break;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530877 default:
878 ALOGE("%s: app type cfg not supported for usecase type (%d)",
879 __func__, usecase->type);
880 }
881}
882
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +0530883void audio_extn_btsco_get_sample_rate(int snd_device, int *sample_rate)
884{
885 switch (snd_device) {
886 case SND_DEVICE_OUT_BT_SCO:
887 case SND_DEVICE_IN_BT_SCO_MIC:
888 case SND_DEVICE_IN_BT_SCO_MIC_NREC:
889 *sample_rate = 8000;
890 break;
891 case SND_DEVICE_OUT_BT_SCO_WB:
892 case SND_DEVICE_IN_BT_SCO_MIC_WB:
893 case SND_DEVICE_IN_BT_SCO_MIC_WB_NREC:
894 *sample_rate = 16000;
895 break;
896 default:
897 ALOGD("%s:Not a BT SCO device, need not update sampling rate\n", __func__);
898 break;
899 }
900}
901
Aalique Grahame22e49102018-12-18 14:23:57 -0800902static int set_stream_app_type_mixer_ctrl(struct audio_device *adev,
903 int pcm_device_id, int app_type,
904 int acdb_dev_id, int sample_rate,
905 int stream_type,
906 snd_device_t snd_device)
907{
908
909 char mixer_ctl_name[MAX_LENGTH_MIXER_CONTROL_IN_INT];
910 struct mixer_ctl *ctl;
911 int app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT], len = 0, rc = 0;
912 int snd_device_be_idx = -1;
913
914 if (stream_type == PCM_PLAYBACK) {
915 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
916 "Audio Stream %d App Type Cfg", pcm_device_id);
917 } else if (stream_type == PCM_CAPTURE) {
918 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
919 "Audio Stream Capture %d App Type Cfg", pcm_device_id);
920 }
921
922 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
923 if (!ctl) {
924 ALOGE("%s: Could not get ctl for mixer cmd - %s",
925 __func__, mixer_ctl_name);
926 rc = -EINVAL;
927 goto exit;
928 }
929 app_type_cfg[len++] = app_type;
930 app_type_cfg[len++] = acdb_dev_id;
931 app_type_cfg[len++] = sample_rate;
932
933 snd_device_be_idx = platform_get_snd_device_backend_index(snd_device);
934 if (snd_device_be_idx > 0)
935 app_type_cfg[len++] = snd_device_be_idx;
936 ALOGV("%s: stream type %d app_type %d, acdb_dev_id %d "
937 "sample rate %d, snd_device_be_idx %d",
938 __func__, stream_type, app_type, acdb_dev_id, sample_rate,
939 snd_device_be_idx);
940 mixer_ctl_set_array(ctl, app_type_cfg, len);
941
942exit:
943 return rc;
944}
945
946static int audio_extn_utils_send_app_type_cfg_hfp(struct audio_device *adev,
947 struct audio_usecase *usecase)
948{
949 int pcm_device_id, acdb_dev_id = 0, snd_device = usecase->out_snd_device;
950 int32_t sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
951 int app_type = 0, rc = 0;
952
953 ALOGV("%s", __func__);
954
955 if (usecase->type != PCM_HFP_CALL) {
956 ALOGV("%s: not a playback or HFP path, no need to cfg app type", __func__);
957 rc = 0;
958 goto exit_send_app_type_cfg;
959 }
960 if ((usecase->id != USECASE_AUDIO_HFP_SCO) &&
961 (usecase->id != USECASE_AUDIO_HFP_SCO_WB)) {
962 ALOGV("%s: a playback path where app type cfg is not required", __func__);
963 rc = 0;
964 goto exit_send_app_type_cfg;
965 }
966
967 snd_device = usecase->out_snd_device;
968 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_PLAYBACK);
969
970 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
971 if (acdb_dev_id < 0) {
972 ALOGE("%s: Couldn't get the acdb dev id", __func__);
973 rc = -EINVAL;
974 goto exit_send_app_type_cfg;
975 }
976
977 if (usecase->type == PCM_HFP_CALL) {
978
979 /* config HFP session:1 playback path */
980 app_type = platform_get_default_app_type_v2(adev->platform, PCM_PLAYBACK);
981 sample_rate= CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
982 rc = set_stream_app_type_mixer_ctrl(adev, pcm_device_id, app_type,
983 acdb_dev_id, sample_rate,
984 PCM_PLAYBACK,
985 SND_DEVICE_NONE); // use legacy behavior
986 if (rc < 0)
987 goto exit_send_app_type_cfg;
988
989 /* config HFP session:1 capture path */
990 app_type = platform_get_default_app_type_v2(adev->platform, PCM_CAPTURE);
991 rc = set_stream_app_type_mixer_ctrl(adev, pcm_device_id, app_type,
992 acdb_dev_id, sample_rate,
993 PCM_CAPTURE,
994 SND_DEVICE_NONE);
995 if (rc < 0)
996 goto exit_send_app_type_cfg;
997
998 /* config HFP session:2 capture path */
999 pcm_device_id = HFP_ASM_RX_TX;
1000 snd_device = usecase->in_snd_device;
1001 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
1002 if (acdb_dev_id <= 0) {
1003 ALOGE("%s: Couldn't get the acdb dev id", __func__);
1004 rc = -EINVAL;
1005 goto exit_send_app_type_cfg;
1006 }
1007 app_type = platform_get_default_app_type_v2(adev->platform, PCM_CAPTURE);
1008 rc = set_stream_app_type_mixer_ctrl(adev, pcm_device_id, app_type,
1009 acdb_dev_id, sample_rate, PCM_CAPTURE,
1010 SND_DEVICE_NONE);
1011 if (rc < 0)
1012 goto exit_send_app_type_cfg;
1013
1014 /* config HFP session:2 playback path */
1015 app_type = platform_get_default_app_type_v2(adev->platform, PCM_PLAYBACK);
1016 rc = set_stream_app_type_mixer_ctrl(adev, pcm_device_id, app_type,
1017 acdb_dev_id, sample_rate,
1018 PCM_PLAYBACK, SND_DEVICE_NONE);
1019 if (rc < 0)
1020 goto exit_send_app_type_cfg;
1021 }
1022
1023 rc = 0;
1024exit_send_app_type_cfg:
1025 return rc;
1026}
1027
Siena Richard7c2db772016-12-21 11:32:34 -08001028static int send_app_type_cfg_for_device(struct audio_device *adev,
1029 struct audio_usecase *usecase,
1030 int split_snd_device)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001031{
1032 char mixer_ctl_name[MAX_LENGTH_MIXER_CONTROL_IN_INT];
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301033 size_t app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT] = {0};
1034 int len = 0, rc;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001035 struct mixer_ctl *ctl;
Siena Richard7c2db772016-12-21 11:32:34 -08001036 int pcm_device_id = 0, acdb_dev_id, app_type;
1037 int snd_device = split_snd_device, snd_device_be_idx = -1;
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +05301038 int32_t sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Manish Dewangan837dc462015-05-27 10:17:41 +05301039 char value[PROPERTY_VALUE_MAX] = {0};
Varun Balaraje49253e2017-07-06 19:48:56 +05301040 struct streams_io_cfg *s_info = NULL;
1041 struct listnode *node = NULL;
Nikhil Laturkarac4a7492017-08-31 18:27:19 +05301042 int bd_app_type = 0;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001043
Siena Richard7c2db772016-12-21 11:32:34 -08001044 ALOGV("%s: usecase->out_snd_device %s, usecase->in_snd_device %s, split_snd_device %s",
1045 __func__, platform_get_snd_device_name(usecase->out_snd_device),
1046 platform_get_snd_device_name(usecase->in_snd_device),
1047 platform_get_snd_device_name(split_snd_device));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001048
Siddartha Shaik343abc62017-08-08 11:15:25 +05301049 if (usecase->type != PCM_PLAYBACK && usecase->type != PCM_CAPTURE &&
Surendar Karka93cd25a2018-08-28 14:21:37 +05301050 usecase->type != TRANSCODE_LOOPBACK_RX) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301051 ALOGE("%s: not a playback/capture path, no need to cfg app type", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001052 rc = 0;
1053 goto exit_send_app_type_cfg;
1054 }
1055 if ((usecase->id != USECASE_AUDIO_PLAYBACK_DEEP_BUFFER) &&
1056 (usecase->id != USECASE_AUDIO_PLAYBACK_LOW_LATENCY) &&
1057 (usecase->id != USECASE_AUDIO_PLAYBACK_MULTI_CH) &&
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -08001058 (usecase->id != USECASE_AUDIO_PLAYBACK_ULL) &&
Vikram Panduranga93f080e2017-06-07 18:16:14 -07001059 (usecase->id != USECASE_AUDIO_PLAYBACK_VOIP) &&
Surendar Karka93cd25a2018-08-28 14:21:37 +05301060 (usecase->id != USECASE_AUDIO_TRANSCODE_LOOPBACK_RX) &&
Varun Balaraje49253e2017-07-06 19:48:56 +05301061 (!is_interactive_usecase(usecase->id)) &&
Srikanth Uyyala9d551402015-08-25 16:03:42 +05301062 (!is_offload_usecase(usecase->id)) &&
Derek Chenf6318be2017-06-12 17:16:24 -04001063 (usecase->type != PCM_CAPTURE) &&
1064 (!audio_extn_auto_hal_is_bus_device_usecase(usecase->id))) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301065 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 -07001066 rc = 0;
1067 goto exit_send_app_type_cfg;
1068 }
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -08001069
1070 //if VR is active then only send the mixer control
1071 if (usecase->id == USECASE_AUDIO_PLAYBACK_ULL && !audio_is_vr_mode_on(adev)) {
1072 ALOGI("ULL doesnt need sending app type cfg, returning");
1073 rc = 0;
1074 goto exit_send_app_type_cfg;
1075 }
1076
Surendar Karka93cd25a2018-08-28 14:21:37 +05301077 if (usecase->type == PCM_PLAYBACK || usecase->type == TRANSCODE_LOOPBACK_RX) {
Ben Romberger1fafdde2015-09-09 19:43:15 -07001078 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_PLAYBACK);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301079 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
1080 "Audio Stream %d App Type Cfg", pcm_device_id);
Ben Romberger1fafdde2015-09-09 19:43:15 -07001081 } else if (usecase->type == PCM_CAPTURE) {
Ben Romberger1fafdde2015-09-09 19:43:15 -07001082 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_CAPTURE);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301083 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
1084 "Audio Stream Capture %d App Type Cfg", pcm_device_id);
Srikanth Uyyala9d551402015-08-25 16:03:42 +05301085 }
Siena Richard7c2db772016-12-21 11:32:34 -08001086
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001087 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
1088 if (!ctl) {
1089 ALOGE("%s: Could not get ctl for mixer cmd - %s", __func__,
1090 mixer_ctl_name);
1091 rc = -EINVAL;
1092 goto exit_send_app_type_cfg;
1093 }
Aditya Bavanari701a6992017-03-30 19:17:16 +05301094 snd_device = platform_get_spkr_prot_snd_device(snd_device);
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301095 if (voice_is_in_call_rec_stream(usecase->stream.in) && usecase->type == PCM_CAPTURE) {
1096 snd_device_t voice_device = voice_get_incall_rec_snd_device(usecase->in_snd_device);
1097 acdb_dev_id = platform_get_snd_device_acdb_id(voice_device);
1098 ALOGV("acdb id for voice call use case %d", acdb_dev_id);
1099 } else {
1100 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
1101 }
Rohit Kumar1181b292017-01-31 18:07:17 +05301102 if (acdb_dev_id <= 0) {
1103 ALOGE("%s: Couldn't get the acdb dev id", __func__);
1104 rc = -EINVAL;
1105 goto exit_send_app_type_cfg;
1106 }
1107
Siena Richard7c2db772016-12-21 11:32:34 -08001108 snd_device_be_idx = platform_get_snd_device_backend_index(snd_device);
1109 if (snd_device_be_idx < 0) {
1110 ALOGE("%s: Couldn't get the backend index for snd device %s ret=%d",
1111 __func__, platform_get_snd_device_name(snd_device),
1112 snd_device_be_idx);
1113 }
1114
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301115 if ((usecase->type == PCM_PLAYBACK) && (usecase->stream.out != NULL)) {
Manish Dewangan837dc462015-05-27 10:17:41 +05301116
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -07001117 property_get("vendor.audio.playback.mch.downsample",value,"");
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301118 if (!strncmp("true", value, sizeof("true"))) {
1119 if ((popcount(usecase->stream.out->channel_mask) > 2) &&
1120 (usecase->stream.out->app_type_cfg.sample_rate > CODEC_BACKEND_DEFAULT_SAMPLE_RATE) &&
Aalique Grahame5cd12ff2017-10-19 10:57:00 -07001121 !(usecase->stream.out->flags &
1122 (audio_output_flags_t)AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH))
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301123 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
1124 }
Ashish Jainc02430d2016-01-04 10:42:43 +05301125
kunleizcdf25942017-09-20 14:01:21 +08001126 if (usecase->id == USECASE_AUDIO_PLAYBACK_VOIP) {
1127 usecase->stream.out->app_type_cfg.sample_rate = usecase->stream.out->sample_rate;
1128 } else if (usecase->stream.out->devices & AUDIO_DEVICE_OUT_SPEAKER) {
Dieter Luecking5d57def2018-09-07 14:23:37 +02001129 if (platform_spkr_use_default_sample_rate(adev->platform)) {
1130 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
1131 } else {
1132 platform_check_and_update_copp_sample_rate(adev->platform, snd_device,
1133 usecase->stream.out->sample_rate,
1134 &usecase->stream.out->app_type_cfg.sample_rate);
1135 }
1136
Ashish Jaina052e572016-11-07 16:41:07 +05301137 } else if ((snd_device == SND_DEVICE_OUT_HDMI ||
1138 snd_device == SND_DEVICE_OUT_USB_HEADSET ||
1139 snd_device == SND_DEVICE_OUT_DISPLAY_PORT) &&
1140 (usecase->stream.out->sample_rate >= OUTPUT_SAMPLING_RATE_44100)) {
1141 /*
1142 * To best utlize DSP, check if the stream sample rate is supported/multiple of
1143 * configured device sample rate, if not update the COPP rate to be equal to the
1144 * device sample rate, else open COPP at stream sample rate
1145 */
1146 platform_check_and_update_copp_sample_rate(adev->platform, snd_device,
1147 usecase->stream.out->sample_rate,
1148 &usecase->stream.out->app_type_cfg.sample_rate);
Ashish Jain4826f6c2017-02-06 13:33:20 +05301149 } else if (((snd_device != SND_DEVICE_OUT_HEADPHONES_44_1 &&
1150 !audio_is_this_native_usecase(usecase)) &&
Mingming Yin21854652016-04-13 11:54:02 -07001151 usecase->stream.out->sample_rate == OUTPUT_SAMPLING_RATE_44100) ||
1152 (usecase->stream.out->sample_rate < OUTPUT_SAMPLING_RATE_44100)) {
Ashish Jain4826f6c2017-02-06 13:33:20 +05301153 /* Reset to default if no native stream is active*/
Mingming Yin21854652016-04-13 11:54:02 -07001154 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Preetam Singh Ranawatb7475ba2017-02-24 18:17:06 +05301155 } else if (usecase->stream.out->devices & AUDIO_DEVICE_OUT_ALL_A2DP) {
1156 /*
1157 * For a2dp playback get encoder sampling rate and set copp sampling rate,
1158 * for bit width use the stream param only.
1159 */
Florian Pfister1a84f312018-07-19 14:38:18 +02001160 audio_extn_a2dp_get_enc_sample_rate(&usecase->stream.out->app_type_cfg.sample_rate);
Preetam Singh Ranawatb7475ba2017-02-24 18:17:06 +05301161 ALOGI("%s using %d sample rate rate for A2DP CoPP",
1162 __func__, usecase->stream.out->app_type_cfg.sample_rate);
Mingming Yin21854652016-04-13 11:54:02 -07001163 }
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301164 audio_extn_btsco_get_sample_rate(snd_device, &usecase->stream.out->app_type_cfg.sample_rate);
Mingming Yin21854652016-04-13 11:54:02 -07001165 sample_rate = usecase->stream.out->app_type_cfg.sample_rate;
1166
Varun Balaraje49253e2017-07-06 19:48:56 +05301167 /* Interactive streams are supported with only direct app type id.
1168 * Get Direct profile app type and use it for interactive streams
1169 */
1170 list_for_each(node, &adev->streams_output_cfg_list) {
1171 s_info = node_to_item(node, struct streams_io_cfg, list);
Aalique Grahame5cd12ff2017-10-19 10:57:00 -07001172 if (s_info->flags.out_flags == (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_BD |
Nikhil Laturkarac4a7492017-08-31 18:27:19 +05301173 AUDIO_OUTPUT_FLAG_DIRECT_PCM |
1174 AUDIO_OUTPUT_FLAG_DIRECT))
1175 bd_app_type = s_info->app_type_cfg.app_type;
Varun Balaraje49253e2017-07-06 19:48:56 +05301176 }
Aalique Grahame5cd12ff2017-10-19 10:57:00 -07001177 if (usecase->stream.out->flags == (audio_output_flags_t)AUDIO_OUTPUT_FLAG_INTERACTIVE)
Nikhil Laturkarac4a7492017-08-31 18:27:19 +05301178 app_type = bd_app_type;
Varun Balaraje49253e2017-07-06 19:48:56 +05301179 else
1180 app_type = usecase->stream.out->app_type_cfg.app_type;
Siena Richard7c2db772016-12-21 11:32:34 -08001181 app_type_cfg[len++] = app_type;
Mingming Yin21854652016-04-13 11:54:02 -07001182 app_type_cfg[len++] = acdb_dev_id;
1183 if (((usecase->stream.out->format == AUDIO_FORMAT_E_AC3) ||
Ben Romberger1aaaf862017-04-06 17:49:46 -07001184 (usecase->stream.out->format == AUDIO_FORMAT_E_AC3_JOC) ||
1185 (usecase->stream.out->format == AUDIO_FORMAT_DOLBY_TRUEHD))
Harsh Bansal026d97f2017-08-17 17:44:49 +05301186 && audio_extn_passthru_is_passthrough_stream(usecase->stream.out)
1187 && !audio_extn_passthru_is_convert_supported(adev, usecase->stream.out)) {
Naresh Tanniru3a406772017-05-10 13:09:05 -07001188
1189 sample_rate = sample_rate * 4;
1190 if (sample_rate > HDMI_PASSTHROUGH_MAX_SAMPLE_RATE)
1191 sample_rate = HDMI_PASSTHROUGH_MAX_SAMPLE_RATE;
Mingming Yin21854652016-04-13 11:54:02 -07001192 }
Naresh Tanniru3a406772017-05-10 13:09:05 -07001193 app_type_cfg[len++] = sample_rate;
1194
Siena Richard7c2db772016-12-21 11:32:34 -08001195 if (snd_device_be_idx > 0)
1196 app_type_cfg[len++] = snd_device_be_idx;
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301197
Siena Richard7c2db772016-12-21 11:32:34 -08001198 ALOGI("%s PLAYBACK app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
1199 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301200
1201 } else if ((usecase->type == PCM_CAPTURE) && (usecase->stream.in != NULL)) {
Siena Richard7c2db772016-12-21 11:32:34 -08001202 app_type = usecase->stream.in->app_type_cfg.app_type;
1203 app_type_cfg[len++] = app_type;
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301204 app_type_cfg[len++] = acdb_dev_id;
kunleizcdf25942017-09-20 14:01:21 +08001205 if (usecase->id == USECASE_AUDIO_RECORD_VOIP)
1206 usecase->stream.in->app_type_cfg.sample_rate = usecase->stream.in->sample_rate;
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301207 if (voice_is_in_call_rec_stream(usecase->stream.in)) {
1208 audio_extn_btsco_get_sample_rate(usecase->in_snd_device, &usecase->stream.in->app_type_cfg.sample_rate);
1209 } else {
1210 audio_extn_btsco_get_sample_rate(snd_device, &usecase->stream.in->app_type_cfg.sample_rate);
1211 }
Deeraj Somanfc791e72018-11-30 13:23:01 +05301212 if (usecase->stream.in->device & AUDIO_DEVICE_IN_BLUETOOTH_A2DP & ~AUDIO_DEVICE_BIT_IN) {
Florian Pfister1a84f312018-07-19 14:38:18 +02001213 audio_extn_a2dp_get_dec_sample_rate(&usecase->stream.in->app_type_cfg.sample_rate);
1214 ALOGI("%s using %d sample rate rate for A2DP dec CoPP",
1215 __func__, usecase->stream.in->app_type_cfg.sample_rate);
1216 }
Siena Richard7c2db772016-12-21 11:32:34 -08001217 sample_rate = usecase->stream.in->app_type_cfg.sample_rate;
1218 app_type_cfg[len++] = sample_rate;
1219 if (snd_device_be_idx > 0)
1220 app_type_cfg[len++] = snd_device_be_idx;
1221 ALOGI("%s CAPTURE app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
1222 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301223 } else {
Siena Richard7c2db772016-12-21 11:32:34 -08001224 app_type = platform_get_default_app_type_v2(adev->platform, usecase->type);
Surendar Karka93cd25a2018-08-28 14:21:37 +05301225 if(usecase->type == TRANSCODE_LOOPBACK_RX) {
Siddartha Shaik343abc62017-08-08 11:15:25 +05301226 sample_rate = usecase->stream.inout->out_config.sample_rate;
1227 app_type = usecase->stream.inout->out_app_type_cfg.app_type;
1228 }
Siena Richard7c2db772016-12-21 11:32:34 -08001229 app_type_cfg[len++] = app_type;
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301230 app_type_cfg[len++] = acdb_dev_id;
1231 app_type_cfg[len++] = sample_rate;
Siena Richard7c2db772016-12-21 11:32:34 -08001232 if (snd_device_be_idx > 0)
1233 app_type_cfg[len++] = snd_device_be_idx;
1234 ALOGI("%s default app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
1235 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Ashish Jainc02430d2016-01-04 10:42:43 +05301236 }
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301237
Siddartha Shaik343abc62017-08-08 11:15:25 +05301238 if(ctl)
1239 mixer_ctl_set_array(ctl, app_type_cfg, len);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001240 rc = 0;
1241exit_send_app_type_cfg:
1242 return rc;
1243}
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001244
Siena Richard7c2db772016-12-21 11:32:34 -08001245int audio_extn_utils_send_app_type_cfg(struct audio_device *adev,
1246 struct audio_usecase *usecase)
1247{
1248 int i, num_devices = 0;
1249 snd_device_t new_snd_devices[SND_DEVICE_OUT_END] = {0};
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301250 snd_device_t in_snd_device = usecase->in_snd_device;
Siena Richard7c2db772016-12-21 11:32:34 -08001251 int rc = 0;
1252
Aalique Grahame22e49102018-12-18 14:23:57 -08001253 if (usecase->type == PCM_HFP_CALL) {
1254 return audio_extn_utils_send_app_type_cfg_hfp(adev, usecase);
1255 }
1256
Siena Richard7c2db772016-12-21 11:32:34 -08001257 switch (usecase->type) {
1258 case PCM_PLAYBACK:
Surendar Karka93cd25a2018-08-28 14:21:37 +05301259 case TRANSCODE_LOOPBACK_RX:
Siena Richard7c2db772016-12-21 11:32:34 -08001260 ALOGD("%s: usecase->out_snd_device %s",
1261 __func__, platform_get_snd_device_name(usecase->out_snd_device));
1262 /* check for out combo device */
1263 if (platform_split_snd_device(adev->platform,
1264 usecase->out_snd_device,
1265 &num_devices, new_snd_devices)) {
1266 new_snd_devices[0] = usecase->out_snd_device;
1267 num_devices = 1;
1268 }
1269 break;
1270 case PCM_CAPTURE:
1271 ALOGD("%s: usecase->in_snd_device %s",
1272 __func__, platform_get_snd_device_name(usecase->in_snd_device));
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301273 if (voice_is_in_call_rec_stream(usecase->stream.in)) {
1274 in_snd_device = voice_get_incall_rec_backend_device(usecase->stream.in);
1275 }
Siena Richard7c2db772016-12-21 11:32:34 -08001276 /* check for in combo device */
1277 if (platform_split_snd_device(adev->platform,
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301278 in_snd_device,
Siena Richard7c2db772016-12-21 11:32:34 -08001279 &num_devices, new_snd_devices)) {
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301280 new_snd_devices[0] = in_snd_device;
Siena Richard7c2db772016-12-21 11:32:34 -08001281 num_devices = 1;
1282 }
1283 break;
1284 default:
1285 ALOGI("%s: not a playback/capture path, no need to cfg app type", __func__);
1286 rc = 0;
1287 break;
1288 }
1289
1290 for (i = 0; i < num_devices; i++) {
1291 rc = send_app_type_cfg_for_device(adev, usecase, new_snd_devices[i]);
1292 if (rc)
1293 break;
1294 }
1295
1296 return rc;
1297}
1298
Preetam Singh Ranawat9519e9c2015-11-18 16:05:55 +05301299int read_line_from_file(const char *path, char *buf, size_t count)
1300{
1301 char * fgets_ret;
1302 FILE * fd;
1303 int rv;
1304
1305 fd = fopen(path, "r");
1306 if (fd == NULL)
1307 return -1;
1308
1309 fgets_ret = fgets(buf, (int)count, fd);
1310 if (NULL != fgets_ret) {
1311 rv = (int)strlen(buf);
1312 } else {
1313 rv = ferror(fd);
1314 }
1315 fclose(fd);
1316
1317 return rv;
1318}
1319
Ashish Jainf1eaa582016-05-23 20:54:24 +05301320/*Translates ALSA formats to AOSP PCM formats*/
1321audio_format_t alsa_format_to_hal(uint32_t alsa_format)
1322{
1323 audio_format_t format;
1324
1325 switch(alsa_format) {
1326 case SNDRV_PCM_FORMAT_S16_LE:
1327 format = AUDIO_FORMAT_PCM_16_BIT;
1328 break;
1329 case SNDRV_PCM_FORMAT_S24_3LE:
1330 format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
1331 break;
1332 case SNDRV_PCM_FORMAT_S24_LE:
1333 format = AUDIO_FORMAT_PCM_8_24_BIT;
1334 break;
1335 case SNDRV_PCM_FORMAT_S32_LE:
1336 format = AUDIO_FORMAT_PCM_32_BIT;
1337 break;
1338 default:
1339 ALOGW("Incorrect ALSA format");
1340 format = AUDIO_FORMAT_INVALID;
1341 }
1342 return format;
1343}
1344
1345/*Translates hal format (AOSP) to alsa formats*/
1346uint32_t hal_format_to_alsa(audio_format_t hal_format)
1347{
1348 uint32_t alsa_format;
1349
1350 switch (hal_format) {
1351 case AUDIO_FORMAT_PCM_32_BIT: {
1352 if (platform_supports_true_32bit())
1353 alsa_format = SNDRV_PCM_FORMAT_S32_LE;
1354 else
1355 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1356 }
1357 break;
1358 case AUDIO_FORMAT_PCM_8_BIT:
1359 alsa_format = SNDRV_PCM_FORMAT_S8;
1360 break;
1361 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
1362 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1363 break;
1364 case AUDIO_FORMAT_PCM_8_24_BIT: {
1365 if (platform_supports_true_32bit())
1366 alsa_format = SNDRV_PCM_FORMAT_S32_LE;
1367 else
1368 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1369 }
1370 break;
1371 case AUDIO_FORMAT_PCM_FLOAT:
1372 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1373 break;
1374 default:
1375 case AUDIO_FORMAT_PCM_16_BIT:
1376 alsa_format = SNDRV_PCM_FORMAT_S16_LE;
1377 break;
1378 }
1379 return alsa_format;
1380}
1381
Ashish Jain83a6cc22016-06-28 14:34:17 +05301382/*Translates PCM formats to AOSP formats*/
1383audio_format_t pcm_format_to_hal(uint32_t pcm_format)
1384{
1385 audio_format_t format = AUDIO_FORMAT_INVALID;
1386
1387 switch(pcm_format) {
1388 case PCM_FORMAT_S16_LE:
1389 format = AUDIO_FORMAT_PCM_16_BIT;
1390 break;
1391 case PCM_FORMAT_S24_3LE:
1392 format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
1393 break;
1394 case PCM_FORMAT_S24_LE:
1395 format = AUDIO_FORMAT_PCM_8_24_BIT;
1396 break;
1397 case PCM_FORMAT_S32_LE:
1398 format = AUDIO_FORMAT_PCM_32_BIT;
1399 break;
1400 default:
1401 ALOGW("Incorrect PCM format");
1402 format = AUDIO_FORMAT_INVALID;
1403 }
1404 return format;
1405}
1406
1407/*Translates hal format (AOSP) to alsa formats*/
1408uint32_t hal_format_to_pcm(audio_format_t hal_format)
1409{
1410 uint32_t pcm_format;
1411
1412 switch (hal_format) {
1413 case AUDIO_FORMAT_PCM_32_BIT:
1414 case AUDIO_FORMAT_PCM_8_24_BIT:
1415 case AUDIO_FORMAT_PCM_FLOAT: {
1416 if (platform_supports_true_32bit())
1417 pcm_format = PCM_FORMAT_S32_LE;
1418 else
1419 pcm_format = PCM_FORMAT_S24_3LE;
1420 }
1421 break;
1422 case AUDIO_FORMAT_PCM_8_BIT:
1423 pcm_format = PCM_FORMAT_S8;
1424 break;
1425 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
1426 pcm_format = PCM_FORMAT_S24_3LE;
1427 break;
1428 default:
1429 case AUDIO_FORMAT_PCM_16_BIT:
1430 pcm_format = PCM_FORMAT_S16_LE;
1431 break;
1432 }
1433 return pcm_format;
1434}
1435
Ashish Jainf1eaa582016-05-23 20:54:24 +05301436uint32_t get_alsa_fragment_size(uint32_t bytes_per_sample,
1437 uint32_t sample_rate,
1438 uint32_t noOfChannels)
1439{
1440 uint32_t fragment_size = 0;
1441 uint32_t pcm_offload_time = PCM_OFFLOAD_BUFFER_DURATION;
1442
1443 fragment_size = (pcm_offload_time
1444 * sample_rate
1445 * bytes_per_sample
1446 * noOfChannels)/1000;
1447 if (fragment_size < MIN_PCM_OFFLOAD_FRAGMENT_SIZE)
1448 fragment_size = MIN_PCM_OFFLOAD_FRAGMENT_SIZE;
1449 else if (fragment_size > MAX_PCM_OFFLOAD_FRAGMENT_SIZE)
1450 fragment_size = MAX_PCM_OFFLOAD_FRAGMENT_SIZE;
1451 /*To have same PCM samples for all channels, the buffer size requires to
1452 *be multiple of (number of channels * bytes per sample)
1453 *For writes to succeed, the buffer must be written at address which is multiple of 32
1454 */
Aalique Grahameb85f2d92017-10-16 17:53:23 -07001455 fragment_size = ALIGN(fragment_size, (bytes_per_sample * noOfChannels * 32));
Ashish Jainf1eaa582016-05-23 20:54:24 +05301456
1457 ALOGI("PCM offload Fragment size to %d bytes", fragment_size);
1458 return fragment_size;
1459}
1460
1461/* Calculates the fragment size required to configure compress session.
1462 * Based on the alsa format selected, decide if conversion is needed in
1463
1464 * HAL ( e.g. convert AUDIO_FORMAT_PCM_FLOAT input format to
1465 * AUDIO_FORMAT_PCM_24_BIT_PACKED before writing to the compress driver.
1466 */
1467void audio_extn_utils_update_direct_pcm_fragment_size(struct stream_out *out)
1468{
Ashish Jain83a6cc22016-06-28 14:34:17 +05301469 audio_format_t dst_format = out->hal_op_format;
1470 audio_format_t src_format = out->hal_ip_format;
Ashish Jainf1eaa582016-05-23 20:54:24 +05301471 uint32_t hal_op_bytes_per_sample = audio_bytes_per_sample(dst_format);
1472 uint32_t hal_ip_bytes_per_sample = audio_bytes_per_sample(src_format);
1473
1474 out->compr_config.fragment_size =
1475 get_alsa_fragment_size(hal_op_bytes_per_sample,
1476 out->sample_rate,
1477 popcount(out->channel_mask));
1478
1479 if ((src_format != dst_format) &&
1480 hal_op_bytes_per_sample != hal_ip_bytes_per_sample) {
1481
Ashish Jain83a6cc22016-06-28 14:34:17 +05301482 out->hal_fragment_size =
Ashish Jainf1eaa582016-05-23 20:54:24 +05301483 ((out->compr_config.fragment_size * hal_ip_bytes_per_sample) /
1484 hal_op_bytes_per_sample);
1485 ALOGI("enable conversion hal_input_fragment_size is %d src_format %x dst_format %x",
Ashish Jain83a6cc22016-06-28 14:34:17 +05301486 out->hal_fragment_size, src_format, dst_format);
Ashish Jainf1eaa582016-05-23 20:54:24 +05301487 } else {
Ashish Jain83a6cc22016-06-28 14:34:17 +05301488 out->hal_fragment_size = out->compr_config.fragment_size;
Ashish Jainf1eaa582016-05-23 20:54:24 +05301489 }
1490}
1491
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301492/* converts pcm format 24_8 to 8_24 inplace */
1493size_t audio_extn_utils_convert_format_24_8_to_8_24(void *buf, size_t bytes)
1494{
1495 size_t i = 0;
1496 int *int_buf_stream = buf;
1497
1498 if ((bytes % 4) != 0) {
1499 ALOGE("%s: wrong inout buffer! ... is not 32 bit aligned ", __func__);
1500 return -EINVAL;
1501 }
1502
1503 for (; i < (bytes / 4); i++)
1504 int_buf_stream[i] >>= 8;
1505
1506 return bytes;
1507}
1508
1509int get_snd_codec_id(audio_format_t format)
1510{
1511 int id = 0;
1512
1513 switch (format & AUDIO_FORMAT_MAIN_MASK) {
1514 case AUDIO_FORMAT_MP3:
1515 id = SND_AUDIOCODEC_MP3;
1516 break;
1517 case AUDIO_FORMAT_AAC:
1518 id = SND_AUDIOCODEC_AAC;
1519 break;
1520 case AUDIO_FORMAT_AAC_ADTS:
1521 id = SND_AUDIOCODEC_AAC;
1522 break;
Arun Kumar Dasari3b174182016-12-27 13:01:14 +05301523 case AUDIO_FORMAT_AAC_LATM:
1524 id = SND_AUDIOCODEC_AAC;
1525 break;
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301526 case AUDIO_FORMAT_PCM:
1527 id = SND_AUDIOCODEC_PCM;
1528 break;
1529 case AUDIO_FORMAT_FLAC:
1530 id = SND_AUDIOCODEC_FLAC;
1531 break;
1532 case AUDIO_FORMAT_ALAC:
1533 id = SND_AUDIOCODEC_ALAC;
1534 break;
1535 case AUDIO_FORMAT_APE:
1536 id = SND_AUDIOCODEC_APE;
1537 break;
1538 case AUDIO_FORMAT_VORBIS:
1539 id = SND_AUDIOCODEC_VORBIS;
1540 break;
1541 case AUDIO_FORMAT_WMA:
1542 id = SND_AUDIOCODEC_WMA;
1543 break;
1544 case AUDIO_FORMAT_WMA_PRO:
1545 id = SND_AUDIOCODEC_WMA_PRO;
1546 break;
Satish Babu Patakokila0c313922016-12-08 12:07:08 +05301547 case AUDIO_FORMAT_MP2:
1548 id = SND_AUDIOCODEC_MP2;
1549 break;
Satish Babu Patakokila915ecba2017-01-10 17:43:56 +05301550 case AUDIO_FORMAT_AC3:
1551 id = SND_AUDIOCODEC_AC3;
1552 break;
1553 case AUDIO_FORMAT_E_AC3:
1554 case AUDIO_FORMAT_E_AC3_JOC:
1555 id = SND_AUDIOCODEC_EAC3;
1556 break;
1557 case AUDIO_FORMAT_DTS:
1558 case AUDIO_FORMAT_DTS_HD:
1559 id = SND_AUDIOCODEC_DTS;
1560 break;
Ben Romberger1aaaf862017-04-06 17:49:46 -07001561 case AUDIO_FORMAT_DOLBY_TRUEHD:
1562 id = SND_AUDIOCODEC_TRUEHD;
1563 break;
Naresh Tanniru928f0862017-04-07 16:44:23 -07001564 case AUDIO_FORMAT_IEC61937:
1565 id = SND_AUDIOCODEC_IEC61937;
1566 break;
Preetam Singh Ranawat4277a5a2017-01-18 19:02:24 +05301567 case AUDIO_FORMAT_DSD:
1568 id = SND_AUDIOCODEC_DSD;
1569 break;
Dhanalakshmi Siddani18737932016-11-29 17:33:17 +05301570 case AUDIO_FORMAT_APTX:
1571 id = SND_AUDIOCODEC_APTX;
1572 break;
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301573 default:
1574 ALOGE("%s: Unsupported audio format :%x", __func__, format);
1575 }
1576
1577 return id;
1578}
1579
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001580void audio_extn_utils_send_audio_calibration(struct audio_device *adev,
1581 struct audio_usecase *usecase)
1582{
1583 int type = usecase->type;
1584
Dhananjay Kumar14245982017-01-16 20:21:00 +05301585 if (type == PCM_PLAYBACK && usecase->stream.out != NULL) {
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001586 struct stream_out *out = usecase->stream.out;
1587 int snd_device = usecase->out_snd_device;
1588 snd_device = (snd_device == SND_DEVICE_OUT_SPEAKER) ?
Xiaojun Sang040cc9f2015-08-03 19:38:28 +08001589 platform_get_spkr_prot_snd_device(snd_device) : snd_device;
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05301590 platform_send_audio_calibration(adev->platform, usecase,
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001591 out->app_type_cfg.app_type,
Preetam Singh Ranawat61716b12015-12-14 11:55:24 +05301592 usecase->stream.out->app_type_cfg.sample_rate);
Dhananjay Kumar14245982017-01-16 20:21:00 +05301593 } else if (type == PCM_CAPTURE && usecase->stream.in != NULL) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301594 platform_send_audio_calibration(adev->platform, usecase,
1595 usecase->stream.in->app_type_cfg.app_type,
1596 usecase->stream.in->app_type_cfg.sample_rate);
Vidyakumar Athota653aaef2017-03-16 11:11:31 -07001597 } else if (type == PCM_HFP_CALL || type == PCM_CAPTURE) {
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05301598 /* when app type is default. the sample rate is not used to send cal */
1599 platform_send_audio_calibration(adev->platform, usecase,
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301600 platform_get_default_app_type_v2(adev->platform, usecase->type),
1601 48000);
Surendar Karka93cd25a2018-08-28 14:21:37 +05301602 } else if (type == TRANSCODE_LOOPBACK_RX && usecase->stream.inout != NULL) {
Siddartha Shaik343abc62017-08-08 11:15:25 +05301603 int snd_device = usecase->out_snd_device;
1604 snd_device = (snd_device == SND_DEVICE_OUT_SPEAKER) ?
1605 platform_get_spkr_prot_snd_device(snd_device) : snd_device;
1606 platform_send_audio_calibration(adev->platform, usecase,
1607 platform_get_default_app_type_v2(adev->platform, usecase->type),
1608 usecase->stream.inout->out_config.sample_rate);
Vidyakumar Athotae57f6002017-03-01 13:13:16 -08001609 } else {
1610 /* No need to send audio calibration for voice and voip call usecases */
1611 if ((type != VOICE_CALL) && (type != VOIP_CALL))
1612 ALOGW("%s: No audio calibration for usecase type = %d", __func__, type);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001613 }
1614}
1615
Ben Rombergera04fabc2014-11-14 12:16:03 -08001616// Base64 Encode and Decode
1617// Not all features supported. This must be used only with following conditions.
1618// Decode Modes: Support with and without padding
1619// CRLF not handling. So no CRLF in string to decode.
1620// Encode Modes: Supports only padding
1621int b64decode(char *inp, int ilen, uint8_t* outp)
1622{
1623 int i, j, k, ii, num;
1624 int rem, pcnt;
1625 uint32_t res=0;
1626 uint8_t getIndex[MAX_BASEINDEX_LEN];
1627 uint8_t tmp, cflag;
1628
1629 if(inp == NULL || outp == NULL || ilen <= 0) {
1630 ALOGE("[%s] received NULL pointer or zero length",__func__);
1631 return -1;
1632 }
1633
1634 memset(getIndex, MAX_BASEINDEX_LEN-1, sizeof(getIndex));
1635 for(i=0;i<BASE_TABLE_SIZE;i++) {
1636 getIndex[(uint8_t)bTable[i]] = (uint8_t)i;
1637 }
1638 getIndex[(uint8_t)'=']=0;
1639
1640 j=0;k=0;
1641 num = ilen/4;
1642 rem = ilen%4;
1643 if(rem==0)
1644 num = num-1;
1645 cflag=0;
1646 for(i=0; i<num; i++) {
1647 res=0;
1648 for(ii=0;ii<4;ii++) {
1649 res = res << 6;
1650 tmp = getIndex[(uint8_t)inp[j++]];
1651 res = res | tmp;
1652 cflag = cflag | tmp;
1653 }
1654 outp[k++] = (res >> 16)&0xFF;
1655 outp[k++] = (res >> 8)&0xFF;
1656 outp[k++] = res & 0xFF;
1657 }
1658
1659 // Handle last bytes special
1660 pcnt=0;
1661 if(rem == 0) {
1662 //With padding or full data
1663 res = 0;
1664 for(ii=0;ii<4;ii++) {
1665 if(inp[j] == '=')
1666 pcnt++;
1667 res = res << 6;
1668 tmp = getIndex[(uint8_t)inp[j++]];
1669 res = res | tmp;
1670 cflag = cflag | tmp;
1671 }
1672 outp[k++] = res >> 16;
1673 if(pcnt == 2)
1674 goto done;
1675 outp[k++] = (res>>8)&0xFF;
1676 if(pcnt == 1)
1677 goto done;
1678 outp[k++] = res&0xFF;
1679 } else {
1680 //without padding
1681 res = 0;
1682 for(i=0;i<rem;i++) {
1683 res = res << 6;
1684 tmp = getIndex[(uint8_t)inp[j++]];
1685 res = res | tmp;
1686 cflag = cflag | tmp;
1687 }
1688 for(i=rem;i<4;i++) {
1689 res = res << 6;
1690 pcnt++;
1691 }
1692 outp[k++] = res >> 16;
1693 if(pcnt == 2)
1694 goto done;
1695 outp[k++] = (res>>8)&0xFF;
1696 if(pcnt == 1)
1697 goto done;
1698 outp[k++] = res&0xFF;
1699 }
1700done:
1701 if(cflag == 0xFF) {
1702 ALOGE("[%s] base64 decode failed. Invalid character found %s",
1703 __func__, inp);
1704 return 0;
1705 }
1706 return k;
1707}
1708
1709int b64encode(uint8_t *inp, int ilen, char* outp)
1710{
1711 int i,j,k, num;
1712 int rem=0;
1713 uint32_t res=0;
1714
1715 if(inp == NULL || outp == NULL || ilen<=0) {
1716 ALOGE("[%s] received NULL pointer or zero input length",__func__);
1717 return -1;
1718 }
1719
1720 num = ilen/3;
1721 rem = ilen%3;
1722 j=0;k=0;
1723 for(i=0; i<num; i++) {
1724 //prepare index
1725 res = inp[j++]<<16;
1726 res = res | inp[j++]<<8;
1727 res = res | inp[j++];
1728 //get output map from index
1729 outp[k++] = (char) bTable[(res>>18)&0x3F];
1730 outp[k++] = (char) bTable[(res>>12)&0x3F];
1731 outp[k++] = (char) bTable[(res>>6)&0x3F];
1732 outp[k++] = (char) bTable[res&0x3F];
1733 }
1734
1735 switch(rem) {
1736 case 1:
1737 res = inp[j++]<<16;
1738 outp[k++] = (char) bTable[res>>18];
1739 outp[k++] = (char) bTable[(res>>12)&0x3F];
1740 //outp[k++] = '=';
1741 //outp[k++] = '=';
1742 break;
1743 case 2:
1744 res = inp[j++]<<16;
1745 res = res | inp[j++]<<8;
1746 outp[k++] = (char) bTable[res>>18];
1747 outp[k++] = (char) bTable[(res>>12)&0x3F];
1748 outp[k++] = (char) bTable[(res>>6)&0x3F];
1749 //outp[k++] = '=';
1750 break;
1751 default:
1752 break;
1753 }
Ben Rombergera04fabc2014-11-14 12:16:03 -08001754 outp[k] = '\0';
1755 return k;
1756}
Ashish Jain81eb2a82015-05-13 10:52:34 +05301757
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301758
1759int audio_extn_utils_get_codec_version(const char *snd_card_name,
1760 int card_num,
1761 char *codec_version)
1762{
1763 char procfs_path[50];
1764 FILE *fp;
1765
1766 if (strstr(snd_card_name, "tasha")) {
1767 snprintf(procfs_path, sizeof(procfs_path),
1768 "/proc/asound/card%d/codecs/tasha/version", card_num);
1769 if ((fp = fopen(procfs_path, "r")) != NULL) {
1770 fgets(codec_version, CODEC_VERSION_MAX_LENGTH, fp);
1771 fclose(fp);
1772 } else {
1773 ALOGE("%s: ERROR. cannot open %s", __func__, procfs_path);
1774 return -ENOENT;
1775 }
1776 ALOGD("%s: codec version %s", __func__, codec_version);
1777 }
1778
1779 return 0;
1780}
1781
1782
Ashish Jain81eb2a82015-05-13 10:52:34 +05301783#ifdef AUDIO_EXTERNAL_HDMI_ENABLED
1784
1785void get_default_compressed_channel_status(
1786 unsigned char *channel_status)
1787{
Ashish Jain81eb2a82015-05-13 10:52:34 +05301788 memset(channel_status,0,24);
1789
1790 /* block start bit in preamble bit 3 */
1791 channel_status[0] |= PROFESSIONAL;
1792 //compre out
1793 channel_status[0] |= NON_LPCM;
1794 // sample rate; fixed 48K for default/transcode
1795 channel_status[3] |= SR_48000;
1796}
1797
Ashish Jain81eb2a82015-05-13 10:52:34 +05301798int32_t get_compressed_channel_status(void *audio_stream_data,
1799 uint32_t audio_frame_size,
1800 unsigned char *channel_status,
1801 enum audio_parser_code_type codec_type)
1802 // codec_type - AUDIO_PARSER_CODEC_AC3
1803 // - AUDIO_PARSER_CODEC_DTS
1804{
1805 unsigned char *stream;
1806 int ret = 0;
1807 stream = (unsigned char *)audio_stream_data;
1808
1809 if (audio_stream_data == NULL || audio_frame_size == 0) {
1810 ALOGW("no buffer to get channel status, return default for compress");
1811 get_default_compressed_channel_status(channel_status);
1812 return ret;
1813 }
1814
1815 memset(channel_status,0,24);
1816 if(init_audio_parser(stream, audio_frame_size, codec_type) == -1)
1817 {
1818 ALOGE("init audio parser failed");
1819 return -1;
1820 }
1821 ret = get_channel_status(channel_status, codec_type);
1822 return ret;
1823
1824}
1825
Ashish Jain81eb2a82015-05-13 10:52:34 +05301826void get_lpcm_channel_status(uint32_t sampleRate,
1827 unsigned char *channel_status)
1828{
1829 int32_t status = 0;
Ashish Jain81eb2a82015-05-13 10:52:34 +05301830 memset(channel_status,0,24);
1831 /* block start bit in preamble bit 3 */
1832 channel_status[0] |= PROFESSIONAL;
1833 //LPCM OUT
1834 channel_status[0] &= ~NON_LPCM;
1835
1836 switch (sampleRate) {
1837 case 8000:
1838 case 11025:
1839 case 12000:
1840 case 16000:
1841 case 22050:
1842 channel_status[3] |= SR_NOTID;
Preetam Singh Ranawat61716b12015-12-14 11:55:24 +05301843 break;
Ashish Jain81eb2a82015-05-13 10:52:34 +05301844 case 24000:
1845 channel_status[3] |= SR_24000;
1846 break;
1847 case 32000:
1848 channel_status[3] |= SR_32000;
1849 break;
1850 case 44100:
1851 channel_status[3] |= SR_44100;
1852 break;
1853 case 48000:
1854 channel_status[3] |= SR_48000;
1855 break;
1856 case 88200:
1857 channel_status[3] |= SR_88200;
1858 break;
1859 case 96000:
1860 channel_status[3] |= SR_96000;
1861 break;
1862 case 176400:
1863 channel_status[3] |= SR_176400;
1864 break;
1865 case 192000:
1866 channel_status[3] |= SR_192000;
1867 break;
1868 default:
1869 ALOGV("Invalid sample_rate %u\n", sampleRate);
1870 status = -1;
1871 break;
1872 }
1873}
1874
1875void audio_utils_set_hdmi_channel_status(struct stream_out *out, char * buffer, size_t bytes)
1876{
1877 unsigned char channel_status[24]={0};
1878 struct snd_aes_iec958 iec958;
1879 const char *mixer_ctl_name = "IEC958 Playback PCM Stream";
1880 struct mixer_ctl *ctl;
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +05301881 ALOGV("%s: buffer %s bytes %zd", __func__, buffer, bytes);
Arun Mirpurie008ed22019-03-21 11:21:04 -07001882
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05301883 if (audio_extn_utils_is_dolby_format(out->format) &&
Ashish Jain81eb2a82015-05-13 10:52:34 +05301884 /*TODO:Extend code to support DTS passthrough*/
1885 /*set compressed channel status bits*/
Arun Mirpurie008ed22019-03-21 11:21:04 -07001886 audio_extn_passthru_is_passthrough_stream(out) &&
1887 audio_extn_is_hdmi_passthru_enabled()) {
Ashish Jain81eb2a82015-05-13 10:52:34 +05301888 get_compressed_channel_status(buffer, bytes, channel_status, AUDIO_PARSER_CODEC_AC3);
Arun Mirpurie008ed22019-03-21 11:21:04 -07001889 } else {
Ashish Jain81eb2a82015-05-13 10:52:34 +05301890 /*set channel status bit for LPCM*/
1891 get_lpcm_channel_status(out->sample_rate, channel_status);
1892 }
1893
1894 memcpy(iec958.status, channel_status,sizeof(iec958.status));
1895 ctl = mixer_get_ctl_by_name(out->dev->mixer, mixer_ctl_name);
1896 if (!ctl) {
1897 ALOGE("%s: Could not get ctl for mixer cmd - %s",
1898 __func__, mixer_ctl_name);
1899 return;
1900 }
1901 if (mixer_ctl_set_array(ctl, &iec958, sizeof(iec958)) < 0) {
1902 ALOGE("%s: Could not set channel status for ext HDMI ",
1903 __func__);
1904 return;
1905 }
1906
1907}
1908#endif
Manish Dewangan3ccdea52017-02-13 19:31:54 +05301909
1910int audio_extn_utils_get_avt_device_drift(
1911 struct audio_usecase *usecase,
1912 struct audio_avt_device_drift_param *drift_param)
1913{
1914 int ret = 0, count = 0;
1915 char avt_device_drift_mixer_ctl_name[MIXER_PATH_MAX_LENGTH] = {0};
Naresh Tanniru6160c712017-04-17 15:43:48 +05301916 const char *backend = NULL;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05301917 struct mixer_ctl *ctl = NULL;
1918 struct audio_avt_device_drift_stats drift_stats;
1919 struct audio_device *adev = NULL;
1920
1921 if (usecase != NULL && usecase->type == PCM_PLAYBACK) {
Naresh Tanniru6160c712017-04-17 15:43:48 +05301922 backend = platform_get_snd_device_backend_interface(usecase->out_snd_device);
1923 if (!backend) {
1924 ALOGE("%s: Unsupported device %d", __func__,
1925 usecase->stream.out->devices);
1926 ret = -EINVAL;
1927 goto done;
1928 }
1929 strlcpy(avt_device_drift_mixer_ctl_name,
1930 backend,
1931 MIXER_PATH_MAX_LENGTH);
1932
1933 count = strlen(backend);
1934 if (MIXER_PATH_MAX_LENGTH - count > 0) {
1935 strlcat(&avt_device_drift_mixer_ctl_name[count],
1936 " DRIFT",
1937 MIXER_PATH_MAX_LENGTH - count);
1938 } else {
1939 ret = -EINVAL;
1940 goto done;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05301941 }
1942 } else {
Naresh Tanniru7586e292017-04-13 10:38:13 +05301943 ALOGE("%s: Invalid usecase",__func__);
Manish Dewangan3ccdea52017-02-13 19:31:54 +05301944 ret = -EINVAL;
Naresh Tanniru6160c712017-04-17 15:43:48 +05301945 goto done;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05301946 }
1947
Naresh Tanniru6160c712017-04-17 15:43:48 +05301948 adev = usecase->stream.out->dev;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05301949 ctl = mixer_get_ctl_by_name(adev->mixer, avt_device_drift_mixer_ctl_name);
1950 if (!ctl) {
1951 ALOGE("%s: Could not get ctl for mixer cmd - %s",
1952 __func__, avt_device_drift_mixer_ctl_name);
1953
1954 ret = -EINVAL;
1955 goto done;
1956 }
1957
1958 ALOGV("%s: Getting AV Timer vs Device Drift mixer ctrl name %s", __func__,
1959 avt_device_drift_mixer_ctl_name);
1960
1961 mixer_ctl_update(ctl);
1962 count = mixer_ctl_get_num_values(ctl);
1963 if (count != sizeof(struct audio_avt_device_drift_stats)) {
1964 ALOGE("%s: mixer_ctl_get_num_values() invalid drift_stats data size",
1965 __func__);
1966
1967 ret = -EINVAL;
1968 goto done;
1969 }
1970
1971 ret = mixer_ctl_get_array(ctl, (void *)&drift_stats, count);
1972 if (ret != 0) {
1973 ALOGE("%s: mixer_ctl_get_array() failed to get drift_stats Params",
1974 __func__);
1975
1976 ret = -EINVAL;
1977 goto done;
1978 }
1979 memcpy(drift_param, &drift_stats.drift_param,
1980 sizeof(struct audio_avt_device_drift_param));
1981done:
1982 return ret;
1983}
Manish Dewangan07de2142017-02-27 19:27:20 +05301984
1985#ifdef SNDRV_COMPRESS_PATH_DELAY
1986int audio_extn_utils_compress_get_dsp_latency(struct stream_out *out)
1987{
1988 int ret = -EINVAL;
1989 struct snd_compr_metadata metadata;
1990 int delay_ms = COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
1991
Weiyin Jiangc49a3b52018-08-17 16:16:08 +08001992 /* override the latency for pcm offload use case */
1993 if ((out->flags & AUDIO_OUTPUT_FLAG_DIRECT) &&
1994 !(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)) {
1995 delay_ms = PCM_OFFLOAD_PLAYBACK_LATENCY;
1996 }
1997
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -07001998 if (property_get_bool("vendor.audio.playback.dsp.pathdelay", false)) {
Manish Dewangan07de2142017-02-27 19:27:20 +05301999 ALOGD("%s:: Quering DSP delay %d",__func__, __LINE__);
2000 if (!(is_offload_usecase(out->usecase))) {
2001 ALOGE("%s:: not supported for non offload session", __func__);
2002 goto exit;
2003 }
2004
2005 if (!out->compr) {
2006 ALOGD("%s:: Invalid compress handle,returning default dsp latency",
2007 __func__);
2008 goto exit;
2009 }
2010
2011 metadata.key = SNDRV_COMPRESS_PATH_DELAY;
2012 ret = compress_get_metadata(out->compr, &metadata);
2013 if(ret) {
2014 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2015 goto exit;
2016 }
2017 delay_ms = metadata.value[0] / 1000; /*convert to ms*/
2018 } else {
2019 ALOGD("%s:: Using Fix DSP delay",__func__);
2020 }
2021
2022exit:
2023 ALOGD("%s:: delay in ms is %d",__func__, delay_ms);
2024 return delay_ms;
2025}
2026#else
2027int audio_extn_utils_compress_get_dsp_latency(struct stream_out *out __unused)
2028{
2029 return COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
2030}
2031#endif
Manish Dewangan69426c82017-01-30 17:35:36 +05302032
2033#ifdef SNDRV_COMPRESS_RENDER_MODE
2034int audio_extn_utils_compress_set_render_mode(struct stream_out *out)
2035{
2036 struct snd_compr_metadata metadata;
2037 int ret = -EINVAL;
2038
2039 if (!(is_offload_usecase(out->usecase))) {
2040 ALOGE("%s:: not supported for non offload session", __func__);
2041 goto exit;
2042 }
2043
2044 if (!out->compr) {
2045 ALOGD("%s:: Invalid compress handle",
2046 __func__);
2047 goto exit;
2048 }
2049
2050 ALOGD("%s:: render mode %d", __func__, out->render_mode);
2051
2052 metadata.key = SNDRV_COMPRESS_RENDER_MODE;
2053 if (out->render_mode == RENDER_MODE_AUDIO_MASTER) {
2054 metadata.value[0] = SNDRV_COMPRESS_RENDER_MODE_AUDIO_MASTER;
2055 } else if (out->render_mode == RENDER_MODE_AUDIO_STC_MASTER) {
2056 metadata.value[0] = SNDRV_COMPRESS_RENDER_MODE_STC_MASTER;
2057 } else {
2058 ret = 0;
2059 goto exit;
2060 }
2061 ret = compress_set_metadata(out->compr, &metadata);
2062 if(ret) {
2063 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2064 }
2065exit:
2066 return ret;
2067}
2068#else
2069int audio_extn_utils_compress_set_render_mode(struct stream_out *out __unused)
2070{
2071 ALOGD("%s:: configuring render mode not supported", __func__);
2072 return 0;
2073}
2074#endif
Manish Dewangan58229382017-02-02 15:48:41 +05302075
2076#ifdef SNDRV_COMPRESS_CLK_REC_MODE
2077int audio_extn_utils_compress_set_clk_rec_mode(
2078 struct audio_usecase *usecase)
2079{
2080 struct snd_compr_metadata metadata;
2081 struct stream_out *out = NULL;
2082 int ret = -EINVAL;
2083
Naresh Tanniru7586e292017-04-13 10:38:13 +05302084 if (usecase == NULL || usecase->type != PCM_PLAYBACK) {
Manish Dewangan58229382017-02-02 15:48:41 +05302085 ALOGE("%s:: Invalid use case", __func__);
2086 goto exit;
2087 }
2088
2089 out = usecase->stream.out;
2090 if (!out) {
2091 ALOGE("%s:: invalid stream", __func__);
2092 goto exit;
2093 }
2094
2095 if (!is_offload_usecase(out->usecase)) {
2096 ALOGE("%s:: not supported for non offload session", __func__);
2097 goto exit;
2098 }
2099
2100 if (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER) {
2101 ALOGD("%s:: clk recovery is only supported in STC render mode",
2102 __func__);
2103 ret = 0;
2104 goto exit;
2105 }
2106
2107 if (!out->compr) {
2108 ALOGD("%s:: Invalid compress handle",
2109 __func__);
2110 goto exit;
2111 }
2112 metadata.key = SNDRV_COMPRESS_CLK_REC_MODE;
2113 switch(usecase->out_snd_device) {
2114 case SND_DEVICE_OUT_HDMI:
2115 case SND_DEVICE_OUT_SPEAKER_AND_HDMI:
2116 case SND_DEVICE_OUT_DISPLAY_PORT:
2117 case SND_DEVICE_OUT_SPEAKER_AND_DISPLAY_PORT:
2118 metadata.value[0] = SNDRV_COMPRESS_CLK_REC_MODE_NONE;
2119 break;
2120 default:
2121 metadata.value[0] = SNDRV_COMPRESS_CLK_REC_MODE_AUTO;
2122 break;
2123 }
2124
2125 ALOGD("%s:: clk recovery mode %d",__func__, metadata.value[0]);
2126
2127 ret = compress_set_metadata(out->compr, &metadata);
2128 if(ret) {
2129 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2130 }
2131
2132exit:
2133 return ret;
2134}
2135#else
2136int audio_extn_utils_compress_set_clk_rec_mode(
2137 struct audio_usecase *usecase __unused)
2138{
2139 ALOGD("%s:: configuring render mode not supported", __func__);
2140 return 0;
2141}
2142#endif
Manish Dewangan27346042017-03-01 12:56:12 +05302143
2144#ifdef SNDRV_COMPRESS_RENDER_WINDOW
2145int audio_extn_utils_compress_set_render_window(
2146 struct stream_out *out,
2147 struct audio_out_render_window_param *render_window)
2148{
2149 struct snd_compr_metadata metadata;
2150 int ret = -EINVAL;
2151
Manish Dewangan27346042017-03-01 12:56:12 +05302152 if(render_window == NULL) {
2153 ALOGE("%s:: Invalid render_window", __func__);
2154 goto exit;
2155 }
2156
Aniket Kumar Lata1e1d3662017-06-01 18:45:48 -07002157 ALOGD("%s:: render window start 0x%"PRIx64" end 0x%"PRIx64"",
2158 __func__,render_window->render_ws, render_window->render_we);
2159
Manish Dewangan27346042017-03-01 12:56:12 +05302160 if (!is_offload_usecase(out->usecase)) {
2161 ALOGE("%s:: not supported for non offload session", __func__);
2162 goto exit;
2163 }
2164
Naresh Tanniru6160c712017-04-17 15:43:48 +05302165 if ((out->render_mode != RENDER_MODE_AUDIO_MASTER) &&
2166 (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER)) {
Manish Dewangan27346042017-03-01 12:56:12 +05302167 ALOGD("%s:: only supported in timestamp mode, current "
2168 "render mode mode %d", __func__, out->render_mode);
2169 goto exit;
2170 }
2171
2172 if (!out->compr) {
2173 ALOGW("%s:: offload session not yet opened,"
2174 "render window will be configure later", __func__);
2175 /* store render window to reconfigure in start_output_stream() */
2176 goto exit;
2177 }
2178
2179 metadata.key = SNDRV_COMPRESS_RENDER_WINDOW;
2180 /*render window start value */
2181 metadata.value[0] = 0xFFFFFFFF & render_window->render_ws; /* lsb */
2182 metadata.value[1] = \
2183 (0xFFFFFFFF00000000 & render_window->render_ws) >> 32; /* msb*/
2184 /*render window end value */
2185 metadata.value[2] = 0xFFFFFFFF & render_window->render_we; /* lsb */
2186 metadata.value[3] = \
2187 (0xFFFFFFFF00000000 & render_window->render_we) >> 32; /* msb*/
2188
2189 ret = compress_set_metadata(out->compr, &metadata);
2190 if(ret) {
2191 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2192 }
2193
2194exit:
2195 return ret;
2196}
2197#else
2198int audio_extn_utils_compress_set_render_window(
2199 struct stream_out *out __unused,
2200 struct audio_out_render_window_param *render_window __unused)
2201{
2202 ALOGD("%s:: configuring render window not supported", __func__);
2203 return 0;
2204}
2205#endif
Manish Dewangan14956cc2017-02-14 18:54:42 +05302206
2207#ifdef SNDRV_COMPRESS_START_DELAY
2208int audio_extn_utils_compress_set_start_delay(
2209 struct stream_out *out,
2210 struct audio_out_start_delay_param *delay_param)
2211{
2212 struct snd_compr_metadata metadata;
2213 int ret = -EINVAL;
2214
2215 if(delay_param == NULL) {
2216 ALOGE("%s:: Invalid delay_param", __func__);
2217 goto exit;
2218 }
2219
2220 ALOGD("%s:: render start delay 0x%"PRIx64" ", __func__,
2221 delay_param->start_delay);
2222
2223 if (!is_offload_usecase(out->usecase)) {
2224 ALOGE("%s:: not supported for non offload session", __func__);
2225 goto exit;
2226 }
2227
Naresh Tanniru6160c712017-04-17 15:43:48 +05302228 if ((out->render_mode != RENDER_MODE_AUDIO_MASTER) &&
2229 (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER)) {
Manish Dewangan14956cc2017-02-14 18:54:42 +05302230 ALOGD("%s:: only supported in timestamp mode, current "
2231 "render mode mode %d", __func__, out->render_mode);
2232 goto exit;
2233 }
2234
2235 if (!out->compr) {
2236 ALOGW("%s:: offload session not yet opened,"
2237 "start delay will be configure later", __func__);
2238 goto exit;
2239 }
2240
2241 metadata.key = SNDRV_COMPRESS_START_DELAY;
2242 metadata.value[0] = 0xFFFFFFFF & delay_param->start_delay; /* lsb */
2243 metadata.value[1] = \
2244 (0xFFFFFFFF00000000 & delay_param->start_delay) >> 32; /* msb*/
2245
2246 ret = compress_set_metadata(out->compr, &metadata);
2247 if(ret) {
2248 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2249 }
2250
2251exit:
2252 return ret;
2253}
2254#else
2255int audio_extn_utils_compress_set_start_delay(
2256 struct stream_out *out __unused,
2257 struct audio_out_start_delay_param *delay_param __unused)
2258{
2259 ALOGD("%s:: configuring render window not supported", __func__);
2260 return 0;
2261}
2262#endif
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002263
Surendar Karka287348c2019-04-10 18:31:46 +05302264#ifdef SNDRV_COMPRESS_DSP_POSITION
2265int audio_extn_utils_compress_get_dsp_presentation_pos(struct stream_out *out,
2266 uint64_t *frames, struct timespec *timestamp, int32_t clock_id)
2267{
2268 int ret = -EINVAL;
2269 uint64_t *val = NULL;
2270 uint64_t time = 0;
2271 struct snd_compr_metadata metadata;
2272
2273 ALOGV("%s:: Quering DSP position with clock id %d",__func__, clock_id);
2274 metadata.key = SNDRV_COMPRESS_DSP_POSITION;
2275 metadata.value[0] = clock_id;
2276 ret = compress_get_metadata(out->compr, &metadata);
2277 if (ret) {
2278 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2279 ret = -errno;
2280 goto exit;
2281 }
2282 val = (uint64_t *)&metadata.value[1];
2283 *frames = *val;
2284 time = *(val + 1);
2285 timestamp->tv_sec = time / 1000000;
2286 timestamp->tv_nsec = (time % 1000000)*1000;
2287
2288exit:
2289 return ret;
2290}
2291#else
2292int audio_extn_utils_compress_get_dsp_presentation_pos(struct stream_out *out __unused,
2293 uint64_t *frames __unused, struct timespec *timestamp __unused,
2294 int32_t clock_id __unused)
2295{
2296 ALOGD("%s:: dsp presentation position not supported", __func__);
2297 return 0;
2298
2299}
2300#endif
2301
2302#ifdef SNDRV_PCM_IOCTL_DSP_POSITION
2303int audio_extn_utils_pcm_get_dsp_presentation_pos(struct stream_out *out,
2304 uint64_t *frames, struct timespec *timestamp, int32_t clock_id)
2305{
2306 int ret = -EINVAL;
2307 uint64_t time = 0;
2308 struct snd_pcm_prsnt_position prsnt_position;
2309
2310 ALOGV("%s:: Quering DSP position with clock id %d",__func__, clock_id);
2311 prsnt_position.clock_id = clock_id;
2312 ret = pcm_ioctl(out->pcm, SNDRV_PCM_IOCTL_DSP_POSITION, &prsnt_position);
2313 if (ret) {
2314 ALOGE("%s::error %d", __func__, ret);
2315 ret = -EIO;
2316 goto exit;
2317 }
2318
2319 *frames = prsnt_position.frames;
2320 time = prsnt_position.timestamp;
2321 timestamp->tv_sec = time / 1000000;
2322 timestamp->tv_nsec = (time % 1000000)*1000;
2323
2324exit:
2325 return ret;
2326}
2327#else
2328int audio_extn_utils_pcm_get_dsp_presentation_pos(struct stream_out *out __unused,
2329 uint64_t *frames __unused, struct timespec *timestamp __unused,
2330 int32_t clock_id __unused)
2331{
2332 ALOGD("%s:: dsp presentation position not supported", __func__);
2333 return 0;
2334
2335}
2336#endif
2337
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002338#define MAX_SND_CARD 8
Ashish Jain30ae8942017-11-05 17:21:23 +05302339#define RETRY_US 1000000
2340#define RETRY_NUMBER 40
Aalique Grahame22e49102018-12-18 14:23:57 -08002341#define PLATFORM_INFO_XML_PATH "audio_platform_info.xml"
2342#define PLATFORM_INFO_XML_BASE_STRING "audio_platform_info"
2343
2344#ifdef LINUX_ENABLED
2345static const char *kConfigLocationList[] =
2346 {"/etc"};
2347#else
2348static const char *kConfigLocationList[] =
2349 {"/vendor/etc"};
2350#endif
2351static const int kConfigLocationListSize =
2352 (sizeof(kConfigLocationList) / sizeof(kConfigLocationList[0]));
2353
2354bool audio_extn_utils_resolve_config_file(char file_name[MIXER_PATH_MAX_LENGTH])
2355{
2356 char full_config_path[MIXER_PATH_MAX_LENGTH];
2357 for (int i = 0; i < kConfigLocationListSize; i++) {
2358 snprintf(full_config_path,
2359 MIXER_PATH_MAX_LENGTH,
2360 "%s/%s",
2361 kConfigLocationList[i],
2362 file_name);
2363 if (F_OK == access(full_config_path, 0)) {
Weiyin Jiang2995f662019-04-17 14:25:12 +08002364 strlcpy(file_name, full_config_path, MIXER_PATH_MAX_LENGTH);
Aalique Grahame22e49102018-12-18 14:23:57 -08002365 return true;
2366 }
2367 }
2368 return false;
2369}
2370
2371/* platform_info_file should be size 'MIXER_PATH_MAX_LENGTH' */
2372int audio_extn_utils_get_platform_info(const char* snd_card_name, char* platform_info_file)
2373{
2374 if (NULL == snd_card_name) {
2375 return -1;
2376 }
2377
2378 struct snd_card_split *snd_split_handle = NULL;
2379 int ret = 0;
2380 audio_extn_set_snd_card_split(snd_card_name);
2381 snd_split_handle = audio_extn_get_snd_card_split();
2382
2383 snprintf(platform_info_file, MIXER_PATH_MAX_LENGTH, "%s_%s_%s.xml",
2384 PLATFORM_INFO_XML_BASE_STRING, snd_split_handle->snd_card,
2385 snd_split_handle->form_factor);
2386
2387 if (!audio_extn_utils_resolve_config_file(platform_info_file)) {
2388 memset(platform_info_file, 0, MIXER_PATH_MAX_LENGTH);
2389 snprintf(platform_info_file, MIXER_PATH_MAX_LENGTH, "%s_%s.xml",
2390 PLATFORM_INFO_XML_BASE_STRING, snd_split_handle->snd_card);
2391
2392 if (!audio_extn_utils_resolve_config_file(platform_info_file)) {
2393 memset(platform_info_file, 0, MIXER_PATH_MAX_LENGTH);
2394 strlcpy(platform_info_file, PLATFORM_INFO_XML_PATH, MIXER_PATH_MAX_LENGTH);
2395 ret = audio_extn_utils_resolve_config_file(platform_info_file) ? 0 : -1;
2396 }
2397 }
2398
2399 return ret;
2400}
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002401
2402int audio_extn_utils_get_snd_card_num()
2403{
Soumya Managoli9fee7c62018-04-06 16:21:50 +05302404 int snd_card_num = 0;
2405 struct mixer *mixer = NULL;
2406
2407 snd_card_num = audio_extn_utils_open_snd_mixer(&mixer);
2408 if (mixer)
2409 mixer_close(mixer);
2410 return snd_card_num;
2411}
2412
2413int audio_extn_utils_open_snd_mixer(struct mixer **mixer_handle)
2414{
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002415
2416 void *hw_info = NULL;
2417 struct mixer *mixer = NULL;
2418 int retry_num = 0;
mpangfaa7bae2019-01-15 16:38:13 +08002419 int snd_card_num = 0;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002420 char* snd_card_name = NULL;
mpangfaa7bae2019-01-15 16:38:13 +08002421 int snd_card_detection_info[MAX_SND_CARD] = {0};
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002422
Soumya Managoli9fee7c62018-04-06 16:21:50 +05302423 if (!mixer_handle) {
2424 ALOGE("invalid mixer handle");
2425 return -1;
2426 }
2427 *mixer_handle = NULL;
Ashish Jain30ae8942017-11-05 17:21:23 +05302428 /*
mpangfaa7bae2019-01-15 16:38:13 +08002429 * Try with all the sound cards ( 0 to 7 ) and if none of them were detected
Ashish Jain30ae8942017-11-05 17:21:23 +05302430 * sleep for 1 sec and try detections with sound card 0 again.
2431 * If sound card gets detected, check if it is relevant, if not check with the
2432 * other sound cards. To ensure that the irrelevant sound card is not check again,
2433 * we maintain it in min_snd_card_num.
2434 */
2435 while (retry_num < RETRY_NUMBER) {
mpangfaa7bae2019-01-15 16:38:13 +08002436 snd_card_num = 0;
2437 while (snd_card_num < MAX_SND_CARD) {
2438 if (snd_card_detection_info[snd_card_num] == 0) {
2439 mixer = mixer_open(snd_card_num);
2440 if (!mixer)
2441 snd_card_num++;
2442 else
2443 break;
2444 } else
2445 snd_card_num++;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002446 }
2447
2448 if (!mixer) {
Ashish Jain30ae8942017-11-05 17:21:23 +05302449 usleep(RETRY_US);
Ashish Jain30ae8942017-11-05 17:21:23 +05302450 retry_num++;
mpangfaa7bae2019-01-15 16:38:13 +08002451 ALOGD("%s: retry, retry_num %d", __func__, retry_num);
Ashish Jain30ae8942017-11-05 17:21:23 +05302452 continue;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002453 }
2454
2455 snd_card_name = strdup(mixer_get_name(mixer));
2456 if (!snd_card_name) {
2457 ALOGE("failed to allocate memory for snd_card_name\n");
2458 mixer_close(mixer);
2459 return -1;
2460 }
2461 ALOGD("%s: snd_card_name: %s", __func__, snd_card_name);
mpangfaa7bae2019-01-15 16:38:13 +08002462 snd_card_detection_info[snd_card_num] = 1;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002463 hw_info = hw_info_init(snd_card_name);
2464 if (hw_info) {
2465 ALOGD("%s: Opened sound card:%d", __func__, snd_card_num);
2466 break;
2467 }
mpangfaa7bae2019-01-15 16:38:13 +08002468 ALOGE("%s: Failed to init hardware info, snd_card_num:%d", __func__, snd_card_num);
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002469
Dhananjay Kumara7e27832017-07-11 15:40:39 +05302470 free(snd_card_name);
2471 snd_card_name = NULL;
2472
2473 mixer_close(mixer);
2474 mixer = NULL;
2475 }
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002476 if (snd_card_name)
2477 free(snd_card_name);
Ashish Jain30ae8942017-11-05 17:21:23 +05302478
Dhananjay Kumara7e27832017-07-11 15:40:39 +05302479 if (hw_info)
2480 hw_info_deinit(hw_info);
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002481
mpangfaa7bae2019-01-15 16:38:13 +08002482 if (retry_num >= RETRY_NUMBER) {
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002483 ALOGE("%s: Unable to find correct sound card, aborting.", __func__);
2484 return -1;
2485 }
2486
Soumya Managoli9fee7c62018-04-06 16:21:50 +05302487 if (mixer)
2488 *mixer_handle = mixer;
2489
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002490 return snd_card_num;
2491}
Naresh Tanniru6160c712017-04-17 15:43:48 +05302492
Soumya Managoli9fee7c62018-04-06 16:21:50 +05302493void audio_extn_utils_close_snd_mixer(struct mixer *mixer)
2494{
2495 if (mixer)
2496 mixer_close(mixer);
2497}
2498
Naresh Tanniru6160c712017-04-17 15:43:48 +05302499#ifdef SNDRV_COMPRESS_ENABLE_ADJUST_SESSION_CLOCK
2500int audio_extn_utils_compress_enable_drift_correction(
2501 struct stream_out *out,
2502 struct audio_out_enable_drift_correction *drift)
2503{
2504 struct snd_compr_metadata metadata;
2505 int ret = -EINVAL;
2506
2507 if(drift == NULL) {
2508 ALOGE("%s:: Invalid param", __func__);
2509 goto exit;
2510 }
2511
2512 ALOGD("%s:: drift enable %d", __func__,drift->enable);
2513
2514 if (!is_offload_usecase(out->usecase)) {
2515 ALOGE("%s:: not supported for non offload session", __func__);
2516 goto exit;
2517 }
2518
2519 if (!out->compr) {
2520 ALOGW("%s:: offload session not yet opened,"
2521 "start delay will be configure later", __func__);
2522 goto exit;
2523 }
2524
2525 metadata.key = SNDRV_COMPRESS_ENABLE_ADJUST_SESSION_CLOCK;
2526 metadata.value[0] = drift->enable;
2527 out->drift_correction_enabled = drift->enable;
2528
2529 ret = compress_set_metadata(out->compr, &metadata);
2530 if(ret) {
2531 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2532 out->drift_correction_enabled = false;
2533 }
2534
2535exit:
2536 return ret;
2537}
2538#else
2539int audio_extn_utils_compress_enable_drift_correction(
2540 struct stream_out *out __unused,
2541 struct audio_out_enable_drift_correction *drift __unused)
2542{
2543 ALOGD("%s:: configuring drift enablement not supported", __func__);
2544 return 0;
2545}
2546#endif
2547
2548#ifdef SNDRV_COMPRESS_ADJUST_SESSION_CLOCK
2549int audio_extn_utils_compress_correct_drift(
2550 struct stream_out *out,
2551 struct audio_out_correct_drift *drift_param)
2552{
2553 struct snd_compr_metadata metadata;
2554 int ret = -EINVAL;
2555
2556 if (drift_param == NULL) {
2557 ALOGE("%s:: Invalid drift_param", __func__);
2558 goto exit;
2559 }
2560
2561 ALOGD("%s:: adjust time 0x%"PRIx64" ", __func__,
2562 drift_param->adjust_time);
2563
2564 if (!is_offload_usecase(out->usecase)) {
2565 ALOGE("%s:: not supported for non offload session", __func__);
2566 goto exit;
2567 }
2568
2569 if (!out->compr) {
2570 ALOGW("%s:: offload session not yet opened", __func__);
2571 goto exit;
2572 }
2573
2574 if (!out->drift_correction_enabled) {
2575 ALOGE("%s:: drift correction not enabled", __func__);
2576 goto exit;
2577 }
2578
2579 metadata.key = SNDRV_COMPRESS_ADJUST_SESSION_CLOCK;
2580 metadata.value[0] = 0xFFFFFFFF & drift_param->adjust_time; /* lsb */
2581 metadata.value[1] = \
2582 (0xFFFFFFFF00000000 & drift_param->adjust_time) >> 32; /* msb*/
2583
2584 ret = compress_set_metadata(out->compr, &metadata);
2585 if(ret)
2586 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2587exit:
2588 return ret;
2589}
2590#else
2591int audio_extn_utils_compress_correct_drift(
2592 struct stream_out *out __unused,
2593 struct audio_out_correct_drift *drift_param __unused)
2594{
2595 ALOGD("%s:: setting adjust clock not supported", __func__);
2596 return 0;
2597}
2598#endif
Naresh Tanniru29bce4e2017-04-27 17:54:30 +05302599
2600int audio_extn_utils_set_channel_map(
2601 struct stream_out *out,
2602 struct audio_out_channel_map_param *channel_map_param)
2603{
2604 int ret = -EINVAL, i = 0;
2605 int channels = audio_channel_count_from_out_mask(out->channel_mask);
2606
2607 if (channel_map_param == NULL) {
2608 ALOGE("%s:: Invalid channel_map", __func__);
2609 goto exit;
2610 }
2611
2612 if (channel_map_param->channels != channels) {
2613 ALOGE("%s:: Channels(%d) does not match stream channels(%d)",
2614 __func__, channel_map_param->channels, channels);
2615 goto exit;
2616 }
2617
2618 for ( i = 0; i < channels; i++) {
2619 ALOGV("%s:: channel_map[%d]- %d", __func__, i, channel_map_param->channel_map[i]);
2620 out->channel_map_param.channel_map[i] = channel_map_param->channel_map[i];
2621 }
2622 ret = 0;
2623exit:
2624 return ret;
2625}
Varun Balaraje49253e2017-07-06 19:48:56 +05302626
2627int audio_extn_utils_set_pan_scale_params(
2628 struct stream_out *out,
2629 struct mix_matrix_params *mm_params)
2630{
2631 int ret = -EINVAL, i = 0, j = 0;
2632
Varun Balaraj003ee752017-08-07 17:54:55 +05302633 if (mm_params == NULL || out == NULL) {
2634 ALOGE("%s:: Invalid mix matrix or out param", __func__);
Varun Balaraje49253e2017-07-06 19:48:56 +05302635 goto exit;
2636 }
2637
2638 if (mm_params->num_output_channels > MAX_CHANNELS_SUPPORTED ||
2639 mm_params->num_output_channels <= 0 ||
2640 mm_params->num_input_channels > MAX_CHANNELS_SUPPORTED ||
2641 mm_params->num_input_channels <= 0)
2642 goto exit;
2643
2644 out->pan_scale_params.num_output_channels = mm_params->num_output_channels;
2645 out->pan_scale_params.num_input_channels = mm_params->num_input_channels;
2646 out->pan_scale_params.has_output_channel_map =
2647 mm_params->has_output_channel_map;
2648 for (i = 0; i < mm_params->num_output_channels; i++)
2649 out->pan_scale_params.output_channel_map[i] =
2650 mm_params->output_channel_map[i];
2651
2652 out->pan_scale_params.has_input_channel_map =
2653 mm_params->has_input_channel_map;
2654 for (i = 0; i < mm_params->num_input_channels; i++)
2655 out->pan_scale_params.input_channel_map[i] =
2656 mm_params->input_channel_map[i];
2657
2658 out->pan_scale_params.has_mixer_coeffs = mm_params->has_mixer_coeffs;
2659 for (i = 0; i < mm_params->num_output_channels; i++)
2660 for (j = 0; j < mm_params->num_input_channels; j++) {
2661 //Convert the channel coefficient gains in Q14 format
2662 out->pan_scale_params.mixer_coeffs[i][j] =
2663 mm_params->mixer_coeffs[i][j] * (2 << 13);
2664 }
2665
2666 ret = platform_set_stream_pan_scale_params(out->dev->platform,
2667 out->pcm_device_id,
2668 out->pan_scale_params);
2669
2670exit:
2671 return ret;
2672}
2673
2674int audio_extn_utils_set_downmix_params(
2675 struct stream_out *out,
2676 struct mix_matrix_params *mm_params)
2677{
2678 int ret = -EINVAL, i = 0, j = 0;
2679 struct audio_usecase *usecase = NULL;
2680
Aniket Kumar Lataf9f246e2017-09-15 15:20:16 -07002681 if (mm_params == NULL || out == NULL) {
Varun Balaraj003ee752017-08-07 17:54:55 +05302682 ALOGE("%s:: Invalid mix matrix or out param", __func__);
Varun Balaraje49253e2017-07-06 19:48:56 +05302683 goto exit;
2684 }
2685
2686 if (mm_params->num_output_channels > MAX_CHANNELS_SUPPORTED ||
2687 mm_params->num_output_channels <= 0 ||
2688 mm_params->num_input_channels > MAX_CHANNELS_SUPPORTED ||
2689 mm_params->num_input_channels <= 0)
2690 goto exit;
2691
2692 usecase = get_usecase_from_list(out->dev, out->usecase);
Varun Balaraj003ee752017-08-07 17:54:55 +05302693 if (!usecase) {
2694 ALOGE("%s: Get usecase list failed!", __func__);
Aniket Kumar Lataf9f246e2017-09-15 15:20:16 -07002695 goto exit;
2696 }
Varun Balaraje49253e2017-07-06 19:48:56 +05302697 out->downmix_params.num_output_channels = mm_params->num_output_channels;
2698 out->downmix_params.num_input_channels = mm_params->num_input_channels;
2699
2700 out->downmix_params.has_output_channel_map =
2701 mm_params->has_output_channel_map;
2702 for (i = 0; i < mm_params->num_output_channels; i++) {
2703 out->downmix_params.output_channel_map[i] =
2704 mm_params->output_channel_map[i];
2705 }
2706
2707 out->downmix_params.has_input_channel_map =
2708 mm_params->has_input_channel_map;
2709 for (i = 0; i < mm_params->num_input_channels; i++)
2710 out->downmix_params.input_channel_map[i] =
2711 mm_params->input_channel_map[i];
2712
2713 out->downmix_params.has_mixer_coeffs = mm_params->has_mixer_coeffs;
2714 for (i = 0; i < mm_params->num_output_channels; i++)
Nikhil Latukar2e6f6242017-08-15 13:37:07 +05302715 for (j = 0; j < mm_params->num_input_channels; j++) {
2716 //Convert the channel coefficient gains in Q14 format
Varun Balaraje49253e2017-07-06 19:48:56 +05302717 out->downmix_params.mixer_coeffs[i][j] =
Nikhil Latukar2e6f6242017-08-15 13:37:07 +05302718 mm_params->mixer_coeffs[i][j] * (2 << 13);
2719 }
Varun Balaraje49253e2017-07-06 19:48:56 +05302720
2721 ret = platform_set_stream_downmix_params(out->dev->platform,
2722 out->pcm_device_id,
2723 usecase->out_snd_device,
2724 out->downmix_params);
2725
2726exit:
2727 return ret;
2728}
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05302729
2730bool audio_extn_utils_is_dolby_format(audio_format_t format)
2731{
2732 if (format == AUDIO_FORMAT_AC3 ||
2733 format == AUDIO_FORMAT_E_AC3 ||
2734 format == AUDIO_FORMAT_E_AC3_JOC)
2735 return true;
2736 else
2737 return false;
2738}
2739
`Deeraj Soman676c2702017-09-18 19:25:53 +05302740int audio_extn_utils_get_bit_width_from_string(const char *id_string)
2741{
2742 int i;
2743 const mixer_config_lookup mixer_bitwidth_config[] = {{"S24_3LE", 24},
2744 {"S32_LE", 32},
2745 {"S24_LE", 24},
2746 {"S16_LE", 16}};
2747 int num_configs = sizeof(mixer_bitwidth_config) / sizeof(mixer_bitwidth_config[0]);
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05302748
`Deeraj Soman676c2702017-09-18 19:25:53 +05302749 for (i = 0; i < num_configs; i++) {
2750 if (!strcmp(id_string, mixer_bitwidth_config[i].id_string))
2751 return mixer_bitwidth_config[i].value;
2752 }
2753
2754 return -EINVAL;
2755}
2756
2757int audio_extn_utils_get_sample_rate_from_string(const char *id_string)
2758{
2759 int i;
2760 const mixer_config_lookup mixer_samplerate_config[] = {{"KHZ_32", 32000},
2761 {"KHZ_48", 48000},
2762 {"KHZ_96", 96000},
2763 {"KHZ_144", 144000},
2764 {"KHZ_192", 192000},
2765 {"KHZ_384", 384000},
2766 {"KHZ_44P1", 44100},
2767 {"KHZ_88P2", 88200},
2768 {"KHZ_176P4", 176400},
2769 {"KHZ_352P8", 352800}};
2770 int num_configs = sizeof(mixer_samplerate_config) / sizeof(mixer_samplerate_config[0]);
2771
2772 for (i = 0; i < num_configs; i++) {
2773 if (!strcmp(id_string, mixer_samplerate_config[i].id_string))
2774 return mixer_samplerate_config[i].value;
2775 }
2776
2777 return -EINVAL;
2778}
2779
2780int audio_extn_utils_get_channels_from_string(const char *id_string)
2781{
2782 int i;
2783 const mixer_config_lookup mixer_channels_config[] = {{"One", 1},
2784 {"Two", 2},
2785 {"Three",3},
2786 {"Four", 4},
2787 {"Five", 5},
2788 {"Six", 6},
2789 {"Seven", 7},
2790 {"Eight", 8}};
2791 int num_configs = sizeof(mixer_channels_config) / sizeof(mixer_channels_config[0]);
2792
2793 for (i = 0; i < num_configs; i++) {
2794 if (!strcmp(id_string, mixer_channels_config[i].id_string))
2795 return mixer_channels_config[i].value;
2796 }
2797
2798 return -EINVAL;
2799}
Surendar karka30569792018-05-08 12:02:21 +05302800
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08002801void audio_extn_utils_release_snd_device(snd_device_t snd_device)
2802{
2803 audio_extn_dev_arbi_release(snd_device);
2804 audio_extn_sound_trigger_update_device_status(snd_device,
2805 ST_EVENT_SND_DEVICE_FREE);
2806 audio_extn_listen_update_device_status(snd_device,
2807 LISTEN_EVENT_SND_DEVICE_FREE);
2808}
2809
Surendar karka30569792018-05-08 12:02:21 +05302810int audio_extn_utils_get_license_params
2811(
2812const struct audio_device *adev,
2813struct audio_license_params *license_params
2814)
2815{
2816 if(!license_params)
2817 return -EINVAL;
2818 return platform_get_license_by_product(adev->platform, (const char*)license_params->product, &license_params->key, license_params->license);
2819}
2820
Aalique Grahame22e49102018-12-18 14:23:57 -08002821int audio_extn_utils_send_app_type_gain(struct audio_device *adev,
2822 int app_type,
2823 int *gain)
2824{
2825 int gain_cfg[4];
2826 const char *mixer_ctl_name = "App Type Gain";
2827 struct mixer_ctl *ctl;
2828 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
2829 if (!ctl) {
2830 ALOGE("%s: Could not get volume ctl mixer %s", __func__,
2831 mixer_ctl_name);
2832 return -EINVAL;
2833 }
2834 gain_cfg[0] = 0;
2835 gain_cfg[1] = app_type;
2836 gain_cfg[2] = gain[0];
2837 gain_cfg[3] = gain[1];
2838 ALOGV("%s app_type %d l(%d) r(%d)", __func__, app_type, gain[0], gain[1]);
2839 return mixer_ctl_set_array(ctl, gain_cfg,
2840 sizeof(gain_cfg)/sizeof(gain_cfg[0]));
2841}
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -07002842
2843int audio_extn_utils_is_vendor_enhanced_fwk()
2844{
2845 static int is_running_with_enhanced_fwk = -EINVAL;
2846
2847 if (is_running_with_enhanced_fwk == -EINVAL) {
2848 vndk_fwk_lib_handle = dlopen(VNDK_FWK_LIB_PATH, RTLD_NOW);
2849 if (vndk_fwk_lib_handle != NULL) {
2850 vndk_fwk_isVendorEnhancedFwk = (vndk_fwk_isVendorEnhancedFwk_t)
2851 dlsym(vndk_fwk_lib_handle, "isRunningWithVendorEnhancedFramework");
2852 if (vndk_fwk_isVendorEnhancedFwk == NULL) {
2853 ALOGW("%s: dlsym failed, defaulting to enhanced_fwk configuration",
2854 __func__);
2855 is_running_with_enhanced_fwk = 1;
2856 } else {
2857 is_running_with_enhanced_fwk = vndk_fwk_isVendorEnhancedFwk();
2858 }
2859 dlclose(vndk_fwk_lib_handle);
2860 vndk_fwk_lib_handle = NULL;
2861 } else {
2862 ALOGW("%s: VNDK_FWK_LIB not found, setting stock configuration", __func__);
2863 is_running_with_enhanced_fwk = 0;
2864 }
2865 ALOGV("%s: vndk_fwk_isVendorEnhancedFwk=%d", __func__, is_running_with_enhanced_fwk);
2866 }
2867
2868 return is_running_with_enhanced_fwk;
2869}