blob: 13faf642a68817426cb59e2013591ff1c08e2e9d [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
Ashish Jain81eb2a82015-05-13 10:52:34 +053073#ifdef AUDIO_EXTERNAL_HDMI_ENABLED
74#define PROFESSIONAL (1<<0) /* 0 = consumer, 1 = professional */
75#define NON_LPCM (1<<1) /* 0 = audio, 1 = non-audio */
76#define SR_44100 (0<<0) /* 44.1kHz */
77#define SR_NOTID (1<<0) /* non indicated */
78#define SR_48000 (2<<0) /* 48kHz */
79#define SR_32000 (3<<0) /* 32kHz */
80#define SR_22050 (4<<0) /* 22.05kHz */
81#define SR_24000 (6<<0) /* 24kHz */
82#define SR_88200 (8<<0) /* 88.2kHz */
83#define SR_96000 (10<<0) /* 96kHz */
84#define SR_176400 (12<<0) /* 176.4kHz */
85#define SR_192000 (14<<0) /* 192kHz */
86
87#endif
Manish Dewangan07de2142017-02-27 19:27:20 +053088
89/* ToDo: Check and update a proper value in msec */
90#define COMPRESS_OFFLOAD_PLAYBACK_LATENCY 50
91
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070092struct string_to_enum {
93 const char *name;
94 uint32_t value;
95};
96
97const struct string_to_enum s_flag_name_to_enum_table[] = {
98 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_DIRECT),
99 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_PRIMARY),
100 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_FAST),
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800101 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_RAW),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700102 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_DEEP_BUFFER),
103 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD),
104 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_NON_BLOCKING),
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700105 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_HW_AV_SYNC),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700106#ifdef INCALL_MUSIC_ENABLED
107 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_INCALL_MUSIC),
108#endif
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700109 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH),
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530110 STRING_TO_ENUM(AUDIO_INPUT_FLAG_NONE),
111 STRING_TO_ENUM(AUDIO_INPUT_FLAG_FAST),
112 STRING_TO_ENUM(AUDIO_INPUT_FLAG_HW_HOTWORD),
113 STRING_TO_ENUM(AUDIO_INPUT_FLAG_RAW),
114 STRING_TO_ENUM(AUDIO_INPUT_FLAG_SYNC),
Dhananjay Kumaree4d2002016-10-25 18:02:58 +0530115 STRING_TO_ENUM(AUDIO_INPUT_FLAG_TIMESTAMP),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700116};
117
118const struct string_to_enum s_format_name_to_enum_table[] = {
Ashish Jain83a6cc22016-06-28 14:34:17 +0530119 STRING_TO_ENUM(AUDIO_FORMAT_PCM_8_BIT),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700120 STRING_TO_ENUM(AUDIO_FORMAT_PCM_16_BIT),
Ashish Jain5106d362016-05-11 19:23:33 +0530121 STRING_TO_ENUM(AUDIO_FORMAT_PCM_24_BIT_PACKED),
122 STRING_TO_ENUM(AUDIO_FORMAT_PCM_8_24_BIT),
Ashish Jainf1eaa582016-05-23 20:54:24 +0530123 STRING_TO_ENUM(AUDIO_FORMAT_PCM_32_BIT),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700124 STRING_TO_ENUM(AUDIO_FORMAT_MP3),
125 STRING_TO_ENUM(AUDIO_FORMAT_AAC),
126 STRING_TO_ENUM(AUDIO_FORMAT_VORBIS),
Mingming Yinae3530f2014-07-03 16:50:18 -0700127 STRING_TO_ENUM(AUDIO_FORMAT_AMR_NB),
128 STRING_TO_ENUM(AUDIO_FORMAT_AMR_WB),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700129 STRING_TO_ENUM(AUDIO_FORMAT_AC3),
Mingming Yinae3530f2014-07-03 16:50:18 -0700130 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700131 STRING_TO_ENUM(AUDIO_FORMAT_DTS),
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530132 STRING_TO_ENUM(AUDIO_FORMAT_DTS_HD),
133#ifdef AUDIO_EXTN_FORMATS_ENABLED
134 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3_JOC),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700135 STRING_TO_ENUM(AUDIO_FORMAT_WMA),
136 STRING_TO_ENUM(AUDIO_FORMAT_WMA_PRO),
137 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADIF),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700138 STRING_TO_ENUM(AUDIO_FORMAT_AMR_WB_PLUS),
139 STRING_TO_ENUM(AUDIO_FORMAT_EVRC),
140 STRING_TO_ENUM(AUDIO_FORMAT_EVRCB),
141 STRING_TO_ENUM(AUDIO_FORMAT_EVRCWB),
142 STRING_TO_ENUM(AUDIO_FORMAT_QCELP),
143 STRING_TO_ENUM(AUDIO_FORMAT_MP2),
144 STRING_TO_ENUM(AUDIO_FORMAT_EVRCNW),
Amit Shekhar6f461b12014-08-01 14:52:58 -0700145 STRING_TO_ENUM(AUDIO_FORMAT_FLAC),
Satya Krishna Pindiproli70471602015-04-24 19:12:43 +0530146 STRING_TO_ENUM(AUDIO_FORMAT_ALAC),
147 STRING_TO_ENUM(AUDIO_FORMAT_APE),
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700148 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3_JOC),
Alexy Josephcd8eaed2014-12-11 12:46:53 -0800149 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LC),
150 STRING_TO_ENUM(AUDIO_FORMAT_AAC_HE_V1),
151 STRING_TO_ENUM(AUDIO_FORMAT_AAC_HE_V2),
Manish Dewangana6fc5442015-08-24 20:30:31 +0530152 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS),
Ashish Jaine513a872015-11-19 17:00:56 +0530153 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_LC),
154 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_HE_V1),
155 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_HE_V2),
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +0530156 STRING_TO_ENUM(AUDIO_FORMAT_DSD),
Arun Kumar Dasari3b174182016-12-27 13:01:14 +0530157 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM),
158 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM_LC),
159 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM_HE_V1),
160 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM_HE_V2),
Dhanalakshmi Siddani18737932016-11-29 17:33:17 +0530161 STRING_TO_ENUM(AUDIO_FORMAT_APTX),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700162#endif
163};
164
Manish Dewangan3ccdea52017-02-13 19:31:54 +0530165/* payload structure avt_device drift query */
166struct audio_avt_device_drift_stats {
167 uint32_t minor_version;
168 /* Indicates the device interface direction as either
169 * source (Tx) or sink (Rx).
170 */
171 uint16_t device_direction;
172 /*params exposed to client */
173 struct audio_avt_device_drift_param drift_param;
174};
175
Ben Rombergera04fabc2014-11-14 12:16:03 -0800176static char bTable[BASE_TABLE_SIZE] = {
177 'A','B','C','D','E','F','G','H','I','J','K','L',
178 'M','N','O','P','Q','R','S','T','U','V','W','X',
179 'Y','Z','a','b','c','d','e','f','g','h','i','j',
180 'k','l','m','n','o','p','q','r','s','t','u','v',
181 'w','x','y','z','0','1','2','3','4','5','6','7',
182 '8','9','+','/'
183};
184
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700185static uint32_t string_to_enum(const struct string_to_enum *table, size_t size,
186 const char *name)
187{
188 size_t i;
189 for (i = 0; i < size; i++) {
190 if (strcmp(table[i].name, name) == 0) {
191 ALOGV("%s found %s", __func__, table[i].name);
192 return table[i].value;
193 }
194 }
195 return 0;
196}
197
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530198static audio_io_flags_t parse_flag_names(char *name)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700199{
200 uint32_t flag = 0;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530201 audio_io_flags_t io_flags;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800202 char *last_r;
203 char *flag_name = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700204 while (flag_name != NULL) {
205 if (strlen(flag_name) != 0) {
206 flag |= string_to_enum(s_flag_name_to_enum_table,
207 ARRAY_SIZE(s_flag_name_to_enum_table),
208 flag_name);
209 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800210 flag_name = strtok_r(NULL, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700211 }
212
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800213 ALOGV("parse_flag_names: flag - %x", flag);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530214 io_flags.in_flags = (audio_input_flags_t)flag;
215 io_flags.out_flags = (audio_output_flags_t)flag;
216 return io_flags;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700217}
218
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530219static void parse_format_names(char *name, struct streams_io_cfg *s_info)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700220{
221 struct stream_format *sf_info = NULL;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800222 char *last_r;
223 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700224
225 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG) == 0)
226 return;
227
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530228 list_init(&s_info->format_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700229 while (str != NULL) {
230 audio_format_t format = (audio_format_t)string_to_enum(s_format_name_to_enum_table,
231 ARRAY_SIZE(s_format_name_to_enum_table), str);
232 ALOGV("%s: format - %d", __func__, format);
233 if (format != 0) {
234 sf_info = (struct stream_format *)calloc(1, sizeof(struct stream_format));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700235 if (sf_info == NULL)
236 break; /* return whatever was parsed */
237
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700238 sf_info->format = format;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530239 list_add_tail(&s_info->format_list, &sf_info->list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700240 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800241 str = strtok_r(NULL, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700242 }
243}
244
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530245static void parse_sample_rate_names(char *name, struct streams_io_cfg *s_info)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700246{
Amit Shekhar6f461b12014-08-01 14:52:58 -0700247 struct stream_sample_rate *ss_info = NULL;
248 uint32_t sample_rate = 48000;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800249 char *last_r;
250 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700251
Amit Shekhar6f461b12014-08-01 14:52:58 -0700252 if (str != NULL && 0 == strcmp(str, DYNAMIC_VALUE_TAG))
253 return;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700254
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530255 list_init(&s_info->sample_rate_list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700256 while (str != NULL) {
257 sample_rate = (uint32_t)strtol(str, (char **)NULL, 10);
258 ALOGV("%s: sample_rate - %d", __func__, sample_rate);
259 if (0 != sample_rate) {
260 ss_info = (struct stream_sample_rate *)calloc(1, sizeof(struct stream_sample_rate));
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800261 if (!ss_info) {
262 ALOGE("%s: memory allocation failure", __func__);
263 return;
264 }
Amit Shekhar6f461b12014-08-01 14:52:58 -0700265 ss_info->sample_rate = sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530266 list_add_tail(&s_info->sample_rate_list, &ss_info->list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700267 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800268 str = strtok_r(NULL, "|", &last_r);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700269 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700270}
271
272static int parse_bit_width_names(char *name)
273{
274 int bit_width = 16;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800275 char *last_r;
276 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700277
278 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG))
279 bit_width = (int)strtol(str, (char **)NULL, 10);
280
281 ALOGV("%s: bit_width - %d", __func__, bit_width);
282 return bit_width;
283}
284
285static int parse_app_type_names(void *platform, char *name)
286{
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700287 int app_type = platform_get_default_app_type(platform);
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800288 char *last_r;
289 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700290
291 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG))
292 app_type = (int)strtol(str, (char **)NULL, 10);
293
294 ALOGV("%s: app_type - %d", __func__, app_type);
295 return app_type;
296}
297
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530298static void update_streams_cfg_list(cnode *root, void *platform,
299 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700300{
301 cnode *node = root->first_child;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530302 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700303
304 ALOGV("%s", __func__);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530305 s_info = (struct streams_io_cfg *)calloc(1, sizeof(struct streams_io_cfg));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700306
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530307 if (!s_info) {
308 ALOGE("failed to allocate mem for s_info list element");
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700309 return;
310 }
311
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700312 while (node) {
313 if (strcmp(node->name, FLAGS_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530314 s_info->flags = parse_flag_names((char *)node->value);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530315 } else if (strcmp(node->name, PROFILES_TAG) == 0) {
316 strlcpy(s_info->profile, (char *)node->value, sizeof(s_info->profile));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700317 } else if (strcmp(node->name, FORMATS_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530318 parse_format_names((char *)node->value, s_info);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700319 } else if (strcmp(node->name, SAMPLING_RATES_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530320 s_info->app_type_cfg.sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
321 parse_sample_rate_names((char *)node->value, s_info);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700322 } else if (strcmp(node->name, BIT_WIDTH_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530323 s_info->app_type_cfg.bit_width = parse_bit_width_names((char *)node->value);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700324 } else if (strcmp(node->name, APP_TYPE_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530325 s_info->app_type_cfg.app_type = parse_app_type_names(platform, (char *)node->value);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700326 }
327 node = node->next;
328 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530329 list_add_tail(streams_cfg_list, &s_info->list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700330}
331
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530332static void load_cfg_list(cnode *root, void *platform,
333 struct listnode *streams_output_cfg_list,
334 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700335{
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530336 cnode *node = NULL;
337
338 node = config_find(root, OUTPUTS_TAG);
339 if (node != NULL) {
340 node = node->first_child;
341 while (node) {
342 ALOGV("%s: loading output %s", __func__, node->name);
343 update_streams_cfg_list(node, platform, streams_output_cfg_list);
344 node = node->next;
345 }
346 } else {
347 ALOGI("%s: could not load output, node is NULL", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700348 }
349
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530350 node = config_find(root, INPUTS_TAG);
351 if (node != NULL) {
352 node = node->first_child;
353 while (node) {
354 ALOGV("%s: loading input %s", __func__, node->name);
355 update_streams_cfg_list(node, platform, streams_input_cfg_list);
356 node = node->next;
357 }
358 } else {
359 ALOGI("%s: could not load input, node is NULL", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700360 }
361}
362
363static void send_app_type_cfg(void *platform, struct mixer *mixer,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530364 struct listnode *streams_output_cfg_list,
365 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700366{
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530367 size_t app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT] = {0};
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700368 int length = 0, i, num_app_types = 0;
369 struct listnode *node;
370 bool update;
371 struct mixer_ctl *ctl = NULL;
372 const char *mixer_ctl_name = "App Type Config";
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530373 struct streams_io_cfg *s_info = NULL;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700374
375 if (!mixer) {
376 ALOGE("%s: mixer is null",__func__);
377 return;
378 }
379 ctl = mixer_get_ctl_by_name(mixer, mixer_ctl_name);
380 if (!ctl) {
381 ALOGE("%s: Could not get ctl for mixer cmd - %s",__func__, mixer_ctl_name);
382 return;
383 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530384 app_type_cfg[length++] = num_app_types;
385
386 if (list_empty(streams_output_cfg_list)) {
387 app_type_cfg[length++] = platform_get_default_app_type_v2(platform, PCM_PLAYBACK);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700388 app_type_cfg[length++] = 48000;
389 app_type_cfg[length++] = 16;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530390 num_app_types += 1;
391 }
392 if (list_empty(streams_input_cfg_list)) {
393 app_type_cfg[length++] = platform_get_default_app_type_v2(platform, PCM_CAPTURE);
394 app_type_cfg[length++] = 48000;
395 app_type_cfg[length++] = 16;
396 num_app_types += 1;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700397 }
398
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700399 list_for_each(node, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530400 s_info = node_to_item(node, struct streams_io_cfg, list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700401 update = true;
402 for (i=0; i<length; i=i+3) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530403 if (app_type_cfg[i+1] == 0)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700404 break;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530405 else if (app_type_cfg[i+1] == (size_t)s_info->app_type_cfg.app_type) {
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530406 if (app_type_cfg[i+2] < (size_t)s_info->app_type_cfg.sample_rate)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530407 app_type_cfg[i+2] = s_info->app_type_cfg.sample_rate;
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530408 if (app_type_cfg[i+3] < (size_t)s_info->app_type_cfg.bit_width)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530409 app_type_cfg[i+3] = s_info->app_type_cfg.bit_width;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700410 update = false;
411 break;
412 }
413 }
414 if (update && ((length + 3) <= MAX_LENGTH_MIXER_CONTROL_IN_INT)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530415 num_app_types += 1;
416 app_type_cfg[length++] = s_info->app_type_cfg.app_type;
417 app_type_cfg[length++] = s_info->app_type_cfg.sample_rate;
418 app_type_cfg[length++] = s_info->app_type_cfg.bit_width;
419 }
420 }
421 list_for_each(node, streams_input_cfg_list) {
422 s_info = node_to_item(node, struct streams_io_cfg, list);
423 update = true;
424 for (i=0; i<length; i=i+3) {
425 if (app_type_cfg[i+1] == 0)
426 break;
427 else if (app_type_cfg[i+1] == (size_t)s_info->app_type_cfg.app_type) {
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530428 if (app_type_cfg[i+2] < (size_t)s_info->app_type_cfg.sample_rate)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530429 app_type_cfg[i+2] = s_info->app_type_cfg.sample_rate;
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530430 if (app_type_cfg[i+3] < (size_t)s_info->app_type_cfg.bit_width)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530431 app_type_cfg[i+3] = s_info->app_type_cfg.bit_width;
432 update = false;
433 break;
434 }
435 }
436 if (update && ((length + 3) <= MAX_LENGTH_MIXER_CONTROL_IN_INT)) {
437 num_app_types += 1;
438 app_type_cfg[length++] = s_info->app_type_cfg.app_type;
439 app_type_cfg[length++] = s_info->app_type_cfg.sample_rate;
440 app_type_cfg[length++] = s_info->app_type_cfg.bit_width;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700441 }
442 }
443 ALOGV("%s: num_app_types: %d", __func__, num_app_types);
444 if (num_app_types) {
445 app_type_cfg[0] = num_app_types;
446 mixer_ctl_set_array(ctl, app_type_cfg, length);
447 }
448}
449
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530450void audio_extn_utils_update_streams_cfg_lists(void *platform,
451 struct mixer *mixer,
452 struct listnode *streams_output_cfg_list,
453 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700454{
455 cnode *root;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530456 char *data = NULL;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700457
458 ALOGV("%s", __func__);
459 list_init(streams_output_cfg_list);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530460 list_init(streams_input_cfg_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700461
462 root = config_node("", "");
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700463 if (root == NULL) {
464 ALOGE("cfg_list, NULL config root");
465 return;
466 }
467
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530468 data = (char *)load_file(AUDIO_IO_POLICY_VENDOR_CONFIG_FILE, NULL);
469 if (data == NULL) {
470 ALOGD("%s: failed to open io config file(%s), trying older config file",
471 __func__, AUDIO_IO_POLICY_VENDOR_CONFIG_FILE);
472 data = (char *)load_file(AUDIO_OUTPUT_POLICY_VENDOR_CONFIG_FILE, NULL);
473 if (data == NULL) {
474 send_app_type_cfg(platform, mixer,
475 streams_output_cfg_list,
476 streams_input_cfg_list);
477 ALOGE("%s: could not load io policy config!", __func__);
yidongh6eb4f752017-04-19 18:20:57 +0800478 free(root);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530479 return;
480 }
481 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700482
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530483 config_load(root, data);
484 load_cfg_list(root, platform, streams_output_cfg_list,
485 streams_input_cfg_list);
486
487 send_app_type_cfg(platform, mixer, streams_output_cfg_list,
488 streams_input_cfg_list);
Alexy Josephaee4fdd2016-01-29 13:02:07 -0800489
490 config_free(root);
yidongh6eb4f752017-04-19 18:20:57 +0800491 free(root);
Alexy Josephaee4fdd2016-01-29 13:02:07 -0800492 free(data);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700493}
494
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530495static void audio_extn_utils_dump_streams_cfg_list(
496 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700497{
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700498 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530499 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700500 struct stream_format *sf_info;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700501 struct stream_sample_rate *ss_info;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530502
503 list_for_each(node_i, streams_cfg_list) {
504 s_info = node_to_item(node_i, struct streams_io_cfg, list);
505 ALOGV("%s: flags-%d, sample_rate-%d, bit_width-%d, app_type-%d",
506 __func__, s_info->flags.out_flags, s_info->app_type_cfg.sample_rate,
507 s_info->app_type_cfg.bit_width, s_info->app_type_cfg.app_type);
508 list_for_each(node_j, &s_info->format_list) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700509 sf_info = node_to_item(node_j, struct stream_format, list);
510 ALOGV("format-%x", sf_info->format);
511 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530512 list_for_each(node_j, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700513 ss_info = node_to_item(node_j, struct stream_sample_rate, list);
514 ALOGV("sample rate-%d", ss_info->sample_rate);
515 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700516 }
517}
518
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530519void audio_extn_utils_dump_streams_cfg_lists(
520 struct listnode *streams_output_cfg_list,
521 struct listnode *streams_input_cfg_list)
522{
523 ALOGV("%s", __func__);
524 audio_extn_utils_dump_streams_cfg_list(streams_output_cfg_list);
525 audio_extn_utils_dump_streams_cfg_list(streams_input_cfg_list);
526}
527
528static void audio_extn_utils_release_streams_cfg_list(
529 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700530{
531 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530532 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700533
534 ALOGV("%s", __func__);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530535
536 while (!list_empty(streams_cfg_list)) {
537 node_i = list_head(streams_cfg_list);
538 s_info = node_to_item(node_i, struct streams_io_cfg, list);
539 while (!list_empty(&s_info->format_list)) {
540 node_j = list_head(&s_info->format_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700541 list_remove(node_j);
542 free(node_to_item(node_j, struct stream_format, list));
543 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530544 while (!list_empty(&s_info->sample_rate_list)) {
545 node_j = list_head(&s_info->sample_rate_list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700546 list_remove(node_j);
547 free(node_to_item(node_j, struct stream_sample_rate, list));
548 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700549 list_remove(node_i);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530550 free(node_to_item(node_i, struct streams_io_cfg, list));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700551 }
552}
553
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530554void audio_extn_utils_release_streams_cfg_lists(
555 struct listnode *streams_output_cfg_list,
556 struct listnode *streams_input_cfg_list)
557{
558 ALOGV("%s", __func__);
559 audio_extn_utils_release_streams_cfg_list(streams_output_cfg_list);
560 audio_extn_utils_release_streams_cfg_list(streams_input_cfg_list);
561}
562
563static bool set_app_type_cfg(struct streams_io_cfg *s_info,
564 struct stream_app_type_cfg *app_type_cfg,
565 uint32_t sample_rate, uint32_t bit_width)
Amit Shekhar6f461b12014-08-01 14:52:58 -0700566 {
567 struct listnode *node_i;
568 struct stream_sample_rate *ss_info;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530569 list_for_each(node_i, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700570 ss_info = node_to_item(node_i, struct stream_sample_rate, list);
571 if ((sample_rate <= ss_info->sample_rate) &&
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530572 (bit_width == s_info->app_type_cfg.bit_width)) {
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700573
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530574 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700575 app_type_cfg->sample_rate = ss_info->sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530576 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700577 ALOGV("%s app_type_cfg->app_type %d, app_type_cfg->sample_rate %d, app_type_cfg->bit_width %d",
578 __func__, app_type_cfg->app_type, app_type_cfg->sample_rate, app_type_cfg->bit_width);
579 return true;
580 }
581 }
582 /*
583 * Reiterate through the list assuming dafault sample rate.
584 * Handles scenario where input sample rate is higher
585 * than all sample rates in list for the input bit width.
586 */
587 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
Apoorv Raghuvanshif59bb222015-02-18 12:23:23 -0800588
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530589 list_for_each(node_i, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700590 ss_info = node_to_item(node_i, struct stream_sample_rate, list);
591 if ((sample_rate <= ss_info->sample_rate) &&
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530592 (bit_width == s_info->app_type_cfg.bit_width)) {
593 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700594 app_type_cfg->sample_rate = sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530595 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Apoorv Raghuvanshif59bb222015-02-18 12:23:23 -0800596 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 -0700597 __func__, app_type_cfg->app_type, app_type_cfg->sample_rate, app_type_cfg->bit_width);
598 return true;
599 }
600 }
601 return false;
602}
603
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530604void audio_extn_utils_update_stream_input_app_type_cfg(void *platform,
605 struct listnode *streams_input_cfg_list,
606 audio_devices_t devices __unused,
607 audio_input_flags_t flags,
608 audio_format_t format,
609 uint32_t sample_rate,
610 uint32_t bit_width,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530611 char* profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530612 struct stream_app_type_cfg *app_type_cfg)
613{
614 struct listnode *node_i, *node_j;
615 struct streams_io_cfg *s_info;
616 struct stream_format *sf_info;
617
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530618 ALOGV("%s: flags: 0x%x, format: 0x%x sample_rate %d, profile %s",
619 __func__, flags, format, sample_rate, profile);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530620
621 list_for_each(node_i, streams_input_cfg_list) {
622 s_info = node_to_item(node_i, struct streams_io_cfg, list);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530623 /* Along with flags do profile matching if set at either end.*/
624 if (s_info->flags.in_flags == flags &&
625 ((profile[0] == '\0' && s_info->profile[0] == '\0') ||
626 strncmp(s_info->profile, profile, sizeof(s_info->profile)) == 0)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530627 list_for_each(node_j, &s_info->format_list) {
628 sf_info = node_to_item(node_j, struct stream_format, list);
629 if (sf_info->format == format) {
630 if (set_app_type_cfg(s_info, app_type_cfg, sample_rate, bit_width))
631 return;
632 }
633 }
634 }
635 }
636 ALOGW("%s: App type could not be selected. Falling back to default", __func__);
637 app_type_cfg->app_type = platform_get_default_app_type_v2(platform, PCM_CAPTURE);
638 app_type_cfg->sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
639 app_type_cfg->bit_width = 16;
640}
641
642void audio_extn_utils_update_stream_output_app_type_cfg(void *platform,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700643 struct listnode *streams_output_cfg_list,
Amit Shekhar1d896042014-10-03 13:16:09 -0700644 audio_devices_t devices,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700645 audio_output_flags_t flags,
646 audio_format_t format,
Amit Shekhar6f461b12014-08-01 14:52:58 -0700647 uint32_t sample_rate,
648 uint32_t bit_width,
Manish Dewangan837dc462015-05-27 10:17:41 +0530649 audio_channel_mask_t channel_mask,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530650 char *profile,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700651 struct stream_app_type_cfg *app_type_cfg)
652{
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +0530653 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530654 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700655 struct stream_format *sf_info;
Manish Dewangan837dc462015-05-27 10:17:41 +0530656 char value[PROPERTY_VALUE_MAX] = {0};
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700657
Ashish Jain058165c2016-09-28 23:18:48 +0530658 if ((bit_width >= 24) &&
Amit Shekhar1d896042014-10-03 13:16:09 -0700659 (devices & AUDIO_DEVICE_OUT_SPEAKER)) {
Amit Shekhar5a39c912014-10-14 15:39:30 -0700660 int32_t bw = platform_get_snd_device_bit_width(SND_DEVICE_OUT_SPEAKER);
661 if (-ENOSYS != bw)
662 bit_width = (uint32_t)bw;
Amit Shekhar1d896042014-10-03 13:16:09 -0700663 sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
664 ALOGI("%s Allowing 24-bit playback on speaker ONLY at default sampling rate", __func__);
665 }
666
Manish Dewangan837dc462015-05-27 10:17:41 +0530667 property_get("audio.playback.mch.downsample",value,"");
668 if (!strncmp("true", value, sizeof("true"))) {
669 if ((popcount(channel_mask) > 2) &&
670 (sample_rate > CODEC_BACKEND_DEFAULT_SAMPLE_RATE) &&
671 !(flags & AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH)) {
672 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
673 ALOGD("%s: MCH session defaulting sample rate to %d",
674 __func__, sample_rate);
675 }
676 }
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +0530677
678 /* Set sampling rate to 176.4 for DSD64
679 * and 352.8Khz for DSD128.
680 * Set Bit Width to 16. output will be 16 bit
681 * post DoP in ASM.
682 */
683 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH) &&
684 (format == AUDIO_FORMAT_DSD)) {
685 bit_width = 16;
686 if (sample_rate == INPUT_SAMPLING_RATE_DSD64)
687 sample_rate = OUTPUT_SAMPLING_RATE_DSD64;
688 else if (sample_rate == INPUT_SAMPLING_RATE_DSD128)
689 sample_rate = OUTPUT_SAMPLING_RATE_DSD128;
690 }
691
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +0530692 if(devices & AUDIO_DEVICE_OUT_ALL_A2DP) {
693 //TODO: Handle fractional sampling rate configuration for LL
694 audio_extn_a2dp_get_apptype_params(&sample_rate, &bit_width);
695 ALOGI("%s using %d sampling rate %d bit width for A2DP CoPP",
696 __func__, sample_rate, bit_width);
697 }
698
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800699 ALOGV("%s: flags: %x, format: %x sample_rate %d, profile %s, app_type %d",
700 __func__, flags, format, sample_rate, profile, app_type_cfg->app_type);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700701 list_for_each(node_i, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530702 s_info = node_to_item(node_i, struct streams_io_cfg, list);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530703 /* Along with flags do profile matching if set at either end.*/
704 if (s_info->flags.out_flags == flags &&
705 ((profile[0] == '\0' && s_info->profile[0] == '\0') ||
706 strncmp(s_info->profile, profile, sizeof(s_info->profile)) == 0)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530707 list_for_each(node_j, &s_info->format_list) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700708 sf_info = node_to_item(node_j, struct stream_format, list);
709 if (sf_info->format == format) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530710 if (set_app_type_cfg(s_info, app_type_cfg, sample_rate, bit_width))
Amit Shekhar6f461b12014-08-01 14:52:58 -0700711 return;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700712 }
713 }
714 }
715 }
716 list_for_each(node_i, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530717 s_info = node_to_item(node_i, struct streams_io_cfg, list);
718 if (s_info->flags.out_flags == AUDIO_OUTPUT_FLAG_PRIMARY) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700719 ALOGV("Compatible output profile not found.");
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530720 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
721 app_type_cfg->sample_rate = s_info->app_type_cfg.sample_rate;
722 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700723 ALOGV("%s Default to primary output: App type: %d sample_rate %d",
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530724 __func__, s_info->app_type_cfg.app_type, app_type_cfg->sample_rate);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700725 return;
726 }
727 }
728 ALOGW("%s: App type could not be selected. Falling back to default", __func__);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700729 app_type_cfg->app_type = platform_get_default_app_type(platform);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700730 app_type_cfg->sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700731 app_type_cfg->bit_width = 16;
732}
733
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +0530734static bool audio_is_this_native_usecase(struct audio_usecase *uc)
735{
736 bool native_usecase = false;
737 struct stream_out *out = (struct stream_out*) uc->stream.out;
738
739 if (PCM_PLAYBACK == uc->type && out != NULL &&
740 NATIVE_AUDIO_MODE_INVALID != platform_get_native_support() &&
741 is_offload_usecase(uc->id) &&
742 (out->sample_rate == OUTPUT_SAMPLING_RATE_44100))
743 native_usecase = true;
744
745 return native_usecase;
746}
747
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800748
749static inline bool audio_is_vr_mode_on(struct audio_device *(__attribute__((unused)) adev))
750{
751 return adev->vr_audio_mode_enabled;
752}
753
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530754void audio_extn_utils_update_stream_app_type_cfg_for_usecase(
755 struct audio_device *adev,
756 struct audio_usecase *usecase)
757{
758 ALOGV("%s", __func__);
759
760 switch(usecase->type) {
761 case PCM_PLAYBACK:
762 audio_extn_utils_update_stream_output_app_type_cfg(adev->platform,
763 &adev->streams_output_cfg_list,
764 usecase->stream.out->devices,
765 usecase->stream.out->flags,
766 usecase->stream.out->format,
767 usecase->stream.out->sample_rate,
768 usecase->stream.out->bit_width,
769 usecase->stream.out->channel_mask,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530770 usecase->stream.out->profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530771 &usecase->stream.out->app_type_cfg);
772 ALOGV("%s Selected apptype: %d", __func__, usecase->stream.out->app_type_cfg.app_type);
773 break;
774 case PCM_CAPTURE:
775 audio_extn_utils_update_stream_input_app_type_cfg(adev->platform,
776 &adev->streams_input_cfg_list,
777 usecase->stream.in->device,
778 usecase->stream.in->flags,
779 usecase->stream.in->format,
780 usecase->stream.in->sample_rate,
781 usecase->stream.in->bit_width,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530782 usecase->stream.in->profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530783 &usecase->stream.in->app_type_cfg);
784 ALOGV("%s Selected apptype: %d", __func__, usecase->stream.in->app_type_cfg.app_type);
785 break;
786 default:
787 ALOGE("%s: app type cfg not supported for usecase type (%d)",
788 __func__, usecase->type);
789 }
790}
791
Siena Richard7c2db772016-12-21 11:32:34 -0800792static int send_app_type_cfg_for_device(struct audio_device *adev,
793 struct audio_usecase *usecase,
794 int split_snd_device)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700795{
796 char mixer_ctl_name[MAX_LENGTH_MIXER_CONTROL_IN_INT];
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530797 size_t app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT] = {0};
798 int len = 0, rc;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700799 struct mixer_ctl *ctl;
Siena Richard7c2db772016-12-21 11:32:34 -0800800 int pcm_device_id = 0, acdb_dev_id, app_type;
801 int snd_device = split_snd_device, snd_device_be_idx = -1;
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +0530802 int32_t sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Manish Dewangan837dc462015-05-27 10:17:41 +0530803 char value[PROPERTY_VALUE_MAX] = {0};
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700804
Siena Richard7c2db772016-12-21 11:32:34 -0800805 ALOGV("%s: usecase->out_snd_device %s, usecase->in_snd_device %s, split_snd_device %s",
806 __func__, platform_get_snd_device_name(usecase->out_snd_device),
807 platform_get_snd_device_name(usecase->in_snd_device),
808 platform_get_snd_device_name(split_snd_device));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700809
Srikanth Uyyala9d551402015-08-25 16:03:42 +0530810 if (usecase->type != PCM_PLAYBACK && usecase->type != PCM_CAPTURE) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530811 ALOGE("%s: not a playback/capture path, no need to cfg app type", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700812 rc = 0;
813 goto exit_send_app_type_cfg;
814 }
815 if ((usecase->id != USECASE_AUDIO_PLAYBACK_DEEP_BUFFER) &&
816 (usecase->id != USECASE_AUDIO_PLAYBACK_LOW_LATENCY) &&
817 (usecase->id != USECASE_AUDIO_PLAYBACK_MULTI_CH) &&
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800818 (usecase->id != USECASE_AUDIO_PLAYBACK_ULL) &&
Srikanth Uyyala9d551402015-08-25 16:03:42 +0530819 (!is_offload_usecase(usecase->id)) &&
820 (usecase->type != PCM_CAPTURE)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530821 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 -0700822 rc = 0;
823 goto exit_send_app_type_cfg;
824 }
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800825
826 //if VR is active then only send the mixer control
827 if (usecase->id == USECASE_AUDIO_PLAYBACK_ULL && !audio_is_vr_mode_on(adev)) {
828 ALOGI("ULL doesnt need sending app type cfg, returning");
829 rc = 0;
830 goto exit_send_app_type_cfg;
831 }
832
Srikanth Uyyala9d551402015-08-25 16:03:42 +0530833 if (usecase->type == PCM_PLAYBACK) {
Ben Romberger1fafdde2015-09-09 19:43:15 -0700834 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_PLAYBACK);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530835 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
836 "Audio Stream %d App Type Cfg", pcm_device_id);
Ben Romberger1fafdde2015-09-09 19:43:15 -0700837 } else if (usecase->type == PCM_CAPTURE) {
Ben Romberger1fafdde2015-09-09 19:43:15 -0700838 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_CAPTURE);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530839 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
840 "Audio Stream Capture %d App Type Cfg", pcm_device_id);
Srikanth Uyyala9d551402015-08-25 16:03:42 +0530841 }
Siena Richard7c2db772016-12-21 11:32:34 -0800842
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700843 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
844 if (!ctl) {
845 ALOGE("%s: Could not get ctl for mixer cmd - %s", __func__,
846 mixer_ctl_name);
847 rc = -EINVAL;
848 goto exit_send_app_type_cfg;
849 }
Aditya Bavanari701a6992017-03-30 19:17:16 +0530850 snd_device = platform_get_spkr_prot_snd_device(snd_device);
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +0530851
Rohit Kumar1181b292017-01-31 18:07:17 +0530852 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
853 if (acdb_dev_id <= 0) {
854 ALOGE("%s: Couldn't get the acdb dev id", __func__);
855 rc = -EINVAL;
856 goto exit_send_app_type_cfg;
857 }
858
Siena Richard7c2db772016-12-21 11:32:34 -0800859 snd_device_be_idx = platform_get_snd_device_backend_index(snd_device);
860 if (snd_device_be_idx < 0) {
861 ALOGE("%s: Couldn't get the backend index for snd device %s ret=%d",
862 __func__, platform_get_snd_device_name(snd_device),
863 snd_device_be_idx);
864 }
865
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530866 if ((usecase->type == PCM_PLAYBACK) && (usecase->stream.out != NULL)) {
Manish Dewangan837dc462015-05-27 10:17:41 +0530867
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530868 property_get("audio.playback.mch.downsample",value,"");
869 if (!strncmp("true", value, sizeof("true"))) {
870 if ((popcount(usecase->stream.out->channel_mask) > 2) &&
871 (usecase->stream.out->app_type_cfg.sample_rate > CODEC_BACKEND_DEFAULT_SAMPLE_RATE) &&
872 !(usecase->stream.out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH))
873 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
874 }
Ashish Jainc02430d2016-01-04 10:42:43 +0530875
Ashish Jain4826f6c2017-02-06 13:33:20 +0530876 if (usecase->stream.out->devices & AUDIO_DEVICE_OUT_SPEAKER) {
Mingming Yin21854652016-04-13 11:54:02 -0700877 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Ashish Jaina052e572016-11-07 16:41:07 +0530878 } else if ((snd_device == SND_DEVICE_OUT_HDMI ||
879 snd_device == SND_DEVICE_OUT_USB_HEADSET ||
880 snd_device == SND_DEVICE_OUT_DISPLAY_PORT) &&
881 (usecase->stream.out->sample_rate >= OUTPUT_SAMPLING_RATE_44100)) {
882 /*
883 * To best utlize DSP, check if the stream sample rate is supported/multiple of
884 * configured device sample rate, if not update the COPP rate to be equal to the
885 * device sample rate, else open COPP at stream sample rate
886 */
887 platform_check_and_update_copp_sample_rate(adev->platform, snd_device,
888 usecase->stream.out->sample_rate,
889 &usecase->stream.out->app_type_cfg.sample_rate);
Ashish Jain4826f6c2017-02-06 13:33:20 +0530890 } else if (((snd_device != SND_DEVICE_OUT_HEADPHONES_44_1 &&
891 !audio_is_this_native_usecase(usecase)) &&
Mingming Yin21854652016-04-13 11:54:02 -0700892 usecase->stream.out->sample_rate == OUTPUT_SAMPLING_RATE_44100) ||
893 (usecase->stream.out->sample_rate < OUTPUT_SAMPLING_RATE_44100)) {
Ashish Jain4826f6c2017-02-06 13:33:20 +0530894 /* Reset to default if no native stream is active*/
Mingming Yin21854652016-04-13 11:54:02 -0700895 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
896 }
897 sample_rate = usecase->stream.out->app_type_cfg.sample_rate;
898
Siena Richard7c2db772016-12-21 11:32:34 -0800899 app_type = usecase->stream.out->app_type_cfg.app_type;
900 app_type_cfg[len++] = app_type;
Mingming Yin21854652016-04-13 11:54:02 -0700901 app_type_cfg[len++] = acdb_dev_id;
902 if (((usecase->stream.out->format == AUDIO_FORMAT_E_AC3) ||
903 (usecase->stream.out->format == AUDIO_FORMAT_E_AC3_JOC))
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530904 && audio_extn_passthru_is_passthrough_stream(usecase->stream.out)) {
Mingming Yin21854652016-04-13 11:54:02 -0700905 app_type_cfg[len++] = sample_rate * 4;
906 } else {
907 app_type_cfg[len++] = sample_rate;
908 }
Siena Richard7c2db772016-12-21 11:32:34 -0800909 if (snd_device_be_idx > 0)
910 app_type_cfg[len++] = snd_device_be_idx;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530911
Siena Richard7c2db772016-12-21 11:32:34 -0800912 ALOGI("%s PLAYBACK app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
913 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530914
915 } else if ((usecase->type == PCM_CAPTURE) && (usecase->stream.in != NULL)) {
Siena Richard7c2db772016-12-21 11:32:34 -0800916 app_type = usecase->stream.in->app_type_cfg.app_type;
917 app_type_cfg[len++] = app_type;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530918 app_type_cfg[len++] = acdb_dev_id;
Siena Richard7c2db772016-12-21 11:32:34 -0800919 sample_rate = usecase->stream.in->app_type_cfg.sample_rate;
920 app_type_cfg[len++] = sample_rate;
921 if (snd_device_be_idx > 0)
922 app_type_cfg[len++] = snd_device_be_idx;
923 ALOGI("%s CAPTURE app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
924 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530925 } else {
Siena Richard7c2db772016-12-21 11:32:34 -0800926 app_type = platform_get_default_app_type_v2(adev->platform, usecase->type);
927 app_type_cfg[len++] = app_type;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530928 app_type_cfg[len++] = acdb_dev_id;
929 app_type_cfg[len++] = sample_rate;
Siena Richard7c2db772016-12-21 11:32:34 -0800930 if (snd_device_be_idx > 0)
931 app_type_cfg[len++] = snd_device_be_idx;
932 ALOGI("%s default app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
933 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Ashish Jainc02430d2016-01-04 10:42:43 +0530934 }
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +0530935
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700936 mixer_ctl_set_array(ctl, app_type_cfg, len);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700937 rc = 0;
938exit_send_app_type_cfg:
939 return rc;
940}
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700941
Siena Richard7c2db772016-12-21 11:32:34 -0800942int audio_extn_utils_send_app_type_cfg(struct audio_device *adev,
943 struct audio_usecase *usecase)
944{
945 int i, num_devices = 0;
946 snd_device_t new_snd_devices[SND_DEVICE_OUT_END] = {0};
947 int rc = 0;
948
949 switch (usecase->type) {
950 case PCM_PLAYBACK:
951 ALOGD("%s: usecase->out_snd_device %s",
952 __func__, platform_get_snd_device_name(usecase->out_snd_device));
953 /* check for out combo device */
954 if (platform_split_snd_device(adev->platform,
955 usecase->out_snd_device,
956 &num_devices, new_snd_devices)) {
957 new_snd_devices[0] = usecase->out_snd_device;
958 num_devices = 1;
959 }
960 break;
961 case PCM_CAPTURE:
962 ALOGD("%s: usecase->in_snd_device %s",
963 __func__, platform_get_snd_device_name(usecase->in_snd_device));
964 /* check for in combo device */
965 if (platform_split_snd_device(adev->platform,
966 usecase->in_snd_device,
967 &num_devices, new_snd_devices)) {
968 new_snd_devices[0] = usecase->in_snd_device;
969 num_devices = 1;
970 }
971 break;
972 default:
973 ALOGI("%s: not a playback/capture path, no need to cfg app type", __func__);
974 rc = 0;
975 break;
976 }
977
978 for (i = 0; i < num_devices; i++) {
979 rc = send_app_type_cfg_for_device(adev, usecase, new_snd_devices[i]);
980 if (rc)
981 break;
982 }
983
984 return rc;
985}
986
Preetam Singh Ranawat9519e9c2015-11-18 16:05:55 +0530987int read_line_from_file(const char *path, char *buf, size_t count)
988{
989 char * fgets_ret;
990 FILE * fd;
991 int rv;
992
993 fd = fopen(path, "r");
994 if (fd == NULL)
995 return -1;
996
997 fgets_ret = fgets(buf, (int)count, fd);
998 if (NULL != fgets_ret) {
999 rv = (int)strlen(buf);
1000 } else {
1001 rv = ferror(fd);
1002 }
1003 fclose(fd);
1004
1005 return rv;
1006}
1007
Ashish Jainf1eaa582016-05-23 20:54:24 +05301008/*Translates ALSA formats to AOSP PCM formats*/
1009audio_format_t alsa_format_to_hal(uint32_t alsa_format)
1010{
1011 audio_format_t format;
1012
1013 switch(alsa_format) {
1014 case SNDRV_PCM_FORMAT_S16_LE:
1015 format = AUDIO_FORMAT_PCM_16_BIT;
1016 break;
1017 case SNDRV_PCM_FORMAT_S24_3LE:
1018 format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
1019 break;
1020 case SNDRV_PCM_FORMAT_S24_LE:
1021 format = AUDIO_FORMAT_PCM_8_24_BIT;
1022 break;
1023 case SNDRV_PCM_FORMAT_S32_LE:
1024 format = AUDIO_FORMAT_PCM_32_BIT;
1025 break;
1026 default:
1027 ALOGW("Incorrect ALSA format");
1028 format = AUDIO_FORMAT_INVALID;
1029 }
1030 return format;
1031}
1032
1033/*Translates hal format (AOSP) to alsa formats*/
1034uint32_t hal_format_to_alsa(audio_format_t hal_format)
1035{
1036 uint32_t alsa_format;
1037
1038 switch (hal_format) {
1039 case AUDIO_FORMAT_PCM_32_BIT: {
1040 if (platform_supports_true_32bit())
1041 alsa_format = SNDRV_PCM_FORMAT_S32_LE;
1042 else
1043 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1044 }
1045 break;
1046 case AUDIO_FORMAT_PCM_8_BIT:
1047 alsa_format = SNDRV_PCM_FORMAT_S8;
1048 break;
1049 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
1050 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1051 break;
1052 case AUDIO_FORMAT_PCM_8_24_BIT: {
1053 if (platform_supports_true_32bit())
1054 alsa_format = SNDRV_PCM_FORMAT_S32_LE;
1055 else
1056 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1057 }
1058 break;
1059 case AUDIO_FORMAT_PCM_FLOAT:
1060 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1061 break;
1062 default:
1063 case AUDIO_FORMAT_PCM_16_BIT:
1064 alsa_format = SNDRV_PCM_FORMAT_S16_LE;
1065 break;
1066 }
1067 return alsa_format;
1068}
1069
Ashish Jain83a6cc22016-06-28 14:34:17 +05301070/*Translates PCM formats to AOSP formats*/
1071audio_format_t pcm_format_to_hal(uint32_t pcm_format)
1072{
1073 audio_format_t format = AUDIO_FORMAT_INVALID;
1074
1075 switch(pcm_format) {
1076 case PCM_FORMAT_S16_LE:
1077 format = AUDIO_FORMAT_PCM_16_BIT;
1078 break;
1079 case PCM_FORMAT_S24_3LE:
1080 format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
1081 break;
1082 case PCM_FORMAT_S24_LE:
1083 format = AUDIO_FORMAT_PCM_8_24_BIT;
1084 break;
1085 case PCM_FORMAT_S32_LE:
1086 format = AUDIO_FORMAT_PCM_32_BIT;
1087 break;
1088 default:
1089 ALOGW("Incorrect PCM format");
1090 format = AUDIO_FORMAT_INVALID;
1091 }
1092 return format;
1093}
1094
1095/*Translates hal format (AOSP) to alsa formats*/
1096uint32_t hal_format_to_pcm(audio_format_t hal_format)
1097{
1098 uint32_t pcm_format;
1099
1100 switch (hal_format) {
1101 case AUDIO_FORMAT_PCM_32_BIT:
1102 case AUDIO_FORMAT_PCM_8_24_BIT:
1103 case AUDIO_FORMAT_PCM_FLOAT: {
1104 if (platform_supports_true_32bit())
1105 pcm_format = PCM_FORMAT_S32_LE;
1106 else
1107 pcm_format = PCM_FORMAT_S24_3LE;
1108 }
1109 break;
1110 case AUDIO_FORMAT_PCM_8_BIT:
1111 pcm_format = PCM_FORMAT_S8;
1112 break;
1113 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
1114 pcm_format = PCM_FORMAT_S24_3LE;
1115 break;
1116 default:
1117 case AUDIO_FORMAT_PCM_16_BIT:
1118 pcm_format = PCM_FORMAT_S16_LE;
1119 break;
1120 }
1121 return pcm_format;
1122}
1123
Ashish Jainf1eaa582016-05-23 20:54:24 +05301124uint32_t get_alsa_fragment_size(uint32_t bytes_per_sample,
1125 uint32_t sample_rate,
1126 uint32_t noOfChannels)
1127{
1128 uint32_t fragment_size = 0;
1129 uint32_t pcm_offload_time = PCM_OFFLOAD_BUFFER_DURATION;
1130
1131 fragment_size = (pcm_offload_time
1132 * sample_rate
1133 * bytes_per_sample
1134 * noOfChannels)/1000;
1135 if (fragment_size < MIN_PCM_OFFLOAD_FRAGMENT_SIZE)
1136 fragment_size = MIN_PCM_OFFLOAD_FRAGMENT_SIZE;
1137 else if (fragment_size > MAX_PCM_OFFLOAD_FRAGMENT_SIZE)
1138 fragment_size = MAX_PCM_OFFLOAD_FRAGMENT_SIZE;
1139 /*To have same PCM samples for all channels, the buffer size requires to
1140 *be multiple of (number of channels * bytes per sample)
1141 *For writes to succeed, the buffer must be written at address which is multiple of 32
1142 */
1143 fragment_size = ALIGN(fragment_size, (bytes_per_sample * noOfChannels * 32));
1144
1145 ALOGI("PCM offload Fragment size to %d bytes", fragment_size);
1146 return fragment_size;
1147}
1148
1149/* Calculates the fragment size required to configure compress session.
1150 * Based on the alsa format selected, decide if conversion is needed in
1151
1152 * HAL ( e.g. convert AUDIO_FORMAT_PCM_FLOAT input format to
1153 * AUDIO_FORMAT_PCM_24_BIT_PACKED before writing to the compress driver.
1154 */
1155void audio_extn_utils_update_direct_pcm_fragment_size(struct stream_out *out)
1156{
Ashish Jain83a6cc22016-06-28 14:34:17 +05301157 audio_format_t dst_format = out->hal_op_format;
1158 audio_format_t src_format = out->hal_ip_format;
Ashish Jainf1eaa582016-05-23 20:54:24 +05301159 uint32_t hal_op_bytes_per_sample = audio_bytes_per_sample(dst_format);
1160 uint32_t hal_ip_bytes_per_sample = audio_bytes_per_sample(src_format);
1161
1162 out->compr_config.fragment_size =
1163 get_alsa_fragment_size(hal_op_bytes_per_sample,
1164 out->sample_rate,
1165 popcount(out->channel_mask));
1166
1167 if ((src_format != dst_format) &&
1168 hal_op_bytes_per_sample != hal_ip_bytes_per_sample) {
1169
Ashish Jain83a6cc22016-06-28 14:34:17 +05301170 out->hal_fragment_size =
Ashish Jainf1eaa582016-05-23 20:54:24 +05301171 ((out->compr_config.fragment_size * hal_ip_bytes_per_sample) /
1172 hal_op_bytes_per_sample);
1173 ALOGI("enable conversion hal_input_fragment_size is %d src_format %x dst_format %x",
Ashish Jain83a6cc22016-06-28 14:34:17 +05301174 out->hal_fragment_size, src_format, dst_format);
Ashish Jainf1eaa582016-05-23 20:54:24 +05301175 } else {
Ashish Jain83a6cc22016-06-28 14:34:17 +05301176 out->hal_fragment_size = out->compr_config.fragment_size;
Ashish Jainf1eaa582016-05-23 20:54:24 +05301177 }
1178}
1179
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301180/* converts pcm format 24_8 to 8_24 inplace */
1181size_t audio_extn_utils_convert_format_24_8_to_8_24(void *buf, size_t bytes)
1182{
1183 size_t i = 0;
1184 int *int_buf_stream = buf;
1185
1186 if ((bytes % 4) != 0) {
1187 ALOGE("%s: wrong inout buffer! ... is not 32 bit aligned ", __func__);
1188 return -EINVAL;
1189 }
1190
1191 for (; i < (bytes / 4); i++)
1192 int_buf_stream[i] >>= 8;
1193
1194 return bytes;
1195}
1196
1197int get_snd_codec_id(audio_format_t format)
1198{
1199 int id = 0;
1200
1201 switch (format & AUDIO_FORMAT_MAIN_MASK) {
1202 case AUDIO_FORMAT_MP3:
1203 id = SND_AUDIOCODEC_MP3;
1204 break;
1205 case AUDIO_FORMAT_AAC:
1206 id = SND_AUDIOCODEC_AAC;
1207 break;
1208 case AUDIO_FORMAT_AAC_ADTS:
1209 id = SND_AUDIOCODEC_AAC;
1210 break;
Arun Kumar Dasari3b174182016-12-27 13:01:14 +05301211 case AUDIO_FORMAT_AAC_LATM:
1212 id = SND_AUDIOCODEC_AAC;
1213 break;
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301214 case AUDIO_FORMAT_PCM:
1215 id = SND_AUDIOCODEC_PCM;
1216 break;
1217 case AUDIO_FORMAT_FLAC:
1218 id = SND_AUDIOCODEC_FLAC;
1219 break;
1220 case AUDIO_FORMAT_ALAC:
1221 id = SND_AUDIOCODEC_ALAC;
1222 break;
1223 case AUDIO_FORMAT_APE:
1224 id = SND_AUDIOCODEC_APE;
1225 break;
1226 case AUDIO_FORMAT_VORBIS:
1227 id = SND_AUDIOCODEC_VORBIS;
1228 break;
1229 case AUDIO_FORMAT_WMA:
1230 id = SND_AUDIOCODEC_WMA;
1231 break;
1232 case AUDIO_FORMAT_WMA_PRO:
1233 id = SND_AUDIOCODEC_WMA_PRO;
1234 break;
Satish Babu Patakokila0c313922016-12-08 12:07:08 +05301235 case AUDIO_FORMAT_MP2:
1236 id = SND_AUDIOCODEC_MP2;
1237 break;
Satish Babu Patakokila915ecba2017-01-10 17:43:56 +05301238 case AUDIO_FORMAT_AC3:
1239 id = SND_AUDIOCODEC_AC3;
1240 break;
1241 case AUDIO_FORMAT_E_AC3:
1242 case AUDIO_FORMAT_E_AC3_JOC:
1243 id = SND_AUDIOCODEC_EAC3;
1244 break;
1245 case AUDIO_FORMAT_DTS:
1246 case AUDIO_FORMAT_DTS_HD:
1247 id = SND_AUDIOCODEC_DTS;
1248 break;
Preetam Singh Ranawat4277a5a2017-01-18 19:02:24 +05301249 case AUDIO_FORMAT_DSD:
1250 id = SND_AUDIOCODEC_DSD;
1251 break;
Dhanalakshmi Siddani18737932016-11-29 17:33:17 +05301252 case AUDIO_FORMAT_APTX:
1253 id = SND_AUDIOCODEC_APTX;
1254 break;
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301255 default:
1256 ALOGE("%s: Unsupported audio format :%x", __func__, format);
1257 }
1258
1259 return id;
1260}
1261
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001262void audio_extn_utils_send_audio_calibration(struct audio_device *adev,
1263 struct audio_usecase *usecase)
1264{
1265 int type = usecase->type;
1266
Dhananjay Kumar14245982017-01-16 20:21:00 +05301267 if (type == PCM_PLAYBACK && usecase->stream.out != NULL) {
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001268 struct stream_out *out = usecase->stream.out;
1269 int snd_device = usecase->out_snd_device;
1270 snd_device = (snd_device == SND_DEVICE_OUT_SPEAKER) ?
Xiaojun Sang040cc9f2015-08-03 19:38:28 +08001271 platform_get_spkr_prot_snd_device(snd_device) : snd_device;
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05301272 platform_send_audio_calibration(adev->platform, usecase,
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001273 out->app_type_cfg.app_type,
Preetam Singh Ranawat61716b12015-12-14 11:55:24 +05301274 usecase->stream.out->app_type_cfg.sample_rate);
Dhananjay Kumar14245982017-01-16 20:21:00 +05301275 } else if (type == PCM_CAPTURE && usecase->stream.in != NULL) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301276 platform_send_audio_calibration(adev->platform, usecase,
1277 usecase->stream.in->app_type_cfg.app_type,
1278 usecase->stream.in->app_type_cfg.sample_rate);
Vidyakumar Athota653aaef2017-03-16 11:11:31 -07001279 } else if (type == PCM_HFP_CALL || type == PCM_CAPTURE) {
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05301280 /* when app type is default. the sample rate is not used to send cal */
1281 platform_send_audio_calibration(adev->platform, usecase,
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301282 platform_get_default_app_type_v2(adev->platform, usecase->type),
1283 48000);
Vidyakumar Athotae57f6002017-03-01 13:13:16 -08001284 } else {
1285 /* No need to send audio calibration for voice and voip call usecases */
1286 if ((type != VOICE_CALL) && (type != VOIP_CALL))
1287 ALOGW("%s: No audio calibration for usecase type = %d", __func__, type);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001288 }
1289}
1290
Ben Rombergera04fabc2014-11-14 12:16:03 -08001291// Base64 Encode and Decode
1292// Not all features supported. This must be used only with following conditions.
1293// Decode Modes: Support with and without padding
1294// CRLF not handling. So no CRLF in string to decode.
1295// Encode Modes: Supports only padding
1296int b64decode(char *inp, int ilen, uint8_t* outp)
1297{
1298 int i, j, k, ii, num;
1299 int rem, pcnt;
1300 uint32_t res=0;
1301 uint8_t getIndex[MAX_BASEINDEX_LEN];
1302 uint8_t tmp, cflag;
1303
1304 if(inp == NULL || outp == NULL || ilen <= 0) {
1305 ALOGE("[%s] received NULL pointer or zero length",__func__);
1306 return -1;
1307 }
1308
1309 memset(getIndex, MAX_BASEINDEX_LEN-1, sizeof(getIndex));
1310 for(i=0;i<BASE_TABLE_SIZE;i++) {
1311 getIndex[(uint8_t)bTable[i]] = (uint8_t)i;
1312 }
1313 getIndex[(uint8_t)'=']=0;
1314
1315 j=0;k=0;
1316 num = ilen/4;
1317 rem = ilen%4;
1318 if(rem==0)
1319 num = num-1;
1320 cflag=0;
1321 for(i=0; i<num; i++) {
1322 res=0;
1323 for(ii=0;ii<4;ii++) {
1324 res = res << 6;
1325 tmp = getIndex[(uint8_t)inp[j++]];
1326 res = res | tmp;
1327 cflag = cflag | tmp;
1328 }
1329 outp[k++] = (res >> 16)&0xFF;
1330 outp[k++] = (res >> 8)&0xFF;
1331 outp[k++] = res & 0xFF;
1332 }
1333
1334 // Handle last bytes special
1335 pcnt=0;
1336 if(rem == 0) {
1337 //With padding or full data
1338 res = 0;
1339 for(ii=0;ii<4;ii++) {
1340 if(inp[j] == '=')
1341 pcnt++;
1342 res = res << 6;
1343 tmp = getIndex[(uint8_t)inp[j++]];
1344 res = res | tmp;
1345 cflag = cflag | tmp;
1346 }
1347 outp[k++] = res >> 16;
1348 if(pcnt == 2)
1349 goto done;
1350 outp[k++] = (res>>8)&0xFF;
1351 if(pcnt == 1)
1352 goto done;
1353 outp[k++] = res&0xFF;
1354 } else {
1355 //without padding
1356 res = 0;
1357 for(i=0;i<rem;i++) {
1358 res = res << 6;
1359 tmp = getIndex[(uint8_t)inp[j++]];
1360 res = res | tmp;
1361 cflag = cflag | tmp;
1362 }
1363 for(i=rem;i<4;i++) {
1364 res = res << 6;
1365 pcnt++;
1366 }
1367 outp[k++] = res >> 16;
1368 if(pcnt == 2)
1369 goto done;
1370 outp[k++] = (res>>8)&0xFF;
1371 if(pcnt == 1)
1372 goto done;
1373 outp[k++] = res&0xFF;
1374 }
1375done:
1376 if(cflag == 0xFF) {
1377 ALOGE("[%s] base64 decode failed. Invalid character found %s",
1378 __func__, inp);
1379 return 0;
1380 }
1381 return k;
1382}
1383
1384int b64encode(uint8_t *inp, int ilen, char* outp)
1385{
1386 int i,j,k, num;
1387 int rem=0;
1388 uint32_t res=0;
1389
1390 if(inp == NULL || outp == NULL || ilen<=0) {
1391 ALOGE("[%s] received NULL pointer or zero input length",__func__);
1392 return -1;
1393 }
1394
1395 num = ilen/3;
1396 rem = ilen%3;
1397 j=0;k=0;
1398 for(i=0; i<num; i++) {
1399 //prepare index
1400 res = inp[j++]<<16;
1401 res = res | inp[j++]<<8;
1402 res = res | inp[j++];
1403 //get output map from index
1404 outp[k++] = (char) bTable[(res>>18)&0x3F];
1405 outp[k++] = (char) bTable[(res>>12)&0x3F];
1406 outp[k++] = (char) bTable[(res>>6)&0x3F];
1407 outp[k++] = (char) bTable[res&0x3F];
1408 }
1409
1410 switch(rem) {
1411 case 1:
1412 res = inp[j++]<<16;
1413 outp[k++] = (char) bTable[res>>18];
1414 outp[k++] = (char) bTable[(res>>12)&0x3F];
1415 //outp[k++] = '=';
1416 //outp[k++] = '=';
1417 break;
1418 case 2:
1419 res = inp[j++]<<16;
1420 res = res | inp[j++]<<8;
1421 outp[k++] = (char) bTable[res>>18];
1422 outp[k++] = (char) bTable[(res>>12)&0x3F];
1423 outp[k++] = (char) bTable[(res>>6)&0x3F];
1424 //outp[k++] = '=';
1425 break;
1426 default:
1427 break;
1428 }
Ben Rombergera04fabc2014-11-14 12:16:03 -08001429 outp[k] = '\0';
1430 return k;
1431}
Ashish Jain81eb2a82015-05-13 10:52:34 +05301432
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301433
1434int audio_extn_utils_get_codec_version(const char *snd_card_name,
1435 int card_num,
1436 char *codec_version)
1437{
1438 char procfs_path[50];
1439 FILE *fp;
1440
1441 if (strstr(snd_card_name, "tasha")) {
1442 snprintf(procfs_path, sizeof(procfs_path),
1443 "/proc/asound/card%d/codecs/tasha/version", card_num);
1444 if ((fp = fopen(procfs_path, "r")) != NULL) {
1445 fgets(codec_version, CODEC_VERSION_MAX_LENGTH, fp);
1446 fclose(fp);
1447 } else {
1448 ALOGE("%s: ERROR. cannot open %s", __func__, procfs_path);
1449 return -ENOENT;
1450 }
1451 ALOGD("%s: codec version %s", __func__, codec_version);
1452 }
1453
1454 return 0;
1455}
1456
1457
Ashish Jain81eb2a82015-05-13 10:52:34 +05301458#ifdef AUDIO_EXTERNAL_HDMI_ENABLED
1459
1460void get_default_compressed_channel_status(
1461 unsigned char *channel_status)
1462{
Ashish Jain81eb2a82015-05-13 10:52:34 +05301463 memset(channel_status,0,24);
1464
1465 /* block start bit in preamble bit 3 */
1466 channel_status[0] |= PROFESSIONAL;
1467 //compre out
1468 channel_status[0] |= NON_LPCM;
1469 // sample rate; fixed 48K for default/transcode
1470 channel_status[3] |= SR_48000;
1471}
1472
1473#ifdef HDMI_PASSTHROUGH_ENABLED
1474int32_t get_compressed_channel_status(void *audio_stream_data,
1475 uint32_t audio_frame_size,
1476 unsigned char *channel_status,
1477 enum audio_parser_code_type codec_type)
1478 // codec_type - AUDIO_PARSER_CODEC_AC3
1479 // - AUDIO_PARSER_CODEC_DTS
1480{
1481 unsigned char *stream;
1482 int ret = 0;
1483 stream = (unsigned char *)audio_stream_data;
1484
1485 if (audio_stream_data == NULL || audio_frame_size == 0) {
1486 ALOGW("no buffer to get channel status, return default for compress");
1487 get_default_compressed_channel_status(channel_status);
1488 return ret;
1489 }
1490
1491 memset(channel_status,0,24);
1492 if(init_audio_parser(stream, audio_frame_size, codec_type) == -1)
1493 {
1494 ALOGE("init audio parser failed");
1495 return -1;
1496 }
1497 ret = get_channel_status(channel_status, codec_type);
1498 return ret;
1499
1500}
1501
1502#endif
1503
1504void get_lpcm_channel_status(uint32_t sampleRate,
1505 unsigned char *channel_status)
1506{
1507 int32_t status = 0;
Ashish Jain81eb2a82015-05-13 10:52:34 +05301508 memset(channel_status,0,24);
1509 /* block start bit in preamble bit 3 */
1510 channel_status[0] |= PROFESSIONAL;
1511 //LPCM OUT
1512 channel_status[0] &= ~NON_LPCM;
1513
1514 switch (sampleRate) {
1515 case 8000:
1516 case 11025:
1517 case 12000:
1518 case 16000:
1519 case 22050:
1520 channel_status[3] |= SR_NOTID;
Preetam Singh Ranawat61716b12015-12-14 11:55:24 +05301521 break;
Ashish Jain81eb2a82015-05-13 10:52:34 +05301522 case 24000:
1523 channel_status[3] |= SR_24000;
1524 break;
1525 case 32000:
1526 channel_status[3] |= SR_32000;
1527 break;
1528 case 44100:
1529 channel_status[3] |= SR_44100;
1530 break;
1531 case 48000:
1532 channel_status[3] |= SR_48000;
1533 break;
1534 case 88200:
1535 channel_status[3] |= SR_88200;
1536 break;
1537 case 96000:
1538 channel_status[3] |= SR_96000;
1539 break;
1540 case 176400:
1541 channel_status[3] |= SR_176400;
1542 break;
1543 case 192000:
1544 channel_status[3] |= SR_192000;
1545 break;
1546 default:
1547 ALOGV("Invalid sample_rate %u\n", sampleRate);
1548 status = -1;
1549 break;
1550 }
1551}
1552
1553void audio_utils_set_hdmi_channel_status(struct stream_out *out, char * buffer, size_t bytes)
1554{
1555 unsigned char channel_status[24]={0};
1556 struct snd_aes_iec958 iec958;
1557 const char *mixer_ctl_name = "IEC958 Playback PCM Stream";
1558 struct mixer_ctl *ctl;
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +05301559 ALOGV("%s: buffer %s bytes %zd", __func__, buffer, bytes);
Ashish Jain81eb2a82015-05-13 10:52:34 +05301560#ifdef HDMI_PASSTHROUGH_ENABLED
1561 if (audio_extn_is_dolby_format(out->format) &&
1562 /*TODO:Extend code to support DTS passthrough*/
1563 /*set compressed channel status bits*/
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05301564 audio_extn_passthru_is_passthrough_stream(out)){
Ashish Jain81eb2a82015-05-13 10:52:34 +05301565 get_compressed_channel_status(buffer, bytes, channel_status, AUDIO_PARSER_CODEC_AC3);
1566 } else
1567#endif
1568 {
1569 /*set channel status bit for LPCM*/
1570 get_lpcm_channel_status(out->sample_rate, channel_status);
1571 }
1572
1573 memcpy(iec958.status, channel_status,sizeof(iec958.status));
1574 ctl = mixer_get_ctl_by_name(out->dev->mixer, mixer_ctl_name);
1575 if (!ctl) {
1576 ALOGE("%s: Could not get ctl for mixer cmd - %s",
1577 __func__, mixer_ctl_name);
1578 return;
1579 }
1580 if (mixer_ctl_set_array(ctl, &iec958, sizeof(iec958)) < 0) {
1581 ALOGE("%s: Could not set channel status for ext HDMI ",
1582 __func__);
1583 return;
1584 }
1585
1586}
1587#endif
Manish Dewangan3ccdea52017-02-13 19:31:54 +05301588
1589int audio_extn_utils_get_avt_device_drift(
1590 struct audio_usecase *usecase,
1591 struct audio_avt_device_drift_param *drift_param)
1592{
1593 int ret = 0, count = 0;
1594 char avt_device_drift_mixer_ctl_name[MIXER_PATH_MAX_LENGTH] = {0};
1595 struct mixer_ctl *ctl = NULL;
1596 struct audio_avt_device_drift_stats drift_stats;
1597 struct audio_device *adev = NULL;
1598
1599 if (usecase != NULL && usecase->type == PCM_PLAYBACK) {
1600 adev = usecase->stream.out->dev;
1601 switch(usecase->out_snd_device) {
1602 case SND_DEVICE_OUT_HDMI:
1603 strlcpy(avt_device_drift_mixer_ctl_name,
1604 "HDMI RX Drift",
1605 MIXER_PATH_MAX_LENGTH);
1606 break;
1607 case SND_DEVICE_OUT_DISPLAY_PORT:
1608 strlcpy(avt_device_drift_mixer_ctl_name,
1609 "DISPLAY Port RX Drift",
1610 MIXER_PATH_MAX_LENGTH);
1611 break;
1612 default :
1613 ALOGE("%s: Unsupported device %d",__func__,
1614 usecase->stream.out->devices);
1615 ret = -EINVAL;
1616 }
1617 } else {
1618 ALOGE("%s: Invalid usecase %d ",__func__, usecase->type);
1619 ret = -EINVAL;
1620 }
1621
1622 if(ret)
1623 goto done;
1624
1625 ctl = mixer_get_ctl_by_name(adev->mixer, avt_device_drift_mixer_ctl_name);
1626 if (!ctl) {
1627 ALOGE("%s: Could not get ctl for mixer cmd - %s",
1628 __func__, avt_device_drift_mixer_ctl_name);
1629
1630 ret = -EINVAL;
1631 goto done;
1632 }
1633
1634 ALOGV("%s: Getting AV Timer vs Device Drift mixer ctrl name %s", __func__,
1635 avt_device_drift_mixer_ctl_name);
1636
1637 mixer_ctl_update(ctl);
1638 count = mixer_ctl_get_num_values(ctl);
1639 if (count != sizeof(struct audio_avt_device_drift_stats)) {
1640 ALOGE("%s: mixer_ctl_get_num_values() invalid drift_stats data size",
1641 __func__);
1642
1643 ret = -EINVAL;
1644 goto done;
1645 }
1646
1647 ret = mixer_ctl_get_array(ctl, (void *)&drift_stats, count);
1648 if (ret != 0) {
1649 ALOGE("%s: mixer_ctl_get_array() failed to get drift_stats Params",
1650 __func__);
1651
1652 ret = -EINVAL;
1653 goto done;
1654 }
1655 memcpy(drift_param, &drift_stats.drift_param,
1656 sizeof(struct audio_avt_device_drift_param));
1657done:
1658 return ret;
1659}
Manish Dewangan07de2142017-02-27 19:27:20 +05301660
1661#ifdef SNDRV_COMPRESS_PATH_DELAY
1662int audio_extn_utils_compress_get_dsp_latency(struct stream_out *out)
1663{
1664 int ret = -EINVAL;
1665 struct snd_compr_metadata metadata;
1666 int delay_ms = COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
1667
1668 if (property_get_bool("audio.playback.dsp.pathdelay", false)) {
1669 ALOGD("%s:: Quering DSP delay %d",__func__, __LINE__);
1670 if (!(is_offload_usecase(out->usecase))) {
1671 ALOGE("%s:: not supported for non offload session", __func__);
1672 goto exit;
1673 }
1674
1675 if (!out->compr) {
1676 ALOGD("%s:: Invalid compress handle,returning default dsp latency",
1677 __func__);
1678 goto exit;
1679 }
1680
1681 metadata.key = SNDRV_COMPRESS_PATH_DELAY;
1682 ret = compress_get_metadata(out->compr, &metadata);
1683 if(ret) {
1684 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
1685 goto exit;
1686 }
1687 delay_ms = metadata.value[0] / 1000; /*convert to ms*/
1688 } else {
1689 ALOGD("%s:: Using Fix DSP delay",__func__);
1690 }
1691
1692exit:
1693 ALOGD("%s:: delay in ms is %d",__func__, delay_ms);
1694 return delay_ms;
1695}
1696#else
1697int audio_extn_utils_compress_get_dsp_latency(struct stream_out *out __unused)
1698{
1699 return COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
1700}
1701#endif
Manish Dewangan69426c82017-01-30 17:35:36 +05301702
1703#ifdef SNDRV_COMPRESS_RENDER_MODE
1704int audio_extn_utils_compress_set_render_mode(struct stream_out *out)
1705{
1706 struct snd_compr_metadata metadata;
1707 int ret = -EINVAL;
1708
1709 if (!(is_offload_usecase(out->usecase))) {
1710 ALOGE("%s:: not supported for non offload session", __func__);
1711 goto exit;
1712 }
1713
1714 if (!out->compr) {
1715 ALOGD("%s:: Invalid compress handle",
1716 __func__);
1717 goto exit;
1718 }
1719
1720 ALOGD("%s:: render mode %d", __func__, out->render_mode);
1721
1722 metadata.key = SNDRV_COMPRESS_RENDER_MODE;
1723 if (out->render_mode == RENDER_MODE_AUDIO_MASTER) {
1724 metadata.value[0] = SNDRV_COMPRESS_RENDER_MODE_AUDIO_MASTER;
1725 } else if (out->render_mode == RENDER_MODE_AUDIO_STC_MASTER) {
1726 metadata.value[0] = SNDRV_COMPRESS_RENDER_MODE_STC_MASTER;
1727 } else {
1728 ret = 0;
1729 goto exit;
1730 }
1731 ret = compress_set_metadata(out->compr, &metadata);
1732 if(ret) {
1733 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
1734 }
1735exit:
1736 return ret;
1737}
1738#else
1739int audio_extn_utils_compress_set_render_mode(struct stream_out *out __unused)
1740{
1741 ALOGD("%s:: configuring render mode not supported", __func__);
1742 return 0;
1743}
1744#endif
Manish Dewangan58229382017-02-02 15:48:41 +05301745
1746#ifdef SNDRV_COMPRESS_CLK_REC_MODE
1747int audio_extn_utils_compress_set_clk_rec_mode(
1748 struct audio_usecase *usecase)
1749{
1750 struct snd_compr_metadata metadata;
1751 struct stream_out *out = NULL;
1752 int ret = -EINVAL;
1753
1754 if (usecase != NULL && usecase->type != PCM_PLAYBACK) {
1755 ALOGE("%s:: Invalid use case", __func__);
1756 goto exit;
1757 }
1758
1759 out = usecase->stream.out;
1760 if (!out) {
1761 ALOGE("%s:: invalid stream", __func__);
1762 goto exit;
1763 }
1764
1765 if (!is_offload_usecase(out->usecase)) {
1766 ALOGE("%s:: not supported for non offload session", __func__);
1767 goto exit;
1768 }
1769
1770 if (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER) {
1771 ALOGD("%s:: clk recovery is only supported in STC render mode",
1772 __func__);
1773 ret = 0;
1774 goto exit;
1775 }
1776
1777 if (!out->compr) {
1778 ALOGD("%s:: Invalid compress handle",
1779 __func__);
1780 goto exit;
1781 }
1782 metadata.key = SNDRV_COMPRESS_CLK_REC_MODE;
1783 switch(usecase->out_snd_device) {
1784 case SND_DEVICE_OUT_HDMI:
1785 case SND_DEVICE_OUT_SPEAKER_AND_HDMI:
1786 case SND_DEVICE_OUT_DISPLAY_PORT:
1787 case SND_DEVICE_OUT_SPEAKER_AND_DISPLAY_PORT:
1788 metadata.value[0] = SNDRV_COMPRESS_CLK_REC_MODE_NONE;
1789 break;
1790 default:
1791 metadata.value[0] = SNDRV_COMPRESS_CLK_REC_MODE_AUTO;
1792 break;
1793 }
1794
1795 ALOGD("%s:: clk recovery mode %d",__func__, metadata.value[0]);
1796
1797 ret = compress_set_metadata(out->compr, &metadata);
1798 if(ret) {
1799 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
1800 }
1801
1802exit:
1803 return ret;
1804}
1805#else
1806int audio_extn_utils_compress_set_clk_rec_mode(
1807 struct audio_usecase *usecase __unused)
1808{
1809 ALOGD("%s:: configuring render mode not supported", __func__);
1810 return 0;
1811}
1812#endif
Manish Dewangan27346042017-03-01 12:56:12 +05301813
1814#ifdef SNDRV_COMPRESS_RENDER_WINDOW
1815int audio_extn_utils_compress_set_render_window(
1816 struct stream_out *out,
1817 struct audio_out_render_window_param *render_window)
1818{
1819 struct snd_compr_metadata metadata;
1820 int ret = -EINVAL;
1821
1822 ALOGD("%s:: render window start 0x%"PRIx64" end 0x%"PRIx64"",
1823 __func__,render_window->render_ws, render_window->render_we);
1824
1825 if(render_window == NULL) {
1826 ALOGE("%s:: Invalid render_window", __func__);
1827 goto exit;
1828 }
1829
1830 if (!is_offload_usecase(out->usecase)) {
1831 ALOGE("%s:: not supported for non offload session", __func__);
1832 goto exit;
1833 }
1834
1835 if ((out->render_mode == RENDER_MODE_AUDIO_MASTER) ||
1836 (out->render_mode == RENDER_MODE_AUDIO_STC_MASTER)) {
1837 memcpy(&out->render_window, render_window,
1838 sizeof(struct audio_out_render_window_param));
1839 } else {
1840 ALOGD("%s:: only supported in timestamp mode, current "
1841 "render mode mode %d", __func__, out->render_mode);
1842 goto exit;
1843 }
1844
1845 if (!out->compr) {
1846 ALOGW("%s:: offload session not yet opened,"
1847 "render window will be configure later", __func__);
1848 /* store render window to reconfigure in start_output_stream() */
1849 goto exit;
1850 }
1851
1852 metadata.key = SNDRV_COMPRESS_RENDER_WINDOW;
1853 /*render window start value */
1854 metadata.value[0] = 0xFFFFFFFF & render_window->render_ws; /* lsb */
1855 metadata.value[1] = \
1856 (0xFFFFFFFF00000000 & render_window->render_ws) >> 32; /* msb*/
1857 /*render window end value */
1858 metadata.value[2] = 0xFFFFFFFF & render_window->render_we; /* lsb */
1859 metadata.value[3] = \
1860 (0xFFFFFFFF00000000 & render_window->render_we) >> 32; /* msb*/
1861
1862 ret = compress_set_metadata(out->compr, &metadata);
1863 if(ret) {
1864 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
1865 }
1866
1867exit:
1868 return ret;
1869}
1870#else
1871int audio_extn_utils_compress_set_render_window(
1872 struct stream_out *out __unused,
1873 struct audio_out_render_window_param *render_window __unused)
1874{
1875 ALOGD("%s:: configuring render window not supported", __func__);
1876 return 0;
1877}
1878#endif
Manish Dewangan14956cc2017-02-14 18:54:42 +05301879
1880#ifdef SNDRV_COMPRESS_START_DELAY
1881int audio_extn_utils_compress_set_start_delay(
1882 struct stream_out *out,
1883 struct audio_out_start_delay_param *delay_param)
1884{
1885 struct snd_compr_metadata metadata;
1886 int ret = -EINVAL;
1887
1888 if(delay_param == NULL) {
1889 ALOGE("%s:: Invalid delay_param", __func__);
1890 goto exit;
1891 }
1892
1893 ALOGD("%s:: render start delay 0x%"PRIx64" ", __func__,
1894 delay_param->start_delay);
1895
1896 if (!is_offload_usecase(out->usecase)) {
1897 ALOGE("%s:: not supported for non offload session", __func__);
1898 goto exit;
1899 }
1900
1901 if ((out->render_mode == RENDER_MODE_AUDIO_MASTER) ||
1902 (out->render_mode == RENDER_MODE_AUDIO_STC_MASTER)) {
1903 /* store it to reconfigure in start_output_stream() */
1904 out->delay_param.start_delay = delay_param->start_delay;
1905 } else {
1906 ALOGD("%s:: only supported in timestamp mode, current "
1907 "render mode mode %d", __func__, out->render_mode);
1908 goto exit;
1909 }
1910
1911 if (!out->compr) {
1912 ALOGW("%s:: offload session not yet opened,"
1913 "start delay will be configure later", __func__);
1914 goto exit;
1915 }
1916
1917 metadata.key = SNDRV_COMPRESS_START_DELAY;
1918 metadata.value[0] = 0xFFFFFFFF & delay_param->start_delay; /* lsb */
1919 metadata.value[1] = \
1920 (0xFFFFFFFF00000000 & delay_param->start_delay) >> 32; /* msb*/
1921
1922 ret = compress_set_metadata(out->compr, &metadata);
1923 if(ret) {
1924 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
1925 }
1926
1927exit:
1928 return ret;
1929}
1930#else
1931int audio_extn_utils_compress_set_start_delay(
1932 struct stream_out *out __unused,
1933 struct audio_out_start_delay_param *delay_param __unused)
1934{
1935 ALOGD("%s:: configuring render window not supported", __func__);
1936 return 0;
1937}
1938#endif
Vignesh Kulothungan55396882017-04-20 14:37:02 -07001939
1940#define MAX_SND_CARD 8
1941#define RETRY_US 500000
1942#define RETRY_NUMBER 10
1943
1944int audio_extn_utils_get_snd_card_num()
1945{
1946
1947 void *hw_info = NULL;
1948 struct mixer *mixer = NULL;
1949 int retry_num = 0;
1950 int snd_card_num = 0;
1951 char* snd_card_name = NULL;
1952
1953 while (snd_card_num < MAX_SND_CARD) {
1954 mixer = mixer_open(snd_card_num);
1955
1956 while (!mixer && retry_num < RETRY_NUMBER) {
1957 usleep(RETRY_US);
1958 mixer = mixer_open(snd_card_num);
1959 retry_num++;
1960 }
1961
1962 if (!mixer) {
1963 ALOGE("%s: Unable to open the mixer card: %d", __func__,
1964 snd_card_num);
1965 retry_num = 0;
1966 snd_card_num++;
1967 continue;
1968 }
1969
1970 snd_card_name = strdup(mixer_get_name(mixer));
1971 if (!snd_card_name) {
1972 ALOGE("failed to allocate memory for snd_card_name\n");
1973 mixer_close(mixer);
1974 return -1;
1975 }
1976 ALOGD("%s: snd_card_name: %s", __func__, snd_card_name);
1977
1978 hw_info = hw_info_init(snd_card_name);
1979 if (hw_info) {
1980 ALOGD("%s: Opened sound card:%d", __func__, snd_card_num);
1981 break;
1982 }
1983 ALOGE("%s: Failed to init hardware info", __func__);
1984 retry_num = 0;
1985 snd_card_num++;
1986 free(snd_card_name);
1987 mixer_close(mixer);
1988 }
1989
1990 mixer_close(mixer);
1991 hw_info_deinit(hw_info);
1992 if (snd_card_name)
1993 free(snd_card_name);
1994
1995 if (snd_card_num >= MAX_SND_CARD) {
1996 ALOGE("%s: Unable to find correct sound card, aborting.", __func__);
1997 return -1;
1998 }
1999
2000 return snd_card_num;
2001}