blob: af89a8aaee1e944e782f85d98d564dc7c324b174 [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>
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070033
Manish Dewangan07de2142017-02-27 19:27:20 +053034
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070035#include "audio_hw.h"
36#include "platform.h"
37#include "platform_api.h"
38#include "audio_extn.h"
Narsinga Rao Chella212e2542014-11-17 19:57:04 -080039#include "voice.h"
Manish Dewangan07de2142017-02-27 19:27:20 +053040#include <sound/compress_params.h>
41#include <sound/compress_offload.h>
42#include <tinycompress/tinycompress.h>
Revathi Uddaraju1eac8b02017-05-18 17:13:33 +053043
44#ifdef DYNAMIC_LOG_ENABLED
45#include <log_xml_parser.h>
46#define LOG_MASK HAL_MOD_FILE_UTILS
47#include <log_utils.h>
48#endif
49
Ashish Jain81eb2a82015-05-13 10:52:34 +053050#ifdef AUDIO_EXTERNAL_HDMI_ENABLED
Ashish Jain81eb2a82015-05-13 10:52:34 +053051#include "audio_parsers.h"
52#endif
Ashish Jain81eb2a82015-05-13 10:52:34 +053053
Yamit Mehtaa0d653a2016-11-25 20:33:25 +053054#ifdef LINUX_ENABLED
55#define AUDIO_OUTPUT_POLICY_VENDOR_CONFIG_FILE "/etc/audio_output_policy.conf"
Dhananjay Kumard6d32152016-10-13 16:11:03 +053056#define AUDIO_IO_POLICY_VENDOR_CONFIG_FILE "/etc/audio_io_policy.conf"
Yamit Mehtaa0d653a2016-11-25 20:33:25 +053057#else
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070058#define AUDIO_OUTPUT_POLICY_VENDOR_CONFIG_FILE "/vendor/etc/audio_output_policy.conf"
Dhananjay Kumard6d32152016-10-13 16:11:03 +053059#define AUDIO_IO_POLICY_VENDOR_CONFIG_FILE "/vendor/etc/audio_io_policy.conf"
Yamit Mehtaa0d653a2016-11-25 20:33:25 +053060#endif
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070061
62#define OUTPUTS_TAG "outputs"
Dhananjay Kumard6d32152016-10-13 16:11:03 +053063#define INPUTS_TAG "inputs"
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070064
65#define DYNAMIC_VALUE_TAG "dynamic"
66#define FLAGS_TAG "flags"
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +053067#define PROFILES_TAG "profile"
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070068#define FORMATS_TAG "formats"
69#define SAMPLING_RATES_TAG "sampling_rates"
70#define BIT_WIDTH_TAG "bit_width"
71#define APP_TYPE_TAG "app_type"
72
73#define STRING_TO_ENUM(string) { #string, string }
74#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
75
Ben Rombergera04fabc2014-11-14 12:16:03 -080076#define BASE_TABLE_SIZE 64
77#define MAX_BASEINDEX_LEN 256
78
Ben Romberger1aaaf862017-04-06 17:49:46 -070079#ifndef SND_AUDIOCODEC_TRUEHD
80#define SND_AUDIOCODEC_TRUEHD 0x00000023
81#endif
82
Vikram Panduranga93f080e2017-06-07 18:16:14 -070083#define APP_TYPE_VOIP_AUDIO 0x1113A
84
Ashish Jain81eb2a82015-05-13 10:52:34 +053085#ifdef AUDIO_EXTERNAL_HDMI_ENABLED
86#define PROFESSIONAL (1<<0) /* 0 = consumer, 1 = professional */
87#define NON_LPCM (1<<1) /* 0 = audio, 1 = non-audio */
88#define SR_44100 (0<<0) /* 44.1kHz */
89#define SR_NOTID (1<<0) /* non indicated */
90#define SR_48000 (2<<0) /* 48kHz */
91#define SR_32000 (3<<0) /* 32kHz */
92#define SR_22050 (4<<0) /* 22.05kHz */
93#define SR_24000 (6<<0) /* 24kHz */
94#define SR_88200 (8<<0) /* 88.2kHz */
95#define SR_96000 (10<<0) /* 96kHz */
96#define SR_176400 (12<<0) /* 176.4kHz */
97#define SR_192000 (14<<0) /* 192kHz */
98
99#endif
Manish Dewangan07de2142017-02-27 19:27:20 +0530100
101/* ToDo: Check and update a proper value in msec */
Weiyin Jiangc49a3b52018-08-17 16:16:08 +0800102#define COMPRESS_OFFLOAD_PLAYBACK_LATENCY 50
103#define PCM_OFFLOAD_PLAYBACK_DSP_PATHDELAY 62
104#define PCM_OFFLOAD_PLAYBACK_LATENCY \
105 (PCM_OFFLOAD_BUFFER_DURATION + PCM_OFFLOAD_PLAYBACK_DSP_PATHDELAY)
Manish Dewangan07de2142017-02-27 19:27:20 +0530106
Varun Balaraje49253e2017-07-06 19:48:56 +0530107#ifndef MAX_CHANNELS_SUPPORTED
108#define MAX_CHANNELS_SUPPORTED 8
109#endif
110
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -0700111#ifdef __LP64__
112#define VNDK_FWK_LIB_PATH "/vendor/lib64/libqti_vndfwk_detect.so"
113#else
114#define VNDK_FWK_LIB_PATH "/vendor/lib/libqti_vndfwk_detect.so"
115#endif
116
117static void *vndk_fwk_lib_handle = NULL;
118typedef int (*vndk_fwk_isVendorEnhancedFwk_t)();
119static vndk_fwk_isVendorEnhancedFwk_t vndk_fwk_isVendorEnhancedFwk;
120
`Deeraj Soman676c2702017-09-18 19:25:53 +0530121typedef struct {
122 const char *id_string;
123 const int value;
124} mixer_config_lookup;
125
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700126struct string_to_enum {
127 const char *name;
128 uint32_t value;
129};
130
131const struct string_to_enum s_flag_name_to_enum_table[] = {
132 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_DIRECT),
133 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_PRIMARY),
134 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_FAST),
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800135 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_RAW),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700136 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_DEEP_BUFFER),
137 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD),
138 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_NON_BLOCKING),
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700139 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_HW_AV_SYNC),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700140 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_INCALL_MUSIC),
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700141 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH),
Naresh Tanniruee3499a2017-01-05 14:05:35 +0530142 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_TIMESTAMP),
Vikram Panduranga93f080e2017-06-07 18:16:14 -0700143 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_VOIP_RX),
Ben Romberger6c4d3812017-06-13 17:46:45 -0700144 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_BD),
Varun Balaraje49253e2017-07-06 19:48:56 +0530145 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_INTERACTIVE),
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530146 STRING_TO_ENUM(AUDIO_INPUT_FLAG_NONE),
147 STRING_TO_ENUM(AUDIO_INPUT_FLAG_FAST),
148 STRING_TO_ENUM(AUDIO_INPUT_FLAG_HW_HOTWORD),
149 STRING_TO_ENUM(AUDIO_INPUT_FLAG_RAW),
150 STRING_TO_ENUM(AUDIO_INPUT_FLAG_SYNC),
Dhananjay Kumaree4d2002016-10-25 18:02:58 +0530151 STRING_TO_ENUM(AUDIO_INPUT_FLAG_TIMESTAMP),
Dhananjay Kumar704ce6f2017-09-28 22:08:00 +0530152 STRING_TO_ENUM(AUDIO_INPUT_FLAG_COMPRESS),
Ralf Herzaec80262018-07-03 07:08:49 +0200153 STRING_TO_ENUM(AUDIO_INPUT_FLAG_PASSTHROUGH),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700154};
155
156const struct string_to_enum s_format_name_to_enum_table[] = {
Ashish Jain83a6cc22016-06-28 14:34:17 +0530157 STRING_TO_ENUM(AUDIO_FORMAT_PCM_8_BIT),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700158 STRING_TO_ENUM(AUDIO_FORMAT_PCM_16_BIT),
Ashish Jain5106d362016-05-11 19:23:33 +0530159 STRING_TO_ENUM(AUDIO_FORMAT_PCM_24_BIT_PACKED),
160 STRING_TO_ENUM(AUDIO_FORMAT_PCM_8_24_BIT),
Ashish Jainf1eaa582016-05-23 20:54:24 +0530161 STRING_TO_ENUM(AUDIO_FORMAT_PCM_32_BIT),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700162 STRING_TO_ENUM(AUDIO_FORMAT_MP3),
163 STRING_TO_ENUM(AUDIO_FORMAT_AAC),
164 STRING_TO_ENUM(AUDIO_FORMAT_VORBIS),
Mingming Yinae3530f2014-07-03 16:50:18 -0700165 STRING_TO_ENUM(AUDIO_FORMAT_AMR_NB),
166 STRING_TO_ENUM(AUDIO_FORMAT_AMR_WB),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700167 STRING_TO_ENUM(AUDIO_FORMAT_AC3),
Mingming Yinae3530f2014-07-03 16:50:18 -0700168 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700169 STRING_TO_ENUM(AUDIO_FORMAT_DTS),
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530170 STRING_TO_ENUM(AUDIO_FORMAT_DTS_HD),
Ben Romberger1aaaf862017-04-06 17:49:46 -0700171 STRING_TO_ENUM(AUDIO_FORMAT_DOLBY_TRUEHD),
Naresh Tanniru928f0862017-04-07 16:44:23 -0700172 STRING_TO_ENUM(AUDIO_FORMAT_IEC61937),
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530173 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3_JOC),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700174 STRING_TO_ENUM(AUDIO_FORMAT_WMA),
175 STRING_TO_ENUM(AUDIO_FORMAT_WMA_PRO),
176 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADIF),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700177 STRING_TO_ENUM(AUDIO_FORMAT_AMR_WB_PLUS),
178 STRING_TO_ENUM(AUDIO_FORMAT_EVRC),
179 STRING_TO_ENUM(AUDIO_FORMAT_EVRCB),
180 STRING_TO_ENUM(AUDIO_FORMAT_EVRCWB),
181 STRING_TO_ENUM(AUDIO_FORMAT_QCELP),
182 STRING_TO_ENUM(AUDIO_FORMAT_MP2),
183 STRING_TO_ENUM(AUDIO_FORMAT_EVRCNW),
Amit Shekhar6f461b12014-08-01 14:52:58 -0700184 STRING_TO_ENUM(AUDIO_FORMAT_FLAC),
Satya Krishna Pindiproli70471602015-04-24 19:12:43 +0530185 STRING_TO_ENUM(AUDIO_FORMAT_ALAC),
186 STRING_TO_ENUM(AUDIO_FORMAT_APE),
Alexy Josephcd8eaed2014-12-11 12:46:53 -0800187 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LC),
188 STRING_TO_ENUM(AUDIO_FORMAT_AAC_HE_V1),
189 STRING_TO_ENUM(AUDIO_FORMAT_AAC_HE_V2),
Manish Dewangana6fc5442015-08-24 20:30:31 +0530190 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS),
Ashish Jaine513a872015-11-19 17:00:56 +0530191 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_LC),
192 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_HE_V1),
193 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_HE_V2),
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +0530194 STRING_TO_ENUM(AUDIO_FORMAT_DSD),
Arun Kumar Dasari3b174182016-12-27 13:01:14 +0530195 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM),
196 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM_LC),
197 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM_HE_V1),
198 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM_HE_V2),
Dhanalakshmi Siddani18737932016-11-29 17:33:17 +0530199 STRING_TO_ENUM(AUDIO_FORMAT_APTX),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700200};
201
Manish Dewangan3ccdea52017-02-13 19:31:54 +0530202/* payload structure avt_device drift query */
203struct audio_avt_device_drift_stats {
204 uint32_t minor_version;
Manish Dewangan338c50a2017-09-12 15:22:03 +0530205
Manish Dewangan3ccdea52017-02-13 19:31:54 +0530206 /* Indicates the device interface direction as either
207 * source (Tx) or sink (Rx).
208 */
209 uint16_t device_direction;
Manish Dewangan338c50a2017-09-12 15:22:03 +0530210
211 /* Reference timer for drift accumulation and time stamp information.
212 * currently it only support AFE_REF_TIMER_TYPE_AVTIMER
213 */
214 uint16_t reference_timer;
Manish Dewangan3ccdea52017-02-13 19:31:54 +0530215 struct audio_avt_device_drift_param drift_param;
Manish Dewangan338c50a2017-09-12 15:22:03 +0530216} __attribute__((packed));
Manish Dewangan3ccdea52017-02-13 19:31:54 +0530217
Ben Rombergera04fabc2014-11-14 12:16:03 -0800218static char bTable[BASE_TABLE_SIZE] = {
219 'A','B','C','D','E','F','G','H','I','J','K','L',
220 'M','N','O','P','Q','R','S','T','U','V','W','X',
221 'Y','Z','a','b','c','d','e','f','g','h','i','j',
222 'k','l','m','n','o','p','q','r','s','t','u','v',
223 'w','x','y','z','0','1','2','3','4','5','6','7',
224 '8','9','+','/'
225};
226
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700227static uint32_t string_to_enum(const struct string_to_enum *table, size_t size,
228 const char *name)
229{
230 size_t i;
231 for (i = 0; i < size; i++) {
232 if (strcmp(table[i].name, name) == 0) {
233 ALOGV("%s found %s", __func__, table[i].name);
234 return table[i].value;
235 }
236 }
237 return 0;
238}
239
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530240static audio_io_flags_t parse_flag_names(char *name)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700241{
242 uint32_t flag = 0;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530243 audio_io_flags_t io_flags;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800244 char *last_r;
245 char *flag_name = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700246 while (flag_name != NULL) {
247 if (strlen(flag_name) != 0) {
248 flag |= string_to_enum(s_flag_name_to_enum_table,
249 ARRAY_SIZE(s_flag_name_to_enum_table),
250 flag_name);
251 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800252 flag_name = strtok_r(NULL, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700253 }
254
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800255 ALOGV("parse_flag_names: flag - %x", flag);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530256 io_flags.in_flags = (audio_input_flags_t)flag;
257 io_flags.out_flags = (audio_output_flags_t)flag;
258 return io_flags;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700259}
260
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530261static void parse_format_names(char *name, struct streams_io_cfg *s_info)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700262{
263 struct stream_format *sf_info = NULL;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800264 char *last_r;
265 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700266
267 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG) == 0)
268 return;
269
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530270 list_init(&s_info->format_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700271 while (str != NULL) {
272 audio_format_t format = (audio_format_t)string_to_enum(s_format_name_to_enum_table,
273 ARRAY_SIZE(s_format_name_to_enum_table), str);
274 ALOGV("%s: format - %d", __func__, format);
275 if (format != 0) {
276 sf_info = (struct stream_format *)calloc(1, sizeof(struct stream_format));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700277 if (sf_info == NULL)
278 break; /* return whatever was parsed */
279
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700280 sf_info->format = format;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530281 list_add_tail(&s_info->format_list, &sf_info->list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700282 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800283 str = strtok_r(NULL, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700284 }
285}
286
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530287static void parse_sample_rate_names(char *name, struct streams_io_cfg *s_info)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700288{
Amit Shekhar6f461b12014-08-01 14:52:58 -0700289 struct stream_sample_rate *ss_info = NULL;
290 uint32_t sample_rate = 48000;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800291 char *last_r;
292 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700293
Amit Shekhar6f461b12014-08-01 14:52:58 -0700294 if (str != NULL && 0 == strcmp(str, DYNAMIC_VALUE_TAG))
295 return;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700296
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530297 list_init(&s_info->sample_rate_list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700298 while (str != NULL) {
299 sample_rate = (uint32_t)strtol(str, (char **)NULL, 10);
300 ALOGV("%s: sample_rate - %d", __func__, sample_rate);
301 if (0 != sample_rate) {
302 ss_info = (struct stream_sample_rate *)calloc(1, sizeof(struct stream_sample_rate));
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800303 if (!ss_info) {
304 ALOGE("%s: memory allocation failure", __func__);
305 return;
306 }
Amit Shekhar6f461b12014-08-01 14:52:58 -0700307 ss_info->sample_rate = sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530308 list_add_tail(&s_info->sample_rate_list, &ss_info->list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700309 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800310 str = strtok_r(NULL, "|", &last_r);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700311 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700312}
313
314static int parse_bit_width_names(char *name)
315{
316 int bit_width = 16;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800317 char *last_r;
318 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700319
320 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG))
321 bit_width = (int)strtol(str, (char **)NULL, 10);
322
323 ALOGV("%s: bit_width - %d", __func__, bit_width);
324 return bit_width;
325}
326
327static int parse_app_type_names(void *platform, char *name)
328{
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700329 int app_type = platform_get_default_app_type(platform);
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800330 char *last_r;
331 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700332
333 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG))
334 app_type = (int)strtol(str, (char **)NULL, 10);
335
336 ALOGV("%s: app_type - %d", __func__, app_type);
337 return app_type;
338}
339
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530340static void update_streams_cfg_list(cnode *root, void *platform,
341 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700342{
343 cnode *node = root->first_child;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530344 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700345
346 ALOGV("%s", __func__);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530347 s_info = (struct streams_io_cfg *)calloc(1, sizeof(struct streams_io_cfg));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700348
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530349 if (!s_info) {
350 ALOGE("failed to allocate mem for s_info list element");
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700351 return;
352 }
353
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700354 while (node) {
355 if (strcmp(node->name, FLAGS_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530356 s_info->flags = parse_flag_names((char *)node->value);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530357 } else if (strcmp(node->name, PROFILES_TAG) == 0) {
358 strlcpy(s_info->profile, (char *)node->value, sizeof(s_info->profile));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700359 } else if (strcmp(node->name, FORMATS_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530360 parse_format_names((char *)node->value, s_info);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700361 } else if (strcmp(node->name, SAMPLING_RATES_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530362 s_info->app_type_cfg.sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
363 parse_sample_rate_names((char *)node->value, s_info);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700364 } else if (strcmp(node->name, BIT_WIDTH_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530365 s_info->app_type_cfg.bit_width = parse_bit_width_names((char *)node->value);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700366 } else if (strcmp(node->name, APP_TYPE_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530367 s_info->app_type_cfg.app_type = parse_app_type_names(platform, (char *)node->value);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700368 }
369 node = node->next;
370 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530371 list_add_tail(streams_cfg_list, &s_info->list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700372}
373
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530374static void load_cfg_list(cnode *root, void *platform,
375 struct listnode *streams_output_cfg_list,
376 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700377{
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530378 cnode *node = NULL;
379
380 node = config_find(root, OUTPUTS_TAG);
381 if (node != NULL) {
382 node = node->first_child;
383 while (node) {
384 ALOGV("%s: loading output %s", __func__, node->name);
385 update_streams_cfg_list(node, platform, streams_output_cfg_list);
386 node = node->next;
387 }
388 } else {
389 ALOGI("%s: could not load output, node is NULL", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700390 }
391
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530392 node = config_find(root, INPUTS_TAG);
393 if (node != NULL) {
394 node = node->first_child;
395 while (node) {
396 ALOGV("%s: loading input %s", __func__, node->name);
397 update_streams_cfg_list(node, platform, streams_input_cfg_list);
398 node = node->next;
399 }
400 } else {
401 ALOGI("%s: could not load input, node is NULL", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700402 }
403}
404
405static void send_app_type_cfg(void *platform, struct mixer *mixer,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530406 struct listnode *streams_output_cfg_list,
407 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700408{
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530409 size_t app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT] = {0};
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700410 int length = 0, i, num_app_types = 0;
411 struct listnode *node;
412 bool update;
413 struct mixer_ctl *ctl = NULL;
414 const char *mixer_ctl_name = "App Type Config";
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530415 struct streams_io_cfg *s_info = NULL;
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800416 uint32_t target_bit_width = 0;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700417
418 if (!mixer) {
419 ALOGE("%s: mixer is null",__func__);
420 return;
421 }
422 ctl = mixer_get_ctl_by_name(mixer, mixer_ctl_name);
423 if (!ctl) {
424 ALOGE("%s: Could not get ctl for mixer cmd - %s",__func__, mixer_ctl_name);
425 return;
426 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530427 app_type_cfg[length++] = num_app_types;
428
429 if (list_empty(streams_output_cfg_list)) {
430 app_type_cfg[length++] = platform_get_default_app_type_v2(platform, PCM_PLAYBACK);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700431 app_type_cfg[length++] = 48000;
432 app_type_cfg[length++] = 16;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530433 num_app_types += 1;
434 }
435 if (list_empty(streams_input_cfg_list)) {
436 app_type_cfg[length++] = platform_get_default_app_type_v2(platform, PCM_CAPTURE);
437 app_type_cfg[length++] = 48000;
438 app_type_cfg[length++] = 16;
439 num_app_types += 1;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700440 }
441
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800442 /* get target bit width for ADM enforce mode */
443 target_bit_width = adev_get_dsp_bit_width_enforce_mode();
444
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700445 list_for_each(node, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530446 s_info = node_to_item(node, struct streams_io_cfg, list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700447 update = true;
448 for (i=0; i<length; i=i+3) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530449 if (app_type_cfg[i+1] == 0)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700450 break;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530451 else if (app_type_cfg[i+1] == (size_t)s_info->app_type_cfg.app_type) {
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530452 if (app_type_cfg[i+2] < (size_t)s_info->app_type_cfg.sample_rate)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530453 app_type_cfg[i+2] = s_info->app_type_cfg.sample_rate;
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530454 if (app_type_cfg[i+3] < (size_t)s_info->app_type_cfg.bit_width)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530455 app_type_cfg[i+3] = s_info->app_type_cfg.bit_width;
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800456 /* ADM bit width = max(enforce_bit_width, bit_width from s_info */
457 if (audio_extn_is_dsp_bit_width_enforce_mode_supported(s_info->flags.out_flags) &&
458 (target_bit_width > app_type_cfg[i+3]))
459 app_type_cfg[i+3] = target_bit_width;
460
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700461 update = false;
462 break;
463 }
464 }
465 if (update && ((length + 3) <= MAX_LENGTH_MIXER_CONTROL_IN_INT)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530466 num_app_types += 1;
467 app_type_cfg[length++] = s_info->app_type_cfg.app_type;
468 app_type_cfg[length++] = s_info->app_type_cfg.sample_rate;
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800469 app_type_cfg[length] = s_info->app_type_cfg.bit_width;
470 if (audio_extn_is_dsp_bit_width_enforce_mode_supported(s_info->flags.out_flags) &&
471 (target_bit_width > app_type_cfg[length]))
472 app_type_cfg[length] = target_bit_width;
473
474 length++;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530475 }
476 }
477 list_for_each(node, streams_input_cfg_list) {
478 s_info = node_to_item(node, struct streams_io_cfg, list);
479 update = true;
480 for (i=0; i<length; i=i+3) {
481 if (app_type_cfg[i+1] == 0)
482 break;
483 else if (app_type_cfg[i+1] == (size_t)s_info->app_type_cfg.app_type) {
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530484 if (app_type_cfg[i+2] < (size_t)s_info->app_type_cfg.sample_rate)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530485 app_type_cfg[i+2] = s_info->app_type_cfg.sample_rate;
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530486 if (app_type_cfg[i+3] < (size_t)s_info->app_type_cfg.bit_width)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530487 app_type_cfg[i+3] = s_info->app_type_cfg.bit_width;
488 update = false;
489 break;
490 }
491 }
492 if (update && ((length + 3) <= MAX_LENGTH_MIXER_CONTROL_IN_INT)) {
493 num_app_types += 1;
494 app_type_cfg[length++] = s_info->app_type_cfg.app_type;
495 app_type_cfg[length++] = s_info->app_type_cfg.sample_rate;
496 app_type_cfg[length++] = s_info->app_type_cfg.bit_width;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700497 }
498 }
499 ALOGV("%s: num_app_types: %d", __func__, num_app_types);
500 if (num_app_types) {
501 app_type_cfg[0] = num_app_types;
502 mixer_ctl_set_array(ctl, app_type_cfg, length);
503 }
504}
505
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530506void audio_extn_utils_update_streams_cfg_lists(void *platform,
507 struct mixer *mixer,
508 struct listnode *streams_output_cfg_list,
509 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700510{
511 cnode *root;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530512 char *data = NULL;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700513
514 ALOGV("%s", __func__);
515 list_init(streams_output_cfg_list);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530516 list_init(streams_input_cfg_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700517
518 root = config_node("", "");
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700519 if (root == NULL) {
520 ALOGE("cfg_list, NULL config root");
521 return;
522 }
523
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530524 data = (char *)load_file(AUDIO_IO_POLICY_VENDOR_CONFIG_FILE, NULL);
525 if (data == NULL) {
526 ALOGD("%s: failed to open io config file(%s), trying older config file",
527 __func__, AUDIO_IO_POLICY_VENDOR_CONFIG_FILE);
528 data = (char *)load_file(AUDIO_OUTPUT_POLICY_VENDOR_CONFIG_FILE, NULL);
529 if (data == NULL) {
530 send_app_type_cfg(platform, mixer,
531 streams_output_cfg_list,
532 streams_input_cfg_list);
533 ALOGE("%s: could not load io policy config!", __func__);
yidongh6eb4f752017-04-19 18:20:57 +0800534 free(root);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530535 return;
536 }
537 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700538
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530539 config_load(root, data);
540 load_cfg_list(root, platform, streams_output_cfg_list,
541 streams_input_cfg_list);
542
543 send_app_type_cfg(platform, mixer, streams_output_cfg_list,
544 streams_input_cfg_list);
Alexy Josephaee4fdd2016-01-29 13:02:07 -0800545
546 config_free(root);
yidongh6eb4f752017-04-19 18:20:57 +0800547 free(root);
Alexy Josephaee4fdd2016-01-29 13:02:07 -0800548 free(data);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700549}
550
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530551static void audio_extn_utils_dump_streams_cfg_list(
552 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700553{
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700554 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530555 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700556 struct stream_format *sf_info;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700557 struct stream_sample_rate *ss_info;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530558
559 list_for_each(node_i, streams_cfg_list) {
560 s_info = node_to_item(node_i, struct streams_io_cfg, list);
561 ALOGV("%s: flags-%d, sample_rate-%d, bit_width-%d, app_type-%d",
562 __func__, s_info->flags.out_flags, s_info->app_type_cfg.sample_rate,
563 s_info->app_type_cfg.bit_width, s_info->app_type_cfg.app_type);
564 list_for_each(node_j, &s_info->format_list) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700565 sf_info = node_to_item(node_j, struct stream_format, list);
566 ALOGV("format-%x", sf_info->format);
567 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530568 list_for_each(node_j, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700569 ss_info = node_to_item(node_j, struct stream_sample_rate, list);
570 ALOGV("sample rate-%d", ss_info->sample_rate);
571 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700572 }
573}
574
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530575void audio_extn_utils_dump_streams_cfg_lists(
576 struct listnode *streams_output_cfg_list,
577 struct listnode *streams_input_cfg_list)
578{
579 ALOGV("%s", __func__);
580 audio_extn_utils_dump_streams_cfg_list(streams_output_cfg_list);
581 audio_extn_utils_dump_streams_cfg_list(streams_input_cfg_list);
582}
583
584static void audio_extn_utils_release_streams_cfg_list(
585 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700586{
587 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530588 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700589
590 ALOGV("%s", __func__);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530591
592 while (!list_empty(streams_cfg_list)) {
593 node_i = list_head(streams_cfg_list);
594 s_info = node_to_item(node_i, struct streams_io_cfg, list);
595 while (!list_empty(&s_info->format_list)) {
596 node_j = list_head(&s_info->format_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700597 list_remove(node_j);
598 free(node_to_item(node_j, struct stream_format, list));
599 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530600 while (!list_empty(&s_info->sample_rate_list)) {
601 node_j = list_head(&s_info->sample_rate_list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700602 list_remove(node_j);
603 free(node_to_item(node_j, struct stream_sample_rate, list));
604 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700605 list_remove(node_i);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530606 free(node_to_item(node_i, struct streams_io_cfg, list));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700607 }
608}
609
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530610void audio_extn_utils_release_streams_cfg_lists(
611 struct listnode *streams_output_cfg_list,
612 struct listnode *streams_input_cfg_list)
613{
614 ALOGV("%s", __func__);
615 audio_extn_utils_release_streams_cfg_list(streams_output_cfg_list);
616 audio_extn_utils_release_streams_cfg_list(streams_input_cfg_list);
617}
618
619static bool set_app_type_cfg(struct streams_io_cfg *s_info,
620 struct stream_app_type_cfg *app_type_cfg,
621 uint32_t sample_rate, uint32_t bit_width)
Amit Shekhar6f461b12014-08-01 14:52:58 -0700622 {
623 struct listnode *node_i;
624 struct stream_sample_rate *ss_info;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530625 list_for_each(node_i, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700626 ss_info = node_to_item(node_i, struct stream_sample_rate, list);
627 if ((sample_rate <= ss_info->sample_rate) &&
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530628 (bit_width == s_info->app_type_cfg.bit_width)) {
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700629
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530630 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700631 app_type_cfg->sample_rate = ss_info->sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530632 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700633 ALOGV("%s app_type_cfg->app_type %d, app_type_cfg->sample_rate %d, app_type_cfg->bit_width %d",
634 __func__, app_type_cfg->app_type, app_type_cfg->sample_rate, app_type_cfg->bit_width);
635 return true;
636 }
637 }
638 /*
639 * Reiterate through the list assuming dafault sample rate.
640 * Handles scenario where input sample rate is higher
641 * than all sample rates in list for the input bit width.
642 */
643 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
Apoorv Raghuvanshif59bb222015-02-18 12:23:23 -0800644
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530645 list_for_each(node_i, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700646 ss_info = node_to_item(node_i, struct stream_sample_rate, list);
647 if ((sample_rate <= ss_info->sample_rate) &&
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530648 (bit_width == s_info->app_type_cfg.bit_width)) {
649 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700650 app_type_cfg->sample_rate = sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530651 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Apoorv Raghuvanshif59bb222015-02-18 12:23:23 -0800652 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 -0700653 __func__, app_type_cfg->app_type, app_type_cfg->sample_rate, app_type_cfg->bit_width);
654 return true;
655 }
656 }
657 return false;
658}
659
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530660void audio_extn_utils_update_stream_input_app_type_cfg(void *platform,
661 struct listnode *streams_input_cfg_list,
662 audio_devices_t devices __unused,
663 audio_input_flags_t flags,
664 audio_format_t format,
665 uint32_t sample_rate,
666 uint32_t bit_width,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530667 char* profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530668 struct stream_app_type_cfg *app_type_cfg)
669{
670 struct listnode *node_i, *node_j;
671 struct streams_io_cfg *s_info;
672 struct stream_format *sf_info;
673
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530674 ALOGV("%s: flags: 0x%x, format: 0x%x sample_rate %d, profile %s",
675 __func__, flags, format, sample_rate, profile);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530676
677 list_for_each(node_i, streams_input_cfg_list) {
678 s_info = node_to_item(node_i, struct streams_io_cfg, list);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530679 /* Along with flags do profile matching if set at either end.*/
680 if (s_info->flags.in_flags == flags &&
681 ((profile[0] == '\0' && s_info->profile[0] == '\0') ||
682 strncmp(s_info->profile, profile, sizeof(s_info->profile)) == 0)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530683 list_for_each(node_j, &s_info->format_list) {
684 sf_info = node_to_item(node_j, struct stream_format, list);
685 if (sf_info->format == format) {
686 if (set_app_type_cfg(s_info, app_type_cfg, sample_rate, bit_width))
687 return;
688 }
689 }
690 }
691 }
692 ALOGW("%s: App type could not be selected. Falling back to default", __func__);
693 app_type_cfg->app_type = platform_get_default_app_type_v2(platform, PCM_CAPTURE);
694 app_type_cfg->sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
695 app_type_cfg->bit_width = 16;
696}
697
698void audio_extn_utils_update_stream_output_app_type_cfg(void *platform,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700699 struct listnode *streams_output_cfg_list,
Amit Shekhar1d896042014-10-03 13:16:09 -0700700 audio_devices_t devices,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700701 audio_output_flags_t flags,
702 audio_format_t format,
Amit Shekhar6f461b12014-08-01 14:52:58 -0700703 uint32_t sample_rate,
704 uint32_t bit_width,
Manish Dewangan837dc462015-05-27 10:17:41 +0530705 audio_channel_mask_t channel_mask,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530706 char *profile,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700707 struct stream_app_type_cfg *app_type_cfg)
708{
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +0530709 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530710 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700711 struct stream_format *sf_info;
Manish Dewangan837dc462015-05-27 10:17:41 +0530712 char value[PROPERTY_VALUE_MAX] = {0};
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700713
Ramu Gottipati074fa4d2018-09-11 20:05:51 +0530714 if (devices & AUDIO_DEVICE_OUT_SPEAKER) {
715 int bw = platform_get_snd_device_bit_width(SND_DEVICE_OUT_SPEAKER);
716 if ((-ENOSYS != bw) && (bit_width > (uint32_t)bw))
Amit Shekhar5a39c912014-10-14 15:39:30 -0700717 bit_width = (uint32_t)bw;
Ramu Gottipati074fa4d2018-09-11 20:05:51 +0530718 else if (-ENOSYS == bw)
719 bit_width = CODEC_BACKEND_DEFAULT_BIT_WIDTH;
Amit Shekhar1d896042014-10-03 13:16:09 -0700720 sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Ramu Gottipati074fa4d2018-09-11 20:05:51 +0530721 ALOGI("%s Allowing 24 and above bits playback on speaker ONLY at default sampling rate", __func__);
Amit Shekhar1d896042014-10-03 13:16:09 -0700722 }
723
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -0700724 property_get("vendor.audio.playback.mch.downsample",value,"");
Manish Dewangan837dc462015-05-27 10:17:41 +0530725 if (!strncmp("true", value, sizeof("true"))) {
726 if ((popcount(channel_mask) > 2) &&
727 (sample_rate > CODEC_BACKEND_DEFAULT_SAMPLE_RATE) &&
Aalique Grahame5cd12ff2017-10-19 10:57:00 -0700728 !(flags & (audio_output_flags_t)AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH)) {
Manish Dewangan837dc462015-05-27 10:17:41 +0530729 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
730 ALOGD("%s: MCH session defaulting sample rate to %d",
731 __func__, sample_rate);
732 }
733 }
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +0530734
735 /* Set sampling rate to 176.4 for DSD64
736 * and 352.8Khz for DSD128.
737 * Set Bit Width to 16. output will be 16 bit
738 * post DoP in ASM.
739 */
Aalique Grahame5cd12ff2017-10-19 10:57:00 -0700740 if ((flags & (audio_output_flags_t)AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH) &&
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +0530741 (format == AUDIO_FORMAT_DSD)) {
742 bit_width = 16;
743 if (sample_rate == INPUT_SAMPLING_RATE_DSD64)
744 sample_rate = OUTPUT_SAMPLING_RATE_DSD64;
745 else if (sample_rate == INPUT_SAMPLING_RATE_DSD128)
746 sample_rate = OUTPUT_SAMPLING_RATE_DSD128;
747 }
748
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +0530749
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800750 ALOGV("%s: flags: %x, format: %x sample_rate %d, profile %s, app_type %d",
751 __func__, flags, format, sample_rate, profile, app_type_cfg->app_type);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700752 list_for_each(node_i, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530753 s_info = node_to_item(node_i, struct streams_io_cfg, list);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530754 /* Along with flags do profile matching if set at either end.*/
755 if (s_info->flags.out_flags == flags &&
756 ((profile[0] == '\0' && s_info->profile[0] == '\0') ||
757 strncmp(s_info->profile, profile, sizeof(s_info->profile)) == 0)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530758 list_for_each(node_j, &s_info->format_list) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700759 sf_info = node_to_item(node_j, struct stream_format, list);
760 if (sf_info->format == format) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530761 if (set_app_type_cfg(s_info, app_type_cfg, sample_rate, bit_width))
Amit Shekhar6f461b12014-08-01 14:52:58 -0700762 return;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700763 }
764 }
765 }
766 }
767 list_for_each(node_i, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530768 s_info = node_to_item(node_i, struct streams_io_cfg, list);
769 if (s_info->flags.out_flags == AUDIO_OUTPUT_FLAG_PRIMARY) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700770 ALOGV("Compatible output profile not found.");
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530771 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
772 app_type_cfg->sample_rate = s_info->app_type_cfg.sample_rate;
773 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700774 ALOGV("%s Default to primary output: App type: %d sample_rate %d",
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530775 __func__, s_info->app_type_cfg.app_type, app_type_cfg->sample_rate);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700776 return;
777 }
778 }
779 ALOGW("%s: App type could not be selected. Falling back to default", __func__);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700780 app_type_cfg->app_type = platform_get_default_app_type(platform);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700781 app_type_cfg->sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700782 app_type_cfg->bit_width = 16;
783}
784
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +0530785static bool audio_is_this_native_usecase(struct audio_usecase *uc)
786{
787 bool native_usecase = false;
788 struct stream_out *out = (struct stream_out*) uc->stream.out;
789
790 if (PCM_PLAYBACK == uc->type && out != NULL &&
791 NATIVE_AUDIO_MODE_INVALID != platform_get_native_support() &&
792 is_offload_usecase(uc->id) &&
793 (out->sample_rate == OUTPUT_SAMPLING_RATE_44100))
794 native_usecase = true;
795
796 return native_usecase;
797}
798
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800799bool audio_extn_is_dsp_bit_width_enforce_mode_supported(audio_output_flags_t flags)
800{
801 /* DSP bitwidth enforce mode for ADM and AFE:
802 * includes:
803 * deep buffer, low latency, direct pcm and offload.
804 * excludes:
805 * ull(raw+fast), VOIP.
806 */
807 if ((flags & AUDIO_OUTPUT_FLAG_VOIP_RX) ||
808 ((flags & AUDIO_OUTPUT_FLAG_RAW) &&
809 (flags & AUDIO_OUTPUT_FLAG_FAST)))
810 return false;
811
812
813 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) ||
814 (flags & AUDIO_OUTPUT_FLAG_DIRECT) ||
815 (flags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) ||
816 (flags & AUDIO_OUTPUT_FLAG_PRIMARY))
817 return true;
818 else
819 return false;
820}
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800821
822static inline bool audio_is_vr_mode_on(struct audio_device *(__attribute__((unused)) adev))
823{
824 return adev->vr_audio_mode_enabled;
825}
826
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530827void audio_extn_utils_update_stream_app_type_cfg_for_usecase(
828 struct audio_device *adev,
829 struct audio_usecase *usecase)
830{
831 ALOGV("%s", __func__);
832
833 switch(usecase->type) {
834 case PCM_PLAYBACK:
835 audio_extn_utils_update_stream_output_app_type_cfg(adev->platform,
836 &adev->streams_output_cfg_list,
837 usecase->stream.out->devices,
838 usecase->stream.out->flags,
Aalique Grahame65780b52017-09-27 14:59:56 -0700839 usecase->stream.out->hal_op_format,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530840 usecase->stream.out->sample_rate,
841 usecase->stream.out->bit_width,
842 usecase->stream.out->channel_mask,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530843 usecase->stream.out->profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530844 &usecase->stream.out->app_type_cfg);
845 ALOGV("%s Selected apptype: %d", __func__, usecase->stream.out->app_type_cfg.app_type);
846 break;
847 case PCM_CAPTURE:
Vikram Panduranga93f080e2017-06-07 18:16:14 -0700848 if (usecase->id == USECASE_AUDIO_RECORD_VOIP)
849 usecase->stream.in->app_type_cfg.app_type = APP_TYPE_VOIP_AUDIO;
850 else
851 audio_extn_utils_update_stream_input_app_type_cfg(adev->platform,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530852 &adev->streams_input_cfg_list,
853 usecase->stream.in->device,
854 usecase->stream.in->flags,
855 usecase->stream.in->format,
856 usecase->stream.in->sample_rate,
857 usecase->stream.in->bit_width,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530858 usecase->stream.in->profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530859 &usecase->stream.in->app_type_cfg);
860 ALOGV("%s Selected apptype: %d", __func__, usecase->stream.in->app_type_cfg.app_type);
861 break;
Surendar Karka93cd25a2018-08-28 14:21:37 +0530862 case TRANSCODE_LOOPBACK_RX :
Siddartha Shaik343abc62017-08-08 11:15:25 +0530863 audio_extn_utils_update_stream_output_app_type_cfg(adev->platform,
864 &adev->streams_output_cfg_list,
865 usecase->stream.inout->out_config.devices,
866 0,
867 usecase->stream.inout->out_config.format,
868 usecase->stream.inout->out_config.sample_rate,
869 usecase->stream.inout->out_config.bit_width,
870 usecase->stream.inout->out_config.channel_mask,
871 usecase->stream.inout->profile,
872 &usecase->stream.inout->out_app_type_cfg);
873 ALOGV("%s Selected apptype: %d", __func__, usecase->stream.inout->out_app_type_cfg.app_type);
874 break;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530875 default:
876 ALOGE("%s: app type cfg not supported for usecase type (%d)",
877 __func__, usecase->type);
878 }
879}
880
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +0530881void audio_extn_btsco_get_sample_rate(int snd_device, int *sample_rate)
882{
883 switch (snd_device) {
884 case SND_DEVICE_OUT_BT_SCO:
885 case SND_DEVICE_IN_BT_SCO_MIC:
886 case SND_DEVICE_IN_BT_SCO_MIC_NREC:
887 *sample_rate = 8000;
888 break;
889 case SND_DEVICE_OUT_BT_SCO_WB:
890 case SND_DEVICE_IN_BT_SCO_MIC_WB:
891 case SND_DEVICE_IN_BT_SCO_MIC_WB_NREC:
892 *sample_rate = 16000;
893 break;
894 default:
895 ALOGD("%s:Not a BT SCO device, need not update sampling rate\n", __func__);
896 break;
897 }
898}
899
Aalique Grahame22e49102018-12-18 14:23:57 -0800900static int set_stream_app_type_mixer_ctrl(struct audio_device *adev,
901 int pcm_device_id, int app_type,
902 int acdb_dev_id, int sample_rate,
903 int stream_type,
904 snd_device_t snd_device)
905{
906
907 char mixer_ctl_name[MAX_LENGTH_MIXER_CONTROL_IN_INT];
908 struct mixer_ctl *ctl;
909 int app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT], len = 0, rc = 0;
910 int snd_device_be_idx = -1;
911
912 if (stream_type == PCM_PLAYBACK) {
913 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
914 "Audio Stream %d App Type Cfg", pcm_device_id);
915 } else if (stream_type == PCM_CAPTURE) {
916 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
917 "Audio Stream Capture %d App Type Cfg", pcm_device_id);
918 }
919
920 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
921 if (!ctl) {
922 ALOGE("%s: Could not get ctl for mixer cmd - %s",
923 __func__, mixer_ctl_name);
924 rc = -EINVAL;
925 goto exit;
926 }
927 app_type_cfg[len++] = app_type;
928 app_type_cfg[len++] = acdb_dev_id;
929 app_type_cfg[len++] = sample_rate;
930
931 snd_device_be_idx = platform_get_snd_device_backend_index(snd_device);
932 if (snd_device_be_idx > 0)
933 app_type_cfg[len++] = snd_device_be_idx;
934 ALOGV("%s: stream type %d app_type %d, acdb_dev_id %d "
935 "sample rate %d, snd_device_be_idx %d",
936 __func__, stream_type, app_type, acdb_dev_id, sample_rate,
937 snd_device_be_idx);
938 mixer_ctl_set_array(ctl, app_type_cfg, len);
939
940exit:
941 return rc;
942}
943
944static int audio_extn_utils_send_app_type_cfg_hfp(struct audio_device *adev,
945 struct audio_usecase *usecase)
946{
947 int pcm_device_id, acdb_dev_id = 0, snd_device = usecase->out_snd_device;
948 int32_t sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
949 int app_type = 0, rc = 0;
950
951 ALOGV("%s", __func__);
952
953 if (usecase->type != PCM_HFP_CALL) {
954 ALOGV("%s: not a playback or HFP path, no need to cfg app type", __func__);
955 rc = 0;
956 goto exit_send_app_type_cfg;
957 }
958 if ((usecase->id != USECASE_AUDIO_HFP_SCO) &&
959 (usecase->id != USECASE_AUDIO_HFP_SCO_WB)) {
960 ALOGV("%s: a playback path where app type cfg is not required", __func__);
961 rc = 0;
962 goto exit_send_app_type_cfg;
963 }
964
965 snd_device = usecase->out_snd_device;
966 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_PLAYBACK);
967
968 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
969 if (acdb_dev_id < 0) {
970 ALOGE("%s: Couldn't get the acdb dev id", __func__);
971 rc = -EINVAL;
972 goto exit_send_app_type_cfg;
973 }
974
975 if (usecase->type == PCM_HFP_CALL) {
976
977 /* config HFP session:1 playback path */
978 app_type = platform_get_default_app_type_v2(adev->platform, PCM_PLAYBACK);
979 sample_rate= CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
980 rc = set_stream_app_type_mixer_ctrl(adev, pcm_device_id, app_type,
981 acdb_dev_id, sample_rate,
982 PCM_PLAYBACK,
983 SND_DEVICE_NONE); // use legacy behavior
984 if (rc < 0)
985 goto exit_send_app_type_cfg;
986
987 /* config HFP session:1 capture path */
988 app_type = platform_get_default_app_type_v2(adev->platform, PCM_CAPTURE);
989 rc = set_stream_app_type_mixer_ctrl(adev, pcm_device_id, app_type,
990 acdb_dev_id, sample_rate,
991 PCM_CAPTURE,
992 SND_DEVICE_NONE);
993 if (rc < 0)
994 goto exit_send_app_type_cfg;
995
996 /* config HFP session:2 capture path */
997 pcm_device_id = HFP_ASM_RX_TX;
998 snd_device = usecase->in_snd_device;
999 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
1000 if (acdb_dev_id <= 0) {
1001 ALOGE("%s: Couldn't get the acdb dev id", __func__);
1002 rc = -EINVAL;
1003 goto exit_send_app_type_cfg;
1004 }
1005 app_type = platform_get_default_app_type_v2(adev->platform, PCM_CAPTURE);
1006 rc = set_stream_app_type_mixer_ctrl(adev, pcm_device_id, app_type,
1007 acdb_dev_id, sample_rate, PCM_CAPTURE,
1008 SND_DEVICE_NONE);
1009 if (rc < 0)
1010 goto exit_send_app_type_cfg;
1011
1012 /* config HFP session:2 playback path */
1013 app_type = platform_get_default_app_type_v2(adev->platform, PCM_PLAYBACK);
1014 rc = set_stream_app_type_mixer_ctrl(adev, pcm_device_id, app_type,
1015 acdb_dev_id, sample_rate,
1016 PCM_PLAYBACK, SND_DEVICE_NONE);
1017 if (rc < 0)
1018 goto exit_send_app_type_cfg;
1019 }
1020
1021 rc = 0;
1022exit_send_app_type_cfg:
1023 return rc;
1024}
1025
Siena Richard7c2db772016-12-21 11:32:34 -08001026static int send_app_type_cfg_for_device(struct audio_device *adev,
1027 struct audio_usecase *usecase,
1028 int split_snd_device)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001029{
1030 char mixer_ctl_name[MAX_LENGTH_MIXER_CONTROL_IN_INT];
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301031 size_t app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT] = {0};
1032 int len = 0, rc;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001033 struct mixer_ctl *ctl;
Siena Richard7c2db772016-12-21 11:32:34 -08001034 int pcm_device_id = 0, acdb_dev_id, app_type;
1035 int snd_device = split_snd_device, snd_device_be_idx = -1;
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +05301036 int32_t sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Manish Dewangan837dc462015-05-27 10:17:41 +05301037 char value[PROPERTY_VALUE_MAX] = {0};
Varun Balaraje49253e2017-07-06 19:48:56 +05301038 struct streams_io_cfg *s_info = NULL;
1039 struct listnode *node = NULL;
Nikhil Laturkarac4a7492017-08-31 18:27:19 +05301040 int bd_app_type = 0;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001041
Siena Richard7c2db772016-12-21 11:32:34 -08001042 ALOGV("%s: usecase->out_snd_device %s, usecase->in_snd_device %s, split_snd_device %s",
1043 __func__, platform_get_snd_device_name(usecase->out_snd_device),
1044 platform_get_snd_device_name(usecase->in_snd_device),
1045 platform_get_snd_device_name(split_snd_device));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001046
Siddartha Shaik343abc62017-08-08 11:15:25 +05301047 if (usecase->type != PCM_PLAYBACK && usecase->type != PCM_CAPTURE &&
Surendar Karka93cd25a2018-08-28 14:21:37 +05301048 usecase->type != TRANSCODE_LOOPBACK_RX) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301049 ALOGE("%s: not a playback/capture path, no need to cfg app type", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001050 rc = 0;
1051 goto exit_send_app_type_cfg;
1052 }
1053 if ((usecase->id != USECASE_AUDIO_PLAYBACK_DEEP_BUFFER) &&
1054 (usecase->id != USECASE_AUDIO_PLAYBACK_LOW_LATENCY) &&
1055 (usecase->id != USECASE_AUDIO_PLAYBACK_MULTI_CH) &&
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -08001056 (usecase->id != USECASE_AUDIO_PLAYBACK_ULL) &&
Vikram Panduranga93f080e2017-06-07 18:16:14 -07001057 (usecase->id != USECASE_AUDIO_PLAYBACK_VOIP) &&
Surendar Karka93cd25a2018-08-28 14:21:37 +05301058 (usecase->id != USECASE_AUDIO_TRANSCODE_LOOPBACK_RX) &&
Varun Balaraje49253e2017-07-06 19:48:56 +05301059 (!is_interactive_usecase(usecase->id)) &&
Srikanth Uyyala9d551402015-08-25 16:03:42 +05301060 (!is_offload_usecase(usecase->id)) &&
1061 (usecase->type != PCM_CAPTURE)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301062 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 -07001063 rc = 0;
1064 goto exit_send_app_type_cfg;
1065 }
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -08001066
1067 //if VR is active then only send the mixer control
1068 if (usecase->id == USECASE_AUDIO_PLAYBACK_ULL && !audio_is_vr_mode_on(adev)) {
1069 ALOGI("ULL doesnt need sending app type cfg, returning");
1070 rc = 0;
1071 goto exit_send_app_type_cfg;
1072 }
1073
Surendar Karka93cd25a2018-08-28 14:21:37 +05301074 if (usecase->type == PCM_PLAYBACK || usecase->type == TRANSCODE_LOOPBACK_RX) {
Ben Romberger1fafdde2015-09-09 19:43:15 -07001075 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_PLAYBACK);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301076 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
1077 "Audio Stream %d App Type Cfg", pcm_device_id);
Ben Romberger1fafdde2015-09-09 19:43:15 -07001078 } else if (usecase->type == PCM_CAPTURE) {
Ben Romberger1fafdde2015-09-09 19:43:15 -07001079 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_CAPTURE);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301080 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
1081 "Audio Stream Capture %d App Type Cfg", pcm_device_id);
Srikanth Uyyala9d551402015-08-25 16:03:42 +05301082 }
Siena Richard7c2db772016-12-21 11:32:34 -08001083
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001084 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
1085 if (!ctl) {
1086 ALOGE("%s: Could not get ctl for mixer cmd - %s", __func__,
1087 mixer_ctl_name);
1088 rc = -EINVAL;
1089 goto exit_send_app_type_cfg;
1090 }
Aditya Bavanari701a6992017-03-30 19:17:16 +05301091 snd_device = platform_get_spkr_prot_snd_device(snd_device);
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301092 if (voice_is_in_call_rec_stream(usecase->stream.in) && usecase->type == PCM_CAPTURE) {
1093 snd_device_t voice_device = voice_get_incall_rec_snd_device(usecase->in_snd_device);
1094 acdb_dev_id = platform_get_snd_device_acdb_id(voice_device);
1095 ALOGV("acdb id for voice call use case %d", acdb_dev_id);
1096 } else {
1097 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
1098 }
Rohit Kumar1181b292017-01-31 18:07:17 +05301099 if (acdb_dev_id <= 0) {
1100 ALOGE("%s: Couldn't get the acdb dev id", __func__);
1101 rc = -EINVAL;
1102 goto exit_send_app_type_cfg;
1103 }
1104
Siena Richard7c2db772016-12-21 11:32:34 -08001105 snd_device_be_idx = platform_get_snd_device_backend_index(snd_device);
1106 if (snd_device_be_idx < 0) {
1107 ALOGE("%s: Couldn't get the backend index for snd device %s ret=%d",
1108 __func__, platform_get_snd_device_name(snd_device),
1109 snd_device_be_idx);
1110 }
1111
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301112 if ((usecase->type == PCM_PLAYBACK) && (usecase->stream.out != NULL)) {
Manish Dewangan837dc462015-05-27 10:17:41 +05301113
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -07001114 property_get("vendor.audio.playback.mch.downsample",value,"");
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301115 if (!strncmp("true", value, sizeof("true"))) {
1116 if ((popcount(usecase->stream.out->channel_mask) > 2) &&
1117 (usecase->stream.out->app_type_cfg.sample_rate > CODEC_BACKEND_DEFAULT_SAMPLE_RATE) &&
Aalique Grahame5cd12ff2017-10-19 10:57:00 -07001118 !(usecase->stream.out->flags &
1119 (audio_output_flags_t)AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH))
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301120 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
1121 }
Ashish Jainc02430d2016-01-04 10:42:43 +05301122
kunleizcdf25942017-09-20 14:01:21 +08001123 if (usecase->id == USECASE_AUDIO_PLAYBACK_VOIP) {
1124 usecase->stream.out->app_type_cfg.sample_rate = usecase->stream.out->sample_rate;
1125 } else if (usecase->stream.out->devices & AUDIO_DEVICE_OUT_SPEAKER) {
Dieter Luecking5d57def2018-09-07 14:23:37 +02001126 if (platform_spkr_use_default_sample_rate(adev->platform)) {
1127 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
1128 } else {
1129 platform_check_and_update_copp_sample_rate(adev->platform, snd_device,
1130 usecase->stream.out->sample_rate,
1131 &usecase->stream.out->app_type_cfg.sample_rate);
1132 }
1133
Ashish Jaina052e572016-11-07 16:41:07 +05301134 } else if ((snd_device == SND_DEVICE_OUT_HDMI ||
1135 snd_device == SND_DEVICE_OUT_USB_HEADSET ||
1136 snd_device == SND_DEVICE_OUT_DISPLAY_PORT) &&
1137 (usecase->stream.out->sample_rate >= OUTPUT_SAMPLING_RATE_44100)) {
1138 /*
1139 * To best utlize DSP, check if the stream sample rate is supported/multiple of
1140 * configured device sample rate, if not update the COPP rate to be equal to the
1141 * device sample rate, else open COPP at stream sample rate
1142 */
1143 platform_check_and_update_copp_sample_rate(adev->platform, snd_device,
1144 usecase->stream.out->sample_rate,
1145 &usecase->stream.out->app_type_cfg.sample_rate);
Ashish Jain4826f6c2017-02-06 13:33:20 +05301146 } else if (((snd_device != SND_DEVICE_OUT_HEADPHONES_44_1 &&
1147 !audio_is_this_native_usecase(usecase)) &&
Mingming Yin21854652016-04-13 11:54:02 -07001148 usecase->stream.out->sample_rate == OUTPUT_SAMPLING_RATE_44100) ||
1149 (usecase->stream.out->sample_rate < OUTPUT_SAMPLING_RATE_44100)) {
Ashish Jain4826f6c2017-02-06 13:33:20 +05301150 /* Reset to default if no native stream is active*/
Mingming Yin21854652016-04-13 11:54:02 -07001151 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Preetam Singh Ranawatb7475ba2017-02-24 18:17:06 +05301152 } else if (usecase->stream.out->devices & AUDIO_DEVICE_OUT_ALL_A2DP) {
1153 /*
1154 * For a2dp playback get encoder sampling rate and set copp sampling rate,
1155 * for bit width use the stream param only.
1156 */
Florian Pfister1a84f312018-07-19 14:38:18 +02001157 audio_extn_a2dp_get_enc_sample_rate(&usecase->stream.out->app_type_cfg.sample_rate);
Preetam Singh Ranawatb7475ba2017-02-24 18:17:06 +05301158 ALOGI("%s using %d sample rate rate for A2DP CoPP",
1159 __func__, usecase->stream.out->app_type_cfg.sample_rate);
Mingming Yin21854652016-04-13 11:54:02 -07001160 }
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301161 audio_extn_btsco_get_sample_rate(snd_device, &usecase->stream.out->app_type_cfg.sample_rate);
Mingming Yin21854652016-04-13 11:54:02 -07001162 sample_rate = usecase->stream.out->app_type_cfg.sample_rate;
1163
Varun Balaraje49253e2017-07-06 19:48:56 +05301164 /* Interactive streams are supported with only direct app type id.
1165 * Get Direct profile app type and use it for interactive streams
1166 */
1167 list_for_each(node, &adev->streams_output_cfg_list) {
1168 s_info = node_to_item(node, struct streams_io_cfg, list);
Aalique Grahame5cd12ff2017-10-19 10:57:00 -07001169 if (s_info->flags.out_flags == (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_BD |
Nikhil Laturkarac4a7492017-08-31 18:27:19 +05301170 AUDIO_OUTPUT_FLAG_DIRECT_PCM |
1171 AUDIO_OUTPUT_FLAG_DIRECT))
1172 bd_app_type = s_info->app_type_cfg.app_type;
Varun Balaraje49253e2017-07-06 19:48:56 +05301173 }
Aalique Grahame5cd12ff2017-10-19 10:57:00 -07001174 if (usecase->stream.out->flags == (audio_output_flags_t)AUDIO_OUTPUT_FLAG_INTERACTIVE)
Nikhil Laturkarac4a7492017-08-31 18:27:19 +05301175 app_type = bd_app_type;
Varun Balaraje49253e2017-07-06 19:48:56 +05301176 else
1177 app_type = usecase->stream.out->app_type_cfg.app_type;
Siena Richard7c2db772016-12-21 11:32:34 -08001178 app_type_cfg[len++] = app_type;
Mingming Yin21854652016-04-13 11:54:02 -07001179 app_type_cfg[len++] = acdb_dev_id;
1180 if (((usecase->stream.out->format == AUDIO_FORMAT_E_AC3) ||
Ben Romberger1aaaf862017-04-06 17:49:46 -07001181 (usecase->stream.out->format == AUDIO_FORMAT_E_AC3_JOC) ||
1182 (usecase->stream.out->format == AUDIO_FORMAT_DOLBY_TRUEHD))
Harsh Bansal026d97f2017-08-17 17:44:49 +05301183 && audio_extn_passthru_is_passthrough_stream(usecase->stream.out)
1184 && !audio_extn_passthru_is_convert_supported(adev, usecase->stream.out)) {
Naresh Tanniru3a406772017-05-10 13:09:05 -07001185
1186 sample_rate = sample_rate * 4;
1187 if (sample_rate > HDMI_PASSTHROUGH_MAX_SAMPLE_RATE)
1188 sample_rate = HDMI_PASSTHROUGH_MAX_SAMPLE_RATE;
Mingming Yin21854652016-04-13 11:54:02 -07001189 }
Naresh Tanniru3a406772017-05-10 13:09:05 -07001190 app_type_cfg[len++] = sample_rate;
1191
Siena Richard7c2db772016-12-21 11:32:34 -08001192 if (snd_device_be_idx > 0)
1193 app_type_cfg[len++] = snd_device_be_idx;
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301194
Siena Richard7c2db772016-12-21 11:32:34 -08001195 ALOGI("%s PLAYBACK app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
1196 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301197
1198 } else if ((usecase->type == PCM_CAPTURE) && (usecase->stream.in != NULL)) {
Siena Richard7c2db772016-12-21 11:32:34 -08001199 app_type = usecase->stream.in->app_type_cfg.app_type;
1200 app_type_cfg[len++] = app_type;
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301201 app_type_cfg[len++] = acdb_dev_id;
kunleizcdf25942017-09-20 14:01:21 +08001202 if (usecase->id == USECASE_AUDIO_RECORD_VOIP)
1203 usecase->stream.in->app_type_cfg.sample_rate = usecase->stream.in->sample_rate;
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301204 if (voice_is_in_call_rec_stream(usecase->stream.in)) {
1205 audio_extn_btsco_get_sample_rate(usecase->in_snd_device, &usecase->stream.in->app_type_cfg.sample_rate);
1206 } else {
1207 audio_extn_btsco_get_sample_rate(snd_device, &usecase->stream.in->app_type_cfg.sample_rate);
1208 }
Deeraj Somanfc791e72018-11-30 13:23:01 +05301209 if (usecase->stream.in->device & AUDIO_DEVICE_IN_BLUETOOTH_A2DP & ~AUDIO_DEVICE_BIT_IN) {
Florian Pfister1a84f312018-07-19 14:38:18 +02001210 audio_extn_a2dp_get_dec_sample_rate(&usecase->stream.in->app_type_cfg.sample_rate);
1211 ALOGI("%s using %d sample rate rate for A2DP dec CoPP",
1212 __func__, usecase->stream.in->app_type_cfg.sample_rate);
1213 }
Siena Richard7c2db772016-12-21 11:32:34 -08001214 sample_rate = usecase->stream.in->app_type_cfg.sample_rate;
1215 app_type_cfg[len++] = sample_rate;
1216 if (snd_device_be_idx > 0)
1217 app_type_cfg[len++] = snd_device_be_idx;
1218 ALOGI("%s CAPTURE app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
1219 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301220 } else {
Siena Richard7c2db772016-12-21 11:32:34 -08001221 app_type = platform_get_default_app_type_v2(adev->platform, usecase->type);
Surendar Karka93cd25a2018-08-28 14:21:37 +05301222 if(usecase->type == TRANSCODE_LOOPBACK_RX) {
Siddartha Shaik343abc62017-08-08 11:15:25 +05301223 sample_rate = usecase->stream.inout->out_config.sample_rate;
1224 app_type = usecase->stream.inout->out_app_type_cfg.app_type;
1225 }
Siena Richard7c2db772016-12-21 11:32:34 -08001226 app_type_cfg[len++] = app_type;
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301227 app_type_cfg[len++] = acdb_dev_id;
1228 app_type_cfg[len++] = sample_rate;
Siena Richard7c2db772016-12-21 11:32:34 -08001229 if (snd_device_be_idx > 0)
1230 app_type_cfg[len++] = snd_device_be_idx;
1231 ALOGI("%s default app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
1232 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Ashish Jainc02430d2016-01-04 10:42:43 +05301233 }
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301234
Siddartha Shaik343abc62017-08-08 11:15:25 +05301235 if(ctl)
1236 mixer_ctl_set_array(ctl, app_type_cfg, len);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001237 rc = 0;
1238exit_send_app_type_cfg:
1239 return rc;
1240}
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001241
Siena Richard7c2db772016-12-21 11:32:34 -08001242int audio_extn_utils_send_app_type_cfg(struct audio_device *adev,
1243 struct audio_usecase *usecase)
1244{
1245 int i, num_devices = 0;
1246 snd_device_t new_snd_devices[SND_DEVICE_OUT_END] = {0};
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301247 snd_device_t in_snd_device = usecase->in_snd_device;
Siena Richard7c2db772016-12-21 11:32:34 -08001248 int rc = 0;
1249
Aalique Grahame22e49102018-12-18 14:23:57 -08001250 if (usecase->type == PCM_HFP_CALL) {
1251 return audio_extn_utils_send_app_type_cfg_hfp(adev, usecase);
1252 }
1253
Siena Richard7c2db772016-12-21 11:32:34 -08001254 switch (usecase->type) {
1255 case PCM_PLAYBACK:
Surendar Karka93cd25a2018-08-28 14:21:37 +05301256 case TRANSCODE_LOOPBACK_RX:
Siena Richard7c2db772016-12-21 11:32:34 -08001257 ALOGD("%s: usecase->out_snd_device %s",
1258 __func__, platform_get_snd_device_name(usecase->out_snd_device));
1259 /* check for out combo device */
1260 if (platform_split_snd_device(adev->platform,
1261 usecase->out_snd_device,
1262 &num_devices, new_snd_devices)) {
1263 new_snd_devices[0] = usecase->out_snd_device;
1264 num_devices = 1;
1265 }
1266 break;
1267 case PCM_CAPTURE:
1268 ALOGD("%s: usecase->in_snd_device %s",
1269 __func__, platform_get_snd_device_name(usecase->in_snd_device));
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301270 if (voice_is_in_call_rec_stream(usecase->stream.in)) {
1271 in_snd_device = voice_get_incall_rec_backend_device(usecase->stream.in);
1272 }
Siena Richard7c2db772016-12-21 11:32:34 -08001273 /* check for in combo device */
1274 if (platform_split_snd_device(adev->platform,
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301275 in_snd_device,
Siena Richard7c2db772016-12-21 11:32:34 -08001276 &num_devices, new_snd_devices)) {
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301277 new_snd_devices[0] = in_snd_device;
Siena Richard7c2db772016-12-21 11:32:34 -08001278 num_devices = 1;
1279 }
1280 break;
1281 default:
1282 ALOGI("%s: not a playback/capture path, no need to cfg app type", __func__);
1283 rc = 0;
1284 break;
1285 }
1286
1287 for (i = 0; i < num_devices; i++) {
1288 rc = send_app_type_cfg_for_device(adev, usecase, new_snd_devices[i]);
1289 if (rc)
1290 break;
1291 }
1292
1293 return rc;
1294}
1295
Preetam Singh Ranawat9519e9c2015-11-18 16:05:55 +05301296int read_line_from_file(const char *path, char *buf, size_t count)
1297{
1298 char * fgets_ret;
1299 FILE * fd;
1300 int rv;
1301
1302 fd = fopen(path, "r");
1303 if (fd == NULL)
1304 return -1;
1305
1306 fgets_ret = fgets(buf, (int)count, fd);
1307 if (NULL != fgets_ret) {
1308 rv = (int)strlen(buf);
1309 } else {
1310 rv = ferror(fd);
1311 }
1312 fclose(fd);
1313
1314 return rv;
1315}
1316
Ashish Jainf1eaa582016-05-23 20:54:24 +05301317/*Translates ALSA formats to AOSP PCM formats*/
1318audio_format_t alsa_format_to_hal(uint32_t alsa_format)
1319{
1320 audio_format_t format;
1321
1322 switch(alsa_format) {
1323 case SNDRV_PCM_FORMAT_S16_LE:
1324 format = AUDIO_FORMAT_PCM_16_BIT;
1325 break;
1326 case SNDRV_PCM_FORMAT_S24_3LE:
1327 format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
1328 break;
1329 case SNDRV_PCM_FORMAT_S24_LE:
1330 format = AUDIO_FORMAT_PCM_8_24_BIT;
1331 break;
1332 case SNDRV_PCM_FORMAT_S32_LE:
1333 format = AUDIO_FORMAT_PCM_32_BIT;
1334 break;
1335 default:
1336 ALOGW("Incorrect ALSA format");
1337 format = AUDIO_FORMAT_INVALID;
1338 }
1339 return format;
1340}
1341
1342/*Translates hal format (AOSP) to alsa formats*/
1343uint32_t hal_format_to_alsa(audio_format_t hal_format)
1344{
1345 uint32_t alsa_format;
1346
1347 switch (hal_format) {
1348 case AUDIO_FORMAT_PCM_32_BIT: {
1349 if (platform_supports_true_32bit())
1350 alsa_format = SNDRV_PCM_FORMAT_S32_LE;
1351 else
1352 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1353 }
1354 break;
1355 case AUDIO_FORMAT_PCM_8_BIT:
1356 alsa_format = SNDRV_PCM_FORMAT_S8;
1357 break;
1358 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
1359 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1360 break;
1361 case AUDIO_FORMAT_PCM_8_24_BIT: {
1362 if (platform_supports_true_32bit())
1363 alsa_format = SNDRV_PCM_FORMAT_S32_LE;
1364 else
1365 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1366 }
1367 break;
1368 case AUDIO_FORMAT_PCM_FLOAT:
1369 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1370 break;
1371 default:
1372 case AUDIO_FORMAT_PCM_16_BIT:
1373 alsa_format = SNDRV_PCM_FORMAT_S16_LE;
1374 break;
1375 }
1376 return alsa_format;
1377}
1378
Ashish Jain83a6cc22016-06-28 14:34:17 +05301379/*Translates PCM formats to AOSP formats*/
1380audio_format_t pcm_format_to_hal(uint32_t pcm_format)
1381{
1382 audio_format_t format = AUDIO_FORMAT_INVALID;
1383
1384 switch(pcm_format) {
1385 case PCM_FORMAT_S16_LE:
1386 format = AUDIO_FORMAT_PCM_16_BIT;
1387 break;
1388 case PCM_FORMAT_S24_3LE:
1389 format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
1390 break;
1391 case PCM_FORMAT_S24_LE:
1392 format = AUDIO_FORMAT_PCM_8_24_BIT;
1393 break;
1394 case PCM_FORMAT_S32_LE:
1395 format = AUDIO_FORMAT_PCM_32_BIT;
1396 break;
1397 default:
1398 ALOGW("Incorrect PCM format");
1399 format = AUDIO_FORMAT_INVALID;
1400 }
1401 return format;
1402}
1403
1404/*Translates hal format (AOSP) to alsa formats*/
1405uint32_t hal_format_to_pcm(audio_format_t hal_format)
1406{
1407 uint32_t pcm_format;
1408
1409 switch (hal_format) {
1410 case AUDIO_FORMAT_PCM_32_BIT:
1411 case AUDIO_FORMAT_PCM_8_24_BIT:
1412 case AUDIO_FORMAT_PCM_FLOAT: {
1413 if (platform_supports_true_32bit())
1414 pcm_format = PCM_FORMAT_S32_LE;
1415 else
1416 pcm_format = PCM_FORMAT_S24_3LE;
1417 }
1418 break;
1419 case AUDIO_FORMAT_PCM_8_BIT:
1420 pcm_format = PCM_FORMAT_S8;
1421 break;
1422 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
1423 pcm_format = PCM_FORMAT_S24_3LE;
1424 break;
1425 default:
1426 case AUDIO_FORMAT_PCM_16_BIT:
1427 pcm_format = PCM_FORMAT_S16_LE;
1428 break;
1429 }
1430 return pcm_format;
1431}
1432
Ashish Jainf1eaa582016-05-23 20:54:24 +05301433uint32_t get_alsa_fragment_size(uint32_t bytes_per_sample,
1434 uint32_t sample_rate,
1435 uint32_t noOfChannels)
1436{
1437 uint32_t fragment_size = 0;
1438 uint32_t pcm_offload_time = PCM_OFFLOAD_BUFFER_DURATION;
1439
1440 fragment_size = (pcm_offload_time
1441 * sample_rate
1442 * bytes_per_sample
1443 * noOfChannels)/1000;
1444 if (fragment_size < MIN_PCM_OFFLOAD_FRAGMENT_SIZE)
1445 fragment_size = MIN_PCM_OFFLOAD_FRAGMENT_SIZE;
1446 else if (fragment_size > MAX_PCM_OFFLOAD_FRAGMENT_SIZE)
1447 fragment_size = MAX_PCM_OFFLOAD_FRAGMENT_SIZE;
1448 /*To have same PCM samples for all channels, the buffer size requires to
1449 *be multiple of (number of channels * bytes per sample)
1450 *For writes to succeed, the buffer must be written at address which is multiple of 32
1451 */
Aalique Grahameb85f2d92017-10-16 17:53:23 -07001452 fragment_size = ALIGN(fragment_size, (bytes_per_sample * noOfChannels * 32));
Ashish Jainf1eaa582016-05-23 20:54:24 +05301453
1454 ALOGI("PCM offload Fragment size to %d bytes", fragment_size);
1455 return fragment_size;
1456}
1457
1458/* Calculates the fragment size required to configure compress session.
1459 * Based on the alsa format selected, decide if conversion is needed in
1460
1461 * HAL ( e.g. convert AUDIO_FORMAT_PCM_FLOAT input format to
1462 * AUDIO_FORMAT_PCM_24_BIT_PACKED before writing to the compress driver.
1463 */
1464void audio_extn_utils_update_direct_pcm_fragment_size(struct stream_out *out)
1465{
Ashish Jain83a6cc22016-06-28 14:34:17 +05301466 audio_format_t dst_format = out->hal_op_format;
1467 audio_format_t src_format = out->hal_ip_format;
Ashish Jainf1eaa582016-05-23 20:54:24 +05301468 uint32_t hal_op_bytes_per_sample = audio_bytes_per_sample(dst_format);
1469 uint32_t hal_ip_bytes_per_sample = audio_bytes_per_sample(src_format);
1470
1471 out->compr_config.fragment_size =
1472 get_alsa_fragment_size(hal_op_bytes_per_sample,
1473 out->sample_rate,
1474 popcount(out->channel_mask));
1475
1476 if ((src_format != dst_format) &&
1477 hal_op_bytes_per_sample != hal_ip_bytes_per_sample) {
1478
Ashish Jain83a6cc22016-06-28 14:34:17 +05301479 out->hal_fragment_size =
Ashish Jainf1eaa582016-05-23 20:54:24 +05301480 ((out->compr_config.fragment_size * hal_ip_bytes_per_sample) /
1481 hal_op_bytes_per_sample);
1482 ALOGI("enable conversion hal_input_fragment_size is %d src_format %x dst_format %x",
Ashish Jain83a6cc22016-06-28 14:34:17 +05301483 out->hal_fragment_size, src_format, dst_format);
Ashish Jainf1eaa582016-05-23 20:54:24 +05301484 } else {
Ashish Jain83a6cc22016-06-28 14:34:17 +05301485 out->hal_fragment_size = out->compr_config.fragment_size;
Ashish Jainf1eaa582016-05-23 20:54:24 +05301486 }
1487}
1488
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301489/* converts pcm format 24_8 to 8_24 inplace */
1490size_t audio_extn_utils_convert_format_24_8_to_8_24(void *buf, size_t bytes)
1491{
1492 size_t i = 0;
1493 int *int_buf_stream = buf;
1494
1495 if ((bytes % 4) != 0) {
1496 ALOGE("%s: wrong inout buffer! ... is not 32 bit aligned ", __func__);
1497 return -EINVAL;
1498 }
1499
1500 for (; i < (bytes / 4); i++)
1501 int_buf_stream[i] >>= 8;
1502
1503 return bytes;
1504}
1505
1506int get_snd_codec_id(audio_format_t format)
1507{
1508 int id = 0;
1509
1510 switch (format & AUDIO_FORMAT_MAIN_MASK) {
1511 case AUDIO_FORMAT_MP3:
1512 id = SND_AUDIOCODEC_MP3;
1513 break;
1514 case AUDIO_FORMAT_AAC:
1515 id = SND_AUDIOCODEC_AAC;
1516 break;
1517 case AUDIO_FORMAT_AAC_ADTS:
1518 id = SND_AUDIOCODEC_AAC;
1519 break;
Arun Kumar Dasari3b174182016-12-27 13:01:14 +05301520 case AUDIO_FORMAT_AAC_LATM:
1521 id = SND_AUDIOCODEC_AAC;
1522 break;
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301523 case AUDIO_FORMAT_PCM:
1524 id = SND_AUDIOCODEC_PCM;
1525 break;
1526 case AUDIO_FORMAT_FLAC:
1527 id = SND_AUDIOCODEC_FLAC;
1528 break;
1529 case AUDIO_FORMAT_ALAC:
1530 id = SND_AUDIOCODEC_ALAC;
1531 break;
1532 case AUDIO_FORMAT_APE:
1533 id = SND_AUDIOCODEC_APE;
1534 break;
1535 case AUDIO_FORMAT_VORBIS:
1536 id = SND_AUDIOCODEC_VORBIS;
1537 break;
1538 case AUDIO_FORMAT_WMA:
1539 id = SND_AUDIOCODEC_WMA;
1540 break;
1541 case AUDIO_FORMAT_WMA_PRO:
1542 id = SND_AUDIOCODEC_WMA_PRO;
1543 break;
Satish Babu Patakokila0c313922016-12-08 12:07:08 +05301544 case AUDIO_FORMAT_MP2:
1545 id = SND_AUDIOCODEC_MP2;
1546 break;
Satish Babu Patakokila915ecba2017-01-10 17:43:56 +05301547 case AUDIO_FORMAT_AC3:
1548 id = SND_AUDIOCODEC_AC3;
1549 break;
1550 case AUDIO_FORMAT_E_AC3:
1551 case AUDIO_FORMAT_E_AC3_JOC:
1552 id = SND_AUDIOCODEC_EAC3;
1553 break;
1554 case AUDIO_FORMAT_DTS:
1555 case AUDIO_FORMAT_DTS_HD:
1556 id = SND_AUDIOCODEC_DTS;
1557 break;
Ben Romberger1aaaf862017-04-06 17:49:46 -07001558 case AUDIO_FORMAT_DOLBY_TRUEHD:
1559 id = SND_AUDIOCODEC_TRUEHD;
1560 break;
Naresh Tanniru928f0862017-04-07 16:44:23 -07001561 case AUDIO_FORMAT_IEC61937:
1562 id = SND_AUDIOCODEC_IEC61937;
1563 break;
Preetam Singh Ranawat4277a5a2017-01-18 19:02:24 +05301564 case AUDIO_FORMAT_DSD:
1565 id = SND_AUDIOCODEC_DSD;
1566 break;
Dhanalakshmi Siddani18737932016-11-29 17:33:17 +05301567 case AUDIO_FORMAT_APTX:
1568 id = SND_AUDIOCODEC_APTX;
1569 break;
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301570 default:
1571 ALOGE("%s: Unsupported audio format :%x", __func__, format);
1572 }
1573
1574 return id;
1575}
1576
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001577void audio_extn_utils_send_audio_calibration(struct audio_device *adev,
1578 struct audio_usecase *usecase)
1579{
1580 int type = usecase->type;
1581
Dhananjay Kumar14245982017-01-16 20:21:00 +05301582 if (type == PCM_PLAYBACK && usecase->stream.out != NULL) {
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001583 struct stream_out *out = usecase->stream.out;
1584 int snd_device = usecase->out_snd_device;
1585 snd_device = (snd_device == SND_DEVICE_OUT_SPEAKER) ?
Xiaojun Sang040cc9f2015-08-03 19:38:28 +08001586 platform_get_spkr_prot_snd_device(snd_device) : snd_device;
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05301587 platform_send_audio_calibration(adev->platform, usecase,
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001588 out->app_type_cfg.app_type,
Preetam Singh Ranawat61716b12015-12-14 11:55:24 +05301589 usecase->stream.out->app_type_cfg.sample_rate);
Dhananjay Kumar14245982017-01-16 20:21:00 +05301590 } else if (type == PCM_CAPTURE && usecase->stream.in != NULL) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301591 platform_send_audio_calibration(adev->platform, usecase,
1592 usecase->stream.in->app_type_cfg.app_type,
1593 usecase->stream.in->app_type_cfg.sample_rate);
Vidyakumar Athota653aaef2017-03-16 11:11:31 -07001594 } else if (type == PCM_HFP_CALL || type == PCM_CAPTURE) {
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05301595 /* when app type is default. the sample rate is not used to send cal */
1596 platform_send_audio_calibration(adev->platform, usecase,
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301597 platform_get_default_app_type_v2(adev->platform, usecase->type),
1598 48000);
Surendar Karka93cd25a2018-08-28 14:21:37 +05301599 } else if (type == TRANSCODE_LOOPBACK_RX && usecase->stream.inout != NULL) {
Siddartha Shaik343abc62017-08-08 11:15:25 +05301600 int snd_device = usecase->out_snd_device;
1601 snd_device = (snd_device == SND_DEVICE_OUT_SPEAKER) ?
1602 platform_get_spkr_prot_snd_device(snd_device) : snd_device;
1603 platform_send_audio_calibration(adev->platform, usecase,
1604 platform_get_default_app_type_v2(adev->platform, usecase->type),
1605 usecase->stream.inout->out_config.sample_rate);
Vidyakumar Athotae57f6002017-03-01 13:13:16 -08001606 } else {
1607 /* No need to send audio calibration for voice and voip call usecases */
1608 if ((type != VOICE_CALL) && (type != VOIP_CALL))
1609 ALOGW("%s: No audio calibration for usecase type = %d", __func__, type);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001610 }
1611}
1612
Ben Rombergera04fabc2014-11-14 12:16:03 -08001613// Base64 Encode and Decode
1614// Not all features supported. This must be used only with following conditions.
1615// Decode Modes: Support with and without padding
1616// CRLF not handling. So no CRLF in string to decode.
1617// Encode Modes: Supports only padding
1618int b64decode(char *inp, int ilen, uint8_t* outp)
1619{
1620 int i, j, k, ii, num;
1621 int rem, pcnt;
1622 uint32_t res=0;
1623 uint8_t getIndex[MAX_BASEINDEX_LEN];
1624 uint8_t tmp, cflag;
1625
1626 if(inp == NULL || outp == NULL || ilen <= 0) {
1627 ALOGE("[%s] received NULL pointer or zero length",__func__);
1628 return -1;
1629 }
1630
1631 memset(getIndex, MAX_BASEINDEX_LEN-1, sizeof(getIndex));
1632 for(i=0;i<BASE_TABLE_SIZE;i++) {
1633 getIndex[(uint8_t)bTable[i]] = (uint8_t)i;
1634 }
1635 getIndex[(uint8_t)'=']=0;
1636
1637 j=0;k=0;
1638 num = ilen/4;
1639 rem = ilen%4;
1640 if(rem==0)
1641 num = num-1;
1642 cflag=0;
1643 for(i=0; i<num; i++) {
1644 res=0;
1645 for(ii=0;ii<4;ii++) {
1646 res = res << 6;
1647 tmp = getIndex[(uint8_t)inp[j++]];
1648 res = res | tmp;
1649 cflag = cflag | tmp;
1650 }
1651 outp[k++] = (res >> 16)&0xFF;
1652 outp[k++] = (res >> 8)&0xFF;
1653 outp[k++] = res & 0xFF;
1654 }
1655
1656 // Handle last bytes special
1657 pcnt=0;
1658 if(rem == 0) {
1659 //With padding or full data
1660 res = 0;
1661 for(ii=0;ii<4;ii++) {
1662 if(inp[j] == '=')
1663 pcnt++;
1664 res = res << 6;
1665 tmp = getIndex[(uint8_t)inp[j++]];
1666 res = res | tmp;
1667 cflag = cflag | tmp;
1668 }
1669 outp[k++] = res >> 16;
1670 if(pcnt == 2)
1671 goto done;
1672 outp[k++] = (res>>8)&0xFF;
1673 if(pcnt == 1)
1674 goto done;
1675 outp[k++] = res&0xFF;
1676 } else {
1677 //without padding
1678 res = 0;
1679 for(i=0;i<rem;i++) {
1680 res = res << 6;
1681 tmp = getIndex[(uint8_t)inp[j++]];
1682 res = res | tmp;
1683 cflag = cflag | tmp;
1684 }
1685 for(i=rem;i<4;i++) {
1686 res = res << 6;
1687 pcnt++;
1688 }
1689 outp[k++] = res >> 16;
1690 if(pcnt == 2)
1691 goto done;
1692 outp[k++] = (res>>8)&0xFF;
1693 if(pcnt == 1)
1694 goto done;
1695 outp[k++] = res&0xFF;
1696 }
1697done:
1698 if(cflag == 0xFF) {
1699 ALOGE("[%s] base64 decode failed. Invalid character found %s",
1700 __func__, inp);
1701 return 0;
1702 }
1703 return k;
1704}
1705
1706int b64encode(uint8_t *inp, int ilen, char* outp)
1707{
1708 int i,j,k, num;
1709 int rem=0;
1710 uint32_t res=0;
1711
1712 if(inp == NULL || outp == NULL || ilen<=0) {
1713 ALOGE("[%s] received NULL pointer or zero input length",__func__);
1714 return -1;
1715 }
1716
1717 num = ilen/3;
1718 rem = ilen%3;
1719 j=0;k=0;
1720 for(i=0; i<num; i++) {
1721 //prepare index
1722 res = inp[j++]<<16;
1723 res = res | inp[j++]<<8;
1724 res = res | inp[j++];
1725 //get output map from index
1726 outp[k++] = (char) bTable[(res>>18)&0x3F];
1727 outp[k++] = (char) bTable[(res>>12)&0x3F];
1728 outp[k++] = (char) bTable[(res>>6)&0x3F];
1729 outp[k++] = (char) bTable[res&0x3F];
1730 }
1731
1732 switch(rem) {
1733 case 1:
1734 res = inp[j++]<<16;
1735 outp[k++] = (char) bTable[res>>18];
1736 outp[k++] = (char) bTable[(res>>12)&0x3F];
1737 //outp[k++] = '=';
1738 //outp[k++] = '=';
1739 break;
1740 case 2:
1741 res = inp[j++]<<16;
1742 res = res | inp[j++]<<8;
1743 outp[k++] = (char) bTable[res>>18];
1744 outp[k++] = (char) bTable[(res>>12)&0x3F];
1745 outp[k++] = (char) bTable[(res>>6)&0x3F];
1746 //outp[k++] = '=';
1747 break;
1748 default:
1749 break;
1750 }
Ben Rombergera04fabc2014-11-14 12:16:03 -08001751 outp[k] = '\0';
1752 return k;
1753}
Ashish Jain81eb2a82015-05-13 10:52:34 +05301754
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301755
1756int audio_extn_utils_get_codec_version(const char *snd_card_name,
1757 int card_num,
1758 char *codec_version)
1759{
1760 char procfs_path[50];
1761 FILE *fp;
1762
1763 if (strstr(snd_card_name, "tasha")) {
1764 snprintf(procfs_path, sizeof(procfs_path),
1765 "/proc/asound/card%d/codecs/tasha/version", card_num);
1766 if ((fp = fopen(procfs_path, "r")) != NULL) {
1767 fgets(codec_version, CODEC_VERSION_MAX_LENGTH, fp);
1768 fclose(fp);
1769 } else {
1770 ALOGE("%s: ERROR. cannot open %s", __func__, procfs_path);
1771 return -ENOENT;
1772 }
1773 ALOGD("%s: codec version %s", __func__, codec_version);
1774 }
1775
1776 return 0;
1777}
1778
1779
Ashish Jain81eb2a82015-05-13 10:52:34 +05301780#ifdef AUDIO_EXTERNAL_HDMI_ENABLED
1781
1782void get_default_compressed_channel_status(
1783 unsigned char *channel_status)
1784{
Ashish Jain81eb2a82015-05-13 10:52:34 +05301785 memset(channel_status,0,24);
1786
1787 /* block start bit in preamble bit 3 */
1788 channel_status[0] |= PROFESSIONAL;
1789 //compre out
1790 channel_status[0] |= NON_LPCM;
1791 // sample rate; fixed 48K for default/transcode
1792 channel_status[3] |= SR_48000;
1793}
1794
Ashish Jain81eb2a82015-05-13 10:52:34 +05301795int32_t get_compressed_channel_status(void *audio_stream_data,
1796 uint32_t audio_frame_size,
1797 unsigned char *channel_status,
1798 enum audio_parser_code_type codec_type)
1799 // codec_type - AUDIO_PARSER_CODEC_AC3
1800 // - AUDIO_PARSER_CODEC_DTS
1801{
1802 unsigned char *stream;
1803 int ret = 0;
1804 stream = (unsigned char *)audio_stream_data;
1805
1806 if (audio_stream_data == NULL || audio_frame_size == 0) {
1807 ALOGW("no buffer to get channel status, return default for compress");
1808 get_default_compressed_channel_status(channel_status);
1809 return ret;
1810 }
1811
1812 memset(channel_status,0,24);
1813 if(init_audio_parser(stream, audio_frame_size, codec_type) == -1)
1814 {
1815 ALOGE("init audio parser failed");
1816 return -1;
1817 }
1818 ret = get_channel_status(channel_status, codec_type);
1819 return ret;
1820
1821}
1822
Ashish Jain81eb2a82015-05-13 10:52:34 +05301823void get_lpcm_channel_status(uint32_t sampleRate,
1824 unsigned char *channel_status)
1825{
1826 int32_t status = 0;
Ashish Jain81eb2a82015-05-13 10:52:34 +05301827 memset(channel_status,0,24);
1828 /* block start bit in preamble bit 3 */
1829 channel_status[0] |= PROFESSIONAL;
1830 //LPCM OUT
1831 channel_status[0] &= ~NON_LPCM;
1832
1833 switch (sampleRate) {
1834 case 8000:
1835 case 11025:
1836 case 12000:
1837 case 16000:
1838 case 22050:
1839 channel_status[3] |= SR_NOTID;
Preetam Singh Ranawat61716b12015-12-14 11:55:24 +05301840 break;
Ashish Jain81eb2a82015-05-13 10:52:34 +05301841 case 24000:
1842 channel_status[3] |= SR_24000;
1843 break;
1844 case 32000:
1845 channel_status[3] |= SR_32000;
1846 break;
1847 case 44100:
1848 channel_status[3] |= SR_44100;
1849 break;
1850 case 48000:
1851 channel_status[3] |= SR_48000;
1852 break;
1853 case 88200:
1854 channel_status[3] |= SR_88200;
1855 break;
1856 case 96000:
1857 channel_status[3] |= SR_96000;
1858 break;
1859 case 176400:
1860 channel_status[3] |= SR_176400;
1861 break;
1862 case 192000:
1863 channel_status[3] |= SR_192000;
1864 break;
1865 default:
1866 ALOGV("Invalid sample_rate %u\n", sampleRate);
1867 status = -1;
1868 break;
1869 }
1870}
1871
1872void audio_utils_set_hdmi_channel_status(struct stream_out *out, char * buffer, size_t bytes)
1873{
1874 unsigned char channel_status[24]={0};
1875 struct snd_aes_iec958 iec958;
1876 const char *mixer_ctl_name = "IEC958 Playback PCM Stream";
1877 struct mixer_ctl *ctl;
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +05301878 ALOGV("%s: buffer %s bytes %zd", __func__, buffer, bytes);
Arun Mirpurie008ed22019-03-21 11:21:04 -07001879
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05301880 if (audio_extn_utils_is_dolby_format(out->format) &&
Ashish Jain81eb2a82015-05-13 10:52:34 +05301881 /*TODO:Extend code to support DTS passthrough*/
1882 /*set compressed channel status bits*/
Arun Mirpurie008ed22019-03-21 11:21:04 -07001883 audio_extn_passthru_is_passthrough_stream(out) &&
1884 audio_extn_is_hdmi_passthru_enabled()) {
Ashish Jain81eb2a82015-05-13 10:52:34 +05301885 get_compressed_channel_status(buffer, bytes, channel_status, AUDIO_PARSER_CODEC_AC3);
Arun Mirpurie008ed22019-03-21 11:21:04 -07001886 } else {
Ashish Jain81eb2a82015-05-13 10:52:34 +05301887 /*set channel status bit for LPCM*/
1888 get_lpcm_channel_status(out->sample_rate, channel_status);
1889 }
1890
1891 memcpy(iec958.status, channel_status,sizeof(iec958.status));
1892 ctl = mixer_get_ctl_by_name(out->dev->mixer, mixer_ctl_name);
1893 if (!ctl) {
1894 ALOGE("%s: Could not get ctl for mixer cmd - %s",
1895 __func__, mixer_ctl_name);
1896 return;
1897 }
1898 if (mixer_ctl_set_array(ctl, &iec958, sizeof(iec958)) < 0) {
1899 ALOGE("%s: Could not set channel status for ext HDMI ",
1900 __func__);
1901 return;
1902 }
1903
1904}
1905#endif
Manish Dewangan3ccdea52017-02-13 19:31:54 +05301906
1907int audio_extn_utils_get_avt_device_drift(
1908 struct audio_usecase *usecase,
1909 struct audio_avt_device_drift_param *drift_param)
1910{
1911 int ret = 0, count = 0;
1912 char avt_device_drift_mixer_ctl_name[MIXER_PATH_MAX_LENGTH] = {0};
Naresh Tanniru6160c712017-04-17 15:43:48 +05301913 const char *backend = NULL;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05301914 struct mixer_ctl *ctl = NULL;
1915 struct audio_avt_device_drift_stats drift_stats;
1916 struct audio_device *adev = NULL;
1917
1918 if (usecase != NULL && usecase->type == PCM_PLAYBACK) {
Naresh Tanniru6160c712017-04-17 15:43:48 +05301919 backend = platform_get_snd_device_backend_interface(usecase->out_snd_device);
1920 if (!backend) {
1921 ALOGE("%s: Unsupported device %d", __func__,
1922 usecase->stream.out->devices);
1923 ret = -EINVAL;
1924 goto done;
1925 }
1926 strlcpy(avt_device_drift_mixer_ctl_name,
1927 backend,
1928 MIXER_PATH_MAX_LENGTH);
1929
1930 count = strlen(backend);
1931 if (MIXER_PATH_MAX_LENGTH - count > 0) {
1932 strlcat(&avt_device_drift_mixer_ctl_name[count],
1933 " DRIFT",
1934 MIXER_PATH_MAX_LENGTH - count);
1935 } else {
1936 ret = -EINVAL;
1937 goto done;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05301938 }
1939 } else {
Naresh Tanniru7586e292017-04-13 10:38:13 +05301940 ALOGE("%s: Invalid usecase",__func__);
Manish Dewangan3ccdea52017-02-13 19:31:54 +05301941 ret = -EINVAL;
Naresh Tanniru6160c712017-04-17 15:43:48 +05301942 goto done;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05301943 }
1944
Naresh Tanniru6160c712017-04-17 15:43:48 +05301945 adev = usecase->stream.out->dev;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05301946 ctl = mixer_get_ctl_by_name(adev->mixer, avt_device_drift_mixer_ctl_name);
1947 if (!ctl) {
1948 ALOGE("%s: Could not get ctl for mixer cmd - %s",
1949 __func__, avt_device_drift_mixer_ctl_name);
1950
1951 ret = -EINVAL;
1952 goto done;
1953 }
1954
1955 ALOGV("%s: Getting AV Timer vs Device Drift mixer ctrl name %s", __func__,
1956 avt_device_drift_mixer_ctl_name);
1957
1958 mixer_ctl_update(ctl);
1959 count = mixer_ctl_get_num_values(ctl);
1960 if (count != sizeof(struct audio_avt_device_drift_stats)) {
1961 ALOGE("%s: mixer_ctl_get_num_values() invalid drift_stats data size",
1962 __func__);
1963
1964 ret = -EINVAL;
1965 goto done;
1966 }
1967
1968 ret = mixer_ctl_get_array(ctl, (void *)&drift_stats, count);
1969 if (ret != 0) {
1970 ALOGE("%s: mixer_ctl_get_array() failed to get drift_stats Params",
1971 __func__);
1972
1973 ret = -EINVAL;
1974 goto done;
1975 }
1976 memcpy(drift_param, &drift_stats.drift_param,
1977 sizeof(struct audio_avt_device_drift_param));
1978done:
1979 return ret;
1980}
Manish Dewangan07de2142017-02-27 19:27:20 +05301981
1982#ifdef SNDRV_COMPRESS_PATH_DELAY
1983int audio_extn_utils_compress_get_dsp_latency(struct stream_out *out)
1984{
1985 int ret = -EINVAL;
1986 struct snd_compr_metadata metadata;
1987 int delay_ms = COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
1988
Weiyin Jiangc49a3b52018-08-17 16:16:08 +08001989 /* override the latency for pcm offload use case */
1990 if ((out->flags & AUDIO_OUTPUT_FLAG_DIRECT) &&
1991 !(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)) {
1992 delay_ms = PCM_OFFLOAD_PLAYBACK_LATENCY;
1993 }
1994
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -07001995 if (property_get_bool("vendor.audio.playback.dsp.pathdelay", false)) {
Manish Dewangan07de2142017-02-27 19:27:20 +05301996 ALOGD("%s:: Quering DSP delay %d",__func__, __LINE__);
1997 if (!(is_offload_usecase(out->usecase))) {
1998 ALOGE("%s:: not supported for non offload session", __func__);
1999 goto exit;
2000 }
2001
2002 if (!out->compr) {
2003 ALOGD("%s:: Invalid compress handle,returning default dsp latency",
2004 __func__);
2005 goto exit;
2006 }
2007
2008 metadata.key = SNDRV_COMPRESS_PATH_DELAY;
2009 ret = compress_get_metadata(out->compr, &metadata);
2010 if(ret) {
2011 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2012 goto exit;
2013 }
2014 delay_ms = metadata.value[0] / 1000; /*convert to ms*/
2015 } else {
2016 ALOGD("%s:: Using Fix DSP delay",__func__);
2017 }
2018
2019exit:
2020 ALOGD("%s:: delay in ms is %d",__func__, delay_ms);
2021 return delay_ms;
2022}
2023#else
2024int audio_extn_utils_compress_get_dsp_latency(struct stream_out *out __unused)
2025{
2026 return COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
2027}
2028#endif
Manish Dewangan69426c82017-01-30 17:35:36 +05302029
2030#ifdef SNDRV_COMPRESS_RENDER_MODE
2031int audio_extn_utils_compress_set_render_mode(struct stream_out *out)
2032{
2033 struct snd_compr_metadata metadata;
2034 int ret = -EINVAL;
2035
2036 if (!(is_offload_usecase(out->usecase))) {
2037 ALOGE("%s:: not supported for non offload session", __func__);
2038 goto exit;
2039 }
2040
2041 if (!out->compr) {
2042 ALOGD("%s:: Invalid compress handle",
2043 __func__);
2044 goto exit;
2045 }
2046
2047 ALOGD("%s:: render mode %d", __func__, out->render_mode);
2048
2049 metadata.key = SNDRV_COMPRESS_RENDER_MODE;
2050 if (out->render_mode == RENDER_MODE_AUDIO_MASTER) {
2051 metadata.value[0] = SNDRV_COMPRESS_RENDER_MODE_AUDIO_MASTER;
2052 } else if (out->render_mode == RENDER_MODE_AUDIO_STC_MASTER) {
2053 metadata.value[0] = SNDRV_COMPRESS_RENDER_MODE_STC_MASTER;
2054 } else {
2055 ret = 0;
2056 goto exit;
2057 }
2058 ret = compress_set_metadata(out->compr, &metadata);
2059 if(ret) {
2060 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2061 }
2062exit:
2063 return ret;
2064}
2065#else
2066int audio_extn_utils_compress_set_render_mode(struct stream_out *out __unused)
2067{
2068 ALOGD("%s:: configuring render mode not supported", __func__);
2069 return 0;
2070}
2071#endif
Manish Dewangan58229382017-02-02 15:48:41 +05302072
2073#ifdef SNDRV_COMPRESS_CLK_REC_MODE
2074int audio_extn_utils_compress_set_clk_rec_mode(
2075 struct audio_usecase *usecase)
2076{
2077 struct snd_compr_metadata metadata;
2078 struct stream_out *out = NULL;
2079 int ret = -EINVAL;
2080
Naresh Tanniru7586e292017-04-13 10:38:13 +05302081 if (usecase == NULL || usecase->type != PCM_PLAYBACK) {
Manish Dewangan58229382017-02-02 15:48:41 +05302082 ALOGE("%s:: Invalid use case", __func__);
2083 goto exit;
2084 }
2085
2086 out = usecase->stream.out;
2087 if (!out) {
2088 ALOGE("%s:: invalid stream", __func__);
2089 goto exit;
2090 }
2091
2092 if (!is_offload_usecase(out->usecase)) {
2093 ALOGE("%s:: not supported for non offload session", __func__);
2094 goto exit;
2095 }
2096
2097 if (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER) {
2098 ALOGD("%s:: clk recovery is only supported in STC render mode",
2099 __func__);
2100 ret = 0;
2101 goto exit;
2102 }
2103
2104 if (!out->compr) {
2105 ALOGD("%s:: Invalid compress handle",
2106 __func__);
2107 goto exit;
2108 }
2109 metadata.key = SNDRV_COMPRESS_CLK_REC_MODE;
2110 switch(usecase->out_snd_device) {
2111 case SND_DEVICE_OUT_HDMI:
2112 case SND_DEVICE_OUT_SPEAKER_AND_HDMI:
2113 case SND_DEVICE_OUT_DISPLAY_PORT:
2114 case SND_DEVICE_OUT_SPEAKER_AND_DISPLAY_PORT:
2115 metadata.value[0] = SNDRV_COMPRESS_CLK_REC_MODE_NONE;
2116 break;
2117 default:
2118 metadata.value[0] = SNDRV_COMPRESS_CLK_REC_MODE_AUTO;
2119 break;
2120 }
2121
2122 ALOGD("%s:: clk recovery mode %d",__func__, metadata.value[0]);
2123
2124 ret = compress_set_metadata(out->compr, &metadata);
2125 if(ret) {
2126 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2127 }
2128
2129exit:
2130 return ret;
2131}
2132#else
2133int audio_extn_utils_compress_set_clk_rec_mode(
2134 struct audio_usecase *usecase __unused)
2135{
2136 ALOGD("%s:: configuring render mode not supported", __func__);
2137 return 0;
2138}
2139#endif
Manish Dewangan27346042017-03-01 12:56:12 +05302140
2141#ifdef SNDRV_COMPRESS_RENDER_WINDOW
2142int audio_extn_utils_compress_set_render_window(
2143 struct stream_out *out,
2144 struct audio_out_render_window_param *render_window)
2145{
2146 struct snd_compr_metadata metadata;
2147 int ret = -EINVAL;
2148
Manish Dewangan27346042017-03-01 12:56:12 +05302149 if(render_window == NULL) {
2150 ALOGE("%s:: Invalid render_window", __func__);
2151 goto exit;
2152 }
2153
Aniket Kumar Lata1e1d3662017-06-01 18:45:48 -07002154 ALOGD("%s:: render window start 0x%"PRIx64" end 0x%"PRIx64"",
2155 __func__,render_window->render_ws, render_window->render_we);
2156
Manish Dewangan27346042017-03-01 12:56:12 +05302157 if (!is_offload_usecase(out->usecase)) {
2158 ALOGE("%s:: not supported for non offload session", __func__);
2159 goto exit;
2160 }
2161
Naresh Tanniru6160c712017-04-17 15:43:48 +05302162 if ((out->render_mode != RENDER_MODE_AUDIO_MASTER) &&
2163 (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER)) {
Manish Dewangan27346042017-03-01 12:56:12 +05302164 ALOGD("%s:: only supported in timestamp mode, current "
2165 "render mode mode %d", __func__, out->render_mode);
2166 goto exit;
2167 }
2168
2169 if (!out->compr) {
2170 ALOGW("%s:: offload session not yet opened,"
2171 "render window will be configure later", __func__);
2172 /* store render window to reconfigure in start_output_stream() */
2173 goto exit;
2174 }
2175
2176 metadata.key = SNDRV_COMPRESS_RENDER_WINDOW;
2177 /*render window start value */
2178 metadata.value[0] = 0xFFFFFFFF & render_window->render_ws; /* lsb */
2179 metadata.value[1] = \
2180 (0xFFFFFFFF00000000 & render_window->render_ws) >> 32; /* msb*/
2181 /*render window end value */
2182 metadata.value[2] = 0xFFFFFFFF & render_window->render_we; /* lsb */
2183 metadata.value[3] = \
2184 (0xFFFFFFFF00000000 & render_window->render_we) >> 32; /* msb*/
2185
2186 ret = compress_set_metadata(out->compr, &metadata);
2187 if(ret) {
2188 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2189 }
2190
2191exit:
2192 return ret;
2193}
2194#else
2195int audio_extn_utils_compress_set_render_window(
2196 struct stream_out *out __unused,
2197 struct audio_out_render_window_param *render_window __unused)
2198{
2199 ALOGD("%s:: configuring render window not supported", __func__);
2200 return 0;
2201}
2202#endif
Manish Dewangan14956cc2017-02-14 18:54:42 +05302203
2204#ifdef SNDRV_COMPRESS_START_DELAY
2205int audio_extn_utils_compress_set_start_delay(
2206 struct stream_out *out,
2207 struct audio_out_start_delay_param *delay_param)
2208{
2209 struct snd_compr_metadata metadata;
2210 int ret = -EINVAL;
2211
2212 if(delay_param == NULL) {
2213 ALOGE("%s:: Invalid delay_param", __func__);
2214 goto exit;
2215 }
2216
2217 ALOGD("%s:: render start delay 0x%"PRIx64" ", __func__,
2218 delay_param->start_delay);
2219
2220 if (!is_offload_usecase(out->usecase)) {
2221 ALOGE("%s:: not supported for non offload session", __func__);
2222 goto exit;
2223 }
2224
Naresh Tanniru6160c712017-04-17 15:43:48 +05302225 if ((out->render_mode != RENDER_MODE_AUDIO_MASTER) &&
2226 (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER)) {
Manish Dewangan14956cc2017-02-14 18:54:42 +05302227 ALOGD("%s:: only supported in timestamp mode, current "
2228 "render mode mode %d", __func__, out->render_mode);
2229 goto exit;
2230 }
2231
2232 if (!out->compr) {
2233 ALOGW("%s:: offload session not yet opened,"
2234 "start delay will be configure later", __func__);
2235 goto exit;
2236 }
2237
2238 metadata.key = SNDRV_COMPRESS_START_DELAY;
2239 metadata.value[0] = 0xFFFFFFFF & delay_param->start_delay; /* lsb */
2240 metadata.value[1] = \
2241 (0xFFFFFFFF00000000 & delay_param->start_delay) >> 32; /* msb*/
2242
2243 ret = compress_set_metadata(out->compr, &metadata);
2244 if(ret) {
2245 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2246 }
2247
2248exit:
2249 return ret;
2250}
2251#else
2252int audio_extn_utils_compress_set_start_delay(
2253 struct stream_out *out __unused,
2254 struct audio_out_start_delay_param *delay_param __unused)
2255{
2256 ALOGD("%s:: configuring render window not supported", __func__);
2257 return 0;
2258}
2259#endif
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002260
2261#define MAX_SND_CARD 8
Ashish Jain30ae8942017-11-05 17:21:23 +05302262#define RETRY_US 1000000
2263#define RETRY_NUMBER 40
Aalique Grahame22e49102018-12-18 14:23:57 -08002264#define PLATFORM_INFO_XML_PATH "audio_platform_info.xml"
2265#define PLATFORM_INFO_XML_BASE_STRING "audio_platform_info"
2266
2267#ifdef LINUX_ENABLED
2268static const char *kConfigLocationList[] =
2269 {"/etc"};
2270#else
2271static const char *kConfigLocationList[] =
2272 {"/vendor/etc"};
2273#endif
2274static const int kConfigLocationListSize =
2275 (sizeof(kConfigLocationList) / sizeof(kConfigLocationList[0]));
2276
2277bool audio_extn_utils_resolve_config_file(char file_name[MIXER_PATH_MAX_LENGTH])
2278{
2279 char full_config_path[MIXER_PATH_MAX_LENGTH];
2280 for (int i = 0; i < kConfigLocationListSize; i++) {
2281 snprintf(full_config_path,
2282 MIXER_PATH_MAX_LENGTH,
2283 "%s/%s",
2284 kConfigLocationList[i],
2285 file_name);
2286 if (F_OK == access(full_config_path, 0)) {
Weiyin Jiang2995f662019-04-17 14:25:12 +08002287 strlcpy(file_name, full_config_path, MIXER_PATH_MAX_LENGTH);
Aalique Grahame22e49102018-12-18 14:23:57 -08002288 return true;
2289 }
2290 }
2291 return false;
2292}
2293
2294/* platform_info_file should be size 'MIXER_PATH_MAX_LENGTH' */
2295int audio_extn_utils_get_platform_info(const char* snd_card_name, char* platform_info_file)
2296{
2297 if (NULL == snd_card_name) {
2298 return -1;
2299 }
2300
2301 struct snd_card_split *snd_split_handle = NULL;
2302 int ret = 0;
2303 audio_extn_set_snd_card_split(snd_card_name);
2304 snd_split_handle = audio_extn_get_snd_card_split();
2305
2306 snprintf(platform_info_file, MIXER_PATH_MAX_LENGTH, "%s_%s_%s.xml",
2307 PLATFORM_INFO_XML_BASE_STRING, snd_split_handle->snd_card,
2308 snd_split_handle->form_factor);
2309
2310 if (!audio_extn_utils_resolve_config_file(platform_info_file)) {
2311 memset(platform_info_file, 0, MIXER_PATH_MAX_LENGTH);
2312 snprintf(platform_info_file, MIXER_PATH_MAX_LENGTH, "%s_%s.xml",
2313 PLATFORM_INFO_XML_BASE_STRING, snd_split_handle->snd_card);
2314
2315 if (!audio_extn_utils_resolve_config_file(platform_info_file)) {
2316 memset(platform_info_file, 0, MIXER_PATH_MAX_LENGTH);
2317 strlcpy(platform_info_file, PLATFORM_INFO_XML_PATH, MIXER_PATH_MAX_LENGTH);
2318 ret = audio_extn_utils_resolve_config_file(platform_info_file) ? 0 : -1;
2319 }
2320 }
2321
2322 return ret;
2323}
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002324
2325int audio_extn_utils_get_snd_card_num()
2326{
Soumya Managoli9fee7c62018-04-06 16:21:50 +05302327 int snd_card_num = 0;
2328 struct mixer *mixer = NULL;
2329
2330 snd_card_num = audio_extn_utils_open_snd_mixer(&mixer);
2331 if (mixer)
2332 mixer_close(mixer);
2333 return snd_card_num;
2334}
2335
2336int audio_extn_utils_open_snd_mixer(struct mixer **mixer_handle)
2337{
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002338
2339 void *hw_info = NULL;
2340 struct mixer *mixer = NULL;
2341 int retry_num = 0;
mpangfaa7bae2019-01-15 16:38:13 +08002342 int snd_card_num = 0;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002343 char* snd_card_name = NULL;
mpangfaa7bae2019-01-15 16:38:13 +08002344 int snd_card_detection_info[MAX_SND_CARD] = {0};
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002345
Soumya Managoli9fee7c62018-04-06 16:21:50 +05302346 if (!mixer_handle) {
2347 ALOGE("invalid mixer handle");
2348 return -1;
2349 }
2350 *mixer_handle = NULL;
Ashish Jain30ae8942017-11-05 17:21:23 +05302351 /*
mpangfaa7bae2019-01-15 16:38:13 +08002352 * Try with all the sound cards ( 0 to 7 ) and if none of them were detected
Ashish Jain30ae8942017-11-05 17:21:23 +05302353 * sleep for 1 sec and try detections with sound card 0 again.
2354 * If sound card gets detected, check if it is relevant, if not check with the
2355 * other sound cards. To ensure that the irrelevant sound card is not check again,
2356 * we maintain it in min_snd_card_num.
2357 */
2358 while (retry_num < RETRY_NUMBER) {
mpangfaa7bae2019-01-15 16:38:13 +08002359 snd_card_num = 0;
2360 while (snd_card_num < MAX_SND_CARD) {
2361 if (snd_card_detection_info[snd_card_num] == 0) {
2362 mixer = mixer_open(snd_card_num);
2363 if (!mixer)
2364 snd_card_num++;
2365 else
2366 break;
2367 } else
2368 snd_card_num++;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002369 }
2370
2371 if (!mixer) {
Ashish Jain30ae8942017-11-05 17:21:23 +05302372 usleep(RETRY_US);
Ashish Jain30ae8942017-11-05 17:21:23 +05302373 retry_num++;
mpangfaa7bae2019-01-15 16:38:13 +08002374 ALOGD("%s: retry, retry_num %d", __func__, retry_num);
Ashish Jain30ae8942017-11-05 17:21:23 +05302375 continue;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002376 }
2377
2378 snd_card_name = strdup(mixer_get_name(mixer));
2379 if (!snd_card_name) {
2380 ALOGE("failed to allocate memory for snd_card_name\n");
2381 mixer_close(mixer);
2382 return -1;
2383 }
2384 ALOGD("%s: snd_card_name: %s", __func__, snd_card_name);
mpangfaa7bae2019-01-15 16:38:13 +08002385 snd_card_detection_info[snd_card_num] = 1;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002386 hw_info = hw_info_init(snd_card_name);
2387 if (hw_info) {
2388 ALOGD("%s: Opened sound card:%d", __func__, snd_card_num);
2389 break;
2390 }
mpangfaa7bae2019-01-15 16:38:13 +08002391 ALOGE("%s: Failed to init hardware info, snd_card_num:%d", __func__, snd_card_num);
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002392
Dhananjay Kumara7e27832017-07-11 15:40:39 +05302393 free(snd_card_name);
2394 snd_card_name = NULL;
2395
2396 mixer_close(mixer);
2397 mixer = NULL;
2398 }
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002399 if (snd_card_name)
2400 free(snd_card_name);
Ashish Jain30ae8942017-11-05 17:21:23 +05302401
Dhananjay Kumara7e27832017-07-11 15:40:39 +05302402 if (hw_info)
2403 hw_info_deinit(hw_info);
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002404
mpangfaa7bae2019-01-15 16:38:13 +08002405 if (retry_num >= RETRY_NUMBER) {
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002406 ALOGE("%s: Unable to find correct sound card, aborting.", __func__);
2407 return -1;
2408 }
2409
Soumya Managoli9fee7c62018-04-06 16:21:50 +05302410 if (mixer)
2411 *mixer_handle = mixer;
2412
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002413 return snd_card_num;
2414}
Naresh Tanniru6160c712017-04-17 15:43:48 +05302415
Soumya Managoli9fee7c62018-04-06 16:21:50 +05302416void audio_extn_utils_close_snd_mixer(struct mixer *mixer)
2417{
2418 if (mixer)
2419 mixer_close(mixer);
2420}
2421
Naresh Tanniru6160c712017-04-17 15:43:48 +05302422#ifdef SNDRV_COMPRESS_ENABLE_ADJUST_SESSION_CLOCK
2423int audio_extn_utils_compress_enable_drift_correction(
2424 struct stream_out *out,
2425 struct audio_out_enable_drift_correction *drift)
2426{
2427 struct snd_compr_metadata metadata;
2428 int ret = -EINVAL;
2429
2430 if(drift == NULL) {
2431 ALOGE("%s:: Invalid param", __func__);
2432 goto exit;
2433 }
2434
2435 ALOGD("%s:: drift enable %d", __func__,drift->enable);
2436
2437 if (!is_offload_usecase(out->usecase)) {
2438 ALOGE("%s:: not supported for non offload session", __func__);
2439 goto exit;
2440 }
2441
2442 if (!out->compr) {
2443 ALOGW("%s:: offload session not yet opened,"
2444 "start delay will be configure later", __func__);
2445 goto exit;
2446 }
2447
2448 metadata.key = SNDRV_COMPRESS_ENABLE_ADJUST_SESSION_CLOCK;
2449 metadata.value[0] = drift->enable;
2450 out->drift_correction_enabled = drift->enable;
2451
2452 ret = compress_set_metadata(out->compr, &metadata);
2453 if(ret) {
2454 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2455 out->drift_correction_enabled = false;
2456 }
2457
2458exit:
2459 return ret;
2460}
2461#else
2462int audio_extn_utils_compress_enable_drift_correction(
2463 struct stream_out *out __unused,
2464 struct audio_out_enable_drift_correction *drift __unused)
2465{
2466 ALOGD("%s:: configuring drift enablement not supported", __func__);
2467 return 0;
2468}
2469#endif
2470
2471#ifdef SNDRV_COMPRESS_ADJUST_SESSION_CLOCK
2472int audio_extn_utils_compress_correct_drift(
2473 struct stream_out *out,
2474 struct audio_out_correct_drift *drift_param)
2475{
2476 struct snd_compr_metadata metadata;
2477 int ret = -EINVAL;
2478
2479 if (drift_param == NULL) {
2480 ALOGE("%s:: Invalid drift_param", __func__);
2481 goto exit;
2482 }
2483
2484 ALOGD("%s:: adjust time 0x%"PRIx64" ", __func__,
2485 drift_param->adjust_time);
2486
2487 if (!is_offload_usecase(out->usecase)) {
2488 ALOGE("%s:: not supported for non offload session", __func__);
2489 goto exit;
2490 }
2491
2492 if (!out->compr) {
2493 ALOGW("%s:: offload session not yet opened", __func__);
2494 goto exit;
2495 }
2496
2497 if (!out->drift_correction_enabled) {
2498 ALOGE("%s:: drift correction not enabled", __func__);
2499 goto exit;
2500 }
2501
2502 metadata.key = SNDRV_COMPRESS_ADJUST_SESSION_CLOCK;
2503 metadata.value[0] = 0xFFFFFFFF & drift_param->adjust_time; /* lsb */
2504 metadata.value[1] = \
2505 (0xFFFFFFFF00000000 & drift_param->adjust_time) >> 32; /* msb*/
2506
2507 ret = compress_set_metadata(out->compr, &metadata);
2508 if(ret)
2509 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2510exit:
2511 return ret;
2512}
2513#else
2514int audio_extn_utils_compress_correct_drift(
2515 struct stream_out *out __unused,
2516 struct audio_out_correct_drift *drift_param __unused)
2517{
2518 ALOGD("%s:: setting adjust clock not supported", __func__);
2519 return 0;
2520}
2521#endif
Naresh Tanniru29bce4e2017-04-27 17:54:30 +05302522
2523int audio_extn_utils_set_channel_map(
2524 struct stream_out *out,
2525 struct audio_out_channel_map_param *channel_map_param)
2526{
2527 int ret = -EINVAL, i = 0;
2528 int channels = audio_channel_count_from_out_mask(out->channel_mask);
2529
2530 if (channel_map_param == NULL) {
2531 ALOGE("%s:: Invalid channel_map", __func__);
2532 goto exit;
2533 }
2534
2535 if (channel_map_param->channels != channels) {
2536 ALOGE("%s:: Channels(%d) does not match stream channels(%d)",
2537 __func__, channel_map_param->channels, channels);
2538 goto exit;
2539 }
2540
2541 for ( i = 0; i < channels; i++) {
2542 ALOGV("%s:: channel_map[%d]- %d", __func__, i, channel_map_param->channel_map[i]);
2543 out->channel_map_param.channel_map[i] = channel_map_param->channel_map[i];
2544 }
2545 ret = 0;
2546exit:
2547 return ret;
2548}
Varun Balaraje49253e2017-07-06 19:48:56 +05302549
2550int audio_extn_utils_set_pan_scale_params(
2551 struct stream_out *out,
2552 struct mix_matrix_params *mm_params)
2553{
2554 int ret = -EINVAL, i = 0, j = 0;
2555
Varun Balaraj003ee752017-08-07 17:54:55 +05302556 if (mm_params == NULL || out == NULL) {
2557 ALOGE("%s:: Invalid mix matrix or out param", __func__);
Varun Balaraje49253e2017-07-06 19:48:56 +05302558 goto exit;
2559 }
2560
2561 if (mm_params->num_output_channels > MAX_CHANNELS_SUPPORTED ||
2562 mm_params->num_output_channels <= 0 ||
2563 mm_params->num_input_channels > MAX_CHANNELS_SUPPORTED ||
2564 mm_params->num_input_channels <= 0)
2565 goto exit;
2566
2567 out->pan_scale_params.num_output_channels = mm_params->num_output_channels;
2568 out->pan_scale_params.num_input_channels = mm_params->num_input_channels;
2569 out->pan_scale_params.has_output_channel_map =
2570 mm_params->has_output_channel_map;
2571 for (i = 0; i < mm_params->num_output_channels; i++)
2572 out->pan_scale_params.output_channel_map[i] =
2573 mm_params->output_channel_map[i];
2574
2575 out->pan_scale_params.has_input_channel_map =
2576 mm_params->has_input_channel_map;
2577 for (i = 0; i < mm_params->num_input_channels; i++)
2578 out->pan_scale_params.input_channel_map[i] =
2579 mm_params->input_channel_map[i];
2580
2581 out->pan_scale_params.has_mixer_coeffs = mm_params->has_mixer_coeffs;
2582 for (i = 0; i < mm_params->num_output_channels; i++)
2583 for (j = 0; j < mm_params->num_input_channels; j++) {
2584 //Convert the channel coefficient gains in Q14 format
2585 out->pan_scale_params.mixer_coeffs[i][j] =
2586 mm_params->mixer_coeffs[i][j] * (2 << 13);
2587 }
2588
2589 ret = platform_set_stream_pan_scale_params(out->dev->platform,
2590 out->pcm_device_id,
2591 out->pan_scale_params);
2592
2593exit:
2594 return ret;
2595}
2596
2597int audio_extn_utils_set_downmix_params(
2598 struct stream_out *out,
2599 struct mix_matrix_params *mm_params)
2600{
2601 int ret = -EINVAL, i = 0, j = 0;
2602 struct audio_usecase *usecase = NULL;
2603
Aniket Kumar Lataf9f246e2017-09-15 15:20:16 -07002604 if (mm_params == NULL || out == NULL) {
Varun Balaraj003ee752017-08-07 17:54:55 +05302605 ALOGE("%s:: Invalid mix matrix or out param", __func__);
Varun Balaraje49253e2017-07-06 19:48:56 +05302606 goto exit;
2607 }
2608
2609 if (mm_params->num_output_channels > MAX_CHANNELS_SUPPORTED ||
2610 mm_params->num_output_channels <= 0 ||
2611 mm_params->num_input_channels > MAX_CHANNELS_SUPPORTED ||
2612 mm_params->num_input_channels <= 0)
2613 goto exit;
2614
2615 usecase = get_usecase_from_list(out->dev, out->usecase);
Varun Balaraj003ee752017-08-07 17:54:55 +05302616 if (!usecase) {
2617 ALOGE("%s: Get usecase list failed!", __func__);
Aniket Kumar Lataf9f246e2017-09-15 15:20:16 -07002618 goto exit;
2619 }
Varun Balaraje49253e2017-07-06 19:48:56 +05302620 out->downmix_params.num_output_channels = mm_params->num_output_channels;
2621 out->downmix_params.num_input_channels = mm_params->num_input_channels;
2622
2623 out->downmix_params.has_output_channel_map =
2624 mm_params->has_output_channel_map;
2625 for (i = 0; i < mm_params->num_output_channels; i++) {
2626 out->downmix_params.output_channel_map[i] =
2627 mm_params->output_channel_map[i];
2628 }
2629
2630 out->downmix_params.has_input_channel_map =
2631 mm_params->has_input_channel_map;
2632 for (i = 0; i < mm_params->num_input_channels; i++)
2633 out->downmix_params.input_channel_map[i] =
2634 mm_params->input_channel_map[i];
2635
2636 out->downmix_params.has_mixer_coeffs = mm_params->has_mixer_coeffs;
2637 for (i = 0; i < mm_params->num_output_channels; i++)
Nikhil Latukar2e6f6242017-08-15 13:37:07 +05302638 for (j = 0; j < mm_params->num_input_channels; j++) {
2639 //Convert the channel coefficient gains in Q14 format
Varun Balaraje49253e2017-07-06 19:48:56 +05302640 out->downmix_params.mixer_coeffs[i][j] =
Nikhil Latukar2e6f6242017-08-15 13:37:07 +05302641 mm_params->mixer_coeffs[i][j] * (2 << 13);
2642 }
Varun Balaraje49253e2017-07-06 19:48:56 +05302643
2644 ret = platform_set_stream_downmix_params(out->dev->platform,
2645 out->pcm_device_id,
2646 usecase->out_snd_device,
2647 out->downmix_params);
2648
2649exit:
2650 return ret;
2651}
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05302652
2653bool audio_extn_utils_is_dolby_format(audio_format_t format)
2654{
2655 if (format == AUDIO_FORMAT_AC3 ||
2656 format == AUDIO_FORMAT_E_AC3 ||
2657 format == AUDIO_FORMAT_E_AC3_JOC)
2658 return true;
2659 else
2660 return false;
2661}
2662
`Deeraj Soman676c2702017-09-18 19:25:53 +05302663int audio_extn_utils_get_bit_width_from_string(const char *id_string)
2664{
2665 int i;
2666 const mixer_config_lookup mixer_bitwidth_config[] = {{"S24_3LE", 24},
2667 {"S32_LE", 32},
2668 {"S24_LE", 24},
2669 {"S16_LE", 16}};
2670 int num_configs = sizeof(mixer_bitwidth_config) / sizeof(mixer_bitwidth_config[0]);
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05302671
`Deeraj Soman676c2702017-09-18 19:25:53 +05302672 for (i = 0; i < num_configs; i++) {
2673 if (!strcmp(id_string, mixer_bitwidth_config[i].id_string))
2674 return mixer_bitwidth_config[i].value;
2675 }
2676
2677 return -EINVAL;
2678}
2679
2680int audio_extn_utils_get_sample_rate_from_string(const char *id_string)
2681{
2682 int i;
2683 const mixer_config_lookup mixer_samplerate_config[] = {{"KHZ_32", 32000},
2684 {"KHZ_48", 48000},
2685 {"KHZ_96", 96000},
2686 {"KHZ_144", 144000},
2687 {"KHZ_192", 192000},
2688 {"KHZ_384", 384000},
2689 {"KHZ_44P1", 44100},
2690 {"KHZ_88P2", 88200},
2691 {"KHZ_176P4", 176400},
2692 {"KHZ_352P8", 352800}};
2693 int num_configs = sizeof(mixer_samplerate_config) / sizeof(mixer_samplerate_config[0]);
2694
2695 for (i = 0; i < num_configs; i++) {
2696 if (!strcmp(id_string, mixer_samplerate_config[i].id_string))
2697 return mixer_samplerate_config[i].value;
2698 }
2699
2700 return -EINVAL;
2701}
2702
2703int audio_extn_utils_get_channels_from_string(const char *id_string)
2704{
2705 int i;
2706 const mixer_config_lookup mixer_channels_config[] = {{"One", 1},
2707 {"Two", 2},
2708 {"Three",3},
2709 {"Four", 4},
2710 {"Five", 5},
2711 {"Six", 6},
2712 {"Seven", 7},
2713 {"Eight", 8}};
2714 int num_configs = sizeof(mixer_channels_config) / sizeof(mixer_channels_config[0]);
2715
2716 for (i = 0; i < num_configs; i++) {
2717 if (!strcmp(id_string, mixer_channels_config[i].id_string))
2718 return mixer_channels_config[i].value;
2719 }
2720
2721 return -EINVAL;
2722}
Surendar karka30569792018-05-08 12:02:21 +05302723
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08002724void audio_extn_utils_release_snd_device(snd_device_t snd_device)
2725{
2726 audio_extn_dev_arbi_release(snd_device);
2727 audio_extn_sound_trigger_update_device_status(snd_device,
2728 ST_EVENT_SND_DEVICE_FREE);
2729 audio_extn_listen_update_device_status(snd_device,
2730 LISTEN_EVENT_SND_DEVICE_FREE);
2731}
2732
Surendar karka30569792018-05-08 12:02:21 +05302733int audio_extn_utils_get_license_params
2734(
2735const struct audio_device *adev,
2736struct audio_license_params *license_params
2737)
2738{
2739 if(!license_params)
2740 return -EINVAL;
2741 return platform_get_license_by_product(adev->platform, (const char*)license_params->product, &license_params->key, license_params->license);
2742}
2743
Aalique Grahame22e49102018-12-18 14:23:57 -08002744int audio_extn_utils_send_app_type_gain(struct audio_device *adev,
2745 int app_type,
2746 int *gain)
2747{
2748 int gain_cfg[4];
2749 const char *mixer_ctl_name = "App Type Gain";
2750 struct mixer_ctl *ctl;
2751 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
2752 if (!ctl) {
2753 ALOGE("%s: Could not get volume ctl mixer %s", __func__,
2754 mixer_ctl_name);
2755 return -EINVAL;
2756 }
2757 gain_cfg[0] = 0;
2758 gain_cfg[1] = app_type;
2759 gain_cfg[2] = gain[0];
2760 gain_cfg[3] = gain[1];
2761 ALOGV("%s app_type %d l(%d) r(%d)", __func__, app_type, gain[0], gain[1]);
2762 return mixer_ctl_set_array(ctl, gain_cfg,
2763 sizeof(gain_cfg)/sizeof(gain_cfg[0]));
2764}
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -07002765
2766int audio_extn_utils_is_vendor_enhanced_fwk()
2767{
2768 static int is_running_with_enhanced_fwk = -EINVAL;
2769
2770 if (is_running_with_enhanced_fwk == -EINVAL) {
2771 vndk_fwk_lib_handle = dlopen(VNDK_FWK_LIB_PATH, RTLD_NOW);
2772 if (vndk_fwk_lib_handle != NULL) {
2773 vndk_fwk_isVendorEnhancedFwk = (vndk_fwk_isVendorEnhancedFwk_t)
2774 dlsym(vndk_fwk_lib_handle, "isRunningWithVendorEnhancedFramework");
2775 if (vndk_fwk_isVendorEnhancedFwk == NULL) {
2776 ALOGW("%s: dlsym failed, defaulting to enhanced_fwk configuration",
2777 __func__);
2778 is_running_with_enhanced_fwk = 1;
2779 } else {
2780 is_running_with_enhanced_fwk = vndk_fwk_isVendorEnhancedFwk();
2781 }
2782 dlclose(vndk_fwk_lib_handle);
2783 vndk_fwk_lib_handle = NULL;
2784 } else {
2785 ALOGW("%s: VNDK_FWK_LIB not found, setting stock configuration", __func__);
2786 is_running_with_enhanced_fwk = 0;
2787 }
2788 ALOGV("%s: vndk_fwk_isVendorEnhancedFwk=%d", __func__, is_running_with_enhanced_fwk);
2789 }
2790
2791 return is_running_with_enhanced_fwk;
2792}