blob: 669bdb47a1936545d84b21a5448b0fc7052ea8c6 [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
Manish Dewangan27346042017-03-01 12:56:12 +053023#include <inttypes.h>
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070024#include <errno.h>
25#include <cutils/properties.h>
26#include <cutils/config_utils.h>
27#include <stdlib.h>
28#include <dlfcn.h>
29#include <cutils/str_parms.h>
30#include <cutils/log.h>
31#include <cutils/misc.h>
32
Manish Dewangan07de2142017-02-27 19:27:20 +053033
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070034#include "audio_hw.h"
35#include "platform.h"
36#include "platform_api.h"
37#include "audio_extn.h"
Narsinga Rao Chella212e2542014-11-17 19:57:04 -080038#include "voice.h"
Manish Dewangan07de2142017-02-27 19:27:20 +053039#include <sound/compress_params.h>
40#include <sound/compress_offload.h>
41#include <tinycompress/tinycompress.h>
Ashish Jain81eb2a82015-05-13 10:52:34 +053042#ifdef AUDIO_EXTERNAL_HDMI_ENABLED
43#ifdef HDMI_PASSTHROUGH_ENABLED
44#include "audio_parsers.h"
45#endif
46#endif
47
Yamit Mehtaa0d653a2016-11-25 20:33:25 +053048#ifdef LINUX_ENABLED
49#define AUDIO_OUTPUT_POLICY_VENDOR_CONFIG_FILE "/etc/audio_output_policy.conf"
Dhananjay Kumard6d32152016-10-13 16:11:03 +053050#define AUDIO_IO_POLICY_VENDOR_CONFIG_FILE "/etc/audio_io_policy.conf"
Yamit Mehtaa0d653a2016-11-25 20:33:25 +053051#else
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070052#define AUDIO_OUTPUT_POLICY_VENDOR_CONFIG_FILE "/vendor/etc/audio_output_policy.conf"
Dhananjay Kumard6d32152016-10-13 16:11:03 +053053#define AUDIO_IO_POLICY_VENDOR_CONFIG_FILE "/vendor/etc/audio_io_policy.conf"
Yamit Mehtaa0d653a2016-11-25 20:33:25 +053054#endif
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070055
56#define OUTPUTS_TAG "outputs"
Dhananjay Kumard6d32152016-10-13 16:11:03 +053057#define INPUTS_TAG "inputs"
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070058
59#define DYNAMIC_VALUE_TAG "dynamic"
60#define FLAGS_TAG "flags"
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +053061#define PROFILES_TAG "profile"
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070062#define FORMATS_TAG "formats"
63#define SAMPLING_RATES_TAG "sampling_rates"
64#define BIT_WIDTH_TAG "bit_width"
65#define APP_TYPE_TAG "app_type"
66
67#define STRING_TO_ENUM(string) { #string, string }
68#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
69
Ben Rombergera04fabc2014-11-14 12:16:03 -080070#define BASE_TABLE_SIZE 64
71#define MAX_BASEINDEX_LEN 256
72
Ben Romberger1aaaf862017-04-06 17:49:46 -070073#ifndef SND_AUDIOCODEC_TRUEHD
74#define SND_AUDIOCODEC_TRUEHD 0x00000023
75#endif
76
Ashish Jain81eb2a82015-05-13 10:52:34 +053077#ifdef AUDIO_EXTERNAL_HDMI_ENABLED
78#define PROFESSIONAL (1<<0) /* 0 = consumer, 1 = professional */
79#define NON_LPCM (1<<1) /* 0 = audio, 1 = non-audio */
80#define SR_44100 (0<<0) /* 44.1kHz */
81#define SR_NOTID (1<<0) /* non indicated */
82#define SR_48000 (2<<0) /* 48kHz */
83#define SR_32000 (3<<0) /* 32kHz */
84#define SR_22050 (4<<0) /* 22.05kHz */
85#define SR_24000 (6<<0) /* 24kHz */
86#define SR_88200 (8<<0) /* 88.2kHz */
87#define SR_96000 (10<<0) /* 96kHz */
88#define SR_176400 (12<<0) /* 176.4kHz */
89#define SR_192000 (14<<0) /* 192kHz */
90
91#endif
Manish Dewangan07de2142017-02-27 19:27:20 +053092
93/* ToDo: Check and update a proper value in msec */
94#define COMPRESS_OFFLOAD_PLAYBACK_LATENCY 50
95
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070096struct string_to_enum {
97 const char *name;
98 uint32_t value;
99};
100
101const struct string_to_enum s_flag_name_to_enum_table[] = {
102 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_DIRECT),
103 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_PRIMARY),
104 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_FAST),
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800105 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_RAW),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700106 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_DEEP_BUFFER),
107 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD),
108 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_NON_BLOCKING),
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700109 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_HW_AV_SYNC),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700110#ifdef INCALL_MUSIC_ENABLED
111 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_INCALL_MUSIC),
112#endif
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700113 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH),
Naresh Tanniruee3499a2017-01-05 14:05:35 +0530114 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_TIMESTAMP),
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530115 STRING_TO_ENUM(AUDIO_INPUT_FLAG_NONE),
116 STRING_TO_ENUM(AUDIO_INPUT_FLAG_FAST),
117 STRING_TO_ENUM(AUDIO_INPUT_FLAG_HW_HOTWORD),
118 STRING_TO_ENUM(AUDIO_INPUT_FLAG_RAW),
119 STRING_TO_ENUM(AUDIO_INPUT_FLAG_SYNC),
Dhananjay Kumaree4d2002016-10-25 18:02:58 +0530120 STRING_TO_ENUM(AUDIO_INPUT_FLAG_TIMESTAMP),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700121};
122
123const struct string_to_enum s_format_name_to_enum_table[] = {
Ashish Jain83a6cc22016-06-28 14:34:17 +0530124 STRING_TO_ENUM(AUDIO_FORMAT_PCM_8_BIT),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700125 STRING_TO_ENUM(AUDIO_FORMAT_PCM_16_BIT),
Ashish Jain5106d362016-05-11 19:23:33 +0530126 STRING_TO_ENUM(AUDIO_FORMAT_PCM_24_BIT_PACKED),
127 STRING_TO_ENUM(AUDIO_FORMAT_PCM_8_24_BIT),
Ashish Jainf1eaa582016-05-23 20:54:24 +0530128 STRING_TO_ENUM(AUDIO_FORMAT_PCM_32_BIT),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700129 STRING_TO_ENUM(AUDIO_FORMAT_MP3),
130 STRING_TO_ENUM(AUDIO_FORMAT_AAC),
131 STRING_TO_ENUM(AUDIO_FORMAT_VORBIS),
Mingming Yinae3530f2014-07-03 16:50:18 -0700132 STRING_TO_ENUM(AUDIO_FORMAT_AMR_NB),
133 STRING_TO_ENUM(AUDIO_FORMAT_AMR_WB),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700134 STRING_TO_ENUM(AUDIO_FORMAT_AC3),
Mingming Yinae3530f2014-07-03 16:50:18 -0700135 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700136 STRING_TO_ENUM(AUDIO_FORMAT_DTS),
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530137 STRING_TO_ENUM(AUDIO_FORMAT_DTS_HD),
Ben Romberger1aaaf862017-04-06 17:49:46 -0700138 STRING_TO_ENUM(AUDIO_FORMAT_DOLBY_TRUEHD),
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530139#ifdef AUDIO_EXTN_FORMATS_ENABLED
140 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3_JOC),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700141 STRING_TO_ENUM(AUDIO_FORMAT_WMA),
142 STRING_TO_ENUM(AUDIO_FORMAT_WMA_PRO),
143 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADIF),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700144 STRING_TO_ENUM(AUDIO_FORMAT_AMR_WB_PLUS),
145 STRING_TO_ENUM(AUDIO_FORMAT_EVRC),
146 STRING_TO_ENUM(AUDIO_FORMAT_EVRCB),
147 STRING_TO_ENUM(AUDIO_FORMAT_EVRCWB),
148 STRING_TO_ENUM(AUDIO_FORMAT_QCELP),
149 STRING_TO_ENUM(AUDIO_FORMAT_MP2),
150 STRING_TO_ENUM(AUDIO_FORMAT_EVRCNW),
Amit Shekhar6f461b12014-08-01 14:52:58 -0700151 STRING_TO_ENUM(AUDIO_FORMAT_FLAC),
Satya Krishna Pindiproli70471602015-04-24 19:12:43 +0530152 STRING_TO_ENUM(AUDIO_FORMAT_ALAC),
153 STRING_TO_ENUM(AUDIO_FORMAT_APE),
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700154 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3_JOC),
Alexy Josephcd8eaed2014-12-11 12:46:53 -0800155 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LC),
156 STRING_TO_ENUM(AUDIO_FORMAT_AAC_HE_V1),
157 STRING_TO_ENUM(AUDIO_FORMAT_AAC_HE_V2),
Manish Dewangana6fc5442015-08-24 20:30:31 +0530158 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS),
Ashish Jaine513a872015-11-19 17:00:56 +0530159 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_LC),
160 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_HE_V1),
161 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_HE_V2),
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +0530162 STRING_TO_ENUM(AUDIO_FORMAT_DSD),
Arun Kumar Dasari3b174182016-12-27 13:01:14 +0530163 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM),
164 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM_LC),
165 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM_HE_V1),
166 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM_HE_V2),
Dhanalakshmi Siddani18737932016-11-29 17:33:17 +0530167 STRING_TO_ENUM(AUDIO_FORMAT_APTX),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700168#endif
169};
170
Manish Dewangan3ccdea52017-02-13 19:31:54 +0530171/* payload structure avt_device drift query */
172struct audio_avt_device_drift_stats {
173 uint32_t minor_version;
174 /* Indicates the device interface direction as either
175 * source (Tx) or sink (Rx).
176 */
177 uint16_t device_direction;
178 /*params exposed to client */
179 struct audio_avt_device_drift_param drift_param;
180};
181
Ben Rombergera04fabc2014-11-14 12:16:03 -0800182static char bTable[BASE_TABLE_SIZE] = {
183 'A','B','C','D','E','F','G','H','I','J','K','L',
184 'M','N','O','P','Q','R','S','T','U','V','W','X',
185 'Y','Z','a','b','c','d','e','f','g','h','i','j',
186 'k','l','m','n','o','p','q','r','s','t','u','v',
187 'w','x','y','z','0','1','2','3','4','5','6','7',
188 '8','9','+','/'
189};
190
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700191static uint32_t string_to_enum(const struct string_to_enum *table, size_t size,
192 const char *name)
193{
194 size_t i;
195 for (i = 0; i < size; i++) {
196 if (strcmp(table[i].name, name) == 0) {
197 ALOGV("%s found %s", __func__, table[i].name);
198 return table[i].value;
199 }
200 }
201 return 0;
202}
203
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530204static audio_io_flags_t parse_flag_names(char *name)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700205{
206 uint32_t flag = 0;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530207 audio_io_flags_t io_flags;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800208 char *last_r;
209 char *flag_name = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700210 while (flag_name != NULL) {
211 if (strlen(flag_name) != 0) {
212 flag |= string_to_enum(s_flag_name_to_enum_table,
213 ARRAY_SIZE(s_flag_name_to_enum_table),
214 flag_name);
215 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800216 flag_name = strtok_r(NULL, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700217 }
218
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800219 ALOGV("parse_flag_names: flag - %x", flag);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530220 io_flags.in_flags = (audio_input_flags_t)flag;
221 io_flags.out_flags = (audio_output_flags_t)flag;
222 return io_flags;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700223}
224
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530225static void parse_format_names(char *name, struct streams_io_cfg *s_info)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700226{
227 struct stream_format *sf_info = NULL;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800228 char *last_r;
229 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700230
231 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG) == 0)
232 return;
233
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530234 list_init(&s_info->format_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700235 while (str != NULL) {
236 audio_format_t format = (audio_format_t)string_to_enum(s_format_name_to_enum_table,
237 ARRAY_SIZE(s_format_name_to_enum_table), str);
238 ALOGV("%s: format - %d", __func__, format);
239 if (format != 0) {
240 sf_info = (struct stream_format *)calloc(1, sizeof(struct stream_format));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700241 if (sf_info == NULL)
242 break; /* return whatever was parsed */
243
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700244 sf_info->format = format;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530245 list_add_tail(&s_info->format_list, &sf_info->list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700246 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800247 str = strtok_r(NULL, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700248 }
249}
250
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530251static void parse_sample_rate_names(char *name, struct streams_io_cfg *s_info)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700252{
Amit Shekhar6f461b12014-08-01 14:52:58 -0700253 struct stream_sample_rate *ss_info = NULL;
254 uint32_t sample_rate = 48000;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800255 char *last_r;
256 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700257
Amit Shekhar6f461b12014-08-01 14:52:58 -0700258 if (str != NULL && 0 == strcmp(str, DYNAMIC_VALUE_TAG))
259 return;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700260
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530261 list_init(&s_info->sample_rate_list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700262 while (str != NULL) {
263 sample_rate = (uint32_t)strtol(str, (char **)NULL, 10);
264 ALOGV("%s: sample_rate - %d", __func__, sample_rate);
265 if (0 != sample_rate) {
266 ss_info = (struct stream_sample_rate *)calloc(1, sizeof(struct stream_sample_rate));
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800267 if (!ss_info) {
268 ALOGE("%s: memory allocation failure", __func__);
269 return;
270 }
Amit Shekhar6f461b12014-08-01 14:52:58 -0700271 ss_info->sample_rate = sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530272 list_add_tail(&s_info->sample_rate_list, &ss_info->list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700273 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800274 str = strtok_r(NULL, "|", &last_r);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700275 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700276}
277
278static int parse_bit_width_names(char *name)
279{
280 int bit_width = 16;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800281 char *last_r;
282 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700283
284 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG))
285 bit_width = (int)strtol(str, (char **)NULL, 10);
286
287 ALOGV("%s: bit_width - %d", __func__, bit_width);
288 return bit_width;
289}
290
291static int parse_app_type_names(void *platform, char *name)
292{
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700293 int app_type = platform_get_default_app_type(platform);
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800294 char *last_r;
295 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700296
297 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG))
298 app_type = (int)strtol(str, (char **)NULL, 10);
299
300 ALOGV("%s: app_type - %d", __func__, app_type);
301 return app_type;
302}
303
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530304static void update_streams_cfg_list(cnode *root, void *platform,
305 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700306{
307 cnode *node = root->first_child;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530308 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700309
310 ALOGV("%s", __func__);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530311 s_info = (struct streams_io_cfg *)calloc(1, sizeof(struct streams_io_cfg));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700312
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530313 if (!s_info) {
314 ALOGE("failed to allocate mem for s_info list element");
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700315 return;
316 }
317
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700318 while (node) {
319 if (strcmp(node->name, FLAGS_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530320 s_info->flags = parse_flag_names((char *)node->value);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530321 } else if (strcmp(node->name, PROFILES_TAG) == 0) {
322 strlcpy(s_info->profile, (char *)node->value, sizeof(s_info->profile));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700323 } else if (strcmp(node->name, FORMATS_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530324 parse_format_names((char *)node->value, s_info);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700325 } else if (strcmp(node->name, SAMPLING_RATES_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530326 s_info->app_type_cfg.sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
327 parse_sample_rate_names((char *)node->value, s_info);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700328 } else if (strcmp(node->name, BIT_WIDTH_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530329 s_info->app_type_cfg.bit_width = parse_bit_width_names((char *)node->value);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700330 } else if (strcmp(node->name, APP_TYPE_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530331 s_info->app_type_cfg.app_type = parse_app_type_names(platform, (char *)node->value);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700332 }
333 node = node->next;
334 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530335 list_add_tail(streams_cfg_list, &s_info->list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700336}
337
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530338static void load_cfg_list(cnode *root, void *platform,
339 struct listnode *streams_output_cfg_list,
340 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700341{
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530342 cnode *node = NULL;
343
344 node = config_find(root, OUTPUTS_TAG);
345 if (node != NULL) {
346 node = node->first_child;
347 while (node) {
348 ALOGV("%s: loading output %s", __func__, node->name);
349 update_streams_cfg_list(node, platform, streams_output_cfg_list);
350 node = node->next;
351 }
352 } else {
353 ALOGI("%s: could not load output, node is NULL", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700354 }
355
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530356 node = config_find(root, INPUTS_TAG);
357 if (node != NULL) {
358 node = node->first_child;
359 while (node) {
360 ALOGV("%s: loading input %s", __func__, node->name);
361 update_streams_cfg_list(node, platform, streams_input_cfg_list);
362 node = node->next;
363 }
364 } else {
365 ALOGI("%s: could not load input, node is NULL", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700366 }
367}
368
369static void send_app_type_cfg(void *platform, struct mixer *mixer,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530370 struct listnode *streams_output_cfg_list,
371 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700372{
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530373 size_t app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT] = {0};
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700374 int length = 0, i, num_app_types = 0;
375 struct listnode *node;
376 bool update;
377 struct mixer_ctl *ctl = NULL;
378 const char *mixer_ctl_name = "App Type Config";
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530379 struct streams_io_cfg *s_info = NULL;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700380
381 if (!mixer) {
382 ALOGE("%s: mixer is null",__func__);
383 return;
384 }
385 ctl = mixer_get_ctl_by_name(mixer, mixer_ctl_name);
386 if (!ctl) {
387 ALOGE("%s: Could not get ctl for mixer cmd - %s",__func__, mixer_ctl_name);
388 return;
389 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530390 app_type_cfg[length++] = num_app_types;
391
392 if (list_empty(streams_output_cfg_list)) {
393 app_type_cfg[length++] = platform_get_default_app_type_v2(platform, PCM_PLAYBACK);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700394 app_type_cfg[length++] = 48000;
395 app_type_cfg[length++] = 16;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530396 num_app_types += 1;
397 }
398 if (list_empty(streams_input_cfg_list)) {
399 app_type_cfg[length++] = platform_get_default_app_type_v2(platform, PCM_CAPTURE);
400 app_type_cfg[length++] = 48000;
401 app_type_cfg[length++] = 16;
402 num_app_types += 1;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700403 }
404
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700405 list_for_each(node, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530406 s_info = node_to_item(node, struct streams_io_cfg, list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700407 update = true;
408 for (i=0; i<length; i=i+3) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530409 if (app_type_cfg[i+1] == 0)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700410 break;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530411 else if (app_type_cfg[i+1] == (size_t)s_info->app_type_cfg.app_type) {
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530412 if (app_type_cfg[i+2] < (size_t)s_info->app_type_cfg.sample_rate)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530413 app_type_cfg[i+2] = s_info->app_type_cfg.sample_rate;
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530414 if (app_type_cfg[i+3] < (size_t)s_info->app_type_cfg.bit_width)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530415 app_type_cfg[i+3] = s_info->app_type_cfg.bit_width;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700416 update = false;
417 break;
418 }
419 }
420 if (update && ((length + 3) <= MAX_LENGTH_MIXER_CONTROL_IN_INT)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530421 num_app_types += 1;
422 app_type_cfg[length++] = s_info->app_type_cfg.app_type;
423 app_type_cfg[length++] = s_info->app_type_cfg.sample_rate;
424 app_type_cfg[length++] = s_info->app_type_cfg.bit_width;
425 }
426 }
427 list_for_each(node, streams_input_cfg_list) {
428 s_info = node_to_item(node, struct streams_io_cfg, list);
429 update = true;
430 for (i=0; i<length; i=i+3) {
431 if (app_type_cfg[i+1] == 0)
432 break;
433 else if (app_type_cfg[i+1] == (size_t)s_info->app_type_cfg.app_type) {
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530434 if (app_type_cfg[i+2] < (size_t)s_info->app_type_cfg.sample_rate)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530435 app_type_cfg[i+2] = s_info->app_type_cfg.sample_rate;
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530436 if (app_type_cfg[i+3] < (size_t)s_info->app_type_cfg.bit_width)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530437 app_type_cfg[i+3] = s_info->app_type_cfg.bit_width;
438 update = false;
439 break;
440 }
441 }
442 if (update && ((length + 3) <= MAX_LENGTH_MIXER_CONTROL_IN_INT)) {
443 num_app_types += 1;
444 app_type_cfg[length++] = s_info->app_type_cfg.app_type;
445 app_type_cfg[length++] = s_info->app_type_cfg.sample_rate;
446 app_type_cfg[length++] = s_info->app_type_cfg.bit_width;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700447 }
448 }
449 ALOGV("%s: num_app_types: %d", __func__, num_app_types);
450 if (num_app_types) {
451 app_type_cfg[0] = num_app_types;
452 mixer_ctl_set_array(ctl, app_type_cfg, length);
453 }
454}
455
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530456void audio_extn_utils_update_streams_cfg_lists(void *platform,
457 struct mixer *mixer,
458 struct listnode *streams_output_cfg_list,
459 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700460{
461 cnode *root;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530462 char *data = NULL;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700463
464 ALOGV("%s", __func__);
465 list_init(streams_output_cfg_list);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530466 list_init(streams_input_cfg_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700467
468 root = config_node("", "");
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700469 if (root == NULL) {
470 ALOGE("cfg_list, NULL config root");
471 return;
472 }
473
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530474 data = (char *)load_file(AUDIO_IO_POLICY_VENDOR_CONFIG_FILE, NULL);
475 if (data == NULL) {
476 ALOGD("%s: failed to open io config file(%s), trying older config file",
477 __func__, AUDIO_IO_POLICY_VENDOR_CONFIG_FILE);
478 data = (char *)load_file(AUDIO_OUTPUT_POLICY_VENDOR_CONFIG_FILE, NULL);
479 if (data == NULL) {
480 send_app_type_cfg(platform, mixer,
481 streams_output_cfg_list,
482 streams_input_cfg_list);
483 ALOGE("%s: could not load io policy config!", __func__);
yidongh6eb4f752017-04-19 18:20:57 +0800484 free(root);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530485 return;
486 }
487 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700488
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530489 config_load(root, data);
490 load_cfg_list(root, platform, streams_output_cfg_list,
491 streams_input_cfg_list);
492
493 send_app_type_cfg(platform, mixer, streams_output_cfg_list,
494 streams_input_cfg_list);
Alexy Josephaee4fdd2016-01-29 13:02:07 -0800495
496 config_free(root);
yidongh6eb4f752017-04-19 18:20:57 +0800497 free(root);
Alexy Josephaee4fdd2016-01-29 13:02:07 -0800498 free(data);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700499}
500
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530501static void audio_extn_utils_dump_streams_cfg_list(
502 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700503{
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700504 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530505 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700506 struct stream_format *sf_info;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700507 struct stream_sample_rate *ss_info;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530508
509 list_for_each(node_i, streams_cfg_list) {
510 s_info = node_to_item(node_i, struct streams_io_cfg, list);
511 ALOGV("%s: flags-%d, sample_rate-%d, bit_width-%d, app_type-%d",
512 __func__, s_info->flags.out_flags, s_info->app_type_cfg.sample_rate,
513 s_info->app_type_cfg.bit_width, s_info->app_type_cfg.app_type);
514 list_for_each(node_j, &s_info->format_list) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700515 sf_info = node_to_item(node_j, struct stream_format, list);
516 ALOGV("format-%x", sf_info->format);
517 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530518 list_for_each(node_j, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700519 ss_info = node_to_item(node_j, struct stream_sample_rate, list);
520 ALOGV("sample rate-%d", ss_info->sample_rate);
521 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700522 }
523}
524
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530525void audio_extn_utils_dump_streams_cfg_lists(
526 struct listnode *streams_output_cfg_list,
527 struct listnode *streams_input_cfg_list)
528{
529 ALOGV("%s", __func__);
530 audio_extn_utils_dump_streams_cfg_list(streams_output_cfg_list);
531 audio_extn_utils_dump_streams_cfg_list(streams_input_cfg_list);
532}
533
534static void audio_extn_utils_release_streams_cfg_list(
535 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700536{
537 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530538 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700539
540 ALOGV("%s", __func__);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530541
542 while (!list_empty(streams_cfg_list)) {
543 node_i = list_head(streams_cfg_list);
544 s_info = node_to_item(node_i, struct streams_io_cfg, list);
545 while (!list_empty(&s_info->format_list)) {
546 node_j = list_head(&s_info->format_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700547 list_remove(node_j);
548 free(node_to_item(node_j, struct stream_format, list));
549 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530550 while (!list_empty(&s_info->sample_rate_list)) {
551 node_j = list_head(&s_info->sample_rate_list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700552 list_remove(node_j);
553 free(node_to_item(node_j, struct stream_sample_rate, list));
554 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700555 list_remove(node_i);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530556 free(node_to_item(node_i, struct streams_io_cfg, list));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700557 }
558}
559
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530560void audio_extn_utils_release_streams_cfg_lists(
561 struct listnode *streams_output_cfg_list,
562 struct listnode *streams_input_cfg_list)
563{
564 ALOGV("%s", __func__);
565 audio_extn_utils_release_streams_cfg_list(streams_output_cfg_list);
566 audio_extn_utils_release_streams_cfg_list(streams_input_cfg_list);
567}
568
569static bool set_app_type_cfg(struct streams_io_cfg *s_info,
570 struct stream_app_type_cfg *app_type_cfg,
571 uint32_t sample_rate, uint32_t bit_width)
Amit Shekhar6f461b12014-08-01 14:52:58 -0700572 {
573 struct listnode *node_i;
574 struct stream_sample_rate *ss_info;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530575 list_for_each(node_i, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700576 ss_info = node_to_item(node_i, struct stream_sample_rate, list);
577 if ((sample_rate <= ss_info->sample_rate) &&
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530578 (bit_width == s_info->app_type_cfg.bit_width)) {
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700579
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530580 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700581 app_type_cfg->sample_rate = ss_info->sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530582 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700583 ALOGV("%s app_type_cfg->app_type %d, app_type_cfg->sample_rate %d, app_type_cfg->bit_width %d",
584 __func__, app_type_cfg->app_type, app_type_cfg->sample_rate, app_type_cfg->bit_width);
585 return true;
586 }
587 }
588 /*
589 * Reiterate through the list assuming dafault sample rate.
590 * Handles scenario where input sample rate is higher
591 * than all sample rates in list for the input bit width.
592 */
593 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
Apoorv Raghuvanshif59bb222015-02-18 12:23:23 -0800594
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530595 list_for_each(node_i, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700596 ss_info = node_to_item(node_i, struct stream_sample_rate, list);
597 if ((sample_rate <= ss_info->sample_rate) &&
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530598 (bit_width == s_info->app_type_cfg.bit_width)) {
599 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700600 app_type_cfg->sample_rate = sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530601 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Apoorv Raghuvanshif59bb222015-02-18 12:23:23 -0800602 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 -0700603 __func__, app_type_cfg->app_type, app_type_cfg->sample_rate, app_type_cfg->bit_width);
604 return true;
605 }
606 }
607 return false;
608}
609
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530610void audio_extn_utils_update_stream_input_app_type_cfg(void *platform,
611 struct listnode *streams_input_cfg_list,
612 audio_devices_t devices __unused,
613 audio_input_flags_t flags,
614 audio_format_t format,
615 uint32_t sample_rate,
616 uint32_t bit_width,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530617 char* profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530618 struct stream_app_type_cfg *app_type_cfg)
619{
620 struct listnode *node_i, *node_j;
621 struct streams_io_cfg *s_info;
622 struct stream_format *sf_info;
623
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530624 ALOGV("%s: flags: 0x%x, format: 0x%x sample_rate %d, profile %s",
625 __func__, flags, format, sample_rate, profile);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530626
627 list_for_each(node_i, streams_input_cfg_list) {
628 s_info = node_to_item(node_i, struct streams_io_cfg, list);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530629 /* Along with flags do profile matching if set at either end.*/
630 if (s_info->flags.in_flags == flags &&
631 ((profile[0] == '\0' && s_info->profile[0] == '\0') ||
632 strncmp(s_info->profile, profile, sizeof(s_info->profile)) == 0)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530633 list_for_each(node_j, &s_info->format_list) {
634 sf_info = node_to_item(node_j, struct stream_format, list);
635 if (sf_info->format == format) {
636 if (set_app_type_cfg(s_info, app_type_cfg, sample_rate, bit_width))
637 return;
638 }
639 }
640 }
641 }
642 ALOGW("%s: App type could not be selected. Falling back to default", __func__);
643 app_type_cfg->app_type = platform_get_default_app_type_v2(platform, PCM_CAPTURE);
644 app_type_cfg->sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
645 app_type_cfg->bit_width = 16;
646}
647
648void audio_extn_utils_update_stream_output_app_type_cfg(void *platform,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700649 struct listnode *streams_output_cfg_list,
Amit Shekhar1d896042014-10-03 13:16:09 -0700650 audio_devices_t devices,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700651 audio_output_flags_t flags,
652 audio_format_t format,
Amit Shekhar6f461b12014-08-01 14:52:58 -0700653 uint32_t sample_rate,
654 uint32_t bit_width,
Manish Dewangan837dc462015-05-27 10:17:41 +0530655 audio_channel_mask_t channel_mask,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530656 char *profile,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700657 struct stream_app_type_cfg *app_type_cfg)
658{
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +0530659 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530660 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700661 struct stream_format *sf_info;
Manish Dewangan837dc462015-05-27 10:17:41 +0530662 char value[PROPERTY_VALUE_MAX] = {0};
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700663
Ashish Jain058165c2016-09-28 23:18:48 +0530664 if ((bit_width >= 24) &&
Amit Shekhar1d896042014-10-03 13:16:09 -0700665 (devices & AUDIO_DEVICE_OUT_SPEAKER)) {
Amit Shekhar5a39c912014-10-14 15:39:30 -0700666 int32_t bw = platform_get_snd_device_bit_width(SND_DEVICE_OUT_SPEAKER);
667 if (-ENOSYS != bw)
668 bit_width = (uint32_t)bw;
Amit Shekhar1d896042014-10-03 13:16:09 -0700669 sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
670 ALOGI("%s Allowing 24-bit playback on speaker ONLY at default sampling rate", __func__);
671 }
672
Manish Dewangan837dc462015-05-27 10:17:41 +0530673 property_get("audio.playback.mch.downsample",value,"");
674 if (!strncmp("true", value, sizeof("true"))) {
675 if ((popcount(channel_mask) > 2) &&
676 (sample_rate > CODEC_BACKEND_DEFAULT_SAMPLE_RATE) &&
677 !(flags & AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH)) {
678 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
679 ALOGD("%s: MCH session defaulting sample rate to %d",
680 __func__, sample_rate);
681 }
682 }
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +0530683
684 /* Set sampling rate to 176.4 for DSD64
685 * and 352.8Khz for DSD128.
686 * Set Bit Width to 16. output will be 16 bit
687 * post DoP in ASM.
688 */
689 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH) &&
690 (format == AUDIO_FORMAT_DSD)) {
691 bit_width = 16;
692 if (sample_rate == INPUT_SAMPLING_RATE_DSD64)
693 sample_rate = OUTPUT_SAMPLING_RATE_DSD64;
694 else if (sample_rate == INPUT_SAMPLING_RATE_DSD128)
695 sample_rate = OUTPUT_SAMPLING_RATE_DSD128;
696 }
697
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +0530698 if(devices & AUDIO_DEVICE_OUT_ALL_A2DP) {
699 //TODO: Handle fractional sampling rate configuration for LL
700 audio_extn_a2dp_get_apptype_params(&sample_rate, &bit_width);
701 ALOGI("%s using %d sampling rate %d bit width for A2DP CoPP",
702 __func__, sample_rate, bit_width);
703 }
704
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800705 ALOGV("%s: flags: %x, format: %x sample_rate %d, profile %s, app_type %d",
706 __func__, flags, format, sample_rate, profile, app_type_cfg->app_type);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700707 list_for_each(node_i, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530708 s_info = node_to_item(node_i, struct streams_io_cfg, list);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530709 /* Along with flags do profile matching if set at either end.*/
710 if (s_info->flags.out_flags == flags &&
711 ((profile[0] == '\0' && s_info->profile[0] == '\0') ||
712 strncmp(s_info->profile, profile, sizeof(s_info->profile)) == 0)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530713 list_for_each(node_j, &s_info->format_list) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700714 sf_info = node_to_item(node_j, struct stream_format, list);
715 if (sf_info->format == format) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530716 if (set_app_type_cfg(s_info, app_type_cfg, sample_rate, bit_width))
Amit Shekhar6f461b12014-08-01 14:52:58 -0700717 return;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700718 }
719 }
720 }
721 }
722 list_for_each(node_i, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530723 s_info = node_to_item(node_i, struct streams_io_cfg, list);
724 if (s_info->flags.out_flags == AUDIO_OUTPUT_FLAG_PRIMARY) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700725 ALOGV("Compatible output profile not found.");
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530726 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
727 app_type_cfg->sample_rate = s_info->app_type_cfg.sample_rate;
728 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700729 ALOGV("%s Default to primary output: App type: %d sample_rate %d",
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530730 __func__, s_info->app_type_cfg.app_type, app_type_cfg->sample_rate);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700731 return;
732 }
733 }
734 ALOGW("%s: App type could not be selected. Falling back to default", __func__);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700735 app_type_cfg->app_type = platform_get_default_app_type(platform);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700736 app_type_cfg->sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700737 app_type_cfg->bit_width = 16;
738}
739
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +0530740static bool audio_is_this_native_usecase(struct audio_usecase *uc)
741{
742 bool native_usecase = false;
743 struct stream_out *out = (struct stream_out*) uc->stream.out;
744
745 if (PCM_PLAYBACK == uc->type && out != NULL &&
746 NATIVE_AUDIO_MODE_INVALID != platform_get_native_support() &&
747 is_offload_usecase(uc->id) &&
748 (out->sample_rate == OUTPUT_SAMPLING_RATE_44100))
749 native_usecase = true;
750
751 return native_usecase;
752}
753
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800754
755static inline bool audio_is_vr_mode_on(struct audio_device *(__attribute__((unused)) adev))
756{
757 return adev->vr_audio_mode_enabled;
758}
759
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530760void audio_extn_utils_update_stream_app_type_cfg_for_usecase(
761 struct audio_device *adev,
762 struct audio_usecase *usecase)
763{
764 ALOGV("%s", __func__);
765
766 switch(usecase->type) {
767 case PCM_PLAYBACK:
768 audio_extn_utils_update_stream_output_app_type_cfg(adev->platform,
769 &adev->streams_output_cfg_list,
770 usecase->stream.out->devices,
771 usecase->stream.out->flags,
772 usecase->stream.out->format,
773 usecase->stream.out->sample_rate,
774 usecase->stream.out->bit_width,
775 usecase->stream.out->channel_mask,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530776 usecase->stream.out->profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530777 &usecase->stream.out->app_type_cfg);
778 ALOGV("%s Selected apptype: %d", __func__, usecase->stream.out->app_type_cfg.app_type);
779 break;
780 case PCM_CAPTURE:
781 audio_extn_utils_update_stream_input_app_type_cfg(adev->platform,
782 &adev->streams_input_cfg_list,
783 usecase->stream.in->device,
784 usecase->stream.in->flags,
785 usecase->stream.in->format,
786 usecase->stream.in->sample_rate,
787 usecase->stream.in->bit_width,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530788 usecase->stream.in->profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530789 &usecase->stream.in->app_type_cfg);
790 ALOGV("%s Selected apptype: %d", __func__, usecase->stream.in->app_type_cfg.app_type);
791 break;
792 default:
793 ALOGE("%s: app type cfg not supported for usecase type (%d)",
794 __func__, usecase->type);
795 }
796}
797
Siena Richard7c2db772016-12-21 11:32:34 -0800798static int send_app_type_cfg_for_device(struct audio_device *adev,
799 struct audio_usecase *usecase,
800 int split_snd_device)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700801{
802 char mixer_ctl_name[MAX_LENGTH_MIXER_CONTROL_IN_INT];
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530803 size_t app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT] = {0};
804 int len = 0, rc;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700805 struct mixer_ctl *ctl;
Siena Richard7c2db772016-12-21 11:32:34 -0800806 int pcm_device_id = 0, acdb_dev_id, app_type;
807 int snd_device = split_snd_device, snd_device_be_idx = -1;
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +0530808 int32_t sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Manish Dewangan837dc462015-05-27 10:17:41 +0530809 char value[PROPERTY_VALUE_MAX] = {0};
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700810
Siena Richard7c2db772016-12-21 11:32:34 -0800811 ALOGV("%s: usecase->out_snd_device %s, usecase->in_snd_device %s, split_snd_device %s",
812 __func__, platform_get_snd_device_name(usecase->out_snd_device),
813 platform_get_snd_device_name(usecase->in_snd_device),
814 platform_get_snd_device_name(split_snd_device));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700815
Srikanth Uyyala9d551402015-08-25 16:03:42 +0530816 if (usecase->type != PCM_PLAYBACK && usecase->type != PCM_CAPTURE) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530817 ALOGE("%s: not a playback/capture path, no need to cfg app type", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700818 rc = 0;
819 goto exit_send_app_type_cfg;
820 }
821 if ((usecase->id != USECASE_AUDIO_PLAYBACK_DEEP_BUFFER) &&
822 (usecase->id != USECASE_AUDIO_PLAYBACK_LOW_LATENCY) &&
823 (usecase->id != USECASE_AUDIO_PLAYBACK_MULTI_CH) &&
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800824 (usecase->id != USECASE_AUDIO_PLAYBACK_ULL) &&
Srikanth Uyyala9d551402015-08-25 16:03:42 +0530825 (!is_offload_usecase(usecase->id)) &&
826 (usecase->type != PCM_CAPTURE)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530827 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 -0700828 rc = 0;
829 goto exit_send_app_type_cfg;
830 }
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800831
832 //if VR is active then only send the mixer control
833 if (usecase->id == USECASE_AUDIO_PLAYBACK_ULL && !audio_is_vr_mode_on(adev)) {
834 ALOGI("ULL doesnt need sending app type cfg, returning");
835 rc = 0;
836 goto exit_send_app_type_cfg;
837 }
838
Srikanth Uyyala9d551402015-08-25 16:03:42 +0530839 if (usecase->type == PCM_PLAYBACK) {
Ben Romberger1fafdde2015-09-09 19:43:15 -0700840 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_PLAYBACK);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530841 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
842 "Audio Stream %d App Type Cfg", pcm_device_id);
Ben Romberger1fafdde2015-09-09 19:43:15 -0700843 } else if (usecase->type == PCM_CAPTURE) {
Ben Romberger1fafdde2015-09-09 19:43:15 -0700844 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_CAPTURE);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530845 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
846 "Audio Stream Capture %d App Type Cfg", pcm_device_id);
Srikanth Uyyala9d551402015-08-25 16:03:42 +0530847 }
Siena Richard7c2db772016-12-21 11:32:34 -0800848
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700849 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
850 if (!ctl) {
851 ALOGE("%s: Could not get ctl for mixer cmd - %s", __func__,
852 mixer_ctl_name);
853 rc = -EINVAL;
854 goto exit_send_app_type_cfg;
855 }
Aditya Bavanari701a6992017-03-30 19:17:16 +0530856 snd_device = platform_get_spkr_prot_snd_device(snd_device);
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +0530857
Rohit Kumar1181b292017-01-31 18:07:17 +0530858 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
859 if (acdb_dev_id <= 0) {
860 ALOGE("%s: Couldn't get the acdb dev id", __func__);
861 rc = -EINVAL;
862 goto exit_send_app_type_cfg;
863 }
864
Siena Richard7c2db772016-12-21 11:32:34 -0800865 snd_device_be_idx = platform_get_snd_device_backend_index(snd_device);
866 if (snd_device_be_idx < 0) {
867 ALOGE("%s: Couldn't get the backend index for snd device %s ret=%d",
868 __func__, platform_get_snd_device_name(snd_device),
869 snd_device_be_idx);
870 }
871
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530872 if ((usecase->type == PCM_PLAYBACK) && (usecase->stream.out != NULL)) {
Manish Dewangan837dc462015-05-27 10:17:41 +0530873
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530874 property_get("audio.playback.mch.downsample",value,"");
875 if (!strncmp("true", value, sizeof("true"))) {
876 if ((popcount(usecase->stream.out->channel_mask) > 2) &&
877 (usecase->stream.out->app_type_cfg.sample_rate > CODEC_BACKEND_DEFAULT_SAMPLE_RATE) &&
878 !(usecase->stream.out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH))
879 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
880 }
Ashish Jainc02430d2016-01-04 10:42:43 +0530881
Ashish Jain4826f6c2017-02-06 13:33:20 +0530882 if (usecase->stream.out->devices & AUDIO_DEVICE_OUT_SPEAKER) {
Mingming Yin21854652016-04-13 11:54:02 -0700883 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Ashish Jaina052e572016-11-07 16:41:07 +0530884 } else if ((snd_device == SND_DEVICE_OUT_HDMI ||
885 snd_device == SND_DEVICE_OUT_USB_HEADSET ||
886 snd_device == SND_DEVICE_OUT_DISPLAY_PORT) &&
887 (usecase->stream.out->sample_rate >= OUTPUT_SAMPLING_RATE_44100)) {
888 /*
889 * To best utlize DSP, check if the stream sample rate is supported/multiple of
890 * configured device sample rate, if not update the COPP rate to be equal to the
891 * device sample rate, else open COPP at stream sample rate
892 */
893 platform_check_and_update_copp_sample_rate(adev->platform, snd_device,
894 usecase->stream.out->sample_rate,
895 &usecase->stream.out->app_type_cfg.sample_rate);
Ashish Jain4826f6c2017-02-06 13:33:20 +0530896 } else if (((snd_device != SND_DEVICE_OUT_HEADPHONES_44_1 &&
897 !audio_is_this_native_usecase(usecase)) &&
Mingming Yin21854652016-04-13 11:54:02 -0700898 usecase->stream.out->sample_rate == OUTPUT_SAMPLING_RATE_44100) ||
899 (usecase->stream.out->sample_rate < OUTPUT_SAMPLING_RATE_44100)) {
Ashish Jain4826f6c2017-02-06 13:33:20 +0530900 /* Reset to default if no native stream is active*/
Mingming Yin21854652016-04-13 11:54:02 -0700901 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
902 }
903 sample_rate = usecase->stream.out->app_type_cfg.sample_rate;
904
Siena Richard7c2db772016-12-21 11:32:34 -0800905 app_type = usecase->stream.out->app_type_cfg.app_type;
906 app_type_cfg[len++] = app_type;
Mingming Yin21854652016-04-13 11:54:02 -0700907 app_type_cfg[len++] = acdb_dev_id;
908 if (((usecase->stream.out->format == AUDIO_FORMAT_E_AC3) ||
Ben Romberger1aaaf862017-04-06 17:49:46 -0700909 (usecase->stream.out->format == AUDIO_FORMAT_E_AC3_JOC) ||
910 (usecase->stream.out->format == AUDIO_FORMAT_DOLBY_TRUEHD))
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530911 && audio_extn_passthru_is_passthrough_stream(usecase->stream.out)) {
Mingming Yin21854652016-04-13 11:54:02 -0700912 app_type_cfg[len++] = sample_rate * 4;
913 } else {
914 app_type_cfg[len++] = sample_rate;
915 }
Siena Richard7c2db772016-12-21 11:32:34 -0800916 if (snd_device_be_idx > 0)
917 app_type_cfg[len++] = snd_device_be_idx;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530918
Siena Richard7c2db772016-12-21 11:32:34 -0800919 ALOGI("%s PLAYBACK app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
920 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530921
922 } else if ((usecase->type == PCM_CAPTURE) && (usecase->stream.in != NULL)) {
Siena Richard7c2db772016-12-21 11:32:34 -0800923 app_type = usecase->stream.in->app_type_cfg.app_type;
924 app_type_cfg[len++] = app_type;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530925 app_type_cfg[len++] = acdb_dev_id;
Siena Richard7c2db772016-12-21 11:32:34 -0800926 sample_rate = usecase->stream.in->app_type_cfg.sample_rate;
927 app_type_cfg[len++] = sample_rate;
928 if (snd_device_be_idx > 0)
929 app_type_cfg[len++] = snd_device_be_idx;
930 ALOGI("%s CAPTURE app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
931 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530932 } else {
Siena Richard7c2db772016-12-21 11:32:34 -0800933 app_type = platform_get_default_app_type_v2(adev->platform, usecase->type);
934 app_type_cfg[len++] = app_type;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530935 app_type_cfg[len++] = acdb_dev_id;
936 app_type_cfg[len++] = sample_rate;
Siena Richard7c2db772016-12-21 11:32:34 -0800937 if (snd_device_be_idx > 0)
938 app_type_cfg[len++] = snd_device_be_idx;
939 ALOGI("%s default app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
940 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Ashish Jainc02430d2016-01-04 10:42:43 +0530941 }
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +0530942
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700943 mixer_ctl_set_array(ctl, app_type_cfg, len);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700944 rc = 0;
945exit_send_app_type_cfg:
946 return rc;
947}
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700948
Siena Richard7c2db772016-12-21 11:32:34 -0800949int audio_extn_utils_send_app_type_cfg(struct audio_device *adev,
950 struct audio_usecase *usecase)
951{
952 int i, num_devices = 0;
953 snd_device_t new_snd_devices[SND_DEVICE_OUT_END] = {0};
954 int rc = 0;
955
956 switch (usecase->type) {
957 case PCM_PLAYBACK:
958 ALOGD("%s: usecase->out_snd_device %s",
959 __func__, platform_get_snd_device_name(usecase->out_snd_device));
960 /* check for out combo device */
961 if (platform_split_snd_device(adev->platform,
962 usecase->out_snd_device,
963 &num_devices, new_snd_devices)) {
964 new_snd_devices[0] = usecase->out_snd_device;
965 num_devices = 1;
966 }
967 break;
968 case PCM_CAPTURE:
969 ALOGD("%s: usecase->in_snd_device %s",
970 __func__, platform_get_snd_device_name(usecase->in_snd_device));
971 /* check for in combo device */
972 if (platform_split_snd_device(adev->platform,
973 usecase->in_snd_device,
974 &num_devices, new_snd_devices)) {
975 new_snd_devices[0] = usecase->in_snd_device;
976 num_devices = 1;
977 }
978 break;
979 default:
980 ALOGI("%s: not a playback/capture path, no need to cfg app type", __func__);
981 rc = 0;
982 break;
983 }
984
985 for (i = 0; i < num_devices; i++) {
986 rc = send_app_type_cfg_for_device(adev, usecase, new_snd_devices[i]);
987 if (rc)
988 break;
989 }
990
991 return rc;
992}
993
Preetam Singh Ranawat9519e9c2015-11-18 16:05:55 +0530994int read_line_from_file(const char *path, char *buf, size_t count)
995{
996 char * fgets_ret;
997 FILE * fd;
998 int rv;
999
1000 fd = fopen(path, "r");
1001 if (fd == NULL)
1002 return -1;
1003
1004 fgets_ret = fgets(buf, (int)count, fd);
1005 if (NULL != fgets_ret) {
1006 rv = (int)strlen(buf);
1007 } else {
1008 rv = ferror(fd);
1009 }
1010 fclose(fd);
1011
1012 return rv;
1013}
1014
Ashish Jainf1eaa582016-05-23 20:54:24 +05301015/*Translates ALSA formats to AOSP PCM formats*/
1016audio_format_t alsa_format_to_hal(uint32_t alsa_format)
1017{
1018 audio_format_t format;
1019
1020 switch(alsa_format) {
1021 case SNDRV_PCM_FORMAT_S16_LE:
1022 format = AUDIO_FORMAT_PCM_16_BIT;
1023 break;
1024 case SNDRV_PCM_FORMAT_S24_3LE:
1025 format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
1026 break;
1027 case SNDRV_PCM_FORMAT_S24_LE:
1028 format = AUDIO_FORMAT_PCM_8_24_BIT;
1029 break;
1030 case SNDRV_PCM_FORMAT_S32_LE:
1031 format = AUDIO_FORMAT_PCM_32_BIT;
1032 break;
1033 default:
1034 ALOGW("Incorrect ALSA format");
1035 format = AUDIO_FORMAT_INVALID;
1036 }
1037 return format;
1038}
1039
1040/*Translates hal format (AOSP) to alsa formats*/
1041uint32_t hal_format_to_alsa(audio_format_t hal_format)
1042{
1043 uint32_t alsa_format;
1044
1045 switch (hal_format) {
1046 case AUDIO_FORMAT_PCM_32_BIT: {
1047 if (platform_supports_true_32bit())
1048 alsa_format = SNDRV_PCM_FORMAT_S32_LE;
1049 else
1050 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1051 }
1052 break;
1053 case AUDIO_FORMAT_PCM_8_BIT:
1054 alsa_format = SNDRV_PCM_FORMAT_S8;
1055 break;
1056 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
1057 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1058 break;
1059 case AUDIO_FORMAT_PCM_8_24_BIT: {
1060 if (platform_supports_true_32bit())
1061 alsa_format = SNDRV_PCM_FORMAT_S32_LE;
1062 else
1063 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1064 }
1065 break;
1066 case AUDIO_FORMAT_PCM_FLOAT:
1067 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1068 break;
1069 default:
1070 case AUDIO_FORMAT_PCM_16_BIT:
1071 alsa_format = SNDRV_PCM_FORMAT_S16_LE;
1072 break;
1073 }
1074 return alsa_format;
1075}
1076
Ashish Jain83a6cc22016-06-28 14:34:17 +05301077/*Translates PCM formats to AOSP formats*/
1078audio_format_t pcm_format_to_hal(uint32_t pcm_format)
1079{
1080 audio_format_t format = AUDIO_FORMAT_INVALID;
1081
1082 switch(pcm_format) {
1083 case PCM_FORMAT_S16_LE:
1084 format = AUDIO_FORMAT_PCM_16_BIT;
1085 break;
1086 case PCM_FORMAT_S24_3LE:
1087 format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
1088 break;
1089 case PCM_FORMAT_S24_LE:
1090 format = AUDIO_FORMAT_PCM_8_24_BIT;
1091 break;
1092 case PCM_FORMAT_S32_LE:
1093 format = AUDIO_FORMAT_PCM_32_BIT;
1094 break;
1095 default:
1096 ALOGW("Incorrect PCM format");
1097 format = AUDIO_FORMAT_INVALID;
1098 }
1099 return format;
1100}
1101
1102/*Translates hal format (AOSP) to alsa formats*/
1103uint32_t hal_format_to_pcm(audio_format_t hal_format)
1104{
1105 uint32_t pcm_format;
1106
1107 switch (hal_format) {
1108 case AUDIO_FORMAT_PCM_32_BIT:
1109 case AUDIO_FORMAT_PCM_8_24_BIT:
1110 case AUDIO_FORMAT_PCM_FLOAT: {
1111 if (platform_supports_true_32bit())
1112 pcm_format = PCM_FORMAT_S32_LE;
1113 else
1114 pcm_format = PCM_FORMAT_S24_3LE;
1115 }
1116 break;
1117 case AUDIO_FORMAT_PCM_8_BIT:
1118 pcm_format = PCM_FORMAT_S8;
1119 break;
1120 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
1121 pcm_format = PCM_FORMAT_S24_3LE;
1122 break;
1123 default:
1124 case AUDIO_FORMAT_PCM_16_BIT:
1125 pcm_format = PCM_FORMAT_S16_LE;
1126 break;
1127 }
1128 return pcm_format;
1129}
1130
Ashish Jainf1eaa582016-05-23 20:54:24 +05301131uint32_t get_alsa_fragment_size(uint32_t bytes_per_sample,
1132 uint32_t sample_rate,
1133 uint32_t noOfChannels)
1134{
1135 uint32_t fragment_size = 0;
1136 uint32_t pcm_offload_time = PCM_OFFLOAD_BUFFER_DURATION;
1137
1138 fragment_size = (pcm_offload_time
1139 * sample_rate
1140 * bytes_per_sample
1141 * noOfChannels)/1000;
1142 if (fragment_size < MIN_PCM_OFFLOAD_FRAGMENT_SIZE)
1143 fragment_size = MIN_PCM_OFFLOAD_FRAGMENT_SIZE;
1144 else if (fragment_size > MAX_PCM_OFFLOAD_FRAGMENT_SIZE)
1145 fragment_size = MAX_PCM_OFFLOAD_FRAGMENT_SIZE;
1146 /*To have same PCM samples for all channels, the buffer size requires to
1147 *be multiple of (number of channels * bytes per sample)
1148 *For writes to succeed, the buffer must be written at address which is multiple of 32
1149 */
1150 fragment_size = ALIGN(fragment_size, (bytes_per_sample * noOfChannels * 32));
1151
1152 ALOGI("PCM offload Fragment size to %d bytes", fragment_size);
1153 return fragment_size;
1154}
1155
1156/* Calculates the fragment size required to configure compress session.
1157 * Based on the alsa format selected, decide if conversion is needed in
1158
1159 * HAL ( e.g. convert AUDIO_FORMAT_PCM_FLOAT input format to
1160 * AUDIO_FORMAT_PCM_24_BIT_PACKED before writing to the compress driver.
1161 */
1162void audio_extn_utils_update_direct_pcm_fragment_size(struct stream_out *out)
1163{
Ashish Jain83a6cc22016-06-28 14:34:17 +05301164 audio_format_t dst_format = out->hal_op_format;
1165 audio_format_t src_format = out->hal_ip_format;
Ashish Jainf1eaa582016-05-23 20:54:24 +05301166 uint32_t hal_op_bytes_per_sample = audio_bytes_per_sample(dst_format);
1167 uint32_t hal_ip_bytes_per_sample = audio_bytes_per_sample(src_format);
1168
1169 out->compr_config.fragment_size =
1170 get_alsa_fragment_size(hal_op_bytes_per_sample,
1171 out->sample_rate,
1172 popcount(out->channel_mask));
1173
1174 if ((src_format != dst_format) &&
1175 hal_op_bytes_per_sample != hal_ip_bytes_per_sample) {
1176
Ashish Jain83a6cc22016-06-28 14:34:17 +05301177 out->hal_fragment_size =
Ashish Jainf1eaa582016-05-23 20:54:24 +05301178 ((out->compr_config.fragment_size * hal_ip_bytes_per_sample) /
1179 hal_op_bytes_per_sample);
1180 ALOGI("enable conversion hal_input_fragment_size is %d src_format %x dst_format %x",
Ashish Jain83a6cc22016-06-28 14:34:17 +05301181 out->hal_fragment_size, src_format, dst_format);
Ashish Jainf1eaa582016-05-23 20:54:24 +05301182 } else {
Ashish Jain83a6cc22016-06-28 14:34:17 +05301183 out->hal_fragment_size = out->compr_config.fragment_size;
Ashish Jainf1eaa582016-05-23 20:54:24 +05301184 }
1185}
1186
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301187/* converts pcm format 24_8 to 8_24 inplace */
1188size_t audio_extn_utils_convert_format_24_8_to_8_24(void *buf, size_t bytes)
1189{
1190 size_t i = 0;
1191 int *int_buf_stream = buf;
1192
1193 if ((bytes % 4) != 0) {
1194 ALOGE("%s: wrong inout buffer! ... is not 32 bit aligned ", __func__);
1195 return -EINVAL;
1196 }
1197
1198 for (; i < (bytes / 4); i++)
1199 int_buf_stream[i] >>= 8;
1200
1201 return bytes;
1202}
1203
1204int get_snd_codec_id(audio_format_t format)
1205{
1206 int id = 0;
1207
1208 switch (format & AUDIO_FORMAT_MAIN_MASK) {
1209 case AUDIO_FORMAT_MP3:
1210 id = SND_AUDIOCODEC_MP3;
1211 break;
1212 case AUDIO_FORMAT_AAC:
1213 id = SND_AUDIOCODEC_AAC;
1214 break;
1215 case AUDIO_FORMAT_AAC_ADTS:
1216 id = SND_AUDIOCODEC_AAC;
1217 break;
Arun Kumar Dasari3b174182016-12-27 13:01:14 +05301218 case AUDIO_FORMAT_AAC_LATM:
1219 id = SND_AUDIOCODEC_AAC;
1220 break;
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301221 case AUDIO_FORMAT_PCM:
1222 id = SND_AUDIOCODEC_PCM;
1223 break;
1224 case AUDIO_FORMAT_FLAC:
1225 id = SND_AUDIOCODEC_FLAC;
1226 break;
1227 case AUDIO_FORMAT_ALAC:
1228 id = SND_AUDIOCODEC_ALAC;
1229 break;
1230 case AUDIO_FORMAT_APE:
1231 id = SND_AUDIOCODEC_APE;
1232 break;
1233 case AUDIO_FORMAT_VORBIS:
1234 id = SND_AUDIOCODEC_VORBIS;
1235 break;
1236 case AUDIO_FORMAT_WMA:
1237 id = SND_AUDIOCODEC_WMA;
1238 break;
1239 case AUDIO_FORMAT_WMA_PRO:
1240 id = SND_AUDIOCODEC_WMA_PRO;
1241 break;
Satish Babu Patakokila0c313922016-12-08 12:07:08 +05301242 case AUDIO_FORMAT_MP2:
1243 id = SND_AUDIOCODEC_MP2;
1244 break;
Satish Babu Patakokila915ecba2017-01-10 17:43:56 +05301245 case AUDIO_FORMAT_AC3:
1246 id = SND_AUDIOCODEC_AC3;
1247 break;
1248 case AUDIO_FORMAT_E_AC3:
1249 case AUDIO_FORMAT_E_AC3_JOC:
1250 id = SND_AUDIOCODEC_EAC3;
1251 break;
1252 case AUDIO_FORMAT_DTS:
1253 case AUDIO_FORMAT_DTS_HD:
1254 id = SND_AUDIOCODEC_DTS;
1255 break;
Ben Romberger1aaaf862017-04-06 17:49:46 -07001256 case AUDIO_FORMAT_DOLBY_TRUEHD:
1257 id = SND_AUDIOCODEC_TRUEHD;
1258 break;
Preetam Singh Ranawat4277a5a2017-01-18 19:02:24 +05301259 case AUDIO_FORMAT_DSD:
1260 id = SND_AUDIOCODEC_DSD;
1261 break;
Dhanalakshmi Siddani18737932016-11-29 17:33:17 +05301262 case AUDIO_FORMAT_APTX:
1263 id = SND_AUDIOCODEC_APTX;
1264 break;
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301265 default:
1266 ALOGE("%s: Unsupported audio format :%x", __func__, format);
1267 }
1268
1269 return id;
1270}
1271
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001272void audio_extn_utils_send_audio_calibration(struct audio_device *adev,
1273 struct audio_usecase *usecase)
1274{
1275 int type = usecase->type;
1276
Dhananjay Kumar14245982017-01-16 20:21:00 +05301277 if (type == PCM_PLAYBACK && usecase->stream.out != NULL) {
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001278 struct stream_out *out = usecase->stream.out;
1279 int snd_device = usecase->out_snd_device;
1280 snd_device = (snd_device == SND_DEVICE_OUT_SPEAKER) ?
Xiaojun Sang040cc9f2015-08-03 19:38:28 +08001281 platform_get_spkr_prot_snd_device(snd_device) : snd_device;
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05301282 platform_send_audio_calibration(adev->platform, usecase,
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001283 out->app_type_cfg.app_type,
Preetam Singh Ranawat61716b12015-12-14 11:55:24 +05301284 usecase->stream.out->app_type_cfg.sample_rate);
Dhananjay Kumar14245982017-01-16 20:21:00 +05301285 } else if (type == PCM_CAPTURE && usecase->stream.in != NULL) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301286 platform_send_audio_calibration(adev->platform, usecase,
1287 usecase->stream.in->app_type_cfg.app_type,
1288 usecase->stream.in->app_type_cfg.sample_rate);
Vidyakumar Athota653aaef2017-03-16 11:11:31 -07001289 } else if (type == PCM_HFP_CALL || type == PCM_CAPTURE) {
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05301290 /* when app type is default. the sample rate is not used to send cal */
1291 platform_send_audio_calibration(adev->platform, usecase,
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301292 platform_get_default_app_type_v2(adev->platform, usecase->type),
1293 48000);
Vidyakumar Athotae57f6002017-03-01 13:13:16 -08001294 } else {
1295 /* No need to send audio calibration for voice and voip call usecases */
1296 if ((type != VOICE_CALL) && (type != VOIP_CALL))
1297 ALOGW("%s: No audio calibration for usecase type = %d", __func__, type);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001298 }
1299}
1300
Ben Rombergera04fabc2014-11-14 12:16:03 -08001301// Base64 Encode and Decode
1302// Not all features supported. This must be used only with following conditions.
1303// Decode Modes: Support with and without padding
1304// CRLF not handling. So no CRLF in string to decode.
1305// Encode Modes: Supports only padding
1306int b64decode(char *inp, int ilen, uint8_t* outp)
1307{
1308 int i, j, k, ii, num;
1309 int rem, pcnt;
1310 uint32_t res=0;
1311 uint8_t getIndex[MAX_BASEINDEX_LEN];
1312 uint8_t tmp, cflag;
1313
1314 if(inp == NULL || outp == NULL || ilen <= 0) {
1315 ALOGE("[%s] received NULL pointer or zero length",__func__);
1316 return -1;
1317 }
1318
1319 memset(getIndex, MAX_BASEINDEX_LEN-1, sizeof(getIndex));
1320 for(i=0;i<BASE_TABLE_SIZE;i++) {
1321 getIndex[(uint8_t)bTable[i]] = (uint8_t)i;
1322 }
1323 getIndex[(uint8_t)'=']=0;
1324
1325 j=0;k=0;
1326 num = ilen/4;
1327 rem = ilen%4;
1328 if(rem==0)
1329 num = num-1;
1330 cflag=0;
1331 for(i=0; i<num; i++) {
1332 res=0;
1333 for(ii=0;ii<4;ii++) {
1334 res = res << 6;
1335 tmp = getIndex[(uint8_t)inp[j++]];
1336 res = res | tmp;
1337 cflag = cflag | tmp;
1338 }
1339 outp[k++] = (res >> 16)&0xFF;
1340 outp[k++] = (res >> 8)&0xFF;
1341 outp[k++] = res & 0xFF;
1342 }
1343
1344 // Handle last bytes special
1345 pcnt=0;
1346 if(rem == 0) {
1347 //With padding or full data
1348 res = 0;
1349 for(ii=0;ii<4;ii++) {
1350 if(inp[j] == '=')
1351 pcnt++;
1352 res = res << 6;
1353 tmp = getIndex[(uint8_t)inp[j++]];
1354 res = res | tmp;
1355 cflag = cflag | tmp;
1356 }
1357 outp[k++] = res >> 16;
1358 if(pcnt == 2)
1359 goto done;
1360 outp[k++] = (res>>8)&0xFF;
1361 if(pcnt == 1)
1362 goto done;
1363 outp[k++] = res&0xFF;
1364 } else {
1365 //without padding
1366 res = 0;
1367 for(i=0;i<rem;i++) {
1368 res = res << 6;
1369 tmp = getIndex[(uint8_t)inp[j++]];
1370 res = res | tmp;
1371 cflag = cflag | tmp;
1372 }
1373 for(i=rem;i<4;i++) {
1374 res = res << 6;
1375 pcnt++;
1376 }
1377 outp[k++] = res >> 16;
1378 if(pcnt == 2)
1379 goto done;
1380 outp[k++] = (res>>8)&0xFF;
1381 if(pcnt == 1)
1382 goto done;
1383 outp[k++] = res&0xFF;
1384 }
1385done:
1386 if(cflag == 0xFF) {
1387 ALOGE("[%s] base64 decode failed. Invalid character found %s",
1388 __func__, inp);
1389 return 0;
1390 }
1391 return k;
1392}
1393
1394int b64encode(uint8_t *inp, int ilen, char* outp)
1395{
1396 int i,j,k, num;
1397 int rem=0;
1398 uint32_t res=0;
1399
1400 if(inp == NULL || outp == NULL || ilen<=0) {
1401 ALOGE("[%s] received NULL pointer or zero input length",__func__);
1402 return -1;
1403 }
1404
1405 num = ilen/3;
1406 rem = ilen%3;
1407 j=0;k=0;
1408 for(i=0; i<num; i++) {
1409 //prepare index
1410 res = inp[j++]<<16;
1411 res = res | inp[j++]<<8;
1412 res = res | inp[j++];
1413 //get output map from index
1414 outp[k++] = (char) bTable[(res>>18)&0x3F];
1415 outp[k++] = (char) bTable[(res>>12)&0x3F];
1416 outp[k++] = (char) bTable[(res>>6)&0x3F];
1417 outp[k++] = (char) bTable[res&0x3F];
1418 }
1419
1420 switch(rem) {
1421 case 1:
1422 res = inp[j++]<<16;
1423 outp[k++] = (char) bTable[res>>18];
1424 outp[k++] = (char) bTable[(res>>12)&0x3F];
1425 //outp[k++] = '=';
1426 //outp[k++] = '=';
1427 break;
1428 case 2:
1429 res = inp[j++]<<16;
1430 res = res | inp[j++]<<8;
1431 outp[k++] = (char) bTable[res>>18];
1432 outp[k++] = (char) bTable[(res>>12)&0x3F];
1433 outp[k++] = (char) bTable[(res>>6)&0x3F];
1434 //outp[k++] = '=';
1435 break;
1436 default:
1437 break;
1438 }
Ben Rombergera04fabc2014-11-14 12:16:03 -08001439 outp[k] = '\0';
1440 return k;
1441}
Ashish Jain81eb2a82015-05-13 10:52:34 +05301442
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301443
1444int audio_extn_utils_get_codec_version(const char *snd_card_name,
1445 int card_num,
1446 char *codec_version)
1447{
1448 char procfs_path[50];
1449 FILE *fp;
1450
1451 if (strstr(snd_card_name, "tasha")) {
1452 snprintf(procfs_path, sizeof(procfs_path),
1453 "/proc/asound/card%d/codecs/tasha/version", card_num);
1454 if ((fp = fopen(procfs_path, "r")) != NULL) {
1455 fgets(codec_version, CODEC_VERSION_MAX_LENGTH, fp);
1456 fclose(fp);
1457 } else {
1458 ALOGE("%s: ERROR. cannot open %s", __func__, procfs_path);
1459 return -ENOENT;
1460 }
1461 ALOGD("%s: codec version %s", __func__, codec_version);
1462 }
1463
1464 return 0;
1465}
1466
1467
Ashish Jain81eb2a82015-05-13 10:52:34 +05301468#ifdef AUDIO_EXTERNAL_HDMI_ENABLED
1469
1470void get_default_compressed_channel_status(
1471 unsigned char *channel_status)
1472{
Ashish Jain81eb2a82015-05-13 10:52:34 +05301473 memset(channel_status,0,24);
1474
1475 /* block start bit in preamble bit 3 */
1476 channel_status[0] |= PROFESSIONAL;
1477 //compre out
1478 channel_status[0] |= NON_LPCM;
1479 // sample rate; fixed 48K for default/transcode
1480 channel_status[3] |= SR_48000;
1481}
1482
1483#ifdef HDMI_PASSTHROUGH_ENABLED
1484int32_t get_compressed_channel_status(void *audio_stream_data,
1485 uint32_t audio_frame_size,
1486 unsigned char *channel_status,
1487 enum audio_parser_code_type codec_type)
1488 // codec_type - AUDIO_PARSER_CODEC_AC3
1489 // - AUDIO_PARSER_CODEC_DTS
1490{
1491 unsigned char *stream;
1492 int ret = 0;
1493 stream = (unsigned char *)audio_stream_data;
1494
1495 if (audio_stream_data == NULL || audio_frame_size == 0) {
1496 ALOGW("no buffer to get channel status, return default for compress");
1497 get_default_compressed_channel_status(channel_status);
1498 return ret;
1499 }
1500
1501 memset(channel_status,0,24);
1502 if(init_audio_parser(stream, audio_frame_size, codec_type) == -1)
1503 {
1504 ALOGE("init audio parser failed");
1505 return -1;
1506 }
1507 ret = get_channel_status(channel_status, codec_type);
1508 return ret;
1509
1510}
1511
1512#endif
1513
1514void get_lpcm_channel_status(uint32_t sampleRate,
1515 unsigned char *channel_status)
1516{
1517 int32_t status = 0;
Ashish Jain81eb2a82015-05-13 10:52:34 +05301518 memset(channel_status,0,24);
1519 /* block start bit in preamble bit 3 */
1520 channel_status[0] |= PROFESSIONAL;
1521 //LPCM OUT
1522 channel_status[0] &= ~NON_LPCM;
1523
1524 switch (sampleRate) {
1525 case 8000:
1526 case 11025:
1527 case 12000:
1528 case 16000:
1529 case 22050:
1530 channel_status[3] |= SR_NOTID;
Preetam Singh Ranawat61716b12015-12-14 11:55:24 +05301531 break;
Ashish Jain81eb2a82015-05-13 10:52:34 +05301532 case 24000:
1533 channel_status[3] |= SR_24000;
1534 break;
1535 case 32000:
1536 channel_status[3] |= SR_32000;
1537 break;
1538 case 44100:
1539 channel_status[3] |= SR_44100;
1540 break;
1541 case 48000:
1542 channel_status[3] |= SR_48000;
1543 break;
1544 case 88200:
1545 channel_status[3] |= SR_88200;
1546 break;
1547 case 96000:
1548 channel_status[3] |= SR_96000;
1549 break;
1550 case 176400:
1551 channel_status[3] |= SR_176400;
1552 break;
1553 case 192000:
1554 channel_status[3] |= SR_192000;
1555 break;
1556 default:
1557 ALOGV("Invalid sample_rate %u\n", sampleRate);
1558 status = -1;
1559 break;
1560 }
1561}
1562
1563void audio_utils_set_hdmi_channel_status(struct stream_out *out, char * buffer, size_t bytes)
1564{
1565 unsigned char channel_status[24]={0};
1566 struct snd_aes_iec958 iec958;
1567 const char *mixer_ctl_name = "IEC958 Playback PCM Stream";
1568 struct mixer_ctl *ctl;
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +05301569 ALOGV("%s: buffer %s bytes %zd", __func__, buffer, bytes);
Ashish Jain81eb2a82015-05-13 10:52:34 +05301570#ifdef HDMI_PASSTHROUGH_ENABLED
1571 if (audio_extn_is_dolby_format(out->format) &&
1572 /*TODO:Extend code to support DTS passthrough*/
1573 /*set compressed channel status bits*/
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05301574 audio_extn_passthru_is_passthrough_stream(out)){
Ashish Jain81eb2a82015-05-13 10:52:34 +05301575 get_compressed_channel_status(buffer, bytes, channel_status, AUDIO_PARSER_CODEC_AC3);
1576 } else
1577#endif
1578 {
1579 /*set channel status bit for LPCM*/
1580 get_lpcm_channel_status(out->sample_rate, channel_status);
1581 }
1582
1583 memcpy(iec958.status, channel_status,sizeof(iec958.status));
1584 ctl = mixer_get_ctl_by_name(out->dev->mixer, mixer_ctl_name);
1585 if (!ctl) {
1586 ALOGE("%s: Could not get ctl for mixer cmd - %s",
1587 __func__, mixer_ctl_name);
1588 return;
1589 }
1590 if (mixer_ctl_set_array(ctl, &iec958, sizeof(iec958)) < 0) {
1591 ALOGE("%s: Could not set channel status for ext HDMI ",
1592 __func__);
1593 return;
1594 }
1595
1596}
1597#endif
Manish Dewangan3ccdea52017-02-13 19:31:54 +05301598
1599int audio_extn_utils_get_avt_device_drift(
1600 struct audio_usecase *usecase,
1601 struct audio_avt_device_drift_param *drift_param)
1602{
1603 int ret = 0, count = 0;
1604 char avt_device_drift_mixer_ctl_name[MIXER_PATH_MAX_LENGTH] = {0};
1605 struct mixer_ctl *ctl = NULL;
1606 struct audio_avt_device_drift_stats drift_stats;
1607 struct audio_device *adev = NULL;
1608
1609 if (usecase != NULL && usecase->type == PCM_PLAYBACK) {
1610 adev = usecase->stream.out->dev;
1611 switch(usecase->out_snd_device) {
1612 case SND_DEVICE_OUT_HDMI:
1613 strlcpy(avt_device_drift_mixer_ctl_name,
1614 "HDMI RX Drift",
1615 MIXER_PATH_MAX_LENGTH);
1616 break;
1617 case SND_DEVICE_OUT_DISPLAY_PORT:
1618 strlcpy(avt_device_drift_mixer_ctl_name,
1619 "DISPLAY Port RX Drift",
1620 MIXER_PATH_MAX_LENGTH);
1621 break;
1622 default :
1623 ALOGE("%s: Unsupported device %d",__func__,
1624 usecase->stream.out->devices);
1625 ret = -EINVAL;
1626 }
1627 } else {
1628 ALOGE("%s: Invalid usecase %d ",__func__, usecase->type);
1629 ret = -EINVAL;
1630 }
1631
1632 if(ret)
1633 goto done;
1634
1635 ctl = mixer_get_ctl_by_name(adev->mixer, avt_device_drift_mixer_ctl_name);
1636 if (!ctl) {
1637 ALOGE("%s: Could not get ctl for mixer cmd - %s",
1638 __func__, avt_device_drift_mixer_ctl_name);
1639
1640 ret = -EINVAL;
1641 goto done;
1642 }
1643
1644 ALOGV("%s: Getting AV Timer vs Device Drift mixer ctrl name %s", __func__,
1645 avt_device_drift_mixer_ctl_name);
1646
1647 mixer_ctl_update(ctl);
1648 count = mixer_ctl_get_num_values(ctl);
1649 if (count != sizeof(struct audio_avt_device_drift_stats)) {
1650 ALOGE("%s: mixer_ctl_get_num_values() invalid drift_stats data size",
1651 __func__);
1652
1653 ret = -EINVAL;
1654 goto done;
1655 }
1656
1657 ret = mixer_ctl_get_array(ctl, (void *)&drift_stats, count);
1658 if (ret != 0) {
1659 ALOGE("%s: mixer_ctl_get_array() failed to get drift_stats Params",
1660 __func__);
1661
1662 ret = -EINVAL;
1663 goto done;
1664 }
1665 memcpy(drift_param, &drift_stats.drift_param,
1666 sizeof(struct audio_avt_device_drift_param));
1667done:
1668 return ret;
1669}
Manish Dewangan07de2142017-02-27 19:27:20 +05301670
1671#ifdef SNDRV_COMPRESS_PATH_DELAY
1672int audio_extn_utils_compress_get_dsp_latency(struct stream_out *out)
1673{
1674 int ret = -EINVAL;
1675 struct snd_compr_metadata metadata;
1676 int delay_ms = COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
1677
1678 if (property_get_bool("audio.playback.dsp.pathdelay", false)) {
1679 ALOGD("%s:: Quering DSP delay %d",__func__, __LINE__);
1680 if (!(is_offload_usecase(out->usecase))) {
1681 ALOGE("%s:: not supported for non offload session", __func__);
1682 goto exit;
1683 }
1684
1685 if (!out->compr) {
1686 ALOGD("%s:: Invalid compress handle,returning default dsp latency",
1687 __func__);
1688 goto exit;
1689 }
1690
1691 metadata.key = SNDRV_COMPRESS_PATH_DELAY;
1692 ret = compress_get_metadata(out->compr, &metadata);
1693 if(ret) {
1694 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
1695 goto exit;
1696 }
1697 delay_ms = metadata.value[0] / 1000; /*convert to ms*/
1698 } else {
1699 ALOGD("%s:: Using Fix DSP delay",__func__);
1700 }
1701
1702exit:
1703 ALOGD("%s:: delay in ms is %d",__func__, delay_ms);
1704 return delay_ms;
1705}
1706#else
1707int audio_extn_utils_compress_get_dsp_latency(struct stream_out *out __unused)
1708{
1709 return COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
1710}
1711#endif
Manish Dewangan69426c82017-01-30 17:35:36 +05301712
1713#ifdef SNDRV_COMPRESS_RENDER_MODE
1714int audio_extn_utils_compress_set_render_mode(struct stream_out *out)
1715{
1716 struct snd_compr_metadata metadata;
1717 int ret = -EINVAL;
1718
1719 if (!(is_offload_usecase(out->usecase))) {
1720 ALOGE("%s:: not supported for non offload session", __func__);
1721 goto exit;
1722 }
1723
1724 if (!out->compr) {
1725 ALOGD("%s:: Invalid compress handle",
1726 __func__);
1727 goto exit;
1728 }
1729
1730 ALOGD("%s:: render mode %d", __func__, out->render_mode);
1731
1732 metadata.key = SNDRV_COMPRESS_RENDER_MODE;
1733 if (out->render_mode == RENDER_MODE_AUDIO_MASTER) {
1734 metadata.value[0] = SNDRV_COMPRESS_RENDER_MODE_AUDIO_MASTER;
1735 } else if (out->render_mode == RENDER_MODE_AUDIO_STC_MASTER) {
1736 metadata.value[0] = SNDRV_COMPRESS_RENDER_MODE_STC_MASTER;
1737 } else {
1738 ret = 0;
1739 goto exit;
1740 }
1741 ret = compress_set_metadata(out->compr, &metadata);
1742 if(ret) {
1743 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
1744 }
1745exit:
1746 return ret;
1747}
1748#else
1749int audio_extn_utils_compress_set_render_mode(struct stream_out *out __unused)
1750{
1751 ALOGD("%s:: configuring render mode not supported", __func__);
1752 return 0;
1753}
1754#endif
Manish Dewangan58229382017-02-02 15:48:41 +05301755
1756#ifdef SNDRV_COMPRESS_CLK_REC_MODE
1757int audio_extn_utils_compress_set_clk_rec_mode(
1758 struct audio_usecase *usecase)
1759{
1760 struct snd_compr_metadata metadata;
1761 struct stream_out *out = NULL;
1762 int ret = -EINVAL;
1763
1764 if (usecase != NULL && usecase->type != PCM_PLAYBACK) {
1765 ALOGE("%s:: Invalid use case", __func__);
1766 goto exit;
1767 }
1768
1769 out = usecase->stream.out;
1770 if (!out) {
1771 ALOGE("%s:: invalid stream", __func__);
1772 goto exit;
1773 }
1774
1775 if (!is_offload_usecase(out->usecase)) {
1776 ALOGE("%s:: not supported for non offload session", __func__);
1777 goto exit;
1778 }
1779
1780 if (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER) {
1781 ALOGD("%s:: clk recovery is only supported in STC render mode",
1782 __func__);
1783 ret = 0;
1784 goto exit;
1785 }
1786
1787 if (!out->compr) {
1788 ALOGD("%s:: Invalid compress handle",
1789 __func__);
1790 goto exit;
1791 }
1792 metadata.key = SNDRV_COMPRESS_CLK_REC_MODE;
1793 switch(usecase->out_snd_device) {
1794 case SND_DEVICE_OUT_HDMI:
1795 case SND_DEVICE_OUT_SPEAKER_AND_HDMI:
1796 case SND_DEVICE_OUT_DISPLAY_PORT:
1797 case SND_DEVICE_OUT_SPEAKER_AND_DISPLAY_PORT:
1798 metadata.value[0] = SNDRV_COMPRESS_CLK_REC_MODE_NONE;
1799 break;
1800 default:
1801 metadata.value[0] = SNDRV_COMPRESS_CLK_REC_MODE_AUTO;
1802 break;
1803 }
1804
1805 ALOGD("%s:: clk recovery mode %d",__func__, metadata.value[0]);
1806
1807 ret = compress_set_metadata(out->compr, &metadata);
1808 if(ret) {
1809 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
1810 }
1811
1812exit:
1813 return ret;
1814}
1815#else
1816int audio_extn_utils_compress_set_clk_rec_mode(
1817 struct audio_usecase *usecase __unused)
1818{
1819 ALOGD("%s:: configuring render mode not supported", __func__);
1820 return 0;
1821}
1822#endif
Manish Dewangan27346042017-03-01 12:56:12 +05301823
1824#ifdef SNDRV_COMPRESS_RENDER_WINDOW
1825int audio_extn_utils_compress_set_render_window(
1826 struct stream_out *out,
1827 struct audio_out_render_window_param *render_window)
1828{
1829 struct snd_compr_metadata metadata;
1830 int ret = -EINVAL;
1831
1832 ALOGD("%s:: render window start 0x%"PRIx64" end 0x%"PRIx64"",
1833 __func__,render_window->render_ws, render_window->render_we);
1834
1835 if(render_window == NULL) {
1836 ALOGE("%s:: Invalid render_window", __func__);
1837 goto exit;
1838 }
1839
1840 if (!is_offload_usecase(out->usecase)) {
1841 ALOGE("%s:: not supported for non offload session", __func__);
1842 goto exit;
1843 }
1844
1845 if ((out->render_mode == RENDER_MODE_AUDIO_MASTER) ||
1846 (out->render_mode == RENDER_MODE_AUDIO_STC_MASTER)) {
1847 memcpy(&out->render_window, render_window,
1848 sizeof(struct audio_out_render_window_param));
1849 } else {
1850 ALOGD("%s:: only supported in timestamp mode, current "
1851 "render mode mode %d", __func__, out->render_mode);
1852 goto exit;
1853 }
1854
1855 if (!out->compr) {
1856 ALOGW("%s:: offload session not yet opened,"
1857 "render window will be configure later", __func__);
1858 /* store render window to reconfigure in start_output_stream() */
1859 goto exit;
1860 }
1861
1862 metadata.key = SNDRV_COMPRESS_RENDER_WINDOW;
1863 /*render window start value */
1864 metadata.value[0] = 0xFFFFFFFF & render_window->render_ws; /* lsb */
1865 metadata.value[1] = \
1866 (0xFFFFFFFF00000000 & render_window->render_ws) >> 32; /* msb*/
1867 /*render window end value */
1868 metadata.value[2] = 0xFFFFFFFF & render_window->render_we; /* lsb */
1869 metadata.value[3] = \
1870 (0xFFFFFFFF00000000 & render_window->render_we) >> 32; /* msb*/
1871
1872 ret = compress_set_metadata(out->compr, &metadata);
1873 if(ret) {
1874 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
1875 }
1876
1877exit:
1878 return ret;
1879}
1880#else
1881int audio_extn_utils_compress_set_render_window(
1882 struct stream_out *out __unused,
1883 struct audio_out_render_window_param *render_window __unused)
1884{
1885 ALOGD("%s:: configuring render window not supported", __func__);
1886 return 0;
1887}
1888#endif
Manish Dewangan14956cc2017-02-14 18:54:42 +05301889
1890#ifdef SNDRV_COMPRESS_START_DELAY
1891int audio_extn_utils_compress_set_start_delay(
1892 struct stream_out *out,
1893 struct audio_out_start_delay_param *delay_param)
1894{
1895 struct snd_compr_metadata metadata;
1896 int ret = -EINVAL;
1897
1898 if(delay_param == NULL) {
1899 ALOGE("%s:: Invalid delay_param", __func__);
1900 goto exit;
1901 }
1902
1903 ALOGD("%s:: render start delay 0x%"PRIx64" ", __func__,
1904 delay_param->start_delay);
1905
1906 if (!is_offload_usecase(out->usecase)) {
1907 ALOGE("%s:: not supported for non offload session", __func__);
1908 goto exit;
1909 }
1910
1911 if ((out->render_mode == RENDER_MODE_AUDIO_MASTER) ||
1912 (out->render_mode == RENDER_MODE_AUDIO_STC_MASTER)) {
1913 /* store it to reconfigure in start_output_stream() */
1914 out->delay_param.start_delay = delay_param->start_delay;
1915 } else {
1916 ALOGD("%s:: only supported in timestamp mode, current "
1917 "render mode mode %d", __func__, out->render_mode);
1918 goto exit;
1919 }
1920
1921 if (!out->compr) {
1922 ALOGW("%s:: offload session not yet opened,"
1923 "start delay will be configure later", __func__);
1924 goto exit;
1925 }
1926
1927 metadata.key = SNDRV_COMPRESS_START_DELAY;
1928 metadata.value[0] = 0xFFFFFFFF & delay_param->start_delay; /* lsb */
1929 metadata.value[1] = \
1930 (0xFFFFFFFF00000000 & delay_param->start_delay) >> 32; /* msb*/
1931
1932 ret = compress_set_metadata(out->compr, &metadata);
1933 if(ret) {
1934 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
1935 }
1936
1937exit:
1938 return ret;
1939}
1940#else
1941int audio_extn_utils_compress_set_start_delay(
1942 struct stream_out *out __unused,
1943 struct audio_out_start_delay_param *delay_param __unused)
1944{
1945 ALOGD("%s:: configuring render window not supported", __func__);
1946 return 0;
1947}
1948#endif