blob: a599731ad14be05367ce781d45a28609b67ba730 [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),
Dhananjay Kumar704ce6f2017-09-28 22:08:00 +0530137 STRING_TO_ENUM(AUDIO_INPUT_FLAG_COMPRESS),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700138};
139
140const struct string_to_enum s_format_name_to_enum_table[] = {
Ashish Jain83a6cc22016-06-28 14:34:17 +0530141 STRING_TO_ENUM(AUDIO_FORMAT_PCM_8_BIT),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700142 STRING_TO_ENUM(AUDIO_FORMAT_PCM_16_BIT),
Ashish Jain5106d362016-05-11 19:23:33 +0530143 STRING_TO_ENUM(AUDIO_FORMAT_PCM_24_BIT_PACKED),
144 STRING_TO_ENUM(AUDIO_FORMAT_PCM_8_24_BIT),
Ashish Jainf1eaa582016-05-23 20:54:24 +0530145 STRING_TO_ENUM(AUDIO_FORMAT_PCM_32_BIT),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700146 STRING_TO_ENUM(AUDIO_FORMAT_MP3),
147 STRING_TO_ENUM(AUDIO_FORMAT_AAC),
148 STRING_TO_ENUM(AUDIO_FORMAT_VORBIS),
Mingming Yinae3530f2014-07-03 16:50:18 -0700149 STRING_TO_ENUM(AUDIO_FORMAT_AMR_NB),
150 STRING_TO_ENUM(AUDIO_FORMAT_AMR_WB),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700151 STRING_TO_ENUM(AUDIO_FORMAT_AC3),
Mingming Yinae3530f2014-07-03 16:50:18 -0700152 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700153 STRING_TO_ENUM(AUDIO_FORMAT_DTS),
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530154 STRING_TO_ENUM(AUDIO_FORMAT_DTS_HD),
Ben Romberger1aaaf862017-04-06 17:49:46 -0700155 STRING_TO_ENUM(AUDIO_FORMAT_DOLBY_TRUEHD),
Naresh Tanniru928f0862017-04-07 16:44:23 -0700156 STRING_TO_ENUM(AUDIO_FORMAT_IEC61937),
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530157#ifdef AUDIO_EXTN_FORMATS_ENABLED
158 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3_JOC),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700159 STRING_TO_ENUM(AUDIO_FORMAT_WMA),
160 STRING_TO_ENUM(AUDIO_FORMAT_WMA_PRO),
161 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADIF),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700162 STRING_TO_ENUM(AUDIO_FORMAT_AMR_WB_PLUS),
163 STRING_TO_ENUM(AUDIO_FORMAT_EVRC),
164 STRING_TO_ENUM(AUDIO_FORMAT_EVRCB),
165 STRING_TO_ENUM(AUDIO_FORMAT_EVRCWB),
166 STRING_TO_ENUM(AUDIO_FORMAT_QCELP),
167 STRING_TO_ENUM(AUDIO_FORMAT_MP2),
168 STRING_TO_ENUM(AUDIO_FORMAT_EVRCNW),
Amit Shekhar6f461b12014-08-01 14:52:58 -0700169 STRING_TO_ENUM(AUDIO_FORMAT_FLAC),
Satya Krishna Pindiproli70471602015-04-24 19:12:43 +0530170 STRING_TO_ENUM(AUDIO_FORMAT_ALAC),
171 STRING_TO_ENUM(AUDIO_FORMAT_APE),
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700172 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3_JOC),
Alexy Josephcd8eaed2014-12-11 12:46:53 -0800173 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LC),
174 STRING_TO_ENUM(AUDIO_FORMAT_AAC_HE_V1),
175 STRING_TO_ENUM(AUDIO_FORMAT_AAC_HE_V2),
Manish Dewangana6fc5442015-08-24 20:30:31 +0530176 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS),
Ashish Jaine513a872015-11-19 17:00:56 +0530177 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_LC),
178 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_HE_V1),
179 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_HE_V2),
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +0530180 STRING_TO_ENUM(AUDIO_FORMAT_DSD),
Arun Kumar Dasari3b174182016-12-27 13:01:14 +0530181 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM),
182 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM_LC),
183 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM_HE_V1),
184 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM_HE_V2),
Dhanalakshmi Siddani18737932016-11-29 17:33:17 +0530185 STRING_TO_ENUM(AUDIO_FORMAT_APTX),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700186#endif
187};
188
Manish Dewangan3ccdea52017-02-13 19:31:54 +0530189/* payload structure avt_device drift query */
190struct audio_avt_device_drift_stats {
191 uint32_t minor_version;
Manish Dewangan338c50a2017-09-12 15:22:03 +0530192
Manish Dewangan3ccdea52017-02-13 19:31:54 +0530193 /* Indicates the device interface direction as either
194 * source (Tx) or sink (Rx).
195 */
196 uint16_t device_direction;
Manish Dewangan338c50a2017-09-12 15:22:03 +0530197
198 /* Reference timer for drift accumulation and time stamp information.
199 * currently it only support AFE_REF_TIMER_TYPE_AVTIMER
200 */
201 uint16_t reference_timer;
Manish Dewangan3ccdea52017-02-13 19:31:54 +0530202 struct audio_avt_device_drift_param drift_param;
Manish Dewangan338c50a2017-09-12 15:22:03 +0530203} __attribute__((packed));
Manish Dewangan3ccdea52017-02-13 19:31:54 +0530204
Ben Rombergera04fabc2014-11-14 12:16:03 -0800205static char bTable[BASE_TABLE_SIZE] = {
206 'A','B','C','D','E','F','G','H','I','J','K','L',
207 'M','N','O','P','Q','R','S','T','U','V','W','X',
208 'Y','Z','a','b','c','d','e','f','g','h','i','j',
209 'k','l','m','n','o','p','q','r','s','t','u','v',
210 'w','x','y','z','0','1','2','3','4','5','6','7',
211 '8','9','+','/'
212};
213
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700214static uint32_t string_to_enum(const struct string_to_enum *table, size_t size,
215 const char *name)
216{
217 size_t i;
218 for (i = 0; i < size; i++) {
219 if (strcmp(table[i].name, name) == 0) {
220 ALOGV("%s found %s", __func__, table[i].name);
221 return table[i].value;
222 }
223 }
224 return 0;
225}
226
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530227static audio_io_flags_t parse_flag_names(char *name)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700228{
229 uint32_t flag = 0;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530230 audio_io_flags_t io_flags;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800231 char *last_r;
232 char *flag_name = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700233 while (flag_name != NULL) {
234 if (strlen(flag_name) != 0) {
235 flag |= string_to_enum(s_flag_name_to_enum_table,
236 ARRAY_SIZE(s_flag_name_to_enum_table),
237 flag_name);
238 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800239 flag_name = strtok_r(NULL, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700240 }
241
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800242 ALOGV("parse_flag_names: flag - %x", flag);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530243 io_flags.in_flags = (audio_input_flags_t)flag;
244 io_flags.out_flags = (audio_output_flags_t)flag;
245 return io_flags;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700246}
247
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530248static void parse_format_names(char *name, struct streams_io_cfg *s_info)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700249{
250 struct stream_format *sf_info = NULL;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800251 char *last_r;
252 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700253
254 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG) == 0)
255 return;
256
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530257 list_init(&s_info->format_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700258 while (str != NULL) {
259 audio_format_t format = (audio_format_t)string_to_enum(s_format_name_to_enum_table,
260 ARRAY_SIZE(s_format_name_to_enum_table), str);
261 ALOGV("%s: format - %d", __func__, format);
262 if (format != 0) {
263 sf_info = (struct stream_format *)calloc(1, sizeof(struct stream_format));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700264 if (sf_info == NULL)
265 break; /* return whatever was parsed */
266
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700267 sf_info->format = format;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530268 list_add_tail(&s_info->format_list, &sf_info->list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700269 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800270 str = strtok_r(NULL, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700271 }
272}
273
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530274static void parse_sample_rate_names(char *name, struct streams_io_cfg *s_info)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700275{
Amit Shekhar6f461b12014-08-01 14:52:58 -0700276 struct stream_sample_rate *ss_info = NULL;
277 uint32_t sample_rate = 48000;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800278 char *last_r;
279 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700280
Amit Shekhar6f461b12014-08-01 14:52:58 -0700281 if (str != NULL && 0 == strcmp(str, DYNAMIC_VALUE_TAG))
282 return;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700283
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530284 list_init(&s_info->sample_rate_list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700285 while (str != NULL) {
286 sample_rate = (uint32_t)strtol(str, (char **)NULL, 10);
287 ALOGV("%s: sample_rate - %d", __func__, sample_rate);
288 if (0 != sample_rate) {
289 ss_info = (struct stream_sample_rate *)calloc(1, sizeof(struct stream_sample_rate));
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800290 if (!ss_info) {
291 ALOGE("%s: memory allocation failure", __func__);
292 return;
293 }
Amit Shekhar6f461b12014-08-01 14:52:58 -0700294 ss_info->sample_rate = sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530295 list_add_tail(&s_info->sample_rate_list, &ss_info->list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700296 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800297 str = strtok_r(NULL, "|", &last_r);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700298 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700299}
300
301static int parse_bit_width_names(char *name)
302{
303 int bit_width = 16;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800304 char *last_r;
305 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700306
307 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG))
308 bit_width = (int)strtol(str, (char **)NULL, 10);
309
310 ALOGV("%s: bit_width - %d", __func__, bit_width);
311 return bit_width;
312}
313
314static int parse_app_type_names(void *platform, char *name)
315{
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700316 int app_type = platform_get_default_app_type(platform);
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800317 char *last_r;
318 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700319
320 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG))
321 app_type = (int)strtol(str, (char **)NULL, 10);
322
323 ALOGV("%s: app_type - %d", __func__, app_type);
324 return app_type;
325}
326
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530327static void update_streams_cfg_list(cnode *root, void *platform,
328 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700329{
330 cnode *node = root->first_child;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530331 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700332
333 ALOGV("%s", __func__);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530334 s_info = (struct streams_io_cfg *)calloc(1, sizeof(struct streams_io_cfg));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700335
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530336 if (!s_info) {
337 ALOGE("failed to allocate mem for s_info list element");
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700338 return;
339 }
340
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700341 while (node) {
342 if (strcmp(node->name, FLAGS_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530343 s_info->flags = parse_flag_names((char *)node->value);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530344 } else if (strcmp(node->name, PROFILES_TAG) == 0) {
345 strlcpy(s_info->profile, (char *)node->value, sizeof(s_info->profile));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700346 } else if (strcmp(node->name, FORMATS_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530347 parse_format_names((char *)node->value, s_info);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700348 } else if (strcmp(node->name, SAMPLING_RATES_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530349 s_info->app_type_cfg.sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
350 parse_sample_rate_names((char *)node->value, s_info);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700351 } else if (strcmp(node->name, BIT_WIDTH_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530352 s_info->app_type_cfg.bit_width = parse_bit_width_names((char *)node->value);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700353 } else if (strcmp(node->name, APP_TYPE_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530354 s_info->app_type_cfg.app_type = parse_app_type_names(platform, (char *)node->value);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700355 }
356 node = node->next;
357 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530358 list_add_tail(streams_cfg_list, &s_info->list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700359}
360
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530361static void load_cfg_list(cnode *root, void *platform,
362 struct listnode *streams_output_cfg_list,
363 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700364{
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530365 cnode *node = NULL;
366
367 node = config_find(root, OUTPUTS_TAG);
368 if (node != NULL) {
369 node = node->first_child;
370 while (node) {
371 ALOGV("%s: loading output %s", __func__, node->name);
372 update_streams_cfg_list(node, platform, streams_output_cfg_list);
373 node = node->next;
374 }
375 } else {
376 ALOGI("%s: could not load output, node is NULL", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700377 }
378
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530379 node = config_find(root, INPUTS_TAG);
380 if (node != NULL) {
381 node = node->first_child;
382 while (node) {
383 ALOGV("%s: loading input %s", __func__, node->name);
384 update_streams_cfg_list(node, platform, streams_input_cfg_list);
385 node = node->next;
386 }
387 } else {
388 ALOGI("%s: could not load input, node is NULL", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700389 }
390}
391
392static void send_app_type_cfg(void *platform, struct mixer *mixer,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530393 struct listnode *streams_output_cfg_list,
394 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700395{
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530396 size_t app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT] = {0};
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700397 int length = 0, i, num_app_types = 0;
398 struct listnode *node;
399 bool update;
400 struct mixer_ctl *ctl = NULL;
401 const char *mixer_ctl_name = "App Type Config";
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530402 struct streams_io_cfg *s_info = NULL;
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800403 uint32_t target_bit_width = 0;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700404
405 if (!mixer) {
406 ALOGE("%s: mixer is null",__func__);
407 return;
408 }
409 ctl = mixer_get_ctl_by_name(mixer, mixer_ctl_name);
410 if (!ctl) {
411 ALOGE("%s: Could not get ctl for mixer cmd - %s",__func__, mixer_ctl_name);
412 return;
413 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530414 app_type_cfg[length++] = num_app_types;
415
416 if (list_empty(streams_output_cfg_list)) {
417 app_type_cfg[length++] = platform_get_default_app_type_v2(platform, PCM_PLAYBACK);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700418 app_type_cfg[length++] = 48000;
419 app_type_cfg[length++] = 16;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530420 num_app_types += 1;
421 }
422 if (list_empty(streams_input_cfg_list)) {
423 app_type_cfg[length++] = platform_get_default_app_type_v2(platform, PCM_CAPTURE);
424 app_type_cfg[length++] = 48000;
425 app_type_cfg[length++] = 16;
426 num_app_types += 1;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700427 }
428
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800429 /* get target bit width for ADM enforce mode */
430 target_bit_width = adev_get_dsp_bit_width_enforce_mode();
431
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700432 list_for_each(node, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530433 s_info = node_to_item(node, struct streams_io_cfg, list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700434 update = true;
435 for (i=0; i<length; i=i+3) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530436 if (app_type_cfg[i+1] == 0)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700437 break;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530438 else if (app_type_cfg[i+1] == (size_t)s_info->app_type_cfg.app_type) {
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530439 if (app_type_cfg[i+2] < (size_t)s_info->app_type_cfg.sample_rate)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530440 app_type_cfg[i+2] = s_info->app_type_cfg.sample_rate;
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530441 if (app_type_cfg[i+3] < (size_t)s_info->app_type_cfg.bit_width)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530442 app_type_cfg[i+3] = s_info->app_type_cfg.bit_width;
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800443 /* ADM bit width = max(enforce_bit_width, bit_width from s_info */
444 if (audio_extn_is_dsp_bit_width_enforce_mode_supported(s_info->flags.out_flags) &&
445 (target_bit_width > app_type_cfg[i+3]))
446 app_type_cfg[i+3] = target_bit_width;
447
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700448 update = false;
449 break;
450 }
451 }
452 if (update && ((length + 3) <= MAX_LENGTH_MIXER_CONTROL_IN_INT)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530453 num_app_types += 1;
454 app_type_cfg[length++] = s_info->app_type_cfg.app_type;
455 app_type_cfg[length++] = s_info->app_type_cfg.sample_rate;
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800456 app_type_cfg[length] = s_info->app_type_cfg.bit_width;
457 if (audio_extn_is_dsp_bit_width_enforce_mode_supported(s_info->flags.out_flags) &&
458 (target_bit_width > app_type_cfg[length]))
459 app_type_cfg[length] = target_bit_width;
460
461 length++;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530462 }
463 }
464 list_for_each(node, streams_input_cfg_list) {
465 s_info = node_to_item(node, struct streams_io_cfg, list);
466 update = true;
467 for (i=0; i<length; i=i+3) {
468 if (app_type_cfg[i+1] == 0)
469 break;
470 else if (app_type_cfg[i+1] == (size_t)s_info->app_type_cfg.app_type) {
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530471 if (app_type_cfg[i+2] < (size_t)s_info->app_type_cfg.sample_rate)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530472 app_type_cfg[i+2] = s_info->app_type_cfg.sample_rate;
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530473 if (app_type_cfg[i+3] < (size_t)s_info->app_type_cfg.bit_width)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530474 app_type_cfg[i+3] = s_info->app_type_cfg.bit_width;
475 update = false;
476 break;
477 }
478 }
479 if (update && ((length + 3) <= MAX_LENGTH_MIXER_CONTROL_IN_INT)) {
480 num_app_types += 1;
481 app_type_cfg[length++] = s_info->app_type_cfg.app_type;
482 app_type_cfg[length++] = s_info->app_type_cfg.sample_rate;
483 app_type_cfg[length++] = s_info->app_type_cfg.bit_width;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700484 }
485 }
486 ALOGV("%s: num_app_types: %d", __func__, num_app_types);
487 if (num_app_types) {
488 app_type_cfg[0] = num_app_types;
489 mixer_ctl_set_array(ctl, app_type_cfg, length);
490 }
491}
492
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530493void audio_extn_utils_update_streams_cfg_lists(void *platform,
494 struct mixer *mixer,
495 struct listnode *streams_output_cfg_list,
496 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700497{
498 cnode *root;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530499 char *data = NULL;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700500
501 ALOGV("%s", __func__);
502 list_init(streams_output_cfg_list);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530503 list_init(streams_input_cfg_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700504
505 root = config_node("", "");
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700506 if (root == NULL) {
507 ALOGE("cfg_list, NULL config root");
508 return;
509 }
510
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530511 data = (char *)load_file(AUDIO_IO_POLICY_VENDOR_CONFIG_FILE, NULL);
512 if (data == NULL) {
513 ALOGD("%s: failed to open io config file(%s), trying older config file",
514 __func__, AUDIO_IO_POLICY_VENDOR_CONFIG_FILE);
515 data = (char *)load_file(AUDIO_OUTPUT_POLICY_VENDOR_CONFIG_FILE, NULL);
516 if (data == NULL) {
517 send_app_type_cfg(platform, mixer,
518 streams_output_cfg_list,
519 streams_input_cfg_list);
520 ALOGE("%s: could not load io policy config!", __func__);
yidongh6eb4f752017-04-19 18:20:57 +0800521 free(root);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530522 return;
523 }
524 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700525
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530526 config_load(root, data);
527 load_cfg_list(root, platform, streams_output_cfg_list,
528 streams_input_cfg_list);
529
530 send_app_type_cfg(platform, mixer, streams_output_cfg_list,
531 streams_input_cfg_list);
Alexy Josephaee4fdd2016-01-29 13:02:07 -0800532
533 config_free(root);
yidongh6eb4f752017-04-19 18:20:57 +0800534 free(root);
Alexy Josephaee4fdd2016-01-29 13:02:07 -0800535 free(data);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700536}
537
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530538static void audio_extn_utils_dump_streams_cfg_list(
539 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700540{
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700541 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530542 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700543 struct stream_format *sf_info;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700544 struct stream_sample_rate *ss_info;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530545
546 list_for_each(node_i, streams_cfg_list) {
547 s_info = node_to_item(node_i, struct streams_io_cfg, list);
548 ALOGV("%s: flags-%d, sample_rate-%d, bit_width-%d, app_type-%d",
549 __func__, s_info->flags.out_flags, s_info->app_type_cfg.sample_rate,
550 s_info->app_type_cfg.bit_width, s_info->app_type_cfg.app_type);
551 list_for_each(node_j, &s_info->format_list) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700552 sf_info = node_to_item(node_j, struct stream_format, list);
553 ALOGV("format-%x", sf_info->format);
554 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530555 list_for_each(node_j, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700556 ss_info = node_to_item(node_j, struct stream_sample_rate, list);
557 ALOGV("sample rate-%d", ss_info->sample_rate);
558 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700559 }
560}
561
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530562void audio_extn_utils_dump_streams_cfg_lists(
563 struct listnode *streams_output_cfg_list,
564 struct listnode *streams_input_cfg_list)
565{
566 ALOGV("%s", __func__);
567 audio_extn_utils_dump_streams_cfg_list(streams_output_cfg_list);
568 audio_extn_utils_dump_streams_cfg_list(streams_input_cfg_list);
569}
570
571static void audio_extn_utils_release_streams_cfg_list(
572 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700573{
574 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530575 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700576
577 ALOGV("%s", __func__);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530578
579 while (!list_empty(streams_cfg_list)) {
580 node_i = list_head(streams_cfg_list);
581 s_info = node_to_item(node_i, struct streams_io_cfg, list);
582 while (!list_empty(&s_info->format_list)) {
583 node_j = list_head(&s_info->format_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700584 list_remove(node_j);
585 free(node_to_item(node_j, struct stream_format, list));
586 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530587 while (!list_empty(&s_info->sample_rate_list)) {
588 node_j = list_head(&s_info->sample_rate_list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700589 list_remove(node_j);
590 free(node_to_item(node_j, struct stream_sample_rate, list));
591 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700592 list_remove(node_i);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530593 free(node_to_item(node_i, struct streams_io_cfg, list));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700594 }
595}
596
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530597void audio_extn_utils_release_streams_cfg_lists(
598 struct listnode *streams_output_cfg_list,
599 struct listnode *streams_input_cfg_list)
600{
601 ALOGV("%s", __func__);
602 audio_extn_utils_release_streams_cfg_list(streams_output_cfg_list);
603 audio_extn_utils_release_streams_cfg_list(streams_input_cfg_list);
604}
605
606static bool set_app_type_cfg(struct streams_io_cfg *s_info,
607 struct stream_app_type_cfg *app_type_cfg,
608 uint32_t sample_rate, uint32_t bit_width)
Amit Shekhar6f461b12014-08-01 14:52:58 -0700609 {
610 struct listnode *node_i;
611 struct stream_sample_rate *ss_info;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530612 list_for_each(node_i, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700613 ss_info = node_to_item(node_i, struct stream_sample_rate, list);
614 if ((sample_rate <= ss_info->sample_rate) &&
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530615 (bit_width == s_info->app_type_cfg.bit_width)) {
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700616
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530617 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700618 app_type_cfg->sample_rate = ss_info->sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530619 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700620 ALOGV("%s app_type_cfg->app_type %d, app_type_cfg->sample_rate %d, app_type_cfg->bit_width %d",
621 __func__, app_type_cfg->app_type, app_type_cfg->sample_rate, app_type_cfg->bit_width);
622 return true;
623 }
624 }
625 /*
626 * Reiterate through the list assuming dafault sample rate.
627 * Handles scenario where input sample rate is higher
628 * than all sample rates in list for the input bit width.
629 */
630 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
Apoorv Raghuvanshif59bb222015-02-18 12:23:23 -0800631
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530632 list_for_each(node_i, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700633 ss_info = node_to_item(node_i, struct stream_sample_rate, list);
634 if ((sample_rate <= ss_info->sample_rate) &&
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530635 (bit_width == s_info->app_type_cfg.bit_width)) {
636 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700637 app_type_cfg->sample_rate = sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530638 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Apoorv Raghuvanshif59bb222015-02-18 12:23:23 -0800639 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 -0700640 __func__, app_type_cfg->app_type, app_type_cfg->sample_rate, app_type_cfg->bit_width);
641 return true;
642 }
643 }
644 return false;
645}
646
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530647void audio_extn_utils_update_stream_input_app_type_cfg(void *platform,
648 struct listnode *streams_input_cfg_list,
649 audio_devices_t devices __unused,
650 audio_input_flags_t flags,
651 audio_format_t format,
652 uint32_t sample_rate,
653 uint32_t bit_width,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530654 char* profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530655 struct stream_app_type_cfg *app_type_cfg)
656{
657 struct listnode *node_i, *node_j;
658 struct streams_io_cfg *s_info;
659 struct stream_format *sf_info;
660
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530661 ALOGV("%s: flags: 0x%x, format: 0x%x sample_rate %d, profile %s",
662 __func__, flags, format, sample_rate, profile);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530663
664 list_for_each(node_i, streams_input_cfg_list) {
665 s_info = node_to_item(node_i, struct streams_io_cfg, list);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530666 /* Along with flags do profile matching if set at either end.*/
667 if (s_info->flags.in_flags == flags &&
668 ((profile[0] == '\0' && s_info->profile[0] == '\0') ||
669 strncmp(s_info->profile, profile, sizeof(s_info->profile)) == 0)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530670 list_for_each(node_j, &s_info->format_list) {
671 sf_info = node_to_item(node_j, struct stream_format, list);
672 if (sf_info->format == format) {
673 if (set_app_type_cfg(s_info, app_type_cfg, sample_rate, bit_width))
674 return;
675 }
676 }
677 }
678 }
679 ALOGW("%s: App type could not be selected. Falling back to default", __func__);
680 app_type_cfg->app_type = platform_get_default_app_type_v2(platform, PCM_CAPTURE);
681 app_type_cfg->sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
682 app_type_cfg->bit_width = 16;
683}
684
685void audio_extn_utils_update_stream_output_app_type_cfg(void *platform,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700686 struct listnode *streams_output_cfg_list,
Amit Shekhar1d896042014-10-03 13:16:09 -0700687 audio_devices_t devices,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700688 audio_output_flags_t flags,
689 audio_format_t format,
Amit Shekhar6f461b12014-08-01 14:52:58 -0700690 uint32_t sample_rate,
691 uint32_t bit_width,
Manish Dewangan837dc462015-05-27 10:17:41 +0530692 audio_channel_mask_t channel_mask,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530693 char *profile,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700694 struct stream_app_type_cfg *app_type_cfg)
695{
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +0530696 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530697 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700698 struct stream_format *sf_info;
Manish Dewangan837dc462015-05-27 10:17:41 +0530699 char value[PROPERTY_VALUE_MAX] = {0};
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700700
Ashish Jain058165c2016-09-28 23:18:48 +0530701 if ((bit_width >= 24) &&
Amit Shekhar1d896042014-10-03 13:16:09 -0700702 (devices & AUDIO_DEVICE_OUT_SPEAKER)) {
Amit Shekhar5a39c912014-10-14 15:39:30 -0700703 int32_t bw = platform_get_snd_device_bit_width(SND_DEVICE_OUT_SPEAKER);
704 if (-ENOSYS != bw)
705 bit_width = (uint32_t)bw;
Amit Shekhar1d896042014-10-03 13:16:09 -0700706 sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
707 ALOGI("%s Allowing 24-bit playback on speaker ONLY at default sampling rate", __func__);
708 }
709
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -0700710 property_get("vendor.audio.playback.mch.downsample",value,"");
Manish Dewangan837dc462015-05-27 10:17:41 +0530711 if (!strncmp("true", value, sizeof("true"))) {
712 if ((popcount(channel_mask) > 2) &&
713 (sample_rate > CODEC_BACKEND_DEFAULT_SAMPLE_RATE) &&
Aalique Grahame5cd12ff2017-10-19 10:57:00 -0700714 !(flags & (audio_output_flags_t)AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH)) {
Manish Dewangan837dc462015-05-27 10:17:41 +0530715 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
716 ALOGD("%s: MCH session defaulting sample rate to %d",
717 __func__, sample_rate);
718 }
719 }
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +0530720
721 /* Set sampling rate to 176.4 for DSD64
722 * and 352.8Khz for DSD128.
723 * Set Bit Width to 16. output will be 16 bit
724 * post DoP in ASM.
725 */
Aalique Grahame5cd12ff2017-10-19 10:57:00 -0700726 if ((flags & (audio_output_flags_t)AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH) &&
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +0530727 (format == AUDIO_FORMAT_DSD)) {
728 bit_width = 16;
729 if (sample_rate == INPUT_SAMPLING_RATE_DSD64)
730 sample_rate = OUTPUT_SAMPLING_RATE_DSD64;
731 else if (sample_rate == INPUT_SAMPLING_RATE_DSD128)
732 sample_rate = OUTPUT_SAMPLING_RATE_DSD128;
733 }
734
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +0530735 if(devices & AUDIO_DEVICE_OUT_ALL_A2DP) {
736 //TODO: Handle fractional sampling rate configuration for LL
737 audio_extn_a2dp_get_apptype_params(&sample_rate, &bit_width);
738 ALOGI("%s using %d sampling rate %d bit width for A2DP CoPP",
739 __func__, sample_rate, bit_width);
740 }
741
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800742 ALOGV("%s: flags: %x, format: %x sample_rate %d, profile %s, app_type %d",
743 __func__, flags, format, sample_rate, profile, app_type_cfg->app_type);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700744 list_for_each(node_i, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530745 s_info = node_to_item(node_i, struct streams_io_cfg, list);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530746 /* Along with flags do profile matching if set at either end.*/
747 if (s_info->flags.out_flags == flags &&
748 ((profile[0] == '\0' && s_info->profile[0] == '\0') ||
749 strncmp(s_info->profile, profile, sizeof(s_info->profile)) == 0)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530750 list_for_each(node_j, &s_info->format_list) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700751 sf_info = node_to_item(node_j, struct stream_format, list);
752 if (sf_info->format == format) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530753 if (set_app_type_cfg(s_info, app_type_cfg, sample_rate, bit_width))
Amit Shekhar6f461b12014-08-01 14:52:58 -0700754 return;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700755 }
756 }
757 }
758 }
759 list_for_each(node_i, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530760 s_info = node_to_item(node_i, struct streams_io_cfg, list);
761 if (s_info->flags.out_flags == AUDIO_OUTPUT_FLAG_PRIMARY) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700762 ALOGV("Compatible output profile not found.");
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530763 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
764 app_type_cfg->sample_rate = s_info->app_type_cfg.sample_rate;
765 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700766 ALOGV("%s Default to primary output: App type: %d sample_rate %d",
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530767 __func__, s_info->app_type_cfg.app_type, app_type_cfg->sample_rate);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700768 return;
769 }
770 }
771 ALOGW("%s: App type could not be selected. Falling back to default", __func__);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700772 app_type_cfg->app_type = platform_get_default_app_type(platform);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700773 app_type_cfg->sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700774 app_type_cfg->bit_width = 16;
775}
776
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +0530777static bool audio_is_this_native_usecase(struct audio_usecase *uc)
778{
779 bool native_usecase = false;
780 struct stream_out *out = (struct stream_out*) uc->stream.out;
781
782 if (PCM_PLAYBACK == uc->type && out != NULL &&
783 NATIVE_AUDIO_MODE_INVALID != platform_get_native_support() &&
784 is_offload_usecase(uc->id) &&
785 (out->sample_rate == OUTPUT_SAMPLING_RATE_44100))
786 native_usecase = true;
787
788 return native_usecase;
789}
790
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800791bool audio_extn_is_dsp_bit_width_enforce_mode_supported(audio_output_flags_t flags)
792{
793 /* DSP bitwidth enforce mode for ADM and AFE:
794 * includes:
795 * deep buffer, low latency, direct pcm and offload.
796 * excludes:
797 * ull(raw+fast), VOIP.
798 */
799 if ((flags & AUDIO_OUTPUT_FLAG_VOIP_RX) ||
800 ((flags & AUDIO_OUTPUT_FLAG_RAW) &&
801 (flags & AUDIO_OUTPUT_FLAG_FAST)))
802 return false;
803
804
805 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) ||
806 (flags & AUDIO_OUTPUT_FLAG_DIRECT) ||
807 (flags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) ||
808 (flags & AUDIO_OUTPUT_FLAG_PRIMARY))
809 return true;
810 else
811 return false;
812}
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800813
814static inline bool audio_is_vr_mode_on(struct audio_device *(__attribute__((unused)) adev))
815{
816 return adev->vr_audio_mode_enabled;
817}
818
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530819void audio_extn_utils_update_stream_app_type_cfg_for_usecase(
820 struct audio_device *adev,
821 struct audio_usecase *usecase)
822{
823 ALOGV("%s", __func__);
824
825 switch(usecase->type) {
826 case PCM_PLAYBACK:
827 audio_extn_utils_update_stream_output_app_type_cfg(adev->platform,
828 &adev->streams_output_cfg_list,
829 usecase->stream.out->devices,
830 usecase->stream.out->flags,
Aalique Grahame65780b52017-09-27 14:59:56 -0700831 usecase->stream.out->hal_op_format,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530832 usecase->stream.out->sample_rate,
833 usecase->stream.out->bit_width,
834 usecase->stream.out->channel_mask,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530835 usecase->stream.out->profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530836 &usecase->stream.out->app_type_cfg);
837 ALOGV("%s Selected apptype: %d", __func__, usecase->stream.out->app_type_cfg.app_type);
838 break;
839 case PCM_CAPTURE:
Vikram Panduranga93f080e2017-06-07 18:16:14 -0700840 if (usecase->id == USECASE_AUDIO_RECORD_VOIP)
841 usecase->stream.in->app_type_cfg.app_type = APP_TYPE_VOIP_AUDIO;
842 else
843 audio_extn_utils_update_stream_input_app_type_cfg(adev->platform,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530844 &adev->streams_input_cfg_list,
845 usecase->stream.in->device,
846 usecase->stream.in->flags,
847 usecase->stream.in->format,
848 usecase->stream.in->sample_rate,
849 usecase->stream.in->bit_width,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530850 usecase->stream.in->profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530851 &usecase->stream.in->app_type_cfg);
852 ALOGV("%s Selected apptype: %d", __func__, usecase->stream.in->app_type_cfg.app_type);
853 break;
Siddartha Shaik343abc62017-08-08 11:15:25 +0530854 case TRANSCODE_LOOPBACK :
855 audio_extn_utils_update_stream_output_app_type_cfg(adev->platform,
856 &adev->streams_output_cfg_list,
857 usecase->stream.inout->out_config.devices,
858 0,
859 usecase->stream.inout->out_config.format,
860 usecase->stream.inout->out_config.sample_rate,
861 usecase->stream.inout->out_config.bit_width,
862 usecase->stream.inout->out_config.channel_mask,
863 usecase->stream.inout->profile,
864 &usecase->stream.inout->out_app_type_cfg);
865 ALOGV("%s Selected apptype: %d", __func__, usecase->stream.inout->out_app_type_cfg.app_type);
866 break;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530867 default:
868 ALOGE("%s: app type cfg not supported for usecase type (%d)",
869 __func__, usecase->type);
870 }
871}
872
Siena Richard7c2db772016-12-21 11:32:34 -0800873static int send_app_type_cfg_for_device(struct audio_device *adev,
874 struct audio_usecase *usecase,
875 int split_snd_device)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700876{
877 char mixer_ctl_name[MAX_LENGTH_MIXER_CONTROL_IN_INT];
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530878 size_t app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT] = {0};
879 int len = 0, rc;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700880 struct mixer_ctl *ctl;
Siena Richard7c2db772016-12-21 11:32:34 -0800881 int pcm_device_id = 0, acdb_dev_id, app_type;
882 int snd_device = split_snd_device, snd_device_be_idx = -1;
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +0530883 int32_t sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Manish Dewangan837dc462015-05-27 10:17:41 +0530884 char value[PROPERTY_VALUE_MAX] = {0};
Varun Balaraje49253e2017-07-06 19:48:56 +0530885 struct streams_io_cfg *s_info = NULL;
886 struct listnode *node = NULL;
Nikhil Laturkarac4a7492017-08-31 18:27:19 +0530887 int bd_app_type = 0;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700888
Siena Richard7c2db772016-12-21 11:32:34 -0800889 ALOGV("%s: usecase->out_snd_device %s, usecase->in_snd_device %s, split_snd_device %s",
890 __func__, platform_get_snd_device_name(usecase->out_snd_device),
891 platform_get_snd_device_name(usecase->in_snd_device),
892 platform_get_snd_device_name(split_snd_device));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700893
Siddartha Shaik343abc62017-08-08 11:15:25 +0530894 if (usecase->type != PCM_PLAYBACK && usecase->type != PCM_CAPTURE &&
895 usecase->type != TRANSCODE_LOOPBACK) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530896 ALOGE("%s: not a playback/capture path, no need to cfg app type", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700897 rc = 0;
898 goto exit_send_app_type_cfg;
899 }
900 if ((usecase->id != USECASE_AUDIO_PLAYBACK_DEEP_BUFFER) &&
901 (usecase->id != USECASE_AUDIO_PLAYBACK_LOW_LATENCY) &&
902 (usecase->id != USECASE_AUDIO_PLAYBACK_MULTI_CH) &&
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800903 (usecase->id != USECASE_AUDIO_PLAYBACK_ULL) &&
Vikram Panduranga93f080e2017-06-07 18:16:14 -0700904 (usecase->id != USECASE_AUDIO_PLAYBACK_VOIP) &&
Siddartha Shaik343abc62017-08-08 11:15:25 +0530905 (usecase->id != USECASE_AUDIO_TRANSCODE_LOOPBACK) &&
Varun Balaraje49253e2017-07-06 19:48:56 +0530906 (!is_interactive_usecase(usecase->id)) &&
Srikanth Uyyala9d551402015-08-25 16:03:42 +0530907 (!is_offload_usecase(usecase->id)) &&
908 (usecase->type != PCM_CAPTURE)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530909 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 -0700910 rc = 0;
911 goto exit_send_app_type_cfg;
912 }
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800913
914 //if VR is active then only send the mixer control
915 if (usecase->id == USECASE_AUDIO_PLAYBACK_ULL && !audio_is_vr_mode_on(adev)) {
916 ALOGI("ULL doesnt need sending app type cfg, returning");
917 rc = 0;
918 goto exit_send_app_type_cfg;
919 }
920
Siddartha Shaik343abc62017-08-08 11:15:25 +0530921 if (usecase->type == PCM_PLAYBACK || usecase->type == TRANSCODE_LOOPBACK) {
Ben Romberger1fafdde2015-09-09 19:43:15 -0700922 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_PLAYBACK);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530923 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
924 "Audio Stream %d App Type Cfg", pcm_device_id);
Ben Romberger1fafdde2015-09-09 19:43:15 -0700925 } else if (usecase->type == PCM_CAPTURE) {
Ben Romberger1fafdde2015-09-09 19:43:15 -0700926 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_CAPTURE);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530927 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
928 "Audio Stream Capture %d App Type Cfg", pcm_device_id);
Srikanth Uyyala9d551402015-08-25 16:03:42 +0530929 }
Siena Richard7c2db772016-12-21 11:32:34 -0800930
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700931 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
932 if (!ctl) {
933 ALOGE("%s: Could not get ctl for mixer cmd - %s", __func__,
934 mixer_ctl_name);
935 rc = -EINVAL;
936 goto exit_send_app_type_cfg;
937 }
Aditya Bavanari701a6992017-03-30 19:17:16 +0530938 snd_device = platform_get_spkr_prot_snd_device(snd_device);
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +0530939
Rohit Kumar1181b292017-01-31 18:07:17 +0530940 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
941 if (acdb_dev_id <= 0) {
942 ALOGE("%s: Couldn't get the acdb dev id", __func__);
943 rc = -EINVAL;
944 goto exit_send_app_type_cfg;
945 }
946
Siena Richard7c2db772016-12-21 11:32:34 -0800947 snd_device_be_idx = platform_get_snd_device_backend_index(snd_device);
948 if (snd_device_be_idx < 0) {
949 ALOGE("%s: Couldn't get the backend index for snd device %s ret=%d",
950 __func__, platform_get_snd_device_name(snd_device),
951 snd_device_be_idx);
952 }
953
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530954 if ((usecase->type == PCM_PLAYBACK) && (usecase->stream.out != NULL)) {
Manish Dewangan837dc462015-05-27 10:17:41 +0530955
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -0700956 property_get("vendor.audio.playback.mch.downsample",value,"");
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530957 if (!strncmp("true", value, sizeof("true"))) {
958 if ((popcount(usecase->stream.out->channel_mask) > 2) &&
959 (usecase->stream.out->app_type_cfg.sample_rate > CODEC_BACKEND_DEFAULT_SAMPLE_RATE) &&
Aalique Grahame5cd12ff2017-10-19 10:57:00 -0700960 !(usecase->stream.out->flags &
961 (audio_output_flags_t)AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH))
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530962 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
963 }
Ashish Jainc02430d2016-01-04 10:42:43 +0530964
kunleizcdf25942017-09-20 14:01:21 +0800965 if (usecase->id == USECASE_AUDIO_PLAYBACK_VOIP) {
966 usecase->stream.out->app_type_cfg.sample_rate = usecase->stream.out->sample_rate;
967 } else if (usecase->stream.out->devices & AUDIO_DEVICE_OUT_SPEAKER) {
Mingming Yin21854652016-04-13 11:54:02 -0700968 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Ashish Jaina052e572016-11-07 16:41:07 +0530969 } else if ((snd_device == SND_DEVICE_OUT_HDMI ||
970 snd_device == SND_DEVICE_OUT_USB_HEADSET ||
971 snd_device == SND_DEVICE_OUT_DISPLAY_PORT) &&
972 (usecase->stream.out->sample_rate >= OUTPUT_SAMPLING_RATE_44100)) {
973 /*
974 * To best utlize DSP, check if the stream sample rate is supported/multiple of
975 * configured device sample rate, if not update the COPP rate to be equal to the
976 * device sample rate, else open COPP at stream sample rate
977 */
978 platform_check_and_update_copp_sample_rate(adev->platform, snd_device,
979 usecase->stream.out->sample_rate,
980 &usecase->stream.out->app_type_cfg.sample_rate);
Ashish Jain4826f6c2017-02-06 13:33:20 +0530981 } else if (((snd_device != SND_DEVICE_OUT_HEADPHONES_44_1 &&
982 !audio_is_this_native_usecase(usecase)) &&
Mingming Yin21854652016-04-13 11:54:02 -0700983 usecase->stream.out->sample_rate == OUTPUT_SAMPLING_RATE_44100) ||
984 (usecase->stream.out->sample_rate < OUTPUT_SAMPLING_RATE_44100)) {
Ashish Jain4826f6c2017-02-06 13:33:20 +0530985 /* Reset to default if no native stream is active*/
Mingming Yin21854652016-04-13 11:54:02 -0700986 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
987 }
988 sample_rate = usecase->stream.out->app_type_cfg.sample_rate;
989
Varun Balaraje49253e2017-07-06 19:48:56 +0530990 /* Interactive streams are supported with only direct app type id.
991 * Get Direct profile app type and use it for interactive streams
992 */
993 list_for_each(node, &adev->streams_output_cfg_list) {
994 s_info = node_to_item(node, struct streams_io_cfg, list);
Aalique Grahame5cd12ff2017-10-19 10:57:00 -0700995 if (s_info->flags.out_flags == (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_BD |
Nikhil Laturkarac4a7492017-08-31 18:27:19 +0530996 AUDIO_OUTPUT_FLAG_DIRECT_PCM |
997 AUDIO_OUTPUT_FLAG_DIRECT))
998 bd_app_type = s_info->app_type_cfg.app_type;
Varun Balaraje49253e2017-07-06 19:48:56 +0530999 }
Aalique Grahame5cd12ff2017-10-19 10:57:00 -07001000 if (usecase->stream.out->flags == (audio_output_flags_t)AUDIO_OUTPUT_FLAG_INTERACTIVE)
Nikhil Laturkarac4a7492017-08-31 18:27:19 +05301001 app_type = bd_app_type;
Varun Balaraje49253e2017-07-06 19:48:56 +05301002 else
1003 app_type = usecase->stream.out->app_type_cfg.app_type;
Siena Richard7c2db772016-12-21 11:32:34 -08001004 app_type_cfg[len++] = app_type;
Mingming Yin21854652016-04-13 11:54:02 -07001005 app_type_cfg[len++] = acdb_dev_id;
1006 if (((usecase->stream.out->format == AUDIO_FORMAT_E_AC3) ||
Ben Romberger1aaaf862017-04-06 17:49:46 -07001007 (usecase->stream.out->format == AUDIO_FORMAT_E_AC3_JOC) ||
1008 (usecase->stream.out->format == AUDIO_FORMAT_DOLBY_TRUEHD))
Harsh Bansal026d97f2017-08-17 17:44:49 +05301009 && audio_extn_passthru_is_passthrough_stream(usecase->stream.out)
1010 && !audio_extn_passthru_is_convert_supported(adev, usecase->stream.out)) {
Naresh Tanniru3a406772017-05-10 13:09:05 -07001011
1012 sample_rate = sample_rate * 4;
1013 if (sample_rate > HDMI_PASSTHROUGH_MAX_SAMPLE_RATE)
1014 sample_rate = HDMI_PASSTHROUGH_MAX_SAMPLE_RATE;
Mingming Yin21854652016-04-13 11:54:02 -07001015 }
Naresh Tanniru3a406772017-05-10 13:09:05 -07001016 app_type_cfg[len++] = sample_rate;
1017
Siena Richard7c2db772016-12-21 11:32:34 -08001018 if (snd_device_be_idx > 0)
1019 app_type_cfg[len++] = snd_device_be_idx;
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301020
Siena Richard7c2db772016-12-21 11:32:34 -08001021 ALOGI("%s PLAYBACK app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
1022 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301023
1024 } else if ((usecase->type == PCM_CAPTURE) && (usecase->stream.in != NULL)) {
Siena Richard7c2db772016-12-21 11:32:34 -08001025 app_type = usecase->stream.in->app_type_cfg.app_type;
1026 app_type_cfg[len++] = app_type;
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301027 app_type_cfg[len++] = acdb_dev_id;
kunleizcdf25942017-09-20 14:01:21 +08001028 if (usecase->id == USECASE_AUDIO_RECORD_VOIP)
1029 usecase->stream.in->app_type_cfg.sample_rate = usecase->stream.in->sample_rate;
Siena Richard7c2db772016-12-21 11:32:34 -08001030 sample_rate = usecase->stream.in->app_type_cfg.sample_rate;
1031 app_type_cfg[len++] = sample_rate;
1032 if (snd_device_be_idx > 0)
1033 app_type_cfg[len++] = snd_device_be_idx;
1034 ALOGI("%s CAPTURE app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
1035 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301036 } else {
Siena Richard7c2db772016-12-21 11:32:34 -08001037 app_type = platform_get_default_app_type_v2(adev->platform, usecase->type);
Siddartha Shaik343abc62017-08-08 11:15:25 +05301038 if(usecase->type == TRANSCODE_LOOPBACK) {
1039 sample_rate = usecase->stream.inout->out_config.sample_rate;
1040 app_type = usecase->stream.inout->out_app_type_cfg.app_type;
1041 }
Siena Richard7c2db772016-12-21 11:32:34 -08001042 app_type_cfg[len++] = app_type;
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301043 app_type_cfg[len++] = acdb_dev_id;
1044 app_type_cfg[len++] = sample_rate;
Siena Richard7c2db772016-12-21 11:32:34 -08001045 if (snd_device_be_idx > 0)
1046 app_type_cfg[len++] = snd_device_be_idx;
1047 ALOGI("%s default app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
1048 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Ashish Jainc02430d2016-01-04 10:42:43 +05301049 }
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301050
Siddartha Shaik343abc62017-08-08 11:15:25 +05301051 if(ctl)
1052 mixer_ctl_set_array(ctl, app_type_cfg, len);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001053 rc = 0;
1054exit_send_app_type_cfg:
1055 return rc;
1056}
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001057
Siena Richard7c2db772016-12-21 11:32:34 -08001058int audio_extn_utils_send_app_type_cfg(struct audio_device *adev,
1059 struct audio_usecase *usecase)
1060{
1061 int i, num_devices = 0;
1062 snd_device_t new_snd_devices[SND_DEVICE_OUT_END] = {0};
1063 int rc = 0;
1064
1065 switch (usecase->type) {
1066 case PCM_PLAYBACK:
Siddartha Shaik343abc62017-08-08 11:15:25 +05301067 case TRANSCODE_LOOPBACK:
Siena Richard7c2db772016-12-21 11:32:34 -08001068 ALOGD("%s: usecase->out_snd_device %s",
1069 __func__, platform_get_snd_device_name(usecase->out_snd_device));
1070 /* check for out combo device */
1071 if (platform_split_snd_device(adev->platform,
1072 usecase->out_snd_device,
1073 &num_devices, new_snd_devices)) {
1074 new_snd_devices[0] = usecase->out_snd_device;
1075 num_devices = 1;
1076 }
1077 break;
1078 case PCM_CAPTURE:
1079 ALOGD("%s: usecase->in_snd_device %s",
1080 __func__, platform_get_snd_device_name(usecase->in_snd_device));
1081 /* check for in combo device */
1082 if (platform_split_snd_device(adev->platform,
1083 usecase->in_snd_device,
1084 &num_devices, new_snd_devices)) {
1085 new_snd_devices[0] = usecase->in_snd_device;
1086 num_devices = 1;
1087 }
1088 break;
1089 default:
1090 ALOGI("%s: not a playback/capture path, no need to cfg app type", __func__);
1091 rc = 0;
1092 break;
1093 }
1094
1095 for (i = 0; i < num_devices; i++) {
1096 rc = send_app_type_cfg_for_device(adev, usecase, new_snd_devices[i]);
1097 if (rc)
1098 break;
1099 }
1100
1101 return rc;
1102}
1103
Preetam Singh Ranawat9519e9c2015-11-18 16:05:55 +05301104int read_line_from_file(const char *path, char *buf, size_t count)
1105{
1106 char * fgets_ret;
1107 FILE * fd;
1108 int rv;
1109
1110 fd = fopen(path, "r");
1111 if (fd == NULL)
1112 return -1;
1113
1114 fgets_ret = fgets(buf, (int)count, fd);
1115 if (NULL != fgets_ret) {
1116 rv = (int)strlen(buf);
1117 } else {
1118 rv = ferror(fd);
1119 }
1120 fclose(fd);
1121
1122 return rv;
1123}
1124
Ashish Jainf1eaa582016-05-23 20:54:24 +05301125/*Translates ALSA formats to AOSP PCM formats*/
1126audio_format_t alsa_format_to_hal(uint32_t alsa_format)
1127{
1128 audio_format_t format;
1129
1130 switch(alsa_format) {
1131 case SNDRV_PCM_FORMAT_S16_LE:
1132 format = AUDIO_FORMAT_PCM_16_BIT;
1133 break;
1134 case SNDRV_PCM_FORMAT_S24_3LE:
1135 format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
1136 break;
1137 case SNDRV_PCM_FORMAT_S24_LE:
1138 format = AUDIO_FORMAT_PCM_8_24_BIT;
1139 break;
1140 case SNDRV_PCM_FORMAT_S32_LE:
1141 format = AUDIO_FORMAT_PCM_32_BIT;
1142 break;
1143 default:
1144 ALOGW("Incorrect ALSA format");
1145 format = AUDIO_FORMAT_INVALID;
1146 }
1147 return format;
1148}
1149
1150/*Translates hal format (AOSP) to alsa formats*/
1151uint32_t hal_format_to_alsa(audio_format_t hal_format)
1152{
1153 uint32_t alsa_format;
1154
1155 switch (hal_format) {
1156 case AUDIO_FORMAT_PCM_32_BIT: {
1157 if (platform_supports_true_32bit())
1158 alsa_format = SNDRV_PCM_FORMAT_S32_LE;
1159 else
1160 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1161 }
1162 break;
1163 case AUDIO_FORMAT_PCM_8_BIT:
1164 alsa_format = SNDRV_PCM_FORMAT_S8;
1165 break;
1166 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
1167 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1168 break;
1169 case AUDIO_FORMAT_PCM_8_24_BIT: {
1170 if (platform_supports_true_32bit())
1171 alsa_format = SNDRV_PCM_FORMAT_S32_LE;
1172 else
1173 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1174 }
1175 break;
1176 case AUDIO_FORMAT_PCM_FLOAT:
1177 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1178 break;
1179 default:
1180 case AUDIO_FORMAT_PCM_16_BIT:
1181 alsa_format = SNDRV_PCM_FORMAT_S16_LE;
1182 break;
1183 }
1184 return alsa_format;
1185}
1186
Ashish Jain83a6cc22016-06-28 14:34:17 +05301187/*Translates PCM formats to AOSP formats*/
1188audio_format_t pcm_format_to_hal(uint32_t pcm_format)
1189{
1190 audio_format_t format = AUDIO_FORMAT_INVALID;
1191
1192 switch(pcm_format) {
1193 case PCM_FORMAT_S16_LE:
1194 format = AUDIO_FORMAT_PCM_16_BIT;
1195 break;
1196 case PCM_FORMAT_S24_3LE:
1197 format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
1198 break;
1199 case PCM_FORMAT_S24_LE:
1200 format = AUDIO_FORMAT_PCM_8_24_BIT;
1201 break;
1202 case PCM_FORMAT_S32_LE:
1203 format = AUDIO_FORMAT_PCM_32_BIT;
1204 break;
1205 default:
1206 ALOGW("Incorrect PCM format");
1207 format = AUDIO_FORMAT_INVALID;
1208 }
1209 return format;
1210}
1211
1212/*Translates hal format (AOSP) to alsa formats*/
1213uint32_t hal_format_to_pcm(audio_format_t hal_format)
1214{
1215 uint32_t pcm_format;
1216
1217 switch (hal_format) {
1218 case AUDIO_FORMAT_PCM_32_BIT:
1219 case AUDIO_FORMAT_PCM_8_24_BIT:
1220 case AUDIO_FORMAT_PCM_FLOAT: {
1221 if (platform_supports_true_32bit())
1222 pcm_format = PCM_FORMAT_S32_LE;
1223 else
1224 pcm_format = PCM_FORMAT_S24_3LE;
1225 }
1226 break;
1227 case AUDIO_FORMAT_PCM_8_BIT:
1228 pcm_format = PCM_FORMAT_S8;
1229 break;
1230 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
1231 pcm_format = PCM_FORMAT_S24_3LE;
1232 break;
1233 default:
1234 case AUDIO_FORMAT_PCM_16_BIT:
1235 pcm_format = PCM_FORMAT_S16_LE;
1236 break;
1237 }
1238 return pcm_format;
1239}
1240
Ashish Jainf1eaa582016-05-23 20:54:24 +05301241uint32_t get_alsa_fragment_size(uint32_t bytes_per_sample,
1242 uint32_t sample_rate,
1243 uint32_t noOfChannels)
1244{
1245 uint32_t fragment_size = 0;
1246 uint32_t pcm_offload_time = PCM_OFFLOAD_BUFFER_DURATION;
1247
1248 fragment_size = (pcm_offload_time
1249 * sample_rate
1250 * bytes_per_sample
1251 * noOfChannels)/1000;
1252 if (fragment_size < MIN_PCM_OFFLOAD_FRAGMENT_SIZE)
1253 fragment_size = MIN_PCM_OFFLOAD_FRAGMENT_SIZE;
1254 else if (fragment_size > MAX_PCM_OFFLOAD_FRAGMENT_SIZE)
1255 fragment_size = MAX_PCM_OFFLOAD_FRAGMENT_SIZE;
1256 /*To have same PCM samples for all channels, the buffer size requires to
1257 *be multiple of (number of channels * bytes per sample)
1258 *For writes to succeed, the buffer must be written at address which is multiple of 32
1259 */
Aalique Grahameb85f2d92017-10-16 17:53:23 -07001260 fragment_size = ALIGN(fragment_size, (bytes_per_sample * noOfChannels * 32));
Ashish Jainf1eaa582016-05-23 20:54:24 +05301261
1262 ALOGI("PCM offload Fragment size to %d bytes", fragment_size);
1263 return fragment_size;
1264}
1265
1266/* Calculates the fragment size required to configure compress session.
1267 * Based on the alsa format selected, decide if conversion is needed in
1268
1269 * HAL ( e.g. convert AUDIO_FORMAT_PCM_FLOAT input format to
1270 * AUDIO_FORMAT_PCM_24_BIT_PACKED before writing to the compress driver.
1271 */
1272void audio_extn_utils_update_direct_pcm_fragment_size(struct stream_out *out)
1273{
Ashish Jain83a6cc22016-06-28 14:34:17 +05301274 audio_format_t dst_format = out->hal_op_format;
1275 audio_format_t src_format = out->hal_ip_format;
Ashish Jainf1eaa582016-05-23 20:54:24 +05301276 uint32_t hal_op_bytes_per_sample = audio_bytes_per_sample(dst_format);
1277 uint32_t hal_ip_bytes_per_sample = audio_bytes_per_sample(src_format);
1278
1279 out->compr_config.fragment_size =
1280 get_alsa_fragment_size(hal_op_bytes_per_sample,
1281 out->sample_rate,
1282 popcount(out->channel_mask));
1283
1284 if ((src_format != dst_format) &&
1285 hal_op_bytes_per_sample != hal_ip_bytes_per_sample) {
1286
Ashish Jain83a6cc22016-06-28 14:34:17 +05301287 out->hal_fragment_size =
Ashish Jainf1eaa582016-05-23 20:54:24 +05301288 ((out->compr_config.fragment_size * hal_ip_bytes_per_sample) /
1289 hal_op_bytes_per_sample);
1290 ALOGI("enable conversion hal_input_fragment_size is %d src_format %x dst_format %x",
Ashish Jain83a6cc22016-06-28 14:34:17 +05301291 out->hal_fragment_size, src_format, dst_format);
Ashish Jainf1eaa582016-05-23 20:54:24 +05301292 } else {
Ashish Jain83a6cc22016-06-28 14:34:17 +05301293 out->hal_fragment_size = out->compr_config.fragment_size;
Ashish Jainf1eaa582016-05-23 20:54:24 +05301294 }
1295}
1296
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301297/* converts pcm format 24_8 to 8_24 inplace */
1298size_t audio_extn_utils_convert_format_24_8_to_8_24(void *buf, size_t bytes)
1299{
1300 size_t i = 0;
1301 int *int_buf_stream = buf;
1302
1303 if ((bytes % 4) != 0) {
1304 ALOGE("%s: wrong inout buffer! ... is not 32 bit aligned ", __func__);
1305 return -EINVAL;
1306 }
1307
1308 for (; i < (bytes / 4); i++)
1309 int_buf_stream[i] >>= 8;
1310
1311 return bytes;
1312}
1313
1314int get_snd_codec_id(audio_format_t format)
1315{
1316 int id = 0;
1317
1318 switch (format & AUDIO_FORMAT_MAIN_MASK) {
1319 case AUDIO_FORMAT_MP3:
1320 id = SND_AUDIOCODEC_MP3;
1321 break;
1322 case AUDIO_FORMAT_AAC:
1323 id = SND_AUDIOCODEC_AAC;
1324 break;
1325 case AUDIO_FORMAT_AAC_ADTS:
1326 id = SND_AUDIOCODEC_AAC;
1327 break;
Arun Kumar Dasari3b174182016-12-27 13:01:14 +05301328 case AUDIO_FORMAT_AAC_LATM:
1329 id = SND_AUDIOCODEC_AAC;
1330 break;
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301331 case AUDIO_FORMAT_PCM:
1332 id = SND_AUDIOCODEC_PCM;
1333 break;
1334 case AUDIO_FORMAT_FLAC:
1335 id = SND_AUDIOCODEC_FLAC;
1336 break;
1337 case AUDIO_FORMAT_ALAC:
1338 id = SND_AUDIOCODEC_ALAC;
1339 break;
1340 case AUDIO_FORMAT_APE:
1341 id = SND_AUDIOCODEC_APE;
1342 break;
1343 case AUDIO_FORMAT_VORBIS:
1344 id = SND_AUDIOCODEC_VORBIS;
1345 break;
1346 case AUDIO_FORMAT_WMA:
1347 id = SND_AUDIOCODEC_WMA;
1348 break;
1349 case AUDIO_FORMAT_WMA_PRO:
1350 id = SND_AUDIOCODEC_WMA_PRO;
1351 break;
Satish Babu Patakokila0c313922016-12-08 12:07:08 +05301352 case AUDIO_FORMAT_MP2:
1353 id = SND_AUDIOCODEC_MP2;
1354 break;
Satish Babu Patakokila915ecba2017-01-10 17:43:56 +05301355 case AUDIO_FORMAT_AC3:
1356 id = SND_AUDIOCODEC_AC3;
1357 break;
1358 case AUDIO_FORMAT_E_AC3:
1359 case AUDIO_FORMAT_E_AC3_JOC:
1360 id = SND_AUDIOCODEC_EAC3;
1361 break;
1362 case AUDIO_FORMAT_DTS:
1363 case AUDIO_FORMAT_DTS_HD:
1364 id = SND_AUDIOCODEC_DTS;
1365 break;
Ben Romberger1aaaf862017-04-06 17:49:46 -07001366 case AUDIO_FORMAT_DOLBY_TRUEHD:
1367 id = SND_AUDIOCODEC_TRUEHD;
1368 break;
Naresh Tanniru928f0862017-04-07 16:44:23 -07001369 case AUDIO_FORMAT_IEC61937:
1370 id = SND_AUDIOCODEC_IEC61937;
1371 break;
Preetam Singh Ranawat4277a5a2017-01-18 19:02:24 +05301372 case AUDIO_FORMAT_DSD:
1373 id = SND_AUDIOCODEC_DSD;
1374 break;
Dhanalakshmi Siddani18737932016-11-29 17:33:17 +05301375 case AUDIO_FORMAT_APTX:
1376 id = SND_AUDIOCODEC_APTX;
1377 break;
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301378 default:
1379 ALOGE("%s: Unsupported audio format :%x", __func__, format);
1380 }
1381
1382 return id;
1383}
1384
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001385void audio_extn_utils_send_audio_calibration(struct audio_device *adev,
1386 struct audio_usecase *usecase)
1387{
1388 int type = usecase->type;
1389
Dhananjay Kumar14245982017-01-16 20:21:00 +05301390 if (type == PCM_PLAYBACK && usecase->stream.out != NULL) {
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001391 struct stream_out *out = usecase->stream.out;
1392 int snd_device = usecase->out_snd_device;
1393 snd_device = (snd_device == SND_DEVICE_OUT_SPEAKER) ?
Xiaojun Sang040cc9f2015-08-03 19:38:28 +08001394 platform_get_spkr_prot_snd_device(snd_device) : snd_device;
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05301395 platform_send_audio_calibration(adev->platform, usecase,
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001396 out->app_type_cfg.app_type,
Preetam Singh Ranawat61716b12015-12-14 11:55:24 +05301397 usecase->stream.out->app_type_cfg.sample_rate);
Dhananjay Kumar14245982017-01-16 20:21:00 +05301398 } else if (type == PCM_CAPTURE && usecase->stream.in != NULL) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301399 platform_send_audio_calibration(adev->platform, usecase,
1400 usecase->stream.in->app_type_cfg.app_type,
1401 usecase->stream.in->app_type_cfg.sample_rate);
Vidyakumar Athota653aaef2017-03-16 11:11:31 -07001402 } else if (type == PCM_HFP_CALL || type == PCM_CAPTURE) {
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05301403 /* when app type is default. the sample rate is not used to send cal */
1404 platform_send_audio_calibration(adev->platform, usecase,
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301405 platform_get_default_app_type_v2(adev->platform, usecase->type),
1406 48000);
Siddartha Shaik343abc62017-08-08 11:15:25 +05301407 } else if (type == TRANSCODE_LOOPBACK && usecase->stream.inout != NULL) {
1408 int snd_device = usecase->out_snd_device;
1409 snd_device = (snd_device == SND_DEVICE_OUT_SPEAKER) ?
1410 platform_get_spkr_prot_snd_device(snd_device) : snd_device;
1411 platform_send_audio_calibration(adev->platform, usecase,
1412 platform_get_default_app_type_v2(adev->platform, usecase->type),
1413 usecase->stream.inout->out_config.sample_rate);
Vidyakumar Athotae57f6002017-03-01 13:13:16 -08001414 } else {
1415 /* No need to send audio calibration for voice and voip call usecases */
1416 if ((type != VOICE_CALL) && (type != VOIP_CALL))
1417 ALOGW("%s: No audio calibration for usecase type = %d", __func__, type);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001418 }
1419}
1420
Ben Rombergera04fabc2014-11-14 12:16:03 -08001421// Base64 Encode and Decode
1422// Not all features supported. This must be used only with following conditions.
1423// Decode Modes: Support with and without padding
1424// CRLF not handling. So no CRLF in string to decode.
1425// Encode Modes: Supports only padding
1426int b64decode(char *inp, int ilen, uint8_t* outp)
1427{
1428 int i, j, k, ii, num;
1429 int rem, pcnt;
1430 uint32_t res=0;
1431 uint8_t getIndex[MAX_BASEINDEX_LEN];
1432 uint8_t tmp, cflag;
1433
1434 if(inp == NULL || outp == NULL || ilen <= 0) {
1435 ALOGE("[%s] received NULL pointer or zero length",__func__);
1436 return -1;
1437 }
1438
1439 memset(getIndex, MAX_BASEINDEX_LEN-1, sizeof(getIndex));
1440 for(i=0;i<BASE_TABLE_SIZE;i++) {
1441 getIndex[(uint8_t)bTable[i]] = (uint8_t)i;
1442 }
1443 getIndex[(uint8_t)'=']=0;
1444
1445 j=0;k=0;
1446 num = ilen/4;
1447 rem = ilen%4;
1448 if(rem==0)
1449 num = num-1;
1450 cflag=0;
1451 for(i=0; i<num; i++) {
1452 res=0;
1453 for(ii=0;ii<4;ii++) {
1454 res = res << 6;
1455 tmp = getIndex[(uint8_t)inp[j++]];
1456 res = res | tmp;
1457 cflag = cflag | tmp;
1458 }
1459 outp[k++] = (res >> 16)&0xFF;
1460 outp[k++] = (res >> 8)&0xFF;
1461 outp[k++] = res & 0xFF;
1462 }
1463
1464 // Handle last bytes special
1465 pcnt=0;
1466 if(rem == 0) {
1467 //With padding or full data
1468 res = 0;
1469 for(ii=0;ii<4;ii++) {
1470 if(inp[j] == '=')
1471 pcnt++;
1472 res = res << 6;
1473 tmp = getIndex[(uint8_t)inp[j++]];
1474 res = res | tmp;
1475 cflag = cflag | tmp;
1476 }
1477 outp[k++] = res >> 16;
1478 if(pcnt == 2)
1479 goto done;
1480 outp[k++] = (res>>8)&0xFF;
1481 if(pcnt == 1)
1482 goto done;
1483 outp[k++] = res&0xFF;
1484 } else {
1485 //without padding
1486 res = 0;
1487 for(i=0;i<rem;i++) {
1488 res = res << 6;
1489 tmp = getIndex[(uint8_t)inp[j++]];
1490 res = res | tmp;
1491 cflag = cflag | tmp;
1492 }
1493 for(i=rem;i<4;i++) {
1494 res = res << 6;
1495 pcnt++;
1496 }
1497 outp[k++] = res >> 16;
1498 if(pcnt == 2)
1499 goto done;
1500 outp[k++] = (res>>8)&0xFF;
1501 if(pcnt == 1)
1502 goto done;
1503 outp[k++] = res&0xFF;
1504 }
1505done:
1506 if(cflag == 0xFF) {
1507 ALOGE("[%s] base64 decode failed. Invalid character found %s",
1508 __func__, inp);
1509 return 0;
1510 }
1511 return k;
1512}
1513
1514int b64encode(uint8_t *inp, int ilen, char* outp)
1515{
1516 int i,j,k, num;
1517 int rem=0;
1518 uint32_t res=0;
1519
1520 if(inp == NULL || outp == NULL || ilen<=0) {
1521 ALOGE("[%s] received NULL pointer or zero input length",__func__);
1522 return -1;
1523 }
1524
1525 num = ilen/3;
1526 rem = ilen%3;
1527 j=0;k=0;
1528 for(i=0; i<num; i++) {
1529 //prepare index
1530 res = inp[j++]<<16;
1531 res = res | inp[j++]<<8;
1532 res = res | inp[j++];
1533 //get output map from index
1534 outp[k++] = (char) bTable[(res>>18)&0x3F];
1535 outp[k++] = (char) bTable[(res>>12)&0x3F];
1536 outp[k++] = (char) bTable[(res>>6)&0x3F];
1537 outp[k++] = (char) bTable[res&0x3F];
1538 }
1539
1540 switch(rem) {
1541 case 1:
1542 res = inp[j++]<<16;
1543 outp[k++] = (char) bTable[res>>18];
1544 outp[k++] = (char) bTable[(res>>12)&0x3F];
1545 //outp[k++] = '=';
1546 //outp[k++] = '=';
1547 break;
1548 case 2:
1549 res = inp[j++]<<16;
1550 res = res | inp[j++]<<8;
1551 outp[k++] = (char) bTable[res>>18];
1552 outp[k++] = (char) bTable[(res>>12)&0x3F];
1553 outp[k++] = (char) bTable[(res>>6)&0x3F];
1554 //outp[k++] = '=';
1555 break;
1556 default:
1557 break;
1558 }
Ben Rombergera04fabc2014-11-14 12:16:03 -08001559 outp[k] = '\0';
1560 return k;
1561}
Ashish Jain81eb2a82015-05-13 10:52:34 +05301562
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301563
1564int audio_extn_utils_get_codec_version(const char *snd_card_name,
1565 int card_num,
1566 char *codec_version)
1567{
1568 char procfs_path[50];
1569 FILE *fp;
1570
1571 if (strstr(snd_card_name, "tasha")) {
1572 snprintf(procfs_path, sizeof(procfs_path),
1573 "/proc/asound/card%d/codecs/tasha/version", card_num);
1574 if ((fp = fopen(procfs_path, "r")) != NULL) {
1575 fgets(codec_version, CODEC_VERSION_MAX_LENGTH, fp);
1576 fclose(fp);
1577 } else {
1578 ALOGE("%s: ERROR. cannot open %s", __func__, procfs_path);
1579 return -ENOENT;
1580 }
1581 ALOGD("%s: codec version %s", __func__, codec_version);
1582 }
1583
1584 return 0;
1585}
1586
1587
Ashish Jain81eb2a82015-05-13 10:52:34 +05301588#ifdef AUDIO_EXTERNAL_HDMI_ENABLED
1589
1590void get_default_compressed_channel_status(
1591 unsigned char *channel_status)
1592{
Ashish Jain81eb2a82015-05-13 10:52:34 +05301593 memset(channel_status,0,24);
1594
1595 /* block start bit in preamble bit 3 */
1596 channel_status[0] |= PROFESSIONAL;
1597 //compre out
1598 channel_status[0] |= NON_LPCM;
1599 // sample rate; fixed 48K for default/transcode
1600 channel_status[3] |= SR_48000;
1601}
1602
1603#ifdef HDMI_PASSTHROUGH_ENABLED
1604int32_t get_compressed_channel_status(void *audio_stream_data,
1605 uint32_t audio_frame_size,
1606 unsigned char *channel_status,
1607 enum audio_parser_code_type codec_type)
1608 // codec_type - AUDIO_PARSER_CODEC_AC3
1609 // - AUDIO_PARSER_CODEC_DTS
1610{
1611 unsigned char *stream;
1612 int ret = 0;
1613 stream = (unsigned char *)audio_stream_data;
1614
1615 if (audio_stream_data == NULL || audio_frame_size == 0) {
1616 ALOGW("no buffer to get channel status, return default for compress");
1617 get_default_compressed_channel_status(channel_status);
1618 return ret;
1619 }
1620
1621 memset(channel_status,0,24);
1622 if(init_audio_parser(stream, audio_frame_size, codec_type) == -1)
1623 {
1624 ALOGE("init audio parser failed");
1625 return -1;
1626 }
1627 ret = get_channel_status(channel_status, codec_type);
1628 return ret;
1629
1630}
1631
1632#endif
1633
1634void get_lpcm_channel_status(uint32_t sampleRate,
1635 unsigned char *channel_status)
1636{
1637 int32_t status = 0;
Ashish Jain81eb2a82015-05-13 10:52:34 +05301638 memset(channel_status,0,24);
1639 /* block start bit in preamble bit 3 */
1640 channel_status[0] |= PROFESSIONAL;
1641 //LPCM OUT
1642 channel_status[0] &= ~NON_LPCM;
1643
1644 switch (sampleRate) {
1645 case 8000:
1646 case 11025:
1647 case 12000:
1648 case 16000:
1649 case 22050:
1650 channel_status[3] |= SR_NOTID;
Preetam Singh Ranawat61716b12015-12-14 11:55:24 +05301651 break;
Ashish Jain81eb2a82015-05-13 10:52:34 +05301652 case 24000:
1653 channel_status[3] |= SR_24000;
1654 break;
1655 case 32000:
1656 channel_status[3] |= SR_32000;
1657 break;
1658 case 44100:
1659 channel_status[3] |= SR_44100;
1660 break;
1661 case 48000:
1662 channel_status[3] |= SR_48000;
1663 break;
1664 case 88200:
1665 channel_status[3] |= SR_88200;
1666 break;
1667 case 96000:
1668 channel_status[3] |= SR_96000;
1669 break;
1670 case 176400:
1671 channel_status[3] |= SR_176400;
1672 break;
1673 case 192000:
1674 channel_status[3] |= SR_192000;
1675 break;
1676 default:
1677 ALOGV("Invalid sample_rate %u\n", sampleRate);
1678 status = -1;
1679 break;
1680 }
1681}
1682
1683void audio_utils_set_hdmi_channel_status(struct stream_out *out, char * buffer, size_t bytes)
1684{
1685 unsigned char channel_status[24]={0};
1686 struct snd_aes_iec958 iec958;
1687 const char *mixer_ctl_name = "IEC958 Playback PCM Stream";
1688 struct mixer_ctl *ctl;
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +05301689 ALOGV("%s: buffer %s bytes %zd", __func__, buffer, bytes);
Ashish Jain81eb2a82015-05-13 10:52:34 +05301690#ifdef HDMI_PASSTHROUGH_ENABLED
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05301691 if (audio_extn_utils_is_dolby_format(out->format) &&
Ashish Jain81eb2a82015-05-13 10:52:34 +05301692 /*TODO:Extend code to support DTS passthrough*/
1693 /*set compressed channel status bits*/
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05301694 audio_extn_passthru_is_passthrough_stream(out)){
Ashish Jain81eb2a82015-05-13 10:52:34 +05301695 get_compressed_channel_status(buffer, bytes, channel_status, AUDIO_PARSER_CODEC_AC3);
1696 } else
1697#endif
1698 {
1699 /*set channel status bit for LPCM*/
1700 get_lpcm_channel_status(out->sample_rate, channel_status);
1701 }
1702
1703 memcpy(iec958.status, channel_status,sizeof(iec958.status));
1704 ctl = mixer_get_ctl_by_name(out->dev->mixer, mixer_ctl_name);
1705 if (!ctl) {
1706 ALOGE("%s: Could not get ctl for mixer cmd - %s",
1707 __func__, mixer_ctl_name);
1708 return;
1709 }
1710 if (mixer_ctl_set_array(ctl, &iec958, sizeof(iec958)) < 0) {
1711 ALOGE("%s: Could not set channel status for ext HDMI ",
1712 __func__);
1713 return;
1714 }
1715
1716}
1717#endif
Manish Dewangan3ccdea52017-02-13 19:31:54 +05301718
1719int audio_extn_utils_get_avt_device_drift(
1720 struct audio_usecase *usecase,
1721 struct audio_avt_device_drift_param *drift_param)
1722{
1723 int ret = 0, count = 0;
1724 char avt_device_drift_mixer_ctl_name[MIXER_PATH_MAX_LENGTH] = {0};
Naresh Tanniru6160c712017-04-17 15:43:48 +05301725 const char *backend = NULL;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05301726 struct mixer_ctl *ctl = NULL;
1727 struct audio_avt_device_drift_stats drift_stats;
1728 struct audio_device *adev = NULL;
1729
1730 if (usecase != NULL && usecase->type == PCM_PLAYBACK) {
Naresh Tanniru6160c712017-04-17 15:43:48 +05301731 backend = platform_get_snd_device_backend_interface(usecase->out_snd_device);
1732 if (!backend) {
1733 ALOGE("%s: Unsupported device %d", __func__,
1734 usecase->stream.out->devices);
1735 ret = -EINVAL;
1736 goto done;
1737 }
1738 strlcpy(avt_device_drift_mixer_ctl_name,
1739 backend,
1740 MIXER_PATH_MAX_LENGTH);
1741
1742 count = strlen(backend);
1743 if (MIXER_PATH_MAX_LENGTH - count > 0) {
1744 strlcat(&avt_device_drift_mixer_ctl_name[count],
1745 " DRIFT",
1746 MIXER_PATH_MAX_LENGTH - count);
1747 } else {
1748 ret = -EINVAL;
1749 goto done;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05301750 }
1751 } else {
Naresh Tanniru7586e292017-04-13 10:38:13 +05301752 ALOGE("%s: Invalid usecase",__func__);
Manish Dewangan3ccdea52017-02-13 19:31:54 +05301753 ret = -EINVAL;
Naresh Tanniru6160c712017-04-17 15:43:48 +05301754 goto done;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05301755 }
1756
Naresh Tanniru6160c712017-04-17 15:43:48 +05301757 adev = usecase->stream.out->dev;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05301758 ctl = mixer_get_ctl_by_name(adev->mixer, avt_device_drift_mixer_ctl_name);
1759 if (!ctl) {
1760 ALOGE("%s: Could not get ctl for mixer cmd - %s",
1761 __func__, avt_device_drift_mixer_ctl_name);
1762
1763 ret = -EINVAL;
1764 goto done;
1765 }
1766
1767 ALOGV("%s: Getting AV Timer vs Device Drift mixer ctrl name %s", __func__,
1768 avt_device_drift_mixer_ctl_name);
1769
1770 mixer_ctl_update(ctl);
1771 count = mixer_ctl_get_num_values(ctl);
1772 if (count != sizeof(struct audio_avt_device_drift_stats)) {
1773 ALOGE("%s: mixer_ctl_get_num_values() invalid drift_stats data size",
1774 __func__);
1775
1776 ret = -EINVAL;
1777 goto done;
1778 }
1779
1780 ret = mixer_ctl_get_array(ctl, (void *)&drift_stats, count);
1781 if (ret != 0) {
1782 ALOGE("%s: mixer_ctl_get_array() failed to get drift_stats Params",
1783 __func__);
1784
1785 ret = -EINVAL;
1786 goto done;
1787 }
1788 memcpy(drift_param, &drift_stats.drift_param,
1789 sizeof(struct audio_avt_device_drift_param));
1790done:
1791 return ret;
1792}
Manish Dewangan07de2142017-02-27 19:27:20 +05301793
1794#ifdef SNDRV_COMPRESS_PATH_DELAY
1795int audio_extn_utils_compress_get_dsp_latency(struct stream_out *out)
1796{
1797 int ret = -EINVAL;
1798 struct snd_compr_metadata metadata;
1799 int delay_ms = COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
1800
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -07001801 if (property_get_bool("vendor.audio.playback.dsp.pathdelay", false)) {
Manish Dewangan07de2142017-02-27 19:27:20 +05301802 ALOGD("%s:: Quering DSP delay %d",__func__, __LINE__);
1803 if (!(is_offload_usecase(out->usecase))) {
1804 ALOGE("%s:: not supported for non offload session", __func__);
1805 goto exit;
1806 }
1807
1808 if (!out->compr) {
1809 ALOGD("%s:: Invalid compress handle,returning default dsp latency",
1810 __func__);
1811 goto exit;
1812 }
1813
1814 metadata.key = SNDRV_COMPRESS_PATH_DELAY;
1815 ret = compress_get_metadata(out->compr, &metadata);
1816 if(ret) {
1817 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
1818 goto exit;
1819 }
1820 delay_ms = metadata.value[0] / 1000; /*convert to ms*/
1821 } else {
1822 ALOGD("%s:: Using Fix DSP delay",__func__);
1823 }
1824
1825exit:
1826 ALOGD("%s:: delay in ms is %d",__func__, delay_ms);
1827 return delay_ms;
1828}
1829#else
1830int audio_extn_utils_compress_get_dsp_latency(struct stream_out *out __unused)
1831{
1832 return COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
1833}
1834#endif
Manish Dewangan69426c82017-01-30 17:35:36 +05301835
1836#ifdef SNDRV_COMPRESS_RENDER_MODE
1837int audio_extn_utils_compress_set_render_mode(struct stream_out *out)
1838{
1839 struct snd_compr_metadata metadata;
1840 int ret = -EINVAL;
1841
1842 if (!(is_offload_usecase(out->usecase))) {
1843 ALOGE("%s:: not supported for non offload session", __func__);
1844 goto exit;
1845 }
1846
1847 if (!out->compr) {
1848 ALOGD("%s:: Invalid compress handle",
1849 __func__);
1850 goto exit;
1851 }
1852
1853 ALOGD("%s:: render mode %d", __func__, out->render_mode);
1854
1855 metadata.key = SNDRV_COMPRESS_RENDER_MODE;
1856 if (out->render_mode == RENDER_MODE_AUDIO_MASTER) {
1857 metadata.value[0] = SNDRV_COMPRESS_RENDER_MODE_AUDIO_MASTER;
1858 } else if (out->render_mode == RENDER_MODE_AUDIO_STC_MASTER) {
1859 metadata.value[0] = SNDRV_COMPRESS_RENDER_MODE_STC_MASTER;
1860 } else {
1861 ret = 0;
1862 goto exit;
1863 }
1864 ret = compress_set_metadata(out->compr, &metadata);
1865 if(ret) {
1866 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
1867 }
1868exit:
1869 return ret;
1870}
1871#else
1872int audio_extn_utils_compress_set_render_mode(struct stream_out *out __unused)
1873{
1874 ALOGD("%s:: configuring render mode not supported", __func__);
1875 return 0;
1876}
1877#endif
Manish Dewangan58229382017-02-02 15:48:41 +05301878
1879#ifdef SNDRV_COMPRESS_CLK_REC_MODE
1880int audio_extn_utils_compress_set_clk_rec_mode(
1881 struct audio_usecase *usecase)
1882{
1883 struct snd_compr_metadata metadata;
1884 struct stream_out *out = NULL;
1885 int ret = -EINVAL;
1886
Naresh Tanniru7586e292017-04-13 10:38:13 +05301887 if (usecase == NULL || usecase->type != PCM_PLAYBACK) {
Manish Dewangan58229382017-02-02 15:48:41 +05301888 ALOGE("%s:: Invalid use case", __func__);
1889 goto exit;
1890 }
1891
1892 out = usecase->stream.out;
1893 if (!out) {
1894 ALOGE("%s:: invalid stream", __func__);
1895 goto exit;
1896 }
1897
1898 if (!is_offload_usecase(out->usecase)) {
1899 ALOGE("%s:: not supported for non offload session", __func__);
1900 goto exit;
1901 }
1902
1903 if (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER) {
1904 ALOGD("%s:: clk recovery is only supported in STC render mode",
1905 __func__);
1906 ret = 0;
1907 goto exit;
1908 }
1909
1910 if (!out->compr) {
1911 ALOGD("%s:: Invalid compress handle",
1912 __func__);
1913 goto exit;
1914 }
1915 metadata.key = SNDRV_COMPRESS_CLK_REC_MODE;
1916 switch(usecase->out_snd_device) {
1917 case SND_DEVICE_OUT_HDMI:
1918 case SND_DEVICE_OUT_SPEAKER_AND_HDMI:
1919 case SND_DEVICE_OUT_DISPLAY_PORT:
1920 case SND_DEVICE_OUT_SPEAKER_AND_DISPLAY_PORT:
1921 metadata.value[0] = SNDRV_COMPRESS_CLK_REC_MODE_NONE;
1922 break;
1923 default:
1924 metadata.value[0] = SNDRV_COMPRESS_CLK_REC_MODE_AUTO;
1925 break;
1926 }
1927
1928 ALOGD("%s:: clk recovery mode %d",__func__, metadata.value[0]);
1929
1930 ret = compress_set_metadata(out->compr, &metadata);
1931 if(ret) {
1932 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
1933 }
1934
1935exit:
1936 return ret;
1937}
1938#else
1939int audio_extn_utils_compress_set_clk_rec_mode(
1940 struct audio_usecase *usecase __unused)
1941{
1942 ALOGD("%s:: configuring render mode not supported", __func__);
1943 return 0;
1944}
1945#endif
Manish Dewangan27346042017-03-01 12:56:12 +05301946
1947#ifdef SNDRV_COMPRESS_RENDER_WINDOW
1948int audio_extn_utils_compress_set_render_window(
1949 struct stream_out *out,
1950 struct audio_out_render_window_param *render_window)
1951{
1952 struct snd_compr_metadata metadata;
1953 int ret = -EINVAL;
1954
Manish Dewangan27346042017-03-01 12:56:12 +05301955 if(render_window == NULL) {
1956 ALOGE("%s:: Invalid render_window", __func__);
1957 goto exit;
1958 }
1959
Aniket Kumar Lata1e1d3662017-06-01 18:45:48 -07001960 ALOGD("%s:: render window start 0x%"PRIx64" end 0x%"PRIx64"",
1961 __func__,render_window->render_ws, render_window->render_we);
1962
Manish Dewangan27346042017-03-01 12:56:12 +05301963 if (!is_offload_usecase(out->usecase)) {
1964 ALOGE("%s:: not supported for non offload session", __func__);
1965 goto exit;
1966 }
1967
Naresh Tanniru6160c712017-04-17 15:43:48 +05301968 if ((out->render_mode != RENDER_MODE_AUDIO_MASTER) &&
1969 (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER)) {
Manish Dewangan27346042017-03-01 12:56:12 +05301970 ALOGD("%s:: only supported in timestamp mode, current "
1971 "render mode mode %d", __func__, out->render_mode);
1972 goto exit;
1973 }
1974
1975 if (!out->compr) {
1976 ALOGW("%s:: offload session not yet opened,"
1977 "render window will be configure later", __func__);
1978 /* store render window to reconfigure in start_output_stream() */
1979 goto exit;
1980 }
1981
1982 metadata.key = SNDRV_COMPRESS_RENDER_WINDOW;
1983 /*render window start value */
1984 metadata.value[0] = 0xFFFFFFFF & render_window->render_ws; /* lsb */
1985 metadata.value[1] = \
1986 (0xFFFFFFFF00000000 & render_window->render_ws) >> 32; /* msb*/
1987 /*render window end value */
1988 metadata.value[2] = 0xFFFFFFFF & render_window->render_we; /* lsb */
1989 metadata.value[3] = \
1990 (0xFFFFFFFF00000000 & render_window->render_we) >> 32; /* msb*/
1991
1992 ret = compress_set_metadata(out->compr, &metadata);
1993 if(ret) {
1994 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
1995 }
1996
1997exit:
1998 return ret;
1999}
2000#else
2001int audio_extn_utils_compress_set_render_window(
2002 struct stream_out *out __unused,
2003 struct audio_out_render_window_param *render_window __unused)
2004{
2005 ALOGD("%s:: configuring render window not supported", __func__);
2006 return 0;
2007}
2008#endif
Manish Dewangan14956cc2017-02-14 18:54:42 +05302009
2010#ifdef SNDRV_COMPRESS_START_DELAY
2011int audio_extn_utils_compress_set_start_delay(
2012 struct stream_out *out,
2013 struct audio_out_start_delay_param *delay_param)
2014{
2015 struct snd_compr_metadata metadata;
2016 int ret = -EINVAL;
2017
2018 if(delay_param == NULL) {
2019 ALOGE("%s:: Invalid delay_param", __func__);
2020 goto exit;
2021 }
2022
2023 ALOGD("%s:: render start delay 0x%"PRIx64" ", __func__,
2024 delay_param->start_delay);
2025
2026 if (!is_offload_usecase(out->usecase)) {
2027 ALOGE("%s:: not supported for non offload session", __func__);
2028 goto exit;
2029 }
2030
Naresh Tanniru6160c712017-04-17 15:43:48 +05302031 if ((out->render_mode != RENDER_MODE_AUDIO_MASTER) &&
2032 (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER)) {
Manish Dewangan14956cc2017-02-14 18:54:42 +05302033 ALOGD("%s:: only supported in timestamp mode, current "
2034 "render mode mode %d", __func__, out->render_mode);
2035 goto exit;
2036 }
2037
2038 if (!out->compr) {
2039 ALOGW("%s:: offload session not yet opened,"
2040 "start delay will be configure later", __func__);
2041 goto exit;
2042 }
2043
2044 metadata.key = SNDRV_COMPRESS_START_DELAY;
2045 metadata.value[0] = 0xFFFFFFFF & delay_param->start_delay; /* lsb */
2046 metadata.value[1] = \
2047 (0xFFFFFFFF00000000 & delay_param->start_delay) >> 32; /* msb*/
2048
2049 ret = compress_set_metadata(out->compr, &metadata);
2050 if(ret) {
2051 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2052 }
2053
2054exit:
2055 return ret;
2056}
2057#else
2058int audio_extn_utils_compress_set_start_delay(
2059 struct stream_out *out __unused,
2060 struct audio_out_start_delay_param *delay_param __unused)
2061{
2062 ALOGD("%s:: configuring render window not supported", __func__);
2063 return 0;
2064}
2065#endif
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002066
2067#define MAX_SND_CARD 8
2068#define RETRY_US 500000
2069#define RETRY_NUMBER 10
2070
2071int audio_extn_utils_get_snd_card_num()
2072{
2073
2074 void *hw_info = NULL;
2075 struct mixer *mixer = NULL;
2076 int retry_num = 0;
2077 int snd_card_num = 0;
2078 char* snd_card_name = NULL;
2079
2080 while (snd_card_num < MAX_SND_CARD) {
2081 mixer = mixer_open(snd_card_num);
2082
2083 while (!mixer && retry_num < RETRY_NUMBER) {
2084 usleep(RETRY_US);
2085 mixer = mixer_open(snd_card_num);
2086 retry_num++;
2087 }
2088
2089 if (!mixer) {
2090 ALOGE("%s: Unable to open the mixer card: %d", __func__,
2091 snd_card_num);
2092 retry_num = 0;
2093 snd_card_num++;
2094 continue;
2095 }
2096
2097 snd_card_name = strdup(mixer_get_name(mixer));
2098 if (!snd_card_name) {
2099 ALOGE("failed to allocate memory for snd_card_name\n");
2100 mixer_close(mixer);
2101 return -1;
2102 }
2103 ALOGD("%s: snd_card_name: %s", __func__, snd_card_name);
2104
2105 hw_info = hw_info_init(snd_card_name);
2106 if (hw_info) {
2107 ALOGD("%s: Opened sound card:%d", __func__, snd_card_num);
2108 break;
2109 }
2110 ALOGE("%s: Failed to init hardware info", __func__);
2111 retry_num = 0;
2112 snd_card_num++;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002113
Dhananjay Kumara7e27832017-07-11 15:40:39 +05302114 free(snd_card_name);
2115 snd_card_name = NULL;
2116
2117 mixer_close(mixer);
2118 mixer = NULL;
2119 }
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002120 if (snd_card_name)
2121 free(snd_card_name);
Dhananjay Kumara7e27832017-07-11 15:40:39 +05302122 if (mixer)
2123 mixer_close(mixer);
2124 if (hw_info)
2125 hw_info_deinit(hw_info);
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002126
2127 if (snd_card_num >= MAX_SND_CARD) {
2128 ALOGE("%s: Unable to find correct sound card, aborting.", __func__);
2129 return -1;
2130 }
2131
2132 return snd_card_num;
2133}
Naresh Tanniru6160c712017-04-17 15:43:48 +05302134
2135#ifdef SNDRV_COMPRESS_ENABLE_ADJUST_SESSION_CLOCK
2136int audio_extn_utils_compress_enable_drift_correction(
2137 struct stream_out *out,
2138 struct audio_out_enable_drift_correction *drift)
2139{
2140 struct snd_compr_metadata metadata;
2141 int ret = -EINVAL;
2142
2143 if(drift == NULL) {
2144 ALOGE("%s:: Invalid param", __func__);
2145 goto exit;
2146 }
2147
2148 ALOGD("%s:: drift enable %d", __func__,drift->enable);
2149
2150 if (!is_offload_usecase(out->usecase)) {
2151 ALOGE("%s:: not supported for non offload session", __func__);
2152 goto exit;
2153 }
2154
2155 if (!out->compr) {
2156 ALOGW("%s:: offload session not yet opened,"
2157 "start delay will be configure later", __func__);
2158 goto exit;
2159 }
2160
2161 metadata.key = SNDRV_COMPRESS_ENABLE_ADJUST_SESSION_CLOCK;
2162 metadata.value[0] = drift->enable;
2163 out->drift_correction_enabled = drift->enable;
2164
2165 ret = compress_set_metadata(out->compr, &metadata);
2166 if(ret) {
2167 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2168 out->drift_correction_enabled = false;
2169 }
2170
2171exit:
2172 return ret;
2173}
2174#else
2175int audio_extn_utils_compress_enable_drift_correction(
2176 struct stream_out *out __unused,
2177 struct audio_out_enable_drift_correction *drift __unused)
2178{
2179 ALOGD("%s:: configuring drift enablement not supported", __func__);
2180 return 0;
2181}
2182#endif
2183
2184#ifdef SNDRV_COMPRESS_ADJUST_SESSION_CLOCK
2185int audio_extn_utils_compress_correct_drift(
2186 struct stream_out *out,
2187 struct audio_out_correct_drift *drift_param)
2188{
2189 struct snd_compr_metadata metadata;
2190 int ret = -EINVAL;
2191
2192 if (drift_param == NULL) {
2193 ALOGE("%s:: Invalid drift_param", __func__);
2194 goto exit;
2195 }
2196
2197 ALOGD("%s:: adjust time 0x%"PRIx64" ", __func__,
2198 drift_param->adjust_time);
2199
2200 if (!is_offload_usecase(out->usecase)) {
2201 ALOGE("%s:: not supported for non offload session", __func__);
2202 goto exit;
2203 }
2204
2205 if (!out->compr) {
2206 ALOGW("%s:: offload session not yet opened", __func__);
2207 goto exit;
2208 }
2209
2210 if (!out->drift_correction_enabled) {
2211 ALOGE("%s:: drift correction not enabled", __func__);
2212 goto exit;
2213 }
2214
2215 metadata.key = SNDRV_COMPRESS_ADJUST_SESSION_CLOCK;
2216 metadata.value[0] = 0xFFFFFFFF & drift_param->adjust_time; /* lsb */
2217 metadata.value[1] = \
2218 (0xFFFFFFFF00000000 & drift_param->adjust_time) >> 32; /* msb*/
2219
2220 ret = compress_set_metadata(out->compr, &metadata);
2221 if(ret)
2222 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2223exit:
2224 return ret;
2225}
2226#else
2227int audio_extn_utils_compress_correct_drift(
2228 struct stream_out *out __unused,
2229 struct audio_out_correct_drift *drift_param __unused)
2230{
2231 ALOGD("%s:: setting adjust clock not supported", __func__);
2232 return 0;
2233}
2234#endif
Naresh Tanniru29bce4e2017-04-27 17:54:30 +05302235
2236int audio_extn_utils_set_channel_map(
2237 struct stream_out *out,
2238 struct audio_out_channel_map_param *channel_map_param)
2239{
2240 int ret = -EINVAL, i = 0;
2241 int channels = audio_channel_count_from_out_mask(out->channel_mask);
2242
2243 if (channel_map_param == NULL) {
2244 ALOGE("%s:: Invalid channel_map", __func__);
2245 goto exit;
2246 }
2247
2248 if (channel_map_param->channels != channels) {
2249 ALOGE("%s:: Channels(%d) does not match stream channels(%d)",
2250 __func__, channel_map_param->channels, channels);
2251 goto exit;
2252 }
2253
2254 for ( i = 0; i < channels; i++) {
2255 ALOGV("%s:: channel_map[%d]- %d", __func__, i, channel_map_param->channel_map[i]);
2256 out->channel_map_param.channel_map[i] = channel_map_param->channel_map[i];
2257 }
2258 ret = 0;
2259exit:
2260 return ret;
2261}
Varun Balaraje49253e2017-07-06 19:48:56 +05302262
2263int audio_extn_utils_set_pan_scale_params(
2264 struct stream_out *out,
2265 struct mix_matrix_params *mm_params)
2266{
2267 int ret = -EINVAL, i = 0, j = 0;
2268
Varun Balaraj003ee752017-08-07 17:54:55 +05302269 if (mm_params == NULL || out == NULL) {
2270 ALOGE("%s:: Invalid mix matrix or out param", __func__);
Varun Balaraje49253e2017-07-06 19:48:56 +05302271 goto exit;
2272 }
2273
2274 if (mm_params->num_output_channels > MAX_CHANNELS_SUPPORTED ||
2275 mm_params->num_output_channels <= 0 ||
2276 mm_params->num_input_channels > MAX_CHANNELS_SUPPORTED ||
2277 mm_params->num_input_channels <= 0)
2278 goto exit;
2279
2280 out->pan_scale_params.num_output_channels = mm_params->num_output_channels;
2281 out->pan_scale_params.num_input_channels = mm_params->num_input_channels;
2282 out->pan_scale_params.has_output_channel_map =
2283 mm_params->has_output_channel_map;
2284 for (i = 0; i < mm_params->num_output_channels; i++)
2285 out->pan_scale_params.output_channel_map[i] =
2286 mm_params->output_channel_map[i];
2287
2288 out->pan_scale_params.has_input_channel_map =
2289 mm_params->has_input_channel_map;
2290 for (i = 0; i < mm_params->num_input_channels; i++)
2291 out->pan_scale_params.input_channel_map[i] =
2292 mm_params->input_channel_map[i];
2293
2294 out->pan_scale_params.has_mixer_coeffs = mm_params->has_mixer_coeffs;
2295 for (i = 0; i < mm_params->num_output_channels; i++)
2296 for (j = 0; j < mm_params->num_input_channels; j++) {
2297 //Convert the channel coefficient gains in Q14 format
2298 out->pan_scale_params.mixer_coeffs[i][j] =
2299 mm_params->mixer_coeffs[i][j] * (2 << 13);
2300 }
2301
2302 ret = platform_set_stream_pan_scale_params(out->dev->platform,
2303 out->pcm_device_id,
2304 out->pan_scale_params);
2305
2306exit:
2307 return ret;
2308}
2309
2310int audio_extn_utils_set_downmix_params(
2311 struct stream_out *out,
2312 struct mix_matrix_params *mm_params)
2313{
2314 int ret = -EINVAL, i = 0, j = 0;
2315 struct audio_usecase *usecase = NULL;
2316
Aniket Kumar Lataf9f246e2017-09-15 15:20:16 -07002317 if (mm_params == NULL || out == NULL) {
Varun Balaraj003ee752017-08-07 17:54:55 +05302318 ALOGE("%s:: Invalid mix matrix or out param", __func__);
Varun Balaraje49253e2017-07-06 19:48:56 +05302319 goto exit;
2320 }
2321
2322 if (mm_params->num_output_channels > MAX_CHANNELS_SUPPORTED ||
2323 mm_params->num_output_channels <= 0 ||
2324 mm_params->num_input_channels > MAX_CHANNELS_SUPPORTED ||
2325 mm_params->num_input_channels <= 0)
2326 goto exit;
2327
2328 usecase = get_usecase_from_list(out->dev, out->usecase);
Varun Balaraj003ee752017-08-07 17:54:55 +05302329 if (!usecase) {
2330 ALOGE("%s: Get usecase list failed!", __func__);
Aniket Kumar Lataf9f246e2017-09-15 15:20:16 -07002331 goto exit;
2332 }
Varun Balaraje49253e2017-07-06 19:48:56 +05302333 out->downmix_params.num_output_channels = mm_params->num_output_channels;
2334 out->downmix_params.num_input_channels = mm_params->num_input_channels;
2335
2336 out->downmix_params.has_output_channel_map =
2337 mm_params->has_output_channel_map;
2338 for (i = 0; i < mm_params->num_output_channels; i++) {
2339 out->downmix_params.output_channel_map[i] =
2340 mm_params->output_channel_map[i];
2341 }
2342
2343 out->downmix_params.has_input_channel_map =
2344 mm_params->has_input_channel_map;
2345 for (i = 0; i < mm_params->num_input_channels; i++)
2346 out->downmix_params.input_channel_map[i] =
2347 mm_params->input_channel_map[i];
2348
2349 out->downmix_params.has_mixer_coeffs = mm_params->has_mixer_coeffs;
2350 for (i = 0; i < mm_params->num_output_channels; i++)
Nikhil Latukar2e6f6242017-08-15 13:37:07 +05302351 for (j = 0; j < mm_params->num_input_channels; j++) {
2352 //Convert the channel coefficient gains in Q14 format
Varun Balaraje49253e2017-07-06 19:48:56 +05302353 out->downmix_params.mixer_coeffs[i][j] =
Nikhil Latukar2e6f6242017-08-15 13:37:07 +05302354 mm_params->mixer_coeffs[i][j] * (2 << 13);
2355 }
Varun Balaraje49253e2017-07-06 19:48:56 +05302356
2357 ret = platform_set_stream_downmix_params(out->dev->platform,
2358 out->pcm_device_id,
2359 usecase->out_snd_device,
2360 out->downmix_params);
2361
2362exit:
2363 return ret;
2364}
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05302365
2366bool audio_extn_utils_is_dolby_format(audio_format_t format)
2367{
2368 if (format == AUDIO_FORMAT_AC3 ||
2369 format == AUDIO_FORMAT_E_AC3 ||
2370 format == AUDIO_FORMAT_E_AC3_JOC)
2371 return true;
2372 else
2373 return false;
2374}
2375
2376