blob: c9d2ba5ea874a17f2aa0fd6efd368d320a655c63 [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
Vikram Panduranga93f080e2017-06-07 18:16:14 -070084#define APP_TYPE_VOIP_AUDIO 0x1113A
85
Ashish Jain81eb2a82015-05-13 10:52:34 +053086#ifdef AUDIO_EXTERNAL_HDMI_ENABLED
87#define PROFESSIONAL (1<<0) /* 0 = consumer, 1 = professional */
88#define NON_LPCM (1<<1) /* 0 = audio, 1 = non-audio */
89#define SR_44100 (0<<0) /* 44.1kHz */
90#define SR_NOTID (1<<0) /* non indicated */
91#define SR_48000 (2<<0) /* 48kHz */
92#define SR_32000 (3<<0) /* 32kHz */
93#define SR_22050 (4<<0) /* 22.05kHz */
94#define SR_24000 (6<<0) /* 24kHz */
95#define SR_88200 (8<<0) /* 88.2kHz */
96#define SR_96000 (10<<0) /* 96kHz */
97#define SR_176400 (12<<0) /* 176.4kHz */
98#define SR_192000 (14<<0) /* 192kHz */
99
100#endif
Manish Dewangan07de2142017-02-27 19:27:20 +0530101
102/* ToDo: Check and update a proper value in msec */
103#define COMPRESS_OFFLOAD_PLAYBACK_LATENCY 50
104
Varun Balaraje49253e2017-07-06 19:48:56 +0530105#ifndef MAX_CHANNELS_SUPPORTED
106#define MAX_CHANNELS_SUPPORTED 8
107#endif
108
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700109struct string_to_enum {
110 const char *name;
111 uint32_t value;
112};
113
114const struct string_to_enum s_flag_name_to_enum_table[] = {
115 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_DIRECT),
116 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_PRIMARY),
117 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_FAST),
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800118 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_RAW),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700119 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_DEEP_BUFFER),
120 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD),
121 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_NON_BLOCKING),
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700122 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_HW_AV_SYNC),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700123#ifdef INCALL_MUSIC_ENABLED
124 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_INCALL_MUSIC),
125#endif
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700126 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH),
Naresh Tanniruee3499a2017-01-05 14:05:35 +0530127 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_TIMESTAMP),
Vikram Panduranga93f080e2017-06-07 18:16:14 -0700128 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_VOIP_RX),
Ben Romberger6c4d3812017-06-13 17:46:45 -0700129 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_BD),
Varun Balaraje49253e2017-07-06 19:48:56 +0530130 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_INTERACTIVE),
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530131 STRING_TO_ENUM(AUDIO_INPUT_FLAG_NONE),
132 STRING_TO_ENUM(AUDIO_INPUT_FLAG_FAST),
133 STRING_TO_ENUM(AUDIO_INPUT_FLAG_HW_HOTWORD),
134 STRING_TO_ENUM(AUDIO_INPUT_FLAG_RAW),
135 STRING_TO_ENUM(AUDIO_INPUT_FLAG_SYNC),
Dhananjay Kumaree4d2002016-10-25 18:02:58 +0530136 STRING_TO_ENUM(AUDIO_INPUT_FLAG_TIMESTAMP),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700137};
138
139const struct string_to_enum s_format_name_to_enum_table[] = {
Ashish Jain83a6cc22016-06-28 14:34:17 +0530140 STRING_TO_ENUM(AUDIO_FORMAT_PCM_8_BIT),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700141 STRING_TO_ENUM(AUDIO_FORMAT_PCM_16_BIT),
Ashish Jain5106d362016-05-11 19:23:33 +0530142 STRING_TO_ENUM(AUDIO_FORMAT_PCM_24_BIT_PACKED),
143 STRING_TO_ENUM(AUDIO_FORMAT_PCM_8_24_BIT),
Ashish Jainf1eaa582016-05-23 20:54:24 +0530144 STRING_TO_ENUM(AUDIO_FORMAT_PCM_32_BIT),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700145 STRING_TO_ENUM(AUDIO_FORMAT_MP3),
146 STRING_TO_ENUM(AUDIO_FORMAT_AAC),
147 STRING_TO_ENUM(AUDIO_FORMAT_VORBIS),
Mingming Yinae3530f2014-07-03 16:50:18 -0700148 STRING_TO_ENUM(AUDIO_FORMAT_AMR_NB),
149 STRING_TO_ENUM(AUDIO_FORMAT_AMR_WB),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700150 STRING_TO_ENUM(AUDIO_FORMAT_AC3),
Mingming Yinae3530f2014-07-03 16:50:18 -0700151 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700152 STRING_TO_ENUM(AUDIO_FORMAT_DTS),
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530153 STRING_TO_ENUM(AUDIO_FORMAT_DTS_HD),
Ben Romberger1aaaf862017-04-06 17:49:46 -0700154 STRING_TO_ENUM(AUDIO_FORMAT_DOLBY_TRUEHD),
Naresh Tanniru928f0862017-04-07 16:44:23 -0700155 STRING_TO_ENUM(AUDIO_FORMAT_IEC61937),
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530156#ifdef AUDIO_EXTN_FORMATS_ENABLED
157 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3_JOC),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700158 STRING_TO_ENUM(AUDIO_FORMAT_WMA),
159 STRING_TO_ENUM(AUDIO_FORMAT_WMA_PRO),
160 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADIF),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700161 STRING_TO_ENUM(AUDIO_FORMAT_AMR_WB_PLUS),
162 STRING_TO_ENUM(AUDIO_FORMAT_EVRC),
163 STRING_TO_ENUM(AUDIO_FORMAT_EVRCB),
164 STRING_TO_ENUM(AUDIO_FORMAT_EVRCWB),
165 STRING_TO_ENUM(AUDIO_FORMAT_QCELP),
166 STRING_TO_ENUM(AUDIO_FORMAT_MP2),
167 STRING_TO_ENUM(AUDIO_FORMAT_EVRCNW),
Amit Shekhar6f461b12014-08-01 14:52:58 -0700168 STRING_TO_ENUM(AUDIO_FORMAT_FLAC),
Satya Krishna Pindiproli70471602015-04-24 19:12:43 +0530169 STRING_TO_ENUM(AUDIO_FORMAT_ALAC),
170 STRING_TO_ENUM(AUDIO_FORMAT_APE),
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700171 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3_JOC),
Alexy Josephcd8eaed2014-12-11 12:46:53 -0800172 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LC),
173 STRING_TO_ENUM(AUDIO_FORMAT_AAC_HE_V1),
174 STRING_TO_ENUM(AUDIO_FORMAT_AAC_HE_V2),
Manish Dewangana6fc5442015-08-24 20:30:31 +0530175 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS),
Ashish Jaine513a872015-11-19 17:00:56 +0530176 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_LC),
177 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_HE_V1),
178 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_HE_V2),
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +0530179 STRING_TO_ENUM(AUDIO_FORMAT_DSD),
Arun Kumar Dasari3b174182016-12-27 13:01:14 +0530180 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM),
181 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM_LC),
182 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM_HE_V1),
183 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM_HE_V2),
Dhanalakshmi Siddani18737932016-11-29 17:33:17 +0530184 STRING_TO_ENUM(AUDIO_FORMAT_APTX),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700185#endif
186};
187
Manish Dewangan3ccdea52017-02-13 19:31:54 +0530188/* payload structure avt_device drift query */
189struct audio_avt_device_drift_stats {
190 uint32_t minor_version;
191 /* Indicates the device interface direction as either
192 * source (Tx) or sink (Rx).
193 */
194 uint16_t device_direction;
195 /*params exposed to client */
196 struct audio_avt_device_drift_param drift_param;
197};
198
Ben Rombergera04fabc2014-11-14 12:16:03 -0800199static char bTable[BASE_TABLE_SIZE] = {
200 'A','B','C','D','E','F','G','H','I','J','K','L',
201 'M','N','O','P','Q','R','S','T','U','V','W','X',
202 'Y','Z','a','b','c','d','e','f','g','h','i','j',
203 'k','l','m','n','o','p','q','r','s','t','u','v',
204 'w','x','y','z','0','1','2','3','4','5','6','7',
205 '8','9','+','/'
206};
207
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700208static uint32_t string_to_enum(const struct string_to_enum *table, size_t size,
209 const char *name)
210{
211 size_t i;
212 for (i = 0; i < size; i++) {
213 if (strcmp(table[i].name, name) == 0) {
214 ALOGV("%s found %s", __func__, table[i].name);
215 return table[i].value;
216 }
217 }
218 return 0;
219}
220
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530221static audio_io_flags_t parse_flag_names(char *name)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700222{
223 uint32_t flag = 0;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530224 audio_io_flags_t io_flags;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800225 char *last_r;
226 char *flag_name = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700227 while (flag_name != NULL) {
228 if (strlen(flag_name) != 0) {
229 flag |= string_to_enum(s_flag_name_to_enum_table,
230 ARRAY_SIZE(s_flag_name_to_enum_table),
231 flag_name);
232 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800233 flag_name = strtok_r(NULL, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700234 }
235
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800236 ALOGV("parse_flag_names: flag - %x", flag);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530237 io_flags.in_flags = (audio_input_flags_t)flag;
238 io_flags.out_flags = (audio_output_flags_t)flag;
239 return io_flags;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700240}
241
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530242static void parse_format_names(char *name, struct streams_io_cfg *s_info)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700243{
244 struct stream_format *sf_info = NULL;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800245 char *last_r;
246 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700247
248 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG) == 0)
249 return;
250
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530251 list_init(&s_info->format_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700252 while (str != NULL) {
253 audio_format_t format = (audio_format_t)string_to_enum(s_format_name_to_enum_table,
254 ARRAY_SIZE(s_format_name_to_enum_table), str);
255 ALOGV("%s: format - %d", __func__, format);
256 if (format != 0) {
257 sf_info = (struct stream_format *)calloc(1, sizeof(struct stream_format));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700258 if (sf_info == NULL)
259 break; /* return whatever was parsed */
260
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700261 sf_info->format = format;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530262 list_add_tail(&s_info->format_list, &sf_info->list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700263 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800264 str = strtok_r(NULL, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700265 }
266}
267
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530268static void parse_sample_rate_names(char *name, struct streams_io_cfg *s_info)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700269{
Amit Shekhar6f461b12014-08-01 14:52:58 -0700270 struct stream_sample_rate *ss_info = NULL;
271 uint32_t sample_rate = 48000;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800272 char *last_r;
273 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700274
Amit Shekhar6f461b12014-08-01 14:52:58 -0700275 if (str != NULL && 0 == strcmp(str, DYNAMIC_VALUE_TAG))
276 return;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700277
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530278 list_init(&s_info->sample_rate_list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700279 while (str != NULL) {
280 sample_rate = (uint32_t)strtol(str, (char **)NULL, 10);
281 ALOGV("%s: sample_rate - %d", __func__, sample_rate);
282 if (0 != sample_rate) {
283 ss_info = (struct stream_sample_rate *)calloc(1, sizeof(struct stream_sample_rate));
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800284 if (!ss_info) {
285 ALOGE("%s: memory allocation failure", __func__);
286 return;
287 }
Amit Shekhar6f461b12014-08-01 14:52:58 -0700288 ss_info->sample_rate = sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530289 list_add_tail(&s_info->sample_rate_list, &ss_info->list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700290 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800291 str = strtok_r(NULL, "|", &last_r);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700292 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700293}
294
295static int parse_bit_width_names(char *name)
296{
297 int bit_width = 16;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800298 char *last_r;
299 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700300
301 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG))
302 bit_width = (int)strtol(str, (char **)NULL, 10);
303
304 ALOGV("%s: bit_width - %d", __func__, bit_width);
305 return bit_width;
306}
307
308static int parse_app_type_names(void *platform, char *name)
309{
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700310 int app_type = platform_get_default_app_type(platform);
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800311 char *last_r;
312 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700313
314 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG))
315 app_type = (int)strtol(str, (char **)NULL, 10);
316
317 ALOGV("%s: app_type - %d", __func__, app_type);
318 return app_type;
319}
320
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530321static void update_streams_cfg_list(cnode *root, void *platform,
322 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700323{
324 cnode *node = root->first_child;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530325 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700326
327 ALOGV("%s", __func__);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530328 s_info = (struct streams_io_cfg *)calloc(1, sizeof(struct streams_io_cfg));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700329
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530330 if (!s_info) {
331 ALOGE("failed to allocate mem for s_info list element");
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700332 return;
333 }
334
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700335 while (node) {
336 if (strcmp(node->name, FLAGS_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530337 s_info->flags = parse_flag_names((char *)node->value);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530338 } else if (strcmp(node->name, PROFILES_TAG) == 0) {
339 strlcpy(s_info->profile, (char *)node->value, sizeof(s_info->profile));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700340 } else if (strcmp(node->name, FORMATS_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530341 parse_format_names((char *)node->value, s_info);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700342 } else if (strcmp(node->name, SAMPLING_RATES_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530343 s_info->app_type_cfg.sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
344 parse_sample_rate_names((char *)node->value, s_info);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700345 } else if (strcmp(node->name, BIT_WIDTH_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530346 s_info->app_type_cfg.bit_width = parse_bit_width_names((char *)node->value);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700347 } else if (strcmp(node->name, APP_TYPE_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530348 s_info->app_type_cfg.app_type = parse_app_type_names(platform, (char *)node->value);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700349 }
350 node = node->next;
351 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530352 list_add_tail(streams_cfg_list, &s_info->list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700353}
354
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530355static void load_cfg_list(cnode *root, void *platform,
356 struct listnode *streams_output_cfg_list,
357 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700358{
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530359 cnode *node = NULL;
360
361 node = config_find(root, OUTPUTS_TAG);
362 if (node != NULL) {
363 node = node->first_child;
364 while (node) {
365 ALOGV("%s: loading output %s", __func__, node->name);
366 update_streams_cfg_list(node, platform, streams_output_cfg_list);
367 node = node->next;
368 }
369 } else {
370 ALOGI("%s: could not load output, node is NULL", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700371 }
372
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530373 node = config_find(root, INPUTS_TAG);
374 if (node != NULL) {
375 node = node->first_child;
376 while (node) {
377 ALOGV("%s: loading input %s", __func__, node->name);
378 update_streams_cfg_list(node, platform, streams_input_cfg_list);
379 node = node->next;
380 }
381 } else {
382 ALOGI("%s: could not load input, node is NULL", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700383 }
384}
385
386static void send_app_type_cfg(void *platform, struct mixer *mixer,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530387 struct listnode *streams_output_cfg_list,
388 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700389{
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530390 size_t app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT] = {0};
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700391 int length = 0, i, num_app_types = 0;
392 struct listnode *node;
393 bool update;
394 struct mixer_ctl *ctl = NULL;
395 const char *mixer_ctl_name = "App Type Config";
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530396 struct streams_io_cfg *s_info = NULL;
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800397 uint32_t target_bit_width = 0;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700398
399 if (!mixer) {
400 ALOGE("%s: mixer is null",__func__);
401 return;
402 }
403 ctl = mixer_get_ctl_by_name(mixer, mixer_ctl_name);
404 if (!ctl) {
405 ALOGE("%s: Could not get ctl for mixer cmd - %s",__func__, mixer_ctl_name);
406 return;
407 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530408 app_type_cfg[length++] = num_app_types;
409
410 if (list_empty(streams_output_cfg_list)) {
411 app_type_cfg[length++] = platform_get_default_app_type_v2(platform, PCM_PLAYBACK);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700412 app_type_cfg[length++] = 48000;
413 app_type_cfg[length++] = 16;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530414 num_app_types += 1;
415 }
416 if (list_empty(streams_input_cfg_list)) {
417 app_type_cfg[length++] = platform_get_default_app_type_v2(platform, PCM_CAPTURE);
418 app_type_cfg[length++] = 48000;
419 app_type_cfg[length++] = 16;
420 num_app_types += 1;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700421 }
422
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800423 /* get target bit width for ADM enforce mode */
424 target_bit_width = adev_get_dsp_bit_width_enforce_mode();
425
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700426 list_for_each(node, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530427 s_info = node_to_item(node, struct streams_io_cfg, list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700428 update = true;
429 for (i=0; i<length; i=i+3) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530430 if (app_type_cfg[i+1] == 0)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700431 break;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530432 else if (app_type_cfg[i+1] == (size_t)s_info->app_type_cfg.app_type) {
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530433 if (app_type_cfg[i+2] < (size_t)s_info->app_type_cfg.sample_rate)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530434 app_type_cfg[i+2] = s_info->app_type_cfg.sample_rate;
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530435 if (app_type_cfg[i+3] < (size_t)s_info->app_type_cfg.bit_width)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530436 app_type_cfg[i+3] = s_info->app_type_cfg.bit_width;
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800437 /* ADM bit width = max(enforce_bit_width, bit_width from s_info */
438 if (audio_extn_is_dsp_bit_width_enforce_mode_supported(s_info->flags.out_flags) &&
439 (target_bit_width > app_type_cfg[i+3]))
440 app_type_cfg[i+3] = target_bit_width;
441
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700442 update = false;
443 break;
444 }
445 }
446 if (update && ((length + 3) <= MAX_LENGTH_MIXER_CONTROL_IN_INT)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530447 num_app_types += 1;
448 app_type_cfg[length++] = s_info->app_type_cfg.app_type;
449 app_type_cfg[length++] = s_info->app_type_cfg.sample_rate;
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800450 app_type_cfg[length] = s_info->app_type_cfg.bit_width;
451 if (audio_extn_is_dsp_bit_width_enforce_mode_supported(s_info->flags.out_flags) &&
452 (target_bit_width > app_type_cfg[length]))
453 app_type_cfg[length] = target_bit_width;
454
455 length++;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530456 }
457 }
458 list_for_each(node, streams_input_cfg_list) {
459 s_info = node_to_item(node, struct streams_io_cfg, list);
460 update = true;
461 for (i=0; i<length; i=i+3) {
462 if (app_type_cfg[i+1] == 0)
463 break;
464 else if (app_type_cfg[i+1] == (size_t)s_info->app_type_cfg.app_type) {
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530465 if (app_type_cfg[i+2] < (size_t)s_info->app_type_cfg.sample_rate)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530466 app_type_cfg[i+2] = s_info->app_type_cfg.sample_rate;
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530467 if (app_type_cfg[i+3] < (size_t)s_info->app_type_cfg.bit_width)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530468 app_type_cfg[i+3] = s_info->app_type_cfg.bit_width;
469 update = false;
470 break;
471 }
472 }
473 if (update && ((length + 3) <= MAX_LENGTH_MIXER_CONTROL_IN_INT)) {
474 num_app_types += 1;
475 app_type_cfg[length++] = s_info->app_type_cfg.app_type;
476 app_type_cfg[length++] = s_info->app_type_cfg.sample_rate;
477 app_type_cfg[length++] = s_info->app_type_cfg.bit_width;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700478 }
479 }
480 ALOGV("%s: num_app_types: %d", __func__, num_app_types);
481 if (num_app_types) {
482 app_type_cfg[0] = num_app_types;
483 mixer_ctl_set_array(ctl, app_type_cfg, length);
484 }
485}
486
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530487void audio_extn_utils_update_streams_cfg_lists(void *platform,
488 struct mixer *mixer,
489 struct listnode *streams_output_cfg_list,
490 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700491{
492 cnode *root;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530493 char *data = NULL;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700494
495 ALOGV("%s", __func__);
496 list_init(streams_output_cfg_list);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530497 list_init(streams_input_cfg_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700498
499 root = config_node("", "");
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700500 if (root == NULL) {
501 ALOGE("cfg_list, NULL config root");
502 return;
503 }
504
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530505 data = (char *)load_file(AUDIO_IO_POLICY_VENDOR_CONFIG_FILE, NULL);
506 if (data == NULL) {
507 ALOGD("%s: failed to open io config file(%s), trying older config file",
508 __func__, AUDIO_IO_POLICY_VENDOR_CONFIG_FILE);
509 data = (char *)load_file(AUDIO_OUTPUT_POLICY_VENDOR_CONFIG_FILE, NULL);
510 if (data == NULL) {
511 send_app_type_cfg(platform, mixer,
512 streams_output_cfg_list,
513 streams_input_cfg_list);
514 ALOGE("%s: could not load io policy config!", __func__);
yidongh6eb4f752017-04-19 18:20:57 +0800515 free(root);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530516 return;
517 }
518 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700519
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530520 config_load(root, data);
521 load_cfg_list(root, platform, streams_output_cfg_list,
522 streams_input_cfg_list);
523
524 send_app_type_cfg(platform, mixer, streams_output_cfg_list,
525 streams_input_cfg_list);
Alexy Josephaee4fdd2016-01-29 13:02:07 -0800526
527 config_free(root);
yidongh6eb4f752017-04-19 18:20:57 +0800528 free(root);
Alexy Josephaee4fdd2016-01-29 13:02:07 -0800529 free(data);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700530}
531
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530532static void audio_extn_utils_dump_streams_cfg_list(
533 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700534{
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700535 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530536 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700537 struct stream_format *sf_info;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700538 struct stream_sample_rate *ss_info;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530539
540 list_for_each(node_i, streams_cfg_list) {
541 s_info = node_to_item(node_i, struct streams_io_cfg, list);
542 ALOGV("%s: flags-%d, sample_rate-%d, bit_width-%d, app_type-%d",
543 __func__, s_info->flags.out_flags, s_info->app_type_cfg.sample_rate,
544 s_info->app_type_cfg.bit_width, s_info->app_type_cfg.app_type);
545 list_for_each(node_j, &s_info->format_list) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700546 sf_info = node_to_item(node_j, struct stream_format, list);
547 ALOGV("format-%x", sf_info->format);
548 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530549 list_for_each(node_j, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700550 ss_info = node_to_item(node_j, struct stream_sample_rate, list);
551 ALOGV("sample rate-%d", ss_info->sample_rate);
552 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700553 }
554}
555
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530556void audio_extn_utils_dump_streams_cfg_lists(
557 struct listnode *streams_output_cfg_list,
558 struct listnode *streams_input_cfg_list)
559{
560 ALOGV("%s", __func__);
561 audio_extn_utils_dump_streams_cfg_list(streams_output_cfg_list);
562 audio_extn_utils_dump_streams_cfg_list(streams_input_cfg_list);
563}
564
565static void audio_extn_utils_release_streams_cfg_list(
566 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700567{
568 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530569 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700570
571 ALOGV("%s", __func__);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530572
573 while (!list_empty(streams_cfg_list)) {
574 node_i = list_head(streams_cfg_list);
575 s_info = node_to_item(node_i, struct streams_io_cfg, list);
576 while (!list_empty(&s_info->format_list)) {
577 node_j = list_head(&s_info->format_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700578 list_remove(node_j);
579 free(node_to_item(node_j, struct stream_format, list));
580 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530581 while (!list_empty(&s_info->sample_rate_list)) {
582 node_j = list_head(&s_info->sample_rate_list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700583 list_remove(node_j);
584 free(node_to_item(node_j, struct stream_sample_rate, list));
585 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700586 list_remove(node_i);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530587 free(node_to_item(node_i, struct streams_io_cfg, list));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700588 }
589}
590
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530591void audio_extn_utils_release_streams_cfg_lists(
592 struct listnode *streams_output_cfg_list,
593 struct listnode *streams_input_cfg_list)
594{
595 ALOGV("%s", __func__);
596 audio_extn_utils_release_streams_cfg_list(streams_output_cfg_list);
597 audio_extn_utils_release_streams_cfg_list(streams_input_cfg_list);
598}
599
600static bool set_app_type_cfg(struct streams_io_cfg *s_info,
601 struct stream_app_type_cfg *app_type_cfg,
602 uint32_t sample_rate, uint32_t bit_width)
Amit Shekhar6f461b12014-08-01 14:52:58 -0700603 {
604 struct listnode *node_i;
605 struct stream_sample_rate *ss_info;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530606 list_for_each(node_i, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700607 ss_info = node_to_item(node_i, struct stream_sample_rate, list);
608 if ((sample_rate <= ss_info->sample_rate) &&
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530609 (bit_width == s_info->app_type_cfg.bit_width)) {
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700610
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530611 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700612 app_type_cfg->sample_rate = ss_info->sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530613 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700614 ALOGV("%s app_type_cfg->app_type %d, app_type_cfg->sample_rate %d, app_type_cfg->bit_width %d",
615 __func__, app_type_cfg->app_type, app_type_cfg->sample_rate, app_type_cfg->bit_width);
616 return true;
617 }
618 }
619 /*
620 * Reiterate through the list assuming dafault sample rate.
621 * Handles scenario where input sample rate is higher
622 * than all sample rates in list for the input bit width.
623 */
624 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
Apoorv Raghuvanshif59bb222015-02-18 12:23:23 -0800625
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530626 list_for_each(node_i, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700627 ss_info = node_to_item(node_i, struct stream_sample_rate, list);
628 if ((sample_rate <= ss_info->sample_rate) &&
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530629 (bit_width == s_info->app_type_cfg.bit_width)) {
630 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700631 app_type_cfg->sample_rate = sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530632 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Apoorv Raghuvanshif59bb222015-02-18 12:23:23 -0800633 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 -0700634 __func__, app_type_cfg->app_type, app_type_cfg->sample_rate, app_type_cfg->bit_width);
635 return true;
636 }
637 }
638 return false;
639}
640
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530641void audio_extn_utils_update_stream_input_app_type_cfg(void *platform,
642 struct listnode *streams_input_cfg_list,
643 audio_devices_t devices __unused,
644 audio_input_flags_t flags,
645 audio_format_t format,
646 uint32_t sample_rate,
647 uint32_t bit_width,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530648 char* profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530649 struct stream_app_type_cfg *app_type_cfg)
650{
651 struct listnode *node_i, *node_j;
652 struct streams_io_cfg *s_info;
653 struct stream_format *sf_info;
654
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530655 ALOGV("%s: flags: 0x%x, format: 0x%x sample_rate %d, profile %s",
656 __func__, flags, format, sample_rate, profile);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530657
658 list_for_each(node_i, streams_input_cfg_list) {
659 s_info = node_to_item(node_i, struct streams_io_cfg, list);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530660 /* Along with flags do profile matching if set at either end.*/
661 if (s_info->flags.in_flags == flags &&
662 ((profile[0] == '\0' && s_info->profile[0] == '\0') ||
663 strncmp(s_info->profile, profile, sizeof(s_info->profile)) == 0)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530664 list_for_each(node_j, &s_info->format_list) {
665 sf_info = node_to_item(node_j, struct stream_format, list);
666 if (sf_info->format == format) {
667 if (set_app_type_cfg(s_info, app_type_cfg, sample_rate, bit_width))
668 return;
669 }
670 }
671 }
672 }
673 ALOGW("%s: App type could not be selected. Falling back to default", __func__);
674 app_type_cfg->app_type = platform_get_default_app_type_v2(platform, PCM_CAPTURE);
675 app_type_cfg->sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
676 app_type_cfg->bit_width = 16;
677}
678
679void audio_extn_utils_update_stream_output_app_type_cfg(void *platform,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700680 struct listnode *streams_output_cfg_list,
Amit Shekhar1d896042014-10-03 13:16:09 -0700681 audio_devices_t devices,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700682 audio_output_flags_t flags,
683 audio_format_t format,
Amit Shekhar6f461b12014-08-01 14:52:58 -0700684 uint32_t sample_rate,
685 uint32_t bit_width,
Manish Dewangan837dc462015-05-27 10:17:41 +0530686 audio_channel_mask_t channel_mask,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530687 char *profile,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700688 struct stream_app_type_cfg *app_type_cfg)
689{
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +0530690 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530691 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700692 struct stream_format *sf_info;
Manish Dewangan837dc462015-05-27 10:17:41 +0530693 char value[PROPERTY_VALUE_MAX] = {0};
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700694
Ashish Jain058165c2016-09-28 23:18:48 +0530695 if ((bit_width >= 24) &&
Amit Shekhar1d896042014-10-03 13:16:09 -0700696 (devices & AUDIO_DEVICE_OUT_SPEAKER)) {
Amit Shekhar5a39c912014-10-14 15:39:30 -0700697 int32_t bw = platform_get_snd_device_bit_width(SND_DEVICE_OUT_SPEAKER);
698 if (-ENOSYS != bw)
699 bit_width = (uint32_t)bw;
Amit Shekhar1d896042014-10-03 13:16:09 -0700700 sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
701 ALOGI("%s Allowing 24-bit playback on speaker ONLY at default sampling rate", __func__);
702 }
703
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -0700704 property_get("vendor.audio.playback.mch.downsample",value,"");
Manish Dewangan837dc462015-05-27 10:17:41 +0530705 if (!strncmp("true", value, sizeof("true"))) {
706 if ((popcount(channel_mask) > 2) &&
707 (sample_rate > CODEC_BACKEND_DEFAULT_SAMPLE_RATE) &&
708 !(flags & AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH)) {
709 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
710 ALOGD("%s: MCH session defaulting sample rate to %d",
711 __func__, sample_rate);
712 }
713 }
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +0530714
715 /* Set sampling rate to 176.4 for DSD64
716 * and 352.8Khz for DSD128.
717 * Set Bit Width to 16. output will be 16 bit
718 * post DoP in ASM.
719 */
720 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH) &&
721 (format == AUDIO_FORMAT_DSD)) {
722 bit_width = 16;
723 if (sample_rate == INPUT_SAMPLING_RATE_DSD64)
724 sample_rate = OUTPUT_SAMPLING_RATE_DSD64;
725 else if (sample_rate == INPUT_SAMPLING_RATE_DSD128)
726 sample_rate = OUTPUT_SAMPLING_RATE_DSD128;
727 }
728
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +0530729 if(devices & AUDIO_DEVICE_OUT_ALL_A2DP) {
730 //TODO: Handle fractional sampling rate configuration for LL
731 audio_extn_a2dp_get_apptype_params(&sample_rate, &bit_width);
732 ALOGI("%s using %d sampling rate %d bit width for A2DP CoPP",
733 __func__, sample_rate, bit_width);
734 }
735
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800736 ALOGV("%s: flags: %x, format: %x sample_rate %d, profile %s, app_type %d",
737 __func__, flags, format, sample_rate, profile, app_type_cfg->app_type);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700738 list_for_each(node_i, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530739 s_info = node_to_item(node_i, struct streams_io_cfg, list);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530740 /* Along with flags do profile matching if set at either end.*/
741 if (s_info->flags.out_flags == flags &&
742 ((profile[0] == '\0' && s_info->profile[0] == '\0') ||
743 strncmp(s_info->profile, profile, sizeof(s_info->profile)) == 0)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530744 list_for_each(node_j, &s_info->format_list) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700745 sf_info = node_to_item(node_j, struct stream_format, list);
746 if (sf_info->format == format) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530747 if (set_app_type_cfg(s_info, app_type_cfg, sample_rate, bit_width))
Amit Shekhar6f461b12014-08-01 14:52:58 -0700748 return;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700749 }
750 }
751 }
752 }
753 list_for_each(node_i, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530754 s_info = node_to_item(node_i, struct streams_io_cfg, list);
755 if (s_info->flags.out_flags == AUDIO_OUTPUT_FLAG_PRIMARY) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700756 ALOGV("Compatible output profile not found.");
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530757 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
758 app_type_cfg->sample_rate = s_info->app_type_cfg.sample_rate;
759 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700760 ALOGV("%s Default to primary output: App type: %d sample_rate %d",
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530761 __func__, s_info->app_type_cfg.app_type, app_type_cfg->sample_rate);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700762 return;
763 }
764 }
765 ALOGW("%s: App type could not be selected. Falling back to default", __func__);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700766 app_type_cfg->app_type = platform_get_default_app_type(platform);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700767 app_type_cfg->sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700768 app_type_cfg->bit_width = 16;
769}
770
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +0530771static bool audio_is_this_native_usecase(struct audio_usecase *uc)
772{
773 bool native_usecase = false;
774 struct stream_out *out = (struct stream_out*) uc->stream.out;
775
776 if (PCM_PLAYBACK == uc->type && out != NULL &&
777 NATIVE_AUDIO_MODE_INVALID != platform_get_native_support() &&
778 is_offload_usecase(uc->id) &&
779 (out->sample_rate == OUTPUT_SAMPLING_RATE_44100))
780 native_usecase = true;
781
782 return native_usecase;
783}
784
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800785bool audio_extn_is_dsp_bit_width_enforce_mode_supported(audio_output_flags_t flags)
786{
787 /* DSP bitwidth enforce mode for ADM and AFE:
788 * includes:
789 * deep buffer, low latency, direct pcm and offload.
790 * excludes:
791 * ull(raw+fast), VOIP.
792 */
793 if ((flags & AUDIO_OUTPUT_FLAG_VOIP_RX) ||
794 ((flags & AUDIO_OUTPUT_FLAG_RAW) &&
795 (flags & AUDIO_OUTPUT_FLAG_FAST)))
796 return false;
797
798
799 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) ||
800 (flags & AUDIO_OUTPUT_FLAG_DIRECT) ||
801 (flags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) ||
802 (flags & AUDIO_OUTPUT_FLAG_PRIMARY))
803 return true;
804 else
805 return false;
806}
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800807
808static inline bool audio_is_vr_mode_on(struct audio_device *(__attribute__((unused)) adev))
809{
810 return adev->vr_audio_mode_enabled;
811}
812
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530813void audio_extn_utils_update_stream_app_type_cfg_for_usecase(
814 struct audio_device *adev,
815 struct audio_usecase *usecase)
816{
817 ALOGV("%s", __func__);
818
819 switch(usecase->type) {
820 case PCM_PLAYBACK:
821 audio_extn_utils_update_stream_output_app_type_cfg(adev->platform,
822 &adev->streams_output_cfg_list,
823 usecase->stream.out->devices,
824 usecase->stream.out->flags,
Aalique Grahame65780b52017-09-27 14:59:56 -0700825 usecase->stream.out->hal_op_format,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530826 usecase->stream.out->sample_rate,
827 usecase->stream.out->bit_width,
828 usecase->stream.out->channel_mask,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530829 usecase->stream.out->profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530830 &usecase->stream.out->app_type_cfg);
831 ALOGV("%s Selected apptype: %d", __func__, usecase->stream.out->app_type_cfg.app_type);
832 break;
833 case PCM_CAPTURE:
Vikram Panduranga93f080e2017-06-07 18:16:14 -0700834 if (usecase->id == USECASE_AUDIO_RECORD_VOIP)
835 usecase->stream.in->app_type_cfg.app_type = APP_TYPE_VOIP_AUDIO;
836 else
837 audio_extn_utils_update_stream_input_app_type_cfg(adev->platform,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530838 &adev->streams_input_cfg_list,
839 usecase->stream.in->device,
840 usecase->stream.in->flags,
841 usecase->stream.in->format,
842 usecase->stream.in->sample_rate,
843 usecase->stream.in->bit_width,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530844 usecase->stream.in->profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530845 &usecase->stream.in->app_type_cfg);
846 ALOGV("%s Selected apptype: %d", __func__, usecase->stream.in->app_type_cfg.app_type);
847 break;
Siddartha Shaik343abc62017-08-08 11:15:25 +0530848 case TRANSCODE_LOOPBACK :
849 audio_extn_utils_update_stream_output_app_type_cfg(adev->platform,
850 &adev->streams_output_cfg_list,
851 usecase->stream.inout->out_config.devices,
852 0,
853 usecase->stream.inout->out_config.format,
854 usecase->stream.inout->out_config.sample_rate,
855 usecase->stream.inout->out_config.bit_width,
856 usecase->stream.inout->out_config.channel_mask,
857 usecase->stream.inout->profile,
858 &usecase->stream.inout->out_app_type_cfg);
859 ALOGV("%s Selected apptype: %d", __func__, usecase->stream.inout->out_app_type_cfg.app_type);
860 break;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530861 default:
862 ALOGE("%s: app type cfg not supported for usecase type (%d)",
863 __func__, usecase->type);
864 }
865}
866
Siena Richard7c2db772016-12-21 11:32:34 -0800867static int send_app_type_cfg_for_device(struct audio_device *adev,
868 struct audio_usecase *usecase,
869 int split_snd_device)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700870{
871 char mixer_ctl_name[MAX_LENGTH_MIXER_CONTROL_IN_INT];
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530872 size_t app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT] = {0};
873 int len = 0, rc;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700874 struct mixer_ctl *ctl;
Siena Richard7c2db772016-12-21 11:32:34 -0800875 int pcm_device_id = 0, acdb_dev_id, app_type;
876 int snd_device = split_snd_device, snd_device_be_idx = -1;
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +0530877 int32_t sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Manish Dewangan837dc462015-05-27 10:17:41 +0530878 char value[PROPERTY_VALUE_MAX] = {0};
Varun Balaraje49253e2017-07-06 19:48:56 +0530879 struct streams_io_cfg *s_info = NULL;
880 struct listnode *node = NULL;
Nikhil Laturkarac4a7492017-08-31 18:27:19 +0530881 int bd_app_type = 0;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700882
Siena Richard7c2db772016-12-21 11:32:34 -0800883 ALOGV("%s: usecase->out_snd_device %s, usecase->in_snd_device %s, split_snd_device %s",
884 __func__, platform_get_snd_device_name(usecase->out_snd_device),
885 platform_get_snd_device_name(usecase->in_snd_device),
886 platform_get_snd_device_name(split_snd_device));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700887
Siddartha Shaik343abc62017-08-08 11:15:25 +0530888 if (usecase->type != PCM_PLAYBACK && usecase->type != PCM_CAPTURE &&
889 usecase->type != TRANSCODE_LOOPBACK) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530890 ALOGE("%s: not a playback/capture path, no need to cfg app type", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700891 rc = 0;
892 goto exit_send_app_type_cfg;
893 }
894 if ((usecase->id != USECASE_AUDIO_PLAYBACK_DEEP_BUFFER) &&
895 (usecase->id != USECASE_AUDIO_PLAYBACK_LOW_LATENCY) &&
896 (usecase->id != USECASE_AUDIO_PLAYBACK_MULTI_CH) &&
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800897 (usecase->id != USECASE_AUDIO_PLAYBACK_ULL) &&
Vikram Panduranga93f080e2017-06-07 18:16:14 -0700898 (usecase->id != USECASE_AUDIO_PLAYBACK_VOIP) &&
Siddartha Shaik343abc62017-08-08 11:15:25 +0530899 (usecase->id != USECASE_AUDIO_TRANSCODE_LOOPBACK) &&
Varun Balaraje49253e2017-07-06 19:48:56 +0530900 (!is_interactive_usecase(usecase->id)) &&
Srikanth Uyyala9d551402015-08-25 16:03:42 +0530901 (!is_offload_usecase(usecase->id)) &&
902 (usecase->type != PCM_CAPTURE)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530903 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 -0700904 rc = 0;
905 goto exit_send_app_type_cfg;
906 }
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800907
908 //if VR is active then only send the mixer control
909 if (usecase->id == USECASE_AUDIO_PLAYBACK_ULL && !audio_is_vr_mode_on(adev)) {
910 ALOGI("ULL doesnt need sending app type cfg, returning");
911 rc = 0;
912 goto exit_send_app_type_cfg;
913 }
914
Siddartha Shaik343abc62017-08-08 11:15:25 +0530915 if (usecase->type == PCM_PLAYBACK || usecase->type == TRANSCODE_LOOPBACK) {
Ben Romberger1fafdde2015-09-09 19:43:15 -0700916 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_PLAYBACK);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530917 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
918 "Audio Stream %d App Type Cfg", pcm_device_id);
Ben Romberger1fafdde2015-09-09 19:43:15 -0700919 } else if (usecase->type == PCM_CAPTURE) {
Ben Romberger1fafdde2015-09-09 19:43:15 -0700920 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_CAPTURE);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530921 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
922 "Audio Stream Capture %d App Type Cfg", pcm_device_id);
Srikanth Uyyala9d551402015-08-25 16:03:42 +0530923 }
Siena Richard7c2db772016-12-21 11:32:34 -0800924
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700925 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
926 if (!ctl) {
927 ALOGE("%s: Could not get ctl for mixer cmd - %s", __func__,
928 mixer_ctl_name);
929 rc = -EINVAL;
930 goto exit_send_app_type_cfg;
931 }
Aditya Bavanari701a6992017-03-30 19:17:16 +0530932 snd_device = platform_get_spkr_prot_snd_device(snd_device);
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +0530933
Rohit Kumar1181b292017-01-31 18:07:17 +0530934 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
935 if (acdb_dev_id <= 0) {
936 ALOGE("%s: Couldn't get the acdb dev id", __func__);
937 rc = -EINVAL;
938 goto exit_send_app_type_cfg;
939 }
940
Siena Richard7c2db772016-12-21 11:32:34 -0800941 snd_device_be_idx = platform_get_snd_device_backend_index(snd_device);
942 if (snd_device_be_idx < 0) {
943 ALOGE("%s: Couldn't get the backend index for snd device %s ret=%d",
944 __func__, platform_get_snd_device_name(snd_device),
945 snd_device_be_idx);
946 }
947
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530948 if ((usecase->type == PCM_PLAYBACK) && (usecase->stream.out != NULL)) {
Manish Dewangan837dc462015-05-27 10:17:41 +0530949
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -0700950 property_get("vendor.audio.playback.mch.downsample",value,"");
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530951 if (!strncmp("true", value, sizeof("true"))) {
952 if ((popcount(usecase->stream.out->channel_mask) > 2) &&
953 (usecase->stream.out->app_type_cfg.sample_rate > CODEC_BACKEND_DEFAULT_SAMPLE_RATE) &&
954 !(usecase->stream.out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH))
955 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
956 }
Ashish Jainc02430d2016-01-04 10:42:43 +0530957
kunleizcdf25942017-09-20 14:01:21 +0800958 if (usecase->id == USECASE_AUDIO_PLAYBACK_VOIP) {
959 usecase->stream.out->app_type_cfg.sample_rate = usecase->stream.out->sample_rate;
960 } else if (usecase->stream.out->devices & AUDIO_DEVICE_OUT_SPEAKER) {
Mingming Yin21854652016-04-13 11:54:02 -0700961 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Ashish Jaina052e572016-11-07 16:41:07 +0530962 } else if ((snd_device == SND_DEVICE_OUT_HDMI ||
963 snd_device == SND_DEVICE_OUT_USB_HEADSET ||
964 snd_device == SND_DEVICE_OUT_DISPLAY_PORT) &&
965 (usecase->stream.out->sample_rate >= OUTPUT_SAMPLING_RATE_44100)) {
966 /*
967 * To best utlize DSP, check if the stream sample rate is supported/multiple of
968 * configured device sample rate, if not update the COPP rate to be equal to the
969 * device sample rate, else open COPP at stream sample rate
970 */
971 platform_check_and_update_copp_sample_rate(adev->platform, snd_device,
972 usecase->stream.out->sample_rate,
973 &usecase->stream.out->app_type_cfg.sample_rate);
Ashish Jain4826f6c2017-02-06 13:33:20 +0530974 } else if (((snd_device != SND_DEVICE_OUT_HEADPHONES_44_1 &&
975 !audio_is_this_native_usecase(usecase)) &&
Mingming Yin21854652016-04-13 11:54:02 -0700976 usecase->stream.out->sample_rate == OUTPUT_SAMPLING_RATE_44100) ||
977 (usecase->stream.out->sample_rate < OUTPUT_SAMPLING_RATE_44100)) {
Ashish Jain4826f6c2017-02-06 13:33:20 +0530978 /* Reset to default if no native stream is active*/
Mingming Yin21854652016-04-13 11:54:02 -0700979 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
980 }
981 sample_rate = usecase->stream.out->app_type_cfg.sample_rate;
982
Varun Balaraje49253e2017-07-06 19:48:56 +0530983 /* Interactive streams are supported with only direct app type id.
984 * Get Direct profile app type and use it for interactive streams
985 */
986 list_for_each(node, &adev->streams_output_cfg_list) {
987 s_info = node_to_item(node, struct streams_io_cfg, list);
Nikhil Laturkarac4a7492017-08-31 18:27:19 +0530988 if (s_info->flags.out_flags == (AUDIO_OUTPUT_FLAG_BD |
989 AUDIO_OUTPUT_FLAG_DIRECT_PCM |
990 AUDIO_OUTPUT_FLAG_DIRECT))
991 bd_app_type = s_info->app_type_cfg.app_type;
Varun Balaraje49253e2017-07-06 19:48:56 +0530992 }
Nikhil Laturkarac4a7492017-08-31 18:27:19 +0530993 if (usecase->stream.out->flags == AUDIO_OUTPUT_FLAG_INTERACTIVE)
994 app_type = bd_app_type;
Varun Balaraje49253e2017-07-06 19:48:56 +0530995 else
996 app_type = usecase->stream.out->app_type_cfg.app_type;
Siena Richard7c2db772016-12-21 11:32:34 -0800997 app_type_cfg[len++] = app_type;
Mingming Yin21854652016-04-13 11:54:02 -0700998 app_type_cfg[len++] = acdb_dev_id;
999 if (((usecase->stream.out->format == AUDIO_FORMAT_E_AC3) ||
Ben Romberger1aaaf862017-04-06 17:49:46 -07001000 (usecase->stream.out->format == AUDIO_FORMAT_E_AC3_JOC) ||
1001 (usecase->stream.out->format == AUDIO_FORMAT_DOLBY_TRUEHD))
Harsh Bansal026d97f2017-08-17 17:44:49 +05301002 && audio_extn_passthru_is_passthrough_stream(usecase->stream.out)
1003 && !audio_extn_passthru_is_convert_supported(adev, usecase->stream.out)) {
Naresh Tanniru3a406772017-05-10 13:09:05 -07001004
1005 sample_rate = sample_rate * 4;
1006 if (sample_rate > HDMI_PASSTHROUGH_MAX_SAMPLE_RATE)
1007 sample_rate = HDMI_PASSTHROUGH_MAX_SAMPLE_RATE;
Mingming Yin21854652016-04-13 11:54:02 -07001008 }
Naresh Tanniru3a406772017-05-10 13:09:05 -07001009 app_type_cfg[len++] = sample_rate;
1010
Siena Richard7c2db772016-12-21 11:32:34 -08001011 if (snd_device_be_idx > 0)
1012 app_type_cfg[len++] = snd_device_be_idx;
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301013
Siena Richard7c2db772016-12-21 11:32:34 -08001014 ALOGI("%s PLAYBACK app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
1015 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301016
1017 } else if ((usecase->type == PCM_CAPTURE) && (usecase->stream.in != NULL)) {
Siena Richard7c2db772016-12-21 11:32:34 -08001018 app_type = usecase->stream.in->app_type_cfg.app_type;
1019 app_type_cfg[len++] = app_type;
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301020 app_type_cfg[len++] = acdb_dev_id;
kunleizcdf25942017-09-20 14:01:21 +08001021 if (usecase->id == USECASE_AUDIO_RECORD_VOIP)
1022 usecase->stream.in->app_type_cfg.sample_rate = usecase->stream.in->sample_rate;
Siena Richard7c2db772016-12-21 11:32:34 -08001023 sample_rate = usecase->stream.in->app_type_cfg.sample_rate;
1024 app_type_cfg[len++] = sample_rate;
1025 if (snd_device_be_idx > 0)
1026 app_type_cfg[len++] = snd_device_be_idx;
1027 ALOGI("%s CAPTURE app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
1028 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301029 } else {
Siena Richard7c2db772016-12-21 11:32:34 -08001030 app_type = platform_get_default_app_type_v2(adev->platform, usecase->type);
Siddartha Shaik343abc62017-08-08 11:15:25 +05301031 if(usecase->type == TRANSCODE_LOOPBACK) {
1032 sample_rate = usecase->stream.inout->out_config.sample_rate;
1033 app_type = usecase->stream.inout->out_app_type_cfg.app_type;
1034 }
Siena Richard7c2db772016-12-21 11:32:34 -08001035 app_type_cfg[len++] = app_type;
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301036 app_type_cfg[len++] = acdb_dev_id;
1037 app_type_cfg[len++] = sample_rate;
Siena Richard7c2db772016-12-21 11:32:34 -08001038 if (snd_device_be_idx > 0)
1039 app_type_cfg[len++] = snd_device_be_idx;
1040 ALOGI("%s default app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
1041 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Ashish Jainc02430d2016-01-04 10:42:43 +05301042 }
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301043
Siddartha Shaik343abc62017-08-08 11:15:25 +05301044 if(ctl)
1045 mixer_ctl_set_array(ctl, app_type_cfg, len);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001046 rc = 0;
1047exit_send_app_type_cfg:
1048 return rc;
1049}
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001050
Siena Richard7c2db772016-12-21 11:32:34 -08001051int audio_extn_utils_send_app_type_cfg(struct audio_device *adev,
1052 struct audio_usecase *usecase)
1053{
1054 int i, num_devices = 0;
1055 snd_device_t new_snd_devices[SND_DEVICE_OUT_END] = {0};
1056 int rc = 0;
1057
1058 switch (usecase->type) {
1059 case PCM_PLAYBACK:
Siddartha Shaik343abc62017-08-08 11:15:25 +05301060 case TRANSCODE_LOOPBACK:
Siena Richard7c2db772016-12-21 11:32:34 -08001061 ALOGD("%s: usecase->out_snd_device %s",
1062 __func__, platform_get_snd_device_name(usecase->out_snd_device));
1063 /* check for out combo device */
1064 if (platform_split_snd_device(adev->platform,
1065 usecase->out_snd_device,
1066 &num_devices, new_snd_devices)) {
1067 new_snd_devices[0] = usecase->out_snd_device;
1068 num_devices = 1;
1069 }
1070 break;
1071 case PCM_CAPTURE:
1072 ALOGD("%s: usecase->in_snd_device %s",
1073 __func__, platform_get_snd_device_name(usecase->in_snd_device));
1074 /* check for in combo device */
1075 if (platform_split_snd_device(adev->platform,
1076 usecase->in_snd_device,
1077 &num_devices, new_snd_devices)) {
1078 new_snd_devices[0] = usecase->in_snd_device;
1079 num_devices = 1;
1080 }
1081 break;
1082 default:
1083 ALOGI("%s: not a playback/capture path, no need to cfg app type", __func__);
1084 rc = 0;
1085 break;
1086 }
1087
1088 for (i = 0; i < num_devices; i++) {
1089 rc = send_app_type_cfg_for_device(adev, usecase, new_snd_devices[i]);
1090 if (rc)
1091 break;
1092 }
1093
1094 return rc;
1095}
1096
Preetam Singh Ranawat9519e9c2015-11-18 16:05:55 +05301097int read_line_from_file(const char *path, char *buf, size_t count)
1098{
1099 char * fgets_ret;
1100 FILE * fd;
1101 int rv;
1102
1103 fd = fopen(path, "r");
1104 if (fd == NULL)
1105 return -1;
1106
1107 fgets_ret = fgets(buf, (int)count, fd);
1108 if (NULL != fgets_ret) {
1109 rv = (int)strlen(buf);
1110 } else {
1111 rv = ferror(fd);
1112 }
1113 fclose(fd);
1114
1115 return rv;
1116}
1117
Ashish Jainf1eaa582016-05-23 20:54:24 +05301118/*Translates ALSA formats to AOSP PCM formats*/
1119audio_format_t alsa_format_to_hal(uint32_t alsa_format)
1120{
1121 audio_format_t format;
1122
1123 switch(alsa_format) {
1124 case SNDRV_PCM_FORMAT_S16_LE:
1125 format = AUDIO_FORMAT_PCM_16_BIT;
1126 break;
1127 case SNDRV_PCM_FORMAT_S24_3LE:
1128 format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
1129 break;
1130 case SNDRV_PCM_FORMAT_S24_LE:
1131 format = AUDIO_FORMAT_PCM_8_24_BIT;
1132 break;
1133 case SNDRV_PCM_FORMAT_S32_LE:
1134 format = AUDIO_FORMAT_PCM_32_BIT;
1135 break;
1136 default:
1137 ALOGW("Incorrect ALSA format");
1138 format = AUDIO_FORMAT_INVALID;
1139 }
1140 return format;
1141}
1142
1143/*Translates hal format (AOSP) to alsa formats*/
1144uint32_t hal_format_to_alsa(audio_format_t hal_format)
1145{
1146 uint32_t alsa_format;
1147
1148 switch (hal_format) {
1149 case AUDIO_FORMAT_PCM_32_BIT: {
1150 if (platform_supports_true_32bit())
1151 alsa_format = SNDRV_PCM_FORMAT_S32_LE;
1152 else
1153 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1154 }
1155 break;
1156 case AUDIO_FORMAT_PCM_8_BIT:
1157 alsa_format = SNDRV_PCM_FORMAT_S8;
1158 break;
1159 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
1160 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1161 break;
1162 case AUDIO_FORMAT_PCM_8_24_BIT: {
1163 if (platform_supports_true_32bit())
1164 alsa_format = SNDRV_PCM_FORMAT_S32_LE;
1165 else
1166 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1167 }
1168 break;
1169 case AUDIO_FORMAT_PCM_FLOAT:
1170 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1171 break;
1172 default:
1173 case AUDIO_FORMAT_PCM_16_BIT:
1174 alsa_format = SNDRV_PCM_FORMAT_S16_LE;
1175 break;
1176 }
1177 return alsa_format;
1178}
1179
Ashish Jain83a6cc22016-06-28 14:34:17 +05301180/*Translates PCM formats to AOSP formats*/
1181audio_format_t pcm_format_to_hal(uint32_t pcm_format)
1182{
1183 audio_format_t format = AUDIO_FORMAT_INVALID;
1184
1185 switch(pcm_format) {
1186 case PCM_FORMAT_S16_LE:
1187 format = AUDIO_FORMAT_PCM_16_BIT;
1188 break;
1189 case PCM_FORMAT_S24_3LE:
1190 format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
1191 break;
1192 case PCM_FORMAT_S24_LE:
1193 format = AUDIO_FORMAT_PCM_8_24_BIT;
1194 break;
1195 case PCM_FORMAT_S32_LE:
1196 format = AUDIO_FORMAT_PCM_32_BIT;
1197 break;
1198 default:
1199 ALOGW("Incorrect PCM format");
1200 format = AUDIO_FORMAT_INVALID;
1201 }
1202 return format;
1203}
1204
1205/*Translates hal format (AOSP) to alsa formats*/
1206uint32_t hal_format_to_pcm(audio_format_t hal_format)
1207{
1208 uint32_t pcm_format;
1209
1210 switch (hal_format) {
1211 case AUDIO_FORMAT_PCM_32_BIT:
1212 case AUDIO_FORMAT_PCM_8_24_BIT:
1213 case AUDIO_FORMAT_PCM_FLOAT: {
1214 if (platform_supports_true_32bit())
1215 pcm_format = PCM_FORMAT_S32_LE;
1216 else
1217 pcm_format = PCM_FORMAT_S24_3LE;
1218 }
1219 break;
1220 case AUDIO_FORMAT_PCM_8_BIT:
1221 pcm_format = PCM_FORMAT_S8;
1222 break;
1223 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
1224 pcm_format = PCM_FORMAT_S24_3LE;
1225 break;
1226 default:
1227 case AUDIO_FORMAT_PCM_16_BIT:
1228 pcm_format = PCM_FORMAT_S16_LE;
1229 break;
1230 }
1231 return pcm_format;
1232}
1233
Ashish Jainf1eaa582016-05-23 20:54:24 +05301234uint32_t get_alsa_fragment_size(uint32_t bytes_per_sample,
1235 uint32_t sample_rate,
1236 uint32_t noOfChannels)
1237{
1238 uint32_t fragment_size = 0;
1239 uint32_t pcm_offload_time = PCM_OFFLOAD_BUFFER_DURATION;
1240
1241 fragment_size = (pcm_offload_time
1242 * sample_rate
1243 * bytes_per_sample
1244 * noOfChannels)/1000;
1245 if (fragment_size < MIN_PCM_OFFLOAD_FRAGMENT_SIZE)
1246 fragment_size = MIN_PCM_OFFLOAD_FRAGMENT_SIZE;
1247 else if (fragment_size > MAX_PCM_OFFLOAD_FRAGMENT_SIZE)
1248 fragment_size = MAX_PCM_OFFLOAD_FRAGMENT_SIZE;
1249 /*To have same PCM samples for all channels, the buffer size requires to
1250 *be multiple of (number of channels * bytes per sample)
1251 *For writes to succeed, the buffer must be written at address which is multiple of 32
1252 */
Aniket Kumar Lataf0531e02017-09-07 15:14:46 -07001253 fragment_size = ALIGN(fragment_size, (bytes_per_sample * noOfChannels));
1254 fragment_size = ALIGN(fragment_size, 32);
Ashish Jainf1eaa582016-05-23 20:54:24 +05301255
1256 ALOGI("PCM offload Fragment size to %d bytes", fragment_size);
1257 return fragment_size;
1258}
1259
1260/* Calculates the fragment size required to configure compress session.
1261 * Based on the alsa format selected, decide if conversion is needed in
1262
1263 * HAL ( e.g. convert AUDIO_FORMAT_PCM_FLOAT input format to
1264 * AUDIO_FORMAT_PCM_24_BIT_PACKED before writing to the compress driver.
1265 */
1266void audio_extn_utils_update_direct_pcm_fragment_size(struct stream_out *out)
1267{
Ashish Jain83a6cc22016-06-28 14:34:17 +05301268 audio_format_t dst_format = out->hal_op_format;
1269 audio_format_t src_format = out->hal_ip_format;
Ashish Jainf1eaa582016-05-23 20:54:24 +05301270 uint32_t hal_op_bytes_per_sample = audio_bytes_per_sample(dst_format);
1271 uint32_t hal_ip_bytes_per_sample = audio_bytes_per_sample(src_format);
1272
1273 out->compr_config.fragment_size =
1274 get_alsa_fragment_size(hal_op_bytes_per_sample,
1275 out->sample_rate,
1276 popcount(out->channel_mask));
1277
1278 if ((src_format != dst_format) &&
1279 hal_op_bytes_per_sample != hal_ip_bytes_per_sample) {
1280
Ashish Jain83a6cc22016-06-28 14:34:17 +05301281 out->hal_fragment_size =
Ashish Jainf1eaa582016-05-23 20:54:24 +05301282 ((out->compr_config.fragment_size * hal_ip_bytes_per_sample) /
1283 hal_op_bytes_per_sample);
1284 ALOGI("enable conversion hal_input_fragment_size is %d src_format %x dst_format %x",
Ashish Jain83a6cc22016-06-28 14:34:17 +05301285 out->hal_fragment_size, src_format, dst_format);
Ashish Jainf1eaa582016-05-23 20:54:24 +05301286 } else {
Ashish Jain83a6cc22016-06-28 14:34:17 +05301287 out->hal_fragment_size = out->compr_config.fragment_size;
Ashish Jainf1eaa582016-05-23 20:54:24 +05301288 }
1289}
1290
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301291/* converts pcm format 24_8 to 8_24 inplace */
1292size_t audio_extn_utils_convert_format_24_8_to_8_24(void *buf, size_t bytes)
1293{
1294 size_t i = 0;
1295 int *int_buf_stream = buf;
1296
1297 if ((bytes % 4) != 0) {
1298 ALOGE("%s: wrong inout buffer! ... is not 32 bit aligned ", __func__);
1299 return -EINVAL;
1300 }
1301
1302 for (; i < (bytes / 4); i++)
1303 int_buf_stream[i] >>= 8;
1304
1305 return bytes;
1306}
1307
1308int get_snd_codec_id(audio_format_t format)
1309{
1310 int id = 0;
1311
1312 switch (format & AUDIO_FORMAT_MAIN_MASK) {
1313 case AUDIO_FORMAT_MP3:
1314 id = SND_AUDIOCODEC_MP3;
1315 break;
1316 case AUDIO_FORMAT_AAC:
1317 id = SND_AUDIOCODEC_AAC;
1318 break;
1319 case AUDIO_FORMAT_AAC_ADTS:
1320 id = SND_AUDIOCODEC_AAC;
1321 break;
Arun Kumar Dasari3b174182016-12-27 13:01:14 +05301322 case AUDIO_FORMAT_AAC_LATM:
1323 id = SND_AUDIOCODEC_AAC;
1324 break;
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301325 case AUDIO_FORMAT_PCM:
1326 id = SND_AUDIOCODEC_PCM;
1327 break;
1328 case AUDIO_FORMAT_FLAC:
1329 id = SND_AUDIOCODEC_FLAC;
1330 break;
1331 case AUDIO_FORMAT_ALAC:
1332 id = SND_AUDIOCODEC_ALAC;
1333 break;
1334 case AUDIO_FORMAT_APE:
1335 id = SND_AUDIOCODEC_APE;
1336 break;
1337 case AUDIO_FORMAT_VORBIS:
1338 id = SND_AUDIOCODEC_VORBIS;
1339 break;
1340 case AUDIO_FORMAT_WMA:
1341 id = SND_AUDIOCODEC_WMA;
1342 break;
1343 case AUDIO_FORMAT_WMA_PRO:
1344 id = SND_AUDIOCODEC_WMA_PRO;
1345 break;
Satish Babu Patakokila0c313922016-12-08 12:07:08 +05301346 case AUDIO_FORMAT_MP2:
1347 id = SND_AUDIOCODEC_MP2;
1348 break;
Satish Babu Patakokila915ecba2017-01-10 17:43:56 +05301349 case AUDIO_FORMAT_AC3:
1350 id = SND_AUDIOCODEC_AC3;
1351 break;
1352 case AUDIO_FORMAT_E_AC3:
1353 case AUDIO_FORMAT_E_AC3_JOC:
1354 id = SND_AUDIOCODEC_EAC3;
1355 break;
1356 case AUDIO_FORMAT_DTS:
1357 case AUDIO_FORMAT_DTS_HD:
1358 id = SND_AUDIOCODEC_DTS;
1359 break;
Ben Romberger1aaaf862017-04-06 17:49:46 -07001360 case AUDIO_FORMAT_DOLBY_TRUEHD:
1361 id = SND_AUDIOCODEC_TRUEHD;
1362 break;
Naresh Tanniru928f0862017-04-07 16:44:23 -07001363 case AUDIO_FORMAT_IEC61937:
1364 id = SND_AUDIOCODEC_IEC61937;
1365 break;
Preetam Singh Ranawat4277a5a2017-01-18 19:02:24 +05301366 case AUDIO_FORMAT_DSD:
1367 id = SND_AUDIOCODEC_DSD;
1368 break;
Dhanalakshmi Siddani18737932016-11-29 17:33:17 +05301369 case AUDIO_FORMAT_APTX:
1370 id = SND_AUDIOCODEC_APTX;
1371 break;
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301372 default:
1373 ALOGE("%s: Unsupported audio format :%x", __func__, format);
1374 }
1375
1376 return id;
1377}
1378
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001379void audio_extn_utils_send_audio_calibration(struct audio_device *adev,
1380 struct audio_usecase *usecase)
1381{
1382 int type = usecase->type;
1383
Dhananjay Kumar14245982017-01-16 20:21:00 +05301384 if (type == PCM_PLAYBACK && usecase->stream.out != NULL) {
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001385 struct stream_out *out = usecase->stream.out;
1386 int snd_device = usecase->out_snd_device;
1387 snd_device = (snd_device == SND_DEVICE_OUT_SPEAKER) ?
Xiaojun Sang040cc9f2015-08-03 19:38:28 +08001388 platform_get_spkr_prot_snd_device(snd_device) : snd_device;
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05301389 platform_send_audio_calibration(adev->platform, usecase,
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001390 out->app_type_cfg.app_type,
Preetam Singh Ranawat61716b12015-12-14 11:55:24 +05301391 usecase->stream.out->app_type_cfg.sample_rate);
Dhananjay Kumar14245982017-01-16 20:21:00 +05301392 } else if (type == PCM_CAPTURE && usecase->stream.in != NULL) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301393 platform_send_audio_calibration(adev->platform, usecase,
1394 usecase->stream.in->app_type_cfg.app_type,
1395 usecase->stream.in->app_type_cfg.sample_rate);
Vidyakumar Athota653aaef2017-03-16 11:11:31 -07001396 } else if (type == PCM_HFP_CALL || type == PCM_CAPTURE) {
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05301397 /* when app type is default. the sample rate is not used to send cal */
1398 platform_send_audio_calibration(adev->platform, usecase,
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301399 platform_get_default_app_type_v2(adev->platform, usecase->type),
1400 48000);
Siddartha Shaik343abc62017-08-08 11:15:25 +05301401 } else if (type == TRANSCODE_LOOPBACK && usecase->stream.inout != NULL) {
1402 int snd_device = usecase->out_snd_device;
1403 snd_device = (snd_device == SND_DEVICE_OUT_SPEAKER) ?
1404 platform_get_spkr_prot_snd_device(snd_device) : snd_device;
1405 platform_send_audio_calibration(adev->platform, usecase,
1406 platform_get_default_app_type_v2(adev->platform, usecase->type),
1407 usecase->stream.inout->out_config.sample_rate);
Vidyakumar Athotae57f6002017-03-01 13:13:16 -08001408 } else {
1409 /* No need to send audio calibration for voice and voip call usecases */
1410 if ((type != VOICE_CALL) && (type != VOIP_CALL))
1411 ALOGW("%s: No audio calibration for usecase type = %d", __func__, type);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001412 }
1413}
1414
Ben Rombergera04fabc2014-11-14 12:16:03 -08001415// Base64 Encode and Decode
1416// Not all features supported. This must be used only with following conditions.
1417// Decode Modes: Support with and without padding
1418// CRLF not handling. So no CRLF in string to decode.
1419// Encode Modes: Supports only padding
1420int b64decode(char *inp, int ilen, uint8_t* outp)
1421{
1422 int i, j, k, ii, num;
1423 int rem, pcnt;
1424 uint32_t res=0;
1425 uint8_t getIndex[MAX_BASEINDEX_LEN];
1426 uint8_t tmp, cflag;
1427
1428 if(inp == NULL || outp == NULL || ilen <= 0) {
1429 ALOGE("[%s] received NULL pointer or zero length",__func__);
1430 return -1;
1431 }
1432
1433 memset(getIndex, MAX_BASEINDEX_LEN-1, sizeof(getIndex));
1434 for(i=0;i<BASE_TABLE_SIZE;i++) {
1435 getIndex[(uint8_t)bTable[i]] = (uint8_t)i;
1436 }
1437 getIndex[(uint8_t)'=']=0;
1438
1439 j=0;k=0;
1440 num = ilen/4;
1441 rem = ilen%4;
1442 if(rem==0)
1443 num = num-1;
1444 cflag=0;
1445 for(i=0; i<num; i++) {
1446 res=0;
1447 for(ii=0;ii<4;ii++) {
1448 res = res << 6;
1449 tmp = getIndex[(uint8_t)inp[j++]];
1450 res = res | tmp;
1451 cflag = cflag | tmp;
1452 }
1453 outp[k++] = (res >> 16)&0xFF;
1454 outp[k++] = (res >> 8)&0xFF;
1455 outp[k++] = res & 0xFF;
1456 }
1457
1458 // Handle last bytes special
1459 pcnt=0;
1460 if(rem == 0) {
1461 //With padding or full data
1462 res = 0;
1463 for(ii=0;ii<4;ii++) {
1464 if(inp[j] == '=')
1465 pcnt++;
1466 res = res << 6;
1467 tmp = getIndex[(uint8_t)inp[j++]];
1468 res = res | tmp;
1469 cflag = cflag | tmp;
1470 }
1471 outp[k++] = res >> 16;
1472 if(pcnt == 2)
1473 goto done;
1474 outp[k++] = (res>>8)&0xFF;
1475 if(pcnt == 1)
1476 goto done;
1477 outp[k++] = res&0xFF;
1478 } else {
1479 //without padding
1480 res = 0;
1481 for(i=0;i<rem;i++) {
1482 res = res << 6;
1483 tmp = getIndex[(uint8_t)inp[j++]];
1484 res = res | tmp;
1485 cflag = cflag | tmp;
1486 }
1487 for(i=rem;i<4;i++) {
1488 res = res << 6;
1489 pcnt++;
1490 }
1491 outp[k++] = res >> 16;
1492 if(pcnt == 2)
1493 goto done;
1494 outp[k++] = (res>>8)&0xFF;
1495 if(pcnt == 1)
1496 goto done;
1497 outp[k++] = res&0xFF;
1498 }
1499done:
1500 if(cflag == 0xFF) {
1501 ALOGE("[%s] base64 decode failed. Invalid character found %s",
1502 __func__, inp);
1503 return 0;
1504 }
1505 return k;
1506}
1507
1508int b64encode(uint8_t *inp, int ilen, char* outp)
1509{
1510 int i,j,k, num;
1511 int rem=0;
1512 uint32_t res=0;
1513
1514 if(inp == NULL || outp == NULL || ilen<=0) {
1515 ALOGE("[%s] received NULL pointer or zero input length",__func__);
1516 return -1;
1517 }
1518
1519 num = ilen/3;
1520 rem = ilen%3;
1521 j=0;k=0;
1522 for(i=0; i<num; i++) {
1523 //prepare index
1524 res = inp[j++]<<16;
1525 res = res | inp[j++]<<8;
1526 res = res | inp[j++];
1527 //get output map from index
1528 outp[k++] = (char) bTable[(res>>18)&0x3F];
1529 outp[k++] = (char) bTable[(res>>12)&0x3F];
1530 outp[k++] = (char) bTable[(res>>6)&0x3F];
1531 outp[k++] = (char) bTable[res&0x3F];
1532 }
1533
1534 switch(rem) {
1535 case 1:
1536 res = inp[j++]<<16;
1537 outp[k++] = (char) bTable[res>>18];
1538 outp[k++] = (char) bTable[(res>>12)&0x3F];
1539 //outp[k++] = '=';
1540 //outp[k++] = '=';
1541 break;
1542 case 2:
1543 res = inp[j++]<<16;
1544 res = res | inp[j++]<<8;
1545 outp[k++] = (char) bTable[res>>18];
1546 outp[k++] = (char) bTable[(res>>12)&0x3F];
1547 outp[k++] = (char) bTable[(res>>6)&0x3F];
1548 //outp[k++] = '=';
1549 break;
1550 default:
1551 break;
1552 }
Ben Rombergera04fabc2014-11-14 12:16:03 -08001553 outp[k] = '\0';
1554 return k;
1555}
Ashish Jain81eb2a82015-05-13 10:52:34 +05301556
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301557
1558int audio_extn_utils_get_codec_version(const char *snd_card_name,
1559 int card_num,
1560 char *codec_version)
1561{
1562 char procfs_path[50];
1563 FILE *fp;
1564
1565 if (strstr(snd_card_name, "tasha")) {
1566 snprintf(procfs_path, sizeof(procfs_path),
1567 "/proc/asound/card%d/codecs/tasha/version", card_num);
1568 if ((fp = fopen(procfs_path, "r")) != NULL) {
1569 fgets(codec_version, CODEC_VERSION_MAX_LENGTH, fp);
1570 fclose(fp);
1571 } else {
1572 ALOGE("%s: ERROR. cannot open %s", __func__, procfs_path);
1573 return -ENOENT;
1574 }
1575 ALOGD("%s: codec version %s", __func__, codec_version);
1576 }
1577
1578 return 0;
1579}
1580
1581
Ashish Jain81eb2a82015-05-13 10:52:34 +05301582#ifdef AUDIO_EXTERNAL_HDMI_ENABLED
1583
1584void get_default_compressed_channel_status(
1585 unsigned char *channel_status)
1586{
Ashish Jain81eb2a82015-05-13 10:52:34 +05301587 memset(channel_status,0,24);
1588
1589 /* block start bit in preamble bit 3 */
1590 channel_status[0] |= PROFESSIONAL;
1591 //compre out
1592 channel_status[0] |= NON_LPCM;
1593 // sample rate; fixed 48K for default/transcode
1594 channel_status[3] |= SR_48000;
1595}
1596
1597#ifdef HDMI_PASSTHROUGH_ENABLED
1598int32_t get_compressed_channel_status(void *audio_stream_data,
1599 uint32_t audio_frame_size,
1600 unsigned char *channel_status,
1601 enum audio_parser_code_type codec_type)
1602 // codec_type - AUDIO_PARSER_CODEC_AC3
1603 // - AUDIO_PARSER_CODEC_DTS
1604{
1605 unsigned char *stream;
1606 int ret = 0;
1607 stream = (unsigned char *)audio_stream_data;
1608
1609 if (audio_stream_data == NULL || audio_frame_size == 0) {
1610 ALOGW("no buffer to get channel status, return default for compress");
1611 get_default_compressed_channel_status(channel_status);
1612 return ret;
1613 }
1614
1615 memset(channel_status,0,24);
1616 if(init_audio_parser(stream, audio_frame_size, codec_type) == -1)
1617 {
1618 ALOGE("init audio parser failed");
1619 return -1;
1620 }
1621 ret = get_channel_status(channel_status, codec_type);
1622 return ret;
1623
1624}
1625
1626#endif
1627
1628void get_lpcm_channel_status(uint32_t sampleRate,
1629 unsigned char *channel_status)
1630{
1631 int32_t status = 0;
Ashish Jain81eb2a82015-05-13 10:52:34 +05301632 memset(channel_status,0,24);
1633 /* block start bit in preamble bit 3 */
1634 channel_status[0] |= PROFESSIONAL;
1635 //LPCM OUT
1636 channel_status[0] &= ~NON_LPCM;
1637
1638 switch (sampleRate) {
1639 case 8000:
1640 case 11025:
1641 case 12000:
1642 case 16000:
1643 case 22050:
1644 channel_status[3] |= SR_NOTID;
Preetam Singh Ranawat61716b12015-12-14 11:55:24 +05301645 break;
Ashish Jain81eb2a82015-05-13 10:52:34 +05301646 case 24000:
1647 channel_status[3] |= SR_24000;
1648 break;
1649 case 32000:
1650 channel_status[3] |= SR_32000;
1651 break;
1652 case 44100:
1653 channel_status[3] |= SR_44100;
1654 break;
1655 case 48000:
1656 channel_status[3] |= SR_48000;
1657 break;
1658 case 88200:
1659 channel_status[3] |= SR_88200;
1660 break;
1661 case 96000:
1662 channel_status[3] |= SR_96000;
1663 break;
1664 case 176400:
1665 channel_status[3] |= SR_176400;
1666 break;
1667 case 192000:
1668 channel_status[3] |= SR_192000;
1669 break;
1670 default:
1671 ALOGV("Invalid sample_rate %u\n", sampleRate);
1672 status = -1;
1673 break;
1674 }
1675}
1676
1677void audio_utils_set_hdmi_channel_status(struct stream_out *out, char * buffer, size_t bytes)
1678{
1679 unsigned char channel_status[24]={0};
1680 struct snd_aes_iec958 iec958;
1681 const char *mixer_ctl_name = "IEC958 Playback PCM Stream";
1682 struct mixer_ctl *ctl;
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +05301683 ALOGV("%s: buffer %s bytes %zd", __func__, buffer, bytes);
Ashish Jain81eb2a82015-05-13 10:52:34 +05301684#ifdef HDMI_PASSTHROUGH_ENABLED
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05301685 if (audio_extn_utils_is_dolby_format(out->format) &&
Ashish Jain81eb2a82015-05-13 10:52:34 +05301686 /*TODO:Extend code to support DTS passthrough*/
1687 /*set compressed channel status bits*/
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05301688 audio_extn_passthru_is_passthrough_stream(out)){
Ashish Jain81eb2a82015-05-13 10:52:34 +05301689 get_compressed_channel_status(buffer, bytes, channel_status, AUDIO_PARSER_CODEC_AC3);
1690 } else
1691#endif
1692 {
1693 /*set channel status bit for LPCM*/
1694 get_lpcm_channel_status(out->sample_rate, channel_status);
1695 }
1696
1697 memcpy(iec958.status, channel_status,sizeof(iec958.status));
1698 ctl = mixer_get_ctl_by_name(out->dev->mixer, mixer_ctl_name);
1699 if (!ctl) {
1700 ALOGE("%s: Could not get ctl for mixer cmd - %s",
1701 __func__, mixer_ctl_name);
1702 return;
1703 }
1704 if (mixer_ctl_set_array(ctl, &iec958, sizeof(iec958)) < 0) {
1705 ALOGE("%s: Could not set channel status for ext HDMI ",
1706 __func__);
1707 return;
1708 }
1709
1710}
1711#endif
Manish Dewangan3ccdea52017-02-13 19:31:54 +05301712
1713int audio_extn_utils_get_avt_device_drift(
1714 struct audio_usecase *usecase,
1715 struct audio_avt_device_drift_param *drift_param)
1716{
1717 int ret = 0, count = 0;
1718 char avt_device_drift_mixer_ctl_name[MIXER_PATH_MAX_LENGTH] = {0};
Naresh Tanniru6160c712017-04-17 15:43:48 +05301719 const char *backend = NULL;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05301720 struct mixer_ctl *ctl = NULL;
1721 struct audio_avt_device_drift_stats drift_stats;
1722 struct audio_device *adev = NULL;
1723
1724 if (usecase != NULL && usecase->type == PCM_PLAYBACK) {
Naresh Tanniru6160c712017-04-17 15:43:48 +05301725 backend = platform_get_snd_device_backend_interface(usecase->out_snd_device);
1726 if (!backend) {
1727 ALOGE("%s: Unsupported device %d", __func__,
1728 usecase->stream.out->devices);
1729 ret = -EINVAL;
1730 goto done;
1731 }
1732 strlcpy(avt_device_drift_mixer_ctl_name,
1733 backend,
1734 MIXER_PATH_MAX_LENGTH);
1735
1736 count = strlen(backend);
1737 if (MIXER_PATH_MAX_LENGTH - count > 0) {
1738 strlcat(&avt_device_drift_mixer_ctl_name[count],
1739 " DRIFT",
1740 MIXER_PATH_MAX_LENGTH - count);
1741 } else {
1742 ret = -EINVAL;
1743 goto done;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05301744 }
1745 } else {
Naresh Tanniru7586e292017-04-13 10:38:13 +05301746 ALOGE("%s: Invalid usecase",__func__);
Manish Dewangan3ccdea52017-02-13 19:31:54 +05301747 ret = -EINVAL;
Naresh Tanniru6160c712017-04-17 15:43:48 +05301748 goto done;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05301749 }
1750
Naresh Tanniru6160c712017-04-17 15:43:48 +05301751 adev = usecase->stream.out->dev;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05301752 ctl = mixer_get_ctl_by_name(adev->mixer, avt_device_drift_mixer_ctl_name);
1753 if (!ctl) {
1754 ALOGE("%s: Could not get ctl for mixer cmd - %s",
1755 __func__, avt_device_drift_mixer_ctl_name);
1756
1757 ret = -EINVAL;
1758 goto done;
1759 }
1760
1761 ALOGV("%s: Getting AV Timer vs Device Drift mixer ctrl name %s", __func__,
1762 avt_device_drift_mixer_ctl_name);
1763
1764 mixer_ctl_update(ctl);
1765 count = mixer_ctl_get_num_values(ctl);
1766 if (count != sizeof(struct audio_avt_device_drift_stats)) {
1767 ALOGE("%s: mixer_ctl_get_num_values() invalid drift_stats data size",
1768 __func__);
1769
1770 ret = -EINVAL;
1771 goto done;
1772 }
1773
1774 ret = mixer_ctl_get_array(ctl, (void *)&drift_stats, count);
1775 if (ret != 0) {
1776 ALOGE("%s: mixer_ctl_get_array() failed to get drift_stats Params",
1777 __func__);
1778
1779 ret = -EINVAL;
1780 goto done;
1781 }
1782 memcpy(drift_param, &drift_stats.drift_param,
1783 sizeof(struct audio_avt_device_drift_param));
1784done:
1785 return ret;
1786}
Manish Dewangan07de2142017-02-27 19:27:20 +05301787
1788#ifdef SNDRV_COMPRESS_PATH_DELAY
1789int audio_extn_utils_compress_get_dsp_latency(struct stream_out *out)
1790{
1791 int ret = -EINVAL;
1792 struct snd_compr_metadata metadata;
1793 int delay_ms = COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
1794
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -07001795 if (property_get_bool("vendor.audio.playback.dsp.pathdelay", false)) {
Manish Dewangan07de2142017-02-27 19:27:20 +05301796 ALOGD("%s:: Quering DSP delay %d",__func__, __LINE__);
1797 if (!(is_offload_usecase(out->usecase))) {
1798 ALOGE("%s:: not supported for non offload session", __func__);
1799 goto exit;
1800 }
1801
1802 if (!out->compr) {
1803 ALOGD("%s:: Invalid compress handle,returning default dsp latency",
1804 __func__);
1805 goto exit;
1806 }
1807
1808 metadata.key = SNDRV_COMPRESS_PATH_DELAY;
1809 ret = compress_get_metadata(out->compr, &metadata);
1810 if(ret) {
1811 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
1812 goto exit;
1813 }
1814 delay_ms = metadata.value[0] / 1000; /*convert to ms*/
1815 } else {
1816 ALOGD("%s:: Using Fix DSP delay",__func__);
1817 }
1818
1819exit:
1820 ALOGD("%s:: delay in ms is %d",__func__, delay_ms);
1821 return delay_ms;
1822}
1823#else
1824int audio_extn_utils_compress_get_dsp_latency(struct stream_out *out __unused)
1825{
1826 return COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
1827}
1828#endif
Manish Dewangan69426c82017-01-30 17:35:36 +05301829
1830#ifdef SNDRV_COMPRESS_RENDER_MODE
1831int audio_extn_utils_compress_set_render_mode(struct stream_out *out)
1832{
1833 struct snd_compr_metadata metadata;
1834 int ret = -EINVAL;
1835
1836 if (!(is_offload_usecase(out->usecase))) {
1837 ALOGE("%s:: not supported for non offload session", __func__);
1838 goto exit;
1839 }
1840
1841 if (!out->compr) {
1842 ALOGD("%s:: Invalid compress handle",
1843 __func__);
1844 goto exit;
1845 }
1846
1847 ALOGD("%s:: render mode %d", __func__, out->render_mode);
1848
1849 metadata.key = SNDRV_COMPRESS_RENDER_MODE;
1850 if (out->render_mode == RENDER_MODE_AUDIO_MASTER) {
1851 metadata.value[0] = SNDRV_COMPRESS_RENDER_MODE_AUDIO_MASTER;
1852 } else if (out->render_mode == RENDER_MODE_AUDIO_STC_MASTER) {
1853 metadata.value[0] = SNDRV_COMPRESS_RENDER_MODE_STC_MASTER;
1854 } else {
1855 ret = 0;
1856 goto exit;
1857 }
1858 ret = compress_set_metadata(out->compr, &metadata);
1859 if(ret) {
1860 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
1861 }
1862exit:
1863 return ret;
1864}
1865#else
1866int audio_extn_utils_compress_set_render_mode(struct stream_out *out __unused)
1867{
1868 ALOGD("%s:: configuring render mode not supported", __func__);
1869 return 0;
1870}
1871#endif
Manish Dewangan58229382017-02-02 15:48:41 +05301872
1873#ifdef SNDRV_COMPRESS_CLK_REC_MODE
1874int audio_extn_utils_compress_set_clk_rec_mode(
1875 struct audio_usecase *usecase)
1876{
1877 struct snd_compr_metadata metadata;
1878 struct stream_out *out = NULL;
1879 int ret = -EINVAL;
1880
Naresh Tanniru7586e292017-04-13 10:38:13 +05301881 if (usecase == NULL || usecase->type != PCM_PLAYBACK) {
Manish Dewangan58229382017-02-02 15:48:41 +05301882 ALOGE("%s:: Invalid use case", __func__);
1883 goto exit;
1884 }
1885
1886 out = usecase->stream.out;
1887 if (!out) {
1888 ALOGE("%s:: invalid stream", __func__);
1889 goto exit;
1890 }
1891
1892 if (!is_offload_usecase(out->usecase)) {
1893 ALOGE("%s:: not supported for non offload session", __func__);
1894 goto exit;
1895 }
1896
1897 if (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER) {
1898 ALOGD("%s:: clk recovery is only supported in STC render mode",
1899 __func__);
1900 ret = 0;
1901 goto exit;
1902 }
1903
1904 if (!out->compr) {
1905 ALOGD("%s:: Invalid compress handle",
1906 __func__);
1907 goto exit;
1908 }
1909 metadata.key = SNDRV_COMPRESS_CLK_REC_MODE;
1910 switch(usecase->out_snd_device) {
1911 case SND_DEVICE_OUT_HDMI:
1912 case SND_DEVICE_OUT_SPEAKER_AND_HDMI:
1913 case SND_DEVICE_OUT_DISPLAY_PORT:
1914 case SND_DEVICE_OUT_SPEAKER_AND_DISPLAY_PORT:
1915 metadata.value[0] = SNDRV_COMPRESS_CLK_REC_MODE_NONE;
1916 break;
1917 default:
1918 metadata.value[0] = SNDRV_COMPRESS_CLK_REC_MODE_AUTO;
1919 break;
1920 }
1921
1922 ALOGD("%s:: clk recovery mode %d",__func__, metadata.value[0]);
1923
1924 ret = compress_set_metadata(out->compr, &metadata);
1925 if(ret) {
1926 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
1927 }
1928
1929exit:
1930 return ret;
1931}
1932#else
1933int audio_extn_utils_compress_set_clk_rec_mode(
1934 struct audio_usecase *usecase __unused)
1935{
1936 ALOGD("%s:: configuring render mode not supported", __func__);
1937 return 0;
1938}
1939#endif
Manish Dewangan27346042017-03-01 12:56:12 +05301940
1941#ifdef SNDRV_COMPRESS_RENDER_WINDOW
1942int audio_extn_utils_compress_set_render_window(
1943 struct stream_out *out,
1944 struct audio_out_render_window_param *render_window)
1945{
1946 struct snd_compr_metadata metadata;
1947 int ret = -EINVAL;
1948
Manish Dewangan27346042017-03-01 12:56:12 +05301949 if(render_window == NULL) {
1950 ALOGE("%s:: Invalid render_window", __func__);
1951 goto exit;
1952 }
1953
Aniket Kumar Lata1e1d3662017-06-01 18:45:48 -07001954 ALOGD("%s:: render window start 0x%"PRIx64" end 0x%"PRIx64"",
1955 __func__,render_window->render_ws, render_window->render_we);
1956
Manish Dewangan27346042017-03-01 12:56:12 +05301957 if (!is_offload_usecase(out->usecase)) {
1958 ALOGE("%s:: not supported for non offload session", __func__);
1959 goto exit;
1960 }
1961
Naresh Tanniru6160c712017-04-17 15:43:48 +05301962 if ((out->render_mode != RENDER_MODE_AUDIO_MASTER) &&
1963 (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER)) {
Manish Dewangan27346042017-03-01 12:56:12 +05301964 ALOGD("%s:: only supported in timestamp mode, current "
1965 "render mode mode %d", __func__, out->render_mode);
1966 goto exit;
1967 }
1968
1969 if (!out->compr) {
1970 ALOGW("%s:: offload session not yet opened,"
1971 "render window will be configure later", __func__);
1972 /* store render window to reconfigure in start_output_stream() */
1973 goto exit;
1974 }
1975
1976 metadata.key = SNDRV_COMPRESS_RENDER_WINDOW;
1977 /*render window start value */
1978 metadata.value[0] = 0xFFFFFFFF & render_window->render_ws; /* lsb */
1979 metadata.value[1] = \
1980 (0xFFFFFFFF00000000 & render_window->render_ws) >> 32; /* msb*/
1981 /*render window end value */
1982 metadata.value[2] = 0xFFFFFFFF & render_window->render_we; /* lsb */
1983 metadata.value[3] = \
1984 (0xFFFFFFFF00000000 & render_window->render_we) >> 32; /* msb*/
1985
1986 ret = compress_set_metadata(out->compr, &metadata);
1987 if(ret) {
1988 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
1989 }
1990
1991exit:
1992 return ret;
1993}
1994#else
1995int audio_extn_utils_compress_set_render_window(
1996 struct stream_out *out __unused,
1997 struct audio_out_render_window_param *render_window __unused)
1998{
1999 ALOGD("%s:: configuring render window not supported", __func__);
2000 return 0;
2001}
2002#endif
Manish Dewangan14956cc2017-02-14 18:54:42 +05302003
2004#ifdef SNDRV_COMPRESS_START_DELAY
2005int audio_extn_utils_compress_set_start_delay(
2006 struct stream_out *out,
2007 struct audio_out_start_delay_param *delay_param)
2008{
2009 struct snd_compr_metadata metadata;
2010 int ret = -EINVAL;
2011
2012 if(delay_param == NULL) {
2013 ALOGE("%s:: Invalid delay_param", __func__);
2014 goto exit;
2015 }
2016
2017 ALOGD("%s:: render start delay 0x%"PRIx64" ", __func__,
2018 delay_param->start_delay);
2019
2020 if (!is_offload_usecase(out->usecase)) {
2021 ALOGE("%s:: not supported for non offload session", __func__);
2022 goto exit;
2023 }
2024
Naresh Tanniru6160c712017-04-17 15:43:48 +05302025 if ((out->render_mode != RENDER_MODE_AUDIO_MASTER) &&
2026 (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER)) {
Manish Dewangan14956cc2017-02-14 18:54:42 +05302027 ALOGD("%s:: only supported in timestamp mode, current "
2028 "render mode mode %d", __func__, out->render_mode);
2029 goto exit;
2030 }
2031
2032 if (!out->compr) {
2033 ALOGW("%s:: offload session not yet opened,"
2034 "start delay will be configure later", __func__);
2035 goto exit;
2036 }
2037
2038 metadata.key = SNDRV_COMPRESS_START_DELAY;
2039 metadata.value[0] = 0xFFFFFFFF & delay_param->start_delay; /* lsb */
2040 metadata.value[1] = \
2041 (0xFFFFFFFF00000000 & delay_param->start_delay) >> 32; /* msb*/
2042
2043 ret = compress_set_metadata(out->compr, &metadata);
2044 if(ret) {
2045 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2046 }
2047
2048exit:
2049 return ret;
2050}
2051#else
2052int audio_extn_utils_compress_set_start_delay(
2053 struct stream_out *out __unused,
2054 struct audio_out_start_delay_param *delay_param __unused)
2055{
2056 ALOGD("%s:: configuring render window not supported", __func__);
2057 return 0;
2058}
2059#endif
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002060
2061#define MAX_SND_CARD 8
2062#define RETRY_US 500000
2063#define RETRY_NUMBER 10
2064
2065int audio_extn_utils_get_snd_card_num()
2066{
2067
2068 void *hw_info = NULL;
2069 struct mixer *mixer = NULL;
2070 int retry_num = 0;
2071 int snd_card_num = 0;
2072 char* snd_card_name = NULL;
2073
2074 while (snd_card_num < MAX_SND_CARD) {
2075 mixer = mixer_open(snd_card_num);
2076
2077 while (!mixer && retry_num < RETRY_NUMBER) {
2078 usleep(RETRY_US);
2079 mixer = mixer_open(snd_card_num);
2080 retry_num++;
2081 }
2082
2083 if (!mixer) {
2084 ALOGE("%s: Unable to open the mixer card: %d", __func__,
2085 snd_card_num);
2086 retry_num = 0;
2087 snd_card_num++;
2088 continue;
2089 }
2090
2091 snd_card_name = strdup(mixer_get_name(mixer));
2092 if (!snd_card_name) {
2093 ALOGE("failed to allocate memory for snd_card_name\n");
2094 mixer_close(mixer);
2095 return -1;
2096 }
2097 ALOGD("%s: snd_card_name: %s", __func__, snd_card_name);
2098
2099 hw_info = hw_info_init(snd_card_name);
2100 if (hw_info) {
2101 ALOGD("%s: Opened sound card:%d", __func__, snd_card_num);
2102 break;
2103 }
2104 ALOGE("%s: Failed to init hardware info", __func__);
2105 retry_num = 0;
2106 snd_card_num++;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002107
Dhananjay Kumara7e27832017-07-11 15:40:39 +05302108 free(snd_card_name);
2109 snd_card_name = NULL;
2110
2111 mixer_close(mixer);
2112 mixer = NULL;
2113 }
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002114 if (snd_card_name)
2115 free(snd_card_name);
Dhananjay Kumara7e27832017-07-11 15:40:39 +05302116 if (mixer)
2117 mixer_close(mixer);
2118 if (hw_info)
2119 hw_info_deinit(hw_info);
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002120
2121 if (snd_card_num >= MAX_SND_CARD) {
2122 ALOGE("%s: Unable to find correct sound card, aborting.", __func__);
2123 return -1;
2124 }
2125
2126 return snd_card_num;
2127}
Naresh Tanniru6160c712017-04-17 15:43:48 +05302128
2129#ifdef SNDRV_COMPRESS_ENABLE_ADJUST_SESSION_CLOCK
2130int audio_extn_utils_compress_enable_drift_correction(
2131 struct stream_out *out,
2132 struct audio_out_enable_drift_correction *drift)
2133{
2134 struct snd_compr_metadata metadata;
2135 int ret = -EINVAL;
2136
2137 if(drift == NULL) {
2138 ALOGE("%s:: Invalid param", __func__);
2139 goto exit;
2140 }
2141
2142 ALOGD("%s:: drift enable %d", __func__,drift->enable);
2143
2144 if (!is_offload_usecase(out->usecase)) {
2145 ALOGE("%s:: not supported for non offload session", __func__);
2146 goto exit;
2147 }
2148
2149 if (!out->compr) {
2150 ALOGW("%s:: offload session not yet opened,"
2151 "start delay will be configure later", __func__);
2152 goto exit;
2153 }
2154
2155 metadata.key = SNDRV_COMPRESS_ENABLE_ADJUST_SESSION_CLOCK;
2156 metadata.value[0] = drift->enable;
2157 out->drift_correction_enabled = drift->enable;
2158
2159 ret = compress_set_metadata(out->compr, &metadata);
2160 if(ret) {
2161 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2162 out->drift_correction_enabled = false;
2163 }
2164
2165exit:
2166 return ret;
2167}
2168#else
2169int audio_extn_utils_compress_enable_drift_correction(
2170 struct stream_out *out __unused,
2171 struct audio_out_enable_drift_correction *drift __unused)
2172{
2173 ALOGD("%s:: configuring drift enablement not supported", __func__);
2174 return 0;
2175}
2176#endif
2177
2178#ifdef SNDRV_COMPRESS_ADJUST_SESSION_CLOCK
2179int audio_extn_utils_compress_correct_drift(
2180 struct stream_out *out,
2181 struct audio_out_correct_drift *drift_param)
2182{
2183 struct snd_compr_metadata metadata;
2184 int ret = -EINVAL;
2185
2186 if (drift_param == NULL) {
2187 ALOGE("%s:: Invalid drift_param", __func__);
2188 goto exit;
2189 }
2190
2191 ALOGD("%s:: adjust time 0x%"PRIx64" ", __func__,
2192 drift_param->adjust_time);
2193
2194 if (!is_offload_usecase(out->usecase)) {
2195 ALOGE("%s:: not supported for non offload session", __func__);
2196 goto exit;
2197 }
2198
2199 if (!out->compr) {
2200 ALOGW("%s:: offload session not yet opened", __func__);
2201 goto exit;
2202 }
2203
2204 if (!out->drift_correction_enabled) {
2205 ALOGE("%s:: drift correction not enabled", __func__);
2206 goto exit;
2207 }
2208
2209 metadata.key = SNDRV_COMPRESS_ADJUST_SESSION_CLOCK;
2210 metadata.value[0] = 0xFFFFFFFF & drift_param->adjust_time; /* lsb */
2211 metadata.value[1] = \
2212 (0xFFFFFFFF00000000 & drift_param->adjust_time) >> 32; /* msb*/
2213
2214 ret = compress_set_metadata(out->compr, &metadata);
2215 if(ret)
2216 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2217exit:
2218 return ret;
2219}
2220#else
2221int audio_extn_utils_compress_correct_drift(
2222 struct stream_out *out __unused,
2223 struct audio_out_correct_drift *drift_param __unused)
2224{
2225 ALOGD("%s:: setting adjust clock not supported", __func__);
2226 return 0;
2227}
2228#endif
Naresh Tanniru29bce4e2017-04-27 17:54:30 +05302229
2230int audio_extn_utils_set_channel_map(
2231 struct stream_out *out,
2232 struct audio_out_channel_map_param *channel_map_param)
2233{
2234 int ret = -EINVAL, i = 0;
2235 int channels = audio_channel_count_from_out_mask(out->channel_mask);
2236
2237 if (channel_map_param == NULL) {
2238 ALOGE("%s:: Invalid channel_map", __func__);
2239 goto exit;
2240 }
2241
2242 if (channel_map_param->channels != channels) {
2243 ALOGE("%s:: Channels(%d) does not match stream channels(%d)",
2244 __func__, channel_map_param->channels, channels);
2245 goto exit;
2246 }
2247
2248 for ( i = 0; i < channels; i++) {
2249 ALOGV("%s:: channel_map[%d]- %d", __func__, i, channel_map_param->channel_map[i]);
2250 out->channel_map_param.channel_map[i] = channel_map_param->channel_map[i];
2251 }
2252 ret = 0;
2253exit:
2254 return ret;
2255}
Varun Balaraje49253e2017-07-06 19:48:56 +05302256
2257int audio_extn_utils_set_pan_scale_params(
2258 struct stream_out *out,
2259 struct mix_matrix_params *mm_params)
2260{
2261 int ret = -EINVAL, i = 0, j = 0;
2262
Varun Balaraj003ee752017-08-07 17:54:55 +05302263 if (mm_params == NULL || out == NULL) {
2264 ALOGE("%s:: Invalid mix matrix or out param", __func__);
Varun Balaraje49253e2017-07-06 19:48:56 +05302265 goto exit;
2266 }
2267
2268 if (mm_params->num_output_channels > MAX_CHANNELS_SUPPORTED ||
2269 mm_params->num_output_channels <= 0 ||
2270 mm_params->num_input_channels > MAX_CHANNELS_SUPPORTED ||
2271 mm_params->num_input_channels <= 0)
2272 goto exit;
2273
2274 out->pan_scale_params.num_output_channels = mm_params->num_output_channels;
2275 out->pan_scale_params.num_input_channels = mm_params->num_input_channels;
2276 out->pan_scale_params.has_output_channel_map =
2277 mm_params->has_output_channel_map;
2278 for (i = 0; i < mm_params->num_output_channels; i++)
2279 out->pan_scale_params.output_channel_map[i] =
2280 mm_params->output_channel_map[i];
2281
2282 out->pan_scale_params.has_input_channel_map =
2283 mm_params->has_input_channel_map;
2284 for (i = 0; i < mm_params->num_input_channels; i++)
2285 out->pan_scale_params.input_channel_map[i] =
2286 mm_params->input_channel_map[i];
2287
2288 out->pan_scale_params.has_mixer_coeffs = mm_params->has_mixer_coeffs;
2289 for (i = 0; i < mm_params->num_output_channels; i++)
2290 for (j = 0; j < mm_params->num_input_channels; j++) {
2291 //Convert the channel coefficient gains in Q14 format
2292 out->pan_scale_params.mixer_coeffs[i][j] =
2293 mm_params->mixer_coeffs[i][j] * (2 << 13);
2294 }
2295
2296 ret = platform_set_stream_pan_scale_params(out->dev->platform,
2297 out->pcm_device_id,
2298 out->pan_scale_params);
2299
2300exit:
2301 return ret;
2302}
2303
2304int audio_extn_utils_set_downmix_params(
2305 struct stream_out *out,
2306 struct mix_matrix_params *mm_params)
2307{
2308 int ret = -EINVAL, i = 0, j = 0;
2309 struct audio_usecase *usecase = NULL;
2310
Aniket Kumar Lataf9f246e2017-09-15 15:20:16 -07002311 if (mm_params == NULL || out == NULL) {
Varun Balaraj003ee752017-08-07 17:54:55 +05302312 ALOGE("%s:: Invalid mix matrix or out param", __func__);
Varun Balaraje49253e2017-07-06 19:48:56 +05302313 goto exit;
2314 }
2315
2316 if (mm_params->num_output_channels > MAX_CHANNELS_SUPPORTED ||
2317 mm_params->num_output_channels <= 0 ||
2318 mm_params->num_input_channels > MAX_CHANNELS_SUPPORTED ||
2319 mm_params->num_input_channels <= 0)
2320 goto exit;
2321
2322 usecase = get_usecase_from_list(out->dev, out->usecase);
Varun Balaraj003ee752017-08-07 17:54:55 +05302323 if (!usecase) {
2324 ALOGE("%s: Get usecase list failed!", __func__);
Aniket Kumar Lataf9f246e2017-09-15 15:20:16 -07002325 goto exit;
2326 }
Varun Balaraje49253e2017-07-06 19:48:56 +05302327 out->downmix_params.num_output_channels = mm_params->num_output_channels;
2328 out->downmix_params.num_input_channels = mm_params->num_input_channels;
2329
2330 out->downmix_params.has_output_channel_map =
2331 mm_params->has_output_channel_map;
2332 for (i = 0; i < mm_params->num_output_channels; i++) {
2333 out->downmix_params.output_channel_map[i] =
2334 mm_params->output_channel_map[i];
2335 }
2336
2337 out->downmix_params.has_input_channel_map =
2338 mm_params->has_input_channel_map;
2339 for (i = 0; i < mm_params->num_input_channels; i++)
2340 out->downmix_params.input_channel_map[i] =
2341 mm_params->input_channel_map[i];
2342
2343 out->downmix_params.has_mixer_coeffs = mm_params->has_mixer_coeffs;
2344 for (i = 0; i < mm_params->num_output_channels; i++)
Nikhil Latukar2e6f6242017-08-15 13:37:07 +05302345 for (j = 0; j < mm_params->num_input_channels; j++) {
2346 //Convert the channel coefficient gains in Q14 format
Varun Balaraje49253e2017-07-06 19:48:56 +05302347 out->downmix_params.mixer_coeffs[i][j] =
Nikhil Latukar2e6f6242017-08-15 13:37:07 +05302348 mm_params->mixer_coeffs[i][j] * (2 << 13);
2349 }
Varun Balaraje49253e2017-07-06 19:48:56 +05302350
2351 ret = platform_set_stream_downmix_params(out->dev->platform,
2352 out->pcm_device_id,
2353 usecase->out_snd_device,
2354 out->downmix_params);
2355
2356exit:
2357 return ret;
2358}
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05302359
2360bool audio_extn_utils_is_dolby_format(audio_format_t format)
2361{
2362 if (format == AUDIO_FORMAT_AC3 ||
2363 format == AUDIO_FORMAT_E_AC3 ||
2364 format == AUDIO_FORMAT_E_AC3_JOC)
2365 return true;
2366 else
2367 return false;
2368}
2369
2370