blob: 3052b23fe22e2036998959bbfb0122f774e7381d [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>
Revathi Uddaraju1eac8b02017-05-18 17:13:33 +053042
43#ifdef DYNAMIC_LOG_ENABLED
44#include <log_xml_parser.h>
45#define LOG_MASK HAL_MOD_FILE_UTILS
46#include <log_utils.h>
47#endif
48
Ashish Jain81eb2a82015-05-13 10:52:34 +053049#ifdef AUDIO_EXTERNAL_HDMI_ENABLED
50#ifdef HDMI_PASSTHROUGH_ENABLED
51#include "audio_parsers.h"
52#endif
53#endif
54
Yamit Mehtaa0d653a2016-11-25 20:33:25 +053055#ifdef LINUX_ENABLED
56#define AUDIO_OUTPUT_POLICY_VENDOR_CONFIG_FILE "/etc/audio_output_policy.conf"
Dhananjay Kumard6d32152016-10-13 16:11:03 +053057#define AUDIO_IO_POLICY_VENDOR_CONFIG_FILE "/etc/audio_io_policy.conf"
Yamit Mehtaa0d653a2016-11-25 20:33:25 +053058#else
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070059#define AUDIO_OUTPUT_POLICY_VENDOR_CONFIG_FILE "/vendor/etc/audio_output_policy.conf"
Dhananjay Kumard6d32152016-10-13 16:11:03 +053060#define AUDIO_IO_POLICY_VENDOR_CONFIG_FILE "/vendor/etc/audio_io_policy.conf"
Yamit Mehtaa0d653a2016-11-25 20:33:25 +053061#endif
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070062
63#define OUTPUTS_TAG "outputs"
Dhananjay Kumard6d32152016-10-13 16:11:03 +053064#define INPUTS_TAG "inputs"
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070065
66#define DYNAMIC_VALUE_TAG "dynamic"
67#define FLAGS_TAG "flags"
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +053068#define PROFILES_TAG "profile"
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070069#define FORMATS_TAG "formats"
70#define SAMPLING_RATES_TAG "sampling_rates"
71#define BIT_WIDTH_TAG "bit_width"
72#define APP_TYPE_TAG "app_type"
73
74#define STRING_TO_ENUM(string) { #string, string }
75#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
76
Ben Rombergera04fabc2014-11-14 12:16:03 -080077#define BASE_TABLE_SIZE 64
78#define MAX_BASEINDEX_LEN 256
79
Ben Romberger1aaaf862017-04-06 17:49:46 -070080#ifndef SND_AUDIOCODEC_TRUEHD
81#define SND_AUDIOCODEC_TRUEHD 0x00000023
82#endif
83
Ashish Jain81eb2a82015-05-13 10:52:34 +053084#ifdef AUDIO_EXTERNAL_HDMI_ENABLED
85#define PROFESSIONAL (1<<0) /* 0 = consumer, 1 = professional */
86#define NON_LPCM (1<<1) /* 0 = audio, 1 = non-audio */
87#define SR_44100 (0<<0) /* 44.1kHz */
88#define SR_NOTID (1<<0) /* non indicated */
89#define SR_48000 (2<<0) /* 48kHz */
90#define SR_32000 (3<<0) /* 32kHz */
91#define SR_22050 (4<<0) /* 22.05kHz */
92#define SR_24000 (6<<0) /* 24kHz */
93#define SR_88200 (8<<0) /* 88.2kHz */
94#define SR_96000 (10<<0) /* 96kHz */
95#define SR_176400 (12<<0) /* 176.4kHz */
96#define SR_192000 (14<<0) /* 192kHz */
97
98#endif
Manish Dewangan07de2142017-02-27 19:27:20 +053099
100/* ToDo: Check and update a proper value in msec */
101#define COMPRESS_OFFLOAD_PLAYBACK_LATENCY 50
102
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700103struct string_to_enum {
104 const char *name;
105 uint32_t value;
106};
107
108const struct string_to_enum s_flag_name_to_enum_table[] = {
109 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_DIRECT),
110 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_PRIMARY),
111 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_FAST),
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800112 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_RAW),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700113 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_DEEP_BUFFER),
114 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD),
115 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_NON_BLOCKING),
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700116 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_HW_AV_SYNC),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700117#ifdef INCALL_MUSIC_ENABLED
118 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_INCALL_MUSIC),
119#endif
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700120 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH),
Naresh Tanniruee3499a2017-01-05 14:05:35 +0530121 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_TIMESTAMP),
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530122 STRING_TO_ENUM(AUDIO_INPUT_FLAG_NONE),
123 STRING_TO_ENUM(AUDIO_INPUT_FLAG_FAST),
124 STRING_TO_ENUM(AUDIO_INPUT_FLAG_HW_HOTWORD),
125 STRING_TO_ENUM(AUDIO_INPUT_FLAG_RAW),
126 STRING_TO_ENUM(AUDIO_INPUT_FLAG_SYNC),
Dhananjay Kumaree4d2002016-10-25 18:02:58 +0530127 STRING_TO_ENUM(AUDIO_INPUT_FLAG_TIMESTAMP),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700128};
129
130const struct string_to_enum s_format_name_to_enum_table[] = {
Ashish Jain83a6cc22016-06-28 14:34:17 +0530131 STRING_TO_ENUM(AUDIO_FORMAT_PCM_8_BIT),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700132 STRING_TO_ENUM(AUDIO_FORMAT_PCM_16_BIT),
Ashish Jain5106d362016-05-11 19:23:33 +0530133 STRING_TO_ENUM(AUDIO_FORMAT_PCM_24_BIT_PACKED),
134 STRING_TO_ENUM(AUDIO_FORMAT_PCM_8_24_BIT),
Ashish Jainf1eaa582016-05-23 20:54:24 +0530135 STRING_TO_ENUM(AUDIO_FORMAT_PCM_32_BIT),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700136 STRING_TO_ENUM(AUDIO_FORMAT_MP3),
137 STRING_TO_ENUM(AUDIO_FORMAT_AAC),
138 STRING_TO_ENUM(AUDIO_FORMAT_VORBIS),
Mingming Yinae3530f2014-07-03 16:50:18 -0700139 STRING_TO_ENUM(AUDIO_FORMAT_AMR_NB),
140 STRING_TO_ENUM(AUDIO_FORMAT_AMR_WB),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700141 STRING_TO_ENUM(AUDIO_FORMAT_AC3),
Mingming Yinae3530f2014-07-03 16:50:18 -0700142 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700143 STRING_TO_ENUM(AUDIO_FORMAT_DTS),
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530144 STRING_TO_ENUM(AUDIO_FORMAT_DTS_HD),
Ben Romberger1aaaf862017-04-06 17:49:46 -0700145 STRING_TO_ENUM(AUDIO_FORMAT_DOLBY_TRUEHD),
Naresh Tanniru928f0862017-04-07 16:44:23 -0700146 STRING_TO_ENUM(AUDIO_FORMAT_IEC61937),
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530147#ifdef AUDIO_EXTN_FORMATS_ENABLED
148 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3_JOC),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700149 STRING_TO_ENUM(AUDIO_FORMAT_WMA),
150 STRING_TO_ENUM(AUDIO_FORMAT_WMA_PRO),
151 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADIF),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700152 STRING_TO_ENUM(AUDIO_FORMAT_AMR_WB_PLUS),
153 STRING_TO_ENUM(AUDIO_FORMAT_EVRC),
154 STRING_TO_ENUM(AUDIO_FORMAT_EVRCB),
155 STRING_TO_ENUM(AUDIO_FORMAT_EVRCWB),
156 STRING_TO_ENUM(AUDIO_FORMAT_QCELP),
157 STRING_TO_ENUM(AUDIO_FORMAT_MP2),
158 STRING_TO_ENUM(AUDIO_FORMAT_EVRCNW),
Amit Shekhar6f461b12014-08-01 14:52:58 -0700159 STRING_TO_ENUM(AUDIO_FORMAT_FLAC),
Satya Krishna Pindiproli70471602015-04-24 19:12:43 +0530160 STRING_TO_ENUM(AUDIO_FORMAT_ALAC),
161 STRING_TO_ENUM(AUDIO_FORMAT_APE),
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700162 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3_JOC),
Alexy Josephcd8eaed2014-12-11 12:46:53 -0800163 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LC),
164 STRING_TO_ENUM(AUDIO_FORMAT_AAC_HE_V1),
165 STRING_TO_ENUM(AUDIO_FORMAT_AAC_HE_V2),
Manish Dewangana6fc5442015-08-24 20:30:31 +0530166 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS),
Ashish Jaine513a872015-11-19 17:00:56 +0530167 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_LC),
168 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_HE_V1),
169 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_HE_V2),
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +0530170 STRING_TO_ENUM(AUDIO_FORMAT_DSD),
Arun Kumar Dasari3b174182016-12-27 13:01:14 +0530171 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM),
172 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM_LC),
173 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM_HE_V1),
174 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM_HE_V2),
Dhanalakshmi Siddani18737932016-11-29 17:33:17 +0530175 STRING_TO_ENUM(AUDIO_FORMAT_APTX),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700176#endif
177};
178
Manish Dewangan3ccdea52017-02-13 19:31:54 +0530179/* payload structure avt_device drift query */
180struct audio_avt_device_drift_stats {
181 uint32_t minor_version;
182 /* Indicates the device interface direction as either
183 * source (Tx) or sink (Rx).
184 */
185 uint16_t device_direction;
186 /*params exposed to client */
187 struct audio_avt_device_drift_param drift_param;
188};
189
Ben Rombergera04fabc2014-11-14 12:16:03 -0800190static char bTable[BASE_TABLE_SIZE] = {
191 'A','B','C','D','E','F','G','H','I','J','K','L',
192 'M','N','O','P','Q','R','S','T','U','V','W','X',
193 'Y','Z','a','b','c','d','e','f','g','h','i','j',
194 'k','l','m','n','o','p','q','r','s','t','u','v',
195 'w','x','y','z','0','1','2','3','4','5','6','7',
196 '8','9','+','/'
197};
198
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700199static uint32_t string_to_enum(const struct string_to_enum *table, size_t size,
200 const char *name)
201{
202 size_t i;
203 for (i = 0; i < size; i++) {
204 if (strcmp(table[i].name, name) == 0) {
205 ALOGV("%s found %s", __func__, table[i].name);
206 return table[i].value;
207 }
208 }
209 return 0;
210}
211
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530212static audio_io_flags_t parse_flag_names(char *name)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700213{
214 uint32_t flag = 0;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530215 audio_io_flags_t io_flags;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800216 char *last_r;
217 char *flag_name = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700218 while (flag_name != NULL) {
219 if (strlen(flag_name) != 0) {
220 flag |= string_to_enum(s_flag_name_to_enum_table,
221 ARRAY_SIZE(s_flag_name_to_enum_table),
222 flag_name);
223 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800224 flag_name = strtok_r(NULL, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700225 }
226
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800227 ALOGV("parse_flag_names: flag - %x", flag);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530228 io_flags.in_flags = (audio_input_flags_t)flag;
229 io_flags.out_flags = (audio_output_flags_t)flag;
230 return io_flags;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700231}
232
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530233static void parse_format_names(char *name, struct streams_io_cfg *s_info)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700234{
235 struct stream_format *sf_info = NULL;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800236 char *last_r;
237 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700238
239 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG) == 0)
240 return;
241
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530242 list_init(&s_info->format_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700243 while (str != NULL) {
244 audio_format_t format = (audio_format_t)string_to_enum(s_format_name_to_enum_table,
245 ARRAY_SIZE(s_format_name_to_enum_table), str);
246 ALOGV("%s: format - %d", __func__, format);
247 if (format != 0) {
248 sf_info = (struct stream_format *)calloc(1, sizeof(struct stream_format));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700249 if (sf_info == NULL)
250 break; /* return whatever was parsed */
251
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700252 sf_info->format = format;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530253 list_add_tail(&s_info->format_list, &sf_info->list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700254 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800255 str = strtok_r(NULL, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700256 }
257}
258
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530259static void parse_sample_rate_names(char *name, struct streams_io_cfg *s_info)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700260{
Amit Shekhar6f461b12014-08-01 14:52:58 -0700261 struct stream_sample_rate *ss_info = NULL;
262 uint32_t sample_rate = 48000;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800263 char *last_r;
264 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700265
Amit Shekhar6f461b12014-08-01 14:52:58 -0700266 if (str != NULL && 0 == strcmp(str, DYNAMIC_VALUE_TAG))
267 return;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700268
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530269 list_init(&s_info->sample_rate_list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700270 while (str != NULL) {
271 sample_rate = (uint32_t)strtol(str, (char **)NULL, 10);
272 ALOGV("%s: sample_rate - %d", __func__, sample_rate);
273 if (0 != sample_rate) {
274 ss_info = (struct stream_sample_rate *)calloc(1, sizeof(struct stream_sample_rate));
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800275 if (!ss_info) {
276 ALOGE("%s: memory allocation failure", __func__);
277 return;
278 }
Amit Shekhar6f461b12014-08-01 14:52:58 -0700279 ss_info->sample_rate = sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530280 list_add_tail(&s_info->sample_rate_list, &ss_info->list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700281 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800282 str = strtok_r(NULL, "|", &last_r);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700283 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700284}
285
286static int parse_bit_width_names(char *name)
287{
288 int bit_width = 16;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800289 char *last_r;
290 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700291
292 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG))
293 bit_width = (int)strtol(str, (char **)NULL, 10);
294
295 ALOGV("%s: bit_width - %d", __func__, bit_width);
296 return bit_width;
297}
298
299static int parse_app_type_names(void *platform, char *name)
300{
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700301 int app_type = platform_get_default_app_type(platform);
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800302 char *last_r;
303 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700304
305 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG))
306 app_type = (int)strtol(str, (char **)NULL, 10);
307
308 ALOGV("%s: app_type - %d", __func__, app_type);
309 return app_type;
310}
311
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530312static void update_streams_cfg_list(cnode *root, void *platform,
313 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700314{
315 cnode *node = root->first_child;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530316 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700317
318 ALOGV("%s", __func__);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530319 s_info = (struct streams_io_cfg *)calloc(1, sizeof(struct streams_io_cfg));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700320
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530321 if (!s_info) {
322 ALOGE("failed to allocate mem for s_info list element");
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700323 return;
324 }
325
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700326 while (node) {
327 if (strcmp(node->name, FLAGS_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530328 s_info->flags = parse_flag_names((char *)node->value);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530329 } else if (strcmp(node->name, PROFILES_TAG) == 0) {
330 strlcpy(s_info->profile, (char *)node->value, sizeof(s_info->profile));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700331 } else if (strcmp(node->name, FORMATS_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530332 parse_format_names((char *)node->value, s_info);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700333 } else if (strcmp(node->name, SAMPLING_RATES_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530334 s_info->app_type_cfg.sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
335 parse_sample_rate_names((char *)node->value, s_info);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700336 } else if (strcmp(node->name, BIT_WIDTH_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530337 s_info->app_type_cfg.bit_width = parse_bit_width_names((char *)node->value);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700338 } else if (strcmp(node->name, APP_TYPE_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530339 s_info->app_type_cfg.app_type = parse_app_type_names(platform, (char *)node->value);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700340 }
341 node = node->next;
342 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530343 list_add_tail(streams_cfg_list, &s_info->list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700344}
345
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530346static void load_cfg_list(cnode *root, void *platform,
347 struct listnode *streams_output_cfg_list,
348 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700349{
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530350 cnode *node = NULL;
351
352 node = config_find(root, OUTPUTS_TAG);
353 if (node != NULL) {
354 node = node->first_child;
355 while (node) {
356 ALOGV("%s: loading output %s", __func__, node->name);
357 update_streams_cfg_list(node, platform, streams_output_cfg_list);
358 node = node->next;
359 }
360 } else {
361 ALOGI("%s: could not load output, node is NULL", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700362 }
363
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530364 node = config_find(root, INPUTS_TAG);
365 if (node != NULL) {
366 node = node->first_child;
367 while (node) {
368 ALOGV("%s: loading input %s", __func__, node->name);
369 update_streams_cfg_list(node, platform, streams_input_cfg_list);
370 node = node->next;
371 }
372 } else {
373 ALOGI("%s: could not load input, node is NULL", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700374 }
375}
376
377static void send_app_type_cfg(void *platform, struct mixer *mixer,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530378 struct listnode *streams_output_cfg_list,
379 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700380{
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530381 size_t app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT] = {0};
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700382 int length = 0, i, num_app_types = 0;
383 struct listnode *node;
384 bool update;
385 struct mixer_ctl *ctl = NULL;
386 const char *mixer_ctl_name = "App Type Config";
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530387 struct streams_io_cfg *s_info = NULL;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700388
389 if (!mixer) {
390 ALOGE("%s: mixer is null",__func__);
391 return;
392 }
393 ctl = mixer_get_ctl_by_name(mixer, mixer_ctl_name);
394 if (!ctl) {
395 ALOGE("%s: Could not get ctl for mixer cmd - %s",__func__, mixer_ctl_name);
396 return;
397 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530398 app_type_cfg[length++] = num_app_types;
399
400 if (list_empty(streams_output_cfg_list)) {
401 app_type_cfg[length++] = platform_get_default_app_type_v2(platform, PCM_PLAYBACK);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700402 app_type_cfg[length++] = 48000;
403 app_type_cfg[length++] = 16;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530404 num_app_types += 1;
405 }
406 if (list_empty(streams_input_cfg_list)) {
407 app_type_cfg[length++] = platform_get_default_app_type_v2(platform, PCM_CAPTURE);
408 app_type_cfg[length++] = 48000;
409 app_type_cfg[length++] = 16;
410 num_app_types += 1;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700411 }
412
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700413 list_for_each(node, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530414 s_info = node_to_item(node, struct streams_io_cfg, list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700415 update = true;
416 for (i=0; i<length; i=i+3) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530417 if (app_type_cfg[i+1] == 0)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700418 break;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530419 else if (app_type_cfg[i+1] == (size_t)s_info->app_type_cfg.app_type) {
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530420 if (app_type_cfg[i+2] < (size_t)s_info->app_type_cfg.sample_rate)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530421 app_type_cfg[i+2] = s_info->app_type_cfg.sample_rate;
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530422 if (app_type_cfg[i+3] < (size_t)s_info->app_type_cfg.bit_width)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530423 app_type_cfg[i+3] = s_info->app_type_cfg.bit_width;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700424 update = false;
425 break;
426 }
427 }
428 if (update && ((length + 3) <= MAX_LENGTH_MIXER_CONTROL_IN_INT)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530429 num_app_types += 1;
430 app_type_cfg[length++] = s_info->app_type_cfg.app_type;
431 app_type_cfg[length++] = s_info->app_type_cfg.sample_rate;
432 app_type_cfg[length++] = s_info->app_type_cfg.bit_width;
433 }
434 }
435 list_for_each(node, streams_input_cfg_list) {
436 s_info = node_to_item(node, struct streams_io_cfg, list);
437 update = true;
438 for (i=0; i<length; i=i+3) {
439 if (app_type_cfg[i+1] == 0)
440 break;
441 else if (app_type_cfg[i+1] == (size_t)s_info->app_type_cfg.app_type) {
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530442 if (app_type_cfg[i+2] < (size_t)s_info->app_type_cfg.sample_rate)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530443 app_type_cfg[i+2] = s_info->app_type_cfg.sample_rate;
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530444 if (app_type_cfg[i+3] < (size_t)s_info->app_type_cfg.bit_width)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530445 app_type_cfg[i+3] = s_info->app_type_cfg.bit_width;
446 update = false;
447 break;
448 }
449 }
450 if (update && ((length + 3) <= MAX_LENGTH_MIXER_CONTROL_IN_INT)) {
451 num_app_types += 1;
452 app_type_cfg[length++] = s_info->app_type_cfg.app_type;
453 app_type_cfg[length++] = s_info->app_type_cfg.sample_rate;
454 app_type_cfg[length++] = s_info->app_type_cfg.bit_width;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700455 }
456 }
457 ALOGV("%s: num_app_types: %d", __func__, num_app_types);
458 if (num_app_types) {
459 app_type_cfg[0] = num_app_types;
460 mixer_ctl_set_array(ctl, app_type_cfg, length);
461 }
462}
463
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530464void audio_extn_utils_update_streams_cfg_lists(void *platform,
465 struct mixer *mixer,
466 struct listnode *streams_output_cfg_list,
467 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700468{
469 cnode *root;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530470 char *data = NULL;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700471
472 ALOGV("%s", __func__);
473 list_init(streams_output_cfg_list);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530474 list_init(streams_input_cfg_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700475
476 root = config_node("", "");
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700477 if (root == NULL) {
478 ALOGE("cfg_list, NULL config root");
479 return;
480 }
481
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530482 data = (char *)load_file(AUDIO_IO_POLICY_VENDOR_CONFIG_FILE, NULL);
483 if (data == NULL) {
484 ALOGD("%s: failed to open io config file(%s), trying older config file",
485 __func__, AUDIO_IO_POLICY_VENDOR_CONFIG_FILE);
486 data = (char *)load_file(AUDIO_OUTPUT_POLICY_VENDOR_CONFIG_FILE, NULL);
487 if (data == NULL) {
488 send_app_type_cfg(platform, mixer,
489 streams_output_cfg_list,
490 streams_input_cfg_list);
491 ALOGE("%s: could not load io policy config!", __func__);
yidongh6eb4f752017-04-19 18:20:57 +0800492 free(root);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530493 return;
494 }
495 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700496
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530497 config_load(root, data);
498 load_cfg_list(root, platform, streams_output_cfg_list,
499 streams_input_cfg_list);
500
501 send_app_type_cfg(platform, mixer, streams_output_cfg_list,
502 streams_input_cfg_list);
Alexy Josephaee4fdd2016-01-29 13:02:07 -0800503
504 config_free(root);
yidongh6eb4f752017-04-19 18:20:57 +0800505 free(root);
Alexy Josephaee4fdd2016-01-29 13:02:07 -0800506 free(data);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700507}
508
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530509static void audio_extn_utils_dump_streams_cfg_list(
510 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700511{
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700512 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530513 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700514 struct stream_format *sf_info;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700515 struct stream_sample_rate *ss_info;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530516
517 list_for_each(node_i, streams_cfg_list) {
518 s_info = node_to_item(node_i, struct streams_io_cfg, list);
519 ALOGV("%s: flags-%d, sample_rate-%d, bit_width-%d, app_type-%d",
520 __func__, s_info->flags.out_flags, s_info->app_type_cfg.sample_rate,
521 s_info->app_type_cfg.bit_width, s_info->app_type_cfg.app_type);
522 list_for_each(node_j, &s_info->format_list) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700523 sf_info = node_to_item(node_j, struct stream_format, list);
524 ALOGV("format-%x", sf_info->format);
525 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530526 list_for_each(node_j, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700527 ss_info = node_to_item(node_j, struct stream_sample_rate, list);
528 ALOGV("sample rate-%d", ss_info->sample_rate);
529 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700530 }
531}
532
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530533void audio_extn_utils_dump_streams_cfg_lists(
534 struct listnode *streams_output_cfg_list,
535 struct listnode *streams_input_cfg_list)
536{
537 ALOGV("%s", __func__);
538 audio_extn_utils_dump_streams_cfg_list(streams_output_cfg_list);
539 audio_extn_utils_dump_streams_cfg_list(streams_input_cfg_list);
540}
541
542static void audio_extn_utils_release_streams_cfg_list(
543 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700544{
545 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530546 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700547
548 ALOGV("%s", __func__);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530549
550 while (!list_empty(streams_cfg_list)) {
551 node_i = list_head(streams_cfg_list);
552 s_info = node_to_item(node_i, struct streams_io_cfg, list);
553 while (!list_empty(&s_info->format_list)) {
554 node_j = list_head(&s_info->format_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700555 list_remove(node_j);
556 free(node_to_item(node_j, struct stream_format, list));
557 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530558 while (!list_empty(&s_info->sample_rate_list)) {
559 node_j = list_head(&s_info->sample_rate_list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700560 list_remove(node_j);
561 free(node_to_item(node_j, struct stream_sample_rate, list));
562 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700563 list_remove(node_i);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530564 free(node_to_item(node_i, struct streams_io_cfg, list));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700565 }
566}
567
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530568void audio_extn_utils_release_streams_cfg_lists(
569 struct listnode *streams_output_cfg_list,
570 struct listnode *streams_input_cfg_list)
571{
572 ALOGV("%s", __func__);
573 audio_extn_utils_release_streams_cfg_list(streams_output_cfg_list);
574 audio_extn_utils_release_streams_cfg_list(streams_input_cfg_list);
575}
576
577static bool set_app_type_cfg(struct streams_io_cfg *s_info,
578 struct stream_app_type_cfg *app_type_cfg,
579 uint32_t sample_rate, uint32_t bit_width)
Amit Shekhar6f461b12014-08-01 14:52:58 -0700580 {
581 struct listnode *node_i;
582 struct stream_sample_rate *ss_info;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530583 list_for_each(node_i, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700584 ss_info = node_to_item(node_i, struct stream_sample_rate, list);
585 if ((sample_rate <= ss_info->sample_rate) &&
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530586 (bit_width == s_info->app_type_cfg.bit_width)) {
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700587
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530588 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700589 app_type_cfg->sample_rate = ss_info->sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530590 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700591 ALOGV("%s app_type_cfg->app_type %d, app_type_cfg->sample_rate %d, app_type_cfg->bit_width %d",
592 __func__, app_type_cfg->app_type, app_type_cfg->sample_rate, app_type_cfg->bit_width);
593 return true;
594 }
595 }
596 /*
597 * Reiterate through the list assuming dafault sample rate.
598 * Handles scenario where input sample rate is higher
599 * than all sample rates in list for the input bit width.
600 */
601 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
Apoorv Raghuvanshif59bb222015-02-18 12:23:23 -0800602
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530603 list_for_each(node_i, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700604 ss_info = node_to_item(node_i, struct stream_sample_rate, list);
605 if ((sample_rate <= ss_info->sample_rate) &&
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530606 (bit_width == s_info->app_type_cfg.bit_width)) {
607 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700608 app_type_cfg->sample_rate = sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530609 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Apoorv Raghuvanshif59bb222015-02-18 12:23:23 -0800610 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 -0700611 __func__, app_type_cfg->app_type, app_type_cfg->sample_rate, app_type_cfg->bit_width);
612 return true;
613 }
614 }
615 return false;
616}
617
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530618void audio_extn_utils_update_stream_input_app_type_cfg(void *platform,
619 struct listnode *streams_input_cfg_list,
620 audio_devices_t devices __unused,
621 audio_input_flags_t flags,
622 audio_format_t format,
623 uint32_t sample_rate,
624 uint32_t bit_width,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530625 char* profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530626 struct stream_app_type_cfg *app_type_cfg)
627{
628 struct listnode *node_i, *node_j;
629 struct streams_io_cfg *s_info;
630 struct stream_format *sf_info;
631
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530632 ALOGV("%s: flags: 0x%x, format: 0x%x sample_rate %d, profile %s",
633 __func__, flags, format, sample_rate, profile);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530634
635 list_for_each(node_i, streams_input_cfg_list) {
636 s_info = node_to_item(node_i, struct streams_io_cfg, list);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530637 /* Along with flags do profile matching if set at either end.*/
638 if (s_info->flags.in_flags == flags &&
639 ((profile[0] == '\0' && s_info->profile[0] == '\0') ||
640 strncmp(s_info->profile, profile, sizeof(s_info->profile)) == 0)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530641 list_for_each(node_j, &s_info->format_list) {
642 sf_info = node_to_item(node_j, struct stream_format, list);
643 if (sf_info->format == format) {
644 if (set_app_type_cfg(s_info, app_type_cfg, sample_rate, bit_width))
645 return;
646 }
647 }
648 }
649 }
650 ALOGW("%s: App type could not be selected. Falling back to default", __func__);
651 app_type_cfg->app_type = platform_get_default_app_type_v2(platform, PCM_CAPTURE);
652 app_type_cfg->sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
653 app_type_cfg->bit_width = 16;
654}
655
656void audio_extn_utils_update_stream_output_app_type_cfg(void *platform,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700657 struct listnode *streams_output_cfg_list,
Amit Shekhar1d896042014-10-03 13:16:09 -0700658 audio_devices_t devices,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700659 audio_output_flags_t flags,
660 audio_format_t format,
Amit Shekhar6f461b12014-08-01 14:52:58 -0700661 uint32_t sample_rate,
662 uint32_t bit_width,
Manish Dewangan837dc462015-05-27 10:17:41 +0530663 audio_channel_mask_t channel_mask,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530664 char *profile,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700665 struct stream_app_type_cfg *app_type_cfg)
666{
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +0530667 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530668 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700669 struct stream_format *sf_info;
Manish Dewangan837dc462015-05-27 10:17:41 +0530670 char value[PROPERTY_VALUE_MAX] = {0};
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700671
Ashish Jain058165c2016-09-28 23:18:48 +0530672 if ((bit_width >= 24) &&
Amit Shekhar1d896042014-10-03 13:16:09 -0700673 (devices & AUDIO_DEVICE_OUT_SPEAKER)) {
Amit Shekhar5a39c912014-10-14 15:39:30 -0700674 int32_t bw = platform_get_snd_device_bit_width(SND_DEVICE_OUT_SPEAKER);
675 if (-ENOSYS != bw)
676 bit_width = (uint32_t)bw;
Amit Shekhar1d896042014-10-03 13:16:09 -0700677 sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
678 ALOGI("%s Allowing 24-bit playback on speaker ONLY at default sampling rate", __func__);
679 }
680
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -0700681 property_get("vendor.audio.playback.mch.downsample",value,"");
Manish Dewangan837dc462015-05-27 10:17:41 +0530682 if (!strncmp("true", value, sizeof("true"))) {
683 if ((popcount(channel_mask) > 2) &&
684 (sample_rate > CODEC_BACKEND_DEFAULT_SAMPLE_RATE) &&
685 !(flags & AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH)) {
686 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
687 ALOGD("%s: MCH session defaulting sample rate to %d",
688 __func__, sample_rate);
689 }
690 }
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +0530691
692 /* Set sampling rate to 176.4 for DSD64
693 * and 352.8Khz for DSD128.
694 * Set Bit Width to 16. output will be 16 bit
695 * post DoP in ASM.
696 */
697 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH) &&
698 (format == AUDIO_FORMAT_DSD)) {
699 bit_width = 16;
700 if (sample_rate == INPUT_SAMPLING_RATE_DSD64)
701 sample_rate = OUTPUT_SAMPLING_RATE_DSD64;
702 else if (sample_rate == INPUT_SAMPLING_RATE_DSD128)
703 sample_rate = OUTPUT_SAMPLING_RATE_DSD128;
704 }
705
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +0530706 if(devices & AUDIO_DEVICE_OUT_ALL_A2DP) {
707 //TODO: Handle fractional sampling rate configuration for LL
708 audio_extn_a2dp_get_apptype_params(&sample_rate, &bit_width);
709 ALOGI("%s using %d sampling rate %d bit width for A2DP CoPP",
710 __func__, sample_rate, bit_width);
711 }
712
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800713 ALOGV("%s: flags: %x, format: %x sample_rate %d, profile %s, app_type %d",
714 __func__, flags, format, sample_rate, profile, app_type_cfg->app_type);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700715 list_for_each(node_i, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530716 s_info = node_to_item(node_i, struct streams_io_cfg, list);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530717 /* Along with flags do profile matching if set at either end.*/
718 if (s_info->flags.out_flags == flags &&
719 ((profile[0] == '\0' && s_info->profile[0] == '\0') ||
720 strncmp(s_info->profile, profile, sizeof(s_info->profile)) == 0)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530721 list_for_each(node_j, &s_info->format_list) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700722 sf_info = node_to_item(node_j, struct stream_format, list);
723 if (sf_info->format == format) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530724 if (set_app_type_cfg(s_info, app_type_cfg, sample_rate, bit_width))
Amit Shekhar6f461b12014-08-01 14:52:58 -0700725 return;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700726 }
727 }
728 }
729 }
730 list_for_each(node_i, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530731 s_info = node_to_item(node_i, struct streams_io_cfg, list);
732 if (s_info->flags.out_flags == AUDIO_OUTPUT_FLAG_PRIMARY) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700733 ALOGV("Compatible output profile not found.");
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530734 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
735 app_type_cfg->sample_rate = s_info->app_type_cfg.sample_rate;
736 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700737 ALOGV("%s Default to primary output: App type: %d sample_rate %d",
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530738 __func__, s_info->app_type_cfg.app_type, app_type_cfg->sample_rate);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700739 return;
740 }
741 }
742 ALOGW("%s: App type could not be selected. Falling back to default", __func__);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700743 app_type_cfg->app_type = platform_get_default_app_type(platform);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700744 app_type_cfg->sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700745 app_type_cfg->bit_width = 16;
746}
747
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +0530748static bool audio_is_this_native_usecase(struct audio_usecase *uc)
749{
750 bool native_usecase = false;
751 struct stream_out *out = (struct stream_out*) uc->stream.out;
752
753 if (PCM_PLAYBACK == uc->type && out != NULL &&
754 NATIVE_AUDIO_MODE_INVALID != platform_get_native_support() &&
755 is_offload_usecase(uc->id) &&
756 (out->sample_rate == OUTPUT_SAMPLING_RATE_44100))
757 native_usecase = true;
758
759 return native_usecase;
760}
761
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800762
763static inline bool audio_is_vr_mode_on(struct audio_device *(__attribute__((unused)) adev))
764{
765 return adev->vr_audio_mode_enabled;
766}
767
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530768void audio_extn_utils_update_stream_app_type_cfg_for_usecase(
769 struct audio_device *adev,
770 struct audio_usecase *usecase)
771{
772 ALOGV("%s", __func__);
773
774 switch(usecase->type) {
775 case PCM_PLAYBACK:
776 audio_extn_utils_update_stream_output_app_type_cfg(adev->platform,
777 &adev->streams_output_cfg_list,
778 usecase->stream.out->devices,
779 usecase->stream.out->flags,
780 usecase->stream.out->format,
781 usecase->stream.out->sample_rate,
782 usecase->stream.out->bit_width,
783 usecase->stream.out->channel_mask,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530784 usecase->stream.out->profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530785 &usecase->stream.out->app_type_cfg);
786 ALOGV("%s Selected apptype: %d", __func__, usecase->stream.out->app_type_cfg.app_type);
787 break;
788 case PCM_CAPTURE:
789 audio_extn_utils_update_stream_input_app_type_cfg(adev->platform,
790 &adev->streams_input_cfg_list,
791 usecase->stream.in->device,
792 usecase->stream.in->flags,
793 usecase->stream.in->format,
794 usecase->stream.in->sample_rate,
795 usecase->stream.in->bit_width,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530796 usecase->stream.in->profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530797 &usecase->stream.in->app_type_cfg);
798 ALOGV("%s Selected apptype: %d", __func__, usecase->stream.in->app_type_cfg.app_type);
799 break;
800 default:
801 ALOGE("%s: app type cfg not supported for usecase type (%d)",
802 __func__, usecase->type);
803 }
804}
805
Siena Richard7c2db772016-12-21 11:32:34 -0800806static int send_app_type_cfg_for_device(struct audio_device *adev,
807 struct audio_usecase *usecase,
808 int split_snd_device)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700809{
810 char mixer_ctl_name[MAX_LENGTH_MIXER_CONTROL_IN_INT];
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530811 size_t app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT] = {0};
812 int len = 0, rc;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700813 struct mixer_ctl *ctl;
Siena Richard7c2db772016-12-21 11:32:34 -0800814 int pcm_device_id = 0, acdb_dev_id, app_type;
815 int snd_device = split_snd_device, snd_device_be_idx = -1;
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +0530816 int32_t sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Manish Dewangan837dc462015-05-27 10:17:41 +0530817 char value[PROPERTY_VALUE_MAX] = {0};
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700818
Siena Richard7c2db772016-12-21 11:32:34 -0800819 ALOGV("%s: usecase->out_snd_device %s, usecase->in_snd_device %s, split_snd_device %s",
820 __func__, platform_get_snd_device_name(usecase->out_snd_device),
821 platform_get_snd_device_name(usecase->in_snd_device),
822 platform_get_snd_device_name(split_snd_device));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700823
Srikanth Uyyala9d551402015-08-25 16:03:42 +0530824 if (usecase->type != PCM_PLAYBACK && usecase->type != PCM_CAPTURE) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530825 ALOGE("%s: not a playback/capture path, no need to cfg app type", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700826 rc = 0;
827 goto exit_send_app_type_cfg;
828 }
829 if ((usecase->id != USECASE_AUDIO_PLAYBACK_DEEP_BUFFER) &&
830 (usecase->id != USECASE_AUDIO_PLAYBACK_LOW_LATENCY) &&
831 (usecase->id != USECASE_AUDIO_PLAYBACK_MULTI_CH) &&
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800832 (usecase->id != USECASE_AUDIO_PLAYBACK_ULL) &&
Srikanth Uyyala9d551402015-08-25 16:03:42 +0530833 (!is_offload_usecase(usecase->id)) &&
834 (usecase->type != PCM_CAPTURE)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530835 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 -0700836 rc = 0;
837 goto exit_send_app_type_cfg;
838 }
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800839
840 //if VR is active then only send the mixer control
841 if (usecase->id == USECASE_AUDIO_PLAYBACK_ULL && !audio_is_vr_mode_on(adev)) {
842 ALOGI("ULL doesnt need sending app type cfg, returning");
843 rc = 0;
844 goto exit_send_app_type_cfg;
845 }
846
Srikanth Uyyala9d551402015-08-25 16:03:42 +0530847 if (usecase->type == PCM_PLAYBACK) {
Ben Romberger1fafdde2015-09-09 19:43:15 -0700848 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_PLAYBACK);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530849 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
850 "Audio Stream %d App Type Cfg", pcm_device_id);
Ben Romberger1fafdde2015-09-09 19:43:15 -0700851 } else if (usecase->type == PCM_CAPTURE) {
Ben Romberger1fafdde2015-09-09 19:43:15 -0700852 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_CAPTURE);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530853 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
854 "Audio Stream Capture %d App Type Cfg", pcm_device_id);
Srikanth Uyyala9d551402015-08-25 16:03:42 +0530855 }
Siena Richard7c2db772016-12-21 11:32:34 -0800856
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700857 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
858 if (!ctl) {
859 ALOGE("%s: Could not get ctl for mixer cmd - %s", __func__,
860 mixer_ctl_name);
861 rc = -EINVAL;
862 goto exit_send_app_type_cfg;
863 }
Aditya Bavanari701a6992017-03-30 19:17:16 +0530864 snd_device = platform_get_spkr_prot_snd_device(snd_device);
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +0530865
Rohit Kumar1181b292017-01-31 18:07:17 +0530866 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
867 if (acdb_dev_id <= 0) {
868 ALOGE("%s: Couldn't get the acdb dev id", __func__);
869 rc = -EINVAL;
870 goto exit_send_app_type_cfg;
871 }
872
Siena Richard7c2db772016-12-21 11:32:34 -0800873 snd_device_be_idx = platform_get_snd_device_backend_index(snd_device);
874 if (snd_device_be_idx < 0) {
875 ALOGE("%s: Couldn't get the backend index for snd device %s ret=%d",
876 __func__, platform_get_snd_device_name(snd_device),
877 snd_device_be_idx);
878 }
879
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530880 if ((usecase->type == PCM_PLAYBACK) && (usecase->stream.out != NULL)) {
Manish Dewangan837dc462015-05-27 10:17:41 +0530881
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -0700882 property_get("vendor.audio.playback.mch.downsample",value,"");
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530883 if (!strncmp("true", value, sizeof("true"))) {
884 if ((popcount(usecase->stream.out->channel_mask) > 2) &&
885 (usecase->stream.out->app_type_cfg.sample_rate > CODEC_BACKEND_DEFAULT_SAMPLE_RATE) &&
886 !(usecase->stream.out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH))
887 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
888 }
Ashish Jainc02430d2016-01-04 10:42:43 +0530889
Ashish Jain4826f6c2017-02-06 13:33:20 +0530890 if (usecase->stream.out->devices & AUDIO_DEVICE_OUT_SPEAKER) {
Mingming Yin21854652016-04-13 11:54:02 -0700891 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Ashish Jaina052e572016-11-07 16:41:07 +0530892 } else if ((snd_device == SND_DEVICE_OUT_HDMI ||
893 snd_device == SND_DEVICE_OUT_USB_HEADSET ||
894 snd_device == SND_DEVICE_OUT_DISPLAY_PORT) &&
895 (usecase->stream.out->sample_rate >= OUTPUT_SAMPLING_RATE_44100)) {
896 /*
897 * To best utlize DSP, check if the stream sample rate is supported/multiple of
898 * configured device sample rate, if not update the COPP rate to be equal to the
899 * device sample rate, else open COPP at stream sample rate
900 */
901 platform_check_and_update_copp_sample_rate(adev->platform, snd_device,
902 usecase->stream.out->sample_rate,
903 &usecase->stream.out->app_type_cfg.sample_rate);
Ashish Jain4826f6c2017-02-06 13:33:20 +0530904 } else if (((snd_device != SND_DEVICE_OUT_HEADPHONES_44_1 &&
905 !audio_is_this_native_usecase(usecase)) &&
Mingming Yin21854652016-04-13 11:54:02 -0700906 usecase->stream.out->sample_rate == OUTPUT_SAMPLING_RATE_44100) ||
907 (usecase->stream.out->sample_rate < OUTPUT_SAMPLING_RATE_44100)) {
Ashish Jain4826f6c2017-02-06 13:33:20 +0530908 /* Reset to default if no native stream is active*/
Mingming Yin21854652016-04-13 11:54:02 -0700909 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
910 }
911 sample_rate = usecase->stream.out->app_type_cfg.sample_rate;
912
Siena Richard7c2db772016-12-21 11:32:34 -0800913 app_type = usecase->stream.out->app_type_cfg.app_type;
914 app_type_cfg[len++] = app_type;
Mingming Yin21854652016-04-13 11:54:02 -0700915 app_type_cfg[len++] = acdb_dev_id;
916 if (((usecase->stream.out->format == AUDIO_FORMAT_E_AC3) ||
Ben Romberger1aaaf862017-04-06 17:49:46 -0700917 (usecase->stream.out->format == AUDIO_FORMAT_E_AC3_JOC) ||
918 (usecase->stream.out->format == AUDIO_FORMAT_DOLBY_TRUEHD))
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530919 && audio_extn_passthru_is_passthrough_stream(usecase->stream.out)) {
Naresh Tanniru3a406772017-05-10 13:09:05 -0700920
921 sample_rate = sample_rate * 4;
922 if (sample_rate > HDMI_PASSTHROUGH_MAX_SAMPLE_RATE)
923 sample_rate = HDMI_PASSTHROUGH_MAX_SAMPLE_RATE;
Mingming Yin21854652016-04-13 11:54:02 -0700924 }
Naresh Tanniru3a406772017-05-10 13:09:05 -0700925 app_type_cfg[len++] = sample_rate;
926
Siena Richard7c2db772016-12-21 11:32:34 -0800927 if (snd_device_be_idx > 0)
928 app_type_cfg[len++] = snd_device_be_idx;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530929
Siena Richard7c2db772016-12-21 11:32:34 -0800930 ALOGI("%s PLAYBACK 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
933 } else if ((usecase->type == PCM_CAPTURE) && (usecase->stream.in != NULL)) {
Siena Richard7c2db772016-12-21 11:32:34 -0800934 app_type = usecase->stream.in->app_type_cfg.app_type;
935 app_type_cfg[len++] = app_type;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530936 app_type_cfg[len++] = acdb_dev_id;
Siena Richard7c2db772016-12-21 11:32:34 -0800937 sample_rate = usecase->stream.in->app_type_cfg.sample_rate;
938 app_type_cfg[len++] = sample_rate;
939 if (snd_device_be_idx > 0)
940 app_type_cfg[len++] = snd_device_be_idx;
941 ALOGI("%s CAPTURE app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
942 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530943 } else {
Siena Richard7c2db772016-12-21 11:32:34 -0800944 app_type = platform_get_default_app_type_v2(adev->platform, usecase->type);
945 app_type_cfg[len++] = app_type;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530946 app_type_cfg[len++] = acdb_dev_id;
947 app_type_cfg[len++] = sample_rate;
Siena Richard7c2db772016-12-21 11:32:34 -0800948 if (snd_device_be_idx > 0)
949 app_type_cfg[len++] = snd_device_be_idx;
950 ALOGI("%s default app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
951 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Ashish Jainc02430d2016-01-04 10:42:43 +0530952 }
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +0530953
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700954 mixer_ctl_set_array(ctl, app_type_cfg, len);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700955 rc = 0;
956exit_send_app_type_cfg:
957 return rc;
958}
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700959
Siena Richard7c2db772016-12-21 11:32:34 -0800960int audio_extn_utils_send_app_type_cfg(struct audio_device *adev,
961 struct audio_usecase *usecase)
962{
963 int i, num_devices = 0;
964 snd_device_t new_snd_devices[SND_DEVICE_OUT_END] = {0};
965 int rc = 0;
966
967 switch (usecase->type) {
968 case PCM_PLAYBACK:
969 ALOGD("%s: usecase->out_snd_device %s",
970 __func__, platform_get_snd_device_name(usecase->out_snd_device));
971 /* check for out combo device */
972 if (platform_split_snd_device(adev->platform,
973 usecase->out_snd_device,
974 &num_devices, new_snd_devices)) {
975 new_snd_devices[0] = usecase->out_snd_device;
976 num_devices = 1;
977 }
978 break;
979 case PCM_CAPTURE:
980 ALOGD("%s: usecase->in_snd_device %s",
981 __func__, platform_get_snd_device_name(usecase->in_snd_device));
982 /* check for in combo device */
983 if (platform_split_snd_device(adev->platform,
984 usecase->in_snd_device,
985 &num_devices, new_snd_devices)) {
986 new_snd_devices[0] = usecase->in_snd_device;
987 num_devices = 1;
988 }
989 break;
990 default:
991 ALOGI("%s: not a playback/capture path, no need to cfg app type", __func__);
992 rc = 0;
993 break;
994 }
995
996 for (i = 0; i < num_devices; i++) {
997 rc = send_app_type_cfg_for_device(adev, usecase, new_snd_devices[i]);
998 if (rc)
999 break;
1000 }
1001
1002 return rc;
1003}
1004
Preetam Singh Ranawat9519e9c2015-11-18 16:05:55 +05301005int read_line_from_file(const char *path, char *buf, size_t count)
1006{
1007 char * fgets_ret;
1008 FILE * fd;
1009 int rv;
1010
1011 fd = fopen(path, "r");
1012 if (fd == NULL)
1013 return -1;
1014
1015 fgets_ret = fgets(buf, (int)count, fd);
1016 if (NULL != fgets_ret) {
1017 rv = (int)strlen(buf);
1018 } else {
1019 rv = ferror(fd);
1020 }
1021 fclose(fd);
1022
1023 return rv;
1024}
1025
Ashish Jainf1eaa582016-05-23 20:54:24 +05301026/*Translates ALSA formats to AOSP PCM formats*/
1027audio_format_t alsa_format_to_hal(uint32_t alsa_format)
1028{
1029 audio_format_t format;
1030
1031 switch(alsa_format) {
1032 case SNDRV_PCM_FORMAT_S16_LE:
1033 format = AUDIO_FORMAT_PCM_16_BIT;
1034 break;
1035 case SNDRV_PCM_FORMAT_S24_3LE:
1036 format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
1037 break;
1038 case SNDRV_PCM_FORMAT_S24_LE:
1039 format = AUDIO_FORMAT_PCM_8_24_BIT;
1040 break;
1041 case SNDRV_PCM_FORMAT_S32_LE:
1042 format = AUDIO_FORMAT_PCM_32_BIT;
1043 break;
1044 default:
1045 ALOGW("Incorrect ALSA format");
1046 format = AUDIO_FORMAT_INVALID;
1047 }
1048 return format;
1049}
1050
1051/*Translates hal format (AOSP) to alsa formats*/
1052uint32_t hal_format_to_alsa(audio_format_t hal_format)
1053{
1054 uint32_t alsa_format;
1055
1056 switch (hal_format) {
1057 case AUDIO_FORMAT_PCM_32_BIT: {
1058 if (platform_supports_true_32bit())
1059 alsa_format = SNDRV_PCM_FORMAT_S32_LE;
1060 else
1061 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1062 }
1063 break;
1064 case AUDIO_FORMAT_PCM_8_BIT:
1065 alsa_format = SNDRV_PCM_FORMAT_S8;
1066 break;
1067 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
1068 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1069 break;
1070 case AUDIO_FORMAT_PCM_8_24_BIT: {
1071 if (platform_supports_true_32bit())
1072 alsa_format = SNDRV_PCM_FORMAT_S32_LE;
1073 else
1074 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1075 }
1076 break;
1077 case AUDIO_FORMAT_PCM_FLOAT:
1078 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1079 break;
1080 default:
1081 case AUDIO_FORMAT_PCM_16_BIT:
1082 alsa_format = SNDRV_PCM_FORMAT_S16_LE;
1083 break;
1084 }
1085 return alsa_format;
1086}
1087
Ashish Jain83a6cc22016-06-28 14:34:17 +05301088/*Translates PCM formats to AOSP formats*/
1089audio_format_t pcm_format_to_hal(uint32_t pcm_format)
1090{
1091 audio_format_t format = AUDIO_FORMAT_INVALID;
1092
1093 switch(pcm_format) {
1094 case PCM_FORMAT_S16_LE:
1095 format = AUDIO_FORMAT_PCM_16_BIT;
1096 break;
1097 case PCM_FORMAT_S24_3LE:
1098 format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
1099 break;
1100 case PCM_FORMAT_S24_LE:
1101 format = AUDIO_FORMAT_PCM_8_24_BIT;
1102 break;
1103 case PCM_FORMAT_S32_LE:
1104 format = AUDIO_FORMAT_PCM_32_BIT;
1105 break;
1106 default:
1107 ALOGW("Incorrect PCM format");
1108 format = AUDIO_FORMAT_INVALID;
1109 }
1110 return format;
1111}
1112
1113/*Translates hal format (AOSP) to alsa formats*/
1114uint32_t hal_format_to_pcm(audio_format_t hal_format)
1115{
1116 uint32_t pcm_format;
1117
1118 switch (hal_format) {
1119 case AUDIO_FORMAT_PCM_32_BIT:
1120 case AUDIO_FORMAT_PCM_8_24_BIT:
1121 case AUDIO_FORMAT_PCM_FLOAT: {
1122 if (platform_supports_true_32bit())
1123 pcm_format = PCM_FORMAT_S32_LE;
1124 else
1125 pcm_format = PCM_FORMAT_S24_3LE;
1126 }
1127 break;
1128 case AUDIO_FORMAT_PCM_8_BIT:
1129 pcm_format = PCM_FORMAT_S8;
1130 break;
1131 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
1132 pcm_format = PCM_FORMAT_S24_3LE;
1133 break;
1134 default:
1135 case AUDIO_FORMAT_PCM_16_BIT:
1136 pcm_format = PCM_FORMAT_S16_LE;
1137 break;
1138 }
1139 return pcm_format;
1140}
1141
Ashish Jainf1eaa582016-05-23 20:54:24 +05301142uint32_t get_alsa_fragment_size(uint32_t bytes_per_sample,
1143 uint32_t sample_rate,
1144 uint32_t noOfChannels)
1145{
1146 uint32_t fragment_size = 0;
1147 uint32_t pcm_offload_time = PCM_OFFLOAD_BUFFER_DURATION;
1148
1149 fragment_size = (pcm_offload_time
1150 * sample_rate
1151 * bytes_per_sample
1152 * noOfChannels)/1000;
1153 if (fragment_size < MIN_PCM_OFFLOAD_FRAGMENT_SIZE)
1154 fragment_size = MIN_PCM_OFFLOAD_FRAGMENT_SIZE;
1155 else if (fragment_size > MAX_PCM_OFFLOAD_FRAGMENT_SIZE)
1156 fragment_size = MAX_PCM_OFFLOAD_FRAGMENT_SIZE;
1157 /*To have same PCM samples for all channels, the buffer size requires to
1158 *be multiple of (number of channels * bytes per sample)
1159 *For writes to succeed, the buffer must be written at address which is multiple of 32
1160 */
1161 fragment_size = ALIGN(fragment_size, (bytes_per_sample * noOfChannels * 32));
1162
1163 ALOGI("PCM offload Fragment size to %d bytes", fragment_size);
1164 return fragment_size;
1165}
1166
1167/* Calculates the fragment size required to configure compress session.
1168 * Based on the alsa format selected, decide if conversion is needed in
1169
1170 * HAL ( e.g. convert AUDIO_FORMAT_PCM_FLOAT input format to
1171 * AUDIO_FORMAT_PCM_24_BIT_PACKED before writing to the compress driver.
1172 */
1173void audio_extn_utils_update_direct_pcm_fragment_size(struct stream_out *out)
1174{
Ashish Jain83a6cc22016-06-28 14:34:17 +05301175 audio_format_t dst_format = out->hal_op_format;
1176 audio_format_t src_format = out->hal_ip_format;
Ashish Jainf1eaa582016-05-23 20:54:24 +05301177 uint32_t hal_op_bytes_per_sample = audio_bytes_per_sample(dst_format);
1178 uint32_t hal_ip_bytes_per_sample = audio_bytes_per_sample(src_format);
1179
1180 out->compr_config.fragment_size =
1181 get_alsa_fragment_size(hal_op_bytes_per_sample,
1182 out->sample_rate,
1183 popcount(out->channel_mask));
1184
1185 if ((src_format != dst_format) &&
1186 hal_op_bytes_per_sample != hal_ip_bytes_per_sample) {
1187
Ashish Jain83a6cc22016-06-28 14:34:17 +05301188 out->hal_fragment_size =
Ashish Jainf1eaa582016-05-23 20:54:24 +05301189 ((out->compr_config.fragment_size * hal_ip_bytes_per_sample) /
1190 hal_op_bytes_per_sample);
1191 ALOGI("enable conversion hal_input_fragment_size is %d src_format %x dst_format %x",
Ashish Jain83a6cc22016-06-28 14:34:17 +05301192 out->hal_fragment_size, src_format, dst_format);
Ashish Jainf1eaa582016-05-23 20:54:24 +05301193 } else {
Ashish Jain83a6cc22016-06-28 14:34:17 +05301194 out->hal_fragment_size = out->compr_config.fragment_size;
Ashish Jainf1eaa582016-05-23 20:54:24 +05301195 }
1196}
1197
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301198/* converts pcm format 24_8 to 8_24 inplace */
1199size_t audio_extn_utils_convert_format_24_8_to_8_24(void *buf, size_t bytes)
1200{
1201 size_t i = 0;
1202 int *int_buf_stream = buf;
1203
1204 if ((bytes % 4) != 0) {
1205 ALOGE("%s: wrong inout buffer! ... is not 32 bit aligned ", __func__);
1206 return -EINVAL;
1207 }
1208
1209 for (; i < (bytes / 4); i++)
1210 int_buf_stream[i] >>= 8;
1211
1212 return bytes;
1213}
1214
1215int get_snd_codec_id(audio_format_t format)
1216{
1217 int id = 0;
1218
1219 switch (format & AUDIO_FORMAT_MAIN_MASK) {
1220 case AUDIO_FORMAT_MP3:
1221 id = SND_AUDIOCODEC_MP3;
1222 break;
1223 case AUDIO_FORMAT_AAC:
1224 id = SND_AUDIOCODEC_AAC;
1225 break;
1226 case AUDIO_FORMAT_AAC_ADTS:
1227 id = SND_AUDIOCODEC_AAC;
1228 break;
Arun Kumar Dasari3b174182016-12-27 13:01:14 +05301229 case AUDIO_FORMAT_AAC_LATM:
1230 id = SND_AUDIOCODEC_AAC;
1231 break;
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301232 case AUDIO_FORMAT_PCM:
1233 id = SND_AUDIOCODEC_PCM;
1234 break;
1235 case AUDIO_FORMAT_FLAC:
1236 id = SND_AUDIOCODEC_FLAC;
1237 break;
1238 case AUDIO_FORMAT_ALAC:
1239 id = SND_AUDIOCODEC_ALAC;
1240 break;
1241 case AUDIO_FORMAT_APE:
1242 id = SND_AUDIOCODEC_APE;
1243 break;
1244 case AUDIO_FORMAT_VORBIS:
1245 id = SND_AUDIOCODEC_VORBIS;
1246 break;
1247 case AUDIO_FORMAT_WMA:
1248 id = SND_AUDIOCODEC_WMA;
1249 break;
1250 case AUDIO_FORMAT_WMA_PRO:
1251 id = SND_AUDIOCODEC_WMA_PRO;
1252 break;
Satish Babu Patakokila0c313922016-12-08 12:07:08 +05301253 case AUDIO_FORMAT_MP2:
1254 id = SND_AUDIOCODEC_MP2;
1255 break;
Satish Babu Patakokila915ecba2017-01-10 17:43:56 +05301256 case AUDIO_FORMAT_AC3:
1257 id = SND_AUDIOCODEC_AC3;
1258 break;
1259 case AUDIO_FORMAT_E_AC3:
1260 case AUDIO_FORMAT_E_AC3_JOC:
1261 id = SND_AUDIOCODEC_EAC3;
1262 break;
1263 case AUDIO_FORMAT_DTS:
1264 case AUDIO_FORMAT_DTS_HD:
1265 id = SND_AUDIOCODEC_DTS;
1266 break;
Ben Romberger1aaaf862017-04-06 17:49:46 -07001267 case AUDIO_FORMAT_DOLBY_TRUEHD:
1268 id = SND_AUDIOCODEC_TRUEHD;
1269 break;
Naresh Tanniru928f0862017-04-07 16:44:23 -07001270 case AUDIO_FORMAT_IEC61937:
1271 id = SND_AUDIOCODEC_IEC61937;
1272 break;
Preetam Singh Ranawat4277a5a2017-01-18 19:02:24 +05301273 case AUDIO_FORMAT_DSD:
1274 id = SND_AUDIOCODEC_DSD;
1275 break;
Dhanalakshmi Siddani18737932016-11-29 17:33:17 +05301276 case AUDIO_FORMAT_APTX:
1277 id = SND_AUDIOCODEC_APTX;
1278 break;
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301279 default:
1280 ALOGE("%s: Unsupported audio format :%x", __func__, format);
1281 }
1282
1283 return id;
1284}
1285
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001286void audio_extn_utils_send_audio_calibration(struct audio_device *adev,
1287 struct audio_usecase *usecase)
1288{
1289 int type = usecase->type;
1290
Dhananjay Kumar14245982017-01-16 20:21:00 +05301291 if (type == PCM_PLAYBACK && usecase->stream.out != NULL) {
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001292 struct stream_out *out = usecase->stream.out;
1293 int snd_device = usecase->out_snd_device;
1294 snd_device = (snd_device == SND_DEVICE_OUT_SPEAKER) ?
Xiaojun Sang040cc9f2015-08-03 19:38:28 +08001295 platform_get_spkr_prot_snd_device(snd_device) : snd_device;
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05301296 platform_send_audio_calibration(adev->platform, usecase,
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001297 out->app_type_cfg.app_type,
Preetam Singh Ranawat61716b12015-12-14 11:55:24 +05301298 usecase->stream.out->app_type_cfg.sample_rate);
Dhananjay Kumar14245982017-01-16 20:21:00 +05301299 } else if (type == PCM_CAPTURE && usecase->stream.in != NULL) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301300 platform_send_audio_calibration(adev->platform, usecase,
1301 usecase->stream.in->app_type_cfg.app_type,
1302 usecase->stream.in->app_type_cfg.sample_rate);
Vidyakumar Athota653aaef2017-03-16 11:11:31 -07001303 } else if (type == PCM_HFP_CALL || type == PCM_CAPTURE) {
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05301304 /* when app type is default. the sample rate is not used to send cal */
1305 platform_send_audio_calibration(adev->platform, usecase,
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301306 platform_get_default_app_type_v2(adev->platform, usecase->type),
1307 48000);
Vidyakumar Athotae57f6002017-03-01 13:13:16 -08001308 } else {
1309 /* No need to send audio calibration for voice and voip call usecases */
1310 if ((type != VOICE_CALL) && (type != VOIP_CALL))
1311 ALOGW("%s: No audio calibration for usecase type = %d", __func__, type);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001312 }
1313}
1314
Ben Rombergera04fabc2014-11-14 12:16:03 -08001315// Base64 Encode and Decode
1316// Not all features supported. This must be used only with following conditions.
1317// Decode Modes: Support with and without padding
1318// CRLF not handling. So no CRLF in string to decode.
1319// Encode Modes: Supports only padding
1320int b64decode(char *inp, int ilen, uint8_t* outp)
1321{
1322 int i, j, k, ii, num;
1323 int rem, pcnt;
1324 uint32_t res=0;
1325 uint8_t getIndex[MAX_BASEINDEX_LEN];
1326 uint8_t tmp, cflag;
1327
1328 if(inp == NULL || outp == NULL || ilen <= 0) {
1329 ALOGE("[%s] received NULL pointer or zero length",__func__);
1330 return -1;
1331 }
1332
1333 memset(getIndex, MAX_BASEINDEX_LEN-1, sizeof(getIndex));
1334 for(i=0;i<BASE_TABLE_SIZE;i++) {
1335 getIndex[(uint8_t)bTable[i]] = (uint8_t)i;
1336 }
1337 getIndex[(uint8_t)'=']=0;
1338
1339 j=0;k=0;
1340 num = ilen/4;
1341 rem = ilen%4;
1342 if(rem==0)
1343 num = num-1;
1344 cflag=0;
1345 for(i=0; i<num; i++) {
1346 res=0;
1347 for(ii=0;ii<4;ii++) {
1348 res = res << 6;
1349 tmp = getIndex[(uint8_t)inp[j++]];
1350 res = res | tmp;
1351 cflag = cflag | tmp;
1352 }
1353 outp[k++] = (res >> 16)&0xFF;
1354 outp[k++] = (res >> 8)&0xFF;
1355 outp[k++] = res & 0xFF;
1356 }
1357
1358 // Handle last bytes special
1359 pcnt=0;
1360 if(rem == 0) {
1361 //With padding or full data
1362 res = 0;
1363 for(ii=0;ii<4;ii++) {
1364 if(inp[j] == '=')
1365 pcnt++;
1366 res = res << 6;
1367 tmp = getIndex[(uint8_t)inp[j++]];
1368 res = res | tmp;
1369 cflag = cflag | tmp;
1370 }
1371 outp[k++] = res >> 16;
1372 if(pcnt == 2)
1373 goto done;
1374 outp[k++] = (res>>8)&0xFF;
1375 if(pcnt == 1)
1376 goto done;
1377 outp[k++] = res&0xFF;
1378 } else {
1379 //without padding
1380 res = 0;
1381 for(i=0;i<rem;i++) {
1382 res = res << 6;
1383 tmp = getIndex[(uint8_t)inp[j++]];
1384 res = res | tmp;
1385 cflag = cflag | tmp;
1386 }
1387 for(i=rem;i<4;i++) {
1388 res = res << 6;
1389 pcnt++;
1390 }
1391 outp[k++] = res >> 16;
1392 if(pcnt == 2)
1393 goto done;
1394 outp[k++] = (res>>8)&0xFF;
1395 if(pcnt == 1)
1396 goto done;
1397 outp[k++] = res&0xFF;
1398 }
1399done:
1400 if(cflag == 0xFF) {
1401 ALOGE("[%s] base64 decode failed. Invalid character found %s",
1402 __func__, inp);
1403 return 0;
1404 }
1405 return k;
1406}
1407
1408int b64encode(uint8_t *inp, int ilen, char* outp)
1409{
1410 int i,j,k, num;
1411 int rem=0;
1412 uint32_t res=0;
1413
1414 if(inp == NULL || outp == NULL || ilen<=0) {
1415 ALOGE("[%s] received NULL pointer or zero input length",__func__);
1416 return -1;
1417 }
1418
1419 num = ilen/3;
1420 rem = ilen%3;
1421 j=0;k=0;
1422 for(i=0; i<num; i++) {
1423 //prepare index
1424 res = inp[j++]<<16;
1425 res = res | inp[j++]<<8;
1426 res = res | inp[j++];
1427 //get output map from index
1428 outp[k++] = (char) bTable[(res>>18)&0x3F];
1429 outp[k++] = (char) bTable[(res>>12)&0x3F];
1430 outp[k++] = (char) bTable[(res>>6)&0x3F];
1431 outp[k++] = (char) bTable[res&0x3F];
1432 }
1433
1434 switch(rem) {
1435 case 1:
1436 res = inp[j++]<<16;
1437 outp[k++] = (char) bTable[res>>18];
1438 outp[k++] = (char) bTable[(res>>12)&0x3F];
1439 //outp[k++] = '=';
1440 //outp[k++] = '=';
1441 break;
1442 case 2:
1443 res = inp[j++]<<16;
1444 res = res | inp[j++]<<8;
1445 outp[k++] = (char) bTable[res>>18];
1446 outp[k++] = (char) bTable[(res>>12)&0x3F];
1447 outp[k++] = (char) bTable[(res>>6)&0x3F];
1448 //outp[k++] = '=';
1449 break;
1450 default:
1451 break;
1452 }
Ben Rombergera04fabc2014-11-14 12:16:03 -08001453 outp[k] = '\0';
1454 return k;
1455}
Ashish Jain81eb2a82015-05-13 10:52:34 +05301456
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301457
1458int audio_extn_utils_get_codec_version(const char *snd_card_name,
1459 int card_num,
1460 char *codec_version)
1461{
1462 char procfs_path[50];
1463 FILE *fp;
1464
1465 if (strstr(snd_card_name, "tasha")) {
1466 snprintf(procfs_path, sizeof(procfs_path),
1467 "/proc/asound/card%d/codecs/tasha/version", card_num);
1468 if ((fp = fopen(procfs_path, "r")) != NULL) {
1469 fgets(codec_version, CODEC_VERSION_MAX_LENGTH, fp);
1470 fclose(fp);
1471 } else {
1472 ALOGE("%s: ERROR. cannot open %s", __func__, procfs_path);
1473 return -ENOENT;
1474 }
1475 ALOGD("%s: codec version %s", __func__, codec_version);
1476 }
1477
1478 return 0;
1479}
1480
1481
Ashish Jain81eb2a82015-05-13 10:52:34 +05301482#ifdef AUDIO_EXTERNAL_HDMI_ENABLED
1483
1484void get_default_compressed_channel_status(
1485 unsigned char *channel_status)
1486{
Ashish Jain81eb2a82015-05-13 10:52:34 +05301487 memset(channel_status,0,24);
1488
1489 /* block start bit in preamble bit 3 */
1490 channel_status[0] |= PROFESSIONAL;
1491 //compre out
1492 channel_status[0] |= NON_LPCM;
1493 // sample rate; fixed 48K for default/transcode
1494 channel_status[3] |= SR_48000;
1495}
1496
1497#ifdef HDMI_PASSTHROUGH_ENABLED
1498int32_t get_compressed_channel_status(void *audio_stream_data,
1499 uint32_t audio_frame_size,
1500 unsigned char *channel_status,
1501 enum audio_parser_code_type codec_type)
1502 // codec_type - AUDIO_PARSER_CODEC_AC3
1503 // - AUDIO_PARSER_CODEC_DTS
1504{
1505 unsigned char *stream;
1506 int ret = 0;
1507 stream = (unsigned char *)audio_stream_data;
1508
1509 if (audio_stream_data == NULL || audio_frame_size == 0) {
1510 ALOGW("no buffer to get channel status, return default for compress");
1511 get_default_compressed_channel_status(channel_status);
1512 return ret;
1513 }
1514
1515 memset(channel_status,0,24);
1516 if(init_audio_parser(stream, audio_frame_size, codec_type) == -1)
1517 {
1518 ALOGE("init audio parser failed");
1519 return -1;
1520 }
1521 ret = get_channel_status(channel_status, codec_type);
1522 return ret;
1523
1524}
1525
1526#endif
1527
1528void get_lpcm_channel_status(uint32_t sampleRate,
1529 unsigned char *channel_status)
1530{
1531 int32_t status = 0;
Ashish Jain81eb2a82015-05-13 10:52:34 +05301532 memset(channel_status,0,24);
1533 /* block start bit in preamble bit 3 */
1534 channel_status[0] |= PROFESSIONAL;
1535 //LPCM OUT
1536 channel_status[0] &= ~NON_LPCM;
1537
1538 switch (sampleRate) {
1539 case 8000:
1540 case 11025:
1541 case 12000:
1542 case 16000:
1543 case 22050:
1544 channel_status[3] |= SR_NOTID;
Preetam Singh Ranawat61716b12015-12-14 11:55:24 +05301545 break;
Ashish Jain81eb2a82015-05-13 10:52:34 +05301546 case 24000:
1547 channel_status[3] |= SR_24000;
1548 break;
1549 case 32000:
1550 channel_status[3] |= SR_32000;
1551 break;
1552 case 44100:
1553 channel_status[3] |= SR_44100;
1554 break;
1555 case 48000:
1556 channel_status[3] |= SR_48000;
1557 break;
1558 case 88200:
1559 channel_status[3] |= SR_88200;
1560 break;
1561 case 96000:
1562 channel_status[3] |= SR_96000;
1563 break;
1564 case 176400:
1565 channel_status[3] |= SR_176400;
1566 break;
1567 case 192000:
1568 channel_status[3] |= SR_192000;
1569 break;
1570 default:
1571 ALOGV("Invalid sample_rate %u\n", sampleRate);
1572 status = -1;
1573 break;
1574 }
1575}
1576
1577void audio_utils_set_hdmi_channel_status(struct stream_out *out, char * buffer, size_t bytes)
1578{
1579 unsigned char channel_status[24]={0};
1580 struct snd_aes_iec958 iec958;
1581 const char *mixer_ctl_name = "IEC958 Playback PCM Stream";
1582 struct mixer_ctl *ctl;
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +05301583 ALOGV("%s: buffer %s bytes %zd", __func__, buffer, bytes);
Ashish Jain81eb2a82015-05-13 10:52:34 +05301584#ifdef HDMI_PASSTHROUGH_ENABLED
1585 if (audio_extn_is_dolby_format(out->format) &&
1586 /*TODO:Extend code to support DTS passthrough*/
1587 /*set compressed channel status bits*/
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05301588 audio_extn_passthru_is_passthrough_stream(out)){
Ashish Jain81eb2a82015-05-13 10:52:34 +05301589 get_compressed_channel_status(buffer, bytes, channel_status, AUDIO_PARSER_CODEC_AC3);
1590 } else
1591#endif
1592 {
1593 /*set channel status bit for LPCM*/
1594 get_lpcm_channel_status(out->sample_rate, channel_status);
1595 }
1596
1597 memcpy(iec958.status, channel_status,sizeof(iec958.status));
1598 ctl = mixer_get_ctl_by_name(out->dev->mixer, mixer_ctl_name);
1599 if (!ctl) {
1600 ALOGE("%s: Could not get ctl for mixer cmd - %s",
1601 __func__, mixer_ctl_name);
1602 return;
1603 }
1604 if (mixer_ctl_set_array(ctl, &iec958, sizeof(iec958)) < 0) {
1605 ALOGE("%s: Could not set channel status for ext HDMI ",
1606 __func__);
1607 return;
1608 }
1609
1610}
1611#endif
Manish Dewangan3ccdea52017-02-13 19:31:54 +05301612
1613int audio_extn_utils_get_avt_device_drift(
1614 struct audio_usecase *usecase,
1615 struct audio_avt_device_drift_param *drift_param)
1616{
1617 int ret = 0, count = 0;
1618 char avt_device_drift_mixer_ctl_name[MIXER_PATH_MAX_LENGTH] = {0};
Naresh Tanniru6160c712017-04-17 15:43:48 +05301619 const char *backend = NULL;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05301620 struct mixer_ctl *ctl = NULL;
1621 struct audio_avt_device_drift_stats drift_stats;
1622 struct audio_device *adev = NULL;
1623
1624 if (usecase != NULL && usecase->type == PCM_PLAYBACK) {
Naresh Tanniru6160c712017-04-17 15:43:48 +05301625 backend = platform_get_snd_device_backend_interface(usecase->out_snd_device);
1626 if (!backend) {
1627 ALOGE("%s: Unsupported device %d", __func__,
1628 usecase->stream.out->devices);
1629 ret = -EINVAL;
1630 goto done;
1631 }
1632 strlcpy(avt_device_drift_mixer_ctl_name,
1633 backend,
1634 MIXER_PATH_MAX_LENGTH);
1635
1636 count = strlen(backend);
1637 if (MIXER_PATH_MAX_LENGTH - count > 0) {
1638 strlcat(&avt_device_drift_mixer_ctl_name[count],
1639 " DRIFT",
1640 MIXER_PATH_MAX_LENGTH - count);
1641 } else {
1642 ret = -EINVAL;
1643 goto done;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05301644 }
1645 } else {
Naresh Tanniru7586e292017-04-13 10:38:13 +05301646 ALOGE("%s: Invalid usecase",__func__);
Manish Dewangan3ccdea52017-02-13 19:31:54 +05301647 ret = -EINVAL;
Naresh Tanniru6160c712017-04-17 15:43:48 +05301648 goto done;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05301649 }
1650
Naresh Tanniru6160c712017-04-17 15:43:48 +05301651 adev = usecase->stream.out->dev;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05301652 ctl = mixer_get_ctl_by_name(adev->mixer, avt_device_drift_mixer_ctl_name);
1653 if (!ctl) {
1654 ALOGE("%s: Could not get ctl for mixer cmd - %s",
1655 __func__, avt_device_drift_mixer_ctl_name);
1656
1657 ret = -EINVAL;
1658 goto done;
1659 }
1660
1661 ALOGV("%s: Getting AV Timer vs Device Drift mixer ctrl name %s", __func__,
1662 avt_device_drift_mixer_ctl_name);
1663
1664 mixer_ctl_update(ctl);
1665 count = mixer_ctl_get_num_values(ctl);
1666 if (count != sizeof(struct audio_avt_device_drift_stats)) {
1667 ALOGE("%s: mixer_ctl_get_num_values() invalid drift_stats data size",
1668 __func__);
1669
1670 ret = -EINVAL;
1671 goto done;
1672 }
1673
1674 ret = mixer_ctl_get_array(ctl, (void *)&drift_stats, count);
1675 if (ret != 0) {
1676 ALOGE("%s: mixer_ctl_get_array() failed to get drift_stats Params",
1677 __func__);
1678
1679 ret = -EINVAL;
1680 goto done;
1681 }
1682 memcpy(drift_param, &drift_stats.drift_param,
1683 sizeof(struct audio_avt_device_drift_param));
1684done:
1685 return ret;
1686}
Manish Dewangan07de2142017-02-27 19:27:20 +05301687
1688#ifdef SNDRV_COMPRESS_PATH_DELAY
1689int audio_extn_utils_compress_get_dsp_latency(struct stream_out *out)
1690{
1691 int ret = -EINVAL;
1692 struct snd_compr_metadata metadata;
1693 int delay_ms = COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
1694
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -07001695 if (property_get_bool("vendor.audio.playback.dsp.pathdelay", false)) {
Manish Dewangan07de2142017-02-27 19:27:20 +05301696 ALOGD("%s:: Quering DSP delay %d",__func__, __LINE__);
1697 if (!(is_offload_usecase(out->usecase))) {
1698 ALOGE("%s:: not supported for non offload session", __func__);
1699 goto exit;
1700 }
1701
1702 if (!out->compr) {
1703 ALOGD("%s:: Invalid compress handle,returning default dsp latency",
1704 __func__);
1705 goto exit;
1706 }
1707
1708 metadata.key = SNDRV_COMPRESS_PATH_DELAY;
1709 ret = compress_get_metadata(out->compr, &metadata);
1710 if(ret) {
1711 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
1712 goto exit;
1713 }
1714 delay_ms = metadata.value[0] / 1000; /*convert to ms*/
1715 } else {
1716 ALOGD("%s:: Using Fix DSP delay",__func__);
1717 }
1718
1719exit:
1720 ALOGD("%s:: delay in ms is %d",__func__, delay_ms);
1721 return delay_ms;
1722}
1723#else
1724int audio_extn_utils_compress_get_dsp_latency(struct stream_out *out __unused)
1725{
1726 return COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
1727}
1728#endif
Manish Dewangan69426c82017-01-30 17:35:36 +05301729
1730#ifdef SNDRV_COMPRESS_RENDER_MODE
1731int audio_extn_utils_compress_set_render_mode(struct stream_out *out)
1732{
1733 struct snd_compr_metadata metadata;
1734 int ret = -EINVAL;
1735
1736 if (!(is_offload_usecase(out->usecase))) {
1737 ALOGE("%s:: not supported for non offload session", __func__);
1738 goto exit;
1739 }
1740
1741 if (!out->compr) {
1742 ALOGD("%s:: Invalid compress handle",
1743 __func__);
1744 goto exit;
1745 }
1746
1747 ALOGD("%s:: render mode %d", __func__, out->render_mode);
1748
1749 metadata.key = SNDRV_COMPRESS_RENDER_MODE;
1750 if (out->render_mode == RENDER_MODE_AUDIO_MASTER) {
1751 metadata.value[0] = SNDRV_COMPRESS_RENDER_MODE_AUDIO_MASTER;
1752 } else if (out->render_mode == RENDER_MODE_AUDIO_STC_MASTER) {
1753 metadata.value[0] = SNDRV_COMPRESS_RENDER_MODE_STC_MASTER;
1754 } else {
1755 ret = 0;
1756 goto exit;
1757 }
1758 ret = compress_set_metadata(out->compr, &metadata);
1759 if(ret) {
1760 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
1761 }
1762exit:
1763 return ret;
1764}
1765#else
1766int audio_extn_utils_compress_set_render_mode(struct stream_out *out __unused)
1767{
1768 ALOGD("%s:: configuring render mode not supported", __func__);
1769 return 0;
1770}
1771#endif
Manish Dewangan58229382017-02-02 15:48:41 +05301772
1773#ifdef SNDRV_COMPRESS_CLK_REC_MODE
1774int audio_extn_utils_compress_set_clk_rec_mode(
1775 struct audio_usecase *usecase)
1776{
1777 struct snd_compr_metadata metadata;
1778 struct stream_out *out = NULL;
1779 int ret = -EINVAL;
1780
Naresh Tanniru7586e292017-04-13 10:38:13 +05301781 if (usecase == NULL || usecase->type != PCM_PLAYBACK) {
Manish Dewangan58229382017-02-02 15:48:41 +05301782 ALOGE("%s:: Invalid use case", __func__);
1783 goto exit;
1784 }
1785
1786 out = usecase->stream.out;
1787 if (!out) {
1788 ALOGE("%s:: invalid stream", __func__);
1789 goto exit;
1790 }
1791
1792 if (!is_offload_usecase(out->usecase)) {
1793 ALOGE("%s:: not supported for non offload session", __func__);
1794 goto exit;
1795 }
1796
1797 if (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER) {
1798 ALOGD("%s:: clk recovery is only supported in STC render mode",
1799 __func__);
1800 ret = 0;
1801 goto exit;
1802 }
1803
1804 if (!out->compr) {
1805 ALOGD("%s:: Invalid compress handle",
1806 __func__);
1807 goto exit;
1808 }
1809 metadata.key = SNDRV_COMPRESS_CLK_REC_MODE;
1810 switch(usecase->out_snd_device) {
1811 case SND_DEVICE_OUT_HDMI:
1812 case SND_DEVICE_OUT_SPEAKER_AND_HDMI:
1813 case SND_DEVICE_OUT_DISPLAY_PORT:
1814 case SND_DEVICE_OUT_SPEAKER_AND_DISPLAY_PORT:
1815 metadata.value[0] = SNDRV_COMPRESS_CLK_REC_MODE_NONE;
1816 break;
1817 default:
1818 metadata.value[0] = SNDRV_COMPRESS_CLK_REC_MODE_AUTO;
1819 break;
1820 }
1821
1822 ALOGD("%s:: clk recovery mode %d",__func__, metadata.value[0]);
1823
1824 ret = compress_set_metadata(out->compr, &metadata);
1825 if(ret) {
1826 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
1827 }
1828
1829exit:
1830 return ret;
1831}
1832#else
1833int audio_extn_utils_compress_set_clk_rec_mode(
1834 struct audio_usecase *usecase __unused)
1835{
1836 ALOGD("%s:: configuring render mode not supported", __func__);
1837 return 0;
1838}
1839#endif
Manish Dewangan27346042017-03-01 12:56:12 +05301840
1841#ifdef SNDRV_COMPRESS_RENDER_WINDOW
1842int audio_extn_utils_compress_set_render_window(
1843 struct stream_out *out,
1844 struct audio_out_render_window_param *render_window)
1845{
1846 struct snd_compr_metadata metadata;
1847 int ret = -EINVAL;
1848
Manish Dewangan27346042017-03-01 12:56:12 +05301849 if(render_window == NULL) {
1850 ALOGE("%s:: Invalid render_window", __func__);
1851 goto exit;
1852 }
1853
Aniket Kumar Lata1e1d3662017-06-01 18:45:48 -07001854 ALOGD("%s:: render window start 0x%"PRIx64" end 0x%"PRIx64"",
1855 __func__,render_window->render_ws, render_window->render_we);
1856
Manish Dewangan27346042017-03-01 12:56:12 +05301857 if (!is_offload_usecase(out->usecase)) {
1858 ALOGE("%s:: not supported for non offload session", __func__);
1859 goto exit;
1860 }
1861
Naresh Tanniru6160c712017-04-17 15:43:48 +05301862 if ((out->render_mode != RENDER_MODE_AUDIO_MASTER) &&
1863 (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER)) {
Manish Dewangan27346042017-03-01 12:56:12 +05301864 ALOGD("%s:: only supported in timestamp mode, current "
1865 "render mode mode %d", __func__, out->render_mode);
1866 goto exit;
1867 }
1868
1869 if (!out->compr) {
1870 ALOGW("%s:: offload session not yet opened,"
1871 "render window will be configure later", __func__);
1872 /* store render window to reconfigure in start_output_stream() */
1873 goto exit;
1874 }
1875
1876 metadata.key = SNDRV_COMPRESS_RENDER_WINDOW;
1877 /*render window start value */
1878 metadata.value[0] = 0xFFFFFFFF & render_window->render_ws; /* lsb */
1879 metadata.value[1] = \
1880 (0xFFFFFFFF00000000 & render_window->render_ws) >> 32; /* msb*/
1881 /*render window end value */
1882 metadata.value[2] = 0xFFFFFFFF & render_window->render_we; /* lsb */
1883 metadata.value[3] = \
1884 (0xFFFFFFFF00000000 & render_window->render_we) >> 32; /* msb*/
1885
1886 ret = compress_set_metadata(out->compr, &metadata);
1887 if(ret) {
1888 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
1889 }
1890
1891exit:
1892 return ret;
1893}
1894#else
1895int audio_extn_utils_compress_set_render_window(
1896 struct stream_out *out __unused,
1897 struct audio_out_render_window_param *render_window __unused)
1898{
1899 ALOGD("%s:: configuring render window not supported", __func__);
1900 return 0;
1901}
1902#endif
Manish Dewangan14956cc2017-02-14 18:54:42 +05301903
1904#ifdef SNDRV_COMPRESS_START_DELAY
1905int audio_extn_utils_compress_set_start_delay(
1906 struct stream_out *out,
1907 struct audio_out_start_delay_param *delay_param)
1908{
1909 struct snd_compr_metadata metadata;
1910 int ret = -EINVAL;
1911
1912 if(delay_param == NULL) {
1913 ALOGE("%s:: Invalid delay_param", __func__);
1914 goto exit;
1915 }
1916
1917 ALOGD("%s:: render start delay 0x%"PRIx64" ", __func__,
1918 delay_param->start_delay);
1919
1920 if (!is_offload_usecase(out->usecase)) {
1921 ALOGE("%s:: not supported for non offload session", __func__);
1922 goto exit;
1923 }
1924
Naresh Tanniru6160c712017-04-17 15:43:48 +05301925 if ((out->render_mode != RENDER_MODE_AUDIO_MASTER) &&
1926 (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER)) {
Manish Dewangan14956cc2017-02-14 18:54:42 +05301927 ALOGD("%s:: only supported in timestamp mode, current "
1928 "render mode mode %d", __func__, out->render_mode);
1929 goto exit;
1930 }
1931
1932 if (!out->compr) {
1933 ALOGW("%s:: offload session not yet opened,"
1934 "start delay will be configure later", __func__);
1935 goto exit;
1936 }
1937
1938 metadata.key = SNDRV_COMPRESS_START_DELAY;
1939 metadata.value[0] = 0xFFFFFFFF & delay_param->start_delay; /* lsb */
1940 metadata.value[1] = \
1941 (0xFFFFFFFF00000000 & delay_param->start_delay) >> 32; /* msb*/
1942
1943 ret = compress_set_metadata(out->compr, &metadata);
1944 if(ret) {
1945 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
1946 }
1947
1948exit:
1949 return ret;
1950}
1951#else
1952int audio_extn_utils_compress_set_start_delay(
1953 struct stream_out *out __unused,
1954 struct audio_out_start_delay_param *delay_param __unused)
1955{
1956 ALOGD("%s:: configuring render window not supported", __func__);
1957 return 0;
1958}
1959#endif
Vignesh Kulothungan55396882017-04-20 14:37:02 -07001960
1961#define MAX_SND_CARD 8
1962#define RETRY_US 500000
1963#define RETRY_NUMBER 10
1964
1965int audio_extn_utils_get_snd_card_num()
1966{
1967
1968 void *hw_info = NULL;
1969 struct mixer *mixer = NULL;
1970 int retry_num = 0;
1971 int snd_card_num = 0;
1972 char* snd_card_name = NULL;
1973
1974 while (snd_card_num < MAX_SND_CARD) {
1975 mixer = mixer_open(snd_card_num);
1976
1977 while (!mixer && retry_num < RETRY_NUMBER) {
1978 usleep(RETRY_US);
1979 mixer = mixer_open(snd_card_num);
1980 retry_num++;
1981 }
1982
1983 if (!mixer) {
1984 ALOGE("%s: Unable to open the mixer card: %d", __func__,
1985 snd_card_num);
1986 retry_num = 0;
1987 snd_card_num++;
1988 continue;
1989 }
1990
1991 snd_card_name = strdup(mixer_get_name(mixer));
1992 if (!snd_card_name) {
1993 ALOGE("failed to allocate memory for snd_card_name\n");
1994 mixer_close(mixer);
1995 return -1;
1996 }
1997 ALOGD("%s: snd_card_name: %s", __func__, snd_card_name);
1998
1999 hw_info = hw_info_init(snd_card_name);
2000 if (hw_info) {
2001 ALOGD("%s: Opened sound card:%d", __func__, snd_card_num);
2002 break;
2003 }
2004 ALOGE("%s: Failed to init hardware info", __func__);
2005 retry_num = 0;
2006 snd_card_num++;
2007 free(snd_card_name);
2008 mixer_close(mixer);
2009 }
2010
2011 mixer_close(mixer);
2012 hw_info_deinit(hw_info);
2013 if (snd_card_name)
2014 free(snd_card_name);
2015
2016 if (snd_card_num >= MAX_SND_CARD) {
2017 ALOGE("%s: Unable to find correct sound card, aborting.", __func__);
2018 return -1;
2019 }
2020
2021 return snd_card_num;
2022}
Naresh Tanniru6160c712017-04-17 15:43:48 +05302023
2024#ifdef SNDRV_COMPRESS_ENABLE_ADJUST_SESSION_CLOCK
2025int audio_extn_utils_compress_enable_drift_correction(
2026 struct stream_out *out,
2027 struct audio_out_enable_drift_correction *drift)
2028{
2029 struct snd_compr_metadata metadata;
2030 int ret = -EINVAL;
2031
2032 if(drift == NULL) {
2033 ALOGE("%s:: Invalid param", __func__);
2034 goto exit;
2035 }
2036
2037 ALOGD("%s:: drift enable %d", __func__,drift->enable);
2038
2039 if (!is_offload_usecase(out->usecase)) {
2040 ALOGE("%s:: not supported for non offload session", __func__);
2041 goto exit;
2042 }
2043
2044 if (!out->compr) {
2045 ALOGW("%s:: offload session not yet opened,"
2046 "start delay will be configure later", __func__);
2047 goto exit;
2048 }
2049
2050 metadata.key = SNDRV_COMPRESS_ENABLE_ADJUST_SESSION_CLOCK;
2051 metadata.value[0] = drift->enable;
2052 out->drift_correction_enabled = drift->enable;
2053
2054 ret = compress_set_metadata(out->compr, &metadata);
2055 if(ret) {
2056 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2057 out->drift_correction_enabled = false;
2058 }
2059
2060exit:
2061 return ret;
2062}
2063#else
2064int audio_extn_utils_compress_enable_drift_correction(
2065 struct stream_out *out __unused,
2066 struct audio_out_enable_drift_correction *drift __unused)
2067{
2068 ALOGD("%s:: configuring drift enablement not supported", __func__);
2069 return 0;
2070}
2071#endif
2072
2073#ifdef SNDRV_COMPRESS_ADJUST_SESSION_CLOCK
2074int audio_extn_utils_compress_correct_drift(
2075 struct stream_out *out,
2076 struct audio_out_correct_drift *drift_param)
2077{
2078 struct snd_compr_metadata metadata;
2079 int ret = -EINVAL;
2080
2081 if (drift_param == NULL) {
2082 ALOGE("%s:: Invalid drift_param", __func__);
2083 goto exit;
2084 }
2085
2086 ALOGD("%s:: adjust time 0x%"PRIx64" ", __func__,
2087 drift_param->adjust_time);
2088
2089 if (!is_offload_usecase(out->usecase)) {
2090 ALOGE("%s:: not supported for non offload session", __func__);
2091 goto exit;
2092 }
2093
2094 if (!out->compr) {
2095 ALOGW("%s:: offload session not yet opened", __func__);
2096 goto exit;
2097 }
2098
2099 if (!out->drift_correction_enabled) {
2100 ALOGE("%s:: drift correction not enabled", __func__);
2101 goto exit;
2102 }
2103
2104 metadata.key = SNDRV_COMPRESS_ADJUST_SESSION_CLOCK;
2105 metadata.value[0] = 0xFFFFFFFF & drift_param->adjust_time; /* lsb */
2106 metadata.value[1] = \
2107 (0xFFFFFFFF00000000 & drift_param->adjust_time) >> 32; /* msb*/
2108
2109 ret = compress_set_metadata(out->compr, &metadata);
2110 if(ret)
2111 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2112exit:
2113 return ret;
2114}
2115#else
2116int audio_extn_utils_compress_correct_drift(
2117 struct stream_out *out __unused,
2118 struct audio_out_correct_drift *drift_param __unused)
2119{
2120 ALOGD("%s:: setting adjust clock not supported", __func__);
2121 return 0;
2122}
2123#endif
Naresh Tanniru29bce4e2017-04-27 17:54:30 +05302124
2125int audio_extn_utils_set_channel_map(
2126 struct stream_out *out,
2127 struct audio_out_channel_map_param *channel_map_param)
2128{
2129 int ret = -EINVAL, i = 0;
2130 int channels = audio_channel_count_from_out_mask(out->channel_mask);
2131
2132 if (channel_map_param == NULL) {
2133 ALOGE("%s:: Invalid channel_map", __func__);
2134 goto exit;
2135 }
2136
2137 if (channel_map_param->channels != channels) {
2138 ALOGE("%s:: Channels(%d) does not match stream channels(%d)",
2139 __func__, channel_map_param->channels, channels);
2140 goto exit;
2141 }
2142
2143 for ( i = 0; i < channels; i++) {
2144 ALOGV("%s:: channel_map[%d]- %d", __func__, i, channel_map_param->channel_map[i]);
2145 out->channel_map_param.channel_map[i] = channel_map_param->channel_map[i];
2146 }
2147 ret = 0;
2148exit:
2149 return ret;
2150}