blob: 349d2b08ef7acc6bf0b649367d1fc4852c699c91 [file] [log] [blame]
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001/*
Dhananjay Kumar14245982017-01-16 20:21:00 +05302 * Copyright (c) 2014-2017, 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
23#include <errno.h>
24#include <cutils/properties.h>
25#include <cutils/config_utils.h>
26#include <stdlib.h>
27#include <dlfcn.h>
28#include <cutils/str_parms.h>
29#include <cutils/log.h>
30#include <cutils/misc.h>
31
Manish Dewangan07de2142017-02-27 19:27:20 +053032
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070033#include "audio_hw.h"
34#include "platform.h"
35#include "platform_api.h"
36#include "audio_extn.h"
Narsinga Rao Chella212e2542014-11-17 19:57:04 -080037#include "voice.h"
Manish Dewangan07de2142017-02-27 19:27:20 +053038#include <sound/compress_params.h>
39#include <sound/compress_offload.h>
40#include <tinycompress/tinycompress.h>
Ashish Jain81eb2a82015-05-13 10:52:34 +053041#ifdef AUDIO_EXTERNAL_HDMI_ENABLED
42#ifdef HDMI_PASSTHROUGH_ENABLED
43#include "audio_parsers.h"
44#endif
45#endif
46
Yamit Mehtaa0d653a2016-11-25 20:33:25 +053047#ifdef LINUX_ENABLED
48#define AUDIO_OUTPUT_POLICY_VENDOR_CONFIG_FILE "/etc/audio_output_policy.conf"
Dhananjay Kumard6d32152016-10-13 16:11:03 +053049#define AUDIO_IO_POLICY_VENDOR_CONFIG_FILE "/etc/audio_io_policy.conf"
Yamit Mehtaa0d653a2016-11-25 20:33:25 +053050#else
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070051#define AUDIO_OUTPUT_POLICY_VENDOR_CONFIG_FILE "/vendor/etc/audio_output_policy.conf"
Dhananjay Kumard6d32152016-10-13 16:11:03 +053052#define AUDIO_IO_POLICY_VENDOR_CONFIG_FILE "/vendor/etc/audio_io_policy.conf"
Yamit Mehtaa0d653a2016-11-25 20:33:25 +053053#endif
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070054
55#define OUTPUTS_TAG "outputs"
Dhananjay Kumard6d32152016-10-13 16:11:03 +053056#define INPUTS_TAG "inputs"
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070057
58#define DYNAMIC_VALUE_TAG "dynamic"
59#define FLAGS_TAG "flags"
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +053060#define PROFILES_TAG "profile"
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070061#define FORMATS_TAG "formats"
62#define SAMPLING_RATES_TAG "sampling_rates"
63#define BIT_WIDTH_TAG "bit_width"
64#define APP_TYPE_TAG "app_type"
65
66#define STRING_TO_ENUM(string) { #string, string }
67#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
68
Ben Rombergera04fabc2014-11-14 12:16:03 -080069#define BASE_TABLE_SIZE 64
70#define MAX_BASEINDEX_LEN 256
71
Ashish Jain81eb2a82015-05-13 10:52:34 +053072#ifdef AUDIO_EXTERNAL_HDMI_ENABLED
73#define PROFESSIONAL (1<<0) /* 0 = consumer, 1 = professional */
74#define NON_LPCM (1<<1) /* 0 = audio, 1 = non-audio */
75#define SR_44100 (0<<0) /* 44.1kHz */
76#define SR_NOTID (1<<0) /* non indicated */
77#define SR_48000 (2<<0) /* 48kHz */
78#define SR_32000 (3<<0) /* 32kHz */
79#define SR_22050 (4<<0) /* 22.05kHz */
80#define SR_24000 (6<<0) /* 24kHz */
81#define SR_88200 (8<<0) /* 88.2kHz */
82#define SR_96000 (10<<0) /* 96kHz */
83#define SR_176400 (12<<0) /* 176.4kHz */
84#define SR_192000 (14<<0) /* 192kHz */
85
86#endif
Manish Dewangan07de2142017-02-27 19:27:20 +053087
88/* ToDo: Check and update a proper value in msec */
89#define COMPRESS_OFFLOAD_PLAYBACK_LATENCY 50
90
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070091struct string_to_enum {
92 const char *name;
93 uint32_t value;
94};
95
96const struct string_to_enum s_flag_name_to_enum_table[] = {
97 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_DIRECT),
vivek mehta0ea887a2015-08-26 14:01:20 -070098 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_DIRECT_PCM),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070099 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_PRIMARY),
100 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_FAST),
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800101 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_RAW),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700102 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_DEEP_BUFFER),
103 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD),
104 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_NON_BLOCKING),
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700105 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_HW_AV_SYNC),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700106#ifdef INCALL_MUSIC_ENABLED
107 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_INCALL_MUSIC),
108#endif
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700109 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH),
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530110 STRING_TO_ENUM(AUDIO_INPUT_FLAG_NONE),
111 STRING_TO_ENUM(AUDIO_INPUT_FLAG_FAST),
112 STRING_TO_ENUM(AUDIO_INPUT_FLAG_HW_HOTWORD),
113 STRING_TO_ENUM(AUDIO_INPUT_FLAG_RAW),
114 STRING_TO_ENUM(AUDIO_INPUT_FLAG_SYNC),
Dhananjay Kumaree4d2002016-10-25 18:02:58 +0530115 STRING_TO_ENUM(AUDIO_INPUT_FLAG_TIMESTAMP),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700116};
117
118const struct string_to_enum s_format_name_to_enum_table[] = {
Ashish Jain83a6cc22016-06-28 14:34:17 +0530119 STRING_TO_ENUM(AUDIO_FORMAT_PCM_8_BIT),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700120 STRING_TO_ENUM(AUDIO_FORMAT_PCM_16_BIT),
Ashish Jain5106d362016-05-11 19:23:33 +0530121 STRING_TO_ENUM(AUDIO_FORMAT_PCM_24_BIT_PACKED),
122 STRING_TO_ENUM(AUDIO_FORMAT_PCM_8_24_BIT),
Ashish Jainf1eaa582016-05-23 20:54:24 +0530123 STRING_TO_ENUM(AUDIO_FORMAT_PCM_32_BIT),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700124 STRING_TO_ENUM(AUDIO_FORMAT_MP3),
125 STRING_TO_ENUM(AUDIO_FORMAT_AAC),
126 STRING_TO_ENUM(AUDIO_FORMAT_VORBIS),
Mingming Yinae3530f2014-07-03 16:50:18 -0700127 STRING_TO_ENUM(AUDIO_FORMAT_AMR_NB),
128 STRING_TO_ENUM(AUDIO_FORMAT_AMR_WB),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700129 STRING_TO_ENUM(AUDIO_FORMAT_AC3),
Mingming Yinae3530f2014-07-03 16:50:18 -0700130 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700131 STRING_TO_ENUM(AUDIO_FORMAT_DTS),
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530132 STRING_TO_ENUM(AUDIO_FORMAT_DTS_HD),
133#ifdef AUDIO_EXTN_FORMATS_ENABLED
134 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3_JOC),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700135 STRING_TO_ENUM(AUDIO_FORMAT_WMA),
136 STRING_TO_ENUM(AUDIO_FORMAT_WMA_PRO),
137 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADIF),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700138 STRING_TO_ENUM(AUDIO_FORMAT_AMR_WB_PLUS),
139 STRING_TO_ENUM(AUDIO_FORMAT_EVRC),
140 STRING_TO_ENUM(AUDIO_FORMAT_EVRCB),
141 STRING_TO_ENUM(AUDIO_FORMAT_EVRCWB),
142 STRING_TO_ENUM(AUDIO_FORMAT_QCELP),
143 STRING_TO_ENUM(AUDIO_FORMAT_MP2),
144 STRING_TO_ENUM(AUDIO_FORMAT_EVRCNW),
Amit Shekhar6f461b12014-08-01 14:52:58 -0700145 STRING_TO_ENUM(AUDIO_FORMAT_FLAC),
Satya Krishna Pindiproli70471602015-04-24 19:12:43 +0530146 STRING_TO_ENUM(AUDIO_FORMAT_ALAC),
147 STRING_TO_ENUM(AUDIO_FORMAT_APE),
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700148 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3_JOC),
Alexy Josephcd8eaed2014-12-11 12:46:53 -0800149 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LC),
150 STRING_TO_ENUM(AUDIO_FORMAT_AAC_HE_V1),
151 STRING_TO_ENUM(AUDIO_FORMAT_AAC_HE_V2),
Manish Dewangana6fc5442015-08-24 20:30:31 +0530152 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS),
Ashish Jaine513a872015-11-19 17:00:56 +0530153 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_LC),
154 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_HE_V1),
155 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_HE_V2),
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +0530156 STRING_TO_ENUM(AUDIO_FORMAT_DSD),
Arun Kumar Dasari3b174182016-12-27 13:01:14 +0530157 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM),
158 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM_LC),
159 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM_HE_V1),
160 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM_HE_V2),
Dhanalakshmi Siddani18737932016-11-29 17:33:17 +0530161 STRING_TO_ENUM(AUDIO_FORMAT_APTX),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700162#endif
163};
164
Manish Dewangan3ccdea52017-02-13 19:31:54 +0530165/* payload structure avt_device drift query */
166struct audio_avt_device_drift_stats {
167 uint32_t minor_version;
168 /* Indicates the device interface direction as either
169 * source (Tx) or sink (Rx).
170 */
171 uint16_t device_direction;
172 /*params exposed to client */
173 struct audio_avt_device_drift_param drift_param;
174};
175
Ben Rombergera04fabc2014-11-14 12:16:03 -0800176static char bTable[BASE_TABLE_SIZE] = {
177 'A','B','C','D','E','F','G','H','I','J','K','L',
178 'M','N','O','P','Q','R','S','T','U','V','W','X',
179 'Y','Z','a','b','c','d','e','f','g','h','i','j',
180 'k','l','m','n','o','p','q','r','s','t','u','v',
181 'w','x','y','z','0','1','2','3','4','5','6','7',
182 '8','9','+','/'
183};
184
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700185static uint32_t string_to_enum(const struct string_to_enum *table, size_t size,
186 const char *name)
187{
188 size_t i;
189 for (i = 0; i < size; i++) {
190 if (strcmp(table[i].name, name) == 0) {
191 ALOGV("%s found %s", __func__, table[i].name);
192 return table[i].value;
193 }
194 }
195 return 0;
196}
197
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530198static audio_io_flags_t parse_flag_names(char *name)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700199{
200 uint32_t flag = 0;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530201 audio_io_flags_t io_flags;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800202 char *last_r;
203 char *flag_name = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700204 while (flag_name != NULL) {
205 if (strlen(flag_name) != 0) {
206 flag |= string_to_enum(s_flag_name_to_enum_table,
207 ARRAY_SIZE(s_flag_name_to_enum_table),
208 flag_name);
209 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800210 flag_name = strtok_r(NULL, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700211 }
212
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800213 ALOGV("parse_flag_names: flag - %x", flag);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530214 io_flags.in_flags = (audio_input_flags_t)flag;
215 io_flags.out_flags = (audio_output_flags_t)flag;
216 return io_flags;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700217}
218
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530219static void parse_format_names(char *name, struct streams_io_cfg *s_info)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700220{
221 struct stream_format *sf_info = NULL;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800222 char *last_r;
223 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700224
225 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG) == 0)
226 return;
227
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530228 list_init(&s_info->format_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700229 while (str != NULL) {
230 audio_format_t format = (audio_format_t)string_to_enum(s_format_name_to_enum_table,
231 ARRAY_SIZE(s_format_name_to_enum_table), str);
232 ALOGV("%s: format - %d", __func__, format);
233 if (format != 0) {
234 sf_info = (struct stream_format *)calloc(1, sizeof(struct stream_format));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700235 if (sf_info == NULL)
236 break; /* return whatever was parsed */
237
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700238 sf_info->format = format;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530239 list_add_tail(&s_info->format_list, &sf_info->list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700240 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800241 str = strtok_r(NULL, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700242 }
243}
244
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530245static void parse_sample_rate_names(char *name, struct streams_io_cfg *s_info)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700246{
Amit Shekhar6f461b12014-08-01 14:52:58 -0700247 struct stream_sample_rate *ss_info = NULL;
248 uint32_t sample_rate = 48000;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800249 char *last_r;
250 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700251
Amit Shekhar6f461b12014-08-01 14:52:58 -0700252 if (str != NULL && 0 == strcmp(str, DYNAMIC_VALUE_TAG))
253 return;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700254
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530255 list_init(&s_info->sample_rate_list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700256 while (str != NULL) {
257 sample_rate = (uint32_t)strtol(str, (char **)NULL, 10);
258 ALOGV("%s: sample_rate - %d", __func__, sample_rate);
259 if (0 != sample_rate) {
260 ss_info = (struct stream_sample_rate *)calloc(1, sizeof(struct stream_sample_rate));
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800261 if (!ss_info) {
262 ALOGE("%s: memory allocation failure", __func__);
263 return;
264 }
Amit Shekhar6f461b12014-08-01 14:52:58 -0700265 ss_info->sample_rate = sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530266 list_add_tail(&s_info->sample_rate_list, &ss_info->list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700267 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800268 str = strtok_r(NULL, "|", &last_r);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700269 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700270}
271
272static int parse_bit_width_names(char *name)
273{
274 int bit_width = 16;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800275 char *last_r;
276 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700277
278 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG))
279 bit_width = (int)strtol(str, (char **)NULL, 10);
280
281 ALOGV("%s: bit_width - %d", __func__, bit_width);
282 return bit_width;
283}
284
285static int parse_app_type_names(void *platform, char *name)
286{
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700287 int app_type = platform_get_default_app_type(platform);
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800288 char *last_r;
289 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700290
291 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG))
292 app_type = (int)strtol(str, (char **)NULL, 10);
293
294 ALOGV("%s: app_type - %d", __func__, app_type);
295 return app_type;
296}
297
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530298static void update_streams_cfg_list(cnode *root, void *platform,
299 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700300{
301 cnode *node = root->first_child;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530302 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700303
304 ALOGV("%s", __func__);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530305 s_info = (struct streams_io_cfg *)calloc(1, sizeof(struct streams_io_cfg));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700306
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530307 if (!s_info) {
308 ALOGE("failed to allocate mem for s_info list element");
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700309 return;
310 }
311
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700312 while (node) {
313 if (strcmp(node->name, FLAGS_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530314 s_info->flags = parse_flag_names((char *)node->value);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530315 } else if (strcmp(node->name, PROFILES_TAG) == 0) {
316 strlcpy(s_info->profile, (char *)node->value, sizeof(s_info->profile));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700317 } else if (strcmp(node->name, FORMATS_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530318 parse_format_names((char *)node->value, s_info);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700319 } else if (strcmp(node->name, SAMPLING_RATES_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530320 s_info->app_type_cfg.sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
321 parse_sample_rate_names((char *)node->value, s_info);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700322 } else if (strcmp(node->name, BIT_WIDTH_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530323 s_info->app_type_cfg.bit_width = parse_bit_width_names((char *)node->value);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700324 } else if (strcmp(node->name, APP_TYPE_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530325 s_info->app_type_cfg.app_type = parse_app_type_names(platform, (char *)node->value);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700326 }
327 node = node->next;
328 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530329 list_add_tail(streams_cfg_list, &s_info->list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700330}
331
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530332static void load_cfg_list(cnode *root, void *platform,
333 struct listnode *streams_output_cfg_list,
334 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700335{
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530336 cnode *node = NULL;
337
338 node = config_find(root, OUTPUTS_TAG);
339 if (node != NULL) {
340 node = node->first_child;
341 while (node) {
342 ALOGV("%s: loading output %s", __func__, node->name);
343 update_streams_cfg_list(node, platform, streams_output_cfg_list);
344 node = node->next;
345 }
346 } else {
347 ALOGI("%s: could not load output, node is NULL", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700348 }
349
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530350 node = config_find(root, INPUTS_TAG);
351 if (node != NULL) {
352 node = node->first_child;
353 while (node) {
354 ALOGV("%s: loading input %s", __func__, node->name);
355 update_streams_cfg_list(node, platform, streams_input_cfg_list);
356 node = node->next;
357 }
358 } else {
359 ALOGI("%s: could not load input, node is NULL", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700360 }
361}
362
363static void send_app_type_cfg(void *platform, struct mixer *mixer,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530364 struct listnode *streams_output_cfg_list,
365 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700366{
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530367 size_t app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT] = {0};
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700368 int length = 0, i, num_app_types = 0;
369 struct listnode *node;
370 bool update;
371 struct mixer_ctl *ctl = NULL;
372 const char *mixer_ctl_name = "App Type Config";
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530373 struct streams_io_cfg *s_info = NULL;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700374
375 if (!mixer) {
376 ALOGE("%s: mixer is null",__func__);
377 return;
378 }
379 ctl = mixer_get_ctl_by_name(mixer, mixer_ctl_name);
380 if (!ctl) {
381 ALOGE("%s: Could not get ctl for mixer cmd - %s",__func__, mixer_ctl_name);
382 return;
383 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530384 app_type_cfg[length++] = num_app_types;
385
386 if (list_empty(streams_output_cfg_list)) {
387 app_type_cfg[length++] = platform_get_default_app_type_v2(platform, PCM_PLAYBACK);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700388 app_type_cfg[length++] = 48000;
389 app_type_cfg[length++] = 16;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530390 num_app_types += 1;
391 }
392 if (list_empty(streams_input_cfg_list)) {
393 app_type_cfg[length++] = platform_get_default_app_type_v2(platform, PCM_CAPTURE);
394 app_type_cfg[length++] = 48000;
395 app_type_cfg[length++] = 16;
396 num_app_types += 1;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700397 }
398
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700399 list_for_each(node, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530400 s_info = node_to_item(node, struct streams_io_cfg, list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700401 update = true;
402 for (i=0; i<length; i=i+3) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530403 if (app_type_cfg[i+1] == 0)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700404 break;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530405 else if (app_type_cfg[i+1] == (size_t)s_info->app_type_cfg.app_type) {
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530406 if (app_type_cfg[i+2] < (size_t)s_info->app_type_cfg.sample_rate)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530407 app_type_cfg[i+2] = s_info->app_type_cfg.sample_rate;
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530408 if (app_type_cfg[i+3] < (size_t)s_info->app_type_cfg.bit_width)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530409 app_type_cfg[i+3] = s_info->app_type_cfg.bit_width;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700410 update = false;
411 break;
412 }
413 }
414 if (update && ((length + 3) <= MAX_LENGTH_MIXER_CONTROL_IN_INT)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530415 num_app_types += 1;
416 app_type_cfg[length++] = s_info->app_type_cfg.app_type;
417 app_type_cfg[length++] = s_info->app_type_cfg.sample_rate;
418 app_type_cfg[length++] = s_info->app_type_cfg.bit_width;
419 }
420 }
421 list_for_each(node, streams_input_cfg_list) {
422 s_info = node_to_item(node, struct streams_io_cfg, list);
423 update = true;
424 for (i=0; i<length; i=i+3) {
425 if (app_type_cfg[i+1] == 0)
426 break;
427 else if (app_type_cfg[i+1] == (size_t)s_info->app_type_cfg.app_type) {
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530428 if (app_type_cfg[i+2] < (size_t)s_info->app_type_cfg.sample_rate)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530429 app_type_cfg[i+2] = s_info->app_type_cfg.sample_rate;
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530430 if (app_type_cfg[i+3] < (size_t)s_info->app_type_cfg.bit_width)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530431 app_type_cfg[i+3] = s_info->app_type_cfg.bit_width;
432 update = false;
433 break;
434 }
435 }
436 if (update && ((length + 3) <= MAX_LENGTH_MIXER_CONTROL_IN_INT)) {
437 num_app_types += 1;
438 app_type_cfg[length++] = s_info->app_type_cfg.app_type;
439 app_type_cfg[length++] = s_info->app_type_cfg.sample_rate;
440 app_type_cfg[length++] = s_info->app_type_cfg.bit_width;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700441 }
442 }
443 ALOGV("%s: num_app_types: %d", __func__, num_app_types);
444 if (num_app_types) {
445 app_type_cfg[0] = num_app_types;
446 mixer_ctl_set_array(ctl, app_type_cfg, length);
447 }
448}
449
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530450void audio_extn_utils_update_streams_cfg_lists(void *platform,
451 struct mixer *mixer,
452 struct listnode *streams_output_cfg_list,
453 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700454{
455 cnode *root;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530456 char *data = NULL;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700457
458 ALOGV("%s", __func__);
459 list_init(streams_output_cfg_list);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530460 list_init(streams_input_cfg_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700461
462 root = config_node("", "");
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700463 if (root == NULL) {
464 ALOGE("cfg_list, NULL config root");
465 return;
466 }
467
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530468 data = (char *)load_file(AUDIO_IO_POLICY_VENDOR_CONFIG_FILE, NULL);
469 if (data == NULL) {
470 ALOGD("%s: failed to open io config file(%s), trying older config file",
471 __func__, AUDIO_IO_POLICY_VENDOR_CONFIG_FILE);
472 data = (char *)load_file(AUDIO_OUTPUT_POLICY_VENDOR_CONFIG_FILE, NULL);
473 if (data == NULL) {
474 send_app_type_cfg(platform, mixer,
475 streams_output_cfg_list,
476 streams_input_cfg_list);
477 ALOGE("%s: could not load io policy config!", __func__);
478 return;
479 }
480 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700481
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530482 config_load(root, data);
483 load_cfg_list(root, platform, streams_output_cfg_list,
484 streams_input_cfg_list);
485
486 send_app_type_cfg(platform, mixer, streams_output_cfg_list,
487 streams_input_cfg_list);
Alexy Josephaee4fdd2016-01-29 13:02:07 -0800488
489 config_free(root);
490 free(data);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700491}
492
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530493static void audio_extn_utils_dump_streams_cfg_list(
494 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700495{
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700496 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530497 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700498 struct stream_format *sf_info;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700499 struct stream_sample_rate *ss_info;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530500
501 list_for_each(node_i, streams_cfg_list) {
502 s_info = node_to_item(node_i, struct streams_io_cfg, list);
503 ALOGV("%s: flags-%d, sample_rate-%d, bit_width-%d, app_type-%d",
504 __func__, s_info->flags.out_flags, s_info->app_type_cfg.sample_rate,
505 s_info->app_type_cfg.bit_width, s_info->app_type_cfg.app_type);
506 list_for_each(node_j, &s_info->format_list) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700507 sf_info = node_to_item(node_j, struct stream_format, list);
508 ALOGV("format-%x", sf_info->format);
509 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530510 list_for_each(node_j, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700511 ss_info = node_to_item(node_j, struct stream_sample_rate, list);
512 ALOGV("sample rate-%d", ss_info->sample_rate);
513 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700514 }
515}
516
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530517void audio_extn_utils_dump_streams_cfg_lists(
518 struct listnode *streams_output_cfg_list,
519 struct listnode *streams_input_cfg_list)
520{
521 ALOGV("%s", __func__);
522 audio_extn_utils_dump_streams_cfg_list(streams_output_cfg_list);
523 audio_extn_utils_dump_streams_cfg_list(streams_input_cfg_list);
524}
525
526static void audio_extn_utils_release_streams_cfg_list(
527 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700528{
529 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530530 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700531
532 ALOGV("%s", __func__);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530533
534 while (!list_empty(streams_cfg_list)) {
535 node_i = list_head(streams_cfg_list);
536 s_info = node_to_item(node_i, struct streams_io_cfg, list);
537 while (!list_empty(&s_info->format_list)) {
538 node_j = list_head(&s_info->format_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700539 list_remove(node_j);
540 free(node_to_item(node_j, struct stream_format, list));
541 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530542 while (!list_empty(&s_info->sample_rate_list)) {
543 node_j = list_head(&s_info->sample_rate_list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700544 list_remove(node_j);
545 free(node_to_item(node_j, struct stream_sample_rate, list));
546 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700547 list_remove(node_i);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530548 free(node_to_item(node_i, struct streams_io_cfg, list));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700549 }
550}
551
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530552void audio_extn_utils_release_streams_cfg_lists(
553 struct listnode *streams_output_cfg_list,
554 struct listnode *streams_input_cfg_list)
555{
556 ALOGV("%s", __func__);
557 audio_extn_utils_release_streams_cfg_list(streams_output_cfg_list);
558 audio_extn_utils_release_streams_cfg_list(streams_input_cfg_list);
559}
560
561static bool set_app_type_cfg(struct streams_io_cfg *s_info,
562 struct stream_app_type_cfg *app_type_cfg,
563 uint32_t sample_rate, uint32_t bit_width)
Amit Shekhar6f461b12014-08-01 14:52:58 -0700564 {
565 struct listnode *node_i;
566 struct stream_sample_rate *ss_info;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530567 list_for_each(node_i, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700568 ss_info = node_to_item(node_i, struct stream_sample_rate, list);
569 if ((sample_rate <= ss_info->sample_rate) &&
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530570 (bit_width == s_info->app_type_cfg.bit_width)) {
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700571
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530572 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700573 app_type_cfg->sample_rate = ss_info->sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530574 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700575 ALOGV("%s app_type_cfg->app_type %d, app_type_cfg->sample_rate %d, app_type_cfg->bit_width %d",
576 __func__, app_type_cfg->app_type, app_type_cfg->sample_rate, app_type_cfg->bit_width);
577 return true;
578 }
579 }
580 /*
581 * Reiterate through the list assuming dafault sample rate.
582 * Handles scenario where input sample rate is higher
583 * than all sample rates in list for the input bit width.
584 */
585 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
Apoorv Raghuvanshif59bb222015-02-18 12:23:23 -0800586
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530587 list_for_each(node_i, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700588 ss_info = node_to_item(node_i, struct stream_sample_rate, list);
589 if ((sample_rate <= ss_info->sample_rate) &&
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530590 (bit_width == s_info->app_type_cfg.bit_width)) {
591 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700592 app_type_cfg->sample_rate = sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530593 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Apoorv Raghuvanshif59bb222015-02-18 12:23:23 -0800594 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 -0700595 __func__, app_type_cfg->app_type, app_type_cfg->sample_rate, app_type_cfg->bit_width);
596 return true;
597 }
598 }
599 return false;
600}
601
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530602void audio_extn_utils_update_stream_input_app_type_cfg(void *platform,
603 struct listnode *streams_input_cfg_list,
604 audio_devices_t devices __unused,
605 audio_input_flags_t flags,
606 audio_format_t format,
607 uint32_t sample_rate,
608 uint32_t bit_width,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530609 char* profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530610 struct stream_app_type_cfg *app_type_cfg)
611{
612 struct listnode *node_i, *node_j;
613 struct streams_io_cfg *s_info;
614 struct stream_format *sf_info;
615
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530616 ALOGV("%s: flags: 0x%x, format: 0x%x sample_rate %d, profile %s",
617 __func__, flags, format, sample_rate, profile);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530618
619 list_for_each(node_i, streams_input_cfg_list) {
620 s_info = node_to_item(node_i, struct streams_io_cfg, list);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530621 /* Along with flags do profile matching if set at either end.*/
622 if (s_info->flags.in_flags == flags &&
623 ((profile[0] == '\0' && s_info->profile[0] == '\0') ||
624 strncmp(s_info->profile, profile, sizeof(s_info->profile)) == 0)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530625 list_for_each(node_j, &s_info->format_list) {
626 sf_info = node_to_item(node_j, struct stream_format, list);
627 if (sf_info->format == format) {
628 if (set_app_type_cfg(s_info, app_type_cfg, sample_rate, bit_width))
629 return;
630 }
631 }
632 }
633 }
634 ALOGW("%s: App type could not be selected. Falling back to default", __func__);
635 app_type_cfg->app_type = platform_get_default_app_type_v2(platform, PCM_CAPTURE);
636 app_type_cfg->sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
637 app_type_cfg->bit_width = 16;
638}
639
640void audio_extn_utils_update_stream_output_app_type_cfg(void *platform,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700641 struct listnode *streams_output_cfg_list,
Amit Shekhar1d896042014-10-03 13:16:09 -0700642 audio_devices_t devices,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700643 audio_output_flags_t flags,
644 audio_format_t format,
Amit Shekhar6f461b12014-08-01 14:52:58 -0700645 uint32_t sample_rate,
646 uint32_t bit_width,
Manish Dewangan837dc462015-05-27 10:17:41 +0530647 audio_channel_mask_t channel_mask,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530648 char *profile,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700649 struct stream_app_type_cfg *app_type_cfg)
650{
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +0530651 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530652 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700653 struct stream_format *sf_info;
Manish Dewangan837dc462015-05-27 10:17:41 +0530654 char value[PROPERTY_VALUE_MAX] = {0};
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700655
Ashish Jain058165c2016-09-28 23:18:48 +0530656 if ((bit_width >= 24) &&
Amit Shekhar1d896042014-10-03 13:16:09 -0700657 (devices & AUDIO_DEVICE_OUT_SPEAKER)) {
Amit Shekhar5a39c912014-10-14 15:39:30 -0700658 int32_t bw = platform_get_snd_device_bit_width(SND_DEVICE_OUT_SPEAKER);
659 if (-ENOSYS != bw)
660 bit_width = (uint32_t)bw;
Amit Shekhar1d896042014-10-03 13:16:09 -0700661 sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
662 ALOGI("%s Allowing 24-bit playback on speaker ONLY at default sampling rate", __func__);
663 }
664
Manish Dewangan837dc462015-05-27 10:17:41 +0530665 property_get("audio.playback.mch.downsample",value,"");
666 if (!strncmp("true", value, sizeof("true"))) {
667 if ((popcount(channel_mask) > 2) &&
668 (sample_rate > CODEC_BACKEND_DEFAULT_SAMPLE_RATE) &&
669 !(flags & AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH)) {
670 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
671 ALOGD("%s: MCH session defaulting sample rate to %d",
672 __func__, sample_rate);
673 }
674 }
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +0530675
676 /* Set sampling rate to 176.4 for DSD64
677 * and 352.8Khz for DSD128.
678 * Set Bit Width to 16. output will be 16 bit
679 * post DoP in ASM.
680 */
681 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH) &&
682 (format == AUDIO_FORMAT_DSD)) {
683 bit_width = 16;
684 if (sample_rate == INPUT_SAMPLING_RATE_DSD64)
685 sample_rate = OUTPUT_SAMPLING_RATE_DSD64;
686 else if (sample_rate == INPUT_SAMPLING_RATE_DSD128)
687 sample_rate = OUTPUT_SAMPLING_RATE_DSD128;
688 }
689
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +0530690 if(devices & AUDIO_DEVICE_OUT_ALL_A2DP) {
691 //TODO: Handle fractional sampling rate configuration for LL
692 audio_extn_a2dp_get_apptype_params(&sample_rate, &bit_width);
693 ALOGI("%s using %d sampling rate %d bit width for A2DP CoPP",
694 __func__, sample_rate, bit_width);
695 }
696
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800697 ALOGV("%s: flags: %x, format: %x sample_rate %d, profile %s, app_type %d",
698 __func__, flags, format, sample_rate, profile, app_type_cfg->app_type);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700699 list_for_each(node_i, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530700 s_info = node_to_item(node_i, struct streams_io_cfg, list);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530701 /* Along with flags do profile matching if set at either end.*/
702 if (s_info->flags.out_flags == flags &&
703 ((profile[0] == '\0' && s_info->profile[0] == '\0') ||
704 strncmp(s_info->profile, profile, sizeof(s_info->profile)) == 0)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530705 list_for_each(node_j, &s_info->format_list) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700706 sf_info = node_to_item(node_j, struct stream_format, list);
707 if (sf_info->format == format) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530708 if (set_app_type_cfg(s_info, app_type_cfg, sample_rate, bit_width))
Amit Shekhar6f461b12014-08-01 14:52:58 -0700709 return;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700710 }
711 }
712 }
713 }
714 list_for_each(node_i, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530715 s_info = node_to_item(node_i, struct streams_io_cfg, list);
716 if (s_info->flags.out_flags == AUDIO_OUTPUT_FLAG_PRIMARY) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700717 ALOGV("Compatible output profile not found.");
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530718 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
719 app_type_cfg->sample_rate = s_info->app_type_cfg.sample_rate;
720 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700721 ALOGV("%s Default to primary output: App type: %d sample_rate %d",
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530722 __func__, s_info->app_type_cfg.app_type, app_type_cfg->sample_rate);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700723 return;
724 }
725 }
726 ALOGW("%s: App type could not be selected. Falling back to default", __func__);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700727 app_type_cfg->app_type = platform_get_default_app_type(platform);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700728 app_type_cfg->sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700729 app_type_cfg->bit_width = 16;
730}
731
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +0530732static bool audio_is_this_native_usecase(struct audio_usecase *uc)
733{
734 bool native_usecase = false;
735 struct stream_out *out = (struct stream_out*) uc->stream.out;
736
737 if (PCM_PLAYBACK == uc->type && out != NULL &&
738 NATIVE_AUDIO_MODE_INVALID != platform_get_native_support() &&
739 is_offload_usecase(uc->id) &&
740 (out->sample_rate == OUTPUT_SAMPLING_RATE_44100))
741 native_usecase = true;
742
743 return native_usecase;
744}
745
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800746
747static inline bool audio_is_vr_mode_on(struct audio_device *(__attribute__((unused)) adev))
748{
749 return adev->vr_audio_mode_enabled;
750}
751
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530752void audio_extn_utils_update_stream_app_type_cfg_for_usecase(
753 struct audio_device *adev,
754 struct audio_usecase *usecase)
755{
756 ALOGV("%s", __func__);
757
758 switch(usecase->type) {
759 case PCM_PLAYBACK:
760 audio_extn_utils_update_stream_output_app_type_cfg(adev->platform,
761 &adev->streams_output_cfg_list,
762 usecase->stream.out->devices,
763 usecase->stream.out->flags,
764 usecase->stream.out->format,
765 usecase->stream.out->sample_rate,
766 usecase->stream.out->bit_width,
767 usecase->stream.out->channel_mask,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530768 usecase->stream.out->profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530769 &usecase->stream.out->app_type_cfg);
770 ALOGV("%s Selected apptype: %d", __func__, usecase->stream.out->app_type_cfg.app_type);
771 break;
772 case PCM_CAPTURE:
773 audio_extn_utils_update_stream_input_app_type_cfg(adev->platform,
774 &adev->streams_input_cfg_list,
775 usecase->stream.in->device,
776 usecase->stream.in->flags,
777 usecase->stream.in->format,
778 usecase->stream.in->sample_rate,
779 usecase->stream.in->bit_width,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530780 usecase->stream.in->profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530781 &usecase->stream.in->app_type_cfg);
782 ALOGV("%s Selected apptype: %d", __func__, usecase->stream.in->app_type_cfg.app_type);
783 break;
784 default:
785 ALOGE("%s: app type cfg not supported for usecase type (%d)",
786 __func__, usecase->type);
787 }
788}
789
Siena Richard7c2db772016-12-21 11:32:34 -0800790static int send_app_type_cfg_for_device(struct audio_device *adev,
791 struct audio_usecase *usecase,
792 int split_snd_device)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700793{
794 char mixer_ctl_name[MAX_LENGTH_MIXER_CONTROL_IN_INT];
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530795 size_t app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT] = {0};
796 int len = 0, rc;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700797 struct mixer_ctl *ctl;
Siena Richard7c2db772016-12-21 11:32:34 -0800798 int pcm_device_id = 0, acdb_dev_id, app_type;
799 int snd_device = split_snd_device, snd_device_be_idx = -1;
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +0530800 int32_t sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Manish Dewangan837dc462015-05-27 10:17:41 +0530801 char value[PROPERTY_VALUE_MAX] = {0};
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700802
Siena Richard7c2db772016-12-21 11:32:34 -0800803 ALOGV("%s: usecase->out_snd_device %s, usecase->in_snd_device %s, split_snd_device %s",
804 __func__, platform_get_snd_device_name(usecase->out_snd_device),
805 platform_get_snd_device_name(usecase->in_snd_device),
806 platform_get_snd_device_name(split_snd_device));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700807
Srikanth Uyyala9d551402015-08-25 16:03:42 +0530808 if (usecase->type != PCM_PLAYBACK && usecase->type != PCM_CAPTURE) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530809 ALOGE("%s: not a playback/capture path, no need to cfg app type", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700810 rc = 0;
811 goto exit_send_app_type_cfg;
812 }
813 if ((usecase->id != USECASE_AUDIO_PLAYBACK_DEEP_BUFFER) &&
814 (usecase->id != USECASE_AUDIO_PLAYBACK_LOW_LATENCY) &&
815 (usecase->id != USECASE_AUDIO_PLAYBACK_MULTI_CH) &&
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800816 (usecase->id != USECASE_AUDIO_PLAYBACK_ULL) &&
Srikanth Uyyala9d551402015-08-25 16:03:42 +0530817 (!is_offload_usecase(usecase->id)) &&
818 (usecase->type != PCM_CAPTURE)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530819 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 -0700820 rc = 0;
821 goto exit_send_app_type_cfg;
822 }
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800823
824 //if VR is active then only send the mixer control
825 if (usecase->id == USECASE_AUDIO_PLAYBACK_ULL && !audio_is_vr_mode_on(adev)) {
826 ALOGI("ULL doesnt need sending app type cfg, returning");
827 rc = 0;
828 goto exit_send_app_type_cfg;
829 }
830
Srikanth Uyyala9d551402015-08-25 16:03:42 +0530831 if (usecase->type == PCM_PLAYBACK) {
Ben Romberger1fafdde2015-09-09 19:43:15 -0700832 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_PLAYBACK);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530833 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
834 "Audio Stream %d App Type Cfg", pcm_device_id);
Ben Romberger1fafdde2015-09-09 19:43:15 -0700835 } else if (usecase->type == PCM_CAPTURE) {
Ben Romberger1fafdde2015-09-09 19:43:15 -0700836 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_CAPTURE);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530837 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
838 "Audio Stream Capture %d App Type Cfg", pcm_device_id);
Srikanth Uyyala9d551402015-08-25 16:03:42 +0530839 }
Siena Richard7c2db772016-12-21 11:32:34 -0800840
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700841 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
842 if (!ctl) {
843 ALOGE("%s: Could not get ctl for mixer cmd - %s", __func__,
844 mixer_ctl_name);
845 rc = -EINVAL;
846 goto exit_send_app_type_cfg;
847 }
848 snd_device = (snd_device == SND_DEVICE_OUT_SPEAKER) ?
Xiaojun Sang040cc9f2015-08-03 19:38:28 +0800849 platform_get_spkr_prot_snd_device(snd_device) : snd_device;
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +0530850
Rohit Kumar1181b292017-01-31 18:07:17 +0530851 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
852 if (acdb_dev_id <= 0) {
853 ALOGE("%s: Couldn't get the acdb dev id", __func__);
854 rc = -EINVAL;
855 goto exit_send_app_type_cfg;
856 }
857
Siena Richard7c2db772016-12-21 11:32:34 -0800858 snd_device_be_idx = platform_get_snd_device_backend_index(snd_device);
859 if (snd_device_be_idx < 0) {
860 ALOGE("%s: Couldn't get the backend index for snd device %s ret=%d",
861 __func__, platform_get_snd_device_name(snd_device),
862 snd_device_be_idx);
863 }
864
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530865 if ((usecase->type == PCM_PLAYBACK) && (usecase->stream.out != NULL)) {
Manish Dewangan837dc462015-05-27 10:17:41 +0530866
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530867 property_get("audio.playback.mch.downsample",value,"");
868 if (!strncmp("true", value, sizeof("true"))) {
869 if ((popcount(usecase->stream.out->channel_mask) > 2) &&
870 (usecase->stream.out->app_type_cfg.sample_rate > CODEC_BACKEND_DEFAULT_SAMPLE_RATE) &&
871 !(usecase->stream.out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH))
872 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
873 }
Ashish Jainc02430d2016-01-04 10:42:43 +0530874
Ashish Jain4826f6c2017-02-06 13:33:20 +0530875 if (usecase->stream.out->devices & AUDIO_DEVICE_OUT_SPEAKER) {
Mingming Yin21854652016-04-13 11:54:02 -0700876 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Ashish Jaina052e572016-11-07 16:41:07 +0530877 } else if ((snd_device == SND_DEVICE_OUT_HDMI ||
878 snd_device == SND_DEVICE_OUT_USB_HEADSET ||
879 snd_device == SND_DEVICE_OUT_DISPLAY_PORT) &&
880 (usecase->stream.out->sample_rate >= OUTPUT_SAMPLING_RATE_44100)) {
881 /*
882 * To best utlize DSP, check if the stream sample rate is supported/multiple of
883 * configured device sample rate, if not update the COPP rate to be equal to the
884 * device sample rate, else open COPP at stream sample rate
885 */
886 platform_check_and_update_copp_sample_rate(adev->platform, snd_device,
887 usecase->stream.out->sample_rate,
888 &usecase->stream.out->app_type_cfg.sample_rate);
Ashish Jain4826f6c2017-02-06 13:33:20 +0530889 } else if (((snd_device != SND_DEVICE_OUT_HEADPHONES_44_1 &&
890 !audio_is_this_native_usecase(usecase)) &&
Mingming Yin21854652016-04-13 11:54:02 -0700891 usecase->stream.out->sample_rate == OUTPUT_SAMPLING_RATE_44100) ||
892 (usecase->stream.out->sample_rate < OUTPUT_SAMPLING_RATE_44100)) {
Ashish Jain4826f6c2017-02-06 13:33:20 +0530893 /* Reset to default if no native stream is active*/
Mingming Yin21854652016-04-13 11:54:02 -0700894 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
895 }
896 sample_rate = usecase->stream.out->app_type_cfg.sample_rate;
897
Siena Richard7c2db772016-12-21 11:32:34 -0800898 app_type = usecase->stream.out->app_type_cfg.app_type;
899 app_type_cfg[len++] = app_type;
Mingming Yin21854652016-04-13 11:54:02 -0700900 app_type_cfg[len++] = acdb_dev_id;
901 if (((usecase->stream.out->format == AUDIO_FORMAT_E_AC3) ||
902 (usecase->stream.out->format == AUDIO_FORMAT_E_AC3_JOC))
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530903 && audio_extn_passthru_is_passthrough_stream(usecase->stream.out)) {
Mingming Yin21854652016-04-13 11:54:02 -0700904 app_type_cfg[len++] = sample_rate * 4;
905 } else {
906 app_type_cfg[len++] = sample_rate;
907 }
Siena Richard7c2db772016-12-21 11:32:34 -0800908 if (snd_device_be_idx > 0)
909 app_type_cfg[len++] = snd_device_be_idx;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530910
Siena Richard7c2db772016-12-21 11:32:34 -0800911 ALOGI("%s PLAYBACK app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
912 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530913
914 } else if ((usecase->type == PCM_CAPTURE) && (usecase->stream.in != NULL)) {
Siena Richard7c2db772016-12-21 11:32:34 -0800915 app_type = usecase->stream.in->app_type_cfg.app_type;
916 app_type_cfg[len++] = app_type;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530917 app_type_cfg[len++] = acdb_dev_id;
Siena Richard7c2db772016-12-21 11:32:34 -0800918 sample_rate = usecase->stream.in->app_type_cfg.sample_rate;
919 app_type_cfg[len++] = sample_rate;
920 if (snd_device_be_idx > 0)
921 app_type_cfg[len++] = snd_device_be_idx;
922 ALOGI("%s CAPTURE app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
923 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530924 } else {
Siena Richard7c2db772016-12-21 11:32:34 -0800925 app_type = platform_get_default_app_type_v2(adev->platform, usecase->type);
926 app_type_cfg[len++] = app_type;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530927 app_type_cfg[len++] = acdb_dev_id;
928 app_type_cfg[len++] = sample_rate;
Siena Richard7c2db772016-12-21 11:32:34 -0800929 if (snd_device_be_idx > 0)
930 app_type_cfg[len++] = snd_device_be_idx;
931 ALOGI("%s default app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
932 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Ashish Jainc02430d2016-01-04 10:42:43 +0530933 }
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +0530934
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700935 mixer_ctl_set_array(ctl, app_type_cfg, len);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700936 rc = 0;
937exit_send_app_type_cfg:
938 return rc;
939}
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700940
Siena Richard7c2db772016-12-21 11:32:34 -0800941int audio_extn_utils_send_app_type_cfg(struct audio_device *adev,
942 struct audio_usecase *usecase)
943{
944 int i, num_devices = 0;
945 snd_device_t new_snd_devices[SND_DEVICE_OUT_END] = {0};
946 int rc = 0;
947
948 switch (usecase->type) {
949 case PCM_PLAYBACK:
950 ALOGD("%s: usecase->out_snd_device %s",
951 __func__, platform_get_snd_device_name(usecase->out_snd_device));
952 /* check for out combo device */
953 if (platform_split_snd_device(adev->platform,
954 usecase->out_snd_device,
955 &num_devices, new_snd_devices)) {
956 new_snd_devices[0] = usecase->out_snd_device;
957 num_devices = 1;
958 }
959 break;
960 case PCM_CAPTURE:
961 ALOGD("%s: usecase->in_snd_device %s",
962 __func__, platform_get_snd_device_name(usecase->in_snd_device));
963 /* check for in combo device */
964 if (platform_split_snd_device(adev->platform,
965 usecase->in_snd_device,
966 &num_devices, new_snd_devices)) {
967 new_snd_devices[0] = usecase->in_snd_device;
968 num_devices = 1;
969 }
970 break;
971 default:
972 ALOGI("%s: not a playback/capture path, no need to cfg app type", __func__);
973 rc = 0;
974 break;
975 }
976
977 for (i = 0; i < num_devices; i++) {
978 rc = send_app_type_cfg_for_device(adev, usecase, new_snd_devices[i]);
979 if (rc)
980 break;
981 }
982
983 return rc;
984}
985
Preetam Singh Ranawat9519e9c2015-11-18 16:05:55 +0530986int read_line_from_file(const char *path, char *buf, size_t count)
987{
988 char * fgets_ret;
989 FILE * fd;
990 int rv;
991
992 fd = fopen(path, "r");
993 if (fd == NULL)
994 return -1;
995
996 fgets_ret = fgets(buf, (int)count, fd);
997 if (NULL != fgets_ret) {
998 rv = (int)strlen(buf);
999 } else {
1000 rv = ferror(fd);
1001 }
1002 fclose(fd);
1003
1004 return rv;
1005}
1006
Ashish Jainf1eaa582016-05-23 20:54:24 +05301007/*Translates ALSA formats to AOSP PCM formats*/
1008audio_format_t alsa_format_to_hal(uint32_t alsa_format)
1009{
1010 audio_format_t format;
1011
1012 switch(alsa_format) {
1013 case SNDRV_PCM_FORMAT_S16_LE:
1014 format = AUDIO_FORMAT_PCM_16_BIT;
1015 break;
1016 case SNDRV_PCM_FORMAT_S24_3LE:
1017 format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
1018 break;
1019 case SNDRV_PCM_FORMAT_S24_LE:
1020 format = AUDIO_FORMAT_PCM_8_24_BIT;
1021 break;
1022 case SNDRV_PCM_FORMAT_S32_LE:
1023 format = AUDIO_FORMAT_PCM_32_BIT;
1024 break;
1025 default:
1026 ALOGW("Incorrect ALSA format");
1027 format = AUDIO_FORMAT_INVALID;
1028 }
1029 return format;
1030}
1031
1032/*Translates hal format (AOSP) to alsa formats*/
1033uint32_t hal_format_to_alsa(audio_format_t hal_format)
1034{
1035 uint32_t alsa_format;
1036
1037 switch (hal_format) {
1038 case AUDIO_FORMAT_PCM_32_BIT: {
1039 if (platform_supports_true_32bit())
1040 alsa_format = SNDRV_PCM_FORMAT_S32_LE;
1041 else
1042 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1043 }
1044 break;
1045 case AUDIO_FORMAT_PCM_8_BIT:
1046 alsa_format = SNDRV_PCM_FORMAT_S8;
1047 break;
1048 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
1049 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1050 break;
1051 case AUDIO_FORMAT_PCM_8_24_BIT: {
1052 if (platform_supports_true_32bit())
1053 alsa_format = SNDRV_PCM_FORMAT_S32_LE;
1054 else
1055 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1056 }
1057 break;
1058 case AUDIO_FORMAT_PCM_FLOAT:
1059 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1060 break;
1061 default:
1062 case AUDIO_FORMAT_PCM_16_BIT:
1063 alsa_format = SNDRV_PCM_FORMAT_S16_LE;
1064 break;
1065 }
1066 return alsa_format;
1067}
1068
Ashish Jain83a6cc22016-06-28 14:34:17 +05301069/*Translates PCM formats to AOSP formats*/
1070audio_format_t pcm_format_to_hal(uint32_t pcm_format)
1071{
1072 audio_format_t format = AUDIO_FORMAT_INVALID;
1073
1074 switch(pcm_format) {
1075 case PCM_FORMAT_S16_LE:
1076 format = AUDIO_FORMAT_PCM_16_BIT;
1077 break;
1078 case PCM_FORMAT_S24_3LE:
1079 format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
1080 break;
1081 case PCM_FORMAT_S24_LE:
1082 format = AUDIO_FORMAT_PCM_8_24_BIT;
1083 break;
1084 case PCM_FORMAT_S32_LE:
1085 format = AUDIO_FORMAT_PCM_32_BIT;
1086 break;
1087 default:
1088 ALOGW("Incorrect PCM format");
1089 format = AUDIO_FORMAT_INVALID;
1090 }
1091 return format;
1092}
1093
1094/*Translates hal format (AOSP) to alsa formats*/
1095uint32_t hal_format_to_pcm(audio_format_t hal_format)
1096{
1097 uint32_t pcm_format;
1098
1099 switch (hal_format) {
1100 case AUDIO_FORMAT_PCM_32_BIT:
1101 case AUDIO_FORMAT_PCM_8_24_BIT:
1102 case AUDIO_FORMAT_PCM_FLOAT: {
1103 if (platform_supports_true_32bit())
1104 pcm_format = PCM_FORMAT_S32_LE;
1105 else
1106 pcm_format = PCM_FORMAT_S24_3LE;
1107 }
1108 break;
1109 case AUDIO_FORMAT_PCM_8_BIT:
1110 pcm_format = PCM_FORMAT_S8;
1111 break;
1112 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
1113 pcm_format = PCM_FORMAT_S24_3LE;
1114 break;
1115 default:
1116 case AUDIO_FORMAT_PCM_16_BIT:
1117 pcm_format = PCM_FORMAT_S16_LE;
1118 break;
1119 }
1120 return pcm_format;
1121}
1122
Ashish Jainf1eaa582016-05-23 20:54:24 +05301123uint32_t get_alsa_fragment_size(uint32_t bytes_per_sample,
1124 uint32_t sample_rate,
1125 uint32_t noOfChannels)
1126{
1127 uint32_t fragment_size = 0;
1128 uint32_t pcm_offload_time = PCM_OFFLOAD_BUFFER_DURATION;
1129
1130 fragment_size = (pcm_offload_time
1131 * sample_rate
1132 * bytes_per_sample
1133 * noOfChannels)/1000;
1134 if (fragment_size < MIN_PCM_OFFLOAD_FRAGMENT_SIZE)
1135 fragment_size = MIN_PCM_OFFLOAD_FRAGMENT_SIZE;
1136 else if (fragment_size > MAX_PCM_OFFLOAD_FRAGMENT_SIZE)
1137 fragment_size = MAX_PCM_OFFLOAD_FRAGMENT_SIZE;
1138 /*To have same PCM samples for all channels, the buffer size requires to
1139 *be multiple of (number of channels * bytes per sample)
1140 *For writes to succeed, the buffer must be written at address which is multiple of 32
1141 */
1142 fragment_size = ALIGN(fragment_size, (bytes_per_sample * noOfChannels * 32));
1143
1144 ALOGI("PCM offload Fragment size to %d bytes", fragment_size);
1145 return fragment_size;
1146}
1147
1148/* Calculates the fragment size required to configure compress session.
1149 * Based on the alsa format selected, decide if conversion is needed in
1150
1151 * HAL ( e.g. convert AUDIO_FORMAT_PCM_FLOAT input format to
1152 * AUDIO_FORMAT_PCM_24_BIT_PACKED before writing to the compress driver.
1153 */
1154void audio_extn_utils_update_direct_pcm_fragment_size(struct stream_out *out)
1155{
Ashish Jain83a6cc22016-06-28 14:34:17 +05301156 audio_format_t dst_format = out->hal_op_format;
1157 audio_format_t src_format = out->hal_ip_format;
Ashish Jainf1eaa582016-05-23 20:54:24 +05301158 uint32_t hal_op_bytes_per_sample = audio_bytes_per_sample(dst_format);
1159 uint32_t hal_ip_bytes_per_sample = audio_bytes_per_sample(src_format);
1160
1161 out->compr_config.fragment_size =
1162 get_alsa_fragment_size(hal_op_bytes_per_sample,
1163 out->sample_rate,
1164 popcount(out->channel_mask));
1165
1166 if ((src_format != dst_format) &&
1167 hal_op_bytes_per_sample != hal_ip_bytes_per_sample) {
1168
Ashish Jain83a6cc22016-06-28 14:34:17 +05301169 out->hal_fragment_size =
Ashish Jainf1eaa582016-05-23 20:54:24 +05301170 ((out->compr_config.fragment_size * hal_ip_bytes_per_sample) /
1171 hal_op_bytes_per_sample);
1172 ALOGI("enable conversion hal_input_fragment_size is %d src_format %x dst_format %x",
Ashish Jain83a6cc22016-06-28 14:34:17 +05301173 out->hal_fragment_size, src_format, dst_format);
Ashish Jainf1eaa582016-05-23 20:54:24 +05301174 } else {
Ashish Jain83a6cc22016-06-28 14:34:17 +05301175 out->hal_fragment_size = out->compr_config.fragment_size;
Ashish Jainf1eaa582016-05-23 20:54:24 +05301176 }
1177}
1178
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301179/* converts pcm format 24_8 to 8_24 inplace */
1180size_t audio_extn_utils_convert_format_24_8_to_8_24(void *buf, size_t bytes)
1181{
1182 size_t i = 0;
1183 int *int_buf_stream = buf;
1184
1185 if ((bytes % 4) != 0) {
1186 ALOGE("%s: wrong inout buffer! ... is not 32 bit aligned ", __func__);
1187 return -EINVAL;
1188 }
1189
1190 for (; i < (bytes / 4); i++)
1191 int_buf_stream[i] >>= 8;
1192
1193 return bytes;
1194}
1195
1196int get_snd_codec_id(audio_format_t format)
1197{
1198 int id = 0;
1199
1200 switch (format & AUDIO_FORMAT_MAIN_MASK) {
1201 case AUDIO_FORMAT_MP3:
1202 id = SND_AUDIOCODEC_MP3;
1203 break;
1204 case AUDIO_FORMAT_AAC:
1205 id = SND_AUDIOCODEC_AAC;
1206 break;
1207 case AUDIO_FORMAT_AAC_ADTS:
1208 id = SND_AUDIOCODEC_AAC;
1209 break;
Arun Kumar Dasari3b174182016-12-27 13:01:14 +05301210 case AUDIO_FORMAT_AAC_LATM:
1211 id = SND_AUDIOCODEC_AAC;
1212 break;
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301213 case AUDIO_FORMAT_PCM:
1214 id = SND_AUDIOCODEC_PCM;
1215 break;
1216 case AUDIO_FORMAT_FLAC:
1217 id = SND_AUDIOCODEC_FLAC;
1218 break;
1219 case AUDIO_FORMAT_ALAC:
1220 id = SND_AUDIOCODEC_ALAC;
1221 break;
1222 case AUDIO_FORMAT_APE:
1223 id = SND_AUDIOCODEC_APE;
1224 break;
1225 case AUDIO_FORMAT_VORBIS:
1226 id = SND_AUDIOCODEC_VORBIS;
1227 break;
1228 case AUDIO_FORMAT_WMA:
1229 id = SND_AUDIOCODEC_WMA;
1230 break;
1231 case AUDIO_FORMAT_WMA_PRO:
1232 id = SND_AUDIOCODEC_WMA_PRO;
1233 break;
Satish Babu Patakokila0c313922016-12-08 12:07:08 +05301234 case AUDIO_FORMAT_MP2:
1235 id = SND_AUDIOCODEC_MP2;
1236 break;
Satish Babu Patakokila915ecba2017-01-10 17:43:56 +05301237 case AUDIO_FORMAT_AC3:
1238 id = SND_AUDIOCODEC_AC3;
1239 break;
1240 case AUDIO_FORMAT_E_AC3:
1241 case AUDIO_FORMAT_E_AC3_JOC:
1242 id = SND_AUDIOCODEC_EAC3;
1243 break;
1244 case AUDIO_FORMAT_DTS:
1245 case AUDIO_FORMAT_DTS_HD:
1246 id = SND_AUDIOCODEC_DTS;
1247 break;
Preetam Singh Ranawat4277a5a2017-01-18 19:02:24 +05301248 case AUDIO_FORMAT_DSD:
1249 id = SND_AUDIOCODEC_DSD;
1250 break;
Dhanalakshmi Siddani18737932016-11-29 17:33:17 +05301251 case AUDIO_FORMAT_APTX:
1252 id = SND_AUDIOCODEC_APTX;
1253 break;
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301254 default:
1255 ALOGE("%s: Unsupported audio format :%x", __func__, format);
1256 }
1257
1258 return id;
1259}
1260
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001261void audio_extn_utils_send_audio_calibration(struct audio_device *adev,
1262 struct audio_usecase *usecase)
1263{
1264 int type = usecase->type;
1265
Dhananjay Kumar14245982017-01-16 20:21:00 +05301266 if (type == PCM_PLAYBACK && usecase->stream.out != NULL) {
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001267 struct stream_out *out = usecase->stream.out;
1268 int snd_device = usecase->out_snd_device;
1269 snd_device = (snd_device == SND_DEVICE_OUT_SPEAKER) ?
Xiaojun Sang040cc9f2015-08-03 19:38:28 +08001270 platform_get_spkr_prot_snd_device(snd_device) : snd_device;
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05301271 platform_send_audio_calibration(adev->platform, usecase,
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001272 out->app_type_cfg.app_type,
Preetam Singh Ranawat61716b12015-12-14 11:55:24 +05301273 usecase->stream.out->app_type_cfg.sample_rate);
Dhananjay Kumar14245982017-01-16 20:21:00 +05301274 } else if (type == PCM_CAPTURE && usecase->stream.in != NULL) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301275 platform_send_audio_calibration(adev->platform, usecase,
1276 usecase->stream.in->app_type_cfg.app_type,
1277 usecase->stream.in->app_type_cfg.sample_rate);
1278 } else {
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05301279 /* when app type is default. the sample rate is not used to send cal */
1280 platform_send_audio_calibration(adev->platform, usecase,
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301281 platform_get_default_app_type_v2(adev->platform, usecase->type),
1282 48000);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001283 }
1284}
1285
Ben Rombergera04fabc2014-11-14 12:16:03 -08001286// Base64 Encode and Decode
1287// Not all features supported. This must be used only with following conditions.
1288// Decode Modes: Support with and without padding
1289// CRLF not handling. So no CRLF in string to decode.
1290// Encode Modes: Supports only padding
1291int b64decode(char *inp, int ilen, uint8_t* outp)
1292{
1293 int i, j, k, ii, num;
1294 int rem, pcnt;
1295 uint32_t res=0;
1296 uint8_t getIndex[MAX_BASEINDEX_LEN];
1297 uint8_t tmp, cflag;
1298
1299 if(inp == NULL || outp == NULL || ilen <= 0) {
1300 ALOGE("[%s] received NULL pointer or zero length",__func__);
1301 return -1;
1302 }
1303
1304 memset(getIndex, MAX_BASEINDEX_LEN-1, sizeof(getIndex));
1305 for(i=0;i<BASE_TABLE_SIZE;i++) {
1306 getIndex[(uint8_t)bTable[i]] = (uint8_t)i;
1307 }
1308 getIndex[(uint8_t)'=']=0;
1309
1310 j=0;k=0;
1311 num = ilen/4;
1312 rem = ilen%4;
1313 if(rem==0)
1314 num = num-1;
1315 cflag=0;
1316 for(i=0; i<num; i++) {
1317 res=0;
1318 for(ii=0;ii<4;ii++) {
1319 res = res << 6;
1320 tmp = getIndex[(uint8_t)inp[j++]];
1321 res = res | tmp;
1322 cflag = cflag | tmp;
1323 }
1324 outp[k++] = (res >> 16)&0xFF;
1325 outp[k++] = (res >> 8)&0xFF;
1326 outp[k++] = res & 0xFF;
1327 }
1328
1329 // Handle last bytes special
1330 pcnt=0;
1331 if(rem == 0) {
1332 //With padding or full data
1333 res = 0;
1334 for(ii=0;ii<4;ii++) {
1335 if(inp[j] == '=')
1336 pcnt++;
1337 res = res << 6;
1338 tmp = getIndex[(uint8_t)inp[j++]];
1339 res = res | tmp;
1340 cflag = cflag | tmp;
1341 }
1342 outp[k++] = res >> 16;
1343 if(pcnt == 2)
1344 goto done;
1345 outp[k++] = (res>>8)&0xFF;
1346 if(pcnt == 1)
1347 goto done;
1348 outp[k++] = res&0xFF;
1349 } else {
1350 //without padding
1351 res = 0;
1352 for(i=0;i<rem;i++) {
1353 res = res << 6;
1354 tmp = getIndex[(uint8_t)inp[j++]];
1355 res = res | tmp;
1356 cflag = cflag | tmp;
1357 }
1358 for(i=rem;i<4;i++) {
1359 res = res << 6;
1360 pcnt++;
1361 }
1362 outp[k++] = res >> 16;
1363 if(pcnt == 2)
1364 goto done;
1365 outp[k++] = (res>>8)&0xFF;
1366 if(pcnt == 1)
1367 goto done;
1368 outp[k++] = res&0xFF;
1369 }
1370done:
1371 if(cflag == 0xFF) {
1372 ALOGE("[%s] base64 decode failed. Invalid character found %s",
1373 __func__, inp);
1374 return 0;
1375 }
1376 return k;
1377}
1378
1379int b64encode(uint8_t *inp, int ilen, char* outp)
1380{
1381 int i,j,k, num;
1382 int rem=0;
1383 uint32_t res=0;
1384
1385 if(inp == NULL || outp == NULL || ilen<=0) {
1386 ALOGE("[%s] received NULL pointer or zero input length",__func__);
1387 return -1;
1388 }
1389
1390 num = ilen/3;
1391 rem = ilen%3;
1392 j=0;k=0;
1393 for(i=0; i<num; i++) {
1394 //prepare index
1395 res = inp[j++]<<16;
1396 res = res | inp[j++]<<8;
1397 res = res | inp[j++];
1398 //get output map from index
1399 outp[k++] = (char) bTable[(res>>18)&0x3F];
1400 outp[k++] = (char) bTable[(res>>12)&0x3F];
1401 outp[k++] = (char) bTable[(res>>6)&0x3F];
1402 outp[k++] = (char) bTable[res&0x3F];
1403 }
1404
1405 switch(rem) {
1406 case 1:
1407 res = inp[j++]<<16;
1408 outp[k++] = (char) bTable[res>>18];
1409 outp[k++] = (char) bTable[(res>>12)&0x3F];
1410 //outp[k++] = '=';
1411 //outp[k++] = '=';
1412 break;
1413 case 2:
1414 res = inp[j++]<<16;
1415 res = res | inp[j++]<<8;
1416 outp[k++] = (char) bTable[res>>18];
1417 outp[k++] = (char) bTable[(res>>12)&0x3F];
1418 outp[k++] = (char) bTable[(res>>6)&0x3F];
1419 //outp[k++] = '=';
1420 break;
1421 default:
1422 break;
1423 }
Ben Rombergera04fabc2014-11-14 12:16:03 -08001424 outp[k] = '\0';
1425 return k;
1426}
Ashish Jain81eb2a82015-05-13 10:52:34 +05301427
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301428
1429int audio_extn_utils_get_codec_version(const char *snd_card_name,
1430 int card_num,
1431 char *codec_version)
1432{
1433 char procfs_path[50];
1434 FILE *fp;
1435
1436 if (strstr(snd_card_name, "tasha")) {
1437 snprintf(procfs_path, sizeof(procfs_path),
1438 "/proc/asound/card%d/codecs/tasha/version", card_num);
1439 if ((fp = fopen(procfs_path, "r")) != NULL) {
1440 fgets(codec_version, CODEC_VERSION_MAX_LENGTH, fp);
1441 fclose(fp);
1442 } else {
1443 ALOGE("%s: ERROR. cannot open %s", __func__, procfs_path);
1444 return -ENOENT;
1445 }
1446 ALOGD("%s: codec version %s", __func__, codec_version);
1447 }
1448
1449 return 0;
1450}
1451
1452
Ashish Jain81eb2a82015-05-13 10:52:34 +05301453#ifdef AUDIO_EXTERNAL_HDMI_ENABLED
1454
1455void get_default_compressed_channel_status(
1456 unsigned char *channel_status)
1457{
Ashish Jain81eb2a82015-05-13 10:52:34 +05301458 memset(channel_status,0,24);
1459
1460 /* block start bit in preamble bit 3 */
1461 channel_status[0] |= PROFESSIONAL;
1462 //compre out
1463 channel_status[0] |= NON_LPCM;
1464 // sample rate; fixed 48K for default/transcode
1465 channel_status[3] |= SR_48000;
1466}
1467
1468#ifdef HDMI_PASSTHROUGH_ENABLED
1469int32_t get_compressed_channel_status(void *audio_stream_data,
1470 uint32_t audio_frame_size,
1471 unsigned char *channel_status,
1472 enum audio_parser_code_type codec_type)
1473 // codec_type - AUDIO_PARSER_CODEC_AC3
1474 // - AUDIO_PARSER_CODEC_DTS
1475{
1476 unsigned char *stream;
1477 int ret = 0;
1478 stream = (unsigned char *)audio_stream_data;
1479
1480 if (audio_stream_data == NULL || audio_frame_size == 0) {
1481 ALOGW("no buffer to get channel status, return default for compress");
1482 get_default_compressed_channel_status(channel_status);
1483 return ret;
1484 }
1485
1486 memset(channel_status,0,24);
1487 if(init_audio_parser(stream, audio_frame_size, codec_type) == -1)
1488 {
1489 ALOGE("init audio parser failed");
1490 return -1;
1491 }
1492 ret = get_channel_status(channel_status, codec_type);
1493 return ret;
1494
1495}
1496
1497#endif
1498
1499void get_lpcm_channel_status(uint32_t sampleRate,
1500 unsigned char *channel_status)
1501{
1502 int32_t status = 0;
Ashish Jain81eb2a82015-05-13 10:52:34 +05301503 memset(channel_status,0,24);
1504 /* block start bit in preamble bit 3 */
1505 channel_status[0] |= PROFESSIONAL;
1506 //LPCM OUT
1507 channel_status[0] &= ~NON_LPCM;
1508
1509 switch (sampleRate) {
1510 case 8000:
1511 case 11025:
1512 case 12000:
1513 case 16000:
1514 case 22050:
1515 channel_status[3] |= SR_NOTID;
Preetam Singh Ranawat61716b12015-12-14 11:55:24 +05301516 break;
Ashish Jain81eb2a82015-05-13 10:52:34 +05301517 case 24000:
1518 channel_status[3] |= SR_24000;
1519 break;
1520 case 32000:
1521 channel_status[3] |= SR_32000;
1522 break;
1523 case 44100:
1524 channel_status[3] |= SR_44100;
1525 break;
1526 case 48000:
1527 channel_status[3] |= SR_48000;
1528 break;
1529 case 88200:
1530 channel_status[3] |= SR_88200;
1531 break;
1532 case 96000:
1533 channel_status[3] |= SR_96000;
1534 break;
1535 case 176400:
1536 channel_status[3] |= SR_176400;
1537 break;
1538 case 192000:
1539 channel_status[3] |= SR_192000;
1540 break;
1541 default:
1542 ALOGV("Invalid sample_rate %u\n", sampleRate);
1543 status = -1;
1544 break;
1545 }
1546}
1547
1548void audio_utils_set_hdmi_channel_status(struct stream_out *out, char * buffer, size_t bytes)
1549{
1550 unsigned char channel_status[24]={0};
1551 struct snd_aes_iec958 iec958;
1552 const char *mixer_ctl_name = "IEC958 Playback PCM Stream";
1553 struct mixer_ctl *ctl;
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +05301554 ALOGV("%s: buffer %s bytes %zd", __func__, buffer, bytes);
Ashish Jain81eb2a82015-05-13 10:52:34 +05301555#ifdef HDMI_PASSTHROUGH_ENABLED
1556 if (audio_extn_is_dolby_format(out->format) &&
1557 /*TODO:Extend code to support DTS passthrough*/
1558 /*set compressed channel status bits*/
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05301559 audio_extn_passthru_is_passthrough_stream(out)){
Ashish Jain81eb2a82015-05-13 10:52:34 +05301560 get_compressed_channel_status(buffer, bytes, channel_status, AUDIO_PARSER_CODEC_AC3);
1561 } else
1562#endif
1563 {
1564 /*set channel status bit for LPCM*/
1565 get_lpcm_channel_status(out->sample_rate, channel_status);
1566 }
1567
1568 memcpy(iec958.status, channel_status,sizeof(iec958.status));
1569 ctl = mixer_get_ctl_by_name(out->dev->mixer, mixer_ctl_name);
1570 if (!ctl) {
1571 ALOGE("%s: Could not get ctl for mixer cmd - %s",
1572 __func__, mixer_ctl_name);
1573 return;
1574 }
1575 if (mixer_ctl_set_array(ctl, &iec958, sizeof(iec958)) < 0) {
1576 ALOGE("%s: Could not set channel status for ext HDMI ",
1577 __func__);
1578 return;
1579 }
1580
1581}
1582#endif
Manish Dewangan3ccdea52017-02-13 19:31:54 +05301583
1584int audio_extn_utils_get_avt_device_drift(
1585 struct audio_usecase *usecase,
1586 struct audio_avt_device_drift_param *drift_param)
1587{
1588 int ret = 0, count = 0;
1589 char avt_device_drift_mixer_ctl_name[MIXER_PATH_MAX_LENGTH] = {0};
1590 struct mixer_ctl *ctl = NULL;
1591 struct audio_avt_device_drift_stats drift_stats;
1592 struct audio_device *adev = NULL;
1593
1594 if (usecase != NULL && usecase->type == PCM_PLAYBACK) {
1595 adev = usecase->stream.out->dev;
1596 switch(usecase->out_snd_device) {
1597 case SND_DEVICE_OUT_HDMI:
1598 strlcpy(avt_device_drift_mixer_ctl_name,
1599 "HDMI RX Drift",
1600 MIXER_PATH_MAX_LENGTH);
1601 break;
1602 case SND_DEVICE_OUT_DISPLAY_PORT:
1603 strlcpy(avt_device_drift_mixer_ctl_name,
1604 "DISPLAY Port RX Drift",
1605 MIXER_PATH_MAX_LENGTH);
1606 break;
1607 default :
1608 ALOGE("%s: Unsupported device %d",__func__,
1609 usecase->stream.out->devices);
1610 ret = -EINVAL;
1611 }
1612 } else {
1613 ALOGE("%s: Invalid usecase %d ",__func__, usecase->type);
1614 ret = -EINVAL;
1615 }
1616
1617 if(ret)
1618 goto done;
1619
1620 ctl = mixer_get_ctl_by_name(adev->mixer, avt_device_drift_mixer_ctl_name);
1621 if (!ctl) {
1622 ALOGE("%s: Could not get ctl for mixer cmd - %s",
1623 __func__, avt_device_drift_mixer_ctl_name);
1624
1625 ret = -EINVAL;
1626 goto done;
1627 }
1628
1629 ALOGV("%s: Getting AV Timer vs Device Drift mixer ctrl name %s", __func__,
1630 avt_device_drift_mixer_ctl_name);
1631
1632 mixer_ctl_update(ctl);
1633 count = mixer_ctl_get_num_values(ctl);
1634 if (count != sizeof(struct audio_avt_device_drift_stats)) {
1635 ALOGE("%s: mixer_ctl_get_num_values() invalid drift_stats data size",
1636 __func__);
1637
1638 ret = -EINVAL;
1639 goto done;
1640 }
1641
1642 ret = mixer_ctl_get_array(ctl, (void *)&drift_stats, count);
1643 if (ret != 0) {
1644 ALOGE("%s: mixer_ctl_get_array() failed to get drift_stats Params",
1645 __func__);
1646
1647 ret = -EINVAL;
1648 goto done;
1649 }
1650 memcpy(drift_param, &drift_stats.drift_param,
1651 sizeof(struct audio_avt_device_drift_param));
1652done:
1653 return ret;
1654}
Manish Dewangan07de2142017-02-27 19:27:20 +05301655
1656#ifdef SNDRV_COMPRESS_PATH_DELAY
1657int audio_extn_utils_compress_get_dsp_latency(struct stream_out *out)
1658{
1659 int ret = -EINVAL;
1660 struct snd_compr_metadata metadata;
1661 int delay_ms = COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
1662
1663 if (property_get_bool("audio.playback.dsp.pathdelay", false)) {
1664 ALOGD("%s:: Quering DSP delay %d",__func__, __LINE__);
1665 if (!(is_offload_usecase(out->usecase))) {
1666 ALOGE("%s:: not supported for non offload session", __func__);
1667 goto exit;
1668 }
1669
1670 if (!out->compr) {
1671 ALOGD("%s:: Invalid compress handle,returning default dsp latency",
1672 __func__);
1673 goto exit;
1674 }
1675
1676 metadata.key = SNDRV_COMPRESS_PATH_DELAY;
1677 ret = compress_get_metadata(out->compr, &metadata);
1678 if(ret) {
1679 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
1680 goto exit;
1681 }
1682 delay_ms = metadata.value[0] / 1000; /*convert to ms*/
1683 } else {
1684 ALOGD("%s:: Using Fix DSP delay",__func__);
1685 }
1686
1687exit:
1688 ALOGD("%s:: delay in ms is %d",__func__, delay_ms);
1689 return delay_ms;
1690}
1691#else
1692int audio_extn_utils_compress_get_dsp_latency(struct stream_out *out __unused)
1693{
1694 return COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
1695}
1696#endif