blob: 050dcd6eaa281b7a353536d2bb23f98fa96d3e36 [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),
98 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_PRIMARY),
99 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_FAST),
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800100 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_RAW),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700101 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_DEEP_BUFFER),
102 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD),
103 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_NON_BLOCKING),
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700104 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_HW_AV_SYNC),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700105#ifdef INCALL_MUSIC_ENABLED
106 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_INCALL_MUSIC),
107#endif
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700108 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH),
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530109 STRING_TO_ENUM(AUDIO_INPUT_FLAG_NONE),
110 STRING_TO_ENUM(AUDIO_INPUT_FLAG_FAST),
111 STRING_TO_ENUM(AUDIO_INPUT_FLAG_HW_HOTWORD),
112 STRING_TO_ENUM(AUDIO_INPUT_FLAG_RAW),
113 STRING_TO_ENUM(AUDIO_INPUT_FLAG_SYNC),
Dhananjay Kumaree4d2002016-10-25 18:02:58 +0530114 STRING_TO_ENUM(AUDIO_INPUT_FLAG_TIMESTAMP),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700115};
116
117const struct string_to_enum s_format_name_to_enum_table[] = {
Ashish Jain83a6cc22016-06-28 14:34:17 +0530118 STRING_TO_ENUM(AUDIO_FORMAT_PCM_8_BIT),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700119 STRING_TO_ENUM(AUDIO_FORMAT_PCM_16_BIT),
Ashish Jain5106d362016-05-11 19:23:33 +0530120 STRING_TO_ENUM(AUDIO_FORMAT_PCM_24_BIT_PACKED),
121 STRING_TO_ENUM(AUDIO_FORMAT_PCM_8_24_BIT),
Ashish Jainf1eaa582016-05-23 20:54:24 +0530122 STRING_TO_ENUM(AUDIO_FORMAT_PCM_32_BIT),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700123 STRING_TO_ENUM(AUDIO_FORMAT_MP3),
124 STRING_TO_ENUM(AUDIO_FORMAT_AAC),
125 STRING_TO_ENUM(AUDIO_FORMAT_VORBIS),
Mingming Yinae3530f2014-07-03 16:50:18 -0700126 STRING_TO_ENUM(AUDIO_FORMAT_AMR_NB),
127 STRING_TO_ENUM(AUDIO_FORMAT_AMR_WB),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700128 STRING_TO_ENUM(AUDIO_FORMAT_AC3),
Mingming Yinae3530f2014-07-03 16:50:18 -0700129 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700130 STRING_TO_ENUM(AUDIO_FORMAT_DTS),
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530131 STRING_TO_ENUM(AUDIO_FORMAT_DTS_HD),
132#ifdef AUDIO_EXTN_FORMATS_ENABLED
133 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3_JOC),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700134 STRING_TO_ENUM(AUDIO_FORMAT_WMA),
135 STRING_TO_ENUM(AUDIO_FORMAT_WMA_PRO),
136 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADIF),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700137 STRING_TO_ENUM(AUDIO_FORMAT_AMR_WB_PLUS),
138 STRING_TO_ENUM(AUDIO_FORMAT_EVRC),
139 STRING_TO_ENUM(AUDIO_FORMAT_EVRCB),
140 STRING_TO_ENUM(AUDIO_FORMAT_EVRCWB),
141 STRING_TO_ENUM(AUDIO_FORMAT_QCELP),
142 STRING_TO_ENUM(AUDIO_FORMAT_MP2),
143 STRING_TO_ENUM(AUDIO_FORMAT_EVRCNW),
Amit Shekhar6f461b12014-08-01 14:52:58 -0700144 STRING_TO_ENUM(AUDIO_FORMAT_FLAC),
Satya Krishna Pindiproli70471602015-04-24 19:12:43 +0530145 STRING_TO_ENUM(AUDIO_FORMAT_ALAC),
146 STRING_TO_ENUM(AUDIO_FORMAT_APE),
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700147 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3_JOC),
Alexy Josephcd8eaed2014-12-11 12:46:53 -0800148 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LC),
149 STRING_TO_ENUM(AUDIO_FORMAT_AAC_HE_V1),
150 STRING_TO_ENUM(AUDIO_FORMAT_AAC_HE_V2),
Manish Dewangana6fc5442015-08-24 20:30:31 +0530151 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS),
Ashish Jaine513a872015-11-19 17:00:56 +0530152 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_LC),
153 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_HE_V1),
154 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_HE_V2),
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +0530155 STRING_TO_ENUM(AUDIO_FORMAT_DSD),
Arun Kumar Dasari3b174182016-12-27 13:01:14 +0530156 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM),
157 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM_LC),
158 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM_HE_V1),
159 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM_HE_V2),
Dhanalakshmi Siddani18737932016-11-29 17:33:17 +0530160 STRING_TO_ENUM(AUDIO_FORMAT_APTX),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700161#endif
162};
163
Manish Dewangan3ccdea52017-02-13 19:31:54 +0530164/* payload structure avt_device drift query */
165struct audio_avt_device_drift_stats {
166 uint32_t minor_version;
167 /* Indicates the device interface direction as either
168 * source (Tx) or sink (Rx).
169 */
170 uint16_t device_direction;
171 /*params exposed to client */
172 struct audio_avt_device_drift_param drift_param;
173};
174
Ben Rombergera04fabc2014-11-14 12:16:03 -0800175static char bTable[BASE_TABLE_SIZE] = {
176 'A','B','C','D','E','F','G','H','I','J','K','L',
177 'M','N','O','P','Q','R','S','T','U','V','W','X',
178 'Y','Z','a','b','c','d','e','f','g','h','i','j',
179 'k','l','m','n','o','p','q','r','s','t','u','v',
180 'w','x','y','z','0','1','2','3','4','5','6','7',
181 '8','9','+','/'
182};
183
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700184static uint32_t string_to_enum(const struct string_to_enum *table, size_t size,
185 const char *name)
186{
187 size_t i;
188 for (i = 0; i < size; i++) {
189 if (strcmp(table[i].name, name) == 0) {
190 ALOGV("%s found %s", __func__, table[i].name);
191 return table[i].value;
192 }
193 }
194 return 0;
195}
196
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530197static audio_io_flags_t parse_flag_names(char *name)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700198{
199 uint32_t flag = 0;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530200 audio_io_flags_t io_flags;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800201 char *last_r;
202 char *flag_name = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700203 while (flag_name != NULL) {
204 if (strlen(flag_name) != 0) {
205 flag |= string_to_enum(s_flag_name_to_enum_table,
206 ARRAY_SIZE(s_flag_name_to_enum_table),
207 flag_name);
208 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800209 flag_name = strtok_r(NULL, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700210 }
211
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800212 ALOGV("parse_flag_names: flag - %x", flag);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530213 io_flags.in_flags = (audio_input_flags_t)flag;
214 io_flags.out_flags = (audio_output_flags_t)flag;
215 return io_flags;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700216}
217
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530218static void parse_format_names(char *name, struct streams_io_cfg *s_info)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700219{
220 struct stream_format *sf_info = NULL;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800221 char *last_r;
222 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700223
224 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG) == 0)
225 return;
226
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530227 list_init(&s_info->format_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700228 while (str != NULL) {
229 audio_format_t format = (audio_format_t)string_to_enum(s_format_name_to_enum_table,
230 ARRAY_SIZE(s_format_name_to_enum_table), str);
231 ALOGV("%s: format - %d", __func__, format);
232 if (format != 0) {
233 sf_info = (struct stream_format *)calloc(1, sizeof(struct stream_format));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700234 if (sf_info == NULL)
235 break; /* return whatever was parsed */
236
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700237 sf_info->format = format;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530238 list_add_tail(&s_info->format_list, &sf_info->list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700239 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800240 str = strtok_r(NULL, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700241 }
242}
243
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530244static void parse_sample_rate_names(char *name, struct streams_io_cfg *s_info)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700245{
Amit Shekhar6f461b12014-08-01 14:52:58 -0700246 struct stream_sample_rate *ss_info = NULL;
247 uint32_t sample_rate = 48000;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800248 char *last_r;
249 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700250
Amit Shekhar6f461b12014-08-01 14:52:58 -0700251 if (str != NULL && 0 == strcmp(str, DYNAMIC_VALUE_TAG))
252 return;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700253
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530254 list_init(&s_info->sample_rate_list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700255 while (str != NULL) {
256 sample_rate = (uint32_t)strtol(str, (char **)NULL, 10);
257 ALOGV("%s: sample_rate - %d", __func__, sample_rate);
258 if (0 != sample_rate) {
259 ss_info = (struct stream_sample_rate *)calloc(1, sizeof(struct stream_sample_rate));
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800260 if (!ss_info) {
261 ALOGE("%s: memory allocation failure", __func__);
262 return;
263 }
Amit Shekhar6f461b12014-08-01 14:52:58 -0700264 ss_info->sample_rate = sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530265 list_add_tail(&s_info->sample_rate_list, &ss_info->list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700266 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800267 str = strtok_r(NULL, "|", &last_r);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700268 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700269}
270
271static int parse_bit_width_names(char *name)
272{
273 int bit_width = 16;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800274 char *last_r;
275 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700276
277 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG))
278 bit_width = (int)strtol(str, (char **)NULL, 10);
279
280 ALOGV("%s: bit_width - %d", __func__, bit_width);
281 return bit_width;
282}
283
284static int parse_app_type_names(void *platform, char *name)
285{
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700286 int app_type = platform_get_default_app_type(platform);
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800287 char *last_r;
288 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700289
290 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG))
291 app_type = (int)strtol(str, (char **)NULL, 10);
292
293 ALOGV("%s: app_type - %d", __func__, app_type);
294 return app_type;
295}
296
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530297static void update_streams_cfg_list(cnode *root, void *platform,
298 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700299{
300 cnode *node = root->first_child;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530301 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700302
303 ALOGV("%s", __func__);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530304 s_info = (struct streams_io_cfg *)calloc(1, sizeof(struct streams_io_cfg));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700305
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530306 if (!s_info) {
307 ALOGE("failed to allocate mem for s_info list element");
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700308 return;
309 }
310
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700311 while (node) {
312 if (strcmp(node->name, FLAGS_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530313 s_info->flags = parse_flag_names((char *)node->value);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530314 } else if (strcmp(node->name, PROFILES_TAG) == 0) {
315 strlcpy(s_info->profile, (char *)node->value, sizeof(s_info->profile));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700316 } else if (strcmp(node->name, FORMATS_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530317 parse_format_names((char *)node->value, s_info);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700318 } else if (strcmp(node->name, SAMPLING_RATES_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530319 s_info->app_type_cfg.sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
320 parse_sample_rate_names((char *)node->value, s_info);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700321 } else if (strcmp(node->name, BIT_WIDTH_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530322 s_info->app_type_cfg.bit_width = parse_bit_width_names((char *)node->value);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700323 } else if (strcmp(node->name, APP_TYPE_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530324 s_info->app_type_cfg.app_type = parse_app_type_names(platform, (char *)node->value);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700325 }
326 node = node->next;
327 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530328 list_add_tail(streams_cfg_list, &s_info->list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700329}
330
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530331static void load_cfg_list(cnode *root, void *platform,
332 struct listnode *streams_output_cfg_list,
333 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700334{
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530335 cnode *node = NULL;
336
337 node = config_find(root, OUTPUTS_TAG);
338 if (node != NULL) {
339 node = node->first_child;
340 while (node) {
341 ALOGV("%s: loading output %s", __func__, node->name);
342 update_streams_cfg_list(node, platform, streams_output_cfg_list);
343 node = node->next;
344 }
345 } else {
346 ALOGI("%s: could not load output, node is NULL", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700347 }
348
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530349 node = config_find(root, INPUTS_TAG);
350 if (node != NULL) {
351 node = node->first_child;
352 while (node) {
353 ALOGV("%s: loading input %s", __func__, node->name);
354 update_streams_cfg_list(node, platform, streams_input_cfg_list);
355 node = node->next;
356 }
357 } else {
358 ALOGI("%s: could not load input, node is NULL", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700359 }
360}
361
362static void send_app_type_cfg(void *platform, struct mixer *mixer,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530363 struct listnode *streams_output_cfg_list,
364 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700365{
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530366 size_t app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT] = {0};
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700367 int length = 0, i, num_app_types = 0;
368 struct listnode *node;
369 bool update;
370 struct mixer_ctl *ctl = NULL;
371 const char *mixer_ctl_name = "App Type Config";
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530372 struct streams_io_cfg *s_info = NULL;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700373
374 if (!mixer) {
375 ALOGE("%s: mixer is null",__func__);
376 return;
377 }
378 ctl = mixer_get_ctl_by_name(mixer, mixer_ctl_name);
379 if (!ctl) {
380 ALOGE("%s: Could not get ctl for mixer cmd - %s",__func__, mixer_ctl_name);
381 return;
382 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530383 app_type_cfg[length++] = num_app_types;
384
385 if (list_empty(streams_output_cfg_list)) {
386 app_type_cfg[length++] = platform_get_default_app_type_v2(platform, PCM_PLAYBACK);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700387 app_type_cfg[length++] = 48000;
388 app_type_cfg[length++] = 16;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530389 num_app_types += 1;
390 }
391 if (list_empty(streams_input_cfg_list)) {
392 app_type_cfg[length++] = platform_get_default_app_type_v2(platform, PCM_CAPTURE);
393 app_type_cfg[length++] = 48000;
394 app_type_cfg[length++] = 16;
395 num_app_types += 1;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700396 }
397
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700398 list_for_each(node, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530399 s_info = node_to_item(node, struct streams_io_cfg, list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700400 update = true;
401 for (i=0; i<length; i=i+3) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530402 if (app_type_cfg[i+1] == 0)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700403 break;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530404 else if (app_type_cfg[i+1] == (size_t)s_info->app_type_cfg.app_type) {
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530405 if (app_type_cfg[i+2] < (size_t)s_info->app_type_cfg.sample_rate)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530406 app_type_cfg[i+2] = s_info->app_type_cfg.sample_rate;
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530407 if (app_type_cfg[i+3] < (size_t)s_info->app_type_cfg.bit_width)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530408 app_type_cfg[i+3] = s_info->app_type_cfg.bit_width;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700409 update = false;
410 break;
411 }
412 }
413 if (update && ((length + 3) <= MAX_LENGTH_MIXER_CONTROL_IN_INT)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530414 num_app_types += 1;
415 app_type_cfg[length++] = s_info->app_type_cfg.app_type;
416 app_type_cfg[length++] = s_info->app_type_cfg.sample_rate;
417 app_type_cfg[length++] = s_info->app_type_cfg.bit_width;
418 }
419 }
420 list_for_each(node, streams_input_cfg_list) {
421 s_info = node_to_item(node, struct streams_io_cfg, list);
422 update = true;
423 for (i=0; i<length; i=i+3) {
424 if (app_type_cfg[i+1] == 0)
425 break;
426 else if (app_type_cfg[i+1] == (size_t)s_info->app_type_cfg.app_type) {
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530427 if (app_type_cfg[i+2] < (size_t)s_info->app_type_cfg.sample_rate)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530428 app_type_cfg[i+2] = s_info->app_type_cfg.sample_rate;
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530429 if (app_type_cfg[i+3] < (size_t)s_info->app_type_cfg.bit_width)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530430 app_type_cfg[i+3] = s_info->app_type_cfg.bit_width;
431 update = false;
432 break;
433 }
434 }
435 if (update && ((length + 3) <= MAX_LENGTH_MIXER_CONTROL_IN_INT)) {
436 num_app_types += 1;
437 app_type_cfg[length++] = s_info->app_type_cfg.app_type;
438 app_type_cfg[length++] = s_info->app_type_cfg.sample_rate;
439 app_type_cfg[length++] = s_info->app_type_cfg.bit_width;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700440 }
441 }
442 ALOGV("%s: num_app_types: %d", __func__, num_app_types);
443 if (num_app_types) {
444 app_type_cfg[0] = num_app_types;
445 mixer_ctl_set_array(ctl, app_type_cfg, length);
446 }
447}
448
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530449void audio_extn_utils_update_streams_cfg_lists(void *platform,
450 struct mixer *mixer,
451 struct listnode *streams_output_cfg_list,
452 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700453{
454 cnode *root;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530455 char *data = NULL;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700456
457 ALOGV("%s", __func__);
458 list_init(streams_output_cfg_list);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530459 list_init(streams_input_cfg_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700460
461 root = config_node("", "");
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700462 if (root == NULL) {
463 ALOGE("cfg_list, NULL config root");
464 return;
465 }
466
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530467 data = (char *)load_file(AUDIO_IO_POLICY_VENDOR_CONFIG_FILE, NULL);
468 if (data == NULL) {
469 ALOGD("%s: failed to open io config file(%s), trying older config file",
470 __func__, AUDIO_IO_POLICY_VENDOR_CONFIG_FILE);
471 data = (char *)load_file(AUDIO_OUTPUT_POLICY_VENDOR_CONFIG_FILE, NULL);
472 if (data == NULL) {
473 send_app_type_cfg(platform, mixer,
474 streams_output_cfg_list,
475 streams_input_cfg_list);
476 ALOGE("%s: could not load io policy config!", __func__);
yidongh6eb4f752017-04-19 18:20:57 +0800477 free(root);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530478 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);
yidongh6eb4f752017-04-19 18:20:57 +0800490 free(root);
Alexy Josephaee4fdd2016-01-29 13:02:07 -0800491 free(data);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700492}
493
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530494static void audio_extn_utils_dump_streams_cfg_list(
495 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700496{
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700497 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530498 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700499 struct stream_format *sf_info;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700500 struct stream_sample_rate *ss_info;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530501
502 list_for_each(node_i, streams_cfg_list) {
503 s_info = node_to_item(node_i, struct streams_io_cfg, list);
504 ALOGV("%s: flags-%d, sample_rate-%d, bit_width-%d, app_type-%d",
505 __func__, s_info->flags.out_flags, s_info->app_type_cfg.sample_rate,
506 s_info->app_type_cfg.bit_width, s_info->app_type_cfg.app_type);
507 list_for_each(node_j, &s_info->format_list) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700508 sf_info = node_to_item(node_j, struct stream_format, list);
509 ALOGV("format-%x", sf_info->format);
510 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530511 list_for_each(node_j, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700512 ss_info = node_to_item(node_j, struct stream_sample_rate, list);
513 ALOGV("sample rate-%d", ss_info->sample_rate);
514 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700515 }
516}
517
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530518void audio_extn_utils_dump_streams_cfg_lists(
519 struct listnode *streams_output_cfg_list,
520 struct listnode *streams_input_cfg_list)
521{
522 ALOGV("%s", __func__);
523 audio_extn_utils_dump_streams_cfg_list(streams_output_cfg_list);
524 audio_extn_utils_dump_streams_cfg_list(streams_input_cfg_list);
525}
526
527static void audio_extn_utils_release_streams_cfg_list(
528 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700529{
530 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530531 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700532
533 ALOGV("%s", __func__);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530534
535 while (!list_empty(streams_cfg_list)) {
536 node_i = list_head(streams_cfg_list);
537 s_info = node_to_item(node_i, struct streams_io_cfg, list);
538 while (!list_empty(&s_info->format_list)) {
539 node_j = list_head(&s_info->format_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700540 list_remove(node_j);
541 free(node_to_item(node_j, struct stream_format, list));
542 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530543 while (!list_empty(&s_info->sample_rate_list)) {
544 node_j = list_head(&s_info->sample_rate_list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700545 list_remove(node_j);
546 free(node_to_item(node_j, struct stream_sample_rate, list));
547 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700548 list_remove(node_i);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530549 free(node_to_item(node_i, struct streams_io_cfg, list));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700550 }
551}
552
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530553void audio_extn_utils_release_streams_cfg_lists(
554 struct listnode *streams_output_cfg_list,
555 struct listnode *streams_input_cfg_list)
556{
557 ALOGV("%s", __func__);
558 audio_extn_utils_release_streams_cfg_list(streams_output_cfg_list);
559 audio_extn_utils_release_streams_cfg_list(streams_input_cfg_list);
560}
561
562static bool set_app_type_cfg(struct streams_io_cfg *s_info,
563 struct stream_app_type_cfg *app_type_cfg,
564 uint32_t sample_rate, uint32_t bit_width)
Amit Shekhar6f461b12014-08-01 14:52:58 -0700565 {
566 struct listnode *node_i;
567 struct stream_sample_rate *ss_info;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530568 list_for_each(node_i, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700569 ss_info = node_to_item(node_i, struct stream_sample_rate, list);
570 if ((sample_rate <= ss_info->sample_rate) &&
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530571 (bit_width == s_info->app_type_cfg.bit_width)) {
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700572
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530573 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700574 app_type_cfg->sample_rate = ss_info->sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530575 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700576 ALOGV("%s app_type_cfg->app_type %d, app_type_cfg->sample_rate %d, app_type_cfg->bit_width %d",
577 __func__, app_type_cfg->app_type, app_type_cfg->sample_rate, app_type_cfg->bit_width);
578 return true;
579 }
580 }
581 /*
582 * Reiterate through the list assuming dafault sample rate.
583 * Handles scenario where input sample rate is higher
584 * than all sample rates in list for the input bit width.
585 */
586 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
Apoorv Raghuvanshif59bb222015-02-18 12:23:23 -0800587
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530588 list_for_each(node_i, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700589 ss_info = node_to_item(node_i, struct stream_sample_rate, list);
590 if ((sample_rate <= ss_info->sample_rate) &&
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530591 (bit_width == s_info->app_type_cfg.bit_width)) {
592 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700593 app_type_cfg->sample_rate = sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530594 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Apoorv Raghuvanshif59bb222015-02-18 12:23:23 -0800595 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 -0700596 __func__, app_type_cfg->app_type, app_type_cfg->sample_rate, app_type_cfg->bit_width);
597 return true;
598 }
599 }
600 return false;
601}
602
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530603void audio_extn_utils_update_stream_input_app_type_cfg(void *platform,
604 struct listnode *streams_input_cfg_list,
605 audio_devices_t devices __unused,
606 audio_input_flags_t flags,
607 audio_format_t format,
608 uint32_t sample_rate,
609 uint32_t bit_width,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530610 char* profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530611 struct stream_app_type_cfg *app_type_cfg)
612{
613 struct listnode *node_i, *node_j;
614 struct streams_io_cfg *s_info;
615 struct stream_format *sf_info;
616
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530617 ALOGV("%s: flags: 0x%x, format: 0x%x sample_rate %d, profile %s",
618 __func__, flags, format, sample_rate, profile);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530619
620 list_for_each(node_i, streams_input_cfg_list) {
621 s_info = node_to_item(node_i, struct streams_io_cfg, list);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530622 /* Along with flags do profile matching if set at either end.*/
623 if (s_info->flags.in_flags == flags &&
624 ((profile[0] == '\0' && s_info->profile[0] == '\0') ||
625 strncmp(s_info->profile, profile, sizeof(s_info->profile)) == 0)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530626 list_for_each(node_j, &s_info->format_list) {
627 sf_info = node_to_item(node_j, struct stream_format, list);
628 if (sf_info->format == format) {
629 if (set_app_type_cfg(s_info, app_type_cfg, sample_rate, bit_width))
630 return;
631 }
632 }
633 }
634 }
635 ALOGW("%s: App type could not be selected. Falling back to default", __func__);
636 app_type_cfg->app_type = platform_get_default_app_type_v2(platform, PCM_CAPTURE);
637 app_type_cfg->sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
638 app_type_cfg->bit_width = 16;
639}
640
641void audio_extn_utils_update_stream_output_app_type_cfg(void *platform,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700642 struct listnode *streams_output_cfg_list,
Amit Shekhar1d896042014-10-03 13:16:09 -0700643 audio_devices_t devices,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700644 audio_output_flags_t flags,
645 audio_format_t format,
Amit Shekhar6f461b12014-08-01 14:52:58 -0700646 uint32_t sample_rate,
647 uint32_t bit_width,
Manish Dewangan837dc462015-05-27 10:17:41 +0530648 audio_channel_mask_t channel_mask,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530649 char *profile,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700650 struct stream_app_type_cfg *app_type_cfg)
651{
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +0530652 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530653 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700654 struct stream_format *sf_info;
Manish Dewangan837dc462015-05-27 10:17:41 +0530655 char value[PROPERTY_VALUE_MAX] = {0};
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700656
Ashish Jain058165c2016-09-28 23:18:48 +0530657 if ((bit_width >= 24) &&
Amit Shekhar1d896042014-10-03 13:16:09 -0700658 (devices & AUDIO_DEVICE_OUT_SPEAKER)) {
Amit Shekhar5a39c912014-10-14 15:39:30 -0700659 int32_t bw = platform_get_snd_device_bit_width(SND_DEVICE_OUT_SPEAKER);
660 if (-ENOSYS != bw)
661 bit_width = (uint32_t)bw;
Amit Shekhar1d896042014-10-03 13:16:09 -0700662 sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
663 ALOGI("%s Allowing 24-bit playback on speaker ONLY at default sampling rate", __func__);
664 }
665
Manish Dewangan837dc462015-05-27 10:17:41 +0530666 property_get("audio.playback.mch.downsample",value,"");
667 if (!strncmp("true", value, sizeof("true"))) {
668 if ((popcount(channel_mask) > 2) &&
669 (sample_rate > CODEC_BACKEND_DEFAULT_SAMPLE_RATE) &&
670 !(flags & AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH)) {
671 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
672 ALOGD("%s: MCH session defaulting sample rate to %d",
673 __func__, sample_rate);
674 }
675 }
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +0530676
677 /* Set sampling rate to 176.4 for DSD64
678 * and 352.8Khz for DSD128.
679 * Set Bit Width to 16. output will be 16 bit
680 * post DoP in ASM.
681 */
682 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH) &&
683 (format == AUDIO_FORMAT_DSD)) {
684 bit_width = 16;
685 if (sample_rate == INPUT_SAMPLING_RATE_DSD64)
686 sample_rate = OUTPUT_SAMPLING_RATE_DSD64;
687 else if (sample_rate == INPUT_SAMPLING_RATE_DSD128)
688 sample_rate = OUTPUT_SAMPLING_RATE_DSD128;
689 }
690
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +0530691 if(devices & AUDIO_DEVICE_OUT_ALL_A2DP) {
692 //TODO: Handle fractional sampling rate configuration for LL
693 audio_extn_a2dp_get_apptype_params(&sample_rate, &bit_width);
694 ALOGI("%s using %d sampling rate %d bit width for A2DP CoPP",
695 __func__, sample_rate, bit_width);
696 }
697
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800698 ALOGV("%s: flags: %x, format: %x sample_rate %d, profile %s, app_type %d",
699 __func__, flags, format, sample_rate, profile, app_type_cfg->app_type);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700700 list_for_each(node_i, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530701 s_info = node_to_item(node_i, struct streams_io_cfg, list);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530702 /* Along with flags do profile matching if set at either end.*/
703 if (s_info->flags.out_flags == flags &&
704 ((profile[0] == '\0' && s_info->profile[0] == '\0') ||
705 strncmp(s_info->profile, profile, sizeof(s_info->profile)) == 0)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530706 list_for_each(node_j, &s_info->format_list) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700707 sf_info = node_to_item(node_j, struct stream_format, list);
708 if (sf_info->format == format) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530709 if (set_app_type_cfg(s_info, app_type_cfg, sample_rate, bit_width))
Amit Shekhar6f461b12014-08-01 14:52:58 -0700710 return;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700711 }
712 }
713 }
714 }
715 list_for_each(node_i, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530716 s_info = node_to_item(node_i, struct streams_io_cfg, list);
717 if (s_info->flags.out_flags == AUDIO_OUTPUT_FLAG_PRIMARY) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700718 ALOGV("Compatible output profile not found.");
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530719 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
720 app_type_cfg->sample_rate = s_info->app_type_cfg.sample_rate;
721 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700722 ALOGV("%s Default to primary output: App type: %d sample_rate %d",
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530723 __func__, s_info->app_type_cfg.app_type, app_type_cfg->sample_rate);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700724 return;
725 }
726 }
727 ALOGW("%s: App type could not be selected. Falling back to default", __func__);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700728 app_type_cfg->app_type = platform_get_default_app_type(platform);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700729 app_type_cfg->sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700730 app_type_cfg->bit_width = 16;
731}
732
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +0530733static bool audio_is_this_native_usecase(struct audio_usecase *uc)
734{
735 bool native_usecase = false;
736 struct stream_out *out = (struct stream_out*) uc->stream.out;
737
738 if (PCM_PLAYBACK == uc->type && out != NULL &&
739 NATIVE_AUDIO_MODE_INVALID != platform_get_native_support() &&
740 is_offload_usecase(uc->id) &&
741 (out->sample_rate == OUTPUT_SAMPLING_RATE_44100))
742 native_usecase = true;
743
744 return native_usecase;
745}
746
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800747
748static inline bool audio_is_vr_mode_on(struct audio_device *(__attribute__((unused)) adev))
749{
750 return adev->vr_audio_mode_enabled;
751}
752
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530753void audio_extn_utils_update_stream_app_type_cfg_for_usecase(
754 struct audio_device *adev,
755 struct audio_usecase *usecase)
756{
757 ALOGV("%s", __func__);
758
759 switch(usecase->type) {
760 case PCM_PLAYBACK:
761 audio_extn_utils_update_stream_output_app_type_cfg(adev->platform,
762 &adev->streams_output_cfg_list,
763 usecase->stream.out->devices,
764 usecase->stream.out->flags,
765 usecase->stream.out->format,
766 usecase->stream.out->sample_rate,
767 usecase->stream.out->bit_width,
768 usecase->stream.out->channel_mask,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530769 usecase->stream.out->profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530770 &usecase->stream.out->app_type_cfg);
771 ALOGV("%s Selected apptype: %d", __func__, usecase->stream.out->app_type_cfg.app_type);
772 break;
773 case PCM_CAPTURE:
774 audio_extn_utils_update_stream_input_app_type_cfg(adev->platform,
775 &adev->streams_input_cfg_list,
776 usecase->stream.in->device,
777 usecase->stream.in->flags,
778 usecase->stream.in->format,
779 usecase->stream.in->sample_rate,
780 usecase->stream.in->bit_width,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530781 usecase->stream.in->profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530782 &usecase->stream.in->app_type_cfg);
783 ALOGV("%s Selected apptype: %d", __func__, usecase->stream.in->app_type_cfg.app_type);
784 break;
785 default:
786 ALOGE("%s: app type cfg not supported for usecase type (%d)",
787 __func__, usecase->type);
788 }
789}
790
Siena Richard7c2db772016-12-21 11:32:34 -0800791static int send_app_type_cfg_for_device(struct audio_device *adev,
792 struct audio_usecase *usecase,
793 int split_snd_device)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700794{
795 char mixer_ctl_name[MAX_LENGTH_MIXER_CONTROL_IN_INT];
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530796 size_t app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT] = {0};
797 int len = 0, rc;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700798 struct mixer_ctl *ctl;
Siena Richard7c2db772016-12-21 11:32:34 -0800799 int pcm_device_id = 0, acdb_dev_id, app_type;
800 int snd_device = split_snd_device, snd_device_be_idx = -1;
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +0530801 int32_t sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Manish Dewangan837dc462015-05-27 10:17:41 +0530802 char value[PROPERTY_VALUE_MAX] = {0};
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700803
Siena Richard7c2db772016-12-21 11:32:34 -0800804 ALOGV("%s: usecase->out_snd_device %s, usecase->in_snd_device %s, split_snd_device %s",
805 __func__, platform_get_snd_device_name(usecase->out_snd_device),
806 platform_get_snd_device_name(usecase->in_snd_device),
807 platform_get_snd_device_name(split_snd_device));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700808
Srikanth Uyyala9d551402015-08-25 16:03:42 +0530809 if (usecase->type != PCM_PLAYBACK && usecase->type != PCM_CAPTURE) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530810 ALOGE("%s: not a playback/capture path, no need to cfg app type", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700811 rc = 0;
812 goto exit_send_app_type_cfg;
813 }
814 if ((usecase->id != USECASE_AUDIO_PLAYBACK_DEEP_BUFFER) &&
815 (usecase->id != USECASE_AUDIO_PLAYBACK_LOW_LATENCY) &&
816 (usecase->id != USECASE_AUDIO_PLAYBACK_MULTI_CH) &&
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800817 (usecase->id != USECASE_AUDIO_PLAYBACK_ULL) &&
Srikanth Uyyala9d551402015-08-25 16:03:42 +0530818 (!is_offload_usecase(usecase->id)) &&
819 (usecase->type != PCM_CAPTURE)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530820 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 -0700821 rc = 0;
822 goto exit_send_app_type_cfg;
823 }
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800824
825 //if VR is active then only send the mixer control
826 if (usecase->id == USECASE_AUDIO_PLAYBACK_ULL && !audio_is_vr_mode_on(adev)) {
827 ALOGI("ULL doesnt need sending app type cfg, returning");
828 rc = 0;
829 goto exit_send_app_type_cfg;
830 }
831
Srikanth Uyyala9d551402015-08-25 16:03:42 +0530832 if (usecase->type == PCM_PLAYBACK) {
Ben Romberger1fafdde2015-09-09 19:43:15 -0700833 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_PLAYBACK);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530834 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
835 "Audio Stream %d App Type Cfg", pcm_device_id);
Ben Romberger1fafdde2015-09-09 19:43:15 -0700836 } else if (usecase->type == PCM_CAPTURE) {
Ben Romberger1fafdde2015-09-09 19:43:15 -0700837 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_CAPTURE);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530838 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
839 "Audio Stream Capture %d App Type Cfg", pcm_device_id);
Srikanth Uyyala9d551402015-08-25 16:03:42 +0530840 }
Siena Richard7c2db772016-12-21 11:32:34 -0800841
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700842 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
843 if (!ctl) {
844 ALOGE("%s: Could not get ctl for mixer cmd - %s", __func__,
845 mixer_ctl_name);
846 rc = -EINVAL;
847 goto exit_send_app_type_cfg;
848 }
Aditya Bavanari701a6992017-03-30 19:17:16 +0530849 snd_device = platform_get_spkr_prot_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);
Vidyakumar Athota653aaef2017-03-16 11:11:31 -07001278 } else if (type == PCM_HFP_CALL || type == PCM_CAPTURE) {
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);
Vidyakumar Athotae57f6002017-03-01 13:13:16 -08001283 } else {
1284 /* No need to send audio calibration for voice and voip call usecases */
1285 if ((type != VOICE_CALL) && (type != VOIP_CALL))
1286 ALOGW("%s: No audio calibration for usecase type = %d", __func__, type);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001287 }
1288}
1289
Ben Rombergera04fabc2014-11-14 12:16:03 -08001290// Base64 Encode and Decode
1291// Not all features supported. This must be used only with following conditions.
1292// Decode Modes: Support with and without padding
1293// CRLF not handling. So no CRLF in string to decode.
1294// Encode Modes: Supports only padding
1295int b64decode(char *inp, int ilen, uint8_t* outp)
1296{
1297 int i, j, k, ii, num;
1298 int rem, pcnt;
1299 uint32_t res=0;
1300 uint8_t getIndex[MAX_BASEINDEX_LEN];
1301 uint8_t tmp, cflag;
1302
1303 if(inp == NULL || outp == NULL || ilen <= 0) {
1304 ALOGE("[%s] received NULL pointer or zero length",__func__);
1305 return -1;
1306 }
1307
1308 memset(getIndex, MAX_BASEINDEX_LEN-1, sizeof(getIndex));
1309 for(i=0;i<BASE_TABLE_SIZE;i++) {
1310 getIndex[(uint8_t)bTable[i]] = (uint8_t)i;
1311 }
1312 getIndex[(uint8_t)'=']=0;
1313
1314 j=0;k=0;
1315 num = ilen/4;
1316 rem = ilen%4;
1317 if(rem==0)
1318 num = num-1;
1319 cflag=0;
1320 for(i=0; i<num; i++) {
1321 res=0;
1322 for(ii=0;ii<4;ii++) {
1323 res = res << 6;
1324 tmp = getIndex[(uint8_t)inp[j++]];
1325 res = res | tmp;
1326 cflag = cflag | tmp;
1327 }
1328 outp[k++] = (res >> 16)&0xFF;
1329 outp[k++] = (res >> 8)&0xFF;
1330 outp[k++] = res & 0xFF;
1331 }
1332
1333 // Handle last bytes special
1334 pcnt=0;
1335 if(rem == 0) {
1336 //With padding or full data
1337 res = 0;
1338 for(ii=0;ii<4;ii++) {
1339 if(inp[j] == '=')
1340 pcnt++;
1341 res = res << 6;
1342 tmp = getIndex[(uint8_t)inp[j++]];
1343 res = res | tmp;
1344 cflag = cflag | tmp;
1345 }
1346 outp[k++] = res >> 16;
1347 if(pcnt == 2)
1348 goto done;
1349 outp[k++] = (res>>8)&0xFF;
1350 if(pcnt == 1)
1351 goto done;
1352 outp[k++] = res&0xFF;
1353 } else {
1354 //without padding
1355 res = 0;
1356 for(i=0;i<rem;i++) {
1357 res = res << 6;
1358 tmp = getIndex[(uint8_t)inp[j++]];
1359 res = res | tmp;
1360 cflag = cflag | tmp;
1361 }
1362 for(i=rem;i<4;i++) {
1363 res = res << 6;
1364 pcnt++;
1365 }
1366 outp[k++] = res >> 16;
1367 if(pcnt == 2)
1368 goto done;
1369 outp[k++] = (res>>8)&0xFF;
1370 if(pcnt == 1)
1371 goto done;
1372 outp[k++] = res&0xFF;
1373 }
1374done:
1375 if(cflag == 0xFF) {
1376 ALOGE("[%s] base64 decode failed. Invalid character found %s",
1377 __func__, inp);
1378 return 0;
1379 }
1380 return k;
1381}
1382
1383int b64encode(uint8_t *inp, int ilen, char* outp)
1384{
1385 int i,j,k, num;
1386 int rem=0;
1387 uint32_t res=0;
1388
1389 if(inp == NULL || outp == NULL || ilen<=0) {
1390 ALOGE("[%s] received NULL pointer or zero input length",__func__);
1391 return -1;
1392 }
1393
1394 num = ilen/3;
1395 rem = ilen%3;
1396 j=0;k=0;
1397 for(i=0; i<num; i++) {
1398 //prepare index
1399 res = inp[j++]<<16;
1400 res = res | inp[j++]<<8;
1401 res = res | inp[j++];
1402 //get output map from index
1403 outp[k++] = (char) bTable[(res>>18)&0x3F];
1404 outp[k++] = (char) bTable[(res>>12)&0x3F];
1405 outp[k++] = (char) bTable[(res>>6)&0x3F];
1406 outp[k++] = (char) bTable[res&0x3F];
1407 }
1408
1409 switch(rem) {
1410 case 1:
1411 res = inp[j++]<<16;
1412 outp[k++] = (char) bTable[res>>18];
1413 outp[k++] = (char) bTable[(res>>12)&0x3F];
1414 //outp[k++] = '=';
1415 //outp[k++] = '=';
1416 break;
1417 case 2:
1418 res = inp[j++]<<16;
1419 res = res | inp[j++]<<8;
1420 outp[k++] = (char) bTable[res>>18];
1421 outp[k++] = (char) bTable[(res>>12)&0x3F];
1422 outp[k++] = (char) bTable[(res>>6)&0x3F];
1423 //outp[k++] = '=';
1424 break;
1425 default:
1426 break;
1427 }
Ben Rombergera04fabc2014-11-14 12:16:03 -08001428 outp[k] = '\0';
1429 return k;
1430}
Ashish Jain81eb2a82015-05-13 10:52:34 +05301431
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301432
1433int audio_extn_utils_get_codec_version(const char *snd_card_name,
1434 int card_num,
1435 char *codec_version)
1436{
1437 char procfs_path[50];
1438 FILE *fp;
1439
1440 if (strstr(snd_card_name, "tasha")) {
1441 snprintf(procfs_path, sizeof(procfs_path),
1442 "/proc/asound/card%d/codecs/tasha/version", card_num);
1443 if ((fp = fopen(procfs_path, "r")) != NULL) {
1444 fgets(codec_version, CODEC_VERSION_MAX_LENGTH, fp);
1445 fclose(fp);
1446 } else {
1447 ALOGE("%s: ERROR. cannot open %s", __func__, procfs_path);
1448 return -ENOENT;
1449 }
1450 ALOGD("%s: codec version %s", __func__, codec_version);
1451 }
1452
1453 return 0;
1454}
1455
1456
Ashish Jain81eb2a82015-05-13 10:52:34 +05301457#ifdef AUDIO_EXTERNAL_HDMI_ENABLED
1458
1459void get_default_compressed_channel_status(
1460 unsigned char *channel_status)
1461{
Ashish Jain81eb2a82015-05-13 10:52:34 +05301462 memset(channel_status,0,24);
1463
1464 /* block start bit in preamble bit 3 */
1465 channel_status[0] |= PROFESSIONAL;
1466 //compre out
1467 channel_status[0] |= NON_LPCM;
1468 // sample rate; fixed 48K for default/transcode
1469 channel_status[3] |= SR_48000;
1470}
1471
1472#ifdef HDMI_PASSTHROUGH_ENABLED
1473int32_t get_compressed_channel_status(void *audio_stream_data,
1474 uint32_t audio_frame_size,
1475 unsigned char *channel_status,
1476 enum audio_parser_code_type codec_type)
1477 // codec_type - AUDIO_PARSER_CODEC_AC3
1478 // - AUDIO_PARSER_CODEC_DTS
1479{
1480 unsigned char *stream;
1481 int ret = 0;
1482 stream = (unsigned char *)audio_stream_data;
1483
1484 if (audio_stream_data == NULL || audio_frame_size == 0) {
1485 ALOGW("no buffer to get channel status, return default for compress");
1486 get_default_compressed_channel_status(channel_status);
1487 return ret;
1488 }
1489
1490 memset(channel_status,0,24);
1491 if(init_audio_parser(stream, audio_frame_size, codec_type) == -1)
1492 {
1493 ALOGE("init audio parser failed");
1494 return -1;
1495 }
1496 ret = get_channel_status(channel_status, codec_type);
1497 return ret;
1498
1499}
1500
1501#endif
1502
1503void get_lpcm_channel_status(uint32_t sampleRate,
1504 unsigned char *channel_status)
1505{
1506 int32_t status = 0;
Ashish Jain81eb2a82015-05-13 10:52:34 +05301507 memset(channel_status,0,24);
1508 /* block start bit in preamble bit 3 */
1509 channel_status[0] |= PROFESSIONAL;
1510 //LPCM OUT
1511 channel_status[0] &= ~NON_LPCM;
1512
1513 switch (sampleRate) {
1514 case 8000:
1515 case 11025:
1516 case 12000:
1517 case 16000:
1518 case 22050:
1519 channel_status[3] |= SR_NOTID;
Preetam Singh Ranawat61716b12015-12-14 11:55:24 +05301520 break;
Ashish Jain81eb2a82015-05-13 10:52:34 +05301521 case 24000:
1522 channel_status[3] |= SR_24000;
1523 break;
1524 case 32000:
1525 channel_status[3] |= SR_32000;
1526 break;
1527 case 44100:
1528 channel_status[3] |= SR_44100;
1529 break;
1530 case 48000:
1531 channel_status[3] |= SR_48000;
1532 break;
1533 case 88200:
1534 channel_status[3] |= SR_88200;
1535 break;
1536 case 96000:
1537 channel_status[3] |= SR_96000;
1538 break;
1539 case 176400:
1540 channel_status[3] |= SR_176400;
1541 break;
1542 case 192000:
1543 channel_status[3] |= SR_192000;
1544 break;
1545 default:
1546 ALOGV("Invalid sample_rate %u\n", sampleRate);
1547 status = -1;
1548 break;
1549 }
1550}
1551
1552void audio_utils_set_hdmi_channel_status(struct stream_out *out, char * buffer, size_t bytes)
1553{
1554 unsigned char channel_status[24]={0};
1555 struct snd_aes_iec958 iec958;
1556 const char *mixer_ctl_name = "IEC958 Playback PCM Stream";
1557 struct mixer_ctl *ctl;
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +05301558 ALOGV("%s: buffer %s bytes %zd", __func__, buffer, bytes);
Ashish Jain81eb2a82015-05-13 10:52:34 +05301559#ifdef HDMI_PASSTHROUGH_ENABLED
1560 if (audio_extn_is_dolby_format(out->format) &&
1561 /*TODO:Extend code to support DTS passthrough*/
1562 /*set compressed channel status bits*/
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05301563 audio_extn_passthru_is_passthrough_stream(out)){
Ashish Jain81eb2a82015-05-13 10:52:34 +05301564 get_compressed_channel_status(buffer, bytes, channel_status, AUDIO_PARSER_CODEC_AC3);
1565 } else
1566#endif
1567 {
1568 /*set channel status bit for LPCM*/
1569 get_lpcm_channel_status(out->sample_rate, channel_status);
1570 }
1571
1572 memcpy(iec958.status, channel_status,sizeof(iec958.status));
1573 ctl = mixer_get_ctl_by_name(out->dev->mixer, mixer_ctl_name);
1574 if (!ctl) {
1575 ALOGE("%s: Could not get ctl for mixer cmd - %s",
1576 __func__, mixer_ctl_name);
1577 return;
1578 }
1579 if (mixer_ctl_set_array(ctl, &iec958, sizeof(iec958)) < 0) {
1580 ALOGE("%s: Could not set channel status for ext HDMI ",
1581 __func__);
1582 return;
1583 }
1584
1585}
1586#endif
Manish Dewangan3ccdea52017-02-13 19:31:54 +05301587
1588int audio_extn_utils_get_avt_device_drift(
1589 struct audio_usecase *usecase,
1590 struct audio_avt_device_drift_param *drift_param)
1591{
1592 int ret = 0, count = 0;
1593 char avt_device_drift_mixer_ctl_name[MIXER_PATH_MAX_LENGTH] = {0};
1594 struct mixer_ctl *ctl = NULL;
1595 struct audio_avt_device_drift_stats drift_stats;
1596 struct audio_device *adev = NULL;
1597
1598 if (usecase != NULL && usecase->type == PCM_PLAYBACK) {
1599 adev = usecase->stream.out->dev;
1600 switch(usecase->out_snd_device) {
1601 case SND_DEVICE_OUT_HDMI:
1602 strlcpy(avt_device_drift_mixer_ctl_name,
1603 "HDMI RX Drift",
1604 MIXER_PATH_MAX_LENGTH);
1605 break;
1606 case SND_DEVICE_OUT_DISPLAY_PORT:
1607 strlcpy(avt_device_drift_mixer_ctl_name,
1608 "DISPLAY Port RX Drift",
1609 MIXER_PATH_MAX_LENGTH);
1610 break;
1611 default :
1612 ALOGE("%s: Unsupported device %d",__func__,
1613 usecase->stream.out->devices);
1614 ret = -EINVAL;
1615 }
1616 } else {
1617 ALOGE("%s: Invalid usecase %d ",__func__, usecase->type);
1618 ret = -EINVAL;
1619 }
1620
1621 if(ret)
1622 goto done;
1623
1624 ctl = mixer_get_ctl_by_name(adev->mixer, avt_device_drift_mixer_ctl_name);
1625 if (!ctl) {
1626 ALOGE("%s: Could not get ctl for mixer cmd - %s",
1627 __func__, avt_device_drift_mixer_ctl_name);
1628
1629 ret = -EINVAL;
1630 goto done;
1631 }
1632
1633 ALOGV("%s: Getting AV Timer vs Device Drift mixer ctrl name %s", __func__,
1634 avt_device_drift_mixer_ctl_name);
1635
1636 mixer_ctl_update(ctl);
1637 count = mixer_ctl_get_num_values(ctl);
1638 if (count != sizeof(struct audio_avt_device_drift_stats)) {
1639 ALOGE("%s: mixer_ctl_get_num_values() invalid drift_stats data size",
1640 __func__);
1641
1642 ret = -EINVAL;
1643 goto done;
1644 }
1645
1646 ret = mixer_ctl_get_array(ctl, (void *)&drift_stats, count);
1647 if (ret != 0) {
1648 ALOGE("%s: mixer_ctl_get_array() failed to get drift_stats Params",
1649 __func__);
1650
1651 ret = -EINVAL;
1652 goto done;
1653 }
1654 memcpy(drift_param, &drift_stats.drift_param,
1655 sizeof(struct audio_avt_device_drift_param));
1656done:
1657 return ret;
1658}
Manish Dewangan07de2142017-02-27 19:27:20 +05301659
1660#ifdef SNDRV_COMPRESS_PATH_DELAY
1661int audio_extn_utils_compress_get_dsp_latency(struct stream_out *out)
1662{
1663 int ret = -EINVAL;
1664 struct snd_compr_metadata metadata;
1665 int delay_ms = COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
1666
1667 if (property_get_bool("audio.playback.dsp.pathdelay", false)) {
1668 ALOGD("%s:: Quering DSP delay %d",__func__, __LINE__);
1669 if (!(is_offload_usecase(out->usecase))) {
1670 ALOGE("%s:: not supported for non offload session", __func__);
1671 goto exit;
1672 }
1673
1674 if (!out->compr) {
1675 ALOGD("%s:: Invalid compress handle,returning default dsp latency",
1676 __func__);
1677 goto exit;
1678 }
1679
1680 metadata.key = SNDRV_COMPRESS_PATH_DELAY;
1681 ret = compress_get_metadata(out->compr, &metadata);
1682 if(ret) {
1683 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
1684 goto exit;
1685 }
1686 delay_ms = metadata.value[0] / 1000; /*convert to ms*/
1687 } else {
1688 ALOGD("%s:: Using Fix DSP delay",__func__);
1689 }
1690
1691exit:
1692 ALOGD("%s:: delay in ms is %d",__func__, delay_ms);
1693 return delay_ms;
1694}
1695#else
1696int audio_extn_utils_compress_get_dsp_latency(struct stream_out *out __unused)
1697{
1698 return COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
1699}
1700#endif
Manish Dewangan69426c82017-01-30 17:35:36 +05301701
1702#ifdef SNDRV_COMPRESS_RENDER_MODE
1703int audio_extn_utils_compress_set_render_mode(struct stream_out *out)
1704{
1705 struct snd_compr_metadata metadata;
1706 int ret = -EINVAL;
1707
1708 if (!(is_offload_usecase(out->usecase))) {
1709 ALOGE("%s:: not supported for non offload session", __func__);
1710 goto exit;
1711 }
1712
1713 if (!out->compr) {
1714 ALOGD("%s:: Invalid compress handle",
1715 __func__);
1716 goto exit;
1717 }
1718
1719 ALOGD("%s:: render mode %d", __func__, out->render_mode);
1720
1721 metadata.key = SNDRV_COMPRESS_RENDER_MODE;
1722 if (out->render_mode == RENDER_MODE_AUDIO_MASTER) {
1723 metadata.value[0] = SNDRV_COMPRESS_RENDER_MODE_AUDIO_MASTER;
1724 } else if (out->render_mode == RENDER_MODE_AUDIO_STC_MASTER) {
1725 metadata.value[0] = SNDRV_COMPRESS_RENDER_MODE_STC_MASTER;
1726 } else {
1727 ret = 0;
1728 goto exit;
1729 }
1730 ret = compress_set_metadata(out->compr, &metadata);
1731 if(ret) {
1732 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
1733 }
1734exit:
1735 return ret;
1736}
1737#else
1738int audio_extn_utils_compress_set_render_mode(struct stream_out *out __unused)
1739{
1740 ALOGD("%s:: configuring render mode not supported", __func__);
1741 return 0;
1742}
1743#endif